quick_trampoline_entrypoints.cc revision 36fea8dd490ab6439f391b8cd7f366c59f026fd2
1/* 2 * Copyright (C) 2012 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 "callee_save_frame.h" 18#include "common_throws.h" 19#include "dex_file-inl.h" 20#include "dex_instruction-inl.h" 21#include "entrypoints/entrypoint_utils.h" 22#include "gc/accounting/card_table-inl.h" 23#include "interpreter/interpreter.h" 24#include "invoke_arg_array_builder.h" 25#include "mirror/art_method-inl.h" 26#include "mirror/class-inl.h" 27#include "mirror/object-inl.h" 28#include "mirror/object_array-inl.h" 29#include "object_utils.h" 30#include "runtime.h" 31 32namespace art { 33 34// Visits the arguments as saved to the stack by a Runtime::kRefAndArgs callee save frame. 35class QuickArgumentVisitor { 36 // Size of each spilled GPR. 37#ifdef __LP64__ 38 static constexpr size_t kBytesPerGprSpillLocation = 8; 39#else 40 static constexpr size_t kBytesPerGprSpillLocation = 4; 41#endif 42 // Number of bytes for each out register in the caller method's frame. 43 static constexpr size_t kBytesStackArgLocation = 4; 44#if defined(__arm__) 45 // The callee save frame is pointed to by SP. 46 // | argN | | 47 // | ... | | 48 // | arg4 | | 49 // | arg3 spill | | Caller's frame 50 // | arg2 spill | | 51 // | arg1 spill | | 52 // | Method* | --- 53 // | LR | 54 // | ... | callee saves 55 // | R3 | arg3 56 // | R2 | arg2 57 // | R1 | arg1 58 // | R0 | padding 59 // | Method* | <- sp 60 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI. 61 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs. 62 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs. 63 static constexpr size_t kBytesPerFprSpillLocation = 4; // FPR spill size is 4 bytes. 64 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg. 65 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 8; // Offset of first GPR arg. 66 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 44; // Offset of return address. 67 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 48; // Frame size. 68 static size_t GprIndexToGprOffset(uint32_t gpr_index) { 69 return gpr_index * kBytesPerGprSpillLocation; 70 } 71#elif defined(__mips__) 72 // The callee save frame is pointed to by SP. 73 // | argN | | 74 // | ... | | 75 // | arg4 | | 76 // | arg3 spill | | Caller's frame 77 // | arg2 spill | | 78 // | arg1 spill | | 79 // | Method* | --- 80 // | RA | 81 // | ... | callee saves 82 // | A3 | arg3 83 // | A2 | arg2 84 // | A1 | arg1 85 // | A0/Method* | <- sp 86 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI. 87 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs. 88 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs. 89 static constexpr size_t kBytesPerFprSpillLocation = 4; // FPR spill size is 4 bytes. 90 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg. 91 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg. 92 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 60; // Offset of return address. 93 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 64; // Frame size. 94 static size_t GprIndexToGprOffset(uint32_t gpr_index) { 95 return gpr_index * kBytesPerGprSpillLocation; 96 } 97#elif defined(__i386__) 98 // The callee save frame is pointed to by SP. 99 // | argN | | 100 // | ... | | 101 // | arg4 | | 102 // | arg3 spill | | Caller's frame 103 // | arg2 spill | | 104 // | arg1 spill | | 105 // | Method* | --- 106 // | Return | 107 // | EBP,ESI,EDI | callee saves 108 // | EBX | arg3 109 // | EDX | arg2 110 // | ECX | arg1 111 // | EAX/Method* | <- sp 112 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI. 113 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs. 114 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs. 115 static constexpr size_t kBytesPerFprSpillLocation = 8; // FPR spill size is 8 bytes. 116 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg. 117 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg. 118 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28; // Offset of return address. 119 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 32; // Frame size. 120 static size_t GprIndexToGprOffset(uint32_t gpr_index) { 121 return gpr_index * kBytesPerGprSpillLocation; 122 } 123#elif defined(__x86_64__) 124 // The callee save frame is pointed to by SP. 125 // | argN | | 126 // | ... | | 127 // | reg. arg spills | | Caller's frame 128 // | Method* | --- 129 // | Return | 130 // | R15 | callee save 131 // | R14 | callee save 132 // | R13 | callee save 133 // | R12 | callee save 134 // | R9 | arg5 135 // | R8 | arg4 136 // | RSI/R6 | arg1 137 // | RBP/R5 | callee save 138 // | RBX/R3 | callee save 139 // | RDX/R2 | arg2 140 // | RCX/R1 | arg3 141 // | XMM7 | float arg 8 142 // | XMM6 | float arg 7 143 // | XMM5 | float arg 6 144 // | XMM4 | float arg 5 145 // | XMM3 | float arg 4 146 // | XMM2 | float arg 3 147 // | XMM1 | float arg 2 148 // | XMM0 | float arg 1 149 // | Padding | 150 // | RDI/Method* | <- sp 151 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI. 152 static constexpr size_t kNumQuickGprArgs = 5; // 3 arguments passed in GPRs. 153 static constexpr size_t kNumQuickFprArgs = 8; // 0 arguments passed in FPRs. 154 static constexpr size_t kBytesPerFprSpillLocation = 8; // FPR spill size is 8 bytes. 155 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg. 156 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg. 157 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168; // Offset of return address. 158 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 176; // Frame size. 159 static size_t GprIndexToGprOffset(uint32_t gpr_index) { 160 switch (gpr_index) { 161 case 0: return (4 * kBytesPerGprSpillLocation); 162 case 1: return (1 * kBytesPerGprSpillLocation); 163 case 2: return (0 * kBytesPerGprSpillLocation); 164 case 3: return (5 * kBytesPerGprSpillLocation); 165 case 4: return (6 * kBytesPerGprSpillLocation); 166 default: 167 LOG(FATAL) << "Unexpected GPR index: " << gpr_index; 168 return 0; 169 } 170 } 171#else 172#error "Unsupported architecture" 173#endif 174 175 public: 176 static mirror::ArtMethod* GetCallingMethod(mirror::ArtMethod** sp) 177 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 178 DCHECK((*sp)->IsCalleeSaveMethod()); 179 byte* previous_sp = reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize; 180 return *reinterpret_cast<mirror::ArtMethod**>(previous_sp); 181 } 182 183 // For the given quick ref and args quick frame, return the caller's PC. 184 static uintptr_t GetCallingPc(mirror::ArtMethod** sp) 185 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 186 DCHECK((*sp)->IsCalleeSaveMethod()); 187 byte* lr = reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset; 188 return *reinterpret_cast<uintptr_t*>(lr); 189 } 190 191 QuickArgumentVisitor(mirror::ArtMethod** sp, bool is_static, 192 const char* shorty, uint32_t shorty_len) 193 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) : 194 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len), 195 gpr_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset), 196 fpr_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset), 197 stack_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize 198 + StackArgumentStartFromShorty(is_static, shorty, shorty_len)), 199 gpr_index_(0), fpr_index_(0), stack_index_(0), cur_type_(Primitive::kPrimVoid), 200 is_split_long_or_double_(false) { 201 DCHECK_EQ(kQuickCalleeSaveFrame_RefAndArgs_FrameSize, 202 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes()); 203 } 204 205 virtual ~QuickArgumentVisitor() {} 206 207 virtual void Visit() = 0; 208 209 Primitive::Type GetParamPrimitiveType() const { 210 return cur_type_; 211 } 212 213 byte* GetParamAddress() const { 214 if (!kQuickSoftFloatAbi) { 215 Primitive::Type type = GetParamPrimitiveType(); 216 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) { 217 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) { 218 return fpr_args_ + (fpr_index_ * kBytesPerFprSpillLocation); 219 } 220 } 221 } 222 if (gpr_index_ < kNumQuickGprArgs) { 223 return gpr_args_ + GprIndexToGprOffset(gpr_index_); 224 } 225 return stack_args_ + (stack_index_ * kBytesStackArgLocation); 226 } 227 228 bool IsSplitLongOrDouble() const { 229 if ((kBytesPerGprSpillLocation == 4) || (kBytesPerFprSpillLocation == 4)) { 230 return is_split_long_or_double_; 231 } else { 232 return false; // An optimization for when GPR and FPRs are 64bit. 233 } 234 } 235 236 bool IsParamAReference() const { 237 return GetParamPrimitiveType() == Primitive::kPrimNot; 238 } 239 240 bool IsParamALongOrDouble() const { 241 Primitive::Type type = GetParamPrimitiveType(); 242 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble; 243 } 244 245 uint64_t ReadSplitLongParam() const { 246 DCHECK(IsSplitLongOrDouble()); 247 uint64_t low_half = *reinterpret_cast<uint32_t*>(GetParamAddress()); 248 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args_); 249 return (low_half & 0xffffffffULL) | (high_half << 32); 250 } 251 252 void VisitArguments() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 253 gpr_index_ = 0; 254 fpr_index_ = 0; 255 stack_index_ = 0; 256 if (!is_static_) { // Handle this. 257 cur_type_ = Primitive::kPrimNot; 258 is_split_long_or_double_ = false; 259 Visit(); 260 if (kNumQuickGprArgs > 0) { 261 gpr_index_++; 262 } else { 263 stack_index_++; 264 } 265 } 266 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) { 267 cur_type_ = Primitive::GetType(shorty_[shorty_index]); 268 switch (cur_type_) { 269 case Primitive::kPrimNot: 270 case Primitive::kPrimBoolean: 271 case Primitive::kPrimByte: 272 case Primitive::kPrimChar: 273 case Primitive::kPrimShort: 274 case Primitive::kPrimInt: 275 is_split_long_or_double_ = false; 276 Visit(); 277 if (gpr_index_ < kNumQuickGprArgs) { 278 gpr_index_++; 279 } else { 280 stack_index_++; 281 } 282 break; 283 case Primitive::kPrimFloat: 284 is_split_long_or_double_ = false; 285 Visit(); 286 if (kQuickSoftFloatAbi) { 287 if (gpr_index_ < kNumQuickGprArgs) { 288 gpr_index_++; 289 } else { 290 stack_index_++; 291 } 292 } else { 293 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) { 294 fpr_index_++; 295 } else { 296 stack_index_++; 297 } 298 } 299 break; 300 case Primitive::kPrimDouble: 301 case Primitive::kPrimLong: 302 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) { 303 is_split_long_or_double_ = (kBytesPerGprSpillLocation == 4) && 304 ((gpr_index_ + 1) == kNumQuickGprArgs); 305 Visit(); 306 if (gpr_index_ < kNumQuickGprArgs) { 307 gpr_index_++; 308 if (kBytesPerGprSpillLocation == 4) { 309 if (gpr_index_ < kNumQuickGprArgs) { 310 gpr_index_++; 311 } else { 312 stack_index_++; 313 } 314 } 315 } else { 316 if (kBytesStackArgLocation == 4) { 317 stack_index_+= 2; 318 } else { 319 CHECK_EQ(kBytesStackArgLocation, 8U); 320 stack_index_++; 321 } 322 } 323 } else { 324 is_split_long_or_double_ = (kBytesPerFprSpillLocation == 4) && 325 ((fpr_index_ + 1) == kNumQuickFprArgs); 326 Visit(); 327 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) { 328 fpr_index_++; 329 if (kBytesPerFprSpillLocation == 4) { 330 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) { 331 fpr_index_++; 332 } else { 333 stack_index_++; 334 } 335 } 336 } else { 337 if (kBytesStackArgLocation == 4) { 338 stack_index_+= 2; 339 } else { 340 CHECK_EQ(kBytesStackArgLocation, 8U); 341 stack_index_++; 342 } 343 } 344 } 345 break; 346 default: 347 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_; 348 } 349 } 350 } 351 352 private: 353 static size_t StackArgumentStartFromShorty(bool is_static, const char* shorty, 354 uint32_t shorty_len) { 355 if (kQuickSoftFloatAbi) { 356 CHECK_EQ(kNumQuickFprArgs, 0U); 357 return (kNumQuickGprArgs * kBytesPerGprSpillLocation) + kBytesPerGprSpillLocation /* ArtMethod* */; 358 } else { 359 size_t offset = kBytesPerGprSpillLocation; // Skip Method*. 360 size_t gprs_seen = 0; 361 size_t fprs_seen = 0; 362 if (!is_static && (gprs_seen < kNumQuickGprArgs)) { 363 gprs_seen++; 364 offset += kBytesStackArgLocation; 365 } 366 for (uint32_t i = 1; i < shorty_len; ++i) { 367 switch (shorty[i]) { 368 case 'Z': 369 case 'B': 370 case 'C': 371 case 'S': 372 case 'I': 373 case 'L': 374 if (gprs_seen < kNumQuickGprArgs) { 375 gprs_seen++; 376 offset += kBytesStackArgLocation; 377 } 378 break; 379 case 'J': 380 if (gprs_seen < kNumQuickGprArgs) { 381 gprs_seen++; 382 offset += 2 * kBytesStackArgLocation; 383 if (kBytesPerGprSpillLocation == 4) { 384 if (gprs_seen < kNumQuickGprArgs) { 385 gprs_seen++; 386 } 387 } 388 } 389 break; 390 case 'F': 391 if ((kNumQuickFprArgs != 0) && (fprs_seen + 1 < kNumQuickFprArgs + 1)) { 392 fprs_seen++; 393 offset += kBytesStackArgLocation; 394 } 395 break; 396 case 'D': 397 if ((kNumQuickFprArgs != 0) && (fprs_seen + 1 < kNumQuickFprArgs + 1)) { 398 fprs_seen++; 399 offset += 2 * kBytesStackArgLocation; 400 if (kBytesPerFprSpillLocation == 4) { 401 if ((kNumQuickFprArgs != 0) && (fprs_seen + 1 < kNumQuickFprArgs + 1)) { 402 fprs_seen++; 403 } 404 } 405 } 406 break; 407 default: 408 LOG(FATAL) << "Unexpected shorty character: " << shorty[i] << " in " << shorty; 409 } 410 } 411 return offset; 412 } 413 } 414 415 const bool is_static_; 416 const char* const shorty_; 417 const uint32_t shorty_len_; 418 byte* const gpr_args_; // Address of GPR arguments in callee save frame. 419 byte* const fpr_args_; // Address of FPR arguments in callee save frame. 420 byte* const stack_args_; // Address of stack arguments in caller's frame. 421 uint32_t gpr_index_; // Index into spilled GPRs. 422 uint32_t fpr_index_; // Index into spilled FPRs. 423 uint32_t stack_index_; // Index into arguments on the stack. 424 // The current type of argument during VisitArguments. 425 Primitive::Type cur_type_; 426 // Does a 64bit parameter straddle the register and stack arguments? 427 bool is_split_long_or_double_; 428}; 429 430// Visits arguments on the stack placing them into the shadow frame. 431class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor { 432 public: 433 BuildQuickShadowFrameVisitor(mirror::ArtMethod** sp, bool is_static, const char* shorty, 434 uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) : 435 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {} 436 437 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE { 438 Primitive::Type type = GetParamPrimitiveType(); 439 switch (type) { 440 case Primitive::kPrimLong: // Fall-through. 441 case Primitive::kPrimDouble: 442 if (IsSplitLongOrDouble()) { 443 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam()); 444 } else { 445 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress())); 446 } 447 ++cur_reg_; 448 break; 449 case Primitive::kPrimNot: { 450 StackReference<mirror::Object>* stack_ref = 451 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress()); 452 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr()); 453 } 454 break; 455 case Primitive::kPrimBoolean: // Fall-through. 456 case Primitive::kPrimByte: // Fall-through. 457 case Primitive::kPrimChar: // Fall-through. 458 case Primitive::kPrimShort: // Fall-through. 459 case Primitive::kPrimInt: // Fall-through. 460 case Primitive::kPrimFloat: 461 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress())); 462 break; 463 case Primitive::kPrimVoid: 464 LOG(FATAL) << "UNREACHABLE"; 465 break; 466 } 467 ++cur_reg_; 468 } 469 470 private: 471 ShadowFrame* const sf_; 472 uint32_t cur_reg_; 473 474 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor); 475}; 476 477extern "C" uint64_t artQuickToInterpreterBridge(mirror::ArtMethod* method, Thread* self, 478 mirror::ArtMethod** sp) 479 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 480 // Ensure we don't get thread suspension until the object arguments are safely in the shadow 481 // frame. 482 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs); 483 484 if (method->IsAbstract()) { 485 ThrowAbstractMethodError(method); 486 return 0; 487 } else { 488 DCHECK(!method->IsNative()) << PrettyMethod(method); 489 const char* old_cause = self->StartAssertNoThreadSuspension("Building interpreter shadow frame"); 490 MethodHelper mh(method); 491 const DexFile::CodeItem* code_item = mh.GetCodeItem(); 492 DCHECK(code_item != nullptr) << PrettyMethod(method); 493 uint16_t num_regs = code_item->registers_size_; 494 void* memory = alloca(ShadowFrame::ComputeSize(num_regs)); 495 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, NULL, // No last shadow coming from quick. 496 method, 0, memory)); 497 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_; 498 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, mh.IsStatic(), mh.GetShorty(), 499 mh.GetShortyLength(), 500 shadow_frame, first_arg_reg); 501 shadow_frame_builder.VisitArguments(); 502 // Push a transition back into managed code onto the linked list in thread. 503 ManagedStack fragment; 504 self->PushManagedStackFragment(&fragment); 505 self->PushShadowFrame(shadow_frame); 506 self->EndAssertNoThreadSuspension(old_cause); 507 508 if (method->IsStatic() && !method->GetDeclaringClass()->IsInitializing()) { 509 // Ensure static method's class is initialized. 510 SirtRef<mirror::Class> sirt_c(self, method->GetDeclaringClass()); 511 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(sirt_c, true, true)) { 512 DCHECK(Thread::Current()->IsExceptionPending()) << PrettyMethod(method); 513 self->PopManagedStackFragment(fragment); 514 return 0; 515 } 516 } 517 518 JValue result = interpreter::EnterInterpreterFromStub(self, mh, code_item, *shadow_frame); 519 // Pop transition. 520 self->PopManagedStackFragment(fragment); 521 // No need to restore the args since the method has already been run by the interpreter. 522 return result.GetJ(); 523 } 524} 525 526// Visits arguments on the stack placing them into the args vector, Object* arguments are converted 527// to jobjects. 528class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor { 529 public: 530 BuildQuickArgumentVisitor(mirror::ArtMethod** sp, bool is_static, const char* shorty, 531 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa, 532 std::vector<jvalue>* args) : 533 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {} 534 535 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE { 536 jvalue val; 537 Primitive::Type type = GetParamPrimitiveType(); 538 switch (type) { 539 case Primitive::kPrimNot: { 540 StackReference<mirror::Object>* stack_ref = 541 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress()); 542 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr()); 543 references_.push_back(std::make_pair(val.l, stack_ref)); 544 break; 545 } 546 case Primitive::kPrimLong: // Fall-through. 547 case Primitive::kPrimDouble: 548 if (IsSplitLongOrDouble()) { 549 val.j = ReadSplitLongParam(); 550 } else { 551 val.j = *reinterpret_cast<jlong*>(GetParamAddress()); 552 } 553 break; 554 case Primitive::kPrimBoolean: // Fall-through. 555 case Primitive::kPrimByte: // Fall-through. 556 case Primitive::kPrimChar: // Fall-through. 557 case Primitive::kPrimShort: // Fall-through. 558 case Primitive::kPrimInt: // Fall-through. 559 case Primitive::kPrimFloat: 560 val.i = *reinterpret_cast<jint*>(GetParamAddress()); 561 break; 562 case Primitive::kPrimVoid: 563 LOG(FATAL) << "UNREACHABLE"; 564 val.j = 0; 565 break; 566 } 567 args_->push_back(val); 568 } 569 570 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 571 // Fixup any references which may have changed. 572 for (const auto& pair : references_) { 573 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first)); 574 } 575 } 576 577 private: 578 ScopedObjectAccessUnchecked* soa_; 579 std::vector<jvalue>* args_; 580 // References which we must update when exiting in case the GC moved the objects. 581 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_; 582 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor); 583}; 584 585// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method 586// which is responsible for recording callee save registers. We explicitly place into jobjects the 587// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a 588// field within the proxy object, which will box the primitive arguments and deal with error cases. 589extern "C" uint64_t artQuickProxyInvokeHandler(mirror::ArtMethod* proxy_method, 590 mirror::Object* receiver, 591 Thread* self, mirror::ArtMethod** sp) 592 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 593 DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method); 594 DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method); 595 // Ensure we don't get thread suspension until the object arguments are safely in jobjects. 596 const char* old_cause = 597 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments"); 598 // Register the top of the managed stack, making stack crawlable. 599 DCHECK_EQ(*sp, proxy_method) << PrettyMethod(proxy_method); 600 self->SetTopOfStack(sp, 0); 601 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(), 602 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes()) 603 << PrettyMethod(proxy_method); 604 self->VerifyStack(); 605 // Start new JNI local reference state. 606 JNIEnvExt* env = self->GetJniEnv(); 607 ScopedObjectAccessUnchecked soa(env); 608 ScopedJniEnvLocalRefState env_state(env); 609 // Create local ref. copies of proxy method and the receiver. 610 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver); 611 612 // Placing arguments into args vector and remove the receiver. 613 MethodHelper proxy_mh(proxy_method); 614 DCHECK(!proxy_mh.IsStatic()) << PrettyMethod(proxy_method); 615 std::vector<jvalue> args; 616 BuildQuickArgumentVisitor local_ref_visitor(sp, proxy_mh.IsStatic(), proxy_mh.GetShorty(), 617 proxy_mh.GetShortyLength(), &soa, &args); 618 619 local_ref_visitor.VisitArguments(); 620 DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method); 621 args.erase(args.begin()); 622 623 // Convert proxy method into expected interface method. 624 mirror::ArtMethod* interface_method = proxy_method->FindOverriddenMethod(); 625 DCHECK(interface_method != NULL) << PrettyMethod(proxy_method); 626 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method); 627 jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_method); 628 629 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code 630 // that performs allocations. 631 self->EndAssertNoThreadSuspension(old_cause); 632 JValue result = InvokeProxyInvocationHandler(soa, proxy_mh.GetShorty(), 633 rcvr_jobj, interface_method_jobj, args); 634 // Restore references which might have moved. 635 local_ref_visitor.FixupReferences(); 636 return result.GetJ(); 637} 638 639// Read object references held in arguments from quick frames and place in a JNI local references, 640// so they don't get garbage collected. 641class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor { 642 public: 643 RememberForGcArgumentVisitor(mirror::ArtMethod** sp, bool is_static, const char* shorty, 644 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) : 645 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {} 646 647 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE { 648 if (IsParamAReference()) { 649 StackReference<mirror::Object>* stack_ref = 650 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress()); 651 jobject reference = 652 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr()); 653 references_.push_back(std::make_pair(reference, stack_ref)); 654 } 655 } 656 657 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 658 // Fixup any references which may have changed. 659 for (const auto& pair : references_) { 660 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first)); 661 } 662 } 663 664 private: 665 ScopedObjectAccessUnchecked* soa_; 666 // References which we must update when exiting in case the GC moved the objects. 667 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_; 668 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor); 669}; 670 671// Lazily resolve a method for quick. Called by stub code. 672extern "C" const void* artQuickResolutionTrampoline(mirror::ArtMethod* called, 673 mirror::Object* receiver, 674 Thread* self, mirror::ArtMethod** sp) 675 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 676 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs); 677 // Start new JNI local reference state 678 JNIEnvExt* env = self->GetJniEnv(); 679 ScopedObjectAccessUnchecked soa(env); 680 ScopedJniEnvLocalRefState env_state(env); 681 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up"); 682 683 // Compute details about the called method (avoid GCs) 684 ClassLinker* linker = Runtime::Current()->GetClassLinker(); 685 mirror::ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp); 686 InvokeType invoke_type; 687 const DexFile* dex_file; 688 uint32_t dex_method_idx; 689 if (called->IsRuntimeMethod()) { 690 uint32_t dex_pc = caller->ToDexPc(QuickArgumentVisitor::GetCallingPc(sp)); 691 const DexFile::CodeItem* code; 692 { 693 MethodHelper mh(caller); 694 dex_file = &mh.GetDexFile(); 695 code = mh.GetCodeItem(); 696 } 697 CHECK_LT(dex_pc, code->insns_size_in_code_units_); 698 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]); 699 Instruction::Code instr_code = instr->Opcode(); 700 bool is_range; 701 switch (instr_code) { 702 case Instruction::INVOKE_DIRECT: 703 invoke_type = kDirect; 704 is_range = false; 705 break; 706 case Instruction::INVOKE_DIRECT_RANGE: 707 invoke_type = kDirect; 708 is_range = true; 709 break; 710 case Instruction::INVOKE_STATIC: 711 invoke_type = kStatic; 712 is_range = false; 713 break; 714 case Instruction::INVOKE_STATIC_RANGE: 715 invoke_type = kStatic; 716 is_range = true; 717 break; 718 case Instruction::INVOKE_SUPER: 719 invoke_type = kSuper; 720 is_range = false; 721 break; 722 case Instruction::INVOKE_SUPER_RANGE: 723 invoke_type = kSuper; 724 is_range = true; 725 break; 726 case Instruction::INVOKE_VIRTUAL: 727 invoke_type = kVirtual; 728 is_range = false; 729 break; 730 case Instruction::INVOKE_VIRTUAL_RANGE: 731 invoke_type = kVirtual; 732 is_range = true; 733 break; 734 case Instruction::INVOKE_INTERFACE: 735 invoke_type = kInterface; 736 is_range = false; 737 break; 738 case Instruction::INVOKE_INTERFACE_RANGE: 739 invoke_type = kInterface; 740 is_range = true; 741 break; 742 default: 743 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(NULL); 744 // Avoid used uninitialized warnings. 745 invoke_type = kDirect; 746 is_range = false; 747 } 748 dex_method_idx = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c(); 749 750 } else { 751 invoke_type = kStatic; 752 dex_file = &MethodHelper(called).GetDexFile(); 753 dex_method_idx = called->GetDexMethodIndex(); 754 } 755 uint32_t shorty_len; 756 const char* shorty = 757 dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx), &shorty_len); 758 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa); 759 visitor.VisitArguments(); 760 self->EndAssertNoThreadSuspension(old_cause); 761 bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface; 762 // Resolve method filling in dex cache. 763 if (called->IsRuntimeMethod()) { 764 SirtRef<mirror::Object> sirt_receiver(soa.Self(), virtual_or_interface ? receiver : nullptr); 765 called = linker->ResolveMethod(dex_method_idx, caller, invoke_type); 766 receiver = sirt_receiver.get(); 767 } 768 const void* code = NULL; 769 if (LIKELY(!self->IsExceptionPending())) { 770 // Incompatible class change should have been handled in resolve method. 771 CHECK(!called->CheckIncompatibleClassChange(invoke_type)) 772 << PrettyMethod(called) << " " << invoke_type; 773 if (virtual_or_interface) { 774 // Refine called method based on receiver. 775 CHECK(receiver != nullptr) << invoke_type; 776 if (invoke_type == kVirtual) { 777 called = receiver->GetClass()->FindVirtualMethodForVirtual(called); 778 } else { 779 called = receiver->GetClass()->FindVirtualMethodForInterface(called); 780 } 781 // We came here because of sharpening. Ensure the dex cache is up-to-date on the method index 782 // of the sharpened method. 783 if (called->GetDexCacheResolvedMethods() == caller->GetDexCacheResolvedMethods()) { 784 caller->GetDexCacheResolvedMethods()->Set<false>(called->GetDexMethodIndex(), called); 785 } else { 786 // Calling from one dex file to another, need to compute the method index appropriate to 787 // the caller's dex file. Since we get here only if the original called was a runtime 788 // method, we've got the correct dex_file and a dex_method_idx from above. 789 DCHECK(&MethodHelper(caller).GetDexFile() == dex_file); 790 uint32_t method_index = 791 MethodHelper(called).FindDexMethodIndexInOtherDexFile(*dex_file, dex_method_idx); 792 if (method_index != DexFile::kDexNoIndex) { 793 caller->GetDexCacheResolvedMethods()->Set<false>(method_index, called); 794 } 795 } 796 } 797 // Ensure that the called method's class is initialized. 798 SirtRef<mirror::Class> called_class(soa.Self(), called->GetDeclaringClass()); 799 linker->EnsureInitialized(called_class, true, true); 800 if (LIKELY(called_class->IsInitialized())) { 801 code = called->GetEntryPointFromQuickCompiledCode(); 802 } else if (called_class->IsInitializing()) { 803 if (invoke_type == kStatic) { 804 // Class is still initializing, go to oat and grab code (trampoline must be left in place 805 // until class is initialized to stop races between threads). 806 code = linker->GetQuickOatCodeFor(called); 807 } else { 808 // No trampoline for non-static methods. 809 code = called->GetEntryPointFromQuickCompiledCode(); 810 } 811 } else { 812 DCHECK(called_class->IsErroneous()); 813 } 814 } 815 CHECK_EQ(code == NULL, self->IsExceptionPending()); 816 // Fixup any locally saved objects may have moved during a GC. 817 visitor.FixupReferences(); 818 // Place called method in callee-save frame to be placed as first argument to quick method. 819 *sp = called; 820 return code; 821} 822 823 824 825/* 826 * This class uses a couple of observations to unite the different calling conventions through 827 * a few constants. 828 * 829 * 1) Number of registers used for passing is normally even, so counting down has no penalty for 830 * possible alignment. 831 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point 832 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote 833 * when we have to split things 834 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats 835 * and we can use Int handling directly. 836 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code 837 * necessary when widening. Also, widening of Ints will take place implicitly, and the 838 * extension should be compatible with Aarch64, which mandates copying the available bits 839 * into LSB and leaving the rest unspecified. 840 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on 841 * the stack. 842 * 6) There is only little endian. 843 * 844 * 845 * Actual work is supposed to be done in a delegate of the template type. The interface is as 846 * follows: 847 * 848 * void PushGpr(uintptr_t): Add a value for the next GPR 849 * 850 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need 851 * padding, that is, think the architecture is 32b and aligns 64b. 852 * 853 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to 854 * split this if necessary. The current state will have aligned, if 855 * necessary. 856 * 857 * void PushStack(uintptr_t): Push a value to the stack. 858 * 859 * uintptr_t PushSirt(mirror::Object* ref): Add a reference to the Sirt. This _will_ have nullptr, 860 * as this might be important for null initialization. 861 * Must return the jobject, that is, the reference to the 862 * entry in the Sirt (nullptr if necessary). 863 * 864 */ 865template <class T> class BuildGenericJniFrameStateMachine { 866 public: 867#if defined(__arm__) 868 // TODO: These are all dummy values! 869 static constexpr bool kNativeSoftFloatAbi = true; 870 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3 871 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs. 872 873 static constexpr size_t kRegistersNeededForLong = 2; 874 static constexpr size_t kRegistersNeededForDouble = 2; 875 static constexpr bool kMultiRegistersAligned = true; 876 static constexpr bool kMultiRegistersWidened = false; 877 static constexpr bool kAlignLongOnStack = true; 878 static constexpr bool kAlignDoubleOnStack = true; 879#elif defined(__mips__) 880 // TODO: These are all dummy values! 881 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI. 882 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs. 883 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs. 884 885 static constexpr size_t kRegistersNeededForLong = 2; 886 static constexpr size_t kRegistersNeededForDouble = 2; 887 static constexpr bool kMultiRegistersAligned = true; 888 static constexpr bool kMultiRegistersWidened = true; 889 static constexpr bool kAlignLongOnStack = false; 890 static constexpr bool kAlignDoubleOnStack = false; 891#elif defined(__i386__) 892 // TODO: Check these! 893 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp 894 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs. 895 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs. 896 897 static constexpr size_t kRegistersNeededForLong = 2; 898 static constexpr size_t kRegistersNeededForDouble = 2; 899 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways 900 static constexpr bool kMultiRegistersWidened = false; 901 static constexpr bool kAlignLongOnStack = false; 902 static constexpr bool kAlignDoubleOnStack = false; 903#elif defined(__x86_64__) 904 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI. 905 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs. 906 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs. 907 908 static constexpr size_t kRegistersNeededForLong = 1; 909 static constexpr size_t kRegistersNeededForDouble = 1; 910 static constexpr bool kMultiRegistersAligned = false; 911 static constexpr bool kMultiRegistersWidened = false; 912 static constexpr bool kAlignLongOnStack = false; 913 static constexpr bool kAlignDoubleOnStack = false; 914#else 915#error "Unsupported architecture" 916#endif 917 918 public: 919 explicit BuildGenericJniFrameStateMachine(T* delegate) : gpr_index_(kNumNativeGprArgs), 920 fpr_index_(kNumNativeFprArgs), 921 stack_entries_(0), 922 delegate_(delegate) { 923 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff 924 // the next register is even; counting down is just to make the compiler happy... 925 CHECK_EQ(kNumNativeGprArgs % 2, 0U); 926 CHECK_EQ(kNumNativeFprArgs % 2, 0U); 927 } 928 929 virtual ~BuildGenericJniFrameStateMachine() {} 930 931 bool HavePointerGpr() { 932 return gpr_index_ > 0; 933 } 934 935 void AdvancePointer(void* val) { 936 if (HavePointerGpr()) { 937 gpr_index_--; 938 PushGpr(reinterpret_cast<uintptr_t>(val)); 939 } else { 940 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b 941 PushStack(reinterpret_cast<uintptr_t>(val)); 942 gpr_index_ = 0; 943 } 944 } 945 946 947 bool HaveSirtGpr() { 948 return gpr_index_ > 0; 949 } 950 951 void AdvanceSirt(mirror::Object* ptr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 952 uintptr_t sirtRef = PushSirt(ptr); 953 if (HaveSirtGpr()) { 954 gpr_index_--; 955 PushGpr(sirtRef); 956 } else { 957 stack_entries_++; 958 PushStack(sirtRef); 959 gpr_index_ = 0; 960 } 961 } 962 963 964 bool HaveIntGpr() { 965 return gpr_index_ > 0; 966 } 967 968 void AdvanceInt(uint32_t val) { 969 if (HaveIntGpr()) { 970 gpr_index_--; 971 PushGpr(val); 972 } else { 973 stack_entries_++; 974 PushStack(val); 975 gpr_index_ = 0; 976 } 977 } 978 979 980 bool HaveLongGpr() { 981 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0); 982 } 983 984 bool LongGprNeedsPadding() { 985 return kRegistersNeededForLong > 1 && // only pad when using multiple registers 986 kAlignLongOnStack && // and when it needs alignment 987 (gpr_index_ & 1) == 1; // counter is odd, see constructor 988 } 989 990 bool LongStackNeedsPadding() { 991 return kRegistersNeededForLong > 1 && // only pad when using multiple registers 992 kAlignLongOnStack && // and when it needs 8B alignment 993 (stack_entries_ & 1) == 1; // counter is odd 994 } 995 996 void AdvanceLong(uint64_t val) { 997 if (HaveLongGpr()) { 998 if (LongGprNeedsPadding()) { 999 PushGpr(0); 1000 gpr_index_--; 1001 } 1002 if (kRegistersNeededForLong == 1) { 1003 PushGpr(static_cast<uintptr_t>(val)); 1004 } else { 1005 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF)); 1006 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF)); 1007 } 1008 gpr_index_ -= kRegistersNeededForLong; 1009 } else { 1010 if (LongStackNeedsPadding()) { 1011 PushStack(0); 1012 stack_entries_++; 1013 } 1014 if (kRegistersNeededForLong == 1) { 1015 PushStack(static_cast<uintptr_t>(val)); 1016 stack_entries_++; 1017 } else { 1018 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF)); 1019 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF)); 1020 stack_entries_ += 2; 1021 } 1022 gpr_index_ = 0; 1023 } 1024 } 1025 1026 1027 bool HaveFloatFpr() { 1028 return fpr_index_ > 0; 1029 } 1030 1031 // TODO: please review this bit representation retrieving. 1032 template <typename U, typename V> V convert(U in) { 1033 CHECK_LE(sizeof(U), sizeof(V)); 1034 union { U u; V v; } tmp; 1035 tmp.u = in; 1036 return tmp.v; 1037 } 1038 1039 void AdvanceFloat(float val) { 1040 if (kNativeSoftFloatAbi) { 1041 AdvanceInt(convert<float, uint32_t>(val)); 1042 } else { 1043 if (HaveFloatFpr()) { 1044 fpr_index_--; 1045 if (kRegistersNeededForDouble == 1) { 1046 if (kMultiRegistersWidened) { 1047 PushFpr8(convert<double, uint64_t>(val)); 1048 } else { 1049 // No widening, just use the bits. 1050 PushFpr8(convert<float, uint64_t>(val)); 1051 } 1052 } else { 1053 PushFpr4(val); 1054 } 1055 } else { 1056 stack_entries_++; 1057 if (kRegistersNeededForDouble == 1 && kMultiRegistersWidened) { 1058 // Need to widen before storing: Note the "double" in the template instantiation. 1059 PushStack(convert<double, uintptr_t>(val)); 1060 } else { 1061 PushStack(convert<float, uintptr_t>(val)); 1062 } 1063 fpr_index_ = 0; 1064 } 1065 } 1066 } 1067 1068 1069 bool HaveDoubleFpr() { 1070 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0); 1071 } 1072 1073 bool DoubleFprNeedsPadding() { 1074 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers 1075 kAlignDoubleOnStack && // and when it needs alignment 1076 (fpr_index_ & 1) == 1; // counter is odd, see constructor 1077 } 1078 1079 bool DoubleStackNeedsPadding() { 1080 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers 1081 kAlignDoubleOnStack && // and when it needs 8B alignment 1082 (stack_entries_ & 1) == 1; // counter is odd 1083 } 1084 1085 void AdvanceDouble(uint64_t val) { 1086 if (kNativeSoftFloatAbi) { 1087 AdvanceLong(val); 1088 } else { 1089 if (HaveDoubleFpr()) { 1090 if (DoubleFprNeedsPadding()) { 1091 PushFpr4(0); 1092 fpr_index_--; 1093 } 1094 PushFpr8(val); 1095 fpr_index_ -= kRegistersNeededForDouble; 1096 } else { 1097 if (DoubleStackNeedsPadding()) { 1098 PushStack(0); 1099 stack_entries_++; 1100 } 1101 if (kRegistersNeededForDouble == 1) { 1102 PushStack(static_cast<uintptr_t>(val)); 1103 stack_entries_++; 1104 } else { 1105 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF)); 1106 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF)); 1107 stack_entries_ += 2; 1108 } 1109 fpr_index_ = 0; 1110 } 1111 } 1112 } 1113 1114 uint32_t getStackEntries() { 1115 return stack_entries_; 1116 } 1117 1118 uint32_t getNumberOfUsedGprs() { 1119 return kNumNativeGprArgs - gpr_index_; 1120 } 1121 1122 uint32_t getNumberOfUsedFprs() { 1123 return kNumNativeFprArgs - fpr_index_; 1124 } 1125 1126 private: 1127 void PushGpr(uintptr_t val) { 1128 delegate_->PushGpr(val); 1129 } 1130 void PushFpr4(float val) { 1131 delegate_->PushFpr4(val); 1132 } 1133 void PushFpr8(uint64_t val) { 1134 delegate_->PushFpr8(val); 1135 } 1136 void PushStack(uintptr_t val) { 1137 delegate_->PushStack(val); 1138 } 1139 uintptr_t PushSirt(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1140 return delegate_->PushSirt(ref); 1141 } 1142 1143 uint32_t gpr_index_; // Number of free GPRs 1144 uint32_t fpr_index_; // Number of free FPRs 1145 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not 1146 // extended 1147 T* delegate_; // What Push implementation gets called 1148}; 1149 1150class ComputeGenericJniFrameSize FINAL { 1151 public: 1152 ComputeGenericJniFrameSize() : num_sirt_references_(0), num_stack_entries_(0) {} 1153 1154 uint32_t GetStackSize() { 1155 return num_stack_entries_ * sizeof(uintptr_t); 1156 } 1157 1158 // WARNING: After this, *sp won't be pointing to the method anymore! 1159 void ComputeLayout(mirror::ArtMethod*** m, bool is_static, const char* shorty, uint32_t shorty_len, 1160 void* sp, StackIndirectReferenceTable** table, uint32_t* sirt_entries, 1161 uintptr_t** start_stack, uintptr_t** start_gpr, uint32_t** start_fpr, 1162 void** code_return, size_t* overall_size) 1163 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1164 ComputeAll(is_static, shorty, shorty_len); 1165 1166 mirror::ArtMethod* method = **m; 1167 1168 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp); 1169 1170 // First, fix up the layout of the callee-save frame. 1171 // We have to squeeze in the Sirt, and relocate the method pointer. 1172 1173 // "Free" the slot for the method. 1174 sp8 += kPointerSize; 1175 1176 // Add the Sirt. 1177 *sirt_entries = num_sirt_references_; 1178 size_t sirt_size = StackIndirectReferenceTable::GetAlignedSirtSize(num_sirt_references_); 1179 sp8 -= sirt_size; 1180 *table = reinterpret_cast<StackIndirectReferenceTable*>(sp8); 1181 (*table)->SetNumberOfReferences(num_sirt_references_); 1182 1183 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us. 1184 sp8 -= kPointerSize; 1185 uint8_t* method_pointer = sp8; 1186 *(reinterpret_cast<mirror::ArtMethod**>(method_pointer)) = method; 1187 *m = reinterpret_cast<mirror::ArtMethod**>(method_pointer); 1188 1189 // Reference cookie and padding 1190 sp8 -= 8; 1191 // Store Sirt size 1192 *reinterpret_cast<uint32_t*>(sp8) = static_cast<uint32_t>(sirt_size & 0xFFFFFFFF); 1193 1194 // Next comes the native call stack. 1195 sp8 -= GetStackSize(); 1196 // Now align the call stack below. This aligns by 16, as AArch64 seems to require. 1197 uintptr_t mask = ~0x0F; 1198 sp8 = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(sp8) & mask); 1199 *start_stack = reinterpret_cast<uintptr_t*>(sp8); 1200 1201 // put fprs and gprs below 1202 // Assumption is OK right now, as we have soft-float arm 1203 size_t fregs = BuildGenericJniFrameStateMachine<ComputeGenericJniFrameSize>::kNumNativeFprArgs; 1204 sp8 -= fregs * sizeof(uintptr_t); 1205 *start_fpr = reinterpret_cast<uint32_t*>(sp8); 1206 size_t iregs = BuildGenericJniFrameStateMachine<ComputeGenericJniFrameSize>::kNumNativeGprArgs; 1207 sp8 -= iregs * sizeof(uintptr_t); 1208 *start_gpr = reinterpret_cast<uintptr_t*>(sp8); 1209 1210 // reserve space for the code pointer 1211 sp8 -= kPointerSize; 1212 *code_return = reinterpret_cast<void*>(sp8); 1213 1214 *overall_size = reinterpret_cast<uint8_t*>(sp) - sp8; 1215 1216 // The new SP is stored at the end of the alloca, so it can be immediately popped 1217 sp8 = reinterpret_cast<uint8_t*>(sp) - 5 * KB; 1218 *(reinterpret_cast<uint8_t**>(sp8)) = method_pointer; 1219 } 1220 1221 void ComputeSirtOffset() { } // nothing to do, static right now 1222 1223 void ComputeAll(bool is_static, const char* shorty, uint32_t shorty_len) 1224 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1225 BuildGenericJniFrameStateMachine<ComputeGenericJniFrameSize> sm(this); 1226 1227 // JNIEnv 1228 sm.AdvancePointer(nullptr); 1229 1230 // Class object or this as first argument 1231 sm.AdvanceSirt(reinterpret_cast<mirror::Object*>(0x12345678)); 1232 1233 for (uint32_t i = 1; i < shorty_len; ++i) { 1234 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]); 1235 switch (cur_type_) { 1236 case Primitive::kPrimNot: 1237 sm.AdvanceSirt(reinterpret_cast<mirror::Object*>(0x12345678)); 1238 break; 1239 1240 case Primitive::kPrimBoolean: 1241 case Primitive::kPrimByte: 1242 case Primitive::kPrimChar: 1243 case Primitive::kPrimShort: 1244 case Primitive::kPrimInt: 1245 sm.AdvanceInt(0); 1246 break; 1247 case Primitive::kPrimFloat: 1248 sm.AdvanceFloat(0); 1249 break; 1250 case Primitive::kPrimDouble: 1251 sm.AdvanceDouble(0); 1252 break; 1253 case Primitive::kPrimLong: 1254 sm.AdvanceLong(0); 1255 break; 1256 default: 1257 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty; 1258 } 1259 } 1260 1261 num_stack_entries_ = sm.getStackEntries(); 1262 } 1263 1264 void PushGpr(uintptr_t /* val */) { 1265 // not optimizing registers, yet 1266 } 1267 1268 void PushFpr4(float /* val */) { 1269 // not optimizing registers, yet 1270 } 1271 1272 void PushFpr8(uint64_t /* val */) { 1273 // not optimizing registers, yet 1274 } 1275 1276 void PushStack(uintptr_t /* val */) { 1277 // counting is already done in the superclass 1278 } 1279 1280 uintptr_t PushSirt(mirror::Object* /* ptr */) { 1281 num_sirt_references_++; 1282 return reinterpret_cast<uintptr_t>(nullptr); 1283 } 1284 1285 private: 1286 uint32_t num_sirt_references_; 1287 uint32_t num_stack_entries_; 1288}; 1289 1290// Visits arguments on the stack placing them into a region lower down the stack for the benefit 1291// of transitioning into native code. 1292class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor { 1293 public: 1294 BuildGenericJniFrameVisitor(mirror::ArtMethod*** sp, bool is_static, const char* shorty, 1295 uint32_t shorty_len, Thread* self) : 1296 QuickArgumentVisitor(*sp, is_static, shorty, shorty_len), sm_(this) { 1297 ComputeGenericJniFrameSize fsc; 1298 fsc.ComputeLayout(sp, is_static, shorty, shorty_len, *sp, &sirt_, &sirt_expected_refs_, 1299 &cur_stack_arg_, &cur_gpr_reg_, &cur_fpr_reg_, &code_return_, 1300 &alloca_used_size_); 1301 sirt_number_of_references_ = 0; 1302 cur_sirt_entry_ = reinterpret_cast<StackReference<mirror::Object>*>(GetFirstSirtEntry()); 1303 1304 // jni environment is always first argument 1305 sm_.AdvancePointer(self->GetJniEnv()); 1306 1307 if (is_static) { 1308 sm_.AdvanceSirt((**sp)->GetDeclaringClass()); 1309 } 1310 } 1311 1312 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE { 1313 Primitive::Type type = GetParamPrimitiveType(); 1314 switch (type) { 1315 case Primitive::kPrimLong: { 1316 jlong long_arg; 1317 if (IsSplitLongOrDouble()) { 1318 long_arg = ReadSplitLongParam(); 1319 } else { 1320 long_arg = *reinterpret_cast<jlong*>(GetParamAddress()); 1321 } 1322 sm_.AdvanceLong(long_arg); 1323 break; 1324 } 1325 case Primitive::kPrimDouble: { 1326 uint64_t double_arg; 1327 if (IsSplitLongOrDouble()) { 1328 // Read into union so that we don't case to a double. 1329 double_arg = ReadSplitLongParam(); 1330 } else { 1331 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress()); 1332 } 1333 sm_.AdvanceDouble(double_arg); 1334 break; 1335 } 1336 case Primitive::kPrimNot: { 1337 StackReference<mirror::Object>* stack_ref = 1338 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress()); 1339 sm_.AdvanceSirt(stack_ref->AsMirrorPtr()); 1340 break; 1341 } 1342 case Primitive::kPrimFloat: 1343 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress())); 1344 break; 1345 case Primitive::kPrimBoolean: // Fall-through. 1346 case Primitive::kPrimByte: // Fall-through. 1347 case Primitive::kPrimChar: // Fall-through. 1348 case Primitive::kPrimShort: // Fall-through. 1349 case Primitive::kPrimInt: // Fall-through. 1350 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress())); 1351 break; 1352 case Primitive::kPrimVoid: 1353 LOG(FATAL) << "UNREACHABLE"; 1354 break; 1355 } 1356 } 1357 1358 void FinalizeSirt(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1359 // Initialize padding entries. 1360 while (sirt_number_of_references_ < sirt_expected_refs_) { 1361 *cur_sirt_entry_ = StackReference<mirror::Object>(); 1362 cur_sirt_entry_++; 1363 sirt_number_of_references_++; 1364 } 1365 sirt_->SetNumberOfReferences(sirt_expected_refs_); 1366 DCHECK_NE(sirt_expected_refs_, 0U); 1367 // Install Sirt. 1368 self->PushSirt(sirt_); 1369 } 1370 1371 jobject GetFirstSirtEntry() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1372 return reinterpret_cast<jobject>(sirt_->GetStackReference(0)); 1373 } 1374 1375 void PushGpr(uintptr_t val) { 1376 *cur_gpr_reg_ = val; 1377 cur_gpr_reg_++; 1378 } 1379 1380 void PushFpr4(float val) { 1381 *cur_fpr_reg_ = val; 1382 cur_fpr_reg_++; 1383 } 1384 1385 void PushFpr8(uint64_t val) { 1386 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_); 1387 *tmp = val; 1388 cur_fpr_reg_ += 2; 1389 } 1390 1391 void PushStack(uintptr_t val) { 1392 *cur_stack_arg_ = val; 1393 cur_stack_arg_++; 1394 } 1395 1396 uintptr_t PushSirt(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1397 uintptr_t tmp; 1398 if (ref == nullptr) { 1399 *cur_sirt_entry_ = StackReference<mirror::Object>(); 1400 tmp = reinterpret_cast<uintptr_t>(nullptr); 1401 } else { 1402 *cur_sirt_entry_ = StackReference<mirror::Object>::FromMirrorPtr(ref); 1403 tmp = reinterpret_cast<uintptr_t>(cur_sirt_entry_); 1404 } 1405 cur_sirt_entry_++; 1406 sirt_number_of_references_++; 1407 return tmp; 1408 } 1409 1410 // Size of the part of the alloca that we actually need. 1411 size_t GetAllocaUsedSize() { 1412 return alloca_used_size_; 1413 } 1414 1415 void* GetCodeReturn() { 1416 return code_return_; 1417 } 1418 1419 private: 1420 uint32_t sirt_number_of_references_; 1421 StackReference<mirror::Object>* cur_sirt_entry_; 1422 StackIndirectReferenceTable* sirt_; 1423 uint32_t sirt_expected_refs_; 1424 uintptr_t* cur_gpr_reg_; 1425 uint32_t* cur_fpr_reg_; 1426 uintptr_t* cur_stack_arg_; 1427 // StackReference<mirror::Object>* top_of_sirt_; 1428 void* code_return_; 1429 size_t alloca_used_size_; 1430 1431 BuildGenericJniFrameStateMachine<BuildGenericJniFrameVisitor> sm_; 1432 1433 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor); 1434}; 1435 1436/* 1437 * Initializes an alloca region assumed to be directly below sp for a native call: 1438 * Create a Sirt and call stack and fill a mini stack with values to be pushed to registers. 1439 * The final element on the stack is a pointer to the native code. 1440 * 1441 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it. 1442 * We need to fix this, as the Sirt needs to go into the callee-save frame. 1443 * 1444 * The return of this function denotes: 1445 * 1) How many bytes of the alloca can be released, if the value is non-negative. 1446 * 2) An error, if the value is negative. 1447 */ 1448extern "C" ssize_t artQuickGenericJniTrampoline(Thread* self, mirror::ArtMethod** sp) 1449 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1450 mirror::ArtMethod* called = *sp; 1451 DCHECK(called->IsNative()) << PrettyMethod(called, true); 1452 1453 // run the visitor 1454 MethodHelper mh(called); 1455 1456 BuildGenericJniFrameVisitor visitor(&sp, called->IsStatic(), mh.GetShorty(), mh.GetShortyLength(), 1457 self); 1458 visitor.VisitArguments(); 1459 visitor.FinalizeSirt(self); 1460 1461 // fix up managed-stack things in Thread 1462 self->SetTopOfStack(sp, 0); 1463 1464 self->VerifyStack(); 1465 1466 // start JNI, save the cookie 1467 uint32_t cookie; 1468 if (called->IsSynchronized()) { 1469 cookie = JniMethodStartSynchronized(visitor.GetFirstSirtEntry(), self); 1470 if (self->IsExceptionPending()) { 1471 self->PopSirt(); 1472 // A negative value denotes an error. 1473 // TODO: Do we still need to fix the stack pointer? I think so. Then it's necessary to push 1474 // that value! 1475 return -1; 1476 } 1477 } else { 1478 cookie = JniMethodStart(self); 1479 } 1480 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp); 1481 *(sp32 - 1) = cookie; 1482 1483 // retrieve native code 1484 const void* nativeCode = called->GetNativeMethod(); 1485 if (nativeCode == nullptr) { 1486 // TODO: is this really an error, or do we need to try to find native code? 1487 LOG(FATAL) << "Finding native code not implemented yet."; 1488 } 1489 1490 uintptr_t* code_pointer = reinterpret_cast<uintptr_t*>(visitor.GetCodeReturn()); 1491 size_t window_size = visitor.GetAllocaUsedSize(); 1492 *code_pointer = reinterpret_cast<uintptr_t>(nativeCode); 1493 1494 // 5K reserved, window_size + frame pointer used. 1495 return (5 * KB) - window_size - kPointerSize; 1496} 1497 1498/* 1499 * Is called after the native JNI code. Responsible for cleanup (SIRT, saved state) and 1500 * unlocking. 1501 */ 1502extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self, mirror::ArtMethod** sp, 1503 jvalue result, uint64_t result_f) 1504 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1505 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp); 1506 mirror::ArtMethod* called = *sp; 1507 uint32_t cookie = *(sp32 - 1); 1508 1509 MethodHelper mh(called); 1510 char return_shorty_char = mh.GetShorty()[0]; 1511 1512 if (return_shorty_char == 'L') { 1513 // the only special ending call 1514 if (called->IsSynchronized()) { 1515 StackIndirectReferenceTable* table = 1516 reinterpret_cast<StackIndirectReferenceTable*>( 1517 reinterpret_cast<uint8_t*>(sp) + kPointerSize); 1518 jobject tmp = reinterpret_cast<jobject>(table->GetStackReference(0)); 1519 1520 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(result.l, cookie, tmp, 1521 self)); 1522 } else { 1523 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(result.l, cookie, self)); 1524 } 1525 } else { 1526 if (called->IsSynchronized()) { 1527 StackIndirectReferenceTable* table = 1528 reinterpret_cast<StackIndirectReferenceTable*>( 1529 reinterpret_cast<uint8_t*>(sp) + kPointerSize); 1530 jobject tmp = reinterpret_cast<jobject>(table->GetStackReference(0)); 1531 1532 JniMethodEndSynchronized(cookie, tmp, self); 1533 } else { 1534 JniMethodEnd(cookie, self); 1535 } 1536 1537 switch (return_shorty_char) { 1538 case 'F': // Fall-through. 1539 case 'D': 1540 return result_f; 1541 case 'Z': 1542 return result.z; 1543 case 'B': 1544 return result.b; 1545 case 'C': 1546 return result.c; 1547 case 'S': 1548 return result.s; 1549 case 'I': 1550 return result.i; 1551 case 'J': 1552 return result.j; 1553 case 'V': 1554 return 0; 1555 default: 1556 LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char; 1557 return 0; 1558 } 1559 } 1560} 1561 1562} // namespace art 1563