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 #include <string.h>
19
20 #include <functional>
21
22 #if defined(__BIONIC__)
23 #include <bionic/pac.h>
24 #endif
25
26 #include <unwindstack/Elf.h>
27 #include <unwindstack/MachineArm64.h>
28 #include <unwindstack/MapInfo.h>
29 #include <unwindstack/Memory.h>
30 #include <unwindstack/RegsArm64.h>
31 #include <unwindstack/UcontextArm64.h>
32 #include <unwindstack/UserArm64.h>
33
34 namespace unwindstack {
35
RegsArm64()36 RegsArm64::RegsArm64()
37 : RegsImpl<uint64_t>(ARM64_REG_LAST, Location(LOCATION_REGISTER, ARM64_REG_LR)) {
38 ResetPseudoRegisters();
39 pac_mask_ = 0;
40 }
41
Arch()42 ArchEnum RegsArm64::Arch() {
43 return ARCH_ARM64;
44 }
45
pc()46 uint64_t RegsArm64::pc() {
47 return regs_[ARM64_REG_PC];
48 }
49
sp()50 uint64_t RegsArm64::sp() {
51 return regs_[ARM64_REG_SP];
52 }
53
strip_pac(uint64_t pc,uint64_t mask)54 static uint64_t strip_pac(uint64_t pc, uint64_t mask) {
55 // If the target is aarch64 then the return address may have been
56 // signed using the Armv8.3-A Pointer Authentication extension. The
57 // original return address can be restored by stripping out the
58 // authentication code using a mask or xpaclri. xpaclri is a NOP on
59 // pre-Armv8.3-A architectures.
60 if (mask) {
61 pc &= ~mask;
62 } else {
63 #if defined(__BIONIC__)
64 pc = __bionic_clear_pac_bits(pc);
65 #endif
66 }
67 return pc;
68 }
69
set_pc(uint64_t pc)70 void RegsArm64::set_pc(uint64_t pc) {
71 if ((0 != pc) && IsRASigned()) {
72 pc = strip_pac(pc, pac_mask_);
73 }
74 regs_[ARM64_REG_PC] = pc;
75 }
76
set_sp(uint64_t sp)77 void RegsArm64::set_sp(uint64_t sp) {
78 regs_[ARM64_REG_SP] = sp;
79 }
80
fallback_pc()81 void RegsArm64::fallback_pc() {
82 // As a last resort, try stripping the PC of the pointer
83 // authentication code.
84 regs_[ARM64_REG_PC] = strip_pac(regs_[ARM64_REG_PC], pac_mask_);
85 }
86
SetPcFromReturnAddress(Memory *)87 bool RegsArm64::SetPcFromReturnAddress(Memory*) {
88 uint64_t lr = regs_[ARM64_REG_LR];
89 if (regs_[ARM64_REG_PC] == lr) {
90 return false;
91 }
92
93 regs_[ARM64_REG_PC] = lr;
94 return true;
95 }
96
IterateRegisters(std::function<void (const char *,uint64_t)> fn)97 void RegsArm64::IterateRegisters(std::function<void(const char*, uint64_t)> fn) {
98 fn("x0", regs_[ARM64_REG_R0]);
99 fn("x1", regs_[ARM64_REG_R1]);
100 fn("x2", regs_[ARM64_REG_R2]);
101 fn("x3", regs_[ARM64_REG_R3]);
102 fn("x4", regs_[ARM64_REG_R4]);
103 fn("x5", regs_[ARM64_REG_R5]);
104 fn("x6", regs_[ARM64_REG_R6]);
105 fn("x7", regs_[ARM64_REG_R7]);
106 fn("x8", regs_[ARM64_REG_R8]);
107 fn("x9", regs_[ARM64_REG_R9]);
108 fn("x10", regs_[ARM64_REG_R10]);
109 fn("x11", regs_[ARM64_REG_R11]);
110 fn("x12", regs_[ARM64_REG_R12]);
111 fn("x13", regs_[ARM64_REG_R13]);
112 fn("x14", regs_[ARM64_REG_R14]);
113 fn("x15", regs_[ARM64_REG_R15]);
114 fn("x16", regs_[ARM64_REG_R16]);
115 fn("x17", regs_[ARM64_REG_R17]);
116 fn("x18", regs_[ARM64_REG_R18]);
117 fn("x19", regs_[ARM64_REG_R19]);
118 fn("x20", regs_[ARM64_REG_R20]);
119 fn("x21", regs_[ARM64_REG_R21]);
120 fn("x22", regs_[ARM64_REG_R22]);
121 fn("x23", regs_[ARM64_REG_R23]);
122 fn("x24", regs_[ARM64_REG_R24]);
123 fn("x25", regs_[ARM64_REG_R25]);
124 fn("x26", regs_[ARM64_REG_R26]);
125 fn("x27", regs_[ARM64_REG_R27]);
126 fn("x28", regs_[ARM64_REG_R28]);
127 fn("x29", regs_[ARM64_REG_R29]);
128 fn("lr", regs_[ARM64_REG_LR]);
129 fn("sp", regs_[ARM64_REG_SP]);
130 fn("pc", regs_[ARM64_REG_PC]);
131 fn("pst", regs_[ARM64_REG_PSTATE]);
132 }
133
Read(const void * remote_data)134 Regs* RegsArm64::Read(const void* remote_data) {
135 const arm64_user_regs* user = reinterpret_cast<const arm64_user_regs*>(remote_data);
136
137 RegsArm64* regs = new RegsArm64();
138 memcpy(regs->RawData(), &user->regs[0], (ARM64_REG_R30 + 1) * sizeof(uint64_t));
139 uint64_t* reg_data = reinterpret_cast<uint64_t*>(regs->RawData());
140 reg_data[ARM64_REG_SP] = user->sp;
141 reg_data[ARM64_REG_PC] = user->pc;
142 reg_data[ARM64_REG_PSTATE] = user->pstate;
143 return regs;
144 }
145
CreateFromUcontext(void * ucontext)146 Regs* RegsArm64::CreateFromUcontext(void* ucontext) {
147 arm64_ucontext_t* arm64_ucontext = reinterpret_cast<arm64_ucontext_t*>(ucontext);
148
149 RegsArm64* regs = new RegsArm64();
150 memcpy(regs->RawData(), &arm64_ucontext->uc_mcontext.regs[0], ARM64_REG_LAST * sizeof(uint64_t));
151 return regs;
152 }
153
StepIfSignalHandler(uint64_t elf_offset,Elf * elf,Memory * process_memory)154 bool RegsArm64::StepIfSignalHandler(uint64_t elf_offset, Elf* elf, Memory* process_memory) {
155 // Read from elf memory since it is usually more expensive to read from
156 // process memory.
157 uint64_t data;
158 if (!elf->memory()->ReadFully(elf_offset, &data, sizeof(data))) {
159 return false;
160 }
161
162 // Look for the kernel sigreturn function.
163 // __kernel_rt_sigreturn:
164 // 0xd2801168 mov x8, #0x8b
165 // 0xd4000001 svc #0x0
166 if (data != 0xd4000001d2801168ULL) {
167 return false;
168 }
169
170 // SP + sizeof(siginfo_t) + uc_mcontext offset + X0 offset.
171 if (!process_memory->ReadFully(regs_[ARM64_REG_SP] + 0x80 + 0xb0 + 0x08, regs_.data(),
172 sizeof(uint64_t) * ARM64_REG_LAST)) {
173 return false;
174 }
175 return true;
176 }
177
ResetPseudoRegisters(void)178 void RegsArm64::ResetPseudoRegisters(void) {
179 // DWARF for AArch64 says RA_SIGN_STATE should be initialized to 0.
180 this->SetPseudoRegister(Arm64Reg::ARM64_PREG_RA_SIGN_STATE, 0);
181 }
182
SetPseudoRegister(uint16_t id,uint64_t value)183 bool RegsArm64::SetPseudoRegister(uint16_t id, uint64_t value) {
184 if ((id >= Arm64Reg::ARM64_PREG_FIRST) && (id < Arm64Reg::ARM64_PREG_LAST)) {
185 pseudo_regs_[id - Arm64Reg::ARM64_PREG_FIRST] = value;
186 return true;
187 }
188 return false;
189 }
190
GetPseudoRegister(uint16_t id,uint64_t * value)191 bool RegsArm64::GetPseudoRegister(uint16_t id, uint64_t* value) {
192 if ((id >= Arm64Reg::ARM64_PREG_FIRST) && (id < Arm64Reg::ARM64_PREG_LAST)) {
193 *value = pseudo_regs_[id - Arm64Reg::ARM64_PREG_FIRST];
194 return true;
195 }
196 return false;
197 }
198
IsRASigned()199 bool RegsArm64::IsRASigned() {
200 uint64_t value;
201 auto result = this->GetPseudoRegister(Arm64Reg::ARM64_PREG_RA_SIGN_STATE, &value);
202 return (result && (value != 0));
203 }
204
SetPACMask(uint64_t mask)205 void RegsArm64::SetPACMask(uint64_t mask) {
206 pac_mask_ = mask;
207 }
208
Clone()209 Regs* RegsArm64::Clone() {
210 return new RegsArm64(*this);
211 }
212
213 } // namespace unwindstack
214