• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <limits.h>
30 #include <pthread.h>
31 #include <stdbool.h>
32 
33 #include <asm/ldt.h>
34 
35 extern int __set_thread_area(struct user_desc*);
36 
__init_user_desc(struct user_desc * result,bool allocate,void * base_addr)37 __LIBC_HIDDEN__ void __init_user_desc(struct user_desc* result, bool allocate, void* base_addr) {
38   if (allocate) {
39     // Let the kernel choose.
40     result->entry_number = -1;
41   } else {
42     // Get the existing entry number from %gs.
43     uint32_t gs;
44     __asm__ __volatile__("movw %%gs, %w0" : "=q"(gs) /*output*/);
45     result->entry_number = (gs & 0xffff) >> 3;
46   }
47 
48   result->base_addr = (uintptr_t) base_addr;
49 
50   result->limit = PAGE_SIZE;
51 
52   result->seg_32bit = 1;
53   result->contents = MODIFY_LDT_CONTENTS_DATA;
54   result->read_exec_only = 0;
55   result->limit_in_pages = 1;
56   result->seg_not_present = 0;
57   result->useable = 1;
58 }
59 
__set_tls(void * ptr)60 __LIBC_HIDDEN__ int __set_tls(void* ptr) {
61   struct user_desc tls_descriptor;
62   __init_user_desc(&tls_descriptor, true, ptr);
63 
64   int rc = __set_thread_area(&tls_descriptor);
65   if (rc != -1) {
66     // Change %gs to be new GDT entry.
67     uint16_t table_indicator = 0;  // GDT
68     uint16_t rpl = 3;  // Requested privilege level
69     uint16_t selector = (tls_descriptor.entry_number << 3) | table_indicator | rpl;
70     __asm__ __volatile__("movw %w0, %%gs" : /*output*/ : "q"(selector) /*input*/ : /*clobber*/);
71   }
72 
73   return rc;
74 }
75