blob: d7dca6455a526d8c15e79b2d4bce0406afca357d [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
Brian Carlstromea46f952013-07-30 01:26:50 -070019#include "mirror/art_method.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070020#include "mirror/object-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "stack.h"
Elliott Hughes85d15452011-09-16 17:33:01 -070022
Ian Rogersbdb03912011-09-14 00:55:44 -070023namespace art {
24namespace x86 {
25
Ian Rogersef7d42f2014-01-06 12:55:46 -080026static const uintptr_t gZero = 0;
Mathieu Chartier67022432012-11-29 18:04:50 -080027
28void X86Context::Reset() {
29 for (int i = 0; i < kNumberOfCpuRegisters; i++) {
30 gprs_[i] = NULL;
Ian Rogers67375ac2011-09-14 00:55:44 -070031 }
Mathieu Chartier67022432012-11-29 18:04:50 -080032 gprs_[ESP] = &esp_;
33 // Initialize registers with easy to spot debug values.
34 esp_ = X86Context::kBadGprBase + ESP;
35 eip_ = X86Context::kBadGprBase + kNumberOfCpuRegisters;
Ian Rogers67375ac2011-09-14 00:55:44 -070036}
37
Ian Rogers0399dde2012-06-06 17:09:28 -070038void X86Context::FillCalleeSaves(const StackVisitor& fr) {
Brian Carlstromea46f952013-07-30 01:26:50 -070039 mirror::ArtMethod* method = fr.GetMethod();
Ian Rogers67375ac2011-09-14 00:55:44 -070040 uint32_t core_spills = method->GetCoreSpillMask();
41 size_t spill_count = __builtin_popcount(core_spills);
Ian Rogers0399dde2012-06-06 17:09:28 -070042 DCHECK_EQ(method->GetFpSpillMask(), 0u);
43 size_t frame_size = method->GetFrameSizeInBytes();
Ian Rogers67375ac2011-09-14 00:55:44 -070044 if (spill_count > 0) {
Mathieu Chartier67022432012-11-29 18:04:50 -080045 // Lowest number spill is farthest away, walk registers and fill into context.
Ian Rogers7caad772012-03-30 01:07:54 -070046 int j = 2; // Offset j to skip return address spill.
Mathieu Chartier67022432012-11-29 18:04:50 -080047 for (int i = 0; i < kNumberOfCpuRegisters; i++) {
Ian Rogers67375ac2011-09-14 00:55:44 -070048 if (((core_spills >> i) & 1) != 0) {
Mathieu Chartier67022432012-11-29 18:04:50 -080049 gprs_[i] = fr.CalleeSaveAddress(spill_count - j, frame_size);
Ian Rogers67375ac2011-09-14 00:55:44 -070050 j++;
51 }
52 }
53 }
54}
55
Elliott Hughes9c750f92012-04-05 12:07:59 -070056void X86Context::SmashCallerSaves() {
Mathieu Chartier67022432012-11-29 18:04:50 -080057 // This needs to be 0 because we want a null/zero return value.
Ian Rogersef7d42f2014-01-06 12:55:46 -080058 gprs_[EAX] = const_cast<uintptr_t*>(&gZero);
59 gprs_[EDX] = const_cast<uintptr_t*>(&gZero);
Mathieu Chartier67022432012-11-29 18:04:50 -080060 gprs_[ECX] = NULL;
61 gprs_[EBX] = NULL;
62}
63
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070064void X86Context::SetGPR(uint32_t reg, uintptr_t value) {
Brian Carlstrom6f675172013-03-31 00:08:13 -070065 CHECK_LT(reg, static_cast<uint32_t>(kNumberOfCpuRegisters));
Mathieu Chartier67022432012-11-29 18:04:50 -080066 CHECK_NE(gprs_[reg], &gZero);
67 CHECK(gprs_[reg] != NULL);
68 *gprs_[reg] = value;
Elliott Hughes9c750f92012-04-05 12:07:59 -070069}
70
Ian Rogersbdb03912011-09-14 00:55:44 -070071void X86Context::DoLongJump() {
Elliott Hughes85d15452011-09-16 17:33:01 -070072#if defined(__i386__)
Mathieu Chartier67022432012-11-29 18:04:50 -080073 // Array of GPR values, filled from the context backward for the long jump pop. We add a slot at
74 // the top for the stack pointer that doesn't get popped in a pop-all.
75 volatile uintptr_t gprs[kNumberOfCpuRegisters + 1];
76 for (size_t i = 0; i < kNumberOfCpuRegisters; ++i) {
77 gprs[kNumberOfCpuRegisters - i - 1] = gprs_[i] != NULL ? *gprs_[i] : X86Context::kBadGprBase + i;
78 }
79 // We want to load the stack pointer one slot below so that the ret will pop eip.
80 uintptr_t esp = gprs[kNumberOfCpuRegisters - ESP - 1] - kWordSize;
81 gprs[kNumberOfCpuRegisters] = esp;
82 *(reinterpret_cast<uintptr_t*>(esp)) = eip_;
Elliott Hughes7834cbd2012-05-14 18:25:16 -070083 __asm__ __volatile__(
Mathieu Chartier67022432012-11-29 18:04:50 -080084 "movl %0, %%esp\n\t" // ESP points to gprs.
85 "popal\n\t" // Load all registers except ESP and EIP with values in gprs.
86 "popl %%esp\n\t" // Load stack pointer.
87 "ret\n\t" // From higher in the stack pop eip.
88 : // output.
89 : "g"(&gprs[0]) // input.
90 :); // clobber.
Elliott Hughes85d15452011-09-16 17:33:01 -070091#else
Ian Rogersef7d42f2014-01-06 12:55:46 -080092 UNIMPLEMENTED(FATAL);
Elliott Hughes85d15452011-09-16 17:33:01 -070093#endif
Ian Rogersbdb03912011-09-14 00:55:44 -070094}
95
96} // namespace x86
97} // namespace art