ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/main_unix.cpp
Revision: 1.46
Committed: 2004-07-01T22:55:02Z (20 years, 4 months ago) by gbeauche
Branch: MAIN
Changes since 1.45: +2 -0 lines
Log Message:
Try to recognize and handle PowerPC 970 (G5). Untested as I don't have such
platforms handy.

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