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 
GetPcAdjustment(uint64_t rel_pc,Elf * elf)54 uint64_t RegsArm::GetPcAdjustment(uint64_t rel_pc, Elf* elf) {
55   if (!elf->valid()) {
56     return 2;
57   }
58 
59   uint64_t load_bias = elf->GetLoadBias();
60   if (rel_pc < load_bias) {
61     if (rel_pc < 2) {
62       return 0;
63     }
64     return 2;
65   }
66   uint64_t adjusted_rel_pc = rel_pc - load_bias;
67   if (adjusted_rel_pc < 5) {
68     if (adjusted_rel_pc < 2) {
69       return 0;
70     }
71     return 2;
72   }
73 
74   if (adjusted_rel_pc & 1) {
75     // This is a thumb instruction, it could be 2 or 4 bytes.
76     uint32_t value;
77     if (!elf->memory()->ReadFully(adjusted_rel_pc - 5, &value, sizeof(value)) ||
78         (value & 0xe000f000) != 0xe000f000) {
79       return 2;
80     }
81   }
82   return 4;
83 }
84 
SetPcFromReturnAddress(Memory *)85 bool RegsArm::SetPcFromReturnAddress(Memory*) {
86   uint32_t lr = regs_[ARM_REG_LR];
87   if (regs_[ARM_REG_PC] == lr) {
88     return false;
89   }
90 
91   regs_[ARM_REG_PC] = lr;
92   return true;
93 }
94 
IterateRegisters(std::function<void (const char *,uint64_t)> fn)95 void RegsArm::IterateRegisters(std::function<void(const char*, uint64_t)> fn) {
96   fn("r0", regs_[ARM_REG_R0]);
97   fn("r1", regs_[ARM_REG_R1]);
98   fn("r2", regs_[ARM_REG_R2]);
99   fn("r3", regs_[ARM_REG_R3]);
100   fn("r4", regs_[ARM_REG_R4]);
101   fn("r5", regs_[ARM_REG_R5]);
102   fn("r6", regs_[ARM_REG_R6]);
103   fn("r7", regs_[ARM_REG_R7]);
104   fn("r8", regs_[ARM_REG_R8]);
105   fn("r9", regs_[ARM_REG_R9]);
106   fn("r10", regs_[ARM_REG_R10]);
107   fn("r11", regs_[ARM_REG_R11]);
108   fn("ip", regs_[ARM_REG_R12]);
109   fn("sp", regs_[ARM_REG_SP]);
110   fn("lr", regs_[ARM_REG_LR]);
111   fn("pc", regs_[ARM_REG_PC]);
112 }
113 
Read(void * remote_data)114 Regs* RegsArm::Read(void* remote_data) {
115   arm_user_regs* user = reinterpret_cast<arm_user_regs*>(remote_data);
116 
117   RegsArm* regs = new RegsArm();
118   memcpy(regs->RawData(), &user->regs[0], ARM_REG_LAST * sizeof(uint32_t));
119   return regs;
120 }
121 
CreateFromUcontext(void * ucontext)122 Regs* RegsArm::CreateFromUcontext(void* ucontext) {
123   arm_ucontext_t* arm_ucontext = reinterpret_cast<arm_ucontext_t*>(ucontext);
124 
125   RegsArm* regs = new RegsArm();
126   memcpy(regs->RawData(), &arm_ucontext->uc_mcontext.regs[0], ARM_REG_LAST * sizeof(uint32_t));
127   return regs;
128 }
129 
StepIfSignalHandler(uint64_t rel_pc,Elf * elf,Memory * process_memory)130 bool RegsArm::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) {
131   uint32_t data;
132   Memory* elf_memory = elf->memory();
133   // Read from elf memory since it is usually more expensive to read from
134   // process memory.
135   if (!elf_memory->ReadFully(rel_pc, &data, sizeof(data))) {
136     return false;
137   }
138 
139   uint64_t offset = 0;
140   if (data == 0xe3a07077 || data == 0xef900077 || data == 0xdf002777) {
141     uint64_t sp = regs_[ARM_REG_SP];
142     // non-RT sigreturn call.
143     // __restore:
144     //
145     // Form 1 (arm):
146     // 0x77 0x70              mov r7, #0x77
147     // 0xa0 0xe3              svc 0x00000000
148     //
149     // Form 2 (arm):
150     // 0x77 0x00 0x90 0xef    svc 0x00900077
151     //
152     // Form 3 (thumb):
153     // 0x77 0x27              movs r7, #77
154     // 0x00 0xdf              svc 0
155     if (!process_memory->ReadFully(sp, &data, sizeof(data))) {
156       return false;
157     }
158     if (data == 0x5ac3c35a) {
159       // SP + uc_mcontext offset + r0 offset.
160       offset = sp + 0x14 + 0xc;
161     } else {
162       // SP + r0 offset
163       offset = sp + 0xc;
164     }
165   } else if (data == 0xe3a070ad || data == 0xef9000ad || data == 0xdf0027ad) {
166     uint64_t sp = regs_[ARM_REG_SP];
167     // RT sigreturn call.
168     // __restore_rt:
169     //
170     // Form 1 (arm):
171     // 0xad 0x70      mov r7, #0xad
172     // 0xa0 0xe3      svc 0x00000000
173     //
174     // Form 2 (arm):
175     // 0xad 0x00 0x90 0xef    svc 0x009000ad
176     //
177     // Form 3 (thumb):
178     // 0xad 0x27              movs r7, #ad
179     // 0x00 0xdf              svc 0
180     if (!process_memory->ReadFully(sp, &data, sizeof(data))) {
181       return false;
182     }
183     if (data == sp + 8) {
184       // SP + 8 + sizeof(siginfo_t) + uc_mcontext_offset + r0 offset
185       offset = sp + 8 + 0x80 + 0x14 + 0xc;
186     } else {
187       // SP + sizeof(siginfo_t) + uc_mcontext_offset + r0 offset
188       offset = sp + 0x80 + 0x14 + 0xc;
189     }
190   }
191   if (offset == 0) {
192     return false;
193   }
194 
195   if (!process_memory->ReadFully(offset, regs_.data(), sizeof(uint32_t) * ARM_REG_LAST)) {
196     return false;
197   }
198   return true;
199 }
200 
Clone()201 Regs* RegsArm::Clone() {
202   return new RegsArm(*this);
203 }
204 
205 }  // namespace unwindstack
206