Search
lxdream.org :: lxdream/src/sh4/xltcache.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/xltcache.c
changeset 428:338966c8aed0
prev422:61a0598e07ff
next488:d19a63e47cd9
author nkeynes
date Sun Oct 07 06:27:12 2007 +0000 (16 years ago)
permissions -rw-r--r--
last change Fix compilation warnings
view annotate diff log raw
     1 /**
     2  * $Id: xltcache.c,v 1.9 2007-10-07 06:27:12 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 <sys/mman.h>
    20 #include <assert.h>
    22 #include "dreamcast.h"
    23 #include "sh4/xltcache.h"
    24 #include "x86dasm/x86dasm.h"
    26 #define XLAT_LUT_PAGE_BITS 12
    27 #define XLAT_LUT_TOTAL_BITS 28
    28 #define XLAT_LUT_PAGE(addr) (((addr)>>13) & 0xFFFF)
    29 #define XLAT_LUT_ENTRY(addr) (((addr)&0x1FFE) >> 1)
    31 #define XLAT_LUT_PAGES (1<<(XLAT_LUT_TOTAL_BITS-XLAT_LUT_PAGE_BITS))
    32 #define XLAT_LUT_PAGE_ENTRIES (1<<XLAT_LUT_PAGE_BITS)
    33 #define XLAT_LUT_PAGE_SIZE (XLAT_LUT_PAGE_ENTRIES * sizeof(void *))
    35 #define XLAT_LUT_ENTRY_EMPTY (void *)0
    36 #define XLAT_LUT_ENTRY_USED  (void *)1
    38 #define NEXT(block) ( (xlat_cache_block_t)&((block)->code[(block)->size]))
    39 #define BLOCK_FOR_CODE(code) (((xlat_cache_block_t)code)-1)
    40 #define IS_ENTRY_POINT(ent) (ent > XLAT_LUT_ENTRY_USED)
    41 #define IS_ENTRY_USED(ent) (ent != XLAT_LUT_ENTRY_EMPTY)
    43 #define MIN_BLOCK_SIZE 32
    44 #define MIN_TOTAL_SIZE (sizeof(struct xlat_cache_block)+MIN_BLOCK_SIZE)
    46 #define BLOCK_INACTIVE 0
    47 #define BLOCK_ACTIVE 1
    48 #define BLOCK_USED 2
    50 xlat_cache_block_t xlat_new_cache;
    51 xlat_cache_block_t xlat_new_cache_ptr;
    52 xlat_cache_block_t xlat_new_create_ptr;
    53 xlat_cache_block_t xlat_temp_cache;
    54 xlat_cache_block_t xlat_temp_cache_ptr;
    55 xlat_cache_block_t xlat_old_cache;
    56 xlat_cache_block_t xlat_old_cache_ptr;
    57 static void ***xlat_lut;
    58 static gboolean xlat_initialized = FALSE;
    60 void xlat_cache_init(void) 
    61 {
    62     if( !xlat_initialized ) {
    63 	xlat_initialized = TRUE;
    64 	xlat_new_cache = mmap( NULL, XLAT_NEW_CACHE_SIZE, PROT_EXEC|PROT_READ|PROT_WRITE,
    65 			       MAP_PRIVATE|MAP_ANONYMOUS, -1, 0 );
    66 	xlat_temp_cache = mmap( NULL, XLAT_TEMP_CACHE_SIZE, PROT_EXEC|PROT_READ|PROT_WRITE,
    67 				MAP_PRIVATE|MAP_ANONYMOUS, -1, 0 );
    68 	xlat_old_cache = mmap( NULL, XLAT_OLD_CACHE_SIZE, PROT_EXEC|PROT_READ|PROT_WRITE,
    69 			       MAP_PRIVATE|MAP_ANONYMOUS, -1, 0 );
    70 	xlat_new_cache_ptr = xlat_new_cache;
    71 	xlat_temp_cache_ptr = xlat_temp_cache;
    72 	xlat_old_cache_ptr = xlat_old_cache;
    73 	xlat_new_create_ptr = xlat_new_cache;
    75 	xlat_lut = mmap( NULL, XLAT_LUT_PAGES*sizeof(void *), PROT_READ|PROT_WRITE,
    76 			 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
    77 	memset( xlat_lut, 0, XLAT_LUT_PAGES*sizeof(void *) );
    78     }
    79     xlat_flush_cache();
    80 }
    82 void xlat_print_free( FILE *out )
    83 {
    84     fprintf( out, "New space: %d\nTemp space: %d\nOld space: %d\n", 
    85 	     xlat_new_cache_ptr->size, xlat_temp_cache_ptr->size, xlat_old_cache_ptr->size );
    86 }
    88 /**
    89  * Reset the cache structure to its default state
    90  */
    91 void xlat_flush_cache() 
    92 {
    93     xlat_cache_block_t tmp;
    94     int i;
    95     xlat_new_cache_ptr = xlat_new_cache;
    96     xlat_new_cache_ptr->active = 0;
    97     xlat_new_cache_ptr->size = XLAT_NEW_CACHE_SIZE - 2*sizeof(struct xlat_cache_block);
    98     tmp = NEXT(xlat_new_cache_ptr);
    99     tmp->active = 1;
   100     tmp->size = 0;
   101     xlat_temp_cache_ptr = xlat_temp_cache;
   102     xlat_temp_cache_ptr->active = 0;
   103     xlat_temp_cache_ptr->size = XLAT_TEMP_CACHE_SIZE - 2*sizeof(struct xlat_cache_block);
   104     tmp = NEXT(xlat_temp_cache_ptr);
   105     tmp->active = 1;
   106     tmp->size = 0;
   107     xlat_old_cache_ptr = xlat_old_cache;
   108     xlat_old_cache_ptr->active = 0;
   109     xlat_old_cache_ptr->size = XLAT_OLD_CACHE_SIZE - 2*sizeof(struct xlat_cache_block);
   110     tmp = NEXT(xlat_old_cache_ptr);
   111     tmp->active = 1;
   112     tmp->size = 0;
   113     for( i=0; i<XLAT_LUT_PAGES; i++ ) {
   114 	if( xlat_lut[i] != NULL ) {
   115 	    memset( xlat_lut[i], 0, XLAT_LUT_PAGE_SIZE );
   116 	}
   117     }
   118 }
   120 static void xlat_flush_page_by_lut( void **page )
   121 {
   122     int i;
   123     for( i=0; i<XLAT_LUT_PAGE_ENTRIES; i++ ) {
   124 	if( IS_ENTRY_POINT(page[i]) ) {
   125 	    BLOCK_FOR_CODE(page[i])->active = 0;
   126 	}
   127 	page[i] = NULL;
   128     }
   129 }
   131 void xlat_invalidate_word( sh4addr_t addr )
   132 {
   133     if( xlat_lut ) {
   134 	void **page = xlat_lut[XLAT_LUT_PAGE(addr)];
   135 	if( page != NULL ) {
   136 	    int entry = XLAT_LUT_ENTRY(addr);
   137 	    if( page[entry] != NULL ) {
   138 		xlat_flush_page_by_lut(page);
   139 	    }
   140 	}
   141     }
   142 }
   144 void xlat_invalidate_long( sh4addr_t addr )
   145 {
   146     if( xlat_lut ) {
   147 	void **page = xlat_lut[XLAT_LUT_PAGE(addr)];
   148 	if( page != NULL ) {
   149 	    int entry = XLAT_LUT_ENTRY(addr);
   150 	    if( page[entry] != NULL || page[entry+1] != NULL ) {
   151 		xlat_flush_page_by_lut(page);
   152 	    }
   153 	}
   154     }
   155 }
   157 void xlat_invalidate_block( sh4addr_t address, size_t size )
   158 {
   159     int i;
   160     int entry_count = size >> 1; // words;
   161     uint32_t page_no = XLAT_LUT_PAGE(address);
   162     int entry = XLAT_LUT_ENTRY(address);
   163     if( xlat_lut ) {
   164 	do {
   165 	    void **page = xlat_lut[page_no];
   166 	    int page_entries = XLAT_LUT_PAGE_ENTRIES - entry;
   167 	    if( entry_count < page_entries ) {
   168 		page_entries = entry_count;
   169 	    }
   170 	    if( page != NULL ) {
   171 		if( page_entries == XLAT_LUT_PAGE_ENTRIES ) {
   172 		    /* Overwriting the entire page anyway */
   173 		    xlat_flush_page_by_lut(page);
   174 		} else {
   175 		    for( i=entry; i<entry+page_entries; i++ ) {
   176 			if( page[i] != NULL ) {
   177 			    xlat_flush_page_by_lut(page);
   178 			    break;
   179 			}
   180 		    }
   181 		}
   182 		entry_count -= page_entries;
   183 	    }
   184 	    page_no ++;
   185 	    entry_count -= page_entries;
   186 	    entry = 0;
   187 	} while( entry_count > 0 );
   188     }
   189 }
   191 void xlat_flush_page( sh4addr_t address )
   192 {
   193     void **page = xlat_lut[XLAT_LUT_PAGE(address)];
   194     if( page != NULL ) {
   195 	xlat_flush_page_by_lut(page);
   196     }
   197 }
   199 void *xlat_get_code( sh4addr_t address )
   200 {
   201     void *result = NULL;
   202     void **page = xlat_lut[XLAT_LUT_PAGE(address)];
   203     if( page != NULL ) {
   204 	result = (void *)(((uint32_t)(page[XLAT_LUT_ENTRY(address)])) & 0xFFFFFFFC);
   205     }
   206     return result;
   207 }
   209 void **xlat_get_lut_entry( sh4addr_t address )
   210 {
   211     void **page = xlat_lut[XLAT_LUT_PAGE(address)];
   213     /* Add the LUT entry for the block */
   214     if( page == NULL ) {
   215 	xlat_lut[XLAT_LUT_PAGE(address)] = page =
   216 	    mmap( NULL, XLAT_LUT_PAGE_SIZE, PROT_READ|PROT_WRITE,
   217 		  MAP_PRIVATE|MAP_ANONYMOUS, -1, 0 );
   218 	memset( page, 0, XLAT_LUT_PAGE_SIZE );
   219     }
   221     return &page[XLAT_LUT_ENTRY(address)];
   222 }
   226 uint32_t xlat_get_block_size( void *block )
   227 {
   228     xlat_cache_block_t xlt = (xlat_cache_block_t)(((char *)block)-sizeof(struct xlat_cache_block));
   229     return xlt->size;
   230 }
   232 /**
   233  * Cut the specified block so that it has the given size, with the remaining data
   234  * forming a new free block. If the free block would be less than the minimum size,
   235  * the cut is not performed.
   236  * @return the next block after the (possibly cut) block.
   237  */
   238 static inline xlat_cache_block_t xlat_cut_block( xlat_cache_block_t block, int cutsize )
   239 {
   240     cutsize = (cutsize + 3) & 0xFFFFFFFC; // force word alignment
   241     assert( cutsize <= block->size );
   242     if( block->size > cutsize + MIN_TOTAL_SIZE ) {
   243 	int oldsize = block->size;
   244 	block->size = cutsize;
   245 	xlat_cache_block_t next = NEXT(block);
   246 	next->active = 0;
   247 	next->size = oldsize - cutsize - sizeof(struct xlat_cache_block);
   248 	return next;
   249     } else {
   250 	return NEXT(block);
   251     }
   252 }
   254 /**
   255  * Promote a block in temp space (or elsewhere for that matter) to old space.
   256  *
   257  * @param block to promote.
   258  */
   259 static void xlat_promote_to_old_space( xlat_cache_block_t block )
   260 {
   261     int allocation = -sizeof(struct xlat_cache_block);
   262     int size = block->size;
   263     xlat_cache_block_t curr = xlat_old_cache_ptr;
   264     xlat_cache_block_t start_block = curr;
   265     do {
   266 	allocation += curr->size + sizeof(struct xlat_cache_block);
   267 	curr = NEXT(curr);
   268 	if( allocation > size ) {
   269 	    break; /* done */
   270 	}
   271 	if( curr->size == 0 ) { /* End-of-cache Sentinel */
   272 	    /* Leave what we just released as free space and start again from the
   273 	     * top of the cache
   274 	     */
   275 	    start_block->active = 0;
   276 	    start_block->size = allocation;
   277 	    allocation = -sizeof(struct xlat_cache_block);
   278 	    start_block = curr = xlat_old_cache;
   279 	}
   280     } while(1);
   281     start_block->active = 1;
   282     start_block->size = allocation;
   283     start_block->lut_entry = block->lut_entry;
   284     *block->lut_entry = &start_block->code;
   285     memcpy( start_block->code, block->code, block->size );
   286     xlat_old_cache_ptr = xlat_cut_block(start_block, size );
   287     if( xlat_old_cache_ptr->size == 0 ) {
   288 	xlat_old_cache_ptr = xlat_old_cache;
   289     }
   290 }
   292 /**
   293  * Similarly to the above method, promotes a block to temp space.
   294  * TODO: Try to combine these - they're nearly identical
   295  */
   296 void xlat_promote_to_temp_space( xlat_cache_block_t block )
   297 {
   298     int size = block->size;
   299     int allocation = -sizeof(struct xlat_cache_block);
   300     xlat_cache_block_t curr = xlat_temp_cache_ptr;
   301     xlat_cache_block_t start_block = curr;
   302     do {
   303 	if( curr->active == BLOCK_USED ) {
   304 	    xlat_promote_to_old_space( curr );
   305 	}
   306 	allocation += curr->size + sizeof(struct xlat_cache_block);
   307 	curr = NEXT(curr);
   308 	if( allocation > size ) {
   309 	    break; /* done */
   310 	}
   311 	if( curr->size == 0 ) { /* End-of-cache Sentinel */
   312 	    /* Leave what we just released as free space and start again from the
   313 	     * top of the cache
   314 	     */
   315 	    start_block->active = 0;
   316 	    start_block->size = allocation;
   317 	    allocation = -sizeof(struct xlat_cache_block);
   318 	    start_block = curr = xlat_temp_cache;
   319 	}
   320     } while(1);
   321     start_block->active = 1;
   322     start_block->size = allocation;
   323     start_block->lut_entry = block->lut_entry;
   324     *block->lut_entry = &start_block->code;
   325     memcpy( start_block->code, block->code, block->size );
   326     xlat_temp_cache_ptr = xlat_cut_block(start_block, size );
   327     if( xlat_temp_cache_ptr->size == 0 ) {
   328 	xlat_temp_cache_ptr = xlat_temp_cache;
   329     }
   331 }
   333 /**
   334  * Returns the next block in the new cache list that can be written to by the
   335  * translator. If the next block is active, it is evicted first.
   336  */
   337 xlat_cache_block_t xlat_start_block( sh4addr_t address )
   338 {
   339     if( xlat_new_cache_ptr->size == 0 ) {
   340 	xlat_new_cache_ptr = xlat_new_cache;
   341     }
   343     if( xlat_new_cache_ptr->active ) {
   344 	xlat_promote_to_temp_space( xlat_new_cache_ptr );
   345     }
   346     xlat_new_create_ptr = xlat_new_cache_ptr;
   347     xlat_new_create_ptr->active = 1;
   348     xlat_new_cache_ptr = NEXT(xlat_new_cache_ptr);
   350     /* Add the LUT entry for the block */
   351     if( xlat_lut[XLAT_LUT_PAGE(address)] == NULL ) {
   352 	xlat_lut[XLAT_LUT_PAGE(address)] =
   353 	    mmap( NULL, XLAT_LUT_PAGE_SIZE, PROT_READ|PROT_WRITE,
   354 		  MAP_PRIVATE|MAP_ANONYMOUS, -1, 0 );
   355 	memset( xlat_lut[XLAT_LUT_PAGE(address)], 0, XLAT_LUT_PAGE_SIZE );
   356     }
   358     if( IS_ENTRY_POINT(xlat_lut[XLAT_LUT_PAGE(address)][XLAT_LUT_ENTRY(address)]) ) {
   359 	xlat_cache_block_t oldblock = BLOCK_FOR_CODE(xlat_lut[XLAT_LUT_PAGE(address)][XLAT_LUT_ENTRY(address)]);
   360 	oldblock->active = 0;
   361     }
   363     xlat_lut[XLAT_LUT_PAGE(address)][XLAT_LUT_ENTRY(address)] = 
   364 	&xlat_new_create_ptr->code;
   365     xlat_new_create_ptr->lut_entry = xlat_lut[XLAT_LUT_PAGE(address)] + XLAT_LUT_ENTRY(address);
   367     return xlat_new_create_ptr;
   368 }
   370 xlat_cache_block_t xlat_extend_block( uint32_t newSize )
   371 {
   372     while( xlat_new_create_ptr->size < newSize ) {
   373 	if( xlat_new_cache_ptr->size == 0 ) {
   374 	    /* Migrate to the front of the cache to keep it contiguous */
   375 	    xlat_new_create_ptr->active = 0;
   376 	    unsigned char *olddata = xlat_new_create_ptr->code;
   377 	    int oldsize = xlat_new_create_ptr->size;
   378 	    int size = oldsize + MIN_BLOCK_SIZE; /* minimum expansion */
   379 	    void **lut_entry = xlat_new_create_ptr->lut_entry;
   380 	    int allocation = -sizeof(struct xlat_cache_block);
   381 	    xlat_new_cache_ptr = xlat_new_cache;
   382 	    do {
   383 		if( xlat_new_cache_ptr->active ) {
   384 		    xlat_promote_to_temp_space( xlat_new_cache_ptr );
   385 		}
   386 		allocation += xlat_new_cache_ptr->size + sizeof(struct xlat_cache_block);
   387 		xlat_new_cache_ptr = NEXT(xlat_new_cache_ptr);
   388 	    } while( allocation < size );
   389 	    xlat_new_create_ptr = xlat_new_cache;
   390 	    xlat_new_create_ptr->active = 1;
   391 	    xlat_new_create_ptr->size = allocation;
   392 	    xlat_new_create_ptr->lut_entry = lut_entry;
   393 	    *lut_entry = &xlat_new_create_ptr->code;
   394 	    memmove( xlat_new_create_ptr->code, olddata, oldsize );
   395 	} else {
   396 	    if( xlat_new_cache_ptr->active ) {
   397 		xlat_promote_to_temp_space( xlat_new_cache_ptr );
   398 	    }
   399 	    xlat_new_create_ptr->size += xlat_new_cache_ptr->size + sizeof(struct xlat_cache_block);
   400 	    xlat_new_cache_ptr = NEXT(xlat_new_cache_ptr);
   401 	}
   402     }
   403     return xlat_new_create_ptr;
   405 }
   407 void xlat_commit_block( uint32_t destsize, uint32_t srcsize )
   408 {
   409     void **ptr = xlat_new_create_ptr->lut_entry;
   410     void **endptr = ptr + (srcsize>>2);
   411     while( ptr < endptr ) {
   412 	if( *ptr == NULL ) {
   413 	    *ptr = XLAT_LUT_ENTRY_USED;
   414 	}
   415 	ptr++;
   416     }
   418     xlat_new_cache_ptr = xlat_cut_block( xlat_new_create_ptr, destsize );
   419 }
   421 void xlat_delete_block( xlat_cache_block_t block ) 
   422 {
   423     block->active = 0;
   424     *block->lut_entry = NULL;
   425 }
   427 void xlat_check_cache_integrity( xlat_cache_block_t cache, xlat_cache_block_t ptr, int size )
   428 {
   429     int foundptr = 0;
   430     xlat_cache_block_t tail = 
   431 	(xlat_cache_block_t)(((char *)cache) + size - sizeof(struct xlat_cache_block));
   433     assert( tail->active == 1 );
   434     assert( tail->size == 0 ); 
   435     while( cache < tail ) {
   436 	assert( cache->active >= 0 && cache->active <= 2 );
   437 	assert( cache->size >= 0 && cache->size < size );
   438 	if( cache == ptr ) {
   439 	    foundptr = 1;
   440 	}
   441 	cache = NEXT(cache);
   442     }
   443     assert( cache == tail );
   444     assert( foundptr == 1 );
   445 }
   447 void xlat_check_integrity( )
   448 {
   449     xlat_check_cache_integrity( xlat_new_cache, xlat_new_cache_ptr, XLAT_NEW_CACHE_SIZE );
   450     xlat_check_cache_integrity( xlat_temp_cache, xlat_temp_cache_ptr, XLAT_TEMP_CACHE_SIZE );
   451     xlat_check_cache_integrity( xlat_old_cache, xlat_old_cache_ptr, XLAT_OLD_CACHE_SIZE );
   452 }
   455 void xlat_disasm_block( FILE *out, void *block )
   456 {
   457     uint32_t buflen = xlat_get_block_size(block);
   458     x86_set_symtab( NULL, 0 );
   459     x86_disasm_block( out, block, buflen );
   460 }
.