blob: 0d88dd0dc56e6ac5a1f5e4281c555263d62a8124 [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
Nicolas Geoffray6bc43742015-10-12 18:11:10 +010019#include "art_code.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010020#include "base/bit_utils.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010021#include "quick/quick_method_frame_info.h"
Nicolas Geoffray6bc43742015-10-12 18:11:10 +010022#include "stack.h"
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() {
Vladimir Marko80afd022015-05-19 18:08:00 +010030 std::fill_n(gprs_, arraysize(gprs_), nullptr);
31 std::fill_n(fprs_, arraysize(fprs_), nullptr);
Mathieu Chartier67022432012-11-29 18:04:50 -080032 gprs_[ESP] = &esp_;
Andreas Gampe639bdd12015-06-03 11:22:45 -070033 gprs_[EAX] = &arg0_;
Mathieu Chartier67022432012-11-29 18:04:50 -080034 // Initialize registers with easy to spot debug values.
35 esp_ = X86Context::kBadGprBase + ESP;
36 eip_ = X86Context::kBadGprBase + kNumberOfCpuRegisters;
Andreas Gampe639bdd12015-06-03 11:22:45 -070037 arg0_ = 0;
Ian Rogers67375ac2011-09-14 00:55:44 -070038}
39
Ian Rogers0399dde2012-06-06 17:09:28 -070040void X86Context::FillCalleeSaves(const StackVisitor& fr) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +010041 ArtCode code = fr.GetCurrentCode();
42 const QuickMethodFrameInfo frame_info = code.GetQuickFrameInfo();
Vladimir Marko80afd022015-05-19 18:08:00 +010043 int spill_pos = 0;
44
45 // Core registers come first, from the highest down to the lowest.
46 uint32_t core_regs =
47 frame_info.CoreSpillMask() & ~(static_cast<uint32_t>(-1) << kNumberOfCpuRegisters);
48 DCHECK_EQ(1, POPCOUNT(frame_info.CoreSpillMask() & ~core_regs)); // Return address spill.
49 for (uint32_t core_reg : HighToLowBits(core_regs)) {
50 gprs_[core_reg] = fr.CalleeSaveAddress(spill_pos, frame_info.FrameSizeInBytes());
51 ++spill_pos;
Ian Rogers67375ac2011-09-14 00:55:44 -070052 }
Vladimir Marko80afd022015-05-19 18:08:00 +010053 DCHECK_EQ(spill_pos, POPCOUNT(frame_info.CoreSpillMask()) - 1);
54
55 // FP registers come second, from the highest down to the lowest.
56 uint32_t fp_regs = frame_info.FpSpillMask();
57 DCHECK_EQ(0u, fp_regs & (static_cast<uint32_t>(-1) << kNumberOfFloatRegisters));
58 for (uint32_t fp_reg : HighToLowBits(fp_regs)) {
59 // Two void* per XMM register.
Vladimir Marko0eb42512015-05-26 20:42:30 +010060 fprs_[2 * fp_reg] = reinterpret_cast<uint32_t*>(
61 fr.CalleeSaveAddress(spill_pos + 1, frame_info.FrameSizeInBytes()));
62 fprs_[2 * fp_reg + 1] = reinterpret_cast<uint32_t*>(
63 fr.CalleeSaveAddress(spill_pos, frame_info.FrameSizeInBytes()));
Vladimir Marko80afd022015-05-19 18:08:00 +010064 spill_pos += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +000065 }
Vladimir Marko80afd022015-05-19 18:08:00 +010066 DCHECK_EQ(spill_pos,
67 POPCOUNT(frame_info.CoreSpillMask()) - 1 + 2 * POPCOUNT(frame_info.FpSpillMask()));
Ian Rogers67375ac2011-09-14 00:55:44 -070068}
69
Elliott Hughes9c750f92012-04-05 12:07:59 -070070void X86Context::SmashCallerSaves() {
Mathieu Chartier67022432012-11-29 18:04:50 -080071 // This needs to be 0 because we want a null/zero return value.
Ian Rogersef7d42f2014-01-06 12:55:46 -080072 gprs_[EAX] = const_cast<uintptr_t*>(&gZero);
73 gprs_[EDX] = const_cast<uintptr_t*>(&gZero);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020074 gprs_[ECX] = nullptr;
75 gprs_[EBX] = nullptr;
Mark P Mendell966c3ae2015-01-27 15:45:27 +000076 memset(&fprs_[0], '\0', sizeof(fprs_));
Mathieu Chartier67022432012-11-29 18:04:50 -080077}
78
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010079void X86Context::SetGPR(uint32_t reg, uintptr_t value) {
Brian Carlstrom6f675172013-03-31 00:08:13 -070080 CHECK_LT(reg, static_cast<uint32_t>(kNumberOfCpuRegisters));
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010081 DCHECK(IsAccessibleGPR(reg));
Mathieu Chartier67022432012-11-29 18:04:50 -080082 CHECK_NE(gprs_[reg], &gZero);
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010083 *gprs_[reg] = value;
Elliott Hughes9c750f92012-04-05 12:07:59 -070084}
85
Mark P Mendell966c3ae2015-01-27 15:45:27 +000086void X86Context::SetFPR(uint32_t reg, uintptr_t value) {
87 CHECK_LT(reg, static_cast<uint32_t>(kNumberOfFloatRegisters));
88 DCHECK(IsAccessibleFPR(reg));
89 CHECK_NE(fprs_[reg], reinterpret_cast<const uint32_t*>(&gZero));
90 *fprs_[reg] = value;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070091}
92
Ian Rogersbdb03912011-09-14 00:55:44 -070093void X86Context::DoLongJump() {
Elliott Hughes85d15452011-09-16 17:33:01 -070094#if defined(__i386__)
Mathieu Chartier67022432012-11-29 18:04:50 -080095 // Array of GPR values, filled from the context backward for the long jump pop. We add a slot at
96 // the top for the stack pointer that doesn't get popped in a pop-all.
97 volatile uintptr_t gprs[kNumberOfCpuRegisters + 1];
98 for (size_t i = 0; i < kNumberOfCpuRegisters; ++i) {
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020099 gprs[kNumberOfCpuRegisters - i - 1] = gprs_[i] != nullptr ? *gprs_[i] : X86Context::kBadGprBase + i;
Mathieu Chartier67022432012-11-29 18:04:50 -0800100 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000101 uint32_t fprs[kNumberOfFloatRegisters];
102 for (size_t i = 0; i < kNumberOfFloatRegisters; ++i) {
103 fprs[i] = fprs_[i] != nullptr ? *fprs_[i] : X86Context::kBadFprBase + i;
104 }
Mathieu Chartier67022432012-11-29 18:04:50 -0800105 // We want to load the stack pointer one slot below so that the ret will pop eip.
Ian Rogers13735952014-10-08 12:43:28 -0700106 uintptr_t esp = gprs[kNumberOfCpuRegisters - ESP - 1] - sizeof(intptr_t);
Mathieu Chartier67022432012-11-29 18:04:50 -0800107 gprs[kNumberOfCpuRegisters] = esp;
108 *(reinterpret_cast<uintptr_t*>(esp)) = eip_;
Elliott Hughes7834cbd2012-05-14 18:25:16 -0700109 __asm__ __volatile__(
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000110 "movl %1, %%ebx\n\t" // Address base of FPRs.
111 "movsd 0(%%ebx), %%xmm0\n\t" // Load up XMM0-XMM7.
112 "movsd 8(%%ebx), %%xmm1\n\t"
113 "movsd 16(%%ebx), %%xmm2\n\t"
114 "movsd 24(%%ebx), %%xmm3\n\t"
115 "movsd 32(%%ebx), %%xmm4\n\t"
116 "movsd 40(%%ebx), %%xmm5\n\t"
117 "movsd 48(%%ebx), %%xmm6\n\t"
118 "movsd 56(%%ebx), %%xmm7\n\t"
Mathieu Chartier67022432012-11-29 18:04:50 -0800119 "movl %0, %%esp\n\t" // ESP points to gprs.
120 "popal\n\t" // Load all registers except ESP and EIP with values in gprs.
121 "popl %%esp\n\t" // Load stack pointer.
122 "ret\n\t" // From higher in the stack pop eip.
123 : // output.
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000124 : "g"(&gprs[0]), "g"(&fprs[0]) // input.
Mathieu Chartier67022432012-11-29 18:04:50 -0800125 :); // clobber.
Elliott Hughes85d15452011-09-16 17:33:01 -0700126#else
Ian Rogersef7d42f2014-01-06 12:55:46 -0800127 UNIMPLEMENTED(FATAL);
Elliott Hughes85d15452011-09-16 17:33:01 -0700128#endif
Andreas Gampe794ad762015-02-23 08:12:24 -0800129 UNREACHABLE();
Ian Rogersbdb03912011-09-14 00:55:44 -0700130}
131
132} // namespace x86
133} // namespace art