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