Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [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 | */ |
| 16 | |
| 17 | #include "stubs/stubs.h" |
| 18 | |
| 19 | #include "jni_internal.h" |
| 20 | #include "oat/runtime/oat_support_entrypoints.h" |
| 21 | #include "oat/utils/arm/assembler_arm.h" |
| 22 | #include "oat/utils/mips/assembler_mips.h" |
| 23 | #include "oat/utils/x86/assembler_x86.h" |
| 24 | #include "sirt_ref.h" |
| 25 | #include "stack_indirect_reference_table.h" |
| 26 | |
| 27 | #define __ assembler-> |
| 28 | |
| 29 | namespace art { |
| 30 | |
| 31 | namespace arm { |
| 32 | const std::vector<uint8_t>* CreateQuickResolutionTrampoline() { |
| 33 | UniquePtr<ArmAssembler> assembler(static_cast<ArmAssembler*>(Assembler::Create(kArm))); |
| 34 | // | Out args | |
| 35 | // | Method* | <- SP on entry |
| 36 | // | LR | return address into caller |
| 37 | // | ... | callee saves |
| 38 | // | R3 | possible argument |
| 39 | // | R2 | possible argument |
| 40 | // | R1 | possible argument |
| 41 | // | R0 | junk on call to QuickResolutionTrampolineFromCode, holds result Method* |
| 42 | // | Method* | Callee save Method* set up by QuickResoltuionTrampolineFromCode |
| 43 | // Save callee saves and ready frame for exception delivery |
| 44 | RegList save = (1 << R1) | (1 << R2) | (1 << R3) | (1 << R5) | (1 << R6) | (1 << R7) | (1 << R8) | |
| 45 | (1 << R10) | (1 << R11) | (1 << LR); |
| 46 | // TODO: enable when GetCalleeSaveMethod is available at stub generation time |
| 47 | // DCHECK_EQ(save, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetCoreSpillMask()); |
| 48 | __ PushList(save); |
| 49 | __ LoadFromOffset(kLoadWord, R12, TR, ENTRYPOINT_OFFSET(pQuickResolutionTrampolineFromCode)); |
| 50 | __ mov(R3, ShifterOperand(TR)); // Pass Thread::Current() in R3 |
| 51 | __ IncreaseFrameSize(8); // 2 words of space for alignment |
| 52 | __ mov(R2, ShifterOperand(SP)); // Pass SP |
| 53 | // Call to resolution trampoline (method_idx, receiver, sp, Thread*) |
| 54 | __ blx(R12); |
| 55 | __ mov(R12, ShifterOperand(R0)); // Save code address returned into R12 |
| 56 | // Restore registers which may have been modified by GC, "R0" will hold the Method* |
| 57 | __ DecreaseFrameSize(4); |
| 58 | __ PopList((1 << R0) | save); |
| 59 | __ bx(R12); // Leaf call to method's code |
| 60 | __ bkpt(0); |
| 61 | |
| 62 | assembler->EmitSlowPaths(); |
| 63 | size_t cs = assembler->CodeSize(); |
| 64 | UniquePtr<std::vector<uint8_t> > resolution_trampoline(new std::vector<uint8_t>(cs)); |
| 65 | MemoryRegion code(&(*resolution_trampoline)[0], resolution_trampoline->size()); |
| 66 | assembler->FinalizeInstructions(code); |
| 67 | |
| 68 | return resolution_trampoline.release(); |
| 69 | } |
| 70 | |
| 71 | const std::vector<uint8_t>* CreateInterpreterToInterpreterEntry() { |
| 72 | UniquePtr<ArmAssembler> assembler(static_cast<ArmAssembler*>(Assembler::Create(kArm))); |
| 73 | |
| 74 | __ LoadFromOffset(kLoadWord, PC, R0, ENTRYPOINT_OFFSET(pInterpreterToInterpreterEntry)); |
| 75 | __ bkpt(0); |
| 76 | |
| 77 | size_t cs = assembler->CodeSize(); |
| 78 | UniquePtr<std::vector<uint8_t> > entry_stub(new std::vector<uint8_t>(cs)); |
| 79 | MemoryRegion code(&(*entry_stub)[0], entry_stub->size()); |
| 80 | assembler->FinalizeInstructions(code); |
| 81 | |
| 82 | return entry_stub.release(); |
| 83 | } |
| 84 | |
| 85 | const std::vector<uint8_t>* CreateInterpreterToQuickEntry() { |
| 86 | UniquePtr<ArmAssembler> assembler(static_cast<ArmAssembler*>(Assembler::Create(kArm))); |
| 87 | |
| 88 | __ LoadFromOffset(kLoadWord, PC, R0, ENTRYPOINT_OFFSET(pInterpreterToQuickEntry)); |
| 89 | __ bkpt(0); |
| 90 | |
| 91 | size_t cs = assembler->CodeSize(); |
| 92 | UniquePtr<std::vector<uint8_t> > entry_stub(new std::vector<uint8_t>(cs)); |
| 93 | MemoryRegion code(&(*entry_stub)[0], entry_stub->size()); |
| 94 | assembler->FinalizeInstructions(code); |
| 95 | |
| 96 | return entry_stub.release(); |
| 97 | } |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 98 | } // namespace arm |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 99 | |
| 100 | namespace mips { |
| 101 | const std::vector<uint8_t>* CreateQuickResolutionTrampoline() { |
| 102 | UniquePtr<MipsAssembler> assembler(static_cast<MipsAssembler*>(Assembler::Create(kMips))); |
| 103 | // | Out args | |
| 104 | // | Method* | <- SP on entry |
| 105 | // | RA | return address into caller |
| 106 | // | ... | callee saves |
| 107 | // | A3 | possible argument |
| 108 | // | A2 | possible argument |
| 109 | // | A1 | possible argument |
| 110 | // | A0/Method* | Callee save Method* set up by UnresolvedDirectMethodTrampolineFromCode |
| 111 | // Save callee saves and ready frame for exception delivery |
| 112 | __ AddConstant(SP, SP, -64); |
| 113 | __ StoreToOffset(kStoreWord, RA, SP, 60); |
| 114 | __ StoreToOffset(kStoreWord, FP, SP, 56); |
| 115 | __ StoreToOffset(kStoreWord, GP, SP, 52); |
| 116 | __ StoreToOffset(kStoreWord, S7, SP, 48); |
| 117 | __ StoreToOffset(kStoreWord, S6, SP, 44); |
| 118 | __ StoreToOffset(kStoreWord, S5, SP, 40); |
| 119 | __ StoreToOffset(kStoreWord, S4, SP, 36); |
| 120 | __ StoreToOffset(kStoreWord, S3, SP, 32); |
| 121 | __ StoreToOffset(kStoreWord, S2, SP, 28); |
| 122 | __ StoreToOffset(kStoreWord, A3, SP, 12); |
| 123 | __ StoreToOffset(kStoreWord, A2, SP, 8); |
| 124 | __ StoreToOffset(kStoreWord, A1, SP, 4); |
| 125 | |
| 126 | __ LoadFromOffset(kLoadWord, T9, S1, ENTRYPOINT_OFFSET(pQuickResolutionTrampolineFromCode)); |
| 127 | __ Move(A3, S1); // Pass Thread::Current() in A3 |
| 128 | __ Move(A2, SP); // Pass SP for Method** callee_addr |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 129 | __ Jalr(T9); // Call to resolution trampoline (method_idx, receiver, sp, Thread*) |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 130 | |
| 131 | // Restore registers which may have been modified by GC |
| 132 | __ LoadFromOffset(kLoadWord, A0, SP, 0); |
| 133 | __ LoadFromOffset(kLoadWord, A1, SP, 4); |
| 134 | __ LoadFromOffset(kLoadWord, A2, SP, 8); |
| 135 | __ LoadFromOffset(kLoadWord, A3, SP, 12); |
| 136 | __ LoadFromOffset(kLoadWord, S2, SP, 28); |
| 137 | __ LoadFromOffset(kLoadWord, S3, SP, 32); |
| 138 | __ LoadFromOffset(kLoadWord, S4, SP, 36); |
| 139 | __ LoadFromOffset(kLoadWord, S5, SP, 40); |
| 140 | __ LoadFromOffset(kLoadWord, S6, SP, 44); |
| 141 | __ LoadFromOffset(kLoadWord, S7, SP, 48); |
| 142 | __ LoadFromOffset(kLoadWord, GP, SP, 52); |
| 143 | __ LoadFromOffset(kLoadWord, FP, SP, 56); |
| 144 | __ LoadFromOffset(kLoadWord, RA, SP, 60); |
| 145 | __ AddConstant(SP, SP, 64); |
| 146 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 147 | __ Move(T9, V0); // Put method's code in T9 |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 148 | __ Jr(T9); // Leaf call to method's code |
| 149 | |
| 150 | __ Break(); |
| 151 | |
| 152 | assembler->EmitSlowPaths(); |
| 153 | size_t cs = assembler->CodeSize(); |
| 154 | UniquePtr<std::vector<uint8_t> > resolution_trampoline(new std::vector<uint8_t>(cs)); |
| 155 | MemoryRegion code(&(*resolution_trampoline)[0], resolution_trampoline->size()); |
| 156 | assembler->FinalizeInstructions(code); |
| 157 | |
| 158 | return resolution_trampoline.release(); |
| 159 | } |
| 160 | |
| 161 | const std::vector<uint8_t>* CreateInterpreterToInterpreterEntry() { |
| 162 | UniquePtr<MipsAssembler> assembler(static_cast<MipsAssembler*>(Assembler::Create(kMips))); |
| 163 | |
| 164 | __ LoadFromOffset(kLoadWord, T9, A0, ENTRYPOINT_OFFSET(pInterpreterToInterpreterEntry)); |
| 165 | __ Jr(T9); |
| 166 | __ Break(); |
| 167 | |
| 168 | size_t cs = assembler->CodeSize(); |
| 169 | UniquePtr<std::vector<uint8_t> > entry_stub(new std::vector<uint8_t>(cs)); |
| 170 | MemoryRegion code(&(*entry_stub)[0], entry_stub->size()); |
| 171 | assembler->FinalizeInstructions(code); |
| 172 | |
| 173 | return entry_stub.release(); |
| 174 | } |
| 175 | |
| 176 | const std::vector<uint8_t>* CreateInterpreterToQuickEntry() { |
| 177 | UniquePtr<MipsAssembler> assembler(static_cast<MipsAssembler*>(Assembler::Create(kMips))); |
| 178 | |
| 179 | __ LoadFromOffset(kLoadWord, T9, A0, ENTRYPOINT_OFFSET(pInterpreterToInterpreterEntry)); |
| 180 | __ Jr(T9); |
| 181 | __ Break(); |
| 182 | |
| 183 | size_t cs = assembler->CodeSize(); |
| 184 | UniquePtr<std::vector<uint8_t> > entry_stub(new std::vector<uint8_t>(cs)); |
| 185 | MemoryRegion code(&(*entry_stub)[0], entry_stub->size()); |
| 186 | assembler->FinalizeInstructions(code); |
| 187 | |
| 188 | return entry_stub.release(); |
| 189 | } |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 190 | } // namespace mips |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 191 | |
| 192 | namespace x86 { |
| 193 | const std::vector<uint8_t>* CreateQuickResolutionTrampoline() { |
| 194 | UniquePtr<X86Assembler> assembler(static_cast<X86Assembler*>(Assembler::Create(kX86))); |
| 195 | // Set up the callee save frame to conform with Runtime::CreateCalleeSaveMethod(kRefsAndArgs) |
| 196 | // return address |
| 197 | __ pushl(EDI); |
| 198 | __ pushl(ESI); |
| 199 | __ pushl(EBP); |
| 200 | __ pushl(EBX); |
| 201 | __ pushl(EDX); |
| 202 | __ pushl(ECX); |
| 203 | __ pushl(EAX); // <-- callee save Method* to go here |
| 204 | __ movl(EDX, ESP); // save ESP |
| 205 | __ fs()->pushl(Address::Absolute(Thread::SelfOffset())); // pass Thread* |
| 206 | __ pushl(EDX); // pass ESP for Method* |
| 207 | __ pushl(ECX); // pass receiver |
| 208 | __ pushl(EAX); // pass Method* |
| 209 | |
| 210 | // Call to resolve method. |
| 211 | __ Call(ThreadOffset(ENTRYPOINT_OFFSET(pQuickResolutionTrampolineFromCode)), |
| 212 | X86ManagedRegister::FromCpuRegister(ECX)); |
| 213 | |
| 214 | __ movl(EDI, EAX); // save code pointer in EDI |
| 215 | __ addl(ESP, Immediate(16)); // Pop arguments |
| 216 | __ popl(EAX); // Restore args. |
| 217 | __ popl(ECX); |
| 218 | __ popl(EDX); |
| 219 | __ popl(EBX); |
| 220 | __ popl(EBP); // Restore callee saves. |
| 221 | __ popl(ESI); |
| 222 | // Swap EDI callee save with code pointer |
| 223 | __ xchgl(EDI, Address(ESP, 0)); |
| 224 | // Tail call to intended method. |
| 225 | __ ret(); |
| 226 | |
| 227 | assembler->EmitSlowPaths(); |
| 228 | size_t cs = assembler->CodeSize(); |
| 229 | UniquePtr<std::vector<uint8_t> > resolution_trampoline(new std::vector<uint8_t>(cs)); |
| 230 | MemoryRegion code(&(*resolution_trampoline)[0], resolution_trampoline->size()); |
| 231 | assembler->FinalizeInstructions(code); |
| 232 | |
| 233 | return resolution_trampoline.release(); |
| 234 | } |
| 235 | |
| 236 | const std::vector<uint8_t>* CreateInterpreterToInterpreterEntry() { |
| 237 | UniquePtr<X86Assembler> assembler(static_cast<X86Assembler*>(Assembler::Create(kX86))); |
| 238 | |
| 239 | __ fs()->jmp(Address::Absolute(ThreadOffset(ENTRYPOINT_OFFSET(pInterpreterToInterpreterEntry)))); |
| 240 | |
| 241 | size_t cs = assembler->CodeSize(); |
| 242 | UniquePtr<std::vector<uint8_t> > entry_stub(new std::vector<uint8_t>(cs)); |
| 243 | MemoryRegion code(&(*entry_stub)[0], entry_stub->size()); |
| 244 | assembler->FinalizeInstructions(code); |
| 245 | |
| 246 | return entry_stub.release(); |
| 247 | } |
| 248 | |
| 249 | const std::vector<uint8_t>* CreateInterpreterToQuickEntry() { |
| 250 | UniquePtr<X86Assembler> assembler(static_cast<X86Assembler*>(Assembler::Create(kX86))); |
| 251 | |
| 252 | __ fs()->jmp(Address::Absolute(ThreadOffset(ENTRYPOINT_OFFSET(pInterpreterToQuickEntry)))); |
| 253 | |
| 254 | size_t cs = assembler->CodeSize(); |
| 255 | UniquePtr<std::vector<uint8_t> > entry_stub(new std::vector<uint8_t>(cs)); |
| 256 | MemoryRegion code(&(*entry_stub)[0], entry_stub->size()); |
| 257 | assembler->FinalizeInstructions(code); |
| 258 | |
| 259 | return entry_stub.release(); |
| 260 | } |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 261 | } // namespace x86 |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 262 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 263 | } // namespace art |