ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/kpx_cpu/sheepshaver_glue.cpp
Revision: 1.21
Committed: 2003-12-04T17:26:38Z (20 years, 11 months ago) by gbeauche
Branch: MAIN
Changes since 1.20: +21 -54 lines
Log Message:
Add new thunking system for 64-bit fixes.

File Contents

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