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 | 9651f42 | 2011-09-19 20:26:07 -0700 | [diff] [blame] | 22 | #include "asm_support.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 23 | #include "macros.h" |
Ian Rogers | 891f4a9 | 2012-02-03 16:04:54 -0800 | [diff] [blame] | 24 | #include "thread_list.h" |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 25 | |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 26 | #if defined(__APPLE__) |
| 27 | #include <architecture/i386/table.h> |
| 28 | #include <i386/user_ldt.h> |
Elliott Hughes | 42f54ad | 2012-04-21 23:23:26 -0700 | [diff] [blame] | 29 | struct descriptor_table_entry_t { |
| 30 | uint16_t limit0; |
| 31 | uint16_t base0; |
| 32 | unsigned base1: 8, type: 4, s: 1, dpl: 2, p: 1; |
| 33 | unsigned limit: 4, avl: 1, l: 1, d: 1, g: 1, base2: 8; |
| 34 | } __attribute__((packed)); |
| 35 | #define MODIFY_LDT_CONTENTS_DATA 0 |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 36 | #else |
| 37 | #include <asm/ldt.h> |
| 38 | #endif |
| 39 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 40 | namespace art { |
| 41 | |
| 42 | void Thread::InitCpu() { |
Elliott Hughes | 8323972 | 2012-02-03 16:49:24 -0800 | [diff] [blame] | 43 | static Mutex modify_ldt_lock("modify_ldt lock"); |
| 44 | MutexLock mu(modify_ldt_lock); |
Ian Rogers | 891f4a9 | 2012-02-03 16:04:54 -0800 | [diff] [blame] | 45 | |
Elliott Hughes | 42f54ad | 2012-04-21 23:23:26 -0700 | [diff] [blame] | 46 | const uintptr_t base = reinterpret_cast<uintptr_t>(this); |
| 47 | const size_t limit = kPageSize; |
| 48 | |
| 49 | const int contents = MODIFY_LDT_CONTENTS_DATA; |
| 50 | const int seg_32bit = 1; |
| 51 | const int read_exec_only = 0; |
| 52 | const int limit_in_pages = 0; |
| 53 | const int seg_not_present = 0; |
| 54 | const int useable = 1; |
| 55 | |
| 56 | int entry_number = -1; |
| 57 | |
| 58 | #if defined(__APPLE__) |
| 59 | descriptor_table_entry_t entry; |
| 60 | memset(&entry, 0, sizeof(entry)); |
| 61 | entry.limit0 = (limit & 0x0ffff); |
| 62 | entry.limit = (limit & 0xf0000) >> 16; |
| 63 | entry.base0 = (base & 0x0000ffff); |
| 64 | entry.base1 = (base & 0x00ff0000) >> 16; |
| 65 | entry.base2 = (base & 0xff000000) >> 24; |
| 66 | entry.type = ((read_exec_only ^ 1) << 1) | (contents << 2); |
| 67 | entry.s = 1; |
| 68 | entry.dpl = 0x3; |
| 69 | entry.p = seg_not_present ^ 1; |
| 70 | entry.avl = useable; |
| 71 | entry.l = 0; |
| 72 | entry.d = seg_32bit; |
| 73 | entry.g = limit_in_pages; |
| 74 | |
| 75 | entry_number = i386_set_ldt(LDT_AUTO_ALLOC, (ldt_entry*)(void*)(&entry), 1); |
| 76 | if (entry_number == -1) { |
| 77 | PLOG(FATAL) << "i386_set_ldt failed"; |
| 78 | } |
| 79 | #else |
| 80 | // Read current LDT entries. |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 81 | CHECK_EQ((size_t)LDT_ENTRY_SIZE, sizeof(uint64_t)); |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 82 | std::vector<uint64_t> ldt(LDT_ENTRIES); |
| 83 | size_t ldt_size(sizeof(uint64_t) * ldt.size()); |
| 84 | memset(&ldt[0], 0, ldt_size); |
Elliott Hughes | 42f54ad | 2012-04-21 23:23:26 -0700 | [diff] [blame] | 85 | // TODO: why doesn't this return LDT_ENTRY_SIZE * LDT_ENTRIES for the main thread? |
Elliott Hughes | 942df41 | 2012-03-26 09:46:56 -0700 | [diff] [blame] | 86 | syscall(__NR_modify_ldt, 0, &ldt[0], ldt_size); |
Elliott Hughes | 42f54ad | 2012-04-21 23:23:26 -0700 | [diff] [blame] | 87 | |
| 88 | // Find the first empty slot. |
| 89 | for (entry_number = 0; entry_number < LDT_ENTRIES && ldt[entry_number] != 0; ++entry_number) { |
| 90 | } |
| 91 | if (entry_number >= LDT_ENTRIES) { |
| 92 | LOG(FATAL) << "Failed to find a free LDT slot"; |
| 93 | } |
| 94 | |
| 95 | // Update LDT entry. |
Elliott Hughes | 7f40ffc | 2011-09-04 10:50:01 -0700 | [diff] [blame] | 96 | user_desc ldt_entry; |
| 97 | memset(&ldt_entry, 0, sizeof(ldt_entry)); |
Elliott Hughes | 42f54ad | 2012-04-21 23:23:26 -0700 | [diff] [blame] | 98 | ldt_entry.entry_number = entry_number; |
| 99 | ldt_entry.base_addr = base; |
| 100 | ldt_entry.limit = limit; |
| 101 | ldt_entry.seg_32bit = seg_32bit; |
| 102 | ldt_entry.contents = contents; |
| 103 | ldt_entry.read_exec_only = read_exec_only; |
| 104 | ldt_entry.limit_in_pages = limit_in_pages; |
| 105 | ldt_entry.seg_not_present = seg_not_present; |
| 106 | ldt_entry.useable = useable; |
Elliott Hughes | 942df41 | 2012-03-26 09:46:56 -0700 | [diff] [blame] | 107 | CHECK_EQ(0, syscall(__NR_modify_ldt, 1, &ldt_entry, sizeof(ldt_entry))); |
Elliott Hughes | 42f54ad | 2012-04-21 23:23:26 -0700 | [diff] [blame] | 108 | entry_number = ldt_entry.entry_number; |
| 109 | #endif |
| 110 | |
| 111 | // Change %fs to be new LDT entry. |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 112 | uint16_t table_indicator = 1 << 2; // LDT |
| 113 | uint16_t rpl = 3; // Requested privilege level |
Elliott Hughes | 42f54ad | 2012-04-21 23:23:26 -0700 | [diff] [blame] | 114 | uint16_t selector = (entry_number << 3) | table_indicator | rpl; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 115 | // TODO: use our assembler to generate code |
Elliott Hughes | 7834cbd | 2012-05-14 18:25:16 -0700 | [diff] [blame] | 116 | __asm__ __volatile__("movw %w0, %%fs" |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 117 | : // output |
| 118 | : "q"(selector) // input |
| 119 | :); // clobber |
Elliott Hughes | 42f54ad | 2012-04-21 23:23:26 -0700 | [diff] [blame] | 120 | |
| 121 | // Allow easy indirection back to Thread*. |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 122 | self_ = this; |
Elliott Hughes | 42f54ad | 2012-04-21 23:23:26 -0700 | [diff] [blame] | 123 | |
| 124 | // Sanity check that reads from %fs point to this Thread*. |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 125 | Thread* self_check; |
| 126 | // TODO: use our assembler to generate code |
Ian Rogers | 9651f42 | 2011-09-19 20:26:07 -0700 | [diff] [blame] | 127 | CHECK_EQ(THREAD_SELF_OFFSET, OFFSETOF_MEMBER(Thread, self_)); |
Elliott Hughes | 7834cbd | 2012-05-14 18:25:16 -0700 | [diff] [blame] | 128 | __asm__ __volatile__("movl %%fs:(%1), %0" |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 129 | : "=r"(self_check) // output |
Ian Rogers | 9651f42 | 2011-09-19 20:26:07 -0700 | [diff] [blame] | 130 | : "r"(THREAD_SELF_OFFSET) // input |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 131 | :); // clobber |
| 132 | CHECK_EQ(self_check, this); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | } // namespace art |