ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/main_unix.cpp
Revision: 1.7
Committed: 2003-09-28T21:20:38Z (21 years, 1 month ago) by gbeauche
Branch: MAIN
Changes since 1.6: +61 -12 lines
Log Message:
Disable SIGUSR2 handler in EMULATED_PPC mode for now

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