blob: 4ea4684f06eab061a3138b2e2bd5c14a9068ad85 [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 Hertz96ba8dc2015-01-22 18:57:14 +010086void X86Context::SetGPR(uint32_t reg, uintptr_t value) {
Brian Carlstrom6f675172013-03-31 00:08:13 -070087 CHECK_LT(reg, static_cast<uint32_t>(kNumberOfCpuRegisters));
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010088 DCHECK(IsAccessibleGPR(reg));
Mathieu Chartier67022432012-11-29 18:04:50 -080089 CHECK_NE(gprs_[reg], &gZero);
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010090 *gprs_[reg] = value;
Elliott Hughes9c750f92012-04-05 12:07:59 -070091}
92
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010093void X86Context::SetFPR(uint32_t reg, uintptr_t value) {
Mark Mendell3d2c8e72015-01-13 17:32:55 -050094 CHECK_LT(reg, static_cast<uint32_t>(kNumberOfFloatRegisters));
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010095 DCHECK(IsAccessibleFPR(reg));
Mark Mendell3d2c8e72015-01-13 17:32:55 -050096 CHECK_NE(fprs_[reg], reinterpret_cast<const uint32_t*>(&gZero));
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010097 *fprs_[reg] = value;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070098}
99
Ian Rogersbdb03912011-09-14 00:55:44 -0700100void X86Context::DoLongJump() {
Elliott Hughes85d15452011-09-16 17:33:01 -0700101#if defined(__i386__)
Mathieu Chartier67022432012-11-29 18:04:50 -0800102 // Array of GPR values, filled from the context backward for the long jump pop. We add a slot at
103 // the top for the stack pointer that doesn't get popped in a pop-all.
104 volatile uintptr_t gprs[kNumberOfCpuRegisters + 1];
105 for (size_t i = 0; i < kNumberOfCpuRegisters; ++i) {
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200106 gprs[kNumberOfCpuRegisters - i - 1] = gprs_[i] != nullptr ? *gprs_[i] : X86Context::kBadGprBase + i;
Mathieu Chartier67022432012-11-29 18:04:50 -0800107 }
Mark Mendell3d2c8e72015-01-13 17:32:55 -0500108 uint32_t fprs[kNumberOfFloatRegisters];
109 for (size_t i = 0; i < kNumberOfFloatRegisters; ++i) {
110 fprs[i] = fprs_[i] != nullptr ? *fprs_[i] : X86Context::kBadFprBase + i;
111 }
Mathieu Chartier67022432012-11-29 18:04:50 -0800112 // We want to load the stack pointer one slot below so that the ret will pop eip.
Ian Rogers13735952014-10-08 12:43:28 -0700113 uintptr_t esp = gprs[kNumberOfCpuRegisters - ESP - 1] - sizeof(intptr_t);
Mathieu Chartier67022432012-11-29 18:04:50 -0800114 gprs[kNumberOfCpuRegisters] = esp;
115 *(reinterpret_cast<uintptr_t*>(esp)) = eip_;
Elliott Hughes7834cbd2012-05-14 18:25:16 -0700116 __asm__ __volatile__(
Mark Mendell3d2c8e72015-01-13 17:32:55 -0500117 "movl %1, %%ebx\n\t" // Address base of FPRs.
118 "movsd 0(%%ebx), %%xmm0\n\t" // Load up XMM0-XMM7.
119 "movsd 8(%%ebx), %%xmm1\n\t"
120 "movsd 16(%%ebx), %%xmm2\n\t"
121 "movsd 24(%%ebx), %%xmm3\n\t"
122 "movsd 32(%%ebx), %%xmm4\n\t"
123 "movsd 40(%%ebx), %%xmm5\n\t"
124 "movsd 48(%%ebx), %%xmm6\n\t"
125 "movsd 56(%%ebx), %%xmm7\n\t"
Mathieu Chartier67022432012-11-29 18:04:50 -0800126 "movl %0, %%esp\n\t" // ESP points to gprs.
127 "popal\n\t" // Load all registers except ESP and EIP with values in gprs.
128 "popl %%esp\n\t" // Load stack pointer.
129 "ret\n\t" // From higher in the stack pop eip.
130 : // output.
Mark Mendell3d2c8e72015-01-13 17:32:55 -0500131 : "g"(&gprs[0]), "g"(&fprs[0]) // input.
Mathieu Chartier67022432012-11-29 18:04:50 -0800132 :); // clobber.
Elliott Hughes85d15452011-09-16 17:33:01 -0700133#else
Ian Rogersef7d42f2014-01-06 12:55:46 -0800134 UNIMPLEMENTED(FATAL);
Elliott Hughes85d15452011-09-16 17:33:01 -0700135#endif
Ian Rogersbdb03912011-09-14 00:55:44 -0700136}
137
138} // namespace x86
139} // namespace art