2 * $Id: xltcache.c,v 1.9 2007-10-07 06:27:12 nkeynes Exp $
4 * Translation cache management. This part is architecture independent.
6 * Copyright (c) 2005 Nathan Keynes.
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.
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.
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
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)
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 *) );
82 void xlat_print_free( FILE *out )
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 );
89 * Reset the cache structure to its default state
91 void xlat_flush_cache()
93 xlat_cache_block_t tmp;
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);
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);
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);
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 );
120 static void xlat_flush_page_by_lut( void **page )
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;
131 void xlat_invalidate_word( sh4addr_t addr )
134 void **page = xlat_lut[XLAT_LUT_PAGE(addr)];
136 int entry = XLAT_LUT_ENTRY(addr);
137 if( page[entry] != NULL ) {
138 xlat_flush_page_by_lut(page);
144 void xlat_invalidate_long( sh4addr_t addr )
147 void **page = xlat_lut[XLAT_LUT_PAGE(addr)];
149 int entry = XLAT_LUT_ENTRY(addr);
150 if( page[entry] != NULL || page[entry+1] != NULL ) {
151 xlat_flush_page_by_lut(page);
157 void xlat_invalidate_block( sh4addr_t address, size_t size )
160 int entry_count = size >> 1; // words;
161 uint32_t page_no = XLAT_LUT_PAGE(address);
162 int entry = XLAT_LUT_ENTRY(address);
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;
171 if( page_entries == XLAT_LUT_PAGE_ENTRIES ) {
172 /* Overwriting the entire page anyway */
173 xlat_flush_page_by_lut(page);
175 for( i=entry; i<entry+page_entries; i++ ) {
176 if( page[i] != NULL ) {
177 xlat_flush_page_by_lut(page);
182 entry_count -= page_entries;
185 entry_count -= page_entries;
187 } while( entry_count > 0 );
191 void xlat_flush_page( sh4addr_t address )
193 void **page = xlat_lut[XLAT_LUT_PAGE(address)];
195 xlat_flush_page_by_lut(page);
199 void *xlat_get_code( sh4addr_t address )
202 void **page = xlat_lut[XLAT_LUT_PAGE(address)];
204 result = (void *)(((uint32_t)(page[XLAT_LUT_ENTRY(address)])) & 0xFFFFFFFC);
209 void **xlat_get_lut_entry( sh4addr_t address )
211 void **page = xlat_lut[XLAT_LUT_PAGE(address)];
213 /* Add the LUT entry for the block */
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 );
221 return &page[XLAT_LUT_ENTRY(address)];
226 uint32_t xlat_get_block_size( void *block )
228 xlat_cache_block_t xlt = (xlat_cache_block_t)(((char *)block)-sizeof(struct xlat_cache_block));
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.
238 static inline xlat_cache_block_t xlat_cut_block( xlat_cache_block_t block, int cutsize )
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);
247 next->size = oldsize - cutsize - sizeof(struct xlat_cache_block);
255 * Promote a block in temp space (or elsewhere for that matter) to old space.
257 * @param block to promote.
259 static void xlat_promote_to_old_space( xlat_cache_block_t block )
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;
266 allocation += curr->size + sizeof(struct xlat_cache_block);
268 if( allocation > size ) {
271 if( curr->size == 0 ) { /* End-of-cache Sentinel */
272 /* Leave what we just released as free space and start again from the
275 start_block->active = 0;
276 start_block->size = allocation;
277 allocation = -sizeof(struct xlat_cache_block);
278 start_block = curr = xlat_old_cache;
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;
293 * Similarly to the above method, promotes a block to temp space.
294 * TODO: Try to combine these - they're nearly identical
296 void xlat_promote_to_temp_space( xlat_cache_block_t block )
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;
303 if( curr->active == BLOCK_USED ) {
304 xlat_promote_to_old_space( curr );
306 allocation += curr->size + sizeof(struct xlat_cache_block);
308 if( allocation > size ) {
311 if( curr->size == 0 ) { /* End-of-cache Sentinel */
312 /* Leave what we just released as free space and start again from the
315 start_block->active = 0;
316 start_block->size = allocation;
317 allocation = -sizeof(struct xlat_cache_block);
318 start_block = curr = xlat_temp_cache;
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;
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.
337 xlat_cache_block_t xlat_start_block( sh4addr_t address )
339 if( xlat_new_cache_ptr->size == 0 ) {
340 xlat_new_cache_ptr = xlat_new_cache;
343 if( xlat_new_cache_ptr->active ) {
344 xlat_promote_to_temp_space( xlat_new_cache_ptr );
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 );
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;
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;
370 xlat_cache_block_t xlat_extend_block( uint32_t newSize )
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;
383 if( xlat_new_cache_ptr->active ) {
384 xlat_promote_to_temp_space( xlat_new_cache_ptr );
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 );
396 if( xlat_new_cache_ptr->active ) {
397 xlat_promote_to_temp_space( xlat_new_cache_ptr );
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);
403 return xlat_new_create_ptr;
407 void xlat_commit_block( uint32_t destsize, uint32_t srcsize )
409 void **ptr = xlat_new_create_ptr->lut_entry;
410 void **endptr = ptr + (srcsize>>2);
411 while( ptr < endptr ) {
413 *ptr = XLAT_LUT_ENTRY_USED;
418 xlat_new_cache_ptr = xlat_cut_block( xlat_new_create_ptr, destsize );
421 void xlat_delete_block( xlat_cache_block_t block )
424 *block->lut_entry = NULL;
427 void xlat_check_cache_integrity( xlat_cache_block_t cache, xlat_cache_block_t ptr, int size )
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 );
443 assert( cache == tail );
444 assert( foundptr == 1 );
447 void xlat_check_integrity( )
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 );
455 void xlat_disasm_block( FILE *out, void *block )
457 uint32_t buflen = xlat_get_block_size(block);
458 x86_set_symtab( NULL, 0 );
459 x86_disasm_block( out, block, buflen );
.