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 |
+ |
// Install SIGSEGV and SIGBUS handlers |
515 |
+ |
sigemptyset(&sigsegv_action.sa_mask); // Block interrupts during SEGV handling |
516 |
+ |
sigaddset(&sigsegv_action.sa_mask, SIGUSR2); |
517 |
+ |
sigsegv_action.sa_sigaction = sigsegv_handler; |
518 |
+ |
sigsegv_action.sa_flags = SA_ONSTACK | SA_SIGINFO; |
519 |
+ |
#ifdef HAVE_SIGNAL_SA_RESTORER |
520 |
+ |
sigsegv_action.sa_restorer = NULL; |
521 |
+ |
#endif |
522 |
+ |
if (sigaction(SIGSEGV, &sigsegv_action, NULL) < 0) { |
523 |
+ |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
524 |
+ |
ErrorAlert(str); |
525 |
+ |
goto quit; |
526 |
+ |
} |
527 |
+ |
if (sigaction(SIGBUS, &sigsegv_action, NULL) < 0) { |
528 |
+ |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
529 |
+ |
ErrorAlert(str); |
530 |
+ |
goto quit; |
531 |
+ |
} |
532 |
+ |
#else |
533 |
+ |
// Install SIGSEGV handler for CPU emulator |
534 |
+ |
if (!sigsegv_install_handler(sigsegv_handler)) { |
535 |
+ |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
536 |
+ |
ErrorAlert(str); |
537 |
+ |
goto quit; |
538 |
+ |
} |
539 |
+ |
#endif |
540 |
+ |
|
541 |
+ |
// Initialize VM system |
542 |
+ |
vm_init(); |
543 |
+ |
|
544 |
|
// Get system info |
545 |
|
PVR = 0x00040000; // Default: 604 |
546 |
|
CPUClockSpeed = 100000000; // Default: 100MHz |
547 |
|
BusClockSpeed = 100000000; // Default: 100MHz |
548 |
< |
#if !EMULATED_PPC |
548 |
> |
#if EMULATED_PPC |
549 |
> |
PVR = 0x000c0000; // Default: 7400 (with AltiVec) |
550 |
> |
#elif defined(__APPLE__) && defined(__MACH__) |
551 |
> |
proc_file = popen("ioreg -c IOPlatformDevice", "r"); |
552 |
> |
if (proc_file) { |
553 |
> |
char line[256]; |
554 |
> |
bool powerpc_node = false; |
555 |
> |
while (fgets(line, sizeof(line) - 1, proc_file)) { |
556 |
> |
// Read line |
557 |
> |
int len = strlen(line); |
558 |
> |
if (len == 0) |
559 |
> |
continue; |
560 |
> |
line[len - 1] = 0; |
561 |
> |
|
562 |
> |
// Parse line |
563 |
> |
if (strstr(line, "o PowerPC,")) |
564 |
> |
powerpc_node = true; |
565 |
> |
else if (powerpc_node) { |
566 |
> |
uint32 value; |
567 |
> |
char head[256]; |
568 |
> |
if (sscanf(line, "%[ |]\"cpu-version\" = <%x>", head, &value) == 2) |
569 |
> |
PVR = value; |
570 |
> |
else if (sscanf(line, "%[ |]\"clock-frequency\" = <%x>", head, &value) == 2) |
571 |
> |
CPUClockSpeed = value; |
572 |
> |
else if (sscanf(line, "%[ |]\"bus-frequency\" = <%x>", head, &value) == 2) |
573 |
> |
BusClockSpeed = value; |
574 |
> |
else if (strchr(line, '}')) |
575 |
> |
powerpc_node = false; |
576 |
> |
} |
577 |
> |
} |
578 |
> |
fclose(proc_file); |
579 |
> |
} else { |
580 |
> |
sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno)); |
581 |
> |
WarningAlert(str); |
582 |
> |
} |
583 |
> |
#else |
584 |
|
proc_file = fopen("/proc/cpuinfo", "r"); |
585 |
|
if (proc_file) { |
586 |
|
char line[256]; |
594 |
|
// Parse line |
595 |
|
int i; |
596 |
|
char value[256]; |
597 |
< |
if (sscanf(line, "cpu : %s", value) == 1) { |
597 |
> |
if (sscanf(line, "cpu : %[0-9A-Za-a]", value) == 1) { |
598 |
|
if (strcmp(value, "601") == 0) |
599 |
|
PVR = 0x00010000; |
600 |
|
else if (strcmp(value, "603") == 0) |
615 |
|
PVR = 0x00320000; |
616 |
|
else if (strcmp(value, "860") == 0) |
617 |
|
PVR = 0x00500000; |
618 |
+ |
else if (strcmp(value, "7400") == 0) |
619 |
+ |
PVR = 0x000c0000; |
620 |
+ |
else if (strcmp(value, "7410") == 0) |
621 |
+ |
PVR = 0x800c0000; |
622 |
|
else |
623 |
|
printf("WARNING: Unknown CPU type '%s', assuming 604\n", value); |
624 |
|
} |
630 |
|
sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno)); |
631 |
|
WarningAlert(str); |
632 |
|
} |
633 |
+ |
|
634 |
+ |
// Get actual bus frequency |
635 |
+ |
proc_file = fopen("/proc/device-tree/clock-frequency", "r"); |
636 |
+ |
if (proc_file) { |
637 |
+ |
union { uint8 b[4]; uint32 l; } value; |
638 |
+ |
if (fread(value.b, sizeof(value), 1, proc_file) == 1) |
639 |
+ |
BusClockSpeed = value.l; |
640 |
+ |
fclose(proc_file); |
641 |
+ |
} |
642 |
|
#endif |
643 |
|
D(bug("PVR: %08x (assumed)\n", PVR)); |
644 |
|
|
663 |
|
goto quit; |
664 |
|
} |
665 |
|
|
666 |
+ |
#ifndef PAGEZERO_HACK |
667 |
|
// Create Low Memory area (0x0000..0x3000) |
668 |
|
if (vm_acquire_fixed((char *)0, 0x3000) < 0) { |
669 |
|
sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno)); |
671 |
|
goto quit; |
672 |
|
} |
673 |
|
lm_area_mapped = true; |
674 |
+ |
#endif |
675 |
|
|
676 |
|
// Create areas for Kernel Data |
677 |
|
kernel_area = shmget(IPC_PRIVATE, KERNEL_AREA_SIZE, 0600); |
690 |
|
ErrorAlert(str); |
691 |
|
goto quit; |
692 |
|
} |
693 |
< |
kernel_data = (KernelData *)0x68ffe000; |
693 |
> |
kernel_data = (KernelData *)KERNEL_DATA_BASE; |
694 |
|
emulator_data = &kernel_data->ed; |
695 |
< |
KernelDataAddr = (uint32)kernel_data; |
695 |
> |
KernelDataAddr = KERNEL_DATA_BASE; |
696 |
|
D(bug("Kernel Data at %p, Emulator Data at %p\n", kernel_data, emulator_data)); |
697 |
|
|
698 |
+ |
// Create area for DR Cache |
699 |
+ |
if (vm_acquire_fixed((void *)DR_EMULATOR_BASE, DR_EMULATOR_SIZE) < 0) { |
700 |
+ |
sprintf(str, GetString(STR_DR_EMULATOR_MMAP_ERR), strerror(errno)); |
701 |
+ |
ErrorAlert(str); |
702 |
+ |
goto quit; |
703 |
+ |
} |
704 |
+ |
dr_emulator_area_mapped = true; |
705 |
+ |
if (vm_acquire_fixed((void *)DR_CACHE_BASE, DR_CACHE_SIZE) < 0) { |
706 |
+ |
sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno)); |
707 |
+ |
ErrorAlert(str); |
708 |
+ |
goto quit; |
709 |
+ |
} |
710 |
+ |
dr_cache_area_mapped = true; |
711 |
+ |
#if !EMULATED_PPC |
712 |
+ |
if (vm_protect((char *)DR_CACHE_BASE, DR_CACHE_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
713 |
+ |
sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno)); |
714 |
+ |
ErrorAlert(str); |
715 |
+ |
goto quit; |
716 |
+ |
} |
717 |
+ |
#endif |
718 |
+ |
DRCacheAddr = DR_CACHE_BASE; |
719 |
+ |
D(bug("DR Cache at %p\n", DRCacheAddr)); |
720 |
+ |
|
721 |
|
// Create area for SheepShaver data |
722 |
< |
if (vm_acquire_fixed((char *)SHEEP_BASE, SHEEP_SIZE) < 0) { |
722 |
> |
if (!SheepMem::Init()) { |
723 |
|
sprintf(str, GetString(STR_SHEEP_MEM_MMAP_ERR), strerror(errno)); |
724 |
|
ErrorAlert(str); |
725 |
|
goto quit; |
726 |
|
} |
484 |
– |
SheepStack1Base = SHEEP_BASE + 0x10000; |
485 |
– |
SheepStack2Base = SheepStack1Base + 0x10000; |
486 |
– |
SheepThunksBase = SheepStack2Base + 0x1000; |
487 |
– |
sheep_area_mapped = true; |
727 |
|
|
728 |
|
// Create area for Mac ROM |
729 |
|
if (vm_acquire_fixed((char *)ROM_BASE, ROM_AREA_SIZE) < 0) { |
731 |
|
ErrorAlert(str); |
732 |
|
goto quit; |
733 |
|
} |
734 |
< |
#if !EMULATED_PPC || defined(__powerpc__) |
734 |
> |
#if !EMULATED_PPC |
735 |
|
if (vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
736 |
|
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
737 |
|
ErrorAlert(str); |
801 |
|
// Load NVRAM |
802 |
|
XPRAMInit(); |
803 |
|
|
804 |
+ |
// Load XPRAM default values if signature not found |
805 |
+ |
if (XPRAM[0x130c] != 0x4e || XPRAM[0x130d] != 0x75 |
806 |
+ |
|| XPRAM[0x130e] != 0x4d || XPRAM[0x130f] != 0x63) { |
807 |
+ |
D(bug("Loading XPRAM default values\n")); |
808 |
+ |
memset(XPRAM + 0x1300, 0, 0x100); |
809 |
+ |
XPRAM[0x130c] = 0x4e; // "NuMc" signature |
810 |
+ |
XPRAM[0x130d] = 0x75; |
811 |
+ |
XPRAM[0x130e] = 0x4d; |
812 |
+ |
XPRAM[0x130f] = 0x63; |
813 |
+ |
XPRAM[0x1301] = 0x80; // InternalWaitFlags = DynWait (don't wait for SCSI devices upon bootup) |
814 |
+ |
XPRAM[0x1310] = 0xa8; // Standard PRAM values |
815 |
+ |
XPRAM[0x1311] = 0x00; |
816 |
+ |
XPRAM[0x1312] = 0x00; |
817 |
+ |
XPRAM[0x1313] = 0x22; |
818 |
+ |
XPRAM[0x1314] = 0xcc; |
819 |
+ |
XPRAM[0x1315] = 0x0a; |
820 |
+ |
XPRAM[0x1316] = 0xcc; |
821 |
+ |
XPRAM[0x1317] = 0x0a; |
822 |
+ |
XPRAM[0x131c] = 0x00; |
823 |
+ |
XPRAM[0x131d] = 0x02; |
824 |
+ |
XPRAM[0x131e] = 0x63; |
825 |
+ |
XPRAM[0x131f] = 0x00; |
826 |
+ |
XPRAM[0x1308] = 0x13; |
827 |
+ |
XPRAM[0x1309] = 0x88; |
828 |
+ |
XPRAM[0x130a] = 0x00; |
829 |
+ |
XPRAM[0x130b] = 0xcc; |
830 |
+ |
XPRAM[0x1376] = 0x00; // OSDefault = MacOS |
831 |
+ |
XPRAM[0x1377] = 0x01; |
832 |
+ |
} |
833 |
+ |
|
834 |
|
// Set boot volume |
835 |
|
i16 = PrefsFindInt32("bootdrive"); |
836 |
|
XPRAM[0x1378] = i16 >> 8; |
848 |
|
boot_globs[1] = htonl(RAMSize); |
849 |
|
boot_globs[2] = htonl((uint32)-1); // End of bank table |
850 |
|
|
851 |
+ |
// Init thunks |
852 |
+ |
if (!ThunksInit()) |
853 |
+ |
goto quit; |
854 |
+ |
|
855 |
|
// Init drivers |
856 |
|
SonyInit(); |
857 |
|
DiskInit(); |
861 |
|
// Init external file system |
862 |
|
ExtFSInit(); |
863 |
|
|
864 |
+ |
// Init ADB |
865 |
+ |
ADBInit(); |
866 |
+ |
|
867 |
|
// Init audio |
868 |
|
AudioInit(); |
869 |
|
|
898 |
|
// Initialize Kernel Data |
899 |
|
memset(kernel_data, 0, sizeof(KernelData)); |
900 |
|
if (ROMType == ROMTYPE_NEWWORLD) { |
901 |
< |
static uint32 of_dev_tree[4] = {0, 0, 0, 0}; |
902 |
< |
static uint8 vector_lookup_tbl[128]; |
903 |
< |
static uint8 vector_mask_tbl[64]; |
901 |
> |
uintptr of_dev_tree = SheepMem::Reserve(4 * sizeof(uint32)); |
902 |
> |
memset((void *)of_dev_tree, 0, 4 * sizeof(uint32)); |
903 |
> |
uintptr vector_lookup_tbl = SheepMem::Reserve(128); |
904 |
> |
uintptr vector_mask_tbl = SheepMem::Reserve(64); |
905 |
|
memset((uint8 *)kernel_data + 0xb80, 0x3d, 0x80); |
906 |
< |
memset(vector_lookup_tbl, 0, 128); |
907 |
< |
memset(vector_mask_tbl, 0, 64); |
906 |
> |
memset((void *)vector_lookup_tbl, 0, 128); |
907 |
> |
memset((void *)vector_mask_tbl, 0, 64); |
908 |
|
kernel_data->v[0xb80 >> 2] = htonl(ROM_BASE); |
909 |
< |
kernel_data->v[0xb84 >> 2] = htonl((uint32)of_dev_tree); // OF device tree base |
910 |
< |
kernel_data->v[0xb90 >> 2] = htonl((uint32)vector_lookup_tbl); |
911 |
< |
kernel_data->v[0xb94 >> 2] = htonl((uint32)vector_mask_tbl); |
909 |
> |
kernel_data->v[0xb84 >> 2] = htonl(of_dev_tree); // OF device tree base |
910 |
> |
kernel_data->v[0xb90 >> 2] = htonl(vector_lookup_tbl); |
911 |
> |
kernel_data->v[0xb94 >> 2] = htonl(vector_mask_tbl); |
912 |
|
kernel_data->v[0xb98 >> 2] = htonl(ROM_BASE); // OpenPIC base |
913 |
|
kernel_data->v[0xbb0 >> 2] = htonl(0); // ADB base |
914 |
|
kernel_data->v[0xc20 >> 2] = htonl(RAMSize); |
921 |
|
kernel_data->v[0xc50 >> 2] = htonl(RAMBase); |
922 |
|
kernel_data->v[0xc54 >> 2] = htonl(RAMSize); |
923 |
|
kernel_data->v[0xf60 >> 2] = htonl(PVR); |
924 |
< |
kernel_data->v[0xf64 >> 2] = htonl(CPUClockSpeed); |
925 |
< |
kernel_data->v[0xf68 >> 2] = htonl(BusClockSpeed); |
926 |
< |
kernel_data->v[0xf6c >> 2] = htonl(CPUClockSpeed); |
924 |
> |
kernel_data->v[0xf64 >> 2] = htonl(CPUClockSpeed); // clock-frequency |
925 |
> |
kernel_data->v[0xf68 >> 2] = htonl(BusClockSpeed); // bus-frequency |
926 |
> |
kernel_data->v[0xf6c >> 2] = htonl(BusClockSpeed / 4); // timebase-frequency |
927 |
|
} else { |
928 |
|
kernel_data->v[0xc80 >> 2] = htonl(RAMSize); |
929 |
|
kernel_data->v[0xc84 >> 2] = htonl(RAMSize); |
935 |
|
kernel_data->v[0xcb0 >> 2] = htonl(RAMBase); |
936 |
|
kernel_data->v[0xcb4 >> 2] = htonl(RAMSize); |
937 |
|
kernel_data->v[0xf80 >> 2] = htonl(PVR); |
938 |
< |
kernel_data->v[0xf84 >> 2] = htonl(CPUClockSpeed); |
939 |
< |
kernel_data->v[0xf88 >> 2] = htonl(BusClockSpeed); |
940 |
< |
kernel_data->v[0xf8c >> 2] = htonl(CPUClockSpeed); |
938 |
> |
kernel_data->v[0xf84 >> 2] = htonl(CPUClockSpeed); // clock-frequency |
939 |
> |
kernel_data->v[0xf88 >> 2] = htonl(BusClockSpeed); // bus-frequency |
940 |
> |
kernel_data->v[0xf8c >> 2] = htonl(BusClockSpeed / 4); // timebase-frequency |
941 |
|
} |
942 |
|
|
943 |
|
// Initialize extra low memory |
944 |
|
D(bug("Initializing Low Memory...\n")); |
945 |
|
memset(NULL, 0, 0x3000); |
946 |
|
WriteMacInt32(XLM_SIGNATURE, FOURCC('B','a','a','h')); // Signature to detect SheepShaver |
947 |
< |
WriteMacInt32(XLM_KERNEL_DATA, (uint32)kernel_data); // For trap replacement routines |
947 |
> |
WriteMacInt32(XLM_KERNEL_DATA, KernelDataAddr); // For trap replacement routines |
948 |
|
WriteMacInt32(XLM_PVR, PVR); // Theoretical PVR |
949 |
|
WriteMacInt32(XLM_BUS_CLOCK, BusClockSpeed); // For DriverServicesLib patch |
950 |
|
WriteMacInt16(XLM_EXEC_RETURN_OPCODE, M68K_EXEC_RETURN); // For Execute68k() (RTS from the executed 68k code will jump here and end 68k mode) |
951 |
< |
#if EMULATED_PPC |
952 |
< |
WriteMacInt32(XLM_ETHER_INIT, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_INIT)); |
953 |
< |
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); |
951 |
> |
WriteMacInt32(XLM_ZERO_PAGE, SheepMem::ZeroPage()); // Pointer to read-only page with all bits set to 0 |
952 |
> |
#if !EMULATED_PPC |
953 |
> |
WriteMacInt32(XLM_TOC, (uint32)TOC); // TOC pointer of emulator |
954 |
|
#endif |
955 |
+ |
WriteMacInt32(XLM_ETHER_INIT, NativeFunction(NATIVE_ETHER_INIT)); // DLPI ethernet driver functions |
956 |
+ |
WriteMacInt32(XLM_ETHER_TERM, NativeFunction(NATIVE_ETHER_TERM)); |
957 |
+ |
WriteMacInt32(XLM_ETHER_OPEN, NativeFunction(NATIVE_ETHER_OPEN)); |
958 |
+ |
WriteMacInt32(XLM_ETHER_CLOSE, NativeFunction(NATIVE_ETHER_CLOSE)); |
959 |
+ |
WriteMacInt32(XLM_ETHER_WPUT, NativeFunction(NATIVE_ETHER_WPUT)); |
960 |
+ |
WriteMacInt32(XLM_ETHER_RSRV, NativeFunction(NATIVE_ETHER_RSRV)); |
961 |
+ |
WriteMacInt32(XLM_VIDEO_DOIO, NativeFunction(NATIVE_VIDEO_DO_DRIVER_IO)); |
962 |
|
D(bug("Low Memory initialized\n")); |
963 |
|
|
964 |
|
// Start 60Hz thread |
965 |
+ |
tick_thread_cancel = false; |
966 |
|
tick_thread_active = (pthread_create(&tick_thread, NULL, tick_func, NULL) == 0); |
967 |
|
D(bug("Tick thread installed (%ld)\n", tick_thread)); |
968 |
|
|
969 |
|
// Start NVRAM watchdog thread |
970 |
|
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
971 |
+ |
nvram_thread_cancel = false; |
972 |
|
nvram_thread_active = (pthread_create(&nvram_thread, NULL, nvram_func, NULL) == 0); |
973 |
|
D(bug("NVRAM thread installed (%ld)\n", nvram_thread)); |
974 |
|
|
975 |
|
#if !EMULATED_PPC |
976 |
|
// Create and install stacks for signal handlers |
977 |
< |
sig_stack = malloc(SIG_STACK_SIZE); |
978 |
< |
D(bug("Signal stack at %p\n", sig_stack)); |
979 |
< |
if (sig_stack == NULL) { |
980 |
< |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
981 |
< |
goto quit; |
982 |
< |
} |
983 |
< |
extra_stack = malloc(SIG_STACK_SIZE); |
984 |
< |
D(bug("Extra stack at %p\n", extra_stack)); |
985 |
< |
if (extra_stack == NULL) { |
986 |
< |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
987 |
< |
goto quit; |
988 |
< |
} |
989 |
< |
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) { |
977 |
> |
for (int i = 0; i < SIG_STACK_COUNT; i++) { |
978 |
> |
void *sig_stack = malloc(SIG_STACK_SIZE); |
979 |
> |
D(bug("Signal stack %d at %p\n", i, sig_stack)); |
980 |
> |
if (sig_stack == NULL) { |
981 |
> |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
982 |
> |
goto quit; |
983 |
> |
} |
984 |
> |
sig_stacks[i].ss_sp = sig_stack; |
985 |
> |
sig_stacks[i].ss_flags = 0; |
986 |
> |
sig_stacks[i].ss_size = SIG_STACK_SIZE; |
987 |
> |
} |
988 |
> |
sig_stack_id = 0; |
989 |
> |
if (sigaltstack(&sig_stacks[0], NULL) < 0) { |
990 |
|
sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno)); |
991 |
|
ErrorAlert(str); |
992 |
|
goto quit; |
994 |
|
#endif |
995 |
|
|
996 |
|
#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 |
– |
|
997 |
|
// Install SIGILL handler |
998 |
|
sigemptyset(&sigill_action.sa_mask); // Block interrupts during ILL handling |
999 |
|
sigaddset(&sigill_action.sa_mask, SIGUSR2); |
1000 |
< |
sigill_action.sa_handler = (__sighandler_t)sigill_handler; |
1001 |
< |
sigill_action.sa_flags = SA_ONSTACK; |
1000 |
> |
sigill_action.sa_sigaction = sigill_handler; |
1001 |
> |
sigill_action.sa_flags = SA_ONSTACK | SA_SIGINFO; |
1002 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
1003 |
|
sigill_action.sa_restorer = NULL; |
1004 |
+ |
#endif |
1005 |
|
if (sigaction(SIGILL, &sigill_action, NULL) < 0) { |
1006 |
|
sprintf(str, GetString(STR_SIGILL_INSTALL_ERR), strerror(errno)); |
1007 |
|
ErrorAlert(str); |
1009 |
|
} |
1010 |
|
#endif |
1011 |
|
|
1012 |
+ |
#if !EMULATED_PPC |
1013 |
|
// Install interrupt signal handler |
1014 |
|
sigemptyset(&sigusr2_action.sa_mask); |
1015 |
< |
sigusr2_action.sa_handler = (__sighandler_t)sigusr2_handler; |
1016 |
< |
sigusr2_action.sa_flags = 0; |
1017 |
< |
#if !EMULATED_PPC |
759 |
< |
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART; |
760 |
< |
#endif |
1015 |
> |
sigusr2_action.sa_sigaction = sigusr2_handler; |
1016 |
> |
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART | SA_SIGINFO; |
1017 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
1018 |
|
sigusr2_action.sa_restorer = NULL; |
1019 |
+ |
#endif |
1020 |
|
if (sigaction(SIGUSR2, &sigusr2_action, NULL) < 0) { |
1021 |
|
sprintf(str, GetString(STR_SIGUSR2_INSTALL_ERR), strerror(errno)); |
1022 |
|
ErrorAlert(str); |
1023 |
|
goto quit; |
1024 |
|
} |
1025 |
+ |
#endif |
1026 |
|
|
1027 |
|
// Get my thread ID and execute MacOS thread function |
1028 |
|
emul_thread = pthread_self(); |
1048 |
|
|
1049 |
|
// Stop 60Hz thread |
1050 |
|
if (tick_thread_active) { |
1051 |
+ |
tick_thread_cancel = true; |
1052 |
|
pthread_cancel(tick_thread); |
1053 |
|
pthread_join(tick_thread, NULL); |
1054 |
|
} |
1055 |
|
|
1056 |
|
// Stop NVRAM watchdog thread |
1057 |
|
if (nvram_thread_active) { |
1058 |
+ |
nvram_thread_cancel = true; |
1059 |
|
pthread_cancel(nvram_thread); |
1060 |
|
pthread_join(nvram_thread, NULL); |
1061 |
|
} |
1062 |
|
|
1063 |
|
#if !EMULATED_PPC |
1064 |
< |
// Uninstall SIGSEGV handler |
1064 |
> |
// Uninstall SIGSEGV and SIGBUS handlers |
1065 |
|
sigemptyset(&sigsegv_action.sa_mask); |
1066 |
|
sigsegv_action.sa_handler = SIG_DFL; |
1067 |
|
sigsegv_action.sa_flags = 0; |
1068 |
|
sigaction(SIGSEGV, &sigsegv_action, NULL); |
1069 |
+ |
sigaction(SIGBUS, &sigsegv_action, NULL); |
1070 |
|
|
1071 |
|
// Uninstall SIGILL handler |
1072 |
|
sigemptyset(&sigill_action.sa_mask); |
1073 |
|
sigill_action.sa_handler = SIG_DFL; |
1074 |
|
sigill_action.sa_flags = 0; |
1075 |
|
sigaction(SIGILL, &sigill_action, NULL); |
1076 |
+ |
|
1077 |
+ |
// Delete stacks for signal handlers |
1078 |
+ |
for (int i = 0; i < SIG_STACK_COUNT; i++) { |
1079 |
+ |
void *sig_stack = sig_stacks[i].ss_sp; |
1080 |
+ |
if (sig_stack) |
1081 |
+ |
free(sig_stack); |
1082 |
+ |
} |
1083 |
|
#endif |
1084 |
|
|
1085 |
|
// Save NVRAM |
1100 |
|
// Exit audio |
1101 |
|
AudioExit(); |
1102 |
|
|
1103 |
+ |
// Exit ADB |
1104 |
+ |
ADBExit(); |
1105 |
+ |
|
1106 |
|
// Exit video |
1107 |
|
VideoExit(); |
1108 |
|
|
1115 |
|
DiskExit(); |
1116 |
|
SonyExit(); |
1117 |
|
|
1118 |
+ |
// Delete thunks |
1119 |
+ |
ThunksExit(); |
1120 |
+ |
|
1121 |
+ |
// Delete SheepShaver globals |
1122 |
+ |
SheepMem::Exit(); |
1123 |
+ |
|
1124 |
|
// Delete RAM area |
1125 |
|
if (ram_area_mapped) |
1126 |
|
vm_release((char *)RAM_BASE, RAMSize); |
1129 |
|
if (rom_area_mapped) |
1130 |
|
vm_release((char *)ROM_BASE, ROM_AREA_SIZE); |
1131 |
|
|
1132 |
+ |
// Delete DR cache areas |
1133 |
+ |
if (dr_emulator_area_mapped) |
1134 |
+ |
vm_release((void *)DR_EMULATOR_BASE, DR_EMULATOR_SIZE); |
1135 |
+ |
if (dr_cache_area_mapped) |
1136 |
+ |
vm_release((void *)DR_CACHE_BASE, DR_CACHE_SIZE); |
1137 |
+ |
|
1138 |
|
// Delete Kernel Data area |
1139 |
|
if (kernel_area >= 0) { |
1140 |
|
shmdt((void *)KERNEL_DATA_BASE); |
1162 |
|
#endif |
1163 |
|
|
1164 |
|
// Close X11 server connection |
1165 |
+ |
#ifndef USE_SDL_VIDEO |
1166 |
|
if (x_display) |
1167 |
|
XCloseDisplay(x_display); |
1168 |
+ |
#endif |
1169 |
|
|
1170 |
|
exit(0); |
1171 |
|
} |
1240 |
|
uint16 proc[2] = {trap, M68K_RTS}; |
1241 |
|
Execute68k((uint32)proc, r); |
1242 |
|
} |
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 |
– |
} |
1243 |
|
#endif |
1244 |
|
|
1245 |
|
|
1318 |
|
|
1319 |
|
void PatchAfterStartup(void) |
1320 |
|
{ |
1048 |
– |
#if EMULATED_PPC |
1321 |
|
ExecuteNative(NATIVE_VIDEO_INSTALL_ACCEL); |
1050 |
– |
#else |
1051 |
– |
ExecutePPC(VideoInstallAccel); |
1052 |
– |
#endif |
1322 |
|
InstallExtFS(); |
1323 |
|
} |
1324 |
|
|
1327 |
|
* NVRAM watchdog thread (saves NVRAM every minute) |
1328 |
|
*/ |
1329 |
|
|
1330 |
< |
static void *nvram_func(void *arg) |
1330 |
> |
static void nvram_watchdog(void) |
1331 |
|
{ |
1332 |
< |
struct timespec req = {60, 0}; // 1 minute |
1332 |
> |
if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) { |
1333 |
> |
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
1334 |
> |
SaveXPRAM(); |
1335 |
> |
} |
1336 |
> |
} |
1337 |
|
|
1338 |
< |
for (;;) { |
1339 |
< |
pthread_testcancel(); |
1340 |
< |
nanosleep(&req, NULL); |
1341 |
< |
pthread_testcancel(); |
1342 |
< |
if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) { |
1343 |
< |
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
1071 |
< |
SaveXPRAM(); |
1072 |
< |
} |
1338 |
> |
static void *nvram_func(void *arg) |
1339 |
> |
{ |
1340 |
> |
while (!nvram_thread_cancel) { |
1341 |
> |
for (int i=0; i<60 && !nvram_thread_cancel; i++) |
1342 |
> |
Delay_usec(999999); // Only wait 1 second so we quit promptly when nvram_thread_cancel becomes true |
1343 |
> |
nvram_watchdog(); |
1344 |
|
} |
1345 |
|
return NULL; |
1346 |
|
} |
1353 |
|
static void *tick_func(void *arg) |
1354 |
|
{ |
1355 |
|
int tick_counter = 0; |
1356 |
< |
struct timespec req = {0, 16625000}; |
1356 |
> |
uint64 start = GetTicks_usec(); |
1357 |
> |
int64 ticks = 0; |
1358 |
> |
uint64 next = GetTicks_usec(); |
1359 |
|
|
1360 |
< |
for (;;) { |
1360 |
> |
while (!tick_thread_cancel) { |
1361 |
|
|
1362 |
|
// Wait |
1363 |
< |
nanosleep(&req, NULL); |
1363 |
> |
next += 16625; |
1364 |
> |
int64 delay = next - GetTicks_usec(); |
1365 |
> |
if (delay > 0) |
1366 |
> |
Delay_usec(delay); |
1367 |
> |
else if (delay < -16625) |
1368 |
> |
next = GetTicks_usec(); |
1369 |
> |
ticks++; |
1370 |
|
|
1371 |
|
#if !EMULATED_PPC |
1372 |
|
// Did we crash? |
1373 |
|
if (emul_thread_fatal) { |
1374 |
|
|
1375 |
|
// Yes, dump registers |
1376 |
< |
pt_regs *r = (pt_regs *)&sigsegv_regs; |
1376 |
> |
sigregs *r = &sigsegv_regs; |
1377 |
|
char str[256]; |
1378 |
< |
sprintf(str, "SIGSEGV\n" |
1378 |
> |
if (crash_reason == NULL) |
1379 |
> |
crash_reason = "SIGSEGV"; |
1380 |
> |
sprintf(str, "%s\n" |
1381 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1382 |
|
" xer %08lx cr %08lx \n" |
1383 |
|
" r0 %08lx r1 %08lx r2 %08lx r3 %08lx\n" |
1388 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1389 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1390 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1391 |
+ |
crash_reason, |
1392 |
|
r->nip, r->link, r->ctr, r->msr, |
1393 |
|
r->xer, r->ccr, |
1394 |
|
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1424 |
|
TriggerInterrupt(); |
1425 |
|
} |
1426 |
|
} |
1427 |
+ |
|
1428 |
+ |
uint64 end = GetTicks_usec(); |
1429 |
+ |
D(bug("%Ld ticks in %Ld usec = %f ticks/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start))); |
1430 |
|
return NULL; |
1431 |
|
} |
1432 |
|
|
1547 |
|
* Trigger signal USR2 from another thread |
1548 |
|
*/ |
1549 |
|
|
1550 |
< |
#if !EMULATED_PPC || ASYNC_IRQ |
1550 |
> |
#if !EMULATED_PPC |
1551 |
|
void TriggerInterrupt(void) |
1552 |
|
{ |
1553 |
|
if (ready_for_signals) |
1579 |
|
|
1580 |
|
void DisableInterrupt(void) |
1581 |
|
{ |
1582 |
+ |
#if EMULATED_PPC |
1583 |
+ |
WriteMacInt32(XLM_IRQ_NEST, int32(ReadMacInt32(XLM_IRQ_NEST)) + 1); |
1584 |
+ |
#else |
1585 |
|
atomic_add((int *)XLM_IRQ_NEST, 1); |
1586 |
+ |
#endif |
1587 |
|
} |
1588 |
|
|
1589 |
|
|
1593 |
|
|
1594 |
|
void EnableInterrupt(void) |
1595 |
|
{ |
1596 |
+ |
#if EMULATED_PPC |
1597 |
+ |
WriteMacInt32(XLM_IRQ_NEST, int32(ReadMacInt32(XLM_IRQ_NEST)) - 1); |
1598 |
+ |
#else |
1599 |
|
atomic_add((int *)XLM_IRQ_NEST, -1); |
1600 |
+ |
#endif |
1601 |
|
} |
1602 |
|
|
1603 |
|
|
1605 |
|
* USR2 handler |
1606 |
|
*/ |
1607 |
|
|
1608 |
< |
#if EMULATED_PPC |
1609 |
< |
static void sigusr2_handler(int sig) |
1608 |
> |
#if !EMULATED_PPC |
1609 |
> |
static void sigusr2_handler(int sig, siginfo_t *sip, void *scp) |
1610 |
|
{ |
1611 |
< |
#if ASYNC_IRQ |
1612 |
< |
extern void HandleInterrupt(void); |
1613 |
< |
HandleInterrupt(); |
1611 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1612 |
> |
|
1613 |
> |
#ifdef USE_SDL_VIDEO |
1614 |
> |
// We must fill in the events queue in the same thread that did call SDL_SetVideoMode() |
1615 |
> |
SDL_PumpEvents(); |
1616 |
|
#endif |
1322 |
– |
} |
1323 |
– |
#else |
1324 |
– |
static void sigusr2_handler(int sig, sigcontext_struct *sc) |
1325 |
– |
{ |
1326 |
– |
pt_regs *r = sc->regs; |
1617 |
|
|
1618 |
|
// Do nothing if interrupts are disabled |
1619 |
|
if (*(int32 *)XLM_IRQ_NEST > 0) |
1627 |
|
case MODE_68K: |
1628 |
|
// 68k emulator active, trigger 68k interrupt level 1 |
1629 |
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1630 |
< |
r->ccr |= ntohl(kernel_data->v[0x674 >> 2]); |
1630 |
> |
r->cr() |= ntohl(kernel_data->v[0x674 >> 2]); |
1631 |
|
break; |
1632 |
|
|
1633 |
|
#if INTERRUPTS_IN_NATIVE_MODE |
1634 |
|
case MODE_NATIVE: |
1635 |
|
// 68k emulator inactive, in nanokernel? |
1636 |
< |
if (r->gpr[1] != KernelDataAddr) { |
1636 |
> |
if (r->gpr(1) != KernelDataAddr) { |
1637 |
> |
|
1638 |
> |
// Set extra stack for nested interrupts |
1639 |
> |
sig_stack_acquire(); |
1640 |
> |
|
1641 |
|
// Prepare for 68k interrupt level 1 |
1642 |
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1643 |
|
WriteMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc, ReadMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc) | ntohl(kernel_data->v[0x674 >> 2])); |
1644 |
|
|
1645 |
|
// Execute nanokernel interrupt routine (this will activate the 68k emulator) |
1646 |
< |
atomic_add((int32 *)XLM_IRQ_NEST, 1); |
1646 |
> |
DisableInterrupt(); |
1647 |
|
if (ROMType == ROMTYPE_NEWWORLD) |
1648 |
|
ppc_interrupt(ROM_BASE + 0x312b1c, KernelDataAddr); |
1649 |
|
else |
1650 |
|
ppc_interrupt(ROM_BASE + 0x312a3c, KernelDataAddr); |
1651 |
+ |
|
1652 |
+ |
// Reset normal signal stack |
1653 |
+ |
sig_stack_release(); |
1654 |
|
} |
1655 |
|
break; |
1656 |
|
#endif |
1661 |
|
if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) { |
1662 |
|
|
1663 |
|
// Set extra stack for SIGSEGV handler |
1664 |
< |
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); |
1664 |
> |
sig_stack_acquire(); |
1665 |
|
#if 1 |
1666 |
|
// Execute full 68k interrupt routine |
1667 |
|
M68kRegisters r; |
1683 |
|
if (InterruptFlags & INTFLAG_VIA) { |
1684 |
|
ClearInterruptFlag(INTFLAG_VIA); |
1685 |
|
ADBInterrupt(); |
1686 |
< |
ExecutePPC(VideoVBL); |
1686 |
> |
ExecuteNative(NATIVE_VIDEO_VBL); |
1687 |
|
} |
1688 |
|
} |
1689 |
|
#endif |
1690 |
|
// Reset normal signal stack |
1691 |
< |
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); |
1691 |
> |
sig_stack_release(); |
1692 |
|
} |
1693 |
|
break; |
1694 |
|
#endif |
1702 |
|
*/ |
1703 |
|
|
1704 |
|
#if !EMULATED_PPC |
1705 |
< |
static void sigsegv_handler(int sig, sigcontext_struct *sc) |
1705 |
> |
static void sigsegv_handler(int sig, siginfo_t *sip, void *scp) |
1706 |
|
{ |
1707 |
< |
pt_regs *r = sc->regs; |
1707 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1708 |
|
|
1709 |
|
// Get effective address |
1710 |
< |
uint32 addr = r->dar; |
1710 |
> |
uint32 addr = r->dar(); |
1711 |
|
|
1712 |
|
#if ENABLE_VOSF |
1713 |
|
// Handle screen fault. |
1714 |
|
extern bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction); |
1715 |
< |
if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->nip)) |
1715 |
> |
if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->pc())) |
1716 |
|
return; |
1717 |
|
#endif |
1718 |
|
|
1719 |
|
num_segv++; |
1720 |
|
|
1721 |
< |
// Fault in Mac ROM or RAM? |
1722 |
< |
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
1721 |
> |
// Fault in Mac ROM or RAM or DR Cache? |
1722 |
> |
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)); |
1723 |
|
if (mac_fault) { |
1724 |
|
|
1725 |
|
// "VM settings" during MacOS 8 installation |
1726 |
< |
if (r->nip == ROM_BASE + 0x488160 && r->gpr[20] == 0xf8000000) { |
1727 |
< |
r->nip += 4; |
1728 |
< |
r->gpr[8] = 0; |
1726 |
> |
if (r->pc() == ROM_BASE + 0x488160 && r->gpr(20) == 0xf8000000) { |
1727 |
> |
r->pc() += 4; |
1728 |
> |
r->gpr(8) = 0; |
1729 |
|
return; |
1730 |
|
|
1731 |
|
// MacOS 8.5 installation |
1732 |
< |
} else if (r->nip == ROM_BASE + 0x488140 && r->gpr[16] == 0xf8000000) { |
1733 |
< |
r->nip += 4; |
1734 |
< |
r->gpr[8] = 0; |
1732 |
> |
} else if (r->pc() == ROM_BASE + 0x488140 && r->gpr(16) == 0xf8000000) { |
1733 |
> |
r->pc() += 4; |
1734 |
> |
r->gpr(8) = 0; |
1735 |
|
return; |
1736 |
|
|
1737 |
|
// MacOS 8 serial drivers on startup |
1738 |
< |
} else if (r->nip == ROM_BASE + 0x48e080 && (r->gpr[8] == 0xf3012002 || r->gpr[8] == 0xf3012000)) { |
1739 |
< |
r->nip += 4; |
1740 |
< |
r->gpr[8] = 0; |
1738 |
> |
} else if (r->pc() == ROM_BASE + 0x48e080 && (r->gpr(8) == 0xf3012002 || r->gpr(8) == 0xf3012000)) { |
1739 |
> |
r->pc() += 4; |
1740 |
> |
r->gpr(8) = 0; |
1741 |
|
return; |
1742 |
|
|
1743 |
|
// MacOS 8.1 serial drivers on startup |
1744 |
< |
} else if (r->nip == ROM_BASE + 0x48c5e0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1745 |
< |
r->nip += 4; |
1744 |
> |
} else if (r->pc() == ROM_BASE + 0x48c5e0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1745 |
> |
r->pc() += 4; |
1746 |
|
return; |
1747 |
< |
} else if (r->nip == ROM_BASE + 0x4a10a0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1748 |
< |
r->nip += 4; |
1747 |
> |
} else if (r->pc() == ROM_BASE + 0x4a10a0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1748 |
> |
r->pc() += 4; |
1749 |
> |
return; |
1750 |
> |
|
1751 |
> |
// MacOS 8.6 serial drivers on startup (with DR Cache and OldWorld ROM) |
1752 |
> |
} else if ((r->pc() - DR_CACHE_BASE) < DR_CACHE_SIZE && (r->gpr(16) == 0xf3012002 || r->gpr(16) == 0xf3012000)) { |
1753 |
> |
r->pc() += 4; |
1754 |
> |
return; |
1755 |
> |
} else if ((r->pc() - DR_CACHE_BASE) < DR_CACHE_SIZE && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1756 |
> |
r->pc() += 4; |
1757 |
|
return; |
1758 |
|
} |
1759 |
|
|
1760 |
|
// Get opcode and divide into fields |
1761 |
< |
uint32 opcode = *((uint32 *)r->nip); |
1761 |
> |
uint32 opcode = *((uint32 *)r->pc()); |
1762 |
|
uint32 primop = opcode >> 26; |
1763 |
|
uint32 exop = (opcode >> 1) & 0x3ff; |
1764 |
|
uint32 ra = (opcode >> 16) & 0x1f; |
1847 |
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_NORM; break; |
1848 |
|
case 45: // sthu |
1849 |
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_U; break; |
1850 |
+ |
#if EMULATE_UNALIGNED_LOADSTORE_MULTIPLE |
1851 |
+ |
case 46: // lmw |
1852 |
+ |
if ((addr % 4) != 0) { |
1853 |
+ |
uint32 ea = addr; |
1854 |
+ |
D(bug("WARNING: unaligned lmw to EA=%08x from IP=%08x\n", ea, r->pc())); |
1855 |
+ |
for (int i = rd; i <= 31; i++) { |
1856 |
+ |
r->gpr(i) = ReadMacInt32(ea); |
1857 |
+ |
ea += 4; |
1858 |
+ |
} |
1859 |
+ |
r->pc() += 4; |
1860 |
+ |
goto rti; |
1861 |
+ |
} |
1862 |
+ |
break; |
1863 |
+ |
case 47: // stmw |
1864 |
+ |
if ((addr % 4) != 0) { |
1865 |
+ |
uint32 ea = addr; |
1866 |
+ |
D(bug("WARNING: unaligned stmw to EA=%08x from IP=%08x\n", ea, r->pc())); |
1867 |
+ |
for (int i = rd; i <= 31; i++) { |
1868 |
+ |
WriteMacInt32(ea, r->gpr(i)); |
1869 |
+ |
ea += 4; |
1870 |
+ |
} |
1871 |
+ |
r->pc() += 4; |
1872 |
+ |
goto rti; |
1873 |
+ |
} |
1874 |
+ |
break; |
1875 |
+ |
#endif |
1876 |
|
} |
1877 |
|
|
1878 |
< |
// Ignore ROM writes |
1879 |
< |
if (transfer_type == TYPE_STORE && addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) { |
1880 |
< |
// 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)); |
1878 |
> |
// Ignore ROM writes (including to the zero page, which is read-only) |
1879 |
> |
if (transfer_type == TYPE_STORE && |
1880 |
> |
((addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) || |
1881 |
> |
(addr >= SheepMem::ZeroPage() && addr < SheepMem::ZeroPage() + SheepMem::PageSize()))) { |
1882 |
> |
// 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())); |
1883 |
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1884 |
< |
r->gpr[ra] = addr; |
1885 |
< |
r->nip += 4; |
1884 |
> |
r->gpr(ra) = addr; |
1885 |
> |
r->pc() += 4; |
1886 |
|
goto rti; |
1887 |
|
} |
1888 |
|
|
1889 |
|
// Ignore illegal memory accesses? |
1890 |
|
if (PrefsFindBool("ignoresegv")) { |
1891 |
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1892 |
< |
r->gpr[ra] = addr; |
1892 |
> |
r->gpr(ra) = addr; |
1893 |
|
if (transfer_type == TYPE_LOAD) |
1894 |
< |
r->gpr[rd] = 0; |
1895 |
< |
r->nip += 4; |
1894 |
> |
r->gpr(rd) = 0; |
1895 |
> |
r->pc() += 4; |
1896 |
|
goto rti; |
1897 |
|
} |
1898 |
|
|
1900 |
|
if (!PrefsFindBool("nogui")) { |
1901 |
|
char str[256]; |
1902 |
|
if (transfer_type == TYPE_LOAD || transfer_type == TYPE_STORE) |
1903 |
< |
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]); |
1903 |
> |
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)); |
1904 |
|
else |
1905 |
< |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode); |
1905 |
> |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode); |
1906 |
|
ErrorAlert(str); |
1907 |
|
QuitEmulator(); |
1908 |
|
return; |
1910 |
|
} |
1911 |
|
|
1912 |
|
// For all other errors, jump into debugger (sort of...) |
1913 |
+ |
crash_reason = (sig == SIGBUS) ? "SIGBUS" : "SIGSEGV"; |
1914 |
|
if (!ready_for_signals) { |
1915 |
< |
printf("SIGSEGV\n"); |
1916 |
< |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
1915 |
> |
printf("%s\n"); |
1916 |
> |
printf(" sigcontext %p, machine_regs %p\n", scp, r); |
1917 |
|
printf( |
1918 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1919 |
|
" xer %08lx cr %08lx \n" |
1925 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1926 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1927 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1928 |
< |
r->nip, r->link, r->ctr, r->msr, |
1929 |
< |
r->xer, r->ccr, |
1930 |
< |
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1931 |
< |
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
1932 |
< |
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
1933 |
< |
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
1934 |
< |
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
1935 |
< |
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
1936 |
< |
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
1937 |
< |
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
1928 |
> |
crash_reason, |
1929 |
> |
r->pc(), r->lr(), r->ctr(), r->msr(), |
1930 |
> |
r->xer(), r->cr(), |
1931 |
> |
r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3), |
1932 |
> |
r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7), |
1933 |
> |
r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11), |
1934 |
> |
r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15), |
1935 |
> |
r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19), |
1936 |
> |
r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23), |
1937 |
> |
r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27), |
1938 |
> |
r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31)); |
1939 |
|
exit(1); |
1940 |
|
QuitEmulator(); |
1941 |
|
return; |
1942 |
|
} else { |
1943 |
|
// We crashed. Save registers, tell tick thread and loop forever |
1944 |
< |
sigsegv_regs = *(sigregs *)r; |
1944 |
> |
build_sigregs(&sigsegv_regs, r); |
1945 |
|
emul_thread_fatal = true; |
1946 |
|
for (;;) ; |
1947 |
|
} |
1953 |
|
* SIGILL handler |
1954 |
|
*/ |
1955 |
|
|
1956 |
< |
static void sigill_handler(int sig, sigcontext_struct *sc) |
1956 |
> |
static void sigill_handler(int sig, siginfo_t *sip, void *scp) |
1957 |
|
{ |
1958 |
< |
pt_regs *r = sc->regs; |
1958 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1959 |
|
char str[256]; |
1960 |
|
|
1961 |
|
// Fault in Mac ROM or RAM? |
1962 |
< |
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
1962 |
> |
bool mac_fault = (r->pc() >= ROM_BASE) && (r->pc() < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize)); |
1963 |
|
if (mac_fault) { |
1964 |
|
|
1965 |
|
// Get opcode and divide into fields |
1966 |
< |
uint32 opcode = *((uint32 *)r->nip); |
1966 |
> |
uint32 opcode = *((uint32 *)r->pc()); |
1967 |
|
uint32 primop = opcode >> 26; |
1968 |
|
uint32 exop = (opcode >> 1) & 0x3ff; |
1969 |
|
uint32 ra = (opcode >> 16) & 0x1f; |
1974 |
|
switch (primop) { |
1975 |
|
case 9: // POWER instructions |
1976 |
|
case 22: |
1977 |
< |
power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->nip, r->gpr[1], opcode); |
1977 |
> |
power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->pc(), r->gpr(1), opcode); |
1978 |
|
ErrorAlert(str); |
1979 |
|
QuitEmulator(); |
1980 |
|
return; |
1982 |
|
case 31: |
1983 |
|
switch (exop) { |
1984 |
|
case 83: // mfmsr |
1985 |
< |
r->gpr[rd] = 0xf072; |
1986 |
< |
r->nip += 4; |
1985 |
> |
r->gpr(rd) = 0xf072; |
1986 |
> |
r->pc() += 4; |
1987 |
|
goto rti; |
1988 |
|
|
1989 |
|
case 210: // mtsr |
1990 |
|
case 242: // mtsrin |
1991 |
|
case 306: // tlbie |
1992 |
< |
r->nip += 4; |
1992 |
> |
r->pc() += 4; |
1993 |
|
goto rti; |
1994 |
|
|
1995 |
|
case 339: { // mfspr |
2005 |
|
case 957: // PMC3 |
2006 |
|
case 958: // PMC4 |
2007 |
|
case 959: // SDA |
2008 |
< |
r->nip += 4; |
2008 |
> |
r->pc() += 4; |
2009 |
|
goto rti; |
2010 |
|
case 25: // SDR1 |
2011 |
< |
r->gpr[rd] = 0xdead001f; |
2012 |
< |
r->nip += 4; |
2011 |
> |
r->gpr(rd) = 0xdead001f; |
2012 |
> |
r->pc() += 4; |
2013 |
|
goto rti; |
2014 |
|
case 287: // PVR |
2015 |
< |
r->gpr[rd] = PVR; |
2016 |
< |
r->nip += 4; |
2015 |
> |
r->gpr(rd) = PVR; |
2016 |
> |
r->pc() += 4; |
2017 |
|
goto rti; |
2018 |
|
} |
2019 |
|
break; |
2049 |
|
case 957: // PMC3 |
2050 |
|
case 958: // PMC4 |
2051 |
|
case 959: // SDA |
2052 |
< |
r->nip += 4; |
2052 |
> |
r->pc() += 4; |
2053 |
|
goto rti; |
2054 |
|
} |
2055 |
|
break; |
2068 |
|
|
2069 |
|
// In GUI mode, show error alert |
2070 |
|
if (!PrefsFindBool("nogui")) { |
2071 |
< |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode); |
2071 |
> |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode); |
2072 |
|
ErrorAlert(str); |
2073 |
|
QuitEmulator(); |
2074 |
|
return; |
2076 |
|
} |
2077 |
|
|
2078 |
|
// For all other errors, jump into debugger (sort of...) |
2079 |
+ |
crash_reason = "SIGILL"; |
2080 |
|
if (!ready_for_signals) { |
2081 |
< |
printf("SIGILL\n"); |
2082 |
< |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
2081 |
> |
printf("%s\n"); |
2082 |
> |
printf(" sigcontext %p, machine_regs %p\n", scp, r); |
2083 |
|
printf( |
2084 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
2085 |
|
" xer %08lx cr %08lx \n" |
2091 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
2092 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
2093 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
2094 |
< |
r->nip, r->link, r->ctr, r->msr, |
2095 |
< |
r->xer, r->ccr, |
2096 |
< |
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
2097 |
< |
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
2098 |
< |
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
2099 |
< |
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
2100 |
< |
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
2101 |
< |
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
2102 |
< |
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
2103 |
< |
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
2094 |
> |
crash_reason, |
2095 |
> |
r->pc(), r->lr(), r->ctr(), r->msr(), |
2096 |
> |
r->xer(), r->cr(), |
2097 |
> |
r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3), |
2098 |
> |
r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7), |
2099 |
> |
r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11), |
2100 |
> |
r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15), |
2101 |
> |
r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19), |
2102 |
> |
r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23), |
2103 |
> |
r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27), |
2104 |
> |
r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31)); |
2105 |
|
exit(1); |
2106 |
|
QuitEmulator(); |
2107 |
|
return; |
2108 |
|
} else { |
2109 |
|
// We crashed. Save registers, tell tick thread and loop forever |
2110 |
< |
sigsegv_regs = *(sigregs *)r; |
2110 |
> |
build_sigregs(&sigsegv_regs, r); |
2111 |
|
emul_thread_fatal = true; |
2112 |
|
for (;;) ; |
2113 |
|
} |
2117 |
|
|
2118 |
|
|
2119 |
|
/* |
2120 |
+ |
* Helpers to share 32-bit addressable data with MacOS |
2121 |
+ |
*/ |
2122 |
+ |
|
2123 |
+ |
bool SheepMem::Init(void) |
2124 |
+ |
{ |
2125 |
+ |
// Size of a native page |
2126 |
+ |
page_size = getpagesize(); |
2127 |
+ |
|
2128 |
+ |
// Allocate SheepShaver globals |
2129 |
+ |
if (vm_acquire_fixed((char *)base, size) < 0) |
2130 |
+ |
return false; |
2131 |
+ |
|
2132 |
+ |
// Allocate page with all bits set to 0 |
2133 |
+ |
zero_page = base + size; |
2134 |
+ |
if (vm_acquire_fixed((char *)zero_page, page_size) < 0) |
2135 |
+ |
return false; |
2136 |
+ |
memset((char *)zero_page, 0, page_size); |
2137 |
+ |
if (vm_protect((char *)zero_page, page_size, VM_PAGE_READ) < 0) |
2138 |
+ |
return false; |
2139 |
+ |
|
2140 |
+ |
#if EMULATED_PPC |
2141 |
+ |
// Allocate alternate stack for PowerPC interrupt routine |
2142 |
+ |
sig_stack = zero_page + page_size; |
2143 |
+ |
if (vm_acquire_fixed((char *)sig_stack, SIG_STACK_SIZE) < 0) |
2144 |
+ |
return false; |
2145 |
+ |
#endif |
2146 |
+ |
|
2147 |
+ |
top = base + size; |
2148 |
+ |
return true; |
2149 |
+ |
} |
2150 |
+ |
|
2151 |
+ |
void SheepMem::Exit(void) |
2152 |
+ |
{ |
2153 |
+ |
if (top) { |
2154 |
+ |
// Delete SheepShaver globals |
2155 |
+ |
vm_release((void *)base, size); |
2156 |
+ |
|
2157 |
+ |
// Delete zero page |
2158 |
+ |
vm_release((void *)zero_page, page_size); |
2159 |
+ |
|
2160 |
+ |
#if EMULATED_PPC |
2161 |
+ |
// Delete alternate stack for PowerPC interrupt routine |
2162 |
+ |
vm_release((void *)sig_stack, SIG_STACK_SIZE); |
2163 |
+ |
#endif |
2164 |
+ |
} |
2165 |
+ |
} |
2166 |
+ |
|
2167 |
+ |
|
2168 |
+ |
/* |
2169 |
|
* Display alert |
2170 |
|
*/ |
2171 |
|
|
2214 |
|
|
2215 |
|
void ErrorAlert(const char *text) |
2216 |
|
{ |
2217 |
< |
#ifdef ENABLE_GTK |
2217 |
> |
#if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO) |
2218 |
|
if (PrefsFindBool("nogui") || x_display == NULL) { |
2219 |
|
printf(GetString(STR_SHELL_ERROR_PREFIX), text); |
2220 |
|
return; |
2233 |
|
|
2234 |
|
void WarningAlert(const char *text) |
2235 |
|
{ |
2236 |
< |
#ifdef ENABLE_GTK |
2236 |
> |
#if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO) |
2237 |
|
if (PrefsFindBool("nogui") || x_display == NULL) { |
2238 |
|
printf(GetString(STR_SHELL_WARNING_PREFIX), text); |
2239 |
|
return; |