1 |
|
/* |
2 |
|
* main_unix.cpp - Emulation core, Unix implementation |
3 |
|
* |
4 |
< |
* SheepShaver (C) 1997-2004 Christian Bauer and Marc Hellwig |
4 |
> |
* SheepShaver (C) Christian Bauer and Marc Hellwig |
5 |
|
* |
6 |
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
* it under the terms of the GNU General Public License as published by |
27 |
|
* is slightly different from the SysV ABI used by Linux: |
28 |
|
* - Stack frames are different (e.g. LR is stored in 8(r1) under |
29 |
|
* MacOS, but in 4(r1) under Linux) |
30 |
< |
* - There is no TOC under Linux; r2 is free for the user |
30 |
> |
* - There is a pointer to Thread Local Storage (TLS) under Linux with |
31 |
> |
* recent enough glibc. This is r2 in 32-bit mode and r13 in |
32 |
> |
* 64-bit mode (PowerOpen/AIX ABI) |
33 |
|
* - r13 is used as a small data pointer under Linux (but appearently |
34 |
|
* it is not used this way? To be sure, we specify -msdata=none |
35 |
|
* in the Makefile) |
36 |
< |
* - As there is no TOC, there are also no TVECTs under Linux; |
37 |
< |
* function pointers point directly to the function code |
36 |
> |
* - There are no TVECTs under Linux; function pointers point |
37 |
> |
* directly to the function code |
38 |
|
* The Execute*() functions have to account for this. Additionally, we |
39 |
|
* cannot simply call MacOS functions by getting their TVECT and jumping |
40 |
|
* to it. Such calls are done via the call_macos*() functions in |
67 |
|
* ExecutePPC (or any function that might cause a mode switch). The signal |
68 |
|
* stack is restored before exiting the SIGUSR2 handler. |
69 |
|
* |
70 |
< |
* There is apparently another problem when processing signals. In |
71 |
< |
* fullscreen mode, we get quick updates of the mouse position. This |
72 |
< |
* causes an increased number of calls to TriggerInterrupt(). And, |
73 |
< |
* since IRQ_NEST is not fully handled atomically, nested calls to |
74 |
< |
* ppc_interrupt() may cause stack corruption to eventually crash the |
75 |
< |
* emulator. |
74 |
< |
* |
75 |
< |
* FIXME: |
76 |
< |
* The current solution is to allocate another signal stack when |
77 |
< |
* processing ppc_interrupt(). However, it may be better to detect |
78 |
< |
* the INTFLAG_ADB case and handle it specifically with some extra mutex? |
70 |
> |
* Note that POSIX standard says you can't modify the alternate |
71 |
> |
* signal stack while the process is executing on it. There is a |
72 |
> |
* hackaround though: we install a trampoline SIGUSR2 handler that |
73 |
> |
* sets up an alternate stack itself and calls the real handler. |
74 |
> |
* Then, when we call sigaltstack() there, we no longer get an EPERM, |
75 |
> |
* i.e. it now works. |
76 |
|
* |
77 |
|
* TODO: |
78 |
|
* check if SIGSEGV handler works for all registers (including FP!) |
89 |
|
#include <sys/mman.h> |
90 |
|
#include <sys/ipc.h> |
91 |
|
#include <sys/shm.h> |
92 |
+ |
#include <sys/stat.h> |
93 |
|
#include <signal.h> |
94 |
|
|
95 |
|
#include "sysdeps.h" |
103 |
|
#include "xpram.h" |
104 |
|
#include "timer.h" |
105 |
|
#include "adb.h" |
108 |
– |
#include "sony.h" |
109 |
– |
#include "disk.h" |
110 |
– |
#include "cdrom.h" |
111 |
– |
#include "scsi.h" |
106 |
|
#include "video.h" |
113 |
– |
#include "audio.h" |
114 |
– |
#include "ether.h" |
115 |
– |
#include "serial.h" |
116 |
– |
#include "clip.h" |
117 |
– |
#include "extfs.h" |
107 |
|
#include "sys.h" |
108 |
|
#include "macos_util.h" |
109 |
|
#include "rom_patches.h" |
110 |
|
#include "user_strings.h" |
111 |
|
#include "vm_alloc.h" |
112 |
|
#include "sigsegv.h" |
113 |
< |
#include "thunks.h" |
113 |
> |
#include "sigregs.h" |
114 |
> |
#include "rpc.h" |
115 |
|
|
116 |
|
#define DEBUG 0 |
117 |
|
#include "debug.h" |
136 |
|
#ifdef ENABLE_XF86_DGA |
137 |
|
#include <X11/Xlib.h> |
138 |
|
#include <X11/Xutil.h> |
139 |
< |
#include <X11/extensions/xf86dga.h> |
139 |
> |
#include <X11/extensions/Xxf86dga.h> |
140 |
|
#endif |
141 |
|
|
142 |
|
#ifdef ENABLE_MON |
156 |
|
// Interrupts in native mode? |
157 |
|
#define INTERRUPTS_IN_NATIVE_MODE 1 |
158 |
|
|
169 |
– |
// Number of alternate stacks for signal handlers? |
170 |
– |
#define SIG_STACK_COUNT 4 |
171 |
– |
|
159 |
|
|
160 |
|
// Constants |
161 |
|
const char ROM_FILE_NAME[] = "ROM"; |
162 |
|
const char ROM_FILE_NAME2[] = "Mac OS ROM"; |
163 |
|
|
164 |
< |
#if REAL_ADDRESSING |
178 |
< |
const uintptr RAM_BASE = 0x20000000; // Base address of RAM |
179 |
< |
#else |
164 |
> |
#if !REAL_ADDRESSING |
165 |
|
// FIXME: needs to be >= 0x04000000 |
166 |
|
const uintptr RAM_BASE = 0x10000000; // Base address of RAM |
167 |
|
#endif |
168 |
< |
const uint32 SIG_STACK_SIZE = 0x10000; // Size of signal stack |
169 |
< |
|
170 |
< |
|
186 |
< |
#if !EMULATED_PPC |
187 |
< |
struct sigregs { |
188 |
< |
uint32 nip; |
189 |
< |
uint32 link; |
190 |
< |
uint32 ctr; |
191 |
< |
uint32 msr; |
192 |
< |
uint32 xer; |
193 |
< |
uint32 ccr; |
194 |
< |
uint32 gpr[32]; |
195 |
< |
}; |
196 |
< |
|
197 |
< |
#if defined(__linux__) |
198 |
< |
#include <sys/ucontext.h> |
199 |
< |
#define MACHINE_REGISTERS(scp) ((machine_regs *)(((ucontext_t *)scp)->uc_mcontext.regs)) |
200 |
< |
|
201 |
< |
struct machine_regs : public pt_regs |
202 |
< |
{ |
203 |
< |
u_long & cr() { return pt_regs::ccr; } |
204 |
< |
uint32 cr() const { return pt_regs::ccr; } |
205 |
< |
uint32 lr() const { return pt_regs::link; } |
206 |
< |
uint32 ctr() const { return pt_regs::ctr; } |
207 |
< |
uint32 xer() const { return pt_regs::xer; } |
208 |
< |
uint32 msr() const { return pt_regs::msr; } |
209 |
< |
uint32 dar() const { return pt_regs::dar; } |
210 |
< |
u_long & pc() { return pt_regs::nip; } |
211 |
< |
uint32 pc() const { return pt_regs::nip; } |
212 |
< |
u_long & gpr(int i) { return pt_regs::gpr[i]; } |
213 |
< |
uint32 gpr(int i) const { return pt_regs::gpr[i]; } |
214 |
< |
}; |
215 |
< |
#endif |
216 |
< |
|
217 |
< |
#if defined(__APPLE__) && defined(__MACH__) |
218 |
< |
#include <sys/signal.h> |
219 |
< |
extern "C" int sigaltstack(const struct sigaltstack *ss, struct sigaltstack *oss); |
220 |
< |
|
221 |
< |
#include <sys/ucontext.h> |
222 |
< |
#define MACHINE_REGISTERS(scp) ((machine_regs *)(((ucontext_t *)scp)->uc_mcontext)) |
223 |
< |
|
224 |
< |
struct machine_regs : public mcontext |
225 |
< |
{ |
226 |
< |
uint32 & cr() { return ss.cr; } |
227 |
< |
uint32 cr() const { return ss.cr; } |
228 |
< |
uint32 lr() const { return ss.lr; } |
229 |
< |
uint32 ctr() const { return ss.ctr; } |
230 |
< |
uint32 xer() const { return ss.xer; } |
231 |
< |
uint32 msr() const { return ss.srr1; } |
232 |
< |
uint32 dar() const { return es.dar; } |
233 |
< |
uint32 & pc() { return ss.srr0; } |
234 |
< |
uint32 pc() const { return ss.srr0; } |
235 |
< |
uint32 & gpr(int i) { return (&ss.r0)[i]; } |
236 |
< |
uint32 gpr(int i) const { return (&ss.r0)[i]; } |
237 |
< |
}; |
238 |
< |
#endif |
239 |
< |
|
240 |
< |
static void build_sigregs(sigregs *srp, machine_regs *mrp) |
241 |
< |
{ |
242 |
< |
srp->nip = mrp->pc(); |
243 |
< |
srp->link = mrp->lr(); |
244 |
< |
srp->ctr = mrp->ctr(); |
245 |
< |
srp->msr = mrp->msr(); |
246 |
< |
srp->xer = mrp->xer(); |
247 |
< |
srp->ccr = mrp->cr(); |
248 |
< |
for (int i = 0; i < 32; i++) |
249 |
< |
srp->gpr[i] = mrp->gpr(i); |
250 |
< |
} |
251 |
< |
|
252 |
< |
static struct sigaltstack sig_stacks[SIG_STACK_COUNT]; // Stacks for signal handlers |
253 |
< |
static int sig_stack_id = 0; // Stack slot currently used |
254 |
< |
|
255 |
< |
static inline void sig_stack_acquire(void) |
256 |
< |
{ |
257 |
< |
if (++sig_stack_id == SIG_STACK_COUNT) { |
258 |
< |
printf("FATAL: signal stack overflow\n"); |
259 |
< |
return; |
260 |
< |
} |
261 |
< |
sigaltstack(&sig_stacks[sig_stack_id], NULL); |
262 |
< |
} |
263 |
< |
|
264 |
< |
static inline void sig_stack_release(void) |
265 |
< |
{ |
266 |
< |
if (--sig_stack_id < 0) { |
267 |
< |
printf("FATAL: signal stack underflow\n"); |
268 |
< |
return; |
269 |
< |
} |
270 |
< |
sigaltstack(&sig_stacks[sig_stack_id], NULL); |
271 |
< |
} |
168 |
> |
const uintptr ROM_BASE = 0x40800000; // Base address of ROM |
169 |
> |
#if REAL_ADDRESSING |
170 |
> |
const uint32 ROM_ALIGNMENT = 0x100000; // ROM must be aligned to a 1MB boundary |
171 |
|
#endif |
172 |
+ |
const uint32 SIG_STACK_SIZE = 0x10000; // Size of signal stack |
173 |
|
|
174 |
|
|
175 |
|
// Global variables (exported) |
176 |
|
#if !EMULATED_PPC |
177 |
< |
void *TOC; // Small data pointer (r13) |
177 |
> |
void *TOC = NULL; // Pointer to Thread Local Storage (r2) |
178 |
> |
void *R13 = NULL; // Pointer to .sdata section (r13 under Linux) |
179 |
|
#endif |
180 |
|
uint32 RAMBase; // Base address of Mac RAM |
181 |
|
uint32 RAMSize; // Size of Mac RAM |
182 |
+ |
uint32 ROMBase; // Base address of Mac ROM |
183 |
|
uint32 KernelDataAddr; // Address of Kernel Data |
184 |
|
uint32 BootGlobsAddr; // Address of BootGlobs structure at top of Mac RAM |
185 |
|
uint32 DRCacheAddr; // Address of DR Cache |
229 |
|
#else |
230 |
|
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 |
+ |
static stack_t sig_stack; // Stack for signal handlers |
233 |
+ |
static stack_t extra_stack; // Stack for SIGSEGV inside interrupt handler |
234 |
|
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 |
|
static const char *crash_reason = NULL; // Reason of the crash (SIGSEGV, SIGBUS, SIGILL) |
237 |
|
#endif |
238 |
|
|
239 |
+ |
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 |
|
uint32 SheepMem::page_size; // Size of a native page |
243 |
|
uintptr SheepMem::zero_page = 0; // Address of ro page filled in with zeros |
244 |
|
uintptr SheepMem::base = 0x60000000; // Address of SheepShaver data |
245 |
< |
uintptr SheepMem::top = 0; // Top of SheepShaver data (stack like storage) |
245 |
> |
uintptr SheepMem::proc; // Bottom address of SheepShave procedures |
246 |
> |
uintptr SheepMem::data; // Top of SheepShaver data (stack like storage) |
247 |
|
|
248 |
|
|
249 |
|
// Prototypes |
250 |
+ |
static bool kernel_data_init(void); |
251 |
+ |
static void kernel_data_exit(void); |
252 |
|
static void Quit(void); |
253 |
|
static void *emul_func(void *arg); |
254 |
|
static void *nvram_func(void *arg); |
257 |
|
extern void emul_ppc(uint32 start); |
258 |
|
extern void init_emul_ppc(void); |
259 |
|
extern void exit_emul_ppc(void); |
260 |
< |
sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t); |
260 |
> |
sigsegv_return_t sigsegv_handler(sigsegv_info_t *sip); |
261 |
|
#else |
262 |
< |
static void sigusr2_handler(int sig, siginfo_t *sip, void *scp); |
262 |
> |
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 |
|
static void sigsegv_handler(int sig, siginfo_t *sip, void *scp); |
265 |
|
static void sigill_handler(int sig, siginfo_t *sip, void *scp); |
266 |
|
#endif |
268 |
|
|
269 |
|
// From asm_linux.S |
270 |
|
#if !EMULATED_PPC |
360 |
– |
extern "C" void *get_toc(void); |
271 |
|
extern "C" void *get_sp(void); |
272 |
< |
extern "C" void flush_icache_range(void *start, void *end); |
272 |
> |
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 |
> |
extern "C" void flush_icache_range(uint32 start, uint32 end); |
277 |
|
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); |
337 |
|
|
338 |
|
|
339 |
|
/* |
340 |
+ |
* Memory management helpers |
341 |
+ |
*/ |
342 |
+ |
|
343 |
+ |
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 |
+ |
{ |
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 |
|
* Main program |
361 |
|
*/ |
362 |
|
|
369 |
|
exit(0); |
370 |
|
} |
371 |
|
|
372 |
< |
int main(int argc, char **argv) |
372 |
> |
static bool valid_vmdir(const char *path) |
373 |
|
{ |
374 |
< |
char str[256]; |
375 |
< |
int16 i16; |
376 |
< |
int rom_fd; |
377 |
< |
FILE *proc_file; |
378 |
< |
const char *rom_path; |
379 |
< |
uint32 rom_size, actual; |
380 |
< |
uint8 *rom_tmp; |
381 |
< |
time_t now, expire; |
448 |
< |
|
449 |
< |
// Initialize variables |
450 |
< |
RAMBase = 0; |
451 |
< |
tzset(); |
452 |
< |
|
453 |
< |
// Print some info |
454 |
< |
printf(GetString(STR_ABOUT_TEXT1), VERSION_MAJOR, VERSION_MINOR); |
455 |
< |
printf(" %s\n", GetString(STR_ABOUT_TEXT2)); |
456 |
< |
|
457 |
< |
#if !EMULATED_PPC |
458 |
< |
// Get TOC pointer |
459 |
< |
TOC = get_toc(); |
460 |
< |
#endif |
461 |
< |
|
462 |
< |
#ifdef ENABLE_GTK |
463 |
< |
// Init GTK |
464 |
< |
gtk_set_locale(); |
465 |
< |
gtk_init(&argc, &argv); |
466 |
< |
#endif |
467 |
< |
|
468 |
< |
// Read preferences |
469 |
< |
PrefsInit(argc, argv); |
470 |
< |
|
471 |
< |
// Parse command line arguments |
472 |
< |
for (int i=1; i<argc; i++) { |
473 |
< |
if (strcmp(argv[i], "--help") == 0) { |
474 |
< |
usage(argv[0]); |
475 |
< |
#ifndef USE_SDL_VIDEO |
476 |
< |
} else if (strcmp(argv[i], "--display") == 0) { |
477 |
< |
i++; |
478 |
< |
if (i < argc) |
479 |
< |
x_display_name = strdup(argv[i]); |
480 |
< |
#endif |
481 |
< |
} else if (argv[i][0] == '-') { |
482 |
< |
fprintf(stderr, "Unrecognized option '%s'\n", argv[i]); |
483 |
< |
usage(argv[0]); |
484 |
< |
} |
485 |
< |
} |
486 |
< |
|
487 |
< |
#ifdef USE_SDL |
488 |
< |
// Initialize SDL system |
489 |
< |
int sdl_flags = 0; |
490 |
< |
#ifdef USE_SDL_VIDEO |
491 |
< |
sdl_flags |= SDL_INIT_VIDEO; |
492 |
< |
#endif |
493 |
< |
#ifdef USE_SDL_AUDIO |
494 |
< |
sdl_flags |= SDL_INIT_AUDIO; |
495 |
< |
#endif |
496 |
< |
assert(sdl_flags != 0); |
497 |
< |
if (SDL_Init(sdl_flags) == -1) { |
498 |
< |
char str[256]; |
499 |
< |
sprintf(str, "Could not initialize SDL: %s.\n", SDL_GetError()); |
500 |
< |
ErrorAlert(str); |
501 |
< |
goto quit; |
502 |
< |
} |
503 |
< |
atexit(SDL_Quit); |
504 |
< |
#endif |
505 |
< |
|
506 |
< |
#ifndef USE_SDL_VIDEO |
507 |
< |
// Open display |
508 |
< |
x_display = XOpenDisplay(x_display_name); |
509 |
< |
if (x_display == NULL) { |
510 |
< |
char str[256]; |
511 |
< |
sprintf(str, GetString(STR_NO_XSERVER_ERR), XDisplayName(x_display_name)); |
512 |
< |
ErrorAlert(str); |
513 |
< |
goto quit; |
514 |
< |
} |
515 |
< |
|
516 |
< |
#if defined(ENABLE_XF86_DGA) && !defined(ENABLE_MON) |
517 |
< |
// Fork out, so we can return from fullscreen mode when things get ugly |
518 |
< |
XF86DGAForkApp(DefaultScreen(x_display)); |
519 |
< |
#endif |
520 |
< |
#endif |
521 |
< |
|
522 |
< |
#ifdef ENABLE_MON |
523 |
< |
// Initialize mon |
524 |
< |
mon_init(); |
525 |
< |
#endif |
526 |
< |
|
527 |
< |
#if !EMULATED_PPC |
528 |
< |
// Create and install stacks for signal handlers |
529 |
< |
for (int i = 0; i < SIG_STACK_COUNT; i++) { |
530 |
< |
void *sig_stack = malloc(SIG_STACK_SIZE); |
531 |
< |
D(bug("Signal stack %d at %p\n", i, sig_stack)); |
532 |
< |
if (sig_stack == NULL) { |
533 |
< |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
534 |
< |
goto quit; |
374 |
> |
const int suffix_len = sizeof(".sheepvm") - 1; |
375 |
> |
int len = strlen(path); |
376 |
> |
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 |
> |
struct stat d; |
380 |
> |
if (!stat(path, &d) && S_ISDIR(d.st_mode)) { |
381 |
> |
return true; |
382 |
|
} |
536 |
– |
sig_stacks[i].ss_sp = sig_stack; |
537 |
– |
sig_stacks[i].ss_flags = 0; |
538 |
– |
sig_stacks[i].ss_size = SIG_STACK_SIZE; |
539 |
– |
} |
540 |
– |
sig_stack_id = 0; |
541 |
– |
if (sigaltstack(&sig_stacks[0], NULL) < 0) { |
542 |
– |
sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno)); |
543 |
– |
ErrorAlert(str); |
544 |
– |
goto quit; |
383 |
|
} |
384 |
< |
#endif |
384 |
> |
return false; |
385 |
> |
} |
386 |
|
|
387 |
+ |
static void get_system_info(void) |
388 |
+ |
{ |
389 |
|
#if !EMULATED_PPC |
390 |
< |
// Install SIGSEGV and SIGBUS handlers |
550 |
< |
sigemptyset(&sigsegv_action.sa_mask); // Block interrupts during SEGV handling |
551 |
< |
sigaddset(&sigsegv_action.sa_mask, SIGUSR2); |
552 |
< |
sigsegv_action.sa_sigaction = sigsegv_handler; |
553 |
< |
sigsegv_action.sa_flags = SA_ONSTACK | SA_SIGINFO; |
554 |
< |
#ifdef HAVE_SIGNAL_SA_RESTORER |
555 |
< |
sigsegv_action.sa_restorer = NULL; |
556 |
< |
#endif |
557 |
< |
if (sigaction(SIGSEGV, &sigsegv_action, NULL) < 0) { |
558 |
< |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
559 |
< |
ErrorAlert(str); |
560 |
< |
goto quit; |
561 |
< |
} |
562 |
< |
if (sigaction(SIGBUS, &sigsegv_action, NULL) < 0) { |
563 |
< |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
564 |
< |
ErrorAlert(str); |
565 |
< |
goto quit; |
566 |
< |
} |
567 |
< |
#else |
568 |
< |
// Install SIGSEGV handler for CPU emulator |
569 |
< |
if (!sigsegv_install_handler(sigsegv_handler)) { |
570 |
< |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
571 |
< |
ErrorAlert(str); |
572 |
< |
goto quit; |
573 |
< |
} |
390 |
> |
FILE *proc_file; |
391 |
|
#endif |
392 |
|
|
576 |
– |
// Initialize VM system |
577 |
– |
vm_init(); |
578 |
– |
|
579 |
– |
// Get system info |
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__) |
429 |
|
} |
430 |
|
fclose(proc_file); |
431 |
|
} else { |
432 |
+ |
char str[256]; |
433 |
|
sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno)); |
434 |
|
WarningAlert(str); |
435 |
|
} |
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)" }, |
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 |
|
|
503 |
|
|
504 |
|
// Parse line |
505 |
|
int i; |
506 |
+ |
float f; |
507 |
|
char value[256]; |
508 |
< |
if (sscanf(line, "cpu : %[0-9A-Za-a]", value) == 1) { |
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++) { |
520 |
|
else |
521 |
|
printf("Found a PowerPC %s processor\n", cpu_name); |
522 |
|
} |
523 |
< |
if (sscanf(line, "clock : %dMHz", &i) == 1) |
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 |
+ |
char str[256]; |
531 |
|
sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno)); |
532 |
|
WarningAlert(str); |
533 |
|
} |
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 |
+ |
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 |
+ |
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 |
+ |
#ifdef USE_SDL |
673 |
+ |
static bool init_sdl() |
674 |
+ |
{ |
675 |
+ |
int sdl_flags = 0; |
676 |
+ |
#ifdef USE_SDL_VIDEO |
677 |
+ |
sdl_flags |= SDL_INIT_VIDEO; |
678 |
+ |
#endif |
679 |
+ |
#ifdef USE_SDL_AUDIO |
680 |
+ |
sdl_flags |= SDL_INIT_AUDIO; |
681 |
+ |
#endif |
682 |
+ |
assert(sdl_flags != 0); |
683 |
+ |
|
684 |
+ |
#ifdef USE_SDL_VIDEO |
685 |
+ |
// Don't let SDL block the screensaver |
686 |
+ |
setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", TRUE); |
687 |
+ |
|
688 |
+ |
// Make SDL pass through command-clicks and option-clicks unaltered |
689 |
+ |
setenv("SDL_HAS3BUTTONMOUSE", "1", TRUE); |
690 |
+ |
#endif |
691 |
+ |
|
692 |
+ |
if (SDL_Init(sdl_flags) == -1) { |
693 |
+ |
char str[256]; |
694 |
+ |
sprintf(str, "Could not initialize SDL: %s.\n", SDL_GetError()); |
695 |
+ |
ErrorAlert(str); |
696 |
+ |
return false; |
697 |
+ |
} |
698 |
+ |
atexit(SDL_Quit); |
699 |
+ |
|
700 |
+ |
// Don't let SDL catch SIGINT and SIGTERM signals |
701 |
+ |
signal(SIGINT, SIG_DFL); |
702 |
+ |
signal(SIGTERM, SIG_DFL); |
703 |
+ |
return true; |
704 |
+ |
} |
705 |
+ |
#endif |
706 |
+ |
|
707 |
+ |
int main(int argc, char **argv) |
708 |
+ |
{ |
709 |
+ |
char str[256]; |
710 |
+ |
bool memory_mapped_from_zero, ram_rom_areas_contiguous; |
711 |
+ |
const char *vmdir = NULL; |
712 |
+ |
|
713 |
+ |
// Initialize variables |
714 |
+ |
RAMBase = 0; |
715 |
+ |
tzset(); |
716 |
+ |
|
717 |
+ |
// Print some info |
718 |
+ |
printf(GetString(STR_ABOUT_TEXT1), VERSION_MAJOR, VERSION_MINOR); |
719 |
+ |
printf(" %s\n", GetString(STR_ABOUT_TEXT2)); |
720 |
+ |
|
721 |
+ |
#if !EMULATED_PPC |
722 |
+ |
#ifdef SYSTEM_CLOBBERS_R2 |
723 |
+ |
// Get TOC pointer |
724 |
+ |
TOC = get_r2(); |
725 |
+ |
#endif |
726 |
+ |
#ifdef SYSTEM_CLOBBERS_R13 |
727 |
+ |
// Get r13 register |
728 |
+ |
R13 = get_r13(); |
729 |
+ |
#endif |
730 |
+ |
#endif |
731 |
+ |
|
732 |
+ |
// Parse command line arguments |
733 |
+ |
for (int i=1; i<argc; i++) { |
734 |
+ |
if (strcmp(argv[i], "--help") == 0) { |
735 |
+ |
usage(argv[0]); |
736 |
+ |
#ifndef USE_SDL_VIDEO |
737 |
+ |
} else if (strcmp(argv[i], "--display") == 0) { |
738 |
+ |
i++; |
739 |
+ |
if (i < argc) |
740 |
+ |
x_display_name = strdup(argv[i]); |
741 |
+ |
#endif |
742 |
+ |
} else if (strcmp(argv[i], "--gui-connection") == 0) { |
743 |
+ |
argv[i++] = NULL; |
744 |
+ |
if (i < argc) { |
745 |
+ |
gui_connection_path = argv[i]; |
746 |
+ |
argv[i] = NULL; |
747 |
+ |
} |
748 |
+ |
} else if (valid_vmdir(argv[i])) { |
749 |
+ |
vmdir = argv[i]; |
750 |
+ |
argv[i] = NULL; |
751 |
+ |
printf("Using %s as vmdir.\n", vmdir); |
752 |
+ |
if (chdir(vmdir)) { |
753 |
+ |
printf("Failed to chdir to %s. Good bye.", vmdir); |
754 |
+ |
exit(1); |
755 |
+ |
} |
756 |
+ |
break; |
757 |
+ |
} |
758 |
+ |
} |
759 |
+ |
|
760 |
+ |
// Remove processed arguments |
761 |
+ |
for (int i=1; i<argc; i++) { |
762 |
+ |
int k; |
763 |
+ |
for (k=i; k<argc; k++) |
764 |
+ |
if (argv[k] != NULL) |
765 |
+ |
break; |
766 |
+ |
if (k > i) { |
767 |
+ |
k -= i; |
768 |
+ |
for (int j=i+k; j<argc; j++) |
769 |
+ |
argv[j-k] = argv[j]; |
770 |
+ |
argc -= k; |
771 |
+ |
} |
772 |
+ |
} |
773 |
+ |
|
774 |
+ |
// Connect to the external GUI |
775 |
+ |
if (gui_connection_path) { |
776 |
+ |
if ((gui_connection = rpc_init_client(gui_connection_path)) == NULL) { |
777 |
+ |
fprintf(stderr, "Failed to initialize RPC client connection to the GUI\n"); |
778 |
+ |
return 1; |
779 |
+ |
} |
780 |
+ |
} |
781 |
+ |
|
782 |
+ |
#ifdef ENABLE_GTK |
783 |
+ |
if (!gui_connection) { |
784 |
+ |
// Init GTK |
785 |
+ |
gtk_set_locale(); |
786 |
+ |
gtk_init(&argc, &argv); |
787 |
+ |
} |
788 |
+ |
#endif |
789 |
+ |
|
790 |
+ |
// Read preferences |
791 |
+ |
PrefsInit(vmdir, argc, argv); |
792 |
+ |
|
793 |
+ |
// Any command line arguments left? |
794 |
+ |
for (int i=1; i<argc; i++) { |
795 |
+ |
if (argv[i][0] == '-') { |
796 |
+ |
fprintf(stderr, "Unrecognized option '%s'\n", argv[i]); |
797 |
+ |
usage(argv[0]); |
798 |
+ |
} |
799 |
+ |
} |
800 |
+ |
|
801 |
+ |
#ifdef USE_SDL |
802 |
+ |
// Initialize SDL system |
803 |
+ |
if (!init_sdl()) |
804 |
+ |
goto quit; |
805 |
+ |
#endif |
806 |
+ |
|
807 |
+ |
#ifndef USE_SDL_VIDEO |
808 |
+ |
// Open display |
809 |
+ |
x_display = XOpenDisplay(x_display_name); |
810 |
+ |
if (x_display == NULL) { |
811 |
+ |
char str[256]; |
812 |
+ |
sprintf(str, GetString(STR_NO_XSERVER_ERR), XDisplayName(x_display_name)); |
813 |
+ |
ErrorAlert(str); |
814 |
+ |
goto quit; |
815 |
+ |
} |
816 |
+ |
|
817 |
+ |
#if defined(ENABLE_XF86_DGA) && !defined(ENABLE_MON) |
818 |
+ |
// Fork out, so we can return from fullscreen mode when things get ugly |
819 |
+ |
XF86DGAForkApp(DefaultScreen(x_display)); |
820 |
+ |
#endif |
821 |
+ |
#endif |
822 |
+ |
|
823 |
+ |
#ifdef ENABLE_MON |
824 |
+ |
// Initialize mon |
825 |
+ |
mon_init(); |
826 |
+ |
#endif |
827 |
+ |
|
828 |
+ |
// Install signal handlers |
829 |
+ |
if (!install_signal_handlers()) |
830 |
+ |
goto quit; |
831 |
+ |
|
832 |
+ |
// Initialize VM system |
833 |
+ |
vm_init(); |
834 |
+ |
|
835 |
+ |
// Get system info |
836 |
+ |
get_system_info(); |
837 |
|
|
838 |
|
// Init system routines |
839 |
|
SysInit(); |
856 |
|
goto quit; |
857 |
|
} |
858 |
|
|
770 |
– |
#ifndef PAGEZERO_HACK |
771 |
– |
// Create Low Memory area (0x0000..0x3000) |
772 |
– |
if (vm_acquire_fixed((char *)NATMEM_OFFSET, 0x3000) < 0) { |
773 |
– |
sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno)); |
774 |
– |
ErrorAlert(str); |
775 |
– |
goto quit; |
776 |
– |
} |
777 |
– |
lm_area_mapped = true; |
778 |
– |
#endif |
779 |
– |
|
859 |
|
// Create areas for Kernel Data |
860 |
< |
kernel_area = shmget(IPC_PRIVATE, KERNEL_AREA_SIZE, 0600); |
782 |
< |
if (kernel_area == -1) { |
783 |
< |
sprintf(str, GetString(STR_KD_SHMGET_ERR), strerror(errno)); |
784 |
< |
ErrorAlert(str); |
785 |
< |
goto quit; |
786 |
< |
} |
787 |
< |
if (shmat(kernel_area, (void *)(KERNEL_DATA_BASE + NATMEM_OFFSET), 0) < 0) { |
788 |
< |
sprintf(str, GetString(STR_KD_SHMAT_ERR), strerror(errno)); |
789 |
< |
ErrorAlert(str); |
790 |
< |
goto quit; |
791 |
< |
} |
792 |
< |
if (shmat(kernel_area, (void *)(KERNEL_DATA2_BASE + NATMEM_OFFSET), 0) < 0) { |
793 |
< |
sprintf(str, GetString(STR_KD2_SHMAT_ERR), strerror(errno)); |
794 |
< |
ErrorAlert(str); |
860 |
> |
if (!kernel_data_init()) |
861 |
|
goto quit; |
862 |
< |
} |
797 |
< |
kernel_data = (KernelData *)(KERNEL_DATA_BASE + NATMEM_OFFSET); |
862 |
> |
kernel_data = (KernelData *)Mac2HostAddr(KERNEL_DATA_BASE); |
863 |
|
emulator_data = &kernel_data->ed; |
864 |
|
KernelDataAddr = KERNEL_DATA_BASE; |
865 |
|
D(bug("Kernel Data at %p (%08x)\n", kernel_data, KERNEL_DATA_BASE)); |
866 |
|
D(bug("Emulator Data at %p (%08x)\n", emulator_data, KERNEL_DATA_BASE + offsetof(KernelData, ed))); |
867 |
|
|
868 |
|
// Create area for DR Cache |
869 |
< |
if (vm_acquire_fixed((void *)(DR_EMULATOR_BASE + NATMEM_OFFSET), DR_EMULATOR_SIZE) < 0) { |
869 |
> |
if (vm_mac_acquire_fixed(DR_EMULATOR_BASE, DR_EMULATOR_SIZE) < 0) { |
870 |
|
sprintf(str, GetString(STR_DR_EMULATOR_MMAP_ERR), strerror(errno)); |
871 |
|
ErrorAlert(str); |
872 |
|
goto quit; |
873 |
|
} |
874 |
|
dr_emulator_area_mapped = true; |
875 |
< |
if (vm_acquire_fixed((void *)(DR_CACHE_BASE + NATMEM_OFFSET), DR_CACHE_SIZE) < 0) { |
875 |
> |
if (vm_mac_acquire_fixed(DR_CACHE_BASE, DR_CACHE_SIZE) < 0) { |
876 |
|
sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno)); |
877 |
|
ErrorAlert(str); |
878 |
|
goto quit; |
894 |
|
ErrorAlert(str); |
895 |
|
goto quit; |
896 |
|
} |
897 |
< |
|
833 |
< |
// Create area for Mac ROM |
834 |
< |
ROMBaseHost = (uint8 *)(ROM_BASE + NATMEM_OFFSET); |
835 |
< |
if (vm_acquire_fixed(ROMBaseHost, ROM_AREA_SIZE) < 0) { |
836 |
< |
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
837 |
< |
ErrorAlert(str); |
838 |
< |
goto quit; |
839 |
< |
} |
840 |
< |
#if !EMULATED_PPC |
841 |
< |
if (vm_protect(ROMBaseHost, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
842 |
< |
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
843 |
< |
ErrorAlert(str); |
844 |
< |
goto quit; |
845 |
< |
} |
846 |
< |
#endif |
847 |
< |
rom_area_mapped = true; |
848 |
< |
D(bug("ROM area at %p (%08x)\n", ROMBaseHost, ROM_BASE)); |
849 |
< |
|
897 |
> |
|
898 |
|
// Create area for Mac RAM |
899 |
|
RAMSize = PrefsFindInt32("ramsize"); |
900 |
|
if (RAMSize < 8*1024*1024) { |
901 |
|
WarningAlert(GetString(STR_SMALL_RAM_WARN)); |
902 |
|
RAMSize = 8*1024*1024; |
903 |
|
} |
904 |
< |
|
905 |
< |
RAMBaseHost = (uint8 *)(RAM_BASE + NATMEM_OFFSET); |
906 |
< |
if (vm_acquire_fixed(RAMBaseHost, RAMSize) < 0) { |
907 |
< |
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
908 |
< |
ErrorAlert(str); |
909 |
< |
goto quit; |
904 |
> |
memory_mapped_from_zero = false; |
905 |
> |
ram_rom_areas_contiguous = false; |
906 |
> |
#if REAL_ADDRESSING && HAVE_LINKER_SCRIPT |
907 |
> |
if (vm_mac_acquire_fixed(0, RAMSize) == 0) { |
908 |
> |
D(bug("Could allocate RAM from 0x0000\n")); |
909 |
> |
RAMBase = 0; |
910 |
> |
RAMBaseHost = Mac2HostAddr(RAMBase); |
911 |
> |
memory_mapped_from_zero = true; |
912 |
> |
} |
913 |
> |
#endif |
914 |
> |
if (!memory_mapped_from_zero) { |
915 |
> |
#ifndef PAGEZERO_HACK |
916 |
> |
// Create Low Memory area (0x0000..0x3000) |
917 |
> |
if (vm_mac_acquire_fixed(0, 0x3000) < 0) { |
918 |
> |
sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno)); |
919 |
> |
ErrorAlert(str); |
920 |
> |
goto quit; |
921 |
> |
} |
922 |
> |
lm_area_mapped = true; |
923 |
> |
#endif |
924 |
> |
#if REAL_ADDRESSING |
925 |
> |
// Allocate RAM at any address. Since ROM must be higher than RAM, allocate the RAM |
926 |
> |
// and ROM areas contiguously, plus a little extra to allow for ROM address alignment. |
927 |
> |
RAMBaseHost = vm_mac_acquire(RAMSize + ROM_AREA_SIZE + ROM_ALIGNMENT); |
928 |
> |
if (RAMBaseHost == VM_MAP_FAILED) { |
929 |
> |
sprintf(str, GetString(STR_RAM_ROM_MMAP_ERR), strerror(errno)); |
930 |
> |
ErrorAlert(str); |
931 |
> |
goto quit; |
932 |
> |
} |
933 |
> |
RAMBase = Host2MacAddr(RAMBaseHost); |
934 |
> |
ROMBase = (RAMBase + RAMSize + ROM_ALIGNMENT -1) & -ROM_ALIGNMENT; |
935 |
> |
ROMBaseHost = Mac2HostAddr(ROMBase); |
936 |
> |
ram_rom_areas_contiguous = true; |
937 |
> |
#else |
938 |
> |
if (vm_mac_acquire_fixed(RAM_BASE, RAMSize) < 0) { |
939 |
> |
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
940 |
> |
ErrorAlert(str); |
941 |
> |
goto quit; |
942 |
> |
} |
943 |
> |
RAMBase = RAM_BASE; |
944 |
> |
RAMBaseHost = Mac2HostAddr(RAMBase); |
945 |
> |
#endif |
946 |
|
} |
947 |
|
#if !EMULATED_PPC |
948 |
|
if (vm_protect(RAMBaseHost, RAMSize, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
951 |
|
goto quit; |
952 |
|
} |
953 |
|
#endif |
870 |
– |
RAMBase = RAM_BASE; |
954 |
|
ram_area_mapped = true; |
955 |
|
D(bug("RAM area at %p (%08x)\n", RAMBaseHost, RAMBase)); |
956 |
|
|
957 |
< |
if (RAMBase > ROM_BASE) { |
958 |
< |
ErrorAlert(GetString(STR_RAM_HIGHER_THAN_ROM_ERR)); |
957 |
> |
if (RAMBase > KernelDataAddr) { |
958 |
> |
ErrorAlert(GetString(STR_RAM_AREA_TOO_HIGH_ERR)); |
959 |
|
goto quit; |
960 |
|
} |
878 |
– |
|
879 |
– |
// Load Mac ROM |
880 |
– |
rom_path = PrefsFindString("rom"); |
881 |
– |
rom_fd = open(rom_path ? rom_path : ROM_FILE_NAME, O_RDONLY); |
882 |
– |
if (rom_fd < 0) { |
883 |
– |
rom_fd = open(rom_path ? rom_path : ROM_FILE_NAME2, O_RDONLY); |
884 |
– |
if (rom_fd < 0) { |
885 |
– |
ErrorAlert(GetString(STR_NO_ROM_FILE_ERR)); |
886 |
– |
goto quit; |
887 |
– |
} |
888 |
– |
} |
889 |
– |
printf(GetString(STR_READING_ROM_FILE)); |
890 |
– |
rom_size = lseek(rom_fd, 0, SEEK_END); |
891 |
– |
lseek(rom_fd, 0, SEEK_SET); |
892 |
– |
rom_tmp = new uint8[ROM_SIZE]; |
893 |
– |
actual = read(rom_fd, (void *)rom_tmp, ROM_SIZE); |
894 |
– |
close(rom_fd); |
961 |
|
|
962 |
< |
// Decode Mac ROM |
963 |
< |
if (!DecodeROM(rom_tmp, actual)) { |
964 |
< |
if (rom_size != 4*1024*1024) { |
965 |
< |
ErrorAlert(GetString(STR_ROM_SIZE_ERR)); |
966 |
< |
goto quit; |
901 |
< |
} else { |
902 |
< |
ErrorAlert(GetString(STR_ROM_FILE_READ_ERR)); |
962 |
> |
// Create area for Mac ROM |
963 |
> |
if (!ram_rom_areas_contiguous) { |
964 |
> |
if (vm_mac_acquire_fixed(ROM_BASE, ROM_AREA_SIZE) < 0) { |
965 |
> |
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
966 |
> |
ErrorAlert(str); |
967 |
|
goto quit; |
968 |
|
} |
969 |
+ |
ROMBase = ROM_BASE; |
970 |
+ |
ROMBaseHost = Mac2HostAddr(ROMBase); |
971 |
|
} |
972 |
< |
delete[] rom_tmp; |
973 |
< |
|
974 |
< |
// Load NVRAM |
975 |
< |
XPRAMInit(); |
910 |
< |
|
911 |
< |
// Load XPRAM default values if signature not found |
912 |
< |
if (XPRAM[0x130c] != 0x4e || XPRAM[0x130d] != 0x75 |
913 |
< |
|| XPRAM[0x130e] != 0x4d || XPRAM[0x130f] != 0x63) { |
914 |
< |
D(bug("Loading XPRAM default values\n")); |
915 |
< |
memset(XPRAM + 0x1300, 0, 0x100); |
916 |
< |
XPRAM[0x130c] = 0x4e; // "NuMc" signature |
917 |
< |
XPRAM[0x130d] = 0x75; |
918 |
< |
XPRAM[0x130e] = 0x4d; |
919 |
< |
XPRAM[0x130f] = 0x63; |
920 |
< |
XPRAM[0x1301] = 0x80; // InternalWaitFlags = DynWait (don't wait for SCSI devices upon bootup) |
921 |
< |
XPRAM[0x1310] = 0xa8; // Standard PRAM values |
922 |
< |
XPRAM[0x1311] = 0x00; |
923 |
< |
XPRAM[0x1312] = 0x00; |
924 |
< |
XPRAM[0x1313] = 0x22; |
925 |
< |
XPRAM[0x1314] = 0xcc; |
926 |
< |
XPRAM[0x1315] = 0x0a; |
927 |
< |
XPRAM[0x1316] = 0xcc; |
928 |
< |
XPRAM[0x1317] = 0x0a; |
929 |
< |
XPRAM[0x131c] = 0x00; |
930 |
< |
XPRAM[0x131d] = 0x02; |
931 |
< |
XPRAM[0x131e] = 0x63; |
932 |
< |
XPRAM[0x131f] = 0x00; |
933 |
< |
XPRAM[0x1308] = 0x13; |
934 |
< |
XPRAM[0x1309] = 0x88; |
935 |
< |
XPRAM[0x130a] = 0x00; |
936 |
< |
XPRAM[0x130b] = 0xcc; |
937 |
< |
XPRAM[0x1376] = 0x00; // OSDefault = MacOS |
938 |
< |
XPRAM[0x1377] = 0x01; |
939 |
< |
} |
940 |
< |
|
941 |
< |
// Set boot volume |
942 |
< |
i16 = PrefsFindInt32("bootdrive"); |
943 |
< |
XPRAM[0x1378] = i16 >> 8; |
944 |
< |
XPRAM[0x1379] = i16 & 0xff; |
945 |
< |
i16 = PrefsFindInt32("bootdriver"); |
946 |
< |
XPRAM[0x137a] = i16 >> 8; |
947 |
< |
XPRAM[0x137b] = i16 & 0xff; |
948 |
< |
|
949 |
< |
// Create BootGlobs at top of Mac memory |
950 |
< |
memset(RAMBaseHost + RAMSize - 4096, 0, 4096); |
951 |
< |
BootGlobsAddr = RAMBase + RAMSize - 0x1c; |
952 |
< |
WriteMacInt32(BootGlobsAddr - 5 * 4, RAMBase + RAMSize); // MemTop |
953 |
< |
WriteMacInt32(BootGlobsAddr + 0 * 4, RAMBase); // First RAM bank |
954 |
< |
WriteMacInt32(BootGlobsAddr + 1 * 4, RAMSize); |
955 |
< |
WriteMacInt32(BootGlobsAddr + 2 * 4, (uint32)-1); // End of bank table |
956 |
< |
|
957 |
< |
// Init thunks |
958 |
< |
if (!ThunksInit()) |
972 |
> |
#if !EMULATED_PPC |
973 |
> |
if (vm_protect(ROMBaseHost, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
974 |
> |
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
975 |
> |
ErrorAlert(str); |
976 |
|
goto quit; |
977 |
+ |
} |
978 |
+ |
#endif |
979 |
+ |
rom_area_mapped = true; |
980 |
+ |
D(bug("ROM area at %p (%08x)\n", ROMBaseHost, ROMBase)); |
981 |
|
|
982 |
< |
// Init drivers |
983 |
< |
SonyInit(); |
984 |
< |
DiskInit(); |
985 |
< |
CDROMInit(); |
965 |
< |
SCSIInit(); |
966 |
< |
|
967 |
< |
// Init external file system |
968 |
< |
ExtFSInit(); |
969 |
< |
|
970 |
< |
// Init ADB |
971 |
< |
ADBInit(); |
972 |
< |
|
973 |
< |
// Init audio |
974 |
< |
AudioInit(); |
975 |
< |
|
976 |
< |
// Init network |
977 |
< |
EtherInit(); |
978 |
< |
|
979 |
< |
// Init serial ports |
980 |
< |
SerialInit(); |
981 |
< |
|
982 |
< |
// Init Time Manager |
983 |
< |
TimerInit(); |
984 |
< |
|
985 |
< |
// Init clipboard |
986 |
< |
ClipInit(); |
982 |
> |
if (RAMBase > ROMBase) { |
983 |
> |
ErrorAlert(GetString(STR_RAM_HIGHER_THAN_ROM_ERR)); |
984 |
> |
goto quit; |
985 |
> |
} |
986 |
|
|
987 |
< |
// Init video |
988 |
< |
if (!VideoInit()) |
987 |
> |
// Load Mac ROM |
988 |
> |
if (!load_mac_rom()) |
989 |
|
goto quit; |
990 |
|
|
991 |
< |
// Install ROM patches |
992 |
< |
if (!PatchROM()) { |
994 |
< |
ErrorAlert(GetString(STR_UNSUPPORTED_ROM_TYPE_ERR)); |
991 |
> |
// Initialize everything |
992 |
> |
if (!InitAll(vmdir)) |
993 |
|
goto quit; |
994 |
< |
} |
994 |
> |
D(bug("Initialization complete\n")); |
995 |
|
|
996 |
|
// Clear caches (as we loaded and patched code) and write protect ROM |
997 |
|
#if !EMULATED_PPC |
998 |
< |
MakeExecutable(0, ROM_BASE, ROM_AREA_SIZE); |
998 |
> |
flush_icache_range(ROMBase, ROMBase + ROM_AREA_SIZE); |
999 |
|
#endif |
1000 |
|
vm_protect(ROMBaseHost, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_EXECUTE); |
1001 |
|
|
1004 |
– |
// Initialize Kernel Data |
1005 |
– |
memset(kernel_data, 0, sizeof(KernelData)); |
1006 |
– |
if (ROMType == ROMTYPE_NEWWORLD) { |
1007 |
– |
uint32 of_dev_tree = SheepMem::Reserve(4 * sizeof(uint32)); |
1008 |
– |
Mac_memset(of_dev_tree, 0, 4 * sizeof(uint32)); |
1009 |
– |
uint32 vector_lookup_tbl = SheepMem::Reserve(128); |
1010 |
– |
uint32 vector_mask_tbl = SheepMem::Reserve(64); |
1011 |
– |
memset((uint8 *)kernel_data + 0xb80, 0x3d, 0x80); |
1012 |
– |
Mac_memset(vector_lookup_tbl, 0, 128); |
1013 |
– |
Mac_memset(vector_mask_tbl, 0, 64); |
1014 |
– |
kernel_data->v[0xb80 >> 2] = htonl(ROM_BASE); |
1015 |
– |
kernel_data->v[0xb84 >> 2] = htonl(of_dev_tree); // OF device tree base |
1016 |
– |
kernel_data->v[0xb90 >> 2] = htonl(vector_lookup_tbl); |
1017 |
– |
kernel_data->v[0xb94 >> 2] = htonl(vector_mask_tbl); |
1018 |
– |
kernel_data->v[0xb98 >> 2] = htonl(ROM_BASE); // OpenPIC base |
1019 |
– |
kernel_data->v[0xbb0 >> 2] = htonl(0); // ADB base |
1020 |
– |
kernel_data->v[0xc20 >> 2] = htonl(RAMSize); |
1021 |
– |
kernel_data->v[0xc24 >> 2] = htonl(RAMSize); |
1022 |
– |
kernel_data->v[0xc30 >> 2] = htonl(RAMSize); |
1023 |
– |
kernel_data->v[0xc34 >> 2] = htonl(RAMSize); |
1024 |
– |
kernel_data->v[0xc38 >> 2] = htonl(0x00010020); |
1025 |
– |
kernel_data->v[0xc3c >> 2] = htonl(0x00200001); |
1026 |
– |
kernel_data->v[0xc40 >> 2] = htonl(0x00010000); |
1027 |
– |
kernel_data->v[0xc50 >> 2] = htonl(RAMBase); |
1028 |
– |
kernel_data->v[0xc54 >> 2] = htonl(RAMSize); |
1029 |
– |
kernel_data->v[0xf60 >> 2] = htonl(PVR); |
1030 |
– |
kernel_data->v[0xf64 >> 2] = htonl(CPUClockSpeed); // clock-frequency |
1031 |
– |
kernel_data->v[0xf68 >> 2] = htonl(BusClockSpeed); // bus-frequency |
1032 |
– |
kernel_data->v[0xf6c >> 2] = htonl(TimebaseSpeed); // timebase-frequency |
1033 |
– |
} else { |
1034 |
– |
kernel_data->v[0xc80 >> 2] = htonl(RAMSize); |
1035 |
– |
kernel_data->v[0xc84 >> 2] = htonl(RAMSize); |
1036 |
– |
kernel_data->v[0xc90 >> 2] = htonl(RAMSize); |
1037 |
– |
kernel_data->v[0xc94 >> 2] = htonl(RAMSize); |
1038 |
– |
kernel_data->v[0xc98 >> 2] = htonl(0x00010020); |
1039 |
– |
kernel_data->v[0xc9c >> 2] = htonl(0x00200001); |
1040 |
– |
kernel_data->v[0xca0 >> 2] = htonl(0x00010000); |
1041 |
– |
kernel_data->v[0xcb0 >> 2] = htonl(RAMBase); |
1042 |
– |
kernel_data->v[0xcb4 >> 2] = htonl(RAMSize); |
1043 |
– |
kernel_data->v[0xf80 >> 2] = htonl(PVR); |
1044 |
– |
kernel_data->v[0xf84 >> 2] = htonl(CPUClockSpeed); // clock-frequency |
1045 |
– |
kernel_data->v[0xf88 >> 2] = htonl(BusClockSpeed); // bus-frequency |
1046 |
– |
kernel_data->v[0xf8c >> 2] = htonl(TimebaseSpeed); // timebase-frequency |
1047 |
– |
} |
1048 |
– |
|
1049 |
– |
// Initialize extra low memory |
1050 |
– |
D(bug("Initializing Low Memory...\n")); |
1051 |
– |
Mac_memset(0, 0, 0x3000); |
1052 |
– |
WriteMacInt32(XLM_SIGNATURE, FOURCC('B','a','a','h')); // Signature to detect SheepShaver |
1053 |
– |
WriteMacInt32(XLM_KERNEL_DATA, KernelDataAddr); // For trap replacement routines |
1054 |
– |
WriteMacInt32(XLM_PVR, PVR); // Theoretical PVR |
1055 |
– |
WriteMacInt32(XLM_BUS_CLOCK, BusClockSpeed); // For DriverServicesLib patch |
1056 |
– |
WriteMacInt16(XLM_EXEC_RETURN_OPCODE, M68K_EXEC_RETURN); // For Execute68k() (RTS from the executed 68k code will jump here and end 68k mode) |
1057 |
– |
WriteMacInt32(XLM_ZERO_PAGE, SheepMem::ZeroPage()); // Pointer to read-only page with all bits set to 0 |
1058 |
– |
#if !EMULATED_PPC |
1059 |
– |
WriteMacInt32(XLM_TOC, (uint32)TOC); // TOC pointer of emulator |
1060 |
– |
#endif |
1061 |
– |
WriteMacInt32(XLM_ETHER_INIT, NativeFunction(NATIVE_ETHER_INIT)); // DLPI ethernet driver functions |
1062 |
– |
WriteMacInt32(XLM_ETHER_TERM, NativeFunction(NATIVE_ETHER_TERM)); |
1063 |
– |
WriteMacInt32(XLM_ETHER_OPEN, NativeFunction(NATIVE_ETHER_OPEN)); |
1064 |
– |
WriteMacInt32(XLM_ETHER_CLOSE, NativeFunction(NATIVE_ETHER_CLOSE)); |
1065 |
– |
WriteMacInt32(XLM_ETHER_WPUT, NativeFunction(NATIVE_ETHER_WPUT)); |
1066 |
– |
WriteMacInt32(XLM_ETHER_RSRV, NativeFunction(NATIVE_ETHER_RSRV)); |
1067 |
– |
WriteMacInt32(XLM_VIDEO_DOIO, NativeFunction(NATIVE_VIDEO_DO_DRIVER_IO)); |
1068 |
– |
D(bug("Low Memory initialized\n")); |
1069 |
– |
|
1002 |
|
// Start 60Hz thread |
1003 |
|
tick_thread_cancel = false; |
1004 |
|
tick_thread_active = (pthread_create(&tick_thread, NULL, tick_func, NULL) == 0); |
1020 |
|
sigill_action.sa_restorer = NULL; |
1021 |
|
#endif |
1022 |
|
if (sigaction(SIGILL, &sigill_action, NULL) < 0) { |
1023 |
< |
sprintf(str, GetString(STR_SIGILL_INSTALL_ERR), strerror(errno)); |
1023 |
> |
sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGILL", strerror(errno)); |
1024 |
|
ErrorAlert(str); |
1025 |
|
goto quit; |
1026 |
|
} |
1029 |
|
#if !EMULATED_PPC |
1030 |
|
// Install interrupt signal handler |
1031 |
|
sigemptyset(&sigusr2_action.sa_mask); |
1032 |
< |
sigusr2_action.sa_sigaction = sigusr2_handler; |
1032 |
> |
sigusr2_action.sa_sigaction = sigusr2_handler_init; |
1033 |
|
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART | SA_SIGINFO; |
1034 |
|
#ifdef HAVE_SIGNAL_SA_RESTORER |
1035 |
|
sigusr2_action.sa_restorer = NULL; |
1036 |
|
#endif |
1037 |
|
if (sigaction(SIGUSR2, &sigusr2_action, NULL) < 0) { |
1038 |
< |
sprintf(str, GetString(STR_SIGUSR2_INSTALL_ERR), strerror(errno)); |
1038 |
> |
sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGUSR2", strerror(errno)); |
1039 |
|
ErrorAlert(str); |
1040 |
|
goto quit; |
1041 |
|
} |
1092 |
|
sigaction(SIGILL, &sigill_action, NULL); |
1093 |
|
|
1094 |
|
// Delete stacks for signal handlers |
1095 |
< |
for (int i = 0; i < SIG_STACK_COUNT; i++) { |
1096 |
< |
void *sig_stack = sig_stacks[i].ss_sp; |
1097 |
< |
if (sig_stack) |
1098 |
< |
free(sig_stack); |
1167 |
< |
} |
1095 |
> |
if (sig_stack.ss_sp) |
1096 |
> |
free(sig_stack.ss_sp); |
1097 |
> |
if (extra_stack.ss_sp) |
1098 |
> |
free(extra_stack.ss_sp); |
1099 |
|
#endif |
1100 |
|
|
1101 |
< |
// Save NVRAM |
1102 |
< |
XPRAMExit(); |
1172 |
< |
|
1173 |
< |
// Exit clipboard |
1174 |
< |
ClipExit(); |
1175 |
< |
|
1176 |
< |
// Exit Time Manager |
1177 |
< |
TimerExit(); |
1178 |
< |
|
1179 |
< |
// Exit serial |
1180 |
< |
SerialExit(); |
1181 |
< |
|
1182 |
< |
// Exit network |
1183 |
< |
EtherExit(); |
1184 |
< |
|
1185 |
< |
// Exit audio |
1186 |
< |
AudioExit(); |
1187 |
< |
|
1188 |
< |
// Exit ADB |
1189 |
< |
ADBExit(); |
1190 |
< |
|
1191 |
< |
// Exit video |
1192 |
< |
VideoExit(); |
1193 |
< |
|
1194 |
< |
// Exit external file system |
1195 |
< |
ExtFSExit(); |
1196 |
< |
|
1197 |
< |
// Exit drivers |
1198 |
< |
SCSIExit(); |
1199 |
< |
CDROMExit(); |
1200 |
< |
DiskExit(); |
1201 |
< |
SonyExit(); |
1202 |
< |
|
1203 |
< |
// Delete thunks |
1204 |
< |
ThunksExit(); |
1101 |
> |
// Deinitialize everything |
1102 |
> |
ExitAll(); |
1103 |
|
|
1104 |
|
// Delete SheepShaver globals |
1105 |
|
SheepMem::Exit(); |
1106 |
|
|
1107 |
|
// Delete RAM area |
1108 |
|
if (ram_area_mapped) |
1109 |
< |
vm_release(RAMBaseHost, RAMSize); |
1109 |
> |
vm_mac_release(RAMBase, RAMSize); |
1110 |
|
|
1111 |
|
// Delete ROM area |
1112 |
|
if (rom_area_mapped) |
1113 |
< |
vm_release(ROMBaseHost, ROM_AREA_SIZE); |
1113 |
> |
vm_mac_release(ROMBase, ROM_AREA_SIZE); |
1114 |
|
|
1115 |
|
// Delete DR cache areas |
1116 |
|
if (dr_emulator_area_mapped) |
1117 |
< |
vm_release((void *)(DR_EMULATOR_BASE + NATMEM_OFFSET), DR_EMULATOR_SIZE); |
1117 |
> |
vm_mac_release(DR_EMULATOR_BASE, DR_EMULATOR_SIZE); |
1118 |
|
if (dr_cache_area_mapped) |
1119 |
< |
vm_release((void *)(DR_CACHE_BASE + NATMEM_OFFSET), DR_CACHE_SIZE); |
1222 |
< |
|
1223 |
< |
// Delete Kernel Data area |
1224 |
< |
if (kernel_area >= 0) { |
1225 |
< |
shmdt((void *)(KERNEL_DATA_BASE + NATMEM_OFFSET)); |
1226 |
< |
shmdt((void *)(KERNEL_DATA2_BASE + NATMEM_OFFSET)); |
1227 |
< |
shmctl(kernel_area, IPC_RMID, NULL); |
1228 |
< |
} |
1119 |
> |
vm_mac_release(DR_CACHE_BASE, DR_CACHE_SIZE); |
1120 |
|
|
1121 |
|
// Delete Low Memory area |
1122 |
|
if (lm_area_mapped) |
1123 |
< |
vm_release((void *)NATMEM_OFFSET, 0x3000); |
1123 |
> |
vm_mac_release(0, 0x3000); |
1124 |
|
|
1125 |
|
// Close /dev/zero |
1126 |
|
if (zero_fd > 0) |
1143 |
|
XCloseDisplay(x_display); |
1144 |
|
#endif |
1145 |
|
|
1146 |
+ |
// Notify GUI we are about to leave |
1147 |
+ |
if (gui_connection) { |
1148 |
+ |
if (rpc_method_invoke(gui_connection, RPC_METHOD_EXIT, RPC_TYPE_INVALID) == RPC_ERROR_NO_ERROR) |
1149 |
+ |
rpc_method_wait_for_reply(gui_connection, RPC_TYPE_INVALID); |
1150 |
+ |
} |
1151 |
+ |
|
1152 |
|
exit(0); |
1153 |
|
} |
1154 |
|
|
1155 |
|
|
1156 |
|
/* |
1157 |
+ |
* Initialize Kernel Data segments |
1158 |
+ |
*/ |
1159 |
+ |
|
1160 |
+ |
static bool kernel_data_init(void) |
1161 |
+ |
{ |
1162 |
+ |
char str[256]; |
1163 |
+ |
void *kernel_addr1 = NULL; |
1164 |
+ |
void *kernel_addr2 = NULL; |
1165 |
+ |
uint32 kernel_area_size = (KERNEL_AREA_SIZE + SHMLBA - 1) & -SHMLBA; |
1166 |
+ |
|
1167 |
+ |
kernel_area = shmget(IPC_PRIVATE, kernel_area_size, 0600); |
1168 |
+ |
if (kernel_area == -1) { |
1169 |
+ |
sprintf(str, GetString(STR_KD_SHMGET_ERR), strerror(errno)); |
1170 |
+ |
goto fail_shmget; |
1171 |
+ |
} |
1172 |
+ |
kernel_addr1 = Mac2HostAddr(KERNEL_DATA_BASE & -SHMLBA); |
1173 |
+ |
if (shmat(kernel_area, kernel_addr1, 0) != kernel_addr1) { |
1174 |
+ |
sprintf(str, GetString(STR_KD_SHMAT_ERR), strerror(errno)); |
1175 |
+ |
goto fail_shmat1; |
1176 |
+ |
} |
1177 |
+ |
kernel_addr2 = Mac2HostAddr(KERNEL_DATA2_BASE & -SHMLBA); |
1178 |
+ |
if (shmat(kernel_area, kernel_addr2, 0) != kernel_addr2) { |
1179 |
+ |
sprintf(str, GetString(STR_KD2_SHMAT_ERR), strerror(errno)); |
1180 |
+ |
goto fail_shmat2; |
1181 |
+ |
} |
1182 |
+ |
atexit(kernel_data_exit); |
1183 |
+ |
return true; |
1184 |
+ |
|
1185 |
+ |
fail_shmat2: |
1186 |
+ |
shmdt(kernel_addr1); |
1187 |
+ |
fail_shmat1: |
1188 |
+ |
shmctl(kernel_area, IPC_RMID, NULL); |
1189 |
+ |
fail_shmget: |
1190 |
+ |
ErrorAlert(str); |
1191 |
+ |
return false; |
1192 |
+ |
} |
1193 |
+ |
|
1194 |
+ |
|
1195 |
+ |
/* |
1196 |
+ |
* Deallocate Kernel Data segments |
1197 |
+ |
*/ |
1198 |
+ |
|
1199 |
+ |
static void kernel_data_exit(void) |
1200 |
+ |
{ |
1201 |
+ |
if (kernel_area >= 0) { |
1202 |
+ |
shmdt(Mac2HostAddr(KERNEL_DATA_BASE & -SHMLBA)); |
1203 |
+ |
shmdt(Mac2HostAddr(KERNEL_DATA2_BASE & -SHMLBA)); |
1204 |
+ |
shmctl(kernel_area, IPC_RMID, NULL); |
1205 |
+ |
kernel_area = NULL; |
1206 |
+ |
} |
1207 |
+ |
} |
1208 |
+ |
|
1209 |
+ |
|
1210 |
+ |
/* |
1211 |
|
* Jump into Mac ROM, start 680x0 emulator |
1212 |
|
*/ |
1213 |
|
|
1235 |
|
// Jump to ROM boot routine |
1236 |
|
D(bug("Jumping to ROM\n")); |
1237 |
|
#if EMULATED_PPC |
1238 |
< |
jump_to_rom(ROM_BASE + 0x310000); |
1238 |
> |
jump_to_rom(ROMBase + 0x310000); |
1239 |
|
#else |
1240 |
< |
jump_to_rom(ROM_BASE + 0x310000, (uint32)emulator_data); |
1240 |
> |
jump_to_rom(ROMBase + 0x310000, (uint32)emulator_data); |
1241 |
|
#endif |
1242 |
|
D(bug("Returned from ROM\n")); |
1243 |
|
|
1294 |
|
|
1295 |
|
|
1296 |
|
/* |
1346 |
– |
* Pause/resume emulator |
1347 |
– |
*/ |
1348 |
– |
|
1349 |
– |
void PauseEmulator(void) |
1350 |
– |
{ |
1351 |
– |
pthread_kill(emul_thread, SIGSTOP); |
1352 |
– |
} |
1353 |
– |
|
1354 |
– |
void ResumeEmulator(void) |
1355 |
– |
{ |
1356 |
– |
pthread_kill(emul_thread, SIGCONT); |
1357 |
– |
} |
1358 |
– |
|
1359 |
– |
|
1360 |
– |
/* |
1297 |
|
* Dump 68k registers |
1298 |
|
*/ |
1299 |
|
|
1323 |
|
|
1324 |
|
void MakeExecutable(int dummy, uint32 start, uint32 length) |
1325 |
|
{ |
1326 |
< |
if ((start >= ROM_BASE) && (start < (ROM_BASE + ROM_SIZE))) |
1326 |
> |
if ((start >= ROMBase) && (start < (ROMBase + ROM_SIZE))) |
1327 |
|
return; |
1328 |
|
#if EMULATED_PPC |
1329 |
|
FlushCodeCache(start, start + length); |
1330 |
|
#else |
1331 |
< |
flush_icache_range(start, (void *)(start + length)); |
1331 |
> |
flush_icache_range(start, start + length); |
1332 |
|
#endif |
1333 |
|
} |
1334 |
|
|
1335 |
|
|
1336 |
|
/* |
1401 |
– |
* Patch things after system startup (gets called by disk driver accRun routine) |
1402 |
– |
*/ |
1403 |
– |
|
1404 |
– |
void PatchAfterStartup(void) |
1405 |
– |
{ |
1406 |
– |
ExecuteNative(NATIVE_VIDEO_INSTALL_ACCEL); |
1407 |
– |
InstallExtFS(); |
1408 |
– |
} |
1409 |
– |
|
1410 |
– |
|
1411 |
– |
/* |
1337 |
|
* NVRAM watchdog thread (saves NVRAM every minute) |
1338 |
|
*/ |
1339 |
|
|
1386 |
|
sigregs *r = &sigsegv_regs; |
1387 |
|
char str[256]; |
1388 |
|
if (crash_reason == NULL) |
1389 |
< |
crash_reason = "SIGSEGV"; |
1389 |
> |
crash_reason = "SIGSEGV!"; |
1390 |
|
sprintf(str, "%s\n" |
1391 |
|
" pc %08lx lr %08lx ctr %08lx msr %08lx\n" |
1392 |
|
" xer %08lx cr %08lx \n" |
1412 |
|
printf(str); |
1413 |
|
VideoQuitFullScreen(); |
1414 |
|
|
1415 |
+ |
{ |
1416 |
+ |
static int (*backtrace_fn)(void**, int); |
1417 |
+ |
static char** (*backtrace_symbols_fn)(void* const*, int); |
1418 |
+ |
backtrace_fn = dlsym(RTLD_DEFAULT, "backtrace"); |
1419 |
+ |
backtrace_symbols_fn = dlsym(RTLD_DEFAULT, "backtrace_symbols"); |
1420 |
+ |
void *frame_ptrs[64]; |
1421 |
+ |
int count = backtrace_fn(frame_ptrs, 64); |
1422 |
+ |
char **fnames = backtrace_symbols_fn(frame_ptrs, count); |
1423 |
+ |
int i; |
1424 |
+ |
for (i = 0; i < count; i++) |
1425 |
+ |
printf("%s", fnames[i]); |
1426 |
+ |
free(fnames); |
1427 |
+ |
} |
1428 |
+ |
|
1429 |
|
#ifdef ENABLE_MON |
1430 |
|
// Start up mon in real-mode |
1431 |
|
printf("Welcome to the sheep factory.\n"); |
1450 |
|
} |
1451 |
|
|
1452 |
|
uint64 end = GetTicks_usec(); |
1453 |
< |
D(bug("%Ld ticks in %Ld usec = %f ticks/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start))); |
1453 |
> |
D(bug("%lld ticks in %lld usec = %f ticks/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start))); |
1454 |
|
return NULL; |
1455 |
|
} |
1456 |
|
|
1500 |
|
pthread_mutexattr_init(&attr); |
1501 |
|
// Initialize the mutex for priority inheritance -- |
1502 |
|
// required for accurate timing. |
1503 |
< |
#ifdef HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL |
1503 |
> |
#if defined(HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL) && !defined(__CYGWIN__) |
1504 |
|
pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT); |
1505 |
|
#endif |
1506 |
|
#if defined(HAVE_PTHREAD_MUTEXATTR_SETTYPE) && defined(PTHREAD_MUTEX_NORMAL) |
1574 |
|
#if !EMULATED_PPC |
1575 |
|
void TriggerInterrupt(void) |
1576 |
|
{ |
1577 |
< |
if (ready_for_signals) |
1577 |
> |
if (ready_for_signals) { |
1578 |
> |
idle_resume(); |
1579 |
|
pthread_kill(emul_thread, SIGUSR2); |
1580 |
+ |
} |
1581 |
|
} |
1582 |
|
#endif |
1583 |
|
|
1632 |
|
*/ |
1633 |
|
|
1634 |
|
#if !EMULATED_PPC |
1635 |
< |
static void sigusr2_handler(int sig, siginfo_t *sip, void *scp) |
1635 |
> |
void sigusr2_handler(int sig, siginfo_t *sip, void *scp) |
1636 |
|
{ |
1637 |
|
machine_regs *r = MACHINE_REGISTERS(scp); |
1638 |
|
|
1639 |
+ |
#ifdef SYSTEM_CLOBBERS_R2 |
1640 |
+ |
// Restore pointer to Thread Local Storage |
1641 |
+ |
set_r2(TOC); |
1642 |
+ |
#endif |
1643 |
+ |
#ifdef SYSTEM_CLOBBERS_R13 |
1644 |
+ |
// Restore pointer to .sdata section |
1645 |
+ |
set_r13(R13); |
1646 |
+ |
#endif |
1647 |
+ |
|
1648 |
|
#ifdef USE_SDL_VIDEO |
1649 |
|
// We must fill in the events queue in the same thread that did call SDL_SetVideoMode() |
1650 |
|
SDL_PumpEvents(); |
1670 |
|
// 68k emulator inactive, in nanokernel? |
1671 |
|
if (r->gpr(1) != KernelDataAddr) { |
1672 |
|
|
1673 |
< |
// Set extra stack for nested interrupts |
1674 |
< |
sig_stack_acquire(); |
1673 |
> |
// Set extra stack for SIGSEGV handler |
1674 |
> |
sigaltstack(&extra_stack, NULL); |
1675 |
|
|
1676 |
|
// Prepare for 68k interrupt level 1 |
1677 |
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1680 |
|
// Execute nanokernel interrupt routine (this will activate the 68k emulator) |
1681 |
|
DisableInterrupt(); |
1682 |
|
if (ROMType == ROMTYPE_NEWWORLD) |
1683 |
< |
ppc_interrupt(ROM_BASE + 0x312b1c, KernelDataAddr); |
1683 |
> |
ppc_interrupt(ROMBase + 0x312b1c, KernelDataAddr); |
1684 |
|
else |
1685 |
< |
ppc_interrupt(ROM_BASE + 0x312a3c, KernelDataAddr); |
1685 |
> |
ppc_interrupt(ROMBase + 0x312a3c, KernelDataAddr); |
1686 |
|
|
1687 |
< |
// Reset normal signal stack |
1688 |
< |
sig_stack_release(); |
1687 |
> |
// Reset normal stack |
1688 |
> |
sigaltstack(&sig_stack, NULL); |
1689 |
|
} |
1690 |
|
break; |
1691 |
|
#endif |
1696 |
|
if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) { |
1697 |
|
|
1698 |
|
// Set extra stack for SIGSEGV handler |
1699 |
< |
sig_stack_acquire(); |
1699 |
> |
sigaltstack(&extra_stack, NULL); |
1700 |
|
#if 1 |
1701 |
|
// Execute full 68k interrupt routine |
1702 |
|
M68kRegisters r; |
1722 |
|
} |
1723 |
|
} |
1724 |
|
#endif |
1725 |
< |
// Reset normal signal stack |
1726 |
< |
sig_stack_release(); |
1725 |
> |
// Reset normal stack |
1726 |
> |
sigaltstack(&sig_stack, NULL); |
1727 |
|
} |
1728 |
|
break; |
1729 |
|
#endif |
1744 |
|
// Get effective address |
1745 |
|
uint32 addr = r->dar(); |
1746 |
|
|
1747 |
+ |
#ifdef SYSTEM_CLOBBERS_R2 |
1748 |
+ |
// Restore pointer to Thread Local Storage |
1749 |
+ |
set_r2(TOC); |
1750 |
+ |
#endif |
1751 |
+ |
#ifdef SYSTEM_CLOBBERS_R13 |
1752 |
+ |
// Restore pointer to .sdata section |
1753 |
+ |
set_r13(R13); |
1754 |
+ |
#endif |
1755 |
+ |
|
1756 |
|
#if ENABLE_VOSF |
1757 |
< |
// Handle screen fault. |
1758 |
< |
extern bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction); |
1759 |
< |
if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->pc())) |
1757 |
> |
// Handle screen fault |
1758 |
> |
#if SIGSEGV_CHECK_VERSION(1,0,0) |
1759 |
> |
sigsegv_info_t si; |
1760 |
> |
si.addr = (sigsegv_address_t)addr; |
1761 |
> |
si.pc = (sigsegv_address_t)r->pc(); |
1762 |
> |
#endif |
1763 |
> |
extern bool Screen_fault_handler(sigsegv_info_t *sip); |
1764 |
> |
if (Screen_fault_handler(&si)) |
1765 |
|
return; |
1766 |
|
#endif |
1767 |
|
|
1768 |
|
num_segv++; |
1769 |
|
|
1770 |
|
// Fault in Mac ROM or RAM or DR Cache? |
1771 |
< |
bool mac_fault = (r->pc() >= ROM_BASE) && (r->pc() < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize)) || (r->pc() >= DR_CACHE_BASE && r->pc() < (DR_CACHE_BASE + DR_CACHE_SIZE)); |
1771 |
> |
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)); |
1772 |
|
if (mac_fault) { |
1773 |
|
|
1774 |
|
// "VM settings" during MacOS 8 installation |
1775 |
< |
if (r->pc() == ROM_BASE + 0x488160 && r->gpr(20) == 0xf8000000) { |
1775 |
> |
if (r->pc() == ROMBase + 0x488160 && r->gpr(20) == 0xf8000000) { |
1776 |
|
r->pc() += 4; |
1777 |
|
r->gpr(8) = 0; |
1778 |
|
return; |
1779 |
|
|
1780 |
|
// MacOS 8.5 installation |
1781 |
< |
} else if (r->pc() == ROM_BASE + 0x488140 && r->gpr(16) == 0xf8000000) { |
1781 |
> |
} else if (r->pc() == ROMBase + 0x488140 && r->gpr(16) == 0xf8000000) { |
1782 |
|
r->pc() += 4; |
1783 |
|
r->gpr(8) = 0; |
1784 |
|
return; |
1785 |
|
|
1786 |
|
// MacOS 8 serial drivers on startup |
1787 |
< |
} else if (r->pc() == ROM_BASE + 0x48e080 && (r->gpr(8) == 0xf3012002 || r->gpr(8) == 0xf3012000)) { |
1787 |
> |
} else if (r->pc() == ROMBase + 0x48e080 && (r->gpr(8) == 0xf3012002 || r->gpr(8) == 0xf3012000)) { |
1788 |
|
r->pc() += 4; |
1789 |
|
r->gpr(8) = 0; |
1790 |
|
return; |
1791 |
|
|
1792 |
|
// MacOS 8.1 serial drivers on startup |
1793 |
< |
} else if (r->pc() == ROM_BASE + 0x48c5e0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1793 |
> |
} else if (r->pc() == ROMBase + 0x48c5e0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1794 |
|
r->pc() += 4; |
1795 |
|
return; |
1796 |
< |
} else if (r->pc() == ROM_BASE + 0x4a10a0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1796 |
> |
} else if (r->pc() == ROMBase + 0x4a10a0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1797 |
|
r->pc() += 4; |
1798 |
|
return; |
1799 |
|
|
1926 |
|
|
1927 |
|
// Ignore ROM writes (including to the zero page, which is read-only) |
1928 |
|
if (transfer_type == TYPE_STORE && |
1929 |
< |
((addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) || |
1929 |
> |
((addr >= ROMBase && addr < ROMBase + ROM_SIZE) || |
1930 |
|
(addr >= SheepMem::ZeroPage() && addr < SheepMem::ZeroPage() + SheepMem::PageSize()))) { |
1931 |
|
// 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())); |
1932 |
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
2007 |
|
machine_regs *r = MACHINE_REGISTERS(scp); |
2008 |
|
char str[256]; |
2009 |
|
|
2010 |
+ |
#ifdef SYSTEM_CLOBBERS_R2 |
2011 |
+ |
// Restore pointer to Thread Local Storage |
2012 |
+ |
set_r2(TOC); |
2013 |
+ |
#endif |
2014 |
+ |
#ifdef SYSTEM_CLOBBERS_R13 |
2015 |
+ |
// Restore pointer to .sdata section |
2016 |
+ |
set_r13(R13); |
2017 |
+ |
#endif |
2018 |
+ |
|
2019 |
|
// Fault in Mac ROM or RAM? |
2020 |
< |
bool mac_fault = (r->pc() >= ROM_BASE) && (r->pc() < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize)); |
2020 |
> |
bool mac_fault = (r->pc() >= ROMBase) && (r->pc() < (ROMBase + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize)); |
2021 |
|
if (mac_fault) { |
2022 |
|
|
2023 |
|
// Get opcode and divide into fields |
2184 |
|
page_size = getpagesize(); |
2185 |
|
|
2186 |
|
// Allocate SheepShaver globals |
2187 |
< |
if (vm_acquire_fixed((char *)(base + NATMEM_OFFSET), size) < 0) |
2187 |
> |
proc = base; |
2188 |
> |
if (vm_mac_acquire_fixed(base, size) < 0) |
2189 |
|
return false; |
2190 |
|
|
2191 |
< |
// Allocate page with all bits set to 0 |
2192 |
< |
zero_page = base + size; |
2193 |
< |
uint8 *zero_page_host = (uint8 *)zero_page + NATMEM_OFFSET; |
2194 |
< |
if (vm_acquire_fixed(zero_page_host, page_size) < 0) |
2195 |
< |
return false; |
2222 |
< |
memset(zero_page_host, 0, page_size); |
2223 |
< |
if (vm_protect(zero_page_host, page_size, VM_PAGE_READ) < 0) |
2191 |
> |
// Allocate page with all bits set to 0, right in the middle |
2192 |
> |
// This is also used to catch undesired overlaps between proc and data areas |
2193 |
> |
zero_page = proc + (size / 2); |
2194 |
> |
Mac_memset(zero_page, 0, page_size); |
2195 |
> |
if (vm_protect(Mac2HostAddr(zero_page), page_size, VM_PAGE_READ) < 0) |
2196 |
|
return false; |
2197 |
|
|
2198 |
|
#if EMULATED_PPC |
2199 |
|
// Allocate alternate stack for PowerPC interrupt routine |
2200 |
< |
sig_stack = zero_page + page_size; |
2201 |
< |
if (vm_acquire_fixed((char *)(sig_stack + NATMEM_OFFSET), SIG_STACK_SIZE) < 0) |
2200 |
> |
sig_stack = base + size; |
2201 |
> |
if (vm_mac_acquire_fixed(sig_stack, SIG_STACK_SIZE) < 0) |
2202 |
|
return false; |
2203 |
|
#endif |
2204 |
|
|
2205 |
< |
top = base + size; |
2205 |
> |
data = base + size; |
2206 |
|
return true; |
2207 |
|
} |
2208 |
|
|
2209 |
|
void SheepMem::Exit(void) |
2210 |
|
{ |
2211 |
< |
if (top) { |
2211 |
> |
if (data) { |
2212 |
|
// Delete SheepShaver globals |
2213 |
< |
vm_release((void *)(base + NATMEM_OFFSET), size); |
2242 |
< |
|
2243 |
< |
// Delete zero page |
2244 |
< |
vm_release((void *)(zero_page + NATMEM_OFFSET), page_size); |
2213 |
> |
vm_mac_release(base, size); |
2214 |
|
|
2215 |
|
#if EMULATED_PPC |
2216 |
|
// Delete alternate stack for PowerPC interrupt routine |
2217 |
< |
vm_release((void *)(sig_stack + NATMEM_OFFSET), SIG_STACK_SIZE); |
2217 |
> |
vm_mac_release(sig_stack, SIG_STACK_SIZE); |
2218 |
|
#endif |
2219 |
|
} |
2220 |
|
} |
2269 |
|
|
2270 |
|
void ErrorAlert(const char *text) |
2271 |
|
{ |
2272 |
+ |
if (gui_connection) { |
2273 |
+ |
if (rpc_method_invoke(gui_connection, RPC_METHOD_ERROR_ALERT, RPC_TYPE_STRING, text, RPC_TYPE_INVALID) == RPC_ERROR_NO_ERROR && |
2274 |
+ |
rpc_method_wait_for_reply(gui_connection, RPC_TYPE_INVALID) == RPC_ERROR_NO_ERROR) |
2275 |
+ |
return; |
2276 |
+ |
} |
2277 |
|
#if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO) |
2278 |
|
if (PrefsFindBool("nogui") || x_display == NULL) { |
2279 |
|
printf(GetString(STR_SHELL_ERROR_PREFIX), text); |
2293 |
|
|
2294 |
|
void WarningAlert(const char *text) |
2295 |
|
{ |
2296 |
+ |
if (gui_connection) { |
2297 |
+ |
if (rpc_method_invoke(gui_connection, RPC_METHOD_WARNING_ALERT, RPC_TYPE_STRING, text, RPC_TYPE_INVALID) == RPC_ERROR_NO_ERROR && |
2298 |
+ |
rpc_method_wait_for_reply(gui_connection, RPC_TYPE_INVALID) == RPC_ERROR_NO_ERROR) |
2299 |
+ |
return; |
2300 |
+ |
} |
2301 |
|
#if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO) |
2302 |
|
if (PrefsFindBool("nogui") || x_display == NULL) { |
2303 |
|
printf(GetString(STR_SHELL_WARNING_PREFIX), text); |