blob: 49aa3268de6a10128028aea7a8905c14d77fd2fc [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 Rogersbdb03912011-09-14 00:55:44 -070016
17#include "context_x86.h"
18
Vladimir Marko7624d252014-05-02 14:40:15 +010019#include "mirror/art_method-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010020#include "quick/quick_method_frame_info.h"
Andreas Gampeb6886112014-11-03 08:47:01 -080021#include "utils.h"
22
Elliott Hughes85d15452011-09-16 17:33:01 -070023
Ian Rogersbdb03912011-09-14 00:55:44 -070024namespace art {
25namespace x86 {
26
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020027static constexpr uintptr_t gZero = 0;
Mathieu Chartier67022432012-11-29 18:04:50 -080028
29void X86Context::Reset() {
Mathieu Chartier815873e2014-02-13 18:02:13 -080030 for (size_t i = 0; i < kNumberOfCpuRegisters; i++) {
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020031 gprs_[i] = nullptr;
Ian Rogers67375ac2011-09-14 00:55:44 -070032 }
Mathieu Chartier67022432012-11-29 18:04:50 -080033 gprs_[ESP] = &esp_;
34 // Initialize registers with easy to spot debug values.
35 esp_ = X86Context::kBadGprBase + ESP;
36 eip_ = X86Context::kBadGprBase + kNumberOfCpuRegisters;
Ian Rogers67375ac2011-09-14 00:55:44 -070037}
38
Ian Rogers0399dde2012-06-06 17:09:28 -070039void X86Context::FillCalleeSaves(const StackVisitor& fr) {
Brian Carlstromea46f952013-07-30 01:26:50 -070040 mirror::ArtMethod* method = fr.GetMethod();
Vladimir Marko7624d252014-05-02 14:40:15 +010041 const QuickMethodFrameInfo frame_info = method->GetQuickFrameInfo();
42 size_t spill_count = POPCOUNT(frame_info.CoreSpillMask());
43 DCHECK_EQ(frame_info.FpSpillMask(), 0u);
Ian Rogers67375ac2011-09-14 00:55:44 -070044 if (spill_count > 0) {
Mathieu Chartier67022432012-11-29 18:04:50 -080045 // Lowest number spill is farthest away, walk registers and fill into context.
Ian Rogers7caad772012-03-30 01:07:54 -070046 int j = 2; // Offset j to skip return address spill.
Mathieu Chartier67022432012-11-29 18:04:50 -080047 for (int i = 0; i < kNumberOfCpuRegisters; i++) {
Vladimir Marko7624d252014-05-02 14:40:15 +010048 if (((frame_info.CoreSpillMask() >> i) & 1) != 0) {
49 gprs_[i] = fr.CalleeSaveAddress(spill_count - j, frame_info.FrameSizeInBytes());
Ian Rogers67375ac2011-09-14 00:55:44 -070050 j++;
51 }
52 }
53 }
54}
55
Elliott Hughes9c750f92012-04-05 12:07:59 -070056void X86Context::SmashCallerSaves() {
Mathieu Chartier67022432012-11-29 18:04:50 -080057 // This needs to be 0 because we want a null/zero return value.
Ian Rogersef7d42f2014-01-06 12:55:46 -080058 gprs_[EAX] = const_cast<uintptr_t*>(&gZero);
59 gprs_[EDX] = const_cast<uintptr_t*>(&gZero);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020060 gprs_[ECX] = nullptr;
61 gprs_[EBX] = nullptr;
Mathieu Chartier67022432012-11-29 18:04:50 -080062}
63
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020064bool X86Context::SetGPR(uint32_t reg, uintptr_t value) {
Brian Carlstrom6f675172013-03-31 00:08:13 -070065 CHECK_LT(reg, static_cast<uint32_t>(kNumberOfCpuRegisters));
Mathieu Chartier67022432012-11-29 18:04:50 -080066 CHECK_NE(gprs_[reg], &gZero);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020067 if (gprs_[reg] != nullptr) {
68 *gprs_[reg] = value;
69 return true;
70 } else {
71 return false;
72 }
Elliott Hughes9c750f92012-04-05 12:07:59 -070073}
74
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070075bool X86Context::GetFPR(uint32_t reg ATTRIBUTE_UNUSED, uintptr_t* val ATTRIBUTE_UNUSED) {
76 LOG(FATAL) << "Floating-point registers are all caller save in X86";
77 UNREACHABLE();
78}
79
80bool X86Context::SetFPR(uint32_t reg ATTRIBUTE_UNUSED, uintptr_t value ATTRIBUTE_UNUSED) {
81 LOG(FATAL) << "Floating-point registers are all caller save in X86";
82 UNREACHABLE();
83}
84
Ian Rogersbdb03912011-09-14 00:55:44 -070085void X86Context::DoLongJump() {
Elliott Hughes85d15452011-09-16 17:33:01 -070086#if defined(__i386__)
Mathieu Chartier67022432012-11-29 18:04:50 -080087 // Array of GPR values, filled from the context backward for the long jump pop. We add a slot at
88 // the top for the stack pointer that doesn't get popped in a pop-all.
89 volatile uintptr_t gprs[kNumberOfCpuRegisters + 1];
90 for (size_t i = 0; i < kNumberOfCpuRegisters; ++i) {
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020091 gprs[kNumberOfCpuRegisters - i - 1] = gprs_[i] != nullptr ? *gprs_[i] : X86Context::kBadGprBase + i;
Mathieu Chartier67022432012-11-29 18:04:50 -080092 }
93 // We want to load the stack pointer one slot below so that the ret will pop eip.
Ian Rogers13735952014-10-08 12:43:28 -070094 uintptr_t esp = gprs[kNumberOfCpuRegisters - ESP - 1] - sizeof(intptr_t);
Mathieu Chartier67022432012-11-29 18:04:50 -080095 gprs[kNumberOfCpuRegisters] = esp;
96 *(reinterpret_cast<uintptr_t*>(esp)) = eip_;
Elliott Hughes7834cbd2012-05-14 18:25:16 -070097 __asm__ __volatile__(
Mathieu Chartier67022432012-11-29 18:04:50 -080098 "movl %0, %%esp\n\t" // ESP points to gprs.
99 "popal\n\t" // Load all registers except ESP and EIP with values in gprs.
100 "popl %%esp\n\t" // Load stack pointer.
101 "ret\n\t" // From higher in the stack pop eip.
102 : // output.
103 : "g"(&gprs[0]) // input.
104 :); // clobber.
Elliott Hughes85d15452011-09-16 17:33:01 -0700105#else
Ian Rogersef7d42f2014-01-06 12:55:46 -0800106 UNIMPLEMENTED(FATAL);
Elliott Hughes85d15452011-09-16 17:33:01 -0700107#endif
Ian Rogersbdb03912011-09-14 00:55:44 -0700108}
109
110} // namespace x86
111} // namespace art