blob: 5b7431a57ce9cccc94c46f2e0f2abab43d71e443 [file] [log] [blame]
Christopher Ferrisd06001d2017-11-30 18:56:01 -08001/*
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 Mayer3f1f2e02018-10-23 15:56:28 +010018#include <string.h>
Christopher Ferrisd06001d2017-11-30 18:56:01 -080019
20#include <functional>
21
22#include <unwindstack/Elf.h>
Christopher Ferris53914162018-02-08 19:27:47 -080023#include <unwindstack/MachineArm64.h>
Christopher Ferrisd06001d2017-11-30 18:56:01 -080024#include <unwindstack/MapInfo.h>
25#include <unwindstack/Memory.h>
26#include <unwindstack/RegsArm64.h>
Christopher Ferris53914162018-02-08 19:27:47 -080027#include <unwindstack/UcontextArm64.h>
28#include <unwindstack/UserArm64.h>
Christopher Ferrisd06001d2017-11-30 18:56:01 -080029
30namespace unwindstack {
31
32RegsArm64::RegsArm64()
Yabin Cui11e96fe2018-03-14 18:16:22 -070033 : RegsImpl<uint64_t>(ARM64_REG_LAST, Location(LOCATION_REGISTER, ARM64_REG_LR)) {}
Christopher Ferrisd06001d2017-11-30 18:56:01 -080034
35ArchEnum RegsArm64::Arch() {
36 return ARCH_ARM64;
37}
38
Yabin Cui11e96fe2018-03-14 18:16:22 -070039uint64_t RegsArm64::pc() {
40 return regs_[ARM64_REG_PC];
41}
42
43uint64_t RegsArm64::sp() {
44 return regs_[ARM64_REG_SP];
45}
46
47void RegsArm64::set_pc(uint64_t pc) {
48 regs_[ARM64_REG_PC] = pc;
49}
50
51void RegsArm64::set_sp(uint64_t sp) {
52 regs_[ARM64_REG_SP] = sp;
53}
54
Christopher Ferrisd06001d2017-11-30 18:56:01 -080055bool RegsArm64::SetPcFromReturnAddress(Memory*) {
Yabin Cui11e96fe2018-03-14 18:16:22 -070056 uint64_t lr = regs_[ARM64_REG_LR];
57 if (regs_[ARM64_REG_PC] == lr) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -080058 return false;
59 }
60
Yabin Cui11e96fe2018-03-14 18:16:22 -070061 regs_[ARM64_REG_PC] = lr;
Christopher Ferrisd06001d2017-11-30 18:56:01 -080062 return true;
63}
64
65void RegsArm64::IterateRegisters(std::function<void(const char*, uint64_t)> fn) {
66 fn("x0", regs_[ARM64_REG_R0]);
67 fn("x1", regs_[ARM64_REG_R1]);
68 fn("x2", regs_[ARM64_REG_R2]);
69 fn("x3", regs_[ARM64_REG_R3]);
70 fn("x4", regs_[ARM64_REG_R4]);
71 fn("x5", regs_[ARM64_REG_R5]);
72 fn("x6", regs_[ARM64_REG_R6]);
73 fn("x7", regs_[ARM64_REG_R7]);
74 fn("x8", regs_[ARM64_REG_R8]);
75 fn("x9", regs_[ARM64_REG_R9]);
76 fn("x10", regs_[ARM64_REG_R10]);
77 fn("x11", regs_[ARM64_REG_R11]);
78 fn("x12", regs_[ARM64_REG_R12]);
79 fn("x13", regs_[ARM64_REG_R13]);
80 fn("x14", regs_[ARM64_REG_R14]);
81 fn("x15", regs_[ARM64_REG_R15]);
82 fn("x16", regs_[ARM64_REG_R16]);
83 fn("x17", regs_[ARM64_REG_R17]);
84 fn("x18", regs_[ARM64_REG_R18]);
85 fn("x19", regs_[ARM64_REG_R19]);
86 fn("x20", regs_[ARM64_REG_R20]);
87 fn("x21", regs_[ARM64_REG_R21]);
88 fn("x22", regs_[ARM64_REG_R22]);
89 fn("x23", regs_[ARM64_REG_R23]);
90 fn("x24", regs_[ARM64_REG_R24]);
91 fn("x25", regs_[ARM64_REG_R25]);
92 fn("x26", regs_[ARM64_REG_R26]);
93 fn("x27", regs_[ARM64_REG_R27]);
94 fn("x28", regs_[ARM64_REG_R28]);
95 fn("x29", regs_[ARM64_REG_R29]);
Christopher Ferrisd06001d2017-11-30 18:56:01 -080096 fn("lr", regs_[ARM64_REG_LR]);
Ryan Savitski92237ba2020-01-24 19:34:13 +000097 fn("sp", regs_[ARM64_REG_SP]);
Christopher Ferrisd06001d2017-11-30 18:56:01 -080098 fn("pc", regs_[ARM64_REG_PC]);
Peter Collingbournebb2f9412019-11-18 12:36:50 -080099 fn("pst", regs_[ARM64_REG_PSTATE]);
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800100}
101
102Regs* RegsArm64::Read(void* remote_data) {
103 arm64_user_regs* user = reinterpret_cast<arm64_user_regs*>(remote_data);
104
105 RegsArm64* regs = new RegsArm64();
Ryan Savitski92237ba2020-01-24 19:34:13 +0000106 memcpy(regs->RawData(), &user->regs[0], (ARM64_REG_R30 + 1) * sizeof(uint64_t));
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800107 uint64_t* reg_data = reinterpret_cast<uint64_t*>(regs->RawData());
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800108 reg_data[ARM64_REG_SP] = user->sp;
Ryan Savitski92237ba2020-01-24 19:34:13 +0000109 reg_data[ARM64_REG_PC] = user->pc;
Peter Collingbournebb2f9412019-11-18 12:36:50 -0800110 reg_data[ARM64_REG_PSTATE] = user->pstate;
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800111 return regs;
112}
113
114Regs* RegsArm64::CreateFromUcontext(void* ucontext) {
115 arm64_ucontext_t* arm64_ucontext = reinterpret_cast<arm64_ucontext_t*>(ucontext);
116
117 RegsArm64* regs = new RegsArm64();
118 memcpy(regs->RawData(), &arm64_ucontext->uc_mcontext.regs[0], ARM64_REG_LAST * sizeof(uint64_t));
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800119 return regs;
120}
121
Christopher Ferrisf0c82e72019-12-04 13:37:11 -0800122bool RegsArm64::StepIfSignalHandler(uint64_t elf_offset, Elf* elf, Memory* process_memory) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800123 uint64_t data;
124 Memory* elf_memory = elf->memory();
125 // Read from elf memory since it is usually more expensive to read from
126 // process memory.
Christopher Ferrisf0c82e72019-12-04 13:37:11 -0800127 if (!elf_memory->ReadFully(elf_offset, &data, sizeof(data))) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800128 return false;
129 }
130
131 // Look for the kernel sigreturn function.
132 // __kernel_rt_sigreturn:
133 // 0xd2801168 mov x8, #0x8b
134 // 0xd4000001 svc #0x0
135 if (data != 0xd4000001d2801168ULL) {
136 return false;
137 }
138
139 // SP + sizeof(siginfo_t) + uc_mcontext offset + X0 offset.
Yabin Cui11e96fe2018-03-14 18:16:22 -0700140 if (!process_memory->ReadFully(regs_[ARM64_REG_SP] + 0x80 + 0xb0 + 0x08, regs_.data(),
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800141 sizeof(uint64_t) * ARM64_REG_LAST)) {
142 return false;
143 }
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800144 return true;
145}
146
Josh Gao2f37a152018-04-20 11:51:14 -0700147Regs* RegsArm64::Clone() {
148 return new RegsArm64(*this);
149}
150
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800151} // namespace unwindstack