ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/kpx_cpu/sheepshaver_glue.cpp
Revision: 1.20
Committed: 2003-12-03T10:52:49Z (20 years, 9 months ago) by gbeauche
Branch: MAIN
Changes since 1.19: +7 -1 lines
Log Message:
Add "jit" prefs item. Fix PPC_DECODE_CACHE version to fill in new min_pc &
max_pc members of block info. Increase -finline-limit to 10000 for older gcc

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