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