ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/main_unix.cpp
Revision: 1.17
Committed: 2003-12-04T23:37:36Z (20 years, 11 months ago) by gbeauche
Branch: MAIN
Changes since 1.16: +5 -30 lines
Log Message:
Use a unique ExecuteNative() interface in any case, i.e. native & emulated

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * main_unix.cpp - Emulation core, Unix implementation
3     *
4     * SheepShaver (C) 1997-2002 Christian Bauer and Marc Hellwig
5     *
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     * TODO:
69     * check if SIGSEGV handler works for all registers (including FP!)
70     */
71    
72     #include <unistd.h>
73     #include <fcntl.h>
74     #include <time.h>
75     #include <errno.h>
76     #include <stdio.h>
77     #include <stdlib.h>
78     #include <string.h>
79     #include <pthread.h>
80     #include <sys/mman.h>
81     #include <sys/ipc.h>
82     #include <sys/shm.h>
83     #include <signal.h>
84    
85     #include "sysdeps.h"
86     #include "main.h"
87     #include "version.h"
88     #include "prefs.h"
89     #include "prefs_editor.h"
90     #include "cpu_emulation.h"
91     #include "emul_op.h"
92     #include "xlowmem.h"
93     #include "xpram.h"
94     #include "timer.h"
95     #include "adb.h"
96     #include "sony.h"
97     #include "disk.h"
98     #include "cdrom.h"
99     #include "scsi.h"
100     #include "video.h"
101     #include "audio.h"
102     #include "ether.h"
103     #include "serial.h"
104     #include "clip.h"
105     #include "extfs.h"
106     #include "sys.h"
107     #include "macos_util.h"
108     #include "rom_patches.h"
109     #include "user_strings.h"
110 gbeauche 1.4 #include "vm_alloc.h"
111 gbeauche 1.5 #include "sigsegv.h"
112 gbeauche 1.15 #include "thunks.h"
113 cebix 1.1
114     #define DEBUG 0
115     #include "debug.h"
116    
117    
118     #include <X11/Xlib.h>
119    
120     #ifdef ENABLE_GTK
121     #include <gtk/gtk.h>
122     #endif
123    
124     #ifdef ENABLE_XF86_DGA
125     #include <X11/Xlib.h>
126     #include <X11/Xutil.h>
127     #include <X11/extensions/xf86dga.h>
128     #endif
129    
130     #ifdef ENABLE_MON
131     #include "mon.h"
132     #endif
133    
134    
135     // Enable Execute68k() safety checks?
136     #define SAFE_EXEC_68K 0
137    
138     // Interrupts in EMUL_OP mode?
139     #define INTERRUPTS_IN_EMUL_OP_MODE 1
140    
141     // Interrupts in native mode?
142     #define INTERRUPTS_IN_NATIVE_MODE 1
143    
144    
145     // Constants
146     const char ROM_FILE_NAME[] = "ROM";
147     const char ROM_FILE_NAME2[] = "Mac OS ROM";
148    
149 gbeauche 1.15 const uintptr RAM_BASE = 0x20000000; // Base address of RAM
150 cebix 1.1 const uint32 SIG_STACK_SIZE = 0x10000; // Size of signal stack
151    
152    
153     #if !EMULATED_PPC
154     // Structure in which registers are saved in a signal handler;
155     // sigcontext->regs points to it
156     // (see arch/ppc/kernel/signal.c)
157     typedef struct {
158     uint32 u[4];
159     } __attribute((aligned(16))) vector128;
160     #include <linux/elf.h>
161    
162     struct sigregs {
163     elf_gregset_t gp_regs; // Identical to pt_regs
164     double fp_regs[ELF_NFPREG]; // f0..f31 and fpsrc
165     //more (uninteresting) stuff following here
166     };
167     #endif
168    
169    
170     // Global variables (exported)
171     #if !EMULATED_PPC
172     void *TOC; // Small data pointer (r13)
173     #endif
174     uint32 RAMBase; // Base address of Mac RAM
175     uint32 RAMSize; // Size of Mac RAM
176     uint32 KernelDataAddr; // Address of Kernel Data
177     uint32 BootGlobsAddr; // Address of BootGlobs structure at top of Mac RAM
178     uint32 PVR; // Theoretical PVR
179     int64 CPUClockSpeed; // Processor clock speed (Hz)
180     int64 BusClockSpeed; // Bus clock speed (Hz)
181    
182    
183     // Global variables
184 gbeauche 1.11 char *x_display_name = NULL; // X11 display name
185 cebix 1.1 Display *x_display = NULL; // X11 display handle
186    
187     static int zero_fd = 0; // FD of /dev/zero
188     static bool lm_area_mapped = false; // Flag: Low Memory area mmap()ped
189     static int kernel_area = -1; // SHM ID of Kernel Data area
190     static bool rom_area_mapped = false; // Flag: Mac ROM mmap()ped
191     static bool ram_area_mapped = false; // Flag: Mac RAM mmap()ped
192     static KernelData *kernel_data; // Pointer to Kernel Data
193     static EmulatorData *emulator_data;
194    
195     static uint8 last_xpram[XPRAM_SIZE]; // Buffer for monitoring XPRAM changes
196    
197     static bool nvram_thread_active = false; // Flag: NVRAM watchdog installed
198     static pthread_t nvram_thread; // NVRAM watchdog
199     static bool tick_thread_active = false; // Flag: MacOS thread installed
200     static pthread_t tick_thread; // 60Hz thread
201     static pthread_t emul_thread; // MacOS thread
202    
203     static bool ready_for_signals = false; // Handler installed, signals can be sent
204     static int64 num_segv = 0; // Number of handled SEGV signals
205    
206 gbeauche 1.6 static struct sigaction sigusr2_action; // Interrupt signal (of emulator thread)
207 cebix 1.1 #if !EMULATED_PPC
208     static struct sigaction sigsegv_action; // Data access exception signal (of emulator thread)
209     static struct sigaction sigill_action; // Illegal instruction signal (of emulator thread)
210     static void *sig_stack = NULL; // Stack for signal handlers
211     static void *extra_stack = NULL; // Stack for SIGSEGV inside interrupt handler
212     static bool emul_thread_fatal = false; // Flag: MacOS thread crashed, tick thread shall dump debug output
213     static sigregs sigsegv_regs; // Register dump when crashed
214     #endif
215    
216 gbeauche 1.15 uintptr SheepMem::base = 0x60000000; // Address of SheepShaver data
217     uintptr SheepMem::top = 0; // Top of SheepShaver data (stack like storage)
218    
219 cebix 1.1
220     // Prototypes
221     static void Quit(void);
222     static void *emul_func(void *arg);
223     static void *nvram_func(void *arg);
224     static void *tick_func(void *arg);
225 gbeauche 1.8 #if EMULATED_PPC
226     static void sigusr2_handler(int sig);
227 gbeauche 1.13 extern void emul_ppc(uint32 start);
228     extern void init_emul_ppc(void);
229     extern void exit_emul_ppc(void);
230 gbeauche 1.8 #else
231 gbeauche 1.6 static void sigusr2_handler(int sig, sigcontext_struct *sc);
232 cebix 1.1 static void sigsegv_handler(int sig, sigcontext_struct *sc);
233     static void sigill_handler(int sig, sigcontext_struct *sc);
234     #endif
235    
236    
237     // From asm_linux.S
238 gbeauche 1.12 #if !EMULATED_PPC
239 cebix 1.1 extern "C" void *get_toc(void);
240     extern "C" void *get_sp(void);
241     extern "C" void flush_icache_range(void *start, void *end);
242     extern "C" void jump_to_rom(uint32 entry, uint32 context);
243     extern "C" void quit_emulator(void);
244     extern "C" void execute_68k(uint32 pc, M68kRegisters *r);
245     extern "C" void ppc_interrupt(uint32 entry, uint32 kernel_data);
246     extern "C" int atomic_add(int *var, int v);
247     extern "C" int atomic_and(int *var, int v);
248     extern "C" int atomic_or(int *var, int v);
249     extern void paranoia_check(void);
250 gbeauche 1.12 #endif
251    
252    
253     #if EMULATED_PPC
254     /*
255     * Atomic operations
256     */
257    
258     #if HAVE_SPINLOCKS
259     static spinlock_t atomic_ops_lock = SPIN_LOCK_UNLOCKED;
260     #else
261     #define spin_lock(LOCK)
262     #define spin_unlock(LOCK)
263     #endif
264    
265     int atomic_add(int *var, int v)
266     {
267     spin_lock(&atomic_ops_lock);
268     int ret = *var;
269     *var += v;
270     spin_unlock(&atomic_ops_lock);
271     return ret;
272     }
273    
274     int atomic_and(int *var, int v)
275     {
276     spin_lock(&atomic_ops_lock);
277     int ret = *var;
278     *var &= v;
279     spin_unlock(&atomic_ops_lock);
280     return ret;
281     }
282    
283     int atomic_or(int *var, int v)
284     {
285     spin_lock(&atomic_ops_lock);
286     int ret = *var;
287     *var |= v;
288     spin_unlock(&atomic_ops_lock);
289     return ret;
290     }
291 cebix 1.1 #endif
292    
293    
294     /*
295     * Main program
296     */
297    
298     static void usage(const char *prg_name)
299     {
300     printf("Usage: %s [OPTION...]\n", prg_name);
301     printf("\nUnix options:\n");
302     printf(" --display STRING\n X display to use\n");
303     PrefsPrintUsage();
304     exit(0);
305     }
306    
307     int main(int argc, char **argv)
308     {
309     char str[256];
310     uint32 *boot_globs;
311     int16 i16;
312     int rom_fd;
313     FILE *proc_file;
314     const char *rom_path;
315     uint32 rom_size, actual;
316     uint8 *rom_tmp;
317     time_t now, expire;
318    
319     // Initialize variables
320     RAMBase = 0;
321     tzset();
322    
323     // Print some info
324     printf(GetString(STR_ABOUT_TEXT1), VERSION_MAJOR, VERSION_MINOR);
325     printf(" %s\n", GetString(STR_ABOUT_TEXT2));
326    
327     #if !EMULATED_PPC
328     // Get TOC pointer
329     TOC = get_toc();
330     #endif
331    
332     #ifdef ENABLE_GTK
333     // Init GTK
334     gtk_set_locale();
335     gtk_init(&argc, &argv);
336     #endif
337    
338     // Read preferences
339     PrefsInit(argc, argv);
340    
341     // Parse command line arguments
342     for (int i=1; i<argc; i++) {
343     if (strcmp(argv[i], "--help") == 0) {
344     usage(argv[0]);
345     } else if (strcmp(argv[i], "--display") == 0) {
346     i++;
347     if (i < argc)
348     x_display_name = strdup(argv[i]);
349     } else if (argv[i][0] == '-') {
350     fprintf(stderr, "Unrecognized option '%s'\n", argv[i]);
351     usage(argv[0]);
352     }
353     }
354    
355     // Open display
356     x_display = XOpenDisplay(x_display_name);
357     if (x_display == NULL) {
358     char str[256];
359     sprintf(str, GetString(STR_NO_XSERVER_ERR), XDisplayName(x_display_name));
360     ErrorAlert(str);
361     goto quit;
362     }
363    
364     #if defined(ENABLE_XF86_DGA) && !defined(ENABLE_MON)
365     // Fork out, so we can return from fullscreen mode when things get ugly
366     XF86DGAForkApp(DefaultScreen(x_display));
367     #endif
368    
369     #ifdef ENABLE_MON
370     // Initialize mon
371     mon_init();
372     #endif
373    
374     // Get system info
375     PVR = 0x00040000; // Default: 604
376     CPUClockSpeed = 100000000; // Default: 100MHz
377     BusClockSpeed = 100000000; // Default: 100MHz
378     #if !EMULATED_PPC
379     proc_file = fopen("/proc/cpuinfo", "r");
380     if (proc_file) {
381     char line[256];
382     while(fgets(line, 255, proc_file)) {
383     // Read line
384     int len = strlen(line);
385     if (len == 0)
386     continue;
387     line[len-1] = 0;
388    
389     // Parse line
390     int i;
391     char value[256];
392     if (sscanf(line, "cpu : %s", value) == 1) {
393     if (strcmp(value, "601") == 0)
394     PVR = 0x00010000;
395     else if (strcmp(value, "603") == 0)
396     PVR = 0x00030000;
397     else if (strcmp(value, "604") == 0)
398     PVR = 0x00040000;
399     else if (strcmp(value, "603e") == 0)
400     PVR = 0x00060000;
401     else if (strcmp(value, "603ev") == 0)
402     PVR = 0x00070000;
403     else if (strcmp(value, "604e") == 0)
404     PVR = 0x00090000;
405     else if (strcmp(value, "604ev5") == 0)
406     PVR = 0x000a0000;
407     else if (strcmp(value, "750") == 0)
408     PVR = 0x00080000;
409     else if (strcmp(value, "821") == 0)
410     PVR = 0x00320000;
411     else if (strcmp(value, "860") == 0)
412     PVR = 0x00500000;
413     else
414     printf("WARNING: Unknown CPU type '%s', assuming 604\n", value);
415     }
416     if (sscanf(line, "clock : %dMHz", &i) == 1)
417     CPUClockSpeed = BusClockSpeed = i * 1000000;
418     }
419     fclose(proc_file);
420     } else {
421     sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno));
422     WarningAlert(str);
423     }
424     #endif
425     D(bug("PVR: %08x (assumed)\n", PVR));
426    
427     // Init system routines
428     SysInit();
429    
430     // Show preferences editor
431     if (!PrefsFindBool("nogui"))
432     if (!PrefsEditor())
433     goto quit;
434    
435     #if !EMULATED_PPC
436     // Check some things
437     paranoia_check();
438     #endif
439    
440     // Open /dev/zero
441     zero_fd = open("/dev/zero", O_RDWR);
442     if (zero_fd < 0) {
443     sprintf(str, GetString(STR_NO_DEV_ZERO_ERR), strerror(errno));
444     ErrorAlert(str);
445     goto quit;
446     }
447    
448     // Create Low Memory area (0x0000..0x3000)
449 gbeauche 1.4 if (vm_acquire_fixed((char *)0, 0x3000) < 0) {
450 cebix 1.1 sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno));
451     ErrorAlert(str);
452     goto quit;
453     }
454     lm_area_mapped = true;
455    
456     // Create areas for Kernel Data
457     kernel_area = shmget(IPC_PRIVATE, KERNEL_AREA_SIZE, 0600);
458     if (kernel_area == -1) {
459     sprintf(str, GetString(STR_KD_SHMGET_ERR), strerror(errno));
460     ErrorAlert(str);
461     goto quit;
462     }
463     if (shmat(kernel_area, (void *)KERNEL_DATA_BASE, 0) < 0) {
464     sprintf(str, GetString(STR_KD_SHMAT_ERR), strerror(errno));
465     ErrorAlert(str);
466     goto quit;
467     }
468     if (shmat(kernel_area, (void *)KERNEL_DATA2_BASE, 0) < 0) {
469     sprintf(str, GetString(STR_KD2_SHMAT_ERR), strerror(errno));
470     ErrorAlert(str);
471     goto quit;
472     }
473 gbeauche 1.15 kernel_data = (KernelData *)KERNEL_DATA_BASE;
474 cebix 1.1 emulator_data = &kernel_data->ed;
475 gbeauche 1.15 KernelDataAddr = KERNEL_DATA_BASE;
476 cebix 1.1 D(bug("Kernel Data at %p, Emulator Data at %p\n", kernel_data, emulator_data));
477    
478 gbeauche 1.8 // Create area for SheepShaver data
479 gbeauche 1.15 if (!SheepMem::Init()) {
480 gbeauche 1.8 sprintf(str, GetString(STR_SHEEP_MEM_MMAP_ERR), strerror(errno));
481     ErrorAlert(str);
482     goto quit;
483     }
484    
485 cebix 1.1 // Create area for Mac ROM
486 gbeauche 1.4 if (vm_acquire_fixed((char *)ROM_BASE, ROM_AREA_SIZE) < 0) {
487 cebix 1.1 sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno));
488     ErrorAlert(str);
489     goto quit;
490     }
491 gbeauche 1.6 #if !EMULATED_PPC || defined(__powerpc__)
492 gbeauche 1.4 if (vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) {
493     sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno));
494     ErrorAlert(str);
495     goto quit;
496     }
497     #endif
498 cebix 1.1 rom_area_mapped = true;
499     D(bug("ROM area at %08x\n", ROM_BASE));
500    
501     // Create area for Mac RAM
502     RAMSize = PrefsFindInt32("ramsize");
503     if (RAMSize < 8*1024*1024) {
504     WarningAlert(GetString(STR_SMALL_RAM_WARN));
505     RAMSize = 8*1024*1024;
506     }
507    
508 gbeauche 1.8 if (vm_acquire_fixed((char *)RAM_BASE, RAMSize) < 0) {
509 cebix 1.1 sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno));
510     ErrorAlert(str);
511     goto quit;
512     }
513 gbeauche 1.4 #if !EMULATED_PPC
514 gbeauche 1.8 if (vm_protect((char *)RAM_BASE, RAMSize, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) {
515 gbeauche 1.4 sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno));
516     ErrorAlert(str);
517     goto quit;
518     }
519     #endif
520 gbeauche 1.8 RAMBase = RAM_BASE;
521 cebix 1.1 ram_area_mapped = true;
522     D(bug("RAM area at %08x\n", RAMBase));
523    
524     if (RAMBase > ROM_BASE) {
525     ErrorAlert(GetString(STR_RAM_HIGHER_THAN_ROM_ERR));
526     goto quit;
527     }
528    
529     // Load Mac ROM
530     rom_path = PrefsFindString("rom");
531     rom_fd = open(rom_path ? rom_path : ROM_FILE_NAME, O_RDONLY);
532     if (rom_fd < 0) {
533     rom_fd = open(rom_path ? rom_path : ROM_FILE_NAME2, O_RDONLY);
534     if (rom_fd < 0) {
535     ErrorAlert(GetString(STR_NO_ROM_FILE_ERR));
536     goto quit;
537     }
538     }
539     printf(GetString(STR_READING_ROM_FILE));
540     rom_size = lseek(rom_fd, 0, SEEK_END);
541     lseek(rom_fd, 0, SEEK_SET);
542     rom_tmp = new uint8[ROM_SIZE];
543     actual = read(rom_fd, (void *)rom_tmp, ROM_SIZE);
544     close(rom_fd);
545 gbeauche 1.3
546     // Decode Mac ROM
547     if (!DecodeROM(rom_tmp, actual)) {
548     if (rom_size != 4*1024*1024) {
549 cebix 1.1 ErrorAlert(GetString(STR_ROM_SIZE_ERR));
550     goto quit;
551     } else {
552     ErrorAlert(GetString(STR_ROM_FILE_READ_ERR));
553     goto quit;
554     }
555     }
556 gbeauche 1.3 delete[] rom_tmp;
557 cebix 1.1
558     // Load NVRAM
559     XPRAMInit();
560    
561     // Set boot volume
562 cebix 1.10 i16 = PrefsFindInt32("bootdrive");
563 cebix 1.1 XPRAM[0x1378] = i16 >> 8;
564     XPRAM[0x1379] = i16 & 0xff;
565 cebix 1.10 i16 = PrefsFindInt32("bootdriver");
566 cebix 1.1 XPRAM[0x137a] = i16 >> 8;
567     XPRAM[0x137b] = i16 & 0xff;
568    
569     // Create BootGlobs at top of Mac memory
570     memset((void *)(RAMBase + RAMSize - 4096), 0, 4096);
571     BootGlobsAddr = RAMBase + RAMSize - 0x1c;
572     boot_globs = (uint32 *)BootGlobsAddr;
573     boot_globs[-5] = htonl(RAMBase + RAMSize); // MemTop
574     boot_globs[0] = htonl(RAMBase); // First RAM bank
575     boot_globs[1] = htonl(RAMSize);
576     boot_globs[2] = htonl((uint32)-1); // End of bank table
577    
578 gbeauche 1.15 // Init thunks
579     if (!ThunksInit())
580     goto quit;
581    
582 cebix 1.1 // Init drivers
583     SonyInit();
584     DiskInit();
585     CDROMInit();
586     SCSIInit();
587    
588     // Init external file system
589     ExtFSInit();
590    
591     // Init audio
592     AudioInit();
593    
594     // Init network
595     EtherInit();
596    
597     // Init serial ports
598     SerialInit();
599    
600     // Init Time Manager
601     TimerInit();
602    
603     // Init clipboard
604     ClipInit();
605    
606     // Init video
607     if (!VideoInit())
608     goto quit;
609    
610     // Install ROM patches
611     if (!PatchROM()) {
612     ErrorAlert(GetString(STR_UNSUPPORTED_ROM_TYPE_ERR));
613     goto quit;
614     }
615    
616     // Clear caches (as we loaded and patched code) and write protect ROM
617     #if !EMULATED_PPC
618     MakeExecutable(0, (void *)ROM_BASE, ROM_AREA_SIZE);
619     #endif
620 gbeauche 1.4 vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_EXECUTE);
621 cebix 1.1
622     // Initialize Kernel Data
623     memset(kernel_data, 0, sizeof(KernelData));
624     if (ROMType == ROMTYPE_NEWWORLD) {
625 gbeauche 1.15 uintptr of_dev_tree = SheepMem::Reserve(4 * sizeof(uint32));
626     memset((void *)of_dev_tree, 0, 4 * sizeof(uint32));
627     uintptr vector_lookup_tbl = SheepMem::Reserve(128);
628     uintptr vector_mask_tbl = SheepMem::Reserve(64);
629 cebix 1.1 memset((uint8 *)kernel_data + 0xb80, 0x3d, 0x80);
630 gbeauche 1.15 memset((void *)vector_lookup_tbl, 0, 128);
631     memset((void *)vector_mask_tbl, 0, 64);
632 cebix 1.1 kernel_data->v[0xb80 >> 2] = htonl(ROM_BASE);
633 gbeauche 1.15 kernel_data->v[0xb84 >> 2] = htonl(of_dev_tree); // OF device tree base
634     kernel_data->v[0xb90 >> 2] = htonl(vector_lookup_tbl);
635     kernel_data->v[0xb94 >> 2] = htonl(vector_mask_tbl);
636 cebix 1.1 kernel_data->v[0xb98 >> 2] = htonl(ROM_BASE); // OpenPIC base
637     kernel_data->v[0xbb0 >> 2] = htonl(0); // ADB base
638     kernel_data->v[0xc20 >> 2] = htonl(RAMSize);
639     kernel_data->v[0xc24 >> 2] = htonl(RAMSize);
640     kernel_data->v[0xc30 >> 2] = htonl(RAMSize);
641     kernel_data->v[0xc34 >> 2] = htonl(RAMSize);
642     kernel_data->v[0xc38 >> 2] = htonl(0x00010020);
643     kernel_data->v[0xc3c >> 2] = htonl(0x00200001);
644     kernel_data->v[0xc40 >> 2] = htonl(0x00010000);
645     kernel_data->v[0xc50 >> 2] = htonl(RAMBase);
646     kernel_data->v[0xc54 >> 2] = htonl(RAMSize);
647     kernel_data->v[0xf60 >> 2] = htonl(PVR);
648     kernel_data->v[0xf64 >> 2] = htonl(CPUClockSpeed);
649     kernel_data->v[0xf68 >> 2] = htonl(BusClockSpeed);
650     kernel_data->v[0xf6c >> 2] = htonl(CPUClockSpeed);
651     } else {
652     kernel_data->v[0xc80 >> 2] = htonl(RAMSize);
653     kernel_data->v[0xc84 >> 2] = htonl(RAMSize);
654     kernel_data->v[0xc90 >> 2] = htonl(RAMSize);
655     kernel_data->v[0xc94 >> 2] = htonl(RAMSize);
656     kernel_data->v[0xc98 >> 2] = htonl(0x00010020);
657     kernel_data->v[0xc9c >> 2] = htonl(0x00200001);
658     kernel_data->v[0xca0 >> 2] = htonl(0x00010000);
659     kernel_data->v[0xcb0 >> 2] = htonl(RAMBase);
660     kernel_data->v[0xcb4 >> 2] = htonl(RAMSize);
661     kernel_data->v[0xf80 >> 2] = htonl(PVR);
662     kernel_data->v[0xf84 >> 2] = htonl(CPUClockSpeed);
663     kernel_data->v[0xf88 >> 2] = htonl(BusClockSpeed);
664     kernel_data->v[0xf8c >> 2] = htonl(CPUClockSpeed);
665     }
666    
667     // Initialize extra low memory
668     D(bug("Initializing Low Memory...\n"));
669     memset(NULL, 0, 0x3000);
670     WriteMacInt32(XLM_SIGNATURE, FOURCC('B','a','a','h')); // Signature to detect SheepShaver
671 gbeauche 1.15 WriteMacInt32(XLM_KERNEL_DATA, KernelDataAddr); // For trap replacement routines
672 cebix 1.1 WriteMacInt32(XLM_PVR, PVR); // Theoretical PVR
673     WriteMacInt32(XLM_BUS_CLOCK, BusClockSpeed); // For DriverServicesLib patch
674     WriteMacInt16(XLM_EXEC_RETURN_OPCODE, M68K_EXEC_RETURN); // For Execute68k() (RTS from the executed 68k code will jump here and end 68k mode)
675 gbeauche 1.17 #if !EMULATED_PPC
676     WriteMacInt32(XLM_TOC, (uint32)TOC); // TOC pointer of emulator
677     #endif
678     WriteMacInt32(XLM_ETHER_INIT, NativeFunction(NATIVE_ETHER_INIT)); // DLPI ethernet driver functions
679 gbeauche 1.15 WriteMacInt32(XLM_ETHER_TERM, NativeFunction(NATIVE_ETHER_TERM));
680     WriteMacInt32(XLM_ETHER_OPEN, NativeFunction(NATIVE_ETHER_OPEN));
681     WriteMacInt32(XLM_ETHER_CLOSE, NativeFunction(NATIVE_ETHER_CLOSE));
682     WriteMacInt32(XLM_ETHER_WPUT, NativeFunction(NATIVE_ETHER_WPUT));
683     WriteMacInt32(XLM_ETHER_RSRV, NativeFunction(NATIVE_ETHER_RSRV));
684     WriteMacInt32(XLM_VIDEO_DOIO, NativeFunction(NATIVE_VIDEO_DO_DRIVER_IO));
685 cebix 1.1 D(bug("Low Memory initialized\n"));
686    
687     // Start 60Hz thread
688     tick_thread_active = (pthread_create(&tick_thread, NULL, tick_func, NULL) == 0);
689     D(bug("Tick thread installed (%ld)\n", tick_thread));
690    
691     // Start NVRAM watchdog thread
692     memcpy(last_xpram, XPRAM, XPRAM_SIZE);
693     nvram_thread_active = (pthread_create(&nvram_thread, NULL, nvram_func, NULL) == 0);
694     D(bug("NVRAM thread installed (%ld)\n", nvram_thread));
695    
696     #if !EMULATED_PPC
697     // Create and install stacks for signal handlers
698     sig_stack = malloc(SIG_STACK_SIZE);
699     D(bug("Signal stack at %p\n", sig_stack));
700     if (sig_stack == NULL) {
701     ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR));
702     goto quit;
703     }
704     extra_stack = malloc(SIG_STACK_SIZE);
705     D(bug("Extra stack at %p\n", extra_stack));
706     if (extra_stack == NULL) {
707     ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR));
708     goto quit;
709     }
710     struct sigaltstack new_stack;
711     new_stack.ss_sp = sig_stack;
712     new_stack.ss_flags = 0;
713     new_stack.ss_size = SIG_STACK_SIZE;
714     if (sigaltstack(&new_stack, NULL) < 0) {
715     sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno));
716     ErrorAlert(str);
717     goto quit;
718     }
719     #endif
720    
721     #if !EMULATED_PPC
722     // Install SIGSEGV handler
723     sigemptyset(&sigsegv_action.sa_mask); // Block interrupts during SEGV handling
724     sigaddset(&sigsegv_action.sa_mask, SIGUSR2);
725     sigsegv_action.sa_handler = (__sighandler_t)sigsegv_handler;
726     sigsegv_action.sa_flags = SA_ONSTACK;
727     sigsegv_action.sa_restorer = NULL;
728     if (sigaction(SIGSEGV, &sigsegv_action, NULL) < 0) {
729     sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno));
730     ErrorAlert(str);
731     goto quit;
732     }
733    
734     // Install SIGILL handler
735     sigemptyset(&sigill_action.sa_mask); // Block interrupts during ILL handling
736     sigaddset(&sigill_action.sa_mask, SIGUSR2);
737     sigill_action.sa_handler = (__sighandler_t)sigill_handler;
738     sigill_action.sa_flags = SA_ONSTACK;
739     sigill_action.sa_restorer = NULL;
740     if (sigaction(SIGILL, &sigill_action, NULL) < 0) {
741     sprintf(str, GetString(STR_SIGILL_INSTALL_ERR), strerror(errno));
742     ErrorAlert(str);
743     goto quit;
744     }
745 gbeauche 1.6 #endif
746 cebix 1.1
747     // Install interrupt signal handler
748     sigemptyset(&sigusr2_action.sa_mask);
749     sigusr2_action.sa_handler = (__sighandler_t)sigusr2_handler;
750 gbeauche 1.8 sigusr2_action.sa_flags = 0;
751     #if !EMULATED_PPC
752 cebix 1.1 sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART;
753 gbeauche 1.8 #endif
754 cebix 1.1 sigusr2_action.sa_restorer = NULL;
755     if (sigaction(SIGUSR2, &sigusr2_action, NULL) < 0) {
756     sprintf(str, GetString(STR_SIGUSR2_INSTALL_ERR), strerror(errno));
757     ErrorAlert(str);
758     goto quit;
759     }
760    
761     // Get my thread ID and execute MacOS thread function
762     emul_thread = pthread_self();
763     D(bug("MacOS thread is %ld\n", emul_thread));
764     emul_func(NULL);
765    
766     quit:
767     Quit();
768     return 0;
769     }
770    
771    
772     /*
773     * Cleanup and quit
774     */
775    
776     static void Quit(void)
777     {
778 gbeauche 1.13 #if EMULATED_PPC
779     // Exit PowerPC emulation
780     exit_emul_ppc();
781     #endif
782    
783 cebix 1.1 // Stop 60Hz thread
784     if (tick_thread_active) {
785     pthread_cancel(tick_thread);
786     pthread_join(tick_thread, NULL);
787     }
788    
789     // Stop NVRAM watchdog thread
790     if (nvram_thread_active) {
791     pthread_cancel(nvram_thread);
792     pthread_join(nvram_thread, NULL);
793     }
794    
795     #if !EMULATED_PPC
796     // Uninstall SIGSEGV handler
797     sigemptyset(&sigsegv_action.sa_mask);
798     sigsegv_action.sa_handler = SIG_DFL;
799     sigsegv_action.sa_flags = 0;
800     sigaction(SIGSEGV, &sigsegv_action, NULL);
801    
802     // Uninstall SIGILL handler
803     sigemptyset(&sigill_action.sa_mask);
804     sigill_action.sa_handler = SIG_DFL;
805     sigill_action.sa_flags = 0;
806     sigaction(SIGILL, &sigill_action, NULL);
807     #endif
808    
809     // Save NVRAM
810     XPRAMExit();
811    
812     // Exit clipboard
813     ClipExit();
814    
815     // Exit Time Manager
816     TimerExit();
817    
818     // Exit serial
819     SerialExit();
820    
821     // Exit network
822     EtherExit();
823    
824     // Exit audio
825     AudioExit();
826    
827     // Exit video
828     VideoExit();
829    
830     // Exit external file system
831     ExtFSExit();
832    
833     // Exit drivers
834     SCSIExit();
835     CDROMExit();
836     DiskExit();
837     SonyExit();
838    
839 gbeauche 1.15 // Delete SheepShaver globals
840     SheepMem::Exit();
841    
842 cebix 1.1 // Delete RAM area
843     if (ram_area_mapped)
844 gbeauche 1.8 vm_release((char *)RAM_BASE, RAMSize);
845 cebix 1.1
846     // Delete ROM area
847     if (rom_area_mapped)
848 gbeauche 1.4 vm_release((char *)ROM_BASE, ROM_AREA_SIZE);
849 cebix 1.1
850     // Delete Kernel Data area
851     if (kernel_area >= 0) {
852     shmdt((void *)KERNEL_DATA_BASE);
853     shmdt((void *)KERNEL_DATA2_BASE);
854     shmctl(kernel_area, IPC_RMID, NULL);
855     }
856    
857     // Delete Low Memory area
858     if (lm_area_mapped)
859     munmap((char *)0x0000, 0x3000);
860    
861     // Close /dev/zero
862     if (zero_fd > 0)
863     close(zero_fd);
864    
865     // Exit system routines
866     SysExit();
867    
868     // Exit preferences
869     PrefsExit();
870    
871     #ifdef ENABLE_MON
872     // Exit mon
873     mon_exit();
874     #endif
875    
876     // Close X11 server connection
877     if (x_display)
878     XCloseDisplay(x_display);
879    
880     exit(0);
881     }
882    
883    
884     /*
885     * Jump into Mac ROM, start 680x0 emulator
886     */
887    
888     #if EMULATED_PPC
889     void jump_to_rom(uint32 entry)
890     {
891     init_emul_ppc();
892     emul_ppc(entry);
893     }
894     #endif
895    
896    
897     /*
898     * Emulator thread function
899     */
900    
901     static void *emul_func(void *arg)
902     {
903     // We're now ready to receive signals
904     ready_for_signals = true;
905    
906     // Decrease priority, so more time-critical things like audio will work better
907     nice(1);
908    
909     // Jump to ROM boot routine
910     D(bug("Jumping to ROM\n"));
911     #if EMULATED_PPC
912     jump_to_rom(ROM_BASE + 0x310000);
913     #else
914     jump_to_rom(ROM_BASE + 0x310000, (uint32)emulator_data);
915     #endif
916     D(bug("Returned from ROM\n"));
917    
918     // We're no longer ready to receive signals
919     ready_for_signals = false;
920     return NULL;
921     }
922    
923    
924     #if !EMULATED_PPC
925     /*
926     * Execute 68k subroutine (must be ended with RTS)
927     * This must only be called by the emul_thread when in EMUL_OP mode
928     * r->a[7] is unused, the routine runs on the caller's stack
929     */
930    
931     void Execute68k(uint32 pc, M68kRegisters *r)
932     {
933     #if SAFE_EXEC_68K
934     if (ReadMacInt32(XLM_RUN_MODE) != MODE_EMUL_OP)
935     printf("FATAL: Execute68k() not called from EMUL_OP mode\n");
936     if (!pthread_equal(pthread_self(), emul_thread))
937     printf("FATAL: Execute68k() not called from emul_thread\n");
938     #endif
939     execute_68k(pc, r);
940     }
941    
942    
943     /*
944     * Execute 68k A-Trap from EMUL_OP routine
945     * r->a[7] is unused, the routine runs on the caller's stack
946     */
947    
948     void Execute68kTrap(uint16 trap, M68kRegisters *r)
949     {
950     uint16 proc[2] = {trap, M68K_RTS};
951     Execute68k((uint32)proc, r);
952     }
953 gbeauche 1.7 #endif
954 cebix 1.1
955    
956     /*
957     * Quit emulator (cause return from jump_to_rom)
958     */
959    
960     void QuitEmulator(void)
961     {
962     #if EMULATED_PPC
963     Quit();
964     #else
965     quit_emulator();
966     #endif
967     }
968    
969    
970     /*
971     * Pause/resume emulator
972     */
973    
974     void PauseEmulator(void)
975     {
976     pthread_kill(emul_thread, SIGSTOP);
977     }
978    
979     void ResumeEmulator(void)
980     {
981     pthread_kill(emul_thread, SIGCONT);
982     }
983    
984    
985     /*
986     * Dump 68k registers
987     */
988    
989     void Dump68kRegs(M68kRegisters *r)
990     {
991     // Display 68k registers
992     for (int i=0; i<8; i++) {
993     printf("d%d: %08x", i, r->d[i]);
994     if (i == 3 || i == 7)
995     printf("\n");
996     else
997     printf(", ");
998     }
999     for (int i=0; i<8; i++) {
1000     printf("a%d: %08x", i, r->a[i]);
1001     if (i == 3 || i == 7)
1002     printf("\n");
1003     else
1004     printf(", ");
1005     }
1006     }
1007    
1008    
1009     /*
1010     * Make code executable
1011     */
1012    
1013     void MakeExecutable(int dummy, void *start, uint32 length)
1014     {
1015 gbeauche 1.9 if (((uintptr)start >= ROM_BASE) && ((uintptr)start < (ROM_BASE + ROM_SIZE)))
1016 cebix 1.1 return;
1017 gbeauche 1.9 #if EMULATED_PPC
1018     FlushCodeCache((uintptr)start, (uintptr)start + length);
1019     #else
1020     flush_icache_range(start, (void *)((uintptr)start + length));
1021 cebix 1.1 #endif
1022     }
1023    
1024    
1025     /*
1026     * Patch things after system startup (gets called by disk driver accRun routine)
1027     */
1028    
1029     void PatchAfterStartup(void)
1030     {
1031 gbeauche 1.6 ExecuteNative(NATIVE_VIDEO_INSTALL_ACCEL);
1032 cebix 1.1 InstallExtFS();
1033     }
1034    
1035    
1036     /*
1037     * NVRAM watchdog thread (saves NVRAM every minute)
1038     */
1039    
1040     static void *nvram_func(void *arg)
1041     {
1042     struct timespec req = {60, 0}; // 1 minute
1043    
1044     for (;;) {
1045     pthread_testcancel();
1046     nanosleep(&req, NULL);
1047     pthread_testcancel();
1048     if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) {
1049     memcpy(last_xpram, XPRAM, XPRAM_SIZE);
1050     SaveXPRAM();
1051     }
1052     }
1053     return NULL;
1054     }
1055    
1056    
1057     /*
1058     * 60Hz thread (really 60.15Hz)
1059     */
1060    
1061     static void *tick_func(void *arg)
1062     {
1063     int tick_counter = 0;
1064     struct timespec req = {0, 16625000};
1065    
1066     for (;;) {
1067    
1068     // Wait
1069     nanosleep(&req, NULL);
1070    
1071     #if !EMULATED_PPC
1072     // Did we crash?
1073     if (emul_thread_fatal) {
1074    
1075     // Yes, dump registers
1076     pt_regs *r = (pt_regs *)&sigsegv_regs;
1077     char str[256];
1078     sprintf(str, "SIGSEGV\n"
1079     " pc %08lx lr %08lx ctr %08lx msr %08lx\n"
1080     " xer %08lx cr %08lx \n"
1081     " r0 %08lx r1 %08lx r2 %08lx r3 %08lx\n"
1082     " r4 %08lx r5 %08lx r6 %08lx r7 %08lx\n"
1083     " r8 %08lx r9 %08lx r10 %08lx r11 %08lx\n"
1084     " r12 %08lx r13 %08lx r14 %08lx r15 %08lx\n"
1085     " r16 %08lx r17 %08lx r18 %08lx r19 %08lx\n"
1086     " r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n"
1087     " r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n"
1088     " r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n",
1089     r->nip, r->link, r->ctr, r->msr,
1090     r->xer, r->ccr,
1091     r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3],
1092     r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7],
1093     r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11],
1094     r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15],
1095     r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19],
1096     r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23],
1097     r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27],
1098     r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]);
1099     printf(str);
1100     VideoQuitFullScreen();
1101    
1102     #ifdef ENABLE_MON
1103     // Start up mon in real-mode
1104     printf("Welcome to the sheep factory.\n");
1105     char *arg[4] = {"mon", "-m", "-r", NULL};
1106     mon(3, arg);
1107     #endif
1108     return NULL;
1109     }
1110     #endif
1111    
1112     // Pseudo Mac 1Hz interrupt, update local time
1113     if (++tick_counter > 60) {
1114     tick_counter = 0;
1115     WriteMacInt32(0x20c, TimerDateTime());
1116     }
1117    
1118     // Trigger 60Hz interrupt
1119     if (ReadMacInt32(XLM_IRQ_NEST) == 0) {
1120     SetInterruptFlag(INTFLAG_VIA);
1121     TriggerInterrupt();
1122     }
1123     }
1124     return NULL;
1125     }
1126    
1127    
1128     /*
1129 cebix 1.2 * Pthread configuration
1130     */
1131    
1132     void Set_pthread_attr(pthread_attr_t *attr, int priority)
1133     {
1134 gbeauche 1.14 #ifdef HAVE_PTHREADS
1135     pthread_attr_init(attr);
1136     #if defined(_POSIX_THREAD_PRIORITY_SCHEDULING)
1137     // Some of these only work for superuser
1138     if (geteuid() == 0) {
1139     pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED);
1140     pthread_attr_setschedpolicy(attr, SCHED_FIFO);
1141     struct sched_param fifo_param;
1142     fifo_param.sched_priority = ((sched_get_priority_min(SCHED_FIFO) +
1143     sched_get_priority_max(SCHED_FIFO)) / 2 +
1144     priority);
1145     pthread_attr_setschedparam(attr, &fifo_param);
1146     }
1147     if (pthread_attr_setscope(attr, PTHREAD_SCOPE_SYSTEM) != 0) {
1148     #ifdef PTHREAD_SCOPE_BOUND_NP
1149     // If system scope is not available (eg. we're not running
1150     // with CAP_SCHED_MGT capability on an SGI box), try bound
1151     // scope. It exposes pthread scheduling to the kernel,
1152     // without setting realtime priority.
1153     pthread_attr_setscope(attr, PTHREAD_SCOPE_BOUND_NP);
1154     #endif
1155     }
1156     #endif
1157     #endif
1158 cebix 1.2 }
1159    
1160    
1161     /*
1162 cebix 1.1 * Mutexes
1163     */
1164    
1165 gbeauche 1.7 #ifdef HAVE_PTHREADS
1166    
1167     struct B2_mutex {
1168     B2_mutex() {
1169     pthread_mutexattr_t attr;
1170     pthread_mutexattr_init(&attr);
1171     // Initialize the mutex for priority inheritance --
1172     // required for accurate timing.
1173     #ifdef HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL
1174     pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
1175     #endif
1176     #if defined(HAVE_PTHREAD_MUTEXATTR_SETTYPE) && defined(PTHREAD_MUTEX_NORMAL)
1177     pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
1178     #endif
1179     #ifdef HAVE_PTHREAD_MUTEXATTR_SETPSHARED
1180     pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE);
1181     #endif
1182     pthread_mutex_init(&m, &attr);
1183     pthread_mutexattr_destroy(&attr);
1184     }
1185     ~B2_mutex() {
1186     pthread_mutex_trylock(&m); // Make sure it's locked before
1187     pthread_mutex_unlock(&m); // unlocking it.
1188     pthread_mutex_destroy(&m);
1189     }
1190     pthread_mutex_t m;
1191     };
1192    
1193     B2_mutex *B2_create_mutex(void)
1194     {
1195     return new B2_mutex;
1196     }
1197    
1198     void B2_lock_mutex(B2_mutex *mutex)
1199     {
1200     pthread_mutex_lock(&mutex->m);
1201     }
1202    
1203     void B2_unlock_mutex(B2_mutex *mutex)
1204     {
1205     pthread_mutex_unlock(&mutex->m);
1206     }
1207    
1208     void B2_delete_mutex(B2_mutex *mutex)
1209     {
1210     delete mutex;
1211     }
1212    
1213     #else
1214    
1215 cebix 1.1 struct B2_mutex {
1216     int dummy;
1217     };
1218    
1219     B2_mutex *B2_create_mutex(void)
1220     {
1221     return new B2_mutex;
1222     }
1223    
1224     void B2_lock_mutex(B2_mutex *mutex)
1225     {
1226     }
1227    
1228     void B2_unlock_mutex(B2_mutex *mutex)
1229     {
1230     }
1231    
1232     void B2_delete_mutex(B2_mutex *mutex)
1233     {
1234     delete mutex;
1235     }
1236    
1237 gbeauche 1.7 #endif
1238    
1239 cebix 1.1
1240     /*
1241     * Trigger signal USR2 from another thread
1242     */
1243    
1244 gbeauche 1.8 #if !EMULATED_PPC || ASYNC_IRQ
1245 cebix 1.1 void TriggerInterrupt(void)
1246     {
1247     if (ready_for_signals)
1248     pthread_kill(emul_thread, SIGUSR2);
1249     }
1250 gbeauche 1.7 #endif
1251 cebix 1.1
1252    
1253     /*
1254     * Interrupt flags (must be handled atomically!)
1255     */
1256    
1257     volatile uint32 InterruptFlags = 0;
1258    
1259     void SetInterruptFlag(uint32 flag)
1260     {
1261     atomic_or((int *)&InterruptFlags, flag);
1262     }
1263    
1264     void ClearInterruptFlag(uint32 flag)
1265     {
1266     atomic_and((int *)&InterruptFlags, ~flag);
1267     }
1268    
1269    
1270     /*
1271     * Disable interrupts
1272     */
1273    
1274     void DisableInterrupt(void)
1275     {
1276 gbeauche 1.7 atomic_add((int *)XLM_IRQ_NEST, 1);
1277 cebix 1.1 }
1278    
1279    
1280     /*
1281     * Enable interrupts
1282     */
1283    
1284     void EnableInterrupt(void)
1285     {
1286 gbeauche 1.7 atomic_add((int *)XLM_IRQ_NEST, -1);
1287 cebix 1.1 }
1288    
1289    
1290     /*
1291     * USR2 handler
1292     */
1293    
1294 gbeauche 1.8 #if EMULATED_PPC
1295     static void sigusr2_handler(int sig)
1296     {
1297     #if ASYNC_IRQ
1298     extern void HandleInterrupt(void);
1299     HandleInterrupt();
1300     #endif
1301     }
1302     #else
1303 cebix 1.1 static void sigusr2_handler(int sig, sigcontext_struct *sc)
1304     {
1305     pt_regs *r = sc->regs;
1306    
1307     // Do nothing if interrupts are disabled
1308     if (*(int32 *)XLM_IRQ_NEST > 0)
1309     return;
1310    
1311     // Disable MacOS stack sniffer
1312     WriteMacInt32(0x110, 0);
1313    
1314     // Interrupt action depends on current run mode
1315     switch (ReadMacInt32(XLM_RUN_MODE)) {
1316     case MODE_68K:
1317     // 68k emulator active, trigger 68k interrupt level 1
1318     WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1);
1319     r->ccr |= ntohl(kernel_data->v[0x674 >> 2]);
1320     break;
1321    
1322     #if INTERRUPTS_IN_NATIVE_MODE
1323     case MODE_NATIVE:
1324     // 68k emulator inactive, in nanokernel?
1325     if (r->gpr[1] != KernelDataAddr) {
1326     // Prepare for 68k interrupt level 1
1327     WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1);
1328     WriteMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc, ReadMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc) | ntohl(kernel_data->v[0x674 >> 2]));
1329    
1330     // Execute nanokernel interrupt routine (this will activate the 68k emulator)
1331     atomic_add((int32 *)XLM_IRQ_NEST, 1);
1332     if (ROMType == ROMTYPE_NEWWORLD)
1333     ppc_interrupt(ROM_BASE + 0x312b1c, KernelDataAddr);
1334     else
1335     ppc_interrupt(ROM_BASE + 0x312a3c, KernelDataAddr);
1336     }
1337     break;
1338     #endif
1339    
1340     #if INTERRUPTS_IN_EMUL_OP_MODE
1341     case MODE_EMUL_OP:
1342     // 68k emulator active, within EMUL_OP routine, execute 68k interrupt routine directly when interrupt level is 0
1343     if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) {
1344    
1345     // Set extra stack for SIGSEGV handler
1346     struct sigaltstack new_stack;
1347     new_stack.ss_sp = extra_stack;
1348     new_stack.ss_flags = 0;
1349     new_stack.ss_size = SIG_STACK_SIZE;
1350     sigaltstack(&new_stack, NULL);
1351     #if 1
1352     // Execute full 68k interrupt routine
1353     M68kRegisters r;
1354     uint32 old_r25 = ReadMacInt32(XLM_68K_R25); // Save interrupt level
1355     WriteMacInt32(XLM_68K_R25, 0x21); // Execute with interrupt level 1
1356     static const uint16 proc[] = {
1357     0x3f3c, 0x0000, // move.w #$0000,-(sp) (fake format word)
1358     0x487a, 0x000a, // pea @1(pc) (return address)
1359     0x40e7, // move sr,-(sp) (saved SR)
1360     0x2078, 0x0064, // move.l $64,a0
1361     0x4ed0, // jmp (a0)
1362     M68K_RTS // @1
1363     };
1364     Execute68k((uint32)proc, &r);
1365     WriteMacInt32(XLM_68K_R25, old_r25); // Restore interrupt level
1366     #else
1367     // Only update cursor
1368     if (HasMacStarted()) {
1369     if (InterruptFlags & INTFLAG_VIA) {
1370     ClearInterruptFlag(INTFLAG_VIA);
1371     ADBInterrupt();
1372 gbeauche 1.17 ExecuteNative(NATIVE_VIDEO_VBL);
1373 cebix 1.1 }
1374     }
1375     #endif
1376     // Reset normal signal stack
1377     new_stack.ss_sp = sig_stack;
1378     new_stack.ss_flags = 0;
1379     new_stack.ss_size = SIG_STACK_SIZE;
1380     sigaltstack(&new_stack, NULL);
1381     }
1382     break;
1383     #endif
1384     }
1385     }
1386 gbeauche 1.8 #endif
1387 cebix 1.1
1388    
1389     /*
1390     * SIGSEGV handler
1391     */
1392    
1393 gbeauche 1.8 #if !EMULATED_PPC
1394 cebix 1.1 static void sigsegv_handler(int sig, sigcontext_struct *sc)
1395     {
1396     pt_regs *r = sc->regs;
1397 gbeauche 1.5
1398     // Get effective address
1399     uint32 addr = r->dar;
1400    
1401     #if ENABLE_VOSF
1402     // Handle screen fault.
1403     extern bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction);
1404     if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->nip))
1405     return;
1406     #endif
1407    
1408 cebix 1.1 num_segv++;
1409    
1410     // Fault in Mac ROM or RAM?
1411     bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize));
1412     if (mac_fault) {
1413    
1414     // "VM settings" during MacOS 8 installation
1415     if (r->nip == ROM_BASE + 0x488160 && r->gpr[20] == 0xf8000000) {
1416     r->nip += 4;
1417     r->gpr[8] = 0;
1418     return;
1419    
1420     // MacOS 8.5 installation
1421     } else if (r->nip == ROM_BASE + 0x488140 && r->gpr[16] == 0xf8000000) {
1422     r->nip += 4;
1423     r->gpr[8] = 0;
1424     return;
1425    
1426     // MacOS 8 serial drivers on startup
1427     } else if (r->nip == ROM_BASE + 0x48e080 && (r->gpr[8] == 0xf3012002 || r->gpr[8] == 0xf3012000)) {
1428     r->nip += 4;
1429     r->gpr[8] = 0;
1430     return;
1431    
1432     // MacOS 8.1 serial drivers on startup
1433     } else if (r->nip == ROM_BASE + 0x48c5e0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) {
1434     r->nip += 4;
1435     return;
1436     } else if (r->nip == ROM_BASE + 0x4a10a0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) {
1437     r->nip += 4;
1438     return;
1439     }
1440    
1441 gbeauche 1.5 // Get opcode and divide into fields
1442     uint32 opcode = *((uint32 *)r->nip);
1443     uint32 primop = opcode >> 26;
1444     uint32 exop = (opcode >> 1) & 0x3ff;
1445     uint32 ra = (opcode >> 16) & 0x1f;
1446     uint32 rb = (opcode >> 11) & 0x1f;
1447     uint32 rd = (opcode >> 21) & 0x1f;
1448     int32 imm = (int16)(opcode & 0xffff);
1449    
1450 cebix 1.1 // Analyze opcode
1451     enum {
1452     TYPE_UNKNOWN,
1453     TYPE_LOAD,
1454     TYPE_STORE
1455     } transfer_type = TYPE_UNKNOWN;
1456     enum {
1457     SIZE_UNKNOWN,
1458     SIZE_BYTE,
1459     SIZE_HALFWORD,
1460     SIZE_WORD
1461     } transfer_size = SIZE_UNKNOWN;
1462     enum {
1463     MODE_UNKNOWN,
1464     MODE_NORM,
1465     MODE_U,
1466     MODE_X,
1467     MODE_UX
1468     } addr_mode = MODE_UNKNOWN;
1469     switch (primop) {
1470     case 31:
1471     switch (exop) {
1472     case 23: // lwzx
1473     transfer_type = TYPE_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_X; break;
1474     case 55: // lwzux
1475     transfer_type = TYPE_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_UX; break;
1476     case 87: // lbzx
1477     transfer_type = TYPE_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_X; break;
1478     case 119: // lbzux
1479     transfer_type = TYPE_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_UX; break;
1480     case 151: // stwx
1481     transfer_type = TYPE_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_X; break;
1482     case 183: // stwux
1483     transfer_type = TYPE_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_UX; break;
1484     case 215: // stbx
1485     transfer_type = TYPE_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_X; break;
1486     case 247: // stbux
1487     transfer_type = TYPE_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_UX; break;
1488     case 279: // lhzx
1489     transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_X; break;
1490     case 311: // lhzux
1491     transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_UX; break;
1492     case 343: // lhax
1493     transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_X; break;
1494     case 375: // lhaux
1495     transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_UX; break;
1496     case 407: // sthx
1497     transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_X; break;
1498     case 439: // sthux
1499     transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_UX; break;
1500     }
1501     break;
1502    
1503     case 32: // lwz
1504     transfer_type = TYPE_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_NORM; break;
1505     case 33: // lwzu
1506     transfer_type = TYPE_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_U; break;
1507     case 34: // lbz
1508     transfer_type = TYPE_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_NORM; break;
1509     case 35: // lbzu
1510     transfer_type = TYPE_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_U; break;
1511     case 36: // stw
1512     transfer_type = TYPE_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_NORM; break;
1513     case 37: // stwu
1514     transfer_type = TYPE_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_U; break;
1515     case 38: // stb
1516     transfer_type = TYPE_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_NORM; break;
1517     case 39: // stbu
1518     transfer_type = TYPE_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_U; break;
1519     case 40: // lhz
1520     transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_NORM; break;
1521     case 41: // lhzu
1522     transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_U; break;
1523     case 42: // lha
1524     transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_NORM; break;
1525     case 43: // lhau
1526     transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_U; break;
1527     case 44: // sth
1528     transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_NORM; break;
1529     case 45: // sthu
1530     transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_U; break;
1531     }
1532    
1533     // Ignore ROM writes
1534     if (transfer_type == TYPE_STORE && addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) {
1535     // 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->nip));
1536     if (addr_mode == MODE_U || addr_mode == MODE_UX)
1537     r->gpr[ra] = addr;
1538     r->nip += 4;
1539     goto rti;
1540     }
1541    
1542     // Ignore illegal memory accesses?
1543     if (PrefsFindBool("ignoresegv")) {
1544     if (addr_mode == MODE_U || addr_mode == MODE_UX)
1545     r->gpr[ra] = addr;
1546     if (transfer_type == TYPE_LOAD)
1547     r->gpr[rd] = 0;
1548     r->nip += 4;
1549     goto rti;
1550     }
1551    
1552     // In GUI mode, show error alert
1553     if (!PrefsFindBool("nogui")) {
1554     char str[256];
1555     if (transfer_type == TYPE_LOAD || transfer_type == TYPE_STORE)
1556     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->nip, r->gpr[24], r->gpr[1]);
1557     else
1558     sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode);
1559     ErrorAlert(str);
1560     QuitEmulator();
1561     return;
1562     }
1563     }
1564    
1565     // For all other errors, jump into debugger (sort of...)
1566     if (!ready_for_signals) {
1567     printf("SIGSEGV\n");
1568     printf(" sigcontext %p, pt_regs %p\n", sc, r);
1569     printf(
1570     " pc %08lx lr %08lx ctr %08lx msr %08lx\n"
1571     " xer %08lx cr %08lx \n"
1572     " r0 %08lx r1 %08lx r2 %08lx r3 %08lx\n"
1573     " r4 %08lx r5 %08lx r6 %08lx r7 %08lx\n"
1574     " r8 %08lx r9 %08lx r10 %08lx r11 %08lx\n"
1575     " r12 %08lx r13 %08lx r14 %08lx r15 %08lx\n"
1576     " r16 %08lx r17 %08lx r18 %08lx r19 %08lx\n"
1577     " r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n"
1578     " r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n"
1579     " r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n",
1580     r->nip, r->link, r->ctr, r->msr,
1581     r->xer, r->ccr,
1582     r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3],
1583     r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7],
1584     r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11],
1585     r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15],
1586     r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19],
1587     r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23],
1588     r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27],
1589     r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]);
1590     exit(1);
1591     QuitEmulator();
1592     return;
1593     } else {
1594     // We crashed. Save registers, tell tick thread and loop forever
1595     sigsegv_regs = *(sigregs *)r;
1596     emul_thread_fatal = true;
1597     for (;;) ;
1598     }
1599     rti:;
1600     }
1601    
1602    
1603     /*
1604     * SIGILL handler
1605     */
1606    
1607     static void sigill_handler(int sig, sigcontext_struct *sc)
1608     {
1609     pt_regs *r = sc->regs;
1610     char str[256];
1611    
1612     // Fault in Mac ROM or RAM?
1613     bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize));
1614     if (mac_fault) {
1615    
1616     // Get opcode and divide into fields
1617     uint32 opcode = *((uint32 *)r->nip);
1618     uint32 primop = opcode >> 26;
1619     uint32 exop = (opcode >> 1) & 0x3ff;
1620     uint32 ra = (opcode >> 16) & 0x1f;
1621     uint32 rb = (opcode >> 11) & 0x1f;
1622     uint32 rd = (opcode >> 21) & 0x1f;
1623     int32 imm = (int16)(opcode & 0xffff);
1624    
1625     switch (primop) {
1626     case 9: // POWER instructions
1627     case 22:
1628     power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->nip, r->gpr[1], opcode);
1629     ErrorAlert(str);
1630     QuitEmulator();
1631     return;
1632    
1633     case 31:
1634     switch (exop) {
1635     case 83: // mfmsr
1636     r->gpr[rd] = 0xf072;
1637     r->nip += 4;
1638     goto rti;
1639    
1640     case 210: // mtsr
1641     case 242: // mtsrin
1642     case 306: // tlbie
1643     r->nip += 4;
1644     goto rti;
1645    
1646     case 339: { // mfspr
1647     int spr = ra | (rb << 5);
1648     switch (spr) {
1649     case 0: // MQ
1650     case 22: // DEC
1651     case 952: // MMCR0
1652     case 953: // PMC1
1653     case 954: // PMC2
1654     case 955: // SIA
1655     case 956: // MMCR1
1656     case 957: // PMC3
1657     case 958: // PMC4
1658     case 959: // SDA
1659     r->nip += 4;
1660     goto rti;
1661     case 25: // SDR1
1662     r->gpr[rd] = 0xdead001f;
1663     r->nip += 4;
1664     goto rti;
1665     case 287: // PVR
1666     r->gpr[rd] = PVR;
1667     r->nip += 4;
1668     goto rti;
1669     }
1670     break;
1671     }
1672    
1673     case 467: { // mtspr
1674     int spr = ra | (rb << 5);
1675     switch (spr) {
1676     case 0: // MQ
1677     case 22: // DEC
1678     case 275: // SPRG3
1679     case 528: // IBAT0U
1680     case 529: // IBAT0L
1681     case 530: // IBAT1U
1682     case 531: // IBAT1L
1683     case 532: // IBAT2U
1684     case 533: // IBAT2L
1685     case 534: // IBAT3U
1686     case 535: // IBAT3L
1687     case 536: // DBAT0U
1688     case 537: // DBAT0L
1689     case 538: // DBAT1U
1690     case 539: // DBAT1L
1691     case 540: // DBAT2U
1692     case 541: // DBAT2L
1693     case 542: // DBAT3U
1694     case 543: // DBAT3L
1695     case 952: // MMCR0
1696     case 953: // PMC1
1697     case 954: // PMC2
1698     case 955: // SIA
1699     case 956: // MMCR1
1700     case 957: // PMC3
1701     case 958: // PMC4
1702     case 959: // SDA
1703     r->nip += 4;
1704     goto rti;
1705     }
1706     break;
1707     }
1708    
1709     case 29: case 107: case 152: case 153: // POWER instructions
1710     case 184: case 216: case 217: case 248:
1711     case 264: case 277: case 331: case 360:
1712     case 363: case 488: case 531: case 537:
1713     case 541: case 664: case 665: case 696:
1714     case 728: case 729: case 760: case 920:
1715     case 921: case 952:
1716     goto power_inst;
1717     }
1718     }
1719    
1720     // In GUI mode, show error alert
1721     if (!PrefsFindBool("nogui")) {
1722     sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode);
1723     ErrorAlert(str);
1724     QuitEmulator();
1725     return;
1726     }
1727     }
1728    
1729     // For all other errors, jump into debugger (sort of...)
1730     if (!ready_for_signals) {
1731     printf("SIGILL\n");
1732     printf(" sigcontext %p, pt_regs %p\n", sc, r);
1733     printf(
1734     " pc %08lx lr %08lx ctr %08lx msr %08lx\n"
1735     " xer %08lx cr %08lx \n"
1736     " r0 %08lx r1 %08lx r2 %08lx r3 %08lx\n"
1737     " r4 %08lx r5 %08lx r6 %08lx r7 %08lx\n"
1738     " r8 %08lx r9 %08lx r10 %08lx r11 %08lx\n"
1739     " r12 %08lx r13 %08lx r14 %08lx r15 %08lx\n"
1740     " r16 %08lx r17 %08lx r18 %08lx r19 %08lx\n"
1741     " r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n"
1742     " r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n"
1743     " r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n",
1744     r->nip, r->link, r->ctr, r->msr,
1745     r->xer, r->ccr,
1746     r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3],
1747     r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7],
1748     r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11],
1749     r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15],
1750     r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19],
1751     r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23],
1752     r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27],
1753     r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]);
1754     exit(1);
1755     QuitEmulator();
1756     return;
1757     } else {
1758     // We crashed. Save registers, tell tick thread and loop forever
1759     sigsegv_regs = *(sigregs *)r;
1760     emul_thread_fatal = true;
1761     for (;;) ;
1762     }
1763     rti:;
1764     }
1765     #endif
1766 gbeauche 1.15
1767    
1768     /*
1769     * Helpers to share 32-bit addressable data with MacOS
1770     */
1771    
1772     bool SheepMem::Init(void)
1773     {
1774     if (vm_acquire_fixed((char *)base, size) < 0)
1775     return false;
1776     top = base + size;
1777     return true;
1778     }
1779    
1780     void SheepMem::Exit(void)
1781     {
1782     if (top)
1783     vm_release((void *)base, size);
1784     }
1785 cebix 1.1
1786    
1787     /*
1788     * Display alert
1789     */
1790    
1791     #ifdef ENABLE_GTK
1792     static void dl_destroyed(void)
1793     {
1794     gtk_main_quit();
1795     }
1796    
1797     static void dl_quit(GtkWidget *dialog)
1798     {
1799     gtk_widget_destroy(dialog);
1800     }
1801    
1802     void display_alert(int title_id, int prefix_id, int button_id, const char *text)
1803     {
1804     char str[256];
1805     sprintf(str, GetString(prefix_id), text);
1806    
1807     GtkWidget *dialog = gtk_dialog_new();
1808     gtk_window_set_title(GTK_WINDOW(dialog), GetString(title_id));
1809     gtk_container_border_width(GTK_CONTAINER(dialog), 5);
1810     gtk_widget_set_uposition(GTK_WIDGET(dialog), 100, 150);
1811     gtk_signal_connect(GTK_OBJECT(dialog), "destroy", GTK_SIGNAL_FUNC(dl_destroyed), NULL);
1812    
1813     GtkWidget *label = gtk_label_new(str);
1814     gtk_widget_show(label);
1815     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), label, TRUE, TRUE, 0);
1816    
1817     GtkWidget *button = gtk_button_new_with_label(GetString(button_id));
1818     gtk_widget_show(button);
1819     gtk_signal_connect_object(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(dl_quit), GTK_OBJECT(dialog));
1820     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area), button, FALSE, FALSE, 0);
1821     GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1822     gtk_widget_grab_default(button);
1823     gtk_widget_show(dialog);
1824    
1825     gtk_main();
1826     }
1827     #endif
1828    
1829    
1830     /*
1831     * Display error alert
1832     */
1833    
1834     void ErrorAlert(const char *text)
1835     {
1836     #ifdef ENABLE_GTK
1837     if (PrefsFindBool("nogui") || x_display == NULL) {
1838     printf(GetString(STR_SHELL_ERROR_PREFIX), text);
1839     return;
1840     }
1841     VideoQuitFullScreen();
1842     display_alert(STR_ERROR_ALERT_TITLE, STR_GUI_ERROR_PREFIX, STR_QUIT_BUTTON, text);
1843     #else
1844     printf(GetString(STR_SHELL_ERROR_PREFIX), text);
1845     #endif
1846     }
1847    
1848    
1849     /*
1850     * Display warning alert
1851     */
1852    
1853     void WarningAlert(const char *text)
1854     {
1855     #ifdef ENABLE_GTK
1856     if (PrefsFindBool("nogui") || x_display == NULL) {
1857     printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1858     return;
1859     }
1860     display_alert(STR_WARNING_ALERT_TITLE, STR_GUI_WARNING_PREFIX, STR_OK_BUTTON, text);
1861     #else
1862     printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1863     #endif
1864     }
1865    
1866    
1867     /*
1868     * Display choice alert
1869     */
1870    
1871     bool ChoiceAlert(const char *text, const char *pos, const char *neg)
1872     {
1873     printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1874     return false; //!!
1875     }