blob: 1aaa08f5697ba6cc59dc760754702433335a4d48 [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/MachineArm.h>
Christopher Ferrisd06001d2017-11-30 18:56:01 -080024#include <unwindstack/MapInfo.h>
25#include <unwindstack/Memory.h>
26#include <unwindstack/RegsArm.h>
Christopher Ferris53914162018-02-08 19:27:47 -080027#include <unwindstack/UcontextArm.h>
28#include <unwindstack/UserArm.h>
Christopher Ferrisd06001d2017-11-30 18:56:01 -080029
30namespace unwindstack {
31
Yabin Cui11e96fe2018-03-14 18:16:22 -070032RegsArm::RegsArm() : RegsImpl<uint32_t>(ARM_REG_LAST, Location(LOCATION_REGISTER, ARM_REG_LR)) {}
Christopher Ferrisd06001d2017-11-30 18:56:01 -080033
34ArchEnum RegsArm::Arch() {
35 return ARCH_ARM;
36}
37
Yabin Cui11e96fe2018-03-14 18:16:22 -070038uint64_t RegsArm::pc() {
39 return regs_[ARM_REG_PC];
40}
41
42uint64_t RegsArm::sp() {
43 return regs_[ARM_REG_SP];
44}
45
46void RegsArm::set_pc(uint64_t pc) {
47 regs_[ARM_REG_PC] = pc;
48}
49
50void RegsArm::set_sp(uint64_t sp) {
51 regs_[ARM_REG_SP] = sp;
52}
53
Christopher Ferrisd06001d2017-11-30 18:56:01 -080054bool RegsArm::SetPcFromReturnAddress(Memory*) {
Yabin Cui11e96fe2018-03-14 18:16:22 -070055 uint32_t lr = regs_[ARM_REG_LR];
56 if (regs_[ARM_REG_PC] == lr) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -080057 return false;
58 }
59
Yabin Cui11e96fe2018-03-14 18:16:22 -070060 regs_[ARM_REG_PC] = lr;
Christopher Ferrisd06001d2017-11-30 18:56:01 -080061 return true;
62}
63
64void RegsArm::IterateRegisters(std::function<void(const char*, uint64_t)> fn) {
65 fn("r0", regs_[ARM_REG_R0]);
66 fn("r1", regs_[ARM_REG_R1]);
67 fn("r2", regs_[ARM_REG_R2]);
68 fn("r3", regs_[ARM_REG_R3]);
69 fn("r4", regs_[ARM_REG_R4]);
70 fn("r5", regs_[ARM_REG_R5]);
71 fn("r6", regs_[ARM_REG_R6]);
72 fn("r7", regs_[ARM_REG_R7]);
73 fn("r8", regs_[ARM_REG_R8]);
74 fn("r9", regs_[ARM_REG_R9]);
75 fn("r10", regs_[ARM_REG_R10]);
76 fn("r11", regs_[ARM_REG_R11]);
77 fn("ip", regs_[ARM_REG_R12]);
78 fn("sp", regs_[ARM_REG_SP]);
79 fn("lr", regs_[ARM_REG_LR]);
80 fn("pc", regs_[ARM_REG_PC]);
81}
82
83Regs* RegsArm::Read(void* remote_data) {
84 arm_user_regs* user = reinterpret_cast<arm_user_regs*>(remote_data);
85
86 RegsArm* regs = new RegsArm();
87 memcpy(regs->RawData(), &user->regs[0], ARM_REG_LAST * sizeof(uint32_t));
Christopher Ferrisd06001d2017-11-30 18:56:01 -080088 return regs;
89}
90
91Regs* RegsArm::CreateFromUcontext(void* ucontext) {
92 arm_ucontext_t* arm_ucontext = reinterpret_cast<arm_ucontext_t*>(ucontext);
93
94 RegsArm* regs = new RegsArm();
95 memcpy(regs->RawData(), &arm_ucontext->uc_mcontext.regs[0], ARM_REG_LAST * sizeof(uint32_t));
Christopher Ferrisd06001d2017-11-30 18:56:01 -080096 return regs;
97}
98
Christopher Ferrisf0c82e72019-12-04 13:37:11 -080099bool RegsArm::StepIfSignalHandler(uint64_t elf_offset, Elf* elf, Memory* process_memory) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800100 uint32_t data;
101 Memory* elf_memory = elf->memory();
102 // Read from elf memory since it is usually more expensive to read from
103 // process memory.
Christopher Ferrisf0c82e72019-12-04 13:37:11 -0800104 if (!elf_memory->ReadFully(elf_offset, &data, sizeof(data))) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800105 return false;
106 }
107
108 uint64_t offset = 0;
109 if (data == 0xe3a07077 || data == 0xef900077 || data == 0xdf002777) {
Yabin Cui11e96fe2018-03-14 18:16:22 -0700110 uint64_t sp = regs_[ARM_REG_SP];
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800111 // non-RT sigreturn call.
112 // __restore:
113 //
114 // Form 1 (arm):
115 // 0x77 0x70 mov r7, #0x77
116 // 0xa0 0xe3 svc 0x00000000
117 //
118 // Form 2 (arm):
119 // 0x77 0x00 0x90 0xef svc 0x00900077
120 //
121 // Form 3 (thumb):
122 // 0x77 0x27 movs r7, #77
123 // 0x00 0xdf svc 0
Yabin Cui11e96fe2018-03-14 18:16:22 -0700124 if (!process_memory->ReadFully(sp, &data, sizeof(data))) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800125 return false;
126 }
127 if (data == 0x5ac3c35a) {
128 // SP + uc_mcontext offset + r0 offset.
Yabin Cui11e96fe2018-03-14 18:16:22 -0700129 offset = sp + 0x14 + 0xc;
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800130 } else {
131 // SP + r0 offset
Yabin Cui11e96fe2018-03-14 18:16:22 -0700132 offset = sp + 0xc;
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800133 }
134 } else if (data == 0xe3a070ad || data == 0xef9000ad || data == 0xdf0027ad) {
Yabin Cui11e96fe2018-03-14 18:16:22 -0700135 uint64_t sp = regs_[ARM_REG_SP];
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800136 // RT sigreturn call.
137 // __restore_rt:
138 //
139 // Form 1 (arm):
140 // 0xad 0x70 mov r7, #0xad
141 // 0xa0 0xe3 svc 0x00000000
142 //
143 // Form 2 (arm):
144 // 0xad 0x00 0x90 0xef svc 0x009000ad
145 //
146 // Form 3 (thumb):
147 // 0xad 0x27 movs r7, #ad
148 // 0x00 0xdf svc 0
Yabin Cui11e96fe2018-03-14 18:16:22 -0700149 if (!process_memory->ReadFully(sp, &data, sizeof(data))) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800150 return false;
151 }
Yabin Cui11e96fe2018-03-14 18:16:22 -0700152 if (data == sp + 8) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800153 // SP + 8 + sizeof(siginfo_t) + uc_mcontext_offset + r0 offset
Yabin Cui11e96fe2018-03-14 18:16:22 -0700154 offset = sp + 8 + 0x80 + 0x14 + 0xc;
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800155 } else {
156 // SP + sizeof(siginfo_t) + uc_mcontext_offset + r0 offset
Yabin Cui11e96fe2018-03-14 18:16:22 -0700157 offset = sp + 0x80 + 0x14 + 0xc;
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800158 }
159 }
160 if (offset == 0) {
161 return false;
162 }
163
164 if (!process_memory->ReadFully(offset, regs_.data(), sizeof(uint32_t) * ARM_REG_LAST)) {
165 return false;
166 }
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800167 return true;
168}
169
Josh Gao2f37a152018-04-20 11:51:14 -0700170Regs* RegsArm::Clone() {
171 return new RegsArm(*this);
172}
173
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800174} // namespace unwindstack