ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/kpx_cpu/sheepshaver_glue.cpp
Revision: 1.48
Committed: 2004-06-26T15:26:18Z (20 years, 5 months ago) by gbeauche
Branch: MAIN
Changes since 1.47: +2 -5 lines
Log Message:
Performance of VOSF is heuristically determined at run-time, so have to
initialize SIGSEGV handlers early, as in Basilisk II. Besides, also add
missing call to vm_init() in case host system doesn't have MAP_ANON.

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