Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-ports
Path: blob/main/emulators/amiberry/files/patch-src_osdep_sigsegv__handler.cpp
46590 views
1
--- src/osdep/sigsegv_handler.cpp.orig 2026-04-26 06:59:39 UTC
2
+++ src/osdep/sigsegv_handler.cpp
3
@@ -63,6 +63,9 @@ void init_max_signals()
4
#else
5
#include <sys/ucontext.h>
6
#endif
7
+#if defined(__FreeBSD__) && defined(CPU_AARCH64)
8
+#include <machine/ucontext.h>
9
+#endif
10
#include <csignal>
11
#include <dlfcn.h>
12
13
@@ -154,8 +157,10 @@ static int handle_exception(mcontext_t sigcont, long f
14
#endif
15
{
16
int handled = HANDLE_EXCEPTION_NONE;
17
+#if defined(__FreeBSD__) && defined(CPU_AARCH64)
18
+ auto fault_pc = static_cast<uintptr>(sigcont->mc_gpregs.gp_elr);
19
+#elif !defined(__MACH__)
20
// Mac OS X struct for this is different
21
-#ifndef __MACH__
22
auto fault_pc = static_cast<uintptr>(sigcont->pc);
23
#else
24
auto fault_pc = static_cast<uintptr>(sigcont->__ss.__pc);
25
@@ -199,7 +204,9 @@ static int handle_exception(mcontext_t sigcont, long f
26
// Check for stupid RAM detection of kickstart
27
if (a3000lmem_bank.allocated_size > 0 && amiga_addr >= a3000lmem_bank.start - 0x00100000 && amiga_addr < a3000lmem_bank.start - 0x00100000 + 8) {
28
output_log(_T(" Stupid kickstart detection for size of ramsey_low at 0x%08x.\n"), amiga_addr);
29
-#ifndef __MACH__
30
+#if defined(__FreeBSD__) && defined(CPU_AARCH64)
31
+ sigcont->mc_gpregs.gp_elr += 4;
32
+#elif !defined(__MACH__)
33
sigcont->pc += 4;
34
#else
35
sigcont->__ss.__pc += 4;
36
@@ -211,7 +218,9 @@ static int handle_exception(mcontext_t sigcont, long f
37
// Check for stupid RAM detection of kickstart
38
if (a3000hmem_bank.allocated_size > 0 && amiga_addr >= a3000hmem_bank.start + a3000hmem_bank.allocated_size && amiga_addr < a3000hmem_bank.start + a3000hmem_bank.allocated_size + 8) {
39
output_log(_T(" Stupid kickstart detection for size of ramsey_high at 0x%08x.\n"), amiga_addr);
40
-#ifndef __MACH__
41
+#if defined(__FreeBSD__) && defined(CPU_AARCH64)
42
+ sigcont->mc_gpregs.gp_elr += 4;
43
+#elif !defined(__MACH__)
44
sigcont->pc += 4;
45
#else
46
sigcont->__ss.__pc += 4;
47
@@ -241,43 +250,58 @@ static int handle_exception(mcontext_t sigcont, long f
48
transfer_type_t transfer_type = TYPE_UNKNOWN;
49
int transfer_size = SIZE_UNKNOWN;
50
51
- const auto get_reg_w = [&](const int reg) -> uae_u32 {
52
- if (reg == 31)
53
- return 0;
54
-#ifndef __MACH__
55
- return static_cast<uae_u32>(sigcont->regs[reg]);
56
+ const auto get_reg_w = [&](const int reg) -> uae_u32 {
57
+ if (reg == 31)
58
+ return 0;
59
+#if defined(__FreeBSD__) && defined(CPU_AARCH64)
60
+ if (reg < 30)
61
+ return static_cast<uae_u32>(sigcont->mc_gpregs.gp_x[reg]);
62
+ return static_cast<uae_u32>(sigcont->mc_gpregs.gp_lr);
63
+#elif !defined(__MACH__)
64
+ return static_cast<uae_u32>(sigcont->regs[reg]);
65
#else
66
- return static_cast<uae_u32>(sigcont->__ss.__x[reg]);
67
+ return static_cast<uae_u32>(sigcont->__ss.__x[reg]);
68
#endif
69
- };
70
- const auto get_reg_x = [&](const int reg) -> uae_u64 {
71
- if (reg == 31)
72
- return 0;
73
-#ifndef __MACH__
74
- return static_cast<uae_u64>(sigcont->regs[reg]);
75
+ };
76
+ const auto get_reg_x = [&](const int reg) -> uae_u64 {
77
+ if (reg == 31)
78
+ return 0;
79
+#if defined(__FreeBSD__) && defined(CPU_AARCH64)
80
+ if (reg < 30)
81
+ return static_cast<uae_u64>(sigcont->mc_gpregs.gp_x[reg]);
82
+ return static_cast<uae_u64>(sigcont->mc_gpregs.gp_lr);
83
+#elif !defined(__MACH__)
84
+ return static_cast<uae_u64>(sigcont->regs[reg]);
85
#else
86
- return static_cast<uae_u64>(sigcont->__ss.__x[reg]);
87
+ return static_cast<uae_u64>(sigcont->__ss.__x[reg]);
88
#endif
89
- };
90
- const auto get_base_x = [&](const int reg) -> uae_u64 {
91
- if (reg == 31) {
92
-#ifndef __MACH__
93
- return static_cast<uae_u64>(sigcont->sp);
94
+ };
95
+ const auto get_base_x = [&](const int reg) -> uae_u64 {
96
+ if (reg == 31) {
97
+#if defined(__FreeBSD__) && defined(CPU_AARCH64)
98
+ return static_cast<uae_u64>(sigcont->mc_gpregs.gp_sp);
99
+#elif !defined(__MACH__)
100
+ return static_cast<uae_u64>(sigcont->sp);
101
#else
102
- return static_cast<uae_u64>(sigcont->__ss.__sp);
103
+ return static_cast<uae_u64>(sigcont->__ss.__sp);
104
#endif
105
- }
106
- return get_reg_x(reg);
107
- };
108
- const auto set_reg_w = [&](const int reg, const uae_u32 value) {
109
- if (reg == 31)
110
- return;
111
-#ifndef __MACH__
112
- sigcont->regs[reg] = value;
113
+ }
114
+ return get_reg_x(reg);
115
+ };
116
+ const auto set_reg_w = [&](const int reg, const uae_u32 value) {
117
+ if (reg == 31)
118
+ return;
119
+#if defined(__FreeBSD__) && defined(CPU_AARCH64)
120
+ if (reg < 30)
121
+ sigcont->mc_gpregs.gp_x[reg] = value;
122
+ else
123
+ sigcont->mc_gpregs.gp_lr = value;
124
+#elif !defined(__MACH__)
125
+ sigcont->regs[reg] = value;
126
#else
127
- sigcont->__ss.__x[reg] = value;
128
+ sigcont->__ss.__x[reg] = value;
129
#endif
130
- };
131
+ };
132
133
// Decode memory operands for additional diagnostics.
134
const int rd = opcode & 0x1f;
135
@@ -489,7 +513,9 @@ static int handle_exception(mcontext_t sigcont, long f
136
}
137
138
// Go to next instruction
139
-#ifndef __MACH__
140
+#if defined(__FreeBSD__) && defined(CPU_AARCH64)
141
+ sigcont->mc_gpregs.gp_elr += 4;
142
+#elif !defined(__MACH__)
143
sigcont->pc += 4;
144
#else
145
sigcont->__ss.__pc += 4;
146
@@ -557,10 +583,14 @@ void signal_segv(int signum, siginfo_t* info, void* pt
147
148
#ifndef __MACH__
149
mcontext_t* context = &(ucontext->uc_mcontext);
150
- unsigned long long* regs = context->regs;
151
+#if defined(__FreeBSD__) && defined(CPU_AARCH64)
152
+ [[maybe_unused]] unsigned long long* regs = reinterpret_cast<unsigned long long*>(context->mc_gpregs.gp_x);
153
#else
154
+ [[maybe_unused]] unsigned long long* regs = context->regs;
155
+#endif
156
+#else
157
mcontext_t context = ucontext->uc_mcontext;
158
- unsigned long long* regs = context->__ss.__x;
159
+ [[maybe_unused]] unsigned long long* regs = context->__ss.__x;
160
#endif
161
162
const auto addr = reinterpret_cast<uintptr>(info->si_addr);
163
@@ -585,13 +615,24 @@ void signal_segv(int signum, siginfo_t* info, void* pt
164
if (signum == 4)
165
output_log(_T(" value = 0x%08x\n"), *static_cast<uae_u32*>(info->si_addr));
166
167
- for (int i = 0; i < 31; ++i)
168
-#ifndef __MACH__
169
- output_log(_T("x%02d = 0x%016llx\n"), i, ucontext->uc_mcontext.regs[i]);
170
-#else
171
- output_log(_T("x%02d = 0x%016llx\n"), i, context->__ss.__x[i]);
172
-#endif
173
-#ifndef __MACH__
174
+ #if defined(__FreeBSD__) && defined(CPU_AARCH64)
175
+ for (int i = 0; i < 30; ++i)
176
+ output_log(_T("x%02d = 0x%016llx\n"), i, ucontext->uc_mcontext.mc_gpregs.gp_x[i]);
177
+ output_log(_T("x30 = 0x%016llx\n"), ucontext->uc_mcontext.mc_gpregs.gp_lr);
178
+ #elif !defined(__MACH__)
179
+ for (int i = 0; i < 31; ++i)
180
+ output_log(_T("x%02d = 0x%016llx\n"), i, ucontext->uc_mcontext.regs[i]);
181
+ #else
182
+ for (int i = 0; i < 31; ++i)
183
+ output_log(_T("x%02d = 0x%016llx\n"), i, context->__ss.__x[i]);
184
+ #endif
185
+#if defined(__FreeBSD__) && defined(CPU_AARCH64)
186
+ output_log(_T("SP = 0x%016llx\n"), ucontext->uc_mcontext.mc_gpregs.gp_sp);
187
+ output_log(_T("PC = 0x%016llx\n"), ucontext->uc_mcontext.mc_gpregs.gp_elr);
188
+ output_log(_T("Fault Address = 0x%016llx\n"), static_cast<unsigned long long>(reinterpret_cast<uintptr_t>(info->si_addr)));
189
+ output_log(_T("SPSR = 0x%016llx\n"), static_cast<unsigned long long>(ucontext->uc_mcontext.mc_gpregs.gp_spsr));
190
+ void* getaddr = reinterpret_cast<void*>(ucontext->uc_mcontext.mc_gpregs.gp_lr);
191
+#elif !defined(__MACH__)
192
output_log(_T("SP = 0x%016llx\n"), ucontext->uc_mcontext.sp);
193
output_log(_T("PC = 0x%016llx\n"), ucontext->uc_mcontext.pc);
194
output_log(_T("Fault Address = 0x%016llx\n"), ucontext->uc_mcontext.fault_address);
195
@@ -672,10 +713,14 @@ void signal_buserror(int signum, siginfo_t* info, void
196
197
#ifndef __MACH__
198
mcontext_t* context = &(ucontext->uc_mcontext);
199
- unsigned long long* regs = context->regs;
200
+#if defined(__FreeBSD__) && defined(CPU_AARCH64)
201
+ [[maybe_unused]] unsigned long long* regs = reinterpret_cast<unsigned long long*>(context->mc_gpregs.gp_x);
202
#else
203
+ [[maybe_unused]] unsigned long long* regs = context->regs;
204
+#endif
205
+#else
206
mcontext_t context = ucontext->uc_mcontext;
207
- unsigned long long* regs = context->__ss.__x;
208
+ [[maybe_unused]] unsigned long long* regs = context->__ss.__x;
209
#endif
210
211
auto addr = reinterpret_cast<uintptr_t>(info->si_addr);
212
@@ -695,8 +740,17 @@ void signal_buserror(int signum, siginfo_t* info, void
213
if (signum == 4)
214
output_log(_T(" value = 0x%08x\n"), *static_cast<uae_u32*>(info->si_addr));
215
216
+ #if defined(__FreeBSD__) && defined(CPU_AARCH64)
217
+ for (int i = 0; i < 30; ++i)
218
+ output_log(_T("x%02d = 0x%016llx\n"), i, ucontext->uc_mcontext.mc_gpregs.gp_x[i]);
219
+ output_log(_T("x30 = 0x%016llx\n"), ucontext->uc_mcontext.mc_gpregs.gp_lr);
220
+ output_log(_T("SP = 0x%016llx\n"), ucontext->uc_mcontext.mc_gpregs.gp_sp);
221
+ output_log(_T("PC = 0x%016llx\n"), ucontext->uc_mcontext.mc_gpregs.gp_elr);
222
+ output_log(_T("Fault Address = 0x%016llx\n"), static_cast<unsigned long long>(reinterpret_cast<uintptr_t>(info->si_addr)));
223
+ output_log(_T("SPSR = 0x%016llx\n"), static_cast<unsigned long long>(ucontext->uc_mcontext.mc_gpregs.gp_spsr));
224
+ void* getaddr = reinterpret_cast<void*>(ucontext->uc_mcontext.mc_gpregs.gp_lr);
225
+ #elif !defined(__MACH__)
226
for (int i = 0; i < 31; ++i)
227
-#ifndef __MACH__
228
output_log(_T("x%02d = 0x%016llx\n"), i, ucontext->uc_mcontext.regs[i]);
229
output_log(_T("SP = 0x%016llx\n"), ucontext->uc_mcontext.sp);
230
output_log(_T("PC = 0x%016llx\n"), ucontext->uc_mcontext.pc);
231
@@ -704,7 +758,8 @@ void signal_buserror(int signum, siginfo_t* info, void
232
output_log(_T("pstate = 0x%016llx\n"), ucontext->uc_mcontext.pstate);
233
234
void* getaddr = (void*)ucontext->uc_mcontext.regs[30];
235
-#else
236
+ #else
237
+ for (int i = 0; i < 31; ++i)
238
output_log(_T("x%02d = 0x%016llx\n"), i, context->__ss.__x[i]);
239
output_log(_T("SP = 0x%016llx\n"), context->__ss.__sp);
240
output_log(_T("PC = 0x%016llx\n"), context->__ss.__pc);
241
242