1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "thread.h"
18
19#include <sys/syscall.h>
20#include <sys/types.h>
21
22#include "asm_support_x86.h"
23#include "base/macros.h"
24#include "thread-inl.h"
25#include "thread_list.h"
26
27#if defined(__APPLE__)
28#include <architecture/i386/table.h>
29#include <i386/user_ldt.h>
30struct descriptor_table_entry_t {
31  uint16_t limit0;
32  uint16_t base0;
33  unsigned base1: 8, type: 4, s: 1, dpl: 2, p: 1;
34  unsigned limit: 4, avl: 1, l: 1, d: 1, g: 1, base2: 8;
35} __attribute__((packed));
36#define MODIFY_LDT_CONTENTS_DATA 0
37#else
38#include <asm/ldt.h>
39#endif
40
41namespace art {
42
43void Thread::InitCpu() {
44  // Take the ldt lock, Thread::Current isn't yet established.
45  MutexLock mu(nullptr, *Locks::modify_ldt_lock_);
46
47  const uintptr_t base = reinterpret_cast<uintptr_t>(this);
48  const size_t limit = sizeof(Thread);
49
50  const int contents = MODIFY_LDT_CONTENTS_DATA;
51  const int seg_32bit = 1;
52  const int read_exec_only = 0;
53  const int limit_in_pages = 1;
54  const int seg_not_present = 0;
55  const int useable = 1;
56
57  int entry_number;
58  uint16_t table_indicator;
59
60#if defined(__APPLE__)
61  descriptor_table_entry_t entry;
62  memset(&entry, 0, sizeof(entry));
63  entry.limit0 = (limit & 0x0ffff);
64  entry.limit  = (limit & 0xf0000) >> 16;
65  entry.base0 = (base & 0x0000ffff);
66  entry.base1 = (base & 0x00ff0000) >> 16;
67  entry.base2 = (base & 0xff000000) >> 24;
68  entry.type = ((read_exec_only ^ 1) << 1) | (contents << 2);
69  entry.s = 1;
70  entry.dpl = 0x3;
71  entry.p = seg_not_present ^ 1;
72  entry.avl = useable;
73  entry.l = 0;
74  entry.d = seg_32bit;
75  entry.g = limit_in_pages;
76
77  entry_number = i386_set_ldt(LDT_AUTO_ALLOC, reinterpret_cast<ldt_entry*>(&entry), 1);
78  if (entry_number == -1) {
79    PLOG(FATAL) << "i386_set_ldt failed";
80  }
81
82  table_indicator = 1 << 2;  // LDT
83#else
84  // We use a GDT entry on Linux.
85  user_desc gdt_entry;
86  memset(&gdt_entry, 0, sizeof(gdt_entry));
87
88  // On Linux, there are 3 TLS GDT entries. We use one of those to to store our segment descriptor
89  // data.
90  //
91  // This entry must be shared, as the kernel only guarantees three TLS entries. For simplicity
92  // (and locality), use this local global, which practically becomes readonly after the first
93  // (startup) thread of the runtime has been initialized (during Runtime::Start()).
94  //
95  // We also share this between all runtimes in the process. This is both for simplicity (one
96  // well-known slot) as well as to avoid the three-slot limitation. Downside is that we cannot
97  // free the slot when it is known that a runtime stops.
98  static unsigned int gdt_entry_number = -1;
99
100  if (gdt_entry_number == static_cast<unsigned int>(-1)) {
101    gdt_entry.entry_number = -1;  // Let the kernel choose.
102  } else {
103    gdt_entry.entry_number = gdt_entry_number;
104  }
105  gdt_entry.base_addr = base;
106  gdt_entry.limit = limit;
107  gdt_entry.seg_32bit = seg_32bit;
108  gdt_entry.contents = contents;
109  gdt_entry.read_exec_only = read_exec_only;
110  gdt_entry.limit_in_pages = limit_in_pages;
111  gdt_entry.seg_not_present = seg_not_present;
112  gdt_entry.useable = useable;
113  int rc = syscall(__NR_set_thread_area, &gdt_entry);
114  if (rc != -1) {
115    entry_number = gdt_entry.entry_number;
116    if (gdt_entry_number == static_cast<unsigned int>(-1)) {
117      gdt_entry_number = entry_number;  // Save the kernel-assigned entry number.
118    }
119  } else {
120    PLOG(FATAL) << "set_thread_area failed";
121    UNREACHABLE();
122  }
123  table_indicator = 0;  // GDT
124#endif
125
126  // Change %fs to be new DT entry.
127  uint16_t rpl = 3;  // Requested privilege level
128  uint16_t selector = (entry_number << 3) | table_indicator | rpl;
129  __asm__ __volatile__("movw %w0, %%fs"
130      :    // output
131      : "q"(selector)  // input
132      :);  // clobber
133
134  // Allow easy indirection back to Thread*.
135  tlsPtr_.self = this;
136
137  // Sanity check that reads from %fs point to this Thread*.
138  Thread* self_check;
139  CHECK_EQ(THREAD_SELF_OFFSET, SelfOffset<4>().Int32Value());
140  __asm__ __volatile__("movl %%fs:(%1), %0"
141      : "=r"(self_check)  // output
142      : "r"(THREAD_SELF_OFFSET)  // input
143      :);  // clobber
144  CHECK_EQ(self_check, this);
145
146  // Sanity check other offsets.
147  CHECK_EQ(THREAD_EXCEPTION_OFFSET, ExceptionOffset<4>().Int32Value());
148  CHECK_EQ(THREAD_CARD_TABLE_OFFSET, CardTableOffset<4>().Int32Value());
149  CHECK_EQ(THREAD_ID_OFFSET, ThinLockIdOffset<4>().Int32Value());
150}
151
152void Thread::CleanupCpu() {
153  MutexLock mu(this, *Locks::modify_ldt_lock_);
154
155  // Sanity check that reads from %fs point to this Thread*.
156  Thread* self_check;
157  __asm__ __volatile__("movl %%fs:(%1), %0"
158      : "=r"(self_check)  // output
159      : "r"(THREAD_SELF_OFFSET)  // input
160      :);  // clobber
161  CHECK_EQ(self_check, this);
162
163  // Extract the LDT entry number from the FS register.
164  uint16_t selector;
165  __asm__ __volatile__("movw %%fs, %w0"
166      : "=q"(selector)  // output
167      :  // input
168      :);  // clobber
169
170  // Free LDT entry.
171#if defined(__APPLE__)
172  // TODO: release selectors on OS/X this is a leak which will cause ldt entries to be exhausted
173  // after enough threads are created. However, the following code results in kernel panics in OS/X
174  // 10.9.
175  UNUSED(selector);
176  // i386_set_ldt(selector >> 3, 0, 1);
177#else
178  // Note if we wanted to clean up the GDT entry, we would do that here, when the *last* thread
179  // is being deleted. But see the comment on gdt_entry_number. Code would look like this:
180  //
181  // user_desc gdt_entry;
182  // memset(&gdt_entry, 0, sizeof(gdt_entry));
183  // gdt_entry.entry_number = selector >> 3;
184  // gdt_entry.contents = MODIFY_LDT_CONTENTS_DATA;
185  // // "Empty" = Delete = seg_not_present==1 && read_exec_only==1.
186  // gdt_entry.seg_not_present = 1;
187  // gdt_entry.read_exec_only = 1;
188  // syscall(__NR_set_thread_area, &gdt_entry);
189  UNUSED(selector);
190#endif
191}
192
193}  // namespace art
194