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(void * remote_data)83 Regs* 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));
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   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.
104   if (!elf_memory->ReadFully(elf_offset, &data, sizeof(data))) {
105     return false;
106   }
107 
108   uint64_t offset = 0;
109   if (data == 0xe3a07077 || data == 0xef900077 || data == 0xdf002777) {
110     uint64_t sp = regs_[ARM_REG_SP];
111     // 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
124     if (!process_memory->ReadFully(sp, &data, sizeof(data))) {
125       return false;
126     }
127     if (data == 0x5ac3c35a) {
128       // SP + uc_mcontext offset + r0 offset.
129       offset = sp + 0x14 + 0xc;
130     } else {
131       // SP + r0 offset
132       offset = sp + 0xc;
133     }
134   } else if (data == 0xe3a070ad || data == 0xef9000ad || data == 0xdf0027ad) {
135     uint64_t sp = regs_[ARM_REG_SP];
136     // 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
149     if (!process_memory->ReadFully(sp, &data, sizeof(data))) {
150       return false;
151     }
152     if (data == sp + 8) {
153       // SP + 8 + sizeof(siginfo_t) + uc_mcontext_offset + r0 offset
154       offset = sp + 8 + 0x80 + 0x14 + 0xc;
155     } else {
156       // SP + sizeof(siginfo_t) + uc_mcontext_offset + r0 offset
157       offset = sp + 0x80 + 0x14 + 0xc;
158     }
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   }
167   return true;
168 }
169 
Clone()170 Regs* RegsArm::Clone() {
171   return new RegsArm(*this);
172 }
173 
174 }  // namespace unwindstack
175