Search
lxdream.org :: lxdream/src/xlat/xltcache.h
lxdream 0.9.1
released Jun 29
Download Now
filename src/xlat/xltcache.h
changeset 1263:b3de98d19faf
prev1214:49152b3d8b75
author nkeynes
date Tue Mar 06 09:04:34 2012 +1000 (12 years ago)
permissions -rw-r--r--
last change Break host disassembly bits out of sh4x86.in, and move the generic disasm
bits from x86dasm to xlat.
view annotate diff log raw
     1 /**
     2  * $Id$
     3  * 
     4  * Translation cache support (architecture independent)
     5  *
     6  * Copyright (c) 2005 Nathan Keynes.
     7  *
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU General Public License as published by
    10  * the Free Software Foundation; either version 2 of the License, or
    11  * (at your option) any later version.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  */
    19 #include "dream.h"
    20 #include "mem.h"
    22 #ifndef lxdream_xltcache_H
    23 #define lxdream_xltcache_H 1
    25 /**
    26  * For now, recovery is purely a matter of mapping native pc => sh4 pc,
    27  * and updating sh4r.pc & sh4r.slice_cycles accordingly. In future more
    28  * detailed recovery may be required if the translator optimizes more
    29  * agressively.
    30  *
    31  * The recovery table contains (at least) one entry per abortable instruction,
    32  * 
    33  */
    34 typedef struct xlat_recovery_record {
    35     uint32_t xlat_offset;    // native (translated) pc 
    36     uint32_t sh4_icount;     // instruction number of the corresponding SH4 instruction
    37                              // (0 = first instruction, 1 = second instruction, ... )
    38 } *xlat_recovery_record_t;
    40 struct xlat_cache_block {
    41     int active;  /* 0 = deleted, 1 = normal. 2 = accessed (temp-space only) */
    42     uint32_t size;
    43     void **lut_entry; /* For deletion */
    44     void *chain;
    45     void *use_list;
    46     uint32_t xlat_sh4_mode; /* comparison with sh4r.xlat_sh4_mode */
    47     uint32_t recover_table_offset; // Offset from code[0] of the recovery table;
    48     uint32_t recover_table_size;
    49     unsigned char code[0];
    50 } __attribute__((packed));
    52 typedef struct xlat_target_fns {
    53     void (*unlink_block)(void *use_list);
    54 } *xlat_target_fns_t;
    56 typedef struct xlat_cache_block *xlat_cache_block_t;
    58 #define XLAT_BLOCK_FOR_CODE(code) (((xlat_cache_block_t)code)-1)
    60 #define XLAT_BLOCK_MODE(code) (XLAT_BLOCK_FOR_CODE(code)->xlat_sh4_mode)
    61 #define XLAT_BLOCK_CHAIN(code) (XLAT_BLOCK_FOR_CODE(code)->chain)
    62 #define XLAT_RECOVERY_TABLE(code) ((xlat_recovery_record_t)(((char *)code) + XLAT_BLOCK_FOR_CODE(code)->recover_table_offset))
    64 /**
    65  * Initialize the translation cache
    66  */
    67 void xlat_cache_init(void);
    69 /**
    70  * Setup target support.
    71  */
    72 void xlat_set_target_fns( xlat_target_fns_t target_fns );
    74 /**
    75  * Returns the next block in the new cache list that can be written to by the
    76  * translator.
    77  */
    78 xlat_cache_block_t xlat_start_block(sh4addr_t address);
    80 /**
    81  * Increases the current block size (only valid between calls to xlat_start_block()
    82  * and xlat_commit_block()). 
    83  * @return the new block, which may be different from the old block.
    84  */
    85 xlat_cache_block_t xlat_extend_block( uint32_t newSize );
    87 /**
    88  * Commit the current translation block
    89  * @param destsize final size of the translation in bytes.
    90  * @param startpc PC at the start of the translation block.
    91  * @param endpc PC at the end of the translation block (i.e. the address of the
    92  *  next instruction after the block).
    93  */
    94 void xlat_commit_block( uint32_t destsize, sh4addr_t startpc, sh4addr_t endpc );
    96 /**
    97  * Delete (deactivate) the specified block from the cache. Caller is responsible
    98  * for ensuring that there really is a block there.
    99  */
   100 void xlat_delete_block( xlat_cache_block_t block );
   102 /**
   103  * Retrieve the entry point for the translated code corresponding to the given
   104  * SH4 address, or NULL if there is no code for that address.
   105  */
   106 void * FASTCALL xlat_get_code( sh4addr_t address );
   108 /**
   109  * Retrieve the pre-instruction recovery record corresponding to the given
   110  * native address, or NULL if there is no recovery code for the address.
   111  * @param code The code block containing the recovery table.
   112  * @param native_pc A pointer that must be within the currently executing 
   113  * return the first record before or equal to the given pc.
   114  * translation block.
   115  */
   116 struct xlat_recovery_record *xlat_get_pre_recovery( void *code, void *native_pc );
   119 /**
   120  * Retrieve the entry point for the translated code corresponding to the given
   121  * SH4 virtual address, or NULL if there is no code for the address. 
   122  * If the virtual address cannot be resolved, this method will raise a TLB miss 
   123  * exception, and return NULL.
   124  */
   125 void * FASTCALL xlat_get_code_by_vma( sh4vma_t address );
   127 /**
   128  * Retrieve the address of the lookup table entry corresponding to the
   129  * given SH4 address.
   130  */
   131 void ** FASTCALL xlat_get_lut_entry( sh4addr_t address );
   133 /**
   134  * Retrieve the current host address of the running translated code block.
   135  * @return the host PC, or null if there is no currently executing translated
   136  * block (or the stack is corrupted)
   137  * Note: the implementation of this method is host (and calling-convention) specific.
   138  * @param block_start start of the block the PC should be in
   139  * @param block_size size of the code block in bytes.
   140  */
   141 void *xlat_get_native_pc( void *block_start, uint32_t block_size );
   143 /**
   144  * Retrieve the size of the block starting at the specified pointer. If the
   145  * pointer is not a valid code block, the return value is undefined.
   146  */
   147 uint32_t FASTCALL xlat_get_block_size( void *ptr );
   149 /**
   150  * Retrieve the size of the code in the block starting at the specified 
   151  * pointer. Effectively this is xlat_get_block_size() minus the size of
   152  * the recovery table. If the pointer is not a valid code block, the 
   153  * return value is undefined.
   154  */
   155 uint32_t FASTCALL xlat_get_code_size( void *ptr );
   157 /**
   158  * Flush the code cache for the page containing the given address
   159  */
   160 void FASTCALL xlat_flush_page( sh4addr_t address );
   162 void FASTCALL xlat_invalidate_word( sh4addr_t address );
   163 void FASTCALL xlat_invalidate_long( sh4addr_t address );
   166 /**
   167  * Invalidate the code cache for a memory region
   168  */
   169 void FASTCALL xlat_invalidate_block( sh4addr_t address, size_t bytes );
   171 /**
   172  * Flush the entire code cache. This isn't as cheap as one might like
   173  */
   174 void xlat_flush_cache();
   176 /**
   177  * Test if the given pointer is within the translation cache, and (is likely)
   178  * the start of a code block
   179  */
   180 gboolean xlat_is_code_pointer( void *p );
   182 /**
   183  * Check the internal integrity of the cache
   184  */
   185 void xlat_check_integrity();
   187 /**
   188  * Short record with block + pc, used for activity dumps
   189  */
   190 typedef struct xlat_block_ref {
   191     xlat_cache_block_t block;
   192     uint32_t pc;
   193 } *xlat_block_ref_t;
   195 /**
   196  * Fetch the top numRecords translated blocks by number of executions (requires block
   197  * profiling to be turned on in order to give meaningful results).
   198  * @param topN Number of blocks to print.
   199  * @return the number of records retrieved
   200  */
   201 unsigned int xlat_get_cache_blocks_by_activity( xlat_block_ref_t records, size_t numRecords );
   203 void xlat_dump_cache_by_activity( unsigned int topN );
   205 #endif /* lxdream_xltcache_H */
.