Search
lxdream.org :: lxdream/src/sh4/xltcache.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/xltcache.c
changeset 376:8c7587af5a5d
prev366:6fb0d05152d7
next383:f597b73474cb
author nkeynes
date Wed Sep 12 09:20:38 2007 +0000 (16 years ago)
permissions -rw-r--r--
last change Start splitting the common SH4 parts into sh4.c, with sh4core.c to become
just the emulation core.
view annotate diff log raw
     1 /**
     2  * $Id: xltcache.c,v 1.3 2007-09-12 09:16:47 nkeynes Exp $
     3  * 
     4  * Translation cache management. This part is 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 "sh4/xltcache.h"
    20 #include "dreamcast.h"
    21 #include <sys/mman.h>
    22 #include <assert.h>
    24 #define XLAT_LUT_PAGE_BITS 12
    25 #define XLAT_LUT_TOTAL_BITS 28
    26 #define XLAT_LUT_PAGE(addr) (((addr)>>13) & 0xFFFF)
    27 #define XLAT_LUT_ENTRY(addr) (((addr)&0x1FFE) >> 1)
    29 #define XLAT_LUT_PAGES (1<<(XLAT_LUT_TOTAL_BITS-XLAT_LUT_PAGE_BITS))
    30 #define XLAT_LUT_PAGE_ENTRIES (1<<XLAT_LUT_PAGE_BITS)
    31 #define XLAT_LUT_PAGE_SIZE (XLAT_LUT_PAGE_ENTRIES * sizeof(void *))
    33 #define XLAT_LUT_ENTRY_EMPTY (void *)0
    34 #define XLAT_LUT_ENTRY_USED  (void *)1
    36 #define NEXT(block) ( (xlat_cache_block_t)&((block)->code[(block)->size]))
    37 #define BLOCK_FOR_CODE(code) (((xlat_cache_block_t)code)-1)
    38 #define IS_ENTRY_POINT(ent) (ent > XLAT_LUT_ENTRY_USED)
    39 #define IS_ENTRY_USED(ent) (ent != XLAT_LUT_ENTRY_EMPTY)
    41 #define MIN_BLOCK_SIZE 32
    42 #define MIN_TOTAL_SIZE (sizeof(struct xlat_cache_block)+MIN_BLOCK_SIZE)
    44 #define BLOCK_INACTIVE 0
    45 #define BLOCK_ACTIVE 1
    46 #define BLOCK_USED 2
    48 xlat_cache_block_t xlat_new_cache;
    49 xlat_cache_block_t xlat_new_cache_ptr;
    50 xlat_cache_block_t xlat_new_create_ptr;
    51 xlat_cache_block_t xlat_temp_cache;
    52 xlat_cache_block_t xlat_temp_cache_ptr;
    53 xlat_cache_block_t xlat_old_cache;
    54 xlat_cache_block_t xlat_old_cache_ptr;
    55 static void ***xlat_lut;
    56 static void **xlat_lut2; /* second-tier page info */
    57 static gboolean xlat_initialized = FALSE;
    59 void xlat_cache_init() 
    60 {
    61     if( !xlat_initialized ) {
    62 	xlat_initialized = TRUE;
    63 	xlat_new_cache = mmap( NULL, XLAT_NEW_CACHE_SIZE, PROT_EXEC|PROT_READ|PROT_WRITE,
    64 			       MAP_PRIVATE|MAP_ANONYMOUS, -1, 0 );
    65 	xlat_temp_cache = mmap( NULL, XLAT_TEMP_CACHE_SIZE, PROT_EXEC|PROT_READ|PROT_WRITE,
    66 				MAP_PRIVATE|MAP_ANONYMOUS, -1, 0 );
    67 	xlat_old_cache = mmap( NULL, XLAT_OLD_CACHE_SIZE, PROT_EXEC|PROT_READ|PROT_WRITE,
    68 			       MAP_PRIVATE|MAP_ANONYMOUS, -1, 0 );
    69 	xlat_new_cache_ptr = xlat_new_cache;
    70 	xlat_temp_cache_ptr = xlat_temp_cache;
    71 	xlat_old_cache_ptr = xlat_old_cache;
    72 	xlat_new_create_ptr = xlat_new_cache;
    74 	xlat_lut = mmap( NULL, XLAT_LUT_PAGES*sizeof(void *), PROT_READ|PROT_WRITE,
    75 			 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
    76 	memset( xlat_lut, 0, XLAT_LUT_PAGES*sizeof(void *) );
    77     }
    78     xlat_flush_cache();
    79 }
    81 /**
    82  * Reset the cache structure to its default state
    83  */
    84 void xlat_flush_cache() 
    85 {
    86     xlat_cache_block_t tmp;
    87     int i;
    88     xlat_new_cache_ptr = xlat_new_cache;
    89     xlat_new_cache_ptr->active = 0;
    90     xlat_new_cache_ptr->size = XLAT_NEW_CACHE_SIZE - 2*sizeof(struct xlat_cache_block);
    91     tmp = NEXT(xlat_new_cache_ptr);
    92     tmp->active = 1;
    93     tmp->size = 0;
    94     xlat_temp_cache_ptr = xlat_temp_cache;
    95     xlat_temp_cache_ptr->active = 0;
    96     xlat_temp_cache_ptr->size = XLAT_TEMP_CACHE_SIZE - 2*sizeof(struct xlat_cache_block);
    97     tmp = NEXT(xlat_temp_cache_ptr);
    98     tmp->active = 1;
    99     tmp->size = 0;
   100     xlat_old_cache_ptr = xlat_old_cache;
   101     xlat_old_cache_ptr->active = 0;
   102     xlat_old_cache_ptr->size = XLAT_OLD_CACHE_SIZE - 2*sizeof(struct xlat_cache_block);
   103     tmp = NEXT(xlat_old_cache_ptr);
   104     tmp->active = 1;
   105     tmp->size = 0;
   106     for( i=0; i<XLAT_LUT_PAGES; i++ ) {
   107 	if( xlat_lut[i] != NULL ) {
   108 	    memset( xlat_lut[i], 0, XLAT_LUT_PAGE_SIZE );
   109 	}
   110     }
   111 }
   113 void xlat_flush_page( sh4addr_t address )
   114 {
   115     int i;
   116     void **page = xlat_lut[XLAT_LUT_PAGE(address)];
   117     for( i=0; i<XLAT_LUT_PAGE_ENTRIES; i++ ) {
   118 	if( IS_ENTRY_POINT(page[i]) ) {
   119 	    BLOCK_FOR_CODE(page[i])->active = 0;
   120 	}
   121 	page[i] = NULL;
   122     }
   123 }
   125 void *xlat_get_code( sh4addr_t address )
   126 {
   127     void **page = xlat_lut[XLAT_LUT_PAGE(address)];
   128     if( page == NULL ) {
   129 	return NULL;
   130     }
   131     return page[XLAT_LUT_ENTRY(address)];
   132 }
   134 uint32_t xlat_get_block_size( void *block )
   135 {
   136     xlat_cache_block_t xlt = (xlat_cache_block_t)(((char *)block)-sizeof(struct xlat_cache_block));
   137     return xlt->size;
   138 }
   140 /**
   141  * Cut the specified block so that it has the given size, with the remaining data
   142  * forming a new free block. If the free block would be less than the minimum size,
   143  * the cut is not performed.
   144  * @return the next block after the (possibly cut) block.
   145  */
   146 static inline xlat_cache_block_t xlat_cut_block( xlat_cache_block_t block, int cutsize )
   147 {
   148     if( block->size > cutsize + MIN_TOTAL_SIZE ) {
   149 	int oldsize = block->size;
   150 	block->size = cutsize;
   151 	xlat_cache_block_t next = NEXT(block);
   152 	next->active = 0;
   153 	next->size = oldsize - cutsize - sizeof(struct xlat_cache_block);
   154 	return next;
   155     } else {
   156 	return NEXT(block);
   157     }
   158 }
   160 /**
   161  * Promote a block in temp space (or elsewhere for that matter) to old space.
   162  *
   163  * @param block to promote.
   164  */
   165 static void xlat_promote_to_old_space( xlat_cache_block_t block )
   166 {
   167     int allocation = -sizeof(struct xlat_cache_block);
   168     int size = block->size;
   169     xlat_cache_block_t curr = xlat_old_cache_ptr;
   170     xlat_cache_block_t start_block = curr;
   171     do {
   172 	allocation += curr->size + sizeof(struct xlat_cache_block);
   173 	curr = NEXT(curr);
   174 	if( allocation > size ) {
   175 	    break; /* done */
   176 	}
   177 	if( curr->size == 0 ) { /* End-of-cache Sentinel */
   178 	    /* Leave what we just released as free space and start again from the
   179 	     * top of the cache
   180 	     */
   181 	    start_block->active = 0;
   182 	    start_block->size = allocation;
   183 	    allocation = -sizeof(struct xlat_cache_block);
   184 	    start_block = curr = xlat_old_cache;
   185 	}
   186     } while(1);
   187     start_block->active = 1;
   188     start_block->size = allocation;
   189     start_block->lut_entry = block->lut_entry;
   190     *block->lut_entry = &start_block->code;
   191     memcpy( start_block->code, block->code, block->size );
   192     xlat_old_cache_ptr = xlat_cut_block(start_block, size );
   193     if( xlat_old_cache_ptr->size == 0 ) {
   194 	xlat_old_cache_ptr = xlat_old_cache;
   195     }
   196 }
   198 /**
   199  * Similarly to the above method, promotes a block to temp space.
   200  * TODO: Try to combine these - they're nearly identical
   201  */
   202 void xlat_promote_to_temp_space( xlat_cache_block_t block )
   203 {
   204     int size = block->size;
   205     int allocation = -sizeof(struct xlat_cache_block);
   206     xlat_cache_block_t curr = xlat_temp_cache_ptr;
   207     xlat_cache_block_t start_block = curr;
   208     do {
   209 	if( curr->active == BLOCK_USED ) {
   210 	    xlat_promote_to_old_space( curr );
   211 	}
   212 	allocation += curr->size + sizeof(struct xlat_cache_block);
   213 	curr = NEXT(curr);
   214 	if( allocation > size ) {
   215 	    break; /* done */
   216 	}
   217 	if( curr->size == 0 ) { /* End-of-cache Sentinel */
   218 	    /* Leave what we just released as free space and start again from the
   219 	     * top of the cache
   220 	     */
   221 	    start_block->active = 0;
   222 	    start_block->size = allocation;
   223 	    allocation = -sizeof(struct xlat_cache_block);
   224 	    start_block = curr = xlat_temp_cache;
   225 	}
   226     } while(1);
   227     start_block->active = 1;
   228     start_block->size = allocation;
   229     start_block->lut_entry = block->lut_entry;
   230     *block->lut_entry = &start_block->code;
   231     memcpy( start_block->code, block->code, block->size );
   232     xlat_temp_cache_ptr = xlat_cut_block(start_block, size );
   233     if( xlat_temp_cache_ptr->size == 0 ) {
   234 	xlat_temp_cache_ptr = xlat_temp_cache;
   235     }
   237 }
   239 /**
   240  * Returns the next block in the new cache list that can be written to by the
   241  * translator. If the next block is active, it is evicted first.
   242  */
   243 xlat_cache_block_t xlat_start_block( sh4addr_t address )
   244 {
   245     if( xlat_new_cache_ptr->size == 0 ) {
   246 	xlat_new_cache_ptr = xlat_new_cache;
   247     }
   249     if( xlat_new_cache_ptr->active ) {
   250 	xlat_promote_to_temp_space( xlat_new_cache_ptr );
   251     }
   252     xlat_new_create_ptr = xlat_new_cache_ptr;
   253     xlat_new_create_ptr->active = 1;
   254     xlat_new_cache_ptr = NEXT(xlat_new_cache_ptr);
   256     /* Add the LUT entry for the block */
   257     if( xlat_lut[XLAT_LUT_PAGE(address)] == NULL ) {
   258 	xlat_lut[XLAT_LUT_PAGE(address)] =
   259 	    mmap( NULL, XLAT_LUT_PAGE_SIZE, PROT_READ|PROT_WRITE,
   260 		  MAP_PRIVATE|MAP_ANONYMOUS, -1, 0 );
   261 	memset( xlat_lut[XLAT_LUT_PAGE(address)], 0, XLAT_LUT_PAGE_SIZE );
   262     }
   264     if( IS_ENTRY_POINT(xlat_lut[XLAT_LUT_PAGE(address)][XLAT_LUT_ENTRY(address)]) ) {
   265 	xlat_cache_block_t oldblock = BLOCK_FOR_CODE(xlat_lut[XLAT_LUT_PAGE(address)][XLAT_LUT_ENTRY(address)]);
   266 	oldblock->active = 0;
   267     }
   269     xlat_lut[XLAT_LUT_PAGE(address)][XLAT_LUT_ENTRY(address)] = 
   270 	&xlat_new_create_ptr->code;
   271     xlat_new_create_ptr->lut_entry = xlat_lut[XLAT_LUT_PAGE(address)] + XLAT_LUT_ENTRY(address);
   273     return xlat_new_create_ptr;
   274 }
   276 xlat_cache_block_t xlat_extend_block()
   277 {
   278     if( xlat_new_cache_ptr->size == 0 ) {
   279 	/* Migrate to the front of the cache to keep it contiguous */
   280 	xlat_new_create_ptr->active = 0;
   281 	char *olddata = xlat_new_create_ptr->code;
   282 	int oldsize = xlat_new_create_ptr->size;
   283 	int size = oldsize + MIN_BLOCK_SIZE; /* minimum expansion */
   284 	void **lut_entry = xlat_new_create_ptr->lut_entry;
   285 	int allocation = -sizeof(struct xlat_cache_block);
   286 	xlat_new_cache_ptr = xlat_new_cache;
   287 	do {
   288 	    if( xlat_new_cache_ptr->active ) {
   289 		xlat_promote_to_temp_space( xlat_new_cache_ptr );
   290 	    }
   291 	    allocation += xlat_new_cache_ptr->size + sizeof(struct xlat_cache_block);
   292 	    xlat_new_cache_ptr = NEXT(xlat_new_cache_ptr);
   293 	} while( allocation < size );
   294 	xlat_new_create_ptr = xlat_new_cache;
   295 	xlat_new_create_ptr->active = 1;
   296 	xlat_new_create_ptr->size = allocation;
   297 	xlat_new_create_ptr->lut_entry = lut_entry;
   298 	*lut_entry = &xlat_new_create_ptr->code;
   299 	memmove( xlat_new_create_ptr->code, olddata, oldsize );
   300     } else {
   301 	if( xlat_new_cache_ptr->active ) {
   302 	    xlat_promote_to_temp_space( xlat_new_cache_ptr );
   303 	}
   304 	xlat_new_create_ptr->size += xlat_new_cache_ptr->size + sizeof(struct xlat_cache_block);
   305 	xlat_new_cache_ptr = NEXT(xlat_new_cache_ptr);
   306     }
   307     return xlat_new_create_ptr;
   309 }
   311 void xlat_commit_block( uint32_t destsize, uint32_t srcsize )
   312 {
   313     void **ptr = xlat_new_create_ptr->lut_entry;
   314     void **endptr = ptr + (srcsize>>2);
   315     while( ptr < endptr ) {
   316 	if( *ptr == NULL ) {
   317 	    *ptr = XLAT_LUT_ENTRY_USED;
   318 	}
   319 	ptr++;
   320     }
   322     xlat_new_cache_ptr = xlat_cut_block( xlat_new_create_ptr, destsize );
   323 }
   325 void xlat_delete_block( xlat_cache_block_t block ) 
   326 {
   327     block->active = 0;
   328     *block->lut_entry = NULL;
   329 }
   331 void xlat_check_cache_integrity( xlat_cache_block_t cache, xlat_cache_block_t ptr, int size )
   332 {
   333     int foundptr = 0;
   334     xlat_cache_block_t tail = 
   335 	(xlat_cache_block_t)(((char *)cache) + size - sizeof(struct xlat_cache_block));
   337     assert( tail->active == 1 );
   338     assert( tail->size == 0 ); 
   339     while( cache < tail ) {
   340 	assert( cache->active >= 0 && cache->active <= 2 );
   341 	assert( cache->size >= 0 && cache->size < size );
   342 	if( cache == ptr ) {
   343 	    foundptr = 1;
   344 	}
   345 	cache = NEXT(cache);
   346     }
   347     assert( cache == tail );
   348     assert( foundptr == 1 );
   349 }
   351 void xlat_check_integrity( )
   352 {
   353     xlat_check_cache_integrity( xlat_new_cache, xlat_new_cache_ptr, XLAT_NEW_CACHE_SIZE );
   354     xlat_check_cache_integrity( xlat_temp_cache, xlat_temp_cache_ptr, XLAT_TEMP_CACHE_SIZE );
   355     xlat_check_cache_integrity( xlat_old_cache, xlat_old_cache_ptr, XLAT_OLD_CACHE_SIZE );
   356 }
   359 void xlat_disasm_block( FILE *out, void *block )
   360 {
   361     uint32_t buflen = xlat_get_block_size(block);
   362     x86_set_symtab( NULL, 0 );
   363     x86_disasm_block( out, block, buflen );
   364 }
.