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 |
< |
static char *x_display_name = NULL; // X11 display name |
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 |
249 |
< |
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 |
236 |
< |
extern int atomic_add(int *var, int v); |
237 |
< |
extern int atomic_and(int *var, int v); |
238 |
< |
extern int atomic_or(int *var, int v); |
239 |
< |
#else |
240 |
< |
extern "C" void *get_toc(void); |
262 |
> |
#if !EMULATED_PPC |
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); |
277 |
|
#endif |
278 |
|
|
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 |
+ |
|
295 |
+ |
#if HAVE_SPINLOCKS |
296 |
+ |
static spinlock_t atomic_ops_lock = SPIN_LOCK_UNLOCKED; |
297 |
+ |
#else |
298 |
+ |
#define spin_lock(LOCK) |
299 |
+ |
#define spin_unlock(LOCK) |
300 |
+ |
#endif |
301 |
+ |
|
302 |
+ |
int atomic_add(int *var, int v) |
303 |
+ |
{ |
304 |
+ |
spin_lock(&atomic_ops_lock); |
305 |
+ |
int ret = *var; |
306 |
+ |
*var += v; |
307 |
+ |
spin_unlock(&atomic_ops_lock); |
308 |
+ |
return ret; |
309 |
+ |
} |
310 |
+ |
|
311 |
+ |
int atomic_and(int *var, int v) |
312 |
+ |
{ |
313 |
+ |
spin_lock(&atomic_ops_lock); |
314 |
+ |
int ret = *var; |
315 |
+ |
*var &= v; |
316 |
+ |
spin_unlock(&atomic_ops_lock); |
317 |
+ |
return ret; |
318 |
+ |
} |
319 |
+ |
|
320 |
+ |
int atomic_or(int *var, int v) |
321 |
+ |
{ |
322 |
+ |
spin_lock(&atomic_ops_lock); |
323 |
+ |
int ret = *var; |
324 |
+ |
*var |= v; |
325 |
+ |
spin_unlock(&atomic_ops_lock); |
326 |
+ |
return ret; |
327 |
+ |
} |
328 |
+ |
#endif |
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 |
|
*/ |
359 |
|
int main(int argc, char **argv) |
360 |
|
{ |
361 |
|
char str[256]; |
270 |
– |
uint32 *boot_globs; |
271 |
– |
int16 i16; |
272 |
– |
int drive, driver; |
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 |
+ |
{ 0x7fff0000, 0x00810000, "82xx" }, |
593 |
+ |
{ 0x7fff0000, 0x00820000, "8280" }, |
594 |
+ |
{ 0xffff0000, 0x00400000, "Power3 (630)" }, |
595 |
+ |
{ 0xffff0000, 0x00410000, "Power3 (630+)" }, |
596 |
+ |
{ 0xffff0000, 0x00360000, "I-star" }, |
597 |
+ |
{ 0xffff0000, 0x00370000, "S-star" }, |
598 |
+ |
{ 0xffff0000, 0x00350000, "Power4" }, |
599 |
+ |
{ 0xffff0000, 0x00390000, "PPC970" }, |
600 |
+ |
{ 0xffff0000, 0x003a0000, "POWER5" }, |
601 |
+ |
{ 0, 0, 0 } |
602 |
+ |
}; |
603 |
+ |
|
604 |
|
char line[256]; |
605 |
|
while(fgets(line, 255, proc_file)) { |
606 |
|
// Read line |
612 |
|
// Parse line |
613 |
|
int i; |
614 |
|
char value[256]; |
615 |
< |
if (sscanf(line, "cpu : %s", value) == 1) { |
616 |
< |
if (strcmp(value, "601") == 0) |
617 |
< |
PVR = 0x00010000; |
618 |
< |
else if (strcmp(value, "603") == 0) |
619 |
< |
PVR = 0x00030000; |
620 |
< |
else if (strcmp(value, "604") == 0) |
621 |
< |
PVR = 0x00040000; |
622 |
< |
else if (strcmp(value, "603e") == 0) |
623 |
< |
PVR = 0x00060000; |
624 |
< |
else if (strcmp(value, "603ev") == 0) |
625 |
< |
PVR = 0x00070000; |
364 |
< |
else if (strcmp(value, "604e") == 0) |
365 |
< |
PVR = 0x00090000; |
366 |
< |
else if (strcmp(value, "604ev5") == 0) |
367 |
< |
PVR = 0x000a0000; |
368 |
< |
else if (strcmp(value, "750") == 0) |
369 |
< |
PVR = 0x00080000; |
370 |
< |
else if (strcmp(value, "821") == 0) |
371 |
< |
PVR = 0x00320000; |
372 |
< |
else if (strcmp(value, "860") == 0) |
373 |
< |
PVR = 0x00500000; |
374 |
< |
else |
615 |
> |
if (sscanf(line, "cpu : %[0-9A-Za-a]", value) == 1) { |
616 |
> |
// Search by name |
617 |
> |
const char *cpu_name = NULL; |
618 |
> |
for (int i = 0; cpu_specs[i].pvr_mask != 0; i++) { |
619 |
> |
if (strcmp(cpu_specs[i].cpu_name, value) == 0) { |
620 |
> |
cpu_name = cpu_specs[i].cpu_name; |
621 |
> |
PVR = cpu_specs[i].pvr_value; |
622 |
> |
break; |
623 |
> |
} |
624 |
> |
} |
625 |
> |
if (cpu_name == NULL) |
626 |
|
printf("WARNING: Unknown CPU type '%s', assuming 604\n", value); |
627 |
+ |
else |
628 |
+ |
printf("Found a PowerPC %s processor\n", cpu_name); |
629 |
|
} |
630 |
|
if (sscanf(line, "clock : %dMHz", &i) == 1) |
631 |
|
CPUClockSpeed = BusClockSpeed = i * 1000000; |
635 |
|
sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno)); |
636 |
|
WarningAlert(str); |
637 |
|
} |
638 |
+ |
|
639 |
+ |
// Get actual bus frequency |
640 |
+ |
proc_file = fopen("/proc/device-tree/clock-frequency", "r"); |
641 |
+ |
if (proc_file) { |
642 |
+ |
union { uint8 b[4]; uint32 l; } value; |
643 |
+ |
if (fread(value.b, sizeof(value), 1, proc_file) == 1) |
644 |
+ |
BusClockSpeed = value.l; |
645 |
+ |
fclose(proc_file); |
646 |
+ |
} |
647 |
+ |
|
648 |
+ |
// Get actual timebase frequency |
649 |
+ |
TimebaseSpeed = BusClockSpeed / 4; |
650 |
+ |
DIR *cpus_dir; |
651 |
+ |
if ((cpus_dir = opendir("/proc/device-tree/cpus")) != NULL) { |
652 |
+ |
struct dirent *cpu_entry; |
653 |
+ |
while ((cpu_entry = readdir(cpus_dir)) != NULL) { |
654 |
+ |
if (strstr(cpu_entry->d_name, "PowerPC,") == cpu_entry->d_name) { |
655 |
+ |
char timebase_freq_node[256]; |
656 |
+ |
sprintf(timebase_freq_node, "/proc/device-tree/cpus/%s/timebase-frequency", cpu_entry->d_name); |
657 |
+ |
proc_file = fopen(timebase_freq_node, "r"); |
658 |
+ |
if (proc_file) { |
659 |
+ |
union { uint8 b[4]; uint32 l; } value; |
660 |
+ |
if (fread(value.b, sizeof(value), 1, proc_file) == 1) |
661 |
+ |
TimebaseSpeed = value.l; |
662 |
+ |
fclose(proc_file); |
663 |
+ |
} |
664 |
+ |
} |
665 |
+ |
} |
666 |
+ |
closedir(cpus_dir); |
667 |
+ |
} |
668 |
|
#endif |
669 |
+ |
// Remap any newer G4/G5 processor to plain G4 for compatibility |
670 |
+ |
switch (PVR >> 16) { |
671 |
+ |
case 0x8000: // 7450 |
672 |
+ |
case 0x8001: // 7455 |
673 |
+ |
case 0x8002: // 7457 |
674 |
+ |
case 0x8003: // 7447A |
675 |
+ |
case 0x0039: // 970 |
676 |
+ |
PVR = 0x000c0000; // 7400 |
677 |
+ |
break; |
678 |
+ |
} |
679 |
|
D(bug("PVR: %08x (assumed)\n", PVR)); |
680 |
|
|
681 |
|
// Init system routines |
699 |
|
goto quit; |
700 |
|
} |
701 |
|
|
702 |
+ |
#ifndef PAGEZERO_HACK |
703 |
|
// Create Low Memory area (0x0000..0x3000) |
704 |
< |
if (vm_acquire_fixed((char *)0, 0x3000) < 0) { |
704 |
> |
if (vm_mac_acquire(0, 0x3000) < 0) { |
705 |
|
sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno)); |
706 |
|
ErrorAlert(str); |
707 |
|
goto quit; |
708 |
|
} |
709 |
|
lm_area_mapped = true; |
710 |
+ |
#endif |
711 |
|
|
712 |
|
// Create areas for Kernel Data |
713 |
< |
kernel_area = shmget(IPC_PRIVATE, KERNEL_AREA_SIZE, 0600); |
714 |
< |
if (kernel_area == -1) { |
715 |
< |
sprintf(str, GetString(STR_KD_SHMGET_ERR), strerror(errno)); |
713 |
> |
if (!kernel_data_init()) |
714 |
> |
goto quit; |
715 |
> |
kernel_data = (KernelData *)Mac2HostAddr(KERNEL_DATA_BASE); |
716 |
> |
emulator_data = &kernel_data->ed; |
717 |
> |
KernelDataAddr = KERNEL_DATA_BASE; |
718 |
> |
D(bug("Kernel Data at %p (%08x)\n", kernel_data, KERNEL_DATA_BASE)); |
719 |
> |
D(bug("Emulator Data at %p (%08x)\n", emulator_data, KERNEL_DATA_BASE + offsetof(KernelData, ed))); |
720 |
> |
|
721 |
> |
// Create area for DR Cache |
722 |
> |
if (vm_mac_acquire(DR_EMULATOR_BASE, DR_EMULATOR_SIZE) < 0) { |
723 |
> |
sprintf(str, GetString(STR_DR_EMULATOR_MMAP_ERR), strerror(errno)); |
724 |
|
ErrorAlert(str); |
725 |
|
goto quit; |
726 |
|
} |
727 |
< |
if (shmat(kernel_area, (void *)KERNEL_DATA_BASE, 0) < 0) { |
728 |
< |
sprintf(str, GetString(STR_KD_SHMAT_ERR), strerror(errno)); |
727 |
> |
dr_emulator_area_mapped = true; |
728 |
> |
if (vm_mac_acquire(DR_CACHE_BASE, DR_CACHE_SIZE) < 0) { |
729 |
> |
sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno)); |
730 |
|
ErrorAlert(str); |
731 |
|
goto quit; |
732 |
|
} |
733 |
< |
if (shmat(kernel_area, (void *)KERNEL_DATA2_BASE, 0) < 0) { |
734 |
< |
sprintf(str, GetString(STR_KD2_SHMAT_ERR), strerror(errno)); |
733 |
> |
dr_cache_area_mapped = true; |
734 |
> |
#if !EMULATED_PPC |
735 |
> |
if (vm_protect((char *)DR_CACHE_BASE, DR_CACHE_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
736 |
> |
sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno)); |
737 |
|
ErrorAlert(str); |
738 |
|
goto quit; |
739 |
|
} |
740 |
< |
kernel_data = (KernelData *)0x68ffe000; |
741 |
< |
emulator_data = &kernel_data->ed; |
742 |
< |
KernelDataAddr = (uint32)kernel_data; |
437 |
< |
D(bug("Kernel Data at %p, Emulator Data at %p\n", kernel_data, emulator_data)); |
740 |
> |
#endif |
741 |
> |
DRCacheAddr = DR_CACHE_BASE; |
742 |
> |
D(bug("DR Cache at %p\n", DRCacheAddr)); |
743 |
|
|
744 |
|
// Create area for SheepShaver data |
745 |
< |
if (vm_acquire_fixed((char *)SHEEP_BASE, SHEEP_SIZE) < 0) { |
745 |
> |
if (!SheepMem::Init()) { |
746 |
|
sprintf(str, GetString(STR_SHEEP_MEM_MMAP_ERR), strerror(errno)); |
747 |
|
ErrorAlert(str); |
748 |
|
goto quit; |
749 |
|
} |
445 |
– |
SheepStack1Base = SHEEP_BASE + 0x10000; |
446 |
– |
SheepStack2Base = SheepStack1Base + 0x10000; |
447 |
– |
SheepThunksBase = SheepStack2Base + 0x1000; |
448 |
– |
sheep_area_mapped = true; |
750 |
|
|
751 |
|
// Create area for Mac ROM |
752 |
< |
if (vm_acquire_fixed((char *)ROM_BASE, ROM_AREA_SIZE) < 0) { |
752 |
> |
if (vm_mac_acquire(ROM_BASE, ROM_AREA_SIZE) < 0) { |
753 |
|
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
754 |
|
ErrorAlert(str); |
755 |
|
goto quit; |
756 |
|
} |
757 |
< |
#if !EMULATED_PPC || defined(__powerpc__) |
758 |
< |
if (vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
757 |
> |
ROMBaseHost = Mac2HostAddr(ROM_BASE); |
758 |
> |
#if !EMULATED_PPC |
759 |
> |
if (vm_protect(ROMBaseHost, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
760 |
|
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
761 |
|
ErrorAlert(str); |
762 |
|
goto quit; |
763 |
|
} |
764 |
|
#endif |
765 |
|
rom_area_mapped = true; |
766 |
< |
D(bug("ROM area at %08x\n", ROM_BASE)); |
766 |
> |
D(bug("ROM area at %p (%08x)\n", ROMBaseHost, ROM_BASE)); |
767 |
|
|
768 |
|
// Create area for Mac RAM |
769 |
|
RAMSize = PrefsFindInt32("ramsize"); |
772 |
|
RAMSize = 8*1024*1024; |
773 |
|
} |
774 |
|
|
775 |
< |
if (vm_acquire_fixed((char *)RAM_BASE, RAMSize) < 0) { |
775 |
> |
if (vm_mac_acquire(RAM_BASE, RAMSize) < 0) { |
776 |
|
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
777 |
|
ErrorAlert(str); |
778 |
|
goto quit; |
779 |
|
} |
780 |
+ |
RAMBaseHost = Mac2HostAddr(RAM_BASE); |
781 |
|
#if !EMULATED_PPC |
782 |
< |
if (vm_protect((char *)RAM_BASE, RAMSize, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
782 |
> |
if (vm_protect(RAMBaseHost, RAMSize, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
783 |
|
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
784 |
|
ErrorAlert(str); |
785 |
|
goto quit; |
787 |
|
#endif |
788 |
|
RAMBase = RAM_BASE; |
789 |
|
ram_area_mapped = true; |
790 |
< |
D(bug("RAM area at %08x\n", RAMBase)); |
790 |
> |
D(bug("RAM area at %p (%08x)\n", RAMBaseHost, RAMBase)); |
791 |
|
|
792 |
|
if (RAMBase > ROM_BASE) { |
793 |
|
ErrorAlert(GetString(STR_RAM_HIGHER_THAN_ROM_ERR)); |
823 |
|
} |
824 |
|
delete[] rom_tmp; |
825 |
|
|
826 |
< |
// Load NVRAM |
827 |
< |
XPRAMInit(); |
525 |
< |
|
526 |
< |
// Set boot volume |
527 |
< |
drive = PrefsFindInt32("bootdrive"); |
528 |
< |
XPRAM[0x1378] = i16 >> 8; |
529 |
< |
XPRAM[0x1379] = i16 & 0xff; |
530 |
< |
driver = PrefsFindInt32("bootdriver"); |
531 |
< |
XPRAM[0x137a] = i16 >> 8; |
532 |
< |
XPRAM[0x137b] = i16 & 0xff; |
533 |
< |
|
534 |
< |
// Create BootGlobs at top of Mac memory |
535 |
< |
memset((void *)(RAMBase + RAMSize - 4096), 0, 4096); |
536 |
< |
BootGlobsAddr = RAMBase + RAMSize - 0x1c; |
537 |
< |
boot_globs = (uint32 *)BootGlobsAddr; |
538 |
< |
boot_globs[-5] = htonl(RAMBase + RAMSize); // MemTop |
539 |
< |
boot_globs[0] = htonl(RAMBase); // First RAM bank |
540 |
< |
boot_globs[1] = htonl(RAMSize); |
541 |
< |
boot_globs[2] = htonl((uint32)-1); // End of bank table |
542 |
< |
|
543 |
< |
// Init drivers |
544 |
< |
SonyInit(); |
545 |
< |
DiskInit(); |
546 |
< |
CDROMInit(); |
547 |
< |
SCSIInit(); |
548 |
< |
|
549 |
< |
// Init external file system |
550 |
< |
ExtFSInit(); |
551 |
< |
|
552 |
< |
// Init audio |
553 |
< |
AudioInit(); |
554 |
< |
|
555 |
< |
// Init network |
556 |
< |
EtherInit(); |
557 |
< |
|
558 |
< |
// Init serial ports |
559 |
< |
SerialInit(); |
560 |
< |
|
561 |
< |
// Init Time Manager |
562 |
< |
TimerInit(); |
563 |
< |
|
564 |
< |
// Init clipboard |
565 |
< |
ClipInit(); |
566 |
< |
|
567 |
< |
// Init video |
568 |
< |
if (!VideoInit()) |
569 |
< |
goto quit; |
570 |
< |
|
571 |
< |
// Install ROM patches |
572 |
< |
if (!PatchROM()) { |
573 |
< |
ErrorAlert(GetString(STR_UNSUPPORTED_ROM_TYPE_ERR)); |
826 |
> |
// Initialize everything |
827 |
> |
if (!InitAll()) |
828 |
|
goto quit; |
829 |
< |
} |
829 |
> |
D(bug("Initialization complete\n")); |
830 |
|
|
831 |
|
// Clear caches (as we loaded and patched code) and write protect ROM |
832 |
|
#if !EMULATED_PPC |
833 |
< |
MakeExecutable(0, (void *)ROM_BASE, ROM_AREA_SIZE); |
580 |
< |
#endif |
581 |
< |
vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_EXECUTE); |
582 |
< |
|
583 |
< |
// Initialize Kernel Data |
584 |
< |
memset(kernel_data, 0, sizeof(KernelData)); |
585 |
< |
if (ROMType == ROMTYPE_NEWWORLD) { |
586 |
< |
static uint32 of_dev_tree[4] = {0, 0, 0, 0}; |
587 |
< |
static uint8 vector_lookup_tbl[128]; |
588 |
< |
static uint8 vector_mask_tbl[64]; |
589 |
< |
memset((uint8 *)kernel_data + 0xb80, 0x3d, 0x80); |
590 |
< |
memset(vector_lookup_tbl, 0, 128); |
591 |
< |
memset(vector_mask_tbl, 0, 64); |
592 |
< |
kernel_data->v[0xb80 >> 2] = htonl(ROM_BASE); |
593 |
< |
kernel_data->v[0xb84 >> 2] = htonl((uint32)of_dev_tree); // OF device tree base |
594 |
< |
kernel_data->v[0xb90 >> 2] = htonl((uint32)vector_lookup_tbl); |
595 |
< |
kernel_data->v[0xb94 >> 2] = htonl((uint32)vector_mask_tbl); |
596 |
< |
kernel_data->v[0xb98 >> 2] = htonl(ROM_BASE); // OpenPIC base |
597 |
< |
kernel_data->v[0xbb0 >> 2] = htonl(0); // ADB base |
598 |
< |
kernel_data->v[0xc20 >> 2] = htonl(RAMSize); |
599 |
< |
kernel_data->v[0xc24 >> 2] = htonl(RAMSize); |
600 |
< |
kernel_data->v[0xc30 >> 2] = htonl(RAMSize); |
601 |
< |
kernel_data->v[0xc34 >> 2] = htonl(RAMSize); |
602 |
< |
kernel_data->v[0xc38 >> 2] = htonl(0x00010020); |
603 |
< |
kernel_data->v[0xc3c >> 2] = htonl(0x00200001); |
604 |
< |
kernel_data->v[0xc40 >> 2] = htonl(0x00010000); |
605 |
< |
kernel_data->v[0xc50 >> 2] = htonl(RAMBase); |
606 |
< |
kernel_data->v[0xc54 >> 2] = htonl(RAMSize); |
607 |
< |
kernel_data->v[0xf60 >> 2] = htonl(PVR); |
608 |
< |
kernel_data->v[0xf64 >> 2] = htonl(CPUClockSpeed); |
609 |
< |
kernel_data->v[0xf68 >> 2] = htonl(BusClockSpeed); |
610 |
< |
kernel_data->v[0xf6c >> 2] = htonl(CPUClockSpeed); |
611 |
< |
} else { |
612 |
< |
kernel_data->v[0xc80 >> 2] = htonl(RAMSize); |
613 |
< |
kernel_data->v[0xc84 >> 2] = htonl(RAMSize); |
614 |
< |
kernel_data->v[0xc90 >> 2] = htonl(RAMSize); |
615 |
< |
kernel_data->v[0xc94 >> 2] = htonl(RAMSize); |
616 |
< |
kernel_data->v[0xc98 >> 2] = htonl(0x00010020); |
617 |
< |
kernel_data->v[0xc9c >> 2] = htonl(0x00200001); |
618 |
< |
kernel_data->v[0xca0 >> 2] = htonl(0x00010000); |
619 |
< |
kernel_data->v[0xcb0 >> 2] = htonl(RAMBase); |
620 |
< |
kernel_data->v[0xcb4 >> 2] = htonl(RAMSize); |
621 |
< |
kernel_data->v[0xf80 >> 2] = htonl(PVR); |
622 |
< |
kernel_data->v[0xf84 >> 2] = htonl(CPUClockSpeed); |
623 |
< |
kernel_data->v[0xf88 >> 2] = htonl(BusClockSpeed); |
624 |
< |
kernel_data->v[0xf8c >> 2] = htonl(CPUClockSpeed); |
625 |
< |
} |
626 |
< |
|
627 |
< |
// Initialize extra low memory |
628 |
< |
D(bug("Initializing Low Memory...\n")); |
629 |
< |
memset(NULL, 0, 0x3000); |
630 |
< |
WriteMacInt32(XLM_SIGNATURE, FOURCC('B','a','a','h')); // Signature to detect SheepShaver |
631 |
< |
WriteMacInt32(XLM_KERNEL_DATA, (uint32)kernel_data); // For trap replacement routines |
632 |
< |
WriteMacInt32(XLM_PVR, PVR); // Theoretical PVR |
633 |
< |
WriteMacInt32(XLM_BUS_CLOCK, BusClockSpeed); // For DriverServicesLib patch |
634 |
< |
WriteMacInt16(XLM_EXEC_RETURN_OPCODE, M68K_EXEC_RETURN); // For Execute68k() (RTS from the executed 68k code will jump here and end 68k mode) |
635 |
< |
#if EMULATED_PPC |
636 |
< |
WriteMacInt32(XLM_ETHER_INIT, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_INIT)); |
637 |
< |
WriteMacInt32(XLM_ETHER_TERM, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_TERM)); |
638 |
< |
WriteMacInt32(XLM_ETHER_OPEN, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_OPEN)); |
639 |
< |
WriteMacInt32(XLM_ETHER_CLOSE, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_CLOSE)); |
640 |
< |
WriteMacInt32(XLM_ETHER_WPUT, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_WPUT)); |
641 |
< |
WriteMacInt32(XLM_ETHER_RSRV, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_RSRV)); |
642 |
< |
WriteMacInt32(XLM_VIDEO_DOIO, POWERPC_NATIVE_OP_FUNC(NATIVE_VIDEO_DO_DRIVER_IO)); |
643 |
< |
#else |
644 |
< |
WriteMacInt32(XLM_TOC, (uint32)TOC); // TOC pointer of emulator |
645 |
< |
WriteMacInt32(XLM_ETHER_INIT, (uint32)InitStreamModule); // DLPI ethernet driver functions |
646 |
< |
WriteMacInt32(XLM_ETHER_TERM, (uint32)TerminateStreamModule); |
647 |
< |
WriteMacInt32(XLM_ETHER_OPEN, (uint32)ether_open); |
648 |
< |
WriteMacInt32(XLM_ETHER_CLOSE, (uint32)ether_close); |
649 |
< |
WriteMacInt32(XLM_ETHER_WPUT, (uint32)ether_wput); |
650 |
< |
WriteMacInt32(XLM_ETHER_RSRV, (uint32)ether_rsrv); |
651 |
< |
WriteMacInt32(XLM_VIDEO_DOIO, (uint32)VideoDoDriverIO); |
833 |
> |
flush_icache_range(ROM_BASE, ROM_BASE + ROM_AREA_SIZE); |
834 |
|
#endif |
835 |
< |
D(bug("Low Memory initialized\n")); |
835 |
> |
vm_protect(ROMBaseHost, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_EXECUTE); |
836 |
|
|
837 |
|
// Start 60Hz thread |
838 |
+ |
tick_thread_cancel = false; |
839 |
|
tick_thread_active = (pthread_create(&tick_thread, NULL, tick_func, NULL) == 0); |
840 |
|
D(bug("Tick thread installed (%ld)\n", tick_thread)); |
841 |
|
|
842 |
|
// Start NVRAM watchdog thread |
843 |
|
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
844 |
+ |
nvram_thread_cancel = false; |
845 |
|
nvram_thread_active = (pthread_create(&nvram_thread, NULL, nvram_func, NULL) == 0); |
846 |
|
D(bug("NVRAM thread installed (%ld)\n", nvram_thread)); |
847 |
|
|
848 |
|
#if !EMULATED_PPC |
665 |
– |
// Create and install stacks for signal handlers |
666 |
– |
sig_stack = malloc(SIG_STACK_SIZE); |
667 |
– |
D(bug("Signal stack at %p\n", sig_stack)); |
668 |
– |
if (sig_stack == NULL) { |
669 |
– |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
670 |
– |
goto quit; |
671 |
– |
} |
672 |
– |
extra_stack = malloc(SIG_STACK_SIZE); |
673 |
– |
D(bug("Extra stack at %p\n", extra_stack)); |
674 |
– |
if (extra_stack == NULL) { |
675 |
– |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
676 |
– |
goto quit; |
677 |
– |
} |
678 |
– |
struct sigaltstack new_stack; |
679 |
– |
new_stack.ss_sp = sig_stack; |
680 |
– |
new_stack.ss_flags = 0; |
681 |
– |
new_stack.ss_size = SIG_STACK_SIZE; |
682 |
– |
if (sigaltstack(&new_stack, NULL) < 0) { |
683 |
– |
sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno)); |
684 |
– |
ErrorAlert(str); |
685 |
– |
goto quit; |
686 |
– |
} |
687 |
– |
#endif |
688 |
– |
|
689 |
– |
#if !EMULATED_PPC |
690 |
– |
// Install SIGSEGV handler |
691 |
– |
sigemptyset(&sigsegv_action.sa_mask); // Block interrupts during SEGV handling |
692 |
– |
sigaddset(&sigsegv_action.sa_mask, SIGUSR2); |
693 |
– |
sigsegv_action.sa_handler = (__sighandler_t)sigsegv_handler; |
694 |
– |
sigsegv_action.sa_flags = SA_ONSTACK; |
695 |
– |
sigsegv_action.sa_restorer = NULL; |
696 |
– |
if (sigaction(SIGSEGV, &sigsegv_action, NULL) < 0) { |
697 |
– |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
698 |
– |
ErrorAlert(str); |
699 |
– |
goto quit; |
700 |
– |
} |
701 |
– |
|
849 |
|
// Install SIGILL handler |
850 |
|
sigemptyset(&sigill_action.sa_mask); // Block interrupts during ILL handling |
851 |
|
sigaddset(&sigill_action.sa_mask, SIGUSR2); |
852 |
< |
sigill_action.sa_handler = (__sighandler_t)sigill_handler; |
853 |
< |
sigill_action.sa_flags = SA_ONSTACK; |
852 |
> |
sigill_action.sa_sigaction = sigill_handler; |
853 |
> |
sigill_action.sa_flags = SA_ONSTACK | SA_SIGINFO; |
854 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
855 |
|
sigill_action.sa_restorer = NULL; |
856 |
+ |
#endif |
857 |
|
if (sigaction(SIGILL, &sigill_action, NULL) < 0) { |
858 |
|
sprintf(str, GetString(STR_SIGILL_INSTALL_ERR), strerror(errno)); |
859 |
|
ErrorAlert(str); |
861 |
|
} |
862 |
|
#endif |
863 |
|
|
864 |
+ |
#if !EMULATED_PPC |
865 |
|
// Install interrupt signal handler |
866 |
|
sigemptyset(&sigusr2_action.sa_mask); |
867 |
< |
sigusr2_action.sa_handler = (__sighandler_t)sigusr2_handler; |
868 |
< |
sigusr2_action.sa_flags = 0; |
869 |
< |
#if !EMULATED_PPC |
720 |
< |
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART; |
721 |
< |
#endif |
867 |
> |
sigusr2_action.sa_sigaction = sigusr2_handler_init; |
868 |
> |
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART | SA_SIGINFO; |
869 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
870 |
|
sigusr2_action.sa_restorer = NULL; |
871 |
+ |
#endif |
872 |
|
if (sigaction(SIGUSR2, &sigusr2_action, NULL) < 0) { |
873 |
|
sprintf(str, GetString(STR_SIGUSR2_INSTALL_ERR), strerror(errno)); |
874 |
|
ErrorAlert(str); |
875 |
|
goto quit; |
876 |
|
} |
877 |
+ |
#endif |
878 |
|
|
879 |
|
// Get my thread ID and execute MacOS thread function |
880 |
|
emul_thread = pthread_self(); |
893 |
|
|
894 |
|
static void Quit(void) |
895 |
|
{ |
896 |
+ |
#if EMULATED_PPC |
897 |
+ |
// Exit PowerPC emulation |
898 |
+ |
exit_emul_ppc(); |
899 |
+ |
#endif |
900 |
+ |
|
901 |
|
// Stop 60Hz thread |
902 |
|
if (tick_thread_active) { |
903 |
+ |
tick_thread_cancel = true; |
904 |
|
pthread_cancel(tick_thread); |
905 |
|
pthread_join(tick_thread, NULL); |
906 |
|
} |
907 |
|
|
908 |
|
// Stop NVRAM watchdog thread |
909 |
|
if (nvram_thread_active) { |
910 |
+ |
nvram_thread_cancel = true; |
911 |
|
pthread_cancel(nvram_thread); |
912 |
|
pthread_join(nvram_thread, NULL); |
913 |
|
} |
914 |
|
|
915 |
|
#if !EMULATED_PPC |
916 |
< |
// Uninstall SIGSEGV handler |
916 |
> |
// Uninstall SIGSEGV and SIGBUS handlers |
917 |
|
sigemptyset(&sigsegv_action.sa_mask); |
918 |
|
sigsegv_action.sa_handler = SIG_DFL; |
919 |
|
sigsegv_action.sa_flags = 0; |
920 |
|
sigaction(SIGSEGV, &sigsegv_action, NULL); |
921 |
+ |
sigaction(SIGBUS, &sigsegv_action, NULL); |
922 |
|
|
923 |
|
// Uninstall SIGILL handler |
924 |
|
sigemptyset(&sigill_action.sa_mask); |
925 |
|
sigill_action.sa_handler = SIG_DFL; |
926 |
|
sigill_action.sa_flags = 0; |
927 |
|
sigaction(SIGILL, &sigill_action, NULL); |
770 |
– |
#endif |
771 |
– |
|
772 |
– |
// Save NVRAM |
773 |
– |
XPRAMExit(); |
774 |
– |
|
775 |
– |
// Exit clipboard |
776 |
– |
ClipExit(); |
777 |
– |
|
778 |
– |
// Exit Time Manager |
779 |
– |
TimerExit(); |
780 |
– |
|
781 |
– |
// Exit serial |
782 |
– |
SerialExit(); |
783 |
– |
|
784 |
– |
// Exit network |
785 |
– |
EtherExit(); |
928 |
|
|
929 |
< |
// Exit audio |
930 |
< |
AudioExit(); |
931 |
< |
|
932 |
< |
// Exit video |
933 |
< |
VideoExit(); |
929 |
> |
// Delete stacks for signal handlers |
930 |
> |
if (sig_stack.ss_sp) |
931 |
> |
free(sig_stack.ss_sp); |
932 |
> |
if (extra_stack.ss_sp) |
933 |
> |
free(extra_stack.ss_sp); |
934 |
> |
#endif |
935 |
|
|
936 |
< |
// Exit external file system |
937 |
< |
ExtFSExit(); |
936 |
> |
// Deinitialize everything |
937 |
> |
ExitAll(); |
938 |
|
|
939 |
< |
// Exit drivers |
940 |
< |
SCSIExit(); |
798 |
< |
CDROMExit(); |
799 |
< |
DiskExit(); |
800 |
< |
SonyExit(); |
939 |
> |
// Delete SheepShaver globals |
940 |
> |
SheepMem::Exit(); |
941 |
|
|
942 |
|
// Delete RAM area |
943 |
|
if (ram_area_mapped) |
944 |
< |
vm_release((char *)RAM_BASE, RAMSize); |
944 |
> |
vm_mac_release(RAM_BASE, RAMSize); |
945 |
|
|
946 |
|
// Delete ROM area |
947 |
|
if (rom_area_mapped) |
948 |
< |
vm_release((char *)ROM_BASE, ROM_AREA_SIZE); |
948 |
> |
vm_mac_release(ROM_BASE, ROM_AREA_SIZE); |
949 |
> |
|
950 |
> |
// Delete DR cache areas |
951 |
> |
if (dr_emulator_area_mapped) |
952 |
> |
vm_mac_release(DR_EMULATOR_BASE, DR_EMULATOR_SIZE); |
953 |
> |
if (dr_cache_area_mapped) |
954 |
> |
vm_mac_release(DR_CACHE_BASE, DR_CACHE_SIZE); |
955 |
|
|
956 |
|
// Delete Kernel Data area |
957 |
< |
if (kernel_area >= 0) { |
812 |
< |
shmdt((void *)KERNEL_DATA_BASE); |
813 |
< |
shmdt((void *)KERNEL_DATA2_BASE); |
814 |
< |
shmctl(kernel_area, IPC_RMID, NULL); |
815 |
< |
} |
957 |
> |
kernel_data_exit(); |
958 |
|
|
959 |
|
// Delete Low Memory area |
960 |
|
if (lm_area_mapped) |
961 |
< |
munmap((char *)0x0000, 0x3000); |
961 |
> |
vm_mac_release(0, 0x3000); |
962 |
|
|
963 |
|
// Close /dev/zero |
964 |
|
if (zero_fd > 0) |
976 |
|
#endif |
977 |
|
|
978 |
|
// Close X11 server connection |
979 |
+ |
#ifndef USE_SDL_VIDEO |
980 |
|
if (x_display) |
981 |
|
XCloseDisplay(x_display); |
982 |
+ |
#endif |
983 |
|
|
984 |
|
exit(0); |
985 |
|
} |
986 |
|
|
987 |
|
|
988 |
|
/* |
989 |
+ |
* Initialize Kernel Data segments |
990 |
+ |
*/ |
991 |
+ |
|
992 |
+ |
#if defined(__CYGWIN__) |
993 |
+ |
#define WIN32_LEAN_AND_MEAN |
994 |
+ |
#include <windows.h> |
995 |
+ |
|
996 |
+ |
static HANDLE kernel_handle; // Shared memory handle for Kernel Data |
997 |
+ |
static DWORD allocation_granule; // Minimum size of allocateable are (64K) |
998 |
+ |
static DWORD kernel_area_size; // Size of Kernel Data area |
999 |
+ |
#endif |
1000 |
+ |
|
1001 |
+ |
static bool kernel_data_init(void) |
1002 |
+ |
{ |
1003 |
+ |
char str[256]; |
1004 |
+ |
#ifdef _WIN32 |
1005 |
+ |
SYSTEM_INFO si; |
1006 |
+ |
GetSystemInfo(&si); |
1007 |
+ |
allocation_granule = si.dwAllocationGranularity; |
1008 |
+ |
kernel_area_size = (KERNEL_AREA_SIZE + allocation_granule - 1) & -allocation_granule; |
1009 |
+ |
|
1010 |
+ |
char rcs[10]; |
1011 |
+ |
LPVOID kernel_addr; |
1012 |
+ |
kernel_handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, kernel_area_size, NULL); |
1013 |
+ |
if (kernel_handle == NULL) { |
1014 |
+ |
sprintf(rcs, "%d", GetLastError()); |
1015 |
+ |
sprintf(str, GetString(STR_KD_SHMGET_ERR), rcs); |
1016 |
+ |
ErrorAlert(str); |
1017 |
+ |
return false; |
1018 |
+ |
} |
1019 |
+ |
kernel_addr = (LPVOID)Mac2HostAddr(KERNEL_DATA_BASE & -allocation_granule); |
1020 |
+ |
if (MapViewOfFileEx(kernel_handle, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, kernel_area_size, kernel_addr) != kernel_addr) { |
1021 |
+ |
sprintf(rcs, "%d", GetLastError()); |
1022 |
+ |
sprintf(str, GetString(STR_KD_SHMAT_ERR), rcs); |
1023 |
+ |
ErrorAlert(str); |
1024 |
+ |
return false; |
1025 |
+ |
} |
1026 |
+ |
kernel_addr = (LPVOID)Mac2HostAddr(KERNEL_DATA2_BASE & -allocation_granule); |
1027 |
+ |
if (MapViewOfFileEx(kernel_handle, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, kernel_area_size, kernel_addr) != kernel_addr) { |
1028 |
+ |
sprintf(rcs, "%d", GetLastError()); |
1029 |
+ |
sprintf(str, GetString(STR_KD2_SHMAT_ERR), rcs); |
1030 |
+ |
ErrorAlert(str); |
1031 |
+ |
return false; |
1032 |
+ |
} |
1033 |
+ |
#else |
1034 |
+ |
kernel_area = shmget(IPC_PRIVATE, KERNEL_AREA_SIZE, 0600); |
1035 |
+ |
if (kernel_area == -1) { |
1036 |
+ |
sprintf(str, GetString(STR_KD_SHMGET_ERR), strerror(errno)); |
1037 |
+ |
ErrorAlert(str); |
1038 |
+ |
return false; |
1039 |
+ |
} |
1040 |
+ |
if (shmat(kernel_area, Mac2HostAddr(KERNEL_DATA_BASE), 0) < 0) { |
1041 |
+ |
sprintf(str, GetString(STR_KD_SHMAT_ERR), strerror(errno)); |
1042 |
+ |
ErrorAlert(str); |
1043 |
+ |
return false; |
1044 |
+ |
} |
1045 |
+ |
if (shmat(kernel_area, Mac2HostAddr(KERNEL_DATA2_BASE), 0) < 0) { |
1046 |
+ |
sprintf(str, GetString(STR_KD2_SHMAT_ERR), strerror(errno)); |
1047 |
+ |
ErrorAlert(str); |
1048 |
+ |
return false; |
1049 |
+ |
} |
1050 |
+ |
#endif |
1051 |
+ |
return true; |
1052 |
+ |
} |
1053 |
+ |
|
1054 |
+ |
|
1055 |
+ |
/* |
1056 |
+ |
* Deallocate Kernel Data segments |
1057 |
+ |
*/ |
1058 |
+ |
|
1059 |
+ |
static void kernel_data_exit(void) |
1060 |
+ |
{ |
1061 |
+ |
#ifdef _WIN32 |
1062 |
+ |
if (kernel_handle) { |
1063 |
+ |
UnmapViewOfFile(Mac2HostAddr(KERNEL_DATA_BASE & -allocation_granule)); |
1064 |
+ |
UnmapViewOfFile(Mac2HostAddr(KERNEL_DATA2_BASE & -allocation_granule)); |
1065 |
+ |
CloseHandle(kernel_handle); |
1066 |
+ |
} |
1067 |
+ |
#else |
1068 |
+ |
if (kernel_area >= 0) { |
1069 |
+ |
shmdt(Mac2HostAddr(KERNEL_DATA_BASE)); |
1070 |
+ |
shmdt(Mac2HostAddr(KERNEL_DATA2_BASE)); |
1071 |
+ |
shmctl(kernel_area, IPC_RMID, NULL); |
1072 |
+ |
} |
1073 |
+ |
#endif |
1074 |
+ |
} |
1075 |
+ |
|
1076 |
+ |
|
1077 |
+ |
/* |
1078 |
|
* Jump into Mac ROM, start 680x0 emulator |
1079 |
|
*/ |
1080 |
|
|
1081 |
|
#if EMULATED_PPC |
849 |
– |
extern void emul_ppc(uint32 start); |
850 |
– |
extern void init_emul_ppc(void); |
1082 |
|
void jump_to_rom(uint32 entry) |
1083 |
|
{ |
1084 |
|
init_emul_ppc(); |
1143 |
|
uint16 proc[2] = {trap, M68K_RTS}; |
1144 |
|
Execute68k((uint32)proc, r); |
1145 |
|
} |
915 |
– |
|
916 |
– |
|
917 |
– |
/* |
918 |
– |
* Execute PPC code from EMUL_OP routine (real mode switch) |
919 |
– |
*/ |
920 |
– |
|
921 |
– |
void ExecutePPC(void (*func)()) |
922 |
– |
{ |
923 |
– |
uint32 tvect[2] = {(uint32)func, 0}; // Fake TVECT |
924 |
– |
RoutineDescriptor desc = BUILD_PPC_ROUTINE_DESCRIPTOR(0, tvect); |
925 |
– |
M68kRegisters r; |
926 |
– |
Execute68k((uint32)&desc, &r); |
927 |
– |
} |
1146 |
|
#endif |
1147 |
|
|
1148 |
|
|
1161 |
|
|
1162 |
|
|
1163 |
|
/* |
946 |
– |
* Pause/resume emulator |
947 |
– |
*/ |
948 |
– |
|
949 |
– |
void PauseEmulator(void) |
950 |
– |
{ |
951 |
– |
pthread_kill(emul_thread, SIGSTOP); |
952 |
– |
} |
953 |
– |
|
954 |
– |
void ResumeEmulator(void) |
955 |
– |
{ |
956 |
– |
pthread_kill(emul_thread, SIGCONT); |
957 |
– |
} |
958 |
– |
|
959 |
– |
|
960 |
– |
/* |
1164 |
|
* Dump 68k registers |
1165 |
|
*/ |
1166 |
|
|
1188 |
|
* Make code executable |
1189 |
|
*/ |
1190 |
|
|
1191 |
< |
void MakeExecutable(int dummy, void *start, uint32 length) |
1191 |
> |
void MakeExecutable(int dummy, uint32 start, uint32 length) |
1192 |
|
{ |
1193 |
< |
if (((uintptr)start >= ROM_BASE) && ((uintptr)start < (ROM_BASE + ROM_SIZE))) |
1193 |
> |
if ((start >= ROM_BASE) && (start < (ROM_BASE + ROM_SIZE))) |
1194 |
|
return; |
1195 |
|
#if EMULATED_PPC |
1196 |
< |
FlushCodeCache((uintptr)start, (uintptr)start + length); |
1196 |
> |
FlushCodeCache(start, start + length); |
1197 |
|
#else |
1198 |
< |
flush_icache_range(start, (void *)((uintptr)start + length)); |
1198 |
> |
flush_icache_range(start, start + length); |
1199 |
|
#endif |
1200 |
|
} |
1201 |
|
|
1202 |
|
|
1203 |
|
/* |
1204 |
< |
* Patch things after system startup (gets called by disk driver accRun routine) |
1204 |
> |
* NVRAM watchdog thread (saves NVRAM every minute) |
1205 |
|
*/ |
1206 |
|
|
1207 |
< |
void PatchAfterStartup(void) |
1207 |
> |
static void nvram_watchdog(void) |
1208 |
|
{ |
1209 |
< |
#if EMULATED_PPC |
1210 |
< |
ExecuteNative(NATIVE_VIDEO_INSTALL_ACCEL); |
1211 |
< |
#else |
1212 |
< |
ExecutePPC(VideoInstallAccel); |
1010 |
< |
#endif |
1011 |
< |
InstallExtFS(); |
1209 |
> |
if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) { |
1210 |
> |
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
1211 |
> |
SaveXPRAM(); |
1212 |
> |
} |
1213 |
|
} |
1214 |
|
|
1014 |
– |
|
1015 |
– |
/* |
1016 |
– |
* NVRAM watchdog thread (saves NVRAM every minute) |
1017 |
– |
*/ |
1018 |
– |
|
1215 |
|
static void *nvram_func(void *arg) |
1216 |
|
{ |
1217 |
< |
struct timespec req = {60, 0}; // 1 minute |
1218 |
< |
|
1219 |
< |
for (;;) { |
1220 |
< |
pthread_testcancel(); |
1025 |
< |
nanosleep(&req, NULL); |
1026 |
< |
pthread_testcancel(); |
1027 |
< |
if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) { |
1028 |
< |
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
1029 |
< |
SaveXPRAM(); |
1030 |
< |
} |
1217 |
> |
while (!nvram_thread_cancel) { |
1218 |
> |
for (int i=0; i<60 && !nvram_thread_cancel; i++) |
1219 |
> |
Delay_usec(999999); // Only wait 1 second so we quit promptly when nvram_thread_cancel becomes true |
1220 |
> |
nvram_watchdog(); |
1221 |
|
} |
1222 |
|
return NULL; |
1223 |
|
} |
1230 |
|
static void *tick_func(void *arg) |
1231 |
|
{ |
1232 |
|
int tick_counter = 0; |
1233 |
< |
struct timespec req = {0, 16625000}; |
1233 |
> |
uint64 start = GetTicks_usec(); |
1234 |
> |
int64 ticks = 0; |
1235 |
> |
uint64 next = GetTicks_usec(); |
1236 |
|
|
1237 |
< |
for (;;) { |
1237 |
> |
while (!tick_thread_cancel) { |
1238 |
|
|
1239 |
|
// Wait |
1240 |
< |
nanosleep(&req, NULL); |
1240 |
> |
next += 16625; |
1241 |
> |
int64 delay = next - GetTicks_usec(); |
1242 |
> |
if (delay > 0) |
1243 |
> |
Delay_usec(delay); |
1244 |
> |
else if (delay < -16625) |
1245 |
> |
next = GetTicks_usec(); |
1246 |
> |
ticks++; |
1247 |
|
|
1248 |
|
#if !EMULATED_PPC |
1249 |
|
// Did we crash? |
1250 |
|
if (emul_thread_fatal) { |
1251 |
|
|
1252 |
|
// Yes, dump registers |
1253 |
< |
pt_regs *r = (pt_regs *)&sigsegv_regs; |
1253 |
> |
sigregs *r = &sigsegv_regs; |
1254 |
|
char str[256]; |
1255 |
< |
sprintf(str, "SIGSEGV\n" |
1255 |
> |
if (crash_reason == NULL) |
1256 |
> |
crash_reason = "SIGSEGV"; |
1257 |
> |
sprintf(str, "%s\n" |
1258 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1259 |
|
" xer %08lx cr %08lx \n" |
1260 |
|
" r0 %08lx r1 %08lx r2 %08lx r3 %08lx\n" |
1265 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1266 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1267 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1268 |
+ |
crash_reason, |
1269 |
|
r->nip, r->link, r->ctr, r->msr, |
1270 |
|
r->xer, r->ccr, |
1271 |
|
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1301 |
|
TriggerInterrupt(); |
1302 |
|
} |
1303 |
|
} |
1304 |
+ |
|
1305 |
+ |
uint64 end = GetTicks_usec(); |
1306 |
+ |
D(bug("%lld ticks in %lld usec = %f ticks/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start))); |
1307 |
|
return NULL; |
1308 |
|
} |
1309 |
|
|
1314 |
|
|
1315 |
|
void Set_pthread_attr(pthread_attr_t *attr, int priority) |
1316 |
|
{ |
1317 |
< |
// nothing to do |
1317 |
> |
#ifdef HAVE_PTHREADS |
1318 |
> |
pthread_attr_init(attr); |
1319 |
> |
#if defined(_POSIX_THREAD_PRIORITY_SCHEDULING) |
1320 |
> |
// Some of these only work for superuser |
1321 |
> |
if (geteuid() == 0) { |
1322 |
> |
pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED); |
1323 |
> |
pthread_attr_setschedpolicy(attr, SCHED_FIFO); |
1324 |
> |
struct sched_param fifo_param; |
1325 |
> |
fifo_param.sched_priority = ((sched_get_priority_min(SCHED_FIFO) + |
1326 |
> |
sched_get_priority_max(SCHED_FIFO)) / 2 + |
1327 |
> |
priority); |
1328 |
> |
pthread_attr_setschedparam(attr, &fifo_param); |
1329 |
> |
} |
1330 |
> |
if (pthread_attr_setscope(attr, PTHREAD_SCOPE_SYSTEM) != 0) { |
1331 |
> |
#ifdef PTHREAD_SCOPE_BOUND_NP |
1332 |
> |
// If system scope is not available (eg. we're not running |
1333 |
> |
// with CAP_SCHED_MGT capability on an SGI box), try bound |
1334 |
> |
// scope. It exposes pthread scheduling to the kernel, |
1335 |
> |
// without setting realtime priority. |
1336 |
> |
pthread_attr_setscope(attr, PTHREAD_SCOPE_BOUND_NP); |
1337 |
> |
#endif |
1338 |
> |
} |
1339 |
> |
#endif |
1340 |
> |
#endif |
1341 |
|
} |
1342 |
|
|
1343 |
|
|
1353 |
|
pthread_mutexattr_init(&attr); |
1354 |
|
// Initialize the mutex for priority inheritance -- |
1355 |
|
// required for accurate timing. |
1356 |
< |
#ifdef HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL |
1356 |
> |
#if defined(HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL) && !defined(__CYGWIN__) |
1357 |
|
pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT); |
1358 |
|
#endif |
1359 |
|
#if defined(HAVE_PTHREAD_MUTEXATTR_SETTYPE) && defined(PTHREAD_MUTEX_NORMAL) |
1424 |
|
* Trigger signal USR2 from another thread |
1425 |
|
*/ |
1426 |
|
|
1427 |
< |
#if !EMULATED_PPC || ASYNC_IRQ |
1427 |
> |
#if !EMULATED_PPC |
1428 |
|
void TriggerInterrupt(void) |
1429 |
|
{ |
1430 |
< |
if (ready_for_signals) |
1430 |
> |
if (ready_for_signals) { |
1431 |
> |
idle_resume(); |
1432 |
|
pthread_kill(emul_thread, SIGUSR2); |
1433 |
+ |
} |
1434 |
|
} |
1435 |
|
#endif |
1436 |
|
|
1458 |
|
|
1459 |
|
void DisableInterrupt(void) |
1460 |
|
{ |
1461 |
+ |
#if EMULATED_PPC |
1462 |
+ |
WriteMacInt32(XLM_IRQ_NEST, int32(ReadMacInt32(XLM_IRQ_NEST)) + 1); |
1463 |
+ |
#else |
1464 |
|
atomic_add((int *)XLM_IRQ_NEST, 1); |
1465 |
+ |
#endif |
1466 |
|
} |
1467 |
|
|
1468 |
|
|
1472 |
|
|
1473 |
|
void EnableInterrupt(void) |
1474 |
|
{ |
1475 |
+ |
#if EMULATED_PPC |
1476 |
+ |
WriteMacInt32(XLM_IRQ_NEST, int32(ReadMacInt32(XLM_IRQ_NEST)) - 1); |
1477 |
+ |
#else |
1478 |
|
atomic_add((int *)XLM_IRQ_NEST, -1); |
1479 |
+ |
#endif |
1480 |
|
} |
1481 |
|
|
1482 |
|
|
1484 |
|
* USR2 handler |
1485 |
|
*/ |
1486 |
|
|
1487 |
< |
#if EMULATED_PPC |
1488 |
< |
static void sigusr2_handler(int sig) |
1487 |
> |
#if !EMULATED_PPC |
1488 |
> |
void sigusr2_handler(int sig, siginfo_t *sip, void *scp) |
1489 |
|
{ |
1490 |
< |
#if ASYNC_IRQ |
1491 |
< |
extern void HandleInterrupt(void); |
1492 |
< |
HandleInterrupt(); |
1490 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1491 |
> |
|
1492 |
> |
#ifdef SYSTEM_CLOBBERS_R2 |
1493 |
> |
// Restore pointer to Thread Local Storage |
1494 |
> |
set_r2(TOC); |
1495 |
> |
#endif |
1496 |
> |
#ifdef SYSTEM_CLOBBERS_R13 |
1497 |
> |
// Restore pointer to .sdata section |
1498 |
> |
set_r13(R13); |
1499 |
> |
#endif |
1500 |
> |
|
1501 |
> |
#ifdef USE_SDL_VIDEO |
1502 |
> |
// We must fill in the events queue in the same thread that did call SDL_SetVideoMode() |
1503 |
> |
SDL_PumpEvents(); |
1504 |
|
#endif |
1257 |
– |
} |
1258 |
– |
#else |
1259 |
– |
static void sigusr2_handler(int sig, sigcontext_struct *sc) |
1260 |
– |
{ |
1261 |
– |
pt_regs *r = sc->regs; |
1505 |
|
|
1506 |
|
// Do nothing if interrupts are disabled |
1507 |
|
if (*(int32 *)XLM_IRQ_NEST > 0) |
1515 |
|
case MODE_68K: |
1516 |
|
// 68k emulator active, trigger 68k interrupt level 1 |
1517 |
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1518 |
< |
r->ccr |= ntohl(kernel_data->v[0x674 >> 2]); |
1518 |
> |
r->cr() |= ntohl(kernel_data->v[0x674 >> 2]); |
1519 |
|
break; |
1520 |
|
|
1521 |
|
#if INTERRUPTS_IN_NATIVE_MODE |
1522 |
|
case MODE_NATIVE: |
1523 |
|
// 68k emulator inactive, in nanokernel? |
1524 |
< |
if (r->gpr[1] != KernelDataAddr) { |
1524 |
> |
if (r->gpr(1) != KernelDataAddr) { |
1525 |
> |
|
1526 |
> |
// Set extra stack for SIGSEGV handler |
1527 |
> |
sigaltstack(&extra_stack, NULL); |
1528 |
> |
|
1529 |
|
// Prepare for 68k interrupt level 1 |
1530 |
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1531 |
|
WriteMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc, ReadMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc) | ntohl(kernel_data->v[0x674 >> 2])); |
1532 |
|
|
1533 |
|
// Execute nanokernel interrupt routine (this will activate the 68k emulator) |
1534 |
< |
atomic_add((int32 *)XLM_IRQ_NEST, 1); |
1534 |
> |
DisableInterrupt(); |
1535 |
|
if (ROMType == ROMTYPE_NEWWORLD) |
1536 |
|
ppc_interrupt(ROM_BASE + 0x312b1c, KernelDataAddr); |
1537 |
|
else |
1538 |
|
ppc_interrupt(ROM_BASE + 0x312a3c, KernelDataAddr); |
1539 |
+ |
|
1540 |
+ |
// Reset normal stack |
1541 |
+ |
sigaltstack(&sig_stack, NULL); |
1542 |
|
} |
1543 |
|
break; |
1544 |
|
#endif |
1549 |
|
if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) { |
1550 |
|
|
1551 |
|
// Set extra stack for SIGSEGV handler |
1552 |
< |
struct sigaltstack new_stack; |
1303 |
< |
new_stack.ss_sp = extra_stack; |
1304 |
< |
new_stack.ss_flags = 0; |
1305 |
< |
new_stack.ss_size = SIG_STACK_SIZE; |
1306 |
< |
sigaltstack(&new_stack, NULL); |
1552 |
> |
sigaltstack(&extra_stack, NULL); |
1553 |
|
#if 1 |
1554 |
|
// Execute full 68k interrupt routine |
1555 |
|
M68kRegisters r; |
1571 |
|
if (InterruptFlags & INTFLAG_VIA) { |
1572 |
|
ClearInterruptFlag(INTFLAG_VIA); |
1573 |
|
ADBInterrupt(); |
1574 |
< |
ExecutePPC(VideoVBL); |
1574 |
> |
ExecuteNative(NATIVE_VIDEO_VBL); |
1575 |
|
} |
1576 |
|
} |
1577 |
|
#endif |
1578 |
< |
// Reset normal signal stack |
1579 |
< |
new_stack.ss_sp = sig_stack; |
1334 |
< |
new_stack.ss_flags = 0; |
1335 |
< |
new_stack.ss_size = SIG_STACK_SIZE; |
1336 |
< |
sigaltstack(&new_stack, NULL); |
1578 |
> |
// Reset normal stack |
1579 |
> |
sigaltstack(&sig_stack, NULL); |
1580 |
|
} |
1581 |
|
break; |
1582 |
|
#endif |
1590 |
|
*/ |
1591 |
|
|
1592 |
|
#if !EMULATED_PPC |
1593 |
< |
static void sigsegv_handler(int sig, sigcontext_struct *sc) |
1593 |
> |
static void sigsegv_handler(int sig, siginfo_t *sip, void *scp) |
1594 |
|
{ |
1595 |
< |
pt_regs *r = sc->regs; |
1595 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1596 |
|
|
1597 |
|
// Get effective address |
1598 |
< |
uint32 addr = r->dar; |
1598 |
> |
uint32 addr = r->dar(); |
1599 |
|
|
1600 |
+ |
#ifdef SYSTEM_CLOBBERS_R2 |
1601 |
+ |
// Restore pointer to Thread Local Storage |
1602 |
+ |
set_r2(TOC); |
1603 |
+ |
#endif |
1604 |
+ |
#ifdef SYSTEM_CLOBBERS_R13 |
1605 |
+ |
// Restore pointer to .sdata section |
1606 |
+ |
set_r13(R13); |
1607 |
+ |
#endif |
1608 |
+ |
|
1609 |
|
#if ENABLE_VOSF |
1610 |
|
// Handle screen fault. |
1611 |
|
extern bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction); |
1612 |
< |
if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->nip)) |
1612 |
> |
if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->pc())) |
1613 |
|
return; |
1614 |
|
#endif |
1615 |
|
|
1616 |
|
num_segv++; |
1617 |
|
|
1618 |
< |
// Fault in Mac ROM or RAM? |
1619 |
< |
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
1618 |
> |
// Fault in Mac ROM or RAM or DR Cache? |
1619 |
> |
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)); |
1620 |
|
if (mac_fault) { |
1621 |
|
|
1622 |
|
// "VM settings" during MacOS 8 installation |
1623 |
< |
if (r->nip == ROM_BASE + 0x488160 && r->gpr[20] == 0xf8000000) { |
1624 |
< |
r->nip += 4; |
1625 |
< |
r->gpr[8] = 0; |
1623 |
> |
if (r->pc() == ROM_BASE + 0x488160 && r->gpr(20) == 0xf8000000) { |
1624 |
> |
r->pc() += 4; |
1625 |
> |
r->gpr(8) = 0; |
1626 |
|
return; |
1627 |
|
|
1628 |
|
// MacOS 8.5 installation |
1629 |
< |
} else if (r->nip == ROM_BASE + 0x488140 && r->gpr[16] == 0xf8000000) { |
1630 |
< |
r->nip += 4; |
1631 |
< |
r->gpr[8] = 0; |
1629 |
> |
} else if (r->pc() == ROM_BASE + 0x488140 && r->gpr(16) == 0xf8000000) { |
1630 |
> |
r->pc() += 4; |
1631 |
> |
r->gpr(8) = 0; |
1632 |
|
return; |
1633 |
|
|
1634 |
|
// MacOS 8 serial drivers on startup |
1635 |
< |
} else if (r->nip == ROM_BASE + 0x48e080 && (r->gpr[8] == 0xf3012002 || r->gpr[8] == 0xf3012000)) { |
1636 |
< |
r->nip += 4; |
1637 |
< |
r->gpr[8] = 0; |
1635 |
> |
} else if (r->pc() == ROM_BASE + 0x48e080 && (r->gpr(8) == 0xf3012002 || r->gpr(8) == 0xf3012000)) { |
1636 |
> |
r->pc() += 4; |
1637 |
> |
r->gpr(8) = 0; |
1638 |
|
return; |
1639 |
|
|
1640 |
|
// MacOS 8.1 serial drivers on startup |
1641 |
< |
} else if (r->nip == ROM_BASE + 0x48c5e0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1642 |
< |
r->nip += 4; |
1641 |
> |
} else if (r->pc() == ROM_BASE + 0x48c5e0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1642 |
> |
r->pc() += 4; |
1643 |
> |
return; |
1644 |
> |
} else if (r->pc() == ROM_BASE + 0x4a10a0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1645 |
> |
r->pc() += 4; |
1646 |
> |
return; |
1647 |
> |
|
1648 |
> |
// MacOS 8.6 serial drivers on startup (with DR Cache and OldWorld ROM) |
1649 |
> |
} else if ((r->pc() - DR_CACHE_BASE) < DR_CACHE_SIZE && (r->gpr(16) == 0xf3012002 || r->gpr(16) == 0xf3012000)) { |
1650 |
> |
r->pc() += 4; |
1651 |
|
return; |
1652 |
< |
} else if (r->nip == ROM_BASE + 0x4a10a0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1653 |
< |
r->nip += 4; |
1652 |
> |
} else if ((r->pc() - DR_CACHE_BASE) < DR_CACHE_SIZE && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1653 |
> |
r->pc() += 4; |
1654 |
|
return; |
1655 |
|
} |
1656 |
|
|
1657 |
|
// Get opcode and divide into fields |
1658 |
< |
uint32 opcode = *((uint32 *)r->nip); |
1658 |
> |
uint32 opcode = *((uint32 *)r->pc()); |
1659 |
|
uint32 primop = opcode >> 26; |
1660 |
|
uint32 exop = (opcode >> 1) & 0x3ff; |
1661 |
|
uint32 ra = (opcode >> 16) & 0x1f; |
1744 |
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_NORM; break; |
1745 |
|
case 45: // sthu |
1746 |
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_U; break; |
1747 |
+ |
#if EMULATE_UNALIGNED_LOADSTORE_MULTIPLE |
1748 |
+ |
case 46: // lmw |
1749 |
+ |
if ((addr % 4) != 0) { |
1750 |
+ |
uint32 ea = addr; |
1751 |
+ |
D(bug("WARNING: unaligned lmw to EA=%08x from IP=%08x\n", ea, r->pc())); |
1752 |
+ |
for (int i = rd; i <= 31; i++) { |
1753 |
+ |
r->gpr(i) = ReadMacInt32(ea); |
1754 |
+ |
ea += 4; |
1755 |
+ |
} |
1756 |
+ |
r->pc() += 4; |
1757 |
+ |
goto rti; |
1758 |
+ |
} |
1759 |
+ |
break; |
1760 |
+ |
case 47: // stmw |
1761 |
+ |
if ((addr % 4) != 0) { |
1762 |
+ |
uint32 ea = addr; |
1763 |
+ |
D(bug("WARNING: unaligned stmw to EA=%08x from IP=%08x\n", ea, r->pc())); |
1764 |
+ |
for (int i = rd; i <= 31; i++) { |
1765 |
+ |
WriteMacInt32(ea, r->gpr(i)); |
1766 |
+ |
ea += 4; |
1767 |
+ |
} |
1768 |
+ |
r->pc() += 4; |
1769 |
+ |
goto rti; |
1770 |
+ |
} |
1771 |
+ |
break; |
1772 |
+ |
#endif |
1773 |
|
} |
1774 |
|
|
1775 |
< |
// Ignore ROM writes |
1776 |
< |
if (transfer_type == TYPE_STORE && addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) { |
1777 |
< |
// 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)); |
1775 |
> |
// Ignore ROM writes (including to the zero page, which is read-only) |
1776 |
> |
if (transfer_type == TYPE_STORE && |
1777 |
> |
((addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) || |
1778 |
> |
(addr >= SheepMem::ZeroPage() && addr < SheepMem::ZeroPage() + SheepMem::PageSize()))) { |
1779 |
> |
// 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())); |
1780 |
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1781 |
< |
r->gpr[ra] = addr; |
1782 |
< |
r->nip += 4; |
1781 |
> |
r->gpr(ra) = addr; |
1782 |
> |
r->pc() += 4; |
1783 |
|
goto rti; |
1784 |
|
} |
1785 |
|
|
1786 |
|
// Ignore illegal memory accesses? |
1787 |
|
if (PrefsFindBool("ignoresegv")) { |
1788 |
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1789 |
< |
r->gpr[ra] = addr; |
1789 |
> |
r->gpr(ra) = addr; |
1790 |
|
if (transfer_type == TYPE_LOAD) |
1791 |
< |
r->gpr[rd] = 0; |
1792 |
< |
r->nip += 4; |
1791 |
> |
r->gpr(rd) = 0; |
1792 |
> |
r->pc() += 4; |
1793 |
|
goto rti; |
1794 |
|
} |
1795 |
|
|
1797 |
|
if (!PrefsFindBool("nogui")) { |
1798 |
|
char str[256]; |
1799 |
|
if (transfer_type == TYPE_LOAD || transfer_type == TYPE_STORE) |
1800 |
< |
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]); |
1800 |
> |
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)); |
1801 |
|
else |
1802 |
< |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode); |
1802 |
> |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode); |
1803 |
|
ErrorAlert(str); |
1804 |
|
QuitEmulator(); |
1805 |
|
return; |
1807 |
|
} |
1808 |
|
|
1809 |
|
// For all other errors, jump into debugger (sort of...) |
1810 |
+ |
crash_reason = (sig == SIGBUS) ? "SIGBUS" : "SIGSEGV"; |
1811 |
|
if (!ready_for_signals) { |
1812 |
< |
printf("SIGSEGV\n"); |
1813 |
< |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
1812 |
> |
printf("%s\n"); |
1813 |
> |
printf(" sigcontext %p, machine_regs %p\n", scp, r); |
1814 |
|
printf( |
1815 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1816 |
|
" xer %08lx cr %08lx \n" |
1822 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1823 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1824 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1825 |
< |
r->nip, r->link, r->ctr, r->msr, |
1826 |
< |
r->xer, r->ccr, |
1827 |
< |
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1828 |
< |
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
1829 |
< |
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
1830 |
< |
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
1831 |
< |
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
1832 |
< |
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
1833 |
< |
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
1834 |
< |
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
1825 |
> |
crash_reason, |
1826 |
> |
r->pc(), r->lr(), r->ctr(), r->msr(), |
1827 |
> |
r->xer(), r->cr(), |
1828 |
> |
r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3), |
1829 |
> |
r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7), |
1830 |
> |
r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11), |
1831 |
> |
r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15), |
1832 |
> |
r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19), |
1833 |
> |
r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23), |
1834 |
> |
r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27), |
1835 |
> |
r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31)); |
1836 |
|
exit(1); |
1837 |
|
QuitEmulator(); |
1838 |
|
return; |
1839 |
|
} else { |
1840 |
|
// We crashed. Save registers, tell tick thread and loop forever |
1841 |
< |
sigsegv_regs = *(sigregs *)r; |
1841 |
> |
build_sigregs(&sigsegv_regs, r); |
1842 |
|
emul_thread_fatal = true; |
1843 |
|
for (;;) ; |
1844 |
|
} |
1850 |
|
* SIGILL handler |
1851 |
|
*/ |
1852 |
|
|
1853 |
< |
static void sigill_handler(int sig, sigcontext_struct *sc) |
1853 |
> |
static void sigill_handler(int sig, siginfo_t *sip, void *scp) |
1854 |
|
{ |
1855 |
< |
pt_regs *r = sc->regs; |
1855 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1856 |
|
char str[256]; |
1857 |
|
|
1858 |
+ |
#ifdef SYSTEM_CLOBBERS_R2 |
1859 |
+ |
// Restore pointer to Thread Local Storage |
1860 |
+ |
set_r2(TOC); |
1861 |
+ |
#endif |
1862 |
+ |
#ifdef SYSTEM_CLOBBERS_R13 |
1863 |
+ |
// Restore pointer to .sdata section |
1864 |
+ |
set_r13(R13); |
1865 |
+ |
#endif |
1866 |
+ |
|
1867 |
|
// Fault in Mac ROM or RAM? |
1868 |
< |
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
1868 |
> |
bool mac_fault = (r->pc() >= ROM_BASE) && (r->pc() < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize)); |
1869 |
|
if (mac_fault) { |
1870 |
|
|
1871 |
|
// Get opcode and divide into fields |
1872 |
< |
uint32 opcode = *((uint32 *)r->nip); |
1872 |
> |
uint32 opcode = *((uint32 *)r->pc()); |
1873 |
|
uint32 primop = opcode >> 26; |
1874 |
|
uint32 exop = (opcode >> 1) & 0x3ff; |
1875 |
|
uint32 ra = (opcode >> 16) & 0x1f; |
1880 |
|
switch (primop) { |
1881 |
|
case 9: // POWER instructions |
1882 |
|
case 22: |
1883 |
< |
power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->nip, r->gpr[1], opcode); |
1883 |
> |
power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->pc(), r->gpr(1), opcode); |
1884 |
|
ErrorAlert(str); |
1885 |
|
QuitEmulator(); |
1886 |
|
return; |
1888 |
|
case 31: |
1889 |
|
switch (exop) { |
1890 |
|
case 83: // mfmsr |
1891 |
< |
r->gpr[rd] = 0xf072; |
1892 |
< |
r->nip += 4; |
1891 |
> |
r->gpr(rd) = 0xf072; |
1892 |
> |
r->pc() += 4; |
1893 |
|
goto rti; |
1894 |
|
|
1895 |
|
case 210: // mtsr |
1896 |
|
case 242: // mtsrin |
1897 |
|
case 306: // tlbie |
1898 |
< |
r->nip += 4; |
1898 |
> |
r->pc() += 4; |
1899 |
|
goto rti; |
1900 |
|
|
1901 |
|
case 339: { // mfspr |
1911 |
|
case 957: // PMC3 |
1912 |
|
case 958: // PMC4 |
1913 |
|
case 959: // SDA |
1914 |
< |
r->nip += 4; |
1914 |
> |
r->pc() += 4; |
1915 |
|
goto rti; |
1916 |
|
case 25: // SDR1 |
1917 |
< |
r->gpr[rd] = 0xdead001f; |
1918 |
< |
r->nip += 4; |
1917 |
> |
r->gpr(rd) = 0xdead001f; |
1918 |
> |
r->pc() += 4; |
1919 |
|
goto rti; |
1920 |
|
case 287: // PVR |
1921 |
< |
r->gpr[rd] = PVR; |
1922 |
< |
r->nip += 4; |
1921 |
> |
r->gpr(rd) = PVR; |
1922 |
> |
r->pc() += 4; |
1923 |
|
goto rti; |
1924 |
|
} |
1925 |
|
break; |
1955 |
|
case 957: // PMC3 |
1956 |
|
case 958: // PMC4 |
1957 |
|
case 959: // SDA |
1958 |
< |
r->nip += 4; |
1958 |
> |
r->pc() += 4; |
1959 |
|
goto rti; |
1960 |
|
} |
1961 |
|
break; |
1974 |
|
|
1975 |
|
// In GUI mode, show error alert |
1976 |
|
if (!PrefsFindBool("nogui")) { |
1977 |
< |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode); |
1977 |
> |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode); |
1978 |
|
ErrorAlert(str); |
1979 |
|
QuitEmulator(); |
1980 |
|
return; |
1982 |
|
} |
1983 |
|
|
1984 |
|
// For all other errors, jump into debugger (sort of...) |
1985 |
+ |
crash_reason = "SIGILL"; |
1986 |
|
if (!ready_for_signals) { |
1987 |
< |
printf("SIGILL\n"); |
1988 |
< |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
1987 |
> |
printf("%s\n"); |
1988 |
> |
printf(" sigcontext %p, machine_regs %p\n", scp, r); |
1989 |
|
printf( |
1990 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1991 |
|
" xer %08lx cr %08lx \n" |
1997 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1998 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1999 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
2000 |
< |
r->nip, r->link, r->ctr, r->msr, |
2001 |
< |
r->xer, r->ccr, |
2002 |
< |
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
2003 |
< |
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
2004 |
< |
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
2005 |
< |
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
2006 |
< |
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
2007 |
< |
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
2008 |
< |
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
2009 |
< |
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
2000 |
> |
crash_reason, |
2001 |
> |
r->pc(), r->lr(), r->ctr(), r->msr(), |
2002 |
> |
r->xer(), r->cr(), |
2003 |
> |
r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3), |
2004 |
> |
r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7), |
2005 |
> |
r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11), |
2006 |
> |
r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15), |
2007 |
> |
r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19), |
2008 |
> |
r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23), |
2009 |
> |
r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27), |
2010 |
> |
r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31)); |
2011 |
|
exit(1); |
2012 |
|
QuitEmulator(); |
2013 |
|
return; |
2014 |
|
} else { |
2015 |
|
// We crashed. Save registers, tell tick thread and loop forever |
2016 |
< |
sigsegv_regs = *(sigregs *)r; |
2016 |
> |
build_sigregs(&sigsegv_regs, r); |
2017 |
|
emul_thread_fatal = true; |
2018 |
|
for (;;) ; |
2019 |
|
} |
2023 |
|
|
2024 |
|
|
2025 |
|
/* |
2026 |
+ |
* Helpers to share 32-bit addressable data with MacOS |
2027 |
+ |
*/ |
2028 |
+ |
|
2029 |
+ |
bool SheepMem::Init(void) |
2030 |
+ |
{ |
2031 |
+ |
// Size of a native page |
2032 |
+ |
page_size = getpagesize(); |
2033 |
+ |
|
2034 |
+ |
// Allocate SheepShaver globals |
2035 |
+ |
proc = base; |
2036 |
+ |
if (vm_mac_acquire(base, size) < 0) |
2037 |
+ |
return false; |
2038 |
+ |
|
2039 |
+ |
// Allocate page with all bits set to 0, right in the middle |
2040 |
+ |
// This is also used to catch undesired overlaps between proc and data areas |
2041 |
+ |
zero_page = proc + (size / 2); |
2042 |
+ |
Mac_memset(zero_page, 0, page_size); |
2043 |
+ |
if (vm_protect(Mac2HostAddr(zero_page), page_size, VM_PAGE_READ) < 0) |
2044 |
+ |
return false; |
2045 |
+ |
|
2046 |
+ |
#if EMULATED_PPC |
2047 |
+ |
// Allocate alternate stack for PowerPC interrupt routine |
2048 |
+ |
sig_stack = base + size; |
2049 |
+ |
if (vm_mac_acquire(sig_stack, SIG_STACK_SIZE) < 0) |
2050 |
+ |
return false; |
2051 |
+ |
#endif |
2052 |
+ |
|
2053 |
+ |
data = base + size; |
2054 |
+ |
return true; |
2055 |
+ |
} |
2056 |
+ |
|
2057 |
+ |
void SheepMem::Exit(void) |
2058 |
+ |
{ |
2059 |
+ |
if (data) { |
2060 |
+ |
// Delete SheepShaver globals |
2061 |
+ |
vm_mac_release(base, size); |
2062 |
+ |
|
2063 |
+ |
#if EMULATED_PPC |
2064 |
+ |
// Delete alternate stack for PowerPC interrupt routine |
2065 |
+ |
vm_mac_release(sig_stack, SIG_STACK_SIZE); |
2066 |
+ |
#endif |
2067 |
+ |
} |
2068 |
+ |
} |
2069 |
+ |
|
2070 |
+ |
|
2071 |
+ |
/* |
2072 |
|
* Display alert |
2073 |
|
*/ |
2074 |
|
|
2117 |
|
|
2118 |
|
void ErrorAlert(const char *text) |
2119 |
|
{ |
2120 |
< |
#ifdef ENABLE_GTK |
2120 |
> |
#if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO) |
2121 |
|
if (PrefsFindBool("nogui") || x_display == NULL) { |
2122 |
|
printf(GetString(STR_SHELL_ERROR_PREFIX), text); |
2123 |
|
return; |
2136 |
|
|
2137 |
|
void WarningAlert(const char *text) |
2138 |
|
{ |
2139 |
< |
#ifdef ENABLE_GTK |
2139 |
> |
#if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO) |
2140 |
|
if (PrefsFindBool("nogui") || x_display == NULL) { |
2141 |
|
printf(GetString(STR_SHELL_WARNING_PREFIX), text); |
2142 |
|
return; |