Search
lxdream.org :: lxdream/src/sh4/sh4trans.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/sh4trans.c
changeset 906:268ea359f884
prev905:4c17ebd9ef5e
next914:72abecf5a315
author nkeynes
date Thu Oct 30 00:06:49 2008 +0000 (13 years ago)
permissions -rw-r--r--
last change Change xlat_get_native_pc to pass in the expected code region - this lets the Mac
unwind implementation range test the IP address (which works) rather than EBP
(which doesn't for some reason).

Remove the test in configure that prevents fomit-frame-pointer being used in Mac
builds.
file annotate diff log raw
nkeynes@359
     1
/**
nkeynes@586
     2
 * $Id$
nkeynes@359
     3
 * 
nkeynes@359
     4
 * SH4 translation core module. This part handles the non-target-specific
nkeynes@359
     5
 * section of the translation.
nkeynes@359
     6
 *
nkeynes@359
     7
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@359
     8
 *
nkeynes@359
     9
 * This program is free software; you can redistribute it and/or modify
nkeynes@359
    10
 * it under the terms of the GNU General Public License as published by
nkeynes@359
    11
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@359
    12
 * (at your option) any later version.
nkeynes@359
    13
 *
nkeynes@359
    14
 * This program is distributed in the hope that it will be useful,
nkeynes@359
    15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@359
    16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@359
    17
 * GNU General Public License for more details.
nkeynes@359
    18
 */
nkeynes@398
    19
#include <assert.h>
nkeynes@430
    20
#include "eventq.h"
nkeynes@430
    21
#include "syscall.h"
nkeynes@586
    22
#include "clock.h"
nkeynes@669
    23
#include "dreamcast.h"
nkeynes@430
    24
#include "sh4/sh4core.h"
nkeynes@430
    25
#include "sh4/sh4trans.h"
nkeynes@430
    26
#include "sh4/xltcache.h"
nkeynes@359
    27
nkeynes@586
    28
nkeynes@359
    29
/**
nkeynes@359
    30
 * Execute a timeslice using translated code only (ie translate/execute loop)
nkeynes@359
    31
 */
nkeynes@740
    32
uint32_t sh4_translate_run_slice( uint32_t nanosecs ) 
nkeynes@359
    33
{
nkeynes@408
    34
    void * (*code)() = NULL;
nkeynes@368
    35
    while( sh4r.slice_cycle < nanosecs ) {
nkeynes@736
    36
        if( sh4r.event_pending <= sh4r.slice_cycle ) {
nkeynes@736
    37
            if( sh4r.event_types & PENDING_EVENT ) {
nkeynes@736
    38
                event_execute();
nkeynes@736
    39
            }
nkeynes@736
    40
            /* Eventq execute may (quite likely) deliver an immediate IRQ */
nkeynes@736
    41
            if( sh4r.event_types & PENDING_IRQ ) {
nkeynes@736
    42
                sh4_accept_interrupt();
nkeynes@736
    43
                code = NULL;
nkeynes@736
    44
            }
nkeynes@736
    45
        }
nkeynes@359
    46
nkeynes@736
    47
        if( code == NULL ) {
nkeynes@736
    48
            if( sh4r.pc > 0xFFFFFF00 ) {
nkeynes@736
    49
                syscall_invoke( sh4r.pc );
nkeynes@736
    50
                sh4r.in_delay_slot = 0;
nkeynes@736
    51
                sh4r.pc = sh4r.pr;
nkeynes@736
    52
            }
nkeynes@736
    53
nkeynes@736
    54
            code = xlat_get_code_by_vma( sh4r.pc );
nkeynes@901
    55
            if( code == NULL || (sh4r.fpscr & (FPSCR_PR|FPSCR_SZ)) != XLAT_BLOCK_FPSCR(code) ) {
nkeynes@736
    56
                code = sh4_translate_basic_block( sh4r.pc );
nkeynes@736
    57
            }
nkeynes@736
    58
        }
nkeynes@736
    59
        code = code();
nkeynes@359
    60
    }
nkeynes@359
    61
    return nanosecs;
nkeynes@359
    62
}
nkeynes@359
    63
nkeynes@359
    64
uint8_t *xlat_output;
nkeynes@596
    65
xlat_cache_block_t xlat_current_block;
nkeynes@586
    66
struct xlat_recovery_record xlat_recovery[MAX_RECOVERY_SIZE];
nkeynes@586
    67
uint32_t xlat_recovery_posn;
nkeynes@359
    68
nkeynes@596
    69
void sh4_translate_add_recovery( uint32_t icount )
nkeynes@596
    70
{
nkeynes@596
    71
    xlat_recovery[xlat_recovery_posn].xlat_offset = 
nkeynes@736
    72
        ((uintptr_t)xlat_output) - ((uintptr_t)xlat_current_block->code);
nkeynes@596
    73
    xlat_recovery[xlat_recovery_posn].sh4_icount = icount;
nkeynes@596
    74
    xlat_recovery_posn++;
nkeynes@596
    75
}
nkeynes@596
    76
nkeynes@359
    77
/**
nkeynes@359
    78
 * Translate a linear basic block, ie all instructions from the start address
nkeynes@359
    79
 * (inclusive) until the next branch/jump instruction or the end of the page
nkeynes@359
    80
 * is reached.
nkeynes@359
    81
 * @return the address of the translated block
nkeynes@359
    82
 * eg due to lack of buffer space.
nkeynes@359
    83
 */
nkeynes@359
    84
void * sh4_translate_basic_block( sh4addr_t start )
nkeynes@359
    85
{
nkeynes@408
    86
    sh4addr_t pc = start;
nkeynes@410
    87
    sh4addr_t lastpc = (pc&0xFFFFF000)+0x1000;
nkeynes@586
    88
    int done, i;
nkeynes@596
    89
    xlat_current_block = xlat_start_block( start );
nkeynes@596
    90
    xlat_output = (uint8_t *)xlat_current_block->code;
nkeynes@586
    91
    xlat_recovery_posn = 0;
nkeynes@596
    92
    uint8_t *eob = xlat_output + xlat_current_block->size;
nkeynes@588
    93
nkeynes@588
    94
    if( GET_ICACHE_END() < lastpc ) {
nkeynes@711
    95
        lastpc = GET_ICACHE_END();
nkeynes@588
    96
    }
nkeynes@588
    97
nkeynes@408
    98
    sh4_translate_begin_block(pc);
nkeynes@359
    99
nkeynes@408
   100
    do {
nkeynes@711
   101
        /* check for breakpoints at this pc */
nkeynes@711
   102
        for( i=0; i<sh4_breakpoint_count; i++ ) {
nkeynes@711
   103
            if( sh4_breakpoints[i].address == pc ) {
nkeynes@711
   104
                sh4_translate_emit_breakpoint(pc);
nkeynes@711
   105
                break;
nkeynes@711
   106
            }
nkeynes@711
   107
        }
nkeynes@711
   108
        if( eob - xlat_output < MAX_INSTRUCTION_SIZE ) {
nkeynes@711
   109
            uint8_t *oldstart = xlat_current_block->code;
nkeynes@711
   110
            xlat_current_block = xlat_extend_block( xlat_output - oldstart + MAX_INSTRUCTION_SIZE );
nkeynes@711
   111
            xlat_output = xlat_current_block->code + (xlat_output - oldstart);
nkeynes@711
   112
            eob = xlat_current_block->code + xlat_current_block->size;
nkeynes@711
   113
        }
nkeynes@711
   114
        done = sh4_translate_instruction( pc ); 
nkeynes@711
   115
        assert( xlat_output <= eob );
nkeynes@711
   116
        pc += 2;
nkeynes@711
   117
        if ( pc >= lastpc ) {
nkeynes@711
   118
            done = 2;
nkeynes@711
   119
        }
nkeynes@408
   120
    } while( !done );
nkeynes@408
   121
    pc += (done - 2);
nkeynes@617
   122
nkeynes@617
   123
    // Add end-of-block recovery for post-instruction checks
nkeynes@617
   124
    sh4_translate_add_recovery( (pc - start)>>1 ); 
nkeynes@617
   125
nkeynes@593
   126
    int epilogue_size = sh4_translate_end_block_size();
nkeynes@593
   127
    uint32_t recovery_size = sizeof(struct xlat_recovery_record)*xlat_recovery_posn;
nkeynes@711
   128
    uint32_t finalsize = (xlat_output - xlat_current_block->code) + epilogue_size + recovery_size;
nkeynes@711
   129
    if( xlat_current_block->size < finalsize ) {
nkeynes@711
   130
        uint8_t *oldstart = xlat_current_block->code;
nkeynes@711
   131
        xlat_current_block = xlat_extend_block( finalsize );
nkeynes@711
   132
        xlat_output = xlat_current_block->code + (xlat_output - oldstart);
nkeynes@410
   133
    }	
nkeynes@368
   134
    sh4_translate_end_block(pc);
nkeynes@711
   135
    assert( xlat_output <= (xlat_current_block->code + xlat_current_block->size - recovery_size) );
nkeynes@736
   136
nkeynes@586
   137
    /* Write the recovery records onto the end of the code block */
nkeynes@586
   138
    memcpy( xlat_output, xlat_recovery, recovery_size);
nkeynes@596
   139
    xlat_current_block->recover_table_offset = xlat_output - (uint8_t *)xlat_current_block->code;
nkeynes@596
   140
    xlat_current_block->recover_table_size = xlat_recovery_posn;
nkeynes@901
   141
    xlat_current_block->fpscr = sh4r.fpscr & (FPSCR_PR|FPSCR_SZ);
nkeynes@901
   142
    xlat_current_block->fpscr_mask = (FPSCR_PR|FPSCR_SZ);
nkeynes@586
   143
    xlat_commit_block( finalsize, pc-start );
nkeynes@596
   144
    return xlat_current_block->code;
nkeynes@359
   145
}
nkeynes@359
   146
nkeynes@398
   147
/**
nkeynes@586
   148
 * "Execute" the supplied recovery record. Currently this only updates
nkeynes@586
   149
 * sh4r.pc and sh4r.slice_cycle according to the currently executing
nkeynes@586
   150
 * instruction. In future this may be more sophisticated (ie will
nkeynes@586
   151
 * call into generated code).
nkeynes@398
   152
 */
nkeynes@586
   153
void sh4_translate_run_recovery( xlat_recovery_record_t recovery )
nkeynes@398
   154
{
nkeynes@586
   155
    sh4r.slice_cycle += (recovery->sh4_icount * sh4_cpu_period);
nkeynes@586
   156
    sh4r.pc += (recovery->sh4_icount<<1);
nkeynes@586
   157
}
nkeynes@359
   158
nkeynes@740
   159
void sh4_translate_exit_recover( )
nkeynes@586
   160
{
nkeynes@906
   161
    void *code = xlat_get_code_by_vma( sh4r.pc );
nkeynes@906
   162
    if( code != NULL ) {
nkeynes@906
   163
        uint32_t size = xlat_get_code_size( code );
nkeynes@906
   164
        void *pc = xlat_get_native_pc( code, size );
nkeynes@906
   165
        if( pc != NULL ) {
nkeynes@906
   166
            // could be null if we're not actually running inside the translator
nkeynes@906
   167
            xlat_recovery_record_t recover = xlat_get_post_recovery(code, pc, TRUE);
nkeynes@906
   168
            if( recover != NULL ) {
nkeynes@906
   169
                // Can be null if there is no recovery necessary
nkeynes@906
   170
                sh4_translate_run_recovery(recover);
nkeynes@906
   171
            }
nkeynes@736
   172
        }
nkeynes@398
   173
    }
nkeynes@586
   174
}
nkeynes@398
   175
nkeynes@905
   176
void FASTCALL sh4_translate_breakpoint_hit(uint32_t pc)
nkeynes@591
   177
{
nkeynes@591
   178
    if( sh4_starting && sh4r.slice_cycle == 0 && pc == sh4r.pc ) {
nkeynes@736
   179
        return;
nkeynes@591
   180
    }
nkeynes@740
   181
    sh4_core_exit( CORE_EXIT_BREAKPOINT );
nkeynes@591
   182
}
nkeynes@591
   183
nkeynes@586
   184
/**
nkeynes@586
   185
 * Exit the current block at the end of the current instruction, flush the
nkeynes@586
   186
 * translation cache (completely) and return control to sh4_xlat_run_slice.
nkeynes@586
   187
 *
nkeynes@586
   188
 * As a special case, if the current instruction is actually the last 
nkeynes@586
   189
 * instruction in the block (ie it's in a delay slot), this function 
nkeynes@586
   190
 * returns to allow normal completion of the translation block. Otherwise
nkeynes@586
   191
 * this function never returns.
nkeynes@586
   192
 *
nkeynes@586
   193
 * Must only be invoked (indirectly) from within translated code.
nkeynes@586
   194
 */
nkeynes@740
   195
gboolean sh4_translate_flush_cache()
nkeynes@586
   196
{
nkeynes@906
   197
    void *code = xlat_get_code_by_vma( sh4r.pc );
nkeynes@906
   198
    if( code != NULL ) {
nkeynes@906
   199
        uint32_t size = xlat_get_code_size( code );
nkeynes@906
   200
        void *pc = xlat_get_native_pc( code, size );
nkeynes@906
   201
        assert( pc != NULL );
nkeynes@586
   202
nkeynes@906
   203
        xlat_recovery_record_t recover = xlat_get_post_recovery(code, pc, FALSE);
nkeynes@906
   204
        if( recover != NULL ) {
nkeynes@906
   205
            // Can be null if there is no recovery necessary
nkeynes@906
   206
            sh4_translate_run_recovery(recover);
nkeynes@906
   207
            xlat_flush_cache();
nkeynes@906
   208
            return TRUE;
nkeynes@906
   209
        } else {
nkeynes@906
   210
            xlat_flush_cache();
nkeynes@906
   211
            return FALSE;
nkeynes@906
   212
        }
nkeynes@586
   213
    }
nkeynes@398
   214
}
nkeynes@586
   215
nkeynes@905
   216
void * FASTCALL xlat_get_code_by_vma( sh4vma_t vma )
nkeynes@586
   217
{
nkeynes@586
   218
    void *result = NULL;
nkeynes@586
   219
nkeynes@588
   220
    if( IS_IN_ICACHE(vma) ) {
nkeynes@736
   221
        return xlat_get_code( GET_ICACHE_PHYS(vma) );
nkeynes@586
   222
    }
nkeynes@586
   223
nkeynes@588
   224
    if( vma > 0xFFFFFF00 ) {
nkeynes@736
   225
        // lxdream hook
nkeynes@736
   226
        return NULL;
nkeynes@588
   227
    }
nkeynes@588
   228
nkeynes@588
   229
    if( !mmu_update_icache(vma) ) {
nkeynes@736
   230
        // fault - off to the fault handler
nkeynes@736
   231
        if( !mmu_update_icache(sh4r.pc) ) {
nkeynes@736
   232
            // double fault - halt
nkeynes@736
   233
            ERROR( "Double fault - halting" );
nkeynes@740
   234
            sh4_core_exit(CORE_EXIT_HALT);
nkeynes@736
   235
            return NULL;
nkeynes@736
   236
        }
nkeynes@588
   237
    }
nkeynes@588
   238
nkeynes@588
   239
    assert( IS_IN_ICACHE(sh4r.pc) );
nkeynes@588
   240
    result = xlat_get_code( GET_ICACHE_PHYS(sh4r.pc) );
nkeynes@586
   241
    return result;
nkeynes@586
   242
}
nkeynes@586
   243
.