blob: 30caaeccd88ae809a8c001e21674b4a99b741232 [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
Brian Carlstrom51477332012-03-25 20:20:26 -070033// TODO: add SYS_modify_ldt definition to bionic
34#ifndef SYS_modify_ldt
35#define SYS_modify_ldt __NR_modify_ldt
36#endif
37
Ian Rogersb033c752011-07-20 12:22:35 -070038namespace art {
39
40void Thread::InitCpu() {
Elliott Hughesad6c9c32012-01-19 17:39:12 -080041#if defined(__APPLE__)
42 UNIMPLEMENTED(WARNING);
43#else
Elliott Hughes83239722012-02-03 16:49:24 -080044 static Mutex modify_ldt_lock("modify_ldt lock");
45 MutexLock mu(modify_ldt_lock);
Ian Rogers891f4a92012-02-03 16:04:54 -080046
Ian Rogersb033c752011-07-20 12:22:35 -070047 // Read LDT
48 CHECK_EQ((size_t)LDT_ENTRY_SIZE, sizeof(uint64_t));
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070049 std::vector<uint64_t> ldt(LDT_ENTRIES);
50 size_t ldt_size(sizeof(uint64_t) * ldt.size());
51 memset(&ldt[0], 0, ldt_size);
52 syscall(SYS_modify_ldt, 0, &ldt[0], ldt_size);
Ian Rogersb033c752011-07-20 12:22:35 -070053 // Create empty slot to point at current Thread*
Elliott Hughes7f40ffc2011-09-04 10:50:01 -070054 user_desc ldt_entry;
55 memset(&ldt_entry, 0, sizeof(ldt_entry));
Ian Rogersb033c752011-07-20 12:22:35 -070056 ldt_entry.entry_number = -1;
57 ldt_entry.base_addr = (unsigned int)this;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070058 ldt_entry.limit = kPageSize;
Ian Rogersb033c752011-07-20 12:22:35 -070059 ldt_entry.seg_32bit = 1;
60 ldt_entry.contents = MODIFY_LDT_CONTENTS_DATA;
61 ldt_entry.read_exec_only = 0;
62 ldt_entry.limit_in_pages = 0;
63 ldt_entry.seg_not_present = 0;
64 ldt_entry.useable = 1;
65 for (int i = 0; i < LDT_ENTRIES; i++) {
Elliott Hughes7f40ffc2011-09-04 10:50:01 -070066 if (ldt[i] == 0) {
Ian Rogersb033c752011-07-20 12:22:35 -070067 ldt_entry.entry_number = i;
68 break;
69 }
70 }
71 if (ldt_entry.entry_number >= LDT_ENTRIES) {
72 LOG(FATAL) << "Failed to find available LDT slot";
73 }
74 // Update LDT
75 CHECK_EQ(0, syscall(SYS_modify_ldt, 1, &ldt_entry, sizeof(ldt_entry)));
76 // Change FS to be new LDT entry
77 uint16_t table_indicator = 1 << 2; // LDT
78 uint16_t rpl = 3; // Requested privilege level
79 uint16_t selector = (ldt_entry.entry_number << 3) | table_indicator | rpl;
80 // TODO: use our assembler to generate code
Elliott Hughes7f40ffc2011-09-04 10:50:01 -070081 asm volatile("movw %w0, %%fs"
Ian Rogersb033c752011-07-20 12:22:35 -070082 : // output
83 : "q"(selector) // input
84 :); // clobber
85 // Allow easy indirection back to Thread*
86 self_ = this;
87 // Sanity check reads from FS goes to this Thread*
Ian Rogersb033c752011-07-20 12:22:35 -070088 Thread* self_check;
89 // TODO: use our assembler to generate code
Ian Rogers9651f422011-09-19 20:26:07 -070090 CHECK_EQ(THREAD_SELF_OFFSET, OFFSETOF_MEMBER(Thread, self_));
Elliott Hughes7f40ffc2011-09-04 10:50:01 -070091 asm volatile("movl %%fs:(%1), %0"
Ian Rogersb033c752011-07-20 12:22:35 -070092 : "=r"(self_check) // output
Ian Rogers9651f422011-09-19 20:26:07 -070093 : "r"(THREAD_SELF_OFFSET) // input
Ian Rogersb033c752011-07-20 12:22:35 -070094 :); // clobber
95 CHECK_EQ(self_check, this);
Elliott Hughesad6c9c32012-01-19 17:39:12 -080096#endif
Ian Rogersb033c752011-07-20 12:22:35 -070097}
98
99} // namespace art