ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/kpx_cpu/sheepshaver_glue.cpp
Revision: 1.16
Committed: 2003-11-10T15:11:44Z (20 years, 10 months ago) by gbeauche
Branch: MAIN
Changes since 1.15: +41 -11 lines
Log Message:
- XLM_IRQ_NEST is always in native byte order format since any write to
  this variable go through {Enable,Disable}Interrupt().
- Add Ether thunks but only for WORDS_BIGENDIAN case since we do need more
complicated translation functions.

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