blob: 3f67d92f54a58bf33e1eb9284ca7c02a3c015abb [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>
Florian Mayer3f1f2e02018-10-23 15:56:28 +010018#include <string.h>
Douglas Leung61b1a1a2017-11-08 10:53:53 +010019
20#include <functional>
21
22#include <unwindstack/Elf.h>
Christopher Ferris53914162018-02-08 19:27:47 -080023#include <unwindstack/MachineMips64.h>
Douglas Leung61b1a1a2017-11-08 10:53:53 +010024#include <unwindstack/MapInfo.h>
25#include <unwindstack/Memory.h>
26#include <unwindstack/RegsMips64.h>
Christopher Ferris53914162018-02-08 19:27:47 -080027#include <unwindstack/UcontextMips64.h>
28#include <unwindstack/UserMips64.h>
Douglas Leung61b1a1a2017-11-08 10:53:53 +010029
30namespace unwindstack {
31
32RegsMips64::RegsMips64()
Yabin Cui11e96fe2018-03-14 18:16:22 -070033 : RegsImpl<uint64_t>(MIPS64_REG_LAST, Location(LOCATION_REGISTER, MIPS64_REG_RA)) {}
Douglas Leung61b1a1a2017-11-08 10:53:53 +010034
35ArchEnum RegsMips64::Arch() {
36 return ARCH_MIPS64;
37}
38
Yabin Cui11e96fe2018-03-14 18:16:22 -070039uint64_t RegsMips64::pc() {
40 return regs_[MIPS64_REG_PC];
41}
42
43uint64_t RegsMips64::sp() {
44 return regs_[MIPS64_REG_SP];
45}
46
47void RegsMips64::set_pc(uint64_t pc) {
48 regs_[MIPS64_REG_PC] = pc;
49}
50
51void RegsMips64::set_sp(uint64_t sp) {
52 regs_[MIPS64_REG_SP] = sp;
53}
54
Christopher Ferris6dbc28e2018-03-28 15:12:49 -070055uint64_t RegsMips64::GetPcAdjustment(uint64_t rel_pc, Elf*) {
56 if (rel_pc < 8) {
Christopher Ferrisa2ec50b2018-02-21 15:39:07 -080057 return 0;
Douglas Leung61b1a1a2017-11-08 10:53:53 +010058 }
Christopher Ferrisa2ec50b2018-02-21 15:39:07 -080059 // For now, just assume no compact branches
60 return 8;
Douglas Leung61b1a1a2017-11-08 10:53:53 +010061}
62
Douglas Leung61b1a1a2017-11-08 10:53:53 +010063bool RegsMips64::SetPcFromReturnAddress(Memory*) {
Yabin Cui11e96fe2018-03-14 18:16:22 -070064 uint64_t ra = regs_[MIPS64_REG_RA];
65 if (regs_[MIPS64_REG_PC] == ra) {
Douglas Leung61b1a1a2017-11-08 10:53:53 +010066 return false;
67 }
68
Yabin Cui11e96fe2018-03-14 18:16:22 -070069 regs_[MIPS64_REG_PC] = ra;
Douglas Leung61b1a1a2017-11-08 10:53:53 +010070 return true;
71}
72
73void RegsMips64::IterateRegisters(std::function<void(const char*, uint64_t)> fn) {
74 fn("r0", regs_[MIPS64_REG_R0]);
75 fn("r1", regs_[MIPS64_REG_R1]);
76 fn("r2", regs_[MIPS64_REG_R2]);
77 fn("r3", regs_[MIPS64_REG_R3]);
78 fn("r4", regs_[MIPS64_REG_R4]);
79 fn("r5", regs_[MIPS64_REG_R5]);
80 fn("r6", regs_[MIPS64_REG_R6]);
81 fn("r7", regs_[MIPS64_REG_R7]);
82 fn("r8", regs_[MIPS64_REG_R8]);
83 fn("r9", regs_[MIPS64_REG_R9]);
84 fn("r10", regs_[MIPS64_REG_R10]);
85 fn("r11", regs_[MIPS64_REG_R11]);
86 fn("r12", regs_[MIPS64_REG_R12]);
87 fn("r13", regs_[MIPS64_REG_R13]);
88 fn("r14", regs_[MIPS64_REG_R14]);
89 fn("r15", regs_[MIPS64_REG_R15]);
90 fn("r16", regs_[MIPS64_REG_R16]);
91 fn("r17", regs_[MIPS64_REG_R17]);
92 fn("r18", regs_[MIPS64_REG_R18]);
93 fn("r19", regs_[MIPS64_REG_R19]);
94 fn("r20", regs_[MIPS64_REG_R20]);
95 fn("r21", regs_[MIPS64_REG_R21]);
96 fn("r22", regs_[MIPS64_REG_R22]);
97 fn("r23", regs_[MIPS64_REG_R23]);
98 fn("r24", regs_[MIPS64_REG_R24]);
99 fn("r25", regs_[MIPS64_REG_R25]);
100 fn("r26", regs_[MIPS64_REG_R26]);
101 fn("r27", regs_[MIPS64_REG_R27]);
102 fn("r28", regs_[MIPS64_REG_R28]);
103 fn("sp", regs_[MIPS64_REG_SP]);
104 fn("r30", regs_[MIPS64_REG_R30]);
105 fn("ra", regs_[MIPS64_REG_RA]);
106 fn("pc", regs_[MIPS64_REG_PC]);
107}
108
109Regs* RegsMips64::Read(void* remote_data) {
110 mips64_user_regs* user = reinterpret_cast<mips64_user_regs*>(remote_data);
111 RegsMips64* regs = new RegsMips64();
112 uint64_t* reg_data = reinterpret_cast<uint64_t*>(regs->RawData());
113
114 memcpy(regs->RawData(), &user->regs[MIPS64_EF_R0], (MIPS64_REG_R31 + 1) * sizeof(uint64_t));
115
116 reg_data[MIPS64_REG_PC] = user->regs[MIPS64_EF_CP0_EPC];
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100117 return regs;
118}
119
120Regs* RegsMips64::CreateFromUcontext(void* ucontext) {
121 mips64_ucontext_t* mips64_ucontext = reinterpret_cast<mips64_ucontext_t*>(ucontext);
122
123 RegsMips64* regs = new RegsMips64();
124 // Copy 64 bit sc_regs over to 64 bit regs
125 memcpy(regs->RawData(), &mips64_ucontext->uc_mcontext.sc_regs[0], 32 * sizeof(uint64_t));
126 (*regs)[MIPS64_REG_PC] = mips64_ucontext->uc_mcontext.sc_pc;
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100127 return regs;
128}
129
130bool RegsMips64::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) {
131 uint64_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->Read(rel_pc, &data, sizeof(data))) {
136 return false;
137 }
138
139 // Look for the kernel sigreturn function.
140 // __vdso_rt_sigreturn:
141 // 0x2402145b li v0, 0x145b
142 // 0x0000000c syscall
143 if (data != 0x0000000c2402145bULL) {
144 return false;
145 }
146
147 // vdso_rt_sigreturn => read rt_sigframe
148 // offset = siginfo offset + sizeof(siginfo) + uc_mcontext offset
149 // read 64 bit sc_regs[32] from stack into 64 bit regs_
Yabin Cui11e96fe2018-03-14 18:16:22 -0700150 uint64_t sp = regs_[MIPS64_REG_SP];
151 if (!process_memory->Read(sp + 24 + 128 + 40, regs_.data(),
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100152 sizeof(uint64_t) * (MIPS64_REG_LAST - 1))) {
153 return false;
154 }
155
156 // offset = siginfo offset + sizeof(siginfo) + uc_mcontext offset + sc_pc offset
157 // read 64 bit sc_pc from stack into 64 bit regs_[MIPS64_REG_PC]
Yabin Cui11e96fe2018-03-14 18:16:22 -0700158 if (!process_memory->Read(sp + 24 + 128 + 40 + 576, &regs_[MIPS64_REG_PC], sizeof(uint64_t))) {
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100159 return false;
160 }
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100161 return true;
162}
163
Josh Gao2f37a152018-04-20 11:51:14 -0700164Regs* RegsMips64::Clone() {
165 return new RegsMips64(*this);
166}
167
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100168} // namespace unwindstack