1 |
|
/* |
2 |
|
* main_unix.cpp - Emulation core, Unix implementation |
3 |
|
* |
4 |
< |
* SheepShaver (C) 1997-2002 Christian Bauer and Marc Hellwig |
4 |
> |
* SheepShaver (C) 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 |
|
*/ |
89 |
|
#include <sys/mman.h> |
90 |
|
#include <sys/ipc.h> |
91 |
|
#include <sys/shm.h> |
92 |
+ |
#include <sys/stat.h> |
93 |
|
#include <signal.h> |
94 |
|
|
95 |
|
#include "sysdeps.h" |
103 |
|
#include "xpram.h" |
104 |
|
#include "timer.h" |
105 |
|
#include "adb.h" |
96 |
– |
#include "sony.h" |
97 |
– |
#include "disk.h" |
98 |
– |
#include "cdrom.h" |
99 |
– |
#include "scsi.h" |
106 |
|
#include "video.h" |
101 |
– |
#include "audio.h" |
102 |
– |
#include "ether.h" |
103 |
– |
#include "serial.h" |
104 |
– |
#include "clip.h" |
105 |
– |
#include "extfs.h" |
107 |
|
#include "sys.h" |
108 |
|
#include "macos_util.h" |
109 |
|
#include "rom_patches.h" |
110 |
|
#include "user_strings.h" |
111 |
|
#include "vm_alloc.h" |
112 |
+ |
#include "sigsegv.h" |
113 |
+ |
#include "sigregs.h" |
114 |
+ |
#include "rpc.h" |
115 |
|
|
116 |
|
#define DEBUG 0 |
117 |
|
#include "debug.h" |
118 |
|
|
119 |
|
|
120 |
+ |
#ifdef HAVE_DIRENT_H |
121 |
+ |
#include <dirent.h> |
122 |
+ |
#endif |
123 |
+ |
|
124 |
+ |
#ifdef USE_SDL |
125 |
+ |
#include <SDL.h> |
126 |
+ |
#endif |
127 |
+ |
|
128 |
+ |
#ifndef USE_SDL_VIDEO |
129 |
|
#include <X11/Xlib.h> |
130 |
+ |
#endif |
131 |
|
|
132 |
|
#ifdef ENABLE_GTK |
133 |
|
#include <gtk/gtk.h> |
136 |
|
#ifdef ENABLE_XF86_DGA |
137 |
|
#include <X11/Xlib.h> |
138 |
|
#include <X11/Xutil.h> |
139 |
< |
#include <X11/extensions/xf86dga.h> |
139 |
> |
#include <X11/extensions/Xxf86dga.h> |
140 |
|
#endif |
141 |
|
|
142 |
|
#ifdef ENABLE_MON |
144 |
|
#endif |
145 |
|
|
146 |
|
|
147 |
+ |
// Enable emulation of unaligned lmw/stmw? |
148 |
+ |
#define EMULATE_UNALIGNED_LOADSTORE_MULTIPLE 1 |
149 |
+ |
|
150 |
|
// Enable Execute68k() safety checks? |
151 |
|
#define SAFE_EXEC_68K 0 |
152 |
|
|
161 |
|
const char ROM_FILE_NAME[] = "ROM"; |
162 |
|
const char ROM_FILE_NAME2[] = "Mac OS ROM"; |
163 |
|
|
164 |
< |
const uint32 ROM_AREA_SIZE = 0x500000; // Size of ROM area |
165 |
< |
const uint32 ROM_END = ROM_BASE + ROM_SIZE; // End of ROM |
166 |
< |
|
167 |
< |
const uint32 KERNEL_DATA_BASE = 0x68ffe000; // Address of Kernel Data |
168 |
< |
const uint32 KERNEL_DATA2_BASE = 0x5fffe000; // Alternate address of Kernel Data |
169 |
< |
const uint32 KERNEL_AREA_SIZE = 0x2000; // Size of Kernel Data area |
170 |
< |
|
154 |
< |
const uint32 SIG_STACK_SIZE = 0x10000; // Size of signal stack |
155 |
< |
|
156 |
< |
|
157 |
< |
// 68k Emulator Data |
158 |
< |
struct EmulatorData { |
159 |
< |
uint32 v[0x400]; |
160 |
< |
}; |
161 |
< |
|
162 |
< |
|
163 |
< |
// Kernel Data |
164 |
< |
struct KernelData { |
165 |
< |
uint32 v[0x400]; |
166 |
< |
EmulatorData ed; |
167 |
< |
}; |
168 |
< |
|
169 |
< |
|
170 |
< |
#if !EMULATED_PPC |
171 |
< |
// Structure in which registers are saved in a signal handler; |
172 |
< |
// sigcontext->regs points to it |
173 |
< |
// (see arch/ppc/kernel/signal.c) |
174 |
< |
typedef struct { |
175 |
< |
uint32 u[4]; |
176 |
< |
} __attribute((aligned(16))) vector128; |
177 |
< |
#include <linux/elf.h> |
178 |
< |
|
179 |
< |
struct sigregs { |
180 |
< |
elf_gregset_t gp_regs; // Identical to pt_regs |
181 |
< |
double fp_regs[ELF_NFPREG]; // f0..f31 and fpsrc |
182 |
< |
//more (uninteresting) stuff following here |
183 |
< |
}; |
164 |
> |
#if !REAL_ADDRESSING |
165 |
> |
// FIXME: needs to be >= 0x04000000 |
166 |
> |
const uintptr RAM_BASE = 0x10000000; // Base address of RAM |
167 |
> |
#endif |
168 |
> |
const uintptr ROM_BASE = 0x40800000; // Base address of ROM |
169 |
> |
#if REAL_ADDRESSING |
170 |
> |
const uint32 ROM_ALIGNMENT = 0x100000; // ROM must be aligned to a 1MB boundary |
171 |
|
#endif |
172 |
+ |
const uint32 SIG_STACK_SIZE = 0x10000; // Size of signal stack |
173 |
|
|
174 |
|
|
175 |
|
// Global variables (exported) |
176 |
|
#if !EMULATED_PPC |
177 |
< |
void *TOC; // Small data pointer (r13) |
177 |
> |
void *TOC = NULL; // Pointer to Thread Local Storage (r2) |
178 |
> |
void *R13 = NULL; // Pointer to .sdata section (r13 under Linux) |
179 |
|
#endif |
180 |
|
uint32 RAMBase; // Base address of Mac RAM |
181 |
|
uint32 RAMSize; // Size of Mac RAM |
182 |
+ |
uint32 ROMBase; // Base address of Mac ROM |
183 |
|
uint32 KernelDataAddr; // Address of Kernel Data |
184 |
|
uint32 BootGlobsAddr; // Address of BootGlobs structure at top of Mac RAM |
185 |
+ |
uint32 DRCacheAddr; // Address of DR Cache |
186 |
|
uint32 PVR; // Theoretical PVR |
187 |
|
int64 CPUClockSpeed; // Processor clock speed (Hz) |
188 |
|
int64 BusClockSpeed; // Bus clock speed (Hz) |
189 |
+ |
int64 TimebaseSpeed; // Timebase clock speed (Hz) |
190 |
+ |
uint8 *RAMBaseHost; // Base address of Mac RAM (host address space) |
191 |
+ |
uint8 *ROMBaseHost; // Base address of Mac ROM (host address space) |
192 |
|
|
193 |
|
|
194 |
|
// Global variables |
195 |
< |
static char *x_display_name = NULL; // X11 display name |
195 |
> |
#ifndef USE_SDL_VIDEO |
196 |
> |
char *x_display_name = NULL; // X11 display name |
197 |
|
Display *x_display = NULL; // X11 display handle |
198 |
+ |
#ifdef X11_LOCK_TYPE |
199 |
+ |
X11_LOCK_TYPE x_display_lock = X11_LOCK_INIT; // X11 display lock |
200 |
+ |
#endif |
201 |
+ |
#endif |
202 |
|
|
203 |
|
static int zero_fd = 0; // FD of /dev/zero |
204 |
|
static bool lm_area_mapped = false; // Flag: Low Memory area mmap()ped |
205 |
|
static int kernel_area = -1; // SHM ID of Kernel Data area |
206 |
|
static bool rom_area_mapped = false; // Flag: Mac ROM mmap()ped |
207 |
|
static bool ram_area_mapped = false; // Flag: Mac RAM mmap()ped |
208 |
< |
static void *mmap_RAMBase = NULL; // Base address of mmap()ed RAM area |
208 |
> |
static bool dr_cache_area_mapped = false; // Flag: Mac DR Cache mmap()ped |
209 |
> |
static bool dr_emulator_area_mapped = false;// Flag: Mac DR Emulator mmap()ped |
210 |
|
static KernelData *kernel_data; // Pointer to Kernel Data |
211 |
|
static EmulatorData *emulator_data; |
212 |
|
|
213 |
|
static uint8 last_xpram[XPRAM_SIZE]; // Buffer for monitoring XPRAM changes |
214 |
|
|
215 |
|
static bool nvram_thread_active = false; // Flag: NVRAM watchdog installed |
216 |
+ |
static volatile bool nvram_thread_cancel; // Flag: Cancel NVRAM thread |
217 |
|
static pthread_t nvram_thread; // NVRAM watchdog |
218 |
|
static bool tick_thread_active = false; // Flag: MacOS thread installed |
219 |
+ |
static volatile bool tick_thread_cancel; // Flag: Cancel 60Hz thread |
220 |
|
static pthread_t tick_thread; // 60Hz thread |
221 |
|
static pthread_t emul_thread; // MacOS thread |
222 |
|
|
223 |
|
static bool ready_for_signals = false; // Handler installed, signals can be sent |
224 |
|
static int64 num_segv = 0; // Number of handled SEGV signals |
225 |
|
|
224 |
– |
#if !EMULATED_PPC |
226 |
|
static struct sigaction sigusr2_action; // Interrupt signal (of emulator thread) |
227 |
+ |
#if EMULATED_PPC |
228 |
+ |
static uintptr sig_stack = 0; // Stack for PowerPC interrupt routine |
229 |
+ |
#else |
230 |
|
static struct sigaction sigsegv_action; // Data access exception signal (of emulator thread) |
231 |
|
static struct sigaction sigill_action; // Illegal instruction signal (of emulator thread) |
232 |
< |
static void *sig_stack = NULL; // Stack for signal handlers |
233 |
< |
static void *extra_stack = NULL; // Stack for SIGSEGV inside interrupt handler |
232 |
> |
static stack_t sig_stack; // Stack for signal handlers |
233 |
> |
static stack_t extra_stack; // Stack for SIGSEGV inside interrupt handler |
234 |
|
static bool emul_thread_fatal = false; // Flag: MacOS thread crashed, tick thread shall dump debug output |
235 |
|
static sigregs sigsegv_regs; // Register dump when crashed |
236 |
+ |
static const char *crash_reason = NULL; // Reason of the crash (SIGSEGV, SIGBUS, SIGILL) |
237 |
|
#endif |
238 |
|
|
239 |
+ |
static rpc_connection_t *gui_connection = NULL; // RPC connection to the GUI |
240 |
+ |
static const char *gui_connection_path = NULL; // GUI connection identifier |
241 |
+ |
|
242 |
+ |
uint32 SheepMem::page_size; // Size of a native page |
243 |
+ |
uintptr SheepMem::zero_page = 0; // Address of ro page filled in with zeros |
244 |
+ |
uintptr SheepMem::base = 0x60000000; // Address of SheepShaver data |
245 |
+ |
uintptr SheepMem::proc; // Bottom address of SheepShave procedures |
246 |
+ |
uintptr SheepMem::data; // Top of SheepShaver data (stack like storage) |
247 |
+ |
|
248 |
|
|
249 |
|
// Prototypes |
250 |
+ |
static bool kernel_data_init(void); |
251 |
+ |
static void kernel_data_exit(void); |
252 |
|
static void Quit(void); |
253 |
|
static void *emul_func(void *arg); |
254 |
|
static void *nvram_func(void *arg); |
255 |
|
static void *tick_func(void *arg); |
256 |
< |
#if !EMULATED_PPC |
257 |
< |
static void sigusr2_handler(int sig, sigcontext_struct *sc); |
258 |
< |
static void sigsegv_handler(int sig, sigcontext_struct *sc); |
259 |
< |
static void sigill_handler(int sig, sigcontext_struct *sc); |
256 |
> |
#if EMULATED_PPC |
257 |
> |
extern void emul_ppc(uint32 start); |
258 |
> |
extern void init_emul_ppc(void); |
259 |
> |
extern void exit_emul_ppc(void); |
260 |
> |
sigsegv_return_t sigsegv_handler(sigsegv_info_t *sip); |
261 |
> |
#else |
262 |
> |
extern "C" void sigusr2_handler_init(int sig, siginfo_t *sip, void *scp); |
263 |
> |
extern "C" void sigusr2_handler(int sig, siginfo_t *sip, void *scp); |
264 |
> |
static void sigsegv_handler(int sig, siginfo_t *sip, void *scp); |
265 |
> |
static void sigill_handler(int sig, siginfo_t *sip, void *scp); |
266 |
|
#endif |
267 |
|
|
268 |
|
|
269 |
|
// From asm_linux.S |
270 |
< |
#if EMULATED_PPC |
249 |
< |
extern int atomic_add(int *var, int v); |
250 |
< |
extern int atomic_and(int *var, int v); |
251 |
< |
extern int atomic_or(int *var, int v); |
252 |
< |
#else |
253 |
< |
extern "C" void *get_toc(void); |
270 |
> |
#if !EMULATED_PPC |
271 |
|
extern "C" void *get_sp(void); |
272 |
< |
extern "C" void flush_icache_range(void *start, void *end); |
272 |
> |
extern "C" void *get_r2(void); |
273 |
> |
extern "C" void set_r2(void *); |
274 |
> |
extern "C" void *get_r13(void); |
275 |
> |
extern "C" void set_r13(void *); |
276 |
> |
extern "C" void flush_icache_range(uint32 start, uint32 end); |
277 |
|
extern "C" void jump_to_rom(uint32 entry, uint32 context); |
278 |
|
extern "C" void quit_emulator(void); |
279 |
|
extern "C" void execute_68k(uint32 pc, M68kRegisters *r); |
285 |
|
#endif |
286 |
|
|
287 |
|
|
288 |
+ |
#if EMULATED_PPC |
289 |
+ |
/* |
290 |
+ |
* Return signal stack base |
291 |
+ |
*/ |
292 |
+ |
|
293 |
+ |
uintptr SignalStackBase(void) |
294 |
+ |
{ |
295 |
+ |
return sig_stack + SIG_STACK_SIZE; |
296 |
+ |
} |
297 |
+ |
|
298 |
+ |
|
299 |
+ |
/* |
300 |
+ |
* Atomic operations |
301 |
+ |
*/ |
302 |
+ |
|
303 |
+ |
#if HAVE_SPINLOCKS |
304 |
+ |
static spinlock_t atomic_ops_lock = SPIN_LOCK_UNLOCKED; |
305 |
+ |
#else |
306 |
+ |
#define spin_lock(LOCK) |
307 |
+ |
#define spin_unlock(LOCK) |
308 |
+ |
#endif |
309 |
+ |
|
310 |
+ |
int atomic_add(int *var, int v) |
311 |
+ |
{ |
312 |
+ |
spin_lock(&atomic_ops_lock); |
313 |
+ |
int ret = *var; |
314 |
+ |
*var += v; |
315 |
+ |
spin_unlock(&atomic_ops_lock); |
316 |
+ |
return ret; |
317 |
+ |
} |
318 |
+ |
|
319 |
+ |
int atomic_and(int *var, int v) |
320 |
+ |
{ |
321 |
+ |
spin_lock(&atomic_ops_lock); |
322 |
+ |
int ret = *var; |
323 |
+ |
*var &= v; |
324 |
+ |
spin_unlock(&atomic_ops_lock); |
325 |
+ |
return ret; |
326 |
+ |
} |
327 |
+ |
|
328 |
+ |
int atomic_or(int *var, int v) |
329 |
+ |
{ |
330 |
+ |
spin_lock(&atomic_ops_lock); |
331 |
+ |
int ret = *var; |
332 |
+ |
*var |= v; |
333 |
+ |
spin_unlock(&atomic_ops_lock); |
334 |
+ |
return ret; |
335 |
+ |
} |
336 |
+ |
#endif |
337 |
+ |
|
338 |
+ |
|
339 |
+ |
/* |
340 |
+ |
* Memory management helpers |
341 |
+ |
*/ |
342 |
+ |
|
343 |
+ |
static inline uint8 *vm_mac_acquire(uint32 size) |
344 |
+ |
{ |
345 |
+ |
return (uint8 *)vm_acquire(size); |
346 |
+ |
} |
347 |
+ |
|
348 |
+ |
static inline int vm_mac_acquire_fixed(uint32 addr, uint32 size) |
349 |
+ |
{ |
350 |
+ |
return vm_acquire_fixed(Mac2HostAddr(addr), size); |
351 |
+ |
} |
352 |
+ |
|
353 |
+ |
static inline int vm_mac_release(uint32 addr, uint32 size) |
354 |
+ |
{ |
355 |
+ |
return vm_release(Mac2HostAddr(addr), size); |
356 |
+ |
} |
357 |
+ |
|
358 |
+ |
|
359 |
|
/* |
360 |
|
* Main program |
361 |
|
*/ |
369 |
|
exit(0); |
370 |
|
} |
371 |
|
|
372 |
< |
int main(int argc, char **argv) |
372 |
> |
static bool valid_vmdir(const char *path) |
373 |
|
{ |
374 |
< |
char str[256]; |
375 |
< |
uint32 *boot_globs; |
376 |
< |
int16 i16; |
377 |
< |
int drive, driver; |
378 |
< |
int rom_fd; |
374 |
> |
const int suffix_len = sizeof(".sheepvm") - 1; |
375 |
> |
int len = strlen(path); |
376 |
> |
if (len && path[len - 1] == '/') // to support both ".sheepvm" and ".sheepvm/" |
377 |
> |
len--; |
378 |
> |
if (len > suffix_len && !strncmp(path + len - suffix_len, ".sheepvm", suffix_len)) { |
379 |
> |
struct stat d; |
380 |
> |
if (!stat(path, &d) && S_ISDIR(d.st_mode)) { |
381 |
> |
return true; |
382 |
> |
} |
383 |
> |
} |
384 |
> |
return false; |
385 |
> |
} |
386 |
> |
|
387 |
> |
static void get_system_info(void) |
388 |
> |
{ |
389 |
> |
#if !EMULATED_PPC |
390 |
|
FILE *proc_file; |
391 |
< |
const char *rom_path; |
391 |
> |
#endif |
392 |
> |
|
393 |
> |
PVR = 0x00040000; // Default: 604 |
394 |
> |
CPUClockSpeed = 100000000; // Default: 100MHz |
395 |
> |
BusClockSpeed = 100000000; // Default: 100MHz |
396 |
> |
TimebaseSpeed = 25000000; // Default: 25MHz |
397 |
> |
|
398 |
> |
#if EMULATED_PPC |
399 |
> |
PVR = 0x000c0000; // Default: 7400 (with AltiVec) |
400 |
> |
#elif defined(__APPLE__) && defined(__MACH__) |
401 |
> |
proc_file = popen("ioreg -c IOPlatformDevice", "r"); |
402 |
> |
if (proc_file) { |
403 |
> |
char line[256]; |
404 |
> |
bool powerpc_node = false; |
405 |
> |
while (fgets(line, sizeof(line) - 1, proc_file)) { |
406 |
> |
// Read line |
407 |
> |
int len = strlen(line); |
408 |
> |
if (len == 0) |
409 |
> |
continue; |
410 |
> |
line[len - 1] = 0; |
411 |
> |
|
412 |
> |
// Parse line |
413 |
> |
if (strstr(line, "o PowerPC,")) |
414 |
> |
powerpc_node = true; |
415 |
> |
else if (powerpc_node) { |
416 |
> |
uint32 value; |
417 |
> |
char head[256]; |
418 |
> |
if (sscanf(line, "%[ |]\"cpu-version\" = <%x>", head, &value) == 2) |
419 |
> |
PVR = value; |
420 |
> |
else if (sscanf(line, "%[ |]\"clock-frequency\" = <%x>", head, &value) == 2) |
421 |
> |
CPUClockSpeed = value; |
422 |
> |
else if (sscanf(line, "%[ |]\"bus-frequency\" = <%x>", head, &value) == 2) |
423 |
> |
BusClockSpeed = value; |
424 |
> |
else if (sscanf(line, "%[ |]\"timebase-frequency\" = <%x>", head, &value) == 2) |
425 |
> |
TimebaseSpeed = value; |
426 |
> |
else if (strchr(line, '}')) |
427 |
> |
powerpc_node = false; |
428 |
> |
} |
429 |
> |
} |
430 |
> |
fclose(proc_file); |
431 |
> |
} else { |
432 |
> |
char str[256]; |
433 |
> |
sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno)); |
434 |
> |
WarningAlert(str); |
435 |
> |
} |
436 |
> |
#else |
437 |
> |
proc_file = fopen("/proc/cpuinfo", "r"); |
438 |
> |
if (proc_file) { |
439 |
> |
// CPU specs from Linux kernel |
440 |
> |
// TODO: make it more generic with features (e.g. AltiVec) and |
441 |
> |
// cache information and friends for NameRegistry |
442 |
> |
static const struct { |
443 |
> |
uint32 pvr_mask; |
444 |
> |
uint32 pvr_value; |
445 |
> |
const char *cpu_name; |
446 |
> |
} |
447 |
> |
cpu_specs[] = { |
448 |
> |
{ 0xffff0000, 0x00010000, "601" }, |
449 |
> |
{ 0xffff0000, 0x00030000, "603" }, |
450 |
> |
{ 0xffff0000, 0x00060000, "603e" }, |
451 |
> |
{ 0xffff0000, 0x00070000, "603ev" }, |
452 |
> |
{ 0xffff0000, 0x00040000, "604" }, |
453 |
> |
{ 0xfffff000, 0x00090000, "604e" }, |
454 |
> |
{ 0xffff0000, 0x00090000, "604r" }, |
455 |
> |
{ 0xffff0000, 0x000a0000, "604ev" }, |
456 |
> |
{ 0xffffffff, 0x00084202, "740/750" }, |
457 |
> |
{ 0xfffff000, 0x00083000, "745/755" }, |
458 |
> |
{ 0xfffffff0, 0x00080100, "750CX" }, |
459 |
> |
{ 0xfffffff0, 0x00082200, "750CX" }, |
460 |
> |
{ 0xfffffff0, 0x00082210, "750CXe" }, |
461 |
> |
{ 0xffffff00, 0x70000100, "750FX" }, |
462 |
> |
{ 0xffffffff, 0x70000200, "750FX" }, |
463 |
> |
{ 0xffff0000, 0x70000000, "750FX" }, |
464 |
> |
{ 0xffff0000, 0x70020000, "750GX" }, |
465 |
> |
{ 0xffff0000, 0x00080000, "740/750" }, |
466 |
> |
{ 0xffffffff, 0x000c1101, "7400 (1.1)" }, |
467 |
> |
{ 0xffff0000, 0x000c0000, "7400" }, |
468 |
> |
{ 0xffff0000, 0x800c0000, "7410" }, |
469 |
> |
{ 0xffffffff, 0x80000200, "7450" }, |
470 |
> |
{ 0xffffffff, 0x80000201, "7450" }, |
471 |
> |
{ 0xffff0000, 0x80000000, "7450" }, |
472 |
> |
{ 0xffffff00, 0x80010100, "7455" }, |
473 |
> |
{ 0xffffffff, 0x80010200, "7455" }, |
474 |
> |
{ 0xffff0000, 0x80010000, "7455" }, |
475 |
> |
{ 0xffff0000, 0x80020000, "7457" }, |
476 |
> |
{ 0xffff0000, 0x80030000, "7447A" }, |
477 |
> |
{ 0xffff0000, 0x80040000, "7448" }, |
478 |
> |
{ 0x7fff0000, 0x00810000, "82xx" }, |
479 |
> |
{ 0x7fff0000, 0x00820000, "8280" }, |
480 |
> |
{ 0xffff0000, 0x00400000, "Power3 (630)" }, |
481 |
> |
{ 0xffff0000, 0x00410000, "Power3 (630+)" }, |
482 |
> |
{ 0xffff0000, 0x00360000, "I-star" }, |
483 |
> |
{ 0xffff0000, 0x00370000, "S-star" }, |
484 |
> |
{ 0xffff0000, 0x00350000, "Power4" }, |
485 |
> |
{ 0xffff0000, 0x00390000, "PPC970" }, |
486 |
> |
{ 0xffff0000, 0x003c0000, "PPC970FX" }, |
487 |
> |
{ 0xffff0000, 0x00440000, "PPC970MP" }, |
488 |
> |
{ 0xffff0000, 0x003a0000, "POWER5 (gr)" }, |
489 |
> |
{ 0xffff0000, 0x003b0000, "POWER5+ (gs)" }, |
490 |
> |
{ 0xffff0000, 0x003e0000, "POWER6" }, |
491 |
> |
{ 0xffff0000, 0x00700000, "Cell Broadband Engine" }, |
492 |
> |
{ 0x7fff0000, 0x00900000, "PA6T" }, |
493 |
> |
{ 0, 0, 0 } |
494 |
> |
}; |
495 |
> |
|
496 |
> |
char line[256]; |
497 |
> |
while(fgets(line, 255, proc_file)) { |
498 |
> |
// Read line |
499 |
> |
int len = strlen(line); |
500 |
> |
if (len == 0) |
501 |
> |
continue; |
502 |
> |
line[len-1] = 0; |
503 |
> |
|
504 |
> |
// Parse line |
505 |
> |
int i; |
506 |
> |
float f; |
507 |
> |
char value[256]; |
508 |
> |
if (sscanf(line, "cpu : %[^,]", value) == 1) { |
509 |
> |
// Search by name |
510 |
> |
const char *cpu_name = NULL; |
511 |
> |
for (int i = 0; cpu_specs[i].pvr_mask != 0; i++) { |
512 |
> |
if (strcmp(cpu_specs[i].cpu_name, value) == 0) { |
513 |
> |
cpu_name = cpu_specs[i].cpu_name; |
514 |
> |
PVR = cpu_specs[i].pvr_value; |
515 |
> |
break; |
516 |
> |
} |
517 |
> |
} |
518 |
> |
if (cpu_name == NULL) |
519 |
> |
printf("WARNING: Unknown CPU type '%s', assuming 604\n", value); |
520 |
> |
else |
521 |
> |
printf("Found a PowerPC %s processor\n", cpu_name); |
522 |
> |
} |
523 |
> |
if (sscanf(line, "clock : %fMHz", &f) == 1) |
524 |
> |
CPUClockSpeed = BusClockSpeed = ((int64)f) * 1000000; |
525 |
> |
else if (sscanf(line, "clock : %dMHz", &i) == 1) |
526 |
> |
CPUClockSpeed = BusClockSpeed = i * 1000000; |
527 |
> |
} |
528 |
> |
fclose(proc_file); |
529 |
> |
} else { |
530 |
> |
sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno)); |
531 |
> |
WarningAlert(str); |
532 |
> |
} |
533 |
> |
|
534 |
> |
// Get actual bus frequency |
535 |
> |
proc_file = fopen("/proc/device-tree/clock-frequency", "r"); |
536 |
> |
if (proc_file) { |
537 |
> |
union { uint8 b[4]; uint32 l; } value; |
538 |
> |
if (fread(value.b, sizeof(value), 1, proc_file) == 1) |
539 |
> |
BusClockSpeed = value.l; |
540 |
> |
fclose(proc_file); |
541 |
> |
} |
542 |
> |
|
543 |
> |
// Get actual timebase frequency |
544 |
> |
TimebaseSpeed = BusClockSpeed / 4; |
545 |
> |
DIR *cpus_dir; |
546 |
> |
if ((cpus_dir = opendir("/proc/device-tree/cpus")) != NULL) { |
547 |
> |
struct dirent *cpu_entry; |
548 |
> |
while ((cpu_entry = readdir(cpus_dir)) != NULL) { |
549 |
> |
if (strstr(cpu_entry->d_name, "PowerPC,") == cpu_entry->d_name) { |
550 |
> |
char timebase_freq_node[256]; |
551 |
> |
sprintf(timebase_freq_node, "/proc/device-tree/cpus/%s/timebase-frequency", cpu_entry->d_name); |
552 |
> |
proc_file = fopen(timebase_freq_node, "r"); |
553 |
> |
if (proc_file) { |
554 |
> |
union { uint8 b[4]; uint32 l; } value; |
555 |
> |
if (fread(value.b, sizeof(value), 1, proc_file) == 1) |
556 |
> |
TimebaseSpeed = value.l; |
557 |
> |
fclose(proc_file); |
558 |
> |
} |
559 |
> |
} |
560 |
> |
} |
561 |
> |
closedir(cpus_dir); |
562 |
> |
} |
563 |
> |
#endif |
564 |
> |
|
565 |
> |
// Remap any newer G4/G5 processor to plain G4 for compatibility |
566 |
> |
switch (PVR >> 16) { |
567 |
> |
case 0x8000: // 7450 |
568 |
> |
case 0x8001: // 7455 |
569 |
> |
case 0x8002: // 7457 |
570 |
> |
case 0x8003: // 7447A |
571 |
> |
case 0x8004: // 7448 |
572 |
> |
case 0x0039: // 970 |
573 |
> |
case 0x003c: // 970FX |
574 |
> |
case 0x0044: // 970MP |
575 |
> |
PVR = 0x000c0000; // 7400 |
576 |
> |
break; |
577 |
> |
} |
578 |
> |
D(bug("PVR: %08x (assumed)\n", PVR)); |
579 |
> |
} |
580 |
> |
|
581 |
> |
static bool load_mac_rom(void) |
582 |
> |
{ |
583 |
|
uint32 rom_size, actual; |
584 |
|
uint8 *rom_tmp; |
585 |
< |
time_t now, expire; |
585 |
> |
const char *rom_path = PrefsFindString("rom"); |
586 |
> |
int rom_fd = open(rom_path && *rom_path ? rom_path : ROM_FILE_NAME, O_RDONLY); |
587 |
> |
if (rom_fd < 0) { |
588 |
> |
rom_fd = open(ROM_FILE_NAME2, O_RDONLY); |
589 |
> |
if (rom_fd < 0) { |
590 |
> |
ErrorAlert(GetString(STR_NO_ROM_FILE_ERR)); |
591 |
> |
return false; |
592 |
> |
} |
593 |
> |
} |
594 |
> |
printf("%s", GetString(STR_READING_ROM_FILE)); |
595 |
> |
rom_size = lseek(rom_fd, 0, SEEK_END); |
596 |
> |
lseek(rom_fd, 0, SEEK_SET); |
597 |
> |
rom_tmp = new uint8[ROM_SIZE]; |
598 |
> |
actual = read(rom_fd, (void *)rom_tmp, ROM_SIZE); |
599 |
> |
close(rom_fd); |
600 |
> |
|
601 |
> |
// Decode Mac ROM |
602 |
> |
if (!DecodeROM(rom_tmp, actual)) { |
603 |
> |
if (rom_size != 4*1024*1024) { |
604 |
> |
ErrorAlert(GetString(STR_ROM_SIZE_ERR)); |
605 |
> |
return false; |
606 |
> |
} else { |
607 |
> |
ErrorAlert(GetString(STR_ROM_FILE_READ_ERR)); |
608 |
> |
return false; |
609 |
> |
} |
610 |
> |
} |
611 |
> |
delete[] rom_tmp; |
612 |
> |
return true; |
613 |
> |
} |
614 |
> |
|
615 |
> |
static bool install_signal_handlers(void) |
616 |
> |
{ |
617 |
> |
char str[256]; |
618 |
> |
#if !EMULATED_PPC |
619 |
> |
// Create and install stacks for signal handlers |
620 |
> |
sig_stack.ss_sp = malloc(SIG_STACK_SIZE); |
621 |
> |
D(bug("Signal stack at %p\n", sig_stack.ss_sp)); |
622 |
> |
if (sig_stack.ss_sp == NULL) { |
623 |
> |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
624 |
> |
return false; |
625 |
> |
} |
626 |
> |
sig_stack.ss_flags = 0; |
627 |
> |
sig_stack.ss_size = SIG_STACK_SIZE; |
628 |
> |
if (sigaltstack(&sig_stack, NULL) < 0) { |
629 |
> |
sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno)); |
630 |
> |
ErrorAlert(str); |
631 |
> |
return false; |
632 |
> |
} |
633 |
> |
extra_stack.ss_sp = malloc(SIG_STACK_SIZE); |
634 |
> |
D(bug("Extra stack at %p\n", extra_stack.ss_sp)); |
635 |
> |
if (extra_stack.ss_sp == NULL) { |
636 |
> |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
637 |
> |
return false; |
638 |
> |
} |
639 |
> |
extra_stack.ss_flags = 0; |
640 |
> |
extra_stack.ss_size = SIG_STACK_SIZE; |
641 |
> |
|
642 |
> |
// Install SIGSEGV and SIGBUS handlers |
643 |
> |
sigemptyset(&sigsegv_action.sa_mask); // Block interrupts during SEGV handling |
644 |
> |
sigaddset(&sigsegv_action.sa_mask, SIGUSR2); |
645 |
> |
sigsegv_action.sa_sigaction = sigsegv_handler; |
646 |
> |
sigsegv_action.sa_flags = SA_ONSTACK | SA_SIGINFO; |
647 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
648 |
> |
sigsegv_action.sa_restorer = NULL; |
649 |
> |
#endif |
650 |
> |
if (sigaction(SIGSEGV, &sigsegv_action, NULL) < 0) { |
651 |
> |
sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGSEGV", strerror(errno)); |
652 |
> |
ErrorAlert(str); |
653 |
> |
return false; |
654 |
> |
} |
655 |
> |
if (sigaction(SIGBUS, &sigsegv_action, NULL) < 0) { |
656 |
> |
sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGBUS", strerror(errno)); |
657 |
> |
ErrorAlert(str); |
658 |
> |
return false; |
659 |
> |
} |
660 |
> |
#else |
661 |
> |
// Install SIGSEGV handler for CPU emulator |
662 |
> |
if (!sigsegv_install_handler(sigsegv_handler)) { |
663 |
> |
sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGSEGV", strerror(errno)); |
664 |
> |
ErrorAlert(str); |
665 |
> |
return false; |
666 |
> |
} |
667 |
> |
#endif |
668 |
> |
return true; |
669 |
> |
} |
670 |
> |
|
671 |
> |
static bool init_sdl() |
672 |
> |
{ |
673 |
> |
int sdl_flags = 0; |
674 |
> |
#ifdef USE_SDL_VIDEO |
675 |
> |
sdl_flags |= SDL_INIT_VIDEO; |
676 |
> |
#endif |
677 |
> |
#ifdef USE_SDL_AUDIO |
678 |
> |
sdl_flags |= SDL_INIT_AUDIO; |
679 |
> |
#endif |
680 |
> |
assert(sdl_flags != 0); |
681 |
> |
|
682 |
> |
#ifdef USE_SDL_VIDEO |
683 |
> |
// Don't let SDL block the screensaver |
684 |
> |
setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", TRUE); |
685 |
> |
|
686 |
> |
// Make SDL pass through command-clicks and option-clicks unaltered |
687 |
> |
setenv("SDL_HAS3BUTTONMOUSE", "1", TRUE); |
688 |
> |
#endif |
689 |
> |
|
690 |
> |
if (SDL_Init(sdl_flags) == -1) { |
691 |
> |
char str[256]; |
692 |
> |
sprintf(str, "Could not initialize SDL: %s.\n", SDL_GetError()); |
693 |
> |
ErrorAlert(str); |
694 |
> |
return false; |
695 |
> |
} |
696 |
> |
atexit(SDL_Quit); |
697 |
> |
|
698 |
> |
// Don't let SDL catch SIGINT and SIGTERM signals |
699 |
> |
signal(SIGINT, SIG_DFL); |
700 |
> |
signal(SIGTERM, SIG_DFL); |
701 |
> |
return true; |
702 |
> |
} |
703 |
> |
|
704 |
> |
int main(int argc, char **argv) |
705 |
> |
{ |
706 |
> |
char str[256]; |
707 |
> |
bool memory_mapped_from_zero, ram_rom_areas_contiguous; |
708 |
> |
const char *vmdir = NULL; |
709 |
|
|
710 |
|
// Initialize variables |
711 |
|
RAMBase = 0; |
295 |
– |
mmap_RAMBase = NULL; |
712 |
|
tzset(); |
713 |
|
|
714 |
|
// Print some info |
716 |
|
printf(" %s\n", GetString(STR_ABOUT_TEXT2)); |
717 |
|
|
718 |
|
#if !EMULATED_PPC |
719 |
+ |
#ifdef SYSTEM_CLOBBERS_R2 |
720 |
|
// Get TOC pointer |
721 |
< |
TOC = get_toc(); |
721 |
> |
TOC = get_r2(); |
722 |
> |
#endif |
723 |
> |
#ifdef SYSTEM_CLOBBERS_R13 |
724 |
> |
// Get r13 register |
725 |
> |
R13 = get_r13(); |
726 |
|
#endif |
306 |
– |
|
307 |
– |
#ifdef ENABLE_GTK |
308 |
– |
// Init GTK |
309 |
– |
gtk_set_locale(); |
310 |
– |
gtk_init(&argc, &argv); |
727 |
|
#endif |
312 |
– |
|
313 |
– |
// Read preferences |
314 |
– |
PrefsInit(argc, argv); |
728 |
|
|
729 |
|
// Parse command line arguments |
730 |
|
for (int i=1; i<argc; i++) { |
731 |
|
if (strcmp(argv[i], "--help") == 0) { |
732 |
|
usage(argv[0]); |
733 |
+ |
#ifndef USE_SDL_VIDEO |
734 |
|
} else if (strcmp(argv[i], "--display") == 0) { |
735 |
|
i++; |
736 |
|
if (i < argc) |
737 |
|
x_display_name = strdup(argv[i]); |
738 |
< |
} else if (argv[i][0] == '-') { |
738 |
> |
#endif |
739 |
> |
} else if (strcmp(argv[i], "--gui-connection") == 0) { |
740 |
> |
argv[i++] = NULL; |
741 |
> |
if (i < argc) { |
742 |
> |
gui_connection_path = argv[i]; |
743 |
> |
argv[i] = NULL; |
744 |
> |
} |
745 |
> |
} else if (valid_vmdir(argv[i])) { |
746 |
> |
vmdir = argv[i]; |
747 |
> |
argv[i] = NULL; |
748 |
> |
printf("Using %s as vmdir.\n", vmdir); |
749 |
> |
if (chdir(vmdir)) { |
750 |
> |
printf("Failed to chdir to %s. Good bye.", vmdir); |
751 |
> |
exit(1); |
752 |
> |
} |
753 |
> |
break; |
754 |
> |
} |
755 |
> |
} |
756 |
> |
|
757 |
> |
// Remove processed arguments |
758 |
> |
for (int i=1; i<argc; i++) { |
759 |
> |
int k; |
760 |
> |
for (k=i; k<argc; k++) |
761 |
> |
if (argv[k] != NULL) |
762 |
> |
break; |
763 |
> |
if (k > i) { |
764 |
> |
k -= i; |
765 |
> |
for (int j=i+k; j<argc; j++) |
766 |
> |
argv[j-k] = argv[j]; |
767 |
> |
argc -= k; |
768 |
> |
} |
769 |
> |
} |
770 |
> |
|
771 |
> |
// Connect to the external GUI |
772 |
> |
if (gui_connection_path) { |
773 |
> |
if ((gui_connection = rpc_init_client(gui_connection_path)) == NULL) { |
774 |
> |
fprintf(stderr, "Failed to initialize RPC client connection to the GUI\n"); |
775 |
> |
return 1; |
776 |
> |
} |
777 |
> |
} |
778 |
> |
|
779 |
> |
#ifdef ENABLE_GTK |
780 |
> |
if (!gui_connection) { |
781 |
> |
// Init GTK |
782 |
> |
gtk_set_locale(); |
783 |
> |
gtk_init(&argc, &argv); |
784 |
> |
} |
785 |
> |
#endif |
786 |
> |
|
787 |
> |
// Read preferences |
788 |
> |
PrefsInit(vmdir, argc, argv); |
789 |
> |
|
790 |
> |
// Any command line arguments left? |
791 |
> |
for (int i=1; i<argc; i++) { |
792 |
> |
if (argv[i][0] == '-') { |
793 |
|
fprintf(stderr, "Unrecognized option '%s'\n", argv[i]); |
794 |
|
usage(argv[0]); |
795 |
|
} |
796 |
|
} |
797 |
|
|
798 |
+ |
#ifdef USE_SDL |
799 |
+ |
// Initialize SDL system |
800 |
+ |
if (!init_sdl()) |
801 |
+ |
goto quit; |
802 |
+ |
#endif |
803 |
+ |
|
804 |
+ |
#ifndef USE_SDL_VIDEO |
805 |
|
// Open display |
806 |
|
x_display = XOpenDisplay(x_display_name); |
807 |
|
if (x_display == NULL) { |
815 |
|
// Fork out, so we can return from fullscreen mode when things get ugly |
816 |
|
XF86DGAForkApp(DefaultScreen(x_display)); |
817 |
|
#endif |
818 |
+ |
#endif |
819 |
|
|
820 |
|
#ifdef ENABLE_MON |
821 |
|
// Initialize mon |
822 |
|
mon_init(); |
823 |
|
#endif |
824 |
|
|
825 |
< |
// Get system info |
826 |
< |
PVR = 0x00040000; // Default: 604 |
827 |
< |
CPUClockSpeed = 100000000; // Default: 100MHz |
352 |
< |
BusClockSpeed = 100000000; // Default: 100MHz |
353 |
< |
#if !EMULATED_PPC |
354 |
< |
proc_file = fopen("/proc/cpuinfo", "r"); |
355 |
< |
if (proc_file) { |
356 |
< |
char line[256]; |
357 |
< |
while(fgets(line, 255, proc_file)) { |
358 |
< |
// Read line |
359 |
< |
int len = strlen(line); |
360 |
< |
if (len == 0) |
361 |
< |
continue; |
362 |
< |
line[len-1] = 0; |
825 |
> |
// Install signal handlers |
826 |
> |
if (!install_signal_handlers()) |
827 |
> |
goto quit; |
828 |
|
|
829 |
< |
// Parse line |
830 |
< |
int i; |
831 |
< |
char value[256]; |
832 |
< |
if (sscanf(line, "cpu : %s", value) == 1) { |
833 |
< |
if (strcmp(value, "601") == 0) |
369 |
< |
PVR = 0x00010000; |
370 |
< |
else if (strcmp(value, "603") == 0) |
371 |
< |
PVR = 0x00030000; |
372 |
< |
else if (strcmp(value, "604") == 0) |
373 |
< |
PVR = 0x00040000; |
374 |
< |
else if (strcmp(value, "603e") == 0) |
375 |
< |
PVR = 0x00060000; |
376 |
< |
else if (strcmp(value, "603ev") == 0) |
377 |
< |
PVR = 0x00070000; |
378 |
< |
else if (strcmp(value, "604e") == 0) |
379 |
< |
PVR = 0x00090000; |
380 |
< |
else if (strcmp(value, "604ev5") == 0) |
381 |
< |
PVR = 0x000a0000; |
382 |
< |
else if (strcmp(value, "750") == 0) |
383 |
< |
PVR = 0x00080000; |
384 |
< |
else if (strcmp(value, "821") == 0) |
385 |
< |
PVR = 0x00320000; |
386 |
< |
else if (strcmp(value, "860") == 0) |
387 |
< |
PVR = 0x00500000; |
388 |
< |
else |
389 |
< |
printf("WARNING: Unknown CPU type '%s', assuming 604\n", value); |
390 |
< |
} |
391 |
< |
if (sscanf(line, "clock : %dMHz", &i) == 1) |
392 |
< |
CPUClockSpeed = BusClockSpeed = i * 1000000; |
393 |
< |
} |
394 |
< |
fclose(proc_file); |
395 |
< |
} else { |
396 |
< |
sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno)); |
397 |
< |
WarningAlert(str); |
398 |
< |
} |
399 |
< |
#endif |
400 |
< |
D(bug("PVR: %08x (assumed)\n", PVR)); |
829 |
> |
// Initialize VM system |
830 |
> |
vm_init(); |
831 |
> |
|
832 |
> |
// Get system info |
833 |
> |
get_system_info(); |
834 |
|
|
835 |
|
// Init system routines |
836 |
|
SysInit(); |
853 |
|
goto quit; |
854 |
|
} |
855 |
|
|
423 |
– |
// Create Low Memory area (0x0000..0x3000) |
424 |
– |
if (vm_acquire_fixed((char *)0, 0x3000) < 0) { |
425 |
– |
sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno)); |
426 |
– |
ErrorAlert(str); |
427 |
– |
goto quit; |
428 |
– |
} |
429 |
– |
lm_area_mapped = true; |
430 |
– |
|
856 |
|
// Create areas for Kernel Data |
857 |
< |
kernel_area = shmget(IPC_PRIVATE, KERNEL_AREA_SIZE, 0600); |
433 |
< |
if (kernel_area == -1) { |
434 |
< |
sprintf(str, GetString(STR_KD_SHMGET_ERR), strerror(errno)); |
435 |
< |
ErrorAlert(str); |
436 |
< |
goto quit; |
437 |
< |
} |
438 |
< |
if (shmat(kernel_area, (void *)KERNEL_DATA_BASE, 0) < 0) { |
439 |
< |
sprintf(str, GetString(STR_KD_SHMAT_ERR), strerror(errno)); |
440 |
< |
ErrorAlert(str); |
857 |
> |
if (!kernel_data_init()) |
858 |
|
goto quit; |
859 |
< |
} |
860 |
< |
if (shmat(kernel_area, (void *)KERNEL_DATA2_BASE, 0) < 0) { |
861 |
< |
sprintf(str, GetString(STR_KD2_SHMAT_ERR), strerror(errno)); |
859 |
> |
kernel_data = (KernelData *)Mac2HostAddr(KERNEL_DATA_BASE); |
860 |
> |
emulator_data = &kernel_data->ed; |
861 |
> |
KernelDataAddr = KERNEL_DATA_BASE; |
862 |
> |
D(bug("Kernel Data at %p (%08x)\n", kernel_data, KERNEL_DATA_BASE)); |
863 |
> |
D(bug("Emulator Data at %p (%08x)\n", emulator_data, KERNEL_DATA_BASE + offsetof(KernelData, ed))); |
864 |
> |
|
865 |
> |
// Create area for DR Cache |
866 |
> |
if (vm_mac_acquire_fixed(DR_EMULATOR_BASE, DR_EMULATOR_SIZE) < 0) { |
867 |
> |
sprintf(str, GetString(STR_DR_EMULATOR_MMAP_ERR), strerror(errno)); |
868 |
|
ErrorAlert(str); |
869 |
|
goto quit; |
870 |
|
} |
871 |
< |
kernel_data = (KernelData *)0x68ffe000; |
872 |
< |
emulator_data = &kernel_data->ed; |
873 |
< |
KernelDataAddr = (uint32)kernel_data; |
451 |
< |
D(bug("Kernel Data at %p, Emulator Data at %p\n", kernel_data, emulator_data)); |
452 |
< |
|
453 |
< |
// Create area for Mac ROM |
454 |
< |
if (vm_acquire_fixed((char *)ROM_BASE, ROM_AREA_SIZE) < 0) { |
455 |
< |
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
871 |
> |
dr_emulator_area_mapped = true; |
872 |
> |
if (vm_mac_acquire_fixed(DR_CACHE_BASE, DR_CACHE_SIZE) < 0) { |
873 |
> |
sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno)); |
874 |
|
ErrorAlert(str); |
875 |
|
goto quit; |
876 |
|
} |
877 |
+ |
dr_cache_area_mapped = true; |
878 |
|
#if !EMULATED_PPC |
879 |
< |
if (vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
880 |
< |
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
879 |
> |
if (vm_protect((char *)DR_CACHE_BASE, DR_CACHE_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
880 |
> |
sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno)); |
881 |
|
ErrorAlert(str); |
882 |
|
goto quit; |
883 |
|
} |
884 |
|
#endif |
885 |
< |
rom_area_mapped = true; |
886 |
< |
D(bug("ROM area at %08x\n", ROM_BASE)); |
885 |
> |
DRCacheAddr = DR_CACHE_BASE; |
886 |
> |
D(bug("DR Cache at %p\n", DRCacheAddr)); |
887 |
|
|
888 |
+ |
// Create area for SheepShaver data |
889 |
+ |
if (!SheepMem::Init()) { |
890 |
+ |
sprintf(str, GetString(STR_SHEEP_MEM_MMAP_ERR), strerror(errno)); |
891 |
+ |
ErrorAlert(str); |
892 |
+ |
goto quit; |
893 |
+ |
} |
894 |
+ |
|
895 |
|
// Create area for Mac RAM |
896 |
|
RAMSize = PrefsFindInt32("ramsize"); |
897 |
|
if (RAMSize < 8*1024*1024) { |
898 |
|
WarningAlert(GetString(STR_SMALL_RAM_WARN)); |
899 |
|
RAMSize = 8*1024*1024; |
900 |
|
} |
901 |
< |
|
902 |
< |
mmap_RAMBase = (void *)0x20000000; |
903 |
< |
if (vm_acquire_fixed(mmap_RAMBase, RAMSize) < 0) { |
904 |
< |
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
905 |
< |
ErrorAlert(str); |
906 |
< |
goto quit; |
901 |
> |
memory_mapped_from_zero = false; |
902 |
> |
ram_rom_areas_contiguous = false; |
903 |
> |
#if REAL_ADDRESSING && HAVE_LINKER_SCRIPT |
904 |
> |
if (vm_mac_acquire_fixed(0, RAMSize) == 0) { |
905 |
> |
D(bug("Could allocate RAM from 0x0000\n")); |
906 |
> |
RAMBase = 0; |
907 |
> |
RAMBaseHost = Mac2HostAddr(RAMBase); |
908 |
> |
memory_mapped_from_zero = true; |
909 |
> |
} |
910 |
> |
#endif |
911 |
> |
if (!memory_mapped_from_zero) { |
912 |
> |
#ifndef PAGEZERO_HACK |
913 |
> |
// Create Low Memory area (0x0000..0x3000) |
914 |
> |
if (vm_mac_acquire_fixed(0, 0x3000) < 0) { |
915 |
> |
sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno)); |
916 |
> |
ErrorAlert(str); |
917 |
> |
goto quit; |
918 |
> |
} |
919 |
> |
lm_area_mapped = true; |
920 |
> |
#endif |
921 |
> |
#if REAL_ADDRESSING |
922 |
> |
// Allocate RAM at any address. Since ROM must be higher than RAM, allocate the RAM |
923 |
> |
// and ROM areas contiguously, plus a little extra to allow for ROM address alignment. |
924 |
> |
RAMBaseHost = vm_mac_acquire(RAMSize + ROM_AREA_SIZE + ROM_ALIGNMENT); |
925 |
> |
if (RAMBaseHost == VM_MAP_FAILED) { |
926 |
> |
sprintf(str, GetString(STR_RAM_ROM_MMAP_ERR), strerror(errno)); |
927 |
> |
ErrorAlert(str); |
928 |
> |
goto quit; |
929 |
> |
} |
930 |
> |
RAMBase = Host2MacAddr(RAMBaseHost); |
931 |
> |
ROMBase = (RAMBase + RAMSize + ROM_ALIGNMENT -1) & -ROM_ALIGNMENT; |
932 |
> |
ROMBaseHost = Mac2HostAddr(ROMBase); |
933 |
> |
ram_rom_areas_contiguous = true; |
934 |
> |
#else |
935 |
> |
if (vm_mac_acquire_fixed(RAM_BASE, RAMSize) < 0) { |
936 |
> |
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
937 |
> |
ErrorAlert(str); |
938 |
> |
goto quit; |
939 |
> |
} |
940 |
> |
RAMBase = RAM_BASE; |
941 |
> |
RAMBaseHost = Mac2HostAddr(RAMBase); |
942 |
> |
#endif |
943 |
|
} |
944 |
|
#if !EMULATED_PPC |
945 |
< |
if (vm_protect(mmap_RAMBase, RAMSize, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
945 |
> |
if (vm_protect(RAMBaseHost, RAMSize, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
946 |
|
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
947 |
|
ErrorAlert(str); |
948 |
|
goto quit; |
949 |
|
} |
950 |
|
#endif |
489 |
– |
RAMBase = (uint32)mmap_RAMBase; |
951 |
|
ram_area_mapped = true; |
952 |
< |
D(bug("RAM area at %08x\n", RAMBase)); |
952 |
> |
D(bug("RAM area at %p (%08x)\n", RAMBaseHost, RAMBase)); |
953 |
|
|
954 |
< |
if (RAMBase > ROM_BASE) { |
955 |
< |
ErrorAlert(GetString(STR_RAM_HIGHER_THAN_ROM_ERR)); |
954 |
> |
if (RAMBase > KernelDataAddr) { |
955 |
> |
ErrorAlert(GetString(STR_RAM_AREA_TOO_HIGH_ERR)); |
956 |
|
goto quit; |
957 |
|
} |
497 |
– |
|
498 |
– |
// Load Mac ROM |
499 |
– |
rom_path = PrefsFindString("rom"); |
500 |
– |
rom_fd = open(rom_path ? rom_path : ROM_FILE_NAME, O_RDONLY); |
501 |
– |
if (rom_fd < 0) { |
502 |
– |
rom_fd = open(rom_path ? rom_path : ROM_FILE_NAME2, O_RDONLY); |
503 |
– |
if (rom_fd < 0) { |
504 |
– |
ErrorAlert(GetString(STR_NO_ROM_FILE_ERR)); |
505 |
– |
goto quit; |
506 |
– |
} |
507 |
– |
} |
508 |
– |
printf(GetString(STR_READING_ROM_FILE)); |
509 |
– |
rom_size = lseek(rom_fd, 0, SEEK_END); |
510 |
– |
lseek(rom_fd, 0, SEEK_SET); |
511 |
– |
rom_tmp = new uint8[ROM_SIZE]; |
512 |
– |
actual = read(rom_fd, (void *)rom_tmp, ROM_SIZE); |
513 |
– |
close(rom_fd); |
958 |
|
|
959 |
< |
// Decode Mac ROM |
960 |
< |
if (!DecodeROM(rom_tmp, actual)) { |
961 |
< |
if (rom_size != 4*1024*1024) { |
962 |
< |
ErrorAlert(GetString(STR_ROM_SIZE_ERR)); |
963 |
< |
goto quit; |
520 |
< |
} else { |
521 |
< |
ErrorAlert(GetString(STR_ROM_FILE_READ_ERR)); |
959 |
> |
// Create area for Mac ROM |
960 |
> |
if (!ram_rom_areas_contiguous) { |
961 |
> |
if (vm_mac_acquire_fixed(ROM_BASE, ROM_AREA_SIZE) < 0) { |
962 |
> |
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
963 |
> |
ErrorAlert(str); |
964 |
|
goto quit; |
965 |
|
} |
966 |
+ |
ROMBase = ROM_BASE; |
967 |
+ |
ROMBaseHost = Mac2HostAddr(ROMBase); |
968 |
|
} |
969 |
< |
delete[] rom_tmp; |
970 |
< |
|
971 |
< |
// Load NVRAM |
972 |
< |
XPRAMInit(); |
973 |
< |
|
974 |
< |
// Set boot volume |
975 |
< |
drive = PrefsFindInt32("bootdrive"); |
976 |
< |
XPRAM[0x1378] = i16 >> 8; |
977 |
< |
XPRAM[0x1379] = i16 & 0xff; |
534 |
< |
driver = PrefsFindInt32("bootdriver"); |
535 |
< |
XPRAM[0x137a] = i16 >> 8; |
536 |
< |
XPRAM[0x137b] = i16 & 0xff; |
537 |
< |
|
538 |
< |
// Create BootGlobs at top of Mac memory |
539 |
< |
memset((void *)(RAMBase + RAMSize - 4096), 0, 4096); |
540 |
< |
BootGlobsAddr = RAMBase + RAMSize - 0x1c; |
541 |
< |
boot_globs = (uint32 *)BootGlobsAddr; |
542 |
< |
boot_globs[-5] = htonl(RAMBase + RAMSize); // MemTop |
543 |
< |
boot_globs[0] = htonl(RAMBase); // First RAM bank |
544 |
< |
boot_globs[1] = htonl(RAMSize); |
545 |
< |
boot_globs[2] = htonl((uint32)-1); // End of bank table |
546 |
< |
|
547 |
< |
// Init drivers |
548 |
< |
SonyInit(); |
549 |
< |
DiskInit(); |
550 |
< |
CDROMInit(); |
551 |
< |
SCSIInit(); |
552 |
< |
|
553 |
< |
// Init external file system |
554 |
< |
ExtFSInit(); |
555 |
< |
|
556 |
< |
// Init audio |
557 |
< |
AudioInit(); |
558 |
< |
|
559 |
< |
// Init network |
560 |
< |
EtherInit(); |
561 |
< |
|
562 |
< |
// Init serial ports |
563 |
< |
SerialInit(); |
564 |
< |
|
565 |
< |
// Init Time Manager |
566 |
< |
TimerInit(); |
969 |
> |
#if !EMULATED_PPC |
970 |
> |
if (vm_protect(ROMBaseHost, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
971 |
> |
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
972 |
> |
ErrorAlert(str); |
973 |
> |
goto quit; |
974 |
> |
} |
975 |
> |
#endif |
976 |
> |
rom_area_mapped = true; |
977 |
> |
D(bug("ROM area at %p (%08x)\n", ROMBaseHost, ROMBase)); |
978 |
|
|
979 |
< |
// Init clipboard |
980 |
< |
ClipInit(); |
979 |
> |
if (RAMBase > ROMBase) { |
980 |
> |
ErrorAlert(GetString(STR_RAM_HIGHER_THAN_ROM_ERR)); |
981 |
> |
goto quit; |
982 |
> |
} |
983 |
|
|
984 |
< |
// Init video |
985 |
< |
if (!VideoInit()) |
984 |
> |
// Load Mac ROM |
985 |
> |
if (!load_mac_rom()) |
986 |
|
goto quit; |
987 |
|
|
988 |
< |
// Install ROM patches |
989 |
< |
if (!PatchROM()) { |
577 |
< |
ErrorAlert(GetString(STR_UNSUPPORTED_ROM_TYPE_ERR)); |
988 |
> |
// Initialize everything |
989 |
> |
if (!InitAll(vmdir)) |
990 |
|
goto quit; |
991 |
< |
} |
991 |
> |
D(bug("Initialization complete\n")); |
992 |
|
|
993 |
|
// Clear caches (as we loaded and patched code) and write protect ROM |
994 |
|
#if !EMULATED_PPC |
995 |
< |
MakeExecutable(0, (void *)ROM_BASE, ROM_AREA_SIZE); |
584 |
< |
#endif |
585 |
< |
vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_EXECUTE); |
586 |
< |
|
587 |
< |
// Initialize Kernel Data |
588 |
< |
memset(kernel_data, 0, sizeof(KernelData)); |
589 |
< |
if (ROMType == ROMTYPE_NEWWORLD) { |
590 |
< |
static uint32 of_dev_tree[4] = {0, 0, 0, 0}; |
591 |
< |
static uint8 vector_lookup_tbl[128]; |
592 |
< |
static uint8 vector_mask_tbl[64]; |
593 |
< |
memset((uint8 *)kernel_data + 0xb80, 0x3d, 0x80); |
594 |
< |
memset(vector_lookup_tbl, 0, 128); |
595 |
< |
memset(vector_mask_tbl, 0, 64); |
596 |
< |
kernel_data->v[0xb80 >> 2] = htonl(ROM_BASE); |
597 |
< |
kernel_data->v[0xb84 >> 2] = htonl((uint32)of_dev_tree); // OF device tree base |
598 |
< |
kernel_data->v[0xb90 >> 2] = htonl((uint32)vector_lookup_tbl); |
599 |
< |
kernel_data->v[0xb94 >> 2] = htonl((uint32)vector_mask_tbl); |
600 |
< |
kernel_data->v[0xb98 >> 2] = htonl(ROM_BASE); // OpenPIC base |
601 |
< |
kernel_data->v[0xbb0 >> 2] = htonl(0); // ADB base |
602 |
< |
kernel_data->v[0xc20 >> 2] = htonl(RAMSize); |
603 |
< |
kernel_data->v[0xc24 >> 2] = htonl(RAMSize); |
604 |
< |
kernel_data->v[0xc30 >> 2] = htonl(RAMSize); |
605 |
< |
kernel_data->v[0xc34 >> 2] = htonl(RAMSize); |
606 |
< |
kernel_data->v[0xc38 >> 2] = htonl(0x00010020); |
607 |
< |
kernel_data->v[0xc3c >> 2] = htonl(0x00200001); |
608 |
< |
kernel_data->v[0xc40 >> 2] = htonl(0x00010000); |
609 |
< |
kernel_data->v[0xc50 >> 2] = htonl(RAMBase); |
610 |
< |
kernel_data->v[0xc54 >> 2] = htonl(RAMSize); |
611 |
< |
kernel_data->v[0xf60 >> 2] = htonl(PVR); |
612 |
< |
kernel_data->v[0xf64 >> 2] = htonl(CPUClockSpeed); |
613 |
< |
kernel_data->v[0xf68 >> 2] = htonl(BusClockSpeed); |
614 |
< |
kernel_data->v[0xf6c >> 2] = htonl(CPUClockSpeed); |
615 |
< |
} else { |
616 |
< |
kernel_data->v[0xc80 >> 2] = htonl(RAMSize); |
617 |
< |
kernel_data->v[0xc84 >> 2] = htonl(RAMSize); |
618 |
< |
kernel_data->v[0xc90 >> 2] = htonl(RAMSize); |
619 |
< |
kernel_data->v[0xc94 >> 2] = htonl(RAMSize); |
620 |
< |
kernel_data->v[0xc98 >> 2] = htonl(0x00010020); |
621 |
< |
kernel_data->v[0xc9c >> 2] = htonl(0x00200001); |
622 |
< |
kernel_data->v[0xca0 >> 2] = htonl(0x00010000); |
623 |
< |
kernel_data->v[0xcb0 >> 2] = htonl(RAMBase); |
624 |
< |
kernel_data->v[0xcb4 >> 2] = htonl(RAMSize); |
625 |
< |
kernel_data->v[0xf80 >> 2] = htonl(PVR); |
626 |
< |
kernel_data->v[0xf84 >> 2] = htonl(CPUClockSpeed); |
627 |
< |
kernel_data->v[0xf88 >> 2] = htonl(BusClockSpeed); |
628 |
< |
kernel_data->v[0xf8c >> 2] = htonl(CPUClockSpeed); |
629 |
< |
} |
630 |
< |
|
631 |
< |
// Initialize extra low memory |
632 |
< |
D(bug("Initializing Low Memory...\n")); |
633 |
< |
memset(NULL, 0, 0x3000); |
634 |
< |
WriteMacInt32(XLM_SIGNATURE, FOURCC('B','a','a','h')); // Signature to detect SheepShaver |
635 |
< |
WriteMacInt32(XLM_KERNEL_DATA, (uint32)kernel_data); // For trap replacement routines |
636 |
< |
WriteMacInt32(XLM_PVR, PVR); // Theoretical PVR |
637 |
< |
WriteMacInt32(XLM_BUS_CLOCK, BusClockSpeed); // For DriverServicesLib patch |
638 |
< |
WriteMacInt16(XLM_EXEC_RETURN_OPCODE, M68K_EXEC_RETURN); // For Execute68k() (RTS from the executed 68k code will jump here and end 68k mode) |
639 |
< |
#if !EMULATED_PPC |
640 |
< |
WriteMacInt32(XLM_TOC, (uint32)TOC); // TOC pointer of emulator |
641 |
< |
WriteMacInt32(XLM_ETHER_INIT, (uint32)InitStreamModule); // DLPI ethernet driver functions |
642 |
< |
WriteMacInt32(XLM_ETHER_TERM, (uint32)TerminateStreamModule); |
643 |
< |
WriteMacInt32(XLM_ETHER_OPEN, (uint32)ether_open); |
644 |
< |
WriteMacInt32(XLM_ETHER_CLOSE, (uint32)ether_close); |
645 |
< |
WriteMacInt32(XLM_ETHER_WPUT, (uint32)ether_wput); |
646 |
< |
WriteMacInt32(XLM_ETHER_RSRV, (uint32)ether_rsrv); |
647 |
< |
WriteMacInt32(XLM_VIDEO_DOIO, (uint32)VideoDoDriverIO); |
995 |
> |
flush_icache_range(ROMBase, ROMBase + ROM_AREA_SIZE); |
996 |
|
#endif |
997 |
< |
D(bug("Low Memory initialized\n")); |
997 |
> |
vm_protect(ROMBaseHost, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_EXECUTE); |
998 |
|
|
999 |
|
// Start 60Hz thread |
1000 |
+ |
tick_thread_cancel = false; |
1001 |
|
tick_thread_active = (pthread_create(&tick_thread, NULL, tick_func, NULL) == 0); |
1002 |
|
D(bug("Tick thread installed (%ld)\n", tick_thread)); |
1003 |
|
|
1004 |
|
// Start NVRAM watchdog thread |
1005 |
|
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
1006 |
+ |
nvram_thread_cancel = false; |
1007 |
|
nvram_thread_active = (pthread_create(&nvram_thread, NULL, nvram_func, NULL) == 0); |
1008 |
|
D(bug("NVRAM thread installed (%ld)\n", nvram_thread)); |
1009 |
|
|
1010 |
|
#if !EMULATED_PPC |
661 |
– |
// Create and install stacks for signal handlers |
662 |
– |
sig_stack = malloc(SIG_STACK_SIZE); |
663 |
– |
D(bug("Signal stack at %p\n", sig_stack)); |
664 |
– |
if (sig_stack == NULL) { |
665 |
– |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
666 |
– |
goto quit; |
667 |
– |
} |
668 |
– |
extra_stack = malloc(SIG_STACK_SIZE); |
669 |
– |
D(bug("Extra stack at %p\n", extra_stack)); |
670 |
– |
if (extra_stack == NULL) { |
671 |
– |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
672 |
– |
goto quit; |
673 |
– |
} |
674 |
– |
struct sigaltstack new_stack; |
675 |
– |
new_stack.ss_sp = sig_stack; |
676 |
– |
new_stack.ss_flags = 0; |
677 |
– |
new_stack.ss_size = SIG_STACK_SIZE; |
678 |
– |
if (sigaltstack(&new_stack, NULL) < 0) { |
679 |
– |
sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno)); |
680 |
– |
ErrorAlert(str); |
681 |
– |
goto quit; |
682 |
– |
} |
683 |
– |
#endif |
684 |
– |
|
685 |
– |
#if !EMULATED_PPC |
686 |
– |
// Install SIGSEGV handler |
687 |
– |
sigemptyset(&sigsegv_action.sa_mask); // Block interrupts during SEGV handling |
688 |
– |
sigaddset(&sigsegv_action.sa_mask, SIGUSR2); |
689 |
– |
sigsegv_action.sa_handler = (__sighandler_t)sigsegv_handler; |
690 |
– |
sigsegv_action.sa_flags = SA_ONSTACK; |
691 |
– |
sigsegv_action.sa_restorer = NULL; |
692 |
– |
if (sigaction(SIGSEGV, &sigsegv_action, NULL) < 0) { |
693 |
– |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
694 |
– |
ErrorAlert(str); |
695 |
– |
goto quit; |
696 |
– |
} |
697 |
– |
|
1011 |
|
// Install SIGILL handler |
1012 |
|
sigemptyset(&sigill_action.sa_mask); // Block interrupts during ILL handling |
1013 |
|
sigaddset(&sigill_action.sa_mask, SIGUSR2); |
1014 |
< |
sigill_action.sa_handler = (__sighandler_t)sigill_handler; |
1015 |
< |
sigill_action.sa_flags = SA_ONSTACK; |
1014 |
> |
sigill_action.sa_sigaction = sigill_handler; |
1015 |
> |
sigill_action.sa_flags = SA_ONSTACK | SA_SIGINFO; |
1016 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
1017 |
|
sigill_action.sa_restorer = NULL; |
1018 |
+ |
#endif |
1019 |
|
if (sigaction(SIGILL, &sigill_action, NULL) < 0) { |
1020 |
< |
sprintf(str, GetString(STR_SIGILL_INSTALL_ERR), strerror(errno)); |
1020 |
> |
sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGILL", strerror(errno)); |
1021 |
|
ErrorAlert(str); |
1022 |
|
goto quit; |
1023 |
|
} |
1024 |
+ |
#endif |
1025 |
|
|
1026 |
+ |
#if !EMULATED_PPC |
1027 |
|
// Install interrupt signal handler |
1028 |
|
sigemptyset(&sigusr2_action.sa_mask); |
1029 |
< |
sigusr2_action.sa_handler = (__sighandler_t)sigusr2_handler; |
1030 |
< |
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART; |
1029 |
> |
sigusr2_action.sa_sigaction = sigusr2_handler_init; |
1030 |
> |
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART | SA_SIGINFO; |
1031 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
1032 |
|
sigusr2_action.sa_restorer = NULL; |
1033 |
+ |
#endif |
1034 |
|
if (sigaction(SIGUSR2, &sigusr2_action, NULL) < 0) { |
1035 |
< |
sprintf(str, GetString(STR_SIGUSR2_INSTALL_ERR), strerror(errno)); |
1035 |
> |
sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGUSR2", strerror(errno)); |
1036 |
|
ErrorAlert(str); |
1037 |
|
goto quit; |
1038 |
|
} |
1055 |
|
|
1056 |
|
static void Quit(void) |
1057 |
|
{ |
1058 |
+ |
#if EMULATED_PPC |
1059 |
+ |
// Exit PowerPC emulation |
1060 |
+ |
exit_emul_ppc(); |
1061 |
+ |
#endif |
1062 |
+ |
|
1063 |
|
// Stop 60Hz thread |
1064 |
|
if (tick_thread_active) { |
1065 |
+ |
tick_thread_cancel = true; |
1066 |
|
pthread_cancel(tick_thread); |
1067 |
|
pthread_join(tick_thread, NULL); |
1068 |
|
} |
1069 |
|
|
1070 |
|
// Stop NVRAM watchdog thread |
1071 |
|
if (nvram_thread_active) { |
1072 |
+ |
nvram_thread_cancel = true; |
1073 |
|
pthread_cancel(nvram_thread); |
1074 |
|
pthread_join(nvram_thread, NULL); |
1075 |
|
} |
1076 |
|
|
1077 |
|
#if !EMULATED_PPC |
1078 |
< |
// Uninstall SIGSEGV handler |
1078 |
> |
// Uninstall SIGSEGV and SIGBUS handlers |
1079 |
|
sigemptyset(&sigsegv_action.sa_mask); |
1080 |
|
sigsegv_action.sa_handler = SIG_DFL; |
1081 |
|
sigsegv_action.sa_flags = 0; |
1082 |
|
sigaction(SIGSEGV, &sigsegv_action, NULL); |
1083 |
+ |
sigaction(SIGBUS, &sigsegv_action, NULL); |
1084 |
|
|
1085 |
|
// Uninstall SIGILL handler |
1086 |
|
sigemptyset(&sigill_action.sa_mask); |
1087 |
|
sigill_action.sa_handler = SIG_DFL; |
1088 |
|
sigill_action.sa_flags = 0; |
1089 |
|
sigaction(SIGILL, &sigill_action, NULL); |
763 |
– |
#endif |
764 |
– |
|
765 |
– |
// Save NVRAM |
766 |
– |
XPRAMExit(); |
767 |
– |
|
768 |
– |
// Exit clipboard |
769 |
– |
ClipExit(); |
770 |
– |
|
771 |
– |
// Exit Time Manager |
772 |
– |
TimerExit(); |
773 |
– |
|
774 |
– |
// Exit serial |
775 |
– |
SerialExit(); |
776 |
– |
|
777 |
– |
// Exit network |
778 |
– |
EtherExit(); |
1090 |
|
|
1091 |
< |
// Exit audio |
1092 |
< |
AudioExit(); |
1093 |
< |
|
1094 |
< |
// Exit video |
1095 |
< |
VideoExit(); |
1091 |
> |
// Delete stacks for signal handlers |
1092 |
> |
if (sig_stack.ss_sp) |
1093 |
> |
free(sig_stack.ss_sp); |
1094 |
> |
if (extra_stack.ss_sp) |
1095 |
> |
free(extra_stack.ss_sp); |
1096 |
> |
#endif |
1097 |
|
|
1098 |
< |
// Exit external file system |
1099 |
< |
ExtFSExit(); |
1098 |
> |
// Deinitialize everything |
1099 |
> |
ExitAll(); |
1100 |
|
|
1101 |
< |
// Exit drivers |
1102 |
< |
SCSIExit(); |
791 |
< |
CDROMExit(); |
792 |
< |
DiskExit(); |
793 |
< |
SonyExit(); |
1101 |
> |
// Delete SheepShaver globals |
1102 |
> |
SheepMem::Exit(); |
1103 |
|
|
1104 |
|
// Delete RAM area |
1105 |
|
if (ram_area_mapped) |
1106 |
< |
vm_release(mmap_RAMBase, RAMSize); |
1106 |
> |
vm_mac_release(RAMBase, RAMSize); |
1107 |
|
|
1108 |
|
// Delete ROM area |
1109 |
|
if (rom_area_mapped) |
1110 |
< |
vm_release((char *)ROM_BASE, ROM_AREA_SIZE); |
1110 |
> |
vm_mac_release(ROMBase, ROM_AREA_SIZE); |
1111 |
> |
|
1112 |
> |
// Delete DR cache areas |
1113 |
> |
if (dr_emulator_area_mapped) |
1114 |
> |
vm_mac_release(DR_EMULATOR_BASE, DR_EMULATOR_SIZE); |
1115 |
> |
if (dr_cache_area_mapped) |
1116 |
> |
vm_mac_release(DR_CACHE_BASE, DR_CACHE_SIZE); |
1117 |
|
|
1118 |
|
// Delete Kernel Data area |
1119 |
< |
if (kernel_area >= 0) { |
805 |
< |
shmdt((void *)KERNEL_DATA_BASE); |
806 |
< |
shmdt((void *)KERNEL_DATA2_BASE); |
807 |
< |
shmctl(kernel_area, IPC_RMID, NULL); |
808 |
< |
} |
1119 |
> |
kernel_data_exit(); |
1120 |
|
|
1121 |
|
// Delete Low Memory area |
1122 |
|
if (lm_area_mapped) |
1123 |
< |
munmap((char *)0x0000, 0x3000); |
1123 |
> |
vm_mac_release(0, 0x3000); |
1124 |
|
|
1125 |
|
// Close /dev/zero |
1126 |
|
if (zero_fd > 0) |
1138 |
|
#endif |
1139 |
|
|
1140 |
|
// Close X11 server connection |
1141 |
+ |
#ifndef USE_SDL_VIDEO |
1142 |
|
if (x_display) |
1143 |
|
XCloseDisplay(x_display); |
1144 |
+ |
#endif |
1145 |
+ |
|
1146 |
+ |
// Notify GUI we are about to leave |
1147 |
+ |
if (gui_connection) { |
1148 |
+ |
if (rpc_method_invoke(gui_connection, RPC_METHOD_EXIT, RPC_TYPE_INVALID) == RPC_ERROR_NO_ERROR) |
1149 |
+ |
rpc_method_wait_for_reply(gui_connection, RPC_TYPE_INVALID); |
1150 |
+ |
} |
1151 |
|
|
1152 |
|
exit(0); |
1153 |
|
} |
1154 |
|
|
1155 |
|
|
1156 |
|
/* |
1157 |
+ |
* Initialize Kernel Data segments |
1158 |
+ |
*/ |
1159 |
+ |
|
1160 |
+ |
static bool kernel_data_init(void) |
1161 |
+ |
{ |
1162 |
+ |
char str[256]; |
1163 |
+ |
uint32 kernel_area_size = (KERNEL_AREA_SIZE + SHMLBA - 1) & -SHMLBA; |
1164 |
+ |
|
1165 |
+ |
kernel_area = shmget(IPC_PRIVATE, kernel_area_size, 0600); |
1166 |
+ |
if (kernel_area == -1) { |
1167 |
+ |
sprintf(str, GetString(STR_KD_SHMGET_ERR), strerror(errno)); |
1168 |
+ |
ErrorAlert(str); |
1169 |
+ |
return false; |
1170 |
+ |
} |
1171 |
+ |
void *kernel_addr = Mac2HostAddr(KERNEL_DATA_BASE & -SHMLBA); |
1172 |
+ |
if (shmat(kernel_area, kernel_addr, 0) != kernel_addr) { |
1173 |
+ |
sprintf(str, GetString(STR_KD_SHMAT_ERR), strerror(errno)); |
1174 |
+ |
ErrorAlert(str); |
1175 |
+ |
return false; |
1176 |
+ |
} |
1177 |
+ |
kernel_addr = Mac2HostAddr(KERNEL_DATA2_BASE & -SHMLBA); |
1178 |
+ |
if (shmat(kernel_area, kernel_addr, 0) != kernel_addr) { |
1179 |
+ |
sprintf(str, GetString(STR_KD2_SHMAT_ERR), strerror(errno)); |
1180 |
+ |
ErrorAlert(str); |
1181 |
+ |
return false; |
1182 |
+ |
} |
1183 |
+ |
return true; |
1184 |
+ |
} |
1185 |
+ |
|
1186 |
+ |
|
1187 |
+ |
/* |
1188 |
+ |
* Deallocate Kernel Data segments |
1189 |
+ |
*/ |
1190 |
+ |
|
1191 |
+ |
static void kernel_data_exit(void) |
1192 |
+ |
{ |
1193 |
+ |
if (kernel_area >= 0) { |
1194 |
+ |
shmdt(Mac2HostAddr(KERNEL_DATA_BASE & -SHMLBA)); |
1195 |
+ |
shmdt(Mac2HostAddr(KERNEL_DATA2_BASE & -SHMLBA)); |
1196 |
+ |
shmctl(kernel_area, IPC_RMID, NULL); |
1197 |
+ |
} |
1198 |
+ |
} |
1199 |
+ |
|
1200 |
+ |
|
1201 |
+ |
/* |
1202 |
|
* Jump into Mac ROM, start 680x0 emulator |
1203 |
|
*/ |
1204 |
|
|
1205 |
|
#if EMULATED_PPC |
842 |
– |
extern void emul_ppc(uint32 start); |
843 |
– |
extern void init_emul_ppc(void); |
1206 |
|
void jump_to_rom(uint32 entry) |
1207 |
|
{ |
1208 |
|
init_emul_ppc(); |
1226 |
|
// Jump to ROM boot routine |
1227 |
|
D(bug("Jumping to ROM\n")); |
1228 |
|
#if EMULATED_PPC |
1229 |
< |
jump_to_rom(ROM_BASE + 0x310000); |
1229 |
> |
jump_to_rom(ROMBase + 0x310000); |
1230 |
|
#else |
1231 |
< |
jump_to_rom(ROM_BASE + 0x310000, (uint32)emulator_data); |
1231 |
> |
jump_to_rom(ROMBase + 0x310000, (uint32)emulator_data); |
1232 |
|
#endif |
1233 |
|
D(bug("Returned from ROM\n")); |
1234 |
|
|
1271 |
|
|
1272 |
|
|
1273 |
|
/* |
912 |
– |
* Execute PPC code from EMUL_OP routine (real mode switch) |
913 |
– |
*/ |
914 |
– |
|
915 |
– |
void ExecutePPC(void (*func)()) |
916 |
– |
{ |
917 |
– |
uint32 tvect[2] = {(uint32)func, 0}; // Fake TVECT |
918 |
– |
RoutineDescriptor desc = BUILD_PPC_ROUTINE_DESCRIPTOR(0, tvect); |
919 |
– |
M68kRegisters r; |
920 |
– |
Execute68k((uint32)&desc, &r); |
921 |
– |
} |
922 |
– |
|
923 |
– |
|
924 |
– |
/* |
1274 |
|
* Quit emulator (cause return from jump_to_rom) |
1275 |
|
*/ |
1276 |
|
|
1285 |
|
|
1286 |
|
|
1287 |
|
/* |
939 |
– |
* Pause/resume emulator |
940 |
– |
*/ |
941 |
– |
|
942 |
– |
void PauseEmulator(void) |
943 |
– |
{ |
944 |
– |
pthread_kill(emul_thread, SIGSTOP); |
945 |
– |
} |
946 |
– |
|
947 |
– |
void ResumeEmulator(void) |
948 |
– |
{ |
949 |
– |
pthread_kill(emul_thread, SIGCONT); |
950 |
– |
} |
951 |
– |
|
952 |
– |
|
953 |
– |
/* |
1288 |
|
* Dump 68k registers |
1289 |
|
*/ |
1290 |
|
|
1312 |
|
* Make code executable |
1313 |
|
*/ |
1314 |
|
|
1315 |
< |
void MakeExecutable(int dummy, void *start, uint32 length) |
1315 |
> |
void MakeExecutable(int dummy, uint32 start, uint32 length) |
1316 |
|
{ |
1317 |
< |
#if !EMULATED_PPC |
984 |
< |
if (((uint32)start >= ROM_BASE) && ((uint32)start < (ROM_BASE + ROM_SIZE))) |
1317 |
> |
if ((start >= ROMBase) && (start < (ROMBase + ROM_SIZE))) |
1318 |
|
return; |
1319 |
< |
flush_icache_range(start, (void *)((uint32)start + length)); |
1319 |
> |
#if EMULATED_PPC |
1320 |
> |
FlushCodeCache(start, start + length); |
1321 |
> |
#else |
1322 |
> |
flush_icache_range(start, start + length); |
1323 |
|
#endif |
1324 |
|
} |
1325 |
|
|
1326 |
|
|
1327 |
|
/* |
1328 |
< |
* Patch things after system startup (gets called by disk driver accRun routine) |
1328 |
> |
* NVRAM watchdog thread (saves NVRAM every minute) |
1329 |
|
*/ |
1330 |
|
|
1331 |
< |
void PatchAfterStartup(void) |
1331 |
> |
static void nvram_watchdog(void) |
1332 |
|
{ |
1333 |
< |
ExecutePPC(VideoInstallAccel); |
1334 |
< |
InstallExtFS(); |
1333 |
> |
if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) { |
1334 |
> |
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
1335 |
> |
SaveXPRAM(); |
1336 |
> |
} |
1337 |
|
} |
1338 |
|
|
1001 |
– |
|
1002 |
– |
/* |
1003 |
– |
* NVRAM watchdog thread (saves NVRAM every minute) |
1004 |
– |
*/ |
1005 |
– |
|
1339 |
|
static void *nvram_func(void *arg) |
1340 |
|
{ |
1341 |
< |
struct timespec req = {60, 0}; // 1 minute |
1342 |
< |
|
1343 |
< |
for (;;) { |
1344 |
< |
pthread_testcancel(); |
1012 |
< |
nanosleep(&req, NULL); |
1013 |
< |
pthread_testcancel(); |
1014 |
< |
if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) { |
1015 |
< |
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
1016 |
< |
SaveXPRAM(); |
1017 |
< |
} |
1341 |
> |
while (!nvram_thread_cancel) { |
1342 |
> |
for (int i=0; i<60 && !nvram_thread_cancel; i++) |
1343 |
> |
Delay_usec(999999); // Only wait 1 second so we quit promptly when nvram_thread_cancel becomes true |
1344 |
> |
nvram_watchdog(); |
1345 |
|
} |
1346 |
|
return NULL; |
1347 |
|
} |
1354 |
|
static void *tick_func(void *arg) |
1355 |
|
{ |
1356 |
|
int tick_counter = 0; |
1357 |
< |
struct timespec req = {0, 16625000}; |
1357 |
> |
uint64 start = GetTicks_usec(); |
1358 |
> |
int64 ticks = 0; |
1359 |
> |
uint64 next = GetTicks_usec(); |
1360 |
|
|
1361 |
< |
for (;;) { |
1361 |
> |
while (!tick_thread_cancel) { |
1362 |
|
|
1363 |
|
// Wait |
1364 |
< |
nanosleep(&req, NULL); |
1364 |
> |
next += 16625; |
1365 |
> |
int64 delay = next - GetTicks_usec(); |
1366 |
> |
if (delay > 0) |
1367 |
> |
Delay_usec(delay); |
1368 |
> |
else if (delay < -16625) |
1369 |
> |
next = GetTicks_usec(); |
1370 |
> |
ticks++; |
1371 |
|
|
1372 |
|
#if !EMULATED_PPC |
1373 |
|
// Did we crash? |
1374 |
|
if (emul_thread_fatal) { |
1375 |
|
|
1376 |
|
// Yes, dump registers |
1377 |
< |
pt_regs *r = (pt_regs *)&sigsegv_regs; |
1377 |
> |
sigregs *r = &sigsegv_regs; |
1378 |
|
char str[256]; |
1379 |
< |
sprintf(str, "SIGSEGV\n" |
1379 |
> |
if (crash_reason == NULL) |
1380 |
> |
crash_reason = "SIGSEGV"; |
1381 |
> |
sprintf(str, "%s\n" |
1382 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1383 |
|
" xer %08lx cr %08lx \n" |
1384 |
|
" r0 %08lx r1 %08lx r2 %08lx r3 %08lx\n" |
1389 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1390 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1391 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1392 |
+ |
crash_reason, |
1393 |
|
r->nip, r->link, r->ctr, r->msr, |
1394 |
|
r->xer, r->ccr, |
1395 |
|
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1425 |
|
TriggerInterrupt(); |
1426 |
|
} |
1427 |
|
} |
1428 |
+ |
|
1429 |
+ |
uint64 end = GetTicks_usec(); |
1430 |
+ |
D(bug("%lld ticks in %lld usec = %f ticks/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start))); |
1431 |
|
return NULL; |
1432 |
|
} |
1433 |
|
|
1438 |
|
|
1439 |
|
void Set_pthread_attr(pthread_attr_t *attr, int priority) |
1440 |
|
{ |
1441 |
< |
// nothing to do |
1441 |
> |
#ifdef HAVE_PTHREADS |
1442 |
> |
pthread_attr_init(attr); |
1443 |
> |
#if defined(_POSIX_THREAD_PRIORITY_SCHEDULING) |
1444 |
> |
// Some of these only work for superuser |
1445 |
> |
if (geteuid() == 0) { |
1446 |
> |
pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED); |
1447 |
> |
pthread_attr_setschedpolicy(attr, SCHED_FIFO); |
1448 |
> |
struct sched_param fifo_param; |
1449 |
> |
fifo_param.sched_priority = ((sched_get_priority_min(SCHED_FIFO) + |
1450 |
> |
sched_get_priority_max(SCHED_FIFO)) / 2 + |
1451 |
> |
priority); |
1452 |
> |
pthread_attr_setschedparam(attr, &fifo_param); |
1453 |
> |
} |
1454 |
> |
if (pthread_attr_setscope(attr, PTHREAD_SCOPE_SYSTEM) != 0) { |
1455 |
> |
#ifdef PTHREAD_SCOPE_BOUND_NP |
1456 |
> |
// If system scope is not available (eg. we're not running |
1457 |
> |
// with CAP_SCHED_MGT capability on an SGI box), try bound |
1458 |
> |
// scope. It exposes pthread scheduling to the kernel, |
1459 |
> |
// without setting realtime priority. |
1460 |
> |
pthread_attr_setscope(attr, PTHREAD_SCOPE_BOUND_NP); |
1461 |
> |
#endif |
1462 |
> |
} |
1463 |
> |
#endif |
1464 |
> |
#endif |
1465 |
|
} |
1466 |
|
|
1467 |
|
|
1469 |
|
* Mutexes |
1470 |
|
*/ |
1471 |
|
|
1472 |
+ |
#ifdef HAVE_PTHREADS |
1473 |
+ |
|
1474 |
+ |
struct B2_mutex { |
1475 |
+ |
B2_mutex() { |
1476 |
+ |
pthread_mutexattr_t attr; |
1477 |
+ |
pthread_mutexattr_init(&attr); |
1478 |
+ |
// Initialize the mutex for priority inheritance -- |
1479 |
+ |
// required for accurate timing. |
1480 |
+ |
#if defined(HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL) && !defined(__CYGWIN__) |
1481 |
+ |
pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT); |
1482 |
+ |
#endif |
1483 |
+ |
#if defined(HAVE_PTHREAD_MUTEXATTR_SETTYPE) && defined(PTHREAD_MUTEX_NORMAL) |
1484 |
+ |
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); |
1485 |
+ |
#endif |
1486 |
+ |
#ifdef HAVE_PTHREAD_MUTEXATTR_SETPSHARED |
1487 |
+ |
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE); |
1488 |
+ |
#endif |
1489 |
+ |
pthread_mutex_init(&m, &attr); |
1490 |
+ |
pthread_mutexattr_destroy(&attr); |
1491 |
+ |
} |
1492 |
+ |
~B2_mutex() { |
1493 |
+ |
pthread_mutex_trylock(&m); // Make sure it's locked before |
1494 |
+ |
pthread_mutex_unlock(&m); // unlocking it. |
1495 |
+ |
pthread_mutex_destroy(&m); |
1496 |
+ |
} |
1497 |
+ |
pthread_mutex_t m; |
1498 |
+ |
}; |
1499 |
+ |
|
1500 |
+ |
B2_mutex *B2_create_mutex(void) |
1501 |
+ |
{ |
1502 |
+ |
return new B2_mutex; |
1503 |
+ |
} |
1504 |
+ |
|
1505 |
+ |
void B2_lock_mutex(B2_mutex *mutex) |
1506 |
+ |
{ |
1507 |
+ |
pthread_mutex_lock(&mutex->m); |
1508 |
+ |
} |
1509 |
+ |
|
1510 |
+ |
void B2_unlock_mutex(B2_mutex *mutex) |
1511 |
+ |
{ |
1512 |
+ |
pthread_mutex_unlock(&mutex->m); |
1513 |
+ |
} |
1514 |
+ |
|
1515 |
+ |
void B2_delete_mutex(B2_mutex *mutex) |
1516 |
+ |
{ |
1517 |
+ |
delete mutex; |
1518 |
+ |
} |
1519 |
+ |
|
1520 |
+ |
#else |
1521 |
+ |
|
1522 |
|
struct B2_mutex { |
1523 |
|
int dummy; |
1524 |
|
}; |
1541 |
|
delete mutex; |
1542 |
|
} |
1543 |
|
|
1544 |
+ |
#endif |
1545 |
+ |
|
1546 |
|
|
1547 |
|
/* |
1548 |
|
* Trigger signal USR2 from another thread |
1549 |
|
*/ |
1550 |
|
|
1551 |
+ |
#if !EMULATED_PPC |
1552 |
|
void TriggerInterrupt(void) |
1553 |
|
{ |
1554 |
< |
#if EMULATED_PPC |
1555 |
< |
WriteMacInt32(0x16a, ReadMacInt32(0x16a) + 1); |
1139 |
< |
#else |
1140 |
< |
#if 0 |
1141 |
< |
WriteMacInt32(0x16a, ReadMacInt32(0x16a) + 1); |
1142 |
< |
#else |
1143 |
< |
if (ready_for_signals) |
1554 |
> |
if (ready_for_signals) { |
1555 |
> |
idle_resume(); |
1556 |
|
pthread_kill(emul_thread, SIGUSR2); |
1557 |
< |
#endif |
1146 |
< |
#endif |
1557 |
> |
} |
1558 |
|
} |
1559 |
+ |
#endif |
1560 |
|
|
1561 |
|
|
1562 |
|
/* |
1582 |
|
|
1583 |
|
void DisableInterrupt(void) |
1584 |
|
{ |
1585 |
+ |
#if EMULATED_PPC |
1586 |
+ |
WriteMacInt32(XLM_IRQ_NEST, int32(ReadMacInt32(XLM_IRQ_NEST)) + 1); |
1587 |
+ |
#else |
1588 |
|
atomic_add((int *)XLM_IRQ_NEST, 1); |
1589 |
+ |
#endif |
1590 |
|
} |
1591 |
|
|
1592 |
|
|
1596 |
|
|
1597 |
|
void EnableInterrupt(void) |
1598 |
|
{ |
1599 |
+ |
#if EMULATED_PPC |
1600 |
+ |
WriteMacInt32(XLM_IRQ_NEST, int32(ReadMacInt32(XLM_IRQ_NEST)) - 1); |
1601 |
+ |
#else |
1602 |
|
atomic_add((int *)XLM_IRQ_NEST, -1); |
1603 |
+ |
#endif |
1604 |
|
} |
1605 |
|
|
1606 |
|
|
1187 |
– |
#if !EMULATED_PPC |
1607 |
|
/* |
1608 |
|
* USR2 handler |
1609 |
|
*/ |
1610 |
|
|
1611 |
< |
static void sigusr2_handler(int sig, sigcontext_struct *sc) |
1611 |
> |
#if !EMULATED_PPC |
1612 |
> |
void sigusr2_handler(int sig, siginfo_t *sip, void *scp) |
1613 |
|
{ |
1614 |
< |
pt_regs *r = sc->regs; |
1614 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1615 |
> |
|
1616 |
> |
#ifdef SYSTEM_CLOBBERS_R2 |
1617 |
> |
// Restore pointer to Thread Local Storage |
1618 |
> |
set_r2(TOC); |
1619 |
> |
#endif |
1620 |
> |
#ifdef SYSTEM_CLOBBERS_R13 |
1621 |
> |
// Restore pointer to .sdata section |
1622 |
> |
set_r13(R13); |
1623 |
> |
#endif |
1624 |
> |
|
1625 |
> |
#ifdef USE_SDL_VIDEO |
1626 |
> |
// We must fill in the events queue in the same thread that did call SDL_SetVideoMode() |
1627 |
> |
SDL_PumpEvents(); |
1628 |
> |
#endif |
1629 |
|
|
1630 |
|
// Do nothing if interrupts are disabled |
1631 |
|
if (*(int32 *)XLM_IRQ_NEST > 0) |
1639 |
|
case MODE_68K: |
1640 |
|
// 68k emulator active, trigger 68k interrupt level 1 |
1641 |
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1642 |
< |
r->ccr |= ntohl(kernel_data->v[0x674 >> 2]); |
1642 |
> |
r->cr() |= ntohl(kernel_data->v[0x674 >> 2]); |
1643 |
|
break; |
1644 |
|
|
1645 |
|
#if INTERRUPTS_IN_NATIVE_MODE |
1646 |
|
case MODE_NATIVE: |
1647 |
|
// 68k emulator inactive, in nanokernel? |
1648 |
< |
if (r->gpr[1] != KernelDataAddr) { |
1648 |
> |
if (r->gpr(1) != KernelDataAddr) { |
1649 |
> |
|
1650 |
> |
// Set extra stack for SIGSEGV handler |
1651 |
> |
sigaltstack(&extra_stack, NULL); |
1652 |
> |
|
1653 |
|
// Prepare for 68k interrupt level 1 |
1654 |
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1655 |
|
WriteMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc, ReadMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc) | ntohl(kernel_data->v[0x674 >> 2])); |
1656 |
|
|
1657 |
|
// Execute nanokernel interrupt routine (this will activate the 68k emulator) |
1658 |
< |
atomic_add((int32 *)XLM_IRQ_NEST, 1); |
1658 |
> |
DisableInterrupt(); |
1659 |
|
if (ROMType == ROMTYPE_NEWWORLD) |
1660 |
< |
ppc_interrupt(ROM_BASE + 0x312b1c, KernelDataAddr); |
1660 |
> |
ppc_interrupt(ROMBase + 0x312b1c, KernelDataAddr); |
1661 |
|
else |
1662 |
< |
ppc_interrupt(ROM_BASE + 0x312a3c, KernelDataAddr); |
1662 |
> |
ppc_interrupt(ROMBase + 0x312a3c, KernelDataAddr); |
1663 |
> |
|
1664 |
> |
// Reset normal stack |
1665 |
> |
sigaltstack(&sig_stack, NULL); |
1666 |
|
} |
1667 |
|
break; |
1668 |
|
#endif |
1673 |
|
if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) { |
1674 |
|
|
1675 |
|
// Set extra stack for SIGSEGV handler |
1676 |
< |
struct sigaltstack new_stack; |
1236 |
< |
new_stack.ss_sp = extra_stack; |
1237 |
< |
new_stack.ss_flags = 0; |
1238 |
< |
new_stack.ss_size = SIG_STACK_SIZE; |
1239 |
< |
sigaltstack(&new_stack, NULL); |
1676 |
> |
sigaltstack(&extra_stack, NULL); |
1677 |
|
#if 1 |
1678 |
|
// Execute full 68k interrupt routine |
1679 |
|
M68kRegisters r; |
1695 |
|
if (InterruptFlags & INTFLAG_VIA) { |
1696 |
|
ClearInterruptFlag(INTFLAG_VIA); |
1697 |
|
ADBInterrupt(); |
1698 |
< |
ExecutePPC(VideoVBL); |
1698 |
> |
ExecuteNative(NATIVE_VIDEO_VBL); |
1699 |
|
} |
1700 |
|
} |
1701 |
|
#endif |
1702 |
< |
// Reset normal signal stack |
1703 |
< |
new_stack.ss_sp = sig_stack; |
1267 |
< |
new_stack.ss_flags = 0; |
1268 |
< |
new_stack.ss_size = SIG_STACK_SIZE; |
1269 |
< |
sigaltstack(&new_stack, NULL); |
1702 |
> |
// Reset normal stack |
1703 |
> |
sigaltstack(&sig_stack, NULL); |
1704 |
|
} |
1705 |
|
break; |
1706 |
|
#endif |
1273 |
– |
|
1707 |
|
} |
1708 |
|
} |
1709 |
+ |
#endif |
1710 |
|
|
1711 |
|
|
1712 |
|
/* |
1713 |
|
* SIGSEGV handler |
1714 |
|
*/ |
1715 |
|
|
1716 |
< |
static void sigsegv_handler(int sig, sigcontext_struct *sc) |
1716 |
> |
#if !EMULATED_PPC |
1717 |
> |
static void sigsegv_handler(int sig, siginfo_t *sip, void *scp) |
1718 |
|
{ |
1719 |
< |
pt_regs *r = sc->regs; |
1719 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1720 |
> |
|
1721 |
> |
// Get effective address |
1722 |
> |
uint32 addr = r->dar(); |
1723 |
> |
|
1724 |
> |
#ifdef SYSTEM_CLOBBERS_R2 |
1725 |
> |
// Restore pointer to Thread Local Storage |
1726 |
> |
set_r2(TOC); |
1727 |
> |
#endif |
1728 |
> |
#ifdef SYSTEM_CLOBBERS_R13 |
1729 |
> |
// Restore pointer to .sdata section |
1730 |
> |
set_r13(R13); |
1731 |
> |
#endif |
1732 |
> |
|
1733 |
> |
#if ENABLE_VOSF |
1734 |
> |
// Handle screen fault |
1735 |
> |
#if SIGSEGV_CHECK_VERSION(1,0,0) |
1736 |
> |
sigsegv_info_t si; |
1737 |
> |
si.addr = (sigsegv_address_t)addr; |
1738 |
> |
si.pc = (sigsegv_address_t)r->pc(); |
1739 |
> |
#endif |
1740 |
> |
extern bool Screen_fault_handler(sigsegv_info_t *sip); |
1741 |
> |
if (Screen_fault_handler(&si)) |
1742 |
> |
return; |
1743 |
> |
#endif |
1744 |
> |
|
1745 |
|
num_segv++; |
1746 |
|
|
1747 |
< |
// Fault in Mac ROM or RAM? |
1748 |
< |
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
1747 |
> |
// Fault in Mac ROM or RAM or DR Cache? |
1748 |
> |
bool mac_fault = (r->pc() >= ROMBase) && (r->pc() < (ROMBase + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize)) || (r->pc() >= DR_CACHE_BASE && r->pc() < (DR_CACHE_BASE + DR_CACHE_SIZE)); |
1749 |
|
if (mac_fault) { |
1750 |
|
|
1291 |
– |
// Get opcode and divide into fields |
1292 |
– |
uint32 opcode = *((uint32 *)r->nip); |
1293 |
– |
uint32 primop = opcode >> 26; |
1294 |
– |
uint32 exop = (opcode >> 1) & 0x3ff; |
1295 |
– |
uint32 ra = (opcode >> 16) & 0x1f; |
1296 |
– |
uint32 rb = (opcode >> 11) & 0x1f; |
1297 |
– |
uint32 rd = (opcode >> 21) & 0x1f; |
1298 |
– |
int32 imm = (int16)(opcode & 0xffff); |
1299 |
– |
|
1751 |
|
// "VM settings" during MacOS 8 installation |
1752 |
< |
if (r->nip == ROM_BASE + 0x488160 && r->gpr[20] == 0xf8000000) { |
1753 |
< |
r->nip += 4; |
1754 |
< |
r->gpr[8] = 0; |
1752 |
> |
if (r->pc() == ROMBase + 0x488160 && r->gpr(20) == 0xf8000000) { |
1753 |
> |
r->pc() += 4; |
1754 |
> |
r->gpr(8) = 0; |
1755 |
|
return; |
1756 |
|
|
1757 |
|
// MacOS 8.5 installation |
1758 |
< |
} else if (r->nip == ROM_BASE + 0x488140 && r->gpr[16] == 0xf8000000) { |
1759 |
< |
r->nip += 4; |
1760 |
< |
r->gpr[8] = 0; |
1758 |
> |
} else if (r->pc() == ROMBase + 0x488140 && r->gpr(16) == 0xf8000000) { |
1759 |
> |
r->pc() += 4; |
1760 |
> |
r->gpr(8) = 0; |
1761 |
|
return; |
1762 |
|
|
1763 |
|
// MacOS 8 serial drivers on startup |
1764 |
< |
} else if (r->nip == ROM_BASE + 0x48e080 && (r->gpr[8] == 0xf3012002 || r->gpr[8] == 0xf3012000)) { |
1765 |
< |
r->nip += 4; |
1766 |
< |
r->gpr[8] = 0; |
1764 |
> |
} else if (r->pc() == ROMBase + 0x48e080 && (r->gpr(8) == 0xf3012002 || r->gpr(8) == 0xf3012000)) { |
1765 |
> |
r->pc() += 4; |
1766 |
> |
r->gpr(8) = 0; |
1767 |
|
return; |
1768 |
|
|
1769 |
|
// MacOS 8.1 serial drivers on startup |
1770 |
< |
} else if (r->nip == ROM_BASE + 0x48c5e0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1771 |
< |
r->nip += 4; |
1770 |
> |
} else if (r->pc() == ROMBase + 0x48c5e0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1771 |
> |
r->pc() += 4; |
1772 |
|
return; |
1773 |
< |
} else if (r->nip == ROM_BASE + 0x4a10a0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1774 |
< |
r->nip += 4; |
1773 |
> |
} else if (r->pc() == ROMBase + 0x4a10a0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1774 |
> |
r->pc() += 4; |
1775 |
> |
return; |
1776 |
> |
|
1777 |
> |
// MacOS 8.6 serial drivers on startup (with DR Cache and OldWorld ROM) |
1778 |
> |
} else if ((r->pc() - DR_CACHE_BASE) < DR_CACHE_SIZE && (r->gpr(16) == 0xf3012002 || r->gpr(16) == 0xf3012000)) { |
1779 |
> |
r->pc() += 4; |
1780 |
> |
return; |
1781 |
> |
} else if ((r->pc() - DR_CACHE_BASE) < DR_CACHE_SIZE && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1782 |
> |
r->pc() += 4; |
1783 |
|
return; |
1784 |
|
} |
1785 |
|
|
1786 |
+ |
// Get opcode and divide into fields |
1787 |
+ |
uint32 opcode = *((uint32 *)r->pc()); |
1788 |
+ |
uint32 primop = opcode >> 26; |
1789 |
+ |
uint32 exop = (opcode >> 1) & 0x3ff; |
1790 |
+ |
uint32 ra = (opcode >> 16) & 0x1f; |
1791 |
+ |
uint32 rb = (opcode >> 11) & 0x1f; |
1792 |
+ |
uint32 rd = (opcode >> 21) & 0x1f; |
1793 |
+ |
int32 imm = (int16)(opcode & 0xffff); |
1794 |
+ |
|
1795 |
|
// Analyze opcode |
1796 |
|
enum { |
1797 |
|
TYPE_UNKNOWN, |
1873 |
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_NORM; break; |
1874 |
|
case 45: // sthu |
1875 |
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_U; break; |
1876 |
< |
} |
1877 |
< |
|
1878 |
< |
// Calculate effective address |
1879 |
< |
uint32 addr = 0; |
1880 |
< |
switch (addr_mode) { |
1881 |
< |
case MODE_X: |
1882 |
< |
case MODE_UX: |
1883 |
< |
if (ra == 0) |
1884 |
< |
addr = r->gpr[rb]; |
1885 |
< |
else |
1886 |
< |
addr = r->gpr[ra] + r->gpr[rb]; |
1887 |
< |
break; |
1420 |
< |
case MODE_NORM: |
1421 |
< |
case MODE_U: |
1422 |
< |
if (ra == 0) |
1423 |
< |
addr = (int32)(int16)imm; |
1424 |
< |
else |
1425 |
< |
addr = r->gpr[ra] + (int32)(int16)imm; |
1876 |
> |
#if EMULATE_UNALIGNED_LOADSTORE_MULTIPLE |
1877 |
> |
case 46: // lmw |
1878 |
> |
if ((addr % 4) != 0) { |
1879 |
> |
uint32 ea = addr; |
1880 |
> |
D(bug("WARNING: unaligned lmw to EA=%08x from IP=%08x\n", ea, r->pc())); |
1881 |
> |
for (int i = rd; i <= 31; i++) { |
1882 |
> |
r->gpr(i) = ReadMacInt32(ea); |
1883 |
> |
ea += 4; |
1884 |
> |
} |
1885 |
> |
r->pc() += 4; |
1886 |
> |
goto rti; |
1887 |
> |
} |
1888 |
|
break; |
1889 |
< |
default: |
1889 |
> |
case 47: // stmw |
1890 |
> |
if ((addr % 4) != 0) { |
1891 |
> |
uint32 ea = addr; |
1892 |
> |
D(bug("WARNING: unaligned stmw to EA=%08x from IP=%08x\n", ea, r->pc())); |
1893 |
> |
for (int i = rd; i <= 31; i++) { |
1894 |
> |
WriteMacInt32(ea, r->gpr(i)); |
1895 |
> |
ea += 4; |
1896 |
> |
} |
1897 |
> |
r->pc() += 4; |
1898 |
> |
goto rti; |
1899 |
> |
} |
1900 |
|
break; |
1901 |
+ |
#endif |
1902 |
|
} |
1903 |
< |
|
1904 |
< |
// Ignore ROM writes |
1905 |
< |
if (transfer_type == TYPE_STORE && addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) { |
1906 |
< |
// 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)); |
1903 |
> |
|
1904 |
> |
// Ignore ROM writes (including to the zero page, which is read-only) |
1905 |
> |
if (transfer_type == TYPE_STORE && |
1906 |
> |
((addr >= ROMBase && addr < ROMBase + ROM_SIZE) || |
1907 |
> |
(addr >= SheepMem::ZeroPage() && addr < SheepMem::ZeroPage() + SheepMem::PageSize()))) { |
1908 |
> |
// 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())); |
1909 |
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1910 |
< |
r->gpr[ra] = addr; |
1911 |
< |
r->nip += 4; |
1910 |
> |
r->gpr(ra) = addr; |
1911 |
> |
r->pc() += 4; |
1912 |
|
goto rti; |
1913 |
|
} |
1914 |
|
|
1915 |
|
// Ignore illegal memory accesses? |
1916 |
|
if (PrefsFindBool("ignoresegv")) { |
1917 |
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1918 |
< |
r->gpr[ra] = addr; |
1918 |
> |
r->gpr(ra) = addr; |
1919 |
|
if (transfer_type == TYPE_LOAD) |
1920 |
< |
r->gpr[rd] = 0; |
1921 |
< |
r->nip += 4; |
1920 |
> |
r->gpr(rd) = 0; |
1921 |
> |
r->pc() += 4; |
1922 |
|
goto rti; |
1923 |
|
} |
1924 |
|
|
1926 |
|
if (!PrefsFindBool("nogui")) { |
1927 |
|
char str[256]; |
1928 |
|
if (transfer_type == TYPE_LOAD || transfer_type == TYPE_STORE) |
1929 |
< |
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]); |
1929 |
> |
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)); |
1930 |
|
else |
1931 |
< |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode); |
1931 |
> |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode); |
1932 |
|
ErrorAlert(str); |
1933 |
|
QuitEmulator(); |
1934 |
|
return; |
1936 |
|
} |
1937 |
|
|
1938 |
|
// For all other errors, jump into debugger (sort of...) |
1939 |
+ |
crash_reason = (sig == SIGBUS) ? "SIGBUS" : "SIGSEGV"; |
1940 |
|
if (!ready_for_signals) { |
1941 |
< |
printf("SIGSEGV\n"); |
1942 |
< |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
1941 |
> |
printf("%s\n"); |
1942 |
> |
printf(" sigcontext %p, machine_regs %p\n", scp, r); |
1943 |
|
printf( |
1944 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1945 |
|
" xer %08lx cr %08lx \n" |
1951 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1952 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1953 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1954 |
< |
r->nip, r->link, r->ctr, r->msr, |
1955 |
< |
r->xer, r->ccr, |
1956 |
< |
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1957 |
< |
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
1958 |
< |
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
1959 |
< |
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
1960 |
< |
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
1961 |
< |
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
1962 |
< |
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
1963 |
< |
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
1954 |
> |
crash_reason, |
1955 |
> |
r->pc(), r->lr(), r->ctr(), r->msr(), |
1956 |
> |
r->xer(), r->cr(), |
1957 |
> |
r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3), |
1958 |
> |
r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7), |
1959 |
> |
r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11), |
1960 |
> |
r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15), |
1961 |
> |
r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19), |
1962 |
> |
r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23), |
1963 |
> |
r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27), |
1964 |
> |
r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31)); |
1965 |
|
exit(1); |
1966 |
|
QuitEmulator(); |
1967 |
|
return; |
1968 |
|
} else { |
1969 |
|
// We crashed. Save registers, tell tick thread and loop forever |
1970 |
< |
sigsegv_regs = *(sigregs *)r; |
1970 |
> |
build_sigregs(&sigsegv_regs, r); |
1971 |
|
emul_thread_fatal = true; |
1972 |
|
for (;;) ; |
1973 |
|
} |
1979 |
|
* SIGILL handler |
1980 |
|
*/ |
1981 |
|
|
1982 |
< |
static void sigill_handler(int sig, sigcontext_struct *sc) |
1982 |
> |
static void sigill_handler(int sig, siginfo_t *sip, void *scp) |
1983 |
|
{ |
1984 |
< |
pt_regs *r = sc->regs; |
1984 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1985 |
|
char str[256]; |
1986 |
|
|
1987 |
+ |
#ifdef SYSTEM_CLOBBERS_R2 |
1988 |
+ |
// Restore pointer to Thread Local Storage |
1989 |
+ |
set_r2(TOC); |
1990 |
+ |
#endif |
1991 |
+ |
#ifdef SYSTEM_CLOBBERS_R13 |
1992 |
+ |
// Restore pointer to .sdata section |
1993 |
+ |
set_r13(R13); |
1994 |
+ |
#endif |
1995 |
+ |
|
1996 |
|
// Fault in Mac ROM or RAM? |
1997 |
< |
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
1997 |
> |
bool mac_fault = (r->pc() >= ROMBase) && (r->pc() < (ROMBase + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize)); |
1998 |
|
if (mac_fault) { |
1999 |
|
|
2000 |
|
// Get opcode and divide into fields |
2001 |
< |
uint32 opcode = *((uint32 *)r->nip); |
2001 |
> |
uint32 opcode = *((uint32 *)r->pc()); |
2002 |
|
uint32 primop = opcode >> 26; |
2003 |
|
uint32 exop = (opcode >> 1) & 0x3ff; |
2004 |
|
uint32 ra = (opcode >> 16) & 0x1f; |
2009 |
|
switch (primop) { |
2010 |
|
case 9: // POWER instructions |
2011 |
|
case 22: |
2012 |
< |
power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->nip, r->gpr[1], opcode); |
2012 |
> |
power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->pc(), r->gpr(1), opcode); |
2013 |
|
ErrorAlert(str); |
2014 |
|
QuitEmulator(); |
2015 |
|
return; |
2017 |
|
case 31: |
2018 |
|
switch (exop) { |
2019 |
|
case 83: // mfmsr |
2020 |
< |
r->gpr[rd] = 0xf072; |
2021 |
< |
r->nip += 4; |
2020 |
> |
r->gpr(rd) = 0xf072; |
2021 |
> |
r->pc() += 4; |
2022 |
|
goto rti; |
2023 |
|
|
2024 |
|
case 210: // mtsr |
2025 |
|
case 242: // mtsrin |
2026 |
|
case 306: // tlbie |
2027 |
< |
r->nip += 4; |
2027 |
> |
r->pc() += 4; |
2028 |
|
goto rti; |
2029 |
|
|
2030 |
|
case 339: { // mfspr |
2040 |
|
case 957: // PMC3 |
2041 |
|
case 958: // PMC4 |
2042 |
|
case 959: // SDA |
2043 |
< |
r->nip += 4; |
2043 |
> |
r->pc() += 4; |
2044 |
|
goto rti; |
2045 |
|
case 25: // SDR1 |
2046 |
< |
r->gpr[rd] = 0xdead001f; |
2047 |
< |
r->nip += 4; |
2046 |
> |
r->gpr(rd) = 0xdead001f; |
2047 |
> |
r->pc() += 4; |
2048 |
|
goto rti; |
2049 |
|
case 287: // PVR |
2050 |
< |
r->gpr[rd] = PVR; |
2051 |
< |
r->nip += 4; |
2050 |
> |
r->gpr(rd) = PVR; |
2051 |
> |
r->pc() += 4; |
2052 |
|
goto rti; |
2053 |
|
} |
2054 |
|
break; |
2084 |
|
case 957: // PMC3 |
2085 |
|
case 958: // PMC4 |
2086 |
|
case 959: // SDA |
2087 |
< |
r->nip += 4; |
2087 |
> |
r->pc() += 4; |
2088 |
|
goto rti; |
2089 |
|
} |
2090 |
|
break; |
2103 |
|
|
2104 |
|
// In GUI mode, show error alert |
2105 |
|
if (!PrefsFindBool("nogui")) { |
2106 |
< |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode); |
2106 |
> |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode); |
2107 |
|
ErrorAlert(str); |
2108 |
|
QuitEmulator(); |
2109 |
|
return; |
2111 |
|
} |
2112 |
|
|
2113 |
|
// For all other errors, jump into debugger (sort of...) |
2114 |
+ |
crash_reason = "SIGILL"; |
2115 |
|
if (!ready_for_signals) { |
2116 |
< |
printf("SIGILL\n"); |
2117 |
< |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
2116 |
> |
printf("%s\n"); |
2117 |
> |
printf(" sigcontext %p, machine_regs %p\n", scp, r); |
2118 |
|
printf( |
2119 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
2120 |
|
" xer %08lx cr %08lx \n" |
2126 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
2127 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
2128 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
2129 |
< |
r->nip, r->link, r->ctr, r->msr, |
2130 |
< |
r->xer, r->ccr, |
2131 |
< |
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
2132 |
< |
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
2133 |
< |
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
2134 |
< |
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
2135 |
< |
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
2136 |
< |
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
2137 |
< |
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
2138 |
< |
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
2129 |
> |
crash_reason, |
2130 |
> |
r->pc(), r->lr(), r->ctr(), r->msr(), |
2131 |
> |
r->xer(), r->cr(), |
2132 |
> |
r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3), |
2133 |
> |
r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7), |
2134 |
> |
r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11), |
2135 |
> |
r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15), |
2136 |
> |
r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19), |
2137 |
> |
r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23), |
2138 |
> |
r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27), |
2139 |
> |
r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31)); |
2140 |
|
exit(1); |
2141 |
|
QuitEmulator(); |
2142 |
|
return; |
2143 |
|
} else { |
2144 |
|
// We crashed. Save registers, tell tick thread and loop forever |
2145 |
< |
sigsegv_regs = *(sigregs *)r; |
2145 |
> |
build_sigregs(&sigsegv_regs, r); |
2146 |
|
emul_thread_fatal = true; |
2147 |
|
for (;;) ; |
2148 |
|
} |
2152 |
|
|
2153 |
|
|
2154 |
|
/* |
2155 |
+ |
* Helpers to share 32-bit addressable data with MacOS |
2156 |
+ |
*/ |
2157 |
+ |
|
2158 |
+ |
bool SheepMem::Init(void) |
2159 |
+ |
{ |
2160 |
+ |
// Size of a native page |
2161 |
+ |
page_size = getpagesize(); |
2162 |
+ |
|
2163 |
+ |
// Allocate SheepShaver globals |
2164 |
+ |
proc = base; |
2165 |
+ |
if (vm_mac_acquire_fixed(base, size) < 0) |
2166 |
+ |
return false; |
2167 |
+ |
|
2168 |
+ |
// Allocate page with all bits set to 0, right in the middle |
2169 |
+ |
// This is also used to catch undesired overlaps between proc and data areas |
2170 |
+ |
zero_page = proc + (size / 2); |
2171 |
+ |
Mac_memset(zero_page, 0, page_size); |
2172 |
+ |
if (vm_protect(Mac2HostAddr(zero_page), page_size, VM_PAGE_READ) < 0) |
2173 |
+ |
return false; |
2174 |
+ |
|
2175 |
+ |
#if EMULATED_PPC |
2176 |
+ |
// Allocate alternate stack for PowerPC interrupt routine |
2177 |
+ |
sig_stack = base + size; |
2178 |
+ |
if (vm_mac_acquire_fixed(sig_stack, SIG_STACK_SIZE) < 0) |
2179 |
+ |
return false; |
2180 |
+ |
#endif |
2181 |
+ |
|
2182 |
+ |
data = base + size; |
2183 |
+ |
return true; |
2184 |
+ |
} |
2185 |
+ |
|
2186 |
+ |
void SheepMem::Exit(void) |
2187 |
+ |
{ |
2188 |
+ |
if (data) { |
2189 |
+ |
// Delete SheepShaver globals |
2190 |
+ |
vm_mac_release(base, size); |
2191 |
+ |
|
2192 |
+ |
#if EMULATED_PPC |
2193 |
+ |
// Delete alternate stack for PowerPC interrupt routine |
2194 |
+ |
vm_mac_release(sig_stack, SIG_STACK_SIZE); |
2195 |
+ |
#endif |
2196 |
+ |
} |
2197 |
+ |
} |
2198 |
+ |
|
2199 |
+ |
|
2200 |
+ |
/* |
2201 |
|
* Display alert |
2202 |
|
*/ |
2203 |
|
|
2246 |
|
|
2247 |
|
void ErrorAlert(const char *text) |
2248 |
|
{ |
2249 |
< |
#ifdef ENABLE_GTK |
2249 |
> |
if (gui_connection) { |
2250 |
> |
if (rpc_method_invoke(gui_connection, RPC_METHOD_ERROR_ALERT, RPC_TYPE_STRING, text, RPC_TYPE_INVALID) == RPC_ERROR_NO_ERROR && |
2251 |
> |
rpc_method_wait_for_reply(gui_connection, RPC_TYPE_INVALID) == RPC_ERROR_NO_ERROR) |
2252 |
> |
return; |
2253 |
> |
} |
2254 |
> |
#if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO) |
2255 |
|
if (PrefsFindBool("nogui") || x_display == NULL) { |
2256 |
|
printf(GetString(STR_SHELL_ERROR_PREFIX), text); |
2257 |
|
return; |
2270 |
|
|
2271 |
|
void WarningAlert(const char *text) |
2272 |
|
{ |
2273 |
< |
#ifdef ENABLE_GTK |
2273 |
> |
if (gui_connection) { |
2274 |
> |
if (rpc_method_invoke(gui_connection, RPC_METHOD_WARNING_ALERT, RPC_TYPE_STRING, text, RPC_TYPE_INVALID) == RPC_ERROR_NO_ERROR && |
2275 |
> |
rpc_method_wait_for_reply(gui_connection, RPC_TYPE_INVALID) == RPC_ERROR_NO_ERROR) |
2276 |
> |
return; |
2277 |
> |
} |
2278 |
> |
#if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO) |
2279 |
|
if (PrefsFindBool("nogui") || x_display == NULL) { |
2280 |
|
printf(GetString(STR_SHELL_WARNING_PREFIX), text); |
2281 |
|
return; |