Search
lxdream.org :: lxdream/src/sh4/xltcache.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/xltcache.c
changeset 938:e377bd827c54
prev935:45246788ca00
next950:cc1e88104360
author nkeynes
date Sat Dec 27 04:09:17 2008 +0000 (13 years ago)
branchlxdream-mem
permissions -rw-r--r--
last change Simplify xlat_invalidate_long slightly
file annotate diff log raw
nkeynes@359
     1
/**
nkeynes@586
     2
 * $Id$
nkeynes@359
     3
 * 
nkeynes@359
     4
 * Translation cache management. This part is architecture independent.
nkeynes@359
     5
 *
nkeynes@359
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@359
     7
 *
nkeynes@359
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@359
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@359
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@359
    11
 * (at your option) any later version.
nkeynes@359
    12
 *
nkeynes@359
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@359
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@359
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@359
    16
 * GNU General Public License for more details.
nkeynes@359
    17
 */
nkeynes@736
    18
nkeynes@488
    19
#include <sys/types.h>
nkeynes@359
    20
#include <sys/mman.h>
nkeynes@359
    21
#include <assert.h>
nkeynes@359
    22
nkeynes@428
    23
#include "dreamcast.h"
nkeynes@586
    24
#include "sh4/sh4core.h"
nkeynes@428
    25
#include "sh4/xltcache.h"
nkeynes@428
    26
#include "x86dasm/x86dasm.h"
nkeynes@428
    27
nkeynes@359
    28
#define XLAT_LUT_PAGE_BITS 12
nkeynes@359
    29
#define XLAT_LUT_TOTAL_BITS 28
nkeynes@359
    30
#define XLAT_LUT_PAGE(addr) (((addr)>>13) & 0xFFFF)
nkeynes@359
    31
#define XLAT_LUT_ENTRY(addr) (((addr)&0x1FFE) >> 1)
nkeynes@359
    32
nkeynes@359
    33
#define XLAT_LUT_PAGES (1<<(XLAT_LUT_TOTAL_BITS-XLAT_LUT_PAGE_BITS))
nkeynes@359
    34
#define XLAT_LUT_PAGE_ENTRIES (1<<XLAT_LUT_PAGE_BITS)
nkeynes@359
    35
#define XLAT_LUT_PAGE_SIZE (XLAT_LUT_PAGE_ENTRIES * sizeof(void *))
nkeynes@359
    36
nkeynes@359
    37
#define XLAT_LUT_ENTRY_EMPTY (void *)0
nkeynes@359
    38
#define XLAT_LUT_ENTRY_USED  (void *)1
nkeynes@359
    39
nkeynes@359
    40
#define NEXT(block) ( (xlat_cache_block_t)&((block)->code[(block)->size]))
nkeynes@359
    41
#define IS_ENTRY_POINT(ent) (ent > XLAT_LUT_ENTRY_USED)
nkeynes@359
    42
#define IS_ENTRY_USED(ent) (ent != XLAT_LUT_ENTRY_EMPTY)
nkeynes@359
    43
nkeynes@359
    44
#define MIN_BLOCK_SIZE 32
nkeynes@359
    45
#define MIN_TOTAL_SIZE (sizeof(struct xlat_cache_block)+MIN_BLOCK_SIZE)
nkeynes@359
    46
nkeynes@359
    47
#define BLOCK_INACTIVE 0
nkeynes@359
    48
#define BLOCK_ACTIVE 1
nkeynes@359
    49
#define BLOCK_USED 2
nkeynes@359
    50
nkeynes@359
    51
xlat_cache_block_t xlat_new_cache;
nkeynes@359
    52
xlat_cache_block_t xlat_new_cache_ptr;
nkeynes@359
    53
xlat_cache_block_t xlat_new_create_ptr;
nkeynes@922
    54
nkeynes@922
    55
#ifdef XLAT_GENERATIONAL_CACHE
nkeynes@359
    56
xlat_cache_block_t xlat_temp_cache;
nkeynes@359
    57
xlat_cache_block_t xlat_temp_cache_ptr;
nkeynes@359
    58
xlat_cache_block_t xlat_old_cache;
nkeynes@359
    59
xlat_cache_block_t xlat_old_cache_ptr;
nkeynes@922
    60
#endif
nkeynes@922
    61
nkeynes@935
    62
static void **xlat_lut[XLAT_LUT_PAGES];
nkeynes@376
    63
static gboolean xlat_initialized = FALSE;
nkeynes@359
    64
nkeynes@422
    65
void xlat_cache_init(void) 
nkeynes@359
    66
{
nkeynes@376
    67
    if( !xlat_initialized ) {
nkeynes@736
    68
        xlat_initialized = TRUE;
nkeynes@736
    69
        xlat_new_cache = mmap( NULL, XLAT_NEW_CACHE_SIZE, PROT_EXEC|PROT_READ|PROT_WRITE,
nkeynes@736
    70
                MAP_PRIVATE|MAP_ANON, -1, 0 );
nkeynes@922
    71
        xlat_new_cache_ptr = xlat_new_cache;
nkeynes@922
    72
        xlat_new_create_ptr = xlat_new_cache;
nkeynes@922
    73
#ifdef XLAT_GENERATIONAL_CACHE
nkeynes@736
    74
        xlat_temp_cache = mmap( NULL, XLAT_TEMP_CACHE_SIZE, PROT_EXEC|PROT_READ|PROT_WRITE,
nkeynes@736
    75
                MAP_PRIVATE|MAP_ANON, -1, 0 );
nkeynes@736
    76
        xlat_old_cache = mmap( NULL, XLAT_OLD_CACHE_SIZE, PROT_EXEC|PROT_READ|PROT_WRITE,
nkeynes@736
    77
                MAP_PRIVATE|MAP_ANON, -1, 0 );
nkeynes@736
    78
        xlat_temp_cache_ptr = xlat_temp_cache;
nkeynes@736
    79
        xlat_old_cache_ptr = xlat_old_cache;
nkeynes@922
    80
#endif
nkeynes@935
    81
//        xlat_lut = mmap( NULL, XLAT_LUT_PAGES*sizeof(void *), PROT_READ|PROT_WRITE,
nkeynes@935
    82
//                MAP_PRIVATE|MAP_ANON, -1, 0);
nkeynes@736
    83
        memset( xlat_lut, 0, XLAT_LUT_PAGES*sizeof(void *) );
nkeynes@376
    84
    }
nkeynes@359
    85
    xlat_flush_cache();
nkeynes@359
    86
}
nkeynes@359
    87
nkeynes@359
    88
/**
nkeynes@359
    89
 * Reset the cache structure to its default state
nkeynes@359
    90
 */
nkeynes@359
    91
void xlat_flush_cache() 
nkeynes@359
    92
{
nkeynes@359
    93
    xlat_cache_block_t tmp;
nkeynes@359
    94
    int i;
nkeynes@359
    95
    xlat_new_cache_ptr = xlat_new_cache;
nkeynes@359
    96
    xlat_new_cache_ptr->active = 0;
nkeynes@359
    97
    xlat_new_cache_ptr->size = XLAT_NEW_CACHE_SIZE - 2*sizeof(struct xlat_cache_block);
nkeynes@359
    98
    tmp = NEXT(xlat_new_cache_ptr);
nkeynes@359
    99
    tmp->active = 1;
nkeynes@359
   100
    tmp->size = 0;
nkeynes@922
   101
#ifdef XLAT_GENERATIONAL_CACHE
nkeynes@359
   102
    xlat_temp_cache_ptr = xlat_temp_cache;
nkeynes@359
   103
    xlat_temp_cache_ptr->active = 0;
nkeynes@359
   104
    xlat_temp_cache_ptr->size = XLAT_TEMP_CACHE_SIZE - 2*sizeof(struct xlat_cache_block);
nkeynes@359
   105
    tmp = NEXT(xlat_temp_cache_ptr);
nkeynes@359
   106
    tmp->active = 1;
nkeynes@359
   107
    tmp->size = 0;
nkeynes@359
   108
    xlat_old_cache_ptr = xlat_old_cache;
nkeynes@359
   109
    xlat_old_cache_ptr->active = 0;
nkeynes@359
   110
    xlat_old_cache_ptr->size = XLAT_OLD_CACHE_SIZE - 2*sizeof(struct xlat_cache_block);
nkeynes@359
   111
    tmp = NEXT(xlat_old_cache_ptr);
nkeynes@359
   112
    tmp->active = 1;
nkeynes@359
   113
    tmp->size = 0;
nkeynes@922
   114
#endif
nkeynes@359
   115
    for( i=0; i<XLAT_LUT_PAGES; i++ ) {
nkeynes@736
   116
        if( xlat_lut[i] != NULL ) {
nkeynes@736
   117
            memset( xlat_lut[i], 0, XLAT_LUT_PAGE_SIZE );
nkeynes@736
   118
        }
nkeynes@359
   119
    }
nkeynes@359
   120
}
nkeynes@359
   121
nkeynes@400
   122
static void xlat_flush_page_by_lut( void **page )
nkeynes@359
   123
{
nkeynes@359
   124
    int i;
nkeynes@359
   125
    for( i=0; i<XLAT_LUT_PAGE_ENTRIES; i++ ) {
nkeynes@736
   126
        if( IS_ENTRY_POINT(page[i]) ) {
nkeynes@901
   127
            XLAT_BLOCK_FOR_CODE(page[i])->active = 0;
nkeynes@736
   128
        }
nkeynes@736
   129
        page[i] = NULL;
nkeynes@359
   130
    }
nkeynes@359
   131
}
nkeynes@359
   132
nkeynes@905
   133
void FASTCALL xlat_invalidate_word( sh4addr_t addr )
nkeynes@400
   134
{
nkeynes@935
   135
    void **page = xlat_lut[XLAT_LUT_PAGE(addr)];
nkeynes@935
   136
    if( page != NULL ) {
nkeynes@935
   137
        int entry = XLAT_LUT_ENTRY(addr);
nkeynes@935
   138
        if( page[entry] != NULL ) {
nkeynes@935
   139
            xlat_flush_page_by_lut(page);
nkeynes@736
   140
        }
nkeynes@400
   141
    }
nkeynes@400
   142
}
nkeynes@400
   143
nkeynes@905
   144
void FASTCALL xlat_invalidate_long( sh4addr_t addr )
nkeynes@400
   145
{
nkeynes@935
   146
    void **page = xlat_lut[XLAT_LUT_PAGE(addr)];
nkeynes@935
   147
    if( page != NULL ) {
nkeynes@935
   148
        int entry = XLAT_LUT_ENTRY(addr);
nkeynes@938
   149
        if( *(uint64_t *)&page[entry] != 0 ) {
nkeynes@935
   150
            xlat_flush_page_by_lut(page);
nkeynes@736
   151
        }
nkeynes@400
   152
    }
nkeynes@400
   153
}
nkeynes@400
   154
nkeynes@905
   155
void FASTCALL xlat_invalidate_block( sh4addr_t address, size_t size )
nkeynes@400
   156
{
nkeynes@400
   157
    int i;
nkeynes@400
   158
    int entry_count = size >> 1; // words;
nkeynes@400
   159
    uint32_t page_no = XLAT_LUT_PAGE(address);
nkeynes@400
   160
    int entry = XLAT_LUT_ENTRY(address);
nkeynes@935
   161
    do {
nkeynes@935
   162
        void **page = xlat_lut[page_no];
nkeynes@935
   163
        int page_entries = XLAT_LUT_PAGE_ENTRIES - entry;
nkeynes@935
   164
        if( entry_count < page_entries ) {
nkeynes@935
   165
            page_entries = entry_count;
nkeynes@935
   166
        }
nkeynes@935
   167
        if( page != NULL ) {
nkeynes@935
   168
            if( page_entries == XLAT_LUT_PAGE_ENTRIES ) {
nkeynes@935
   169
                /* Overwriting the entire page anyway */
nkeynes@935
   170
                xlat_flush_page_by_lut(page);
nkeynes@935
   171
            } else {
nkeynes@935
   172
                for( i=entry; i<entry+page_entries; i++ ) {
nkeynes@935
   173
                    if( page[i] != NULL ) {
nkeynes@935
   174
                        xlat_flush_page_by_lut(page);
nkeynes@935
   175
                        break;
nkeynes@736
   176
                    }
nkeynes@736
   177
                }
nkeynes@736
   178
            }
nkeynes@736
   179
            entry_count -= page_entries;
nkeynes@935
   180
        }
nkeynes@935
   181
        page_no ++;
nkeynes@935
   182
        entry_count -= page_entries;
nkeynes@935
   183
        entry = 0;
nkeynes@935
   184
    } while( entry_count > 0 );
nkeynes@400
   185
}
nkeynes@400
   186
nkeynes@905
   187
void FASTCALL xlat_flush_page( sh4addr_t address )
nkeynes@400
   188
{
nkeynes@400
   189
    void **page = xlat_lut[XLAT_LUT_PAGE(address)];
nkeynes@400
   190
    if( page != NULL ) {
nkeynes@736
   191
        xlat_flush_page_by_lut(page);
nkeynes@400
   192
    }
nkeynes@400
   193
}
nkeynes@400
   194
nkeynes@905
   195
void * FASTCALL xlat_get_code( sh4addr_t address )
nkeynes@359
   196
{
nkeynes@410
   197
    void *result = NULL;
nkeynes@359
   198
    void **page = xlat_lut[XLAT_LUT_PAGE(address)];
nkeynes@407
   199
    if( page != NULL ) {
nkeynes@736
   200
        result = (void *)(((uintptr_t)(page[XLAT_LUT_ENTRY(address)])) & (~((uintptr_t)0x03)));
nkeynes@407
   201
    }
nkeynes@407
   202
    return result;
nkeynes@407
   203
}
nkeynes@407
   204
nkeynes@809
   205
xlat_recovery_record_t xlat_get_post_recovery( void *code, void *native_pc, gboolean with_terminal )
nkeynes@586
   206
{
nkeynes@586
   207
    if( code != NULL ) {
nkeynes@736
   208
        uintptr_t pc_offset = ((uint8_t *)native_pc) - ((uint8_t *)code);
nkeynes@901
   209
        xlat_cache_block_t block = XLAT_BLOCK_FOR_CODE(code);
nkeynes@736
   210
        uint32_t count = block->recover_table_size;
nkeynes@736
   211
        xlat_recovery_record_t records = (xlat_recovery_record_t)(&block->code[block->recover_table_offset]);
nkeynes@736
   212
        uint32_t posn;
nkeynes@809
   213
        if( count > 0 && !with_terminal )
nkeynes@809
   214
        	count--;
nkeynes@809
   215
        if( records[count-1].xlat_offset < pc_offset ) {
nkeynes@809
   216
        	return NULL;
nkeynes@736
   217
        }
nkeynes@809
   218
        for( posn=count-1; posn > 0; posn-- ) {
nkeynes@809
   219
        	if( records[posn-1].xlat_offset < pc_offset ) {
nkeynes@809
   220
        		return &records[posn];
nkeynes@809
   221
        	}
nkeynes@809
   222
        }
nkeynes@809
   223
        return &records[0]; // shouldn't happen
nkeynes@586
   224
    }
nkeynes@586
   225
    return NULL;
nkeynes@586
   226
}
nkeynes@586
   227
nkeynes@809
   228
xlat_recovery_record_t xlat_get_pre_recovery( void *code, void *native_pc )
nkeynes@809
   229
{
nkeynes@809
   230
    if( code != NULL ) {
nkeynes@809
   231
        uintptr_t pc_offset = ((uint8_t *)native_pc) - ((uint8_t *)code);
nkeynes@901
   232
        xlat_cache_block_t block = XLAT_BLOCK_FOR_CODE(code);
nkeynes@809
   233
        uint32_t count = block->recover_table_size;
nkeynes@809
   234
        xlat_recovery_record_t records = (xlat_recovery_record_t)(&block->code[block->recover_table_offset]);
nkeynes@809
   235
        uint32_t posn;
nkeynes@809
   236
        for( posn = 1; posn < count; posn++ ) {
nkeynes@809
   237
        	if( records[posn].xlat_offset >= pc_offset ) {
nkeynes@809
   238
        		return &records[posn-1];
nkeynes@809
   239
        	}
nkeynes@809
   240
        }
nkeynes@809
   241
        return &records[count-1];
nkeynes@809
   242
    }
nkeynes@809
   243
    return NULL;	
nkeynes@809
   244
}
nkeynes@809
   245
nkeynes@905
   246
void ** FASTCALL xlat_get_lut_entry( sh4addr_t address )
nkeynes@407
   247
{
nkeynes@407
   248
    void **page = xlat_lut[XLAT_LUT_PAGE(address)];
nkeynes@407
   249
nkeynes@407
   250
    /* Add the LUT entry for the block */
nkeynes@359
   251
    if( page == NULL ) {
nkeynes@736
   252
        xlat_lut[XLAT_LUT_PAGE(address)] = page =
nkeynes@736
   253
            mmap( NULL, XLAT_LUT_PAGE_SIZE, PROT_READ|PROT_WRITE,
nkeynes@736
   254
                    MAP_PRIVATE|MAP_ANON, -1, 0 );
nkeynes@736
   255
        memset( page, 0, XLAT_LUT_PAGE_SIZE );
nkeynes@359
   256
    }
nkeynes@736
   257
nkeynes@407
   258
    return &page[XLAT_LUT_ENTRY(address)];
nkeynes@359
   259
}
nkeynes@359
   260
nkeynes@407
   261
nkeynes@407
   262
nkeynes@905
   263
uint32_t FASTCALL xlat_get_block_size( void *block )
nkeynes@366
   264
{
nkeynes@366
   265
    xlat_cache_block_t xlt = (xlat_cache_block_t)(((char *)block)-sizeof(struct xlat_cache_block));
nkeynes@366
   266
    return xlt->size;
nkeynes@366
   267
}
nkeynes@366
   268
nkeynes@905
   269
uint32_t FASTCALL xlat_get_code_size( void *block )
nkeynes@586
   270
{
nkeynes@586
   271
    xlat_cache_block_t xlt = (xlat_cache_block_t)(((char *)block)-sizeof(struct xlat_cache_block));
nkeynes@592
   272
    if( xlt->recover_table_offset == 0 ) {
nkeynes@736
   273
        return xlt->size;
nkeynes@586
   274
    } else {
nkeynes@736
   275
        return xlt->recover_table_offset;
nkeynes@586
   276
    }
nkeynes@586
   277
}
nkeynes@586
   278
nkeynes@359
   279
/**
nkeynes@359
   280
 * Cut the specified block so that it has the given size, with the remaining data
nkeynes@359
   281
 * forming a new free block. If the free block would be less than the minimum size,
nkeynes@359
   282
 * the cut is not performed.
nkeynes@359
   283
 * @return the next block after the (possibly cut) block.
nkeynes@359
   284
 */
nkeynes@359
   285
static inline xlat_cache_block_t xlat_cut_block( xlat_cache_block_t block, int cutsize )
nkeynes@359
   286
{
nkeynes@407
   287
    cutsize = (cutsize + 3) & 0xFFFFFFFC; // force word alignment
nkeynes@410
   288
    assert( cutsize <= block->size );
nkeynes@359
   289
    if( block->size > cutsize + MIN_TOTAL_SIZE ) {
nkeynes@736
   290
        int oldsize = block->size;
nkeynes@736
   291
        block->size = cutsize;
nkeynes@736
   292
        xlat_cache_block_t next = NEXT(block);
nkeynes@736
   293
        next->active = 0;
nkeynes@736
   294
        next->size = oldsize - cutsize - sizeof(struct xlat_cache_block);
nkeynes@736
   295
        return next;
nkeynes@359
   296
    } else {
nkeynes@736
   297
        return NEXT(block);
nkeynes@359
   298
    }
nkeynes@359
   299
}
nkeynes@359
   300
nkeynes@922
   301
#ifdef XLAT_GENERATIONAL_CACHE
nkeynes@359
   302
/**
nkeynes@359
   303
 * Promote a block in temp space (or elsewhere for that matter) to old space.
nkeynes@359
   304
 *
nkeynes@359
   305
 * @param block to promote.
nkeynes@359
   306
 */
nkeynes@359
   307
static void xlat_promote_to_old_space( xlat_cache_block_t block )
nkeynes@359
   308
{
nkeynes@788
   309
    int allocation = (int)-sizeof(struct xlat_cache_block);
nkeynes@359
   310
    int size = block->size;
nkeynes@359
   311
    xlat_cache_block_t curr = xlat_old_cache_ptr;
nkeynes@359
   312
    xlat_cache_block_t start_block = curr;
nkeynes@359
   313
    do {
nkeynes@736
   314
        allocation += curr->size + sizeof(struct xlat_cache_block);
nkeynes@736
   315
        curr = NEXT(curr);
nkeynes@736
   316
        if( allocation > size ) {
nkeynes@736
   317
            break; /* done */
nkeynes@736
   318
        }
nkeynes@736
   319
        if( curr->size == 0 ) { /* End-of-cache Sentinel */
nkeynes@736
   320
            /* Leave what we just released as free space and start again from the
nkeynes@736
   321
             * top of the cache
nkeynes@736
   322
             */
nkeynes@736
   323
            start_block->active = 0;
nkeynes@736
   324
            start_block->size = allocation;
nkeynes@788
   325
            allocation = (int)-sizeof(struct xlat_cache_block);
nkeynes@736
   326
            start_block = curr = xlat_old_cache;
nkeynes@736
   327
        }
nkeynes@359
   328
    } while(1);
nkeynes@359
   329
    start_block->active = 1;
nkeynes@359
   330
    start_block->size = allocation;
nkeynes@359
   331
    start_block->lut_entry = block->lut_entry;
nkeynes@901
   332
    start_block->fpscr_mask = block->fpscr_mask;
nkeynes@901
   333
    start_block->fpscr = block->fpscr;
nkeynes@596
   334
    start_block->recover_table_offset = block->recover_table_offset;
nkeynes@596
   335
    start_block->recover_table_size = block->recover_table_size;
nkeynes@359
   336
    *block->lut_entry = &start_block->code;
nkeynes@359
   337
    memcpy( start_block->code, block->code, block->size );
nkeynes@359
   338
    xlat_old_cache_ptr = xlat_cut_block(start_block, size );
nkeynes@359
   339
    if( xlat_old_cache_ptr->size == 0 ) {
nkeynes@736
   340
        xlat_old_cache_ptr = xlat_old_cache;
nkeynes@359
   341
    }
nkeynes@359
   342
}
nkeynes@359
   343
nkeynes@359
   344
/**
nkeynes@359
   345
 * Similarly to the above method, promotes a block to temp space.
nkeynes@359
   346
 * TODO: Try to combine these - they're nearly identical
nkeynes@359
   347
 */
nkeynes@359
   348
void xlat_promote_to_temp_space( xlat_cache_block_t block )
nkeynes@359
   349
{
nkeynes@359
   350
    int size = block->size;
nkeynes@788
   351
    int allocation = (int)-sizeof(struct xlat_cache_block);
nkeynes@359
   352
    xlat_cache_block_t curr = xlat_temp_cache_ptr;
nkeynes@359
   353
    xlat_cache_block_t start_block = curr;
nkeynes@359
   354
    do {
nkeynes@736
   355
        if( curr->active == BLOCK_USED ) {
nkeynes@736
   356
            xlat_promote_to_old_space( curr );
nkeynes@736
   357
        } else if( curr->active == BLOCK_ACTIVE ) {
nkeynes@736
   358
            // Active but not used, release block
nkeynes@736
   359
            *((uintptr_t *)curr->lut_entry) &= ((uintptr_t)0x03);
nkeynes@736
   360
        }
nkeynes@736
   361
        allocation += curr->size + sizeof(struct xlat_cache_block);
nkeynes@736
   362
        curr = NEXT(curr);
nkeynes@736
   363
        if( allocation > size ) {
nkeynes@736
   364
            break; /* done */
nkeynes@736
   365
        }
nkeynes@736
   366
        if( curr->size == 0 ) { /* End-of-cache Sentinel */
nkeynes@736
   367
            /* Leave what we just released as free space and start again from the
nkeynes@736
   368
             * top of the cache
nkeynes@736
   369
             */
nkeynes@736
   370
            start_block->active = 0;
nkeynes@736
   371
            start_block->size = allocation;
nkeynes@788
   372
            allocation = (int)-sizeof(struct xlat_cache_block);
nkeynes@736
   373
            start_block = curr = xlat_temp_cache;
nkeynes@736
   374
        }
nkeynes@359
   375
    } while(1);
nkeynes@359
   376
    start_block->active = 1;
nkeynes@359
   377
    start_block->size = allocation;
nkeynes@359
   378
    start_block->lut_entry = block->lut_entry;
nkeynes@901
   379
    start_block->fpscr_mask = block->fpscr_mask;
nkeynes@901
   380
    start_block->fpscr = block->fpscr;
nkeynes@596
   381
    start_block->recover_table_offset = block->recover_table_offset;
nkeynes@596
   382
    start_block->recover_table_size = block->recover_table_size;
nkeynes@359
   383
    *block->lut_entry = &start_block->code;
nkeynes@359
   384
    memcpy( start_block->code, block->code, block->size );
nkeynes@359
   385
    xlat_temp_cache_ptr = xlat_cut_block(start_block, size );
nkeynes@359
   386
    if( xlat_temp_cache_ptr->size == 0 ) {
nkeynes@736
   387
        xlat_temp_cache_ptr = xlat_temp_cache;
nkeynes@359
   388
    }
nkeynes@736
   389
nkeynes@359
   390
}
nkeynes@922
   391
#else 
nkeynes@922
   392
void xlat_promote_to_temp_space( xlat_cache_block_t block )
nkeynes@922
   393
{
nkeynes@922
   394
    *block->lut_entry = 0;
nkeynes@922
   395
}
nkeynes@922
   396
#endif
nkeynes@359
   397
nkeynes@359
   398
/**
nkeynes@359
   399
 * Returns the next block in the new cache list that can be written to by the
nkeynes@359
   400
 * translator. If the next block is active, it is evicted first.
nkeynes@359
   401
 */
nkeynes@359
   402
xlat_cache_block_t xlat_start_block( sh4addr_t address )
nkeynes@359
   403
{
nkeynes@359
   404
    if( xlat_new_cache_ptr->size == 0 ) {
nkeynes@736
   405
        xlat_new_cache_ptr = xlat_new_cache;
nkeynes@359
   406
    }
nkeynes@359
   407
nkeynes@359
   408
    if( xlat_new_cache_ptr->active ) {
nkeynes@736
   409
        xlat_promote_to_temp_space( xlat_new_cache_ptr );
nkeynes@359
   410
    }
nkeynes@359
   411
    xlat_new_create_ptr = xlat_new_cache_ptr;
nkeynes@359
   412
    xlat_new_create_ptr->active = 1;
nkeynes@359
   413
    xlat_new_cache_ptr = NEXT(xlat_new_cache_ptr);
nkeynes@359
   414
nkeynes@359
   415
    /* Add the LUT entry for the block */
nkeynes@359
   416
    if( xlat_lut[XLAT_LUT_PAGE(address)] == NULL ) {
nkeynes@736
   417
        xlat_lut[XLAT_LUT_PAGE(address)] =
nkeynes@736
   418
            mmap( NULL, XLAT_LUT_PAGE_SIZE, PROT_READ|PROT_WRITE,
nkeynes@736
   419
                    MAP_PRIVATE|MAP_ANON, -1, 0 );
nkeynes@736
   420
        memset( xlat_lut[XLAT_LUT_PAGE(address)], 0, XLAT_LUT_PAGE_SIZE );
nkeynes@359
   421
    }
nkeynes@359
   422
nkeynes@359
   423
    if( IS_ENTRY_POINT(xlat_lut[XLAT_LUT_PAGE(address)][XLAT_LUT_ENTRY(address)]) ) {
nkeynes@901
   424
        xlat_cache_block_t oldblock = XLAT_BLOCK_FOR_CODE(xlat_lut[XLAT_LUT_PAGE(address)][XLAT_LUT_ENTRY(address)]);
nkeynes@736
   425
        oldblock->active = 0;
nkeynes@359
   426
    }
nkeynes@359
   427
nkeynes@359
   428
    xlat_lut[XLAT_LUT_PAGE(address)][XLAT_LUT_ENTRY(address)] = 
nkeynes@736
   429
        &xlat_new_create_ptr->code;
nkeynes@359
   430
    xlat_new_create_ptr->lut_entry = xlat_lut[XLAT_LUT_PAGE(address)] + XLAT_LUT_ENTRY(address);
nkeynes@736
   431
nkeynes@359
   432
    return xlat_new_create_ptr;
nkeynes@359
   433
}
nkeynes@359
   434
nkeynes@410
   435
xlat_cache_block_t xlat_extend_block( uint32_t newSize )
nkeynes@359
   436
{
nkeynes@410
   437
    while( xlat_new_create_ptr->size < newSize ) {
nkeynes@736
   438
        if( xlat_new_cache_ptr->size == 0 ) {
nkeynes@736
   439
            /* Migrate to the front of the cache to keep it contiguous */
nkeynes@736
   440
            xlat_new_create_ptr->active = 0;
nkeynes@736
   441
            sh4ptr_t olddata = xlat_new_create_ptr->code;
nkeynes@736
   442
            int oldsize = xlat_new_create_ptr->size;
nkeynes@736
   443
            int size = oldsize + MIN_BLOCK_SIZE; /* minimum expansion */
nkeynes@736
   444
            void **lut_entry = xlat_new_create_ptr->lut_entry;
nkeynes@788
   445
            int allocation = (int)-sizeof(struct xlat_cache_block);
nkeynes@736
   446
            xlat_new_cache_ptr = xlat_new_cache;
nkeynes@736
   447
            do {
nkeynes@736
   448
                if( xlat_new_cache_ptr->active ) {
nkeynes@736
   449
                    xlat_promote_to_temp_space( xlat_new_cache_ptr );
nkeynes@736
   450
                }
nkeynes@736
   451
                allocation += xlat_new_cache_ptr->size + sizeof(struct xlat_cache_block);
nkeynes@736
   452
                xlat_new_cache_ptr = NEXT(xlat_new_cache_ptr);
nkeynes@736
   453
            } while( allocation < size );
nkeynes@736
   454
            xlat_new_create_ptr = xlat_new_cache;
nkeynes@736
   455
            xlat_new_create_ptr->active = 1;
nkeynes@736
   456
            xlat_new_create_ptr->size = allocation;
nkeynes@736
   457
            xlat_new_create_ptr->lut_entry = lut_entry;
nkeynes@736
   458
            *lut_entry = &xlat_new_create_ptr->code;
nkeynes@736
   459
            memmove( xlat_new_create_ptr->code, olddata, oldsize );
nkeynes@736
   460
        } else {
nkeynes@736
   461
            if( xlat_new_cache_ptr->active ) {
nkeynes@736
   462
                xlat_promote_to_temp_space( xlat_new_cache_ptr );
nkeynes@736
   463
            }
nkeynes@736
   464
            xlat_new_create_ptr->size += xlat_new_cache_ptr->size + sizeof(struct xlat_cache_block);
nkeynes@736
   465
            xlat_new_cache_ptr = NEXT(xlat_new_cache_ptr);
nkeynes@736
   466
        }
nkeynes@359
   467
    }
nkeynes@359
   468
    return xlat_new_create_ptr;
nkeynes@359
   469
nkeynes@359
   470
}
nkeynes@359
   471
nkeynes@359
   472
void xlat_commit_block( uint32_t destsize, uint32_t srcsize )
nkeynes@359
   473
{
nkeynes@359
   474
    void **ptr = xlat_new_create_ptr->lut_entry;
nkeynes@359
   475
    void **endptr = ptr + (srcsize>>2);
nkeynes@359
   476
    while( ptr < endptr ) {
nkeynes@736
   477
        if( *ptr == NULL ) {
nkeynes@736
   478
            *ptr = XLAT_LUT_ENTRY_USED;
nkeynes@736
   479
        }
nkeynes@736
   480
        ptr++;
nkeynes@359
   481
    }
nkeynes@359
   482
nkeynes@359
   483
    xlat_new_cache_ptr = xlat_cut_block( xlat_new_create_ptr, destsize );
nkeynes@359
   484
}
nkeynes@359
   485
nkeynes@359
   486
void xlat_delete_block( xlat_cache_block_t block ) 
nkeynes@359
   487
{
nkeynes@359
   488
    block->active = 0;
nkeynes@359
   489
    *block->lut_entry = NULL;
nkeynes@359
   490
}
nkeynes@359
   491
nkeynes@359
   492
void xlat_check_cache_integrity( xlat_cache_block_t cache, xlat_cache_block_t ptr, int size )
nkeynes@359
   493
{
nkeynes@359
   494
    int foundptr = 0;
nkeynes@359
   495
    xlat_cache_block_t tail = 
nkeynes@736
   496
        (xlat_cache_block_t)(((char *)cache) + size - sizeof(struct xlat_cache_block));
nkeynes@359
   497
nkeynes@359
   498
    assert( tail->active == 1 );
nkeynes@359
   499
    assert( tail->size == 0 ); 
nkeynes@359
   500
    while( cache < tail ) {
nkeynes@736
   501
        assert( cache->active >= 0 && cache->active <= 2 );
nkeynes@736
   502
        assert( cache->size >= 0 && cache->size < size );
nkeynes@736
   503
        if( cache == ptr ) {
nkeynes@736
   504
            foundptr = 1;
nkeynes@736
   505
        }
nkeynes@736
   506
        cache = NEXT(cache);
nkeynes@359
   507
    }
nkeynes@359
   508
    assert( cache == tail );
nkeynes@596
   509
    assert( foundptr == 1 || tail == ptr );
nkeynes@359
   510
}
nkeynes@359
   511
nkeynes@359
   512
void xlat_check_integrity( )
nkeynes@359
   513
{
nkeynes@359
   514
    xlat_check_cache_integrity( xlat_new_cache, xlat_new_cache_ptr, XLAT_NEW_CACHE_SIZE );
nkeynes@922
   515
#ifdef XLAT_GENERATIONAL_CACHE
nkeynes@359
   516
    xlat_check_cache_integrity( xlat_temp_cache, xlat_temp_cache_ptr, XLAT_TEMP_CACHE_SIZE );
nkeynes@359
   517
    xlat_check_cache_integrity( xlat_old_cache, xlat_old_cache_ptr, XLAT_OLD_CACHE_SIZE );
nkeynes@922
   518
#endif
nkeynes@359
   519
}
nkeynes@376
   520
.