ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/kpx_cpu/sheepshaver_glue.cpp
Revision: 1.25
Committed: 2004-01-12T15:37:24Z (20 years, 4 months ago) by cebix
Branch: MAIN
Changes since 1.24: +1 -1 lines
Log Message:
Happy New Year! :)

File Contents

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