ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/main_unix.cpp
Revision: 1.12
Committed: 2003-10-26T09:14:11Z (21 years ago) by gbeauche
Branch: MAIN
Changes since 1.11: +42 -5 lines
Log Message:
- enable multicore cpu emulation with ASYNC_IRQ
- move atomic_* operations to main_unix so that they could use spinlocks or
  other platform-specific locking mechanisms

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