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 #include <unwindstack/Elf.h>
23 #include <unwindstack/MachineArm.h>
24 #include <unwindstack/MapInfo.h>
25 #include <unwindstack/Memory.h>
26 #include <unwindstack/RegsArm.h>
27 #include <unwindstack/UcontextArm.h>
28 #include <unwindstack/UserArm.h>
29
30 namespace unwindstack {
31
RegsArm()32 RegsArm::RegsArm() : RegsImpl<uint32_t>(ARM_REG_LAST, Location(LOCATION_REGISTER, ARM_REG_LR)) {}
33
Arch()34 ArchEnum RegsArm::Arch() {
35 return ARCH_ARM;
36 }
37
pc()38 uint64_t RegsArm::pc() {
39 return regs_[ARM_REG_PC];
40 }
41
sp()42 uint64_t RegsArm::sp() {
43 return regs_[ARM_REG_SP];
44 }
45
set_pc(uint64_t pc)46 void RegsArm::set_pc(uint64_t pc) {
47 regs_[ARM_REG_PC] = pc;
48 }
49
set_sp(uint64_t sp)50 void RegsArm::set_sp(uint64_t sp) {
51 regs_[ARM_REG_SP] = sp;
52 }
53
SetPcFromReturnAddress(Memory *)54 bool RegsArm::SetPcFromReturnAddress(Memory*) {
55 uint32_t lr = regs_[ARM_REG_LR];
56 if (regs_[ARM_REG_PC] == lr) {
57 return false;
58 }
59
60 regs_[ARM_REG_PC] = lr;
61 return true;
62 }
63
IterateRegisters(std::function<void (const char *,uint64_t)> fn)64 void 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
Read(const void * remote_data)83 Regs* RegsArm::Read(const void* remote_data) {
84 const arm_user_regs* user = reinterpret_cast<const arm_user_regs*>(remote_data);
85
86 RegsArm* regs = new RegsArm();
87 memcpy(regs->RawData(), &user->regs[0], ARM_REG_LAST * sizeof(uint32_t));
88 return regs;
89 }
90
CreateFromUcontext(void * ucontext)91 Regs* 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));
96 return regs;
97 }
98
StepIfSignalHandler(uint64_t elf_offset,Elf * elf,Memory * process_memory)99 bool RegsArm::StepIfSignalHandler(uint64_t elf_offset, Elf* elf, Memory* process_memory) {
100 // Read from elf memory since it is usually more expensive to read from
101 // process memory.
102 uint32_t data;
103 if (!elf->memory()->ReadFully(elf_offset, &data, sizeof(data))) {
104 return false;
105 }
106
107 uint64_t offset = 0;
108 if (data == 0xe3a07077 || data == 0xef900077 || data == 0xdf002777) {
109 uint64_t sp = regs_[ARM_REG_SP];
110 // non-RT sigreturn call.
111 // __restore:
112 //
113 // Form 1 (arm):
114 // 0x77 0x70 mov r7, #0x77
115 // 0xa0 0xe3 svc 0x00000000
116 //
117 // Form 2 (arm):
118 // 0x77 0x00 0x90 0xef svc 0x00900077
119 //
120 // Form 3 (thumb):
121 // 0x77 0x27 movs r7, #77
122 // 0x00 0xdf svc 0
123 if (!process_memory->ReadFully(sp, &data, sizeof(data))) {
124 return false;
125 }
126 if (data == 0x5ac3c35a) {
127 // SP + uc_mcontext offset + r0 offset.
128 offset = sp + 0x14 + 0xc;
129 } else {
130 // SP + r0 offset
131 offset = sp + 0xc;
132 }
133 } else if (data == 0xe3a070ad || data == 0xef9000ad || data == 0xdf0027ad) {
134 uint64_t sp = regs_[ARM_REG_SP];
135 // RT sigreturn call.
136 // __restore_rt:
137 //
138 // Form 1 (arm):
139 // 0xad 0x70 mov r7, #0xad
140 // 0xa0 0xe3 svc 0x00000000
141 //
142 // Form 2 (arm):
143 // 0xad 0x00 0x90 0xef svc 0x009000ad
144 //
145 // Form 3 (thumb):
146 // 0xad 0x27 movs r7, #ad
147 // 0x00 0xdf svc 0
148 if (!process_memory->ReadFully(sp, &data, sizeof(data))) {
149 return false;
150 }
151 if (data == sp + 8) {
152 // SP + 8 + sizeof(siginfo_t) + uc_mcontext_offset + r0 offset
153 offset = sp + 8 + 0x80 + 0x14 + 0xc;
154 } else {
155 // SP + sizeof(siginfo_t) + uc_mcontext_offset + r0 offset
156 offset = sp + 0x80 + 0x14 + 0xc;
157 }
158 }
159 if (offset == 0) {
160 return false;
161 }
162
163 if (!process_memory->ReadFully(offset, regs_.data(), sizeof(uint32_t) * ARM_REG_LAST)) {
164 return false;
165 }
166 return true;
167 }
168
Clone()169 Regs* RegsArm::Clone() {
170 return new RegsArm(*this);
171 }
172
173 } // namespace unwindstack
174