1 //===-- ABISysV_mips64.cpp ------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "ABISysV_mips64.h" 10 11 #include "llvm/ADT/STLExtras.h" 12 #include "llvm/ADT/Triple.h" 13 14 #include "lldb/Core/Module.h" 15 #include "lldb/Core/PluginManager.h" 16 #include "lldb/Core/Value.h" 17 #include "lldb/Core/ValueObjectConstResult.h" 18 #include "lldb/Core/ValueObjectMemory.h" 19 #include "lldb/Core/ValueObjectRegister.h" 20 #include "lldb/Symbol/UnwindPlan.h" 21 #include "lldb/Target/Process.h" 22 #include "lldb/Target/RegisterContext.h" 23 #include "lldb/Target/StackFrame.h" 24 #include "lldb/Target/Target.h" 25 #include "lldb/Target/Thread.h" 26 #include "lldb/Utility/ConstString.h" 27 #include "lldb/Utility/DataExtractor.h" 28 #include "lldb/Utility/LLDBLog.h" 29 #include "lldb/Utility/Log.h" 30 #include "lldb/Utility/RegisterValue.h" 31 #include "lldb/Utility/Status.h" 32 33 using namespace lldb; 34 using namespace lldb_private; 35 36 LLDB_PLUGIN_DEFINE(ABISysV_mips64) 37 38 enum dwarf_regnums { 39 dwarf_r0 = 0, 40 dwarf_r1, 41 dwarf_r2, 42 dwarf_r3, 43 dwarf_r4, 44 dwarf_r5, 45 dwarf_r6, 46 dwarf_r7, 47 dwarf_r8, 48 dwarf_r9, 49 dwarf_r10, 50 dwarf_r11, 51 dwarf_r12, 52 dwarf_r13, 53 dwarf_r14, 54 dwarf_r15, 55 dwarf_r16, 56 dwarf_r17, 57 dwarf_r18, 58 dwarf_r19, 59 dwarf_r20, 60 dwarf_r21, 61 dwarf_r22, 62 dwarf_r23, 63 dwarf_r24, 64 dwarf_r25, 65 dwarf_r26, 66 dwarf_r27, 67 dwarf_r28, 68 dwarf_r29, 69 dwarf_r30, 70 dwarf_r31, 71 dwarf_sr, 72 dwarf_lo, 73 dwarf_hi, 74 dwarf_bad, 75 dwarf_cause, 76 dwarf_pc 77 }; 78 79 static const RegisterInfo g_register_infos_mips64[] = { 80 // NAME ALT SZ OFF ENCODING FORMAT EH_FRAME 81 // DWARF GENERIC PROCESS PLUGIN 82 // LLDB NATIVE 83 // ======== ====== == === ============= ========== ============= 84 // ================= ==================== ================= 85 // ==================== 86 {"r0", 87 "zero", 88 8, 89 0, 90 eEncodingUint, 91 eFormatHex, 92 {dwarf_r0, dwarf_r0, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 93 LLDB_INVALID_REGNUM}, 94 nullptr, 95 nullptr, 96 }, 97 {"r1", 98 "AT", 99 8, 100 0, 101 eEncodingUint, 102 eFormatHex, 103 {dwarf_r1, dwarf_r1, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 104 LLDB_INVALID_REGNUM}, 105 nullptr, 106 nullptr, 107 }, 108 {"r2", 109 "v0", 110 8, 111 0, 112 eEncodingUint, 113 eFormatHex, 114 {dwarf_r2, dwarf_r2, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 115 LLDB_INVALID_REGNUM}, 116 nullptr, 117 nullptr, 118 }, 119 {"r3", 120 "v1", 121 8, 122 0, 123 eEncodingUint, 124 eFormatHex, 125 {dwarf_r3, dwarf_r3, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 126 LLDB_INVALID_REGNUM}, 127 nullptr, 128 nullptr, 129 }, 130 {"r4", 131 nullptr, 132 8, 133 0, 134 eEncodingUint, 135 eFormatHex, 136 {dwarf_r4, dwarf_r4, LLDB_REGNUM_GENERIC_ARG1, LLDB_INVALID_REGNUM, 137 LLDB_INVALID_REGNUM}, 138 nullptr, 139 nullptr, 140 }, 141 {"r5", 142 nullptr, 143 8, 144 0, 145 eEncodingUint, 146 eFormatHex, 147 {dwarf_r5, dwarf_r5, LLDB_REGNUM_GENERIC_ARG2, LLDB_INVALID_REGNUM, 148 LLDB_INVALID_REGNUM}, 149 nullptr, 150 nullptr, 151 }, 152 {"r6", 153 nullptr, 154 8, 155 0, 156 eEncodingUint, 157 eFormatHex, 158 {dwarf_r6, dwarf_r6, LLDB_REGNUM_GENERIC_ARG3, LLDB_INVALID_REGNUM, 159 LLDB_INVALID_REGNUM}, 160 nullptr, 161 nullptr, 162 }, 163 {"r7", 164 nullptr, 165 8, 166 0, 167 eEncodingUint, 168 eFormatHex, 169 {dwarf_r7, dwarf_r7, LLDB_REGNUM_GENERIC_ARG4, LLDB_INVALID_REGNUM, 170 LLDB_INVALID_REGNUM}, 171 nullptr, 172 nullptr, 173 }, 174 {"r8", 175 nullptr, 176 8, 177 0, 178 eEncodingUint, 179 eFormatHex, 180 {dwarf_r8, dwarf_r8, LLDB_REGNUM_GENERIC_ARG5, LLDB_INVALID_REGNUM, 181 LLDB_INVALID_REGNUM}, 182 nullptr, 183 nullptr, 184 }, 185 {"r9", 186 nullptr, 187 8, 188 0, 189 eEncodingUint, 190 eFormatHex, 191 {dwarf_r9, dwarf_r9, LLDB_REGNUM_GENERIC_ARG6, LLDB_INVALID_REGNUM, 192 LLDB_INVALID_REGNUM}, 193 nullptr, 194 nullptr, 195 }, 196 {"r10", 197 nullptr, 198 8, 199 0, 200 eEncodingUint, 201 eFormatHex, 202 {dwarf_r10, dwarf_r10, LLDB_REGNUM_GENERIC_ARG7, LLDB_INVALID_REGNUM, 203 LLDB_INVALID_REGNUM}, 204 nullptr, 205 nullptr, 206 }, 207 {"r11", 208 nullptr, 209 8, 210 0, 211 eEncodingUint, 212 eFormatHex, 213 {dwarf_r11, dwarf_r11, LLDB_REGNUM_GENERIC_ARG8, LLDB_INVALID_REGNUM, 214 LLDB_INVALID_REGNUM}, 215 nullptr, 216 nullptr, 217 }, 218 {"r12", 219 nullptr, 220 8, 221 0, 222 eEncodingUint, 223 eFormatHex, 224 {dwarf_r12, dwarf_r12, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 225 LLDB_INVALID_REGNUM}, 226 nullptr, 227 nullptr, 228 }, 229 {"r13", 230 nullptr, 231 8, 232 0, 233 eEncodingUint, 234 eFormatHex, 235 {dwarf_r13, dwarf_r13, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 236 LLDB_INVALID_REGNUM}, 237 nullptr, 238 nullptr, 239 }, 240 {"r14", 241 nullptr, 242 8, 243 0, 244 eEncodingUint, 245 eFormatHex, 246 {dwarf_r14, dwarf_r14, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 247 LLDB_INVALID_REGNUM}, 248 nullptr, 249 nullptr, 250 }, 251 {"r15", 252 nullptr, 253 8, 254 0, 255 eEncodingUint, 256 eFormatHex, 257 {dwarf_r15, dwarf_r15, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 258 LLDB_INVALID_REGNUM}, 259 nullptr, 260 nullptr, 261 }, 262 {"r16", 263 nullptr, 264 8, 265 0, 266 eEncodingUint, 267 eFormatHex, 268 {dwarf_r16, dwarf_r16, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 269 LLDB_INVALID_REGNUM}, 270 nullptr, 271 nullptr, 272 }, 273 {"r17", 274 nullptr, 275 8, 276 0, 277 eEncodingUint, 278 eFormatHex, 279 {dwarf_r17, dwarf_r17, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 280 LLDB_INVALID_REGNUM}, 281 nullptr, 282 nullptr, 283 }, 284 {"r18", 285 nullptr, 286 8, 287 0, 288 eEncodingUint, 289 eFormatHex, 290 {dwarf_r18, dwarf_r18, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 291 LLDB_INVALID_REGNUM}, 292 nullptr, 293 nullptr, 294 }, 295 {"r19", 296 nullptr, 297 8, 298 0, 299 eEncodingUint, 300 eFormatHex, 301 {dwarf_r19, dwarf_r19, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 302 LLDB_INVALID_REGNUM}, 303 nullptr, 304 nullptr, 305 }, 306 {"r20", 307 nullptr, 308 8, 309 0, 310 eEncodingUint, 311 eFormatHex, 312 {dwarf_r20, dwarf_r20, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 313 LLDB_INVALID_REGNUM}, 314 nullptr, 315 nullptr, 316 }, 317 {"r21", 318 nullptr, 319 8, 320 0, 321 eEncodingUint, 322 eFormatHex, 323 {dwarf_r21, dwarf_r21, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 324 LLDB_INVALID_REGNUM}, 325 nullptr, 326 nullptr, 327 }, 328 {"r22", 329 nullptr, 330 8, 331 0, 332 eEncodingUint, 333 eFormatHex, 334 {dwarf_r22, dwarf_r22, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 335 LLDB_INVALID_REGNUM}, 336 nullptr, 337 nullptr, 338 }, 339 {"r23", 340 nullptr, 341 8, 342 0, 343 eEncodingUint, 344 eFormatHex, 345 {dwarf_r23, dwarf_r23, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 346 LLDB_INVALID_REGNUM}, 347 nullptr, 348 nullptr, 349 }, 350 {"r24", 351 nullptr, 352 8, 353 0, 354 eEncodingUint, 355 eFormatHex, 356 {dwarf_r24, dwarf_r24, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 357 LLDB_INVALID_REGNUM}, 358 nullptr, 359 nullptr, 360 }, 361 {"r25", 362 nullptr, 363 8, 364 0, 365 eEncodingUint, 366 eFormatHex, 367 {dwarf_r25, dwarf_r25, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 368 LLDB_INVALID_REGNUM}, 369 nullptr, 370 nullptr, 371 }, 372 {"r26", 373 nullptr, 374 8, 375 0, 376 eEncodingUint, 377 eFormatHex, 378 {dwarf_r26, dwarf_r26, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 379 LLDB_INVALID_REGNUM}, 380 nullptr, 381 nullptr, 382 }, 383 {"r27", 384 nullptr, 385 8, 386 0, 387 eEncodingUint, 388 eFormatHex, 389 {dwarf_r27, dwarf_r27, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 390 LLDB_INVALID_REGNUM}, 391 nullptr, 392 nullptr, 393 }, 394 {"r28", 395 "gp", 396 8, 397 0, 398 eEncodingUint, 399 eFormatHex, 400 {dwarf_r28, dwarf_r28, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 401 LLDB_INVALID_REGNUM}, 402 nullptr, 403 nullptr, 404 }, 405 {"r29", 406 nullptr, 407 8, 408 0, 409 eEncodingUint, 410 eFormatHex, 411 {dwarf_r29, dwarf_r29, LLDB_REGNUM_GENERIC_SP, LLDB_INVALID_REGNUM, 412 LLDB_INVALID_REGNUM}, 413 nullptr, 414 nullptr, 415 }, 416 {"r30", 417 nullptr, 418 8, 419 0, 420 eEncodingUint, 421 eFormatHex, 422 {dwarf_r30, dwarf_r30, LLDB_REGNUM_GENERIC_FP, LLDB_INVALID_REGNUM, 423 LLDB_INVALID_REGNUM}, 424 nullptr, 425 nullptr, 426 }, 427 {"r31", 428 nullptr, 429 8, 430 0, 431 eEncodingUint, 432 eFormatHex, 433 {dwarf_r31, dwarf_r31, LLDB_REGNUM_GENERIC_RA, LLDB_INVALID_REGNUM, 434 LLDB_INVALID_REGNUM}, 435 nullptr, 436 nullptr, 437 }, 438 {"sr", 439 nullptr, 440 4, 441 0, 442 eEncodingUint, 443 eFormatHex, 444 {dwarf_sr, dwarf_sr, LLDB_REGNUM_GENERIC_FLAGS, LLDB_INVALID_REGNUM, 445 LLDB_INVALID_REGNUM}, 446 nullptr, 447 nullptr, 448 }, 449 {"lo", 450 nullptr, 451 8, 452 0, 453 eEncodingUint, 454 eFormatHex, 455 {dwarf_lo, dwarf_lo, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 456 LLDB_INVALID_REGNUM}, 457 nullptr, 458 nullptr, 459 }, 460 {"hi", 461 nullptr, 462 8, 463 0, 464 eEncodingUint, 465 eFormatHex, 466 {dwarf_hi, dwarf_hi, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 467 LLDB_INVALID_REGNUM}, 468 nullptr, 469 nullptr, 470 }, 471 {"bad", 472 nullptr, 473 8, 474 0, 475 eEncodingUint, 476 eFormatHex, 477 {dwarf_bad, dwarf_bad, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 478 LLDB_INVALID_REGNUM}, 479 nullptr, 480 nullptr, 481 }, 482 {"cause", 483 nullptr, 484 8, 485 0, 486 eEncodingUint, 487 eFormatHex, 488 {dwarf_cause, dwarf_cause, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, 489 LLDB_INVALID_REGNUM}, 490 nullptr, 491 nullptr, 492 }, 493 {"pc", 494 nullptr, 495 8, 496 0, 497 eEncodingUint, 498 eFormatHex, 499 {dwarf_pc, dwarf_pc, LLDB_REGNUM_GENERIC_PC, LLDB_INVALID_REGNUM, 500 LLDB_INVALID_REGNUM}, 501 nullptr, 502 nullptr, 503 }, 504 }; 505 506 static const uint32_t k_num_register_infos = 507 llvm::array_lengthof(g_register_infos_mips64); 508 509 const lldb_private::RegisterInfo * 510 ABISysV_mips64::GetRegisterInfoArray(uint32_t &count) { 511 count = k_num_register_infos; 512 return g_register_infos_mips64; 513 } 514 515 size_t ABISysV_mips64::GetRedZoneSize() const { return 0; } 516 517 // Static Functions 518 519 ABISP 520 ABISysV_mips64::CreateInstance(lldb::ProcessSP process_sp, const ArchSpec &arch) { 521 if (arch.GetTriple().isMIPS64()) 522 return ABISP( 523 new ABISysV_mips64(std::move(process_sp), MakeMCRegisterInfo(arch))); 524 return ABISP(); 525 } 526 527 bool ABISysV_mips64::PrepareTrivialCall(Thread &thread, addr_t sp, 528 addr_t func_addr, addr_t return_addr, 529 llvm::ArrayRef<addr_t> args) const { 530 Log *log = GetLog(LLDBLog::Expressions); 531 532 if (log) { 533 StreamString s; 534 s.Printf("ABISysV_mips64::PrepareTrivialCall (tid = 0x%" PRIx64 535 ", sp = 0x%" PRIx64 ", func_addr = 0x%" PRIx64 536 ", return_addr = 0x%" PRIx64, 537 thread.GetID(), (uint64_t)sp, (uint64_t)func_addr, 538 (uint64_t)return_addr); 539 540 for (size_t i = 0; i < args.size(); ++i) 541 s.Printf(", arg%zd = 0x%" PRIx64, i + 1, args[i]); 542 s.PutCString(")"); 543 log->PutString(s.GetString()); 544 } 545 546 RegisterContext *reg_ctx = thread.GetRegisterContext().get(); 547 if (!reg_ctx) 548 return false; 549 550 const RegisterInfo *reg_info = nullptr; 551 552 if (args.size() > 8) // TODO handle more than 8 arguments 553 return false; 554 555 for (size_t i = 0; i < args.size(); ++i) { 556 reg_info = reg_ctx->GetRegisterInfo(eRegisterKindGeneric, 557 LLDB_REGNUM_GENERIC_ARG1 + i); 558 LLDB_LOGF(log, "About to write arg%zd (0x%" PRIx64 ") into %s", i + 1, 559 args[i], reg_info->name); 560 if (!reg_ctx->WriteRegisterFromUnsigned(reg_info, args[i])) 561 return false; 562 } 563 564 // First, align the SP 565 566 LLDB_LOGF(log, "16-byte aligning SP: 0x%" PRIx64 " to 0x%" PRIx64, 567 (uint64_t)sp, (uint64_t)(sp & ~0xfull)); 568 569 sp &= ~(0xfull); // 16-byte alignment 570 571 Status error; 572 const RegisterInfo *pc_reg_info = 573 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); 574 const RegisterInfo *sp_reg_info = 575 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP); 576 const RegisterInfo *ra_reg_info = 577 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA); 578 const RegisterInfo *r25_info = reg_ctx->GetRegisterInfoByName("r25", 0); 579 const RegisterInfo *r0_info = reg_ctx->GetRegisterInfoByName("zero", 0); 580 581 LLDB_LOGF(log, "Writing R0: 0x%" PRIx64, (uint64_t)0); 582 583 /* Write r0 with 0, in case we are stopped in syscall, 584 * such setting prevents automatic decrement of the PC. 585 * This clears the bug 23659 for MIPS. 586 */ 587 if (!reg_ctx->WriteRegisterFromUnsigned(r0_info, (uint64_t)0)) 588 return false; 589 590 LLDB_LOGF(log, "Writing SP: 0x%" PRIx64, (uint64_t)sp); 591 592 // Set "sp" to the requested value 593 if (!reg_ctx->WriteRegisterFromUnsigned(sp_reg_info, sp)) 594 return false; 595 596 LLDB_LOGF(log, "Writing RA: 0x%" PRIx64, (uint64_t)return_addr); 597 598 // Set "ra" to the return address 599 if (!reg_ctx->WriteRegisterFromUnsigned(ra_reg_info, return_addr)) 600 return false; 601 602 LLDB_LOGF(log, "Writing PC: 0x%" PRIx64, (uint64_t)func_addr); 603 604 // Set pc to the address of the called function. 605 if (!reg_ctx->WriteRegisterFromUnsigned(pc_reg_info, func_addr)) 606 return false; 607 608 LLDB_LOGF(log, "Writing r25: 0x%" PRIx64, (uint64_t)func_addr); 609 610 // All callers of position independent functions must place the address of 611 // the called function in t9 (r25) 612 if (!reg_ctx->WriteRegisterFromUnsigned(r25_info, func_addr)) 613 return false; 614 615 return true; 616 } 617 618 bool ABISysV_mips64::GetArgumentValues(Thread &thread, 619 ValueList &values) const { 620 return false; 621 } 622 623 Status ABISysV_mips64::SetReturnValueObject(lldb::StackFrameSP &frame_sp, 624 lldb::ValueObjectSP &new_value_sp) { 625 Status error; 626 if (!new_value_sp) { 627 error.SetErrorString("Empty value object for return value."); 628 return error; 629 } 630 631 CompilerType compiler_type = new_value_sp->GetCompilerType(); 632 if (!compiler_type) { 633 error.SetErrorString("Null clang type for return value."); 634 return error; 635 } 636 637 Thread *thread = frame_sp->GetThread().get(); 638 639 RegisterContext *reg_ctx = thread->GetRegisterContext().get(); 640 641 if (!reg_ctx) 642 error.SetErrorString("no registers are available"); 643 644 DataExtractor data; 645 Status data_error; 646 size_t num_bytes = new_value_sp->GetData(data, data_error); 647 if (data_error.Fail()) { 648 error.SetErrorStringWithFormat( 649 "Couldn't convert return value to raw data: %s", 650 data_error.AsCString()); 651 return error; 652 } 653 654 const uint32_t type_flags = compiler_type.GetTypeInfo(nullptr); 655 656 if (type_flags & eTypeIsScalar || type_flags & eTypeIsPointer) { 657 if (type_flags & eTypeIsInteger || type_flags & eTypeIsPointer) { 658 lldb::offset_t offset = 0; 659 660 if (num_bytes <= 16) { 661 const RegisterInfo *r2_info = reg_ctx->GetRegisterInfoByName("r2", 0); 662 if (num_bytes <= 8) { 663 uint64_t raw_value = data.GetMaxU64(&offset, num_bytes); 664 665 if (!reg_ctx->WriteRegisterFromUnsigned(r2_info, raw_value)) 666 error.SetErrorString("failed to write register r2"); 667 } else { 668 uint64_t raw_value = data.GetMaxU64(&offset, 8); 669 if (reg_ctx->WriteRegisterFromUnsigned(r2_info, raw_value)) { 670 const RegisterInfo *r3_info = 671 reg_ctx->GetRegisterInfoByName("r3", 0); 672 raw_value = data.GetMaxU64(&offset, num_bytes - offset); 673 674 if (!reg_ctx->WriteRegisterFromUnsigned(r3_info, raw_value)) 675 error.SetErrorString("failed to write register r3"); 676 } else 677 error.SetErrorString("failed to write register r2"); 678 } 679 } else { 680 error.SetErrorString("We don't support returning longer than 128 bit " 681 "integer values at present."); 682 } 683 } else if (type_flags & eTypeIsFloat) { 684 error.SetErrorString("TODO: Handle Float Types."); 685 } 686 } else if (type_flags & eTypeIsVector) { 687 error.SetErrorString("returning vector values are not supported"); 688 } 689 690 return error; 691 } 692 693 ValueObjectSP ABISysV_mips64::GetReturnValueObjectSimple( 694 Thread &thread, CompilerType &return_compiler_type) const { 695 ValueObjectSP return_valobj_sp; 696 return return_valobj_sp; 697 } 698 699 ValueObjectSP ABISysV_mips64::GetReturnValueObjectImpl( 700 Thread &thread, CompilerType &return_compiler_type) const { 701 ValueObjectSP return_valobj_sp; 702 Value value; 703 Status error; 704 705 ExecutionContext exe_ctx(thread.shared_from_this()); 706 if (exe_ctx.GetTargetPtr() == nullptr || exe_ctx.GetProcessPtr() == nullptr) 707 return return_valobj_sp; 708 709 value.SetCompilerType(return_compiler_type); 710 711 RegisterContext *reg_ctx = thread.GetRegisterContext().get(); 712 if (!reg_ctx) 713 return return_valobj_sp; 714 715 Target *target = exe_ctx.GetTargetPtr(); 716 const ArchSpec target_arch = target->GetArchitecture(); 717 ByteOrder target_byte_order = target_arch.GetByteOrder(); 718 llvm::Optional<uint64_t> byte_size = 719 return_compiler_type.GetByteSize(&thread); 720 if (!byte_size) 721 return return_valobj_sp; 722 const uint32_t type_flags = return_compiler_type.GetTypeInfo(nullptr); 723 uint32_t fp_flag = 724 target_arch.GetFlags() & lldb_private::ArchSpec::eMIPS_ABI_FP_mask; 725 726 const RegisterInfo *r2_info = reg_ctx->GetRegisterInfoByName("r2", 0); 727 const RegisterInfo *r3_info = reg_ctx->GetRegisterInfoByName("r3", 0); 728 729 if (type_flags & eTypeIsScalar || type_flags & eTypeIsPointer) { 730 value.SetValueType(Value::ValueType::Scalar); 731 732 bool success = false; 733 if (type_flags & eTypeIsInteger || type_flags & eTypeIsPointer) { 734 // Extract the register context so we can read arguments from registers 735 // In MIPS register "r2" (v0) holds the integer function return values 736 737 uint64_t raw_value = reg_ctx->ReadRegisterAsUnsigned(r2_info, 0); 738 739 const bool is_signed = (type_flags & eTypeIsSigned) != 0; 740 switch (*byte_size) { 741 default: 742 break; 743 744 case sizeof(uint64_t): 745 if (is_signed) 746 value.GetScalar() = (int64_t)(raw_value); 747 else 748 value.GetScalar() = (uint64_t)(raw_value); 749 success = true; 750 break; 751 752 case sizeof(uint32_t): 753 if (is_signed) 754 value.GetScalar() = (int32_t)(raw_value & UINT32_MAX); 755 else 756 value.GetScalar() = (uint32_t)(raw_value & UINT32_MAX); 757 success = true; 758 break; 759 760 case sizeof(uint16_t): 761 if (is_signed) 762 value.GetScalar() = (int16_t)(raw_value & UINT16_MAX); 763 else 764 value.GetScalar() = (uint16_t)(raw_value & UINT16_MAX); 765 success = true; 766 break; 767 768 case sizeof(uint8_t): 769 if (is_signed) 770 value.GetScalar() = (int8_t)(raw_value & UINT8_MAX); 771 else 772 value.GetScalar() = (uint8_t)(raw_value & UINT8_MAX); 773 success = true; 774 break; 775 } 776 } else if (type_flags & eTypeIsFloat) { 777 if (type_flags & eTypeIsComplex) { 778 // Don't handle complex yet. 779 } else if (IsSoftFloat(fp_flag)) { 780 uint64_t raw_value = reg_ctx->ReadRegisterAsUnsigned(r2_info, 0); 781 switch (*byte_size) { 782 case 4: 783 value.GetScalar() = *((float *)(&raw_value)); 784 success = true; 785 break; 786 case 8: 787 value.GetScalar() = *((double *)(&raw_value)); 788 success = true; 789 break; 790 case 16: 791 uint64_t result[2]; 792 if (target_byte_order == eByteOrderLittle) { 793 result[0] = raw_value; 794 result[1] = reg_ctx->ReadRegisterAsUnsigned(r3_info, 0); 795 value.GetScalar() = *((long double *)(result)); 796 } else { 797 result[0] = reg_ctx->ReadRegisterAsUnsigned(r3_info, 0); 798 result[1] = raw_value; 799 value.GetScalar() = *((long double *)(result)); 800 } 801 success = true; 802 break; 803 } 804 805 } else { 806 if (*byte_size <= sizeof(long double)) { 807 const RegisterInfo *f0_info = reg_ctx->GetRegisterInfoByName("f0", 0); 808 809 RegisterValue f0_value; 810 DataExtractor f0_data; 811 812 reg_ctx->ReadRegister(f0_info, f0_value); 813 814 f0_value.GetData(f0_data); 815 816 lldb::offset_t offset = 0; 817 if (*byte_size == sizeof(float)) { 818 value.GetScalar() = (float)f0_data.GetFloat(&offset); 819 success = true; 820 } else if (*byte_size == sizeof(double)) { 821 value.GetScalar() = (double)f0_data.GetDouble(&offset); 822 success = true; 823 } else if (*byte_size == sizeof(long double)) { 824 const RegisterInfo *f2_info = 825 reg_ctx->GetRegisterInfoByName("f2", 0); 826 RegisterValue f2_value; 827 DataExtractor f2_data; 828 reg_ctx->ReadRegister(f2_info, f2_value); 829 DataExtractor *copy_from_extractor = nullptr; 830 WritableDataBufferSP data_sp(new DataBufferHeap(16, 0)); 831 DataExtractor return_ext( 832 data_sp, target_byte_order, 833 target->GetArchitecture().GetAddressByteSize()); 834 835 if (target_byte_order == eByteOrderLittle) { 836 copy_from_extractor = &f0_data; 837 copy_from_extractor->CopyByteOrderedData( 838 0, 8, data_sp->GetBytes(), *byte_size - 8, target_byte_order); 839 f2_value.GetData(f2_data); 840 copy_from_extractor = &f2_data; 841 copy_from_extractor->CopyByteOrderedData( 842 0, 8, data_sp->GetBytes() + 8, *byte_size - 8, 843 target_byte_order); 844 } else { 845 copy_from_extractor = &f0_data; 846 copy_from_extractor->CopyByteOrderedData( 847 0, 8, data_sp->GetBytes() + 8, *byte_size - 8, 848 target_byte_order); 849 f2_value.GetData(f2_data); 850 copy_from_extractor = &f2_data; 851 copy_from_extractor->CopyByteOrderedData( 852 0, 8, data_sp->GetBytes(), *byte_size - 8, target_byte_order); 853 } 854 855 return_valobj_sp = ValueObjectConstResult::Create( 856 &thread, return_compiler_type, ConstString(""), return_ext); 857 return return_valobj_sp; 858 } 859 } 860 } 861 } 862 863 if (success) 864 return_valobj_sp = ValueObjectConstResult::Create( 865 thread.GetStackFrameAtIndex(0).get(), value, ConstString("")); 866 } else if (type_flags & eTypeIsStructUnion || type_flags & eTypeIsClass || 867 type_flags & eTypeIsVector) { 868 // Any structure of up to 16 bytes in size is returned in the registers. 869 if (*byte_size <= 16) { 870 WritableDataBufferSP data_sp(new DataBufferHeap(16, 0)); 871 DataExtractor return_ext(data_sp, target_byte_order, 872 target->GetArchitecture().GetAddressByteSize()); 873 874 RegisterValue r2_value, r3_value, f0_value, f1_value, f2_value; 875 // Tracks how much bytes of r2 and r3 registers we've consumed so far 876 uint32_t integer_bytes = 0; 877 878 // True if return values are in FP return registers. 879 bool use_fp_regs = false; 880 // True if we found any non floating point field in structure. 881 bool found_non_fp_field = false; 882 // True if return values are in r2 register. 883 bool use_r2 = false; 884 // True if return values are in r3 register. 885 bool use_r3 = false; 886 // True if the result is copied into our data buffer 887 bool sucess = false; 888 std::string name; 889 bool is_complex; 890 uint32_t count; 891 const uint32_t num_children = return_compiler_type.GetNumFields(); 892 893 // A structure consisting of one or two FP values (and nothing else) will 894 // be returned in the two FP return-value registers i.e fp0 and fp2. 895 if (num_children <= 2) { 896 uint64_t field_bit_offset = 0; 897 898 // Check if this structure contains only floating point fields 899 for (uint32_t idx = 0; idx < num_children; idx++) { 900 CompilerType field_compiler_type = 901 return_compiler_type.GetFieldAtIndex(idx, name, &field_bit_offset, 902 nullptr, nullptr); 903 904 if (field_compiler_type.IsFloatingPointType(count, is_complex)) 905 use_fp_regs = true; 906 else 907 found_non_fp_field = true; 908 } 909 910 if (use_fp_regs && !found_non_fp_field) { 911 // We have one or two FP-only values in this structure. Get it from 912 // f0/f2 registers. 913 DataExtractor f0_data, f1_data, f2_data; 914 const RegisterInfo *f0_info = reg_ctx->GetRegisterInfoByName("f0", 0); 915 const RegisterInfo *f1_info = reg_ctx->GetRegisterInfoByName("f1", 0); 916 const RegisterInfo *f2_info = reg_ctx->GetRegisterInfoByName("f2", 0); 917 918 reg_ctx->ReadRegister(f0_info, f0_value); 919 reg_ctx->ReadRegister(f2_info, f2_value); 920 921 f0_value.GetData(f0_data); 922 923 for (uint32_t idx = 0; idx < num_children; idx++) { 924 CompilerType field_compiler_type = 925 return_compiler_type.GetFieldAtIndex( 926 idx, name, &field_bit_offset, nullptr, nullptr); 927 llvm::Optional<uint64_t> field_byte_width = 928 field_compiler_type.GetByteSize(&thread); 929 if (!field_byte_width) 930 return return_valobj_sp; 931 932 DataExtractor *copy_from_extractor = nullptr; 933 uint64_t return_value[2]; 934 offset_t offset = 0; 935 936 if (idx == 0) { 937 // This case is for long double type. 938 if (*field_byte_width == 16) { 939 940 // If structure contains long double type, then it is returned 941 // in fp0/fp1 registers. 942 if (target_byte_order == eByteOrderLittle) { 943 return_value[0] = f0_data.GetU64(&offset); 944 reg_ctx->ReadRegister(f1_info, f1_value); 945 f1_value.GetData(f1_data); 946 offset = 0; 947 return_value[1] = f1_data.GetU64(&offset); 948 } else { 949 return_value[1] = f0_data.GetU64(&offset); 950 reg_ctx->ReadRegister(f1_info, f1_value); 951 f1_value.GetData(f1_data); 952 offset = 0; 953 return_value[0] = f1_data.GetU64(&offset); 954 } 955 956 f0_data.SetData(return_value, *field_byte_width, 957 target_byte_order); 958 } 959 copy_from_extractor = &f0_data; // This is in f0, copy from 960 // register to our result 961 // structure 962 } else { 963 f2_value.GetData(f2_data); 964 // This is in f2, copy from register to our result structure 965 copy_from_extractor = &f2_data; 966 } 967 968 // Sanity check to avoid crash 969 if (!copy_from_extractor || 970 *field_byte_width > copy_from_extractor->GetByteSize()) 971 return return_valobj_sp; 972 973 // copy the register contents into our data buffer 974 copy_from_extractor->CopyByteOrderedData( 975 0, *field_byte_width, 976 data_sp->GetBytes() + (field_bit_offset / 8), *field_byte_width, 977 target_byte_order); 978 } 979 980 // The result is in our data buffer. Create a variable object out of 981 // it 982 return_valobj_sp = ValueObjectConstResult::Create( 983 &thread, return_compiler_type, ConstString(""), return_ext); 984 985 return return_valobj_sp; 986 } 987 } 988 989 // If we reach here, it means this structure either contains more than 990 // two fields or it contains at least one non floating point type. In 991 // that case, all fields are returned in GP return registers. 992 for (uint32_t idx = 0; idx < num_children; idx++) { 993 uint64_t field_bit_offset = 0; 994 bool is_signed; 995 uint32_t padding; 996 997 CompilerType field_compiler_type = return_compiler_type.GetFieldAtIndex( 998 idx, name, &field_bit_offset, nullptr, nullptr); 999 llvm::Optional<uint64_t> field_byte_width = 1000 field_compiler_type.GetByteSize(&thread); 1001 1002 // if we don't know the size of the field (e.g. invalid type), just 1003 // bail out 1004 if (!field_byte_width || *field_byte_width == 0) 1005 break; 1006 1007 uint32_t field_byte_offset = field_bit_offset / 8; 1008 1009 if (field_compiler_type.IsIntegerOrEnumerationType(is_signed) || 1010 field_compiler_type.IsPointerType() || 1011 field_compiler_type.IsFloatingPointType(count, is_complex)) { 1012 padding = field_byte_offset - integer_bytes; 1013 1014 if (integer_bytes < 8) { 1015 // We have not yet consumed r2 completely. 1016 if (integer_bytes + *field_byte_width + padding <= 8) { 1017 // This field fits in r2, copy its value from r2 to our result 1018 // structure 1019 integer_bytes = integer_bytes + *field_byte_width + 1020 padding; // Increase the consumed bytes. 1021 use_r2 = true; 1022 } else { 1023 // There isn't enough space left in r2 for this field, so this 1024 // will be in r3. 1025 integer_bytes = integer_bytes + *field_byte_width + 1026 padding; // Increase the consumed bytes. 1027 use_r3 = true; 1028 } 1029 } 1030 // We already have consumed at-least 8 bytes that means r2 is done, 1031 // and this field will be in r3. Check if this field can fit in r3. 1032 else if (integer_bytes + *field_byte_width + padding <= 16) { 1033 integer_bytes = integer_bytes + *field_byte_width + padding; 1034 use_r3 = true; 1035 } else { 1036 // There isn't any space left for this field, this should not 1037 // happen as we have already checked the overall size is not 1038 // greater than 16 bytes. For now, return a nullptr return value 1039 // object. 1040 return return_valobj_sp; 1041 } 1042 } 1043 } 1044 // Vector types up to 16 bytes are returned in GP return registers 1045 if (type_flags & eTypeIsVector) { 1046 if (*byte_size <= 8) 1047 use_r2 = true; 1048 else { 1049 use_r2 = true; 1050 use_r3 = true; 1051 } 1052 } 1053 1054 if (use_r2) { 1055 reg_ctx->ReadRegister(r2_info, r2_value); 1056 1057 const size_t bytes_copied = r2_value.GetAsMemoryData( 1058 r2_info, data_sp->GetBytes(), r2_info->byte_size, target_byte_order, 1059 error); 1060 if (bytes_copied != r2_info->byte_size) 1061 return return_valobj_sp; 1062 sucess = true; 1063 } 1064 if (use_r3) { 1065 reg_ctx->ReadRegister(r3_info, r3_value); 1066 const size_t bytes_copied = r3_value.GetAsMemoryData( 1067 r3_info, data_sp->GetBytes() + r2_info->byte_size, 1068 r3_info->byte_size, target_byte_order, error); 1069 1070 if (bytes_copied != r3_info->byte_size) 1071 return return_valobj_sp; 1072 sucess = true; 1073 } 1074 if (sucess) { 1075 // The result is in our data buffer. Create a variable object out of 1076 // it 1077 return_valobj_sp = ValueObjectConstResult::Create( 1078 &thread, return_compiler_type, ConstString(""), return_ext); 1079 } 1080 return return_valobj_sp; 1081 } 1082 1083 // Any structure/vector greater than 16 bytes in size is returned in 1084 // memory. The pointer to that memory is returned in r2. 1085 uint64_t mem_address = reg_ctx->ReadRegisterAsUnsigned( 1086 reg_ctx->GetRegisterInfoByName("r2", 0), 0); 1087 1088 // We have got the address. Create a memory object out of it 1089 return_valobj_sp = ValueObjectMemory::Create( 1090 &thread, "", Address(mem_address, nullptr), return_compiler_type); 1091 } 1092 return return_valobj_sp; 1093 } 1094 1095 bool ABISysV_mips64::CreateFunctionEntryUnwindPlan(UnwindPlan &unwind_plan) { 1096 unwind_plan.Clear(); 1097 unwind_plan.SetRegisterKind(eRegisterKindDWARF); 1098 1099 UnwindPlan::RowSP row(new UnwindPlan::Row); 1100 1101 // Our Call Frame Address is the stack pointer value 1102 row->GetCFAValue().SetIsRegisterPlusOffset(dwarf_r29, 0); 1103 1104 // The previous PC is in the RA 1105 row->SetRegisterLocationToRegister(dwarf_pc, dwarf_r31, true); 1106 unwind_plan.AppendRow(row); 1107 1108 // All other registers are the same. 1109 1110 unwind_plan.SetSourceName("mips64 at-func-entry default"); 1111 unwind_plan.SetSourcedFromCompiler(eLazyBoolNo); 1112 unwind_plan.SetReturnAddressRegister(dwarf_r31); 1113 return true; 1114 } 1115 1116 bool ABISysV_mips64::CreateDefaultUnwindPlan(UnwindPlan &unwind_plan) { 1117 unwind_plan.Clear(); 1118 unwind_plan.SetRegisterKind(eRegisterKindDWARF); 1119 1120 UnwindPlan::RowSP row(new UnwindPlan::Row); 1121 1122 row->SetUnspecifiedRegistersAreUndefined(true); 1123 row->GetCFAValue().SetIsRegisterPlusOffset(dwarf_r29, 0); 1124 1125 row->SetRegisterLocationToRegister(dwarf_pc, dwarf_r31, true); 1126 1127 unwind_plan.AppendRow(row); 1128 unwind_plan.SetSourceName("mips64 default unwind plan"); 1129 unwind_plan.SetSourcedFromCompiler(eLazyBoolNo); 1130 unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo); 1131 unwind_plan.SetUnwindPlanForSignalTrap(eLazyBoolNo); 1132 return true; 1133 } 1134 1135 bool ABISysV_mips64::RegisterIsVolatile(const RegisterInfo *reg_info) { 1136 return !RegisterIsCalleeSaved(reg_info); 1137 } 1138 1139 bool ABISysV_mips64::IsSoftFloat(uint32_t fp_flag) const { 1140 return (fp_flag == lldb_private::ArchSpec::eMIPS_ABI_FP_SOFT); 1141 } 1142 1143 bool ABISysV_mips64::RegisterIsCalleeSaved(const RegisterInfo *reg_info) { 1144 if (reg_info) { 1145 // Preserved registers are : 1146 // r16-r23, r28, r29, r30, r31 1147 1148 int reg = ((reg_info->byte_offset) / 8); 1149 1150 bool save = (reg >= 16) && (reg <= 23); 1151 save |= (reg >= 28) && (reg <= 31); 1152 1153 return save; 1154 } 1155 return false; 1156 } 1157 1158 void ABISysV_mips64::Initialize() { 1159 PluginManager::RegisterPlugin( 1160 GetPluginNameStatic(), "System V ABI for mips64 targets", CreateInstance); 1161 } 1162 1163 void ABISysV_mips64::Terminate() { 1164 PluginManager::UnregisterPlugin(CreateInstance); 1165 } 1166