blob: cdba1389539a1c8d1e6bb794cf1e27c170415c82 [file] [log] [blame]
Ian Rogersb033c752011-07-20 12:22:35 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "thread.h"
4
Ian Rogersb033c752011-07-20 12:22:35 -07005#include <sys/syscall.h>
6#include <sys/types.h>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07007
Ian Rogers9651f422011-09-19 20:26:07 -07008#include "asm_support.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07009#include "macros.h"
Ian Rogersb033c752011-07-20 12:22:35 -070010
Elliott Hughesad6c9c32012-01-19 17:39:12 -080011#if defined(__APPLE__)
12#include <architecture/i386/table.h>
13#include <i386/user_ldt.h>
14#else
15#include <asm/ldt.h>
16#endif
17
Ian Rogersb033c752011-07-20 12:22:35 -070018namespace art {
19
20void Thread::InitCpu() {
Elliott Hughesad6c9c32012-01-19 17:39:12 -080021#if defined(__APPLE__)
22 UNIMPLEMENTED(WARNING);
23#else
Ian Rogersb033c752011-07-20 12:22:35 -070024 // Read LDT
25 CHECK_EQ((size_t)LDT_ENTRY_SIZE, sizeof(uint64_t));
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070026 std::vector<uint64_t> ldt(LDT_ENTRIES);
27 size_t ldt_size(sizeof(uint64_t) * ldt.size());
28 memset(&ldt[0], 0, ldt_size);
29 syscall(SYS_modify_ldt, 0, &ldt[0], ldt_size);
Ian Rogersb033c752011-07-20 12:22:35 -070030 // Create empty slot to point at current Thread*
Elliott Hughes7f40ffc2011-09-04 10:50:01 -070031 user_desc ldt_entry;
32 memset(&ldt_entry, 0, sizeof(ldt_entry));
Ian Rogersb033c752011-07-20 12:22:35 -070033 ldt_entry.entry_number = -1;
34 ldt_entry.base_addr = (unsigned int)this;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070035 ldt_entry.limit = kPageSize;
Ian Rogersb033c752011-07-20 12:22:35 -070036 ldt_entry.seg_32bit = 1;
37 ldt_entry.contents = MODIFY_LDT_CONTENTS_DATA;
38 ldt_entry.read_exec_only = 0;
39 ldt_entry.limit_in_pages = 0;
40 ldt_entry.seg_not_present = 0;
41 ldt_entry.useable = 1;
42 for (int i = 0; i < LDT_ENTRIES; i++) {
Elliott Hughes7f40ffc2011-09-04 10:50:01 -070043 if (ldt[i] == 0) {
Ian Rogersb033c752011-07-20 12:22:35 -070044 ldt_entry.entry_number = i;
45 break;
46 }
47 }
48 if (ldt_entry.entry_number >= LDT_ENTRIES) {
49 LOG(FATAL) << "Failed to find available LDT slot";
50 }
51 // Update LDT
52 CHECK_EQ(0, syscall(SYS_modify_ldt, 1, &ldt_entry, sizeof(ldt_entry)));
53 // Change FS to be new LDT entry
54 uint16_t table_indicator = 1 << 2; // LDT
55 uint16_t rpl = 3; // Requested privilege level
56 uint16_t selector = (ldt_entry.entry_number << 3) | table_indicator | rpl;
57 // TODO: use our assembler to generate code
Elliott Hughes7f40ffc2011-09-04 10:50:01 -070058 asm volatile("movw %w0, %%fs"
Ian Rogersb033c752011-07-20 12:22:35 -070059 : // output
60 : "q"(selector) // input
61 :); // clobber
62 // Allow easy indirection back to Thread*
63 self_ = this;
64 // Sanity check reads from FS goes to this Thread*
Ian Rogersb033c752011-07-20 12:22:35 -070065 Thread* self_check;
66 // TODO: use our assembler to generate code
Ian Rogers9651f422011-09-19 20:26:07 -070067 CHECK_EQ(THREAD_SELF_OFFSET, OFFSETOF_MEMBER(Thread, self_));
Elliott Hughes7f40ffc2011-09-04 10:50:01 -070068 asm volatile("movl %%fs:(%1), %0"
Ian Rogersb033c752011-07-20 12:22:35 -070069 : "=r"(self_check) // output
Ian Rogers9651f422011-09-19 20:26:07 -070070 : "r"(THREAD_SELF_OFFSET) // input
Ian Rogersb033c752011-07-20 12:22:35 -070071 :); // clobber
72 CHECK_EQ(self_check, this);
Elliott Hughesad6c9c32012-01-19 17:39:12 -080073#endif
Ian Rogersb033c752011-07-20 12:22:35 -070074}
75
76} // namespace art