blob: b4e524603e43f9b066b71e1ac17c10a41a5b4c55 [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>
22#include <unwindstack/MapInfo.h>
23#include <unwindstack/Memory.h>
24#include <unwindstack/RegsMips64.h>
25
26#include "MachineMips64.h"
27#include "UcontextMips64.h"
28#include "UserMips64.h"
29
30namespace unwindstack {
31
32RegsMips64::RegsMips64()
33 : RegsImpl<uint64_t>(MIPS64_REG_LAST, MIPS64_REG_SP,
34 Location(LOCATION_REGISTER, MIPS64_REG_RA)) {}
35
36ArchEnum RegsMips64::Arch() {
37 return ARCH_MIPS64;
38}
39
40uint64_t RegsMips64::GetAdjustedPc(uint64_t rel_pc, Elf* elf) {
41 if (!elf->valid()) {
42 return rel_pc;
43 }
44
45 // For now, just assuming no compact branches
46 if (rel_pc < 8) {
47 return rel_pc;
48 }
49 return rel_pc - 8;
50}
51
52void RegsMips64::SetFromRaw() {
53 set_pc(regs_[MIPS64_REG_PC]);
54 set_sp(regs_[MIPS64_REG_SP]);
55}
56
57bool RegsMips64::SetPcFromReturnAddress(Memory*) {
58 if (pc() == regs_[MIPS64_REG_RA]) {
59 return false;
60 }
61
62 set_pc(regs_[MIPS64_REG_RA]);
63 return true;
64}
65
66void RegsMips64::IterateRegisters(std::function<void(const char*, uint64_t)> fn) {
67 fn("r0", regs_[MIPS64_REG_R0]);
68 fn("r1", regs_[MIPS64_REG_R1]);
69 fn("r2", regs_[MIPS64_REG_R2]);
70 fn("r3", regs_[MIPS64_REG_R3]);
71 fn("r4", regs_[MIPS64_REG_R4]);
72 fn("r5", regs_[MIPS64_REG_R5]);
73 fn("r6", regs_[MIPS64_REG_R6]);
74 fn("r7", regs_[MIPS64_REG_R7]);
75 fn("r8", regs_[MIPS64_REG_R8]);
76 fn("r9", regs_[MIPS64_REG_R9]);
77 fn("r10", regs_[MIPS64_REG_R10]);
78 fn("r11", regs_[MIPS64_REG_R11]);
79 fn("r12", regs_[MIPS64_REG_R12]);
80 fn("r13", regs_[MIPS64_REG_R13]);
81 fn("r14", regs_[MIPS64_REG_R14]);
82 fn("r15", regs_[MIPS64_REG_R15]);
83 fn("r16", regs_[MIPS64_REG_R16]);
84 fn("r17", regs_[MIPS64_REG_R17]);
85 fn("r18", regs_[MIPS64_REG_R18]);
86 fn("r19", regs_[MIPS64_REG_R19]);
87 fn("r20", regs_[MIPS64_REG_R20]);
88 fn("r21", regs_[MIPS64_REG_R21]);
89 fn("r22", regs_[MIPS64_REG_R22]);
90 fn("r23", regs_[MIPS64_REG_R23]);
91 fn("r24", regs_[MIPS64_REG_R24]);
92 fn("r25", regs_[MIPS64_REG_R25]);
93 fn("r26", regs_[MIPS64_REG_R26]);
94 fn("r27", regs_[MIPS64_REG_R27]);
95 fn("r28", regs_[MIPS64_REG_R28]);
96 fn("sp", regs_[MIPS64_REG_SP]);
97 fn("r30", regs_[MIPS64_REG_R30]);
98 fn("ra", regs_[MIPS64_REG_RA]);
99 fn("pc", regs_[MIPS64_REG_PC]);
100}
101
102Regs* RegsMips64::Read(void* remote_data) {
103 mips64_user_regs* user = reinterpret_cast<mips64_user_regs*>(remote_data);
104 RegsMips64* regs = new RegsMips64();
105 uint64_t* reg_data = reinterpret_cast<uint64_t*>(regs->RawData());
106
107 memcpy(regs->RawData(), &user->regs[MIPS64_EF_R0], (MIPS64_REG_R31 + 1) * sizeof(uint64_t));
108
109 reg_data[MIPS64_REG_PC] = user->regs[MIPS64_EF_CP0_EPC];
110 regs->SetFromRaw();
111 return regs;
112}
113
114Regs* RegsMips64::CreateFromUcontext(void* ucontext) {
115 mips64_ucontext_t* mips64_ucontext = reinterpret_cast<mips64_ucontext_t*>(ucontext);
116
117 RegsMips64* regs = new RegsMips64();
118 // Copy 64 bit sc_regs over to 64 bit regs
119 memcpy(regs->RawData(), &mips64_ucontext->uc_mcontext.sc_regs[0], 32 * sizeof(uint64_t));
120 (*regs)[MIPS64_REG_PC] = mips64_ucontext->uc_mcontext.sc_pc;
121 regs->SetFromRaw();
122 return regs;
123}
124
125bool RegsMips64::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) {
126 uint64_t data;
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->Read(rel_pc, &data, sizeof(data))) {
131 return false;
132 }
133
134 // Look for the kernel sigreturn function.
135 // __vdso_rt_sigreturn:
136 // 0x2402145b li v0, 0x145b
137 // 0x0000000c syscall
138 if (data != 0x0000000c2402145bULL) {
139 return false;
140 }
141
142 // vdso_rt_sigreturn => read rt_sigframe
143 // offset = siginfo offset + sizeof(siginfo) + uc_mcontext offset
144 // read 64 bit sc_regs[32] from stack into 64 bit regs_
145 if (!process_memory->Read(sp() + 24 + 128 + 40, regs_.data(),
146 sizeof(uint64_t) * (MIPS64_REG_LAST - 1))) {
147 return false;
148 }
149
150 // offset = siginfo offset + sizeof(siginfo) + uc_mcontext offset + sc_pc offset
151 // read 64 bit sc_pc from stack into 64 bit regs_[MIPS64_REG_PC]
152 if (!process_memory->Read(sp() + 24 + 128 + 40 + 576, &regs_[MIPS64_REG_PC],
153 sizeof(uint64_t))) {
154 return false;
155 }
156
157 SetFromRaw();
158 return true;
159}
160
161} // namespace unwindstack