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-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   CHECK_EQ(THREAD_SELF_OFFSET, SelfOffset<8>().Int32Value());
     53   __asm__ __volatile__("movq %%gs:(%1), %0"
     54       : "=r"(self_check)  // output
     55       : "r"(THREAD_SELF_OFFSET)  // input
     56       :);  // clobber
     57   CHECK_EQ(self_check, this);
     58 
     59   // Sanity check other offsets.
     60   CHECK_EQ(static_cast<size_t>(RUNTIME_SAVE_ALL_CALLEE_SAVE_FRAME_OFFSET),
     61            Runtime::GetCalleeSaveMethodOffset(Runtime::kSaveAll));
     62   CHECK_EQ(static_cast<size_t>(RUNTIME_REFS_ONLY_CALLEE_SAVE_FRAME_OFFSET),
     63            Runtime::GetCalleeSaveMethodOffset(Runtime::kRefsOnly));
     64   CHECK_EQ(static_cast<size_t>(RUNTIME_REF_AND_ARGS_CALLEE_SAVE_FRAME_OFFSET),
     65            Runtime::GetCalleeSaveMethodOffset(Runtime::kRefsAndArgs));
     66   CHECK_EQ(THREAD_EXCEPTION_OFFSET, ExceptionOffset<8>().Int32Value());
     67   CHECK_EQ(THREAD_CARD_TABLE_OFFSET, CardTableOffset<8>().Int32Value());
     68   CHECK_EQ(THREAD_ID_OFFSET, ThinLockIdOffset<8>().Int32Value());
     69 }
     70 
     71 void Thread::CleanupCpu() {
     72   // Sanity check that reads from %gs point to this Thread*.
     73   Thread* self_check;
     74   CHECK_EQ(THREAD_SELF_OFFSET, SelfOffset<8>().Int32Value());
     75   __asm__ __volatile__("movq %%gs:(%1), %0"
     76       : "=r"(self_check)  // output
     77       : "r"(THREAD_SELF_OFFSET)  // input
     78       :);  // clobber
     79   CHECK_EQ(self_check, this);
     80 
     81   // Do nothing.
     82 }
     83 
     84 }  // namespace art
     85