ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/kpx_cpu/sheepshaver_glue.cpp
Revision: 1.28
Committed: 2004-02-16T15:34:55Z (20 years, 9 months ago) by gbeauche
Branch: MAIN
Changes since 1.27: +4 -1 lines
Log Message:
GCC 3.4 does not allow the lazy_allocator instantiation, the other form is
not supported by any GCC but ICC accepts it.

File Contents

# Content
1 /*
2 * sheepshaver_glue.cpp - Glue Kheperix CPU to SheepShaver CPU engine interface
3 *
4 * SheepShaver (C) 1997-2004 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
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include "sysdeps.h"
22 #include "cpu_emulation.h"
23 #include "main.h"
24 #include "prefs.h"
25 #include "xlowmem.h"
26 #include "emul_op.h"
27 #include "rom_patches.h"
28 #include "macos_util.h"
29 #include "block-alloc.hpp"
30 #include "sigsegv.h"
31 #include "cpu/ppc/ppc-cpu.hpp"
32 #include "cpu/ppc/ppc-operations.hpp"
33 #include "cpu/ppc/ppc-instructions.hpp"
34 #include "thunks.h"
35
36 // Used for NativeOp trampolines
37 #include "video.h"
38 #include "name_registry.h"
39 #include "serial.h"
40 #include "ether.h"
41
42 #include <stdio.h>
43
44 #if ENABLE_MON
45 #include "mon.h"
46 #include "mon_disass.h"
47 #endif
48
49 #define DEBUG 0
50 #include "debug.h"
51
52 // Emulation time statistics
53 #define EMUL_TIME_STATS 1
54
55 #if EMUL_TIME_STATS
56 static clock_t emul_start_time;
57 static uint32 interrupt_count = 0;
58 static clock_t interrupt_time = 0;
59 static uint32 exec68k_count = 0;
60 static clock_t exec68k_time = 0;
61 static uint32 native_exec_count = 0;
62 static clock_t native_exec_time = 0;
63 static uint32 macos_exec_count = 0;
64 static clock_t macos_exec_time = 0;
65 #endif
66
67 static void enter_mon(void)
68 {
69 // Start up mon in real-mode
70 #if ENABLE_MON
71 char *arg[4] = {"mon", "-m", "-r", NULL};
72 mon(3, arg);
73 #endif
74 }
75
76 // From main_*.cpp
77 extern uintptr SignalStackBase();
78
79 // From rsrc_patches.cpp
80 extern "C" void check_load_invoc(uint32 type, int16 id, uint32 h);
81
82 // PowerPC EmulOp to exit from emulation looop
83 const uint32 POWERPC_EXEC_RETURN = POWERPC_EMUL_OP | 1;
84
85 // Enable multicore (main/interrupts) cpu emulation?
86 #define MULTICORE_CPU (ASYNC_IRQ ? 1 : 0)
87
88 // Enable Execute68k() safety checks?
89 #define SAFE_EXEC_68K 1
90
91 // Save FP state in Execute68k()?
92 #define SAVE_FP_EXEC_68K 1
93
94 // Interrupts in EMUL_OP mode?
95 #define INTERRUPTS_IN_EMUL_OP_MODE 1
96
97 // Interrupts in native mode?
98 #define INTERRUPTS_IN_NATIVE_MODE 1
99
100 // Pointer to Kernel Data
101 static KernelData * const kernel_data = (KernelData *)KERNEL_DATA_BASE;
102
103 // SIGSEGV handler
104 static sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t);
105
106 // JIT Compiler enabled?
107 static inline bool enable_jit_p()
108 {
109 return PrefsFindBool("jit");
110 }
111
112
113 /**
114 * PowerPC emulator glue with special 'sheep' opcodes
115 **/
116
117 enum {
118 PPC_I(SHEEP) = PPC_I(MAX),
119 PPC_I(SHEEP_MAX)
120 };
121
122 class sheepshaver_cpu
123 : public powerpc_cpu
124 {
125 void init_decoder();
126 void execute_sheep(uint32 opcode);
127
128 public:
129
130 // Constructor
131 sheepshaver_cpu();
132
133 // CR & XER accessors
134 uint32 get_cr() const { return cr().get(); }
135 void set_cr(uint32 v) { cr().set(v); }
136 uint32 get_xer() const { return xer().get(); }
137 void set_xer(uint32 v) { xer().set(v); }
138
139 // Execute EMUL_OP routine
140 void execute_emul_op(uint32 emul_op);
141
142 // Execute 68k routine
143 void execute_68k(uint32 entry, M68kRegisters *r);
144
145 // Execute ppc routine
146 void execute_ppc(uint32 entry);
147
148 // Execute MacOS/PPC code
149 uint32 execute_macos_code(uint32 tvect, int nargs, uint32 const *args);
150
151 // Compile one instruction
152 virtual bool compile1(codegen_context_t & cg_context);
153
154 // Resource manager thunk
155 void get_resource(uint32 old_get_resource);
156
157 // Handle MacOS interrupt
158 void interrupt(uint32 entry);
159 void handle_interrupt();
160
161 // Lazy memory allocator (one item at a time)
162 void *operator new(size_t size)
163 { return allocator_helper< sheepshaver_cpu, lazy_allocator >::allocate(); }
164 void operator delete(void *p)
165 { allocator_helper< sheepshaver_cpu, lazy_allocator >::deallocate(p); }
166 // FIXME: really make surre array allocation fail at link time?
167 void *operator new[](size_t);
168 void operator delete[](void *p);
169
170 // Make sure the SIGSEGV handler can access CPU registers
171 friend sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t);
172 };
173
174 // FIXME: this specialization doesn't work with GCC
175 // template<> lazy_allocator< sheepshaver_cpu > allocator_helper< sheepshaver_cpu, lazy_allocator >::allocator;
176 template< class data_type, template< class > class allocator_type >
177 allocator_type< data_type > allocator_helper< data_type, allocator_type >::allocator;
178
179 sheepshaver_cpu::sheepshaver_cpu()
180 : powerpc_cpu(enable_jit_p())
181 {
182 init_decoder();
183 }
184
185 void sheepshaver_cpu::init_decoder()
186 {
187 static const instr_info_t sheep_ii_table[] = {
188 { "sheep",
189 (execute_pmf)&sheepshaver_cpu::execute_sheep,
190 NULL,
191 PPC_I(SHEEP),
192 D_form, 6, 0, CFLOW_JUMP | CFLOW_TRAP
193 }
194 };
195
196 const int ii_count = sizeof(sheep_ii_table)/sizeof(sheep_ii_table[0]);
197 D(bug("SheepShaver extra decode table has %d entries\n", ii_count));
198
199 for (int i = 0; i < ii_count; i++) {
200 const instr_info_t * ii = &sheep_ii_table[i];
201 init_decoder_entry(ii);
202 }
203 }
204
205 // Forward declaration for native opcode handler
206 static void NativeOp(int selector);
207
208 /* NativeOp instruction format:
209 +------------+--------------------------+--+----------+------------+
210 | 6 | |FN| OP | 2 |
211 +------------+--------------------------+--+----------+------------+
212 0 5 |6 19 20 21 25 26 31
213 */
214
215 typedef bit_field< 20, 20 > FN_field;
216 typedef bit_field< 21, 25 > NATIVE_OP_field;
217 typedef bit_field< 26, 31 > EMUL_OP_field;
218
219 // Execute EMUL_OP routine
220 void sheepshaver_cpu::execute_emul_op(uint32 emul_op)
221 {
222 M68kRegisters r68;
223 WriteMacInt32(XLM_68K_R25, gpr(25));
224 WriteMacInt32(XLM_RUN_MODE, MODE_EMUL_OP);
225 for (int i = 0; i < 8; i++)
226 r68.d[i] = gpr(8 + i);
227 for (int i = 0; i < 7; i++)
228 r68.a[i] = gpr(16 + i);
229 r68.a[7] = gpr(1);
230 uint32 saved_cr = get_cr() & CR_field<2>::mask();
231 uint32 saved_xer = get_xer();
232 EmulOp(&r68, gpr(24), emul_op);
233 set_cr(saved_cr);
234 set_xer(saved_xer);
235 for (int i = 0; i < 8; i++)
236 gpr(8 + i) = r68.d[i];
237 for (int i = 0; i < 7; i++)
238 gpr(16 + i) = r68.a[i];
239 gpr(1) = r68.a[7];
240 WriteMacInt32(XLM_RUN_MODE, MODE_68K);
241 }
242
243 // Execute SheepShaver instruction
244 void sheepshaver_cpu::execute_sheep(uint32 opcode)
245 {
246 // D(bug("Extended opcode %08x at %08x (68k pc %08x)\n", opcode, pc(), gpr(24)));
247 assert((((opcode >> 26) & 0x3f) == 6) && OP_MAX <= 64 + 3);
248
249 switch (opcode & 0x3f) {
250 case 0: // EMUL_RETURN
251 QuitEmulator();
252 break;
253
254 case 1: // EXEC_RETURN
255 spcflags().set(SPCFLAG_CPU_EXEC_RETURN);
256 break;
257
258 case 2: // EXEC_NATIVE
259 NativeOp(NATIVE_OP_field::extract(opcode));
260 if (FN_field::test(opcode))
261 pc() = lr();
262 else
263 pc() += 4;
264 break;
265
266 default: // EMUL_OP
267 execute_emul_op(EMUL_OP_field::extract(opcode) - 3);
268 pc() += 4;
269 break;
270 }
271 }
272
273 // Compile one instruction
274 bool sheepshaver_cpu::compile1(codegen_context_t & cg_context)
275 {
276 #if PPC_ENABLE_JIT
277 const instr_info_t *ii = cg_context.instr_info;
278 if (ii->mnemo != PPC_I(SHEEP))
279 return false;
280
281 bool compiled = false;
282 powerpc_dyngen & dg = cg_context.codegen;
283 uint32 opcode = cg_context.opcode;
284
285 switch (opcode & 0x3f) {
286 case 0: // EMUL_RETURN
287 dg.gen_invoke(QuitEmulator);
288 compiled = true;
289 break;
290
291 case 1: // EXEC_RETURN
292 dg.gen_spcflags_set(SPCFLAG_CPU_EXEC_RETURN);
293 compiled = true;
294 break;
295
296 case 2: { // EXEC_NATIVE
297 uint32 selector = NATIVE_OP_field::extract(opcode);
298 switch (selector) {
299 case NATIVE_PATCH_NAME_REGISTRY:
300 dg.gen_invoke(DoPatchNameRegistry);
301 compiled = true;
302 break;
303 case NATIVE_VIDEO_INSTALL_ACCEL:
304 dg.gen_invoke(VideoInstallAccel);
305 compiled = true;
306 break;
307 case NATIVE_VIDEO_VBL:
308 dg.gen_invoke(VideoVBL);
309 compiled = true;
310 break;
311 case NATIVE_GET_RESOURCE:
312 case NATIVE_GET_1_RESOURCE:
313 case NATIVE_GET_IND_RESOURCE:
314 case NATIVE_GET_1_IND_RESOURCE:
315 case NATIVE_R_GET_RESOURCE: {
316 static const uint32 get_resource_ptr[] = {
317 XLM_GET_RESOURCE,
318 XLM_GET_1_RESOURCE,
319 XLM_GET_IND_RESOURCE,
320 XLM_GET_1_IND_RESOURCE,
321 XLM_R_GET_RESOURCE
322 };
323 uint32 old_get_resource = ReadMacInt32(get_resource_ptr[selector - NATIVE_GET_RESOURCE]);
324 typedef void (*func_t)(dyngen_cpu_base, uint32);
325 func_t func = (func_t)nv_mem_fun(&sheepshaver_cpu::get_resource).ptr();
326 dg.gen_invoke_CPU_im(func, old_get_resource);
327 compiled = true;
328 break;
329 }
330 case NATIVE_DISABLE_INTERRUPT:
331 dg.gen_invoke(DisableInterrupt);
332 compiled = true;
333 break;
334 case NATIVE_ENABLE_INTERRUPT:
335 dg.gen_invoke(EnableInterrupt);
336 compiled = true;
337 break;
338 case NATIVE_CHECK_LOAD_INVOC:
339 dg.gen_load_T0_GPR(3);
340 dg.gen_load_T1_GPR(4);
341 dg.gen_se_16_32_T1();
342 dg.gen_load_T2_GPR(5);
343 dg.gen_invoke_T0_T1_T2((void (*)(uint32, uint32, uint32))check_load_invoc);
344 compiled = true;
345 break;
346 }
347 if (FN_field::test(opcode)) {
348 if (compiled) {
349 dg.gen_load_A0_LR();
350 dg.gen_set_PC_A0();
351 }
352 cg_context.done_compile = true;
353 }
354 else
355 cg_context.done_compile = false;
356 break;
357 }
358
359 default: { // EMUL_OP
360 typedef void (*func_t)(dyngen_cpu_base, uint32);
361 func_t func = (func_t)nv_mem_fun(&sheepshaver_cpu::execute_emul_op).ptr();
362 dg.gen_invoke_CPU_im(func, EMUL_OP_field::extract(opcode) - 3);
363 cg_context.done_compile = false;
364 compiled = true;
365 break;
366 }
367 }
368 return compiled;
369 #endif
370 return false;
371 }
372
373 // Handle MacOS interrupt
374 void sheepshaver_cpu::interrupt(uint32 entry)
375 {
376 #if EMUL_TIME_STATS
377 interrupt_count++;
378 const clock_t interrupt_start = clock();
379 #endif
380
381 #if !MULTICORE_CPU
382 // Save program counters and branch registers
383 uint32 saved_pc = pc();
384 uint32 saved_lr = lr();
385 uint32 saved_ctr= ctr();
386 uint32 saved_sp = gpr(1);
387 #endif
388
389 // Initialize stack pointer to SheepShaver alternate stack base
390 gpr(1) = SignalStackBase() - 64;
391
392 // Build trampoline to return from interrupt
393 SheepVar32 trampoline = POWERPC_EXEC_RETURN;
394
395 // Prepare registers for nanokernel interrupt routine
396 kernel_data->v[0x004 >> 2] = htonl(gpr(1));
397 kernel_data->v[0x018 >> 2] = htonl(gpr(6));
398
399 gpr(6) = ntohl(kernel_data->v[0x65c >> 2]);
400 assert(gpr(6) != 0);
401 WriteMacInt32(gpr(6) + 0x13c, gpr(7));
402 WriteMacInt32(gpr(6) + 0x144, gpr(8));
403 WriteMacInt32(gpr(6) + 0x14c, gpr(9));
404 WriteMacInt32(gpr(6) + 0x154, gpr(10));
405 WriteMacInt32(gpr(6) + 0x15c, gpr(11));
406 WriteMacInt32(gpr(6) + 0x164, gpr(12));
407 WriteMacInt32(gpr(6) + 0x16c, gpr(13));
408
409 gpr(1) = KernelDataAddr;
410 gpr(7) = ntohl(kernel_data->v[0x660 >> 2]);
411 gpr(8) = 0;
412 gpr(10) = trampoline.addr();
413 gpr(12) = trampoline.addr();
414 gpr(13) = get_cr();
415
416 // rlwimi. r7,r7,8,0,0
417 uint32 result = op_ppc_rlwimi::apply(gpr(7), 8, 0x80000000, gpr(7));
418 record_cr0(result);
419 gpr(7) = result;
420
421 gpr(11) = 0xf072; // MSR (SRR1)
422 cr().set((gpr(11) & 0x0fff0000) | (get_cr() & ~0x0fff0000));
423
424 // Enter nanokernel
425 execute(entry);
426
427 #if !MULTICORE_CPU
428 // Restore program counters and branch registers
429 pc() = saved_pc;
430 lr() = saved_lr;
431 ctr()= saved_ctr;
432 gpr(1) = saved_sp;
433 #endif
434
435 #if EMUL_TIME_STATS
436 interrupt_time += (clock() - interrupt_start);
437 #endif
438 }
439
440 // Execute 68k routine
441 void sheepshaver_cpu::execute_68k(uint32 entry, M68kRegisters *r)
442 {
443 #if EMUL_TIME_STATS
444 exec68k_count++;
445 const clock_t exec68k_start = clock();
446 #endif
447
448 #if SAFE_EXEC_68K
449 if (ReadMacInt32(XLM_RUN_MODE) != MODE_EMUL_OP)
450 printf("FATAL: Execute68k() not called from EMUL_OP mode\n");
451 #endif
452
453 // Save program counters and branch registers
454 uint32 saved_pc = pc();
455 uint32 saved_lr = lr();
456 uint32 saved_ctr= ctr();
457 uint32 saved_cr = get_cr();
458
459 // Create MacOS stack frame
460 // FIXME: make sure MacOS doesn't expect PPC registers to live on top
461 uint32 sp = gpr(1);
462 gpr(1) -= 56;
463 WriteMacInt32(gpr(1), sp);
464
465 // Save PowerPC registers
466 uint32 saved_GPRs[19];
467 memcpy(&saved_GPRs[0], &gpr(13), sizeof(uint32)*(32-13));
468 #if SAVE_FP_EXEC_68K
469 double saved_FPRs[18];
470 memcpy(&saved_FPRs[0], &fpr(14), sizeof(double)*(32-14));
471 #endif
472
473 // Setup registers for 68k emulator
474 cr().set(CR_SO_field<2>::mask()); // Supervisor mode
475 for (int i = 0; i < 8; i++) // d[0]..d[7]
476 gpr(8 + i) = r->d[i];
477 for (int i = 0; i < 7; i++) // a[0]..a[6]
478 gpr(16 + i) = r->a[i];
479 gpr(23) = 0;
480 gpr(24) = entry;
481 gpr(25) = ReadMacInt32(XLM_68K_R25); // MSB of SR
482 gpr(26) = 0;
483 gpr(28) = 0; // VBR
484 gpr(29) = ntohl(kernel_data->ed.v[0x74 >> 2]); // Pointer to opcode table
485 gpr(30) = ntohl(kernel_data->ed.v[0x78 >> 2]); // Address of emulator
486 gpr(31) = KernelDataAddr + 0x1000;
487
488 // Push return address (points to EXEC_RETURN opcode) on stack
489 gpr(1) -= 4;
490 WriteMacInt32(gpr(1), XLM_EXEC_RETURN_OPCODE);
491
492 // Rentering 68k emulator
493 WriteMacInt32(XLM_RUN_MODE, MODE_68K);
494
495 // Set r0 to 0 for 68k emulator
496 gpr(0) = 0;
497
498 // Execute 68k opcode
499 uint32 opcode = ReadMacInt16(gpr(24));
500 gpr(27) = (int32)(int16)ReadMacInt16(gpr(24) += 2);
501 gpr(29) += opcode * 8;
502 execute(gpr(29));
503
504 // Save r25 (contains current 68k interrupt level)
505 WriteMacInt32(XLM_68K_R25, gpr(25));
506
507 // Reentering EMUL_OP mode
508 WriteMacInt32(XLM_RUN_MODE, MODE_EMUL_OP);
509
510 // Save 68k registers
511 for (int i = 0; i < 8; i++) // d[0]..d[7]
512 r->d[i] = gpr(8 + i);
513 for (int i = 0; i < 7; i++) // a[0]..a[6]
514 r->a[i] = gpr(16 + i);
515
516 // Restore PowerPC registers
517 memcpy(&gpr(13), &saved_GPRs[0], sizeof(uint32)*(32-13));
518 #if SAVE_FP_EXEC_68K
519 memcpy(&fpr(14), &saved_FPRs[0], sizeof(double)*(32-14));
520 #endif
521
522 // Cleanup stack
523 gpr(1) += 56;
524
525 // Restore program counters and branch registers
526 pc() = saved_pc;
527 lr() = saved_lr;
528 ctr()= saved_ctr;
529 set_cr(saved_cr);
530
531 #if EMUL_TIME_STATS
532 exec68k_time += (clock() - exec68k_start);
533 #endif
534 }
535
536 // Call MacOS PPC code
537 uint32 sheepshaver_cpu::execute_macos_code(uint32 tvect, int nargs, uint32 const *args)
538 {
539 #if EMUL_TIME_STATS
540 macos_exec_count++;
541 const clock_t macos_exec_start = clock();
542 #endif
543
544 // Save program counters and branch registers
545 uint32 saved_pc = pc();
546 uint32 saved_lr = lr();
547 uint32 saved_ctr= ctr();
548
549 // Build trampoline with EXEC_RETURN
550 SheepVar32 trampoline = POWERPC_EXEC_RETURN;
551 lr() = trampoline.addr();
552
553 gpr(1) -= 64; // Create stack frame
554 uint32 proc = ReadMacInt32(tvect); // Get routine address
555 uint32 toc = ReadMacInt32(tvect + 4); // Get TOC pointer
556
557 // Save PowerPC registers
558 uint32 regs[8];
559 regs[0] = gpr(2);
560 for (int i = 0; i < nargs; i++)
561 regs[i + 1] = gpr(i + 3);
562
563 // Prepare and call MacOS routine
564 gpr(2) = toc;
565 for (int i = 0; i < nargs; i++)
566 gpr(i + 3) = args[i];
567 execute(proc);
568 uint32 retval = gpr(3);
569
570 // Restore PowerPC registers
571 for (int i = 0; i <= nargs; i++)
572 gpr(i + 2) = regs[i];
573
574 // Cleanup stack
575 gpr(1) += 64;
576
577 // Restore program counters and branch registers
578 pc() = saved_pc;
579 lr() = saved_lr;
580 ctr()= saved_ctr;
581
582 #if EMUL_TIME_STATS
583 macos_exec_time += (clock() - macos_exec_start);
584 #endif
585
586 return retval;
587 }
588
589 // Execute ppc routine
590 inline void sheepshaver_cpu::execute_ppc(uint32 entry)
591 {
592 // Save branch registers
593 uint32 saved_lr = lr();
594
595 SheepVar32 trampoline = POWERPC_EXEC_RETURN;
596 WriteMacInt32(trampoline.addr(), POWERPC_EXEC_RETURN);
597 lr() = trampoline.addr();
598
599 execute(entry);
600
601 // Restore branch registers
602 lr() = saved_lr;
603 }
604
605 // Resource Manager thunk
606 inline void sheepshaver_cpu::get_resource(uint32 old_get_resource)
607 {
608 uint32 type = gpr(3);
609 int16 id = gpr(4);
610
611 // Create stack frame
612 gpr(1) -= 56;
613
614 // Call old routine
615 execute_ppc(old_get_resource);
616
617 // Call CheckLoad()
618 uint32 handle = gpr(3);
619 check_load_invoc(type, id, handle);
620 gpr(3) = handle;
621
622 // Cleanup stack
623 gpr(1) += 56;
624 }
625
626
627 /**
628 * SheepShaver CPU engine interface
629 **/
630
631 static sheepshaver_cpu *main_cpu = NULL; // CPU emulator to handle usual control flow
632 static sheepshaver_cpu *interrupt_cpu = NULL; // CPU emulator to handle interrupts
633 static sheepshaver_cpu *current_cpu = NULL; // Current CPU emulator context
634
635 void FlushCodeCache(uintptr start, uintptr end)
636 {
637 D(bug("FlushCodeCache(%08x, %08x)\n", start, end));
638 main_cpu->invalidate_cache_range(start, end);
639 #if MULTICORE_CPU
640 interrupt_cpu->invalidate_cache_range(start, end);
641 #endif
642 }
643
644 static inline void cpu_push(sheepshaver_cpu *new_cpu)
645 {
646 #if MULTICORE_CPU
647 current_cpu = new_cpu;
648 #endif
649 }
650
651 static inline void cpu_pop()
652 {
653 #if MULTICORE_CPU
654 current_cpu = main_cpu;
655 #endif
656 }
657
658 // Dump PPC registers
659 static void dump_registers(void)
660 {
661 current_cpu->dump_registers();
662 }
663
664 // Dump log
665 static void dump_log(void)
666 {
667 current_cpu->dump_log();
668 }
669
670 /*
671 * Initialize CPU emulation
672 */
673
674 static sigsegv_return_t sigsegv_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction)
675 {
676 #if ENABLE_VOSF
677 // Handle screen fault
678 extern bool Screen_fault_handler(sigsegv_address_t, sigsegv_address_t);
679 if (Screen_fault_handler(fault_address, fault_instruction))
680 return SIGSEGV_RETURN_SUCCESS;
681 #endif
682
683 const uintptr addr = (uintptr)fault_address;
684 #if HAVE_SIGSEGV_SKIP_INSTRUCTION
685 // Ignore writes to ROM
686 if ((addr - ROM_BASE) < ROM_SIZE)
687 return SIGSEGV_RETURN_SKIP_INSTRUCTION;
688
689 // Get program counter of target CPU
690 sheepshaver_cpu * const cpu = current_cpu;
691 const uint32 pc = cpu->pc();
692
693 // Fault in Mac ROM or RAM?
694 bool mac_fault = (pc >= ROM_BASE) && (pc < (ROM_BASE + ROM_AREA_SIZE)) || (pc >= RAMBase) && (pc < (RAMBase + RAMSize));
695 if (mac_fault) {
696
697 // "VM settings" during MacOS 8 installation
698 if (pc == ROM_BASE + 0x488160 && cpu->gpr(20) == 0xf8000000)
699 return SIGSEGV_RETURN_SKIP_INSTRUCTION;
700
701 // MacOS 8.5 installation
702 else if (pc == ROM_BASE + 0x488140 && cpu->gpr(16) == 0xf8000000)
703 return SIGSEGV_RETURN_SKIP_INSTRUCTION;
704
705 // MacOS 8 serial drivers on startup
706 else if (pc == ROM_BASE + 0x48e080 && (cpu->gpr(8) == 0xf3012002 || cpu->gpr(8) == 0xf3012000))
707 return SIGSEGV_RETURN_SKIP_INSTRUCTION;
708
709 // MacOS 8.1 serial drivers on startup
710 else if (pc == ROM_BASE + 0x48c5e0 && (cpu->gpr(20) == 0xf3012002 || cpu->gpr(20) == 0xf3012000))
711 return SIGSEGV_RETURN_SKIP_INSTRUCTION;
712 else if (pc == ROM_BASE + 0x4a10a0 && (cpu->gpr(20) == 0xf3012002 || cpu->gpr(20) == 0xf3012000))
713 return SIGSEGV_RETURN_SKIP_INSTRUCTION;
714
715 // Ignore all other faults, if requested
716 if (PrefsFindBool("ignoresegv"))
717 return SIGSEGV_RETURN_SKIP_INSTRUCTION;
718 }
719 #else
720 #error "FIXME: You don't have the capability to skip instruction within signal handlers"
721 #endif
722
723 printf("SIGSEGV\n");
724 printf(" pc %p\n", fault_instruction);
725 printf(" ea %p\n", fault_address);
726 printf(" cpu %s\n", current_cpu == main_cpu ? "main" : "interrupts");
727 dump_registers();
728 current_cpu->dump_log();
729 enter_mon();
730 QuitEmulator();
731
732 return SIGSEGV_RETURN_FAILURE;
733 }
734
735 void init_emul_ppc(void)
736 {
737 // Initialize main CPU emulator
738 main_cpu = new sheepshaver_cpu();
739 main_cpu->set_register(powerpc_registers::GPR(3), any_register((uint32)ROM_BASE + 0x30d000));
740 main_cpu->set_register(powerpc_registers::GPR(4), any_register(KernelDataAddr + 0x1000));
741 WriteMacInt32(XLM_RUN_MODE, MODE_68K);
742
743 #if MULTICORE_CPU
744 // Initialize alternate CPU emulator to handle interrupts
745 interrupt_cpu = new sheepshaver_cpu();
746 #endif
747
748 // Install the handler for SIGSEGV
749 sigsegv_install_handler(sigsegv_handler);
750
751 #if ENABLE_MON
752 // Install "regs" command in cxmon
753 mon_add_command("regs", dump_registers, "regs Dump PowerPC registers\n");
754 mon_add_command("log", dump_log, "log Dump PowerPC emulation log\n");
755 #endif
756
757 #if EMUL_TIME_STATS
758 emul_start_time = clock();
759 #endif
760 }
761
762 /*
763 * Deinitialize emulation
764 */
765
766 void exit_emul_ppc(void)
767 {
768 #if EMUL_TIME_STATS
769 clock_t emul_end_time = clock();
770
771 printf("### Statistics for SheepShaver emulation parts\n");
772 const clock_t emul_time = emul_end_time - emul_start_time;
773 printf("Total emulation time : %.1f sec\n", double(emul_time) / double(CLOCKS_PER_SEC));
774 printf("Total interrupt count: %d (%2.1f Hz)\n", interrupt_count,
775 (double(interrupt_count) * CLOCKS_PER_SEC) / double(emul_time));
776
777 #define PRINT_STATS(LABEL, VAR_PREFIX) do { \
778 printf("Total " LABEL " count : %d\n", VAR_PREFIX##_count); \
779 printf("Total " LABEL " time : %.1f sec (%.1f%%)\n", \
780 double(VAR_PREFIX##_time) / double(CLOCKS_PER_SEC), \
781 100.0 * double(VAR_PREFIX##_time) / double(emul_time)); \
782 } while (0)
783
784 PRINT_STATS("Execute68k[Trap] execution", exec68k);
785 PRINT_STATS("NativeOp execution", native_exec);
786 PRINT_STATS("MacOS routine execution", macos_exec);
787
788 #undef PRINT_STATS
789 printf("\n");
790 #endif
791
792 delete main_cpu;
793 #if MULTICORE_CPU
794 delete interrupt_cpu;
795 #endif
796 }
797
798 /*
799 * Emulation loop
800 */
801
802 void emul_ppc(uint32 entry)
803 {
804 current_cpu = main_cpu;
805 #if 0
806 current_cpu->start_log();
807 #endif
808 // start emulation loop and enable code translation or caching
809 current_cpu->execute(entry);
810 }
811
812 /*
813 * Handle PowerPC interrupt
814 */
815
816 #if ASYNC_IRQ
817 void HandleInterrupt(void)
818 {
819 main_cpu->handle_interrupt();
820 }
821 #else
822 void TriggerInterrupt(void)
823 {
824 #if 0
825 WriteMacInt32(0x16a, ReadMacInt32(0x16a) + 1);
826 #else
827 // Trigger interrupt to main cpu only
828 if (main_cpu)
829 main_cpu->trigger_interrupt();
830 #endif
831 }
832 #endif
833
834 void sheepshaver_cpu::handle_interrupt(void)
835 {
836 // Do nothing if interrupts are disabled
837 if (*(int32 *)XLM_IRQ_NEST > 0)
838 return;
839
840 // Do nothing if there is no interrupt pending
841 if (InterruptFlags == 0)
842 return;
843
844 // Disable MacOS stack sniffer
845 WriteMacInt32(0x110, 0);
846
847 // Interrupt action depends on current run mode
848 switch (ReadMacInt32(XLM_RUN_MODE)) {
849 case MODE_68K:
850 // 68k emulator active, trigger 68k interrupt level 1
851 assert(current_cpu == main_cpu);
852 WriteMacInt16(tswap32(kernel_data->v[0x67c >> 2]), 1);
853 set_cr(get_cr() | tswap32(kernel_data->v[0x674 >> 2]));
854 break;
855
856 #if INTERRUPTS_IN_NATIVE_MODE
857 case MODE_NATIVE:
858 // 68k emulator inactive, in nanokernel?
859 assert(current_cpu == main_cpu);
860 if (gpr(1) != KernelDataAddr) {
861 // Prepare for 68k interrupt level 1
862 WriteMacInt16(tswap32(kernel_data->v[0x67c >> 2]), 1);
863 WriteMacInt32(tswap32(kernel_data->v[0x658 >> 2]) + 0xdc,
864 ReadMacInt32(tswap32(kernel_data->v[0x658 >> 2]) + 0xdc)
865 | tswap32(kernel_data->v[0x674 >> 2]));
866
867 // Execute nanokernel interrupt routine (this will activate the 68k emulator)
868 DisableInterrupt();
869 cpu_push(interrupt_cpu);
870 if (ROMType == ROMTYPE_NEWWORLD)
871 current_cpu->interrupt(ROM_BASE + 0x312b1c);
872 else
873 current_cpu->interrupt(ROM_BASE + 0x312a3c);
874 cpu_pop();
875 }
876 break;
877 #endif
878
879 #if INTERRUPTS_IN_EMUL_OP_MODE
880 case MODE_EMUL_OP:
881 // 68k emulator active, within EMUL_OP routine, execute 68k interrupt routine directly when interrupt level is 0
882 if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) {
883 #if 1
884 // Execute full 68k interrupt routine
885 M68kRegisters r;
886 uint32 old_r25 = ReadMacInt32(XLM_68K_R25); // Save interrupt level
887 WriteMacInt32(XLM_68K_R25, 0x21); // Execute with interrupt level 1
888 static const uint8 proc[] = {
889 0x3f, 0x3c, 0x00, 0x00, // move.w #$0000,-(sp) (fake format word)
890 0x48, 0x7a, 0x00, 0x0a, // pea @1(pc) (return address)
891 0x40, 0xe7, // move sr,-(sp) (saved SR)
892 0x20, 0x78, 0x00, 0x064, // move.l $64,a0
893 0x4e, 0xd0, // jmp (a0)
894 M68K_RTS >> 8, M68K_RTS & 0xff // @1
895 };
896 Execute68k((uint32)proc, &r);
897 WriteMacInt32(XLM_68K_R25, old_r25); // Restore interrupt level
898 #else
899 // Only update cursor
900 if (HasMacStarted()) {
901 if (InterruptFlags & INTFLAG_VIA) {
902 ClearInterruptFlag(INTFLAG_VIA);
903 ADBInterrupt();
904 ExecuteNative(NATIVE_VIDEO_VBL);
905 }
906 }
907 #endif
908 }
909 break;
910 #endif
911 }
912 }
913
914 static void get_resource(void);
915 static void get_1_resource(void);
916 static void get_ind_resource(void);
917 static void get_1_ind_resource(void);
918 static void r_get_resource(void);
919
920 #define GPR(REG) current_cpu->gpr(REG)
921
922 static void NativeOp(int selector)
923 {
924 #if EMUL_TIME_STATS
925 native_exec_count++;
926 const clock_t native_exec_start = clock();
927 #endif
928
929 switch (selector) {
930 case NATIVE_PATCH_NAME_REGISTRY:
931 DoPatchNameRegistry();
932 break;
933 case NATIVE_VIDEO_INSTALL_ACCEL:
934 VideoInstallAccel();
935 break;
936 case NATIVE_VIDEO_VBL:
937 VideoVBL();
938 break;
939 case NATIVE_VIDEO_DO_DRIVER_IO:
940 GPR(3) = (int32)(int16)VideoDoDriverIO((void *)GPR(3), (void *)GPR(4),
941 (void *)GPR(5), GPR(6), GPR(7));
942 break;
943 #ifdef WORDS_BIGENDIAN
944 case NATIVE_ETHER_IRQ:
945 EtherIRQ();
946 break;
947 case NATIVE_ETHER_INIT:
948 GPR(3) = InitStreamModule((void *)GPR(3));
949 break;
950 case NATIVE_ETHER_TERM:
951 TerminateStreamModule();
952 break;
953 case NATIVE_ETHER_OPEN:
954 GPR(3) = ether_open((queue_t *)GPR(3), (void *)GPR(4), GPR(5), GPR(6), (void*)GPR(7));
955 break;
956 case NATIVE_ETHER_CLOSE:
957 GPR(3) = ether_close((queue_t *)GPR(3), GPR(4), (void *)GPR(5));
958 break;
959 case NATIVE_ETHER_WPUT:
960 GPR(3) = ether_wput((queue_t *)GPR(3), (mblk_t *)GPR(4));
961 break;
962 case NATIVE_ETHER_RSRV:
963 GPR(3) = ether_rsrv((queue_t *)GPR(3));
964 break;
965 #else
966 case NATIVE_ETHER_INIT:
967 // FIXME: needs more complicated thunks
968 GPR(3) = false;
969 break;
970 #endif
971 case NATIVE_SERIAL_NOTHING:
972 case NATIVE_SERIAL_OPEN:
973 case NATIVE_SERIAL_PRIME_IN:
974 case NATIVE_SERIAL_PRIME_OUT:
975 case NATIVE_SERIAL_CONTROL:
976 case NATIVE_SERIAL_STATUS:
977 case NATIVE_SERIAL_CLOSE: {
978 typedef int16 (*SerialCallback)(uint32, uint32);
979 static const SerialCallback serial_callbacks[] = {
980 SerialNothing,
981 SerialOpen,
982 SerialPrimeIn,
983 SerialPrimeOut,
984 SerialControl,
985 SerialStatus,
986 SerialClose
987 };
988 GPR(3) = serial_callbacks[selector - NATIVE_SERIAL_NOTHING](GPR(3), GPR(4));
989 break;
990 }
991 case NATIVE_GET_RESOURCE:
992 case NATIVE_GET_1_RESOURCE:
993 case NATIVE_GET_IND_RESOURCE:
994 case NATIVE_GET_1_IND_RESOURCE:
995 case NATIVE_R_GET_RESOURCE: {
996 typedef void (*GetResourceCallback)(void);
997 static const GetResourceCallback get_resource_callbacks[] = {
998 get_resource,
999 get_1_resource,
1000 get_ind_resource,
1001 get_1_ind_resource,
1002 r_get_resource
1003 };
1004 get_resource_callbacks[selector - NATIVE_GET_RESOURCE]();
1005 break;
1006 }
1007 case NATIVE_DISABLE_INTERRUPT:
1008 DisableInterrupt();
1009 break;
1010 case NATIVE_ENABLE_INTERRUPT:
1011 EnableInterrupt();
1012 break;
1013 case NATIVE_MAKE_EXECUTABLE:
1014 MakeExecutable(0, (void *)GPR(4), GPR(5));
1015 break;
1016 case NATIVE_CHECK_LOAD_INVOC:
1017 check_load_invoc(GPR(3), GPR(4), GPR(5));
1018 break;
1019 default:
1020 printf("FATAL: NATIVE_OP called with bogus selector %d\n", selector);
1021 QuitEmulator();
1022 break;
1023 }
1024
1025 #if EMUL_TIME_STATS
1026 native_exec_time += (clock() - native_exec_start);
1027 #endif
1028 }
1029
1030 /*
1031 * Execute 68k subroutine (must be ended with EXEC_RETURN)
1032 * This must only be called by the emul_thread when in EMUL_OP mode
1033 * r->a[7] is unused, the routine runs on the caller's stack
1034 */
1035
1036 void Execute68k(uint32 pc, M68kRegisters *r)
1037 {
1038 current_cpu->execute_68k(pc, r);
1039 }
1040
1041 /*
1042 * Execute 68k A-Trap from EMUL_OP routine
1043 * r->a[7] is unused, the routine runs on the caller's stack
1044 */
1045
1046 void Execute68kTrap(uint16 trap, M68kRegisters *r)
1047 {
1048 SheepVar proc_var(4);
1049 uint32 proc = proc_var.addr();
1050 WriteMacInt16(proc, trap);
1051 WriteMacInt16(proc + 2, M68K_RTS);
1052 Execute68k(proc, r);
1053 }
1054
1055 /*
1056 * Call MacOS PPC code
1057 */
1058
1059 uint32 call_macos(uint32 tvect)
1060 {
1061 return current_cpu->execute_macos_code(tvect, 0, NULL);
1062 }
1063
1064 uint32 call_macos1(uint32 tvect, uint32 arg1)
1065 {
1066 const uint32 args[] = { arg1 };
1067 return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
1068 }
1069
1070 uint32 call_macos2(uint32 tvect, uint32 arg1, uint32 arg2)
1071 {
1072 const uint32 args[] = { arg1, arg2 };
1073 return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
1074 }
1075
1076 uint32 call_macos3(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3)
1077 {
1078 const uint32 args[] = { arg1, arg2, arg3 };
1079 return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
1080 }
1081
1082 uint32 call_macos4(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4)
1083 {
1084 const uint32 args[] = { arg1, arg2, arg3, arg4 };
1085 return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
1086 }
1087
1088 uint32 call_macos5(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5)
1089 {
1090 const uint32 args[] = { arg1, arg2, arg3, arg4, arg5 };
1091 return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
1092 }
1093
1094 uint32 call_macos6(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5, uint32 arg6)
1095 {
1096 const uint32 args[] = { arg1, arg2, arg3, arg4, arg5, arg6 };
1097 return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
1098 }
1099
1100 uint32 call_macos7(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5, uint32 arg6, uint32 arg7)
1101 {
1102 const uint32 args[] = { arg1, arg2, arg3, arg4, arg5, arg6, arg7 };
1103 return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
1104 }
1105
1106 /*
1107 * Resource Manager thunks
1108 */
1109
1110 void get_resource(void)
1111 {
1112 current_cpu->get_resource(ReadMacInt32(XLM_GET_RESOURCE));
1113 }
1114
1115 void get_1_resource(void)
1116 {
1117 current_cpu->get_resource(ReadMacInt32(XLM_GET_1_RESOURCE));
1118 }
1119
1120 void get_ind_resource(void)
1121 {
1122 current_cpu->get_resource(ReadMacInt32(XLM_GET_IND_RESOURCE));
1123 }
1124
1125 void get_1_ind_resource(void)
1126 {
1127 current_cpu->get_resource(ReadMacInt32(XLM_GET_1_IND_RESOURCE));
1128 }
1129
1130 void r_get_resource(void)
1131 {
1132 current_cpu->get_resource(ReadMacInt32(XLM_R_GET_RESOURCE));
1133 }