Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 16 | |
| 17 | #include "context_x86.h" |
| 18 | |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 19 | #include "mirror/art_method-inl.h" |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 20 | #include "quick/quick_method_frame_info.h" |
Andreas Gampe | b688611 | 2014-11-03 08:47:01 -0800 | [diff] [blame] | 21 | #include "utils.h" |
| 22 | |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 23 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 24 | namespace art { |
| 25 | namespace x86 { |
| 26 | |
Sebastien Hertz | 0bcb290 | 2014-06-17 15:52:45 +0200 | [diff] [blame] | 27 | static constexpr uintptr_t gZero = 0; |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 28 | |
| 29 | void X86Context::Reset() { |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 30 | for (size_t i = 0; i < kNumberOfCpuRegisters; i++) { |
Sebastien Hertz | 0bcb290 | 2014-06-17 15:52:45 +0200 | [diff] [blame] | 31 | gprs_[i] = nullptr; |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 32 | } |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 33 | gprs_[ESP] = &esp_; |
| 34 | // Initialize registers with easy to spot debug values. |
| 35 | esp_ = X86Context::kBadGprBase + ESP; |
| 36 | eip_ = X86Context::kBadGprBase + kNumberOfCpuRegisters; |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 39 | void X86Context::FillCalleeSaves(const StackVisitor& fr) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 40 | mirror::ArtMethod* method = fr.GetMethod(); |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 41 | const QuickMethodFrameInfo frame_info = method->GetQuickFrameInfo(); |
| 42 | size_t spill_count = POPCOUNT(frame_info.CoreSpillMask()); |
| 43 | DCHECK_EQ(frame_info.FpSpillMask(), 0u); |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 44 | if (spill_count > 0) { |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 45 | // Lowest number spill is farthest away, walk registers and fill into context. |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 46 | int j = 2; // Offset j to skip return address spill. |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 47 | for (int i = 0; i < kNumberOfCpuRegisters; i++) { |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 48 | if (((frame_info.CoreSpillMask() >> i) & 1) != 0) { |
| 49 | gprs_[i] = fr.CalleeSaveAddress(spill_count - j, frame_info.FrameSizeInBytes()); |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 50 | j++; |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
Elliott Hughes | 9c750f9 | 2012-04-05 12:07:59 -0700 | [diff] [blame] | 56 | void X86Context::SmashCallerSaves() { |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 57 | // This needs to be 0 because we want a null/zero return value. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 58 | gprs_[EAX] = const_cast<uintptr_t*>(&gZero); |
| 59 | gprs_[EDX] = const_cast<uintptr_t*>(&gZero); |
Sebastien Hertz | 0bcb290 | 2014-06-17 15:52:45 +0200 | [diff] [blame] | 60 | gprs_[ECX] = nullptr; |
| 61 | gprs_[EBX] = nullptr; |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Sebastien Hertz | 0bcb290 | 2014-06-17 15:52:45 +0200 | [diff] [blame] | 64 | bool X86Context::SetGPR(uint32_t reg, uintptr_t value) { |
Brian Carlstrom | 6f67517 | 2013-03-31 00:08:13 -0700 | [diff] [blame] | 65 | CHECK_LT(reg, static_cast<uint32_t>(kNumberOfCpuRegisters)); |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 66 | CHECK_NE(gprs_[reg], &gZero); |
Sebastien Hertz | 0bcb290 | 2014-06-17 15:52:45 +0200 | [diff] [blame] | 67 | if (gprs_[reg] != nullptr) { |
| 68 | *gprs_[reg] = value; |
| 69 | return true; |
| 70 | } else { |
| 71 | return false; |
| 72 | } |
Elliott Hughes | 9c750f9 | 2012-04-05 12:07:59 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame^] | 75 | bool X86Context::GetFPR(uint32_t reg ATTRIBUTE_UNUSED, uintptr_t* val ATTRIBUTE_UNUSED) { |
| 76 | LOG(FATAL) << "Floating-point registers are all caller save in X86"; |
| 77 | UNREACHABLE(); |
| 78 | } |
| 79 | |
| 80 | bool X86Context::SetFPR(uint32_t reg ATTRIBUTE_UNUSED, uintptr_t value ATTRIBUTE_UNUSED) { |
| 81 | LOG(FATAL) << "Floating-point registers are all caller save in X86"; |
| 82 | UNREACHABLE(); |
| 83 | } |
| 84 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 85 | void X86Context::DoLongJump() { |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 86 | #if defined(__i386__) |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 87 | // Array of GPR values, filled from the context backward for the long jump pop. We add a slot at |
| 88 | // the top for the stack pointer that doesn't get popped in a pop-all. |
| 89 | volatile uintptr_t gprs[kNumberOfCpuRegisters + 1]; |
| 90 | for (size_t i = 0; i < kNumberOfCpuRegisters; ++i) { |
Sebastien Hertz | 0bcb290 | 2014-06-17 15:52:45 +0200 | [diff] [blame] | 91 | gprs[kNumberOfCpuRegisters - i - 1] = gprs_[i] != nullptr ? *gprs_[i] : X86Context::kBadGprBase + i; |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 92 | } |
| 93 | // We want to load the stack pointer one slot below so that the ret will pop eip. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 94 | uintptr_t esp = gprs[kNumberOfCpuRegisters - ESP - 1] - sizeof(intptr_t); |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 95 | gprs[kNumberOfCpuRegisters] = esp; |
| 96 | *(reinterpret_cast<uintptr_t*>(esp)) = eip_; |
Elliott Hughes | 7834cbd | 2012-05-14 18:25:16 -0700 | [diff] [blame] | 97 | __asm__ __volatile__( |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 98 | "movl %0, %%esp\n\t" // ESP points to gprs. |
| 99 | "popal\n\t" // Load all registers except ESP and EIP with values in gprs. |
| 100 | "popl %%esp\n\t" // Load stack pointer. |
| 101 | "ret\n\t" // From higher in the stack pop eip. |
| 102 | : // output. |
| 103 | : "g"(&gprs[0]) // input. |
| 104 | :); // clobber. |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 105 | #else |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 106 | UNIMPLEMENTED(FATAL); |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 107 | #endif |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | } // namespace x86 |
| 111 | } // namespace art |