blob: 85da1f38e04519b3bf741ab23fb52192d9381da1 [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 Rogers891f4a92012-02-03 16:04:54 -080024#include "thread_list.h"
Ian Rogersb033c752011-07-20 12:22:35 -070025
Elliott Hughesad6c9c32012-01-19 17:39:12 -080026#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 Rogersb033c752011-07-20 12:22:35 -070033namespace art {
34
35void Thread::InitCpu() {
Elliott Hughesad6c9c32012-01-19 17:39:12 -080036#if defined(__APPLE__)
37 UNIMPLEMENTED(WARNING);
38#else
Elliott Hughes83239722012-02-03 16:49:24 -080039 static Mutex modify_ldt_lock("modify_ldt lock");
40 MutexLock mu(modify_ldt_lock);
Ian Rogers891f4a92012-02-03 16:04:54 -080041
Ian Rogersb033c752011-07-20 12:22:35 -070042 // Read LDT
43 CHECK_EQ((size_t)LDT_ENTRY_SIZE, sizeof(uint64_t));
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070044 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 Rogersb033c752011-07-20 12:22:35 -070048 // Create empty slot to point at current Thread*
Elliott Hughes7f40ffc2011-09-04 10:50:01 -070049 user_desc ldt_entry;
50 memset(&ldt_entry, 0, sizeof(ldt_entry));
Ian Rogersb033c752011-07-20 12:22:35 -070051 ldt_entry.entry_number = -1;
52 ldt_entry.base_addr = (unsigned int)this;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070053 ldt_entry.limit = kPageSize;
Ian Rogersb033c752011-07-20 12:22:35 -070054 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 Hughes7f40ffc2011-09-04 10:50:01 -070061 if (ldt[i] == 0) {
Ian Rogersb033c752011-07-20 12:22:35 -070062 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 Hughes7f40ffc2011-09-04 10:50:01 -070076 asm volatile("movw %w0, %%fs"
Ian Rogersb033c752011-07-20 12:22:35 -070077 : // 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 Rogersb033c752011-07-20 12:22:35 -070083 Thread* self_check;
84 // TODO: use our assembler to generate code
Ian Rogers9651f422011-09-19 20:26:07 -070085 CHECK_EQ(THREAD_SELF_OFFSET, OFFSETOF_MEMBER(Thread, self_));
Elliott Hughes7f40ffc2011-09-04 10:50:01 -070086 asm volatile("movl %%fs:(%1), %0"
Ian Rogersb033c752011-07-20 12:22:35 -070087 : "=r"(self_check) // output
Ian Rogers9651f422011-09-19 20:26:07 -070088 : "r"(THREAD_SELF_OFFSET) // input
Ian Rogersb033c752011-07-20 12:22:35 -070089 :); // clobber
90 CHECK_EQ(self_check, this);
Elliott Hughesad6c9c32012-01-19 17:39:12 -080091#endif
Ian Rogersb033c752011-07-20 12:22:35 -070092}
93
94} // namespace art