| Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame] | 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 |  | 
 | 17 | #include <stdint.h> | 
| Florian Mayer | 3f1f2e0 | 2018-10-23 15:56:28 +0100 | [diff] [blame] | 18 | #include <string.h> | 
| Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame] | 19 |  | 
 | 20 | #include <functional> | 
 | 21 |  | 
 | 22 | #include <unwindstack/Elf.h> | 
| Christopher Ferris | 5391416 | 2018-02-08 19:27:47 -0800 | [diff] [blame] | 23 | #include <unwindstack/MachineMips.h> | 
| Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame] | 24 | #include <unwindstack/MapInfo.h> | 
 | 25 | #include <unwindstack/Memory.h> | 
 | 26 | #include <unwindstack/RegsMips.h> | 
| Christopher Ferris | 5391416 | 2018-02-08 19:27:47 -0800 | [diff] [blame] | 27 | #include <unwindstack/UcontextMips.h> | 
 | 28 | #include <unwindstack/UserMips.h> | 
| Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame] | 29 |  | 
 | 30 | namespace unwindstack { | 
 | 31 |  | 
 | 32 | RegsMips::RegsMips() | 
| Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 33 |     : RegsImpl<uint32_t>(MIPS_REG_LAST, Location(LOCATION_REGISTER, MIPS_REG_RA)) {} | 
| Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame] | 34 |  | 
 | 35 | ArchEnum RegsMips::Arch() { | 
 | 36 |   return ARCH_MIPS; | 
 | 37 | } | 
 | 38 |  | 
| Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 39 | uint64_t RegsMips::pc() { | 
 | 40 |   return regs_[MIPS_REG_PC]; | 
 | 41 | } | 
 | 42 |  | 
 | 43 | uint64_t RegsMips::sp() { | 
 | 44 |   return regs_[MIPS_REG_SP]; | 
 | 45 | } | 
 | 46 |  | 
 | 47 | void RegsMips::set_pc(uint64_t pc) { | 
 | 48 |   regs_[MIPS_REG_PC] = static_cast<uint32_t>(pc); | 
 | 49 | } | 
 | 50 |  | 
 | 51 | void RegsMips::set_sp(uint64_t sp) { | 
 | 52 |   regs_[MIPS_REG_SP] = static_cast<uint32_t>(sp); | 
 | 53 | } | 
 | 54 |  | 
| Christopher Ferris | 6dbc28e | 2018-03-28 15:12:49 -0700 | [diff] [blame] | 55 | uint64_t RegsMips::GetPcAdjustment(uint64_t rel_pc, Elf*) { | 
 | 56 |   if (rel_pc < 8) { | 
| Christopher Ferris | a2ec50b | 2018-02-21 15:39:07 -0800 | [diff] [blame] | 57 |     return 0; | 
| Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame] | 58 |   } | 
| Christopher Ferris | a2ec50b | 2018-02-21 15:39:07 -0800 | [diff] [blame] | 59 |   // For now, just assume no compact branches | 
 | 60 |   return 8; | 
| Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame] | 61 | } | 
 | 62 |  | 
| Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame] | 63 | bool RegsMips::SetPcFromReturnAddress(Memory*) { | 
| Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 64 |   uint32_t ra = regs_[MIPS_REG_RA]; | 
 | 65 |   if (regs_[MIPS_REG_PC] == ra) { | 
| Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame] | 66 |     return false; | 
 | 67 |   } | 
 | 68 |  | 
| Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 69 |   regs_[MIPS_REG_PC] = ra; | 
| Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame] | 70 |   return true; | 
 | 71 | } | 
 | 72 |  | 
 | 73 | void RegsMips::IterateRegisters(std::function<void(const char*, uint64_t)> fn) { | 
 | 74 |   fn("r0", regs_[MIPS_REG_R0]); | 
 | 75 |   fn("r1", regs_[MIPS_REG_R1]); | 
 | 76 |   fn("r2", regs_[MIPS_REG_R2]); | 
 | 77 |   fn("r3", regs_[MIPS_REG_R3]); | 
 | 78 |   fn("r4", regs_[MIPS_REG_R4]); | 
 | 79 |   fn("r5", regs_[MIPS_REG_R5]); | 
 | 80 |   fn("r6", regs_[MIPS_REG_R6]); | 
 | 81 |   fn("r7", regs_[MIPS_REG_R7]); | 
 | 82 |   fn("r8", regs_[MIPS_REG_R8]); | 
 | 83 |   fn("r9", regs_[MIPS_REG_R9]); | 
 | 84 |   fn("r10", regs_[MIPS_REG_R10]); | 
 | 85 |   fn("r11", regs_[MIPS_REG_R11]); | 
 | 86 |   fn("r12", regs_[MIPS_REG_R12]); | 
 | 87 |   fn("r13", regs_[MIPS_REG_R13]); | 
 | 88 |   fn("r14", regs_[MIPS_REG_R14]); | 
 | 89 |   fn("r15", regs_[MIPS_REG_R15]); | 
 | 90 |   fn("r16", regs_[MIPS_REG_R16]); | 
 | 91 |   fn("r17", regs_[MIPS_REG_R17]); | 
 | 92 |   fn("r18", regs_[MIPS_REG_R18]); | 
 | 93 |   fn("r19", regs_[MIPS_REG_R19]); | 
 | 94 |   fn("r20", regs_[MIPS_REG_R20]); | 
 | 95 |   fn("r21", regs_[MIPS_REG_R21]); | 
 | 96 |   fn("r22", regs_[MIPS_REG_R22]); | 
 | 97 |   fn("r23", regs_[MIPS_REG_R23]); | 
 | 98 |   fn("r24", regs_[MIPS_REG_R24]); | 
 | 99 |   fn("r25", regs_[MIPS_REG_R25]); | 
 | 100 |   fn("r26", regs_[MIPS_REG_R26]); | 
 | 101 |   fn("r27", regs_[MIPS_REG_R27]); | 
 | 102 |   fn("r28", regs_[MIPS_REG_R28]); | 
 | 103 |   fn("sp", regs_[MIPS_REG_SP]); | 
 | 104 |   fn("r30", regs_[MIPS_REG_R30]); | 
 | 105 |   fn("ra", regs_[MIPS_REG_RA]); | 
 | 106 |   fn("pc", regs_[MIPS_REG_PC]); | 
 | 107 | } | 
 | 108 |  | 
 | 109 | Regs* RegsMips::Read(void* remote_data) { | 
 | 110 |   mips_user_regs* user = reinterpret_cast<mips_user_regs*>(remote_data); | 
 | 111 |   RegsMips* regs = new RegsMips(); | 
 | 112 |   uint32_t* reg_data = reinterpret_cast<uint32_t*>(regs->RawData()); | 
 | 113 |  | 
 | 114 |   memcpy(regs->RawData(), &user->regs[MIPS32_EF_R0], (MIPS_REG_R31 + 1) * sizeof(uint32_t)); | 
 | 115 |  | 
 | 116 |   reg_data[MIPS_REG_PC] = user->regs[MIPS32_EF_CP0_EPC]; | 
| Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame] | 117 |   return regs; | 
 | 118 | } | 
 | 119 |  | 
 | 120 | Regs* RegsMips::CreateFromUcontext(void* ucontext) { | 
 | 121 |   mips_ucontext_t* mips_ucontext = reinterpret_cast<mips_ucontext_t*>(ucontext); | 
 | 122 |  | 
 | 123 |   RegsMips* regs = new RegsMips(); | 
 | 124 |   // Copy 64 bit sc_regs over to 32 bit regs | 
 | 125 |   for (int i = 0; i < 32; i++) { | 
 | 126 |       (*regs)[MIPS_REG_R0 + i] = mips_ucontext->uc_mcontext.sc_regs[i]; | 
 | 127 |   } | 
 | 128 |   (*regs)[MIPS_REG_PC] = mips_ucontext->uc_mcontext.sc_pc; | 
| Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame] | 129 |   return regs; | 
 | 130 | } | 
 | 131 |  | 
 | 132 | bool RegsMips::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) { | 
 | 133 |   uint64_t data; | 
 | 134 |   uint64_t offset = 0; | 
 | 135 |   Memory* elf_memory = elf->memory(); | 
 | 136 |   // Read from elf memory since it is usually more expensive to read from | 
 | 137 |   // process memory. | 
| Christopher Ferris | cadacdf | 2019-01-15 19:10:49 -0800 | [diff] [blame] | 138 |   if (!elf_memory->ReadFully(rel_pc, &data, sizeof(data))) { | 
| Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame] | 139 |     return false; | 
 | 140 |   } | 
 | 141 |  | 
 | 142 |   // Look for the kernel sigreturn functions. | 
 | 143 |   // __vdso_rt_sigreturn: | 
 | 144 |   // 0x24021061     li  v0, 0x1061 | 
 | 145 |   // 0x0000000c     syscall | 
 | 146 |   // __vdso_sigreturn: | 
 | 147 |   // 0x24021017     li  v0, 0x1017 | 
 | 148 |   // 0x0000000c     syscall | 
 | 149 |   if (data == 0x0000000c24021061ULL) { | 
 | 150 |     // vdso_rt_sigreturn => read rt_sigframe | 
 | 151 |     // offset = siginfo offset + sizeof(siginfo) + uc_mcontext offset + sc_pc offset | 
 | 152 |     offset = 24 + 128 + 24 + 8; | 
 | 153 |   } else if (data == 0x0000000c24021017LL) { | 
 | 154 |     // vdso_sigreturn => read sigframe | 
 | 155 |     // offset = sigcontext offset + sc_pc offset | 
 | 156 |     offset = 24 + 8; | 
 | 157 |   } else { | 
 | 158 |     return false; | 
 | 159 |   } | 
 | 160 |  | 
 | 161 |   // read sc_pc and sc_regs[32] from stack | 
 | 162 |   uint64_t values[MIPS_REG_LAST]; | 
| Christopher Ferris | cadacdf | 2019-01-15 19:10:49 -0800 | [diff] [blame] | 163 |   if (!process_memory->ReadFully(regs_[MIPS_REG_SP] + offset, values, sizeof(values))) { | 
| Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame] | 164 |     return false; | 
 | 165 |   } | 
 | 166 |  | 
 | 167 |   // Copy 64 bit sc_pc over to 32 bit regs_[MIPS_REG_PC] | 
 | 168 |   regs_[MIPS_REG_PC] = values[0]; | 
 | 169 |  | 
 | 170 |   // Copy 64 bit sc_regs over to 32 bit regs | 
 | 171 |   for (int i = 0; i < 32; i++) { | 
 | 172 |       regs_[MIPS_REG_R0 + i] = values[1 + i]; | 
 | 173 |   } | 
| Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame] | 174 |   return true; | 
 | 175 | } | 
 | 176 |  | 
| Josh Gao | 2f37a15 | 2018-04-20 11:51:14 -0700 | [diff] [blame] | 177 | Regs* RegsMips::Clone() { | 
 | 178 |   return new RegsMips(*this); | 
 | 179 | } | 
 | 180 |  | 
| Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame] | 181 | }  // namespace unwindstack |