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" |
144 |
|
#endif |
145 |
|
|
146 |
|
|
147 |
+ |
// Enable emulation of unaligned lmw/stmw? |
148 |
+ |
#define EMULATE_UNALIGNED_LOADSTORE_MULTIPLE 1 |
149 |
+ |
|
150 |
|
// Enable Execute68k() safety checks? |
151 |
|
#define SAFE_EXEC_68K 0 |
152 |
|
|
156 |
|
// Interrupts in native mode? |
157 |
|
#define INTERRUPTS_IN_NATIVE_MODE 1 |
158 |
|
|
159 |
+ |
// Number of alternate stacks for signal handlers? |
160 |
+ |
#define SIG_STACK_COUNT 4 |
161 |
+ |
|
162 |
|
|
163 |
|
// Constants |
164 |
|
const char ROM_FILE_NAME[] = "ROM"; |
165 |
|
const char ROM_FILE_NAME2[] = "Mac OS ROM"; |
166 |
|
|
167 |
< |
const uint32 RAM_BASE = 0x20000000; // Base address of RAM |
167 |
> |
const uintptr RAM_BASE = 0x20000000; // Base address of RAM |
168 |
|
const uint32 SIG_STACK_SIZE = 0x10000; // Size of signal stack |
169 |
|
|
170 |
|
|
171 |
|
#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 |
– |
|
172 |
|
struct sigregs { |
173 |
< |
elf_gregset_t gp_regs; // Identical to pt_regs |
174 |
< |
double fp_regs[ELF_NFPREG]; // f0..f31 and fpsrc |
175 |
< |
//more (uninteresting) stuff following here |
173 |
> |
uint32 nip; |
174 |
> |
uint32 link; |
175 |
> |
uint32 ctr; |
176 |
> |
uint32 msr; |
177 |
> |
uint32 xer; |
178 |
> |
uint32 ccr; |
179 |
> |
uint32 gpr[32]; |
180 |
> |
}; |
181 |
> |
|
182 |
> |
#if defined(__linux__) |
183 |
> |
#include <sys/ucontext.h> |
184 |
> |
#define MACHINE_REGISTERS(scp) ((machine_regs *)(((ucontext_t *)scp)->uc_mcontext.regs)) |
185 |
> |
|
186 |
> |
struct machine_regs : public pt_regs |
187 |
> |
{ |
188 |
> |
u_long & cr() { return pt_regs::ccr; } |
189 |
> |
uint32 cr() const { return pt_regs::ccr; } |
190 |
> |
uint32 lr() const { return pt_regs::link; } |
191 |
> |
uint32 ctr() const { return pt_regs::ctr; } |
192 |
> |
uint32 xer() const { return pt_regs::xer; } |
193 |
> |
uint32 msr() const { return pt_regs::msr; } |
194 |
> |
uint32 dar() const { return pt_regs::dar; } |
195 |
> |
u_long & pc() { return pt_regs::nip; } |
196 |
> |
uint32 pc() const { return pt_regs::nip; } |
197 |
> |
u_long & gpr(int i) { return pt_regs::gpr[i]; } |
198 |
> |
uint32 gpr(int i) const { return pt_regs::gpr[i]; } |
199 |
|
}; |
200 |
|
#endif |
201 |
|
|
202 |
+ |
#if defined(__APPLE__) && defined(__MACH__) |
203 |
+ |
#include <sys/signal.h> |
204 |
+ |
extern "C" int sigaltstack(const struct sigaltstack *ss, struct sigaltstack *oss); |
205 |
+ |
|
206 |
+ |
#include <sys/ucontext.h> |
207 |
+ |
#define MACHINE_REGISTERS(scp) ((machine_regs *)(((ucontext_t *)scp)->uc_mcontext)) |
208 |
+ |
|
209 |
+ |
struct machine_regs : public mcontext |
210 |
+ |
{ |
211 |
+ |
uint32 & cr() { return ss.cr; } |
212 |
+ |
uint32 cr() const { return ss.cr; } |
213 |
+ |
uint32 lr() const { return ss.lr; } |
214 |
+ |
uint32 ctr() const { return ss.ctr; } |
215 |
+ |
uint32 xer() const { return ss.xer; } |
216 |
+ |
uint32 msr() const { return ss.srr1; } |
217 |
+ |
uint32 dar() const { return es.dar; } |
218 |
+ |
uint32 & pc() { return ss.srr0; } |
219 |
+ |
uint32 pc() const { return ss.srr0; } |
220 |
+ |
uint32 & gpr(int i) { return (&ss.r0)[i]; } |
221 |
+ |
uint32 gpr(int i) const { return (&ss.r0)[i]; } |
222 |
+ |
}; |
223 |
+ |
#endif |
224 |
+ |
|
225 |
+ |
static void build_sigregs(sigregs *srp, machine_regs *mrp) |
226 |
+ |
{ |
227 |
+ |
srp->nip = mrp->pc(); |
228 |
+ |
srp->link = mrp->lr(); |
229 |
+ |
srp->ctr = mrp->ctr(); |
230 |
+ |
srp->msr = mrp->msr(); |
231 |
+ |
srp->xer = mrp->xer(); |
232 |
+ |
srp->ccr = mrp->cr(); |
233 |
+ |
for (int i = 0; i < 32; i++) |
234 |
+ |
srp->gpr[i] = mrp->gpr(i); |
235 |
+ |
} |
236 |
+ |
|
237 |
+ |
static struct sigaltstack sig_stacks[SIG_STACK_COUNT]; // Stacks for signal handlers |
238 |
+ |
static int sig_stack_id = 0; // Stack slot currently used |
239 |
+ |
|
240 |
+ |
static inline void sig_stack_acquire(void) |
241 |
+ |
{ |
242 |
+ |
if (++sig_stack_id == SIG_STACK_COUNT) { |
243 |
+ |
printf("FATAL: signal stack overflow\n"); |
244 |
+ |
return; |
245 |
+ |
} |
246 |
+ |
sigaltstack(&sig_stacks[sig_stack_id], NULL); |
247 |
+ |
} |
248 |
+ |
|
249 |
+ |
static inline void sig_stack_release(void) |
250 |
+ |
{ |
251 |
+ |
if (--sig_stack_id < 0) { |
252 |
+ |
printf("FATAL: signal stack underflow\n"); |
253 |
+ |
return; |
254 |
+ |
} |
255 |
+ |
sigaltstack(&sig_stacks[sig_stack_id], NULL); |
256 |
+ |
} |
257 |
+ |
#endif |
258 |
+ |
|
259 |
|
|
260 |
|
// Global variables (exported) |
261 |
|
#if !EMULATED_PPC |
263 |
|
#endif |
264 |
|
uint32 RAMBase; // Base address of Mac RAM |
265 |
|
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 |
266 |
|
uint32 KernelDataAddr; // Address of Kernel Data |
267 |
|
uint32 BootGlobsAddr; // Address of BootGlobs structure at top of Mac RAM |
268 |
|
uint32 PVR; // Theoretical PVR |
271 |
|
|
272 |
|
|
273 |
|
// Global variables |
274 |
< |
static char *x_display_name = NULL; // X11 display name |
274 |
> |
char *x_display_name = NULL; // X11 display name |
275 |
|
Display *x_display = NULL; // X11 display handle |
276 |
+ |
#ifdef X11_LOCK_TYPE |
277 |
+ |
X11_LOCK_TYPE x_display_lock = X11_LOCK_INIT; // X11 display lock |
278 |
+ |
#endif |
279 |
|
|
280 |
|
static int zero_fd = 0; // FD of /dev/zero |
190 |
– |
static bool sheep_area_mapped = false; // Flag: SheepShaver data area mmap()ed |
281 |
|
static bool lm_area_mapped = false; // Flag: Low Memory area mmap()ped |
282 |
|
static int kernel_area = -1; // SHM ID of Kernel Data area |
283 |
|
static bool rom_area_mapped = false; // Flag: Mac ROM mmap()ped |
297 |
|
static int64 num_segv = 0; // Number of handled SEGV signals |
298 |
|
|
299 |
|
static struct sigaction sigusr2_action; // Interrupt signal (of emulator thread) |
300 |
< |
#if !EMULATED_PPC |
300 |
> |
#if EMULATED_PPC |
301 |
> |
static uintptr sig_stack = 0; // Stack for PowerPC interrupt routine |
302 |
> |
#else |
303 |
|
static struct sigaction sigsegv_action; // Data access exception signal (of emulator thread) |
304 |
|
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 |
305 |
|
static bool emul_thread_fatal = false; // Flag: MacOS thread crashed, tick thread shall dump debug output |
306 |
|
static sigregs sigsegv_regs; // Register dump when crashed |
307 |
+ |
static const char *crash_reason = NULL; // Reason of the crash (SIGSEGV, SIGBUS, SIGILL) |
308 |
|
#endif |
309 |
|
|
310 |
+ |
uint32 SheepMem::page_size; // Size of a native page |
311 |
+ |
uintptr SheepMem::zero_page = 0; // Address of ro page filled in with zeros |
312 |
+ |
uintptr SheepMem::base = 0x60000000; // Address of SheepShaver data |
313 |
+ |
uintptr SheepMem::top = 0; // Top of SheepShaver data (stack like storage) |
314 |
+ |
|
315 |
|
|
316 |
|
// Prototypes |
317 |
|
static void Quit(void); |
320 |
|
static void *tick_func(void *arg); |
321 |
|
#if EMULATED_PPC |
322 |
|
static void sigusr2_handler(int sig); |
323 |
+ |
extern void emul_ppc(uint32 start); |
324 |
+ |
extern void init_emul_ppc(void); |
325 |
+ |
extern void exit_emul_ppc(void); |
326 |
|
#else |
327 |
< |
static void sigusr2_handler(int sig, sigcontext_struct *sc); |
328 |
< |
static void sigsegv_handler(int sig, sigcontext_struct *sc); |
329 |
< |
static void sigill_handler(int sig, sigcontext_struct *sc); |
327 |
> |
static void sigusr2_handler(int sig, siginfo_t *sip, void *scp); |
328 |
> |
static void sigsegv_handler(int sig, siginfo_t *sip, void *scp); |
329 |
> |
static void sigill_handler(int sig, siginfo_t *sip, void *scp); |
330 |
|
#endif |
331 |
|
|
332 |
|
|
333 |
|
// From asm_linux.S |
334 |
< |
#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 |
334 |
> |
#if !EMULATED_PPC |
335 |
|
extern "C" void *get_toc(void); |
336 |
|
extern "C" void *get_sp(void); |
337 |
|
extern "C" void flush_icache_range(void *start, void *end); |
346 |
|
#endif |
347 |
|
|
348 |
|
|
349 |
+ |
#if EMULATED_PPC |
350 |
+ |
/* |
351 |
+ |
* Return signal stack base |
352 |
+ |
*/ |
353 |
+ |
|
354 |
+ |
uintptr SignalStackBase(void) |
355 |
+ |
{ |
356 |
+ |
return sig_stack + SIG_STACK_SIZE; |
357 |
+ |
} |
358 |
+ |
|
359 |
+ |
|
360 |
+ |
/* |
361 |
+ |
* Atomic operations |
362 |
+ |
*/ |
363 |
+ |
|
364 |
+ |
#if HAVE_SPINLOCKS |
365 |
+ |
static spinlock_t atomic_ops_lock = SPIN_LOCK_UNLOCKED; |
366 |
+ |
#else |
367 |
+ |
#define spin_lock(LOCK) |
368 |
+ |
#define spin_unlock(LOCK) |
369 |
+ |
#endif |
370 |
+ |
|
371 |
+ |
int atomic_add(int *var, int v) |
372 |
+ |
{ |
373 |
+ |
spin_lock(&atomic_ops_lock); |
374 |
+ |
int ret = *var; |
375 |
+ |
*var += v; |
376 |
+ |
spin_unlock(&atomic_ops_lock); |
377 |
+ |
return ret; |
378 |
+ |
} |
379 |
+ |
|
380 |
+ |
int atomic_and(int *var, int v) |
381 |
+ |
{ |
382 |
+ |
spin_lock(&atomic_ops_lock); |
383 |
+ |
int ret = *var; |
384 |
+ |
*var &= v; |
385 |
+ |
spin_unlock(&atomic_ops_lock); |
386 |
+ |
return ret; |
387 |
+ |
} |
388 |
+ |
|
389 |
+ |
int atomic_or(int *var, int v) |
390 |
+ |
{ |
391 |
+ |
spin_lock(&atomic_ops_lock); |
392 |
+ |
int ret = *var; |
393 |
+ |
*var |= v; |
394 |
+ |
spin_unlock(&atomic_ops_lock); |
395 |
+ |
return ret; |
396 |
+ |
} |
397 |
+ |
#endif |
398 |
+ |
|
399 |
+ |
|
400 |
|
/* |
401 |
|
* Main program |
402 |
|
*/ |
415 |
|
char str[256]; |
416 |
|
uint32 *boot_globs; |
417 |
|
int16 i16; |
272 |
– |
int drive, driver; |
418 |
|
int rom_fd; |
419 |
|
FILE *proc_file; |
420 |
|
const char *rom_path; |
481 |
|
PVR = 0x00040000; // Default: 604 |
482 |
|
CPUClockSpeed = 100000000; // Default: 100MHz |
483 |
|
BusClockSpeed = 100000000; // Default: 100MHz |
484 |
< |
#if !EMULATED_PPC |
484 |
> |
#if EMULATED_PPC |
485 |
> |
PVR = 0x000c0000; // Default: 7400 (with AltiVec) |
486 |
> |
#else |
487 |
|
proc_file = fopen("/proc/cpuinfo", "r"); |
488 |
|
if (proc_file) { |
489 |
|
char line[256]; |
497 |
|
// Parse line |
498 |
|
int i; |
499 |
|
char value[256]; |
500 |
< |
if (sscanf(line, "cpu : %s", value) == 1) { |
500 |
> |
if (sscanf(line, "cpu : %[0-9A-Za-a]", value) == 1) { |
501 |
|
if (strcmp(value, "601") == 0) |
502 |
|
PVR = 0x00010000; |
503 |
|
else if (strcmp(value, "603") == 0) |
518 |
|
PVR = 0x00320000; |
519 |
|
else if (strcmp(value, "860") == 0) |
520 |
|
PVR = 0x00500000; |
521 |
+ |
else if (strcmp(value, "7400") == 0) |
522 |
+ |
PVR = 0x000c0000; |
523 |
+ |
else if (strcmp(value, "7410") == 0) |
524 |
+ |
PVR = 0x800c0000; |
525 |
|
else |
526 |
|
printf("WARNING: Unknown CPU type '%s', assuming 604\n", value); |
527 |
|
} |
557 |
|
goto quit; |
558 |
|
} |
559 |
|
|
560 |
+ |
#ifndef PAGEZERO_HACK |
561 |
|
// Create Low Memory area (0x0000..0x3000) |
562 |
|
if (vm_acquire_fixed((char *)0, 0x3000) < 0) { |
563 |
|
sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno)); |
565 |
|
goto quit; |
566 |
|
} |
567 |
|
lm_area_mapped = true; |
568 |
+ |
#endif |
569 |
|
|
570 |
|
// Create areas for Kernel Data |
571 |
|
kernel_area = shmget(IPC_PRIVATE, KERNEL_AREA_SIZE, 0600); |
584 |
|
ErrorAlert(str); |
585 |
|
goto quit; |
586 |
|
} |
587 |
< |
kernel_data = (KernelData *)0x68ffe000; |
587 |
> |
kernel_data = (KernelData *)KERNEL_DATA_BASE; |
588 |
|
emulator_data = &kernel_data->ed; |
589 |
< |
KernelDataAddr = (uint32)kernel_data; |
589 |
> |
KernelDataAddr = KERNEL_DATA_BASE; |
590 |
|
D(bug("Kernel Data at %p, Emulator Data at %p\n", kernel_data, emulator_data)); |
591 |
|
|
592 |
|
// Create area for SheepShaver data |
593 |
< |
if (vm_acquire_fixed((char *)SHEEP_BASE, SHEEP_SIZE) < 0) { |
593 |
> |
if (!SheepMem::Init()) { |
594 |
|
sprintf(str, GetString(STR_SHEEP_MEM_MMAP_ERR), strerror(errno)); |
595 |
|
ErrorAlert(str); |
596 |
|
goto quit; |
597 |
|
} |
445 |
– |
SheepStack1Base = SHEEP_BASE + 0x10000; |
446 |
– |
SheepStack2Base = SheepStack1Base + 0x10000; |
447 |
– |
SheepThunksBase = SheepStack2Base + 0x1000; |
448 |
– |
sheep_area_mapped = true; |
598 |
|
|
599 |
|
// Create area for Mac ROM |
600 |
|
if (vm_acquire_fixed((char *)ROM_BASE, ROM_AREA_SIZE) < 0) { |
602 |
|
ErrorAlert(str); |
603 |
|
goto quit; |
604 |
|
} |
605 |
< |
#if !EMULATED_PPC || defined(__powerpc__) |
605 |
> |
#if !EMULATED_PPC |
606 |
|
if (vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
607 |
|
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
608 |
|
ErrorAlert(str); |
672 |
|
// Load NVRAM |
673 |
|
XPRAMInit(); |
674 |
|
|
675 |
+ |
// Load XPRAM default values if signature not found |
676 |
+ |
if (XPRAM[0x130c] != 0x4e || XPRAM[0x130d] != 0x75 |
677 |
+ |
|| XPRAM[0x130e] != 0x4d || XPRAM[0x130f] != 0x63) { |
678 |
+ |
D(bug("Loading XPRAM default values\n")); |
679 |
+ |
memset(XPRAM + 0x1300, 0, 0x100); |
680 |
+ |
XPRAM[0x130c] = 0x4e; // "NuMc" signature |
681 |
+ |
XPRAM[0x130d] = 0x75; |
682 |
+ |
XPRAM[0x130e] = 0x4d; |
683 |
+ |
XPRAM[0x130f] = 0x63; |
684 |
+ |
XPRAM[0x1301] = 0x80; // InternalWaitFlags = DynWait (don't wait for SCSI devices upon bootup) |
685 |
+ |
XPRAM[0x1310] = 0xa8; // Standard PRAM values |
686 |
+ |
XPRAM[0x1311] = 0x00; |
687 |
+ |
XPRAM[0x1312] = 0x00; |
688 |
+ |
XPRAM[0x1313] = 0x22; |
689 |
+ |
XPRAM[0x1314] = 0xcc; |
690 |
+ |
XPRAM[0x1315] = 0x0a; |
691 |
+ |
XPRAM[0x1316] = 0xcc; |
692 |
+ |
XPRAM[0x1317] = 0x0a; |
693 |
+ |
XPRAM[0x131c] = 0x00; |
694 |
+ |
XPRAM[0x131d] = 0x02; |
695 |
+ |
XPRAM[0x131e] = 0x63; |
696 |
+ |
XPRAM[0x131f] = 0x00; |
697 |
+ |
XPRAM[0x1308] = 0x13; |
698 |
+ |
XPRAM[0x1309] = 0x88; |
699 |
+ |
XPRAM[0x130a] = 0x00; |
700 |
+ |
XPRAM[0x130b] = 0xcc; |
701 |
+ |
XPRAM[0x1376] = 0x00; // OSDefault = MacOS |
702 |
+ |
XPRAM[0x1377] = 0x01; |
703 |
+ |
} |
704 |
+ |
|
705 |
|
// Set boot volume |
706 |
< |
drive = PrefsFindInt32("bootdrive"); |
706 |
> |
i16 = PrefsFindInt32("bootdrive"); |
707 |
|
XPRAM[0x1378] = i16 >> 8; |
708 |
|
XPRAM[0x1379] = i16 & 0xff; |
709 |
< |
driver = PrefsFindInt32("bootdriver"); |
709 |
> |
i16 = PrefsFindInt32("bootdriver"); |
710 |
|
XPRAM[0x137a] = i16 >> 8; |
711 |
|
XPRAM[0x137b] = i16 & 0xff; |
712 |
|
|
719 |
|
boot_globs[1] = htonl(RAMSize); |
720 |
|
boot_globs[2] = htonl((uint32)-1); // End of bank table |
721 |
|
|
722 |
+ |
// Init thunks |
723 |
+ |
if (!ThunksInit()) |
724 |
+ |
goto quit; |
725 |
+ |
|
726 |
|
// Init drivers |
727 |
|
SonyInit(); |
728 |
|
DiskInit(); |
732 |
|
// Init external file system |
733 |
|
ExtFSInit(); |
734 |
|
|
735 |
+ |
// Init ADB |
736 |
+ |
ADBInit(); |
737 |
+ |
|
738 |
|
// Init audio |
739 |
|
AudioInit(); |
740 |
|
|
769 |
|
// Initialize Kernel Data |
770 |
|
memset(kernel_data, 0, sizeof(KernelData)); |
771 |
|
if (ROMType == ROMTYPE_NEWWORLD) { |
772 |
< |
static uint32 of_dev_tree[4] = {0, 0, 0, 0}; |
773 |
< |
static uint8 vector_lookup_tbl[128]; |
774 |
< |
static uint8 vector_mask_tbl[64]; |
772 |
> |
uintptr of_dev_tree = SheepMem::Reserve(4 * sizeof(uint32)); |
773 |
> |
memset((void *)of_dev_tree, 0, 4 * sizeof(uint32)); |
774 |
> |
uintptr vector_lookup_tbl = SheepMem::Reserve(128); |
775 |
> |
uintptr vector_mask_tbl = SheepMem::Reserve(64); |
776 |
|
memset((uint8 *)kernel_data + 0xb80, 0x3d, 0x80); |
777 |
< |
memset(vector_lookup_tbl, 0, 128); |
778 |
< |
memset(vector_mask_tbl, 0, 64); |
777 |
> |
memset((void *)vector_lookup_tbl, 0, 128); |
778 |
> |
memset((void *)vector_mask_tbl, 0, 64); |
779 |
|
kernel_data->v[0xb80 >> 2] = htonl(ROM_BASE); |
780 |
< |
kernel_data->v[0xb84 >> 2] = htonl((uint32)of_dev_tree); // OF device tree base |
781 |
< |
kernel_data->v[0xb90 >> 2] = htonl((uint32)vector_lookup_tbl); |
782 |
< |
kernel_data->v[0xb94 >> 2] = htonl((uint32)vector_mask_tbl); |
780 |
> |
kernel_data->v[0xb84 >> 2] = htonl(of_dev_tree); // OF device tree base |
781 |
> |
kernel_data->v[0xb90 >> 2] = htonl(vector_lookup_tbl); |
782 |
> |
kernel_data->v[0xb94 >> 2] = htonl(vector_mask_tbl); |
783 |
|
kernel_data->v[0xb98 >> 2] = htonl(ROM_BASE); // OpenPIC base |
784 |
|
kernel_data->v[0xbb0 >> 2] = htonl(0); // ADB base |
785 |
|
kernel_data->v[0xc20 >> 2] = htonl(RAMSize); |
815 |
|
D(bug("Initializing Low Memory...\n")); |
816 |
|
memset(NULL, 0, 0x3000); |
817 |
|
WriteMacInt32(XLM_SIGNATURE, FOURCC('B','a','a','h')); // Signature to detect SheepShaver |
818 |
< |
WriteMacInt32(XLM_KERNEL_DATA, (uint32)kernel_data); // For trap replacement routines |
818 |
> |
WriteMacInt32(XLM_KERNEL_DATA, KernelDataAddr); // For trap replacement routines |
819 |
|
WriteMacInt32(XLM_PVR, PVR); // Theoretical PVR |
820 |
|
WriteMacInt32(XLM_BUS_CLOCK, BusClockSpeed); // For DriverServicesLib patch |
821 |
|
WriteMacInt16(XLM_EXEC_RETURN_OPCODE, M68K_EXEC_RETURN); // For Execute68k() (RTS from the executed 68k code will jump here and end 68k mode) |
822 |
< |
#if EMULATED_PPC |
823 |
< |
WriteMacInt32(XLM_ETHER_INIT, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_INIT)); |
824 |
< |
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); |
822 |
> |
WriteMacInt32(XLM_ZERO_PAGE, SheepMem::ZeroPage()); // Pointer to read-only page with all bits set to 0 |
823 |
> |
#if !EMULATED_PPC |
824 |
> |
WriteMacInt32(XLM_TOC, (uint32)TOC); // TOC pointer of emulator |
825 |
|
#endif |
826 |
+ |
WriteMacInt32(XLM_ETHER_INIT, NativeFunction(NATIVE_ETHER_INIT)); // DLPI ethernet driver functions |
827 |
+ |
WriteMacInt32(XLM_ETHER_TERM, NativeFunction(NATIVE_ETHER_TERM)); |
828 |
+ |
WriteMacInt32(XLM_ETHER_OPEN, NativeFunction(NATIVE_ETHER_OPEN)); |
829 |
+ |
WriteMacInt32(XLM_ETHER_CLOSE, NativeFunction(NATIVE_ETHER_CLOSE)); |
830 |
+ |
WriteMacInt32(XLM_ETHER_WPUT, NativeFunction(NATIVE_ETHER_WPUT)); |
831 |
+ |
WriteMacInt32(XLM_ETHER_RSRV, NativeFunction(NATIVE_ETHER_RSRV)); |
832 |
+ |
WriteMacInt32(XLM_VIDEO_DOIO, NativeFunction(NATIVE_VIDEO_DO_DRIVER_IO)); |
833 |
|
D(bug("Low Memory initialized\n")); |
834 |
|
|
835 |
|
// Start 60Hz thread |
843 |
|
|
844 |
|
#if !EMULATED_PPC |
845 |
|
// Create and install stacks for signal handlers |
846 |
< |
sig_stack = malloc(SIG_STACK_SIZE); |
847 |
< |
D(bug("Signal stack at %p\n", sig_stack)); |
848 |
< |
if (sig_stack == NULL) { |
849 |
< |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
850 |
< |
goto quit; |
851 |
< |
} |
852 |
< |
extra_stack = malloc(SIG_STACK_SIZE); |
853 |
< |
D(bug("Extra stack at %p\n", extra_stack)); |
854 |
< |
if (extra_stack == NULL) { |
855 |
< |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
676 |
< |
goto quit; |
846 |
> |
for (int i = 0; i < SIG_STACK_COUNT; i++) { |
847 |
> |
void *sig_stack = malloc(SIG_STACK_SIZE); |
848 |
> |
D(bug("Signal stack %d at %p\n", i, sig_stack)); |
849 |
> |
if (sig_stack == NULL) { |
850 |
> |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
851 |
> |
goto quit; |
852 |
> |
} |
853 |
> |
sig_stacks[i].ss_sp = sig_stack; |
854 |
> |
sig_stacks[i].ss_flags = 0; |
855 |
> |
sig_stacks[i].ss_size = SIG_STACK_SIZE; |
856 |
|
} |
857 |
< |
struct sigaltstack new_stack; |
858 |
< |
new_stack.ss_sp = sig_stack; |
680 |
< |
new_stack.ss_flags = 0; |
681 |
< |
new_stack.ss_size = SIG_STACK_SIZE; |
682 |
< |
if (sigaltstack(&new_stack, NULL) < 0) { |
857 |
> |
sig_stack_id = 0; |
858 |
> |
if (sigaltstack(&sig_stacks[0], NULL) < 0) { |
859 |
|
sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno)); |
860 |
|
ErrorAlert(str); |
861 |
|
goto quit; |
863 |
|
#endif |
864 |
|
|
865 |
|
#if !EMULATED_PPC |
866 |
< |
// Install SIGSEGV handler |
866 |
> |
// Install SIGSEGV and SIGBUS handlers |
867 |
|
sigemptyset(&sigsegv_action.sa_mask); // Block interrupts during SEGV handling |
868 |
|
sigaddset(&sigsegv_action.sa_mask, SIGUSR2); |
869 |
< |
sigsegv_action.sa_handler = (__sighandler_t)sigsegv_handler; |
870 |
< |
sigsegv_action.sa_flags = SA_ONSTACK; |
869 |
> |
sigsegv_action.sa_sigaction = sigsegv_handler; |
870 |
> |
sigsegv_action.sa_flags = SA_ONSTACK | SA_SIGINFO; |
871 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
872 |
|
sigsegv_action.sa_restorer = NULL; |
873 |
+ |
#endif |
874 |
|
if (sigaction(SIGSEGV, &sigsegv_action, NULL) < 0) { |
875 |
|
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
876 |
|
ErrorAlert(str); |
877 |
|
goto quit; |
878 |
|
} |
879 |
+ |
if (sigaction(SIGBUS, &sigsegv_action, NULL) < 0) { |
880 |
+ |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
881 |
+ |
ErrorAlert(str); |
882 |
+ |
goto quit; |
883 |
+ |
} |
884 |
|
|
885 |
|
// Install SIGILL handler |
886 |
|
sigemptyset(&sigill_action.sa_mask); // Block interrupts during ILL handling |
887 |
|
sigaddset(&sigill_action.sa_mask, SIGUSR2); |
888 |
< |
sigill_action.sa_handler = (__sighandler_t)sigill_handler; |
889 |
< |
sigill_action.sa_flags = SA_ONSTACK; |
888 |
> |
sigill_action.sa_sigaction = sigill_handler; |
889 |
> |
sigill_action.sa_flags = SA_ONSTACK | SA_SIGINFO; |
890 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
891 |
|
sigill_action.sa_restorer = NULL; |
892 |
+ |
#endif |
893 |
|
if (sigaction(SIGILL, &sigill_action, NULL) < 0) { |
894 |
|
sprintf(str, GetString(STR_SIGILL_INSTALL_ERR), strerror(errno)); |
895 |
|
ErrorAlert(str); |
897 |
|
} |
898 |
|
#endif |
899 |
|
|
900 |
+ |
#if !EMULATED_PPC |
901 |
|
// Install interrupt signal handler |
902 |
|
sigemptyset(&sigusr2_action.sa_mask); |
903 |
< |
sigusr2_action.sa_handler = (__sighandler_t)sigusr2_handler; |
904 |
< |
sigusr2_action.sa_flags = 0; |
905 |
< |
#if !EMULATED_PPC |
720 |
< |
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART; |
721 |
< |
#endif |
903 |
> |
sigusr2_action.sa_sigaction = sigusr2_handler; |
904 |
> |
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART | SA_SIGINFO; |
905 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
906 |
|
sigusr2_action.sa_restorer = NULL; |
907 |
+ |
#endif |
908 |
|
if (sigaction(SIGUSR2, &sigusr2_action, NULL) < 0) { |
909 |
|
sprintf(str, GetString(STR_SIGUSR2_INSTALL_ERR), strerror(errno)); |
910 |
|
ErrorAlert(str); |
911 |
|
goto quit; |
912 |
|
} |
913 |
+ |
#endif |
914 |
|
|
915 |
|
// Get my thread ID and execute MacOS thread function |
916 |
|
emul_thread = pthread_self(); |
929 |
|
|
930 |
|
static void Quit(void) |
931 |
|
{ |
932 |
+ |
#if EMULATED_PPC |
933 |
+ |
// Exit PowerPC emulation |
934 |
+ |
exit_emul_ppc(); |
935 |
+ |
#endif |
936 |
+ |
|
937 |
|
// Stop 60Hz thread |
938 |
|
if (tick_thread_active) { |
939 |
|
pthread_cancel(tick_thread); |
947 |
|
} |
948 |
|
|
949 |
|
#if !EMULATED_PPC |
950 |
< |
// Uninstall SIGSEGV handler |
950 |
> |
// Uninstall SIGSEGV and SIGBUS handlers |
951 |
|
sigemptyset(&sigsegv_action.sa_mask); |
952 |
|
sigsegv_action.sa_handler = SIG_DFL; |
953 |
|
sigsegv_action.sa_flags = 0; |
954 |
|
sigaction(SIGSEGV, &sigsegv_action, NULL); |
955 |
+ |
sigaction(SIGBUS, &sigsegv_action, NULL); |
956 |
|
|
957 |
|
// Uninstall SIGILL handler |
958 |
|
sigemptyset(&sigill_action.sa_mask); |
959 |
|
sigill_action.sa_handler = SIG_DFL; |
960 |
|
sigill_action.sa_flags = 0; |
961 |
|
sigaction(SIGILL, &sigill_action, NULL); |
962 |
+ |
|
963 |
+ |
// Delete stacks for signal handlers |
964 |
+ |
for (int i = 0; i < SIG_STACK_COUNT; i++) { |
965 |
+ |
void *sig_stack = sig_stacks[i].ss_sp; |
966 |
+ |
if (sig_stack) |
967 |
+ |
free(sig_stack); |
968 |
+ |
} |
969 |
|
#endif |
970 |
|
|
971 |
|
// Save NVRAM |
986 |
|
// Exit audio |
987 |
|
AudioExit(); |
988 |
|
|
989 |
+ |
// Exit ADB |
990 |
+ |
ADBExit(); |
991 |
+ |
|
992 |
|
// Exit video |
993 |
|
VideoExit(); |
994 |
|
|
1001 |
|
DiskExit(); |
1002 |
|
SonyExit(); |
1003 |
|
|
1004 |
+ |
// Delete thunks |
1005 |
+ |
ThunksExit(); |
1006 |
+ |
|
1007 |
+ |
// Delete SheepShaver globals |
1008 |
+ |
SheepMem::Exit(); |
1009 |
+ |
|
1010 |
|
// Delete RAM area |
1011 |
|
if (ram_area_mapped) |
1012 |
|
vm_release((char *)RAM_BASE, RAMSize); |
1054 |
|
*/ |
1055 |
|
|
1056 |
|
#if EMULATED_PPC |
849 |
– |
extern void emul_ppc(uint32 start); |
850 |
– |
extern void init_emul_ppc(void); |
1057 |
|
void jump_to_rom(uint32 entry) |
1058 |
|
{ |
1059 |
|
init_emul_ppc(); |
1118 |
|
uint16 proc[2] = {trap, M68K_RTS}; |
1119 |
|
Execute68k((uint32)proc, r); |
1120 |
|
} |
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 |
– |
} |
1121 |
|
#endif |
1122 |
|
|
1123 |
|
|
1180 |
|
|
1181 |
|
void MakeExecutable(int dummy, void *start, uint32 length) |
1182 |
|
{ |
1183 |
< |
#if !EMULATED_PPC |
991 |
< |
if (((uint32)start >= ROM_BASE) && ((uint32)start < (ROM_BASE + ROM_SIZE))) |
1183 |
> |
if (((uintptr)start >= ROM_BASE) && ((uintptr)start < (ROM_BASE + ROM_SIZE))) |
1184 |
|
return; |
1185 |
< |
flush_icache_range(start, (void *)((uint32)start + length)); |
1185 |
> |
#if EMULATED_PPC |
1186 |
> |
FlushCodeCache((uintptr)start, (uintptr)start + length); |
1187 |
> |
#else |
1188 |
> |
flush_icache_range(start, (void *)((uintptr)start + length)); |
1189 |
|
#endif |
1190 |
|
} |
1191 |
|
|
1196 |
|
|
1197 |
|
void PatchAfterStartup(void) |
1198 |
|
{ |
1004 |
– |
#if EMULATED_PPC |
1199 |
|
ExecuteNative(NATIVE_VIDEO_INSTALL_ACCEL); |
1006 |
– |
#else |
1007 |
– |
ExecutePPC(VideoInstallAccel); |
1008 |
– |
#endif |
1200 |
|
InstallExtFS(); |
1201 |
|
} |
1202 |
|
|
1241 |
|
if (emul_thread_fatal) { |
1242 |
|
|
1243 |
|
// Yes, dump registers |
1244 |
< |
pt_regs *r = (pt_regs *)&sigsegv_regs; |
1244 |
> |
sigregs *r = &sigsegv_regs; |
1245 |
|
char str[256]; |
1246 |
< |
sprintf(str, "SIGSEGV\n" |
1246 |
> |
if (crash_reason == NULL) |
1247 |
> |
crash_reason = "SIGSEGV"; |
1248 |
> |
sprintf(str, "%s\n" |
1249 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1250 |
|
" xer %08lx cr %08lx \n" |
1251 |
|
" r0 %08lx r1 %08lx r2 %08lx r3 %08lx\n" |
1256 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1257 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1258 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1259 |
+ |
crash_reason, |
1260 |
|
r->nip, r->link, r->ctr, r->msr, |
1261 |
|
r->xer, r->ccr, |
1262 |
|
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1302 |
|
|
1303 |
|
void Set_pthread_attr(pthread_attr_t *attr, int priority) |
1304 |
|
{ |
1305 |
< |
// nothing to do |
1305 |
> |
#ifdef HAVE_PTHREADS |
1306 |
> |
pthread_attr_init(attr); |
1307 |
> |
#if defined(_POSIX_THREAD_PRIORITY_SCHEDULING) |
1308 |
> |
// Some of these only work for superuser |
1309 |
> |
if (geteuid() == 0) { |
1310 |
> |
pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED); |
1311 |
> |
pthread_attr_setschedpolicy(attr, SCHED_FIFO); |
1312 |
> |
struct sched_param fifo_param; |
1313 |
> |
fifo_param.sched_priority = ((sched_get_priority_min(SCHED_FIFO) + |
1314 |
> |
sched_get_priority_max(SCHED_FIFO)) / 2 + |
1315 |
> |
priority); |
1316 |
> |
pthread_attr_setschedparam(attr, &fifo_param); |
1317 |
> |
} |
1318 |
> |
if (pthread_attr_setscope(attr, PTHREAD_SCOPE_SYSTEM) != 0) { |
1319 |
> |
#ifdef PTHREAD_SCOPE_BOUND_NP |
1320 |
> |
// If system scope is not available (eg. we're not running |
1321 |
> |
// with CAP_SCHED_MGT capability on an SGI box), try bound |
1322 |
> |
// scope. It exposes pthread scheduling to the kernel, |
1323 |
> |
// without setting realtime priority. |
1324 |
> |
pthread_attr_setscope(attr, PTHREAD_SCOPE_BOUND_NP); |
1325 |
> |
#endif |
1326 |
> |
} |
1327 |
> |
#endif |
1328 |
> |
#endif |
1329 |
|
} |
1330 |
|
|
1331 |
|
|
1471 |
|
#endif |
1472 |
|
} |
1473 |
|
#else |
1474 |
< |
static void sigusr2_handler(int sig, sigcontext_struct *sc) |
1474 |
> |
static void sigusr2_handler(int sig, siginfo_t *sip, void *scp) |
1475 |
|
{ |
1476 |
< |
pt_regs *r = sc->regs; |
1476 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1477 |
|
|
1478 |
|
// Do nothing if interrupts are disabled |
1479 |
|
if (*(int32 *)XLM_IRQ_NEST > 0) |
1487 |
|
case MODE_68K: |
1488 |
|
// 68k emulator active, trigger 68k interrupt level 1 |
1489 |
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1490 |
< |
r->ccr |= ntohl(kernel_data->v[0x674 >> 2]); |
1490 |
> |
r->cr() |= ntohl(kernel_data->v[0x674 >> 2]); |
1491 |
|
break; |
1492 |
|
|
1493 |
|
#if INTERRUPTS_IN_NATIVE_MODE |
1494 |
|
case MODE_NATIVE: |
1495 |
|
// 68k emulator inactive, in nanokernel? |
1496 |
< |
if (r->gpr[1] != KernelDataAddr) { |
1496 |
> |
if (r->gpr(1) != KernelDataAddr) { |
1497 |
> |
|
1498 |
> |
// Set extra stack for nested interrupts |
1499 |
> |
sig_stack_acquire(); |
1500 |
> |
|
1501 |
|
// Prepare for 68k interrupt level 1 |
1502 |
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1503 |
|
WriteMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc, ReadMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc) | ntohl(kernel_data->v[0x674 >> 2])); |
1504 |
|
|
1505 |
|
// Execute nanokernel interrupt routine (this will activate the 68k emulator) |
1506 |
< |
atomic_add((int32 *)XLM_IRQ_NEST, 1); |
1506 |
> |
DisableInterrupt(); |
1507 |
|
if (ROMType == ROMTYPE_NEWWORLD) |
1508 |
|
ppc_interrupt(ROM_BASE + 0x312b1c, KernelDataAddr); |
1509 |
|
else |
1510 |
|
ppc_interrupt(ROM_BASE + 0x312a3c, KernelDataAddr); |
1511 |
+ |
|
1512 |
+ |
// Reset normal signal stack |
1513 |
+ |
sig_stack_release(); |
1514 |
|
} |
1515 |
|
break; |
1516 |
|
#endif |
1521 |
|
if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) { |
1522 |
|
|
1523 |
|
// Set extra stack for SIGSEGV handler |
1524 |
< |
struct sigaltstack new_stack; |
1301 |
< |
new_stack.ss_sp = extra_stack; |
1302 |
< |
new_stack.ss_flags = 0; |
1303 |
< |
new_stack.ss_size = SIG_STACK_SIZE; |
1304 |
< |
sigaltstack(&new_stack, NULL); |
1524 |
> |
sig_stack_acquire(); |
1525 |
|
#if 1 |
1526 |
|
// Execute full 68k interrupt routine |
1527 |
|
M68kRegisters r; |
1543 |
|
if (InterruptFlags & INTFLAG_VIA) { |
1544 |
|
ClearInterruptFlag(INTFLAG_VIA); |
1545 |
|
ADBInterrupt(); |
1546 |
< |
ExecutePPC(VideoVBL); |
1546 |
> |
ExecuteNative(NATIVE_VIDEO_VBL); |
1547 |
|
} |
1548 |
|
} |
1549 |
|
#endif |
1550 |
|
// Reset normal signal stack |
1551 |
< |
new_stack.ss_sp = sig_stack; |
1332 |
< |
new_stack.ss_flags = 0; |
1333 |
< |
new_stack.ss_size = SIG_STACK_SIZE; |
1334 |
< |
sigaltstack(&new_stack, NULL); |
1551 |
> |
sig_stack_release(); |
1552 |
|
} |
1553 |
|
break; |
1554 |
|
#endif |
1562 |
|
*/ |
1563 |
|
|
1564 |
|
#if !EMULATED_PPC |
1565 |
< |
static void sigsegv_handler(int sig, sigcontext_struct *sc) |
1565 |
> |
static void sigsegv_handler(int sig, siginfo_t *sip, void *scp) |
1566 |
|
{ |
1567 |
< |
pt_regs *r = sc->regs; |
1567 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1568 |
|
|
1569 |
|
// Get effective address |
1570 |
< |
uint32 addr = r->dar; |
1570 |
> |
uint32 addr = r->dar(); |
1571 |
|
|
1572 |
|
#if ENABLE_VOSF |
1573 |
|
// Handle screen fault. |
1574 |
|
extern bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction); |
1575 |
< |
if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->nip)) |
1575 |
> |
if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->pc())) |
1576 |
|
return; |
1577 |
|
#endif |
1578 |
|
|
1579 |
|
num_segv++; |
1580 |
|
|
1581 |
|
// Fault in Mac ROM or RAM? |
1582 |
< |
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
1582 |
> |
bool mac_fault = (r->pc() >= ROM_BASE) && (r->pc() < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize)); |
1583 |
|
if (mac_fault) { |
1584 |
|
|
1585 |
|
// "VM settings" during MacOS 8 installation |
1586 |
< |
if (r->nip == ROM_BASE + 0x488160 && r->gpr[20] == 0xf8000000) { |
1587 |
< |
r->nip += 4; |
1588 |
< |
r->gpr[8] = 0; |
1586 |
> |
if (r->pc() == ROM_BASE + 0x488160 && r->gpr(20) == 0xf8000000) { |
1587 |
> |
r->pc() += 4; |
1588 |
> |
r->gpr(8) = 0; |
1589 |
|
return; |
1590 |
|
|
1591 |
|
// MacOS 8.5 installation |
1592 |
< |
} else if (r->nip == ROM_BASE + 0x488140 && r->gpr[16] == 0xf8000000) { |
1593 |
< |
r->nip += 4; |
1594 |
< |
r->gpr[8] = 0; |
1592 |
> |
} else if (r->pc() == ROM_BASE + 0x488140 && r->gpr(16) == 0xf8000000) { |
1593 |
> |
r->pc() += 4; |
1594 |
> |
r->gpr(8) = 0; |
1595 |
|
return; |
1596 |
|
|
1597 |
|
// MacOS 8 serial drivers on startup |
1598 |
< |
} else if (r->nip == ROM_BASE + 0x48e080 && (r->gpr[8] == 0xf3012002 || r->gpr[8] == 0xf3012000)) { |
1599 |
< |
r->nip += 4; |
1600 |
< |
r->gpr[8] = 0; |
1598 |
> |
} else if (r->pc() == ROM_BASE + 0x48e080 && (r->gpr(8) == 0xf3012002 || r->gpr(8) == 0xf3012000)) { |
1599 |
> |
r->pc() += 4; |
1600 |
> |
r->gpr(8) = 0; |
1601 |
|
return; |
1602 |
|
|
1603 |
|
// MacOS 8.1 serial drivers on startup |
1604 |
< |
} else if (r->nip == ROM_BASE + 0x48c5e0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1605 |
< |
r->nip += 4; |
1604 |
> |
} else if (r->pc() == ROM_BASE + 0x48c5e0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1605 |
> |
r->pc() += 4; |
1606 |
|
return; |
1607 |
< |
} else if (r->nip == ROM_BASE + 0x4a10a0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1608 |
< |
r->nip += 4; |
1607 |
> |
} else if (r->pc() == ROM_BASE + 0x4a10a0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1608 |
> |
r->pc() += 4; |
1609 |
|
return; |
1610 |
|
} |
1611 |
|
|
1612 |
|
// Get opcode and divide into fields |
1613 |
< |
uint32 opcode = *((uint32 *)r->nip); |
1613 |
> |
uint32 opcode = *((uint32 *)r->pc()); |
1614 |
|
uint32 primop = opcode >> 26; |
1615 |
|
uint32 exop = (opcode >> 1) & 0x3ff; |
1616 |
|
uint32 ra = (opcode >> 16) & 0x1f; |
1699 |
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_NORM; break; |
1700 |
|
case 45: // sthu |
1701 |
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_U; break; |
1702 |
+ |
#if EMULATE_UNALIGNED_LOADSTORE_MULTIPLE |
1703 |
+ |
case 46: // lmw |
1704 |
+ |
if ((addr % 4) != 0) { |
1705 |
+ |
uint32 ea = addr; |
1706 |
+ |
D(bug("WARNING: unaligned lmw to EA=%08x from IP=%08x\n", ea, r->pc())); |
1707 |
+ |
for (int i = rd; i <= 31; i++) { |
1708 |
+ |
r->gpr(i) = ReadMacInt32(ea); |
1709 |
+ |
ea += 4; |
1710 |
+ |
} |
1711 |
+ |
r->pc() += 4; |
1712 |
+ |
goto rti; |
1713 |
+ |
} |
1714 |
+ |
break; |
1715 |
+ |
case 47: // stmw |
1716 |
+ |
if ((addr % 4) != 0) { |
1717 |
+ |
uint32 ea = addr; |
1718 |
+ |
D(bug("WARNING: unaligned stmw to EA=%08x from IP=%08x\n", ea, r->pc())); |
1719 |
+ |
for (int i = rd; i <= 31; i++) { |
1720 |
+ |
WriteMacInt32(ea, r->gpr(i)); |
1721 |
+ |
ea += 4; |
1722 |
+ |
} |
1723 |
+ |
r->pc() += 4; |
1724 |
+ |
goto rti; |
1725 |
+ |
} |
1726 |
+ |
break; |
1727 |
+ |
#endif |
1728 |
|
} |
1729 |
|
|
1730 |
< |
// Ignore ROM writes |
1731 |
< |
if (transfer_type == TYPE_STORE && addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) { |
1732 |
< |
// 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)); |
1730 |
> |
// Ignore ROM writes (including to the zero page, which is read-only) |
1731 |
> |
if (transfer_type == TYPE_STORE && |
1732 |
> |
((addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) || |
1733 |
> |
(addr >= SheepMem::ZeroPage() && addr < SheepMem::ZeroPage() + SheepMem::PageSize()))) { |
1734 |
> |
// 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())); |
1735 |
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1736 |
< |
r->gpr[ra] = addr; |
1737 |
< |
r->nip += 4; |
1736 |
> |
r->gpr(ra) = addr; |
1737 |
> |
r->pc() += 4; |
1738 |
|
goto rti; |
1739 |
|
} |
1740 |
|
|
1741 |
|
// Ignore illegal memory accesses? |
1742 |
|
if (PrefsFindBool("ignoresegv")) { |
1743 |
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1744 |
< |
r->gpr[ra] = addr; |
1744 |
> |
r->gpr(ra) = addr; |
1745 |
|
if (transfer_type == TYPE_LOAD) |
1746 |
< |
r->gpr[rd] = 0; |
1747 |
< |
r->nip += 4; |
1746 |
> |
r->gpr(rd) = 0; |
1747 |
> |
r->pc() += 4; |
1748 |
|
goto rti; |
1749 |
|
} |
1750 |
|
|
1752 |
|
if (!PrefsFindBool("nogui")) { |
1753 |
|
char str[256]; |
1754 |
|
if (transfer_type == TYPE_LOAD || transfer_type == TYPE_STORE) |
1755 |
< |
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]); |
1755 |
> |
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)); |
1756 |
|
else |
1757 |
< |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode); |
1757 |
> |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode); |
1758 |
|
ErrorAlert(str); |
1759 |
|
QuitEmulator(); |
1760 |
|
return; |
1762 |
|
} |
1763 |
|
|
1764 |
|
// For all other errors, jump into debugger (sort of...) |
1765 |
+ |
crash_reason = (sig == SIGBUS) ? "SIGBUS" : "SIGSEGV"; |
1766 |
|
if (!ready_for_signals) { |
1767 |
< |
printf("SIGSEGV\n"); |
1768 |
< |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
1767 |
> |
printf("%s\n"); |
1768 |
> |
printf(" sigcontext %p, machine_regs %p\n", scp, r); |
1769 |
|
printf( |
1770 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1771 |
|
" xer %08lx cr %08lx \n" |
1777 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1778 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1779 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1780 |
< |
r->nip, r->link, r->ctr, r->msr, |
1781 |
< |
r->xer, r->ccr, |
1782 |
< |
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1783 |
< |
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
1784 |
< |
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
1785 |
< |
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
1786 |
< |
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
1787 |
< |
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
1788 |
< |
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
1789 |
< |
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
1780 |
> |
crash_reason, |
1781 |
> |
r->pc(), r->lr(), r->ctr(), r->msr(), |
1782 |
> |
r->xer(), r->cr(), |
1783 |
> |
r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3), |
1784 |
> |
r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7), |
1785 |
> |
r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11), |
1786 |
> |
r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15), |
1787 |
> |
r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19), |
1788 |
> |
r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23), |
1789 |
> |
r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27), |
1790 |
> |
r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31)); |
1791 |
|
exit(1); |
1792 |
|
QuitEmulator(); |
1793 |
|
return; |
1794 |
|
} else { |
1795 |
|
// We crashed. Save registers, tell tick thread and loop forever |
1796 |
< |
sigsegv_regs = *(sigregs *)r; |
1796 |
> |
build_sigregs(&sigsegv_regs, r); |
1797 |
|
emul_thread_fatal = true; |
1798 |
|
for (;;) ; |
1799 |
|
} |
1805 |
|
* SIGILL handler |
1806 |
|
*/ |
1807 |
|
|
1808 |
< |
static void sigill_handler(int sig, sigcontext_struct *sc) |
1808 |
> |
static void sigill_handler(int sig, siginfo_t *sip, void *scp) |
1809 |
|
{ |
1810 |
< |
pt_regs *r = sc->regs; |
1810 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1811 |
|
char str[256]; |
1812 |
|
|
1813 |
|
// Fault in Mac ROM or RAM? |
1814 |
< |
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
1814 |
> |
bool mac_fault = (r->pc() >= ROM_BASE) && (r->pc() < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize)); |
1815 |
|
if (mac_fault) { |
1816 |
|
|
1817 |
|
// Get opcode and divide into fields |
1818 |
< |
uint32 opcode = *((uint32 *)r->nip); |
1818 |
> |
uint32 opcode = *((uint32 *)r->pc()); |
1819 |
|
uint32 primop = opcode >> 26; |
1820 |
|
uint32 exop = (opcode >> 1) & 0x3ff; |
1821 |
|
uint32 ra = (opcode >> 16) & 0x1f; |
1826 |
|
switch (primop) { |
1827 |
|
case 9: // POWER instructions |
1828 |
|
case 22: |
1829 |
< |
power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->nip, r->gpr[1], opcode); |
1829 |
> |
power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->pc(), r->gpr(1), opcode); |
1830 |
|
ErrorAlert(str); |
1831 |
|
QuitEmulator(); |
1832 |
|
return; |
1834 |
|
case 31: |
1835 |
|
switch (exop) { |
1836 |
|
case 83: // mfmsr |
1837 |
< |
r->gpr[rd] = 0xf072; |
1838 |
< |
r->nip += 4; |
1837 |
> |
r->gpr(rd) = 0xf072; |
1838 |
> |
r->pc() += 4; |
1839 |
|
goto rti; |
1840 |
|
|
1841 |
|
case 210: // mtsr |
1842 |
|
case 242: // mtsrin |
1843 |
|
case 306: // tlbie |
1844 |
< |
r->nip += 4; |
1844 |
> |
r->pc() += 4; |
1845 |
|
goto rti; |
1846 |
|
|
1847 |
|
case 339: { // mfspr |
1857 |
|
case 957: // PMC3 |
1858 |
|
case 958: // PMC4 |
1859 |
|
case 959: // SDA |
1860 |
< |
r->nip += 4; |
1860 |
> |
r->pc() += 4; |
1861 |
|
goto rti; |
1862 |
|
case 25: // SDR1 |
1863 |
< |
r->gpr[rd] = 0xdead001f; |
1864 |
< |
r->nip += 4; |
1863 |
> |
r->gpr(rd) = 0xdead001f; |
1864 |
> |
r->pc() += 4; |
1865 |
|
goto rti; |
1866 |
|
case 287: // PVR |
1867 |
< |
r->gpr[rd] = PVR; |
1868 |
< |
r->nip += 4; |
1867 |
> |
r->gpr(rd) = PVR; |
1868 |
> |
r->pc() += 4; |
1869 |
|
goto rti; |
1870 |
|
} |
1871 |
|
break; |
1901 |
|
case 957: // PMC3 |
1902 |
|
case 958: // PMC4 |
1903 |
|
case 959: // SDA |
1904 |
< |
r->nip += 4; |
1904 |
> |
r->pc() += 4; |
1905 |
|
goto rti; |
1906 |
|
} |
1907 |
|
break; |
1920 |
|
|
1921 |
|
// In GUI mode, show error alert |
1922 |
|
if (!PrefsFindBool("nogui")) { |
1923 |
< |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode); |
1923 |
> |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode); |
1924 |
|
ErrorAlert(str); |
1925 |
|
QuitEmulator(); |
1926 |
|
return; |
1928 |
|
} |
1929 |
|
|
1930 |
|
// For all other errors, jump into debugger (sort of...) |
1931 |
+ |
crash_reason = "SIGILL"; |
1932 |
|
if (!ready_for_signals) { |
1933 |
< |
printf("SIGILL\n"); |
1934 |
< |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
1933 |
> |
printf("%s\n"); |
1934 |
> |
printf(" sigcontext %p, machine_regs %p\n", scp, r); |
1935 |
|
printf( |
1936 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1937 |
|
" xer %08lx cr %08lx \n" |
1943 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1944 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1945 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1946 |
< |
r->nip, r->link, r->ctr, r->msr, |
1947 |
< |
r->xer, r->ccr, |
1948 |
< |
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1949 |
< |
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
1950 |
< |
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
1951 |
< |
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
1952 |
< |
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
1953 |
< |
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
1954 |
< |
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
1955 |
< |
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
1946 |
> |
crash_reason, |
1947 |
> |
r->pc(), r->lr(), r->ctr(), r->msr(), |
1948 |
> |
r->xer(), r->cr(), |
1949 |
> |
r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3), |
1950 |
> |
r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7), |
1951 |
> |
r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11), |
1952 |
> |
r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15), |
1953 |
> |
r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19), |
1954 |
> |
r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23), |
1955 |
> |
r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27), |
1956 |
> |
r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31)); |
1957 |
|
exit(1); |
1958 |
|
QuitEmulator(); |
1959 |
|
return; |
1960 |
|
} else { |
1961 |
|
// We crashed. Save registers, tell tick thread and loop forever |
1962 |
< |
sigsegv_regs = *(sigregs *)r; |
1962 |
> |
build_sigregs(&sigsegv_regs, r); |
1963 |
|
emul_thread_fatal = true; |
1964 |
|
for (;;) ; |
1965 |
|
} |
1969 |
|
|
1970 |
|
|
1971 |
|
/* |
1972 |
+ |
* Helpers to share 32-bit addressable data with MacOS |
1973 |
+ |
*/ |
1974 |
+ |
|
1975 |
+ |
bool SheepMem::Init(void) |
1976 |
+ |
{ |
1977 |
+ |
// Size of a native page |
1978 |
+ |
page_size = getpagesize(); |
1979 |
+ |
|
1980 |
+ |
// Allocate SheepShaver globals |
1981 |
+ |
if (vm_acquire_fixed((char *)base, size) < 0) |
1982 |
+ |
return false; |
1983 |
+ |
|
1984 |
+ |
// Allocate page with all bits set to 0 |
1985 |
+ |
zero_page = base + size; |
1986 |
+ |
if (vm_acquire_fixed((char *)zero_page, page_size) < 0) |
1987 |
+ |
return false; |
1988 |
+ |
memset((char *)zero_page, 0, page_size); |
1989 |
+ |
if (vm_protect((char *)zero_page, page_size, VM_PAGE_READ) < 0) |
1990 |
+ |
return false; |
1991 |
+ |
|
1992 |
+ |
#if EMULATED_PPC |
1993 |
+ |
// Allocate alternate stack for PowerPC interrupt routine |
1994 |
+ |
sig_stack = zero_page + page_size; |
1995 |
+ |
if (vm_acquire_fixed((char *)sig_stack, SIG_STACK_SIZE) < 0) |
1996 |
+ |
return false; |
1997 |
+ |
#endif |
1998 |
+ |
|
1999 |
+ |
top = base + size; |
2000 |
+ |
return true; |
2001 |
+ |
} |
2002 |
+ |
|
2003 |
+ |
void SheepMem::Exit(void) |
2004 |
+ |
{ |
2005 |
+ |
if (top) { |
2006 |
+ |
// Delete SheepShaver globals |
2007 |
+ |
vm_release((void *)base, size); |
2008 |
+ |
|
2009 |
+ |
// Delete zero page |
2010 |
+ |
vm_release((void *)zero_page, page_size); |
2011 |
+ |
|
2012 |
+ |
#if EMULATED_PPC |
2013 |
+ |
// Delete alternate stack for PowerPC interrupt routine |
2014 |
+ |
vm_release((void *)sig_stack, SIG_STACK_SIZE); |
2015 |
+ |
#endif |
2016 |
+ |
} |
2017 |
+ |
} |
2018 |
+ |
|
2019 |
+ |
|
2020 |
+ |
/* |
2021 |
|
* Display alert |
2022 |
|
*/ |
2023 |
|
|