blob: 38ed3c9cf16e2c2345c8af2f71fd3e0a3ff51690 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
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 Hughes0d236aa2014-05-09 14:42:16 -070028
29#include <limits.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080030#include <pthread.h>
Elliott Hughes0d236aa2014-05-09 14:42:16 -070031#include <stdbool.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080032
Elliott Hughes0d236aa2014-05-09 14:42:16 -070033#include <asm/ldt.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080034
Elliott Hughes0d236aa2014-05-09 14:42:16 -070035extern int __set_thread_area(struct user_desc*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080036
Elliott Hughes0d236aa2014-05-09 14:42:16 -070037__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 Project1dc9e472009-03-03 19:28:35 -080047
Elliott Hughes0d236aa2014-05-09 14:42:16 -070048 result->base_addr = (uintptr_t) base_addr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080049
Elliott Hughes0d236aa2014-05-09 14:42:16 -070050 result->limit = PAGE_SIZE;
Jack Ren31e72bc2011-08-01 18:44:55 +080051
Elliott Hughes0d236aa2014-05-09 14:42:16 -070052 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 Project1dc9e472009-03-03 19:28:35 -080058}
59
Elliott Hughesa75869f2014-05-20 20:32:28 -070060__LIBC_HIDDEN__ int __set_tls(void* ptr) {
Elliott Hughes0d236aa2014-05-09 14:42:16 -070061 struct user_desc tls_descriptor;
62 __init_user_desc(&tls_descriptor, true, ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080063
Elliott Hughes0d236aa2014-05-09 14:42:16 -070064 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 Project1dc9e472009-03-03 19:28:35 -080072
Elliott Hughes0d236aa2014-05-09 14:42:16 -070073 return rc;
74}