ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/main_unix.cpp
(Generate patch)

Comparing SheepShaver/src/Unix/main_unix.cpp (file contents):
Revision 1.19 by gbeauche, 2003-12-05T12:41:19Z vs.
Revision 1.39 by gbeauche, 2004-06-17T05:15:25Z

# Line 1 | Line 1
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
# Line 65 | Line 65
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   */
# Line 132 | Line 144
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  
# Line 141 | Line 156
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";
# Line 151 | Line 169 | const uint32 SIG_STACK_SIZE = 0x10000;
169  
170  
171   #if !EMULATED_PPC
154 // Structure in which registers are saved in a signal handler;
155 // sigcontext->regs points to it
156 // (see arch/ppc/kernel/signal.c)
157 typedef struct {
158        uint32 u[4];
159 } __attribute((aligned(16))) vector128;
160 #include <linux/elf.h>
161
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
# Line 175 | Line 265 | uint32 RAMBase;                        // Base address of Mac
265   uint32 RAMSize;                 // Size of Mac RAM
266   uint32 KernelDataAddr;  // Address of Kernel Data
267   uint32 BootGlobsAddr;   // Address of BootGlobs structure at top of Mac RAM
268 + uint32 DRCacheAddr;             // Address of DR Cache
269   uint32 PVR;                             // Theoretical PVR
270   int64 CPUClockSpeed;    // Processor clock speed (Hz)
271   int64 BusClockSpeed;    // Bus clock speed (Hz)
# Line 183 | Line 274 | int64 BusClockSpeed;   // Bus clock speed
274   // Global variables
275   char *x_display_name = NULL;                            // X11 display name
276   Display *x_display = NULL;                                      // X11 display handle
277 + #ifdef X11_LOCK_TYPE
278 + X11_LOCK_TYPE x_display_lock = X11_LOCK_INIT; // X11 display lock
279 + #endif
280  
281   static int zero_fd = 0;                                         // FD of /dev/zero
282   static bool lm_area_mapped = false;                     // Flag: Low Memory area mmap()ped
283   static int kernel_area = -1;                            // SHM ID of Kernel Data area
284   static bool rom_area_mapped = false;            // Flag: Mac ROM mmap()ped
285   static bool ram_area_mapped = false;            // Flag: Mac RAM mmap()ped
286 + static bool dr_cache_area_mapped = false;       // Flag: Mac DR Cache mmap()ped
287 + static bool dr_emulator_area_mapped = false;// Flag: Mac DR Emulator mmap()ped
288   static KernelData *kernel_data;                         // Pointer to Kernel Data
289   static EmulatorData *emulator_data;
290  
# Line 204 | Line 300 | static bool ready_for_signals = false;
300   static int64 num_segv = 0;                                      // Number of handled SEGV signals
301  
302   static struct sigaction sigusr2_action;         // Interrupt signal (of emulator thread)
303 < #if !EMULATED_PPC
303 > #if EMULATED_PPC
304 > static uintptr sig_stack = 0;                           // Stack for PowerPC interrupt routine
305 > #else
306   static struct sigaction sigsegv_action;         // Data access exception signal (of emulator thread)
307   static struct sigaction sigill_action;          // Illegal instruction signal (of emulator thread)
210 static void *sig_stack = NULL;                          // Stack for signal handlers
211 static void *extra_stack = NULL;                        // Stack for SIGSEGV inside interrupt handler
308   static bool emul_thread_fatal = false;          // Flag: MacOS thread crashed, tick thread shall dump debug output
309   static sigregs sigsegv_regs;                            // Register dump when crashed
310 + static const char *crash_reason = NULL;         // Reason of the crash (SIGSEGV, SIGBUS, SIGILL)
311   #endif
312  
313 + uint32  SheepMem::page_size;                            // Size of a native page
314   uintptr SheepMem::zero_page = 0;                        // Address of ro page filled in with zeros
315   uintptr SheepMem::base = 0x60000000;            // Address of SheepShaver data
316   uintptr SheepMem::top = 0;                                      // Top of SheepShaver data (stack like storage)
# Line 224 | Line 322 | static void *emul_func(void *arg);
322   static void *nvram_func(void *arg);
323   static void *tick_func(void *arg);
324   #if EMULATED_PPC
227 static void sigusr2_handler(int sig);
325   extern void emul_ppc(uint32 start);
326   extern void init_emul_ppc(void);
327   extern void exit_emul_ppc(void);
328   #else
329 < static void sigusr2_handler(int sig, sigcontext_struct *sc);
330 < static void sigsegv_handler(int sig, sigcontext_struct *sc);
331 < static void sigill_handler(int sig, sigcontext_struct *sc);
329 > static void sigusr2_handler(int sig, siginfo_t *sip, void *scp);
330 > static void sigsegv_handler(int sig, siginfo_t *sip, void *scp);
331 > static void sigill_handler(int sig, siginfo_t *sip, void *scp);
332   #endif
333  
334  
# Line 253 | Line 350 | extern void paranoia_check(void);
350  
351   #if EMULATED_PPC
352   /*
353 + *  Return signal stack base
354 + */
355 +
356 + uintptr SignalStackBase(void)
357 + {
358 +        return sig_stack + SIG_STACK_SIZE;
359 + }
360 +
361 +
362 + /*
363   *  Atomic operations
364   */
365  
# Line 376 | Line 483 | int main(int argc, char **argv)
483          PVR = 0x00040000;                       // Default: 604
484          CPUClockSpeed = 100000000;      // Default: 100MHz
485          BusClockSpeed = 100000000;      // Default: 100MHz
486 < #if !EMULATED_PPC
486 > #if EMULATED_PPC
487 >        PVR = 0x000c0000;                       // Default: 7400 (with AltiVec)
488 > #elif defined(__APPLE__) && defined(__MACH__)
489 >        proc_file = popen("ioreg -c IOPlatformDevice", "r");
490 >        if (proc_file) {
491 >                char line[256];
492 >                bool powerpc_node = false;
493 >                while (fgets(line, sizeof(line) - 1, proc_file)) {
494 >                        // Read line
495 >                        int len = strlen(line);
496 >                        if (len == 0)
497 >                                continue;
498 >                        line[len - 1] = 0;
499 >
500 >                        // Parse line
501 >                        if (strstr(line, "o PowerPC,"))
502 >                                powerpc_node = true;
503 >                        else if (powerpc_node) {
504 >                                uint32 value;
505 >                                char head[256];
506 >                                if (sscanf(line, "%[ |]\"cpu-version\" = <%x>", head, &value) == 2)
507 >                                        PVR = value;
508 >                                else if (sscanf(line, "%[ |]\"clock-frequency\" = <%x>", head, &value) == 2)
509 >                                        CPUClockSpeed = value;
510 >                                else if (sscanf(line, "%[ |]\"bus-frequency\" = <%x>", head, &value) == 2)
511 >                                        BusClockSpeed = value;
512 >                                else if (strchr(line, '}'))
513 >                                        powerpc_node = false;
514 >                        }
515 >                }
516 >                fclose(proc_file);
517 >        } else {
518 >                sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno));
519 >                WarningAlert(str);
520 >        }
521 > #else
522          proc_file = fopen("/proc/cpuinfo", "r");
523          if (proc_file) {
524                  char line[256];
# Line 390 | Line 532 | int main(int argc, char **argv)
532                          // Parse line
533                          int i;
534                          char value[256];
535 <                        if (sscanf(line, "cpu : %s", value) == 1) {
535 >                        if (sscanf(line, "cpu : %[0-9A-Za-a]", value) == 1) {
536                                  if (strcmp(value, "601") == 0)
537                                          PVR = 0x00010000;
538                                  else if (strcmp(value, "603") == 0)
# Line 411 | Line 553 | int main(int argc, char **argv)
553                                          PVR = 0x00320000;
554                                  else if (strcmp(value, "860") == 0)
555                                          PVR = 0x00500000;
556 +                                else if (strcmp(value, "7400") == 0)
557 +                                        PVR = 0x000c0000;
558 +                                else if (strcmp(value, "7410") == 0)
559 +                                        PVR = 0x800c0000;
560                                  else
561                                          printf("WARNING: Unknown CPU type '%s', assuming 604\n", value);
562                          }
# Line 422 | Line 568 | int main(int argc, char **argv)
568                  sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno));
569                  WarningAlert(str);
570          }
571 +
572 +        // Get actual bus frequency
573 +        proc_file = fopen("/proc/device-tree/clock-frequency", "r");
574 +        if (proc_file) {
575 +                union { uint8 b[4]; uint32 l; } value;
576 +                if (fread(value.b, sizeof(value), 1, proc_file) == 1)
577 +                        BusClockSpeed = value.l;
578 +                fclose(proc_file);
579 +        }
580   #endif
581          D(bug("PVR: %08x (assumed)\n", PVR));
582  
# Line 446 | Line 601 | int main(int argc, char **argv)
601                  goto quit;
602          }
603  
604 + #ifndef PAGEZERO_HACK
605          // Create Low Memory area (0x0000..0x3000)
606          if (vm_acquire_fixed((char *)0, 0x3000) < 0) {
607                  sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno));
# Line 453 | Line 609 | int main(int argc, char **argv)
609                  goto quit;
610          }
611          lm_area_mapped = true;
612 + #endif
613  
614          // Create areas for Kernel Data
615          kernel_area = shmget(IPC_PRIVATE, KERNEL_AREA_SIZE, 0600);
# Line 476 | Line 633 | int main(int argc, char **argv)
633          KernelDataAddr = KERNEL_DATA_BASE;
634          D(bug("Kernel Data at %p, Emulator Data at %p\n", kernel_data, emulator_data));
635  
636 +        // Create area for DR Cache
637 +        if (vm_acquire_fixed((void *)DR_EMULATOR_BASE, DR_EMULATOR_SIZE) < 0) {
638 +                sprintf(str, GetString(STR_DR_EMULATOR_MMAP_ERR), strerror(errno));
639 +                ErrorAlert(str);
640 +                goto quit;
641 +        }
642 +        dr_emulator_area_mapped = true;
643 +        if (vm_acquire_fixed((void *)DR_CACHE_BASE, DR_CACHE_SIZE) < 0) {
644 +                sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno));
645 +                ErrorAlert(str);
646 +                goto quit;
647 +        }
648 +        dr_cache_area_mapped = true;
649 + #if !EMULATED_PPC
650 +        if (vm_protect((char *)DR_CACHE_BASE, DR_CACHE_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) {
651 +                sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno));
652 +                ErrorAlert(str);
653 +                goto quit;
654 +        }
655 + #endif
656 +        DRCacheAddr = DR_CACHE_BASE;
657 +        D(bug("DR Cache at %p\n", DRCacheAddr));
658 +
659          // Create area for SheepShaver data
660          if (!SheepMem::Init()) {
661                  sprintf(str, GetString(STR_SHEEP_MEM_MMAP_ERR), strerror(errno));
# Line 489 | Line 669 | int main(int argc, char **argv)
669                  ErrorAlert(str);
670                  goto quit;
671          }
672 < #if !EMULATED_PPC || defined(__powerpc__)
672 > #if !EMULATED_PPC
673          if (vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) {
674                  sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno));
675                  ErrorAlert(str);
# Line 559 | Line 739 | int main(int argc, char **argv)
739          // Load NVRAM
740          XPRAMInit();
741  
742 +        // Load XPRAM default values if signature not found
743 +        if (XPRAM[0x130c] != 0x4e || XPRAM[0x130d] != 0x75
744 +         || XPRAM[0x130e] != 0x4d || XPRAM[0x130f] != 0x63) {
745 +                D(bug("Loading XPRAM default values\n"));
746 +                memset(XPRAM + 0x1300, 0, 0x100);
747 +                XPRAM[0x130c] = 0x4e;   // "NuMc" signature
748 +                XPRAM[0x130d] = 0x75;
749 +                XPRAM[0x130e] = 0x4d;
750 +                XPRAM[0x130f] = 0x63;
751 +                XPRAM[0x1301] = 0x80;   // InternalWaitFlags = DynWait (don't wait for SCSI devices upon bootup)
752 +                XPRAM[0x1310] = 0xa8;   // Standard PRAM values
753 +                XPRAM[0x1311] = 0x00;
754 +                XPRAM[0x1312] = 0x00;
755 +                XPRAM[0x1313] = 0x22;
756 +                XPRAM[0x1314] = 0xcc;
757 +                XPRAM[0x1315] = 0x0a;
758 +                XPRAM[0x1316] = 0xcc;
759 +                XPRAM[0x1317] = 0x0a;
760 +                XPRAM[0x131c] = 0x00;
761 +                XPRAM[0x131d] = 0x02;
762 +                XPRAM[0x131e] = 0x63;
763 +                XPRAM[0x131f] = 0x00;
764 +                XPRAM[0x1308] = 0x13;
765 +                XPRAM[0x1309] = 0x88;
766 +                XPRAM[0x130a] = 0x00;
767 +                XPRAM[0x130b] = 0xcc;
768 +                XPRAM[0x1376] = 0x00;   // OSDefault = MacOS
769 +                XPRAM[0x1377] = 0x01;
770 +        }
771 +
772          // Set boot volume
773          i16 = PrefsFindInt32("bootdrive");
774          XPRAM[0x1378] = i16 >> 8;
# Line 589 | Line 799 | int main(int argc, char **argv)
799          // Init external file system
800          ExtFSInit();
801  
802 +        // Init ADB
803 +        ADBInit();
804 +
805          // Init audio
806          AudioInit();
807  
# Line 646 | Line 859 | int main(int argc, char **argv)
859                  kernel_data->v[0xc50 >> 2] = htonl(RAMBase);
860                  kernel_data->v[0xc54 >> 2] = htonl(RAMSize);
861                  kernel_data->v[0xf60 >> 2] = htonl(PVR);
862 <                kernel_data->v[0xf64 >> 2] = htonl(CPUClockSpeed);
863 <                kernel_data->v[0xf68 >> 2] = htonl(BusClockSpeed);
864 <                kernel_data->v[0xf6c >> 2] = htonl(CPUClockSpeed);
862 >                kernel_data->v[0xf64 >> 2] = htonl(CPUClockSpeed);                      // clock-frequency
863 >                kernel_data->v[0xf68 >> 2] = htonl(BusClockSpeed);                      // bus-frequency
864 >                kernel_data->v[0xf6c >> 2] = htonl(BusClockSpeed / 4);          // timebase-frequency
865          } else {
866                  kernel_data->v[0xc80 >> 2] = htonl(RAMSize);
867                  kernel_data->v[0xc84 >> 2] = htonl(RAMSize);
# Line 660 | Line 873 | int main(int argc, char **argv)
873                  kernel_data->v[0xcb0 >> 2] = htonl(RAMBase);
874                  kernel_data->v[0xcb4 >> 2] = htonl(RAMSize);
875                  kernel_data->v[0xf80 >> 2] = htonl(PVR);
876 <                kernel_data->v[0xf84 >> 2] = htonl(CPUClockSpeed);
877 <                kernel_data->v[0xf88 >> 2] = htonl(BusClockSpeed);
878 <                kernel_data->v[0xf8c >> 2] = htonl(CPUClockSpeed);
876 >                kernel_data->v[0xf84 >> 2] = htonl(CPUClockSpeed);                      // clock-frequency
877 >                kernel_data->v[0xf88 >> 2] = htonl(BusClockSpeed);                      // bus-frequency
878 >                kernel_data->v[0xf8c >> 2] = htonl(BusClockSpeed / 4);          // timebase-frequency
879          }
880  
881          // Initialize extra low memory
# Line 697 | Line 910 | int main(int argc, char **argv)
910  
911   #if !EMULATED_PPC
912          // Create and install stacks for signal handlers
913 <        sig_stack = malloc(SIG_STACK_SIZE);
914 <        D(bug("Signal stack at %p\n", sig_stack));
915 <        if (sig_stack == NULL) {
916 <                ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR));
917 <                goto quit;
918 <        }
919 <        extra_stack = malloc(SIG_STACK_SIZE);
920 <        D(bug("Extra stack at %p\n", extra_stack));
921 <        if (extra_stack == NULL) {
922 <                ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR));
923 <                goto quit;
924 <        }
925 <        struct sigaltstack new_stack;
713 <        new_stack.ss_sp = sig_stack;
714 <        new_stack.ss_flags = 0;
715 <        new_stack.ss_size = SIG_STACK_SIZE;
716 <        if (sigaltstack(&new_stack, NULL) < 0) {
913 >        for (int i = 0; i < SIG_STACK_COUNT; i++) {
914 >                void *sig_stack = malloc(SIG_STACK_SIZE);
915 >                D(bug("Signal stack %d at %p\n", i, sig_stack));
916 >                if (sig_stack == NULL) {
917 >                        ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR));
918 >                        goto quit;
919 >                }
920 >                sig_stacks[i].ss_sp = sig_stack;
921 >                sig_stacks[i].ss_flags = 0;
922 >                sig_stacks[i].ss_size = SIG_STACK_SIZE;
923 >        }
924 >        sig_stack_id = 0;
925 >        if (sigaltstack(&sig_stacks[0], NULL) < 0) {
926                  sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno));
927                  ErrorAlert(str);
928                  goto quit;
# Line 721 | Line 930 | int main(int argc, char **argv)
930   #endif
931  
932   #if !EMULATED_PPC
933 <        // Install SIGSEGV handler
933 >        // Install SIGSEGV and SIGBUS handlers
934          sigemptyset(&sigsegv_action.sa_mask);   // Block interrupts during SEGV handling
935          sigaddset(&sigsegv_action.sa_mask, SIGUSR2);
936 <        sigsegv_action.sa_handler = (__sighandler_t)sigsegv_handler;
937 <        sigsegv_action.sa_flags = SA_ONSTACK;
936 >        sigsegv_action.sa_sigaction = sigsegv_handler;
937 >        sigsegv_action.sa_flags = SA_ONSTACK | SA_SIGINFO;
938 > #ifdef HAVE_SIGNAL_SA_RESTORER
939          sigsegv_action.sa_restorer = NULL;
940 + #endif
941          if (sigaction(SIGSEGV, &sigsegv_action, NULL) < 0) {
942                  sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno));
943                  ErrorAlert(str);
944                  goto quit;
945          }
946 +        if (sigaction(SIGBUS, &sigsegv_action, NULL) < 0) {
947 +                sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno));
948 +                ErrorAlert(str);
949 +                goto quit;
950 +        }
951  
952          // Install SIGILL handler
953          sigemptyset(&sigill_action.sa_mask);    // Block interrupts during ILL handling
954          sigaddset(&sigill_action.sa_mask, SIGUSR2);
955 <        sigill_action.sa_handler = (__sighandler_t)sigill_handler;
956 <        sigill_action.sa_flags = SA_ONSTACK;
955 >        sigill_action.sa_sigaction = sigill_handler;
956 >        sigill_action.sa_flags = SA_ONSTACK | SA_SIGINFO;
957 > #ifdef HAVE_SIGNAL_SA_RESTORER
958          sigill_action.sa_restorer = NULL;
959 + #endif
960          if (sigaction(SIGILL, &sigill_action, NULL) < 0) {
961                  sprintf(str, GetString(STR_SIGILL_INSTALL_ERR), strerror(errno));
962                  ErrorAlert(str);
# Line 746 | Line 964 | int main(int argc, char **argv)
964          }
965   #endif
966  
967 + #if !EMULATED_PPC
968          // Install interrupt signal handler
969          sigemptyset(&sigusr2_action.sa_mask);
970 <        sigusr2_action.sa_handler = (__sighandler_t)sigusr2_handler;
971 <        sigusr2_action.sa_flags = 0;
972 < #if !EMULATED_PPC
754 <        sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART;
755 < #endif
970 >        sigusr2_action.sa_sigaction = sigusr2_handler;
971 >        sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART | SA_SIGINFO;
972 > #ifdef HAVE_SIGNAL_SA_RESTORER
973          sigusr2_action.sa_restorer = NULL;
974 + #endif
975          if (sigaction(SIGUSR2, &sigusr2_action, NULL) < 0) {
976                  sprintf(str, GetString(STR_SIGUSR2_INSTALL_ERR), strerror(errno));
977                  ErrorAlert(str);
978                  goto quit;
979          }
980 + #endif
981  
982          // Get my thread ID and execute MacOS thread function
983          emul_thread = pthread_self();
# Line 795 | Line 1014 | static void Quit(void)
1014          }
1015  
1016   #if !EMULATED_PPC
1017 <        // Uninstall SIGSEGV handler
1017 >        // Uninstall SIGSEGV and SIGBUS handlers
1018          sigemptyset(&sigsegv_action.sa_mask);
1019          sigsegv_action.sa_handler = SIG_DFL;
1020          sigsegv_action.sa_flags = 0;
1021          sigaction(SIGSEGV, &sigsegv_action, NULL);
1022 +        sigaction(SIGBUS, &sigsegv_action, NULL);
1023  
1024          // Uninstall SIGILL handler
1025          sigemptyset(&sigill_action.sa_mask);
1026          sigill_action.sa_handler = SIG_DFL;
1027          sigill_action.sa_flags = 0;
1028          sigaction(SIGILL, &sigill_action, NULL);
1029 +
1030 +        // Delete stacks for signal handlers
1031 +        for (int i = 0; i < SIG_STACK_COUNT; i++) {
1032 +                void *sig_stack = sig_stacks[i].ss_sp;
1033 +                if (sig_stack)
1034 +                        free(sig_stack);
1035 +        }
1036   #endif
1037  
1038          // Save NVRAM
# Line 826 | Line 1053 | static void Quit(void)
1053          // Exit audio
1054          AudioExit();
1055  
1056 +        // Exit ADB
1057 +        ADBExit();
1058 +
1059          // Exit video
1060          VideoExit();
1061  
# Line 838 | Line 1068 | static void Quit(void)
1068          DiskExit();
1069          SonyExit();
1070  
1071 +        // Delete thunks
1072 +        ThunksExit();
1073 +
1074          // Delete SheepShaver globals
1075          SheepMem::Exit();
1076  
# Line 849 | Line 1082 | static void Quit(void)
1082          if (rom_area_mapped)
1083                  vm_release((char *)ROM_BASE, ROM_AREA_SIZE);
1084  
1085 +        // Delete DR cache areas
1086 +        if (dr_emulator_area_mapped)
1087 +                vm_release((void *)DR_EMULATOR_BASE, DR_EMULATOR_SIZE);
1088 +        if (dr_cache_area_mapped)
1089 +                vm_release((void *)DR_CACHE_BASE, DR_CACHE_SIZE);
1090 +
1091          // Delete Kernel Data area
1092          if (kernel_area >= 0) {
1093                  shmdt((void *)KERNEL_DATA_BASE);
# Line 1075 | Line 1314 | static void *tick_func(void *arg)
1314                  if (emul_thread_fatal) {
1315  
1316                          // Yes, dump registers
1317 <                        pt_regs *r = (pt_regs *)&sigsegv_regs;
1317 >                        sigregs *r = &sigsegv_regs;
1318                          char str[256];
1319 <                        sprintf(str, "SIGSEGV\n"
1319 >                        if (crash_reason == NULL)
1320 >                                crash_reason = "SIGSEGV";
1321 >                        sprintf(str, "%s\n"
1322                                  "   pc %08lx     lr %08lx    ctr %08lx    msr %08lx\n"
1323                                  "  xer %08lx     cr %08lx  \n"
1324                                  "   r0 %08lx     r1 %08lx     r2 %08lx     r3 %08lx\n"
# Line 1088 | Line 1329 | static void *tick_func(void *arg)
1329                                  "  r20 %08lx    r21 %08lx    r22 %08lx    r23 %08lx\n"
1330                                  "  r24 %08lx    r25 %08lx    r26 %08lx    r27 %08lx\n"
1331                                  "  r28 %08lx    r29 %08lx    r30 %08lx    r31 %08lx\n",
1332 +                                crash_reason,
1333                                  r->nip, r->link, r->ctr, r->msr,
1334                                  r->xer, r->ccr,
1335                                  r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3],
# Line 1243 | Line 1485 | void B2_delete_mutex(B2_mutex *mutex)
1485   *  Trigger signal USR2 from another thread
1486   */
1487  
1488 < #if !EMULATED_PPC || ASYNC_IRQ
1488 > #if !EMULATED_PPC
1489   void TriggerInterrupt(void)
1490   {
1491          if (ready_for_signals)
# Line 1293 | Line 1535 | void EnableInterrupt(void)
1535   *  USR2 handler
1536   */
1537  
1538 < #if EMULATED_PPC
1539 < static void sigusr2_handler(int sig)
1298 < {
1299 < #if ASYNC_IRQ
1300 <        extern void HandleInterrupt(void);
1301 <        HandleInterrupt();
1302 < #endif
1303 < }
1304 < #else
1305 < static void sigusr2_handler(int sig, sigcontext_struct *sc)
1538 > #if !EMULATED_PPC
1539 > static void sigusr2_handler(int sig, siginfo_t *sip, void *scp)
1540   {
1541 <        pt_regs *r = sc->regs;
1541 >        machine_regs *r = MACHINE_REGISTERS(scp);
1542  
1543          // Do nothing if interrupts are disabled
1544          if (*(int32 *)XLM_IRQ_NEST > 0)
# Line 1318 | Line 1552 | static void sigusr2_handler(int sig, sig
1552                  case MODE_68K:
1553                          // 68k emulator active, trigger 68k interrupt level 1
1554                          WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1);
1555 <                        r->ccr |= ntohl(kernel_data->v[0x674 >> 2]);
1555 >                        r->cr() |= ntohl(kernel_data->v[0x674 >> 2]);
1556                          break;
1557  
1558   #if INTERRUPTS_IN_NATIVE_MODE
1559                  case MODE_NATIVE:
1560                          // 68k emulator inactive, in nanokernel?
1561 <                        if (r->gpr[1] != KernelDataAddr) {
1561 >                        if (r->gpr(1) != KernelDataAddr) {
1562 >
1563 >                                // Set extra stack for nested interrupts
1564 >                                sig_stack_acquire();
1565 >                                
1566                                  // Prepare for 68k interrupt level 1
1567                                  WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1);
1568                                  WriteMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc, ReadMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc) | ntohl(kernel_data->v[0x674 >> 2]));
1569  
1570                                  // Execute nanokernel interrupt routine (this will activate the 68k emulator)
1571 <                                atomic_add((int32 *)XLM_IRQ_NEST, 1);
1571 >                                DisableInterrupt();
1572                                  if (ROMType == ROMTYPE_NEWWORLD)
1573                                          ppc_interrupt(ROM_BASE + 0x312b1c, KernelDataAddr);
1574                                  else
1575                                          ppc_interrupt(ROM_BASE + 0x312a3c, KernelDataAddr);
1576 +
1577 +                                // Reset normal signal stack
1578 +                                sig_stack_release();
1579                          }
1580                          break;
1581   #endif
# Line 1345 | Line 1586 | static void sigusr2_handler(int sig, sig
1586                          if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) {
1587  
1588                                  // Set extra stack for SIGSEGV handler
1589 <                                struct sigaltstack new_stack;
1349 <                                new_stack.ss_sp = extra_stack;
1350 <                                new_stack.ss_flags = 0;
1351 <                                new_stack.ss_size = SIG_STACK_SIZE;
1352 <                                sigaltstack(&new_stack, NULL);
1589 >                                sig_stack_acquire();
1590   #if 1
1591                                  // Execute full 68k interrupt routine
1592                                  M68kRegisters r;
# Line 1376 | Line 1613 | static void sigusr2_handler(int sig, sig
1613                                  }
1614   #endif
1615                                  // Reset normal signal stack
1616 <                                new_stack.ss_sp = sig_stack;
1380 <                                new_stack.ss_flags = 0;
1381 <                                new_stack.ss_size = SIG_STACK_SIZE;
1382 <                                sigaltstack(&new_stack, NULL);
1616 >                                sig_stack_release();
1617                          }
1618                          break;
1619   #endif
# Line 1393 | Line 1627 | static void sigusr2_handler(int sig, sig
1627   */
1628  
1629   #if !EMULATED_PPC
1630 < static void sigsegv_handler(int sig, sigcontext_struct *sc)
1630 > static void sigsegv_handler(int sig, siginfo_t *sip, void *scp)
1631   {
1632 <        pt_regs *r = sc->regs;
1632 >        machine_regs *r = MACHINE_REGISTERS(scp);
1633  
1634          // Get effective address
1635 <        uint32 addr = r->dar;
1635 >        uint32 addr = r->dar();
1636          
1637   #if ENABLE_VOSF
1638          // Handle screen fault.
1639          extern bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction);
1640 <        if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->nip))
1640 >        if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->pc()))
1641                  return;
1642   #endif
1643  
1644          num_segv++;
1645  
1646 <        // Fault in Mac ROM or RAM?
1647 <        bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize));
1646 >        // Fault in Mac ROM or RAM or DR Cache?
1647 >        bool mac_fault = (r->pc() >= ROM_BASE) && (r->pc() < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize)) || (r->pc() >= DR_CACHE_BASE && r->pc() < (DR_CACHE_BASE + DR_CACHE_SIZE));
1648          if (mac_fault) {
1649  
1650                  // "VM settings" during MacOS 8 installation
1651 <                if (r->nip == ROM_BASE + 0x488160 && r->gpr[20] == 0xf8000000) {
1652 <                        r->nip += 4;
1653 <                        r->gpr[8] = 0;
1651 >                if (r->pc() == ROM_BASE + 0x488160 && r->gpr(20) == 0xf8000000) {
1652 >                        r->pc() += 4;
1653 >                        r->gpr(8) = 0;
1654                          return;
1655          
1656                  // MacOS 8.5 installation
1657 <                } else if (r->nip == ROM_BASE + 0x488140 && r->gpr[16] == 0xf8000000) {
1658 <                        r->nip += 4;
1659 <                        r->gpr[8] = 0;
1657 >                } else if (r->pc() == ROM_BASE + 0x488140 && r->gpr(16) == 0xf8000000) {
1658 >                        r->pc() += 4;
1659 >                        r->gpr(8) = 0;
1660                          return;
1661          
1662                  // MacOS 8 serial drivers on startup
1663 <                } else if (r->nip == ROM_BASE + 0x48e080 && (r->gpr[8] == 0xf3012002 || r->gpr[8] == 0xf3012000)) {
1664 <                        r->nip += 4;
1665 <                        r->gpr[8] = 0;
1663 >                } else if (r->pc() == ROM_BASE + 0x48e080 && (r->gpr(8) == 0xf3012002 || r->gpr(8) == 0xf3012000)) {
1664 >                        r->pc() += 4;
1665 >                        r->gpr(8) = 0;
1666                          return;
1667          
1668                  // MacOS 8.1 serial drivers on startup
1669 <                } else if (r->nip == ROM_BASE + 0x48c5e0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) {
1670 <                        r->nip += 4;
1669 >                } else if (r->pc() == ROM_BASE + 0x48c5e0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) {
1670 >                        r->pc() += 4;
1671 >                        return;
1672 >                } else if (r->pc() == ROM_BASE + 0x4a10a0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) {
1673 >                        r->pc() += 4;
1674                          return;
1675 <                } else if (r->nip == ROM_BASE + 0x4a10a0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) {
1676 <                        r->nip += 4;
1675 >        
1676 >                // MacOS 8.6 serial drivers on startup (with DR Cache and OldWorld ROM)
1677 >                } else if ((r->pc() - DR_CACHE_BASE) < DR_CACHE_SIZE && (r->gpr(16) == 0xf3012002 || r->gpr(16) == 0xf3012000)) {
1678 >                        r->pc() += 4;
1679 >                        return;
1680 >                } else if ((r->pc() - DR_CACHE_BASE) < DR_CACHE_SIZE && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) {
1681 >                        r->pc() += 4;
1682                          return;
1683                  }
1684  
1685                  // Get opcode and divide into fields
1686 <                uint32 opcode = *((uint32 *)r->nip);
1686 >                uint32 opcode = *((uint32 *)r->pc());
1687                  uint32 primop = opcode >> 26;
1688                  uint32 exop = (opcode >> 1) & 0x3ff;
1689                  uint32 ra = (opcode >> 16) & 0x1f;
# Line 1530 | Line 1772 | static void sigsegv_handler(int sig, sig
1772                                  transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_NORM; break;
1773                          case 45:        // sthu
1774                                  transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_U; break;
1775 + #if EMULATE_UNALIGNED_LOADSTORE_MULTIPLE
1776 +                        case 46:        // lmw
1777 +                                if ((addr % 4) != 0) {
1778 +                                        uint32 ea = addr;
1779 +                                        D(bug("WARNING: unaligned lmw to EA=%08x from IP=%08x\n", ea, r->pc()));
1780 +                                        for (int i = rd; i <= 31; i++) {
1781 +                                                r->gpr(i) = ReadMacInt32(ea);
1782 +                                                ea += 4;
1783 +                                        }
1784 +                                        r->pc() += 4;
1785 +                                        goto rti;
1786 +                                }
1787 +                                break;
1788 +                        case 47:        // stmw
1789 +                                if ((addr % 4) != 0) {
1790 +                                        uint32 ea = addr;
1791 +                                        D(bug("WARNING: unaligned stmw to EA=%08x from IP=%08x\n", ea, r->pc()));
1792 +                                        for (int i = rd; i <= 31; i++) {
1793 +                                                WriteMacInt32(ea, r->gpr(i));
1794 +                                                ea += 4;
1795 +                                        }
1796 +                                        r->pc() += 4;
1797 +                                        goto rti;
1798 +                                }
1799 +                                break;
1800 + #endif
1801                  }
1802          
1803 <                // Ignore ROM writes
1804 <                if (transfer_type == TYPE_STORE && addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) {
1805 < //                      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));
1803 >                // Ignore ROM writes (including to the zero page, which is read-only)
1804 >                if (transfer_type == TYPE_STORE &&
1805 >                        ((addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) ||
1806 >                         (addr >= SheepMem::ZeroPage() && addr < SheepMem::ZeroPage() + SheepMem::PageSize()))) {
1807 > //                      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()));
1808                          if (addr_mode == MODE_U || addr_mode == MODE_UX)
1809 <                                r->gpr[ra] = addr;
1810 <                        r->nip += 4;
1809 >                                r->gpr(ra) = addr;
1810 >                        r->pc() += 4;
1811                          goto rti;
1812                  }
1813  
1814                  // Ignore illegal memory accesses?
1815                  if (PrefsFindBool("ignoresegv")) {
1816                          if (addr_mode == MODE_U || addr_mode == MODE_UX)
1817 <                                r->gpr[ra] = addr;
1817 >                                r->gpr(ra) = addr;
1818                          if (transfer_type == TYPE_LOAD)
1819 <                                r->gpr[rd] = 0;
1820 <                        r->nip += 4;
1819 >                                r->gpr(rd) = 0;
1820 >                        r->pc() += 4;
1821                          goto rti;
1822                  }
1823  
# Line 1555 | Line 1825 | static void sigsegv_handler(int sig, sig
1825                  if (!PrefsFindBool("nogui")) {
1826                          char str[256];
1827                          if (transfer_type == TYPE_LOAD || transfer_type == TYPE_STORE)
1828 <                                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]);
1828 >                                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));
1829                          else
1830 <                                sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode);
1830 >                                sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode);
1831                          ErrorAlert(str);
1832                          QuitEmulator();
1833                          return;
# Line 1565 | Line 1835 | static void sigsegv_handler(int sig, sig
1835          }
1836  
1837          // For all other errors, jump into debugger (sort of...)
1838 +        crash_reason = (sig == SIGBUS) ? "SIGBUS" : "SIGSEGV";
1839          if (!ready_for_signals) {
1840 <                printf("SIGSEGV\n");
1841 <                printf(" sigcontext %p, pt_regs %p\n", sc, r);
1840 >                printf("%s\n");
1841 >                printf(" sigcontext %p, machine_regs %p\n", scp, r);
1842                  printf(
1843                          "   pc %08lx     lr %08lx    ctr %08lx    msr %08lx\n"
1844                          "  xer %08lx     cr %08lx  \n"
# Line 1579 | Line 1850 | static void sigsegv_handler(int sig, sig
1850                          "  r20 %08lx    r21 %08lx    r22 %08lx    r23 %08lx\n"
1851                          "  r24 %08lx    r25 %08lx    r26 %08lx    r27 %08lx\n"
1852                          "  r28 %08lx    r29 %08lx    r30 %08lx    r31 %08lx\n",
1853 <                        r->nip, r->link, r->ctr, r->msr,
1854 <                        r->xer, r->ccr,
1855 <                        r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3],
1856 <                        r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7],
1857 <                        r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11],
1858 <                        r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15],
1859 <                        r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19],
1860 <                        r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23],
1861 <                        r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27],
1862 <                        r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]);
1853 >                        crash_reason,
1854 >                        r->pc(), r->lr(), r->ctr(), r->msr(),
1855 >                        r->xer(), r->cr(),
1856 >                        r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3),
1857 >                        r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7),
1858 >                        r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11),
1859 >                        r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15),
1860 >                        r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19),
1861 >                        r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23),
1862 >                        r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27),
1863 >                        r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31));
1864                  exit(1);
1865                  QuitEmulator();
1866                  return;
1867          } else {
1868                  // We crashed. Save registers, tell tick thread and loop forever
1869 <                sigsegv_regs = *(sigregs *)r;
1869 >                build_sigregs(&sigsegv_regs, r);
1870                  emul_thread_fatal = true;
1871                  for (;;) ;
1872          }
# Line 1606 | Line 1878 | rti:;
1878   *  SIGILL handler
1879   */
1880  
1881 < static void sigill_handler(int sig, sigcontext_struct *sc)
1881 > static void sigill_handler(int sig, siginfo_t *sip, void *scp)
1882   {
1883 <        pt_regs *r = sc->regs;
1883 >        machine_regs *r = MACHINE_REGISTERS(scp);
1884          char str[256];
1885  
1886          // Fault in Mac ROM or RAM?
1887 <        bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize));
1887 >        bool mac_fault = (r->pc() >= ROM_BASE) && (r->pc() < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize));
1888          if (mac_fault) {
1889  
1890                  // Get opcode and divide into fields
1891 <                uint32 opcode = *((uint32 *)r->nip);
1891 >                uint32 opcode = *((uint32 *)r->pc());
1892                  uint32 primop = opcode >> 26;
1893                  uint32 exop = (opcode >> 1) & 0x3ff;
1894                  uint32 ra = (opcode >> 16) & 0x1f;
# Line 1627 | Line 1899 | static void sigill_handler(int sig, sigc
1899                  switch (primop) {
1900                          case 9:         // POWER instructions
1901                          case 22:
1902 < power_inst:             sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->nip, r->gpr[1], opcode);
1902 > power_inst:             sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->pc(), r->gpr(1), opcode);
1903                                  ErrorAlert(str);
1904                                  QuitEmulator();
1905                                  return;
# Line 1635 | Line 1907 | power_inst:            sprintf(str, GetString(STR_
1907                          case 31:
1908                                  switch (exop) {
1909                                          case 83:        // mfmsr
1910 <                                                r->gpr[rd] = 0xf072;
1911 <                                                r->nip += 4;
1910 >                                                r->gpr(rd) = 0xf072;
1911 >                                                r->pc() += 4;
1912                                                  goto rti;
1913  
1914                                          case 210:       // mtsr
1915                                          case 242:       // mtsrin
1916                                          case 306:       // tlbie
1917 <                                                r->nip += 4;
1917 >                                                r->pc() += 4;
1918                                                  goto rti;
1919  
1920                                          case 339: {     // mfspr
# Line 1658 | Line 1930 | power_inst:            sprintf(str, GetString(STR_
1930                                                          case 957:       // PMC3
1931                                                          case 958:       // PMC4
1932                                                          case 959:       // SDA
1933 <                                                                r->nip += 4;
1933 >                                                                r->pc() += 4;
1934                                                                  goto rti;
1935                                                          case 25:        // SDR1
1936 <                                                                r->gpr[rd] = 0xdead001f;
1937 <                                                                r->nip += 4;
1936 >                                                                r->gpr(rd) = 0xdead001f;
1937 >                                                                r->pc() += 4;
1938                                                                  goto rti;
1939                                                          case 287:       // PVR
1940 <                                                                r->gpr[rd] = PVR;
1941 <                                                                r->nip += 4;
1940 >                                                                r->gpr(rd) = PVR;
1941 >                                                                r->pc() += 4;
1942                                                                  goto rti;
1943                                                  }
1944                                                  break;
# Line 1702 | Line 1974 | power_inst:            sprintf(str, GetString(STR_
1974                                                          case 957:       // PMC3
1975                                                          case 958:       // PMC4
1976                                                          case 959:       // SDA
1977 <                                                                r->nip += 4;
1977 >                                                                r->pc() += 4;
1978                                                                  goto rti;
1979                                                  }
1980                                                  break;
# Line 1721 | Line 1993 | power_inst:            sprintf(str, GetString(STR_
1993  
1994                  // In GUI mode, show error alert
1995                  if (!PrefsFindBool("nogui")) {
1996 <                        sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode);
1996 >                        sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode);
1997                          ErrorAlert(str);
1998                          QuitEmulator();
1999                          return;
# Line 1729 | Line 2001 | power_inst:            sprintf(str, GetString(STR_
2001          }
2002  
2003          // For all other errors, jump into debugger (sort of...)
2004 +        crash_reason = "SIGILL";
2005          if (!ready_for_signals) {
2006 <                printf("SIGILL\n");
2007 <                printf(" sigcontext %p, pt_regs %p\n", sc, r);
2006 >                printf("%s\n");
2007 >                printf(" sigcontext %p, machine_regs %p\n", scp, r);
2008                  printf(
2009                          "   pc %08lx     lr %08lx    ctr %08lx    msr %08lx\n"
2010                          "  xer %08lx     cr %08lx  \n"
# Line 1743 | Line 2016 | power_inst:            sprintf(str, GetString(STR_
2016                          "  r20 %08lx    r21 %08lx    r22 %08lx    r23 %08lx\n"
2017                          "  r24 %08lx    r25 %08lx    r26 %08lx    r27 %08lx\n"
2018                          "  r28 %08lx    r29 %08lx    r30 %08lx    r31 %08lx\n",
2019 <                        r->nip, r->link, r->ctr, r->msr,
2020 <                        r->xer, r->ccr,
2021 <                        r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3],
2022 <                        r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7],
2023 <                        r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11],
2024 <                        r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15],
2025 <                        r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19],
2026 <                        r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23],
2027 <                        r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27],
2028 <                        r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]);
2019 >                        crash_reason,
2020 >                        r->pc(), r->lr(), r->ctr(), r->msr(),
2021 >                        r->xer(), r->cr(),
2022 >                        r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3),
2023 >                        r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7),
2024 >                        r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11),
2025 >                        r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15),
2026 >                        r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19),
2027 >                        r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23),
2028 >                        r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27),
2029 >                        r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31));
2030                  exit(1);
2031                  QuitEmulator();
2032                  return;
2033          } else {
2034                  // We crashed. Save registers, tell tick thread and loop forever
2035 <                sigsegv_regs = *(sigregs *)r;
2035 >                build_sigregs(&sigsegv_regs, r);
2036                  emul_thread_fatal = true;
2037                  for (;;) ;
2038          }
# Line 1773 | Line 2047 | rti:;
2047  
2048   bool SheepMem::Init(void)
2049   {
2050 +        // Size of a native page
2051 +        page_size = getpagesize();
2052 +
2053 +        // Allocate SheepShaver globals
2054          if (vm_acquire_fixed((char *)base, size) < 0)
2055                  return false;
2056  
2057 +        // Allocate page with all bits set to 0
2058          zero_page = base + size;
1780
1781        int page_size = getpagesize();
2059          if (vm_acquire_fixed((char *)zero_page, page_size) < 0)
2060                  return false;
2061          memset((char *)zero_page, 0, page_size);
2062          if (vm_protect((char *)zero_page, page_size, VM_PAGE_READ) < 0)
2063                  return false;
2064  
2065 + #if EMULATED_PPC
2066 +        // Allocate alternate stack for PowerPC interrupt routine
2067 +        sig_stack = zero_page + page_size;
2068 +        if (vm_acquire_fixed((char *)sig_stack, SIG_STACK_SIZE) < 0)
2069 +                return false;
2070 + #endif
2071 +
2072          top = base + size;
2073          return true;
2074   }
# Line 1792 | Line 2076 | bool SheepMem::Init(void)
2076   void SheepMem::Exit(void)
2077   {
2078          if (top) {
2079 <                // The zero page is next to SheepShaver globals
2080 <                vm_release((void *)base, size + getpagesize());
2079 >                // Delete SheepShaver globals
2080 >                vm_release((void *)base, size);
2081 >
2082 >                // Delete zero page
2083 >                vm_release((void *)zero_page, page_size);
2084 >
2085 > #if EMULATED_PPC
2086 >                // Delete alternate stack for PowerPC interrupt routine
2087 >                vm_release((void *)sig_stack, SIG_STACK_SIZE);
2088 > #endif
2089          }
2090   }
2091  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines