Repository navigation
Expand file tree
/
Copy pathcommon.cpp
More file actions
392 lines (320 loc) · 9.97 KB
/
Copy pathcommon.cpp
File metadata and controls
392 lines (320 loc) · 9.97 KB
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
This file is part of libswirl
*/
#include "license/bsd"
#include "types.h"
#include "cfg/cfg.h"
#include <pthread.h>
#if HOST_OS==OS_LINUX || HOST_OS == OS_DARWIN
#if HOST_OS == OS_DARWIN
#define _XOPEN_SOURCE 1
#define __USE_GNU 1
#include <TargetConditionals.h>
#include <dlfcn.h>
#include <util.h>
#endif
#if FEAT_HAS_SERIAL_TTY
#include <sys/stat.h>
#if HOST_OS == OS_DARWIN
#import <util.h>
#else
#include <pty.h>
#endif
#endif
#include <fcntl.h>
#include <semaphore.h>
#include <stdarg.h>
#include <signal.h>
#include <sys/param.h>
#include <sys/mman.h>
#include <sys/time.h>
#if !defined(TARGET_BSD) && !defined(TARGET_IPHONE) && !defined(TARGET_NACL32) && !defined(TARGET_EMSCRIPTEN) && !defined(TARGET_OSX) && !defined(TARGET_OSX_X64)
#include <sys/personality.h>
#include <dlfcn.h>
#endif
#include <unistd.h>
#include "hw/sh4/dyna/blockmanager.h"
#include "linux/context.h"
#include "hw/sh4/dyna/ngen.h"
#include "hw/mem/_vmem.h"
// #include "rend/TexCache.h"
#include "libswirl.h"
#define fault_printf(...)
#if !defined(TARGET_NO_EXCEPTIONS)
#if HOST_OS == OS_DARWIN
void sigill_handler(int sn, siginfo_t * si, void *segfault_ctx) {
rei_host_context_t ctx;
context_from_segfault(&ctx);
unat pc = (unat)ctx.pc;
bool dyna_cde = (pc>(unat)CodeCache) && (pc<(unat)(CodeCache + CODE_SIZE));
printf("SIGILL @ %lx -> %p was not in vram, dynacode:%d\n", pc, si->si_addr, dyna_cde);
//printf("PC is used here %08X\n", pc);
kill(getpid(), SIGABRT);
}
#endif
#define TRAP_STACK_SIZE (1024 * 1024)
static u8* trap_ptr_fault;
static u8* trap_ptr_stack;
static u8* trap_ptr_stack_top;
static unat trap_si_addr;
static unat trap_pc;
static bool trap_handled = true;
static bool in_handler = false;
u8* generic_fault_handler ()
{
fault_printf("generic_fault_handler\n");
rei_host_context_t ctx;
fault_printf("generic_fault_handler\n");
context_from_segfault(&ctx);
fault_printf("generic_fault_handler: original pc: %p, trap pc: %p\n", ctx.pc, trap_pc);
ctx.pc = trap_pc;
fault_printf("generic_fault_handler\n");
if (virtualDreamcast->HandleFault(trap_si_addr, &ctx))
{
fault_printf("generic_fault_handler: final pc: %p, trap pc: %p\n", ctx.pc, trap_pc);
context_to_segfault(&ctx);
fault_printf("generic_fault_handler: handled\n");
trap_handled = true;
}
else
{
fault_printf("generic_fault_handler: not in handled SIGSEGV pc: %lx addr:%p here %p\n", ctx.pc, (void*)trap_si_addr, (void*)generic_fault_handler);
trap_handled = false;
}
fault_printf("generic_fault_handler: end\n");
return trap_ptr_fault;
}
naked void re_raise_fault()
{
__asm__ __volatile__(
#if HOST_CPU == CPU_X86
"movl %0, %%esp\n"
"call *%1\n"
"movb $0, (%%eax)"
#elif HOST_CPU == CPU_X64
"movq %0, %%rsp\n"
"call *%1\n"
"movb $0, (%%rax)"
#elif HOST_CPU == CPU_ARM
"mov sp, %0\n"
"blx %1\n"
"ldr r1, [r0]"
#elif HOST_CPU == CPU_ARM64
"mov sp, %0\n"
"blr %1\n"
"ldr w1, [x0]"
#else
#error missing cpu
#endif
: : "r"(trap_ptr_stack_top), "r"(generic_fault_handler) : "memory"
);
}
bool break_segfault = false;
static void fatal_error()
{
while(!break_segfault)
{
printf("fault_handler: Blocking before restoring default SIGSEGV handler\n");
sleep(1);
}
signal(SIGSEGV, SIG_DFL);
}
static void fault_handler (int sn, siginfo_t * si, void *segfault_ctx)
{
fault_printf("fault_handler: thread: %p si_addr:%p trap_ptr_fault: %p\n", (void*)pthread_self(), si->si_addr, trap_ptr_fault);
if ((unat)si->si_addr == (unat)trap_ptr_fault)
{
if (!in_handler) {
fault_printf("fault_handler: !in_handler\n");
fatal_error();
}
segfault_load(segfault_ctx);
rei_host_context_t ctx;
context_from_segfault(&ctx);
in_handler = false;
fault_printf("fault_handler:resume %p\n", ctx.pc);
}
else
{
if (in_handler)
{
fault_printf("fault_handler: double fault\n");
fatal_error();
}
in_handler = true;
if (!trap_handled)
{
fault_printf("fault_handler: trap not handled\n");
fatal_error();
}
else
{
fault_printf("fault_handler:catch -> thunking to %p\n", trap_pc);
trap_si_addr = (unat)si->si_addr;
segfault_store(segfault_ctx);
segfault_set_pc(segfault_ctx, (unat)&re_raise_fault, &trap_pc);
}
}
fault_printf("fault_handler exit\n");
}
typedef int sigaction_fn(int signum, const struct sigaction *act,
struct sigaction *oldact);
static sigaction_fn* get_sigaction()
{
// void* libc;
// sigaction_fn* rv = nullptr;
// libc = dlopen("libc.so", RTLD_NOLOAD);
// if (libc)
// {
// printf("get_sigaction: found libc.so\n");
// *(void**) (&rv) = dlsym(libc, "sigaction");
// if (rv && rv != sigaction) {
// printf("get_sigaction: libc override detected, working around...\n");
// return rv;
// }
// }
return &sigaction;
}
static void install_fault_handler(void)
{
// initialize trap state
trap_ptr_fault = (u8*)mmap(0, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
trap_ptr_stack = (u8*)mmap(0, TRAP_STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
trap_ptr_stack_top = trap_ptr_stack + TRAP_STACK_SIZE - 128;
fault_printf("trap_ptr_fault %p\n", trap_ptr_fault);
struct sigaction act, segv_oact;
memset(&act, 0, sizeof(act));
act.sa_sigaction = fault_handler;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_SIGINFO;
auto libc_sigaction = get_sigaction();
libc_sigaction(SIGSEGV, &act, &segv_oact);
#if HOST_OS == OS_DARWIN
//this is broken on osx/ios/mach in general
libc_sigaction(SIGBUS, &act, &segv_oact);
act.sa_sigaction = sigill_handler;
libc_sigaction(SIGILL, &act, &segv_oact);
#endif
}
#else // !defined(TARGET_NO_EXCEPTIONS)
// No exceptions/nvmem dummy handlers.
void install_fault_handler(void) {}
#endif // !defined(TARGET_NO_EXCEPTIONS)
#include <errno.h>
double os_GetSeconds()
{
timeval a;
gettimeofday (&a,0);
static u64 tvs_base=a.tv_sec;
return a.tv_sec-tvs_base+a.tv_usec/1000000.0;
}
#if TARGET_IPHONE
void os_DebugBreak() {
__asm__("trap");
}
#elif HOST_OS != OS_LINUX
void os_DebugBreak()
{
__builtin_trap();
}
#endif
void enable_runfast()
{
#if HOST_CPU==CPU_ARM && !defined(ARMCC)
static const unsigned int x = 0x04086060;
static const unsigned int y = 0x03000000;
int r;
asm volatile (
"fmrx %0, fpscr \n\t" //r0 = FPSCR
"and %0, %0, %1 \n\t" //r0 = r0 & 0x04086060
"orr %0, %0, %2 \n\t" //r0 = r0 | 0x03000000
"fmxr fpscr, %0 \n\t" //FPSCR = r0
: "=r"(r)
: "r"(x), "r"(y)
);
printf("ARM VFP-Run Fast (NFP) enabled !\n");
#endif
}
void linux_fix_personality() {
#if !defined(TARGET_BSD) && !defined(_ANDROID) && !defined(TARGET_OS_MAC) && !defined(TARGET_NACL32) && !defined(TARGET_EMSCRIPTEN)
printf("Personality: %08X\n", personality(0xFFFFFFFF));
personality(~READ_IMPLIES_EXEC & personality(0xFFFFFFFF));
printf("Updated personality: %08X\n", personality(0xFFFFFFFF));
#endif
}
void linux_rpi2_init() {
#if !defined(TARGET_BSD) && !defined(_ANDROID) && !defined(TARGET_NACL32) && !defined(TARGET_EMSCRIPTEN) && defined(TARGET_VIDEOCORE)
void* handle;
void (*rpi_bcm_init)(void);
handle = dlopen("libbcm_host.so", RTLD_LAZY);
if (handle) {
printf("found libbcm_host\n");
*(void**) (&rpi_bcm_init) = dlsym(handle, "bcm_host_init");
if (rpi_bcm_init) {
printf("rpi2: bcm_init\n");
rpi_bcm_init();
}
}
#endif
}
void common_linux_setup()
{
linux_fix_personality();
linux_rpi2_init();
enable_runfast();
install_fault_handler();
// signal(SIGINT, exit);
settings.profile.run_counts=0;
printf("Linux paging: %ld %08X %08X\n",sysconf(_SC_PAGESIZE),PAGE_SIZE,PAGE_MASK);
#if !defined(TARGET_EMSCRIPTEN)
verify(PAGE_MASK==(sysconf(_SC_PAGESIZE)-1));
#endif
}
#if FEAT_HAS_SERIAL_TTY
int pty_master = -1;
bool common_serial_pty_setup() {
bool cleanup_pty_symlink = false;
if (settings.debug.VirtualSerialPort) {
int slave;
char slave_name[2048];
pty_master = -1;
if (openpty(&pty_master, &slave, slave_name, nullptr, nullptr) >= 0) {
// Set the port to be non-blocking
int flags = fcntl(pty_master, F_GETFL, 0);
fcntl(pty_master, F_SETFL, flags | O_NONBLOCK);
// Turn ECHO off, we don't want to loop-back
struct termios tp;
tcgetattr(pty_master, &tp);
tp.c_lflag &= ~ECHO;
tcsetattr(pty_master, TCSAFLUSH, &tp);
printf("Serial: Created virtual serial port at %s\n", slave_name);
if (settings.debug.VirtualSerialPortFile.size()) {
const char *file_name = settings.debug.VirtualSerialPortFile.c_str();
// Check if a symlink already exists at this location, and if so, delete it so we can make a new one
struct stat stat_buffer;
if (lstat(file_name, &stat_buffer) == 0 && S_ISLNK(stat_buffer.st_mode)) {
// File is a symlink, so delete it
if (remove(file_name) == 0) {
printf("Serial: Removed existing symlink at %s\n", file_name);
} else {
printf("Serial: Failed to remove existing symlink at %s, %s\n", file_name, strerror(errno));
}
}
if (symlink(slave_name, file_name) == 0) {
cleanup_pty_symlink = true;
printf("Serial: Created symlink to %s\n", file_name);
} else {
printf("Serial: Failed to create symlink to %s, %s\n", file_name, strerror(errno));
}
}
// not for us to use, we use master
// do not close to avoid EIO though
// close(slave);
} else {
printf("Serial: Failed to create PTY: %s\n", strerror(errno));
}
}
return cleanup_pty_symlink;
}
#endif
#endif