ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/main_unix.cpp
Revision: 1.9
Committed: 2003-10-12T05:44:14Z (21 years, 1 month ago) by gbeauche
Branch: MAIN
Changes since 1.8: +5 -3 lines
Log Message:
- Handle MakeExecutable() replacement
- Disable predecode cache in CVS for now
- Fix flight recorder ordering in predecode cache mode

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