ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/main_unix.cpp
Revision: 1.8
Committed: 2003-09-29T15:46:07Z (21 years, 1 month ago) by gbeauche
Branch: MAIN
Changes since 1.7: +38 -33 lines
Log Message:
- Share EmulatorData & KernelData struct definitions
- Introduce new SheepShaver data area for alternate stacks, thunks, etc.
- Experimental asynchronous interrupts handling. This improves performance
  by 30% but some (rare) lockups may occur. To be debugged!

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     #if !EMULATED_PPC
991     if (((uint32)start >= ROM_BASE) && ((uint32)start < (ROM_BASE + ROM_SIZE)))
992     return;
993     flush_icache_range(start, (void *)((uint32)start + length));
994     #endif
995     }
996    
997    
998     /*
999     * Patch things after system startup (gets called by disk driver accRun routine)
1000     */
1001    
1002     void PatchAfterStartup(void)
1003     {
1004 gbeauche 1.6 #if EMULATED_PPC
1005     ExecuteNative(NATIVE_VIDEO_INSTALL_ACCEL);
1006     #else
1007 cebix 1.1 ExecutePPC(VideoInstallAccel);
1008 gbeauche 1.6 #endif
1009 cebix 1.1 InstallExtFS();
1010     }
1011    
1012    
1013     /*
1014     * NVRAM watchdog thread (saves NVRAM every minute)
1015     */
1016    
1017     static void *nvram_func(void *arg)
1018     {
1019     struct timespec req = {60, 0}; // 1 minute
1020    
1021     for (;;) {
1022     pthread_testcancel();
1023     nanosleep(&req, NULL);
1024     pthread_testcancel();
1025     if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) {
1026     memcpy(last_xpram, XPRAM, XPRAM_SIZE);
1027     SaveXPRAM();
1028     }
1029     }
1030     return NULL;
1031     }
1032    
1033    
1034     /*
1035     * 60Hz thread (really 60.15Hz)
1036     */
1037    
1038     static void *tick_func(void *arg)
1039     {
1040     int tick_counter = 0;
1041     struct timespec req = {0, 16625000};
1042    
1043     for (;;) {
1044    
1045     // Wait
1046     nanosleep(&req, NULL);
1047    
1048     #if !EMULATED_PPC
1049     // Did we crash?
1050     if (emul_thread_fatal) {
1051    
1052     // Yes, dump registers
1053     pt_regs *r = (pt_regs *)&sigsegv_regs;
1054     char str[256];
1055     sprintf(str, "SIGSEGV\n"
1056     " pc %08lx lr %08lx ctr %08lx msr %08lx\n"
1057     " xer %08lx cr %08lx \n"
1058     " r0 %08lx r1 %08lx r2 %08lx r3 %08lx\n"
1059     " r4 %08lx r5 %08lx r6 %08lx r7 %08lx\n"
1060     " r8 %08lx r9 %08lx r10 %08lx r11 %08lx\n"
1061     " r12 %08lx r13 %08lx r14 %08lx r15 %08lx\n"
1062     " r16 %08lx r17 %08lx r18 %08lx r19 %08lx\n"
1063     " r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n"
1064     " r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n"
1065     " r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n",
1066     r->nip, r->link, r->ctr, r->msr,
1067     r->xer, r->ccr,
1068     r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3],
1069     r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7],
1070     r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11],
1071     r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15],
1072     r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19],
1073     r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23],
1074     r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27],
1075     r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]);
1076     printf(str);
1077     VideoQuitFullScreen();
1078    
1079     #ifdef ENABLE_MON
1080     // Start up mon in real-mode
1081     printf("Welcome to the sheep factory.\n");
1082     char *arg[4] = {"mon", "-m", "-r", NULL};
1083     mon(3, arg);
1084     #endif
1085     return NULL;
1086     }
1087     #endif
1088    
1089     // Pseudo Mac 1Hz interrupt, update local time
1090     if (++tick_counter > 60) {
1091     tick_counter = 0;
1092     WriteMacInt32(0x20c, TimerDateTime());
1093     }
1094    
1095     // Trigger 60Hz interrupt
1096     if (ReadMacInt32(XLM_IRQ_NEST) == 0) {
1097     SetInterruptFlag(INTFLAG_VIA);
1098     TriggerInterrupt();
1099     }
1100     }
1101     return NULL;
1102     }
1103    
1104    
1105     /*
1106 cebix 1.2 * Pthread configuration
1107     */
1108    
1109     void Set_pthread_attr(pthread_attr_t *attr, int priority)
1110     {
1111     // nothing to do
1112     }
1113    
1114    
1115     /*
1116 cebix 1.1 * Mutexes
1117     */
1118    
1119 gbeauche 1.7 #ifdef HAVE_PTHREADS
1120    
1121     struct B2_mutex {
1122     B2_mutex() {
1123     pthread_mutexattr_t attr;
1124     pthread_mutexattr_init(&attr);
1125     // Initialize the mutex for priority inheritance --
1126     // required for accurate timing.
1127     #ifdef HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL
1128     pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
1129     #endif
1130     #if defined(HAVE_PTHREAD_MUTEXATTR_SETTYPE) && defined(PTHREAD_MUTEX_NORMAL)
1131     pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
1132     #endif
1133     #ifdef HAVE_PTHREAD_MUTEXATTR_SETPSHARED
1134     pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE);
1135     #endif
1136     pthread_mutex_init(&m, &attr);
1137     pthread_mutexattr_destroy(&attr);
1138     }
1139     ~B2_mutex() {
1140     pthread_mutex_trylock(&m); // Make sure it's locked before
1141     pthread_mutex_unlock(&m); // unlocking it.
1142     pthread_mutex_destroy(&m);
1143     }
1144     pthread_mutex_t m;
1145     };
1146    
1147     B2_mutex *B2_create_mutex(void)
1148     {
1149     return new B2_mutex;
1150     }
1151    
1152     void B2_lock_mutex(B2_mutex *mutex)
1153     {
1154     pthread_mutex_lock(&mutex->m);
1155     }
1156    
1157     void B2_unlock_mutex(B2_mutex *mutex)
1158     {
1159     pthread_mutex_unlock(&mutex->m);
1160     }
1161    
1162     void B2_delete_mutex(B2_mutex *mutex)
1163     {
1164     delete mutex;
1165     }
1166    
1167     #else
1168    
1169 cebix 1.1 struct B2_mutex {
1170     int dummy;
1171     };
1172    
1173     B2_mutex *B2_create_mutex(void)
1174     {
1175     return new B2_mutex;
1176     }
1177    
1178     void B2_lock_mutex(B2_mutex *mutex)
1179     {
1180     }
1181    
1182     void B2_unlock_mutex(B2_mutex *mutex)
1183     {
1184     }
1185    
1186     void B2_delete_mutex(B2_mutex *mutex)
1187     {
1188     delete mutex;
1189     }
1190    
1191 gbeauche 1.7 #endif
1192    
1193 cebix 1.1
1194     /*
1195     * Trigger signal USR2 from another thread
1196     */
1197    
1198 gbeauche 1.8 #if !EMULATED_PPC || ASYNC_IRQ
1199 cebix 1.1 void TriggerInterrupt(void)
1200     {
1201     if (ready_for_signals)
1202     pthread_kill(emul_thread, SIGUSR2);
1203     }
1204 gbeauche 1.7 #endif
1205 cebix 1.1
1206    
1207     /*
1208     * Interrupt flags (must be handled atomically!)
1209     */
1210    
1211     volatile uint32 InterruptFlags = 0;
1212    
1213     void SetInterruptFlag(uint32 flag)
1214     {
1215     atomic_or((int *)&InterruptFlags, flag);
1216     }
1217    
1218     void ClearInterruptFlag(uint32 flag)
1219     {
1220     atomic_and((int *)&InterruptFlags, ~flag);
1221     }
1222    
1223    
1224     /*
1225     * Disable interrupts
1226     */
1227    
1228     void DisableInterrupt(void)
1229     {
1230 gbeauche 1.7 atomic_add((int *)XLM_IRQ_NEST, 1);
1231 cebix 1.1 }
1232    
1233    
1234     /*
1235     * Enable interrupts
1236     */
1237    
1238     void EnableInterrupt(void)
1239     {
1240 gbeauche 1.7 atomic_add((int *)XLM_IRQ_NEST, -1);
1241 cebix 1.1 }
1242    
1243    
1244     /*
1245     * USR2 handler
1246     */
1247    
1248 gbeauche 1.8 #if EMULATED_PPC
1249     static void sigusr2_handler(int sig)
1250     {
1251     #if ASYNC_IRQ
1252     extern void HandleInterrupt(void);
1253     HandleInterrupt();
1254     #endif
1255     }
1256     #else
1257 cebix 1.1 static void sigusr2_handler(int sig, sigcontext_struct *sc)
1258     {
1259     pt_regs *r = sc->regs;
1260    
1261     // Do nothing if interrupts are disabled
1262     if (*(int32 *)XLM_IRQ_NEST > 0)
1263     return;
1264    
1265     // Disable MacOS stack sniffer
1266     WriteMacInt32(0x110, 0);
1267    
1268     // Interrupt action depends on current run mode
1269     switch (ReadMacInt32(XLM_RUN_MODE)) {
1270     case MODE_68K:
1271     // 68k emulator active, trigger 68k interrupt level 1
1272     WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1);
1273     r->ccr |= ntohl(kernel_data->v[0x674 >> 2]);
1274     break;
1275    
1276     #if INTERRUPTS_IN_NATIVE_MODE
1277     case MODE_NATIVE:
1278     // 68k emulator inactive, in nanokernel?
1279     if (r->gpr[1] != KernelDataAddr) {
1280     // Prepare for 68k interrupt level 1
1281     WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1);
1282     WriteMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc, ReadMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc) | ntohl(kernel_data->v[0x674 >> 2]));
1283    
1284     // Execute nanokernel interrupt routine (this will activate the 68k emulator)
1285     atomic_add((int32 *)XLM_IRQ_NEST, 1);
1286     if (ROMType == ROMTYPE_NEWWORLD)
1287     ppc_interrupt(ROM_BASE + 0x312b1c, KernelDataAddr);
1288     else
1289     ppc_interrupt(ROM_BASE + 0x312a3c, KernelDataAddr);
1290     }
1291     break;
1292     #endif
1293    
1294     #if INTERRUPTS_IN_EMUL_OP_MODE
1295     case MODE_EMUL_OP:
1296     // 68k emulator active, within EMUL_OP routine, execute 68k interrupt routine directly when interrupt level is 0
1297     if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) {
1298    
1299     // Set extra stack for SIGSEGV handler
1300     struct sigaltstack new_stack;
1301     new_stack.ss_sp = extra_stack;
1302     new_stack.ss_flags = 0;
1303     new_stack.ss_size = SIG_STACK_SIZE;
1304     sigaltstack(&new_stack, NULL);
1305     #if 1
1306     // Execute full 68k interrupt routine
1307     M68kRegisters r;
1308     uint32 old_r25 = ReadMacInt32(XLM_68K_R25); // Save interrupt level
1309     WriteMacInt32(XLM_68K_R25, 0x21); // Execute with interrupt level 1
1310     static const uint16 proc[] = {
1311     0x3f3c, 0x0000, // move.w #$0000,-(sp) (fake format word)
1312     0x487a, 0x000a, // pea @1(pc) (return address)
1313     0x40e7, // move sr,-(sp) (saved SR)
1314     0x2078, 0x0064, // move.l $64,a0
1315     0x4ed0, // jmp (a0)
1316     M68K_RTS // @1
1317     };
1318     Execute68k((uint32)proc, &r);
1319     WriteMacInt32(XLM_68K_R25, old_r25); // Restore interrupt level
1320     #else
1321     // Only update cursor
1322     if (HasMacStarted()) {
1323     if (InterruptFlags & INTFLAG_VIA) {
1324     ClearInterruptFlag(INTFLAG_VIA);
1325     ADBInterrupt();
1326     ExecutePPC(VideoVBL);
1327     }
1328     }
1329     #endif
1330     // Reset normal signal stack
1331     new_stack.ss_sp = sig_stack;
1332     new_stack.ss_flags = 0;
1333     new_stack.ss_size = SIG_STACK_SIZE;
1334     sigaltstack(&new_stack, NULL);
1335     }
1336     break;
1337     #endif
1338     }
1339     }
1340 gbeauche 1.8 #endif
1341 cebix 1.1
1342    
1343     /*
1344     * SIGSEGV handler
1345     */
1346    
1347 gbeauche 1.8 #if !EMULATED_PPC
1348 cebix 1.1 static void sigsegv_handler(int sig, sigcontext_struct *sc)
1349     {
1350     pt_regs *r = sc->regs;
1351 gbeauche 1.5
1352     // Get effective address
1353     uint32 addr = r->dar;
1354    
1355     #if ENABLE_VOSF
1356     // Handle screen fault.
1357     extern bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction);
1358     if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->nip))
1359     return;
1360     #endif
1361    
1362 cebix 1.1 num_segv++;
1363    
1364     // Fault in Mac ROM or RAM?
1365     bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize));
1366     if (mac_fault) {
1367    
1368     // "VM settings" during MacOS 8 installation
1369     if (r->nip == ROM_BASE + 0x488160 && r->gpr[20] == 0xf8000000) {
1370     r->nip += 4;
1371     r->gpr[8] = 0;
1372     return;
1373    
1374     // MacOS 8.5 installation
1375     } else if (r->nip == ROM_BASE + 0x488140 && r->gpr[16] == 0xf8000000) {
1376     r->nip += 4;
1377     r->gpr[8] = 0;
1378     return;
1379    
1380     // MacOS 8 serial drivers on startup
1381     } else if (r->nip == ROM_BASE + 0x48e080 && (r->gpr[8] == 0xf3012002 || r->gpr[8] == 0xf3012000)) {
1382     r->nip += 4;
1383     r->gpr[8] = 0;
1384     return;
1385    
1386     // MacOS 8.1 serial drivers on startup
1387     } else if (r->nip == ROM_BASE + 0x48c5e0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) {
1388     r->nip += 4;
1389     return;
1390     } else if (r->nip == ROM_BASE + 0x4a10a0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) {
1391     r->nip += 4;
1392     return;
1393     }
1394    
1395 gbeauche 1.5 // Get opcode and divide into fields
1396     uint32 opcode = *((uint32 *)r->nip);
1397     uint32 primop = opcode >> 26;
1398     uint32 exop = (opcode >> 1) & 0x3ff;
1399     uint32 ra = (opcode >> 16) & 0x1f;
1400     uint32 rb = (opcode >> 11) & 0x1f;
1401     uint32 rd = (opcode >> 21) & 0x1f;
1402     int32 imm = (int16)(opcode & 0xffff);
1403    
1404 cebix 1.1 // Analyze opcode
1405     enum {
1406     TYPE_UNKNOWN,
1407     TYPE_LOAD,
1408     TYPE_STORE
1409     } transfer_type = TYPE_UNKNOWN;
1410     enum {
1411     SIZE_UNKNOWN,
1412     SIZE_BYTE,
1413     SIZE_HALFWORD,
1414     SIZE_WORD
1415     } transfer_size = SIZE_UNKNOWN;
1416     enum {
1417     MODE_UNKNOWN,
1418     MODE_NORM,
1419     MODE_U,
1420     MODE_X,
1421     MODE_UX
1422     } addr_mode = MODE_UNKNOWN;
1423     switch (primop) {
1424     case 31:
1425     switch (exop) {
1426     case 23: // lwzx
1427     transfer_type = TYPE_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_X; break;
1428     case 55: // lwzux
1429     transfer_type = TYPE_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_UX; break;
1430     case 87: // lbzx
1431     transfer_type = TYPE_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_X; break;
1432     case 119: // lbzux
1433     transfer_type = TYPE_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_UX; break;
1434     case 151: // stwx
1435     transfer_type = TYPE_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_X; break;
1436     case 183: // stwux
1437     transfer_type = TYPE_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_UX; break;
1438     case 215: // stbx
1439     transfer_type = TYPE_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_X; break;
1440     case 247: // stbux
1441     transfer_type = TYPE_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_UX; break;
1442     case 279: // lhzx
1443     transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_X; break;
1444     case 311: // lhzux
1445     transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_UX; break;
1446     case 343: // lhax
1447     transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_X; break;
1448     case 375: // lhaux
1449     transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_UX; break;
1450     case 407: // sthx
1451     transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_X; break;
1452     case 439: // sthux
1453     transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_UX; break;
1454     }
1455     break;
1456    
1457     case 32: // lwz
1458     transfer_type = TYPE_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_NORM; break;
1459     case 33: // lwzu
1460     transfer_type = TYPE_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_U; break;
1461     case 34: // lbz
1462     transfer_type = TYPE_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_NORM; break;
1463     case 35: // lbzu
1464     transfer_type = TYPE_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_U; break;
1465     case 36: // stw
1466     transfer_type = TYPE_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_NORM; break;
1467     case 37: // stwu
1468     transfer_type = TYPE_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_U; break;
1469     case 38: // stb
1470     transfer_type = TYPE_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_NORM; break;
1471     case 39: // stbu
1472     transfer_type = TYPE_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_U; break;
1473     case 40: // lhz
1474     transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_NORM; break;
1475     case 41: // lhzu
1476     transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_U; break;
1477     case 42: // lha
1478     transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_NORM; break;
1479     case 43: // lhau
1480     transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_U; break;
1481     case 44: // sth
1482     transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_NORM; break;
1483     case 45: // sthu
1484     transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_U; break;
1485     }
1486    
1487     // Ignore ROM writes
1488     if (transfer_type == TYPE_STORE && addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) {
1489     // 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));
1490     if (addr_mode == MODE_U || addr_mode == MODE_UX)
1491     r->gpr[ra] = addr;
1492     r->nip += 4;
1493     goto rti;
1494     }
1495    
1496     // Ignore illegal memory accesses?
1497     if (PrefsFindBool("ignoresegv")) {
1498     if (addr_mode == MODE_U || addr_mode == MODE_UX)
1499     r->gpr[ra] = addr;
1500     if (transfer_type == TYPE_LOAD)
1501     r->gpr[rd] = 0;
1502     r->nip += 4;
1503     goto rti;
1504     }
1505    
1506     // In GUI mode, show error alert
1507     if (!PrefsFindBool("nogui")) {
1508     char str[256];
1509     if (transfer_type == TYPE_LOAD || transfer_type == TYPE_STORE)
1510     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]);
1511     else
1512     sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode);
1513     ErrorAlert(str);
1514     QuitEmulator();
1515     return;
1516     }
1517     }
1518    
1519     // For all other errors, jump into debugger (sort of...)
1520     if (!ready_for_signals) {
1521     printf("SIGSEGV\n");
1522     printf(" sigcontext %p, pt_regs %p\n", sc, r);
1523     printf(
1524     " pc %08lx lr %08lx ctr %08lx msr %08lx\n"
1525     " xer %08lx cr %08lx \n"
1526     " r0 %08lx r1 %08lx r2 %08lx r3 %08lx\n"
1527     " r4 %08lx r5 %08lx r6 %08lx r7 %08lx\n"
1528     " r8 %08lx r9 %08lx r10 %08lx r11 %08lx\n"
1529     " r12 %08lx r13 %08lx r14 %08lx r15 %08lx\n"
1530     " r16 %08lx r17 %08lx r18 %08lx r19 %08lx\n"
1531     " r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n"
1532     " r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n"
1533     " r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n",
1534     r->nip, r->link, r->ctr, r->msr,
1535     r->xer, r->ccr,
1536     r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3],
1537     r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7],
1538     r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11],
1539     r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15],
1540     r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19],
1541     r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23],
1542     r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27],
1543     r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]);
1544     exit(1);
1545     QuitEmulator();
1546     return;
1547     } else {
1548     // We crashed. Save registers, tell tick thread and loop forever
1549     sigsegv_regs = *(sigregs *)r;
1550     emul_thread_fatal = true;
1551     for (;;) ;
1552     }
1553     rti:;
1554     }
1555    
1556    
1557     /*
1558     * SIGILL handler
1559     */
1560    
1561     static void sigill_handler(int sig, sigcontext_struct *sc)
1562     {
1563     pt_regs *r = sc->regs;
1564     char str[256];
1565    
1566     // Fault in Mac ROM or RAM?
1567     bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize));
1568     if (mac_fault) {
1569    
1570     // Get opcode and divide into fields
1571     uint32 opcode = *((uint32 *)r->nip);
1572     uint32 primop = opcode >> 26;
1573     uint32 exop = (opcode >> 1) & 0x3ff;
1574     uint32 ra = (opcode >> 16) & 0x1f;
1575     uint32 rb = (opcode >> 11) & 0x1f;
1576     uint32 rd = (opcode >> 21) & 0x1f;
1577     int32 imm = (int16)(opcode & 0xffff);
1578    
1579     switch (primop) {
1580     case 9: // POWER instructions
1581     case 22:
1582     power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->nip, r->gpr[1], opcode);
1583     ErrorAlert(str);
1584     QuitEmulator();
1585     return;
1586    
1587     case 31:
1588     switch (exop) {
1589     case 83: // mfmsr
1590     r->gpr[rd] = 0xf072;
1591     r->nip += 4;
1592     goto rti;
1593    
1594     case 210: // mtsr
1595     case 242: // mtsrin
1596     case 306: // tlbie
1597     r->nip += 4;
1598     goto rti;
1599    
1600     case 339: { // mfspr
1601     int spr = ra | (rb << 5);
1602     switch (spr) {
1603     case 0: // MQ
1604     case 22: // DEC
1605     case 952: // MMCR0
1606     case 953: // PMC1
1607     case 954: // PMC2
1608     case 955: // SIA
1609     case 956: // MMCR1
1610     case 957: // PMC3
1611     case 958: // PMC4
1612     case 959: // SDA
1613     r->nip += 4;
1614     goto rti;
1615     case 25: // SDR1
1616     r->gpr[rd] = 0xdead001f;
1617     r->nip += 4;
1618     goto rti;
1619     case 287: // PVR
1620     r->gpr[rd] = PVR;
1621     r->nip += 4;
1622     goto rti;
1623     }
1624     break;
1625     }
1626    
1627     case 467: { // mtspr
1628     int spr = ra | (rb << 5);
1629     switch (spr) {
1630     case 0: // MQ
1631     case 22: // DEC
1632     case 275: // SPRG3
1633     case 528: // IBAT0U
1634     case 529: // IBAT0L
1635     case 530: // IBAT1U
1636     case 531: // IBAT1L
1637     case 532: // IBAT2U
1638     case 533: // IBAT2L
1639     case 534: // IBAT3U
1640     case 535: // IBAT3L
1641     case 536: // DBAT0U
1642     case 537: // DBAT0L
1643     case 538: // DBAT1U
1644     case 539: // DBAT1L
1645     case 540: // DBAT2U
1646     case 541: // DBAT2L
1647     case 542: // DBAT3U
1648     case 543: // DBAT3L
1649     case 952: // MMCR0
1650     case 953: // PMC1
1651     case 954: // PMC2
1652     case 955: // SIA
1653     case 956: // MMCR1
1654     case 957: // PMC3
1655     case 958: // PMC4
1656     case 959: // SDA
1657     r->nip += 4;
1658     goto rti;
1659     }
1660     break;
1661     }
1662    
1663     case 29: case 107: case 152: case 153: // POWER instructions
1664     case 184: case 216: case 217: case 248:
1665     case 264: case 277: case 331: case 360:
1666     case 363: case 488: case 531: case 537:
1667     case 541: case 664: case 665: case 696:
1668     case 728: case 729: case 760: case 920:
1669     case 921: case 952:
1670     goto power_inst;
1671     }
1672     }
1673    
1674     // In GUI mode, show error alert
1675     if (!PrefsFindBool("nogui")) {
1676     sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode);
1677     ErrorAlert(str);
1678     QuitEmulator();
1679     return;
1680     }
1681     }
1682    
1683     // For all other errors, jump into debugger (sort of...)
1684     if (!ready_for_signals) {
1685     printf("SIGILL\n");
1686     printf(" sigcontext %p, pt_regs %p\n", sc, r);
1687     printf(
1688     " pc %08lx lr %08lx ctr %08lx msr %08lx\n"
1689     " xer %08lx cr %08lx \n"
1690     " r0 %08lx r1 %08lx r2 %08lx r3 %08lx\n"
1691     " r4 %08lx r5 %08lx r6 %08lx r7 %08lx\n"
1692     " r8 %08lx r9 %08lx r10 %08lx r11 %08lx\n"
1693     " r12 %08lx r13 %08lx r14 %08lx r15 %08lx\n"
1694     " r16 %08lx r17 %08lx r18 %08lx r19 %08lx\n"
1695     " r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n"
1696     " r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n"
1697     " r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n",
1698     r->nip, r->link, r->ctr, r->msr,
1699     r->xer, r->ccr,
1700     r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3],
1701     r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7],
1702     r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11],
1703     r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15],
1704     r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19],
1705     r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23],
1706     r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27],
1707     r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]);
1708     exit(1);
1709     QuitEmulator();
1710     return;
1711     } else {
1712     // We crashed. Save registers, tell tick thread and loop forever
1713     sigsegv_regs = *(sigregs *)r;
1714     emul_thread_fatal = true;
1715     for (;;) ;
1716     }
1717     rti:;
1718     }
1719     #endif
1720    
1721    
1722     /*
1723     * Display alert
1724     */
1725    
1726     #ifdef ENABLE_GTK
1727     static void dl_destroyed(void)
1728     {
1729     gtk_main_quit();
1730     }
1731    
1732     static void dl_quit(GtkWidget *dialog)
1733     {
1734     gtk_widget_destroy(dialog);
1735     }
1736    
1737     void display_alert(int title_id, int prefix_id, int button_id, const char *text)
1738     {
1739     char str[256];
1740     sprintf(str, GetString(prefix_id), text);
1741    
1742     GtkWidget *dialog = gtk_dialog_new();
1743     gtk_window_set_title(GTK_WINDOW(dialog), GetString(title_id));
1744     gtk_container_border_width(GTK_CONTAINER(dialog), 5);
1745     gtk_widget_set_uposition(GTK_WIDGET(dialog), 100, 150);
1746     gtk_signal_connect(GTK_OBJECT(dialog), "destroy", GTK_SIGNAL_FUNC(dl_destroyed), NULL);
1747    
1748     GtkWidget *label = gtk_label_new(str);
1749     gtk_widget_show(label);
1750     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), label, TRUE, TRUE, 0);
1751    
1752     GtkWidget *button = gtk_button_new_with_label(GetString(button_id));
1753     gtk_widget_show(button);
1754     gtk_signal_connect_object(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(dl_quit), GTK_OBJECT(dialog));
1755     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area), button, FALSE, FALSE, 0);
1756     GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1757     gtk_widget_grab_default(button);
1758     gtk_widget_show(dialog);
1759    
1760     gtk_main();
1761     }
1762     #endif
1763    
1764    
1765     /*
1766     * Display error alert
1767     */
1768    
1769     void ErrorAlert(const char *text)
1770     {
1771     #ifdef ENABLE_GTK
1772     if (PrefsFindBool("nogui") || x_display == NULL) {
1773     printf(GetString(STR_SHELL_ERROR_PREFIX), text);
1774     return;
1775     }
1776     VideoQuitFullScreen();
1777     display_alert(STR_ERROR_ALERT_TITLE, STR_GUI_ERROR_PREFIX, STR_QUIT_BUTTON, text);
1778     #else
1779     printf(GetString(STR_SHELL_ERROR_PREFIX), text);
1780     #endif
1781     }
1782    
1783    
1784     /*
1785     * Display warning alert
1786     */
1787    
1788     void WarningAlert(const char *text)
1789     {
1790     #ifdef ENABLE_GTK
1791     if (PrefsFindBool("nogui") || x_display == NULL) {
1792     printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1793     return;
1794     }
1795     display_alert(STR_WARNING_ALERT_TITLE, STR_GUI_WARNING_PREFIX, STR_OK_BUTTON, text);
1796     #else
1797     printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1798     #endif
1799     }
1800    
1801    
1802     /*
1803     * Display choice alert
1804     */
1805    
1806     bool ChoiceAlert(const char *text, const char *pos, const char *neg)
1807     {
1808     printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1809     return false; //!!
1810     }