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