Home | History | Annotate | Download | only in x86
      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  */
     16 
     17 #include "thread.h"
     18 
     19 #include <sys/syscall.h>
     20 #include <sys/types.h>
     21 
     22 #include "asm_support_x86.h"
     23 #include "base/macros.h"
     24 #include "thread.h"
     25 #include "thread_list.h"
     26 
     27 #if defined(__APPLE__)
     28 #include <architecture/i386/table.h>
     29 #include <i386/user_ldt.h>
     30 struct descriptor_table_entry_t {
     31   uint16_t limit0;
     32   uint16_t base0;
     33   unsigned base1: 8, type: 4, s: 1, dpl: 2, p: 1;
     34   unsigned limit: 4, avl: 1, l: 1, d: 1, g: 1, base2: 8;
     35 } __attribute__((packed));
     36 #define MODIFY_LDT_CONTENTS_DATA 0
     37 #else
     38 #include <asm/ldt.h>
     39 #endif
     40 
     41 namespace art {
     42 
     43 void Thread::InitCpu() {
     44   static Mutex modify_ldt_lock("modify_ldt lock");
     45   MutexLock mu(Thread::Current(), modify_ldt_lock);
     46 
     47   const uintptr_t base = reinterpret_cast<uintptr_t>(this);
     48   const size_t limit = kPageSize;
     49 
     50   const int contents = MODIFY_LDT_CONTENTS_DATA;
     51   const int seg_32bit = 1;
     52   const int read_exec_only = 0;
     53   const int limit_in_pages = 0;
     54   const int seg_not_present = 0;
     55   const int useable = 1;
     56 
     57   int entry_number = -1;
     58 
     59 #if defined(__APPLE__)
     60   descriptor_table_entry_t entry;
     61   memset(&entry, 0, sizeof(entry));
     62   entry.limit0 = (limit & 0x0ffff);
     63   entry.limit  = (limit & 0xf0000) >> 16;
     64   entry.base0 = (base & 0x0000ffff);
     65   entry.base1 = (base & 0x00ff0000) >> 16;
     66   entry.base2 = (base & 0xff000000) >> 24;
     67   entry.type = ((read_exec_only ^ 1) << 1) | (contents << 2);
     68   entry.s = 1;
     69   entry.dpl = 0x3;
     70   entry.p = seg_not_present ^ 1;
     71   entry.avl = useable;
     72   entry.l = 0;
     73   entry.d = seg_32bit;
     74   entry.g = limit_in_pages;
     75 
     76   entry_number = i386_set_ldt(LDT_AUTO_ALLOC, reinterpret_cast<ldt_entry*>(&entry), 1);
     77   if (entry_number == -1) {
     78     PLOG(FATAL) << "i386_set_ldt failed";
     79   }
     80 #else
     81   // Read current LDT entries.
     82   CHECK_EQ((size_t)LDT_ENTRY_SIZE, sizeof(uint64_t));
     83   std::vector<uint64_t> ldt(LDT_ENTRIES);
     84   size_t ldt_size(sizeof(uint64_t) * ldt.size());
     85   memset(&ldt[0], 0, ldt_size);
     86   // TODO: why doesn't this return LDT_ENTRY_SIZE * LDT_ENTRIES for the main thread?
     87   syscall(__NR_modify_ldt, 0, &ldt[0], ldt_size);
     88 
     89   // Find the first empty slot.
     90   for (entry_number = 0; entry_number < LDT_ENTRIES && ldt[entry_number] != 0; ++entry_number) {
     91   }
     92   if (entry_number >= LDT_ENTRIES) {
     93     LOG(FATAL) << "Failed to find a free LDT slot";
     94   }
     95 
     96   // Update LDT entry.
     97   user_desc ldt_entry;
     98   memset(&ldt_entry, 0, sizeof(ldt_entry));
     99   ldt_entry.entry_number = entry_number;
    100   ldt_entry.base_addr = base;
    101   ldt_entry.limit = limit;
    102   ldt_entry.seg_32bit = seg_32bit;
    103   ldt_entry.contents = contents;
    104   ldt_entry.read_exec_only = read_exec_only;
    105   ldt_entry.limit_in_pages = limit_in_pages;
    106   ldt_entry.seg_not_present = seg_not_present;
    107   ldt_entry.useable = useable;
    108   CHECK_EQ(0, syscall(__NR_modify_ldt, 1, &ldt_entry, sizeof(ldt_entry)));
    109   entry_number = ldt_entry.entry_number;
    110 #endif
    111 
    112   // Change %fs to be new LDT entry.
    113   uint16_t table_indicator = 1 << 2;  // LDT
    114   uint16_t rpl = 3;  // Requested privilege level
    115   uint16_t selector = (entry_number << 3) | table_indicator | rpl;
    116   // TODO: use our assembler to generate code
    117   __asm__ __volatile__("movw %w0, %%fs"
    118       :    // output
    119       : "q"(selector)  // input
    120       :);  // clobber
    121 
    122   // Allow easy indirection back to Thread*.
    123   self_ = this;
    124 
    125   // Sanity check that reads from %fs point to this Thread*.
    126   Thread* self_check;
    127   // TODO: use our assembler to generate code
    128   CHECK_EQ(THREAD_SELF_OFFSET, OFFSETOF_MEMBER(Thread, self_));
    129   __asm__ __volatile__("movl %%fs:(%1), %0"
    130       : "=r"(self_check)  // output
    131       : "r"(THREAD_SELF_OFFSET)  // input
    132       :);  // clobber
    133   CHECK_EQ(self_check, this);
    134 
    135   // Sanity check other offsets.
    136   CHECK_EQ(THREAD_EXCEPTION_OFFSET, OFFSETOF_MEMBER(Thread, exception_));
    137 }
    138 
    139 }  // namespace art
    140