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 |
108 |
|
#include "rom_patches.h" |
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 ROM_AREA_SIZE = 0x500000; // Size of ROM area |
148 |
< |
const uint32 ROM_END = ROM_BASE + ROM_SIZE; // End of ROM |
149 |
< |
|
150 |
< |
const uint32 KERNEL_DATA_BASE = 0x68ffe000; // Address of Kernel Data |
151 |
< |
const uint32 KERNEL_DATA2_BASE = 0x5fffe000; // Alternate address of Kernel Data |
152 |
< |
const uint32 KERNEL_AREA_SIZE = 0x2000; // Size of Kernel Data area |
153 |
< |
|
152 |
> |
const uintptr RAM_BASE = 0x20000000; // Base address of RAM |
153 |
|
const uint32 SIG_STACK_SIZE = 0x10000; // Size of signal stack |
154 |
|
|
155 |
|
|
156 |
< |
// 68k Emulator Data |
157 |
< |
struct EmulatorData { |
158 |
< |
uint32 v[0x400]; |
156 |
> |
#if !EMULATED_PPC |
157 |
> |
struct sigregs { |
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 |
< |
|
168 |
< |
// Kernel Data |
169 |
< |
struct KernelData { |
170 |
< |
uint32 v[0x400]; |
171 |
< |
EmulatorData ed; |
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 |
< |
|
188 |
< |
#if !EMULATED_PPC |
189 |
< |
// Structure in which registers are saved in a signal handler; |
190 |
< |
// sigcontext->regs points to it |
191 |
< |
// (see arch/ppc/kernel/signal.c) |
192 |
< |
typedef struct { |
193 |
< |
uint32 u[4]; |
194 |
< |
} __attribute((aligned(16))) vector128; |
195 |
< |
#include <linux/elf.h> |
196 |
< |
|
197 |
< |
struct sigregs { |
198 |
< |
elf_gregset_t gp_regs; // Identical to pt_regs |
199 |
< |
double fp_regs[ELF_NFPREG]; // f0..f31 and fpsrc |
200 |
< |
//more (uninteresting) stuff following here |
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 |
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 |
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 |
248 |
|
static bool ram_area_mapped = false; // Flag: Mac RAM mmap()ped |
209 |
– |
static void *mmap_RAMBase = NULL; // Base address of mmap()ed RAM area |
249 |
|
static KernelData *kernel_data; // Pointer to Kernel Data |
250 |
|
static EmulatorData *emulator_data; |
251 |
|
|
260 |
|
static bool ready_for_signals = false; // Handler installed, signals can be sent |
261 |
|
static int64 num_segv = 0; // Number of handled SEGV signals |
262 |
|
|
224 |
– |
#if !EMULATED_PPC |
263 |
|
static struct sigaction sigusr2_action; // Interrupt signal (of emulator thread) |
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 |
+ |
uintptr SheepMem::zero_page = 0; // Address of ro page filled in with zeros |
277 |
+ |
uintptr SheepMem::base = 0x60000000; // Address of SheepShaver data |
278 |
+ |
uintptr SheepMem::top = 0; // Top of SheepShaver data (stack like storage) |
279 |
+ |
|
280 |
|
|
281 |
|
// Prototypes |
282 |
|
static void Quit(void); |
283 |
|
static void *emul_func(void *arg); |
284 |
|
static void *nvram_func(void *arg); |
285 |
|
static void *tick_func(void *arg); |
286 |
< |
#if !EMULATED_PPC |
287 |
< |
static void sigusr2_handler(int sig, sigcontext_struct *sc); |
288 |
< |
static void sigsegv_handler(int sig, sigcontext_struct *sc); |
289 |
< |
static void sigill_handler(int sig, sigcontext_struct *sc); |
286 |
> |
#if EMULATED_PPC |
287 |
> |
static void sigusr2_handler(int sig); |
288 |
> |
extern void emul_ppc(uint32 start); |
289 |
> |
extern void init_emul_ppc(void); |
290 |
> |
extern void exit_emul_ppc(void); |
291 |
> |
#else |
292 |
> |
static void sigusr2_handler(int sig, siginfo_t *sip, void *scp); |
293 |
> |
static void sigsegv_handler(int sig, siginfo_t *sip, void *scp); |
294 |
> |
static void sigill_handler(int sig, siginfo_t *sip, void *scp); |
295 |
|
#endif |
296 |
|
|
297 |
|
|
298 |
|
// From asm_linux.S |
299 |
< |
#if EMULATED_PPC |
249 |
< |
extern int atomic_add(int *var, int v); |
250 |
< |
extern int atomic_and(int *var, int v); |
251 |
< |
extern int atomic_or(int *var, int v); |
252 |
< |
#else |
299 |
> |
#if !EMULATED_PPC |
300 |
|
extern "C" void *get_toc(void); |
301 |
|
extern "C" void *get_sp(void); |
302 |
|
extern "C" void flush_icache_range(void *start, void *end); |
311 |
|
#endif |
312 |
|
|
313 |
|
|
314 |
+ |
#if EMULATED_PPC |
315 |
+ |
/* |
316 |
+ |
* Return signal stack base |
317 |
+ |
*/ |
318 |
+ |
|
319 |
+ |
uintptr SignalStackBase(void) |
320 |
+ |
{ |
321 |
+ |
return sig_stack + SIG_STACK_SIZE; |
322 |
+ |
} |
323 |
+ |
|
324 |
+ |
|
325 |
+ |
/* |
326 |
+ |
* Atomic operations |
327 |
+ |
*/ |
328 |
+ |
|
329 |
+ |
#if HAVE_SPINLOCKS |
330 |
+ |
static spinlock_t atomic_ops_lock = SPIN_LOCK_UNLOCKED; |
331 |
+ |
#else |
332 |
+ |
#define spin_lock(LOCK) |
333 |
+ |
#define spin_unlock(LOCK) |
334 |
+ |
#endif |
335 |
+ |
|
336 |
+ |
int atomic_add(int *var, int v) |
337 |
+ |
{ |
338 |
+ |
spin_lock(&atomic_ops_lock); |
339 |
+ |
int ret = *var; |
340 |
+ |
*var += v; |
341 |
+ |
spin_unlock(&atomic_ops_lock); |
342 |
+ |
return ret; |
343 |
+ |
} |
344 |
+ |
|
345 |
+ |
int atomic_and(int *var, int v) |
346 |
+ |
{ |
347 |
+ |
spin_lock(&atomic_ops_lock); |
348 |
+ |
int ret = *var; |
349 |
+ |
*var &= v; |
350 |
+ |
spin_unlock(&atomic_ops_lock); |
351 |
+ |
return ret; |
352 |
+ |
} |
353 |
+ |
|
354 |
+ |
int atomic_or(int *var, int v) |
355 |
+ |
{ |
356 |
+ |
spin_lock(&atomic_ops_lock); |
357 |
+ |
int ret = *var; |
358 |
+ |
*var |= v; |
359 |
+ |
spin_unlock(&atomic_ops_lock); |
360 |
+ |
return ret; |
361 |
+ |
} |
362 |
+ |
#endif |
363 |
+ |
|
364 |
+ |
|
365 |
|
/* |
366 |
|
* Main program |
367 |
|
*/ |
380 |
|
char str[256]; |
381 |
|
uint32 *boot_globs; |
382 |
|
int16 i16; |
285 |
– |
int drive, driver; |
383 |
|
int rom_fd; |
384 |
|
FILE *proc_file; |
385 |
|
const char *rom_path; |
389 |
|
|
390 |
|
// Initialize variables |
391 |
|
RAMBase = 0; |
295 |
– |
mmap_RAMBase = NULL; |
392 |
|
tzset(); |
393 |
|
|
394 |
|
// Print some info |
516 |
|
goto quit; |
517 |
|
} |
518 |
|
|
519 |
+ |
#ifndef PAGEZERO_HACK |
520 |
|
// Create Low Memory area (0x0000..0x3000) |
521 |
|
if (vm_acquire_fixed((char *)0, 0x3000) < 0) { |
522 |
|
sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno)); |
524 |
|
goto quit; |
525 |
|
} |
526 |
|
lm_area_mapped = true; |
527 |
+ |
#endif |
528 |
|
|
529 |
|
// Create areas for Kernel Data |
530 |
|
kernel_area = shmget(IPC_PRIVATE, KERNEL_AREA_SIZE, 0600); |
543 |
|
ErrorAlert(str); |
544 |
|
goto quit; |
545 |
|
} |
546 |
< |
kernel_data = (KernelData *)0x68ffe000; |
546 |
> |
kernel_data = (KernelData *)KERNEL_DATA_BASE; |
547 |
|
emulator_data = &kernel_data->ed; |
548 |
< |
KernelDataAddr = (uint32)kernel_data; |
548 |
> |
KernelDataAddr = KERNEL_DATA_BASE; |
549 |
|
D(bug("Kernel Data at %p, Emulator Data at %p\n", kernel_data, emulator_data)); |
550 |
|
|
551 |
+ |
// Create area for SheepShaver data |
552 |
+ |
if (!SheepMem::Init()) { |
553 |
+ |
sprintf(str, GetString(STR_SHEEP_MEM_MMAP_ERR), strerror(errno)); |
554 |
+ |
ErrorAlert(str); |
555 |
+ |
goto quit; |
556 |
+ |
} |
557 |
+ |
|
558 |
|
// Create area for Mac ROM |
559 |
|
if (vm_acquire_fixed((char *)ROM_BASE, ROM_AREA_SIZE) < 0) { |
560 |
|
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
578 |
|
RAMSize = 8*1024*1024; |
579 |
|
} |
580 |
|
|
581 |
< |
mmap_RAMBase = (void *)0x20000000; |
477 |
< |
if (vm_acquire_fixed(mmap_RAMBase, RAMSize) < 0) { |
581 |
> |
if (vm_acquire_fixed((char *)RAM_BASE, RAMSize) < 0) { |
582 |
|
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
583 |
|
ErrorAlert(str); |
584 |
|
goto quit; |
585 |
|
} |
586 |
|
#if !EMULATED_PPC |
587 |
< |
if (vm_protect(mmap_RAMBase, RAMSize, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
587 |
> |
if (vm_protect((char *)RAM_BASE, RAMSize, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
588 |
|
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
589 |
|
ErrorAlert(str); |
590 |
|
goto quit; |
591 |
|
} |
592 |
|
#endif |
593 |
< |
RAMBase = (uint32)mmap_RAMBase; |
593 |
> |
RAMBase = RAM_BASE; |
594 |
|
ram_area_mapped = true; |
595 |
|
D(bug("RAM area at %08x\n", RAMBase)); |
596 |
|
|
632 |
|
XPRAMInit(); |
633 |
|
|
634 |
|
// Set boot volume |
635 |
< |
drive = PrefsFindInt32("bootdrive"); |
635 |
> |
i16 = PrefsFindInt32("bootdrive"); |
636 |
|
XPRAM[0x1378] = i16 >> 8; |
637 |
|
XPRAM[0x1379] = i16 & 0xff; |
638 |
< |
driver = PrefsFindInt32("bootdriver"); |
638 |
> |
i16 = PrefsFindInt32("bootdriver"); |
639 |
|
XPRAM[0x137a] = i16 >> 8; |
640 |
|
XPRAM[0x137b] = i16 & 0xff; |
641 |
|
|
648 |
|
boot_globs[1] = htonl(RAMSize); |
649 |
|
boot_globs[2] = htonl((uint32)-1); // End of bank table |
650 |
|
|
651 |
+ |
// Init thunks |
652 |
+ |
if (!ThunksInit()) |
653 |
+ |
goto quit; |
654 |
+ |
|
655 |
|
// Init drivers |
656 |
|
SonyInit(); |
657 |
|
DiskInit(); |
661 |
|
// Init external file system |
662 |
|
ExtFSInit(); |
663 |
|
|
664 |
+ |
// Init ADB |
665 |
+ |
ADBInit(); |
666 |
+ |
|
667 |
|
// Init audio |
668 |
|
AudioInit(); |
669 |
|
|
698 |
|
// Initialize Kernel Data |
699 |
|
memset(kernel_data, 0, sizeof(KernelData)); |
700 |
|
if (ROMType == ROMTYPE_NEWWORLD) { |
701 |
< |
static uint32 of_dev_tree[4] = {0, 0, 0, 0}; |
702 |
< |
static uint8 vector_lookup_tbl[128]; |
703 |
< |
static uint8 vector_mask_tbl[64]; |
701 |
> |
uintptr of_dev_tree = SheepMem::Reserve(4 * sizeof(uint32)); |
702 |
> |
memset((void *)of_dev_tree, 0, 4 * sizeof(uint32)); |
703 |
> |
uintptr vector_lookup_tbl = SheepMem::Reserve(128); |
704 |
> |
uintptr vector_mask_tbl = SheepMem::Reserve(64); |
705 |
|
memset((uint8 *)kernel_data + 0xb80, 0x3d, 0x80); |
706 |
< |
memset(vector_lookup_tbl, 0, 128); |
707 |
< |
memset(vector_mask_tbl, 0, 64); |
706 |
> |
memset((void *)vector_lookup_tbl, 0, 128); |
707 |
> |
memset((void *)vector_mask_tbl, 0, 64); |
708 |
|
kernel_data->v[0xb80 >> 2] = htonl(ROM_BASE); |
709 |
< |
kernel_data->v[0xb84 >> 2] = htonl((uint32)of_dev_tree); // OF device tree base |
710 |
< |
kernel_data->v[0xb90 >> 2] = htonl((uint32)vector_lookup_tbl); |
711 |
< |
kernel_data->v[0xb94 >> 2] = htonl((uint32)vector_mask_tbl); |
709 |
> |
kernel_data->v[0xb84 >> 2] = htonl(of_dev_tree); // OF device tree base |
710 |
> |
kernel_data->v[0xb90 >> 2] = htonl(vector_lookup_tbl); |
711 |
> |
kernel_data->v[0xb94 >> 2] = htonl(vector_mask_tbl); |
712 |
|
kernel_data->v[0xb98 >> 2] = htonl(ROM_BASE); // OpenPIC base |
713 |
|
kernel_data->v[0xbb0 >> 2] = htonl(0); // ADB base |
714 |
|
kernel_data->v[0xc20 >> 2] = htonl(RAMSize); |
744 |
|
D(bug("Initializing Low Memory...\n")); |
745 |
|
memset(NULL, 0, 0x3000); |
746 |
|
WriteMacInt32(XLM_SIGNATURE, FOURCC('B','a','a','h')); // Signature to detect SheepShaver |
747 |
< |
WriteMacInt32(XLM_KERNEL_DATA, (uint32)kernel_data); // For trap replacement routines |
747 |
> |
WriteMacInt32(XLM_KERNEL_DATA, KernelDataAddr); // For trap replacement routines |
748 |
|
WriteMacInt32(XLM_PVR, PVR); // Theoretical PVR |
749 |
|
WriteMacInt32(XLM_BUS_CLOCK, BusClockSpeed); // For DriverServicesLib patch |
750 |
|
WriteMacInt16(XLM_EXEC_RETURN_OPCODE, M68K_EXEC_RETURN); // For Execute68k() (RTS from the executed 68k code will jump here and end 68k mode) |
751 |
+ |
WriteMacInt32(XLM_ZERO_PAGE, SheepMem::ZeroPage()); // Pointer to read-only page with all bits set to 0 |
752 |
|
#if !EMULATED_PPC |
753 |
< |
WriteMacInt32(XLM_TOC, (uint32)TOC); // TOC pointer of emulator |
641 |
< |
WriteMacInt32(XLM_ETHER_INIT, (uint32)InitStreamModule); // DLPI ethernet driver functions |
642 |
< |
WriteMacInt32(XLM_ETHER_TERM, (uint32)TerminateStreamModule); |
643 |
< |
WriteMacInt32(XLM_ETHER_OPEN, (uint32)ether_open); |
644 |
< |
WriteMacInt32(XLM_ETHER_CLOSE, (uint32)ether_close); |
645 |
< |
WriteMacInt32(XLM_ETHER_WPUT, (uint32)ether_wput); |
646 |
< |
WriteMacInt32(XLM_ETHER_RSRV, (uint32)ether_rsrv); |
647 |
< |
WriteMacInt32(XLM_VIDEO_DOIO, (uint32)VideoDoDriverIO); |
753 |
> |
WriteMacInt32(XLM_TOC, (uint32)TOC); // TOC pointer of emulator |
754 |
|
#endif |
755 |
+ |
WriteMacInt32(XLM_ETHER_INIT, NativeFunction(NATIVE_ETHER_INIT)); // DLPI ethernet driver functions |
756 |
+ |
WriteMacInt32(XLM_ETHER_TERM, NativeFunction(NATIVE_ETHER_TERM)); |
757 |
+ |
WriteMacInt32(XLM_ETHER_OPEN, NativeFunction(NATIVE_ETHER_OPEN)); |
758 |
+ |
WriteMacInt32(XLM_ETHER_CLOSE, NativeFunction(NATIVE_ETHER_CLOSE)); |
759 |
+ |
WriteMacInt32(XLM_ETHER_WPUT, NativeFunction(NATIVE_ETHER_WPUT)); |
760 |
+ |
WriteMacInt32(XLM_ETHER_RSRV, NativeFunction(NATIVE_ETHER_RSRV)); |
761 |
+ |
WriteMacInt32(XLM_VIDEO_DOIO, NativeFunction(NATIVE_VIDEO_DO_DRIVER_IO)); |
762 |
|
D(bug("Low Memory initialized\n")); |
763 |
|
|
764 |
|
// Start 60Hz thread |
796 |
|
#endif |
797 |
|
|
798 |
|
#if !EMULATED_PPC |
799 |
< |
// Install SIGSEGV handler |
799 |
> |
// Install SIGSEGV and SIGBUS handlers |
800 |
|
sigemptyset(&sigsegv_action.sa_mask); // Block interrupts during SEGV handling |
801 |
|
sigaddset(&sigsegv_action.sa_mask, SIGUSR2); |
802 |
< |
sigsegv_action.sa_handler = (__sighandler_t)sigsegv_handler; |
803 |
< |
sigsegv_action.sa_flags = SA_ONSTACK; |
802 |
> |
sigsegv_action.sa_sigaction = sigsegv_handler; |
803 |
> |
sigsegv_action.sa_flags = SA_ONSTACK | SA_SIGINFO; |
804 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
805 |
|
sigsegv_action.sa_restorer = NULL; |
806 |
+ |
#endif |
807 |
|
if (sigaction(SIGSEGV, &sigsegv_action, NULL) < 0) { |
808 |
|
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
809 |
|
ErrorAlert(str); |
810 |
|
goto quit; |
811 |
|
} |
812 |
+ |
if (sigaction(SIGBUS, &sigsegv_action, NULL) < 0) { |
813 |
+ |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
814 |
+ |
ErrorAlert(str); |
815 |
+ |
goto quit; |
816 |
+ |
} |
817 |
|
|
818 |
|
// Install SIGILL handler |
819 |
|
sigemptyset(&sigill_action.sa_mask); // Block interrupts during ILL handling |
820 |
|
sigaddset(&sigill_action.sa_mask, SIGUSR2); |
821 |
< |
sigill_action.sa_handler = (__sighandler_t)sigill_handler; |
822 |
< |
sigill_action.sa_flags = SA_ONSTACK; |
821 |
> |
sigill_action.sa_sigaction = sigill_handler; |
822 |
> |
sigill_action.sa_flags = SA_ONSTACK | SA_SIGINFO; |
823 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
824 |
|
sigill_action.sa_restorer = NULL; |
825 |
+ |
#endif |
826 |
|
if (sigaction(SIGILL, &sigill_action, NULL) < 0) { |
827 |
|
sprintf(str, GetString(STR_SIGILL_INSTALL_ERR), strerror(errno)); |
828 |
|
ErrorAlert(str); |
829 |
|
goto quit; |
830 |
|
} |
831 |
+ |
#endif |
832 |
|
|
833 |
+ |
#if !EMULATED_PPC |
834 |
|
// Install interrupt signal handler |
835 |
|
sigemptyset(&sigusr2_action.sa_mask); |
836 |
< |
sigusr2_action.sa_handler = (__sighandler_t)sigusr2_handler; |
837 |
< |
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART; |
836 |
> |
sigusr2_action.sa_sigaction = sigusr2_handler; |
837 |
> |
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART | SA_SIGINFO; |
838 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
839 |
|
sigusr2_action.sa_restorer = NULL; |
840 |
+ |
#endif |
841 |
|
if (sigaction(SIGUSR2, &sigusr2_action, NULL) < 0) { |
842 |
|
sprintf(str, GetString(STR_SIGUSR2_INSTALL_ERR), strerror(errno)); |
843 |
|
ErrorAlert(str); |
862 |
|
|
863 |
|
static void Quit(void) |
864 |
|
{ |
865 |
+ |
#if EMULATED_PPC |
866 |
+ |
// Exit PowerPC emulation |
867 |
+ |
exit_emul_ppc(); |
868 |
+ |
#endif |
869 |
+ |
|
870 |
|
// Stop 60Hz thread |
871 |
|
if (tick_thread_active) { |
872 |
|
pthread_cancel(tick_thread); |
880 |
|
} |
881 |
|
|
882 |
|
#if !EMULATED_PPC |
883 |
< |
// Uninstall SIGSEGV handler |
883 |
> |
// Uninstall SIGSEGV and SIGBUS handlers |
884 |
|
sigemptyset(&sigsegv_action.sa_mask); |
885 |
|
sigsegv_action.sa_handler = SIG_DFL; |
886 |
|
sigsegv_action.sa_flags = 0; |
887 |
|
sigaction(SIGSEGV, &sigsegv_action, NULL); |
888 |
+ |
sigaction(SIGBUS, &sigsegv_action, NULL); |
889 |
|
|
890 |
|
// Uninstall SIGILL handler |
891 |
|
sigemptyset(&sigill_action.sa_mask); |
912 |
|
// Exit audio |
913 |
|
AudioExit(); |
914 |
|
|
915 |
+ |
// Exit ADB |
916 |
+ |
ADBExit(); |
917 |
+ |
|
918 |
|
// Exit video |
919 |
|
VideoExit(); |
920 |
|
|
927 |
|
DiskExit(); |
928 |
|
SonyExit(); |
929 |
|
|
930 |
+ |
// Delete thunks |
931 |
+ |
ThunksExit(); |
932 |
+ |
|
933 |
+ |
// Delete SheepShaver globals |
934 |
+ |
SheepMem::Exit(); |
935 |
+ |
|
936 |
|
// Delete RAM area |
937 |
|
if (ram_area_mapped) |
938 |
< |
vm_release(mmap_RAMBase, RAMSize); |
938 |
> |
vm_release((char *)RAM_BASE, RAMSize); |
939 |
|
|
940 |
|
// Delete ROM area |
941 |
|
if (rom_area_mapped) |
980 |
|
*/ |
981 |
|
|
982 |
|
#if EMULATED_PPC |
842 |
– |
extern void emul_ppc(uint32 start); |
843 |
– |
extern void init_emul_ppc(void); |
983 |
|
void jump_to_rom(uint32 entry) |
984 |
|
{ |
985 |
|
init_emul_ppc(); |
1048 |
|
|
1049 |
|
|
1050 |
|
/* |
912 |
– |
* Execute PPC code from EMUL_OP routine (real mode switch) |
913 |
– |
*/ |
914 |
– |
|
915 |
– |
void ExecutePPC(void (*func)()) |
916 |
– |
{ |
917 |
– |
uint32 tvect[2] = {(uint32)func, 0}; // Fake TVECT |
918 |
– |
RoutineDescriptor desc = BUILD_PPC_ROUTINE_DESCRIPTOR(0, tvect); |
919 |
– |
M68kRegisters r; |
920 |
– |
Execute68k((uint32)&desc, &r); |
921 |
– |
} |
922 |
– |
|
923 |
– |
|
924 |
– |
/* |
1051 |
|
* Quit emulator (cause return from jump_to_rom) |
1052 |
|
*/ |
1053 |
|
|
1106 |
|
|
1107 |
|
void MakeExecutable(int dummy, void *start, uint32 length) |
1108 |
|
{ |
1109 |
< |
#if !EMULATED_PPC |
984 |
< |
if (((uint32)start >= ROM_BASE) && ((uint32)start < (ROM_BASE + ROM_SIZE))) |
1109 |
> |
if (((uintptr)start >= ROM_BASE) && ((uintptr)start < (ROM_BASE + ROM_SIZE))) |
1110 |
|
return; |
1111 |
< |
flush_icache_range(start, (void *)((uint32)start + length)); |
1111 |
> |
#if EMULATED_PPC |
1112 |
> |
FlushCodeCache((uintptr)start, (uintptr)start + length); |
1113 |
> |
#else |
1114 |
> |
flush_icache_range(start, (void *)((uintptr)start + length)); |
1115 |
|
#endif |
1116 |
|
} |
1117 |
|
|
1122 |
|
|
1123 |
|
void PatchAfterStartup(void) |
1124 |
|
{ |
1125 |
< |
ExecutePPC(VideoInstallAccel); |
1125 |
> |
ExecuteNative(NATIVE_VIDEO_INSTALL_ACCEL); |
1126 |
|
InstallExtFS(); |
1127 |
|
} |
1128 |
|
|
1167 |
|
if (emul_thread_fatal) { |
1168 |
|
|
1169 |
|
// Yes, dump registers |
1170 |
< |
pt_regs *r = (pt_regs *)&sigsegv_regs; |
1170 |
> |
sigregs *r = &sigsegv_regs; |
1171 |
|
char str[256]; |
1172 |
< |
sprintf(str, "SIGSEGV\n" |
1172 |
> |
if (crash_reason == NULL) |
1173 |
> |
crash_reason = "SIGSEGV"; |
1174 |
> |
sprintf(str, "%s\n" |
1175 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1176 |
|
" xer %08lx cr %08lx \n" |
1177 |
|
" r0 %08lx r1 %08lx r2 %08lx r3 %08lx\n" |
1182 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1183 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1184 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1185 |
+ |
crash_reason, |
1186 |
|
r->nip, r->link, r->ctr, r->msr, |
1187 |
|
r->xer, r->ccr, |
1188 |
|
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1228 |
|
|
1229 |
|
void Set_pthread_attr(pthread_attr_t *attr, int priority) |
1230 |
|
{ |
1231 |
< |
// nothing to do |
1231 |
> |
#ifdef HAVE_PTHREADS |
1232 |
> |
pthread_attr_init(attr); |
1233 |
> |
#if defined(_POSIX_THREAD_PRIORITY_SCHEDULING) |
1234 |
> |
// Some of these only work for superuser |
1235 |
> |
if (geteuid() == 0) { |
1236 |
> |
pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED); |
1237 |
> |
pthread_attr_setschedpolicy(attr, SCHED_FIFO); |
1238 |
> |
struct sched_param fifo_param; |
1239 |
> |
fifo_param.sched_priority = ((sched_get_priority_min(SCHED_FIFO) + |
1240 |
> |
sched_get_priority_max(SCHED_FIFO)) / 2 + |
1241 |
> |
priority); |
1242 |
> |
pthread_attr_setschedparam(attr, &fifo_param); |
1243 |
> |
} |
1244 |
> |
if (pthread_attr_setscope(attr, PTHREAD_SCOPE_SYSTEM) != 0) { |
1245 |
> |
#ifdef PTHREAD_SCOPE_BOUND_NP |
1246 |
> |
// If system scope is not available (eg. we're not running |
1247 |
> |
// with CAP_SCHED_MGT capability on an SGI box), try bound |
1248 |
> |
// scope. It exposes pthread scheduling to the kernel, |
1249 |
> |
// without setting realtime priority. |
1250 |
> |
pthread_attr_setscope(attr, PTHREAD_SCOPE_BOUND_NP); |
1251 |
> |
#endif |
1252 |
> |
} |
1253 |
> |
#endif |
1254 |
> |
#endif |
1255 |
|
} |
1256 |
|
|
1257 |
|
|
1259 |
|
* Mutexes |
1260 |
|
*/ |
1261 |
|
|
1262 |
+ |
#ifdef HAVE_PTHREADS |
1263 |
+ |
|
1264 |
+ |
struct B2_mutex { |
1265 |
+ |
B2_mutex() { |
1266 |
+ |
pthread_mutexattr_t attr; |
1267 |
+ |
pthread_mutexattr_init(&attr); |
1268 |
+ |
// Initialize the mutex for priority inheritance -- |
1269 |
+ |
// required for accurate timing. |
1270 |
+ |
#ifdef HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL |
1271 |
+ |
pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT); |
1272 |
+ |
#endif |
1273 |
+ |
#if defined(HAVE_PTHREAD_MUTEXATTR_SETTYPE) && defined(PTHREAD_MUTEX_NORMAL) |
1274 |
+ |
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); |
1275 |
+ |
#endif |
1276 |
+ |
#ifdef HAVE_PTHREAD_MUTEXATTR_SETPSHARED |
1277 |
+ |
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE); |
1278 |
+ |
#endif |
1279 |
+ |
pthread_mutex_init(&m, &attr); |
1280 |
+ |
pthread_mutexattr_destroy(&attr); |
1281 |
+ |
} |
1282 |
+ |
~B2_mutex() { |
1283 |
+ |
pthread_mutex_trylock(&m); // Make sure it's locked before |
1284 |
+ |
pthread_mutex_unlock(&m); // unlocking it. |
1285 |
+ |
pthread_mutex_destroy(&m); |
1286 |
+ |
} |
1287 |
+ |
pthread_mutex_t m; |
1288 |
+ |
}; |
1289 |
+ |
|
1290 |
+ |
B2_mutex *B2_create_mutex(void) |
1291 |
+ |
{ |
1292 |
+ |
return new B2_mutex; |
1293 |
+ |
} |
1294 |
+ |
|
1295 |
+ |
void B2_lock_mutex(B2_mutex *mutex) |
1296 |
+ |
{ |
1297 |
+ |
pthread_mutex_lock(&mutex->m); |
1298 |
+ |
} |
1299 |
+ |
|
1300 |
+ |
void B2_unlock_mutex(B2_mutex *mutex) |
1301 |
+ |
{ |
1302 |
+ |
pthread_mutex_unlock(&mutex->m); |
1303 |
+ |
} |
1304 |
+ |
|
1305 |
+ |
void B2_delete_mutex(B2_mutex *mutex) |
1306 |
+ |
{ |
1307 |
+ |
delete mutex; |
1308 |
+ |
} |
1309 |
+ |
|
1310 |
+ |
#else |
1311 |
+ |
|
1312 |
|
struct B2_mutex { |
1313 |
|
int dummy; |
1314 |
|
}; |
1331 |
|
delete mutex; |
1332 |
|
} |
1333 |
|
|
1334 |
+ |
#endif |
1335 |
+ |
|
1336 |
|
|
1337 |
|
/* |
1338 |
|
* Trigger signal USR2 from another thread |
1339 |
|
*/ |
1340 |
|
|
1341 |
+ |
#if !EMULATED_PPC || ASYNC_IRQ |
1342 |
|
void TriggerInterrupt(void) |
1343 |
|
{ |
1137 |
– |
#if EMULATED_PPC |
1138 |
– |
WriteMacInt32(0x16a, ReadMacInt32(0x16a) + 1); |
1139 |
– |
#else |
1140 |
– |
#if 0 |
1141 |
– |
WriteMacInt32(0x16a, ReadMacInt32(0x16a) + 1); |
1142 |
– |
#else |
1344 |
|
if (ready_for_signals) |
1345 |
|
pthread_kill(emul_thread, SIGUSR2); |
1145 |
– |
#endif |
1146 |
– |
#endif |
1346 |
|
} |
1347 |
+ |
#endif |
1348 |
|
|
1349 |
|
|
1350 |
|
/* |
1384 |
|
} |
1385 |
|
|
1386 |
|
|
1187 |
– |
#if !EMULATED_PPC |
1387 |
|
/* |
1388 |
|
* USR2 handler |
1389 |
|
*/ |
1390 |
|
|
1391 |
< |
static void sigusr2_handler(int sig, sigcontext_struct *sc) |
1391 |
> |
#if EMULATED_PPC |
1392 |
> |
static void sigusr2_handler(int sig) |
1393 |
> |
{ |
1394 |
> |
#if ASYNC_IRQ |
1395 |
> |
extern void HandleInterrupt(void); |
1396 |
> |
HandleInterrupt(); |
1397 |
> |
#endif |
1398 |
> |
} |
1399 |
> |
#else |
1400 |
> |
static void sigusr2_handler(int sig, siginfo_t *sip, void *scp) |
1401 |
|
{ |
1402 |
< |
pt_regs *r = sc->regs; |
1402 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1403 |
|
|
1404 |
|
// Do nothing if interrupts are disabled |
1405 |
|
if (*(int32 *)XLM_IRQ_NEST > 0) |
1413 |
|
case MODE_68K: |
1414 |
|
// 68k emulator active, trigger 68k interrupt level 1 |
1415 |
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1416 |
< |
r->ccr |= ntohl(kernel_data->v[0x674 >> 2]); |
1416 |
> |
r->cr() |= ntohl(kernel_data->v[0x674 >> 2]); |
1417 |
|
break; |
1418 |
|
|
1419 |
|
#if INTERRUPTS_IN_NATIVE_MODE |
1420 |
|
case MODE_NATIVE: |
1421 |
|
// 68k emulator inactive, in nanokernel? |
1422 |
< |
if (r->gpr[1] != KernelDataAddr) { |
1422 |
> |
if (r->gpr(1) != KernelDataAddr) { |
1423 |
|
// Prepare for 68k interrupt level 1 |
1424 |
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1425 |
|
WriteMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc, ReadMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc) | ntohl(kernel_data->v[0x674 >> 2])); |
1466 |
|
if (InterruptFlags & INTFLAG_VIA) { |
1467 |
|
ClearInterruptFlag(INTFLAG_VIA); |
1468 |
|
ADBInterrupt(); |
1469 |
< |
ExecutePPC(VideoVBL); |
1469 |
> |
ExecuteNative(NATIVE_VIDEO_VBL); |
1470 |
|
} |
1471 |
|
} |
1472 |
|
#endif |
1478 |
|
} |
1479 |
|
break; |
1480 |
|
#endif |
1273 |
– |
|
1481 |
|
} |
1482 |
|
} |
1483 |
+ |
#endif |
1484 |
|
|
1485 |
|
|
1486 |
|
/* |
1487 |
|
* SIGSEGV handler |
1488 |
|
*/ |
1489 |
|
|
1490 |
< |
static void sigsegv_handler(int sig, sigcontext_struct *sc) |
1490 |
> |
#if !EMULATED_PPC |
1491 |
> |
static void sigsegv_handler(int sig, siginfo_t *sip, void *scp) |
1492 |
|
{ |
1493 |
< |
pt_regs *r = sc->regs; |
1493 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1494 |
> |
|
1495 |
> |
// Get effective address |
1496 |
> |
uint32 addr = r->dar(); |
1497 |
> |
|
1498 |
> |
#if ENABLE_VOSF |
1499 |
> |
// Handle screen fault. |
1500 |
> |
extern bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction); |
1501 |
> |
if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->pc())) |
1502 |
> |
return; |
1503 |
> |
#endif |
1504 |
> |
|
1505 |
|
num_segv++; |
1506 |
|
|
1507 |
|
// Fault in Mac ROM or RAM? |
1508 |
< |
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
1508 |
> |
bool mac_fault = (r->pc() >= ROM_BASE) && (r->pc() < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize)); |
1509 |
|
if (mac_fault) { |
1510 |
|
|
1291 |
– |
// Get opcode and divide into fields |
1292 |
– |
uint32 opcode = *((uint32 *)r->nip); |
1293 |
– |
uint32 primop = opcode >> 26; |
1294 |
– |
uint32 exop = (opcode >> 1) & 0x3ff; |
1295 |
– |
uint32 ra = (opcode >> 16) & 0x1f; |
1296 |
– |
uint32 rb = (opcode >> 11) & 0x1f; |
1297 |
– |
uint32 rd = (opcode >> 21) & 0x1f; |
1298 |
– |
int32 imm = (int16)(opcode & 0xffff); |
1299 |
– |
|
1511 |
|
// "VM settings" during MacOS 8 installation |
1512 |
< |
if (r->nip == ROM_BASE + 0x488160 && r->gpr[20] == 0xf8000000) { |
1513 |
< |
r->nip += 4; |
1514 |
< |
r->gpr[8] = 0; |
1512 |
> |
if (r->pc() == ROM_BASE + 0x488160 && r->gpr(20) == 0xf8000000) { |
1513 |
> |
r->pc() += 4; |
1514 |
> |
r->gpr(8) = 0; |
1515 |
|
return; |
1516 |
|
|
1517 |
|
// MacOS 8.5 installation |
1518 |
< |
} else if (r->nip == ROM_BASE + 0x488140 && r->gpr[16] == 0xf8000000) { |
1519 |
< |
r->nip += 4; |
1520 |
< |
r->gpr[8] = 0; |
1518 |
> |
} else if (r->pc() == ROM_BASE + 0x488140 && r->gpr(16) == 0xf8000000) { |
1519 |
> |
r->pc() += 4; |
1520 |
> |
r->gpr(8) = 0; |
1521 |
|
return; |
1522 |
|
|
1523 |
|
// MacOS 8 serial drivers on startup |
1524 |
< |
} else if (r->nip == ROM_BASE + 0x48e080 && (r->gpr[8] == 0xf3012002 || r->gpr[8] == 0xf3012000)) { |
1525 |
< |
r->nip += 4; |
1526 |
< |
r->gpr[8] = 0; |
1524 |
> |
} else if (r->pc() == ROM_BASE + 0x48e080 && (r->gpr(8) == 0xf3012002 || r->gpr(8) == 0xf3012000)) { |
1525 |
> |
r->pc() += 4; |
1526 |
> |
r->gpr(8) = 0; |
1527 |
|
return; |
1528 |
|
|
1529 |
|
// MacOS 8.1 serial drivers on startup |
1530 |
< |
} else if (r->nip == ROM_BASE + 0x48c5e0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1531 |
< |
r->nip += 4; |
1530 |
> |
} else if (r->pc() == ROM_BASE + 0x48c5e0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1531 |
> |
r->pc() += 4; |
1532 |
|
return; |
1533 |
< |
} else if (r->nip == ROM_BASE + 0x4a10a0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1534 |
< |
r->nip += 4; |
1533 |
> |
} else if (r->pc() == ROM_BASE + 0x4a10a0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1534 |
> |
r->pc() += 4; |
1535 |
|
return; |
1536 |
|
} |
1537 |
|
|
1538 |
+ |
// Get opcode and divide into fields |
1539 |
+ |
uint32 opcode = *((uint32 *)r->pc()); |
1540 |
+ |
uint32 primop = opcode >> 26; |
1541 |
+ |
uint32 exop = (opcode >> 1) & 0x3ff; |
1542 |
+ |
uint32 ra = (opcode >> 16) & 0x1f; |
1543 |
+ |
uint32 rb = (opcode >> 11) & 0x1f; |
1544 |
+ |
uint32 rd = (opcode >> 21) & 0x1f; |
1545 |
+ |
int32 imm = (int16)(opcode & 0xffff); |
1546 |
+ |
|
1547 |
|
// Analyze opcode |
1548 |
|
enum { |
1549 |
|
TYPE_UNKNOWN, |
1625 |
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_NORM; break; |
1626 |
|
case 45: // sthu |
1627 |
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_U; break; |
1628 |
< |
} |
1629 |
< |
|
1630 |
< |
// Calculate effective address |
1631 |
< |
uint32 addr = 0; |
1632 |
< |
switch (addr_mode) { |
1633 |
< |
case MODE_X: |
1634 |
< |
case MODE_UX: |
1635 |
< |
if (ra == 0) |
1636 |
< |
addr = r->gpr[rb]; |
1637 |
< |
else |
1638 |
< |
addr = r->gpr[ra] + r->gpr[rb]; |
1639 |
< |
break; |
1420 |
< |
case MODE_NORM: |
1421 |
< |
case MODE_U: |
1422 |
< |
if (ra == 0) |
1423 |
< |
addr = (int32)(int16)imm; |
1424 |
< |
else |
1425 |
< |
addr = r->gpr[ra] + (int32)(int16)imm; |
1628 |
> |
#if EMULATE_UNALIGNED_LOADSTORE_MULTIPLE |
1629 |
> |
case 46: // lmw |
1630 |
> |
if ((addr % 4) != 0) { |
1631 |
> |
uint32 ea = addr; |
1632 |
> |
D(bug("WARNING: unaligned lmw to EA=%08x from IP=%08x\n", ea, r->pc())); |
1633 |
> |
for (int i = rd; i <= 31; i++) { |
1634 |
> |
r->gpr(i) = ReadMacInt32(ea); |
1635 |
> |
ea += 4; |
1636 |
> |
} |
1637 |
> |
r->pc() += 4; |
1638 |
> |
goto rti; |
1639 |
> |
} |
1640 |
|
break; |
1641 |
< |
default: |
1641 |
> |
case 47: // stmw |
1642 |
> |
if ((addr % 4) != 0) { |
1643 |
> |
uint32 ea = addr; |
1644 |
> |
D(bug("WARNING: unaligned stmw to EA=%08x from IP=%08x\n", ea, r->pc())); |
1645 |
> |
for (int i = rd; i <= 31; i++) { |
1646 |
> |
WriteMacInt32(ea, r->gpr(i)); |
1647 |
> |
ea += 4; |
1648 |
> |
} |
1649 |
> |
r->pc() += 4; |
1650 |
> |
goto rti; |
1651 |
> |
} |
1652 |
|
break; |
1653 |
+ |
#endif |
1654 |
|
} |
1655 |
< |
|
1655 |
> |
|
1656 |
|
// Ignore ROM writes |
1657 |
|
if (transfer_type == TYPE_STORE && addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) { |
1658 |
< |
// 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)); |
1658 |
> |
// 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())); |
1659 |
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1660 |
< |
r->gpr[ra] = addr; |
1661 |
< |
r->nip += 4; |
1660 |
> |
r->gpr(ra) = addr; |
1661 |
> |
r->pc() += 4; |
1662 |
|
goto rti; |
1663 |
|
} |
1664 |
|
|
1665 |
|
// Ignore illegal memory accesses? |
1666 |
|
if (PrefsFindBool("ignoresegv")) { |
1667 |
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1668 |
< |
r->gpr[ra] = addr; |
1668 |
> |
r->gpr(ra) = addr; |
1669 |
|
if (transfer_type == TYPE_LOAD) |
1670 |
< |
r->gpr[rd] = 0; |
1671 |
< |
r->nip += 4; |
1670 |
> |
r->gpr(rd) = 0; |
1671 |
> |
r->pc() += 4; |
1672 |
|
goto rti; |
1673 |
|
} |
1674 |
|
|
1676 |
|
if (!PrefsFindBool("nogui")) { |
1677 |
|
char str[256]; |
1678 |
|
if (transfer_type == TYPE_LOAD || transfer_type == TYPE_STORE) |
1679 |
< |
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]); |
1679 |
> |
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)); |
1680 |
|
else |
1681 |
< |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode); |
1681 |
> |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode); |
1682 |
|
ErrorAlert(str); |
1683 |
|
QuitEmulator(); |
1684 |
|
return; |
1686 |
|
} |
1687 |
|
|
1688 |
|
// For all other errors, jump into debugger (sort of...) |
1689 |
+ |
crash_reason = (sig == SIGBUS) ? "SIGBUS" : "SIGSEGV"; |
1690 |
|
if (!ready_for_signals) { |
1691 |
< |
printf("SIGSEGV\n"); |
1692 |
< |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
1691 |
> |
printf("%s\n"); |
1692 |
> |
printf(" sigcontext %p, machine_regs %p\n", scp, r); |
1693 |
|
printf( |
1694 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1695 |
|
" xer %08lx cr %08lx \n" |
1701 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1702 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1703 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1704 |
< |
r->nip, r->link, r->ctr, r->msr, |
1705 |
< |
r->xer, r->ccr, |
1706 |
< |
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1707 |
< |
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
1708 |
< |
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
1709 |
< |
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
1710 |
< |
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
1711 |
< |
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
1712 |
< |
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
1713 |
< |
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
1704 |
> |
crash_reason, |
1705 |
> |
r->pc(), r->lr(), r->ctr(), r->msr(), |
1706 |
> |
r->xer(), r->cr(), |
1707 |
> |
r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3), |
1708 |
> |
r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7), |
1709 |
> |
r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11), |
1710 |
> |
r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15), |
1711 |
> |
r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19), |
1712 |
> |
r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23), |
1713 |
> |
r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27), |
1714 |
> |
r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31)); |
1715 |
|
exit(1); |
1716 |
|
QuitEmulator(); |
1717 |
|
return; |
1718 |
|
} else { |
1719 |
|
// We crashed. Save registers, tell tick thread and loop forever |
1720 |
< |
sigsegv_regs = *(sigregs *)r; |
1720 |
> |
build_sigregs(&sigsegv_regs, r); |
1721 |
|
emul_thread_fatal = true; |
1722 |
|
for (;;) ; |
1723 |
|
} |
1729 |
|
* SIGILL handler |
1730 |
|
*/ |
1731 |
|
|
1732 |
< |
static void sigill_handler(int sig, sigcontext_struct *sc) |
1732 |
> |
static void sigill_handler(int sig, siginfo_t *sip, void *scp) |
1733 |
|
{ |
1734 |
< |
pt_regs *r = sc->regs; |
1734 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1735 |
|
char str[256]; |
1736 |
|
|
1737 |
|
// Fault in Mac ROM or RAM? |
1738 |
< |
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
1738 |
> |
bool mac_fault = (r->pc() >= ROM_BASE) && (r->pc() < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize)); |
1739 |
|
if (mac_fault) { |
1740 |
|
|
1741 |
|
// Get opcode and divide into fields |
1742 |
< |
uint32 opcode = *((uint32 *)r->nip); |
1742 |
> |
uint32 opcode = *((uint32 *)r->pc()); |
1743 |
|
uint32 primop = opcode >> 26; |
1744 |
|
uint32 exop = (opcode >> 1) & 0x3ff; |
1745 |
|
uint32 ra = (opcode >> 16) & 0x1f; |
1750 |
|
switch (primop) { |
1751 |
|
case 9: // POWER instructions |
1752 |
|
case 22: |
1753 |
< |
power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->nip, r->gpr[1], opcode); |
1753 |
> |
power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->pc(), r->gpr(1), opcode); |
1754 |
|
ErrorAlert(str); |
1755 |
|
QuitEmulator(); |
1756 |
|
return; |
1758 |
|
case 31: |
1759 |
|
switch (exop) { |
1760 |
|
case 83: // mfmsr |
1761 |
< |
r->gpr[rd] = 0xf072; |
1762 |
< |
r->nip += 4; |
1761 |
> |
r->gpr(rd) = 0xf072; |
1762 |
> |
r->pc() += 4; |
1763 |
|
goto rti; |
1764 |
|
|
1765 |
|
case 210: // mtsr |
1766 |
|
case 242: // mtsrin |
1767 |
|
case 306: // tlbie |
1768 |
< |
r->nip += 4; |
1768 |
> |
r->pc() += 4; |
1769 |
|
goto rti; |
1770 |
|
|
1771 |
|
case 339: { // mfspr |
1781 |
|
case 957: // PMC3 |
1782 |
|
case 958: // PMC4 |
1783 |
|
case 959: // SDA |
1784 |
< |
r->nip += 4; |
1784 |
> |
r->pc() += 4; |
1785 |
|
goto rti; |
1786 |
|
case 25: // SDR1 |
1787 |
< |
r->gpr[rd] = 0xdead001f; |
1788 |
< |
r->nip += 4; |
1787 |
> |
r->gpr(rd) = 0xdead001f; |
1788 |
> |
r->pc() += 4; |
1789 |
|
goto rti; |
1790 |
|
case 287: // PVR |
1791 |
< |
r->gpr[rd] = PVR; |
1792 |
< |
r->nip += 4; |
1791 |
> |
r->gpr(rd) = PVR; |
1792 |
> |
r->pc() += 4; |
1793 |
|
goto rti; |
1794 |
|
} |
1795 |
|
break; |
1825 |
|
case 957: // PMC3 |
1826 |
|
case 958: // PMC4 |
1827 |
|
case 959: // SDA |
1828 |
< |
r->nip += 4; |
1828 |
> |
r->pc() += 4; |
1829 |
|
goto rti; |
1830 |
|
} |
1831 |
|
break; |
1844 |
|
|
1845 |
|
// In GUI mode, show error alert |
1846 |
|
if (!PrefsFindBool("nogui")) { |
1847 |
< |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode); |
1847 |
> |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode); |
1848 |
|
ErrorAlert(str); |
1849 |
|
QuitEmulator(); |
1850 |
|
return; |
1852 |
|
} |
1853 |
|
|
1854 |
|
// For all other errors, jump into debugger (sort of...) |
1855 |
+ |
crash_reason = "SIGILL"; |
1856 |
|
if (!ready_for_signals) { |
1857 |
< |
printf("SIGILL\n"); |
1858 |
< |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
1857 |
> |
printf("%s\n"); |
1858 |
> |
printf(" sigcontext %p, machine_regs %p\n", scp, r); |
1859 |
|
printf( |
1860 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1861 |
|
" xer %08lx cr %08lx \n" |
1867 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1868 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1869 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1870 |
< |
r->nip, r->link, r->ctr, r->msr, |
1871 |
< |
r->xer, r->ccr, |
1872 |
< |
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1873 |
< |
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
1874 |
< |
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
1875 |
< |
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
1876 |
< |
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
1877 |
< |
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
1878 |
< |
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
1879 |
< |
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
1870 |
> |
crash_reason, |
1871 |
> |
r->pc(), r->lr(), r->ctr(), r->msr(), |
1872 |
> |
r->xer(), r->cr(), |
1873 |
> |
r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3), |
1874 |
> |
r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7), |
1875 |
> |
r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11), |
1876 |
> |
r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15), |
1877 |
> |
r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19), |
1878 |
> |
r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23), |
1879 |
> |
r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27), |
1880 |
> |
r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31)); |
1881 |
|
exit(1); |
1882 |
|
QuitEmulator(); |
1883 |
|
return; |
1884 |
|
} else { |
1885 |
|
// We crashed. Save registers, tell tick thread and loop forever |
1886 |
< |
sigsegv_regs = *(sigregs *)r; |
1886 |
> |
build_sigregs(&sigsegv_regs, r); |
1887 |
|
emul_thread_fatal = true; |
1888 |
|
for (;;) ; |
1889 |
|
} |
1893 |
|
|
1894 |
|
|
1895 |
|
/* |
1896 |
+ |
* Helpers to share 32-bit addressable data with MacOS |
1897 |
+ |
*/ |
1898 |
+ |
|
1899 |
+ |
bool SheepMem::Init(void) |
1900 |
+ |
{ |
1901 |
+ |
const int page_size = getpagesize(); |
1902 |
+ |
|
1903 |
+ |
// Allocate SheepShaver globals |
1904 |
+ |
if (vm_acquire_fixed((char *)base, size) < 0) |
1905 |
+ |
return false; |
1906 |
+ |
|
1907 |
+ |
// Allocate page with all bits set to 0 |
1908 |
+ |
zero_page = base + size; |
1909 |
+ |
if (vm_acquire_fixed((char *)zero_page, page_size) < 0) |
1910 |
+ |
return false; |
1911 |
+ |
memset((char *)zero_page, 0, page_size); |
1912 |
+ |
if (vm_protect((char *)zero_page, page_size, VM_PAGE_READ) < 0) |
1913 |
+ |
return false; |
1914 |
+ |
|
1915 |
+ |
#if EMULATED_PPC |
1916 |
+ |
// Allocate alternate stack for PowerPC interrupt routine |
1917 |
+ |
sig_stack = zero_page + page_size; |
1918 |
+ |
if (vm_acquire_fixed((char *)sig_stack, SIG_STACK_SIZE) < 0) |
1919 |
+ |
return false; |
1920 |
+ |
#endif |
1921 |
+ |
|
1922 |
+ |
top = base + size; |
1923 |
+ |
return true; |
1924 |
+ |
} |
1925 |
+ |
|
1926 |
+ |
void SheepMem::Exit(void) |
1927 |
+ |
{ |
1928 |
+ |
if (top) { |
1929 |
+ |
const int page_size = getpagesize(); |
1930 |
+ |
|
1931 |
+ |
// Delete SheepShaver globals |
1932 |
+ |
vm_release((void *)base, size); |
1933 |
+ |
|
1934 |
+ |
// Delete zero page |
1935 |
+ |
vm_release((void *)zero_page, page_size); |
1936 |
+ |
|
1937 |
+ |
#if EMULATED_PPC |
1938 |
+ |
// Delete alternate stack for PowerPC interrupt routine |
1939 |
+ |
vm_release((void *)sig_stack, SIG_STACK_SIZE); |
1940 |
+ |
#endif |
1941 |
+ |
} |
1942 |
+ |
} |
1943 |
+ |
|
1944 |
+ |
|
1945 |
+ |
/* |
1946 |
|
* Display alert |
1947 |
|
*/ |
1948 |
|
|