Search
lxdream.org :: lxdream/src/sh4/xltcache.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/xltcache.c
changeset 400:049d72a7a229
prev383:f597b73474cb
next407:d24ab36150c4
author nkeynes
date Fri Sep 28 07:25:22 2007 +0000 (16 years ago)
permissions -rw-r--r--
last change Remove MMU check (probably shouldn't be here anyway), and disable
TRACE_IO checks by default
view annotate diff log raw
     1 /**
     2  * $Id: xltcache.c,v 1.5 2007-09-20 08:35:04 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 void xlat_print_free( FILE *out )
    82 {
    83     fprintf( out, "New space: %d\nTemp space: %d\nOld space: %d\n", 
    84 	     xlat_new_cache_ptr->size, xlat_temp_cache_ptr->size, xlat_old_cache_ptr->size );
    85 }
    87 /**
    88  * Reset the cache structure to its default state
    89  */
    90 void xlat_flush_cache() 
    91 {
    92     xlat_cache_block_t tmp;
    93     int i;
    94     xlat_new_cache_ptr = xlat_new_cache;
    95     xlat_new_cache_ptr->active = 0;
    96     xlat_new_cache_ptr->size = XLAT_NEW_CACHE_SIZE - 2*sizeof(struct xlat_cache_block);
    97     tmp = NEXT(xlat_new_cache_ptr);
    98     tmp->active = 1;
    99     tmp->size = 0;
   100     xlat_temp_cache_ptr = xlat_temp_cache;
   101     xlat_temp_cache_ptr->active = 0;
   102     xlat_temp_cache_ptr->size = XLAT_TEMP_CACHE_SIZE - 2*sizeof(struct xlat_cache_block);
   103     tmp = NEXT(xlat_temp_cache_ptr);
   104     tmp->active = 1;
   105     tmp->size = 0;
   106     xlat_old_cache_ptr = xlat_old_cache;
   107     xlat_old_cache_ptr->active = 0;
   108     xlat_old_cache_ptr->size = XLAT_OLD_CACHE_SIZE - 2*sizeof(struct xlat_cache_block);
   109     tmp = NEXT(xlat_old_cache_ptr);
   110     tmp->active = 1;
   111     tmp->size = 0;
   112     for( i=0; i<XLAT_LUT_PAGES; i++ ) {
   113 	if( xlat_lut[i] != NULL ) {
   114 	    memset( xlat_lut[i], 0, XLAT_LUT_PAGE_SIZE );
   115 	}
   116     }
   117 }
   119 static void xlat_flush_page_by_lut( void **page )
   120 {
   121     int i;
   122     for( i=0; i<XLAT_LUT_PAGE_ENTRIES; i++ ) {
   123 	if( IS_ENTRY_POINT(page[i]) ) {
   124 	    BLOCK_FOR_CODE(page[i])->active = 0;
   125 	}
   126 	page[i] = NULL;
   127     }
   128 }
   130 void xlat_invalidate_word( sh4addr_t addr )
   131 {
   132     if( xlat_lut ) {
   133 	void **page = xlat_lut[XLAT_LUT_PAGE(addr)];
   134 	if( page != NULL ) {
   135 	    int entry = XLAT_LUT_ENTRY(addr);
   136 	    if( page[entry] != NULL ) {
   137 		xlat_flush_page_by_lut(page);
   138 	    }
   139 	}
   140     }
   141 }
   143 void xlat_invalidate_long( sh4addr_t addr )
   144 {
   145     if( xlat_lut ) {
   146 	void **page = xlat_lut[XLAT_LUT_PAGE(addr)];
   147 	if( page != NULL ) {
   148 	    int entry = XLAT_LUT_ENTRY(addr);
   149 	    if( page[entry] != NULL || page[entry+1] != NULL ) {
   150 		xlat_flush_page_by_lut(page);
   151 	    }
   152 	}
   153     }
   154 }
   156 void xlat_invalidate_block( sh4addr_t address, size_t size )
   157 {
   158     int i;
   159     int entry_count = size >> 1; // words;
   160     uint32_t page_no = XLAT_LUT_PAGE(address);
   161     int entry = XLAT_LUT_ENTRY(address);
   162     if( xlat_lut ) {
   163 	do {
   164 	    void **page = xlat_lut[page_no];
   165 	    int page_entries = XLAT_LUT_PAGE_ENTRIES - entry;
   166 	    if( entry_count < page_entries ) {
   167 		page_entries = entry_count;
   168 	    }
   169 	    if( page != NULL ) {
   170 		if( page_entries == XLAT_LUT_PAGE_ENTRIES ) {
   171 		    /* Overwriting the entire page anyway */
   172 		    xlat_flush_page_by_lut(page);
   173 		} else {
   174 		    for( i=entry; i<entry+page_entries; i++ ) {
   175 			if( page[i] != NULL ) {
   176 			    xlat_flush_page_by_lut(page);
   177 			    break;
   178 			}
   179 		    }
   180 		}
   181 		entry_count -= page_entries;
   182 	    }
   183 	    page_no ++;
   184 	    entry_count -= page_entries;
   185 	    entry = 0;
   186 	} while( entry_count > 0 );
   187     }
   188 }
   190 void xlat_flush_page( sh4addr_t address )
   191 {
   192     void **page = xlat_lut[XLAT_LUT_PAGE(address)];
   193     if( page != NULL ) {
   194 	xlat_flush_page_by_lut(page);
   195     }
   196 }
   198 void *xlat_get_code( sh4addr_t address )
   199 {
   200     void **page = xlat_lut[XLAT_LUT_PAGE(address)];
   201     if( page == NULL ) {
   202 	return NULL;
   203     }
   204     void *result = page[XLAT_LUT_ENTRY(address)];
   205     if( result == ((void *)(1)) ) {
   206 	return NULL;
   207     } else {
   208 	return result;
   209     }
   210 }
   212 uint32_t xlat_get_block_size( void *block )
   213 {
   214     xlat_cache_block_t xlt = (xlat_cache_block_t)(((char *)block)-sizeof(struct xlat_cache_block));
   215     return xlt->size;
   216 }
   218 /**
   219  * Cut the specified block so that it has the given size, with the remaining data
   220  * forming a new free block. If the free block would be less than the minimum size,
   221  * the cut is not performed.
   222  * @return the next block after the (possibly cut) block.
   223  */
   224 static inline xlat_cache_block_t xlat_cut_block( xlat_cache_block_t block, int cutsize )
   225 {
   226     if( block->size > cutsize + MIN_TOTAL_SIZE ) {
   227 	int oldsize = block->size;
   228 	block->size = cutsize;
   229 	xlat_cache_block_t next = NEXT(block);
   230 	next->active = 0;
   231 	next->size = oldsize - cutsize - sizeof(struct xlat_cache_block);
   232 	return next;
   233     } else {
   234 	return NEXT(block);
   235     }
   236 }
   238 /**
   239  * Promote a block in temp space (or elsewhere for that matter) to old space.
   240  *
   241  * @param block to promote.
   242  */
   243 static void xlat_promote_to_old_space( xlat_cache_block_t block )
   244 {
   245     int allocation = -sizeof(struct xlat_cache_block);
   246     int size = block->size;
   247     xlat_cache_block_t curr = xlat_old_cache_ptr;
   248     xlat_cache_block_t start_block = curr;
   249     do {
   250 	allocation += curr->size + sizeof(struct xlat_cache_block);
   251 	curr = NEXT(curr);
   252 	if( allocation > size ) {
   253 	    break; /* done */
   254 	}
   255 	if( curr->size == 0 ) { /* End-of-cache Sentinel */
   256 	    /* Leave what we just released as free space and start again from the
   257 	     * top of the cache
   258 	     */
   259 	    start_block->active = 0;
   260 	    start_block->size = allocation;
   261 	    allocation = -sizeof(struct xlat_cache_block);
   262 	    start_block = curr = xlat_old_cache;
   263 	}
   264     } while(1);
   265     start_block->active = 1;
   266     start_block->size = allocation;
   267     start_block->lut_entry = block->lut_entry;
   268     *block->lut_entry = &start_block->code;
   269     memcpy( start_block->code, block->code, block->size );
   270     xlat_old_cache_ptr = xlat_cut_block(start_block, size );
   271     if( xlat_old_cache_ptr->size == 0 ) {
   272 	xlat_old_cache_ptr = xlat_old_cache;
   273     }
   274 }
   276 /**
   277  * Similarly to the above method, promotes a block to temp space.
   278  * TODO: Try to combine these - they're nearly identical
   279  */
   280 void xlat_promote_to_temp_space( xlat_cache_block_t block )
   281 {
   282     int size = block->size;
   283     int allocation = -sizeof(struct xlat_cache_block);
   284     xlat_cache_block_t curr = xlat_temp_cache_ptr;
   285     xlat_cache_block_t start_block = curr;
   286     do {
   287 	if( curr->active == BLOCK_USED ) {
   288 	    xlat_promote_to_old_space( curr );
   289 	}
   290 	allocation += curr->size + sizeof(struct xlat_cache_block);
   291 	curr = NEXT(curr);
   292 	if( allocation > size ) {
   293 	    break; /* done */
   294 	}
   295 	if( curr->size == 0 ) { /* End-of-cache Sentinel */
   296 	    /* Leave what we just released as free space and start again from the
   297 	     * top of the cache
   298 	     */
   299 	    start_block->active = 0;
   300 	    start_block->size = allocation;
   301 	    allocation = -sizeof(struct xlat_cache_block);
   302 	    start_block = curr = xlat_temp_cache;
   303 	}
   304     } while(1);
   305     start_block->active = 1;
   306     start_block->size = allocation;
   307     start_block->lut_entry = block->lut_entry;
   308     *block->lut_entry = &start_block->code;
   309     memcpy( start_block->code, block->code, block->size );
   310     xlat_temp_cache_ptr = xlat_cut_block(start_block, size );
   311     if( xlat_temp_cache_ptr->size == 0 ) {
   312 	xlat_temp_cache_ptr = xlat_temp_cache;
   313     }
   315 }
   317 /**
   318  * Returns the next block in the new cache list that can be written to by the
   319  * translator. If the next block is active, it is evicted first.
   320  */
   321 xlat_cache_block_t xlat_start_block( sh4addr_t address )
   322 {
   323     if( xlat_new_cache_ptr->size == 0 ) {
   324 	xlat_new_cache_ptr = xlat_new_cache;
   325     }
   327     if( xlat_new_cache_ptr->active ) {
   328 	xlat_promote_to_temp_space( xlat_new_cache_ptr );
   329     }
   330     xlat_new_create_ptr = xlat_new_cache_ptr;
   331     xlat_new_create_ptr->active = 1;
   332     xlat_new_cache_ptr = NEXT(xlat_new_cache_ptr);
   334     /* Add the LUT entry for the block */
   335     if( xlat_lut[XLAT_LUT_PAGE(address)] == NULL ) {
   336 	xlat_lut[XLAT_LUT_PAGE(address)] =
   337 	    mmap( NULL, XLAT_LUT_PAGE_SIZE, PROT_READ|PROT_WRITE,
   338 		  MAP_PRIVATE|MAP_ANONYMOUS, -1, 0 );
   339 	memset( xlat_lut[XLAT_LUT_PAGE(address)], 0, XLAT_LUT_PAGE_SIZE );
   340     }
   342     if( IS_ENTRY_POINT(xlat_lut[XLAT_LUT_PAGE(address)][XLAT_LUT_ENTRY(address)]) ) {
   343 	xlat_cache_block_t oldblock = BLOCK_FOR_CODE(xlat_lut[XLAT_LUT_PAGE(address)][XLAT_LUT_ENTRY(address)]);
   344 	oldblock->active = 0;
   345     }
   347     xlat_lut[XLAT_LUT_PAGE(address)][XLAT_LUT_ENTRY(address)] = 
   348 	&xlat_new_create_ptr->code;
   349     xlat_new_create_ptr->lut_entry = xlat_lut[XLAT_LUT_PAGE(address)] + XLAT_LUT_ENTRY(address);
   351     return xlat_new_create_ptr;
   352 }
   354 xlat_cache_block_t xlat_extend_block()
   355 {
   356     if( xlat_new_cache_ptr->size == 0 ) {
   357 	/* Migrate to the front of the cache to keep it contiguous */
   358 	xlat_new_create_ptr->active = 0;
   359 	char *olddata = xlat_new_create_ptr->code;
   360 	int oldsize = xlat_new_create_ptr->size;
   361 	int size = oldsize + MIN_BLOCK_SIZE; /* minimum expansion */
   362 	void **lut_entry = xlat_new_create_ptr->lut_entry;
   363 	int allocation = -sizeof(struct xlat_cache_block);
   364 	xlat_new_cache_ptr = xlat_new_cache;
   365 	do {
   366 	    if( xlat_new_cache_ptr->active ) {
   367 		xlat_promote_to_temp_space( xlat_new_cache_ptr );
   368 	    }
   369 	    allocation += xlat_new_cache_ptr->size + sizeof(struct xlat_cache_block);
   370 	    xlat_new_cache_ptr = NEXT(xlat_new_cache_ptr);
   371 	} while( allocation < size );
   372 	xlat_new_create_ptr = xlat_new_cache;
   373 	xlat_new_create_ptr->active = 1;
   374 	xlat_new_create_ptr->size = allocation;
   375 	xlat_new_create_ptr->lut_entry = lut_entry;
   376 	*lut_entry = &xlat_new_create_ptr->code;
   377 	memmove( xlat_new_create_ptr->code, olddata, oldsize );
   378     } else {
   379 	if( xlat_new_cache_ptr->active ) {
   380 	    xlat_promote_to_temp_space( xlat_new_cache_ptr );
   381 	}
   382 	xlat_new_create_ptr->size += xlat_new_cache_ptr->size + sizeof(struct xlat_cache_block);
   383 	xlat_new_cache_ptr = NEXT(xlat_new_cache_ptr);
   384     }
   385     return xlat_new_create_ptr;
   387 }
   389 void xlat_commit_block( uint32_t destsize, uint32_t srcsize )
   390 {
   391     void **ptr = xlat_new_create_ptr->lut_entry;
   392     void **endptr = ptr + (srcsize>>2);
   393     while( ptr < endptr ) {
   394 	if( *ptr == NULL ) {
   395 	    *ptr = XLAT_LUT_ENTRY_USED;
   396 	}
   397 	ptr++;
   398     }
   400     xlat_new_cache_ptr = xlat_cut_block( xlat_new_create_ptr, destsize );
   401 }
   403 void xlat_delete_block( xlat_cache_block_t block ) 
   404 {
   405     block->active = 0;
   406     *block->lut_entry = NULL;
   407 }
   409 void xlat_check_cache_integrity( xlat_cache_block_t cache, xlat_cache_block_t ptr, int size )
   410 {
   411     int foundptr = 0;
   412     xlat_cache_block_t tail = 
   413 	(xlat_cache_block_t)(((char *)cache) + size - sizeof(struct xlat_cache_block));
   415     assert( tail->active == 1 );
   416     assert( tail->size == 0 ); 
   417     while( cache < tail ) {
   418 	assert( cache->active >= 0 && cache->active <= 2 );
   419 	assert( cache->size >= 0 && cache->size < size );
   420 	if( cache == ptr ) {
   421 	    foundptr = 1;
   422 	}
   423 	cache = NEXT(cache);
   424     }
   425     assert( cache == tail );
   426     assert( foundptr == 1 );
   427 }
   429 void xlat_check_integrity( )
   430 {
   431     xlat_check_cache_integrity( xlat_new_cache, xlat_new_cache_ptr, XLAT_NEW_CACHE_SIZE );
   432     xlat_check_cache_integrity( xlat_temp_cache, xlat_temp_cache_ptr, XLAT_TEMP_CACHE_SIZE );
   433     xlat_check_cache_integrity( xlat_old_cache, xlat_old_cache_ptr, XLAT_OLD_CACHE_SIZE );
   434 }
   437 void xlat_disasm_block( FILE *out, void *block )
   438 {
   439     uint32_t buflen = xlat_get_block_size(block);
   440     x86_set_symtab( NULL, 0 );
   441     x86_disasm_block( out, block, buflen );
   442 }
.