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