filename | src/sh4/xltcache.c |
changeset | 569:a1c49e1e8776 |
prev | 561:533f6b478071 |
next | 571:9bc09948d0f2 |
author | nkeynes |
date | Fri Jan 04 11:54:17 2008 +0000 (15 years ago) |
branch | lxdream-mmu |
permissions | -rw-r--r-- |
last change | Bring icache partially into line with the mmu, a little less slow with AT off now. |
view | annotate | diff | log | raw |
1 /**
2 * $Id$
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/types.h>
20 #include <sys/mman.h>
21 #include <assert.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
50 #define BLOCK_USED 2
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)
63 {
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 *) );
80 }
81 xlat_flush_cache();
82 }
84 void xlat_print_free( FILE *out )
85 {
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 );
88 }
90 /**
91 * Reset the cache structure to its default state
92 */
93 void xlat_flush_cache()
94 {
95 xlat_cache_block_t tmp;
96 int i;
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);
101 tmp->active = 1;
102 tmp->size = 0;
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);
107 tmp->active = 1;
108 tmp->size = 0;
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);
113 tmp->active = 1;
114 tmp->size = 0;
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 );
118 }
119 }
120 }
122 static void xlat_flush_page_by_lut( void **page )
123 {
124 int i;
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;
128 }
129 page[i] = NULL;
130 }
131 }
133 void xlat_invalidate_word( sh4addr_t addr )
134 {
135 if( xlat_lut ) {
136 void **page = xlat_lut[XLAT_LUT_PAGE(addr)];
137 if( page != NULL ) {
138 int entry = XLAT_LUT_ENTRY(addr);
139 if( page[entry] != NULL ) {
140 xlat_flush_page_by_lut(page);
141 }
142 }
143 }
144 }
146 void xlat_invalidate_long( sh4addr_t addr )
147 {
148 if( xlat_lut ) {
149 void **page = xlat_lut[XLAT_LUT_PAGE(addr)];
150 if( page != NULL ) {
151 int entry = XLAT_LUT_ENTRY(addr);
152 if( page[entry] != NULL || page[entry+1] != NULL ) {
153 xlat_flush_page_by_lut(page);
154 }
155 }
156 }
157 }
159 void xlat_invalidate_block( sh4addr_t address, size_t size )
160 {
161 int i;
162 int entry_count = size >> 1; // words;
163 uint32_t page_no = XLAT_LUT_PAGE(address);
164 int entry = XLAT_LUT_ENTRY(address);
165 if( xlat_lut ) {
166 do {
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;
171 }
172 if( page != NULL ) {
173 if( page_entries == XLAT_LUT_PAGE_ENTRIES ) {
174 /* Overwriting the entire page anyway */
175 xlat_flush_page_by_lut(page);
176 } else {
177 for( i=entry; i<entry+page_entries; i++ ) {
178 if( page[i] != NULL ) {
179 xlat_flush_page_by_lut(page);
180 break;
181 }
182 }
183 }
184 entry_count -= page_entries;
185 }
186 page_no ++;
187 entry_count -= page_entries;
188 entry = 0;
189 } while( entry_count > 0 );
190 }
191 }
193 void xlat_flush_page( sh4addr_t address )
194 {
195 void **page = xlat_lut[XLAT_LUT_PAGE(address)];
196 if( page != NULL ) {
197 xlat_flush_page_by_lut(page);
198 }
199 }
201 void *xlat_get_code( sh4addr_t address )
202 {
203 void *result = NULL;
204 void **page = xlat_lut[XLAT_LUT_PAGE(address)];
205 if( page != NULL ) {
206 result = (void *)(((uintptr_t)(page[XLAT_LUT_ENTRY(address)])) & (~((uintptr_t)0x03)));
207 }
208 return result;
209 }
211 void *xlat_get_code_by_vma( sh4vma_t vma )
212 {
213 void *result = NULL;
216 if( !IS_IN_ICACHE(vma) ) {
217 if( !mmu_update_icache(sh4r.pc) ) {
218 // fault - off to the fault handler
219 if( !mmu_update_icache(sh4r.pc) ) {
220 // double fault - halt
221 dreamcast_stop();
222 ERROR( "Double fault - halting" );
223 return NULL;
224 }
225 }
226 }
227 if( sh4_icache.page_vma != -1 ) {
228 result = xlat_get_code( GET_ICACHE_PHYS(vma) );
229 }
231 return result;
232 }
234 void **xlat_get_lut_entry( sh4addr_t address )
235 {
236 void **page = xlat_lut[XLAT_LUT_PAGE(address)];
238 /* Add the LUT entry for the block */
239 if( page == NULL ) {
240 xlat_lut[XLAT_LUT_PAGE(address)] = page =
241 mmap( NULL, XLAT_LUT_PAGE_SIZE, PROT_READ|PROT_WRITE,
242 MAP_PRIVATE|MAP_ANON, -1, 0 );
243 memset( page, 0, XLAT_LUT_PAGE_SIZE );
244 }
246 return &page[XLAT_LUT_ENTRY(address)];
247 }
251 uint32_t xlat_get_block_size( void *block )
252 {
253 xlat_cache_block_t xlt = (xlat_cache_block_t)(((char *)block)-sizeof(struct xlat_cache_block));
254 return xlt->size;
255 }
257 /**
258 * Cut the specified block so that it has the given size, with the remaining data
259 * forming a new free block. If the free block would be less than the minimum size,
260 * the cut is not performed.
261 * @return the next block after the (possibly cut) block.
262 */
263 static inline xlat_cache_block_t xlat_cut_block( xlat_cache_block_t block, int cutsize )
264 {
265 cutsize = (cutsize + 3) & 0xFFFFFFFC; // force word alignment
266 assert( cutsize <= block->size );
267 if( block->size > cutsize + MIN_TOTAL_SIZE ) {
268 int oldsize = block->size;
269 block->size = cutsize;
270 xlat_cache_block_t next = NEXT(block);
271 next->active = 0;
272 next->size = oldsize - cutsize - sizeof(struct xlat_cache_block);
273 return next;
274 } else {
275 return NEXT(block);
276 }
277 }
279 /**
280 * Promote a block in temp space (or elsewhere for that matter) to old space.
281 *
282 * @param block to promote.
283 */
284 static void xlat_promote_to_old_space( xlat_cache_block_t block )
285 {
286 int allocation = -sizeof(struct xlat_cache_block);
287 int size = block->size;
288 xlat_cache_block_t curr = xlat_old_cache_ptr;
289 xlat_cache_block_t start_block = curr;
290 do {
291 allocation += curr->size + sizeof(struct xlat_cache_block);
292 curr = NEXT(curr);
293 if( allocation > size ) {
294 break; /* done */
295 }
296 if( curr->size == 0 ) { /* End-of-cache Sentinel */
297 /* Leave what we just released as free space and start again from the
298 * top of the cache
299 */
300 start_block->active = 0;
301 start_block->size = allocation;
302 allocation = -sizeof(struct xlat_cache_block);
303 start_block = curr = xlat_old_cache;
304 }
305 } while(1);
306 start_block->active = 1;
307 start_block->size = allocation;
308 start_block->lut_entry = block->lut_entry;
309 *block->lut_entry = &start_block->code;
310 memcpy( start_block->code, block->code, block->size );
311 xlat_old_cache_ptr = xlat_cut_block(start_block, size );
312 if( xlat_old_cache_ptr->size == 0 ) {
313 xlat_old_cache_ptr = xlat_old_cache;
314 }
315 }
317 /**
318 * Similarly to the above method, promotes a block to temp space.
319 * TODO: Try to combine these - they're nearly identical
320 */
321 void xlat_promote_to_temp_space( xlat_cache_block_t block )
322 {
323 int size = block->size;
324 int allocation = -sizeof(struct xlat_cache_block);
325 xlat_cache_block_t curr = xlat_temp_cache_ptr;
326 xlat_cache_block_t start_block = curr;
327 do {
328 if( curr->active == BLOCK_USED ) {
329 xlat_promote_to_old_space( curr );
330 }
331 allocation += curr->size + sizeof(struct xlat_cache_block);
332 curr = NEXT(curr);
333 if( allocation > size ) {
334 break; /* done */
335 }
336 if( curr->size == 0 ) { /* End-of-cache Sentinel */
337 /* Leave what we just released as free space and start again from the
338 * top of the cache
339 */
340 start_block->active = 0;
341 start_block->size = allocation;
342 allocation = -sizeof(struct xlat_cache_block);
343 start_block = curr = xlat_temp_cache;
344 }
345 } while(1);
346 start_block->active = 1;
347 start_block->size = allocation;
348 start_block->lut_entry = block->lut_entry;
349 *block->lut_entry = &start_block->code;
350 memcpy( start_block->code, block->code, block->size );
351 xlat_temp_cache_ptr = xlat_cut_block(start_block, size );
352 if( xlat_temp_cache_ptr->size == 0 ) {
353 xlat_temp_cache_ptr = xlat_temp_cache;
354 }
356 }
358 /**
359 * Returns the next block in the new cache list that can be written to by the
360 * translator. If the next block is active, it is evicted first.
361 */
362 xlat_cache_block_t xlat_start_block( sh4addr_t address )
363 {
364 if( xlat_new_cache_ptr->size == 0 ) {
365 xlat_new_cache_ptr = xlat_new_cache;
366 }
368 if( xlat_new_cache_ptr->active ) {
369 xlat_promote_to_temp_space( xlat_new_cache_ptr );
370 }
371 xlat_new_create_ptr = xlat_new_cache_ptr;
372 xlat_new_create_ptr->active = 1;
373 xlat_new_cache_ptr = NEXT(xlat_new_cache_ptr);
375 /* Add the LUT entry for the block */
376 if( xlat_lut[XLAT_LUT_PAGE(address)] == NULL ) {
377 xlat_lut[XLAT_LUT_PAGE(address)] =
378 mmap( NULL, XLAT_LUT_PAGE_SIZE, PROT_READ|PROT_WRITE,
379 MAP_PRIVATE|MAP_ANON, -1, 0 );
380 memset( xlat_lut[XLAT_LUT_PAGE(address)], 0, XLAT_LUT_PAGE_SIZE );
381 }
383 if( IS_ENTRY_POINT(xlat_lut[XLAT_LUT_PAGE(address)][XLAT_LUT_ENTRY(address)]) ) {
384 xlat_cache_block_t oldblock = BLOCK_FOR_CODE(xlat_lut[XLAT_LUT_PAGE(address)][XLAT_LUT_ENTRY(address)]);
385 oldblock->active = 0;
386 }
388 xlat_lut[XLAT_LUT_PAGE(address)][XLAT_LUT_ENTRY(address)] =
389 &xlat_new_create_ptr->code;
390 xlat_new_create_ptr->lut_entry = xlat_lut[XLAT_LUT_PAGE(address)] + XLAT_LUT_ENTRY(address);
392 return xlat_new_create_ptr;
393 }
395 xlat_cache_block_t xlat_extend_block( uint32_t newSize )
396 {
397 while( xlat_new_create_ptr->size < newSize ) {
398 if( xlat_new_cache_ptr->size == 0 ) {
399 /* Migrate to the front of the cache to keep it contiguous */
400 xlat_new_create_ptr->active = 0;
401 sh4ptr_t olddata = xlat_new_create_ptr->code;
402 int oldsize = xlat_new_create_ptr->size;
403 int size = oldsize + MIN_BLOCK_SIZE; /* minimum expansion */
404 void **lut_entry = xlat_new_create_ptr->lut_entry;
405 int allocation = -sizeof(struct xlat_cache_block);
406 xlat_new_cache_ptr = xlat_new_cache;
407 do {
408 if( xlat_new_cache_ptr->active ) {
409 xlat_promote_to_temp_space( xlat_new_cache_ptr );
410 }
411 allocation += xlat_new_cache_ptr->size + sizeof(struct xlat_cache_block);
412 xlat_new_cache_ptr = NEXT(xlat_new_cache_ptr);
413 } while( allocation < size );
414 xlat_new_create_ptr = xlat_new_cache;
415 xlat_new_create_ptr->active = 1;
416 xlat_new_create_ptr->size = allocation;
417 xlat_new_create_ptr->lut_entry = lut_entry;
418 *lut_entry = &xlat_new_create_ptr->code;
419 memmove( xlat_new_create_ptr->code, olddata, oldsize );
420 } else {
421 if( xlat_new_cache_ptr->active ) {
422 xlat_promote_to_temp_space( xlat_new_cache_ptr );
423 }
424 xlat_new_create_ptr->size += xlat_new_cache_ptr->size + sizeof(struct xlat_cache_block);
425 xlat_new_cache_ptr = NEXT(xlat_new_cache_ptr);
426 }
427 }
428 return xlat_new_create_ptr;
430 }
432 void xlat_commit_block( uint32_t destsize, uint32_t srcsize )
433 {
434 void **ptr = xlat_new_create_ptr->lut_entry;
435 void **endptr = ptr + (srcsize>>2);
436 while( ptr < endptr ) {
437 if( *ptr == NULL ) {
438 *ptr = XLAT_LUT_ENTRY_USED;
439 }
440 ptr++;
441 }
443 xlat_new_cache_ptr = xlat_cut_block( xlat_new_create_ptr, destsize );
444 }
446 void xlat_delete_block( xlat_cache_block_t block )
447 {
448 block->active = 0;
449 *block->lut_entry = NULL;
450 }
452 void xlat_check_cache_integrity( xlat_cache_block_t cache, xlat_cache_block_t ptr, int size )
453 {
454 int foundptr = 0;
455 xlat_cache_block_t tail =
456 (xlat_cache_block_t)(((char *)cache) + size - sizeof(struct xlat_cache_block));
458 assert( tail->active == 1 );
459 assert( tail->size == 0 );
460 while( cache < tail ) {
461 assert( cache->active >= 0 && cache->active <= 2 );
462 assert( cache->size >= 0 && cache->size < size );
463 if( cache == ptr ) {
464 foundptr = 1;
465 }
466 cache = NEXT(cache);
467 }
468 assert( cache == tail );
469 assert( foundptr == 1 );
470 }
472 void xlat_check_integrity( )
473 {
474 xlat_check_cache_integrity( xlat_new_cache, xlat_new_cache_ptr, XLAT_NEW_CACHE_SIZE );
475 xlat_check_cache_integrity( xlat_temp_cache, xlat_temp_cache_ptr, XLAT_TEMP_CACHE_SIZE );
476 xlat_check_cache_integrity( xlat_old_cache, xlat_old_cache_ptr, XLAT_OLD_CACHE_SIZE );
477 }
.