ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/kpx_cpu/sheepshaver_glue.cpp
Revision: 1.27
Committed: 2004-02-15T17:17:36Z (20 years, 3 months ago) by gbeauche
Branch: MAIN
Changes since 1.26: +0 -7 lines
Log Message:
AltiVec emulation! ;-)

File Contents

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