1 |
cebix |
1.1 |
/* |
2 |
|
|
* main_unix.cpp - Emulation core, Unix implementation |
3 |
|
|
* |
4 |
cebix |
1.25 |
* SheepShaver (C) 1997-2004 Christian Bauer and Marc Hellwig |
5 |
cebix |
1.1 |
* |
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 |
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* This program is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
/* |
22 |
|
|
* NOTES: |
23 |
|
|
* |
24 |
|
|
* See main_beos.cpp for a description of the three operating modes. |
25 |
|
|
* |
26 |
|
|
* In addition to that, we have to handle the fact that the MacOS ABI |
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 |
31 |
|
|
* - r13 is used as a small data pointer under Linux (but appearently |
32 |
|
|
* it is not used this way? To be sure, we specify -msdata=none |
33 |
|
|
* in the Makefile) |
34 |
|
|
* - As there is no TOC, there are also no TVECTs under Linux; |
35 |
|
|
* function pointers point directly to the function code |
36 |
|
|
* The Execute*() functions have to account for this. Additionally, we |
37 |
|
|
* cannot simply call MacOS functions by getting their TVECT and jumping |
38 |
|
|
* to it. Such calls are done via the call_macos*() functions in |
39 |
|
|
* asm_linux.S that create a MacOS stack frame, load the TOC pointer |
40 |
|
|
* and put the arguments into the right registers. |
41 |
|
|
* |
42 |
|
|
* As on the BeOS, we have to specify an alternate signal stack because |
43 |
|
|
* interrupts (and, under Linux, Low Memory accesses) may occur when r1 |
44 |
|
|
* is pointing to the Kernel Data or to Low Memory. There is one |
45 |
|
|
* problem, however, due to the alternate signal stack being global to |
46 |
|
|
* all signal handlers. Consider the following scenario: |
47 |
|
|
* - The main thread is executing some native PPC MacOS code in |
48 |
|
|
* MODE_NATIVE, running on the MacOS stack (somewhere in the Mac RAM). |
49 |
|
|
* - A SIGUSR2 interrupt occurs. The kernel switches to the signal |
50 |
|
|
* stack and starts executing the SIGUSR2 signal handler. |
51 |
|
|
* - The signal handler sees the MODE_NATIVE and calls ppc_interrupt() |
52 |
|
|
* to handle a native interrupt. |
53 |
|
|
* - ppc_interrupt() sets r1 to point to the Kernel Data and jumps to |
54 |
|
|
* the nanokernel. |
55 |
|
|
* - The nanokernel accesses a Low Memory global (most likely one of |
56 |
|
|
* the XLMs), a SIGSEGV occurs. |
57 |
|
|
* - The kernel sees that r1 does not point to the signal stack and |
58 |
|
|
* switches to the signal stack again, thus overwriting the data that |
59 |
|
|
* the SIGUSR2 handler put there. |
60 |
|
|
* The same problem arises when calling ExecutePPC() inside the MODE_EMUL_OP |
61 |
|
|
* interrupt handler. |
62 |
|
|
* |
63 |
|
|
* The solution is to set the signal stack to a second, "extra" stack |
64 |
|
|
* inside the SIGUSR2 handler before entering the Nanokernel or calling |
65 |
|
|
* ExecutePPC (or any function that might cause a mode switch). The signal |
66 |
|
|
* stack is restored before exiting the SIGUSR2 handler. |
67 |
|
|
* |
68 |
|
|
* TODO: |
69 |
|
|
* check if SIGSEGV handler works for all registers (including FP!) |
70 |
|
|
*/ |
71 |
|
|
|
72 |
|
|
#include <unistd.h> |
73 |
|
|
#include <fcntl.h> |
74 |
|
|
#include <time.h> |
75 |
|
|
#include <errno.h> |
76 |
|
|
#include <stdio.h> |
77 |
|
|
#include <stdlib.h> |
78 |
|
|
#include <string.h> |
79 |
|
|
#include <pthread.h> |
80 |
|
|
#include <sys/mman.h> |
81 |
|
|
#include <sys/ipc.h> |
82 |
|
|
#include <sys/shm.h> |
83 |
|
|
#include <signal.h> |
84 |
|
|
|
85 |
|
|
#include "sysdeps.h" |
86 |
|
|
#include "main.h" |
87 |
|
|
#include "version.h" |
88 |
|
|
#include "prefs.h" |
89 |
|
|
#include "prefs_editor.h" |
90 |
|
|
#include "cpu_emulation.h" |
91 |
|
|
#include "emul_op.h" |
92 |
|
|
#include "xlowmem.h" |
93 |
|
|
#include "xpram.h" |
94 |
|
|
#include "timer.h" |
95 |
|
|
#include "adb.h" |
96 |
|
|
#include "sony.h" |
97 |
|
|
#include "disk.h" |
98 |
|
|
#include "cdrom.h" |
99 |
|
|
#include "scsi.h" |
100 |
|
|
#include "video.h" |
101 |
|
|
#include "audio.h" |
102 |
|
|
#include "ether.h" |
103 |
|
|
#include "serial.h" |
104 |
|
|
#include "clip.h" |
105 |
|
|
#include "extfs.h" |
106 |
|
|
#include "sys.h" |
107 |
|
|
#include "macos_util.h" |
108 |
|
|
#include "rom_patches.h" |
109 |
|
|
#include "user_strings.h" |
110 |
gbeauche |
1.4 |
#include "vm_alloc.h" |
111 |
gbeauche |
1.5 |
#include "sigsegv.h" |
112 |
gbeauche |
1.15 |
#include "thunks.h" |
113 |
cebix |
1.1 |
|
114 |
|
|
#define DEBUG 0 |
115 |
|
|
#include "debug.h" |
116 |
|
|
|
117 |
|
|
|
118 |
|
|
#include <X11/Xlib.h> |
119 |
|
|
|
120 |
|
|
#ifdef ENABLE_GTK |
121 |
|
|
#include <gtk/gtk.h> |
122 |
|
|
#endif |
123 |
|
|
|
124 |
|
|
#ifdef ENABLE_XF86_DGA |
125 |
|
|
#include <X11/Xlib.h> |
126 |
|
|
#include <X11/Xutil.h> |
127 |
|
|
#include <X11/extensions/xf86dga.h> |
128 |
|
|
#endif |
129 |
|
|
|
130 |
|
|
#ifdef ENABLE_MON |
131 |
|
|
#include "mon.h" |
132 |
|
|
#endif |
133 |
|
|
|
134 |
|
|
|
135 |
gbeauche |
1.23 |
// Enable emulation of unaligned lmw/stmw? |
136 |
|
|
#define EMULATE_UNALIGNED_LOADSTORE_MULTIPLE 1 |
137 |
|
|
|
138 |
cebix |
1.1 |
// Enable Execute68k() safety checks? |
139 |
|
|
#define SAFE_EXEC_68K 0 |
140 |
|
|
|
141 |
|
|
// Interrupts in EMUL_OP mode? |
142 |
|
|
#define INTERRUPTS_IN_EMUL_OP_MODE 1 |
143 |
|
|
|
144 |
|
|
// Interrupts in native mode? |
145 |
|
|
#define INTERRUPTS_IN_NATIVE_MODE 1 |
146 |
|
|
|
147 |
|
|
|
148 |
|
|
// Constants |
149 |
|
|
const char ROM_FILE_NAME[] = "ROM"; |
150 |
|
|
const char ROM_FILE_NAME2[] = "Mac OS ROM"; |
151 |
|
|
|
152 |
gbeauche |
1.15 |
const uintptr RAM_BASE = 0x20000000; // Base address of RAM |
153 |
cebix |
1.1 |
const uint32 SIG_STACK_SIZE = 0x10000; // Size of signal stack |
154 |
|
|
|
155 |
|
|
|
156 |
|
|
#if !EMULATED_PPC |
157 |
|
|
// Structure in which registers are saved in a signal handler; |
158 |
|
|
// sigcontext->regs points to it |
159 |
|
|
// (see arch/ppc/kernel/signal.c) |
160 |
|
|
typedef struct { |
161 |
|
|
uint32 u[4]; |
162 |
|
|
} __attribute((aligned(16))) vector128; |
163 |
|
|
#include <linux/elf.h> |
164 |
|
|
|
165 |
|
|
struct sigregs { |
166 |
|
|
elf_gregset_t gp_regs; // Identical to pt_regs |
167 |
|
|
double fp_regs[ELF_NFPREG]; // f0..f31 and fpsrc |
168 |
|
|
//more (uninteresting) stuff following here |
169 |
|
|
}; |
170 |
|
|
#endif |
171 |
|
|
|
172 |
|
|
|
173 |
|
|
// Global variables (exported) |
174 |
|
|
#if !EMULATED_PPC |
175 |
|
|
void *TOC; // Small data pointer (r13) |
176 |
|
|
#endif |
177 |
|
|
uint32 RAMBase; // Base address of Mac RAM |
178 |
|
|
uint32 RAMSize; // Size of Mac RAM |
179 |
|
|
uint32 KernelDataAddr; // Address of Kernel Data |
180 |
|
|
uint32 BootGlobsAddr; // Address of BootGlobs structure at top of Mac RAM |
181 |
|
|
uint32 PVR; // Theoretical PVR |
182 |
|
|
int64 CPUClockSpeed; // Processor clock speed (Hz) |
183 |
|
|
int64 BusClockSpeed; // Bus clock speed (Hz) |
184 |
|
|
|
185 |
|
|
|
186 |
|
|
// Global variables |
187 |
gbeauche |
1.11 |
char *x_display_name = NULL; // X11 display name |
188 |
cebix |
1.1 |
Display *x_display = NULL; // X11 display handle |
189 |
gbeauche |
1.21 |
#ifdef X11_LOCK_TYPE |
190 |
|
|
X11_LOCK_TYPE x_display_lock = X11_LOCK_INIT; // X11 display lock |
191 |
|
|
#endif |
192 |
cebix |
1.1 |
|
193 |
|
|
static int zero_fd = 0; // FD of /dev/zero |
194 |
|
|
static bool lm_area_mapped = false; // Flag: Low Memory area mmap()ped |
195 |
|
|
static int kernel_area = -1; // SHM ID of Kernel Data area |
196 |
|
|
static bool rom_area_mapped = false; // Flag: Mac ROM mmap()ped |
197 |
|
|
static bool ram_area_mapped = false; // Flag: Mac RAM mmap()ped |
198 |
|
|
static KernelData *kernel_data; // Pointer to Kernel Data |
199 |
|
|
static EmulatorData *emulator_data; |
200 |
|
|
|
201 |
|
|
static uint8 last_xpram[XPRAM_SIZE]; // Buffer for monitoring XPRAM changes |
202 |
|
|
|
203 |
|
|
static bool nvram_thread_active = false; // Flag: NVRAM watchdog installed |
204 |
|
|
static pthread_t nvram_thread; // NVRAM watchdog |
205 |
|
|
static bool tick_thread_active = false; // Flag: MacOS thread installed |
206 |
|
|
static pthread_t tick_thread; // 60Hz thread |
207 |
|
|
static pthread_t emul_thread; // MacOS thread |
208 |
|
|
|
209 |
|
|
static bool ready_for_signals = false; // Handler installed, signals can be sent |
210 |
|
|
static int64 num_segv = 0; // Number of handled SEGV signals |
211 |
|
|
|
212 |
gbeauche |
1.6 |
static struct sigaction sigusr2_action; // Interrupt signal (of emulator thread) |
213 |
gbeauche |
1.20 |
#if EMULATED_PPC |
214 |
|
|
static uintptr sig_stack = 0; // Stack for PowerPC interrupt routine |
215 |
|
|
#else |
216 |
cebix |
1.1 |
static struct sigaction sigsegv_action; // Data access exception signal (of emulator thread) |
217 |
|
|
static struct sigaction sigill_action; // Illegal instruction signal (of emulator thread) |
218 |
|
|
static void *sig_stack = NULL; // Stack for signal handlers |
219 |
|
|
static void *extra_stack = NULL; // Stack for SIGSEGV inside interrupt handler |
220 |
|
|
static bool emul_thread_fatal = false; // Flag: MacOS thread crashed, tick thread shall dump debug output |
221 |
|
|
static sigregs sigsegv_regs; // Register dump when crashed |
222 |
gbeauche |
1.23 |
static const char *crash_reason = NULL; // Reason of the crash (SIGSEGV, SIGBUS, SIGILL) |
223 |
cebix |
1.1 |
#endif |
224 |
|
|
|
225 |
gbeauche |
1.18 |
uintptr SheepMem::zero_page = 0; // Address of ro page filled in with zeros |
226 |
gbeauche |
1.15 |
uintptr SheepMem::base = 0x60000000; // Address of SheepShaver data |
227 |
|
|
uintptr SheepMem::top = 0; // Top of SheepShaver data (stack like storage) |
228 |
|
|
|
229 |
cebix |
1.1 |
|
230 |
|
|
// Prototypes |
231 |
|
|
static void Quit(void); |
232 |
|
|
static void *emul_func(void *arg); |
233 |
|
|
static void *nvram_func(void *arg); |
234 |
|
|
static void *tick_func(void *arg); |
235 |
gbeauche |
1.8 |
#if EMULATED_PPC |
236 |
|
|
static void sigusr2_handler(int sig); |
237 |
gbeauche |
1.13 |
extern void emul_ppc(uint32 start); |
238 |
|
|
extern void init_emul_ppc(void); |
239 |
|
|
extern void exit_emul_ppc(void); |
240 |
gbeauche |
1.8 |
#else |
241 |
gbeauche |
1.6 |
static void sigusr2_handler(int sig, sigcontext_struct *sc); |
242 |
cebix |
1.1 |
static void sigsegv_handler(int sig, sigcontext_struct *sc); |
243 |
|
|
static void sigill_handler(int sig, sigcontext_struct *sc); |
244 |
|
|
#endif |
245 |
|
|
|
246 |
|
|
|
247 |
|
|
// From asm_linux.S |
248 |
gbeauche |
1.12 |
#if !EMULATED_PPC |
249 |
cebix |
1.1 |
extern "C" void *get_toc(void); |
250 |
|
|
extern "C" void *get_sp(void); |
251 |
|
|
extern "C" void flush_icache_range(void *start, void *end); |
252 |
|
|
extern "C" void jump_to_rom(uint32 entry, uint32 context); |
253 |
|
|
extern "C" void quit_emulator(void); |
254 |
|
|
extern "C" void execute_68k(uint32 pc, M68kRegisters *r); |
255 |
|
|
extern "C" void ppc_interrupt(uint32 entry, uint32 kernel_data); |
256 |
|
|
extern "C" int atomic_add(int *var, int v); |
257 |
|
|
extern "C" int atomic_and(int *var, int v); |
258 |
|
|
extern "C" int atomic_or(int *var, int v); |
259 |
|
|
extern void paranoia_check(void); |
260 |
gbeauche |
1.12 |
#endif |
261 |
|
|
|
262 |
|
|
|
263 |
|
|
#if EMULATED_PPC |
264 |
|
|
/* |
265 |
gbeauche |
1.20 |
* Return signal stack base |
266 |
|
|
*/ |
267 |
|
|
|
268 |
|
|
uintptr SignalStackBase(void) |
269 |
|
|
{ |
270 |
|
|
return sig_stack + SIG_STACK_SIZE; |
271 |
|
|
} |
272 |
|
|
|
273 |
|
|
|
274 |
|
|
/* |
275 |
gbeauche |
1.12 |
* Atomic operations |
276 |
|
|
*/ |
277 |
|
|
|
278 |
|
|
#if HAVE_SPINLOCKS |
279 |
|
|
static spinlock_t atomic_ops_lock = SPIN_LOCK_UNLOCKED; |
280 |
|
|
#else |
281 |
|
|
#define spin_lock(LOCK) |
282 |
|
|
#define spin_unlock(LOCK) |
283 |
|
|
#endif |
284 |
|
|
|
285 |
|
|
int atomic_add(int *var, int v) |
286 |
|
|
{ |
287 |
|
|
spin_lock(&atomic_ops_lock); |
288 |
|
|
int ret = *var; |
289 |
|
|
*var += v; |
290 |
|
|
spin_unlock(&atomic_ops_lock); |
291 |
|
|
return ret; |
292 |
|
|
} |
293 |
|
|
|
294 |
|
|
int atomic_and(int *var, int v) |
295 |
|
|
{ |
296 |
|
|
spin_lock(&atomic_ops_lock); |
297 |
|
|
int ret = *var; |
298 |
|
|
*var &= v; |
299 |
|
|
spin_unlock(&atomic_ops_lock); |
300 |
|
|
return ret; |
301 |
|
|
} |
302 |
|
|
|
303 |
|
|
int atomic_or(int *var, int v) |
304 |
|
|
{ |
305 |
|
|
spin_lock(&atomic_ops_lock); |
306 |
|
|
int ret = *var; |
307 |
|
|
*var |= v; |
308 |
|
|
spin_unlock(&atomic_ops_lock); |
309 |
|
|
return ret; |
310 |
|
|
} |
311 |
cebix |
1.1 |
#endif |
312 |
|
|
|
313 |
|
|
|
314 |
|
|
/* |
315 |
|
|
* Main program |
316 |
|
|
*/ |
317 |
|
|
|
318 |
|
|
static void usage(const char *prg_name) |
319 |
|
|
{ |
320 |
|
|
printf("Usage: %s [OPTION...]\n", prg_name); |
321 |
|
|
printf("\nUnix options:\n"); |
322 |
|
|
printf(" --display STRING\n X display to use\n"); |
323 |
|
|
PrefsPrintUsage(); |
324 |
|
|
exit(0); |
325 |
|
|
} |
326 |
|
|
|
327 |
|
|
int main(int argc, char **argv) |
328 |
|
|
{ |
329 |
|
|
char str[256]; |
330 |
|
|
uint32 *boot_globs; |
331 |
|
|
int16 i16; |
332 |
|
|
int rom_fd; |
333 |
|
|
FILE *proc_file; |
334 |
|
|
const char *rom_path; |
335 |
|
|
uint32 rom_size, actual; |
336 |
|
|
uint8 *rom_tmp; |
337 |
|
|
time_t now, expire; |
338 |
|
|
|
339 |
|
|
// Initialize variables |
340 |
|
|
RAMBase = 0; |
341 |
|
|
tzset(); |
342 |
|
|
|
343 |
|
|
// Print some info |
344 |
|
|
printf(GetString(STR_ABOUT_TEXT1), VERSION_MAJOR, VERSION_MINOR); |
345 |
|
|
printf(" %s\n", GetString(STR_ABOUT_TEXT2)); |
346 |
|
|
|
347 |
|
|
#if !EMULATED_PPC |
348 |
|
|
// Get TOC pointer |
349 |
|
|
TOC = get_toc(); |
350 |
|
|
#endif |
351 |
|
|
|
352 |
|
|
#ifdef ENABLE_GTK |
353 |
|
|
// Init GTK |
354 |
|
|
gtk_set_locale(); |
355 |
|
|
gtk_init(&argc, &argv); |
356 |
|
|
#endif |
357 |
|
|
|
358 |
|
|
// Read preferences |
359 |
|
|
PrefsInit(argc, argv); |
360 |
|
|
|
361 |
|
|
// Parse command line arguments |
362 |
|
|
for (int i=1; i<argc; i++) { |
363 |
|
|
if (strcmp(argv[i], "--help") == 0) { |
364 |
|
|
usage(argv[0]); |
365 |
|
|
} else if (strcmp(argv[i], "--display") == 0) { |
366 |
|
|
i++; |
367 |
|
|
if (i < argc) |
368 |
|
|
x_display_name = strdup(argv[i]); |
369 |
|
|
} else if (argv[i][0] == '-') { |
370 |
|
|
fprintf(stderr, "Unrecognized option '%s'\n", argv[i]); |
371 |
|
|
usage(argv[0]); |
372 |
|
|
} |
373 |
|
|
} |
374 |
|
|
|
375 |
|
|
// Open display |
376 |
|
|
x_display = XOpenDisplay(x_display_name); |
377 |
|
|
if (x_display == NULL) { |
378 |
|
|
char str[256]; |
379 |
|
|
sprintf(str, GetString(STR_NO_XSERVER_ERR), XDisplayName(x_display_name)); |
380 |
|
|
ErrorAlert(str); |
381 |
|
|
goto quit; |
382 |
|
|
} |
383 |
|
|
|
384 |
|
|
#if defined(ENABLE_XF86_DGA) && !defined(ENABLE_MON) |
385 |
|
|
// Fork out, so we can return from fullscreen mode when things get ugly |
386 |
|
|
XF86DGAForkApp(DefaultScreen(x_display)); |
387 |
|
|
#endif |
388 |
|
|
|
389 |
|
|
#ifdef ENABLE_MON |
390 |
|
|
// Initialize mon |
391 |
|
|
mon_init(); |
392 |
|
|
#endif |
393 |
|
|
|
394 |
|
|
// Get system info |
395 |
|
|
PVR = 0x00040000; // Default: 604 |
396 |
|
|
CPUClockSpeed = 100000000; // Default: 100MHz |
397 |
|
|
BusClockSpeed = 100000000; // Default: 100MHz |
398 |
|
|
#if !EMULATED_PPC |
399 |
|
|
proc_file = fopen("/proc/cpuinfo", "r"); |
400 |
|
|
if (proc_file) { |
401 |
|
|
char line[256]; |
402 |
|
|
while(fgets(line, 255, proc_file)) { |
403 |
|
|
// Read line |
404 |
|
|
int len = strlen(line); |
405 |
|
|
if (len == 0) |
406 |
|
|
continue; |
407 |
|
|
line[len-1] = 0; |
408 |
|
|
|
409 |
|
|
// Parse line |
410 |
|
|
int i; |
411 |
|
|
char value[256]; |
412 |
|
|
if (sscanf(line, "cpu : %s", value) == 1) { |
413 |
|
|
if (strcmp(value, "601") == 0) |
414 |
|
|
PVR = 0x00010000; |
415 |
|
|
else if (strcmp(value, "603") == 0) |
416 |
|
|
PVR = 0x00030000; |
417 |
|
|
else if (strcmp(value, "604") == 0) |
418 |
|
|
PVR = 0x00040000; |
419 |
|
|
else if (strcmp(value, "603e") == 0) |
420 |
|
|
PVR = 0x00060000; |
421 |
|
|
else if (strcmp(value, "603ev") == 0) |
422 |
|
|
PVR = 0x00070000; |
423 |
|
|
else if (strcmp(value, "604e") == 0) |
424 |
|
|
PVR = 0x00090000; |
425 |
|
|
else if (strcmp(value, "604ev5") == 0) |
426 |
|
|
PVR = 0x000a0000; |
427 |
|
|
else if (strcmp(value, "750") == 0) |
428 |
|
|
PVR = 0x00080000; |
429 |
|
|
else if (strcmp(value, "821") == 0) |
430 |
|
|
PVR = 0x00320000; |
431 |
|
|
else if (strcmp(value, "860") == 0) |
432 |
|
|
PVR = 0x00500000; |
433 |
|
|
else |
434 |
|
|
printf("WARNING: Unknown CPU type '%s', assuming 604\n", value); |
435 |
|
|
} |
436 |
|
|
if (sscanf(line, "clock : %dMHz", &i) == 1) |
437 |
|
|
CPUClockSpeed = BusClockSpeed = i * 1000000; |
438 |
|
|
} |
439 |
|
|
fclose(proc_file); |
440 |
|
|
} else { |
441 |
|
|
sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno)); |
442 |
|
|
WarningAlert(str); |
443 |
|
|
} |
444 |
|
|
#endif |
445 |
|
|
D(bug("PVR: %08x (assumed)\n", PVR)); |
446 |
|
|
|
447 |
|
|
// Init system routines |
448 |
|
|
SysInit(); |
449 |
|
|
|
450 |
|
|
// Show preferences editor |
451 |
|
|
if (!PrefsFindBool("nogui")) |
452 |
|
|
if (!PrefsEditor()) |
453 |
|
|
goto quit; |
454 |
|
|
|
455 |
|
|
#if !EMULATED_PPC |
456 |
|
|
// Check some things |
457 |
|
|
paranoia_check(); |
458 |
|
|
#endif |
459 |
|
|
|
460 |
|
|
// Open /dev/zero |
461 |
|
|
zero_fd = open("/dev/zero", O_RDWR); |
462 |
|
|
if (zero_fd < 0) { |
463 |
|
|
sprintf(str, GetString(STR_NO_DEV_ZERO_ERR), strerror(errno)); |
464 |
|
|
ErrorAlert(str); |
465 |
|
|
goto quit; |
466 |
|
|
} |
467 |
|
|
|
468 |
|
|
// Create Low Memory area (0x0000..0x3000) |
469 |
gbeauche |
1.4 |
if (vm_acquire_fixed((char *)0, 0x3000) < 0) { |
470 |
cebix |
1.1 |
sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno)); |
471 |
|
|
ErrorAlert(str); |
472 |
|
|
goto quit; |
473 |
|
|
} |
474 |
|
|
lm_area_mapped = true; |
475 |
|
|
|
476 |
|
|
// Create areas for Kernel Data |
477 |
|
|
kernel_area = shmget(IPC_PRIVATE, KERNEL_AREA_SIZE, 0600); |
478 |
|
|
if (kernel_area == -1) { |
479 |
|
|
sprintf(str, GetString(STR_KD_SHMGET_ERR), strerror(errno)); |
480 |
|
|
ErrorAlert(str); |
481 |
|
|
goto quit; |
482 |
|
|
} |
483 |
|
|
if (shmat(kernel_area, (void *)KERNEL_DATA_BASE, 0) < 0) { |
484 |
|
|
sprintf(str, GetString(STR_KD_SHMAT_ERR), strerror(errno)); |
485 |
|
|
ErrorAlert(str); |
486 |
|
|
goto quit; |
487 |
|
|
} |
488 |
|
|
if (shmat(kernel_area, (void *)KERNEL_DATA2_BASE, 0) < 0) { |
489 |
|
|
sprintf(str, GetString(STR_KD2_SHMAT_ERR), strerror(errno)); |
490 |
|
|
ErrorAlert(str); |
491 |
|
|
goto quit; |
492 |
|
|
} |
493 |
gbeauche |
1.15 |
kernel_data = (KernelData *)KERNEL_DATA_BASE; |
494 |
cebix |
1.1 |
emulator_data = &kernel_data->ed; |
495 |
gbeauche |
1.15 |
KernelDataAddr = KERNEL_DATA_BASE; |
496 |
cebix |
1.1 |
D(bug("Kernel Data at %p, Emulator Data at %p\n", kernel_data, emulator_data)); |
497 |
|
|
|
498 |
gbeauche |
1.8 |
// Create area for SheepShaver data |
499 |
gbeauche |
1.15 |
if (!SheepMem::Init()) { |
500 |
gbeauche |
1.8 |
sprintf(str, GetString(STR_SHEEP_MEM_MMAP_ERR), strerror(errno)); |
501 |
|
|
ErrorAlert(str); |
502 |
|
|
goto quit; |
503 |
|
|
} |
504 |
|
|
|
505 |
cebix |
1.1 |
// Create area for Mac ROM |
506 |
gbeauche |
1.4 |
if (vm_acquire_fixed((char *)ROM_BASE, ROM_AREA_SIZE) < 0) { |
507 |
cebix |
1.1 |
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
508 |
|
|
ErrorAlert(str); |
509 |
|
|
goto quit; |
510 |
|
|
} |
511 |
gbeauche |
1.6 |
#if !EMULATED_PPC || defined(__powerpc__) |
512 |
gbeauche |
1.4 |
if (vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
513 |
|
|
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
514 |
|
|
ErrorAlert(str); |
515 |
|
|
goto quit; |
516 |
|
|
} |
517 |
|
|
#endif |
518 |
cebix |
1.1 |
rom_area_mapped = true; |
519 |
|
|
D(bug("ROM area at %08x\n", ROM_BASE)); |
520 |
|
|
|
521 |
|
|
// Create area for Mac RAM |
522 |
|
|
RAMSize = PrefsFindInt32("ramsize"); |
523 |
|
|
if (RAMSize < 8*1024*1024) { |
524 |
|
|
WarningAlert(GetString(STR_SMALL_RAM_WARN)); |
525 |
|
|
RAMSize = 8*1024*1024; |
526 |
|
|
} |
527 |
|
|
|
528 |
gbeauche |
1.8 |
if (vm_acquire_fixed((char *)RAM_BASE, RAMSize) < 0) { |
529 |
cebix |
1.1 |
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
530 |
|
|
ErrorAlert(str); |
531 |
|
|
goto quit; |
532 |
|
|
} |
533 |
gbeauche |
1.4 |
#if !EMULATED_PPC |
534 |
gbeauche |
1.8 |
if (vm_protect((char *)RAM_BASE, RAMSize, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
535 |
gbeauche |
1.4 |
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
536 |
|
|
ErrorAlert(str); |
537 |
|
|
goto quit; |
538 |
|
|
} |
539 |
|
|
#endif |
540 |
gbeauche |
1.8 |
RAMBase = RAM_BASE; |
541 |
cebix |
1.1 |
ram_area_mapped = true; |
542 |
|
|
D(bug("RAM area at %08x\n", RAMBase)); |
543 |
|
|
|
544 |
|
|
if (RAMBase > ROM_BASE) { |
545 |
|
|
ErrorAlert(GetString(STR_RAM_HIGHER_THAN_ROM_ERR)); |
546 |
|
|
goto quit; |
547 |
|
|
} |
548 |
|
|
|
549 |
|
|
// Load Mac ROM |
550 |
|
|
rom_path = PrefsFindString("rom"); |
551 |
|
|
rom_fd = open(rom_path ? rom_path : ROM_FILE_NAME, O_RDONLY); |
552 |
|
|
if (rom_fd < 0) { |
553 |
|
|
rom_fd = open(rom_path ? rom_path : ROM_FILE_NAME2, O_RDONLY); |
554 |
|
|
if (rom_fd < 0) { |
555 |
|
|
ErrorAlert(GetString(STR_NO_ROM_FILE_ERR)); |
556 |
|
|
goto quit; |
557 |
|
|
} |
558 |
|
|
} |
559 |
|
|
printf(GetString(STR_READING_ROM_FILE)); |
560 |
|
|
rom_size = lseek(rom_fd, 0, SEEK_END); |
561 |
|
|
lseek(rom_fd, 0, SEEK_SET); |
562 |
|
|
rom_tmp = new uint8[ROM_SIZE]; |
563 |
|
|
actual = read(rom_fd, (void *)rom_tmp, ROM_SIZE); |
564 |
|
|
close(rom_fd); |
565 |
gbeauche |
1.3 |
|
566 |
|
|
// Decode Mac ROM |
567 |
|
|
if (!DecodeROM(rom_tmp, actual)) { |
568 |
|
|
if (rom_size != 4*1024*1024) { |
569 |
cebix |
1.1 |
ErrorAlert(GetString(STR_ROM_SIZE_ERR)); |
570 |
|
|
goto quit; |
571 |
|
|
} else { |
572 |
|
|
ErrorAlert(GetString(STR_ROM_FILE_READ_ERR)); |
573 |
|
|
goto quit; |
574 |
|
|
} |
575 |
|
|
} |
576 |
gbeauche |
1.3 |
delete[] rom_tmp; |
577 |
cebix |
1.1 |
|
578 |
|
|
// Load NVRAM |
579 |
|
|
XPRAMInit(); |
580 |
|
|
|
581 |
|
|
// Set boot volume |
582 |
cebix |
1.10 |
i16 = PrefsFindInt32("bootdrive"); |
583 |
cebix |
1.1 |
XPRAM[0x1378] = i16 >> 8; |
584 |
|
|
XPRAM[0x1379] = i16 & 0xff; |
585 |
cebix |
1.10 |
i16 = PrefsFindInt32("bootdriver"); |
586 |
cebix |
1.1 |
XPRAM[0x137a] = i16 >> 8; |
587 |
|
|
XPRAM[0x137b] = i16 & 0xff; |
588 |
|
|
|
589 |
|
|
// Create BootGlobs at top of Mac memory |
590 |
|
|
memset((void *)(RAMBase + RAMSize - 4096), 0, 4096); |
591 |
|
|
BootGlobsAddr = RAMBase + RAMSize - 0x1c; |
592 |
|
|
boot_globs = (uint32 *)BootGlobsAddr; |
593 |
|
|
boot_globs[-5] = htonl(RAMBase + RAMSize); // MemTop |
594 |
|
|
boot_globs[0] = htonl(RAMBase); // First RAM bank |
595 |
|
|
boot_globs[1] = htonl(RAMSize); |
596 |
|
|
boot_globs[2] = htonl((uint32)-1); // End of bank table |
597 |
|
|
|
598 |
gbeauche |
1.15 |
// Init thunks |
599 |
|
|
if (!ThunksInit()) |
600 |
|
|
goto quit; |
601 |
|
|
|
602 |
cebix |
1.1 |
// Init drivers |
603 |
|
|
SonyInit(); |
604 |
|
|
DiskInit(); |
605 |
|
|
CDROMInit(); |
606 |
|
|
SCSIInit(); |
607 |
|
|
|
608 |
|
|
// Init external file system |
609 |
|
|
ExtFSInit(); |
610 |
|
|
|
611 |
gbeauche |
1.22 |
// Init ADB |
612 |
|
|
ADBInit(); |
613 |
|
|
|
614 |
cebix |
1.1 |
// Init audio |
615 |
|
|
AudioInit(); |
616 |
|
|
|
617 |
|
|
// Init network |
618 |
|
|
EtherInit(); |
619 |
|
|
|
620 |
|
|
// Init serial ports |
621 |
|
|
SerialInit(); |
622 |
|
|
|
623 |
|
|
// Init Time Manager |
624 |
|
|
TimerInit(); |
625 |
|
|
|
626 |
|
|
// Init clipboard |
627 |
|
|
ClipInit(); |
628 |
|
|
|
629 |
|
|
// Init video |
630 |
|
|
if (!VideoInit()) |
631 |
|
|
goto quit; |
632 |
|
|
|
633 |
|
|
// Install ROM patches |
634 |
|
|
if (!PatchROM()) { |
635 |
|
|
ErrorAlert(GetString(STR_UNSUPPORTED_ROM_TYPE_ERR)); |
636 |
|
|
goto quit; |
637 |
|
|
} |
638 |
|
|
|
639 |
|
|
// Clear caches (as we loaded and patched code) and write protect ROM |
640 |
|
|
#if !EMULATED_PPC |
641 |
|
|
MakeExecutable(0, (void *)ROM_BASE, ROM_AREA_SIZE); |
642 |
|
|
#endif |
643 |
gbeauche |
1.4 |
vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_EXECUTE); |
644 |
cebix |
1.1 |
|
645 |
|
|
// Initialize Kernel Data |
646 |
|
|
memset(kernel_data, 0, sizeof(KernelData)); |
647 |
|
|
if (ROMType == ROMTYPE_NEWWORLD) { |
648 |
gbeauche |
1.15 |
uintptr of_dev_tree = SheepMem::Reserve(4 * sizeof(uint32)); |
649 |
|
|
memset((void *)of_dev_tree, 0, 4 * sizeof(uint32)); |
650 |
|
|
uintptr vector_lookup_tbl = SheepMem::Reserve(128); |
651 |
|
|
uintptr vector_mask_tbl = SheepMem::Reserve(64); |
652 |
cebix |
1.1 |
memset((uint8 *)kernel_data + 0xb80, 0x3d, 0x80); |
653 |
gbeauche |
1.15 |
memset((void *)vector_lookup_tbl, 0, 128); |
654 |
|
|
memset((void *)vector_mask_tbl, 0, 64); |
655 |
cebix |
1.1 |
kernel_data->v[0xb80 >> 2] = htonl(ROM_BASE); |
656 |
gbeauche |
1.15 |
kernel_data->v[0xb84 >> 2] = htonl(of_dev_tree); // OF device tree base |
657 |
|
|
kernel_data->v[0xb90 >> 2] = htonl(vector_lookup_tbl); |
658 |
|
|
kernel_data->v[0xb94 >> 2] = htonl(vector_mask_tbl); |
659 |
cebix |
1.1 |
kernel_data->v[0xb98 >> 2] = htonl(ROM_BASE); // OpenPIC base |
660 |
|
|
kernel_data->v[0xbb0 >> 2] = htonl(0); // ADB base |
661 |
|
|
kernel_data->v[0xc20 >> 2] = htonl(RAMSize); |
662 |
|
|
kernel_data->v[0xc24 >> 2] = htonl(RAMSize); |
663 |
|
|
kernel_data->v[0xc30 >> 2] = htonl(RAMSize); |
664 |
|
|
kernel_data->v[0xc34 >> 2] = htonl(RAMSize); |
665 |
|
|
kernel_data->v[0xc38 >> 2] = htonl(0x00010020); |
666 |
|
|
kernel_data->v[0xc3c >> 2] = htonl(0x00200001); |
667 |
|
|
kernel_data->v[0xc40 >> 2] = htonl(0x00010000); |
668 |
|
|
kernel_data->v[0xc50 >> 2] = htonl(RAMBase); |
669 |
|
|
kernel_data->v[0xc54 >> 2] = htonl(RAMSize); |
670 |
|
|
kernel_data->v[0xf60 >> 2] = htonl(PVR); |
671 |
|
|
kernel_data->v[0xf64 >> 2] = htonl(CPUClockSpeed); |
672 |
|
|
kernel_data->v[0xf68 >> 2] = htonl(BusClockSpeed); |
673 |
|
|
kernel_data->v[0xf6c >> 2] = htonl(CPUClockSpeed); |
674 |
|
|
} else { |
675 |
|
|
kernel_data->v[0xc80 >> 2] = htonl(RAMSize); |
676 |
|
|
kernel_data->v[0xc84 >> 2] = htonl(RAMSize); |
677 |
|
|
kernel_data->v[0xc90 >> 2] = htonl(RAMSize); |
678 |
|
|
kernel_data->v[0xc94 >> 2] = htonl(RAMSize); |
679 |
|
|
kernel_data->v[0xc98 >> 2] = htonl(0x00010020); |
680 |
|
|
kernel_data->v[0xc9c >> 2] = htonl(0x00200001); |
681 |
|
|
kernel_data->v[0xca0 >> 2] = htonl(0x00010000); |
682 |
|
|
kernel_data->v[0xcb0 >> 2] = htonl(RAMBase); |
683 |
|
|
kernel_data->v[0xcb4 >> 2] = htonl(RAMSize); |
684 |
|
|
kernel_data->v[0xf80 >> 2] = htonl(PVR); |
685 |
|
|
kernel_data->v[0xf84 >> 2] = htonl(CPUClockSpeed); |
686 |
|
|
kernel_data->v[0xf88 >> 2] = htonl(BusClockSpeed); |
687 |
|
|
kernel_data->v[0xf8c >> 2] = htonl(CPUClockSpeed); |
688 |
|
|
} |
689 |
|
|
|
690 |
|
|
// Initialize extra low memory |
691 |
|
|
D(bug("Initializing Low Memory...\n")); |
692 |
|
|
memset(NULL, 0, 0x3000); |
693 |
|
|
WriteMacInt32(XLM_SIGNATURE, FOURCC('B','a','a','h')); // Signature to detect SheepShaver |
694 |
gbeauche |
1.15 |
WriteMacInt32(XLM_KERNEL_DATA, KernelDataAddr); // For trap replacement routines |
695 |
cebix |
1.1 |
WriteMacInt32(XLM_PVR, PVR); // Theoretical PVR |
696 |
|
|
WriteMacInt32(XLM_BUS_CLOCK, BusClockSpeed); // For DriverServicesLib patch |
697 |
|
|
WriteMacInt16(XLM_EXEC_RETURN_OPCODE, M68K_EXEC_RETURN); // For Execute68k() (RTS from the executed 68k code will jump here and end 68k mode) |
698 |
gbeauche |
1.18 |
WriteMacInt32(XLM_ZERO_PAGE, SheepMem::ZeroPage()); // Pointer to read-only page with all bits set to 0 |
699 |
gbeauche |
1.17 |
#if !EMULATED_PPC |
700 |
|
|
WriteMacInt32(XLM_TOC, (uint32)TOC); // TOC pointer of emulator |
701 |
|
|
#endif |
702 |
|
|
WriteMacInt32(XLM_ETHER_INIT, NativeFunction(NATIVE_ETHER_INIT)); // DLPI ethernet driver functions |
703 |
gbeauche |
1.15 |
WriteMacInt32(XLM_ETHER_TERM, NativeFunction(NATIVE_ETHER_TERM)); |
704 |
|
|
WriteMacInt32(XLM_ETHER_OPEN, NativeFunction(NATIVE_ETHER_OPEN)); |
705 |
|
|
WriteMacInt32(XLM_ETHER_CLOSE, NativeFunction(NATIVE_ETHER_CLOSE)); |
706 |
|
|
WriteMacInt32(XLM_ETHER_WPUT, NativeFunction(NATIVE_ETHER_WPUT)); |
707 |
|
|
WriteMacInt32(XLM_ETHER_RSRV, NativeFunction(NATIVE_ETHER_RSRV)); |
708 |
|
|
WriteMacInt32(XLM_VIDEO_DOIO, NativeFunction(NATIVE_VIDEO_DO_DRIVER_IO)); |
709 |
cebix |
1.1 |
D(bug("Low Memory initialized\n")); |
710 |
|
|
|
711 |
|
|
// Start 60Hz thread |
712 |
|
|
tick_thread_active = (pthread_create(&tick_thread, NULL, tick_func, NULL) == 0); |
713 |
|
|
D(bug("Tick thread installed (%ld)\n", tick_thread)); |
714 |
|
|
|
715 |
|
|
// Start NVRAM watchdog thread |
716 |
|
|
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
717 |
|
|
nvram_thread_active = (pthread_create(&nvram_thread, NULL, nvram_func, NULL) == 0); |
718 |
|
|
D(bug("NVRAM thread installed (%ld)\n", nvram_thread)); |
719 |
|
|
|
720 |
|
|
#if !EMULATED_PPC |
721 |
|
|
// Create and install stacks for signal handlers |
722 |
|
|
sig_stack = malloc(SIG_STACK_SIZE); |
723 |
|
|
D(bug("Signal stack at %p\n", sig_stack)); |
724 |
|
|
if (sig_stack == NULL) { |
725 |
|
|
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
726 |
|
|
goto quit; |
727 |
|
|
} |
728 |
|
|
extra_stack = malloc(SIG_STACK_SIZE); |
729 |
|
|
D(bug("Extra stack at %p\n", extra_stack)); |
730 |
|
|
if (extra_stack == NULL) { |
731 |
|
|
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
732 |
|
|
goto quit; |
733 |
|
|
} |
734 |
|
|
struct sigaltstack new_stack; |
735 |
|
|
new_stack.ss_sp = sig_stack; |
736 |
|
|
new_stack.ss_flags = 0; |
737 |
|
|
new_stack.ss_size = SIG_STACK_SIZE; |
738 |
|
|
if (sigaltstack(&new_stack, NULL) < 0) { |
739 |
|
|
sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno)); |
740 |
|
|
ErrorAlert(str); |
741 |
|
|
goto quit; |
742 |
|
|
} |
743 |
|
|
#endif |
744 |
|
|
|
745 |
|
|
#if !EMULATED_PPC |
746 |
gbeauche |
1.23 |
// Install SIGSEGV and SIGBUS handlers |
747 |
cebix |
1.1 |
sigemptyset(&sigsegv_action.sa_mask); // Block interrupts during SEGV handling |
748 |
|
|
sigaddset(&sigsegv_action.sa_mask, SIGUSR2); |
749 |
|
|
sigsegv_action.sa_handler = (__sighandler_t)sigsegv_handler; |
750 |
|
|
sigsegv_action.sa_flags = SA_ONSTACK; |
751 |
|
|
sigsegv_action.sa_restorer = NULL; |
752 |
|
|
if (sigaction(SIGSEGV, &sigsegv_action, NULL) < 0) { |
753 |
|
|
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
754 |
|
|
ErrorAlert(str); |
755 |
|
|
goto quit; |
756 |
|
|
} |
757 |
gbeauche |
1.23 |
if (sigaction(SIGBUS, &sigsegv_action, NULL) < 0) { |
758 |
|
|
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
759 |
|
|
ErrorAlert(str); |
760 |
|
|
goto quit; |
761 |
|
|
} |
762 |
cebix |
1.1 |
|
763 |
|
|
// Install SIGILL handler |
764 |
|
|
sigemptyset(&sigill_action.sa_mask); // Block interrupts during ILL handling |
765 |
|
|
sigaddset(&sigill_action.sa_mask, SIGUSR2); |
766 |
|
|
sigill_action.sa_handler = (__sighandler_t)sigill_handler; |
767 |
|
|
sigill_action.sa_flags = SA_ONSTACK; |
768 |
|
|
sigill_action.sa_restorer = NULL; |
769 |
|
|
if (sigaction(SIGILL, &sigill_action, NULL) < 0) { |
770 |
|
|
sprintf(str, GetString(STR_SIGILL_INSTALL_ERR), strerror(errno)); |
771 |
|
|
ErrorAlert(str); |
772 |
|
|
goto quit; |
773 |
|
|
} |
774 |
gbeauche |
1.6 |
#endif |
775 |
cebix |
1.1 |
|
776 |
|
|
// Install interrupt signal handler |
777 |
|
|
sigemptyset(&sigusr2_action.sa_mask); |
778 |
|
|
sigusr2_action.sa_handler = (__sighandler_t)sigusr2_handler; |
779 |
gbeauche |
1.8 |
sigusr2_action.sa_flags = 0; |
780 |
|
|
#if !EMULATED_PPC |
781 |
cebix |
1.1 |
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART; |
782 |
gbeauche |
1.8 |
#endif |
783 |
cebix |
1.1 |
sigusr2_action.sa_restorer = NULL; |
784 |
|
|
if (sigaction(SIGUSR2, &sigusr2_action, NULL) < 0) { |
785 |
|
|
sprintf(str, GetString(STR_SIGUSR2_INSTALL_ERR), strerror(errno)); |
786 |
|
|
ErrorAlert(str); |
787 |
|
|
goto quit; |
788 |
|
|
} |
789 |
|
|
|
790 |
|
|
// Get my thread ID and execute MacOS thread function |
791 |
|
|
emul_thread = pthread_self(); |
792 |
|
|
D(bug("MacOS thread is %ld\n", emul_thread)); |
793 |
|
|
emul_func(NULL); |
794 |
|
|
|
795 |
|
|
quit: |
796 |
|
|
Quit(); |
797 |
|
|
return 0; |
798 |
|
|
} |
799 |
|
|
|
800 |
|
|
|
801 |
|
|
/* |
802 |
|
|
* Cleanup and quit |
803 |
|
|
*/ |
804 |
|
|
|
805 |
|
|
static void Quit(void) |
806 |
|
|
{ |
807 |
gbeauche |
1.13 |
#if EMULATED_PPC |
808 |
|
|
// Exit PowerPC emulation |
809 |
|
|
exit_emul_ppc(); |
810 |
|
|
#endif |
811 |
|
|
|
812 |
cebix |
1.1 |
// Stop 60Hz thread |
813 |
|
|
if (tick_thread_active) { |
814 |
|
|
pthread_cancel(tick_thread); |
815 |
|
|
pthread_join(tick_thread, NULL); |
816 |
|
|
} |
817 |
|
|
|
818 |
|
|
// Stop NVRAM watchdog thread |
819 |
|
|
if (nvram_thread_active) { |
820 |
|
|
pthread_cancel(nvram_thread); |
821 |
|
|
pthread_join(nvram_thread, NULL); |
822 |
|
|
} |
823 |
|
|
|
824 |
|
|
#if !EMULATED_PPC |
825 |
gbeauche |
1.23 |
// Uninstall SIGSEGV and SIGBUS handlers |
826 |
cebix |
1.1 |
sigemptyset(&sigsegv_action.sa_mask); |
827 |
|
|
sigsegv_action.sa_handler = SIG_DFL; |
828 |
|
|
sigsegv_action.sa_flags = 0; |
829 |
|
|
sigaction(SIGSEGV, &sigsegv_action, NULL); |
830 |
gbeauche |
1.23 |
sigaction(SIGBUS, &sigsegv_action, NULL); |
831 |
cebix |
1.1 |
|
832 |
|
|
// Uninstall SIGILL handler |
833 |
|
|
sigemptyset(&sigill_action.sa_mask); |
834 |
|
|
sigill_action.sa_handler = SIG_DFL; |
835 |
|
|
sigill_action.sa_flags = 0; |
836 |
|
|
sigaction(SIGILL, &sigill_action, NULL); |
837 |
|
|
#endif |
838 |
|
|
|
839 |
|
|
// Save NVRAM |
840 |
|
|
XPRAMExit(); |
841 |
|
|
|
842 |
|
|
// Exit clipboard |
843 |
|
|
ClipExit(); |
844 |
|
|
|
845 |
|
|
// Exit Time Manager |
846 |
|
|
TimerExit(); |
847 |
|
|
|
848 |
|
|
// Exit serial |
849 |
|
|
SerialExit(); |
850 |
|
|
|
851 |
|
|
// Exit network |
852 |
|
|
EtherExit(); |
853 |
|
|
|
854 |
|
|
// Exit audio |
855 |
|
|
AudioExit(); |
856 |
gbeauche |
1.22 |
|
857 |
|
|
// Exit ADB |
858 |
|
|
ADBExit(); |
859 |
cebix |
1.1 |
|
860 |
|
|
// Exit video |
861 |
|
|
VideoExit(); |
862 |
|
|
|
863 |
|
|
// Exit external file system |
864 |
|
|
ExtFSExit(); |
865 |
|
|
|
866 |
|
|
// Exit drivers |
867 |
|
|
SCSIExit(); |
868 |
|
|
CDROMExit(); |
869 |
|
|
DiskExit(); |
870 |
|
|
SonyExit(); |
871 |
|
|
|
872 |
gbeauche |
1.24 |
// Delete thunks |
873 |
|
|
ThunksExit(); |
874 |
|
|
|
875 |
gbeauche |
1.15 |
// Delete SheepShaver globals |
876 |
|
|
SheepMem::Exit(); |
877 |
|
|
|
878 |
cebix |
1.1 |
// Delete RAM area |
879 |
|
|
if (ram_area_mapped) |
880 |
gbeauche |
1.8 |
vm_release((char *)RAM_BASE, RAMSize); |
881 |
cebix |
1.1 |
|
882 |
|
|
// Delete ROM area |
883 |
|
|
if (rom_area_mapped) |
884 |
gbeauche |
1.4 |
vm_release((char *)ROM_BASE, ROM_AREA_SIZE); |
885 |
cebix |
1.1 |
|
886 |
|
|
// Delete Kernel Data area |
887 |
|
|
if (kernel_area >= 0) { |
888 |
|
|
shmdt((void *)KERNEL_DATA_BASE); |
889 |
|
|
shmdt((void *)KERNEL_DATA2_BASE); |
890 |
|
|
shmctl(kernel_area, IPC_RMID, NULL); |
891 |
|
|
} |
892 |
|
|
|
893 |
|
|
// Delete Low Memory area |
894 |
|
|
if (lm_area_mapped) |
895 |
|
|
munmap((char *)0x0000, 0x3000); |
896 |
|
|
|
897 |
|
|
// Close /dev/zero |
898 |
|
|
if (zero_fd > 0) |
899 |
|
|
close(zero_fd); |
900 |
|
|
|
901 |
|
|
// Exit system routines |
902 |
|
|
SysExit(); |
903 |
|
|
|
904 |
|
|
// Exit preferences |
905 |
|
|
PrefsExit(); |
906 |
|
|
|
907 |
|
|
#ifdef ENABLE_MON |
908 |
|
|
// Exit mon |
909 |
|
|
mon_exit(); |
910 |
|
|
#endif |
911 |
|
|
|
912 |
|
|
// Close X11 server connection |
913 |
|
|
if (x_display) |
914 |
|
|
XCloseDisplay(x_display); |
915 |
|
|
|
916 |
|
|
exit(0); |
917 |
|
|
} |
918 |
|
|
|
919 |
|
|
|
920 |
|
|
/* |
921 |
|
|
* Jump into Mac ROM, start 680x0 emulator |
922 |
|
|
*/ |
923 |
|
|
|
924 |
|
|
#if EMULATED_PPC |
925 |
|
|
void jump_to_rom(uint32 entry) |
926 |
|
|
{ |
927 |
|
|
init_emul_ppc(); |
928 |
|
|
emul_ppc(entry); |
929 |
|
|
} |
930 |
|
|
#endif |
931 |
|
|
|
932 |
|
|
|
933 |
|
|
/* |
934 |
|
|
* Emulator thread function |
935 |
|
|
*/ |
936 |
|
|
|
937 |
|
|
static void *emul_func(void *arg) |
938 |
|
|
{ |
939 |
|
|
// We're now ready to receive signals |
940 |
|
|
ready_for_signals = true; |
941 |
|
|
|
942 |
|
|
// Decrease priority, so more time-critical things like audio will work better |
943 |
|
|
nice(1); |
944 |
|
|
|
945 |
|
|
// Jump to ROM boot routine |
946 |
|
|
D(bug("Jumping to ROM\n")); |
947 |
|
|
#if EMULATED_PPC |
948 |
|
|
jump_to_rom(ROM_BASE + 0x310000); |
949 |
|
|
#else |
950 |
|
|
jump_to_rom(ROM_BASE + 0x310000, (uint32)emulator_data); |
951 |
|
|
#endif |
952 |
|
|
D(bug("Returned from ROM\n")); |
953 |
|
|
|
954 |
|
|
// We're no longer ready to receive signals |
955 |
|
|
ready_for_signals = false; |
956 |
|
|
return NULL; |
957 |
|
|
} |
958 |
|
|
|
959 |
|
|
|
960 |
|
|
#if !EMULATED_PPC |
961 |
|
|
/* |
962 |
|
|
* Execute 68k subroutine (must be ended with RTS) |
963 |
|
|
* This must only be called by the emul_thread when in EMUL_OP mode |
964 |
|
|
* r->a[7] is unused, the routine runs on the caller's stack |
965 |
|
|
*/ |
966 |
|
|
|
967 |
|
|
void Execute68k(uint32 pc, M68kRegisters *r) |
968 |
|
|
{ |
969 |
|
|
#if SAFE_EXEC_68K |
970 |
|
|
if (ReadMacInt32(XLM_RUN_MODE) != MODE_EMUL_OP) |
971 |
|
|
printf("FATAL: Execute68k() not called from EMUL_OP mode\n"); |
972 |
|
|
if (!pthread_equal(pthread_self(), emul_thread)) |
973 |
|
|
printf("FATAL: Execute68k() not called from emul_thread\n"); |
974 |
|
|
#endif |
975 |
|
|
execute_68k(pc, r); |
976 |
|
|
} |
977 |
|
|
|
978 |
|
|
|
979 |
|
|
/* |
980 |
|
|
* Execute 68k A-Trap from EMUL_OP routine |
981 |
|
|
* r->a[7] is unused, the routine runs on the caller's stack |
982 |
|
|
*/ |
983 |
|
|
|
984 |
|
|
void Execute68kTrap(uint16 trap, M68kRegisters *r) |
985 |
|
|
{ |
986 |
|
|
uint16 proc[2] = {trap, M68K_RTS}; |
987 |
|
|
Execute68k((uint32)proc, r); |
988 |
|
|
} |
989 |
gbeauche |
1.7 |
#endif |
990 |
cebix |
1.1 |
|
991 |
|
|
|
992 |
|
|
/* |
993 |
|
|
* Quit emulator (cause return from jump_to_rom) |
994 |
|
|
*/ |
995 |
|
|
|
996 |
|
|
void QuitEmulator(void) |
997 |
|
|
{ |
998 |
|
|
#if EMULATED_PPC |
999 |
|
|
Quit(); |
1000 |
|
|
#else |
1001 |
|
|
quit_emulator(); |
1002 |
|
|
#endif |
1003 |
|
|
} |
1004 |
|
|
|
1005 |
|
|
|
1006 |
|
|
/* |
1007 |
|
|
* Pause/resume emulator |
1008 |
|
|
*/ |
1009 |
|
|
|
1010 |
|
|
void PauseEmulator(void) |
1011 |
|
|
{ |
1012 |
|
|
pthread_kill(emul_thread, SIGSTOP); |
1013 |
|
|
} |
1014 |
|
|
|
1015 |
|
|
void ResumeEmulator(void) |
1016 |
|
|
{ |
1017 |
|
|
pthread_kill(emul_thread, SIGCONT); |
1018 |
|
|
} |
1019 |
|
|
|
1020 |
|
|
|
1021 |
|
|
/* |
1022 |
|
|
* Dump 68k registers |
1023 |
|
|
*/ |
1024 |
|
|
|
1025 |
|
|
void Dump68kRegs(M68kRegisters *r) |
1026 |
|
|
{ |
1027 |
|
|
// Display 68k registers |
1028 |
|
|
for (int i=0; i<8; i++) { |
1029 |
|
|
printf("d%d: %08x", i, r->d[i]); |
1030 |
|
|
if (i == 3 || i == 7) |
1031 |
|
|
printf("\n"); |
1032 |
|
|
else |
1033 |
|
|
printf(", "); |
1034 |
|
|
} |
1035 |
|
|
for (int i=0; i<8; i++) { |
1036 |
|
|
printf("a%d: %08x", i, r->a[i]); |
1037 |
|
|
if (i == 3 || i == 7) |
1038 |
|
|
printf("\n"); |
1039 |
|
|
else |
1040 |
|
|
printf(", "); |
1041 |
|
|
} |
1042 |
|
|
} |
1043 |
|
|
|
1044 |
|
|
|
1045 |
|
|
/* |
1046 |
|
|
* Make code executable |
1047 |
|
|
*/ |
1048 |
|
|
|
1049 |
|
|
void MakeExecutable(int dummy, void *start, uint32 length) |
1050 |
|
|
{ |
1051 |
gbeauche |
1.9 |
if (((uintptr)start >= ROM_BASE) && ((uintptr)start < (ROM_BASE + ROM_SIZE))) |
1052 |
cebix |
1.1 |
return; |
1053 |
gbeauche |
1.9 |
#if EMULATED_PPC |
1054 |
|
|
FlushCodeCache((uintptr)start, (uintptr)start + length); |
1055 |
|
|
#else |
1056 |
|
|
flush_icache_range(start, (void *)((uintptr)start + length)); |
1057 |
cebix |
1.1 |
#endif |
1058 |
|
|
} |
1059 |
|
|
|
1060 |
|
|
|
1061 |
|
|
/* |
1062 |
|
|
* Patch things after system startup (gets called by disk driver accRun routine) |
1063 |
|
|
*/ |
1064 |
|
|
|
1065 |
|
|
void PatchAfterStartup(void) |
1066 |
|
|
{ |
1067 |
gbeauche |
1.6 |
ExecuteNative(NATIVE_VIDEO_INSTALL_ACCEL); |
1068 |
cebix |
1.1 |
InstallExtFS(); |
1069 |
|
|
} |
1070 |
|
|
|
1071 |
|
|
|
1072 |
|
|
/* |
1073 |
|
|
* NVRAM watchdog thread (saves NVRAM every minute) |
1074 |
|
|
*/ |
1075 |
|
|
|
1076 |
|
|
static void *nvram_func(void *arg) |
1077 |
|
|
{ |
1078 |
|
|
struct timespec req = {60, 0}; // 1 minute |
1079 |
|
|
|
1080 |
|
|
for (;;) { |
1081 |
|
|
pthread_testcancel(); |
1082 |
|
|
nanosleep(&req, NULL); |
1083 |
|
|
pthread_testcancel(); |
1084 |
|
|
if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) { |
1085 |
|
|
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
1086 |
|
|
SaveXPRAM(); |
1087 |
|
|
} |
1088 |
|
|
} |
1089 |
|
|
return NULL; |
1090 |
|
|
} |
1091 |
|
|
|
1092 |
|
|
|
1093 |
|
|
/* |
1094 |
|
|
* 60Hz thread (really 60.15Hz) |
1095 |
|
|
*/ |
1096 |
|
|
|
1097 |
|
|
static void *tick_func(void *arg) |
1098 |
|
|
{ |
1099 |
|
|
int tick_counter = 0; |
1100 |
|
|
struct timespec req = {0, 16625000}; |
1101 |
|
|
|
1102 |
|
|
for (;;) { |
1103 |
|
|
|
1104 |
|
|
// Wait |
1105 |
|
|
nanosleep(&req, NULL); |
1106 |
|
|
|
1107 |
|
|
#if !EMULATED_PPC |
1108 |
|
|
// Did we crash? |
1109 |
|
|
if (emul_thread_fatal) { |
1110 |
|
|
|
1111 |
|
|
// Yes, dump registers |
1112 |
|
|
pt_regs *r = (pt_regs *)&sigsegv_regs; |
1113 |
|
|
char str[256]; |
1114 |
gbeauche |
1.23 |
if (crash_reason == NULL) |
1115 |
|
|
crash_reason = "SIGSEGV"; |
1116 |
|
|
sprintf(str, "%s\n" |
1117 |
cebix |
1.1 |
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1118 |
|
|
" xer %08lx cr %08lx \n" |
1119 |
|
|
" r0 %08lx r1 %08lx r2 %08lx r3 %08lx\n" |
1120 |
|
|
" r4 %08lx r5 %08lx r6 %08lx r7 %08lx\n" |
1121 |
|
|
" r8 %08lx r9 %08lx r10 %08lx r11 %08lx\n" |
1122 |
|
|
" r12 %08lx r13 %08lx r14 %08lx r15 %08lx\n" |
1123 |
|
|
" r16 %08lx r17 %08lx r18 %08lx r19 %08lx\n" |
1124 |
|
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1125 |
|
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1126 |
|
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1127 |
gbeauche |
1.23 |
crash_reason, |
1128 |
cebix |
1.1 |
r->nip, r->link, r->ctr, r->msr, |
1129 |
|
|
r->xer, r->ccr, |
1130 |
|
|
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1131 |
|
|
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
1132 |
|
|
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
1133 |
|
|
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
1134 |
|
|
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
1135 |
|
|
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
1136 |
|
|
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
1137 |
|
|
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
1138 |
|
|
printf(str); |
1139 |
|
|
VideoQuitFullScreen(); |
1140 |
|
|
|
1141 |
|
|
#ifdef ENABLE_MON |
1142 |
|
|
// Start up mon in real-mode |
1143 |
|
|
printf("Welcome to the sheep factory.\n"); |
1144 |
|
|
char *arg[4] = {"mon", "-m", "-r", NULL}; |
1145 |
|
|
mon(3, arg); |
1146 |
|
|
#endif |
1147 |
|
|
return NULL; |
1148 |
|
|
} |
1149 |
|
|
#endif |
1150 |
|
|
|
1151 |
|
|
// Pseudo Mac 1Hz interrupt, update local time |
1152 |
|
|
if (++tick_counter > 60) { |
1153 |
|
|
tick_counter = 0; |
1154 |
|
|
WriteMacInt32(0x20c, TimerDateTime()); |
1155 |
|
|
} |
1156 |
|
|
|
1157 |
|
|
// Trigger 60Hz interrupt |
1158 |
|
|
if (ReadMacInt32(XLM_IRQ_NEST) == 0) { |
1159 |
|
|
SetInterruptFlag(INTFLAG_VIA); |
1160 |
|
|
TriggerInterrupt(); |
1161 |
|
|
} |
1162 |
|
|
} |
1163 |
|
|
return NULL; |
1164 |
|
|
} |
1165 |
|
|
|
1166 |
|
|
|
1167 |
|
|
/* |
1168 |
cebix |
1.2 |
* Pthread configuration |
1169 |
|
|
*/ |
1170 |
|
|
|
1171 |
|
|
void Set_pthread_attr(pthread_attr_t *attr, int priority) |
1172 |
|
|
{ |
1173 |
gbeauche |
1.14 |
#ifdef HAVE_PTHREADS |
1174 |
|
|
pthread_attr_init(attr); |
1175 |
|
|
#if defined(_POSIX_THREAD_PRIORITY_SCHEDULING) |
1176 |
|
|
// Some of these only work for superuser |
1177 |
|
|
if (geteuid() == 0) { |
1178 |
|
|
pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED); |
1179 |
|
|
pthread_attr_setschedpolicy(attr, SCHED_FIFO); |
1180 |
|
|
struct sched_param fifo_param; |
1181 |
|
|
fifo_param.sched_priority = ((sched_get_priority_min(SCHED_FIFO) + |
1182 |
|
|
sched_get_priority_max(SCHED_FIFO)) / 2 + |
1183 |
|
|
priority); |
1184 |
|
|
pthread_attr_setschedparam(attr, &fifo_param); |
1185 |
|
|
} |
1186 |
|
|
if (pthread_attr_setscope(attr, PTHREAD_SCOPE_SYSTEM) != 0) { |
1187 |
|
|
#ifdef PTHREAD_SCOPE_BOUND_NP |
1188 |
|
|
// If system scope is not available (eg. we're not running |
1189 |
|
|
// with CAP_SCHED_MGT capability on an SGI box), try bound |
1190 |
|
|
// scope. It exposes pthread scheduling to the kernel, |
1191 |
|
|
// without setting realtime priority. |
1192 |
|
|
pthread_attr_setscope(attr, PTHREAD_SCOPE_BOUND_NP); |
1193 |
|
|
#endif |
1194 |
|
|
} |
1195 |
|
|
#endif |
1196 |
|
|
#endif |
1197 |
cebix |
1.2 |
} |
1198 |
|
|
|
1199 |
|
|
|
1200 |
|
|
/* |
1201 |
cebix |
1.1 |
* Mutexes |
1202 |
|
|
*/ |
1203 |
|
|
|
1204 |
gbeauche |
1.7 |
#ifdef HAVE_PTHREADS |
1205 |
|
|
|
1206 |
|
|
struct B2_mutex { |
1207 |
|
|
B2_mutex() { |
1208 |
|
|
pthread_mutexattr_t attr; |
1209 |
|
|
pthread_mutexattr_init(&attr); |
1210 |
|
|
// Initialize the mutex for priority inheritance -- |
1211 |
|
|
// required for accurate timing. |
1212 |
|
|
#ifdef HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL |
1213 |
|
|
pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT); |
1214 |
|
|
#endif |
1215 |
|
|
#if defined(HAVE_PTHREAD_MUTEXATTR_SETTYPE) && defined(PTHREAD_MUTEX_NORMAL) |
1216 |
|
|
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); |
1217 |
|
|
#endif |
1218 |
|
|
#ifdef HAVE_PTHREAD_MUTEXATTR_SETPSHARED |
1219 |
|
|
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE); |
1220 |
|
|
#endif |
1221 |
|
|
pthread_mutex_init(&m, &attr); |
1222 |
|
|
pthread_mutexattr_destroy(&attr); |
1223 |
|
|
} |
1224 |
|
|
~B2_mutex() { |
1225 |
|
|
pthread_mutex_trylock(&m); // Make sure it's locked before |
1226 |
|
|
pthread_mutex_unlock(&m); // unlocking it. |
1227 |
|
|
pthread_mutex_destroy(&m); |
1228 |
|
|
} |
1229 |
|
|
pthread_mutex_t m; |
1230 |
|
|
}; |
1231 |
|
|
|
1232 |
|
|
B2_mutex *B2_create_mutex(void) |
1233 |
|
|
{ |
1234 |
|
|
return new B2_mutex; |
1235 |
|
|
} |
1236 |
|
|
|
1237 |
|
|
void B2_lock_mutex(B2_mutex *mutex) |
1238 |
|
|
{ |
1239 |
|
|
pthread_mutex_lock(&mutex->m); |
1240 |
|
|
} |
1241 |
|
|
|
1242 |
|
|
void B2_unlock_mutex(B2_mutex *mutex) |
1243 |
|
|
{ |
1244 |
|
|
pthread_mutex_unlock(&mutex->m); |
1245 |
|
|
} |
1246 |
|
|
|
1247 |
|
|
void B2_delete_mutex(B2_mutex *mutex) |
1248 |
|
|
{ |
1249 |
|
|
delete mutex; |
1250 |
|
|
} |
1251 |
|
|
|
1252 |
|
|
#else |
1253 |
|
|
|
1254 |
cebix |
1.1 |
struct B2_mutex { |
1255 |
|
|
int dummy; |
1256 |
|
|
}; |
1257 |
|
|
|
1258 |
|
|
B2_mutex *B2_create_mutex(void) |
1259 |
|
|
{ |
1260 |
|
|
return new B2_mutex; |
1261 |
|
|
} |
1262 |
|
|
|
1263 |
|
|
void B2_lock_mutex(B2_mutex *mutex) |
1264 |
|
|
{ |
1265 |
|
|
} |
1266 |
|
|
|
1267 |
|
|
void B2_unlock_mutex(B2_mutex *mutex) |
1268 |
|
|
{ |
1269 |
|
|
} |
1270 |
|
|
|
1271 |
|
|
void B2_delete_mutex(B2_mutex *mutex) |
1272 |
|
|
{ |
1273 |
|
|
delete mutex; |
1274 |
|
|
} |
1275 |
|
|
|
1276 |
gbeauche |
1.7 |
#endif |
1277 |
|
|
|
1278 |
cebix |
1.1 |
|
1279 |
|
|
/* |
1280 |
|
|
* Trigger signal USR2 from another thread |
1281 |
|
|
*/ |
1282 |
|
|
|
1283 |
gbeauche |
1.8 |
#if !EMULATED_PPC || ASYNC_IRQ |
1284 |
cebix |
1.1 |
void TriggerInterrupt(void) |
1285 |
|
|
{ |
1286 |
|
|
if (ready_for_signals) |
1287 |
|
|
pthread_kill(emul_thread, SIGUSR2); |
1288 |
|
|
} |
1289 |
gbeauche |
1.7 |
#endif |
1290 |
cebix |
1.1 |
|
1291 |
|
|
|
1292 |
|
|
/* |
1293 |
|
|
* Interrupt flags (must be handled atomically!) |
1294 |
|
|
*/ |
1295 |
|
|
|
1296 |
|
|
volatile uint32 InterruptFlags = 0; |
1297 |
|
|
|
1298 |
|
|
void SetInterruptFlag(uint32 flag) |
1299 |
|
|
{ |
1300 |
|
|
atomic_or((int *)&InterruptFlags, flag); |
1301 |
|
|
} |
1302 |
|
|
|
1303 |
|
|
void ClearInterruptFlag(uint32 flag) |
1304 |
|
|
{ |
1305 |
|
|
atomic_and((int *)&InterruptFlags, ~flag); |
1306 |
|
|
} |
1307 |
|
|
|
1308 |
|
|
|
1309 |
|
|
/* |
1310 |
|
|
* Disable interrupts |
1311 |
|
|
*/ |
1312 |
|
|
|
1313 |
|
|
void DisableInterrupt(void) |
1314 |
|
|
{ |
1315 |
gbeauche |
1.7 |
atomic_add((int *)XLM_IRQ_NEST, 1); |
1316 |
cebix |
1.1 |
} |
1317 |
|
|
|
1318 |
|
|
|
1319 |
|
|
/* |
1320 |
|
|
* Enable interrupts |
1321 |
|
|
*/ |
1322 |
|
|
|
1323 |
|
|
void EnableInterrupt(void) |
1324 |
|
|
{ |
1325 |
gbeauche |
1.7 |
atomic_add((int *)XLM_IRQ_NEST, -1); |
1326 |
cebix |
1.1 |
} |
1327 |
|
|
|
1328 |
|
|
|
1329 |
|
|
/* |
1330 |
|
|
* USR2 handler |
1331 |
|
|
*/ |
1332 |
|
|
|
1333 |
gbeauche |
1.8 |
#if EMULATED_PPC |
1334 |
|
|
static void sigusr2_handler(int sig) |
1335 |
|
|
{ |
1336 |
|
|
#if ASYNC_IRQ |
1337 |
|
|
extern void HandleInterrupt(void); |
1338 |
|
|
HandleInterrupt(); |
1339 |
|
|
#endif |
1340 |
|
|
} |
1341 |
|
|
#else |
1342 |
cebix |
1.1 |
static void sigusr2_handler(int sig, sigcontext_struct *sc) |
1343 |
|
|
{ |
1344 |
|
|
pt_regs *r = sc->regs; |
1345 |
|
|
|
1346 |
|
|
// Do nothing if interrupts are disabled |
1347 |
|
|
if (*(int32 *)XLM_IRQ_NEST > 0) |
1348 |
|
|
return; |
1349 |
|
|
|
1350 |
|
|
// Disable MacOS stack sniffer |
1351 |
|
|
WriteMacInt32(0x110, 0); |
1352 |
|
|
|
1353 |
|
|
// Interrupt action depends on current run mode |
1354 |
|
|
switch (ReadMacInt32(XLM_RUN_MODE)) { |
1355 |
|
|
case MODE_68K: |
1356 |
|
|
// 68k emulator active, trigger 68k interrupt level 1 |
1357 |
|
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1358 |
|
|
r->ccr |= ntohl(kernel_data->v[0x674 >> 2]); |
1359 |
|
|
break; |
1360 |
|
|
|
1361 |
|
|
#if INTERRUPTS_IN_NATIVE_MODE |
1362 |
|
|
case MODE_NATIVE: |
1363 |
|
|
// 68k emulator inactive, in nanokernel? |
1364 |
|
|
if (r->gpr[1] != KernelDataAddr) { |
1365 |
|
|
// Prepare for 68k interrupt level 1 |
1366 |
|
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1367 |
|
|
WriteMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc, ReadMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc) | ntohl(kernel_data->v[0x674 >> 2])); |
1368 |
|
|
|
1369 |
|
|
// Execute nanokernel interrupt routine (this will activate the 68k emulator) |
1370 |
|
|
atomic_add((int32 *)XLM_IRQ_NEST, 1); |
1371 |
|
|
if (ROMType == ROMTYPE_NEWWORLD) |
1372 |
|
|
ppc_interrupt(ROM_BASE + 0x312b1c, KernelDataAddr); |
1373 |
|
|
else |
1374 |
|
|
ppc_interrupt(ROM_BASE + 0x312a3c, KernelDataAddr); |
1375 |
|
|
} |
1376 |
|
|
break; |
1377 |
|
|
#endif |
1378 |
|
|
|
1379 |
|
|
#if INTERRUPTS_IN_EMUL_OP_MODE |
1380 |
|
|
case MODE_EMUL_OP: |
1381 |
|
|
// 68k emulator active, within EMUL_OP routine, execute 68k interrupt routine directly when interrupt level is 0 |
1382 |
|
|
if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) { |
1383 |
|
|
|
1384 |
|
|
// Set extra stack for SIGSEGV handler |
1385 |
|
|
struct sigaltstack new_stack; |
1386 |
|
|
new_stack.ss_sp = extra_stack; |
1387 |
|
|
new_stack.ss_flags = 0; |
1388 |
|
|
new_stack.ss_size = SIG_STACK_SIZE; |
1389 |
|
|
sigaltstack(&new_stack, NULL); |
1390 |
|
|
#if 1 |
1391 |
|
|
// Execute full 68k interrupt routine |
1392 |
|
|
M68kRegisters r; |
1393 |
|
|
uint32 old_r25 = ReadMacInt32(XLM_68K_R25); // Save interrupt level |
1394 |
|
|
WriteMacInt32(XLM_68K_R25, 0x21); // Execute with interrupt level 1 |
1395 |
|
|
static const uint16 proc[] = { |
1396 |
|
|
0x3f3c, 0x0000, // move.w #$0000,-(sp) (fake format word) |
1397 |
|
|
0x487a, 0x000a, // pea @1(pc) (return address) |
1398 |
|
|
0x40e7, // move sr,-(sp) (saved SR) |
1399 |
|
|
0x2078, 0x0064, // move.l $64,a0 |
1400 |
|
|
0x4ed0, // jmp (a0) |
1401 |
|
|
M68K_RTS // @1 |
1402 |
|
|
}; |
1403 |
|
|
Execute68k((uint32)proc, &r); |
1404 |
|
|
WriteMacInt32(XLM_68K_R25, old_r25); // Restore interrupt level |
1405 |
|
|
#else |
1406 |
|
|
// Only update cursor |
1407 |
|
|
if (HasMacStarted()) { |
1408 |
|
|
if (InterruptFlags & INTFLAG_VIA) { |
1409 |
|
|
ClearInterruptFlag(INTFLAG_VIA); |
1410 |
|
|
ADBInterrupt(); |
1411 |
gbeauche |
1.17 |
ExecuteNative(NATIVE_VIDEO_VBL); |
1412 |
cebix |
1.1 |
} |
1413 |
|
|
} |
1414 |
|
|
#endif |
1415 |
|
|
// Reset normal signal stack |
1416 |
|
|
new_stack.ss_sp = sig_stack; |
1417 |
|
|
new_stack.ss_flags = 0; |
1418 |
|
|
new_stack.ss_size = SIG_STACK_SIZE; |
1419 |
|
|
sigaltstack(&new_stack, NULL); |
1420 |
|
|
} |
1421 |
|
|
break; |
1422 |
|
|
#endif |
1423 |
|
|
} |
1424 |
|
|
} |
1425 |
gbeauche |
1.8 |
#endif |
1426 |
cebix |
1.1 |
|
1427 |
|
|
|
1428 |
|
|
/* |
1429 |
|
|
* SIGSEGV handler |
1430 |
|
|
*/ |
1431 |
|
|
|
1432 |
gbeauche |
1.8 |
#if !EMULATED_PPC |
1433 |
cebix |
1.1 |
static void sigsegv_handler(int sig, sigcontext_struct *sc) |
1434 |
|
|
{ |
1435 |
|
|
pt_regs *r = sc->regs; |
1436 |
gbeauche |
1.5 |
|
1437 |
|
|
// Get effective address |
1438 |
|
|
uint32 addr = r->dar; |
1439 |
|
|
|
1440 |
|
|
#if ENABLE_VOSF |
1441 |
|
|
// Handle screen fault. |
1442 |
|
|
extern bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction); |
1443 |
|
|
if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->nip)) |
1444 |
|
|
return; |
1445 |
|
|
#endif |
1446 |
|
|
|
1447 |
cebix |
1.1 |
num_segv++; |
1448 |
|
|
|
1449 |
|
|
// Fault in Mac ROM or RAM? |
1450 |
|
|
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
1451 |
|
|
if (mac_fault) { |
1452 |
|
|
|
1453 |
|
|
// "VM settings" during MacOS 8 installation |
1454 |
|
|
if (r->nip == ROM_BASE + 0x488160 && r->gpr[20] == 0xf8000000) { |
1455 |
|
|
r->nip += 4; |
1456 |
|
|
r->gpr[8] = 0; |
1457 |
|
|
return; |
1458 |
|
|
|
1459 |
|
|
// MacOS 8.5 installation |
1460 |
|
|
} else if (r->nip == ROM_BASE + 0x488140 && r->gpr[16] == 0xf8000000) { |
1461 |
|
|
r->nip += 4; |
1462 |
|
|
r->gpr[8] = 0; |
1463 |
|
|
return; |
1464 |
|
|
|
1465 |
|
|
// MacOS 8 serial drivers on startup |
1466 |
|
|
} else if (r->nip == ROM_BASE + 0x48e080 && (r->gpr[8] == 0xf3012002 || r->gpr[8] == 0xf3012000)) { |
1467 |
|
|
r->nip += 4; |
1468 |
|
|
r->gpr[8] = 0; |
1469 |
|
|
return; |
1470 |
|
|
|
1471 |
|
|
// MacOS 8.1 serial drivers on startup |
1472 |
|
|
} else if (r->nip == ROM_BASE + 0x48c5e0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1473 |
|
|
r->nip += 4; |
1474 |
|
|
return; |
1475 |
|
|
} else if (r->nip == ROM_BASE + 0x4a10a0 && (r->gpr[20] == 0xf3012002 || r->gpr[20] == 0xf3012000)) { |
1476 |
|
|
r->nip += 4; |
1477 |
|
|
return; |
1478 |
|
|
} |
1479 |
|
|
|
1480 |
gbeauche |
1.5 |
// Get opcode and divide into fields |
1481 |
|
|
uint32 opcode = *((uint32 *)r->nip); |
1482 |
|
|
uint32 primop = opcode >> 26; |
1483 |
|
|
uint32 exop = (opcode >> 1) & 0x3ff; |
1484 |
|
|
uint32 ra = (opcode >> 16) & 0x1f; |
1485 |
|
|
uint32 rb = (opcode >> 11) & 0x1f; |
1486 |
|
|
uint32 rd = (opcode >> 21) & 0x1f; |
1487 |
|
|
int32 imm = (int16)(opcode & 0xffff); |
1488 |
|
|
|
1489 |
cebix |
1.1 |
// Analyze opcode |
1490 |
|
|
enum { |
1491 |
|
|
TYPE_UNKNOWN, |
1492 |
|
|
TYPE_LOAD, |
1493 |
|
|
TYPE_STORE |
1494 |
|
|
} transfer_type = TYPE_UNKNOWN; |
1495 |
|
|
enum { |
1496 |
|
|
SIZE_UNKNOWN, |
1497 |
|
|
SIZE_BYTE, |
1498 |
|
|
SIZE_HALFWORD, |
1499 |
|
|
SIZE_WORD |
1500 |
|
|
} transfer_size = SIZE_UNKNOWN; |
1501 |
|
|
enum { |
1502 |
|
|
MODE_UNKNOWN, |
1503 |
|
|
MODE_NORM, |
1504 |
|
|
MODE_U, |
1505 |
|
|
MODE_X, |
1506 |
|
|
MODE_UX |
1507 |
|
|
} addr_mode = MODE_UNKNOWN; |
1508 |
|
|
switch (primop) { |
1509 |
|
|
case 31: |
1510 |
|
|
switch (exop) { |
1511 |
|
|
case 23: // lwzx |
1512 |
|
|
transfer_type = TYPE_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_X; break; |
1513 |
|
|
case 55: // lwzux |
1514 |
|
|
transfer_type = TYPE_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_UX; break; |
1515 |
|
|
case 87: // lbzx |
1516 |
|
|
transfer_type = TYPE_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_X; break; |
1517 |
|
|
case 119: // lbzux |
1518 |
|
|
transfer_type = TYPE_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_UX; break; |
1519 |
|
|
case 151: // stwx |
1520 |
|
|
transfer_type = TYPE_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_X; break; |
1521 |
|
|
case 183: // stwux |
1522 |
|
|
transfer_type = TYPE_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_UX; break; |
1523 |
|
|
case 215: // stbx |
1524 |
|
|
transfer_type = TYPE_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_X; break; |
1525 |
|
|
case 247: // stbux |
1526 |
|
|
transfer_type = TYPE_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_UX; break; |
1527 |
|
|
case 279: // lhzx |
1528 |
|
|
transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_X; break; |
1529 |
|
|
case 311: // lhzux |
1530 |
|
|
transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_UX; break; |
1531 |
|
|
case 343: // lhax |
1532 |
|
|
transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_X; break; |
1533 |
|
|
case 375: // lhaux |
1534 |
|
|
transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_UX; break; |
1535 |
|
|
case 407: // sthx |
1536 |
|
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_X; break; |
1537 |
|
|
case 439: // sthux |
1538 |
|
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_UX; break; |
1539 |
|
|
} |
1540 |
|
|
break; |
1541 |
|
|
|
1542 |
|
|
case 32: // lwz |
1543 |
|
|
transfer_type = TYPE_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_NORM; break; |
1544 |
|
|
case 33: // lwzu |
1545 |
|
|
transfer_type = TYPE_LOAD; transfer_size = SIZE_WORD; addr_mode = MODE_U; break; |
1546 |
|
|
case 34: // lbz |
1547 |
|
|
transfer_type = TYPE_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_NORM; break; |
1548 |
|
|
case 35: // lbzu |
1549 |
|
|
transfer_type = TYPE_LOAD; transfer_size = SIZE_BYTE; addr_mode = MODE_U; break; |
1550 |
|
|
case 36: // stw |
1551 |
|
|
transfer_type = TYPE_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_NORM; break; |
1552 |
|
|
case 37: // stwu |
1553 |
|
|
transfer_type = TYPE_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_U; break; |
1554 |
|
|
case 38: // stb |
1555 |
|
|
transfer_type = TYPE_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_NORM; break; |
1556 |
|
|
case 39: // stbu |
1557 |
|
|
transfer_type = TYPE_STORE; transfer_size = SIZE_BYTE; addr_mode = MODE_U; break; |
1558 |
|
|
case 40: // lhz |
1559 |
|
|
transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_NORM; break; |
1560 |
|
|
case 41: // lhzu |
1561 |
|
|
transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_U; break; |
1562 |
|
|
case 42: // lha |
1563 |
|
|
transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_NORM; break; |
1564 |
|
|
case 43: // lhau |
1565 |
|
|
transfer_type = TYPE_LOAD; transfer_size = SIZE_HALFWORD; addr_mode = MODE_U; break; |
1566 |
|
|
case 44: // sth |
1567 |
|
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_NORM; break; |
1568 |
|
|
case 45: // sthu |
1569 |
|
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_U; break; |
1570 |
gbeauche |
1.23 |
#if EMULATE_UNALIGNED_LOADSTORE_MULTIPLE |
1571 |
|
|
case 46: // lmw |
1572 |
|
|
if (sig == SIGBUS) { |
1573 |
|
|
uint32 ea = (ra == 0 ? 0 : r->gpr[ra]) + imm; |
1574 |
|
|
D(bug("WARNING: unaligned lmw to EA=%08x from IP=%08x\n", ea, r->nip)); |
1575 |
|
|
for (int i = rd; i <= 31; i++) { |
1576 |
|
|
r->gpr[i] = ReadMacInt32(ea); |
1577 |
|
|
ea += 4; |
1578 |
|
|
} |
1579 |
|
|
r->nip += 4; |
1580 |
|
|
goto rti; |
1581 |
|
|
} |
1582 |
|
|
break; |
1583 |
|
|
case 47: // stmw |
1584 |
|
|
if (sig == SIGBUS) { |
1585 |
|
|
uint32 ea = (ra == 0 ? 0 : r->gpr[ra]) + imm; |
1586 |
|
|
D(bug("WARNING: unaligned stmw to EA=%08x from IP=%08x\n", ea, r->nip)); |
1587 |
|
|
for (int i = rd; i <= 31; i++) { |
1588 |
|
|
WriteMacInt32(ea, r->gpr[i]); |
1589 |
|
|
ea += 4; |
1590 |
|
|
} |
1591 |
|
|
r->nip += 4; |
1592 |
|
|
goto rti; |
1593 |
|
|
} |
1594 |
|
|
break; |
1595 |
|
|
#endif |
1596 |
cebix |
1.1 |
} |
1597 |
|
|
|
1598 |
|
|
// Ignore ROM writes |
1599 |
|
|
if (transfer_type == TYPE_STORE && addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) { |
1600 |
|
|
// 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)); |
1601 |
|
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1602 |
|
|
r->gpr[ra] = addr; |
1603 |
|
|
r->nip += 4; |
1604 |
|
|
goto rti; |
1605 |
|
|
} |
1606 |
|
|
|
1607 |
|
|
// Ignore illegal memory accesses? |
1608 |
|
|
if (PrefsFindBool("ignoresegv")) { |
1609 |
|
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1610 |
|
|
r->gpr[ra] = addr; |
1611 |
|
|
if (transfer_type == TYPE_LOAD) |
1612 |
|
|
r->gpr[rd] = 0; |
1613 |
|
|
r->nip += 4; |
1614 |
|
|
goto rti; |
1615 |
|
|
} |
1616 |
|
|
|
1617 |
|
|
// In GUI mode, show error alert |
1618 |
|
|
if (!PrefsFindBool("nogui")) { |
1619 |
|
|
char str[256]; |
1620 |
|
|
if (transfer_type == TYPE_LOAD || transfer_type == TYPE_STORE) |
1621 |
|
|
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]); |
1622 |
|
|
else |
1623 |
|
|
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode); |
1624 |
|
|
ErrorAlert(str); |
1625 |
|
|
QuitEmulator(); |
1626 |
|
|
return; |
1627 |
|
|
} |
1628 |
|
|
} |
1629 |
|
|
|
1630 |
|
|
// For all other errors, jump into debugger (sort of...) |
1631 |
gbeauche |
1.23 |
crash_reason = (sig == SIGBUS) ? "SIGBUS" : "SIGSEGV"; |
1632 |
cebix |
1.1 |
if (!ready_for_signals) { |
1633 |
gbeauche |
1.23 |
printf("%s\n"); |
1634 |
cebix |
1.1 |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
1635 |
|
|
printf( |
1636 |
|
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1637 |
|
|
" xer %08lx cr %08lx \n" |
1638 |
|
|
" r0 %08lx r1 %08lx r2 %08lx r3 %08lx\n" |
1639 |
|
|
" r4 %08lx r5 %08lx r6 %08lx r7 %08lx\n" |
1640 |
|
|
" r8 %08lx r9 %08lx r10 %08lx r11 %08lx\n" |
1641 |
|
|
" r12 %08lx r13 %08lx r14 %08lx r15 %08lx\n" |
1642 |
|
|
" r16 %08lx r17 %08lx r18 %08lx r19 %08lx\n" |
1643 |
|
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1644 |
|
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1645 |
|
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1646 |
gbeauche |
1.23 |
crash_reason, |
1647 |
cebix |
1.1 |
r->nip, r->link, r->ctr, r->msr, |
1648 |
|
|
r->xer, r->ccr, |
1649 |
|
|
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1650 |
|
|
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
1651 |
|
|
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
1652 |
|
|
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
1653 |
|
|
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
1654 |
|
|
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
1655 |
|
|
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
1656 |
|
|
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
1657 |
|
|
exit(1); |
1658 |
|
|
QuitEmulator(); |
1659 |
|
|
return; |
1660 |
|
|
} else { |
1661 |
|
|
// We crashed. Save registers, tell tick thread and loop forever |
1662 |
|
|
sigsegv_regs = *(sigregs *)r; |
1663 |
|
|
emul_thread_fatal = true; |
1664 |
|
|
for (;;) ; |
1665 |
|
|
} |
1666 |
|
|
rti:; |
1667 |
|
|
} |
1668 |
|
|
|
1669 |
|
|
|
1670 |
|
|
/* |
1671 |
|
|
* SIGILL handler |
1672 |
|
|
*/ |
1673 |
|
|
|
1674 |
|
|
static void sigill_handler(int sig, sigcontext_struct *sc) |
1675 |
|
|
{ |
1676 |
|
|
pt_regs *r = sc->regs; |
1677 |
|
|
char str[256]; |
1678 |
|
|
|
1679 |
|
|
// Fault in Mac ROM or RAM? |
1680 |
|
|
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
1681 |
|
|
if (mac_fault) { |
1682 |
|
|
|
1683 |
|
|
// Get opcode and divide into fields |
1684 |
|
|
uint32 opcode = *((uint32 *)r->nip); |
1685 |
|
|
uint32 primop = opcode >> 26; |
1686 |
|
|
uint32 exop = (opcode >> 1) & 0x3ff; |
1687 |
|
|
uint32 ra = (opcode >> 16) & 0x1f; |
1688 |
|
|
uint32 rb = (opcode >> 11) & 0x1f; |
1689 |
|
|
uint32 rd = (opcode >> 21) & 0x1f; |
1690 |
|
|
int32 imm = (int16)(opcode & 0xffff); |
1691 |
|
|
|
1692 |
|
|
switch (primop) { |
1693 |
|
|
case 9: // POWER instructions |
1694 |
|
|
case 22: |
1695 |
|
|
power_inst: sprintf(str, GetString(STR_POWER_INSTRUCTION_ERR), r->nip, r->gpr[1], opcode); |
1696 |
|
|
ErrorAlert(str); |
1697 |
|
|
QuitEmulator(); |
1698 |
|
|
return; |
1699 |
|
|
|
1700 |
|
|
case 31: |
1701 |
|
|
switch (exop) { |
1702 |
|
|
case 83: // mfmsr |
1703 |
|
|
r->gpr[rd] = 0xf072; |
1704 |
|
|
r->nip += 4; |
1705 |
|
|
goto rti; |
1706 |
|
|
|
1707 |
|
|
case 210: // mtsr |
1708 |
|
|
case 242: // mtsrin |
1709 |
|
|
case 306: // tlbie |
1710 |
|
|
r->nip += 4; |
1711 |
|
|
goto rti; |
1712 |
|
|
|
1713 |
|
|
case 339: { // mfspr |
1714 |
|
|
int spr = ra | (rb << 5); |
1715 |
|
|
switch (spr) { |
1716 |
|
|
case 0: // MQ |
1717 |
|
|
case 22: // DEC |
1718 |
|
|
case 952: // MMCR0 |
1719 |
|
|
case 953: // PMC1 |
1720 |
|
|
case 954: // PMC2 |
1721 |
|
|
case 955: // SIA |
1722 |
|
|
case 956: // MMCR1 |
1723 |
|
|
case 957: // PMC3 |
1724 |
|
|
case 958: // PMC4 |
1725 |
|
|
case 959: // SDA |
1726 |
|
|
r->nip += 4; |
1727 |
|
|
goto rti; |
1728 |
|
|
case 25: // SDR1 |
1729 |
|
|
r->gpr[rd] = 0xdead001f; |
1730 |
|
|
r->nip += 4; |
1731 |
|
|
goto rti; |
1732 |
|
|
case 287: // PVR |
1733 |
|
|
r->gpr[rd] = PVR; |
1734 |
|
|
r->nip += 4; |
1735 |
|
|
goto rti; |
1736 |
|
|
} |
1737 |
|
|
break; |
1738 |
|
|
} |
1739 |
|
|
|
1740 |
|
|
case 467: { // mtspr |
1741 |
|
|
int spr = ra | (rb << 5); |
1742 |
|
|
switch (spr) { |
1743 |
|
|
case 0: // MQ |
1744 |
|
|
case 22: // DEC |
1745 |
|
|
case 275: // SPRG3 |
1746 |
|
|
case 528: // IBAT0U |
1747 |
|
|
case 529: // IBAT0L |
1748 |
|
|
case 530: // IBAT1U |
1749 |
|
|
case 531: // IBAT1L |
1750 |
|
|
case 532: // IBAT2U |
1751 |
|
|
case 533: // IBAT2L |
1752 |
|
|
case 534: // IBAT3U |
1753 |
|
|
case 535: // IBAT3L |
1754 |
|
|
case 536: // DBAT0U |
1755 |
|
|
case 537: // DBAT0L |
1756 |
|
|
case 538: // DBAT1U |
1757 |
|
|
case 539: // DBAT1L |
1758 |
|
|
case 540: // DBAT2U |
1759 |
|
|
case 541: // DBAT2L |
1760 |
|
|
case 542: // DBAT3U |
1761 |
|
|
case 543: // DBAT3L |
1762 |
|
|
case 952: // MMCR0 |
1763 |
|
|
case 953: // PMC1 |
1764 |
|
|
case 954: // PMC2 |
1765 |
|
|
case 955: // SIA |
1766 |
|
|
case 956: // MMCR1 |
1767 |
|
|
case 957: // PMC3 |
1768 |
|
|
case 958: // PMC4 |
1769 |
|
|
case 959: // SDA |
1770 |
|
|
r->nip += 4; |
1771 |
|
|
goto rti; |
1772 |
|
|
} |
1773 |
|
|
break; |
1774 |
|
|
} |
1775 |
|
|
|
1776 |
|
|
case 29: case 107: case 152: case 153: // POWER instructions |
1777 |
|
|
case 184: case 216: case 217: case 248: |
1778 |
|
|
case 264: case 277: case 331: case 360: |
1779 |
|
|
case 363: case 488: case 531: case 537: |
1780 |
|
|
case 541: case 664: case 665: case 696: |
1781 |
|
|
case 728: case 729: case 760: case 920: |
1782 |
|
|
case 921: case 952: |
1783 |
|
|
goto power_inst; |
1784 |
|
|
} |
1785 |
|
|
} |
1786 |
|
|
|
1787 |
|
|
// In GUI mode, show error alert |
1788 |
|
|
if (!PrefsFindBool("nogui")) { |
1789 |
|
|
sprintf(str, GetString(STR_UNKNOWN_SEGV_ERR), r->nip, r->gpr[24], r->gpr[1], opcode); |
1790 |
|
|
ErrorAlert(str); |
1791 |
|
|
QuitEmulator(); |
1792 |
|
|
return; |
1793 |
|
|
} |
1794 |
|
|
} |
1795 |
|
|
|
1796 |
|
|
// For all other errors, jump into debugger (sort of...) |
1797 |
gbeauche |
1.23 |
crash_reason = "SIGILL"; |
1798 |
cebix |
1.1 |
if (!ready_for_signals) { |
1799 |
gbeauche |
1.23 |
printf("%s\n"); |
1800 |
cebix |
1.1 |
printf(" sigcontext %p, pt_regs %p\n", sc, r); |
1801 |
|
|
printf( |
1802 |
|
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1803 |
|
|
" xer %08lx cr %08lx \n" |
1804 |
|
|
" r0 %08lx r1 %08lx r2 %08lx r3 %08lx\n" |
1805 |
|
|
" r4 %08lx r5 %08lx r6 %08lx r7 %08lx\n" |
1806 |
|
|
" r8 %08lx r9 %08lx r10 %08lx r11 %08lx\n" |
1807 |
|
|
" r12 %08lx r13 %08lx r14 %08lx r15 %08lx\n" |
1808 |
|
|
" r16 %08lx r17 %08lx r18 %08lx r19 %08lx\n" |
1809 |
|
|
" r20 %08lx r21 %08lx r22 %08lx r23 %08lx\n" |
1810 |
|
|
" r24 %08lx r25 %08lx r26 %08lx r27 %08lx\n" |
1811 |
|
|
" r28 %08lx r29 %08lx r30 %08lx r31 %08lx\n", |
1812 |
gbeauche |
1.23 |
crash_reason, |
1813 |
cebix |
1.1 |
r->nip, r->link, r->ctr, r->msr, |
1814 |
|
|
r->xer, r->ccr, |
1815 |
|
|
r->gpr[0], r->gpr[1], r->gpr[2], r->gpr[3], |
1816 |
|
|
r->gpr[4], r->gpr[5], r->gpr[6], r->gpr[7], |
1817 |
|
|
r->gpr[8], r->gpr[9], r->gpr[10], r->gpr[11], |
1818 |
|
|
r->gpr[12], r->gpr[13], r->gpr[14], r->gpr[15], |
1819 |
|
|
r->gpr[16], r->gpr[17], r->gpr[18], r->gpr[19], |
1820 |
|
|
r->gpr[20], r->gpr[21], r->gpr[22], r->gpr[23], |
1821 |
|
|
r->gpr[24], r->gpr[25], r->gpr[26], r->gpr[27], |
1822 |
|
|
r->gpr[28], r->gpr[29], r->gpr[30], r->gpr[31]); |
1823 |
|
|
exit(1); |
1824 |
|
|
QuitEmulator(); |
1825 |
|
|
return; |
1826 |
|
|
} else { |
1827 |
|
|
// We crashed. Save registers, tell tick thread and loop forever |
1828 |
|
|
sigsegv_regs = *(sigregs *)r; |
1829 |
|
|
emul_thread_fatal = true; |
1830 |
|
|
for (;;) ; |
1831 |
|
|
} |
1832 |
|
|
rti:; |
1833 |
|
|
} |
1834 |
|
|
#endif |
1835 |
gbeauche |
1.15 |
|
1836 |
|
|
|
1837 |
|
|
/* |
1838 |
|
|
* Helpers to share 32-bit addressable data with MacOS |
1839 |
|
|
*/ |
1840 |
|
|
|
1841 |
|
|
bool SheepMem::Init(void) |
1842 |
|
|
{ |
1843 |
gbeauche |
1.20 |
const int page_size = getpagesize(); |
1844 |
|
|
|
1845 |
|
|
// Allocate SheepShaver globals |
1846 |
gbeauche |
1.15 |
if (vm_acquire_fixed((char *)base, size) < 0) |
1847 |
|
|
return false; |
1848 |
gbeauche |
1.18 |
|
1849 |
gbeauche |
1.20 |
// Allocate page with all bits set to 0 |
1850 |
gbeauche |
1.18 |
zero_page = base + size; |
1851 |
|
|
if (vm_acquire_fixed((char *)zero_page, page_size) < 0) |
1852 |
|
|
return false; |
1853 |
gbeauche |
1.19 |
memset((char *)zero_page, 0, page_size); |
1854 |
gbeauche |
1.18 |
if (vm_protect((char *)zero_page, page_size, VM_PAGE_READ) < 0) |
1855 |
|
|
return false; |
1856 |
|
|
|
1857 |
gbeauche |
1.20 |
#if EMULATED_PPC |
1858 |
|
|
// Allocate alternate stack for PowerPC interrupt routine |
1859 |
|
|
sig_stack = zero_page + page_size; |
1860 |
|
|
if (vm_acquire_fixed((char *)sig_stack, SIG_STACK_SIZE) < 0) |
1861 |
|
|
return false; |
1862 |
|
|
#endif |
1863 |
|
|
|
1864 |
gbeauche |
1.15 |
top = base + size; |
1865 |
|
|
return true; |
1866 |
|
|
} |
1867 |
|
|
|
1868 |
|
|
void SheepMem::Exit(void) |
1869 |
|
|
{ |
1870 |
gbeauche |
1.18 |
if (top) { |
1871 |
gbeauche |
1.20 |
const int page_size = getpagesize(); |
1872 |
|
|
|
1873 |
|
|
// Delete SheepShaver globals |
1874 |
|
|
vm_release((void *)base, size); |
1875 |
|
|
|
1876 |
|
|
// Delete zero page |
1877 |
|
|
vm_release((void *)zero_page, page_size); |
1878 |
|
|
|
1879 |
|
|
#if EMULATED_PPC |
1880 |
|
|
// Delete alternate stack for PowerPC interrupt routine |
1881 |
|
|
vm_release((void *)sig_stack, SIG_STACK_SIZE); |
1882 |
|
|
#endif |
1883 |
gbeauche |
1.18 |
} |
1884 |
gbeauche |
1.15 |
} |
1885 |
cebix |
1.1 |
|
1886 |
|
|
|
1887 |
|
|
/* |
1888 |
|
|
* Display alert |
1889 |
|
|
*/ |
1890 |
|
|
|
1891 |
|
|
#ifdef ENABLE_GTK |
1892 |
|
|
static void dl_destroyed(void) |
1893 |
|
|
{ |
1894 |
|
|
gtk_main_quit(); |
1895 |
|
|
} |
1896 |
|
|
|
1897 |
|
|
static void dl_quit(GtkWidget *dialog) |
1898 |
|
|
{ |
1899 |
|
|
gtk_widget_destroy(dialog); |
1900 |
|
|
} |
1901 |
|
|
|
1902 |
|
|
void display_alert(int title_id, int prefix_id, int button_id, const char *text) |
1903 |
|
|
{ |
1904 |
|
|
char str[256]; |
1905 |
|
|
sprintf(str, GetString(prefix_id), text); |
1906 |
|
|
|
1907 |
|
|
GtkWidget *dialog = gtk_dialog_new(); |
1908 |
|
|
gtk_window_set_title(GTK_WINDOW(dialog), GetString(title_id)); |
1909 |
|
|
gtk_container_border_width(GTK_CONTAINER(dialog), 5); |
1910 |
|
|
gtk_widget_set_uposition(GTK_WIDGET(dialog), 100, 150); |
1911 |
|
|
gtk_signal_connect(GTK_OBJECT(dialog), "destroy", GTK_SIGNAL_FUNC(dl_destroyed), NULL); |
1912 |
|
|
|
1913 |
|
|
GtkWidget *label = gtk_label_new(str); |
1914 |
|
|
gtk_widget_show(label); |
1915 |
|
|
gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), label, TRUE, TRUE, 0); |
1916 |
|
|
|
1917 |
|
|
GtkWidget *button = gtk_button_new_with_label(GetString(button_id)); |
1918 |
|
|
gtk_widget_show(button); |
1919 |
|
|
gtk_signal_connect_object(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(dl_quit), GTK_OBJECT(dialog)); |
1920 |
|
|
gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area), button, FALSE, FALSE, 0); |
1921 |
|
|
GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT); |
1922 |
|
|
gtk_widget_grab_default(button); |
1923 |
|
|
gtk_widget_show(dialog); |
1924 |
|
|
|
1925 |
|
|
gtk_main(); |
1926 |
|
|
} |
1927 |
|
|
#endif |
1928 |
|
|
|
1929 |
|
|
|
1930 |
|
|
/* |
1931 |
|
|
* Display error alert |
1932 |
|
|
*/ |
1933 |
|
|
|
1934 |
|
|
void ErrorAlert(const char *text) |
1935 |
|
|
{ |
1936 |
|
|
#ifdef ENABLE_GTK |
1937 |
|
|
if (PrefsFindBool("nogui") || x_display == NULL) { |
1938 |
|
|
printf(GetString(STR_SHELL_ERROR_PREFIX), text); |
1939 |
|
|
return; |
1940 |
|
|
} |
1941 |
|
|
VideoQuitFullScreen(); |
1942 |
|
|
display_alert(STR_ERROR_ALERT_TITLE, STR_GUI_ERROR_PREFIX, STR_QUIT_BUTTON, text); |
1943 |
|
|
#else |
1944 |
|
|
printf(GetString(STR_SHELL_ERROR_PREFIX), text); |
1945 |
|
|
#endif |
1946 |
|
|
} |
1947 |
|
|
|
1948 |
|
|
|
1949 |
|
|
/* |
1950 |
|
|
* Display warning alert |
1951 |
|
|
*/ |
1952 |
|
|
|
1953 |
|
|
void WarningAlert(const char *text) |
1954 |
|
|
{ |
1955 |
|
|
#ifdef ENABLE_GTK |
1956 |
|
|
if (PrefsFindBool("nogui") || x_display == NULL) { |
1957 |
|
|
printf(GetString(STR_SHELL_WARNING_PREFIX), text); |
1958 |
|
|
return; |
1959 |
|
|
} |
1960 |
|
|
display_alert(STR_WARNING_ALERT_TITLE, STR_GUI_WARNING_PREFIX, STR_OK_BUTTON, text); |
1961 |
|
|
#else |
1962 |
|
|
printf(GetString(STR_SHELL_WARNING_PREFIX), text); |
1963 |
|
|
#endif |
1964 |
|
|
} |
1965 |
|
|
|
1966 |
|
|
|
1967 |
|
|
/* |
1968 |
|
|
* Display choice alert |
1969 |
|
|
*/ |
1970 |
|
|
|
1971 |
|
|
bool ChoiceAlert(const char *text, const char *pos, const char *neg) |
1972 |
|
|
{ |
1973 |
|
|
printf(GetString(STR_SHELL_WARNING_PREFIX), text); |
1974 |
|
|
return false; //!! |
1975 |
|
|
} |