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 HAVE_DIRENT_H |
131 |
+ |
#include <dirent.h> |
132 |
+ |
#endif |
133 |
+ |
|
134 |
+ |
#ifdef USE_SDL |
135 |
+ |
#include <SDL.h> |
136 |
+ |
#endif |
137 |
+ |
|
138 |
+ |
#ifndef USE_SDL_VIDEO |
139 |
|
#include <X11/Xlib.h> |
140 |
+ |
#endif |
141 |
|
|
142 |
|
#ifdef ENABLE_GTK |
143 |
|
#include <gtk/gtk.h> |
154 |
|
#endif |
155 |
|
|
156 |
|
|
157 |
+ |
// Enable emulation of unaligned lmw/stmw? |
158 |
+ |
#define EMULATE_UNALIGNED_LOADSTORE_MULTIPLE 1 |
159 |
+ |
|
160 |
|
// Enable Execute68k() safety checks? |
161 |
|
#define SAFE_EXEC_68K 0 |
162 |
|
|
166 |
|
// Interrupts in native mode? |
167 |
|
#define INTERRUPTS_IN_NATIVE_MODE 1 |
168 |
|
|
169 |
+ |
// Number of alternate stacks for signal handlers? |
170 |
+ |
#define SIG_STACK_COUNT 4 |
171 |
+ |
|
172 |
|
|
173 |
|
// Constants |
174 |
|
const char ROM_FILE_NAME[] = "ROM"; |
175 |
|
const char ROM_FILE_NAME2[] = "Mac OS ROM"; |
176 |
|
|
177 |
< |
const uint32 RAM_BASE = 0x20000000; // Base address of RAM |
177 |
> |
const uintptr RAM_BASE = 0x20000000; // Base address of RAM |
178 |
|
const uint32 SIG_STACK_SIZE = 0x10000; // Size of signal stack |
179 |
|
|
180 |
|
|
181 |
|
#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 |
– |
|
182 |
|
struct sigregs { |
183 |
< |
elf_gregset_t gp_regs; // Identical to pt_regs |
184 |
< |
double fp_regs[ELF_NFPREG]; // f0..f31 and fpsrc |
185 |
< |
//more (uninteresting) stuff following here |
183 |
> |
uint32 nip; |
184 |
> |
uint32 link; |
185 |
> |
uint32 ctr; |
186 |
> |
uint32 msr; |
187 |
> |
uint32 xer; |
188 |
> |
uint32 ccr; |
189 |
> |
uint32 gpr[32]; |
190 |
> |
}; |
191 |
> |
|
192 |
> |
#if defined(__linux__) |
193 |
> |
#include <sys/ucontext.h> |
194 |
> |
#define MACHINE_REGISTERS(scp) ((machine_regs *)(((ucontext_t *)scp)->uc_mcontext.regs)) |
195 |
> |
|
196 |
> |
struct machine_regs : public pt_regs |
197 |
> |
{ |
198 |
> |
u_long & cr() { return pt_regs::ccr; } |
199 |
> |
uint32 cr() const { return pt_regs::ccr; } |
200 |
> |
uint32 lr() const { return pt_regs::link; } |
201 |
> |
uint32 ctr() const { return pt_regs::ctr; } |
202 |
> |
uint32 xer() const { return pt_regs::xer; } |
203 |
> |
uint32 msr() const { return pt_regs::msr; } |
204 |
> |
uint32 dar() const { return pt_regs::dar; } |
205 |
> |
u_long & pc() { return pt_regs::nip; } |
206 |
> |
uint32 pc() const { return pt_regs::nip; } |
207 |
> |
u_long & gpr(int i) { return pt_regs::gpr[i]; } |
208 |
> |
uint32 gpr(int i) const { return pt_regs::gpr[i]; } |
209 |
> |
}; |
210 |
> |
#endif |
211 |
> |
|
212 |
> |
#if defined(__APPLE__) && defined(__MACH__) |
213 |
> |
#include <sys/signal.h> |
214 |
> |
extern "C" int sigaltstack(const struct sigaltstack *ss, struct sigaltstack *oss); |
215 |
> |
|
216 |
> |
#include <sys/ucontext.h> |
217 |
> |
#define MACHINE_REGISTERS(scp) ((machine_regs *)(((ucontext_t *)scp)->uc_mcontext)) |
218 |
> |
|
219 |
> |
struct machine_regs : public mcontext |
220 |
> |
{ |
221 |
> |
uint32 & cr() { return ss.cr; } |
222 |
> |
uint32 cr() const { return ss.cr; } |
223 |
> |
uint32 lr() const { return ss.lr; } |
224 |
> |
uint32 ctr() const { return ss.ctr; } |
225 |
> |
uint32 xer() const { return ss.xer; } |
226 |
> |
uint32 msr() const { return ss.srr1; } |
227 |
> |
uint32 dar() const { return es.dar; } |
228 |
> |
uint32 & pc() { return ss.srr0; } |
229 |
> |
uint32 pc() const { return ss.srr0; } |
230 |
> |
uint32 & gpr(int i) { return (&ss.r0)[i]; } |
231 |
> |
uint32 gpr(int i) const { return (&ss.r0)[i]; } |
232 |
|
}; |
233 |
|
#endif |
234 |
|
|
235 |
+ |
static void build_sigregs(sigregs *srp, machine_regs *mrp) |
236 |
+ |
{ |
237 |
+ |
srp->nip = mrp->pc(); |
238 |
+ |
srp->link = mrp->lr(); |
239 |
+ |
srp->ctr = mrp->ctr(); |
240 |
+ |
srp->msr = mrp->msr(); |
241 |
+ |
srp->xer = mrp->xer(); |
242 |
+ |
srp->ccr = mrp->cr(); |
243 |
+ |
for (int i = 0; i < 32; i++) |
244 |
+ |
srp->gpr[i] = mrp->gpr(i); |
245 |
+ |
} |
246 |
+ |
|
247 |
+ |
static struct sigaltstack sig_stacks[SIG_STACK_COUNT]; // Stacks for signal handlers |
248 |
+ |
static int sig_stack_id = 0; // Stack slot currently used |
249 |
+ |
|
250 |
+ |
static inline void sig_stack_acquire(void) |
251 |
+ |
{ |
252 |
+ |
if (++sig_stack_id == SIG_STACK_COUNT) { |
253 |
+ |
printf("FATAL: signal stack overflow\n"); |
254 |
+ |
return; |
255 |
+ |
} |
256 |
+ |
sigaltstack(&sig_stacks[sig_stack_id], NULL); |
257 |
+ |
} |
258 |
+ |
|
259 |
+ |
static inline void sig_stack_release(void) |
260 |
+ |
{ |
261 |
+ |
if (--sig_stack_id < 0) { |
262 |
+ |
printf("FATAL: signal stack underflow\n"); |
263 |
+ |
return; |
264 |
+ |
} |
265 |
+ |
sigaltstack(&sig_stacks[sig_stack_id], NULL); |
266 |
+ |
} |
267 |
+ |
#endif |
268 |
+ |
|
269 |
|
|
270 |
|
// Global variables (exported) |
271 |
|
#if !EMULATED_PPC |
273 |
|
#endif |
274 |
|
uint32 RAMBase; // Base address of Mac RAM |
275 |
|
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 |
276 |
|
uint32 KernelDataAddr; // Address of Kernel Data |
277 |
|
uint32 BootGlobsAddr; // Address of BootGlobs structure at top of Mac RAM |
278 |
+ |
uint32 DRCacheAddr; // Address of DR Cache |
279 |
|
uint32 PVR; // Theoretical PVR |
280 |
|
int64 CPUClockSpeed; // Processor clock speed (Hz) |
281 |
|
int64 BusClockSpeed; // Bus clock speed (Hz) |
282 |
+ |
int64 TimebaseSpeed; // Timebase clock speed (Hz) |
283 |
|
|
284 |
|
|
285 |
|
// Global variables |
286 |
+ |
#ifndef USE_SDL_VIDEO |
287 |
|
char *x_display_name = NULL; // X11 display name |
288 |
|
Display *x_display = NULL; // X11 display handle |
289 |
+ |
#ifdef X11_LOCK_TYPE |
290 |
+ |
X11_LOCK_TYPE x_display_lock = X11_LOCK_INIT; // X11 display lock |
291 |
+ |
#endif |
292 |
+ |
#endif |
293 |
|
|
294 |
|
static int zero_fd = 0; // FD of /dev/zero |
190 |
– |
static bool sheep_area_mapped = false; // Flag: SheepShaver data area mmap()ed |
295 |
|
static bool lm_area_mapped = false; // Flag: Low Memory area mmap()ped |
296 |
|
static int kernel_area = -1; // SHM ID of Kernel Data area |
297 |
|
static bool rom_area_mapped = false; // Flag: Mac ROM mmap()ped |
298 |
|
static bool ram_area_mapped = false; // Flag: Mac RAM mmap()ped |
299 |
+ |
static bool dr_cache_area_mapped = false; // Flag: Mac DR Cache mmap()ped |
300 |
+ |
static bool dr_emulator_area_mapped = false;// Flag: Mac DR Emulator mmap()ped |
301 |
|
static KernelData *kernel_data; // Pointer to Kernel Data |
302 |
|
static EmulatorData *emulator_data; |
303 |
|
|
304 |
|
static uint8 last_xpram[XPRAM_SIZE]; // Buffer for monitoring XPRAM changes |
305 |
|
|
306 |
|
static bool nvram_thread_active = false; // Flag: NVRAM watchdog installed |
307 |
+ |
static volatile bool nvram_thread_cancel; // Flag: Cancel NVRAM thread |
308 |
|
static pthread_t nvram_thread; // NVRAM watchdog |
309 |
|
static bool tick_thread_active = false; // Flag: MacOS thread installed |
310 |
+ |
static volatile bool tick_thread_cancel; // Flag: Cancel 60Hz thread |
311 |
|
static pthread_t tick_thread; // 60Hz thread |
312 |
|
static pthread_t emul_thread; // MacOS thread |
313 |
|
|
315 |
|
static int64 num_segv = 0; // Number of handled SEGV signals |
316 |
|
|
317 |
|
static struct sigaction sigusr2_action; // Interrupt signal (of emulator thread) |
318 |
< |
#if !EMULATED_PPC |
318 |
> |
#if EMULATED_PPC |
319 |
> |
static uintptr sig_stack = 0; // Stack for PowerPC interrupt routine |
320 |
> |
#else |
321 |
|
static struct sigaction sigsegv_action; // Data access exception signal (of emulator thread) |
322 |
|
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 |
323 |
|
static bool emul_thread_fatal = false; // Flag: MacOS thread crashed, tick thread shall dump debug output |
324 |
|
static sigregs sigsegv_regs; // Register dump when crashed |
325 |
+ |
static const char *crash_reason = NULL; // Reason of the crash (SIGSEGV, SIGBUS, SIGILL) |
326 |
|
#endif |
327 |
|
|
328 |
+ |
uint32 SheepMem::page_size; // Size of a native page |
329 |
+ |
uintptr SheepMem::zero_page = 0; // Address of ro page filled in with zeros |
330 |
+ |
uintptr SheepMem::base = 0x60000000; // Address of SheepShaver data |
331 |
+ |
uintptr SheepMem::top = 0; // Top of SheepShaver data (stack like storage) |
332 |
+ |
|
333 |
|
|
334 |
|
// Prototypes |
335 |
|
static void Quit(void); |
337 |
|
static void *nvram_func(void *arg); |
338 |
|
static void *tick_func(void *arg); |
339 |
|
#if EMULATED_PPC |
226 |
– |
static void sigusr2_handler(int sig); |
340 |
|
extern void emul_ppc(uint32 start); |
341 |
|
extern void init_emul_ppc(void); |
342 |
|
extern void exit_emul_ppc(void); |
343 |
+ |
sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t); |
344 |
|
#else |
345 |
< |
static void sigusr2_handler(int sig, sigcontext_struct *sc); |
346 |
< |
static void sigsegv_handler(int sig, sigcontext_struct *sc); |
347 |
< |
static void sigill_handler(int sig, sigcontext_struct *sc); |
345 |
> |
static void sigusr2_handler(int sig, siginfo_t *sip, void *scp); |
346 |
> |
static void sigsegv_handler(int sig, siginfo_t *sip, void *scp); |
347 |
> |
static void sigill_handler(int sig, siginfo_t *sip, void *scp); |
348 |
|
#endif |
349 |
|
|
350 |
|
|
366 |
|
|
367 |
|
#if EMULATED_PPC |
368 |
|
/* |
369 |
+ |
* Return signal stack base |
370 |
+ |
*/ |
371 |
+ |
|
372 |
+ |
uintptr SignalStackBase(void) |
373 |
+ |
{ |
374 |
+ |
return sig_stack + SIG_STACK_SIZE; |
375 |
+ |
} |
376 |
+ |
|
377 |
+ |
|
378 |
+ |
/* |
379 |
|
* Atomic operations |
380 |
|
*/ |
381 |
|
|
466 |
|
for (int i=1; i<argc; i++) { |
467 |
|
if (strcmp(argv[i], "--help") == 0) { |
468 |
|
usage(argv[0]); |
469 |
+ |
#ifndef USE_SDL_VIDEO |
470 |
|
} else if (strcmp(argv[i], "--display") == 0) { |
471 |
|
i++; |
472 |
|
if (i < argc) |
473 |
|
x_display_name = strdup(argv[i]); |
474 |
+ |
#endif |
475 |
|
} else if (argv[i][0] == '-') { |
476 |
|
fprintf(stderr, "Unrecognized option '%s'\n", argv[i]); |
477 |
|
usage(argv[0]); |
478 |
|
} |
479 |
|
} |
480 |
|
|
481 |
+ |
#ifdef USE_SDL |
482 |
+ |
// Initialize SDL system |
483 |
+ |
int sdl_flags = 0; |
484 |
+ |
#ifdef USE_SDL_VIDEO |
485 |
+ |
sdl_flags |= SDL_INIT_VIDEO; |
486 |
+ |
#endif |
487 |
+ |
assert(sdl_flags != 0); |
488 |
+ |
if (SDL_Init(sdl_flags) == -1) { |
489 |
+ |
char str[256]; |
490 |
+ |
sprintf(str, "Could not initialize SDL: %s.\n", SDL_GetError()); |
491 |
+ |
ErrorAlert(str); |
492 |
+ |
goto quit; |
493 |
+ |
} |
494 |
+ |
atexit(SDL_Quit); |
495 |
+ |
#endif |
496 |
+ |
|
497 |
+ |
#ifndef USE_SDL_VIDEO |
498 |
|
// Open display |
499 |
|
x_display = XOpenDisplay(x_display_name); |
500 |
|
if (x_display == NULL) { |
508 |
|
// Fork out, so we can return from fullscreen mode when things get ugly |
509 |
|
XF86DGAForkApp(DefaultScreen(x_display)); |
510 |
|
#endif |
511 |
+ |
#endif |
512 |
|
|
513 |
|
#ifdef ENABLE_MON |
514 |
|
// Initialize mon |
515 |
|
mon_init(); |
516 |
|
#endif |
517 |
|
|
518 |
+ |
#if !EMULATED_PPC |
519 |
+ |
// Create and install stacks for signal handlers |
520 |
+ |
for (int i = 0; i < SIG_STACK_COUNT; i++) { |
521 |
+ |
void *sig_stack = malloc(SIG_STACK_SIZE); |
522 |
+ |
D(bug("Signal stack %d at %p\n", i, sig_stack)); |
523 |
+ |
if (sig_stack == NULL) { |
524 |
+ |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
525 |
+ |
goto quit; |
526 |
+ |
} |
527 |
+ |
sig_stacks[i].ss_sp = sig_stack; |
528 |
+ |
sig_stacks[i].ss_flags = 0; |
529 |
+ |
sig_stacks[i].ss_size = SIG_STACK_SIZE; |
530 |
+ |
} |
531 |
+ |
sig_stack_id = 0; |
532 |
+ |
if (sigaltstack(&sig_stacks[0], NULL) < 0) { |
533 |
+ |
sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno)); |
534 |
+ |
ErrorAlert(str); |
535 |
+ |
goto quit; |
536 |
+ |
} |
537 |
+ |
#endif |
538 |
+ |
|
539 |
+ |
#if !EMULATED_PPC |
540 |
+ |
// Install SIGSEGV and SIGBUS handlers |
541 |
+ |
sigemptyset(&sigsegv_action.sa_mask); // Block interrupts during SEGV handling |
542 |
+ |
sigaddset(&sigsegv_action.sa_mask, SIGUSR2); |
543 |
+ |
sigsegv_action.sa_sigaction = sigsegv_handler; |
544 |
+ |
sigsegv_action.sa_flags = SA_ONSTACK | SA_SIGINFO; |
545 |
+ |
#ifdef HAVE_SIGNAL_SA_RESTORER |
546 |
+ |
sigsegv_action.sa_restorer = NULL; |
547 |
+ |
#endif |
548 |
+ |
if (sigaction(SIGSEGV, &sigsegv_action, NULL) < 0) { |
549 |
+ |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
550 |
+ |
ErrorAlert(str); |
551 |
+ |
goto quit; |
552 |
+ |
} |
553 |
+ |
if (sigaction(SIGBUS, &sigsegv_action, NULL) < 0) { |
554 |
+ |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
555 |
+ |
ErrorAlert(str); |
556 |
+ |
goto quit; |
557 |
+ |
} |
558 |
+ |
#else |
559 |
+ |
// Install SIGSEGV handler for CPU emulator |
560 |
+ |
if (!sigsegv_install_handler(sigsegv_handler)) { |
561 |
+ |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
562 |
+ |
ErrorAlert(str); |
563 |
+ |
goto quit; |
564 |
+ |
} |
565 |
+ |
#endif |
566 |
+ |
|
567 |
+ |
// Initialize VM system |
568 |
+ |
vm_init(); |
569 |
+ |
|
570 |
|
// Get system info |
571 |
|
PVR = 0x00040000; // Default: 604 |
572 |
|
CPUClockSpeed = 100000000; // Default: 100MHz |
573 |
|
BusClockSpeed = 100000000; // Default: 100MHz |
574 |
< |
#if !EMULATED_PPC |
574 |
> |
TimebaseSpeed = 25000000; // Default: 25MHz |
575 |
> |
#if EMULATED_PPC |
576 |
> |
PVR = 0x000c0000; // Default: 7400 (with AltiVec) |
577 |
> |
#elif defined(__APPLE__) && defined(__MACH__) |
578 |
> |
proc_file = popen("ioreg -c IOPlatformDevice", "r"); |
579 |
> |
if (proc_file) { |
580 |
> |
char line[256]; |
581 |
> |
bool powerpc_node = false; |
582 |
> |
while (fgets(line, sizeof(line) - 1, proc_file)) { |
583 |
> |
// Read line |
584 |
> |
int len = strlen(line); |
585 |
> |
if (len == 0) |
586 |
> |
continue; |
587 |
> |
line[len - 1] = 0; |
588 |
> |
|
589 |
> |
// Parse line |
590 |
> |
if (strstr(line, "o PowerPC,")) |
591 |
> |
powerpc_node = true; |
592 |
> |
else if (powerpc_node) { |
593 |
> |
uint32 value; |
594 |
> |
char head[256]; |
595 |
> |
if (sscanf(line, "%[ |]\"cpu-version\" = <%x>", head, &value) == 2) |
596 |
> |
PVR = value; |
597 |
> |
else if (sscanf(line, "%[ |]\"clock-frequency\" = <%x>", head, &value) == 2) |
598 |
> |
CPUClockSpeed = value; |
599 |
> |
else if (sscanf(line, "%[ |]\"bus-frequency\" = <%x>", head, &value) == 2) |
600 |
> |
BusClockSpeed = value; |
601 |
> |
else if (strchr(line, '}')) |
602 |
> |
powerpc_node = false; |
603 |
> |
} |
604 |
> |
} |
605 |
> |
fclose(proc_file); |
606 |
> |
} else { |
607 |
> |
sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno)); |
608 |
> |
WarningAlert(str); |
609 |
> |
} |
610 |
> |
#else |
611 |
|
proc_file = fopen("/proc/cpuinfo", "r"); |
612 |
|
if (proc_file) { |
613 |
|
char line[256]; |
621 |
|
// Parse line |
622 |
|
int i; |
623 |
|
char value[256]; |
624 |
< |
if (sscanf(line, "cpu : %s", value) == 1) { |
624 |
> |
if (sscanf(line, "cpu : %[0-9A-Za-a]", value) == 1) { |
625 |
|
if (strcmp(value, "601") == 0) |
626 |
|
PVR = 0x00010000; |
627 |
|
else if (strcmp(value, "603") == 0) |
638 |
|
PVR = 0x000a0000; |
639 |
|
else if (strcmp(value, "750") == 0) |
640 |
|
PVR = 0x00080000; |
641 |
+ |
else if (strcmp(value, "750FX") == 0) |
642 |
+ |
PVR = 0x70000000; |
643 |
|
else if (strcmp(value, "821") == 0) |
644 |
|
PVR = 0x00320000; |
645 |
|
else if (strcmp(value, "860") == 0) |
646 |
|
PVR = 0x00500000; |
647 |
+ |
else if (strcmp(value, "7400") == 0) |
648 |
+ |
PVR = 0x000c0000; |
649 |
+ |
else if (strcmp(value, "7410") == 0) |
650 |
+ |
PVR = 0x800c0000; |
651 |
+ |
else if (strcmp(value, "7450") == 0) |
652 |
+ |
PVR = 0x80000000; |
653 |
+ |
else if (strcmp(value, "7455") == 0) |
654 |
+ |
PVR = 0x80010000; |
655 |
+ |
else if (strcmp(value, "7457") == 0) |
656 |
+ |
PVR = 0x80020000; |
657 |
+ |
else if (strcmp(value, "PPC970") == 0) |
658 |
+ |
PVR = 0x00390000; |
659 |
|
else |
660 |
|
printf("WARNING: Unknown CPU type '%s', assuming 604\n", value); |
661 |
|
} |
667 |
|
sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno)); |
668 |
|
WarningAlert(str); |
669 |
|
} |
670 |
+ |
|
671 |
+ |
// Get actual bus frequency |
672 |
+ |
proc_file = fopen("/proc/device-tree/clock-frequency", "r"); |
673 |
+ |
if (proc_file) { |
674 |
+ |
union { uint8 b[4]; uint32 l; } value; |
675 |
+ |
if (fread(value.b, sizeof(value), 1, proc_file) == 1) |
676 |
+ |
BusClockSpeed = value.l; |
677 |
+ |
fclose(proc_file); |
678 |
+ |
} |
679 |
+ |
|
680 |
+ |
// Get actual timebase frequency |
681 |
+ |
TimebaseSpeed = BusClockSpeed / 4; |
682 |
+ |
DIR *cpus_dir; |
683 |
+ |
if ((cpus_dir = opendir("/proc/device-tree/cpus")) != NULL) { |
684 |
+ |
struct dirent *cpu_entry; |
685 |
+ |
while ((cpu_entry = readdir(cpus_dir)) != NULL) { |
686 |
+ |
if (strstr(cpu_entry->d_name, "PowerPC,") == cpu_entry->d_name) { |
687 |
+ |
char timebase_freq_node[256]; |
688 |
+ |
sprintf(timebase_freq_node, "/proc/device-tree/cpus/%s/timebase-frequency", cpu_entry->d_name); |
689 |
+ |
proc_file = fopen(timebase_freq_node, "r"); |
690 |
+ |
if (proc_file) { |
691 |
+ |
union { uint8 b[4]; uint32 l; } value; |
692 |
+ |
if (fread(value.b, sizeof(value), 1, proc_file) == 1) |
693 |
+ |
TimebaseSpeed = value.l; |
694 |
+ |
fclose(proc_file); |
695 |
+ |
} |
696 |
+ |
} |
697 |
+ |
} |
698 |
+ |
closedir(cpus_dir); |
699 |
+ |
} |
700 |
|
#endif |
701 |
|
D(bug("PVR: %08x (assumed)\n", PVR)); |
702 |
|
|
721 |
|
goto quit; |
722 |
|
} |
723 |
|
|
724 |
+ |
#ifndef PAGEZERO_HACK |
725 |
|
// Create Low Memory area (0x0000..0x3000) |
726 |
|
if (vm_acquire_fixed((char *)0, 0x3000) < 0) { |
727 |
|
sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno)); |
729 |
|
goto quit; |
730 |
|
} |
731 |
|
lm_area_mapped = true; |
732 |
+ |
#endif |
733 |
|
|
734 |
|
// Create areas for Kernel Data |
735 |
|
kernel_area = shmget(IPC_PRIVATE, KERNEL_AREA_SIZE, 0600); |
748 |
|
ErrorAlert(str); |
749 |
|
goto quit; |
750 |
|
} |
751 |
< |
kernel_data = (KernelData *)0x68ffe000; |
751 |
> |
kernel_data = (KernelData *)KERNEL_DATA_BASE; |
752 |
|
emulator_data = &kernel_data->ed; |
753 |
< |
KernelDataAddr = (uint32)kernel_data; |
753 |
> |
KernelDataAddr = KERNEL_DATA_BASE; |
754 |
|
D(bug("Kernel Data at %p, Emulator Data at %p\n", kernel_data, emulator_data)); |
755 |
|
|
756 |
+ |
// Create area for DR Cache |
757 |
+ |
if (vm_acquire_fixed((void *)DR_EMULATOR_BASE, DR_EMULATOR_SIZE) < 0) { |
758 |
+ |
sprintf(str, GetString(STR_DR_EMULATOR_MMAP_ERR), strerror(errno)); |
759 |
+ |
ErrorAlert(str); |
760 |
+ |
goto quit; |
761 |
+ |
} |
762 |
+ |
dr_emulator_area_mapped = true; |
763 |
+ |
if (vm_acquire_fixed((void *)DR_CACHE_BASE, DR_CACHE_SIZE) < 0) { |
764 |
+ |
sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno)); |
765 |
+ |
ErrorAlert(str); |
766 |
+ |
goto quit; |
767 |
+ |
} |
768 |
+ |
dr_cache_area_mapped = true; |
769 |
+ |
#if !EMULATED_PPC |
770 |
+ |
if (vm_protect((char *)DR_CACHE_BASE, DR_CACHE_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
771 |
+ |
sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno)); |
772 |
+ |
ErrorAlert(str); |
773 |
+ |
goto quit; |
774 |
+ |
} |
775 |
+ |
#endif |
776 |
+ |
DRCacheAddr = DR_CACHE_BASE; |
777 |
+ |
D(bug("DR Cache at %p\n", DRCacheAddr)); |
778 |
+ |
|
779 |
|
// Create area for SheepShaver data |
780 |
< |
if (vm_acquire_fixed((char *)SHEEP_BASE, SHEEP_SIZE) < 0) { |
780 |
> |
if (!SheepMem::Init()) { |
781 |
|
sprintf(str, GetString(STR_SHEEP_MEM_MMAP_ERR), strerror(errno)); |
782 |
|
ErrorAlert(str); |
783 |
|
goto quit; |
784 |
|
} |
484 |
– |
SheepStack1Base = SHEEP_BASE + 0x10000; |
485 |
– |
SheepStack2Base = SheepStack1Base + 0x10000; |
486 |
– |
SheepThunksBase = SheepStack2Base + 0x1000; |
487 |
– |
sheep_area_mapped = true; |
785 |
|
|
786 |
|
// Create area for Mac ROM |
787 |
|
if (vm_acquire_fixed((char *)ROM_BASE, ROM_AREA_SIZE) < 0) { |
789 |
|
ErrorAlert(str); |
790 |
|
goto quit; |
791 |
|
} |
792 |
< |
#if !EMULATED_PPC || defined(__powerpc__) |
792 |
> |
#if !EMULATED_PPC |
793 |
|
if (vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
794 |
|
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
795 |
|
ErrorAlert(str); |
859 |
|
// Load NVRAM |
860 |
|
XPRAMInit(); |
861 |
|
|
862 |
+ |
// Load XPRAM default values if signature not found |
863 |
+ |
if (XPRAM[0x130c] != 0x4e || XPRAM[0x130d] != 0x75 |
864 |
+ |
|| XPRAM[0x130e] != 0x4d || XPRAM[0x130f] != 0x63) { |
865 |
+ |
D(bug("Loading XPRAM default values\n")); |
866 |
+ |
memset(XPRAM + 0x1300, 0, 0x100); |
867 |
+ |
XPRAM[0x130c] = 0x4e; // "NuMc" signature |
868 |
+ |
XPRAM[0x130d] = 0x75; |
869 |
+ |
XPRAM[0x130e] = 0x4d; |
870 |
+ |
XPRAM[0x130f] = 0x63; |
871 |
+ |
XPRAM[0x1301] = 0x80; // InternalWaitFlags = DynWait (don't wait for SCSI devices upon bootup) |
872 |
+ |
XPRAM[0x1310] = 0xa8; // Standard PRAM values |
873 |
+ |
XPRAM[0x1311] = 0x00; |
874 |
+ |
XPRAM[0x1312] = 0x00; |
875 |
+ |
XPRAM[0x1313] = 0x22; |
876 |
+ |
XPRAM[0x1314] = 0xcc; |
877 |
+ |
XPRAM[0x1315] = 0x0a; |
878 |
+ |
XPRAM[0x1316] = 0xcc; |
879 |
+ |
XPRAM[0x1317] = 0x0a; |
880 |
+ |
XPRAM[0x131c] = 0x00; |
881 |
+ |
XPRAM[0x131d] = 0x02; |
882 |
+ |
XPRAM[0x131e] = 0x63; |
883 |
+ |
XPRAM[0x131f] = 0x00; |
884 |
+ |
XPRAM[0x1308] = 0x13; |
885 |
+ |
XPRAM[0x1309] = 0x88; |
886 |
+ |
XPRAM[0x130a] = 0x00; |
887 |
+ |
XPRAM[0x130b] = 0xcc; |
888 |
+ |
XPRAM[0x1376] = 0x00; // OSDefault = MacOS |
889 |
+ |
XPRAM[0x1377] = 0x01; |
890 |
+ |
} |
891 |
+ |
|
892 |
|
// Set boot volume |
893 |
|
i16 = PrefsFindInt32("bootdrive"); |
894 |
|
XPRAM[0x1378] = i16 >> 8; |
906 |
|
boot_globs[1] = htonl(RAMSize); |
907 |
|
boot_globs[2] = htonl((uint32)-1); // End of bank table |
908 |
|
|
909 |
+ |
// Init thunks |
910 |
+ |
if (!ThunksInit()) |
911 |
+ |
goto quit; |
912 |
+ |
|
913 |
|
// Init drivers |
914 |
|
SonyInit(); |
915 |
|
DiskInit(); |
919 |
|
// Init external file system |
920 |
|
ExtFSInit(); |
921 |
|
|
922 |
+ |
// Init ADB |
923 |
+ |
ADBInit(); |
924 |
+ |
|
925 |
|
// Init audio |
926 |
|
AudioInit(); |
927 |
|
|
956 |
|
// Initialize Kernel Data |
957 |
|
memset(kernel_data, 0, sizeof(KernelData)); |
958 |
|
if (ROMType == ROMTYPE_NEWWORLD) { |
959 |
< |
static uint32 of_dev_tree[4] = {0, 0, 0, 0}; |
960 |
< |
static uint8 vector_lookup_tbl[128]; |
961 |
< |
static uint8 vector_mask_tbl[64]; |
959 |
> |
uintptr of_dev_tree = SheepMem::Reserve(4 * sizeof(uint32)); |
960 |
> |
memset((void *)of_dev_tree, 0, 4 * sizeof(uint32)); |
961 |
> |
uintptr vector_lookup_tbl = SheepMem::Reserve(128); |
962 |
> |
uintptr vector_mask_tbl = SheepMem::Reserve(64); |
963 |
|
memset((uint8 *)kernel_data + 0xb80, 0x3d, 0x80); |
964 |
< |
memset(vector_lookup_tbl, 0, 128); |
965 |
< |
memset(vector_mask_tbl, 0, 64); |
964 |
> |
memset((void *)vector_lookup_tbl, 0, 128); |
965 |
> |
memset((void *)vector_mask_tbl, 0, 64); |
966 |
|
kernel_data->v[0xb80 >> 2] = htonl(ROM_BASE); |
967 |
< |
kernel_data->v[0xb84 >> 2] = htonl((uint32)of_dev_tree); // OF device tree base |
968 |
< |
kernel_data->v[0xb90 >> 2] = htonl((uint32)vector_lookup_tbl); |
969 |
< |
kernel_data->v[0xb94 >> 2] = htonl((uint32)vector_mask_tbl); |
967 |
> |
kernel_data->v[0xb84 >> 2] = htonl(of_dev_tree); // OF device tree base |
968 |
> |
kernel_data->v[0xb90 >> 2] = htonl(vector_lookup_tbl); |
969 |
> |
kernel_data->v[0xb94 >> 2] = htonl(vector_mask_tbl); |
970 |
|
kernel_data->v[0xb98 >> 2] = htonl(ROM_BASE); // OpenPIC base |
971 |
|
kernel_data->v[0xbb0 >> 2] = htonl(0); // ADB base |
972 |
|
kernel_data->v[0xc20 >> 2] = htonl(RAMSize); |
979 |
|
kernel_data->v[0xc50 >> 2] = htonl(RAMBase); |
980 |
|
kernel_data->v[0xc54 >> 2] = htonl(RAMSize); |
981 |
|
kernel_data->v[0xf60 >> 2] = htonl(PVR); |
982 |
< |
kernel_data->v[0xf64 >> 2] = htonl(CPUClockSpeed); |
983 |
< |
kernel_data->v[0xf68 >> 2] = htonl(BusClockSpeed); |
984 |
< |
kernel_data->v[0xf6c >> 2] = htonl(CPUClockSpeed); |
982 |
> |
kernel_data->v[0xf64 >> 2] = htonl(CPUClockSpeed); // clock-frequency |
983 |
> |
kernel_data->v[0xf68 >> 2] = htonl(BusClockSpeed); // bus-frequency |
984 |
> |
kernel_data->v[0xf6c >> 2] = htonl(TimebaseSpeed); // timebase-frequency |
985 |
|
} else { |
986 |
|
kernel_data->v[0xc80 >> 2] = htonl(RAMSize); |
987 |
|
kernel_data->v[0xc84 >> 2] = htonl(RAMSize); |
993 |
|
kernel_data->v[0xcb0 >> 2] = htonl(RAMBase); |
994 |
|
kernel_data->v[0xcb4 >> 2] = htonl(RAMSize); |
995 |
|
kernel_data->v[0xf80 >> 2] = htonl(PVR); |
996 |
< |
kernel_data->v[0xf84 >> 2] = htonl(CPUClockSpeed); |
997 |
< |
kernel_data->v[0xf88 >> 2] = htonl(BusClockSpeed); |
998 |
< |
kernel_data->v[0xf8c >> 2] = htonl(CPUClockSpeed); |
996 |
> |
kernel_data->v[0xf84 >> 2] = htonl(CPUClockSpeed); // clock-frequency |
997 |
> |
kernel_data->v[0xf88 >> 2] = htonl(BusClockSpeed); // bus-frequency |
998 |
> |
kernel_data->v[0xf8c >> 2] = htonl(TimebaseSpeed); // timebase-frequency |
999 |
|
} |
1000 |
|
|
1001 |
|
// Initialize extra low memory |
1002 |
|
D(bug("Initializing Low Memory...\n")); |
1003 |
|
memset(NULL, 0, 0x3000); |
1004 |
|
WriteMacInt32(XLM_SIGNATURE, FOURCC('B','a','a','h')); // Signature to detect SheepShaver |
1005 |
< |
WriteMacInt32(XLM_KERNEL_DATA, (uint32)kernel_data); // For trap replacement routines |
1005 |
> |
WriteMacInt32(XLM_KERNEL_DATA, KernelDataAddr); // For trap replacement routines |
1006 |
|
WriteMacInt32(XLM_PVR, PVR); // Theoretical PVR |
1007 |
|
WriteMacInt32(XLM_BUS_CLOCK, BusClockSpeed); // For DriverServicesLib patch |
1008 |
|
WriteMacInt16(XLM_EXEC_RETURN_OPCODE, M68K_EXEC_RETURN); // For Execute68k() (RTS from the executed 68k code will jump here and end 68k mode) |
1009 |
< |
#if EMULATED_PPC |
1010 |
< |
WriteMacInt32(XLM_ETHER_INIT, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_INIT)); |
1011 |
< |
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); |
1009 |
> |
WriteMacInt32(XLM_ZERO_PAGE, SheepMem::ZeroPage()); // Pointer to read-only page with all bits set to 0 |
1010 |
> |
#if !EMULATED_PPC |
1011 |
> |
WriteMacInt32(XLM_TOC, (uint32)TOC); // TOC pointer of emulator |
1012 |
|
#endif |
1013 |
+ |
WriteMacInt32(XLM_ETHER_INIT, NativeFunction(NATIVE_ETHER_INIT)); // DLPI ethernet driver functions |
1014 |
+ |
WriteMacInt32(XLM_ETHER_TERM, NativeFunction(NATIVE_ETHER_TERM)); |
1015 |
+ |
WriteMacInt32(XLM_ETHER_OPEN, NativeFunction(NATIVE_ETHER_OPEN)); |
1016 |
+ |
WriteMacInt32(XLM_ETHER_CLOSE, NativeFunction(NATIVE_ETHER_CLOSE)); |
1017 |
+ |
WriteMacInt32(XLM_ETHER_WPUT, NativeFunction(NATIVE_ETHER_WPUT)); |
1018 |
+ |
WriteMacInt32(XLM_ETHER_RSRV, NativeFunction(NATIVE_ETHER_RSRV)); |
1019 |
+ |
WriteMacInt32(XLM_VIDEO_DOIO, NativeFunction(NATIVE_VIDEO_DO_DRIVER_IO)); |
1020 |
|
D(bug("Low Memory initialized\n")); |
1021 |
|
|
1022 |
|
// Start 60Hz thread |
1023 |
+ |
tick_thread_cancel = false; |
1024 |
|
tick_thread_active = (pthread_create(&tick_thread, NULL, tick_func, NULL) == 0); |
1025 |
|
D(bug("Tick thread installed (%ld)\n", tick_thread)); |
1026 |
|
|
1027 |
|
// Start NVRAM watchdog thread |
1028 |
|
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
1029 |
+ |
nvram_thread_cancel = false; |
1030 |
|
nvram_thread_active = (pthread_create(&nvram_thread, NULL, nvram_func, NULL) == 0); |
1031 |
|
D(bug("NVRAM thread installed (%ld)\n", nvram_thread)); |
1032 |
|
|
1033 |
|
#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 |
– |
|
1034 |
|
// Install SIGILL handler |
1035 |
|
sigemptyset(&sigill_action.sa_mask); // Block interrupts during ILL handling |
1036 |
|
sigaddset(&sigill_action.sa_mask, SIGUSR2); |
1037 |
< |
sigill_action.sa_handler = (__sighandler_t)sigill_handler; |
1038 |
< |
sigill_action.sa_flags = SA_ONSTACK; |
1037 |
> |
sigill_action.sa_sigaction = sigill_handler; |
1038 |
> |
sigill_action.sa_flags = SA_ONSTACK | SA_SIGINFO; |
1039 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
1040 |
|
sigill_action.sa_restorer = NULL; |
1041 |
+ |
#endif |
1042 |
|
if (sigaction(SIGILL, &sigill_action, NULL) < 0) { |
1043 |
|
sprintf(str, GetString(STR_SIGILL_INSTALL_ERR), strerror(errno)); |
1044 |
|
ErrorAlert(str); |
1046 |
|
} |
1047 |
|
#endif |
1048 |
|
|
1049 |
+ |
#if !EMULATED_PPC |
1050 |
|
// Install interrupt signal handler |
1051 |
|
sigemptyset(&sigusr2_action.sa_mask); |
1052 |
< |
sigusr2_action.sa_handler = (__sighandler_t)sigusr2_handler; |
1053 |
< |
sigusr2_action.sa_flags = 0; |
1054 |
< |
#if !EMULATED_PPC |
759 |
< |
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART; |
760 |
< |
#endif |
1052 |
> |
sigusr2_action.sa_sigaction = sigusr2_handler; |
1053 |
> |
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART | SA_SIGINFO; |
1054 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
1055 |
|
sigusr2_action.sa_restorer = NULL; |
1056 |
+ |
#endif |
1057 |
|
if (sigaction(SIGUSR2, &sigusr2_action, NULL) < 0) { |
1058 |
|
sprintf(str, GetString(STR_SIGUSR2_INSTALL_ERR), strerror(errno)); |
1059 |
|
ErrorAlert(str); |
1060 |
|
goto quit; |
1061 |
|
} |
1062 |
+ |
#endif |
1063 |
|
|
1064 |
|
// Get my thread ID and execute MacOS thread function |
1065 |
|
emul_thread = pthread_self(); |
1085 |
|
|
1086 |
|
// Stop 60Hz thread |
1087 |
|
if (tick_thread_active) { |
1088 |
+ |
tick_thread_cancel = true; |
1089 |
|
pthread_cancel(tick_thread); |
1090 |
|
pthread_join(tick_thread, NULL); |
1091 |
|
} |
1092 |
|
|
1093 |
|
// Stop NVRAM watchdog thread |
1094 |
|
if (nvram_thread_active) { |
1095 |
+ |
nvram_thread_cancel = true; |
1096 |
|
pthread_cancel(nvram_thread); |
1097 |
|
pthread_join(nvram_thread, NULL); |
1098 |
|
} |
1099 |
|
|
1100 |
|
#if !EMULATED_PPC |
1101 |
< |
// Uninstall SIGSEGV handler |
1101 |
> |
// Uninstall SIGSEGV and SIGBUS handlers |
1102 |
|
sigemptyset(&sigsegv_action.sa_mask); |
1103 |
|
sigsegv_action.sa_handler = SIG_DFL; |
1104 |
|
sigsegv_action.sa_flags = 0; |
1105 |
|
sigaction(SIGSEGV, &sigsegv_action, NULL); |
1106 |
+ |
sigaction(SIGBUS, &sigsegv_action, NULL); |
1107 |
|
|
1108 |
|
// Uninstall SIGILL handler |
1109 |
|
sigemptyset(&sigill_action.sa_mask); |
1110 |
|
sigill_action.sa_handler = SIG_DFL; |
1111 |
|
sigill_action.sa_flags = 0; |
1112 |
|
sigaction(SIGILL, &sigill_action, NULL); |
1113 |
+ |
|
1114 |
+ |
// Delete stacks for signal handlers |
1115 |
+ |
for (int i = 0; i < SIG_STACK_COUNT; i++) { |
1116 |
+ |
void *sig_stack = sig_stacks[i].ss_sp; |
1117 |
+ |
if (sig_stack) |
1118 |
+ |
free(sig_stack); |
1119 |
+ |
} |
1120 |
|
#endif |
1121 |
|
|
1122 |
|
// Save NVRAM |
1137 |
|
// Exit audio |
1138 |
|
AudioExit(); |
1139 |
|
|
1140 |
+ |
// Exit ADB |
1141 |
+ |
ADBExit(); |
1142 |
+ |
|
1143 |
|
// Exit video |
1144 |
|
VideoExit(); |
1145 |
|
|
1152 |
|
DiskExit(); |
1153 |
|
SonyExit(); |
1154 |
|
|
1155 |
+ |
// Delete thunks |
1156 |
+ |
ThunksExit(); |
1157 |
+ |
|
1158 |
+ |
// Delete SheepShaver globals |
1159 |
+ |
SheepMem::Exit(); |
1160 |
+ |
|
1161 |
|
// Delete RAM area |
1162 |
|
if (ram_area_mapped) |
1163 |
|
vm_release((char *)RAM_BASE, RAMSize); |
1166 |
|
if (rom_area_mapped) |
1167 |
|
vm_release((char *)ROM_BASE, ROM_AREA_SIZE); |
1168 |
|
|
1169 |
+ |
// Delete DR cache areas |
1170 |
+ |
if (dr_emulator_area_mapped) |
1171 |
+ |
vm_release((void *)DR_EMULATOR_BASE, DR_EMULATOR_SIZE); |
1172 |
+ |
if (dr_cache_area_mapped) |
1173 |
+ |
vm_release((void *)DR_CACHE_BASE, DR_CACHE_SIZE); |
1174 |
+ |
|
1175 |
|
// Delete Kernel Data area |
1176 |
|
if (kernel_area >= 0) { |
1177 |
|
shmdt((void *)KERNEL_DATA_BASE); |
1199 |
|
#endif |
1200 |
|
|
1201 |
|
// Close X11 server connection |
1202 |
+ |
#ifndef USE_SDL_VIDEO |
1203 |
|
if (x_display) |
1204 |
|
XCloseDisplay(x_display); |
1205 |
+ |
#endif |
1206 |
|
|
1207 |
|
exit(0); |
1208 |
|
} |
1277 |
|
uint16 proc[2] = {trap, M68K_RTS}; |
1278 |
|
Execute68k((uint32)proc, r); |
1279 |
|
} |
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 |
– |
} |
1280 |
|
#endif |
1281 |
|
|
1282 |
|
|
1355 |
|
|
1356 |
|
void PatchAfterStartup(void) |
1357 |
|
{ |
1048 |
– |
#if EMULATED_PPC |
1358 |
|
ExecuteNative(NATIVE_VIDEO_INSTALL_ACCEL); |
1050 |
– |
#else |
1051 |
– |
ExecutePPC(VideoInstallAccel); |
1052 |
– |
#endif |
1359 |
|
InstallExtFS(); |
1360 |
|
} |
1361 |
|
|
1364 |
|
* NVRAM watchdog thread (saves NVRAM every minute) |
1365 |
|
*/ |
1366 |
|
|
1367 |
< |
static void *nvram_func(void *arg) |
1367 |
> |
static void nvram_watchdog(void) |
1368 |
|
{ |
1369 |
< |
struct timespec req = {60, 0}; // 1 minute |
1369 |
> |
if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) { |
1370 |
> |
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
1371 |
> |
SaveXPRAM(); |
1372 |
> |
} |
1373 |
> |
} |
1374 |
|
|
1375 |
< |
for (;;) { |
1376 |
< |
pthread_testcancel(); |
1377 |
< |
nanosleep(&req, NULL); |
1378 |
< |
pthread_testcancel(); |
1379 |
< |
if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) { |
1380 |
< |
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
1071 |
< |
SaveXPRAM(); |
1072 |
< |
} |
1375 |
> |
static void *nvram_func(void *arg) |
1376 |
> |
{ |
1377 |
> |
while (!nvram_thread_cancel) { |
1378 |
> |
for (int i=0; i<60 && !nvram_thread_cancel; i++) |
1379 |
> |
Delay_usec(999999); // Only wait 1 second so we quit promptly when nvram_thread_cancel becomes true |
1380 |
> |
nvram_watchdog(); |
1381 |
|
} |
1382 |
|
return NULL; |
1383 |
|
} |
1390 |
|
static void *tick_func(void *arg) |
1391 |
|
{ |
1392 |
|
int tick_counter = 0; |
1393 |
< |
struct timespec req = {0, 16625000}; |
1393 |
> |
uint64 start = GetTicks_usec(); |
1394 |
> |
int64 ticks = 0; |
1395 |
> |
uint64 next = GetTicks_usec(); |
1396 |
|
|
1397 |
< |
for (;;) { |
1397 |
> |
while (!tick_thread_cancel) { |
1398 |
|
|
1399 |
|
// Wait |
1400 |
< |
nanosleep(&req, NULL); |
1400 |
> |
next += 16625; |
1401 |
> |
int64 delay = next - GetTicks_usec(); |
1402 |
> |
if (delay > 0) |
1403 |
> |
Delay_usec(delay); |
1404 |
> |
else if (delay < -16625) |
1405 |
> |
next = GetTicks_usec(); |
1406 |
> |
ticks++; |
1407 |
|
|
1408 |
|
#if !EMULATED_PPC |
1409 |
|
// Did we crash? |
1410 |
|
if (emul_thread_fatal) { |
1411 |
|
|
1412 |
|
// Yes, dump registers |
1413 |
< |
pt_regs *r = (pt_regs *)&sigsegv_regs; |
1413 |
> |
sigregs *r = &sigsegv_regs; |
1414 |
|
char str[256]; |
1415 |
< |
sprintf(str, "SIGSEGV\n" |
1415 |
> |
if (crash_reason == NULL) |
1416 |
> |
crash_reason = "SIGSEGV"; |
1417 |
> |
sprintf(str, "%s\n" |
1418 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1419 |
|
" xer %08lx cr %08lx \n" |
1420 |
|
" r0 %08lx r1 %08lx r2 %08lx r3 %08lx\n" |
1425 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1426 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1427 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1428 |
+ |
crash_reason, |
1429 |
|
r->nip, r->link, r->ctr, r->msr, |
1430 |
|
r->xer, r->ccr, |
1431 |
|
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1461 |
|
TriggerInterrupt(); |
1462 |
|
} |
1463 |
|
} |
1464 |
+ |
|
1465 |
+ |
uint64 end = GetTicks_usec(); |
1466 |
+ |
D(bug("%Ld ticks in %Ld usec = %f ticks/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start))); |
1467 |
|
return NULL; |
1468 |
|
} |
1469 |
|
|
1584 |
|
* Trigger signal USR2 from another thread |
1585 |
|
*/ |
1586 |
|
|
1587 |
< |
#if !EMULATED_PPC || ASYNC_IRQ |
1587 |
> |
#if !EMULATED_PPC |
1588 |
|
void TriggerInterrupt(void) |
1589 |
|
{ |
1590 |
|
if (ready_for_signals) |
1616 |
|
|
1617 |
|
void DisableInterrupt(void) |
1618 |
|
{ |
1619 |
+ |
#if EMULATED_PPC |
1620 |
+ |
WriteMacInt32(XLM_IRQ_NEST, int32(ReadMacInt32(XLM_IRQ_NEST)) + 1); |
1621 |
+ |
#else |
1622 |
|
atomic_add((int *)XLM_IRQ_NEST, 1); |
1623 |
+ |
#endif |
1624 |
|
} |
1625 |
|
|
1626 |
|
|
1630 |
|
|
1631 |
|
void EnableInterrupt(void) |
1632 |
|
{ |
1633 |
+ |
#if EMULATED_PPC |
1634 |
+ |
WriteMacInt32(XLM_IRQ_NEST, int32(ReadMacInt32(XLM_IRQ_NEST)) - 1); |
1635 |
+ |
#else |
1636 |
|
atomic_add((int *)XLM_IRQ_NEST, -1); |
1637 |
+ |
#endif |
1638 |
|
} |
1639 |
|
|
1640 |
|
|
1642 |
|
* USR2 handler |
1643 |
|
*/ |
1644 |
|
|
1645 |
< |
#if EMULATED_PPC |
1646 |
< |
static void sigusr2_handler(int sig) |
1645 |
> |
#if !EMULATED_PPC |
1646 |
> |
static void sigusr2_handler(int sig, siginfo_t *sip, void *scp) |
1647 |
|
{ |
1648 |
< |
#if ASYNC_IRQ |
1649 |
< |
extern void HandleInterrupt(void); |
1650 |
< |
HandleInterrupt(); |
1648 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1649 |
> |
|
1650 |
> |
#ifdef USE_SDL_VIDEO |
1651 |
> |
// We must fill in the events queue in the same thread that did call SDL_SetVideoMode() |
1652 |
> |
SDL_PumpEvents(); |
1653 |
|
#endif |
1322 |
– |
} |
1323 |
– |
#else |
1324 |
– |
static void sigusr2_handler(int sig, sigcontext_struct *sc) |
1325 |
– |
{ |
1326 |
– |
pt_regs *r = sc->regs; |
1654 |
|
|
1655 |
|
// Do nothing if interrupts are disabled |
1656 |
|
if (*(int32 *)XLM_IRQ_NEST > 0) |
1664 |
|
case MODE_68K: |
1665 |
|
// 68k emulator active, trigger 68k interrupt level 1 |
1666 |
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1667 |
< |
r->ccr |= ntohl(kernel_data->v[0x674 >> 2]); |
1667 |
> |
r->cr() |= ntohl(kernel_data->v[0x674 >> 2]); |
1668 |
|
break; |
1669 |
|
|
1670 |
|
#if INTERRUPTS_IN_NATIVE_MODE |
1671 |
|
case MODE_NATIVE: |
1672 |
|
// 68k emulator inactive, in nanokernel? |
1673 |
< |
if (r->gpr[1] != KernelDataAddr) { |
1673 |
> |
if (r->gpr(1) != KernelDataAddr) { |
1674 |
> |
|
1675 |
> |
// Set extra stack for nested interrupts |
1676 |
> |
sig_stack_acquire(); |
1677 |
> |
|
1678 |
|
// Prepare for 68k interrupt level 1 |
1679 |
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1680 |
|
WriteMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc, ReadMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc) | ntohl(kernel_data->v[0x674 >> 2])); |
1681 |
|
|
1682 |
|
// Execute nanokernel interrupt routine (this will activate the 68k emulator) |
1683 |
< |
atomic_add((int32 *)XLM_IRQ_NEST, 1); |
1683 |
> |
DisableInterrupt(); |
1684 |
|
if (ROMType == ROMTYPE_NEWWORLD) |
1685 |
|
ppc_interrupt(ROM_BASE + 0x312b1c, KernelDataAddr); |
1686 |
|
else |
1687 |
|
ppc_interrupt(ROM_BASE + 0x312a3c, KernelDataAddr); |
1688 |
+ |
|
1689 |
+ |
// Reset normal signal stack |
1690 |
+ |
sig_stack_release(); |
1691 |
|
} |
1692 |
|
break; |
1693 |
|
#endif |
1698 |
|
if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) { |
1699 |
|
|
1700 |
|
// Set extra stack for SIGSEGV handler |
1701 |
< |
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); |
1701 |
> |
sig_stack_acquire(); |
1702 |
|
#if 1 |
1703 |
|
// Execute full 68k interrupt routine |
1704 |
|
M68kRegisters r; |
1720 |
|
if (InterruptFlags & INTFLAG_VIA) { |
1721 |
|
ClearInterruptFlag(INTFLAG_VIA); |
1722 |
|
ADBInterrupt(); |
1723 |
< |
ExecutePPC(VideoVBL); |
1723 |
> |
ExecuteNative(NATIVE_VIDEO_VBL); |
1724 |
|
} |
1725 |
|
} |
1726 |
|
#endif |
1727 |
|
// Reset normal signal stack |
1728 |
< |
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); |
1728 |
> |
sig_stack_release(); |
1729 |
|
} |
1730 |
|
break; |
1731 |
|
#endif |
1739 |
|
*/ |
1740 |
|
|
1741 |
|
#if !EMULATED_PPC |
1742 |
< |
static void sigsegv_handler(int sig, sigcontext_struct *sc) |
1742 |
> |
static void sigsegv_handler(int sig, siginfo_t *sip, void *scp) |
1743 |
|
{ |
1744 |
< |
pt_regs *r = sc->regs; |
1744 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1745 |
|
|
1746 |
|
// Get effective address |
1747 |
< |
uint32 addr = r->dar; |
1747 |
> |
uint32 addr = r->dar(); |
1748 |
|
|
1749 |
|
#if ENABLE_VOSF |
1750 |
|
// Handle screen fault. |
1751 |
|
extern bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction); |
1752 |
< |
if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->nip)) |
1752 |
> |
if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->pc())) |
1753 |
|
return; |
1754 |
|
#endif |
1755 |
|
|
1756 |
|
num_segv++; |
1757 |
|
|
1758 |
< |
// Fault in Mac ROM or RAM? |
1759 |
< |
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
1758 |
> |
// Fault in Mac ROM or RAM or DR Cache? |
1759 |
> |
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)); |
1760 |
|
if (mac_fault) { |
1761 |
|
|
1762 |
|
// "VM settings" during MacOS 8 installation |
1763 |
< |
if (r->nip == ROM_BASE + 0x488160 && r->gpr[20] == 0xf8000000) { |
1764 |
< |
r->nip += 4; |
1765 |
< |
r->gpr[8] = 0; |
1763 |
> |
if (r->pc() == ROM_BASE + 0x488160 && r->gpr(20) == 0xf8000000) { |
1764 |
> |
r->pc() += 4; |
1765 |
> |
r->gpr(8) = 0; |
1766 |
|
return; |
1767 |
|
|
1768 |
|
// MacOS 8.5 installation |
1769 |
< |
} else if (r->nip == ROM_BASE + 0x488140 && r->gpr[16] == 0xf8000000) { |
1770 |
< |
r->nip += 4; |
1771 |
< |
r->gpr[8] = 0; |
1769 |
> |
} else if (r->pc() == ROM_BASE + 0x488140 && r->gpr(16) == 0xf8000000) { |
1770 |
> |
r->pc() += 4; |
1771 |
> |
r->gpr(8) = 0; |
1772 |
|
return; |
1773 |
|
|
1774 |
|
// MacOS 8 serial drivers on startup |
1775 |
< |
} else if (r->nip == ROM_BASE + 0x48e080 && (r->gpr[8] == 0xf3012002 || r->gpr[8] == 0xf3012000)) { |
1776 |
< |
r->nip += 4; |
1777 |
< |
r->gpr[8] = 0; |
1775 |
> |
} else if (r->pc() == ROM_BASE + 0x48e080 && (r->gpr(8) == 0xf3012002 || r->gpr(8) == 0xf3012000)) { |
1776 |
> |
r->pc() += 4; |
1777 |
> |
r->gpr(8) = 0; |
1778 |
|
return; |
1779 |
|
|
1780 |
|
// MacOS 8.1 serial drivers on startup |
1781 |
< |
} else if (r->nip == ROM_BASE + 0x48c5e0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1782 |
< |
r->nip += 4; |
1781 |
> |
} else if (r->pc() == ROM_BASE + 0x48c5e0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1782 |
> |
r->pc() += 4; |
1783 |
|
return; |
1784 |
< |
} else if (r->nip == ROM_BASE + 0x4a10a0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1785 |
< |
r->nip += 4; |
1784 |
> |
} else if (r->pc() == ROM_BASE + 0x4a10a0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1785 |
> |
r->pc() += 4; |
1786 |
> |
return; |
1787 |
> |
|
1788 |
> |
// MacOS 8.6 serial drivers on startup (with DR Cache and OldWorld ROM) |
1789 |
> |
} else if ((r->pc() - DR_CACHE_BASE) < DR_CACHE_SIZE && (r->gpr(16) == 0xf3012002 || r->gpr(16) == 0xf3012000)) { |
1790 |
> |
r->pc() += 4; |
1791 |
> |
return; |
1792 |
> |
} else if ((r->pc() - DR_CACHE_BASE) < DR_CACHE_SIZE && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1793 |
> |
r->pc() += 4; |
1794 |
|
return; |
1795 |
|
} |
1796 |
|
|
1797 |
|
// Get opcode and divide into fields |
1798 |
< |
uint32 opcode = *((uint32 *)r->nip); |
1798 |
> |
uint32 opcode = *((uint32 *)r->pc()); |
1799 |
|
uint32 primop = opcode >> 26; |
1800 |
|
uint32 exop = (opcode >> 1) & 0x3ff; |
1801 |
|
uint32 ra = (opcode >> 16) & 0x1f; |
1884 |
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_NORM; break; |
1885 |
|
case 45: // sthu |
1886 |
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_U; break; |
1887 |
+ |
#if EMULATE_UNALIGNED_LOADSTORE_MULTIPLE |
1888 |
+ |
case 46: // lmw |
1889 |
+ |
if ((addr % 4) != 0) { |
1890 |
+ |
uint32 ea = addr; |
1891 |
+ |
D(bug("WARNING: unaligned lmw to EA=%08x from IP=%08x\n", ea, r->pc())); |
1892 |
+ |
for (int i = rd; i <= 31; i++) { |
1893 |
+ |
r->gpr(i) = ReadMacInt32(ea); |
1894 |
+ |
ea += 4; |
1895 |
+ |
} |
1896 |
+ |
r->pc() += 4; |
1897 |
+ |
goto rti; |
1898 |
+ |
} |
1899 |
+ |
break; |
1900 |
+ |
case 47: // stmw |
1901 |
+ |
if ((addr % 4) != 0) { |
1902 |
+ |
uint32 ea = addr; |
1903 |
+ |
D(bug("WARNING: unaligned stmw to EA=%08x from IP=%08x\n", ea, r->pc())); |
1904 |
+ |
for (int i = rd; i <= 31; i++) { |
1905 |
+ |
WriteMacInt32(ea, r->gpr(i)); |
1906 |
+ |
ea += 4; |
1907 |
+ |
} |
1908 |
+ |
r->pc() += 4; |
1909 |
+ |
goto rti; |
1910 |
+ |
} |
1911 |
+ |
break; |
1912 |
+ |
#endif |
1913 |
|
} |
1914 |
|
|
1915 |
< |
// Ignore ROM writes |
1916 |
< |
if (transfer_type == TYPE_STORE && addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) { |
1917 |
< |
// 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)); |
1915 |
> |
// Ignore ROM writes (including to the zero page, which is read-only) |
1916 |
> |
if (transfer_type == TYPE_STORE && |
1917 |
> |
((addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) || |
1918 |
> |
(addr >= SheepMem::ZeroPage() && addr < SheepMem::ZeroPage() + SheepMem::PageSize()))) { |
1919 |
> |
// 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())); |
1920 |
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1921 |
< |
r->gpr[ra] = addr; |
1922 |
< |
r->nip += 4; |
1921 |
> |
r->gpr(ra) = addr; |
1922 |
> |
r->pc() += 4; |
1923 |
|
goto rti; |
1924 |
|
} |
1925 |
|
|
1926 |
|
// Ignore illegal memory accesses? |
1927 |
|
if (PrefsFindBool("ignoresegv")) { |
1928 |
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1929 |
< |
r->gpr[ra] = addr; |
1929 |
> |
r->gpr(ra) = addr; |
1930 |
|
if (transfer_type == TYPE_LOAD) |
1931 |
< |
r->gpr[rd] = 0; |
1932 |
< |
r->nip += 4; |
1931 |
> |
r->gpr(rd) = 0; |
1932 |
> |
r->pc() += 4; |
1933 |
|
goto rti; |
1934 |
|
} |
1935 |
|
|
1937 |
|
if (!PrefsFindBool("nogui")) { |
1938 |
|
char str[256]; |
1939 |
|
if (transfer_type == TYPE_LOAD || transfer_type == TYPE_STORE) |
1940 |
< |
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]); |
1940 |
> |
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)); |
1941 |
|
else |
1942 |
< |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode); |
1942 |
> |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode); |
1943 |
|
ErrorAlert(str); |
1944 |
|
QuitEmulator(); |
1945 |
|
return; |
1947 |
|
} |
1948 |
|
|
1949 |
|
// For all other errors, jump into debugger (sort of...) |
1950 |
+ |
crash_reason = (sig == SIGBUS) ? "SIGBUS" : "SIGSEGV"; |
1951 |
|
if (!ready_for_signals) { |
1952 |
< |
printf("SIGSEGV\n"); |
1953 |
< |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
1952 |
> |
printf("%s\n"); |
1953 |
> |
printf(" sigcontext %p, machine_regs %p\n", scp, r); |
1954 |
|
printf( |
1955 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1956 |
|
" xer %08lx cr %08lx \n" |
1962 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1963 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1964 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1965 |
< |
r->nip, r->link, r->ctr, r->msr, |
1966 |
< |
r->xer, r->ccr, |
1967 |
< |
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1968 |
< |
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
1969 |
< |
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
1970 |
< |
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
1971 |
< |
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
1972 |
< |
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
1973 |
< |
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
1974 |
< |
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
1965 |
> |
crash_reason, |
1966 |
> |
r->pc(), r->lr(), r->ctr(), r->msr(), |
1967 |
> |
r->xer(), r->cr(), |
1968 |
> |
r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3), |
1969 |
> |
r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7), |
1970 |
> |
r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11), |
1971 |
> |
r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15), |
1972 |
> |
r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19), |
1973 |
> |
r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23), |
1974 |
> |
r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27), |
1975 |
> |
r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31)); |
1976 |
|
exit(1); |
1977 |
|
QuitEmulator(); |
1978 |
|
return; |
1979 |
|
} else { |
1980 |
|
// We crashed. Save registers, tell tick thread and loop forever |
1981 |
< |
sigsegv_regs = *(sigregs *)r; |
1981 |
> |
build_sigregs(&sigsegv_regs, r); |
1982 |
|
emul_thread_fatal = true; |
1983 |
|
for (;;) ; |
1984 |
|
} |
1990 |
|
* SIGILL handler |
1991 |
|
*/ |
1992 |
|
|
1993 |
< |
static void sigill_handler(int sig, sigcontext_struct *sc) |
1993 |
> |
static void sigill_handler(int sig, siginfo_t *sip, void *scp) |
1994 |
|
{ |
1995 |
< |
pt_regs *r = sc->regs; |
1995 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1996 |
|
char str[256]; |
1997 |
|
|
1998 |
|
// Fault in Mac ROM or RAM? |
1999 |
< |
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
1999 |
> |
bool mac_fault = (r->pc() >= ROM_BASE) && (r->pc() < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize)); |
2000 |
|
if (mac_fault) { |
2001 |
|
|
2002 |
|
// Get opcode and divide into fields |
2003 |
< |
uint32 opcode = *((uint32 *)r->nip); |
2003 |
> |
uint32 opcode = *((uint32 *)r->pc()); |
2004 |
|
uint32 primop = opcode >> 26; |
2005 |
|
uint32 exop = (opcode >> 1) & 0x3ff; |
2006 |
|
uint32 ra = (opcode >> 16) & 0x1f; |
2011 |
|
switch (primop) { |
2012 |
|
case 9: // POWER instructions |
2013 |
|
case 22: |
2014 |
< |
power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->nip, r->gpr[1], opcode); |
2014 |
> |
power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->pc(), r->gpr(1), opcode); |
2015 |
|
ErrorAlert(str); |
2016 |
|
QuitEmulator(); |
2017 |
|
return; |
2019 |
|
case 31: |
2020 |
|
switch (exop) { |
2021 |
|
case 83: // mfmsr |
2022 |
< |
r->gpr[rd] = 0xf072; |
2023 |
< |
r->nip += 4; |
2022 |
> |
r->gpr(rd) = 0xf072; |
2023 |
> |
r->pc() += 4; |
2024 |
|
goto rti; |
2025 |
|
|
2026 |
|
case 210: // mtsr |
2027 |
|
case 242: // mtsrin |
2028 |
|
case 306: // tlbie |
2029 |
< |
r->nip += 4; |
2029 |
> |
r->pc() += 4; |
2030 |
|
goto rti; |
2031 |
|
|
2032 |
|
case 339: { // mfspr |
2042 |
|
case 957: // PMC3 |
2043 |
|
case 958: // PMC4 |
2044 |
|
case 959: // SDA |
2045 |
< |
r->nip += 4; |
2045 |
> |
r->pc() += 4; |
2046 |
|
goto rti; |
2047 |
|
case 25: // SDR1 |
2048 |
< |
r->gpr[rd] = 0xdead001f; |
2049 |
< |
r->nip += 4; |
2048 |
> |
r->gpr(rd) = 0xdead001f; |
2049 |
> |
r->pc() += 4; |
2050 |
|
goto rti; |
2051 |
|
case 287: // PVR |
2052 |
< |
r->gpr[rd] = PVR; |
2053 |
< |
r->nip += 4; |
2052 |
> |
r->gpr(rd) = PVR; |
2053 |
> |
r->pc() += 4; |
2054 |
|
goto rti; |
2055 |
|
} |
2056 |
|
break; |
2086 |
|
case 957: // PMC3 |
2087 |
|
case 958: // PMC4 |
2088 |
|
case 959: // SDA |
2089 |
< |
r->nip += 4; |
2089 |
> |
r->pc() += 4; |
2090 |
|
goto rti; |
2091 |
|
} |
2092 |
|
break; |
2105 |
|
|
2106 |
|
// In GUI mode, show error alert |
2107 |
|
if (!PrefsFindBool("nogui")) { |
2108 |
< |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode); |
2108 |
> |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode); |
2109 |
|
ErrorAlert(str); |
2110 |
|
QuitEmulator(); |
2111 |
|
return; |
2113 |
|
} |
2114 |
|
|
2115 |
|
// For all other errors, jump into debugger (sort of...) |
2116 |
+ |
crash_reason = "SIGILL"; |
2117 |
|
if (!ready_for_signals) { |
2118 |
< |
printf("SIGILL\n"); |
2119 |
< |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
2118 |
> |
printf("%s\n"); |
2119 |
> |
printf(" sigcontext %p, machine_regs %p\n", scp, r); |
2120 |
|
printf( |
2121 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
2122 |
|
" xer %08lx cr %08lx \n" |
2128 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
2129 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
2130 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
2131 |
< |
r->nip, r->link, r->ctr, r->msr, |
2132 |
< |
r->xer, r->ccr, |
2133 |
< |
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
2134 |
< |
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
2135 |
< |
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
2136 |
< |
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
2137 |
< |
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
2138 |
< |
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
2139 |
< |
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
2140 |
< |
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
2131 |
> |
crash_reason, |
2132 |
> |
r->pc(), r->lr(), r->ctr(), r->msr(), |
2133 |
> |
r->xer(), r->cr(), |
2134 |
> |
r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3), |
2135 |
> |
r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7), |
2136 |
> |
r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11), |
2137 |
> |
r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15), |
2138 |
> |
r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19), |
2139 |
> |
r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23), |
2140 |
> |
r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27), |
2141 |
> |
r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31)); |
2142 |
|
exit(1); |
2143 |
|
QuitEmulator(); |
2144 |
|
return; |
2145 |
|
} else { |
2146 |
|
// We crashed. Save registers, tell tick thread and loop forever |
2147 |
< |
sigsegv_regs = *(sigregs *)r; |
2147 |
> |
build_sigregs(&sigsegv_regs, r); |
2148 |
|
emul_thread_fatal = true; |
2149 |
|
for (;;) ; |
2150 |
|
} |
2154 |
|
|
2155 |
|
|
2156 |
|
/* |
2157 |
+ |
* Helpers to share 32-bit addressable data with MacOS |
2158 |
+ |
*/ |
2159 |
+ |
|
2160 |
+ |
bool SheepMem::Init(void) |
2161 |
+ |
{ |
2162 |
+ |
// Size of a native page |
2163 |
+ |
page_size = getpagesize(); |
2164 |
+ |
|
2165 |
+ |
// Allocate SheepShaver globals |
2166 |
+ |
if (vm_acquire_fixed((char *)base, size) < 0) |
2167 |
+ |
return false; |
2168 |
+ |
|
2169 |
+ |
// Allocate page with all bits set to 0 |
2170 |
+ |
zero_page = base + size; |
2171 |
+ |
if (vm_acquire_fixed((char *)zero_page, page_size) < 0) |
2172 |
+ |
return false; |
2173 |
+ |
memset((char *)zero_page, 0, page_size); |
2174 |
+ |
if (vm_protect((char *)zero_page, page_size, VM_PAGE_READ) < 0) |
2175 |
+ |
return false; |
2176 |
+ |
|
2177 |
+ |
#if EMULATED_PPC |
2178 |
+ |
// Allocate alternate stack for PowerPC interrupt routine |
2179 |
+ |
sig_stack = zero_page + page_size; |
2180 |
+ |
if (vm_acquire_fixed((char *)sig_stack, SIG_STACK_SIZE) < 0) |
2181 |
+ |
return false; |
2182 |
+ |
#endif |
2183 |
+ |
|
2184 |
+ |
top = base + size; |
2185 |
+ |
return true; |
2186 |
+ |
} |
2187 |
+ |
|
2188 |
+ |
void SheepMem::Exit(void) |
2189 |
+ |
{ |
2190 |
+ |
if (top) { |
2191 |
+ |
// Delete SheepShaver globals |
2192 |
+ |
vm_release((void *)base, size); |
2193 |
+ |
|
2194 |
+ |
// Delete zero page |
2195 |
+ |
vm_release((void *)zero_page, page_size); |
2196 |
+ |
|
2197 |
+ |
#if EMULATED_PPC |
2198 |
+ |
// Delete alternate stack for PowerPC interrupt routine |
2199 |
+ |
vm_release((void *)sig_stack, SIG_STACK_SIZE); |
2200 |
+ |
#endif |
2201 |
+ |
} |
2202 |
+ |
} |
2203 |
+ |
|
2204 |
+ |
|
2205 |
+ |
/* |
2206 |
|
* Display alert |
2207 |
|
*/ |
2208 |
|
|
2251 |
|
|
2252 |
|
void ErrorAlert(const char *text) |
2253 |
|
{ |
2254 |
< |
#ifdef ENABLE_GTK |
2254 |
> |
#if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO) |
2255 |
|
if (PrefsFindBool("nogui") || x_display == NULL) { |
2256 |
|
printf(GetString(STR_SHELL_ERROR_PREFIX), text); |
2257 |
|
return; |
2270 |
|
|
2271 |
|
void WarningAlert(const char *text) |
2272 |
|
{ |
2273 |
< |
#ifdef ENABLE_GTK |
2273 |
> |
#if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO) |
2274 |
|
if (PrefsFindBool("nogui") || x_display == NULL) { |
2275 |
|
printf(GetString(STR_SHELL_WARNING_PREFIX), text); |
2276 |
|
return; |