xref: /aosp_15_r20/art/runtime/arch/x86/context_x86.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2011 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "context_x86.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include "base/bit_utils.h"
20*795d594fSAndroid Build Coastguard Worker #include "base/bit_utils_iterator.h"
21*795d594fSAndroid Build Coastguard Worker #include "base/memory_tool.h"
22*795d594fSAndroid Build Coastguard Worker #include "quick/quick_method_frame_info.h"
23*795d594fSAndroid Build Coastguard Worker 
24*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
25*795d594fSAndroid Build Coastguard Worker namespace x86 {
26*795d594fSAndroid Build Coastguard Worker 
27*795d594fSAndroid Build Coastguard Worker static constexpr uintptr_t gZero = 0;
28*795d594fSAndroid Build Coastguard Worker 
Reset()29*795d594fSAndroid Build Coastguard Worker void X86Context::Reset() {
30*795d594fSAndroid Build Coastguard Worker   std::fill_n(gprs_, arraysize(gprs_), nullptr);
31*795d594fSAndroid Build Coastguard Worker   std::fill_n(fprs_, arraysize(fprs_), nullptr);
32*795d594fSAndroid Build Coastguard Worker   gprs_[ESP] = &esp_;
33*795d594fSAndroid Build Coastguard Worker   gprs_[EAX] = &arg0_;
34*795d594fSAndroid Build Coastguard Worker   // Initialize registers with easy to spot debug values.
35*795d594fSAndroid Build Coastguard Worker   esp_ = kBadGprBase + ESP;
36*795d594fSAndroid Build Coastguard Worker   eip_ = kBadGprBase + kNumberOfCpuRegisters;
37*795d594fSAndroid Build Coastguard Worker   arg0_ = 0;
38*795d594fSAndroid Build Coastguard Worker }
39*795d594fSAndroid Build Coastguard Worker 
FillCalleeSaves(uint8_t * frame,const QuickMethodFrameInfo & frame_info)40*795d594fSAndroid Build Coastguard Worker void X86Context::FillCalleeSaves(uint8_t* frame, const QuickMethodFrameInfo& frame_info) {
41*795d594fSAndroid Build Coastguard Worker   const size_t frame_size = frame_info.FrameSizeInBytes();
42*795d594fSAndroid Build Coastguard Worker   int spill_pos = 0;
43*795d594fSAndroid Build Coastguard Worker 
44*795d594fSAndroid Build Coastguard Worker   // Core registers come first, from the highest down to the lowest.
45*795d594fSAndroid Build Coastguard Worker   uint32_t core_regs =
46*795d594fSAndroid Build Coastguard Worker       frame_info.CoreSpillMask() & ~(static_cast<uint32_t>(-1) << kNumberOfCpuRegisters);
47*795d594fSAndroid Build Coastguard Worker   DCHECK_EQ(1, POPCOUNT(frame_info.CoreSpillMask() & ~core_regs));  // Return address spill.
48*795d594fSAndroid Build Coastguard Worker   for (uint32_t core_reg : HighToLowBits(core_regs)) {
49*795d594fSAndroid Build Coastguard Worker     gprs_[core_reg] = CalleeSaveAddress<InstructionSet::kX86>(frame, spill_pos, frame_size);
50*795d594fSAndroid Build Coastguard Worker     ++spill_pos;
51*795d594fSAndroid Build Coastguard Worker   }
52*795d594fSAndroid Build Coastguard Worker   DCHECK_EQ(spill_pos, POPCOUNT(frame_info.CoreSpillMask()) - 1);
53*795d594fSAndroid Build Coastguard Worker 
54*795d594fSAndroid Build Coastguard Worker   // FP registers come second, from the highest down to the lowest.
55*795d594fSAndroid Build Coastguard Worker   uint32_t fp_regs = frame_info.FpSpillMask();
56*795d594fSAndroid Build Coastguard Worker   DCHECK_EQ(0u, fp_regs & (static_cast<uint32_t>(-1) << kNumberOfFloatRegisters));
57*795d594fSAndroid Build Coastguard Worker   for (uint32_t fp_reg : HighToLowBits(fp_regs)) {
58*795d594fSAndroid Build Coastguard Worker     // Two void* per XMM register.
59*795d594fSAndroid Build Coastguard Worker     fprs_[2 * fp_reg] = reinterpret_cast<uint32_t*>(
60*795d594fSAndroid Build Coastguard Worker         CalleeSaveAddress<InstructionSet::kX86>(frame, spill_pos + 1, frame_size));
61*795d594fSAndroid Build Coastguard Worker     fprs_[2 * fp_reg + 1] = reinterpret_cast<uint32_t*>(
62*795d594fSAndroid Build Coastguard Worker         CalleeSaveAddress<InstructionSet::kX86>(frame, spill_pos, frame_size));
63*795d594fSAndroid Build Coastguard Worker     spill_pos += 2;
64*795d594fSAndroid Build Coastguard Worker   }
65*795d594fSAndroid Build Coastguard Worker   DCHECK_EQ(spill_pos,
66*795d594fSAndroid Build Coastguard Worker             POPCOUNT(frame_info.CoreSpillMask()) - 1 + 2 * POPCOUNT(frame_info.FpSpillMask()));
67*795d594fSAndroid Build Coastguard Worker }
68*795d594fSAndroid Build Coastguard Worker 
SmashCallerSaves()69*795d594fSAndroid Build Coastguard Worker void X86Context::SmashCallerSaves() {
70*795d594fSAndroid Build Coastguard Worker   // This needs to be 0 because we want a null/zero return value.
71*795d594fSAndroid Build Coastguard Worker   gprs_[EAX] = const_cast<uintptr_t*>(&gZero);
72*795d594fSAndroid Build Coastguard Worker   gprs_[EDX] = const_cast<uintptr_t*>(&gZero);
73*795d594fSAndroid Build Coastguard Worker   gprs_[ECX] = nullptr;
74*795d594fSAndroid Build Coastguard Worker   gprs_[EBX] = nullptr;
75*795d594fSAndroid Build Coastguard Worker   memset(&fprs_[0], '\0', sizeof(fprs_));
76*795d594fSAndroid Build Coastguard Worker }
77*795d594fSAndroid Build Coastguard Worker 
SetGPR(uint32_t reg,uintptr_t value)78*795d594fSAndroid Build Coastguard Worker void X86Context::SetGPR(uint32_t reg, uintptr_t value) {
79*795d594fSAndroid Build Coastguard Worker   CHECK_LT(reg, static_cast<uint32_t>(kNumberOfCpuRegisters));
80*795d594fSAndroid Build Coastguard Worker   DCHECK(IsAccessibleGPR(reg));
81*795d594fSAndroid Build Coastguard Worker   CHECK_NE(gprs_[reg], &gZero);
82*795d594fSAndroid Build Coastguard Worker   *gprs_[reg] = value;
83*795d594fSAndroid Build Coastguard Worker }
84*795d594fSAndroid Build Coastguard Worker 
SetFPR(uint32_t reg,uintptr_t value)85*795d594fSAndroid Build Coastguard Worker void X86Context::SetFPR(uint32_t reg, uintptr_t value) {
86*795d594fSAndroid Build Coastguard Worker   CHECK_LT(reg, static_cast<uint32_t>(kNumberOfFloatRegisters));
87*795d594fSAndroid Build Coastguard Worker   DCHECK(IsAccessibleFPR(reg));
88*795d594fSAndroid Build Coastguard Worker   CHECK_NE(fprs_[reg], reinterpret_cast<const uint32_t*>(&gZero));
89*795d594fSAndroid Build Coastguard Worker   *fprs_[reg] = value;
90*795d594fSAndroid Build Coastguard Worker }
91*795d594fSAndroid Build Coastguard Worker 
CopyContextTo(uintptr_t * gprs,uintptr_t * fprs)92*795d594fSAndroid Build Coastguard Worker void X86Context::CopyContextTo(uintptr_t* gprs, uintptr_t* fprs) {
93*795d594fSAndroid Build Coastguard Worker #if defined(__i386__)
94*795d594fSAndroid Build Coastguard Worker   // Array of GPR values, filled from the context backward for the long jump pop. We add a slot at
95*795d594fSAndroid Build Coastguard Worker   // the top for the stack pointer that doesn't get popped in a pop-all.
96*795d594fSAndroid Build Coastguard Worker   for (size_t i = 0; i < kNumberOfCpuRegisters; ++i) {
97*795d594fSAndroid Build Coastguard Worker     gprs[kNumberOfCpuRegisters - i - 1] = (gprs_[i] != nullptr) ? *gprs_[i] : (kBadGprBase + i);
98*795d594fSAndroid Build Coastguard Worker   }
99*795d594fSAndroid Build Coastguard Worker   for (size_t i = 0; i < kNumberOfFloatRegisters; ++i) {
100*795d594fSAndroid Build Coastguard Worker     fprs[i] = fprs_[i] != nullptr ? *fprs_[i] : kBadFprBase + i;
101*795d594fSAndroid Build Coastguard Worker   }
102*795d594fSAndroid Build Coastguard Worker   // We want to load the stack pointer one slot below so that the ret will pop eip.
103*795d594fSAndroid Build Coastguard Worker   uintptr_t esp = gprs[kNumberOfCpuRegisters - ESP - 1] - sizeof(intptr_t);
104*795d594fSAndroid Build Coastguard Worker   gprs[kNumberOfCpuRegisters] = esp;
105*795d594fSAndroid Build Coastguard Worker   *(reinterpret_cast<uintptr_t*>(esp)) = eip_;
106*795d594fSAndroid Build Coastguard Worker #else
107*795d594fSAndroid Build Coastguard Worker   UNIMPLEMENTED(FATAL);
108*795d594fSAndroid Build Coastguard Worker #endif
109*795d594fSAndroid Build Coastguard Worker }
110*795d594fSAndroid Build Coastguard Worker 
111*795d594fSAndroid Build Coastguard Worker }  // namespace x86
112*795d594fSAndroid Build Coastguard Worker }  // namespace art
113