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 |
109 |
|
#include "user_strings.h" |
110 |
|
#include "vm_alloc.h" |
111 |
|
#include "sigsegv.h" |
112 |
+ |
#include "thunks.h" |
113 |
|
|
114 |
|
#define DEBUG 0 |
115 |
|
#include "debug.h" |
132 |
|
#endif |
133 |
|
|
134 |
|
|
135 |
+ |
// Enable emulation of unaligned lmw/stmw? |
136 |
+ |
#define EMULATE_UNALIGNED_LOADSTORE_MULTIPLE 1 |
137 |
+ |
|
138 |
|
// Enable Execute68k() safety checks? |
139 |
|
#define SAFE_EXEC_68K 0 |
140 |
|
|
149 |
|
const char ROM_FILE_NAME[] = "ROM"; |
150 |
|
const char ROM_FILE_NAME2[] = "Mac OS ROM"; |
151 |
|
|
152 |
< |
const uint32 RAM_BASE = 0x20000000; // Base address of RAM |
152 |
> |
const uintptr RAM_BASE = 0x20000000; // Base address of RAM |
153 |
|
const uint32 SIG_STACK_SIZE = 0x10000; // Size of signal stack |
154 |
|
|
155 |
|
|
156 |
|
#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 |
– |
|
157 |
|
struct sigregs { |
158 |
< |
elf_gregset_t gp_regs; // Identical to pt_regs |
159 |
< |
double fp_regs[ELF_NFPREG]; // f0..f31 and fpsrc |
160 |
< |
//more (uninteresting) stuff following here |
158 |
> |
uint32 nip; |
159 |
> |
uint32 link; |
160 |
> |
uint32 ctr; |
161 |
> |
uint32 msr; |
162 |
> |
uint32 xer; |
163 |
> |
uint32 ccr; |
164 |
> |
uint32 gpr[32]; |
165 |
> |
}; |
166 |
> |
|
167 |
> |
#if defined(__linux__) |
168 |
> |
#include <sys/ucontext.h> |
169 |
> |
#define MACHINE_REGISTERS(scp) ((machine_regs *)(((ucontext_t *)scp)->uc_mcontext.regs)) |
170 |
> |
|
171 |
> |
struct machine_regs : public pt_regs |
172 |
> |
{ |
173 |
> |
u_long & cr() { return pt_regs::ccr; } |
174 |
> |
uint32 cr() const { return pt_regs::ccr; } |
175 |
> |
uint32 lr() const { return pt_regs::link; } |
176 |
> |
uint32 ctr() const { return pt_regs::ctr; } |
177 |
> |
uint32 xer() const { return pt_regs::xer; } |
178 |
> |
uint32 msr() const { return pt_regs::msr; } |
179 |
> |
uint32 dar() const { return pt_regs::dar; } |
180 |
> |
u_long & pc() { return pt_regs::nip; } |
181 |
> |
uint32 pc() const { return pt_regs::nip; } |
182 |
> |
u_long & gpr(int i) { return pt_regs::gpr[i]; } |
183 |
> |
uint32 gpr(int i) const { return pt_regs::gpr[i]; } |
184 |
|
}; |
185 |
|
#endif |
186 |
|
|
187 |
+ |
#if defined(__APPLE__) && defined(__MACH__) |
188 |
+ |
#include <sys/signal.h> |
189 |
+ |
extern "C" int sigaltstack(const struct sigaltstack *ss, struct sigaltstack *oss); |
190 |
+ |
|
191 |
+ |
#include <sys/ucontext.h> |
192 |
+ |
#define MACHINE_REGISTERS(scp) ((machine_regs *)(((ucontext_t *)scp)->uc_mcontext)) |
193 |
+ |
|
194 |
+ |
struct machine_regs : public mcontext |
195 |
+ |
{ |
196 |
+ |
uint32 & cr() { return ss.cr; } |
197 |
+ |
uint32 cr() const { return ss.cr; } |
198 |
+ |
uint32 lr() const { return ss.lr; } |
199 |
+ |
uint32 ctr() const { return ss.ctr; } |
200 |
+ |
uint32 xer() const { return ss.xer; } |
201 |
+ |
uint32 msr() const { return ss.srr1; } |
202 |
+ |
uint32 dar() const { return es.dar; } |
203 |
+ |
uint32 & pc() { return ss.srr0; } |
204 |
+ |
uint32 pc() const { return ss.srr0; } |
205 |
+ |
uint32 & gpr(int i) { return (&ss.r0)[i]; } |
206 |
+ |
uint32 gpr(int i) const { return (&ss.r0)[i]; } |
207 |
+ |
}; |
208 |
+ |
#endif |
209 |
+ |
|
210 |
+ |
static void build_sigregs(sigregs *srp, machine_regs *mrp) |
211 |
+ |
{ |
212 |
+ |
srp->nip = mrp->pc(); |
213 |
+ |
srp->link = mrp->lr(); |
214 |
+ |
srp->ctr = mrp->ctr(); |
215 |
+ |
srp->msr = mrp->msr(); |
216 |
+ |
srp->xer = mrp->xer(); |
217 |
+ |
srp->ccr = mrp->cr(); |
218 |
+ |
for (int i = 0; i < 32; i++) |
219 |
+ |
srp->gpr[i] = mrp->gpr(i); |
220 |
+ |
} |
221 |
+ |
#endif |
222 |
+ |
|
223 |
|
|
224 |
|
// Global variables (exported) |
225 |
|
#if !EMULATED_PPC |
227 |
|
#endif |
228 |
|
uint32 RAMBase; // Base address of Mac RAM |
229 |
|
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 |
230 |
|
uint32 KernelDataAddr; // Address of Kernel Data |
231 |
|
uint32 BootGlobsAddr; // Address of BootGlobs structure at top of Mac RAM |
232 |
|
uint32 PVR; // Theoretical PVR |
235 |
|
|
236 |
|
|
237 |
|
// Global variables |
238 |
< |
static char *x_display_name = NULL; // X11 display name |
238 |
> |
char *x_display_name = NULL; // X11 display name |
239 |
|
Display *x_display = NULL; // X11 display handle |
240 |
+ |
#ifdef X11_LOCK_TYPE |
241 |
+ |
X11_LOCK_TYPE x_display_lock = X11_LOCK_INIT; // X11 display lock |
242 |
+ |
#endif |
243 |
|
|
244 |
|
static int zero_fd = 0; // FD of /dev/zero |
190 |
– |
static bool sheep_area_mapped = false; // Flag: SheepShaver data area mmap()ed |
245 |
|
static bool lm_area_mapped = false; // Flag: Low Memory area mmap()ped |
246 |
|
static int kernel_area = -1; // SHM ID of Kernel Data area |
247 |
|
static bool rom_area_mapped = false; // Flag: Mac ROM mmap()ped |
261 |
|
static int64 num_segv = 0; // Number of handled SEGV signals |
262 |
|
|
263 |
|
static struct sigaction sigusr2_action; // Interrupt signal (of emulator thread) |
264 |
< |
#if !EMULATED_PPC |
264 |
> |
#if EMULATED_PPC |
265 |
> |
static uintptr sig_stack = 0; // Stack for PowerPC interrupt routine |
266 |
> |
#else |
267 |
|
static struct sigaction sigsegv_action; // Data access exception signal (of emulator thread) |
268 |
|
static struct sigaction sigill_action; // Illegal instruction signal (of emulator thread) |
269 |
|
static void *sig_stack = NULL; // Stack for signal handlers |
270 |
|
static void *extra_stack = NULL; // Stack for SIGSEGV inside interrupt handler |
271 |
|
static bool emul_thread_fatal = false; // Flag: MacOS thread crashed, tick thread shall dump debug output |
272 |
|
static sigregs sigsegv_regs; // Register dump when crashed |
273 |
+ |
static const char *crash_reason = NULL; // Reason of the crash (SIGSEGV, SIGBUS, SIGILL) |
274 |
|
#endif |
275 |
|
|
276 |
+ |
uint32 SheepMem::page_size; // Size of a native page |
277 |
+ |
uintptr SheepMem::zero_page = 0; // Address of ro page filled in with zeros |
278 |
+ |
uintptr SheepMem::base = 0x60000000; // Address of SheepShaver data |
279 |
+ |
uintptr SheepMem::top = 0; // Top of SheepShaver data (stack like storage) |
280 |
+ |
|
281 |
|
|
282 |
|
// Prototypes |
283 |
|
static void Quit(void); |
286 |
|
static void *tick_func(void *arg); |
287 |
|
#if EMULATED_PPC |
288 |
|
static void sigusr2_handler(int sig); |
289 |
+ |
extern void emul_ppc(uint32 start); |
290 |
+ |
extern void init_emul_ppc(void); |
291 |
+ |
extern void exit_emul_ppc(void); |
292 |
|
#else |
293 |
< |
static void sigusr2_handler(int sig, sigcontext_struct *sc); |
294 |
< |
static void sigsegv_handler(int sig, sigcontext_struct *sc); |
295 |
< |
static void sigill_handler(int sig, sigcontext_struct *sc); |
293 |
> |
static void sigusr2_handler(int sig, siginfo_t *sip, void *scp); |
294 |
> |
static void sigsegv_handler(int sig, siginfo_t *sip, void *scp); |
295 |
> |
static void sigill_handler(int sig, siginfo_t *sip, void *scp); |
296 |
|
#endif |
297 |
|
|
298 |
|
|
299 |
|
// From asm_linux.S |
300 |
< |
#if EMULATED_PPC |
236 |
< |
extern int atomic_add(int *var, int v); |
237 |
< |
extern int atomic_and(int *var, int v); |
238 |
< |
extern int atomic_or(int *var, int v); |
239 |
< |
#else |
300 |
> |
#if !EMULATED_PPC |
301 |
|
extern "C" void *get_toc(void); |
302 |
|
extern "C" void *get_sp(void); |
303 |
|
extern "C" void flush_icache_range(void *start, void *end); |
312 |
|
#endif |
313 |
|
|
314 |
|
|
315 |
+ |
#if EMULATED_PPC |
316 |
+ |
/* |
317 |
+ |
* Return signal stack base |
318 |
+ |
*/ |
319 |
+ |
|
320 |
+ |
uintptr SignalStackBase(void) |
321 |
+ |
{ |
322 |
+ |
return sig_stack + SIG_STACK_SIZE; |
323 |
+ |
} |
324 |
+ |
|
325 |
+ |
|
326 |
+ |
/* |
327 |
+ |
* Atomic operations |
328 |
+ |
*/ |
329 |
+ |
|
330 |
+ |
#if HAVE_SPINLOCKS |
331 |
+ |
static spinlock_t atomic_ops_lock = SPIN_LOCK_UNLOCKED; |
332 |
+ |
#else |
333 |
+ |
#define spin_lock(LOCK) |
334 |
+ |
#define spin_unlock(LOCK) |
335 |
+ |
#endif |
336 |
+ |
|
337 |
+ |
int atomic_add(int *var, int v) |
338 |
+ |
{ |
339 |
+ |
spin_lock(&atomic_ops_lock); |
340 |
+ |
int ret = *var; |
341 |
+ |
*var += v; |
342 |
+ |
spin_unlock(&atomic_ops_lock); |
343 |
+ |
return ret; |
344 |
+ |
} |
345 |
+ |
|
346 |
+ |
int atomic_and(int *var, int v) |
347 |
+ |
{ |
348 |
+ |
spin_lock(&atomic_ops_lock); |
349 |
+ |
int ret = *var; |
350 |
+ |
*var &= v; |
351 |
+ |
spin_unlock(&atomic_ops_lock); |
352 |
+ |
return ret; |
353 |
+ |
} |
354 |
+ |
|
355 |
+ |
int atomic_or(int *var, int v) |
356 |
+ |
{ |
357 |
+ |
spin_lock(&atomic_ops_lock); |
358 |
+ |
int ret = *var; |
359 |
+ |
*var |= v; |
360 |
+ |
spin_unlock(&atomic_ops_lock); |
361 |
+ |
return ret; |
362 |
+ |
} |
363 |
+ |
#endif |
364 |
+ |
|
365 |
+ |
|
366 |
|
/* |
367 |
|
* Main program |
368 |
|
*/ |
381 |
|
char str[256]; |
382 |
|
uint32 *boot_globs; |
383 |
|
int16 i16; |
272 |
– |
int drive, driver; |
384 |
|
int rom_fd; |
385 |
|
FILE *proc_file; |
386 |
|
const char *rom_path; |
447 |
|
PVR = 0x00040000; // Default: 604 |
448 |
|
CPUClockSpeed = 100000000; // Default: 100MHz |
449 |
|
BusClockSpeed = 100000000; // Default: 100MHz |
450 |
< |
#if !EMULATED_PPC |
450 |
> |
#if EMULATED_PPC |
451 |
> |
PVR = 0x000c0000; // Default: 7400 (with AltiVec) |
452 |
> |
#else |
453 |
|
proc_file = fopen("/proc/cpuinfo", "r"); |
454 |
|
if (proc_file) { |
455 |
|
char line[256]; |
463 |
|
// Parse line |
464 |
|
int i; |
465 |
|
char value[256]; |
466 |
< |
if (sscanf(line, "cpu : %s", value) == 1) { |
466 |
> |
if (sscanf(line, "cpu : %[0-9A-Za-a]", value) == 1) { |
467 |
|
if (strcmp(value, "601") == 0) |
468 |
|
PVR = 0x00010000; |
469 |
|
else if (strcmp(value, "603") == 0) |
484 |
|
PVR = 0x00320000; |
485 |
|
else if (strcmp(value, "860") == 0) |
486 |
|
PVR = 0x00500000; |
487 |
+ |
else if (strcmp(value, "7400") == 0) |
488 |
+ |
PVR = 0x000c0000; |
489 |
+ |
else if (strcmp(value, "7410") == 0) |
490 |
+ |
PVR = 0x800c0000; |
491 |
|
else |
492 |
|
printf("WARNING: Unknown CPU type '%s', assuming 604\n", value); |
493 |
|
} |
523 |
|
goto quit; |
524 |
|
} |
525 |
|
|
526 |
+ |
#ifndef PAGEZERO_HACK |
527 |
|
// Create Low Memory area (0x0000..0x3000) |
528 |
|
if (vm_acquire_fixed((char *)0, 0x3000) < 0) { |
529 |
|
sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno)); |
531 |
|
goto quit; |
532 |
|
} |
533 |
|
lm_area_mapped = true; |
534 |
+ |
#endif |
535 |
|
|
536 |
|
// Create areas for Kernel Data |
537 |
|
kernel_area = shmget(IPC_PRIVATE, KERNEL_AREA_SIZE, 0600); |
550 |
|
ErrorAlert(str); |
551 |
|
goto quit; |
552 |
|
} |
553 |
< |
kernel_data = (KernelData *)0x68ffe000; |
553 |
> |
kernel_data = (KernelData *)KERNEL_DATA_BASE; |
554 |
|
emulator_data = &kernel_data->ed; |
555 |
< |
KernelDataAddr = (uint32)kernel_data; |
555 |
> |
KernelDataAddr = KERNEL_DATA_BASE; |
556 |
|
D(bug("Kernel Data at %p, Emulator Data at %p\n", kernel_data, emulator_data)); |
557 |
|
|
558 |
|
// Create area for SheepShaver data |
559 |
< |
if (vm_acquire_fixed((char *)SHEEP_BASE, SHEEP_SIZE) < 0) { |
559 |
> |
if (!SheepMem::Init()) { |
560 |
|
sprintf(str, GetString(STR_SHEEP_MEM_MMAP_ERR), strerror(errno)); |
561 |
|
ErrorAlert(str); |
562 |
|
goto quit; |
563 |
|
} |
445 |
– |
SheepStack1Base = SHEEP_BASE + 0x10000; |
446 |
– |
SheepStack2Base = SheepStack1Base + 0x10000; |
447 |
– |
SheepThunksBase = SheepStack2Base + 0x1000; |
448 |
– |
sheep_area_mapped = true; |
564 |
|
|
565 |
|
// Create area for Mac ROM |
566 |
|
if (vm_acquire_fixed((char *)ROM_BASE, ROM_AREA_SIZE) < 0) { |
568 |
|
ErrorAlert(str); |
569 |
|
goto quit; |
570 |
|
} |
571 |
< |
#if !EMULATED_PPC || defined(__powerpc__) |
571 |
> |
#if !EMULATED_PPC |
572 |
|
if (vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
573 |
|
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
574 |
|
ErrorAlert(str); |
638 |
|
// Load NVRAM |
639 |
|
XPRAMInit(); |
640 |
|
|
641 |
+ |
// Load XPRAM default values if signature not found |
642 |
+ |
if (XPRAM[0x130c] != 0x4e || XPRAM[0x130d] != 0x75 |
643 |
+ |
|| XPRAM[0x130e] != 0x4d || XPRAM[0x130f] != 0x63) { |
644 |
+ |
D(bug("Loading XPRAM default values\n")); |
645 |
+ |
memset(XPRAM + 0x1300, 0, 0x100); |
646 |
+ |
XPRAM[0x130c] = 0x4e; // "NuMc" signature |
647 |
+ |
XPRAM[0x130d] = 0x75; |
648 |
+ |
XPRAM[0x130e] = 0x4d; |
649 |
+ |
XPRAM[0x130f] = 0x63; |
650 |
+ |
XPRAM[0x1301] = 0x80; // InternalWaitFlags = DynWait (don't wait for SCSI devices upon bootup) |
651 |
+ |
XPRAM[0x1310] = 0xa8; // Standard PRAM values |
652 |
+ |
XPRAM[0x1311] = 0x00; |
653 |
+ |
XPRAM[0x1312] = 0x00; |
654 |
+ |
XPRAM[0x1313] = 0x22; |
655 |
+ |
XPRAM[0x1314] = 0xcc; |
656 |
+ |
XPRAM[0x1315] = 0x0a; |
657 |
+ |
XPRAM[0x1316] = 0xcc; |
658 |
+ |
XPRAM[0x1317] = 0x0a; |
659 |
+ |
XPRAM[0x131c] = 0x00; |
660 |
+ |
XPRAM[0x131d] = 0x02; |
661 |
+ |
XPRAM[0x131e] = 0x63; |
662 |
+ |
XPRAM[0x131f] = 0x00; |
663 |
+ |
XPRAM[0x1308] = 0x13; |
664 |
+ |
XPRAM[0x1309] = 0x88; |
665 |
+ |
XPRAM[0x130a] = 0x00; |
666 |
+ |
XPRAM[0x130b] = 0xcc; |
667 |
+ |
XPRAM[0x1376] = 0x00; // OSDefault = MacOS |
668 |
+ |
XPRAM[0x1377] = 0x01; |
669 |
+ |
} |
670 |
+ |
|
671 |
|
// Set boot volume |
672 |
< |
drive = PrefsFindInt32("bootdrive"); |
672 |
> |
i16 = PrefsFindInt32("bootdrive"); |
673 |
|
XPRAM[0x1378] = i16 >> 8; |
674 |
|
XPRAM[0x1379] = i16 & 0xff; |
675 |
< |
driver = PrefsFindInt32("bootdriver"); |
675 |
> |
i16 = PrefsFindInt32("bootdriver"); |
676 |
|
XPRAM[0x137a] = i16 >> 8; |
677 |
|
XPRAM[0x137b] = i16 & 0xff; |
678 |
|
|
685 |
|
boot_globs[1] = htonl(RAMSize); |
686 |
|
boot_globs[2] = htonl((uint32)-1); // End of bank table |
687 |
|
|
688 |
+ |
// Init thunks |
689 |
+ |
if (!ThunksInit()) |
690 |
+ |
goto quit; |
691 |
+ |
|
692 |
|
// Init drivers |
693 |
|
SonyInit(); |
694 |
|
DiskInit(); |
698 |
|
// Init external file system |
699 |
|
ExtFSInit(); |
700 |
|
|
701 |
+ |
// Init ADB |
702 |
+ |
ADBInit(); |
703 |
+ |
|
704 |
|
// Init audio |
705 |
|
AudioInit(); |
706 |
|
|
735 |
|
// Initialize Kernel Data |
736 |
|
memset(kernel_data, 0, sizeof(KernelData)); |
737 |
|
if (ROMType == ROMTYPE_NEWWORLD) { |
738 |
< |
static uint32 of_dev_tree[4] = {0, 0, 0, 0}; |
739 |
< |
static uint8 vector_lookup_tbl[128]; |
740 |
< |
static uint8 vector_mask_tbl[64]; |
738 |
> |
uintptr of_dev_tree = SheepMem::Reserve(4 * sizeof(uint32)); |
739 |
> |
memset((void *)of_dev_tree, 0, 4 * sizeof(uint32)); |
740 |
> |
uintptr vector_lookup_tbl = SheepMem::Reserve(128); |
741 |
> |
uintptr vector_mask_tbl = SheepMem::Reserve(64); |
742 |
|
memset((uint8 *)kernel_data + 0xb80, 0x3d, 0x80); |
743 |
< |
memset(vector_lookup_tbl, 0, 128); |
744 |
< |
memset(vector_mask_tbl, 0, 64); |
743 |
> |
memset((void *)vector_lookup_tbl, 0, 128); |
744 |
> |
memset((void *)vector_mask_tbl, 0, 64); |
745 |
|
kernel_data->v[0xb80 >> 2] = htonl(ROM_BASE); |
746 |
< |
kernel_data->v[0xb84 >> 2] = htonl((uint32)of_dev_tree); // OF device tree base |
747 |
< |
kernel_data->v[0xb90 >> 2] = htonl((uint32)vector_lookup_tbl); |
748 |
< |
kernel_data->v[0xb94 >> 2] = htonl((uint32)vector_mask_tbl); |
746 |
> |
kernel_data->v[0xb84 >> 2] = htonl(of_dev_tree); // OF device tree base |
747 |
> |
kernel_data->v[0xb90 >> 2] = htonl(vector_lookup_tbl); |
748 |
> |
kernel_data->v[0xb94 >> 2] = htonl(vector_mask_tbl); |
749 |
|
kernel_data->v[0xb98 >> 2] = htonl(ROM_BASE); // OpenPIC base |
750 |
|
kernel_data->v[0xbb0 >> 2] = htonl(0); // ADB base |
751 |
|
kernel_data->v[0xc20 >> 2] = htonl(RAMSize); |
781 |
|
D(bug("Initializing Low Memory...\n")); |
782 |
|
memset(NULL, 0, 0x3000); |
783 |
|
WriteMacInt32(XLM_SIGNATURE, FOURCC('B','a','a','h')); // Signature to detect SheepShaver |
784 |
< |
WriteMacInt32(XLM_KERNEL_DATA, (uint32)kernel_data); // For trap replacement routines |
784 |
> |
WriteMacInt32(XLM_KERNEL_DATA, KernelDataAddr); // For trap replacement routines |
785 |
|
WriteMacInt32(XLM_PVR, PVR); // Theoretical PVR |
786 |
|
WriteMacInt32(XLM_BUS_CLOCK, BusClockSpeed); // For DriverServicesLib patch |
787 |
|
WriteMacInt16(XLM_EXEC_RETURN_OPCODE, M68K_EXEC_RETURN); // For Execute68k() (RTS from the executed 68k code will jump here and end 68k mode) |
788 |
< |
#if EMULATED_PPC |
789 |
< |
WriteMacInt32(XLM_ETHER_INIT, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_INIT)); |
790 |
< |
WriteMacInt32(XLM_ETHER_TERM, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_TERM)); |
638 |
< |
WriteMacInt32(XLM_ETHER_OPEN, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_OPEN)); |
639 |
< |
WriteMacInt32(XLM_ETHER_CLOSE, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_CLOSE)); |
640 |
< |
WriteMacInt32(XLM_ETHER_WPUT, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_WPUT)); |
641 |
< |
WriteMacInt32(XLM_ETHER_RSRV, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_RSRV)); |
642 |
< |
WriteMacInt32(XLM_VIDEO_DOIO, POWERPC_NATIVE_OP_FUNC(NATIVE_VIDEO_DO_DRIVER_IO)); |
643 |
< |
#else |
644 |
< |
WriteMacInt32(XLM_TOC, (uint32)TOC); // TOC pointer of emulator |
645 |
< |
WriteMacInt32(XLM_ETHER_INIT, (uint32)InitStreamModule); // DLPI ethernet driver functions |
646 |
< |
WriteMacInt32(XLM_ETHER_TERM, (uint32)TerminateStreamModule); |
647 |
< |
WriteMacInt32(XLM_ETHER_OPEN, (uint32)ether_open); |
648 |
< |
WriteMacInt32(XLM_ETHER_CLOSE, (uint32)ether_close); |
649 |
< |
WriteMacInt32(XLM_ETHER_WPUT, (uint32)ether_wput); |
650 |
< |
WriteMacInt32(XLM_ETHER_RSRV, (uint32)ether_rsrv); |
651 |
< |
WriteMacInt32(XLM_VIDEO_DOIO, (uint32)VideoDoDriverIO); |
788 |
> |
WriteMacInt32(XLM_ZERO_PAGE, SheepMem::ZeroPage()); // Pointer to read-only page with all bits set to 0 |
789 |
> |
#if !EMULATED_PPC |
790 |
> |
WriteMacInt32(XLM_TOC, (uint32)TOC); // TOC pointer of emulator |
791 |
|
#endif |
792 |
+ |
WriteMacInt32(XLM_ETHER_INIT, NativeFunction(NATIVE_ETHER_INIT)); // DLPI ethernet driver functions |
793 |
+ |
WriteMacInt32(XLM_ETHER_TERM, NativeFunction(NATIVE_ETHER_TERM)); |
794 |
+ |
WriteMacInt32(XLM_ETHER_OPEN, NativeFunction(NATIVE_ETHER_OPEN)); |
795 |
+ |
WriteMacInt32(XLM_ETHER_CLOSE, NativeFunction(NATIVE_ETHER_CLOSE)); |
796 |
+ |
WriteMacInt32(XLM_ETHER_WPUT, NativeFunction(NATIVE_ETHER_WPUT)); |
797 |
+ |
WriteMacInt32(XLM_ETHER_RSRV, NativeFunction(NATIVE_ETHER_RSRV)); |
798 |
+ |
WriteMacInt32(XLM_VIDEO_DOIO, NativeFunction(NATIVE_VIDEO_DO_DRIVER_IO)); |
799 |
|
D(bug("Low Memory initialized\n")); |
800 |
|
|
801 |
|
// Start 60Hz thread |
833 |
|
#endif |
834 |
|
|
835 |
|
#if !EMULATED_PPC |
836 |
< |
// Install SIGSEGV handler |
836 |
> |
// Install SIGSEGV and SIGBUS handlers |
837 |
|
sigemptyset(&sigsegv_action.sa_mask); // Block interrupts during SEGV handling |
838 |
|
sigaddset(&sigsegv_action.sa_mask, SIGUSR2); |
839 |
< |
sigsegv_action.sa_handler = (__sighandler_t)sigsegv_handler; |
840 |
< |
sigsegv_action.sa_flags = SA_ONSTACK; |
839 |
> |
sigsegv_action.sa_sigaction = sigsegv_handler; |
840 |
> |
sigsegv_action.sa_flags = SA_ONSTACK | SA_SIGINFO; |
841 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
842 |
|
sigsegv_action.sa_restorer = NULL; |
843 |
+ |
#endif |
844 |
|
if (sigaction(SIGSEGV, &sigsegv_action, NULL) < 0) { |
845 |
|
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
846 |
|
ErrorAlert(str); |
847 |
|
goto quit; |
848 |
|
} |
849 |
+ |
if (sigaction(SIGBUS, &sigsegv_action, NULL) < 0) { |
850 |
+ |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
851 |
+ |
ErrorAlert(str); |
852 |
+ |
goto quit; |
853 |
+ |
} |
854 |
|
|
855 |
|
// Install SIGILL handler |
856 |
|
sigemptyset(&sigill_action.sa_mask); // Block interrupts during ILL handling |
857 |
|
sigaddset(&sigill_action.sa_mask, SIGUSR2); |
858 |
< |
sigill_action.sa_handler = (__sighandler_t)sigill_handler; |
859 |
< |
sigill_action.sa_flags = SA_ONSTACK; |
858 |
> |
sigill_action.sa_sigaction = sigill_handler; |
859 |
> |
sigill_action.sa_flags = SA_ONSTACK | SA_SIGINFO; |
860 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
861 |
|
sigill_action.sa_restorer = NULL; |
862 |
+ |
#endif |
863 |
|
if (sigaction(SIGILL, &sigill_action, NULL) < 0) { |
864 |
|
sprintf(str, GetString(STR_SIGILL_INSTALL_ERR), strerror(errno)); |
865 |
|
ErrorAlert(str); |
867 |
|
} |
868 |
|
#endif |
869 |
|
|
870 |
+ |
#if !EMULATED_PPC |
871 |
|
// Install interrupt signal handler |
872 |
|
sigemptyset(&sigusr2_action.sa_mask); |
873 |
< |
sigusr2_action.sa_handler = (__sighandler_t)sigusr2_handler; |
874 |
< |
sigusr2_action.sa_flags = 0; |
875 |
< |
#if !EMULATED_PPC |
720 |
< |
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART; |
721 |
< |
#endif |
873 |
> |
sigusr2_action.sa_sigaction = sigusr2_handler; |
874 |
> |
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART | SA_SIGINFO; |
875 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
876 |
|
sigusr2_action.sa_restorer = NULL; |
877 |
+ |
#endif |
878 |
|
if (sigaction(SIGUSR2, &sigusr2_action, NULL) < 0) { |
879 |
|
sprintf(str, GetString(STR_SIGUSR2_INSTALL_ERR), strerror(errno)); |
880 |
|
ErrorAlert(str); |
881 |
|
goto quit; |
882 |
|
} |
883 |
+ |
#endif |
884 |
|
|
885 |
|
// Get my thread ID and execute MacOS thread function |
886 |
|
emul_thread = pthread_self(); |
899 |
|
|
900 |
|
static void Quit(void) |
901 |
|
{ |
902 |
+ |
#if EMULATED_PPC |
903 |
+ |
// Exit PowerPC emulation |
904 |
+ |
exit_emul_ppc(); |
905 |
+ |
#endif |
906 |
+ |
|
907 |
|
// Stop 60Hz thread |
908 |
|
if (tick_thread_active) { |
909 |
|
pthread_cancel(tick_thread); |
917 |
|
} |
918 |
|
|
919 |
|
#if !EMULATED_PPC |
920 |
< |
// Uninstall SIGSEGV handler |
920 |
> |
// Uninstall SIGSEGV and SIGBUS handlers |
921 |
|
sigemptyset(&sigsegv_action.sa_mask); |
922 |
|
sigsegv_action.sa_handler = SIG_DFL; |
923 |
|
sigsegv_action.sa_flags = 0; |
924 |
|
sigaction(SIGSEGV, &sigsegv_action, NULL); |
925 |
+ |
sigaction(SIGBUS, &sigsegv_action, NULL); |
926 |
|
|
927 |
|
// Uninstall SIGILL handler |
928 |
|
sigemptyset(&sigill_action.sa_mask); |
949 |
|
// Exit audio |
950 |
|
AudioExit(); |
951 |
|
|
952 |
+ |
// Exit ADB |
953 |
+ |
ADBExit(); |
954 |
+ |
|
955 |
|
// Exit video |
956 |
|
VideoExit(); |
957 |
|
|
964 |
|
DiskExit(); |
965 |
|
SonyExit(); |
966 |
|
|
967 |
+ |
// Delete thunks |
968 |
+ |
ThunksExit(); |
969 |
+ |
|
970 |
+ |
// Delete SheepShaver globals |
971 |
+ |
SheepMem::Exit(); |
972 |
+ |
|
973 |
|
// Delete RAM area |
974 |
|
if (ram_area_mapped) |
975 |
|
vm_release((char *)RAM_BASE, RAMSize); |
1017 |
|
*/ |
1018 |
|
|
1019 |
|
#if EMULATED_PPC |
849 |
– |
extern void emul_ppc(uint32 start); |
850 |
– |
extern void init_emul_ppc(void); |
1020 |
|
void jump_to_rom(uint32 entry) |
1021 |
|
{ |
1022 |
|
init_emul_ppc(); |
1081 |
|
uint16 proc[2] = {trap, M68K_RTS}; |
1082 |
|
Execute68k((uint32)proc, r); |
1083 |
|
} |
915 |
– |
|
916 |
– |
|
917 |
– |
/* |
918 |
– |
* Execute PPC code from EMUL_OP routine (real mode switch) |
919 |
– |
*/ |
920 |
– |
|
921 |
– |
void ExecutePPC(void (*func)()) |
922 |
– |
{ |
923 |
– |
uint32 tvect[2] = {(uint32)func, 0}; // Fake TVECT |
924 |
– |
RoutineDescriptor desc = BUILD_PPC_ROUTINE_DESCRIPTOR(0, tvect); |
925 |
– |
M68kRegisters r; |
926 |
– |
Execute68k((uint32)&desc, &r); |
927 |
– |
} |
1084 |
|
#endif |
1085 |
|
|
1086 |
|
|
1143 |
|
|
1144 |
|
void MakeExecutable(int dummy, void *start, uint32 length) |
1145 |
|
{ |
1146 |
< |
#if !EMULATED_PPC |
991 |
< |
if (((uint32)start >= ROM_BASE) && ((uint32)start < (ROM_BASE + ROM_SIZE))) |
1146 |
> |
if (((uintptr)start >= ROM_BASE) && ((uintptr)start < (ROM_BASE + ROM_SIZE))) |
1147 |
|
return; |
1148 |
< |
flush_icache_range(start, (void *)((uint32)start + length)); |
1148 |
> |
#if EMULATED_PPC |
1149 |
> |
FlushCodeCache((uintptr)start, (uintptr)start + length); |
1150 |
> |
#else |
1151 |
> |
flush_icache_range(start, (void *)((uintptr)start + length)); |
1152 |
|
#endif |
1153 |
|
} |
1154 |
|
|
1159 |
|
|
1160 |
|
void PatchAfterStartup(void) |
1161 |
|
{ |
1004 |
– |
#if EMULATED_PPC |
1162 |
|
ExecuteNative(NATIVE_VIDEO_INSTALL_ACCEL); |
1006 |
– |
#else |
1007 |
– |
ExecutePPC(VideoInstallAccel); |
1008 |
– |
#endif |
1163 |
|
InstallExtFS(); |
1164 |
|
} |
1165 |
|
|
1204 |
|
if (emul_thread_fatal) { |
1205 |
|
|
1206 |
|
// Yes, dump registers |
1207 |
< |
pt_regs *r = (pt_regs *)&sigsegv_regs; |
1207 |
> |
sigregs *r = &sigsegv_regs; |
1208 |
|
char str[256]; |
1209 |
< |
sprintf(str, "SIGSEGV\n" |
1209 |
> |
if (crash_reason == NULL) |
1210 |
> |
crash_reason = "SIGSEGV"; |
1211 |
> |
sprintf(str, "%s\n" |
1212 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1213 |
|
" xer %08lx cr %08lx \n" |
1214 |
|
" r0 %08lx r1 %08lx r2 %08lx r3 %08lx\n" |
1219 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1220 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1221 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1222 |
+ |
crash_reason, |
1223 |
|
r->nip, r->link, r->ctr, r->msr, |
1224 |
|
r->xer, r->ccr, |
1225 |
|
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1265 |
|
|
1266 |
|
void Set_pthread_attr(pthread_attr_t *attr, int priority) |
1267 |
|
{ |
1268 |
< |
// nothing to do |
1268 |
> |
#ifdef HAVE_PTHREADS |
1269 |
> |
pthread_attr_init(attr); |
1270 |
> |
#if defined(_POSIX_THREAD_PRIORITY_SCHEDULING) |
1271 |
> |
// Some of these only work for superuser |
1272 |
> |
if (geteuid() == 0) { |
1273 |
> |
pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED); |
1274 |
> |
pthread_attr_setschedpolicy(attr, SCHED_FIFO); |
1275 |
> |
struct sched_param fifo_param; |
1276 |
> |
fifo_param.sched_priority = ((sched_get_priority_min(SCHED_FIFO) + |
1277 |
> |
sched_get_priority_max(SCHED_FIFO)) / 2 + |
1278 |
> |
priority); |
1279 |
> |
pthread_attr_setschedparam(attr, &fifo_param); |
1280 |
> |
} |
1281 |
> |
if (pthread_attr_setscope(attr, PTHREAD_SCOPE_SYSTEM) != 0) { |
1282 |
> |
#ifdef PTHREAD_SCOPE_BOUND_NP |
1283 |
> |
// If system scope is not available (eg. we're not running |
1284 |
> |
// with CAP_SCHED_MGT capability on an SGI box), try bound |
1285 |
> |
// scope. It exposes pthread scheduling to the kernel, |
1286 |
> |
// without setting realtime priority. |
1287 |
> |
pthread_attr_setscope(attr, PTHREAD_SCOPE_BOUND_NP); |
1288 |
> |
#endif |
1289 |
> |
} |
1290 |
> |
#endif |
1291 |
> |
#endif |
1292 |
|
} |
1293 |
|
|
1294 |
|
|
1434 |
|
#endif |
1435 |
|
} |
1436 |
|
#else |
1437 |
< |
static void sigusr2_handler(int sig, sigcontext_struct *sc) |
1437 |
> |
static void sigusr2_handler(int sig, siginfo_t *sip, void *scp) |
1438 |
|
{ |
1439 |
< |
pt_regs *r = sc->regs; |
1439 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1440 |
|
|
1441 |
|
// Do nothing if interrupts are disabled |
1442 |
|
if (*(int32 *)XLM_IRQ_NEST > 0) |
1450 |
|
case MODE_68K: |
1451 |
|
// 68k emulator active, trigger 68k interrupt level 1 |
1452 |
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1453 |
< |
r->ccr |= ntohl(kernel_data->v[0x674 >> 2]); |
1453 |
> |
r->cr() |= ntohl(kernel_data->v[0x674 >> 2]); |
1454 |
|
break; |
1455 |
|
|
1456 |
|
#if INTERRUPTS_IN_NATIVE_MODE |
1457 |
|
case MODE_NATIVE: |
1458 |
|
// 68k emulator inactive, in nanokernel? |
1459 |
< |
if (r->gpr[1] != KernelDataAddr) { |
1459 |
> |
if (r->gpr(1) != KernelDataAddr) { |
1460 |
|
// Prepare for 68k interrupt level 1 |
1461 |
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1462 |
|
WriteMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc, ReadMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc) | ntohl(kernel_data->v[0x674 >> 2])); |
1503 |
|
if (InterruptFlags & INTFLAG_VIA) { |
1504 |
|
ClearInterruptFlag(INTFLAG_VIA); |
1505 |
|
ADBInterrupt(); |
1506 |
< |
ExecutePPC(VideoVBL); |
1506 |
> |
ExecuteNative(NATIVE_VIDEO_VBL); |
1507 |
|
} |
1508 |
|
} |
1509 |
|
#endif |
1525 |
|
*/ |
1526 |
|
|
1527 |
|
#if !EMULATED_PPC |
1528 |
< |
static void sigsegv_handler(int sig, sigcontext_struct *sc) |
1528 |
> |
static void sigsegv_handler(int sig, siginfo_t *sip, void *scp) |
1529 |
|
{ |
1530 |
< |
pt_regs *r = sc->regs; |
1530 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1531 |
|
|
1532 |
|
// Get effective address |
1533 |
< |
uint32 addr = r->dar; |
1533 |
> |
uint32 addr = r->dar(); |
1534 |
|
|
1535 |
|
#if ENABLE_VOSF |
1536 |
|
// Handle screen fault. |
1537 |
|
extern bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction); |
1538 |
< |
if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->nip)) |
1538 |
> |
if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->pc())) |
1539 |
|
return; |
1540 |
|
#endif |
1541 |
|
|
1542 |
|
num_segv++; |
1543 |
|
|
1544 |
|
// Fault in Mac ROM or RAM? |
1545 |
< |
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
1545 |
> |
bool mac_fault = (r->pc() >= ROM_BASE) && (r->pc() < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize)); |
1546 |
|
if (mac_fault) { |
1547 |
|
|
1548 |
|
// "VM settings" during MacOS 8 installation |
1549 |
< |
if (r->nip == ROM_BASE + 0x488160 && r->gpr[20] == 0xf8000000) { |
1550 |
< |
r->nip += 4; |
1551 |
< |
r->gpr[8] = 0; |
1549 |
> |
if (r->pc() == ROM_BASE + 0x488160 && r->gpr(20) == 0xf8000000) { |
1550 |
> |
r->pc() += 4; |
1551 |
> |
r->gpr(8) = 0; |
1552 |
|
return; |
1553 |
|
|
1554 |
|
// MacOS 8.5 installation |
1555 |
< |
} else if (r->nip == ROM_BASE + 0x488140 && r->gpr[16] == 0xf8000000) { |
1556 |
< |
r->nip += 4; |
1557 |
< |
r->gpr[8] = 0; |
1555 |
> |
} else if (r->pc() == ROM_BASE + 0x488140 && r->gpr(16) == 0xf8000000) { |
1556 |
> |
r->pc() += 4; |
1557 |
> |
r->gpr(8) = 0; |
1558 |
|
return; |
1559 |
|
|
1560 |
|
// MacOS 8 serial drivers on startup |
1561 |
< |
} else if (r->nip == ROM_BASE + 0x48e080 && (r->gpr[8] == 0xf3012002 || r->gpr[8] == 0xf3012000)) { |
1562 |
< |
r->nip += 4; |
1563 |
< |
r->gpr[8] = 0; |
1561 |
> |
} else if (r->pc() == ROM_BASE + 0x48e080 && (r->gpr(8) == 0xf3012002 || r->gpr(8) == 0xf3012000)) { |
1562 |
> |
r->pc() += 4; |
1563 |
> |
r->gpr(8) = 0; |
1564 |
|
return; |
1565 |
|
|
1566 |
|
// MacOS 8.1 serial drivers on startup |
1567 |
< |
} else if (r->nip == ROM_BASE + 0x48c5e0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1568 |
< |
r->nip += 4; |
1567 |
> |
} else if (r->pc() == ROM_BASE + 0x48c5e0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1568 |
> |
r->pc() += 4; |
1569 |
|
return; |
1570 |
< |
} else if (r->nip == ROM_BASE + 0x4a10a0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1571 |
< |
r->nip += 4; |
1570 |
> |
} else if (r->pc() == ROM_BASE + 0x4a10a0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1571 |
> |
r->pc() += 4; |
1572 |
|
return; |
1573 |
|
} |
1574 |
|
|
1575 |
|
// Get opcode and divide into fields |
1576 |
< |
uint32 opcode = *((uint32 *)r->nip); |
1576 |
> |
uint32 opcode = *((uint32 *)r->pc()); |
1577 |
|
uint32 primop = opcode >> 26; |
1578 |
|
uint32 exop = (opcode >> 1) & 0x3ff; |
1579 |
|
uint32 ra = (opcode >> 16) & 0x1f; |
1662 |
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_NORM; break; |
1663 |
|
case 45: // sthu |
1664 |
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_U; break; |
1665 |
+ |
#if EMULATE_UNALIGNED_LOADSTORE_MULTIPLE |
1666 |
+ |
case 46: // lmw |
1667 |
+ |
if ((addr % 4) != 0) { |
1668 |
+ |
uint32 ea = addr; |
1669 |
+ |
D(bug("WARNING: unaligned lmw to EA=%08x from IP=%08x\n", ea, r->pc())); |
1670 |
+ |
for (int i = rd; i <= 31; i++) { |
1671 |
+ |
r->gpr(i) = ReadMacInt32(ea); |
1672 |
+ |
ea += 4; |
1673 |
+ |
} |
1674 |
+ |
r->pc() += 4; |
1675 |
+ |
goto rti; |
1676 |
+ |
} |
1677 |
+ |
break; |
1678 |
+ |
case 47: // stmw |
1679 |
+ |
if ((addr % 4) != 0) { |
1680 |
+ |
uint32 ea = addr; |
1681 |
+ |
D(bug("WARNING: unaligned stmw to EA=%08x from IP=%08x\n", ea, r->pc())); |
1682 |
+ |
for (int i = rd; i <= 31; i++) { |
1683 |
+ |
WriteMacInt32(ea, r->gpr(i)); |
1684 |
+ |
ea += 4; |
1685 |
+ |
} |
1686 |
+ |
r->pc() += 4; |
1687 |
+ |
goto rti; |
1688 |
+ |
} |
1689 |
+ |
break; |
1690 |
+ |
#endif |
1691 |
|
} |
1692 |
|
|
1693 |
< |
// Ignore ROM writes |
1694 |
< |
if (transfer_type == TYPE_STORE && addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) { |
1695 |
< |
// 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)); |
1693 |
> |
// Ignore ROM writes (including to the zero page, which is read-only) |
1694 |
> |
if (transfer_type == TYPE_STORE && |
1695 |
> |
((addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) || |
1696 |
> |
(addr >= SheepMem::ZeroPage() && addr < SheepMem::ZeroPage() + SheepMem::PageSize()))) { |
1697 |
> |
// 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())); |
1698 |
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1699 |
< |
r->gpr[ra] = addr; |
1700 |
< |
r->nip += 4; |
1699 |
> |
r->gpr(ra) = addr; |
1700 |
> |
r->pc() += 4; |
1701 |
|
goto rti; |
1702 |
|
} |
1703 |
|
|
1704 |
|
// Ignore illegal memory accesses? |
1705 |
|
if (PrefsFindBool("ignoresegv")) { |
1706 |
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1707 |
< |
r->gpr[ra] = addr; |
1707 |
> |
r->gpr(ra) = addr; |
1708 |
|
if (transfer_type == TYPE_LOAD) |
1709 |
< |
r->gpr[rd] = 0; |
1710 |
< |
r->nip += 4; |
1709 |
> |
r->gpr(rd) = 0; |
1710 |
> |
r->pc() += 4; |
1711 |
|
goto rti; |
1712 |
|
} |
1713 |
|
|
1715 |
|
if (!PrefsFindBool("nogui")) { |
1716 |
|
char str[256]; |
1717 |
|
if (transfer_type == TYPE_LOAD || transfer_type == TYPE_STORE) |
1718 |
< |
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]); |
1718 |
> |
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)); |
1719 |
|
else |
1720 |
< |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode); |
1720 |
> |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode); |
1721 |
|
ErrorAlert(str); |
1722 |
|
QuitEmulator(); |
1723 |
|
return; |
1725 |
|
} |
1726 |
|
|
1727 |
|
// For all other errors, jump into debugger (sort of...) |
1728 |
+ |
crash_reason = (sig == SIGBUS) ? "SIGBUS" : "SIGSEGV"; |
1729 |
|
if (!ready_for_signals) { |
1730 |
< |
printf("SIGSEGV\n"); |
1731 |
< |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
1730 |
> |
printf("%s\n"); |
1731 |
> |
printf(" sigcontext %p, machine_regs %p\n", scp, r); |
1732 |
|
printf( |
1733 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1734 |
|
" xer %08lx cr %08lx \n" |
1740 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1741 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1742 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1743 |
< |
r->nip, r->link, r->ctr, r->msr, |
1744 |
< |
r->xer, r->ccr, |
1745 |
< |
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1746 |
< |
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
1747 |
< |
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
1748 |
< |
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
1749 |
< |
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
1750 |
< |
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
1751 |
< |
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
1752 |
< |
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
1743 |
> |
crash_reason, |
1744 |
> |
r->pc(), r->lr(), r->ctr(), r->msr(), |
1745 |
> |
r->xer(), r->cr(), |
1746 |
> |
r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3), |
1747 |
> |
r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7), |
1748 |
> |
r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11), |
1749 |
> |
r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15), |
1750 |
> |
r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19), |
1751 |
> |
r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23), |
1752 |
> |
r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27), |
1753 |
> |
r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31)); |
1754 |
|
exit(1); |
1755 |
|
QuitEmulator(); |
1756 |
|
return; |
1757 |
|
} else { |
1758 |
|
// We crashed. Save registers, tell tick thread and loop forever |
1759 |
< |
sigsegv_regs = *(sigregs *)r; |
1759 |
> |
build_sigregs(&sigsegv_regs, r); |
1760 |
|
emul_thread_fatal = true; |
1761 |
|
for (;;) ; |
1762 |
|
} |
1768 |
|
* SIGILL handler |
1769 |
|
*/ |
1770 |
|
|
1771 |
< |
static void sigill_handler(int sig, sigcontext_struct *sc) |
1771 |
> |
static void sigill_handler(int sig, siginfo_t *sip, void *scp) |
1772 |
|
{ |
1773 |
< |
pt_regs *r = sc->regs; |
1773 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1774 |
|
char str[256]; |
1775 |
|
|
1776 |
|
// Fault in Mac ROM or RAM? |
1777 |
< |
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
1777 |
> |
bool mac_fault = (r->pc() >= ROM_BASE) && (r->pc() < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize)); |
1778 |
|
if (mac_fault) { |
1779 |
|
|
1780 |
|
// Get opcode and divide into fields |
1781 |
< |
uint32 opcode = *((uint32 *)r->nip); |
1781 |
> |
uint32 opcode = *((uint32 *)r->pc()); |
1782 |
|
uint32 primop = opcode >> 26; |
1783 |
|
uint32 exop = (opcode >> 1) & 0x3ff; |
1784 |
|
uint32 ra = (opcode >> 16) & 0x1f; |
1789 |
|
switch (primop) { |
1790 |
|
case 9: // POWER instructions |
1791 |
|
case 22: |
1792 |
< |
power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->nip, r->gpr[1], opcode); |
1792 |
> |
power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->pc(), r->gpr(1), opcode); |
1793 |
|
ErrorAlert(str); |
1794 |
|
QuitEmulator(); |
1795 |
|
return; |
1797 |
|
case 31: |
1798 |
|
switch (exop) { |
1799 |
|
case 83: // mfmsr |
1800 |
< |
r->gpr[rd] = 0xf072; |
1801 |
< |
r->nip += 4; |
1800 |
> |
r->gpr(rd) = 0xf072; |
1801 |
> |
r->pc() += 4; |
1802 |
|
goto rti; |
1803 |
|
|
1804 |
|
case 210: // mtsr |
1805 |
|
case 242: // mtsrin |
1806 |
|
case 306: // tlbie |
1807 |
< |
r->nip += 4; |
1807 |
> |
r->pc() += 4; |
1808 |
|
goto rti; |
1809 |
|
|
1810 |
|
case 339: { // mfspr |
1820 |
|
case 957: // PMC3 |
1821 |
|
case 958: // PMC4 |
1822 |
|
case 959: // SDA |
1823 |
< |
r->nip += 4; |
1823 |
> |
r->pc() += 4; |
1824 |
|
goto rti; |
1825 |
|
case 25: // SDR1 |
1826 |
< |
r->gpr[rd] = 0xdead001f; |
1827 |
< |
r->nip += 4; |
1826 |
> |
r->gpr(rd) = 0xdead001f; |
1827 |
> |
r->pc() += 4; |
1828 |
|
goto rti; |
1829 |
|
case 287: // PVR |
1830 |
< |
r->gpr[rd] = PVR; |
1831 |
< |
r->nip += 4; |
1830 |
> |
r->gpr(rd) = PVR; |
1831 |
> |
r->pc() += 4; |
1832 |
|
goto rti; |
1833 |
|
} |
1834 |
|
break; |
1864 |
|
case 957: // PMC3 |
1865 |
|
case 958: // PMC4 |
1866 |
|
case 959: // SDA |
1867 |
< |
r->nip += 4; |
1867 |
> |
r->pc() += 4; |
1868 |
|
goto rti; |
1869 |
|
} |
1870 |
|
break; |
1883 |
|
|
1884 |
|
// In GUI mode, show error alert |
1885 |
|
if (!PrefsFindBool("nogui")) { |
1886 |
< |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode); |
1886 |
> |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode); |
1887 |
|
ErrorAlert(str); |
1888 |
|
QuitEmulator(); |
1889 |
|
return; |
1891 |
|
} |
1892 |
|
|
1893 |
|
// For all other errors, jump into debugger (sort of...) |
1894 |
+ |
crash_reason = "SIGILL"; |
1895 |
|
if (!ready_for_signals) { |
1896 |
< |
printf("SIGILL\n"); |
1897 |
< |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
1896 |
> |
printf("%s\n"); |
1897 |
> |
printf(" sigcontext %p, machine_regs %p\n", scp, r); |
1898 |
|
printf( |
1899 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1900 |
|
" xer %08lx cr %08lx \n" |
1906 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1907 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1908 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1909 |
< |
r->nip, r->link, r->ctr, r->msr, |
1910 |
< |
r->xer, r->ccr, |
1911 |
< |
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1912 |
< |
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
1913 |
< |
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
1914 |
< |
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
1915 |
< |
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
1916 |
< |
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
1917 |
< |
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
1918 |
< |
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
1909 |
> |
crash_reason, |
1910 |
> |
r->pc(), r->lr(), r->ctr(), r->msr(), |
1911 |
> |
r->xer(), r->cr(), |
1912 |
> |
r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3), |
1913 |
> |
r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7), |
1914 |
> |
r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11), |
1915 |
> |
r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15), |
1916 |
> |
r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19), |
1917 |
> |
r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23), |
1918 |
> |
r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27), |
1919 |
> |
r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31)); |
1920 |
|
exit(1); |
1921 |
|
QuitEmulator(); |
1922 |
|
return; |
1923 |
|
} else { |
1924 |
|
// We crashed. Save registers, tell tick thread and loop forever |
1925 |
< |
sigsegv_regs = *(sigregs *)r; |
1925 |
> |
build_sigregs(&sigsegv_regs, r); |
1926 |
|
emul_thread_fatal = true; |
1927 |
|
for (;;) ; |
1928 |
|
} |
1932 |
|
|
1933 |
|
|
1934 |
|
/* |
1935 |
+ |
* Helpers to share 32-bit addressable data with MacOS |
1936 |
+ |
*/ |
1937 |
+ |
|
1938 |
+ |
bool SheepMem::Init(void) |
1939 |
+ |
{ |
1940 |
+ |
// Size of a native page |
1941 |
+ |
page_size = getpagesize(); |
1942 |
+ |
|
1943 |
+ |
// Allocate SheepShaver globals |
1944 |
+ |
if (vm_acquire_fixed((char *)base, size) < 0) |
1945 |
+ |
return false; |
1946 |
+ |
|
1947 |
+ |
// Allocate page with all bits set to 0 |
1948 |
+ |
zero_page = base + size; |
1949 |
+ |
if (vm_acquire_fixed((char *)zero_page, page_size) < 0) |
1950 |
+ |
return false; |
1951 |
+ |
memset((char *)zero_page, 0, page_size); |
1952 |
+ |
if (vm_protect((char *)zero_page, page_size, VM_PAGE_READ) < 0) |
1953 |
+ |
return false; |
1954 |
+ |
|
1955 |
+ |
#if EMULATED_PPC |
1956 |
+ |
// Allocate alternate stack for PowerPC interrupt routine |
1957 |
+ |
sig_stack = zero_page + page_size; |
1958 |
+ |
if (vm_acquire_fixed((char *)sig_stack, SIG_STACK_SIZE) < 0) |
1959 |
+ |
return false; |
1960 |
+ |
#endif |
1961 |
+ |
|
1962 |
+ |
top = base + size; |
1963 |
+ |
return true; |
1964 |
+ |
} |
1965 |
+ |
|
1966 |
+ |
void SheepMem::Exit(void) |
1967 |
+ |
{ |
1968 |
+ |
if (top) { |
1969 |
+ |
// Delete SheepShaver globals |
1970 |
+ |
vm_release((void *)base, size); |
1971 |
+ |
|
1972 |
+ |
// Delete zero page |
1973 |
+ |
vm_release((void *)zero_page, page_size); |
1974 |
+ |
|
1975 |
+ |
#if EMULATED_PPC |
1976 |
+ |
// Delete alternate stack for PowerPC interrupt routine |
1977 |
+ |
vm_release((void *)sig_stack, SIG_STACK_SIZE); |
1978 |
+ |
#endif |
1979 |
+ |
} |
1980 |
+ |
} |
1981 |
+ |
|
1982 |
+ |
|
1983 |
+ |
/* |
1984 |
|
* Display alert |
1985 |
|
*/ |
1986 |
|
|