28 |
|
#include "macos_util.h" |
29 |
|
#include "block-alloc.hpp" |
30 |
|
#include "sigsegv.h" |
31 |
– |
#include "spcflags.h" |
31 |
|
#include "cpu/ppc/ppc-cpu.hpp" |
32 |
|
#include "cpu/ppc/ppc-operations.hpp" |
33 |
+ |
#include "cpu/ppc/ppc-instructions.hpp" |
34 |
|
|
35 |
|
// Used for NativeOp trampolines |
36 |
|
#include "video.h" |
37 |
|
#include "name_registry.h" |
38 |
|
#include "serial.h" |
39 |
+ |
#include "ether.h" |
40 |
|
|
41 |
|
#include <stdio.h> |
42 |
|
|
48 |
|
#define DEBUG 0 |
49 |
|
#include "debug.h" |
50 |
|
|
51 |
+ |
// Emulation time statistics |
52 |
+ |
#define EMUL_TIME_STATS 1 |
53 |
+ |
|
54 |
+ |
#if EMUL_TIME_STATS |
55 |
+ |
static clock_t emul_start_time; |
56 |
+ |
static uint32 interrupt_count = 0; |
57 |
+ |
static clock_t interrupt_time = 0; |
58 |
+ |
static uint32 exec68k_count = 0; |
59 |
+ |
static clock_t exec68k_time = 0; |
60 |
+ |
static uint32 native_exec_count = 0; |
61 |
+ |
static clock_t native_exec_time = 0; |
62 |
+ |
static uint32 macos_exec_count = 0; |
63 |
+ |
static clock_t macos_exec_time = 0; |
64 |
+ |
#endif |
65 |
+ |
|
66 |
|
static void enter_mon(void) |
67 |
|
{ |
68 |
|
// Start up mon in real-mode |
90 |
|
// Pointer to Kernel Data |
91 |
|
static KernelData * const kernel_data = (KernelData *)KERNEL_DATA_BASE; |
92 |
|
|
93 |
+ |
// SIGSEGV handler |
94 |
+ |
static sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t); |
95 |
+ |
|
96 |
|
|
97 |
|
/** |
98 |
|
* PowerPC emulator glue with special 'sheep' opcodes |
99 |
|
**/ |
100 |
|
|
101 |
< |
struct sheepshaver_exec_return { }; |
101 |
> |
enum { |
102 |
> |
PPC_I(SHEEP) = PPC_I(MAX), |
103 |
> |
PPC_I(SHEEP_MAX) |
104 |
> |
}; |
105 |
|
|
106 |
|
class sheepshaver_cpu |
107 |
|
: public powerpc_cpu |
137 |
|
void interrupt(uint32 entry); |
138 |
|
void handle_interrupt(); |
139 |
|
|
118 |
– |
// spcflags for interrupts handling |
119 |
– |
static uint32 spcflags; |
120 |
– |
|
140 |
|
// Lazy memory allocator (one item at a time) |
141 |
|
void *operator new(size_t size) |
142 |
|
{ return allocator_helper< sheepshaver_cpu, lazy_allocator >::allocate(); } |
145 |
|
// FIXME: really make surre array allocation fail at link time? |
146 |
|
void *operator new[](size_t); |
147 |
|
void operator delete[](void *p); |
148 |
+ |
|
149 |
+ |
// Make sure the SIGSEGV handler can access CPU registers |
150 |
+ |
friend sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t); |
151 |
|
}; |
152 |
|
|
131 |
– |
uint32 sheepshaver_cpu::spcflags = 0; |
153 |
|
lazy_allocator< sheepshaver_cpu > allocator_helper< sheepshaver_cpu, lazy_allocator >::allocator; |
154 |
|
|
155 |
|
sheepshaver_cpu::sheepshaver_cpu() |
169 |
|
|
170 |
|
static const instr_info_t sheep_ii_table[] = { |
171 |
|
{ "sheep", |
172 |
< |
(execute_fn)&sheepshaver_cpu::execute_sheep, |
172 |
> |
(execute_pmf)&sheepshaver_cpu::execute_sheep, |
173 |
|
NULL, |
174 |
+ |
PPC_I(SHEEP), |
175 |
|
D_form, 6, 0, CFLOW_JUMP | CFLOW_TRAP |
176 |
|
} |
177 |
|
}; |
211 |
|
break; |
212 |
|
|
213 |
|
case 1: // EXEC_RETURN |
214 |
< |
throw sheepshaver_exec_return(); |
214 |
> |
spcflags().set(SPCFLAG_CPU_EXEC_RETURN); |
215 |
|
break; |
216 |
|
|
217 |
|
case 2: // EXEC_NATIVE |
247 |
|
// Execution loop |
248 |
|
void sheepshaver_cpu::execute(uint32 entry, bool enable_cache) |
249 |
|
{ |
250 |
< |
try { |
229 |
< |
powerpc_cpu::execute(entry, enable_cache); |
230 |
< |
} |
231 |
< |
catch (sheepshaver_exec_return const &) { |
232 |
< |
// Nothing, simply return |
233 |
< |
} |
234 |
< |
catch (...) { |
235 |
< |
printf("ERROR: execute() received an unknown exception!\n"); |
236 |
< |
QuitEmulator(); |
237 |
< |
} |
250 |
> |
powerpc_cpu::execute(entry, enable_cache); |
251 |
|
} |
252 |
|
|
253 |
|
// Handle MacOS interrupt |
254 |
|
void sheepshaver_cpu::interrupt(uint32 entry) |
255 |
|
{ |
256 |
+ |
#if EMUL_TIME_STATS |
257 |
+ |
interrupt_count++; |
258 |
+ |
const clock_t interrupt_start = clock(); |
259 |
+ |
#endif |
260 |
+ |
|
261 |
|
#if !MULTICORE_CPU |
262 |
|
// Save program counters and branch registers |
263 |
|
uint32 saved_pc = pc(); |
311 |
|
ctr()= saved_ctr; |
312 |
|
gpr(1) = saved_sp; |
313 |
|
#endif |
314 |
+ |
|
315 |
+ |
#if EMUL_TIME_STATS |
316 |
+ |
interrupt_time += (clock() - interrupt_start); |
317 |
+ |
#endif |
318 |
|
} |
319 |
|
|
320 |
|
// Execute 68k routine |
321 |
|
void sheepshaver_cpu::execute_68k(uint32 entry, M68kRegisters *r) |
322 |
|
{ |
323 |
+ |
#if EMUL_TIME_STATS |
324 |
+ |
exec68k_count++; |
325 |
+ |
const clock_t exec68k_start = clock(); |
326 |
+ |
#endif |
327 |
+ |
|
328 |
|
#if SAFE_EXEC_68K |
329 |
|
if (ReadMacInt32(XLM_RUN_MODE) != MODE_EMUL_OP) |
330 |
|
printf("FATAL: Execute68k() not called from EMUL_OP mode\n"); |
407 |
|
lr() = saved_lr; |
408 |
|
ctr()= saved_ctr; |
409 |
|
set_cr(saved_cr); |
410 |
+ |
|
411 |
+ |
#if EMUL_TIME_STATS |
412 |
+ |
exec68k_time += (clock() - exec68k_start); |
413 |
+ |
#endif |
414 |
|
} |
415 |
|
|
416 |
|
// Call MacOS PPC code |
417 |
|
uint32 sheepshaver_cpu::execute_macos_code(uint32 tvect, int nargs, uint32 const *args) |
418 |
|
{ |
419 |
+ |
#if EMUL_TIME_STATS |
420 |
+ |
macos_exec_count++; |
421 |
+ |
const clock_t macos_exec_start = clock(); |
422 |
+ |
#endif |
423 |
+ |
|
424 |
|
// Save program counters and branch registers |
425 |
|
uint32 saved_pc = pc(); |
426 |
|
uint32 saved_lr = lr(); |
459 |
|
lr() = saved_lr; |
460 |
|
ctr()= saved_ctr; |
461 |
|
|
462 |
+ |
#if EMUL_TIME_STATS |
463 |
+ |
macos_exec_time += (clock() - macos_exec_start); |
464 |
+ |
#endif |
465 |
+ |
|
466 |
|
return retval; |
467 |
|
} |
468 |
|
|
567 |
|
if ((addr - ROM_BASE) < ROM_SIZE) |
568 |
|
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
569 |
|
|
570 |
< |
// Ignore all other faults, if requested |
571 |
< |
if (PrefsFindBool("ignoresegv")) |
572 |
< |
return SIGSEGV_RETURN_FAILURE; |
570 |
> |
// Get program counter of target CPU |
571 |
> |
sheepshaver_cpu * const cpu = current_cpu; |
572 |
> |
const uint32 pc = cpu->pc(); |
573 |
> |
|
574 |
> |
// Fault in Mac ROM or RAM? |
575 |
> |
bool mac_fault = (pc >= ROM_BASE) && (pc < (ROM_BASE + ROM_AREA_SIZE)) || (pc >= RAMBase) && (pc < (RAMBase + RAMSize)); |
576 |
> |
if (mac_fault) { |
577 |
> |
|
578 |
> |
// "VM settings" during MacOS 8 installation |
579 |
> |
if (pc == ROM_BASE + 0x488160 && cpu->gpr(20) == 0xf8000000) |
580 |
> |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
581 |
> |
|
582 |
> |
// MacOS 8.5 installation |
583 |
> |
else if (pc == ROM_BASE + 0x488140 && cpu->gpr(16) == 0xf8000000) |
584 |
> |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
585 |
> |
|
586 |
> |
// MacOS 8 serial drivers on startup |
587 |
> |
else if (pc == ROM_BASE + 0x48e080 && (cpu->gpr(8) == 0xf3012002 || cpu->gpr(8) == 0xf3012000)) |
588 |
> |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
589 |
> |
|
590 |
> |
// MacOS 8.1 serial drivers on startup |
591 |
> |
else if (pc == ROM_BASE + 0x48c5e0 && (cpu->gpr(20) == 0xf3012002 || cpu->gpr(20) == 0xf3012000)) |
592 |
> |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
593 |
> |
else if (pc == ROM_BASE + 0x4a10a0 && (cpu->gpr(20) == 0xf3012002 || cpu->gpr(20) == 0xf3012000)) |
594 |
> |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
595 |
> |
|
596 |
> |
// Ignore all other faults, if requested |
597 |
> |
if (PrefsFindBool("ignoresegv")) |
598 |
> |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
599 |
> |
} |
600 |
|
#else |
601 |
|
#error "FIXME: You don't have the capability to skip instruction within signal handlers" |
602 |
|
#endif |
633 |
|
mon_add_command("regs", dump_registers, "regs Dump PowerPC registers\n"); |
634 |
|
mon_add_command("log", dump_log, "log Dump PowerPC emulation log\n"); |
635 |
|
#endif |
636 |
+ |
|
637 |
+ |
#if EMUL_TIME_STATS |
638 |
+ |
emul_start_time = clock(); |
639 |
+ |
#endif |
640 |
+ |
} |
641 |
+ |
|
642 |
+ |
/* |
643 |
+ |
* Deinitialize emulation |
644 |
+ |
*/ |
645 |
+ |
|
646 |
+ |
void exit_emul_ppc(void) |
647 |
+ |
{ |
648 |
+ |
#if EMUL_TIME_STATS |
649 |
+ |
clock_t emul_end_time = clock(); |
650 |
+ |
|
651 |
+ |
printf("### Statistics for SheepShaver emulation parts\n"); |
652 |
+ |
const clock_t emul_time = emul_end_time - emul_start_time; |
653 |
+ |
printf("Total emulation time : %.1f sec\n", double(emul_time) / double(CLOCKS_PER_SEC)); |
654 |
+ |
printf("Total interrupt count: %d (%2.1f Hz)\n", interrupt_count, |
655 |
+ |
(double(interrupt_count) * CLOCKS_PER_SEC) / double(emul_time)); |
656 |
+ |
|
657 |
+ |
#define PRINT_STATS(LABEL, VAR_PREFIX) do { \ |
658 |
+ |
printf("Total " LABEL " count : %d\n", VAR_PREFIX##_count); \ |
659 |
+ |
printf("Total " LABEL " time : %.1f sec (%.1f%%)\n", \ |
660 |
+ |
double(VAR_PREFIX##_time) / double(CLOCKS_PER_SEC), \ |
661 |
+ |
100.0 * double(VAR_PREFIX##_time) / double(emul_time)); \ |
662 |
+ |
} while (0) |
663 |
+ |
|
664 |
+ |
PRINT_STATS("Execute68k[Trap] execution", exec68k); |
665 |
+ |
PRINT_STATS("NativeOp execution", native_exec); |
666 |
+ |
PRINT_STATS("MacOS routine execution", macos_exec); |
667 |
+ |
|
668 |
+ |
#undef PRINT_STATS |
669 |
+ |
printf("\n"); |
670 |
+ |
#endif |
671 |
+ |
|
672 |
+ |
delete main_cpu; |
673 |
+ |
#if MULTICORE_CPU |
674 |
+ |
delete interrupt_cpu; |
675 |
+ |
#endif |
676 |
|
} |
677 |
|
|
678 |
|
/* |
714 |
|
void sheepshaver_cpu::handle_interrupt(void) |
715 |
|
{ |
716 |
|
// Do nothing if interrupts are disabled |
717 |
< |
if (int32(ReadMacInt32(XLM_IRQ_NEST)) > 0) |
717 |
> |
if (*(int32 *)XLM_IRQ_NEST > 0) |
718 |
|
return; |
719 |
|
|
720 |
|
// Do nothing if there is no interrupt pending |
838 |
|
|
839 |
|
static void NativeOp(int selector) |
840 |
|
{ |
841 |
+ |
#if EMUL_TIME_STATS |
842 |
+ |
native_exec_count++; |
843 |
+ |
const clock_t native_exec_start = clock(); |
844 |
+ |
#endif |
845 |
+ |
|
846 |
|
switch (selector) { |
847 |
|
case NATIVE_PATCH_NAME_REGISTRY: |
848 |
|
DoPatchNameRegistry(); |
857 |
|
GPR(3) = (int32)(int16)VideoDoDriverIO((void *)GPR(3), (void *)GPR(4), |
858 |
|
(void *)GPR(5), GPR(6), GPR(7)); |
859 |
|
break; |
860 |
< |
case NATIVE_GET_RESOURCE: |
861 |
< |
get_resource(); |
860 |
> |
#ifdef WORDS_BIGENDIAN |
861 |
> |
case NATIVE_ETHER_IRQ: |
862 |
> |
EtherIRQ(); |
863 |
|
break; |
864 |
< |
case NATIVE_GET_1_RESOURCE: |
865 |
< |
get_1_resource(); |
864 |
> |
case NATIVE_ETHER_INIT: |
865 |
> |
GPR(3) = InitStreamModule((void *)GPR(3)); |
866 |
|
break; |
867 |
< |
case NATIVE_GET_IND_RESOURCE: |
868 |
< |
get_ind_resource(); |
867 |
> |
case NATIVE_ETHER_TERM: |
868 |
> |
TerminateStreamModule(); |
869 |
|
break; |
870 |
< |
case NATIVE_GET_1_IND_RESOURCE: |
871 |
< |
get_1_ind_resource(); |
870 |
> |
case NATIVE_ETHER_OPEN: |
871 |
> |
GPR(3) = ether_open((queue_t *)GPR(3), (void *)GPR(4), GPR(5), GPR(6), (void*)GPR(7)); |
872 |
> |
break; |
873 |
> |
case NATIVE_ETHER_CLOSE: |
874 |
> |
GPR(3) = ether_close((queue_t *)GPR(3), GPR(4), (void *)GPR(5)); |
875 |
> |
break; |
876 |
> |
case NATIVE_ETHER_WPUT: |
877 |
> |
GPR(3) = ether_wput((queue_t *)GPR(3), (mblk_t *)GPR(4)); |
878 |
|
break; |
879 |
< |
case NATIVE_R_GET_RESOURCE: |
880 |
< |
r_get_resource(); |
879 |
> |
case NATIVE_ETHER_RSRV: |
880 |
> |
GPR(3) = ether_rsrv((queue_t *)GPR(3)); |
881 |
|
break; |
882 |
+ |
#else |
883 |
+ |
case NATIVE_ETHER_INIT: |
884 |
+ |
// FIXME: needs more complicated thunks |
885 |
+ |
GPR(3) = false; |
886 |
+ |
break; |
887 |
+ |
#endif |
888 |
|
case NATIVE_SERIAL_NOTHING: |
889 |
|
case NATIVE_SERIAL_OPEN: |
890 |
|
case NATIVE_SERIAL_PRIME_IN: |
905 |
|
GPR(3) = serial_callbacks[selector - NATIVE_SERIAL_NOTHING](GPR(3), GPR(4)); |
906 |
|
break; |
907 |
|
} |
908 |
+ |
case NATIVE_GET_RESOURCE: |
909 |
+ |
case NATIVE_GET_1_RESOURCE: |
910 |
+ |
case NATIVE_GET_IND_RESOURCE: |
911 |
+ |
case NATIVE_GET_1_IND_RESOURCE: |
912 |
+ |
case NATIVE_R_GET_RESOURCE: { |
913 |
+ |
typedef void (*GetResourceCallback)(void); |
914 |
+ |
static const GetResourceCallback get_resource_callbacks[] = { |
915 |
+ |
get_resource, |
916 |
+ |
get_1_resource, |
917 |
+ |
get_ind_resource, |
918 |
+ |
get_1_ind_resource, |
919 |
+ |
r_get_resource |
920 |
+ |
}; |
921 |
+ |
get_resource_callbacks[selector - NATIVE_GET_RESOURCE](); |
922 |
+ |
break; |
923 |
+ |
} |
924 |
|
case NATIVE_DISABLE_INTERRUPT: |
925 |
|
DisableInterrupt(); |
926 |
|
break; |
935 |
|
QuitEmulator(); |
936 |
|
break; |
937 |
|
} |
938 |
+ |
|
939 |
+ |
#if EMUL_TIME_STATS |
940 |
+ |
native_exec_time += (clock() - native_exec_start); |
941 |
+ |
#endif |
942 |
|
} |
943 |
|
|
944 |
|
/* |