blob: 3d19f06092c3a4cec347a25ac6e7a6d43f4bcc23 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Rogersb033c752011-07-20 12:22:35 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "thread.h"
18
Ian Rogersb033c752011-07-20 12:22:35 -070019#include <sys/syscall.h>
20#include <sys/types.h>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070021
Ian Rogers7655f292013-07-29 11:07:13 -070022#include "asm_support_x86.h"
Elliott Hughes76160052012-12-12 16:31:20 -080023#include "base/macros.h"
Brian Carlstrom8b31a362013-11-07 14:58:15 -080024#include "thread-inl.h"
Ian Rogers891f4a92012-02-03 16:04:54 -080025#include "thread_list.h"
Ian Rogersb033c752011-07-20 12:22:35 -070026
Elliott Hughesad6c9c32012-01-19 17:39:12 -080027#if defined(__APPLE__)
28#include <architecture/i386/table.h>
29#include <i386/user_ldt.h>
Elliott Hughes42f54ad2012-04-21 23:23:26 -070030struct 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
Elliott Hughesad6c9c32012-01-19 17:39:12 -080037#else
38#include <asm/ldt.h>
39#endif
40
Ian Rogersb033c752011-07-20 12:22:35 -070041namespace art {
42
43void Thread::InitCpu() {
Chao-ying Fu9e369312014-05-21 11:20:52 -070044 // Take the ldt lock, Thread::Current isn't yet established.
45 MutexLock mu(nullptr, *Locks::modify_ldt_lock_);
Ian Rogers891f4a92012-02-03 16:04:54 -080046
Elliott Hughes42f54ad2012-04-21 23:23:26 -070047 const uintptr_t base = reinterpret_cast<uintptr_t>(this);
48 const size_t limit = kPageSize;
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 = 0;
54 const int seg_not_present = 0;
55 const int useable = 1;
56
57 int entry_number = -1;
58
59#if defined(__APPLE__)
60 descriptor_table_entry_t entry;
61 memset(&entry, 0, sizeof(entry));
62 entry.limit0 = (limit & 0x0ffff);
63 entry.limit = (limit & 0xf0000) >> 16;
64 entry.base0 = (base & 0x0000ffff);
65 entry.base1 = (base & 0x00ff0000) >> 16;
66 entry.base2 = (base & 0xff000000) >> 24;
67 entry.type = ((read_exec_only ^ 1) << 1) | (contents << 2);
68 entry.s = 1;
69 entry.dpl = 0x3;
70 entry.p = seg_not_present ^ 1;
71 entry.avl = useable;
72 entry.l = 0;
73 entry.d = seg_32bit;
74 entry.g = limit_in_pages;
75
Brian Carlstrom2d888622013-07-18 17:02:00 -070076 entry_number = i386_set_ldt(LDT_AUTO_ALLOC, reinterpret_cast<ldt_entry*>(&entry), 1);
Elliott Hughes42f54ad2012-04-21 23:23:26 -070077 if (entry_number == -1) {
78 PLOG(FATAL) << "i386_set_ldt failed";
79 }
80#else
81 // Read current LDT entries.
Roland Levillain33d69032015-06-18 18:20:59 +010082 static_assert(static_cast<size_t>(LDT_ENTRY_SIZE) == sizeof(uint64_t),
83 "LDT_ENTRY_SIZE is different from sizeof(uint64_t).");
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070084 std::vector<uint64_t> ldt(LDT_ENTRIES);
85 size_t ldt_size(sizeof(uint64_t) * ldt.size());
86 memset(&ldt[0], 0, ldt_size);
Elliott Hughes42f54ad2012-04-21 23:23:26 -070087 // TODO: why doesn't this return LDT_ENTRY_SIZE * LDT_ENTRIES for the main thread?
Elliott Hughes942df412012-03-26 09:46:56 -070088 syscall(__NR_modify_ldt, 0, &ldt[0], ldt_size);
Elliott Hughes42f54ad2012-04-21 23:23:26 -070089
90 // Find the first empty slot.
91 for (entry_number = 0; entry_number < LDT_ENTRIES && ldt[entry_number] != 0; ++entry_number) {
92 }
93 if (entry_number >= LDT_ENTRIES) {
94 LOG(FATAL) << "Failed to find a free LDT slot";
95 }
96
97 // Update LDT entry.
Elliott Hughes7f40ffc2011-09-04 10:50:01 -070098 user_desc ldt_entry;
99 memset(&ldt_entry, 0, sizeof(ldt_entry));
Elliott Hughes42f54ad2012-04-21 23:23:26 -0700100 ldt_entry.entry_number = entry_number;
101 ldt_entry.base_addr = base;
102 ldt_entry.limit = limit;
103 ldt_entry.seg_32bit = seg_32bit;
104 ldt_entry.contents = contents;
105 ldt_entry.read_exec_only = read_exec_only;
106 ldt_entry.limit_in_pages = limit_in_pages;
107 ldt_entry.seg_not_present = seg_not_present;
108 ldt_entry.useable = useable;
Elliott Hughes942df412012-03-26 09:46:56 -0700109 CHECK_EQ(0, syscall(__NR_modify_ldt, 1, &ldt_entry, sizeof(ldt_entry)));
Elliott Hughes42f54ad2012-04-21 23:23:26 -0700110 entry_number = ldt_entry.entry_number;
111#endif
112
113 // Change %fs to be new LDT entry.
Ian Rogersb033c752011-07-20 12:22:35 -0700114 uint16_t table_indicator = 1 << 2; // LDT
115 uint16_t rpl = 3; // Requested privilege level
Elliott Hughes42f54ad2012-04-21 23:23:26 -0700116 uint16_t selector = (entry_number << 3) | table_indicator | rpl;
Elliott Hughes7834cbd2012-05-14 18:25:16 -0700117 __asm__ __volatile__("movw %w0, %%fs"
Ian Rogersb033c752011-07-20 12:22:35 -0700118 : // output
119 : "q"(selector) // input
120 :); // clobber
Elliott Hughes42f54ad2012-04-21 23:23:26 -0700121
122 // Allow easy indirection back to Thread*.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700123 tlsPtr_.self = this;
Elliott Hughes42f54ad2012-04-21 23:23:26 -0700124
125 // Sanity check that reads from %fs point to this Thread*.
Ian Rogersb033c752011-07-20 12:22:35 -0700126 Thread* self_check;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700127 CHECK_EQ(THREAD_SELF_OFFSET, SelfOffset<4>().Int32Value());
Elliott Hughes7834cbd2012-05-14 18:25:16 -0700128 __asm__ __volatile__("movl %%fs:(%1), %0"
Ian Rogersb033c752011-07-20 12:22:35 -0700129 : "=r"(self_check) // output
Ian Rogers9651f422011-09-19 20:26:07 -0700130 : "r"(THREAD_SELF_OFFSET) // input
Ian Rogersb033c752011-07-20 12:22:35 -0700131 :); // clobber
132 CHECK_EQ(self_check, this);
Ian Rogers0399dde2012-06-06 17:09:28 -0700133
134 // Sanity check other offsets.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700135 CHECK_EQ(THREAD_EXCEPTION_OFFSET, ExceptionOffset<4>().Int32Value());
136 CHECK_EQ(THREAD_CARD_TABLE_OFFSET, CardTableOffset<4>().Int32Value());
137 CHECK_EQ(THREAD_ID_OFFSET, ThinLockIdOffset<4>().Int32Value());
Ian Rogersb033c752011-07-20 12:22:35 -0700138}
139
Alexei Zavjalov1efa0a92014-02-04 02:08:31 +0700140void Thread::CleanupCpu() {
Chao-ying Fu9e369312014-05-21 11:20:52 -0700141 MutexLock mu(this, *Locks::modify_ldt_lock_);
Alexei Zavjalov1efa0a92014-02-04 02:08:31 +0700142
143 // Sanity check that reads from %fs point to this Thread*.
144 Thread* self_check;
145 __asm__ __volatile__("movl %%fs:(%1), %0"
146 : "=r"(self_check) // output
147 : "r"(THREAD_SELF_OFFSET) // input
148 :); // clobber
149 CHECK_EQ(self_check, this);
150
151 // Extract the LDT entry number from the FS register.
152 uint16_t selector;
153 __asm__ __volatile__("movw %%fs, %w0"
154 : "=q"(selector) // output
155 : // input
156 :); // clobber
157
158 // Free LDT entry.
159#if defined(__APPLE__)
Ian Rogersc5f17732014-06-05 20:48:42 -0700160 // TODO: release selectors on OS/X this is a leak which will cause ldt entries to be exhausted
161 // after enough threads are created. However, the following code results in kernel panics in OS/X
162 // 10.9.
163 UNUSED(selector);
164 // i386_set_ldt(selector >> 3, 0, 1);
Alexei Zavjalov1efa0a92014-02-04 02:08:31 +0700165#else
166 user_desc ldt_entry;
167 memset(&ldt_entry, 0, sizeof(ldt_entry));
168 ldt_entry.entry_number = selector >> 3;
169 ldt_entry.contents = MODIFY_LDT_CONTENTS_DATA;
170 ldt_entry.seg_not_present = 1;
171
172 syscall(__NR_modify_ldt, 1, &ldt_entry, sizeof(ldt_entry));
173#endif
174}
175
Ian Rogersb033c752011-07-20 12:22:35 -0700176} // namespace art