ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/main_unix.cpp
Revision: 1.47
Committed: 2004-07-03T10:39:06Z (20 years, 4 months ago) by gbeauche
Branch: MAIN
Changes since 1.46: +29 -2 lines
Log Message:
Introducce TimebaseSpeed which represents exact timebase-frequency instead
of supposing it to be (BusClockSpeed/4), which is no longer true on G5 et al.

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