ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/main_unix.cpp
Revision: 1.49
Committed: 2004-07-07T04:33:37Z (20 years, 4 months ago) by gbeauche
Branch: MAIN
Changes since 1.48: +9 -0 lines
Log Message:
Remap any newer G4/G5 processor to plain G4 for compatibility

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