ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/kpx_cpu/sheepshaver_glue.cpp
Revision: 1.30
Committed: 2004-02-24T11:12:54Z (20 years, 7 months ago) by gbeauche
Branch: MAIN
Changes since 1.29: +4 -0 lines
Log Message:
Make SheepShaver work with OS 8.6 out-of-the-box with no extra patch for
the time being. i.e. ignore writes to the zero page when faking SCSIGlobals

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