blob: 9d930ca5a86dc6aa0016b849ffde62130add7682 [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
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019#include "mirror/abstract_method.h"
20#include "stack.h"
Elliott Hughes85d15452011-09-16 17:33:01 -070021
Ian Rogersbdb03912011-09-14 00:55:44 -070022namespace art {
23namespace x86 {
24
Mathieu Chartier67022432012-11-29 18:04:50 -080025static const uint32_t gZero = 0;
26
27void X86Context::Reset() {
28 for (int i = 0; i < kNumberOfCpuRegisters; i++) {
29 gprs_[i] = NULL;
Ian Rogers67375ac2011-09-14 00:55:44 -070030 }
Mathieu Chartier67022432012-11-29 18:04:50 -080031 gprs_[ESP] = &esp_;
32 // Initialize registers with easy to spot debug values.
33 esp_ = X86Context::kBadGprBase + ESP;
34 eip_ = X86Context::kBadGprBase + kNumberOfCpuRegisters;
Ian Rogers67375ac2011-09-14 00:55:44 -070035}
36
Ian Rogers0399dde2012-06-06 17:09:28 -070037void X86Context::FillCalleeSaves(const StackVisitor& fr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038 mirror::AbstractMethod* method = fr.GetMethod();
Ian Rogers67375ac2011-09-14 00:55:44 -070039 uint32_t core_spills = method->GetCoreSpillMask();
40 size_t spill_count = __builtin_popcount(core_spills);
Ian Rogers0399dde2012-06-06 17:09:28 -070041 DCHECK_EQ(method->GetFpSpillMask(), 0u);
42 size_t frame_size = method->GetFrameSizeInBytes();
Ian Rogers67375ac2011-09-14 00:55:44 -070043 if (spill_count > 0) {
Mathieu Chartier67022432012-11-29 18:04:50 -080044 // Lowest number spill is farthest away, walk registers and fill into context.
Ian Rogers7caad772012-03-30 01:07:54 -070045 int j = 2; // Offset j to skip return address spill.
Mathieu Chartier67022432012-11-29 18:04:50 -080046 for (int i = 0; i < kNumberOfCpuRegisters; i++) {
Ian Rogers67375ac2011-09-14 00:55:44 -070047 if (((core_spills >> i) & 1) != 0) {
Mathieu Chartier67022432012-11-29 18:04:50 -080048 gprs_[i] = fr.CalleeSaveAddress(spill_count - j, frame_size);
Ian Rogers67375ac2011-09-14 00:55:44 -070049 j++;
50 }
51 }
52 }
53}
54
Elliott Hughes9c750f92012-04-05 12:07:59 -070055void X86Context::SmashCallerSaves() {
Mathieu Chartier67022432012-11-29 18:04:50 -080056 // This needs to be 0 because we want a null/zero return value.
57 gprs_[EAX] = const_cast<uint32_t*>(&gZero);
58 gprs_[EDX] = const_cast<uint32_t*>(&gZero);
59 gprs_[ECX] = NULL;
60 gprs_[EBX] = NULL;
61}
62
63void X86Context::SetGPR(uint32_t reg, uintptr_t value){
64 CHECK_LT(reg, kNumberOfCpuRegisters);
65 CHECK_NE(gprs_[reg], &gZero);
66 CHECK(gprs_[reg] != NULL);
67 *gprs_[reg] = value;
Elliott Hughes9c750f92012-04-05 12:07:59 -070068}
69
Ian Rogersbdb03912011-09-14 00:55:44 -070070void X86Context::DoLongJump() {
Elliott Hughes85d15452011-09-16 17:33:01 -070071#if defined(__i386__)
Mathieu Chartier67022432012-11-29 18:04:50 -080072 // Array of GPR values, filled from the context backward for the long jump pop. We add a slot at
73 // the top for the stack pointer that doesn't get popped in a pop-all.
74 volatile uintptr_t gprs[kNumberOfCpuRegisters + 1];
75 for (size_t i = 0; i < kNumberOfCpuRegisters; ++i) {
76 gprs[kNumberOfCpuRegisters - i - 1] = gprs_[i] != NULL ? *gprs_[i] : X86Context::kBadGprBase + i;
77 }
78 // We want to load the stack pointer one slot below so that the ret will pop eip.
79 uintptr_t esp = gprs[kNumberOfCpuRegisters - ESP - 1] - kWordSize;
80 gprs[kNumberOfCpuRegisters] = esp;
81 *(reinterpret_cast<uintptr_t*>(esp)) = eip_;
Elliott Hughes7834cbd2012-05-14 18:25:16 -070082 __asm__ __volatile__(
Mathieu Chartier67022432012-11-29 18:04:50 -080083 "movl %0, %%esp\n\t" // ESP points to gprs.
84 "popal\n\t" // Load all registers except ESP and EIP with values in gprs.
85 "popl %%esp\n\t" // Load stack pointer.
86 "ret\n\t" // From higher in the stack pop eip.
87 : // output.
88 : "g"(&gprs[0]) // input.
89 :); // clobber.
Elliott Hughes85d15452011-09-16 17:33:01 -070090#else
Ian Rogers67375ac2011-09-14 00:55:44 -070091 UNIMPLEMENTED(FATAL);
Elliott Hughes85d15452011-09-16 17:33:01 -070092#endif
Ian Rogersbdb03912011-09-14 00:55:44 -070093}
94
95} // namespace x86
96} // namespace art