ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/kpx_cpu/sheepshaver_glue.cpp
Revision: 1.20
Committed: 2003-12-03T10:52:49Z (20 years, 11 months ago) by gbeauche
Branch: MAIN
Changes since 1.19: +7 -1 lines
Log Message:
Add "jit" prefs item. Fix PPC_DECODE_CACHE version to fill in new min_pc &
max_pc members of block info. Increase -finline-limit to 10000 for older gcc

File Contents

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