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