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 "spcflags.h" |
32 |
|
#include "cpu/ppc/ppc-cpu.hpp" |
33 |
|
#include "cpu/ppc/ppc-operations.hpp" |
34 |
|
|
44 |
|
#include "mon_disass.h" |
45 |
|
#endif |
46 |
|
|
47 |
< |
#define DEBUG 1 |
47 |
> |
#define DEBUG 0 |
48 |
|
#include "debug.h" |
49 |
|
|
50 |
|
static void enter_mon(void) |
56 |
|
#endif |
57 |
|
} |
58 |
|
|
59 |
+ |
// Enable multicore (main/interrupts) cpu emulation? |
60 |
+ |
#define MULTICORE_CPU (ASYNC_IRQ ? 1 : 0) |
61 |
+ |
|
62 |
|
// Enable Execute68k() safety checks? |
63 |
|
#define SAFE_EXEC_68K 1 |
64 |
|
|
71 |
|
// Interrupts in native mode? |
72 |
|
#define INTERRUPTS_IN_NATIVE_MODE 1 |
73 |
|
|
69 |
– |
// 68k Emulator Data |
70 |
– |
struct EmulatorData { |
71 |
– |
uint32 v[0x400]; |
72 |
– |
}; |
73 |
– |
|
74 |
– |
// Kernel Data |
75 |
– |
struct KernelData { |
76 |
– |
uint32 v[0x400]; |
77 |
– |
EmulatorData ed; |
78 |
– |
}; |
79 |
– |
|
74 |
|
// Pointer to Kernel Data |
75 |
< |
static KernelData * const kernel_data = (KernelData *)0x68ffe000; |
75 |
> |
static KernelData * const kernel_data = (KernelData *)KERNEL_DATA_BASE; |
76 |
|
|
77 |
|
|
78 |
|
/** |
89 |
|
|
90 |
|
public: |
91 |
|
|
92 |
< |
sheepshaver_cpu() |
93 |
< |
: powerpc_cpu() |
100 |
< |
{ init_decoder(); } |
101 |
< |
|
102 |
< |
// Stack pointer accessors |
103 |
< |
uint32 get_sp() const { return gpr(1); } |
104 |
< |
void set_sp(uint32 v) { gpr(1) = v; } |
92 |
> |
// Constructor |
93 |
> |
sheepshaver_cpu(); |
94 |
|
|
95 |
|
// Condition Register accessors |
96 |
|
uint32 get_cr() const { return cr().get(); } |
97 |
|
void set_cr(uint32 v) { cr().set(v); } |
98 |
|
|
99 |
|
// Execution loop |
100 |
< |
void execute(uint32 pc); |
100 |
> |
void execute(uint32 entry, bool enable_cache = false); |
101 |
|
|
102 |
|
// Execute 68k routine |
103 |
|
void execute_68k(uint32 entry, M68kRegisters *r); |
104 |
|
|
105 |
+ |
// Execute ppc routine |
106 |
+ |
void execute_ppc(uint32 entry); |
107 |
+ |
|
108 |
|
// Execute MacOS/PPC code |
109 |
|
uint32 execute_macos_code(uint32 tvect, int nargs, uint32 const *args); |
110 |
|
|
112 |
|
void get_resource(uint32 old_get_resource); |
113 |
|
|
114 |
|
// Handle MacOS interrupt |
115 |
< |
void interrupt(uint32 entry, uint32 sp); |
115 |
> |
void interrupt(uint32 entry); |
116 |
> |
void handle_interrupt(); |
117 |
> |
|
118 |
> |
// spcflags for interrupts handling |
119 |
> |
static uint32 spcflags; |
120 |
|
|
121 |
|
// Lazy memory allocator (one item at a time) |
122 |
|
void *operator new(size_t size) |
128 |
|
void operator delete[](void *p); |
129 |
|
}; |
130 |
|
|
131 |
+ |
uint32 sheepshaver_cpu::spcflags = 0; |
132 |
|
lazy_allocator< sheepshaver_cpu > allocator_helper< sheepshaver_cpu, lazy_allocator >::allocator; |
133 |
|
|
134 |
+ |
sheepshaver_cpu::sheepshaver_cpu() |
135 |
+ |
: powerpc_cpu() |
136 |
+ |
{ |
137 |
+ |
init_decoder(); |
138 |
+ |
} |
139 |
+ |
|
140 |
|
void sheepshaver_cpu::init_decoder() |
141 |
|
{ |
142 |
|
#ifndef PPC_NO_STATIC_II_INDEX_TABLE |
150 |
|
{ "sheep", |
151 |
|
(execute_fn)&sheepshaver_cpu::execute_sheep, |
152 |
|
NULL, |
153 |
< |
D_form, 6, 0, CFLOW_TRAP |
153 |
> |
D_form, 6, 0, CFLOW_JUMP | CFLOW_TRAP |
154 |
|
} |
155 |
|
}; |
156 |
|
|
166 |
|
// Forward declaration for native opcode handler |
167 |
|
static void NativeOp(int selector); |
168 |
|
|
169 |
+ |
/* NativeOp instruction format: |
170 |
+ |
+------------+--------------------------+--+----------+------------+ |
171 |
+ |
| 6 | |FN| OP | 2 | |
172 |
+ |
+------------+--------------------------+--+----------+------------+ |
173 |
+ |
0 5 |6 19 20 21 25 26 31 |
174 |
+ |
*/ |
175 |
+ |
|
176 |
+ |
typedef bit_field< 20, 20 > FN_field; |
177 |
+ |
typedef bit_field< 21, 25 > NATIVE_OP_field; |
178 |
+ |
typedef bit_field< 26, 31 > EMUL_OP_field; |
179 |
+ |
|
180 |
|
// Execute SheepShaver instruction |
181 |
|
void sheepshaver_cpu::execute_sheep(uint32 opcode) |
182 |
|
{ |
187 |
|
case 0: // EMUL_RETURN |
188 |
|
QuitEmulator(); |
189 |
|
break; |
190 |
< |
|
190 |
> |
|
191 |
|
case 1: // EXEC_RETURN |
192 |
|
throw sheepshaver_exec_return(); |
193 |
|
break; |
194 |
|
|
195 |
|
case 2: // EXEC_NATIVE |
196 |
< |
NativeOp((opcode >> 6) & 0x1f); |
197 |
< |
pc() = lr(); |
196 |
> |
NativeOp(NATIVE_OP_field::extract(opcode)); |
197 |
> |
if (FN_field::test(opcode)) |
198 |
> |
pc() = lr(); |
199 |
> |
else |
200 |
> |
pc() += 4; |
201 |
|
break; |
202 |
|
|
203 |
|
default: { // EMUL_OP |
209 |
|
for (int i = 0; i < 7; i++) |
210 |
|
r68.a[i] = gpr(16 + i); |
211 |
|
r68.a[7] = gpr(1); |
212 |
< |
EmulOp(&r68, gpr(24), (opcode & 0x3f) - 3); |
212 |
> |
EmulOp(&r68, gpr(24), EMUL_OP_field::extract(opcode) - 3); |
213 |
|
for (int i = 0; i < 8; i++) |
214 |
|
gpr(8 + i) = r68.d[i]; |
215 |
|
for (int i = 0; i < 7; i++) |
223 |
|
} |
224 |
|
|
225 |
|
// Execution loop |
226 |
< |
void sheepshaver_cpu::execute(uint32 entry) |
226 |
> |
void sheepshaver_cpu::execute(uint32 entry, bool enable_cache) |
227 |
|
{ |
228 |
|
try { |
229 |
< |
pc() = entry; |
213 |
< |
powerpc_cpu::execute(); |
229 |
> |
powerpc_cpu::execute(entry, enable_cache); |
230 |
|
} |
231 |
|
catch (sheepshaver_exec_return const &) { |
232 |
|
// Nothing, simply return |
238 |
|
} |
239 |
|
|
240 |
|
// Handle MacOS interrupt |
241 |
< |
void sheepshaver_cpu::interrupt(uint32 entry, uint32 sp) |
241 |
> |
void sheepshaver_cpu::interrupt(uint32 entry) |
242 |
|
{ |
243 |
< |
// Create stack frame |
244 |
< |
gpr(1) = sp - 64; |
243 |
> |
#if !MULTICORE_CPU |
244 |
> |
// Save program counters and branch registers |
245 |
> |
uint32 saved_pc = pc(); |
246 |
> |
uint32 saved_lr = lr(); |
247 |
> |
uint32 saved_ctr= ctr(); |
248 |
> |
uint32 saved_sp = gpr(1); |
249 |
> |
#endif |
250 |
> |
|
251 |
> |
// Initialize stack pointer to SheepShaver alternate stack base |
252 |
> |
gpr(1) = SheepStack1Base - 64; |
253 |
|
|
254 |
|
// Build trampoline to return from interrupt |
255 |
< |
uint32 trampoline[] = { POWERPC_EMUL_OP | 1 }; |
255 |
> |
uint32 trampoline[] = { htonl(POWERPC_EMUL_OP | 1) }; |
256 |
|
|
257 |
|
// Prepare registers for nanokernel interrupt routine |
258 |
< |
kernel_data->v[0x004 >> 2] = gpr(1); |
259 |
< |
kernel_data->v[0x018 >> 2] = gpr(6); |
258 |
> |
kernel_data->v[0x004 >> 2] = htonl(gpr(1)); |
259 |
> |
kernel_data->v[0x018 >> 2] = htonl(gpr(6)); |
260 |
|
|
261 |
< |
gpr(6) = kernel_data->v[0x65c >> 2]; |
261 |
> |
gpr(6) = ntohl(kernel_data->v[0x65c >> 2]); |
262 |
> |
assert(gpr(6) != 0); |
263 |
|
WriteMacInt32(gpr(6) + 0x13c, gpr(7)); |
264 |
|
WriteMacInt32(gpr(6) + 0x144, gpr(8)); |
265 |
|
WriteMacInt32(gpr(6) + 0x14c, gpr(9)); |
269 |
|
WriteMacInt32(gpr(6) + 0x16c, gpr(13)); |
270 |
|
|
271 |
|
gpr(1) = KernelDataAddr; |
272 |
< |
gpr(7) = kernel_data->v[0x660 >> 2]; |
272 |
> |
gpr(7) = ntohl(kernel_data->v[0x660 >> 2]); |
273 |
|
gpr(8) = 0; |
274 |
|
gpr(10) = (uint32)trampoline; |
275 |
|
gpr(12) = (uint32)trampoline; |
276 |
< |
gpr(13) = cr().get(); |
276 |
> |
gpr(13) = get_cr(); |
277 |
|
|
278 |
|
// rlwimi. r7,r7,8,0,0 |
279 |
|
uint32 result = op_ppc_rlwimi::apply(gpr(7), 8, 0x80000000, gpr(7)); |
281 |
|
gpr(7) = result; |
282 |
|
|
283 |
|
gpr(11) = 0xf072; // MSR (SRR1) |
284 |
< |
cr().set((gpr(11) & 0x0fff0000) | (cr().get() & ~0x0fff0000)); |
284 |
> |
cr().set((gpr(11) & 0x0fff0000) | (get_cr() & ~0x0fff0000)); |
285 |
|
|
286 |
|
// Enter nanokernel |
287 |
|
execute(entry); |
288 |
|
|
289 |
< |
// Cleanup stack |
290 |
< |
gpr(1) += 64; |
289 |
> |
#if !MULTICORE_CPU |
290 |
> |
// Restore program counters and branch registers |
291 |
> |
pc() = saved_pc; |
292 |
> |
lr() = saved_lr; |
293 |
> |
ctr()= saved_ctr; |
294 |
> |
gpr(1) = saved_sp; |
295 |
> |
#endif |
296 |
|
} |
297 |
|
|
298 |
|
// Execute 68k routine |
307 |
|
uint32 saved_pc = pc(); |
308 |
|
uint32 saved_lr = lr(); |
309 |
|
uint32 saved_ctr= ctr(); |
310 |
+ |
uint32 saved_cr = get_cr(); |
311 |
|
|
312 |
|
// Create MacOS stack frame |
313 |
+ |
// FIXME: make sure MacOS doesn't expect PPC registers to live on top |
314 |
|
uint32 sp = gpr(1); |
315 |
< |
gpr(1) -= 56 + 19*4 + 18*8; |
315 |
> |
gpr(1) -= 56; |
316 |
|
WriteMacInt32(gpr(1), sp); |
317 |
|
|
318 |
|
// Save PowerPC registers |
319 |
< |
memcpy(Mac2HostAddr(gpr(1)+56), &gpr(13), sizeof(uint32)*(32-13)); |
319 |
> |
uint32 saved_GPRs[19]; |
320 |
> |
memcpy(&saved_GPRs[0], &gpr(13), sizeof(uint32)*(32-13)); |
321 |
|
#if SAVE_FP_EXEC_68K |
322 |
< |
memcpy(Mac2HostAddr(gpr(1)+56+19*4), &fpr(14), sizeof(double)*(32-14)); |
322 |
> |
double saved_FPRs[18]; |
323 |
> |
memcpy(&saved_FPRs[0], &fpr(14), sizeof(double)*(32-14)); |
324 |
|
#endif |
325 |
|
|
326 |
|
// Setup registers for 68k emulator |
327 |
< |
cr().set(0); |
294 |
< |
cr().set(2, 1); // Supervisor mode |
327 |
> |
cr().set(CR_SO_field<2>::mask()); // Supervisor mode |
328 |
|
for (int i = 0; i < 8; i++) // d[0]..d[7] |
329 |
|
gpr(8 + i) = r->d[i]; |
330 |
|
for (int i = 0; i < 7; i++) // a[0]..a[6] |
334 |
|
gpr(25) = ReadMacInt32(XLM_68K_R25); // MSB of SR |
335 |
|
gpr(26) = 0; |
336 |
|
gpr(28) = 0; // VBR |
337 |
< |
gpr(29) = kernel_data->ed.v[0x74 >> 2]; // Pointer to opcode table |
338 |
< |
gpr(30) = kernel_data->ed.v[0x78 >> 2]; // Address of emulator |
337 |
> |
gpr(29) = ntohl(kernel_data->ed.v[0x74 >> 2]); // Pointer to opcode table |
338 |
> |
gpr(30) = ntohl(kernel_data->ed.v[0x78 >> 2]); // Address of emulator |
339 |
|
gpr(31) = KernelDataAddr + 0x1000; |
340 |
|
|
341 |
|
// Push return address (points to EXEC_RETURN opcode) on stack |
367 |
|
r->a[i] = gpr(16 + i); |
368 |
|
|
369 |
|
// Restore PowerPC registers |
370 |
< |
memcpy(&gpr(13), Mac2HostAddr(gpr(1)+56), sizeof(uint32)*(32-13)); |
370 |
> |
memcpy(&gpr(13), &saved_GPRs[0], sizeof(uint32)*(32-13)); |
371 |
|
#if SAVE_FP_EXEC_68K |
372 |
< |
memcpy(&fpr(14), Mac2HostAddr(gpr(1)+56+19*4), sizeof(double)*(32-14)); |
372 |
> |
memcpy(&fpr(14), &saved_FPRs[0], sizeof(double)*(32-14)); |
373 |
|
#endif |
374 |
|
|
375 |
|
// Cleanup stack |
376 |
< |
gpr(1) += 56 + 19*4 + 18*8; |
376 |
> |
gpr(1) += 56; |
377 |
|
|
378 |
|
// Restore program counters and branch registers |
379 |
|
pc() = saved_pc; |
380 |
|
lr() = saved_lr; |
381 |
|
ctr()= saved_ctr; |
382 |
+ |
set_cr(saved_cr); |
383 |
|
} |
384 |
|
|
385 |
|
// Call MacOS PPC code |
391 |
|
uint32 saved_ctr= ctr(); |
392 |
|
|
393 |
|
// Build trampoline with EXEC_RETURN |
394 |
< |
uint32 trampoline[] = { POWERPC_EMUL_OP | 1 }; |
394 |
> |
uint32 trampoline[] = { htonl(POWERPC_EMUL_OP | 1) }; |
395 |
|
lr() = (uint32)trampoline; |
396 |
|
|
397 |
|
gpr(1) -= 64; // Create stack frame |
426 |
|
return retval; |
427 |
|
} |
428 |
|
|
429 |
+ |
// Execute ppc routine |
430 |
+ |
inline void sheepshaver_cpu::execute_ppc(uint32 entry) |
431 |
+ |
{ |
432 |
+ |
// Save branch registers |
433 |
+ |
uint32 saved_lr = lr(); |
434 |
+ |
|
435 |
+ |
const uint32 trampoline[] = { htonl(POWERPC_EMUL_OP | 1) }; |
436 |
+ |
lr() = (uint32)trampoline; |
437 |
+ |
|
438 |
+ |
execute(entry); |
439 |
+ |
|
440 |
+ |
// Restore branch registers |
441 |
+ |
lr() = saved_lr; |
442 |
+ |
} |
443 |
+ |
|
444 |
|
// Resource Manager thunk |
445 |
+ |
extern "C" void check_load_invoc(uint32 type, int16 id, uint32 h); |
446 |
+ |
|
447 |
|
inline void sheepshaver_cpu::get_resource(uint32 old_get_resource) |
448 |
|
{ |
449 |
< |
printf("ERROR: get_resource() unimplemented\n"); |
450 |
< |
QuitEmulator(); |
449 |
> |
uint32 type = gpr(3); |
450 |
> |
int16 id = gpr(4); |
451 |
> |
|
452 |
> |
// Create stack frame |
453 |
> |
gpr(1) -= 56; |
454 |
> |
|
455 |
> |
// Call old routine |
456 |
> |
execute_ppc(old_get_resource); |
457 |
> |
|
458 |
> |
// Call CheckLoad() |
459 |
> |
uint32 handle = gpr(3); |
460 |
> |
check_load_invoc(type, id, handle); |
461 |
> |
gpr(3) = handle; |
462 |
> |
|
463 |
> |
// Cleanup stack |
464 |
> |
gpr(1) += 56; |
465 |
|
} |
466 |
|
|
467 |
|
|
473 |
|
static sheepshaver_cpu *interrupt_cpu = NULL; // CPU emulator to handle interrupts |
474 |
|
static sheepshaver_cpu *current_cpu = NULL; // Current CPU emulator context |
475 |
|
|
476 |
+ |
void FlushCodeCache(uintptr start, uintptr end) |
477 |
+ |
{ |
478 |
+ |
D(bug("FlushCodeCache(%08x, %08x)\n", start, end)); |
479 |
+ |
main_cpu->invalidate_cache_range(start, end); |
480 |
+ |
#if MULTICORE_CPU |
481 |
+ |
interrupt_cpu->invalidate_cache_range(start, end); |
482 |
+ |
#endif |
483 |
+ |
} |
484 |
+ |
|
485 |
+ |
static inline void cpu_push(sheepshaver_cpu *new_cpu) |
486 |
+ |
{ |
487 |
+ |
#if MULTICORE_CPU |
488 |
+ |
current_cpu = new_cpu; |
489 |
+ |
#endif |
490 |
+ |
} |
491 |
+ |
|
492 |
+ |
static inline void cpu_pop() |
493 |
+ |
{ |
494 |
+ |
#if MULTICORE_CPU |
495 |
+ |
current_cpu = main_cpu; |
496 |
+ |
#endif |
497 |
+ |
} |
498 |
+ |
|
499 |
|
// Dump PPC registers |
500 |
|
static void dump_registers(void) |
501 |
|
{ |
512 |
|
* Initialize CPU emulation |
513 |
|
*/ |
514 |
|
|
515 |
< |
static struct sigaction sigsegv_action; |
428 |
< |
|
429 |
< |
#if defined(__powerpc__) |
430 |
< |
#include <sys/ucontext.h> |
431 |
< |
#endif |
432 |
< |
|
433 |
< |
static void sigsegv_handler(int sig, siginfo_t *sip, void *scp) |
515 |
> |
static sigsegv_return_t sigsegv_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction) |
516 |
|
{ |
435 |
– |
const uintptr addr = (uintptr)sip->si_addr; |
517 |
|
#if ENABLE_VOSF |
518 |
< |
// Handle screen fault. |
519 |
< |
extern bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction); |
520 |
< |
if (Screen_fault_handler((sigsegv_address_t)addr, SIGSEGV_INVALID_PC)) |
521 |
< |
return; |
441 |
< |
#endif |
442 |
< |
#if defined(__powerpc__) |
443 |
< |
if (addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) { |
444 |
< |
printf("IGNORE write access to ROM at %08x\n", addr); |
445 |
< |
(((ucontext_t *)scp)->uc_mcontext.regs)->nip += 4; |
446 |
< |
return; |
447 |
< |
} |
448 |
< |
if (addr >= 0xf3012000 && addr < 0xf3014000 && 0) { |
449 |
< |
printf("IGNORE write access to ROM at %08x\n", addr); |
450 |
< |
(((ucontext_t *)scp)->uc_mcontext.regs)->nip += 4; |
451 |
< |
return; |
452 |
< |
} |
518 |
> |
// Handle screen fault |
519 |
> |
extern bool Screen_fault_handler(sigsegv_address_t, sigsegv_address_t); |
520 |
> |
if (Screen_fault_handler(fault_address, fault_instruction)) |
521 |
> |
return SIGSEGV_RETURN_SUCCESS; |
522 |
|
#endif |
523 |
< |
printf("Caught SIGSEGV at address %p\n", sip->si_addr); |
524 |
< |
printf("Native PC: %08x\n", (((ucontext_t *)scp)->uc_mcontext.regs)->nip); |
525 |
< |
printf("Current CPU: %s\n", current_cpu == main_cpu ? "main" : "interrupts"); |
526 |
< |
#if 1 |
527 |
< |
dump_registers(); |
523 |
> |
|
524 |
> |
const uintptr addr = (uintptr)fault_address; |
525 |
> |
#if HAVE_SIGSEGV_SKIP_INSTRUCTION |
526 |
> |
// Ignore writes to ROM |
527 |
> |
if ((addr - ROM_BASE) < ROM_SIZE) |
528 |
> |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
529 |
> |
|
530 |
> |
// Ignore all other faults, if requested |
531 |
> |
if (PrefsFindBool("ignoresegv")) |
532 |
> |
return SIGSEGV_RETURN_FAILURE; |
533 |
|
#else |
534 |
< |
printf("Main CPU context\n"); |
461 |
< |
main_cpu->dump_registers(); |
462 |
< |
printf("Interrupts CPU context\n"); |
463 |
< |
interrupt_cpu->dump_registers(); |
534 |
> |
#error "FIXME: You don't have the capability to skip instruction within signal handlers" |
535 |
|
#endif |
536 |
+ |
|
537 |
+ |
printf("SIGSEGV\n"); |
538 |
+ |
printf(" pc %p\n", fault_instruction); |
539 |
+ |
printf(" ea %p\n", fault_address); |
540 |
+ |
printf(" cpu %s\n", current_cpu == main_cpu ? "main" : "interrupts"); |
541 |
+ |
dump_registers(); |
542 |
|
current_cpu->dump_log(); |
466 |
– |
WriteMacInt32(XLM_IRQ_NEST, 1); |
543 |
|
enter_mon(); |
544 |
|
QuitEmulator(); |
545 |
+ |
|
546 |
+ |
return SIGSEGV_RETURN_FAILURE; |
547 |
|
} |
548 |
|
|
549 |
|
void init_emul_ppc(void) |
553 |
|
main_cpu->set_register(powerpc_registers::GPR(3), any_register((uint32)ROM_BASE + 0x30d000)); |
554 |
|
WriteMacInt32(XLM_RUN_MODE, MODE_68K); |
555 |
|
|
556 |
+ |
#if MULTICORE_CPU |
557 |
|
// Initialize alternate CPU emulator to handle interrupts |
558 |
|
interrupt_cpu = new sheepshaver_cpu(); |
559 |
+ |
#endif |
560 |
|
|
561 |
< |
// Install SIGSEGV handler |
562 |
< |
sigemptyset(&sigsegv_action.sa_mask); |
483 |
< |
sigsegv_action.sa_sigaction = sigsegv_handler; |
484 |
< |
sigsegv_action.sa_flags = SA_SIGINFO; |
485 |
< |
sigsegv_action.sa_restorer = NULL; |
486 |
< |
sigaction(SIGSEGV, &sigsegv_action, NULL); |
561 |
> |
// Install the handler for SIGSEGV |
562 |
> |
sigsegv_install_handler(sigsegv_handler); |
563 |
|
|
564 |
|
#if ENABLE_MON |
565 |
|
// Install "regs" command in cxmon |
575 |
|
void emul_ppc(uint32 entry) |
576 |
|
{ |
577 |
|
current_cpu = main_cpu; |
578 |
+ |
#if DEBUG |
579 |
|
current_cpu->start_log(); |
580 |
< |
current_cpu->execute(entry); |
580 |
> |
#endif |
581 |
> |
// start emulation loop and enable code translation or caching |
582 |
> |
current_cpu->execute(entry, true); |
583 |
|
} |
584 |
|
|
585 |
|
/* |
586 |
|
* Handle PowerPC interrupt |
587 |
|
*/ |
588 |
|
|
589 |
< |
// Atomic operations |
590 |
< |
extern int atomic_add(int *var, int v); |
591 |
< |
extern int atomic_and(int *var, int v); |
592 |
< |
extern int atomic_or(int *var, int v); |
589 |
> |
#if !ASYNC_IRQ |
590 |
> |
void TriggerInterrupt(void) |
591 |
> |
{ |
592 |
> |
#if 0 |
593 |
> |
WriteMacInt32(0x16a, ReadMacInt32(0x16a) + 1); |
594 |
> |
#else |
595 |
> |
// Trigger interrupt to main cpu only |
596 |
> |
if (main_cpu) |
597 |
> |
main_cpu->trigger_interrupt(); |
598 |
> |
#endif |
599 |
> |
} |
600 |
> |
#endif |
601 |
|
|
602 |
< |
void HandleInterrupt(void) |
602 |
> |
void sheepshaver_cpu::handle_interrupt(void) |
603 |
|
{ |
604 |
|
// Do nothing if interrupts are disabled |
605 |
< |
if (ReadMacInt32(XLM_IRQ_NEST) > 0 || InterruptFlags == 0) |
605 |
> |
if (int32(ReadMacInt32(XLM_IRQ_NEST)) > 0) |
606 |
|
return; |
607 |
|
|
608 |
< |
// Do nothing if CPU objects are not initialized yet |
609 |
< |
if (current_cpu == NULL) |
608 |
> |
// Do nothing if there is no interrupt pending |
609 |
> |
if (InterruptFlags == 0) |
610 |
|
return; |
611 |
|
|
612 |
|
// Disable MacOS stack sniffer |
618 |
|
// 68k emulator active, trigger 68k interrupt level 1 |
619 |
|
assert(current_cpu == main_cpu); |
620 |
|
WriteMacInt16(tswap32(kernel_data->v[0x67c >> 2]), 1); |
621 |
< |
main_cpu->set_cr(main_cpu->get_cr() | tswap32(kernel_data->v[0x674 >> 2])); |
621 |
> |
set_cr(get_cr() | tswap32(kernel_data->v[0x674 >> 2])); |
622 |
|
break; |
623 |
|
|
624 |
|
#if INTERRUPTS_IN_NATIVE_MODE |
625 |
|
case MODE_NATIVE: |
626 |
|
// 68k emulator inactive, in nanokernel? |
627 |
|
assert(current_cpu == main_cpu); |
628 |
< |
if (main_cpu->gpr(1) != KernelDataAddr) { |
628 |
> |
if (gpr(1) != KernelDataAddr) { |
629 |
|
// Prepare for 68k interrupt level 1 |
630 |
|
WriteMacInt16(tswap32(kernel_data->v[0x67c >> 2]), 1); |
631 |
|
WriteMacInt32(tswap32(kernel_data->v[0x658 >> 2]) + 0xdc, |
633 |
|
| tswap32(kernel_data->v[0x674 >> 2])); |
634 |
|
|
635 |
|
// Execute nanokernel interrupt routine (this will activate the 68k emulator) |
636 |
< |
atomic_add((int32 *)XLM_IRQ_NEST, htonl(1)); |
637 |
< |
current_cpu = interrupt_cpu; |
636 |
> |
DisableInterrupt(); |
637 |
> |
cpu_push(interrupt_cpu); |
638 |
|
if (ROMType == ROMTYPE_NEWWORLD) |
639 |
< |
current_cpu->interrupt(ROM_BASE + 0x312b1c, main_cpu->get_sp()); |
639 |
> |
current_cpu->interrupt(ROM_BASE + 0x312b1c); |
640 |
|
else |
641 |
< |
current_cpu->interrupt(ROM_BASE + 0x312a3c, main_cpu->get_sp()); |
642 |
< |
current_cpu = main_cpu; |
641 |
> |
current_cpu->interrupt(ROM_BASE + 0x312a3c); |
642 |
> |
cpu_pop(); |
643 |
|
} |
644 |
|
break; |
645 |
|
#endif |
653 |
|
M68kRegisters r; |
654 |
|
uint32 old_r25 = ReadMacInt32(XLM_68K_R25); // Save interrupt level |
655 |
|
WriteMacInt32(XLM_68K_R25, 0x21); // Execute with interrupt level 1 |
656 |
< |
static const uint16 proc[] = { |
657 |
< |
0x3f3c, 0x0000, // move.w #$0000,-(sp) (fake format word) |
658 |
< |
0x487a, 0x000a, // pea @1(pc) (return address) |
659 |
< |
0x40e7, // move sr,-(sp) (saved SR) |
660 |
< |
0x2078, 0x0064, // move.l $64,a0 |
661 |
< |
0x4ed0, // jmp (a0) |
662 |
< |
M68K_RTS // @1 |
656 |
> |
static const uint8 proc[] = { |
657 |
> |
0x3f, 0x3c, 0x00, 0x00, // move.w #$0000,-(sp) (fake format word) |
658 |
> |
0x48, 0x7a, 0x00, 0x0a, // pea @1(pc) (return address) |
659 |
> |
0x40, 0xe7, // move sr,-(sp) (saved SR) |
660 |
> |
0x20, 0x78, 0x00, 0x064, // move.l $64,a0 |
661 |
> |
0x4e, 0xd0, // jmp (a0) |
662 |
> |
M68K_RTS >> 8, M68K_RTS & 0xff // @1 |
663 |
|
}; |
664 |
|
Execute68k((uint32)proc, &r); |
665 |
|
WriteMacInt32(XLM_68K_R25, old_r25); // Restore interrupt level |
683 |
|
* Execute NATIVE_OP opcode (called by PowerPC emulator) |
684 |
|
*/ |
685 |
|
|
686 |
< |
#define POWERPC_NATIVE_OP(selector) \ |
687 |
< |
{ tswap32(POWERPC_EMUL_OP | 2 | (((uint32)selector) << 6)) } |
686 |
> |
#define POWERPC_NATIVE_OP_INIT(LR, OP) \ |
687 |
> |
tswap32(POWERPC_EMUL_OP | ((LR) << 11) | (((uint32)OP) << 6) | 2) |
688 |
|
|
689 |
|
// FIXME: Make sure 32-bit relocations are used |
690 |
|
const uint32 NativeOpTable[NATIVE_OP_MAX] = { |
691 |
< |
POWERPC_NATIVE_OP(NATIVE_PATCH_NAME_REGISTRY), |
692 |
< |
POWERPC_NATIVE_OP(NATIVE_VIDEO_INSTALL_ACCEL), |
693 |
< |
POWERPC_NATIVE_OP(NATIVE_VIDEO_VBL), |
694 |
< |
POWERPC_NATIVE_OP(NATIVE_VIDEO_DO_DRIVER_IO), |
695 |
< |
POWERPC_NATIVE_OP(NATIVE_ETHER_IRQ), |
696 |
< |
POWERPC_NATIVE_OP(NATIVE_ETHER_INIT), |
697 |
< |
POWERPC_NATIVE_OP(NATIVE_ETHER_TERM), |
698 |
< |
POWERPC_NATIVE_OP(NATIVE_ETHER_OPEN), |
699 |
< |
POWERPC_NATIVE_OP(NATIVE_ETHER_CLOSE), |
700 |
< |
POWERPC_NATIVE_OP(NATIVE_ETHER_WPUT), |
701 |
< |
POWERPC_NATIVE_OP(NATIVE_ETHER_RSRV), |
702 |
< |
POWERPC_NATIVE_OP(NATIVE_SERIAL_NOTHING), |
703 |
< |
POWERPC_NATIVE_OP(NATIVE_SERIAL_OPEN), |
704 |
< |
POWERPC_NATIVE_OP(NATIVE_SERIAL_PRIME_IN), |
705 |
< |
POWERPC_NATIVE_OP(NATIVE_SERIAL_PRIME_OUT), |
706 |
< |
POWERPC_NATIVE_OP(NATIVE_SERIAL_CONTROL), |
707 |
< |
POWERPC_NATIVE_OP(NATIVE_SERIAL_STATUS), |
708 |
< |
POWERPC_NATIVE_OP(NATIVE_SERIAL_CLOSE), |
709 |
< |
POWERPC_NATIVE_OP(NATIVE_GET_RESOURCE), |
710 |
< |
POWERPC_NATIVE_OP(NATIVE_GET_1_RESOURCE), |
711 |
< |
POWERPC_NATIVE_OP(NATIVE_GET_IND_RESOURCE), |
712 |
< |
POWERPC_NATIVE_OP(NATIVE_GET_1_IND_RESOURCE), |
713 |
< |
POWERPC_NATIVE_OP(NATIVE_R_GET_RESOURCE), |
691 |
> |
POWERPC_NATIVE_OP_INIT(1, NATIVE_PATCH_NAME_REGISTRY), |
692 |
> |
POWERPC_NATIVE_OP_INIT(1, NATIVE_VIDEO_INSTALL_ACCEL), |
693 |
> |
POWERPC_NATIVE_OP_INIT(1, NATIVE_VIDEO_VBL), |
694 |
> |
POWERPC_NATIVE_OP_INIT(1, NATIVE_VIDEO_DO_DRIVER_IO), |
695 |
> |
POWERPC_NATIVE_OP_INIT(1, NATIVE_ETHER_IRQ), |
696 |
> |
POWERPC_NATIVE_OP_INIT(1, NATIVE_ETHER_INIT), |
697 |
> |
POWERPC_NATIVE_OP_INIT(1, NATIVE_ETHER_TERM), |
698 |
> |
POWERPC_NATIVE_OP_INIT(1, NATIVE_ETHER_OPEN), |
699 |
> |
POWERPC_NATIVE_OP_INIT(1, NATIVE_ETHER_CLOSE), |
700 |
> |
POWERPC_NATIVE_OP_INIT(1, NATIVE_ETHER_WPUT), |
701 |
> |
POWERPC_NATIVE_OP_INIT(1, NATIVE_ETHER_RSRV), |
702 |
> |
POWERPC_NATIVE_OP_INIT(1, NATIVE_SERIAL_NOTHING), |
703 |
> |
POWERPC_NATIVE_OP_INIT(1, NATIVE_SERIAL_OPEN), |
704 |
> |
POWERPC_NATIVE_OP_INIT(1, NATIVE_SERIAL_PRIME_IN), |
705 |
> |
POWERPC_NATIVE_OP_INIT(1, NATIVE_SERIAL_PRIME_OUT), |
706 |
> |
POWERPC_NATIVE_OP_INIT(1, NATIVE_SERIAL_CONTROL), |
707 |
> |
POWERPC_NATIVE_OP_INIT(1, NATIVE_SERIAL_STATUS), |
708 |
> |
POWERPC_NATIVE_OP_INIT(1, NATIVE_SERIAL_CLOSE), |
709 |
> |
POWERPC_NATIVE_OP_INIT(1, NATIVE_GET_RESOURCE), |
710 |
> |
POWERPC_NATIVE_OP_INIT(1, NATIVE_GET_1_RESOURCE), |
711 |
> |
POWERPC_NATIVE_OP_INIT(1, NATIVE_GET_IND_RESOURCE), |
712 |
> |
POWERPC_NATIVE_OP_INIT(1, NATIVE_GET_1_IND_RESOURCE), |
713 |
> |
POWERPC_NATIVE_OP_INIT(1, NATIVE_R_GET_RESOURCE), |
714 |
> |
POWERPC_NATIVE_OP_INIT(0, NATIVE_DISABLE_INTERRUPT), |
715 |
> |
POWERPC_NATIVE_OP_INIT(0, NATIVE_ENABLE_INTERRUPT), |
716 |
> |
POWERPC_NATIVE_OP_INIT(1, NATIVE_MAKE_EXECUTABLE), |
717 |
|
}; |
718 |
|
|
719 |
|
static void get_resource(void); |
775 |
|
GPR(3) = serial_callbacks[selector - NATIVE_SERIAL_NOTHING](GPR(3), GPR(4)); |
776 |
|
break; |
777 |
|
} |
778 |
+ |
case NATIVE_DISABLE_INTERRUPT: |
779 |
+ |
DisableInterrupt(); |
780 |
+ |
break; |
781 |
+ |
case NATIVE_ENABLE_INTERRUPT: |
782 |
+ |
EnableInterrupt(); |
783 |
+ |
break; |
784 |
+ |
case NATIVE_MAKE_EXECUTABLE: |
785 |
+ |
MakeExecutable(0, (void *)GPR(4), GPR(5)); |
786 |
+ |
break; |
787 |
|
default: |
788 |
|
printf("FATAL: NATIVE_OP called with bogus selector %d\n", selector); |
789 |
|
QuitEmulator(); |
823 |
|
|
824 |
|
void Execute68kTrap(uint16 trap, M68kRegisters *r) |
825 |
|
{ |
826 |
< |
uint16 proc[2] = {trap, M68K_RTS}; |
826 |
> |
uint16 proc[2]; |
827 |
> |
proc[0] = htons(trap); |
828 |
> |
proc[1] = htons(M68K_RTS); |
829 |
|
Execute68k((uint32)proc, r); |
830 |
|
} |
831 |
|
|
881 |
|
} |
882 |
|
|
883 |
|
/* |
783 |
– |
* Atomic operations |
784 |
– |
*/ |
785 |
– |
|
786 |
– |
int atomic_add(int *var, int v) |
787 |
– |
{ |
788 |
– |
int ret = *var; |
789 |
– |
*var += v; |
790 |
– |
return ret; |
791 |
– |
} |
792 |
– |
|
793 |
– |
int atomic_and(int *var, int v) |
794 |
– |
{ |
795 |
– |
int ret = *var; |
796 |
– |
*var &= v; |
797 |
– |
return ret; |
798 |
– |
} |
799 |
– |
|
800 |
– |
int atomic_or(int *var, int v) |
801 |
– |
{ |
802 |
– |
int ret = *var; |
803 |
– |
*var |= v; |
804 |
– |
return ret; |
805 |
– |
} |
806 |
– |
|
807 |
– |
/* |
884 |
|
* Resource Manager thunks |
885 |
|
*/ |
886 |
|
|
811 |
– |
extern "C" void check_load_invoc(uint32 type, int16 id, uint16 **h); |
812 |
– |
|
887 |
|
void get_resource(void) |
888 |
|
{ |
889 |
|
current_cpu->get_resource(ReadMacInt32(XLM_GET_RESOURCE)); |