Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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> |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [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/MachineArm.h> |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 24 | #include <unwindstack/MapInfo.h> |
| 25 | #include <unwindstack/Memory.h> |
| 26 | #include <unwindstack/RegsArm.h> |
Christopher Ferris | 5391416 | 2018-02-08 19:27:47 -0800 | [diff] [blame] | 27 | #include <unwindstack/UcontextArm.h> |
| 28 | #include <unwindstack/UserArm.h> |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 29 | |
| 30 | namespace unwindstack { |
| 31 | |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 32 | RegsArm::RegsArm() : RegsImpl<uint32_t>(ARM_REG_LAST, Location(LOCATION_REGISTER, ARM_REG_LR)) {} |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 33 | |
| 34 | ArchEnum RegsArm::Arch() { |
| 35 | return ARCH_ARM; |
| 36 | } |
| 37 | |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 38 | uint64_t RegsArm::pc() { |
| 39 | return regs_[ARM_REG_PC]; |
| 40 | } |
| 41 | |
| 42 | uint64_t RegsArm::sp() { |
| 43 | return regs_[ARM_REG_SP]; |
| 44 | } |
| 45 | |
| 46 | void RegsArm::set_pc(uint64_t pc) { |
| 47 | regs_[ARM_REG_PC] = pc; |
| 48 | } |
| 49 | |
| 50 | void RegsArm::set_sp(uint64_t sp) { |
| 51 | regs_[ARM_REG_SP] = sp; |
| 52 | } |
| 53 | |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 54 | bool RegsArm::SetPcFromReturnAddress(Memory*) { |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 55 | uint32_t lr = regs_[ARM_REG_LR]; |
| 56 | if (regs_[ARM_REG_PC] == lr) { |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 57 | return false; |
| 58 | } |
| 59 | |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 60 | regs_[ARM_REG_PC] = lr; |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 61 | return true; |
| 62 | } |
| 63 | |
| 64 | void RegsArm::IterateRegisters(std::function<void(const char*, uint64_t)> fn) { |
| 65 | fn("r0", regs_[ARM_REG_R0]); |
| 66 | fn("r1", regs_[ARM_REG_R1]); |
| 67 | fn("r2", regs_[ARM_REG_R2]); |
| 68 | fn("r3", regs_[ARM_REG_R3]); |
| 69 | fn("r4", regs_[ARM_REG_R4]); |
| 70 | fn("r5", regs_[ARM_REG_R5]); |
| 71 | fn("r6", regs_[ARM_REG_R6]); |
| 72 | fn("r7", regs_[ARM_REG_R7]); |
| 73 | fn("r8", regs_[ARM_REG_R8]); |
| 74 | fn("r9", regs_[ARM_REG_R9]); |
| 75 | fn("r10", regs_[ARM_REG_R10]); |
| 76 | fn("r11", regs_[ARM_REG_R11]); |
| 77 | fn("ip", regs_[ARM_REG_R12]); |
| 78 | fn("sp", regs_[ARM_REG_SP]); |
| 79 | fn("lr", regs_[ARM_REG_LR]); |
| 80 | fn("pc", regs_[ARM_REG_PC]); |
| 81 | } |
| 82 | |
| 83 | Regs* RegsArm::Read(void* remote_data) { |
| 84 | arm_user_regs* user = reinterpret_cast<arm_user_regs*>(remote_data); |
| 85 | |
| 86 | RegsArm* regs = new RegsArm(); |
| 87 | memcpy(regs->RawData(), &user->regs[0], ARM_REG_LAST * sizeof(uint32_t)); |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 88 | return regs; |
| 89 | } |
| 90 | |
| 91 | Regs* RegsArm::CreateFromUcontext(void* ucontext) { |
| 92 | arm_ucontext_t* arm_ucontext = reinterpret_cast<arm_ucontext_t*>(ucontext); |
| 93 | |
| 94 | RegsArm* regs = new RegsArm(); |
| 95 | memcpy(regs->RawData(), &arm_ucontext->uc_mcontext.regs[0], ARM_REG_LAST * sizeof(uint32_t)); |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 96 | return regs; |
| 97 | } |
| 98 | |
Christopher Ferris | f0c82e7 | 2019-12-04 13:37:11 -0800 | [diff] [blame] | 99 | bool RegsArm::StepIfSignalHandler(uint64_t elf_offset, Elf* elf, Memory* process_memory) { |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 100 | uint32_t data; |
| 101 | Memory* elf_memory = elf->memory(); |
| 102 | // Read from elf memory since it is usually more expensive to read from |
| 103 | // process memory. |
Christopher Ferris | f0c82e7 | 2019-12-04 13:37:11 -0800 | [diff] [blame] | 104 | if (!elf_memory->ReadFully(elf_offset, &data, sizeof(data))) { |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 105 | return false; |
| 106 | } |
| 107 | |
| 108 | uint64_t offset = 0; |
| 109 | if (data == 0xe3a07077 || data == 0xef900077 || data == 0xdf002777) { |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 110 | uint64_t sp = regs_[ARM_REG_SP]; |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 111 | // non-RT sigreturn call. |
| 112 | // __restore: |
| 113 | // |
| 114 | // Form 1 (arm): |
| 115 | // 0x77 0x70 mov r7, #0x77 |
| 116 | // 0xa0 0xe3 svc 0x00000000 |
| 117 | // |
| 118 | // Form 2 (arm): |
| 119 | // 0x77 0x00 0x90 0xef svc 0x00900077 |
| 120 | // |
| 121 | // Form 3 (thumb): |
| 122 | // 0x77 0x27 movs r7, #77 |
| 123 | // 0x00 0xdf svc 0 |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 124 | if (!process_memory->ReadFully(sp, &data, sizeof(data))) { |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 125 | return false; |
| 126 | } |
| 127 | if (data == 0x5ac3c35a) { |
| 128 | // SP + uc_mcontext offset + r0 offset. |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 129 | offset = sp + 0x14 + 0xc; |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 130 | } else { |
| 131 | // SP + r0 offset |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 132 | offset = sp + 0xc; |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 133 | } |
| 134 | } else if (data == 0xe3a070ad || data == 0xef9000ad || data == 0xdf0027ad) { |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 135 | uint64_t sp = regs_[ARM_REG_SP]; |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 136 | // RT sigreturn call. |
| 137 | // __restore_rt: |
| 138 | // |
| 139 | // Form 1 (arm): |
| 140 | // 0xad 0x70 mov r7, #0xad |
| 141 | // 0xa0 0xe3 svc 0x00000000 |
| 142 | // |
| 143 | // Form 2 (arm): |
| 144 | // 0xad 0x00 0x90 0xef svc 0x009000ad |
| 145 | // |
| 146 | // Form 3 (thumb): |
| 147 | // 0xad 0x27 movs r7, #ad |
| 148 | // 0x00 0xdf svc 0 |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 149 | if (!process_memory->ReadFully(sp, &data, sizeof(data))) { |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 150 | return false; |
| 151 | } |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 152 | if (data == sp + 8) { |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 153 | // SP + 8 + sizeof(siginfo_t) + uc_mcontext_offset + r0 offset |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 154 | offset = sp + 8 + 0x80 + 0x14 + 0xc; |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 155 | } else { |
| 156 | // SP + sizeof(siginfo_t) + uc_mcontext_offset + r0 offset |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 157 | offset = sp + 0x80 + 0x14 + 0xc; |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | if (offset == 0) { |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | if (!process_memory->ReadFully(offset, regs_.data(), sizeof(uint32_t) * ARM_REG_LAST)) { |
| 165 | return false; |
| 166 | } |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 167 | return true; |
| 168 | } |
| 169 | |
Josh Gao | 2f37a15 | 2018-04-20 11:51:14 -0700 | [diff] [blame] | 170 | Regs* RegsArm::Clone() { |
| 171 | return new RegsArm(*this); |
| 172 | } |
| 173 | |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 174 | } // namespace unwindstack |