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" |
144 |
|
#endif |
145 |
|
|
146 |
|
|
147 |
+ |
// Enable emulation of unaligned lmw/stmw? |
148 |
+ |
#define EMULATE_UNALIGNED_LOADSTORE_MULTIPLE 1 |
149 |
+ |
|
150 |
|
// Enable Execute68k() safety checks? |
151 |
|
#define SAFE_EXEC_68K 0 |
152 |
|
|
156 |
|
// Interrupts in native mode? |
157 |
|
#define INTERRUPTS_IN_NATIVE_MODE 1 |
158 |
|
|
159 |
+ |
// Number of alternate stacks for signal handlers? |
160 |
+ |
#define SIG_STACK_COUNT 4 |
161 |
+ |
|
162 |
|
|
163 |
|
// Constants |
164 |
|
const char ROM_FILE_NAME[] = "ROM"; |
165 |
|
const char ROM_FILE_NAME2[] = "Mac OS ROM"; |
166 |
|
|
167 |
< |
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 |
< |
|
167 |
> |
const uintptr RAM_BASE = 0x20000000; // Base address of RAM |
168 |
|
const uint32 SIG_STACK_SIZE = 0x10000; // Size of signal stack |
169 |
|
|
170 |
|
|
171 |
< |
// 68k Emulator Data |
172 |
< |
struct EmulatorData { |
173 |
< |
uint32 v[0x400]; |
171 |
> |
#if !EMULATED_PPC |
172 |
> |
struct sigregs { |
173 |
> |
uint32 nip; |
174 |
> |
uint32 link; |
175 |
> |
uint32 ctr; |
176 |
> |
uint32 msr; |
177 |
> |
uint32 xer; |
178 |
> |
uint32 ccr; |
179 |
> |
uint32 gpr[32]; |
180 |
|
}; |
181 |
|
|
182 |
+ |
#if defined(__linux__) |
183 |
+ |
#include <sys/ucontext.h> |
184 |
+ |
#define MACHINE_REGISTERS(scp) ((machine_regs *)(((ucontext_t *)scp)->uc_mcontext.regs)) |
185 |
+ |
|
186 |
+ |
struct machine_regs : public pt_regs |
187 |
+ |
{ |
188 |
+ |
u_long & cr() { return pt_regs::ccr; } |
189 |
+ |
uint32 cr() const { return pt_regs::ccr; } |
190 |
+ |
uint32 lr() const { return pt_regs::link; } |
191 |
+ |
uint32 ctr() const { return pt_regs::ctr; } |
192 |
+ |
uint32 xer() const { return pt_regs::xer; } |
193 |
+ |
uint32 msr() const { return pt_regs::msr; } |
194 |
+ |
uint32 dar() const { return pt_regs::dar; } |
195 |
+ |
u_long & pc() { return pt_regs::nip; } |
196 |
+ |
uint32 pc() const { return pt_regs::nip; } |
197 |
+ |
u_long & gpr(int i) { return pt_regs::gpr[i]; } |
198 |
+ |
uint32 gpr(int i) const { return pt_regs::gpr[i]; } |
199 |
+ |
}; |
200 |
+ |
#endif |
201 |
|
|
202 |
< |
// Kernel Data |
203 |
< |
struct KernelData { |
204 |
< |
uint32 v[0x400]; |
205 |
< |
EmulatorData ed; |
202 |
> |
#if defined(__APPLE__) && defined(__MACH__) |
203 |
> |
#include <sys/signal.h> |
204 |
> |
extern "C" int sigaltstack(const struct sigaltstack *ss, struct sigaltstack *oss); |
205 |
> |
|
206 |
> |
#include <sys/ucontext.h> |
207 |
> |
#define MACHINE_REGISTERS(scp) ((machine_regs *)(((ucontext_t *)scp)->uc_mcontext)) |
208 |
> |
|
209 |
> |
struct machine_regs : public mcontext |
210 |
> |
{ |
211 |
> |
uint32 & cr() { return ss.cr; } |
212 |
> |
uint32 cr() const { return ss.cr; } |
213 |
> |
uint32 lr() const { return ss.lr; } |
214 |
> |
uint32 ctr() const { return ss.ctr; } |
215 |
> |
uint32 xer() const { return ss.xer; } |
216 |
> |
uint32 msr() const { return ss.srr1; } |
217 |
> |
uint32 dar() const { return es.dar; } |
218 |
> |
uint32 & pc() { return ss.srr0; } |
219 |
> |
uint32 pc() const { return ss.srr0; } |
220 |
> |
uint32 & gpr(int i) { return (&ss.r0)[i]; } |
221 |
> |
uint32 gpr(int i) const { return (&ss.r0)[i]; } |
222 |
|
}; |
223 |
+ |
#endif |
224 |
|
|
225 |
+ |
static void build_sigregs(sigregs *srp, machine_regs *mrp) |
226 |
+ |
{ |
227 |
+ |
srp->nip = mrp->pc(); |
228 |
+ |
srp->link = mrp->lr(); |
229 |
+ |
srp->ctr = mrp->ctr(); |
230 |
+ |
srp->msr = mrp->msr(); |
231 |
+ |
srp->xer = mrp->xer(); |
232 |
+ |
srp->ccr = mrp->cr(); |
233 |
+ |
for (int i = 0; i < 32; i++) |
234 |
+ |
srp->gpr[i] = mrp->gpr(i); |
235 |
+ |
} |
236 |
|
|
237 |
< |
#if !EMULATED_PPC |
238 |
< |
// 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> |
237 |
> |
static struct sigaltstack sig_stacks[SIG_STACK_COUNT]; // Stacks for signal handlers |
238 |
> |
static int sig_stack_id = 0; // Stack slot currently used |
239 |
|
|
240 |
< |
struct sigregs { |
241 |
< |
elf_gregset_t gp_regs; // Identical to pt_regs |
242 |
< |
double fp_regs[ELF_NFPREG]; // f0..f31 and fpsrc |
243 |
< |
//more (uninteresting) stuff following here |
244 |
< |
}; |
240 |
> |
static inline void sig_stack_acquire(void) |
241 |
> |
{ |
242 |
> |
if (++sig_stack_id == SIG_STACK_COUNT) { |
243 |
> |
printf("FATAL: signal stack overflow\n"); |
244 |
> |
return; |
245 |
> |
} |
246 |
> |
sigaltstack(&sig_stacks[sig_stack_id], NULL); |
247 |
> |
} |
248 |
> |
|
249 |
> |
static inline void sig_stack_release(void) |
250 |
> |
{ |
251 |
> |
if (--sig_stack_id < 0) { |
252 |
> |
printf("FATAL: signal stack underflow\n"); |
253 |
> |
return; |
254 |
> |
} |
255 |
> |
sigaltstack(&sig_stacks[sig_stack_id], NULL); |
256 |
> |
} |
257 |
|
#endif |
258 |
|
|
259 |
|
|
265 |
|
uint32 RAMSize; // Size of Mac RAM |
266 |
|
uint32 KernelDataAddr; // Address of Kernel Data |
267 |
|
uint32 BootGlobsAddr; // Address of BootGlobs structure at top of Mac RAM |
268 |
+ |
uint32 DRCacheAddr; // Address of DR Cache |
269 |
|
uint32 PVR; // Theoretical PVR |
270 |
|
int64 CPUClockSpeed; // Processor clock speed (Hz) |
271 |
|
int64 BusClockSpeed; // Bus clock speed (Hz) |
272 |
|
|
273 |
|
|
274 |
|
// Global variables |
275 |
< |
static char *x_display_name = NULL; // X11 display name |
275 |
> |
char *x_display_name = NULL; // X11 display name |
276 |
|
Display *x_display = NULL; // X11 display handle |
277 |
+ |
#ifdef X11_LOCK_TYPE |
278 |
+ |
X11_LOCK_TYPE x_display_lock = X11_LOCK_INIT; // X11 display lock |
279 |
+ |
#endif |
280 |
|
|
281 |
|
static int zero_fd = 0; // FD of /dev/zero |
282 |
|
static bool lm_area_mapped = false; // Flag: Low Memory area mmap()ped |
283 |
|
static int kernel_area = -1; // SHM ID of Kernel Data area |
284 |
|
static bool rom_area_mapped = false; // Flag: Mac ROM mmap()ped |
285 |
|
static bool ram_area_mapped = false; // Flag: Mac RAM mmap()ped |
286 |
< |
static void *mmap_RAMBase = NULL; // Base address of mmap()ed RAM area |
286 |
> |
static bool dr_cache_area_mapped = false; // Flag: Mac DR Cache mmap()ped |
287 |
> |
static bool dr_emulator_area_mapped = false;// Flag: Mac DR Emulator mmap()ped |
288 |
|
static KernelData *kernel_data; // Pointer to Kernel Data |
289 |
|
static EmulatorData *emulator_data; |
290 |
|
|
299 |
|
static bool ready_for_signals = false; // Handler installed, signals can be sent |
300 |
|
static int64 num_segv = 0; // Number of handled SEGV signals |
301 |
|
|
223 |
– |
#if !EMULATED_PPC |
302 |
|
static struct sigaction sigusr2_action; // Interrupt signal (of emulator thread) |
303 |
+ |
#if EMULATED_PPC |
304 |
+ |
static uintptr sig_stack = 0; // Stack for PowerPC interrupt routine |
305 |
+ |
#else |
306 |
|
static struct sigaction sigsegv_action; // Data access exception signal (of emulator thread) |
307 |
|
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 |
308 |
|
static bool emul_thread_fatal = false; // Flag: MacOS thread crashed, tick thread shall dump debug output |
309 |
|
static sigregs sigsegv_regs; // Register dump when crashed |
310 |
+ |
static const char *crash_reason = NULL; // Reason of the crash (SIGSEGV, SIGBUS, SIGILL) |
311 |
|
#endif |
312 |
|
|
313 |
+ |
uint32 SheepMem::page_size; // Size of a native page |
314 |
+ |
uintptr SheepMem::zero_page = 0; // Address of ro page filled in with zeros |
315 |
+ |
uintptr SheepMem::base = 0x60000000; // Address of SheepShaver data |
316 |
+ |
uintptr SheepMem::top = 0; // Top of SheepShaver data (stack like storage) |
317 |
+ |
|
318 |
|
|
319 |
|
// Prototypes |
320 |
|
static void Quit(void); |
321 |
|
static void *emul_func(void *arg); |
322 |
|
static void *nvram_func(void *arg); |
323 |
|
static void *tick_func(void *arg); |
324 |
< |
#if !EMULATED_PPC |
325 |
< |
static void sigusr2_handler(int sig, sigcontext_struct *sc); |
326 |
< |
static void sigsegv_handler(int sig, sigcontext_struct *sc); |
327 |
< |
static void sigill_handler(int sig, sigcontext_struct *sc); |
324 |
> |
#if EMULATED_PPC |
325 |
> |
extern void emul_ppc(uint32 start); |
326 |
> |
extern void init_emul_ppc(void); |
327 |
> |
extern void exit_emul_ppc(void); |
328 |
> |
#else |
329 |
> |
static void sigusr2_handler(int sig, siginfo_t *sip, void *scp); |
330 |
> |
static void sigsegv_handler(int sig, siginfo_t *sip, void *scp); |
331 |
> |
static void sigill_handler(int sig, siginfo_t *sip, void *scp); |
332 |
|
#endif |
333 |
|
|
334 |
|
|
335 |
|
// From asm_linux.S |
336 |
< |
#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 |
336 |
> |
#if !EMULATED_PPC |
337 |
|
extern "C" void *get_toc(void); |
338 |
|
extern "C" void *get_sp(void); |
339 |
|
extern "C" void flush_icache_range(void *start, void *end); |
348 |
|
#endif |
349 |
|
|
350 |
|
|
351 |
< |
// Decode LZSS data |
352 |
< |
static void decode_lzss(const uint8 *src, uint8 *dest, int size) |
351 |
> |
#if EMULATED_PPC |
352 |
> |
/* |
353 |
> |
* Return signal stack base |
354 |
> |
*/ |
355 |
> |
|
356 |
> |
uintptr SignalStackBase(void) |
357 |
|
{ |
358 |
< |
char dict[0x1000]; |
359 |
< |
int run_mask = 0, dict_idx = 0xfee; |
360 |
< |
for (;;) { |
361 |
< |
if (run_mask < 0x100) { |
362 |
< |
// Start new run |
363 |
< |
if (--size < 0) |
364 |
< |
break; |
365 |
< |
run_mask = *src++ | 0xff00; |
366 |
< |
} |
367 |
< |
bool bit = run_mask & 1; |
368 |
< |
run_mask >>= 1; |
369 |
< |
if (bit) { |
370 |
< |
// Verbatim copy |
371 |
< |
if (--size < 0) |
372 |
< |
break; |
373 |
< |
int c = *src++; |
374 |
< |
dict[dict_idx++] = c; |
375 |
< |
*dest++ = c; |
376 |
< |
dict_idx &= 0xfff; |
377 |
< |
} else { |
378 |
< |
// Copy from dictionary |
379 |
< |
if (--size < 0) |
380 |
< |
break; |
381 |
< |
int idx = *src++; |
382 |
< |
if (--size < 0) |
383 |
< |
break; |
384 |
< |
int cnt = *src++; |
385 |
< |
idx |= (cnt << 4) & 0xf00; |
386 |
< |
cnt = (cnt & 0x0f) + 3; |
387 |
< |
while (cnt--) { |
388 |
< |
char c = dict[idx++]; |
300 |
< |
dict[dict_idx++] = c; |
301 |
< |
*dest++ = c; |
302 |
< |
idx &= 0xfff; |
303 |
< |
dict_idx &= 0xfff; |
304 |
< |
} |
305 |
< |
} |
306 |
< |
} |
358 |
> |
return sig_stack + SIG_STACK_SIZE; |
359 |
> |
} |
360 |
> |
|
361 |
> |
|
362 |
> |
/* |
363 |
> |
* Atomic operations |
364 |
> |
*/ |
365 |
> |
|
366 |
> |
#if HAVE_SPINLOCKS |
367 |
> |
static spinlock_t atomic_ops_lock = SPIN_LOCK_UNLOCKED; |
368 |
> |
#else |
369 |
> |
#define spin_lock(LOCK) |
370 |
> |
#define spin_unlock(LOCK) |
371 |
> |
#endif |
372 |
> |
|
373 |
> |
int atomic_add(int *var, int v) |
374 |
> |
{ |
375 |
> |
spin_lock(&atomic_ops_lock); |
376 |
> |
int ret = *var; |
377 |
> |
*var += v; |
378 |
> |
spin_unlock(&atomic_ops_lock); |
379 |
> |
return ret; |
380 |
> |
} |
381 |
> |
|
382 |
> |
int atomic_and(int *var, int v) |
383 |
> |
{ |
384 |
> |
spin_lock(&atomic_ops_lock); |
385 |
> |
int ret = *var; |
386 |
> |
*var &= v; |
387 |
> |
spin_unlock(&atomic_ops_lock); |
388 |
> |
return ret; |
389 |
|
} |
390 |
|
|
391 |
+ |
int atomic_or(int *var, int v) |
392 |
+ |
{ |
393 |
+ |
spin_lock(&atomic_ops_lock); |
394 |
+ |
int ret = *var; |
395 |
+ |
*var |= v; |
396 |
+ |
spin_unlock(&atomic_ops_lock); |
397 |
+ |
return ret; |
398 |
+ |
} |
399 |
+ |
#endif |
400 |
+ |
|
401 |
|
|
402 |
|
/* |
403 |
|
* Main program |
417 |
|
char str[256]; |
418 |
|
uint32 *boot_globs; |
419 |
|
int16 i16; |
328 |
– |
int drive, driver; |
420 |
|
int rom_fd; |
421 |
|
FILE *proc_file; |
422 |
|
const char *rom_path; |
426 |
|
|
427 |
|
// Initialize variables |
428 |
|
RAMBase = 0; |
338 |
– |
mmap_RAMBase = NULL; |
429 |
|
tzset(); |
430 |
|
|
431 |
|
// Print some info |
483 |
|
PVR = 0x00040000; // Default: 604 |
484 |
|
CPUClockSpeed = 100000000; // Default: 100MHz |
485 |
|
BusClockSpeed = 100000000; // Default: 100MHz |
486 |
< |
#if !EMULATED_PPC |
486 |
> |
#if EMULATED_PPC |
487 |
> |
PVR = 0x000c0000; // Default: 7400 (with AltiVec) |
488 |
> |
#else |
489 |
|
proc_file = fopen("/proc/cpuinfo", "r"); |
490 |
|
if (proc_file) { |
491 |
|
char line[256]; |
499 |
|
// Parse line |
500 |
|
int i; |
501 |
|
char value[256]; |
502 |
< |
if (sscanf(line, "cpu : %s", value) == 1) { |
502 |
> |
if (sscanf(line, "cpu : %[0-9A-Za-a]", value) == 1) { |
503 |
|
if (strcmp(value, "601") == 0) |
504 |
|
PVR = 0x00010000; |
505 |
|
else if (strcmp(value, "603") == 0) |
520 |
|
PVR = 0x00320000; |
521 |
|
else if (strcmp(value, "860") == 0) |
522 |
|
PVR = 0x00500000; |
523 |
+ |
else if (strcmp(value, "7400") == 0) |
524 |
+ |
PVR = 0x000c0000; |
525 |
+ |
else if (strcmp(value, "7410") == 0) |
526 |
+ |
PVR = 0x800c0000; |
527 |
|
else |
528 |
|
printf("WARNING: Unknown CPU type '%s', assuming 604\n", value); |
529 |
|
} |
535 |
|
sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno)); |
536 |
|
WarningAlert(str); |
537 |
|
} |
538 |
+ |
|
539 |
+ |
// Get actual bus frequency |
540 |
+ |
proc_file = fopen("/proc/device-tree/clock-frequency", "r"); |
541 |
+ |
if (proc_file) { |
542 |
+ |
union { uint8 b[4]; uint32 l; } value; |
543 |
+ |
if (fread(value.b, sizeof(value), 1, proc_file) == 1) |
544 |
+ |
BusClockSpeed = value.l; |
545 |
+ |
fclose(proc_file); |
546 |
+ |
} |
547 |
|
#endif |
548 |
|
D(bug("PVR: %08x (assumed)\n", PVR)); |
549 |
|
|
568 |
|
goto quit; |
569 |
|
} |
570 |
|
|
571 |
+ |
#ifndef PAGEZERO_HACK |
572 |
|
// Create Low Memory area (0x0000..0x3000) |
573 |
< |
if (mmap((char *)0x0000, 0x3000, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, zero_fd, 0) == (void *)-1) { |
573 |
> |
if (vm_acquire_fixed((char *)0, 0x3000) < 0) { |
574 |
|
sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno)); |
575 |
|
ErrorAlert(str); |
576 |
|
goto quit; |
577 |
|
} |
578 |
|
lm_area_mapped = true; |
579 |
+ |
#endif |
580 |
|
|
581 |
|
// Create areas for Kernel Data |
582 |
|
kernel_area = shmget(IPC_PRIVATE, KERNEL_AREA_SIZE, 0600); |
595 |
|
ErrorAlert(str); |
596 |
|
goto quit; |
597 |
|
} |
598 |
< |
kernel_data = (KernelData *)0x68ffe000; |
598 |
> |
kernel_data = (KernelData *)KERNEL_DATA_BASE; |
599 |
|
emulator_data = &kernel_data->ed; |
600 |
< |
KernelDataAddr = (uint32)kernel_data; |
600 |
> |
KernelDataAddr = KERNEL_DATA_BASE; |
601 |
|
D(bug("Kernel Data at %p, Emulator Data at %p\n", kernel_data, emulator_data)); |
602 |
|
|
603 |
+ |
// Create area for DR Cache |
604 |
+ |
if (vm_acquire_fixed((void *)DR_EMULATOR_BASE, DR_EMULATOR_SIZE) < 0) { |
605 |
+ |
sprintf(str, GetString(STR_DR_EMULATOR_MMAP_ERR), strerror(errno)); |
606 |
+ |
ErrorAlert(str); |
607 |
+ |
goto quit; |
608 |
+ |
} |
609 |
+ |
dr_emulator_area_mapped = true; |
610 |
+ |
if (vm_acquire_fixed((void *)DR_CACHE_BASE, DR_CACHE_SIZE) < 0) { |
611 |
+ |
sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno)); |
612 |
+ |
ErrorAlert(str); |
613 |
+ |
goto quit; |
614 |
+ |
} |
615 |
+ |
dr_cache_area_mapped = true; |
616 |
+ |
DRCacheAddr = DR_CACHE_BASE; |
617 |
+ |
D(bug("DR Cache at %p\n", DRCacheAddr)); |
618 |
+ |
|
619 |
+ |
// Create area for SheepShaver data |
620 |
+ |
if (!SheepMem::Init()) { |
621 |
+ |
sprintf(str, GetString(STR_SHEEP_MEM_MMAP_ERR), strerror(errno)); |
622 |
+ |
ErrorAlert(str); |
623 |
+ |
goto quit; |
624 |
+ |
} |
625 |
+ |
|
626 |
|
// Create area for Mac ROM |
627 |
< |
if (mmap((char *)ROM_BASE, ROM_AREA_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, zero_fd, 0) == (void *)-1) { |
627 |
> |
if (vm_acquire_fixed((char *)ROM_BASE, ROM_AREA_SIZE) < 0) { |
628 |
> |
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
629 |
> |
ErrorAlert(str); |
630 |
> |
goto quit; |
631 |
> |
} |
632 |
> |
#if !EMULATED_PPC |
633 |
> |
if (vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
634 |
|
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
635 |
|
ErrorAlert(str); |
636 |
|
goto quit; |
637 |
|
} |
638 |
+ |
#endif |
639 |
|
rom_area_mapped = true; |
640 |
|
D(bug("ROM area at %08x\n", ROM_BASE)); |
641 |
|
|
646 |
|
RAMSize = 8*1024*1024; |
647 |
|
} |
648 |
|
|
649 |
< |
mmap_RAMBase = mmap((void *)0x20000000, RAMSize, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, zero_fd, 0); |
650 |
< |
if (mmap_RAMBase == (void *)-1) { |
649 |
> |
if (vm_acquire_fixed((char *)RAM_BASE, RAMSize) < 0) { |
650 |
> |
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
651 |
> |
ErrorAlert(str); |
652 |
> |
goto quit; |
653 |
> |
} |
654 |
> |
#if !EMULATED_PPC |
655 |
> |
if (vm_protect((char *)RAM_BASE, RAMSize, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
656 |
|
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
657 |
|
ErrorAlert(str); |
658 |
|
goto quit; |
659 |
|
} |
660 |
< |
RAMBase = (uint32)mmap_RAMBase; |
660 |
> |
#endif |
661 |
> |
RAMBase = RAM_BASE; |
662 |
|
ram_area_mapped = true; |
663 |
|
D(bug("RAM area at %08x\n", RAMBase)); |
664 |
|
|
683 |
|
rom_tmp = new uint8[ROM_SIZE]; |
684 |
|
actual = read(rom_fd, (void *)rom_tmp, ROM_SIZE); |
685 |
|
close(rom_fd); |
686 |
< |
if (actual == ROM_SIZE) { |
687 |
< |
// Plain ROM image |
688 |
< |
memcpy((void *)ROM_BASE, rom_tmp, ROM_SIZE); |
689 |
< |
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) { |
686 |
> |
|
687 |
> |
// Decode Mac ROM |
688 |
> |
if (!DecodeROM(rom_tmp, actual)) { |
689 |
> |
if (rom_size != 4*1024*1024) { |
690 |
|
ErrorAlert(GetString(STR_ROM_SIZE_ERR)); |
691 |
|
goto quit; |
692 |
|
} else { |
694 |
|
goto quit; |
695 |
|
} |
696 |
|
} |
697 |
+ |
delete[] rom_tmp; |
698 |
|
|
699 |
|
// Load NVRAM |
700 |
|
XPRAMInit(); |
701 |
|
|
702 |
+ |
// Load XPRAM default values if signature not found |
703 |
+ |
if (XPRAM[0x130c] != 0x4e || XPRAM[0x130d] != 0x75 |
704 |
+ |
|| XPRAM[0x130e] != 0x4d || XPRAM[0x130f] != 0x63) { |
705 |
+ |
D(bug("Loading XPRAM default values\n")); |
706 |
+ |
memset(XPRAM + 0x1300, 0, 0x100); |
707 |
+ |
XPRAM[0x130c] = 0x4e; // "NuMc" signature |
708 |
+ |
XPRAM[0x130d] = 0x75; |
709 |
+ |
XPRAM[0x130e] = 0x4d; |
710 |
+ |
XPRAM[0x130f] = 0x63; |
711 |
+ |
XPRAM[0x1301] = 0x80; // InternalWaitFlags = DynWait (don't wait for SCSI devices upon bootup) |
712 |
+ |
XPRAM[0x1310] = 0xa8; // Standard PRAM values |
713 |
+ |
XPRAM[0x1311] = 0x00; |
714 |
+ |
XPRAM[0x1312] = 0x00; |
715 |
+ |
XPRAM[0x1313] = 0x22; |
716 |
+ |
XPRAM[0x1314] = 0xcc; |
717 |
+ |
XPRAM[0x1315] = 0x0a; |
718 |
+ |
XPRAM[0x1316] = 0xcc; |
719 |
+ |
XPRAM[0x1317] = 0x0a; |
720 |
+ |
XPRAM[0x131c] = 0x00; |
721 |
+ |
XPRAM[0x131d] = 0x02; |
722 |
+ |
XPRAM[0x131e] = 0x63; |
723 |
+ |
XPRAM[0x131f] = 0x00; |
724 |
+ |
XPRAM[0x1308] = 0x13; |
725 |
+ |
XPRAM[0x1309] = 0x88; |
726 |
+ |
XPRAM[0x130a] = 0x00; |
727 |
+ |
XPRAM[0x130b] = 0xcc; |
728 |
+ |
XPRAM[0x1376] = 0x00; // OSDefault = MacOS |
729 |
+ |
XPRAM[0x1377] = 0x01; |
730 |
+ |
} |
731 |
+ |
|
732 |
|
// Set boot volume |
733 |
< |
drive = PrefsFindInt32("bootdrive"); |
733 |
> |
i16 = PrefsFindInt32("bootdrive"); |
734 |
|
XPRAM[0x1378] = i16 >> 8; |
735 |
|
XPRAM[0x1379] = i16 & 0xff; |
736 |
< |
driver = PrefsFindInt32("bootdriver"); |
736 |
> |
i16 = PrefsFindInt32("bootdriver"); |
737 |
|
XPRAM[0x137a] = i16 >> 8; |
738 |
|
XPRAM[0x137b] = i16 & 0xff; |
739 |
|
|
746 |
|
boot_globs[1] = htonl(RAMSize); |
747 |
|
boot_globs[2] = htonl((uint32)-1); // End of bank table |
748 |
|
|
749 |
+ |
// Init thunks |
750 |
+ |
if (!ThunksInit()) |
751 |
+ |
goto quit; |
752 |
+ |
|
753 |
|
// Init drivers |
754 |
|
SonyInit(); |
755 |
|
DiskInit(); |
759 |
|
// Init external file system |
760 |
|
ExtFSInit(); |
761 |
|
|
762 |
+ |
// Init ADB |
763 |
+ |
ADBInit(); |
764 |
+ |
|
765 |
|
// Init audio |
766 |
|
AudioInit(); |
767 |
|
|
791 |
|
#if !EMULATED_PPC |
792 |
|
MakeExecutable(0, (void *)ROM_BASE, ROM_AREA_SIZE); |
793 |
|
#endif |
794 |
< |
mprotect((char *)ROM_BASE, ROM_AREA_SIZE, PROT_EXEC | PROT_READ); |
794 |
> |
vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_EXECUTE); |
795 |
|
|
796 |
|
// Initialize Kernel Data |
797 |
|
memset(kernel_data, 0, sizeof(KernelData)); |
798 |
|
if (ROMType == ROMTYPE_NEWWORLD) { |
799 |
< |
static uint32 of_dev_tree[4] = {0, 0, 0, 0}; |
800 |
< |
static uint8 vector_lookup_tbl[128]; |
801 |
< |
static uint8 vector_mask_tbl[64]; |
799 |
> |
uintptr of_dev_tree = SheepMem::Reserve(4 * sizeof(uint32)); |
800 |
> |
memset((void *)of_dev_tree, 0, 4 * sizeof(uint32)); |
801 |
> |
uintptr vector_lookup_tbl = SheepMem::Reserve(128); |
802 |
> |
uintptr vector_mask_tbl = SheepMem::Reserve(64); |
803 |
|
memset((uint8 *)kernel_data + 0xb80, 0x3d, 0x80); |
804 |
< |
memset(vector_lookup_tbl, 0, 128); |
805 |
< |
memset(vector_mask_tbl, 0, 64); |
804 |
> |
memset((void *)vector_lookup_tbl, 0, 128); |
805 |
> |
memset((void *)vector_mask_tbl, 0, 64); |
806 |
|
kernel_data->v[0xb80 >> 2] = htonl(ROM_BASE); |
807 |
< |
kernel_data->v[0xb84 >> 2] = htonl((uint32)of_dev_tree); // OF device tree base |
808 |
< |
kernel_data->v[0xb90 >> 2] = htonl((uint32)vector_lookup_tbl); |
809 |
< |
kernel_data->v[0xb94 >> 2] = htonl((uint32)vector_mask_tbl); |
807 |
> |
kernel_data->v[0xb84 >> 2] = htonl(of_dev_tree); // OF device tree base |
808 |
> |
kernel_data->v[0xb90 >> 2] = htonl(vector_lookup_tbl); |
809 |
> |
kernel_data->v[0xb94 >> 2] = htonl(vector_mask_tbl); |
810 |
|
kernel_data->v[0xb98 >> 2] = htonl(ROM_BASE); // OpenPIC base |
811 |
|
kernel_data->v[0xbb0 >> 2] = htonl(0); // ADB base |
812 |
|
kernel_data->v[0xc20 >> 2] = htonl(RAMSize); |
819 |
|
kernel_data->v[0xc50 >> 2] = htonl(RAMBase); |
820 |
|
kernel_data->v[0xc54 >> 2] = htonl(RAMSize); |
821 |
|
kernel_data->v[0xf60 >> 2] = htonl(PVR); |
822 |
< |
kernel_data->v[0xf64 >> 2] = htonl(CPUClockSpeed); |
823 |
< |
kernel_data->v[0xf68 >> 2] = htonl(BusClockSpeed); |
824 |
< |
kernel_data->v[0xf6c >> 2] = htonl(CPUClockSpeed); |
822 |
> |
kernel_data->v[0xf64 >> 2] = htonl(CPUClockSpeed); // clock-frequency |
823 |
> |
kernel_data->v[0xf68 >> 2] = htonl(BusClockSpeed); // bus-frequency |
824 |
> |
kernel_data->v[0xf6c >> 2] = htonl(BusClockSpeed / 4); // timebase-frequency |
825 |
|
} else { |
826 |
|
kernel_data->v[0xc80 >> 2] = htonl(RAMSize); |
827 |
|
kernel_data->v[0xc84 >> 2] = htonl(RAMSize); |
833 |
|
kernel_data->v[0xcb0 >> 2] = htonl(RAMBase); |
834 |
|
kernel_data->v[0xcb4 >> 2] = htonl(RAMSize); |
835 |
|
kernel_data->v[0xf80 >> 2] = htonl(PVR); |
836 |
< |
kernel_data->v[0xf84 >> 2] = htonl(CPUClockSpeed); |
837 |
< |
kernel_data->v[0xf88 >> 2] = htonl(BusClockSpeed); |
838 |
< |
kernel_data->v[0xf8c >> 2] = htonl(CPUClockSpeed); |
836 |
> |
kernel_data->v[0xf84 >> 2] = htonl(CPUClockSpeed); // clock-frequency |
837 |
> |
kernel_data->v[0xf88 >> 2] = htonl(BusClockSpeed); // bus-frequency |
838 |
> |
kernel_data->v[0xf8c >> 2] = htonl(BusClockSpeed / 4); // timebase-frequency |
839 |
|
} |
840 |
|
|
841 |
|
// Initialize extra low memory |
842 |
|
D(bug("Initializing Low Memory...\n")); |
843 |
|
memset(NULL, 0, 0x3000); |
844 |
|
WriteMacInt32(XLM_SIGNATURE, FOURCC('B','a','a','h')); // Signature to detect SheepShaver |
845 |
< |
WriteMacInt32(XLM_KERNEL_DATA, (uint32)kernel_data); // For trap replacement routines |
845 |
> |
WriteMacInt32(XLM_KERNEL_DATA, KernelDataAddr); // For trap replacement routines |
846 |
|
WriteMacInt32(XLM_PVR, PVR); // Theoretical PVR |
847 |
|
WriteMacInt32(XLM_BUS_CLOCK, BusClockSpeed); // For DriverServicesLib patch |
848 |
|
WriteMacInt16(XLM_EXEC_RETURN_OPCODE, M68K_EXEC_RETURN); // For Execute68k() (RTS from the executed 68k code will jump here and end 68k mode) |
849 |
+ |
WriteMacInt32(XLM_ZERO_PAGE, SheepMem::ZeroPage()); // Pointer to read-only page with all bits set to 0 |
850 |
|
#if !EMULATED_PPC |
851 |
< |
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); |
851 |
> |
WriteMacInt32(XLM_TOC, (uint32)TOC); // TOC pointer of emulator |
852 |
|
#endif |
853 |
+ |
WriteMacInt32(XLM_ETHER_INIT, NativeFunction(NATIVE_ETHER_INIT)); // DLPI ethernet driver functions |
854 |
+ |
WriteMacInt32(XLM_ETHER_TERM, NativeFunction(NATIVE_ETHER_TERM)); |
855 |
+ |
WriteMacInt32(XLM_ETHER_OPEN, NativeFunction(NATIVE_ETHER_OPEN)); |
856 |
+ |
WriteMacInt32(XLM_ETHER_CLOSE, NativeFunction(NATIVE_ETHER_CLOSE)); |
857 |
+ |
WriteMacInt32(XLM_ETHER_WPUT, NativeFunction(NATIVE_ETHER_WPUT)); |
858 |
+ |
WriteMacInt32(XLM_ETHER_RSRV, NativeFunction(NATIVE_ETHER_RSRV)); |
859 |
+ |
WriteMacInt32(XLM_VIDEO_DOIO, NativeFunction(NATIVE_VIDEO_DO_DRIVER_IO)); |
860 |
|
D(bug("Low Memory initialized\n")); |
861 |
|
|
862 |
|
// Start 60Hz thread |
870 |
|
|
871 |
|
#if !EMULATED_PPC |
872 |
|
// Create and install stacks for signal handlers |
873 |
< |
sig_stack = malloc(SIG_STACK_SIZE); |
874 |
< |
D(bug("Signal stack at %p\n", sig_stack)); |
875 |
< |
if (sig_stack == NULL) { |
876 |
< |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
877 |
< |
goto quit; |
878 |
< |
} |
879 |
< |
extra_stack = malloc(SIG_STACK_SIZE); |
880 |
< |
D(bug("Extra stack at %p\n", extra_stack)); |
881 |
< |
if (extra_stack == NULL) { |
882 |
< |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
883 |
< |
goto quit; |
884 |
< |
} |
885 |
< |
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) { |
873 |
> |
for (int i = 0; i < SIG_STACK_COUNT; i++) { |
874 |
> |
void *sig_stack = malloc(SIG_STACK_SIZE); |
875 |
> |
D(bug("Signal stack %d at %p\n", i, sig_stack)); |
876 |
> |
if (sig_stack == NULL) { |
877 |
> |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
878 |
> |
goto quit; |
879 |
> |
} |
880 |
> |
sig_stacks[i].ss_sp = sig_stack; |
881 |
> |
sig_stacks[i].ss_flags = 0; |
882 |
> |
sig_stacks[i].ss_size = SIG_STACK_SIZE; |
883 |
> |
} |
884 |
> |
sig_stack_id = 0; |
885 |
> |
if (sigaltstack(&sig_stacks[0], NULL) < 0) { |
886 |
|
sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno)); |
887 |
|
ErrorAlert(str); |
888 |
|
goto quit; |
890 |
|
#endif |
891 |
|
|
892 |
|
#if !EMULATED_PPC |
893 |
< |
// Install SIGSEGV handler |
893 |
> |
// Install SIGSEGV and SIGBUS handlers |
894 |
|
sigemptyset(&sigsegv_action.sa_mask); // Block interrupts during SEGV handling |
895 |
|
sigaddset(&sigsegv_action.sa_mask, SIGUSR2); |
896 |
< |
sigsegv_action.sa_handler = (__sighandler_t)sigsegv_handler; |
897 |
< |
sigsegv_action.sa_flags = SA_ONSTACK; |
896 |
> |
sigsegv_action.sa_sigaction = sigsegv_handler; |
897 |
> |
sigsegv_action.sa_flags = SA_ONSTACK | SA_SIGINFO; |
898 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
899 |
|
sigsegv_action.sa_restorer = NULL; |
900 |
+ |
#endif |
901 |
|
if (sigaction(SIGSEGV, &sigsegv_action, NULL) < 0) { |
902 |
|
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
903 |
|
ErrorAlert(str); |
904 |
|
goto quit; |
905 |
|
} |
906 |
+ |
if (sigaction(SIGBUS, &sigsegv_action, NULL) < 0) { |
907 |
+ |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
908 |
+ |
ErrorAlert(str); |
909 |
+ |
goto quit; |
910 |
+ |
} |
911 |
|
|
912 |
|
// Install SIGILL handler |
913 |
|
sigemptyset(&sigill_action.sa_mask); // Block interrupts during ILL handling |
914 |
|
sigaddset(&sigill_action.sa_mask, SIGUSR2); |
915 |
< |
sigill_action.sa_handler = (__sighandler_t)sigill_handler; |
916 |
< |
sigill_action.sa_flags = SA_ONSTACK; |
915 |
> |
sigill_action.sa_sigaction = sigill_handler; |
916 |
> |
sigill_action.sa_flags = SA_ONSTACK | SA_SIGINFO; |
917 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
918 |
|
sigill_action.sa_restorer = NULL; |
919 |
+ |
#endif |
920 |
|
if (sigaction(SIGILL, &sigill_action, NULL) < 0) { |
921 |
|
sprintf(str, GetString(STR_SIGILL_INSTALL_ERR), strerror(errno)); |
922 |
|
ErrorAlert(str); |
923 |
|
goto quit; |
924 |
|
} |
925 |
+ |
#endif |
926 |
|
|
927 |
+ |
#if !EMULATED_PPC |
928 |
|
// Install interrupt signal handler |
929 |
|
sigemptyset(&sigusr2_action.sa_mask); |
930 |
< |
sigusr2_action.sa_handler = (__sighandler_t)sigusr2_handler; |
931 |
< |
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART; |
930 |
> |
sigusr2_action.sa_sigaction = sigusr2_handler; |
931 |
> |
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART | SA_SIGINFO; |
932 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
933 |
|
sigusr2_action.sa_restorer = NULL; |
934 |
+ |
#endif |
935 |
|
if (sigaction(SIGUSR2, &sigusr2_action, NULL) < 0) { |
936 |
|
sprintf(str, GetString(STR_SIGUSR2_INSTALL_ERR), strerror(errno)); |
937 |
|
ErrorAlert(str); |
956 |
|
|
957 |
|
static void Quit(void) |
958 |
|
{ |
959 |
+ |
#if EMULATED_PPC |
960 |
+ |
// Exit PowerPC emulation |
961 |
+ |
exit_emul_ppc(); |
962 |
+ |
#endif |
963 |
+ |
|
964 |
|
// Stop 60Hz thread |
965 |
|
if (tick_thread_active) { |
966 |
|
pthread_cancel(tick_thread); |
974 |
|
} |
975 |
|
|
976 |
|
#if !EMULATED_PPC |
977 |
< |
// Uninstall SIGSEGV handler |
977 |
> |
// Uninstall SIGSEGV and SIGBUS handlers |
978 |
|
sigemptyset(&sigsegv_action.sa_mask); |
979 |
|
sigsegv_action.sa_handler = SIG_DFL; |
980 |
|
sigsegv_action.sa_flags = 0; |
981 |
|
sigaction(SIGSEGV, &sigsegv_action, NULL); |
982 |
+ |
sigaction(SIGBUS, &sigsegv_action, NULL); |
983 |
|
|
984 |
|
// Uninstall SIGILL handler |
985 |
|
sigemptyset(&sigill_action.sa_mask); |
986 |
|
sigill_action.sa_handler = SIG_DFL; |
987 |
|
sigill_action.sa_flags = 0; |
988 |
|
sigaction(SIGILL, &sigill_action, NULL); |
989 |
+ |
|
990 |
+ |
// Delete stacks for signal handlers |
991 |
+ |
for (int i = 0; i < SIG_STACK_COUNT; i++) { |
992 |
+ |
void *sig_stack = sig_stacks[i].ss_sp; |
993 |
+ |
if (sig_stack) |
994 |
+ |
free(sig_stack); |
995 |
+ |
} |
996 |
|
#endif |
997 |
|
|
998 |
|
// Save NVRAM |
1013 |
|
// Exit audio |
1014 |
|
AudioExit(); |
1015 |
|
|
1016 |
+ |
// Exit ADB |
1017 |
+ |
ADBExit(); |
1018 |
+ |
|
1019 |
|
// Exit video |
1020 |
|
VideoExit(); |
1021 |
|
|
1028 |
|
DiskExit(); |
1029 |
|
SonyExit(); |
1030 |
|
|
1031 |
+ |
// Delete thunks |
1032 |
+ |
ThunksExit(); |
1033 |
+ |
|
1034 |
+ |
// Delete SheepShaver globals |
1035 |
+ |
SheepMem::Exit(); |
1036 |
+ |
|
1037 |
|
// Delete RAM area |
1038 |
|
if (ram_area_mapped) |
1039 |
< |
munmap(mmap_RAMBase, RAMSize); |
1039 |
> |
vm_release((char *)RAM_BASE, RAMSize); |
1040 |
|
|
1041 |
|
// Delete ROM area |
1042 |
|
if (rom_area_mapped) |
1043 |
< |
munmap((char *)ROM_BASE, ROM_AREA_SIZE); |
1043 |
> |
vm_release((char *)ROM_BASE, ROM_AREA_SIZE); |
1044 |
> |
|
1045 |
> |
// Delete DR cache areas |
1046 |
> |
if (dr_emulator_area_mapped) |
1047 |
> |
vm_release((void *)DR_EMULATOR_BASE, DR_EMULATOR_SIZE); |
1048 |
> |
if (dr_cache_area_mapped) |
1049 |
> |
vm_release((void *)DR_CACHE_BASE, DR_CACHE_SIZE); |
1050 |
|
|
1051 |
|
// Delete Kernel Data area |
1052 |
|
if (kernel_area >= 0) { |
1087 |
|
*/ |
1088 |
|
|
1089 |
|
#if EMULATED_PPC |
903 |
– |
extern void emul_ppc(uint32 start); |
904 |
– |
extern void init_emul_ppc(void); |
1090 |
|
void jump_to_rom(uint32 entry) |
1091 |
|
{ |
1092 |
|
init_emul_ppc(); |
1155 |
|
|
1156 |
|
|
1157 |
|
/* |
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 |
– |
/* |
1158 |
|
* Quit emulator (cause return from jump_to_rom) |
1159 |
|
*/ |
1160 |
|
|
1213 |
|
|
1214 |
|
void MakeExecutable(int dummy, void *start, uint32 length) |
1215 |
|
{ |
1216 |
< |
#if !EMULATED_PPC |
1045 |
< |
if (((uint32)start >= ROM_BASE) && ((uint32)start < (ROM_BASE + ROM_SIZE))) |
1216 |
> |
if (((uintptr)start >= ROM_BASE) && ((uintptr)start < (ROM_BASE + ROM_SIZE))) |
1217 |
|
return; |
1218 |
< |
flush_icache_range(start, (void *)((uint32)start + length)); |
1218 |
> |
#if EMULATED_PPC |
1219 |
> |
FlushCodeCache((uintptr)start, (uintptr)start + length); |
1220 |
> |
#else |
1221 |
> |
flush_icache_range(start, (void *)((uintptr)start + length)); |
1222 |
|
#endif |
1223 |
|
} |
1224 |
|
|
1229 |
|
|
1230 |
|
void PatchAfterStartup(void) |
1231 |
|
{ |
1232 |
< |
ExecutePPC(VideoInstallAccel); |
1232 |
> |
ExecuteNative(NATIVE_VIDEO_INSTALL_ACCEL); |
1233 |
|
InstallExtFS(); |
1234 |
|
} |
1235 |
|
|
1274 |
|
if (emul_thread_fatal) { |
1275 |
|
|
1276 |
|
// Yes, dump registers |
1277 |
< |
pt_regs *r = (pt_regs *)&sigsegv_regs; |
1277 |
> |
sigregs *r = &sigsegv_regs; |
1278 |
|
char str[256]; |
1279 |
< |
sprintf(str, "SIGSEGV\n" |
1279 |
> |
if (crash_reason == NULL) |
1280 |
> |
crash_reason = "SIGSEGV"; |
1281 |
> |
sprintf(str, "%s\n" |
1282 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1283 |
|
" xer %08lx cr %08lx \n" |
1284 |
|
" r0 %08lx r1 %08lx r2 %08lx r3 %08lx\n" |
1289 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1290 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1291 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1292 |
+ |
crash_reason, |
1293 |
|
r->nip, r->link, r->ctr, r->msr, |
1294 |
|
r->xer, r->ccr, |
1295 |
|
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1335 |
|
|
1336 |
|
void Set_pthread_attr(pthread_attr_t *attr, int priority) |
1337 |
|
{ |
1338 |
< |
// nothing to do |
1338 |
> |
#ifdef HAVE_PTHREADS |
1339 |
> |
pthread_attr_init(attr); |
1340 |
> |
#if defined(_POSIX_THREAD_PRIORITY_SCHEDULING) |
1341 |
> |
// Some of these only work for superuser |
1342 |
> |
if (geteuid() == 0) { |
1343 |
> |
pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED); |
1344 |
> |
pthread_attr_setschedpolicy(attr, SCHED_FIFO); |
1345 |
> |
struct sched_param fifo_param; |
1346 |
> |
fifo_param.sched_priority = ((sched_get_priority_min(SCHED_FIFO) + |
1347 |
> |
sched_get_priority_max(SCHED_FIFO)) / 2 + |
1348 |
> |
priority); |
1349 |
> |
pthread_attr_setschedparam(attr, &fifo_param); |
1350 |
> |
} |
1351 |
> |
if (pthread_attr_setscope(attr, PTHREAD_SCOPE_SYSTEM) != 0) { |
1352 |
> |
#ifdef PTHREAD_SCOPE_BOUND_NP |
1353 |
> |
// If system scope is not available (eg. we're not running |
1354 |
> |
// with CAP_SCHED_MGT capability on an SGI box), try bound |
1355 |
> |
// scope. It exposes pthread scheduling to the kernel, |
1356 |
> |
// without setting realtime priority. |
1357 |
> |
pthread_attr_setscope(attr, PTHREAD_SCOPE_BOUND_NP); |
1358 |
> |
#endif |
1359 |
> |
} |
1360 |
> |
#endif |
1361 |
> |
#endif |
1362 |
|
} |
1363 |
|
|
1364 |
|
|
1366 |
|
* Mutexes |
1367 |
|
*/ |
1368 |
|
|
1369 |
+ |
#ifdef HAVE_PTHREADS |
1370 |
+ |
|
1371 |
+ |
struct B2_mutex { |
1372 |
+ |
B2_mutex() { |
1373 |
+ |
pthread_mutexattr_t attr; |
1374 |
+ |
pthread_mutexattr_init(&attr); |
1375 |
+ |
// Initialize the mutex for priority inheritance -- |
1376 |
+ |
// required for accurate timing. |
1377 |
+ |
#ifdef HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL |
1378 |
+ |
pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT); |
1379 |
+ |
#endif |
1380 |
+ |
#if defined(HAVE_PTHREAD_MUTEXATTR_SETTYPE) && defined(PTHREAD_MUTEX_NORMAL) |
1381 |
+ |
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); |
1382 |
+ |
#endif |
1383 |
+ |
#ifdef HAVE_PTHREAD_MUTEXATTR_SETPSHARED |
1384 |
+ |
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE); |
1385 |
+ |
#endif |
1386 |
+ |
pthread_mutex_init(&m, &attr); |
1387 |
+ |
pthread_mutexattr_destroy(&attr); |
1388 |
+ |
} |
1389 |
+ |
~B2_mutex() { |
1390 |
+ |
pthread_mutex_trylock(&m); // Make sure it's locked before |
1391 |
+ |
pthread_mutex_unlock(&m); // unlocking it. |
1392 |
+ |
pthread_mutex_destroy(&m); |
1393 |
+ |
} |
1394 |
+ |
pthread_mutex_t m; |
1395 |
+ |
}; |
1396 |
+ |
|
1397 |
+ |
B2_mutex *B2_create_mutex(void) |
1398 |
+ |
{ |
1399 |
+ |
return new B2_mutex; |
1400 |
+ |
} |
1401 |
+ |
|
1402 |
+ |
void B2_lock_mutex(B2_mutex *mutex) |
1403 |
+ |
{ |
1404 |
+ |
pthread_mutex_lock(&mutex->m); |
1405 |
+ |
} |
1406 |
+ |
|
1407 |
+ |
void B2_unlock_mutex(B2_mutex *mutex) |
1408 |
+ |
{ |
1409 |
+ |
pthread_mutex_unlock(&mutex->m); |
1410 |
+ |
} |
1411 |
+ |
|
1412 |
+ |
void B2_delete_mutex(B2_mutex *mutex) |
1413 |
+ |
{ |
1414 |
+ |
delete mutex; |
1415 |
+ |
} |
1416 |
+ |
|
1417 |
+ |
#else |
1418 |
+ |
|
1419 |
|
struct B2_mutex { |
1420 |
|
int dummy; |
1421 |
|
}; |
1438 |
|
delete mutex; |
1439 |
|
} |
1440 |
|
|
1441 |
+ |
#endif |
1442 |
+ |
|
1443 |
|
|
1444 |
|
/* |
1445 |
|
* Trigger signal USR2 from another thread |
1446 |
|
*/ |
1447 |
|
|
1448 |
+ |
#if !EMULATED_PPC |
1449 |
|
void TriggerInterrupt(void) |
1450 |
|
{ |
1198 |
– |
#if EMULATED_PPC |
1199 |
– |
WriteMacInt32(0x16a, ReadMacInt32(0x16a) + 1); |
1200 |
– |
#else |
1201 |
– |
#if 0 |
1202 |
– |
WriteMacInt32(0x16a, ReadMacInt32(0x16a) + 1); |
1203 |
– |
#else |
1451 |
|
if (ready_for_signals) |
1452 |
|
pthread_kill(emul_thread, SIGUSR2); |
1206 |
– |
#endif |
1207 |
– |
#endif |
1453 |
|
} |
1454 |
+ |
#endif |
1455 |
|
|
1456 |
|
|
1457 |
|
/* |
1491 |
|
} |
1492 |
|
|
1493 |
|
|
1248 |
– |
#if !EMULATED_PPC |
1494 |
|
/* |
1495 |
|
* USR2 handler |
1496 |
|
*/ |
1497 |
|
|
1498 |
< |
static void sigusr2_handler(int sig, sigcontext_struct *sc) |
1498 |
> |
#if !EMULATED_PPC |
1499 |
> |
static void sigusr2_handler(int sig, siginfo_t *sip, void *scp) |
1500 |
|
{ |
1501 |
< |
pt_regs *r = sc->regs; |
1501 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1502 |
|
|
1503 |
|
// Do nothing if interrupts are disabled |
1504 |
|
if (*(int32 *)XLM_IRQ_NEST > 0) |
1512 |
|
case MODE_68K: |
1513 |
|
// 68k emulator active, trigger 68k interrupt level 1 |
1514 |
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1515 |
< |
r->ccr |= ntohl(kernel_data->v[0x674 >> 2]); |
1515 |
> |
r->cr() |= ntohl(kernel_data->v[0x674 >> 2]); |
1516 |
|
break; |
1517 |
|
|
1518 |
|
#if INTERRUPTS_IN_NATIVE_MODE |
1519 |
|
case MODE_NATIVE: |
1520 |
|
// 68k emulator inactive, in nanokernel? |
1521 |
< |
if (r->gpr[1] != KernelDataAddr) { |
1521 |
> |
if (r->gpr(1) != KernelDataAddr) { |
1522 |
> |
|
1523 |
> |
// Set extra stack for nested interrupts |
1524 |
> |
sig_stack_acquire(); |
1525 |
> |
|
1526 |
|
// Prepare for 68k interrupt level 1 |
1527 |
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1528 |
|
WriteMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc, ReadMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc) | ntohl(kernel_data->v[0x674 >> 2])); |
1529 |
|
|
1530 |
|
// Execute nanokernel interrupt routine (this will activate the 68k emulator) |
1531 |
< |
atomic_add((int32 *)XLM_IRQ_NEST, 1); |
1531 |
> |
DisableInterrupt(); |
1532 |
|
if (ROMType == ROMTYPE_NEWWORLD) |
1533 |
|
ppc_interrupt(ROM_BASE + 0x312b1c, KernelDataAddr); |
1534 |
|
else |
1535 |
|
ppc_interrupt(ROM_BASE + 0x312a3c, KernelDataAddr); |
1536 |
+ |
|
1537 |
+ |
// Reset normal signal stack |
1538 |
+ |
sig_stack_release(); |
1539 |
|
} |
1540 |
|
break; |
1541 |
|
#endif |
1546 |
|
if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) { |
1547 |
|
|
1548 |
|
// Set extra stack for SIGSEGV handler |
1549 |
< |
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); |
1549 |
> |
sig_stack_acquire(); |
1550 |
|
#if 1 |
1551 |
|
// Execute full 68k interrupt routine |
1552 |
|
M68kRegisters r; |
1568 |
|
if (InterruptFlags & INTFLAG_VIA) { |
1569 |
|
ClearInterruptFlag(INTFLAG_VIA); |
1570 |
|
ADBInterrupt(); |
1571 |
< |
ExecutePPC(VideoVBL); |
1571 |
> |
ExecuteNative(NATIVE_VIDEO_VBL); |
1572 |
|
} |
1573 |
|
} |
1574 |
|
#endif |
1575 |
|
// Reset normal signal stack |
1576 |
< |
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); |
1576 |
> |
sig_stack_release(); |
1577 |
|
} |
1578 |
|
break; |
1579 |
|
#endif |
1334 |
– |
|
1580 |
|
} |
1581 |
|
} |
1582 |
+ |
#endif |
1583 |
|
|
1584 |
|
|
1585 |
|
/* |
1586 |
|
* SIGSEGV handler |
1587 |
|
*/ |
1588 |
|
|
1589 |
< |
static void sigsegv_handler(int sig, sigcontext_struct *sc) |
1589 |
> |
#if !EMULATED_PPC |
1590 |
> |
static void sigsegv_handler(int sig, siginfo_t *sip, void *scp) |
1591 |
|
{ |
1592 |
< |
pt_regs *r = sc->regs; |
1592 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1593 |
> |
|
1594 |
> |
// Get effective address |
1595 |
> |
uint32 addr = r->dar(); |
1596 |
> |
|
1597 |
> |
#if ENABLE_VOSF |
1598 |
> |
// Handle screen fault. |
1599 |
> |
extern bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction); |
1600 |
> |
if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->pc())) |
1601 |
> |
return; |
1602 |
> |
#endif |
1603 |
> |
|
1604 |
|
num_segv++; |
1605 |
|
|
1606 |
|
// Fault in Mac ROM or RAM? |
1607 |
< |
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
1607 |
> |
bool mac_fault = (r->pc() >= ROM_BASE) && (r->pc() < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize)); |
1608 |
|
if (mac_fault) { |
1609 |
|
|
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 |
– |
|
1610 |
|
// "VM settings" during MacOS 8 installation |
1611 |
< |
if (r->nip == ROM_BASE + 0x488160 && r->gpr[20] == 0xf8000000) { |
1612 |
< |
r->nip += 4; |
1613 |
< |
r->gpr[8] = 0; |
1611 |
> |
if (r->pc() == ROM_BASE + 0x488160 && r->gpr(20) == 0xf8000000) { |
1612 |
> |
r->pc() += 4; |
1613 |
> |
r->gpr(8) = 0; |
1614 |
|
return; |
1615 |
|
|
1616 |
|
// MacOS 8.5 installation |
1617 |
< |
} else if (r->nip == ROM_BASE + 0x488140 && r->gpr[16] == 0xf8000000) { |
1618 |
< |
r->nip += 4; |
1619 |
< |
r->gpr[8] = 0; |
1617 |
> |
} else if (r->pc() == ROM_BASE + 0x488140 && r->gpr(16) == 0xf8000000) { |
1618 |
> |
r->pc() += 4; |
1619 |
> |
r->gpr(8) = 0; |
1620 |
|
return; |
1621 |
|
|
1622 |
|
// MacOS 8 serial drivers on startup |
1623 |
< |
} else if (r->nip == ROM_BASE + 0x48e080 && (r->gpr[8] == 0xf3012002 || r->gpr[8] == 0xf3012000)) { |
1624 |
< |
r->nip += 4; |
1625 |
< |
r->gpr[8] = 0; |
1623 |
> |
} else if (r->pc() == ROM_BASE + 0x48e080 && (r->gpr(8) == 0xf3012002 || r->gpr(8) == 0xf3012000)) { |
1624 |
> |
r->pc() += 4; |
1625 |
> |
r->gpr(8) = 0; |
1626 |
|
return; |
1627 |
|
|
1628 |
|
// MacOS 8.1 serial drivers on startup |
1629 |
< |
} else if (r->nip == ROM_BASE + 0x48c5e0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1630 |
< |
r->nip += 4; |
1629 |
> |
} else if (r->pc() == ROM_BASE + 0x48c5e0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1630 |
> |
r->pc() += 4; |
1631 |
|
return; |
1632 |
< |
} else if (r->nip == ROM_BASE + 0x4a10a0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1633 |
< |
r->nip += 4; |
1632 |
> |
} else if (r->pc() == ROM_BASE + 0x4a10a0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1633 |
> |
r->pc() += 4; |
1634 |
|
return; |
1635 |
|
} |
1636 |
|
|
1637 |
+ |
// Get opcode and divide into fields |
1638 |
+ |
uint32 opcode = *((uint32 *)r->pc()); |
1639 |
+ |
uint32 primop = opcode >> 26; |
1640 |
+ |
uint32 exop = (opcode >> 1) & 0x3ff; |
1641 |
+ |
uint32 ra = (opcode >> 16) & 0x1f; |
1642 |
+ |
uint32 rb = (opcode >> 11) & 0x1f; |
1643 |
+ |
uint32 rd = (opcode >> 21) & 0x1f; |
1644 |
+ |
int32 imm = (int16)(opcode & 0xffff); |
1645 |
+ |
|
1646 |
|
// Analyze opcode |
1647 |
|
enum { |
1648 |
|
TYPE_UNKNOWN, |
1724 |
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_NORM; break; |
1725 |
|
case 45: // sthu |
1726 |
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_U; break; |
1727 |
< |
} |
1728 |
< |
|
1729 |
< |
// Calculate effective address |
1730 |
< |
uint32 addr = 0; |
1731 |
< |
switch (addr_mode) { |
1732 |
< |
case MODE_X: |
1733 |
< |
case MODE_UX: |
1734 |
< |
if (ra == 0) |
1735 |
< |
addr = r->gpr[rb]; |
1736 |
< |
else |
1737 |
< |
addr = r->gpr[ra] + r->gpr[rb]; |
1738 |
< |
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; |
1727 |
> |
#if EMULATE_UNALIGNED_LOADSTORE_MULTIPLE |
1728 |
> |
case 46: // lmw |
1729 |
> |
if ((addr % 4) != 0) { |
1730 |
> |
uint32 ea = addr; |
1731 |
> |
D(bug("WARNING: unaligned lmw to EA=%08x from IP=%08x\n", ea, r->pc())); |
1732 |
> |
for (int i = rd; i <= 31; i++) { |
1733 |
> |
r->gpr(i) = ReadMacInt32(ea); |
1734 |
> |
ea += 4; |
1735 |
> |
} |
1736 |
> |
r->pc() += 4; |
1737 |
> |
goto rti; |
1738 |
> |
} |
1739 |
|
break; |
1740 |
< |
default: |
1740 |
> |
case 47: // stmw |
1741 |
> |
if ((addr % 4) != 0) { |
1742 |
> |
uint32 ea = addr; |
1743 |
> |
D(bug("WARNING: unaligned stmw to EA=%08x from IP=%08x\n", ea, r->pc())); |
1744 |
> |
for (int i = rd; i <= 31; i++) { |
1745 |
> |
WriteMacInt32(ea, r->gpr(i)); |
1746 |
> |
ea += 4; |
1747 |
> |
} |
1748 |
> |
r->pc() += 4; |
1749 |
> |
goto rti; |
1750 |
> |
} |
1751 |
|
break; |
1752 |
+ |
#endif |
1753 |
|
} |
1754 |
< |
|
1755 |
< |
// Ignore ROM writes |
1756 |
< |
if (transfer_type == TYPE_STORE && addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) { |
1757 |
< |
// 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)); |
1754 |
> |
|
1755 |
> |
// Ignore ROM writes (including to the zero page, which is read-only) |
1756 |
> |
if (transfer_type == TYPE_STORE && |
1757 |
> |
((addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) || |
1758 |
> |
(addr >= SheepMem::ZeroPage() && addr < SheepMem::ZeroPage() + SheepMem::PageSize()))) { |
1759 |
> |
// 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())); |
1760 |
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1761 |
< |
r->gpr[ra] = addr; |
1762 |
< |
r->nip += 4; |
1761 |
> |
r->gpr(ra) = addr; |
1762 |
> |
r->pc() += 4; |
1763 |
|
goto rti; |
1764 |
|
} |
1765 |
|
|
1766 |
|
// Ignore illegal memory accesses? |
1767 |
|
if (PrefsFindBool("ignoresegv")) { |
1768 |
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1769 |
< |
r->gpr[ra] = addr; |
1769 |
> |
r->gpr(ra) = addr; |
1770 |
|
if (transfer_type == TYPE_LOAD) |
1771 |
< |
r->gpr[rd] = 0; |
1772 |
< |
r->nip += 4; |
1771 |
> |
r->gpr(rd) = 0; |
1772 |
> |
r->pc() += 4; |
1773 |
|
goto rti; |
1774 |
|
} |
1775 |
|
|
1777 |
|
if (!PrefsFindBool("nogui")) { |
1778 |
|
char str[256]; |
1779 |
|
if (transfer_type == TYPE_LOAD || transfer_type == TYPE_STORE) |
1780 |
< |
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]); |
1780 |
> |
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)); |
1781 |
|
else |
1782 |
< |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode); |
1782 |
> |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode); |
1783 |
|
ErrorAlert(str); |
1784 |
|
QuitEmulator(); |
1785 |
|
return; |
1787 |
|
} |
1788 |
|
|
1789 |
|
// For all other errors, jump into debugger (sort of...) |
1790 |
+ |
crash_reason = (sig == SIGBUS) ? "SIGBUS" : "SIGSEGV"; |
1791 |
|
if (!ready_for_signals) { |
1792 |
< |
printf("SIGSEGV\n"); |
1793 |
< |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
1792 |
> |
printf("%s\n"); |
1793 |
> |
printf(" sigcontext %p, machine_regs %p\n", scp, r); |
1794 |
|
printf( |
1795 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1796 |
|
" xer %08lx cr %08lx \n" |
1802 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1803 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1804 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1805 |
< |
r->nip, r->link, r->ctr, r->msr, |
1806 |
< |
r->xer, r->ccr, |
1807 |
< |
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1808 |
< |
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
1809 |
< |
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
1810 |
< |
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
1811 |
< |
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
1812 |
< |
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
1813 |
< |
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
1814 |
< |
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
1805 |
> |
crash_reason, |
1806 |
> |
r->pc(), r->lr(), r->ctr(), r->msr(), |
1807 |
> |
r->xer(), r->cr(), |
1808 |
> |
r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3), |
1809 |
> |
r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7), |
1810 |
> |
r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11), |
1811 |
> |
r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15), |
1812 |
> |
r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19), |
1813 |
> |
r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23), |
1814 |
> |
r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27), |
1815 |
> |
r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31)); |
1816 |
|
exit(1); |
1817 |
|
QuitEmulator(); |
1818 |
|
return; |
1819 |
|
} else { |
1820 |
|
// We crashed. Save registers, tell tick thread and loop forever |
1821 |
< |
sigsegv_regs = *(sigregs *)r; |
1821 |
> |
build_sigregs(&sigsegv_regs, r); |
1822 |
|
emul_thread_fatal = true; |
1823 |
|
for (;;) ; |
1824 |
|
} |
1830 |
|
* SIGILL handler |
1831 |
|
*/ |
1832 |
|
|
1833 |
< |
static void sigill_handler(int sig, sigcontext_struct *sc) |
1833 |
> |
static void sigill_handler(int sig, siginfo_t *sip, void *scp) |
1834 |
|
{ |
1835 |
< |
pt_regs *r = sc->regs; |
1835 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1836 |
|
char str[256]; |
1837 |
|
|
1838 |
|
// Fault in Mac ROM or RAM? |
1839 |
< |
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
1839 |
> |
bool mac_fault = (r->pc() >= ROM_BASE) && (r->pc() < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize)); |
1840 |
|
if (mac_fault) { |
1841 |
|
|
1842 |
|
// Get opcode and divide into fields |
1843 |
< |
uint32 opcode = *((uint32 *)r->nip); |
1843 |
> |
uint32 opcode = *((uint32 *)r->pc()); |
1844 |
|
uint32 primop = opcode >> 26; |
1845 |
|
uint32 exop = (opcode >> 1) & 0x3ff; |
1846 |
|
uint32 ra = (opcode >> 16) & 0x1f; |
1851 |
|
switch (primop) { |
1852 |
|
case 9: // POWER instructions |
1853 |
|
case 22: |
1854 |
< |
power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->nip, r->gpr[1], opcode); |
1854 |
> |
power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->pc(), r->gpr(1), opcode); |
1855 |
|
ErrorAlert(str); |
1856 |
|
QuitEmulator(); |
1857 |
|
return; |
1859 |
|
case 31: |
1860 |
|
switch (exop) { |
1861 |
|
case 83: // mfmsr |
1862 |
< |
r->gpr[rd] = 0xf072; |
1863 |
< |
r->nip += 4; |
1862 |
> |
r->gpr(rd) = 0xf072; |
1863 |
> |
r->pc() += 4; |
1864 |
|
goto rti; |
1865 |
|
|
1866 |
|
case 210: // mtsr |
1867 |
|
case 242: // mtsrin |
1868 |
|
case 306: // tlbie |
1869 |
< |
r->nip += 4; |
1869 |
> |
r->pc() += 4; |
1870 |
|
goto rti; |
1871 |
|
|
1872 |
|
case 339: { // mfspr |
1882 |
|
case 957: // PMC3 |
1883 |
|
case 958: // PMC4 |
1884 |
|
case 959: // SDA |
1885 |
< |
r->nip += 4; |
1885 |
> |
r->pc() += 4; |
1886 |
|
goto rti; |
1887 |
|
case 25: // SDR1 |
1888 |
< |
r->gpr[rd] = 0xdead001f; |
1889 |
< |
r->nip += 4; |
1888 |
> |
r->gpr(rd) = 0xdead001f; |
1889 |
> |
r->pc() += 4; |
1890 |
|
goto rti; |
1891 |
|
case 287: // PVR |
1892 |
< |
r->gpr[rd] = PVR; |
1893 |
< |
r->nip += 4; |
1892 |
> |
r->gpr(rd) = PVR; |
1893 |
> |
r->pc() += 4; |
1894 |
|
goto rti; |
1895 |
|
} |
1896 |
|
break; |
1926 |
|
case 957: // PMC3 |
1927 |
|
case 958: // PMC4 |
1928 |
|
case 959: // SDA |
1929 |
< |
r->nip += 4; |
1929 |
> |
r->pc() += 4; |
1930 |
|
goto rti; |
1931 |
|
} |
1932 |
|
break; |
1945 |
|
|
1946 |
|
// In GUI mode, show error alert |
1947 |
|
if (!PrefsFindBool("nogui")) { |
1948 |
< |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode); |
1948 |
> |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode); |
1949 |
|
ErrorAlert(str); |
1950 |
|
QuitEmulator(); |
1951 |
|
return; |
1953 |
|
} |
1954 |
|
|
1955 |
|
// For all other errors, jump into debugger (sort of...) |
1956 |
+ |
crash_reason = "SIGILL"; |
1957 |
|
if (!ready_for_signals) { |
1958 |
< |
printf("SIGILL\n"); |
1959 |
< |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
1958 |
> |
printf("%s\n"); |
1959 |
> |
printf(" sigcontext %p, machine_regs %p\n", scp, r); |
1960 |
|
printf( |
1961 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1962 |
|
" xer %08lx cr %08lx \n" |
1968 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1969 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1970 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1971 |
< |
r->nip, r->link, r->ctr, r->msr, |
1972 |
< |
r->xer, r->ccr, |
1973 |
< |
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1974 |
< |
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
1975 |
< |
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
1976 |
< |
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
1977 |
< |
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
1978 |
< |
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
1979 |
< |
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
1980 |
< |
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
1971 |
> |
crash_reason, |
1972 |
> |
r->pc(), r->lr(), r->ctr(), r->msr(), |
1973 |
> |
r->xer(), r->cr(), |
1974 |
> |
r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3), |
1975 |
> |
r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7), |
1976 |
> |
r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11), |
1977 |
> |
r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15), |
1978 |
> |
r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19), |
1979 |
> |
r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23), |
1980 |
> |
r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27), |
1981 |
> |
r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31)); |
1982 |
|
exit(1); |
1983 |
|
QuitEmulator(); |
1984 |
|
return; |
1985 |
|
} else { |
1986 |
|
// We crashed. Save registers, tell tick thread and loop forever |
1987 |
< |
sigsegv_regs = *(sigregs *)r; |
1987 |
> |
build_sigregs(&sigsegv_regs, r); |
1988 |
|
emul_thread_fatal = true; |
1989 |
|
for (;;) ; |
1990 |
|
} |
1994 |
|
|
1995 |
|
|
1996 |
|
/* |
1997 |
+ |
* Helpers to share 32-bit addressable data with MacOS |
1998 |
+ |
*/ |
1999 |
+ |
|
2000 |
+ |
bool SheepMem::Init(void) |
2001 |
+ |
{ |
2002 |
+ |
// Size of a native page |
2003 |
+ |
page_size = getpagesize(); |
2004 |
+ |
|
2005 |
+ |
// Allocate SheepShaver globals |
2006 |
+ |
if (vm_acquire_fixed((char *)base, size) < 0) |
2007 |
+ |
return false; |
2008 |
+ |
|
2009 |
+ |
// Allocate page with all bits set to 0 |
2010 |
+ |
zero_page = base + size; |
2011 |
+ |
if (vm_acquire_fixed((char *)zero_page, page_size) < 0) |
2012 |
+ |
return false; |
2013 |
+ |
memset((char *)zero_page, 0, page_size); |
2014 |
+ |
if (vm_protect((char *)zero_page, page_size, VM_PAGE_READ) < 0) |
2015 |
+ |
return false; |
2016 |
+ |
|
2017 |
+ |
#if EMULATED_PPC |
2018 |
+ |
// Allocate alternate stack for PowerPC interrupt routine |
2019 |
+ |
sig_stack = zero_page + page_size; |
2020 |
+ |
if (vm_acquire_fixed((char *)sig_stack, SIG_STACK_SIZE) < 0) |
2021 |
+ |
return false; |
2022 |
+ |
#endif |
2023 |
+ |
|
2024 |
+ |
top = base + size; |
2025 |
+ |
return true; |
2026 |
+ |
} |
2027 |
+ |
|
2028 |
+ |
void SheepMem::Exit(void) |
2029 |
+ |
{ |
2030 |
+ |
if (top) { |
2031 |
+ |
// Delete SheepShaver globals |
2032 |
+ |
vm_release((void *)base, size); |
2033 |
+ |
|
2034 |
+ |
// Delete zero page |
2035 |
+ |
vm_release((void *)zero_page, page_size); |
2036 |
+ |
|
2037 |
+ |
#if EMULATED_PPC |
2038 |
+ |
// Delete alternate stack for PowerPC interrupt routine |
2039 |
+ |
vm_release((void *)sig_stack, SIG_STACK_SIZE); |
2040 |
+ |
#endif |
2041 |
+ |
} |
2042 |
+ |
} |
2043 |
+ |
|
2044 |
+ |
|
2045 |
+ |
/* |
2046 |
|
* Display alert |
2047 |
|
*/ |
2048 |
|
|