1 |
|
/* |
2 |
|
* main_unix.cpp - Emulation core, Unix implementation |
3 |
|
* |
4 |
< |
* SheepShaver (C) 1997-2002 Christian Bauer and Marc Hellwig |
4 |
> |
* SheepShaver (C) 1997-2004 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 |
65 |
|
* ExecutePPC (or any function that might cause a mode switch). The signal |
66 |
|
* stack is restored before exiting the SIGUSR2 handler. |
67 |
|
* |
68 |
+ |
* There is apparently another problem when processing signals. In |
69 |
+ |
* fullscreen mode, we get quick updates of the mouse position. This |
70 |
+ |
* causes an increased number of calls to TriggerInterrupt(). And, |
71 |
+ |
* since IRQ_NEST is not fully handled atomically, nested calls to |
72 |
+ |
* ppc_interrupt() may cause stack corruption to eventually crash the |
73 |
+ |
* emulator. |
74 |
+ |
* |
75 |
+ |
* FIXME: |
76 |
+ |
* The current solution is to allocate another signal stack when |
77 |
+ |
* processing ppc_interrupt(). However, it may be better to detect |
78 |
+ |
* the INTFLAG_ADB case and handle it specifically with some extra mutex? |
79 |
+ |
* |
80 |
|
* TODO: |
81 |
|
* check if SIGSEGV handler works for all registers (including FP!) |
82 |
|
*/ |
119 |
|
#include "macos_util.h" |
120 |
|
#include "rom_patches.h" |
121 |
|
#include "user_strings.h" |
122 |
+ |
#include "vm_alloc.h" |
123 |
+ |
#include "sigsegv.h" |
124 |
+ |
#include "thunks.h" |
125 |
|
|
126 |
|
#define DEBUG 0 |
127 |
|
#include "debug.h" |
128 |
|
|
129 |
|
|
130 |
+ |
#ifdef USE_SDL |
131 |
+ |
#include <SDL.h> |
132 |
+ |
#endif |
133 |
+ |
|
134 |
+ |
#ifndef USE_SDL_VIDEO |
135 |
|
#include <X11/Xlib.h> |
136 |
+ |
#endif |
137 |
|
|
138 |
|
#ifdef ENABLE_GTK |
139 |
|
#include <gtk/gtk.h> |
150 |
|
#endif |
151 |
|
|
152 |
|
|
153 |
+ |
// Enable emulation of unaligned lmw/stmw? |
154 |
+ |
#define EMULATE_UNALIGNED_LOADSTORE_MULTIPLE 1 |
155 |
+ |
|
156 |
|
// Enable Execute68k() safety checks? |
157 |
|
#define SAFE_EXEC_68K 0 |
158 |
|
|
162 |
|
// Interrupts in native mode? |
163 |
|
#define INTERRUPTS_IN_NATIVE_MODE 1 |
164 |
|
|
165 |
+ |
// Number of alternate stacks for signal handlers? |
166 |
+ |
#define SIG_STACK_COUNT 4 |
167 |
+ |
|
168 |
|
|
169 |
|
// Constants |
170 |
|
const char ROM_FILE_NAME[] = "ROM"; |
171 |
|
const char ROM_FILE_NAME2[] = "Mac OS ROM"; |
172 |
|
|
173 |
< |
const uint32 ROM_AREA_SIZE = 0x500000; // Size of ROM area |
147 |
< |
const uint32 ROM_END = ROM_BASE + ROM_SIZE; // End of ROM |
148 |
< |
|
149 |
< |
const uint32 KERNEL_DATA_BASE = 0x68ffe000; // Address of Kernel Data |
150 |
< |
const uint32 KERNEL_DATA2_BASE = 0x5fffe000; // Alternate address of Kernel Data |
151 |
< |
const uint32 KERNEL_AREA_SIZE = 0x2000; // Size of Kernel Data area |
152 |
< |
|
173 |
> |
const uintptr RAM_BASE = 0x20000000; // Base address of RAM |
174 |
|
const uint32 SIG_STACK_SIZE = 0x10000; // Size of signal stack |
175 |
|
|
176 |
|
|
177 |
< |
// 68k Emulator Data |
178 |
< |
struct EmulatorData { |
179 |
< |
uint32 v[0x400]; |
177 |
> |
#if !EMULATED_PPC |
178 |
> |
struct sigregs { |
179 |
> |
uint32 nip; |
180 |
> |
uint32 link; |
181 |
> |
uint32 ctr; |
182 |
> |
uint32 msr; |
183 |
> |
uint32 xer; |
184 |
> |
uint32 ccr; |
185 |
> |
uint32 gpr[32]; |
186 |
|
}; |
187 |
|
|
188 |
+ |
#if defined(__linux__) |
189 |
+ |
#include <sys/ucontext.h> |
190 |
+ |
#define MACHINE_REGISTERS(scp) ((machine_regs *)(((ucontext_t *)scp)->uc_mcontext.regs)) |
191 |
+ |
|
192 |
+ |
struct machine_regs : public pt_regs |
193 |
+ |
{ |
194 |
+ |
u_long & cr() { return pt_regs::ccr; } |
195 |
+ |
uint32 cr() const { return pt_regs::ccr; } |
196 |
+ |
uint32 lr() const { return pt_regs::link; } |
197 |
+ |
uint32 ctr() const { return pt_regs::ctr; } |
198 |
+ |
uint32 xer() const { return pt_regs::xer; } |
199 |
+ |
uint32 msr() const { return pt_regs::msr; } |
200 |
+ |
uint32 dar() const { return pt_regs::dar; } |
201 |
+ |
u_long & pc() { return pt_regs::nip; } |
202 |
+ |
uint32 pc() const { return pt_regs::nip; } |
203 |
+ |
u_long & gpr(int i) { return pt_regs::gpr[i]; } |
204 |
+ |
uint32 gpr(int i) const { return pt_regs::gpr[i]; } |
205 |
+ |
}; |
206 |
+ |
#endif |
207 |
|
|
208 |
< |
// Kernel Data |
209 |
< |
struct KernelData { |
210 |
< |
uint32 v[0x400]; |
211 |
< |
EmulatorData ed; |
208 |
> |
#if defined(__APPLE__) && defined(__MACH__) |
209 |
> |
#include <sys/signal.h> |
210 |
> |
extern "C" int sigaltstack(const struct sigaltstack *ss, struct sigaltstack *oss); |
211 |
> |
|
212 |
> |
#include <sys/ucontext.h> |
213 |
> |
#define MACHINE_REGISTERS(scp) ((machine_regs *)(((ucontext_t *)scp)->uc_mcontext)) |
214 |
> |
|
215 |
> |
struct machine_regs : public mcontext |
216 |
> |
{ |
217 |
> |
uint32 & cr() { return ss.cr; } |
218 |
> |
uint32 cr() const { return ss.cr; } |
219 |
> |
uint32 lr() const { return ss.lr; } |
220 |
> |
uint32 ctr() const { return ss.ctr; } |
221 |
> |
uint32 xer() const { return ss.xer; } |
222 |
> |
uint32 msr() const { return ss.srr1; } |
223 |
> |
uint32 dar() const { return es.dar; } |
224 |
> |
uint32 & pc() { return ss.srr0; } |
225 |
> |
uint32 pc() const { return ss.srr0; } |
226 |
> |
uint32 & gpr(int i) { return (&ss.r0)[i]; } |
227 |
> |
uint32 gpr(int i) const { return (&ss.r0)[i]; } |
228 |
|
}; |
229 |
+ |
#endif |
230 |
|
|
231 |
+ |
static void build_sigregs(sigregs *srp, machine_regs *mrp) |
232 |
+ |
{ |
233 |
+ |
srp->nip = mrp->pc(); |
234 |
+ |
srp->link = mrp->lr(); |
235 |
+ |
srp->ctr = mrp->ctr(); |
236 |
+ |
srp->msr = mrp->msr(); |
237 |
+ |
srp->xer = mrp->xer(); |
238 |
+ |
srp->ccr = mrp->cr(); |
239 |
+ |
for (int i = 0; i < 32; i++) |
240 |
+ |
srp->gpr[i] = mrp->gpr(i); |
241 |
+ |
} |
242 |
|
|
243 |
< |
#if !EMULATED_PPC |
244 |
< |
// Structure in which registers are saved in a signal handler; |
171 |
< |
// sigcontext->regs points to it |
172 |
< |
// (see arch/ppc/kernel/signal.c) |
173 |
< |
typedef struct { |
174 |
< |
uint32 u[4]; |
175 |
< |
} __attribute((aligned(16))) vector128; |
176 |
< |
#include <linux/elf.h> |
243 |
> |
static struct sigaltstack sig_stacks[SIG_STACK_COUNT]; // Stacks for signal handlers |
244 |
> |
static int sig_stack_id = 0; // Stack slot currently used |
245 |
|
|
246 |
< |
struct sigregs { |
247 |
< |
elf_gregset_t gp_regs; // Identical to pt_regs |
248 |
< |
double fp_regs[ELF_NFPREG]; // f0..f31 and fpsrc |
249 |
< |
//more (uninteresting) stuff following here |
250 |
< |
}; |
246 |
> |
static inline void sig_stack_acquire(void) |
247 |
> |
{ |
248 |
> |
if (++sig_stack_id == SIG_STACK_COUNT) { |
249 |
> |
printf("FATAL: signal stack overflow\n"); |
250 |
> |
return; |
251 |
> |
} |
252 |
> |
sigaltstack(&sig_stacks[sig_stack_id], NULL); |
253 |
> |
} |
254 |
> |
|
255 |
> |
static inline void sig_stack_release(void) |
256 |
> |
{ |
257 |
> |
if (--sig_stack_id < 0) { |
258 |
> |
printf("FATAL: signal stack underflow\n"); |
259 |
> |
return; |
260 |
> |
} |
261 |
> |
sigaltstack(&sig_stacks[sig_stack_id], NULL); |
262 |
> |
} |
263 |
|
#endif |
264 |
|
|
265 |
|
|
271 |
|
uint32 RAMSize; // Size of Mac RAM |
272 |
|
uint32 KernelDataAddr; // Address of Kernel Data |
273 |
|
uint32 BootGlobsAddr; // Address of BootGlobs structure at top of Mac RAM |
274 |
+ |
uint32 DRCacheAddr; // Address of DR Cache |
275 |
|
uint32 PVR; // Theoretical PVR |
276 |
|
int64 CPUClockSpeed; // Processor clock speed (Hz) |
277 |
|
int64 BusClockSpeed; // Bus clock speed (Hz) |
278 |
|
|
279 |
|
|
280 |
|
// Global variables |
281 |
< |
static char *x_display_name = NULL; // X11 display name |
281 |
> |
#ifndef USE_SDL_VIDEO |
282 |
> |
char *x_display_name = NULL; // X11 display name |
283 |
|
Display *x_display = NULL; // X11 display handle |
284 |
+ |
#ifdef X11_LOCK_TYPE |
285 |
+ |
X11_LOCK_TYPE x_display_lock = X11_LOCK_INIT; // X11 display lock |
286 |
+ |
#endif |
287 |
+ |
#endif |
288 |
|
|
289 |
|
static int zero_fd = 0; // FD of /dev/zero |
290 |
|
static bool lm_area_mapped = false; // Flag: Low Memory area mmap()ped |
291 |
|
static int kernel_area = -1; // SHM ID of Kernel Data area |
292 |
|
static bool rom_area_mapped = false; // Flag: Mac ROM mmap()ped |
293 |
|
static bool ram_area_mapped = false; // Flag: Mac RAM mmap()ped |
294 |
< |
static void *mmap_RAMBase = NULL; // Base address of mmap()ed RAM area |
294 |
> |
static bool dr_cache_area_mapped = false; // Flag: Mac DR Cache mmap()ped |
295 |
> |
static bool dr_emulator_area_mapped = false;// Flag: Mac DR Emulator mmap()ped |
296 |
|
static KernelData *kernel_data; // Pointer to Kernel Data |
297 |
|
static EmulatorData *emulator_data; |
298 |
|
|
299 |
|
static uint8 last_xpram[XPRAM_SIZE]; // Buffer for monitoring XPRAM changes |
300 |
|
|
301 |
|
static bool nvram_thread_active = false; // Flag: NVRAM watchdog installed |
302 |
+ |
static volatile bool nvram_thread_cancel; // Flag: Cancel NVRAM thread |
303 |
|
static pthread_t nvram_thread; // NVRAM watchdog |
304 |
|
static bool tick_thread_active = false; // Flag: MacOS thread installed |
305 |
+ |
static volatile bool tick_thread_cancel; // Flag: Cancel 60Hz thread |
306 |
|
static pthread_t tick_thread; // 60Hz thread |
307 |
|
static pthread_t emul_thread; // MacOS thread |
308 |
|
|
309 |
|
static bool ready_for_signals = false; // Handler installed, signals can be sent |
310 |
|
static int64 num_segv = 0; // Number of handled SEGV signals |
311 |
|
|
223 |
– |
#if !EMULATED_PPC |
312 |
|
static struct sigaction sigusr2_action; // Interrupt signal (of emulator thread) |
313 |
+ |
#if EMULATED_PPC |
314 |
+ |
static uintptr sig_stack = 0; // Stack for PowerPC interrupt routine |
315 |
+ |
#else |
316 |
|
static struct sigaction sigsegv_action; // Data access exception signal (of emulator thread) |
317 |
|
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 |
318 |
|
static bool emul_thread_fatal = false; // Flag: MacOS thread crashed, tick thread shall dump debug output |
319 |
|
static sigregs sigsegv_regs; // Register dump when crashed |
320 |
+ |
static const char *crash_reason = NULL; // Reason of the crash (SIGSEGV, SIGBUS, SIGILL) |
321 |
|
#endif |
322 |
|
|
323 |
+ |
uint32 SheepMem::page_size; // Size of a native page |
324 |
+ |
uintptr SheepMem::zero_page = 0; // Address of ro page filled in with zeros |
325 |
+ |
uintptr SheepMem::base = 0x60000000; // Address of SheepShaver data |
326 |
+ |
uintptr SheepMem::top = 0; // Top of SheepShaver data (stack like storage) |
327 |
+ |
|
328 |
|
|
329 |
|
// Prototypes |
330 |
|
static void Quit(void); |
331 |
|
static void *emul_func(void *arg); |
332 |
|
static void *nvram_func(void *arg); |
333 |
|
static void *tick_func(void *arg); |
334 |
< |
#if !EMULATED_PPC |
335 |
< |
static void sigusr2_handler(int sig, sigcontext_struct *sc); |
336 |
< |
static void sigsegv_handler(int sig, sigcontext_struct *sc); |
337 |
< |
static void sigill_handler(int sig, sigcontext_struct *sc); |
334 |
> |
#if EMULATED_PPC |
335 |
> |
extern void emul_ppc(uint32 start); |
336 |
> |
extern void init_emul_ppc(void); |
337 |
> |
extern void exit_emul_ppc(void); |
338 |
> |
#else |
339 |
> |
static void sigusr2_handler(int sig, siginfo_t *sip, void *scp); |
340 |
> |
static void sigsegv_handler(int sig, siginfo_t *sip, void *scp); |
341 |
> |
static void sigill_handler(int sig, siginfo_t *sip, void *scp); |
342 |
|
#endif |
343 |
|
|
344 |
|
|
345 |
|
// From asm_linux.S |
346 |
< |
#if EMULATED_PPC |
248 |
< |
extern int atomic_add(int *var, int v); |
249 |
< |
extern int atomic_and(int *var, int v); |
250 |
< |
extern int atomic_or(int *var, int v); |
251 |
< |
#else |
346 |
> |
#if !EMULATED_PPC |
347 |
|
extern "C" void *get_toc(void); |
348 |
|
extern "C" void *get_sp(void); |
349 |
|
extern "C" void flush_icache_range(void *start, void *end); |
358 |
|
#endif |
359 |
|
|
360 |
|
|
361 |
< |
// Decode LZSS data |
362 |
< |
static void decode_lzss(const uint8 *src, uint8 *dest, int size) |
361 |
> |
#if EMULATED_PPC |
362 |
> |
/* |
363 |
> |
* Return signal stack base |
364 |
> |
*/ |
365 |
> |
|
366 |
> |
uintptr SignalStackBase(void) |
367 |
|
{ |
368 |
< |
char dict[0x1000]; |
270 |
< |
int run_mask = 0, dict_idx = 0xfee; |
271 |
< |
for (;;) { |
272 |
< |
if (run_mask < 0x100) { |
273 |
< |
// Start new run |
274 |
< |
if (--size < 0) |
275 |
< |
break; |
276 |
< |
run_mask = *src++ | 0xff00; |
277 |
< |
} |
278 |
< |
bool bit = run_mask & 1; |
279 |
< |
run_mask >>= 1; |
280 |
< |
if (bit) { |
281 |
< |
// Verbatim copy |
282 |
< |
if (--size < 0) |
283 |
< |
break; |
284 |
< |
int c = *src++; |
285 |
< |
dict[dict_idx++] = c; |
286 |
< |
*dest++ = c; |
287 |
< |
dict_idx &= 0xfff; |
288 |
< |
} else { |
289 |
< |
// Copy from dictionary |
290 |
< |
if (--size < 0) |
291 |
< |
break; |
292 |
< |
int idx = *src++; |
293 |
< |
if (--size < 0) |
294 |
< |
break; |
295 |
< |
int cnt = *src++; |
296 |
< |
idx |= (cnt << 4) & 0xf00; |
297 |
< |
cnt = (cnt & 0x0f) + 3; |
298 |
< |
while (cnt--) { |
299 |
< |
char c = dict[idx++]; |
300 |
< |
dict[dict_idx++] = c; |
301 |
< |
*dest++ = c; |
302 |
< |
idx &= 0xfff; |
303 |
< |
dict_idx &= 0xfff; |
304 |
< |
} |
305 |
< |
} |
306 |
< |
} |
368 |
> |
return sig_stack + SIG_STACK_SIZE; |
369 |
|
} |
370 |
|
|
371 |
|
|
372 |
|
/* |
373 |
+ |
* Atomic operations |
374 |
+ |
*/ |
375 |
+ |
|
376 |
+ |
#if HAVE_SPINLOCKS |
377 |
+ |
static spinlock_t atomic_ops_lock = SPIN_LOCK_UNLOCKED; |
378 |
+ |
#else |
379 |
+ |
#define spin_lock(LOCK) |
380 |
+ |
#define spin_unlock(LOCK) |
381 |
+ |
#endif |
382 |
+ |
|
383 |
+ |
int atomic_add(int *var, int v) |
384 |
+ |
{ |
385 |
+ |
spin_lock(&atomic_ops_lock); |
386 |
+ |
int ret = *var; |
387 |
+ |
*var += v; |
388 |
+ |
spin_unlock(&atomic_ops_lock); |
389 |
+ |
return ret; |
390 |
+ |
} |
391 |
+ |
|
392 |
+ |
int atomic_and(int *var, int v) |
393 |
+ |
{ |
394 |
+ |
spin_lock(&atomic_ops_lock); |
395 |
+ |
int ret = *var; |
396 |
+ |
*var &= v; |
397 |
+ |
spin_unlock(&atomic_ops_lock); |
398 |
+ |
return ret; |
399 |
+ |
} |
400 |
+ |
|
401 |
+ |
int atomic_or(int *var, int v) |
402 |
+ |
{ |
403 |
+ |
spin_lock(&atomic_ops_lock); |
404 |
+ |
int ret = *var; |
405 |
+ |
*var |= v; |
406 |
+ |
spin_unlock(&atomic_ops_lock); |
407 |
+ |
return ret; |
408 |
+ |
} |
409 |
+ |
#endif |
410 |
+ |
|
411 |
+ |
|
412 |
+ |
/* |
413 |
|
* Main program |
414 |
|
*/ |
415 |
|
|
427 |
|
char str[256]; |
428 |
|
uint32 *boot_globs; |
429 |
|
int16 i16; |
328 |
– |
int drive, driver; |
430 |
|
int rom_fd; |
431 |
|
FILE *proc_file; |
432 |
|
const char *rom_path; |
436 |
|
|
437 |
|
// Initialize variables |
438 |
|
RAMBase = 0; |
338 |
– |
mmap_RAMBase = NULL; |
439 |
|
tzset(); |
440 |
|
|
441 |
|
// Print some info |
460 |
|
for (int i=1; i<argc; i++) { |
461 |
|
if (strcmp(argv[i], "--help") == 0) { |
462 |
|
usage(argv[0]); |
463 |
+ |
#ifndef USE_SDL_VIDEO |
464 |
|
} else if (strcmp(argv[i], "--display") == 0) { |
465 |
|
i++; |
466 |
|
if (i < argc) |
467 |
|
x_display_name = strdup(argv[i]); |
468 |
+ |
#endif |
469 |
|
} else if (argv[i][0] == '-') { |
470 |
|
fprintf(stderr, "Unrecognized option '%s'\n", argv[i]); |
471 |
|
usage(argv[0]); |
472 |
|
} |
473 |
|
} |
474 |
|
|
475 |
+ |
#ifdef USE_SDL |
476 |
+ |
// Initialize SDL system |
477 |
+ |
int sdl_flags = 0; |
478 |
+ |
#ifdef USE_SDL_VIDEO |
479 |
+ |
sdl_flags |= SDL_INIT_VIDEO; |
480 |
+ |
#endif |
481 |
+ |
assert(sdl_flags != 0); |
482 |
+ |
if (SDL_Init(sdl_flags) == -1) { |
483 |
+ |
char str[256]; |
484 |
+ |
sprintf(str, "Could not initialize SDL: %s.\n", SDL_GetError()); |
485 |
+ |
ErrorAlert(str); |
486 |
+ |
goto quit; |
487 |
+ |
} |
488 |
+ |
atexit(SDL_Quit); |
489 |
+ |
#endif |
490 |
+ |
|
491 |
+ |
#ifndef USE_SDL_VIDEO |
492 |
|
// Open display |
493 |
|
x_display = XOpenDisplay(x_display_name); |
494 |
|
if (x_display == NULL) { |
502 |
|
// Fork out, so we can return from fullscreen mode when things get ugly |
503 |
|
XF86DGAForkApp(DefaultScreen(x_display)); |
504 |
|
#endif |
505 |
+ |
#endif |
506 |
|
|
507 |
|
#ifdef ENABLE_MON |
508 |
|
// Initialize mon |
513 |
|
PVR = 0x00040000; // Default: 604 |
514 |
|
CPUClockSpeed = 100000000; // Default: 100MHz |
515 |
|
BusClockSpeed = 100000000; // Default: 100MHz |
516 |
< |
#if !EMULATED_PPC |
516 |
> |
#if EMULATED_PPC |
517 |
> |
PVR = 0x000c0000; // Default: 7400 (with AltiVec) |
518 |
> |
#elif defined(__APPLE__) && defined(__MACH__) |
519 |
> |
proc_file = popen("ioreg -c IOPlatformDevice", "r"); |
520 |
> |
if (proc_file) { |
521 |
> |
char line[256]; |
522 |
> |
bool powerpc_node = false; |
523 |
> |
while (fgets(line, sizeof(line) - 1, proc_file)) { |
524 |
> |
// Read line |
525 |
> |
int len = strlen(line); |
526 |
> |
if (len == 0) |
527 |
> |
continue; |
528 |
> |
line[len - 1] = 0; |
529 |
> |
|
530 |
> |
// Parse line |
531 |
> |
if (strstr(line, "o PowerPC,")) |
532 |
> |
powerpc_node = true; |
533 |
> |
else if (powerpc_node) { |
534 |
> |
uint32 value; |
535 |
> |
char head[256]; |
536 |
> |
if (sscanf(line, "%[ |]\"cpu-version\" = <%x>", head, &value) == 2) |
537 |
> |
PVR = value; |
538 |
> |
else if (sscanf(line, "%[ |]\"clock-frequency\" = <%x>", head, &value) == 2) |
539 |
> |
CPUClockSpeed = value; |
540 |
> |
else if (sscanf(line, "%[ |]\"bus-frequency\" = <%x>", head, &value) == 2) |
541 |
> |
BusClockSpeed = 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 |
|
char line[256]; |
562 |
|
// Parse line |
563 |
|
int i; |
564 |
|
char value[256]; |
565 |
< |
if (sscanf(line, "cpu : %s", value) == 1) { |
565 |
> |
if (sscanf(line, "cpu : %[0-9A-Za-a]", value) == 1) { |
566 |
|
if (strcmp(value, "601") == 0) |
567 |
|
PVR = 0x00010000; |
568 |
|
else if (strcmp(value, "603") == 0) |
583 |
|
PVR = 0x00320000; |
584 |
|
else if (strcmp(value, "860") == 0) |
585 |
|
PVR = 0x00500000; |
586 |
+ |
else if (strcmp(value, "7400") == 0) |
587 |
+ |
PVR = 0x000c0000; |
588 |
+ |
else if (strcmp(value, "7410") == 0) |
589 |
+ |
PVR = 0x800c0000; |
590 |
|
else |
591 |
|
printf("WARNING: Unknown CPU type '%s', assuming 604\n", value); |
592 |
|
} |
598 |
|
sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno)); |
599 |
|
WarningAlert(str); |
600 |
|
} |
601 |
+ |
|
602 |
+ |
// Get actual bus frequency |
603 |
+ |
proc_file = fopen("/proc/device-tree/clock-frequency", "r"); |
604 |
+ |
if (proc_file) { |
605 |
+ |
union { uint8 b[4]; uint32 l; } value; |
606 |
+ |
if (fread(value.b, sizeof(value), 1, proc_file) == 1) |
607 |
+ |
BusClockSpeed = value.l; |
608 |
+ |
fclose(proc_file); |
609 |
+ |
} |
610 |
|
#endif |
611 |
|
D(bug("PVR: %08x (assumed)\n", PVR)); |
612 |
|
|
631 |
|
goto quit; |
632 |
|
} |
633 |
|
|
634 |
+ |
#ifndef PAGEZERO_HACK |
635 |
|
// Create Low Memory area (0x0000..0x3000) |
636 |
< |
if (mmap((char *)0x0000, 0x3000, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, zero_fd, 0) == (void *)-1) { |
636 |
> |
if (vm_acquire_fixed((char *)0, 0x3000) < 0) { |
637 |
|
sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno)); |
638 |
|
ErrorAlert(str); |
639 |
|
goto quit; |
640 |
|
} |
641 |
|
lm_area_mapped = true; |
642 |
+ |
#endif |
643 |
|
|
644 |
|
// Create areas for Kernel Data |
645 |
|
kernel_area = shmget(IPC_PRIVATE, KERNEL_AREA_SIZE, 0600); |
658 |
|
ErrorAlert(str); |
659 |
|
goto quit; |
660 |
|
} |
661 |
< |
kernel_data = (KernelData *)0x68ffe000; |
661 |
> |
kernel_data = (KernelData *)KERNEL_DATA_BASE; |
662 |
|
emulator_data = &kernel_data->ed; |
663 |
< |
KernelDataAddr = (uint32)kernel_data; |
663 |
> |
KernelDataAddr = KERNEL_DATA_BASE; |
664 |
|
D(bug("Kernel Data at %p, Emulator Data at %p\n", kernel_data, emulator_data)); |
665 |
|
|
666 |
+ |
// Create area for DR Cache |
667 |
+ |
if (vm_acquire_fixed((void *)DR_EMULATOR_BASE, DR_EMULATOR_SIZE) < 0) { |
668 |
+ |
sprintf(str, GetString(STR_DR_EMULATOR_MMAP_ERR), strerror(errno)); |
669 |
+ |
ErrorAlert(str); |
670 |
+ |
goto quit; |
671 |
+ |
} |
672 |
+ |
dr_emulator_area_mapped = true; |
673 |
+ |
if (vm_acquire_fixed((void *)DR_CACHE_BASE, DR_CACHE_SIZE) < 0) { |
674 |
+ |
sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno)); |
675 |
+ |
ErrorAlert(str); |
676 |
+ |
goto quit; |
677 |
+ |
} |
678 |
+ |
dr_cache_area_mapped = true; |
679 |
+ |
#if !EMULATED_PPC |
680 |
+ |
if (vm_protect((char *)DR_CACHE_BASE, DR_CACHE_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
681 |
+ |
sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno)); |
682 |
+ |
ErrorAlert(str); |
683 |
+ |
goto quit; |
684 |
+ |
} |
685 |
+ |
#endif |
686 |
+ |
DRCacheAddr = DR_CACHE_BASE; |
687 |
+ |
D(bug("DR Cache at %p\n", DRCacheAddr)); |
688 |
+ |
|
689 |
+ |
// Create area for SheepShaver data |
690 |
+ |
if (!SheepMem::Init()) { |
691 |
+ |
sprintf(str, GetString(STR_SHEEP_MEM_MMAP_ERR), strerror(errno)); |
692 |
+ |
ErrorAlert(str); |
693 |
+ |
goto quit; |
694 |
+ |
} |
695 |
+ |
|
696 |
|
// Create area for Mac ROM |
697 |
< |
if (mmap((char *)ROM_BASE, ROM_AREA_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, zero_fd, 0) == (void *)-1) { |
697 |
> |
if (vm_acquire_fixed((char *)ROM_BASE, ROM_AREA_SIZE) < 0) { |
698 |
|
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
699 |
|
ErrorAlert(str); |
700 |
|
goto quit; |
701 |
|
} |
702 |
+ |
#if !EMULATED_PPC |
703 |
+ |
if (vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
704 |
+ |
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
705 |
+ |
ErrorAlert(str); |
706 |
+ |
goto quit; |
707 |
+ |
} |
708 |
+ |
#endif |
709 |
|
rom_area_mapped = true; |
710 |
|
D(bug("ROM area at %08x\n", ROM_BASE)); |
711 |
|
|
716 |
|
RAMSize = 8*1024*1024; |
717 |
|
} |
718 |
|
|
719 |
< |
mmap_RAMBase = mmap((void *)0x20000000, RAMSize, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, zero_fd, 0); |
513 |
< |
if (mmap_RAMBase == (void *)-1) { |
719 |
> |
if (vm_acquire_fixed((char *)RAM_BASE, RAMSize) < 0) { |
720 |
|
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
721 |
|
ErrorAlert(str); |
722 |
|
goto quit; |
723 |
|
} |
724 |
< |
RAMBase = (uint32)mmap_RAMBase; |
724 |
> |
#if !EMULATED_PPC |
725 |
> |
if (vm_protect((char *)RAM_BASE, RAMSize, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
726 |
> |
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
727 |
> |
ErrorAlert(str); |
728 |
> |
goto quit; |
729 |
> |
} |
730 |
> |
#endif |
731 |
> |
RAMBase = RAM_BASE; |
732 |
|
ram_area_mapped = true; |
733 |
|
D(bug("RAM area at %08x\n", RAMBase)); |
734 |
|
|
753 |
|
rom_tmp = new uint8[ROM_SIZE]; |
754 |
|
actual = read(rom_fd, (void *)rom_tmp, ROM_SIZE); |
755 |
|
close(rom_fd); |
756 |
< |
if (actual == ROM_SIZE) { |
757 |
< |
// Plain ROM image |
758 |
< |
memcpy((void *)ROM_BASE, rom_tmp, ROM_SIZE); |
759 |
< |
delete[] rom_tmp; |
547 |
< |
} else { |
548 |
< |
if (strncmp((char *)rom_tmp, "<CHRP-BOOT>", 11) == 0) { |
549 |
< |
// CHRP compressed ROM image |
550 |
< |
D(bug("CHRP ROM image\n")); |
551 |
< |
uint32 lzss_offset, lzss_size; |
552 |
< |
|
553 |
< |
char *s = strstr((char *)rom_tmp, "constant lzss-offset"); |
554 |
< |
if (s == NULL) { |
555 |
< |
ErrorAlert(GetString(STR_ROM_SIZE_ERR)); |
556 |
< |
goto quit; |
557 |
< |
} |
558 |
< |
s -= 7; |
559 |
< |
if (sscanf(s, "%06x", &lzss_offset) != 1) { |
560 |
< |
ErrorAlert(GetString(STR_ROM_SIZE_ERR)); |
561 |
< |
goto quit; |
562 |
< |
} |
563 |
< |
s = strstr((char *)rom_tmp, "constant lzss-size"); |
564 |
< |
if (s == NULL) { |
565 |
< |
ErrorAlert(GetString(STR_ROM_SIZE_ERR)); |
566 |
< |
goto quit; |
567 |
< |
} |
568 |
< |
s -= 7; |
569 |
< |
if (sscanf(s, "%06x", &lzss_size) != 1) { |
570 |
< |
ErrorAlert(GetString(STR_ROM_SIZE_ERR)); |
571 |
< |
goto quit; |
572 |
< |
} |
573 |
< |
D(bug("Offset of compressed data: %08x\n", lzss_offset)); |
574 |
< |
D(bug("Size of compressed data: %08x\n", lzss_size)); |
575 |
< |
|
576 |
< |
D(bug("Uncompressing ROM...\n")); |
577 |
< |
decode_lzss(rom_tmp + lzss_offset, (uint8 *)ROM_BASE, lzss_size); |
578 |
< |
delete[] rom_tmp; |
579 |
< |
} else if (rom_size != 4*1024*1024) { |
756 |
> |
|
757 |
> |
// Decode Mac ROM |
758 |
> |
if (!DecodeROM(rom_tmp, actual)) { |
759 |
> |
if (rom_size != 4*1024*1024) { |
760 |
|
ErrorAlert(GetString(STR_ROM_SIZE_ERR)); |
761 |
|
goto quit; |
762 |
|
} else { |
764 |
|
goto quit; |
765 |
|
} |
766 |
|
} |
767 |
+ |
delete[] rom_tmp; |
768 |
|
|
769 |
|
// Load NVRAM |
770 |
|
XPRAMInit(); |
771 |
|
|
772 |
+ |
// Load XPRAM default values if signature not found |
773 |
+ |
if (XPRAM[0x130c] != 0x4e || XPRAM[0x130d] != 0x75 |
774 |
+ |
|| XPRAM[0x130e] != 0x4d || XPRAM[0x130f] != 0x63) { |
775 |
+ |
D(bug("Loading XPRAM default values\n")); |
776 |
+ |
memset(XPRAM + 0x1300, 0, 0x100); |
777 |
+ |
XPRAM[0x130c] = 0x4e; // "NuMc" signature |
778 |
+ |
XPRAM[0x130d] = 0x75; |
779 |
+ |
XPRAM[0x130e] = 0x4d; |
780 |
+ |
XPRAM[0x130f] = 0x63; |
781 |
+ |
XPRAM[0x1301] = 0x80; // InternalWaitFlags = DynWait (don't wait for SCSI devices upon bootup) |
782 |
+ |
XPRAM[0x1310] = 0xa8; // Standard PRAM values |
783 |
+ |
XPRAM[0x1311] = 0x00; |
784 |
+ |
XPRAM[0x1312] = 0x00; |
785 |
+ |
XPRAM[0x1313] = 0x22; |
786 |
+ |
XPRAM[0x1314] = 0xcc; |
787 |
+ |
XPRAM[0x1315] = 0x0a; |
788 |
+ |
XPRAM[0x1316] = 0xcc; |
789 |
+ |
XPRAM[0x1317] = 0x0a; |
790 |
+ |
XPRAM[0x131c] = 0x00; |
791 |
+ |
XPRAM[0x131d] = 0x02; |
792 |
+ |
XPRAM[0x131e] = 0x63; |
793 |
+ |
XPRAM[0x131f] = 0x00; |
794 |
+ |
XPRAM[0x1308] = 0x13; |
795 |
+ |
XPRAM[0x1309] = 0x88; |
796 |
+ |
XPRAM[0x130a] = 0x00; |
797 |
+ |
XPRAM[0x130b] = 0xcc; |
798 |
+ |
XPRAM[0x1376] = 0x00; // OSDefault = MacOS |
799 |
+ |
XPRAM[0x1377] = 0x01; |
800 |
+ |
} |
801 |
+ |
|
802 |
|
// Set boot volume |
803 |
< |
drive = PrefsFindInt32("bootdrive"); |
803 |
> |
i16 = PrefsFindInt32("bootdrive"); |
804 |
|
XPRAM[0x1378] = i16 >> 8; |
805 |
|
XPRAM[0x1379] = i16 & 0xff; |
806 |
< |
driver = PrefsFindInt32("bootdriver"); |
806 |
> |
i16 = PrefsFindInt32("bootdriver"); |
807 |
|
XPRAM[0x137a] = i16 >> 8; |
808 |
|
XPRAM[0x137b] = i16 & 0xff; |
809 |
|
|
816 |
|
boot_globs[1] = htonl(RAMSize); |
817 |
|
boot_globs[2] = htonl((uint32)-1); // End of bank table |
818 |
|
|
819 |
+ |
// Init thunks |
820 |
+ |
if (!ThunksInit()) |
821 |
+ |
goto quit; |
822 |
+ |
|
823 |
|
// Init drivers |
824 |
|
SonyInit(); |
825 |
|
DiskInit(); |
829 |
|
// Init external file system |
830 |
|
ExtFSInit(); |
831 |
|
|
832 |
+ |
// Init ADB |
833 |
+ |
ADBInit(); |
834 |
+ |
|
835 |
|
// Init audio |
836 |
|
AudioInit(); |
837 |
|
|
861 |
|
#if !EMULATED_PPC |
862 |
|
MakeExecutable(0, (void *)ROM_BASE, ROM_AREA_SIZE); |
863 |
|
#endif |
864 |
< |
mprotect((char *)ROM_BASE, ROM_AREA_SIZE, PROT_EXEC | PROT_READ); |
864 |
> |
vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_EXECUTE); |
865 |
|
|
866 |
|
// Initialize Kernel Data |
867 |
|
memset(kernel_data, 0, sizeof(KernelData)); |
868 |
|
if (ROMType == ROMTYPE_NEWWORLD) { |
869 |
< |
static uint32 of_dev_tree[4] = {0, 0, 0, 0}; |
870 |
< |
static uint8 vector_lookup_tbl[128]; |
871 |
< |
static uint8 vector_mask_tbl[64]; |
869 |
> |
uintptr of_dev_tree = SheepMem::Reserve(4 * sizeof(uint32)); |
870 |
> |
memset((void *)of_dev_tree, 0, 4 * sizeof(uint32)); |
871 |
> |
uintptr vector_lookup_tbl = SheepMem::Reserve(128); |
872 |
> |
uintptr vector_mask_tbl = SheepMem::Reserve(64); |
873 |
|
memset((uint8 *)kernel_data + 0xb80, 0x3d, 0x80); |
874 |
< |
memset(vector_lookup_tbl, 0, 128); |
875 |
< |
memset(vector_mask_tbl, 0, 64); |
874 |
> |
memset((void *)vector_lookup_tbl, 0, 128); |
875 |
> |
memset((void *)vector_mask_tbl, 0, 64); |
876 |
|
kernel_data->v[0xb80 >> 2] = htonl(ROM_BASE); |
877 |
< |
kernel_data->v[0xb84 >> 2] = htonl((uint32)of_dev_tree); // OF device tree base |
878 |
< |
kernel_data->v[0xb90 >> 2] = htonl((uint32)vector_lookup_tbl); |
879 |
< |
kernel_data->v[0xb94 >> 2] = htonl((uint32)vector_mask_tbl); |
877 |
> |
kernel_data->v[0xb84 >> 2] = htonl(of_dev_tree); // OF device tree base |
878 |
> |
kernel_data->v[0xb90 >> 2] = htonl(vector_lookup_tbl); |
879 |
> |
kernel_data->v[0xb94 >> 2] = htonl(vector_mask_tbl); |
880 |
|
kernel_data->v[0xb98 >> 2] = htonl(ROM_BASE); // OpenPIC base |
881 |
|
kernel_data->v[0xbb0 >> 2] = htonl(0); // ADB base |
882 |
|
kernel_data->v[0xc20 >> 2] = htonl(RAMSize); |
889 |
|
kernel_data->v[0xc50 >> 2] = htonl(RAMBase); |
890 |
|
kernel_data->v[0xc54 >> 2] = htonl(RAMSize); |
891 |
|
kernel_data->v[0xf60 >> 2] = htonl(PVR); |
892 |
< |
kernel_data->v[0xf64 >> 2] = htonl(CPUClockSpeed); |
893 |
< |
kernel_data->v[0xf68 >> 2] = htonl(BusClockSpeed); |
894 |
< |
kernel_data->v[0xf6c >> 2] = htonl(CPUClockSpeed); |
892 |
> |
kernel_data->v[0xf64 >> 2] = htonl(CPUClockSpeed); // clock-frequency |
893 |
> |
kernel_data->v[0xf68 >> 2] = htonl(BusClockSpeed); // bus-frequency |
894 |
> |
kernel_data->v[0xf6c >> 2] = htonl(BusClockSpeed / 4); // timebase-frequency |
895 |
|
} else { |
896 |
|
kernel_data->v[0xc80 >> 2] = htonl(RAMSize); |
897 |
|
kernel_data->v[0xc84 >> 2] = htonl(RAMSize); |
903 |
|
kernel_data->v[0xcb0 >> 2] = htonl(RAMBase); |
904 |
|
kernel_data->v[0xcb4 >> 2] = htonl(RAMSize); |
905 |
|
kernel_data->v[0xf80 >> 2] = htonl(PVR); |
906 |
< |
kernel_data->v[0xf84 >> 2] = htonl(CPUClockSpeed); |
907 |
< |
kernel_data->v[0xf88 >> 2] = htonl(BusClockSpeed); |
908 |
< |
kernel_data->v[0xf8c >> 2] = htonl(CPUClockSpeed); |
906 |
> |
kernel_data->v[0xf84 >> 2] = htonl(CPUClockSpeed); // clock-frequency |
907 |
> |
kernel_data->v[0xf88 >> 2] = htonl(BusClockSpeed); // bus-frequency |
908 |
> |
kernel_data->v[0xf8c >> 2] = htonl(BusClockSpeed / 4); // timebase-frequency |
909 |
|
} |
910 |
|
|
911 |
|
// Initialize extra low memory |
912 |
|
D(bug("Initializing Low Memory...\n")); |
913 |
|
memset(NULL, 0, 0x3000); |
914 |
|
WriteMacInt32(XLM_SIGNATURE, FOURCC('B','a','a','h')); // Signature to detect SheepShaver |
915 |
< |
WriteMacInt32(XLM_KERNEL_DATA, (uint32)kernel_data); // For trap replacement routines |
915 |
> |
WriteMacInt32(XLM_KERNEL_DATA, KernelDataAddr); // For trap replacement routines |
916 |
|
WriteMacInt32(XLM_PVR, PVR); // Theoretical PVR |
917 |
|
WriteMacInt32(XLM_BUS_CLOCK, BusClockSpeed); // For DriverServicesLib patch |
918 |
|
WriteMacInt16(XLM_EXEC_RETURN_OPCODE, M68K_EXEC_RETURN); // For Execute68k() (RTS from the executed 68k code will jump here and end 68k mode) |
919 |
+ |
WriteMacInt32(XLM_ZERO_PAGE, SheepMem::ZeroPage()); // Pointer to read-only page with all bits set to 0 |
920 |
|
#if !EMULATED_PPC |
921 |
< |
WriteMacInt32(XLM_TOC, (uint32)TOC); // TOC pointer of emulator |
702 |
< |
WriteMacInt32(XLM_ETHER_INIT, (uint32)InitStreamModule); // DLPI ethernet driver functions |
703 |
< |
WriteMacInt32(XLM_ETHER_TERM, (uint32)TerminateStreamModule); |
704 |
< |
WriteMacInt32(XLM_ETHER_OPEN, (uint32)ether_open); |
705 |
< |
WriteMacInt32(XLM_ETHER_CLOSE, (uint32)ether_close); |
706 |
< |
WriteMacInt32(XLM_ETHER_WPUT, (uint32)ether_wput); |
707 |
< |
WriteMacInt32(XLM_ETHER_RSRV, (uint32)ether_rsrv); |
708 |
< |
WriteMacInt32(XLM_VIDEO_DOIO, (uint32)VideoDoDriverIO); |
921 |
> |
WriteMacInt32(XLM_TOC, (uint32)TOC); // TOC pointer of emulator |
922 |
|
#endif |
923 |
+ |
WriteMacInt32(XLM_ETHER_INIT, NativeFunction(NATIVE_ETHER_INIT)); // DLPI ethernet driver functions |
924 |
+ |
WriteMacInt32(XLM_ETHER_TERM, NativeFunction(NATIVE_ETHER_TERM)); |
925 |
+ |
WriteMacInt32(XLM_ETHER_OPEN, NativeFunction(NATIVE_ETHER_OPEN)); |
926 |
+ |
WriteMacInt32(XLM_ETHER_CLOSE, NativeFunction(NATIVE_ETHER_CLOSE)); |
927 |
+ |
WriteMacInt32(XLM_ETHER_WPUT, NativeFunction(NATIVE_ETHER_WPUT)); |
928 |
+ |
WriteMacInt32(XLM_ETHER_RSRV, NativeFunction(NATIVE_ETHER_RSRV)); |
929 |
+ |
WriteMacInt32(XLM_VIDEO_DOIO, NativeFunction(NATIVE_VIDEO_DO_DRIVER_IO)); |
930 |
|
D(bug("Low Memory initialized\n")); |
931 |
|
|
932 |
|
// Start 60Hz thread |
933 |
+ |
tick_thread_cancel = false; |
934 |
|
tick_thread_active = (pthread_create(&tick_thread, NULL, tick_func, NULL) == 0); |
935 |
|
D(bug("Tick thread installed (%ld)\n", tick_thread)); |
936 |
|
|
937 |
|
// Start NVRAM watchdog thread |
938 |
|
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
939 |
+ |
nvram_thread_cancel = false; |
940 |
|
nvram_thread_active = (pthread_create(&nvram_thread, NULL, nvram_func, NULL) == 0); |
941 |
|
D(bug("NVRAM thread installed (%ld)\n", nvram_thread)); |
942 |
|
|
943 |
|
#if !EMULATED_PPC |
944 |
|
// Create and install stacks for signal handlers |
945 |
< |
sig_stack = malloc(SIG_STACK_SIZE); |
946 |
< |
D(bug("Signal stack at %p\n", sig_stack)); |
947 |
< |
if (sig_stack == NULL) { |
948 |
< |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
949 |
< |
goto quit; |
950 |
< |
} |
951 |
< |
extra_stack = malloc(SIG_STACK_SIZE); |
952 |
< |
D(bug("Extra stack at %p\n", extra_stack)); |
953 |
< |
if (extra_stack == NULL) { |
954 |
< |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
955 |
< |
goto quit; |
956 |
< |
} |
957 |
< |
struct sigaltstack new_stack; |
736 |
< |
new_stack.ss_sp = sig_stack; |
737 |
< |
new_stack.ss_flags = 0; |
738 |
< |
new_stack.ss_size = SIG_STACK_SIZE; |
739 |
< |
if (sigaltstack(&new_stack, NULL) < 0) { |
945 |
> |
for (int i = 0; i < SIG_STACK_COUNT; i++) { |
946 |
> |
void *sig_stack = malloc(SIG_STACK_SIZE); |
947 |
> |
D(bug("Signal stack %d at %p\n", i, sig_stack)); |
948 |
> |
if (sig_stack == NULL) { |
949 |
> |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
950 |
> |
goto quit; |
951 |
> |
} |
952 |
> |
sig_stacks[i].ss_sp = sig_stack; |
953 |
> |
sig_stacks[i].ss_flags = 0; |
954 |
> |
sig_stacks[i].ss_size = SIG_STACK_SIZE; |
955 |
> |
} |
956 |
> |
sig_stack_id = 0; |
957 |
> |
if (sigaltstack(&sig_stacks[0], NULL) < 0) { |
958 |
|
sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno)); |
959 |
|
ErrorAlert(str); |
960 |
|
goto quit; |
962 |
|
#endif |
963 |
|
|
964 |
|
#if !EMULATED_PPC |
965 |
< |
// Install SIGSEGV handler |
965 |
> |
// Install SIGSEGV and SIGBUS handlers |
966 |
|
sigemptyset(&sigsegv_action.sa_mask); // Block interrupts during SEGV handling |
967 |
|
sigaddset(&sigsegv_action.sa_mask, SIGUSR2); |
968 |
< |
sigsegv_action.sa_handler = (__sighandler_t)sigsegv_handler; |
969 |
< |
sigsegv_action.sa_flags = SA_ONSTACK; |
968 |
> |
sigsegv_action.sa_sigaction = sigsegv_handler; |
969 |
> |
sigsegv_action.sa_flags = SA_ONSTACK | SA_SIGINFO; |
970 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
971 |
|
sigsegv_action.sa_restorer = NULL; |
972 |
+ |
#endif |
973 |
|
if (sigaction(SIGSEGV, &sigsegv_action, NULL) < 0) { |
974 |
|
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
975 |
|
ErrorAlert(str); |
976 |
|
goto quit; |
977 |
|
} |
978 |
+ |
if (sigaction(SIGBUS, &sigsegv_action, NULL) < 0) { |
979 |
+ |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
980 |
+ |
ErrorAlert(str); |
981 |
+ |
goto quit; |
982 |
+ |
} |
983 |
|
|
984 |
|
// Install SIGILL handler |
985 |
|
sigemptyset(&sigill_action.sa_mask); // Block interrupts during ILL handling |
986 |
|
sigaddset(&sigill_action.sa_mask, SIGUSR2); |
987 |
< |
sigill_action.sa_handler = (__sighandler_t)sigill_handler; |
988 |
< |
sigill_action.sa_flags = SA_ONSTACK; |
987 |
> |
sigill_action.sa_sigaction = sigill_handler; |
988 |
> |
sigill_action.sa_flags = SA_ONSTACK | SA_SIGINFO; |
989 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
990 |
|
sigill_action.sa_restorer = NULL; |
991 |
+ |
#endif |
992 |
|
if (sigaction(SIGILL, &sigill_action, NULL) < 0) { |
993 |
|
sprintf(str, GetString(STR_SIGILL_INSTALL_ERR), strerror(errno)); |
994 |
|
ErrorAlert(str); |
995 |
|
goto quit; |
996 |
|
} |
997 |
+ |
#endif |
998 |
|
|
999 |
+ |
#if !EMULATED_PPC |
1000 |
|
// Install interrupt signal handler |
1001 |
|
sigemptyset(&sigusr2_action.sa_mask); |
1002 |
< |
sigusr2_action.sa_handler = (__sighandler_t)sigusr2_handler; |
1003 |
< |
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART; |
1002 |
> |
sigusr2_action.sa_sigaction = sigusr2_handler; |
1003 |
> |
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART | SA_SIGINFO; |
1004 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
1005 |
|
sigusr2_action.sa_restorer = NULL; |
1006 |
+ |
#endif |
1007 |
|
if (sigaction(SIGUSR2, &sigusr2_action, NULL) < 0) { |
1008 |
|
sprintf(str, GetString(STR_SIGUSR2_INSTALL_ERR), strerror(errno)); |
1009 |
|
ErrorAlert(str); |
1028 |
|
|
1029 |
|
static void Quit(void) |
1030 |
|
{ |
1031 |
+ |
#if EMULATED_PPC |
1032 |
+ |
// Exit PowerPC emulation |
1033 |
+ |
exit_emul_ppc(); |
1034 |
+ |
#endif |
1035 |
+ |
|
1036 |
|
// Stop 60Hz thread |
1037 |
|
if (tick_thread_active) { |
1038 |
+ |
tick_thread_cancel = true; |
1039 |
|
pthread_cancel(tick_thread); |
1040 |
|
pthread_join(tick_thread, NULL); |
1041 |
|
} |
1042 |
|
|
1043 |
|
// Stop NVRAM watchdog thread |
1044 |
|
if (nvram_thread_active) { |
1045 |
+ |
nvram_thread_cancel = true; |
1046 |
|
pthread_cancel(nvram_thread); |
1047 |
|
pthread_join(nvram_thread, NULL); |
1048 |
|
} |
1049 |
|
|
1050 |
|
#if !EMULATED_PPC |
1051 |
< |
// Uninstall SIGSEGV handler |
1051 |
> |
// Uninstall SIGSEGV and SIGBUS handlers |
1052 |
|
sigemptyset(&sigsegv_action.sa_mask); |
1053 |
|
sigsegv_action.sa_handler = SIG_DFL; |
1054 |
|
sigsegv_action.sa_flags = 0; |
1055 |
|
sigaction(SIGSEGV, &sigsegv_action, NULL); |
1056 |
+ |
sigaction(SIGBUS, &sigsegv_action, NULL); |
1057 |
|
|
1058 |
|
// Uninstall SIGILL handler |
1059 |
|
sigemptyset(&sigill_action.sa_mask); |
1060 |
|
sigill_action.sa_handler = SIG_DFL; |
1061 |
|
sigill_action.sa_flags = 0; |
1062 |
|
sigaction(SIGILL, &sigill_action, NULL); |
1063 |
+ |
|
1064 |
+ |
// Delete stacks for signal handlers |
1065 |
+ |
for (int i = 0; i < SIG_STACK_COUNT; i++) { |
1066 |
+ |
void *sig_stack = sig_stacks[i].ss_sp; |
1067 |
+ |
if (sig_stack) |
1068 |
+ |
free(sig_stack); |
1069 |
+ |
} |
1070 |
|
#endif |
1071 |
|
|
1072 |
|
// Save NVRAM |
1087 |
|
// Exit audio |
1088 |
|
AudioExit(); |
1089 |
|
|
1090 |
+ |
// Exit ADB |
1091 |
+ |
ADBExit(); |
1092 |
+ |
|
1093 |
|
// Exit video |
1094 |
|
VideoExit(); |
1095 |
|
|
1102 |
|
DiskExit(); |
1103 |
|
SonyExit(); |
1104 |
|
|
1105 |
+ |
// Delete thunks |
1106 |
+ |
ThunksExit(); |
1107 |
+ |
|
1108 |
+ |
// Delete SheepShaver globals |
1109 |
+ |
SheepMem::Exit(); |
1110 |
+ |
|
1111 |
|
// Delete RAM area |
1112 |
|
if (ram_area_mapped) |
1113 |
< |
munmap(mmap_RAMBase, RAMSize); |
1113 |
> |
vm_release((char *)RAM_BASE, RAMSize); |
1114 |
|
|
1115 |
|
// Delete ROM area |
1116 |
|
if (rom_area_mapped) |
1117 |
< |
munmap((char *)ROM_BASE, ROM_AREA_SIZE); |
1117 |
> |
vm_release((char *)ROM_BASE, ROM_AREA_SIZE); |
1118 |
> |
|
1119 |
> |
// Delete DR cache areas |
1120 |
> |
if (dr_emulator_area_mapped) |
1121 |
> |
vm_release((void *)DR_EMULATOR_BASE, DR_EMULATOR_SIZE); |
1122 |
> |
if (dr_cache_area_mapped) |
1123 |
> |
vm_release((void *)DR_CACHE_BASE, DR_CACHE_SIZE); |
1124 |
|
|
1125 |
|
// Delete Kernel Data area |
1126 |
|
if (kernel_area >= 0) { |
1149 |
|
#endif |
1150 |
|
|
1151 |
|
// Close X11 server connection |
1152 |
+ |
#ifndef USE_SDL_VIDEO |
1153 |
|
if (x_display) |
1154 |
|
XCloseDisplay(x_display); |
1155 |
+ |
#endif |
1156 |
|
|
1157 |
|
exit(0); |
1158 |
|
} |
1163 |
|
*/ |
1164 |
|
|
1165 |
|
#if EMULATED_PPC |
903 |
– |
extern void emul_ppc(uint32 start); |
904 |
– |
extern void init_emul_ppc(void); |
1166 |
|
void jump_to_rom(uint32 entry) |
1167 |
|
{ |
1168 |
|
init_emul_ppc(); |
1231 |
|
|
1232 |
|
|
1233 |
|
/* |
973 |
– |
* Execute PPC code from EMUL_OP routine (real mode switch) |
974 |
– |
*/ |
975 |
– |
|
976 |
– |
void ExecutePPC(void (*func)()) |
977 |
– |
{ |
978 |
– |
uint32 tvect[2] = {(uint32)func, 0}; // Fake TVECT |
979 |
– |
RoutineDescriptor desc = BUILD_PPC_ROUTINE_DESCRIPTOR(0, tvect); |
980 |
– |
M68kRegisters r; |
981 |
– |
Execute68k((uint32)&desc, &r); |
982 |
– |
} |
983 |
– |
|
984 |
– |
|
985 |
– |
/* |
1234 |
|
* Quit emulator (cause return from jump_to_rom) |
1235 |
|
*/ |
1236 |
|
|
1289 |
|
|
1290 |
|
void MakeExecutable(int dummy, void *start, uint32 length) |
1291 |
|
{ |
1292 |
< |
#if !EMULATED_PPC |
1045 |
< |
if (((uint32)start >= ROM_BASE) && ((uint32)start < (ROM_BASE + ROM_SIZE))) |
1292 |
> |
if (((uintptr)start >= ROM_BASE) && ((uintptr)start < (ROM_BASE + ROM_SIZE))) |
1293 |
|
return; |
1294 |
< |
flush_icache_range(start, (void *)((uint32)start + length)); |
1294 |
> |
#if EMULATED_PPC |
1295 |
> |
FlushCodeCache((uintptr)start, (uintptr)start + length); |
1296 |
> |
#else |
1297 |
> |
flush_icache_range(start, (void *)((uintptr)start + length)); |
1298 |
|
#endif |
1299 |
|
} |
1300 |
|
|
1305 |
|
|
1306 |
|
void PatchAfterStartup(void) |
1307 |
|
{ |
1308 |
< |
ExecutePPC(VideoInstallAccel); |
1308 |
> |
ExecuteNative(NATIVE_VIDEO_INSTALL_ACCEL); |
1309 |
|
InstallExtFS(); |
1310 |
|
} |
1311 |
|
|
1314 |
|
* NVRAM watchdog thread (saves NVRAM every minute) |
1315 |
|
*/ |
1316 |
|
|
1317 |
< |
static void *nvram_func(void *arg) |
1317 |
> |
static void nvram_watchdog(void) |
1318 |
|
{ |
1319 |
< |
struct timespec req = {60, 0}; // 1 minute |
1319 |
> |
if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) { |
1320 |
> |
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
1321 |
> |
SaveXPRAM(); |
1322 |
> |
} |
1323 |
> |
} |
1324 |
|
|
1325 |
< |
for (;;) { |
1326 |
< |
pthread_testcancel(); |
1327 |
< |
nanosleep(&req, NULL); |
1328 |
< |
pthread_testcancel(); |
1329 |
< |
if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) { |
1330 |
< |
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
1077 |
< |
SaveXPRAM(); |
1078 |
< |
} |
1325 |
> |
static void *nvram_func(void *arg) |
1326 |
> |
{ |
1327 |
> |
while (!nvram_thread_cancel) { |
1328 |
> |
for (int i=0; i<60 && !nvram_thread_cancel; i++) |
1329 |
> |
Delay_usec(999999); // Only wait 1 second so we quit promptly when nvram_thread_cancel becomes true |
1330 |
> |
nvram_watchdog(); |
1331 |
|
} |
1332 |
|
return NULL; |
1333 |
|
} |
1340 |
|
static void *tick_func(void *arg) |
1341 |
|
{ |
1342 |
|
int tick_counter = 0; |
1343 |
< |
struct timespec req = {0, 16625000}; |
1343 |
> |
uint64 start = GetTicks_usec(); |
1344 |
> |
int64 ticks = 0; |
1345 |
> |
uint64 next = GetTicks_usec(); |
1346 |
|
|
1347 |
< |
for (;;) { |
1347 |
> |
while (!tick_thread_cancel) { |
1348 |
|
|
1349 |
|
// Wait |
1350 |
< |
nanosleep(&req, NULL); |
1350 |
> |
next += 16625; |
1351 |
> |
int64 delay = next - GetTicks_usec(); |
1352 |
> |
if (delay > 0) |
1353 |
> |
Delay_usec(delay); |
1354 |
> |
else if (delay < -16625) |
1355 |
> |
next = GetTicks_usec(); |
1356 |
> |
ticks++; |
1357 |
|
|
1358 |
|
#if !EMULATED_PPC |
1359 |
|
// Did we crash? |
1360 |
|
if (emul_thread_fatal) { |
1361 |
|
|
1362 |
|
// Yes, dump registers |
1363 |
< |
pt_regs *r = (pt_regs *)&sigsegv_regs; |
1363 |
> |
sigregs *r = &sigsegv_regs; |
1364 |
|
char str[256]; |
1365 |
< |
sprintf(str, "SIGSEGV\n" |
1365 |
> |
if (crash_reason == NULL) |
1366 |
> |
crash_reason = "SIGSEGV"; |
1367 |
> |
sprintf(str, "%s\n" |
1368 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1369 |
|
" xer %08lx cr %08lx \n" |
1370 |
|
" r0 %08lx r1 %08lx r2 %08lx r3 %08lx\n" |
1375 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1376 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1377 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1378 |
+ |
crash_reason, |
1379 |
|
r->nip, r->link, r->ctr, r->msr, |
1380 |
|
r->xer, r->ccr, |
1381 |
|
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1411 |
|
TriggerInterrupt(); |
1412 |
|
} |
1413 |
|
} |
1414 |
+ |
|
1415 |
+ |
uint64 end = GetTicks_usec(); |
1416 |
+ |
D(bug("%Ld ticks in %Ld usec = %f ticks/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start))); |
1417 |
|
return NULL; |
1418 |
|
} |
1419 |
|
|
1424 |
|
|
1425 |
|
void Set_pthread_attr(pthread_attr_t *attr, int priority) |
1426 |
|
{ |
1427 |
< |
// nothing to do |
1427 |
> |
#ifdef HAVE_PTHREADS |
1428 |
> |
pthread_attr_init(attr); |
1429 |
> |
#if defined(_POSIX_THREAD_PRIORITY_SCHEDULING) |
1430 |
> |
// Some of these only work for superuser |
1431 |
> |
if (geteuid() == 0) { |
1432 |
> |
pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED); |
1433 |
> |
pthread_attr_setschedpolicy(attr, SCHED_FIFO); |
1434 |
> |
struct sched_param fifo_param; |
1435 |
> |
fifo_param.sched_priority = ((sched_get_priority_min(SCHED_FIFO) + |
1436 |
> |
sched_get_priority_max(SCHED_FIFO)) / 2 + |
1437 |
> |
priority); |
1438 |
> |
pthread_attr_setschedparam(attr, &fifo_param); |
1439 |
> |
} |
1440 |
> |
if (pthread_attr_setscope(attr, PTHREAD_SCOPE_SYSTEM) != 0) { |
1441 |
> |
#ifdef PTHREAD_SCOPE_BOUND_NP |
1442 |
> |
// If system scope is not available (eg. we're not running |
1443 |
> |
// with CAP_SCHED_MGT capability on an SGI box), try bound |
1444 |
> |
// scope. It exposes pthread scheduling to the kernel, |
1445 |
> |
// without setting realtime priority. |
1446 |
> |
pthread_attr_setscope(attr, PTHREAD_SCOPE_BOUND_NP); |
1447 |
> |
#endif |
1448 |
> |
} |
1449 |
> |
#endif |
1450 |
> |
#endif |
1451 |
|
} |
1452 |
|
|
1453 |
|
|
1455 |
|
* Mutexes |
1456 |
|
*/ |
1457 |
|
|
1458 |
+ |
#ifdef HAVE_PTHREADS |
1459 |
+ |
|
1460 |
+ |
struct B2_mutex { |
1461 |
+ |
B2_mutex() { |
1462 |
+ |
pthread_mutexattr_t attr; |
1463 |
+ |
pthread_mutexattr_init(&attr); |
1464 |
+ |
// Initialize the mutex for priority inheritance -- |
1465 |
+ |
// required for accurate timing. |
1466 |
+ |
#ifdef HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL |
1467 |
+ |
pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT); |
1468 |
+ |
#endif |
1469 |
+ |
#if defined(HAVE_PTHREAD_MUTEXATTR_SETTYPE) && defined(PTHREAD_MUTEX_NORMAL) |
1470 |
+ |
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); |
1471 |
+ |
#endif |
1472 |
+ |
#ifdef HAVE_PTHREAD_MUTEXATTR_SETPSHARED |
1473 |
+ |
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE); |
1474 |
+ |
#endif |
1475 |
+ |
pthread_mutex_init(&m, &attr); |
1476 |
+ |
pthread_mutexattr_destroy(&attr); |
1477 |
+ |
} |
1478 |
+ |
~B2_mutex() { |
1479 |
+ |
pthread_mutex_trylock(&m); // Make sure it's locked before |
1480 |
+ |
pthread_mutex_unlock(&m); // unlocking it. |
1481 |
+ |
pthread_mutex_destroy(&m); |
1482 |
+ |
} |
1483 |
+ |
pthread_mutex_t m; |
1484 |
+ |
}; |
1485 |
+ |
|
1486 |
+ |
B2_mutex *B2_create_mutex(void) |
1487 |
+ |
{ |
1488 |
+ |
return new B2_mutex; |
1489 |
+ |
} |
1490 |
+ |
|
1491 |
+ |
void B2_lock_mutex(B2_mutex *mutex) |
1492 |
+ |
{ |
1493 |
+ |
pthread_mutex_lock(&mutex->m); |
1494 |
+ |
} |
1495 |
+ |
|
1496 |
+ |
void B2_unlock_mutex(B2_mutex *mutex) |
1497 |
+ |
{ |
1498 |
+ |
pthread_mutex_unlock(&mutex->m); |
1499 |
+ |
} |
1500 |
+ |
|
1501 |
+ |
void B2_delete_mutex(B2_mutex *mutex) |
1502 |
+ |
{ |
1503 |
+ |
delete mutex; |
1504 |
+ |
} |
1505 |
+ |
|
1506 |
+ |
#else |
1507 |
+ |
|
1508 |
|
struct B2_mutex { |
1509 |
|
int dummy; |
1510 |
|
}; |
1527 |
|
delete mutex; |
1528 |
|
} |
1529 |
|
|
1530 |
+ |
#endif |
1531 |
+ |
|
1532 |
|
|
1533 |
|
/* |
1534 |
|
* Trigger signal USR2 from another thread |
1535 |
|
*/ |
1536 |
|
|
1537 |
+ |
#if !EMULATED_PPC |
1538 |
|
void TriggerInterrupt(void) |
1539 |
|
{ |
1198 |
– |
#if EMULATED_PPC |
1199 |
– |
WriteMacInt32(0x16a, ReadMacInt32(0x16a) + 1); |
1200 |
– |
#else |
1201 |
– |
#if 0 |
1202 |
– |
WriteMacInt32(0x16a, ReadMacInt32(0x16a) + 1); |
1203 |
– |
#else |
1540 |
|
if (ready_for_signals) |
1541 |
|
pthread_kill(emul_thread, SIGUSR2); |
1206 |
– |
#endif |
1207 |
– |
#endif |
1542 |
|
} |
1543 |
+ |
#endif |
1544 |
|
|
1545 |
|
|
1546 |
|
/* |
1566 |
|
|
1567 |
|
void DisableInterrupt(void) |
1568 |
|
{ |
1569 |
+ |
#if EMULATED_PPC |
1570 |
+ |
WriteMacInt32(XLM_IRQ_NEST, int32(ReadMacInt32(XLM_IRQ_NEST)) + 1); |
1571 |
+ |
#else |
1572 |
|
atomic_add((int *)XLM_IRQ_NEST, 1); |
1573 |
+ |
#endif |
1574 |
|
} |
1575 |
|
|
1576 |
|
|
1580 |
|
|
1581 |
|
void EnableInterrupt(void) |
1582 |
|
{ |
1583 |
+ |
#if EMULATED_PPC |
1584 |
+ |
WriteMacInt32(XLM_IRQ_NEST, int32(ReadMacInt32(XLM_IRQ_NEST)) - 1); |
1585 |
+ |
#else |
1586 |
|
atomic_add((int *)XLM_IRQ_NEST, -1); |
1587 |
+ |
#endif |
1588 |
|
} |
1589 |
|
|
1590 |
|
|
1248 |
– |
#if !EMULATED_PPC |
1591 |
|
/* |
1592 |
|
* USR2 handler |
1593 |
|
*/ |
1594 |
|
|
1595 |
< |
static void sigusr2_handler(int sig, sigcontext_struct *sc) |
1595 |
> |
#if !EMULATED_PPC |
1596 |
> |
static void sigusr2_handler(int sig, siginfo_t *sip, void *scp) |
1597 |
|
{ |
1598 |
< |
pt_regs *r = sc->regs; |
1598 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1599 |
> |
|
1600 |
> |
#ifdef USE_SDL_VIDEO |
1601 |
> |
// We must fill in the events queue in the same thread that did call SDL_SetVideoMode() |
1602 |
> |
SDL_PumpEvents(); |
1603 |
> |
#endif |
1604 |
|
|
1605 |
|
// Do nothing if interrupts are disabled |
1606 |
|
if (*(int32 *)XLM_IRQ_NEST > 0) |
1614 |
|
case MODE_68K: |
1615 |
|
// 68k emulator active, trigger 68k interrupt level 1 |
1616 |
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1617 |
< |
r->ccr |= ntohl(kernel_data->v[0x674 >> 2]); |
1617 |
> |
r->cr() |= ntohl(kernel_data->v[0x674 >> 2]); |
1618 |
|
break; |
1619 |
|
|
1620 |
|
#if INTERRUPTS_IN_NATIVE_MODE |
1621 |
|
case MODE_NATIVE: |
1622 |
|
// 68k emulator inactive, in nanokernel? |
1623 |
< |
if (r->gpr[1] != KernelDataAddr) { |
1623 |
> |
if (r->gpr(1) != KernelDataAddr) { |
1624 |
> |
|
1625 |
> |
// Set extra stack for nested interrupts |
1626 |
> |
sig_stack_acquire(); |
1627 |
> |
|
1628 |
|
// Prepare for 68k interrupt level 1 |
1629 |
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1630 |
|
WriteMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc, ReadMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc) | ntohl(kernel_data->v[0x674 >> 2])); |
1631 |
|
|
1632 |
|
// Execute nanokernel interrupt routine (this will activate the 68k emulator) |
1633 |
< |
atomic_add((int32 *)XLM_IRQ_NEST, 1); |
1633 |
> |
DisableInterrupt(); |
1634 |
|
if (ROMType == ROMTYPE_NEWWORLD) |
1635 |
|
ppc_interrupt(ROM_BASE + 0x312b1c, KernelDataAddr); |
1636 |
|
else |
1637 |
|
ppc_interrupt(ROM_BASE + 0x312a3c, KernelDataAddr); |
1638 |
+ |
|
1639 |
+ |
// Reset normal signal stack |
1640 |
+ |
sig_stack_release(); |
1641 |
|
} |
1642 |
|
break; |
1643 |
|
#endif |
1648 |
|
if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) { |
1649 |
|
|
1650 |
|
// Set extra stack for SIGSEGV handler |
1651 |
< |
struct sigaltstack new_stack; |
1297 |
< |
new_stack.ss_sp = extra_stack; |
1298 |
< |
new_stack.ss_flags = 0; |
1299 |
< |
new_stack.ss_size = SIG_STACK_SIZE; |
1300 |
< |
sigaltstack(&new_stack, NULL); |
1651 |
> |
sig_stack_acquire(); |
1652 |
|
#if 1 |
1653 |
|
// Execute full 68k interrupt routine |
1654 |
|
M68kRegisters r; |
1670 |
|
if (InterruptFlags & INTFLAG_VIA) { |
1671 |
|
ClearInterruptFlag(INTFLAG_VIA); |
1672 |
|
ADBInterrupt(); |
1673 |
< |
ExecutePPC(VideoVBL); |
1673 |
> |
ExecuteNative(NATIVE_VIDEO_VBL); |
1674 |
|
} |
1675 |
|
} |
1676 |
|
#endif |
1677 |
|
// Reset normal signal stack |
1678 |
< |
new_stack.ss_sp = sig_stack; |
1328 |
< |
new_stack.ss_flags = 0; |
1329 |
< |
new_stack.ss_size = SIG_STACK_SIZE; |
1330 |
< |
sigaltstack(&new_stack, NULL); |
1678 |
> |
sig_stack_release(); |
1679 |
|
} |
1680 |
|
break; |
1681 |
|
#endif |
1334 |
– |
|
1682 |
|
} |
1683 |
|
} |
1684 |
+ |
#endif |
1685 |
|
|
1686 |
|
|
1687 |
|
/* |
1688 |
|
* SIGSEGV handler |
1689 |
|
*/ |
1690 |
|
|
1691 |
< |
static void sigsegv_handler(int sig, sigcontext_struct *sc) |
1691 |
> |
#if !EMULATED_PPC |
1692 |
> |
static void sigsegv_handler(int sig, siginfo_t *sip, void *scp) |
1693 |
|
{ |
1694 |
< |
pt_regs *r = sc->regs; |
1694 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1695 |
> |
|
1696 |
> |
// Get effective address |
1697 |
> |
uint32 addr = r->dar(); |
1698 |
> |
|
1699 |
> |
#if ENABLE_VOSF |
1700 |
> |
// Handle screen fault. |
1701 |
> |
extern bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction); |
1702 |
> |
if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->pc())) |
1703 |
> |
return; |
1704 |
> |
#endif |
1705 |
> |
|
1706 |
|
num_segv++; |
1707 |
|
|
1708 |
< |
// Fault in Mac ROM or RAM? |
1709 |
< |
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
1708 |
> |
// Fault in Mac ROM or RAM or DR Cache? |
1709 |
> |
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)); |
1710 |
|
if (mac_fault) { |
1711 |
|
|
1352 |
– |
// Get opcode and divide into fields |
1353 |
– |
uint32 opcode = *((uint32 *)r->nip); |
1354 |
– |
uint32 primop = opcode >> 26; |
1355 |
– |
uint32 exop = (opcode >> 1) & 0x3ff; |
1356 |
– |
uint32 ra = (opcode >> 16) & 0x1f; |
1357 |
– |
uint32 rb = (opcode >> 11) & 0x1f; |
1358 |
– |
uint32 rd = (opcode >> 21) & 0x1f; |
1359 |
– |
int32 imm = (int16)(opcode & 0xffff); |
1360 |
– |
|
1712 |
|
// "VM settings" during MacOS 8 installation |
1713 |
< |
if (r->nip == ROM_BASE + 0x488160 && r->gpr[20] == 0xf8000000) { |
1714 |
< |
r->nip += 4; |
1715 |
< |
r->gpr[8] = 0; |
1713 |
> |
if (r->pc() == ROM_BASE + 0x488160 && r->gpr(20) == 0xf8000000) { |
1714 |
> |
r->pc() += 4; |
1715 |
> |
r->gpr(8) = 0; |
1716 |
|
return; |
1717 |
|
|
1718 |
|
// MacOS 8.5 installation |
1719 |
< |
} else if (r->nip == ROM_BASE + 0x488140 && r->gpr[16] == 0xf8000000) { |
1720 |
< |
r->nip += 4; |
1721 |
< |
r->gpr[8] = 0; |
1719 |
> |
} else if (r->pc() == ROM_BASE + 0x488140 && r->gpr(16) == 0xf8000000) { |
1720 |
> |
r->pc() += 4; |
1721 |
> |
r->gpr(8) = 0; |
1722 |
|
return; |
1723 |
|
|
1724 |
|
// MacOS 8 serial drivers on startup |
1725 |
< |
} else if (r->nip == ROM_BASE + 0x48e080 && (r->gpr[8] == 0xf3012002 || r->gpr[8] == 0xf3012000)) { |
1726 |
< |
r->nip += 4; |
1727 |
< |
r->gpr[8] = 0; |
1725 |
> |
} else if (r->pc() == ROM_BASE + 0x48e080 && (r->gpr(8) == 0xf3012002 || r->gpr(8) == 0xf3012000)) { |
1726 |
> |
r->pc() += 4; |
1727 |
> |
r->gpr(8) = 0; |
1728 |
|
return; |
1729 |
|
|
1730 |
|
// MacOS 8.1 serial drivers on startup |
1731 |
< |
} else if (r->nip == ROM_BASE + 0x48c5e0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1732 |
< |
r->nip += 4; |
1731 |
> |
} else if (r->pc() == ROM_BASE + 0x48c5e0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1732 |
> |
r->pc() += 4; |
1733 |
|
return; |
1734 |
< |
} else if (r->nip == ROM_BASE + 0x4a10a0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1735 |
< |
r->nip += 4; |
1734 |
> |
} else if (r->pc() == ROM_BASE + 0x4a10a0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1735 |
> |
r->pc() += 4; |
1736 |
> |
return; |
1737 |
> |
|
1738 |
> |
// MacOS 8.6 serial drivers on startup (with DR Cache and OldWorld ROM) |
1739 |
> |
} else if ((r->pc() - DR_CACHE_BASE) < DR_CACHE_SIZE && (r->gpr(16) == 0xf3012002 || r->gpr(16) == 0xf3012000)) { |
1740 |
> |
r->pc() += 4; |
1741 |
> |
return; |
1742 |
> |
} else if ((r->pc() - DR_CACHE_BASE) < DR_CACHE_SIZE && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1743 |
> |
r->pc() += 4; |
1744 |
|
return; |
1745 |
|
} |
1746 |
|
|
1747 |
+ |
// Get opcode and divide into fields |
1748 |
+ |
uint32 opcode = *((uint32 *)r->pc()); |
1749 |
+ |
uint32 primop = opcode >> 26; |
1750 |
+ |
uint32 exop = (opcode >> 1) & 0x3ff; |
1751 |
+ |
uint32 ra = (opcode >> 16) & 0x1f; |
1752 |
+ |
uint32 rb = (opcode >> 11) & 0x1f; |
1753 |
+ |
uint32 rd = (opcode >> 21) & 0x1f; |
1754 |
+ |
int32 imm = (int16)(opcode & 0xffff); |
1755 |
+ |
|
1756 |
|
// Analyze opcode |
1757 |
|
enum { |
1758 |
|
TYPE_UNKNOWN, |
1834 |
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_NORM; break; |
1835 |
|
case 45: // sthu |
1836 |
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_U; break; |
1837 |
< |
} |
1838 |
< |
|
1839 |
< |
// Calculate effective address |
1840 |
< |
uint32 addr = 0; |
1841 |
< |
switch (addr_mode) { |
1842 |
< |
case MODE_X: |
1843 |
< |
case MODE_UX: |
1844 |
< |
if (ra == 0) |
1845 |
< |
addr = r->gpr[rb]; |
1846 |
< |
else |
1847 |
< |
addr = r->gpr[ra] + r->gpr[rb]; |
1848 |
< |
break; |
1481 |
< |
case MODE_NORM: |
1482 |
< |
case MODE_U: |
1483 |
< |
if (ra == 0) |
1484 |
< |
addr = (int32)(int16)imm; |
1485 |
< |
else |
1486 |
< |
addr = r->gpr[ra] + (int32)(int16)imm; |
1837 |
> |
#if EMULATE_UNALIGNED_LOADSTORE_MULTIPLE |
1838 |
> |
case 46: // lmw |
1839 |
> |
if ((addr % 4) != 0) { |
1840 |
> |
uint32 ea = addr; |
1841 |
> |
D(bug("WARNING: unaligned lmw to EA=%08x from IP=%08x\n", ea, r->pc())); |
1842 |
> |
for (int i = rd; i <= 31; i++) { |
1843 |
> |
r->gpr(i) = ReadMacInt32(ea); |
1844 |
> |
ea += 4; |
1845 |
> |
} |
1846 |
> |
r->pc() += 4; |
1847 |
> |
goto rti; |
1848 |
> |
} |
1849 |
|
break; |
1850 |
< |
default: |
1850 |
> |
case 47: // stmw |
1851 |
> |
if ((addr % 4) != 0) { |
1852 |
> |
uint32 ea = addr; |
1853 |
> |
D(bug("WARNING: unaligned stmw to EA=%08x from IP=%08x\n", ea, r->pc())); |
1854 |
> |
for (int i = rd; i <= 31; i++) { |
1855 |
> |
WriteMacInt32(ea, r->gpr(i)); |
1856 |
> |
ea += 4; |
1857 |
> |
} |
1858 |
> |
r->pc() += 4; |
1859 |
> |
goto rti; |
1860 |
> |
} |
1861 |
|
break; |
1862 |
+ |
#endif |
1863 |
|
} |
1864 |
< |
|
1865 |
< |
// Ignore ROM writes |
1866 |
< |
if (transfer_type == TYPE_STORE && addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) { |
1867 |
< |
// 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)); |
1864 |
> |
|
1865 |
> |
// Ignore ROM writes (including to the zero page, which is read-only) |
1866 |
> |
if (transfer_type == TYPE_STORE && |
1867 |
> |
((addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) || |
1868 |
> |
(addr >= SheepMem::ZeroPage() && addr < SheepMem::ZeroPage() + SheepMem::PageSize()))) { |
1869 |
> |
// 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())); |
1870 |
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1871 |
< |
r->gpr[ra] = addr; |
1872 |
< |
r->nip += 4; |
1871 |
> |
r->gpr(ra) = addr; |
1872 |
> |
r->pc() += 4; |
1873 |
|
goto rti; |
1874 |
|
} |
1875 |
|
|
1876 |
|
// Ignore illegal memory accesses? |
1877 |
|
if (PrefsFindBool("ignoresegv")) { |
1878 |
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1879 |
< |
r->gpr[ra] = addr; |
1879 |
> |
r->gpr(ra) = addr; |
1880 |
|
if (transfer_type == TYPE_LOAD) |
1881 |
< |
r->gpr[rd] = 0; |
1882 |
< |
r->nip += 4; |
1881 |
> |
r->gpr(rd) = 0; |
1882 |
> |
r->pc() += 4; |
1883 |
|
goto rti; |
1884 |
|
} |
1885 |
|
|
1887 |
|
if (!PrefsFindBool("nogui")) { |
1888 |
|
char str[256]; |
1889 |
|
if (transfer_type == TYPE_LOAD || transfer_type == TYPE_STORE) |
1890 |
< |
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]); |
1890 |
> |
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)); |
1891 |
|
else |
1892 |
< |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode); |
1892 |
> |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode); |
1893 |
|
ErrorAlert(str); |
1894 |
|
QuitEmulator(); |
1895 |
|
return; |
1897 |
|
} |
1898 |
|
|
1899 |
|
// For all other errors, jump into debugger (sort of...) |
1900 |
+ |
crash_reason = (sig == SIGBUS) ? "SIGBUS" : "SIGSEGV"; |
1901 |
|
if (!ready_for_signals) { |
1902 |
< |
printf("SIGSEGV\n"); |
1903 |
< |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
1902 |
> |
printf("%s\n"); |
1903 |
> |
printf(" sigcontext %p, machine_regs %p\n", scp, r); |
1904 |
|
printf( |
1905 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1906 |
|
" xer %08lx cr %08lx \n" |
1912 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1913 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1914 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1915 |
< |
r->nip, r->link, r->ctr, r->msr, |
1916 |
< |
r->xer, r->ccr, |
1917 |
< |
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1918 |
< |
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
1919 |
< |
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
1920 |
< |
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
1921 |
< |
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
1922 |
< |
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
1923 |
< |
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
1924 |
< |
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
1915 |
> |
crash_reason, |
1916 |
> |
r->pc(), r->lr(), r->ctr(), r->msr(), |
1917 |
> |
r->xer(), r->cr(), |
1918 |
> |
r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3), |
1919 |
> |
r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7), |
1920 |
> |
r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11), |
1921 |
> |
r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15), |
1922 |
> |
r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19), |
1923 |
> |
r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23), |
1924 |
> |
r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27), |
1925 |
> |
r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31)); |
1926 |
|
exit(1); |
1927 |
|
QuitEmulator(); |
1928 |
|
return; |
1929 |
|
} else { |
1930 |
|
// We crashed. Save registers, tell tick thread and loop forever |
1931 |
< |
sigsegv_regs = *(sigregs *)r; |
1931 |
> |
build_sigregs(&sigsegv_regs, r); |
1932 |
|
emul_thread_fatal = true; |
1933 |
|
for (;;) ; |
1934 |
|
} |
1940 |
|
* SIGILL handler |
1941 |
|
*/ |
1942 |
|
|
1943 |
< |
static void sigill_handler(int sig, sigcontext_struct *sc) |
1943 |
> |
static void sigill_handler(int sig, siginfo_t *sip, void *scp) |
1944 |
|
{ |
1945 |
< |
pt_regs *r = sc->regs; |
1945 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1946 |
|
char str[256]; |
1947 |
|
|
1948 |
|
// Fault in Mac ROM or RAM? |
1949 |
< |
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
1949 |
> |
bool mac_fault = (r->pc() >= ROM_BASE) && (r->pc() < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize)); |
1950 |
|
if (mac_fault) { |
1951 |
|
|
1952 |
|
// Get opcode and divide into fields |
1953 |
< |
uint32 opcode = *((uint32 *)r->nip); |
1953 |
> |
uint32 opcode = *((uint32 *)r->pc()); |
1954 |
|
uint32 primop = opcode >> 26; |
1955 |
|
uint32 exop = (opcode >> 1) & 0x3ff; |
1956 |
|
uint32 ra = (opcode >> 16) & 0x1f; |
1961 |
|
switch (primop) { |
1962 |
|
case 9: // POWER instructions |
1963 |
|
case 22: |
1964 |
< |
power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->nip, r->gpr[1], opcode); |
1964 |
> |
power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->pc(), r->gpr(1), opcode); |
1965 |
|
ErrorAlert(str); |
1966 |
|
QuitEmulator(); |
1967 |
|
return; |
1969 |
|
case 31: |
1970 |
|
switch (exop) { |
1971 |
|
case 83: // mfmsr |
1972 |
< |
r->gpr[rd] = 0xf072; |
1973 |
< |
r->nip += 4; |
1972 |
> |
r->gpr(rd) = 0xf072; |
1973 |
> |
r->pc() += 4; |
1974 |
|
goto rti; |
1975 |
|
|
1976 |
|
case 210: // mtsr |
1977 |
|
case 242: // mtsrin |
1978 |
|
case 306: // tlbie |
1979 |
< |
r->nip += 4; |
1979 |
> |
r->pc() += 4; |
1980 |
|
goto rti; |
1981 |
|
|
1982 |
|
case 339: { // mfspr |
1992 |
|
case 957: // PMC3 |
1993 |
|
case 958: // PMC4 |
1994 |
|
case 959: // SDA |
1995 |
< |
r->nip += 4; |
1995 |
> |
r->pc() += 4; |
1996 |
|
goto rti; |
1997 |
|
case 25: // SDR1 |
1998 |
< |
r->gpr[rd] = 0xdead001f; |
1999 |
< |
r->nip += 4; |
1998 |
> |
r->gpr(rd) = 0xdead001f; |
1999 |
> |
r->pc() += 4; |
2000 |
|
goto rti; |
2001 |
|
case 287: // PVR |
2002 |
< |
r->gpr[rd] = PVR; |
2003 |
< |
r->nip += 4; |
2002 |
> |
r->gpr(rd) = PVR; |
2003 |
> |
r->pc() += 4; |
2004 |
|
goto rti; |
2005 |
|
} |
2006 |
|
break; |
2036 |
|
case 957: // PMC3 |
2037 |
|
case 958: // PMC4 |
2038 |
|
case 959: // SDA |
2039 |
< |
r->nip += 4; |
2039 |
> |
r->pc() += 4; |
2040 |
|
goto rti; |
2041 |
|
} |
2042 |
|
break; |
2055 |
|
|
2056 |
|
// In GUI mode, show error alert |
2057 |
|
if (!PrefsFindBool("nogui")) { |
2058 |
< |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode); |
2058 |
> |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode); |
2059 |
|
ErrorAlert(str); |
2060 |
|
QuitEmulator(); |
2061 |
|
return; |
2063 |
|
} |
2064 |
|
|
2065 |
|
// For all other errors, jump into debugger (sort of...) |
2066 |
+ |
crash_reason = "SIGILL"; |
2067 |
|
if (!ready_for_signals) { |
2068 |
< |
printf("SIGILL\n"); |
2069 |
< |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
2068 |
> |
printf("%s\n"); |
2069 |
> |
printf(" sigcontext %p, machine_regs %p\n", scp, r); |
2070 |
|
printf( |
2071 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
2072 |
|
" xer %08lx cr %08lx \n" |
2078 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
2079 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
2080 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
2081 |
< |
r->nip, r->link, r->ctr, r->msr, |
2082 |
< |
r->xer, r->ccr, |
2083 |
< |
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
2084 |
< |
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
2085 |
< |
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
2086 |
< |
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
2087 |
< |
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
2088 |
< |
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
2089 |
< |
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
2090 |
< |
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
2081 |
> |
crash_reason, |
2082 |
> |
r->pc(), r->lr(), r->ctr(), r->msr(), |
2083 |
> |
r->xer(), r->cr(), |
2084 |
> |
r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3), |
2085 |
> |
r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7), |
2086 |
> |
r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11), |
2087 |
> |
r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15), |
2088 |
> |
r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19), |
2089 |
> |
r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23), |
2090 |
> |
r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27), |
2091 |
> |
r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31)); |
2092 |
|
exit(1); |
2093 |
|
QuitEmulator(); |
2094 |
|
return; |
2095 |
|
} else { |
2096 |
|
// We crashed. Save registers, tell tick thread and loop forever |
2097 |
< |
sigsegv_regs = *(sigregs *)r; |
2097 |
> |
build_sigregs(&sigsegv_regs, r); |
2098 |
|
emul_thread_fatal = true; |
2099 |
|
for (;;) ; |
2100 |
|
} |
2104 |
|
|
2105 |
|
|
2106 |
|
/* |
2107 |
+ |
* Helpers to share 32-bit addressable data with MacOS |
2108 |
+ |
*/ |
2109 |
+ |
|
2110 |
+ |
bool SheepMem::Init(void) |
2111 |
+ |
{ |
2112 |
+ |
// Size of a native page |
2113 |
+ |
page_size = getpagesize(); |
2114 |
+ |
|
2115 |
+ |
// Allocate SheepShaver globals |
2116 |
+ |
if (vm_acquire_fixed((char *)base, size) < 0) |
2117 |
+ |
return false; |
2118 |
+ |
|
2119 |
+ |
// Allocate page with all bits set to 0 |
2120 |
+ |
zero_page = base + size; |
2121 |
+ |
if (vm_acquire_fixed((char *)zero_page, page_size) < 0) |
2122 |
+ |
return false; |
2123 |
+ |
memset((char *)zero_page, 0, page_size); |
2124 |
+ |
if (vm_protect((char *)zero_page, page_size, VM_PAGE_READ) < 0) |
2125 |
+ |
return false; |
2126 |
+ |
|
2127 |
+ |
#if EMULATED_PPC |
2128 |
+ |
// Allocate alternate stack for PowerPC interrupt routine |
2129 |
+ |
sig_stack = zero_page + page_size; |
2130 |
+ |
if (vm_acquire_fixed((char *)sig_stack, SIG_STACK_SIZE) < 0) |
2131 |
+ |
return false; |
2132 |
+ |
#endif |
2133 |
+ |
|
2134 |
+ |
top = base + size; |
2135 |
+ |
return true; |
2136 |
+ |
} |
2137 |
+ |
|
2138 |
+ |
void SheepMem::Exit(void) |
2139 |
+ |
{ |
2140 |
+ |
if (top) { |
2141 |
+ |
// Delete SheepShaver globals |
2142 |
+ |
vm_release((void *)base, size); |
2143 |
+ |
|
2144 |
+ |
// Delete zero page |
2145 |
+ |
vm_release((void *)zero_page, page_size); |
2146 |
+ |
|
2147 |
+ |
#if EMULATED_PPC |
2148 |
+ |
// Delete alternate stack for PowerPC interrupt routine |
2149 |
+ |
vm_release((void *)sig_stack, SIG_STACK_SIZE); |
2150 |
+ |
#endif |
2151 |
+ |
} |
2152 |
+ |
} |
2153 |
+ |
|
2154 |
+ |
|
2155 |
+ |
/* |
2156 |
|
* Display alert |
2157 |
|
*/ |
2158 |
|
|
2201 |
|
|
2202 |
|
void ErrorAlert(const char *text) |
2203 |
|
{ |
2204 |
< |
#ifdef ENABLE_GTK |
2204 |
> |
#if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO) |
2205 |
|
if (PrefsFindBool("nogui") || x_display == NULL) { |
2206 |
|
printf(GetString(STR_SHELL_ERROR_PREFIX), text); |
2207 |
|
return; |
2220 |
|
|
2221 |
|
void WarningAlert(const char *text) |
2222 |
|
{ |
2223 |
< |
#ifdef ENABLE_GTK |
2223 |
> |
#if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO) |
2224 |
|
if (PrefsFindBool("nogui") || x_display == NULL) { |
2225 |
|
printf(GetString(STR_SHELL_WARNING_PREFIX), text); |
2226 |
|
return; |