blob: 4efdf81d5e5bf5c334baded949f9ba79643bc97b [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 Rogers67375ac2011-09-14 00:55:44 -070019#include "object.h"
Elliott Hughes85d15452011-09-16 17:33:01 -070020
Ian Rogersbdb03912011-09-14 00:55:44 -070021namespace art {
22namespace x86 {
23
Mathieu Chartier67022432012-11-29 18:04:50 -080024static const uint32_t gZero = 0;
25
26void X86Context::Reset() {
27 for (int i = 0; i < kNumberOfCpuRegisters; i++) {
28 gprs_[i] = NULL;
Ian Rogers67375ac2011-09-14 00:55:44 -070029 }
Mathieu Chartier67022432012-11-29 18:04:50 -080030 gprs_[ESP] = &esp_;
31 // Initialize registers with easy to spot debug values.
32 esp_ = X86Context::kBadGprBase + ESP;
33 eip_ = X86Context::kBadGprBase + kNumberOfCpuRegisters;
Ian Rogers67375ac2011-09-14 00:55:44 -070034}
35
Ian Rogers0399dde2012-06-06 17:09:28 -070036void X86Context::FillCalleeSaves(const StackVisitor& fr) {
Mathieu Chartier66f19252012-09-18 08:57:04 -070037 AbstractMethod* method = fr.GetMethod();
Ian Rogers67375ac2011-09-14 00:55:44 -070038 uint32_t core_spills = method->GetCoreSpillMask();
39 size_t spill_count = __builtin_popcount(core_spills);
Ian Rogers0399dde2012-06-06 17:09:28 -070040 DCHECK_EQ(method->GetFpSpillMask(), 0u);
41 size_t frame_size = method->GetFrameSizeInBytes();
Ian Rogers67375ac2011-09-14 00:55:44 -070042 if (spill_count > 0) {
Mathieu Chartier67022432012-11-29 18:04:50 -080043 // Lowest number spill is farthest away, walk registers and fill into context.
Ian Rogers7caad772012-03-30 01:07:54 -070044 int j = 2; // Offset j to skip return address spill.
Mathieu Chartier67022432012-11-29 18:04:50 -080045 for (int i = 0; i < kNumberOfCpuRegisters; i++) {
Ian Rogers67375ac2011-09-14 00:55:44 -070046 if (((core_spills >> i) & 1) != 0) {
Mathieu Chartier67022432012-11-29 18:04:50 -080047 gprs_[i] = fr.CalleeSaveAddress(spill_count - j, frame_size);
Ian Rogers67375ac2011-09-14 00:55:44 -070048 j++;
49 }
50 }
51 }
52}
53
Elliott Hughes9c750f92012-04-05 12:07:59 -070054void X86Context::SmashCallerSaves() {
Mathieu Chartier67022432012-11-29 18:04:50 -080055 // This needs to be 0 because we want a null/zero return value.
56 gprs_[EAX] = const_cast<uint32_t*>(&gZero);
57 gprs_[EDX] = const_cast<uint32_t*>(&gZero);
58 gprs_[ECX] = NULL;
59 gprs_[EBX] = NULL;
60}
61
62void X86Context::SetGPR(uint32_t reg, uintptr_t value){
63 CHECK_LT(reg, kNumberOfCpuRegisters);
64 CHECK_NE(gprs_[reg], &gZero);
65 CHECK(gprs_[reg] != NULL);
66 *gprs_[reg] = value;
Elliott Hughes9c750f92012-04-05 12:07:59 -070067}
68
Ian Rogersbdb03912011-09-14 00:55:44 -070069void X86Context::DoLongJump() {
Elliott Hughes85d15452011-09-16 17:33:01 -070070#if defined(__i386__)
Mathieu Chartier67022432012-11-29 18:04:50 -080071 // Array of GPR values, filled from the context backward for the long jump pop. We add a slot at
72 // the top for the stack pointer that doesn't get popped in a pop-all.
73 volatile uintptr_t gprs[kNumberOfCpuRegisters + 1];
74 for (size_t i = 0; i < kNumberOfCpuRegisters; ++i) {
75 gprs[kNumberOfCpuRegisters - i - 1] = gprs_[i] != NULL ? *gprs_[i] : X86Context::kBadGprBase + i;
76 }
77 // We want to load the stack pointer one slot below so that the ret will pop eip.
78 uintptr_t esp = gprs[kNumberOfCpuRegisters - ESP - 1] - kWordSize;
79 gprs[kNumberOfCpuRegisters] = esp;
80 *(reinterpret_cast<uintptr_t*>(esp)) = eip_;
Elliott Hughes7834cbd2012-05-14 18:25:16 -070081 __asm__ __volatile__(
Mathieu Chartier67022432012-11-29 18:04:50 -080082 "movl %0, %%esp\n\t" // ESP points to gprs.
83 "popal\n\t" // Load all registers except ESP and EIP with values in gprs.
84 "popl %%esp\n\t" // Load stack pointer.
85 "ret\n\t" // From higher in the stack pop eip.
86 : // output.
87 : "g"(&gprs[0]) // input.
88 :); // clobber.
Elliott Hughes85d15452011-09-16 17:33:01 -070089#else
Ian Rogers67375ac2011-09-14 00:55:44 -070090 UNIMPLEMENTED(FATAL);
Elliott Hughes85d15452011-09-16 17:33:01 -070091#endif
Ian Rogersbdb03912011-09-14 00:55:44 -070092}
93
94} // namespace x86
95} // namespace art