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 |
|
|
114 |
|
#define DEBUG 0 |
115 |
|
#include "debug.h" |
116 |
|
|
117 |
|
|
118 |
+ |
#ifdef HAVE_DIRENT_H |
119 |
+ |
#include <dirent.h> |
120 |
+ |
#endif |
121 |
+ |
|
122 |
+ |
#ifdef USE_SDL |
123 |
+ |
#include <SDL.h> |
124 |
+ |
#endif |
125 |
+ |
|
126 |
+ |
#ifndef USE_SDL_VIDEO |
127 |
|
#include <X11/Xlib.h> |
128 |
+ |
#endif |
129 |
|
|
130 |
|
#ifdef ENABLE_GTK |
131 |
|
#include <gtk/gtk.h> |
142 |
|
#endif |
143 |
|
|
144 |
|
|
145 |
+ |
// Enable emulation of unaligned lmw/stmw? |
146 |
+ |
#define EMULATE_UNALIGNED_LOADSTORE_MULTIPLE 1 |
147 |
+ |
|
148 |
|
// Enable Execute68k() safety checks? |
149 |
|
#define SAFE_EXEC_68K 0 |
150 |
|
|
159 |
|
const char ROM_FILE_NAME[] = "ROM"; |
160 |
|
const char ROM_FILE_NAME2[] = "Mac OS ROM"; |
161 |
|
|
162 |
< |
const uint32 RAM_BASE = 0x20000000; // Base address of RAM |
163 |
< |
const uint32 SIG_STACK_SIZE = 0x10000; // Size of signal stack |
164 |
< |
|
165 |
< |
|
166 |
< |
#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 |
< |
}; |
162 |
> |
#if REAL_ADDRESSING |
163 |
> |
const uintptr RAM_BASE = 0x20000000; // Base address of RAM |
164 |
> |
#else |
165 |
> |
// FIXME: needs to be >= 0x04000000 |
166 |
> |
const uintptr RAM_BASE = 0x10000000; // Base address of RAM |
167 |
|
#endif |
168 |
+ |
const uint32 SIG_STACK_SIZE = 0x10000; // Size of signal stack |
169 |
|
|
170 |
|
|
171 |
|
// Global variables (exported) |
172 |
|
#if !EMULATED_PPC |
173 |
< |
void *TOC; // Small data pointer (r13) |
173 |
> |
void *TOC = NULL; // Pointer to Thread Local Storage (r2) |
174 |
> |
void *R13 = NULL; // Pointer to .sdata section (r13 under Linux) |
175 |
|
#endif |
176 |
|
uint32 RAMBase; // Base address of Mac RAM |
177 |
|
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 |
178 |
|
uint32 KernelDataAddr; // Address of Kernel Data |
179 |
|
uint32 BootGlobsAddr; // Address of BootGlobs structure at top of Mac RAM |
180 |
+ |
uint32 DRCacheAddr; // Address of DR Cache |
181 |
|
uint32 PVR; // Theoretical PVR |
182 |
|
int64 CPUClockSpeed; // Processor clock speed (Hz) |
183 |
|
int64 BusClockSpeed; // Bus clock speed (Hz) |
184 |
+ |
int64 TimebaseSpeed; // Timebase clock speed (Hz) |
185 |
+ |
uint8 *RAMBaseHost; // Base address of Mac RAM (host address space) |
186 |
+ |
uint8 *ROMBaseHost; // Base address of Mac ROM (host address space) |
187 |
|
|
188 |
|
|
189 |
|
// Global variables |
190 |
+ |
#ifndef USE_SDL_VIDEO |
191 |
|
char *x_display_name = NULL; // X11 display name |
192 |
|
Display *x_display = NULL; // X11 display handle |
193 |
+ |
#ifdef X11_LOCK_TYPE |
194 |
+ |
X11_LOCK_TYPE x_display_lock = X11_LOCK_INIT; // X11 display lock |
195 |
+ |
#endif |
196 |
+ |
#endif |
197 |
|
|
198 |
|
static int zero_fd = 0; // FD of /dev/zero |
190 |
– |
static bool sheep_area_mapped = false; // Flag: SheepShaver data area mmap()ed |
199 |
|
static bool lm_area_mapped = false; // Flag: Low Memory area mmap()ped |
200 |
|
static int kernel_area = -1; // SHM ID of Kernel Data area |
201 |
|
static bool rom_area_mapped = false; // Flag: Mac ROM mmap()ped |
202 |
|
static bool ram_area_mapped = false; // Flag: Mac RAM mmap()ped |
203 |
+ |
static bool dr_cache_area_mapped = false; // Flag: Mac DR Cache mmap()ped |
204 |
+ |
static bool dr_emulator_area_mapped = false;// Flag: Mac DR Emulator mmap()ped |
205 |
|
static KernelData *kernel_data; // Pointer to Kernel Data |
206 |
|
static EmulatorData *emulator_data; |
207 |
|
|
208 |
|
static uint8 last_xpram[XPRAM_SIZE]; // Buffer for monitoring XPRAM changes |
209 |
|
|
210 |
|
static bool nvram_thread_active = false; // Flag: NVRAM watchdog installed |
211 |
+ |
static volatile bool nvram_thread_cancel; // Flag: Cancel NVRAM thread |
212 |
|
static pthread_t nvram_thread; // NVRAM watchdog |
213 |
|
static bool tick_thread_active = false; // Flag: MacOS thread installed |
214 |
+ |
static volatile bool tick_thread_cancel; // Flag: Cancel 60Hz thread |
215 |
|
static pthread_t tick_thread; // 60Hz thread |
216 |
|
static pthread_t emul_thread; // MacOS thread |
217 |
|
|
219 |
|
static int64 num_segv = 0; // Number of handled SEGV signals |
220 |
|
|
221 |
|
static struct sigaction sigusr2_action; // Interrupt signal (of emulator thread) |
222 |
< |
#if !EMULATED_PPC |
222 |
> |
#if EMULATED_PPC |
223 |
> |
static uintptr sig_stack = 0; // Stack for PowerPC interrupt routine |
224 |
> |
#else |
225 |
|
static struct sigaction sigsegv_action; // Data access exception signal (of emulator thread) |
226 |
|
static struct sigaction sigill_action; // Illegal instruction signal (of emulator thread) |
227 |
< |
static void *sig_stack = NULL; // Stack for signal handlers |
228 |
< |
static void *extra_stack = NULL; // Stack for SIGSEGV inside interrupt handler |
227 |
> |
static struct sigaltstack sig_stack; // Stack for signal handlers |
228 |
> |
static struct sigaltstack extra_stack; // Stack for SIGSEGV inside interrupt handler |
229 |
|
static bool emul_thread_fatal = false; // Flag: MacOS thread crashed, tick thread shall dump debug output |
230 |
|
static sigregs sigsegv_regs; // Register dump when crashed |
231 |
+ |
static const char *crash_reason = NULL; // Reason of the crash (SIGSEGV, SIGBUS, SIGILL) |
232 |
|
#endif |
233 |
|
|
234 |
+ |
uint32 SheepMem::page_size; // Size of a native page |
235 |
+ |
uintptr SheepMem::zero_page = 0; // Address of ro page filled in with zeros |
236 |
+ |
uintptr SheepMem::base = 0x60000000; // Address of SheepShaver data |
237 |
+ |
uintptr SheepMem::proc; // Bottom address of SheepShave procedures |
238 |
+ |
uintptr SheepMem::data; // Top of SheepShaver data (stack like storage) |
239 |
+ |
|
240 |
|
|
241 |
|
// Prototypes |
242 |
+ |
static bool kernel_data_init(void); |
243 |
+ |
static void kernel_data_exit(void); |
244 |
|
static void Quit(void); |
245 |
|
static void *emul_func(void *arg); |
246 |
|
static void *nvram_func(void *arg); |
247 |
|
static void *tick_func(void *arg); |
248 |
|
#if EMULATED_PPC |
226 |
– |
static void sigusr2_handler(int sig); |
249 |
|
extern void emul_ppc(uint32 start); |
250 |
|
extern void init_emul_ppc(void); |
251 |
|
extern void exit_emul_ppc(void); |
252 |
+ |
sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t); |
253 |
|
#else |
254 |
< |
static void sigusr2_handler(int sig, sigcontext_struct *sc); |
255 |
< |
static void sigsegv_handler(int sig, sigcontext_struct *sc); |
256 |
< |
static void sigill_handler(int sig, sigcontext_struct *sc); |
254 |
> |
extern "C" void sigusr2_handler_init(int sig, siginfo_t *sip, void *scp); |
255 |
> |
extern "C" void sigusr2_handler(int sig, siginfo_t *sip, void *scp); |
256 |
> |
static void sigsegv_handler(int sig, siginfo_t *sip, void *scp); |
257 |
> |
static void sigill_handler(int sig, siginfo_t *sip, void *scp); |
258 |
|
#endif |
259 |
|
|
260 |
|
|
261 |
|
// From asm_linux.S |
262 |
|
#if !EMULATED_PPC |
239 |
– |
extern "C" void *get_toc(void); |
263 |
|
extern "C" void *get_sp(void); |
264 |
< |
extern "C" void flush_icache_range(void *start, void *end); |
264 |
> |
extern "C" void *get_r2(void); |
265 |
> |
extern "C" void set_r2(void *); |
266 |
> |
extern "C" void *get_r13(void); |
267 |
> |
extern "C" void set_r13(void *); |
268 |
> |
extern "C" void flush_icache_range(uint32 start, uint32 end); |
269 |
|
extern "C" void jump_to_rom(uint32 entry, uint32 context); |
270 |
|
extern "C" void quit_emulator(void); |
271 |
|
extern "C" void execute_68k(uint32 pc, M68kRegisters *r); |
279 |
|
|
280 |
|
#if EMULATED_PPC |
281 |
|
/* |
282 |
+ |
* Return signal stack base |
283 |
+ |
*/ |
284 |
+ |
|
285 |
+ |
uintptr SignalStackBase(void) |
286 |
+ |
{ |
287 |
+ |
return sig_stack + SIG_STACK_SIZE; |
288 |
+ |
} |
289 |
+ |
|
290 |
+ |
|
291 |
+ |
/* |
292 |
|
* Atomic operations |
293 |
|
*/ |
294 |
|
|
329 |
|
|
330 |
|
|
331 |
|
/* |
332 |
+ |
* Memory management helpers |
333 |
+ |
*/ |
334 |
+ |
|
335 |
+ |
static inline int vm_mac_acquire(uint32 addr, uint32 size) |
336 |
+ |
{ |
337 |
+ |
return vm_acquire_fixed(Mac2HostAddr(addr), size); |
338 |
+ |
} |
339 |
+ |
|
340 |
+ |
static inline int vm_mac_release(uint32 addr, uint32 size) |
341 |
+ |
{ |
342 |
+ |
return vm_release(Mac2HostAddr(addr), size); |
343 |
+ |
} |
344 |
+ |
|
345 |
+ |
|
346 |
+ |
/* |
347 |
|
* Main program |
348 |
|
*/ |
349 |
|
|
359 |
|
int main(int argc, char **argv) |
360 |
|
{ |
361 |
|
char str[256]; |
310 |
– |
uint32 *boot_globs; |
311 |
– |
int16 i16; |
362 |
|
int rom_fd; |
363 |
|
FILE *proc_file; |
364 |
|
const char *rom_path; |
375 |
|
printf(" %s\n", GetString(STR_ABOUT_TEXT2)); |
376 |
|
|
377 |
|
#if !EMULATED_PPC |
378 |
+ |
#ifdef SYSTEM_CLOBBERS_R2 |
379 |
|
// Get TOC pointer |
380 |
< |
TOC = get_toc(); |
380 |
> |
TOC = get_r2(); |
381 |
> |
#endif |
382 |
> |
#ifdef SYSTEM_CLOBBERS_R13 |
383 |
> |
// Get r13 register |
384 |
> |
R13 = get_r13(); |
385 |
> |
#endif |
386 |
|
#endif |
387 |
|
|
388 |
|
#ifdef ENABLE_GTK |
398 |
|
for (int i=1; i<argc; i++) { |
399 |
|
if (strcmp(argv[i], "--help") == 0) { |
400 |
|
usage(argv[0]); |
401 |
+ |
#ifndef USE_SDL_VIDEO |
402 |
|
} else if (strcmp(argv[i], "--display") == 0) { |
403 |
|
i++; |
404 |
|
if (i < argc) |
405 |
|
x_display_name = strdup(argv[i]); |
406 |
+ |
#endif |
407 |
|
} else if (argv[i][0] == '-') { |
408 |
|
fprintf(stderr, "Unrecognized option '%s'\n", argv[i]); |
409 |
|
usage(argv[0]); |
410 |
|
} |
411 |
|
} |
412 |
|
|
413 |
+ |
#ifdef USE_SDL |
414 |
+ |
// Initialize SDL system |
415 |
+ |
int sdl_flags = 0; |
416 |
+ |
#ifdef USE_SDL_VIDEO |
417 |
+ |
sdl_flags |= SDL_INIT_VIDEO; |
418 |
+ |
#endif |
419 |
+ |
#ifdef USE_SDL_AUDIO |
420 |
+ |
sdl_flags |= SDL_INIT_AUDIO; |
421 |
+ |
#endif |
422 |
+ |
assert(sdl_flags != 0); |
423 |
+ |
if (SDL_Init(sdl_flags) == -1) { |
424 |
+ |
char str[256]; |
425 |
+ |
sprintf(str, "Could not initialize SDL: %s.\n", SDL_GetError()); |
426 |
+ |
ErrorAlert(str); |
427 |
+ |
goto quit; |
428 |
+ |
} |
429 |
+ |
atexit(SDL_Quit); |
430 |
+ |
#endif |
431 |
+ |
|
432 |
+ |
#ifndef USE_SDL_VIDEO |
433 |
|
// Open display |
434 |
|
x_display = XOpenDisplay(x_display_name); |
435 |
|
if (x_display == NULL) { |
443 |
|
// Fork out, so we can return from fullscreen mode when things get ugly |
444 |
|
XF86DGAForkApp(DefaultScreen(x_display)); |
445 |
|
#endif |
446 |
+ |
#endif |
447 |
|
|
448 |
|
#ifdef ENABLE_MON |
449 |
|
// Initialize mon |
450 |
|
mon_init(); |
451 |
|
#endif |
452 |
|
|
453 |
+ |
#if !EMULATED_PPC |
454 |
+ |
// Create and install stacks for signal handlers |
455 |
+ |
sig_stack.ss_sp = malloc(SIG_STACK_SIZE); |
456 |
+ |
D(bug("Signal stack at %p\n", sig_stack.ss_sp)); |
457 |
+ |
if (sig_stack.ss_sp == NULL) { |
458 |
+ |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
459 |
+ |
goto quit; |
460 |
+ |
} |
461 |
+ |
sig_stack.ss_flags = 0; |
462 |
+ |
sig_stack.ss_size = SIG_STACK_SIZE; |
463 |
+ |
if (sigaltstack(&sig_stack, NULL) < 0) { |
464 |
+ |
sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno)); |
465 |
+ |
ErrorAlert(str); |
466 |
+ |
goto quit; |
467 |
+ |
} |
468 |
+ |
extra_stack.ss_sp = malloc(SIG_STACK_SIZE); |
469 |
+ |
D(bug("Extra stack at %p\n", extra_stack.ss_sp)); |
470 |
+ |
if (extra_stack.ss_sp == NULL) { |
471 |
+ |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
472 |
+ |
goto quit; |
473 |
+ |
} |
474 |
+ |
extra_stack.ss_flags = 0; |
475 |
+ |
extra_stack.ss_size = SIG_STACK_SIZE; |
476 |
+ |
#endif |
477 |
+ |
|
478 |
+ |
#if !EMULATED_PPC |
479 |
+ |
// Install SIGSEGV and SIGBUS handlers |
480 |
+ |
sigemptyset(&sigsegv_action.sa_mask); // Block interrupts during SEGV handling |
481 |
+ |
sigaddset(&sigsegv_action.sa_mask, SIGUSR2); |
482 |
+ |
sigsegv_action.sa_sigaction = sigsegv_handler; |
483 |
+ |
sigsegv_action.sa_flags = SA_ONSTACK | SA_SIGINFO; |
484 |
+ |
#ifdef HAVE_SIGNAL_SA_RESTORER |
485 |
+ |
sigsegv_action.sa_restorer = NULL; |
486 |
+ |
#endif |
487 |
+ |
if (sigaction(SIGSEGV, &sigsegv_action, NULL) < 0) { |
488 |
+ |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
489 |
+ |
ErrorAlert(str); |
490 |
+ |
goto quit; |
491 |
+ |
} |
492 |
+ |
if (sigaction(SIGBUS, &sigsegv_action, NULL) < 0) { |
493 |
+ |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
494 |
+ |
ErrorAlert(str); |
495 |
+ |
goto quit; |
496 |
+ |
} |
497 |
+ |
#else |
498 |
+ |
// Install SIGSEGV handler for CPU emulator |
499 |
+ |
if (!sigsegv_install_handler(sigsegv_handler)) { |
500 |
+ |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
501 |
+ |
ErrorAlert(str); |
502 |
+ |
goto quit; |
503 |
+ |
} |
504 |
+ |
#endif |
505 |
+ |
|
506 |
+ |
// Initialize VM system |
507 |
+ |
vm_init(); |
508 |
+ |
|
509 |
|
// Get system info |
510 |
|
PVR = 0x00040000; // Default: 604 |
511 |
|
CPUClockSpeed = 100000000; // Default: 100MHz |
512 |
|
BusClockSpeed = 100000000; // Default: 100MHz |
513 |
< |
#if !EMULATED_PPC |
513 |
> |
TimebaseSpeed = 25000000; // Default: 25MHz |
514 |
> |
#if EMULATED_PPC |
515 |
> |
PVR = 0x000c0000; // Default: 7400 (with AltiVec) |
516 |
> |
#elif defined(__APPLE__) && defined(__MACH__) |
517 |
> |
proc_file = popen("ioreg -c IOPlatformDevice", "r"); |
518 |
> |
if (proc_file) { |
519 |
> |
char line[256]; |
520 |
> |
bool powerpc_node = false; |
521 |
> |
while (fgets(line, sizeof(line) - 1, proc_file)) { |
522 |
> |
// Read line |
523 |
> |
int len = strlen(line); |
524 |
> |
if (len == 0) |
525 |
> |
continue; |
526 |
> |
line[len - 1] = 0; |
527 |
> |
|
528 |
> |
// Parse line |
529 |
> |
if (strstr(line, "o PowerPC,")) |
530 |
> |
powerpc_node = true; |
531 |
> |
else if (powerpc_node) { |
532 |
> |
uint32 value; |
533 |
> |
char head[256]; |
534 |
> |
if (sscanf(line, "%[ |]\"cpu-version\" = <%x>", head, &value) == 2) |
535 |
> |
PVR = value; |
536 |
> |
else if (sscanf(line, "%[ |]\"clock-frequency\" = <%x>", head, &value) == 2) |
537 |
> |
CPUClockSpeed = value; |
538 |
> |
else if (sscanf(line, "%[ |]\"bus-frequency\" = <%x>", head, &value) == 2) |
539 |
> |
BusClockSpeed = value; |
540 |
> |
else if (sscanf(line, "%[ |]\"timebase-frequency\" = <%x>", head, &value) == 2) |
541 |
> |
TimebaseSpeed = value; |
542 |
> |
else if (strchr(line, '}')) |
543 |
> |
powerpc_node = false; |
544 |
> |
} |
545 |
> |
} |
546 |
> |
fclose(proc_file); |
547 |
> |
} else { |
548 |
> |
sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno)); |
549 |
> |
WarningAlert(str); |
550 |
> |
} |
551 |
> |
#else |
552 |
|
proc_file = fopen("/proc/cpuinfo", "r"); |
553 |
|
if (proc_file) { |
554 |
+ |
// CPU specs from Linux kernel |
555 |
+ |
// TODO: make it more generic with features (e.g. AltiVec) and |
556 |
+ |
// cache information and friends for NameRegistry |
557 |
+ |
static const struct { |
558 |
+ |
uint32 pvr_mask; |
559 |
+ |
uint32 pvr_value; |
560 |
+ |
const char *cpu_name; |
561 |
+ |
} |
562 |
+ |
cpu_specs[] = { |
563 |
+ |
{ 0xffff0000, 0x00010000, "601" }, |
564 |
+ |
{ 0xffff0000, 0x00030000, "603" }, |
565 |
+ |
{ 0xffff0000, 0x00060000, "603e" }, |
566 |
+ |
{ 0xffff0000, 0x00070000, "603ev" }, |
567 |
+ |
{ 0xffff0000, 0x00040000, "604" }, |
568 |
+ |
{ 0xfffff000, 0x00090000, "604e" }, |
569 |
+ |
{ 0xffff0000, 0x00090000, "604r" }, |
570 |
+ |
{ 0xffff0000, 0x000a0000, "604ev" }, |
571 |
+ |
{ 0xffffffff, 0x00084202, "740/750" }, |
572 |
+ |
{ 0xfffff000, 0x00083000, "745/755" }, |
573 |
+ |
{ 0xfffffff0, 0x00080100, "750CX" }, |
574 |
+ |
{ 0xfffffff0, 0x00082200, "750CX" }, |
575 |
+ |
{ 0xfffffff0, 0x00082210, "750CXe" }, |
576 |
+ |
{ 0xffffff00, 0x70000100, "750FX" }, |
577 |
+ |
{ 0xffffffff, 0x70000200, "750FX" }, |
578 |
+ |
{ 0xffff0000, 0x70000000, "750FX" }, |
579 |
+ |
{ 0xffff0000, 0x70020000, "750GX" }, |
580 |
+ |
{ 0xffff0000, 0x00080000, "740/750" }, |
581 |
+ |
{ 0xffffffff, 0x000c1101, "7400 (1.1)" }, |
582 |
+ |
{ 0xffff0000, 0x000c0000, "7400" }, |
583 |
+ |
{ 0xffff0000, 0x800c0000, "7410" }, |
584 |
+ |
{ 0xffffffff, 0x80000200, "7450" }, |
585 |
+ |
{ 0xffffffff, 0x80000201, "7450" }, |
586 |
+ |
{ 0xffff0000, 0x80000000, "7450" }, |
587 |
+ |
{ 0xffffff00, 0x80010100, "7455" }, |
588 |
+ |
{ 0xffffffff, 0x80010200, "7455" }, |
589 |
+ |
{ 0xffff0000, 0x80010000, "7455" }, |
590 |
+ |
{ 0xffff0000, 0x80020000, "7457" }, |
591 |
+ |
{ 0xffff0000, 0x80030000, "7447A" }, |
592 |
+ |
{ 0xffff0000, 0x80040000, "7448" }, |
593 |
+ |
{ 0x7fff0000, 0x00810000, "82xx" }, |
594 |
+ |
{ 0x7fff0000, 0x00820000, "8280" }, |
595 |
+ |
{ 0xffff0000, 0x00400000, "Power3 (630)" }, |
596 |
+ |
{ 0xffff0000, 0x00410000, "Power3 (630+)" }, |
597 |
+ |
{ 0xffff0000, 0x00360000, "I-star" }, |
598 |
+ |
{ 0xffff0000, 0x00370000, "S-star" }, |
599 |
+ |
{ 0xffff0000, 0x00350000, "Power4" }, |
600 |
+ |
{ 0xffff0000, 0x00390000, "PPC970" }, |
601 |
+ |
{ 0xffff0000, 0x003c0000, "PPC970FX" }, |
602 |
+ |
{ 0xffff0000, 0x003a0000, "POWER5" }, |
603 |
+ |
{ 0, 0, 0 } |
604 |
+ |
}; |
605 |
+ |
|
606 |
|
char line[256]; |
607 |
|
while(fgets(line, 255, proc_file)) { |
608 |
|
// Read line |
614 |
|
// Parse line |
615 |
|
int i; |
616 |
|
char value[256]; |
617 |
< |
if (sscanf(line, "cpu : %s", value) == 1) { |
618 |
< |
if (strcmp(value, "601") == 0) |
619 |
< |
PVR = 0x00010000; |
620 |
< |
else if (strcmp(value, "603") == 0) |
621 |
< |
PVR = 0x00030000; |
622 |
< |
else if (strcmp(value, "604") == 0) |
623 |
< |
PVR = 0x00040000; |
624 |
< |
else if (strcmp(value, "603e") == 0) |
625 |
< |
PVR = 0x00060000; |
626 |
< |
else if (strcmp(value, "603ev") == 0) |
627 |
< |
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 |
617 |
> |
if (sscanf(line, "cpu : %[0-9A-Za-a]", value) == 1) { |
618 |
> |
// Search by name |
619 |
> |
const char *cpu_name = NULL; |
620 |
> |
for (int i = 0; cpu_specs[i].pvr_mask != 0; i++) { |
621 |
> |
if (strcmp(cpu_specs[i].cpu_name, value) == 0) { |
622 |
> |
cpu_name = cpu_specs[i].cpu_name; |
623 |
> |
PVR = cpu_specs[i].pvr_value; |
624 |
> |
break; |
625 |
> |
} |
626 |
> |
} |
627 |
> |
if (cpu_name == NULL) |
628 |
|
printf("WARNING: Unknown CPU type '%s', assuming 604\n", value); |
629 |
+ |
else |
630 |
+ |
printf("Found a PowerPC %s processor\n", cpu_name); |
631 |
|
} |
632 |
|
if (sscanf(line, "clock : %dMHz", &i) == 1) |
633 |
|
CPUClockSpeed = BusClockSpeed = i * 1000000; |
637 |
|
sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno)); |
638 |
|
WarningAlert(str); |
639 |
|
} |
640 |
+ |
|
641 |
+ |
// Get actual bus frequency |
642 |
+ |
proc_file = fopen("/proc/device-tree/clock-frequency", "r"); |
643 |
+ |
if (proc_file) { |
644 |
+ |
union { uint8 b[4]; uint32 l; } value; |
645 |
+ |
if (fread(value.b, sizeof(value), 1, proc_file) == 1) |
646 |
+ |
BusClockSpeed = value.l; |
647 |
+ |
fclose(proc_file); |
648 |
+ |
} |
649 |
+ |
|
650 |
+ |
// Get actual timebase frequency |
651 |
+ |
TimebaseSpeed = BusClockSpeed / 4; |
652 |
+ |
DIR *cpus_dir; |
653 |
+ |
if ((cpus_dir = opendir("/proc/device-tree/cpus")) != NULL) { |
654 |
+ |
struct dirent *cpu_entry; |
655 |
+ |
while ((cpu_entry = readdir(cpus_dir)) != NULL) { |
656 |
+ |
if (strstr(cpu_entry->d_name, "PowerPC,") == cpu_entry->d_name) { |
657 |
+ |
char timebase_freq_node[256]; |
658 |
+ |
sprintf(timebase_freq_node, "/proc/device-tree/cpus/%s/timebase-frequency", cpu_entry->d_name); |
659 |
+ |
proc_file = fopen(timebase_freq_node, "r"); |
660 |
+ |
if (proc_file) { |
661 |
+ |
union { uint8 b[4]; uint32 l; } value; |
662 |
+ |
if (fread(value.b, sizeof(value), 1, proc_file) == 1) |
663 |
+ |
TimebaseSpeed = value.l; |
664 |
+ |
fclose(proc_file); |
665 |
+ |
} |
666 |
+ |
} |
667 |
+ |
} |
668 |
+ |
closedir(cpus_dir); |
669 |
+ |
} |
670 |
|
#endif |
671 |
+ |
// Remap any newer G4/G5 processor to plain G4 for compatibility |
672 |
+ |
switch (PVR >> 16) { |
673 |
+ |
case 0x8000: // 7450 |
674 |
+ |
case 0x8001: // 7455 |
675 |
+ |
case 0x8002: // 7457 |
676 |
+ |
case 0x8003: // 7447A |
677 |
+ |
case 0x8004: // 7448 |
678 |
+ |
case 0x0039: // 970 |
679 |
+ |
case 0x003c: // 970FX |
680 |
+ |
PVR = 0x000c0000; // 7400 |
681 |
+ |
break; |
682 |
+ |
} |
683 |
|
D(bug("PVR: %08x (assumed)\n", PVR)); |
684 |
|
|
685 |
|
// Init system routines |
703 |
|
goto quit; |
704 |
|
} |
705 |
|
|
706 |
+ |
#ifndef PAGEZERO_HACK |
707 |
|
// Create Low Memory area (0x0000..0x3000) |
708 |
< |
if (vm_acquire_fixed((char *)0, 0x3000) < 0) { |
708 |
> |
if (vm_mac_acquire(0, 0x3000) < 0) { |
709 |
|
sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno)); |
710 |
|
ErrorAlert(str); |
711 |
|
goto quit; |
712 |
|
} |
713 |
|
lm_area_mapped = true; |
714 |
+ |
#endif |
715 |
|
|
716 |
|
// Create areas for Kernel Data |
717 |
< |
kernel_area = shmget(IPC_PRIVATE, KERNEL_AREA_SIZE, 0600); |
718 |
< |
if (kernel_area == -1) { |
719 |
< |
sprintf(str, GetString(STR_KD_SHMGET_ERR), strerror(errno)); |
717 |
> |
if (!kernel_data_init()) |
718 |
> |
goto quit; |
719 |
> |
kernel_data = (KernelData *)Mac2HostAddr(KERNEL_DATA_BASE); |
720 |
> |
emulator_data = &kernel_data->ed; |
721 |
> |
KernelDataAddr = KERNEL_DATA_BASE; |
722 |
> |
D(bug("Kernel Data at %p (%08x)\n", kernel_data, KERNEL_DATA_BASE)); |
723 |
> |
D(bug("Emulator Data at %p (%08x)\n", emulator_data, KERNEL_DATA_BASE + offsetof(KernelData, ed))); |
724 |
> |
|
725 |
> |
// Create area for DR Cache |
726 |
> |
if (vm_mac_acquire(DR_EMULATOR_BASE, DR_EMULATOR_SIZE) < 0) { |
727 |
> |
sprintf(str, GetString(STR_DR_EMULATOR_MMAP_ERR), strerror(errno)); |
728 |
|
ErrorAlert(str); |
729 |
|
goto quit; |
730 |
|
} |
731 |
< |
if (shmat(kernel_area, (void *)KERNEL_DATA_BASE, 0) < 0) { |
732 |
< |
sprintf(str, GetString(STR_KD_SHMAT_ERR), strerror(errno)); |
731 |
> |
dr_emulator_area_mapped = true; |
732 |
> |
if (vm_mac_acquire(DR_CACHE_BASE, DR_CACHE_SIZE) < 0) { |
733 |
> |
sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno)); |
734 |
|
ErrorAlert(str); |
735 |
|
goto quit; |
736 |
|
} |
737 |
< |
if (shmat(kernel_area, (void *)KERNEL_DATA2_BASE, 0) < 0) { |
738 |
< |
sprintf(str, GetString(STR_KD2_SHMAT_ERR), strerror(errno)); |
737 |
> |
dr_cache_area_mapped = true; |
738 |
> |
#if !EMULATED_PPC |
739 |
> |
if (vm_protect((char *)DR_CACHE_BASE, DR_CACHE_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
740 |
> |
sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno)); |
741 |
|
ErrorAlert(str); |
742 |
|
goto quit; |
743 |
|
} |
744 |
< |
kernel_data = (KernelData *)0x68ffe000; |
745 |
< |
emulator_data = &kernel_data->ed; |
746 |
< |
KernelDataAddr = (uint32)kernel_data; |
476 |
< |
D(bug("Kernel Data at %p, Emulator Data at %p\n", kernel_data, emulator_data)); |
744 |
> |
#endif |
745 |
> |
DRCacheAddr = DR_CACHE_BASE; |
746 |
> |
D(bug("DR Cache at %p\n", DRCacheAddr)); |
747 |
|
|
748 |
|
// Create area for SheepShaver data |
749 |
< |
if (vm_acquire_fixed((char *)SHEEP_BASE, SHEEP_SIZE) < 0) { |
749 |
> |
if (!SheepMem::Init()) { |
750 |
|
sprintf(str, GetString(STR_SHEEP_MEM_MMAP_ERR), strerror(errno)); |
751 |
|
ErrorAlert(str); |
752 |
|
goto quit; |
753 |
|
} |
484 |
– |
SheepStack1Base = SHEEP_BASE + 0x10000; |
485 |
– |
SheepStack2Base = SheepStack1Base + 0x10000; |
486 |
– |
SheepThunksBase = SheepStack2Base + 0x1000; |
487 |
– |
sheep_area_mapped = true; |
754 |
|
|
755 |
|
// Create area for Mac ROM |
756 |
< |
if (vm_acquire_fixed((char *)ROM_BASE, ROM_AREA_SIZE) < 0) { |
756 |
> |
if (vm_mac_acquire(ROM_BASE, ROM_AREA_SIZE) < 0) { |
757 |
|
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
758 |
|
ErrorAlert(str); |
759 |
|
goto quit; |
760 |
|
} |
761 |
< |
#if !EMULATED_PPC || defined(__powerpc__) |
762 |
< |
if (vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
761 |
> |
ROMBaseHost = Mac2HostAddr(ROM_BASE); |
762 |
> |
#if !EMULATED_PPC |
763 |
> |
if (vm_protect(ROMBaseHost, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
764 |
|
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
765 |
|
ErrorAlert(str); |
766 |
|
goto quit; |
767 |
|
} |
768 |
|
#endif |
769 |
|
rom_area_mapped = true; |
770 |
< |
D(bug("ROM area at %08x\n", ROM_BASE)); |
770 |
> |
D(bug("ROM area at %p (%08x)\n", ROMBaseHost, ROM_BASE)); |
771 |
|
|
772 |
|
// Create area for Mac RAM |
773 |
|
RAMSize = PrefsFindInt32("ramsize"); |
776 |
|
RAMSize = 8*1024*1024; |
777 |
|
} |
778 |
|
|
779 |
< |
if (vm_acquire_fixed((char *)RAM_BASE, RAMSize) < 0) { |
779 |
> |
if (vm_mac_acquire(RAM_BASE, RAMSize) < 0) { |
780 |
|
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
781 |
|
ErrorAlert(str); |
782 |
|
goto quit; |
783 |
|
} |
784 |
+ |
RAMBaseHost = Mac2HostAddr(RAM_BASE); |
785 |
|
#if !EMULATED_PPC |
786 |
< |
if (vm_protect((char *)RAM_BASE, RAMSize, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
786 |
> |
if (vm_protect(RAMBaseHost, RAMSize, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
787 |
|
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
788 |
|
ErrorAlert(str); |
789 |
|
goto quit; |
791 |
|
#endif |
792 |
|
RAMBase = RAM_BASE; |
793 |
|
ram_area_mapped = true; |
794 |
< |
D(bug("RAM area at %08x\n", RAMBase)); |
794 |
> |
D(bug("RAM area at %p (%08x)\n", RAMBaseHost, RAMBase)); |
795 |
|
|
796 |
|
if (RAMBase > ROM_BASE) { |
797 |
|
ErrorAlert(GetString(STR_RAM_HIGHER_THAN_ROM_ERR)); |
827 |
|
} |
828 |
|
delete[] rom_tmp; |
829 |
|
|
830 |
< |
// Load NVRAM |
831 |
< |
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)); |
830 |
> |
// Initialize everything |
831 |
> |
if (!InitAll()) |
832 |
|
goto quit; |
833 |
< |
} |
833 |
> |
D(bug("Initialization complete\n")); |
834 |
|
|
835 |
|
// Clear caches (as we loaded and patched code) and write protect ROM |
836 |
|
#if !EMULATED_PPC |
837 |
< |
MakeExecutable(0, (void *)ROM_BASE, ROM_AREA_SIZE); |
837 |
> |
flush_icache_range(ROM_BASE, ROM_BASE + ROM_AREA_SIZE); |
838 |
|
#endif |
839 |
< |
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); |
691 |
< |
#endif |
692 |
< |
D(bug("Low Memory initialized\n")); |
839 |
> |
vm_protect(ROMBaseHost, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_EXECUTE); |
840 |
|
|
841 |
|
// Start 60Hz thread |
842 |
+ |
tick_thread_cancel = false; |
843 |
|
tick_thread_active = (pthread_create(&tick_thread, NULL, tick_func, NULL) == 0); |
844 |
|
D(bug("Tick thread installed (%ld)\n", tick_thread)); |
845 |
|
|
846 |
|
// Start NVRAM watchdog thread |
847 |
|
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
848 |
+ |
nvram_thread_cancel = false; |
849 |
|
nvram_thread_active = (pthread_create(&nvram_thread, NULL, nvram_func, NULL) == 0); |
850 |
|
D(bug("NVRAM thread installed (%ld)\n", nvram_thread)); |
851 |
|
|
852 |
|
#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 |
– |
|
853 |
|
// Install SIGILL handler |
854 |
|
sigemptyset(&sigill_action.sa_mask); // Block interrupts during ILL handling |
855 |
|
sigaddset(&sigill_action.sa_mask, SIGUSR2); |
856 |
< |
sigill_action.sa_handler = (__sighandler_t)sigill_handler; |
857 |
< |
sigill_action.sa_flags = SA_ONSTACK; |
856 |
> |
sigill_action.sa_sigaction = sigill_handler; |
857 |
> |
sigill_action.sa_flags = SA_ONSTACK | SA_SIGINFO; |
858 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
859 |
|
sigill_action.sa_restorer = NULL; |
860 |
+ |
#endif |
861 |
|
if (sigaction(SIGILL, &sigill_action, NULL) < 0) { |
862 |
|
sprintf(str, GetString(STR_SIGILL_INSTALL_ERR), strerror(errno)); |
863 |
|
ErrorAlert(str); |
865 |
|
} |
866 |
|
#endif |
867 |
|
|
868 |
+ |
#if !EMULATED_PPC |
869 |
|
// Install interrupt signal handler |
870 |
|
sigemptyset(&sigusr2_action.sa_mask); |
871 |
< |
sigusr2_action.sa_handler = (__sighandler_t)sigusr2_handler; |
872 |
< |
sigusr2_action.sa_flags = 0; |
873 |
< |
#if !EMULATED_PPC |
759 |
< |
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART; |
760 |
< |
#endif |
871 |
> |
sigusr2_action.sa_sigaction = sigusr2_handler_init; |
872 |
> |
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART | SA_SIGINFO; |
873 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
874 |
|
sigusr2_action.sa_restorer = NULL; |
875 |
+ |
#endif |
876 |
|
if (sigaction(SIGUSR2, &sigusr2_action, NULL) < 0) { |
877 |
|
sprintf(str, GetString(STR_SIGUSR2_INSTALL_ERR), strerror(errno)); |
878 |
|
ErrorAlert(str); |
879 |
|
goto quit; |
880 |
|
} |
881 |
+ |
#endif |
882 |
|
|
883 |
|
// Get my thread ID and execute MacOS thread function |
884 |
|
emul_thread = pthread_self(); |
904 |
|
|
905 |
|
// Stop 60Hz thread |
906 |
|
if (tick_thread_active) { |
907 |
+ |
tick_thread_cancel = true; |
908 |
|
pthread_cancel(tick_thread); |
909 |
|
pthread_join(tick_thread, NULL); |
910 |
|
} |
911 |
|
|
912 |
|
// Stop NVRAM watchdog thread |
913 |
|
if (nvram_thread_active) { |
914 |
+ |
nvram_thread_cancel = true; |
915 |
|
pthread_cancel(nvram_thread); |
916 |
|
pthread_join(nvram_thread, NULL); |
917 |
|
} |
918 |
|
|
919 |
|
#if !EMULATED_PPC |
920 |
< |
// Uninstall SIGSEGV handler |
920 |
> |
// Uninstall SIGSEGV and SIGBUS handlers |
921 |
|
sigemptyset(&sigsegv_action.sa_mask); |
922 |
|
sigsegv_action.sa_handler = SIG_DFL; |
923 |
|
sigsegv_action.sa_flags = 0; |
924 |
|
sigaction(SIGSEGV, &sigsegv_action, NULL); |
925 |
+ |
sigaction(SIGBUS, &sigsegv_action, NULL); |
926 |
|
|
927 |
|
// Uninstall SIGILL handler |
928 |
|
sigemptyset(&sigill_action.sa_mask); |
929 |
|
sigill_action.sa_handler = SIG_DFL; |
930 |
|
sigill_action.sa_flags = 0; |
931 |
|
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(); |
824 |
– |
|
825 |
– |
// Exit serial |
826 |
– |
SerialExit(); |
932 |
|
|
933 |
< |
// Exit network |
934 |
< |
EtherExit(); |
935 |
< |
|
936 |
< |
// Exit audio |
937 |
< |
AudioExit(); |
938 |
< |
|
834 |
< |
// Exit video |
835 |
< |
VideoExit(); |
933 |
> |
// Delete stacks for signal handlers |
934 |
> |
if (sig_stack.ss_sp) |
935 |
> |
free(sig_stack.ss_sp); |
936 |
> |
if (extra_stack.ss_sp) |
937 |
> |
free(extra_stack.ss_sp); |
938 |
> |
#endif |
939 |
|
|
940 |
< |
// Exit external file system |
941 |
< |
ExtFSExit(); |
940 |
> |
// Deinitialize everything |
941 |
> |
ExitAll(); |
942 |
|
|
943 |
< |
// Exit drivers |
944 |
< |
SCSIExit(); |
842 |
< |
CDROMExit(); |
843 |
< |
DiskExit(); |
844 |
< |
SonyExit(); |
943 |
> |
// Delete SheepShaver globals |
944 |
> |
SheepMem::Exit(); |
945 |
|
|
946 |
|
// Delete RAM area |
947 |
|
if (ram_area_mapped) |
948 |
< |
vm_release((char *)RAM_BASE, RAMSize); |
948 |
> |
vm_mac_release(RAM_BASE, RAMSize); |
949 |
|
|
950 |
|
// Delete ROM area |
951 |
|
if (rom_area_mapped) |
952 |
< |
vm_release((char *)ROM_BASE, ROM_AREA_SIZE); |
952 |
> |
vm_mac_release(ROM_BASE, ROM_AREA_SIZE); |
953 |
> |
|
954 |
> |
// Delete DR cache areas |
955 |
> |
if (dr_emulator_area_mapped) |
956 |
> |
vm_mac_release(DR_EMULATOR_BASE, DR_EMULATOR_SIZE); |
957 |
> |
if (dr_cache_area_mapped) |
958 |
> |
vm_mac_release(DR_CACHE_BASE, DR_CACHE_SIZE); |
959 |
|
|
960 |
|
// Delete Kernel Data area |
961 |
< |
if (kernel_area >= 0) { |
856 |
< |
shmdt((void *)KERNEL_DATA_BASE); |
857 |
< |
shmdt((void *)KERNEL_DATA2_BASE); |
858 |
< |
shmctl(kernel_area, IPC_RMID, NULL); |
859 |
< |
} |
961 |
> |
kernel_data_exit(); |
962 |
|
|
963 |
|
// Delete Low Memory area |
964 |
|
if (lm_area_mapped) |
965 |
< |
munmap((char *)0x0000, 0x3000); |
965 |
> |
vm_mac_release(0, 0x3000); |
966 |
|
|
967 |
|
// Close /dev/zero |
968 |
|
if (zero_fd > 0) |
980 |
|
#endif |
981 |
|
|
982 |
|
// Close X11 server connection |
983 |
+ |
#ifndef USE_SDL_VIDEO |
984 |
|
if (x_display) |
985 |
|
XCloseDisplay(x_display); |
986 |
+ |
#endif |
987 |
|
|
988 |
|
exit(0); |
989 |
|
} |
990 |
|
|
991 |
|
|
992 |
|
/* |
993 |
+ |
* Initialize Kernel Data segments |
994 |
+ |
*/ |
995 |
+ |
|
996 |
+ |
static bool kernel_data_init(void) |
997 |
+ |
{ |
998 |
+ |
char str[256]; |
999 |
+ |
uint32 kernel_area_size = (KERNEL_AREA_SIZE + SHMLBA - 1) & -SHMLBA; |
1000 |
+ |
|
1001 |
+ |
kernel_area = shmget(IPC_PRIVATE, kernel_area_size, 0600); |
1002 |
+ |
if (kernel_area == -1) { |
1003 |
+ |
sprintf(str, GetString(STR_KD_SHMGET_ERR), strerror(errno)); |
1004 |
+ |
ErrorAlert(str); |
1005 |
+ |
return false; |
1006 |
+ |
} |
1007 |
+ |
void *kernel_addr = Mac2HostAddr(KERNEL_DATA_BASE & -SHMLBA); |
1008 |
+ |
if (shmat(kernel_area, kernel_addr, 0) != kernel_addr) { |
1009 |
+ |
sprintf(str, GetString(STR_KD_SHMAT_ERR), strerror(errno)); |
1010 |
+ |
ErrorAlert(str); |
1011 |
+ |
return false; |
1012 |
+ |
} |
1013 |
+ |
kernel_addr = Mac2HostAddr(KERNEL_DATA2_BASE & -SHMLBA); |
1014 |
+ |
if (shmat(kernel_area, kernel_addr, 0) != kernel_addr) { |
1015 |
+ |
sprintf(str, GetString(STR_KD2_SHMAT_ERR), strerror(errno)); |
1016 |
+ |
ErrorAlert(str); |
1017 |
+ |
return false; |
1018 |
+ |
} |
1019 |
+ |
return true; |
1020 |
+ |
} |
1021 |
+ |
|
1022 |
+ |
|
1023 |
+ |
/* |
1024 |
+ |
* Deallocate Kernel Data segments |
1025 |
+ |
*/ |
1026 |
+ |
|
1027 |
+ |
static void kernel_data_exit(void) |
1028 |
+ |
{ |
1029 |
+ |
if (kernel_area >= 0) { |
1030 |
+ |
shmdt(Mac2HostAddr(KERNEL_DATA_BASE & -SHMLBA)); |
1031 |
+ |
shmdt(Mac2HostAddr(KERNEL_DATA2_BASE & -SHMLBA)); |
1032 |
+ |
shmctl(kernel_area, IPC_RMID, NULL); |
1033 |
+ |
} |
1034 |
+ |
} |
1035 |
+ |
|
1036 |
+ |
|
1037 |
+ |
/* |
1038 |
|
* Jump into Mac ROM, start 680x0 emulator |
1039 |
|
*/ |
1040 |
|
|
1103 |
|
uint16 proc[2] = {trap, M68K_RTS}; |
1104 |
|
Execute68k((uint32)proc, r); |
1105 |
|
} |
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 |
– |
} |
1106 |
|
#endif |
1107 |
|
|
1108 |
|
|
1121 |
|
|
1122 |
|
|
1123 |
|
/* |
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 |
– |
/* |
1124 |
|
* Dump 68k registers |
1125 |
|
*/ |
1126 |
|
|
1148 |
|
* Make code executable |
1149 |
|
*/ |
1150 |
|
|
1151 |
< |
void MakeExecutable(int dummy, void *start, uint32 length) |
1151 |
> |
void MakeExecutable(int dummy, uint32 start, uint32 length) |
1152 |
|
{ |
1153 |
< |
if (((uintptr)start >= ROM_BASE) && ((uintptr)start < (ROM_BASE + ROM_SIZE))) |
1153 |
> |
if ((start >= ROM_BASE) && (start < (ROM_BASE + ROM_SIZE))) |
1154 |
|
return; |
1155 |
|
#if EMULATED_PPC |
1156 |
< |
FlushCodeCache((uintptr)start, (uintptr)start + length); |
1156 |
> |
FlushCodeCache(start, start + length); |
1157 |
|
#else |
1158 |
< |
flush_icache_range(start, (void *)((uintptr)start + length)); |
1158 |
> |
flush_icache_range(start, start + length); |
1159 |
|
#endif |
1160 |
|
} |
1161 |
|
|
1162 |
|
|
1163 |
|
/* |
1164 |
< |
* Patch things after system startup (gets called by disk driver accRun routine) |
1164 |
> |
* NVRAM watchdog thread (saves NVRAM every minute) |
1165 |
|
*/ |
1166 |
|
|
1167 |
< |
void PatchAfterStartup(void) |
1167 |
> |
static void nvram_watchdog(void) |
1168 |
|
{ |
1169 |
< |
#if EMULATED_PPC |
1170 |
< |
ExecuteNative(NATIVE_VIDEO_INSTALL_ACCEL); |
1171 |
< |
#else |
1172 |
< |
ExecutePPC(VideoInstallAccel); |
1052 |
< |
#endif |
1053 |
< |
InstallExtFS(); |
1169 |
> |
if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) { |
1170 |
> |
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
1171 |
> |
SaveXPRAM(); |
1172 |
> |
} |
1173 |
|
} |
1174 |
|
|
1056 |
– |
|
1057 |
– |
/* |
1058 |
– |
* NVRAM watchdog thread (saves NVRAM every minute) |
1059 |
– |
*/ |
1060 |
– |
|
1175 |
|
static void *nvram_func(void *arg) |
1176 |
|
{ |
1177 |
< |
struct timespec req = {60, 0}; // 1 minute |
1178 |
< |
|
1179 |
< |
for (;;) { |
1180 |
< |
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 |
< |
} |
1177 |
> |
while (!nvram_thread_cancel) { |
1178 |
> |
for (int i=0; i<60 && !nvram_thread_cancel; i++) |
1179 |
> |
Delay_usec(999999); // Only wait 1 second so we quit promptly when nvram_thread_cancel becomes true |
1180 |
> |
nvram_watchdog(); |
1181 |
|
} |
1182 |
|
return NULL; |
1183 |
|
} |
1190 |
|
static void *tick_func(void *arg) |
1191 |
|
{ |
1192 |
|
int tick_counter = 0; |
1193 |
< |
struct timespec req = {0, 16625000}; |
1193 |
> |
uint64 start = GetTicks_usec(); |
1194 |
> |
int64 ticks = 0; |
1195 |
> |
uint64 next = GetTicks_usec(); |
1196 |
|
|
1197 |
< |
for (;;) { |
1197 |
> |
while (!tick_thread_cancel) { |
1198 |
|
|
1199 |
|
// Wait |
1200 |
< |
nanosleep(&req, NULL); |
1200 |
> |
next += 16625; |
1201 |
> |
int64 delay = next - GetTicks_usec(); |
1202 |
> |
if (delay > 0) |
1203 |
> |
Delay_usec(delay); |
1204 |
> |
else if (delay < -16625) |
1205 |
> |
next = GetTicks_usec(); |
1206 |
> |
ticks++; |
1207 |
|
|
1208 |
|
#if !EMULATED_PPC |
1209 |
|
// Did we crash? |
1210 |
|
if (emul_thread_fatal) { |
1211 |
|
|
1212 |
|
// Yes, dump registers |
1213 |
< |
pt_regs *r = (pt_regs *)&sigsegv_regs; |
1213 |
> |
sigregs *r = &sigsegv_regs; |
1214 |
|
char str[256]; |
1215 |
< |
sprintf(str, "SIGSEGV\n" |
1215 |
> |
if (crash_reason == NULL) |
1216 |
> |
crash_reason = "SIGSEGV"; |
1217 |
> |
sprintf(str, "%s\n" |
1218 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1219 |
|
" xer %08lx cr %08lx \n" |
1220 |
|
" r0 %08lx r1 %08lx r2 %08lx r3 %08lx\n" |
1225 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1226 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1227 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1228 |
+ |
crash_reason, |
1229 |
|
r->nip, r->link, r->ctr, r->msr, |
1230 |
|
r->xer, r->ccr, |
1231 |
|
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1261 |
|
TriggerInterrupt(); |
1262 |
|
} |
1263 |
|
} |
1264 |
+ |
|
1265 |
+ |
uint64 end = GetTicks_usec(); |
1266 |
+ |
D(bug("%lld ticks in %lld usec = %f ticks/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start))); |
1267 |
|
return NULL; |
1268 |
|
} |
1269 |
|
|
1313 |
|
pthread_mutexattr_init(&attr); |
1314 |
|
// Initialize the mutex for priority inheritance -- |
1315 |
|
// required for accurate timing. |
1316 |
< |
#ifdef HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL |
1316 |
> |
#if defined(HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL) && !defined(__CYGWIN__) |
1317 |
|
pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT); |
1318 |
|
#endif |
1319 |
|
#if defined(HAVE_PTHREAD_MUTEXATTR_SETTYPE) && defined(PTHREAD_MUTEX_NORMAL) |
1384 |
|
* Trigger signal USR2 from another thread |
1385 |
|
*/ |
1386 |
|
|
1387 |
< |
#if !EMULATED_PPC || ASYNC_IRQ |
1387 |
> |
#if !EMULATED_PPC |
1388 |
|
void TriggerInterrupt(void) |
1389 |
|
{ |
1390 |
< |
if (ready_for_signals) |
1390 |
> |
if (ready_for_signals) { |
1391 |
> |
idle_resume(); |
1392 |
|
pthread_kill(emul_thread, SIGUSR2); |
1393 |
+ |
} |
1394 |
|
} |
1395 |
|
#endif |
1396 |
|
|
1418 |
|
|
1419 |
|
void DisableInterrupt(void) |
1420 |
|
{ |
1421 |
+ |
#if EMULATED_PPC |
1422 |
+ |
WriteMacInt32(XLM_IRQ_NEST, int32(ReadMacInt32(XLM_IRQ_NEST)) + 1); |
1423 |
+ |
#else |
1424 |
|
atomic_add((int *)XLM_IRQ_NEST, 1); |
1425 |
+ |
#endif |
1426 |
|
} |
1427 |
|
|
1428 |
|
|
1432 |
|
|
1433 |
|
void EnableInterrupt(void) |
1434 |
|
{ |
1435 |
+ |
#if EMULATED_PPC |
1436 |
+ |
WriteMacInt32(XLM_IRQ_NEST, int32(ReadMacInt32(XLM_IRQ_NEST)) - 1); |
1437 |
+ |
#else |
1438 |
|
atomic_add((int *)XLM_IRQ_NEST, -1); |
1439 |
+ |
#endif |
1440 |
|
} |
1441 |
|
|
1442 |
|
|
1444 |
|
* USR2 handler |
1445 |
|
*/ |
1446 |
|
|
1447 |
< |
#if EMULATED_PPC |
1448 |
< |
static void sigusr2_handler(int sig) |
1447 |
> |
#if !EMULATED_PPC |
1448 |
> |
void sigusr2_handler(int sig, siginfo_t *sip, void *scp) |
1449 |
|
{ |
1450 |
< |
#if ASYNC_IRQ |
1451 |
< |
extern void HandleInterrupt(void); |
1452 |
< |
HandleInterrupt(); |
1450 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1451 |
> |
|
1452 |
> |
#ifdef SYSTEM_CLOBBERS_R2 |
1453 |
> |
// Restore pointer to Thread Local Storage |
1454 |
> |
set_r2(TOC); |
1455 |
> |
#endif |
1456 |
> |
#ifdef SYSTEM_CLOBBERS_R13 |
1457 |
> |
// Restore pointer to .sdata section |
1458 |
> |
set_r13(R13); |
1459 |
> |
#endif |
1460 |
> |
|
1461 |
> |
#ifdef USE_SDL_VIDEO |
1462 |
> |
// We must fill in the events queue in the same thread that did call SDL_SetVideoMode() |
1463 |
> |
SDL_PumpEvents(); |
1464 |
|
#endif |
1322 |
– |
} |
1323 |
– |
#else |
1324 |
– |
static void sigusr2_handler(int sig, sigcontext_struct *sc) |
1325 |
– |
{ |
1326 |
– |
pt_regs *r = sc->regs; |
1465 |
|
|
1466 |
|
// Do nothing if interrupts are disabled |
1467 |
|
if (*(int32 *)XLM_IRQ_NEST > 0) |
1475 |
|
case MODE_68K: |
1476 |
|
// 68k emulator active, trigger 68k interrupt level 1 |
1477 |
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1478 |
< |
r->ccr |= ntohl(kernel_data->v[0x674 >> 2]); |
1478 |
> |
r->cr() |= ntohl(kernel_data->v[0x674 >> 2]); |
1479 |
|
break; |
1480 |
|
|
1481 |
|
#if INTERRUPTS_IN_NATIVE_MODE |
1482 |
|
case MODE_NATIVE: |
1483 |
|
// 68k emulator inactive, in nanokernel? |
1484 |
< |
if (r->gpr[1] != KernelDataAddr) { |
1484 |
> |
if (r->gpr(1) != KernelDataAddr) { |
1485 |
> |
|
1486 |
> |
// Set extra stack for SIGSEGV handler |
1487 |
> |
sigaltstack(&extra_stack, NULL); |
1488 |
> |
|
1489 |
|
// Prepare for 68k interrupt level 1 |
1490 |
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1491 |
|
WriteMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc, ReadMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc) | ntohl(kernel_data->v[0x674 >> 2])); |
1492 |
|
|
1493 |
|
// Execute nanokernel interrupt routine (this will activate the 68k emulator) |
1494 |
< |
atomic_add((int32 *)XLM_IRQ_NEST, 1); |
1494 |
> |
DisableInterrupt(); |
1495 |
|
if (ROMType == ROMTYPE_NEWWORLD) |
1496 |
|
ppc_interrupt(ROM_BASE + 0x312b1c, KernelDataAddr); |
1497 |
|
else |
1498 |
|
ppc_interrupt(ROM_BASE + 0x312a3c, KernelDataAddr); |
1499 |
+ |
|
1500 |
+ |
// Reset normal stack |
1501 |
+ |
sigaltstack(&sig_stack, NULL); |
1502 |
|
} |
1503 |
|
break; |
1504 |
|
#endif |
1509 |
|
if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) { |
1510 |
|
|
1511 |
|
// Set extra stack for SIGSEGV handler |
1512 |
< |
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); |
1512 |
> |
sigaltstack(&extra_stack, NULL); |
1513 |
|
#if 1 |
1514 |
|
// Execute full 68k interrupt routine |
1515 |
|
M68kRegisters r; |
1531 |
|
if (InterruptFlags & INTFLAG_VIA) { |
1532 |
|
ClearInterruptFlag(INTFLAG_VIA); |
1533 |
|
ADBInterrupt(); |
1534 |
< |
ExecutePPC(VideoVBL); |
1534 |
> |
ExecuteNative(NATIVE_VIDEO_VBL); |
1535 |
|
} |
1536 |
|
} |
1537 |
|
#endif |
1538 |
< |
// Reset normal signal stack |
1539 |
< |
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); |
1538 |
> |
// Reset normal stack |
1539 |
> |
sigaltstack(&sig_stack, NULL); |
1540 |
|
} |
1541 |
|
break; |
1542 |
|
#endif |
1550 |
|
*/ |
1551 |
|
|
1552 |
|
#if !EMULATED_PPC |
1553 |
< |
static void sigsegv_handler(int sig, sigcontext_struct *sc) |
1553 |
> |
static void sigsegv_handler(int sig, siginfo_t *sip, void *scp) |
1554 |
|
{ |
1555 |
< |
pt_regs *r = sc->regs; |
1555 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1556 |
|
|
1557 |
|
// Get effective address |
1558 |
< |
uint32 addr = r->dar; |
1558 |
> |
uint32 addr = r->dar(); |
1559 |
|
|
1560 |
+ |
#ifdef SYSTEM_CLOBBERS_R2 |
1561 |
+ |
// Restore pointer to Thread Local Storage |
1562 |
+ |
set_r2(TOC); |
1563 |
+ |
#endif |
1564 |
+ |
#ifdef SYSTEM_CLOBBERS_R13 |
1565 |
+ |
// Restore pointer to .sdata section |
1566 |
+ |
set_r13(R13); |
1567 |
+ |
#endif |
1568 |
+ |
|
1569 |
|
#if ENABLE_VOSF |
1570 |
|
// Handle screen fault. |
1571 |
|
extern bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction); |
1572 |
< |
if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->nip)) |
1572 |
> |
if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->pc())) |
1573 |
|
return; |
1574 |
|
#endif |
1575 |
|
|
1576 |
|
num_segv++; |
1577 |
|
|
1578 |
< |
// Fault in Mac ROM or RAM? |
1579 |
< |
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
1578 |
> |
// Fault in Mac ROM or RAM or DR Cache? |
1579 |
> |
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)); |
1580 |
|
if (mac_fault) { |
1581 |
|
|
1582 |
|
// "VM settings" during MacOS 8 installation |
1583 |
< |
if (r->nip == ROM_BASE + 0x488160 && r->gpr[20] == 0xf8000000) { |
1584 |
< |
r->nip += 4; |
1585 |
< |
r->gpr[8] = 0; |
1583 |
> |
if (r->pc() == ROM_BASE + 0x488160 && r->gpr(20) == 0xf8000000) { |
1584 |
> |
r->pc() += 4; |
1585 |
> |
r->gpr(8) = 0; |
1586 |
|
return; |
1587 |
|
|
1588 |
|
// MacOS 8.5 installation |
1589 |
< |
} else if (r->nip == ROM_BASE + 0x488140 && r->gpr[16] == 0xf8000000) { |
1590 |
< |
r->nip += 4; |
1591 |
< |
r->gpr[8] = 0; |
1589 |
> |
} else if (r->pc() == ROM_BASE + 0x488140 && r->gpr(16) == 0xf8000000) { |
1590 |
> |
r->pc() += 4; |
1591 |
> |
r->gpr(8) = 0; |
1592 |
|
return; |
1593 |
|
|
1594 |
|
// MacOS 8 serial drivers on startup |
1595 |
< |
} else if (r->nip == ROM_BASE + 0x48e080 && (r->gpr[8] == 0xf3012002 || r->gpr[8] == 0xf3012000)) { |
1596 |
< |
r->nip += 4; |
1597 |
< |
r->gpr[8] = 0; |
1595 |
> |
} else if (r->pc() == ROM_BASE + 0x48e080 && (r->gpr(8) == 0xf3012002 || r->gpr(8) == 0xf3012000)) { |
1596 |
> |
r->pc() += 4; |
1597 |
> |
r->gpr(8) = 0; |
1598 |
|
return; |
1599 |
|
|
1600 |
|
// MacOS 8.1 serial drivers on startup |
1601 |
< |
} else if (r->nip == ROM_BASE + 0x48c5e0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1602 |
< |
r->nip += 4; |
1601 |
> |
} else if (r->pc() == ROM_BASE + 0x48c5e0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1602 |
> |
r->pc() += 4; |
1603 |
> |
return; |
1604 |
> |
} else if (r->pc() == ROM_BASE + 0x4a10a0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1605 |
> |
r->pc() += 4; |
1606 |
> |
return; |
1607 |
> |
|
1608 |
> |
// MacOS 8.6 serial drivers on startup (with DR Cache and OldWorld ROM) |
1609 |
> |
} else if ((r->pc() - DR_CACHE_BASE) < DR_CACHE_SIZE && (r->gpr(16) == 0xf3012002 || r->gpr(16) == 0xf3012000)) { |
1610 |
> |
r->pc() += 4; |
1611 |
|
return; |
1612 |
< |
} else if (r->nip == ROM_BASE + 0x4a10a0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1613 |
< |
r->nip += 4; |
1612 |
> |
} else if ((r->pc() - DR_CACHE_BASE) < DR_CACHE_SIZE && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1613 |
> |
r->pc() += 4; |
1614 |
|
return; |
1615 |
|
} |
1616 |
|
|
1617 |
|
// Get opcode and divide into fields |
1618 |
< |
uint32 opcode = *((uint32 *)r->nip); |
1618 |
> |
uint32 opcode = *((uint32 *)r->pc()); |
1619 |
|
uint32 primop = opcode >> 26; |
1620 |
|
uint32 exop = (opcode >> 1) & 0x3ff; |
1621 |
|
uint32 ra = (opcode >> 16) & 0x1f; |
1704 |
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_NORM; break; |
1705 |
|
case 45: // sthu |
1706 |
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_U; break; |
1707 |
+ |
#if EMULATE_UNALIGNED_LOADSTORE_MULTIPLE |
1708 |
+ |
case 46: // lmw |
1709 |
+ |
if ((addr % 4) != 0) { |
1710 |
+ |
uint32 ea = addr; |
1711 |
+ |
D(bug("WARNING: unaligned lmw to EA=%08x from IP=%08x\n", ea, r->pc())); |
1712 |
+ |
for (int i = rd; i <= 31; i++) { |
1713 |
+ |
r->gpr(i) = ReadMacInt32(ea); |
1714 |
+ |
ea += 4; |
1715 |
+ |
} |
1716 |
+ |
r->pc() += 4; |
1717 |
+ |
goto rti; |
1718 |
+ |
} |
1719 |
+ |
break; |
1720 |
+ |
case 47: // stmw |
1721 |
+ |
if ((addr % 4) != 0) { |
1722 |
+ |
uint32 ea = addr; |
1723 |
+ |
D(bug("WARNING: unaligned stmw to EA=%08x from IP=%08x\n", ea, r->pc())); |
1724 |
+ |
for (int i = rd; i <= 31; i++) { |
1725 |
+ |
WriteMacInt32(ea, r->gpr(i)); |
1726 |
+ |
ea += 4; |
1727 |
+ |
} |
1728 |
+ |
r->pc() += 4; |
1729 |
+ |
goto rti; |
1730 |
+ |
} |
1731 |
+ |
break; |
1732 |
+ |
#endif |
1733 |
|
} |
1734 |
|
|
1735 |
< |
// Ignore ROM writes |
1736 |
< |
if (transfer_type == TYPE_STORE && addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) { |
1737 |
< |
// 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)); |
1735 |
> |
// Ignore ROM writes (including to the zero page, which is read-only) |
1736 |
> |
if (transfer_type == TYPE_STORE && |
1737 |
> |
((addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) || |
1738 |
> |
(addr >= SheepMem::ZeroPage() && addr < SheepMem::ZeroPage() + SheepMem::PageSize()))) { |
1739 |
> |
// 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())); |
1740 |
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1741 |
< |
r->gpr[ra] = addr; |
1742 |
< |
r->nip += 4; |
1741 |
> |
r->gpr(ra) = addr; |
1742 |
> |
r->pc() += 4; |
1743 |
|
goto rti; |
1744 |
|
} |
1745 |
|
|
1746 |
|
// Ignore illegal memory accesses? |
1747 |
|
if (PrefsFindBool("ignoresegv")) { |
1748 |
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1749 |
< |
r->gpr[ra] = addr; |
1749 |
> |
r->gpr(ra) = addr; |
1750 |
|
if (transfer_type == TYPE_LOAD) |
1751 |
< |
r->gpr[rd] = 0; |
1752 |
< |
r->nip += 4; |
1751 |
> |
r->gpr(rd) = 0; |
1752 |
> |
r->pc() += 4; |
1753 |
|
goto rti; |
1754 |
|
} |
1755 |
|
|
1757 |
|
if (!PrefsFindBool("nogui")) { |
1758 |
|
char str[256]; |
1759 |
|
if (transfer_type == TYPE_LOAD || transfer_type == TYPE_STORE) |
1760 |
< |
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]); |
1760 |
> |
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)); |
1761 |
|
else |
1762 |
< |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode); |
1762 |
> |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode); |
1763 |
|
ErrorAlert(str); |
1764 |
|
QuitEmulator(); |
1765 |
|
return; |
1767 |
|
} |
1768 |
|
|
1769 |
|
// For all other errors, jump into debugger (sort of...) |
1770 |
+ |
crash_reason = (sig == SIGBUS) ? "SIGBUS" : "SIGSEGV"; |
1771 |
|
if (!ready_for_signals) { |
1772 |
< |
printf("SIGSEGV\n"); |
1773 |
< |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
1772 |
> |
printf("%s\n"); |
1773 |
> |
printf(" sigcontext %p, machine_regs %p\n", scp, r); |
1774 |
|
printf( |
1775 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1776 |
|
" xer %08lx cr %08lx \n" |
1782 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1783 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1784 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1785 |
< |
r->nip, r->link, r->ctr, r->msr, |
1786 |
< |
r->xer, r->ccr, |
1787 |
< |
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1788 |
< |
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
1789 |
< |
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
1790 |
< |
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
1791 |
< |
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
1792 |
< |
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
1793 |
< |
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
1794 |
< |
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
1785 |
> |
crash_reason, |
1786 |
> |
r->pc(), r->lr(), r->ctr(), r->msr(), |
1787 |
> |
r->xer(), r->cr(), |
1788 |
> |
r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3), |
1789 |
> |
r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7), |
1790 |
> |
r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11), |
1791 |
> |
r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15), |
1792 |
> |
r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19), |
1793 |
> |
r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23), |
1794 |
> |
r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27), |
1795 |
> |
r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31)); |
1796 |
|
exit(1); |
1797 |
|
QuitEmulator(); |
1798 |
|
return; |
1799 |
|
} else { |
1800 |
|
// We crashed. Save registers, tell tick thread and loop forever |
1801 |
< |
sigsegv_regs = *(sigregs *)r; |
1801 |
> |
build_sigregs(&sigsegv_regs, r); |
1802 |
|
emul_thread_fatal = true; |
1803 |
|
for (;;) ; |
1804 |
|
} |
1810 |
|
* SIGILL handler |
1811 |
|
*/ |
1812 |
|
|
1813 |
< |
static void sigill_handler(int sig, sigcontext_struct *sc) |
1813 |
> |
static void sigill_handler(int sig, siginfo_t *sip, void *scp) |
1814 |
|
{ |
1815 |
< |
pt_regs *r = sc->regs; |
1815 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1816 |
|
char str[256]; |
1817 |
|
|
1818 |
+ |
#ifdef SYSTEM_CLOBBERS_R2 |
1819 |
+ |
// Restore pointer to Thread Local Storage |
1820 |
+ |
set_r2(TOC); |
1821 |
+ |
#endif |
1822 |
+ |
#ifdef SYSTEM_CLOBBERS_R13 |
1823 |
+ |
// Restore pointer to .sdata section |
1824 |
+ |
set_r13(R13); |
1825 |
+ |
#endif |
1826 |
+ |
|
1827 |
|
// Fault in Mac ROM or RAM? |
1828 |
< |
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
1828 |
> |
bool mac_fault = (r->pc() >= ROM_BASE) && (r->pc() < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize)); |
1829 |
|
if (mac_fault) { |
1830 |
|
|
1831 |
|
// Get opcode and divide into fields |
1832 |
< |
uint32 opcode = *((uint32 *)r->nip); |
1832 |
> |
uint32 opcode = *((uint32 *)r->pc()); |
1833 |
|
uint32 primop = opcode >> 26; |
1834 |
|
uint32 exop = (opcode >> 1) & 0x3ff; |
1835 |
|
uint32 ra = (opcode >> 16) & 0x1f; |
1840 |
|
switch (primop) { |
1841 |
|
case 9: // POWER instructions |
1842 |
|
case 22: |
1843 |
< |
power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->nip, r->gpr[1], opcode); |
1843 |
> |
power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->pc(), r->gpr(1), opcode); |
1844 |
|
ErrorAlert(str); |
1845 |
|
QuitEmulator(); |
1846 |
|
return; |
1848 |
|
case 31: |
1849 |
|
switch (exop) { |
1850 |
|
case 83: // mfmsr |
1851 |
< |
r->gpr[rd] = 0xf072; |
1852 |
< |
r->nip += 4; |
1851 |
> |
r->gpr(rd) = 0xf072; |
1852 |
> |
r->pc() += 4; |
1853 |
|
goto rti; |
1854 |
|
|
1855 |
|
case 210: // mtsr |
1856 |
|
case 242: // mtsrin |
1857 |
|
case 306: // tlbie |
1858 |
< |
r->nip += 4; |
1858 |
> |
r->pc() += 4; |
1859 |
|
goto rti; |
1860 |
|
|
1861 |
|
case 339: { // mfspr |
1871 |
|
case 957: // PMC3 |
1872 |
|
case 958: // PMC4 |
1873 |
|
case 959: // SDA |
1874 |
< |
r->nip += 4; |
1874 |
> |
r->pc() += 4; |
1875 |
|
goto rti; |
1876 |
|
case 25: // SDR1 |
1877 |
< |
r->gpr[rd] = 0xdead001f; |
1878 |
< |
r->nip += 4; |
1877 |
> |
r->gpr(rd) = 0xdead001f; |
1878 |
> |
r->pc() += 4; |
1879 |
|
goto rti; |
1880 |
|
case 287: // PVR |
1881 |
< |
r->gpr[rd] = PVR; |
1882 |
< |
r->nip += 4; |
1881 |
> |
r->gpr(rd) = PVR; |
1882 |
> |
r->pc() += 4; |
1883 |
|
goto rti; |
1884 |
|
} |
1885 |
|
break; |
1915 |
|
case 957: // PMC3 |
1916 |
|
case 958: // PMC4 |
1917 |
|
case 959: // SDA |
1918 |
< |
r->nip += 4; |
1918 |
> |
r->pc() += 4; |
1919 |
|
goto rti; |
1920 |
|
} |
1921 |
|
break; |
1934 |
|
|
1935 |
|
// In GUI mode, show error alert |
1936 |
|
if (!PrefsFindBool("nogui")) { |
1937 |
< |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode); |
1937 |
> |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode); |
1938 |
|
ErrorAlert(str); |
1939 |
|
QuitEmulator(); |
1940 |
|
return; |
1942 |
|
} |
1943 |
|
|
1944 |
|
// For all other errors, jump into debugger (sort of...) |
1945 |
+ |
crash_reason = "SIGILL"; |
1946 |
|
if (!ready_for_signals) { |
1947 |
< |
printf("SIGILL\n"); |
1948 |
< |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
1947 |
> |
printf("%s\n"); |
1948 |
> |
printf(" sigcontext %p, machine_regs %p\n", scp, r); |
1949 |
|
printf( |
1950 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1951 |
|
" xer %08lx cr %08lx \n" |
1957 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1958 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1959 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1960 |
< |
r->nip, r->link, r->ctr, r->msr, |
1961 |
< |
r->xer, r->ccr, |
1962 |
< |
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1963 |
< |
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
1964 |
< |
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
1965 |
< |
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
1966 |
< |
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
1967 |
< |
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
1968 |
< |
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
1969 |
< |
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
1960 |
> |
crash_reason, |
1961 |
> |
r->pc(), r->lr(), r->ctr(), r->msr(), |
1962 |
> |
r->xer(), r->cr(), |
1963 |
> |
r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3), |
1964 |
> |
r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7), |
1965 |
> |
r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11), |
1966 |
> |
r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15), |
1967 |
> |
r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19), |
1968 |
> |
r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23), |
1969 |
> |
r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27), |
1970 |
> |
r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31)); |
1971 |
|
exit(1); |
1972 |
|
QuitEmulator(); |
1973 |
|
return; |
1974 |
|
} else { |
1975 |
|
// We crashed. Save registers, tell tick thread and loop forever |
1976 |
< |
sigsegv_regs = *(sigregs *)r; |
1976 |
> |
build_sigregs(&sigsegv_regs, r); |
1977 |
|
emul_thread_fatal = true; |
1978 |
|
for (;;) ; |
1979 |
|
} |
1983 |
|
|
1984 |
|
|
1985 |
|
/* |
1986 |
+ |
* Helpers to share 32-bit addressable data with MacOS |
1987 |
+ |
*/ |
1988 |
+ |
|
1989 |
+ |
bool SheepMem::Init(void) |
1990 |
+ |
{ |
1991 |
+ |
// Size of a native page |
1992 |
+ |
page_size = getpagesize(); |
1993 |
+ |
|
1994 |
+ |
// Allocate SheepShaver globals |
1995 |
+ |
proc = base; |
1996 |
+ |
if (vm_mac_acquire(base, size) < 0) |
1997 |
+ |
return false; |
1998 |
+ |
|
1999 |
+ |
// Allocate page with all bits set to 0, right in the middle |
2000 |
+ |
// This is also used to catch undesired overlaps between proc and data areas |
2001 |
+ |
zero_page = proc + (size / 2); |
2002 |
+ |
Mac_memset(zero_page, 0, page_size); |
2003 |
+ |
if (vm_protect(Mac2HostAddr(zero_page), page_size, VM_PAGE_READ) < 0) |
2004 |
+ |
return false; |
2005 |
+ |
|
2006 |
+ |
#if EMULATED_PPC |
2007 |
+ |
// Allocate alternate stack for PowerPC interrupt routine |
2008 |
+ |
sig_stack = base + size; |
2009 |
+ |
if (vm_mac_acquire(sig_stack, SIG_STACK_SIZE) < 0) |
2010 |
+ |
return false; |
2011 |
+ |
#endif |
2012 |
+ |
|
2013 |
+ |
data = base + size; |
2014 |
+ |
return true; |
2015 |
+ |
} |
2016 |
+ |
|
2017 |
+ |
void SheepMem::Exit(void) |
2018 |
+ |
{ |
2019 |
+ |
if (data) { |
2020 |
+ |
// Delete SheepShaver globals |
2021 |
+ |
vm_mac_release(base, size); |
2022 |
+ |
|
2023 |
+ |
#if EMULATED_PPC |
2024 |
+ |
// Delete alternate stack for PowerPC interrupt routine |
2025 |
+ |
vm_mac_release(sig_stack, SIG_STACK_SIZE); |
2026 |
+ |
#endif |
2027 |
+ |
} |
2028 |
+ |
} |
2029 |
+ |
|
2030 |
+ |
|
2031 |
+ |
/* |
2032 |
|
* Display alert |
2033 |
|
*/ |
2034 |
|
|
2077 |
|
|
2078 |
|
void ErrorAlert(const char *text) |
2079 |
|
{ |
2080 |
< |
#ifdef ENABLE_GTK |
2080 |
> |
#if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO) |
2081 |
|
if (PrefsFindBool("nogui") || x_display == NULL) { |
2082 |
|
printf(GetString(STR_SHELL_ERROR_PREFIX), text); |
2083 |
|
return; |
2096 |
|
|
2097 |
|
void WarningAlert(const char *text) |
2098 |
|
{ |
2099 |
< |
#ifdef ENABLE_GTK |
2099 |
> |
#if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO) |
2100 |
|
if (PrefsFindBool("nogui") || x_display == NULL) { |
2101 |
|
printf(GetString(STR_SHELL_WARNING_PREFIX), text); |
2102 |
|
return; |