ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/kpx_cpu/sheepshaver_glue.cpp
Revision: 1.41
Committed: 2004-05-20T12:33:58Z (20 years, 1 month ago) by gbeauche
Branch: MAIN
Changes since 1.40: +31 -77 lines
Log Message:
Get rid of old (and broken) ASYNC_IRQ / MUTICORE code

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