1 |
|
/* |
2 |
|
* main_unix.cpp - Emulation core, Unix implementation |
3 |
|
* |
4 |
< |
* SheepShaver (C) 1997-2002 Christian Bauer and Marc Hellwig |
4 |
> |
* SheepShaver (C) 1997-2004 Christian Bauer and Marc Hellwig |
5 |
|
* |
6 |
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
* it under the terms of the GNU General Public License as published by |
65 |
|
* ExecutePPC (or any function that might cause a mode switch). The signal |
66 |
|
* stack is restored before exiting the SIGUSR2 handler. |
67 |
|
* |
68 |
+ |
* There is apparently another problem when processing signals. In |
69 |
+ |
* fullscreen mode, we get quick updates of the mouse position. This |
70 |
+ |
* causes an increased number of calls to TriggerInterrupt(). And, |
71 |
+ |
* since IRQ_NEST is not fully handled atomically, nested calls to |
72 |
+ |
* ppc_interrupt() may cause stack corruption to eventually crash the |
73 |
+ |
* emulator. |
74 |
+ |
* |
75 |
+ |
* FIXME: |
76 |
+ |
* The current solution is to allocate another signal stack when |
77 |
+ |
* processing ppc_interrupt(). However, it may be better to detect |
78 |
+ |
* the INTFLAG_ADB case and handle it specifically with some extra mutex? |
79 |
+ |
* |
80 |
|
* TODO: |
81 |
|
* check if SIGSEGV handler works for all registers (including FP!) |
82 |
|
*/ |
121 |
|
#include "user_strings.h" |
122 |
|
#include "vm_alloc.h" |
123 |
|
#include "sigsegv.h" |
124 |
+ |
#include "thunks.h" |
125 |
|
|
126 |
|
#define DEBUG 0 |
127 |
|
#include "debug.h" |
128 |
|
|
129 |
|
|
130 |
+ |
#ifdef USE_SDL |
131 |
+ |
#include <SDL.h> |
132 |
+ |
#endif |
133 |
+ |
|
134 |
+ |
#ifndef USE_SDL_VIDEO |
135 |
|
#include <X11/Xlib.h> |
136 |
+ |
#endif |
137 |
|
|
138 |
|
#ifdef ENABLE_GTK |
139 |
|
#include <gtk/gtk.h> |
150 |
|
#endif |
151 |
|
|
152 |
|
|
153 |
+ |
// Enable emulation of unaligned lmw/stmw? |
154 |
+ |
#define EMULATE_UNALIGNED_LOADSTORE_MULTIPLE 1 |
155 |
+ |
|
156 |
|
// Enable Execute68k() safety checks? |
157 |
|
#define SAFE_EXEC_68K 0 |
158 |
|
|
162 |
|
// Interrupts in native mode? |
163 |
|
#define INTERRUPTS_IN_NATIVE_MODE 1 |
164 |
|
|
165 |
+ |
// Number of alternate stacks for signal handlers? |
166 |
+ |
#define SIG_STACK_COUNT 4 |
167 |
+ |
|
168 |
|
|
169 |
|
// Constants |
170 |
|
const char ROM_FILE_NAME[] = "ROM"; |
171 |
|
const char ROM_FILE_NAME2[] = "Mac OS ROM"; |
172 |
|
|
173 |
< |
const uint32 RAM_BASE = 0x20000000; // Base address of RAM |
173 |
> |
const uintptr RAM_BASE = 0x20000000; // Base address of RAM |
174 |
|
const uint32 SIG_STACK_SIZE = 0x10000; // Size of signal stack |
175 |
|
|
176 |
|
|
177 |
|
#if !EMULATED_PPC |
153 |
– |
// Structure in which registers are saved in a signal handler; |
154 |
– |
// sigcontext->regs points to it |
155 |
– |
// (see arch/ppc/kernel/signal.c) |
156 |
– |
typedef struct { |
157 |
– |
uint32 u[4]; |
158 |
– |
} __attribute((aligned(16))) vector128; |
159 |
– |
#include <linux/elf.h> |
160 |
– |
|
178 |
|
struct sigregs { |
179 |
< |
elf_gregset_t gp_regs; // Identical to pt_regs |
180 |
< |
double fp_regs[ELF_NFPREG]; // f0..f31 and fpsrc |
181 |
< |
//more (uninteresting) stuff following here |
179 |
> |
uint32 nip; |
180 |
> |
uint32 link; |
181 |
> |
uint32 ctr; |
182 |
> |
uint32 msr; |
183 |
> |
uint32 xer; |
184 |
> |
uint32 ccr; |
185 |
> |
uint32 gpr[32]; |
186 |
> |
}; |
187 |
> |
|
188 |
> |
#if defined(__linux__) |
189 |
> |
#include <sys/ucontext.h> |
190 |
> |
#define MACHINE_REGISTERS(scp) ((machine_regs *)(((ucontext_t *)scp)->uc_mcontext.regs)) |
191 |
> |
|
192 |
> |
struct machine_regs : public pt_regs |
193 |
> |
{ |
194 |
> |
u_long & cr() { return pt_regs::ccr; } |
195 |
> |
uint32 cr() const { return pt_regs::ccr; } |
196 |
> |
uint32 lr() const { return pt_regs::link; } |
197 |
> |
uint32 ctr() const { return pt_regs::ctr; } |
198 |
> |
uint32 xer() const { return pt_regs::xer; } |
199 |
> |
uint32 msr() const { return pt_regs::msr; } |
200 |
> |
uint32 dar() const { return pt_regs::dar; } |
201 |
> |
u_long & pc() { return pt_regs::nip; } |
202 |
> |
uint32 pc() const { return pt_regs::nip; } |
203 |
> |
u_long & gpr(int i) { return pt_regs::gpr[i]; } |
204 |
> |
uint32 gpr(int i) const { return pt_regs::gpr[i]; } |
205 |
|
}; |
206 |
|
#endif |
207 |
|
|
208 |
+ |
#if defined(__APPLE__) && defined(__MACH__) |
209 |
+ |
#include <sys/signal.h> |
210 |
+ |
extern "C" int sigaltstack(const struct sigaltstack *ss, struct sigaltstack *oss); |
211 |
+ |
|
212 |
+ |
#include <sys/ucontext.h> |
213 |
+ |
#define MACHINE_REGISTERS(scp) ((machine_regs *)(((ucontext_t *)scp)->uc_mcontext)) |
214 |
+ |
|
215 |
+ |
struct machine_regs : public mcontext |
216 |
+ |
{ |
217 |
+ |
uint32 & cr() { return ss.cr; } |
218 |
+ |
uint32 cr() const { return ss.cr; } |
219 |
+ |
uint32 lr() const { return ss.lr; } |
220 |
+ |
uint32 ctr() const { return ss.ctr; } |
221 |
+ |
uint32 xer() const { return ss.xer; } |
222 |
+ |
uint32 msr() const { return ss.srr1; } |
223 |
+ |
uint32 dar() const { return es.dar; } |
224 |
+ |
uint32 & pc() { return ss.srr0; } |
225 |
+ |
uint32 pc() const { return ss.srr0; } |
226 |
+ |
uint32 & gpr(int i) { return (&ss.r0)[i]; } |
227 |
+ |
uint32 gpr(int i) const { return (&ss.r0)[i]; } |
228 |
+ |
}; |
229 |
+ |
#endif |
230 |
+ |
|
231 |
+ |
static void build_sigregs(sigregs *srp, machine_regs *mrp) |
232 |
+ |
{ |
233 |
+ |
srp->nip = mrp->pc(); |
234 |
+ |
srp->link = mrp->lr(); |
235 |
+ |
srp->ctr = mrp->ctr(); |
236 |
+ |
srp->msr = mrp->msr(); |
237 |
+ |
srp->xer = mrp->xer(); |
238 |
+ |
srp->ccr = mrp->cr(); |
239 |
+ |
for (int i = 0; i < 32; i++) |
240 |
+ |
srp->gpr[i] = mrp->gpr(i); |
241 |
+ |
} |
242 |
+ |
|
243 |
+ |
static struct sigaltstack sig_stacks[SIG_STACK_COUNT]; // Stacks for signal handlers |
244 |
+ |
static int sig_stack_id = 0; // Stack slot currently used |
245 |
+ |
|
246 |
+ |
static inline void sig_stack_acquire(void) |
247 |
+ |
{ |
248 |
+ |
if (++sig_stack_id == SIG_STACK_COUNT) { |
249 |
+ |
printf("FATAL: signal stack overflow\n"); |
250 |
+ |
return; |
251 |
+ |
} |
252 |
+ |
sigaltstack(&sig_stacks[sig_stack_id], NULL); |
253 |
+ |
} |
254 |
+ |
|
255 |
+ |
static inline void sig_stack_release(void) |
256 |
+ |
{ |
257 |
+ |
if (--sig_stack_id < 0) { |
258 |
+ |
printf("FATAL: signal stack underflow\n"); |
259 |
+ |
return; |
260 |
+ |
} |
261 |
+ |
sigaltstack(&sig_stacks[sig_stack_id], NULL); |
262 |
+ |
} |
263 |
+ |
#endif |
264 |
+ |
|
265 |
|
|
266 |
|
// Global variables (exported) |
267 |
|
#if !EMULATED_PPC |
269 |
|
#endif |
270 |
|
uint32 RAMBase; // Base address of Mac RAM |
271 |
|
uint32 RAMSize; // Size of Mac RAM |
175 |
– |
uint32 SheepStack1Base; // SheepShaver first alternate stack base |
176 |
– |
uint32 SheepStack2Base; // SheepShaver second alternate stack base |
177 |
– |
uint32 SheepThunksBase; // SheepShaver thunks base |
272 |
|
uint32 KernelDataAddr; // Address of Kernel Data |
273 |
|
uint32 BootGlobsAddr; // Address of BootGlobs structure at top of Mac RAM |
274 |
+ |
uint32 DRCacheAddr; // Address of DR Cache |
275 |
|
uint32 PVR; // Theoretical PVR |
276 |
|
int64 CPUClockSpeed; // Processor clock speed (Hz) |
277 |
|
int64 BusClockSpeed; // Bus clock speed (Hz) |
278 |
|
|
279 |
|
|
280 |
|
// Global variables |
281 |
+ |
#ifndef USE_SDL_VIDEO |
282 |
|
char *x_display_name = NULL; // X11 display name |
283 |
|
Display *x_display = NULL; // X11 display handle |
284 |
+ |
#ifdef X11_LOCK_TYPE |
285 |
+ |
X11_LOCK_TYPE x_display_lock = X11_LOCK_INIT; // X11 display lock |
286 |
+ |
#endif |
287 |
+ |
#endif |
288 |
|
|
289 |
|
static int zero_fd = 0; // FD of /dev/zero |
190 |
– |
static bool sheep_area_mapped = false; // Flag: SheepShaver data area mmap()ed |
290 |
|
static bool lm_area_mapped = false; // Flag: Low Memory area mmap()ped |
291 |
|
static int kernel_area = -1; // SHM ID of Kernel Data area |
292 |
|
static bool rom_area_mapped = false; // Flag: Mac ROM mmap()ped |
293 |
|
static bool ram_area_mapped = false; // Flag: Mac RAM mmap()ped |
294 |
+ |
static bool dr_cache_area_mapped = false; // Flag: Mac DR Cache mmap()ped |
295 |
+ |
static bool dr_emulator_area_mapped = false;// Flag: Mac DR Emulator mmap()ped |
296 |
|
static KernelData *kernel_data; // Pointer to Kernel Data |
297 |
|
static EmulatorData *emulator_data; |
298 |
|
|
299 |
|
static uint8 last_xpram[XPRAM_SIZE]; // Buffer for monitoring XPRAM changes |
300 |
|
|
301 |
|
static bool nvram_thread_active = false; // Flag: NVRAM watchdog installed |
302 |
+ |
static volatile bool nvram_thread_cancel; // Flag: Cancel NVRAM thread |
303 |
|
static pthread_t nvram_thread; // NVRAM watchdog |
304 |
|
static bool tick_thread_active = false; // Flag: MacOS thread installed |
305 |
+ |
static volatile bool tick_thread_cancel; // Flag: Cancel 60Hz thread |
306 |
|
static pthread_t tick_thread; // 60Hz thread |
307 |
|
static pthread_t emul_thread; // MacOS thread |
308 |
|
|
310 |
|
static int64 num_segv = 0; // Number of handled SEGV signals |
311 |
|
|
312 |
|
static struct sigaction sigusr2_action; // Interrupt signal (of emulator thread) |
313 |
< |
#if !EMULATED_PPC |
313 |
> |
#if EMULATED_PPC |
314 |
> |
static uintptr sig_stack = 0; // Stack for PowerPC interrupt routine |
315 |
> |
#else |
316 |
|
static struct sigaction sigsegv_action; // Data access exception signal (of emulator thread) |
317 |
|
static struct sigaction sigill_action; // Illegal instruction signal (of emulator thread) |
213 |
– |
static void *sig_stack = NULL; // Stack for signal handlers |
214 |
– |
static void *extra_stack = NULL; // Stack for SIGSEGV inside interrupt handler |
318 |
|
static bool emul_thread_fatal = false; // Flag: MacOS thread crashed, tick thread shall dump debug output |
319 |
|
static sigregs sigsegv_regs; // Register dump when crashed |
320 |
+ |
static const char *crash_reason = NULL; // Reason of the crash (SIGSEGV, SIGBUS, SIGILL) |
321 |
|
#endif |
322 |
|
|
323 |
+ |
uint32 SheepMem::page_size; // Size of a native page |
324 |
+ |
uintptr SheepMem::zero_page = 0; // Address of ro page filled in with zeros |
325 |
+ |
uintptr SheepMem::base = 0x60000000; // Address of SheepShaver data |
326 |
+ |
uintptr SheepMem::top = 0; // Top of SheepShaver data (stack like storage) |
327 |
+ |
|
328 |
|
|
329 |
|
// Prototypes |
330 |
|
static void Quit(void); |
332 |
|
static void *nvram_func(void *arg); |
333 |
|
static void *tick_func(void *arg); |
334 |
|
#if EMULATED_PPC |
226 |
– |
static void sigusr2_handler(int sig); |
335 |
|
extern void emul_ppc(uint32 start); |
336 |
|
extern void init_emul_ppc(void); |
337 |
|
extern void exit_emul_ppc(void); |
338 |
+ |
sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t); |
339 |
|
#else |
340 |
< |
static void sigusr2_handler(int sig, sigcontext_struct *sc); |
341 |
< |
static void sigsegv_handler(int sig, sigcontext_struct *sc); |
342 |
< |
static void sigill_handler(int sig, sigcontext_struct *sc); |
340 |
> |
static void sigusr2_handler(int sig, siginfo_t *sip, void *scp); |
341 |
> |
static void sigsegv_handler(int sig, siginfo_t *sip, void *scp); |
342 |
> |
static void sigill_handler(int sig, siginfo_t *sip, void *scp); |
343 |
|
#endif |
344 |
|
|
345 |
|
|
361 |
|
|
362 |
|
#if EMULATED_PPC |
363 |
|
/* |
364 |
+ |
* Return signal stack base |
365 |
+ |
*/ |
366 |
+ |
|
367 |
+ |
uintptr SignalStackBase(void) |
368 |
+ |
{ |
369 |
+ |
return sig_stack + SIG_STACK_SIZE; |
370 |
+ |
} |
371 |
+ |
|
372 |
+ |
|
373 |
+ |
/* |
374 |
|
* Atomic operations |
375 |
|
*/ |
376 |
|
|
461 |
|
for (int i=1; i<argc; i++) { |
462 |
|
if (strcmp(argv[i], "--help") == 0) { |
463 |
|
usage(argv[0]); |
464 |
+ |
#ifndef USE_SDL_VIDEO |
465 |
|
} else if (strcmp(argv[i], "--display") == 0) { |
466 |
|
i++; |
467 |
|
if (i < argc) |
468 |
|
x_display_name = strdup(argv[i]); |
469 |
+ |
#endif |
470 |
|
} else if (argv[i][0] == '-') { |
471 |
|
fprintf(stderr, "Unrecognized option '%s'\n", argv[i]); |
472 |
|
usage(argv[0]); |
473 |
|
} |
474 |
|
} |
475 |
|
|
476 |
+ |
#ifdef USE_SDL |
477 |
+ |
// Initialize SDL system |
478 |
+ |
int sdl_flags = 0; |
479 |
+ |
#ifdef USE_SDL_VIDEO |
480 |
+ |
sdl_flags |= SDL_INIT_VIDEO; |
481 |
+ |
#endif |
482 |
+ |
assert(sdl_flags != 0); |
483 |
+ |
if (SDL_Init(sdl_flags) == -1) { |
484 |
+ |
char str[256]; |
485 |
+ |
sprintf(str, "Could not initialize SDL: %s.\n", SDL_GetError()); |
486 |
+ |
ErrorAlert(str); |
487 |
+ |
goto quit; |
488 |
+ |
} |
489 |
+ |
atexit(SDL_Quit); |
490 |
+ |
#endif |
491 |
+ |
|
492 |
+ |
#ifndef USE_SDL_VIDEO |
493 |
|
// Open display |
494 |
|
x_display = XOpenDisplay(x_display_name); |
495 |
|
if (x_display == NULL) { |
503 |
|
// Fork out, so we can return from fullscreen mode when things get ugly |
504 |
|
XF86DGAForkApp(DefaultScreen(x_display)); |
505 |
|
#endif |
506 |
+ |
#endif |
507 |
|
|
508 |
|
#ifdef ENABLE_MON |
509 |
|
// Initialize mon |
510 |
|
mon_init(); |
511 |
|
#endif |
512 |
|
|
513 |
+ |
#if !EMULATED_PPC |
514 |
+ |
// Create and install stacks for signal handlers |
515 |
+ |
for (int i = 0; i < SIG_STACK_COUNT; i++) { |
516 |
+ |
void *sig_stack = malloc(SIG_STACK_SIZE); |
517 |
+ |
D(bug("Signal stack %d at %p\n", i, sig_stack)); |
518 |
+ |
if (sig_stack == NULL) { |
519 |
+ |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
520 |
+ |
goto quit; |
521 |
+ |
} |
522 |
+ |
sig_stacks[i].ss_sp = sig_stack; |
523 |
+ |
sig_stacks[i].ss_flags = 0; |
524 |
+ |
sig_stacks[i].ss_size = SIG_STACK_SIZE; |
525 |
+ |
} |
526 |
+ |
sig_stack_id = 0; |
527 |
+ |
if (sigaltstack(&sig_stacks[0], NULL) < 0) { |
528 |
+ |
sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno)); |
529 |
+ |
ErrorAlert(str); |
530 |
+ |
goto quit; |
531 |
+ |
} |
532 |
+ |
#endif |
533 |
+ |
|
534 |
+ |
#if !EMULATED_PPC |
535 |
+ |
// Install SIGSEGV and SIGBUS handlers |
536 |
+ |
sigemptyset(&sigsegv_action.sa_mask); // Block interrupts during SEGV handling |
537 |
+ |
sigaddset(&sigsegv_action.sa_mask, SIGUSR2); |
538 |
+ |
sigsegv_action.sa_sigaction = sigsegv_handler; |
539 |
+ |
sigsegv_action.sa_flags = SA_ONSTACK | SA_SIGINFO; |
540 |
+ |
#ifdef HAVE_SIGNAL_SA_RESTORER |
541 |
+ |
sigsegv_action.sa_restorer = NULL; |
542 |
+ |
#endif |
543 |
+ |
if (sigaction(SIGSEGV, &sigsegv_action, NULL) < 0) { |
544 |
+ |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
545 |
+ |
ErrorAlert(str); |
546 |
+ |
goto quit; |
547 |
+ |
} |
548 |
+ |
if (sigaction(SIGBUS, &sigsegv_action, NULL) < 0) { |
549 |
+ |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
550 |
+ |
ErrorAlert(str); |
551 |
+ |
goto quit; |
552 |
+ |
} |
553 |
+ |
#else |
554 |
+ |
// Install SIGSEGV handler for CPU emulator |
555 |
+ |
if (!sigsegv_install_handler(sigsegv_handler)) { |
556 |
+ |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
557 |
+ |
ErrorAlert(str); |
558 |
+ |
goto quit; |
559 |
+ |
} |
560 |
+ |
#endif |
561 |
+ |
|
562 |
+ |
// Initialize VM system |
563 |
+ |
vm_init(); |
564 |
+ |
|
565 |
|
// Get system info |
566 |
|
PVR = 0x00040000; // Default: 604 |
567 |
|
CPUClockSpeed = 100000000; // Default: 100MHz |
568 |
|
BusClockSpeed = 100000000; // Default: 100MHz |
569 |
< |
#if !EMULATED_PPC |
569 |
> |
#if EMULATED_PPC |
570 |
> |
PVR = 0x000c0000; // Default: 7400 (with AltiVec) |
571 |
> |
#elif defined(__APPLE__) && defined(__MACH__) |
572 |
> |
proc_file = popen("ioreg -c IOPlatformDevice", "r"); |
573 |
> |
if (proc_file) { |
574 |
> |
char line[256]; |
575 |
> |
bool powerpc_node = false; |
576 |
> |
while (fgets(line, sizeof(line) - 1, proc_file)) { |
577 |
> |
// Read line |
578 |
> |
int len = strlen(line); |
579 |
> |
if (len == 0) |
580 |
> |
continue; |
581 |
> |
line[len - 1] = 0; |
582 |
> |
|
583 |
> |
// Parse line |
584 |
> |
if (strstr(line, "o PowerPC,")) |
585 |
> |
powerpc_node = true; |
586 |
> |
else if (powerpc_node) { |
587 |
> |
uint32 value; |
588 |
> |
char head[256]; |
589 |
> |
if (sscanf(line, "%[ |]\"cpu-version\" = <%x>", head, &value) == 2) |
590 |
> |
PVR = value; |
591 |
> |
else if (sscanf(line, "%[ |]\"clock-frequency\" = <%x>", head, &value) == 2) |
592 |
> |
CPUClockSpeed = value; |
593 |
> |
else if (sscanf(line, "%[ |]\"bus-frequency\" = <%x>", head, &value) == 2) |
594 |
> |
BusClockSpeed = value; |
595 |
> |
else if (strchr(line, '}')) |
596 |
> |
powerpc_node = false; |
597 |
> |
} |
598 |
> |
} |
599 |
> |
fclose(proc_file); |
600 |
> |
} else { |
601 |
> |
sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno)); |
602 |
> |
WarningAlert(str); |
603 |
> |
} |
604 |
> |
#else |
605 |
|
proc_file = fopen("/proc/cpuinfo", "r"); |
606 |
|
if (proc_file) { |
607 |
|
char line[256]; |
615 |
|
// Parse line |
616 |
|
int i; |
617 |
|
char value[256]; |
618 |
< |
if (sscanf(line, "cpu : %s", value) == 1) { |
618 |
> |
if (sscanf(line, "cpu : %[0-9A-Za-a]", value) == 1) { |
619 |
|
if (strcmp(value, "601") == 0) |
620 |
|
PVR = 0x00010000; |
621 |
|
else if (strcmp(value, "603") == 0) |
632 |
|
PVR = 0x000a0000; |
633 |
|
else if (strcmp(value, "750") == 0) |
634 |
|
PVR = 0x00080000; |
635 |
+ |
else if (strcmp(value, "750FX") == 0) |
636 |
+ |
PVR = 0x70000000; |
637 |
|
else if (strcmp(value, "821") == 0) |
638 |
|
PVR = 0x00320000; |
639 |
|
else if (strcmp(value, "860") == 0) |
640 |
|
PVR = 0x00500000; |
641 |
+ |
else if (strcmp(value, "7400") == 0) |
642 |
+ |
PVR = 0x000c0000; |
643 |
+ |
else if (strcmp(value, "7410") == 0) |
644 |
+ |
PVR = 0x800c0000; |
645 |
+ |
else if (strcmp(value, "7450") == 0) |
646 |
+ |
PVR = 0x80000000; |
647 |
+ |
else if (strcmp(value, "7455") == 0) |
648 |
+ |
PVR = 0x80010000; |
649 |
+ |
else if (strcmp(value, "7457") == 0) |
650 |
+ |
PVR = 0x80020000; |
651 |
|
else |
652 |
|
printf("WARNING: Unknown CPU type '%s', assuming 604\n", value); |
653 |
|
} |
659 |
|
sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno)); |
660 |
|
WarningAlert(str); |
661 |
|
} |
662 |
+ |
|
663 |
+ |
// Get actual bus frequency |
664 |
+ |
proc_file = fopen("/proc/device-tree/clock-frequency", "r"); |
665 |
+ |
if (proc_file) { |
666 |
+ |
union { uint8 b[4]; uint32 l; } value; |
667 |
+ |
if (fread(value.b, sizeof(value), 1, proc_file) == 1) |
668 |
+ |
BusClockSpeed = value.l; |
669 |
+ |
fclose(proc_file); |
670 |
+ |
} |
671 |
|
#endif |
672 |
|
D(bug("PVR: %08x (assumed)\n", PVR)); |
673 |
|
|
692 |
|
goto quit; |
693 |
|
} |
694 |
|
|
695 |
+ |
#ifndef PAGEZERO_HACK |
696 |
|
// Create Low Memory area (0x0000..0x3000) |
697 |
|
if (vm_acquire_fixed((char *)0, 0x3000) < 0) { |
698 |
|
sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno)); |
700 |
|
goto quit; |
701 |
|
} |
702 |
|
lm_area_mapped = true; |
703 |
+ |
#endif |
704 |
|
|
705 |
|
// Create areas for Kernel Data |
706 |
|
kernel_area = shmget(IPC_PRIVATE, KERNEL_AREA_SIZE, 0600); |
719 |
|
ErrorAlert(str); |
720 |
|
goto quit; |
721 |
|
} |
722 |
< |
kernel_data = (KernelData *)0x68ffe000; |
722 |
> |
kernel_data = (KernelData *)KERNEL_DATA_BASE; |
723 |
|
emulator_data = &kernel_data->ed; |
724 |
< |
KernelDataAddr = (uint32)kernel_data; |
724 |
> |
KernelDataAddr = KERNEL_DATA_BASE; |
725 |
|
D(bug("Kernel Data at %p, Emulator Data at %p\n", kernel_data, emulator_data)); |
726 |
|
|
727 |
+ |
// Create area for DR Cache |
728 |
+ |
if (vm_acquire_fixed((void *)DR_EMULATOR_BASE, DR_EMULATOR_SIZE) < 0) { |
729 |
+ |
sprintf(str, GetString(STR_DR_EMULATOR_MMAP_ERR), strerror(errno)); |
730 |
+ |
ErrorAlert(str); |
731 |
+ |
goto quit; |
732 |
+ |
} |
733 |
+ |
dr_emulator_area_mapped = true; |
734 |
+ |
if (vm_acquire_fixed((void *)DR_CACHE_BASE, DR_CACHE_SIZE) < 0) { |
735 |
+ |
sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno)); |
736 |
+ |
ErrorAlert(str); |
737 |
+ |
goto quit; |
738 |
+ |
} |
739 |
+ |
dr_cache_area_mapped = true; |
740 |
+ |
#if !EMULATED_PPC |
741 |
+ |
if (vm_protect((char *)DR_CACHE_BASE, DR_CACHE_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
742 |
+ |
sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno)); |
743 |
+ |
ErrorAlert(str); |
744 |
+ |
goto quit; |
745 |
+ |
} |
746 |
+ |
#endif |
747 |
+ |
DRCacheAddr = DR_CACHE_BASE; |
748 |
+ |
D(bug("DR Cache at %p\n", DRCacheAddr)); |
749 |
+ |
|
750 |
|
// Create area for SheepShaver data |
751 |
< |
if (vm_acquire_fixed((char *)SHEEP_BASE, SHEEP_SIZE) < 0) { |
751 |
> |
if (!SheepMem::Init()) { |
752 |
|
sprintf(str, GetString(STR_SHEEP_MEM_MMAP_ERR), strerror(errno)); |
753 |
|
ErrorAlert(str); |
754 |
|
goto quit; |
755 |
|
} |
484 |
– |
SheepStack1Base = SHEEP_BASE + 0x10000; |
485 |
– |
SheepStack2Base = SheepStack1Base + 0x10000; |
486 |
– |
SheepThunksBase = SheepStack2Base + 0x1000; |
487 |
– |
sheep_area_mapped = true; |
756 |
|
|
757 |
|
// Create area for Mac ROM |
758 |
|
if (vm_acquire_fixed((char *)ROM_BASE, ROM_AREA_SIZE) < 0) { |
760 |
|
ErrorAlert(str); |
761 |
|
goto quit; |
762 |
|
} |
763 |
< |
#if !EMULATED_PPC || defined(__powerpc__) |
763 |
> |
#if !EMULATED_PPC |
764 |
|
if (vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
765 |
|
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
766 |
|
ErrorAlert(str); |
830 |
|
// Load NVRAM |
831 |
|
XPRAMInit(); |
832 |
|
|
833 |
+ |
// Load XPRAM default values if signature not found |
834 |
+ |
if (XPRAM[0x130c] != 0x4e || XPRAM[0x130d] != 0x75 |
835 |
+ |
|| XPRAM[0x130e] != 0x4d || XPRAM[0x130f] != 0x63) { |
836 |
+ |
D(bug("Loading XPRAM default values\n")); |
837 |
+ |
memset(XPRAM + 0x1300, 0, 0x100); |
838 |
+ |
XPRAM[0x130c] = 0x4e; // "NuMc" signature |
839 |
+ |
XPRAM[0x130d] = 0x75; |
840 |
+ |
XPRAM[0x130e] = 0x4d; |
841 |
+ |
XPRAM[0x130f] = 0x63; |
842 |
+ |
XPRAM[0x1301] = 0x80; // InternalWaitFlags = DynWait (don't wait for SCSI devices upon bootup) |
843 |
+ |
XPRAM[0x1310] = 0xa8; // Standard PRAM values |
844 |
+ |
XPRAM[0x1311] = 0x00; |
845 |
+ |
XPRAM[0x1312] = 0x00; |
846 |
+ |
XPRAM[0x1313] = 0x22; |
847 |
+ |
XPRAM[0x1314] = 0xcc; |
848 |
+ |
XPRAM[0x1315] = 0x0a; |
849 |
+ |
XPRAM[0x1316] = 0xcc; |
850 |
+ |
XPRAM[0x1317] = 0x0a; |
851 |
+ |
XPRAM[0x131c] = 0x00; |
852 |
+ |
XPRAM[0x131d] = 0x02; |
853 |
+ |
XPRAM[0x131e] = 0x63; |
854 |
+ |
XPRAM[0x131f] = 0x00; |
855 |
+ |
XPRAM[0x1308] = 0x13; |
856 |
+ |
XPRAM[0x1309] = 0x88; |
857 |
+ |
XPRAM[0x130a] = 0x00; |
858 |
+ |
XPRAM[0x130b] = 0xcc; |
859 |
+ |
XPRAM[0x1376] = 0x00; // OSDefault = MacOS |
860 |
+ |
XPRAM[0x1377] = 0x01; |
861 |
+ |
} |
862 |
+ |
|
863 |
|
// Set boot volume |
864 |
|
i16 = PrefsFindInt32("bootdrive"); |
865 |
|
XPRAM[0x1378] = i16 >> 8; |
877 |
|
boot_globs[1] = htonl(RAMSize); |
878 |
|
boot_globs[2] = htonl((uint32)-1); // End of bank table |
879 |
|
|
880 |
+ |
// Init thunks |
881 |
+ |
if (!ThunksInit()) |
882 |
+ |
goto quit; |
883 |
+ |
|
884 |
|
// Init drivers |
885 |
|
SonyInit(); |
886 |
|
DiskInit(); |
890 |
|
// Init external file system |
891 |
|
ExtFSInit(); |
892 |
|
|
893 |
+ |
// Init ADB |
894 |
+ |
ADBInit(); |
895 |
+ |
|
896 |
|
// Init audio |
897 |
|
AudioInit(); |
898 |
|
|
927 |
|
// Initialize Kernel Data |
928 |
|
memset(kernel_data, 0, sizeof(KernelData)); |
929 |
|
if (ROMType == ROMTYPE_NEWWORLD) { |
930 |
< |
static uint32 of_dev_tree[4] = {0, 0, 0, 0}; |
931 |
< |
static uint8 vector_lookup_tbl[128]; |
932 |
< |
static uint8 vector_mask_tbl[64]; |
930 |
> |
uintptr of_dev_tree = SheepMem::Reserve(4 * sizeof(uint32)); |
931 |
> |
memset((void *)of_dev_tree, 0, 4 * sizeof(uint32)); |
932 |
> |
uintptr vector_lookup_tbl = SheepMem::Reserve(128); |
933 |
> |
uintptr vector_mask_tbl = SheepMem::Reserve(64); |
934 |
|
memset((uint8 *)kernel_data + 0xb80, 0x3d, 0x80); |
935 |
< |
memset(vector_lookup_tbl, 0, 128); |
936 |
< |
memset(vector_mask_tbl, 0, 64); |
935 |
> |
memset((void *)vector_lookup_tbl, 0, 128); |
936 |
> |
memset((void *)vector_mask_tbl, 0, 64); |
937 |
|
kernel_data->v[0xb80 >> 2] = htonl(ROM_BASE); |
938 |
< |
kernel_data->v[0xb84 >> 2] = htonl((uint32)of_dev_tree); // OF device tree base |
939 |
< |
kernel_data->v[0xb90 >> 2] = htonl((uint32)vector_lookup_tbl); |
940 |
< |
kernel_data->v[0xb94 >> 2] = htonl((uint32)vector_mask_tbl); |
938 |
> |
kernel_data->v[0xb84 >> 2] = htonl(of_dev_tree); // OF device tree base |
939 |
> |
kernel_data->v[0xb90 >> 2] = htonl(vector_lookup_tbl); |
940 |
> |
kernel_data->v[0xb94 >> 2] = htonl(vector_mask_tbl); |
941 |
|
kernel_data->v[0xb98 >> 2] = htonl(ROM_BASE); // OpenPIC base |
942 |
|
kernel_data->v[0xbb0 >> 2] = htonl(0); // ADB base |
943 |
|
kernel_data->v[0xc20 >> 2] = htonl(RAMSize); |
950 |
|
kernel_data->v[0xc50 >> 2] = htonl(RAMBase); |
951 |
|
kernel_data->v[0xc54 >> 2] = htonl(RAMSize); |
952 |
|
kernel_data->v[0xf60 >> 2] = htonl(PVR); |
953 |
< |
kernel_data->v[0xf64 >> 2] = htonl(CPUClockSpeed); |
954 |
< |
kernel_data->v[0xf68 >> 2] = htonl(BusClockSpeed); |
955 |
< |
kernel_data->v[0xf6c >> 2] = htonl(CPUClockSpeed); |
953 |
> |
kernel_data->v[0xf64 >> 2] = htonl(CPUClockSpeed); // clock-frequency |
954 |
> |
kernel_data->v[0xf68 >> 2] = htonl(BusClockSpeed); // bus-frequency |
955 |
> |
kernel_data->v[0xf6c >> 2] = htonl(BusClockSpeed / 4); // timebase-frequency |
956 |
|
} else { |
957 |
|
kernel_data->v[0xc80 >> 2] = htonl(RAMSize); |
958 |
|
kernel_data->v[0xc84 >> 2] = htonl(RAMSize); |
964 |
|
kernel_data->v[0xcb0 >> 2] = htonl(RAMBase); |
965 |
|
kernel_data->v[0xcb4 >> 2] = htonl(RAMSize); |
966 |
|
kernel_data->v[0xf80 >> 2] = htonl(PVR); |
967 |
< |
kernel_data->v[0xf84 >> 2] = htonl(CPUClockSpeed); |
968 |
< |
kernel_data->v[0xf88 >> 2] = htonl(BusClockSpeed); |
969 |
< |
kernel_data->v[0xf8c >> 2] = htonl(CPUClockSpeed); |
967 |
> |
kernel_data->v[0xf84 >> 2] = htonl(CPUClockSpeed); // clock-frequency |
968 |
> |
kernel_data->v[0xf88 >> 2] = htonl(BusClockSpeed); // bus-frequency |
969 |
> |
kernel_data->v[0xf8c >> 2] = htonl(BusClockSpeed / 4); // timebase-frequency |
970 |
|
} |
971 |
|
|
972 |
|
// Initialize extra low memory |
973 |
|
D(bug("Initializing Low Memory...\n")); |
974 |
|
memset(NULL, 0, 0x3000); |
975 |
|
WriteMacInt32(XLM_SIGNATURE, FOURCC('B','a','a','h')); // Signature to detect SheepShaver |
976 |
< |
WriteMacInt32(XLM_KERNEL_DATA, (uint32)kernel_data); // For trap replacement routines |
976 |
> |
WriteMacInt32(XLM_KERNEL_DATA, KernelDataAddr); // For trap replacement routines |
977 |
|
WriteMacInt32(XLM_PVR, PVR); // Theoretical PVR |
978 |
|
WriteMacInt32(XLM_BUS_CLOCK, BusClockSpeed); // For DriverServicesLib patch |
979 |
|
WriteMacInt16(XLM_EXEC_RETURN_OPCODE, M68K_EXEC_RETURN); // For Execute68k() (RTS from the executed 68k code will jump here and end 68k mode) |
980 |
< |
#if EMULATED_PPC |
981 |
< |
WriteMacInt32(XLM_ETHER_INIT, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_INIT)); |
982 |
< |
WriteMacInt32(XLM_ETHER_TERM, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_TERM)); |
677 |
< |
WriteMacInt32(XLM_ETHER_OPEN, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_OPEN)); |
678 |
< |
WriteMacInt32(XLM_ETHER_CLOSE, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_CLOSE)); |
679 |
< |
WriteMacInt32(XLM_ETHER_WPUT, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_WPUT)); |
680 |
< |
WriteMacInt32(XLM_ETHER_RSRV, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_RSRV)); |
681 |
< |
WriteMacInt32(XLM_VIDEO_DOIO, POWERPC_NATIVE_OP_FUNC(NATIVE_VIDEO_DO_DRIVER_IO)); |
682 |
< |
#else |
683 |
< |
WriteMacInt32(XLM_TOC, (uint32)TOC); // TOC pointer of emulator |
684 |
< |
WriteMacInt32(XLM_ETHER_INIT, (uint32)InitStreamModule); // DLPI ethernet driver functions |
685 |
< |
WriteMacInt32(XLM_ETHER_TERM, (uint32)TerminateStreamModule); |
686 |
< |
WriteMacInt32(XLM_ETHER_OPEN, (uint32)ether_open); |
687 |
< |
WriteMacInt32(XLM_ETHER_CLOSE, (uint32)ether_close); |
688 |
< |
WriteMacInt32(XLM_ETHER_WPUT, (uint32)ether_wput); |
689 |
< |
WriteMacInt32(XLM_ETHER_RSRV, (uint32)ether_rsrv); |
690 |
< |
WriteMacInt32(XLM_VIDEO_DOIO, (uint32)VideoDoDriverIO); |
980 |
> |
WriteMacInt32(XLM_ZERO_PAGE, SheepMem::ZeroPage()); // Pointer to read-only page with all bits set to 0 |
981 |
> |
#if !EMULATED_PPC |
982 |
> |
WriteMacInt32(XLM_TOC, (uint32)TOC); // TOC pointer of emulator |
983 |
|
#endif |
984 |
+ |
WriteMacInt32(XLM_ETHER_INIT, NativeFunction(NATIVE_ETHER_INIT)); // DLPI ethernet driver functions |
985 |
+ |
WriteMacInt32(XLM_ETHER_TERM, NativeFunction(NATIVE_ETHER_TERM)); |
986 |
+ |
WriteMacInt32(XLM_ETHER_OPEN, NativeFunction(NATIVE_ETHER_OPEN)); |
987 |
+ |
WriteMacInt32(XLM_ETHER_CLOSE, NativeFunction(NATIVE_ETHER_CLOSE)); |
988 |
+ |
WriteMacInt32(XLM_ETHER_WPUT, NativeFunction(NATIVE_ETHER_WPUT)); |
989 |
+ |
WriteMacInt32(XLM_ETHER_RSRV, NativeFunction(NATIVE_ETHER_RSRV)); |
990 |
+ |
WriteMacInt32(XLM_VIDEO_DOIO, NativeFunction(NATIVE_VIDEO_DO_DRIVER_IO)); |
991 |
|
D(bug("Low Memory initialized\n")); |
992 |
|
|
993 |
|
// Start 60Hz thread |
994 |
+ |
tick_thread_cancel = false; |
995 |
|
tick_thread_active = (pthread_create(&tick_thread, NULL, tick_func, NULL) == 0); |
996 |
|
D(bug("Tick thread installed (%ld)\n", tick_thread)); |
997 |
|
|
998 |
|
// Start NVRAM watchdog thread |
999 |
|
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
1000 |
+ |
nvram_thread_cancel = false; |
1001 |
|
nvram_thread_active = (pthread_create(&nvram_thread, NULL, nvram_func, NULL) == 0); |
1002 |
|
D(bug("NVRAM thread installed (%ld)\n", nvram_thread)); |
1003 |
|
|
1004 |
|
#if !EMULATED_PPC |
704 |
– |
// Create and install stacks for signal handlers |
705 |
– |
sig_stack = malloc(SIG_STACK_SIZE); |
706 |
– |
D(bug("Signal stack at %p\n", sig_stack)); |
707 |
– |
if (sig_stack == NULL) { |
708 |
– |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
709 |
– |
goto quit; |
710 |
– |
} |
711 |
– |
extra_stack = malloc(SIG_STACK_SIZE); |
712 |
– |
D(bug("Extra stack at %p\n", extra_stack)); |
713 |
– |
if (extra_stack == NULL) { |
714 |
– |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
715 |
– |
goto quit; |
716 |
– |
} |
717 |
– |
struct sigaltstack new_stack; |
718 |
– |
new_stack.ss_sp = sig_stack; |
719 |
– |
new_stack.ss_flags = 0; |
720 |
– |
new_stack.ss_size = SIG_STACK_SIZE; |
721 |
– |
if (sigaltstack(&new_stack, NULL) < 0) { |
722 |
– |
sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno)); |
723 |
– |
ErrorAlert(str); |
724 |
– |
goto quit; |
725 |
– |
} |
726 |
– |
#endif |
727 |
– |
|
728 |
– |
#if !EMULATED_PPC |
729 |
– |
// Install SIGSEGV handler |
730 |
– |
sigemptyset(&sigsegv_action.sa_mask); // Block interrupts during SEGV handling |
731 |
– |
sigaddset(&sigsegv_action.sa_mask, SIGUSR2); |
732 |
– |
sigsegv_action.sa_handler = (__sighandler_t)sigsegv_handler; |
733 |
– |
sigsegv_action.sa_flags = SA_ONSTACK; |
734 |
– |
sigsegv_action.sa_restorer = NULL; |
735 |
– |
if (sigaction(SIGSEGV, &sigsegv_action, NULL) < 0) { |
736 |
– |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
737 |
– |
ErrorAlert(str); |
738 |
– |
goto quit; |
739 |
– |
} |
740 |
– |
|
1005 |
|
// Install SIGILL handler |
1006 |
|
sigemptyset(&sigill_action.sa_mask); // Block interrupts during ILL handling |
1007 |
|
sigaddset(&sigill_action.sa_mask, SIGUSR2); |
1008 |
< |
sigill_action.sa_handler = (__sighandler_t)sigill_handler; |
1009 |
< |
sigill_action.sa_flags = SA_ONSTACK; |
1008 |
> |
sigill_action.sa_sigaction = sigill_handler; |
1009 |
> |
sigill_action.sa_flags = SA_ONSTACK | SA_SIGINFO; |
1010 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
1011 |
|
sigill_action.sa_restorer = NULL; |
1012 |
+ |
#endif |
1013 |
|
if (sigaction(SIGILL, &sigill_action, NULL) < 0) { |
1014 |
|
sprintf(str, GetString(STR_SIGILL_INSTALL_ERR), strerror(errno)); |
1015 |
|
ErrorAlert(str); |
1017 |
|
} |
1018 |
|
#endif |
1019 |
|
|
1020 |
+ |
#if !EMULATED_PPC |
1021 |
|
// Install interrupt signal handler |
1022 |
|
sigemptyset(&sigusr2_action.sa_mask); |
1023 |
< |
sigusr2_action.sa_handler = (__sighandler_t)sigusr2_handler; |
1024 |
< |
sigusr2_action.sa_flags = 0; |
1025 |
< |
#if !EMULATED_PPC |
759 |
< |
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART; |
760 |
< |
#endif |
1023 |
> |
sigusr2_action.sa_sigaction = sigusr2_handler; |
1024 |
> |
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART | SA_SIGINFO; |
1025 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
1026 |
|
sigusr2_action.sa_restorer = NULL; |
1027 |
+ |
#endif |
1028 |
|
if (sigaction(SIGUSR2, &sigusr2_action, NULL) < 0) { |
1029 |
|
sprintf(str, GetString(STR_SIGUSR2_INSTALL_ERR), strerror(errno)); |
1030 |
|
ErrorAlert(str); |
1031 |
|
goto quit; |
1032 |
|
} |
1033 |
+ |
#endif |
1034 |
|
|
1035 |
|
// Get my thread ID and execute MacOS thread function |
1036 |
|
emul_thread = pthread_self(); |
1056 |
|
|
1057 |
|
// Stop 60Hz thread |
1058 |
|
if (tick_thread_active) { |
1059 |
+ |
tick_thread_cancel = true; |
1060 |
|
pthread_cancel(tick_thread); |
1061 |
|
pthread_join(tick_thread, NULL); |
1062 |
|
} |
1063 |
|
|
1064 |
|
// Stop NVRAM watchdog thread |
1065 |
|
if (nvram_thread_active) { |
1066 |
+ |
nvram_thread_cancel = true; |
1067 |
|
pthread_cancel(nvram_thread); |
1068 |
|
pthread_join(nvram_thread, NULL); |
1069 |
|
} |
1070 |
|
|
1071 |
|
#if !EMULATED_PPC |
1072 |
< |
// Uninstall SIGSEGV handler |
1072 |
> |
// Uninstall SIGSEGV and SIGBUS handlers |
1073 |
|
sigemptyset(&sigsegv_action.sa_mask); |
1074 |
|
sigsegv_action.sa_handler = SIG_DFL; |
1075 |
|
sigsegv_action.sa_flags = 0; |
1076 |
|
sigaction(SIGSEGV, &sigsegv_action, NULL); |
1077 |
+ |
sigaction(SIGBUS, &sigsegv_action, NULL); |
1078 |
|
|
1079 |
|
// Uninstall SIGILL handler |
1080 |
|
sigemptyset(&sigill_action.sa_mask); |
1081 |
|
sigill_action.sa_handler = SIG_DFL; |
1082 |
|
sigill_action.sa_flags = 0; |
1083 |
|
sigaction(SIGILL, &sigill_action, NULL); |
1084 |
+ |
|
1085 |
+ |
// Delete stacks for signal handlers |
1086 |
+ |
for (int i = 0; i < SIG_STACK_COUNT; i++) { |
1087 |
+ |
void *sig_stack = sig_stacks[i].ss_sp; |
1088 |
+ |
if (sig_stack) |
1089 |
+ |
free(sig_stack); |
1090 |
+ |
} |
1091 |
|
#endif |
1092 |
|
|
1093 |
|
// Save NVRAM |
1108 |
|
// Exit audio |
1109 |
|
AudioExit(); |
1110 |
|
|
1111 |
+ |
// Exit ADB |
1112 |
+ |
ADBExit(); |
1113 |
+ |
|
1114 |
|
// Exit video |
1115 |
|
VideoExit(); |
1116 |
|
|
1123 |
|
DiskExit(); |
1124 |
|
SonyExit(); |
1125 |
|
|
1126 |
+ |
// Delete thunks |
1127 |
+ |
ThunksExit(); |
1128 |
+ |
|
1129 |
+ |
// Delete SheepShaver globals |
1130 |
+ |
SheepMem::Exit(); |
1131 |
+ |
|
1132 |
|
// Delete RAM area |
1133 |
|
if (ram_area_mapped) |
1134 |
|
vm_release((char *)RAM_BASE, RAMSize); |
1137 |
|
if (rom_area_mapped) |
1138 |
|
vm_release((char *)ROM_BASE, ROM_AREA_SIZE); |
1139 |
|
|
1140 |
+ |
// Delete DR cache areas |
1141 |
+ |
if (dr_emulator_area_mapped) |
1142 |
+ |
vm_release((void *)DR_EMULATOR_BASE, DR_EMULATOR_SIZE); |
1143 |
+ |
if (dr_cache_area_mapped) |
1144 |
+ |
vm_release((void *)DR_CACHE_BASE, DR_CACHE_SIZE); |
1145 |
+ |
|
1146 |
|
// Delete Kernel Data area |
1147 |
|
if (kernel_area >= 0) { |
1148 |
|
shmdt((void *)KERNEL_DATA_BASE); |
1170 |
|
#endif |
1171 |
|
|
1172 |
|
// Close X11 server connection |
1173 |
+ |
#ifndef USE_SDL_VIDEO |
1174 |
|
if (x_display) |
1175 |
|
XCloseDisplay(x_display); |
1176 |
+ |
#endif |
1177 |
|
|
1178 |
|
exit(0); |
1179 |
|
} |
1248 |
|
uint16 proc[2] = {trap, M68K_RTS}; |
1249 |
|
Execute68k((uint32)proc, r); |
1250 |
|
} |
957 |
– |
|
958 |
– |
|
959 |
– |
/* |
960 |
– |
* Execute PPC code from EMUL_OP routine (real mode switch) |
961 |
– |
*/ |
962 |
– |
|
963 |
– |
void ExecutePPC(void (*func)()) |
964 |
– |
{ |
965 |
– |
uint32 tvect[2] = {(uint32)func, 0}; // Fake TVECT |
966 |
– |
RoutineDescriptor desc = BUILD_PPC_ROUTINE_DESCRIPTOR(0, tvect); |
967 |
– |
M68kRegisters r; |
968 |
– |
Execute68k((uint32)&desc, &r); |
969 |
– |
} |
1251 |
|
#endif |
1252 |
|
|
1253 |
|
|
1326 |
|
|
1327 |
|
void PatchAfterStartup(void) |
1328 |
|
{ |
1048 |
– |
#if EMULATED_PPC |
1329 |
|
ExecuteNative(NATIVE_VIDEO_INSTALL_ACCEL); |
1050 |
– |
#else |
1051 |
– |
ExecutePPC(VideoInstallAccel); |
1052 |
– |
#endif |
1330 |
|
InstallExtFS(); |
1331 |
|
} |
1332 |
|
|
1335 |
|
* NVRAM watchdog thread (saves NVRAM every minute) |
1336 |
|
*/ |
1337 |
|
|
1338 |
< |
static void *nvram_func(void *arg) |
1338 |
> |
static void nvram_watchdog(void) |
1339 |
|
{ |
1340 |
< |
struct timespec req = {60, 0}; // 1 minute |
1340 |
> |
if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) { |
1341 |
> |
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
1342 |
> |
SaveXPRAM(); |
1343 |
> |
} |
1344 |
> |
} |
1345 |
|
|
1346 |
< |
for (;;) { |
1347 |
< |
pthread_testcancel(); |
1348 |
< |
nanosleep(&req, NULL); |
1349 |
< |
pthread_testcancel(); |
1350 |
< |
if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) { |
1351 |
< |
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
1071 |
< |
SaveXPRAM(); |
1072 |
< |
} |
1346 |
> |
static void *nvram_func(void *arg) |
1347 |
> |
{ |
1348 |
> |
while (!nvram_thread_cancel) { |
1349 |
> |
for (int i=0; i<60 && !nvram_thread_cancel; i++) |
1350 |
> |
Delay_usec(999999); // Only wait 1 second so we quit promptly when nvram_thread_cancel becomes true |
1351 |
> |
nvram_watchdog(); |
1352 |
|
} |
1353 |
|
return NULL; |
1354 |
|
} |
1361 |
|
static void *tick_func(void *arg) |
1362 |
|
{ |
1363 |
|
int tick_counter = 0; |
1364 |
< |
struct timespec req = {0, 16625000}; |
1364 |
> |
uint64 start = GetTicks_usec(); |
1365 |
> |
int64 ticks = 0; |
1366 |
> |
uint64 next = GetTicks_usec(); |
1367 |
|
|
1368 |
< |
for (;;) { |
1368 |
> |
while (!tick_thread_cancel) { |
1369 |
|
|
1370 |
|
// Wait |
1371 |
< |
nanosleep(&req, NULL); |
1371 |
> |
next += 16625; |
1372 |
> |
int64 delay = next - GetTicks_usec(); |
1373 |
> |
if (delay > 0) |
1374 |
> |
Delay_usec(delay); |
1375 |
> |
else if (delay < -16625) |
1376 |
> |
next = GetTicks_usec(); |
1377 |
> |
ticks++; |
1378 |
|
|
1379 |
|
#if !EMULATED_PPC |
1380 |
|
// Did we crash? |
1381 |
|
if (emul_thread_fatal) { |
1382 |
|
|
1383 |
|
// Yes, dump registers |
1384 |
< |
pt_regs *r = (pt_regs *)&sigsegv_regs; |
1384 |
> |
sigregs *r = &sigsegv_regs; |
1385 |
|
char str[256]; |
1386 |
< |
sprintf(str, "SIGSEGV\n" |
1386 |
> |
if (crash_reason == NULL) |
1387 |
> |
crash_reason = "SIGSEGV"; |
1388 |
> |
sprintf(str, "%s\n" |
1389 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1390 |
|
" xer %08lx cr %08lx \n" |
1391 |
|
" r0 %08lx r1 %08lx r2 %08lx r3 %08lx\n" |
1396 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1397 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1398 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1399 |
+ |
crash_reason, |
1400 |
|
r->nip, r->link, r->ctr, r->msr, |
1401 |
|
r->xer, r->ccr, |
1402 |
|
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1432 |
|
TriggerInterrupt(); |
1433 |
|
} |
1434 |
|
} |
1435 |
+ |
|
1436 |
+ |
uint64 end = GetTicks_usec(); |
1437 |
+ |
D(bug("%Ld ticks in %Ld usec = %f ticks/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start))); |
1438 |
|
return NULL; |
1439 |
|
} |
1440 |
|
|
1555 |
|
* Trigger signal USR2 from another thread |
1556 |
|
*/ |
1557 |
|
|
1558 |
< |
#if !EMULATED_PPC || ASYNC_IRQ |
1558 |
> |
#if !EMULATED_PPC |
1559 |
|
void TriggerInterrupt(void) |
1560 |
|
{ |
1561 |
|
if (ready_for_signals) |
1587 |
|
|
1588 |
|
void DisableInterrupt(void) |
1589 |
|
{ |
1590 |
+ |
#if EMULATED_PPC |
1591 |
+ |
WriteMacInt32(XLM_IRQ_NEST, int32(ReadMacInt32(XLM_IRQ_NEST)) + 1); |
1592 |
+ |
#else |
1593 |
|
atomic_add((int *)XLM_IRQ_NEST, 1); |
1594 |
+ |
#endif |
1595 |
|
} |
1596 |
|
|
1597 |
|
|
1601 |
|
|
1602 |
|
void EnableInterrupt(void) |
1603 |
|
{ |
1604 |
+ |
#if EMULATED_PPC |
1605 |
+ |
WriteMacInt32(XLM_IRQ_NEST, int32(ReadMacInt32(XLM_IRQ_NEST)) - 1); |
1606 |
+ |
#else |
1607 |
|
atomic_add((int *)XLM_IRQ_NEST, -1); |
1608 |
+ |
#endif |
1609 |
|
} |
1610 |
|
|
1611 |
|
|
1613 |
|
* USR2 handler |
1614 |
|
*/ |
1615 |
|
|
1616 |
< |
#if EMULATED_PPC |
1617 |
< |
static void sigusr2_handler(int sig) |
1616 |
> |
#if !EMULATED_PPC |
1617 |
> |
static void sigusr2_handler(int sig, siginfo_t *sip, void *scp) |
1618 |
|
{ |
1619 |
< |
#if ASYNC_IRQ |
1620 |
< |
extern void HandleInterrupt(void); |
1621 |
< |
HandleInterrupt(); |
1619 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1620 |
> |
|
1621 |
> |
#ifdef USE_SDL_VIDEO |
1622 |
> |
// We must fill in the events queue in the same thread that did call SDL_SetVideoMode() |
1623 |
> |
SDL_PumpEvents(); |
1624 |
|
#endif |
1322 |
– |
} |
1323 |
– |
#else |
1324 |
– |
static void sigusr2_handler(int sig, sigcontext_struct *sc) |
1325 |
– |
{ |
1326 |
– |
pt_regs *r = sc->regs; |
1625 |
|
|
1626 |
|
// Do nothing if interrupts are disabled |
1627 |
|
if (*(int32 *)XLM_IRQ_NEST > 0) |
1635 |
|
case MODE_68K: |
1636 |
|
// 68k emulator active, trigger 68k interrupt level 1 |
1637 |
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1638 |
< |
r->ccr |= ntohl(kernel_data->v[0x674 >> 2]); |
1638 |
> |
r->cr() |= ntohl(kernel_data->v[0x674 >> 2]); |
1639 |
|
break; |
1640 |
|
|
1641 |
|
#if INTERRUPTS_IN_NATIVE_MODE |
1642 |
|
case MODE_NATIVE: |
1643 |
|
// 68k emulator inactive, in nanokernel? |
1644 |
< |
if (r->gpr[1] != KernelDataAddr) { |
1644 |
> |
if (r->gpr(1) != KernelDataAddr) { |
1645 |
> |
|
1646 |
> |
// Set extra stack for nested interrupts |
1647 |
> |
sig_stack_acquire(); |
1648 |
> |
|
1649 |
|
// Prepare for 68k interrupt level 1 |
1650 |
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1651 |
|
WriteMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc, ReadMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc) | ntohl(kernel_data->v[0x674 >> 2])); |
1652 |
|
|
1653 |
|
// Execute nanokernel interrupt routine (this will activate the 68k emulator) |
1654 |
< |
atomic_add((int32 *)XLM_IRQ_NEST, 1); |
1654 |
> |
DisableInterrupt(); |
1655 |
|
if (ROMType == ROMTYPE_NEWWORLD) |
1656 |
|
ppc_interrupt(ROM_BASE + 0x312b1c, KernelDataAddr); |
1657 |
|
else |
1658 |
|
ppc_interrupt(ROM_BASE + 0x312a3c, KernelDataAddr); |
1659 |
+ |
|
1660 |
+ |
// Reset normal signal stack |
1661 |
+ |
sig_stack_release(); |
1662 |
|
} |
1663 |
|
break; |
1664 |
|
#endif |
1669 |
|
if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) { |
1670 |
|
|
1671 |
|
// Set extra stack for SIGSEGV handler |
1672 |
< |
struct sigaltstack new_stack; |
1368 |
< |
new_stack.ss_sp = extra_stack; |
1369 |
< |
new_stack.ss_flags = 0; |
1370 |
< |
new_stack.ss_size = SIG_STACK_SIZE; |
1371 |
< |
sigaltstack(&new_stack, NULL); |
1672 |
> |
sig_stack_acquire(); |
1673 |
|
#if 1 |
1674 |
|
// Execute full 68k interrupt routine |
1675 |
|
M68kRegisters r; |
1691 |
|
if (InterruptFlags & INTFLAG_VIA) { |
1692 |
|
ClearInterruptFlag(INTFLAG_VIA); |
1693 |
|
ADBInterrupt(); |
1694 |
< |
ExecutePPC(VideoVBL); |
1694 |
> |
ExecuteNative(NATIVE_VIDEO_VBL); |
1695 |
|
} |
1696 |
|
} |
1697 |
|
#endif |
1698 |
|
// Reset normal signal stack |
1699 |
< |
new_stack.ss_sp = sig_stack; |
1399 |
< |
new_stack.ss_flags = 0; |
1400 |
< |
new_stack.ss_size = SIG_STACK_SIZE; |
1401 |
< |
sigaltstack(&new_stack, NULL); |
1699 |
> |
sig_stack_release(); |
1700 |
|
} |
1701 |
|
break; |
1702 |
|
#endif |
1710 |
|
*/ |
1711 |
|
|
1712 |
|
#if !EMULATED_PPC |
1713 |
< |
static void sigsegv_handler(int sig, sigcontext_struct *sc) |
1713 |
> |
static void sigsegv_handler(int sig, siginfo_t *sip, void *scp) |
1714 |
|
{ |
1715 |
< |
pt_regs *r = sc->regs; |
1715 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1716 |
|
|
1717 |
|
// Get effective address |
1718 |
< |
uint32 addr = r->dar; |
1718 |
> |
uint32 addr = r->dar(); |
1719 |
|
|
1720 |
|
#if ENABLE_VOSF |
1721 |
|
// Handle screen fault. |
1722 |
|
extern bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction); |
1723 |
< |
if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->nip)) |
1723 |
> |
if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->pc())) |
1724 |
|
return; |
1725 |
|
#endif |
1726 |
|
|
1727 |
|
num_segv++; |
1728 |
|
|
1729 |
< |
// Fault in Mac ROM or RAM? |
1730 |
< |
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
1729 |
> |
// Fault in Mac ROM or RAM or DR Cache? |
1730 |
> |
bool mac_fault = (r->pc() >= ROM_BASE) && (r->pc() < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize)) || (r->pc() >= DR_CACHE_BASE && r->pc() < (DR_CACHE_BASE + DR_CACHE_SIZE)); |
1731 |
|
if (mac_fault) { |
1732 |
|
|
1733 |
|
// "VM settings" during MacOS 8 installation |
1734 |
< |
if (r->nip == ROM_BASE + 0x488160 && r->gpr[20] == 0xf8000000) { |
1735 |
< |
r->nip += 4; |
1736 |
< |
r->gpr[8] = 0; |
1734 |
> |
if (r->pc() == ROM_BASE + 0x488160 && r->gpr(20) == 0xf8000000) { |
1735 |
> |
r->pc() += 4; |
1736 |
> |
r->gpr(8) = 0; |
1737 |
|
return; |
1738 |
|
|
1739 |
|
// MacOS 8.5 installation |
1740 |
< |
} else if (r->nip == ROM_BASE + 0x488140 && r->gpr[16] == 0xf8000000) { |
1741 |
< |
r->nip += 4; |
1742 |
< |
r->gpr[8] = 0; |
1740 |
> |
} else if (r->pc() == ROM_BASE + 0x488140 && r->gpr(16) == 0xf8000000) { |
1741 |
> |
r->pc() += 4; |
1742 |
> |
r->gpr(8) = 0; |
1743 |
|
return; |
1744 |
|
|
1745 |
|
// MacOS 8 serial drivers on startup |
1746 |
< |
} else if (r->nip == ROM_BASE + 0x48e080 && (r->gpr[8] == 0xf3012002 || r->gpr[8] == 0xf3012000)) { |
1747 |
< |
r->nip += 4; |
1748 |
< |
r->gpr[8] = 0; |
1746 |
> |
} else if (r->pc() == ROM_BASE + 0x48e080 && (r->gpr(8) == 0xf3012002 || r->gpr(8) == 0xf3012000)) { |
1747 |
> |
r->pc() += 4; |
1748 |
> |
r->gpr(8) = 0; |
1749 |
|
return; |
1750 |
|
|
1751 |
|
// MacOS 8.1 serial drivers on startup |
1752 |
< |
} else if (r->nip == ROM_BASE + 0x48c5e0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1753 |
< |
r->nip += 4; |
1752 |
> |
} else if (r->pc() == ROM_BASE + 0x48c5e0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1753 |
> |
r->pc() += 4; |
1754 |
|
return; |
1755 |
< |
} else if (r->nip == ROM_BASE + 0x4a10a0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1756 |
< |
r->nip += 4; |
1755 |
> |
} else if (r->pc() == ROM_BASE + 0x4a10a0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1756 |
> |
r->pc() += 4; |
1757 |
> |
return; |
1758 |
> |
|
1759 |
> |
// MacOS 8.6 serial drivers on startup (with DR Cache and OldWorld ROM) |
1760 |
> |
} else if ((r->pc() - DR_CACHE_BASE) < DR_CACHE_SIZE && (r->gpr(16) == 0xf3012002 || r->gpr(16) == 0xf3012000)) { |
1761 |
> |
r->pc() += 4; |
1762 |
> |
return; |
1763 |
> |
} else if ((r->pc() - DR_CACHE_BASE) < DR_CACHE_SIZE && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1764 |
> |
r->pc() += 4; |
1765 |
|
return; |
1766 |
|
} |
1767 |
|
|
1768 |
|
// Get opcode and divide into fields |
1769 |
< |
uint32 opcode = *((uint32 *)r->nip); |
1769 |
> |
uint32 opcode = *((uint32 *)r->pc()); |
1770 |
|
uint32 primop = opcode >> 26; |
1771 |
|
uint32 exop = (opcode >> 1) & 0x3ff; |
1772 |
|
uint32 ra = (opcode >> 16) & 0x1f; |
1855 |
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_NORM; break; |
1856 |
|
case 45: // sthu |
1857 |
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_U; break; |
1858 |
+ |
#if EMULATE_UNALIGNED_LOADSTORE_MULTIPLE |
1859 |
+ |
case 46: // lmw |
1860 |
+ |
if ((addr % 4) != 0) { |
1861 |
+ |
uint32 ea = addr; |
1862 |
+ |
D(bug("WARNING: unaligned lmw to EA=%08x from IP=%08x\n", ea, r->pc())); |
1863 |
+ |
for (int i = rd; i <= 31; i++) { |
1864 |
+ |
r->gpr(i) = ReadMacInt32(ea); |
1865 |
+ |
ea += 4; |
1866 |
+ |
} |
1867 |
+ |
r->pc() += 4; |
1868 |
+ |
goto rti; |
1869 |
+ |
} |
1870 |
+ |
break; |
1871 |
+ |
case 47: // stmw |
1872 |
+ |
if ((addr % 4) != 0) { |
1873 |
+ |
uint32 ea = addr; |
1874 |
+ |
D(bug("WARNING: unaligned stmw to EA=%08x from IP=%08x\n", ea, r->pc())); |
1875 |
+ |
for (int i = rd; i <= 31; i++) { |
1876 |
+ |
WriteMacInt32(ea, r->gpr(i)); |
1877 |
+ |
ea += 4; |
1878 |
+ |
} |
1879 |
+ |
r->pc() += 4; |
1880 |
+ |
goto rti; |
1881 |
+ |
} |
1882 |
+ |
break; |
1883 |
+ |
#endif |
1884 |
|
} |
1885 |
|
|
1886 |
< |
// Ignore ROM writes |
1887 |
< |
if (transfer_type == TYPE_STORE && addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) { |
1888 |
< |
// D(bug("WARNING: %s write access to ROM at %08lx, pc %08lx\n", transfer_size == SIZE_BYTE ? "Byte" : transfer_size == SIZE_HALFWORD ? "Halfword" : "Word", addr, r->nip)); |
1886 |
> |
// Ignore ROM writes (including to the zero page, which is read-only) |
1887 |
> |
if (transfer_type == TYPE_STORE && |
1888 |
> |
((addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) || |
1889 |
> |
(addr >= SheepMem::ZeroPage() && addr < SheepMem::ZeroPage() + SheepMem::PageSize()))) { |
1890 |
> |
// D(bug("WARNING: %s write access to ROM at %08lx, pc %08lx\n", transfer_size == SIZE_BYTE ? "Byte" : transfer_size == SIZE_HALFWORD ? "Halfword" : "Word", addr, r->pc())); |
1891 |
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1892 |
< |
r->gpr[ra] = addr; |
1893 |
< |
r->nip += 4; |
1892 |
> |
r->gpr(ra) = addr; |
1893 |
> |
r->pc() += 4; |
1894 |
|
goto rti; |
1895 |
|
} |
1896 |
|
|
1897 |
|
// Ignore illegal memory accesses? |
1898 |
|
if (PrefsFindBool("ignoresegv")) { |
1899 |
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1900 |
< |
r->gpr[ra] = addr; |
1900 |
> |
r->gpr(ra) = addr; |
1901 |
|
if (transfer_type == TYPE_LOAD) |
1902 |
< |
r->gpr[rd] = 0; |
1903 |
< |
r->nip += 4; |
1902 |
> |
r->gpr(rd) = 0; |
1903 |
> |
r->pc() += 4; |
1904 |
|
goto rti; |
1905 |
|
} |
1906 |
|
|
1908 |
|
if (!PrefsFindBool("nogui")) { |
1909 |
|
char str[256]; |
1910 |
|
if (transfer_type == TYPE_LOAD || transfer_type == TYPE_STORE) |
1911 |
< |
sprintf(str, GetString(STR_MEM_ACCESS_ERR), transfer_size == SIZE_BYTE ? "byte" : transfer_size == SIZE_HALFWORD ? "halfword" : "word", transfer_type == TYPE_LOAD ? GetString(STR_MEM_ACCESS_READ) : GetString(STR_MEM_ACCESS_WRITE), addr, r->nip, r->gpr[24], r->gpr[1]); |
1911 |
> |
sprintf(str, GetString(STR_MEM_ACCESS_ERR), transfer_size == SIZE_BYTE ? "byte" : transfer_size == SIZE_HALFWORD ? "halfword" : "word", transfer_type == TYPE_LOAD ? GetString(STR_MEM_ACCESS_READ) : GetString(STR_MEM_ACCESS_WRITE), addr, r->pc(), r->gpr(24), r->gpr(1)); |
1912 |
|
else |
1913 |
< |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode); |
1913 |
> |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode); |
1914 |
|
ErrorAlert(str); |
1915 |
|
QuitEmulator(); |
1916 |
|
return; |
1918 |
|
} |
1919 |
|
|
1920 |
|
// For all other errors, jump into debugger (sort of...) |
1921 |
+ |
crash_reason = (sig == SIGBUS) ? "SIGBUS" : "SIGSEGV"; |
1922 |
|
if (!ready_for_signals) { |
1923 |
< |
printf("SIGSEGV\n"); |
1924 |
< |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
1923 |
> |
printf("%s\n"); |
1924 |
> |
printf(" sigcontext %p, machine_regs %p\n", scp, r); |
1925 |
|
printf( |
1926 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1927 |
|
" xer %08lx cr %08lx \n" |
1933 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1934 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1935 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1936 |
< |
r->nip, r->link, r->ctr, r->msr, |
1937 |
< |
r->xer, r->ccr, |
1938 |
< |
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1939 |
< |
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
1940 |
< |
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
1941 |
< |
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
1942 |
< |
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
1943 |
< |
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
1944 |
< |
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
1945 |
< |
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
1936 |
> |
crash_reason, |
1937 |
> |
r->pc(), r->lr(), r->ctr(), r->msr(), |
1938 |
> |
r->xer(), r->cr(), |
1939 |
> |
r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3), |
1940 |
> |
r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7), |
1941 |
> |
r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11), |
1942 |
> |
r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15), |
1943 |
> |
r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19), |
1944 |
> |
r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23), |
1945 |
> |
r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27), |
1946 |
> |
r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31)); |
1947 |
|
exit(1); |
1948 |
|
QuitEmulator(); |
1949 |
|
return; |
1950 |
|
} else { |
1951 |
|
// We crashed. Save registers, tell tick thread and loop forever |
1952 |
< |
sigsegv_regs = *(sigregs *)r; |
1952 |
> |
build_sigregs(&sigsegv_regs, r); |
1953 |
|
emul_thread_fatal = true; |
1954 |
|
for (;;) ; |
1955 |
|
} |
1961 |
|
* SIGILL handler |
1962 |
|
*/ |
1963 |
|
|
1964 |
< |
static void sigill_handler(int sig, sigcontext_struct *sc) |
1964 |
> |
static void sigill_handler(int sig, siginfo_t *sip, void *scp) |
1965 |
|
{ |
1966 |
< |
pt_regs *r = sc->regs; |
1966 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1967 |
|
char str[256]; |
1968 |
|
|
1969 |
|
// Fault in Mac ROM or RAM? |
1970 |
< |
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
1970 |
> |
bool mac_fault = (r->pc() >= ROM_BASE) && (r->pc() < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize)); |
1971 |
|
if (mac_fault) { |
1972 |
|
|
1973 |
|
// Get opcode and divide into fields |
1974 |
< |
uint32 opcode = *((uint32 *)r->nip); |
1974 |
> |
uint32 opcode = *((uint32 *)r->pc()); |
1975 |
|
uint32 primop = opcode >> 26; |
1976 |
|
uint32 exop = (opcode >> 1) & 0x3ff; |
1977 |
|
uint32 ra = (opcode >> 16) & 0x1f; |
1982 |
|
switch (primop) { |
1983 |
|
case 9: // POWER instructions |
1984 |
|
case 22: |
1985 |
< |
power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->nip, r->gpr[1], opcode); |
1985 |
> |
power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->pc(), r->gpr(1), opcode); |
1986 |
|
ErrorAlert(str); |
1987 |
|
QuitEmulator(); |
1988 |
|
return; |
1990 |
|
case 31: |
1991 |
|
switch (exop) { |
1992 |
|
case 83: // mfmsr |
1993 |
< |
r->gpr[rd] = 0xf072; |
1994 |
< |
r->nip += 4; |
1993 |
> |
r->gpr(rd) = 0xf072; |
1994 |
> |
r->pc() += 4; |
1995 |
|
goto rti; |
1996 |
|
|
1997 |
|
case 210: // mtsr |
1998 |
|
case 242: // mtsrin |
1999 |
|
case 306: // tlbie |
2000 |
< |
r->nip += 4; |
2000 |
> |
r->pc() += 4; |
2001 |
|
goto rti; |
2002 |
|
|
2003 |
|
case 339: { // mfspr |
2013 |
|
case 957: // PMC3 |
2014 |
|
case 958: // PMC4 |
2015 |
|
case 959: // SDA |
2016 |
< |
r->nip += 4; |
2016 |
> |
r->pc() += 4; |
2017 |
|
goto rti; |
2018 |
|
case 25: // SDR1 |
2019 |
< |
r->gpr[rd] = 0xdead001f; |
2020 |
< |
r->nip += 4; |
2019 |
> |
r->gpr(rd) = 0xdead001f; |
2020 |
> |
r->pc() += 4; |
2021 |
|
goto rti; |
2022 |
|
case 287: // PVR |
2023 |
< |
r->gpr[rd] = PVR; |
2024 |
< |
r->nip += 4; |
2023 |
> |
r->gpr(rd) = PVR; |
2024 |
> |
r->pc() += 4; |
2025 |
|
goto rti; |
2026 |
|
} |
2027 |
|
break; |
2057 |
|
case 957: // PMC3 |
2058 |
|
case 958: // PMC4 |
2059 |
|
case 959: // SDA |
2060 |
< |
r->nip += 4; |
2060 |
> |
r->pc() += 4; |
2061 |
|
goto rti; |
2062 |
|
} |
2063 |
|
break; |
2076 |
|
|
2077 |
|
// In GUI mode, show error alert |
2078 |
|
if (!PrefsFindBool("nogui")) { |
2079 |
< |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode); |
2079 |
> |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode); |
2080 |
|
ErrorAlert(str); |
2081 |
|
QuitEmulator(); |
2082 |
|
return; |
2084 |
|
} |
2085 |
|
|
2086 |
|
// For all other errors, jump into debugger (sort of...) |
2087 |
+ |
crash_reason = "SIGILL"; |
2088 |
|
if (!ready_for_signals) { |
2089 |
< |
printf("SIGILL\n"); |
2090 |
< |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
2089 |
> |
printf("%s\n"); |
2090 |
> |
printf(" sigcontext %p, machine_regs %p\n", scp, r); |
2091 |
|
printf( |
2092 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
2093 |
|
" xer %08lx cr %08lx \n" |
2099 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
2100 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
2101 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
2102 |
< |
r->nip, r->link, r->ctr, r->msr, |
2103 |
< |
r->xer, r->ccr, |
2104 |
< |
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
2105 |
< |
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
2106 |
< |
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
2107 |
< |
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
2108 |
< |
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
2109 |
< |
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
2110 |
< |
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
2111 |
< |
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
2102 |
> |
crash_reason, |
2103 |
> |
r->pc(), r->lr(), r->ctr(), r->msr(), |
2104 |
> |
r->xer(), r->cr(), |
2105 |
> |
r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3), |
2106 |
> |
r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7), |
2107 |
> |
r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11), |
2108 |
> |
r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15), |
2109 |
> |
r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19), |
2110 |
> |
r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23), |
2111 |
> |
r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27), |
2112 |
> |
r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31)); |
2113 |
|
exit(1); |
2114 |
|
QuitEmulator(); |
2115 |
|
return; |
2116 |
|
} else { |
2117 |
|
// We crashed. Save registers, tell tick thread and loop forever |
2118 |
< |
sigsegv_regs = *(sigregs *)r; |
2118 |
> |
build_sigregs(&sigsegv_regs, r); |
2119 |
|
emul_thread_fatal = true; |
2120 |
|
for (;;) ; |
2121 |
|
} |
2125 |
|
|
2126 |
|
|
2127 |
|
/* |
2128 |
+ |
* Helpers to share 32-bit addressable data with MacOS |
2129 |
+ |
*/ |
2130 |
+ |
|
2131 |
+ |
bool SheepMem::Init(void) |
2132 |
+ |
{ |
2133 |
+ |
// Size of a native page |
2134 |
+ |
page_size = getpagesize(); |
2135 |
+ |
|
2136 |
+ |
// Allocate SheepShaver globals |
2137 |
+ |
if (vm_acquire_fixed((char *)base, size) < 0) |
2138 |
+ |
return false; |
2139 |
+ |
|
2140 |
+ |
// Allocate page with all bits set to 0 |
2141 |
+ |
zero_page = base + size; |
2142 |
+ |
if (vm_acquire_fixed((char *)zero_page, page_size) < 0) |
2143 |
+ |
return false; |
2144 |
+ |
memset((char *)zero_page, 0, page_size); |
2145 |
+ |
if (vm_protect((char *)zero_page, page_size, VM_PAGE_READ) < 0) |
2146 |
+ |
return false; |
2147 |
+ |
|
2148 |
+ |
#if EMULATED_PPC |
2149 |
+ |
// Allocate alternate stack for PowerPC interrupt routine |
2150 |
+ |
sig_stack = zero_page + page_size; |
2151 |
+ |
if (vm_acquire_fixed((char *)sig_stack, SIG_STACK_SIZE) < 0) |
2152 |
+ |
return false; |
2153 |
+ |
#endif |
2154 |
+ |
|
2155 |
+ |
top = base + size; |
2156 |
+ |
return true; |
2157 |
+ |
} |
2158 |
+ |
|
2159 |
+ |
void SheepMem::Exit(void) |
2160 |
+ |
{ |
2161 |
+ |
if (top) { |
2162 |
+ |
// Delete SheepShaver globals |
2163 |
+ |
vm_release((void *)base, size); |
2164 |
+ |
|
2165 |
+ |
// Delete zero page |
2166 |
+ |
vm_release((void *)zero_page, page_size); |
2167 |
+ |
|
2168 |
+ |
#if EMULATED_PPC |
2169 |
+ |
// Delete alternate stack for PowerPC interrupt routine |
2170 |
+ |
vm_release((void *)sig_stack, SIG_STACK_SIZE); |
2171 |
+ |
#endif |
2172 |
+ |
} |
2173 |
+ |
} |
2174 |
+ |
|
2175 |
+ |
|
2176 |
+ |
/* |
2177 |
|
* Display alert |
2178 |
|
*/ |
2179 |
|
|
2222 |
|
|
2223 |
|
void ErrorAlert(const char *text) |
2224 |
|
{ |
2225 |
< |
#ifdef ENABLE_GTK |
2225 |
> |
#if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO) |
2226 |
|
if (PrefsFindBool("nogui") || x_display == NULL) { |
2227 |
|
printf(GetString(STR_SHELL_ERROR_PREFIX), text); |
2228 |
|
return; |
2241 |
|
|
2242 |
|
void WarningAlert(const char *text) |
2243 |
|
{ |
2244 |
< |
#ifdef ENABLE_GTK |
2244 |
> |
#if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO) |
2245 |
|
if (PrefsFindBool("nogui") || x_display == NULL) { |
2246 |
|
printf(GetString(STR_SHELL_WARNING_PREFIX), text); |
2247 |
|
return; |