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