1 |
|
/* |
2 |
|
* main_unix.cpp - Emulation core, Unix implementation |
3 |
|
* |
4 |
< |
* SheepShaver (C) 1997-2002 Christian Bauer and Marc Hellwig |
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 |
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 no TOC under Linux; r2 is free for the user |
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 |
< |
* - As there is no TOC, there are also no TVECTs under Linux; |
37 |
< |
* function pointers point directly to the function code |
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 |
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 |
|
*/ |
102 |
|
#include "xpram.h" |
103 |
|
#include "timer.h" |
104 |
|
#include "adb.h" |
96 |
– |
#include "sony.h" |
97 |
– |
#include "disk.h" |
98 |
– |
#include "cdrom.h" |
99 |
– |
#include "scsi.h" |
105 |
|
#include "video.h" |
101 |
– |
#include "audio.h" |
102 |
– |
#include "ether.h" |
103 |
– |
#include "serial.h" |
104 |
– |
#include "clip.h" |
105 |
– |
#include "extfs.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> |
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 |
|
|
160 |
|
const char ROM_FILE_NAME[] = "ROM"; |
161 |
|
const char ROM_FILE_NAME2[] = "Mac OS ROM"; |
162 |
|
|
163 |
< |
const uint32 RAM_BASE = 0x20000000; // Base address of RAM |
164 |
< |
const uint32 SIG_STACK_SIZE = 0x10000; // Size of signal stack |
165 |
< |
|
166 |
< |
|
167 |
< |
#if !EMULATED_PPC |
153 |
< |
// Structure in which registers are saved in a signal handler; |
154 |
< |
// sigcontext->regs points to it |
155 |
< |
// (see arch/ppc/kernel/signal.c) |
156 |
< |
typedef struct { |
157 |
< |
uint32 u[4]; |
158 |
< |
} __attribute((aligned(16))) vector128; |
159 |
< |
#include <linux/elf.h> |
160 |
< |
|
161 |
< |
struct sigregs { |
162 |
< |
elf_gregset_t gp_regs; // Identical to pt_regs |
163 |
< |
double fp_regs[ELF_NFPREG]; // f0..f31 and fpsrc |
164 |
< |
//more (uninteresting) stuff following here |
165 |
< |
}; |
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; // Small data pointer (r13) |
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 |
175 |
– |
uint32 SheepStack1Base; // SheepShaver first alternate stack base |
176 |
– |
uint32 SheepStack2Base; // SheepShaver second alternate stack base |
177 |
– |
uint32 SheepThunksBase; // SheepShaver thunks base |
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 |
190 |
– |
static bool sheep_area_mapped = false; // Flag: SheepShaver data area mmap()ed |
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 |
|
|
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 |
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 void *sig_stack = NULL; // Stack for signal handlers |
229 |
< |
static void *extra_stack = NULL; // Stack for SIGSEGV inside interrupt handler |
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 |
226 |
– |
static void sigusr2_handler(int sig); |
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 |
< |
static void sigusr2_handler(int sig, sigcontext_struct *sc); |
259 |
< |
static void sigsegv_handler(int sig, sigcontext_struct *sc); |
260 |
< |
static void sigill_handler(int sig, sigcontext_struct *sc); |
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 |
239 |
– |
extern "C" void *get_toc(void); |
267 |
|
extern "C" void *get_sp(void); |
268 |
< |
extern "C" void flush_icache_range(void *start, void *end); |
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); |
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 |
|
|
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 |
|
|
363 |
|
int main(int argc, char **argv) |
364 |
|
{ |
365 |
|
char str[256]; |
310 |
– |
uint32 *boot_globs; |
311 |
– |
int16 i16; |
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; |
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_toc(); |
385 |
> |
TOC = get_r2(); |
386 |
> |
#endif |
387 |
> |
#ifdef SYSTEM_CLOBBERS_R13 |
388 |
> |
// Get r13 register |
389 |
> |
R13 = get_r13(); |
390 |
|
#endif |
331 |
– |
|
332 |
– |
#ifdef ENABLE_GTK |
333 |
– |
// Init GTK |
334 |
– |
gtk_set_locale(); |
335 |
– |
gtk_init(&argc, &argv); |
391 |
|
#endif |
337 |
– |
|
338 |
– |
// Read preferences |
339 |
– |
PrefsInit(argc, argv); |
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 |
< |
} else if (argv[i][0] == '-') { |
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) { |
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 |
< |
#if !EMULATED_PPC |
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 |
655 |
|
// Parse line |
656 |
|
int i; |
657 |
|
char value[256]; |
658 |
< |
if (sscanf(line, "cpu : %s", value) == 1) { |
659 |
< |
if (strcmp(value, "601") == 0) |
660 |
< |
PVR = 0x00010000; |
661 |
< |
else if (strcmp(value, "603") == 0) |
662 |
< |
PVR = 0x00030000; |
663 |
< |
else if (strcmp(value, "604") == 0) |
664 |
< |
PVR = 0x00040000; |
665 |
< |
else if (strcmp(value, "603e") == 0) |
666 |
< |
PVR = 0x00060000; |
667 |
< |
else if (strcmp(value, "603ev") == 0) |
668 |
< |
PVR = 0x00070000; |
403 |
< |
else if (strcmp(value, "604e") == 0) |
404 |
< |
PVR = 0x00090000; |
405 |
< |
else if (strcmp(value, "604ev5") == 0) |
406 |
< |
PVR = 0x000a0000; |
407 |
< |
else if (strcmp(value, "750") == 0) |
408 |
< |
PVR = 0x00080000; |
409 |
< |
else if (strcmp(value, "821") == 0) |
410 |
< |
PVR = 0x00320000; |
411 |
< |
else if (strcmp(value, "860") == 0) |
412 |
< |
PVR = 0x00500000; |
413 |
< |
else |
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; |
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 |
744 |
|
goto quit; |
745 |
|
} |
746 |
|
|
448 |
– |
// Create Low Memory area (0x0000..0x3000) |
449 |
– |
if (vm_acquire_fixed((char *)0, 0x3000) < 0) { |
450 |
– |
sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno)); |
451 |
– |
ErrorAlert(str); |
452 |
– |
goto quit; |
453 |
– |
} |
454 |
– |
lm_area_mapped = true; |
455 |
– |
|
747 |
|
// Create areas for Kernel Data |
748 |
< |
kernel_area = shmget(IPC_PRIVATE, KERNEL_AREA_SIZE, 0600); |
749 |
< |
if (kernel_area == -1) { |
750 |
< |
sprintf(str, GetString(STR_KD_SHMGET_ERR), strerror(errno)); |
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 |
< |
if (shmat(kernel_area, (void *)KERNEL_DATA_BASE, 0) < 0) { |
763 |
< |
sprintf(str, GetString(STR_KD_SHMAT_ERR), strerror(errno)); |
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 |
< |
if (shmat(kernel_area, (void *)KERNEL_DATA2_BASE, 0) < 0) { |
769 |
< |
sprintf(str, GetString(STR_KD2_SHMAT_ERR), strerror(errno)); |
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 |
< |
kernel_data = (KernelData *)0x68ffe000; |
776 |
< |
emulator_data = &kernel_data->ed; |
777 |
< |
KernelDataAddr = (uint32)kernel_data; |
476 |
< |
D(bug("Kernel Data at %p, Emulator Data at %p\n", kernel_data, emulator_data)); |
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 (vm_acquire_fixed((char *)SHEEP_BASE, SHEEP_SIZE) < 0) { |
780 |
> |
if (!SheepMem::Init()) { |
781 |
|
sprintf(str, GetString(STR_SHEEP_MEM_MMAP_ERR), strerror(errno)); |
782 |
|
ErrorAlert(str); |
783 |
|
goto quit; |
784 |
|
} |
484 |
– |
SheepStack1Base = SHEEP_BASE + 0x10000; |
485 |
– |
SheepStack2Base = SheepStack1Base + 0x10000; |
486 |
– |
SheepThunksBase = SheepStack2Base + 0x1000; |
487 |
– |
sheep_area_mapped = true; |
785 |
|
|
786 |
|
// Create area for Mac ROM |
787 |
< |
if (vm_acquire_fixed((char *)ROM_BASE, ROM_AREA_SIZE) < 0) { |
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 |
< |
#if !EMULATED_PPC || defined(__powerpc__) |
793 |
< |
if (vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
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 %08x\n", ROM_BASE)); |
801 |
> |
D(bug("ROM area at %p (%08x)\n", ROMBaseHost, ROM_BASE)); |
802 |
|
|
803 |
|
// Create area for Mac RAM |
804 |
|
RAMSize = PrefsFindInt32("ramsize"); |
806 |
|
WarningAlert(GetString(STR_SMALL_RAM_WARN)); |
807 |
|
RAMSize = 8*1024*1024; |
808 |
|
} |
809 |
< |
|
810 |
< |
if (vm_acquire_fixed((char *)RAM_BASE, RAMSize) < 0) { |
811 |
< |
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
812 |
< |
ErrorAlert(str); |
813 |
< |
goto quit; |
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((char *)RAM_BASE, RAMSize, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
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 |
524 |
– |
RAMBase = RAM_BASE; |
842 |
|
ram_area_mapped = true; |
843 |
< |
D(bug("RAM area at %08x\n", RAMBase)); |
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)); |
876 |
|
} |
877 |
|
delete[] rom_tmp; |
878 |
|
|
879 |
< |
// Load NVRAM |
880 |
< |
XPRAMInit(); |
564 |
< |
|
565 |
< |
// Set boot volume |
566 |
< |
i16 = PrefsFindInt32("bootdrive"); |
567 |
< |
XPRAM[0x1378] = i16 >> 8; |
568 |
< |
XPRAM[0x1379] = i16 & 0xff; |
569 |
< |
i16 = PrefsFindInt32("bootdriver"); |
570 |
< |
XPRAM[0x137a] = i16 >> 8; |
571 |
< |
XPRAM[0x137b] = i16 & 0xff; |
572 |
< |
|
573 |
< |
// Create BootGlobs at top of Mac memory |
574 |
< |
memset((void *)(RAMBase + RAMSize - 4096), 0, 4096); |
575 |
< |
BootGlobsAddr = RAMBase + RAMSize - 0x1c; |
576 |
< |
boot_globs = (uint32 *)BootGlobsAddr; |
577 |
< |
boot_globs[-5] = htonl(RAMBase + RAMSize); // MemTop |
578 |
< |
boot_globs[0] = htonl(RAMBase); // First RAM bank |
579 |
< |
boot_globs[1] = htonl(RAMSize); |
580 |
< |
boot_globs[2] = htonl((uint32)-1); // End of bank table |
581 |
< |
|
582 |
< |
// Init drivers |
583 |
< |
SonyInit(); |
584 |
< |
DiskInit(); |
585 |
< |
CDROMInit(); |
586 |
< |
SCSIInit(); |
587 |
< |
|
588 |
< |
// Init external file system |
589 |
< |
ExtFSInit(); |
590 |
< |
|
591 |
< |
// Init audio |
592 |
< |
AudioInit(); |
593 |
< |
|
594 |
< |
// Init network |
595 |
< |
EtherInit(); |
596 |
< |
|
597 |
< |
// Init serial ports |
598 |
< |
SerialInit(); |
599 |
< |
|
600 |
< |
// Init Time Manager |
601 |
< |
TimerInit(); |
602 |
< |
|
603 |
< |
// Init clipboard |
604 |
< |
ClipInit(); |
605 |
< |
|
606 |
< |
// Init video |
607 |
< |
if (!VideoInit()) |
608 |
< |
goto quit; |
609 |
< |
|
610 |
< |
// Install ROM patches |
611 |
< |
if (!PatchROM()) { |
612 |
< |
ErrorAlert(GetString(STR_UNSUPPORTED_ROM_TYPE_ERR)); |
879 |
> |
// Initialize everything |
880 |
> |
if (!InitAll()) |
881 |
|
goto quit; |
882 |
< |
} |
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 |
< |
MakeExecutable(0, (void *)ROM_BASE, ROM_AREA_SIZE); |
619 |
< |
#endif |
620 |
< |
vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_EXECUTE); |
621 |
< |
|
622 |
< |
// Initialize Kernel Data |
623 |
< |
memset(kernel_data, 0, sizeof(KernelData)); |
624 |
< |
if (ROMType == ROMTYPE_NEWWORLD) { |
625 |
< |
static uint32 of_dev_tree[4] = {0, 0, 0, 0}; |
626 |
< |
static uint8 vector_lookup_tbl[128]; |
627 |
< |
static uint8 vector_mask_tbl[64]; |
628 |
< |
memset((uint8 *)kernel_data + 0xb80, 0x3d, 0x80); |
629 |
< |
memset(vector_lookup_tbl, 0, 128); |
630 |
< |
memset(vector_mask_tbl, 0, 64); |
631 |
< |
kernel_data->v[0xb80 >> 2] = htonl(ROM_BASE); |
632 |
< |
kernel_data->v[0xb84 >> 2] = htonl((uint32)of_dev_tree); // OF device tree base |
633 |
< |
kernel_data->v[0xb90 >> 2] = htonl((uint32)vector_lookup_tbl); |
634 |
< |
kernel_data->v[0xb94 >> 2] = htonl((uint32)vector_mask_tbl); |
635 |
< |
kernel_data->v[0xb98 >> 2] = htonl(ROM_BASE); // OpenPIC base |
636 |
< |
kernel_data->v[0xbb0 >> 2] = htonl(0); // ADB base |
637 |
< |
kernel_data->v[0xc20 >> 2] = htonl(RAMSize); |
638 |
< |
kernel_data->v[0xc24 >> 2] = htonl(RAMSize); |
639 |
< |
kernel_data->v[0xc30 >> 2] = htonl(RAMSize); |
640 |
< |
kernel_data->v[0xc34 >> 2] = htonl(RAMSize); |
641 |
< |
kernel_data->v[0xc38 >> 2] = htonl(0x00010020); |
642 |
< |
kernel_data->v[0xc3c >> 2] = htonl(0x00200001); |
643 |
< |
kernel_data->v[0xc40 >> 2] = htonl(0x00010000); |
644 |
< |
kernel_data->v[0xc50 >> 2] = htonl(RAMBase); |
645 |
< |
kernel_data->v[0xc54 >> 2] = htonl(RAMSize); |
646 |
< |
kernel_data->v[0xf60 >> 2] = htonl(PVR); |
647 |
< |
kernel_data->v[0xf64 >> 2] = htonl(CPUClockSpeed); |
648 |
< |
kernel_data->v[0xf68 >> 2] = htonl(BusClockSpeed); |
649 |
< |
kernel_data->v[0xf6c >> 2] = htonl(CPUClockSpeed); |
650 |
< |
} else { |
651 |
< |
kernel_data->v[0xc80 >> 2] = htonl(RAMSize); |
652 |
< |
kernel_data->v[0xc84 >> 2] = htonl(RAMSize); |
653 |
< |
kernel_data->v[0xc90 >> 2] = htonl(RAMSize); |
654 |
< |
kernel_data->v[0xc94 >> 2] = htonl(RAMSize); |
655 |
< |
kernel_data->v[0xc98 >> 2] = htonl(0x00010020); |
656 |
< |
kernel_data->v[0xc9c >> 2] = htonl(0x00200001); |
657 |
< |
kernel_data->v[0xca0 >> 2] = htonl(0x00010000); |
658 |
< |
kernel_data->v[0xcb0 >> 2] = htonl(RAMBase); |
659 |
< |
kernel_data->v[0xcb4 >> 2] = htonl(RAMSize); |
660 |
< |
kernel_data->v[0xf80 >> 2] = htonl(PVR); |
661 |
< |
kernel_data->v[0xf84 >> 2] = htonl(CPUClockSpeed); |
662 |
< |
kernel_data->v[0xf88 >> 2] = htonl(BusClockSpeed); |
663 |
< |
kernel_data->v[0xf8c >> 2] = htonl(CPUClockSpeed); |
664 |
< |
} |
665 |
< |
|
666 |
< |
// Initialize extra low memory |
667 |
< |
D(bug("Initializing Low Memory...\n")); |
668 |
< |
memset(NULL, 0, 0x3000); |
669 |
< |
WriteMacInt32(XLM_SIGNATURE, FOURCC('B','a','a','h')); // Signature to detect SheepShaver |
670 |
< |
WriteMacInt32(XLM_KERNEL_DATA, (uint32)kernel_data); // For trap replacement routines |
671 |
< |
WriteMacInt32(XLM_PVR, PVR); // Theoretical PVR |
672 |
< |
WriteMacInt32(XLM_BUS_CLOCK, BusClockSpeed); // For DriverServicesLib patch |
673 |
< |
WriteMacInt16(XLM_EXEC_RETURN_OPCODE, M68K_EXEC_RETURN); // For Execute68k() (RTS from the executed 68k code will jump here and end 68k mode) |
674 |
< |
#if EMULATED_PPC |
675 |
< |
WriteMacInt32(XLM_ETHER_INIT, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_INIT)); |
676 |
< |
WriteMacInt32(XLM_ETHER_TERM, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_TERM)); |
677 |
< |
WriteMacInt32(XLM_ETHER_OPEN, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_OPEN)); |
678 |
< |
WriteMacInt32(XLM_ETHER_CLOSE, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_CLOSE)); |
679 |
< |
WriteMacInt32(XLM_ETHER_WPUT, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_WPUT)); |
680 |
< |
WriteMacInt32(XLM_ETHER_RSRV, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_RSRV)); |
681 |
< |
WriteMacInt32(XLM_VIDEO_DOIO, POWERPC_NATIVE_OP_FUNC(NATIVE_VIDEO_DO_DRIVER_IO)); |
682 |
< |
#else |
683 |
< |
WriteMacInt32(XLM_TOC, (uint32)TOC); // TOC pointer of emulator |
684 |
< |
WriteMacInt32(XLM_ETHER_INIT, (uint32)InitStreamModule); // DLPI ethernet driver functions |
685 |
< |
WriteMacInt32(XLM_ETHER_TERM, (uint32)TerminateStreamModule); |
686 |
< |
WriteMacInt32(XLM_ETHER_OPEN, (uint32)ether_open); |
687 |
< |
WriteMacInt32(XLM_ETHER_CLOSE, (uint32)ether_close); |
688 |
< |
WriteMacInt32(XLM_ETHER_WPUT, (uint32)ether_wput); |
689 |
< |
WriteMacInt32(XLM_ETHER_RSRV, (uint32)ether_rsrv); |
690 |
< |
WriteMacInt32(XLM_VIDEO_DOIO, (uint32)VideoDoDriverIO); |
886 |
> |
flush_icache_range(ROM_BASE, ROM_BASE + ROM_AREA_SIZE); |
887 |
|
#endif |
888 |
< |
D(bug("Low Memory initialized\n")); |
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 |
704 |
– |
// Create and install stacks for signal handlers |
705 |
– |
sig_stack = malloc(SIG_STACK_SIZE); |
706 |
– |
D(bug("Signal stack at %p\n", sig_stack)); |
707 |
– |
if (sig_stack == NULL) { |
708 |
– |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
709 |
– |
goto quit; |
710 |
– |
} |
711 |
– |
extra_stack = malloc(SIG_STACK_SIZE); |
712 |
– |
D(bug("Extra stack at %p\n", extra_stack)); |
713 |
– |
if (extra_stack == NULL) { |
714 |
– |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
715 |
– |
goto quit; |
716 |
– |
} |
717 |
– |
struct sigaltstack new_stack; |
718 |
– |
new_stack.ss_sp = sig_stack; |
719 |
– |
new_stack.ss_flags = 0; |
720 |
– |
new_stack.ss_size = SIG_STACK_SIZE; |
721 |
– |
if (sigaltstack(&new_stack, NULL) < 0) { |
722 |
– |
sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno)); |
723 |
– |
ErrorAlert(str); |
724 |
– |
goto quit; |
725 |
– |
} |
726 |
– |
#endif |
727 |
– |
|
728 |
– |
#if !EMULATED_PPC |
729 |
– |
// Install SIGSEGV handler |
730 |
– |
sigemptyset(&sigsegv_action.sa_mask); // Block interrupts during SEGV handling |
731 |
– |
sigaddset(&sigsegv_action.sa_mask, SIGUSR2); |
732 |
– |
sigsegv_action.sa_handler = (__sighandler_t)sigsegv_handler; |
733 |
– |
sigsegv_action.sa_flags = SA_ONSTACK; |
734 |
– |
sigsegv_action.sa_restorer = NULL; |
735 |
– |
if (sigaction(SIGSEGV, &sigsegv_action, NULL) < 0) { |
736 |
– |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
737 |
– |
ErrorAlert(str); |
738 |
– |
goto quit; |
739 |
– |
} |
740 |
– |
|
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_handler = (__sighandler_t)sigill_handler; |
906 |
< |
sigill_action.sa_flags = SA_ONSTACK; |
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_SIGILL_INSTALL_ERR), strerror(errno)); |
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_handler = (__sighandler_t)sigusr2_handler; |
921 |
< |
sigusr2_action.sa_flags = 0; |
922 |
< |
#if !EMULATED_PPC |
759 |
< |
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART; |
760 |
< |
#endif |
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_SIGUSR2_INSTALL_ERR), strerror(errno)); |
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(); |
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 handler |
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); |
814 |
– |
#endif |
815 |
– |
|
816 |
– |
// Save NVRAM |
817 |
– |
XPRAMExit(); |
818 |
– |
|
819 |
– |
// Exit clipboard |
820 |
– |
ClipExit(); |
821 |
– |
|
822 |
– |
// Exit Time Manager |
823 |
– |
TimerExit(); |
981 |
|
|
982 |
< |
// Exit serial |
983 |
< |
SerialExit(); |
984 |
< |
|
985 |
< |
// Exit network |
986 |
< |
EtherExit(); |
987 |
< |
|
831 |
< |
// Exit audio |
832 |
< |
AudioExit(); |
833 |
< |
|
834 |
< |
// Exit video |
835 |
< |
VideoExit(); |
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 |
< |
// Exit external file system |
990 |
< |
ExtFSExit(); |
989 |
> |
// Deinitialize everything |
990 |
> |
ExitAll(); |
991 |
|
|
992 |
< |
// Exit drivers |
993 |
< |
SCSIExit(); |
842 |
< |
CDROMExit(); |
843 |
< |
DiskExit(); |
844 |
< |
SonyExit(); |
992 |
> |
// Delete SheepShaver globals |
993 |
> |
SheepMem::Exit(); |
994 |
|
|
995 |
|
// Delete RAM area |
996 |
|
if (ram_area_mapped) |
997 |
< |
vm_release((char *)RAM_BASE, RAMSize); |
997 |
> |
vm_mac_release(RAMBase, RAMSize); |
998 |
|
|
999 |
|
// Delete ROM area |
1000 |
|
if (rom_area_mapped) |
1001 |
< |
vm_release((char *)ROM_BASE, ROM_AREA_SIZE); |
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 |
< |
if (kernel_area >= 0) { |
856 |
< |
shmdt((void *)KERNEL_DATA_BASE); |
857 |
< |
shmdt((void *)KERNEL_DATA2_BASE); |
858 |
< |
shmctl(kernel_area, IPC_RMID, NULL); |
859 |
< |
} |
1010 |
> |
kernel_data_exit(); |
1011 |
|
|
1012 |
|
// Delete Low Memory area |
1013 |
|
if (lm_area_mapped) |
1014 |
< |
munmap((char *)0x0000, 0x3000); |
1014 |
> |
vm_mac_release(0, 0x3000); |
1015 |
|
|
1016 |
|
// Close /dev/zero |
1017 |
|
if (zero_fd > 0) |
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 |
|
|
1158 |
|
uint16 proc[2] = {trap, M68K_RTS}; |
1159 |
|
Execute68k((uint32)proc, r); |
1160 |
|
} |
957 |
– |
|
958 |
– |
|
959 |
– |
/* |
960 |
– |
* Execute PPC code from EMUL_OP routine (real mode switch) |
961 |
– |
*/ |
962 |
– |
|
963 |
– |
void ExecutePPC(void (*func)()) |
964 |
– |
{ |
965 |
– |
uint32 tvect[2] = {(uint32)func, 0}; // Fake TVECT |
966 |
– |
RoutineDescriptor desc = BUILD_PPC_ROUTINE_DESCRIPTOR(0, tvect); |
967 |
– |
M68kRegisters r; |
968 |
– |
Execute68k((uint32)&desc, &r); |
969 |
– |
} |
1161 |
|
#endif |
1162 |
|
|
1163 |
|
|
1176 |
|
|
1177 |
|
|
1178 |
|
/* |
988 |
– |
* Pause/resume emulator |
989 |
– |
*/ |
990 |
– |
|
991 |
– |
void PauseEmulator(void) |
992 |
– |
{ |
993 |
– |
pthread_kill(emul_thread, SIGSTOP); |
994 |
– |
} |
995 |
– |
|
996 |
– |
void ResumeEmulator(void) |
997 |
– |
{ |
998 |
– |
pthread_kill(emul_thread, SIGCONT); |
999 |
– |
} |
1000 |
– |
|
1001 |
– |
|
1002 |
– |
/* |
1179 |
|
* Dump 68k registers |
1180 |
|
*/ |
1181 |
|
|
1203 |
|
* Make code executable |
1204 |
|
*/ |
1205 |
|
|
1206 |
< |
void MakeExecutable(int dummy, void *start, uint32 length) |
1206 |
> |
void MakeExecutable(int dummy, uint32 start, uint32 length) |
1207 |
|
{ |
1208 |
< |
if (((uintptr)start >= ROM_BASE) && ((uintptr)start < (ROM_BASE + ROM_SIZE))) |
1208 |
> |
if ((start >= ROM_BASE) && (start < (ROM_BASE + ROM_SIZE))) |
1209 |
|
return; |
1210 |
|
#if EMULATED_PPC |
1211 |
< |
FlushCodeCache((uintptr)start, (uintptr)start + length); |
1211 |
> |
FlushCodeCache(start, start + length); |
1212 |
|
#else |
1213 |
< |
flush_icache_range(start, (void *)((uintptr)start + length)); |
1213 |
> |
flush_icache_range(start, start + length); |
1214 |
|
#endif |
1215 |
|
} |
1216 |
|
|
1217 |
|
|
1218 |
|
/* |
1219 |
< |
* Patch things after system startup (gets called by disk driver accRun routine) |
1219 |
> |
* NVRAM watchdog thread (saves NVRAM every minute) |
1220 |
|
*/ |
1221 |
|
|
1222 |
< |
void PatchAfterStartup(void) |
1222 |
> |
static void nvram_watchdog(void) |
1223 |
|
{ |
1224 |
< |
#if EMULATED_PPC |
1225 |
< |
ExecuteNative(NATIVE_VIDEO_INSTALL_ACCEL); |
1226 |
< |
#else |
1227 |
< |
ExecutePPC(VideoInstallAccel); |
1052 |
< |
#endif |
1053 |
< |
InstallExtFS(); |
1224 |
> |
if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) { |
1225 |
> |
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
1226 |
> |
SaveXPRAM(); |
1227 |
> |
} |
1228 |
|
} |
1229 |
|
|
1056 |
– |
|
1057 |
– |
/* |
1058 |
– |
* NVRAM watchdog thread (saves NVRAM every minute) |
1059 |
– |
*/ |
1060 |
– |
|
1230 |
|
static void *nvram_func(void *arg) |
1231 |
|
{ |
1232 |
< |
struct timespec req = {60, 0}; // 1 minute |
1233 |
< |
|
1234 |
< |
for (;;) { |
1235 |
< |
pthread_testcancel(); |
1067 |
< |
nanosleep(&req, NULL); |
1068 |
< |
pthread_testcancel(); |
1069 |
< |
if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) { |
1070 |
< |
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
1071 |
< |
SaveXPRAM(); |
1072 |
< |
} |
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 |
|
} |
1245 |
|
static void *tick_func(void *arg) |
1246 |
|
{ |
1247 |
|
int tick_counter = 0; |
1248 |
< |
struct timespec req = {0, 16625000}; |
1248 |
> |
uint64 start = GetTicks_usec(); |
1249 |
> |
int64 ticks = 0; |
1250 |
> |
uint64 next = GetTicks_usec(); |
1251 |
|
|
1252 |
< |
for (;;) { |
1252 |
> |
while (!tick_thread_cancel) { |
1253 |
|
|
1254 |
|
// Wait |
1255 |
< |
nanosleep(&req, NULL); |
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 |
< |
pt_regs *r = (pt_regs *)&sigsegv_regs; |
1268 |
> |
sigregs *r = &sigsegv_regs; |
1269 |
|
char str[256]; |
1270 |
< |
sprintf(str, "SIGSEGV\n" |
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" |
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], |
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 |
|
|
1368 |
|
pthread_mutexattr_init(&attr); |
1369 |
|
// Initialize the mutex for priority inheritance -- |
1370 |
|
// required for accurate timing. |
1371 |
< |
#ifdef HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL |
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) |
1439 |
|
* Trigger signal USR2 from another thread |
1440 |
|
*/ |
1441 |
|
|
1442 |
< |
#if !EMULATED_PPC || ASYNC_IRQ |
1442 |
> |
#if !EMULATED_PPC |
1443 |
|
void TriggerInterrupt(void) |
1444 |
|
{ |
1445 |
< |
if (ready_for_signals) |
1445 |
> |
if (ready_for_signals) { |
1446 |
> |
idle_resume(); |
1447 |
|
pthread_kill(emul_thread, SIGUSR2); |
1448 |
+ |
} |
1449 |
|
} |
1450 |
|
#endif |
1451 |
|
|
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 |
|
|
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 |
|
|
1499 |
|
* USR2 handler |
1500 |
|
*/ |
1501 |
|
|
1502 |
< |
#if EMULATED_PPC |
1503 |
< |
static void sigusr2_handler(int sig) |
1502 |
> |
#if !EMULATED_PPC |
1503 |
> |
void sigusr2_handler(int sig, siginfo_t *sip, void *scp) |
1504 |
|
{ |
1505 |
< |
#if ASYNC_IRQ |
1506 |
< |
extern void HandleInterrupt(void); |
1507 |
< |
HandleInterrupt(); |
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 |
1322 |
– |
} |
1323 |
– |
#else |
1324 |
– |
static void sigusr2_handler(int sig, sigcontext_struct *sc) |
1325 |
– |
{ |
1326 |
– |
pt_regs *r = sc->regs; |
1520 |
|
|
1521 |
|
// Do nothing if interrupts are disabled |
1522 |
|
if (*(int32 *)XLM_IRQ_NEST > 0) |
1530 |
|
case MODE_68K: |
1531 |
|
// 68k emulator active, trigger 68k interrupt level 1 |
1532 |
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1533 |
< |
r->ccr |= ntohl(kernel_data->v[0x674 >> 2]); |
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) { |
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 |
< |
atomic_add((int32 *)XLM_IRQ_NEST, 1); |
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 |
1564 |
|
if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) { |
1565 |
|
|
1566 |
|
// Set extra stack for SIGSEGV handler |
1567 |
< |
struct sigaltstack new_stack; |
1368 |
< |
new_stack.ss_sp = extra_stack; |
1369 |
< |
new_stack.ss_flags = 0; |
1370 |
< |
new_stack.ss_size = SIG_STACK_SIZE; |
1371 |
< |
sigaltstack(&new_stack, NULL); |
1567 |
> |
sigaltstack(&extra_stack, NULL); |
1568 |
|
#if 1 |
1569 |
|
// Execute full 68k interrupt routine |
1570 |
|
M68kRegisters r; |
1586 |
|
if (InterruptFlags & INTFLAG_VIA) { |
1587 |
|
ClearInterruptFlag(INTFLAG_VIA); |
1588 |
|
ADBInterrupt(); |
1589 |
< |
ExecutePPC(VideoVBL); |
1589 |
> |
ExecuteNative(NATIVE_VIDEO_VBL); |
1590 |
|
} |
1591 |
|
} |
1592 |
|
#endif |
1593 |
< |
// Reset normal signal stack |
1594 |
< |
new_stack.ss_sp = sig_stack; |
1399 |
< |
new_stack.ss_flags = 0; |
1400 |
< |
new_stack.ss_size = SIG_STACK_SIZE; |
1401 |
< |
sigaltstack(&new_stack, NULL); |
1593 |
> |
// Reset normal stack |
1594 |
> |
sigaltstack(&sig_stack, NULL); |
1595 |
|
} |
1596 |
|
break; |
1597 |
|
#endif |
1605 |
|
*/ |
1606 |
|
|
1607 |
|
#if !EMULATED_PPC |
1608 |
< |
static void sigsegv_handler(int sig, sigcontext_struct *sc) |
1608 |
> |
static void sigsegv_handler(int sig, siginfo_t *sip, void *scp) |
1609 |
|
{ |
1610 |
< |
pt_regs *r = sc->regs; |
1610 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1611 |
|
|
1612 |
|
// Get effective address |
1613 |
< |
uint32 addr = r->dar; |
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->nip)) |
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? |
1634 |
< |
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
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->nip == ROM_BASE + 0x488160 && r->gpr[20] == 0xf8000000) { |
1639 |
< |
r->nip += 4; |
1640 |
< |
r->gpr[8] = 0; |
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->nip == ROM_BASE + 0x488140 && r->gpr[16] == 0xf8000000) { |
1645 |
< |
r->nip += 4; |
1646 |
< |
r->gpr[8] = 0; |
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->nip == ROM_BASE + 0x48e080 && (r->gpr[8] == 0xf3012002 || r->gpr[8] == 0xf3012000)) { |
1651 |
< |
r->nip += 4; |
1652 |
< |
r->gpr[8] = 0; |
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->nip == ROM_BASE + 0x48c5e0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1657 |
< |
r->nip += 4; |
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 |
< |
} else if (r->nip == ROM_BASE + 0x4a10a0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1663 |
< |
r->nip += 4; |
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->nip); |
1673 |
> |
uint32 opcode = *((uint32 *)r->pc()); |
1674 |
|
uint32 primop = opcode >> 26; |
1675 |
|
uint32 exop = (opcode >> 1) & 0x3ff; |
1676 |
|
uint32 ra = (opcode >> 16) & 0x1f; |
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 |
1791 |
< |
if (transfer_type == TYPE_STORE && addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) { |
1792 |
< |
// 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->nip)); |
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->nip += 4; |
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; |
1804 |
> |
r->gpr(ra) = addr; |
1805 |
|
if (transfer_type == TYPE_LOAD) |
1806 |
< |
r->gpr[rd] = 0; |
1807 |
< |
r->nip += 4; |
1806 |
> |
r->gpr(rd) = 0; |
1807 |
> |
r->pc() += 4; |
1808 |
|
goto rti; |
1809 |
|
} |
1810 |
|
|
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->nip, r->gpr[24], r->gpr[1]); |
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->nip, r->gpr[24], r->gpr[1], opcode); |
1817 |
> |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode); |
1818 |
|
ErrorAlert(str); |
1819 |
|
QuitEmulator(); |
1820 |
|
return; |
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("SIGSEGV\n"); |
1828 |
< |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
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" |
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 |
< |
r->nip, r->link, r->ctr, r->msr, |
1841 |
< |
r->xer, r->ccr, |
1842 |
< |
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1843 |
< |
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
1844 |
< |
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
1845 |
< |
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
1846 |
< |
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
1847 |
< |
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
1848 |
< |
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
1849 |
< |
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
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 |
< |
sigsegv_regs = *(sigregs *)r; |
1856 |
> |
build_sigregs(&sigsegv_regs, r); |
1857 |
|
emul_thread_fatal = true; |
1858 |
|
for (;;) ; |
1859 |
|
} |
1865 |
|
* SIGILL handler |
1866 |
|
*/ |
1867 |
|
|
1868 |
< |
static void sigill_handler(int sig, sigcontext_struct *sc) |
1868 |
> |
static void sigill_handler(int sig, siginfo_t *sip, void *scp) |
1869 |
|
{ |
1870 |
< |
pt_regs *r = sc->regs; |
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->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
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->nip); |
1887 |
> |
uint32 opcode = *((uint32 *)r->pc()); |
1888 |
|
uint32 primop = opcode >> 26; |
1889 |
|
uint32 exop = (opcode >> 1) & 0x3ff; |
1890 |
|
uint32 ra = (opcode >> 16) & 0x1f; |
1895 |
|
switch (primop) { |
1896 |
|
case 9: // POWER instructions |
1897 |
|
case 22: |
1898 |
< |
power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->nip, r->gpr[1], opcode); |
1898 |
> |
power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->pc(), r->gpr(1), opcode); |
1899 |
|
ErrorAlert(str); |
1900 |
|
QuitEmulator(); |
1901 |
|
return; |
1903 |
|
case 31: |
1904 |
|
switch (exop) { |
1905 |
|
case 83: // mfmsr |
1906 |
< |
r->gpr[rd] = 0xf072; |
1907 |
< |
r->nip += 4; |
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->nip += 4; |
1913 |
> |
r->pc() += 4; |
1914 |
|
goto rti; |
1915 |
|
|
1916 |
|
case 339: { // mfspr |
1926 |
|
case 957: // PMC3 |
1927 |
|
case 958: // PMC4 |
1928 |
|
case 959: // SDA |
1929 |
< |
r->nip += 4; |
1929 |
> |
r->pc() += 4; |
1930 |
|
goto rti; |
1931 |
|
case 25: // SDR1 |
1932 |
< |
r->gpr[rd] = 0xdead001f; |
1933 |
< |
r->nip += 4; |
1932 |
> |
r->gpr(rd) = 0xdead001f; |
1933 |
> |
r->pc() += 4; |
1934 |
|
goto rti; |
1935 |
|
case 287: // PVR |
1936 |
< |
r->gpr[rd] = PVR; |
1937 |
< |
r->nip += 4; |
1936 |
> |
r->gpr(rd) = PVR; |
1937 |
> |
r->pc() += 4; |
1938 |
|
goto rti; |
1939 |
|
} |
1940 |
|
break; |
1970 |
|
case 957: // PMC3 |
1971 |
|
case 958: // PMC4 |
1972 |
|
case 959: // SDA |
1973 |
< |
r->nip += 4; |
1973 |
> |
r->pc() += 4; |
1974 |
|
goto rti; |
1975 |
|
} |
1976 |
|
break; |
1989 |
|
|
1990 |
|
// In GUI mode, show error alert |
1991 |
|
if (!PrefsFindBool("nogui")) { |
1992 |
< |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode); |
1992 |
> |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode); |
1993 |
|
ErrorAlert(str); |
1994 |
|
QuitEmulator(); |
1995 |
|
return; |
1997 |
|
} |
1998 |
|
|
1999 |
|
// For all other errors, jump into debugger (sort of...) |
2000 |
+ |
crash_reason = "SIGILL"; |
2001 |
|
if (!ready_for_signals) { |
2002 |
< |
printf("SIGILL\n"); |
2003 |
< |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
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" |
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 |
< |
r->nip, r->link, r->ctr, r->msr, |
2016 |
< |
r->xer, r->ccr, |
2017 |
< |
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
2018 |
< |
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
2019 |
< |
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
2020 |
< |
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
2021 |
< |
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
2022 |
< |
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
2023 |
< |
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
2024 |
< |
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
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 |
< |
sigsegv_regs = *(sigregs *)r; |
2031 |
> |
build_sigregs(&sigsegv_regs, r); |
2032 |
|
emul_thread_fatal = true; |
2033 |
|
for (;;) ; |
2034 |
|
} |
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 |
|
|
2132 |
|
|
2133 |
|
void ErrorAlert(const char *text) |
2134 |
|
{ |
2135 |
< |
#ifdef ENABLE_GTK |
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; |
2156 |
|
|
2157 |
|
void WarningAlert(const char *text) |
2158 |
|
{ |
2159 |
< |
#ifdef ENABLE_GTK |
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; |