forked from hrydgard/ppsspp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MachineContext.h
360 lines (267 loc) · 7.18 KB
/
MachineContext.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Copyright 2008 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
// Note: If MACHINE_CONTEXT_SUPPORTED is not set after including this,
// there is no access to the context from exception handlers (and possibly no exception handling support).
#pragma once
#include "ppsspp_config.h"
#if PPSSPP_PLATFORM(WINDOWS) && !PPSSPP_PLATFORM(UWP)
#include <windows.h>
typedef CONTEXT SContext;
#if defined(__LIBRETRO__)
#elif PPSSPP_ARCH(AMD64)
#define MACHINE_CONTEXT_SUPPORTED
#define CTX_RAX Rax
#define CTX_RBX Rbx
#define CTX_RCX Rcx
#define CTX_RDX Rdx
#define CTX_RDI Rdi
#define CTX_RSI Rsi
#define CTX_RBP Rbp
#define CTX_RSP Rsp
#define CTX_R8 R8
#define CTX_R9 R9
#define CTX_R10 R10
#define CTX_R11 R11
#define CTX_R12 R12
#define CTX_R13 R13
#define CTX_R14 R14
#define CTX_R15 R15
#define CTX_RIP Rip
#elif PPSSPP_ARCH(X86)
#define MACHINE_CONTEXT_SUPPORTED
#define CTX_RAX Eax
#define CTX_RBX Ebx
#define CTX_RCX Ecx
#define CTX_RDX Edx
#define CTX_RDI Edi
#define CTX_RSI Esi
#define CTX_RBP Ebp
#define CTX_RSP Esp
#define CTX_RIP Eip
#elif PPSSPP_ARCH(ARM64)
#define CTX_REG(x) X[x]
#define CTX_SP Sp
#define CTX_PC Pc
#elif PPSSPP_ARCH(ARM)
//#define CTX_REG(x) R##x
#define CTX_SP Sp
#define CTX_PC Pc
#endif
#elif PPSSPP_PLATFORM(MAC)
// for modules:
#define _XOPEN_SOURCE
#include <ucontext.h>
#include <mach/mach.h>
#include <mach/message.h>
#if PPSSPP_ARCH(AMD64)
#define MACHINE_CONTEXT_SUPPORTED
typedef x86_thread_state64_t SContext;
#define CTX_RAX __rax
#define CTX_RBX __rbx
#define CTX_RCX __rcx
#define CTX_RDX __rdx
#define CTX_RDI __rdi
#define CTX_RSI __rsi
#define CTX_RBP __rbp
#define CTX_RSP __rsp
#define CTX_R8 __r8
#define CTX_R9 __r9
#define CTX_R10 __r10
#define CTX_R11 __r11
#define CTX_R12 __r12
#define CTX_R13 __r13
#define CTX_R14 __r14
#define CTX_R15 __r15
#define CTX_RIP __rip
#else
// No context definition for architecture
#endif
#elif defined(__linux__)
#include <signal.h>
#if PPSSPP_ARCH(AMD64)
#include <ucontext.h>
typedef mcontext_t SContext;
#define MACHINE_CONTEXT_SUPPORTED
#define CTX_RAX gregs[REG_RAX]
#define CTX_RBX gregs[REG_RBX]
#define CTX_RCX gregs[REG_RCX]
#define CTX_RDX gregs[REG_RDX]
#define CTX_RDI gregs[REG_RDI]
#define CTX_RSI gregs[REG_RSI]
#define CTX_RBP gregs[REG_RBP]
#define CTX_RSP gregs[REG_RSP]
#define CTX_R8 gregs[REG_R8]
#define CTX_R9 gregs[REG_R9]
#define CTX_R10 gregs[REG_R10]
#define CTX_R11 gregs[REG_R11]
#define CTX_R12 gregs[REG_R12]
#define CTX_R13 gregs[REG_R13]
#define CTX_R14 gregs[REG_R14]
#define CTX_R15 gregs[REG_R15]
#define CTX_RIP gregs[REG_RIP]
#elif PPSSPP_ARCH(X86)
#include <ucontext.h>
typedef mcontext_t SContext;
#define MACHINE_CONTEXT_SUPPORTED
#define CTX_RAX gregs[REG_EAX]
#define CTX_RBX gregs[REG_EBX]
#define CTX_RCX gregs[REG_ECX]
#define CTX_RDX gregs[REG_EDX]
#define CTX_RDI gregs[REG_EDI]
#define CTX_RSI gregs[REG_ESI]
#define CTX_RBP gregs[REG_EBP]
#define CTX_RSP gregs[REG_ESP]
#define CTX_RIP gregs[REG_EIP]
#elif PPSSPP_ARCH(ARM64)
#define MACHINE_CONTEXT_SUPPORTED
typedef sigcontext SContext;
#define CTX_REG(x) regs[x]
#define CTX_SP sp
#define CTX_PC pc
#elif PPSSPP_ARCH(ARM)
#define MACHINE_CONTEXT_SUPPORTED
typedef sigcontext SContext;
#define CTX_PC arm_pc
#define CTX_REG(x) regs[x]
#elif PPSSPP_ARCH(RISCV64)
#include <ucontext.h>
typedef mcontext_t SContext;
#define MACHINE_CONTEXT_SUPPORTED
#define CTX_REG(x) __gregs[x]
#define CTX_PC CTX_REG(0)
#define CTX_SP CTX_REG(2)
#else
// No context definition for architecture
#endif
#elif defined(__OpenBSD__)
#if PPSSPP_ARCH(AMD64)
#include <signal.h>
typedef ucontext_t SContext;
#define MACHINE_CONTEXT_SUPPORTED
#define CTX_RAX sc_rax
#define CTX_RBX sc_rbx
#define CTX_RCX sc_rcx
#define CTX_RDX sc_rdx
#define CTX_RDI sc_rdi
#define CTX_RSI sc_rsi
#define CTX_RBP sc_rbp
#define CTX_RSP sc_rsp
#define CTX_R8 sc_r8
#define CTX_R9 sc_r9
#define CTX_R10 sc_r10
#define CTX_R11 sc_r11
#define CTX_R12 sc_r12
#define CTX_R13 sc_r13
#define CTX_R14 sc_r14
#define CTX_R15 sc_r15
#define CTX_RIP sc_rip
#else
// No context definition for architecture
#endif
#elif defined(__NetBSD__)
#if PPSSPP_ARCH(AMD64)
#include <ucontext.h>
typedef mcontext_t SContext;
#define MACHINE_CONTEXT_SUPPORTED
#define CTX_RAX __gregs[_REG_RAX]
#define CTX_RBX __gregs[_REG_RBX]
#define CTX_RCX __gregs[_REG_RCX]
#define CTX_RDX __gregs[_REG_RDX]
#define CTX_RDI __gregs[_REG_RDI]
#define CTX_RSI __gregs[_REG_RSI]
#define CTX_RBP __gregs[_REG_RBP]
#define CTX_RSP __gregs[_REG_RSP]
#define CTX_R8 __gregs[_REG_R8]
#define CTX_R9 __gregs[_REG_R9]
#define CTX_R10 __gregs[_REG_R10]
#define CTX_R11 __gregs[_REG_R11]
#define CTX_R12 __gregs[_REG_R12]
#define CTX_R13 __gregs[_REG_R13]
#define CTX_R14 __gregs[_REG_R14]
#define CTX_R15 __gregs[_REG_R15]
#define CTX_RIP __gregs[_REG_RIP]
#else
// No context definition for architecture
#endif
#elif defined(__FreeBSD__)
#if PPSSPP_ARCH(AMD64)
#include <ucontext.h>
typedef mcontext_t SContext;
#define MACHINE_CONTEXT_SUPPORTED
#define CTX_RAX mc_rax
#define CTX_RBX mc_rbx
#define CTX_RCX mc_rcx
#define CTX_RDX mc_rdx
#define CTX_RDI mc_rdi
#define CTX_RSI mc_rsi
#define CTX_RBP mc_rbp
#define CTX_RSP mc_rsp
#define CTX_R8 mc_r8
#define CTX_R9 mc_r9
#define CTX_R10 mc_r10
#define CTX_R11 mc_r11
#define CTX_R12 mc_r12
#define CTX_R13 mc_r13
#define CTX_R14 mc_r14
#define CTX_R15 mc_r15
#define CTX_RIP mc_rip
#else
// No context definition for architecture
#endif
#elif defined(__HAIKU__)
#if PPSSPP_ARCH(AMD64)
#include <signal.h>
typedef mcontext_t SContext;
#define MACHINE_CONTEXT_SUPPORTED
#define CTX_RAX rax
#define CTX_RBX rbx
#define CTX_RCX rcx
#define CTX_RDX rdx
#define CTX_RDI rdi
#define CTX_RSI rsi
#define CTX_RBP rbp
#define CTX_RSP rsp
#define CTX_R8 r8
#define CTX_R9 r9
#define CTX_R10 r10
#define CTX_R11 r11
#define CTX_R12 r12
#define CTX_R13 r13
#define CTX_R14 r14
#define CTX_R15 r15
#define CTX_RIP rip
#else
// No context definition for machine
#endif
#else
// No context definition for OS
#endif
#ifdef MACHINE_CONTEXT_SUPPORTED
#if PPSSPP_ARCH(AMD64)
#include <cstdint>
#include <stddef.h>
#define CTX_PC CTX_RIP
static inline uint64_t *ContextRN(SContext* ctx, int n) {
static const uint8_t offsets[] = {
offsetof(SContext, CTX_RAX), offsetof(SContext, CTX_RCX), offsetof(SContext, CTX_RDX),
offsetof(SContext, CTX_RBX), offsetof(SContext, CTX_RSP), offsetof(SContext, CTX_RBP),
offsetof(SContext, CTX_RSI), offsetof(SContext, CTX_RDI), offsetof(SContext, CTX_R8),
offsetof(SContext, CTX_R9), offsetof(SContext, CTX_R10), offsetof(SContext, CTX_R11),
offsetof(SContext, CTX_R12), offsetof(SContext, CTX_R13), offsetof(SContext, CTX_R14),
offsetof(SContext, CTX_R15)};
return (uint64_t *)((char *)ctx + offsets[n]);
}
#elif PPSSPP_ARCH(X86)
#include <cstdint>
#include <stddef.h>
#define CTX_PC CTX_RIP
static inline uint32_t *ContextRN(SContext* ctx, int n) {
static const uint8_t offsets[] = {
offsetof(SContext, CTX_RAX), offsetof(SContext, CTX_RCX), offsetof(SContext, CTX_RDX),
offsetof(SContext, CTX_RBX), offsetof(SContext, CTX_RSP), offsetof(SContext, CTX_RBP),
offsetof(SContext, CTX_RSI), offsetof(SContext, CTX_RDI)};
return (uint32_t *)((char*)ctx + offsets[n]);
}
#endif // arch
#endif // MACHINE_CONTEXT_SUPPORTED