Search
lxdream.org :: lxdream/src/sh4/xltcache.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/xltcache.c
changeset 359:c588dce7ebde
next366:6fb0d05152d7
author nkeynes
date Thu Aug 23 12:33:27 2007 +0000 (14 years ago)
permissions -rw-r--r--
last change Commit decoder generator
Translator work in progress
Fix mac.l, mac.w in emu core
view annotate diff log raw
     1 /**
     2  * $Id: xltcache.c,v 1.1 2007-08-23 12:33:27 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 */
    58 void xlat_cache_init() 
    59 {
    60     xlat_new_cache = mmap( NULL, XLAT_NEW_CACHE_SIZE, PROT_EXEC|PROT_READ|PROT_WRITE,
    61 			   MAP_PRIVATE|MAP_ANONYMOUS, -1, 0 );
    62     xlat_temp_cache = mmap( NULL, XLAT_TEMP_CACHE_SIZE, PROT_EXEC|PROT_READ|PROT_WRITE,
    63 			   MAP_PRIVATE|MAP_ANONYMOUS, -1, 0 );
    64     xlat_old_cache = mmap( NULL, XLAT_OLD_CACHE_SIZE, PROT_EXEC|PROT_READ|PROT_WRITE,
    65 			   MAP_PRIVATE|MAP_ANONYMOUS, -1, 0 );
    66     xlat_new_cache_ptr = xlat_new_cache;
    67     xlat_temp_cache_ptr = xlat_temp_cache;
    68     xlat_old_cache_ptr = xlat_old_cache;
    69     xlat_new_create_ptr = xlat_new_cache;
    71     xlat_lut = mmap( NULL, XLAT_LUT_PAGES*sizeof(void *), PROT_READ|PROT_WRITE,
    72 		     MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
    73     memset( xlat_lut, 0, XLAT_LUT_PAGES*sizeof(void *) );
    75     xlat_flush_cache();
    76 }
    78 /**
    79  * Reset the cache structure to its default state
    80  */
    81 void xlat_flush_cache() 
    82 {
    83     xlat_cache_block_t tmp;
    84     int i;
    85     xlat_new_cache_ptr = xlat_new_cache;
    86     xlat_new_cache_ptr->active = 0;
    87     xlat_new_cache_ptr->size = XLAT_NEW_CACHE_SIZE - 2*sizeof(struct xlat_cache_block);
    88     tmp = NEXT(xlat_new_cache_ptr);
    89     tmp->active = 1;
    90     tmp->size = 0;
    91     xlat_temp_cache_ptr = xlat_temp_cache;
    92     xlat_temp_cache_ptr->active = 0;
    93     xlat_temp_cache_ptr->size = XLAT_TEMP_CACHE_SIZE - 2*sizeof(struct xlat_cache_block);
    94     tmp = NEXT(xlat_temp_cache_ptr);
    95     tmp->active = 1;
    96     tmp->size = 0;
    97     xlat_old_cache_ptr = xlat_old_cache;
    98     xlat_old_cache_ptr->active = 0;
    99     xlat_old_cache_ptr->size = XLAT_OLD_CACHE_SIZE - 2*sizeof(struct xlat_cache_block);
   100     tmp = NEXT(xlat_old_cache_ptr);
   101     tmp->active = 1;
   102     tmp->size = 0;
   103     for( i=0; i<XLAT_LUT_PAGES; i++ ) {
   104 	if( xlat_lut[i] != NULL ) {
   105 	    memset( xlat_lut[i], 0, XLAT_LUT_PAGE_SIZE );
   106 	}
   107     }
   108 }
   110 void xlat_flush_page( sh4addr_t address )
   111 {
   112     int i;
   113     void **page = xlat_lut[XLAT_LUT_PAGE(address)];
   114     for( i=0; i<XLAT_LUT_PAGE_ENTRIES; i++ ) {
   115 	if( IS_ENTRY_POINT(page[i]) ) {
   116 	    BLOCK_FOR_CODE(page[i])->active = 0;
   117 	}
   118 	page[i] = NULL;
   119     }
   120 }
   122 void *xlat_get_code( sh4addr_t address )
   123 {
   124     void **page = xlat_lut[XLAT_LUT_PAGE(address)];
   125     if( page == NULL ) {
   126 	return NULL;
   127     }
   128     return page[XLAT_LUT_ENTRY(address)];
   129 }
   131 /**
   132  * Cut the specified block so that it has the given size, with the remaining data
   133  * forming a new free block. If the free block would be less than the minimum size,
   134  * the cut is not performed.
   135  * @return the next block after the (possibly cut) block.
   136  */
   137 static inline xlat_cache_block_t xlat_cut_block( xlat_cache_block_t block, int cutsize )
   138 {
   139     if( block->size > cutsize + MIN_TOTAL_SIZE ) {
   140 	int oldsize = block->size;
   141 	block->size = cutsize;
   142 	xlat_cache_block_t next = NEXT(block);
   143 	next->active = 0;
   144 	next->size = oldsize - cutsize - sizeof(struct xlat_cache_block);
   145 	return next;
   146     } else {
   147 	return NEXT(block);
   148     }
   149 }
   151 /**
   152  * Promote a block in temp space (or elsewhere for that matter) to old space.
   153  *
   154  * @param block to promote.
   155  */
   156 static void xlat_promote_to_old_space( xlat_cache_block_t block )
   157 {
   158     int allocation = -sizeof(struct xlat_cache_block);
   159     int size = block->size;
   160     xlat_cache_block_t curr = xlat_old_cache_ptr;
   161     xlat_cache_block_t start_block = curr;
   162     do {
   163 	allocation += curr->size + sizeof(struct xlat_cache_block);
   164 	curr = NEXT(curr);
   165 	if( allocation > size ) {
   166 	    break; /* done */
   167 	}
   168 	if( curr->size == 0 ) { /* End-of-cache Sentinel */
   169 	    /* Leave what we just released as free space and start again from the
   170 	     * top of the cache
   171 	     */
   172 	    start_block->active = 0;
   173 	    start_block->size = allocation;
   174 	    allocation = -sizeof(struct xlat_cache_block);
   175 	    start_block = curr = xlat_old_cache;
   176 	}
   177     } while(1);
   178     start_block->active = 1;
   179     start_block->size = allocation;
   180     start_block->lut_entry = block->lut_entry;
   181     *block->lut_entry = &start_block->code;
   182     memcpy( start_block->code, block->code, block->size );
   183     xlat_old_cache_ptr = xlat_cut_block(start_block, size );
   184     if( xlat_old_cache_ptr->size == 0 ) {
   185 	xlat_old_cache_ptr = xlat_old_cache;
   186     }
   187 }
   189 /**
   190  * Similarly to the above method, promotes a block to temp space.
   191  * TODO: Try to combine these - they're nearly identical
   192  */
   193 void xlat_promote_to_temp_space( xlat_cache_block_t block )
   194 {
   195     int size = block->size;
   196     int allocation = -sizeof(struct xlat_cache_block);
   197     xlat_cache_block_t curr = xlat_temp_cache_ptr;
   198     xlat_cache_block_t start_block = curr;
   199     do {
   200 	if( curr->active == BLOCK_USED ) {
   201 	    xlat_promote_to_old_space( curr );
   202 	}
   203 	allocation += curr->size + sizeof(struct xlat_cache_block);
   204 	curr = NEXT(curr);
   205 	if( allocation > size ) {
   206 	    break; /* done */
   207 	}
   208 	if( curr->size == 0 ) { /* End-of-cache Sentinel */
   209 	    /* Leave what we just released as free space and start again from the
   210 	     * top of the cache
   211 	     */
   212 	    start_block->active = 0;
   213 	    start_block->size = allocation;
   214 	    allocation = -sizeof(struct xlat_cache_block);
   215 	    start_block = curr = xlat_temp_cache;
   216 	}
   217     } while(1);
   218     start_block->active = 1;
   219     start_block->size = allocation;
   220     start_block->lut_entry = block->lut_entry;
   221     *block->lut_entry = &start_block->code;
   222     memcpy( start_block->code, block->code, block->size );
   223     xlat_temp_cache_ptr = xlat_cut_block(start_block, size );
   224     if( xlat_temp_cache_ptr->size == 0 ) {
   225 	xlat_temp_cache_ptr = xlat_temp_cache;
   226     }
   228 }
   230 /**
   231  * Returns the next block in the new cache list that can be written to by the
   232  * translator. If the next block is active, it is evicted first.
   233  */
   234 xlat_cache_block_t xlat_start_block( sh4addr_t address )
   235 {
   236     if( xlat_new_cache_ptr->size == 0 ) {
   237 	xlat_new_cache_ptr = xlat_new_cache;
   238     }
   240     if( xlat_new_cache_ptr->active ) {
   241 	xlat_promote_to_temp_space( xlat_new_cache_ptr );
   242     }
   243     xlat_new_create_ptr = xlat_new_cache_ptr;
   244     xlat_new_create_ptr->active = 1;
   245     xlat_new_cache_ptr = NEXT(xlat_new_cache_ptr);
   247     /* Add the LUT entry for the block */
   248     if( xlat_lut[XLAT_LUT_PAGE(address)] == NULL ) {
   249 	xlat_lut[XLAT_LUT_PAGE(address)] =
   250 	    mmap( NULL, XLAT_LUT_PAGE_SIZE, PROT_READ|PROT_WRITE,
   251 		  MAP_PRIVATE|MAP_ANONYMOUS, -1, 0 );
   252 	memset( xlat_lut[XLAT_LUT_PAGE(address)], 0, XLAT_LUT_PAGE_SIZE );
   253     }
   255     if( IS_ENTRY_POINT(xlat_lut[XLAT_LUT_PAGE(address)][XLAT_LUT_ENTRY(address)]) ) {
   256 	xlat_cache_block_t oldblock = BLOCK_FOR_CODE(xlat_lut[XLAT_LUT_PAGE(address)][XLAT_LUT_ENTRY(address)]);
   257 	oldblock->active = 0;
   258     }
   260     xlat_lut[XLAT_LUT_PAGE(address)][XLAT_LUT_ENTRY(address)] = 
   261 	&xlat_new_create_ptr->code;
   262     xlat_new_create_ptr->lut_entry = xlat_lut[XLAT_LUT_PAGE(address)] + XLAT_LUT_ENTRY(address);
   264     return xlat_new_create_ptr;
   265 }
   267 xlat_cache_block_t xlat_extend_block()
   268 {
   269     if( xlat_new_cache_ptr->size == 0 ) {
   270 	/* Migrate to the front of the cache to keep it contiguous */
   271 	xlat_new_create_ptr->active = 0;
   272 	char *olddata = xlat_new_create_ptr->code;
   273 	int oldsize = xlat_new_create_ptr->size;
   274 	int size = oldsize + MIN_BLOCK_SIZE; /* minimum expansion */
   275 	void **lut_entry = xlat_new_create_ptr->lut_entry;
   276 	int allocation = -sizeof(struct xlat_cache_block);
   277 	xlat_new_cache_ptr = xlat_new_cache;
   278 	do {
   279 	    if( xlat_new_cache_ptr->active ) {
   280 		xlat_promote_to_temp_space( xlat_new_cache_ptr );
   281 	    }
   282 	    allocation += xlat_new_cache_ptr->size + sizeof(struct xlat_cache_block);
   283 	    xlat_new_cache_ptr = NEXT(xlat_new_cache_ptr);
   284 	} while( allocation < size );
   285 	xlat_new_create_ptr = xlat_new_cache;
   286 	xlat_new_create_ptr->active = 1;
   287 	xlat_new_create_ptr->size = allocation;
   288 	xlat_new_create_ptr->lut_entry = lut_entry;
   289 	*lut_entry = &xlat_new_create_ptr->code;
   290 	memmove( xlat_new_create_ptr->code, olddata, oldsize );
   291     } else {
   292 	if( xlat_new_cache_ptr->active ) {
   293 	    xlat_promote_to_temp_space( xlat_new_cache_ptr );
   294 	}
   295 	xlat_new_create_ptr->size += xlat_new_cache_ptr->size + sizeof(struct xlat_cache_block);
   296 	xlat_new_cache_ptr = NEXT(xlat_new_cache_ptr);
   297     }
   298     return xlat_new_create_ptr;
   300 }
   302 void xlat_commit_block( uint32_t destsize, uint32_t srcsize )
   303 {
   304     void **ptr = xlat_new_create_ptr->lut_entry;
   305     void **endptr = ptr + (srcsize>>2);
   306     while( ptr < endptr ) {
   307 	if( *ptr == NULL ) {
   308 	    *ptr = XLAT_LUT_ENTRY_USED;
   309 	}
   310 	ptr++;
   311     }
   313     xlat_new_cache_ptr = xlat_cut_block( xlat_new_create_ptr, destsize );
   314 }
   316 void xlat_delete_block( xlat_cache_block_t block ) 
   317 {
   318     block->active = 0;
   319     *block->lut_entry = NULL;
   320 }
   322 void xlat_check_cache_integrity( xlat_cache_block_t cache, xlat_cache_block_t ptr, int size )
   323 {
   324     int foundptr = 0;
   325     xlat_cache_block_t tail = 
   326 	(xlat_cache_block_t)(((char *)cache) + size - sizeof(struct xlat_cache_block));
   328     assert( tail->active == 1 );
   329     assert( tail->size == 0 ); 
   330     while( cache < tail ) {
   331 	assert( cache->active >= 0 && cache->active <= 2 );
   332 	assert( cache->size >= 0 && cache->size < size );
   333 	if( cache == ptr ) {
   334 	    foundptr = 1;
   335 	}
   336 	cache = NEXT(cache);
   337     }
   338     assert( cache == tail );
   339     assert( foundptr == 1 );
   340 }
   342 void xlat_check_integrity( )
   343 {
   344     xlat_check_cache_integrity( xlat_new_cache, xlat_new_cache_ptr, XLAT_NEW_CACHE_SIZE );
   345     xlat_check_cache_integrity( xlat_temp_cache, xlat_temp_cache_ptr, XLAT_TEMP_CACHE_SIZE );
   346     xlat_check_cache_integrity( xlat_old_cache, xlat_old_cache_ptr, XLAT_OLD_CACHE_SIZE );
   347 }
.