blob: 2a6ff14c7575d06532296cdf541a16af5e29727d [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());
Vladimir Marko949c91f2015-01-27 10:48:44 +000043 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 Hertz96ba8dc2015-01-22 18:57:14 +010064void X86Context::SetGPR(uint32_t reg, uintptr_t value) {
Brian Carlstrom6f675172013-03-31 00:08:13 -070065 CHECK_LT(reg, static_cast<uint32_t>(kNumberOfCpuRegisters));
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010066 DCHECK(IsAccessibleGPR(reg));
Mathieu Chartier67022432012-11-29 18:04:50 -080067 CHECK_NE(gprs_[reg], &gZero);
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010068 *gprs_[reg] = value;
Elliott Hughes9c750f92012-04-05 12:07:59 -070069}
70
Vladimir Marko949c91f2015-01-27 10:48:44 +000071uintptr_t X86Context::GetFPR(uint32_t reg ATTRIBUTE_UNUSED) {
72 LOG(FATAL) << "Floating-point registers are all caller save in X86";
73 UNREACHABLE();
74}
75
76void X86Context::SetFPR(uint32_t reg ATTRIBUTE_UNUSED, uintptr_t value ATTRIBUTE_UNUSED) {
77 LOG(FATAL) << "Floating-point registers are all caller save in X86";
78 UNREACHABLE();
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070079}
80
Ian Rogersbdb03912011-09-14 00:55:44 -070081void X86Context::DoLongJump() {
Elliott Hughes85d15452011-09-16 17:33:01 -070082#if defined(__i386__)
Mathieu Chartier67022432012-11-29 18:04:50 -080083 // Array of GPR values, filled from the context backward for the long jump pop. We add a slot at
84 // the top for the stack pointer that doesn't get popped in a pop-all.
85 volatile uintptr_t gprs[kNumberOfCpuRegisters + 1];
86 for (size_t i = 0; i < kNumberOfCpuRegisters; ++i) {
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020087 gprs[kNumberOfCpuRegisters - i - 1] = gprs_[i] != nullptr ? *gprs_[i] : X86Context::kBadGprBase + i;
Mathieu Chartier67022432012-11-29 18:04:50 -080088 }
89 // We want to load the stack pointer one slot below so that the ret will pop eip.
Ian Rogers13735952014-10-08 12:43:28 -070090 uintptr_t esp = gprs[kNumberOfCpuRegisters - ESP - 1] - sizeof(intptr_t);
Mathieu Chartier67022432012-11-29 18:04:50 -080091 gprs[kNumberOfCpuRegisters] = esp;
92 *(reinterpret_cast<uintptr_t*>(esp)) = eip_;
Elliott Hughes7834cbd2012-05-14 18:25:16 -070093 __asm__ __volatile__(
Mathieu Chartier67022432012-11-29 18:04:50 -080094 "movl %0, %%esp\n\t" // ESP points to gprs.
95 "popal\n\t" // Load all registers except ESP and EIP with values in gprs.
96 "popl %%esp\n\t" // Load stack pointer.
97 "ret\n\t" // From higher in the stack pop eip.
98 : // output.
Vladimir Marko949c91f2015-01-27 10:48:44 +000099 : "g"(&gprs[0]) // input.
Mathieu Chartier67022432012-11-29 18:04:50 -0800100 :); // clobber.
Elliott Hughes85d15452011-09-16 17:33:01 -0700101#else
Ian Rogersef7d42f2014-01-06 12:55:46 -0800102 UNIMPLEMENTED(FATAL);
Elliott Hughes85d15452011-09-16 17:33:01 -0700103#endif
Ian Rogersbdb03912011-09-14 00:55:44 -0700104}
105
106} // namespace x86
107} // namespace art