CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/MIPS/MIPSStackWalk.cpp
Views: 1401
1
// Copyright (c) 2012- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include "Core/MemMap.h"
19
#include "Core/Debugger/SymbolMap.h"
20
#include "Core/MIPS/MIPSCodeUtils.h"
21
#include "Core/MIPS/MIPSStackWalk.h"
22
23
#define _RS ((op >> 21) & 0x1F)
24
#define _RT ((op >> 16) & 0x1F)
25
#define _RD ((op >> 11) & 0x1F)
26
#define _IMM16 ((signed short)(op & 0xFFFF))
27
28
#define MIPSTABLE_IMM_MASK 0xFC000000
29
#define MIPSTABLE_SPECIAL_MASK 0xFC00003F
30
31
namespace MIPSStackWalk {
32
using namespace MIPSCodeUtils;
33
34
// In the worst case, we scan this far above the pc for an entry.
35
const int MAX_FUNC_SIZE = 32768 * 4;
36
// After this we assume we're stuck.
37
const size_t MAX_DEPTH = 1024;
38
39
static u32 GuessEntry(u32 pc) {
40
SymbolInfo info;
41
if (g_symbolMap->GetSymbolInfo(&info, pc)) {
42
return info.address;
43
}
44
return INVALIDTARGET;
45
}
46
47
bool IsSWInstr(MIPSOpcode op) {
48
return (op & MIPSTABLE_IMM_MASK) == 0xAC000000;
49
}
50
51
bool IsAddImmInstr(MIPSOpcode op) {
52
return (op & MIPSTABLE_IMM_MASK) == 0x20000000 || (op & MIPSTABLE_IMM_MASK) == 0x24000000;
53
}
54
55
bool IsMovRegsInstr(MIPSOpcode op) {
56
// TODO: There are more options here. Let's assume addu for now.
57
if ((op & MIPSTABLE_SPECIAL_MASK) == 0x00000021) {
58
return _RS == 0 || _RT == 0;
59
}
60
return false;
61
}
62
63
bool ScanForAllocaSignature(u32 pc) {
64
// In God Eater Burst, for example, after 0880E750, there's what looks like an alloca().
65
// It's surrounded by "mov fp, sp" and "mov sp, fp", which is unlikely to be used for other reasons.
66
67
// It ought to be pretty close.
68
u32 stop = pc - 32 * 4;
69
for (; Memory::IsValidAddress(pc) && pc >= stop; pc -= 4) {
70
MIPSOpcode op = Memory::Read_Instruction(pc, true);
71
72
// We're looking for a "mov fp, sp" close by a "addiu sp, sp, -N".
73
if (IsMovRegsInstr(op) && _RD == MIPS_REG_FP && (_RS == MIPS_REG_SP || _RT == MIPS_REG_SP)) {
74
return true;
75
}
76
}
77
return false;
78
}
79
80
bool ScanForEntry(StackFrame &frame, u32 entry, u32 &ra) {
81
// Let's hope there are no > 1MB functions on the PSP, for the sake of humanity...
82
const u32 LONGEST_FUNCTION = 1024 * 1024;
83
// TODO: Check if found entry is in the same symbol? Might be wrong sometimes...
84
85
if (entry != INVALIDTARGET && frame.pc == entry) {
86
// This happens when we're at the start of a function. Our ra is already correct.
87
frame.entry = entry;
88
// This function may consume stack, but the frame hasn't used it yet.
89
frame.stackSize = 0;
90
return true;
91
}
92
93
int ra_offset = -1;
94
// Start one instruction before the current frame pc, as that hasn't run yet.
95
const u32 start = frame.pc - 4;
96
u32 stop = entry;
97
if (entry == INVALIDTARGET) {
98
if (start >= PSP_GetUserMemoryBase()) {
99
stop = PSP_GetUserMemoryBase();
100
} else if (start >= PSP_GetKernelMemoryBase()) {
101
stop = PSP_GetKernelMemoryBase();
102
} else if (start >= PSP_GetScratchpadMemoryBase()) {
103
stop = PSP_GetScratchpadMemoryBase();
104
}
105
}
106
107
if (!Memory::IsValidAddress(start)) {
108
return false;
109
}
110
111
if (stop < start - LONGEST_FUNCTION) {
112
stop = start - LONGEST_FUNCTION;
113
}
114
for (u32 pc = start; Memory::IsValidAddress(pc) && pc >= stop; pc -= 4) {
115
MIPSOpcode op = Memory::Read_Instruction(pc, true);
116
117
// Here's where they store the ra address.
118
if (IsSWInstr(op) && _RT == MIPS_REG_RA && _RS == MIPS_REG_SP) {
119
ra_offset = _IMM16;
120
}
121
122
if (IsAddImmInstr(op) && _RT == MIPS_REG_SP && _RS == MIPS_REG_SP) {
123
// A positive imm either means alloca() or we went too far.
124
if (_IMM16 > 0) {
125
// TODO: Maybe check for any alloca() signature and bail?
126
continue;
127
}
128
if (ScanForAllocaSignature(pc)) {
129
continue;
130
}
131
132
frame.entry = pc;
133
frame.stackSize = -_IMM16;
134
if (ra_offset != -1 && Memory::IsValidAddress(frame.sp + ra_offset)) {
135
ra = Memory::Read_U32(frame.sp + ra_offset);
136
}
137
return true;
138
}
139
}
140
return false;
141
}
142
143
bool DetermineFrameInfo(StackFrame &frame, u32 possibleEntry, u32 threadEntry, u32 &ra) {
144
if (ScanForEntry(frame, possibleEntry, ra)) {
145
// Awesome, found one that looks right.
146
return true;
147
} else if (ra != INVALIDTARGET && possibleEntry != INVALIDTARGET) {
148
// Let's just assume it's a leaf.
149
frame.entry = possibleEntry;
150
frame.stackSize = 0;
151
return true;
152
}
153
154
// Okay, we failed to get one. Our possibleEntry could be wrong, it often is.
155
// Let's just scan upward.
156
u32 newPossibleEntry = frame.pc > threadEntry ? threadEntry : frame.pc - MAX_FUNC_SIZE;
157
if (ScanForEntry(frame, newPossibleEntry, ra)) {
158
return true;
159
} else {
160
return false;
161
}
162
}
163
164
std::vector<StackFrame> Walk(u32 pc, u32 ra, u32 sp, u32 threadEntry, u32 threadStackTop) {
165
std::vector<StackFrame> frames;
166
StackFrame current;
167
current.pc = pc;
168
current.sp = sp;
169
current.entry = INVALIDTARGET;
170
current.stackSize = -1;
171
172
u32 prevEntry = INVALIDTARGET;
173
while (pc != threadEntry) {
174
u32 possibleEntry = GuessEntry(current.pc);
175
if (DetermineFrameInfo(current, possibleEntry, threadEntry, ra)) {
176
frames.push_back(current);
177
if (current.entry == threadEntry || GuessEntry(current.entry) == threadEntry) {
178
break;
179
}
180
if (current.entry == prevEntry || frames.size() >= MAX_DEPTH) {
181
// Recursion, means we're screwed. Let's just give up.
182
break;
183
}
184
prevEntry = current.entry;
185
186
current.pc = ra;
187
current.sp += current.stackSize;
188
ra = INVALIDTARGET;
189
current.entry = INVALIDTARGET;
190
current.stackSize = -1;
191
} else {
192
// Well, we got as far as we could.
193
current.entry = possibleEntry;
194
current.stackSize = 0;
195
frames.push_back(current);
196
break;
197
}
198
}
199
200
return frames;
201
}
202
};
203
204