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