2 * $Id: xltcache.c,v 1.11 2007-11-08 11:54:16 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.
19 #include <sys/types.h>
23 #include "dreamcast.h"
24 #include "sh4/xltcache.h"
25 #include "x86dasm/x86dasm.h"
27 #define XLAT_LUT_PAGE_BITS 12
28 #define XLAT_LUT_TOTAL_BITS 28
29 #define XLAT_LUT_PAGE(addr) (((addr)>>13) & 0xFFFF)
30 #define XLAT_LUT_ENTRY(addr) (((addr)&0x1FFE) >> 1)
32 #define XLAT_LUT_PAGES (1<<(XLAT_LUT_TOTAL_BITS-XLAT_LUT_PAGE_BITS))
33 #define XLAT_LUT_PAGE_ENTRIES (1<<XLAT_LUT_PAGE_BITS)
34 #define XLAT_LUT_PAGE_SIZE (XLAT_LUT_PAGE_ENTRIES * sizeof(void *))
36 #define XLAT_LUT_ENTRY_EMPTY (void *)0
37 #define XLAT_LUT_ENTRY_USED (void *)1
39 #define NEXT(block) ( (xlat_cache_block_t)&((block)->code[(block)->size]))
40 #define BLOCK_FOR_CODE(code) (((xlat_cache_block_t)code)-1)
41 #define IS_ENTRY_POINT(ent) (ent > XLAT_LUT_ENTRY_USED)
42 #define IS_ENTRY_USED(ent) (ent != XLAT_LUT_ENTRY_EMPTY)
44 #define MIN_BLOCK_SIZE 32
45 #define MIN_TOTAL_SIZE (sizeof(struct xlat_cache_block)+MIN_BLOCK_SIZE)
47 #define BLOCK_INACTIVE 0
48 #define BLOCK_ACTIVE 1
51 xlat_cache_block_t xlat_new_cache;
52 xlat_cache_block_t xlat_new_cache_ptr;
53 xlat_cache_block_t xlat_new_create_ptr;
54 xlat_cache_block_t xlat_temp_cache;
55 xlat_cache_block_t xlat_temp_cache_ptr;
56 xlat_cache_block_t xlat_old_cache;
57 xlat_cache_block_t xlat_old_cache_ptr;
58 static void ***xlat_lut;
59 static gboolean xlat_initialized = FALSE;
61 void xlat_cache_init(void)
63 if( !xlat_initialized ) {
64 xlat_initialized = TRUE;
65 xlat_new_cache = mmap( NULL, XLAT_NEW_CACHE_SIZE, PROT_EXEC|PROT_READ|PROT_WRITE,
66 MAP_PRIVATE|MAP_ANON, -1, 0 );
67 xlat_temp_cache = mmap( NULL, XLAT_TEMP_CACHE_SIZE, PROT_EXEC|PROT_READ|PROT_WRITE,
68 MAP_PRIVATE|MAP_ANON, -1, 0 );
69 xlat_old_cache = mmap( NULL, XLAT_OLD_CACHE_SIZE, PROT_EXEC|PROT_READ|PROT_WRITE,
70 MAP_PRIVATE|MAP_ANON, -1, 0 );
71 xlat_new_cache_ptr = xlat_new_cache;
72 xlat_temp_cache_ptr = xlat_temp_cache;
73 xlat_old_cache_ptr = xlat_old_cache;
74 xlat_new_create_ptr = xlat_new_cache;
76 xlat_lut = mmap( NULL, XLAT_LUT_PAGES*sizeof(void *), PROT_READ|PROT_WRITE,
77 MAP_PRIVATE|MAP_ANON, -1, 0);
78 memset( xlat_lut, 0, XLAT_LUT_PAGES*sizeof(void *) );
83 void xlat_print_free( FILE *out )
85 fprintf( out, "New space: %d\nTemp space: %d\nOld space: %d\n",
86 xlat_new_cache_ptr->size, xlat_temp_cache_ptr->size, xlat_old_cache_ptr->size );
90 * Reset the cache structure to its default state
92 void xlat_flush_cache()
94 xlat_cache_block_t tmp;
96 xlat_new_cache_ptr = xlat_new_cache;
97 xlat_new_cache_ptr->active = 0;
98 xlat_new_cache_ptr->size = XLAT_NEW_CACHE_SIZE - 2*sizeof(struct xlat_cache_block);
99 tmp = NEXT(xlat_new_cache_ptr);
102 xlat_temp_cache_ptr = xlat_temp_cache;
103 xlat_temp_cache_ptr->active = 0;
104 xlat_temp_cache_ptr->size = XLAT_TEMP_CACHE_SIZE - 2*sizeof(struct xlat_cache_block);
105 tmp = NEXT(xlat_temp_cache_ptr);
108 xlat_old_cache_ptr = xlat_old_cache;
109 xlat_old_cache_ptr->active = 0;
110 xlat_old_cache_ptr->size = XLAT_OLD_CACHE_SIZE - 2*sizeof(struct xlat_cache_block);
111 tmp = NEXT(xlat_old_cache_ptr);
114 for( i=0; i<XLAT_LUT_PAGES; i++ ) {
115 if( xlat_lut[i] != NULL ) {
116 memset( xlat_lut[i], 0, XLAT_LUT_PAGE_SIZE );
121 static void xlat_flush_page_by_lut( void **page )
124 for( i=0; i<XLAT_LUT_PAGE_ENTRIES; i++ ) {
125 if( IS_ENTRY_POINT(page[i]) ) {
126 BLOCK_FOR_CODE(page[i])->active = 0;
132 void xlat_invalidate_word( sh4addr_t addr )
135 void **page = xlat_lut[XLAT_LUT_PAGE(addr)];
137 int entry = XLAT_LUT_ENTRY(addr);
138 if( page[entry] != NULL ) {
139 xlat_flush_page_by_lut(page);
145 void xlat_invalidate_long( sh4addr_t addr )
148 void **page = xlat_lut[XLAT_LUT_PAGE(addr)];
150 int entry = XLAT_LUT_ENTRY(addr);
151 if( page[entry] != NULL || page[entry+1] != NULL ) {
152 xlat_flush_page_by_lut(page);
158 void xlat_invalidate_block( sh4addr_t address, size_t size )
161 int entry_count = size >> 1; // words;
162 uint32_t page_no = XLAT_LUT_PAGE(address);
163 int entry = XLAT_LUT_ENTRY(address);
166 void **page = xlat_lut[page_no];
167 int page_entries = XLAT_LUT_PAGE_ENTRIES - entry;
168 if( entry_count < page_entries ) {
169 page_entries = entry_count;
172 if( page_entries == XLAT_LUT_PAGE_ENTRIES ) {
173 /* Overwriting the entire page anyway */
174 xlat_flush_page_by_lut(page);
176 for( i=entry; i<entry+page_entries; i++ ) {
177 if( page[i] != NULL ) {
178 xlat_flush_page_by_lut(page);
183 entry_count -= page_entries;
186 entry_count -= page_entries;
188 } while( entry_count > 0 );
192 void xlat_flush_page( sh4addr_t address )
194 void **page = xlat_lut[XLAT_LUT_PAGE(address)];
196 xlat_flush_page_by_lut(page);
200 void *xlat_get_code( sh4addr_t address )
203 void **page = xlat_lut[XLAT_LUT_PAGE(address)];
205 result = (void *)(((uintptr_t)(page[XLAT_LUT_ENTRY(address)])) & (~((uintptr_t)0x03)));
210 void **xlat_get_lut_entry( sh4addr_t address )
212 void **page = xlat_lut[XLAT_LUT_PAGE(address)];
214 /* Add the LUT entry for the block */
216 xlat_lut[XLAT_LUT_PAGE(address)] = page =
217 mmap( NULL, XLAT_LUT_PAGE_SIZE, PROT_READ|PROT_WRITE,
218 MAP_PRIVATE|MAP_ANON, -1, 0 );
219 memset( page, 0, XLAT_LUT_PAGE_SIZE );
222 return &page[XLAT_LUT_ENTRY(address)];
227 uint32_t xlat_get_block_size( void *block )
229 xlat_cache_block_t xlt = (xlat_cache_block_t)(((char *)block)-sizeof(struct xlat_cache_block));
234 * Cut the specified block so that it has the given size, with the remaining data
235 * forming a new free block. If the free block would be less than the minimum size,
236 * the cut is not performed.
237 * @return the next block after the (possibly cut) block.
239 static inline xlat_cache_block_t xlat_cut_block( xlat_cache_block_t block, int cutsize )
241 cutsize = (cutsize + 3) & 0xFFFFFFFC; // force word alignment
242 assert( cutsize <= block->size );
243 if( block->size > cutsize + MIN_TOTAL_SIZE ) {
244 int oldsize = block->size;
245 block->size = cutsize;
246 xlat_cache_block_t next = NEXT(block);
248 next->size = oldsize - cutsize - sizeof(struct xlat_cache_block);
256 * Promote a block in temp space (or elsewhere for that matter) to old space.
258 * @param block to promote.
260 static void xlat_promote_to_old_space( xlat_cache_block_t block )
262 int allocation = -sizeof(struct xlat_cache_block);
263 int size = block->size;
264 xlat_cache_block_t curr = xlat_old_cache_ptr;
265 xlat_cache_block_t start_block = curr;
267 allocation += curr->size + sizeof(struct xlat_cache_block);
269 if( allocation > size ) {
272 if( curr->size == 0 ) { /* End-of-cache Sentinel */
273 /* Leave what we just released as free space and start again from the
276 start_block->active = 0;
277 start_block->size = allocation;
278 allocation = -sizeof(struct xlat_cache_block);
279 start_block = curr = xlat_old_cache;
282 start_block->active = 1;
283 start_block->size = allocation;
284 start_block->lut_entry = block->lut_entry;
285 *block->lut_entry = &start_block->code;
286 memcpy( start_block->code, block->code, block->size );
287 xlat_old_cache_ptr = xlat_cut_block(start_block, size );
288 if( xlat_old_cache_ptr->size == 0 ) {
289 xlat_old_cache_ptr = xlat_old_cache;
294 * Similarly to the above method, promotes a block to temp space.
295 * TODO: Try to combine these - they're nearly identical
297 void xlat_promote_to_temp_space( xlat_cache_block_t block )
299 int size = block->size;
300 int allocation = -sizeof(struct xlat_cache_block);
301 xlat_cache_block_t curr = xlat_temp_cache_ptr;
302 xlat_cache_block_t start_block = curr;
304 if( curr->active == BLOCK_USED ) {
305 xlat_promote_to_old_space( curr );
307 allocation += curr->size + sizeof(struct xlat_cache_block);
309 if( allocation > size ) {
312 if( curr->size == 0 ) { /* End-of-cache Sentinel */
313 /* Leave what we just released as free space and start again from the
316 start_block->active = 0;
317 start_block->size = allocation;
318 allocation = -sizeof(struct xlat_cache_block);
319 start_block = curr = xlat_temp_cache;
322 start_block->active = 1;
323 start_block->size = allocation;
324 start_block->lut_entry = block->lut_entry;
325 *block->lut_entry = &start_block->code;
326 memcpy( start_block->code, block->code, block->size );
327 xlat_temp_cache_ptr = xlat_cut_block(start_block, size );
328 if( xlat_temp_cache_ptr->size == 0 ) {
329 xlat_temp_cache_ptr = xlat_temp_cache;
335 * Returns the next block in the new cache list that can be written to by the
336 * translator. If the next block is active, it is evicted first.
338 xlat_cache_block_t xlat_start_block( sh4addr_t address )
340 if( xlat_new_cache_ptr->size == 0 ) {
341 xlat_new_cache_ptr = xlat_new_cache;
344 if( xlat_new_cache_ptr->active ) {
345 xlat_promote_to_temp_space( xlat_new_cache_ptr );
347 xlat_new_create_ptr = xlat_new_cache_ptr;
348 xlat_new_create_ptr->active = 1;
349 xlat_new_cache_ptr = NEXT(xlat_new_cache_ptr);
351 /* Add the LUT entry for the block */
352 if( xlat_lut[XLAT_LUT_PAGE(address)] == NULL ) {
353 xlat_lut[XLAT_LUT_PAGE(address)] =
354 mmap( NULL, XLAT_LUT_PAGE_SIZE, PROT_READ|PROT_WRITE,
355 MAP_PRIVATE|MAP_ANON, -1, 0 );
356 memset( xlat_lut[XLAT_LUT_PAGE(address)], 0, XLAT_LUT_PAGE_SIZE );
359 if( IS_ENTRY_POINT(xlat_lut[XLAT_LUT_PAGE(address)][XLAT_LUT_ENTRY(address)]) ) {
360 xlat_cache_block_t oldblock = BLOCK_FOR_CODE(xlat_lut[XLAT_LUT_PAGE(address)][XLAT_LUT_ENTRY(address)]);
361 oldblock->active = 0;
364 xlat_lut[XLAT_LUT_PAGE(address)][XLAT_LUT_ENTRY(address)] =
365 &xlat_new_create_ptr->code;
366 xlat_new_create_ptr->lut_entry = xlat_lut[XLAT_LUT_PAGE(address)] + XLAT_LUT_ENTRY(address);
368 return xlat_new_create_ptr;
371 xlat_cache_block_t xlat_extend_block( uint32_t newSize )
373 while( xlat_new_create_ptr->size < newSize ) {
374 if( xlat_new_cache_ptr->size == 0 ) {
375 /* Migrate to the front of the cache to keep it contiguous */
376 xlat_new_create_ptr->active = 0;
377 sh4ptr_t olddata = xlat_new_create_ptr->code;
378 int oldsize = xlat_new_create_ptr->size;
379 int size = oldsize + MIN_BLOCK_SIZE; /* minimum expansion */
380 void **lut_entry = xlat_new_create_ptr->lut_entry;
381 int allocation = -sizeof(struct xlat_cache_block);
382 xlat_new_cache_ptr = xlat_new_cache;
384 if( xlat_new_cache_ptr->active ) {
385 xlat_promote_to_temp_space( xlat_new_cache_ptr );
387 allocation += xlat_new_cache_ptr->size + sizeof(struct xlat_cache_block);
388 xlat_new_cache_ptr = NEXT(xlat_new_cache_ptr);
389 } while( allocation < size );
390 xlat_new_create_ptr = xlat_new_cache;
391 xlat_new_create_ptr->active = 1;
392 xlat_new_create_ptr->size = allocation;
393 xlat_new_create_ptr->lut_entry = lut_entry;
394 *lut_entry = &xlat_new_create_ptr->code;
395 memmove( xlat_new_create_ptr->code, olddata, oldsize );
397 if( xlat_new_cache_ptr->active ) {
398 xlat_promote_to_temp_space( xlat_new_cache_ptr );
400 xlat_new_create_ptr->size += xlat_new_cache_ptr->size + sizeof(struct xlat_cache_block);
401 xlat_new_cache_ptr = NEXT(xlat_new_cache_ptr);
404 return xlat_new_create_ptr;
408 void xlat_commit_block( uint32_t destsize, uint32_t srcsize )
410 void **ptr = xlat_new_create_ptr->lut_entry;
411 void **endptr = ptr + (srcsize>>2);
412 while( ptr < endptr ) {
414 *ptr = XLAT_LUT_ENTRY_USED;
419 xlat_new_cache_ptr = xlat_cut_block( xlat_new_create_ptr, destsize );
422 void xlat_delete_block( xlat_cache_block_t block )
425 *block->lut_entry = NULL;
428 void xlat_check_cache_integrity( xlat_cache_block_t cache, xlat_cache_block_t ptr, int size )
431 xlat_cache_block_t tail =
432 (xlat_cache_block_t)(((char *)cache) + size - sizeof(struct xlat_cache_block));
434 assert( tail->active == 1 );
435 assert( tail->size == 0 );
436 while( cache < tail ) {
437 assert( cache->active >= 0 && cache->active <= 2 );
438 assert( cache->size >= 0 && cache->size < size );
444 assert( cache == tail );
445 assert( foundptr == 1 );
448 void xlat_check_integrity( )
450 xlat_check_cache_integrity( xlat_new_cache, xlat_new_cache_ptr, XLAT_NEW_CACHE_SIZE );
451 xlat_check_cache_integrity( xlat_temp_cache, xlat_temp_cache_ptr, XLAT_TEMP_CACHE_SIZE );
452 xlat_check_cache_integrity( xlat_old_cache, xlat_old_cache_ptr, XLAT_OLD_CACHE_SIZE );
.