RegsMips64.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (C) 2017 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. #include <stdint.h>
  17. #include <string.h>
  18. #include <functional>
  19. #include <unwindstack/Elf.h>
  20. #include <unwindstack/MachineMips64.h>
  21. #include <unwindstack/MapInfo.h>
  22. #include <unwindstack/Memory.h>
  23. #include <unwindstack/RegsMips64.h>
  24. #include <unwindstack/UcontextMips64.h>
  25. #include <unwindstack/UserMips64.h>
  26. namespace unwindstack {
  27. RegsMips64::RegsMips64()
  28. : RegsImpl<uint64_t>(MIPS64_REG_LAST, Location(LOCATION_REGISTER, MIPS64_REG_RA)) {}
  29. ArchEnum RegsMips64::Arch() {
  30. return ARCH_MIPS64;
  31. }
  32. uint64_t RegsMips64::pc() {
  33. return regs_[MIPS64_REG_PC];
  34. }
  35. uint64_t RegsMips64::sp() {
  36. return regs_[MIPS64_REG_SP];
  37. }
  38. void RegsMips64::set_pc(uint64_t pc) {
  39. regs_[MIPS64_REG_PC] = pc;
  40. }
  41. void RegsMips64::set_sp(uint64_t sp) {
  42. regs_[MIPS64_REG_SP] = sp;
  43. }
  44. uint64_t RegsMips64::GetPcAdjustment(uint64_t rel_pc, Elf*) {
  45. if (rel_pc < 8) {
  46. return 0;
  47. }
  48. // For now, just assume no compact branches
  49. return 8;
  50. }
  51. bool RegsMips64::SetPcFromReturnAddress(Memory*) {
  52. uint64_t ra = regs_[MIPS64_REG_RA];
  53. if (regs_[MIPS64_REG_PC] == ra) {
  54. return false;
  55. }
  56. regs_[MIPS64_REG_PC] = ra;
  57. return true;
  58. }
  59. void RegsMips64::IterateRegisters(std::function<void(const char*, uint64_t)> fn) {
  60. fn("r0", regs_[MIPS64_REG_R0]);
  61. fn("r1", regs_[MIPS64_REG_R1]);
  62. fn("r2", regs_[MIPS64_REG_R2]);
  63. fn("r3", regs_[MIPS64_REG_R3]);
  64. fn("r4", regs_[MIPS64_REG_R4]);
  65. fn("r5", regs_[MIPS64_REG_R5]);
  66. fn("r6", regs_[MIPS64_REG_R6]);
  67. fn("r7", regs_[MIPS64_REG_R7]);
  68. fn("r8", regs_[MIPS64_REG_R8]);
  69. fn("r9", regs_[MIPS64_REG_R9]);
  70. fn("r10", regs_[MIPS64_REG_R10]);
  71. fn("r11", regs_[MIPS64_REG_R11]);
  72. fn("r12", regs_[MIPS64_REG_R12]);
  73. fn("r13", regs_[MIPS64_REG_R13]);
  74. fn("r14", regs_[MIPS64_REG_R14]);
  75. fn("r15", regs_[MIPS64_REG_R15]);
  76. fn("r16", regs_[MIPS64_REG_R16]);
  77. fn("r17", regs_[MIPS64_REG_R17]);
  78. fn("r18", regs_[MIPS64_REG_R18]);
  79. fn("r19", regs_[MIPS64_REG_R19]);
  80. fn("r20", regs_[MIPS64_REG_R20]);
  81. fn("r21", regs_[MIPS64_REG_R21]);
  82. fn("r22", regs_[MIPS64_REG_R22]);
  83. fn("r23", regs_[MIPS64_REG_R23]);
  84. fn("r24", regs_[MIPS64_REG_R24]);
  85. fn("r25", regs_[MIPS64_REG_R25]);
  86. fn("r26", regs_[MIPS64_REG_R26]);
  87. fn("r27", regs_[MIPS64_REG_R27]);
  88. fn("r28", regs_[MIPS64_REG_R28]);
  89. fn("sp", regs_[MIPS64_REG_SP]);
  90. fn("r30", regs_[MIPS64_REG_R30]);
  91. fn("ra", regs_[MIPS64_REG_RA]);
  92. fn("pc", regs_[MIPS64_REG_PC]);
  93. }
  94. Regs* RegsMips64::Read(void* remote_data) {
  95. mips64_user_regs* user = reinterpret_cast<mips64_user_regs*>(remote_data);
  96. RegsMips64* regs = new RegsMips64();
  97. uint64_t* reg_data = reinterpret_cast<uint64_t*>(regs->RawData());
  98. memcpy(regs->RawData(), &user->regs[MIPS64_EF_R0], (MIPS64_REG_R31 + 1) * sizeof(uint64_t));
  99. reg_data[MIPS64_REG_PC] = user->regs[MIPS64_EF_CP0_EPC];
  100. return regs;
  101. }
  102. Regs* RegsMips64::CreateFromUcontext(void* ucontext) {
  103. mips64_ucontext_t* mips64_ucontext = reinterpret_cast<mips64_ucontext_t*>(ucontext);
  104. RegsMips64* regs = new RegsMips64();
  105. // Copy 64 bit sc_regs over to 64 bit regs
  106. memcpy(regs->RawData(), &mips64_ucontext->uc_mcontext.sc_regs[0], 32 * sizeof(uint64_t));
  107. (*regs)[MIPS64_REG_PC] = mips64_ucontext->uc_mcontext.sc_pc;
  108. return regs;
  109. }
  110. bool RegsMips64::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) {
  111. uint64_t data;
  112. Memory* elf_memory = elf->memory();
  113. // Read from elf memory since it is usually more expensive to read from
  114. // process memory.
  115. if (!elf_memory->Read(rel_pc, &data, sizeof(data))) {
  116. return false;
  117. }
  118. // Look for the kernel sigreturn function.
  119. // __vdso_rt_sigreturn:
  120. // 0x2402145b li v0, 0x145b
  121. // 0x0000000c syscall
  122. if (data != 0x0000000c2402145bULL) {
  123. return false;
  124. }
  125. // vdso_rt_sigreturn => read rt_sigframe
  126. // offset = siginfo offset + sizeof(siginfo) + uc_mcontext offset
  127. // read 64 bit sc_regs[32] from stack into 64 bit regs_
  128. uint64_t sp = regs_[MIPS64_REG_SP];
  129. if (!process_memory->Read(sp + 24 + 128 + 40, regs_.data(),
  130. sizeof(uint64_t) * (MIPS64_REG_LAST - 1))) {
  131. return false;
  132. }
  133. // offset = siginfo offset + sizeof(siginfo) + uc_mcontext offset + sc_pc offset
  134. // read 64 bit sc_pc from stack into 64 bit regs_[MIPS64_REG_PC]
  135. if (!process_memory->Read(sp + 24 + 128 + 40 + 576, &regs_[MIPS64_REG_PC], sizeof(uint64_t))) {
  136. return false;
  137. }
  138. return true;
  139. }
  140. Regs* RegsMips64::Clone() {
  141. return new RegsMips64(*this);
  142. }
  143. } // namespace unwindstack