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