ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/main_unix.cpp
Revision: 1.83
Committed: 2009-02-11T20:44:49Z (15 years, 3 months ago) by asvitkine
Branch: MAIN
Changes since 1.82: +0 -9 lines
Log Message:
don't re-declare sigsegv_info_t, instead use the one from the header

File Contents

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