blob: 2e6908c264b2835246e68df5e7b060f784efac18 [file] [log] [blame]
Douglas Leung61b1a1a2017-11-08 10:53:53 +01001/*
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
19#include <functional>
20
21#include <unwindstack/Elf.h>
Christopher Ferris53914162018-02-08 19:27:47 -080022#include <unwindstack/MachineMips.h>
Douglas Leung61b1a1a2017-11-08 10:53:53 +010023#include <unwindstack/MapInfo.h>
24#include <unwindstack/Memory.h>
25#include <unwindstack/RegsMips.h>
Christopher Ferris53914162018-02-08 19:27:47 -080026#include <unwindstack/UcontextMips.h>
27#include <unwindstack/UserMips.h>
Douglas Leung61b1a1a2017-11-08 10:53:53 +010028
29namespace unwindstack {
30
31RegsMips::RegsMips()
Yabin Cui414df3e2018-03-14 18:16:22 -070032 : RegsImpl<uint32_t>(MIPS_REG_LAST, Location(LOCATION_REGISTER, MIPS_REG_RA)) {}
Douglas Leung61b1a1a2017-11-08 10:53:53 +010033
34ArchEnum RegsMips::Arch() {
35 return ARCH_MIPS;
36}
37
Yabin Cui414df3e2018-03-14 18:16:22 -070038uint64_t RegsMips::pc() {
39 return regs_[MIPS_REG_PC];
40}
41
42uint64_t RegsMips::sp() {
43 return regs_[MIPS_REG_SP];
44}
45
46void RegsMips::set_pc(uint64_t pc) {
47 regs_[MIPS_REG_PC] = static_cast<uint32_t>(pc);
48}
49
50void RegsMips::set_sp(uint64_t sp) {
51 regs_[MIPS_REG_SP] = static_cast<uint32_t>(sp);
52}
53
Christopher Ferrisfd6b7282018-03-28 15:12:49 -070054uint64_t RegsMips::GetPcAdjustment(uint64_t rel_pc, Elf*) {
55 if (rel_pc < 8) {
Christopher Ferrisa2ec50b2018-02-21 15:39:07 -080056 return 0;
Douglas Leung61b1a1a2017-11-08 10:53:53 +010057 }
Christopher Ferrisa2ec50b2018-02-21 15:39:07 -080058 // For now, just assume no compact branches
59 return 8;
Douglas Leung61b1a1a2017-11-08 10:53:53 +010060}
61
Douglas Leung61b1a1a2017-11-08 10:53:53 +010062bool RegsMips::SetPcFromReturnAddress(Memory*) {
Yabin Cui414df3e2018-03-14 18:16:22 -070063 uint32_t ra = regs_[MIPS_REG_RA];
64 if (regs_[MIPS_REG_PC] == ra) {
Douglas Leung61b1a1a2017-11-08 10:53:53 +010065 return false;
66 }
67
Yabin Cui414df3e2018-03-14 18:16:22 -070068 regs_[MIPS_REG_PC] = ra;
Douglas Leung61b1a1a2017-11-08 10:53:53 +010069 return true;
70}
71
72void RegsMips::IterateRegisters(std::function<void(const char*, uint64_t)> fn) {
73 fn("r0", regs_[MIPS_REG_R0]);
74 fn("r1", regs_[MIPS_REG_R1]);
75 fn("r2", regs_[MIPS_REG_R2]);
76 fn("r3", regs_[MIPS_REG_R3]);
77 fn("r4", regs_[MIPS_REG_R4]);
78 fn("r5", regs_[MIPS_REG_R5]);
79 fn("r6", regs_[MIPS_REG_R6]);
80 fn("r7", regs_[MIPS_REG_R7]);
81 fn("r8", regs_[MIPS_REG_R8]);
82 fn("r9", regs_[MIPS_REG_R9]);
83 fn("r10", regs_[MIPS_REG_R10]);
84 fn("r11", regs_[MIPS_REG_R11]);
85 fn("r12", regs_[MIPS_REG_R12]);
86 fn("r13", regs_[MIPS_REG_R13]);
87 fn("r14", regs_[MIPS_REG_R14]);
88 fn("r15", regs_[MIPS_REG_R15]);
89 fn("r16", regs_[MIPS_REG_R16]);
90 fn("r17", regs_[MIPS_REG_R17]);
91 fn("r18", regs_[MIPS_REG_R18]);
92 fn("r19", regs_[MIPS_REG_R19]);
93 fn("r20", regs_[MIPS_REG_R20]);
94 fn("r21", regs_[MIPS_REG_R21]);
95 fn("r22", regs_[MIPS_REG_R22]);
96 fn("r23", regs_[MIPS_REG_R23]);
97 fn("r24", regs_[MIPS_REG_R24]);
98 fn("r25", regs_[MIPS_REG_R25]);
99 fn("r26", regs_[MIPS_REG_R26]);
100 fn("r27", regs_[MIPS_REG_R27]);
101 fn("r28", regs_[MIPS_REG_R28]);
102 fn("sp", regs_[MIPS_REG_SP]);
103 fn("r30", regs_[MIPS_REG_R30]);
104 fn("ra", regs_[MIPS_REG_RA]);
105 fn("pc", regs_[MIPS_REG_PC]);
106}
107
108Regs* RegsMips::Read(void* remote_data) {
109 mips_user_regs* user = reinterpret_cast<mips_user_regs*>(remote_data);
110 RegsMips* regs = new RegsMips();
111 uint32_t* reg_data = reinterpret_cast<uint32_t*>(regs->RawData());
112
113 memcpy(regs->RawData(), &user->regs[MIPS32_EF_R0], (MIPS_REG_R31 + 1) * sizeof(uint32_t));
114
115 reg_data[MIPS_REG_PC] = user->regs[MIPS32_EF_CP0_EPC];
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100116 return regs;
117}
118
119Regs* RegsMips::CreateFromUcontext(void* ucontext) {
120 mips_ucontext_t* mips_ucontext = reinterpret_cast<mips_ucontext_t*>(ucontext);
121
122 RegsMips* regs = new RegsMips();
123 // Copy 64 bit sc_regs over to 32 bit regs
124 for (int i = 0; i < 32; i++) {
125 (*regs)[MIPS_REG_R0 + i] = mips_ucontext->uc_mcontext.sc_regs[i];
126 }
127 (*regs)[MIPS_REG_PC] = mips_ucontext->uc_mcontext.sc_pc;
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100128 return regs;
129}
130
131bool RegsMips::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) {
132 uint64_t data;
133 uint64_t offset = 0;
134 Memory* elf_memory = elf->memory();
135 // Read from elf memory since it is usually more expensive to read from
136 // process memory.
137 if (!elf_memory->Read(rel_pc, &data, sizeof(data))) {
138 return false;
139 }
140
141 // Look for the kernel sigreturn functions.
142 // __vdso_rt_sigreturn:
143 // 0x24021061 li v0, 0x1061
144 // 0x0000000c syscall
145 // __vdso_sigreturn:
146 // 0x24021017 li v0, 0x1017
147 // 0x0000000c syscall
148 if (data == 0x0000000c24021061ULL) {
149 // vdso_rt_sigreturn => read rt_sigframe
150 // offset = siginfo offset + sizeof(siginfo) + uc_mcontext offset + sc_pc offset
151 offset = 24 + 128 + 24 + 8;
152 } else if (data == 0x0000000c24021017LL) {
153 // vdso_sigreturn => read sigframe
154 // offset = sigcontext offset + sc_pc offset
155 offset = 24 + 8;
156 } else {
157 return false;
158 }
159
160 // read sc_pc and sc_regs[32] from stack
161 uint64_t values[MIPS_REG_LAST];
Yabin Cui414df3e2018-03-14 18:16:22 -0700162 if (!process_memory->Read(regs_[MIPS_REG_SP] + offset, values, sizeof(values))) {
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100163 return false;
164 }
165
166 // Copy 64 bit sc_pc over to 32 bit regs_[MIPS_REG_PC]
167 regs_[MIPS_REG_PC] = values[0];
168
169 // Copy 64 bit sc_regs over to 32 bit regs
170 for (int i = 0; i < 32; i++) {
171 regs_[MIPS_REG_R0 + i] = values[1 + i];
172 }
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100173 return true;
174}
175
Josh Gaofe396312018-04-20 11:51:14 -0700176Regs* RegsMips::Clone() {
177 return new RegsMips(*this);
178}
179
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100180} // namespace unwindstack