| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 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 |  */ | 
| Elliott Hughes | 0d236aa | 2014-05-09 14:42:16 -0700 | [diff] [blame] | 28 |  | 
 | 29 | #include <limits.h> | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 30 | #include <pthread.h> | 
 | 31 |  | 
| Elliott Hughes | 0d236aa | 2014-05-09 14:42:16 -0700 | [diff] [blame] | 32 | #include <asm/ldt.h> | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 33 |  | 
| Elliott Hughes | b291d08 | 2017-05-09 14:39:24 -0700 | [diff] [blame] | 34 | extern "C" int __set_thread_area(user_desc*); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 35 |  | 
| Elliott Hughes | b291d08 | 2017-05-09 14:39:24 -0700 | [diff] [blame] | 36 | __LIBC_HIDDEN__ void __init_user_desc(user_desc* result, bool allocate, void* base_addr) { | 
| Elliott Hughes | 0d236aa | 2014-05-09 14:42:16 -0700 | [diff] [blame] | 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 |   } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 46 |  | 
| Elliott Hughes | 01b85d5 | 2016-02-09 22:44:16 -0800 | [diff] [blame] | 47 |   result->base_addr = reinterpret_cast<uintptr_t>(base_addr); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 48 |  | 
| Elliott Hughes | 01b85d5 | 2016-02-09 22:44:16 -0800 | [diff] [blame] | 49 |   result->limit = 0xfffff; | 
| Jack Ren | 31e72bc | 2011-08-01 18:44:55 +0800 | [diff] [blame] | 50 |  | 
| Elliott Hughes | 0d236aa | 2014-05-09 14:42:16 -0700 | [diff] [blame] | 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; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 57 | } | 
 | 58 |  | 
| Elliott Hughes | 01b85d5 | 2016-02-09 22:44:16 -0800 | [diff] [blame] | 59 | extern "C" __LIBC_HIDDEN__ int __set_tls(void* ptr) { | 
| Elliott Hughes | b291d08 | 2017-05-09 14:39:24 -0700 | [diff] [blame] | 60 |   user_desc tls_descriptor = {}; | 
| Elliott Hughes | 0d236aa | 2014-05-09 14:42:16 -0700 | [diff] [blame] | 61 |   __init_user_desc(&tls_descriptor, true, ptr); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 62 |  | 
| Elliott Hughes | 0d236aa | 2014-05-09 14:42:16 -0700 | [diff] [blame] | 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 |   } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 71 |  | 
| Elliott Hughes | 0d236aa | 2014-05-09 14:42:16 -0700 | [diff] [blame] | 72 |   return rc; | 
 | 73 | } |