1 |
|
/* |
2 |
|
* main_unix.cpp - Emulation core, Unix implementation |
3 |
|
* |
4 |
< |
* SheepShaver (C) 1997-2002 Christian Bauer and Marc Hellwig |
4 |
> |
* SheepShaver (C) 1997-2005 Christian Bauer and Marc Hellwig |
5 |
|
* |
6 |
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
* it under the terms of the GNU General Public License as published by |
27 |
|
* is slightly different from the SysV ABI used by Linux: |
28 |
|
* - Stack frames are different (e.g. LR is stored in 8(r1) under |
29 |
|
* MacOS, but in 4(r1) under Linux) |
30 |
< |
* - There is no TOC under Linux; r2 is free for the user |
30 |
> |
* - There is a pointer to Thread Local Storage (TLS) under Linux with |
31 |
> |
* recent enough glibc. This is r2 in 32-bit mode and r13 in |
32 |
> |
* 64-bit mode (PowerOpen/AIX ABI) |
33 |
|
* - r13 is used as a small data pointer under Linux (but appearently |
34 |
|
* it is not used this way? To be sure, we specify -msdata=none |
35 |
|
* in the Makefile) |
36 |
< |
* - As there is no TOC, there are also no TVECTs under Linux; |
37 |
< |
* function pointers point directly to the function code |
36 |
> |
* - There are no TVECTs under Linux; function pointers point |
37 |
> |
* directly to the function code |
38 |
|
* The Execute*() functions have to account for this. Additionally, we |
39 |
|
* cannot simply call MacOS functions by getting their TVECT and jumping |
40 |
|
* to it. Such calls are done via the call_macos*() functions in |
67 |
|
* ExecutePPC (or any function that might cause a mode switch). The signal |
68 |
|
* stack is restored before exiting the SIGUSR2 handler. |
69 |
|
* |
70 |
+ |
* Note that POSIX standard says you can't modify the alternate |
71 |
+ |
* signal stack while the process is executing on it. There is a |
72 |
+ |
* hackaround though: we install a trampoline SIGUSR2 handler that |
73 |
+ |
* sets up an alternate stack itself and calls the real handler. |
74 |
+ |
* Then, when we call sigaltstack() there, we no longer get an EPERM, |
75 |
+ |
* i.e. it now works. |
76 |
+ |
* |
77 |
|
* TODO: |
78 |
|
* check if SIGSEGV handler works for all registers (including FP!) |
79 |
|
*/ |
102 |
|
#include "xpram.h" |
103 |
|
#include "timer.h" |
104 |
|
#include "adb.h" |
96 |
– |
#include "sony.h" |
97 |
– |
#include "disk.h" |
98 |
– |
#include "cdrom.h" |
99 |
– |
#include "scsi.h" |
105 |
|
#include "video.h" |
101 |
– |
#include "audio.h" |
102 |
– |
#include "ether.h" |
103 |
– |
#include "serial.h" |
104 |
– |
#include "clip.h" |
105 |
– |
#include "extfs.h" |
106 |
|
#include "sys.h" |
107 |
|
#include "macos_util.h" |
108 |
|
#include "rom_patches.h" |
114 |
|
#include "debug.h" |
115 |
|
|
116 |
|
|
117 |
+ |
#ifdef HAVE_DIRENT_H |
118 |
+ |
#include <dirent.h> |
119 |
+ |
#endif |
120 |
+ |
|
121 |
+ |
#ifdef USE_SDL |
122 |
+ |
#include <SDL.h> |
123 |
+ |
#endif |
124 |
+ |
|
125 |
+ |
#ifndef USE_SDL_VIDEO |
126 |
|
#include <X11/Xlib.h> |
127 |
+ |
#endif |
128 |
|
|
129 |
|
#ifdef ENABLE_GTK |
130 |
|
#include <gtk/gtk.h> |
141 |
|
#endif |
142 |
|
|
143 |
|
|
144 |
+ |
// Enable emulation of unaligned lmw/stmw? |
145 |
+ |
#define EMULATE_UNALIGNED_LOADSTORE_MULTIPLE 1 |
146 |
+ |
|
147 |
|
// Enable Execute68k() safety checks? |
148 |
|
#define SAFE_EXEC_68K 0 |
149 |
|
|
158 |
|
const char ROM_FILE_NAME[] = "ROM"; |
159 |
|
const char ROM_FILE_NAME2[] = "Mac OS ROM"; |
160 |
|
|
161 |
< |
const uint32 RAM_BASE = 0x20000000; // Base address of RAM |
161 |
> |
#if REAL_ADDRESSING |
162 |
> |
const uintptr RAM_BASE = 0x20000000; // Base address of RAM |
163 |
> |
#else |
164 |
> |
// FIXME: needs to be >= 0x04000000 |
165 |
> |
const uintptr RAM_BASE = 0x10000000; // Base address of RAM |
166 |
> |
#endif |
167 |
|
const uint32 SIG_STACK_SIZE = 0x10000; // Size of signal stack |
168 |
|
|
169 |
|
|
170 |
|
#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 |
– |
|
171 |
|
struct sigregs { |
172 |
< |
elf_gregset_t gp_regs; // Identical to pt_regs |
173 |
< |
double fp_regs[ELF_NFPREG]; // f0..f31 and fpsrc |
174 |
< |
//more (uninteresting) stuff following here |
172 |
> |
uint32 nip; |
173 |
> |
uint32 link; |
174 |
> |
uint32 ctr; |
175 |
> |
uint32 msr; |
176 |
> |
uint32 xer; |
177 |
> |
uint32 ccr; |
178 |
> |
uint32 gpr[32]; |
179 |
> |
}; |
180 |
> |
|
181 |
> |
#if defined(__linux__) |
182 |
> |
#include <sys/ucontext.h> |
183 |
> |
#define MACHINE_REGISTERS(scp) ((machine_regs *)(((ucontext_t *)scp)->uc_mcontext.regs)) |
184 |
> |
|
185 |
> |
struct machine_regs : public pt_regs |
186 |
> |
{ |
187 |
> |
u_long & cr() { return pt_regs::ccr; } |
188 |
> |
uint32 cr() const { return pt_regs::ccr; } |
189 |
> |
uint32 lr() const { return pt_regs::link; } |
190 |
> |
uint32 ctr() const { return pt_regs::ctr; } |
191 |
> |
uint32 xer() const { return pt_regs::xer; } |
192 |
> |
uint32 msr() const { return pt_regs::msr; } |
193 |
> |
uint32 dar() const { return pt_regs::dar; } |
194 |
> |
u_long & pc() { return pt_regs::nip; } |
195 |
> |
uint32 pc() const { return pt_regs::nip; } |
196 |
> |
u_long & gpr(int i) { return pt_regs::gpr[i]; } |
197 |
> |
uint32 gpr(int i) const { return pt_regs::gpr[i]; } |
198 |
|
}; |
199 |
|
#endif |
200 |
|
|
201 |
+ |
#if defined(__NetBSD__) |
202 |
+ |
#include <sys/ucontext.h> |
203 |
+ |
#define MACHINE_REGISTERS(scp) ((machine_regs *)&(((ucontext_t *)scp)->uc_mcontext)) |
204 |
+ |
|
205 |
+ |
struct machine_regs : public mcontext_t |
206 |
+ |
{ |
207 |
+ |
long & cr() { return __gregs[_REG_CR]; } |
208 |
+ |
uint32 cr() const { return __gregs[_REG_CR]; } |
209 |
+ |
uint32 lr() const { return __gregs[_REG_LR]; } |
210 |
+ |
uint32 ctr() const { return __gregs[_REG_CTR]; } |
211 |
+ |
uint32 xer() const { return __gregs[_REG_XER]; } |
212 |
+ |
uint32 msr() const { return __gregs[_REG_MSR]; } |
213 |
+ |
uint32 dar() const { return (uint32)(((siginfo_t *)(((unsigned long)this) - offsetof(ucontext_t, uc_mcontext))) - 1)->si_addr; } /* HACK */ |
214 |
+ |
long & pc() { return __gregs[_REG_PC]; } |
215 |
+ |
uint32 pc() const { return __gregs[_REG_PC]; } |
216 |
+ |
long & gpr(int i) { return __gregs[_REG_R0 + i]; } |
217 |
+ |
uint32 gpr(int i) const { return __gregs[_REG_R0 + i]; } |
218 |
+ |
}; |
219 |
+ |
#endif |
220 |
+ |
|
221 |
+ |
#if defined(__APPLE__) && defined(__MACH__) |
222 |
+ |
#include <sys/signal.h> |
223 |
+ |
extern "C" int sigaltstack(const struct sigaltstack *ss, struct sigaltstack *oss); |
224 |
+ |
|
225 |
+ |
#include <sys/ucontext.h> |
226 |
+ |
#define MACHINE_REGISTERS(scp) ((machine_regs *)(((ucontext_t *)scp)->uc_mcontext)) |
227 |
+ |
|
228 |
+ |
struct machine_regs : public mcontext |
229 |
+ |
{ |
230 |
+ |
uint32 & cr() { return ss.cr; } |
231 |
+ |
uint32 cr() const { return ss.cr; } |
232 |
+ |
uint32 lr() const { return ss.lr; } |
233 |
+ |
uint32 ctr() const { return ss.ctr; } |
234 |
+ |
uint32 xer() const { return ss.xer; } |
235 |
+ |
uint32 msr() const { return ss.srr1; } |
236 |
+ |
uint32 dar() const { return es.dar; } |
237 |
+ |
uint32 & pc() { return ss.srr0; } |
238 |
+ |
uint32 pc() const { return ss.srr0; } |
239 |
+ |
uint32 & gpr(int i) { return (&ss.r0)[i]; } |
240 |
+ |
uint32 gpr(int i) const { return (&ss.r0)[i]; } |
241 |
+ |
}; |
242 |
+ |
#endif |
243 |
+ |
|
244 |
+ |
static void build_sigregs(sigregs *srp, machine_regs *mrp) |
245 |
+ |
{ |
246 |
+ |
srp->nip = mrp->pc(); |
247 |
+ |
srp->link = mrp->lr(); |
248 |
+ |
srp->ctr = mrp->ctr(); |
249 |
+ |
srp->msr = mrp->msr(); |
250 |
+ |
srp->xer = mrp->xer(); |
251 |
+ |
srp->ccr = mrp->cr(); |
252 |
+ |
for (int i = 0; i < 32; i++) |
253 |
+ |
srp->gpr[i] = mrp->gpr(i); |
254 |
+ |
} |
255 |
+ |
#endif |
256 |
+ |
|
257 |
|
|
258 |
|
// Global variables (exported) |
259 |
|
#if !EMULATED_PPC |
260 |
< |
void *TOC; // Small data pointer (r13) |
260 |
> |
void *TOC = NULL; // Pointer to Thread Local Storage (r2) |
261 |
> |
void *R13 = NULL; // Pointer to .sdata section (r13 under Linux) |
262 |
|
#endif |
263 |
|
uint32 RAMBase; // Base address of Mac RAM |
264 |
|
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 |
265 |
|
uint32 KernelDataAddr; // Address of Kernel Data |
266 |
|
uint32 BootGlobsAddr; // Address of BootGlobs structure at top of Mac RAM |
267 |
+ |
uint32 DRCacheAddr; // Address of DR Cache |
268 |
|
uint32 PVR; // Theoretical PVR |
269 |
|
int64 CPUClockSpeed; // Processor clock speed (Hz) |
270 |
|
int64 BusClockSpeed; // Bus clock speed (Hz) |
271 |
+ |
int64 TimebaseSpeed; // Timebase clock speed (Hz) |
272 |
+ |
uint8 *RAMBaseHost; // Base address of Mac RAM (host address space) |
273 |
+ |
uint8 *ROMBaseHost; // Base address of Mac ROM (host address space) |
274 |
|
|
275 |
|
|
276 |
|
// Global variables |
277 |
< |
static char *x_display_name = NULL; // X11 display name |
277 |
> |
#ifndef USE_SDL_VIDEO |
278 |
> |
char *x_display_name = NULL; // X11 display name |
279 |
|
Display *x_display = NULL; // X11 display handle |
280 |
+ |
#ifdef X11_LOCK_TYPE |
281 |
+ |
X11_LOCK_TYPE x_display_lock = X11_LOCK_INIT; // X11 display lock |
282 |
+ |
#endif |
283 |
+ |
#endif |
284 |
|
|
285 |
|
static int zero_fd = 0; // FD of /dev/zero |
190 |
– |
static bool sheep_area_mapped = false; // Flag: SheepShaver data area mmap()ed |
286 |
|
static bool lm_area_mapped = false; // Flag: Low Memory area mmap()ped |
287 |
|
static int kernel_area = -1; // SHM ID of Kernel Data area |
288 |
|
static bool rom_area_mapped = false; // Flag: Mac ROM mmap()ped |
289 |
|
static bool ram_area_mapped = false; // Flag: Mac RAM mmap()ped |
290 |
+ |
static bool dr_cache_area_mapped = false; // Flag: Mac DR Cache mmap()ped |
291 |
+ |
static bool dr_emulator_area_mapped = false;// Flag: Mac DR Emulator mmap()ped |
292 |
|
static KernelData *kernel_data; // Pointer to Kernel Data |
293 |
|
static EmulatorData *emulator_data; |
294 |
|
|
295 |
|
static uint8 last_xpram[XPRAM_SIZE]; // Buffer for monitoring XPRAM changes |
296 |
|
|
297 |
|
static bool nvram_thread_active = false; // Flag: NVRAM watchdog installed |
298 |
+ |
static volatile bool nvram_thread_cancel; // Flag: Cancel NVRAM thread |
299 |
|
static pthread_t nvram_thread; // NVRAM watchdog |
300 |
|
static bool tick_thread_active = false; // Flag: MacOS thread installed |
301 |
+ |
static volatile bool tick_thread_cancel; // Flag: Cancel 60Hz thread |
302 |
|
static pthread_t tick_thread; // 60Hz thread |
303 |
|
static pthread_t emul_thread; // MacOS thread |
304 |
|
|
306 |
|
static int64 num_segv = 0; // Number of handled SEGV signals |
307 |
|
|
308 |
|
static struct sigaction sigusr2_action; // Interrupt signal (of emulator thread) |
309 |
< |
#if !EMULATED_PPC |
309 |
> |
#if EMULATED_PPC |
310 |
> |
static uintptr sig_stack = 0; // Stack for PowerPC interrupt routine |
311 |
> |
#else |
312 |
|
static struct sigaction sigsegv_action; // Data access exception signal (of emulator thread) |
313 |
|
static struct sigaction sigill_action; // Illegal instruction signal (of emulator thread) |
314 |
< |
static void *sig_stack = NULL; // Stack for signal handlers |
315 |
< |
static void *extra_stack = NULL; // Stack for SIGSEGV inside interrupt handler |
314 |
> |
static struct sigaltstack sig_stack; // Stack for signal handlers |
315 |
> |
static struct sigaltstack extra_stack; // Stack for SIGSEGV inside interrupt handler |
316 |
|
static bool emul_thread_fatal = false; // Flag: MacOS thread crashed, tick thread shall dump debug output |
317 |
|
static sigregs sigsegv_regs; // Register dump when crashed |
318 |
+ |
static const char *crash_reason = NULL; // Reason of the crash (SIGSEGV, SIGBUS, SIGILL) |
319 |
|
#endif |
320 |
|
|
321 |
+ |
uint32 SheepMem::page_size; // Size of a native page |
322 |
+ |
uintptr SheepMem::zero_page = 0; // Address of ro page filled in with zeros |
323 |
+ |
uintptr SheepMem::base = 0x60000000; // Address of SheepShaver data |
324 |
+ |
uintptr SheepMem::proc; // Bottom address of SheepShave procedures |
325 |
+ |
uintptr SheepMem::data; // Top of SheepShaver data (stack like storage) |
326 |
+ |
|
327 |
|
|
328 |
|
// Prototypes |
329 |
+ |
static bool kernel_data_init(void); |
330 |
+ |
static void kernel_data_exit(void); |
331 |
|
static void Quit(void); |
332 |
|
static void *emul_func(void *arg); |
333 |
|
static void *nvram_func(void *arg); |
334 |
|
static void *tick_func(void *arg); |
335 |
|
#if EMULATED_PPC |
336 |
< |
static void sigusr2_handler(int sig); |
336 |
> |
extern void emul_ppc(uint32 start); |
337 |
> |
extern void init_emul_ppc(void); |
338 |
> |
extern void exit_emul_ppc(void); |
339 |
> |
sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t); |
340 |
|
#else |
341 |
< |
static void sigusr2_handler(int sig, sigcontext_struct *sc); |
342 |
< |
static void sigsegv_handler(int sig, sigcontext_struct *sc); |
343 |
< |
static void sigill_handler(int sig, sigcontext_struct *sc); |
341 |
> |
extern "C" void sigusr2_handler_init(int sig, siginfo_t *sip, void *scp); |
342 |
> |
extern "C" void sigusr2_handler(int sig, siginfo_t *sip, void *scp); |
343 |
> |
static void sigsegv_handler(int sig, siginfo_t *sip, void *scp); |
344 |
> |
static void sigill_handler(int sig, siginfo_t *sip, void *scp); |
345 |
|
#endif |
346 |
|
|
347 |
|
|
348 |
|
// From asm_linux.S |
349 |
< |
#if EMULATED_PPC |
236 |
< |
extern int atomic_add(int *var, int v); |
237 |
< |
extern int atomic_and(int *var, int v); |
238 |
< |
extern int atomic_or(int *var, int v); |
239 |
< |
#else |
240 |
< |
extern "C" void *get_toc(void); |
349 |
> |
#if !EMULATED_PPC |
350 |
|
extern "C" void *get_sp(void); |
351 |
< |
extern "C" void flush_icache_range(void *start, void *end); |
351 |
> |
extern "C" void *get_r2(void); |
352 |
> |
extern "C" void set_r2(void *); |
353 |
> |
extern "C" void *get_r13(void); |
354 |
> |
extern "C" void set_r13(void *); |
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; |
462 |
|
printf(" %s\n", GetString(STR_ABOUT_TEXT2)); |
463 |
|
|
464 |
|
#if !EMULATED_PPC |
465 |
+ |
#ifdef SYSTEM_CLOBBERS_R2 |
466 |
|
// Get TOC pointer |
467 |
< |
TOC = get_toc(); |
467 |
> |
TOC = get_r2(); |
468 |
> |
#endif |
469 |
> |
#ifdef SYSTEM_CLOBBERS_R13 |
470 |
> |
// Get r13 register |
471 |
> |
R13 = get_r13(); |
472 |
> |
#endif |
473 |
|
#endif |
474 |
|
|
475 |
|
#ifdef ENABLE_GTK |
485 |
|
for (int i=1; i<argc; i++) { |
486 |
|
if (strcmp(argv[i], "--help") == 0) { |
487 |
|
usage(argv[0]); |
488 |
+ |
#ifndef USE_SDL_VIDEO |
489 |
|
} else if (strcmp(argv[i], "--display") == 0) { |
490 |
|
i++; |
491 |
|
if (i < argc) |
492 |
|
x_display_name = strdup(argv[i]); |
493 |
+ |
#endif |
494 |
|
} else if (argv[i][0] == '-') { |
495 |
|
fprintf(stderr, "Unrecognized option '%s'\n", argv[i]); |
496 |
|
usage(argv[0]); |
497 |
|
} |
498 |
|
} |
499 |
|
|
500 |
+ |
#ifdef USE_SDL |
501 |
+ |
// Initialize SDL system |
502 |
+ |
int sdl_flags = 0; |
503 |
+ |
#ifdef USE_SDL_VIDEO |
504 |
+ |
sdl_flags |= SDL_INIT_VIDEO; |
505 |
+ |
#endif |
506 |
+ |
#ifdef USE_SDL_AUDIO |
507 |
+ |
sdl_flags |= SDL_INIT_AUDIO; |
508 |
+ |
#endif |
509 |
+ |
assert(sdl_flags != 0); |
510 |
+ |
if (SDL_Init(sdl_flags) == -1) { |
511 |
+ |
char str[256]; |
512 |
+ |
sprintf(str, "Could not initialize SDL: %s.\n", SDL_GetError()); |
513 |
+ |
ErrorAlert(str); |
514 |
+ |
goto quit; |
515 |
+ |
} |
516 |
+ |
atexit(SDL_Quit); |
517 |
+ |
#endif |
518 |
+ |
|
519 |
+ |
#ifndef USE_SDL_VIDEO |
520 |
|
// Open display |
521 |
|
x_display = XOpenDisplay(x_display_name); |
522 |
|
if (x_display == NULL) { |
530 |
|
// Fork out, so we can return from fullscreen mode when things get ugly |
531 |
|
XF86DGAForkApp(DefaultScreen(x_display)); |
532 |
|
#endif |
533 |
+ |
#endif |
534 |
|
|
535 |
|
#ifdef ENABLE_MON |
536 |
|
// Initialize mon |
537 |
|
mon_init(); |
538 |
|
#endif |
539 |
|
|
540 |
+ |
#if !EMULATED_PPC |
541 |
+ |
// Create and install stacks for signal handlers |
542 |
+ |
sig_stack.ss_sp = malloc(SIG_STACK_SIZE); |
543 |
+ |
D(bug("Signal stack at %p\n", sig_stack.ss_sp)); |
544 |
+ |
if (sig_stack.ss_sp == NULL) { |
545 |
+ |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
546 |
+ |
goto quit; |
547 |
+ |
} |
548 |
+ |
sig_stack.ss_flags = 0; |
549 |
+ |
sig_stack.ss_size = SIG_STACK_SIZE; |
550 |
+ |
if (sigaltstack(&sig_stack, NULL) < 0) { |
551 |
+ |
sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno)); |
552 |
+ |
ErrorAlert(str); |
553 |
+ |
goto quit; |
554 |
+ |
} |
555 |
+ |
extra_stack.ss_sp = malloc(SIG_STACK_SIZE); |
556 |
+ |
D(bug("Extra stack at %p\n", extra_stack.ss_sp)); |
557 |
+ |
if (extra_stack.ss_sp == NULL) { |
558 |
+ |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
559 |
+ |
goto quit; |
560 |
+ |
} |
561 |
+ |
extra_stack.ss_flags = 0; |
562 |
+ |
extra_stack.ss_size = SIG_STACK_SIZE; |
563 |
+ |
#endif |
564 |
+ |
|
565 |
+ |
#if !EMULATED_PPC |
566 |
+ |
// Install SIGSEGV and SIGBUS handlers |
567 |
+ |
sigemptyset(&sigsegv_action.sa_mask); // Block interrupts during SEGV handling |
568 |
+ |
sigaddset(&sigsegv_action.sa_mask, SIGUSR2); |
569 |
+ |
sigsegv_action.sa_sigaction = sigsegv_handler; |
570 |
+ |
sigsegv_action.sa_flags = SA_ONSTACK | SA_SIGINFO; |
571 |
+ |
#ifdef HAVE_SIGNAL_SA_RESTORER |
572 |
+ |
sigsegv_action.sa_restorer = NULL; |
573 |
+ |
#endif |
574 |
+ |
if (sigaction(SIGSEGV, &sigsegv_action, NULL) < 0) { |
575 |
+ |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
576 |
+ |
ErrorAlert(str); |
577 |
+ |
goto quit; |
578 |
+ |
} |
579 |
+ |
if (sigaction(SIGBUS, &sigsegv_action, NULL) < 0) { |
580 |
+ |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
581 |
+ |
ErrorAlert(str); |
582 |
+ |
goto quit; |
583 |
+ |
} |
584 |
+ |
#else |
585 |
+ |
// Install SIGSEGV handler for CPU emulator |
586 |
+ |
if (!sigsegv_install_handler(sigsegv_handler)) { |
587 |
+ |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
588 |
+ |
ErrorAlert(str); |
589 |
+ |
goto quit; |
590 |
+ |
} |
591 |
+ |
#endif |
592 |
+ |
|
593 |
+ |
// Initialize VM system |
594 |
+ |
vm_init(); |
595 |
+ |
|
596 |
|
// Get system info |
597 |
|
PVR = 0x00040000; // Default: 604 |
598 |
|
CPUClockSpeed = 100000000; // Default: 100MHz |
599 |
|
BusClockSpeed = 100000000; // Default: 100MHz |
600 |
< |
#if !EMULATED_PPC |
600 |
> |
TimebaseSpeed = 25000000; // Default: 25MHz |
601 |
> |
#if EMULATED_PPC |
602 |
> |
PVR = 0x000c0000; // Default: 7400 (with AltiVec) |
603 |
> |
#elif defined(__APPLE__) && defined(__MACH__) |
604 |
> |
proc_file = popen("ioreg -c IOPlatformDevice", "r"); |
605 |
> |
if (proc_file) { |
606 |
> |
char line[256]; |
607 |
> |
bool powerpc_node = false; |
608 |
> |
while (fgets(line, sizeof(line) - 1, proc_file)) { |
609 |
> |
// Read line |
610 |
> |
int len = strlen(line); |
611 |
> |
if (len == 0) |
612 |
> |
continue; |
613 |
> |
line[len - 1] = 0; |
614 |
> |
|
615 |
> |
// Parse line |
616 |
> |
if (strstr(line, "o PowerPC,")) |
617 |
> |
powerpc_node = true; |
618 |
> |
else if (powerpc_node) { |
619 |
> |
uint32 value; |
620 |
> |
char head[256]; |
621 |
> |
if (sscanf(line, "%[ |]\"cpu-version\" = <%x>", head, &value) == 2) |
622 |
> |
PVR = value; |
623 |
> |
else if (sscanf(line, "%[ |]\"clock-frequency\" = <%x>", head, &value) == 2) |
624 |
> |
CPUClockSpeed = value; |
625 |
> |
else if (sscanf(line, "%[ |]\"bus-frequency\" = <%x>", head, &value) == 2) |
626 |
> |
BusClockSpeed = value; |
627 |
> |
else if (sscanf(line, "%[ |]\"timebase-frequency\" = <%x>", head, &value) == 2) |
628 |
> |
TimebaseSpeed = value; |
629 |
> |
else if (strchr(line, '}')) |
630 |
> |
powerpc_node = false; |
631 |
> |
} |
632 |
> |
} |
633 |
> |
fclose(proc_file); |
634 |
> |
} else { |
635 |
> |
sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno)); |
636 |
> |
WarningAlert(str); |
637 |
> |
} |
638 |
> |
#else |
639 |
|
proc_file = fopen("/proc/cpuinfo", "r"); |
640 |
|
if (proc_file) { |
641 |
+ |
// CPU specs from Linux kernel |
642 |
+ |
// TODO: make it more generic with features (e.g. AltiVec) and |
643 |
+ |
// cache information and friends for NameRegistry |
644 |
+ |
static const struct { |
645 |
+ |
uint32 pvr_mask; |
646 |
+ |
uint32 pvr_value; |
647 |
+ |
const char *cpu_name; |
648 |
+ |
} |
649 |
+ |
cpu_specs[] = { |
650 |
+ |
{ 0xffff0000, 0x00010000, "601" }, |
651 |
+ |
{ 0xffff0000, 0x00030000, "603" }, |
652 |
+ |
{ 0xffff0000, 0x00060000, "603e" }, |
653 |
+ |
{ 0xffff0000, 0x00070000, "603ev" }, |
654 |
+ |
{ 0xffff0000, 0x00040000, "604" }, |
655 |
+ |
{ 0xfffff000, 0x00090000, "604e" }, |
656 |
+ |
{ 0xffff0000, 0x00090000, "604r" }, |
657 |
+ |
{ 0xffff0000, 0x000a0000, "604ev" }, |
658 |
+ |
{ 0xffffffff, 0x00084202, "740/750" }, |
659 |
+ |
{ 0xfffff000, 0x00083000, "745/755" }, |
660 |
+ |
{ 0xfffffff0, 0x00080100, "750CX" }, |
661 |
+ |
{ 0xfffffff0, 0x00082200, "750CX" }, |
662 |
+ |
{ 0xfffffff0, 0x00082210, "750CXe" }, |
663 |
+ |
{ 0xffffff00, 0x70000100, "750FX" }, |
664 |
+ |
{ 0xffffffff, 0x70000200, "750FX" }, |
665 |
+ |
{ 0xffff0000, 0x70000000, "750FX" }, |
666 |
+ |
{ 0xffff0000, 0x70020000, "750GX" }, |
667 |
+ |
{ 0xffff0000, 0x00080000, "740/750" }, |
668 |
+ |
{ 0xffffffff, 0x000c1101, "7400 (1.1)" }, |
669 |
+ |
{ 0xffff0000, 0x000c0000, "7400" }, |
670 |
+ |
{ 0xffff0000, 0x800c0000, "7410" }, |
671 |
+ |
{ 0xffffffff, 0x80000200, "7450" }, |
672 |
+ |
{ 0xffffffff, 0x80000201, "7450" }, |
673 |
+ |
{ 0xffff0000, 0x80000000, "7450" }, |
674 |
+ |
{ 0xffffff00, 0x80010100, "7455" }, |
675 |
+ |
{ 0xffffffff, 0x80010200, "7455" }, |
676 |
+ |
{ 0xffff0000, 0x80010000, "7455" }, |
677 |
+ |
{ 0xffff0000, 0x80020000, "7457" }, |
678 |
+ |
{ 0xffff0000, 0x80030000, "7447A" }, |
679 |
+ |
{ 0x7fff0000, 0x00810000, "82xx" }, |
680 |
+ |
{ 0x7fff0000, 0x00820000, "8280" }, |
681 |
+ |
{ 0xffff0000, 0x00400000, "Power3 (630)" }, |
682 |
+ |
{ 0xffff0000, 0x00410000, "Power3 (630+)" }, |
683 |
+ |
{ 0xffff0000, 0x00360000, "I-star" }, |
684 |
+ |
{ 0xffff0000, 0x00370000, "S-star" }, |
685 |
+ |
{ 0xffff0000, 0x00350000, "Power4" }, |
686 |
+ |
{ 0xffff0000, 0x00390000, "PPC970" }, |
687 |
+ |
{ 0xffff0000, 0x003a0000, "POWER5" }, |
688 |
+ |
{ 0, 0, 0 } |
689 |
+ |
}; |
690 |
+ |
|
691 |
|
char line[256]; |
692 |
|
while(fgets(line, 255, proc_file)) { |
693 |
|
// Read line |
699 |
|
// Parse line |
700 |
|
int i; |
701 |
|
char value[256]; |
702 |
< |
if (sscanf(line, "cpu : %s", value) == 1) { |
703 |
< |
if (strcmp(value, "601") == 0) |
704 |
< |
PVR = 0x00010000; |
705 |
< |
else if (strcmp(value, "603") == 0) |
706 |
< |
PVR = 0x00030000; |
707 |
< |
else if (strcmp(value, "604") == 0) |
708 |
< |
PVR = 0x00040000; |
709 |
< |
else if (strcmp(value, "603e") == 0) |
710 |
< |
PVR = 0x00060000; |
711 |
< |
else if (strcmp(value, "603ev") == 0) |
712 |
< |
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 |
702 |
> |
if (sscanf(line, "cpu : %[0-9A-Za-a]", value) == 1) { |
703 |
> |
// Search by name |
704 |
> |
const char *cpu_name = NULL; |
705 |
> |
for (int i = 0; cpu_specs[i].pvr_mask != 0; i++) { |
706 |
> |
if (strcmp(cpu_specs[i].cpu_name, value) == 0) { |
707 |
> |
cpu_name = cpu_specs[i].cpu_name; |
708 |
> |
PVR = cpu_specs[i].pvr_value; |
709 |
> |
break; |
710 |
> |
} |
711 |
> |
} |
712 |
> |
if (cpu_name == NULL) |
713 |
|
printf("WARNING: Unknown CPU type '%s', assuming 604\n", value); |
714 |
+ |
else |
715 |
+ |
printf("Found a PowerPC %s processor\n", cpu_name); |
716 |
|
} |
717 |
|
if (sscanf(line, "clock : %dMHz", &i) == 1) |
718 |
|
CPUClockSpeed = BusClockSpeed = i * 1000000; |
722 |
|
sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno)); |
723 |
|
WarningAlert(str); |
724 |
|
} |
725 |
+ |
|
726 |
+ |
// Get actual bus frequency |
727 |
+ |
proc_file = fopen("/proc/device-tree/clock-frequency", "r"); |
728 |
+ |
if (proc_file) { |
729 |
+ |
union { uint8 b[4]; uint32 l; } value; |
730 |
+ |
if (fread(value.b, sizeof(value), 1, proc_file) == 1) |
731 |
+ |
BusClockSpeed = value.l; |
732 |
+ |
fclose(proc_file); |
733 |
+ |
} |
734 |
+ |
|
735 |
+ |
// Get actual timebase frequency |
736 |
+ |
TimebaseSpeed = BusClockSpeed / 4; |
737 |
+ |
DIR *cpus_dir; |
738 |
+ |
if ((cpus_dir = opendir("/proc/device-tree/cpus")) != NULL) { |
739 |
+ |
struct dirent *cpu_entry; |
740 |
+ |
while ((cpu_entry = readdir(cpus_dir)) != NULL) { |
741 |
+ |
if (strstr(cpu_entry->d_name, "PowerPC,") == cpu_entry->d_name) { |
742 |
+ |
char timebase_freq_node[256]; |
743 |
+ |
sprintf(timebase_freq_node, "/proc/device-tree/cpus/%s/timebase-frequency", cpu_entry->d_name); |
744 |
+ |
proc_file = fopen(timebase_freq_node, "r"); |
745 |
+ |
if (proc_file) { |
746 |
+ |
union { uint8 b[4]; uint32 l; } value; |
747 |
+ |
if (fread(value.b, sizeof(value), 1, proc_file) == 1) |
748 |
+ |
TimebaseSpeed = value.l; |
749 |
+ |
fclose(proc_file); |
750 |
+ |
} |
751 |
+ |
} |
752 |
+ |
} |
753 |
+ |
closedir(cpus_dir); |
754 |
+ |
} |
755 |
|
#endif |
756 |
+ |
// Remap any newer G4/G5 processor to plain G4 for compatibility |
757 |
+ |
switch (PVR >> 16) { |
758 |
+ |
case 0x8000: // 7450 |
759 |
+ |
case 0x8001: // 7455 |
760 |
+ |
case 0x8002: // 7457 |
761 |
+ |
case 0x0039: // 970 |
762 |
+ |
PVR = 0x000c0000; // 7400 |
763 |
+ |
break; |
764 |
+ |
} |
765 |
|
D(bug("PVR: %08x (assumed)\n", PVR)); |
766 |
|
|
767 |
|
// Init system routines |
785 |
|
goto quit; |
786 |
|
} |
787 |
|
|
788 |
+ |
#ifndef PAGEZERO_HACK |
789 |
|
// Create Low Memory area (0x0000..0x3000) |
790 |
< |
if (vm_acquire_fixed((char *)0, 0x3000) < 0) { |
790 |
> |
if (vm_mac_acquire(0, 0x3000) < 0) { |
791 |
|
sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno)); |
792 |
|
ErrorAlert(str); |
793 |
|
goto quit; |
794 |
|
} |
795 |
|
lm_area_mapped = true; |
796 |
+ |
#endif |
797 |
|
|
798 |
|
// Create areas for Kernel Data |
799 |
< |
kernel_area = shmget(IPC_PRIVATE, KERNEL_AREA_SIZE, 0600); |
800 |
< |
if (kernel_area == -1) { |
801 |
< |
sprintf(str, GetString(STR_KD_SHMGET_ERR), strerror(errno)); |
799 |
> |
if (!kernel_data_init()) |
800 |
> |
goto quit; |
801 |
> |
kernel_data = (KernelData *)Mac2HostAddr(KERNEL_DATA_BASE); |
802 |
> |
emulator_data = &kernel_data->ed; |
803 |
> |
KernelDataAddr = KERNEL_DATA_BASE; |
804 |
> |
D(bug("Kernel Data at %p (%08x)\n", kernel_data, KERNEL_DATA_BASE)); |
805 |
> |
D(bug("Emulator Data at %p (%08x)\n", emulator_data, KERNEL_DATA_BASE + offsetof(KernelData, ed))); |
806 |
> |
|
807 |
> |
// Create area for DR Cache |
808 |
> |
if (vm_mac_acquire(DR_EMULATOR_BASE, DR_EMULATOR_SIZE) < 0) { |
809 |
> |
sprintf(str, GetString(STR_DR_EMULATOR_MMAP_ERR), strerror(errno)); |
810 |
|
ErrorAlert(str); |
811 |
|
goto quit; |
812 |
|
} |
813 |
< |
if (shmat(kernel_area, (void *)KERNEL_DATA_BASE, 0) < 0) { |
814 |
< |
sprintf(str, GetString(STR_KD_SHMAT_ERR), strerror(errno)); |
813 |
> |
dr_emulator_area_mapped = true; |
814 |
> |
if (vm_mac_acquire(DR_CACHE_BASE, DR_CACHE_SIZE) < 0) { |
815 |
> |
sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno)); |
816 |
|
ErrorAlert(str); |
817 |
|
goto quit; |
818 |
|
} |
819 |
< |
if (shmat(kernel_area, (void *)KERNEL_DATA2_BASE, 0) < 0) { |
820 |
< |
sprintf(str, GetString(STR_KD2_SHMAT_ERR), strerror(errno)); |
819 |
> |
dr_cache_area_mapped = true; |
820 |
> |
#if !EMULATED_PPC |
821 |
> |
if (vm_protect((char *)DR_CACHE_BASE, DR_CACHE_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
822 |
> |
sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno)); |
823 |
|
ErrorAlert(str); |
824 |
|
goto quit; |
825 |
|
} |
826 |
< |
kernel_data = (KernelData *)0x68ffe000; |
827 |
< |
emulator_data = &kernel_data->ed; |
828 |
< |
KernelDataAddr = (uint32)kernel_data; |
437 |
< |
D(bug("Kernel Data at %p, Emulator Data at %p\n", kernel_data, emulator_data)); |
826 |
> |
#endif |
827 |
> |
DRCacheAddr = DR_CACHE_BASE; |
828 |
> |
D(bug("DR Cache at %p\n", DRCacheAddr)); |
829 |
|
|
830 |
|
// Create area for SheepShaver data |
831 |
< |
if (vm_acquire_fixed((char *)SHEEP_BASE, SHEEP_SIZE) < 0) { |
831 |
> |
if (!SheepMem::Init()) { |
832 |
|
sprintf(str, GetString(STR_SHEEP_MEM_MMAP_ERR), strerror(errno)); |
833 |
|
ErrorAlert(str); |
834 |
|
goto quit; |
835 |
|
} |
445 |
– |
SheepStack1Base = SHEEP_BASE + 0x10000; |
446 |
– |
SheepStack2Base = SheepStack1Base + 0x10000; |
447 |
– |
SheepThunksBase = SheepStack2Base + 0x1000; |
448 |
– |
sheep_area_mapped = true; |
836 |
|
|
837 |
|
// Create area for Mac ROM |
838 |
< |
if (vm_acquire_fixed((char *)ROM_BASE, ROM_AREA_SIZE) < 0) { |
838 |
> |
if (vm_mac_acquire(ROM_BASE, ROM_AREA_SIZE) < 0) { |
839 |
|
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
840 |
|
ErrorAlert(str); |
841 |
|
goto quit; |
842 |
|
} |
843 |
< |
#if !EMULATED_PPC || defined(__powerpc__) |
844 |
< |
if (vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
843 |
> |
ROMBaseHost = Mac2HostAddr(ROM_BASE); |
844 |
> |
#if !EMULATED_PPC |
845 |
> |
if (vm_protect(ROMBaseHost, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
846 |
|
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
847 |
|
ErrorAlert(str); |
848 |
|
goto quit; |
849 |
|
} |
850 |
|
#endif |
851 |
|
rom_area_mapped = true; |
852 |
< |
D(bug("ROM area at %08x\n", ROM_BASE)); |
852 |
> |
D(bug("ROM area at %p (%08x)\n", ROMBaseHost, ROM_BASE)); |
853 |
|
|
854 |
|
// Create area for Mac RAM |
855 |
|
RAMSize = PrefsFindInt32("ramsize"); |
858 |
|
RAMSize = 8*1024*1024; |
859 |
|
} |
860 |
|
|
861 |
< |
if (vm_acquire_fixed((char *)RAM_BASE, RAMSize) < 0) { |
861 |
> |
if (vm_mac_acquire(RAM_BASE, RAMSize) < 0) { |
862 |
|
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
863 |
|
ErrorAlert(str); |
864 |
|
goto quit; |
865 |
|
} |
866 |
+ |
RAMBaseHost = Mac2HostAddr(RAM_BASE); |
867 |
|
#if !EMULATED_PPC |
868 |
< |
if (vm_protect((char *)RAM_BASE, RAMSize, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
868 |
> |
if (vm_protect(RAMBaseHost, RAMSize, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
869 |
|
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
870 |
|
ErrorAlert(str); |
871 |
|
goto quit; |
873 |
|
#endif |
874 |
|
RAMBase = RAM_BASE; |
875 |
|
ram_area_mapped = true; |
876 |
< |
D(bug("RAM area at %08x\n", RAMBase)); |
876 |
> |
D(bug("RAM area at %p (%08x)\n", RAMBaseHost, RAMBase)); |
877 |
|
|
878 |
|
if (RAMBase > ROM_BASE) { |
879 |
|
ErrorAlert(GetString(STR_RAM_HIGHER_THAN_ROM_ERR)); |
909 |
|
} |
910 |
|
delete[] rom_tmp; |
911 |
|
|
912 |
< |
// Load NVRAM |
913 |
< |
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)); |
912 |
> |
// Initialize everything |
913 |
> |
if (!InitAll()) |
914 |
|
goto quit; |
915 |
< |
} |
915 |
> |
D(bug("Initialization complete\n")); |
916 |
|
|
917 |
|
// Clear caches (as we loaded and patched code) and write protect ROM |
918 |
|
#if !EMULATED_PPC |
919 |
< |
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); |
919 |
> |
flush_icache_range(ROM_BASE, ROM_BASE + ROM_AREA_SIZE); |
920 |
|
#endif |
921 |
< |
D(bug("Low Memory initialized\n")); |
921 |
> |
vm_protect(ROMBaseHost, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_EXECUTE); |
922 |
|
|
923 |
|
// Start 60Hz thread |
924 |
+ |
tick_thread_cancel = false; |
925 |
|
tick_thread_active = (pthread_create(&tick_thread, NULL, tick_func, NULL) == 0); |
926 |
|
D(bug("Tick thread installed (%ld)\n", tick_thread)); |
927 |
|
|
928 |
|
// Start NVRAM watchdog thread |
929 |
|
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
930 |
+ |
nvram_thread_cancel = false; |
931 |
|
nvram_thread_active = (pthread_create(&nvram_thread, NULL, nvram_func, NULL) == 0); |
932 |
|
D(bug("NVRAM thread installed (%ld)\n", nvram_thread)); |
933 |
|
|
934 |
|
#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 |
– |
|
935 |
|
// Install SIGILL handler |
936 |
|
sigemptyset(&sigill_action.sa_mask); // Block interrupts during ILL handling |
937 |
|
sigaddset(&sigill_action.sa_mask, SIGUSR2); |
938 |
< |
sigill_action.sa_handler = (__sighandler_t)sigill_handler; |
939 |
< |
sigill_action.sa_flags = SA_ONSTACK; |
938 |
> |
sigill_action.sa_sigaction = sigill_handler; |
939 |
> |
sigill_action.sa_flags = SA_ONSTACK | SA_SIGINFO; |
940 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
941 |
|
sigill_action.sa_restorer = NULL; |
942 |
+ |
#endif |
943 |
|
if (sigaction(SIGILL, &sigill_action, NULL) < 0) { |
944 |
|
sprintf(str, GetString(STR_SIGILL_INSTALL_ERR), strerror(errno)); |
945 |
|
ErrorAlert(str); |
947 |
|
} |
948 |
|
#endif |
949 |
|
|
950 |
+ |
#if !EMULATED_PPC |
951 |
|
// Install interrupt signal handler |
952 |
|
sigemptyset(&sigusr2_action.sa_mask); |
953 |
< |
sigusr2_action.sa_handler = (__sighandler_t)sigusr2_handler; |
954 |
< |
sigusr2_action.sa_flags = 0; |
955 |
< |
#if !EMULATED_PPC |
720 |
< |
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART; |
721 |
< |
#endif |
953 |
> |
sigusr2_action.sa_sigaction = sigusr2_handler_init; |
954 |
> |
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART | SA_SIGINFO; |
955 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
956 |
|
sigusr2_action.sa_restorer = NULL; |
957 |
+ |
#endif |
958 |
|
if (sigaction(SIGUSR2, &sigusr2_action, NULL) < 0) { |
959 |
|
sprintf(str, GetString(STR_SIGUSR2_INSTALL_ERR), strerror(errno)); |
960 |
|
ErrorAlert(str); |
961 |
|
goto quit; |
962 |
|
} |
963 |
+ |
#endif |
964 |
|
|
965 |
|
// Get my thread ID and execute MacOS thread function |
966 |
|
emul_thread = pthread_self(); |
979 |
|
|
980 |
|
static void Quit(void) |
981 |
|
{ |
982 |
+ |
#if EMULATED_PPC |
983 |
+ |
// Exit PowerPC emulation |
984 |
+ |
exit_emul_ppc(); |
985 |
+ |
#endif |
986 |
+ |
|
987 |
|
// Stop 60Hz thread |
988 |
|
if (tick_thread_active) { |
989 |
+ |
tick_thread_cancel = true; |
990 |
|
pthread_cancel(tick_thread); |
991 |
|
pthread_join(tick_thread, NULL); |
992 |
|
} |
993 |
|
|
994 |
|
// Stop NVRAM watchdog thread |
995 |
|
if (nvram_thread_active) { |
996 |
+ |
nvram_thread_cancel = true; |
997 |
|
pthread_cancel(nvram_thread); |
998 |
|
pthread_join(nvram_thread, NULL); |
999 |
|
} |
1000 |
|
|
1001 |
|
#if !EMULATED_PPC |
1002 |
< |
// Uninstall SIGSEGV handler |
1002 |
> |
// Uninstall SIGSEGV and SIGBUS handlers |
1003 |
|
sigemptyset(&sigsegv_action.sa_mask); |
1004 |
|
sigsegv_action.sa_handler = SIG_DFL; |
1005 |
|
sigsegv_action.sa_flags = 0; |
1006 |
|
sigaction(SIGSEGV, &sigsegv_action, NULL); |
1007 |
+ |
sigaction(SIGBUS, &sigsegv_action, NULL); |
1008 |
|
|
1009 |
|
// Uninstall SIGILL handler |
1010 |
|
sigemptyset(&sigill_action.sa_mask); |
1011 |
|
sigill_action.sa_handler = SIG_DFL; |
1012 |
|
sigill_action.sa_flags = 0; |
1013 |
|
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(); |
1014 |
|
|
1015 |
< |
// Exit serial |
1016 |
< |
SerialExit(); |
1017 |
< |
|
1018 |
< |
// Exit network |
1019 |
< |
EtherExit(); |
1020 |
< |
|
787 |
< |
// Exit audio |
788 |
< |
AudioExit(); |
789 |
< |
|
790 |
< |
// Exit video |
791 |
< |
VideoExit(); |
1015 |
> |
// Delete stacks for signal handlers |
1016 |
> |
if (sig_stack.ss_sp) |
1017 |
> |
free(sig_stack.ss_sp); |
1018 |
> |
if (extra_stack.ss_sp) |
1019 |
> |
free(extra_stack.ss_sp); |
1020 |
> |
#endif |
1021 |
|
|
1022 |
< |
// Exit external file system |
1023 |
< |
ExtFSExit(); |
1022 |
> |
// Deinitialize everything |
1023 |
> |
ExitAll(); |
1024 |
|
|
1025 |
< |
// Exit drivers |
1026 |
< |
SCSIExit(); |
798 |
< |
CDROMExit(); |
799 |
< |
DiskExit(); |
800 |
< |
SonyExit(); |
1025 |
> |
// Delete SheepShaver globals |
1026 |
> |
SheepMem::Exit(); |
1027 |
|
|
1028 |
|
// Delete RAM area |
1029 |
|
if (ram_area_mapped) |
1030 |
< |
vm_release((char *)RAM_BASE, RAMSize); |
1030 |
> |
vm_mac_release(RAM_BASE, RAMSize); |
1031 |
|
|
1032 |
|
// Delete ROM area |
1033 |
|
if (rom_area_mapped) |
1034 |
< |
vm_release((char *)ROM_BASE, ROM_AREA_SIZE); |
1034 |
> |
vm_mac_release(ROM_BASE, ROM_AREA_SIZE); |
1035 |
> |
|
1036 |
> |
// Delete DR cache areas |
1037 |
> |
if (dr_emulator_area_mapped) |
1038 |
> |
vm_mac_release(DR_EMULATOR_BASE, DR_EMULATOR_SIZE); |
1039 |
> |
if (dr_cache_area_mapped) |
1040 |
> |
vm_mac_release(DR_CACHE_BASE, DR_CACHE_SIZE); |
1041 |
|
|
1042 |
|
// Delete Kernel Data area |
1043 |
< |
if (kernel_area >= 0) { |
812 |
< |
shmdt((void *)KERNEL_DATA_BASE); |
813 |
< |
shmdt((void *)KERNEL_DATA2_BASE); |
814 |
< |
shmctl(kernel_area, IPC_RMID, NULL); |
815 |
< |
} |
1043 |
> |
kernel_data_exit(); |
1044 |
|
|
1045 |
|
// Delete Low Memory area |
1046 |
|
if (lm_area_mapped) |
1047 |
< |
munmap((char *)0x0000, 0x3000); |
1047 |
> |
vm_mac_release(0, 0x3000); |
1048 |
|
|
1049 |
|
// Close /dev/zero |
1050 |
|
if (zero_fd > 0) |
1062 |
|
#endif |
1063 |
|
|
1064 |
|
// Close X11 server connection |
1065 |
+ |
#ifndef USE_SDL_VIDEO |
1066 |
|
if (x_display) |
1067 |
|
XCloseDisplay(x_display); |
1068 |
+ |
#endif |
1069 |
|
|
1070 |
|
exit(0); |
1071 |
|
} |
1072 |
|
|
1073 |
|
|
1074 |
|
/* |
1075 |
+ |
* Initialize Kernel Data segments |
1076 |
+ |
*/ |
1077 |
+ |
|
1078 |
+ |
#if defined(__CYGWIN__) |
1079 |
+ |
#define WIN32_LEAN_AND_MEAN |
1080 |
+ |
#include <windows.h> |
1081 |
+ |
|
1082 |
+ |
static HANDLE kernel_handle; // Shared memory handle for Kernel Data |
1083 |
+ |
static DWORD allocation_granule; // Minimum size of allocateable are (64K) |
1084 |
+ |
static DWORD kernel_area_size; // Size of Kernel Data area |
1085 |
+ |
#endif |
1086 |
+ |
|
1087 |
+ |
static bool kernel_data_init(void) |
1088 |
+ |
{ |
1089 |
+ |
char str[256]; |
1090 |
+ |
#ifdef _WIN32 |
1091 |
+ |
SYSTEM_INFO si; |
1092 |
+ |
GetSystemInfo(&si); |
1093 |
+ |
allocation_granule = si.dwAllocationGranularity; |
1094 |
+ |
kernel_area_size = (KERNEL_AREA_SIZE + allocation_granule - 1) & -allocation_granule; |
1095 |
+ |
|
1096 |
+ |
char rcs[10]; |
1097 |
+ |
LPVOID kernel_addr; |
1098 |
+ |
kernel_handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, kernel_area_size, NULL); |
1099 |
+ |
if (kernel_handle == NULL) { |
1100 |
+ |
sprintf(rcs, "%d", GetLastError()); |
1101 |
+ |
sprintf(str, GetString(STR_KD_SHMGET_ERR), rcs); |
1102 |
+ |
ErrorAlert(str); |
1103 |
+ |
return false; |
1104 |
+ |
} |
1105 |
+ |
kernel_addr = (LPVOID)Mac2HostAddr(KERNEL_DATA_BASE & -allocation_granule); |
1106 |
+ |
if (MapViewOfFileEx(kernel_handle, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, kernel_area_size, kernel_addr) != kernel_addr) { |
1107 |
+ |
sprintf(rcs, "%d", GetLastError()); |
1108 |
+ |
sprintf(str, GetString(STR_KD_SHMAT_ERR), rcs); |
1109 |
+ |
ErrorAlert(str); |
1110 |
+ |
return false; |
1111 |
+ |
} |
1112 |
+ |
kernel_addr = (LPVOID)Mac2HostAddr(KERNEL_DATA2_BASE & -allocation_granule); |
1113 |
+ |
if (MapViewOfFileEx(kernel_handle, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, kernel_area_size, kernel_addr) != kernel_addr) { |
1114 |
+ |
sprintf(rcs, "%d", GetLastError()); |
1115 |
+ |
sprintf(str, GetString(STR_KD2_SHMAT_ERR), rcs); |
1116 |
+ |
ErrorAlert(str); |
1117 |
+ |
return false; |
1118 |
+ |
} |
1119 |
+ |
#else |
1120 |
+ |
kernel_area = shmget(IPC_PRIVATE, KERNEL_AREA_SIZE, 0600); |
1121 |
+ |
if (kernel_area == -1) { |
1122 |
+ |
sprintf(str, GetString(STR_KD_SHMGET_ERR), strerror(errno)); |
1123 |
+ |
ErrorAlert(str); |
1124 |
+ |
return false; |
1125 |
+ |
} |
1126 |
+ |
if (shmat(kernel_area, Mac2HostAddr(KERNEL_DATA_BASE), 0) < 0) { |
1127 |
+ |
sprintf(str, GetString(STR_KD_SHMAT_ERR), strerror(errno)); |
1128 |
+ |
ErrorAlert(str); |
1129 |
+ |
return false; |
1130 |
+ |
} |
1131 |
+ |
if (shmat(kernel_area, Mac2HostAddr(KERNEL_DATA2_BASE), 0) < 0) { |
1132 |
+ |
sprintf(str, GetString(STR_KD2_SHMAT_ERR), strerror(errno)); |
1133 |
+ |
ErrorAlert(str); |
1134 |
+ |
return false; |
1135 |
+ |
} |
1136 |
+ |
#endif |
1137 |
+ |
return true; |
1138 |
+ |
} |
1139 |
+ |
|
1140 |
+ |
|
1141 |
+ |
/* |
1142 |
+ |
* Deallocate Kernel Data segments |
1143 |
+ |
*/ |
1144 |
+ |
|
1145 |
+ |
static void kernel_data_exit(void) |
1146 |
+ |
{ |
1147 |
+ |
#ifdef _WIN32 |
1148 |
+ |
if (kernel_handle) { |
1149 |
+ |
UnmapViewOfFile(Mac2HostAddr(KERNEL_DATA_BASE & -allocation_granule)); |
1150 |
+ |
UnmapViewOfFile(Mac2HostAddr(KERNEL_DATA2_BASE & -allocation_granule)); |
1151 |
+ |
CloseHandle(kernel_handle); |
1152 |
+ |
} |
1153 |
+ |
#else |
1154 |
+ |
if (kernel_area >= 0) { |
1155 |
+ |
shmdt(Mac2HostAddr(KERNEL_DATA_BASE)); |
1156 |
+ |
shmdt(Mac2HostAddr(KERNEL_DATA2_BASE)); |
1157 |
+ |
shmctl(kernel_area, IPC_RMID, NULL); |
1158 |
+ |
} |
1159 |
+ |
#endif |
1160 |
+ |
} |
1161 |
+ |
|
1162 |
+ |
|
1163 |
+ |
/* |
1164 |
|
* Jump into Mac ROM, start 680x0 emulator |
1165 |
|
*/ |
1166 |
|
|
1167 |
|
#if EMULATED_PPC |
849 |
– |
extern void emul_ppc(uint32 start); |
850 |
– |
extern void init_emul_ppc(void); |
1168 |
|
void jump_to_rom(uint32 entry) |
1169 |
|
{ |
1170 |
|
init_emul_ppc(); |
1229 |
|
uint16 proc[2] = {trap, M68K_RTS}; |
1230 |
|
Execute68k((uint32)proc, r); |
1231 |
|
} |
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 |
– |
} |
1232 |
|
#endif |
1233 |
|
|
1234 |
|
|
1247 |
|
|
1248 |
|
|
1249 |
|
/* |
946 |
– |
* Pause/resume emulator |
947 |
– |
*/ |
948 |
– |
|
949 |
– |
void PauseEmulator(void) |
950 |
– |
{ |
951 |
– |
pthread_kill(emul_thread, SIGSTOP); |
952 |
– |
} |
953 |
– |
|
954 |
– |
void ResumeEmulator(void) |
955 |
– |
{ |
956 |
– |
pthread_kill(emul_thread, SIGCONT); |
957 |
– |
} |
958 |
– |
|
959 |
– |
|
960 |
– |
/* |
1250 |
|
* Dump 68k registers |
1251 |
|
*/ |
1252 |
|
|
1274 |
|
* Make code executable |
1275 |
|
*/ |
1276 |
|
|
1277 |
< |
void MakeExecutable(int dummy, void *start, uint32 length) |
1277 |
> |
void MakeExecutable(int dummy, uint32 start, uint32 length) |
1278 |
|
{ |
1279 |
< |
#if !EMULATED_PPC |
991 |
< |
if (((uint32)start >= ROM_BASE) && ((uint32)start < (ROM_BASE + ROM_SIZE))) |
1279 |
> |
if ((start >= ROM_BASE) && (start < (ROM_BASE + ROM_SIZE))) |
1280 |
|
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 |
– |
{ |
1281 |
|
#if EMULATED_PPC |
1282 |
< |
ExecuteNative(NATIVE_VIDEO_INSTALL_ACCEL); |
1282 |
> |
FlushCodeCache(start, start + length); |
1283 |
|
#else |
1284 |
< |
ExecutePPC(VideoInstallAccel); |
1284 |
> |
flush_icache_range(start, start + length); |
1285 |
|
#endif |
1009 |
– |
InstallExtFS(); |
1286 |
|
} |
1287 |
|
|
1288 |
|
|
1290 |
|
* NVRAM watchdog thread (saves NVRAM every minute) |
1291 |
|
*/ |
1292 |
|
|
1293 |
< |
static void *nvram_func(void *arg) |
1293 |
> |
static void nvram_watchdog(void) |
1294 |
|
{ |
1295 |
< |
struct timespec req = {60, 0}; // 1 minute |
1295 |
> |
if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) { |
1296 |
> |
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
1297 |
> |
SaveXPRAM(); |
1298 |
> |
} |
1299 |
> |
} |
1300 |
|
|
1301 |
< |
for (;;) { |
1302 |
< |
pthread_testcancel(); |
1303 |
< |
nanosleep(&req, NULL); |
1304 |
< |
pthread_testcancel(); |
1305 |
< |
if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) { |
1306 |
< |
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
1027 |
< |
SaveXPRAM(); |
1028 |
< |
} |
1301 |
> |
static void *nvram_func(void *arg) |
1302 |
> |
{ |
1303 |
> |
while (!nvram_thread_cancel) { |
1304 |
> |
for (int i=0; i<60 && !nvram_thread_cancel; i++) |
1305 |
> |
Delay_usec(999999); // Only wait 1 second so we quit promptly when nvram_thread_cancel becomes true |
1306 |
> |
nvram_watchdog(); |
1307 |
|
} |
1308 |
|
return NULL; |
1309 |
|
} |
1316 |
|
static void *tick_func(void *arg) |
1317 |
|
{ |
1318 |
|
int tick_counter = 0; |
1319 |
< |
struct timespec req = {0, 16625000}; |
1319 |
> |
uint64 start = GetTicks_usec(); |
1320 |
> |
int64 ticks = 0; |
1321 |
> |
uint64 next = GetTicks_usec(); |
1322 |
|
|
1323 |
< |
for (;;) { |
1323 |
> |
while (!tick_thread_cancel) { |
1324 |
|
|
1325 |
|
// Wait |
1326 |
< |
nanosleep(&req, NULL); |
1326 |
> |
next += 16625; |
1327 |
> |
int64 delay = next - GetTicks_usec(); |
1328 |
> |
if (delay > 0) |
1329 |
> |
Delay_usec(delay); |
1330 |
> |
else if (delay < -16625) |
1331 |
> |
next = GetTicks_usec(); |
1332 |
> |
ticks++; |
1333 |
|
|
1334 |
|
#if !EMULATED_PPC |
1335 |
|
// Did we crash? |
1336 |
|
if (emul_thread_fatal) { |
1337 |
|
|
1338 |
|
// Yes, dump registers |
1339 |
< |
pt_regs *r = (pt_regs *)&sigsegv_regs; |
1339 |
> |
sigregs *r = &sigsegv_regs; |
1340 |
|
char str[256]; |
1341 |
< |
sprintf(str, "SIGSEGV\n" |
1341 |
> |
if (crash_reason == NULL) |
1342 |
> |
crash_reason = "SIGSEGV"; |
1343 |
> |
sprintf(str, "%s\n" |
1344 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1345 |
|
" xer %08lx cr %08lx \n" |
1346 |
|
" r0 %08lx r1 %08lx r2 %08lx r3 %08lx\n" |
1351 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1352 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1353 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1354 |
+ |
crash_reason, |
1355 |
|
r->nip, r->link, r->ctr, r->msr, |
1356 |
|
r->xer, r->ccr, |
1357 |
|
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1387 |
|
TriggerInterrupt(); |
1388 |
|
} |
1389 |
|
} |
1390 |
+ |
|
1391 |
+ |
uint64 end = GetTicks_usec(); |
1392 |
+ |
D(bug("%lld ticks in %lld usec = %f ticks/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start))); |
1393 |
|
return NULL; |
1394 |
|
} |
1395 |
|
|
1400 |
|
|
1401 |
|
void Set_pthread_attr(pthread_attr_t *attr, int priority) |
1402 |
|
{ |
1403 |
< |
// nothing to do |
1403 |
> |
#ifdef HAVE_PTHREADS |
1404 |
> |
pthread_attr_init(attr); |
1405 |
> |
#if defined(_POSIX_THREAD_PRIORITY_SCHEDULING) |
1406 |
> |
// Some of these only work for superuser |
1407 |
> |
if (geteuid() == 0) { |
1408 |
> |
pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED); |
1409 |
> |
pthread_attr_setschedpolicy(attr, SCHED_FIFO); |
1410 |
> |
struct sched_param fifo_param; |
1411 |
> |
fifo_param.sched_priority = ((sched_get_priority_min(SCHED_FIFO) + |
1412 |
> |
sched_get_priority_max(SCHED_FIFO)) / 2 + |
1413 |
> |
priority); |
1414 |
> |
pthread_attr_setschedparam(attr, &fifo_param); |
1415 |
> |
} |
1416 |
> |
if (pthread_attr_setscope(attr, PTHREAD_SCOPE_SYSTEM) != 0) { |
1417 |
> |
#ifdef PTHREAD_SCOPE_BOUND_NP |
1418 |
> |
// If system scope is not available (eg. we're not running |
1419 |
> |
// with CAP_SCHED_MGT capability on an SGI box), try bound |
1420 |
> |
// scope. It exposes pthread scheduling to the kernel, |
1421 |
> |
// without setting realtime priority. |
1422 |
> |
pthread_attr_setscope(attr, PTHREAD_SCOPE_BOUND_NP); |
1423 |
> |
#endif |
1424 |
> |
} |
1425 |
> |
#endif |
1426 |
> |
#endif |
1427 |
|
} |
1428 |
|
|
1429 |
|
|
1439 |
|
pthread_mutexattr_init(&attr); |
1440 |
|
// Initialize the mutex for priority inheritance -- |
1441 |
|
// required for accurate timing. |
1442 |
< |
#ifdef HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL |
1442 |
> |
#if defined(HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL) && !defined(__CYGWIN__) |
1443 |
|
pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT); |
1444 |
|
#endif |
1445 |
|
#if defined(HAVE_PTHREAD_MUTEXATTR_SETTYPE) && defined(PTHREAD_MUTEX_NORMAL) |
1510 |
|
* Trigger signal USR2 from another thread |
1511 |
|
*/ |
1512 |
|
|
1513 |
< |
#if !EMULATED_PPC || ASYNC_IRQ |
1513 |
> |
#if !EMULATED_PPC |
1514 |
|
void TriggerInterrupt(void) |
1515 |
|
{ |
1516 |
< |
if (ready_for_signals) |
1516 |
> |
if (ready_for_signals) { |
1517 |
> |
idle_resume(); |
1518 |
|
pthread_kill(emul_thread, SIGUSR2); |
1519 |
+ |
} |
1520 |
|
} |
1521 |
|
#endif |
1522 |
|
|
1544 |
|
|
1545 |
|
void DisableInterrupt(void) |
1546 |
|
{ |
1547 |
+ |
#if EMULATED_PPC |
1548 |
+ |
WriteMacInt32(XLM_IRQ_NEST, int32(ReadMacInt32(XLM_IRQ_NEST)) + 1); |
1549 |
+ |
#else |
1550 |
|
atomic_add((int *)XLM_IRQ_NEST, 1); |
1551 |
+ |
#endif |
1552 |
|
} |
1553 |
|
|
1554 |
|
|
1558 |
|
|
1559 |
|
void EnableInterrupt(void) |
1560 |
|
{ |
1561 |
+ |
#if EMULATED_PPC |
1562 |
+ |
WriteMacInt32(XLM_IRQ_NEST, int32(ReadMacInt32(XLM_IRQ_NEST)) - 1); |
1563 |
+ |
#else |
1564 |
|
atomic_add((int *)XLM_IRQ_NEST, -1); |
1565 |
+ |
#endif |
1566 |
|
} |
1567 |
|
|
1568 |
|
|
1570 |
|
* USR2 handler |
1571 |
|
*/ |
1572 |
|
|
1573 |
< |
#if EMULATED_PPC |
1574 |
< |
static void sigusr2_handler(int sig) |
1573 |
> |
#if !EMULATED_PPC |
1574 |
> |
void sigusr2_handler(int sig, siginfo_t *sip, void *scp) |
1575 |
|
{ |
1576 |
< |
#if ASYNC_IRQ |
1577 |
< |
extern void HandleInterrupt(void); |
1578 |
< |
HandleInterrupt(); |
1576 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1577 |
> |
|
1578 |
> |
#ifdef USE_SDL_VIDEO |
1579 |
> |
// We must fill in the events queue in the same thread that did call SDL_SetVideoMode() |
1580 |
> |
SDL_PumpEvents(); |
1581 |
|
#endif |
1255 |
– |
} |
1256 |
– |
#else |
1257 |
– |
static void sigusr2_handler(int sig, sigcontext_struct *sc) |
1258 |
– |
{ |
1259 |
– |
pt_regs *r = sc->regs; |
1582 |
|
|
1583 |
|
// Do nothing if interrupts are disabled |
1584 |
|
if (*(int32 *)XLM_IRQ_NEST > 0) |
1585 |
|
return; |
1586 |
|
|
1587 |
+ |
#ifdef SYSTEM_CLOBBERS_R2 |
1588 |
+ |
// Restore pointer to Thread Local Storage |
1589 |
+ |
set_r2(TOC); |
1590 |
+ |
#endif |
1591 |
+ |
#ifdef SYSTEM_CLOBBERS_R13 |
1592 |
+ |
// Restore pointer to .sdata section |
1593 |
+ |
set_r13(R13); |
1594 |
+ |
#endif |
1595 |
+ |
|
1596 |
|
// Disable MacOS stack sniffer |
1597 |
|
WriteMacInt32(0x110, 0); |
1598 |
|
|
1601 |
|
case MODE_68K: |
1602 |
|
// 68k emulator active, trigger 68k interrupt level 1 |
1603 |
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1604 |
< |
r->ccr |= ntohl(kernel_data->v[0x674 >> 2]); |
1604 |
> |
r->cr() |= ntohl(kernel_data->v[0x674 >> 2]); |
1605 |
|
break; |
1606 |
|
|
1607 |
|
#if INTERRUPTS_IN_NATIVE_MODE |
1608 |
|
case MODE_NATIVE: |
1609 |
|
// 68k emulator inactive, in nanokernel? |
1610 |
< |
if (r->gpr[1] != KernelDataAddr) { |
1610 |
> |
if (r->gpr(1) != KernelDataAddr) { |
1611 |
> |
|
1612 |
> |
// Set extra stack for SIGSEGV handler |
1613 |
> |
sigaltstack(&extra_stack, NULL); |
1614 |
> |
|
1615 |
|
// Prepare for 68k interrupt level 1 |
1616 |
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1617 |
|
WriteMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc, ReadMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc) | ntohl(kernel_data->v[0x674 >> 2])); |
1618 |
|
|
1619 |
|
// Execute nanokernel interrupt routine (this will activate the 68k emulator) |
1620 |
< |
atomic_add((int32 *)XLM_IRQ_NEST, 1); |
1620 |
> |
DisableInterrupt(); |
1621 |
|
if (ROMType == ROMTYPE_NEWWORLD) |
1622 |
|
ppc_interrupt(ROM_BASE + 0x312b1c, KernelDataAddr); |
1623 |
|
else |
1624 |
|
ppc_interrupt(ROM_BASE + 0x312a3c, KernelDataAddr); |
1625 |
+ |
|
1626 |
+ |
// Reset normal stack |
1627 |
+ |
sigaltstack(&sig_stack, NULL); |
1628 |
|
} |
1629 |
|
break; |
1630 |
|
#endif |
1635 |
|
if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) { |
1636 |
|
|
1637 |
|
// Set extra stack for SIGSEGV handler |
1638 |
< |
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); |
1638 |
> |
sigaltstack(&extra_stack, NULL); |
1639 |
|
#if 1 |
1640 |
|
// Execute full 68k interrupt routine |
1641 |
|
M68kRegisters r; |
1657 |
|
if (InterruptFlags & INTFLAG_VIA) { |
1658 |
|
ClearInterruptFlag(INTFLAG_VIA); |
1659 |
|
ADBInterrupt(); |
1660 |
< |
ExecutePPC(VideoVBL); |
1660 |
> |
ExecuteNative(NATIVE_VIDEO_VBL); |
1661 |
|
} |
1662 |
|
} |
1663 |
|
#endif |
1664 |
< |
// Reset normal signal stack |
1665 |
< |
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); |
1664 |
> |
// Reset normal stack |
1665 |
> |
sigaltstack(&sig_stack, NULL); |
1666 |
|
} |
1667 |
|
break; |
1668 |
|
#endif |
1676 |
|
*/ |
1677 |
|
|
1678 |
|
#if !EMULATED_PPC |
1679 |
< |
static void sigsegv_handler(int sig, sigcontext_struct *sc) |
1679 |
> |
static void sigsegv_handler(int sig, siginfo_t *sip, void *scp) |
1680 |
|
{ |
1681 |
< |
pt_regs *r = sc->regs; |
1681 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1682 |
|
|
1683 |
|
// Get effective address |
1684 |
< |
uint32 addr = r->dar; |
1684 |
> |
uint32 addr = r->dar(); |
1685 |
|
|
1686 |
+ |
#ifdef SYSTEM_CLOBBERS_R2 |
1687 |
+ |
// Restore pointer to Thread Local Storage |
1688 |
+ |
set_r2(TOC); |
1689 |
+ |
#endif |
1690 |
+ |
#ifdef SYSTEM_CLOBBERS_R13 |
1691 |
+ |
// Restore pointer to .sdata section |
1692 |
+ |
set_r13(R13); |
1693 |
+ |
#endif |
1694 |
+ |
|
1695 |
|
#if ENABLE_VOSF |
1696 |
|
// Handle screen fault. |
1697 |
|
extern bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction); |
1698 |
< |
if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->nip)) |
1698 |
> |
if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->pc())) |
1699 |
|
return; |
1700 |
|
#endif |
1701 |
|
|
1702 |
|
num_segv++; |
1703 |
|
|
1704 |
< |
// Fault in Mac ROM or RAM? |
1705 |
< |
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
1704 |
> |
// Fault in Mac ROM or RAM or DR Cache? |
1705 |
> |
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)); |
1706 |
|
if (mac_fault) { |
1707 |
|
|
1708 |
|
// "VM settings" during MacOS 8 installation |
1709 |
< |
if (r->nip == ROM_BASE + 0x488160 && r->gpr[20] == 0xf8000000) { |
1710 |
< |
r->nip += 4; |
1711 |
< |
r->gpr[8] = 0; |
1709 |
> |
if (r->pc() == ROM_BASE + 0x488160 && r->gpr(20) == 0xf8000000) { |
1710 |
> |
r->pc() += 4; |
1711 |
> |
r->gpr(8) = 0; |
1712 |
|
return; |
1713 |
|
|
1714 |
|
// MacOS 8.5 installation |
1715 |
< |
} else if (r->nip == ROM_BASE + 0x488140 && r->gpr[16] == 0xf8000000) { |
1716 |
< |
r->nip += 4; |
1717 |
< |
r->gpr[8] = 0; |
1715 |
> |
} else if (r->pc() == ROM_BASE + 0x488140 && r->gpr(16) == 0xf8000000) { |
1716 |
> |
r->pc() += 4; |
1717 |
> |
r->gpr(8) = 0; |
1718 |
|
return; |
1719 |
|
|
1720 |
|
// MacOS 8 serial drivers on startup |
1721 |
< |
} else if (r->nip == ROM_BASE + 0x48e080 && (r->gpr[8] == 0xf3012002 || r->gpr[8] == 0xf3012000)) { |
1722 |
< |
r->nip += 4; |
1723 |
< |
r->gpr[8] = 0; |
1721 |
> |
} else if (r->pc() == ROM_BASE + 0x48e080 && (r->gpr(8) == 0xf3012002 || r->gpr(8) == 0xf3012000)) { |
1722 |
> |
r->pc() += 4; |
1723 |
> |
r->gpr(8) = 0; |
1724 |
|
return; |
1725 |
|
|
1726 |
|
// MacOS 8.1 serial drivers on startup |
1727 |
< |
} else if (r->nip == ROM_BASE + 0x48c5e0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1728 |
< |
r->nip += 4; |
1727 |
> |
} else if (r->pc() == ROM_BASE + 0x48c5e0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1728 |
> |
r->pc() += 4; |
1729 |
> |
return; |
1730 |
> |
} else if (r->pc() == ROM_BASE + 0x4a10a0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1731 |
> |
r->pc() += 4; |
1732 |
|
return; |
1733 |
< |
} else if (r->nip == ROM_BASE + 0x4a10a0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1734 |
< |
r->nip += 4; |
1733 |
> |
|
1734 |
> |
// MacOS 8.6 serial drivers on startup (with DR Cache and OldWorld ROM) |
1735 |
> |
} else if ((r->pc() - DR_CACHE_BASE) < DR_CACHE_SIZE && (r->gpr(16) == 0xf3012002 || r->gpr(16) == 0xf3012000)) { |
1736 |
> |
r->pc() += 4; |
1737 |
> |
return; |
1738 |
> |
} else if ((r->pc() - DR_CACHE_BASE) < DR_CACHE_SIZE && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1739 |
> |
r->pc() += 4; |
1740 |
|
return; |
1741 |
|
} |
1742 |
|
|
1743 |
|
// Get opcode and divide into fields |
1744 |
< |
uint32 opcode = *((uint32 *)r->nip); |
1744 |
> |
uint32 opcode = *((uint32 *)r->pc()); |
1745 |
|
uint32 primop = opcode >> 26; |
1746 |
|
uint32 exop = (opcode >> 1) & 0x3ff; |
1747 |
|
uint32 ra = (opcode >> 16) & 0x1f; |
1830 |
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_NORM; break; |
1831 |
|
case 45: // sthu |
1832 |
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_U; break; |
1833 |
+ |
#if EMULATE_UNALIGNED_LOADSTORE_MULTIPLE |
1834 |
+ |
case 46: // lmw |
1835 |
+ |
if ((addr % 4) != 0) { |
1836 |
+ |
uint32 ea = addr; |
1837 |
+ |
D(bug("WARNING: unaligned lmw to EA=%08x from IP=%08x\n", ea, r->pc())); |
1838 |
+ |
for (int i = rd; i <= 31; i++) { |
1839 |
+ |
r->gpr(i) = ReadMacInt32(ea); |
1840 |
+ |
ea += 4; |
1841 |
+ |
} |
1842 |
+ |
r->pc() += 4; |
1843 |
+ |
goto rti; |
1844 |
+ |
} |
1845 |
+ |
break; |
1846 |
+ |
case 47: // stmw |
1847 |
+ |
if ((addr % 4) != 0) { |
1848 |
+ |
uint32 ea = addr; |
1849 |
+ |
D(bug("WARNING: unaligned stmw to EA=%08x from IP=%08x\n", ea, r->pc())); |
1850 |
+ |
for (int i = rd; i <= 31; i++) { |
1851 |
+ |
WriteMacInt32(ea, r->gpr(i)); |
1852 |
+ |
ea += 4; |
1853 |
+ |
} |
1854 |
+ |
r->pc() += 4; |
1855 |
+ |
goto rti; |
1856 |
+ |
} |
1857 |
+ |
break; |
1858 |
+ |
#endif |
1859 |
|
} |
1860 |
|
|
1861 |
< |
// Ignore ROM writes |
1862 |
< |
if (transfer_type == TYPE_STORE && addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) { |
1863 |
< |
// 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)); |
1861 |
> |
// Ignore ROM writes (including to the zero page, which is read-only) |
1862 |
> |
if (transfer_type == TYPE_STORE && |
1863 |
> |
((addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) || |
1864 |
> |
(addr >= SheepMem::ZeroPage() && addr < SheepMem::ZeroPage() + SheepMem::PageSize()))) { |
1865 |
> |
// 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())); |
1866 |
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1867 |
< |
r->gpr[ra] = addr; |
1868 |
< |
r->nip += 4; |
1867 |
> |
r->gpr(ra) = addr; |
1868 |
> |
r->pc() += 4; |
1869 |
|
goto rti; |
1870 |
|
} |
1871 |
|
|
1872 |
|
// Ignore illegal memory accesses? |
1873 |
|
if (PrefsFindBool("ignoresegv")) { |
1874 |
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1875 |
< |
r->gpr[ra] = addr; |
1875 |
> |
r->gpr(ra) = addr; |
1876 |
|
if (transfer_type == TYPE_LOAD) |
1877 |
< |
r->gpr[rd] = 0; |
1878 |
< |
r->nip += 4; |
1877 |
> |
r->gpr(rd) = 0; |
1878 |
> |
r->pc() += 4; |
1879 |
|
goto rti; |
1880 |
|
} |
1881 |
|
|
1883 |
|
if (!PrefsFindBool("nogui")) { |
1884 |
|
char str[256]; |
1885 |
|
if (transfer_type == TYPE_LOAD || transfer_type == TYPE_STORE) |
1886 |
< |
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]); |
1886 |
> |
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)); |
1887 |
|
else |
1888 |
< |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode); |
1888 |
> |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode); |
1889 |
|
ErrorAlert(str); |
1890 |
|
QuitEmulator(); |
1891 |
|
return; |
1893 |
|
} |
1894 |
|
|
1895 |
|
// For all other errors, jump into debugger (sort of...) |
1896 |
+ |
crash_reason = (sig == SIGBUS) ? "SIGBUS" : "SIGSEGV"; |
1897 |
|
if (!ready_for_signals) { |
1898 |
< |
printf("SIGSEGV\n"); |
1899 |
< |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
1898 |
> |
printf("%s\n"); |
1899 |
> |
printf(" sigcontext %p, machine_regs %p\n", scp, r); |
1900 |
|
printf( |
1901 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1902 |
|
" xer %08lx cr %08lx \n" |
1908 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1909 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1910 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1911 |
< |
r->nip, r->link, r->ctr, r->msr, |
1912 |
< |
r->xer, r->ccr, |
1913 |
< |
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1914 |
< |
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
1915 |
< |
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
1916 |
< |
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
1917 |
< |
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
1918 |
< |
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
1919 |
< |
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
1920 |
< |
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
1911 |
> |
crash_reason, |
1912 |
> |
r->pc(), r->lr(), r->ctr(), r->msr(), |
1913 |
> |
r->xer(), r->cr(), |
1914 |
> |
r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3), |
1915 |
> |
r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7), |
1916 |
> |
r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11), |
1917 |
> |
r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15), |
1918 |
> |
r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19), |
1919 |
> |
r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23), |
1920 |
> |
r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27), |
1921 |
> |
r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31)); |
1922 |
|
exit(1); |
1923 |
|
QuitEmulator(); |
1924 |
|
return; |
1925 |
|
} else { |
1926 |
|
// We crashed. Save registers, tell tick thread and loop forever |
1927 |
< |
sigsegv_regs = *(sigregs *)r; |
1927 |
> |
build_sigregs(&sigsegv_regs, r); |
1928 |
|
emul_thread_fatal = true; |
1929 |
|
for (;;) ; |
1930 |
|
} |
1936 |
|
* SIGILL handler |
1937 |
|
*/ |
1938 |
|
|
1939 |
< |
static void sigill_handler(int sig, sigcontext_struct *sc) |
1939 |
> |
static void sigill_handler(int sig, siginfo_t *sip, void *scp) |
1940 |
|
{ |
1941 |
< |
pt_regs *r = sc->regs; |
1941 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1942 |
|
char str[256]; |
1943 |
|
|
1944 |
+ |
#ifdef SYSTEM_CLOBBERS_R2 |
1945 |
+ |
// Restore pointer to Thread Local Storage |
1946 |
+ |
set_r2(TOC); |
1947 |
+ |
#endif |
1948 |
+ |
#ifdef SYSTEM_CLOBBERS_R13 |
1949 |
+ |
// Restore pointer to .sdata section |
1950 |
+ |
set_r13(R13); |
1951 |
+ |
#endif |
1952 |
+ |
|
1953 |
|
// Fault in Mac ROM or RAM? |
1954 |
< |
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
1954 |
> |
bool mac_fault = (r->pc() >= ROM_BASE) && (r->pc() < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize)); |
1955 |
|
if (mac_fault) { |
1956 |
|
|
1957 |
|
// Get opcode and divide into fields |
1958 |
< |
uint32 opcode = *((uint32 *)r->nip); |
1958 |
> |
uint32 opcode = *((uint32 *)r->pc()); |
1959 |
|
uint32 primop = opcode >> 26; |
1960 |
|
uint32 exop = (opcode >> 1) & 0x3ff; |
1961 |
|
uint32 ra = (opcode >> 16) & 0x1f; |
1966 |
|
switch (primop) { |
1967 |
|
case 9: // POWER instructions |
1968 |
|
case 22: |
1969 |
< |
power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->nip, r->gpr[1], opcode); |
1969 |
> |
power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->pc(), r->gpr(1), opcode); |
1970 |
|
ErrorAlert(str); |
1971 |
|
QuitEmulator(); |
1972 |
|
return; |
1974 |
|
case 31: |
1975 |
|
switch (exop) { |
1976 |
|
case 83: // mfmsr |
1977 |
< |
r->gpr[rd] = 0xf072; |
1978 |
< |
r->nip += 4; |
1977 |
> |
r->gpr(rd) = 0xf072; |
1978 |
> |
r->pc() += 4; |
1979 |
|
goto rti; |
1980 |
|
|
1981 |
|
case 210: // mtsr |
1982 |
|
case 242: // mtsrin |
1983 |
|
case 306: // tlbie |
1984 |
< |
r->nip += 4; |
1984 |
> |
r->pc() += 4; |
1985 |
|
goto rti; |
1986 |
|
|
1987 |
|
case 339: { // mfspr |
1997 |
|
case 957: // PMC3 |
1998 |
|
case 958: // PMC4 |
1999 |
|
case 959: // SDA |
2000 |
< |
r->nip += 4; |
2000 |
> |
r->pc() += 4; |
2001 |
|
goto rti; |
2002 |
|
case 25: // SDR1 |
2003 |
< |
r->gpr[rd] = 0xdead001f; |
2004 |
< |
r->nip += 4; |
2003 |
> |
r->gpr(rd) = 0xdead001f; |
2004 |
> |
r->pc() += 4; |
2005 |
|
goto rti; |
2006 |
|
case 287: // PVR |
2007 |
< |
r->gpr[rd] = PVR; |
2008 |
< |
r->nip += 4; |
2007 |
> |
r->gpr(rd) = PVR; |
2008 |
> |
r->pc() += 4; |
2009 |
|
goto rti; |
2010 |
|
} |
2011 |
|
break; |
2041 |
|
case 957: // PMC3 |
2042 |
|
case 958: // PMC4 |
2043 |
|
case 959: // SDA |
2044 |
< |
r->nip += 4; |
2044 |
> |
r->pc() += 4; |
2045 |
|
goto rti; |
2046 |
|
} |
2047 |
|
break; |
2060 |
|
|
2061 |
|
// In GUI mode, show error alert |
2062 |
|
if (!PrefsFindBool("nogui")) { |
2063 |
< |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode); |
2063 |
> |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode); |
2064 |
|
ErrorAlert(str); |
2065 |
|
QuitEmulator(); |
2066 |
|
return; |
2068 |
|
} |
2069 |
|
|
2070 |
|
// For all other errors, jump into debugger (sort of...) |
2071 |
+ |
crash_reason = "SIGILL"; |
2072 |
|
if (!ready_for_signals) { |
2073 |
< |
printf("SIGILL\n"); |
2074 |
< |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
2073 |
> |
printf("%s\n"); |
2074 |
> |
printf(" sigcontext %p, machine_regs %p\n", scp, r); |
2075 |
|
printf( |
2076 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
2077 |
|
" xer %08lx cr %08lx \n" |
2083 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
2084 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
2085 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
2086 |
< |
r->nip, r->link, r->ctr, r->msr, |
2087 |
< |
r->xer, r->ccr, |
2088 |
< |
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
2089 |
< |
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
2090 |
< |
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
2091 |
< |
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
2092 |
< |
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
2093 |
< |
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
2094 |
< |
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
2095 |
< |
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
2086 |
> |
crash_reason, |
2087 |
> |
r->pc(), r->lr(), r->ctr(), r->msr(), |
2088 |
> |
r->xer(), r->cr(), |
2089 |
> |
r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3), |
2090 |
> |
r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7), |
2091 |
> |
r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11), |
2092 |
> |
r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15), |
2093 |
> |
r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19), |
2094 |
> |
r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23), |
2095 |
> |
r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27), |
2096 |
> |
r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31)); |
2097 |
|
exit(1); |
2098 |
|
QuitEmulator(); |
2099 |
|
return; |
2100 |
|
} else { |
2101 |
|
// We crashed. Save registers, tell tick thread and loop forever |
2102 |
< |
sigsegv_regs = *(sigregs *)r; |
2102 |
> |
build_sigregs(&sigsegv_regs, r); |
2103 |
|
emul_thread_fatal = true; |
2104 |
|
for (;;) ; |
2105 |
|
} |
2109 |
|
|
2110 |
|
|
2111 |
|
/* |
2112 |
+ |
* Helpers to share 32-bit addressable data with MacOS |
2113 |
+ |
*/ |
2114 |
+ |
|
2115 |
+ |
bool SheepMem::Init(void) |
2116 |
+ |
{ |
2117 |
+ |
// Size of a native page |
2118 |
+ |
page_size = getpagesize(); |
2119 |
+ |
|
2120 |
+ |
// Allocate SheepShaver globals |
2121 |
+ |
proc = base; |
2122 |
+ |
if (vm_mac_acquire(base, size) < 0) |
2123 |
+ |
return false; |
2124 |
+ |
|
2125 |
+ |
// Allocate page with all bits set to 0, right in the middle |
2126 |
+ |
// This is also used to catch undesired overlaps between proc and data areas |
2127 |
+ |
zero_page = proc + (size / 2); |
2128 |
+ |
Mac_memset(zero_page, 0, page_size); |
2129 |
+ |
if (vm_protect(Mac2HostAddr(zero_page), page_size, VM_PAGE_READ) < 0) |
2130 |
+ |
return false; |
2131 |
+ |
|
2132 |
+ |
#if EMULATED_PPC |
2133 |
+ |
// Allocate alternate stack for PowerPC interrupt routine |
2134 |
+ |
sig_stack = base + size; |
2135 |
+ |
if (vm_mac_acquire(sig_stack, SIG_STACK_SIZE) < 0) |
2136 |
+ |
return false; |
2137 |
+ |
#endif |
2138 |
+ |
|
2139 |
+ |
data = base + size; |
2140 |
+ |
return true; |
2141 |
+ |
} |
2142 |
+ |
|
2143 |
+ |
void SheepMem::Exit(void) |
2144 |
+ |
{ |
2145 |
+ |
if (data) { |
2146 |
+ |
// Delete SheepShaver globals |
2147 |
+ |
vm_mac_release(base, size); |
2148 |
+ |
|
2149 |
+ |
#if EMULATED_PPC |
2150 |
+ |
// Delete alternate stack for PowerPC interrupt routine |
2151 |
+ |
vm_mac_release(sig_stack, SIG_STACK_SIZE); |
2152 |
+ |
#endif |
2153 |
+ |
} |
2154 |
+ |
} |
2155 |
+ |
|
2156 |
+ |
|
2157 |
+ |
/* |
2158 |
|
* Display alert |
2159 |
|
*/ |
2160 |
|
|
2203 |
|
|
2204 |
|
void ErrorAlert(const char *text) |
2205 |
|
{ |
2206 |
< |
#ifdef ENABLE_GTK |
2206 |
> |
#if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO) |
2207 |
|
if (PrefsFindBool("nogui") || x_display == NULL) { |
2208 |
|
printf(GetString(STR_SHELL_ERROR_PREFIX), text); |
2209 |
|
return; |
2222 |
|
|
2223 |
|
void WarningAlert(const char *text) |
2224 |
|
{ |
2225 |
< |
#ifdef ENABLE_GTK |
2225 |
> |
#if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO) |
2226 |
|
if (PrefsFindBool("nogui") || x_display == NULL) { |
2227 |
|
printf(GetString(STR_SHELL_WARNING_PREFIX), text); |
2228 |
|
return; |