ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/kpx_cpu/sheepshaver_glue.cpp
Revision: 1.19
Committed: 2003-11-30T17:21:52Z (20 years, 9 months ago) by gbeauche
Branch: MAIN
Changes since 1.18: +1 -10 lines
Log Message:
better handling of static translation cache allocation, handle nested
execution paths from the cpu core, cleanups for KPX_MAX_CPUS == 1.

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