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