ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/main_unix.cpp
Revision: 1.59
Committed: 2005-02-20T18:08:50Z (19 years, 8 months ago) by gbeauche
Branch: MAIN
Changes since 1.58: +20 -0 lines
Log Message:
Initial support for NetBSD/ppc in native mode (some crashes occur but I
could boot MacOS 9.0.4)

File Contents

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