blob: d7cc70488dd5c5ccadf860c92b9dc798be070a8c [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 }
Mark Mendell3d2c8e72015-01-13 17:32:55 -050033 for (size_t i = 0; i < kNumberOfFloatRegisters; ++i) {
34 fprs_[i] = nullptr;
35 }
Mathieu Chartier67022432012-11-29 18:04:50 -080036 gprs_[ESP] = &esp_;
37 // Initialize registers with easy to spot debug values.
38 esp_ = X86Context::kBadGprBase + ESP;
39 eip_ = X86Context::kBadGprBase + kNumberOfCpuRegisters;
Ian Rogers67375ac2011-09-14 00:55:44 -070040}
41
Ian Rogers0399dde2012-06-06 17:09:28 -070042void X86Context::FillCalleeSaves(const StackVisitor& fr) {
Brian Carlstromea46f952013-07-30 01:26:50 -070043 mirror::ArtMethod* method = fr.GetMethod();
Vladimir Marko7624d252014-05-02 14:40:15 +010044 const QuickMethodFrameInfo frame_info = method->GetQuickFrameInfo();
45 size_t spill_count = POPCOUNT(frame_info.CoreSpillMask());
Mark Mendell3d2c8e72015-01-13 17:32:55 -050046 size_t fp_spill_count = POPCOUNT(frame_info.FpSpillMask());
Ian Rogers67375ac2011-09-14 00:55:44 -070047 if (spill_count > 0) {
Mathieu Chartier67022432012-11-29 18:04:50 -080048 // Lowest number spill is farthest away, walk registers and fill into context.
Ian Rogers7caad772012-03-30 01:07:54 -070049 int j = 2; // Offset j to skip return address spill.
Mathieu Chartier67022432012-11-29 18:04:50 -080050 for (int i = 0; i < kNumberOfCpuRegisters; i++) {
Vladimir Marko7624d252014-05-02 14:40:15 +010051 if (((frame_info.CoreSpillMask() >> i) & 1) != 0) {
52 gprs_[i] = fr.CalleeSaveAddress(spill_count - j, frame_info.FrameSizeInBytes());
Ian Rogers67375ac2011-09-14 00:55:44 -070053 j++;
54 }
55 }
56 }
Mark Mendell3d2c8e72015-01-13 17:32:55 -050057 if (fp_spill_count > 0) {
58 // Lowest number spill is farthest away, walk registers and fill into context.
59 size_t j = 2; // Offset j to skip return address spill.
60 size_t fp_spill_size_in_words = fp_spill_count * 2;
61 for (size_t i = 0; i < kNumberOfFloatRegisters; ++i) {
62 if (((frame_info.FpSpillMask() >> i) & 1) != 0) {
63 // There are 2 pieces to each XMM register, to match VR size.
64 fprs_[2*i] = reinterpret_cast<uint32_t*>(
65 fr.CalleeSaveAddress(spill_count + fp_spill_size_in_words - j,
66 frame_info.FrameSizeInBytes()));
67 fprs_[2*i+1] = reinterpret_cast<uint32_t*>(
68 fr.CalleeSaveAddress(spill_count + fp_spill_size_in_words - j - 1,
69 frame_info.FrameSizeInBytes()));
70 // Two void* per XMM register.
71 j += 2;
72 }
73 }
74 }
Ian Rogers67375ac2011-09-14 00:55:44 -070075}
76
Elliott Hughes9c750f92012-04-05 12:07:59 -070077void X86Context::SmashCallerSaves() {
Mathieu Chartier67022432012-11-29 18:04:50 -080078 // This needs to be 0 because we want a null/zero return value.
Ian Rogersef7d42f2014-01-06 12:55:46 -080079 gprs_[EAX] = const_cast<uintptr_t*>(&gZero);
80 gprs_[EDX] = const_cast<uintptr_t*>(&gZero);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020081 gprs_[ECX] = nullptr;
82 gprs_[EBX] = nullptr;
Mark Mendell3d2c8e72015-01-13 17:32:55 -050083 memset(&fprs_[0], '\0', sizeof(fprs_));
Mathieu Chartier67022432012-11-29 18:04:50 -080084}
85
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020086bool X86Context::SetGPR(uint32_t reg, uintptr_t value) {
Brian Carlstrom6f675172013-03-31 00:08:13 -070087 CHECK_LT(reg, static_cast<uint32_t>(kNumberOfCpuRegisters));
Mathieu Chartier67022432012-11-29 18:04:50 -080088 CHECK_NE(gprs_[reg], &gZero);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020089 if (gprs_[reg] != nullptr) {
90 *gprs_[reg] = value;
91 return true;
92 } else {
93 return false;
94 }
Elliott Hughes9c750f92012-04-05 12:07:59 -070095}
96
Mark Mendell3d2c8e72015-01-13 17:32:55 -050097bool X86Context::SetFPR(uint32_t reg, uintptr_t value) {
98 CHECK_LT(reg, static_cast<uint32_t>(kNumberOfFloatRegisters));
99 CHECK_NE(fprs_[reg], reinterpret_cast<const uint32_t*>(&gZero));
100 if (fprs_[reg] != nullptr) {
101 *fprs_[reg] = value;
102 return true;
103 } else {
104 return false;
105 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700106}
107
Ian Rogersbdb03912011-09-14 00:55:44 -0700108void X86Context::DoLongJump() {
Elliott Hughes85d15452011-09-16 17:33:01 -0700109#if defined(__i386__)
Mathieu Chartier67022432012-11-29 18:04:50 -0800110 // Array of GPR values, filled from the context backward for the long jump pop. We add a slot at
111 // the top for the stack pointer that doesn't get popped in a pop-all.
112 volatile uintptr_t gprs[kNumberOfCpuRegisters + 1];
113 for (size_t i = 0; i < kNumberOfCpuRegisters; ++i) {
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200114 gprs[kNumberOfCpuRegisters - i - 1] = gprs_[i] != nullptr ? *gprs_[i] : X86Context::kBadGprBase + i;
Mathieu Chartier67022432012-11-29 18:04:50 -0800115 }
Mark Mendell3d2c8e72015-01-13 17:32:55 -0500116 uint32_t fprs[kNumberOfFloatRegisters];
117 for (size_t i = 0; i < kNumberOfFloatRegisters; ++i) {
118 fprs[i] = fprs_[i] != nullptr ? *fprs_[i] : X86Context::kBadFprBase + i;
119 }
Mathieu Chartier67022432012-11-29 18:04:50 -0800120 // We want to load the stack pointer one slot below so that the ret will pop eip.
Ian Rogers13735952014-10-08 12:43:28 -0700121 uintptr_t esp = gprs[kNumberOfCpuRegisters - ESP - 1] - sizeof(intptr_t);
Mathieu Chartier67022432012-11-29 18:04:50 -0800122 gprs[kNumberOfCpuRegisters] = esp;
123 *(reinterpret_cast<uintptr_t*>(esp)) = eip_;
Elliott Hughes7834cbd2012-05-14 18:25:16 -0700124 __asm__ __volatile__(
Mark Mendell3d2c8e72015-01-13 17:32:55 -0500125 "movl %1, %%ebx\n\t" // Address base of FPRs.
126 "movsd 0(%%ebx), %%xmm0\n\t" // Load up XMM0-XMM7.
127 "movsd 8(%%ebx), %%xmm1\n\t"
128 "movsd 16(%%ebx), %%xmm2\n\t"
129 "movsd 24(%%ebx), %%xmm3\n\t"
130 "movsd 32(%%ebx), %%xmm4\n\t"
131 "movsd 40(%%ebx), %%xmm5\n\t"
132 "movsd 48(%%ebx), %%xmm6\n\t"
133 "movsd 56(%%ebx), %%xmm7\n\t"
Mathieu Chartier67022432012-11-29 18:04:50 -0800134 "movl %0, %%esp\n\t" // ESP points to gprs.
135 "popal\n\t" // Load all registers except ESP and EIP with values in gprs.
136 "popl %%esp\n\t" // Load stack pointer.
137 "ret\n\t" // From higher in the stack pop eip.
138 : // output.
Mark Mendell3d2c8e72015-01-13 17:32:55 -0500139 : "g"(&gprs[0]), "g"(&fprs[0]) // input.
Mathieu Chartier67022432012-11-29 18:04:50 -0800140 :); // clobber.
Elliott Hughes85d15452011-09-16 17:33:01 -0700141#else
Ian Rogersef7d42f2014-01-06 12:55:46 -0800142 UNIMPLEMENTED(FATAL);
Elliott Hughes85d15452011-09-16 17:33:01 -0700143#endif
Ian Rogersbdb03912011-09-14 00:55:44 -0700144}
145
146} // namespace x86
147} // namespace art