ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/kpx_cpu/sheepshaver_glue.cpp
Revision: 1.15
Committed: 2003-11-04T20:48:29Z (20 years, 7 months ago) by gbeauche
Branch: MAIN
Changes since 1.14: +79 -0 lines
Log Message:
Add some statistics for interrupt handling, Execute68k/Trap, MacOS & NativeOp

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