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.
19 #include <sys/types.h>
23 #include "dreamcast.h"
24 #include "sh4/sh4core.h"
25 #include "sh4/xltcache.h"
26 #include "x86dasm/x86dasm.h"
28 #define XLAT_LUT_PAGE_BITS 12
29 #define XLAT_LUT_TOTAL_BITS 28
30 #define XLAT_LUT_PAGE(addr) (((addr)>>13) & 0xFFFF)
31 #define XLAT_LUT_ENTRY(addr) (((addr)&0x1FFE) >> 1)
33 #define XLAT_LUT_PAGES (1<<(XLAT_LUT_TOTAL_BITS-XLAT_LUT_PAGE_BITS))
34 #define XLAT_LUT_PAGE_ENTRIES (1<<XLAT_LUT_PAGE_BITS)
35 #define XLAT_LUT_PAGE_SIZE (XLAT_LUT_PAGE_ENTRIES * sizeof(void *))
37 #define XLAT_LUT_ENTRY_EMPTY (void *)0
38 #define XLAT_LUT_ENTRY_USED (void *)1
40 #define NEXT(block) ( (xlat_cache_block_t)&((block)->code[(block)->size]))
41 #define BLOCK_FOR_CODE(code) (((xlat_cache_block_t)code)-1)
42 #define IS_ENTRY_POINT(ent) (ent > XLAT_LUT_ENTRY_USED)
43 #define IS_ENTRY_USED(ent) (ent != XLAT_LUT_ENTRY_EMPTY)
45 #define MIN_BLOCK_SIZE 32
46 #define MIN_TOTAL_SIZE (sizeof(struct xlat_cache_block)+MIN_BLOCK_SIZE)
48 #define BLOCK_INACTIVE 0
49 #define BLOCK_ACTIVE 1
52 xlat_cache_block_t xlat_new_cache;
53 xlat_cache_block_t xlat_new_cache_ptr;
54 xlat_cache_block_t xlat_new_create_ptr;
55 xlat_cache_block_t xlat_temp_cache;
56 xlat_cache_block_t xlat_temp_cache_ptr;
57 xlat_cache_block_t xlat_old_cache;
58 xlat_cache_block_t xlat_old_cache_ptr;
59 static void ***xlat_lut;
60 static gboolean xlat_initialized = FALSE;
62 void xlat_cache_init(void)
64 if( !xlat_initialized ) {
65 xlat_initialized = TRUE;
66 xlat_new_cache = mmap( NULL, XLAT_NEW_CACHE_SIZE, PROT_EXEC|PROT_READ|PROT_WRITE,
67 MAP_PRIVATE|MAP_ANON, -1, 0 );
68 xlat_temp_cache = mmap( NULL, XLAT_TEMP_CACHE_SIZE, PROT_EXEC|PROT_READ|PROT_WRITE,
69 MAP_PRIVATE|MAP_ANON, -1, 0 );
70 xlat_old_cache = mmap( NULL, XLAT_OLD_CACHE_SIZE, PROT_EXEC|PROT_READ|PROT_WRITE,
71 MAP_PRIVATE|MAP_ANON, -1, 0 );
72 xlat_new_cache_ptr = xlat_new_cache;
73 xlat_temp_cache_ptr = xlat_temp_cache;
74 xlat_old_cache_ptr = xlat_old_cache;
75 xlat_new_create_ptr = xlat_new_cache;
77 xlat_lut = mmap( NULL, XLAT_LUT_PAGES*sizeof(void *), PROT_READ|PROT_WRITE,
78 MAP_PRIVATE|MAP_ANON, -1, 0);
79 memset( xlat_lut, 0, XLAT_LUT_PAGES*sizeof(void *) );
84 void xlat_print_free( FILE *out )
86 fprintf( out, "New space: %d\nTemp space: %d\nOld space: %d\n",
87 xlat_new_cache_ptr->size, xlat_temp_cache_ptr->size, xlat_old_cache_ptr->size );
91 * Reset the cache structure to its default state
93 void xlat_flush_cache()
95 xlat_cache_block_t tmp;
97 xlat_new_cache_ptr = xlat_new_cache;
98 xlat_new_cache_ptr->active = 0;
99 xlat_new_cache_ptr->size = XLAT_NEW_CACHE_SIZE - 2*sizeof(struct xlat_cache_block);
100 tmp = NEXT(xlat_new_cache_ptr);
103 xlat_temp_cache_ptr = xlat_temp_cache;
104 xlat_temp_cache_ptr->active = 0;
105 xlat_temp_cache_ptr->size = XLAT_TEMP_CACHE_SIZE - 2*sizeof(struct xlat_cache_block);
106 tmp = NEXT(xlat_temp_cache_ptr);
109 xlat_old_cache_ptr = xlat_old_cache;
110 xlat_old_cache_ptr->active = 0;
111 xlat_old_cache_ptr->size = XLAT_OLD_CACHE_SIZE - 2*sizeof(struct xlat_cache_block);
112 tmp = NEXT(xlat_old_cache_ptr);
115 for( i=0; i<XLAT_LUT_PAGES; i++ ) {
116 if( xlat_lut[i] != NULL ) {
117 memset( xlat_lut[i], 0, XLAT_LUT_PAGE_SIZE );
122 static void xlat_flush_page_by_lut( void **page )
125 for( i=0; i<XLAT_LUT_PAGE_ENTRIES; i++ ) {
126 if( IS_ENTRY_POINT(page[i]) ) {
127 BLOCK_FOR_CODE(page[i])->active = 0;
133 void xlat_invalidate_word( sh4addr_t addr )
136 void **page = xlat_lut[XLAT_LUT_PAGE(addr)];
138 int entry = XLAT_LUT_ENTRY(addr);
139 if( page[entry] != NULL ) {
140 xlat_flush_page_by_lut(page);
146 void xlat_invalidate_long( sh4addr_t addr )
149 void **page = xlat_lut[XLAT_LUT_PAGE(addr)];
151 int entry = XLAT_LUT_ENTRY(addr);
152 if( page[entry] != NULL || page[entry+1] != NULL ) {
153 xlat_flush_page_by_lut(page);
159 void xlat_invalidate_block( sh4addr_t address, size_t size )
162 int entry_count = size >> 1; // words;
163 uint32_t page_no = XLAT_LUT_PAGE(address);
164 int entry = XLAT_LUT_ENTRY(address);
167 void **page = xlat_lut[page_no];
168 int page_entries = XLAT_LUT_PAGE_ENTRIES - entry;
169 if( entry_count < page_entries ) {
170 page_entries = entry_count;
173 if( page_entries == XLAT_LUT_PAGE_ENTRIES ) {
174 /* Overwriting the entire page anyway */
175 xlat_flush_page_by_lut(page);
177 for( i=entry; i<entry+page_entries; i++ ) {
178 if( page[i] != NULL ) {
179 xlat_flush_page_by_lut(page);
184 entry_count -= page_entries;
187 entry_count -= page_entries;
189 } while( entry_count > 0 );
193 void xlat_flush_page( sh4addr_t address )
195 void **page = xlat_lut[XLAT_LUT_PAGE(address)];
197 xlat_flush_page_by_lut(page);
201 void *xlat_get_code( sh4addr_t address )
204 void **page = xlat_lut[XLAT_LUT_PAGE(address)];
206 result = (void *)(((uintptr_t)(page[XLAT_LUT_ENTRY(address)])) & (~((uintptr_t)0x03)));
211 xlat_recovery_record_t xlat_get_recovery( void *code, void *native_pc, gboolean recover_after )
214 uintptr_t pc_offset = ((uint8_t *)native_pc) - ((uint8_t *)code);
215 xlat_cache_block_t block = BLOCK_FOR_CODE(code);
216 uint32_t count = block->recover_table_size;
217 xlat_recovery_record_t records = (xlat_recovery_record_t)(&block->code[block->recover_table_offset]);
219 if( recover_after ) {
220 if( records[count-1].xlat_offset < pc_offset ) {
223 for( posn=count-1; posn > 0; posn-- ) {
224 if( records[posn-1].xlat_offset < pc_offset ) {
225 return &records[posn];
228 return &records[0]; // shouldn't happen
230 for( posn = 1; posn < count; posn++ ) {
231 if( records[posn].xlat_offset >= pc_offset ) {
232 return &records[posn-1];
235 return &records[count-1];
241 void **xlat_get_lut_entry( sh4addr_t address )
243 void **page = xlat_lut[XLAT_LUT_PAGE(address)];
245 /* Add the LUT entry for the block */
247 xlat_lut[XLAT_LUT_PAGE(address)] = page =
248 mmap( NULL, XLAT_LUT_PAGE_SIZE, PROT_READ|PROT_WRITE,
249 MAP_PRIVATE|MAP_ANON, -1, 0 );
250 memset( page, 0, XLAT_LUT_PAGE_SIZE );
253 return &page[XLAT_LUT_ENTRY(address)];
258 uint32_t xlat_get_block_size( void *block )
260 xlat_cache_block_t xlt = (xlat_cache_block_t)(((char *)block)-sizeof(struct xlat_cache_block));
264 uint32_t xlat_get_code_size( void *block )
266 xlat_cache_block_t xlt = (xlat_cache_block_t)(((char *)block)-sizeof(struct xlat_cache_block));
267 if( xlt->recover_table_offset == 0 ) {
270 return xlt->recover_table_offset;
275 * Cut the specified block so that it has the given size, with the remaining data
276 * forming a new free block. If the free block would be less than the minimum size,
277 * the cut is not performed.
278 * @return the next block after the (possibly cut) block.
280 static inline xlat_cache_block_t xlat_cut_block( xlat_cache_block_t block, int cutsize )
282 cutsize = (cutsize + 3) & 0xFFFFFFFC; // force word alignment
283 assert( cutsize <= block->size );
284 if( block->size > cutsize + MIN_TOTAL_SIZE ) {
285 int oldsize = block->size;
286 block->size = cutsize;
287 xlat_cache_block_t next = NEXT(block);
289 next->size = oldsize - cutsize - sizeof(struct xlat_cache_block);
297 * Promote a block in temp space (or elsewhere for that matter) to old space.
299 * @param block to promote.
301 static void xlat_promote_to_old_space( xlat_cache_block_t block )
303 int allocation = -sizeof(struct xlat_cache_block);
304 int size = block->size;
305 xlat_cache_block_t curr = xlat_old_cache_ptr;
306 xlat_cache_block_t start_block = curr;
308 allocation += curr->size + sizeof(struct xlat_cache_block);
310 if( allocation > size ) {
313 if( curr->size == 0 ) { /* End-of-cache Sentinel */
314 /* Leave what we just released as free space and start again from the
317 start_block->active = 0;
318 start_block->size = allocation;
319 allocation = -sizeof(struct xlat_cache_block);
320 start_block = curr = xlat_old_cache;
323 start_block->active = 1;
324 start_block->size = allocation;
325 start_block->lut_entry = block->lut_entry;
326 start_block->recover_table_offset = block->recover_table_offset;
327 start_block->recover_table_size = block->recover_table_size;
328 *block->lut_entry = &start_block->code;
329 memcpy( start_block->code, block->code, block->size );
330 xlat_old_cache_ptr = xlat_cut_block(start_block, size );
331 if( xlat_old_cache_ptr->size == 0 ) {
332 xlat_old_cache_ptr = xlat_old_cache;
337 * Similarly to the above method, promotes a block to temp space.
338 * TODO: Try to combine these - they're nearly identical
340 void xlat_promote_to_temp_space( xlat_cache_block_t block )
342 int size = block->size;
343 int allocation = -sizeof(struct xlat_cache_block);
344 xlat_cache_block_t curr = xlat_temp_cache_ptr;
345 xlat_cache_block_t start_block = curr;
347 if( curr->active == BLOCK_USED ) {
348 xlat_promote_to_old_space( curr );
349 } else if( curr->active == BLOCK_ACTIVE ) {
350 // Active but not used, release block
351 *((uintptr_t *)curr->lut_entry) &= ((uintptr_t)0x03);
353 allocation += curr->size + sizeof(struct xlat_cache_block);
355 if( allocation > size ) {
358 if( curr->size == 0 ) { /* End-of-cache Sentinel */
359 /* Leave what we just released as free space and start again from the
362 start_block->active = 0;
363 start_block->size = allocation;
364 allocation = -sizeof(struct xlat_cache_block);
365 start_block = curr = xlat_temp_cache;
368 start_block->active = 1;
369 start_block->size = allocation;
370 start_block->lut_entry = block->lut_entry;
371 start_block->recover_table_offset = block->recover_table_offset;
372 start_block->recover_table_size = block->recover_table_size;
373 *block->lut_entry = &start_block->code;
374 memcpy( start_block->code, block->code, block->size );
375 xlat_temp_cache_ptr = xlat_cut_block(start_block, size );
376 if( xlat_temp_cache_ptr->size == 0 ) {
377 xlat_temp_cache_ptr = xlat_temp_cache;
383 * Returns the next block in the new cache list that can be written to by the
384 * translator. If the next block is active, it is evicted first.
386 xlat_cache_block_t xlat_start_block( sh4addr_t address )
388 if( xlat_new_cache_ptr->size == 0 ) {
389 xlat_new_cache_ptr = xlat_new_cache;
392 if( xlat_new_cache_ptr->active ) {
393 xlat_promote_to_temp_space( xlat_new_cache_ptr );
395 xlat_new_create_ptr = xlat_new_cache_ptr;
396 xlat_new_create_ptr->active = 1;
397 xlat_new_cache_ptr = NEXT(xlat_new_cache_ptr);
399 /* Add the LUT entry for the block */
400 if( xlat_lut[XLAT_LUT_PAGE(address)] == NULL ) {
401 xlat_lut[XLAT_LUT_PAGE(address)] =
402 mmap( NULL, XLAT_LUT_PAGE_SIZE, PROT_READ|PROT_WRITE,
403 MAP_PRIVATE|MAP_ANON, -1, 0 );
404 memset( xlat_lut[XLAT_LUT_PAGE(address)], 0, XLAT_LUT_PAGE_SIZE );
407 if( IS_ENTRY_POINT(xlat_lut[XLAT_LUT_PAGE(address)][XLAT_LUT_ENTRY(address)]) ) {
408 xlat_cache_block_t oldblock = BLOCK_FOR_CODE(xlat_lut[XLAT_LUT_PAGE(address)][XLAT_LUT_ENTRY(address)]);
409 oldblock->active = 0;
412 xlat_lut[XLAT_LUT_PAGE(address)][XLAT_LUT_ENTRY(address)] =
413 &xlat_new_create_ptr->code;
414 xlat_new_create_ptr->lut_entry = xlat_lut[XLAT_LUT_PAGE(address)] + XLAT_LUT_ENTRY(address);
416 return xlat_new_create_ptr;
419 xlat_cache_block_t xlat_extend_block( uint32_t newSize )
421 while( xlat_new_create_ptr->size < newSize ) {
422 if( xlat_new_cache_ptr->size == 0 ) {
423 /* Migrate to the front of the cache to keep it contiguous */
424 xlat_new_create_ptr->active = 0;
425 sh4ptr_t olddata = xlat_new_create_ptr->code;
426 int oldsize = xlat_new_create_ptr->size;
427 int size = oldsize + MIN_BLOCK_SIZE; /* minimum expansion */
428 void **lut_entry = xlat_new_create_ptr->lut_entry;
429 int allocation = -sizeof(struct xlat_cache_block);
430 xlat_new_cache_ptr = xlat_new_cache;
432 if( xlat_new_cache_ptr->active ) {
433 xlat_promote_to_temp_space( xlat_new_cache_ptr );
435 allocation += xlat_new_cache_ptr->size + sizeof(struct xlat_cache_block);
436 xlat_new_cache_ptr = NEXT(xlat_new_cache_ptr);
437 } while( allocation < size );
438 xlat_new_create_ptr = xlat_new_cache;
439 xlat_new_create_ptr->active = 1;
440 xlat_new_create_ptr->size = allocation;
441 xlat_new_create_ptr->lut_entry = lut_entry;
442 *lut_entry = &xlat_new_create_ptr->code;
443 memmove( xlat_new_create_ptr->code, olddata, oldsize );
445 if( xlat_new_cache_ptr->active ) {
446 xlat_promote_to_temp_space( xlat_new_cache_ptr );
448 xlat_new_create_ptr->size += xlat_new_cache_ptr->size + sizeof(struct xlat_cache_block);
449 xlat_new_cache_ptr = NEXT(xlat_new_cache_ptr);
452 return xlat_new_create_ptr;
456 void xlat_commit_block( uint32_t destsize, uint32_t srcsize )
458 void **ptr = xlat_new_create_ptr->lut_entry;
459 void **endptr = ptr + (srcsize>>2);
460 while( ptr < endptr ) {
462 *ptr = XLAT_LUT_ENTRY_USED;
467 xlat_new_cache_ptr = xlat_cut_block( xlat_new_create_ptr, destsize );
470 void xlat_delete_block( xlat_cache_block_t block )
473 *block->lut_entry = NULL;
476 void xlat_check_cache_integrity( xlat_cache_block_t cache, xlat_cache_block_t ptr, int size )
479 xlat_cache_block_t tail =
480 (xlat_cache_block_t)(((char *)cache) + size - sizeof(struct xlat_cache_block));
482 assert( tail->active == 1 );
483 assert( tail->size == 0 );
484 while( cache < tail ) {
485 assert( cache->active >= 0 && cache->active <= 2 );
486 assert( cache->size >= 0 && cache->size < size );
492 assert( cache == tail );
493 assert( foundptr == 1 || tail == ptr );
496 void xlat_check_integrity( )
498 xlat_check_cache_integrity( xlat_new_cache, xlat_new_cache_ptr, XLAT_NEW_CACHE_SIZE );
499 xlat_check_cache_integrity( xlat_temp_cache, xlat_temp_cache_ptr, XLAT_TEMP_CACHE_SIZE );
500 xlat_check_cache_integrity( xlat_old_cache, xlat_old_cache_ptr, XLAT_OLD_CACHE_SIZE );
.