ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/main_unix.cpp
Revision: 1.42
Committed: 2004-06-24T15:37:25Z (20 years, 4 months ago) by gbeauche
Branch: MAIN
Changes since 1.41: +37 -2 lines
Log Message:
SDL support in SheepShaver too, though it doesn't work in native mode
on Linux/ppc as libSDL is pulling in libpthread which conflicts with
our sheepthreads.

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * main_unix.cpp - Emulation core, Unix implementation
3     *
4 cebix 1.25 * SheepShaver (C) 1997-2004 Christian Bauer and Marc Hellwig
5 cebix 1.1 *
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 gbeauche 1.33 * There is apparently another problem when processing signals. In
69     * fullscreen mode, we get quick updates of the mouse position. This
70     * causes an increased number of calls to TriggerInterrupt(). And,
71     * since IRQ_NEST is not fully handled atomically, nested calls to
72     * ppc_interrupt() may cause stack corruption to eventually crash the
73     * emulator.
74     *
75     * FIXME:
76     * The current solution is to allocate another signal stack when
77     * processing ppc_interrupt(). However, it may be better to detect
78     * the INTFLAG_ADB case and handle it specifically with some extra mutex?
79     *
80 cebix 1.1 * TODO:
81     * check if SIGSEGV handler works for all registers (including FP!)
82     */
83    
84     #include <unistd.h>
85     #include <fcntl.h>
86     #include <time.h>
87     #include <errno.h>
88     #include <stdio.h>
89     #include <stdlib.h>
90     #include <string.h>
91     #include <pthread.h>
92     #include <sys/mman.h>
93     #include <sys/ipc.h>
94     #include <sys/shm.h>
95     #include <signal.h>
96    
97     #include "sysdeps.h"
98     #include "main.h"
99     #include "version.h"
100     #include "prefs.h"
101     #include "prefs_editor.h"
102     #include "cpu_emulation.h"
103     #include "emul_op.h"
104     #include "xlowmem.h"
105     #include "xpram.h"
106     #include "timer.h"
107     #include "adb.h"
108     #include "sony.h"
109     #include "disk.h"
110     #include "cdrom.h"
111     #include "scsi.h"
112     #include "video.h"
113     #include "audio.h"
114     #include "ether.h"
115     #include "serial.h"
116     #include "clip.h"
117     #include "extfs.h"
118     #include "sys.h"
119     #include "macos_util.h"
120     #include "rom_patches.h"
121     #include "user_strings.h"
122 gbeauche 1.4 #include "vm_alloc.h"
123 gbeauche 1.5 #include "sigsegv.h"
124 gbeauche 1.15 #include "thunks.h"
125 cebix 1.1
126     #define DEBUG 0
127     #include "debug.h"
128    
129    
130 gbeauche 1.42 #ifdef USE_SDL
131     #include <SDL.h>
132     #endif
133    
134     #ifndef USE_SDL_VIDEO
135 cebix 1.1 #include <X11/Xlib.h>
136 gbeauche 1.42 #endif
137 cebix 1.1
138     #ifdef ENABLE_GTK
139     #include <gtk/gtk.h>
140     #endif
141    
142     #ifdef ENABLE_XF86_DGA
143     #include <X11/Xlib.h>
144     #include <X11/Xutil.h>
145     #include <X11/extensions/xf86dga.h>
146     #endif
147    
148     #ifdef ENABLE_MON
149     #include "mon.h"
150     #endif
151    
152    
153 gbeauche 1.23 // Enable emulation of unaligned lmw/stmw?
154     #define EMULATE_UNALIGNED_LOADSTORE_MULTIPLE 1
155    
156 cebix 1.1 // Enable Execute68k() safety checks?
157     #define SAFE_EXEC_68K 0
158    
159     // Interrupts in EMUL_OP mode?
160     #define INTERRUPTS_IN_EMUL_OP_MODE 1
161    
162     // Interrupts in native mode?
163     #define INTERRUPTS_IN_NATIVE_MODE 1
164    
165 gbeauche 1.33 // Number of alternate stacks for signal handlers?
166     #define SIG_STACK_COUNT 4
167    
168 cebix 1.1
169     // Constants
170     const char ROM_FILE_NAME[] = "ROM";
171     const char ROM_FILE_NAME2[] = "Mac OS ROM";
172    
173 gbeauche 1.15 const uintptr RAM_BASE = 0x20000000; // Base address of RAM
174 cebix 1.1 const uint32 SIG_STACK_SIZE = 0x10000; // Size of signal stack
175    
176    
177     #if !EMULATED_PPC
178 gbeauche 1.26 struct sigregs {
179     uint32 nip;
180     uint32 link;
181     uint32 ctr;
182     uint32 msr;
183     uint32 xer;
184     uint32 ccr;
185     uint32 gpr[32];
186     };
187 cebix 1.1
188 gbeauche 1.26 #if defined(__linux__)
189 gbeauche 1.28 #include <sys/ucontext.h>
190     #define MACHINE_REGISTERS(scp) ((machine_regs *)(((ucontext_t *)scp)->uc_mcontext.regs))
191    
192 gbeauche 1.26 struct machine_regs : public pt_regs
193     {
194     u_long & cr() { return pt_regs::ccr; }
195     uint32 cr() const { return pt_regs::ccr; }
196     uint32 lr() const { return pt_regs::link; }
197     uint32 ctr() const { return pt_regs::ctr; }
198     uint32 xer() const { return pt_regs::xer; }
199     uint32 msr() const { return pt_regs::msr; }
200     uint32 dar() const { return pt_regs::dar; }
201     u_long & pc() { return pt_regs::nip; }
202     uint32 pc() const { return pt_regs::nip; }
203     u_long & gpr(int i) { return pt_regs::gpr[i]; }
204     uint32 gpr(int i) const { return pt_regs::gpr[i]; }
205 cebix 1.1 };
206 gbeauche 1.28 #endif
207    
208     #if defined(__APPLE__) && defined(__MACH__)
209     #include <sys/signal.h>
210     extern "C" int sigaltstack(const struct sigaltstack *ss, struct sigaltstack *oss);
211 gbeauche 1.26
212     #include <sys/ucontext.h>
213 gbeauche 1.28 #define MACHINE_REGISTERS(scp) ((machine_regs *)(((ucontext_t *)scp)->uc_mcontext))
214 gbeauche 1.26
215     struct machine_regs : public mcontext
216     {
217     uint32 & cr() { return ss.cr; }
218     uint32 cr() const { return ss.cr; }
219     uint32 lr() const { return ss.lr; }
220     uint32 ctr() const { return ss.ctr; }
221     uint32 xer() const { return ss.xer; }
222     uint32 msr() const { return ss.srr1; }
223     uint32 dar() const { return es.dar; }
224     uint32 & pc() { return ss.srr0; }
225     uint32 pc() const { return ss.srr0; }
226     uint32 & gpr(int i) { return (&ss.r0)[i]; }
227     uint32 gpr(int i) const { return (&ss.r0)[i]; }
228     };
229     #endif
230    
231     static void build_sigregs(sigregs *srp, machine_regs *mrp)
232     {
233     srp->nip = mrp->pc();
234     srp->link = mrp->lr();
235     srp->ctr = mrp->ctr();
236     srp->msr = mrp->msr();
237     srp->xer = mrp->xer();
238     srp->ccr = mrp->cr();
239     for (int i = 0; i < 32; i++)
240     srp->gpr[i] = mrp->gpr(i);
241     }
242 gbeauche 1.33
243     static struct sigaltstack sig_stacks[SIG_STACK_COUNT]; // Stacks for signal handlers
244     static int sig_stack_id = 0; // Stack slot currently used
245    
246     static inline void sig_stack_acquire(void)
247     {
248     if (++sig_stack_id == SIG_STACK_COUNT) {
249     printf("FATAL: signal stack overflow\n");
250     return;
251     }
252     sigaltstack(&sig_stacks[sig_stack_id], NULL);
253     }
254    
255     static inline void sig_stack_release(void)
256     {
257     if (--sig_stack_id < 0) {
258     printf("FATAL: signal stack underflow\n");
259     return;
260     }
261     sigaltstack(&sig_stacks[sig_stack_id], NULL);
262     }
263 cebix 1.1 #endif
264    
265    
266     // Global variables (exported)
267     #if !EMULATED_PPC
268     void *TOC; // Small data pointer (r13)
269     #endif
270     uint32 RAMBase; // Base address of Mac RAM
271     uint32 RAMSize; // Size of Mac RAM
272     uint32 KernelDataAddr; // Address of Kernel Data
273     uint32 BootGlobsAddr; // Address of BootGlobs structure at top of Mac RAM
274 gbeauche 1.36 uint32 DRCacheAddr; // Address of DR Cache
275 cebix 1.1 uint32 PVR; // Theoretical PVR
276     int64 CPUClockSpeed; // Processor clock speed (Hz)
277     int64 BusClockSpeed; // Bus clock speed (Hz)
278    
279    
280     // Global variables
281 gbeauche 1.42 #ifndef USE_SDL_VIDEO
282 gbeauche 1.11 char *x_display_name = NULL; // X11 display name
283 cebix 1.1 Display *x_display = NULL; // X11 display handle
284 gbeauche 1.21 #ifdef X11_LOCK_TYPE
285     X11_LOCK_TYPE x_display_lock = X11_LOCK_INIT; // X11 display lock
286     #endif
287 gbeauche 1.42 #endif
288 cebix 1.1
289     static int zero_fd = 0; // FD of /dev/zero
290     static bool lm_area_mapped = false; // Flag: Low Memory area mmap()ped
291     static int kernel_area = -1; // SHM ID of Kernel Data area
292     static bool rom_area_mapped = false; // Flag: Mac ROM mmap()ped
293     static bool ram_area_mapped = false; // Flag: Mac RAM mmap()ped
294 gbeauche 1.36 static bool dr_cache_area_mapped = false; // Flag: Mac DR Cache mmap()ped
295     static bool dr_emulator_area_mapped = false;// Flag: Mac DR Emulator mmap()ped
296 cebix 1.1 static KernelData *kernel_data; // Pointer to Kernel Data
297     static EmulatorData *emulator_data;
298    
299     static uint8 last_xpram[XPRAM_SIZE]; // Buffer for monitoring XPRAM changes
300    
301     static bool nvram_thread_active = false; // Flag: NVRAM watchdog installed
302 gbeauche 1.40 static volatile bool nvram_thread_cancel; // Flag: Cancel NVRAM thread
303 cebix 1.1 static pthread_t nvram_thread; // NVRAM watchdog
304     static bool tick_thread_active = false; // Flag: MacOS thread installed
305 gbeauche 1.40 static volatile bool tick_thread_cancel; // Flag: Cancel 60Hz thread
306 cebix 1.1 static pthread_t tick_thread; // 60Hz thread
307     static pthread_t emul_thread; // MacOS thread
308    
309     static bool ready_for_signals = false; // Handler installed, signals can be sent
310     static int64 num_segv = 0; // Number of handled SEGV signals
311    
312 gbeauche 1.6 static struct sigaction sigusr2_action; // Interrupt signal (of emulator thread)
313 gbeauche 1.20 #if EMULATED_PPC
314     static uintptr sig_stack = 0; // Stack for PowerPC interrupt routine
315     #else
316 cebix 1.1 static struct sigaction sigsegv_action; // Data access exception signal (of emulator thread)
317     static struct sigaction sigill_action; // Illegal instruction signal (of emulator thread)
318     static bool emul_thread_fatal = false; // Flag: MacOS thread crashed, tick thread shall dump debug output
319     static sigregs sigsegv_regs; // Register dump when crashed
320 gbeauche 1.23 static const char *crash_reason = NULL; // Reason of the crash (SIGSEGV, SIGBUS, SIGILL)
321 cebix 1.1 #endif
322    
323 gbeauche 1.31 uint32 SheepMem::page_size; // Size of a native page
324 gbeauche 1.18 uintptr SheepMem::zero_page = 0; // Address of ro page filled in with zeros
325 gbeauche 1.15 uintptr SheepMem::base = 0x60000000; // Address of SheepShaver data
326     uintptr SheepMem::top = 0; // Top of SheepShaver data (stack like storage)
327    
328 cebix 1.1
329     // Prototypes
330     static void Quit(void);
331     static void *emul_func(void *arg);
332     static void *nvram_func(void *arg);
333     static void *tick_func(void *arg);
334 gbeauche 1.8 #if EMULATED_PPC
335 gbeauche 1.13 extern void emul_ppc(uint32 start);
336     extern void init_emul_ppc(void);
337     extern void exit_emul_ppc(void);
338 gbeauche 1.8 #else
339 gbeauche 1.26 static void sigusr2_handler(int sig, siginfo_t *sip, void *scp);
340     static void sigsegv_handler(int sig, siginfo_t *sip, void *scp);
341     static void sigill_handler(int sig, siginfo_t *sip, void *scp);
342 cebix 1.1 #endif
343    
344    
345     // From asm_linux.S
346 gbeauche 1.12 #if !EMULATED_PPC
347 cebix 1.1 extern "C" void *get_toc(void);
348     extern "C" void *get_sp(void);
349     extern "C" void flush_icache_range(void *start, void *end);
350     extern "C" void jump_to_rom(uint32 entry, uint32 context);
351     extern "C" void quit_emulator(void);
352     extern "C" void execute_68k(uint32 pc, M68kRegisters *r);
353     extern "C" void ppc_interrupt(uint32 entry, uint32 kernel_data);
354     extern "C" int atomic_add(int *var, int v);
355     extern "C" int atomic_and(int *var, int v);
356     extern "C" int atomic_or(int *var, int v);
357     extern void paranoia_check(void);
358 gbeauche 1.12 #endif
359    
360    
361     #if EMULATED_PPC
362     /*
363 gbeauche 1.20 * Return signal stack base
364     */
365    
366     uintptr SignalStackBase(void)
367     {
368     return sig_stack + SIG_STACK_SIZE;
369     }
370    
371    
372     /*
373 gbeauche 1.12 * Atomic operations
374     */
375    
376     #if HAVE_SPINLOCKS
377     static spinlock_t atomic_ops_lock = SPIN_LOCK_UNLOCKED;
378     #else
379     #define spin_lock(LOCK)
380     #define spin_unlock(LOCK)
381     #endif
382    
383     int atomic_add(int *var, int v)
384     {
385     spin_lock(&atomic_ops_lock);
386     int ret = *var;
387     *var += v;
388     spin_unlock(&atomic_ops_lock);
389     return ret;
390     }
391    
392     int atomic_and(int *var, int v)
393     {
394     spin_lock(&atomic_ops_lock);
395     int ret = *var;
396     *var &= v;
397     spin_unlock(&atomic_ops_lock);
398     return ret;
399     }
400    
401     int atomic_or(int *var, int v)
402     {
403     spin_lock(&atomic_ops_lock);
404     int ret = *var;
405     *var |= v;
406     spin_unlock(&atomic_ops_lock);
407     return ret;
408     }
409 cebix 1.1 #endif
410    
411    
412     /*
413     * Main program
414     */
415    
416     static void usage(const char *prg_name)
417     {
418     printf("Usage: %s [OPTION...]\n", prg_name);
419     printf("\nUnix options:\n");
420     printf(" --display STRING\n X display to use\n");
421     PrefsPrintUsage();
422     exit(0);
423     }
424    
425     int main(int argc, char **argv)
426     {
427     char str[256];
428     uint32 *boot_globs;
429     int16 i16;
430     int rom_fd;
431     FILE *proc_file;
432     const char *rom_path;
433     uint32 rom_size, actual;
434     uint8 *rom_tmp;
435     time_t now, expire;
436    
437     // Initialize variables
438     RAMBase = 0;
439     tzset();
440    
441     // Print some info
442     printf(GetString(STR_ABOUT_TEXT1), VERSION_MAJOR, VERSION_MINOR);
443     printf(" %s\n", GetString(STR_ABOUT_TEXT2));
444    
445     #if !EMULATED_PPC
446     // Get TOC pointer
447     TOC = get_toc();
448     #endif
449    
450     #ifdef ENABLE_GTK
451     // Init GTK
452     gtk_set_locale();
453     gtk_init(&argc, &argv);
454     #endif
455    
456     // Read preferences
457     PrefsInit(argc, argv);
458    
459     // Parse command line arguments
460     for (int i=1; i<argc; i++) {
461     if (strcmp(argv[i], "--help") == 0) {
462     usage(argv[0]);
463 gbeauche 1.42 #ifndef USE_SDL_VIDEO
464 cebix 1.1 } else if (strcmp(argv[i], "--display") == 0) {
465     i++;
466     if (i < argc)
467     x_display_name = strdup(argv[i]);
468 gbeauche 1.42 #endif
469 cebix 1.1 } else if (argv[i][0] == '-') {
470     fprintf(stderr, "Unrecognized option '%s'\n", argv[i]);
471     usage(argv[0]);
472     }
473     }
474    
475 gbeauche 1.42 #ifdef USE_SDL
476     // Initialize SDL system
477     int sdl_flags = 0;
478     #ifdef USE_SDL_VIDEO
479     sdl_flags |= SDL_INIT_VIDEO;
480     #endif
481     assert(sdl_flags != 0);
482     if (SDL_Init(sdl_flags) == -1) {
483     char str[256];
484     sprintf(str, "Could not initialize SDL: %s.\n", SDL_GetError());
485     ErrorAlert(str);
486     goto quit;
487     }
488     atexit(SDL_Quit);
489     #endif
490    
491     #ifndef USE_SDL_VIDEO
492 cebix 1.1 // Open display
493     x_display = XOpenDisplay(x_display_name);
494     if (x_display == NULL) {
495     char str[256];
496     sprintf(str, GetString(STR_NO_XSERVER_ERR), XDisplayName(x_display_name));
497     ErrorAlert(str);
498     goto quit;
499     }
500    
501     #if defined(ENABLE_XF86_DGA) && !defined(ENABLE_MON)
502     // Fork out, so we can return from fullscreen mode when things get ugly
503     XF86DGAForkApp(DefaultScreen(x_display));
504     #endif
505 gbeauche 1.42 #endif
506 cebix 1.1
507     #ifdef ENABLE_MON
508     // Initialize mon
509     mon_init();
510     #endif
511    
512     // Get system info
513     PVR = 0x00040000; // Default: 604
514     CPUClockSpeed = 100000000; // Default: 100MHz
515     BusClockSpeed = 100000000; // Default: 100MHz
516 gbeauche 1.30 #if EMULATED_PPC
517     PVR = 0x000c0000; // Default: 7400 (with AltiVec)
518 gbeauche 1.39 #elif defined(__APPLE__) && defined(__MACH__)
519     proc_file = popen("ioreg -c IOPlatformDevice", "r");
520     if (proc_file) {
521     char line[256];
522     bool powerpc_node = false;
523     while (fgets(line, sizeof(line) - 1, proc_file)) {
524     // Read line
525     int len = strlen(line);
526     if (len == 0)
527     continue;
528     line[len - 1] = 0;
529    
530     // Parse line
531     if (strstr(line, "o PowerPC,"))
532     powerpc_node = true;
533     else if (powerpc_node) {
534     uint32 value;
535     char head[256];
536     if (sscanf(line, "%[ |]\"cpu-version\" = <%x>", head, &value) == 2)
537     PVR = value;
538     else if (sscanf(line, "%[ |]\"clock-frequency\" = <%x>", head, &value) == 2)
539     CPUClockSpeed = value;
540     else if (sscanf(line, "%[ |]\"bus-frequency\" = <%x>", head, &value) == 2)
541     BusClockSpeed = value;
542     else if (strchr(line, '}'))
543     powerpc_node = false;
544     }
545     }
546     fclose(proc_file);
547     } else {
548     sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno));
549     WarningAlert(str);
550     }
551 gbeauche 1.30 #else
552 cebix 1.1 proc_file = fopen("/proc/cpuinfo", "r");
553     if (proc_file) {
554     char line[256];
555     while(fgets(line, 255, proc_file)) {
556     // Read line
557     int len = strlen(line);
558     if (len == 0)
559     continue;
560     line[len-1] = 0;
561    
562     // Parse line
563     int i;
564     char value[256];
565 gbeauche 1.29 if (sscanf(line, "cpu : %[0-9A-Za-a]", value) == 1) {
566 cebix 1.1 if (strcmp(value, "601") == 0)
567     PVR = 0x00010000;
568     else if (strcmp(value, "603") == 0)
569     PVR = 0x00030000;
570     else if (strcmp(value, "604") == 0)
571     PVR = 0x00040000;
572     else if (strcmp(value, "603e") == 0)
573     PVR = 0x00060000;
574     else if (strcmp(value, "603ev") == 0)
575     PVR = 0x00070000;
576     else if (strcmp(value, "604e") == 0)
577     PVR = 0x00090000;
578     else if (strcmp(value, "604ev5") == 0)
579     PVR = 0x000a0000;
580     else if (strcmp(value, "750") == 0)
581     PVR = 0x00080000;
582     else if (strcmp(value, "821") == 0)
583     PVR = 0x00320000;
584     else if (strcmp(value, "860") == 0)
585     PVR = 0x00500000;
586 gbeauche 1.29 else if (strcmp(value, "7400") == 0)
587 gbeauche 1.30 PVR = 0x000c0000;
588 gbeauche 1.29 else if (strcmp(value, "7410") == 0)
589     PVR = 0x800c0000;
590 cebix 1.1 else
591     printf("WARNING: Unknown CPU type '%s', assuming 604\n", value);
592     }
593     if (sscanf(line, "clock : %dMHz", &i) == 1)
594     CPUClockSpeed = BusClockSpeed = i * 1000000;
595     }
596     fclose(proc_file);
597     } else {
598     sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno));
599     WarningAlert(str);
600     }
601 gbeauche 1.34
602     // Get actual bus frequency
603     proc_file = fopen("/proc/device-tree/clock-frequency", "r");
604     if (proc_file) {
605     union { uint8 b[4]; uint32 l; } value;
606     if (fread(value.b, sizeof(value), 1, proc_file) == 1)
607     BusClockSpeed = value.l;
608     fclose(proc_file);
609     }
610 cebix 1.1 #endif
611     D(bug("PVR: %08x (assumed)\n", PVR));
612    
613     // Init system routines
614     SysInit();
615    
616     // Show preferences editor
617     if (!PrefsFindBool("nogui"))
618     if (!PrefsEditor())
619     goto quit;
620    
621     #if !EMULATED_PPC
622     // Check some things
623     paranoia_check();
624     #endif
625    
626     // Open /dev/zero
627     zero_fd = open("/dev/zero", O_RDWR);
628     if (zero_fd < 0) {
629     sprintf(str, GetString(STR_NO_DEV_ZERO_ERR), strerror(errno));
630     ErrorAlert(str);
631     goto quit;
632     }
633    
634 gbeauche 1.26 #ifndef PAGEZERO_HACK
635 cebix 1.1 // Create Low Memory area (0x0000..0x3000)
636 gbeauche 1.4 if (vm_acquire_fixed((char *)0, 0x3000) < 0) {
637 cebix 1.1 sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno));
638     ErrorAlert(str);
639     goto quit;
640     }
641     lm_area_mapped = true;
642 gbeauche 1.26 #endif
643 cebix 1.1
644     // Create areas for Kernel Data
645     kernel_area = shmget(IPC_PRIVATE, KERNEL_AREA_SIZE, 0600);
646     if (kernel_area == -1) {
647     sprintf(str, GetString(STR_KD_SHMGET_ERR), strerror(errno));
648     ErrorAlert(str);
649     goto quit;
650     }
651     if (shmat(kernel_area, (void *)KERNEL_DATA_BASE, 0) < 0) {
652     sprintf(str, GetString(STR_KD_SHMAT_ERR), strerror(errno));
653     ErrorAlert(str);
654     goto quit;
655     }
656     if (shmat(kernel_area, (void *)KERNEL_DATA2_BASE, 0) < 0) {
657     sprintf(str, GetString(STR_KD2_SHMAT_ERR), strerror(errno));
658     ErrorAlert(str);
659     goto quit;
660     }
661 gbeauche 1.15 kernel_data = (KernelData *)KERNEL_DATA_BASE;
662 cebix 1.1 emulator_data = &kernel_data->ed;
663 gbeauche 1.15 KernelDataAddr = KERNEL_DATA_BASE;
664 cebix 1.1 D(bug("Kernel Data at %p, Emulator Data at %p\n", kernel_data, emulator_data));
665    
666 gbeauche 1.36 // Create area for DR Cache
667     if (vm_acquire_fixed((void *)DR_EMULATOR_BASE, DR_EMULATOR_SIZE) < 0) {
668     sprintf(str, GetString(STR_DR_EMULATOR_MMAP_ERR), strerror(errno));
669     ErrorAlert(str);
670     goto quit;
671     }
672     dr_emulator_area_mapped = true;
673     if (vm_acquire_fixed((void *)DR_CACHE_BASE, DR_CACHE_SIZE) < 0) {
674     sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno));
675     ErrorAlert(str);
676     goto quit;
677     }
678     dr_cache_area_mapped = true;
679 gbeauche 1.38 #if !EMULATED_PPC
680     if (vm_protect((char *)DR_CACHE_BASE, DR_CACHE_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) {
681     sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno));
682     ErrorAlert(str);
683     goto quit;
684     }
685     #endif
686 gbeauche 1.36 DRCacheAddr = DR_CACHE_BASE;
687     D(bug("DR Cache at %p\n", DRCacheAddr));
688    
689 gbeauche 1.8 // Create area for SheepShaver data
690 gbeauche 1.15 if (!SheepMem::Init()) {
691 gbeauche 1.8 sprintf(str, GetString(STR_SHEEP_MEM_MMAP_ERR), strerror(errno));
692     ErrorAlert(str);
693     goto quit;
694     }
695    
696 cebix 1.1 // Create area for Mac ROM
697 gbeauche 1.4 if (vm_acquire_fixed((char *)ROM_BASE, ROM_AREA_SIZE) < 0) {
698 cebix 1.1 sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno));
699     ErrorAlert(str);
700     goto quit;
701     }
702 gbeauche 1.27 #if !EMULATED_PPC
703 gbeauche 1.4 if (vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) {
704     sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno));
705     ErrorAlert(str);
706     goto quit;
707     }
708     #endif
709 cebix 1.1 rom_area_mapped = true;
710     D(bug("ROM area at %08x\n", ROM_BASE));
711    
712     // Create area for Mac RAM
713     RAMSize = PrefsFindInt32("ramsize");
714     if (RAMSize < 8*1024*1024) {
715     WarningAlert(GetString(STR_SMALL_RAM_WARN));
716     RAMSize = 8*1024*1024;
717     }
718    
719 gbeauche 1.8 if (vm_acquire_fixed((char *)RAM_BASE, RAMSize) < 0) {
720 cebix 1.1 sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno));
721     ErrorAlert(str);
722     goto quit;
723     }
724 gbeauche 1.4 #if !EMULATED_PPC
725 gbeauche 1.8 if (vm_protect((char *)RAM_BASE, RAMSize, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) {
726 gbeauche 1.4 sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno));
727     ErrorAlert(str);
728     goto quit;
729     }
730     #endif
731 gbeauche 1.8 RAMBase = RAM_BASE;
732 cebix 1.1 ram_area_mapped = true;
733     D(bug("RAM area at %08x\n", RAMBase));
734    
735     if (RAMBase > ROM_BASE) {
736     ErrorAlert(GetString(STR_RAM_HIGHER_THAN_ROM_ERR));
737     goto quit;
738     }
739    
740     // Load Mac ROM
741     rom_path = PrefsFindString("rom");
742     rom_fd = open(rom_path ? rom_path : ROM_FILE_NAME, O_RDONLY);
743     if (rom_fd < 0) {
744     rom_fd = open(rom_path ? rom_path : ROM_FILE_NAME2, O_RDONLY);
745     if (rom_fd < 0) {
746     ErrorAlert(GetString(STR_NO_ROM_FILE_ERR));
747     goto quit;
748     }
749     }
750     printf(GetString(STR_READING_ROM_FILE));
751     rom_size = lseek(rom_fd, 0, SEEK_END);
752     lseek(rom_fd, 0, SEEK_SET);
753     rom_tmp = new uint8[ROM_SIZE];
754     actual = read(rom_fd, (void *)rom_tmp, ROM_SIZE);
755     close(rom_fd);
756 gbeauche 1.3
757     // Decode Mac ROM
758     if (!DecodeROM(rom_tmp, actual)) {
759     if (rom_size != 4*1024*1024) {
760 cebix 1.1 ErrorAlert(GetString(STR_ROM_SIZE_ERR));
761     goto quit;
762     } else {
763     ErrorAlert(GetString(STR_ROM_FILE_READ_ERR));
764     goto quit;
765     }
766     }
767 gbeauche 1.3 delete[] rom_tmp;
768 cebix 1.1
769     // Load NVRAM
770     XPRAMInit();
771    
772 gbeauche 1.32 // Load XPRAM default values if signature not found
773     if (XPRAM[0x130c] != 0x4e || XPRAM[0x130d] != 0x75
774     || XPRAM[0x130e] != 0x4d || XPRAM[0x130f] != 0x63) {
775     D(bug("Loading XPRAM default values\n"));
776     memset(XPRAM + 0x1300, 0, 0x100);
777     XPRAM[0x130c] = 0x4e; // "NuMc" signature
778     XPRAM[0x130d] = 0x75;
779     XPRAM[0x130e] = 0x4d;
780     XPRAM[0x130f] = 0x63;
781     XPRAM[0x1301] = 0x80; // InternalWaitFlags = DynWait (don't wait for SCSI devices upon bootup)
782     XPRAM[0x1310] = 0xa8; // Standard PRAM values
783     XPRAM[0x1311] = 0x00;
784     XPRAM[0x1312] = 0x00;
785     XPRAM[0x1313] = 0x22;
786     XPRAM[0x1314] = 0xcc;
787     XPRAM[0x1315] = 0x0a;
788     XPRAM[0x1316] = 0xcc;
789     XPRAM[0x1317] = 0x0a;
790     XPRAM[0x131c] = 0x00;
791     XPRAM[0x131d] = 0x02;
792     XPRAM[0x131e] = 0x63;
793     XPRAM[0x131f] = 0x00;
794     XPRAM[0x1308] = 0x13;
795     XPRAM[0x1309] = 0x88;
796     XPRAM[0x130a] = 0x00;
797     XPRAM[0x130b] = 0xcc;
798     XPRAM[0x1376] = 0x00; // OSDefault = MacOS
799     XPRAM[0x1377] = 0x01;
800     }
801    
802 cebix 1.1 // Set boot volume
803 cebix 1.10 i16 = PrefsFindInt32("bootdrive");
804 cebix 1.1 XPRAM[0x1378] = i16 >> 8;
805     XPRAM[0x1379] = i16 & 0xff;
806 cebix 1.10 i16 = PrefsFindInt32("bootdriver");
807 cebix 1.1 XPRAM[0x137a] = i16 >> 8;
808     XPRAM[0x137b] = i16 & 0xff;
809    
810     // Create BootGlobs at top of Mac memory
811     memset((void *)(RAMBase + RAMSize - 4096), 0, 4096);
812     BootGlobsAddr = RAMBase + RAMSize - 0x1c;
813     boot_globs = (uint32 *)BootGlobsAddr;
814     boot_globs[-5] = htonl(RAMBase + RAMSize); // MemTop
815     boot_globs[0] = htonl(RAMBase); // First RAM bank
816     boot_globs[1] = htonl(RAMSize);
817     boot_globs[2] = htonl((uint32)-1); // End of bank table
818    
819 gbeauche 1.15 // Init thunks
820     if (!ThunksInit())
821     goto quit;
822    
823 cebix 1.1 // Init drivers
824     SonyInit();
825     DiskInit();
826     CDROMInit();
827     SCSIInit();
828    
829     // Init external file system
830     ExtFSInit();
831    
832 gbeauche 1.22 // Init ADB
833     ADBInit();
834    
835 cebix 1.1 // Init audio
836     AudioInit();
837    
838     // Init network
839     EtherInit();
840    
841     // Init serial ports
842     SerialInit();
843    
844     // Init Time Manager
845     TimerInit();
846    
847     // Init clipboard
848     ClipInit();
849    
850     // Init video
851     if (!VideoInit())
852     goto quit;
853    
854     // Install ROM patches
855     if (!PatchROM()) {
856     ErrorAlert(GetString(STR_UNSUPPORTED_ROM_TYPE_ERR));
857     goto quit;
858     }
859    
860     // Clear caches (as we loaded and patched code) and write protect ROM
861     #if !EMULATED_PPC
862     MakeExecutable(0, (void *)ROM_BASE, ROM_AREA_SIZE);
863     #endif
864 gbeauche 1.4 vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_EXECUTE);
865 cebix 1.1
866     // Initialize Kernel Data
867     memset(kernel_data, 0, sizeof(KernelData));
868     if (ROMType == ROMTYPE_NEWWORLD) {
869 gbeauche 1.15 uintptr of_dev_tree = SheepMem::Reserve(4 * sizeof(uint32));
870     memset((void *)of_dev_tree, 0, 4 * sizeof(uint32));
871     uintptr vector_lookup_tbl = SheepMem::Reserve(128);
872     uintptr vector_mask_tbl = SheepMem::Reserve(64);
873 cebix 1.1 memset((uint8 *)kernel_data + 0xb80, 0x3d, 0x80);
874 gbeauche 1.15 memset((void *)vector_lookup_tbl, 0, 128);
875     memset((void *)vector_mask_tbl, 0, 64);
876 cebix 1.1 kernel_data->v[0xb80 >> 2] = htonl(ROM_BASE);
877 gbeauche 1.15 kernel_data->v[0xb84 >> 2] = htonl(of_dev_tree); // OF device tree base
878     kernel_data->v[0xb90 >> 2] = htonl(vector_lookup_tbl);
879     kernel_data->v[0xb94 >> 2] = htonl(vector_mask_tbl);
880 cebix 1.1 kernel_data->v[0xb98 >> 2] = htonl(ROM_BASE); // OpenPIC base
881     kernel_data->v[0xbb0 >> 2] = htonl(0); // ADB base
882     kernel_data->v[0xc20 >> 2] = htonl(RAMSize);
883     kernel_data->v[0xc24 >> 2] = htonl(RAMSize);
884     kernel_data->v[0xc30 >> 2] = htonl(RAMSize);
885     kernel_data->v[0xc34 >> 2] = htonl(RAMSize);
886     kernel_data->v[0xc38 >> 2] = htonl(0x00010020);
887     kernel_data->v[0xc3c >> 2] = htonl(0x00200001);
888     kernel_data->v[0xc40 >> 2] = htonl(0x00010000);
889     kernel_data->v[0xc50 >> 2] = htonl(RAMBase);
890     kernel_data->v[0xc54 >> 2] = htonl(RAMSize);
891     kernel_data->v[0xf60 >> 2] = htonl(PVR);
892 gbeauche 1.34 kernel_data->v[0xf64 >> 2] = htonl(CPUClockSpeed); // clock-frequency
893     kernel_data->v[0xf68 >> 2] = htonl(BusClockSpeed); // bus-frequency
894     kernel_data->v[0xf6c >> 2] = htonl(BusClockSpeed / 4); // timebase-frequency
895 cebix 1.1 } else {
896     kernel_data->v[0xc80 >> 2] = htonl(RAMSize);
897     kernel_data->v[0xc84 >> 2] = htonl(RAMSize);
898     kernel_data->v[0xc90 >> 2] = htonl(RAMSize);
899     kernel_data->v[0xc94 >> 2] = htonl(RAMSize);
900     kernel_data->v[0xc98 >> 2] = htonl(0x00010020);
901     kernel_data->v[0xc9c >> 2] = htonl(0x00200001);
902     kernel_data->v[0xca0 >> 2] = htonl(0x00010000);
903     kernel_data->v[0xcb0 >> 2] = htonl(RAMBase);
904     kernel_data->v[0xcb4 >> 2] = htonl(RAMSize);
905     kernel_data->v[0xf80 >> 2] = htonl(PVR);
906 gbeauche 1.34 kernel_data->v[0xf84 >> 2] = htonl(CPUClockSpeed); // clock-frequency
907     kernel_data->v[0xf88 >> 2] = htonl(BusClockSpeed); // bus-frequency
908     kernel_data->v[0xf8c >> 2] = htonl(BusClockSpeed / 4); // timebase-frequency
909 cebix 1.1 }
910    
911     // Initialize extra low memory
912     D(bug("Initializing Low Memory...\n"));
913     memset(NULL, 0, 0x3000);
914     WriteMacInt32(XLM_SIGNATURE, FOURCC('B','a','a','h')); // Signature to detect SheepShaver
915 gbeauche 1.15 WriteMacInt32(XLM_KERNEL_DATA, KernelDataAddr); // For trap replacement routines
916 cebix 1.1 WriteMacInt32(XLM_PVR, PVR); // Theoretical PVR
917     WriteMacInt32(XLM_BUS_CLOCK, BusClockSpeed); // For DriverServicesLib patch
918     WriteMacInt16(XLM_EXEC_RETURN_OPCODE, M68K_EXEC_RETURN); // For Execute68k() (RTS from the executed 68k code will jump here and end 68k mode)
919 gbeauche 1.18 WriteMacInt32(XLM_ZERO_PAGE, SheepMem::ZeroPage()); // Pointer to read-only page with all bits set to 0
920 gbeauche 1.17 #if !EMULATED_PPC
921     WriteMacInt32(XLM_TOC, (uint32)TOC); // TOC pointer of emulator
922     #endif
923     WriteMacInt32(XLM_ETHER_INIT, NativeFunction(NATIVE_ETHER_INIT)); // DLPI ethernet driver functions
924 gbeauche 1.15 WriteMacInt32(XLM_ETHER_TERM, NativeFunction(NATIVE_ETHER_TERM));
925     WriteMacInt32(XLM_ETHER_OPEN, NativeFunction(NATIVE_ETHER_OPEN));
926     WriteMacInt32(XLM_ETHER_CLOSE, NativeFunction(NATIVE_ETHER_CLOSE));
927     WriteMacInt32(XLM_ETHER_WPUT, NativeFunction(NATIVE_ETHER_WPUT));
928     WriteMacInt32(XLM_ETHER_RSRV, NativeFunction(NATIVE_ETHER_RSRV));
929     WriteMacInt32(XLM_VIDEO_DOIO, NativeFunction(NATIVE_VIDEO_DO_DRIVER_IO));
930 cebix 1.1 D(bug("Low Memory initialized\n"));
931    
932     // Start 60Hz thread
933 gbeauche 1.40 tick_thread_cancel = false;
934 cebix 1.1 tick_thread_active = (pthread_create(&tick_thread, NULL, tick_func, NULL) == 0);
935     D(bug("Tick thread installed (%ld)\n", tick_thread));
936    
937     // Start NVRAM watchdog thread
938     memcpy(last_xpram, XPRAM, XPRAM_SIZE);
939 gbeauche 1.40 nvram_thread_cancel = false;
940 cebix 1.1 nvram_thread_active = (pthread_create(&nvram_thread, NULL, nvram_func, NULL) == 0);
941     D(bug("NVRAM thread installed (%ld)\n", nvram_thread));
942    
943     #if !EMULATED_PPC
944     // Create and install stacks for signal handlers
945 gbeauche 1.33 for (int i = 0; i < SIG_STACK_COUNT; i++) {
946     void *sig_stack = malloc(SIG_STACK_SIZE);
947     D(bug("Signal stack %d at %p\n", i, sig_stack));
948     if (sig_stack == NULL) {
949     ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR));
950     goto quit;
951     }
952     sig_stacks[i].ss_sp = sig_stack;
953     sig_stacks[i].ss_flags = 0;
954     sig_stacks[i].ss_size = SIG_STACK_SIZE;
955 cebix 1.1 }
956 gbeauche 1.33 sig_stack_id = 0;
957     if (sigaltstack(&sig_stacks[0], NULL) < 0) {
958 cebix 1.1 sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno));
959     ErrorAlert(str);
960     goto quit;
961     }
962     #endif
963    
964     #if !EMULATED_PPC
965 gbeauche 1.23 // Install SIGSEGV and SIGBUS handlers
966 cebix 1.1 sigemptyset(&sigsegv_action.sa_mask); // Block interrupts during SEGV handling
967     sigaddset(&sigsegv_action.sa_mask, SIGUSR2);
968 gbeauche 1.26 sigsegv_action.sa_sigaction = sigsegv_handler;
969     sigsegv_action.sa_flags = SA_ONSTACK | SA_SIGINFO;
970     #ifdef HAVE_SIGNAL_SA_RESTORER
971 cebix 1.1 sigsegv_action.sa_restorer = NULL;
972 gbeauche 1.26 #endif
973 cebix 1.1 if (sigaction(SIGSEGV, &sigsegv_action, NULL) < 0) {
974     sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno));
975     ErrorAlert(str);
976     goto quit;
977     }
978 gbeauche 1.23 if (sigaction(SIGBUS, &sigsegv_action, NULL) < 0) {
979     sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno));
980     ErrorAlert(str);
981     goto quit;
982     }
983 cebix 1.1
984     // Install SIGILL handler
985     sigemptyset(&sigill_action.sa_mask); // Block interrupts during ILL handling
986     sigaddset(&sigill_action.sa_mask, SIGUSR2);
987 gbeauche 1.26 sigill_action.sa_sigaction = sigill_handler;
988     sigill_action.sa_flags = SA_ONSTACK | SA_SIGINFO;
989     #ifdef HAVE_SIGNAL_SA_RESTORER
990 cebix 1.1 sigill_action.sa_restorer = NULL;
991 gbeauche 1.26 #endif
992 cebix 1.1 if (sigaction(SIGILL, &sigill_action, NULL) < 0) {
993     sprintf(str, GetString(STR_SIGILL_INSTALL_ERR), strerror(errno));
994     ErrorAlert(str);
995     goto quit;
996     }
997 gbeauche 1.6 #endif
998 cebix 1.1
999 gbeauche 1.26 #if !EMULATED_PPC
1000 cebix 1.1 // Install interrupt signal handler
1001     sigemptyset(&sigusr2_action.sa_mask);
1002 gbeauche 1.26 sigusr2_action.sa_sigaction = sigusr2_handler;
1003     sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART | SA_SIGINFO;
1004     #ifdef HAVE_SIGNAL_SA_RESTORER
1005     sigusr2_action.sa_restorer = NULL;
1006 gbeauche 1.8 #endif
1007 cebix 1.1 if (sigaction(SIGUSR2, &sigusr2_action, NULL) < 0) {
1008     sprintf(str, GetString(STR_SIGUSR2_INSTALL_ERR), strerror(errno));
1009     ErrorAlert(str);
1010     goto quit;
1011     }
1012 gbeauche 1.26 #endif
1013 cebix 1.1
1014     // Get my thread ID and execute MacOS thread function
1015     emul_thread = pthread_self();
1016     D(bug("MacOS thread is %ld\n", emul_thread));
1017     emul_func(NULL);
1018    
1019     quit:
1020     Quit();
1021     return 0;
1022     }
1023    
1024    
1025     /*
1026     * Cleanup and quit
1027     */
1028    
1029     static void Quit(void)
1030     {
1031 gbeauche 1.13 #if EMULATED_PPC
1032     // Exit PowerPC emulation
1033     exit_emul_ppc();
1034     #endif
1035    
1036 cebix 1.1 // Stop 60Hz thread
1037     if (tick_thread_active) {
1038 gbeauche 1.40 tick_thread_cancel = true;
1039 cebix 1.1 pthread_cancel(tick_thread);
1040     pthread_join(tick_thread, NULL);
1041     }
1042    
1043     // Stop NVRAM watchdog thread
1044     if (nvram_thread_active) {
1045 gbeauche 1.40 nvram_thread_cancel = true;
1046 cebix 1.1 pthread_cancel(nvram_thread);
1047     pthread_join(nvram_thread, NULL);
1048     }
1049    
1050     #if !EMULATED_PPC
1051 gbeauche 1.23 // Uninstall SIGSEGV and SIGBUS handlers
1052 cebix 1.1 sigemptyset(&sigsegv_action.sa_mask);
1053     sigsegv_action.sa_handler = SIG_DFL;
1054     sigsegv_action.sa_flags = 0;
1055     sigaction(SIGSEGV, &sigsegv_action, NULL);
1056 gbeauche 1.23 sigaction(SIGBUS, &sigsegv_action, NULL);
1057 cebix 1.1
1058     // Uninstall SIGILL handler
1059     sigemptyset(&sigill_action.sa_mask);
1060     sigill_action.sa_handler = SIG_DFL;
1061     sigill_action.sa_flags = 0;
1062     sigaction(SIGILL, &sigill_action, NULL);
1063 gbeauche 1.33
1064     // Delete stacks for signal handlers
1065     for (int i = 0; i < SIG_STACK_COUNT; i++) {
1066     void *sig_stack = sig_stacks[i].ss_sp;
1067     if (sig_stack)
1068     free(sig_stack);
1069     }
1070 cebix 1.1 #endif
1071    
1072     // Save NVRAM
1073     XPRAMExit();
1074    
1075     // Exit clipboard
1076     ClipExit();
1077    
1078     // Exit Time Manager
1079     TimerExit();
1080    
1081     // Exit serial
1082     SerialExit();
1083    
1084     // Exit network
1085     EtherExit();
1086    
1087     // Exit audio
1088     AudioExit();
1089 gbeauche 1.22
1090     // Exit ADB
1091     ADBExit();
1092 cebix 1.1
1093     // Exit video
1094     VideoExit();
1095    
1096     // Exit external file system
1097     ExtFSExit();
1098    
1099     // Exit drivers
1100     SCSIExit();
1101     CDROMExit();
1102     DiskExit();
1103     SonyExit();
1104    
1105 gbeauche 1.24 // Delete thunks
1106     ThunksExit();
1107    
1108 gbeauche 1.15 // Delete SheepShaver globals
1109     SheepMem::Exit();
1110    
1111 cebix 1.1 // Delete RAM area
1112     if (ram_area_mapped)
1113 gbeauche 1.8 vm_release((char *)RAM_BASE, RAMSize);
1114 cebix 1.1
1115     // Delete ROM area
1116     if (rom_area_mapped)
1117 gbeauche 1.4 vm_release((char *)ROM_BASE, ROM_AREA_SIZE);
1118 cebix 1.1
1119 gbeauche 1.36 // Delete DR cache areas
1120     if (dr_emulator_area_mapped)
1121     vm_release((void *)DR_EMULATOR_BASE, DR_EMULATOR_SIZE);
1122     if (dr_cache_area_mapped)
1123     vm_release((void *)DR_CACHE_BASE, DR_CACHE_SIZE);
1124    
1125 cebix 1.1 // Delete Kernel Data area
1126     if (kernel_area >= 0) {
1127     shmdt((void *)KERNEL_DATA_BASE);
1128     shmdt((void *)KERNEL_DATA2_BASE);
1129     shmctl(kernel_area, IPC_RMID, NULL);
1130     }
1131    
1132     // Delete Low Memory area
1133     if (lm_area_mapped)
1134     munmap((char *)0x0000, 0x3000);
1135    
1136     // Close /dev/zero
1137     if (zero_fd > 0)
1138     close(zero_fd);
1139    
1140     // Exit system routines
1141     SysExit();
1142    
1143     // Exit preferences
1144     PrefsExit();
1145    
1146     #ifdef ENABLE_MON
1147     // Exit mon
1148     mon_exit();
1149     #endif
1150    
1151     // Close X11 server connection
1152 gbeauche 1.42 #ifndef USE_SDL_VIDEO
1153 cebix 1.1 if (x_display)
1154     XCloseDisplay(x_display);
1155 gbeauche 1.42 #endif
1156 cebix 1.1
1157     exit(0);
1158     }
1159    
1160    
1161     /*
1162     * Jump into Mac ROM, start 680x0 emulator
1163     */
1164    
1165     #if EMULATED_PPC
1166     void jump_to_rom(uint32 entry)
1167     {
1168     init_emul_ppc();
1169     emul_ppc(entry);
1170     }
1171     #endif
1172    
1173    
1174     /*
1175     * Emulator thread function
1176     */
1177    
1178     static void *emul_func(void *arg)
1179     {
1180     // We're now ready to receive signals
1181     ready_for_signals = true;
1182    
1183     // Decrease priority, so more time-critical things like audio will work better
1184     nice(1);
1185    
1186     // Jump to ROM boot routine
1187     D(bug("Jumping to ROM\n"));
1188     #if EMULATED_PPC
1189     jump_to_rom(ROM_BASE + 0x310000);
1190     #else
1191     jump_to_rom(ROM_BASE + 0x310000, (uint32)emulator_data);
1192     #endif
1193     D(bug("Returned from ROM\n"));
1194    
1195     // We're no longer ready to receive signals
1196     ready_for_signals = false;
1197     return NULL;
1198     }
1199    
1200    
1201     #if !EMULATED_PPC
1202     /*
1203     * Execute 68k subroutine (must be ended with RTS)
1204     * This must only be called by the emul_thread when in EMUL_OP mode
1205     * r->a[7] is unused, the routine runs on the caller's stack
1206     */
1207    
1208     void Execute68k(uint32 pc, M68kRegisters *r)
1209     {
1210     #if SAFE_EXEC_68K
1211     if (ReadMacInt32(XLM_RUN_MODE) != MODE_EMUL_OP)
1212     printf("FATAL: Execute68k() not called from EMUL_OP mode\n");
1213     if (!pthread_equal(pthread_self(), emul_thread))
1214     printf("FATAL: Execute68k() not called from emul_thread\n");
1215     #endif
1216     execute_68k(pc, r);
1217     }
1218    
1219    
1220     /*
1221     * Execute 68k A-Trap from EMUL_OP routine
1222     * r->a[7] is unused, the routine runs on the caller's stack
1223     */
1224    
1225     void Execute68kTrap(uint16 trap, M68kRegisters *r)
1226     {
1227     uint16 proc[2] = {trap, M68K_RTS};
1228     Execute68k((uint32)proc, r);
1229     }
1230 gbeauche 1.7 #endif
1231 cebix 1.1
1232    
1233     /*
1234     * Quit emulator (cause return from jump_to_rom)
1235     */
1236    
1237     void QuitEmulator(void)
1238     {
1239     #if EMULATED_PPC
1240     Quit();
1241     #else
1242     quit_emulator();
1243     #endif
1244     }
1245    
1246    
1247     /*
1248     * Pause/resume emulator
1249     */
1250    
1251     void PauseEmulator(void)
1252     {
1253     pthread_kill(emul_thread, SIGSTOP);
1254     }
1255    
1256     void ResumeEmulator(void)
1257     {
1258     pthread_kill(emul_thread, SIGCONT);
1259     }
1260    
1261    
1262     /*
1263     * Dump 68k registers
1264     */
1265    
1266     void Dump68kRegs(M68kRegisters *r)
1267     {
1268     // Display 68k registers
1269     for (int i=0; i<8; i++) {
1270     printf("d%d: %08x", i, r->d[i]);
1271     if (i == 3 || i == 7)
1272     printf("\n");
1273     else
1274     printf(", ");
1275     }
1276     for (int i=0; i<8; i++) {
1277     printf("a%d: %08x", i, r->a[i]);
1278     if (i == 3 || i == 7)
1279     printf("\n");
1280     else
1281     printf(", ");
1282     }
1283     }
1284    
1285    
1286     /*
1287     * Make code executable
1288     */
1289    
1290     void MakeExecutable(int dummy, void *start, uint32 length)
1291     {
1292 gbeauche 1.9 if (((uintptr)start >= ROM_BASE) && ((uintptr)start < (ROM_BASE + ROM_SIZE)))
1293 cebix 1.1 return;
1294 gbeauche 1.9 #if EMULATED_PPC
1295     FlushCodeCache((uintptr)start, (uintptr)start + length);
1296     #else
1297     flush_icache_range(start, (void *)((uintptr)start + length));
1298 cebix 1.1 #endif
1299     }
1300    
1301    
1302     /*
1303     * Patch things after system startup (gets called by disk driver accRun routine)
1304     */
1305    
1306     void PatchAfterStartup(void)
1307     {
1308 gbeauche 1.6 ExecuteNative(NATIVE_VIDEO_INSTALL_ACCEL);
1309 cebix 1.1 InstallExtFS();
1310     }
1311    
1312    
1313     /*
1314     * NVRAM watchdog thread (saves NVRAM every minute)
1315     */
1316    
1317 gbeauche 1.40 static void nvram_watchdog(void)
1318     {
1319     if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) {
1320     memcpy(last_xpram, XPRAM, XPRAM_SIZE);
1321     SaveXPRAM();
1322     }
1323     }
1324    
1325 cebix 1.1 static void *nvram_func(void *arg)
1326     {
1327 gbeauche 1.40 while (!nvram_thread_cancel) {
1328     for (int i=0; i<60 && !nvram_thread_cancel; i++)
1329     Delay_usec(999999); // Only wait 1 second so we quit promptly when nvram_thread_cancel becomes true
1330     nvram_watchdog();
1331 cebix 1.1 }
1332     return NULL;
1333     }
1334    
1335    
1336     /*
1337     * 60Hz thread (really 60.15Hz)
1338     */
1339    
1340     static void *tick_func(void *arg)
1341     {
1342     int tick_counter = 0;
1343 gbeauche 1.40 uint64 start = GetTicks_usec();
1344     int64 ticks = 0;
1345     uint64 next = GetTicks_usec();
1346 cebix 1.1
1347 gbeauche 1.40 while (!tick_thread_cancel) {
1348 cebix 1.1
1349     // Wait
1350 gbeauche 1.40 next += 16625;
1351     int64 delay = next - GetTicks_usec();
1352     if (delay > 0)
1353     Delay_usec(delay);
1354     else if (delay < -16625)
1355     next = GetTicks_usec();
1356     ticks++;
1357 cebix 1.1
1358     #if !EMULATED_PPC
1359     // Did we crash?
1360     if (emul_thread_fatal) {
1361    
1362     // Yes, dump registers
1363 gbeauche 1.26 sigregs *r = &sigsegv_regs;
1364 cebix 1.1 char str[256];
1365 gbeauche 1.23 if (crash_reason == NULL)
1366     crash_reason = "SIGSEGV";
1367     sprintf(str, "%s\n"
1368 cebix 1.1 " pc %08lx lr %08lx ctr %08lx msr %08lx\n"
1369     " xer %08lx cr %08lx \n"
1370     " r0 %08lx r1 %08lx r2 %08lx r3 %08lx\n"
1371     " r4 %08lx r5 %08lx r6 %08lx r7 %08lx\n"
1372     " r8 %08lx r9 %08lx r10 %08lx r11 %08lx\n"
1373     " r12 %08lx r13 %08lx r14 %08lx r15 %08lx\n"
1374     " r16 %08lx r17 %08lx r18 %08lx r19 %08lx\n"
1375     " r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n"
1376     " r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n"
1377     " r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n",
1378 gbeauche 1.23 crash_reason,
1379 cebix 1.1 r->nip, r->link, r->ctr, r->msr,
1380     r->xer, r->ccr,
1381     r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3],
1382     r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7],
1383     r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11],
1384     r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15],
1385     r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19],
1386     r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23],
1387     r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27],
1388     r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]);
1389     printf(str);
1390     VideoQuitFullScreen();
1391    
1392     #ifdef ENABLE_MON
1393     // Start up mon in real-mode
1394     printf("Welcome to the sheep factory.\n");
1395     char *arg[4] = {"mon", "-m", "-r", NULL};
1396     mon(3, arg);
1397     #endif
1398     return NULL;
1399     }
1400     #endif
1401    
1402     // Pseudo Mac 1Hz interrupt, update local time
1403     if (++tick_counter > 60) {
1404     tick_counter = 0;
1405     WriteMacInt32(0x20c, TimerDateTime());
1406     }
1407    
1408     // Trigger 60Hz interrupt
1409     if (ReadMacInt32(XLM_IRQ_NEST) == 0) {
1410     SetInterruptFlag(INTFLAG_VIA);
1411     TriggerInterrupt();
1412     }
1413     }
1414 gbeauche 1.40
1415     uint64 end = GetTicks_usec();
1416     D(bug("%Ld ticks in %Ld usec = %f ticks/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
1417 cebix 1.1 return NULL;
1418     }
1419    
1420    
1421     /*
1422 cebix 1.2 * Pthread configuration
1423     */
1424    
1425     void Set_pthread_attr(pthread_attr_t *attr, int priority)
1426     {
1427 gbeauche 1.14 #ifdef HAVE_PTHREADS
1428     pthread_attr_init(attr);
1429     #if defined(_POSIX_THREAD_PRIORITY_SCHEDULING)
1430     // Some of these only work for superuser
1431     if (geteuid() == 0) {
1432     pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED);
1433     pthread_attr_setschedpolicy(attr, SCHED_FIFO);
1434     struct sched_param fifo_param;
1435     fifo_param.sched_priority = ((sched_get_priority_min(SCHED_FIFO) +
1436     sched_get_priority_max(SCHED_FIFO)) / 2 +
1437     priority);
1438     pthread_attr_setschedparam(attr, &fifo_param);
1439     }
1440     if (pthread_attr_setscope(attr, PTHREAD_SCOPE_SYSTEM) != 0) {
1441     #ifdef PTHREAD_SCOPE_BOUND_NP
1442     // If system scope is not available (eg. we're not running
1443     // with CAP_SCHED_MGT capability on an SGI box), try bound
1444     // scope. It exposes pthread scheduling to the kernel,
1445     // without setting realtime priority.
1446     pthread_attr_setscope(attr, PTHREAD_SCOPE_BOUND_NP);
1447     #endif
1448     }
1449     #endif
1450     #endif
1451 cebix 1.2 }
1452    
1453    
1454     /*
1455 cebix 1.1 * Mutexes
1456     */
1457    
1458 gbeauche 1.7 #ifdef HAVE_PTHREADS
1459    
1460     struct B2_mutex {
1461     B2_mutex() {
1462     pthread_mutexattr_t attr;
1463     pthread_mutexattr_init(&attr);
1464     // Initialize the mutex for priority inheritance --
1465     // required for accurate timing.
1466     #ifdef HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL
1467     pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
1468     #endif
1469     #if defined(HAVE_PTHREAD_MUTEXATTR_SETTYPE) && defined(PTHREAD_MUTEX_NORMAL)
1470     pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
1471     #endif
1472     #ifdef HAVE_PTHREAD_MUTEXATTR_SETPSHARED
1473     pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE);
1474     #endif
1475     pthread_mutex_init(&m, &attr);
1476     pthread_mutexattr_destroy(&attr);
1477     }
1478     ~B2_mutex() {
1479     pthread_mutex_trylock(&m); // Make sure it's locked before
1480     pthread_mutex_unlock(&m); // unlocking it.
1481     pthread_mutex_destroy(&m);
1482     }
1483     pthread_mutex_t m;
1484     };
1485    
1486     B2_mutex *B2_create_mutex(void)
1487     {
1488     return new B2_mutex;
1489     }
1490    
1491     void B2_lock_mutex(B2_mutex *mutex)
1492     {
1493     pthread_mutex_lock(&mutex->m);
1494     }
1495    
1496     void B2_unlock_mutex(B2_mutex *mutex)
1497     {
1498     pthread_mutex_unlock(&mutex->m);
1499     }
1500    
1501     void B2_delete_mutex(B2_mutex *mutex)
1502     {
1503     delete mutex;
1504     }
1505    
1506     #else
1507    
1508 cebix 1.1 struct B2_mutex {
1509     int dummy;
1510     };
1511    
1512     B2_mutex *B2_create_mutex(void)
1513     {
1514     return new B2_mutex;
1515     }
1516    
1517     void B2_lock_mutex(B2_mutex *mutex)
1518     {
1519     }
1520    
1521     void B2_unlock_mutex(B2_mutex *mutex)
1522     {
1523     }
1524    
1525     void B2_delete_mutex(B2_mutex *mutex)
1526     {
1527     delete mutex;
1528     }
1529    
1530 gbeauche 1.7 #endif
1531    
1532 cebix 1.1
1533     /*
1534     * Trigger signal USR2 from another thread
1535     */
1536    
1537 gbeauche 1.35 #if !EMULATED_PPC
1538 cebix 1.1 void TriggerInterrupt(void)
1539     {
1540     if (ready_for_signals)
1541     pthread_kill(emul_thread, SIGUSR2);
1542     }
1543 gbeauche 1.7 #endif
1544 cebix 1.1
1545    
1546     /*
1547     * Interrupt flags (must be handled atomically!)
1548     */
1549    
1550     volatile uint32 InterruptFlags = 0;
1551    
1552     void SetInterruptFlag(uint32 flag)
1553     {
1554     atomic_or((int *)&InterruptFlags, flag);
1555     }
1556    
1557     void ClearInterruptFlag(uint32 flag)
1558     {
1559     atomic_and((int *)&InterruptFlags, ~flag);
1560     }
1561    
1562    
1563     /*
1564     * Disable interrupts
1565     */
1566    
1567     void DisableInterrupt(void)
1568     {
1569 gbeauche 1.41 #if EMULATED_PPC
1570     WriteMacInt32(XLM_IRQ_NEST, int32(ReadMacInt32(XLM_IRQ_NEST)) + 1);
1571     #else
1572 gbeauche 1.7 atomic_add((int *)XLM_IRQ_NEST, 1);
1573 gbeauche 1.41 #endif
1574 cebix 1.1 }
1575    
1576    
1577     /*
1578     * Enable interrupts
1579     */
1580    
1581     void EnableInterrupt(void)
1582     {
1583 gbeauche 1.41 #if EMULATED_PPC
1584     WriteMacInt32(XLM_IRQ_NEST, int32(ReadMacInt32(XLM_IRQ_NEST)) - 1);
1585     #else
1586 gbeauche 1.7 atomic_add((int *)XLM_IRQ_NEST, -1);
1587 gbeauche 1.41 #endif
1588 cebix 1.1 }
1589    
1590    
1591     /*
1592     * USR2 handler
1593     */
1594    
1595 gbeauche 1.35 #if !EMULATED_PPC
1596 gbeauche 1.26 static void sigusr2_handler(int sig, siginfo_t *sip, void *scp)
1597 cebix 1.1 {
1598 gbeauche 1.26 machine_regs *r = MACHINE_REGISTERS(scp);
1599 cebix 1.1
1600 gbeauche 1.42 #ifdef USE_SDL_VIDEO
1601     // We must fill in the events queue in the same thread that did call SDL_SetVideoMode()
1602     SDL_PumpEvents();
1603     #endif
1604    
1605 cebix 1.1 // Do nothing if interrupts are disabled
1606     if (*(int32 *)XLM_IRQ_NEST > 0)
1607     return;
1608    
1609     // Disable MacOS stack sniffer
1610     WriteMacInt32(0x110, 0);
1611    
1612     // Interrupt action depends on current run mode
1613     switch (ReadMacInt32(XLM_RUN_MODE)) {
1614     case MODE_68K:
1615     // 68k emulator active, trigger 68k interrupt level 1
1616     WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1);
1617 gbeauche 1.26 r->cr() |= ntohl(kernel_data->v[0x674 >> 2]);
1618 cebix 1.1 break;
1619    
1620     #if INTERRUPTS_IN_NATIVE_MODE
1621     case MODE_NATIVE:
1622     // 68k emulator inactive, in nanokernel?
1623 gbeauche 1.26 if (r->gpr(1) != KernelDataAddr) {
1624 gbeauche 1.33
1625     // Set extra stack for nested interrupts
1626     sig_stack_acquire();
1627    
1628 cebix 1.1 // Prepare for 68k interrupt level 1
1629     WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1);
1630     WriteMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc, ReadMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc) | ntohl(kernel_data->v[0x674 >> 2]));
1631    
1632     // Execute nanokernel interrupt routine (this will activate the 68k emulator)
1633 gbeauche 1.33 DisableInterrupt();
1634 cebix 1.1 if (ROMType == ROMTYPE_NEWWORLD)
1635     ppc_interrupt(ROM_BASE + 0x312b1c, KernelDataAddr);
1636     else
1637     ppc_interrupt(ROM_BASE + 0x312a3c, KernelDataAddr);
1638 gbeauche 1.33
1639     // Reset normal signal stack
1640     sig_stack_release();
1641 cebix 1.1 }
1642     break;
1643     #endif
1644    
1645     #if INTERRUPTS_IN_EMUL_OP_MODE
1646     case MODE_EMUL_OP:
1647     // 68k emulator active, within EMUL_OP routine, execute 68k interrupt routine directly when interrupt level is 0
1648     if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) {
1649    
1650     // Set extra stack for SIGSEGV handler
1651 gbeauche 1.33 sig_stack_acquire();
1652 cebix 1.1 #if 1
1653     // Execute full 68k interrupt routine
1654     M68kRegisters r;
1655     uint32 old_r25 = ReadMacInt32(XLM_68K_R25); // Save interrupt level
1656     WriteMacInt32(XLM_68K_R25, 0x21); // Execute with interrupt level 1
1657     static const uint16 proc[] = {
1658     0x3f3c, 0x0000, // move.w #$0000,-(sp) (fake format word)
1659     0x487a, 0x000a, // pea @1(pc) (return address)
1660     0x40e7, // move sr,-(sp) (saved SR)
1661     0x2078, 0x0064, // move.l $64,a0
1662     0x4ed0, // jmp (a0)
1663     M68K_RTS // @1
1664     };
1665     Execute68k((uint32)proc, &r);
1666     WriteMacInt32(XLM_68K_R25, old_r25); // Restore interrupt level
1667     #else
1668     // Only update cursor
1669     if (HasMacStarted()) {
1670     if (InterruptFlags & INTFLAG_VIA) {
1671     ClearInterruptFlag(INTFLAG_VIA);
1672     ADBInterrupt();
1673 gbeauche 1.17 ExecuteNative(NATIVE_VIDEO_VBL);
1674 cebix 1.1 }
1675     }
1676     #endif
1677     // Reset normal signal stack
1678 gbeauche 1.33 sig_stack_release();
1679 cebix 1.1 }
1680     break;
1681     #endif
1682     }
1683     }
1684 gbeauche 1.8 #endif
1685 cebix 1.1
1686    
1687     /*
1688     * SIGSEGV handler
1689     */
1690    
1691 gbeauche 1.8 #if !EMULATED_PPC
1692 gbeauche 1.26 static void sigsegv_handler(int sig, siginfo_t *sip, void *scp)
1693 cebix 1.1 {
1694 gbeauche 1.26 machine_regs *r = MACHINE_REGISTERS(scp);
1695 gbeauche 1.5
1696     // Get effective address
1697 gbeauche 1.26 uint32 addr = r->dar();
1698 gbeauche 1.5
1699     #if ENABLE_VOSF
1700     // Handle screen fault.
1701     extern bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction);
1702 gbeauche 1.26 if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->pc()))
1703 gbeauche 1.5 return;
1704     #endif
1705    
1706 cebix 1.1 num_segv++;
1707    
1708 gbeauche 1.37 // Fault in Mac ROM or RAM or DR Cache?
1709     bool mac_fault = (r->pc() >= ROM_BASE) && (r->pc() < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize)) || (r->pc() >= DR_CACHE_BASE && r->pc() < (DR_CACHE_BASE + DR_CACHE_SIZE));
1710 cebix 1.1 if (mac_fault) {
1711    
1712     // "VM settings" during MacOS 8 installation
1713 gbeauche 1.26 if (r->pc() == ROM_BASE + 0x488160 && r->gpr(20) == 0xf8000000) {
1714     r->pc() += 4;
1715     r->gpr(8) = 0;
1716 cebix 1.1 return;
1717    
1718     // MacOS 8.5 installation
1719 gbeauche 1.26 } else if (r->pc() == ROM_BASE + 0x488140 && r->gpr(16) == 0xf8000000) {
1720     r->pc() += 4;
1721     r->gpr(8) = 0;
1722 cebix 1.1 return;
1723    
1724     // MacOS 8 serial drivers on startup
1725 gbeauche 1.26 } else if (r->pc() == ROM_BASE + 0x48e080 && (r->gpr(8) == 0xf3012002 || r->gpr(8) == 0xf3012000)) {
1726     r->pc() += 4;
1727     r->gpr(8) = 0;
1728 cebix 1.1 return;
1729    
1730     // MacOS 8.1 serial drivers on startup
1731 gbeauche 1.26 } else if (r->pc() == ROM_BASE + 0x48c5e0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) {
1732     r->pc() += 4;
1733 cebix 1.1 return;
1734 gbeauche 1.26 } else if (r->pc() == ROM_BASE + 0x4a10a0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) {
1735     r->pc() += 4;
1736 cebix 1.1 return;
1737 gbeauche 1.37
1738     // MacOS 8.6 serial drivers on startup (with DR Cache and OldWorld ROM)
1739     } else if ((r->pc() - DR_CACHE_BASE) < DR_CACHE_SIZE && (r->gpr(16) == 0xf3012002 || r->gpr(16) == 0xf3012000)) {
1740     r->pc() += 4;
1741     return;
1742     } else if ((r->pc() - DR_CACHE_BASE) < DR_CACHE_SIZE && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) {
1743     r->pc() += 4;
1744     return;
1745 cebix 1.1 }
1746    
1747 gbeauche 1.5 // Get opcode and divide into fields
1748 gbeauche 1.26 uint32 opcode = *((uint32 *)r->pc());
1749 gbeauche 1.5 uint32 primop = opcode >> 26;
1750     uint32 exop = (opcode >> 1) & 0x3ff;
1751     uint32 ra = (opcode >> 16) & 0x1f;
1752     uint32 rb = (opcode >> 11) & 0x1f;
1753     uint32 rd = (opcode >> 21) & 0x1f;
1754     int32 imm = (int16)(opcode & 0xffff);
1755    
1756 cebix 1.1 // Analyze opcode
1757     enum {
1758     TYPE_UNKNOWN,
1759     TYPE_LOAD,
1760     TYPE_STORE
1761     } transfer_type = TYPE_UNKNOWN;
1762     enum {
1763     SIZE_UNKNOWN,
1764     SIZE_BYTE,
1765     SIZE_HALFWORD,
1766     SIZE_WORD
1767     } transfer_size = SIZE_UNKNOWN;
1768     enum {
1769     MODE_UNKNOWN,
1770     MODE_NORM,
1771     MODE_U,
1772     MODE_X,
1773     MODE_UX
1774     } addr_mode = MODE_UNKNOWN;
1775     switch (primop) {
1776     case 31:
1777     switch (exop) {
1778     case 23: // lwzx
1779     transfer_type = TYPE_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_X; break;
1780     case 55: // lwzux
1781     transfer_type = TYPE_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_UX; break;
1782     case 87: // lbzx
1783     transfer_type = TYPE_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_X; break;
1784     case 119: // lbzux
1785     transfer_type = TYPE_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_UX; break;
1786     case 151: // stwx
1787     transfer_type = TYPE_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_X; break;
1788     case 183: // stwux
1789     transfer_type = TYPE_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_UX; break;
1790     case 215: // stbx
1791     transfer_type = TYPE_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_X; break;
1792     case 247: // stbux
1793     transfer_type = TYPE_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_UX; break;
1794     case 279: // lhzx
1795     transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_X; break;
1796     case 311: // lhzux
1797     transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_UX; break;
1798     case 343: // lhax
1799     transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_X; break;
1800     case 375: // lhaux
1801     transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_UX; break;
1802     case 407: // sthx
1803     transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_X; break;
1804     case 439: // sthux
1805     transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_UX; break;
1806     }
1807     break;
1808    
1809     case 32: // lwz
1810     transfer_type = TYPE_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_NORM; break;
1811     case 33: // lwzu
1812     transfer_type = TYPE_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_U; break;
1813     case 34: // lbz
1814     transfer_type = TYPE_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_NORM; break;
1815     case 35: // lbzu
1816     transfer_type = TYPE_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_U; break;
1817     case 36: // stw
1818     transfer_type = TYPE_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_NORM; break;
1819     case 37: // stwu
1820     transfer_type = TYPE_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_U; break;
1821     case 38: // stb
1822     transfer_type = TYPE_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_NORM; break;
1823     case 39: // stbu
1824     transfer_type = TYPE_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_U; break;
1825     case 40: // lhz
1826     transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_NORM; break;
1827     case 41: // lhzu
1828     transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_U; break;
1829     case 42: // lha
1830     transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_NORM; break;
1831     case 43: // lhau
1832     transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_U; break;
1833     case 44: // sth
1834     transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_NORM; break;
1835     case 45: // sthu
1836     transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_U; break;
1837 gbeauche 1.23 #if EMULATE_UNALIGNED_LOADSTORE_MULTIPLE
1838     case 46: // lmw
1839 gbeauche 1.27 if ((addr % 4) != 0) {
1840     uint32 ea = addr;
1841 gbeauche 1.26 D(bug("WARNING: unaligned lmw to EA=%08x from IP=%08x\n", ea, r->pc()));
1842 gbeauche 1.23 for (int i = rd; i <= 31; i++) {
1843 gbeauche 1.26 r->gpr(i) = ReadMacInt32(ea);
1844 gbeauche 1.23 ea += 4;
1845     }
1846 gbeauche 1.26 r->pc() += 4;
1847 gbeauche 1.23 goto rti;
1848     }
1849     break;
1850     case 47: // stmw
1851 gbeauche 1.27 if ((addr % 4) != 0) {
1852     uint32 ea = addr;
1853 gbeauche 1.26 D(bug("WARNING: unaligned stmw to EA=%08x from IP=%08x\n", ea, r->pc()));
1854 gbeauche 1.23 for (int i = rd; i <= 31; i++) {
1855 gbeauche 1.26 WriteMacInt32(ea, r->gpr(i));
1856 gbeauche 1.23 ea += 4;
1857     }
1858 gbeauche 1.26 r->pc() += 4;
1859 gbeauche 1.23 goto rti;
1860     }
1861     break;
1862     #endif
1863 cebix 1.1 }
1864    
1865 gbeauche 1.31 // Ignore ROM writes (including to the zero page, which is read-only)
1866     if (transfer_type == TYPE_STORE &&
1867     ((addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) ||
1868     (addr >= SheepMem::ZeroPage() && addr < SheepMem::ZeroPage() + SheepMem::PageSize()))) {
1869 gbeauche 1.26 // 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->pc()));
1870 cebix 1.1 if (addr_mode == MODE_U || addr_mode == MODE_UX)
1871 gbeauche 1.26 r->gpr(ra) = addr;
1872     r->pc() += 4;
1873 cebix 1.1 goto rti;
1874     }
1875    
1876     // Ignore illegal memory accesses?
1877     if (PrefsFindBool("ignoresegv")) {
1878     if (addr_mode == MODE_U || addr_mode == MODE_UX)
1879 gbeauche 1.26 r->gpr(ra) = addr;
1880 cebix 1.1 if (transfer_type == TYPE_LOAD)
1881 gbeauche 1.26 r->gpr(rd) = 0;
1882     r->pc() += 4;
1883 cebix 1.1 goto rti;
1884     }
1885    
1886     // In GUI mode, show error alert
1887     if (!PrefsFindBool("nogui")) {
1888     char str[256];
1889     if (transfer_type == TYPE_LOAD || transfer_type == TYPE_STORE)
1890 gbeauche 1.26 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->pc(), r->gpr(24), r->gpr(1));
1891 cebix 1.1 else
1892 gbeauche 1.26 sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode);
1893 cebix 1.1 ErrorAlert(str);
1894     QuitEmulator();
1895     return;
1896     }
1897     }
1898    
1899     // For all other errors, jump into debugger (sort of...)
1900 gbeauche 1.23 crash_reason = (sig == SIGBUS) ? "SIGBUS" : "SIGSEGV";
1901 cebix 1.1 if (!ready_for_signals) {
1902 gbeauche 1.23 printf("%s\n");
1903 gbeauche 1.26 printf(" sigcontext %p, machine_regs %p\n", scp, r);
1904 cebix 1.1 printf(
1905     " pc %08lx lr %08lx ctr %08lx msr %08lx\n"
1906     " xer %08lx cr %08lx \n"
1907     " r0 %08lx r1 %08lx r2 %08lx r3 %08lx\n"
1908     " r4 %08lx r5 %08lx r6 %08lx r7 %08lx\n"
1909     " r8 %08lx r9 %08lx r10 %08lx r11 %08lx\n"
1910     " r12 %08lx r13 %08lx r14 %08lx r15 %08lx\n"
1911     " r16 %08lx r17 %08lx r18 %08lx r19 %08lx\n"
1912     " r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n"
1913     " r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n"
1914     " r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n",
1915 gbeauche 1.23 crash_reason,
1916 gbeauche 1.26 r->pc(), r->lr(), r->ctr(), r->msr(),
1917     r->xer(), r->cr(),
1918     r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3),
1919     r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7),
1920     r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11),
1921     r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15),
1922     r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19),
1923     r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23),
1924     r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27),
1925     r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31));
1926 cebix 1.1 exit(1);
1927     QuitEmulator();
1928     return;
1929     } else {
1930     // We crashed. Save registers, tell tick thread and loop forever
1931 gbeauche 1.26 build_sigregs(&sigsegv_regs, r);
1932 cebix 1.1 emul_thread_fatal = true;
1933     for (;;) ;
1934     }
1935     rti:;
1936     }
1937    
1938    
1939     /*
1940     * SIGILL handler
1941     */
1942    
1943 gbeauche 1.26 static void sigill_handler(int sig, siginfo_t *sip, void *scp)
1944 cebix 1.1 {
1945 gbeauche 1.26 machine_regs *r = MACHINE_REGISTERS(scp);
1946 cebix 1.1 char str[256];
1947    
1948     // Fault in Mac ROM or RAM?
1949 gbeauche 1.26 bool mac_fault = (r->pc() >= ROM_BASE) && (r->pc() < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize));
1950 cebix 1.1 if (mac_fault) {
1951    
1952     // Get opcode and divide into fields
1953 gbeauche 1.26 uint32 opcode = *((uint32 *)r->pc());
1954 cebix 1.1 uint32 primop = opcode >> 26;
1955     uint32 exop = (opcode >> 1) & 0x3ff;
1956     uint32 ra = (opcode >> 16) & 0x1f;
1957     uint32 rb = (opcode >> 11) & 0x1f;
1958     uint32 rd = (opcode >> 21) & 0x1f;
1959     int32 imm = (int16)(opcode & 0xffff);
1960    
1961     switch (primop) {
1962     case 9: // POWER instructions
1963     case 22:
1964 gbeauche 1.26 power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->pc(), r->gpr(1), opcode);
1965 cebix 1.1 ErrorAlert(str);
1966     QuitEmulator();
1967     return;
1968    
1969     case 31:
1970     switch (exop) {
1971     case 83: // mfmsr
1972 gbeauche 1.26 r->gpr(rd) = 0xf072;
1973     r->pc() += 4;
1974 cebix 1.1 goto rti;
1975    
1976     case 210: // mtsr
1977     case 242: // mtsrin
1978     case 306: // tlbie
1979 gbeauche 1.26 r->pc() += 4;
1980 cebix 1.1 goto rti;
1981    
1982     case 339: { // mfspr
1983     int spr = ra | (rb << 5);
1984     switch (spr) {
1985     case 0: // MQ
1986     case 22: // DEC
1987     case 952: // MMCR0
1988     case 953: // PMC1
1989     case 954: // PMC2
1990     case 955: // SIA
1991     case 956: // MMCR1
1992     case 957: // PMC3
1993     case 958: // PMC4
1994     case 959: // SDA
1995 gbeauche 1.26 r->pc() += 4;
1996 cebix 1.1 goto rti;
1997     case 25: // SDR1
1998 gbeauche 1.26 r->gpr(rd) = 0xdead001f;
1999     r->pc() += 4;
2000 cebix 1.1 goto rti;
2001     case 287: // PVR
2002 gbeauche 1.26 r->gpr(rd) = PVR;
2003     r->pc() += 4;
2004 cebix 1.1 goto rti;
2005     }
2006     break;
2007     }
2008    
2009     case 467: { // mtspr
2010     int spr = ra | (rb << 5);
2011     switch (spr) {
2012     case 0: // MQ
2013     case 22: // DEC
2014     case 275: // SPRG3
2015     case 528: // IBAT0U
2016     case 529: // IBAT0L
2017     case 530: // IBAT1U
2018     case 531: // IBAT1L
2019     case 532: // IBAT2U
2020     case 533: // IBAT2L
2021     case 534: // IBAT3U
2022     case 535: // IBAT3L
2023     case 536: // DBAT0U
2024     case 537: // DBAT0L
2025     case 538: // DBAT1U
2026     case 539: // DBAT1L
2027     case 540: // DBAT2U
2028     case 541: // DBAT2L
2029     case 542: // DBAT3U
2030     case 543: // DBAT3L
2031     case 952: // MMCR0
2032     case 953: // PMC1
2033     case 954: // PMC2
2034     case 955: // SIA
2035     case 956: // MMCR1
2036     case 957: // PMC3
2037     case 958: // PMC4
2038     case 959: // SDA
2039 gbeauche 1.26 r->pc() += 4;
2040 cebix 1.1 goto rti;
2041     }
2042     break;
2043     }
2044    
2045     case 29: case 107: case 152: case 153: // POWER instructions
2046     case 184: case 216: case 217: case 248:
2047     case 264: case 277: case 331: case 360:
2048     case 363: case 488: case 531: case 537:
2049     case 541: case 664: case 665: case 696:
2050     case 728: case 729: case 760: case 920:
2051     case 921: case 952:
2052     goto power_inst;
2053     }
2054     }
2055    
2056     // In GUI mode, show error alert
2057     if (!PrefsFindBool("nogui")) {
2058 gbeauche 1.26 sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode);
2059 cebix 1.1 ErrorAlert(str);
2060     QuitEmulator();
2061     return;
2062     }
2063     }
2064    
2065     // For all other errors, jump into debugger (sort of...)
2066 gbeauche 1.23 crash_reason = "SIGILL";
2067 cebix 1.1 if (!ready_for_signals) {
2068 gbeauche 1.23 printf("%s\n");
2069 gbeauche 1.26 printf(" sigcontext %p, machine_regs %p\n", scp, r);
2070 cebix 1.1 printf(
2071     " pc %08lx lr %08lx ctr %08lx msr %08lx\n"
2072     " xer %08lx cr %08lx \n"
2073     " r0 %08lx r1 %08lx r2 %08lx r3 %08lx\n"
2074     " r4 %08lx r5 %08lx r6 %08lx r7 %08lx\n"
2075     " r8 %08lx r9 %08lx r10 %08lx r11 %08lx\n"
2076     " r12 %08lx r13 %08lx r14 %08lx r15 %08lx\n"
2077     " r16 %08lx r17 %08lx r18 %08lx r19 %08lx\n"
2078     " r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n"
2079     " r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n"
2080     " r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n",
2081 gbeauche 1.23 crash_reason,
2082 gbeauche 1.26 r->pc(), r->lr(), r->ctr(), r->msr(),
2083     r->xer(), r->cr(),
2084     r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3),
2085     r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7),
2086     r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11),
2087     r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15),
2088     r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19),
2089     r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23),
2090     r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27),
2091     r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31));
2092 cebix 1.1 exit(1);
2093     QuitEmulator();
2094     return;
2095     } else {
2096     // We crashed. Save registers, tell tick thread and loop forever
2097 gbeauche 1.26 build_sigregs(&sigsegv_regs, r);
2098 cebix 1.1 emul_thread_fatal = true;
2099     for (;;) ;
2100     }
2101     rti:;
2102     }
2103     #endif
2104 gbeauche 1.15
2105    
2106     /*
2107     * Helpers to share 32-bit addressable data with MacOS
2108     */
2109    
2110     bool SheepMem::Init(void)
2111     {
2112 gbeauche 1.31 // Size of a native page
2113     page_size = getpagesize();
2114 gbeauche 1.20
2115     // Allocate SheepShaver globals
2116 gbeauche 1.15 if (vm_acquire_fixed((char *)base, size) < 0)
2117     return false;
2118 gbeauche 1.18
2119 gbeauche 1.20 // Allocate page with all bits set to 0
2120 gbeauche 1.18 zero_page = base + size;
2121     if (vm_acquire_fixed((char *)zero_page, page_size) < 0)
2122     return false;
2123 gbeauche 1.19 memset((char *)zero_page, 0, page_size);
2124 gbeauche 1.18 if (vm_protect((char *)zero_page, page_size, VM_PAGE_READ) < 0)
2125     return false;
2126    
2127 gbeauche 1.20 #if EMULATED_PPC
2128     // Allocate alternate stack for PowerPC interrupt routine
2129     sig_stack = zero_page + page_size;
2130     if (vm_acquire_fixed((char *)sig_stack, SIG_STACK_SIZE) < 0)
2131     return false;
2132     #endif
2133    
2134 gbeauche 1.15 top = base + size;
2135     return true;
2136     }
2137    
2138     void SheepMem::Exit(void)
2139     {
2140 gbeauche 1.18 if (top) {
2141 gbeauche 1.20 // Delete SheepShaver globals
2142     vm_release((void *)base, size);
2143    
2144     // Delete zero page
2145     vm_release((void *)zero_page, page_size);
2146    
2147     #if EMULATED_PPC
2148     // Delete alternate stack for PowerPC interrupt routine
2149     vm_release((void *)sig_stack, SIG_STACK_SIZE);
2150     #endif
2151 gbeauche 1.18 }
2152 gbeauche 1.15 }
2153 cebix 1.1
2154    
2155     /*
2156     * Display alert
2157     */
2158    
2159     #ifdef ENABLE_GTK
2160     static void dl_destroyed(void)
2161     {
2162     gtk_main_quit();
2163     }
2164    
2165     static void dl_quit(GtkWidget *dialog)
2166     {
2167     gtk_widget_destroy(dialog);
2168     }
2169    
2170     void display_alert(int title_id, int prefix_id, int button_id, const char *text)
2171     {
2172     char str[256];
2173     sprintf(str, GetString(prefix_id), text);
2174    
2175     GtkWidget *dialog = gtk_dialog_new();
2176     gtk_window_set_title(GTK_WINDOW(dialog), GetString(title_id));
2177     gtk_container_border_width(GTK_CONTAINER(dialog), 5);
2178     gtk_widget_set_uposition(GTK_WIDGET(dialog), 100, 150);
2179     gtk_signal_connect(GTK_OBJECT(dialog), "destroy", GTK_SIGNAL_FUNC(dl_destroyed), NULL);
2180    
2181     GtkWidget *label = gtk_label_new(str);
2182     gtk_widget_show(label);
2183     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), label, TRUE, TRUE, 0);
2184    
2185     GtkWidget *button = gtk_button_new_with_label(GetString(button_id));
2186     gtk_widget_show(button);
2187     gtk_signal_connect_object(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(dl_quit), GTK_OBJECT(dialog));
2188     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area), button, FALSE, FALSE, 0);
2189     GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
2190     gtk_widget_grab_default(button);
2191     gtk_widget_show(dialog);
2192    
2193     gtk_main();
2194     }
2195     #endif
2196    
2197    
2198     /*
2199     * Display error alert
2200     */
2201    
2202     void ErrorAlert(const char *text)
2203     {
2204 gbeauche 1.42 #if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO)
2205 cebix 1.1 if (PrefsFindBool("nogui") || x_display == NULL) {
2206     printf(GetString(STR_SHELL_ERROR_PREFIX), text);
2207     return;
2208     }
2209     VideoQuitFullScreen();
2210     display_alert(STR_ERROR_ALERT_TITLE, STR_GUI_ERROR_PREFIX, STR_QUIT_BUTTON, text);
2211     #else
2212     printf(GetString(STR_SHELL_ERROR_PREFIX), text);
2213     #endif
2214     }
2215    
2216    
2217     /*
2218     * Display warning alert
2219     */
2220    
2221     void WarningAlert(const char *text)
2222     {
2223 gbeauche 1.42 #if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO)
2224 cebix 1.1 if (PrefsFindBool("nogui") || x_display == NULL) {
2225     printf(GetString(STR_SHELL_WARNING_PREFIX), text);
2226     return;
2227     }
2228     display_alert(STR_WARNING_ALERT_TITLE, STR_GUI_WARNING_PREFIX, STR_OK_BUTTON, text);
2229     #else
2230     printf(GetString(STR_SHELL_WARNING_PREFIX), text);
2231     #endif
2232     }
2233    
2234    
2235     /*
2236     * Display choice alert
2237     */
2238    
2239     bool ChoiceAlert(const char *text, const char *pos, const char *neg)
2240     {
2241     printf(GetString(STR_SHELL_WARNING_PREFIX), text);
2242     return false; //!!
2243     }