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 |
118 |
|
uint32 get_cr() const { return cr().get(); } |
119 |
|
void set_cr(uint32 v) { cr().set(v); } |
120 |
|
|
99 |
– |
// Execution loop |
100 |
– |
void execute(uint32 entry, bool enable_cache = false); |
101 |
– |
|
121 |
|
// Execute 68k routine |
122 |
|
void execute_68k(uint32 entry, M68kRegisters *r); |
123 |
|
|
134 |
|
void interrupt(uint32 entry); |
135 |
|
void handle_interrupt(); |
136 |
|
|
118 |
– |
// spcflags for interrupts handling |
119 |
– |
static uint32 spcflags; |
120 |
– |
|
137 |
|
// Lazy memory allocator (one item at a time) |
138 |
|
void *operator new(size_t size) |
139 |
|
{ return allocator_helper< sheepshaver_cpu, lazy_allocator >::allocate(); } |
142 |
|
// FIXME: really make surre array allocation fail at link time? |
143 |
|
void *operator new[](size_t); |
144 |
|
void operator delete[](void *p); |
145 |
+ |
|
146 |
+ |
// Make sure the SIGSEGV handler can access CPU registers |
147 |
+ |
friend sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t); |
148 |
|
}; |
149 |
|
|
131 |
– |
uint32 sheepshaver_cpu::spcflags = 0; |
150 |
|
lazy_allocator< sheepshaver_cpu > allocator_helper< sheepshaver_cpu, lazy_allocator >::allocator; |
151 |
|
|
152 |
|
sheepshaver_cpu::sheepshaver_cpu() |
166 |
|
|
167 |
|
static const instr_info_t sheep_ii_table[] = { |
168 |
|
{ "sheep", |
169 |
< |
(execute_fn)&sheepshaver_cpu::execute_sheep, |
169 |
> |
(execute_pmf)&sheepshaver_cpu::execute_sheep, |
170 |
|
NULL, |
171 |
+ |
PPC_I(SHEEP), |
172 |
|
D_form, 6, 0, CFLOW_JUMP | CFLOW_TRAP |
173 |
|
} |
174 |
|
}; |
208 |
|
break; |
209 |
|
|
210 |
|
case 1: // EXEC_RETURN |
211 |
< |
throw sheepshaver_exec_return(); |
211 |
> |
spcflags().set(SPCFLAG_CPU_EXEC_RETURN); |
212 |
|
break; |
213 |
|
|
214 |
|
case 2: // EXEC_NATIVE |
241 |
|
} |
242 |
|
} |
243 |
|
|
225 |
– |
// Execution loop |
226 |
– |
void sheepshaver_cpu::execute(uint32 entry, bool enable_cache) |
227 |
– |
{ |
228 |
– |
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 |
– |
} |
238 |
– |
} |
239 |
– |
|
244 |
|
// Handle MacOS interrupt |
245 |
|
void sheepshaver_cpu::interrupt(uint32 entry) |
246 |
|
{ |
247 |
+ |
#if EMUL_TIME_STATS |
248 |
+ |
interrupt_count++; |
249 |
+ |
const clock_t interrupt_start = clock(); |
250 |
+ |
#endif |
251 |
+ |
|
252 |
|
#if !MULTICORE_CPU |
253 |
|
// Save program counters and branch registers |
254 |
|
uint32 saved_pc = pc(); |
302 |
|
ctr()= saved_ctr; |
303 |
|
gpr(1) = saved_sp; |
304 |
|
#endif |
305 |
+ |
|
306 |
+ |
#if EMUL_TIME_STATS |
307 |
+ |
interrupt_time += (clock() - interrupt_start); |
308 |
+ |
#endif |
309 |
|
} |
310 |
|
|
311 |
|
// Execute 68k routine |
312 |
|
void sheepshaver_cpu::execute_68k(uint32 entry, M68kRegisters *r) |
313 |
|
{ |
314 |
+ |
#if EMUL_TIME_STATS |
315 |
+ |
exec68k_count++; |
316 |
+ |
const clock_t exec68k_start = clock(); |
317 |
+ |
#endif |
318 |
+ |
|
319 |
|
#if SAFE_EXEC_68K |
320 |
|
if (ReadMacInt32(XLM_RUN_MODE) != MODE_EMUL_OP) |
321 |
|
printf("FATAL: Execute68k() not called from EMUL_OP mode\n"); |
398 |
|
lr() = saved_lr; |
399 |
|
ctr()= saved_ctr; |
400 |
|
set_cr(saved_cr); |
401 |
+ |
|
402 |
+ |
#if EMUL_TIME_STATS |
403 |
+ |
exec68k_time += (clock() - exec68k_start); |
404 |
+ |
#endif |
405 |
|
} |
406 |
|
|
407 |
|
// Call MacOS PPC code |
408 |
|
uint32 sheepshaver_cpu::execute_macos_code(uint32 tvect, int nargs, uint32 const *args) |
409 |
|
{ |
410 |
+ |
#if EMUL_TIME_STATS |
411 |
+ |
macos_exec_count++; |
412 |
+ |
const clock_t macos_exec_start = clock(); |
413 |
+ |
#endif |
414 |
+ |
|
415 |
|
// Save program counters and branch registers |
416 |
|
uint32 saved_pc = pc(); |
417 |
|
uint32 saved_lr = lr(); |
450 |
|
lr() = saved_lr; |
451 |
|
ctr()= saved_ctr; |
452 |
|
|
453 |
+ |
#if EMUL_TIME_STATS |
454 |
+ |
macos_exec_time += (clock() - macos_exec_start); |
455 |
+ |
#endif |
456 |
+ |
|
457 |
|
return retval; |
458 |
|
} |
459 |
|
|
558 |
|
if ((addr - ROM_BASE) < ROM_SIZE) |
559 |
|
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
560 |
|
|
561 |
< |
// Ignore all other faults, if requested |
562 |
< |
if (PrefsFindBool("ignoresegv")) |
563 |
< |
return SIGSEGV_RETURN_FAILURE; |
561 |
> |
// Get program counter of target CPU |
562 |
> |
sheepshaver_cpu * const cpu = current_cpu; |
563 |
> |
const uint32 pc = cpu->pc(); |
564 |
> |
|
565 |
> |
// Fault in Mac ROM or RAM? |
566 |
> |
bool mac_fault = (pc >= ROM_BASE) && (pc < (ROM_BASE + ROM_AREA_SIZE)) || (pc >= RAMBase) && (pc < (RAMBase + RAMSize)); |
567 |
> |
if (mac_fault) { |
568 |
> |
|
569 |
> |
// "VM settings" during MacOS 8 installation |
570 |
> |
if (pc == ROM_BASE + 0x488160 && cpu->gpr(20) == 0xf8000000) |
571 |
> |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
572 |
> |
|
573 |
> |
// MacOS 8.5 installation |
574 |
> |
else if (pc == ROM_BASE + 0x488140 && cpu->gpr(16) == 0xf8000000) |
575 |
> |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
576 |
> |
|
577 |
> |
// MacOS 8 serial drivers on startup |
578 |
> |
else if (pc == ROM_BASE + 0x48e080 && (cpu->gpr(8) == 0xf3012002 || cpu->gpr(8) == 0xf3012000)) |
579 |
> |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
580 |
> |
|
581 |
> |
// MacOS 8.1 serial drivers on startup |
582 |
> |
else if (pc == ROM_BASE + 0x48c5e0 && (cpu->gpr(20) == 0xf3012002 || cpu->gpr(20) == 0xf3012000)) |
583 |
> |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
584 |
> |
else if (pc == ROM_BASE + 0x4a10a0 && (cpu->gpr(20) == 0xf3012002 || cpu->gpr(20) == 0xf3012000)) |
585 |
> |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
586 |
> |
|
587 |
> |
// Ignore all other faults, if requested |
588 |
> |
if (PrefsFindBool("ignoresegv")) |
589 |
> |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
590 |
> |
} |
591 |
|
#else |
592 |
|
#error "FIXME: You don't have the capability to skip instruction within signal handlers" |
593 |
|
#endif |
624 |
|
mon_add_command("regs", dump_registers, "regs Dump PowerPC registers\n"); |
625 |
|
mon_add_command("log", dump_log, "log Dump PowerPC emulation log\n"); |
626 |
|
#endif |
627 |
+ |
|
628 |
+ |
#if EMUL_TIME_STATS |
629 |
+ |
emul_start_time = clock(); |
630 |
+ |
#endif |
631 |
+ |
} |
632 |
+ |
|
633 |
+ |
/* |
634 |
+ |
* Deinitialize emulation |
635 |
+ |
*/ |
636 |
+ |
|
637 |
+ |
void exit_emul_ppc(void) |
638 |
+ |
{ |
639 |
+ |
#if EMUL_TIME_STATS |
640 |
+ |
clock_t emul_end_time = clock(); |
641 |
+ |
|
642 |
+ |
printf("### Statistics for SheepShaver emulation parts\n"); |
643 |
+ |
const clock_t emul_time = emul_end_time - emul_start_time; |
644 |
+ |
printf("Total emulation time : %.1f sec\n", double(emul_time) / double(CLOCKS_PER_SEC)); |
645 |
+ |
printf("Total interrupt count: %d (%2.1f Hz)\n", interrupt_count, |
646 |
+ |
(double(interrupt_count) * CLOCKS_PER_SEC) / double(emul_time)); |
647 |
+ |
|
648 |
+ |
#define PRINT_STATS(LABEL, VAR_PREFIX) do { \ |
649 |
+ |
printf("Total " LABEL " count : %d\n", VAR_PREFIX##_count); \ |
650 |
+ |
printf("Total " LABEL " time : %.1f sec (%.1f%%)\n", \ |
651 |
+ |
double(VAR_PREFIX##_time) / double(CLOCKS_PER_SEC), \ |
652 |
+ |
100.0 * double(VAR_PREFIX##_time) / double(emul_time)); \ |
653 |
+ |
} while (0) |
654 |
+ |
|
655 |
+ |
PRINT_STATS("Execute68k[Trap] execution", exec68k); |
656 |
+ |
PRINT_STATS("NativeOp execution", native_exec); |
657 |
+ |
PRINT_STATS("MacOS routine execution", macos_exec); |
658 |
+ |
|
659 |
+ |
#undef PRINT_STATS |
660 |
+ |
printf("\n"); |
661 |
+ |
#endif |
662 |
+ |
|
663 |
+ |
delete main_cpu; |
664 |
+ |
#if MULTICORE_CPU |
665 |
+ |
delete interrupt_cpu; |
666 |
+ |
#endif |
667 |
|
} |
668 |
|
|
669 |
|
/* |
677 |
|
current_cpu->start_log(); |
678 |
|
#endif |
679 |
|
// start emulation loop and enable code translation or caching |
680 |
< |
current_cpu->execute(entry, true); |
680 |
> |
current_cpu->execute(entry); |
681 |
|
} |
682 |
|
|
683 |
|
/* |
684 |
|
* Handle PowerPC interrupt |
685 |
|
*/ |
686 |
|
|
687 |
< |
#if !ASYNC_IRQ |
687 |
> |
#if ASYNC_IRQ |
688 |
> |
void HandleInterrupt(void) |
689 |
> |
{ |
690 |
> |
main_cpu->handle_interrupt(); |
691 |
> |
} |
692 |
> |
#else |
693 |
|
void TriggerInterrupt(void) |
694 |
|
{ |
695 |
|
#if 0 |
705 |
|
void sheepshaver_cpu::handle_interrupt(void) |
706 |
|
{ |
707 |
|
// Do nothing if interrupts are disabled |
708 |
< |
if (int32(ReadMacInt32(XLM_IRQ_NEST)) > 0) |
708 |
> |
if (*(int32 *)XLM_IRQ_NEST > 0) |
709 |
|
return; |
710 |
|
|
711 |
|
// Do nothing if there is no interrupt pending |
829 |
|
|
830 |
|
static void NativeOp(int selector) |
831 |
|
{ |
832 |
+ |
#if EMUL_TIME_STATS |
833 |
+ |
native_exec_count++; |
834 |
+ |
const clock_t native_exec_start = clock(); |
835 |
+ |
#endif |
836 |
+ |
|
837 |
|
switch (selector) { |
838 |
|
case NATIVE_PATCH_NAME_REGISTRY: |
839 |
|
DoPatchNameRegistry(); |
848 |
|
GPR(3) = (int32)(int16)VideoDoDriverIO((void *)GPR(3), (void *)GPR(4), |
849 |
|
(void *)GPR(5), GPR(6), GPR(7)); |
850 |
|
break; |
851 |
< |
case NATIVE_GET_RESOURCE: |
852 |
< |
get_resource(); |
851 |
> |
#ifdef WORDS_BIGENDIAN |
852 |
> |
case NATIVE_ETHER_IRQ: |
853 |
> |
EtherIRQ(); |
854 |
|
break; |
855 |
< |
case NATIVE_GET_1_RESOURCE: |
856 |
< |
get_1_resource(); |
855 |
> |
case NATIVE_ETHER_INIT: |
856 |
> |
GPR(3) = InitStreamModule((void *)GPR(3)); |
857 |
|
break; |
858 |
< |
case NATIVE_GET_IND_RESOURCE: |
859 |
< |
get_ind_resource(); |
858 |
> |
case NATIVE_ETHER_TERM: |
859 |
> |
TerminateStreamModule(); |
860 |
|
break; |
861 |
< |
case NATIVE_GET_1_IND_RESOURCE: |
862 |
< |
get_1_ind_resource(); |
861 |
> |
case NATIVE_ETHER_OPEN: |
862 |
> |
GPR(3) = ether_open((queue_t *)GPR(3), (void *)GPR(4), GPR(5), GPR(6), (void*)GPR(7)); |
863 |
> |
break; |
864 |
> |
case NATIVE_ETHER_CLOSE: |
865 |
> |
GPR(3) = ether_close((queue_t *)GPR(3), GPR(4), (void *)GPR(5)); |
866 |
> |
break; |
867 |
> |
case NATIVE_ETHER_WPUT: |
868 |
> |
GPR(3) = ether_wput((queue_t *)GPR(3), (mblk_t *)GPR(4)); |
869 |
|
break; |
870 |
< |
case NATIVE_R_GET_RESOURCE: |
871 |
< |
r_get_resource(); |
870 |
> |
case NATIVE_ETHER_RSRV: |
871 |
> |
GPR(3) = ether_rsrv((queue_t *)GPR(3)); |
872 |
|
break; |
873 |
+ |
#else |
874 |
+ |
case NATIVE_ETHER_INIT: |
875 |
+ |
// FIXME: needs more complicated thunks |
876 |
+ |
GPR(3) = false; |
877 |
+ |
break; |
878 |
+ |
#endif |
879 |
|
case NATIVE_SERIAL_NOTHING: |
880 |
|
case NATIVE_SERIAL_OPEN: |
881 |
|
case NATIVE_SERIAL_PRIME_IN: |
896 |
|
GPR(3) = serial_callbacks[selector - NATIVE_SERIAL_NOTHING](GPR(3), GPR(4)); |
897 |
|
break; |
898 |
|
} |
899 |
+ |
case NATIVE_GET_RESOURCE: |
900 |
+ |
case NATIVE_GET_1_RESOURCE: |
901 |
+ |
case NATIVE_GET_IND_RESOURCE: |
902 |
+ |
case NATIVE_GET_1_IND_RESOURCE: |
903 |
+ |
case NATIVE_R_GET_RESOURCE: { |
904 |
+ |
typedef void (*GetResourceCallback)(void); |
905 |
+ |
static const GetResourceCallback get_resource_callbacks[] = { |
906 |
+ |
get_resource, |
907 |
+ |
get_1_resource, |
908 |
+ |
get_ind_resource, |
909 |
+ |
get_1_ind_resource, |
910 |
+ |
r_get_resource |
911 |
+ |
}; |
912 |
+ |
get_resource_callbacks[selector - NATIVE_GET_RESOURCE](); |
913 |
+ |
break; |
914 |
+ |
} |
915 |
|
case NATIVE_DISABLE_INTERRUPT: |
916 |
|
DisableInterrupt(); |
917 |
|
break; |
926 |
|
QuitEmulator(); |
927 |
|
break; |
928 |
|
} |
929 |
+ |
|
930 |
+ |
#if EMUL_TIME_STATS |
931 |
+ |
native_exec_time += (clock() - native_exec_start); |
932 |
+ |
#endif |
933 |
|
} |
934 |
|
|
935 |
|
/* |