ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/main_unix.cpp
Revision: 1.45
Committed: 2004-06-29T20:25:55Z (20 years, 4 months ago) by gbeauche
Branch: MAIN
Changes since 1.44: +8 -0 lines
Log Message:
Handle 750FX, 7450, 7455, 7457.

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