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 |
#include "prefs.h" |
25 |
#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 |
#define DEBUG 0 |
47 |
#include "debug.h" |
48 |
|
49 |
// 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 |
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 |
// Enable multicore (main/interrupts) cpu emulation? |
74 |
#define MULTICORE_CPU (ASYNC_IRQ ? 1 : 0) |
75 |
|
76 |
// 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 |
static KernelData * const kernel_data = (KernelData *)KERNEL_DATA_BASE; |
90 |
|
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 |
// Constructor |
105 |
sheepshaver_cpu(); |
106 |
|
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 |
void execute(uint32 entry, bool enable_cache = false); |
113 |
|
114 |
// Execute 68k routine |
115 |
void execute_68k(uint32 entry, M68kRegisters *r); |
116 |
|
117 |
// Execute ppc routine |
118 |
void execute_ppc(uint32 entry); |
119 |
|
120 |
// 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 |
void interrupt(uint32 entry); |
128 |
void handle_interrupt(); |
129 |
|
130 |
// 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 |
sheepshaver_cpu::sheepshaver_cpu() |
143 |
: powerpc_cpu() |
144 |
{ |
145 |
init_decoder(); |
146 |
} |
147 |
|
148 |
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 |
(execute_pmf)&sheepshaver_cpu::execute_sheep, |
160 |
NULL, |
161 |
D_form, 6, 0, CFLOW_JUMP | CFLOW_TRAP |
162 |
} |
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 |
/* 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 |
// 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 |
|
199 |
case 1: // EXEC_RETURN |
200 |
spcflags().set(SPCFLAG_CPU_EXEC_RETURN); |
201 |
break; |
202 |
|
203 |
case 2: // EXEC_NATIVE |
204 |
NativeOp(NATIVE_OP_field::extract(opcode)); |
205 |
if (FN_field::test(opcode)) |
206 |
pc() = lr(); |
207 |
else |
208 |
pc() += 4; |
209 |
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 |
EmulOp(&r68, gpr(24), EMUL_OP_field::extract(opcode) - 3); |
221 |
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 |
void sheepshaver_cpu::execute(uint32 entry, bool enable_cache) |
235 |
{ |
236 |
powerpc_cpu::execute(entry, enable_cache); |
237 |
} |
238 |
|
239 |
// Handle MacOS interrupt |
240 |
void sheepshaver_cpu::interrupt(uint32 entry) |
241 |
{ |
242 |
#if EMUL_TIME_STATS |
243 |
interrupt_count++; |
244 |
const clock_t interrupt_start = clock(); |
245 |
#endif |
246 |
|
247 |
#if !MULTICORE_CPU |
248 |
// Save program counters and branch registers |
249 |
uint32 saved_pc = pc(); |
250 |
uint32 saved_lr = lr(); |
251 |
uint32 saved_ctr= ctr(); |
252 |
uint32 saved_sp = gpr(1); |
253 |
#endif |
254 |
|
255 |
// Initialize stack pointer to SheepShaver alternate stack base |
256 |
gpr(1) = SheepStack1Base - 64; |
257 |
|
258 |
// Build trampoline to return from interrupt |
259 |
uint32 trampoline[] = { htonl(POWERPC_EMUL_OP | 1) }; |
260 |
|
261 |
// Prepare registers for nanokernel interrupt routine |
262 |
kernel_data->v[0x004 >> 2] = htonl(gpr(1)); |
263 |
kernel_data->v[0x018 >> 2] = htonl(gpr(6)); |
264 |
|
265 |
gpr(6) = ntohl(kernel_data->v[0x65c >> 2]); |
266 |
assert(gpr(6) != 0); |
267 |
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 |
gpr(7) = ntohl(kernel_data->v[0x660 >> 2]); |
277 |
gpr(8) = 0; |
278 |
gpr(10) = (uint32)trampoline; |
279 |
gpr(12) = (uint32)trampoline; |
280 |
gpr(13) = get_cr(); |
281 |
|
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 |
cr().set((gpr(11) & 0x0fff0000) | (get_cr() & ~0x0fff0000)); |
289 |
|
290 |
// Enter nanokernel |
291 |
execute(entry); |
292 |
|
293 |
#if !MULTICORE_CPU |
294 |
// Restore program counters and branch registers |
295 |
pc() = saved_pc; |
296 |
lr() = saved_lr; |
297 |
ctr()= saved_ctr; |
298 |
gpr(1) = saved_sp; |
299 |
#endif |
300 |
|
301 |
#if EMUL_TIME_STATS |
302 |
interrupt_time += (clock() - interrupt_start); |
303 |
#endif |
304 |
} |
305 |
|
306 |
// Execute 68k routine |
307 |
void sheepshaver_cpu::execute_68k(uint32 entry, M68kRegisters *r) |
308 |
{ |
309 |
#if EMUL_TIME_STATS |
310 |
exec68k_count++; |
311 |
const clock_t exec68k_start = clock(); |
312 |
#endif |
313 |
|
314 |
#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 |
uint32 saved_cr = get_cr(); |
324 |
|
325 |
// Create MacOS stack frame |
326 |
// FIXME: make sure MacOS doesn't expect PPC registers to live on top |
327 |
uint32 sp = gpr(1); |
328 |
gpr(1) -= 56; |
329 |
WriteMacInt32(gpr(1), sp); |
330 |
|
331 |
// Save PowerPC registers |
332 |
uint32 saved_GPRs[19]; |
333 |
memcpy(&saved_GPRs[0], &gpr(13), sizeof(uint32)*(32-13)); |
334 |
#if SAVE_FP_EXEC_68K |
335 |
double saved_FPRs[18]; |
336 |
memcpy(&saved_FPRs[0], &fpr(14), sizeof(double)*(32-14)); |
337 |
#endif |
338 |
|
339 |
// Setup registers for 68k emulator |
340 |
cr().set(CR_SO_field<2>::mask()); // Supervisor mode |
341 |
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 |
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 |
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 |
memcpy(&gpr(13), &saved_GPRs[0], sizeof(uint32)*(32-13)); |
384 |
#if SAVE_FP_EXEC_68K |
385 |
memcpy(&fpr(14), &saved_FPRs[0], sizeof(double)*(32-14)); |
386 |
#endif |
387 |
|
388 |
// Cleanup stack |
389 |
gpr(1) += 56; |
390 |
|
391 |
// Restore program counters and branch registers |
392 |
pc() = saved_pc; |
393 |
lr() = saved_lr; |
394 |
ctr()= saved_ctr; |
395 |
set_cr(saved_cr); |
396 |
|
397 |
#if EMUL_TIME_STATS |
398 |
exec68k_time += (clock() - exec68k_start); |
399 |
#endif |
400 |
} |
401 |
|
402 |
// Call MacOS PPC code |
403 |
uint32 sheepshaver_cpu::execute_macos_code(uint32 tvect, int nargs, uint32 const *args) |
404 |
{ |
405 |
#if EMUL_TIME_STATS |
406 |
macos_exec_count++; |
407 |
const clock_t macos_exec_start = clock(); |
408 |
#endif |
409 |
|
410 |
// 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 |
uint32 trampoline[] = { htonl(POWERPC_EMUL_OP | 1) }; |
417 |
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 |
#if EMUL_TIME_STATS |
449 |
macos_exec_time += (clock() - macos_exec_start); |
450 |
#endif |
451 |
|
452 |
return retval; |
453 |
} |
454 |
|
455 |
// Execute ppc routine |
456 |
inline void sheepshaver_cpu::execute_ppc(uint32 entry) |
457 |
{ |
458 |
// Save branch registers |
459 |
uint32 saved_lr = lr(); |
460 |
|
461 |
const uint32 trampoline[] = { htonl(POWERPC_EMUL_OP | 1) }; |
462 |
lr() = (uint32)trampoline; |
463 |
|
464 |
execute(entry); |
465 |
|
466 |
// Restore branch registers |
467 |
lr() = saved_lr; |
468 |
} |
469 |
|
470 |
// Resource Manager thunk |
471 |
extern "C" void check_load_invoc(uint32 type, int16 id, uint32 h); |
472 |
|
473 |
inline void sheepshaver_cpu::get_resource(uint32 old_get_resource) |
474 |
{ |
475 |
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 |
uint32 handle = gpr(3); |
486 |
check_load_invoc(type, id, handle); |
487 |
gpr(3) = handle; |
488 |
|
489 |
// Cleanup stack |
490 |
gpr(1) += 56; |
491 |
} |
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 |
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 |
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 |
// 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 |
static sigsegv_return_t sigsegv_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction) |
542 |
{ |
543 |
#if ENABLE_VOSF |
544 |
// 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 |
#endif |
549 |
|
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 |
#endif |
562 |
|
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 |
dump_registers(); |
568 |
current_cpu->dump_log(); |
569 |
enter_mon(); |
570 |
QuitEmulator(); |
571 |
|
572 |
return SIGSEGV_RETURN_FAILURE; |
573 |
} |
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 |
#if MULTICORE_CPU |
583 |
// Initialize alternate CPU emulator to handle interrupts |
584 |
interrupt_cpu = new sheepshaver_cpu(); |
585 |
#endif |
586 |
|
587 |
// Install the handler for SIGSEGV |
588 |
sigsegv_install_handler(sigsegv_handler); |
589 |
|
590 |
#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 |
|
596 |
#if EMUL_TIME_STATS |
597 |
emul_start_time = clock(); |
598 |
#endif |
599 |
} |
600 |
|
601 |
/* |
602 |
* Deinitialize emulation |
603 |
*/ |
604 |
|
605 |
void exit_emul_ppc(void) |
606 |
{ |
607 |
#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 |
delete main_cpu; |
632 |
#if MULTICORE_CPU |
633 |
delete interrupt_cpu; |
634 |
#endif |
635 |
} |
636 |
|
637 |
/* |
638 |
* Emulation loop |
639 |
*/ |
640 |
|
641 |
void emul_ppc(uint32 entry) |
642 |
{ |
643 |
current_cpu = main_cpu; |
644 |
#if DEBUG |
645 |
current_cpu->start_log(); |
646 |
#endif |
647 |
// start emulation loop and enable code translation or caching |
648 |
current_cpu->execute(entry, true); |
649 |
} |
650 |
|
651 |
/* |
652 |
* Handle PowerPC interrupt |
653 |
*/ |
654 |
|
655 |
#if ASYNC_IRQ |
656 |
void HandleInterrupt(void) |
657 |
{ |
658 |
main_cpu->handle_interrupt(); |
659 |
} |
660 |
#else |
661 |
void TriggerInterrupt(void) |
662 |
{ |
663 |
#if 0 |
664 |
WriteMacInt32(0x16a, ReadMacInt32(0x16a) + 1); |
665 |
#else |
666 |
// Trigger interrupt to main cpu only |
667 |
if (main_cpu) |
668 |
main_cpu->trigger_interrupt(); |
669 |
#endif |
670 |
} |
671 |
#endif |
672 |
|
673 |
void sheepshaver_cpu::handle_interrupt(void) |
674 |
{ |
675 |
// Do nothing if interrupts are disabled |
676 |
if (int32(ReadMacInt32(XLM_IRQ_NEST)) > 0) |
677 |
return; |
678 |
|
679 |
// Do nothing if there is no interrupt pending |
680 |
if (InterruptFlags == 0) |
681 |
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 |
set_cr(get_cr() | tswap32(kernel_data->v[0x674 >> 2])); |
693 |
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 |
if (gpr(1) != KernelDataAddr) { |
700 |
// 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 |
DisableInterrupt(); |
708 |
cpu_push(interrupt_cpu); |
709 |
if (ROMType == ROMTYPE_NEWWORLD) |
710 |
current_cpu->interrupt(ROM_BASE + 0x312b1c); |
711 |
else |
712 |
current_cpu->interrupt(ROM_BASE + 0x312a3c); |
713 |
cpu_pop(); |
714 |
} |
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 |
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 |
}; |
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 |
#define POWERPC_NATIVE_OP_INIT(LR, OP) \ |
758 |
tswap32(POWERPC_EMUL_OP | ((LR) << 11) | (((uint32)OP) << 6) | 2) |
759 |
|
760 |
// FIXME: Make sure 32-bit relocations are used |
761 |
const uint32 NativeOpTable[NATIVE_OP_MAX] = { |
762 |
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 |
POWERPC_NATIVE_OP_INIT(1, NATIVE_MAKE_EXECUTABLE), |
788 |
}; |
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 |
#if EMUL_TIME_STATS |
801 |
native_exec_count++; |
802 |
const clock_t native_exec_start = clock(); |
803 |
#endif |
804 |
|
805 |
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 |
case NATIVE_DISABLE_INTERRUPT: |
855 |
DisableInterrupt(); |
856 |
break; |
857 |
case NATIVE_ENABLE_INTERRUPT: |
858 |
EnableInterrupt(); |
859 |
break; |
860 |
case NATIVE_MAKE_EXECUTABLE: |
861 |
MakeExecutable(0, (void *)GPR(4), GPR(5)); |
862 |
break; |
863 |
default: |
864 |
printf("FATAL: NATIVE_OP called with bogus selector %d\n", selector); |
865 |
QuitEmulator(); |
866 |
break; |
867 |
} |
868 |
|
869 |
#if EMUL_TIME_STATS |
870 |
native_exec_time += (clock() - native_exec_start); |
871 |
#endif |
872 |
} |
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 |
uint16 proc[2]; |
907 |
proc[0] = htons(trap); |
908 |
proc[1] = htons(M68K_RTS); |
909 |
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 |
} |