Home | History | Annotate | Download | only in bionic
      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 
     32 #include <asm/ldt.h>
     33 
     34 extern "C" int __set_thread_area(user_desc*);
     35 
     36 __LIBC_HIDDEN__ void __init_user_desc(user_desc* result, bool allocate, void* base_addr) {
     37   if (allocate) {
     38     // Let the kernel choose.
     39     result->entry_number = -1;
     40   } else {
     41     // Get the existing entry number from %gs.
     42     uint32_t gs;
     43     __asm__ __volatile__("movw %%gs, %w0" : "=q"(gs) /*output*/);
     44     result->entry_number = (gs & 0xffff) >> 3;
     45   }
     46 
     47   result->base_addr = reinterpret_cast<uintptr_t>(base_addr);
     48 
     49   result->limit = 0xfffff;
     50 
     51   result->seg_32bit = 1;
     52   result->contents = MODIFY_LDT_CONTENTS_DATA;
     53   result->read_exec_only = 0;
     54   result->limit_in_pages = 1;
     55   result->seg_not_present = 0;
     56   result->useable = 1;
     57 }
     58 
     59 extern "C" __LIBC_HIDDEN__ int __set_tls(void* ptr) {
     60   user_desc tls_descriptor = {};
     61   __init_user_desc(&tls_descriptor, true, ptr);
     62 
     63   int rc = __set_thread_area(&tls_descriptor);
     64   if (rc != -1) {
     65     // Change %gs to be new GDT entry.
     66     uint16_t table_indicator = 0;  // GDT
     67     uint16_t rpl = 3;  // Requested privilege level
     68     uint16_t selector = (tls_descriptor.entry_number << 3) | table_indicator | rpl;
     69     __asm__ __volatile__("movw %w0, %%gs" : /*output*/ : "q"(selector) /*input*/ : /*clobber*/);
     70   }
     71 
     72   return rc;
     73 }
     74