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 RAM_BASE = 0x20000000; // Base address of RAM |
165 |
< |
const uint32 SIG_STACK_SIZE = 0x10000; // Size of signal stack |
166 |
< |
|
167 |
< |
|
168 |
< |
#if !EMULATED_PPC |
169 |
< |
// Structure in which registers are saved in a signal handler; |
170 |
< |
// sigcontext->regs points to it |
155 |
< |
// (see arch/ppc/kernel/signal.c) |
156 |
< |
typedef struct { |
157 |
< |
uint32 u[4]; |
158 |
< |
} __attribute((aligned(16))) vector128; |
159 |
< |
#include <linux/elf.h> |
160 |
< |
|
161 |
< |
struct sigregs { |
162 |
< |
elf_gregset_t gp_regs; // Identical to pt_regs |
163 |
< |
double fp_regs[ELF_NFPREG]; // f0..f31 and fpsrc |
164 |
< |
//more (uninteresting) stuff following here |
165 |
< |
}; |
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 SheepStack1Base; // SheepShaver first alternate stack base |
176 |
< |
uint32 SheepStack2Base; // SheepShaver second alternate stack base |
177 |
< |
uint32 SheepThunksBase; // SheepShaver thunks base |
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 |
+ |
#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 |
190 |
– |
static bool sheep_area_mapped = false; // Flag: SheepShaver data area mmap()ed |
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 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 |
207 |
– |
static int64 num_segv = 0; // Number of handled SEGV signals |
224 |
|
|
225 |
+ |
#if EMULATED_PPC |
226 |
+ |
static uintptr sig_stack = 0; // Stack for PowerPC interrupt routine |
227 |
+ |
#else |
228 |
|
static struct sigaction sigusr2_action; // Interrupt signal (of emulator thread) |
210 |
– |
#if !EMULATED_PPC |
229 |
|
static struct sigaction sigsegv_action; // Data access exception signal (of emulator thread) |
230 |
|
static struct sigaction sigill_action; // Illegal instruction signal (of emulator thread) |
231 |
< |
static void *sig_stack = NULL; // Stack for signal handlers |
232 |
< |
static void *extra_stack = NULL; // Stack for SIGSEGV inside interrupt handler |
231 |
> |
static stack_t sig_stack; // Stack for signal handlers |
232 |
> |
static stack_t extra_stack; // Stack for SIGSEGV inside interrupt handler |
233 |
|
static bool emul_thread_fatal = false; // Flag: MacOS thread crashed, tick thread shall dump debug output |
234 |
|
static sigregs sigsegv_regs; // Register dump when crashed |
235 |
+ |
static const char *crash_reason = NULL; // Reason of the crash (SIGSEGV, SIGBUS, SIGILL) |
236 |
|
#endif |
237 |
|
|
238 |
+ |
static rpc_connection_t *gui_connection = NULL; // RPC connection to the GUI |
239 |
+ |
static const char *gui_connection_path = NULL; // GUI connection identifier |
240 |
+ |
|
241 |
+ |
uint32 SheepMem::page_size; // Size of a native page |
242 |
+ |
uintptr SheepMem::zero_page = 0; // Address of ro page filled in with zeros |
243 |
+ |
uintptr SheepMem::base = 0x60000000; // Address of SheepShaver data |
244 |
+ |
uintptr SheepMem::proc; // Bottom address of SheepShave procedures |
245 |
+ |
uintptr SheepMem::data; // Top of SheepShaver data (stack like storage) |
246 |
+ |
|
247 |
|
|
248 |
|
// Prototypes |
249 |
+ |
static bool kernel_data_init(void); |
250 |
+ |
static void kernel_data_exit(void); |
251 |
|
static void Quit(void); |
252 |
|
static void *emul_func(void *arg); |
253 |
|
static void *nvram_func(void *arg); |
254 |
|
static void *tick_func(void *arg); |
255 |
|
#if EMULATED_PPC |
226 |
– |
static void sigusr2_handler(int sig); |
256 |
|
extern void emul_ppc(uint32 start); |
257 |
|
extern void init_emul_ppc(void); |
258 |
|
extern void exit_emul_ppc(void); |
259 |
+ |
sigsegv_return_t sigsegv_handler(sigsegv_info_t *sip); |
260 |
|
#else |
261 |
< |
static void sigusr2_handler(int sig, sigcontext_struct *sc); |
262 |
< |
static void sigsegv_handler(int sig, sigcontext_struct *sc); |
263 |
< |
static void sigill_handler(int sig, sigcontext_struct *sc); |
261 |
> |
extern "C" void sigusr2_handler_init(int sig, siginfo_t *sip, void *scp); |
262 |
> |
extern "C" void sigusr2_handler(int sig, siginfo_t *sip, void *scp); |
263 |
> |
static void sigsegv_handler(int sig, siginfo_t *sip, void *scp); |
264 |
> |
static void sigill_handler(int sig, siginfo_t *sip, void *scp); |
265 |
|
#endif |
266 |
|
|
267 |
|
|
268 |
|
// From asm_linux.S |
269 |
|
#if !EMULATED_PPC |
239 |
– |
extern "C" void *get_toc(void); |
270 |
|
extern "C" void *get_sp(void); |
271 |
< |
extern "C" void flush_icache_range(void *start, void *end); |
271 |
> |
extern "C" void *get_r2(void); |
272 |
> |
extern "C" void set_r2(void *); |
273 |
> |
extern "C" void *get_r13(void); |
274 |
> |
extern "C" void set_r13(void *); |
275 |
> |
extern "C" void flush_icache_range(uint32 start, uint32 end); |
276 |
|
extern "C" void jump_to_rom(uint32 entry, uint32 context); |
277 |
|
extern "C" void quit_emulator(void); |
278 |
|
extern "C" void execute_68k(uint32 pc, M68kRegisters *r); |
286 |
|
|
287 |
|
#if EMULATED_PPC |
288 |
|
/* |
289 |
+ |
* Return signal stack base |
290 |
+ |
*/ |
291 |
+ |
|
292 |
+ |
uintptr SignalStackBase(void) |
293 |
+ |
{ |
294 |
+ |
return sig_stack + SIG_STACK_SIZE; |
295 |
+ |
} |
296 |
+ |
|
297 |
+ |
|
298 |
+ |
/* |
299 |
|
* Atomic operations |
300 |
|
*/ |
301 |
|
|
336 |
|
|
337 |
|
|
338 |
|
/* |
339 |
+ |
* Memory management helpers |
340 |
+ |
*/ |
341 |
+ |
|
342 |
+ |
static inline uint8 *vm_mac_acquire(uint32 size) |
343 |
+ |
{ |
344 |
+ |
return (uint8 *)vm_acquire(size); |
345 |
+ |
} |
346 |
+ |
|
347 |
+ |
static inline int vm_mac_acquire_fixed(uint32 addr, uint32 size) |
348 |
+ |
{ |
349 |
+ |
return vm_acquire_fixed(Mac2HostAddr(addr), size); |
350 |
+ |
} |
351 |
+ |
|
352 |
+ |
static inline int vm_mac_release(uint32 addr, uint32 size) |
353 |
+ |
{ |
354 |
+ |
return vm_release(Mac2HostAddr(addr), size); |
355 |
+ |
} |
356 |
+ |
|
357 |
+ |
|
358 |
+ |
/* |
359 |
|
* Main program |
360 |
|
*/ |
361 |
|
|
368 |
|
exit(0); |
369 |
|
} |
370 |
|
|
371 |
< |
int main(int argc, char **argv) |
371 |
> |
static bool valid_vmdir(const char *path) |
372 |
|
{ |
373 |
< |
char str[256]; |
374 |
< |
uint32 *boot_globs; |
375 |
< |
int16 i16; |
376 |
< |
int rom_fd; |
373 |
> |
const int suffix_len = sizeof(".sheepvm") - 1; |
374 |
> |
int len = strlen(path); |
375 |
> |
if (len && path[len - 1] == '/') // to support both ".sheepvm" and ".sheepvm/" |
376 |
> |
len--; |
377 |
> |
if (len > suffix_len && !strncmp(path + len - suffix_len, ".sheepvm", suffix_len)) { |
378 |
> |
struct stat d; |
379 |
> |
if (!stat(path, &d) && S_ISDIR(d.st_mode)) { |
380 |
> |
return true; |
381 |
> |
} |
382 |
> |
} |
383 |
> |
return false; |
384 |
> |
} |
385 |
> |
|
386 |
> |
static void get_system_info(void) |
387 |
> |
{ |
388 |
> |
#if !EMULATED_PPC |
389 |
|
FILE *proc_file; |
390 |
< |
const char *rom_path; |
390 |
> |
#endif |
391 |
> |
|
392 |
> |
PVR = 0x00040000; // Default: 604 |
393 |
> |
CPUClockSpeed = 100000000; // Default: 100MHz |
394 |
> |
BusClockSpeed = 100000000; // Default: 100MHz |
395 |
> |
TimebaseSpeed = 25000000; // Default: 25MHz |
396 |
> |
|
397 |
> |
#if EMULATED_PPC |
398 |
> |
PVR = 0x000c0000; // Default: 7400 (with AltiVec) |
399 |
> |
#elif defined(__APPLE__) && defined(__MACH__) |
400 |
> |
proc_file = popen("ioreg -c IOPlatformDevice", "r"); |
401 |
> |
if (proc_file) { |
402 |
> |
char line[256]; |
403 |
> |
bool powerpc_node = false; |
404 |
> |
while (fgets(line, sizeof(line) - 1, proc_file)) { |
405 |
> |
// Read line |
406 |
> |
int len = strlen(line); |
407 |
> |
if (len == 0) |
408 |
> |
continue; |
409 |
> |
line[len - 1] = 0; |
410 |
> |
|
411 |
> |
// Parse line |
412 |
> |
if (strstr(line, "o PowerPC,")) |
413 |
> |
powerpc_node = true; |
414 |
> |
else if (powerpc_node) { |
415 |
> |
uint32 value; |
416 |
> |
char head[256]; |
417 |
> |
if (sscanf(line, "%[ |]\"cpu-version\" = <%x>", head, &value) == 2) |
418 |
> |
PVR = value; |
419 |
> |
else if (sscanf(line, "%[ |]\"clock-frequency\" = <%x>", head, &value) == 2) |
420 |
> |
CPUClockSpeed = value; |
421 |
> |
else if (sscanf(line, "%[ |]\"bus-frequency\" = <%x>", head, &value) == 2) |
422 |
> |
BusClockSpeed = value; |
423 |
> |
else if (sscanf(line, "%[ |]\"timebase-frequency\" = <%x>", head, &value) == 2) |
424 |
> |
TimebaseSpeed = value; |
425 |
> |
else if (strchr(line, '}')) |
426 |
> |
powerpc_node = false; |
427 |
> |
} |
428 |
> |
} |
429 |
> |
fclose(proc_file); |
430 |
> |
} else { |
431 |
> |
char str[256]; |
432 |
> |
sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno)); |
433 |
> |
WarningAlert(str); |
434 |
> |
} |
435 |
> |
#else |
436 |
> |
proc_file = fopen("/proc/cpuinfo", "r"); |
437 |
> |
if (proc_file) { |
438 |
> |
// CPU specs from Linux kernel |
439 |
> |
// TODO: make it more generic with features (e.g. AltiVec) and |
440 |
> |
// cache information and friends for NameRegistry |
441 |
> |
static const struct { |
442 |
> |
uint32 pvr_mask; |
443 |
> |
uint32 pvr_value; |
444 |
> |
const char *cpu_name; |
445 |
> |
} |
446 |
> |
cpu_specs[] = { |
447 |
> |
{ 0xffff0000, 0x00010000, "601" }, |
448 |
> |
{ 0xffff0000, 0x00030000, "603" }, |
449 |
> |
{ 0xffff0000, 0x00060000, "603e" }, |
450 |
> |
{ 0xffff0000, 0x00070000, "603ev" }, |
451 |
> |
{ 0xffff0000, 0x00040000, "604" }, |
452 |
> |
{ 0xfffff000, 0x00090000, "604e" }, |
453 |
> |
{ 0xffff0000, 0x00090000, "604r" }, |
454 |
> |
{ 0xffff0000, 0x000a0000, "604ev" }, |
455 |
> |
{ 0xffffffff, 0x00084202, "740/750" }, |
456 |
> |
{ 0xfffff000, 0x00083000, "745/755" }, |
457 |
> |
{ 0xfffffff0, 0x00080100, "750CX" }, |
458 |
> |
{ 0xfffffff0, 0x00082200, "750CX" }, |
459 |
> |
{ 0xfffffff0, 0x00082210, "750CXe" }, |
460 |
> |
{ 0xffffff00, 0x70000100, "750FX" }, |
461 |
> |
{ 0xffffffff, 0x70000200, "750FX" }, |
462 |
> |
{ 0xffff0000, 0x70000000, "750FX" }, |
463 |
> |
{ 0xffff0000, 0x70020000, "750GX" }, |
464 |
> |
{ 0xffff0000, 0x00080000, "740/750" }, |
465 |
> |
{ 0xffffffff, 0x000c1101, "7400 (1.1)" }, |
466 |
> |
{ 0xffff0000, 0x000c0000, "7400" }, |
467 |
> |
{ 0xffff0000, 0x800c0000, "7410" }, |
468 |
> |
{ 0xffffffff, 0x80000200, "7450" }, |
469 |
> |
{ 0xffffffff, 0x80000201, "7450" }, |
470 |
> |
{ 0xffff0000, 0x80000000, "7450" }, |
471 |
> |
{ 0xffffff00, 0x80010100, "7455" }, |
472 |
> |
{ 0xffffffff, 0x80010200, "7455" }, |
473 |
> |
{ 0xffff0000, 0x80010000, "7455" }, |
474 |
> |
{ 0xffff0000, 0x80020000, "7457" }, |
475 |
> |
{ 0xffff0000, 0x80030000, "7447A" }, |
476 |
> |
{ 0xffff0000, 0x80040000, "7448" }, |
477 |
> |
{ 0x7fff0000, 0x00810000, "82xx" }, |
478 |
> |
{ 0x7fff0000, 0x00820000, "8280" }, |
479 |
> |
{ 0xffff0000, 0x00400000, "Power3 (630)" }, |
480 |
> |
{ 0xffff0000, 0x00410000, "Power3 (630+)" }, |
481 |
> |
{ 0xffff0000, 0x00360000, "I-star" }, |
482 |
> |
{ 0xffff0000, 0x00370000, "S-star" }, |
483 |
> |
{ 0xffff0000, 0x00350000, "Power4" }, |
484 |
> |
{ 0xffff0000, 0x00390000, "PPC970" }, |
485 |
> |
{ 0xffff0000, 0x003c0000, "PPC970FX" }, |
486 |
> |
{ 0xffff0000, 0x00440000, "PPC970MP" }, |
487 |
> |
{ 0xffff0000, 0x003a0000, "POWER5 (gr)" }, |
488 |
> |
{ 0xffff0000, 0x003b0000, "POWER5+ (gs)" }, |
489 |
> |
{ 0xffff0000, 0x003e0000, "POWER6" }, |
490 |
> |
{ 0xffff0000, 0x00700000, "Cell Broadband Engine" }, |
491 |
> |
{ 0x7fff0000, 0x00900000, "PA6T" }, |
492 |
> |
{ 0, 0, 0 } |
493 |
> |
}; |
494 |
> |
|
495 |
> |
char line[256]; |
496 |
> |
while(fgets(line, 255, proc_file)) { |
497 |
> |
// Read line |
498 |
> |
int len = strlen(line); |
499 |
> |
if (len == 0) |
500 |
> |
continue; |
501 |
> |
line[len-1] = 0; |
502 |
> |
|
503 |
> |
// Parse line |
504 |
> |
int i; |
505 |
> |
float f; |
506 |
> |
char value[256]; |
507 |
> |
if (sscanf(line, "cpu : %[^,]", value) == 1) { |
508 |
> |
// Search by name |
509 |
> |
const char *cpu_name = NULL; |
510 |
> |
for (int i = 0; cpu_specs[i].pvr_mask != 0; i++) { |
511 |
> |
if (strcmp(cpu_specs[i].cpu_name, value) == 0) { |
512 |
> |
cpu_name = cpu_specs[i].cpu_name; |
513 |
> |
PVR = cpu_specs[i].pvr_value; |
514 |
> |
break; |
515 |
> |
} |
516 |
> |
} |
517 |
> |
if (cpu_name == NULL) |
518 |
> |
printf("WARNING: Unknown CPU type '%s', assuming 604\n", value); |
519 |
> |
else |
520 |
> |
printf("Found a PowerPC %s processor\n", cpu_name); |
521 |
> |
} |
522 |
> |
if (sscanf(line, "clock : %fMHz", &f) == 1) |
523 |
> |
CPUClockSpeed = BusClockSpeed = ((int64)f) * 1000000; |
524 |
> |
else if (sscanf(line, "clock : %dMHz", &i) == 1) |
525 |
> |
CPUClockSpeed = BusClockSpeed = i * 1000000; |
526 |
> |
} |
527 |
> |
fclose(proc_file); |
528 |
> |
} else { |
529 |
> |
char str[256]; |
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 |
> |
#ifdef USE_SDL |
672 |
> |
static bool init_sdl() |
673 |
> |
{ |
674 |
> |
int sdl_flags = 0; |
675 |
> |
#ifdef USE_SDL_VIDEO |
676 |
> |
sdl_flags |= SDL_INIT_VIDEO; |
677 |
> |
#endif |
678 |
> |
#ifdef USE_SDL_AUDIO |
679 |
> |
sdl_flags |= SDL_INIT_AUDIO; |
680 |
> |
#endif |
681 |
> |
assert(sdl_flags != 0); |
682 |
> |
|
683 |
> |
#ifdef USE_SDL_VIDEO |
684 |
> |
// Don't let SDL block the screensaver |
685 |
> |
setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", TRUE); |
686 |
> |
|
687 |
> |
// Make SDL pass through command-clicks and option-clicks unaltered |
688 |
> |
setenv("SDL_HAS3BUTTONMOUSE", "1", TRUE); |
689 |
> |
#endif |
690 |
> |
|
691 |
> |
if (SDL_Init(sdl_flags) == -1) { |
692 |
> |
char str[256]; |
693 |
> |
sprintf(str, "Could not initialize SDL: %s.\n", SDL_GetError()); |
694 |
> |
ErrorAlert(str); |
695 |
> |
return false; |
696 |
> |
} |
697 |
> |
atexit(SDL_Quit); |
698 |
> |
|
699 |
> |
// Don't let SDL catch SIGINT and SIGTERM signals |
700 |
> |
signal(SIGINT, SIG_DFL); |
701 |
> |
signal(SIGTERM, SIG_DFL); |
702 |
> |
return true; |
703 |
> |
} |
704 |
> |
#endif |
705 |
> |
|
706 |
> |
int main(int argc, char **argv) |
707 |
> |
{ |
708 |
> |
char str[256]; |
709 |
> |
bool memory_mapped_from_zero, ram_rom_areas_contiguous; |
710 |
> |
const char *vmdir = NULL; |
711 |
|
|
712 |
|
// Initialize variables |
713 |
|
RAMBase = 0; |
718 |
|
printf(" %s\n", GetString(STR_ABOUT_TEXT2)); |
719 |
|
|
720 |
|
#if !EMULATED_PPC |
721 |
+ |
#ifdef SYSTEM_CLOBBERS_R2 |
722 |
|
// Get TOC pointer |
723 |
< |
TOC = get_toc(); |
723 |
> |
TOC = get_r2(); |
724 |
> |
#endif |
725 |
> |
#ifdef SYSTEM_CLOBBERS_R13 |
726 |
> |
// Get r13 register |
727 |
> |
R13 = get_r13(); |
728 |
|
#endif |
331 |
– |
|
332 |
– |
#ifdef ENABLE_GTK |
333 |
– |
// Init GTK |
334 |
– |
gtk_set_locale(); |
335 |
– |
gtk_init(&argc, &argv); |
729 |
|
#endif |
337 |
– |
|
338 |
– |
// Read preferences |
339 |
– |
PrefsInit(argc, argv); |
730 |
|
|
731 |
|
// Parse command line arguments |
732 |
|
for (int i=1; i<argc; i++) { |
733 |
|
if (strcmp(argv[i], "--help") == 0) { |
734 |
|
usage(argv[0]); |
735 |
+ |
#ifndef USE_SDL_VIDEO |
736 |
|
} else if (strcmp(argv[i], "--display") == 0) { |
737 |
|
i++; |
738 |
|
if (i < argc) |
739 |
|
x_display_name = strdup(argv[i]); |
740 |
< |
} else if (argv[i][0] == '-') { |
740 |
> |
#endif |
741 |
> |
} else if (strcmp(argv[i], "--gui-connection") == 0) { |
742 |
> |
argv[i++] = NULL; |
743 |
> |
if (i < argc) { |
744 |
> |
gui_connection_path = argv[i]; |
745 |
> |
argv[i] = NULL; |
746 |
> |
} |
747 |
> |
} else if (valid_vmdir(argv[i])) { |
748 |
> |
vmdir = argv[i]; |
749 |
> |
argv[i] = NULL; |
750 |
> |
printf("Using %s as vmdir.\n", vmdir); |
751 |
> |
if (chdir(vmdir)) { |
752 |
> |
printf("Failed to chdir to %s. Good bye.", vmdir); |
753 |
> |
exit(1); |
754 |
> |
} |
755 |
> |
break; |
756 |
> |
} |
757 |
> |
} |
758 |
> |
|
759 |
> |
// Remove processed arguments |
760 |
> |
for (int i=1; i<argc; i++) { |
761 |
> |
int k; |
762 |
> |
for (k=i; k<argc; k++) |
763 |
> |
if (argv[k] != NULL) |
764 |
> |
break; |
765 |
> |
if (k > i) { |
766 |
> |
k -= i; |
767 |
> |
for (int j=i+k; j<argc; j++) |
768 |
> |
argv[j-k] = argv[j]; |
769 |
> |
argc -= k; |
770 |
> |
} |
771 |
> |
} |
772 |
> |
|
773 |
> |
// Connect to the external GUI |
774 |
> |
if (gui_connection_path) { |
775 |
> |
if ((gui_connection = rpc_init_client(gui_connection_path)) == NULL) { |
776 |
> |
fprintf(stderr, "Failed to initialize RPC client connection to the GUI\n"); |
777 |
> |
return 1; |
778 |
> |
} |
779 |
> |
} |
780 |
> |
|
781 |
> |
#ifdef ENABLE_GTK |
782 |
> |
if (!gui_connection) { |
783 |
> |
// Init GTK |
784 |
> |
gtk_set_locale(); |
785 |
> |
gtk_init(&argc, &argv); |
786 |
> |
} |
787 |
> |
#endif |
788 |
> |
|
789 |
> |
// Read preferences |
790 |
> |
PrefsInit(vmdir, argc, argv); |
791 |
> |
|
792 |
> |
// Any command line arguments left? |
793 |
> |
for (int i=1; i<argc; i++) { |
794 |
> |
if (argv[i][0] == '-') { |
795 |
|
fprintf(stderr, "Unrecognized option '%s'\n", argv[i]); |
796 |
|
usage(argv[0]); |
797 |
|
} |
798 |
|
} |
799 |
|
|
800 |
+ |
#ifdef USE_SDL |
801 |
+ |
// Initialize SDL system |
802 |
+ |
if (!init_sdl()) |
803 |
+ |
goto quit; |
804 |
+ |
#endif |
805 |
+ |
|
806 |
+ |
#ifndef USE_SDL_VIDEO |
807 |
|
// Open display |
808 |
|
x_display = XOpenDisplay(x_display_name); |
809 |
|
if (x_display == NULL) { |
817 |
|
// Fork out, so we can return from fullscreen mode when things get ugly |
818 |
|
XF86DGAForkApp(DefaultScreen(x_display)); |
819 |
|
#endif |
820 |
+ |
#endif |
821 |
|
|
822 |
|
#ifdef ENABLE_MON |
823 |
|
// Initialize mon |
824 |
|
mon_init(); |
825 |
|
#endif |
826 |
|
|
827 |
< |
// Get system info |
828 |
< |
PVR = 0x00040000; // Default: 604 |
829 |
< |
CPUClockSpeed = 100000000; // Default: 100MHz |
377 |
< |
BusClockSpeed = 100000000; // Default: 100MHz |
378 |
< |
#if !EMULATED_PPC |
379 |
< |
proc_file = fopen("/proc/cpuinfo", "r"); |
380 |
< |
if (proc_file) { |
381 |
< |
char line[256]; |
382 |
< |
while(fgets(line, 255, proc_file)) { |
383 |
< |
// Read line |
384 |
< |
int len = strlen(line); |
385 |
< |
if (len == 0) |
386 |
< |
continue; |
387 |
< |
line[len-1] = 0; |
827 |
> |
// Install signal handlers |
828 |
> |
if (!install_signal_handlers()) |
829 |
> |
goto quit; |
830 |
|
|
831 |
< |
// Parse line |
832 |
< |
int i; |
833 |
< |
char value[256]; |
834 |
< |
if (sscanf(line, "cpu : %s", value) == 1) { |
835 |
< |
if (strcmp(value, "601") == 0) |
394 |
< |
PVR = 0x00010000; |
395 |
< |
else if (strcmp(value, "603") == 0) |
396 |
< |
PVR = 0x00030000; |
397 |
< |
else if (strcmp(value, "604") == 0) |
398 |
< |
PVR = 0x00040000; |
399 |
< |
else if (strcmp(value, "603e") == 0) |
400 |
< |
PVR = 0x00060000; |
401 |
< |
else if (strcmp(value, "603ev") == 0) |
402 |
< |
PVR = 0x00070000; |
403 |
< |
else if (strcmp(value, "604e") == 0) |
404 |
< |
PVR = 0x00090000; |
405 |
< |
else if (strcmp(value, "604ev5") == 0) |
406 |
< |
PVR = 0x000a0000; |
407 |
< |
else if (strcmp(value, "750") == 0) |
408 |
< |
PVR = 0x00080000; |
409 |
< |
else if (strcmp(value, "821") == 0) |
410 |
< |
PVR = 0x00320000; |
411 |
< |
else if (strcmp(value, "860") == 0) |
412 |
< |
PVR = 0x00500000; |
413 |
< |
else |
414 |
< |
printf("WARNING: Unknown CPU type '%s', assuming 604\n", value); |
415 |
< |
} |
416 |
< |
if (sscanf(line, "clock : %dMHz", &i) == 1) |
417 |
< |
CPUClockSpeed = BusClockSpeed = i * 1000000; |
418 |
< |
} |
419 |
< |
fclose(proc_file); |
420 |
< |
} else { |
421 |
< |
sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno)); |
422 |
< |
WarningAlert(str); |
423 |
< |
} |
424 |
< |
#endif |
425 |
< |
D(bug("PVR: %08x (assumed)\n", PVR)); |
831 |
> |
// Initialize VM system |
832 |
> |
vm_init(); |
833 |
> |
|
834 |
> |
// Get system info |
835 |
> |
get_system_info(); |
836 |
|
|
837 |
|
// Init system routines |
838 |
|
SysInit(); |
855 |
|
goto quit; |
856 |
|
} |
857 |
|
|
448 |
– |
// Create Low Memory area (0x0000..0x3000) |
449 |
– |
if (vm_acquire_fixed((char *)0, 0x3000) < 0) { |
450 |
– |
sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno)); |
451 |
– |
ErrorAlert(str); |
452 |
– |
goto quit; |
453 |
– |
} |
454 |
– |
lm_area_mapped = true; |
455 |
– |
|
858 |
|
// Create areas for Kernel Data |
859 |
< |
kernel_area = shmget(IPC_PRIVATE, KERNEL_AREA_SIZE, 0600); |
860 |
< |
if (kernel_area == -1) { |
861 |
< |
sprintf(str, GetString(STR_KD_SHMGET_ERR), strerror(errno)); |
859 |
> |
if (!kernel_data_init()) |
860 |
> |
goto quit; |
861 |
> |
kernel_data = (KernelData *)Mac2HostAddr(KERNEL_DATA_BASE); |
862 |
> |
emulator_data = &kernel_data->ed; |
863 |
> |
KernelDataAddr = KERNEL_DATA_BASE; |
864 |
> |
D(bug("Kernel Data at %p (%08x)\n", kernel_data, KERNEL_DATA_BASE)); |
865 |
> |
D(bug("Emulator Data at %p (%08x)\n", emulator_data, KERNEL_DATA_BASE + offsetof(KernelData, ed))); |
866 |
> |
|
867 |
> |
// Create area for DR Cache |
868 |
> |
if (vm_mac_acquire_fixed(DR_EMULATOR_BASE, DR_EMULATOR_SIZE) < 0) { |
869 |
> |
sprintf(str, GetString(STR_DR_EMULATOR_MMAP_ERR), strerror(errno)); |
870 |
|
ErrorAlert(str); |
871 |
|
goto quit; |
872 |
|
} |
873 |
< |
if (shmat(kernel_area, (void *)KERNEL_DATA_BASE, 0) < 0) { |
874 |
< |
sprintf(str, GetString(STR_KD_SHMAT_ERR), strerror(errno)); |
873 |
> |
dr_emulator_area_mapped = true; |
874 |
> |
if (vm_mac_acquire_fixed(DR_CACHE_BASE, DR_CACHE_SIZE) < 0) { |
875 |
> |
sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno)); |
876 |
|
ErrorAlert(str); |
877 |
|
goto quit; |
878 |
|
} |
879 |
< |
if (shmat(kernel_area, (void *)KERNEL_DATA2_BASE, 0) < 0) { |
880 |
< |
sprintf(str, GetString(STR_KD2_SHMAT_ERR), strerror(errno)); |
879 |
> |
dr_cache_area_mapped = true; |
880 |
> |
#if !EMULATED_PPC |
881 |
> |
if (vm_protect((char *)DR_CACHE_BASE, DR_CACHE_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
882 |
> |
sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno)); |
883 |
|
ErrorAlert(str); |
884 |
|
goto quit; |
885 |
|
} |
886 |
< |
kernel_data = (KernelData *)0x68ffe000; |
887 |
< |
emulator_data = &kernel_data->ed; |
888 |
< |
KernelDataAddr = (uint32)kernel_data; |
476 |
< |
D(bug("Kernel Data at %p, Emulator Data at %p\n", kernel_data, emulator_data)); |
886 |
> |
#endif |
887 |
> |
DRCacheAddr = DR_CACHE_BASE; |
888 |
> |
D(bug("DR Cache at %p\n", DRCacheAddr)); |
889 |
|
|
890 |
|
// Create area for SheepShaver data |
891 |
< |
if (vm_acquire_fixed((char *)SHEEP_BASE, SHEEP_SIZE) < 0) { |
891 |
> |
if (!SheepMem::Init()) { |
892 |
|
sprintf(str, GetString(STR_SHEEP_MEM_MMAP_ERR), strerror(errno)); |
893 |
|
ErrorAlert(str); |
894 |
|
goto quit; |
895 |
|
} |
896 |
< |
SheepStack1Base = SHEEP_BASE + 0x10000; |
485 |
< |
SheepStack2Base = SheepStack1Base + 0x10000; |
486 |
< |
SheepThunksBase = SheepStack2Base + 0x1000; |
487 |
< |
sheep_area_mapped = true; |
488 |
< |
|
489 |
< |
// Create area for Mac ROM |
490 |
< |
if (vm_acquire_fixed((char *)ROM_BASE, ROM_AREA_SIZE) < 0) { |
491 |
< |
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
492 |
< |
ErrorAlert(str); |
493 |
< |
goto quit; |
494 |
< |
} |
495 |
< |
#if !EMULATED_PPC || defined(__powerpc__) |
496 |
< |
if (vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
497 |
< |
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
498 |
< |
ErrorAlert(str); |
499 |
< |
goto quit; |
500 |
< |
} |
501 |
< |
#endif |
502 |
< |
rom_area_mapped = true; |
503 |
< |
D(bug("ROM area at %08x\n", ROM_BASE)); |
504 |
< |
|
896 |
> |
|
897 |
|
// Create area for Mac RAM |
898 |
|
RAMSize = PrefsFindInt32("ramsize"); |
899 |
|
if (RAMSize < 8*1024*1024) { |
900 |
|
WarningAlert(GetString(STR_SMALL_RAM_WARN)); |
901 |
|
RAMSize = 8*1024*1024; |
902 |
|
} |
903 |
< |
|
904 |
< |
if (vm_acquire_fixed((char *)RAM_BASE, RAMSize) < 0) { |
905 |
< |
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
906 |
< |
ErrorAlert(str); |
907 |
< |
goto quit; |
903 |
> |
memory_mapped_from_zero = false; |
904 |
> |
ram_rom_areas_contiguous = false; |
905 |
> |
#if REAL_ADDRESSING && HAVE_LINKER_SCRIPT |
906 |
> |
if (vm_mac_acquire_fixed(0, RAMSize) == 0) { |
907 |
> |
D(bug("Could allocate RAM from 0x0000\n")); |
908 |
> |
RAMBase = 0; |
909 |
> |
RAMBaseHost = Mac2HostAddr(RAMBase); |
910 |
> |
memory_mapped_from_zero = true; |
911 |
> |
} |
912 |
> |
#endif |
913 |
> |
if (!memory_mapped_from_zero) { |
914 |
> |
#ifndef PAGEZERO_HACK |
915 |
> |
// Create Low Memory area (0x0000..0x3000) |
916 |
> |
if (vm_mac_acquire_fixed(0, 0x3000) < 0) { |
917 |
> |
sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno)); |
918 |
> |
ErrorAlert(str); |
919 |
> |
goto quit; |
920 |
> |
} |
921 |
> |
lm_area_mapped = true; |
922 |
> |
#endif |
923 |
> |
#if REAL_ADDRESSING |
924 |
> |
// Allocate RAM at any address. Since ROM must be higher than RAM, allocate the RAM |
925 |
> |
// and ROM areas contiguously, plus a little extra to allow for ROM address alignment. |
926 |
> |
RAMBaseHost = vm_mac_acquire(RAMSize + ROM_AREA_SIZE + ROM_ALIGNMENT); |
927 |
> |
if (RAMBaseHost == VM_MAP_FAILED) { |
928 |
> |
sprintf(str, GetString(STR_RAM_ROM_MMAP_ERR), strerror(errno)); |
929 |
> |
ErrorAlert(str); |
930 |
> |
goto quit; |
931 |
> |
} |
932 |
> |
RAMBase = Host2MacAddr(RAMBaseHost); |
933 |
> |
ROMBase = (RAMBase + RAMSize + ROM_ALIGNMENT -1) & -ROM_ALIGNMENT; |
934 |
> |
ROMBaseHost = Mac2HostAddr(ROMBase); |
935 |
> |
ram_rom_areas_contiguous = true; |
936 |
> |
#else |
937 |
> |
if (vm_mac_acquire_fixed(RAM_BASE, RAMSize) < 0) { |
938 |
> |
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
939 |
> |
ErrorAlert(str); |
940 |
> |
goto quit; |
941 |
> |
} |
942 |
> |
RAMBase = RAM_BASE; |
943 |
> |
RAMBaseHost = Mac2HostAddr(RAMBase); |
944 |
> |
#endif |
945 |
|
} |
946 |
|
#if !EMULATED_PPC |
947 |
< |
if (vm_protect((char *)RAM_BASE, RAMSize, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
947 |
> |
if (vm_protect(RAMBaseHost, RAMSize, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
948 |
|
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
949 |
|
ErrorAlert(str); |
950 |
|
goto quit; |
951 |
|
} |
952 |
|
#endif |
524 |
– |
RAMBase = RAM_BASE; |
953 |
|
ram_area_mapped = true; |
954 |
< |
D(bug("RAM area at %08x\n", RAMBase)); |
954 |
> |
D(bug("RAM area at %p (%08x)\n", RAMBaseHost, RAMBase)); |
955 |
|
|
956 |
< |
if (RAMBase > ROM_BASE) { |
957 |
< |
ErrorAlert(GetString(STR_RAM_HIGHER_THAN_ROM_ERR)); |
956 |
> |
if (RAMBase > KernelDataAddr) { |
957 |
> |
ErrorAlert(GetString(STR_RAM_AREA_TOO_HIGH_ERR)); |
958 |
|
goto quit; |
959 |
|
} |
532 |
– |
|
533 |
– |
// Load Mac ROM |
534 |
– |
rom_path = PrefsFindString("rom"); |
535 |
– |
rom_fd = open(rom_path ? rom_path : ROM_FILE_NAME, O_RDONLY); |
536 |
– |
if (rom_fd < 0) { |
537 |
– |
rom_fd = open(rom_path ? rom_path : ROM_FILE_NAME2, O_RDONLY); |
538 |
– |
if (rom_fd < 0) { |
539 |
– |
ErrorAlert(GetString(STR_NO_ROM_FILE_ERR)); |
540 |
– |
goto quit; |
541 |
– |
} |
542 |
– |
} |
543 |
– |
printf(GetString(STR_READING_ROM_FILE)); |
544 |
– |
rom_size = lseek(rom_fd, 0, SEEK_END); |
545 |
– |
lseek(rom_fd, 0, SEEK_SET); |
546 |
– |
rom_tmp = new uint8[ROM_SIZE]; |
547 |
– |
actual = read(rom_fd, (void *)rom_tmp, ROM_SIZE); |
548 |
– |
close(rom_fd); |
960 |
|
|
961 |
< |
// Decode Mac ROM |
962 |
< |
if (!DecodeROM(rom_tmp, actual)) { |
963 |
< |
if (rom_size != 4*1024*1024) { |
964 |
< |
ErrorAlert(GetString(STR_ROM_SIZE_ERR)); |
965 |
< |
goto quit; |
555 |
< |
} else { |
556 |
< |
ErrorAlert(GetString(STR_ROM_FILE_READ_ERR)); |
961 |
> |
// Create area for Mac ROM |
962 |
> |
if (!ram_rom_areas_contiguous) { |
963 |
> |
if (vm_mac_acquire_fixed(ROM_BASE, ROM_AREA_SIZE) < 0) { |
964 |
> |
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
965 |
> |
ErrorAlert(str); |
966 |
|
goto quit; |
967 |
|
} |
968 |
+ |
ROMBase = ROM_BASE; |
969 |
+ |
ROMBaseHost = Mac2HostAddr(ROMBase); |
970 |
|
} |
971 |
< |
delete[] rom_tmp; |
972 |
< |
|
973 |
< |
// Load NVRAM |
974 |
< |
XPRAMInit(); |
975 |
< |
|
976 |
< |
// Set boot volume |
977 |
< |
i16 = PrefsFindInt32("bootdrive"); |
978 |
< |
XPRAM[0x1378] = i16 >> 8; |
979 |
< |
XPRAM[0x1379] = i16 & 0xff; |
569 |
< |
i16 = PrefsFindInt32("bootdriver"); |
570 |
< |
XPRAM[0x137a] = i16 >> 8; |
571 |
< |
XPRAM[0x137b] = i16 & 0xff; |
572 |
< |
|
573 |
< |
// Create BootGlobs at top of Mac memory |
574 |
< |
memset((void *)(RAMBase + RAMSize - 4096), 0, 4096); |
575 |
< |
BootGlobsAddr = RAMBase + RAMSize - 0x1c; |
576 |
< |
boot_globs = (uint32 *)BootGlobsAddr; |
577 |
< |
boot_globs[-5] = htonl(RAMBase + RAMSize); // MemTop |
578 |
< |
boot_globs[0] = htonl(RAMBase); // First RAM bank |
579 |
< |
boot_globs[1] = htonl(RAMSize); |
580 |
< |
boot_globs[2] = htonl((uint32)-1); // End of bank table |
581 |
< |
|
582 |
< |
// Init drivers |
583 |
< |
SonyInit(); |
584 |
< |
DiskInit(); |
585 |
< |
CDROMInit(); |
586 |
< |
SCSIInit(); |
587 |
< |
|
588 |
< |
// Init external file system |
589 |
< |
ExtFSInit(); |
590 |
< |
|
591 |
< |
// Init audio |
592 |
< |
AudioInit(); |
593 |
< |
|
594 |
< |
// Init network |
595 |
< |
EtherInit(); |
596 |
< |
|
597 |
< |
// Init serial ports |
598 |
< |
SerialInit(); |
599 |
< |
|
600 |
< |
// Init Time Manager |
601 |
< |
TimerInit(); |
971 |
> |
#if !EMULATED_PPC |
972 |
> |
if (vm_protect(ROMBaseHost, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
973 |
> |
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
974 |
> |
ErrorAlert(str); |
975 |
> |
goto quit; |
976 |
> |
} |
977 |
> |
#endif |
978 |
> |
rom_area_mapped = true; |
979 |
> |
D(bug("ROM area at %p (%08x)\n", ROMBaseHost, ROMBase)); |
980 |
|
|
981 |
< |
// Init clipboard |
982 |
< |
ClipInit(); |
981 |
> |
if (RAMBase > ROMBase) { |
982 |
> |
ErrorAlert(GetString(STR_RAM_HIGHER_THAN_ROM_ERR)); |
983 |
> |
goto quit; |
984 |
> |
} |
985 |
|
|
986 |
< |
// Init video |
987 |
< |
if (!VideoInit()) |
986 |
> |
// Load Mac ROM |
987 |
> |
if (!load_mac_rom()) |
988 |
|
goto quit; |
989 |
|
|
990 |
< |
// Install ROM patches |
991 |
< |
if (!PatchROM()) { |
612 |
< |
ErrorAlert(GetString(STR_UNSUPPORTED_ROM_TYPE_ERR)); |
990 |
> |
// Initialize everything |
991 |
> |
if (!InitAll(vmdir)) |
992 |
|
goto quit; |
993 |
< |
} |
993 |
> |
D(bug("Initialization complete\n")); |
994 |
|
|
995 |
|
// Clear caches (as we loaded and patched code) and write protect ROM |
996 |
|
#if !EMULATED_PPC |
997 |
< |
MakeExecutable(0, (void *)ROM_BASE, ROM_AREA_SIZE); |
619 |
< |
#endif |
620 |
< |
vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_EXECUTE); |
621 |
< |
|
622 |
< |
// Initialize Kernel Data |
623 |
< |
memset(kernel_data, 0, sizeof(KernelData)); |
624 |
< |
if (ROMType == ROMTYPE_NEWWORLD) { |
625 |
< |
static uint32 of_dev_tree[4] = {0, 0, 0, 0}; |
626 |
< |
static uint8 vector_lookup_tbl[128]; |
627 |
< |
static uint8 vector_mask_tbl[64]; |
628 |
< |
memset((uint8 *)kernel_data + 0xb80, 0x3d, 0x80); |
629 |
< |
memset(vector_lookup_tbl, 0, 128); |
630 |
< |
memset(vector_mask_tbl, 0, 64); |
631 |
< |
kernel_data->v[0xb80 >> 2] = htonl(ROM_BASE); |
632 |
< |
kernel_data->v[0xb84 >> 2] = htonl((uint32)of_dev_tree); // OF device tree base |
633 |
< |
kernel_data->v[0xb90 >> 2] = htonl((uint32)vector_lookup_tbl); |
634 |
< |
kernel_data->v[0xb94 >> 2] = htonl((uint32)vector_mask_tbl); |
635 |
< |
kernel_data->v[0xb98 >> 2] = htonl(ROM_BASE); // OpenPIC base |
636 |
< |
kernel_data->v[0xbb0 >> 2] = htonl(0); // ADB base |
637 |
< |
kernel_data->v[0xc20 >> 2] = htonl(RAMSize); |
638 |
< |
kernel_data->v[0xc24 >> 2] = htonl(RAMSize); |
639 |
< |
kernel_data->v[0xc30 >> 2] = htonl(RAMSize); |
640 |
< |
kernel_data->v[0xc34 >> 2] = htonl(RAMSize); |
641 |
< |
kernel_data->v[0xc38 >> 2] = htonl(0x00010020); |
642 |
< |
kernel_data->v[0xc3c >> 2] = htonl(0x00200001); |
643 |
< |
kernel_data->v[0xc40 >> 2] = htonl(0x00010000); |
644 |
< |
kernel_data->v[0xc50 >> 2] = htonl(RAMBase); |
645 |
< |
kernel_data->v[0xc54 >> 2] = htonl(RAMSize); |
646 |
< |
kernel_data->v[0xf60 >> 2] = htonl(PVR); |
647 |
< |
kernel_data->v[0xf64 >> 2] = htonl(CPUClockSpeed); |
648 |
< |
kernel_data->v[0xf68 >> 2] = htonl(BusClockSpeed); |
649 |
< |
kernel_data->v[0xf6c >> 2] = htonl(CPUClockSpeed); |
650 |
< |
} else { |
651 |
< |
kernel_data->v[0xc80 >> 2] = htonl(RAMSize); |
652 |
< |
kernel_data->v[0xc84 >> 2] = htonl(RAMSize); |
653 |
< |
kernel_data->v[0xc90 >> 2] = htonl(RAMSize); |
654 |
< |
kernel_data->v[0xc94 >> 2] = htonl(RAMSize); |
655 |
< |
kernel_data->v[0xc98 >> 2] = htonl(0x00010020); |
656 |
< |
kernel_data->v[0xc9c >> 2] = htonl(0x00200001); |
657 |
< |
kernel_data->v[0xca0 >> 2] = htonl(0x00010000); |
658 |
< |
kernel_data->v[0xcb0 >> 2] = htonl(RAMBase); |
659 |
< |
kernel_data->v[0xcb4 >> 2] = htonl(RAMSize); |
660 |
< |
kernel_data->v[0xf80 >> 2] = htonl(PVR); |
661 |
< |
kernel_data->v[0xf84 >> 2] = htonl(CPUClockSpeed); |
662 |
< |
kernel_data->v[0xf88 >> 2] = htonl(BusClockSpeed); |
663 |
< |
kernel_data->v[0xf8c >> 2] = htonl(CPUClockSpeed); |
664 |
< |
} |
665 |
< |
|
666 |
< |
// Initialize extra low memory |
667 |
< |
D(bug("Initializing Low Memory...\n")); |
668 |
< |
memset(NULL, 0, 0x3000); |
669 |
< |
WriteMacInt32(XLM_SIGNATURE, FOURCC('B','a','a','h')); // Signature to detect SheepShaver |
670 |
< |
WriteMacInt32(XLM_KERNEL_DATA, (uint32)kernel_data); // For trap replacement routines |
671 |
< |
WriteMacInt32(XLM_PVR, PVR); // Theoretical PVR |
672 |
< |
WriteMacInt32(XLM_BUS_CLOCK, BusClockSpeed); // For DriverServicesLib patch |
673 |
< |
WriteMacInt16(XLM_EXEC_RETURN_OPCODE, M68K_EXEC_RETURN); // For Execute68k() (RTS from the executed 68k code will jump here and end 68k mode) |
674 |
< |
#if EMULATED_PPC |
675 |
< |
WriteMacInt32(XLM_ETHER_INIT, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_INIT)); |
676 |
< |
WriteMacInt32(XLM_ETHER_TERM, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_TERM)); |
677 |
< |
WriteMacInt32(XLM_ETHER_OPEN, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_OPEN)); |
678 |
< |
WriteMacInt32(XLM_ETHER_CLOSE, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_CLOSE)); |
679 |
< |
WriteMacInt32(XLM_ETHER_WPUT, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_WPUT)); |
680 |
< |
WriteMacInt32(XLM_ETHER_RSRV, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_RSRV)); |
681 |
< |
WriteMacInt32(XLM_VIDEO_DOIO, POWERPC_NATIVE_OP_FUNC(NATIVE_VIDEO_DO_DRIVER_IO)); |
682 |
< |
#else |
683 |
< |
WriteMacInt32(XLM_TOC, (uint32)TOC); // TOC pointer of emulator |
684 |
< |
WriteMacInt32(XLM_ETHER_INIT, (uint32)InitStreamModule); // DLPI ethernet driver functions |
685 |
< |
WriteMacInt32(XLM_ETHER_TERM, (uint32)TerminateStreamModule); |
686 |
< |
WriteMacInt32(XLM_ETHER_OPEN, (uint32)ether_open); |
687 |
< |
WriteMacInt32(XLM_ETHER_CLOSE, (uint32)ether_close); |
688 |
< |
WriteMacInt32(XLM_ETHER_WPUT, (uint32)ether_wput); |
689 |
< |
WriteMacInt32(XLM_ETHER_RSRV, (uint32)ether_rsrv); |
690 |
< |
WriteMacInt32(XLM_VIDEO_DOIO, (uint32)VideoDoDriverIO); |
997 |
> |
flush_icache_range(ROMBase, ROMBase + ROM_AREA_SIZE); |
998 |
|
#endif |
999 |
< |
D(bug("Low Memory initialized\n")); |
999 |
> |
vm_protect(ROMBaseHost, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_EXECUTE); |
1000 |
|
|
1001 |
|
// Start 60Hz thread |
1002 |
+ |
tick_thread_cancel = false; |
1003 |
|
tick_thread_active = (pthread_create(&tick_thread, NULL, tick_func, NULL) == 0); |
1004 |
|
D(bug("Tick thread installed (%ld)\n", tick_thread)); |
1005 |
|
|
1006 |
|
// Start NVRAM watchdog thread |
1007 |
|
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
1008 |
+ |
nvram_thread_cancel = false; |
1009 |
|
nvram_thread_active = (pthread_create(&nvram_thread, NULL, nvram_func, NULL) == 0); |
1010 |
|
D(bug("NVRAM thread installed (%ld)\n", nvram_thread)); |
1011 |
|
|
1012 |
|
#if !EMULATED_PPC |
704 |
– |
// Create and install stacks for signal handlers |
705 |
– |
sig_stack = malloc(SIG_STACK_SIZE); |
706 |
– |
D(bug("Signal stack at %p\n", sig_stack)); |
707 |
– |
if (sig_stack == NULL) { |
708 |
– |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
709 |
– |
goto quit; |
710 |
– |
} |
711 |
– |
extra_stack = malloc(SIG_STACK_SIZE); |
712 |
– |
D(bug("Extra stack at %p\n", extra_stack)); |
713 |
– |
if (extra_stack == NULL) { |
714 |
– |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
715 |
– |
goto quit; |
716 |
– |
} |
717 |
– |
struct sigaltstack new_stack; |
718 |
– |
new_stack.ss_sp = sig_stack; |
719 |
– |
new_stack.ss_flags = 0; |
720 |
– |
new_stack.ss_size = SIG_STACK_SIZE; |
721 |
– |
if (sigaltstack(&new_stack, NULL) < 0) { |
722 |
– |
sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno)); |
723 |
– |
ErrorAlert(str); |
724 |
– |
goto quit; |
725 |
– |
} |
726 |
– |
#endif |
727 |
– |
|
728 |
– |
#if !EMULATED_PPC |
729 |
– |
// Install SIGSEGV handler |
730 |
– |
sigemptyset(&sigsegv_action.sa_mask); // Block interrupts during SEGV handling |
731 |
– |
sigaddset(&sigsegv_action.sa_mask, SIGUSR2); |
732 |
– |
sigsegv_action.sa_handler = (__sighandler_t)sigsegv_handler; |
733 |
– |
sigsegv_action.sa_flags = SA_ONSTACK; |
734 |
– |
sigsegv_action.sa_restorer = NULL; |
735 |
– |
if (sigaction(SIGSEGV, &sigsegv_action, NULL) < 0) { |
736 |
– |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
737 |
– |
ErrorAlert(str); |
738 |
– |
goto quit; |
739 |
– |
} |
740 |
– |
|
1013 |
|
// Install SIGILL handler |
1014 |
|
sigemptyset(&sigill_action.sa_mask); // Block interrupts during ILL handling |
1015 |
|
sigaddset(&sigill_action.sa_mask, SIGUSR2); |
1016 |
< |
sigill_action.sa_handler = (__sighandler_t)sigill_handler; |
1017 |
< |
sigill_action.sa_flags = SA_ONSTACK; |
1016 |
> |
sigill_action.sa_sigaction = sigill_handler; |
1017 |
> |
sigill_action.sa_flags = SA_ONSTACK | SA_SIGINFO; |
1018 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
1019 |
|
sigill_action.sa_restorer = NULL; |
1020 |
+ |
#endif |
1021 |
|
if (sigaction(SIGILL, &sigill_action, NULL) < 0) { |
1022 |
< |
sprintf(str, GetString(STR_SIGILL_INSTALL_ERR), strerror(errno)); |
1022 |
> |
sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGILL", strerror(errno)); |
1023 |
|
ErrorAlert(str); |
1024 |
|
goto quit; |
1025 |
|
} |
1026 |
|
#endif |
1027 |
|
|
1028 |
+ |
#if !EMULATED_PPC |
1029 |
|
// Install interrupt signal handler |
1030 |
|
sigemptyset(&sigusr2_action.sa_mask); |
1031 |
< |
sigusr2_action.sa_handler = (__sighandler_t)sigusr2_handler; |
1032 |
< |
sigusr2_action.sa_flags = 0; |
1033 |
< |
#if !EMULATED_PPC |
759 |
< |
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART; |
760 |
< |
#endif |
1031 |
> |
sigusr2_action.sa_sigaction = sigusr2_handler_init; |
1032 |
> |
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART | SA_SIGINFO; |
1033 |
> |
#ifdef HAVE_SIGNAL_SA_RESTORER |
1034 |
|
sigusr2_action.sa_restorer = NULL; |
1035 |
+ |
#endif |
1036 |
|
if (sigaction(SIGUSR2, &sigusr2_action, NULL) < 0) { |
1037 |
< |
sprintf(str, GetString(STR_SIGUSR2_INSTALL_ERR), strerror(errno)); |
1037 |
> |
sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGUSR2", strerror(errno)); |
1038 |
|
ErrorAlert(str); |
1039 |
|
goto quit; |
1040 |
|
} |
1041 |
+ |
#endif |
1042 |
|
|
1043 |
|
// Get my thread ID and execute MacOS thread function |
1044 |
|
emul_thread = pthread_self(); |
1064 |
|
|
1065 |
|
// Stop 60Hz thread |
1066 |
|
if (tick_thread_active) { |
1067 |
+ |
tick_thread_cancel = true; |
1068 |
|
pthread_cancel(tick_thread); |
1069 |
|
pthread_join(tick_thread, NULL); |
1070 |
|
} |
1071 |
|
|
1072 |
|
// Stop NVRAM watchdog thread |
1073 |
|
if (nvram_thread_active) { |
1074 |
+ |
nvram_thread_cancel = true; |
1075 |
|
pthread_cancel(nvram_thread); |
1076 |
|
pthread_join(nvram_thread, NULL); |
1077 |
|
} |
1078 |
|
|
1079 |
|
#if !EMULATED_PPC |
1080 |
< |
// Uninstall SIGSEGV handler |
1080 |
> |
// Uninstall SIGSEGV and SIGBUS handlers |
1081 |
|
sigemptyset(&sigsegv_action.sa_mask); |
1082 |
|
sigsegv_action.sa_handler = SIG_DFL; |
1083 |
|
sigsegv_action.sa_flags = 0; |
1084 |
|
sigaction(SIGSEGV, &sigsegv_action, NULL); |
1085 |
+ |
sigaction(SIGBUS, &sigsegv_action, NULL); |
1086 |
|
|
1087 |
|
// Uninstall SIGILL handler |
1088 |
|
sigemptyset(&sigill_action.sa_mask); |
1089 |
|
sigill_action.sa_handler = SIG_DFL; |
1090 |
|
sigill_action.sa_flags = 0; |
1091 |
|
sigaction(SIGILL, &sigill_action, NULL); |
814 |
– |
#endif |
815 |
– |
|
816 |
– |
// Save NVRAM |
817 |
– |
XPRAMExit(); |
818 |
– |
|
819 |
– |
// Exit clipboard |
820 |
– |
ClipExit(); |
1092 |
|
|
1093 |
< |
// Exit Time Manager |
1094 |
< |
TimerExit(); |
1095 |
< |
|
1096 |
< |
// Exit serial |
1097 |
< |
SerialExit(); |
1098 |
< |
|
828 |
< |
// Exit network |
829 |
< |
EtherExit(); |
830 |
< |
|
831 |
< |
// Exit audio |
832 |
< |
AudioExit(); |
833 |
< |
|
834 |
< |
// Exit video |
835 |
< |
VideoExit(); |
1093 |
> |
// Delete stacks for signal handlers |
1094 |
> |
if (sig_stack.ss_sp) |
1095 |
> |
free(sig_stack.ss_sp); |
1096 |
> |
if (extra_stack.ss_sp) |
1097 |
> |
free(extra_stack.ss_sp); |
1098 |
> |
#endif |
1099 |
|
|
1100 |
< |
// Exit external file system |
1101 |
< |
ExtFSExit(); |
1100 |
> |
// Deinitialize everything |
1101 |
> |
ExitAll(); |
1102 |
|
|
1103 |
< |
// Exit drivers |
1104 |
< |
SCSIExit(); |
842 |
< |
CDROMExit(); |
843 |
< |
DiskExit(); |
844 |
< |
SonyExit(); |
1103 |
> |
// Delete SheepShaver globals |
1104 |
> |
SheepMem::Exit(); |
1105 |
|
|
1106 |
|
// Delete RAM area |
1107 |
|
if (ram_area_mapped) |
1108 |
< |
vm_release((char *)RAM_BASE, RAMSize); |
1108 |
> |
vm_mac_release(RAMBase, RAMSize); |
1109 |
|
|
1110 |
|
// Delete ROM area |
1111 |
|
if (rom_area_mapped) |
1112 |
< |
vm_release((char *)ROM_BASE, ROM_AREA_SIZE); |
1112 |
> |
vm_mac_release(ROMBase, ROM_AREA_SIZE); |
1113 |
|
|
1114 |
< |
// Delete Kernel Data area |
1115 |
< |
if (kernel_area >= 0) { |
1116 |
< |
shmdt((void *)KERNEL_DATA_BASE); |
1117 |
< |
shmdt((void *)KERNEL_DATA2_BASE); |
1118 |
< |
shmctl(kernel_area, IPC_RMID, NULL); |
859 |
< |
} |
1114 |
> |
// Delete DR cache areas |
1115 |
> |
if (dr_emulator_area_mapped) |
1116 |
> |
vm_mac_release(DR_EMULATOR_BASE, DR_EMULATOR_SIZE); |
1117 |
> |
if (dr_cache_area_mapped) |
1118 |
> |
vm_mac_release(DR_CACHE_BASE, DR_CACHE_SIZE); |
1119 |
|
|
1120 |
|
// Delete Low Memory area |
1121 |
|
if (lm_area_mapped) |
1122 |
< |
munmap((char *)0x0000, 0x3000); |
1122 |
> |
vm_mac_release(0, 0x3000); |
1123 |
|
|
1124 |
|
// Close /dev/zero |
1125 |
|
if (zero_fd > 0) |
1137 |
|
#endif |
1138 |
|
|
1139 |
|
// Close X11 server connection |
1140 |
+ |
#ifndef USE_SDL_VIDEO |
1141 |
|
if (x_display) |
1142 |
|
XCloseDisplay(x_display); |
1143 |
+ |
#endif |
1144 |
+ |
|
1145 |
+ |
// Notify GUI we are about to leave |
1146 |
+ |
if (gui_connection) { |
1147 |
+ |
if (rpc_method_invoke(gui_connection, RPC_METHOD_EXIT, RPC_TYPE_INVALID) == RPC_ERROR_NO_ERROR) |
1148 |
+ |
rpc_method_wait_for_reply(gui_connection, RPC_TYPE_INVALID); |
1149 |
+ |
} |
1150 |
|
|
1151 |
|
exit(0); |
1152 |
|
} |
1153 |
|
|
1154 |
|
|
1155 |
|
/* |
1156 |
+ |
* Initialize Kernel Data segments |
1157 |
+ |
*/ |
1158 |
+ |
|
1159 |
+ |
static bool kernel_data_init(void) |
1160 |
+ |
{ |
1161 |
+ |
char str[256]; |
1162 |
+ |
void *kernel_addr1 = NULL; |
1163 |
+ |
void *kernel_addr2 = NULL; |
1164 |
+ |
uint32 kernel_area_size = (KERNEL_AREA_SIZE + SHMLBA - 1) & -SHMLBA; |
1165 |
+ |
|
1166 |
+ |
kernel_area = shmget(IPC_PRIVATE, kernel_area_size, 0600); |
1167 |
+ |
if (kernel_area == -1) { |
1168 |
+ |
sprintf(str, GetString(STR_KD_SHMGET_ERR), strerror(errno)); |
1169 |
+ |
goto fail_shmget; |
1170 |
+ |
} |
1171 |
+ |
kernel_addr1 = Mac2HostAddr(KERNEL_DATA_BASE & -SHMLBA); |
1172 |
+ |
if (shmat(kernel_area, kernel_addr1, 0) != kernel_addr1) { |
1173 |
+ |
sprintf(str, GetString(STR_KD_SHMAT_ERR), strerror(errno)); |
1174 |
+ |
goto fail_shmat1; |
1175 |
+ |
} |
1176 |
+ |
kernel_addr2 = Mac2HostAddr(KERNEL_DATA2_BASE & -SHMLBA); |
1177 |
+ |
if (shmat(kernel_area, kernel_addr2, 0) != kernel_addr2) { |
1178 |
+ |
sprintf(str, GetString(STR_KD2_SHMAT_ERR), strerror(errno)); |
1179 |
+ |
goto fail_shmat2; |
1180 |
+ |
} |
1181 |
+ |
atexit(kernel_data_exit); |
1182 |
+ |
return true; |
1183 |
+ |
|
1184 |
+ |
fail_shmat2: |
1185 |
+ |
shmdt(kernel_addr1); |
1186 |
+ |
fail_shmat1: |
1187 |
+ |
shmctl(kernel_area, IPC_RMID, NULL); |
1188 |
+ |
fail_shmget: |
1189 |
+ |
ErrorAlert(str); |
1190 |
+ |
return false; |
1191 |
+ |
} |
1192 |
+ |
|
1193 |
+ |
|
1194 |
+ |
/* |
1195 |
+ |
* Deallocate Kernel Data segments |
1196 |
+ |
*/ |
1197 |
+ |
|
1198 |
+ |
static void kernel_data_exit(void) |
1199 |
+ |
{ |
1200 |
+ |
if (kernel_area >= 0) { |
1201 |
+ |
shmdt(Mac2HostAddr(KERNEL_DATA_BASE & -SHMLBA)); |
1202 |
+ |
shmdt(Mac2HostAddr(KERNEL_DATA2_BASE & -SHMLBA)); |
1203 |
+ |
shmctl(kernel_area, IPC_RMID, NULL); |
1204 |
+ |
kernel_area = NULL; |
1205 |
+ |
} |
1206 |
+ |
} |
1207 |
+ |
|
1208 |
+ |
|
1209 |
+ |
/* |
1210 |
|
* Jump into Mac ROM, start 680x0 emulator |
1211 |
|
*/ |
1212 |
|
|
1234 |
|
// Jump to ROM boot routine |
1235 |
|
D(bug("Jumping to ROM\n")); |
1236 |
|
#if EMULATED_PPC |
1237 |
< |
jump_to_rom(ROM_BASE + 0x310000); |
1237 |
> |
jump_to_rom(ROMBase + 0x310000); |
1238 |
|
#else |
1239 |
< |
jump_to_rom(ROM_BASE + 0x310000, (uint32)emulator_data); |
1239 |
> |
jump_to_rom(ROMBase + 0x310000, (uint32)emulator_data); |
1240 |
|
#endif |
1241 |
|
D(bug("Returned from ROM\n")); |
1242 |
|
|
1275 |
|
uint16 proc[2] = {trap, M68K_RTS}; |
1276 |
|
Execute68k((uint32)proc, r); |
1277 |
|
} |
957 |
– |
|
958 |
– |
|
959 |
– |
/* |
960 |
– |
* Execute PPC code from EMUL_OP routine (real mode switch) |
961 |
– |
*/ |
962 |
– |
|
963 |
– |
void ExecutePPC(void (*func)()) |
964 |
– |
{ |
965 |
– |
uint32 tvect[2] = {(uint32)func, 0}; // Fake TVECT |
966 |
– |
RoutineDescriptor desc = BUILD_PPC_ROUTINE_DESCRIPTOR(0, tvect); |
967 |
– |
M68kRegisters r; |
968 |
– |
Execute68k((uint32)&desc, &r); |
969 |
– |
} |
1278 |
|
#endif |
1279 |
|
|
1280 |
|
|
1293 |
|
|
1294 |
|
|
1295 |
|
/* |
988 |
– |
* Pause/resume emulator |
989 |
– |
*/ |
990 |
– |
|
991 |
– |
void PauseEmulator(void) |
992 |
– |
{ |
993 |
– |
pthread_kill(emul_thread, SIGSTOP); |
994 |
– |
} |
995 |
– |
|
996 |
– |
void ResumeEmulator(void) |
997 |
– |
{ |
998 |
– |
pthread_kill(emul_thread, SIGCONT); |
999 |
– |
} |
1000 |
– |
|
1001 |
– |
|
1002 |
– |
/* |
1296 |
|
* Dump 68k registers |
1297 |
|
*/ |
1298 |
|
|
1320 |
|
* Make code executable |
1321 |
|
*/ |
1322 |
|
|
1323 |
< |
void MakeExecutable(int dummy, void *start, uint32 length) |
1323 |
> |
void MakeExecutable(int dummy, uint32 start, uint32 length) |
1324 |
|
{ |
1325 |
< |
if (((uintptr)start >= ROM_BASE) && ((uintptr)start < (ROM_BASE + ROM_SIZE))) |
1325 |
> |
if ((start >= ROMBase) && (start < (ROMBase + ROM_SIZE))) |
1326 |
|
return; |
1327 |
|
#if EMULATED_PPC |
1328 |
< |
FlushCodeCache((uintptr)start, (uintptr)start + length); |
1328 |
> |
FlushCodeCache(start, start + length); |
1329 |
|
#else |
1330 |
< |
flush_icache_range(start, (void *)((uintptr)start + length)); |
1330 |
> |
flush_icache_range(start, start + length); |
1331 |
|
#endif |
1332 |
|
} |
1333 |
|
|
1334 |
|
|
1335 |
|
/* |
1336 |
< |
* Patch things after system startup (gets called by disk driver accRun routine) |
1336 |
> |
* NVRAM watchdog thread (saves NVRAM every minute) |
1337 |
|
*/ |
1338 |
|
|
1339 |
< |
void PatchAfterStartup(void) |
1339 |
> |
static void nvram_watchdog(void) |
1340 |
|
{ |
1341 |
< |
#if EMULATED_PPC |
1342 |
< |
ExecuteNative(NATIVE_VIDEO_INSTALL_ACCEL); |
1343 |
< |
#else |
1344 |
< |
ExecutePPC(VideoInstallAccel); |
1052 |
< |
#endif |
1053 |
< |
InstallExtFS(); |
1341 |
> |
if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) { |
1342 |
> |
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
1343 |
> |
SaveXPRAM(); |
1344 |
> |
} |
1345 |
|
} |
1346 |
|
|
1056 |
– |
|
1057 |
– |
/* |
1058 |
– |
* NVRAM watchdog thread (saves NVRAM every minute) |
1059 |
– |
*/ |
1060 |
– |
|
1347 |
|
static void *nvram_func(void *arg) |
1348 |
|
{ |
1349 |
< |
struct timespec req = {60, 0}; // 1 minute |
1350 |
< |
|
1351 |
< |
for (;;) { |
1352 |
< |
pthread_testcancel(); |
1067 |
< |
nanosleep(&req, NULL); |
1068 |
< |
pthread_testcancel(); |
1069 |
< |
if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) { |
1070 |
< |
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
1071 |
< |
SaveXPRAM(); |
1072 |
< |
} |
1349 |
> |
while (!nvram_thread_cancel) { |
1350 |
> |
for (int i=0; i<60 && !nvram_thread_cancel; i++) |
1351 |
> |
Delay_usec(999999); // Only wait 1 second so we quit promptly when nvram_thread_cancel becomes true |
1352 |
> |
nvram_watchdog(); |
1353 |
|
} |
1354 |
|
return NULL; |
1355 |
|
} |
1362 |
|
static void *tick_func(void *arg) |
1363 |
|
{ |
1364 |
|
int tick_counter = 0; |
1365 |
< |
struct timespec req = {0, 16625000}; |
1365 |
> |
uint64 start = GetTicks_usec(); |
1366 |
> |
int64 ticks = 0; |
1367 |
> |
uint64 next = GetTicks_usec(); |
1368 |
|
|
1369 |
< |
for (;;) { |
1369 |
> |
while (!tick_thread_cancel) { |
1370 |
|
|
1371 |
|
// Wait |
1372 |
< |
nanosleep(&req, NULL); |
1372 |
> |
next += 16625; |
1373 |
> |
int64 delay = next - GetTicks_usec(); |
1374 |
> |
if (delay > 0) |
1375 |
> |
Delay_usec(delay); |
1376 |
> |
else if (delay < -16625) |
1377 |
> |
next = GetTicks_usec(); |
1378 |
> |
ticks++; |
1379 |
|
|
1380 |
|
#if !EMULATED_PPC |
1381 |
|
// Did we crash? |
1382 |
|
if (emul_thread_fatal) { |
1383 |
|
|
1384 |
|
// Yes, dump registers |
1385 |
< |
pt_regs *r = (pt_regs *)&sigsegv_regs; |
1385 |
> |
sigregs *r = &sigsegv_regs; |
1386 |
|
char str[256]; |
1387 |
< |
sprintf(str, "SIGSEGV\n" |
1387 |
> |
if (crash_reason == NULL) |
1388 |
> |
crash_reason = "SIGSEGV"; |
1389 |
> |
sprintf(str, "%s\n" |
1390 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1391 |
|
" xer %08lx cr %08lx \n" |
1392 |
|
" r0 %08lx r1 %08lx r2 %08lx r3 %08lx\n" |
1397 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1398 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1399 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1400 |
+ |
crash_reason, |
1401 |
|
r->nip, r->link, r->ctr, r->msr, |
1402 |
|
r->xer, r->ccr, |
1403 |
|
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1433 |
|
TriggerInterrupt(); |
1434 |
|
} |
1435 |
|
} |
1436 |
+ |
|
1437 |
+ |
uint64 end = GetTicks_usec(); |
1438 |
+ |
D(bug("%lld ticks in %lld usec = %f ticks/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start))); |
1439 |
|
return NULL; |
1440 |
|
} |
1441 |
|
|
1485 |
|
pthread_mutexattr_init(&attr); |
1486 |
|
// Initialize the mutex for priority inheritance -- |
1487 |
|
// required for accurate timing. |
1488 |
< |
#ifdef HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL |
1488 |
> |
#if defined(HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL) && !defined(__CYGWIN__) |
1489 |
|
pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT); |
1490 |
|
#endif |
1491 |
|
#if defined(HAVE_PTHREAD_MUTEXATTR_SETTYPE) && defined(PTHREAD_MUTEX_NORMAL) |
1556 |
|
* Trigger signal USR2 from another thread |
1557 |
|
*/ |
1558 |
|
|
1559 |
< |
#if !EMULATED_PPC || ASYNC_IRQ |
1559 |
> |
#if !EMULATED_PPC |
1560 |
|
void TriggerInterrupt(void) |
1561 |
|
{ |
1562 |
< |
if (ready_for_signals) |
1562 |
> |
if (ready_for_signals) { |
1563 |
> |
idle_resume(); |
1564 |
|
pthread_kill(emul_thread, SIGUSR2); |
1565 |
+ |
} |
1566 |
|
} |
1567 |
|
#endif |
1568 |
|
|
1590 |
|
|
1591 |
|
void DisableInterrupt(void) |
1592 |
|
{ |
1593 |
+ |
#if EMULATED_PPC |
1594 |
+ |
WriteMacInt32(XLM_IRQ_NEST, int32(ReadMacInt32(XLM_IRQ_NEST)) + 1); |
1595 |
+ |
#else |
1596 |
|
atomic_add((int *)XLM_IRQ_NEST, 1); |
1597 |
+ |
#endif |
1598 |
|
} |
1599 |
|
|
1600 |
|
|
1604 |
|
|
1605 |
|
void EnableInterrupt(void) |
1606 |
|
{ |
1607 |
+ |
#if EMULATED_PPC |
1608 |
+ |
WriteMacInt32(XLM_IRQ_NEST, int32(ReadMacInt32(XLM_IRQ_NEST)) - 1); |
1609 |
+ |
#else |
1610 |
|
atomic_add((int *)XLM_IRQ_NEST, -1); |
1611 |
+ |
#endif |
1612 |
|
} |
1613 |
|
|
1614 |
|
|
1616 |
|
* USR2 handler |
1617 |
|
*/ |
1618 |
|
|
1619 |
< |
#if EMULATED_PPC |
1620 |
< |
static void sigusr2_handler(int sig) |
1619 |
> |
#if !EMULATED_PPC |
1620 |
> |
void sigusr2_handler(int sig, siginfo_t *sip, void *scp) |
1621 |
|
{ |
1622 |
< |
#if ASYNC_IRQ |
1623 |
< |
extern void HandleInterrupt(void); |
1624 |
< |
HandleInterrupt(); |
1622 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1623 |
> |
|
1624 |
> |
#ifdef SYSTEM_CLOBBERS_R2 |
1625 |
> |
// Restore pointer to Thread Local Storage |
1626 |
> |
set_r2(TOC); |
1627 |
> |
#endif |
1628 |
> |
#ifdef SYSTEM_CLOBBERS_R13 |
1629 |
> |
// Restore pointer to .sdata section |
1630 |
> |
set_r13(R13); |
1631 |
> |
#endif |
1632 |
> |
|
1633 |
> |
#ifdef USE_SDL_VIDEO |
1634 |
> |
// We must fill in the events queue in the same thread that did call SDL_SetVideoMode() |
1635 |
> |
SDL_PumpEvents(); |
1636 |
|
#endif |
1322 |
– |
} |
1323 |
– |
#else |
1324 |
– |
static void sigusr2_handler(int sig, sigcontext_struct *sc) |
1325 |
– |
{ |
1326 |
– |
pt_regs *r = sc->regs; |
1637 |
|
|
1638 |
|
// Do nothing if interrupts are disabled |
1639 |
|
if (*(int32 *)XLM_IRQ_NEST > 0) |
1647 |
|
case MODE_68K: |
1648 |
|
// 68k emulator active, trigger 68k interrupt level 1 |
1649 |
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1650 |
< |
r->ccr |= ntohl(kernel_data->v[0x674 >> 2]); |
1650 |
> |
r->cr() |= ntohl(kernel_data->v[0x674 >> 2]); |
1651 |
|
break; |
1652 |
|
|
1653 |
|
#if INTERRUPTS_IN_NATIVE_MODE |
1654 |
|
case MODE_NATIVE: |
1655 |
|
// 68k emulator inactive, in nanokernel? |
1656 |
< |
if (r->gpr[1] != KernelDataAddr) { |
1656 |
> |
if (r->gpr(1) != KernelDataAddr) { |
1657 |
> |
|
1658 |
> |
// Set extra stack for SIGSEGV handler |
1659 |
> |
sigaltstack(&extra_stack, NULL); |
1660 |
> |
|
1661 |
|
// Prepare for 68k interrupt level 1 |
1662 |
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1663 |
|
WriteMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc, ReadMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc) | ntohl(kernel_data->v[0x674 >> 2])); |
1664 |
|
|
1665 |
|
// Execute nanokernel interrupt routine (this will activate the 68k emulator) |
1666 |
< |
atomic_add((int32 *)XLM_IRQ_NEST, 1); |
1666 |
> |
DisableInterrupt(); |
1667 |
|
if (ROMType == ROMTYPE_NEWWORLD) |
1668 |
< |
ppc_interrupt(ROM_BASE + 0x312b1c, KernelDataAddr); |
1668 |
> |
ppc_interrupt(ROMBase + 0x312b1c, KernelDataAddr); |
1669 |
|
else |
1670 |
< |
ppc_interrupt(ROM_BASE + 0x312a3c, KernelDataAddr); |
1670 |
> |
ppc_interrupt(ROMBase + 0x312a3c, KernelDataAddr); |
1671 |
> |
|
1672 |
> |
// Reset normal stack |
1673 |
> |
sigaltstack(&sig_stack, NULL); |
1674 |
|
} |
1675 |
|
break; |
1676 |
|
#endif |
1681 |
|
if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) { |
1682 |
|
|
1683 |
|
// Set extra stack for SIGSEGV handler |
1684 |
< |
struct sigaltstack new_stack; |
1368 |
< |
new_stack.ss_sp = extra_stack; |
1369 |
< |
new_stack.ss_flags = 0; |
1370 |
< |
new_stack.ss_size = SIG_STACK_SIZE; |
1371 |
< |
sigaltstack(&new_stack, NULL); |
1684 |
> |
sigaltstack(&extra_stack, NULL); |
1685 |
|
#if 1 |
1686 |
|
// Execute full 68k interrupt routine |
1687 |
|
M68kRegisters r; |
1703 |
|
if (InterruptFlags & INTFLAG_VIA) { |
1704 |
|
ClearInterruptFlag(INTFLAG_VIA); |
1705 |
|
ADBInterrupt(); |
1706 |
< |
ExecutePPC(VideoVBL); |
1706 |
> |
ExecuteNative(NATIVE_VIDEO_VBL); |
1707 |
|
} |
1708 |
|
} |
1709 |
|
#endif |
1710 |
< |
// Reset normal signal stack |
1711 |
< |
new_stack.ss_sp = sig_stack; |
1399 |
< |
new_stack.ss_flags = 0; |
1400 |
< |
new_stack.ss_size = SIG_STACK_SIZE; |
1401 |
< |
sigaltstack(&new_stack, NULL); |
1710 |
> |
// Reset normal stack |
1711 |
> |
sigaltstack(&sig_stack, NULL); |
1712 |
|
} |
1713 |
|
break; |
1714 |
|
#endif |
1722 |
|
*/ |
1723 |
|
|
1724 |
|
#if !EMULATED_PPC |
1725 |
< |
static void sigsegv_handler(int sig, sigcontext_struct *sc) |
1725 |
> |
static void sigsegv_handler(int sig, siginfo_t *sip, void *scp) |
1726 |
|
{ |
1727 |
< |
pt_regs *r = sc->regs; |
1727 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1728 |
|
|
1729 |
|
// Get effective address |
1730 |
< |
uint32 addr = r->dar; |
1730 |
> |
uint32 addr = r->dar(); |
1731 |
|
|
1732 |
+ |
#ifdef SYSTEM_CLOBBERS_R2 |
1733 |
+ |
// Restore pointer to Thread Local Storage |
1734 |
+ |
set_r2(TOC); |
1735 |
+ |
#endif |
1736 |
+ |
#ifdef SYSTEM_CLOBBERS_R13 |
1737 |
+ |
// Restore pointer to .sdata section |
1738 |
+ |
set_r13(R13); |
1739 |
+ |
#endif |
1740 |
+ |
|
1741 |
|
#if ENABLE_VOSF |
1742 |
< |
// Handle screen fault. |
1743 |
< |
extern bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction); |
1744 |
< |
if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->nip)) |
1742 |
> |
// Handle screen fault |
1743 |
> |
#if SIGSEGV_CHECK_VERSION(1,0,0) |
1744 |
> |
sigsegv_info_t si; |
1745 |
> |
si.addr = (sigsegv_address_t)addr; |
1746 |
> |
si.pc = (sigsegv_address_t)r->pc(); |
1747 |
> |
#endif |
1748 |
> |
extern bool Screen_fault_handler(sigsegv_info_t *sip); |
1749 |
> |
if (Screen_fault_handler(&si)) |
1750 |
|
return; |
1751 |
|
#endif |
1752 |
|
|
1753 |
< |
num_segv++; |
1754 |
< |
|
1431 |
< |
// Fault in Mac ROM or RAM? |
1432 |
< |
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
1753 |
> |
// Fault in Mac ROM or RAM or DR Cache? |
1754 |
> |
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)); |
1755 |
|
if (mac_fault) { |
1756 |
|
|
1757 |
|
// "VM settings" during MacOS 8 installation |
1758 |
< |
if (r->nip == ROM_BASE + 0x488160 && r->gpr[20] == 0xf8000000) { |
1759 |
< |
r->nip += 4; |
1760 |
< |
r->gpr[8] = 0; |
1758 |
> |
if (r->pc() == ROMBase + 0x488160 && r->gpr(20) == 0xf8000000) { |
1759 |
> |
r->pc() += 4; |
1760 |
> |
r->gpr(8) = 0; |
1761 |
|
return; |
1762 |
|
|
1763 |
|
// MacOS 8.5 installation |
1764 |
< |
} else if (r->nip == ROM_BASE + 0x488140 && r->gpr[16] == 0xf8000000) { |
1765 |
< |
r->nip += 4; |
1766 |
< |
r->gpr[8] = 0; |
1764 |
> |
} else if (r->pc() == ROMBase + 0x488140 && r->gpr(16) == 0xf8000000) { |
1765 |
> |
r->pc() += 4; |
1766 |
> |
r->gpr(8) = 0; |
1767 |
|
return; |
1768 |
|
|
1769 |
|
// MacOS 8 serial drivers on startup |
1770 |
< |
} else if (r->nip == ROM_BASE + 0x48e080 && (r->gpr[8] == 0xf3012002 || r->gpr[8] == 0xf3012000)) { |
1771 |
< |
r->nip += 4; |
1772 |
< |
r->gpr[8] = 0; |
1770 |
> |
} else if (r->pc() == ROMBase + 0x48e080 && (r->gpr(8) == 0xf3012002 || r->gpr(8) == 0xf3012000)) { |
1771 |
> |
r->pc() += 4; |
1772 |
> |
r->gpr(8) = 0; |
1773 |
|
return; |
1774 |
|
|
1775 |
|
// MacOS 8.1 serial drivers on startup |
1776 |
< |
} else if (r->nip == ROM_BASE + 0x48c5e0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1777 |
< |
r->nip += 4; |
1776 |
> |
} else if (r->pc() == ROMBase + 0x48c5e0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1777 |
> |
r->pc() += 4; |
1778 |
|
return; |
1779 |
< |
} else if (r->nip == ROM_BASE + 0x4a10a0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1780 |
< |
r->nip += 4; |
1779 |
> |
} else if (r->pc() == ROMBase + 0x4a10a0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1780 |
> |
r->pc() += 4; |
1781 |
> |
return; |
1782 |
> |
|
1783 |
> |
// MacOS 8.6 serial drivers on startup (with DR Cache and OldWorld ROM) |
1784 |
> |
} else if ((r->pc() - DR_CACHE_BASE) < DR_CACHE_SIZE && (r->gpr(16) == 0xf3012002 || r->gpr(16) == 0xf3012000)) { |
1785 |
> |
r->pc() += 4; |
1786 |
> |
return; |
1787 |
> |
} else if ((r->pc() - DR_CACHE_BASE) < DR_CACHE_SIZE && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1788 |
> |
r->pc() += 4; |
1789 |
|
return; |
1790 |
|
} |
1791 |
|
|
1792 |
|
// Get opcode and divide into fields |
1793 |
< |
uint32 opcode = *((uint32 *)r->nip); |
1793 |
> |
uint32 opcode = *((uint32 *)r->pc()); |
1794 |
|
uint32 primop = opcode >> 26; |
1795 |
|
uint32 exop = (opcode >> 1) & 0x3ff; |
1796 |
|
uint32 ra = (opcode >> 16) & 0x1f; |
1879 |
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_NORM; break; |
1880 |
|
case 45: // sthu |
1881 |
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_U; break; |
1882 |
+ |
#if EMULATE_UNALIGNED_LOADSTORE_MULTIPLE |
1883 |
+ |
case 46: // lmw |
1884 |
+ |
if ((addr % 4) != 0) { |
1885 |
+ |
uint32 ea = addr; |
1886 |
+ |
D(bug("WARNING: unaligned lmw to EA=%08x from IP=%08x\n", ea, r->pc())); |
1887 |
+ |
for (int i = rd; i <= 31; i++) { |
1888 |
+ |
r->gpr(i) = ReadMacInt32(ea); |
1889 |
+ |
ea += 4; |
1890 |
+ |
} |
1891 |
+ |
r->pc() += 4; |
1892 |
+ |
goto rti; |
1893 |
+ |
} |
1894 |
+ |
break; |
1895 |
+ |
case 47: // stmw |
1896 |
+ |
if ((addr % 4) != 0) { |
1897 |
+ |
uint32 ea = addr; |
1898 |
+ |
D(bug("WARNING: unaligned stmw to EA=%08x from IP=%08x\n", ea, r->pc())); |
1899 |
+ |
for (int i = rd; i <= 31; i++) { |
1900 |
+ |
WriteMacInt32(ea, r->gpr(i)); |
1901 |
+ |
ea += 4; |
1902 |
+ |
} |
1903 |
+ |
r->pc() += 4; |
1904 |
+ |
goto rti; |
1905 |
+ |
} |
1906 |
+ |
break; |
1907 |
+ |
#endif |
1908 |
|
} |
1909 |
|
|
1910 |
< |
// Ignore ROM writes |
1911 |
< |
if (transfer_type == TYPE_STORE && addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) { |
1912 |
< |
// 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)); |
1910 |
> |
// Ignore ROM writes (including to the zero page, which is read-only) |
1911 |
> |
if (transfer_type == TYPE_STORE && |
1912 |
> |
((addr >= ROMBase && addr < ROMBase + ROM_SIZE) || |
1913 |
> |
(addr >= SheepMem::ZeroPage() && addr < SheepMem::ZeroPage() + SheepMem::PageSize()))) { |
1914 |
> |
// 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())); |
1915 |
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1916 |
< |
r->gpr[ra] = addr; |
1917 |
< |
r->nip += 4; |
1916 |
> |
r->gpr(ra) = addr; |
1917 |
> |
r->pc() += 4; |
1918 |
|
goto rti; |
1919 |
|
} |
1920 |
|
|
1921 |
|
// Ignore illegal memory accesses? |
1922 |
|
if (PrefsFindBool("ignoresegv")) { |
1923 |
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1924 |
< |
r->gpr[ra] = addr; |
1924 |
> |
r->gpr(ra) = addr; |
1925 |
|
if (transfer_type == TYPE_LOAD) |
1926 |
< |
r->gpr[rd] = 0; |
1927 |
< |
r->nip += 4; |
1926 |
> |
r->gpr(rd) = 0; |
1927 |
> |
r->pc() += 4; |
1928 |
|
goto rti; |
1929 |
|
} |
1930 |
|
|
1932 |
|
if (!PrefsFindBool("nogui")) { |
1933 |
|
char str[256]; |
1934 |
|
if (transfer_type == TYPE_LOAD || transfer_type == TYPE_STORE) |
1935 |
< |
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]); |
1935 |
> |
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)); |
1936 |
|
else |
1937 |
< |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode); |
1937 |
> |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode); |
1938 |
|
ErrorAlert(str); |
1939 |
|
QuitEmulator(); |
1940 |
|
return; |
1942 |
|
} |
1943 |
|
|
1944 |
|
// For all other errors, jump into debugger (sort of...) |
1945 |
+ |
crash_reason = (sig == SIGBUS) ? "SIGBUS" : "SIGSEGV"; |
1946 |
|
if (!ready_for_signals) { |
1947 |
< |
printf("SIGSEGV\n"); |
1948 |
< |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
1947 |
> |
printf("%s\n"); |
1948 |
> |
printf(" sigcontext %p, machine_regs %p\n", scp, r); |
1949 |
|
printf( |
1950 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1951 |
|
" xer %08lx cr %08lx \n" |
1957 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1958 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1959 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1960 |
< |
r->nip, r->link, r->ctr, r->msr, |
1961 |
< |
r->xer, r->ccr, |
1962 |
< |
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1963 |
< |
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
1964 |
< |
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
1965 |
< |
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
1966 |
< |
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
1967 |
< |
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
1968 |
< |
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
1969 |
< |
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
1960 |
> |
crash_reason, |
1961 |
> |
r->pc(), r->lr(), r->ctr(), r->msr(), |
1962 |
> |
r->xer(), r->cr(), |
1963 |
> |
r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3), |
1964 |
> |
r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7), |
1965 |
> |
r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11), |
1966 |
> |
r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15), |
1967 |
> |
r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19), |
1968 |
> |
r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23), |
1969 |
> |
r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27), |
1970 |
> |
r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31)); |
1971 |
|
exit(1); |
1972 |
|
QuitEmulator(); |
1973 |
|
return; |
1974 |
|
} else { |
1975 |
|
// We crashed. Save registers, tell tick thread and loop forever |
1976 |
< |
sigsegv_regs = *(sigregs *)r; |
1976 |
> |
build_sigregs(&sigsegv_regs, r); |
1977 |
|
emul_thread_fatal = true; |
1978 |
|
for (;;) ; |
1979 |
|
} |
1985 |
|
* SIGILL handler |
1986 |
|
*/ |
1987 |
|
|
1988 |
< |
static void sigill_handler(int sig, sigcontext_struct *sc) |
1988 |
> |
static void sigill_handler(int sig, siginfo_t *sip, void *scp) |
1989 |
|
{ |
1990 |
< |
pt_regs *r = sc->regs; |
1990 |
> |
machine_regs *r = MACHINE_REGISTERS(scp); |
1991 |
|
char str[256]; |
1992 |
|
|
1993 |
+ |
#ifdef SYSTEM_CLOBBERS_R2 |
1994 |
+ |
// Restore pointer to Thread Local Storage |
1995 |
+ |
set_r2(TOC); |
1996 |
+ |
#endif |
1997 |
+ |
#ifdef SYSTEM_CLOBBERS_R13 |
1998 |
+ |
// Restore pointer to .sdata section |
1999 |
+ |
set_r13(R13); |
2000 |
+ |
#endif |
2001 |
+ |
|
2002 |
|
// Fault in Mac ROM or RAM? |
2003 |
< |
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
2003 |
> |
bool mac_fault = (r->pc() >= ROMBase) && (r->pc() < (ROMBase + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize)); |
2004 |
|
if (mac_fault) { |
2005 |
|
|
2006 |
|
// Get opcode and divide into fields |
2007 |
< |
uint32 opcode = *((uint32 *)r->nip); |
2007 |
> |
uint32 opcode = *((uint32 *)r->pc()); |
2008 |
|
uint32 primop = opcode >> 26; |
2009 |
|
uint32 exop = (opcode >> 1) & 0x3ff; |
2010 |
|
uint32 ra = (opcode >> 16) & 0x1f; |
2015 |
|
switch (primop) { |
2016 |
|
case 9: // POWER instructions |
2017 |
|
case 22: |
2018 |
< |
power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->nip, r->gpr[1], opcode); |
2018 |
> |
power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->pc(), r->gpr(1), opcode); |
2019 |
|
ErrorAlert(str); |
2020 |
|
QuitEmulator(); |
2021 |
|
return; |
2023 |
|
case 31: |
2024 |
|
switch (exop) { |
2025 |
|
case 83: // mfmsr |
2026 |
< |
r->gpr[rd] = 0xf072; |
2027 |
< |
r->nip += 4; |
2026 |
> |
r->gpr(rd) = 0xf072; |
2027 |
> |
r->pc() += 4; |
2028 |
|
goto rti; |
2029 |
|
|
2030 |
|
case 210: // mtsr |
2031 |
|
case 242: // mtsrin |
2032 |
|
case 306: // tlbie |
2033 |
< |
r->nip += 4; |
2033 |
> |
r->pc() += 4; |
2034 |
|
goto rti; |
2035 |
|
|
2036 |
|
case 339: { // mfspr |
2046 |
|
case 957: // PMC3 |
2047 |
|
case 958: // PMC4 |
2048 |
|
case 959: // SDA |
2049 |
< |
r->nip += 4; |
2049 |
> |
r->pc() += 4; |
2050 |
|
goto rti; |
2051 |
|
case 25: // SDR1 |
2052 |
< |
r->gpr[rd] = 0xdead001f; |
2053 |
< |
r->nip += 4; |
2052 |
> |
r->gpr(rd) = 0xdead001f; |
2053 |
> |
r->pc() += 4; |
2054 |
|
goto rti; |
2055 |
|
case 287: // PVR |
2056 |
< |
r->gpr[rd] = PVR; |
2057 |
< |
r->nip += 4; |
2056 |
> |
r->gpr(rd) = PVR; |
2057 |
> |
r->pc() += 4; |
2058 |
|
goto rti; |
2059 |
|
} |
2060 |
|
break; |
2090 |
|
case 957: // PMC3 |
2091 |
|
case 958: // PMC4 |
2092 |
|
case 959: // SDA |
2093 |
< |
r->nip += 4; |
2093 |
> |
r->pc() += 4; |
2094 |
|
goto rti; |
2095 |
|
} |
2096 |
|
break; |
2109 |
|
|
2110 |
|
// In GUI mode, show error alert |
2111 |
|
if (!PrefsFindBool("nogui")) { |
2112 |
< |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode); |
2112 |
> |
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->pc(), r->gpr(24), r->gpr(1), opcode); |
2113 |
|
ErrorAlert(str); |
2114 |
|
QuitEmulator(); |
2115 |
|
return; |
2117 |
|
} |
2118 |
|
|
2119 |
|
// For all other errors, jump into debugger (sort of...) |
2120 |
+ |
crash_reason = "SIGILL"; |
2121 |
|
if (!ready_for_signals) { |
2122 |
< |
printf("SIGILL\n"); |
2123 |
< |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
2122 |
> |
printf("%s\n"); |
2123 |
> |
printf(" sigcontext %p, machine_regs %p\n", scp, r); |
2124 |
|
printf( |
2125 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
2126 |
|
" xer %08lx cr %08lx \n" |
2132 |
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
2133 |
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
2134 |
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
2135 |
< |
r->nip, r->link, r->ctr, r->msr, |
2136 |
< |
r->xer, r->ccr, |
2137 |
< |
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
2138 |
< |
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
2139 |
< |
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
2140 |
< |
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
2141 |
< |
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
2142 |
< |
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
2143 |
< |
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
2144 |
< |
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
2135 |
> |
crash_reason, |
2136 |
> |
r->pc(), r->lr(), r->ctr(), r->msr(), |
2137 |
> |
r->xer(), r->cr(), |
2138 |
> |
r->gpr(0), r->gpr(1), r->gpr(2), r->gpr(3), |
2139 |
> |
r->gpr(4), r->gpr(5), r->gpr(6), r->gpr(7), |
2140 |
> |
r->gpr(8), r->gpr(9), r->gpr(10), r->gpr(11), |
2141 |
> |
r->gpr(12), r->gpr(13), r->gpr(14), r->gpr(15), |
2142 |
> |
r->gpr(16), r->gpr(17), r->gpr(18), r->gpr(19), |
2143 |
> |
r->gpr(20), r->gpr(21), r->gpr(22), r->gpr(23), |
2144 |
> |
r->gpr(24), r->gpr(25), r->gpr(26), r->gpr(27), |
2145 |
> |
r->gpr(28), r->gpr(29), r->gpr(30), r->gpr(31)); |
2146 |
|
exit(1); |
2147 |
|
QuitEmulator(); |
2148 |
|
return; |
2149 |
|
} else { |
2150 |
|
// We crashed. Save registers, tell tick thread and loop forever |
2151 |
< |
sigsegv_regs = *(sigregs *)r; |
2151 |
> |
build_sigregs(&sigsegv_regs, r); |
2152 |
|
emul_thread_fatal = true; |
2153 |
|
for (;;) ; |
2154 |
|
} |
2158 |
|
|
2159 |
|
|
2160 |
|
/* |
2161 |
+ |
* Helpers to share 32-bit addressable data with MacOS |
2162 |
+ |
*/ |
2163 |
+ |
|
2164 |
+ |
bool SheepMem::Init(void) |
2165 |
+ |
{ |
2166 |
+ |
// Size of a native page |
2167 |
+ |
page_size = getpagesize(); |
2168 |
+ |
|
2169 |
+ |
// Allocate SheepShaver globals |
2170 |
+ |
proc = base; |
2171 |
+ |
if (vm_mac_acquire_fixed(base, size) < 0) |
2172 |
+ |
return false; |
2173 |
+ |
|
2174 |
+ |
// Allocate page with all bits set to 0, right in the middle |
2175 |
+ |
// This is also used to catch undesired overlaps between proc and data areas |
2176 |
+ |
zero_page = proc + (size / 2); |
2177 |
+ |
Mac_memset(zero_page, 0, page_size); |
2178 |
+ |
if (vm_protect(Mac2HostAddr(zero_page), page_size, VM_PAGE_READ) < 0) |
2179 |
+ |
return false; |
2180 |
+ |
|
2181 |
+ |
#if EMULATED_PPC |
2182 |
+ |
// Allocate alternate stack for PowerPC interrupt routine |
2183 |
+ |
sig_stack = base + size; |
2184 |
+ |
if (vm_mac_acquire_fixed(sig_stack, SIG_STACK_SIZE) < 0) |
2185 |
+ |
return false; |
2186 |
+ |
#endif |
2187 |
+ |
|
2188 |
+ |
data = base + size; |
2189 |
+ |
return true; |
2190 |
+ |
} |
2191 |
+ |
|
2192 |
+ |
void SheepMem::Exit(void) |
2193 |
+ |
{ |
2194 |
+ |
if (data) { |
2195 |
+ |
// Delete SheepShaver globals |
2196 |
+ |
vm_mac_release(base, size); |
2197 |
+ |
|
2198 |
+ |
#if EMULATED_PPC |
2199 |
+ |
// Delete alternate stack for PowerPC interrupt routine |
2200 |
+ |
vm_mac_release(sig_stack, SIG_STACK_SIZE); |
2201 |
+ |
#endif |
2202 |
+ |
} |
2203 |
+ |
} |
2204 |
+ |
|
2205 |
+ |
|
2206 |
+ |
/* |
2207 |
|
* Display alert |
2208 |
|
*/ |
2209 |
|
|
2252 |
|
|
2253 |
|
void ErrorAlert(const char *text) |
2254 |
|
{ |
2255 |
< |
#ifdef ENABLE_GTK |
2255 |
> |
if (gui_connection) { |
2256 |
> |
if (rpc_method_invoke(gui_connection, RPC_METHOD_ERROR_ALERT, RPC_TYPE_STRING, text, RPC_TYPE_INVALID) == RPC_ERROR_NO_ERROR && |
2257 |
> |
rpc_method_wait_for_reply(gui_connection, RPC_TYPE_INVALID) == RPC_ERROR_NO_ERROR) |
2258 |
> |
return; |
2259 |
> |
} |
2260 |
> |
#if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO) |
2261 |
|
if (PrefsFindBool("nogui") || x_display == NULL) { |
2262 |
|
printf(GetString(STR_SHELL_ERROR_PREFIX), text); |
2263 |
|
return; |
2276 |
|
|
2277 |
|
void WarningAlert(const char *text) |
2278 |
|
{ |
2279 |
< |
#ifdef ENABLE_GTK |
2279 |
> |
if (gui_connection) { |
2280 |
> |
if (rpc_method_invoke(gui_connection, RPC_METHOD_WARNING_ALERT, RPC_TYPE_STRING, text, RPC_TYPE_INVALID) == RPC_ERROR_NO_ERROR && |
2281 |
> |
rpc_method_wait_for_reply(gui_connection, RPC_TYPE_INVALID) == RPC_ERROR_NO_ERROR) |
2282 |
> |
return; |
2283 |
> |
} |
2284 |
> |
#if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO) |
2285 |
|
if (PrefsFindBool("nogui") || x_display == NULL) { |
2286 |
|
printf(GetString(STR_SHELL_WARNING_PREFIX), text); |
2287 |
|
return; |