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