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 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 19 | #include "art_method-inl.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 20 | #include "base/bit_utils.h" |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 21 | #include "quick/quick_method_frame_info.h" |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 22 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 23 | namespace art { |
| 24 | namespace x86 { |
| 25 | |
Sebastien Hertz | 0bcb290 | 2014-06-17 15:52:45 +0200 | [diff] [blame] | 26 | static constexpr uintptr_t gZero = 0; |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 27 | |
| 28 | void X86Context::Reset() { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 29 | std::fill_n(gprs_, arraysize(gprs_), nullptr); |
| 30 | std::fill_n(fprs_, arraysize(fprs_), nullptr); |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 31 | gprs_[ESP] = &esp_; |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame^] | 32 | gprs_[EAX] = &arg0_; |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 33 | // Initialize registers with easy to spot debug values. |
| 34 | esp_ = X86Context::kBadGprBase + ESP; |
| 35 | eip_ = X86Context::kBadGprBase + kNumberOfCpuRegisters; |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame^] | 36 | arg0_ = 0; |
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) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 40 | ArtMethod* method = fr.GetMethod(); |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 41 | const QuickMethodFrameInfo frame_info = method->GetQuickFrameInfo(); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 42 | int spill_pos = 0; |
| 43 | |
| 44 | // Core registers come first, from the highest down to the lowest. |
| 45 | uint32_t core_regs = |
| 46 | frame_info.CoreSpillMask() & ~(static_cast<uint32_t>(-1) << kNumberOfCpuRegisters); |
| 47 | DCHECK_EQ(1, POPCOUNT(frame_info.CoreSpillMask() & ~core_regs)); // Return address spill. |
| 48 | for (uint32_t core_reg : HighToLowBits(core_regs)) { |
| 49 | gprs_[core_reg] = fr.CalleeSaveAddress(spill_pos, frame_info.FrameSizeInBytes()); |
| 50 | ++spill_pos; |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 51 | } |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 52 | DCHECK_EQ(spill_pos, POPCOUNT(frame_info.CoreSpillMask()) - 1); |
| 53 | |
| 54 | // FP registers come second, from the highest down to the lowest. |
| 55 | uint32_t fp_regs = frame_info.FpSpillMask(); |
| 56 | DCHECK_EQ(0u, fp_regs & (static_cast<uint32_t>(-1) << kNumberOfFloatRegisters)); |
| 57 | for (uint32_t fp_reg : HighToLowBits(fp_regs)) { |
| 58 | // Two void* per XMM register. |
Vladimir Marko | 0eb4251 | 2015-05-26 20:42:30 +0100 | [diff] [blame] | 59 | fprs_[2 * fp_reg] = reinterpret_cast<uint32_t*>( |
| 60 | fr.CalleeSaveAddress(spill_pos + 1, frame_info.FrameSizeInBytes())); |
| 61 | fprs_[2 * fp_reg + 1] = reinterpret_cast<uint32_t*>( |
| 62 | fr.CalleeSaveAddress(spill_pos, frame_info.FrameSizeInBytes())); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 63 | spill_pos += 2; |
Mark P Mendell | 966c3ae | 2015-01-27 15:45:27 +0000 | [diff] [blame] | 64 | } |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 65 | DCHECK_EQ(spill_pos, |
| 66 | POPCOUNT(frame_info.CoreSpillMask()) - 1 + 2 * POPCOUNT(frame_info.FpSpillMask())); |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Elliott Hughes | 9c750f9 | 2012-04-05 12:07:59 -0700 | [diff] [blame] | 69 | void X86Context::SmashCallerSaves() { |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 70 | // 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] | 71 | gprs_[EAX] = const_cast<uintptr_t*>(&gZero); |
| 72 | gprs_[EDX] = const_cast<uintptr_t*>(&gZero); |
Sebastien Hertz | 0bcb290 | 2014-06-17 15:52:45 +0200 | [diff] [blame] | 73 | gprs_[ECX] = nullptr; |
| 74 | gprs_[EBX] = nullptr; |
Mark P Mendell | 966c3ae | 2015-01-27 15:45:27 +0000 | [diff] [blame] | 75 | memset(&fprs_[0], '\0', sizeof(fprs_)); |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Sebastien Hertz | 96ba8dc | 2015-01-22 18:57:14 +0100 | [diff] [blame] | 78 | void X86Context::SetGPR(uint32_t reg, uintptr_t value) { |
Brian Carlstrom | 6f67517 | 2013-03-31 00:08:13 -0700 | [diff] [blame] | 79 | CHECK_LT(reg, static_cast<uint32_t>(kNumberOfCpuRegisters)); |
Sebastien Hertz | 96ba8dc | 2015-01-22 18:57:14 +0100 | [diff] [blame] | 80 | DCHECK(IsAccessibleGPR(reg)); |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 81 | CHECK_NE(gprs_[reg], &gZero); |
Sebastien Hertz | 96ba8dc | 2015-01-22 18:57:14 +0100 | [diff] [blame] | 82 | *gprs_[reg] = value; |
Elliott Hughes | 9c750f9 | 2012-04-05 12:07:59 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Mark P Mendell | 966c3ae | 2015-01-27 15:45:27 +0000 | [diff] [blame] | 85 | void X86Context::SetFPR(uint32_t reg, uintptr_t value) { |
| 86 | CHECK_LT(reg, static_cast<uint32_t>(kNumberOfFloatRegisters)); |
| 87 | DCHECK(IsAccessibleFPR(reg)); |
| 88 | CHECK_NE(fprs_[reg], reinterpret_cast<const uint32_t*>(&gZero)); |
| 89 | *fprs_[reg] = value; |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 92 | void X86Context::DoLongJump() { |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 93 | #if defined(__i386__) |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 94 | // Array of GPR values, filled from the context backward for the long jump pop. We add a slot at |
| 95 | // the top for the stack pointer that doesn't get popped in a pop-all. |
| 96 | volatile uintptr_t gprs[kNumberOfCpuRegisters + 1]; |
| 97 | for (size_t i = 0; i < kNumberOfCpuRegisters; ++i) { |
Sebastien Hertz | 0bcb290 | 2014-06-17 15:52:45 +0200 | [diff] [blame] | 98 | gprs[kNumberOfCpuRegisters - i - 1] = gprs_[i] != nullptr ? *gprs_[i] : X86Context::kBadGprBase + i; |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 99 | } |
Mark P Mendell | 966c3ae | 2015-01-27 15:45:27 +0000 | [diff] [blame] | 100 | uint32_t fprs[kNumberOfFloatRegisters]; |
| 101 | for (size_t i = 0; i < kNumberOfFloatRegisters; ++i) { |
| 102 | fprs[i] = fprs_[i] != nullptr ? *fprs_[i] : X86Context::kBadFprBase + i; |
| 103 | } |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 104 | // 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] | 105 | uintptr_t esp = gprs[kNumberOfCpuRegisters - ESP - 1] - sizeof(intptr_t); |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 106 | gprs[kNumberOfCpuRegisters] = esp; |
| 107 | *(reinterpret_cast<uintptr_t*>(esp)) = eip_; |
Elliott Hughes | 7834cbd | 2012-05-14 18:25:16 -0700 | [diff] [blame] | 108 | __asm__ __volatile__( |
Mark P Mendell | 966c3ae | 2015-01-27 15:45:27 +0000 | [diff] [blame] | 109 | "movl %1, %%ebx\n\t" // Address base of FPRs. |
| 110 | "movsd 0(%%ebx), %%xmm0\n\t" // Load up XMM0-XMM7. |
| 111 | "movsd 8(%%ebx), %%xmm1\n\t" |
| 112 | "movsd 16(%%ebx), %%xmm2\n\t" |
| 113 | "movsd 24(%%ebx), %%xmm3\n\t" |
| 114 | "movsd 32(%%ebx), %%xmm4\n\t" |
| 115 | "movsd 40(%%ebx), %%xmm5\n\t" |
| 116 | "movsd 48(%%ebx), %%xmm6\n\t" |
| 117 | "movsd 56(%%ebx), %%xmm7\n\t" |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 118 | "movl %0, %%esp\n\t" // ESP points to gprs. |
| 119 | "popal\n\t" // Load all registers except ESP and EIP with values in gprs. |
| 120 | "popl %%esp\n\t" // Load stack pointer. |
| 121 | "ret\n\t" // From higher in the stack pop eip. |
| 122 | : // output. |
Mark P Mendell | 966c3ae | 2015-01-27 15:45:27 +0000 | [diff] [blame] | 123 | : "g"(&gprs[0]), "g"(&fprs[0]) // input. |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 124 | :); // clobber. |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 125 | #else |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 126 | UNIMPLEMENTED(FATAL); |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 127 | #endif |
Andreas Gampe | 794ad76 | 2015-02-23 08:12:24 -0800 | [diff] [blame] | 128 | UNREACHABLE(); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | } // namespace x86 |
| 132 | } // namespace art |