Search
lxdream.org :: lxdream/src/sh4/sh4trans.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/sh4trans.c
changeset 711:4f0ba72e58fe
prev669:ab344e42bca9
next736:a02d1475ccfd
author nkeynes
date Sun Jul 06 04:52:37 2008 +0000 (15 years ago)
permissions -rw-r--r--
last change Fix exit code from exit() being lost (from SH4 code)
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 <setjmp.h>
    21 #include "eventq.h"
    22 #include "syscall.h"
    23 #include "clock.h"
    24 #include "dreamcast.h"
    25 #include "sh4/sh4core.h"
    26 #include "sh4/sh4trans.h"
    27 #include "sh4/xltcache.h"
    30 static jmp_buf xlat_jmp_buf;
    31 static gboolean xlat_running = FALSE;
    33 gboolean sh4_xlat_is_running()
    34 {
    35     return xlat_running;
    36 }
    38 /**
    39  * Execute a timeslice using translated code only (ie translate/execute loop)
    40  */
    41 uint32_t sh4_xlat_run_slice( uint32_t nanosecs ) 
    42 {
    43     sh4r.slice_cycle = 0;
    45     if( sh4r.sh4_state != SH4_STATE_RUNNING ) {
    46 	sh4_sleep_run_slice(nanosecs);
    47     }
    49     switch( setjmp(xlat_jmp_buf) ) {
    50     case XLAT_EXIT_BREAKPOINT:
    51 	sh4_clear_breakpoint( sh4r.pc, BREAK_ONESHOT );
    52 	/* fallthrough */
    53     case XLAT_EXIT_HALT:
    54 	if( sh4r.sh4_state != SH4_STATE_STANDBY ) {
    55 	    TMU_run_slice( sh4r.slice_cycle );
    56 	    SCIF_run_slice( sh4r.slice_cycle );
    57 	    dreamcast_stop();
    58 	    return sh4r.slice_cycle;
    59 	}
    60     case XLAT_EXIT_SYSRESET:
    61 	dreamcast_reset();
    62 	break;
    63     case XLAT_EXIT_SLEEP:
    64 	sh4_sleep_run_slice(nanosecs);
    65         break;	
    66     }
    68     xlat_running = TRUE;
    69     void * (*code)() = NULL;
    70     while( sh4r.slice_cycle < nanosecs ) {
    71 	if( sh4r.event_pending <= sh4r.slice_cycle ) {
    72 	    if( sh4r.event_types & PENDING_EVENT ) {
    73 		event_execute();
    74 	    }
    75 	    /* Eventq execute may (quite likely) deliver an immediate IRQ */
    76 	    if( sh4r.event_types & PENDING_IRQ ) {
    77 		sh4_accept_interrupt();
    78 		code = NULL;
    79 	    }
    80 	}
    82 	if( code == NULL ) {
    83 	    if( sh4r.pc > 0xFFFFFF00 ) {
    84 		syscall_invoke( sh4r.pc );
    85 		sh4r.in_delay_slot = 0;
    86 		sh4r.pc = sh4r.pr;
    87 	    }
    89 	    code = xlat_get_code_by_vma( sh4r.pc );
    90 	    if( code == NULL ) {
    91 		code = sh4_translate_basic_block( sh4r.pc );
    92 	    }
    93 	}
    94 	code = code();
    95     }
    97     xlat_running = FALSE;
    98     sh4_starting = FALSE;
    99     sh4r.slice_cycle = nanosecs;
   100     if( sh4r.sh4_state != SH4_STATE_STANDBY ) {
   101 	TMU_run_slice( nanosecs );
   102 	SCIF_run_slice( nanosecs );
   103     }
   104     return nanosecs;
   105 }
   107 uint8_t *xlat_output;
   108 xlat_cache_block_t xlat_current_block;
   109 struct xlat_recovery_record xlat_recovery[MAX_RECOVERY_SIZE];
   110 uint32_t xlat_recovery_posn;
   112 void sh4_translate_add_recovery( uint32_t icount )
   113 {
   114     xlat_recovery[xlat_recovery_posn].xlat_offset = 
   115 	((uintptr_t)xlat_output) - ((uintptr_t)xlat_current_block->code);
   116     xlat_recovery[xlat_recovery_posn].sh4_icount = icount;
   117     xlat_recovery_posn++;
   118 }
   120 /**
   121  * Translate a linear basic block, ie all instructions from the start address
   122  * (inclusive) until the next branch/jump instruction or the end of the page
   123  * is reached.
   124  * @return the address of the translated block
   125  * eg due to lack of buffer space.
   126  */
   127 void * sh4_translate_basic_block( sh4addr_t start )
   128 {
   129     sh4addr_t pc = start;
   130     sh4addr_t lastpc = (pc&0xFFFFF000)+0x1000;
   131     int done, i;
   132     xlat_current_block = xlat_start_block( start );
   133     xlat_output = (uint8_t *)xlat_current_block->code;
   134     xlat_recovery_posn = 0;
   135     uint8_t *eob = xlat_output + xlat_current_block->size;
   137     if( GET_ICACHE_END() < lastpc ) {
   138         lastpc = GET_ICACHE_END();
   139     }
   141     sh4_translate_begin_block(pc);
   143     do {
   144         /* check for breakpoints at this pc */
   145         for( i=0; i<sh4_breakpoint_count; i++ ) {
   146             if( sh4_breakpoints[i].address == pc ) {
   147                 sh4_translate_emit_breakpoint(pc);
   148                 break;
   149             }
   150         }
   151         if( eob - xlat_output < MAX_INSTRUCTION_SIZE ) {
   152             uint8_t *oldstart = xlat_current_block->code;
   153             xlat_current_block = xlat_extend_block( xlat_output - oldstart + MAX_INSTRUCTION_SIZE );
   154             xlat_output = xlat_current_block->code + (xlat_output - oldstart);
   155             eob = xlat_current_block->code + xlat_current_block->size;
   156         }
   157         done = sh4_translate_instruction( pc ); 
   158         assert( xlat_output <= eob );
   159         pc += 2;
   160         if ( pc >= lastpc ) {
   161             done = 2;
   162         }
   163     } while( !done );
   164     pc += (done - 2);
   166     // Add end-of-block recovery for post-instruction checks
   167     sh4_translate_add_recovery( (pc - start)>>1 ); 
   169     int epilogue_size = sh4_translate_end_block_size();
   170     uint32_t recovery_size = sizeof(struct xlat_recovery_record)*xlat_recovery_posn;
   171     uint32_t finalsize = (xlat_output - xlat_current_block->code) + epilogue_size + recovery_size;
   172     if( xlat_current_block->size < finalsize ) {
   173         uint8_t *oldstart = xlat_current_block->code;
   174         xlat_current_block = xlat_extend_block( finalsize );
   175         xlat_output = xlat_current_block->code + (xlat_output - oldstart);
   176     }	
   177     sh4_translate_end_block(pc);
   178     assert( xlat_output <= (xlat_current_block->code + xlat_current_block->size - recovery_size) );
   180     /* Write the recovery records onto the end of the code block */
   181     memcpy( xlat_output, xlat_recovery, recovery_size);
   182     xlat_current_block->recover_table_offset = xlat_output - (uint8_t *)xlat_current_block->code;
   183     xlat_current_block->recover_table_size = xlat_recovery_posn;
   184     xlat_commit_block( finalsize, pc-start );
   185     return xlat_current_block->code;
   186 }
   188 /**
   189  * "Execute" the supplied recovery record. Currently this only updates
   190  * sh4r.pc and sh4r.slice_cycle according to the currently executing
   191  * instruction. In future this may be more sophisticated (ie will
   192  * call into generated code).
   193  */
   194 void sh4_translate_run_recovery( xlat_recovery_record_t recovery )
   195 {
   196     sh4r.slice_cycle += (recovery->sh4_icount * sh4_cpu_period);
   197     sh4r.pc += (recovery->sh4_icount<<1);
   198 }
   200 void sh4_translate_unwind_stack( gboolean abort_after, unwind_thunk_t thunk )
   201 {
   202     void *pc = xlat_get_native_pc();
   204     assert( pc != NULL );
   205     void *code = xlat_get_code( sh4r.pc );
   206     xlat_recovery_record_t recover = xlat_get_recovery(code, pc, TRUE);
   207     if( recover != NULL ) {
   208 	// Can be null if there is no recovery necessary
   209 	sh4_translate_run_recovery(recover);
   210     }
   211     if( thunk != NULL ) {
   212 	thunk();
   213     }
   214     // finally longjmp back into sh4_xlat_run_slice
   215     xlat_running = FALSE;
   216     longjmp(xlat_jmp_buf, XLAT_EXIT_CONTINUE);
   217 } 
   219 void sh4_translate_exit( int exit_code )
   220 {
   221     void *pc = xlat_get_native_pc();
   222     if( pc != NULL ) {
   223 	// could be null if we're not actually running inside the translator
   224 	void *code = xlat_get_code( sh4r.pc );
   225 	xlat_recovery_record_t recover = xlat_get_recovery(code, pc, TRUE);
   226 	if( recover != NULL ) {
   227 	    // Can be null if there is no recovery necessary
   228 	    sh4_translate_run_recovery(recover);
   229 	}
   230     }
   231     // finally longjmp back into sh4_xlat_run_slice
   232     xlat_running = FALSE;
   233     longjmp(xlat_jmp_buf, exit_code);
   234 }
   236 void sh4_translate_breakpoint_hit(uint32_t pc)
   237 {
   238     if( sh4_starting && sh4r.slice_cycle == 0 && pc == sh4r.pc ) {
   239 	return;
   240     }
   241     sh4_translate_exit( XLAT_EXIT_BREAKPOINT );
   242 }
   244 /**
   245  * Exit the current block at the end of the current instruction, flush the
   246  * translation cache (completely) and return control to sh4_xlat_run_slice.
   247  *
   248  * As a special case, if the current instruction is actually the last 
   249  * instruction in the block (ie it's in a delay slot), this function 
   250  * returns to allow normal completion of the translation block. Otherwise
   251  * this function never returns.
   252  *
   253  * Must only be invoked (indirectly) from within translated code.
   254  */
   255 void sh4_translate_flush_cache()
   256 {
   257     void *pc = xlat_get_native_pc();
   258     assert( pc != NULL );
   260     void *code = xlat_get_code( sh4r.pc );
   261     xlat_recovery_record_t recover = xlat_get_recovery(code, pc, TRUE);
   262     if( recover != NULL ) {
   263 	// Can be null if there is no recovery necessary
   264 	sh4_translate_run_recovery(recover);
   265 	xlat_flush_cache();
   266 	xlat_running = FALSE;
   267 	longjmp(xlat_jmp_buf, XLAT_EXIT_CONTINUE);
   268     } else {
   269 	xlat_flush_cache();
   270 	return;
   271     }
   272 }
   274 void *xlat_get_code_by_vma( sh4vma_t vma )
   275 {
   276     void *result = NULL;
   278     if( IS_IN_ICACHE(vma) ) {
   279 	return xlat_get_code( GET_ICACHE_PHYS(vma) );
   280     }
   282     if( vma > 0xFFFFFF00 ) {
   283 	// lxdream hook
   284 	return NULL;
   285     }
   287     if( !mmu_update_icache(vma) ) {
   288 	// fault - off to the fault handler
   289 	if( !mmu_update_icache(sh4r.pc) ) {
   290 	    // double fault - halt
   291 	    ERROR( "Double fault - halting" );
   292 	    dreamcast_stop();
   293 	    return NULL;
   294 	}
   295     }
   297     assert( IS_IN_ICACHE(sh4r.pc) );
   298     result = xlat_get_code( GET_ICACHE_PHYS(sh4r.pc) );
   299     return result;
   300 }
.