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> |
Elliott Hughes | 0d236aa | 2014-05-09 14:42:16 -0700 | [diff] [blame] | 31 | #include <stdbool.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 32 | |
Elliott Hughes | 0d236aa | 2014-05-09 14:42:16 -0700 | [diff] [blame] | 33 | #include <asm/ldt.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 34 | |
Elliott Hughes | 0d236aa | 2014-05-09 14:42:16 -0700 | [diff] [blame] | 35 | extern int __set_thread_area(struct user_desc*); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 36 | |
Elliott Hughes | 0d236aa | 2014-05-09 14:42:16 -0700 | [diff] [blame] | 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 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 47 | |
Elliott Hughes | 0d236aa | 2014-05-09 14:42:16 -0700 | [diff] [blame] | 48 | result->base_addr = (uintptr_t) base_addr; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 49 | |
Elliott Hughes | 0d236aa | 2014-05-09 14:42:16 -0700 | [diff] [blame] | 50 | result->limit = PAGE_SIZE; |
Jack Ren | 31e72bc | 2011-08-01 18:44:55 +0800 | [diff] [blame] | 51 | |
Elliott Hughes | 0d236aa | 2014-05-09 14:42:16 -0700 | [diff] [blame] | 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; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Elliott Hughes | a75869f | 2014-05-20 20:32:28 -0700 | [diff] [blame] | 60 | __LIBC_HIDDEN__ int __set_tls(void* ptr) { |
Elliott Hughes | 0d236aa | 2014-05-09 14:42:16 -0700 | [diff] [blame] | 61 | struct user_desc tls_descriptor; |
| 62 | __init_user_desc(&tls_descriptor, true, ptr); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 63 | |
Elliott Hughes | 0d236aa | 2014-05-09 14:42:16 -0700 | [diff] [blame] | 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 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 72 | |
Elliott Hughes | 0d236aa | 2014-05-09 14:42:16 -0700 | [diff] [blame] | 73 | return rc; |
| 74 | } |