blob: 075bf5cfe53ce631acba888d274643765d1cf6e8 [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 Rogers9651f422011-09-19 20:26:07 -070022#include "asm_support.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070023#include "macros.h"
Ian Rogersb033c752011-07-20 12:22:35 -070024
Elliott Hughesad6c9c32012-01-19 17:39:12 -080025#if defined(__APPLE__)
26#include <architecture/i386/table.h>
27#include <i386/user_ldt.h>
28#else
29#include <asm/ldt.h>
30#endif
31
Ian Rogersb033c752011-07-20 12:22:35 -070032namespace art {
33
34void Thread::InitCpu() {
Elliott Hughesad6c9c32012-01-19 17:39:12 -080035#if defined(__APPLE__)
36 UNIMPLEMENTED(WARNING);
37#else
Elliott Hughesd9c67be2012-02-02 19:54:06 -080038 /*
Ian Rogersb033c752011-07-20 12:22:35 -070039 // Read LDT
40 CHECK_EQ((size_t)LDT_ENTRY_SIZE, sizeof(uint64_t));
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070041 std::vector<uint64_t> ldt(LDT_ENTRIES);
42 size_t ldt_size(sizeof(uint64_t) * ldt.size());
43 memset(&ldt[0], 0, ldt_size);
44 syscall(SYS_modify_ldt, 0, &ldt[0], ldt_size);
Ian Rogersb033c752011-07-20 12:22:35 -070045 // Create empty slot to point at current Thread*
Elliott Hughes7f40ffc2011-09-04 10:50:01 -070046 user_desc ldt_entry;
47 memset(&ldt_entry, 0, sizeof(ldt_entry));
Ian Rogersb033c752011-07-20 12:22:35 -070048 ldt_entry.entry_number = -1;
49 ldt_entry.base_addr = (unsigned int)this;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070050 ldt_entry.limit = kPageSize;
Ian Rogersb033c752011-07-20 12:22:35 -070051 ldt_entry.seg_32bit = 1;
52 ldt_entry.contents = MODIFY_LDT_CONTENTS_DATA;
53 ldt_entry.read_exec_only = 0;
54 ldt_entry.limit_in_pages = 0;
55 ldt_entry.seg_not_present = 0;
56 ldt_entry.useable = 1;
57 for (int i = 0; i < LDT_ENTRIES; i++) {
Elliott Hughes7f40ffc2011-09-04 10:50:01 -070058 if (ldt[i] == 0) {
Ian Rogersb033c752011-07-20 12:22:35 -070059 ldt_entry.entry_number = i;
60 break;
61 }
62 }
63 if (ldt_entry.entry_number >= LDT_ENTRIES) {
64 LOG(FATAL) << "Failed to find available LDT slot";
65 }
66 // Update LDT
67 CHECK_EQ(0, syscall(SYS_modify_ldt, 1, &ldt_entry, sizeof(ldt_entry)));
68 // Change FS to be new LDT entry
69 uint16_t table_indicator = 1 << 2; // LDT
70 uint16_t rpl = 3; // Requested privilege level
71 uint16_t selector = (ldt_entry.entry_number << 3) | table_indicator | rpl;
72 // TODO: use our assembler to generate code
Elliott Hughes7f40ffc2011-09-04 10:50:01 -070073 asm volatile("movw %w0, %%fs"
Ian Rogersb033c752011-07-20 12:22:35 -070074 : // output
75 : "q"(selector) // input
76 :); // clobber
77 // Allow easy indirection back to Thread*
78 self_ = this;
79 // Sanity check reads from FS goes to this Thread*
Ian Rogersb033c752011-07-20 12:22:35 -070080 Thread* self_check;
81 // TODO: use our assembler to generate code
Ian Rogers9651f422011-09-19 20:26:07 -070082 CHECK_EQ(THREAD_SELF_OFFSET, OFFSETOF_MEMBER(Thread, self_));
Elliott Hughes7f40ffc2011-09-04 10:50:01 -070083 asm volatile("movl %%fs:(%1), %0"
Ian Rogersb033c752011-07-20 12:22:35 -070084 : "=r"(self_check) // output
Ian Rogers9651f422011-09-19 20:26:07 -070085 : "r"(THREAD_SELF_OFFSET) // input
Ian Rogersb033c752011-07-20 12:22:35 -070086 :); // clobber
87 CHECK_EQ(self_check, this);
Elliott Hughesd9c67be2012-02-02 19:54:06 -080088 */
Elliott Hughesad6c9c32012-01-19 17:39:12 -080089#endif
Ian Rogersb033c752011-07-20 12:22:35 -070090}
91
92} // namespace art