1 |
|
/* |
2 |
|
* main_unix.cpp - Emulation core, Unix implementation |
3 |
|
* |
4 |
< |
* SheepShaver (C) 1997-2002 Christian Bauer and Marc Hellwig |
4 |
> |
* SheepShaver (C) 1997-2005 Christian Bauer and Marc Hellwig |
5 |
|
* |
6 |
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
* it under the terms of the GNU General Public License as published by |
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 |
|
*/ |
105 |
|
#include "xpram.h" |
106 |
|
#include "timer.h" |
107 |
|
#include "adb.h" |
96 |
– |
#include "sony.h" |
97 |
– |
#include "disk.h" |
98 |
– |
#include "cdrom.h" |
99 |
– |
#include "scsi.h" |
108 |
|
#include "video.h" |
101 |
– |
#include "audio.h" |
102 |
– |
#include "ether.h" |
103 |
– |
#include "serial.h" |
104 |
– |
#include "clip.h" |
105 |
– |
#include "extfs.h" |
109 |
|
#include "sys.h" |
110 |
|
#include "macos_util.h" |
111 |
|
#include "rom_patches.h" |
117 |
|
#include "debug.h" |
118 |
|
|
119 |
|
|
120 |
+ |
#ifdef HAVE_DIRENT_H |
121 |
+ |
#include <dirent.h> |
122 |
+ |
#endif |
123 |
+ |
|
124 |
+ |
#ifdef USE_SDL |
125 |
+ |
#include <SDL.h> |
126 |
+ |
#endif |
127 |
+ |
|
128 |
+ |
#ifndef USE_SDL_VIDEO |
129 |
|
#include <X11/Xlib.h> |
130 |
+ |
#endif |
131 |
|
|
132 |
|
#ifdef ENABLE_GTK |
133 |
|
#include <gtk/gtk.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 RAM_BASE = 0x20000000; // Base address of RAM |
167 |
> |
#if REAL_ADDRESSING |
168 |
> |
const uintptr RAM_BASE = 0x20000000; // Base address of RAM |
169 |
> |
#else |
170 |
> |
// FIXME: needs to be >= 0x04000000 |
171 |
> |
const uintptr RAM_BASE = 0x10000000; // Base address of RAM |
172 |
> |
#endif |
173 |
|
const uint32 SIG_STACK_SIZE = 0x10000; // Size of signal stack |
174 |
|
|
175 |
|
|
176 |
|
#if !EMULATED_PPC |
153 |
– |
// Structure in which registers are saved in a signal handler; |
154 |
– |
// sigcontext->regs points to it |
155 |
– |
// (see arch/ppc/kernel/signal.c) |
156 |
– |
typedef struct { |
157 |
– |
uint32 u[4]; |
158 |
– |
} __attribute((aligned(16))) vector128; |
159 |
– |
#include <linux/elf.h> |
160 |
– |
|
177 |
|
struct sigregs { |
178 |
< |
elf_gregset_t gp_regs; // Identical to pt_regs |
179 |
< |
double fp_regs[ELF_NFPREG]; // f0..f31 and fpsrc |
180 |
< |
//more (uninteresting) stuff following here |
178 |
> |
uint32 nip; |
179 |
> |
uint32 link; |
180 |
> |
uint32 ctr; |
181 |
> |
uint32 msr; |
182 |
> |
uint32 xer; |
183 |
> |
uint32 ccr; |
184 |
> |
uint32 gpr[32]; |
185 |
> |
}; |
186 |
> |
|
187 |
> |
#if defined(__linux__) |
188 |
> |
#include <sys/ucontext.h> |
189 |
> |
#define MACHINE_REGISTERS(scp) ((machine_regs *)(((ucontext_t *)scp)->uc_mcontext.regs)) |
190 |
> |
|
191 |
> |
struct machine_regs : public pt_regs |
192 |
> |
{ |
193 |
> |
u_long & cr() { return pt_regs::ccr; } |
194 |
> |
uint32 cr() const { return pt_regs::ccr; } |
195 |
> |
uint32 lr() const { return pt_regs::link; } |
196 |
> |
uint32 ctr() const { return pt_regs::ctr; } |
197 |
> |
uint32 xer() const { return pt_regs::xer; } |
198 |
> |
uint32 msr() const { return pt_regs::msr; } |
199 |
> |
uint32 dar() const { return pt_regs::dar; } |
200 |
> |
u_long & pc() { return pt_regs::nip; } |
201 |
> |
uint32 pc() const { return pt_regs::nip; } |
202 |
> |
u_long & gpr(int i) { return pt_regs::gpr[i]; } |
203 |
> |
uint32 gpr(int i) const { return pt_regs::gpr[i]; } |
204 |
|
}; |
205 |
|
#endif |
206 |
|
|
207 |
+ |
#if defined(__APPLE__) && defined(__MACH__) |
208 |
+ |
#include <sys/signal.h> |
209 |
+ |
extern "C" int sigaltstack(const struct sigaltstack *ss, struct sigaltstack *oss); |
210 |
+ |
|
211 |
+ |
#include <sys/ucontext.h> |
212 |
+ |
#define MACHINE_REGISTERS(scp) ((machine_regs *)(((ucontext_t *)scp)->uc_mcontext)) |
213 |
+ |
|
214 |
+ |
struct machine_regs : public mcontext |
215 |
+ |
{ |
216 |
+ |
uint32 & cr() { return ss.cr; } |
217 |
+ |
uint32 cr() const { return ss.cr; } |
218 |
+ |
uint32 lr() const { return ss.lr; } |
219 |
+ |
uint32 ctr() const { return ss.ctr; } |
220 |
+ |
uint32 xer() const { return ss.xer; } |
221 |
+ |
uint32 msr() const { return ss.srr1; } |
222 |
+ |
uint32 dar() const { return es.dar; } |
223 |
+ |
uint32 & pc() { return ss.srr0; } |
224 |
+ |
uint32 pc() const { return ss.srr0; } |
225 |
+ |
uint32 & gpr(int i) { return (&ss.r0)[i]; } |
226 |
+ |
uint32 gpr(int i) const { return (&ss.r0)[i]; } |
227 |
+ |
}; |
228 |
+ |
#endif |
229 |
+ |
|
230 |
+ |
static void build_sigregs(sigregs *srp, machine_regs *mrp) |
231 |
+ |
{ |
232 |
+ |
srp->nip = mrp->pc(); |
233 |
+ |
srp->link = mrp->lr(); |
234 |
+ |
srp->ctr = mrp->ctr(); |
235 |
+ |
srp->msr = mrp->msr(); |
236 |
+ |
srp->xer = mrp->xer(); |
237 |
+ |
srp->ccr = mrp->cr(); |
238 |
+ |
for (int i = 0; i < 32; i++) |
239 |
+ |
srp->gpr[i] = mrp->gpr(i); |
240 |
+ |
} |
241 |
+ |
|
242 |
+ |
static struct sigaltstack sig_stacks[SIG_STACK_COUNT]; // Stacks for signal handlers |
243 |
+ |
static int sig_stack_id = 0; // Stack slot currently used |
244 |
+ |
|
245 |
+ |
static inline void sig_stack_acquire(void) |
246 |
+ |
{ |
247 |
+ |
if (++sig_stack_id == SIG_STACK_COUNT) { |
248 |
+ |
printf("FATAL: signal stack overflow\n"); |
249 |
+ |
return; |
250 |
+ |
} |
251 |
+ |
sigaltstack(&sig_stacks[sig_stack_id], NULL); |
252 |
+ |
} |
253 |
+ |
|
254 |
+ |
static inline void sig_stack_release(void) |
255 |
+ |
{ |
256 |
+ |
if (--sig_stack_id < 0) { |
257 |
+ |
printf("FATAL: signal stack underflow\n"); |
258 |
+ |
return; |
259 |
+ |
} |
260 |
+ |
sigaltstack(&sig_stacks[sig_stack_id], NULL); |
261 |
+ |
} |
262 |
+ |
#endif |
263 |
+ |
|
264 |
|
|
265 |
|
// Global variables (exported) |
266 |
|
#if !EMULATED_PPC |
268 |
|
#endif |
269 |
|
uint32 RAMBase; // Base address of Mac RAM |
270 |
|
uint32 RAMSize; // Size of Mac RAM |
175 |
– |
uint32 SheepStack1Base; // SheepShaver first alternate stack base |
176 |
– |
uint32 SheepStack2Base; // SheepShaver second alternate stack base |
177 |
– |
uint32 SheepThunksBase; // SheepShaver thunks base |
271 |
|
uint32 KernelDataAddr; // Address of Kernel Data |
272 |
|
uint32 BootGlobsAddr; // Address of BootGlobs structure at top of Mac RAM |
273 |
+ |
uint32 DRCacheAddr; // Address of DR Cache |
274 |
|
uint32 PVR; // Theoretical PVR |
275 |
|
int64 CPUClockSpeed; // Processor clock speed (Hz) |
276 |
|
int64 BusClockSpeed; // Bus clock speed (Hz) |
277 |
+ |
int64 TimebaseSpeed; // Timebase clock speed (Hz) |
278 |
+ |
uint8 *RAMBaseHost; // Base address of Mac RAM (host address space) |
279 |
+ |
uint8 *ROMBaseHost; // Base address of Mac ROM (host address space) |
280 |
|
|
281 |
|
|
282 |
|
// Global variables |
283 |
< |
static char *x_display_name = NULL; // X11 display name |
283 |
> |
#ifndef USE_SDL_VIDEO |
284 |
> |
char *x_display_name = NULL; // X11 display name |
285 |
|
Display *x_display = NULL; // X11 display handle |
286 |
+ |
#ifdef X11_LOCK_TYPE |
287 |
+ |
X11_LOCK_TYPE x_display_lock = X11_LOCK_INIT; // X11 display lock |
288 |
+ |
#endif |
289 |
+ |
#endif |
290 |
|
|
291 |
|
static int zero_fd = 0; // FD of /dev/zero |
190 |
– |
static bool sheep_area_mapped = false; // Flag: SheepShaver data area mmap()ed |
292 |
|
static bool lm_area_mapped = false; // Flag: Low Memory area mmap()ped |
293 |
|
static int kernel_area = -1; // SHM ID of Kernel Data area |
294 |
|
static bool rom_area_mapped = false; // Flag: Mac ROM mmap()ped |
295 |
|
static bool ram_area_mapped = false; // Flag: Mac RAM mmap()ped |
296 |
+ |
static bool dr_cache_area_mapped = false; // Flag: Mac DR Cache mmap()ped |
297 |
+ |
static bool dr_emulator_area_mapped = false;// Flag: Mac DR Emulator mmap()ped |
298 |
|
static KernelData *kernel_data; // Pointer to Kernel Data |
299 |
|
static EmulatorData *emulator_data; |
300 |
|
|
301 |
|
static uint8 last_xpram[XPRAM_SIZE]; // Buffer for monitoring XPRAM changes |
302 |
|
|
303 |
|
static bool nvram_thread_active = false; // Flag: NVRAM watchdog installed |
304 |
+ |
static volatile bool nvram_thread_cancel; // Flag: Cancel NVRAM thread |
305 |
|
static pthread_t nvram_thread; // NVRAM watchdog |
306 |
|
static bool tick_thread_active = false; // Flag: MacOS thread installed |
307 |
+ |
static volatile bool tick_thread_cancel; // Flag: Cancel 60Hz thread |
308 |
|
static pthread_t tick_thread; // 60Hz thread |
309 |
|
static pthread_t emul_thread; // MacOS thread |
310 |
|
|
312 |
|
static int64 num_segv = 0; // Number of handled SEGV signals |
313 |
|
|
314 |
|
static struct sigaction sigusr2_action; // Interrupt signal (of emulator thread) |
315 |
< |
#if !EMULATED_PPC |
315 |
> |
#if EMULATED_PPC |
316 |
> |
static uintptr sig_stack = 0; // Stack for PowerPC interrupt routine |
317 |
> |
#else |
318 |
|
static struct sigaction sigsegv_action; // Data access exception signal (of emulator thread) |
319 |
|
static struct sigaction sigill_action; // Illegal instruction signal (of emulator thread) |
213 |
– |
static void *sig_stack = NULL; // Stack for signal handlers |
214 |
– |
static void *extra_stack = NULL; // Stack for SIGSEGV inside interrupt handler |
320 |
|
static bool emul_thread_fatal = false; // Flag: MacOS thread crashed, tick thread shall dump debug output |
321 |
|
static sigregs sigsegv_regs; // Register dump when crashed |
322 |
+ |
static const char *crash_reason = NULL; // Reason of the crash (SIGSEGV, SIGBUS, SIGILL) |
323 |
|
#endif |
324 |
|
|
325 |
+ |
uint32 SheepMem::page_size; // Size of a native page |
326 |
+ |
uintptr SheepMem::zero_page = 0; // Address of ro page filled in with zeros |
327 |
+ |
uintptr SheepMem::base = 0x60000000; // Address of SheepShaver data |
328 |
+ |
uintptr SheepMem::proc; // Bottom address of SheepShave procedures |
329 |
+ |
uintptr SheepMem::data; // Top of SheepShaver data (stack like storage) |
330 |
+ |
|
331 |
|
|
332 |
|
// Prototypes |
333 |
+ |
static bool kernel_data_init(void); |
334 |
+ |
static void kernel_data_exit(void); |
335 |
|
static void Quit(void); |
336 |
|
static void *emul_func(void *arg); |
337 |
|
static void *nvram_func(void *arg); |
338 |
|
static void *tick_func(void *arg); |
339 |
|
#if EMULATED_PPC |
340 |
< |
static void sigusr2_handler(int sig); |
340 |
> |
extern void emul_ppc(uint32 start); |
341 |
> |
extern void init_emul_ppc(void); |
342 |
> |
extern void exit_emul_ppc(void); |
343 |
> |
sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t); |
344 |
|
#else |
345 |
< |
static void sigusr2_handler(int sig, sigcontext_struct *sc); |
346 |
< |
static void sigsegv_handler(int sig, sigcontext_struct *sc); |
347 |
< |
static void sigill_handler(int sig, sigcontext_struct *sc); |
345 |
> |
static void sigusr2_handler(int sig, siginfo_t *sip, void *scp); |
346 |
> |
static void sigsegv_handler(int sig, siginfo_t *sip, void *scp); |
347 |
> |
static void sigill_handler(int sig, siginfo_t *sip, void *scp); |
348 |
|
#endif |
349 |
|
|
350 |
|
|
351 |
|
// From asm_linux.S |
352 |
< |
#if EMULATED_PPC |
236 |
< |
extern int atomic_add(int *var, int v); |
237 |
< |
extern int atomic_and(int *var, int v); |
238 |
< |
extern int atomic_or(int *var, int v); |
239 |
< |
#else |
352 |
> |
#if !EMULATED_PPC |
353 |
|
extern "C" void *get_toc(void); |
354 |
|
extern "C" void *get_sp(void); |
355 |
< |
extern "C" void flush_icache_range(void *start, void *end); |
355 |
> |
extern "C" void flush_icache_range(uint32 start, uint32 end); |
356 |
|
extern "C" void jump_to_rom(uint32 entry, uint32 context); |
357 |
|
extern "C" void quit_emulator(void); |
358 |
|
extern "C" void execute_68k(uint32 pc, M68kRegisters *r); |
364 |
|
#endif |
365 |
|
|
366 |
|
|
367 |
+ |
#if EMULATED_PPC |
368 |
+ |
/* |
369 |
+ |
* Return signal stack base |
370 |
+ |
*/ |
371 |
+ |
|
372 |
+ |
uintptr SignalStackBase(void) |
373 |
+ |
{ |
374 |
+ |
return sig_stack + SIG_STACK_SIZE; |
375 |
+ |
} |
376 |
+ |
|
377 |
+ |
|
378 |
+ |
/* |
379 |
+ |
* Atomic operations |
380 |
+ |
*/ |
381 |
+ |
|
382 |
+ |
#if HAVE_SPINLOCKS |
383 |
+ |
static spinlock_t atomic_ops_lock = SPIN_LOCK_UNLOCKED; |
384 |
+ |
#else |
385 |
+ |
#define spin_lock(LOCK) |
386 |
+ |
#define spin_unlock(LOCK) |
387 |
+ |
#endif |
388 |
+ |
|
389 |
+ |
int atomic_add(int *var, int v) |
390 |
+ |
{ |
391 |
+ |
spin_lock(&atomic_ops_lock); |
392 |
+ |
int ret = *var; |
393 |
+ |
*var += v; |
394 |
+ |
spin_unlock(&atomic_ops_lock); |
395 |
+ |
return ret; |
396 |
+ |
} |
397 |
+ |
|
398 |
+ |
int atomic_and(int *var, int v) |
399 |
+ |
{ |
400 |
+ |
spin_lock(&atomic_ops_lock); |
401 |
+ |
int ret = *var; |
402 |
+ |
*var &= v; |
403 |
+ |
spin_unlock(&atomic_ops_lock); |
404 |
+ |
return ret; |
405 |
+ |
} |
406 |
+ |
|
407 |
+ |
int atomic_or(int *var, int v) |
408 |
+ |
{ |
409 |
+ |
spin_lock(&atomic_ops_lock); |
410 |
+ |
int ret = *var; |
411 |
+ |
*var |= v; |
412 |
+ |
spin_unlock(&atomic_ops_lock); |
413 |
+ |
return ret; |
414 |
+ |
} |
415 |
+ |
#endif |
416 |
+ |
|
417 |
+ |
|
418 |
+ |
/* |
419 |
+ |
* Memory management helpers |
420 |
+ |
*/ |
421 |
+ |
|
422 |
+ |
static inline int vm_mac_acquire(uint32 addr, uint32 size) |
423 |
+ |
{ |
424 |
+ |
return vm_acquire_fixed(Mac2HostAddr(addr), size); |
425 |
+ |
} |
426 |
+ |
|
427 |
+ |
static inline int vm_mac_release(uint32 addr, uint32 size) |
428 |
+ |
{ |
429 |
+ |
return vm_release(Mac2HostAddr(addr), size); |
430 |
+ |
} |
431 |
+ |
|
432 |
+ |
|
433 |
|
/* |
434 |
|
* Main program |
435 |
|
*/ |
446 |
|
int main(int argc, char **argv) |
447 |
|
{ |
448 |
|
char str[256]; |
270 |
– |
uint32 *boot_globs; |
271 |
– |
int16 i16; |
272 |
– |
int drive, driver; |
449 |
|
int rom_fd; |
450 |
|
FILE *proc_file; |
451 |
|
const char *rom_path; |
479 |
|
for (int i=1; i<argc; i++) { |
480 |
|
if (strcmp(argv[i], "--help") == 0) { |
481 |
|
usage(argv[0]); |
482 |
+ |
#ifndef USE_SDL_VIDEO |
483 |
|
} else if (strcmp(argv[i], "--display") == 0) { |
484 |
|
i++; |
485 |
|
if (i < argc) |
486 |
|
x_display_name = strdup(argv[i]); |
487 |
+ |
#endif |
488 |
|
} else if (argv[i][0] == '-') { |
489 |
|
fprintf(stderr, "Unrecognized option '%s'\n", argv[i]); |
490 |
|
usage(argv[0]); |
491 |
|
} |
492 |
|
} |
493 |
|
|
494 |
+ |
#ifdef USE_SDL |
495 |
+ |
// Initialize SDL system |
496 |
+ |
int sdl_flags = 0; |
497 |
+ |
#ifdef USE_SDL_VIDEO |
498 |
+ |
sdl_flags |= SDL_INIT_VIDEO; |
499 |
+ |
#endif |
500 |
+ |
#ifdef USE_SDL_AUDIO |
501 |
+ |
sdl_flags |= SDL_INIT_AUDIO; |
502 |
+ |
#endif |
503 |
+ |
assert(sdl_flags != 0); |
504 |
+ |
if (SDL_Init(sdl_flags) == -1) { |
505 |
+ |
char str[256]; |
506 |
+ |
sprintf(str, "Could not initialize SDL: %s.\n", SDL_GetError()); |
507 |
+ |
ErrorAlert(str); |
508 |
+ |
goto quit; |
509 |
+ |
} |
510 |
+ |
atexit(SDL_Quit); |
511 |
+ |
#endif |
512 |
+ |
|
513 |
+ |
#ifndef USE_SDL_VIDEO |
514 |
|
// Open display |
515 |
|
x_display = XOpenDisplay(x_display_name); |
516 |
|
if (x_display == NULL) { |
524 |
|
// Fork out, so we can return from fullscreen mode when things get ugly |
525 |
|
XF86DGAForkApp(DefaultScreen(x_display)); |
526 |
|
#endif |
527 |
+ |
#endif |
528 |
|
|
529 |
|
#ifdef ENABLE_MON |
530 |
|
// Initialize mon |
531 |
|
mon_init(); |
532 |
|
#endif |
533 |
|
|
534 |
+ |
#if !EMULATED_PPC |
535 |
+ |
// Create and install stacks for signal handlers |
536 |
+ |
for (int i = 0; i < SIG_STACK_COUNT; i++) { |
537 |
+ |
void *sig_stack = malloc(SIG_STACK_SIZE); |
538 |
+ |
D(bug("Signal stack %d at %p\n", i, sig_stack)); |
539 |
+ |
if (sig_stack == NULL) { |
540 |
+ |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
541 |
+ |
goto quit; |
542 |
+ |
} |
543 |
+ |
sig_stacks[i].ss_sp = sig_stack; |
544 |
+ |
sig_stacks[i].ss_flags = 0; |
545 |
+ |
sig_stacks[i].ss_size = SIG_STACK_SIZE; |
546 |
+ |
} |
547 |
+ |
sig_stack_id = 0; |
548 |
+ |
if (sigaltstack(&sig_stacks[0], NULL) < 0) { |
549 |
+ |
sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno)); |
550 |
+ |
ErrorAlert(str); |
551 |
+ |
goto quit; |
552 |
+ |
} |
553 |
+ |
#endif |
554 |
+ |
|
555 |
+ |
#if !EMULATED_PPC |
556 |
+ |
// Install SIGSEGV and SIGBUS handlers |
557 |
+ |
sigemptyset(&sigsegv_action.sa_mask); // Block interrupts during SEGV handling |
558 |
+ |
sigaddset(&sigsegv_action.sa_mask, SIGUSR2); |
559 |
+ |
sigsegv_action.sa_sigaction = sigsegv_handler; |
560 |
+ |
sigsegv_action.sa_flags = SA_ONSTACK | SA_SIGINFO; |
561 |
+ |
#ifdef HAVE_SIGNAL_SA_RESTORER |
562 |
+ |
sigsegv_action.sa_restorer = NULL; |
563 |
+ |
#endif |
564 |
+ |
if (sigaction(SIGSEGV, &sigsegv_action, NULL) < 0) { |
565 |
+ |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
566 |
+ |
ErrorAlert(str); |
567 |
+ |
goto quit; |
568 |
+ |
} |
569 |
+ |
if (sigaction(SIGBUS, &sigsegv_action, NULL) < 0) { |
570 |
+ |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
571 |
+ |
ErrorAlert(str); |
572 |
+ |
goto quit; |
573 |
+ |
} |
574 |
+ |
#else |
575 |
+ |
// Install SIGSEGV handler for CPU emulator |
576 |
+ |
if (!sigsegv_install_handler(sigsegv_handler)) { |
577 |
+ |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
578 |
+ |
ErrorAlert(str); |
579 |
+ |
goto quit; |
580 |
+ |
} |
581 |
+ |
#endif |
582 |
+ |
|
583 |
+ |
// Initialize VM system |
584 |
+ |
vm_init(); |
585 |
+ |
|
586 |
|
// Get system info |
587 |
|
PVR = 0x00040000; // Default: 604 |
588 |
|
CPUClockSpeed = 100000000; // Default: 100MHz |
589 |
|
BusClockSpeed = 100000000; // Default: 100MHz |
590 |
< |
#if !EMULATED_PPC |
590 |
> |
TimebaseSpeed = 25000000; // Default: 25MHz |
591 |
> |
#if EMULATED_PPC |
592 |
> |
PVR = 0x000c0000; // Default: 7400 (with AltiVec) |
593 |
> |
#elif defined(__APPLE__) && defined(__MACH__) |
594 |
> |
proc_file = popen("ioreg -c IOPlatformDevice", "r"); |
595 |
> |
if (proc_file) { |
596 |
> |
char line[256]; |
597 |
> |
bool powerpc_node = false; |
598 |
> |
while (fgets(line, sizeof(line) - 1, proc_file)) { |
599 |
> |
// Read line |
600 |
> |
int len = strlen(line); |
601 |
> |
if (len == 0) |
602 |
> |
continue; |
603 |
> |
line[len - 1] = 0; |
604 |
> |
|
605 |
> |
// Parse line |
606 |
> |
if (strstr(line, "o PowerPC,")) |
607 |
> |
powerpc_node = true; |
608 |
> |
else if (powerpc_node) { |
609 |
> |
uint32 value; |
610 |
> |
char head[256]; |
611 |
> |
if (sscanf(line, "%[ |]\"cpu-version\" = <%x>", head, &value) == 2) |
612 |
> |
PVR = value; |
613 |
> |
else if (sscanf(line, "%[ |]\"clock-frequency\" = <%x>", head, &value) == 2) |
614 |
> |
CPUClockSpeed = value; |
615 |
> |
else if (sscanf(line, "%[ |]\"bus-frequency\" = <%x>", head, &value) == 2) |
616 |
> |
BusClockSpeed = value; |
617 |
> |
else if (sscanf(line, "%[ |]\"timebase-frequency\" = <%x>", head, &value) == 2) |
618 |
> |
TimebaseSpeed = value; |
619 |
> |
else if (strchr(line, '}')) |
620 |
> |
powerpc_node = false; |
621 |
> |
} |
622 |
> |
} |
623 |
> |
fclose(proc_file); |
624 |
> |
} else { |
625 |
> |
sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno)); |
626 |
> |
WarningAlert(str); |
627 |
> |
} |
628 |
> |
#else |
629 |
|
proc_file = fopen("/proc/cpuinfo", "r"); |
630 |
|
if (proc_file) { |
631 |
+ |
// CPU specs from Linux kernel |
632 |
+ |
// TODO: make it more generic with features (e.g. AltiVec) and |
633 |
+ |
// cache information and friends for NameRegistry |
634 |
+ |
static const struct { |
635 |
+ |
uint32 pvr_mask; |
636 |
+ |
uint32 pvr_value; |
637 |
+ |
const char *cpu_name; |
638 |
+ |
} |
639 |
+ |
cpu_specs[] = { |
640 |
+ |
{ 0xffff0000, 0x00010000, "601" }, |
641 |
+ |
{ 0xffff0000, 0x00030000, "603" }, |
642 |
+ |
{ 0xffff0000, 0x00060000, "603e" }, |
643 |
+ |
{ 0xffff0000, 0x00070000, "603ev" }, |
644 |
+ |
{ 0xffff0000, 0x00040000, "604" }, |
645 |
+ |
{ 0xfffff000, 0x00090000, "604e" }, |
646 |
+ |
{ 0xffff0000, 0x00090000, "604r" }, |
647 |
+ |
{ 0xffff0000, 0x000a0000, "604ev" }, |
648 |
+ |
{ 0xffffffff, 0x00084202, "740/750" }, |
649 |
+ |
{ 0xfffff000, 0x00083000, "745/755" }, |
650 |
+ |
{ 0xfffffff0, 0x00080100, "750CX" }, |
651 |
+ |
{ 0xfffffff0, 0x00082200, "750CX" }, |
652 |
+ |
{ 0xfffffff0, 0x00082210, "750CXe" }, |
653 |
+ |
{ 0xffffff00, 0x70000100, "750FX" }, |
654 |
+ |
{ 0xffffffff, 0x70000200, "750FX" }, |
655 |
+ |
{ 0xffff0000, 0x70000000, "750FX" }, |
656 |
+ |
{ 0xffff0000, 0x70020000, "750GX" }, |
657 |
+ |
{ 0xffff0000, 0x00080000, "740/750" }, |
658 |
+ |
{ 0xffffffff, 0x000c1101, "7400 (1.1)" }, |
659 |
+ |
{ 0xffff0000, 0x000c0000, "7400" }, |
660 |
+ |
{ 0xffff0000, 0x800c0000, "7410" }, |
661 |
+ |
{ 0xffffffff, 0x80000200, "7450" }, |
662 |
+ |
{ 0xffffffff, 0x80000201, "7450" }, |
663 |
+ |
{ 0xffff0000, 0x80000000, "7450" }, |
664 |
+ |
{ 0xffffff00, 0x80010100, "7455" }, |
665 |
+ |
{ 0xffffffff, 0x80010200, "7455" }, |
666 |
+ |
{ 0xffff0000, 0x80010000, "7455" }, |
667 |
+ |
{ 0xffff0000, 0x80020000, "7457" }, |
668 |
+ |
{ 0xffff0000, 0x80030000, "7447A" }, |
669 |
+ |
{ 0x7fff0000, 0x00810000, "82xx" }, |
670 |
+ |
{ 0x7fff0000, 0x00820000, "8280" }, |
671 |
+ |
{ 0xffff0000, 0x00400000, "Power3 (630)" }, |
672 |
+ |
{ 0xffff0000, 0x00410000, "Power3 (630+)" }, |
673 |
+ |
{ 0xffff0000, 0x00360000, "I-star" }, |
674 |
+ |
{ 0xffff0000, 0x00370000, "S-star" }, |
675 |
+ |
{ 0xffff0000, 0x00350000, "Power4" }, |
676 |
+ |
{ 0xffff0000, 0x00390000, "PPC970" }, |
677 |
+ |
{ 0, 0, 0 } |
678 |
+ |
}; |
679 |
+ |
|
680 |
|
char line[256]; |
681 |
|
while(fgets(line, 255, proc_file)) { |
682 |
|
// Read line |
688 |
|
// Parse line |
689 |
|
int i; |
690 |
|
char value[256]; |
691 |
< |
if (sscanf(line, "cpu : %s", value) == 1) { |
692 |
< |
if (strcmp(value, "601") == 0) |
693 |
< |
PVR = 0x00010000; |
694 |
< |
else if (strcmp(value, "603") == 0) |
695 |
< |
PVR = 0x00030000; |
696 |
< |
else if (strcmp(value, "604") == 0) |
697 |
< |
PVR = 0x00040000; |
698 |
< |
else if (strcmp(value, "603e") == 0) |
699 |
< |
PVR = 0x00060000; |
700 |
< |
else if (strcmp(value, "603ev") == 0) |
701 |
< |
PVR = 0x00070000; |
364 |
< |
else if (strcmp(value, "604e") == 0) |
365 |
< |
PVR = 0x00090000; |
366 |
< |
else if (strcmp(value, "604ev5") == 0) |
367 |
< |
PVR = 0x000a0000; |
368 |
< |
else if (strcmp(value, "750") == 0) |
369 |
< |
PVR = 0x00080000; |
370 |
< |
else if (strcmp(value, "821") == 0) |
371 |
< |
PVR = 0x00320000; |
372 |
< |
else if (strcmp(value, "860") == 0) |
373 |
< |
PVR = 0x00500000; |
374 |
< |
else |
691 |
> |
if (sscanf(line, "cpu : %[0-9A-Za-a]", value) == 1) { |
692 |
> |
// Search by name |
693 |
> |
const char *cpu_name = NULL; |
694 |
> |
for (int i = 0; cpu_specs[i].pvr_mask != 0; i++) { |
695 |
> |
if (strcmp(cpu_specs[i].cpu_name, value) == 0) { |
696 |
> |
cpu_name = cpu_specs[i].cpu_name; |
697 |
> |
PVR = cpu_specs[i].pvr_value; |
698 |
> |
break; |
699 |
> |
} |
700 |
> |
} |
701 |
> |
if (cpu_name == NULL) |
702 |
|
printf("WARNING: Unknown CPU type '%s', assuming 604\n", value); |
703 |
+ |
else |
704 |
+ |
printf("Found a PowerPC %s processor\n", cpu_name); |
705 |
|
} |
706 |
|
if (sscanf(line, "clock : %dMHz", &i) == 1) |
707 |
|
CPUClockSpeed = BusClockSpeed = i * 1000000; |
711 |
|
sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno)); |
712 |
|
WarningAlert(str); |
713 |
|
} |
714 |
+ |
|
715 |
+ |
// Get actual bus frequency |
716 |
+ |
proc_file = fopen("/proc/device-tree/clock-frequency", "r"); |
717 |
+ |
if (proc_file) { |
718 |
+ |
union { uint8 b[4]; uint32 l; } value; |
719 |
+ |
if (fread(value.b, sizeof(value), 1, proc_file) == 1) |
720 |
+ |
BusClockSpeed = value.l; |
721 |
+ |
fclose(proc_file); |
722 |
+ |
} |
723 |
+ |
|
724 |
+ |
// Get actual timebase frequency |
725 |
+ |
TimebaseSpeed = BusClockSpeed / 4; |
726 |
+ |
DIR *cpus_dir; |
727 |
+ |
if ((cpus_dir = opendir("/proc/device-tree/cpus")) != NULL) { |
728 |
+ |
struct dirent *cpu_entry; |
729 |
+ |
while ((cpu_entry = readdir(cpus_dir)) != NULL) { |
730 |
+ |
if (strstr(cpu_entry->d_name, "PowerPC,") == cpu_entry->d_name) { |
731 |
+ |
char timebase_freq_node[256]; |
732 |
+ |
sprintf(timebase_freq_node, "/proc/device-tree/cpus/%s/timebase-frequency", cpu_entry->d_name); |
733 |
+ |
proc_file = fopen(timebase_freq_node, "r"); |
734 |
+ |
if (proc_file) { |
735 |
+ |
union { uint8 b[4]; uint32 l; } value; |
736 |
+ |
if (fread(value.b, sizeof(value), 1, proc_file) == 1) |
737 |
+ |
TimebaseSpeed = value.l; |
738 |
+ |
fclose(proc_file); |
739 |
+ |
} |
740 |
+ |
} |
741 |
+ |
} |
742 |
+ |
closedir(cpus_dir); |
743 |
+ |
} |
744 |
|
#endif |
745 |
+ |
// Remap any newer G4/G5 processor to plain G4 for compatibility |
746 |
+ |
switch (PVR >> 16) { |
747 |
+ |
case 0x8000: // 7450 |
748 |
+ |
case 0x8001: // 7455 |
749 |
+ |
case 0x8002: // 7457 |
750 |
+ |
case 0x0039: // 970 |
751 |
+ |
PVR = 0x000c0000; // 7400 |
752 |
+ |
break; |
753 |
+ |
} |
754 |
|
D(bug("PVR: %08x (assumed)\n", PVR)); |
755 |
|
|
756 |
|
// Init system routines |
774 |
|
goto quit; |
775 |
|
} |
776 |
|
|
777 |
+ |
#ifndef PAGEZERO_HACK |
778 |
|
// Create Low Memory area (0x0000..0x3000) |
779 |
< |
if (vm_acquire_fixed((char *)0, 0x3000) < 0) { |
779 |
> |
if (vm_mac_acquire(0, 0x3000) < 0) { |
780 |
|
sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno)); |
781 |
|
ErrorAlert(str); |
782 |
|
goto quit; |
783 |
|
} |
784 |
|
lm_area_mapped = true; |
785 |
+ |
#endif |
786 |
|
|
787 |
|
// Create areas for Kernel Data |
788 |
< |
kernel_area = shmget(IPC_PRIVATE, KERNEL_AREA_SIZE, 0600); |
789 |
< |
if (kernel_area == -1) { |
790 |
< |
sprintf(str, GetString(STR_KD_SHMGET_ERR), strerror(errno)); |
788 |
> |
if (!kernel_data_init()) |
789 |
> |
goto quit; |
790 |
> |
kernel_data = (KernelData *)Mac2HostAddr(KERNEL_DATA_BASE); |
791 |
> |
emulator_data = &kernel_data->ed; |
792 |
> |
KernelDataAddr = KERNEL_DATA_BASE; |
793 |
> |
D(bug("Kernel Data at %p (%08x)\n", kernel_data, KERNEL_DATA_BASE)); |
794 |
> |
D(bug("Emulator Data at %p (%08x)\n", emulator_data, KERNEL_DATA_BASE + offsetof(KernelData, ed))); |
795 |
> |
|
796 |
> |
// Create area for DR Cache |
797 |
> |
if (vm_mac_acquire(DR_EMULATOR_BASE, DR_EMULATOR_SIZE) < 0) { |
798 |
> |
sprintf(str, GetString(STR_DR_EMULATOR_MMAP_ERR), strerror(errno)); |
799 |
|
ErrorAlert(str); |
800 |
|
goto quit; |
801 |
|
} |
802 |
< |
if (shmat(kernel_area, (void *)KERNEL_DATA_BASE, 0) < 0) { |
803 |
< |
sprintf(str, GetString(STR_KD_SHMAT_ERR), strerror(errno)); |
802 |
> |
dr_emulator_area_mapped = true; |
803 |
> |
if (vm_mac_acquire(DR_CACHE_BASE, DR_CACHE_SIZE) < 0) { |
804 |
> |
sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno)); |
805 |
|
ErrorAlert(str); |
806 |
|
goto quit; |
807 |
|
} |
808 |
< |
if (shmat(kernel_area, (void *)KERNEL_DATA2_BASE, 0) < 0) { |
809 |
< |
sprintf(str, GetString(STR_KD2_SHMAT_ERR), strerror(errno)); |
808 |
> |
dr_cache_area_mapped = true; |
809 |
> |
#if !EMULATED_PPC |
810 |
> |
if (vm_protect((char *)DR_CACHE_BASE, DR_CACHE_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
811 |
> |
sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno)); |
812 |
|
ErrorAlert(str); |
813 |
|
goto quit; |
814 |
|
} |
815 |
< |
kernel_data = (KernelData *)0x68ffe000; |
816 |
< |
emulator_data = &kernel_data->ed; |
817 |
< |
KernelDataAddr = (uint32)kernel_data; |
437 |
< |
D(bug("Kernel Data at %p, Emulator Data at %p\n", kernel_data, emulator_data)); |
815 |
> |
#endif |
816 |
> |
DRCacheAddr = DR_CACHE_BASE; |
817 |
> |
D(bug("DR Cache at %p\n", DRCacheAddr)); |
818 |
|
|
819 |
|
// Create area for SheepShaver data |
820 |
< |
if (vm_acquire_fixed((char *)SHEEP_BASE, SHEEP_SIZE) < 0) { |
820 |
> |
if (!SheepMem::Init()) { |
821 |
|
sprintf(str, GetString(STR_SHEEP_MEM_MMAP_ERR), strerror(errno)); |
822 |
|
ErrorAlert(str); |
823 |
|
goto quit; |
824 |
|
} |
445 |
– |
SheepStack1Base = SHEEP_BASE + 0x10000; |
446 |
– |
SheepStack2Base = SheepStack1Base + 0x10000; |
447 |
– |
SheepThunksBase = SheepStack2Base + 0x1000; |
448 |
– |
sheep_area_mapped = true; |
825 |
|
|
826 |
|
// Create area for Mac ROM |
827 |
< |
if (vm_acquire_fixed((char *)ROM_BASE, ROM_AREA_SIZE) < 0) { |
827 |
> |
if (vm_mac_acquire(ROM_BASE, ROM_AREA_SIZE) < 0) { |
828 |
|
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
829 |
|
ErrorAlert(str); |
830 |
|
goto quit; |
831 |
|
} |
832 |
< |
#if !EMULATED_PPC || defined(__powerpc__) |
833 |
< |
if (vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
832 |
> |
ROMBaseHost = Mac2HostAddr(ROM_BASE); |
833 |
> |
#if !EMULATED_PPC |
834 |
> |
if (vm_protect(ROMBaseHost, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
835 |
|
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
836 |
|
ErrorAlert(str); |
837 |
|
goto quit; |
838 |
|
} |
839 |
|
#endif |
840 |
|
rom_area_mapped = true; |
841 |
< |
D(bug("ROM area at %08x\n", ROM_BASE)); |
841 |
> |
D(bug("ROM area at %p (%08x)\n", ROMBaseHost, ROM_BASE)); |
842 |
|
|
843 |
|
// Create area for Mac RAM |
844 |
|
RAMSize = PrefsFindInt32("ramsize"); |
847 |
|
RAMSize = 8*1024*1024; |
848 |
|
} |
849 |
|
|
850 |
< |
if (vm_acquire_fixed((char *)RAM_BASE, RAMSize) < 0) { |
850 |
> |
if (vm_mac_acquire(RAM_BASE, RAMSize) < 0) { |
851 |
|
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
852 |
|
ErrorAlert(str); |
853 |
|
goto quit; |
854 |
|
} |
855 |
+ |
RAMBaseHost = Mac2HostAddr(RAM_BASE); |
856 |
|
#if !EMULATED_PPC |
857 |
< |
if (vm_protect((char *)RAM_BASE, RAMSize, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
857 |
> |
if (vm_protect(RAMBaseHost, RAMSize, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
858 |
|
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
859 |
|
ErrorAlert(str); |
860 |
|
goto quit; |
862 |
|
#endif |
863 |
|
RAMBase = RAM_BASE; |
864 |
|
ram_area_mapped = true; |
865 |
< |
D(bug("RAM area at %08x\n", RAMBase)); |
865 |
> |
D(bug("RAM area at %p (%08x)\n", RAMBaseHost, RAMBase)); |
866 |
|
|
867 |
|
if (RAMBase > ROM_BASE) { |
868 |
|
ErrorAlert(GetString(STR_RAM_HIGHER_THAN_ROM_ERR)); |
898 |
|
} |
899 |
|
delete[] rom_tmp; |
900 |
|
|
901 |
< |
// Load NVRAM |
902 |
< |
XPRAMInit(); |
525 |
< |
|
526 |
< |
// Set boot volume |
527 |
< |
drive = PrefsFindInt32("bootdrive"); |
528 |
< |
XPRAM[0x1378] = i16 >> 8; |
529 |
< |
XPRAM[0x1379] = i16 & 0xff; |
530 |
< |
driver = PrefsFindInt32("bootdriver"); |
531 |
< |
XPRAM[0x137a] = i16 >> 8; |
532 |
< |
XPRAM[0x137b] = i16 & 0xff; |
533 |
< |
|
534 |
< |
// Create BootGlobs at top of Mac memory |
535 |
< |
memset((void *)(RAMBase + RAMSize - 4096), 0, 4096); |
536 |
< |
BootGlobsAddr = RAMBase + RAMSize - 0x1c; |
537 |
< |
boot_globs = (uint32 *)BootGlobsAddr; |
538 |
< |
boot_globs[-5] = htonl(RAMBase + RAMSize); // MemTop |
539 |
< |
boot_globs[0] = htonl(RAMBase); // First RAM bank |
540 |
< |
boot_globs[1] = htonl(RAMSize); |
541 |
< |
boot_globs[2] = htonl((uint32)-1); // End of bank table |
542 |
< |
|
543 |
< |
// Init drivers |
544 |
< |
SonyInit(); |
545 |
< |
DiskInit(); |
546 |
< |
CDROMInit(); |
547 |
< |
SCSIInit(); |
548 |
< |
|
549 |
< |
// Init external file system |
550 |
< |
ExtFSInit(); |
551 |
< |
|
552 |
< |
// Init audio |
553 |
< |
AudioInit(); |
554 |
< |
|
555 |
< |
// Init network |
556 |
< |
EtherInit(); |
557 |
< |
|
558 |
< |
// Init serial ports |
559 |
< |
SerialInit(); |
560 |
< |
|
561 |
< |
// Init Time Manager |
562 |
< |
TimerInit(); |
563 |
< |
|
564 |
< |
// Init clipboard |
565 |
< |
ClipInit(); |
566 |
< |
|
567 |
< |
// Init video |
568 |
< |
if (!VideoInit()) |
569 |
< |
goto quit; |
570 |
< |
|
571 |
< |
// Install ROM patches |
572 |
< |
if (!PatchROM()) { |
573 |
< |
ErrorAlert(GetString(STR_UNSUPPORTED_ROM_TYPE_ERR)); |
901 |
> |
// Initialize everything |
902 |
> |
if (!InitAll()) |
903 |
|
goto quit; |
904 |
< |
} |
904 |
> |
D(bug("Initialization complete\n")); |
905 |
|
|
906 |
|
// Clear caches (as we loaded and patched code) and write protect ROM |
907 |
|
#if !EMULATED_PPC |
908 |
< |
MakeExecutable(0, (void *)ROM_BASE, ROM_AREA_SIZE); |
580 |
< |
#endif |
581 |
< |
vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_EXECUTE); |
582 |
< |
|
583 |
< |
// Initialize Kernel Data |
584 |
< |
memset(kernel_data, 0, sizeof(KernelData)); |
585 |
< |
if (ROMType == ROMTYPE_NEWWORLD) { |
586 |
< |
static uint32 of_dev_tree[4] = {0, 0, 0, 0}; |
587 |
< |
static uint8 vector_lookup_tbl[128]; |
588 |
< |
static uint8 vector_mask_tbl[64]; |
589 |
< |
memset((uint8 *)kernel_data + 0xb80, 0x3d, 0x80); |
590 |
< |
memset(vector_lookup_tbl, 0, 128); |
591 |
< |
memset(vector_mask_tbl, 0, 64); |
592 |
< |
kernel_data->v[0xb80 >> 2] = htonl(ROM_BASE); |
593 |
< |
kernel_data->v[0xb84 >> 2] = htonl((uint32)of_dev_tree); // OF device tree base |
594 |
< |
kernel_data->v[0xb90 >> 2] = htonl((uint32)vector_lookup_tbl); |
595 |
< |
kernel_data->v[0xb94 >> 2] = htonl((uint32)vector_mask_tbl); |
596 |
< |
kernel_data->v[0xb98 >> 2] = htonl(ROM_BASE); // OpenPIC base |
597 |
< |
kernel_data->v[0xbb0 >> 2] = htonl(0); // ADB base |
598 |
< |
kernel_data->v[0xc20 >> 2] = htonl(RAMSize); |
599 |
< |
kernel_data->v[0xc24 >> 2] = htonl(RAMSize); |
600 |
< |
kernel_data->v[0xc30 >> 2] = htonl(RAMSize); |
601 |
< |
kernel_data->v[0xc34 >> 2] = htonl(RAMSize); |
602 |
< |
kernel_data->v[0xc38 >> 2] = htonl(0x00010020); |
603 |
< |
kernel_data->v[0xc3c >> 2] = htonl(0x00200001); |
604 |
< |
kernel_data->v[0xc40 >> 2] = htonl(0x00010000); |
605 |
< |
kernel_data->v[0xc50 >> 2] = htonl(RAMBase); |
606 |
< |
kernel_data->v[0xc54 >> 2] = htonl(RAMSize); |
607 |
< |
kernel_data->v[0xf60 >> 2] = htonl(PVR); |
608 |
< |
kernel_data->v[0xf64 >> 2] = htonl(CPUClockSpeed); |
609 |
< |
kernel_data->v[0xf68 >> 2] = htonl(BusClockSpeed); |
610 |
< |
kernel_data->v[0xf6c >> 2] = htonl(CPUClockSpeed); |
611 |
< |
} else { |
612 |
< |
kernel_data->v[0xc80 >> 2] = htonl(RAMSize); |
613 |
< |
kernel_data->v[0xc84 >> 2] = htonl(RAMSize); |
614 |
< |
kernel_data->v[0xc90 >> 2] = htonl(RAMSize); |
615 |
< |
kernel_data->v[0xc94 >> 2] = htonl(RAMSize); |
616 |
< |
kernel_data->v[0xc98 >> 2] = htonl(0x00010020); |
617 |
< |
kernel_data->v[0xc9c >> 2] = htonl(0x00200001); |
618 |
< |
kernel_data->v[0xca0 >> 2] = htonl(0x00010000); |
619 |
< |
kernel_data->v[0xcb0 >> 2] = htonl(RAMBase); |
620 |
< |
kernel_data->v[0xcb4 >> 2] = htonl(RAMSize); |
621 |
< |
kernel_data->v[0xf80 >> 2] = htonl(PVR); |
622 |
< |
kernel_data->v[0xf84 >> 2] = htonl(CPUClockSpeed); |
623 |
< |
kernel_data->v[0xf88 >> 2] = htonl(BusClockSpeed); |
624 |
< |
kernel_data->v[0xf8c >> 2] = htonl(CPUClockSpeed); |
625 |
< |
} |
626 |
< |
|
627 |
< |
// Initialize extra low memory |
628 |
< |
D(bug("Initializing Low Memory...\n")); |
629 |
< |
memset(NULL, 0, 0x3000); |
630 |
< |
WriteMacInt32(XLM_SIGNATURE, FOURCC('B','a','a','h')); // Signature to detect SheepShaver |
631 |
< |
WriteMacInt32(XLM_KERNEL_DATA, (uint32)kernel_data); // For trap replacement routines |
632 |
< |
WriteMacInt32(XLM_PVR, PVR); // Theoretical PVR |
633 |
< |
WriteMacInt32(XLM_BUS_CLOCK, BusClockSpeed); // For DriverServicesLib patch |
634 |
< |
WriteMacInt16(XLM_EXEC_RETURN_OPCODE, M68K_EXEC_RETURN); // For Execute68k() (RTS from the executed 68k code will jump here and end 68k mode) |
635 |
< |
#if EMULATED_PPC |
636 |
< |
WriteMacInt32(XLM_ETHER_INIT, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_INIT)); |
637 |
< |
WriteMacInt32(XLM_ETHER_TERM, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_TERM)); |
638 |
< |
WriteMacInt32(XLM_ETHER_OPEN, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_OPEN)); |
639 |
< |
WriteMacInt32(XLM_ETHER_CLOSE, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_CLOSE)); |
640 |
< |
WriteMacInt32(XLM_ETHER_WPUT, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_WPUT)); |
641 |
< |
WriteMacInt32(XLM_ETHER_RSRV, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_RSRV)); |
642 |
< |
WriteMacInt32(XLM_VIDEO_DOIO, POWERPC_NATIVE_OP_FUNC(NATIVE_VIDEO_DO_DRIVER_IO)); |
643 |
< |
#else |
644 |
< |
WriteMacInt32(XLM_TOC, (uint32)TOC); // TOC pointer of emulator |
645 |
< |
WriteMacInt32(XLM_ETHER_INIT, (uint32)InitStreamModule); // DLPI ethernet driver functions |
646 |
< |
WriteMacInt32(XLM_ETHER_TERM, (uint32)TerminateStreamModule); |
647 |
< |
WriteMacInt32(XLM_ETHER_OPEN, (uint32)ether_open); |
648 |
< |
WriteMacInt32(XLM_ETHER_CLOSE, (uint32)ether_close); |
649 |
< |
WriteMacInt32(XLM_ETHER_WPUT, (uint32)ether_wput); |
650 |
< |
WriteMacInt32(XLM_ETHER_RSRV, (uint32)ether_rsrv); |
651 |
< |
WriteMacInt32(XLM_VIDEO_DOIO, (uint32)VideoDoDriverIO); |
908 |
> |
flush_icache_range(ROM_BASE, ROM_BASE + ROM_AREA_SIZE); |
909 |
|
#endif |
910 |
< |
D(bug("Low Memory initialized\n")); |
910 |
> |
vm_protect(ROMBaseHost, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_EXECUTE); |
911 |
|
|
912 |
|
// Start 60Hz thread |
913 |
+ |
tick_thread_cancel = false; |
914 |
|
tick_thread_active = (pthread_create(&tick_thread, NULL, tick_func, NULL) == 0); |
915 |
|
D(bug("Tick thread installed (%ld)\n", tick_thread)); |
916 |
|
|
917 |
|
// Start NVRAM watchdog thread |
918 |
|
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
919 |
+ |
nvram_thread_cancel = false; |
920 |
|
nvram_thread_active = (pthread_create(&nvram_thread, NULL, nvram_func, NULL) == 0); |
921 |
|
D(bug("NVRAM thread installed (%ld)\n", nvram_thread)); |
922 |
|
|
923 |
|
#if !EMULATED_PPC |
665 |
– |
// Create and install stacks for signal handlers |
666 |
– |
sig_stack = malloc(SIG_STACK_SIZE); |
667 |
– |
D(bug("Signal stack at %p\n", sig_stack)); |
668 |
– |
if (sig_stack == NULL) { |
669 |
– |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
670 |
– |
goto quit; |
671 |
– |
} |
672 |
– |
extra_stack = malloc(SIG_STACK_SIZE); |
673 |
– |
D(bug("Extra stack at %p\n", extra_stack)); |
674 |
– |
if (extra_stack == NULL) { |
675 |
– |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
676 |
– |
goto quit; |
677 |
– |
} |
678 |
– |
struct sigaltstack new_stack; |
679 |
– |
new_stack.ss_sp = sig_stack; |
680 |
– |
new_stack.ss_flags = 0; |
681 |
– |
new_stack.ss_size = SIG_STACK_SIZE; |
682 |
– |
if (sigaltstack(&new_stack, NULL) < 0) { |
683 |
– |
sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno)); |
684 |
– |
ErrorAlert(str); |
685 |
– |
goto quit; |
686 |
– |
} |
687 |
– |
#endif |
688 |
– |
|
689 |
– |
#if !EMULATED_PPC |
690 |
– |
// Install SIGSEGV handler |
691 |
– |
sigemptyset(&sigsegv_action.sa_mask); // Block interrupts during SEGV handling |
692 |
– |
sigaddset(&sigsegv_action.sa_mask, SIGUSR2); |
693 |
– |
sigsegv_action.sa_handler = (__sighandler_t)sigsegv_handler; |
694 |
– |
sigsegv_action.sa_flags = SA_ONSTACK; |
695 |
– |
sigsegv_action.sa_restorer = NULL; |
696 |
– |
if (sigaction(SIGSEGV, &sigsegv_action, NULL) < 0) { |
697 |
– |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
698 |
– |
ErrorAlert(str); |
699 |
– |
goto quit; |
700 |
– |
} |
701 |
– |
|
924 |
|
// Install SIGILL handler |
925 |
|
sigemptyset(&sigill_action.sa_mask); // Block interrupts during ILL handling |
926 |
|
sigaddset(&sigill_action.sa_mask, SIGUSR2); |
927 |
< |
sigill_action.sa_handler = (__sighandler_t)sigill_handler; |
928 |
< |
sigill_action.sa_flags = SA_ONSTACK; |
927 |
> |
sigill_action.sa_sigaction = sigill_handler; |
928 |
> |
sigill_action.sa_flags = SA_ONSTACK | SA_SIGINFO; |
929 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
930 |
|
sigill_action.sa_restorer = NULL; |
931 |
+ |
#endif |
932 |
|
if (sigaction(SIGILL, &sigill_action, NULL) < 0) { |
933 |
|
sprintf(str, GetString(STR_SIGILL_INSTALL_ERR), strerror(errno)); |
934 |
|
ErrorAlert(str); |
936 |
|
} |
937 |
|
#endif |
938 |
|
|
939 |
+ |
#if !EMULATED_PPC |
940 |
|
// Install interrupt signal handler |
941 |
|
sigemptyset(&sigusr2_action.sa_mask); |
942 |
< |
sigusr2_action.sa_handler = (__sighandler_t)sigusr2_handler; |
943 |
< |
sigusr2_action.sa_flags = 0; |
944 |
< |
#if !EMULATED_PPC |
720 |
< |
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART; |
721 |
< |
#endif |
942 |
> |
sigusr2_action.sa_sigaction = sigusr2_handler; |
943 |
> |
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART | SA_SIGINFO; |
944 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
945 |
|
sigusr2_action.sa_restorer = NULL; |
946 |
+ |
#endif |
947 |
|
if (sigaction(SIGUSR2, &sigusr2_action, NULL) < 0) { |
948 |
|
sprintf(str, GetString(STR_SIGUSR2_INSTALL_ERR), strerror(errno)); |
949 |
|
ErrorAlert(str); |
950 |
|
goto quit; |
951 |
|
} |
952 |
+ |
#endif |
953 |
|
|
954 |
|
// Get my thread ID and execute MacOS thread function |
955 |
|
emul_thread = pthread_self(); |
968 |
|
|
969 |
|
static void Quit(void) |
970 |
|
{ |
971 |
+ |
#if EMULATED_PPC |
972 |
+ |
// Exit PowerPC emulation |
973 |
+ |
exit_emul_ppc(); |
974 |
+ |
#endif |
975 |
+ |
|
976 |
|
// Stop 60Hz thread |
977 |
|
if (tick_thread_active) { |
978 |
+ |
tick_thread_cancel = true; |
979 |
|
pthread_cancel(tick_thread); |
980 |
|
pthread_join(tick_thread, NULL); |
981 |
|
} |
982 |
|
|
983 |
|
// Stop NVRAM watchdog thread |
984 |
|
if (nvram_thread_active) { |
985 |
+ |
nvram_thread_cancel = true; |
986 |
|
pthread_cancel(nvram_thread); |
987 |
|
pthread_join(nvram_thread, NULL); |
988 |
|
} |
989 |
|
|
990 |
|
#if !EMULATED_PPC |
991 |
< |
// Uninstall SIGSEGV handler |
991 |
> |
// Uninstall SIGSEGV and SIGBUS handlers |
992 |
|
sigemptyset(&sigsegv_action.sa_mask); |
993 |
|
sigsegv_action.sa_handler = SIG_DFL; |
994 |
|
sigsegv_action.sa_flags = 0; |
995 |
|
sigaction(SIGSEGV, &sigsegv_action, NULL); |
996 |
+ |
sigaction(SIGBUS, &sigsegv_action, NULL); |
997 |
|
|
998 |
|
// Uninstall SIGILL handler |
999 |
|
sigemptyset(&sigill_action.sa_mask); |
1000 |
|
sigill_action.sa_handler = SIG_DFL; |
1001 |
|
sigill_action.sa_flags = 0; |
1002 |
|
sigaction(SIGILL, &sigill_action, NULL); |
770 |
– |
#endif |
771 |
– |
|
772 |
– |
// Save NVRAM |
773 |
– |
XPRAMExit(); |
774 |
– |
|
775 |
– |
// Exit clipboard |
776 |
– |
ClipExit(); |
777 |
– |
|
778 |
– |
// Exit Time Manager |
779 |
– |
TimerExit(); |
780 |
– |
|
781 |
– |
// Exit serial |
782 |
– |
SerialExit(); |
1003 |
|
|
1004 |
< |
// Exit network |
1005 |
< |
EtherExit(); |
1006 |
< |
|
1007 |
< |
// Exit audio |
1008 |
< |
AudioExit(); |
1009 |
< |
|
1010 |
< |
// Exit video |
791 |
< |
VideoExit(); |
1004 |
> |
// Delete stacks for signal handlers |
1005 |
> |
for (int i = 0; i < SIG_STACK_COUNT; i++) { |
1006 |
> |
void *sig_stack = sig_stacks[i].ss_sp; |
1007 |
> |
if (sig_stack) |
1008 |
> |
free(sig_stack); |
1009 |
> |
} |
1010 |
> |
#endif |
1011 |
|
|
1012 |
< |
// Exit external file system |
1013 |
< |
ExtFSExit(); |
1012 |
> |
// Deinitialize everything |
1013 |
> |
ExitAll(); |
1014 |
|
|
1015 |
< |
// Exit drivers |
1016 |
< |
SCSIExit(); |
798 |
< |
CDROMExit(); |
799 |
< |
DiskExit(); |
800 |
< |
SonyExit(); |
1015 |
> |
// Delete SheepShaver globals |
1016 |
> |
SheepMem::Exit(); |
1017 |
|
|
1018 |
|
// Delete RAM area |
1019 |
|
if (ram_area_mapped) |
1020 |
< |
vm_release((char *)RAM_BASE, RAMSize); |
1020 |
> |
vm_mac_release(RAM_BASE, RAMSize); |
1021 |
|
|
1022 |
|
// Delete ROM area |
1023 |
|
if (rom_area_mapped) |
1024 |
< |
vm_release((char *)ROM_BASE, ROM_AREA_SIZE); |
1024 |
> |
vm_mac_release(ROM_BASE, ROM_AREA_SIZE); |
1025 |
> |
|
1026 |
> |
// Delete DR cache areas |
1027 |
> |
if (dr_emulator_area_mapped) |
1028 |
> |
vm_mac_release(DR_EMULATOR_BASE, DR_EMULATOR_SIZE); |
1029 |
> |
if (dr_cache_area_mapped) |
1030 |
> |
vm_mac_release(DR_CACHE_BASE, DR_CACHE_SIZE); |
1031 |
|
|
1032 |
|
// Delete Kernel Data area |
1033 |
< |
if (kernel_area >= 0) { |
812 |
< |
shmdt((void *)KERNEL_DATA_BASE); |
813 |
< |
shmdt((void *)KERNEL_DATA2_BASE); |
814 |
< |
shmctl(kernel_area, IPC_RMID, NULL); |
815 |
< |
} |
1033 |
> |
kernel_data_exit(); |
1034 |
|
|
1035 |
|
// Delete Low Memory area |
1036 |
|
if (lm_area_mapped) |
1037 |
< |
munmap((char *)0x0000, 0x3000); |
1037 |
> |
vm_mac_release(0, 0x3000); |
1038 |
|
|
1039 |
|
// Close /dev/zero |
1040 |
|
if (zero_fd > 0) |
1052 |
|
#endif |
1053 |
|
|
1054 |
|
// Close X11 server connection |
1055 |
+ |
#ifndef USE_SDL_VIDEO |
1056 |
|
if (x_display) |
1057 |
|
XCloseDisplay(x_display); |
1058 |
+ |
#endif |
1059 |
|
|
1060 |
|
exit(0); |
1061 |
|
} |
1062 |
|
|
1063 |
|
|
1064 |
|
/* |
1065 |
+ |
* Initialize Kernel Data segments |
1066 |
+ |
*/ |
1067 |
+ |
|
1068 |
+ |
#if defined(__CYGWIN__) |
1069 |
+ |
#define WIN32_LEAN_AND_MEAN |
1070 |
+ |
#include <windows.h> |
1071 |
+ |
|
1072 |
+ |
static HANDLE kernel_handle; // Shared memory handle for Kernel Data |
1073 |
+ |
static DWORD allocation_granule; // Minimum size of allocateable are (64K) |
1074 |
+ |
static DWORD kernel_area_size; // Size of Kernel Data area |
1075 |
+ |
#endif |
1076 |
+ |
|
1077 |
+ |
static bool kernel_data_init(void) |
1078 |
+ |
{ |
1079 |
+ |
char str[256]; |
1080 |
+ |
#ifdef _WIN32 |
1081 |
+ |
SYSTEM_INFO si; |
1082 |
+ |
GetSystemInfo(&si); |
1083 |
+ |
allocation_granule = si.dwAllocationGranularity; |
1084 |
+ |
kernel_area_size = (KERNEL_AREA_SIZE + allocation_granule - 1) & -allocation_granule; |
1085 |
+ |
|
1086 |
+ |
char rcs[10]; |
1087 |
+ |
LPVOID kernel_addr; |
1088 |
+ |
kernel_handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, kernel_area_size, NULL); |
1089 |
+ |
if (kernel_handle == NULL) { |
1090 |
+ |
sprintf(rcs, "%d", GetLastError()); |
1091 |
+ |
sprintf(str, GetString(STR_KD_SHMGET_ERR), rcs); |
1092 |
+ |
ErrorAlert(str); |
1093 |
+ |
return false; |
1094 |
+ |
} |
1095 |
+ |
kernel_addr = (LPVOID)Mac2HostAddr(KERNEL_DATA_BASE & -allocation_granule); |
1096 |
+ |
if (MapViewOfFileEx(kernel_handle, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, kernel_area_size, kernel_addr) != kernel_addr) { |
1097 |
+ |
sprintf(rcs, "%d", GetLastError()); |
1098 |
+ |
sprintf(str, GetString(STR_KD_SHMAT_ERR), rcs); |
1099 |
+ |
ErrorAlert(str); |
1100 |
+ |
return false; |
1101 |
+ |
} |
1102 |
+ |
kernel_addr = (LPVOID)Mac2HostAddr(KERNEL_DATA2_BASE & -allocation_granule); |
1103 |
+ |
if (MapViewOfFileEx(kernel_handle, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, kernel_area_size, kernel_addr) != kernel_addr) { |
1104 |
+ |
sprintf(rcs, "%d", GetLastError()); |
1105 |
+ |
sprintf(str, GetString(STR_KD2_SHMAT_ERR), rcs); |
1106 |
+ |
ErrorAlert(str); |
1107 |
+ |
return false; |
1108 |
+ |
} |
1109 |
+ |
#else |
1110 |
+ |
kernel_area = shmget(IPC_PRIVATE, KERNEL_AREA_SIZE, 0600); |
1111 |
+ |
if (kernel_area == -1) { |
1112 |
+ |
sprintf(str, GetString(STR_KD_SHMGET_ERR), strerror(errno)); |
1113 |
+ |
ErrorAlert(str); |
1114 |
+ |
return false; |
1115 |
+ |
} |
1116 |
+ |
if (shmat(kernel_area, Mac2HostAddr(KERNEL_DATA_BASE), 0) < 0) { |
1117 |
+ |
sprintf(str, GetString(STR_KD_SHMAT_ERR), strerror(errno)); |
1118 |
+ |
ErrorAlert(str); |
1119 |
+ |
return false; |
1120 |
+ |
} |
1121 |
+ |
if (shmat(kernel_area, Mac2HostAddr(KERNEL_DATA2_BASE), 0) < 0) { |
1122 |
+ |
sprintf(str, GetString(STR_KD2_SHMAT_ERR), strerror(errno)); |
1123 |
+ |
ErrorAlert(str); |
1124 |
+ |
return false; |
1125 |
+ |
} |
1126 |
+ |
#endif |
1127 |
+ |
return true; |
1128 |
+ |
} |
1129 |
+ |
|
1130 |
+ |
|
1131 |
+ |
/* |
1132 |
+ |
* Deallocate Kernel Data segments |
1133 |
+ |
*/ |
1134 |
+ |
|
1135 |
+ |
static void kernel_data_exit(void) |
1136 |
+ |
{ |
1137 |
+ |
#ifdef _WIN32 |
1138 |
+ |
if (kernel_handle) { |
1139 |
+ |
UnmapViewOfFile(Mac2HostAddr(KERNEL_DATA_BASE & -allocation_granule)); |
1140 |
+ |
UnmapViewOfFile(Mac2HostAddr(KERNEL_DATA2_BASE & -allocation_granule)); |
1141 |
+ |
CloseHandle(kernel_handle); |
1142 |
+ |
} |
1143 |
+ |
#else |
1144 |
+ |
if (kernel_area >= 0) { |
1145 |
+ |
shmdt(Mac2HostAddr(KERNEL_DATA_BASE)); |
1146 |
+ |
shmdt(Mac2HostAddr(KERNEL_DATA2_BASE)); |
1147 |
+ |
shmctl(kernel_area, IPC_RMID, NULL); |
1148 |
+ |
} |
1149 |
+ |
#endif |
1150 |
+ |
} |
1151 |
+ |
|
1152 |
+ |
|
1153 |
+ |
/* |
1154 |
|
* Jump into Mac ROM, start 680x0 emulator |
1155 |
|
*/ |
1156 |
|
|
1157 |
|
#if EMULATED_PPC |
849 |
– |
extern void emul_ppc(uint32 start); |
850 |
– |
extern void init_emul_ppc(void); |
1158 |
|
void jump_to_rom(uint32 entry) |
1159 |
|
{ |
1160 |
|
init_emul_ppc(); |
1219 |
|
uint16 proc[2] = {trap, M68K_RTS}; |
1220 |
|
Execute68k((uint32)proc, r); |
1221 |
|
} |
915 |
– |
|
916 |
– |
|
917 |
– |
/* |
918 |
– |
* Execute PPC code from EMUL_OP routine (real mode switch) |
919 |
– |
*/ |
920 |
– |
|
921 |
– |
void ExecutePPC(void (*func)()) |
922 |
– |
{ |
923 |
– |
uint32 tvect[2] = {(uint32)func, 0}; // Fake TVECT |
924 |
– |
RoutineDescriptor desc = BUILD_PPC_ROUTINE_DESCRIPTOR(0, tvect); |
925 |
– |
M68kRegisters r; |
926 |
– |
Execute68k((uint32)&desc, &r); |
927 |
– |
} |
1222 |
|
#endif |
1223 |
|
|
1224 |
|
|
1279 |
|
* Make code executable |
1280 |
|
*/ |
1281 |
|
|
1282 |
< |
void MakeExecutable(int dummy, void *start, uint32 length) |
1282 |
> |
void MakeExecutable(int dummy, uint32 start, uint32 length) |
1283 |
|
{ |
1284 |
< |
#if !EMULATED_PPC |
991 |
< |
if (((uint32)start >= ROM_BASE) && ((uint32)start < (ROM_BASE + ROM_SIZE))) |
1284 |
> |
if ((start >= ROM_BASE) && (start < (ROM_BASE + ROM_SIZE))) |
1285 |
|
return; |
993 |
– |
flush_icache_range(start, (void *)((uint32)start + length)); |
994 |
– |
#endif |
995 |
– |
} |
996 |
– |
|
997 |
– |
|
998 |
– |
/* |
999 |
– |
* Patch things after system startup (gets called by disk driver accRun routine) |
1000 |
– |
*/ |
1001 |
– |
|
1002 |
– |
void PatchAfterStartup(void) |
1003 |
– |
{ |
1286 |
|
#if EMULATED_PPC |
1287 |
< |
ExecuteNative(NATIVE_VIDEO_INSTALL_ACCEL); |
1287 |
> |
FlushCodeCache(start, start + length); |
1288 |
|
#else |
1289 |
< |
ExecutePPC(VideoInstallAccel); |
1289 |
> |
flush_icache_range(start, start + length); |
1290 |
|
#endif |
1009 |
– |
InstallExtFS(); |
1291 |
|
} |
1292 |
|
|
1293 |
|
|
1295 |
|
* NVRAM watchdog thread (saves NVRAM every minute) |
1296 |
|
*/ |
1297 |
|
|
1298 |
< |
static void *nvram_func(void *arg) |
1298 |
> |
static void nvram_watchdog(void) |
1299 |
|
{ |
1300 |
< |
struct timespec req = {60, 0}; // 1 minute |
1300 |
> |
if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) { |
1301 |
> |
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
1302 |
> |
SaveXPRAM(); |
1303 |
> |
} |
1304 |
> |
} |
1305 |
|
|
1306 |
< |
for (;;) { |
1307 |
< |
pthread_testcancel(); |
1308 |
< |
nanosleep(&req, NULL); |
1309 |
< |
pthread_testcancel(); |
1310 |
< |
if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) { |
1311 |
< |
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
1027 |
< |
SaveXPRAM(); |
1028 |
< |
} |
1306 |
> |
static void *nvram_func(void *arg) |
1307 |
> |
{ |
1308 |
> |
while (!nvram_thread_cancel) { |
1309 |
> |
for (int i=0; i<60 && !nvram_thread_cancel; i++) |
1310 |
> |
Delay_usec(999999); // Only wait 1 second so we quit promptly when nvram_thread_cancel becomes true |
1311 |
> |
nvram_watchdog(); |
1312 |
|
} |
1313 |
|
return NULL; |
1314 |
|
} |
1321 |
|
static void *tick_func(void *arg) |
1322 |
|
{ |
1323 |
|
int tick_counter = 0; |
1324 |
< |
struct timespec req = {0, 16625000}; |
1324 |
> |
uint64 start = GetTicks_usec(); |
1325 |
> |
int64 ticks = 0; |
1326 |
> |
uint64 next = GetTicks_usec(); |
1327 |
|
|
1328 |
< |
for (;;) { |
1328 |
> |
while (!tick_thread_cancel) { |
1329 |
|
|
1330 |
|
// Wait |
1331 |
< |
nanosleep(&req, NULL); |
1331 |
> |
next += 16625; |
1332 |
> |
int64 delay = next - GetTicks_usec(); |
1333 |
> |
if (delay > 0) |
1334 |
> |
Delay_usec(delay); |
1335 |
> |
else if (delay < -16625) |
1336 |
> |
next = GetTicks_usec(); |
1337 |
> |
ticks++; |
1338 |
|
|
1339 |
|
#if !EMULATED_PPC |
1340 |
|
// Did we crash? |
1341 |
|
if (emul_thread_fatal) { |
1342 |
|
|
1343 |
|
// Yes, dump registers |
1344 |
< |
pt_regs *r = (pt_regs *)&sigsegv_regs; |
1344 |
> |
sigregs *r = &sigsegv_regs; |
1345 |
|
char str[256]; |
1346 |
< |
sprintf(str, "SIGSEGV\n" |
1346 |
> |
if (crash_reason == NULL) |
1347 |
> |
crash_reason = "SIGSEGV"; |
1348 |
> |
sprintf(str, "%s\n" |
1349 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1350 |
|
" xer %08lx cr %08lx \n" |
1351 |
|
" r0 %08lx r1 %08lx r2 %08lx r3 %08lx\n" |
1356 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1357 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1358 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1359 |
+ |
crash_reason, |
1360 |
|
r->nip, r->link, r->ctr, r->msr, |
1361 |
|
r->xer, r->ccr, |
1362 |
|
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1392 |
|
TriggerInterrupt(); |
1393 |
|
} |
1394 |
|
} |
1395 |
+ |
|
1396 |
+ |
uint64 end = GetTicks_usec(); |
1397 |
+ |
D(bug("%Ld ticks in %Ld usec = %f ticks/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start))); |
1398 |
|
return NULL; |
1399 |
|
} |
1400 |
|
|
1405 |
|
|
1406 |
|
void Set_pthread_attr(pthread_attr_t *attr, int priority) |
1407 |
|
{ |
1408 |
< |
// nothing to do |
1408 |
> |
#ifdef HAVE_PTHREADS |
1409 |
> |
pthread_attr_init(attr); |
1410 |
> |
#if defined(_POSIX_THREAD_PRIORITY_SCHEDULING) |
1411 |
> |
// Some of these only work for superuser |
1412 |
> |
if (geteuid() == 0) { |
1413 |
> |
pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED); |
1414 |
> |
pthread_attr_setschedpolicy(attr, SCHED_FIFO); |
1415 |
> |
struct sched_param fifo_param; |
1416 |
> |
fifo_param.sched_priority = ((sched_get_priority_min(SCHED_FIFO) + |
1417 |
> |
sched_get_priority_max(SCHED_FIFO)) / 2 + |
1418 |
> |
priority); |
1419 |
> |
pthread_attr_setschedparam(attr, &fifo_param); |
1420 |
> |
} |
1421 |
> |
if (pthread_attr_setscope(attr, PTHREAD_SCOPE_SYSTEM) != 0) { |
1422 |
> |
#ifdef PTHREAD_SCOPE_BOUND_NP |
1423 |
> |
// If system scope is not available (eg. we're not running |
1424 |
> |
// with CAP_SCHED_MGT capability on an SGI box), try bound |
1425 |
> |
// scope. It exposes pthread scheduling to the kernel, |
1426 |
> |
// without setting realtime priority. |
1427 |
> |
pthread_attr_setscope(attr, PTHREAD_SCOPE_BOUND_NP); |
1428 |
> |
#endif |
1429 |
> |
} |
1430 |
> |
#endif |
1431 |
> |
#endif |
1432 |
|
} |
1433 |
|
|
1434 |
|
|
1444 |
|
pthread_mutexattr_init(&attr); |
1445 |
|
// Initialize the mutex for priority inheritance -- |
1446 |
|
// required for accurate timing. |
1447 |
< |
#ifdef HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL |
1447 |
> |
#if defined(HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL) && !defined(__CYGWIN__) |
1448 |
|
pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT); |
1449 |
|
#endif |
1450 |
|
#if defined(HAVE_PTHREAD_MUTEXATTR_SETTYPE) && defined(PTHREAD_MUTEX_NORMAL) |
1515 |
|
* Trigger signal USR2 from another thread |
1516 |
|
*/ |
1517 |
|
|
1518 |
< |
#if !EMULATED_PPC || ASYNC_IRQ |
1518 |
> |
#if !EMULATED_PPC |
1519 |
|
void TriggerInterrupt(void) |
1520 |
|
{ |
1521 |
|
if (ready_for_signals) |
1547 |
|
|
1548 |
|
void DisableInterrupt(void) |
1549 |
|
{ |
1550 |
+ |
#if EMULATED_PPC |
1551 |
+ |
WriteMacInt32(XLM_IRQ_NEST, int32(ReadMacInt32(XLM_IRQ_NEST)) + 1); |
1552 |
+ |
#else |
1553 |
|
atomic_add((int *)XLM_IRQ_NEST, 1); |
1554 |
+ |
#endif |
1555 |
|
} |
1556 |
|
|
1557 |
|
|
1561 |
|
|
1562 |
|
void EnableInterrupt(void) |
1563 |
|
{ |
1564 |
+ |
#if EMULATED_PPC |
1565 |
+ |
WriteMacInt32(XLM_IRQ_NEST, int32(ReadMacInt32(XLM_IRQ_NEST)) - 1); |
1566 |
+ |
#else |
1567 |
|
atomic_add((int *)XLM_IRQ_NEST, -1); |
1568 |
+ |
#endif |
1569 |
|
} |
1570 |
|
|
1571 |
|
|
1573 |
|
* USR2 handler |
1574 |
|
*/ |
1575 |
|
|
1576 |
< |
#if EMULATED_PPC |
1577 |
< |
static void sigusr2_handler(int sig) |
1576 |
> |
#if !EMULATED_PPC |
1577 |
> |
static void sigusr2_handler(int sig, siginfo_t *sip, void *scp) |
1578 |
|
{ |
1579 |
< |
#if ASYNC_IRQ |
1580 |
< |
extern void HandleInterrupt(void); |
1581 |
< |
HandleInterrupt(); |
1579 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1580 |
> |
|
1581 |
> |
#ifdef USE_SDL_VIDEO |
1582 |
> |
// We must fill in the events queue in the same thread that did call SDL_SetVideoMode() |
1583 |
> |
SDL_PumpEvents(); |
1584 |
|
#endif |
1255 |
– |
} |
1256 |
– |
#else |
1257 |
– |
static void sigusr2_handler(int sig, sigcontext_struct *sc) |
1258 |
– |
{ |
1259 |
– |
pt_regs *r = sc->regs; |
1585 |
|
|
1586 |
|
// Do nothing if interrupts are disabled |
1587 |
|
if (*(int32 *)XLM_IRQ_NEST > 0) |
1595 |
|
case MODE_68K: |
1596 |
|
// 68k emulator active, trigger 68k interrupt level 1 |
1597 |
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1598 |
< |
r->ccr |= ntohl(kernel_data->v[0x674 >> 2]); |
1598 |
> |
r->cr() |= ntohl(kernel_data->v[0x674 >> 2]); |
1599 |
|
break; |
1600 |
|
|
1601 |
|
#if INTERRUPTS_IN_NATIVE_MODE |
1602 |
|
case MODE_NATIVE: |
1603 |
|
// 68k emulator inactive, in nanokernel? |
1604 |
< |
if (r->gpr[1] != KernelDataAddr) { |
1604 |
> |
if (r->gpr(1) != KernelDataAddr) { |
1605 |
> |
|
1606 |
> |
// Set extra stack for nested interrupts |
1607 |
> |
sig_stack_acquire(); |
1608 |
> |
|
1609 |
|
// Prepare for 68k interrupt level 1 |
1610 |
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1611 |
|
WriteMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc, ReadMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc) | ntohl(kernel_data->v[0x674 >> 2])); |
1612 |
|
|
1613 |
|
// Execute nanokernel interrupt routine (this will activate the 68k emulator) |
1614 |
< |
atomic_add((int32 *)XLM_IRQ_NEST, 1); |
1614 |
> |
DisableInterrupt(); |
1615 |
|
if (ROMType == ROMTYPE_NEWWORLD) |
1616 |
|
ppc_interrupt(ROM_BASE + 0x312b1c, KernelDataAddr); |
1617 |
|
else |
1618 |
|
ppc_interrupt(ROM_BASE + 0x312a3c, KernelDataAddr); |
1619 |
+ |
|
1620 |
+ |
// Reset normal signal stack |
1621 |
+ |
sig_stack_release(); |
1622 |
|
} |
1623 |
|
break; |
1624 |
|
#endif |
1629 |
|
if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) { |
1630 |
|
|
1631 |
|
// Set extra stack for SIGSEGV handler |
1632 |
< |
struct sigaltstack new_stack; |
1301 |
< |
new_stack.ss_sp = extra_stack; |
1302 |
< |
new_stack.ss_flags = 0; |
1303 |
< |
new_stack.ss_size = SIG_STACK_SIZE; |
1304 |
< |
sigaltstack(&new_stack, NULL); |
1632 |
> |
sig_stack_acquire(); |
1633 |
|
#if 1 |
1634 |
|
// Execute full 68k interrupt routine |
1635 |
|
M68kRegisters r; |
1651 |
|
if (InterruptFlags & INTFLAG_VIA) { |
1652 |
|
ClearInterruptFlag(INTFLAG_VIA); |
1653 |
|
ADBInterrupt(); |
1654 |
< |
ExecutePPC(VideoVBL); |
1654 |
> |
ExecuteNative(NATIVE_VIDEO_VBL); |
1655 |
|
} |
1656 |
|
} |
1657 |
|
#endif |
1658 |
|
// Reset normal signal stack |
1659 |
< |
new_stack.ss_sp = sig_stack; |
1332 |
< |
new_stack.ss_flags = 0; |
1333 |
< |
new_stack.ss_size = SIG_STACK_SIZE; |
1334 |
< |
sigaltstack(&new_stack, NULL); |
1659 |
> |
sig_stack_release(); |
1660 |
|
} |
1661 |
|
break; |
1662 |
|
#endif |
1670 |
|
*/ |
1671 |
|
|
1672 |
|
#if !EMULATED_PPC |
1673 |
< |
static void sigsegv_handler(int sig, sigcontext_struct *sc) |
1673 |
> |
static void sigsegv_handler(int sig, siginfo_t *sip, void *scp) |
1674 |
|
{ |
1675 |
< |
pt_regs *r = sc->regs; |
1675 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1676 |
|
|
1677 |
|
// Get effective address |
1678 |
< |
uint32 addr = r->dar; |
1678 |
> |
uint32 addr = r->dar(); |
1679 |
|
|
1680 |
|
#if ENABLE_VOSF |
1681 |
|
// Handle screen fault. |
1682 |
|
extern bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction); |
1683 |
< |
if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->nip)) |
1683 |
> |
if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->pc())) |
1684 |
|
return; |
1685 |
|
#endif |
1686 |
|
|
1687 |
|
num_segv++; |
1688 |
|
|
1689 |
< |
// Fault in Mac ROM or RAM? |
1690 |
< |
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
1689 |
> |
// Fault in Mac ROM or RAM or DR Cache? |
1690 |
> |
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)); |
1691 |
|
if (mac_fault) { |
1692 |
|
|
1693 |
|
// "VM settings" during MacOS 8 installation |
1694 |
< |
if (r->nip == ROM_BASE + 0x488160 && r->gpr[20] == 0xf8000000) { |
1695 |
< |
r->nip += 4; |
1696 |
< |
r->gpr[8] = 0; |
1694 |
> |
if (r->pc() == ROM_BASE + 0x488160 && r->gpr(20) == 0xf8000000) { |
1695 |
> |
r->pc() += 4; |
1696 |
> |
r->gpr(8) = 0; |
1697 |
|
return; |
1698 |
|
|
1699 |
|
// MacOS 8.5 installation |
1700 |
< |
} else if (r->nip == ROM_BASE + 0x488140 && r->gpr[16] == 0xf8000000) { |
1701 |
< |
r->nip += 4; |
1702 |
< |
r->gpr[8] = 0; |
1700 |
> |
} else if (r->pc() == ROM_BASE + 0x488140 && r->gpr(16) == 0xf8000000) { |
1701 |
> |
r->pc() += 4; |
1702 |
> |
r->gpr(8) = 0; |
1703 |
|
return; |
1704 |
|
|
1705 |
|
// MacOS 8 serial drivers on startup |
1706 |
< |
} else if (r->nip == ROM_BASE + 0x48e080 && (r->gpr[8] == 0xf3012002 || r->gpr[8] == 0xf3012000)) { |
1707 |
< |
r->nip += 4; |
1708 |
< |
r->gpr[8] = 0; |
1706 |
> |
} else if (r->pc() == ROM_BASE + 0x48e080 && (r->gpr(8) == 0xf3012002 || r->gpr(8) == 0xf3012000)) { |
1707 |
> |
r->pc() += 4; |
1708 |
> |
r->gpr(8) = 0; |
1709 |
|
return; |
1710 |
|
|
1711 |
|
// MacOS 8.1 serial drivers on startup |
1712 |
< |
} else if (r->nip == ROM_BASE + 0x48c5e0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1713 |
< |
r->nip += 4; |
1712 |
> |
} else if (r->pc() == ROM_BASE + 0x48c5e0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1713 |
> |
r->pc() += 4; |
1714 |
|
return; |
1715 |
< |
} else if (r->nip == ROM_BASE + 0x4a10a0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1716 |
< |
r->nip += 4; |
1715 |
> |
} else if (r->pc() == ROM_BASE + 0x4a10a0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1716 |
> |
r->pc() += 4; |
1717 |
> |
return; |
1718 |
> |
|
1719 |
> |
// MacOS 8.6 serial drivers on startup (with DR Cache and OldWorld ROM) |
1720 |
> |
} else if ((r->pc() - DR_CACHE_BASE) < DR_CACHE_SIZE && (r->gpr(16) == 0xf3012002 || r->gpr(16) == 0xf3012000)) { |
1721 |
> |
r->pc() += 4; |
1722 |
> |
return; |
1723 |
> |
} else if ((r->pc() - DR_CACHE_BASE) < DR_CACHE_SIZE && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1724 |
> |
r->pc() += 4; |
1725 |
|
return; |
1726 |
|
} |
1727 |
|
|
1728 |
|
// Get opcode and divide into fields |
1729 |
< |
uint32 opcode = *((uint32 *)r->nip); |
1729 |
> |
uint32 opcode = *((uint32 *)r->pc()); |
1730 |
|
uint32 primop = opcode >> 26; |
1731 |
|
uint32 exop = (opcode >> 1) & 0x3ff; |
1732 |
|
uint32 ra = (opcode >> 16) & 0x1f; |
1815 |
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_NORM; break; |
1816 |
|
case 45: // sthu |
1817 |
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_U; break; |
1818 |
+ |
#if EMULATE_UNALIGNED_LOADSTORE_MULTIPLE |
1819 |
+ |
case 46: // lmw |
1820 |
+ |
if ((addr % 4) != 0) { |
1821 |
+ |
uint32 ea = addr; |
1822 |
+ |
D(bug("WARNING: unaligned lmw to EA=%08x from IP=%08x\n", ea, r->pc())); |
1823 |
+ |
for (int i = rd; i <= 31; i++) { |
1824 |
+ |
r->gpr(i) = ReadMacInt32(ea); |
1825 |
+ |
ea += 4; |
1826 |
+ |
} |
1827 |
+ |
r->pc() += 4; |
1828 |
+ |
goto rti; |
1829 |
+ |
} |
1830 |
+ |
break; |
1831 |
+ |
case 47: // stmw |
1832 |
+ |
if ((addr % 4) != 0) { |
1833 |
+ |
uint32 ea = addr; |
1834 |
+ |
D(bug("WARNING: unaligned stmw to EA=%08x from IP=%08x\n", ea, r->pc())); |
1835 |
+ |
for (int i = rd; i <= 31; i++) { |
1836 |
+ |
WriteMacInt32(ea, r->gpr(i)); |
1837 |
+ |
ea += 4; |
1838 |
+ |
} |
1839 |
+ |
r->pc() += 4; |
1840 |
+ |
goto rti; |
1841 |
+ |
} |
1842 |
+ |
break; |
1843 |
+ |
#endif |
1844 |
|
} |
1845 |
|
|
1846 |
< |
// Ignore ROM writes |
1847 |
< |
if (transfer_type == TYPE_STORE && addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) { |
1848 |
< |
// 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)); |
1846 |
> |
// Ignore ROM writes (including to the zero page, which is read-only) |
1847 |
> |
if (transfer_type == TYPE_STORE && |
1848 |
> |
((addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) || |
1849 |
> |
(addr >= SheepMem::ZeroPage() && addr < SheepMem::ZeroPage() + SheepMem::PageSize()))) { |
1850 |
> |
// 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())); |
1851 |
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1852 |
< |
r->gpr[ra] = addr; |
1853 |
< |
r->nip += 4; |
1852 |
> |
r->gpr(ra) = addr; |
1853 |
> |
r->pc() += 4; |
1854 |
|
goto rti; |
1855 |
|
} |
1856 |
|
|
1857 |
|
// Ignore illegal memory accesses? |
1858 |
|
if (PrefsFindBool("ignoresegv")) { |
1859 |
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1860 |
< |
r->gpr[ra] = addr; |
1860 |
> |
r->gpr(ra) = addr; |
1861 |
|
if (transfer_type == TYPE_LOAD) |
1862 |
< |
r->gpr[rd] = 0; |
1863 |
< |
r->nip += 4; |
1862 |
> |
r->gpr(rd) = 0; |
1863 |
> |
r->pc() += 4; |
1864 |
|
goto rti; |
1865 |
|
} |
1866 |
|
|
1868 |
|
if (!PrefsFindBool("nogui")) { |
1869 |
|
char str[256]; |
1870 |
|
if (transfer_type == TYPE_LOAD || transfer_type == TYPE_STORE) |
1871 |
< |
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]); |
1871 |
> |
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)); |
1872 |
|
else |
1873 |
< |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode); |
1873 |
> |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode); |
1874 |
|
ErrorAlert(str); |
1875 |
|
QuitEmulator(); |
1876 |
|
return; |
1878 |
|
} |
1879 |
|
|
1880 |
|
// For all other errors, jump into debugger (sort of...) |
1881 |
+ |
crash_reason = (sig == SIGBUS) ? "SIGBUS" : "SIGSEGV"; |
1882 |
|
if (!ready_for_signals) { |
1883 |
< |
printf("SIGSEGV\n"); |
1884 |
< |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
1883 |
> |
printf("%s\n"); |
1884 |
> |
printf(" sigcontext %p, machine_regs %p\n", scp, r); |
1885 |
|
printf( |
1886 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1887 |
|
" xer %08lx cr %08lx \n" |
1893 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1894 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1895 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1896 |
< |
r->nip, r->link, r->ctr, r->msr, |
1897 |
< |
r->xer, r->ccr, |
1898 |
< |
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1899 |
< |
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
1900 |
< |
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
1901 |
< |
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
1902 |
< |
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
1903 |
< |
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
1904 |
< |
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
1905 |
< |
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
1896 |
> |
crash_reason, |
1897 |
> |
r->pc(), r->lr(), r->ctr(), r->msr(), |
1898 |
> |
r->xer(), r->cr(), |
1899 |
> |
r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3), |
1900 |
> |
r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7), |
1901 |
> |
r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11), |
1902 |
> |
r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15), |
1903 |
> |
r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19), |
1904 |
> |
r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23), |
1905 |
> |
r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27), |
1906 |
> |
r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31)); |
1907 |
|
exit(1); |
1908 |
|
QuitEmulator(); |
1909 |
|
return; |
1910 |
|
} else { |
1911 |
|
// We crashed. Save registers, tell tick thread and loop forever |
1912 |
< |
sigsegv_regs = *(sigregs *)r; |
1912 |
> |
build_sigregs(&sigsegv_regs, r); |
1913 |
|
emul_thread_fatal = true; |
1914 |
|
for (;;) ; |
1915 |
|
} |
1921 |
|
* SIGILL handler |
1922 |
|
*/ |
1923 |
|
|
1924 |
< |
static void sigill_handler(int sig, sigcontext_struct *sc) |
1924 |
> |
static void sigill_handler(int sig, siginfo_t *sip, void *scp) |
1925 |
|
{ |
1926 |
< |
pt_regs *r = sc->regs; |
1926 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1927 |
|
char str[256]; |
1928 |
|
|
1929 |
|
// Fault in Mac ROM or RAM? |
1930 |
< |
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
1930 |
> |
bool mac_fault = (r->pc() >= ROM_BASE) && (r->pc() < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize)); |
1931 |
|
if (mac_fault) { |
1932 |
|
|
1933 |
|
// Get opcode and divide into fields |
1934 |
< |
uint32 opcode = *((uint32 *)r->nip); |
1934 |
> |
uint32 opcode = *((uint32 *)r->pc()); |
1935 |
|
uint32 primop = opcode >> 26; |
1936 |
|
uint32 exop = (opcode >> 1) & 0x3ff; |
1937 |
|
uint32 ra = (opcode >> 16) & 0x1f; |
1942 |
|
switch (primop) { |
1943 |
|
case 9: // POWER instructions |
1944 |
|
case 22: |
1945 |
< |
power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->nip, r->gpr[1], opcode); |
1945 |
> |
power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->pc(), r->gpr(1), opcode); |
1946 |
|
ErrorAlert(str); |
1947 |
|
QuitEmulator(); |
1948 |
|
return; |
1950 |
|
case 31: |
1951 |
|
switch (exop) { |
1952 |
|
case 83: // mfmsr |
1953 |
< |
r->gpr[rd] = 0xf072; |
1954 |
< |
r->nip += 4; |
1953 |
> |
r->gpr(rd) = 0xf072; |
1954 |
> |
r->pc() += 4; |
1955 |
|
goto rti; |
1956 |
|
|
1957 |
|
case 210: // mtsr |
1958 |
|
case 242: // mtsrin |
1959 |
|
case 306: // tlbie |
1960 |
< |
r->nip += 4; |
1960 |
> |
r->pc() += 4; |
1961 |
|
goto rti; |
1962 |
|
|
1963 |
|
case 339: { // mfspr |
1973 |
|
case 957: // PMC3 |
1974 |
|
case 958: // PMC4 |
1975 |
|
case 959: // SDA |
1976 |
< |
r->nip += 4; |
1976 |
> |
r->pc() += 4; |
1977 |
|
goto rti; |
1978 |
|
case 25: // SDR1 |
1979 |
< |
r->gpr[rd] = 0xdead001f; |
1980 |
< |
r->nip += 4; |
1979 |
> |
r->gpr(rd) = 0xdead001f; |
1980 |
> |
r->pc() += 4; |
1981 |
|
goto rti; |
1982 |
|
case 287: // PVR |
1983 |
< |
r->gpr[rd] = PVR; |
1984 |
< |
r->nip += 4; |
1983 |
> |
r->gpr(rd) = PVR; |
1984 |
> |
r->pc() += 4; |
1985 |
|
goto rti; |
1986 |
|
} |
1987 |
|
break; |
2017 |
|
case 957: // PMC3 |
2018 |
|
case 958: // PMC4 |
2019 |
|
case 959: // SDA |
2020 |
< |
r->nip += 4; |
2020 |
> |
r->pc() += 4; |
2021 |
|
goto rti; |
2022 |
|
} |
2023 |
|
break; |
2036 |
|
|
2037 |
|
// In GUI mode, show error alert |
2038 |
|
if (!PrefsFindBool("nogui")) { |
2039 |
< |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode); |
2039 |
> |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode); |
2040 |
|
ErrorAlert(str); |
2041 |
|
QuitEmulator(); |
2042 |
|
return; |
2044 |
|
} |
2045 |
|
|
2046 |
|
// For all other errors, jump into debugger (sort of...) |
2047 |
+ |
crash_reason = "SIGILL"; |
2048 |
|
if (!ready_for_signals) { |
2049 |
< |
printf("SIGILL\n"); |
2050 |
< |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
2049 |
> |
printf("%s\n"); |
2050 |
> |
printf(" sigcontext %p, machine_regs %p\n", scp, r); |
2051 |
|
printf( |
2052 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
2053 |
|
" xer %08lx cr %08lx \n" |
2059 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
2060 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
2061 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
2062 |
< |
r->nip, r->link, r->ctr, r->msr, |
2063 |
< |
r->xer, r->ccr, |
2064 |
< |
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
2065 |
< |
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
2066 |
< |
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
2067 |
< |
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
2068 |
< |
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
2069 |
< |
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
2070 |
< |
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
2071 |
< |
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
2062 |
> |
crash_reason, |
2063 |
> |
r->pc(), r->lr(), r->ctr(), r->msr(), |
2064 |
> |
r->xer(), r->cr(), |
2065 |
> |
r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3), |
2066 |
> |
r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7), |
2067 |
> |
r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11), |
2068 |
> |
r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15), |
2069 |
> |
r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19), |
2070 |
> |
r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23), |
2071 |
> |
r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27), |
2072 |
> |
r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31)); |
2073 |
|
exit(1); |
2074 |
|
QuitEmulator(); |
2075 |
|
return; |
2076 |
|
} else { |
2077 |
|
// We crashed. Save registers, tell tick thread and loop forever |
2078 |
< |
sigsegv_regs = *(sigregs *)r; |
2078 |
> |
build_sigregs(&sigsegv_regs, r); |
2079 |
|
emul_thread_fatal = true; |
2080 |
|
for (;;) ; |
2081 |
|
} |
2085 |
|
|
2086 |
|
|
2087 |
|
/* |
2088 |
+ |
* Helpers to share 32-bit addressable data with MacOS |
2089 |
+ |
*/ |
2090 |
+ |
|
2091 |
+ |
bool SheepMem::Init(void) |
2092 |
+ |
{ |
2093 |
+ |
// Size of a native page |
2094 |
+ |
page_size = getpagesize(); |
2095 |
+ |
|
2096 |
+ |
// Allocate SheepShaver globals |
2097 |
+ |
proc = base; |
2098 |
+ |
if (vm_mac_acquire(base, size) < 0) |
2099 |
+ |
return false; |
2100 |
+ |
|
2101 |
+ |
// Allocate page with all bits set to 0, right in the middle |
2102 |
+ |
// This is also used to catch undesired overlaps between proc and data areas |
2103 |
+ |
zero_page = proc + (size / 2); |
2104 |
+ |
Mac_memset(zero_page, 0, page_size); |
2105 |
+ |
if (vm_protect(Mac2HostAddr(zero_page), page_size, VM_PAGE_READ) < 0) |
2106 |
+ |
return false; |
2107 |
+ |
|
2108 |
+ |
#if EMULATED_PPC |
2109 |
+ |
// Allocate alternate stack for PowerPC interrupt routine |
2110 |
+ |
sig_stack = base + size; |
2111 |
+ |
if (vm_mac_acquire(sig_stack, SIG_STACK_SIZE) < 0) |
2112 |
+ |
return false; |
2113 |
+ |
#endif |
2114 |
+ |
|
2115 |
+ |
data = base + size; |
2116 |
+ |
return true; |
2117 |
+ |
} |
2118 |
+ |
|
2119 |
+ |
void SheepMem::Exit(void) |
2120 |
+ |
{ |
2121 |
+ |
if (data) { |
2122 |
+ |
// Delete SheepShaver globals |
2123 |
+ |
vm_mac_release(base, size); |
2124 |
+ |
|
2125 |
+ |
#if EMULATED_PPC |
2126 |
+ |
// Delete alternate stack for PowerPC interrupt routine |
2127 |
+ |
vm_mac_release(sig_stack, SIG_STACK_SIZE); |
2128 |
+ |
#endif |
2129 |
+ |
} |
2130 |
+ |
} |
2131 |
+ |
|
2132 |
+ |
|
2133 |
+ |
/* |
2134 |
|
* Display alert |
2135 |
|
*/ |
2136 |
|
|
2179 |
|
|
2180 |
|
void ErrorAlert(const char *text) |
2181 |
|
{ |
2182 |
< |
#ifdef ENABLE_GTK |
2182 |
> |
#if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO) |
2183 |
|
if (PrefsFindBool("nogui") || x_display == NULL) { |
2184 |
|
printf(GetString(STR_SHELL_ERROR_PREFIX), text); |
2185 |
|
return; |
2198 |
|
|
2199 |
|
void WarningAlert(const char *text) |
2200 |
|
{ |
2201 |
< |
#ifdef ENABLE_GTK |
2201 |
> |
#if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO) |
2202 |
|
if (PrefsFindBool("nogui") || x_display == NULL) { |
2203 |
|
printf(GetString(STR_SHELL_WARNING_PREFIX), text); |
2204 |
|
return; |