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> |
| 18 | |
| 19 | #include <functional> |
| 20 | |
| 21 | #include <unwindstack/Elf.h> |
| 22 | #include <unwindstack/MapInfo.h> |
| 23 | #include <unwindstack/Memory.h> |
| 24 | #include <unwindstack/RegsArm64.h> |
| 25 | |
| 26 | #include "MachineArm64.h" |
| 27 | #include "UcontextArm64.h" |
| 28 | #include "UserArm64.h" |
| 29 | |
| 30 | namespace unwindstack { |
| 31 | |
| 32 | RegsArm64::RegsArm64() |
| 33 | : RegsImpl<uint64_t>(ARM64_REG_LAST, ARM64_REG_SP, Location(LOCATION_REGISTER, ARM64_REG_LR)) {} |
| 34 | |
| 35 | ArchEnum RegsArm64::Arch() { |
| 36 | return ARCH_ARM64; |
| 37 | } |
| 38 | |
| 39 | uint64_t RegsArm64::GetAdjustedPc(uint64_t rel_pc, Elf* elf) { |
| 40 | if (!elf->valid()) { |
| 41 | return rel_pc; |
| 42 | } |
| 43 | |
| 44 | if (rel_pc < 4) { |
| 45 | return rel_pc; |
| 46 | } |
| 47 | return rel_pc - 4; |
| 48 | } |
| 49 | |
| 50 | void RegsArm64::SetFromRaw() { |
| 51 | set_pc(regs_[ARM64_REG_PC]); |
| 52 | set_sp(regs_[ARM64_REG_SP]); |
| 53 | } |
| 54 | |
| 55 | bool RegsArm64::SetPcFromReturnAddress(Memory*) { |
| 56 | if (pc() == regs_[ARM64_REG_LR]) { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | set_pc(regs_[ARM64_REG_LR]); |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | void RegsArm64::IterateRegisters(std::function<void(const char*, uint64_t)> fn) { |
| 65 | fn("x0", regs_[ARM64_REG_R0]); |
| 66 | fn("x1", regs_[ARM64_REG_R1]); |
| 67 | fn("x2", regs_[ARM64_REG_R2]); |
| 68 | fn("x3", regs_[ARM64_REG_R3]); |
| 69 | fn("x4", regs_[ARM64_REG_R4]); |
| 70 | fn("x5", regs_[ARM64_REG_R5]); |
| 71 | fn("x6", regs_[ARM64_REG_R6]); |
| 72 | fn("x7", regs_[ARM64_REG_R7]); |
| 73 | fn("x8", regs_[ARM64_REG_R8]); |
| 74 | fn("x9", regs_[ARM64_REG_R9]); |
| 75 | fn("x10", regs_[ARM64_REG_R10]); |
| 76 | fn("x11", regs_[ARM64_REG_R11]); |
| 77 | fn("x12", regs_[ARM64_REG_R12]); |
| 78 | fn("x13", regs_[ARM64_REG_R13]); |
| 79 | fn("x14", regs_[ARM64_REG_R14]); |
| 80 | fn("x15", regs_[ARM64_REG_R15]); |
| 81 | fn("x16", regs_[ARM64_REG_R16]); |
| 82 | fn("x17", regs_[ARM64_REG_R17]); |
| 83 | fn("x18", regs_[ARM64_REG_R18]); |
| 84 | fn("x19", regs_[ARM64_REG_R19]); |
| 85 | fn("x20", regs_[ARM64_REG_R20]); |
| 86 | fn("x21", regs_[ARM64_REG_R21]); |
| 87 | fn("x22", regs_[ARM64_REG_R22]); |
| 88 | fn("x23", regs_[ARM64_REG_R23]); |
| 89 | fn("x24", regs_[ARM64_REG_R24]); |
| 90 | fn("x25", regs_[ARM64_REG_R25]); |
| 91 | fn("x26", regs_[ARM64_REG_R26]); |
| 92 | fn("x27", regs_[ARM64_REG_R27]); |
| 93 | fn("x28", regs_[ARM64_REG_R28]); |
| 94 | fn("x29", regs_[ARM64_REG_R29]); |
| 95 | fn("sp", regs_[ARM64_REG_SP]); |
| 96 | fn("lr", regs_[ARM64_REG_LR]); |
| 97 | fn("pc", regs_[ARM64_REG_PC]); |
| 98 | } |
| 99 | |
| 100 | Regs* RegsArm64::Read(void* remote_data) { |
| 101 | arm64_user_regs* user = reinterpret_cast<arm64_user_regs*>(remote_data); |
| 102 | |
| 103 | RegsArm64* regs = new RegsArm64(); |
| 104 | memcpy(regs->RawData(), &user->regs[0], (ARM64_REG_R31 + 1) * sizeof(uint64_t)); |
| 105 | uint64_t* reg_data = reinterpret_cast<uint64_t*>(regs->RawData()); |
| 106 | reg_data[ARM64_REG_PC] = user->pc; |
| 107 | reg_data[ARM64_REG_SP] = user->sp; |
| 108 | regs->SetFromRaw(); |
| 109 | return regs; |
| 110 | } |
| 111 | |
| 112 | Regs* RegsArm64::CreateFromUcontext(void* ucontext) { |
| 113 | arm64_ucontext_t* arm64_ucontext = reinterpret_cast<arm64_ucontext_t*>(ucontext); |
| 114 | |
| 115 | RegsArm64* regs = new RegsArm64(); |
| 116 | memcpy(regs->RawData(), &arm64_ucontext->uc_mcontext.regs[0], ARM64_REG_LAST * sizeof(uint64_t)); |
| 117 | regs->SetFromRaw(); |
| 118 | return regs; |
| 119 | } |
| 120 | |
| 121 | bool RegsArm64::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) { |
| 122 | uint64_t data; |
| 123 | Memory* elf_memory = elf->memory(); |
| 124 | // Read from elf memory since it is usually more expensive to read from |
| 125 | // process memory. |
| 126 | if (!elf_memory->ReadFully(rel_pc, &data, sizeof(data))) { |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | // Look for the kernel sigreturn function. |
| 131 | // __kernel_rt_sigreturn: |
| 132 | // 0xd2801168 mov x8, #0x8b |
| 133 | // 0xd4000001 svc #0x0 |
| 134 | if (data != 0xd4000001d2801168ULL) { |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | // SP + sizeof(siginfo_t) + uc_mcontext offset + X0 offset. |
| 139 | if (!process_memory->ReadFully(sp() + 0x80 + 0xb0 + 0x08, regs_.data(), |
| 140 | sizeof(uint64_t) * ARM64_REG_LAST)) { |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | SetFromRaw(); |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | } // namespace unwindstack |