ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/main_unix.cpp
Revision: 1.16
Committed: 2003-12-04T22:33:24Z (20 years, 11 months ago) by gbeauche
Branch: MAIN
Changes since 1.15: +2 -2 lines
Log Message:
Fix ExecutePPC() merge with new SheepRoutineDescriptor

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * main_unix.cpp - Emulation core, Unix implementation
3     *
4     * SheepShaver (C) 1997-2002 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
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19     */
20    
21     /*
22     * NOTES:
23     *
24     * See main_beos.cpp for a description of the three operating modes.
25     *
26     * In addition to that, we have to handle the fact that the MacOS ABI
27     * is slightly different from the SysV ABI used by Linux:
28     * - Stack frames are different (e.g. LR is stored in 8(r1) under
29     * MacOS, but in 4(r1) under Linux)
30     * - There is no TOC under Linux; r2 is free for the user
31     * - r13 is used as a small data pointer under Linux (but appearently
32     * it is not used this way? To be sure, we specify -msdata=none
33     * in the Makefile)
34     * - As there is no TOC, there are also no TVECTs under Linux;
35     * function pointers point directly to the function code
36     * The Execute*() functions have to account for this. Additionally, we
37     * cannot simply call MacOS functions by getting their TVECT and jumping
38     * to it. Such calls are done via the call_macos*() functions in
39     * asm_linux.S that create a MacOS stack frame, load the TOC pointer
40     * and put the arguments into the right registers.
41     *
42     * As on the BeOS, we have to specify an alternate signal stack because
43     * interrupts (and, under Linux, Low Memory accesses) may occur when r1
44     * is pointing to the Kernel Data or to Low Memory. There is one
45     * problem, however, due to the alternate signal stack being global to
46     * all signal handlers. Consider the following scenario:
47     * - The main thread is executing some native PPC MacOS code in
48     * MODE_NATIVE, running on the MacOS stack (somewhere in the Mac RAM).
49     * - A SIGUSR2 interrupt occurs. The kernel switches to the signal
50     * stack and starts executing the SIGUSR2 signal handler.
51     * - The signal handler sees the MODE_NATIVE and calls ppc_interrupt()
52     * to handle a native interrupt.
53     * - ppc_interrupt() sets r1 to point to the Kernel Data and jumps to
54     * the nanokernel.
55     * - The nanokernel accesses a Low Memory global (most likely one of
56     * the XLMs), a SIGSEGV occurs.
57     * - The kernel sees that r1 does not point to the signal stack and
58     * switches to the signal stack again, thus overwriting the data that
59     * the SIGUSR2 handler put there.
60     * The same problem arises when calling ExecutePPC() inside the MODE_EMUL_OP
61     * interrupt handler.
62     *
63     * The solution is to set the signal stack to a second, "extra" stack
64     * inside the SIGUSR2 handler before entering the Nanokernel or calling
65     * ExecutePPC (or any function that might cause a mode switch). The signal
66     * stack is restored before exiting the SIGUSR2 handler.
67     *
68     * TODO:
69     * check if SIGSEGV handler works for all registers (including FP!)
70     */
71    
72     #include <unistd.h>
73     #include <fcntl.h>
74     #include <time.h>
75     #include <errno.h>
76     #include <stdio.h>
77     #include <stdlib.h>
78     #include <string.h>
79     #include <pthread.h>
80     #include <sys/mman.h>
81     #include <sys/ipc.h>
82     #include <sys/shm.h>
83     #include <signal.h>
84    
85     #include "sysdeps.h"
86     #include "main.h"
87     #include "version.h"
88     #include "prefs.h"
89     #include "prefs_editor.h"
90     #include "cpu_emulation.h"
91     #include "emul_op.h"
92     #include "xlowmem.h"
93     #include "xpram.h"
94     #include "timer.h"
95     #include "adb.h"
96     #include "sony.h"
97     #include "disk.h"
98     #include "cdrom.h"
99     #include "scsi.h"
100     #include "video.h"
101     #include "audio.h"
102     #include "ether.h"
103     #include "serial.h"
104     #include "clip.h"
105     #include "extfs.h"
106     #include "sys.h"
107     #include "macos_util.h"
108     #include "rom_patches.h"
109     #include "user_strings.h"
110 gbeauche 1.4 #include "vm_alloc.h"
111 gbeauche 1.5 #include "sigsegv.h"
112 gbeauche 1.15 #include "thunks.h"
113 cebix 1.1
114     #define DEBUG 0
115     #include "debug.h"
116    
117    
118     #include <X11/Xlib.h>
119    
120     #ifdef ENABLE_GTK
121     #include <gtk/gtk.h>
122     #endif
123    
124     #ifdef ENABLE_XF86_DGA
125     #include <X11/Xlib.h>
126     #include <X11/Xutil.h>
127     #include <X11/extensions/xf86dga.h>
128     #endif
129    
130     #ifdef ENABLE_MON
131     #include "mon.h"
132     #endif
133    
134    
135     // Enable Execute68k() safety checks?
136     #define SAFE_EXEC_68K 0
137    
138     // Interrupts in EMUL_OP mode?
139     #define INTERRUPTS_IN_EMUL_OP_MODE 1
140    
141     // Interrupts in native mode?
142     #define INTERRUPTS_IN_NATIVE_MODE 1
143    
144    
145     // Constants
146     const char ROM_FILE_NAME[] = "ROM";
147     const char ROM_FILE_NAME2[] = "Mac OS ROM";
148    
149 gbeauche 1.15 const uintptr RAM_BASE = 0x20000000; // Base address of RAM
150 cebix 1.1 const uint32 SIG_STACK_SIZE = 0x10000; // Size of signal stack
151    
152    
153     #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    
162     struct sigregs {
163     elf_gregset_t gp_regs; // Identical to pt_regs
164     double fp_regs[ELF_NFPREG]; // f0..f31 and fpsrc
165     //more (uninteresting) stuff following here
166     };
167     #endif
168    
169    
170     // Global variables (exported)
171     #if !EMULATED_PPC
172     void *TOC; // Small data pointer (r13)
173     #endif
174     uint32 RAMBase; // Base address of Mac RAM
175     uint32 RAMSize; // Size of Mac RAM
176     uint32 KernelDataAddr; // Address of Kernel Data
177     uint32 BootGlobsAddr; // Address of BootGlobs structure at top of Mac RAM
178     uint32 PVR; // Theoretical PVR
179     int64 CPUClockSpeed; // Processor clock speed (Hz)
180     int64 BusClockSpeed; // Bus clock speed (Hz)
181    
182    
183     // Global variables
184 gbeauche 1.11 char *x_display_name = NULL; // X11 display name
185 cebix 1.1 Display *x_display = NULL; // X11 display handle
186    
187     static int zero_fd = 0; // FD of /dev/zero
188     static bool lm_area_mapped = false; // Flag: Low Memory area mmap()ped
189     static int kernel_area = -1; // SHM ID of Kernel Data area
190     static bool rom_area_mapped = false; // Flag: Mac ROM mmap()ped
191     static bool ram_area_mapped = false; // Flag: Mac RAM mmap()ped
192     static KernelData *kernel_data; // Pointer to Kernel Data
193     static EmulatorData *emulator_data;
194    
195     static uint8 last_xpram[XPRAM_SIZE]; // Buffer for monitoring XPRAM changes
196    
197     static bool nvram_thread_active = false; // Flag: NVRAM watchdog installed
198     static pthread_t nvram_thread; // NVRAM watchdog
199     static bool tick_thread_active = false; // Flag: MacOS thread installed
200     static pthread_t tick_thread; // 60Hz thread
201     static pthread_t emul_thread; // MacOS thread
202    
203     static bool ready_for_signals = false; // Handler installed, signals can be sent
204     static int64 num_segv = 0; // Number of handled SEGV signals
205    
206 gbeauche 1.6 static struct sigaction sigusr2_action; // Interrupt signal (of emulator thread)
207 cebix 1.1 #if !EMULATED_PPC
208     static struct sigaction sigsegv_action; // Data access exception signal (of emulator thread)
209     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
212     static bool emul_thread_fatal = false; // Flag: MacOS thread crashed, tick thread shall dump debug output
213     static sigregs sigsegv_regs; // Register dump when crashed
214     #endif
215    
216 gbeauche 1.15 uintptr SheepMem::base = 0x60000000; // Address of SheepShaver data
217     uintptr SheepMem::top = 0; // Top of SheepShaver data (stack like storage)
218    
219 cebix 1.1
220     // Prototypes
221     static void Quit(void);
222     static void *emul_func(void *arg);
223     static void *nvram_func(void *arg);
224     static void *tick_func(void *arg);
225 gbeauche 1.8 #if EMULATED_PPC
226     static void sigusr2_handler(int sig);
227 gbeauche 1.13 extern void emul_ppc(uint32 start);
228     extern void init_emul_ppc(void);
229     extern void exit_emul_ppc(void);
230 gbeauche 1.8 #else
231 gbeauche 1.6 static void sigusr2_handler(int sig, sigcontext_struct *sc);
232 cebix 1.1 static void sigsegv_handler(int sig, sigcontext_struct *sc);
233     static void sigill_handler(int sig, sigcontext_struct *sc);
234     #endif
235    
236    
237     // From asm_linux.S
238 gbeauche 1.12 #if !EMULATED_PPC
239 cebix 1.1 extern "C" void *get_toc(void);
240     extern "C" void *get_sp(void);
241     extern "C" void flush_icache_range(void *start, void *end);
242     extern "C" void jump_to_rom(uint32 entry, uint32 context);
243     extern "C" void quit_emulator(void);
244     extern "C" void execute_68k(uint32 pc, M68kRegisters *r);
245     extern "C" void ppc_interrupt(uint32 entry, uint32 kernel_data);
246     extern "C" int atomic_add(int *var, int v);
247     extern "C" int atomic_and(int *var, int v);
248     extern "C" int atomic_or(int *var, int v);
249     extern void paranoia_check(void);
250 gbeauche 1.12 #endif
251    
252    
253     #if EMULATED_PPC
254     /*
255     * Atomic operations
256     */
257    
258     #if HAVE_SPINLOCKS
259     static spinlock_t atomic_ops_lock = SPIN_LOCK_UNLOCKED;
260     #else
261     #define spin_lock(LOCK)
262     #define spin_unlock(LOCK)
263     #endif
264    
265     int atomic_add(int *var, int v)
266     {
267     spin_lock(&atomic_ops_lock);
268     int ret = *var;
269     *var += v;
270     spin_unlock(&atomic_ops_lock);
271     return ret;
272     }
273    
274     int atomic_and(int *var, int v)
275     {
276     spin_lock(&atomic_ops_lock);
277     int ret = *var;
278     *var &= v;
279     spin_unlock(&atomic_ops_lock);
280     return ret;
281     }
282    
283     int atomic_or(int *var, int v)
284     {
285     spin_lock(&atomic_ops_lock);
286     int ret = *var;
287     *var |= v;
288     spin_unlock(&atomic_ops_lock);
289     return ret;
290     }
291 cebix 1.1 #endif
292    
293    
294     /*
295     * Main program
296     */
297    
298     static void usage(const char *prg_name)
299     {
300     printf("Usage: %s [OPTION...]\n", prg_name);
301     printf("\nUnix options:\n");
302     printf(" --display STRING\n X display to use\n");
303     PrefsPrintUsage();
304     exit(0);
305     }
306    
307     int main(int argc, char **argv)
308     {
309     char str[256];
310     uint32 *boot_globs;
311     int16 i16;
312     int rom_fd;
313     FILE *proc_file;
314     const char *rom_path;
315     uint32 rom_size, actual;
316     uint8 *rom_tmp;
317     time_t now, expire;
318    
319     // Initialize variables
320     RAMBase = 0;
321     tzset();
322    
323     // Print some info
324     printf(GetString(STR_ABOUT_TEXT1), VERSION_MAJOR, VERSION_MINOR);
325     printf(" %s\n", GetString(STR_ABOUT_TEXT2));
326    
327     #if !EMULATED_PPC
328     // Get TOC pointer
329     TOC = get_toc();
330     #endif
331    
332     #ifdef ENABLE_GTK
333     // Init GTK
334     gtk_set_locale();
335     gtk_init(&argc, &argv);
336     #endif
337    
338     // Read preferences
339     PrefsInit(argc, argv);
340    
341     // Parse command line arguments
342     for (int i=1; i<argc; i++) {
343     if (strcmp(argv[i], "--help") == 0) {
344     usage(argv[0]);
345     } else if (strcmp(argv[i], "--display") == 0) {
346     i++;
347     if (i < argc)
348     x_display_name = strdup(argv[i]);
349     } else if (argv[i][0] == '-') {
350     fprintf(stderr, "Unrecognized option '%s'\n", argv[i]);
351     usage(argv[0]);
352     }
353     }
354    
355     // Open display
356     x_display = XOpenDisplay(x_display_name);
357     if (x_display == NULL) {
358     char str[256];
359     sprintf(str, GetString(STR_NO_XSERVER_ERR), XDisplayName(x_display_name));
360     ErrorAlert(str);
361     goto quit;
362     }
363    
364     #if defined(ENABLE_XF86_DGA) && !defined(ENABLE_MON)
365     // Fork out, so we can return from fullscreen mode when things get ugly
366     XF86DGAForkApp(DefaultScreen(x_display));
367     #endif
368    
369     #ifdef ENABLE_MON
370     // Initialize mon
371     mon_init();
372     #endif
373    
374     // Get system info
375     PVR = 0x00040000; // Default: 604
376     CPUClockSpeed = 100000000; // Default: 100MHz
377     BusClockSpeed = 100000000; // Default: 100MHz
378     #if !EMULATED_PPC
379     proc_file = fopen("/proc/cpuinfo", "r");
380     if (proc_file) {
381     char line[256];
382     while(fgets(line, 255, proc_file)) {
383     // Read line
384     int len = strlen(line);
385     if (len == 0)
386     continue;
387     line[len-1] = 0;
388    
389     // Parse line
390     int i;
391     char value[256];
392     if (sscanf(line, "cpu : %s", value) == 1) {
393     if (strcmp(value, "601") == 0)
394     PVR = 0x00010000;
395     else if (strcmp(value, "603") == 0)
396     PVR = 0x00030000;
397     else if (strcmp(value, "604") == 0)
398     PVR = 0x00040000;
399     else if (strcmp(value, "603e") == 0)
400     PVR = 0x00060000;
401     else if (strcmp(value, "603ev") == 0)
402     PVR = 0x00070000;
403     else if (strcmp(value, "604e") == 0)
404     PVR = 0x00090000;
405     else if (strcmp(value, "604ev5") == 0)
406     PVR = 0x000a0000;
407     else if (strcmp(value, "750") == 0)
408     PVR = 0x00080000;
409     else if (strcmp(value, "821") == 0)
410     PVR = 0x00320000;
411     else if (strcmp(value, "860") == 0)
412     PVR = 0x00500000;
413     else
414     printf("WARNING: Unknown CPU type '%s', assuming 604\n", value);
415     }
416     if (sscanf(line, "clock : %dMHz", &i) == 1)
417     CPUClockSpeed = BusClockSpeed = i * 1000000;
418     }
419     fclose(proc_file);
420     } else {
421     sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno));
422     WarningAlert(str);
423     }
424     #endif
425     D(bug("PVR: %08x (assumed)\n", PVR));
426    
427     // Init system routines
428     SysInit();
429    
430     // Show preferences editor
431     if (!PrefsFindBool("nogui"))
432     if (!PrefsEditor())
433     goto quit;
434    
435     #if !EMULATED_PPC
436     // Check some things
437     paranoia_check();
438     #endif
439    
440     // Open /dev/zero
441     zero_fd = open("/dev/zero", O_RDWR);
442     if (zero_fd < 0) {
443     sprintf(str, GetString(STR_NO_DEV_ZERO_ERR), strerror(errno));
444     ErrorAlert(str);
445     goto quit;
446     }
447    
448     // Create Low Memory area (0x0000..0x3000)
449 gbeauche 1.4 if (vm_acquire_fixed((char *)0, 0x3000) < 0) {
450 cebix 1.1 sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno));
451     ErrorAlert(str);
452     goto quit;
453     }
454     lm_area_mapped = true;
455    
456     // Create areas for Kernel Data
457     kernel_area = shmget(IPC_PRIVATE, KERNEL_AREA_SIZE, 0600);
458     if (kernel_area == -1) {
459     sprintf(str, GetString(STR_KD_SHMGET_ERR), strerror(errno));
460     ErrorAlert(str);
461     goto quit;
462     }
463     if (shmat(kernel_area, (void *)KERNEL_DATA_BASE, 0) < 0) {
464     sprintf(str, GetString(STR_KD_SHMAT_ERR), strerror(errno));
465     ErrorAlert(str);
466     goto quit;
467     }
468     if (shmat(kernel_area, (void *)KERNEL_DATA2_BASE, 0) < 0) {
469     sprintf(str, GetString(STR_KD2_SHMAT_ERR), strerror(errno));
470     ErrorAlert(str);
471     goto quit;
472     }
473 gbeauche 1.15 kernel_data = (KernelData *)KERNEL_DATA_BASE;
474 cebix 1.1 emulator_data = &kernel_data->ed;
475 gbeauche 1.15 KernelDataAddr = KERNEL_DATA_BASE;
476 cebix 1.1 D(bug("Kernel Data at %p, Emulator Data at %p\n", kernel_data, emulator_data));
477    
478 gbeauche 1.8 // Create area for SheepShaver data
479 gbeauche 1.15 if (!SheepMem::Init()) {
480 gbeauche 1.8 sprintf(str, GetString(STR_SHEEP_MEM_MMAP_ERR), strerror(errno));
481     ErrorAlert(str);
482     goto quit;
483     }
484    
485 cebix 1.1 // Create area for Mac ROM
486 gbeauche 1.4 if (vm_acquire_fixed((char *)ROM_BASE, ROM_AREA_SIZE) < 0) {
487 cebix 1.1 sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno));
488     ErrorAlert(str);
489     goto quit;
490     }
491 gbeauche 1.6 #if !EMULATED_PPC || defined(__powerpc__)
492 gbeauche 1.4 if (vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) {
493     sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno));
494     ErrorAlert(str);
495     goto quit;
496     }
497     #endif
498 cebix 1.1 rom_area_mapped = true;
499     D(bug("ROM area at %08x\n", ROM_BASE));
500    
501     // Create area for Mac RAM
502     RAMSize = PrefsFindInt32("ramsize");
503     if (RAMSize < 8*1024*1024) {
504     WarningAlert(GetString(STR_SMALL_RAM_WARN));
505     RAMSize = 8*1024*1024;
506     }
507    
508 gbeauche 1.8 if (vm_acquire_fixed((char *)RAM_BASE, RAMSize) < 0) {
509 cebix 1.1 sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno));
510     ErrorAlert(str);
511     goto quit;
512     }
513 gbeauche 1.4 #if !EMULATED_PPC
514 gbeauche 1.8 if (vm_protect((char *)RAM_BASE, RAMSize, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) {
515 gbeauche 1.4 sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno));
516     ErrorAlert(str);
517     goto quit;
518     }
519     #endif
520 gbeauche 1.8 RAMBase = RAM_BASE;
521 cebix 1.1 ram_area_mapped = true;
522     D(bug("RAM area at %08x\n", RAMBase));
523    
524     if (RAMBase > ROM_BASE) {
525     ErrorAlert(GetString(STR_RAM_HIGHER_THAN_ROM_ERR));
526     goto quit;
527     }
528    
529     // Load Mac ROM
530     rom_path = PrefsFindString("rom");
531     rom_fd = open(rom_path ? rom_path : ROM_FILE_NAME, O_RDONLY);
532     if (rom_fd < 0) {
533     rom_fd = open(rom_path ? rom_path : ROM_FILE_NAME2, O_RDONLY);
534     if (rom_fd < 0) {
535     ErrorAlert(GetString(STR_NO_ROM_FILE_ERR));
536     goto quit;
537     }
538     }
539     printf(GetString(STR_READING_ROM_FILE));
540     rom_size = lseek(rom_fd, 0, SEEK_END);
541     lseek(rom_fd, 0, SEEK_SET);
542     rom_tmp = new uint8[ROM_SIZE];
543     actual = read(rom_fd, (void *)rom_tmp, ROM_SIZE);
544     close(rom_fd);
545 gbeauche 1.3
546     // Decode Mac ROM
547     if (!DecodeROM(rom_tmp, actual)) {
548     if (rom_size != 4*1024*1024) {
549 cebix 1.1 ErrorAlert(GetString(STR_ROM_SIZE_ERR));
550     goto quit;
551     } else {
552     ErrorAlert(GetString(STR_ROM_FILE_READ_ERR));
553     goto quit;
554     }
555     }
556 gbeauche 1.3 delete[] rom_tmp;
557 cebix 1.1
558     // Load NVRAM
559     XPRAMInit();
560    
561     // Set boot volume
562 cebix 1.10 i16 = PrefsFindInt32("bootdrive");
563 cebix 1.1 XPRAM[0x1378] = i16 >> 8;
564     XPRAM[0x1379] = i16 & 0xff;
565 cebix 1.10 i16 = PrefsFindInt32("bootdriver");
566 cebix 1.1 XPRAM[0x137a] = i16 >> 8;
567     XPRAM[0x137b] = i16 & 0xff;
568    
569     // Create BootGlobs at top of Mac memory
570     memset((void *)(RAMBase + RAMSize - 4096), 0, 4096);
571     BootGlobsAddr = RAMBase + RAMSize - 0x1c;
572     boot_globs = (uint32 *)BootGlobsAddr;
573     boot_globs[-5] = htonl(RAMBase + RAMSize); // MemTop
574     boot_globs[0] = htonl(RAMBase); // First RAM bank
575     boot_globs[1] = htonl(RAMSize);
576     boot_globs[2] = htonl((uint32)-1); // End of bank table
577    
578 gbeauche 1.15 // Init thunks
579     if (!ThunksInit())
580     goto quit;
581    
582 cebix 1.1 // Init drivers
583     SonyInit();
584     DiskInit();
585     CDROMInit();
586     SCSIInit();
587    
588     // Init external file system
589     ExtFSInit();
590    
591     // Init audio
592     AudioInit();
593    
594     // Init network
595     EtherInit();
596    
597     // Init serial ports
598     SerialInit();
599    
600     // Init Time Manager
601     TimerInit();
602    
603     // Init clipboard
604     ClipInit();
605    
606     // Init video
607     if (!VideoInit())
608     goto quit;
609    
610     // Install ROM patches
611     if (!PatchROM()) {
612     ErrorAlert(GetString(STR_UNSUPPORTED_ROM_TYPE_ERR));
613     goto quit;
614     }
615    
616     // Clear caches (as we loaded and patched code) and write protect ROM
617     #if !EMULATED_PPC
618     MakeExecutable(0, (void *)ROM_BASE, ROM_AREA_SIZE);
619     #endif
620 gbeauche 1.4 vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_EXECUTE);
621 cebix 1.1
622     // Initialize Kernel Data
623     memset(kernel_data, 0, sizeof(KernelData));
624     if (ROMType == ROMTYPE_NEWWORLD) {
625 gbeauche 1.15 uintptr of_dev_tree = SheepMem::Reserve(4 * sizeof(uint32));
626     memset((void *)of_dev_tree, 0, 4 * sizeof(uint32));
627     uintptr vector_lookup_tbl = SheepMem::Reserve(128);
628     uintptr vector_mask_tbl = SheepMem::Reserve(64);
629 cebix 1.1 memset((uint8 *)kernel_data + 0xb80, 0x3d, 0x80);
630 gbeauche 1.15 memset((void *)vector_lookup_tbl, 0, 128);
631     memset((void *)vector_mask_tbl, 0, 64);
632 cebix 1.1 kernel_data->v[0xb80 >> 2] = htonl(ROM_BASE);
633 gbeauche 1.15 kernel_data->v[0xb84 >> 2] = htonl(of_dev_tree); // OF device tree base
634     kernel_data->v[0xb90 >> 2] = htonl(vector_lookup_tbl);
635     kernel_data->v[0xb94 >> 2] = htonl(vector_mask_tbl);
636 cebix 1.1 kernel_data->v[0xb98 >> 2] = htonl(ROM_BASE); // OpenPIC base
637     kernel_data->v[0xbb0 >> 2] = htonl(0); // ADB base
638     kernel_data->v[0xc20 >> 2] = htonl(RAMSize);
639     kernel_data->v[0xc24 >> 2] = htonl(RAMSize);
640     kernel_data->v[0xc30 >> 2] = htonl(RAMSize);
641     kernel_data->v[0xc34 >> 2] = htonl(RAMSize);
642     kernel_data->v[0xc38 >> 2] = htonl(0x00010020);
643     kernel_data->v[0xc3c >> 2] = htonl(0x00200001);
644     kernel_data->v[0xc40 >> 2] = htonl(0x00010000);
645     kernel_data->v[0xc50 >> 2] = htonl(RAMBase);
646     kernel_data->v[0xc54 >> 2] = htonl(RAMSize);
647     kernel_data->v[0xf60 >> 2] = htonl(PVR);
648     kernel_data->v[0xf64 >> 2] = htonl(CPUClockSpeed);
649     kernel_data->v[0xf68 >> 2] = htonl(BusClockSpeed);
650     kernel_data->v[0xf6c >> 2] = htonl(CPUClockSpeed);
651     } else {
652     kernel_data->v[0xc80 >> 2] = htonl(RAMSize);
653     kernel_data->v[0xc84 >> 2] = htonl(RAMSize);
654     kernel_data->v[0xc90 >> 2] = htonl(RAMSize);
655     kernel_data->v[0xc94 >> 2] = htonl(RAMSize);
656     kernel_data->v[0xc98 >> 2] = htonl(0x00010020);
657     kernel_data->v[0xc9c >> 2] = htonl(0x00200001);
658     kernel_data->v[0xca0 >> 2] = htonl(0x00010000);
659     kernel_data->v[0xcb0 >> 2] = htonl(RAMBase);
660     kernel_data->v[0xcb4 >> 2] = htonl(RAMSize);
661     kernel_data->v[0xf80 >> 2] = htonl(PVR);
662     kernel_data->v[0xf84 >> 2] = htonl(CPUClockSpeed);
663     kernel_data->v[0xf88 >> 2] = htonl(BusClockSpeed);
664     kernel_data->v[0xf8c >> 2] = htonl(CPUClockSpeed);
665     }
666    
667     // Initialize extra low memory
668     D(bug("Initializing Low Memory...\n"));
669     memset(NULL, 0, 0x3000);
670     WriteMacInt32(XLM_SIGNATURE, FOURCC('B','a','a','h')); // Signature to detect SheepShaver
671 gbeauche 1.15 WriteMacInt32(XLM_KERNEL_DATA, KernelDataAddr); // For trap replacement routines
672 cebix 1.1 WriteMacInt32(XLM_PVR, PVR); // Theoretical PVR
673     WriteMacInt32(XLM_BUS_CLOCK, BusClockSpeed); // For DriverServicesLib patch
674     WriteMacInt16(XLM_EXEC_RETURN_OPCODE, M68K_EXEC_RETURN); // For Execute68k() (RTS from the executed 68k code will jump here and end 68k mode)
675 gbeauche 1.6 #if EMULATED_PPC
676 gbeauche 1.15 WriteMacInt32(XLM_ETHER_INIT, NativeFunction(NATIVE_ETHER_INIT));
677     WriteMacInt32(XLM_ETHER_TERM, NativeFunction(NATIVE_ETHER_TERM));
678     WriteMacInt32(XLM_ETHER_OPEN, NativeFunction(NATIVE_ETHER_OPEN));
679     WriteMacInt32(XLM_ETHER_CLOSE, NativeFunction(NATIVE_ETHER_CLOSE));
680     WriteMacInt32(XLM_ETHER_WPUT, NativeFunction(NATIVE_ETHER_WPUT));
681     WriteMacInt32(XLM_ETHER_RSRV, NativeFunction(NATIVE_ETHER_RSRV));
682     WriteMacInt32(XLM_VIDEO_DOIO, NativeFunction(NATIVE_VIDEO_DO_DRIVER_IO));
683 gbeauche 1.6 #else
684 cebix 1.1 WriteMacInt32(XLM_TOC, (uint32)TOC); // TOC pointer of emulator
685     WriteMacInt32(XLM_ETHER_INIT, (uint32)InitStreamModule); // DLPI ethernet driver functions
686     WriteMacInt32(XLM_ETHER_TERM, (uint32)TerminateStreamModule);
687     WriteMacInt32(XLM_ETHER_OPEN, (uint32)ether_open);
688     WriteMacInt32(XLM_ETHER_CLOSE, (uint32)ether_close);
689     WriteMacInt32(XLM_ETHER_WPUT, (uint32)ether_wput);
690     WriteMacInt32(XLM_ETHER_RSRV, (uint32)ether_rsrv);
691     WriteMacInt32(XLM_VIDEO_DOIO, (uint32)VideoDoDriverIO);
692     #endif
693     D(bug("Low Memory initialized\n"));
694    
695     // Start 60Hz thread
696     tick_thread_active = (pthread_create(&tick_thread, NULL, tick_func, NULL) == 0);
697     D(bug("Tick thread installed (%ld)\n", tick_thread));
698    
699     // Start NVRAM watchdog thread
700     memcpy(last_xpram, XPRAM, XPRAM_SIZE);
701     nvram_thread_active = (pthread_create(&nvram_thread, NULL, nvram_func, NULL) == 0);
702     D(bug("NVRAM thread installed (%ld)\n", nvram_thread));
703    
704     #if !EMULATED_PPC
705     // Create and install stacks for signal handlers
706     sig_stack = malloc(SIG_STACK_SIZE);
707     D(bug("Signal stack at %p\n", sig_stack));
708     if (sig_stack == NULL) {
709     ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR));
710     goto quit;
711     }
712     extra_stack = malloc(SIG_STACK_SIZE);
713     D(bug("Extra stack at %p\n", extra_stack));
714     if (extra_stack == NULL) {
715     ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR));
716     goto quit;
717     }
718     struct sigaltstack new_stack;
719     new_stack.ss_sp = sig_stack;
720     new_stack.ss_flags = 0;
721     new_stack.ss_size = SIG_STACK_SIZE;
722     if (sigaltstack(&new_stack, NULL) < 0) {
723     sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno));
724     ErrorAlert(str);
725     goto quit;
726     }
727     #endif
728    
729     #if !EMULATED_PPC
730     // Install SIGSEGV handler
731     sigemptyset(&sigsegv_action.sa_mask); // Block interrupts during SEGV handling
732     sigaddset(&sigsegv_action.sa_mask, SIGUSR2);
733     sigsegv_action.sa_handler = (__sighandler_t)sigsegv_handler;
734     sigsegv_action.sa_flags = SA_ONSTACK;
735     sigsegv_action.sa_restorer = NULL;
736     if (sigaction(SIGSEGV, &sigsegv_action, NULL) < 0) {
737     sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno));
738     ErrorAlert(str);
739     goto quit;
740     }
741    
742     // Install SIGILL handler
743     sigemptyset(&sigill_action.sa_mask); // Block interrupts during ILL handling
744     sigaddset(&sigill_action.sa_mask, SIGUSR2);
745     sigill_action.sa_handler = (__sighandler_t)sigill_handler;
746     sigill_action.sa_flags = SA_ONSTACK;
747     sigill_action.sa_restorer = NULL;
748     if (sigaction(SIGILL, &sigill_action, NULL) < 0) {
749     sprintf(str, GetString(STR_SIGILL_INSTALL_ERR), strerror(errno));
750     ErrorAlert(str);
751     goto quit;
752     }
753 gbeauche 1.6 #endif
754 cebix 1.1
755     // Install interrupt signal handler
756     sigemptyset(&sigusr2_action.sa_mask);
757     sigusr2_action.sa_handler = (__sighandler_t)sigusr2_handler;
758 gbeauche 1.8 sigusr2_action.sa_flags = 0;
759     #if !EMULATED_PPC
760 cebix 1.1 sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART;
761 gbeauche 1.8 #endif
762 cebix 1.1 sigusr2_action.sa_restorer = NULL;
763     if (sigaction(SIGUSR2, &sigusr2_action, NULL) < 0) {
764     sprintf(str, GetString(STR_SIGUSR2_INSTALL_ERR), strerror(errno));
765     ErrorAlert(str);
766     goto quit;
767     }
768    
769     // Get my thread ID and execute MacOS thread function
770     emul_thread = pthread_self();
771     D(bug("MacOS thread is %ld\n", emul_thread));
772     emul_func(NULL);
773    
774     quit:
775     Quit();
776     return 0;
777     }
778    
779    
780     /*
781     * Cleanup and quit
782     */
783    
784     static void Quit(void)
785     {
786 gbeauche 1.13 #if EMULATED_PPC
787     // Exit PowerPC emulation
788     exit_emul_ppc();
789     #endif
790    
791 cebix 1.1 // Stop 60Hz thread
792     if (tick_thread_active) {
793     pthread_cancel(tick_thread);
794     pthread_join(tick_thread, NULL);
795     }
796    
797     // Stop NVRAM watchdog thread
798     if (nvram_thread_active) {
799     pthread_cancel(nvram_thread);
800     pthread_join(nvram_thread, NULL);
801     }
802    
803     #if !EMULATED_PPC
804     // Uninstall SIGSEGV handler
805     sigemptyset(&sigsegv_action.sa_mask);
806     sigsegv_action.sa_handler = SIG_DFL;
807     sigsegv_action.sa_flags = 0;
808     sigaction(SIGSEGV, &sigsegv_action, NULL);
809    
810     // Uninstall SIGILL handler
811     sigemptyset(&sigill_action.sa_mask);
812     sigill_action.sa_handler = SIG_DFL;
813     sigill_action.sa_flags = 0;
814     sigaction(SIGILL, &sigill_action, NULL);
815     #endif
816    
817     // Save NVRAM
818     XPRAMExit();
819    
820     // Exit clipboard
821     ClipExit();
822    
823     // Exit Time Manager
824     TimerExit();
825    
826     // Exit serial
827     SerialExit();
828    
829     // Exit network
830     EtherExit();
831    
832     // Exit audio
833     AudioExit();
834    
835     // Exit video
836     VideoExit();
837    
838     // Exit external file system
839     ExtFSExit();
840    
841     // Exit drivers
842     SCSIExit();
843     CDROMExit();
844     DiskExit();
845     SonyExit();
846    
847 gbeauche 1.15 // Delete SheepShaver globals
848     SheepMem::Exit();
849    
850 cebix 1.1 // Delete RAM area
851     if (ram_area_mapped)
852 gbeauche 1.8 vm_release((char *)RAM_BASE, RAMSize);
853 cebix 1.1
854     // Delete ROM area
855     if (rom_area_mapped)
856 gbeauche 1.4 vm_release((char *)ROM_BASE, ROM_AREA_SIZE);
857 cebix 1.1
858     // Delete Kernel Data area
859     if (kernel_area >= 0) {
860     shmdt((void *)KERNEL_DATA_BASE);
861     shmdt((void *)KERNEL_DATA2_BASE);
862     shmctl(kernel_area, IPC_RMID, NULL);
863     }
864    
865     // Delete Low Memory area
866     if (lm_area_mapped)
867     munmap((char *)0x0000, 0x3000);
868    
869     // Close /dev/zero
870     if (zero_fd > 0)
871     close(zero_fd);
872    
873     // Exit system routines
874     SysExit();
875    
876     // Exit preferences
877     PrefsExit();
878    
879     #ifdef ENABLE_MON
880     // Exit mon
881     mon_exit();
882     #endif
883    
884     // Close X11 server connection
885     if (x_display)
886     XCloseDisplay(x_display);
887    
888     exit(0);
889     }
890    
891    
892     /*
893     * Jump into Mac ROM, start 680x0 emulator
894     */
895    
896     #if EMULATED_PPC
897     void jump_to_rom(uint32 entry)
898     {
899     init_emul_ppc();
900     emul_ppc(entry);
901     }
902     #endif
903    
904    
905     /*
906     * Emulator thread function
907     */
908    
909     static void *emul_func(void *arg)
910     {
911     // We're now ready to receive signals
912     ready_for_signals = true;
913    
914     // Decrease priority, so more time-critical things like audio will work better
915     nice(1);
916    
917     // Jump to ROM boot routine
918     D(bug("Jumping to ROM\n"));
919     #if EMULATED_PPC
920     jump_to_rom(ROM_BASE + 0x310000);
921     #else
922     jump_to_rom(ROM_BASE + 0x310000, (uint32)emulator_data);
923     #endif
924     D(bug("Returned from ROM\n"));
925    
926     // We're no longer ready to receive signals
927     ready_for_signals = false;
928     return NULL;
929     }
930    
931    
932     #if !EMULATED_PPC
933     /*
934     * Execute 68k subroutine (must be ended with RTS)
935     * This must only be called by the emul_thread when in EMUL_OP mode
936     * r->a[7] is unused, the routine runs on the caller's stack
937     */
938    
939     void Execute68k(uint32 pc, M68kRegisters *r)
940     {
941     #if SAFE_EXEC_68K
942     if (ReadMacInt32(XLM_RUN_MODE) != MODE_EMUL_OP)
943     printf("FATAL: Execute68k() not called from EMUL_OP mode\n");
944     if (!pthread_equal(pthread_self(), emul_thread))
945     printf("FATAL: Execute68k() not called from emul_thread\n");
946     #endif
947     execute_68k(pc, r);
948     }
949    
950    
951     /*
952     * Execute 68k A-Trap from EMUL_OP routine
953     * r->a[7] is unused, the routine runs on the caller's stack
954     */
955    
956     void Execute68kTrap(uint16 trap, M68kRegisters *r)
957     {
958     uint16 proc[2] = {trap, M68K_RTS};
959     Execute68k((uint32)proc, r);
960     }
961    
962    
963     /*
964     * Execute PPC code from EMUL_OP routine (real mode switch)
965     */
966    
967     void ExecutePPC(void (*func)())
968     {
969     uint32 tvect[2] = {(uint32)func, 0}; // Fake TVECT
970 gbeauche 1.16 SheepRoutineDescriptor desc(0, (uint32)tvect);
971 cebix 1.1 M68kRegisters r;
972 gbeauche 1.16 Execute68k(desc.addr(), &r);
973 cebix 1.1 }
974 gbeauche 1.7 #endif
975 cebix 1.1
976    
977     /*
978     * Quit emulator (cause return from jump_to_rom)
979     */
980    
981     void QuitEmulator(void)
982     {
983     #if EMULATED_PPC
984     Quit();
985     #else
986     quit_emulator();
987     #endif
988     }
989    
990    
991     /*
992     * Pause/resume emulator
993     */
994    
995     void PauseEmulator(void)
996     {
997     pthread_kill(emul_thread, SIGSTOP);
998     }
999    
1000     void ResumeEmulator(void)
1001     {
1002     pthread_kill(emul_thread, SIGCONT);
1003     }
1004    
1005    
1006     /*
1007     * Dump 68k registers
1008     */
1009    
1010     void Dump68kRegs(M68kRegisters *r)
1011     {
1012     // Display 68k registers
1013     for (int i=0; i<8; i++) {
1014     printf("d%d: %08x", i, r->d[i]);
1015     if (i == 3 || i == 7)
1016     printf("\n");
1017     else
1018     printf(", ");
1019     }
1020     for (int i=0; i<8; i++) {
1021     printf("a%d: %08x", i, r->a[i]);
1022     if (i == 3 || i == 7)
1023     printf("\n");
1024     else
1025     printf(", ");
1026     }
1027     }
1028    
1029    
1030     /*
1031     * Make code executable
1032     */
1033    
1034     void MakeExecutable(int dummy, void *start, uint32 length)
1035     {
1036 gbeauche 1.9 if (((uintptr)start >= ROM_BASE) && ((uintptr)start < (ROM_BASE + ROM_SIZE)))
1037 cebix 1.1 return;
1038 gbeauche 1.9 #if EMULATED_PPC
1039     FlushCodeCache((uintptr)start, (uintptr)start + length);
1040     #else
1041     flush_icache_range(start, (void *)((uintptr)start + length));
1042 cebix 1.1 #endif
1043     }
1044    
1045    
1046     /*
1047     * Patch things after system startup (gets called by disk driver accRun routine)
1048     */
1049    
1050     void PatchAfterStartup(void)
1051     {
1052 gbeauche 1.6 #if EMULATED_PPC
1053     ExecuteNative(NATIVE_VIDEO_INSTALL_ACCEL);
1054     #else
1055 cebix 1.1 ExecutePPC(VideoInstallAccel);
1056 gbeauche 1.6 #endif
1057 cebix 1.1 InstallExtFS();
1058     }
1059    
1060    
1061     /*
1062     * NVRAM watchdog thread (saves NVRAM every minute)
1063     */
1064    
1065     static void *nvram_func(void *arg)
1066     {
1067     struct timespec req = {60, 0}; // 1 minute
1068    
1069     for (;;) {
1070     pthread_testcancel();
1071     nanosleep(&req, NULL);
1072     pthread_testcancel();
1073     if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) {
1074     memcpy(last_xpram, XPRAM, XPRAM_SIZE);
1075     SaveXPRAM();
1076     }
1077     }
1078     return NULL;
1079     }
1080    
1081    
1082     /*
1083     * 60Hz thread (really 60.15Hz)
1084     */
1085    
1086     static void *tick_func(void *arg)
1087     {
1088     int tick_counter = 0;
1089     struct timespec req = {0, 16625000};
1090    
1091     for (;;) {
1092    
1093     // Wait
1094     nanosleep(&req, NULL);
1095    
1096     #if !EMULATED_PPC
1097     // Did we crash?
1098     if (emul_thread_fatal) {
1099    
1100     // Yes, dump registers
1101     pt_regs *r = (pt_regs *)&sigsegv_regs;
1102     char str[256];
1103     sprintf(str, "SIGSEGV\n"
1104     " pc %08lx lr %08lx ctr %08lx msr %08lx\n"
1105     " xer %08lx cr %08lx \n"
1106     " r0 %08lx r1 %08lx r2 %08lx r3 %08lx\n"
1107     " r4 %08lx r5 %08lx r6 %08lx r7 %08lx\n"
1108     " r8 %08lx r9 %08lx r10 %08lx r11 %08lx\n"
1109     " r12 %08lx r13 %08lx r14 %08lx r15 %08lx\n"
1110     " r16 %08lx r17 %08lx r18 %08lx r19 %08lx\n"
1111     " r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n"
1112     " r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n"
1113     " r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n",
1114     r->nip, r->link, r->ctr, r->msr,
1115     r->xer, r->ccr,
1116     r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3],
1117     r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7],
1118     r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11],
1119     r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15],
1120     r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19],
1121     r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23],
1122     r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27],
1123     r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]);
1124     printf(str);
1125     VideoQuitFullScreen();
1126    
1127     #ifdef ENABLE_MON
1128     // Start up mon in real-mode
1129     printf("Welcome to the sheep factory.\n");
1130     char *arg[4] = {"mon", "-m", "-r", NULL};
1131     mon(3, arg);
1132     #endif
1133     return NULL;
1134     }
1135     #endif
1136    
1137     // Pseudo Mac 1Hz interrupt, update local time
1138     if (++tick_counter > 60) {
1139     tick_counter = 0;
1140     WriteMacInt32(0x20c, TimerDateTime());
1141     }
1142    
1143     // Trigger 60Hz interrupt
1144     if (ReadMacInt32(XLM_IRQ_NEST) == 0) {
1145     SetInterruptFlag(INTFLAG_VIA);
1146     TriggerInterrupt();
1147     }
1148     }
1149     return NULL;
1150     }
1151    
1152    
1153     /*
1154 cebix 1.2 * Pthread configuration
1155     */
1156    
1157     void Set_pthread_attr(pthread_attr_t *attr, int priority)
1158     {
1159 gbeauche 1.14 #ifdef HAVE_PTHREADS
1160     pthread_attr_init(attr);
1161     #if defined(_POSIX_THREAD_PRIORITY_SCHEDULING)
1162     // Some of these only work for superuser
1163     if (geteuid() == 0) {
1164     pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED);
1165     pthread_attr_setschedpolicy(attr, SCHED_FIFO);
1166     struct sched_param fifo_param;
1167     fifo_param.sched_priority = ((sched_get_priority_min(SCHED_FIFO) +
1168     sched_get_priority_max(SCHED_FIFO)) / 2 +
1169     priority);
1170     pthread_attr_setschedparam(attr, &fifo_param);
1171     }
1172     if (pthread_attr_setscope(attr, PTHREAD_SCOPE_SYSTEM) != 0) {
1173     #ifdef PTHREAD_SCOPE_BOUND_NP
1174     // If system scope is not available (eg. we're not running
1175     // with CAP_SCHED_MGT capability on an SGI box), try bound
1176     // scope. It exposes pthread scheduling to the kernel,
1177     // without setting realtime priority.
1178     pthread_attr_setscope(attr, PTHREAD_SCOPE_BOUND_NP);
1179     #endif
1180     }
1181     #endif
1182     #endif
1183 cebix 1.2 }
1184    
1185    
1186     /*
1187 cebix 1.1 * Mutexes
1188     */
1189    
1190 gbeauche 1.7 #ifdef HAVE_PTHREADS
1191    
1192     struct B2_mutex {
1193     B2_mutex() {
1194     pthread_mutexattr_t attr;
1195     pthread_mutexattr_init(&attr);
1196     // Initialize the mutex for priority inheritance --
1197     // required for accurate timing.
1198     #ifdef HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL
1199     pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
1200     #endif
1201     #if defined(HAVE_PTHREAD_MUTEXATTR_SETTYPE) && defined(PTHREAD_MUTEX_NORMAL)
1202     pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
1203     #endif
1204     #ifdef HAVE_PTHREAD_MUTEXATTR_SETPSHARED
1205     pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE);
1206     #endif
1207     pthread_mutex_init(&m, &attr);
1208     pthread_mutexattr_destroy(&attr);
1209     }
1210     ~B2_mutex() {
1211     pthread_mutex_trylock(&m); // Make sure it's locked before
1212     pthread_mutex_unlock(&m); // unlocking it.
1213     pthread_mutex_destroy(&m);
1214     }
1215     pthread_mutex_t m;
1216     };
1217    
1218     B2_mutex *B2_create_mutex(void)
1219     {
1220     return new B2_mutex;
1221     }
1222    
1223     void B2_lock_mutex(B2_mutex *mutex)
1224     {
1225     pthread_mutex_lock(&mutex->m);
1226     }
1227    
1228     void B2_unlock_mutex(B2_mutex *mutex)
1229     {
1230     pthread_mutex_unlock(&mutex->m);
1231     }
1232    
1233     void B2_delete_mutex(B2_mutex *mutex)
1234     {
1235     delete mutex;
1236     }
1237    
1238     #else
1239    
1240 cebix 1.1 struct B2_mutex {
1241     int dummy;
1242     };
1243    
1244     B2_mutex *B2_create_mutex(void)
1245     {
1246     return new B2_mutex;
1247     }
1248    
1249     void B2_lock_mutex(B2_mutex *mutex)
1250     {
1251     }
1252    
1253     void B2_unlock_mutex(B2_mutex *mutex)
1254     {
1255     }
1256    
1257     void B2_delete_mutex(B2_mutex *mutex)
1258     {
1259     delete mutex;
1260     }
1261    
1262 gbeauche 1.7 #endif
1263    
1264 cebix 1.1
1265     /*
1266     * Trigger signal USR2 from another thread
1267     */
1268    
1269 gbeauche 1.8 #if !EMULATED_PPC || ASYNC_IRQ
1270 cebix 1.1 void TriggerInterrupt(void)
1271     {
1272     if (ready_for_signals)
1273     pthread_kill(emul_thread, SIGUSR2);
1274     }
1275 gbeauche 1.7 #endif
1276 cebix 1.1
1277    
1278     /*
1279     * Interrupt flags (must be handled atomically!)
1280     */
1281    
1282     volatile uint32 InterruptFlags = 0;
1283    
1284     void SetInterruptFlag(uint32 flag)
1285     {
1286     atomic_or((int *)&InterruptFlags, flag);
1287     }
1288    
1289     void ClearInterruptFlag(uint32 flag)
1290     {
1291     atomic_and((int *)&InterruptFlags, ~flag);
1292     }
1293    
1294    
1295     /*
1296     * Disable interrupts
1297     */
1298    
1299     void DisableInterrupt(void)
1300     {
1301 gbeauche 1.7 atomic_add((int *)XLM_IRQ_NEST, 1);
1302 cebix 1.1 }
1303    
1304    
1305     /*
1306     * Enable interrupts
1307     */
1308    
1309     void EnableInterrupt(void)
1310     {
1311 gbeauche 1.7 atomic_add((int *)XLM_IRQ_NEST, -1);
1312 cebix 1.1 }
1313    
1314    
1315     /*
1316     * USR2 handler
1317     */
1318    
1319 gbeauche 1.8 #if EMULATED_PPC
1320     static void sigusr2_handler(int sig)
1321     {
1322     #if ASYNC_IRQ
1323     extern void HandleInterrupt(void);
1324     HandleInterrupt();
1325     #endif
1326     }
1327     #else
1328 cebix 1.1 static void sigusr2_handler(int sig, sigcontext_struct *sc)
1329     {
1330     pt_regs *r = sc->regs;
1331    
1332     // Do nothing if interrupts are disabled
1333     if (*(int32 *)XLM_IRQ_NEST > 0)
1334     return;
1335    
1336     // Disable MacOS stack sniffer
1337     WriteMacInt32(0x110, 0);
1338    
1339     // Interrupt action depends on current run mode
1340     switch (ReadMacInt32(XLM_RUN_MODE)) {
1341     case MODE_68K:
1342     // 68k emulator active, trigger 68k interrupt level 1
1343     WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1);
1344     r->ccr |= ntohl(kernel_data->v[0x674 >> 2]);
1345     break;
1346    
1347     #if INTERRUPTS_IN_NATIVE_MODE
1348     case MODE_NATIVE:
1349     // 68k emulator inactive, in nanokernel?
1350     if (r->gpr[1] != KernelDataAddr) {
1351     // Prepare for 68k interrupt level 1
1352     WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1);
1353     WriteMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc, ReadMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc) | ntohl(kernel_data->v[0x674 >> 2]));
1354    
1355     // Execute nanokernel interrupt routine (this will activate the 68k emulator)
1356     atomic_add((int32 *)XLM_IRQ_NEST, 1);
1357     if (ROMType == ROMTYPE_NEWWORLD)
1358     ppc_interrupt(ROM_BASE + 0x312b1c, KernelDataAddr);
1359     else
1360     ppc_interrupt(ROM_BASE + 0x312a3c, KernelDataAddr);
1361     }
1362     break;
1363     #endif
1364    
1365     #if INTERRUPTS_IN_EMUL_OP_MODE
1366     case MODE_EMUL_OP:
1367     // 68k emulator active, within EMUL_OP routine, execute 68k interrupt routine directly when interrupt level is 0
1368     if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) {
1369    
1370     // Set extra stack for SIGSEGV handler
1371     struct sigaltstack new_stack;
1372     new_stack.ss_sp = extra_stack;
1373     new_stack.ss_flags = 0;
1374     new_stack.ss_size = SIG_STACK_SIZE;
1375     sigaltstack(&new_stack, NULL);
1376     #if 1
1377     // Execute full 68k interrupt routine
1378     M68kRegisters r;
1379     uint32 old_r25 = ReadMacInt32(XLM_68K_R25); // Save interrupt level
1380     WriteMacInt32(XLM_68K_R25, 0x21); // Execute with interrupt level 1
1381     static const uint16 proc[] = {
1382     0x3f3c, 0x0000, // move.w #$0000,-(sp) (fake format word)
1383     0x487a, 0x000a, // pea @1(pc) (return address)
1384     0x40e7, // move sr,-(sp) (saved SR)
1385     0x2078, 0x0064, // move.l $64,a0
1386     0x4ed0, // jmp (a0)
1387     M68K_RTS // @1
1388     };
1389     Execute68k((uint32)proc, &r);
1390     WriteMacInt32(XLM_68K_R25, old_r25); // Restore interrupt level
1391     #else
1392     // Only update cursor
1393     if (HasMacStarted()) {
1394     if (InterruptFlags & INTFLAG_VIA) {
1395     ClearInterruptFlag(INTFLAG_VIA);
1396     ADBInterrupt();
1397     ExecutePPC(VideoVBL);
1398     }
1399     }
1400     #endif
1401     // Reset normal signal stack
1402     new_stack.ss_sp = sig_stack;
1403     new_stack.ss_flags = 0;
1404     new_stack.ss_size = SIG_STACK_SIZE;
1405     sigaltstack(&new_stack, NULL);
1406     }
1407     break;
1408     #endif
1409     }
1410     }
1411 gbeauche 1.8 #endif
1412 cebix 1.1
1413    
1414     /*
1415     * SIGSEGV handler
1416     */
1417    
1418 gbeauche 1.8 #if !EMULATED_PPC
1419 cebix 1.1 static void sigsegv_handler(int sig, sigcontext_struct *sc)
1420     {
1421     pt_regs *r = sc->regs;
1422 gbeauche 1.5
1423     // Get effective address
1424     uint32 addr = r->dar;
1425    
1426     #if ENABLE_VOSF
1427     // Handle screen fault.
1428     extern bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction);
1429     if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->nip))
1430     return;
1431     #endif
1432    
1433 cebix 1.1 num_segv++;
1434    
1435     // Fault in Mac ROM or RAM?
1436     bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize));
1437     if (mac_fault) {
1438    
1439     // "VM settings" during MacOS 8 installation
1440     if (r->nip == ROM_BASE + 0x488160 && r->gpr[20] == 0xf8000000) {
1441     r->nip += 4;
1442     r->gpr[8] = 0;
1443     return;
1444    
1445     // MacOS 8.5 installation
1446     } else if (r->nip == ROM_BASE + 0x488140 && r->gpr[16] == 0xf8000000) {
1447     r->nip += 4;
1448     r->gpr[8] = 0;
1449     return;
1450    
1451     // MacOS 8 serial drivers on startup
1452     } else if (r->nip == ROM_BASE + 0x48e080 && (r->gpr[8] == 0xf3012002 || r->gpr[8] == 0xf3012000)) {
1453     r->nip += 4;
1454     r->gpr[8] = 0;
1455     return;
1456    
1457     // MacOS 8.1 serial drivers on startup
1458     } else if (r->nip == ROM_BASE + 0x48c5e0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) {
1459     r->nip += 4;
1460     return;
1461     } else if (r->nip == ROM_BASE + 0x4a10a0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) {
1462     r->nip += 4;
1463     return;
1464     }
1465    
1466 gbeauche 1.5 // Get opcode and divide into fields
1467     uint32 opcode = *((uint32 *)r->nip);
1468     uint32 primop = opcode >> 26;
1469     uint32 exop = (opcode >> 1) & 0x3ff;
1470     uint32 ra = (opcode >> 16) & 0x1f;
1471     uint32 rb = (opcode >> 11) & 0x1f;
1472     uint32 rd = (opcode >> 21) & 0x1f;
1473     int32 imm = (int16)(opcode & 0xffff);
1474    
1475 cebix 1.1 // Analyze opcode
1476     enum {
1477     TYPE_UNKNOWN,
1478     TYPE_LOAD,
1479     TYPE_STORE
1480     } transfer_type = TYPE_UNKNOWN;
1481     enum {
1482     SIZE_UNKNOWN,
1483     SIZE_BYTE,
1484     SIZE_HALFWORD,
1485     SIZE_WORD
1486     } transfer_size = SIZE_UNKNOWN;
1487     enum {
1488     MODE_UNKNOWN,
1489     MODE_NORM,
1490     MODE_U,
1491     MODE_X,
1492     MODE_UX
1493     } addr_mode = MODE_UNKNOWN;
1494     switch (primop) {
1495     case 31:
1496     switch (exop) {
1497     case 23: // lwzx
1498     transfer_type = TYPE_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_X; break;
1499     case 55: // lwzux
1500     transfer_type = TYPE_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_UX; break;
1501     case 87: // lbzx
1502     transfer_type = TYPE_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_X; break;
1503     case 119: // lbzux
1504     transfer_type = TYPE_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_UX; break;
1505     case 151: // stwx
1506     transfer_type = TYPE_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_X; break;
1507     case 183: // stwux
1508     transfer_type = TYPE_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_UX; break;
1509     case 215: // stbx
1510     transfer_type = TYPE_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_X; break;
1511     case 247: // stbux
1512     transfer_type = TYPE_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_UX; break;
1513     case 279: // lhzx
1514     transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_X; break;
1515     case 311: // lhzux
1516     transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_UX; break;
1517     case 343: // lhax
1518     transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_X; break;
1519     case 375: // lhaux
1520     transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_UX; break;
1521     case 407: // sthx
1522     transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_X; break;
1523     case 439: // sthux
1524     transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_UX; break;
1525     }
1526     break;
1527    
1528     case 32: // lwz
1529     transfer_type = TYPE_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_NORM; break;
1530     case 33: // lwzu
1531     transfer_type = TYPE_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_U; break;
1532     case 34: // lbz
1533     transfer_type = TYPE_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_NORM; break;
1534     case 35: // lbzu
1535     transfer_type = TYPE_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_U; break;
1536     case 36: // stw
1537     transfer_type = TYPE_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_NORM; break;
1538     case 37: // stwu
1539     transfer_type = TYPE_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_U; break;
1540     case 38: // stb
1541     transfer_type = TYPE_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_NORM; break;
1542     case 39: // stbu
1543     transfer_type = TYPE_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_U; break;
1544     case 40: // lhz
1545     transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_NORM; break;
1546     case 41: // lhzu
1547     transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_U; break;
1548     case 42: // lha
1549     transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_NORM; break;
1550     case 43: // lhau
1551     transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_U; break;
1552     case 44: // sth
1553     transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_NORM; break;
1554     case 45: // sthu
1555     transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_U; break;
1556     }
1557    
1558     // Ignore ROM writes
1559     if (transfer_type == TYPE_STORE && addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) {
1560     // 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));
1561     if (addr_mode == MODE_U || addr_mode == MODE_UX)
1562     r->gpr[ra] = addr;
1563     r->nip += 4;
1564     goto rti;
1565     }
1566    
1567     // Ignore illegal memory accesses?
1568     if (PrefsFindBool("ignoresegv")) {
1569     if (addr_mode == MODE_U || addr_mode == MODE_UX)
1570     r->gpr[ra] = addr;
1571     if (transfer_type == TYPE_LOAD)
1572     r->gpr[rd] = 0;
1573     r->nip += 4;
1574     goto rti;
1575     }
1576    
1577     // In GUI mode, show error alert
1578     if (!PrefsFindBool("nogui")) {
1579     char str[256];
1580     if (transfer_type == TYPE_LOAD || transfer_type == TYPE_STORE)
1581     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]);
1582     else
1583     sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode);
1584     ErrorAlert(str);
1585     QuitEmulator();
1586     return;
1587     }
1588     }
1589    
1590     // For all other errors, jump into debugger (sort of...)
1591     if (!ready_for_signals) {
1592     printf("SIGSEGV\n");
1593     printf(" sigcontext %p, pt_regs %p\n", sc, r);
1594     printf(
1595     " pc %08lx lr %08lx ctr %08lx msr %08lx\n"
1596     " xer %08lx cr %08lx \n"
1597     " r0 %08lx r1 %08lx r2 %08lx r3 %08lx\n"
1598     " r4 %08lx r5 %08lx r6 %08lx r7 %08lx\n"
1599     " r8 %08lx r9 %08lx r10 %08lx r11 %08lx\n"
1600     " r12 %08lx r13 %08lx r14 %08lx r15 %08lx\n"
1601     " r16 %08lx r17 %08lx r18 %08lx r19 %08lx\n"
1602     " r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n"
1603     " r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n"
1604     " r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n",
1605     r->nip, r->link, r->ctr, r->msr,
1606     r->xer, r->ccr,
1607     r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3],
1608     r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7],
1609     r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11],
1610     r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15],
1611     r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19],
1612     r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23],
1613     r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27],
1614     r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]);
1615     exit(1);
1616     QuitEmulator();
1617     return;
1618     } else {
1619     // We crashed. Save registers, tell tick thread and loop forever
1620     sigsegv_regs = *(sigregs *)r;
1621     emul_thread_fatal = true;
1622     for (;;) ;
1623     }
1624     rti:;
1625     }
1626    
1627    
1628     /*
1629     * SIGILL handler
1630     */
1631    
1632     static void sigill_handler(int sig, sigcontext_struct *sc)
1633     {
1634     pt_regs *r = sc->regs;
1635     char str[256];
1636    
1637     // Fault in Mac ROM or RAM?
1638     bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize));
1639     if (mac_fault) {
1640    
1641     // Get opcode and divide into fields
1642     uint32 opcode = *((uint32 *)r->nip);
1643     uint32 primop = opcode >> 26;
1644     uint32 exop = (opcode >> 1) & 0x3ff;
1645     uint32 ra = (opcode >> 16) & 0x1f;
1646     uint32 rb = (opcode >> 11) & 0x1f;
1647     uint32 rd = (opcode >> 21) & 0x1f;
1648     int32 imm = (int16)(opcode & 0xffff);
1649    
1650     switch (primop) {
1651     case 9: // POWER instructions
1652     case 22:
1653     power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->nip, r->gpr[1], opcode);
1654     ErrorAlert(str);
1655     QuitEmulator();
1656     return;
1657    
1658     case 31:
1659     switch (exop) {
1660     case 83: // mfmsr
1661     r->gpr[rd] = 0xf072;
1662     r->nip += 4;
1663     goto rti;
1664    
1665     case 210: // mtsr
1666     case 242: // mtsrin
1667     case 306: // tlbie
1668     r->nip += 4;
1669     goto rti;
1670    
1671     case 339: { // mfspr
1672     int spr = ra | (rb << 5);
1673     switch (spr) {
1674     case 0: // MQ
1675     case 22: // DEC
1676     case 952: // MMCR0
1677     case 953: // PMC1
1678     case 954: // PMC2
1679     case 955: // SIA
1680     case 956: // MMCR1
1681     case 957: // PMC3
1682     case 958: // PMC4
1683     case 959: // SDA
1684     r->nip += 4;
1685     goto rti;
1686     case 25: // SDR1
1687     r->gpr[rd] = 0xdead001f;
1688     r->nip += 4;
1689     goto rti;
1690     case 287: // PVR
1691     r->gpr[rd] = PVR;
1692     r->nip += 4;
1693     goto rti;
1694     }
1695     break;
1696     }
1697    
1698     case 467: { // mtspr
1699     int spr = ra | (rb << 5);
1700     switch (spr) {
1701     case 0: // MQ
1702     case 22: // DEC
1703     case 275: // SPRG3
1704     case 528: // IBAT0U
1705     case 529: // IBAT0L
1706     case 530: // IBAT1U
1707     case 531: // IBAT1L
1708     case 532: // IBAT2U
1709     case 533: // IBAT2L
1710     case 534: // IBAT3U
1711     case 535: // IBAT3L
1712     case 536: // DBAT0U
1713     case 537: // DBAT0L
1714     case 538: // DBAT1U
1715     case 539: // DBAT1L
1716     case 540: // DBAT2U
1717     case 541: // DBAT2L
1718     case 542: // DBAT3U
1719     case 543: // DBAT3L
1720     case 952: // MMCR0
1721     case 953: // PMC1
1722     case 954: // PMC2
1723     case 955: // SIA
1724     case 956: // MMCR1
1725     case 957: // PMC3
1726     case 958: // PMC4
1727     case 959: // SDA
1728     r->nip += 4;
1729     goto rti;
1730     }
1731     break;
1732     }
1733    
1734     case 29: case 107: case 152: case 153: // POWER instructions
1735     case 184: case 216: case 217: case 248:
1736     case 264: case 277: case 331: case 360:
1737     case 363: case 488: case 531: case 537:
1738     case 541: case 664: case 665: case 696:
1739     case 728: case 729: case 760: case 920:
1740     case 921: case 952:
1741     goto power_inst;
1742     }
1743     }
1744    
1745     // In GUI mode, show error alert
1746     if (!PrefsFindBool("nogui")) {
1747     sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode);
1748     ErrorAlert(str);
1749     QuitEmulator();
1750     return;
1751     }
1752     }
1753    
1754     // For all other errors, jump into debugger (sort of...)
1755     if (!ready_for_signals) {
1756     printf("SIGILL\n");
1757     printf(" sigcontext %p, pt_regs %p\n", sc, r);
1758     printf(
1759     " pc %08lx lr %08lx ctr %08lx msr %08lx\n"
1760     " xer %08lx cr %08lx \n"
1761     " r0 %08lx r1 %08lx r2 %08lx r3 %08lx\n"
1762     " r4 %08lx r5 %08lx r6 %08lx r7 %08lx\n"
1763     " r8 %08lx r9 %08lx r10 %08lx r11 %08lx\n"
1764     " r12 %08lx r13 %08lx r14 %08lx r15 %08lx\n"
1765     " r16 %08lx r17 %08lx r18 %08lx r19 %08lx\n"
1766     " r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n"
1767     " r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n"
1768     " r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n",
1769     r->nip, r->link, r->ctr, r->msr,
1770     r->xer, r->ccr,
1771     r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3],
1772     r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7],
1773     r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11],
1774     r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15],
1775     r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19],
1776     r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23],
1777     r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27],
1778     r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]);
1779     exit(1);
1780     QuitEmulator();
1781     return;
1782     } else {
1783     // We crashed. Save registers, tell tick thread and loop forever
1784     sigsegv_regs = *(sigregs *)r;
1785     emul_thread_fatal = true;
1786     for (;;) ;
1787     }
1788     rti:;
1789     }
1790     #endif
1791 gbeauche 1.15
1792    
1793     /*
1794     * Helpers to share 32-bit addressable data with MacOS
1795     */
1796    
1797     bool SheepMem::Init(void)
1798     {
1799     if (vm_acquire_fixed((char *)base, size) < 0)
1800     return false;
1801     top = base + size;
1802     return true;
1803     }
1804    
1805     void SheepMem::Exit(void)
1806     {
1807     if (top)
1808     vm_release((void *)base, size);
1809     }
1810 cebix 1.1
1811    
1812     /*
1813     * Display alert
1814     */
1815    
1816     #ifdef ENABLE_GTK
1817     static void dl_destroyed(void)
1818     {
1819     gtk_main_quit();
1820     }
1821    
1822     static void dl_quit(GtkWidget *dialog)
1823     {
1824     gtk_widget_destroy(dialog);
1825     }
1826    
1827     void display_alert(int title_id, int prefix_id, int button_id, const char *text)
1828     {
1829     char str[256];
1830     sprintf(str, GetString(prefix_id), text);
1831    
1832     GtkWidget *dialog = gtk_dialog_new();
1833     gtk_window_set_title(GTK_WINDOW(dialog), GetString(title_id));
1834     gtk_container_border_width(GTK_CONTAINER(dialog), 5);
1835     gtk_widget_set_uposition(GTK_WIDGET(dialog), 100, 150);
1836     gtk_signal_connect(GTK_OBJECT(dialog), "destroy", GTK_SIGNAL_FUNC(dl_destroyed), NULL);
1837    
1838     GtkWidget *label = gtk_label_new(str);
1839     gtk_widget_show(label);
1840     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), label, TRUE, TRUE, 0);
1841    
1842     GtkWidget *button = gtk_button_new_with_label(GetString(button_id));
1843     gtk_widget_show(button);
1844     gtk_signal_connect_object(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(dl_quit), GTK_OBJECT(dialog));
1845     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area), button, FALSE, FALSE, 0);
1846     GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1847     gtk_widget_grab_default(button);
1848     gtk_widget_show(dialog);
1849    
1850     gtk_main();
1851     }
1852     #endif
1853    
1854    
1855     /*
1856     * Display error alert
1857     */
1858    
1859     void ErrorAlert(const char *text)
1860     {
1861     #ifdef ENABLE_GTK
1862     if (PrefsFindBool("nogui") || x_display == NULL) {
1863     printf(GetString(STR_SHELL_ERROR_PREFIX), text);
1864     return;
1865     }
1866     VideoQuitFullScreen();
1867     display_alert(STR_ERROR_ALERT_TITLE, STR_GUI_ERROR_PREFIX, STR_QUIT_BUTTON, text);
1868     #else
1869     printf(GetString(STR_SHELL_ERROR_PREFIX), text);
1870     #endif
1871     }
1872    
1873    
1874     /*
1875     * Display warning alert
1876     */
1877    
1878     void WarningAlert(const char *text)
1879     {
1880     #ifdef ENABLE_GTK
1881     if (PrefsFindBool("nogui") || x_display == NULL) {
1882     printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1883     return;
1884     }
1885     display_alert(STR_WARNING_ALERT_TITLE, STR_GUI_WARNING_PREFIX, STR_OK_BUTTON, text);
1886     #else
1887     printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1888     #endif
1889     }
1890    
1891    
1892     /*
1893     * Display choice alert
1894     */
1895    
1896     bool ChoiceAlert(const char *text, const char *pos, const char *neg)
1897     {
1898     printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1899     return false; //!!
1900     }