Search
lxdream.org :: lxdream/src/sh4/sh4trans.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/sh4trans.c
changeset 1103:de9ad2c0cf56
prev1094:d2324eb67223
next1112:4cac5e474d4c
author nkeynes
date Sun Feb 21 11:19:59 2010 +1000 (14 years ago)
permissions -rw-r--r--
last change Update sh4r.pc before doing the syscall - mainly so that debugging etc
statements come out with a useful PC value rather than the syscall id
view annotate diff log raw
     1 /**
     2  * $Id$
     3  * 
     4  * SH4 translation core module. This part handles the non-target-specific
     5  * section of the translation.
     6  *
     7  * Copyright (c) 2005 Nathan Keynes.
     8  *
     9  * This program is free software; you can redistribute it and/or modify
    10  * it under the terms of the GNU General Public License as published by
    11  * the Free Software Foundation; either version 2 of the License, or
    12  * (at your option) any later version.
    13  *
    14  * This program is distributed in the hope that it will be useful,
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    17  * GNU General Public License for more details.
    18  */
    19 #include <assert.h>
    20 #include "eventq.h"
    21 #include "syscall.h"
    22 #include "clock.h"
    23 #include "dreamcast.h"
    24 #include "sh4/sh4core.h"
    25 #include "sh4/sh4trans.h"
    26 #include "sh4/sh4mmio.h"
    27 #include "sh4/mmu.h"
    28 #include "xlat/xltcache.h"
    30 /**
    31  * Execute a timeslice using translated code only (ie translate/execute loop)
    32  */
    33 uint32_t sh4_translate_run_slice( uint32_t nanosecs ) 
    34 {
    35     void * (*code)() = NULL;
    36     while( sh4r.slice_cycle < nanosecs ) {
    37         if( sh4r.event_pending <= sh4r.slice_cycle ) {
    38             if( sh4r.event_types & PENDING_EVENT ) {
    39                 event_execute();
    40             }
    41             /* Eventq execute may (quite likely) deliver an immediate IRQ */
    42             if( sh4r.event_types & PENDING_IRQ ) {
    43                 sh4_accept_interrupt();
    44                 code = NULL;
    45             }
    46         }
    48         if( code == NULL ) {
    49             if( IS_SYSCALL(sh4r.pc) ) {
    50                 uint32_t pc = sh4r.pc;
    51                 sh4r.pc = sh4r.pr;
    52                 sh4r.in_delay_slot = 0;
    53                 syscall_invoke( pc );
    54             }
    56             code = xlat_get_code_by_vma( sh4r.pc );
    57             if( code == NULL || sh4r.xlat_sh4_mode != XLAT_BLOCK_MODE(code) ) {
    58                 code = sh4_translate_basic_block( sh4r.pc );
    59             }
    60         } else if( sh4r.xlat_sh4_mode != XLAT_BLOCK_MODE(code) ) {
    61             if( !IS_IN_ICACHE(sh4r.pc) ) {
    62                 /* If TLB is off, we may have gotten here without updating
    63                  * the icache, so do it now. This should never fail, so...
    64                  */
    65                 mmu_update_icache(sh4r.pc);
    66                 assert( IS_IN_ICACHE(sh4r.pc) ); 
    67             }
    68             code = sh4_translate_basic_block( sh4r.pc );
    69         }
    70         code = code();
    71     }
    72     return nanosecs;
    73 }
    75 uint8_t *xlat_output;
    76 xlat_cache_block_t xlat_current_block;
    77 struct xlat_recovery_record xlat_recovery[MAX_RECOVERY_SIZE];
    78 uint32_t xlat_recovery_posn;
    80 void sh4_translate_add_recovery( uint32_t icount )
    81 {
    82     xlat_recovery[xlat_recovery_posn].xlat_offset = 
    83         ((uintptr_t)xlat_output) - ((uintptr_t)xlat_current_block->code);
    84     xlat_recovery[xlat_recovery_posn].sh4_icount = icount;
    85     xlat_recovery_posn++;
    86 }
    88 /**
    89  * Translate a linear basic block, ie all instructions from the start address
    90  * (inclusive) until the next branch/jump instruction or the end of the page
    91  * is reached.
    92  * @param start VMA of the block start (which must already be in the icache)
    93  * @return the address of the translated block
    94  * eg due to lack of buffer space.
    95  */
    96 void * sh4_translate_basic_block( sh4addr_t start )
    97 {
    98     sh4addr_t pc = start;
    99     sh4addr_t lastpc = (pc&0xFFFFF000)+0x1000;
   100     int done, i;
   101     xlat_current_block = xlat_start_block( GET_ICACHE_PHYS(start) );
   102     xlat_output = (uint8_t *)xlat_current_block->code;
   103     xlat_recovery_posn = 0;
   104     uint8_t *eob = xlat_output + xlat_current_block->size;
   106     if( GET_ICACHE_END() < lastpc ) {
   107         lastpc = GET_ICACHE_END();
   108     }
   110     sh4_translate_begin_block(pc);
   112     do {
   113         if( eob - xlat_output < MAX_INSTRUCTION_SIZE ) {
   114             uint8_t *oldstart = xlat_current_block->code;
   115             xlat_current_block = xlat_extend_block( xlat_output - oldstart + MAX_INSTRUCTION_SIZE );
   116             xlat_output = xlat_current_block->code + (xlat_output - oldstart);
   117             eob = xlat_current_block->code + xlat_current_block->size;
   118         }
   119         done = sh4_translate_instruction( pc ); 
   120         assert( xlat_output <= eob );
   121         pc += 2;
   122         if ( pc >= lastpc ) {
   123             done = 2;
   124         }
   125     } while( !done );
   126     pc += (done - 2);
   128     // Add end-of-block recovery for post-instruction checks
   129     sh4_translate_add_recovery( (pc - start)>>1 ); 
   131     int epilogue_size = sh4_translate_end_block_size();
   132     uint32_t recovery_size = sizeof(struct xlat_recovery_record)*xlat_recovery_posn;
   133     uint32_t finalsize = (xlat_output - xlat_current_block->code) + epilogue_size + recovery_size;
   134     if( xlat_current_block->size < finalsize ) {
   135         uint8_t *oldstart = xlat_current_block->code;
   136         xlat_current_block = xlat_extend_block( finalsize );
   137         xlat_output = xlat_current_block->code + (xlat_output - oldstart);
   138     }	
   139     sh4_translate_end_block(pc);
   140     assert( xlat_output <= (xlat_current_block->code + xlat_current_block->size - recovery_size) );
   142     /* Write the recovery records onto the end of the code block */
   143     memcpy( xlat_output, xlat_recovery, recovery_size);
   144     xlat_current_block->recover_table_offset = xlat_output - (uint8_t *)xlat_current_block->code;
   145     xlat_current_block->recover_table_size = xlat_recovery_posn;
   146     xlat_current_block->xlat_sh4_mode = sh4r.xlat_sh4_mode;
   147     xlat_commit_block( finalsize, pc-start );
   148     return xlat_current_block->code;
   149 }
   151 /**
   152  * "Execute" the supplied recovery record. Currently this only updates
   153  * sh4r.pc and sh4r.slice_cycle according to the currently executing
   154  * instruction. In future this may be more sophisticated (ie will
   155  * call into generated code).
   156  */
   157 void sh4_translate_run_recovery( xlat_recovery_record_t recovery )
   158 {
   159     sh4r.slice_cycle += (recovery->sh4_icount * sh4_cpu_period);
   160     sh4r.pc += (recovery->sh4_icount<<1);
   161 }
   163 /**
   164  * Same as sh4_translate_run_recovery, but is used to recover from a taken
   165  * exception - that is, it fixes sh4r.spc rather than sh4r.pc
   166  */
   167 void sh4_translate_run_exception_recovery( xlat_recovery_record_t recovery )
   168 {
   169     sh4r.slice_cycle += (recovery->sh4_icount * sh4_cpu_period);
   170     sh4r.spc += (recovery->sh4_icount<<1);
   171 }    
   173 void sh4_translate_exit_recover( )
   174 {
   175     void *code = xlat_get_code_by_vma( sh4r.pc );
   176     if( code != NULL ) {
   177         uint32_t size = xlat_get_code_size( code );
   178         void *pc = xlat_get_native_pc( code, size );
   179         if( pc != NULL ) {
   180             // could be null if we're not actually running inside the translator
   181             xlat_recovery_record_t recover = xlat_get_pre_recovery(code, pc);
   182             if( recover != NULL ) {
   183                 // Can be null if there is no recovery necessary
   184                 sh4_translate_run_recovery(recover);
   185             }
   186         }
   187     }
   188 }
   190 void sh4_translate_exception_exit_recover( )
   191 {
   192     void *code = xlat_get_code_by_vma( sh4r.spc );
   193     if( code != NULL ) {
   194         uint32_t size = xlat_get_code_size( code );
   195         void *pc = xlat_get_native_pc( code, size );
   196         if( pc != NULL ) {
   197             // could be null if we're not actually running inside the translator
   198             xlat_recovery_record_t recover = xlat_get_pre_recovery(code, pc);
   199             if( recover != NULL ) {
   200                 // Can be null if there is no recovery necessary
   201                 sh4_translate_run_exception_recovery(recover);
   202             }
   203         }
   204     }
   206 }
   208 void FASTCALL sh4_translate_breakpoint_hit(uint32_t pc)
   209 {
   210     if( sh4_starting && sh4r.slice_cycle == 0 && pc == sh4r.pc ) {
   211         return;
   212     }
   213     sh4_core_exit( CORE_EXIT_BREAKPOINT );
   214 }
   216 void * FASTCALL xlat_get_code_by_vma( sh4vma_t vma )
   217 {
   218     void *result = NULL;
   220     if( IS_IN_ICACHE(vma) ) {
   221         return xlat_get_code( GET_ICACHE_PHYS(vma) );
   222     }
   224     if( IS_SYSCALL(vma) ) {
   225         // lxdream hook
   226         return NULL;
   227     }
   229     if( !mmu_update_icache(vma) ) {
   230         // fault - off to the fault handler
   231         if( !mmu_update_icache(sh4r.pc) ) {
   232             // double fault - halt
   233             ERROR( "Double fault - halting" );
   234             sh4_core_exit(CORE_EXIT_HALT);
   235             return NULL;
   236         }
   237     }
   239     assert( IS_IN_ICACHE(sh4r.pc) );
   240     result = xlat_get_code( GET_ICACHE_PHYS(sh4r.pc) );
   241     return result;
   242 }
   244 /**
   245  * Crashdump translation information.
   246  *
   247  * Print out the currently executing block (if any), in source and target
   248  * assembly.
   249  *
   250  * Note: we want to be _really_ careful not to cause a second-level crash
   251  * at this point (e.g. if the lookup tables are corrupted...)
   252  */
   253 void sh4_translate_crashdump()
   254 {
   255     if( !IS_IN_ICACHE(sh4r.pc) ) {
   256         /** If we're crashing due to an icache lookup failure, we'll probably
   257          * hit this case - just complain and return.
   258          */
   259         fprintf( stderr, "** SH4 PC not in current instruction region **\n" );
   260         return;
   261     }
   262     uint32_t pma = GET_ICACHE_PHYS(sh4r.pc);
   263     void *code = xlat_get_code( pma );
   264     if( code == NULL ) {
   265         fprintf( stderr, "** No translated block for current SH4 PC **\n" );
   266         return;
   267     }
   269     /* Sanity check on the code pointer */
   270     if( !xlat_is_code_pointer(code) ) {
   271         fprintf( stderr, "** Possibly corrupt translation cache **\n" );
   272         return;
   273     }
   275     void *native_pc = xlat_get_native_pc( code, xlat_get_code_size(code) );
   276     sh4_translate_disasm_block( stderr, code, sh4r.pc, native_pc );
   277 }
   279 /**
   280  * Dual-dump the translated block and original SH4 code for the basic block
   281  * starting at sh4_pc. If there is no translated block, this prints an error
   282  * and returns.
   283  */
   284 void sh4_translate_dump_block( uint32_t sh4_pc )
   285 {
   286     if( !IS_IN_ICACHE(sh4_pc) ) {
   287         fprintf( stderr, "** Address %08x not in current instruction region **\n", sh4_pc );
   288         return;
   289     }
   290     uint32_t pma = GET_ICACHE_PHYS(sh4_pc);
   291     void *code = xlat_get_code( pma );
   292     if( code == NULL ) {
   293         fprintf( stderr, "** No translated block for address %08x **\n", sh4_pc );
   294         return;
   295     }
   296     sh4_translate_disasm_block( stderr, code, sh4_pc, NULL );
   297 }
.