ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/main_unix.cpp
Revision: 1.76
Committed: 2006-05-09T19:53:31Z (18 years, 6 months ago) by gbeauche
Branch: MAIN
Changes since 1.75: +4 -0 lines
Log Message:
Don't let SDL catch SIGINT and SIGTERM signals. This is not suitable for
SheepShaver since we are typically translating SDL_QUIT events to PowerOff()
on MacOS side. And, if MacOS is not fully booted, it's not really convenient
to shut it down, even with ctrl-C. i.e. you had to kill -9 it.

File Contents

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