filename | src/sh4/xltcache.c |
changeset | 922:8a8361264b1e |
prev | 905:4c17ebd9ef5e |
next | 935:45246788ca00 |
next | 953:f4a156508ad1 |
author | nkeynes |
date | Sun Dec 14 07:50:48 2008 +0000 (15 years ago) |
permissions | -rw-r--r-- |
last change | Setup a 'proper' stackframe in translated blocks. This doesn't affect performance noticeably, but does ensure that a) The stack is aligned correctly on OS X with no extra effort, and b) We can't mess up the stack and crash that way anymore. Replace all PUSH/POP instructions (outside of prologue/epilogue) with ESP-rel moves to stack local variables. Finally merge ia32mac and ia32abi together, since they're pretty much the same now anyway (and thereby simplifying maintenance a good deal) |
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 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
49 #define BLOCK_USED 2
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;
55 #ifdef XLAT_GENERATIONAL_CACHE
56 xlat_cache_block_t xlat_temp_cache;
57 xlat_cache_block_t xlat_temp_cache_ptr;
58 xlat_cache_block_t xlat_old_cache;
59 xlat_cache_block_t xlat_old_cache_ptr;
60 #endif
62 static void ***xlat_lut;
63 static gboolean xlat_initialized = FALSE;
65 void xlat_cache_init(void)
66 {
67 if( !xlat_initialized ) {
68 xlat_initialized = TRUE;
69 xlat_new_cache = mmap( NULL, XLAT_NEW_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_new_create_ptr = xlat_new_cache;
73 #ifdef XLAT_GENERATIONAL_CACHE
74 xlat_temp_cache = mmap( NULL, XLAT_TEMP_CACHE_SIZE, PROT_EXEC|PROT_READ|PROT_WRITE,
75 MAP_PRIVATE|MAP_ANON, -1, 0 );
76 xlat_old_cache = mmap( NULL, XLAT_OLD_CACHE_SIZE, PROT_EXEC|PROT_READ|PROT_WRITE,
77 MAP_PRIVATE|MAP_ANON, -1, 0 );
78 xlat_temp_cache_ptr = xlat_temp_cache;
79 xlat_old_cache_ptr = xlat_old_cache;
80 #endif
81 xlat_lut = mmap( NULL, XLAT_LUT_PAGES*sizeof(void *), PROT_READ|PROT_WRITE,
82 MAP_PRIVATE|MAP_ANON, -1, 0);
83 memset( xlat_lut, 0, XLAT_LUT_PAGES*sizeof(void *) );
84 }
85 xlat_flush_cache();
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 #ifdef XLAT_GENERATIONAL_CACHE
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);
106 tmp->active = 1;
107 tmp->size = 0;
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);
112 tmp->active = 1;
113 tmp->size = 0;
114 #endif
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 XLAT_BLOCK_FOR_CODE(page[i])->active = 0;
128 }
129 page[i] = NULL;
130 }
131 }
133 void FASTCALL 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 FASTCALL 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 FASTCALL 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 FASTCALL 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 * FASTCALL 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 xlat_recovery_record_t xlat_get_post_recovery( void *code, void *native_pc, gboolean with_terminal )
212 {
213 if( code != NULL ) {
214 uintptr_t pc_offset = ((uint8_t *)native_pc) - ((uint8_t *)code);
215 xlat_cache_block_t block = XLAT_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]);
218 uint32_t posn;
219 if( count > 0 && !with_terminal )
220 count--;
221 if( records[count-1].xlat_offset < pc_offset ) {
222 return NULL;
223 }
224 for( posn=count-1; posn > 0; posn-- ) {
225 if( records[posn-1].xlat_offset < pc_offset ) {
226 return &records[posn];
227 }
228 }
229 return &records[0]; // shouldn't happen
230 }
231 return NULL;
232 }
234 xlat_recovery_record_t xlat_get_pre_recovery( void *code, void *native_pc )
235 {
236 if( code != NULL ) {
237 uintptr_t pc_offset = ((uint8_t *)native_pc) - ((uint8_t *)code);
238 xlat_cache_block_t block = XLAT_BLOCK_FOR_CODE(code);
239 uint32_t count = block->recover_table_size;
240 xlat_recovery_record_t records = (xlat_recovery_record_t)(&block->code[block->recover_table_offset]);
241 uint32_t posn;
242 for( posn = 1; posn < count; posn++ ) {
243 if( records[posn].xlat_offset >= pc_offset ) {
244 return &records[posn-1];
245 }
246 }
247 return &records[count-1];
248 }
249 return NULL;
250 }
252 void ** FASTCALL xlat_get_lut_entry( sh4addr_t address )
253 {
254 void **page = xlat_lut[XLAT_LUT_PAGE(address)];
256 /* Add the LUT entry for the block */
257 if( page == NULL ) {
258 xlat_lut[XLAT_LUT_PAGE(address)] = page =
259 mmap( NULL, XLAT_LUT_PAGE_SIZE, PROT_READ|PROT_WRITE,
260 MAP_PRIVATE|MAP_ANON, -1, 0 );
261 memset( page, 0, XLAT_LUT_PAGE_SIZE );
262 }
264 return &page[XLAT_LUT_ENTRY(address)];
265 }
269 uint32_t FASTCALL xlat_get_block_size( void *block )
270 {
271 xlat_cache_block_t xlt = (xlat_cache_block_t)(((char *)block)-sizeof(struct xlat_cache_block));
272 return xlt->size;
273 }
275 uint32_t FASTCALL xlat_get_code_size( void *block )
276 {
277 xlat_cache_block_t xlt = (xlat_cache_block_t)(((char *)block)-sizeof(struct xlat_cache_block));
278 if( xlt->recover_table_offset == 0 ) {
279 return xlt->size;
280 } else {
281 return xlt->recover_table_offset;
282 }
283 }
285 /**
286 * Cut the specified block so that it has the given size, with the remaining data
287 * forming a new free block. If the free block would be less than the minimum size,
288 * the cut is not performed.
289 * @return the next block after the (possibly cut) block.
290 */
291 static inline xlat_cache_block_t xlat_cut_block( xlat_cache_block_t block, int cutsize )
292 {
293 cutsize = (cutsize + 3) & 0xFFFFFFFC; // force word alignment
294 assert( cutsize <= block->size );
295 if( block->size > cutsize + MIN_TOTAL_SIZE ) {
296 int oldsize = block->size;
297 block->size = cutsize;
298 xlat_cache_block_t next = NEXT(block);
299 next->active = 0;
300 next->size = oldsize - cutsize - sizeof(struct xlat_cache_block);
301 return next;
302 } else {
303 return NEXT(block);
304 }
305 }
307 #ifdef XLAT_GENERATIONAL_CACHE
308 /**
309 * Promote a block in temp space (or elsewhere for that matter) to old space.
310 *
311 * @param block to promote.
312 */
313 static void xlat_promote_to_old_space( xlat_cache_block_t block )
314 {
315 int allocation = (int)-sizeof(struct xlat_cache_block);
316 int size = block->size;
317 xlat_cache_block_t curr = xlat_old_cache_ptr;
318 xlat_cache_block_t start_block = curr;
319 do {
320 allocation += curr->size + sizeof(struct xlat_cache_block);
321 curr = NEXT(curr);
322 if( allocation > size ) {
323 break; /* done */
324 }
325 if( curr->size == 0 ) { /* End-of-cache Sentinel */
326 /* Leave what we just released as free space and start again from the
327 * top of the cache
328 */
329 start_block->active = 0;
330 start_block->size = allocation;
331 allocation = (int)-sizeof(struct xlat_cache_block);
332 start_block = curr = xlat_old_cache;
333 }
334 } while(1);
335 start_block->active = 1;
336 start_block->size = allocation;
337 start_block->lut_entry = block->lut_entry;
338 start_block->fpscr_mask = block->fpscr_mask;
339 start_block->fpscr = block->fpscr;
340 start_block->recover_table_offset = block->recover_table_offset;
341 start_block->recover_table_size = block->recover_table_size;
342 *block->lut_entry = &start_block->code;
343 memcpy( start_block->code, block->code, block->size );
344 xlat_old_cache_ptr = xlat_cut_block(start_block, size );
345 if( xlat_old_cache_ptr->size == 0 ) {
346 xlat_old_cache_ptr = xlat_old_cache;
347 }
348 }
350 /**
351 * Similarly to the above method, promotes a block to temp space.
352 * TODO: Try to combine these - they're nearly identical
353 */
354 void xlat_promote_to_temp_space( xlat_cache_block_t block )
355 {
356 int size = block->size;
357 int allocation = (int)-sizeof(struct xlat_cache_block);
358 xlat_cache_block_t curr = xlat_temp_cache_ptr;
359 xlat_cache_block_t start_block = curr;
360 do {
361 if( curr->active == BLOCK_USED ) {
362 xlat_promote_to_old_space( curr );
363 } else if( curr->active == BLOCK_ACTIVE ) {
364 // Active but not used, release block
365 *((uintptr_t *)curr->lut_entry) &= ((uintptr_t)0x03);
366 }
367 allocation += curr->size + sizeof(struct xlat_cache_block);
368 curr = NEXT(curr);
369 if( allocation > size ) {
370 break; /* done */
371 }
372 if( curr->size == 0 ) { /* End-of-cache Sentinel */
373 /* Leave what we just released as free space and start again from the
374 * top of the cache
375 */
376 start_block->active = 0;
377 start_block->size = allocation;
378 allocation = (int)-sizeof(struct xlat_cache_block);
379 start_block = curr = xlat_temp_cache;
380 }
381 } while(1);
382 start_block->active = 1;
383 start_block->size = allocation;
384 start_block->lut_entry = block->lut_entry;
385 start_block->fpscr_mask = block->fpscr_mask;
386 start_block->fpscr = block->fpscr;
387 start_block->recover_table_offset = block->recover_table_offset;
388 start_block->recover_table_size = block->recover_table_size;
389 *block->lut_entry = &start_block->code;
390 memcpy( start_block->code, block->code, block->size );
391 xlat_temp_cache_ptr = xlat_cut_block(start_block, size );
392 if( xlat_temp_cache_ptr->size == 0 ) {
393 xlat_temp_cache_ptr = xlat_temp_cache;
394 }
396 }
397 #else
398 void xlat_promote_to_temp_space( xlat_cache_block_t block )
399 {
400 *block->lut_entry = 0;
401 }
402 #endif
404 /**
405 * Returns the next block in the new cache list that can be written to by the
406 * translator. If the next block is active, it is evicted first.
407 */
408 xlat_cache_block_t xlat_start_block( sh4addr_t address )
409 {
410 if( xlat_new_cache_ptr->size == 0 ) {
411 xlat_new_cache_ptr = xlat_new_cache;
412 }
414 if( xlat_new_cache_ptr->active ) {
415 xlat_promote_to_temp_space( xlat_new_cache_ptr );
416 }
417 xlat_new_create_ptr = xlat_new_cache_ptr;
418 xlat_new_create_ptr->active = 1;
419 xlat_new_cache_ptr = NEXT(xlat_new_cache_ptr);
421 /* Add the LUT entry for the block */
422 if( xlat_lut[XLAT_LUT_PAGE(address)] == NULL ) {
423 xlat_lut[XLAT_LUT_PAGE(address)] =
424 mmap( NULL, XLAT_LUT_PAGE_SIZE, PROT_READ|PROT_WRITE,
425 MAP_PRIVATE|MAP_ANON, -1, 0 );
426 memset( xlat_lut[XLAT_LUT_PAGE(address)], 0, XLAT_LUT_PAGE_SIZE );
427 }
429 if( IS_ENTRY_POINT(xlat_lut[XLAT_LUT_PAGE(address)][XLAT_LUT_ENTRY(address)]) ) {
430 xlat_cache_block_t oldblock = XLAT_BLOCK_FOR_CODE(xlat_lut[XLAT_LUT_PAGE(address)][XLAT_LUT_ENTRY(address)]);
431 oldblock->active = 0;
432 }
434 xlat_lut[XLAT_LUT_PAGE(address)][XLAT_LUT_ENTRY(address)] =
435 &xlat_new_create_ptr->code;
436 xlat_new_create_ptr->lut_entry = xlat_lut[XLAT_LUT_PAGE(address)] + XLAT_LUT_ENTRY(address);
438 return xlat_new_create_ptr;
439 }
441 xlat_cache_block_t xlat_extend_block( uint32_t newSize )
442 {
443 while( xlat_new_create_ptr->size < newSize ) {
444 if( xlat_new_cache_ptr->size == 0 ) {
445 /* Migrate to the front of the cache to keep it contiguous */
446 xlat_new_create_ptr->active = 0;
447 sh4ptr_t olddata = xlat_new_create_ptr->code;
448 int oldsize = xlat_new_create_ptr->size;
449 int size = oldsize + MIN_BLOCK_SIZE; /* minimum expansion */
450 void **lut_entry = xlat_new_create_ptr->lut_entry;
451 int allocation = (int)-sizeof(struct xlat_cache_block);
452 xlat_new_cache_ptr = xlat_new_cache;
453 do {
454 if( xlat_new_cache_ptr->active ) {
455 xlat_promote_to_temp_space( xlat_new_cache_ptr );
456 }
457 allocation += xlat_new_cache_ptr->size + sizeof(struct xlat_cache_block);
458 xlat_new_cache_ptr = NEXT(xlat_new_cache_ptr);
459 } while( allocation < size );
460 xlat_new_create_ptr = xlat_new_cache;
461 xlat_new_create_ptr->active = 1;
462 xlat_new_create_ptr->size = allocation;
463 xlat_new_create_ptr->lut_entry = lut_entry;
464 *lut_entry = &xlat_new_create_ptr->code;
465 memmove( xlat_new_create_ptr->code, olddata, oldsize );
466 } else {
467 if( xlat_new_cache_ptr->active ) {
468 xlat_promote_to_temp_space( xlat_new_cache_ptr );
469 }
470 xlat_new_create_ptr->size += xlat_new_cache_ptr->size + sizeof(struct xlat_cache_block);
471 xlat_new_cache_ptr = NEXT(xlat_new_cache_ptr);
472 }
473 }
474 return xlat_new_create_ptr;
476 }
478 void xlat_commit_block( uint32_t destsize, uint32_t srcsize )
479 {
480 void **ptr = xlat_new_create_ptr->lut_entry;
481 void **endptr = ptr + (srcsize>>2);
482 while( ptr < endptr ) {
483 if( *ptr == NULL ) {
484 *ptr = XLAT_LUT_ENTRY_USED;
485 }
486 ptr++;
487 }
489 xlat_new_cache_ptr = xlat_cut_block( xlat_new_create_ptr, destsize );
490 }
492 void xlat_delete_block( xlat_cache_block_t block )
493 {
494 block->active = 0;
495 *block->lut_entry = NULL;
496 }
498 void xlat_check_cache_integrity( xlat_cache_block_t cache, xlat_cache_block_t ptr, int size )
499 {
500 int foundptr = 0;
501 xlat_cache_block_t tail =
502 (xlat_cache_block_t)(((char *)cache) + size - sizeof(struct xlat_cache_block));
504 assert( tail->active == 1 );
505 assert( tail->size == 0 );
506 while( cache < tail ) {
507 assert( cache->active >= 0 && cache->active <= 2 );
508 assert( cache->size >= 0 && cache->size < size );
509 if( cache == ptr ) {
510 foundptr = 1;
511 }
512 cache = NEXT(cache);
513 }
514 assert( cache == tail );
515 assert( foundptr == 1 || tail == ptr );
516 }
518 void xlat_check_integrity( )
519 {
520 xlat_check_cache_integrity( xlat_new_cache, xlat_new_cache_ptr, XLAT_NEW_CACHE_SIZE );
521 #ifdef XLAT_GENERATIONAL_CACHE
522 xlat_check_cache_integrity( xlat_temp_cache, xlat_temp_cache_ptr, XLAT_TEMP_CACHE_SIZE );
523 xlat_check_cache_integrity( xlat_old_cache, xlat_old_cache_ptr, XLAT_OLD_CACHE_SIZE );
524 #endif
525 }
.