65 |
|
* ExecutePPC (or any function that might cause a mode switch). The signal |
66 |
|
* stack is restored before exiting the SIGUSR2 handler. |
67 |
|
* |
68 |
+ |
* There is apparently another problem when processing signals. In |
69 |
+ |
* fullscreen mode, we get quick updates of the mouse position. This |
70 |
+ |
* causes an increased number of calls to TriggerInterrupt(). And, |
71 |
+ |
* since IRQ_NEST is not fully handled atomically, nested calls to |
72 |
+ |
* ppc_interrupt() may cause stack corruption to eventually crash the |
73 |
+ |
* 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? |
79 |
+ |
* |
80 |
|
* TODO: |
81 |
|
* check if SIGSEGV handler works for all registers (including FP!) |
82 |
|
*/ |
127 |
|
#include "debug.h" |
128 |
|
|
129 |
|
|
130 |
+ |
#ifdef HAVE_DIRENT_H |
131 |
+ |
#include <dirent.h> |
132 |
+ |
#endif |
133 |
+ |
|
134 |
+ |
#ifdef USE_SDL |
135 |
+ |
#include <SDL.h> |
136 |
+ |
#endif |
137 |
+ |
|
138 |
+ |
#ifndef USE_SDL_VIDEO |
139 |
|
#include <X11/Xlib.h> |
140 |
+ |
#endif |
141 |
|
|
142 |
|
#ifdef ENABLE_GTK |
143 |
|
#include <gtk/gtk.h> |
166 |
|
// Interrupts in native mode? |
167 |
|
#define INTERRUPTS_IN_NATIVE_MODE 1 |
168 |
|
|
169 |
+ |
// Number of alternate stacks for signal handlers? |
170 |
+ |
#define SIG_STACK_COUNT 4 |
171 |
+ |
|
172 |
|
|
173 |
|
// Constants |
174 |
|
const char ROM_FILE_NAME[] = "ROM"; |
175 |
|
const char ROM_FILE_NAME2[] = "Mac OS ROM"; |
176 |
|
|
177 |
+ |
#if REAL_ADDRESSING |
178 |
|
const uintptr RAM_BASE = 0x20000000; // Base address of RAM |
179 |
+ |
#else |
180 |
+ |
// FIXME: needs to be >= 0x04000000 |
181 |
+ |
const uintptr RAM_BASE = 0x10000000; // Base address of RAM |
182 |
+ |
#endif |
183 |
|
const uint32 SIG_STACK_SIZE = 0x10000; // Size of signal stack |
184 |
|
|
185 |
|
|
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 |
+ |
} |
272 |
|
#endif |
273 |
|
|
274 |
|
|
280 |
|
uint32 RAMSize; // Size of Mac RAM |
281 |
|
uint32 KernelDataAddr; // Address of Kernel Data |
282 |
|
uint32 BootGlobsAddr; // Address of BootGlobs structure at top of Mac RAM |
283 |
+ |
uint32 DRCacheAddr; // Address of DR Cache |
284 |
|
uint32 PVR; // Theoretical PVR |
285 |
|
int64 CPUClockSpeed; // Processor clock speed (Hz) |
286 |
|
int64 BusClockSpeed; // Bus clock speed (Hz) |
287 |
+ |
int64 TimebaseSpeed; // Timebase clock speed (Hz) |
288 |
+ |
uint8 *RAMBaseHost; // Base address of Mac RAM (host address space) |
289 |
+ |
uint8 *ROMBaseHost; // Base address of Mac ROM (host address space) |
290 |
|
|
291 |
|
|
292 |
|
// Global variables |
293 |
+ |
#ifndef USE_SDL_VIDEO |
294 |
|
char *x_display_name = NULL; // X11 display name |
295 |
|
Display *x_display = NULL; // X11 display handle |
296 |
|
#ifdef X11_LOCK_TYPE |
297 |
|
X11_LOCK_TYPE x_display_lock = X11_LOCK_INIT; // X11 display lock |
298 |
|
#endif |
299 |
+ |
#endif |
300 |
|
|
301 |
|
static int zero_fd = 0; // FD of /dev/zero |
302 |
|
static bool lm_area_mapped = false; // Flag: Low Memory area mmap()ped |
303 |
|
static int kernel_area = -1; // SHM ID of Kernel Data area |
304 |
|
static bool rom_area_mapped = false; // Flag: Mac ROM mmap()ped |
305 |
|
static bool ram_area_mapped = false; // Flag: Mac RAM mmap()ped |
306 |
+ |
static bool dr_cache_area_mapped = false; // Flag: Mac DR Cache mmap()ped |
307 |
+ |
static bool dr_emulator_area_mapped = false;// Flag: Mac DR Emulator mmap()ped |
308 |
|
static KernelData *kernel_data; // Pointer to Kernel Data |
309 |
|
static EmulatorData *emulator_data; |
310 |
|
|
311 |
|
static uint8 last_xpram[XPRAM_SIZE]; // Buffer for monitoring XPRAM changes |
312 |
|
|
313 |
|
static bool nvram_thread_active = false; // Flag: NVRAM watchdog installed |
314 |
+ |
static volatile bool nvram_thread_cancel; // Flag: Cancel NVRAM thread |
315 |
|
static pthread_t nvram_thread; // NVRAM watchdog |
316 |
|
static bool tick_thread_active = false; // Flag: MacOS thread installed |
317 |
+ |
static volatile bool tick_thread_cancel; // Flag: Cancel 60Hz thread |
318 |
|
static pthread_t tick_thread; // 60Hz thread |
319 |
|
static pthread_t emul_thread; // MacOS thread |
320 |
|
|
327 |
|
#else |
328 |
|
static struct sigaction sigsegv_action; // Data access exception signal (of emulator thread) |
329 |
|
static struct sigaction sigill_action; // Illegal instruction signal (of emulator thread) |
269 |
– |
static void *sig_stack = NULL; // Stack for signal handlers |
270 |
– |
static void *extra_stack = NULL; // Stack for SIGSEGV inside interrupt handler |
330 |
|
static bool emul_thread_fatal = false; // Flag: MacOS thread crashed, tick thread shall dump debug output |
331 |
|
static sigregs sigsegv_regs; // Register dump when crashed |
332 |
|
static const char *crash_reason = NULL; // Reason of the crash (SIGSEGV, SIGBUS, SIGILL) |
333 |
|
#endif |
334 |
|
|
335 |
+ |
uint32 SheepMem::page_size; // Size of a native page |
336 |
|
uintptr SheepMem::zero_page = 0; // Address of ro page filled in with zeros |
337 |
|
uintptr SheepMem::base = 0x60000000; // Address of SheepShaver data |
338 |
|
uintptr SheepMem::top = 0; // Top of SheepShaver data (stack like storage) |
344 |
|
static void *nvram_func(void *arg); |
345 |
|
static void *tick_func(void *arg); |
346 |
|
#if EMULATED_PPC |
287 |
– |
static void sigusr2_handler(int sig); |
347 |
|
extern void emul_ppc(uint32 start); |
348 |
|
extern void init_emul_ppc(void); |
349 |
|
extern void exit_emul_ppc(void); |
350 |
+ |
sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t); |
351 |
|
#else |
352 |
|
static void sigusr2_handler(int sig, siginfo_t *sip, void *scp); |
353 |
|
static void sigsegv_handler(int sig, siginfo_t *sip, void *scp); |
438 |
|
int main(int argc, char **argv) |
439 |
|
{ |
440 |
|
char str[256]; |
381 |
– |
uint32 *boot_globs; |
441 |
|
int16 i16; |
442 |
|
int rom_fd; |
443 |
|
FILE *proc_file; |
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) { |
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; |
535 |
+ |
} |
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; |
545 |
+ |
} |
546 |
+ |
#endif |
547 |
+ |
|
548 |
+ |
#if !EMULATED_PPC |
549 |
+ |
// 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 |
+ |
} |
574 |
+ |
#endif |
575 |
+ |
|
576 |
+ |
// Initialize VM system |
577 |
+ |
vm_init(); |
578 |
+ |
|
579 |
|
// Get system info |
580 |
|
PVR = 0x00040000; // Default: 604 |
581 |
|
CPUClockSpeed = 100000000; // Default: 100MHz |
582 |
|
BusClockSpeed = 100000000; // Default: 100MHz |
583 |
< |
#if !EMULATED_PPC |
583 |
> |
TimebaseSpeed = 25000000; // Default: 25MHz |
584 |
> |
#if EMULATED_PPC |
585 |
> |
PVR = 0x000c0000; // Default: 7400 (with AltiVec) |
586 |
> |
#elif defined(__APPLE__) && defined(__MACH__) |
587 |
> |
proc_file = popen("ioreg -c IOPlatformDevice", "r"); |
588 |
> |
if (proc_file) { |
589 |
> |
char line[256]; |
590 |
> |
bool powerpc_node = false; |
591 |
> |
while (fgets(line, sizeof(line) - 1, proc_file)) { |
592 |
> |
// Read line |
593 |
> |
int len = strlen(line); |
594 |
> |
if (len == 0) |
595 |
> |
continue; |
596 |
> |
line[len - 1] = 0; |
597 |
> |
|
598 |
> |
// Parse line |
599 |
> |
if (strstr(line, "o PowerPC,")) |
600 |
> |
powerpc_node = true; |
601 |
> |
else if (powerpc_node) { |
602 |
> |
uint32 value; |
603 |
> |
char head[256]; |
604 |
> |
if (sscanf(line, "%[ |]\"cpu-version\" = <%x>", head, &value) == 2) |
605 |
> |
PVR = value; |
606 |
> |
else if (sscanf(line, "%[ |]\"clock-frequency\" = <%x>", head, &value) == 2) |
607 |
> |
CPUClockSpeed = value; |
608 |
> |
else if (sscanf(line, "%[ |]\"bus-frequency\" = <%x>", head, &value) == 2) |
609 |
> |
BusClockSpeed = value; |
610 |
> |
else if (sscanf(line, "%[ |]\"timebase-frequency\" = <%x>", head, &value) == 2) |
611 |
> |
TimebaseSpeed = value; |
612 |
> |
else if (strchr(line, '}')) |
613 |
> |
powerpc_node = false; |
614 |
> |
} |
615 |
> |
} |
616 |
> |
fclose(proc_file); |
617 |
> |
} else { |
618 |
> |
sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno)); |
619 |
> |
WarningAlert(str); |
620 |
> |
} |
621 |
> |
#else |
622 |
|
proc_file = fopen("/proc/cpuinfo", "r"); |
623 |
|
if (proc_file) { |
624 |
+ |
// CPU specs from Linux kernel |
625 |
+ |
// TODO: make it more generic with features (e.g. AltiVec) and |
626 |
+ |
// cache information and friends for NameRegistry |
627 |
+ |
static const struct { |
628 |
+ |
uint32 pvr_mask; |
629 |
+ |
uint32 pvr_value; |
630 |
+ |
const char *cpu_name; |
631 |
+ |
} |
632 |
+ |
cpu_specs[] = { |
633 |
+ |
{ 0xffff0000, 0x00010000, "601" }, |
634 |
+ |
{ 0xffff0000, 0x00030000, "603" }, |
635 |
+ |
{ 0xffff0000, 0x00060000, "603e" }, |
636 |
+ |
{ 0xffff0000, 0x00070000, "603ev" }, |
637 |
+ |
{ 0xffff0000, 0x00040000, "604" }, |
638 |
+ |
{ 0xfffff000, 0x00090000, "604e" }, |
639 |
+ |
{ 0xffff0000, 0x00090000, "604r" }, |
640 |
+ |
{ 0xffff0000, 0x000a0000, "604ev" }, |
641 |
+ |
{ 0xffffffff, 0x00084202, "740/750" }, |
642 |
+ |
{ 0xfffff000, 0x00083000, "745/755" }, |
643 |
+ |
{ 0xfffffff0, 0x00080100, "750CX" }, |
644 |
+ |
{ 0xfffffff0, 0x00082200, "750CX" }, |
645 |
+ |
{ 0xfffffff0, 0x00082210, "750CXe" }, |
646 |
+ |
{ 0xffffff00, 0x70000100, "750FX" }, |
647 |
+ |
{ 0xffffffff, 0x70000200, "750FX" }, |
648 |
+ |
{ 0xffff0000, 0x70000000, "750FX" }, |
649 |
+ |
{ 0xffff0000, 0x70020000, "750GX" }, |
650 |
+ |
{ 0xffff0000, 0x00080000, "740/750" }, |
651 |
+ |
{ 0xffffffff, 0x000c1101, "7400 (1.1)" }, |
652 |
+ |
{ 0xffff0000, 0x000c0000, "7400" }, |
653 |
+ |
{ 0xffff0000, 0x800c0000, "7410" }, |
654 |
+ |
{ 0xffffffff, 0x80000200, "7450" }, |
655 |
+ |
{ 0xffffffff, 0x80000201, "7450" }, |
656 |
+ |
{ 0xffff0000, 0x80000000, "7450" }, |
657 |
+ |
{ 0xffffff00, 0x80010100, "7455" }, |
658 |
+ |
{ 0xffffffff, 0x80010200, "7455" }, |
659 |
+ |
{ 0xffff0000, 0x80010000, "7455" }, |
660 |
+ |
{ 0xffff0000, 0x80020000, "7457" }, |
661 |
+ |
{ 0xffff0000, 0x80030000, "7447A" }, |
662 |
+ |
{ 0x7fff0000, 0x00810000, "82xx" }, |
663 |
+ |
{ 0x7fff0000, 0x00820000, "8280" }, |
664 |
+ |
{ 0xffff0000, 0x00400000, "Power3 (630)" }, |
665 |
+ |
{ 0xffff0000, 0x00410000, "Power3 (630+)" }, |
666 |
+ |
{ 0xffff0000, 0x00360000, "I-star" }, |
667 |
+ |
{ 0xffff0000, 0x00370000, "S-star" }, |
668 |
+ |
{ 0xffff0000, 0x00350000, "Power4" }, |
669 |
+ |
{ 0xffff0000, 0x00390000, "PPC970" }, |
670 |
+ |
{ 0, 0, 0 } |
671 |
+ |
}; |
672 |
+ |
|
673 |
|
char line[256]; |
674 |
|
while(fgets(line, 255, proc_file)) { |
675 |
|
// Read line |
681 |
|
// Parse line |
682 |
|
int i; |
683 |
|
char value[256]; |
684 |
< |
if (sscanf(line, "cpu : %s", value) == 1) { |
685 |
< |
if (strcmp(value, "601") == 0) |
686 |
< |
PVR = 0x00010000; |
687 |
< |
else if (strcmp(value, "603") == 0) |
688 |
< |
PVR = 0x00030000; |
689 |
< |
else if (strcmp(value, "604") == 0) |
690 |
< |
PVR = 0x00040000; |
691 |
< |
else if (strcmp(value, "603e") == 0) |
692 |
< |
PVR = 0x00060000; |
693 |
< |
else if (strcmp(value, "603ev") == 0) |
694 |
< |
PVR = 0x00070000; |
474 |
< |
else if (strcmp(value, "604e") == 0) |
475 |
< |
PVR = 0x00090000; |
476 |
< |
else if (strcmp(value, "604ev5") == 0) |
477 |
< |
PVR = 0x000a0000; |
478 |
< |
else if (strcmp(value, "750") == 0) |
479 |
< |
PVR = 0x00080000; |
480 |
< |
else if (strcmp(value, "821") == 0) |
481 |
< |
PVR = 0x00320000; |
482 |
< |
else if (strcmp(value, "860") == 0) |
483 |
< |
PVR = 0x00500000; |
484 |
< |
else |
684 |
> |
if (sscanf(line, "cpu : %[0-9A-Za-a]", value) == 1) { |
685 |
> |
// Search by name |
686 |
> |
const char *cpu_name = NULL; |
687 |
> |
for (int i = 0; cpu_specs[i].pvr_mask != 0; i++) { |
688 |
> |
if (strcmp(cpu_specs[i].cpu_name, value) == 0) { |
689 |
> |
cpu_name = cpu_specs[i].cpu_name; |
690 |
> |
PVR = cpu_specs[i].pvr_value; |
691 |
> |
break; |
692 |
> |
} |
693 |
> |
} |
694 |
> |
if (cpu_name == NULL) |
695 |
|
printf("WARNING: Unknown CPU type '%s', assuming 604\n", value); |
696 |
+ |
else |
697 |
+ |
printf("Found a PowerPC %s processor\n", cpu_name); |
698 |
|
} |
699 |
|
if (sscanf(line, "clock : %dMHz", &i) == 1) |
700 |
|
CPUClockSpeed = BusClockSpeed = i * 1000000; |
704 |
|
sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno)); |
705 |
|
WarningAlert(str); |
706 |
|
} |
707 |
+ |
|
708 |
+ |
// Get actual bus frequency |
709 |
+ |
proc_file = fopen("/proc/device-tree/clock-frequency", "r"); |
710 |
+ |
if (proc_file) { |
711 |
+ |
union { uint8 b[4]; uint32 l; } value; |
712 |
+ |
if (fread(value.b, sizeof(value), 1, proc_file) == 1) |
713 |
+ |
BusClockSpeed = value.l; |
714 |
+ |
fclose(proc_file); |
715 |
+ |
} |
716 |
+ |
|
717 |
+ |
// Get actual timebase frequency |
718 |
+ |
TimebaseSpeed = BusClockSpeed / 4; |
719 |
+ |
DIR *cpus_dir; |
720 |
+ |
if ((cpus_dir = opendir("/proc/device-tree/cpus")) != NULL) { |
721 |
+ |
struct dirent *cpu_entry; |
722 |
+ |
while ((cpu_entry = readdir(cpus_dir)) != NULL) { |
723 |
+ |
if (strstr(cpu_entry->d_name, "PowerPC,") == cpu_entry->d_name) { |
724 |
+ |
char timebase_freq_node[256]; |
725 |
+ |
sprintf(timebase_freq_node, "/proc/device-tree/cpus/%s/timebase-frequency", cpu_entry->d_name); |
726 |
+ |
proc_file = fopen(timebase_freq_node, "r"); |
727 |
+ |
if (proc_file) { |
728 |
+ |
union { uint8 b[4]; uint32 l; } value; |
729 |
+ |
if (fread(value.b, sizeof(value), 1, proc_file) == 1) |
730 |
+ |
TimebaseSpeed = value.l; |
731 |
+ |
fclose(proc_file); |
732 |
+ |
} |
733 |
+ |
} |
734 |
+ |
} |
735 |
+ |
closedir(cpus_dir); |
736 |
+ |
} |
737 |
|
#endif |
738 |
+ |
// Remap any newer G4/G5 processor to plain G4 for compatibility |
739 |
+ |
switch (PVR >> 16) { |
740 |
+ |
case 0x8000: // 7450 |
741 |
+ |
case 0x8001: // 7455 |
742 |
+ |
case 0x8002: // 7457 |
743 |
+ |
case 0x0039: // 970 |
744 |
+ |
PVR = 0x000c0000; // 7400 |
745 |
+ |
break; |
746 |
+ |
} |
747 |
|
D(bug("PVR: %08x (assumed)\n", PVR)); |
748 |
|
|
749 |
|
// Init system routines |
769 |
|
|
770 |
|
#ifndef PAGEZERO_HACK |
771 |
|
// Create Low Memory area (0x0000..0x3000) |
772 |
< |
if (vm_acquire_fixed((char *)0, 0x3000) < 0) { |
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; |
784 |
|
ErrorAlert(str); |
785 |
|
goto quit; |
786 |
|
} |
787 |
< |
if (shmat(kernel_area, (void *)KERNEL_DATA_BASE, 0) < 0) { |
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, 0) < 0) { |
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); |
795 |
|
goto quit; |
796 |
|
} |
797 |
< |
kernel_data = (KernelData *)KERNEL_DATA_BASE; |
797 |
> |
kernel_data = (KernelData *)(KERNEL_DATA_BASE + NATMEM_OFFSET); |
798 |
|
emulator_data = &kernel_data->ed; |
799 |
|
KernelDataAddr = KERNEL_DATA_BASE; |
800 |
< |
D(bug("Kernel Data at %p, Emulator Data at %p\n", kernel_data, emulator_data)); |
800 |
> |
D(bug("Kernel Data at %p (%08x)\n", kernel_data, KERNEL_DATA_BASE)); |
801 |
> |
D(bug("Emulator Data at %p (%08x)\n", emulator_data, KERNEL_DATA_BASE + offsetof(KernelData, ed))); |
802 |
> |
|
803 |
> |
// Create area for DR Cache |
804 |
> |
if (vm_acquire_fixed((void *)(DR_EMULATOR_BASE + NATMEM_OFFSET), DR_EMULATOR_SIZE) < 0) { |
805 |
> |
sprintf(str, GetString(STR_DR_EMULATOR_MMAP_ERR), strerror(errno)); |
806 |
> |
ErrorAlert(str); |
807 |
> |
goto quit; |
808 |
> |
} |
809 |
> |
dr_emulator_area_mapped = true; |
810 |
> |
if (vm_acquire_fixed((void *)(DR_CACHE_BASE + NATMEM_OFFSET), DR_CACHE_SIZE) < 0) { |
811 |
> |
sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno)); |
812 |
> |
ErrorAlert(str); |
813 |
> |
goto quit; |
814 |
> |
} |
815 |
> |
dr_cache_area_mapped = true; |
816 |
> |
#if !EMULATED_PPC |
817 |
> |
if (vm_protect((char *)DR_CACHE_BASE, DR_CACHE_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
818 |
> |
sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno)); |
819 |
> |
ErrorAlert(str); |
820 |
> |
goto quit; |
821 |
> |
} |
822 |
> |
#endif |
823 |
> |
DRCacheAddr = DR_CACHE_BASE; |
824 |
> |
D(bug("DR Cache at %p\n", DRCacheAddr)); |
825 |
|
|
826 |
|
// Create area for SheepShaver data |
827 |
|
if (!SheepMem::Init()) { |
831 |
|
} |
832 |
|
|
833 |
|
// Create area for Mac ROM |
834 |
< |
if (vm_acquire_fixed((char *)ROM_BASE, ROM_AREA_SIZE) < 0) { |
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((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
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 %08x\n", ROM_BASE)); |
848 |
> |
D(bug("ROM area at %p (%08x)\n", ROMBaseHost, ROM_BASE)); |
849 |
|
|
850 |
|
// Create area for Mac RAM |
851 |
|
RAMSize = PrefsFindInt32("ramsize"); |
854 |
|
RAMSize = 8*1024*1024; |
855 |
|
} |
856 |
|
|
857 |
< |
if (vm_acquire_fixed((char *)RAM_BASE, RAMSize) < 0) { |
857 |
> |
RAMBaseHost = (uint8 *)(RAM_BASE + NATMEM_OFFSET); |
858 |
> |
if (vm_acquire_fixed(RAMBaseHost, RAMSize) < 0) { |
859 |
|
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
860 |
|
ErrorAlert(str); |
861 |
|
goto quit; |
862 |
|
} |
863 |
|
#if !EMULATED_PPC |
864 |
< |
if (vm_protect((char *)RAM_BASE, RAMSize, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
864 |
> |
if (vm_protect(RAMBaseHost, RAMSize, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
865 |
|
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
866 |
|
ErrorAlert(str); |
867 |
|
goto quit; |
869 |
|
#endif |
870 |
|
RAMBase = RAM_BASE; |
871 |
|
ram_area_mapped = true; |
872 |
< |
D(bug("RAM area at %08x\n", RAMBase)); |
872 |
> |
D(bug("RAM area at %p (%08x)\n", RAMBaseHost, RAMBase)); |
873 |
|
|
874 |
|
if (RAMBase > ROM_BASE) { |
875 |
|
ErrorAlert(GetString(STR_RAM_HIGHER_THAN_ROM_ERR)); |
908 |
|
// Load NVRAM |
909 |
|
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; |
947 |
|
XPRAM[0x137b] = i16 & 0xff; |
948 |
|
|
949 |
|
// Create BootGlobs at top of Mac memory |
950 |
< |
memset((void *)(RAMBase + RAMSize - 4096), 0, 4096); |
950 |
> |
memset(RAMBaseHost + RAMSize - 4096, 0, 4096); |
951 |
|
BootGlobsAddr = RAMBase + RAMSize - 0x1c; |
952 |
< |
boot_globs = (uint32 *)BootGlobsAddr; |
953 |
< |
boot_globs[-5] = htonl(RAMBase + RAMSize); // MemTop |
954 |
< |
boot_globs[0] = htonl(RAMBase); // First RAM bank |
955 |
< |
boot_globs[1] = htonl(RAMSize); |
649 |
< |
boot_globs[2] = htonl((uint32)-1); // End of bank table |
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()) |
997 |
|
|
998 |
|
// Clear caches (as we loaded and patched code) and write protect ROM |
999 |
|
#if !EMULATED_PPC |
1000 |
< |
MakeExecutable(0, (void *)ROM_BASE, ROM_AREA_SIZE); |
1000 |
> |
MakeExecutable(0, ROM_BASE, ROM_AREA_SIZE); |
1001 |
|
#endif |
1002 |
< |
vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_EXECUTE); |
1002 |
> |
vm_protect(ROMBaseHost, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_EXECUTE); |
1003 |
|
|
1004 |
|
// Initialize Kernel Data |
1005 |
|
memset(kernel_data, 0, sizeof(KernelData)); |
1006 |
|
if (ROMType == ROMTYPE_NEWWORLD) { |
1007 |
< |
uintptr of_dev_tree = SheepMem::Reserve(4 * sizeof(uint32)); |
1008 |
< |
memset((void *)of_dev_tree, 0, 4 * sizeof(uint32)); |
1009 |
< |
uintptr vector_lookup_tbl = SheepMem::Reserve(128); |
1010 |
< |
uintptr vector_mask_tbl = SheepMem::Reserve(64); |
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 |
< |
memset((void *)vector_lookup_tbl, 0, 128); |
1013 |
< |
memset((void *)vector_mask_tbl, 0, 64); |
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); |
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); |
1031 |
< |
kernel_data->v[0xf68 >> 2] = htonl(BusClockSpeed); |
1032 |
< |
kernel_data->v[0xf6c >> 2] = htonl(CPUClockSpeed); |
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); |
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); |
1045 |
< |
kernel_data->v[0xf88 >> 2] = htonl(BusClockSpeed); |
1046 |
< |
kernel_data->v[0xf8c >> 2] = htonl(CPUClockSpeed); |
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 |
< |
memset(NULL, 0, 0x3000); |
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 |
1068 |
|
D(bug("Low Memory initialized\n")); |
1069 |
|
|
1070 |
|
// Start 60Hz thread |
1071 |
+ |
tick_thread_cancel = false; |
1072 |
|
tick_thread_active = (pthread_create(&tick_thread, NULL, tick_func, NULL) == 0); |
1073 |
|
D(bug("Tick thread installed (%ld)\n", tick_thread)); |
1074 |
|
|
1075 |
|
// Start NVRAM watchdog thread |
1076 |
|
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
1077 |
+ |
nvram_thread_cancel = false; |
1078 |
|
nvram_thread_active = (pthread_create(&nvram_thread, NULL, nvram_func, NULL) == 0); |
1079 |
|
D(bug("NVRAM thread installed (%ld)\n", nvram_thread)); |
1080 |
|
|
1081 |
|
#if !EMULATED_PPC |
774 |
– |
// Create and install stacks for signal handlers |
775 |
– |
sig_stack = malloc(SIG_STACK_SIZE); |
776 |
– |
D(bug("Signal stack at %p\n", sig_stack)); |
777 |
– |
if (sig_stack == NULL) { |
778 |
– |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
779 |
– |
goto quit; |
780 |
– |
} |
781 |
– |
extra_stack = malloc(SIG_STACK_SIZE); |
782 |
– |
D(bug("Extra stack at %p\n", extra_stack)); |
783 |
– |
if (extra_stack == NULL) { |
784 |
– |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
785 |
– |
goto quit; |
786 |
– |
} |
787 |
– |
struct sigaltstack new_stack; |
788 |
– |
new_stack.ss_sp = sig_stack; |
789 |
– |
new_stack.ss_flags = 0; |
790 |
– |
new_stack.ss_size = SIG_STACK_SIZE; |
791 |
– |
if (sigaltstack(&new_stack, NULL) < 0) { |
792 |
– |
sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno)); |
793 |
– |
ErrorAlert(str); |
794 |
– |
goto quit; |
795 |
– |
} |
796 |
– |
#endif |
797 |
– |
|
798 |
– |
#if !EMULATED_PPC |
799 |
– |
// Install SIGSEGV and SIGBUS handlers |
800 |
– |
sigemptyset(&sigsegv_action.sa_mask); // Block interrupts during SEGV handling |
801 |
– |
sigaddset(&sigsegv_action.sa_mask, SIGUSR2); |
802 |
– |
sigsegv_action.sa_sigaction = sigsegv_handler; |
803 |
– |
sigsegv_action.sa_flags = SA_ONSTACK | SA_SIGINFO; |
804 |
– |
#ifdef HAVE_SIGNAL_SA_RESTORER |
805 |
– |
sigsegv_action.sa_restorer = NULL; |
806 |
– |
#endif |
807 |
– |
if (sigaction(SIGSEGV, &sigsegv_action, NULL) < 0) { |
808 |
– |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
809 |
– |
ErrorAlert(str); |
810 |
– |
goto quit; |
811 |
– |
} |
812 |
– |
if (sigaction(SIGBUS, &sigsegv_action, NULL) < 0) { |
813 |
– |
sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno)); |
814 |
– |
ErrorAlert(str); |
815 |
– |
goto quit; |
816 |
– |
} |
817 |
– |
|
1082 |
|
// Install SIGILL handler |
1083 |
|
sigemptyset(&sigill_action.sa_mask); // Block interrupts during ILL handling |
1084 |
|
sigaddset(&sigill_action.sa_mask, SIGUSR2); |
1133 |
|
|
1134 |
|
// Stop 60Hz thread |
1135 |
|
if (tick_thread_active) { |
1136 |
+ |
tick_thread_cancel = true; |
1137 |
|
pthread_cancel(tick_thread); |
1138 |
|
pthread_join(tick_thread, NULL); |
1139 |
|
} |
1140 |
|
|
1141 |
|
// Stop NVRAM watchdog thread |
1142 |
|
if (nvram_thread_active) { |
1143 |
+ |
nvram_thread_cancel = true; |
1144 |
|
pthread_cancel(nvram_thread); |
1145 |
|
pthread_join(nvram_thread, NULL); |
1146 |
|
} |
1158 |
|
sigill_action.sa_handler = SIG_DFL; |
1159 |
|
sigill_action.sa_flags = 0; |
1160 |
|
sigaction(SIGILL, &sigill_action, NULL); |
1161 |
+ |
|
1162 |
+ |
// Delete stacks for signal handlers |
1163 |
+ |
for (int i = 0; i < SIG_STACK_COUNT; i++) { |
1164 |
+ |
void *sig_stack = sig_stacks[i].ss_sp; |
1165 |
+ |
if (sig_stack) |
1166 |
+ |
free(sig_stack); |
1167 |
+ |
} |
1168 |
|
#endif |
1169 |
|
|
1170 |
|
// Save NVRAM |
1208 |
|
|
1209 |
|
// Delete RAM area |
1210 |
|
if (ram_area_mapped) |
1211 |
< |
vm_release((char *)RAM_BASE, RAMSize); |
1211 |
> |
vm_release(RAMBaseHost, RAMSize); |
1212 |
|
|
1213 |
|
// Delete ROM area |
1214 |
|
if (rom_area_mapped) |
1215 |
< |
vm_release((char *)ROM_BASE, ROM_AREA_SIZE); |
1215 |
> |
vm_release(ROMBaseHost, ROM_AREA_SIZE); |
1216 |
> |
|
1217 |
> |
// Delete DR cache areas |
1218 |
> |
if (dr_emulator_area_mapped) |
1219 |
> |
vm_release((void *)(DR_EMULATOR_BASE + NATMEM_OFFSET), DR_EMULATOR_SIZE); |
1220 |
> |
if (dr_cache_area_mapped) |
1221 |
> |
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); |
1226 |
< |
shmdt((void *)KERNEL_DATA2_BASE); |
1225 |
> |
shmdt((void *)(KERNEL_DATA_BASE + NATMEM_OFFSET)); |
1226 |
> |
shmdt((void *)(KERNEL_DATA2_BASE + NATMEM_OFFSET)); |
1227 |
|
shmctl(kernel_area, IPC_RMID, NULL); |
1228 |
|
} |
1229 |
|
|
1230 |
|
// Delete Low Memory area |
1231 |
|
if (lm_area_mapped) |
1232 |
< |
munmap((char *)0x0000, 0x3000); |
1232 |
> |
vm_release((void *)NATMEM_OFFSET, 0x3000); |
1233 |
|
|
1234 |
|
// Close /dev/zero |
1235 |
|
if (zero_fd > 0) |
1247 |
|
#endif |
1248 |
|
|
1249 |
|
// Close X11 server connection |
1250 |
+ |
#ifndef USE_SDL_VIDEO |
1251 |
|
if (x_display) |
1252 |
|
XCloseDisplay(x_display); |
1253 |
+ |
#endif |
1254 |
|
|
1255 |
|
exit(0); |
1256 |
|
} |
1385 |
|
* Make code executable |
1386 |
|
*/ |
1387 |
|
|
1388 |
< |
void MakeExecutable(int dummy, void *start, uint32 length) |
1388 |
> |
void MakeExecutable(int dummy, uint32 start, uint32 length) |
1389 |
|
{ |
1390 |
< |
if (((uintptr)start >= ROM_BASE) && ((uintptr)start < (ROM_BASE + ROM_SIZE))) |
1390 |
> |
if ((start >= ROM_BASE) && (start < (ROM_BASE + ROM_SIZE))) |
1391 |
|
return; |
1392 |
|
#if EMULATED_PPC |
1393 |
< |
FlushCodeCache((uintptr)start, (uintptr)start + length); |
1393 |
> |
FlushCodeCache(start, start + length); |
1394 |
|
#else |
1395 |
< |
flush_icache_range(start, (void *)((uintptr)start + length)); |
1395 |
> |
flush_icache_range(start, (void *)(start + length)); |
1396 |
|
#endif |
1397 |
|
} |
1398 |
|
|
1412 |
|
* NVRAM watchdog thread (saves NVRAM every minute) |
1413 |
|
*/ |
1414 |
|
|
1415 |
< |
static void *nvram_func(void *arg) |
1415 |
> |
static void nvram_watchdog(void) |
1416 |
|
{ |
1417 |
< |
struct timespec req = {60, 0}; // 1 minute |
1417 |
> |
if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) { |
1418 |
> |
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
1419 |
> |
SaveXPRAM(); |
1420 |
> |
} |
1421 |
> |
} |
1422 |
|
|
1423 |
< |
for (;;) { |
1424 |
< |
pthread_testcancel(); |
1425 |
< |
nanosleep(&req, NULL); |
1426 |
< |
pthread_testcancel(); |
1427 |
< |
if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) { |
1428 |
< |
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
1144 |
< |
SaveXPRAM(); |
1145 |
< |
} |
1423 |
> |
static void *nvram_func(void *arg) |
1424 |
> |
{ |
1425 |
> |
while (!nvram_thread_cancel) { |
1426 |
> |
for (int i=0; i<60 && !nvram_thread_cancel; i++) |
1427 |
> |
Delay_usec(999999); // Only wait 1 second so we quit promptly when nvram_thread_cancel becomes true |
1428 |
> |
nvram_watchdog(); |
1429 |
|
} |
1430 |
|
return NULL; |
1431 |
|
} |
1438 |
|
static void *tick_func(void *arg) |
1439 |
|
{ |
1440 |
|
int tick_counter = 0; |
1441 |
< |
struct timespec req = {0, 16625000}; |
1441 |
> |
uint64 start = GetTicks_usec(); |
1442 |
> |
int64 ticks = 0; |
1443 |
> |
uint64 next = GetTicks_usec(); |
1444 |
|
|
1445 |
< |
for (;;) { |
1445 |
> |
while (!tick_thread_cancel) { |
1446 |
|
|
1447 |
|
// Wait |
1448 |
< |
nanosleep(&req, NULL); |
1448 |
> |
next += 16625; |
1449 |
> |
int64 delay = next - GetTicks_usec(); |
1450 |
> |
if (delay > 0) |
1451 |
> |
Delay_usec(delay); |
1452 |
> |
else if (delay < -16625) |
1453 |
> |
next = GetTicks_usec(); |
1454 |
> |
ticks++; |
1455 |
|
|
1456 |
|
#if !EMULATED_PPC |
1457 |
|
// Did we crash? |
1509 |
|
TriggerInterrupt(); |
1510 |
|
} |
1511 |
|
} |
1512 |
+ |
|
1513 |
+ |
uint64 end = GetTicks_usec(); |
1514 |
+ |
D(bug("%Ld ticks in %Ld usec = %f ticks/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start))); |
1515 |
|
return NULL; |
1516 |
|
} |
1517 |
|
|
1632 |
|
* Trigger signal USR2 from another thread |
1633 |
|
*/ |
1634 |
|
|
1635 |
< |
#if !EMULATED_PPC || ASYNC_IRQ |
1635 |
> |
#if !EMULATED_PPC |
1636 |
|
void TriggerInterrupt(void) |
1637 |
|
{ |
1638 |
|
if (ready_for_signals) |
1664 |
|
|
1665 |
|
void DisableInterrupt(void) |
1666 |
|
{ |
1667 |
+ |
#if EMULATED_PPC |
1668 |
+ |
WriteMacInt32(XLM_IRQ_NEST, int32(ReadMacInt32(XLM_IRQ_NEST)) + 1); |
1669 |
+ |
#else |
1670 |
|
atomic_add((int *)XLM_IRQ_NEST, 1); |
1671 |
+ |
#endif |
1672 |
|
} |
1673 |
|
|
1674 |
|
|
1678 |
|
|
1679 |
|
void EnableInterrupt(void) |
1680 |
|
{ |
1681 |
+ |
#if EMULATED_PPC |
1682 |
+ |
WriteMacInt32(XLM_IRQ_NEST, int32(ReadMacInt32(XLM_IRQ_NEST)) - 1); |
1683 |
+ |
#else |
1684 |
|
atomic_add((int *)XLM_IRQ_NEST, -1); |
1685 |
+ |
#endif |
1686 |
|
} |
1687 |
|
|
1688 |
|
|
1690 |
|
* USR2 handler |
1691 |
|
*/ |
1692 |
|
|
1693 |
< |
#if EMULATED_PPC |
1392 |
< |
static void sigusr2_handler(int sig) |
1393 |
< |
{ |
1394 |
< |
#if ASYNC_IRQ |
1395 |
< |
extern void HandleInterrupt(void); |
1396 |
< |
HandleInterrupt(); |
1397 |
< |
#endif |
1398 |
< |
} |
1399 |
< |
#else |
1693 |
> |
#if !EMULATED_PPC |
1694 |
|
static void sigusr2_handler(int sig, siginfo_t *sip, void *scp) |
1695 |
|
{ |
1696 |
|
machine_regs *r = MACHINE_REGISTERS(scp); |
1697 |
|
|
1698 |
+ |
#ifdef USE_SDL_VIDEO |
1699 |
+ |
// We must fill in the events queue in the same thread that did call SDL_SetVideoMode() |
1700 |
+ |
SDL_PumpEvents(); |
1701 |
+ |
#endif |
1702 |
+ |
|
1703 |
|
// Do nothing if interrupts are disabled |
1704 |
|
if (*(int32 *)XLM_IRQ_NEST > 0) |
1705 |
|
return; |
1719 |
|
case MODE_NATIVE: |
1720 |
|
// 68k emulator inactive, in nanokernel? |
1721 |
|
if (r->gpr(1) != KernelDataAddr) { |
1722 |
+ |
|
1723 |
+ |
// Set extra stack for nested interrupts |
1724 |
+ |
sig_stack_acquire(); |
1725 |
+ |
|
1726 |
|
// Prepare for 68k interrupt level 1 |
1727 |
|
WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1); |
1728 |
|
WriteMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc, ReadMacInt32(ntohl(kernel_data->v[0x658 >> 2]) + 0xdc) | ntohl(kernel_data->v[0x674 >> 2])); |
1729 |
|
|
1730 |
|
// Execute nanokernel interrupt routine (this will activate the 68k emulator) |
1731 |
< |
atomic_add((int32 *)XLM_IRQ_NEST, 1); |
1731 |
> |
DisableInterrupt(); |
1732 |
|
if (ROMType == ROMTYPE_NEWWORLD) |
1733 |
|
ppc_interrupt(ROM_BASE + 0x312b1c, KernelDataAddr); |
1734 |
|
else |
1735 |
|
ppc_interrupt(ROM_BASE + 0x312a3c, KernelDataAddr); |
1736 |
+ |
|
1737 |
+ |
// Reset normal signal stack |
1738 |
+ |
sig_stack_release(); |
1739 |
|
} |
1740 |
|
break; |
1741 |
|
#endif |
1746 |
|
if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) { |
1747 |
|
|
1748 |
|
// Set extra stack for SIGSEGV handler |
1749 |
< |
struct sigaltstack new_stack; |
1444 |
< |
new_stack.ss_sp = extra_stack; |
1445 |
< |
new_stack.ss_flags = 0; |
1446 |
< |
new_stack.ss_size = SIG_STACK_SIZE; |
1447 |
< |
sigaltstack(&new_stack, NULL); |
1749 |
> |
sig_stack_acquire(); |
1750 |
|
#if 1 |
1751 |
|
// Execute full 68k interrupt routine |
1752 |
|
M68kRegisters r; |
1773 |
|
} |
1774 |
|
#endif |
1775 |
|
// Reset normal signal stack |
1776 |
< |
new_stack.ss_sp = sig_stack; |
1475 |
< |
new_stack.ss_flags = 0; |
1476 |
< |
new_stack.ss_size = SIG_STACK_SIZE; |
1477 |
< |
sigaltstack(&new_stack, NULL); |
1776 |
> |
sig_stack_release(); |
1777 |
|
} |
1778 |
|
break; |
1779 |
|
#endif |
1803 |
|
|
1804 |
|
num_segv++; |
1805 |
|
|
1806 |
< |
// Fault in Mac ROM or RAM? |
1807 |
< |
bool mac_fault = (r->pc() >= ROM_BASE) && (r->pc() < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize)); |
1806 |
> |
// Fault in Mac ROM or RAM or DR Cache? |
1807 |
> |
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)); |
1808 |
|
if (mac_fault) { |
1809 |
|
|
1810 |
|
// "VM settings" during MacOS 8 installation |
1832 |
|
} else if (r->pc() == ROM_BASE + 0x4a10a0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1833 |
|
r->pc() += 4; |
1834 |
|
return; |
1835 |
+ |
|
1836 |
+ |
// MacOS 8.6 serial drivers on startup (with DR Cache and OldWorld ROM) |
1837 |
+ |
} else if ((r->pc() - DR_CACHE_BASE) < DR_CACHE_SIZE && (r->gpr(16) == 0xf3012002 || r->gpr(16) == 0xf3012000)) { |
1838 |
+ |
r->pc() += 4; |
1839 |
+ |
return; |
1840 |
+ |
} else if ((r->pc() - DR_CACHE_BASE) < DR_CACHE_SIZE && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1841 |
+ |
r->pc() += 4; |
1842 |
+ |
return; |
1843 |
|
} |
1844 |
|
|
1845 |
|
// Get opcode and divide into fields |
1960 |
|
#endif |
1961 |
|
} |
1962 |
|
|
1963 |
< |
// Ignore ROM writes |
1964 |
< |
if (transfer_type == TYPE_STORE && addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) { |
1963 |
> |
// Ignore ROM writes (including to the zero page, which is read-only) |
1964 |
> |
if (transfer_type == TYPE_STORE && |
1965 |
> |
((addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) || |
1966 |
> |
(addr >= SheepMem::ZeroPage() && addr < SheepMem::ZeroPage() + SheepMem::PageSize()))) { |
1967 |
|
// 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())); |
1968 |
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1969 |
|
r->gpr(ra) = addr; |
2207 |
|
|
2208 |
|
bool SheepMem::Init(void) |
2209 |
|
{ |
2210 |
< |
const int page_size = getpagesize(); |
2210 |
> |
// Size of a native page |
2211 |
> |
page_size = getpagesize(); |
2212 |
|
|
2213 |
|
// Allocate SheepShaver globals |
2214 |
< |
if (vm_acquire_fixed((char *)base, size) < 0) |
2214 |
> |
if (vm_acquire_fixed((char *)(base + NATMEM_OFFSET), size) < 0) |
2215 |
|
return false; |
2216 |
|
|
2217 |
|
// Allocate page with all bits set to 0 |
2218 |
|
zero_page = base + size; |
2219 |
< |
if (vm_acquire_fixed((char *)zero_page, page_size) < 0) |
2219 |
> |
uint8 *zero_page_host = (uint8 *)zero_page + NATMEM_OFFSET; |
2220 |
> |
if (vm_acquire_fixed(zero_page_host, page_size) < 0) |
2221 |
|
return false; |
2222 |
< |
memset((char *)zero_page, 0, page_size); |
2223 |
< |
if (vm_protect((char *)zero_page, page_size, VM_PAGE_READ) < 0) |
2222 |
> |
memset(zero_page_host, 0, page_size); |
2223 |
> |
if (vm_protect(zero_page_host, page_size, VM_PAGE_READ) < 0) |
2224 |
|
return false; |
2225 |
|
|
2226 |
|
#if EMULATED_PPC |
2227 |
|
// Allocate alternate stack for PowerPC interrupt routine |
2228 |
|
sig_stack = zero_page + page_size; |
2229 |
< |
if (vm_acquire_fixed((char *)sig_stack, SIG_STACK_SIZE) < 0) |
2229 |
> |
if (vm_acquire_fixed((char *)(sig_stack + NATMEM_OFFSET), SIG_STACK_SIZE) < 0) |
2230 |
|
return false; |
2231 |
|
#endif |
2232 |
|
|
2237 |
|
void SheepMem::Exit(void) |
2238 |
|
{ |
2239 |
|
if (top) { |
1929 |
– |
const int page_size = getpagesize(); |
1930 |
– |
|
2240 |
|
// Delete SheepShaver globals |
2241 |
< |
vm_release((void *)base, size); |
2241 |
> |
vm_release((void *)(base + NATMEM_OFFSET), size); |
2242 |
|
|
2243 |
|
// Delete zero page |
2244 |
< |
vm_release((void *)zero_page, page_size); |
2244 |
> |
vm_release((void *)(zero_page + NATMEM_OFFSET), page_size); |
2245 |
|
|
2246 |
|
#if EMULATED_PPC |
2247 |
|
// Delete alternate stack for PowerPC interrupt routine |
2248 |
< |
vm_release((void *)sig_stack, SIG_STACK_SIZE); |
2248 |
> |
vm_release((void *)(sig_stack + NATMEM_OFFSET), SIG_STACK_SIZE); |
2249 |
|
#endif |
2250 |
|
} |
2251 |
|
} |
2300 |
|
|
2301 |
|
void ErrorAlert(const char *text) |
2302 |
|
{ |
2303 |
< |
#ifdef ENABLE_GTK |
2303 |
> |
#if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO) |
2304 |
|
if (PrefsFindBool("nogui") || x_display == NULL) { |
2305 |
|
printf(GetString(STR_SHELL_ERROR_PREFIX), text); |
2306 |
|
return; |
2319 |
|
|
2320 |
|
void WarningAlert(const char *text) |
2321 |
|
{ |
2322 |
< |
#ifdef ENABLE_GTK |
2322 |
> |
#if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO) |
2323 |
|
if (PrefsFindBool("nogui") || x_display == NULL) { |
2324 |
|
printf(GetString(STR_SHELL_WARNING_PREFIX), text); |
2325 |
|
return; |