1 /*
2 * Copyright (C) 2017 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/MachineMips.h>
24 #include <unwindstack/MapInfo.h>
25 #include <unwindstack/Memory.h>
26 #include <unwindstack/RegsMips.h>
27 #include <unwindstack/UcontextMips.h>
28 #include <unwindstack/UserMips.h>
29
30 namespace unwindstack {
31
RegsMips()32 RegsMips::RegsMips()
33 : RegsImpl<uint32_t>(MIPS_REG_LAST, Location(LOCATION_REGISTER, MIPS_REG_RA)) {}
34
Arch()35 ArchEnum RegsMips::Arch() {
36 return ARCH_MIPS;
37 }
38
pc()39 uint64_t RegsMips::pc() {
40 return regs_[MIPS_REG_PC];
41 }
42
sp()43 uint64_t RegsMips::sp() {
44 return regs_[MIPS_REG_SP];
45 }
46
set_pc(uint64_t pc)47 void RegsMips::set_pc(uint64_t pc) {
48 regs_[MIPS_REG_PC] = static_cast<uint32_t>(pc);
49 }
50
set_sp(uint64_t sp)51 void RegsMips::set_sp(uint64_t sp) {
52 regs_[MIPS_REG_SP] = static_cast<uint32_t>(sp);
53 }
54
SetPcFromReturnAddress(Memory *)55 bool RegsMips::SetPcFromReturnAddress(Memory*) {
56 uint32_t ra = regs_[MIPS_REG_RA];
57 if (regs_[MIPS_REG_PC] == ra) {
58 return false;
59 }
60
61 regs_[MIPS_REG_PC] = ra;
62 return true;
63 }
64
IterateRegisters(std::function<void (const char *,uint64_t)> fn)65 void RegsMips::IterateRegisters(std::function<void(const char*, uint64_t)> fn) {
66 fn("r0", regs_[MIPS_REG_R0]);
67 fn("r1", regs_[MIPS_REG_R1]);
68 fn("r2", regs_[MIPS_REG_R2]);
69 fn("r3", regs_[MIPS_REG_R3]);
70 fn("r4", regs_[MIPS_REG_R4]);
71 fn("r5", regs_[MIPS_REG_R5]);
72 fn("r6", regs_[MIPS_REG_R6]);
73 fn("r7", regs_[MIPS_REG_R7]);
74 fn("r8", regs_[MIPS_REG_R8]);
75 fn("r9", regs_[MIPS_REG_R9]);
76 fn("r10", regs_[MIPS_REG_R10]);
77 fn("r11", regs_[MIPS_REG_R11]);
78 fn("r12", regs_[MIPS_REG_R12]);
79 fn("r13", regs_[MIPS_REG_R13]);
80 fn("r14", regs_[MIPS_REG_R14]);
81 fn("r15", regs_[MIPS_REG_R15]);
82 fn("r16", regs_[MIPS_REG_R16]);
83 fn("r17", regs_[MIPS_REG_R17]);
84 fn("r18", regs_[MIPS_REG_R18]);
85 fn("r19", regs_[MIPS_REG_R19]);
86 fn("r20", regs_[MIPS_REG_R20]);
87 fn("r21", regs_[MIPS_REG_R21]);
88 fn("r22", regs_[MIPS_REG_R22]);
89 fn("r23", regs_[MIPS_REG_R23]);
90 fn("r24", regs_[MIPS_REG_R24]);
91 fn("r25", regs_[MIPS_REG_R25]);
92 fn("r26", regs_[MIPS_REG_R26]);
93 fn("r27", regs_[MIPS_REG_R27]);
94 fn("r28", regs_[MIPS_REG_R28]);
95 fn("sp", regs_[MIPS_REG_SP]);
96 fn("r30", regs_[MIPS_REG_R30]);
97 fn("ra", regs_[MIPS_REG_RA]);
98 fn("pc", regs_[MIPS_REG_PC]);
99 }
100
Read(void * remote_data)101 Regs* RegsMips::Read(void* remote_data) {
102 mips_user_regs* user = reinterpret_cast<mips_user_regs*>(remote_data);
103 RegsMips* regs = new RegsMips();
104 uint32_t* reg_data = reinterpret_cast<uint32_t*>(regs->RawData());
105
106 memcpy(regs->RawData(), &user->regs[MIPS32_EF_R0], (MIPS_REG_R31 + 1) * sizeof(uint32_t));
107
108 reg_data[MIPS_REG_PC] = user->regs[MIPS32_EF_CP0_EPC];
109 return regs;
110 }
111
CreateFromUcontext(void * ucontext)112 Regs* RegsMips::CreateFromUcontext(void* ucontext) {
113 mips_ucontext_t* mips_ucontext = reinterpret_cast<mips_ucontext_t*>(ucontext);
114
115 RegsMips* regs = new RegsMips();
116 // Copy 64 bit sc_regs over to 32 bit regs
117 for (int i = 0; i < 32; i++) {
118 (*regs)[MIPS_REG_R0 + i] = mips_ucontext->uc_mcontext.sc_regs[i];
119 }
120 (*regs)[MIPS_REG_PC] = mips_ucontext->uc_mcontext.sc_pc;
121 return regs;
122 }
123
StepIfSignalHandler(uint64_t elf_offset,Elf * elf,Memory * process_memory)124 bool RegsMips::StepIfSignalHandler(uint64_t elf_offset, Elf* elf, Memory* process_memory) {
125 uint64_t data;
126 uint64_t offset = 0;
127 Memory* elf_memory = elf->memory();
128 // Read from elf memory since it is usually more expensive to read from
129 // process memory.
130 if (!elf_memory->ReadFully(elf_offset, &data, sizeof(data))) {
131 return false;
132 }
133
134 // Look for the kernel sigreturn functions.
135 // __vdso_rt_sigreturn:
136 // 0x24021061 li v0, 0x1061
137 // 0x0000000c syscall
138 // __vdso_sigreturn:
139 // 0x24021017 li v0, 0x1017
140 // 0x0000000c syscall
141 if (data == 0x0000000c24021061ULL) {
142 // vdso_rt_sigreturn => read rt_sigframe
143 // offset = siginfo offset + sizeof(siginfo) + uc_mcontext offset + sc_pc offset
144 offset = 24 + 128 + 24 + 8;
145 } else if (data == 0x0000000c24021017LL) {
146 // vdso_sigreturn => read sigframe
147 // offset = sigcontext offset + sc_pc offset
148 offset = 24 + 8;
149 } else {
150 return false;
151 }
152
153 // read sc_pc and sc_regs[32] from stack
154 uint64_t values[MIPS_REG_LAST];
155 if (!process_memory->ReadFully(regs_[MIPS_REG_SP] + offset, values, sizeof(values))) {
156 return false;
157 }
158
159 // Copy 64 bit sc_pc over to 32 bit regs_[MIPS_REG_PC]
160 regs_[MIPS_REG_PC] = values[0];
161
162 // Copy 64 bit sc_regs over to 32 bit regs
163 for (int i = 0; i < 32; i++) {
164 regs_[MIPS_REG_R0 + i] = values[1 + i];
165 }
166 return true;
167 }
168
Clone()169 Regs* RegsMips::Clone() {
170 return new RegsMips(*this);
171 }
172
173 } // namespace unwindstack
174