ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/main_unix.cpp
Revision: 1.48
Committed: 2004-07-04T05:19:44Z (20 years, 4 months ago) by gbeauche
Branch: MAIN
Changes since 1.47: +2 -0 lines
Log Message:
get timebase-frequency on osx too.

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