ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/kpx_cpu/sheepshaver_glue.cpp
(Generate patch)

Comparing SheepShaver/src/kpx_cpu/sheepshaver_glue.cpp (file contents):
Revision 1.25 by cebix, 2004-01-12T15:37:24Z vs.
Revision 1.37 by gbeauche, 2004-05-15T17:26:28Z

# Line 38 | Line 38
38   #include "name_registry.h"
39   #include "serial.h"
40   #include "ether.h"
41 + #include "timer.h"
42  
43   #include <stdio.h>
44 + #include <stdlib.h>
45  
46   #if ENABLE_MON
47   #include "mon.h"
# Line 76 | Line 78 | static void enter_mon(void)
78   // From main_*.cpp
79   extern uintptr SignalStackBase();
80  
81 + // From rsrc_patches.cpp
82 + extern "C" void check_load_invoc(uint32 type, int16 id, uint32 h);
83 +
84   // PowerPC EmulOp to exit from emulation looop
85   const uint32 POWERPC_EXEC_RETURN = POWERPC_EMUL_OP | 1;
86  
87   // Enable multicore (main/interrupts) cpu emulation?
88   #define MULTICORE_CPU (ASYNC_IRQ ? 1 : 0)
89  
90 + // Enable interrupt routine safety checks?
91 + #define SAFE_INTERRUPT_PPC 1
92 +
93   // Enable Execute68k() safety checks?
94   #define SAFE_EXEC_68K 1
95  
# Line 94 | Line 102 | const uint32 POWERPC_EXEC_RETURN = POWER
102   // Interrupts in native mode?
103   #define INTERRUPTS_IN_NATIVE_MODE 1
104  
105 + // Enable native EMUL_OPs to be run without a mode switch
106 + #define ENABLE_NATIVE_EMUL_OP 1
107 +
108   // Pointer to Kernel Data
109   static KernelData * const kernel_data = (KernelData *)KERNEL_DATA_BASE;
110  
# Line 122 | Line 133 | class sheepshaver_cpu
133          void init_decoder();
134          void execute_sheep(uint32 opcode);
135  
136 +        // Filter out EMUL_OP routines that only call native code
137 +        bool filter_execute_emul_op(uint32 emul_op);
138 +
139 +        // "Native" EMUL_OP routines
140 +        void execute_emul_op_microseconds();
141 +        void execute_emul_op_idle_time_1();
142 +        void execute_emul_op_idle_time_2();
143 +
144   public:
145  
146          // Constructor
# Line 133 | Line 152 | public:
152          uint32 get_xer() const          { return xer().get(); }
153          void set_xer(uint32 v)          { xer().set(v); }
154  
155 +        // Execute EMUL_OP routine
156 +        void execute_emul_op(uint32 emul_op);
157 +
158          // Execute 68k routine
159          void execute_68k(uint32 entry, M68kRegisters *r);
160  
# Line 142 | Line 164 | public:
164          // Execute MacOS/PPC code
165          uint32 execute_macos_code(uint32 tvect, int nargs, uint32 const *args);
166  
167 +        // Compile one instruction
168 +        virtual bool compile1(codegen_context_t & cg_context);
169 +
170          // Resource manager thunk
171          void get_resource(uint32 old_get_resource);
172  
# Line 149 | Line 174 | public:
174          void interrupt(uint32 entry);
175          void handle_interrupt();
176  
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
177          // Make sure the SIGSEGV handler can access CPU registers
178          friend sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t);
179   };
180  
181 < lazy_allocator< sheepshaver_cpu > allocator_helper< sheepshaver_cpu, lazy_allocator >::allocator;
181 > // Memory allocator returning areas aligned on 16-byte boundaries
182 > void *operator new(size_t size)
183 > {
184 >        void *p;
185 >
186 > #if defined(HAVE_POSIX_MEMALIGN)
187 >        if (posix_memalign(&p, 16, size) != 0)
188 >                throw std::bad_alloc();
189 > #elif defined(HAVE_MEMALIGN)
190 >        p = memalign(16, size);
191 > #elif defined(HAVE_VALLOC)
192 >        p = valloc(size); // page-aligned!
193 > #else
194 >        /* XXX: handle padding ourselves */
195 >        p = malloc(size);
196 > #endif
197 >
198 >        return p;
199 > }
200 >
201 > void operator delete(void *p)
202 > {
203 > #if defined(HAVE_MEMALIGN) || defined(HAVE_VALLOC)
204 > #if defined(__GLIBC__)
205 >        // this is known to work only with GNU libc
206 >        free(p);
207 > #endif
208 > #else
209 >        free(p);
210 > #endif
211 > }
212  
213   sheepshaver_cpu::sheepshaver_cpu()
214          : powerpc_cpu(enable_jit_p())
# Line 172 | Line 218 | sheepshaver_cpu::sheepshaver_cpu()
218  
219   void sheepshaver_cpu::init_decoder()
220   {
175 #ifndef PPC_NO_STATIC_II_INDEX_TABLE
176        static bool initialized = false;
177        if (initialized)
178                return;
179        initialized = true;
180 #endif
181
221          static const instr_info_t sheep_ii_table[] = {
222                  { "sheep",
223                    (execute_pmf)&sheepshaver_cpu::execute_sheep,
# Line 201 | Line 240 | void sheepshaver_cpu::init_decoder()
240   static void NativeOp(int selector);
241  
242   /*              NativeOp instruction format:
243 <                +------------+--------------------------+--+----------+------------+
244 <                |      6     |                          |FN|    OP    |      2     |
245 <                +------------+--------------------------+--+----------+------------+
246 <                 0         5 |6                       19 20 21      25 26        31
243 >                +------------+-------------------------+--+-----------+------------+
244 >                |      6     |                         |FN|    OP     |      2     |
245 >                +------------+-------------------------+--+-----------+------------+
246 >                 0         5 |6                      18 19 20      25 26        31
247   */
248  
249 < typedef bit_field< 20, 20 > FN_field;
250 < typedef bit_field< 21, 25 > NATIVE_OP_field;
249 > typedef bit_field< 19, 19 > FN_field;
250 > typedef bit_field< 20, 25 > NATIVE_OP_field;
251   typedef bit_field< 26, 31 > EMUL_OP_field;
252  
253 + // "Native" EMUL_OP routines
254 + #define GPR_A(REG) gpr(16 + (REG))
255 + #define GPR_D(REG) gpr( 8 + (REG))
256 +
257 + void sheepshaver_cpu::execute_emul_op_microseconds()
258 + {
259 +        Microseconds(GPR_A(0), GPR_D(0));
260 + }
261 +
262 + void sheepshaver_cpu::execute_emul_op_idle_time_1()
263 + {
264 +        // Sleep if no events pending
265 +        if (ReadMacInt32(0x14c) == 0)
266 +                Delay_usec(16667);
267 +        GPR_A(0) = ReadMacInt32(0x2b6);
268 + }
269 +
270 + void sheepshaver_cpu::execute_emul_op_idle_time_2()
271 + {
272 +        // Sleep if no events pending
273 +        if (ReadMacInt32(0x14c) == 0)
274 +                Delay_usec(16667);
275 +        GPR_D(0) = (uint32)-2;
276 + }
277 +
278 + // Filter out EMUL_OP routines that only call native code
279 + bool sheepshaver_cpu::filter_execute_emul_op(uint32 emul_op)
280 + {
281 +        switch (emul_op) {
282 +        case OP_MICROSECONDS:
283 +                execute_emul_op_microseconds();
284 +                return true;
285 +        case OP_IDLE_TIME:
286 +                execute_emul_op_idle_time_1();
287 +                return true;
288 +        case OP_IDLE_TIME_2:
289 +                execute_emul_op_idle_time_2();
290 +                return true;
291 +        }
292 +        return false;
293 + }
294 +
295 + // Execute EMUL_OP routine
296 + void sheepshaver_cpu::execute_emul_op(uint32 emul_op)
297 + {
298 + #if ENABLE_NATIVE_EMUL_OP
299 +        // First, filter out EMUL_OPs that can be executed without a mode switch
300 +        if (filter_execute_emul_op(emul_op))
301 +                return;
302 + #endif
303 +
304 +        M68kRegisters r68;
305 +        WriteMacInt32(XLM_68K_R25, gpr(25));
306 +        WriteMacInt32(XLM_RUN_MODE, MODE_EMUL_OP);
307 +        for (int i = 0; i < 8; i++)
308 +                r68.d[i] = gpr(8 + i);
309 +        for (int i = 0; i < 7; i++)
310 +                r68.a[i] = gpr(16 + i);
311 +        r68.a[7] = gpr(1);
312 +        uint32 saved_cr = get_cr() & CR_field<2>::mask();
313 +        uint32 saved_xer = get_xer();
314 +        EmulOp(&r68, gpr(24), emul_op);
315 +        set_cr(saved_cr);
316 +        set_xer(saved_xer);
317 +        for (int i = 0; i < 8; i++)
318 +                gpr(8 + i) = r68.d[i];
319 +        for (int i = 0; i < 7; i++)
320 +                gpr(16 + i) = r68.a[i];
321 +        gpr(1) = r68.a[7];
322 +        WriteMacInt32(XLM_RUN_MODE, MODE_68K);
323 + }
324 +
325   // Execute SheepShaver instruction
326   void sheepshaver_cpu::execute_sheep(uint32 opcode)
327   {
# Line 234 | Line 345 | void sheepshaver_cpu::execute_sheep(uint
345                          pc() += 4;
346                  break;
347  
348 <        default: {      // EMUL_OP
349 <                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);
348 >        default:        // EMUL_OP
349 >                execute_emul_op(EMUL_OP_field::extract(opcode) - 3);
350                  pc() += 4;
351                  break;
352          }
353 + }
354 +
355 + // Compile one instruction
356 + bool sheepshaver_cpu::compile1(codegen_context_t & cg_context)
357 + {
358 + #if PPC_ENABLE_JIT
359 +        const instr_info_t *ii = cg_context.instr_info;
360 +        if (ii->mnemo != PPC_I(SHEEP))
361 +                return false;
362 +
363 +        bool compiled = false;
364 +        powerpc_dyngen & dg = cg_context.codegen;
365 +        uint32 opcode = cg_context.opcode;
366 +
367 +        switch (opcode & 0x3f) {
368 +        case 0:         // EMUL_RETURN
369 +                dg.gen_invoke(QuitEmulator);
370 +                compiled = true;
371 +                break;
372 +
373 +        case 1:         // EXEC_RETURN
374 +                dg.gen_spcflags_set(SPCFLAG_CPU_EXEC_RETURN);
375 +                compiled = true;
376 +                break;
377 +
378 +        case 2: {       // EXEC_NATIVE
379 +                uint32 selector = NATIVE_OP_field::extract(opcode);
380 +                switch (selector) {
381 +                case NATIVE_PATCH_NAME_REGISTRY:
382 +                        dg.gen_invoke(DoPatchNameRegistry);
383 +                        compiled = true;
384 +                        break;
385 +                case NATIVE_VIDEO_INSTALL_ACCEL:
386 +                        dg.gen_invoke(VideoInstallAccel);
387 +                        compiled = true;
388 +                        break;
389 +                case NATIVE_VIDEO_VBL:
390 +                        dg.gen_invoke(VideoVBL);
391 +                        compiled = true;
392 +                        break;
393 +                case NATIVE_GET_RESOURCE:
394 +                case NATIVE_GET_1_RESOURCE:
395 +                case NATIVE_GET_IND_RESOURCE:
396 +                case NATIVE_GET_1_IND_RESOURCE:
397 +                case NATIVE_R_GET_RESOURCE: {
398 +                        static const uint32 get_resource_ptr[] = {
399 +                                XLM_GET_RESOURCE,
400 +                                XLM_GET_1_RESOURCE,
401 +                                XLM_GET_IND_RESOURCE,
402 +                                XLM_GET_1_IND_RESOURCE,
403 +                                XLM_R_GET_RESOURCE
404 +                        };
405 +                        uint32 old_get_resource = ReadMacInt32(get_resource_ptr[selector - NATIVE_GET_RESOURCE]);
406 +                        typedef void (*func_t)(dyngen_cpu_base, uint32);
407 +                        func_t func = (func_t)nv_mem_fun(&sheepshaver_cpu::get_resource).ptr();
408 +                        dg.gen_invoke_CPU_im(func, old_get_resource);
409 +                        compiled = true;
410 +                        break;
411 +                }
412 +                case NATIVE_DISABLE_INTERRUPT:
413 +                        dg.gen_invoke(DisableInterrupt);
414 +                        compiled = true;
415 +                        break;
416 +                case NATIVE_ENABLE_INTERRUPT:
417 +                        dg.gen_invoke(EnableInterrupt);
418 +                        compiled = true;
419 +                        break;
420 +                case NATIVE_CHECK_LOAD_INVOC:
421 +                        dg.gen_load_T0_GPR(3);
422 +                        dg.gen_load_T1_GPR(4);
423 +                        dg.gen_se_16_32_T1();
424 +                        dg.gen_load_T2_GPR(5);
425 +                        dg.gen_invoke_T0_T1_T2((void (*)(uint32, uint32, uint32))check_load_invoc);
426 +                        compiled = true;
427 +                        break;
428 +                case NATIVE_BITBLT:
429 +                        dg.gen_load_T0_GPR(3);
430 +                        dg.gen_invoke_T0((void (*)(uint32))NQD_bitblt);
431 +                        compiled = true;
432 +                        break;
433 +                case NATIVE_INVRECT:
434 +                        dg.gen_load_T0_GPR(3);
435 +                        dg.gen_invoke_T0((void (*)(uint32))NQD_invrect);
436 +                        compiled = true;
437 +                        break;
438 +                case NATIVE_FILLRECT:
439 +                        dg.gen_load_T0_GPR(3);
440 +                        dg.gen_invoke_T0((void (*)(uint32))NQD_fillrect);
441 +                        compiled = true;
442 +                        break;
443 +                }
444 +                if (FN_field::test(opcode)) {
445 +                        if (compiled) {
446 +                                dg.gen_load_A0_LR();
447 +                                dg.gen_set_PC_A0();
448 +                        }
449 +                        cg_context.done_compile = true;
450 +                }
451 +                else
452 +                        cg_context.done_compile = false;
453 +                break;
454 +        }
455 +
456 +        default: {      // EMUL_OP
457 +                uint32 emul_op = EMUL_OP_field::extract(opcode) - 3;
458 + #if ENABLE_NATIVE_EMUL_OP
459 +                typedef void (*emul_op_func_t)(dyngen_cpu_base);
460 +                emul_op_func_t emul_op_func = 0;
461 +                switch (emul_op) {
462 +                case OP_MICROSECONDS:
463 +                        emul_op_func = (emul_op_func_t)nv_mem_fun(&sheepshaver_cpu::execute_emul_op_microseconds).ptr();
464 +                        break;
465 +                case OP_IDLE_TIME:
466 +                        emul_op_func = (emul_op_func_t)nv_mem_fun(&sheepshaver_cpu::execute_emul_op_idle_time_1).ptr();
467 +                        break;
468 +                case OP_IDLE_TIME_2:
469 +                        emul_op_func = (emul_op_func_t)nv_mem_fun(&sheepshaver_cpu::execute_emul_op_idle_time_2).ptr();
470 +                        break;
471 +                }
472 +                if (emul_op_func) {
473 +                        dg.gen_invoke_CPU(emul_op_func);
474 +                        cg_context.done_compile = false;
475 +                        compiled = true;
476 +                        break;
477 +                }
478 + #endif
479 +                typedef void (*func_t)(dyngen_cpu_base, uint32);
480 +                func_t func = (func_t)nv_mem_fun(&sheepshaver_cpu::execute_emul_op).ptr();
481 +                dg.gen_invoke_CPU_im(func, emul_op);
482 +                cg_context.done_compile = false;
483 +                compiled = true;
484 +                break;
485 +        }
486          }
487 +        return compiled;
488 + #endif
489 +        return false;
490   }
491  
492   // Handle MacOS interrupt
# Line 268 | Line 497 | void sheepshaver_cpu::interrupt(uint32 e
497          const clock_t interrupt_start = clock();
498   #endif
499  
500 + #if SAFE_INTERRUPT_PPC
501 +        static int depth = 0;
502 +        if (depth != 0)
503 +                printf("FATAL: sheepshaver_cpu::interrupt() called more than once: %d\n", depth);
504 +        depth++;
505 + #endif
506 + #if SAFE_INTERRUPT_PPC >= 2
507 +        uint32 saved_regs[32];
508 +        memcpy(&saved_regs[0], &gpr(0), sizeof(saved_regs));
509 + #endif
510 +
511   #if !MULTICORE_CPU
512          // Save program counters and branch registers
513          uint32 saved_pc = pc();
# Line 325 | Line 565 | void sheepshaver_cpu::interrupt(uint32 e
565   #if EMUL_TIME_STATS
566          interrupt_time += (clock() - interrupt_start);
567   #endif
568 +
569 + #if SAFE_INTERRUPT_PPC >= 2
570 +        if (memcmp(&saved_regs[0], &gpr(0), sizeof(saved_regs)) != 0)
571 +                printf("FATAL: dirty PowerPC registers\n");
572 + #endif
573 + #if SAFE_INTERRUPT_PPC
574 +        depth--;
575 + #endif
576   }
577  
578   // Execute 68k routine
# Line 493 | Line 741 | inline void sheepshaver_cpu::execute_ppc
741   }
742  
743   // Resource Manager thunk
496 extern "C" void check_load_invoc(uint32 type, int16 id, uint32 h);
497
744   inline void sheepshaver_cpu::get_resource(uint32 old_get_resource)
745   {
746          uint32 type = gpr(3);
# Line 604 | Line 850 | static sigsegv_return_t sigsegv_handler(
850                  else if (pc == ROM_BASE + 0x4a10a0 && (cpu->gpr(20) == 0xf3012002 || cpu->gpr(20) == 0xf3012000))
851                          return SIGSEGV_RETURN_SKIP_INSTRUCTION;
852  
853 +                // Ignore writes to the zero page
854 +                else if ((uint32)(addr - SheepMem::ZeroPage()) < (uint32)SheepMem::PageSize())
855 +                        return SIGSEGV_RETURN_SKIP_INSTRUCTION;
856 +
857                  // Ignore all other faults, if requested
858                  if (PrefsFindBool("ignoresegv"))
859                          return SIGSEGV_RETURN_SKIP_INSTRUCTION;
# Line 860 | Line 1110 | static void NativeOp(int selector)
1110                  GPR(3) = false;
1111                  break;
1112   #endif
1113 +        case NATIVE_SYNC_HOOK:
1114 +                GPR(3) = NQD_sync_hook(GPR(3));
1115 +                break;
1116 +        case NATIVE_BITBLT_HOOK:
1117 +                GPR(3) = NQD_bitblt_hook(GPR(3));
1118 +                break;
1119 +        case NATIVE_BITBLT:
1120 +                NQD_bitblt(GPR(3));
1121 +                break;
1122 +        case NATIVE_FILLRECT_HOOK:
1123 +                GPR(3) = NQD_fillrect_hook(GPR(3));
1124 +                break;
1125 +        case NATIVE_INVRECT:
1126 +                NQD_invrect(GPR(3));
1127 +                break;
1128 +        case NATIVE_FILLRECT:
1129 +                NQD_fillrect(GPR(3));
1130 +                break;
1131          case NATIVE_SERIAL_NOTHING:
1132          case NATIVE_SERIAL_OPEN:
1133          case NATIVE_SERIAL_PRIME_IN:
# Line 905 | Line 1173 | static void NativeOp(int selector)
1173          case NATIVE_MAKE_EXECUTABLE:
1174                  MakeExecutable(0, (void *)GPR(4), GPR(5));
1175                  break;
1176 +        case NATIVE_CHECK_LOAD_INVOC:
1177 +                check_load_invoc(GPR(3), GPR(4), GPR(5));
1178 +                break;
1179          default:
1180                  printf("FATAL: NATIVE_OP called with bogus selector %d\n", selector);
1181                  QuitEmulator();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines