ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/main_unix.cpp
Revision: 1.74
Committed: 2006-05-01T22:33:34Z (18 years, 6 months ago) by gbeauche
Branch: MAIN
Changes since 1.73: +70 -15 lines
Log Message:
Port --enable-standalone-gui support to SheepShaver

Others changes include:
- Factor out STR_SIG_INSTALL_ERR messages
- Process command line arguments early (prior to calling PrefsInit())
- GUI: set start_clicked only if the "Start" button was clicked
- GUI: save changes to the "Input" pane when the "Start" button was clicked

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