Home | History | Annotate | Download | only in x86_64
      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 "asm_support_x86_64.h"
     20 #include "base/macros.h"
     21 #include "thread-current-inl.h"
     22 #include "thread_list.h"
     23 
     24 #if defined(__linux__)
     25 #include <asm/prctl.h>
     26 #include <sys/prctl.h>
     27 #include <sys/syscall.h>
     28 #endif
     29 
     30 namespace art {
     31 
     32 #if defined(__linux__)
     33 static void arch_prctl(int code, void* val) {
     34   syscall(__NR_arch_prctl, code, val);
     35 }
     36 #endif
     37 
     38 void Thread::InitCpu() {
     39   MutexLock mu(nullptr, *Locks::modify_ldt_lock_);
     40 
     41 #if defined(__linux__)
     42   arch_prctl(ARCH_SET_GS, this);
     43 #else
     44   UNIMPLEMENTED(FATAL) << "Need to set GS";
     45 #endif
     46 
     47   // Allow easy indirection back to Thread*.
     48   tlsPtr_.self = this;
     49 
     50   // Sanity check that reads from %gs point to this Thread*.
     51   Thread* self_check;
     52   __asm__ __volatile__("movq %%gs:(%1), %0"
     53       : "=r"(self_check)  // output
     54       : "r"(THREAD_SELF_OFFSET)  // input
     55       :);  // clobber
     56   CHECK_EQ(self_check, this);
     57 }
     58 
     59 void Thread::CleanupCpu() {
     60   // Sanity check that reads from %gs point to this Thread*.
     61   Thread* self_check;
     62   __asm__ __volatile__("movq %%gs:(%1), %0"
     63       : "=r"(self_check)  // output
     64       : "r"(THREAD_SELF_OFFSET)  // input
     65       :);  // clobber
     66   CHECK_EQ(self_check, this);
     67 
     68   // Do nothing.
     69 }
     70 
     71 }  // namespace art
     72