ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/kpx_cpu/sheepshaver_glue.cpp
Revision: 1.45
Committed: 2004-06-22T14:18:35Z (20 years, 5 months ago) by gbeauche
Branch: MAIN
Changes since 1.44: +0 -4 lines
Log Message:
Always handle interrupt even if InterruptFlags == 0, though it should not
really happen in practise.

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.37 #include "timer.h"
42 gbeauche 1.1
43     #include <stdio.h>
44 gbeauche 1.31 #include <stdlib.h>
45 gbeauche 1.1
46     #if ENABLE_MON
47     #include "mon.h"
48     #include "mon_disass.h"
49     #endif
50    
51 gbeauche 1.10 #define DEBUG 0
52 gbeauche 1.1 #include "debug.h"
53    
54 gbeauche 1.15 // Emulation time statistics
55 gbeauche 1.44 #ifndef EMUL_TIME_STATS
56     #define EMUL_TIME_STATS 0
57     #endif
58 gbeauche 1.15
59     #if EMUL_TIME_STATS
60     static clock_t emul_start_time;
61 gbeauche 1.44 static uint32 interrupt_count = 0, ppc_interrupt_count = 0;
62 gbeauche 1.15 static clock_t interrupt_time = 0;
63     static uint32 exec68k_count = 0;
64     static clock_t exec68k_time = 0;
65     static uint32 native_exec_count = 0;
66     static clock_t native_exec_time = 0;
67     static uint32 macos_exec_count = 0;
68     static clock_t macos_exec_time = 0;
69     #endif
70    
71 gbeauche 1.1 static void enter_mon(void)
72     {
73     // Start up mon in real-mode
74     #if ENABLE_MON
75     char *arg[4] = {"mon", "-m", "-r", NULL};
76     mon(3, arg);
77     #endif
78     }
79    
80 gbeauche 1.23 // From main_*.cpp
81     extern uintptr SignalStackBase();
82    
83 gbeauche 1.26 // From rsrc_patches.cpp
84     extern "C" void check_load_invoc(uint32 type, int16 id, uint32 h);
85    
86 gbeauche 1.21 // PowerPC EmulOp to exit from emulation looop
87     const uint32 POWERPC_EXEC_RETURN = POWERPC_EMUL_OP | 1;
88    
89 gbeauche 1.36 // Enable interrupt routine safety checks?
90     #define SAFE_INTERRUPT_PPC 1
91    
92 gbeauche 1.1 // Enable Execute68k() safety checks?
93     #define SAFE_EXEC_68K 1
94    
95     // Save FP state in Execute68k()?
96     #define SAVE_FP_EXEC_68K 1
97    
98     // Interrupts in EMUL_OP mode?
99     #define INTERRUPTS_IN_EMUL_OP_MODE 1
100    
101     // Interrupts in native mode?
102     #define INTERRUPTS_IN_NATIVE_MODE 1
103    
104 gbeauche 1.37 // Enable native EMUL_OPs to be run without a mode switch
105     #define ENABLE_NATIVE_EMUL_OP 1
106    
107 gbeauche 1.1 // Pointer to Kernel Data
108 gbeauche 1.4 static KernelData * const kernel_data = (KernelData *)KERNEL_DATA_BASE;
109 gbeauche 1.1
110 gbeauche 1.17 // SIGSEGV handler
111     static sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t);
112    
113 gbeauche 1.38 #if PPC_ENABLE_JIT && PPC_REENTRANT_JIT
114     // Special trampolines for EmulOp and NativeOp
115     static uint8 *emul_op_trampoline;
116     static uint8 *native_op_trampoline;
117     #endif
118    
119 gbeauche 1.20 // JIT Compiler enabled?
120     static inline bool enable_jit_p()
121     {
122     return PrefsFindBool("jit");
123     }
124    
125 gbeauche 1.1
126     /**
127     * PowerPC emulator glue with special 'sheep' opcodes
128     **/
129    
130 gbeauche 1.18 enum {
131     PPC_I(SHEEP) = PPC_I(MAX),
132     PPC_I(SHEEP_MAX)
133     };
134    
135 gbeauche 1.1 class sheepshaver_cpu
136     : public powerpc_cpu
137     {
138     void init_decoder();
139     void execute_sheep(uint32 opcode);
140    
141 gbeauche 1.37 // Filter out EMUL_OP routines that only call native code
142     bool filter_execute_emul_op(uint32 emul_op);
143    
144     // "Native" EMUL_OP routines
145     void execute_emul_op_microseconds();
146     void execute_emul_op_idle_time_1();
147     void execute_emul_op_idle_time_2();
148    
149 gbeauche 1.39 // CPU context to preserve on interrupt
150     class interrupt_context {
151     uint32 gpr[32];
152     uint32 pc;
153     uint32 lr;
154     uint32 ctr;
155     uint32 cr;
156     uint32 xer;
157     sheepshaver_cpu *cpu;
158     const char *where;
159     public:
160     interrupt_context(sheepshaver_cpu *_cpu, const char *_where);
161     ~interrupt_context();
162     };
163    
164 gbeauche 1.1 public:
165    
166 gbeauche 1.10 // Constructor
167     sheepshaver_cpu();
168 gbeauche 1.1
169 gbeauche 1.24 // CR & XER accessors
170 gbeauche 1.1 uint32 get_cr() const { return cr().get(); }
171     void set_cr(uint32 v) { cr().set(v); }
172 gbeauche 1.24 uint32 get_xer() const { return xer().get(); }
173     void set_xer(uint32 v) { xer().set(v); }
174 gbeauche 1.1
175 gbeauche 1.38 // Execute NATIVE_OP routine
176     void execute_native_op(uint32 native_op);
177    
178 gbeauche 1.26 // Execute EMUL_OP routine
179     void execute_emul_op(uint32 emul_op);
180    
181 gbeauche 1.1 // Execute 68k routine
182     void execute_68k(uint32 entry, M68kRegisters *r);
183    
184 gbeauche 1.2 // Execute ppc routine
185     void execute_ppc(uint32 entry);
186    
187 gbeauche 1.1 // Execute MacOS/PPC code
188     uint32 execute_macos_code(uint32 tvect, int nargs, uint32 const *args);
189    
190 gbeauche 1.26 // Compile one instruction
191 gbeauche 1.38 virtual int compile1(codegen_context_t & cg_context);
192 gbeauche 1.26
193 gbeauche 1.1 // Resource manager thunk
194     void get_resource(uint32 old_get_resource);
195    
196     // Handle MacOS interrupt
197 gbeauche 1.4 void interrupt(uint32 entry);
198 gbeauche 1.10 void handle_interrupt();
199 gbeauche 1.2
200 gbeauche 1.17 // Make sure the SIGSEGV handler can access CPU registers
201     friend sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t);
202 gbeauche 1.1 };
203    
204 gbeauche 1.29 // Memory allocator returning areas aligned on 16-byte boundaries
205     void *operator new(size_t size)
206     {
207     void *p;
208    
209 gbeauche 1.31 #if defined(HAVE_POSIX_MEMALIGN)
210 gbeauche 1.29 if (posix_memalign(&p, 16, size) != 0)
211     throw std::bad_alloc();
212 gbeauche 1.31 #elif defined(HAVE_MEMALIGN)
213     p = memalign(16, size);
214     #elif defined(HAVE_VALLOC)
215     p = valloc(size); // page-aligned!
216     #else
217     /* XXX: handle padding ourselves */
218     p = malloc(size);
219     #endif
220 gbeauche 1.29
221     return p;
222     }
223    
224     void operator delete(void *p)
225     {
226 gbeauche 1.31 #if defined(HAVE_MEMALIGN) || defined(HAVE_VALLOC)
227     #if defined(__GLIBC__)
228     // this is known to work only with GNU libc
229     free(p);
230     #endif
231     #else
232 gbeauche 1.29 free(p);
233 gbeauche 1.31 #endif
234 gbeauche 1.29 }
235 gbeauche 1.1
236 gbeauche 1.10 sheepshaver_cpu::sheepshaver_cpu()
237 gbeauche 1.20 : powerpc_cpu(enable_jit_p())
238 gbeauche 1.10 {
239     init_decoder();
240     }
241    
242 gbeauche 1.1 void sheepshaver_cpu::init_decoder()
243     {
244     static const instr_info_t sheep_ii_table[] = {
245     { "sheep",
246 gbeauche 1.13 (execute_pmf)&sheepshaver_cpu::execute_sheep,
247 gbeauche 1.1 NULL,
248 gbeauche 1.18 PPC_I(SHEEP),
249 gbeauche 1.7 D_form, 6, 0, CFLOW_JUMP | CFLOW_TRAP
250 gbeauche 1.1 }
251     };
252    
253     const int ii_count = sizeof(sheep_ii_table)/sizeof(sheep_ii_table[0]);
254     D(bug("SheepShaver extra decode table has %d entries\n", ii_count));
255    
256     for (int i = 0; i < ii_count; i++) {
257     const instr_info_t * ii = &sheep_ii_table[i];
258     init_decoder_entry(ii);
259     }
260     }
261    
262 gbeauche 1.2 /* NativeOp instruction format:
263 gbeauche 1.35 +------------+-------------------------+--+-----------+------------+
264     | 6 | |FN| OP | 2 |
265     +------------+-------------------------+--+-----------+------------+
266     0 5 |6 18 19 20 25 26 31
267 gbeauche 1.2 */
268    
269 gbeauche 1.35 typedef bit_field< 19, 19 > FN_field;
270     typedef bit_field< 20, 25 > NATIVE_OP_field;
271 gbeauche 1.2 typedef bit_field< 26, 31 > EMUL_OP_field;
272    
273 gbeauche 1.37 // "Native" EMUL_OP routines
274     #define GPR_A(REG) gpr(16 + (REG))
275     #define GPR_D(REG) gpr( 8 + (REG))
276    
277     void sheepshaver_cpu::execute_emul_op_microseconds()
278     {
279     Microseconds(GPR_A(0), GPR_D(0));
280     }
281    
282     void sheepshaver_cpu::execute_emul_op_idle_time_1()
283     {
284     // Sleep if no events pending
285     if (ReadMacInt32(0x14c) == 0)
286     Delay_usec(16667);
287     GPR_A(0) = ReadMacInt32(0x2b6);
288     }
289    
290     void sheepshaver_cpu::execute_emul_op_idle_time_2()
291     {
292     // Sleep if no events pending
293     if (ReadMacInt32(0x14c) == 0)
294     Delay_usec(16667);
295     GPR_D(0) = (uint32)-2;
296     }
297    
298     // Filter out EMUL_OP routines that only call native code
299     bool sheepshaver_cpu::filter_execute_emul_op(uint32 emul_op)
300     {
301     switch (emul_op) {
302     case OP_MICROSECONDS:
303     execute_emul_op_microseconds();
304     return true;
305     case OP_IDLE_TIME:
306     execute_emul_op_idle_time_1();
307     return true;
308     case OP_IDLE_TIME_2:
309     execute_emul_op_idle_time_2();
310     return true;
311     }
312     return false;
313     }
314    
315 gbeauche 1.26 // Execute EMUL_OP routine
316     void sheepshaver_cpu::execute_emul_op(uint32 emul_op)
317     {
318 gbeauche 1.37 #if ENABLE_NATIVE_EMUL_OP
319     // First, filter out EMUL_OPs that can be executed without a mode switch
320     if (filter_execute_emul_op(emul_op))
321     return;
322     #endif
323    
324 gbeauche 1.26 M68kRegisters r68;
325     WriteMacInt32(XLM_68K_R25, gpr(25));
326     WriteMacInt32(XLM_RUN_MODE, MODE_EMUL_OP);
327     for (int i = 0; i < 8; i++)
328     r68.d[i] = gpr(8 + i);
329     for (int i = 0; i < 7; i++)
330     r68.a[i] = gpr(16 + i);
331     r68.a[7] = gpr(1);
332     uint32 saved_cr = get_cr() & CR_field<2>::mask();
333     uint32 saved_xer = get_xer();
334     EmulOp(&r68, gpr(24), emul_op);
335     set_cr(saved_cr);
336     set_xer(saved_xer);
337     for (int i = 0; i < 8; i++)
338     gpr(8 + i) = r68.d[i];
339     for (int i = 0; i < 7; i++)
340     gpr(16 + i) = r68.a[i];
341     gpr(1) = r68.a[7];
342     WriteMacInt32(XLM_RUN_MODE, MODE_68K);
343     }
344    
345 gbeauche 1.1 // Execute SheepShaver instruction
346     void sheepshaver_cpu::execute_sheep(uint32 opcode)
347     {
348     // D(bug("Extended opcode %08x at %08x (68k pc %08x)\n", opcode, pc(), gpr(24)));
349     assert((((opcode >> 26) & 0x3f) == 6) && OP_MAX <= 64 + 3);
350    
351     switch (opcode & 0x3f) {
352     case 0: // EMUL_RETURN
353     QuitEmulator();
354     break;
355 gbeauche 1.8
356 gbeauche 1.1 case 1: // EXEC_RETURN
357 gbeauche 1.12 spcflags().set(SPCFLAG_CPU_EXEC_RETURN);
358 gbeauche 1.1 break;
359    
360     case 2: // EXEC_NATIVE
361 gbeauche 1.38 execute_native_op(NATIVE_OP_field::extract(opcode));
362 gbeauche 1.2 if (FN_field::test(opcode))
363     pc() = lr();
364     else
365     pc() += 4;
366 gbeauche 1.1 break;
367    
368 gbeauche 1.26 default: // EMUL_OP
369     execute_emul_op(EMUL_OP_field::extract(opcode) - 3);
370     pc() += 4;
371     break;
372     }
373     }
374    
375     // Compile one instruction
376 gbeauche 1.38 int sheepshaver_cpu::compile1(codegen_context_t & cg_context)
377 gbeauche 1.26 {
378     #if PPC_ENABLE_JIT
379     const instr_info_t *ii = cg_context.instr_info;
380     if (ii->mnemo != PPC_I(SHEEP))
381 gbeauche 1.38 return COMPILE_FAILURE;
382 gbeauche 1.26
383 gbeauche 1.38 int status = COMPILE_FAILURE;
384 gbeauche 1.26 powerpc_dyngen & dg = cg_context.codegen;
385     uint32 opcode = cg_context.opcode;
386    
387     switch (opcode & 0x3f) {
388     case 0: // EMUL_RETURN
389     dg.gen_invoke(QuitEmulator);
390 gbeauche 1.38 status = COMPILE_CODE_OK;
391 gbeauche 1.26 break;
392    
393     case 1: // EXEC_RETURN
394     dg.gen_spcflags_set(SPCFLAG_CPU_EXEC_RETURN);
395 gbeauche 1.38 // Don't check for pending interrupts, we do know we have to
396     // get out of this block ASAP
397     dg.gen_exec_return();
398     status = COMPILE_EPILOGUE_OK;
399 gbeauche 1.26 break;
400    
401     case 2: { // EXEC_NATIVE
402     uint32 selector = NATIVE_OP_field::extract(opcode);
403     switch (selector) {
404 gbeauche 1.38 #if !PPC_REENTRANT_JIT
405     // Filter out functions that may invoke Execute68k() or
406     // CallMacOS(), this would break reentrancy as they could
407     // invalidate the translation cache and even overwrite
408     // continuation code when we are done with them.
409 gbeauche 1.26 case NATIVE_PATCH_NAME_REGISTRY:
410     dg.gen_invoke(DoPatchNameRegistry);
411 gbeauche 1.38 status = COMPILE_CODE_OK;
412 gbeauche 1.26 break;
413     case NATIVE_VIDEO_INSTALL_ACCEL:
414     dg.gen_invoke(VideoInstallAccel);
415 gbeauche 1.38 status = COMPILE_CODE_OK;
416 gbeauche 1.26 break;
417     case NATIVE_VIDEO_VBL:
418     dg.gen_invoke(VideoVBL);
419 gbeauche 1.38 status = COMPILE_CODE_OK;
420 gbeauche 1.26 break;
421     case NATIVE_GET_RESOURCE:
422     case NATIVE_GET_1_RESOURCE:
423     case NATIVE_GET_IND_RESOURCE:
424     case NATIVE_GET_1_IND_RESOURCE:
425     case NATIVE_R_GET_RESOURCE: {
426     static const uint32 get_resource_ptr[] = {
427     XLM_GET_RESOURCE,
428     XLM_GET_1_RESOURCE,
429     XLM_GET_IND_RESOURCE,
430     XLM_GET_1_IND_RESOURCE,
431     XLM_R_GET_RESOURCE
432     };
433     uint32 old_get_resource = ReadMacInt32(get_resource_ptr[selector - NATIVE_GET_RESOURCE]);
434     typedef void (*func_t)(dyngen_cpu_base, uint32);
435     func_t func = (func_t)nv_mem_fun(&sheepshaver_cpu::get_resource).ptr();
436     dg.gen_invoke_CPU_im(func, old_get_resource);
437 gbeauche 1.38 status = COMPILE_CODE_OK;
438 gbeauche 1.26 break;
439     }
440     case NATIVE_CHECK_LOAD_INVOC:
441     dg.gen_load_T0_GPR(3);
442     dg.gen_load_T1_GPR(4);
443     dg.gen_se_16_32_T1();
444     dg.gen_load_T2_GPR(5);
445     dg.gen_invoke_T0_T1_T2((void (*)(uint32, uint32, uint32))check_load_invoc);
446 gbeauche 1.38 status = COMPILE_CODE_OK;
447     break;
448     #endif
449     case NATIVE_DISABLE_INTERRUPT:
450     dg.gen_invoke(DisableInterrupt);
451     status = COMPILE_CODE_OK;
452     break;
453     case NATIVE_ENABLE_INTERRUPT:
454     dg.gen_invoke(EnableInterrupt);
455     status = COMPILE_CODE_OK;
456 gbeauche 1.35 break;
457     case NATIVE_BITBLT:
458     dg.gen_load_T0_GPR(3);
459     dg.gen_invoke_T0((void (*)(uint32))NQD_bitblt);
460 gbeauche 1.38 status = COMPILE_CODE_OK;
461 gbeauche 1.35 break;
462     case NATIVE_INVRECT:
463     dg.gen_load_T0_GPR(3);
464     dg.gen_invoke_T0((void (*)(uint32))NQD_invrect);
465 gbeauche 1.38 status = COMPILE_CODE_OK;
466 gbeauche 1.35 break;
467     case NATIVE_FILLRECT:
468     dg.gen_load_T0_GPR(3);
469     dg.gen_invoke_T0((void (*)(uint32))NQD_fillrect);
470 gbeauche 1.38 status = COMPILE_CODE_OK;
471 gbeauche 1.26 break;
472     }
473 gbeauche 1.38 // Could we fully translate this NativeOp?
474 gbeauche 1.42 if (status == COMPILE_CODE_OK) {
475     if (!FN_field::test(opcode))
476     cg_context.done_compile = false;
477     else {
478 gbeauche 1.26 dg.gen_load_A0_LR();
479     dg.gen_set_PC_A0();
480 gbeauche 1.42 cg_context.done_compile = true;
481 gbeauche 1.26 }
482 gbeauche 1.38 break;
483     }
484     #if PPC_REENTRANT_JIT
485     // Try to execute NativeOp trampoline
486 gbeauche 1.42 if (!FN_field::test(opcode))
487     dg.gen_set_PC_im(cg_context.pc + 4);
488     else {
489     dg.gen_load_A0_LR();
490     dg.gen_set_PC_A0();
491     }
492 gbeauche 1.38 dg.gen_mov_32_T0_im(selector);
493     dg.gen_jmp(native_op_trampoline);
494     cg_context.done_compile = true;
495     status = COMPILE_EPILOGUE_OK;
496     break;
497     #endif
498     // Invoke NativeOp handler
499 gbeauche 1.42 if (!FN_field::test(opcode)) {
500     typedef void (*func_t)(dyngen_cpu_base, uint32);
501     func_t func = (func_t)nv_mem_fun(&sheepshaver_cpu::execute_native_op).ptr();
502     dg.gen_invoke_CPU_im(func, selector);
503     cg_context.done_compile = false;
504     status = COMPILE_CODE_OK;
505     }
506     // Otherwise, let it generate a call to execute_sheep() which
507     // will cause necessary updates to the program counter
508 gbeauche 1.26 break;
509     }
510    
511 gbeauche 1.1 default: { // EMUL_OP
512 gbeauche 1.37 uint32 emul_op = EMUL_OP_field::extract(opcode) - 3;
513     #if ENABLE_NATIVE_EMUL_OP
514     typedef void (*emul_op_func_t)(dyngen_cpu_base);
515     emul_op_func_t emul_op_func = 0;
516     switch (emul_op) {
517     case OP_MICROSECONDS:
518     emul_op_func = (emul_op_func_t)nv_mem_fun(&sheepshaver_cpu::execute_emul_op_microseconds).ptr();
519     break;
520     case OP_IDLE_TIME:
521     emul_op_func = (emul_op_func_t)nv_mem_fun(&sheepshaver_cpu::execute_emul_op_idle_time_1).ptr();
522     break;
523     case OP_IDLE_TIME_2:
524     emul_op_func = (emul_op_func_t)nv_mem_fun(&sheepshaver_cpu::execute_emul_op_idle_time_2).ptr();
525     break;
526     }
527     if (emul_op_func) {
528     dg.gen_invoke_CPU(emul_op_func);
529     cg_context.done_compile = false;
530 gbeauche 1.38 status = COMPILE_CODE_OK;
531 gbeauche 1.37 break;
532     }
533     #endif
534 gbeauche 1.38 #if PPC_REENTRANT_JIT
535     // Try to execute EmulOp trampoline
536     dg.gen_set_PC_im(cg_context.pc + 4);
537     dg.gen_mov_32_T0_im(emul_op);
538     dg.gen_jmp(emul_op_trampoline);
539     cg_context.done_compile = true;
540     status = COMPILE_EPILOGUE_OK;
541     break;
542     #endif
543     // Invoke EmulOp handler
544 gbeauche 1.26 typedef void (*func_t)(dyngen_cpu_base, uint32);
545     func_t func = (func_t)nv_mem_fun(&sheepshaver_cpu::execute_emul_op).ptr();
546 gbeauche 1.37 dg.gen_invoke_CPU_im(func, emul_op);
547 gbeauche 1.26 cg_context.done_compile = false;
548 gbeauche 1.38 status = COMPILE_CODE_OK;
549 gbeauche 1.1 break;
550     }
551     }
552 gbeauche 1.38 return status;
553 gbeauche 1.26 #endif
554 gbeauche 1.38 return COMPILE_FAILURE;
555 gbeauche 1.1 }
556    
557 gbeauche 1.39 // CPU context to preserve on interrupt
558     sheepshaver_cpu::interrupt_context::interrupt_context(sheepshaver_cpu *_cpu, const char *_where)
559     {
560     #if SAFE_INTERRUPT_PPC >= 2
561     cpu = _cpu;
562     where = _where;
563    
564     // Save interrupt context
565     memcpy(&gpr[0], &cpu->gpr(0), sizeof(gpr));
566     pc = cpu->pc();
567     lr = cpu->lr();
568     ctr = cpu->ctr();
569     cr = cpu->get_cr();
570     xer = cpu->get_xer();
571     #endif
572     }
573    
574     sheepshaver_cpu::interrupt_context::~interrupt_context()
575     {
576     #if SAFE_INTERRUPT_PPC >= 2
577     // Check whether CPU context was preserved by interrupt
578     if (memcmp(&gpr[0], &cpu->gpr(0), sizeof(gpr)) != 0) {
579     printf("FATAL: %s: interrupt clobbers registers\n", where);
580     for (int i = 0; i < 32; i++)
581     if (gpr[i] != cpu->gpr(i))
582     printf(" r%d: %08x -> %08x\n", i, gpr[i], cpu->gpr(i));
583     }
584     if (pc != cpu->pc())
585     printf("FATAL: %s: interrupt clobbers PC\n", where);
586     if (lr != cpu->lr())
587     printf("FATAL: %s: interrupt clobbers LR\n", where);
588     if (ctr != cpu->ctr())
589     printf("FATAL: %s: interrupt clobbers CTR\n", where);
590     if (cr != cpu->get_cr())
591     printf("FATAL: %s: interrupt clobbers CR\n", where);
592     if (xer != cpu->get_xer())
593     printf("FATAL: %s: interrupt clobbers XER\n", where);
594     #endif
595     }
596    
597 gbeauche 1.1 // Handle MacOS interrupt
598 gbeauche 1.4 void sheepshaver_cpu::interrupt(uint32 entry)
599 gbeauche 1.1 {
600 gbeauche 1.15 #if EMUL_TIME_STATS
601 gbeauche 1.44 ppc_interrupt_count++;
602 gbeauche 1.15 const clock_t interrupt_start = clock();
603     #endif
604    
605 gbeauche 1.36 #if SAFE_INTERRUPT_PPC
606     static int depth = 0;
607     if (depth != 0)
608     printf("FATAL: sheepshaver_cpu::interrupt() called more than once: %d\n", depth);
609     depth++;
610     #endif
611    
612 gbeauche 1.2 // Save program counters and branch registers
613     uint32 saved_pc = pc();
614     uint32 saved_lr = lr();
615     uint32 saved_ctr= ctr();
616 gbeauche 1.4 uint32 saved_sp = gpr(1);
617 gbeauche 1.2
618 gbeauche 1.4 // Initialize stack pointer to SheepShaver alternate stack base
619 gbeauche 1.23 gpr(1) = SignalStackBase() - 64;
620 gbeauche 1.1
621     // Build trampoline to return from interrupt
622 gbeauche 1.21 SheepVar32 trampoline = POWERPC_EXEC_RETURN;
623 gbeauche 1.1
624     // Prepare registers for nanokernel interrupt routine
625 gbeauche 1.5 kernel_data->v[0x004 >> 2] = htonl(gpr(1));
626     kernel_data->v[0x018 >> 2] = htonl(gpr(6));
627 gbeauche 1.1
628 gbeauche 1.5 gpr(6) = ntohl(kernel_data->v[0x65c >> 2]);
629 gbeauche 1.2 assert(gpr(6) != 0);
630 gbeauche 1.1 WriteMacInt32(gpr(6) + 0x13c, gpr(7));
631     WriteMacInt32(gpr(6) + 0x144, gpr(8));
632     WriteMacInt32(gpr(6) + 0x14c, gpr(9));
633     WriteMacInt32(gpr(6) + 0x154, gpr(10));
634     WriteMacInt32(gpr(6) + 0x15c, gpr(11));
635     WriteMacInt32(gpr(6) + 0x164, gpr(12));
636     WriteMacInt32(gpr(6) + 0x16c, gpr(13));
637    
638     gpr(1) = KernelDataAddr;
639 gbeauche 1.5 gpr(7) = ntohl(kernel_data->v[0x660 >> 2]);
640 gbeauche 1.1 gpr(8) = 0;
641 gbeauche 1.21 gpr(10) = trampoline.addr();
642     gpr(12) = trampoline.addr();
643 gbeauche 1.8 gpr(13) = get_cr();
644 gbeauche 1.1
645     // rlwimi. r7,r7,8,0,0
646     uint32 result = op_ppc_rlwimi::apply(gpr(7), 8, 0x80000000, gpr(7));
647     record_cr0(result);
648     gpr(7) = result;
649    
650     gpr(11) = 0xf072; // MSR (SRR1)
651 gbeauche 1.8 cr().set((gpr(11) & 0x0fff0000) | (get_cr() & ~0x0fff0000));
652 gbeauche 1.1
653     // Enter nanokernel
654     execute(entry);
655    
656 gbeauche 1.2 // Restore program counters and branch registers
657     pc() = saved_pc;
658     lr() = saved_lr;
659     ctr()= saved_ctr;
660 gbeauche 1.4 gpr(1) = saved_sp;
661 gbeauche 1.15
662     #if EMUL_TIME_STATS
663     interrupt_time += (clock() - interrupt_start);
664     #endif
665 gbeauche 1.36
666     #if SAFE_INTERRUPT_PPC
667     depth--;
668     #endif
669 gbeauche 1.1 }
670    
671     // Execute 68k routine
672     void sheepshaver_cpu::execute_68k(uint32 entry, M68kRegisters *r)
673     {
674 gbeauche 1.15 #if EMUL_TIME_STATS
675     exec68k_count++;
676     const clock_t exec68k_start = clock();
677     #endif
678    
679 gbeauche 1.1 #if SAFE_EXEC_68K
680     if (ReadMacInt32(XLM_RUN_MODE) != MODE_EMUL_OP)
681     printf("FATAL: Execute68k() not called from EMUL_OP mode\n");
682     #endif
683    
684     // Save program counters and branch registers
685     uint32 saved_pc = pc();
686     uint32 saved_lr = lr();
687     uint32 saved_ctr= ctr();
688 gbeauche 1.8 uint32 saved_cr = get_cr();
689 gbeauche 1.1
690     // Create MacOS stack frame
691 gbeauche 1.6 // FIXME: make sure MacOS doesn't expect PPC registers to live on top
692 gbeauche 1.1 uint32 sp = gpr(1);
693 gbeauche 1.6 gpr(1) -= 56;
694 gbeauche 1.1 WriteMacInt32(gpr(1), sp);
695    
696     // Save PowerPC registers
697 gbeauche 1.6 uint32 saved_GPRs[19];
698     memcpy(&saved_GPRs[0], &gpr(13), sizeof(uint32)*(32-13));
699 gbeauche 1.1 #if SAVE_FP_EXEC_68K
700 gbeauche 1.6 double saved_FPRs[18];
701     memcpy(&saved_FPRs[0], &fpr(14), sizeof(double)*(32-14));
702 gbeauche 1.1 #endif
703    
704     // Setup registers for 68k emulator
705 gbeauche 1.2 cr().set(CR_SO_field<2>::mask()); // Supervisor mode
706 gbeauche 1.1 for (int i = 0; i < 8; i++) // d[0]..d[7]
707     gpr(8 + i) = r->d[i];
708     for (int i = 0; i < 7; i++) // a[0]..a[6]
709     gpr(16 + i) = r->a[i];
710     gpr(23) = 0;
711     gpr(24) = entry;
712     gpr(25) = ReadMacInt32(XLM_68K_R25); // MSB of SR
713     gpr(26) = 0;
714     gpr(28) = 0; // VBR
715 gbeauche 1.5 gpr(29) = ntohl(kernel_data->ed.v[0x74 >> 2]); // Pointer to opcode table
716     gpr(30) = ntohl(kernel_data->ed.v[0x78 >> 2]); // Address of emulator
717 gbeauche 1.1 gpr(31) = KernelDataAddr + 0x1000;
718    
719     // Push return address (points to EXEC_RETURN opcode) on stack
720     gpr(1) -= 4;
721     WriteMacInt32(gpr(1), XLM_EXEC_RETURN_OPCODE);
722    
723     // Rentering 68k emulator
724     WriteMacInt32(XLM_RUN_MODE, MODE_68K);
725    
726     // Set r0 to 0 for 68k emulator
727     gpr(0) = 0;
728    
729     // Execute 68k opcode
730     uint32 opcode = ReadMacInt16(gpr(24));
731     gpr(27) = (int32)(int16)ReadMacInt16(gpr(24) += 2);
732     gpr(29) += opcode * 8;
733     execute(gpr(29));
734    
735     // Save r25 (contains current 68k interrupt level)
736     WriteMacInt32(XLM_68K_R25, gpr(25));
737    
738     // Reentering EMUL_OP mode
739     WriteMacInt32(XLM_RUN_MODE, MODE_EMUL_OP);
740    
741     // Save 68k registers
742     for (int i = 0; i < 8; i++) // d[0]..d[7]
743     r->d[i] = gpr(8 + i);
744     for (int i = 0; i < 7; i++) // a[0]..a[6]
745     r->a[i] = gpr(16 + i);
746    
747     // Restore PowerPC registers
748 gbeauche 1.6 memcpy(&gpr(13), &saved_GPRs[0], sizeof(uint32)*(32-13));
749 gbeauche 1.1 #if SAVE_FP_EXEC_68K
750 gbeauche 1.6 memcpy(&fpr(14), &saved_FPRs[0], sizeof(double)*(32-14));
751 gbeauche 1.1 #endif
752    
753     // Cleanup stack
754 gbeauche 1.6 gpr(1) += 56;
755 gbeauche 1.1
756     // Restore program counters and branch registers
757     pc() = saved_pc;
758     lr() = saved_lr;
759     ctr()= saved_ctr;
760 gbeauche 1.8 set_cr(saved_cr);
761 gbeauche 1.15
762     #if EMUL_TIME_STATS
763     exec68k_time += (clock() - exec68k_start);
764     #endif
765 gbeauche 1.1 }
766    
767     // Call MacOS PPC code
768     uint32 sheepshaver_cpu::execute_macos_code(uint32 tvect, int nargs, uint32 const *args)
769     {
770 gbeauche 1.15 #if EMUL_TIME_STATS
771     macos_exec_count++;
772     const clock_t macos_exec_start = clock();
773     #endif
774    
775 gbeauche 1.1 // Save program counters and branch registers
776     uint32 saved_pc = pc();
777     uint32 saved_lr = lr();
778     uint32 saved_ctr= ctr();
779    
780     // Build trampoline with EXEC_RETURN
781 gbeauche 1.21 SheepVar32 trampoline = POWERPC_EXEC_RETURN;
782     lr() = trampoline.addr();
783 gbeauche 1.1
784     gpr(1) -= 64; // Create stack frame
785     uint32 proc = ReadMacInt32(tvect); // Get routine address
786     uint32 toc = ReadMacInt32(tvect + 4); // Get TOC pointer
787    
788     // Save PowerPC registers
789     uint32 regs[8];
790     regs[0] = gpr(2);
791     for (int i = 0; i < nargs; i++)
792     regs[i + 1] = gpr(i + 3);
793    
794     // Prepare and call MacOS routine
795     gpr(2) = toc;
796     for (int i = 0; i < nargs; i++)
797     gpr(i + 3) = args[i];
798     execute(proc);
799     uint32 retval = gpr(3);
800    
801     // Restore PowerPC registers
802     for (int i = 0; i <= nargs; i++)
803     gpr(i + 2) = regs[i];
804    
805     // Cleanup stack
806     gpr(1) += 64;
807    
808     // Restore program counters and branch registers
809     pc() = saved_pc;
810     lr() = saved_lr;
811     ctr()= saved_ctr;
812    
813 gbeauche 1.15 #if EMUL_TIME_STATS
814     macos_exec_time += (clock() - macos_exec_start);
815     #endif
816    
817 gbeauche 1.1 return retval;
818     }
819    
820 gbeauche 1.2 // Execute ppc routine
821     inline void sheepshaver_cpu::execute_ppc(uint32 entry)
822     {
823     // Save branch registers
824     uint32 saved_lr = lr();
825    
826 gbeauche 1.21 SheepVar32 trampoline = POWERPC_EXEC_RETURN;
827     WriteMacInt32(trampoline.addr(), POWERPC_EXEC_RETURN);
828     lr() = trampoline.addr();
829 gbeauche 1.2
830     execute(entry);
831    
832     // Restore branch registers
833     lr() = saved_lr;
834     }
835    
836 gbeauche 1.1 // Resource Manager thunk
837     inline void sheepshaver_cpu::get_resource(uint32 old_get_resource)
838     {
839 gbeauche 1.2 uint32 type = gpr(3);
840     int16 id = gpr(4);
841    
842     // Create stack frame
843     gpr(1) -= 56;
844    
845     // Call old routine
846     execute_ppc(old_get_resource);
847    
848     // Call CheckLoad()
849 gbeauche 1.5 uint32 handle = gpr(3);
850 gbeauche 1.2 check_load_invoc(type, id, handle);
851 gbeauche 1.5 gpr(3) = handle;
852 gbeauche 1.2
853     // Cleanup stack
854     gpr(1) += 56;
855 gbeauche 1.1 }
856    
857    
858     /**
859     * SheepShaver CPU engine interface
860     **/
861    
862 gbeauche 1.41 // PowerPC CPU emulator
863     static sheepshaver_cpu *ppc_cpu = NULL;
864 gbeauche 1.1
865 gbeauche 1.7 void FlushCodeCache(uintptr start, uintptr end)
866     {
867     D(bug("FlushCodeCache(%08x, %08x)\n", start, end));
868 gbeauche 1.41 ppc_cpu->invalidate_cache_range(start, end);
869 gbeauche 1.2 }
870    
871 gbeauche 1.1 // Dump PPC registers
872     static void dump_registers(void)
873     {
874 gbeauche 1.41 ppc_cpu->dump_registers();
875 gbeauche 1.1 }
876    
877     // Dump log
878     static void dump_log(void)
879     {
880 gbeauche 1.41 ppc_cpu->dump_log();
881 gbeauche 1.1 }
882    
883     /*
884     * Initialize CPU emulation
885     */
886    
887 gbeauche 1.3 static sigsegv_return_t sigsegv_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction)
888 gbeauche 1.1 {
889     #if ENABLE_VOSF
890 gbeauche 1.3 // Handle screen fault
891     extern bool Screen_fault_handler(sigsegv_address_t, sigsegv_address_t);
892     if (Screen_fault_handler(fault_address, fault_instruction))
893     return SIGSEGV_RETURN_SUCCESS;
894 gbeauche 1.1 #endif
895 gbeauche 1.3
896     const uintptr addr = (uintptr)fault_address;
897     #if HAVE_SIGSEGV_SKIP_INSTRUCTION
898     // Ignore writes to ROM
899     if ((addr - ROM_BASE) < ROM_SIZE)
900     return SIGSEGV_RETURN_SKIP_INSTRUCTION;
901    
902 gbeauche 1.17 // Get program counter of target CPU
903 gbeauche 1.41 sheepshaver_cpu * const cpu = ppc_cpu;
904 gbeauche 1.17 const uint32 pc = cpu->pc();
905    
906     // Fault in Mac ROM or RAM?
907 gbeauche 1.43 bool mac_fault = (pc >= ROM_BASE) && (pc < (ROM_BASE + ROM_AREA_SIZE)) || (pc >= RAMBase) && (pc < (RAMBase + RAMSize)) || (pc >= DR_CACHE_BASE && pc < (DR_CACHE_BASE + DR_CACHE_SIZE));
908 gbeauche 1.17 if (mac_fault) {
909    
910     // "VM settings" during MacOS 8 installation
911     if (pc == ROM_BASE + 0x488160 && cpu->gpr(20) == 0xf8000000)
912     return SIGSEGV_RETURN_SKIP_INSTRUCTION;
913    
914     // MacOS 8.5 installation
915     else if (pc == ROM_BASE + 0x488140 && cpu->gpr(16) == 0xf8000000)
916     return SIGSEGV_RETURN_SKIP_INSTRUCTION;
917    
918     // MacOS 8 serial drivers on startup
919     else if (pc == ROM_BASE + 0x48e080 && (cpu->gpr(8) == 0xf3012002 || cpu->gpr(8) == 0xf3012000))
920     return SIGSEGV_RETURN_SKIP_INSTRUCTION;
921    
922     // MacOS 8.1 serial drivers on startup
923     else if (pc == ROM_BASE + 0x48c5e0 && (cpu->gpr(20) == 0xf3012002 || cpu->gpr(20) == 0xf3012000))
924     return SIGSEGV_RETURN_SKIP_INSTRUCTION;
925     else if (pc == ROM_BASE + 0x4a10a0 && (cpu->gpr(20) == 0xf3012002 || cpu->gpr(20) == 0xf3012000))
926     return SIGSEGV_RETURN_SKIP_INSTRUCTION;
927 gbeauche 1.43
928     // MacOS 8.6 serial drivers on startup (with DR Cache and OldWorld ROM)
929     else if ((pc - DR_CACHE_BASE) < DR_CACHE_SIZE && (cpu->gpr(16) == 0xf3012002 || cpu->gpr(16) == 0xf3012000))
930     return SIGSEGV_RETURN_SKIP_INSTRUCTION;
931     else if ((pc - DR_CACHE_BASE) < DR_CACHE_SIZE && (cpu->gpr(20) == 0xf3012002 || cpu->gpr(20) == 0xf3012000))
932     return SIGSEGV_RETURN_SKIP_INSTRUCTION;
933 gbeauche 1.17
934 gbeauche 1.30 // Ignore writes to the zero page
935     else if ((uint32)(addr - SheepMem::ZeroPage()) < (uint32)SheepMem::PageSize())
936     return SIGSEGV_RETURN_SKIP_INSTRUCTION;
937    
938 gbeauche 1.17 // Ignore all other faults, if requested
939     if (PrefsFindBool("ignoresegv"))
940     return SIGSEGV_RETURN_SKIP_INSTRUCTION;
941     }
942 gbeauche 1.3 #else
943     #error "FIXME: You don't have the capability to skip instruction within signal handlers"
944 gbeauche 1.1 #endif
945 gbeauche 1.3
946     printf("SIGSEGV\n");
947     printf(" pc %p\n", fault_instruction);
948     printf(" ea %p\n", fault_address);
949 gbeauche 1.1 dump_registers();
950 gbeauche 1.41 ppc_cpu->dump_log();
951 gbeauche 1.1 enter_mon();
952     QuitEmulator();
953 gbeauche 1.3
954     return SIGSEGV_RETURN_FAILURE;
955 gbeauche 1.1 }
956    
957     void init_emul_ppc(void)
958     {
959     // Initialize main CPU emulator
960 gbeauche 1.41 ppc_cpu = new sheepshaver_cpu();
961     ppc_cpu->set_register(powerpc_registers::GPR(3), any_register((uint32)ROM_BASE + 0x30d000));
962     ppc_cpu->set_register(powerpc_registers::GPR(4), any_register(KernelDataAddr + 0x1000));
963 gbeauche 1.1 WriteMacInt32(XLM_RUN_MODE, MODE_68K);
964    
965 gbeauche 1.3 // Install the handler for SIGSEGV
966     sigsegv_install_handler(sigsegv_handler);
967 gbeauche 1.4
968 gbeauche 1.1 #if ENABLE_MON
969     // Install "regs" command in cxmon
970     mon_add_command("regs", dump_registers, "regs Dump PowerPC registers\n");
971     mon_add_command("log", dump_log, "log Dump PowerPC emulation log\n");
972     #endif
973 gbeauche 1.15
974     #if EMUL_TIME_STATS
975     emul_start_time = clock();
976     #endif
977 gbeauche 1.1 }
978    
979     /*
980 gbeauche 1.14 * Deinitialize emulation
981     */
982    
983     void exit_emul_ppc(void)
984     {
985 gbeauche 1.15 #if EMUL_TIME_STATS
986     clock_t emul_end_time = clock();
987    
988     printf("### Statistics for SheepShaver emulation parts\n");
989     const clock_t emul_time = emul_end_time - emul_start_time;
990     printf("Total emulation time : %.1f sec\n", double(emul_time) / double(CLOCKS_PER_SEC));
991     printf("Total interrupt count: %d (%2.1f Hz)\n", interrupt_count,
992     (double(interrupt_count) * CLOCKS_PER_SEC) / double(emul_time));
993 gbeauche 1.44 printf("Total ppc interrupt count: %d (%2.1f %%)\n", ppc_interrupt_count,
994     (double(ppc_interrupt_count) * 100.0) / double(interrupt_count));
995 gbeauche 1.15
996     #define PRINT_STATS(LABEL, VAR_PREFIX) do { \
997     printf("Total " LABEL " count : %d\n", VAR_PREFIX##_count); \
998     printf("Total " LABEL " time : %.1f sec (%.1f%%)\n", \
999     double(VAR_PREFIX##_time) / double(CLOCKS_PER_SEC), \
1000     100.0 * double(VAR_PREFIX##_time) / double(emul_time)); \
1001     } while (0)
1002    
1003     PRINT_STATS("Execute68k[Trap] execution", exec68k);
1004     PRINT_STATS("NativeOp execution", native_exec);
1005     PRINT_STATS("MacOS routine execution", macos_exec);
1006    
1007     #undef PRINT_STATS
1008     printf("\n");
1009     #endif
1010    
1011 gbeauche 1.41 delete ppc_cpu;
1012 gbeauche 1.14 }
1013    
1014 gbeauche 1.38 #if PPC_ENABLE_JIT && PPC_REENTRANT_JIT
1015     // Initialize EmulOp trampolines
1016     void init_emul_op_trampolines(basic_dyngen & dg)
1017     {
1018     typedef void (*func_t)(dyngen_cpu_base, uint32);
1019     func_t func;
1020    
1021     // EmulOp
1022     emul_op_trampoline = dg.gen_start();
1023     func = (func_t)nv_mem_fun(&sheepshaver_cpu::execute_emul_op).ptr();
1024     dg.gen_invoke_CPU_T0(func);
1025     dg.gen_exec_return();
1026     dg.gen_end();
1027    
1028     // NativeOp
1029     native_op_trampoline = dg.gen_start();
1030     func = (func_t)nv_mem_fun(&sheepshaver_cpu::execute_native_op).ptr();
1031     dg.gen_invoke_CPU_T0(func);
1032     dg.gen_exec_return();
1033     dg.gen_end();
1034    
1035     D(bug("EmulOp trampoline: %p\n", emul_op_trampoline));
1036     D(bug("NativeOp trampoline: %p\n", native_op_trampoline));
1037     }
1038     #endif
1039    
1040 gbeauche 1.14 /*
1041 gbeauche 1.1 * Emulation loop
1042     */
1043    
1044     void emul_ppc(uint32 entry)
1045     {
1046 gbeauche 1.24 #if 0
1047 gbeauche 1.41 ppc_cpu->start_log();
1048 gbeauche 1.10 #endif
1049     // start emulation loop and enable code translation or caching
1050 gbeauche 1.41 ppc_cpu->execute(entry);
1051 gbeauche 1.1 }
1052    
1053     /*
1054     * Handle PowerPC interrupt
1055     */
1056    
1057 gbeauche 1.2 void TriggerInterrupt(void)
1058     {
1059     #if 0
1060     WriteMacInt32(0x16a, ReadMacInt32(0x16a) + 1);
1061     #else
1062 gbeauche 1.10 // Trigger interrupt to main cpu only
1063 gbeauche 1.41 if (ppc_cpu)
1064     ppc_cpu->trigger_interrupt();
1065 gbeauche 1.2 #endif
1066     }
1067    
1068 gbeauche 1.10 void sheepshaver_cpu::handle_interrupt(void)
1069 gbeauche 1.1 {
1070     // Do nothing if interrupts are disabled
1071 gbeauche 1.16 if (*(int32 *)XLM_IRQ_NEST > 0)
1072 gbeauche 1.1 return;
1073    
1074 gbeauche 1.40 // Current interrupt nest level
1075     static int interrupt_depth = 0;
1076     ++interrupt_depth;
1077 gbeauche 1.44 #if EMUL_TIME_STATS
1078     interrupt_count++;
1079     #endif
1080 gbeauche 1.40
1081 gbeauche 1.1 // Disable MacOS stack sniffer
1082     WriteMacInt32(0x110, 0);
1083    
1084     // Interrupt action depends on current run mode
1085     switch (ReadMacInt32(XLM_RUN_MODE)) {
1086     case MODE_68K:
1087     // 68k emulator active, trigger 68k interrupt level 1
1088     WriteMacInt16(tswap32(kernel_data->v[0x67c >> 2]), 1);
1089 gbeauche 1.10 set_cr(get_cr() | tswap32(kernel_data->v[0x674 >> 2]));
1090 gbeauche 1.1 break;
1091    
1092     #if INTERRUPTS_IN_NATIVE_MODE
1093     case MODE_NATIVE:
1094     // 68k emulator inactive, in nanokernel?
1095 gbeauche 1.40 if (gpr(1) != KernelDataAddr && interrupt_depth == 1) {
1096 gbeauche 1.39 interrupt_context ctx(this, "PowerPC mode");
1097    
1098 gbeauche 1.1 // Prepare for 68k interrupt level 1
1099     WriteMacInt16(tswap32(kernel_data->v[0x67c >> 2]), 1);
1100     WriteMacInt32(tswap32(kernel_data->v[0x658 >> 2]) + 0xdc,
1101     ReadMacInt32(tswap32(kernel_data->v[0x658 >> 2]) + 0xdc)
1102     | tswap32(kernel_data->v[0x674 >> 2]));
1103    
1104     // Execute nanokernel interrupt routine (this will activate the 68k emulator)
1105 gbeauche 1.2 DisableInterrupt();
1106 gbeauche 1.1 if (ROMType == ROMTYPE_NEWWORLD)
1107 gbeauche 1.41 ppc_cpu->interrupt(ROM_BASE + 0x312b1c);
1108 gbeauche 1.1 else
1109 gbeauche 1.41 ppc_cpu->interrupt(ROM_BASE + 0x312a3c);
1110 gbeauche 1.1 }
1111     break;
1112     #endif
1113    
1114     #if INTERRUPTS_IN_EMUL_OP_MODE
1115     case MODE_EMUL_OP:
1116     // 68k emulator active, within EMUL_OP routine, execute 68k interrupt routine directly when interrupt level is 0
1117     if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) {
1118 gbeauche 1.39 interrupt_context ctx(this, "68k mode");
1119 gbeauche 1.44 #if EMUL_TIME_STATS
1120     const clock_t interrupt_start = clock();
1121     #endif
1122 gbeauche 1.1 #if 1
1123     // Execute full 68k interrupt routine
1124     M68kRegisters r;
1125     uint32 old_r25 = ReadMacInt32(XLM_68K_R25); // Save interrupt level
1126     WriteMacInt32(XLM_68K_R25, 0x21); // Execute with interrupt level 1
1127 gbeauche 1.2 static const uint8 proc[] = {
1128     0x3f, 0x3c, 0x00, 0x00, // move.w #$0000,-(sp) (fake format word)
1129     0x48, 0x7a, 0x00, 0x0a, // pea @1(pc) (return address)
1130     0x40, 0xe7, // move sr,-(sp) (saved SR)
1131     0x20, 0x78, 0x00, 0x064, // move.l $64,a0
1132     0x4e, 0xd0, // jmp (a0)
1133     M68K_RTS >> 8, M68K_RTS & 0xff // @1
1134 gbeauche 1.1 };
1135     Execute68k((uint32)proc, &r);
1136     WriteMacInt32(XLM_68K_R25, old_r25); // Restore interrupt level
1137     #else
1138     // Only update cursor
1139     if (HasMacStarted()) {
1140     if (InterruptFlags & INTFLAG_VIA) {
1141     ClearInterruptFlag(INTFLAG_VIA);
1142     ADBInterrupt();
1143 gbeauche 1.22 ExecuteNative(NATIVE_VIDEO_VBL);
1144 gbeauche 1.1 }
1145     }
1146     #endif
1147 gbeauche 1.44 #if EMUL_TIME_STATS
1148     interrupt_time += (clock() - interrupt_start);
1149     #endif
1150 gbeauche 1.1 }
1151     break;
1152     #endif
1153     }
1154 gbeauche 1.40
1155     // We are done with this interrupt
1156     --interrupt_depth;
1157 gbeauche 1.1 }
1158    
1159     static void get_resource(void);
1160     static void get_1_resource(void);
1161     static void get_ind_resource(void);
1162     static void get_1_ind_resource(void);
1163     static void r_get_resource(void);
1164    
1165 gbeauche 1.38 // Execute NATIVE_OP routine
1166     void sheepshaver_cpu::execute_native_op(uint32 selector)
1167 gbeauche 1.1 {
1168 gbeauche 1.15 #if EMUL_TIME_STATS
1169     native_exec_count++;
1170     const clock_t native_exec_start = clock();
1171     #endif
1172    
1173 gbeauche 1.1 switch (selector) {
1174     case NATIVE_PATCH_NAME_REGISTRY:
1175     DoPatchNameRegistry();
1176     break;
1177     case NATIVE_VIDEO_INSTALL_ACCEL:
1178     VideoInstallAccel();
1179     break;
1180     case NATIVE_VIDEO_VBL:
1181     VideoVBL();
1182     break;
1183     case NATIVE_VIDEO_DO_DRIVER_IO:
1184 gbeauche 1.38 gpr(3) = (int32)(int16)VideoDoDriverIO((void *)gpr(3), (void *)gpr(4),
1185     (void *)gpr(5), gpr(6), gpr(7));
1186 gbeauche 1.1 break;
1187 gbeauche 1.16 #ifdef WORDS_BIGENDIAN
1188     case NATIVE_ETHER_IRQ:
1189     EtherIRQ();
1190     break;
1191     case NATIVE_ETHER_INIT:
1192 gbeauche 1.38 gpr(3) = InitStreamModule((void *)gpr(3));
1193 gbeauche 1.16 break;
1194     case NATIVE_ETHER_TERM:
1195     TerminateStreamModule();
1196     break;
1197     case NATIVE_ETHER_OPEN:
1198 gbeauche 1.38 gpr(3) = ether_open((queue_t *)gpr(3), (void *)gpr(4), gpr(5), gpr(6), (void*)gpr(7));
1199 gbeauche 1.1 break;
1200 gbeauche 1.16 case NATIVE_ETHER_CLOSE:
1201 gbeauche 1.38 gpr(3) = ether_close((queue_t *)gpr(3), gpr(4), (void *)gpr(5));
1202 gbeauche 1.1 break;
1203 gbeauche 1.16 case NATIVE_ETHER_WPUT:
1204 gbeauche 1.38 gpr(3) = ether_wput((queue_t *)gpr(3), (mblk_t *)gpr(4));
1205 gbeauche 1.1 break;
1206 gbeauche 1.16 case NATIVE_ETHER_RSRV:
1207 gbeauche 1.38 gpr(3) = ether_rsrv((queue_t *)gpr(3));
1208 gbeauche 1.1 break;
1209 gbeauche 1.34 #else
1210     case NATIVE_ETHER_INIT:
1211     // FIXME: needs more complicated thunks
1212 gbeauche 1.38 gpr(3) = false;
1213 gbeauche 1.34 break;
1214     #endif
1215 gbeauche 1.32 case NATIVE_SYNC_HOOK:
1216 gbeauche 1.38 gpr(3) = NQD_sync_hook(gpr(3));
1217 gbeauche 1.32 break;
1218     case NATIVE_BITBLT_HOOK:
1219 gbeauche 1.38 gpr(3) = NQD_bitblt_hook(gpr(3));
1220 gbeauche 1.32 break;
1221     case NATIVE_BITBLT:
1222 gbeauche 1.38 NQD_bitblt(gpr(3));
1223 gbeauche 1.32 break;
1224     case NATIVE_FILLRECT_HOOK:
1225 gbeauche 1.38 gpr(3) = NQD_fillrect_hook(gpr(3));
1226 gbeauche 1.32 break;
1227     case NATIVE_INVRECT:
1228 gbeauche 1.38 NQD_invrect(gpr(3));
1229 gbeauche 1.32 break;
1230 gbeauche 1.33 case NATIVE_FILLRECT:
1231 gbeauche 1.38 NQD_fillrect(gpr(3));
1232 gbeauche 1.32 break;
1233 gbeauche 1.1 case NATIVE_SERIAL_NOTHING:
1234     case NATIVE_SERIAL_OPEN:
1235     case NATIVE_SERIAL_PRIME_IN:
1236     case NATIVE_SERIAL_PRIME_OUT:
1237     case NATIVE_SERIAL_CONTROL:
1238     case NATIVE_SERIAL_STATUS:
1239     case NATIVE_SERIAL_CLOSE: {
1240     typedef int16 (*SerialCallback)(uint32, uint32);
1241     static const SerialCallback serial_callbacks[] = {
1242     SerialNothing,
1243     SerialOpen,
1244     SerialPrimeIn,
1245     SerialPrimeOut,
1246     SerialControl,
1247     SerialStatus,
1248     SerialClose
1249     };
1250 gbeauche 1.38 gpr(3) = serial_callbacks[selector - NATIVE_SERIAL_NOTHING](gpr(3), gpr(4));
1251 gbeauche 1.16 break;
1252     }
1253     case NATIVE_GET_RESOURCE:
1254     case NATIVE_GET_1_RESOURCE:
1255     case NATIVE_GET_IND_RESOURCE:
1256     case NATIVE_GET_1_IND_RESOURCE:
1257     case NATIVE_R_GET_RESOURCE: {
1258     typedef void (*GetResourceCallback)(void);
1259     static const GetResourceCallback get_resource_callbacks[] = {
1260 gbeauche 1.38 ::get_resource,
1261     ::get_1_resource,
1262     ::get_ind_resource,
1263     ::get_1_ind_resource,
1264     ::r_get_resource
1265 gbeauche 1.16 };
1266     get_resource_callbacks[selector - NATIVE_GET_RESOURCE]();
1267 gbeauche 1.1 break;
1268     }
1269 gbeauche 1.2 case NATIVE_DISABLE_INTERRUPT:
1270     DisableInterrupt();
1271     break;
1272     case NATIVE_ENABLE_INTERRUPT:
1273     EnableInterrupt();
1274 gbeauche 1.7 break;
1275     case NATIVE_MAKE_EXECUTABLE:
1276 gbeauche 1.38 MakeExecutable(0, (void *)gpr(4), gpr(5));
1277 gbeauche 1.26 break;
1278     case NATIVE_CHECK_LOAD_INVOC:
1279 gbeauche 1.38 check_load_invoc(gpr(3), gpr(4), gpr(5));
1280 gbeauche 1.2 break;
1281 gbeauche 1.1 default:
1282     printf("FATAL: NATIVE_OP called with bogus selector %d\n", selector);
1283     QuitEmulator();
1284     break;
1285     }
1286 gbeauche 1.15
1287     #if EMUL_TIME_STATS
1288     native_exec_time += (clock() - native_exec_start);
1289     #endif
1290 gbeauche 1.1 }
1291    
1292     /*
1293     * Execute 68k subroutine (must be ended with EXEC_RETURN)
1294     * This must only be called by the emul_thread when in EMUL_OP mode
1295     * r->a[7] is unused, the routine runs on the caller's stack
1296     */
1297    
1298     void Execute68k(uint32 pc, M68kRegisters *r)
1299     {
1300 gbeauche 1.41 ppc_cpu->execute_68k(pc, r);
1301 gbeauche 1.1 }
1302    
1303     /*
1304     * Execute 68k A-Trap from EMUL_OP routine
1305     * r->a[7] is unused, the routine runs on the caller's stack
1306     */
1307    
1308     void Execute68kTrap(uint16 trap, M68kRegisters *r)
1309     {
1310 gbeauche 1.21 SheepVar proc_var(4);
1311     uint32 proc = proc_var.addr();
1312     WriteMacInt16(proc, trap);
1313     WriteMacInt16(proc + 2, M68K_RTS);
1314     Execute68k(proc, r);
1315 gbeauche 1.1 }
1316    
1317     /*
1318     * Call MacOS PPC code
1319     */
1320    
1321     uint32 call_macos(uint32 tvect)
1322     {
1323 gbeauche 1.41 return ppc_cpu->execute_macos_code(tvect, 0, NULL);
1324 gbeauche 1.1 }
1325    
1326     uint32 call_macos1(uint32 tvect, uint32 arg1)
1327     {
1328     const uint32 args[] = { arg1 };
1329 gbeauche 1.41 return ppc_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
1330 gbeauche 1.1 }
1331    
1332     uint32 call_macos2(uint32 tvect, uint32 arg1, uint32 arg2)
1333     {
1334     const uint32 args[] = { arg1, arg2 };
1335 gbeauche 1.41 return ppc_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
1336 gbeauche 1.1 }
1337    
1338     uint32 call_macos3(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3)
1339     {
1340     const uint32 args[] = { arg1, arg2, arg3 };
1341 gbeauche 1.41 return ppc_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
1342 gbeauche 1.1 }
1343    
1344     uint32 call_macos4(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4)
1345     {
1346     const uint32 args[] = { arg1, arg2, arg3, arg4 };
1347 gbeauche 1.41 return ppc_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
1348 gbeauche 1.1 }
1349    
1350     uint32 call_macos5(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5)
1351     {
1352     const uint32 args[] = { arg1, arg2, arg3, arg4, arg5 };
1353 gbeauche 1.41 return ppc_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
1354 gbeauche 1.1 }
1355    
1356     uint32 call_macos6(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5, uint32 arg6)
1357     {
1358     const uint32 args[] = { arg1, arg2, arg3, arg4, arg5, arg6 };
1359 gbeauche 1.41 return ppc_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
1360 gbeauche 1.1 }
1361    
1362     uint32 call_macos7(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5, uint32 arg6, uint32 arg7)
1363     {
1364     const uint32 args[] = { arg1, arg2, arg3, arg4, arg5, arg6, arg7 };
1365 gbeauche 1.41 return ppc_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
1366 gbeauche 1.1 }
1367    
1368     /*
1369     * Resource Manager thunks
1370     */
1371    
1372     void get_resource(void)
1373     {
1374 gbeauche 1.41 ppc_cpu->get_resource(ReadMacInt32(XLM_GET_RESOURCE));
1375 gbeauche 1.1 }
1376    
1377     void get_1_resource(void)
1378     {
1379 gbeauche 1.41 ppc_cpu->get_resource(ReadMacInt32(XLM_GET_1_RESOURCE));
1380 gbeauche 1.1 }
1381    
1382     void get_ind_resource(void)
1383     {
1384 gbeauche 1.41 ppc_cpu->get_resource(ReadMacInt32(XLM_GET_IND_RESOURCE));
1385 gbeauche 1.1 }
1386    
1387     void get_1_ind_resource(void)
1388     {
1389 gbeauche 1.41 ppc_cpu->get_resource(ReadMacInt32(XLM_GET_1_IND_RESOURCE));
1390 gbeauche 1.1 }
1391    
1392     void r_get_resource(void)
1393     {
1394 gbeauche 1.41 ppc_cpu->get_resource(ReadMacInt32(XLM_R_GET_RESOURCE));
1395 gbeauche 1.1 }