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 |
+ |
* There is apparently another problem when processing signals. In |
71 |
+ |
* fullscreen mode, we get quick updates of the mouse position. This |
72 |
+ |
* causes an increased number of calls to TriggerInterrupt(). And, |
73 |
+ |
* since IRQ_NEST is not fully handled atomically, nested calls to |
74 |
+ |
* ppc_interrupt() may cause stack corruption to eventually crash the |
75 |
+ |
* emulator. |
76 |
+ |
* |
77 |
+ |
* FIXME: |
78 |
+ |
* The current solution is to allocate another signal stack when |
79 |
+ |
* processing ppc_interrupt(). However, it may be better to detect |
80 |
+ |
* the INTFLAG_ADB case and handle it specifically with some extra mutex? |
81 |
+ |
* |
82 |
|
* TODO: |
83 |
|
* check if SIGSEGV handler works for all registers (including FP!) |
84 |
|
*/ |
107 |
|
#include "xpram.h" |
108 |
|
#include "timer.h" |
109 |
|
#include "adb.h" |
96 |
– |
#include "sony.h" |
97 |
– |
#include "disk.h" |
98 |
– |
#include "cdrom.h" |
99 |
– |
#include "scsi.h" |
110 |
|
#include "video.h" |
101 |
– |
#include "audio.h" |
102 |
– |
#include "ether.h" |
103 |
– |
#include "serial.h" |
104 |
– |
#include "clip.h" |
105 |
– |
#include "extfs.h" |
111 |
|
#include "sys.h" |
112 |
|
#include "macos_util.h" |
113 |
|
#include "rom_patches.h" |
114 |
|
#include "user_strings.h" |
115 |
+ |
#include "vm_alloc.h" |
116 |
+ |
#include "sigsegv.h" |
117 |
|
|
118 |
|
#define DEBUG 0 |
119 |
|
#include "debug.h" |
120 |
|
|
121 |
|
|
122 |
+ |
#ifdef HAVE_DIRENT_H |
123 |
+ |
#include <dirent.h> |
124 |
+ |
#endif |
125 |
+ |
|
126 |
+ |
#ifdef USE_SDL |
127 |
+ |
#include <SDL.h> |
128 |
+ |
#endif |
129 |
+ |
|
130 |
+ |
#ifndef USE_SDL_VIDEO |
131 |
|
#include <X11/Xlib.h> |
132 |
+ |
#endif |
133 |
|
|
134 |
|
#ifdef ENABLE_GTK |
135 |
|
#include <gtk/gtk.h> |
146 |
|
#endif |
147 |
|
|
148 |
|
|
149 |
+ |
// Enable emulation of unaligned lmw/stmw? |
150 |
+ |
#define EMULATE_UNALIGNED_LOADSTORE_MULTIPLE 1 |
151 |
+ |
|
152 |
|
// Enable Execute68k() safety checks? |
153 |
|
#define SAFE_EXEC_68K 0 |
154 |
|
|
158 |
|
// Interrupts in native mode? |
159 |
|
#define INTERRUPTS_IN_NATIVE_MODE 1 |
160 |
|
|
161 |
+ |
// Number of alternate stacks for signal handlers? |
162 |
+ |
#define SIG_STACK_COUNT 4 |
163 |
+ |
|
164 |
|
|
165 |
|
// Constants |
166 |
|
const char ROM_FILE_NAME[] = "ROM"; |
167 |
|
const char ROM_FILE_NAME2[] = "Mac OS ROM"; |
168 |
|
|
169 |
< |
const uint32 ROM_AREA_SIZE = 0x500000; // Size of ROM area |
170 |
< |
const uint32 ROM_END = ROM_BASE + ROM_SIZE; // End of ROM |
171 |
< |
|
172 |
< |
const uint32 KERNEL_DATA_BASE = 0x68ffe000; // Address of Kernel Data |
173 |
< |
const uint32 KERNEL_DATA2_BASE = 0x5fffe000; // Alternate address of Kernel Data |
174 |
< |
const uint32 KERNEL_AREA_SIZE = 0x2000; // Size of Kernel Data area |
152 |
< |
|
169 |
> |
#if REAL_ADDRESSING |
170 |
> |
const uintptr RAM_BASE = 0x20000000; // Base address of RAM |
171 |
> |
#else |
172 |
> |
// FIXME: needs to be >= 0x04000000 |
173 |
> |
const uintptr RAM_BASE = 0x10000000; // Base address of RAM |
174 |
> |
#endif |
175 |
|
const uint32 SIG_STACK_SIZE = 0x10000; // Size of signal stack |
176 |
|
|
177 |
|
|
178 |
< |
// 68k Emulator Data |
179 |
< |
struct EmulatorData { |
180 |
< |
uint32 v[0x400]; |
178 |
> |
#if !EMULATED_PPC |
179 |
> |
struct sigregs { |
180 |
> |
uint32 nip; |
181 |
> |
uint32 link; |
182 |
> |
uint32 ctr; |
183 |
> |
uint32 msr; |
184 |
> |
uint32 xer; |
185 |
> |
uint32 ccr; |
186 |
> |
uint32 gpr[32]; |
187 |
> |
}; |
188 |
> |
|
189 |
> |
#if defined(__linux__) |
190 |
> |
#include <sys/ucontext.h> |
191 |
> |
#define MACHINE_REGISTERS(scp) ((machine_regs *)(((ucontext_t *)scp)->uc_mcontext.regs)) |
192 |
> |
|
193 |
> |
struct machine_regs : public pt_regs |
194 |
> |
{ |
195 |
> |
u_long & cr() { return pt_regs::ccr; } |
196 |
> |
uint32 cr() const { return pt_regs::ccr; } |
197 |
> |
uint32 lr() const { return pt_regs::link; } |
198 |
> |
uint32 ctr() const { return pt_regs::ctr; } |
199 |
> |
uint32 xer() const { return pt_regs::xer; } |
200 |
> |
uint32 msr() const { return pt_regs::msr; } |
201 |
> |
uint32 dar() const { return pt_regs::dar; } |
202 |
> |
u_long & pc() { return pt_regs::nip; } |
203 |
> |
uint32 pc() const { return pt_regs::nip; } |
204 |
> |
u_long & gpr(int i) { return pt_regs::gpr[i]; } |
205 |
> |
uint32 gpr(int i) const { return pt_regs::gpr[i]; } |
206 |
|
}; |
207 |
+ |
#endif |
208 |
|
|
209 |
+ |
#if defined(__NetBSD__) |
210 |
+ |
#include <sys/ucontext.h> |
211 |
+ |
#define MACHINE_REGISTERS(scp) ((machine_regs *)&(((ucontext_t *)scp)->uc_mcontext)) |
212 |
+ |
|
213 |
+ |
struct machine_regs : public mcontext_t |
214 |
+ |
{ |
215 |
+ |
long & cr() { return __gregs[_REG_CR]; } |
216 |
+ |
uint32 cr() const { return __gregs[_REG_CR]; } |
217 |
+ |
uint32 lr() const { return __gregs[_REG_LR]; } |
218 |
+ |
uint32 ctr() const { return __gregs[_REG_CTR]; } |
219 |
+ |
uint32 xer() const { return __gregs[_REG_XER]; } |
220 |
+ |
uint32 msr() const { return __gregs[_REG_MSR]; } |
221 |
+ |
uint32 dar() const { return (uint32)(((siginfo_t *)(((unsigned long)this) - offsetof(ucontext_t, uc_mcontext))) - 1)->si_addr; } /* HACK */ |
222 |
+ |
long & pc() { return __gregs[_REG_PC]; } |
223 |
+ |
uint32 pc() const { return __gregs[_REG_PC]; } |
224 |
+ |
long & gpr(int i) { return __gregs[_REG_R0 + i]; } |
225 |
+ |
uint32 gpr(int i) const { return __gregs[_REG_R0 + i]; } |
226 |
+ |
}; |
227 |
+ |
#endif |
228 |
|
|
229 |
< |
// Kernel Data |
230 |
< |
struct KernelData { |
231 |
< |
uint32 v[0x400]; |
232 |
< |
EmulatorData ed; |
229 |
> |
#if defined(__APPLE__) && defined(__MACH__) |
230 |
> |
#include <sys/signal.h> |
231 |
> |
extern "C" int sigaltstack(const struct sigaltstack *ss, struct sigaltstack *oss); |
232 |
> |
|
233 |
> |
#include <sys/ucontext.h> |
234 |
> |
#define MACHINE_REGISTERS(scp) ((machine_regs *)(((ucontext_t *)scp)->uc_mcontext)) |
235 |
> |
|
236 |
> |
struct machine_regs : public mcontext |
237 |
> |
{ |
238 |
> |
uint32 & cr() { return ss.cr; } |
239 |
> |
uint32 cr() const { return ss.cr; } |
240 |
> |
uint32 lr() const { return ss.lr; } |
241 |
> |
uint32 ctr() const { return ss.ctr; } |
242 |
> |
uint32 xer() const { return ss.xer; } |
243 |
> |
uint32 msr() const { return ss.srr1; } |
244 |
> |
uint32 dar() const { return es.dar; } |
245 |
> |
uint32 & pc() { return ss.srr0; } |
246 |
> |
uint32 pc() const { return ss.srr0; } |
247 |
> |
uint32 & gpr(int i) { return (&ss.r0)[i]; } |
248 |
> |
uint32 gpr(int i) const { return (&ss.r0)[i]; } |
249 |
|
}; |
250 |
+ |
#endif |
251 |
|
|
252 |
+ |
static void build_sigregs(sigregs *srp, machine_regs *mrp) |
253 |
+ |
{ |
254 |
+ |
srp->nip = mrp->pc(); |
255 |
+ |
srp->link = mrp->lr(); |
256 |
+ |
srp->ctr = mrp->ctr(); |
257 |
+ |
srp->msr = mrp->msr(); |
258 |
+ |
srp->xer = mrp->xer(); |
259 |
+ |
srp->ccr = mrp->cr(); |
260 |
+ |
for (int i = 0; i < 32; i++) |
261 |
+ |
srp->gpr[i] = mrp->gpr(i); |
262 |
+ |
} |
263 |
|
|
264 |
< |
#if !EMULATED_PPC |
265 |
< |
// Structure in which registers are saved in a signal handler; |
171 |
< |
// sigcontext->regs points to it |
172 |
< |
// (see arch/ppc/kernel/signal.c) |
173 |
< |
typedef struct { |
174 |
< |
uint32 u[4]; |
175 |
< |
} __attribute((aligned(16))) vector128; |
176 |
< |
#include <linux/elf.h> |
264 |
> |
static struct sigaltstack sig_stacks[SIG_STACK_COUNT]; // Stacks for signal handlers |
265 |
> |
static int sig_stack_id = 0; // Stack slot currently used |
266 |
|
|
267 |
< |
struct sigregs { |
268 |
< |
elf_gregset_t gp_regs; // Identical to pt_regs |
269 |
< |
double fp_regs[ELF_NFPREG]; // f0..f31 and fpsrc |
270 |
< |
//more (uninteresting) stuff following here |
271 |
< |
}; |
267 |
> |
static inline int sig_stack_acquire(void) |
268 |
> |
{ |
269 |
> |
if (sig_stack_id >= SIG_STACK_COUNT) { |
270 |
> |
printf("FATAL: signal stack overflow\n"); |
271 |
> |
return -1; |
272 |
> |
} |
273 |
> |
return sigaltstack(&sig_stacks[sig_stack_id++], NULL); |
274 |
> |
} |
275 |
> |
|
276 |
> |
static inline int sig_stack_release(void) |
277 |
> |
{ |
278 |
> |
if (sig_stack_id <= 0) { |
279 |
> |
printf("FATAL: signal stack underflow\n"); |
280 |
> |
return -1; |
281 |
> |
} |
282 |
> |
return sigaltstack(&sig_stacks[--sig_stack_id], NULL); |
283 |
> |
} |
284 |
|
#endif |
285 |
|
|
286 |
|
|
287 |
|
// Global variables (exported) |
288 |
|
#if !EMULATED_PPC |
289 |
< |
void *TOC; // Small data pointer (r13) |
289 |
> |
void *TOC; // Pointer to Thread Local Storage (r2) |
290 |
> |
void *R13; // Pointer to .sdata section (r13 under Linux) |
291 |
|
#endif |
292 |
|
uint32 RAMBase; // Base address of Mac RAM |
293 |
|
uint32 RAMSize; // Size of Mac RAM |
294 |
|
uint32 KernelDataAddr; // Address of Kernel Data |
295 |
|
uint32 BootGlobsAddr; // Address of BootGlobs structure at top of Mac RAM |
296 |
+ |
uint32 DRCacheAddr; // Address of DR Cache |
297 |
|
uint32 PVR; // Theoretical PVR |
298 |
|
int64 CPUClockSpeed; // Processor clock speed (Hz) |
299 |
|
int64 BusClockSpeed; // Bus clock speed (Hz) |
300 |
+ |
int64 TimebaseSpeed; // Timebase clock speed (Hz) |
301 |
+ |
uint8 *RAMBaseHost; // Base address of Mac RAM (host address space) |
302 |
+ |
uint8 *ROMBaseHost; // Base address of Mac ROM (host address space) |
303 |
|
|
304 |
|
|
305 |
|
// Global variables |
306 |
< |
static char *x_display_name = NULL; // X11 display name |
306 |
> |
#ifndef USE_SDL_VIDEO |
307 |
> |
char *x_display_name = NULL; // X11 display name |
308 |
|
Display *x_display = NULL; // X11 display handle |
309 |
+ |
#ifdef X11_LOCK_TYPE |
310 |
+ |
X11_LOCK_TYPE x_display_lock = X11_LOCK_INIT; // X11 display lock |
311 |
+ |
#endif |
312 |
+ |
#endif |
313 |
|
|
314 |
|
static int zero_fd = 0; // FD of /dev/zero |
315 |
|
static bool lm_area_mapped = false; // Flag: Low Memory area mmap()ped |
316 |
|
static int kernel_area = -1; // SHM ID of Kernel Data area |
317 |
|
static bool rom_area_mapped = false; // Flag: Mac ROM mmap()ped |
318 |
|
static bool ram_area_mapped = false; // Flag: Mac RAM mmap()ped |
319 |
< |
static void *mmap_RAMBase = NULL; // Base address of mmap()ed RAM area |
319 |
> |
static bool dr_cache_area_mapped = false; // Flag: Mac DR Cache mmap()ped |
320 |
> |
static bool dr_emulator_area_mapped = false;// Flag: Mac DR Emulator mmap()ped |
321 |
|
static KernelData *kernel_data; // Pointer to Kernel Data |
322 |
|
static EmulatorData *emulator_data; |
323 |
|
|
324 |
|
static uint8 last_xpram[XPRAM_SIZE]; // Buffer for monitoring XPRAM changes |
325 |
|
|
326 |
|
static bool nvram_thread_active = false; // Flag: NVRAM watchdog installed |
327 |
+ |
static volatile bool nvram_thread_cancel; // Flag: Cancel NVRAM thread |
328 |
|
static pthread_t nvram_thread; // NVRAM watchdog |
329 |
|
static bool tick_thread_active = false; // Flag: MacOS thread installed |
330 |
+ |
static volatile bool tick_thread_cancel; // Flag: Cancel 60Hz thread |
331 |
|
static pthread_t tick_thread; // 60Hz thread |
332 |
|
static pthread_t emul_thread; // MacOS thread |
333 |
|
|
334 |
|
static bool ready_for_signals = false; // Handler installed, signals can be sent |
335 |
|
static int64 num_segv = 0; // Number of handled SEGV signals |
336 |
|
|
223 |
– |
#if !EMULATED_PPC |
337 |
|
static struct sigaction sigusr2_action; // Interrupt signal (of emulator thread) |
338 |
+ |
#if EMULATED_PPC |
339 |
+ |
static uintptr sig_stack = 0; // Stack for PowerPC interrupt routine |
340 |
+ |
#else |
341 |
|
static struct sigaction sigsegv_action; // Data access exception signal (of emulator thread) |
342 |
|
static struct sigaction sigill_action; // Illegal instruction signal (of emulator thread) |
227 |
– |
static void *sig_stack = NULL; // Stack for signal handlers |
228 |
– |
static void *extra_stack = NULL; // Stack for SIGSEGV inside interrupt handler |
343 |
|
static bool emul_thread_fatal = false; // Flag: MacOS thread crashed, tick thread shall dump debug output |
344 |
|
static sigregs sigsegv_regs; // Register dump when crashed |
345 |
+ |
static const char *crash_reason = NULL; // Reason of the crash (SIGSEGV, SIGBUS, SIGILL) |
346 |
|
#endif |
347 |
|
|
348 |
+ |
uint32 SheepMem::page_size; // Size of a native page |
349 |
+ |
uintptr SheepMem::zero_page = 0; // Address of ro page filled in with zeros |
350 |
+ |
uintptr SheepMem::base = 0x60000000; // Address of SheepShaver data |
351 |
+ |
uintptr SheepMem::proc; // Bottom address of SheepShave procedures |
352 |
+ |
uintptr SheepMem::data; // Top of SheepShaver data (stack like storage) |
353 |
+ |
|
354 |
|
|
355 |
|
// Prototypes |
356 |
+ |
static bool kernel_data_init(void); |
357 |
+ |
static void kernel_data_exit(void); |
358 |
|
static void Quit(void); |
359 |
|
static void *emul_func(void *arg); |
360 |
|
static void *nvram_func(void *arg); |
361 |
|
static void *tick_func(void *arg); |
362 |
< |
#if !EMULATED_PPC |
363 |
< |
static void sigusr2_handler(int sig, sigcontext_struct *sc); |
364 |
< |
static void sigsegv_handler(int sig, sigcontext_struct *sc); |
365 |
< |
static void sigill_handler(int sig, sigcontext_struct *sc); |
362 |
> |
#if EMULATED_PPC |
363 |
> |
extern void emul_ppc(uint32 start); |
364 |
> |
extern void init_emul_ppc(void); |
365 |
> |
extern void exit_emul_ppc(void); |
366 |
> |
sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t); |
367 |
> |
#else |
368 |
> |
static void sigusr2_handler(int sig, siginfo_t *sip, void *scp); |
369 |
> |
static void sigsegv_handler(int sig, siginfo_t *sip, void *scp); |
370 |
> |
static void sigill_handler(int sig, siginfo_t *sip, void *scp); |
371 |
|
#endif |
372 |
|
|
373 |
|
|
374 |
|
// From asm_linux.S |
375 |
< |
#if EMULATED_PPC |
248 |
< |
extern int atomic_add(int *var, int v); |
249 |
< |
extern int atomic_and(int *var, int v); |
250 |
< |
extern int atomic_or(int *var, int v); |
251 |
< |
#else |
252 |
< |
extern "C" void *get_toc(void); |
375 |
> |
#if !EMULATED_PPC |
376 |
|
extern "C" void *get_sp(void); |
377 |
< |
extern "C" void flush_icache_range(void *start, void *end); |
377 |
> |
extern "C" void *get_r2(void); |
378 |
> |
extern "C" void set_r2(void *); |
379 |
> |
extern "C" void *get_r13(void); |
380 |
> |
extern "C" void set_r13(void *); |
381 |
> |
extern "C" void flush_icache_range(uint32 start, uint32 end); |
382 |
|
extern "C" void jump_to_rom(uint32 entry, uint32 context); |
383 |
|
extern "C" void quit_emulator(void); |
384 |
|
extern "C" void execute_68k(uint32 pc, M68kRegisters *r); |
390 |
|
#endif |
391 |
|
|
392 |
|
|
393 |
< |
// Decode LZSS data |
394 |
< |
static void decode_lzss(const uint8 *src, uint8 *dest, int size) |
393 |
> |
#if EMULATED_PPC |
394 |
> |
/* |
395 |
> |
* Return signal stack base |
396 |
> |
*/ |
397 |
> |
|
398 |
> |
uintptr SignalStackBase(void) |
399 |
|
{ |
400 |
< |
char dict[0x1000]; |
401 |
< |
int run_mask = 0, dict_idx = 0xfee; |
402 |
< |
for (;;) { |
403 |
< |
if (run_mask < 0x100) { |
404 |
< |
// Start new run |
405 |
< |
if (--size < 0) |
406 |
< |
break; |
407 |
< |
run_mask = *src++ | 0xff00; |
408 |
< |
} |
409 |
< |
bool bit = run_mask & 1; |
410 |
< |
run_mask >>= 1; |
411 |
< |
if (bit) { |
412 |
< |
// Verbatim copy |
413 |
< |
if (--size < 0) |
414 |
< |
break; |
415 |
< |
int c = *src++; |
416 |
< |
dict[dict_idx++] = c; |
417 |
< |
*dest++ = c; |
418 |
< |
dict_idx &= 0xfff; |
419 |
< |
} else { |
420 |
< |
// Copy from dictionary |
421 |
< |
if (--size < 0) |
422 |
< |
break; |
423 |
< |
int idx = *src++; |
424 |
< |
if (--size < 0) |
425 |
< |
break; |
426 |
< |
int cnt = *src++; |
427 |
< |
idx |= (cnt << 4) & 0xf00; |
428 |
< |
cnt = (cnt & 0x0f) + 3; |
429 |
< |
while (cnt--) { |
430 |
< |
char c = dict[idx++]; |
431 |
< |
dict[dict_idx++] = c; |
432 |
< |
*dest++ = c; |
433 |
< |
idx &= 0xfff; |
434 |
< |
dict_idx &= 0xfff; |
435 |
< |
} |
436 |
< |
} |
437 |
< |
} |
400 |
> |
return sig_stack + SIG_STACK_SIZE; |
401 |
> |
} |
402 |
> |
|
403 |
> |
|
404 |
> |
/* |
405 |
> |
* Atomic operations |
406 |
> |
*/ |
407 |
> |
|
408 |
> |
#if HAVE_SPINLOCKS |
409 |
> |
static spinlock_t atomic_ops_lock = SPIN_LOCK_UNLOCKED; |
410 |
> |
#else |
411 |
> |
#define spin_lock(LOCK) |
412 |
> |
#define spin_unlock(LOCK) |
413 |
> |
#endif |
414 |
> |
|
415 |
> |
int atomic_add(int *var, int v) |
416 |
> |
{ |
417 |
> |
spin_lock(&atomic_ops_lock); |
418 |
> |
int ret = *var; |
419 |
> |
*var += v; |
420 |
> |
spin_unlock(&atomic_ops_lock); |
421 |
> |
return ret; |
422 |
> |
} |
423 |
> |
|
424 |
> |
int atomic_and(int *var, int v) |
425 |
> |
{ |
426 |
> |
spin_lock(&atomic_ops_lock); |
427 |
> |
int ret = *var; |
428 |
> |
*var &= v; |
429 |
> |
spin_unlock(&atomic_ops_lock); |
430 |
> |
return ret; |
431 |
> |
} |
432 |
> |
|
433 |
> |
int atomic_or(int *var, int v) |
434 |
> |
{ |
435 |
> |
spin_lock(&atomic_ops_lock); |
436 |
> |
int ret = *var; |
437 |
> |
*var |= v; |
438 |
> |
spin_unlock(&atomic_ops_lock); |
439 |
> |
return ret; |
440 |
> |
} |
441 |
> |
#endif |
442 |
> |
|
443 |
> |
|
444 |
> |
/* |
445 |
> |
* Memory management helpers |
446 |
> |
*/ |
447 |
> |
|
448 |
> |
static inline int vm_mac_acquire(uint32 addr, uint32 size) |
449 |
> |
{ |
450 |
> |
return vm_acquire_fixed(Mac2HostAddr(addr), size); |
451 |
> |
} |
452 |
> |
|
453 |
> |
static inline int vm_mac_release(uint32 addr, uint32 size) |
454 |
> |
{ |
455 |
> |
return vm_release(Mac2HostAddr(addr), size); |
456 |
|
} |
457 |
|
|
458 |
|
|
472 |
|
int main(int argc, char **argv) |
473 |
|
{ |
474 |
|
char str[256]; |
326 |
– |
uint32 *boot_globs; |
327 |
– |
int16 i16; |
328 |
– |
int drive, driver; |
475 |
|
int rom_fd; |
476 |
|
FILE *proc_file; |
477 |
|
const char *rom_path; |
481 |
|
|
482 |
|
// Initialize variables |
483 |
|
RAMBase = 0; |
338 |
– |
mmap_RAMBase = NULL; |
484 |
|
tzset(); |
485 |
|
|
486 |
|
// Print some info |
488 |
|
printf(" %s\n", GetString(STR_ABOUT_TEXT2)); |
489 |
|
|
490 |
|
#if !EMULATED_PPC |
491 |
+ |
#ifdef SYSTEM_CLOBBERS_R2 |
492 |
|
// Get TOC pointer |
493 |
< |
TOC = get_toc(); |
493 |
> |
TOC = get_r2(); |
494 |
> |
#endif |
495 |
> |
#ifdef SYSTEM_CLOBBERS_R13 |
496 |
> |
// Get r13 register |
497 |
> |
R13 = get_r13(); |
498 |
> |
#endif |
499 |
|
#endif |
500 |
|
|
501 |
|
#ifdef ENABLE_GTK |
511 |
|
for (int i=1; i<argc; i++) { |
512 |
|
if (strcmp(argv[i], "--help") == 0) { |
513 |
|
usage(argv[0]); |
514 |
+ |
#ifndef USE_SDL_VIDEO |
515 |
|
} else if (strcmp(argv[i], "--display") == 0) { |
516 |
|
i++; |
517 |
|
if (i < argc) |
518 |
|
x_display_name = strdup(argv[i]); |
519 |
+ |
#endif |
520 |
|
} else if (argv[i][0] == '-') { |
521 |
|
fprintf(stderr, "Unrecognized option '%s'\n", argv[i]); |
522 |
|
usage(argv[0]); |
523 |
|
} |
524 |
|
} |
525 |
|
|
526 |
+ |
#ifdef USE_SDL |
527 |
+ |
// Initialize SDL system |
528 |
+ |
int sdl_flags = 0; |
529 |
+ |
#ifdef USE_SDL_VIDEO |
530 |
+ |
sdl_flags |= SDL_INIT_VIDEO; |
531 |
+ |
#endif |
532 |
+ |
#ifdef USE_SDL_AUDIO |
533 |
+ |
sdl_flags |= SDL_INIT_AUDIO; |
534 |
+ |
#endif |
535 |
+ |
assert(sdl_flags != 0); |
536 |
+ |
if (SDL_Init(sdl_flags) == -1) { |
537 |
+ |
char str[256]; |
538 |
+ |
sprintf(str, "Could not initialize SDL: %s.\n", SDL_GetError()); |
539 |
+ |
ErrorAlert(str); |
540 |
+ |
goto quit; |
541 |
+ |
} |
542 |
+ |
atexit(SDL_Quit); |
543 |
+ |
#endif |
544 |
+ |
|
545 |
+ |
#ifndef USE_SDL_VIDEO |
546 |
|
// Open display |
547 |
|
x_display = XOpenDisplay(x_display_name); |
548 |
|
if (x_display == NULL) { |
556 |
|
// Fork out, so we can return from fullscreen mode when things get ugly |
557 |
|
XF86DGAForkApp(DefaultScreen(x_display)); |
558 |
|
#endif |
559 |
+ |
#endif |
560 |
|
|
561 |
|
#ifdef ENABLE_MON |
562 |
|
// Initialize mon |
563 |
|
mon_init(); |
564 |
|
#endif |
565 |
|
|
566 |
+ |
#if !EMULATED_PPC |
567 |
+ |
// Create and install stacks for signal handlers |
568 |
+ |
for (int i = 0; i < SIG_STACK_COUNT; i++) { |
569 |
+ |
void *sig_stack = malloc(SIG_STACK_SIZE); |
570 |
+ |
D(bug("Signal stack %d at %p\n", i, sig_stack)); |
571 |
+ |
if (sig_stack == NULL) { |
572 |
+ |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
573 |
+ |
goto quit; |
574 |
+ |
} |
575 |
+ |
sig_stacks[i].ss_sp = sig_stack; |
576 |
+ |
sig_stacks[i].ss_flags = 0; |
577 |
+ |
sig_stacks[i].ss_size = SIG_STACK_SIZE; |
578 |
+ |
} |
579 |
+ |
if (sig_stack_acquire() < 0) { |
580 |
+ |
sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno)); |
581 |
+ |
ErrorAlert(str); |
582 |
+ |
goto quit; |
583 |
+ |
} |
584 |
+ |
#endif |
585 |
+ |
|
586 |
+ |
#if !EMULATED_PPC |
587 |
+ |
// Install SIGSEGV and SIGBUS handlers |
588 |
+ |
sigemptyset(&sigsegv_action.sa_mask); // Block interrupts during SEGV handling |
589 |
+ |
sigaddset(&sigsegv_action.sa_mask, SIGUSR2); |
590 |
+ |
sigsegv_action.sa_sigaction = sigsegv_handler; |
591 |
+ |
sigsegv_action.sa_flags = SA_ONSTACK | SA_SIGINFO; |
592 |
+ |
#ifdef HAVE_SIGNAL_SA_RESTORER |
593 |
+ |
sigsegv_action.sa_restorer = NULL; |
594 |
+ |
#endif |
595 |
+ |
if (sigaction(SIGSEGV, &sigsegv_action, NULL) < 0) { |
596 |
+ |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
597 |
+ |
ErrorAlert(str); |
598 |
+ |
goto quit; |
599 |
+ |
} |
600 |
+ |
if (sigaction(SIGBUS, &sigsegv_action, NULL) < 0) { |
601 |
+ |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
602 |
+ |
ErrorAlert(str); |
603 |
+ |
goto quit; |
604 |
+ |
} |
605 |
+ |
#else |
606 |
+ |
// Install SIGSEGV handler for CPU emulator |
607 |
+ |
if (!sigsegv_install_handler(sigsegv_handler)) { |
608 |
+ |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
609 |
+ |
ErrorAlert(str); |
610 |
+ |
goto quit; |
611 |
+ |
} |
612 |
+ |
#endif |
613 |
+ |
|
614 |
+ |
// Initialize VM system |
615 |
+ |
vm_init(); |
616 |
+ |
|
617 |
|
// Get system info |
618 |
|
PVR = 0x00040000; // Default: 604 |
619 |
|
CPUClockSpeed = 100000000; // Default: 100MHz |
620 |
|
BusClockSpeed = 100000000; // Default: 100MHz |
621 |
< |
#if !EMULATED_PPC |
621 |
> |
TimebaseSpeed = 25000000; // Default: 25MHz |
622 |
> |
#if EMULATED_PPC |
623 |
> |
PVR = 0x000c0000; // Default: 7400 (with AltiVec) |
624 |
> |
#elif defined(__APPLE__) && defined(__MACH__) |
625 |
> |
proc_file = popen("ioreg -c IOPlatformDevice", "r"); |
626 |
> |
if (proc_file) { |
627 |
> |
char line[256]; |
628 |
> |
bool powerpc_node = false; |
629 |
> |
while (fgets(line, sizeof(line) - 1, proc_file)) { |
630 |
> |
// Read line |
631 |
> |
int len = strlen(line); |
632 |
> |
if (len == 0) |
633 |
> |
continue; |
634 |
> |
line[len - 1] = 0; |
635 |
> |
|
636 |
> |
// Parse line |
637 |
> |
if (strstr(line, "o PowerPC,")) |
638 |
> |
powerpc_node = true; |
639 |
> |
else if (powerpc_node) { |
640 |
> |
uint32 value; |
641 |
> |
char head[256]; |
642 |
> |
if (sscanf(line, "%[ |]\"cpu-version\" = <%x>", head, &value) == 2) |
643 |
> |
PVR = value; |
644 |
> |
else if (sscanf(line, "%[ |]\"clock-frequency\" = <%x>", head, &value) == 2) |
645 |
> |
CPUClockSpeed = value; |
646 |
> |
else if (sscanf(line, "%[ |]\"bus-frequency\" = <%x>", head, &value) == 2) |
647 |
> |
BusClockSpeed = value; |
648 |
> |
else if (sscanf(line, "%[ |]\"timebase-frequency\" = <%x>", head, &value) == 2) |
649 |
> |
TimebaseSpeed = value; |
650 |
> |
else if (strchr(line, '}')) |
651 |
> |
powerpc_node = false; |
652 |
> |
} |
653 |
> |
} |
654 |
> |
fclose(proc_file); |
655 |
> |
} else { |
656 |
> |
sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno)); |
657 |
> |
WarningAlert(str); |
658 |
> |
} |
659 |
> |
#else |
660 |
|
proc_file = fopen("/proc/cpuinfo", "r"); |
661 |
|
if (proc_file) { |
662 |
+ |
// CPU specs from Linux kernel |
663 |
+ |
// TODO: make it more generic with features (e.g. AltiVec) and |
664 |
+ |
// cache information and friends for NameRegistry |
665 |
+ |
static const struct { |
666 |
+ |
uint32 pvr_mask; |
667 |
+ |
uint32 pvr_value; |
668 |
+ |
const char *cpu_name; |
669 |
+ |
} |
670 |
+ |
cpu_specs[] = { |
671 |
+ |
{ 0xffff0000, 0x00010000, "601" }, |
672 |
+ |
{ 0xffff0000, 0x00030000, "603" }, |
673 |
+ |
{ 0xffff0000, 0x00060000, "603e" }, |
674 |
+ |
{ 0xffff0000, 0x00070000, "603ev" }, |
675 |
+ |
{ 0xffff0000, 0x00040000, "604" }, |
676 |
+ |
{ 0xfffff000, 0x00090000, "604e" }, |
677 |
+ |
{ 0xffff0000, 0x00090000, "604r" }, |
678 |
+ |
{ 0xffff0000, 0x000a0000, "604ev" }, |
679 |
+ |
{ 0xffffffff, 0x00084202, "740/750" }, |
680 |
+ |
{ 0xfffff000, 0x00083000, "745/755" }, |
681 |
+ |
{ 0xfffffff0, 0x00080100, "750CX" }, |
682 |
+ |
{ 0xfffffff0, 0x00082200, "750CX" }, |
683 |
+ |
{ 0xfffffff0, 0x00082210, "750CXe" }, |
684 |
+ |
{ 0xffffff00, 0x70000100, "750FX" }, |
685 |
+ |
{ 0xffffffff, 0x70000200, "750FX" }, |
686 |
+ |
{ 0xffff0000, 0x70000000, "750FX" }, |
687 |
+ |
{ 0xffff0000, 0x70020000, "750GX" }, |
688 |
+ |
{ 0xffff0000, 0x00080000, "740/750" }, |
689 |
+ |
{ 0xffffffff, 0x000c1101, "7400 (1.1)" }, |
690 |
+ |
{ 0xffff0000, 0x000c0000, "7400" }, |
691 |
+ |
{ 0xffff0000, 0x800c0000, "7410" }, |
692 |
+ |
{ 0xffffffff, 0x80000200, "7450" }, |
693 |
+ |
{ 0xffffffff, 0x80000201, "7450" }, |
694 |
+ |
{ 0xffff0000, 0x80000000, "7450" }, |
695 |
+ |
{ 0xffffff00, 0x80010100, "7455" }, |
696 |
+ |
{ 0xffffffff, 0x80010200, "7455" }, |
697 |
+ |
{ 0xffff0000, 0x80010000, "7455" }, |
698 |
+ |
{ 0xffff0000, 0x80020000, "7457" }, |
699 |
+ |
{ 0xffff0000, 0x80030000, "7447A" }, |
700 |
+ |
{ 0x7fff0000, 0x00810000, "82xx" }, |
701 |
+ |
{ 0x7fff0000, 0x00820000, "8280" }, |
702 |
+ |
{ 0xffff0000, 0x00400000, "Power3 (630)" }, |
703 |
+ |
{ 0xffff0000, 0x00410000, "Power3 (630+)" }, |
704 |
+ |
{ 0xffff0000, 0x00360000, "I-star" }, |
705 |
+ |
{ 0xffff0000, 0x00370000, "S-star" }, |
706 |
+ |
{ 0xffff0000, 0x00350000, "Power4" }, |
707 |
+ |
{ 0xffff0000, 0x00390000, "PPC970" }, |
708 |
+ |
{ 0xffff0000, 0x003a0000, "POWER5" }, |
709 |
+ |
{ 0, 0, 0 } |
710 |
+ |
}; |
711 |
+ |
|
712 |
|
char line[256]; |
713 |
|
while(fgets(line, 255, proc_file)) { |
714 |
|
// Read line |
720 |
|
// Parse line |
721 |
|
int i; |
722 |
|
char value[256]; |
723 |
< |
if (sscanf(line, "cpu : %s", value) == 1) { |
724 |
< |
if (strcmp(value, "601") == 0) |
725 |
< |
PVR = 0x00010000; |
726 |
< |
else if (strcmp(value, "603") == 0) |
727 |
< |
PVR = 0x00030000; |
728 |
< |
else if (strcmp(value, "604") == 0) |
729 |
< |
PVR = 0x00040000; |
730 |
< |
else if (strcmp(value, "603e") == 0) |
731 |
< |
PVR = 0x00060000; |
732 |
< |
else if (strcmp(value, "603ev") == 0) |
733 |
< |
PVR = 0x00070000; |
421 |
< |
else if (strcmp(value, "604e") == 0) |
422 |
< |
PVR = 0x00090000; |
423 |
< |
else if (strcmp(value, "604ev5") == 0) |
424 |
< |
PVR = 0x000a0000; |
425 |
< |
else if (strcmp(value, "750") == 0) |
426 |
< |
PVR = 0x00080000; |
427 |
< |
else if (strcmp(value, "821") == 0) |
428 |
< |
PVR = 0x00320000; |
429 |
< |
else if (strcmp(value, "860") == 0) |
430 |
< |
PVR = 0x00500000; |
431 |
< |
else |
723 |
> |
if (sscanf(line, "cpu : %[0-9A-Za-a]", value) == 1) { |
724 |
> |
// Search by name |
725 |
> |
const char *cpu_name = NULL; |
726 |
> |
for (int i = 0; cpu_specs[i].pvr_mask != 0; i++) { |
727 |
> |
if (strcmp(cpu_specs[i].cpu_name, value) == 0) { |
728 |
> |
cpu_name = cpu_specs[i].cpu_name; |
729 |
> |
PVR = cpu_specs[i].pvr_value; |
730 |
> |
break; |
731 |
> |
} |
732 |
> |
} |
733 |
> |
if (cpu_name == NULL) |
734 |
|
printf("WARNING: Unknown CPU type '%s', assuming 604\n", value); |
735 |
+ |
else |
736 |
+ |
printf("Found a PowerPC %s processor\n", cpu_name); |
737 |
|
} |
738 |
|
if (sscanf(line, "clock : %dMHz", &i) == 1) |
739 |
|
CPUClockSpeed = BusClockSpeed = i * 1000000; |
743 |
|
sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno)); |
744 |
|
WarningAlert(str); |
745 |
|
} |
746 |
+ |
|
747 |
+ |
// Get actual bus frequency |
748 |
+ |
proc_file = fopen("/proc/device-tree/clock-frequency", "r"); |
749 |
+ |
if (proc_file) { |
750 |
+ |
union { uint8 b[4]; uint32 l; } value; |
751 |
+ |
if (fread(value.b, sizeof(value), 1, proc_file) == 1) |
752 |
+ |
BusClockSpeed = value.l; |
753 |
+ |
fclose(proc_file); |
754 |
+ |
} |
755 |
+ |
|
756 |
+ |
// Get actual timebase frequency |
757 |
+ |
TimebaseSpeed = BusClockSpeed / 4; |
758 |
+ |
DIR *cpus_dir; |
759 |
+ |
if ((cpus_dir = opendir("/proc/device-tree/cpus")) != NULL) { |
760 |
+ |
struct dirent *cpu_entry; |
761 |
+ |
while ((cpu_entry = readdir(cpus_dir)) != NULL) { |
762 |
+ |
if (strstr(cpu_entry->d_name, "PowerPC,") == cpu_entry->d_name) { |
763 |
+ |
char timebase_freq_node[256]; |
764 |
+ |
sprintf(timebase_freq_node, "/proc/device-tree/cpus/%s/timebase-frequency", cpu_entry->d_name); |
765 |
+ |
proc_file = fopen(timebase_freq_node, "r"); |
766 |
+ |
if (proc_file) { |
767 |
+ |
union { uint8 b[4]; uint32 l; } value; |
768 |
+ |
if (fread(value.b, sizeof(value), 1, proc_file) == 1) |
769 |
+ |
TimebaseSpeed = value.l; |
770 |
+ |
fclose(proc_file); |
771 |
+ |
} |
772 |
+ |
} |
773 |
+ |
} |
774 |
+ |
closedir(cpus_dir); |
775 |
+ |
} |
776 |
|
#endif |
777 |
+ |
// Remap any newer G4/G5 processor to plain G4 for compatibility |
778 |
+ |
switch (PVR >> 16) { |
779 |
+ |
case 0x8000: // 7450 |
780 |
+ |
case 0x8001: // 7455 |
781 |
+ |
case 0x8002: // 7457 |
782 |
+ |
case 0x0039: // 970 |
783 |
+ |
PVR = 0x000c0000; // 7400 |
784 |
+ |
break; |
785 |
+ |
} |
786 |
|
D(bug("PVR: %08x (assumed)\n", PVR)); |
787 |
|
|
788 |
|
// Init system routines |
806 |
|
goto quit; |
807 |
|
} |
808 |
|
|
809 |
+ |
#ifndef PAGEZERO_HACK |
810 |
|
// Create Low Memory area (0x0000..0x3000) |
811 |
< |
if (mmap((char *)0x0000, 0x3000, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, zero_fd, 0) == (void *)-1) { |
811 |
> |
if (vm_mac_acquire(0, 0x3000) < 0) { |
812 |
|
sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno)); |
813 |
|
ErrorAlert(str); |
814 |
|
goto quit; |
815 |
|
} |
816 |
|
lm_area_mapped = true; |
817 |
+ |
#endif |
818 |
|
|
819 |
|
// Create areas for Kernel Data |
820 |
< |
kernel_area = shmget(IPC_PRIVATE, KERNEL_AREA_SIZE, 0600); |
821 |
< |
if (kernel_area == -1) { |
822 |
< |
sprintf(str, GetString(STR_KD_SHMGET_ERR), strerror(errno)); |
820 |
> |
if (!kernel_data_init()) |
821 |
> |
goto quit; |
822 |
> |
kernel_data = (KernelData *)Mac2HostAddr(KERNEL_DATA_BASE); |
823 |
> |
emulator_data = &kernel_data->ed; |
824 |
> |
KernelDataAddr = KERNEL_DATA_BASE; |
825 |
> |
D(bug("Kernel Data at %p (%08x)\n", kernel_data, KERNEL_DATA_BASE)); |
826 |
> |
D(bug("Emulator Data at %p (%08x)\n", emulator_data, KERNEL_DATA_BASE + offsetof(KernelData, ed))); |
827 |
> |
|
828 |
> |
// Create area for DR Cache |
829 |
> |
if (vm_mac_acquire(DR_EMULATOR_BASE, DR_EMULATOR_SIZE) < 0) { |
830 |
> |
sprintf(str, GetString(STR_DR_EMULATOR_MMAP_ERR), strerror(errno)); |
831 |
|
ErrorAlert(str); |
832 |
|
goto quit; |
833 |
|
} |
834 |
< |
if (shmat(kernel_area, (void *)KERNEL_DATA_BASE, 0) < 0) { |
835 |
< |
sprintf(str, GetString(STR_KD_SHMAT_ERR), strerror(errno)); |
834 |
> |
dr_emulator_area_mapped = true; |
835 |
> |
if (vm_mac_acquire(DR_CACHE_BASE, DR_CACHE_SIZE) < 0) { |
836 |
> |
sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno)); |
837 |
|
ErrorAlert(str); |
838 |
|
goto quit; |
839 |
|
} |
840 |
< |
if (shmat(kernel_area, (void *)KERNEL_DATA2_BASE, 0) < 0) { |
841 |
< |
sprintf(str, GetString(STR_KD2_SHMAT_ERR), strerror(errno)); |
840 |
> |
dr_cache_area_mapped = true; |
841 |
> |
#if !EMULATED_PPC |
842 |
> |
if (vm_protect((char *)DR_CACHE_BASE, DR_CACHE_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
843 |
> |
sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno)); |
844 |
> |
ErrorAlert(str); |
845 |
> |
goto quit; |
846 |
> |
} |
847 |
> |
#endif |
848 |
> |
DRCacheAddr = DR_CACHE_BASE; |
849 |
> |
D(bug("DR Cache at %p\n", DRCacheAddr)); |
850 |
> |
|
851 |
> |
// Create area for SheepShaver data |
852 |
> |
if (!SheepMem::Init()) { |
853 |
> |
sprintf(str, GetString(STR_SHEEP_MEM_MMAP_ERR), strerror(errno)); |
854 |
|
ErrorAlert(str); |
855 |
|
goto quit; |
856 |
|
} |
491 |
– |
kernel_data = (KernelData *)0x68ffe000; |
492 |
– |
emulator_data = &kernel_data->ed; |
493 |
– |
KernelDataAddr = (uint32)kernel_data; |
494 |
– |
D(bug("Kernel Data at %p, Emulator Data at %p\n", kernel_data, emulator_data)); |
857 |
|
|
858 |
|
// Create area for Mac ROM |
859 |
< |
if (mmap((char *)ROM_BASE, ROM_AREA_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, zero_fd, 0) == (void *)-1) { |
859 |
> |
if (vm_mac_acquire(ROM_BASE, ROM_AREA_SIZE) < 0) { |
860 |
> |
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
861 |
> |
ErrorAlert(str); |
862 |
> |
goto quit; |
863 |
> |
} |
864 |
> |
ROMBaseHost = Mac2HostAddr(ROM_BASE); |
865 |
> |
#if !EMULATED_PPC |
866 |
> |
if (vm_protect(ROMBaseHost, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
867 |
|
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
868 |
|
ErrorAlert(str); |
869 |
|
goto quit; |
870 |
|
} |
871 |
+ |
#endif |
872 |
|
rom_area_mapped = true; |
873 |
< |
D(bug("ROM area at %08x\n", ROM_BASE)); |
873 |
> |
D(bug("ROM area at %p (%08x)\n", ROMBaseHost, ROM_BASE)); |
874 |
|
|
875 |
|
// Create area for Mac RAM |
876 |
|
RAMSize = PrefsFindInt32("ramsize"); |
879 |
|
RAMSize = 8*1024*1024; |
880 |
|
} |
881 |
|
|
882 |
< |
mmap_RAMBase = mmap((void *)0x20000000, RAMSize, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, zero_fd, 0); |
883 |
< |
if (mmap_RAMBase == (void *)-1) { |
882 |
> |
if (vm_mac_acquire(RAM_BASE, RAMSize) < 0) { |
883 |
> |
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
884 |
> |
ErrorAlert(str); |
885 |
> |
goto quit; |
886 |
> |
} |
887 |
> |
RAMBaseHost = Mac2HostAddr(RAM_BASE); |
888 |
> |
#if !EMULATED_PPC |
889 |
> |
if (vm_protect(RAMBaseHost, RAMSize, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
890 |
|
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
891 |
|
ErrorAlert(str); |
892 |
|
goto quit; |
893 |
|
} |
894 |
< |
RAMBase = (uint32)mmap_RAMBase; |
894 |
> |
#endif |
895 |
> |
RAMBase = RAM_BASE; |
896 |
|
ram_area_mapped = true; |
897 |
< |
D(bug("RAM area at %08x\n", RAMBase)); |
897 |
> |
D(bug("RAM area at %p (%08x)\n", RAMBaseHost, RAMBase)); |
898 |
|
|
899 |
|
if (RAMBase > ROM_BASE) { |
900 |
|
ErrorAlert(GetString(STR_RAM_HIGHER_THAN_ROM_ERR)); |
917 |
|
rom_tmp = new uint8[ROM_SIZE]; |
918 |
|
actual = read(rom_fd, (void *)rom_tmp, ROM_SIZE); |
919 |
|
close(rom_fd); |
920 |
< |
if (actual == ROM_SIZE) { |
921 |
< |
// Plain ROM image |
922 |
< |
memcpy((void *)ROM_BASE, rom_tmp, ROM_SIZE); |
923 |
< |
delete[] rom_tmp; |
547 |
< |
} else { |
548 |
< |
if (strncmp((char *)rom_tmp, "<CHRP-BOOT>", 11) == 0) { |
549 |
< |
// CHRP compressed ROM image |
550 |
< |
D(bug("CHRP ROM image\n")); |
551 |
< |
uint32 lzss_offset, lzss_size; |
552 |
< |
|
553 |
< |
char *s = strstr((char *)rom_tmp, "constant lzss-offset"); |
554 |
< |
if (s == NULL) { |
555 |
< |
ErrorAlert(GetString(STR_ROM_SIZE_ERR)); |
556 |
< |
goto quit; |
557 |
< |
} |
558 |
< |
s -= 7; |
559 |
< |
if (sscanf(s, "%06x", &lzss_offset) != 1) { |
560 |
< |
ErrorAlert(GetString(STR_ROM_SIZE_ERR)); |
561 |
< |
goto quit; |
562 |
< |
} |
563 |
< |
s = strstr((char *)rom_tmp, "constant lzss-size"); |
564 |
< |
if (s == NULL) { |
565 |
< |
ErrorAlert(GetString(STR_ROM_SIZE_ERR)); |
566 |
< |
goto quit; |
567 |
< |
} |
568 |
< |
s -= 7; |
569 |
< |
if (sscanf(s, "%06x", &lzss_size) != 1) { |
570 |
< |
ErrorAlert(GetString(STR_ROM_SIZE_ERR)); |
571 |
< |
goto quit; |
572 |
< |
} |
573 |
< |
D(bug("Offset of compressed data: %08x\n", lzss_offset)); |
574 |
< |
D(bug("Size of compressed data: %08x\n", lzss_size)); |
575 |
< |
|
576 |
< |
D(bug("Uncompressing ROM...\n")); |
577 |
< |
decode_lzss(rom_tmp + lzss_offset, (uint8 *)ROM_BASE, lzss_size); |
578 |
< |
delete[] rom_tmp; |
579 |
< |
} else if (rom_size != 4*1024*1024) { |
920 |
> |
|
921 |
> |
// Decode Mac ROM |
922 |
> |
if (!DecodeROM(rom_tmp, actual)) { |
923 |
> |
if (rom_size != 4*1024*1024) { |
924 |
|
ErrorAlert(GetString(STR_ROM_SIZE_ERR)); |
925 |
|
goto quit; |
926 |
|
} else { |
928 |
|
goto quit; |
929 |
|
} |
930 |
|
} |
931 |
+ |
delete[] rom_tmp; |
932 |
|
|
933 |
< |
// Load NVRAM |
934 |
< |
XPRAMInit(); |
590 |
< |
|
591 |
< |
// Set boot volume |
592 |
< |
drive = PrefsFindInt32("bootdrive"); |
593 |
< |
XPRAM[0x1378] = i16 >> 8; |
594 |
< |
XPRAM[0x1379] = i16 & 0xff; |
595 |
< |
driver = PrefsFindInt32("bootdriver"); |
596 |
< |
XPRAM[0x137a] = i16 >> 8; |
597 |
< |
XPRAM[0x137b] = i16 & 0xff; |
598 |
< |
|
599 |
< |
// Create BootGlobs at top of Mac memory |
600 |
< |
memset((void *)(RAMBase + RAMSize - 4096), 0, 4096); |
601 |
< |
BootGlobsAddr = RAMBase + RAMSize - 0x1c; |
602 |
< |
boot_globs = (uint32 *)BootGlobsAddr; |
603 |
< |
boot_globs[-5] = htonl(RAMBase + RAMSize); // MemTop |
604 |
< |
boot_globs[0] = htonl(RAMBase); // First RAM bank |
605 |
< |
boot_globs[1] = htonl(RAMSize); |
606 |
< |
boot_globs[2] = htonl((uint32)-1); // End of bank table |
607 |
< |
|
608 |
< |
// Init drivers |
609 |
< |
SonyInit(); |
610 |
< |
DiskInit(); |
611 |
< |
CDROMInit(); |
612 |
< |
SCSIInit(); |
613 |
< |
|
614 |
< |
// Init external file system |
615 |
< |
ExtFSInit(); |
616 |
< |
|
617 |
< |
// Init audio |
618 |
< |
AudioInit(); |
619 |
< |
|
620 |
< |
// Init network |
621 |
< |
EtherInit(); |
622 |
< |
|
623 |
< |
// Init serial ports |
624 |
< |
SerialInit(); |
625 |
< |
|
626 |
< |
// Init Time Manager |
627 |
< |
TimerInit(); |
628 |
< |
|
629 |
< |
// Init clipboard |
630 |
< |
ClipInit(); |
631 |
< |
|
632 |
< |
// Init video |
633 |
< |
if (!VideoInit()) |
634 |
< |
goto quit; |
635 |
< |
|
636 |
< |
// Install ROM patches |
637 |
< |
if (!PatchROM()) { |
638 |
< |
ErrorAlert(GetString(STR_UNSUPPORTED_ROM_TYPE_ERR)); |
933 |
> |
// Initialize everything |
934 |
> |
if (!InitAll()) |
935 |
|
goto quit; |
936 |
< |
} |
936 |
> |
D(bug("Initialization complete\n")); |
937 |
|
|
938 |
|
// Clear caches (as we loaded and patched code) and write protect ROM |
939 |
|
#if !EMULATED_PPC |
940 |
< |
MakeExecutable(0, (void *)ROM_BASE, ROM_AREA_SIZE); |
645 |
< |
#endif |
646 |
< |
mprotect((char *)ROM_BASE, ROM_AREA_SIZE, PROT_EXEC | PROT_READ); |
647 |
< |
|
648 |
< |
// Initialize Kernel Data |
649 |
< |
memset(kernel_data, 0, sizeof(KernelData)); |
650 |
< |
if (ROMType == ROMTYPE_NEWWORLD) { |
651 |
< |
static uint32 of_dev_tree[4] = {0, 0, 0, 0}; |
652 |
< |
static uint8 vector_lookup_tbl[128]; |
653 |
< |
static uint8 vector_mask_tbl[64]; |
654 |
< |
memset((uint8 *)kernel_data + 0xb80, 0x3d, 0x80); |
655 |
< |
memset(vector_lookup_tbl, 0, 128); |
656 |
< |
memset(vector_mask_tbl, 0, 64); |
657 |
< |
kernel_data->v[0xb80 >> 2] = htonl(ROM_BASE); |
658 |
< |
kernel_data->v[0xb84 >> 2] = htonl((uint32)of_dev_tree); // OF device tree base |
659 |
< |
kernel_data->v[0xb90 >> 2] = htonl((uint32)vector_lookup_tbl); |
660 |
< |
kernel_data->v[0xb94 >> 2] = htonl((uint32)vector_mask_tbl); |
661 |
< |
kernel_data->v[0xb98 >> 2] = htonl(ROM_BASE); // OpenPIC base |
662 |
< |
kernel_data->v[0xbb0 >> 2] = htonl(0); // ADB base |
663 |
< |
kernel_data->v[0xc20 >> 2] = htonl(RAMSize); |
664 |
< |
kernel_data->v[0xc24 >> 2] = htonl(RAMSize); |
665 |
< |
kernel_data->v[0xc30 >> 2] = htonl(RAMSize); |
666 |
< |
kernel_data->v[0xc34 >> 2] = htonl(RAMSize); |
667 |
< |
kernel_data->v[0xc38 >> 2] = htonl(0x00010020); |
668 |
< |
kernel_data->v[0xc3c >> 2] = htonl(0x00200001); |
669 |
< |
kernel_data->v[0xc40 >> 2] = htonl(0x00010000); |
670 |
< |
kernel_data->v[0xc50 >> 2] = htonl(RAMBase); |
671 |
< |
kernel_data->v[0xc54 >> 2] = htonl(RAMSize); |
672 |
< |
kernel_data->v[0xf60 >> 2] = htonl(PVR); |
673 |
< |
kernel_data->v[0xf64 >> 2] = htonl(CPUClockSpeed); |
674 |
< |
kernel_data->v[0xf68 >> 2] = htonl(BusClockSpeed); |
675 |
< |
kernel_data->v[0xf6c >> 2] = htonl(CPUClockSpeed); |
676 |
< |
} else { |
677 |
< |
kernel_data->v[0xc80 >> 2] = htonl(RAMSize); |
678 |
< |
kernel_data->v[0xc84 >> 2] = htonl(RAMSize); |
679 |
< |
kernel_data->v[0xc90 >> 2] = htonl(RAMSize); |
680 |
< |
kernel_data->v[0xc94 >> 2] = htonl(RAMSize); |
681 |
< |
kernel_data->v[0xc98 >> 2] = htonl(0x00010020); |
682 |
< |
kernel_data->v[0xc9c >> 2] = htonl(0x00200001); |
683 |
< |
kernel_data->v[0xca0 >> 2] = htonl(0x00010000); |
684 |
< |
kernel_data->v[0xcb0 >> 2] = htonl(RAMBase); |
685 |
< |
kernel_data->v[0xcb4 >> 2] = htonl(RAMSize); |
686 |
< |
kernel_data->v[0xf80 >> 2] = htonl(PVR); |
687 |
< |
kernel_data->v[0xf84 >> 2] = htonl(CPUClockSpeed); |
688 |
< |
kernel_data->v[0xf88 >> 2] = htonl(BusClockSpeed); |
689 |
< |
kernel_data->v[0xf8c >> 2] = htonl(CPUClockSpeed); |
690 |
< |
} |
691 |
< |
|
692 |
< |
// Initialize extra low memory |
693 |
< |
D(bug("Initializing Low Memory...\n")); |
694 |
< |
memset(NULL, 0, 0x3000); |
695 |
< |
WriteMacInt32(XLM_SIGNATURE, FOURCC('B','a','a','h')); // Signature to detect SheepShaver |
696 |
< |
WriteMacInt32(XLM_KERNEL_DATA, (uint32)kernel_data); // For trap replacement routines |
697 |
< |
WriteMacInt32(XLM_PVR, PVR); // Theoretical PVR |
698 |
< |
WriteMacInt32(XLM_BUS_CLOCK, BusClockSpeed); // For DriverServicesLib patch |
699 |
< |
WriteMacInt16(XLM_EXEC_RETURN_OPCODE, M68K_EXEC_RETURN); // For Execute68k() (RTS from the executed 68k code will jump here and end 68k mode) |
700 |
< |
#if !EMULATED_PPC |
701 |
< |
WriteMacInt32(XLM_TOC, (uint32)TOC); // TOC pointer of emulator |
702 |
< |
WriteMacInt32(XLM_ETHER_INIT, (uint32)InitStreamModule); // DLPI ethernet driver functions |
703 |
< |
WriteMacInt32(XLM_ETHER_TERM, (uint32)TerminateStreamModule); |
704 |
< |
WriteMacInt32(XLM_ETHER_OPEN, (uint32)ether_open); |
705 |
< |
WriteMacInt32(XLM_ETHER_CLOSE, (uint32)ether_close); |
706 |
< |
WriteMacInt32(XLM_ETHER_WPUT, (uint32)ether_wput); |
707 |
< |
WriteMacInt32(XLM_ETHER_RSRV, (uint32)ether_rsrv); |
708 |
< |
WriteMacInt32(XLM_VIDEO_DOIO, (uint32)VideoDoDriverIO); |
940 |
> |
flush_icache_range(ROM_BASE, ROM_BASE + ROM_AREA_SIZE); |
941 |
|
#endif |
942 |
< |
D(bug("Low Memory initialized\n")); |
942 |
> |
vm_protect(ROMBaseHost, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_EXECUTE); |
943 |
|
|
944 |
|
// Start 60Hz thread |
945 |
+ |
tick_thread_cancel = false; |
946 |
|
tick_thread_active = (pthread_create(&tick_thread, NULL, tick_func, NULL) == 0); |
947 |
|
D(bug("Tick thread installed (%ld)\n", tick_thread)); |
948 |
|
|
949 |
|
// Start NVRAM watchdog thread |
950 |
|
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
951 |
+ |
nvram_thread_cancel = false; |
952 |
|
nvram_thread_active = (pthread_create(&nvram_thread, NULL, nvram_func, NULL) == 0); |
953 |
|
D(bug("NVRAM thread installed (%ld)\n", nvram_thread)); |
954 |
|
|
955 |
|
#if !EMULATED_PPC |
722 |
– |
// Create and install stacks for signal handlers |
723 |
– |
sig_stack = malloc(SIG_STACK_SIZE); |
724 |
– |
D(bug("Signal stack at %p\n", sig_stack)); |
725 |
– |
if (sig_stack == NULL) { |
726 |
– |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
727 |
– |
goto quit; |
728 |
– |
} |
729 |
– |
extra_stack = malloc(SIG_STACK_SIZE); |
730 |
– |
D(bug("Extra stack at %p\n", extra_stack)); |
731 |
– |
if (extra_stack == NULL) { |
732 |
– |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
733 |
– |
goto quit; |
734 |
– |
} |
735 |
– |
struct sigaltstack new_stack; |
736 |
– |
new_stack.ss_sp = sig_stack; |
737 |
– |
new_stack.ss_flags = 0; |
738 |
– |
new_stack.ss_size = SIG_STACK_SIZE; |
739 |
– |
if (sigaltstack(&new_stack, NULL) < 0) { |
740 |
– |
sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno)); |
741 |
– |
ErrorAlert(str); |
742 |
– |
goto quit; |
743 |
– |
} |
744 |
– |
#endif |
745 |
– |
|
746 |
– |
#if !EMULATED_PPC |
747 |
– |
// Install SIGSEGV handler |
748 |
– |
sigemptyset(&sigsegv_action.sa_mask); // Block interrupts during SEGV handling |
749 |
– |
sigaddset(&sigsegv_action.sa_mask, SIGUSR2); |
750 |
– |
sigsegv_action.sa_handler = (__sighandler_t)sigsegv_handler; |
751 |
– |
sigsegv_action.sa_flags = SA_ONSTACK; |
752 |
– |
sigsegv_action.sa_restorer = NULL; |
753 |
– |
if (sigaction(SIGSEGV, &sigsegv_action, NULL) < 0) { |
754 |
– |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
755 |
– |
ErrorAlert(str); |
756 |
– |
goto quit; |
757 |
– |
} |
758 |
– |
|
956 |
|
// Install SIGILL handler |
957 |
|
sigemptyset(&sigill_action.sa_mask); // Block interrupts during ILL handling |
958 |
|
sigaddset(&sigill_action.sa_mask, SIGUSR2); |
959 |
< |
sigill_action.sa_handler = (__sighandler_t)sigill_handler; |
960 |
< |
sigill_action.sa_flags = SA_ONSTACK; |
959 |
> |
sigill_action.sa_sigaction = sigill_handler; |
960 |
> |
sigill_action.sa_flags = SA_ONSTACK | SA_SIGINFO; |
961 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
962 |
|
sigill_action.sa_restorer = NULL; |
963 |
+ |
#endif |
964 |
|
if (sigaction(SIGILL, &sigill_action, NULL) < 0) { |
965 |
|
sprintf(str, GetString(STR_SIGILL_INSTALL_ERR), strerror(errno)); |
966 |
|
ErrorAlert(str); |
967 |
|
goto quit; |
968 |
|
} |
969 |
+ |
#endif |
970 |
|
|
971 |
+ |
#if !EMULATED_PPC |
972 |
|
// Install interrupt signal handler |
973 |
|
sigemptyset(&sigusr2_action.sa_mask); |
974 |
< |
sigusr2_action.sa_handler = (__sighandler_t)sigusr2_handler; |
975 |
< |
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART; |
974 |
> |
sigusr2_action.sa_sigaction = sigusr2_handler; |
975 |
> |
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART | SA_SIGINFO; |
976 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
977 |
|
sigusr2_action.sa_restorer = NULL; |
978 |
+ |
#endif |
979 |
|
if (sigaction(SIGUSR2, &sigusr2_action, NULL) < 0) { |
980 |
|
sprintf(str, GetString(STR_SIGUSR2_INSTALL_ERR), strerror(errno)); |
981 |
|
ErrorAlert(str); |
1000 |
|
|
1001 |
|
static void Quit(void) |
1002 |
|
{ |
1003 |
+ |
#if EMULATED_PPC |
1004 |
+ |
// Exit PowerPC emulation |
1005 |
+ |
exit_emul_ppc(); |
1006 |
+ |
#endif |
1007 |
+ |
|
1008 |
|
// Stop 60Hz thread |
1009 |
|
if (tick_thread_active) { |
1010 |
+ |
tick_thread_cancel = true; |
1011 |
|
pthread_cancel(tick_thread); |
1012 |
|
pthread_join(tick_thread, NULL); |
1013 |
|
} |
1014 |
|
|
1015 |
|
// Stop NVRAM watchdog thread |
1016 |
|
if (nvram_thread_active) { |
1017 |
+ |
nvram_thread_cancel = true; |
1018 |
|
pthread_cancel(nvram_thread); |
1019 |
|
pthread_join(nvram_thread, NULL); |
1020 |
|
} |
1021 |
|
|
1022 |
|
#if !EMULATED_PPC |
1023 |
< |
// Uninstall SIGSEGV handler |
1023 |
> |
// Uninstall SIGSEGV and SIGBUS handlers |
1024 |
|
sigemptyset(&sigsegv_action.sa_mask); |
1025 |
|
sigsegv_action.sa_handler = SIG_DFL; |
1026 |
|
sigsegv_action.sa_flags = 0; |
1027 |
|
sigaction(SIGSEGV, &sigsegv_action, NULL); |
1028 |
+ |
sigaction(SIGBUS, &sigsegv_action, NULL); |
1029 |
|
|
1030 |
|
// Uninstall SIGILL handler |
1031 |
|
sigemptyset(&sigill_action.sa_mask); |
1032 |
|
sigill_action.sa_handler = SIG_DFL; |
1033 |
|
sigill_action.sa_flags = 0; |
1034 |
|
sigaction(SIGILL, &sigill_action, NULL); |
824 |
– |
#endif |
825 |
– |
|
826 |
– |
// Save NVRAM |
827 |
– |
XPRAMExit(); |
828 |
– |
|
829 |
– |
// Exit clipboard |
830 |
– |
ClipExit(); |
1035 |
|
|
1036 |
< |
// Exit Time Manager |
1037 |
< |
TimerExit(); |
1038 |
< |
|
1039 |
< |
// Exit serial |
1040 |
< |
SerialExit(); |
1041 |
< |
|
1042 |
< |
// Exit network |
839 |
< |
EtherExit(); |
840 |
< |
|
841 |
< |
// Exit audio |
842 |
< |
AudioExit(); |
843 |
< |
|
844 |
< |
// Exit video |
845 |
< |
VideoExit(); |
1036 |
> |
// Delete stacks for signal handlers |
1037 |
> |
for (int i = 0; i < SIG_STACK_COUNT; i++) { |
1038 |
> |
void *sig_stack = sig_stacks[i].ss_sp; |
1039 |
> |
if (sig_stack) |
1040 |
> |
free(sig_stack); |
1041 |
> |
} |
1042 |
> |
#endif |
1043 |
|
|
1044 |
< |
// Exit external file system |
1045 |
< |
ExtFSExit(); |
1044 |
> |
// Deinitialize everything |
1045 |
> |
ExitAll(); |
1046 |
|
|
1047 |
< |
// Exit drivers |
1048 |
< |
SCSIExit(); |
852 |
< |
CDROMExit(); |
853 |
< |
DiskExit(); |
854 |
< |
SonyExit(); |
1047 |
> |
// Delete SheepShaver globals |
1048 |
> |
SheepMem::Exit(); |
1049 |
|
|
1050 |
|
// Delete RAM area |
1051 |
|
if (ram_area_mapped) |
1052 |
< |
munmap(mmap_RAMBase, RAMSize); |
1052 |
> |
vm_mac_release(RAM_BASE, RAMSize); |
1053 |
|
|
1054 |
|
// Delete ROM area |
1055 |
|
if (rom_area_mapped) |
1056 |
< |
munmap((char *)ROM_BASE, ROM_AREA_SIZE); |
1056 |
> |
vm_mac_release(ROM_BASE, ROM_AREA_SIZE); |
1057 |
> |
|
1058 |
> |
// Delete DR cache areas |
1059 |
> |
if (dr_emulator_area_mapped) |
1060 |
> |
vm_mac_release(DR_EMULATOR_BASE, DR_EMULATOR_SIZE); |
1061 |
> |
if (dr_cache_area_mapped) |
1062 |
> |
vm_mac_release(DR_CACHE_BASE, DR_CACHE_SIZE); |
1063 |
|
|
1064 |
|
// Delete Kernel Data area |
1065 |
< |
if (kernel_area >= 0) { |
866 |
< |
shmdt((void *)KERNEL_DATA_BASE); |
867 |
< |
shmdt((void *)KERNEL_DATA2_BASE); |
868 |
< |
shmctl(kernel_area, IPC_RMID, NULL); |
869 |
< |
} |
1065 |
> |
kernel_data_exit(); |
1066 |
|
|
1067 |
|
// Delete Low Memory area |
1068 |
|
if (lm_area_mapped) |
1069 |
< |
munmap((char *)0x0000, 0x3000); |
1069 |
> |
vm_mac_release(0, 0x3000); |
1070 |
|
|
1071 |
|
// Close /dev/zero |
1072 |
|
if (zero_fd > 0) |
1084 |
|
#endif |
1085 |
|
|
1086 |
|
// Close X11 server connection |
1087 |
+ |
#ifndef USE_SDL_VIDEO |
1088 |
|
if (x_display) |
1089 |
|
XCloseDisplay(x_display); |
1090 |
+ |
#endif |
1091 |
|
|
1092 |
|
exit(0); |
1093 |
|
} |
1094 |
|
|
1095 |
|
|
1096 |
|
/* |
1097 |
+ |
* Initialize Kernel Data segments |
1098 |
+ |
*/ |
1099 |
+ |
|
1100 |
+ |
#if defined(__CYGWIN__) |
1101 |
+ |
#define WIN32_LEAN_AND_MEAN |
1102 |
+ |
#include <windows.h> |
1103 |
+ |
|
1104 |
+ |
static HANDLE kernel_handle; // Shared memory handle for Kernel Data |
1105 |
+ |
static DWORD allocation_granule; // Minimum size of allocateable are (64K) |
1106 |
+ |
static DWORD kernel_area_size; // Size of Kernel Data area |
1107 |
+ |
#endif |
1108 |
+ |
|
1109 |
+ |
static bool kernel_data_init(void) |
1110 |
+ |
{ |
1111 |
+ |
char str[256]; |
1112 |
+ |
#ifdef _WIN32 |
1113 |
+ |
SYSTEM_INFO si; |
1114 |
+ |
GetSystemInfo(&si); |
1115 |
+ |
allocation_granule = si.dwAllocationGranularity; |
1116 |
+ |
kernel_area_size = (KERNEL_AREA_SIZE + allocation_granule - 1) & -allocation_granule; |
1117 |
+ |
|
1118 |
+ |
char rcs[10]; |
1119 |
+ |
LPVOID kernel_addr; |
1120 |
+ |
kernel_handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, kernel_area_size, NULL); |
1121 |
+ |
if (kernel_handle == NULL) { |
1122 |
+ |
sprintf(rcs, "%d", GetLastError()); |
1123 |
+ |
sprintf(str, GetString(STR_KD_SHMGET_ERR), rcs); |
1124 |
+ |
ErrorAlert(str); |
1125 |
+ |
return false; |
1126 |
+ |
} |
1127 |
+ |
kernel_addr = (LPVOID)Mac2HostAddr(KERNEL_DATA_BASE & -allocation_granule); |
1128 |
+ |
if (MapViewOfFileEx(kernel_handle, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, kernel_area_size, kernel_addr) != kernel_addr) { |
1129 |
+ |
sprintf(rcs, "%d", GetLastError()); |
1130 |
+ |
sprintf(str, GetString(STR_KD_SHMAT_ERR), rcs); |
1131 |
+ |
ErrorAlert(str); |
1132 |
+ |
return false; |
1133 |
+ |
} |
1134 |
+ |
kernel_addr = (LPVOID)Mac2HostAddr(KERNEL_DATA2_BASE & -allocation_granule); |
1135 |
+ |
if (MapViewOfFileEx(kernel_handle, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, kernel_area_size, kernel_addr) != kernel_addr) { |
1136 |
+ |
sprintf(rcs, "%d", GetLastError()); |
1137 |
+ |
sprintf(str, GetString(STR_KD2_SHMAT_ERR), rcs); |
1138 |
+ |
ErrorAlert(str); |
1139 |
+ |
return false; |
1140 |
+ |
} |
1141 |
+ |
#else |
1142 |
+ |
kernel_area = shmget(IPC_PRIVATE, KERNEL_AREA_SIZE, 0600); |
1143 |
+ |
if (kernel_area == -1) { |
1144 |
+ |
sprintf(str, GetString(STR_KD_SHMGET_ERR), strerror(errno)); |
1145 |
+ |
ErrorAlert(str); |
1146 |
+ |
return false; |
1147 |
+ |
} |
1148 |
+ |
if (shmat(kernel_area, Mac2HostAddr(KERNEL_DATA_BASE), 0) < 0) { |
1149 |
+ |
sprintf(str, GetString(STR_KD_SHMAT_ERR), strerror(errno)); |
1150 |
+ |
ErrorAlert(str); |
1151 |
+ |
return false; |
1152 |
+ |
} |
1153 |
+ |
if (shmat(kernel_area, Mac2HostAddr(KERNEL_DATA2_BASE), 0) < 0) { |
1154 |
+ |
sprintf(str, GetString(STR_KD2_SHMAT_ERR), strerror(errno)); |
1155 |
+ |
ErrorAlert(str); |
1156 |
+ |
return false; |
1157 |
+ |
} |
1158 |
+ |
#endif |
1159 |
+ |
return true; |
1160 |
+ |
} |
1161 |
+ |
|
1162 |
+ |
|
1163 |
+ |
/* |
1164 |
+ |
* Deallocate Kernel Data segments |
1165 |
+ |
*/ |
1166 |
+ |
|
1167 |
+ |
static void kernel_data_exit(void) |
1168 |
+ |
{ |
1169 |
+ |
#ifdef _WIN32 |
1170 |
+ |
if (kernel_handle) { |
1171 |
+ |
UnmapViewOfFile(Mac2HostAddr(KERNEL_DATA_BASE & -allocation_granule)); |
1172 |
+ |
UnmapViewOfFile(Mac2HostAddr(KERNEL_DATA2_BASE & -allocation_granule)); |
1173 |
+ |
CloseHandle(kernel_handle); |
1174 |
+ |
} |
1175 |
+ |
#else |
1176 |
+ |
if (kernel_area >= 0) { |
1177 |
+ |
shmdt(Mac2HostAddr(KERNEL_DATA_BASE)); |
1178 |
+ |
shmdt(Mac2HostAddr(KERNEL_DATA2_BASE)); |
1179 |
+ |
shmctl(kernel_area, IPC_RMID, NULL); |
1180 |
+ |
} |
1181 |
+ |
#endif |
1182 |
+ |
} |
1183 |
+ |
|
1184 |
+ |
|
1185 |
+ |
/* |
1186 |
|
* Jump into Mac ROM, start 680x0 emulator |
1187 |
|
*/ |
1188 |
|
|
1189 |
|
#if EMULATED_PPC |
903 |
– |
extern void emul_ppc(uint32 start); |
904 |
– |
extern void init_emul_ppc(void); |
1190 |
|
void jump_to_rom(uint32 entry) |
1191 |
|
{ |
1192 |
|
init_emul_ppc(); |
1255 |
|
|
1256 |
|
|
1257 |
|
/* |
973 |
– |
* Execute PPC code from EMUL_OP routine (real mode switch) |
974 |
– |
*/ |
975 |
– |
|
976 |
– |
void ExecutePPC(void (*func)()) |
977 |
– |
{ |
978 |
– |
uint32 tvect[2] = {(uint32)func, 0}; // Fake TVECT |
979 |
– |
RoutineDescriptor desc = BUILD_PPC_ROUTINE_DESCRIPTOR(0, tvect); |
980 |
– |
M68kRegisters r; |
981 |
– |
Execute68k((uint32)&desc, &r); |
982 |
– |
} |
983 |
– |
|
984 |
– |
|
985 |
– |
/* |
1258 |
|
* Quit emulator (cause return from jump_to_rom) |
1259 |
|
*/ |
1260 |
|
|
1269 |
|
|
1270 |
|
|
1271 |
|
/* |
1000 |
– |
* Pause/resume emulator |
1001 |
– |
*/ |
1002 |
– |
|
1003 |
– |
void PauseEmulator(void) |
1004 |
– |
{ |
1005 |
– |
pthread_kill(emul_thread, SIGSTOP); |
1006 |
– |
} |
1007 |
– |
|
1008 |
– |
void ResumeEmulator(void) |
1009 |
– |
{ |
1010 |
– |
pthread_kill(emul_thread, SIGCONT); |
1011 |
– |
} |
1012 |
– |
|
1013 |
– |
|
1014 |
– |
/* |
1272 |
|
* Dump 68k registers |
1273 |
|
*/ |
1274 |
|
|
1296 |
|
* Make code executable |
1297 |
|
*/ |
1298 |
|
|
1299 |
< |
void MakeExecutable(int dummy, void *start, uint32 length) |
1299 |
> |
void MakeExecutable(int dummy, uint32 start, uint32 length) |
1300 |
|
{ |
1301 |
< |
#if !EMULATED_PPC |
1045 |
< |
if (((uint32)start >= ROM_BASE) && ((uint32)start < (ROM_BASE + ROM_SIZE))) |
1301 |
> |
if ((start >= ROM_BASE) && (start < (ROM_BASE + ROM_SIZE))) |
1302 |
|
return; |
1303 |
< |
flush_icache_range(start, (void *)((uint32)start + length)); |
1303 |
> |
#if EMULATED_PPC |
1304 |
> |
FlushCodeCache(start, start + length); |
1305 |
> |
#else |
1306 |
> |
flush_icache_range(start, start + length); |
1307 |
|
#endif |
1308 |
|
} |
1309 |
|
|
1310 |
|
|
1311 |
|
/* |
1312 |
< |
* Patch things after system startup (gets called by disk driver accRun routine) |
1312 |
> |
* NVRAM watchdog thread (saves NVRAM every minute) |
1313 |
|
*/ |
1314 |
|
|
1315 |
< |
void PatchAfterStartup(void) |
1315 |
> |
static void nvram_watchdog(void) |
1316 |
|
{ |
1317 |
< |
ExecutePPC(VideoInstallAccel); |
1318 |
< |
InstallExtFS(); |
1317 |
> |
if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) { |
1318 |
> |
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
1319 |
> |
SaveXPRAM(); |
1320 |
> |
} |
1321 |
|
} |
1322 |
|
|
1062 |
– |
|
1063 |
– |
/* |
1064 |
– |
* NVRAM watchdog thread (saves NVRAM every minute) |
1065 |
– |
*/ |
1066 |
– |
|
1323 |
|
static void *nvram_func(void *arg) |
1324 |
|
{ |
1325 |
< |
struct timespec req = {60, 0}; // 1 minute |
1326 |
< |
|
1327 |
< |
for (;;) { |
1328 |
< |
pthread_testcancel(); |
1073 |
< |
nanosleep(&req, NULL); |
1074 |
< |
pthread_testcancel(); |
1075 |
< |
if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) { |
1076 |
< |
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
1077 |
< |
SaveXPRAM(); |
1078 |
< |
} |
1325 |
> |
while (!nvram_thread_cancel) { |
1326 |
> |
for (int i=0; i<60 && !nvram_thread_cancel; i++) |
1327 |
> |
Delay_usec(999999); // Only wait 1 second so we quit promptly when nvram_thread_cancel becomes true |
1328 |
> |
nvram_watchdog(); |
1329 |
|
} |
1330 |
|
return NULL; |
1331 |
|
} |
1338 |
|
static void *tick_func(void *arg) |
1339 |
|
{ |
1340 |
|
int tick_counter = 0; |
1341 |
< |
struct timespec req = {0, 16625000}; |
1341 |
> |
uint64 start = GetTicks_usec(); |
1342 |
> |
int64 ticks = 0; |
1343 |
> |
uint64 next = GetTicks_usec(); |
1344 |
|
|
1345 |
< |
for (;;) { |
1345 |
> |
while (!tick_thread_cancel) { |
1346 |
|
|
1347 |
|
// Wait |
1348 |
< |
nanosleep(&req, NULL); |
1348 |
> |
next += 16625; |
1349 |
> |
int64 delay = next - GetTicks_usec(); |
1350 |
> |
if (delay > 0) |
1351 |
> |
Delay_usec(delay); |
1352 |
> |
else if (delay < -16625) |
1353 |
> |
next = GetTicks_usec(); |
1354 |
> |
ticks++; |
1355 |
|
|
1356 |
|
#if !EMULATED_PPC |
1357 |
|
// Did we crash? |
1358 |
|
if (emul_thread_fatal) { |
1359 |
|
|
1360 |
|
// Yes, dump registers |
1361 |
< |
pt_regs *r = (pt_regs *)&sigsegv_regs; |
1361 |
> |
sigregs *r = &sigsegv_regs; |
1362 |
|
char str[256]; |
1363 |
< |
sprintf(str, "SIGSEGV\n" |
1363 |
> |
if (crash_reason == NULL) |
1364 |
> |
crash_reason = "SIGSEGV"; |
1365 |
> |
sprintf(str, "%s\n" |
1366 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1367 |
|
" xer %08lx cr %08lx \n" |
1368 |
|
" r0 %08lx r1 %08lx r2 %08lx r3 %08lx\n" |
1373 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1374 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1375 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1376 |
+ |
crash_reason, |
1377 |
|
r->nip, r->link, r->ctr, r->msr, |
1378 |
|
r->xer, r->ccr, |
1379 |
|
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1409 |
|
TriggerInterrupt(); |
1410 |
|
} |
1411 |
|
} |
1412 |
+ |
|
1413 |
+ |
uint64 end = GetTicks_usec(); |
1414 |
+ |
D(bug("%Ld ticks in %Ld usec = %f ticks/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start))); |
1415 |
|
return NULL; |
1416 |
|
} |
1417 |
|
|
1422 |
|
|
1423 |
|
void Set_pthread_attr(pthread_attr_t *attr, int priority) |
1424 |
|
{ |
1425 |
< |
// nothing to do |
1425 |
> |
#ifdef HAVE_PTHREADS |
1426 |
> |
pthread_attr_init(attr); |
1427 |
> |
#if defined(_POSIX_THREAD_PRIORITY_SCHEDULING) |
1428 |
> |
// Some of these only work for superuser |
1429 |
> |
if (geteuid() == 0) { |
1430 |
> |
pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED); |
1431 |
> |
pthread_attr_setschedpolicy(attr, SCHED_FIFO); |
1432 |
> |
struct sched_param fifo_param; |
1433 |
> |
fifo_param.sched_priority = ((sched_get_priority_min(SCHED_FIFO) + |
1434 |
> |
sched_get_priority_max(SCHED_FIFO)) / 2 + |
1435 |
> |
priority); |
1436 |
> |
pthread_attr_setschedparam(attr, &fifo_param); |
1437 |
> |
} |
1438 |
> |
if (pthread_attr_setscope(attr, PTHREAD_SCOPE_SYSTEM) != 0) { |
1439 |
> |
#ifdef PTHREAD_SCOPE_BOUND_NP |
1440 |
> |
// If system scope is not available (eg. we're not running |
1441 |
> |
// with CAP_SCHED_MGT capability on an SGI box), try bound |
1442 |
> |
// scope. It exposes pthread scheduling to the kernel, |
1443 |
> |
// without setting realtime priority. |
1444 |
> |
pthread_attr_setscope(attr, PTHREAD_SCOPE_BOUND_NP); |
1445 |
> |
#endif |
1446 |
> |
} |
1447 |
> |
#endif |
1448 |
> |
#endif |
1449 |
|
} |
1450 |
|
|
1451 |
|
|
1453 |
|
* Mutexes |
1454 |
|
*/ |
1455 |
|
|
1456 |
+ |
#ifdef HAVE_PTHREADS |
1457 |
+ |
|
1458 |
+ |
struct B2_mutex { |
1459 |
+ |
B2_mutex() { |
1460 |
+ |
pthread_mutexattr_t attr; |
1461 |
+ |
pthread_mutexattr_init(&attr); |
1462 |
+ |
// Initialize the mutex for priority inheritance -- |
1463 |
+ |
// required for accurate timing. |
1464 |
+ |
#if defined(HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL) && !defined(__CYGWIN__) |
1465 |
+ |
pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT); |
1466 |
+ |
#endif |
1467 |
+ |
#if defined(HAVE_PTHREAD_MUTEXATTR_SETTYPE) && defined(PTHREAD_MUTEX_NORMAL) |
1468 |
+ |
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); |
1469 |
+ |
#endif |
1470 |
+ |
#ifdef HAVE_PTHREAD_MUTEXATTR_SETPSHARED |
1471 |
+ |
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE); |
1472 |
+ |
#endif |
1473 |
+ |
pthread_mutex_init(&m, &attr); |
1474 |
+ |
pthread_mutexattr_destroy(&attr); |
1475 |
+ |
} |
1476 |
+ |
~B2_mutex() { |
1477 |
+ |
pthread_mutex_trylock(&m); // Make sure it's locked before |
1478 |
+ |
pthread_mutex_unlock(&m); // unlocking it. |
1479 |
+ |
pthread_mutex_destroy(&m); |
1480 |
+ |
} |
1481 |
+ |
pthread_mutex_t m; |
1482 |
+ |
}; |
1483 |
+ |
|
1484 |
+ |
B2_mutex *B2_create_mutex(void) |
1485 |
+ |
{ |
1486 |
+ |
return new B2_mutex; |
1487 |
+ |
} |
1488 |
+ |
|
1489 |
+ |
void B2_lock_mutex(B2_mutex *mutex) |
1490 |
+ |
{ |
1491 |
+ |
pthread_mutex_lock(&mutex->m); |
1492 |
+ |
} |
1493 |
+ |
|
1494 |
+ |
void B2_unlock_mutex(B2_mutex *mutex) |
1495 |
+ |
{ |
1496 |
+ |
pthread_mutex_unlock(&mutex->m); |
1497 |
+ |
} |
1498 |
+ |
|
1499 |
+ |
void B2_delete_mutex(B2_mutex *mutex) |
1500 |
+ |
{ |
1501 |
+ |
delete mutex; |
1502 |
+ |
} |
1503 |
+ |
|
1504 |
+ |
#else |
1505 |
+ |
|
1506 |
|
struct B2_mutex { |
1507 |
|
int dummy; |
1508 |
|
}; |
1525 |
|
delete mutex; |
1526 |
|
} |
1527 |
|
|
1528 |
+ |
#endif |
1529 |
+ |
|
1530 |
|
|
1531 |
|
/* |
1532 |
|
* Trigger signal USR2 from another thread |
1533 |
|
*/ |
1534 |
|
|
1535 |
+ |
#if !EMULATED_PPC |
1536 |
|
void TriggerInterrupt(void) |
1537 |
|
{ |
1198 |
– |
#if EMULATED_PPC |
1199 |
– |
WriteMacInt32(0x16a, ReadMacInt32(0x16a) + 1); |
1200 |
– |
#else |
1201 |
– |
#if 0 |
1202 |
– |
WriteMacInt32(0x16a, ReadMacInt32(0x16a) + 1); |
1203 |
– |
#else |
1538 |
|
if (ready_for_signals) |
1539 |
|
pthread_kill(emul_thread, SIGUSR2); |
1206 |
– |
#endif |
1207 |
– |
#endif |
1540 |
|
} |
1541 |
+ |
#endif |
1542 |
|
|
1543 |
|
|
1544 |
|
/* |
1564 |
|
|
1565 |
|
void DisableInterrupt(void) |
1566 |
|
{ |
1567 |
+ |
#if EMULATED_PPC |
1568 |
+ |
WriteMacInt32(XLM_IRQ_NEST, int32(ReadMacInt32(XLM_IRQ_NEST)) + 1); |
1569 |
+ |
#else |
1570 |
|
atomic_add((int *)XLM_IRQ_NEST, 1); |
1571 |
+ |
#endif |
1572 |
|
} |
1573 |
|
|
1574 |
|
|
1578 |
|
|
1579 |
|
void EnableInterrupt(void) |
1580 |
|
{ |
1581 |
+ |
#if EMULATED_PPC |
1582 |
+ |
WriteMacInt32(XLM_IRQ_NEST, int32(ReadMacInt32(XLM_IRQ_NEST)) - 1); |
1583 |
+ |
#else |
1584 |
|
atomic_add((int *)XLM_IRQ_NEST, -1); |
1585 |
+ |
#endif |
1586 |
|
} |
1587 |
|
|
1588 |
|
|
1248 |
– |
#if !EMULATED_PPC |
1589 |
|
/* |
1590 |
|
* USR2 handler |
1591 |
|
*/ |
1592 |
|
|
1593 |
< |
static void sigusr2_handler(int sig, sigcontext_struct *sc) |
1593 |
> |
#if !EMULATED_PPC |
1594 |
> |
static void sigusr2_handler(int sig, siginfo_t *sip, void *scp) |
1595 |
|
{ |
1596 |
< |
pt_regs *r = sc->regs; |
1596 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1597 |
> |
|
1598 |
> |
#ifdef USE_SDL_VIDEO |
1599 |
> |
// We must fill in the events queue in the same thread that did call SDL_SetVideoMode() |
1600 |
> |
SDL_PumpEvents(); |
1601 |
> |
#endif |
1602 |
|
|
1603 |
|
// Do nothing if interrupts are disabled |
1604 |
|
if (*(int32 *)XLM_IRQ_NEST > 0) |
1605 |
|
return; |
1606 |
|
|
1607 |
+ |
#ifdef SYSTEM_CLOBBERS_R2 |
1608 |
+ |
// Restore pointer to Thread Local Storage |
1609 |
+ |
set_r2(TOC); |
1610 |
+ |
#endif |
1611 |
+ |
#ifdef SYSTEM_CLOBBERS_R13 |
1612 |
+ |
// Restore pointer to .sdata section |
1613 |
+ |
set_r13(R13); |
1614 |
+ |
#endif |
1615 |
+ |
|
1616 |
|
// Disable MacOS stack sniffer |
1617 |
|
WriteMacInt32(0x110, 0); |
1618 |
|
|
1621 |
|
case MODE_68K: |
1622 |
|
// 68k emulator active, trigger 68k interrupt level 1 |
1623 |
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1624 |
< |
r->ccr |= ntohl(kernel_data->v[0x674 >> 2]); |
1624 |
> |
r->cr() |= ntohl(kernel_data->v[0x674 >> 2]); |
1625 |
|
break; |
1626 |
|
|
1627 |
|
#if INTERRUPTS_IN_NATIVE_MODE |
1628 |
|
case MODE_NATIVE: |
1629 |
|
// 68k emulator inactive, in nanokernel? |
1630 |
< |
if (r->gpr[1] != KernelDataAddr) { |
1630 |
> |
if (r->gpr(1) != KernelDataAddr) { |
1631 |
> |
|
1632 |
> |
// Set extra stack for nested interrupts |
1633 |
> |
sig_stack_acquire(); |
1634 |
> |
|
1635 |
|
// Prepare for 68k interrupt level 1 |
1636 |
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1637 |
|
WriteMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc, ReadMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc) | ntohl(kernel_data->v[0x674 >> 2])); |
1638 |
|
|
1639 |
|
// Execute nanokernel interrupt routine (this will activate the 68k emulator) |
1640 |
< |
atomic_add((int32 *)XLM_IRQ_NEST, 1); |
1640 |
> |
DisableInterrupt(); |
1641 |
|
if (ROMType == ROMTYPE_NEWWORLD) |
1642 |
|
ppc_interrupt(ROM_BASE + 0x312b1c, KernelDataAddr); |
1643 |
|
else |
1644 |
|
ppc_interrupt(ROM_BASE + 0x312a3c, KernelDataAddr); |
1645 |
+ |
|
1646 |
+ |
// Reset normal signal stack |
1647 |
+ |
sig_stack_release(); |
1648 |
|
} |
1649 |
|
break; |
1650 |
|
#endif |
1655 |
|
if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) { |
1656 |
|
|
1657 |
|
// Set extra stack for SIGSEGV handler |
1658 |
< |
struct sigaltstack new_stack; |
1297 |
< |
new_stack.ss_sp = extra_stack; |
1298 |
< |
new_stack.ss_flags = 0; |
1299 |
< |
new_stack.ss_size = SIG_STACK_SIZE; |
1300 |
< |
sigaltstack(&new_stack, NULL); |
1658 |
> |
sig_stack_acquire(); |
1659 |
|
#if 1 |
1660 |
|
// Execute full 68k interrupt routine |
1661 |
|
M68kRegisters r; |
1677 |
|
if (InterruptFlags & INTFLAG_VIA) { |
1678 |
|
ClearInterruptFlag(INTFLAG_VIA); |
1679 |
|
ADBInterrupt(); |
1680 |
< |
ExecutePPC(VideoVBL); |
1680 |
> |
ExecuteNative(NATIVE_VIDEO_VBL); |
1681 |
|
} |
1682 |
|
} |
1683 |
|
#endif |
1684 |
|
// Reset normal signal stack |
1685 |
< |
new_stack.ss_sp = sig_stack; |
1328 |
< |
new_stack.ss_flags = 0; |
1329 |
< |
new_stack.ss_size = SIG_STACK_SIZE; |
1330 |
< |
sigaltstack(&new_stack, NULL); |
1685 |
> |
sig_stack_release(); |
1686 |
|
} |
1687 |
|
break; |
1688 |
|
#endif |
1334 |
– |
|
1689 |
|
} |
1690 |
|
} |
1691 |
+ |
#endif |
1692 |
|
|
1693 |
|
|
1694 |
|
/* |
1695 |
|
* SIGSEGV handler |
1696 |
|
*/ |
1697 |
|
|
1698 |
< |
static void sigsegv_handler(int sig, sigcontext_struct *sc) |
1698 |
> |
#if !EMULATED_PPC |
1699 |
> |
static void sigsegv_handler(int sig, siginfo_t *sip, void *scp) |
1700 |
|
{ |
1701 |
< |
pt_regs *r = sc->regs; |
1701 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1702 |
> |
|
1703 |
> |
// Get effective address |
1704 |
> |
uint32 addr = r->dar(); |
1705 |
> |
|
1706 |
> |
#ifdef SYSTEM_CLOBBERS_R2 |
1707 |
> |
// Restore pointer to Thread Local Storage |
1708 |
> |
set_r2(TOC); |
1709 |
> |
#endif |
1710 |
> |
#ifdef SYSTEM_CLOBBERS_R13 |
1711 |
> |
// Restore pointer to .sdata section |
1712 |
> |
set_r13(R13); |
1713 |
> |
#endif |
1714 |
> |
|
1715 |
> |
#if ENABLE_VOSF |
1716 |
> |
// Handle screen fault. |
1717 |
> |
extern bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction); |
1718 |
> |
if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->pc())) |
1719 |
> |
return; |
1720 |
> |
#endif |
1721 |
> |
|
1722 |
|
num_segv++; |
1723 |
|
|
1724 |
< |
// Fault in Mac ROM or RAM? |
1725 |
< |
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
1724 |
> |
// Fault in Mac ROM or RAM or DR Cache? |
1725 |
> |
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)); |
1726 |
|
if (mac_fault) { |
1727 |
|
|
1352 |
– |
// Get opcode and divide into fields |
1353 |
– |
uint32 opcode = *((uint32 *)r->nip); |
1354 |
– |
uint32 primop = opcode >> 26; |
1355 |
– |
uint32 exop = (opcode >> 1) & 0x3ff; |
1356 |
– |
uint32 ra = (opcode >> 16) & 0x1f; |
1357 |
– |
uint32 rb = (opcode >> 11) & 0x1f; |
1358 |
– |
uint32 rd = (opcode >> 21) & 0x1f; |
1359 |
– |
int32 imm = (int16)(opcode & 0xffff); |
1360 |
– |
|
1728 |
|
// "VM settings" during MacOS 8 installation |
1729 |
< |
if (r->nip == ROM_BASE + 0x488160 && r->gpr[20] == 0xf8000000) { |
1730 |
< |
r->nip += 4; |
1731 |
< |
r->gpr[8] = 0; |
1729 |
> |
if (r->pc() == ROM_BASE + 0x488160 && r->gpr(20) == 0xf8000000) { |
1730 |
> |
r->pc() += 4; |
1731 |
> |
r->gpr(8) = 0; |
1732 |
|
return; |
1733 |
|
|
1734 |
|
// MacOS 8.5 installation |
1735 |
< |
} else if (r->nip == ROM_BASE + 0x488140 && r->gpr[16] == 0xf8000000) { |
1736 |
< |
r->nip += 4; |
1737 |
< |
r->gpr[8] = 0; |
1735 |
> |
} else if (r->pc() == ROM_BASE + 0x488140 && r->gpr(16) == 0xf8000000) { |
1736 |
> |
r->pc() += 4; |
1737 |
> |
r->gpr(8) = 0; |
1738 |
|
return; |
1739 |
|
|
1740 |
|
// MacOS 8 serial drivers on startup |
1741 |
< |
} else if (r->nip == ROM_BASE + 0x48e080 && (r->gpr[8] == 0xf3012002 || r->gpr[8] == 0xf3012000)) { |
1742 |
< |
r->nip += 4; |
1743 |
< |
r->gpr[8] = 0; |
1741 |
> |
} else if (r->pc() == ROM_BASE + 0x48e080 && (r->gpr(8) == 0xf3012002 || r->gpr(8) == 0xf3012000)) { |
1742 |
> |
r->pc() += 4; |
1743 |
> |
r->gpr(8) = 0; |
1744 |
|
return; |
1745 |
|
|
1746 |
|
// MacOS 8.1 serial drivers on startup |
1747 |
< |
} else if (r->nip == ROM_BASE + 0x48c5e0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1748 |
< |
r->nip += 4; |
1747 |
> |
} else if (r->pc() == ROM_BASE + 0x48c5e0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1748 |
> |
r->pc() += 4; |
1749 |
|
return; |
1750 |
< |
} else if (r->nip == ROM_BASE + 0x4a10a0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1751 |
< |
r->nip += 4; |
1750 |
> |
} else if (r->pc() == ROM_BASE + 0x4a10a0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1751 |
> |
r->pc() += 4; |
1752 |
> |
return; |
1753 |
> |
|
1754 |
> |
// MacOS 8.6 serial drivers on startup (with DR Cache and OldWorld ROM) |
1755 |
> |
} else if ((r->pc() - DR_CACHE_BASE) < DR_CACHE_SIZE && (r->gpr(16) == 0xf3012002 || r->gpr(16) == 0xf3012000)) { |
1756 |
> |
r->pc() += 4; |
1757 |
> |
return; |
1758 |
> |
} else if ((r->pc() - DR_CACHE_BASE) < DR_CACHE_SIZE && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1759 |
> |
r->pc() += 4; |
1760 |
|
return; |
1761 |
|
} |
1762 |
|
|
1763 |
+ |
// Get opcode and divide into fields |
1764 |
+ |
uint32 opcode = *((uint32 *)r->pc()); |
1765 |
+ |
uint32 primop = opcode >> 26; |
1766 |
+ |
uint32 exop = (opcode >> 1) & 0x3ff; |
1767 |
+ |
uint32 ra = (opcode >> 16) & 0x1f; |
1768 |
+ |
uint32 rb = (opcode >> 11) & 0x1f; |
1769 |
+ |
uint32 rd = (opcode >> 21) & 0x1f; |
1770 |
+ |
int32 imm = (int16)(opcode & 0xffff); |
1771 |
+ |
|
1772 |
|
// Analyze opcode |
1773 |
|
enum { |
1774 |
|
TYPE_UNKNOWN, |
1850 |
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_NORM; break; |
1851 |
|
case 45: // sthu |
1852 |
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_U; break; |
1853 |
< |
} |
1854 |
< |
|
1855 |
< |
// Calculate effective address |
1856 |
< |
uint32 addr = 0; |
1857 |
< |
switch (addr_mode) { |
1858 |
< |
case MODE_X: |
1859 |
< |
case MODE_UX: |
1860 |
< |
if (ra == 0) |
1861 |
< |
addr = r->gpr[rb]; |
1862 |
< |
else |
1863 |
< |
addr = r->gpr[ra] + r->gpr[rb]; |
1864 |
< |
break; |
1481 |
< |
case MODE_NORM: |
1482 |
< |
case MODE_U: |
1483 |
< |
if (ra == 0) |
1484 |
< |
addr = (int32)(int16)imm; |
1485 |
< |
else |
1486 |
< |
addr = r->gpr[ra] + (int32)(int16)imm; |
1853 |
> |
#if EMULATE_UNALIGNED_LOADSTORE_MULTIPLE |
1854 |
> |
case 46: // lmw |
1855 |
> |
if ((addr % 4) != 0) { |
1856 |
> |
uint32 ea = addr; |
1857 |
> |
D(bug("WARNING: unaligned lmw to EA=%08x from IP=%08x\n", ea, r->pc())); |
1858 |
> |
for (int i = rd; i <= 31; i++) { |
1859 |
> |
r->gpr(i) = ReadMacInt32(ea); |
1860 |
> |
ea += 4; |
1861 |
> |
} |
1862 |
> |
r->pc() += 4; |
1863 |
> |
goto rti; |
1864 |
> |
} |
1865 |
|
break; |
1866 |
< |
default: |
1866 |
> |
case 47: // stmw |
1867 |
> |
if ((addr % 4) != 0) { |
1868 |
> |
uint32 ea = addr; |
1869 |
> |
D(bug("WARNING: unaligned stmw to EA=%08x from IP=%08x\n", ea, r->pc())); |
1870 |
> |
for (int i = rd; i <= 31; i++) { |
1871 |
> |
WriteMacInt32(ea, r->gpr(i)); |
1872 |
> |
ea += 4; |
1873 |
> |
} |
1874 |
> |
r->pc() += 4; |
1875 |
> |
goto rti; |
1876 |
> |
} |
1877 |
|
break; |
1878 |
+ |
#endif |
1879 |
|
} |
1880 |
< |
|
1881 |
< |
// Ignore ROM writes |
1882 |
< |
if (transfer_type == TYPE_STORE && addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) { |
1883 |
< |
// 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)); |
1880 |
> |
|
1881 |
> |
// Ignore ROM writes (including to the zero page, which is read-only) |
1882 |
> |
if (transfer_type == TYPE_STORE && |
1883 |
> |
((addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) || |
1884 |
> |
(addr >= SheepMem::ZeroPage() && addr < SheepMem::ZeroPage() + SheepMem::PageSize()))) { |
1885 |
> |
// 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())); |
1886 |
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1887 |
< |
r->gpr[ra] = addr; |
1888 |
< |
r->nip += 4; |
1887 |
> |
r->gpr(ra) = addr; |
1888 |
> |
r->pc() += 4; |
1889 |
|
goto rti; |
1890 |
|
} |
1891 |
|
|
1892 |
|
// Ignore illegal memory accesses? |
1893 |
|
if (PrefsFindBool("ignoresegv")) { |
1894 |
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1895 |
< |
r->gpr[ra] = addr; |
1895 |
> |
r->gpr(ra) = addr; |
1896 |
|
if (transfer_type == TYPE_LOAD) |
1897 |
< |
r->gpr[rd] = 0; |
1898 |
< |
r->nip += 4; |
1897 |
> |
r->gpr(rd) = 0; |
1898 |
> |
r->pc() += 4; |
1899 |
|
goto rti; |
1900 |
|
} |
1901 |
|
|
1903 |
|
if (!PrefsFindBool("nogui")) { |
1904 |
|
char str[256]; |
1905 |
|
if (transfer_type == TYPE_LOAD || transfer_type == TYPE_STORE) |
1906 |
< |
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]); |
1906 |
> |
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)); |
1907 |
|
else |
1908 |
< |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode); |
1908 |
> |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode); |
1909 |
|
ErrorAlert(str); |
1910 |
|
QuitEmulator(); |
1911 |
|
return; |
1913 |
|
} |
1914 |
|
|
1915 |
|
// For all other errors, jump into debugger (sort of...) |
1916 |
+ |
crash_reason = (sig == SIGBUS) ? "SIGBUS" : "SIGSEGV"; |
1917 |
|
if (!ready_for_signals) { |
1918 |
< |
printf("SIGSEGV\n"); |
1919 |
< |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
1918 |
> |
printf("%s\n"); |
1919 |
> |
printf(" sigcontext %p, machine_regs %p\n", scp, r); |
1920 |
|
printf( |
1921 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1922 |
|
" xer %08lx cr %08lx \n" |
1928 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1929 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1930 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1931 |
< |
r->nip, r->link, r->ctr, r->msr, |
1932 |
< |
r->xer, r->ccr, |
1933 |
< |
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1934 |
< |
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
1935 |
< |
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
1936 |
< |
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
1937 |
< |
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
1938 |
< |
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
1939 |
< |
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
1940 |
< |
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
1931 |
> |
crash_reason, |
1932 |
> |
r->pc(), r->lr(), r->ctr(), r->msr(), |
1933 |
> |
r->xer(), r->cr(), |
1934 |
> |
r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3), |
1935 |
> |
r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7), |
1936 |
> |
r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11), |
1937 |
> |
r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15), |
1938 |
> |
r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19), |
1939 |
> |
r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23), |
1940 |
> |
r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27), |
1941 |
> |
r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31)); |
1942 |
|
exit(1); |
1943 |
|
QuitEmulator(); |
1944 |
|
return; |
1945 |
|
} else { |
1946 |
|
// We crashed. Save registers, tell tick thread and loop forever |
1947 |
< |
sigsegv_regs = *(sigregs *)r; |
1947 |
> |
build_sigregs(&sigsegv_regs, r); |
1948 |
|
emul_thread_fatal = true; |
1949 |
|
for (;;) ; |
1950 |
|
} |
1956 |
|
* SIGILL handler |
1957 |
|
*/ |
1958 |
|
|
1959 |
< |
static void sigill_handler(int sig, sigcontext_struct *sc) |
1959 |
> |
static void sigill_handler(int sig, siginfo_t *sip, void *scp) |
1960 |
|
{ |
1961 |
< |
pt_regs *r = sc->regs; |
1961 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1962 |
|
char str[256]; |
1963 |
|
|
1964 |
+ |
#ifdef SYSTEM_CLOBBERS_R2 |
1965 |
+ |
// Restore pointer to Thread Local Storage |
1966 |
+ |
set_r2(TOC); |
1967 |
+ |
#endif |
1968 |
+ |
#ifdef SYSTEM_CLOBBERS_R13 |
1969 |
+ |
// Restore pointer to .sdata section |
1970 |
+ |
set_r13(R13); |
1971 |
+ |
#endif |
1972 |
+ |
|
1973 |
|
// Fault in Mac ROM or RAM? |
1974 |
< |
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
1974 |
> |
bool mac_fault = (r->pc() >= ROM_BASE) && (r->pc() < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize)); |
1975 |
|
if (mac_fault) { |
1976 |
|
|
1977 |
|
// Get opcode and divide into fields |
1978 |
< |
uint32 opcode = *((uint32 *)r->nip); |
1978 |
> |
uint32 opcode = *((uint32 *)r->pc()); |
1979 |
|
uint32 primop = opcode >> 26; |
1980 |
|
uint32 exop = (opcode >> 1) & 0x3ff; |
1981 |
|
uint32 ra = (opcode >> 16) & 0x1f; |
1986 |
|
switch (primop) { |
1987 |
|
case 9: // POWER instructions |
1988 |
|
case 22: |
1989 |
< |
power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->nip, r->gpr[1], opcode); |
1989 |
> |
power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->pc(), r->gpr(1), opcode); |
1990 |
|
ErrorAlert(str); |
1991 |
|
QuitEmulator(); |
1992 |
|
return; |
1994 |
|
case 31: |
1995 |
|
switch (exop) { |
1996 |
|
case 83: // mfmsr |
1997 |
< |
r->gpr[rd] = 0xf072; |
1998 |
< |
r->nip += 4; |
1997 |
> |
r->gpr(rd) = 0xf072; |
1998 |
> |
r->pc() += 4; |
1999 |
|
goto rti; |
2000 |
|
|
2001 |
|
case 210: // mtsr |
2002 |
|
case 242: // mtsrin |
2003 |
|
case 306: // tlbie |
2004 |
< |
r->nip += 4; |
2004 |
> |
r->pc() += 4; |
2005 |
|
goto rti; |
2006 |
|
|
2007 |
|
case 339: { // mfspr |
2017 |
|
case 957: // PMC3 |
2018 |
|
case 958: // PMC4 |
2019 |
|
case 959: // SDA |
2020 |
< |
r->nip += 4; |
2020 |
> |
r->pc() += 4; |
2021 |
|
goto rti; |
2022 |
|
case 25: // SDR1 |
2023 |
< |
r->gpr[rd] = 0xdead001f; |
2024 |
< |
r->nip += 4; |
2023 |
> |
r->gpr(rd) = 0xdead001f; |
2024 |
> |
r->pc() += 4; |
2025 |
|
goto rti; |
2026 |
|
case 287: // PVR |
2027 |
< |
r->gpr[rd] = PVR; |
2028 |
< |
r->nip += 4; |
2027 |
> |
r->gpr(rd) = PVR; |
2028 |
> |
r->pc() += 4; |
2029 |
|
goto rti; |
2030 |
|
} |
2031 |
|
break; |
2061 |
|
case 957: // PMC3 |
2062 |
|
case 958: // PMC4 |
2063 |
|
case 959: // SDA |
2064 |
< |
r->nip += 4; |
2064 |
> |
r->pc() += 4; |
2065 |
|
goto rti; |
2066 |
|
} |
2067 |
|
break; |
2080 |
|
|
2081 |
|
// In GUI mode, show error alert |
2082 |
|
if (!PrefsFindBool("nogui")) { |
2083 |
< |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode); |
2083 |
> |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode); |
2084 |
|
ErrorAlert(str); |
2085 |
|
QuitEmulator(); |
2086 |
|
return; |
2088 |
|
} |
2089 |
|
|
2090 |
|
// For all other errors, jump into debugger (sort of...) |
2091 |
+ |
crash_reason = "SIGILL"; |
2092 |
|
if (!ready_for_signals) { |
2093 |
< |
printf("SIGILL\n"); |
2094 |
< |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
2093 |
> |
printf("%s\n"); |
2094 |
> |
printf(" sigcontext %p, machine_regs %p\n", scp, r); |
2095 |
|
printf( |
2096 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
2097 |
|
" xer %08lx cr %08lx \n" |
2103 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
2104 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
2105 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
2106 |
< |
r->nip, r->link, r->ctr, r->msr, |
2107 |
< |
r->xer, r->ccr, |
2108 |
< |
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
2109 |
< |
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
2110 |
< |
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
2111 |
< |
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
2112 |
< |
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
2113 |
< |
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
2114 |
< |
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
2115 |
< |
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
2106 |
> |
crash_reason, |
2107 |
> |
r->pc(), r->lr(), r->ctr(), r->msr(), |
2108 |
> |
r->xer(), r->cr(), |
2109 |
> |
r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3), |
2110 |
> |
r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7), |
2111 |
> |
r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11), |
2112 |
> |
r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15), |
2113 |
> |
r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19), |
2114 |
> |
r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23), |
2115 |
> |
r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27), |
2116 |
> |
r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31)); |
2117 |
|
exit(1); |
2118 |
|
QuitEmulator(); |
2119 |
|
return; |
2120 |
|
} else { |
2121 |
|
// We crashed. Save registers, tell tick thread and loop forever |
2122 |
< |
sigsegv_regs = *(sigregs *)r; |
2122 |
> |
build_sigregs(&sigsegv_regs, r); |
2123 |
|
emul_thread_fatal = true; |
2124 |
|
for (;;) ; |
2125 |
|
} |
2129 |
|
|
2130 |
|
|
2131 |
|
/* |
2132 |
+ |
* Helpers to share 32-bit addressable data with MacOS |
2133 |
+ |
*/ |
2134 |
+ |
|
2135 |
+ |
bool SheepMem::Init(void) |
2136 |
+ |
{ |
2137 |
+ |
// Size of a native page |
2138 |
+ |
page_size = getpagesize(); |
2139 |
+ |
|
2140 |
+ |
// Allocate SheepShaver globals |
2141 |
+ |
proc = base; |
2142 |
+ |
if (vm_mac_acquire(base, size) < 0) |
2143 |
+ |
return false; |
2144 |
+ |
|
2145 |
+ |
// Allocate page with all bits set to 0, right in the middle |
2146 |
+ |
// This is also used to catch undesired overlaps between proc and data areas |
2147 |
+ |
zero_page = proc + (size / 2); |
2148 |
+ |
Mac_memset(zero_page, 0, page_size); |
2149 |
+ |
if (vm_protect(Mac2HostAddr(zero_page), page_size, VM_PAGE_READ) < 0) |
2150 |
+ |
return false; |
2151 |
+ |
|
2152 |
+ |
#if EMULATED_PPC |
2153 |
+ |
// Allocate alternate stack for PowerPC interrupt routine |
2154 |
+ |
sig_stack = base + size; |
2155 |
+ |
if (vm_mac_acquire(sig_stack, SIG_STACK_SIZE) < 0) |
2156 |
+ |
return false; |
2157 |
+ |
#endif |
2158 |
+ |
|
2159 |
+ |
data = base + size; |
2160 |
+ |
return true; |
2161 |
+ |
} |
2162 |
+ |
|
2163 |
+ |
void SheepMem::Exit(void) |
2164 |
+ |
{ |
2165 |
+ |
if (data) { |
2166 |
+ |
// Delete SheepShaver globals |
2167 |
+ |
vm_mac_release(base, size); |
2168 |
+ |
|
2169 |
+ |
#if EMULATED_PPC |
2170 |
+ |
// Delete alternate stack for PowerPC interrupt routine |
2171 |
+ |
vm_mac_release(sig_stack, SIG_STACK_SIZE); |
2172 |
+ |
#endif |
2173 |
+ |
} |
2174 |
+ |
} |
2175 |
+ |
|
2176 |
+ |
|
2177 |
+ |
/* |
2178 |
|
* Display alert |
2179 |
|
*/ |
2180 |
|
|
2223 |
|
|
2224 |
|
void ErrorAlert(const char *text) |
2225 |
|
{ |
2226 |
< |
#ifdef ENABLE_GTK |
2226 |
> |
#if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO) |
2227 |
|
if (PrefsFindBool("nogui") || x_display == NULL) { |
2228 |
|
printf(GetString(STR_SHELL_ERROR_PREFIX), text); |
2229 |
|
return; |
2242 |
|
|
2243 |
|
void WarningAlert(const char *text) |
2244 |
|
{ |
2245 |
< |
#ifdef ENABLE_GTK |
2245 |
> |
#if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO) |
2246 |
|
if (PrefsFindBool("nogui") || x_display == NULL) { |
2247 |
|
printf(GetString(STR_SHELL_WARNING_PREFIX), text); |
2248 |
|
return; |