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