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