blob: 92b6e22401e1e1b7d77b6475c4dbe8ddd83867e2 [file] [log] [blame]
Christopher Ferris3958f802017-02-01 15:44:40 -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
Christopher Ferris3958f802017-02-01 15:44:40 -080017#include <elf.h>
18#include <stdint.h>
19#include <sys/ptrace.h>
20#include <sys/uio.h>
21
22#include <vector>
23
Christopher Ferris94167032017-06-28 18:56:52 -070024#include "Check.h"
Christopher Ferris3958f802017-02-01 15:44:40 -080025#include "Elf.h"
26#include "ElfInterface.h"
27#include "Machine.h"
28#include "MapInfo.h"
29#include "Regs.h"
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070030#include "Ucontext.h"
Christopher Ferris3958f802017-02-01 15:44:40 -080031#include "User.h"
32
33template <typename AddressType>
Christopher Ferris7b8e4672017-06-01 17:55:25 -070034uint64_t RegsImpl<AddressType>::GetRelPc(Elf* elf, const MapInfo* map_info) {
Christopher Ferris3958f802017-02-01 15:44:40 -080035 uint64_t load_bias = 0;
36 if (elf->valid()) {
37 load_bias = elf->interface()->load_bias();
38 }
39
40 return pc_ - map_info->start + load_bias + map_info->elf_offset;
41}
42
43template <typename AddressType>
Christopher Ferris7b8e4672017-06-01 17:55:25 -070044bool RegsImpl<AddressType>::GetReturnAddressFromDefault(Memory* memory, uint64_t* value) {
Christopher Ferris3958f802017-02-01 15:44:40 -080045 switch (return_loc_.type) {
46 case LOCATION_REGISTER:
Christopher Ferris94167032017-06-28 18:56:52 -070047 CHECK(return_loc_.value < total_regs_);
Christopher Ferris3958f802017-02-01 15:44:40 -080048 *value = regs_[return_loc_.value];
49 return true;
50 case LOCATION_SP_OFFSET:
51 AddressType return_value;
52 if (!memory->Read(sp_ + return_loc_.value, &return_value, sizeof(return_value))) {
53 return false;
54 }
55 *value = return_value;
56 return true;
57 case LOCATION_UNKNOWN:
58 default:
59 return false;
60 }
61}
62
Christopher Ferris7b8e4672017-06-01 17:55:25 -070063RegsArm::RegsArm()
64 : RegsImpl<uint32_t>(ARM_REG_LAST, ARM_REG_SP, Location(LOCATION_REGISTER, ARM_REG_LR)) {}
Christopher Ferris3958f802017-02-01 15:44:40 -080065
66uint64_t RegsArm::GetAdjustedPc(uint64_t rel_pc, Elf* elf) {
67 if (!elf->valid()) {
68 return rel_pc;
69 }
70
71 uint64_t load_bias = elf->interface()->load_bias();
72 if (rel_pc < load_bias) {
73 return rel_pc;
74 }
75 uint64_t adjusted_rel_pc = rel_pc - load_bias;
76
77 if (adjusted_rel_pc < 5) {
78 return rel_pc;
79 }
80
81 if (adjusted_rel_pc & 1) {
82 // This is a thumb instruction, it could be 2 or 4 bytes.
83 uint32_t value;
84 if (rel_pc < 5 || !elf->memory()->Read(adjusted_rel_pc - 5, &value, sizeof(value)) ||
85 (value & 0xe000f000) != 0xe000f000) {
86 return rel_pc - 2;
87 }
88 }
89 return rel_pc - 4;
90}
91
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070092void RegsArm::SetFromRaw() {
93 set_pc(regs_[ARM_REG_PC]);
94 set_sp(regs_[ARM_REG_SP]);
95}
96
Christopher Ferris7b8e4672017-06-01 17:55:25 -070097RegsArm64::RegsArm64()
98 : RegsImpl<uint64_t>(ARM64_REG_LAST, ARM64_REG_SP, Location(LOCATION_REGISTER, ARM64_REG_LR)) {}
Christopher Ferris3958f802017-02-01 15:44:40 -080099
100uint64_t RegsArm64::GetAdjustedPc(uint64_t rel_pc, Elf* elf) {
101 if (!elf->valid()) {
102 return rel_pc;
103 }
104
105 if (rel_pc < 4) {
106 return rel_pc;
107 }
108 return rel_pc - 4;
109}
110
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700111void RegsArm64::SetFromRaw() {
112 set_pc(regs_[ARM64_REG_PC]);
113 set_sp(regs_[ARM64_REG_SP]);
114}
115
Christopher Ferris7b8e4672017-06-01 17:55:25 -0700116RegsX86::RegsX86()
117 : RegsImpl<uint32_t>(X86_REG_LAST, X86_REG_SP, Location(LOCATION_SP_OFFSET, -4)) {}
Christopher Ferris3958f802017-02-01 15:44:40 -0800118
119uint64_t RegsX86::GetAdjustedPc(uint64_t rel_pc, Elf* elf) {
120 if (!elf->valid()) {
121 return rel_pc;
122 }
123
124 if (rel_pc == 0) {
125 return 0;
126 }
127 return rel_pc - 1;
128}
129
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700130void RegsX86::SetFromRaw() {
131 set_pc(regs_[X86_REG_PC]);
132 set_sp(regs_[X86_REG_SP]);
133}
134
Christopher Ferris7b8e4672017-06-01 17:55:25 -0700135RegsX86_64::RegsX86_64()
136 : RegsImpl<uint64_t>(X86_64_REG_LAST, X86_64_REG_SP, Location(LOCATION_SP_OFFSET, -8)) {}
Christopher Ferris3958f802017-02-01 15:44:40 -0800137
138uint64_t RegsX86_64::GetAdjustedPc(uint64_t rel_pc, Elf* elf) {
139 if (!elf->valid()) {
140 return rel_pc;
141 }
142
143 if (rel_pc == 0) {
144 return 0;
145 }
146
147 return rel_pc - 1;
148}
149
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700150void RegsX86_64::SetFromRaw() {
151 set_pc(regs_[X86_64_REG_PC]);
152 set_sp(regs_[X86_64_REG_SP]);
153}
154
Christopher Ferris3958f802017-02-01 15:44:40 -0800155static Regs* ReadArm(void* remote_data) {
156 arm_user_regs* user = reinterpret_cast<arm_user_regs*>(remote_data);
157
158 RegsArm* regs = new RegsArm();
159 memcpy(regs->RawData(), &user->regs[0], ARM_REG_LAST * sizeof(uint32_t));
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700160 regs->SetFromRaw();
Christopher Ferris3958f802017-02-01 15:44:40 -0800161 return regs;
162}
163
164static Regs* ReadArm64(void* remote_data) {
165 arm64_user_regs* user = reinterpret_cast<arm64_user_regs*>(remote_data);
166
167 RegsArm64* regs = new RegsArm64();
168 memcpy(regs->RawData(), &user->regs[0], (ARM64_REG_R31 + 1) * sizeof(uint64_t));
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700169 uint64_t* reg_data = reinterpret_cast<uint64_t*>(regs->RawData());
170 reg_data[ARM64_REG_PC] = user->pc;
171 reg_data[ARM64_REG_SP] = user->sp;
172 regs->SetFromRaw();
Christopher Ferris3958f802017-02-01 15:44:40 -0800173 return regs;
174}
175
176static Regs* ReadX86(void* remote_data) {
177 x86_user_regs* user = reinterpret_cast<x86_user_regs*>(remote_data);
178
179 RegsX86* regs = new RegsX86();
180 (*regs)[X86_REG_EAX] = user->eax;
181 (*regs)[X86_REG_EBX] = user->ebx;
182 (*regs)[X86_REG_ECX] = user->ecx;
183 (*regs)[X86_REG_EDX] = user->edx;
184 (*regs)[X86_REG_EBP] = user->ebp;
185 (*regs)[X86_REG_EDI] = user->edi;
186 (*regs)[X86_REG_ESI] = user->esi;
187 (*regs)[X86_REG_ESP] = user->esp;
188 (*regs)[X86_REG_EIP] = user->eip;
189
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700190 regs->SetFromRaw();
Christopher Ferris3958f802017-02-01 15:44:40 -0800191 return regs;
192}
193
194static Regs* ReadX86_64(void* remote_data) {
195 x86_64_user_regs* user = reinterpret_cast<x86_64_user_regs*>(remote_data);
196
197 RegsX86_64* regs = new RegsX86_64();
198 (*regs)[X86_64_REG_RAX] = user->rax;
199 (*regs)[X86_64_REG_RBX] = user->rbx;
200 (*regs)[X86_64_REG_RCX] = user->rcx;
201 (*regs)[X86_64_REG_RDX] = user->rdx;
202 (*regs)[X86_64_REG_R8] = user->r8;
203 (*regs)[X86_64_REG_R9] = user->r9;
204 (*regs)[X86_64_REG_R10] = user->r10;
205 (*regs)[X86_64_REG_R11] = user->r11;
206 (*regs)[X86_64_REG_R12] = user->r12;
207 (*regs)[X86_64_REG_R13] = user->r13;
208 (*regs)[X86_64_REG_R14] = user->r14;
209 (*regs)[X86_64_REG_R15] = user->r15;
210 (*regs)[X86_64_REG_RDI] = user->rdi;
211 (*regs)[X86_64_REG_RSI] = user->rsi;
212 (*regs)[X86_64_REG_RBP] = user->rbp;
213 (*regs)[X86_64_REG_RSP] = user->rsp;
214 (*regs)[X86_64_REG_RIP] = user->rip;
215
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700216 regs->SetFromRaw();
Christopher Ferris3958f802017-02-01 15:44:40 -0800217 return regs;
218}
219
220// This function assumes that reg_data is already aligned to a 64 bit value.
221// If not this could crash with an unaligned access.
222Regs* Regs::RemoteGet(pid_t pid, uint32_t* machine_type) {
223 // Make the buffer large enough to contain the largest registers type.
224 std::vector<uint64_t> buffer(MAX_USER_REGS_SIZE / sizeof(uint64_t));
225 struct iovec io;
226 io.iov_base = buffer.data();
227 io.iov_len = buffer.size() * sizeof(uint64_t);
228
229 if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, reinterpret_cast<void*>(&io)) == -1) {
230 return nullptr;
231 }
232
233 switch (io.iov_len) {
234 case sizeof(x86_user_regs):
235 *machine_type = EM_386;
236 return ReadX86(buffer.data());
237 case sizeof(x86_64_user_regs):
238 *machine_type = EM_X86_64;
239 return ReadX86_64(buffer.data());
240 case sizeof(arm_user_regs):
241 *machine_type = EM_ARM;
242 return ReadArm(buffer.data());
243 case sizeof(arm64_user_regs):
244 *machine_type = EM_AARCH64;
245 return ReadArm64(buffer.data());
246 }
247 return nullptr;
248}
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700249
250static Regs* CreateFromArmUcontext(void* ucontext) {
251 arm_ucontext_t* arm_ucontext = reinterpret_cast<arm_ucontext_t*>(ucontext);
252
253 RegsArm* regs = new RegsArm();
254 memcpy(regs->RawData(), &arm_ucontext->uc_mcontext.regs[0], ARM_REG_LAST * sizeof(uint32_t));
255 regs->SetFromRaw();
256 return regs;
257}
258
259static Regs* CreateFromArm64Ucontext(void* ucontext) {
260 arm64_ucontext_t* arm64_ucontext = reinterpret_cast<arm64_ucontext_t*>(ucontext);
261
262 RegsArm64* regs = new RegsArm64();
263 memcpy(regs->RawData(), &arm64_ucontext->uc_mcontext.regs[0], ARM64_REG_LAST * sizeof(uint64_t));
264 regs->SetFromRaw();
265 return regs;
266}
267
268static Regs* CreateFromX86Ucontext(void* ucontext) {
269 x86_ucontext_t* x86_ucontext = reinterpret_cast<x86_ucontext_t*>(ucontext);
270
271 RegsX86* regs = new RegsX86();
272 // Put the registers in the expected order.
273 (*regs)[X86_REG_GS] = x86_ucontext->uc_mcontext.gs;
274 (*regs)[X86_REG_FS] = x86_ucontext->uc_mcontext.fs;
275 (*regs)[X86_REG_ES] = x86_ucontext->uc_mcontext.es;
276 (*regs)[X86_REG_DS] = x86_ucontext->uc_mcontext.ds;
277 (*regs)[X86_REG_EDI] = x86_ucontext->uc_mcontext.edi;
278 (*regs)[X86_REG_ESI] = x86_ucontext->uc_mcontext.esi;
279 (*regs)[X86_REG_EBP] = x86_ucontext->uc_mcontext.ebp;
280 (*regs)[X86_REG_ESP] = x86_ucontext->uc_mcontext.esp;
281 (*regs)[X86_REG_EBX] = x86_ucontext->uc_mcontext.ebx;
282 (*regs)[X86_REG_EDX] = x86_ucontext->uc_mcontext.edx;
283 (*regs)[X86_REG_ECX] = x86_ucontext->uc_mcontext.ecx;
284 (*regs)[X86_REG_EAX] = x86_ucontext->uc_mcontext.eax;
285 (*regs)[X86_REG_EIP] = x86_ucontext->uc_mcontext.eip;
286 regs->SetFromRaw();
287 return regs;
288}
289
290static Regs* CreateFromX86_64Ucontext(void* ucontext) {
291 x86_64_ucontext_t* x86_64_ucontext = reinterpret_cast<x86_64_ucontext_t*>(ucontext);
292
293 RegsX86_64* regs = new RegsX86_64();
294 // Put the registers in the expected order.
295
296 // R8-R15
297 memcpy(&(*regs)[X86_64_REG_R8], &x86_64_ucontext->uc_mcontext.r8, 8 * sizeof(uint64_t));
298
299 // Rest of the registers.
300 (*regs)[X86_64_REG_RDI] = x86_64_ucontext->uc_mcontext.rdi;
301 (*regs)[X86_64_REG_RSI] = x86_64_ucontext->uc_mcontext.rsi;
302 (*regs)[X86_64_REG_RBP] = x86_64_ucontext->uc_mcontext.rbp;
303 (*regs)[X86_64_REG_RBX] = x86_64_ucontext->uc_mcontext.rbx;
304 (*regs)[X86_64_REG_RDX] = x86_64_ucontext->uc_mcontext.rdx;
305 (*regs)[X86_64_REG_RAX] = x86_64_ucontext->uc_mcontext.rax;
306 (*regs)[X86_64_REG_RCX] = x86_64_ucontext->uc_mcontext.rcx;
307 (*regs)[X86_64_REG_RSP] = x86_64_ucontext->uc_mcontext.rsp;
308 (*regs)[X86_64_REG_RIP] = x86_64_ucontext->uc_mcontext.rip;
309
310 regs->SetFromRaw();
311 return regs;
312}
313
314Regs* Regs::CreateFromUcontext(uint32_t machine_type, void* ucontext) {
315 switch (machine_type) {
316 case EM_386:
317 return CreateFromX86Ucontext(ucontext);
318 case EM_X86_64:
319 return CreateFromX86_64Ucontext(ucontext);
320 case EM_ARM:
321 return CreateFromArmUcontext(ucontext);
322 case EM_AARCH64:
323 return CreateFromArm64Ucontext(ucontext);
324 }
325 return nullptr;
326}
327
328uint32_t Regs::GetMachineType() {
329#if defined(__arm__)
330 return EM_ARM;
331#elif defined(__aarch64__)
332 return EM_AARCH64;
333#elif defined(__i386__)
334 return EM_386;
335#elif defined(__x86_64__)
336 return EM_X86_64;
337#else
338 abort();
339#endif
340}
341
342Regs* Regs::CreateFromLocal() {
343 Regs* regs;
344#if defined(__arm__)
345 regs = new RegsArm();
346#elif defined(__aarch64__)
347 regs = new RegsArm64();
348#elif defined(__i386__)
349 regs = new RegsX86();
350#elif defined(__x86_64__)
351 regs = new RegsX86_64();
352#else
353 abort();
354#endif
355 return regs;
356}