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> |
| 29 | #else |
| 30 | #include <asm/ldt.h> |
| 31 | #endif |
| 32 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 33 | namespace art { |
| 34 | |
| 35 | void Thread::InitCpu() { |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 36 | #if defined(__APPLE__) |
| 37 | UNIMPLEMENTED(WARNING); |
| 38 | #else |
Elliott Hughes | 8323972 | 2012-02-03 16:49:24 -0800 | [diff] [blame^] | 39 | static Mutex modify_ldt_lock("modify_ldt lock"); |
| 40 | MutexLock mu(modify_ldt_lock); |
Ian Rogers | 891f4a9 | 2012-02-03 16:04:54 -0800 | [diff] [blame] | 41 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 42 | // Read LDT |
| 43 | CHECK_EQ((size_t)LDT_ENTRY_SIZE, sizeof(uint64_t)); |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 44 | std::vector<uint64_t> ldt(LDT_ENTRIES); |
| 45 | size_t ldt_size(sizeof(uint64_t) * ldt.size()); |
| 46 | memset(&ldt[0], 0, ldt_size); |
| 47 | syscall(SYS_modify_ldt, 0, &ldt[0], ldt_size); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 48 | // Create empty slot to point at current Thread* |
Elliott Hughes | 7f40ffc | 2011-09-04 10:50:01 -0700 | [diff] [blame] | 49 | user_desc ldt_entry; |
| 50 | memset(&ldt_entry, 0, sizeof(ldt_entry)); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 51 | ldt_entry.entry_number = -1; |
| 52 | ldt_entry.base_addr = (unsigned int)this; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 53 | ldt_entry.limit = kPageSize; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 54 | ldt_entry.seg_32bit = 1; |
| 55 | ldt_entry.contents = MODIFY_LDT_CONTENTS_DATA; |
| 56 | ldt_entry.read_exec_only = 0; |
| 57 | ldt_entry.limit_in_pages = 0; |
| 58 | ldt_entry.seg_not_present = 0; |
| 59 | ldt_entry.useable = 1; |
| 60 | for (int i = 0; i < LDT_ENTRIES; i++) { |
Elliott Hughes | 7f40ffc | 2011-09-04 10:50:01 -0700 | [diff] [blame] | 61 | if (ldt[i] == 0) { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 62 | ldt_entry.entry_number = i; |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | if (ldt_entry.entry_number >= LDT_ENTRIES) { |
| 67 | LOG(FATAL) << "Failed to find available LDT slot"; |
| 68 | } |
| 69 | // Update LDT |
| 70 | CHECK_EQ(0, syscall(SYS_modify_ldt, 1, &ldt_entry, sizeof(ldt_entry))); |
| 71 | // Change FS to be new LDT entry |
| 72 | uint16_t table_indicator = 1 << 2; // LDT |
| 73 | uint16_t rpl = 3; // Requested privilege level |
| 74 | uint16_t selector = (ldt_entry.entry_number << 3) | table_indicator | rpl; |
| 75 | // TODO: use our assembler to generate code |
Elliott Hughes | 7f40ffc | 2011-09-04 10:50:01 -0700 | [diff] [blame] | 76 | asm volatile("movw %w0, %%fs" |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 77 | : // output |
| 78 | : "q"(selector) // input |
| 79 | :); // clobber |
| 80 | // Allow easy indirection back to Thread* |
| 81 | self_ = this; |
| 82 | // Sanity check reads from FS goes to this Thread* |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 83 | Thread* self_check; |
| 84 | // TODO: use our assembler to generate code |
Ian Rogers | 9651f42 | 2011-09-19 20:26:07 -0700 | [diff] [blame] | 85 | CHECK_EQ(THREAD_SELF_OFFSET, OFFSETOF_MEMBER(Thread, self_)); |
Elliott Hughes | 7f40ffc | 2011-09-04 10:50:01 -0700 | [diff] [blame] | 86 | asm volatile("movl %%fs:(%1), %0" |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 87 | : "=r"(self_check) // output |
Ian Rogers | 9651f42 | 2011-09-19 20:26:07 -0700 | [diff] [blame] | 88 | : "r"(THREAD_SELF_OFFSET) // input |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 89 | :); // clobber |
| 90 | CHECK_EQ(self_check, this); |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 91 | #endif |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | } // namespace art |