Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 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 | */ |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "thread.h" |
| 18 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 19 | #include <sys/syscall.h> |
| 20 | #include <sys/types.h> |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 21 | |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 22 | #include "asm_support_x86.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 23 | #include "base/enums.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 24 | #include "base/macros.h" |
Brian Carlstrom | 8b31a36 | 2013-11-07 14:58:15 -0800 | [diff] [blame] | 25 | #include "thread-inl.h" |
Ian Rogers | 891f4a9 | 2012-02-03 16:04:54 -0800 | [diff] [blame] | 26 | #include "thread_list.h" |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 27 | |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 28 | #if defined(__APPLE__) |
| 29 | #include <architecture/i386/table.h> |
| 30 | #include <i386/user_ldt.h> |
Elliott Hughes | 42f54ad | 2012-04-21 23:23:26 -0700 | [diff] [blame] | 31 | struct descriptor_table_entry_t { |
| 32 | uint16_t limit0; |
| 33 | uint16_t base0; |
| 34 | unsigned base1: 8, type: 4, s: 1, dpl: 2, p: 1; |
| 35 | unsigned limit: 4, avl: 1, l: 1, d: 1, g: 1, base2: 8; |
| 36 | } __attribute__((packed)); |
| 37 | #define MODIFY_LDT_CONTENTS_DATA 0 |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 38 | #else |
| 39 | #include <asm/ldt.h> |
| 40 | #endif |
| 41 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 42 | namespace art { |
| 43 | |
| 44 | void Thread::InitCpu() { |
Chao-ying Fu | 9e36931 | 2014-05-21 11:20:52 -0700 | [diff] [blame] | 45 | // Take the ldt lock, Thread::Current isn't yet established. |
| 46 | MutexLock mu(nullptr, *Locks::modify_ldt_lock_); |
Ian Rogers | 891f4a9 | 2012-02-03 16:04:54 -0800 | [diff] [blame] | 47 | |
Elliott Hughes | 42f54ad | 2012-04-21 23:23:26 -0700 | [diff] [blame] | 48 | const uintptr_t base = reinterpret_cast<uintptr_t>(this); |
Andreas Gampe | 8a2c62c | 2016-02-16 15:58:20 -0800 | [diff] [blame] | 49 | const size_t limit = sizeof(Thread); |
Elliott Hughes | 42f54ad | 2012-04-21 23:23:26 -0700 | [diff] [blame] | 50 | |
| 51 | const int contents = MODIFY_LDT_CONTENTS_DATA; |
| 52 | const int seg_32bit = 1; |
| 53 | const int read_exec_only = 0; |
Andreas Gampe | 8a2c62c | 2016-02-16 15:58:20 -0800 | [diff] [blame] | 54 | const int limit_in_pages = 1; |
Elliott Hughes | 42f54ad | 2012-04-21 23:23:26 -0700 | [diff] [blame] | 55 | const int seg_not_present = 0; |
| 56 | const int useable = 1; |
| 57 | |
Andreas Gampe | 8a2c62c | 2016-02-16 15:58:20 -0800 | [diff] [blame] | 58 | int entry_number; |
| 59 | uint16_t table_indicator; |
Elliott Hughes | 42f54ad | 2012-04-21 23:23:26 -0700 | [diff] [blame] | 60 | |
| 61 | #if defined(__APPLE__) |
| 62 | descriptor_table_entry_t entry; |
| 63 | memset(&entry, 0, sizeof(entry)); |
| 64 | entry.limit0 = (limit & 0x0ffff); |
| 65 | entry.limit = (limit & 0xf0000) >> 16; |
| 66 | entry.base0 = (base & 0x0000ffff); |
| 67 | entry.base1 = (base & 0x00ff0000) >> 16; |
| 68 | entry.base2 = (base & 0xff000000) >> 24; |
| 69 | entry.type = ((read_exec_only ^ 1) << 1) | (contents << 2); |
| 70 | entry.s = 1; |
| 71 | entry.dpl = 0x3; |
| 72 | entry.p = seg_not_present ^ 1; |
| 73 | entry.avl = useable; |
| 74 | entry.l = 0; |
| 75 | entry.d = seg_32bit; |
| 76 | entry.g = limit_in_pages; |
| 77 | |
Brian Carlstrom | 2d88862 | 2013-07-18 17:02:00 -0700 | [diff] [blame] | 78 | entry_number = i386_set_ldt(LDT_AUTO_ALLOC, reinterpret_cast<ldt_entry*>(&entry), 1); |
Elliott Hughes | 42f54ad | 2012-04-21 23:23:26 -0700 | [diff] [blame] | 79 | if (entry_number == -1) { |
| 80 | PLOG(FATAL) << "i386_set_ldt failed"; |
| 81 | } |
Andreas Gampe | 8a2c62c | 2016-02-16 15:58:20 -0800 | [diff] [blame] | 82 | |
| 83 | table_indicator = 1 << 2; // LDT |
Elliott Hughes | 42f54ad | 2012-04-21 23:23:26 -0700 | [diff] [blame] | 84 | #else |
Andreas Gampe | 8a2c62c | 2016-02-16 15:58:20 -0800 | [diff] [blame] | 85 | // We use a GDT entry on Linux. |
| 86 | user_desc gdt_entry; |
| 87 | memset(&gdt_entry, 0, sizeof(gdt_entry)); |
Elliott Hughes | 42f54ad | 2012-04-21 23:23:26 -0700 | [diff] [blame] | 88 | |
Andreas Gampe | 8a2c62c | 2016-02-16 15:58:20 -0800 | [diff] [blame] | 89 | // On Linux, there are 3 TLS GDT entries. We use one of those to to store our segment descriptor |
| 90 | // data. |
| 91 | // |
| 92 | // This entry must be shared, as the kernel only guarantees three TLS entries. For simplicity |
| 93 | // (and locality), use this local global, which practically becomes readonly after the first |
| 94 | // (startup) thread of the runtime has been initialized (during Runtime::Start()). |
| 95 | // |
| 96 | // We also share this between all runtimes in the process. This is both for simplicity (one |
| 97 | // well-known slot) as well as to avoid the three-slot limitation. Downside is that we cannot |
| 98 | // free the slot when it is known that a runtime stops. |
| 99 | static unsigned int gdt_entry_number = -1; |
Elliott Hughes | 42f54ad | 2012-04-21 23:23:26 -0700 | [diff] [blame] | 100 | |
Andreas Gampe | 8a2c62c | 2016-02-16 15:58:20 -0800 | [diff] [blame] | 101 | if (gdt_entry_number == static_cast<unsigned int>(-1)) { |
| 102 | gdt_entry.entry_number = -1; // Let the kernel choose. |
| 103 | } else { |
| 104 | gdt_entry.entry_number = gdt_entry_number; |
| 105 | } |
| 106 | gdt_entry.base_addr = base; |
| 107 | gdt_entry.limit = limit; |
| 108 | gdt_entry.seg_32bit = seg_32bit; |
| 109 | gdt_entry.contents = contents; |
| 110 | gdt_entry.read_exec_only = read_exec_only; |
| 111 | gdt_entry.limit_in_pages = limit_in_pages; |
| 112 | gdt_entry.seg_not_present = seg_not_present; |
| 113 | gdt_entry.useable = useable; |
| 114 | int rc = syscall(__NR_set_thread_area, &gdt_entry); |
| 115 | if (rc != -1) { |
| 116 | entry_number = gdt_entry.entry_number; |
| 117 | if (gdt_entry_number == static_cast<unsigned int>(-1)) { |
| 118 | gdt_entry_number = entry_number; // Save the kernel-assigned entry number. |
| 119 | } |
| 120 | } else { |
| 121 | PLOG(FATAL) << "set_thread_area failed"; |
| 122 | UNREACHABLE(); |
| 123 | } |
| 124 | table_indicator = 0; // GDT |
Elliott Hughes | 42f54ad | 2012-04-21 23:23:26 -0700 | [diff] [blame] | 125 | #endif |
| 126 | |
Andreas Gampe | 8a2c62c | 2016-02-16 15:58:20 -0800 | [diff] [blame] | 127 | // Change %fs to be new DT entry. |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 128 | uint16_t rpl = 3; // Requested privilege level |
Elliott Hughes | 42f54ad | 2012-04-21 23:23:26 -0700 | [diff] [blame] | 129 | uint16_t selector = (entry_number << 3) | table_indicator | rpl; |
Elliott Hughes | 7834cbd | 2012-05-14 18:25:16 -0700 | [diff] [blame] | 130 | __asm__ __volatile__("movw %w0, %%fs" |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 131 | : // output |
| 132 | : "q"(selector) // input |
| 133 | :); // clobber |
Elliott Hughes | 42f54ad | 2012-04-21 23:23:26 -0700 | [diff] [blame] | 134 | |
| 135 | // Allow easy indirection back to Thread*. |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 136 | tlsPtr_.self = this; |
Elliott Hughes | 42f54ad | 2012-04-21 23:23:26 -0700 | [diff] [blame] | 137 | |
| 138 | // Sanity check that reads from %fs point to this Thread*. |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 139 | Thread* self_check; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 140 | CHECK_EQ(THREAD_SELF_OFFSET, SelfOffset<PointerSize::k32>().Int32Value()); |
Elliott Hughes | 7834cbd | 2012-05-14 18:25:16 -0700 | [diff] [blame] | 141 | __asm__ __volatile__("movl %%fs:(%1), %0" |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 142 | : "=r"(self_check) // output |
Ian Rogers | 9651f42 | 2011-09-19 20:26:07 -0700 | [diff] [blame] | 143 | : "r"(THREAD_SELF_OFFSET) // input |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 144 | :); // clobber |
| 145 | CHECK_EQ(self_check, this); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 146 | |
| 147 | // Sanity check other offsets. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 148 | CHECK_EQ(THREAD_EXCEPTION_OFFSET, ExceptionOffset<PointerSize::k32>().Int32Value()); |
| 149 | CHECK_EQ(THREAD_CARD_TABLE_OFFSET, CardTableOffset<PointerSize::k32>().Int32Value()); |
| 150 | CHECK_EQ(THREAD_ID_OFFSET, ThinLockIdOffset<PointerSize::k32>().Int32Value()); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Alexei Zavjalov | 1efa0a9 | 2014-02-04 02:08:31 +0700 | [diff] [blame] | 153 | void Thread::CleanupCpu() { |
Chao-ying Fu | 9e36931 | 2014-05-21 11:20:52 -0700 | [diff] [blame] | 154 | MutexLock mu(this, *Locks::modify_ldt_lock_); |
Alexei Zavjalov | 1efa0a9 | 2014-02-04 02:08:31 +0700 | [diff] [blame] | 155 | |
| 156 | // Sanity check that reads from %fs point to this Thread*. |
| 157 | Thread* self_check; |
| 158 | __asm__ __volatile__("movl %%fs:(%1), %0" |
| 159 | : "=r"(self_check) // output |
| 160 | : "r"(THREAD_SELF_OFFSET) // input |
| 161 | :); // clobber |
| 162 | CHECK_EQ(self_check, this); |
| 163 | |
| 164 | // Extract the LDT entry number from the FS register. |
| 165 | uint16_t selector; |
| 166 | __asm__ __volatile__("movw %%fs, %w0" |
| 167 | : "=q"(selector) // output |
| 168 | : // input |
| 169 | :); // clobber |
| 170 | |
| 171 | // Free LDT entry. |
| 172 | #if defined(__APPLE__) |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 173 | // TODO: release selectors on OS/X this is a leak which will cause ldt entries to be exhausted |
| 174 | // after enough threads are created. However, the following code results in kernel panics in OS/X |
| 175 | // 10.9. |
| 176 | UNUSED(selector); |
| 177 | // i386_set_ldt(selector >> 3, 0, 1); |
Alexei Zavjalov | 1efa0a9 | 2014-02-04 02:08:31 +0700 | [diff] [blame] | 178 | #else |
Andreas Gampe | 8a2c62c | 2016-02-16 15:58:20 -0800 | [diff] [blame] | 179 | // Note if we wanted to clean up the GDT entry, we would do that here, when the *last* thread |
| 180 | // is being deleted. But see the comment on gdt_entry_number. Code would look like this: |
| 181 | // |
| 182 | // user_desc gdt_entry; |
| 183 | // memset(&gdt_entry, 0, sizeof(gdt_entry)); |
| 184 | // gdt_entry.entry_number = selector >> 3; |
| 185 | // gdt_entry.contents = MODIFY_LDT_CONTENTS_DATA; |
| 186 | // // "Empty" = Delete = seg_not_present==1 && read_exec_only==1. |
| 187 | // gdt_entry.seg_not_present = 1; |
| 188 | // gdt_entry.read_exec_only = 1; |
| 189 | // syscall(__NR_set_thread_area, &gdt_entry); |
| 190 | UNUSED(selector); |
Alexei Zavjalov | 1efa0a9 | 2014-02-04 02:08:31 +0700 | [diff] [blame] | 191 | #endif |
| 192 | } |
| 193 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 194 | } // namespace art |