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 | |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 19 | #include "object.h" |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 20 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 21 | namespace art { |
| 22 | namespace x86 { |
| 23 | |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 24 | X86Context::X86Context() { |
Ian Rogers | ad42e13 | 2011-09-17 20:23:33 -0700 | [diff] [blame] | 25 | #ifndef NDEBUG |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 26 | // Initialize registers with easy to spot debug values. |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 27 | for (int i = 0; i < 8; i++) { |
Elliott Hughes | 9c750f9 | 2012-04-05 12:07:59 -0700 | [diff] [blame^] | 28 | gprs_[i] = kBadGprBase + i; |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 29 | } |
Ian Rogers | ad42e13 | 2011-09-17 20:23:33 -0700 | [diff] [blame] | 30 | eip_ = 0xEBAD601F; |
| 31 | #endif |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | void X86Context::FillCalleeSaves(const Frame& fr) { |
| 35 | Method* method = fr.GetMethod(); |
| 36 | uint32_t core_spills = method->GetCoreSpillMask(); |
| 37 | size_t spill_count = __builtin_popcount(core_spills); |
| 38 | CHECK_EQ(method->GetFpSpillMask(), 0u); |
| 39 | if (spill_count > 0) { |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 40 | // Lowest number spill is furthest away, walk registers and fill into context. |
| 41 | int j = 2; // Offset j to skip return address spill. |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 42 | for (int i = 0; i < 8; i++) { |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 43 | if (((core_spills >> i) & 1) != 0) { |
| 44 | gprs_[i] = fr.LoadCalleeSave(spill_count - j); |
| 45 | j++; |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
Elliott Hughes | 9c750f9 | 2012-04-05 12:07:59 -0700 | [diff] [blame^] | 51 | void X86Context::SmashCallerSaves() { |
| 52 | gprs_[EAX] = 0; // This needs to be 0 because we want a null/zero return value. |
| 53 | gprs_[ECX] = kBadGprBase + ECX; |
| 54 | gprs_[EDX] = kBadGprBase + EDX; |
| 55 | gprs_[EBX] = kBadGprBase + EBX; |
| 56 | } |
| 57 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 58 | void X86Context::DoLongJump() { |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 59 | #if defined(__i386__) |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 60 | // We push all the registers using memory-memory pushes, we then pop-all to get the registers |
| 61 | // set up, we then pop esp which will move us down the stack to the delivery address. At the frame |
| 62 | // where the exception will be delivered, we push EIP so that the return will take us to the |
| 63 | // correct delivery instruction. |
| 64 | gprs_[ESP] -= 4; |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 65 | *(reinterpret_cast<uintptr_t*>(gprs_[ESP])) = eip_; |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 66 | asm volatile( |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 67 | "pushl %4\n\t" |
| 68 | "pushl %0\n\t" |
| 69 | "pushl %1\n\t" |
| 70 | "pushl %2\n\t" |
| 71 | "pushl %3\n\t" |
| 72 | "pushl %4\n\t" |
| 73 | "pushl %5\n\t" |
| 74 | "pushl %6\n\t" |
| 75 | "pushl %7\n\t" |
| 76 | "popal\n\t" |
| 77 | "popl %%esp\n\t" |
| 78 | "ret\n\t" |
| 79 | : //output |
| 80 | : "g"(gprs_[EAX]), "g"(gprs_[ECX]), "g"(gprs_[EDX]), "g"(gprs_[EBX]), |
| 81 | "g"(gprs_[ESP]), "g"(gprs_[EBP]), "g"(gprs_[ESI]), "g"(gprs_[EDI]) |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 82 | :); // clobber |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 83 | #else |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 84 | UNIMPLEMENTED(FATAL); |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 85 | #endif |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | } // namespace x86 |
| 89 | } // namespace art |