Search
lxdream.org :: lxdream/src/xlat/xltcache.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/xlat/xltcache.c
changeset 1182:b38a327ad8fa
prev1175:712c418cad83
next1186:2dc47c67bb93
author nkeynes
date Sun Nov 27 18:20:21 2011 +1000 (10 years ago)
permissions -rw-r--r--
last change Add block profiling option to count the number of executions of each block,
and dump them out from most-to-least used.
file annotate diff log raw
nkeynes@991
     1
/**
nkeynes@991
     2
 * $Id$
nkeynes@991
     3
 * 
nkeynes@991
     4
 * Translation cache management. This part is architecture independent.
nkeynes@991
     5
 *
nkeynes@991
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@991
     7
 *
nkeynes@991
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@991
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@991
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@991
    11
 * (at your option) any later version.
nkeynes@991
    12
 *
nkeynes@991
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@991
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@991
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@991
    16
 * GNU General Public License for more details.
nkeynes@991
    17
 */
nkeynes@991
    18
nkeynes@991
    19
#include <sys/types.h>
nkeynes@991
    20
#include <sys/mman.h>
nkeynes@991
    21
#include <assert.h>
nkeynes@991
    22
nkeynes@991
    23
#include "dreamcast.h"
nkeynes@991
    24
#include "sh4/sh4core.h"
nkeynes@991
    25
#include "xlat/xltcache.h"
nkeynes@991
    26
#include "x86dasm/x86dasm.h"
nkeynes@991
    27
nkeynes@991
    28
#define XLAT_LUT_PAGE_BITS 12
nkeynes@991
    29
#define XLAT_LUT_TOTAL_BITS 28
nkeynes@991
    30
#define XLAT_LUT_PAGE(addr) (((addr)>>13) & 0xFFFF)
nkeynes@991
    31
#define XLAT_LUT_ENTRY(addr) (((addr)&0x1FFE) >> 1)
nkeynes@991
    32
nkeynes@991
    33
#define XLAT_LUT_PAGES (1<<(XLAT_LUT_TOTAL_BITS-XLAT_LUT_PAGE_BITS))
nkeynes@991
    34
#define XLAT_LUT_PAGE_ENTRIES (1<<XLAT_LUT_PAGE_BITS)
nkeynes@991
    35
#define XLAT_LUT_PAGE_SIZE (XLAT_LUT_PAGE_ENTRIES * sizeof(void *))
nkeynes@991
    36
nkeynes@991
    37
#define XLAT_LUT_ENTRY_EMPTY (void *)0
nkeynes@991
    38
#define XLAT_LUT_ENTRY_USED  (void *)1
nkeynes@991
    39
nkeynes@1182
    40
#define XLAT_ADDR_FROM_ENTRY(pagenum,entrynum) ((((pagenum)&0xFFFF)<<13)|(((entrynum)<<1)&0x1FFE))
nkeynes@1182
    41
nkeynes@991
    42
#define NEXT(block) ( (xlat_cache_block_t)&((block)->code[(block)->size]))
nkeynes@991
    43
#define IS_ENTRY_POINT(ent) (ent > XLAT_LUT_ENTRY_USED)
nkeynes@991
    44
#define IS_ENTRY_USED(ent) (ent != XLAT_LUT_ENTRY_EMPTY)
nkeynes@991
    45
nkeynes@991
    46
#define MIN_BLOCK_SIZE 32
nkeynes@991
    47
#define MIN_TOTAL_SIZE (sizeof(struct xlat_cache_block)+MIN_BLOCK_SIZE)
nkeynes@991
    48
nkeynes@991
    49
#define BLOCK_INACTIVE 0
nkeynes@991
    50
#define BLOCK_ACTIVE 1
nkeynes@991
    51
#define BLOCK_USED 2
nkeynes@991
    52
nkeynes@991
    53
xlat_cache_block_t xlat_new_cache;
nkeynes@991
    54
xlat_cache_block_t xlat_new_cache_ptr;
nkeynes@991
    55
xlat_cache_block_t xlat_new_create_ptr;
nkeynes@991
    56
nkeynes@991
    57
#ifdef XLAT_GENERATIONAL_CACHE
nkeynes@991
    58
xlat_cache_block_t xlat_temp_cache;
nkeynes@991
    59
xlat_cache_block_t xlat_temp_cache_ptr;
nkeynes@991
    60
xlat_cache_block_t xlat_old_cache;
nkeynes@991
    61
xlat_cache_block_t xlat_old_cache_ptr;
nkeynes@991
    62
#endif
nkeynes@991
    63
nkeynes@991
    64
static void **xlat_lut[XLAT_LUT_PAGES];
nkeynes@991
    65
static gboolean xlat_initialized = FALSE;
nkeynes@991
    66
nkeynes@991
    67
void xlat_cache_init(void) 
nkeynes@991
    68
{
nkeynes@991
    69
    if( !xlat_initialized ) {
nkeynes@991
    70
        xlat_initialized = TRUE;
nkeynes@991
    71
        xlat_new_cache = (xlat_cache_block_t)mmap( NULL, XLAT_NEW_CACHE_SIZE, PROT_EXEC|PROT_READ|PROT_WRITE,
nkeynes@991
    72
                MAP_PRIVATE|MAP_ANON, -1, 0 );
nkeynes@991
    73
        xlat_new_cache_ptr = xlat_new_cache;
nkeynes@991
    74
        xlat_new_create_ptr = xlat_new_cache;
nkeynes@991
    75
#ifdef XLAT_GENERATIONAL_CACHE
nkeynes@991
    76
        xlat_temp_cache = mmap( NULL, XLAT_TEMP_CACHE_SIZE, PROT_EXEC|PROT_READ|PROT_WRITE,
nkeynes@991
    77
                MAP_PRIVATE|MAP_ANON, -1, 0 );
nkeynes@991
    78
        xlat_old_cache = mmap( NULL, XLAT_OLD_CACHE_SIZE, PROT_EXEC|PROT_READ|PROT_WRITE,
nkeynes@991
    79
                MAP_PRIVATE|MAP_ANON, -1, 0 );
nkeynes@991
    80
        xlat_temp_cache_ptr = xlat_temp_cache;
nkeynes@991
    81
        xlat_old_cache_ptr = xlat_old_cache;
nkeynes@991
    82
#endif
nkeynes@991
    83
//        xlat_lut = mmap( NULL, XLAT_LUT_PAGES*sizeof(void *), PROT_READ|PROT_WRITE,
nkeynes@991
    84
//                MAP_PRIVATE|MAP_ANON, -1, 0);
nkeynes@991
    85
        memset( xlat_lut, 0, XLAT_LUT_PAGES*sizeof(void *) );
nkeynes@991
    86
    }
nkeynes@991
    87
    xlat_flush_cache();
nkeynes@991
    88
}
nkeynes@991
    89
nkeynes@991
    90
/**
nkeynes@991
    91
 * Reset the cache structure to its default state
nkeynes@991
    92
 */
nkeynes@991
    93
void xlat_flush_cache() 
nkeynes@991
    94
{
nkeynes@991
    95
    xlat_cache_block_t tmp;
nkeynes@991
    96
    int i;
nkeynes@991
    97
    xlat_new_cache_ptr = xlat_new_cache;
nkeynes@991
    98
    xlat_new_cache_ptr->active = 0;
nkeynes@991
    99
    xlat_new_cache_ptr->size = XLAT_NEW_CACHE_SIZE - 2*sizeof(struct xlat_cache_block);
nkeynes@991
   100
    tmp = NEXT(xlat_new_cache_ptr);
nkeynes@991
   101
    tmp->active = 1;
nkeynes@991
   102
    tmp->size = 0;
nkeynes@991
   103
#ifdef XLAT_GENERATIONAL_CACHE
nkeynes@991
   104
    xlat_temp_cache_ptr = xlat_temp_cache;
nkeynes@991
   105
    xlat_temp_cache_ptr->active = 0;
nkeynes@991
   106
    xlat_temp_cache_ptr->size = XLAT_TEMP_CACHE_SIZE - 2*sizeof(struct xlat_cache_block);
nkeynes@991
   107
    tmp = NEXT(xlat_temp_cache_ptr);
nkeynes@991
   108
    tmp->active = 1;
nkeynes@991
   109
    tmp->size = 0;
nkeynes@991
   110
    xlat_old_cache_ptr = xlat_old_cache;
nkeynes@991
   111
    xlat_old_cache_ptr->active = 0;
nkeynes@991
   112
    xlat_old_cache_ptr->size = XLAT_OLD_CACHE_SIZE - 2*sizeof(struct xlat_cache_block);
nkeynes@991
   113
    tmp = NEXT(xlat_old_cache_ptr);
nkeynes@991
   114
    tmp->active = 1;
nkeynes@991
   115
    tmp->size = 0;
nkeynes@991
   116
#endif
nkeynes@991
   117
    for( i=0; i<XLAT_LUT_PAGES; i++ ) {
nkeynes@991
   118
        if( xlat_lut[i] != NULL ) {
nkeynes@991
   119
            memset( xlat_lut[i], 0, XLAT_LUT_PAGE_SIZE );
nkeynes@991
   120
        }
nkeynes@991
   121
    }
nkeynes@991
   122
}
nkeynes@991
   123
nkeynes@991
   124
static void xlat_flush_page_by_lut( void **page )
nkeynes@991
   125
{
nkeynes@991
   126
    int i;
nkeynes@991
   127
    for( i=0; i<XLAT_LUT_PAGE_ENTRIES; i++ ) {
nkeynes@991
   128
        if( IS_ENTRY_POINT(page[i]) ) {
nkeynes@1149
   129
            void *p = page[i];
nkeynes@1149
   130
            do {
nkeynes@1149
   131
                xlat_cache_block_t block = XLAT_BLOCK_FOR_CODE(p);
nkeynes@1149
   132
                block->active = 0;
nkeynes@1149
   133
                p = block->chain;
nkeynes@1149
   134
            } while( p != NULL );
nkeynes@991
   135
        }
nkeynes@991
   136
        page[i] = NULL;
nkeynes@991
   137
    }
nkeynes@991
   138
}
nkeynes@991
   139
nkeynes@991
   140
void FASTCALL xlat_invalidate_word( sh4addr_t addr )
nkeynes@991
   141
{
nkeynes@991
   142
    void **page = xlat_lut[XLAT_LUT_PAGE(addr)];
nkeynes@991
   143
    if( page != NULL ) {
nkeynes@991
   144
        int entry = XLAT_LUT_ENTRY(addr);
nkeynes@991
   145
        if( page[entry] != NULL ) {
nkeynes@991
   146
            xlat_flush_page_by_lut(page);
nkeynes@991
   147
        }
nkeynes@991
   148
    }
nkeynes@991
   149
}
nkeynes@991
   150
nkeynes@991
   151
void FASTCALL xlat_invalidate_long( sh4addr_t addr )
nkeynes@991
   152
{
nkeynes@991
   153
    void **page = xlat_lut[XLAT_LUT_PAGE(addr)];
nkeynes@991
   154
    if( page != NULL ) {
nkeynes@991
   155
        int entry = XLAT_LUT_ENTRY(addr);
nkeynes@991
   156
        if( *(uint64_t *)&page[entry] != 0 ) {
nkeynes@991
   157
            xlat_flush_page_by_lut(page);
nkeynes@991
   158
        }
nkeynes@991
   159
    }
nkeynes@991
   160
}
nkeynes@991
   161
nkeynes@991
   162
void FASTCALL xlat_invalidate_block( sh4addr_t address, size_t size )
nkeynes@991
   163
{
nkeynes@991
   164
    int i;
nkeynes@991
   165
    int entry_count = size >> 1; // words;
nkeynes@991
   166
    uint32_t page_no = XLAT_LUT_PAGE(address);
nkeynes@991
   167
    int entry = XLAT_LUT_ENTRY(address);
nkeynes@991
   168
    do {
nkeynes@991
   169
        void **page = xlat_lut[page_no];
nkeynes@991
   170
        int page_entries = XLAT_LUT_PAGE_ENTRIES - entry;
nkeynes@991
   171
        if( entry_count < page_entries ) {
nkeynes@991
   172
            page_entries = entry_count;
nkeynes@991
   173
        }
nkeynes@991
   174
        if( page != NULL ) {
nkeynes@991
   175
            if( page_entries == XLAT_LUT_PAGE_ENTRIES ) {
nkeynes@991
   176
                /* Overwriting the entire page anyway */
nkeynes@991
   177
                xlat_flush_page_by_lut(page);
nkeynes@991
   178
            } else {
nkeynes@991
   179
                for( i=entry; i<entry+page_entries; i++ ) {
nkeynes@991
   180
                    if( page[i] != NULL ) {
nkeynes@991
   181
                        xlat_flush_page_by_lut(page);
nkeynes@991
   182
                        break;
nkeynes@991
   183
                    }
nkeynes@991
   184
                }
nkeynes@991
   185
            }
nkeynes@991
   186
            entry_count -= page_entries;
nkeynes@991
   187
        }
nkeynes@991
   188
        page_no ++;
nkeynes@991
   189
        entry_count -= page_entries;
nkeynes@991
   190
        entry = 0;
nkeynes@991
   191
    } while( entry_count > 0 );
nkeynes@991
   192
}
nkeynes@991
   193
nkeynes@991
   194
void FASTCALL xlat_flush_page( sh4addr_t address )
nkeynes@991
   195
{
nkeynes@991
   196
    void **page = xlat_lut[XLAT_LUT_PAGE(address)];
nkeynes@991
   197
    if( page != NULL ) {
nkeynes@991
   198
        xlat_flush_page_by_lut(page);
nkeynes@991
   199
    }
nkeynes@991
   200
}
nkeynes@991
   201
nkeynes@991
   202
void * FASTCALL xlat_get_code( sh4addr_t address )
nkeynes@991
   203
{
nkeynes@991
   204
    void *result = NULL;
nkeynes@991
   205
    void **page = xlat_lut[XLAT_LUT_PAGE(address)];
nkeynes@991
   206
    if( page != NULL ) {
nkeynes@991
   207
        result = (void *)(((uintptr_t)(page[XLAT_LUT_ENTRY(address)])) & (~((uintptr_t)0x03)));
nkeynes@991
   208
    }
nkeynes@991
   209
    return result;
nkeynes@991
   210
}
nkeynes@991
   211
nkeynes@991
   212
xlat_recovery_record_t xlat_get_pre_recovery( void *code, void *native_pc )
nkeynes@991
   213
{
nkeynes@991
   214
    if( code != NULL ) {
nkeynes@991
   215
        uintptr_t pc_offset = ((uint8_t *)native_pc) - ((uint8_t *)code);
nkeynes@991
   216
        xlat_cache_block_t block = XLAT_BLOCK_FOR_CODE(code);
nkeynes@991
   217
        uint32_t count = block->recover_table_size;
nkeynes@991
   218
        xlat_recovery_record_t records = (xlat_recovery_record_t)(&block->code[block->recover_table_offset]);
nkeynes@991
   219
        uint32_t posn;
nkeynes@991
   220
        for( posn = 1; posn < count; posn++ ) {
nkeynes@1003
   221
        	if( records[posn].xlat_offset >= pc_offset ) {
nkeynes@991
   222
        		return &records[posn-1];
nkeynes@991
   223
        	}
nkeynes@991
   224
        }
nkeynes@991
   225
        return &records[count-1];
nkeynes@991
   226
    }
nkeynes@991
   227
    return NULL;	
nkeynes@991
   228
}
nkeynes@991
   229
nkeynes@991
   230
void ** FASTCALL xlat_get_lut_entry( sh4addr_t address )
nkeynes@991
   231
{
nkeynes@991
   232
    void **page = xlat_lut[XLAT_LUT_PAGE(address)];
nkeynes@991
   233
nkeynes@991
   234
    /* Add the LUT entry for the block */
nkeynes@991
   235
    if( page == NULL ) {
nkeynes@991
   236
        xlat_lut[XLAT_LUT_PAGE(address)] = page =
nkeynes@991
   237
            (void **)mmap( NULL, XLAT_LUT_PAGE_SIZE, PROT_READ|PROT_WRITE,
nkeynes@991
   238
                    MAP_PRIVATE|MAP_ANON, -1, 0 );
nkeynes@991
   239
        memset( page, 0, XLAT_LUT_PAGE_SIZE );
nkeynes@991
   240
    }
nkeynes@991
   241
nkeynes@991
   242
    return &page[XLAT_LUT_ENTRY(address)];
nkeynes@991
   243
}
nkeynes@991
   244
nkeynes@991
   245
nkeynes@991
   246
nkeynes@991
   247
uint32_t FASTCALL xlat_get_block_size( void *block )
nkeynes@991
   248
{
nkeynes@991
   249
    xlat_cache_block_t xlt = (xlat_cache_block_t)(((char *)block)-sizeof(struct xlat_cache_block));
nkeynes@991
   250
    return xlt->size;
nkeynes@991
   251
}
nkeynes@991
   252
nkeynes@991
   253
uint32_t FASTCALL xlat_get_code_size( void *block )
nkeynes@991
   254
{
nkeynes@991
   255
    xlat_cache_block_t xlt = (xlat_cache_block_t)(((char *)block)-sizeof(struct xlat_cache_block));
nkeynes@991
   256
    if( xlt->recover_table_offset == 0 ) {
nkeynes@991
   257
        return xlt->size;
nkeynes@991
   258
    } else {
nkeynes@991
   259
        return xlt->recover_table_offset;
nkeynes@991
   260
    }
nkeynes@991
   261
}
nkeynes@991
   262
nkeynes@991
   263
/**
nkeynes@991
   264
 * Cut the specified block so that it has the given size, with the remaining data
nkeynes@991
   265
 * forming a new free block. If the free block would be less than the minimum size,
nkeynes@991
   266
 * the cut is not performed.
nkeynes@991
   267
 * @return the next block after the (possibly cut) block.
nkeynes@991
   268
 */
nkeynes@991
   269
static inline xlat_cache_block_t xlat_cut_block( xlat_cache_block_t block, int cutsize )
nkeynes@991
   270
{
nkeynes@991
   271
    cutsize = (cutsize + 3) & 0xFFFFFFFC; // force word alignment
nkeynes@991
   272
    assert( cutsize <= block->size );
nkeynes@991
   273
    if( block->size > cutsize + MIN_TOTAL_SIZE ) {
nkeynes@991
   274
        int oldsize = block->size;
nkeynes@991
   275
        block->size = cutsize;
nkeynes@991
   276
        xlat_cache_block_t next = NEXT(block);
nkeynes@991
   277
        next->active = 0;
nkeynes@991
   278
        next->size = oldsize - cutsize - sizeof(struct xlat_cache_block);
nkeynes@991
   279
        return next;
nkeynes@991
   280
    } else {
nkeynes@991
   281
        return NEXT(block);
nkeynes@991
   282
    }
nkeynes@991
   283
}
nkeynes@991
   284
nkeynes@991
   285
#ifdef XLAT_GENERATIONAL_CACHE
nkeynes@991
   286
/**
nkeynes@991
   287
 * Promote a block in temp space (or elsewhere for that matter) to old space.
nkeynes@991
   288
 *
nkeynes@991
   289
 * @param block to promote.
nkeynes@991
   290
 */
nkeynes@991
   291
static void xlat_promote_to_old_space( xlat_cache_block_t block )
nkeynes@991
   292
{
nkeynes@991
   293
    int allocation = (int)-sizeof(struct xlat_cache_block);
nkeynes@991
   294
    int size = block->size;
nkeynes@991
   295
    xlat_cache_block_t curr = xlat_old_cache_ptr;
nkeynes@991
   296
    xlat_cache_block_t start_block = curr;
nkeynes@991
   297
    do {
nkeynes@991
   298
        allocation += curr->size + sizeof(struct xlat_cache_block);
nkeynes@991
   299
        curr = NEXT(curr);
nkeynes@991
   300
        if( allocation > size ) {
nkeynes@991
   301
            break; /* done */
nkeynes@991
   302
        }
nkeynes@991
   303
        if( curr->size == 0 ) { /* End-of-cache Sentinel */
nkeynes@991
   304
            /* Leave what we just released as free space and start again from the
nkeynes@991
   305
             * top of the cache
nkeynes@991
   306
             */
nkeynes@991
   307
            start_block->active = 0;
nkeynes@991
   308
            start_block->size = allocation;
nkeynes@991
   309
            allocation = (int)-sizeof(struct xlat_cache_block);
nkeynes@991
   310
            start_block = curr = xlat_old_cache;
nkeynes@991
   311
        }
nkeynes@991
   312
    } while(1);
nkeynes@991
   313
    start_block->active = 1;
nkeynes@991
   314
    start_block->size = allocation;
nkeynes@991
   315
    start_block->lut_entry = block->lut_entry;
nkeynes@1149
   316
    start_block->chain = block->chain;
nkeynes@991
   317
    start_block->fpscr_mask = block->fpscr_mask;
nkeynes@991
   318
    start_block->fpscr = block->fpscr;
nkeynes@991
   319
    start_block->recover_table_offset = block->recover_table_offset;
nkeynes@991
   320
    start_block->recover_table_size = block->recover_table_size;
nkeynes@991
   321
    *block->lut_entry = &start_block->code;
nkeynes@991
   322
    memcpy( start_block->code, block->code, block->size );
nkeynes@991
   323
    xlat_old_cache_ptr = xlat_cut_block(start_block, size );
nkeynes@991
   324
    if( xlat_old_cache_ptr->size == 0 ) {
nkeynes@991
   325
        xlat_old_cache_ptr = xlat_old_cache;
nkeynes@991
   326
    }
nkeynes@991
   327
}
nkeynes@991
   328
nkeynes@991
   329
/**
nkeynes@991
   330
 * Similarly to the above method, promotes a block to temp space.
nkeynes@991
   331
 * TODO: Try to combine these - they're nearly identical
nkeynes@991
   332
 */
nkeynes@991
   333
void xlat_promote_to_temp_space( xlat_cache_block_t block )
nkeynes@991
   334
{
nkeynes@991
   335
    int size = block->size;
nkeynes@991
   336
    int allocation = (int)-sizeof(struct xlat_cache_block);
nkeynes@991
   337
    xlat_cache_block_t curr = xlat_temp_cache_ptr;
nkeynes@991
   338
    xlat_cache_block_t start_block = curr;
nkeynes@991
   339
    do {
nkeynes@991
   340
        if( curr->active == BLOCK_USED ) {
nkeynes@991
   341
            xlat_promote_to_old_space( curr );
nkeynes@991
   342
        } else if( curr->active == BLOCK_ACTIVE ) {
nkeynes@991
   343
            // Active but not used, release block
nkeynes@991
   344
            *((uintptr_t *)curr->lut_entry) &= ((uintptr_t)0x03);
nkeynes@991
   345
        }
nkeynes@991
   346
        allocation += curr->size + sizeof(struct xlat_cache_block);
nkeynes@991
   347
        curr = NEXT(curr);
nkeynes@991
   348
        if( allocation > size ) {
nkeynes@991
   349
            break; /* done */
nkeynes@991
   350
        }
nkeynes@991
   351
        if( curr->size == 0 ) { /* End-of-cache Sentinel */
nkeynes@991
   352
            /* Leave what we just released as free space and start again from the
nkeynes@991
   353
             * top of the cache
nkeynes@991
   354
             */
nkeynes@991
   355
            start_block->active = 0;
nkeynes@991
   356
            start_block->size = allocation;
nkeynes@991
   357
            allocation = (int)-sizeof(struct xlat_cache_block);
nkeynes@991
   358
            start_block = curr = xlat_temp_cache;
nkeynes@991
   359
        }
nkeynes@991
   360
    } while(1);
nkeynes@991
   361
    start_block->active = 1;
nkeynes@991
   362
    start_block->size = allocation;
nkeynes@991
   363
    start_block->lut_entry = block->lut_entry;
nkeynes@1149
   364
    start_block->chain = block->chain;
nkeynes@991
   365
    start_block->fpscr_mask = block->fpscr_mask;
nkeynes@991
   366
    start_block->fpscr = block->fpscr;
nkeynes@991
   367
    start_block->recover_table_offset = block->recover_table_offset;
nkeynes@991
   368
    start_block->recover_table_size = block->recover_table_size;
nkeynes@991
   369
    *block->lut_entry = &start_block->code;
nkeynes@991
   370
    memcpy( start_block->code, block->code, block->size );
nkeynes@991
   371
    xlat_temp_cache_ptr = xlat_cut_block(start_block, size );
nkeynes@991
   372
    if( xlat_temp_cache_ptr->size == 0 ) {
nkeynes@991
   373
        xlat_temp_cache_ptr = xlat_temp_cache;
nkeynes@991
   374
    }
nkeynes@991
   375
nkeynes@991
   376
}
nkeynes@991
   377
#else 
nkeynes@991
   378
void xlat_promote_to_temp_space( xlat_cache_block_t block )
nkeynes@991
   379
{
nkeynes@991
   380
    *block->lut_entry = 0;
nkeynes@991
   381
}
nkeynes@991
   382
#endif
nkeynes@991
   383
nkeynes@991
   384
/**
nkeynes@991
   385
 * Returns the next block in the new cache list that can be written to by the
nkeynes@991
   386
 * translator. If the next block is active, it is evicted first.
nkeynes@991
   387
 */
nkeynes@991
   388
xlat_cache_block_t xlat_start_block( sh4addr_t address )
nkeynes@991
   389
{
nkeynes@991
   390
    if( xlat_new_cache_ptr->size == 0 ) {
nkeynes@991
   391
        xlat_new_cache_ptr = xlat_new_cache;
nkeynes@991
   392
    }
nkeynes@991
   393
nkeynes@991
   394
    if( xlat_new_cache_ptr->active ) {
nkeynes@991
   395
        xlat_promote_to_temp_space( xlat_new_cache_ptr );
nkeynes@991
   396
    }
nkeynes@991
   397
    xlat_new_create_ptr = xlat_new_cache_ptr;
nkeynes@991
   398
    xlat_new_create_ptr->active = 1;
nkeynes@991
   399
    xlat_new_cache_ptr = NEXT(xlat_new_cache_ptr);
nkeynes@991
   400
nkeynes@991
   401
    /* Add the LUT entry for the block */
nkeynes@991
   402
    if( xlat_lut[XLAT_LUT_PAGE(address)] == NULL ) {
nkeynes@991
   403
        xlat_lut[XLAT_LUT_PAGE(address)] =
nkeynes@991
   404
            (void **)mmap( NULL, XLAT_LUT_PAGE_SIZE, PROT_READ|PROT_WRITE,
nkeynes@991
   405
                    MAP_PRIVATE|MAP_ANON, -1, 0 );
nkeynes@991
   406
        memset( xlat_lut[XLAT_LUT_PAGE(address)], 0, XLAT_LUT_PAGE_SIZE );
nkeynes@991
   407
    }
nkeynes@991
   408
nkeynes@991
   409
    if( IS_ENTRY_POINT(xlat_lut[XLAT_LUT_PAGE(address)][XLAT_LUT_ENTRY(address)]) ) {
nkeynes@1149
   410
        void *p = xlat_lut[XLAT_LUT_PAGE(address)][XLAT_LUT_ENTRY(address)];
nkeynes@1149
   411
        xlat_cache_block_t oldblock = XLAT_BLOCK_FOR_CODE(p);
nkeynes@1149
   412
        assert( oldblock->active );
nkeynes@1149
   413
        xlat_new_create_ptr->chain = p;
nkeynes@1149
   414
    } else {
nkeynes@1149
   415
        xlat_new_create_ptr->chain = NULL;
nkeynes@991
   416
    }
nkeynes@991
   417
nkeynes@991
   418
    xlat_lut[XLAT_LUT_PAGE(address)][XLAT_LUT_ENTRY(address)] = 
nkeynes@991
   419
        &xlat_new_create_ptr->code;
nkeynes@991
   420
    xlat_new_create_ptr->lut_entry = xlat_lut[XLAT_LUT_PAGE(address)] + XLAT_LUT_ENTRY(address);
nkeynes@991
   421
nkeynes@991
   422
    return xlat_new_create_ptr;
nkeynes@991
   423
}
nkeynes@991
   424
nkeynes@991
   425
xlat_cache_block_t xlat_extend_block( uint32_t newSize )
nkeynes@991
   426
{
nkeynes@991
   427
    while( xlat_new_create_ptr->size < newSize ) {
nkeynes@991
   428
        if( xlat_new_cache_ptr->size == 0 ) {
nkeynes@991
   429
            /* Migrate to the front of the cache to keep it contiguous */
nkeynes@991
   430
            xlat_new_create_ptr->active = 0;
nkeynes@991
   431
            sh4ptr_t olddata = xlat_new_create_ptr->code;
nkeynes@991
   432
            int oldsize = xlat_new_create_ptr->size;
nkeynes@991
   433
            int size = oldsize + MIN_BLOCK_SIZE; /* minimum expansion */
nkeynes@991
   434
            void **lut_entry = xlat_new_create_ptr->lut_entry;
nkeynes@1149
   435
            void *chain = xlat_new_create_ptr->chain;
nkeynes@991
   436
            int allocation = (int)-sizeof(struct xlat_cache_block);
nkeynes@991
   437
            xlat_new_cache_ptr = xlat_new_cache;
nkeynes@991
   438
            do {
nkeynes@991
   439
                if( xlat_new_cache_ptr->active ) {
nkeynes@991
   440
                    xlat_promote_to_temp_space( xlat_new_cache_ptr );
nkeynes@991
   441
                }
nkeynes@991
   442
                allocation += xlat_new_cache_ptr->size + sizeof(struct xlat_cache_block);
nkeynes@991
   443
                xlat_new_cache_ptr = NEXT(xlat_new_cache_ptr);
nkeynes@991
   444
            } while( allocation < size );
nkeynes@991
   445
            xlat_new_create_ptr = xlat_new_cache;
nkeynes@991
   446
            xlat_new_create_ptr->active = 1;
nkeynes@991
   447
            xlat_new_create_ptr->size = allocation;
nkeynes@991
   448
            xlat_new_create_ptr->lut_entry = lut_entry;
nkeynes@1149
   449
            xlat_new_create_ptr->chain = chain;
nkeynes@991
   450
            *lut_entry = &xlat_new_create_ptr->code;
nkeynes@991
   451
            memmove( xlat_new_create_ptr->code, olddata, oldsize );
nkeynes@991
   452
        } else {
nkeynes@991
   453
            if( xlat_new_cache_ptr->active ) {
nkeynes@991
   454
                xlat_promote_to_temp_space( xlat_new_cache_ptr );
nkeynes@991
   455
            }
nkeynes@991
   456
            xlat_new_create_ptr->size += xlat_new_cache_ptr->size + sizeof(struct xlat_cache_block);
nkeynes@991
   457
            xlat_new_cache_ptr = NEXT(xlat_new_cache_ptr);
nkeynes@991
   458
        }
nkeynes@991
   459
    }
nkeynes@991
   460
    return xlat_new_create_ptr;
nkeynes@991
   461
nkeynes@991
   462
}
nkeynes@991
   463
nkeynes@991
   464
void xlat_commit_block( uint32_t destsize, uint32_t srcsize )
nkeynes@991
   465
{
nkeynes@991
   466
    void **ptr = xlat_new_create_ptr->lut_entry;
nkeynes@1126
   467
    void **endptr = ptr + (srcsize>>1);
nkeynes@991
   468
    while( ptr < endptr ) {
nkeynes@991
   469
        if( *ptr == NULL ) {
nkeynes@991
   470
            *ptr = XLAT_LUT_ENTRY_USED;
nkeynes@991
   471
        }
nkeynes@991
   472
        ptr++;
nkeynes@991
   473
    }
nkeynes@991
   474
nkeynes@991
   475
    xlat_new_cache_ptr = xlat_cut_block( xlat_new_create_ptr, destsize );
nkeynes@991
   476
}
nkeynes@991
   477
nkeynes@991
   478
void xlat_delete_block( xlat_cache_block_t block ) 
nkeynes@991
   479
{
nkeynes@991
   480
    block->active = 0;
nkeynes@991
   481
    *block->lut_entry = NULL;
nkeynes@991
   482
}
nkeynes@991
   483
nkeynes@991
   484
void xlat_check_cache_integrity( xlat_cache_block_t cache, xlat_cache_block_t ptr, int size )
nkeynes@991
   485
{
nkeynes@991
   486
    int foundptr = 0;
nkeynes@991
   487
    xlat_cache_block_t tail = 
nkeynes@991
   488
        (xlat_cache_block_t)(((char *)cache) + size - sizeof(struct xlat_cache_block));
nkeynes@991
   489
nkeynes@991
   490
    assert( tail->active == 1 );
nkeynes@991
   491
    assert( tail->size == 0 ); 
nkeynes@991
   492
    while( cache < tail ) {
nkeynes@991
   493
        assert( cache->active >= 0 && cache->active <= 2 );
nkeynes@991
   494
        assert( cache->size >= 0 && cache->size < size );
nkeynes@991
   495
        if( cache == ptr ) {
nkeynes@991
   496
            foundptr = 1;
nkeynes@991
   497
        }
nkeynes@991
   498
        cache = NEXT(cache);
nkeynes@991
   499
    }
nkeynes@991
   500
    assert( cache == tail );
nkeynes@991
   501
    assert( foundptr == 1 || tail == ptr );
nkeynes@991
   502
}
nkeynes@991
   503
nkeynes@1091
   504
/**
nkeynes@1175
   505
 * Perform a reverse lookup to determine the SH4 address corresponding to
nkeynes@1175
   506
 * the start of the code block containing ptr. This is _slow_ - it does a
nkeynes@1175
   507
 * linear scan of the lookup table to find this.
nkeynes@1175
   508
 *
nkeynes@1175
   509
 * If the pointer cannot be found in any live block, returns -1 (as this
nkeynes@1175
   510
 * is not a legal PC)
nkeynes@1175
   511
 */
nkeynes@1175
   512
sh4addr_t xlat_get_address( unsigned char *ptr )
nkeynes@1175
   513
{
nkeynes@1175
   514
    int i,j;
nkeynes@1175
   515
    for( i=0; i<XLAT_LUT_PAGES; i++ ) {
nkeynes@1175
   516
        void **page = xlat_lut[i];
nkeynes@1175
   517
        if( page != NULL ) {
nkeynes@1175
   518
            for( j=0; j<XLAT_LUT_PAGE_ENTRIES; j++ ) {
nkeynes@1175
   519
                void *entry = page[j];
nkeynes@1175
   520
                if( ((uintptr_t)entry) > XLAT_LUT_ENTRY_USED ) {
nkeynes@1175
   521
                    xlat_cache_block_t block = XLAT_BLOCK_FOR_CODE(entry);
nkeynes@1175
   522
                    if( ptr >= block->code && ptr < block->code + block->size) {
nkeynes@1175
   523
                        /* Found it */
nkeynes@1175
   524
                        return (i<<13) | (j<<1);
nkeynes@1175
   525
                    }
nkeynes@1175
   526
                }
nkeynes@1175
   527
            }
nkeynes@1175
   528
        }
nkeynes@1175
   529
    }
nkeynes@1175
   530
    return -1;
nkeynes@1175
   531
}
nkeynes@1175
   532
nkeynes@1175
   533
/**
nkeynes@1091
   534
 * Sanity check that the given pointer is at least contained in one of cache
nkeynes@1091
   535
 * regions, and has a sane-ish size. We don't do a full region walk atm.
nkeynes@1091
   536
 */
nkeynes@1091
   537
gboolean xlat_is_code_pointer( void *p )
nkeynes@1091
   538
{
nkeynes@1091
   539
    char *region;
nkeynes@1091
   540
    uintptr_t region_size;
nkeynes@1091
   541
nkeynes@1091
   542
    xlat_cache_block_t block = XLAT_BLOCK_FOR_CODE(p);
nkeynes@1091
   543
    if( (((char *)block) - (char *)xlat_new_cache) < XLAT_NEW_CACHE_SIZE ) {
nkeynes@1091
   544
         /* Pointer is in new cache */
nkeynes@1091
   545
        region = (char *)xlat_new_cache;
nkeynes@1091
   546
        region_size = XLAT_NEW_CACHE_SIZE;
nkeynes@1091
   547
    }
nkeynes@1091
   548
#ifdef XLAT_GENERATIONAL_CACHE
nkeynes@1091
   549
    else if( (((char *)block) - (char *)xlat_temp_cache) < XLAT_TEMP_CACHE_SIZE ) {
nkeynes@1091
   550
         /* Pointer is in temp cache */
nkeynes@1091
   551
        region = (char *)xlat_temp_cache;
nkeynes@1091
   552
        region_size = XLAT_TEMP_CACHE_SIZE;
nkeynes@1091
   553
    } else if( (((char *)block) - (char *)xlat_odl_cache) < XLAT_OLD_CACHE_SIZE ) {
nkeynes@1091
   554
        /* Pointer is in old cache */
nkeynes@1091
   555
        region = (char *)xlat_old_cache;
nkeynes@1091
   556
        region_size = XLAT_OLD_CACHE_SIZE;
nkeynes@1091
   557
    }
nkeynes@1091
   558
#endif
nkeynes@1091
   559
    else {
nkeynes@1091
   560
        /* Not a valid cache pointer */
nkeynes@1091
   561
        return FALSE;
nkeynes@1091
   562
    }
nkeynes@1091
   563
nkeynes@1091
   564
    /* Make sure the whole block is in the region */
nkeynes@1091
   565
    if( (((char *)p) - region) >= region_size ||
nkeynes@1091
   566
        (((char *)(NEXT(block))) - region) >= region_size )
nkeynes@1091
   567
        return FALSE;
nkeynes@1091
   568
    return TRUE;
nkeynes@1091
   569
}
nkeynes@1091
   570
nkeynes@991
   571
void xlat_check_integrity( )
nkeynes@991
   572
{
nkeynes@991
   573
    xlat_check_cache_integrity( xlat_new_cache, xlat_new_cache_ptr, XLAT_NEW_CACHE_SIZE );
nkeynes@991
   574
#ifdef XLAT_GENERATIONAL_CACHE
nkeynes@991
   575
    xlat_check_cache_integrity( xlat_temp_cache, xlat_temp_cache_ptr, XLAT_TEMP_CACHE_SIZE );
nkeynes@991
   576
    xlat_check_cache_integrity( xlat_old_cache, xlat_old_cache_ptr, XLAT_OLD_CACHE_SIZE );
nkeynes@991
   577
#endif
nkeynes@991
   578
}
nkeynes@991
   579
nkeynes@1182
   580
typedef struct {
nkeynes@1182
   581
    xlat_cache_block_t block;
nkeynes@1182
   582
    sh4addr_t sh4_pc;
nkeynes@1182
   583
} block_sh4_entry;
nkeynes@1182
   584
nkeynes@1182
   585
unsigned int xlat_get_active_block_count()
nkeynes@1182
   586
{
nkeynes@1182
   587
    unsigned int count = 0;
nkeynes@1182
   588
    xlat_cache_block_t ptr = xlat_new_cache;
nkeynes@1182
   589
    while( ptr->size != 0 ) {
nkeynes@1182
   590
        if( ptr->active != 0 ) {
nkeynes@1182
   591
            count++;
nkeynes@1182
   592
        }
nkeynes@1182
   593
        ptr = NEXT(ptr);
nkeynes@1182
   594
    }
nkeynes@1182
   595
    return count;
nkeynes@1182
   596
}
nkeynes@1182
   597
nkeynes@1182
   598
unsigned int xlat_get_active_blocks( block_sh4_entry *blocks, unsigned int size )
nkeynes@1182
   599
{
nkeynes@1182
   600
    unsigned int count = 0;
nkeynes@1182
   601
    xlat_cache_block_t ptr = xlat_new_cache;
nkeynes@1182
   602
    while( ptr->size != 0 ) {
nkeynes@1182
   603
        if( ptr->active != 0 ) {
nkeynes@1182
   604
            blocks[count].block = ptr;
nkeynes@1182
   605
            blocks[count].sh4_pc = 0;
nkeynes@1182
   606
            count++;
nkeynes@1182
   607
        }
nkeynes@1182
   608
        if( count >= size )
nkeynes@1182
   609
            break;
nkeynes@1182
   610
        ptr = NEXT(ptr);
nkeynes@1182
   611
    }
nkeynes@1182
   612
    return count;
nkeynes@1182
   613
}
nkeynes@1182
   614
nkeynes@1182
   615
void xlat_get_block_sh4addrs( block_sh4_entry *blocks, unsigned int size )
nkeynes@1182
   616
{
nkeynes@1182
   617
    unsigned i;
nkeynes@1182
   618
    for( i=0; i<XLAT_LUT_PAGES;i ++ ) {
nkeynes@1182
   619
        void **page = xlat_lut[i];
nkeynes@1182
   620
        if( page != NULL ) {
nkeynes@1182
   621
            for( unsigned j=0; j < XLAT_LUT_PAGE_ENTRIES; j++ ) {
nkeynes@1182
   622
                void *code = (void *)(((uintptr_t)(page[j])) & (~((uintptr_t)0x03)));
nkeynes@1182
   623
                if( code != NULL ) {
nkeynes@1182
   624
                    xlat_cache_block_t ptr = XLAT_BLOCK_FOR_CODE(code);
nkeynes@1182
   625
                    sh4addr_t pc = XLAT_ADDR_FROM_ENTRY(i,j);
nkeynes@1182
   626
                    for( unsigned k=0; k<size; k++ ) {
nkeynes@1182
   627
                        if( blocks[k].block == ptr ) {
nkeynes@1182
   628
                            blocks[k].sh4_pc = pc;
nkeynes@1182
   629
                            ptr = ptr->chain;
nkeynes@1182
   630
                            if( ptr == NULL )
nkeynes@1182
   631
                                break;
nkeynes@1182
   632
                            else {
nkeynes@1182
   633
                                ptr = XLAT_BLOCK_FOR_CODE(ptr);
nkeynes@1182
   634
                                k = 0;
nkeynes@1182
   635
                            }
nkeynes@1182
   636
                        }
nkeynes@1182
   637
                    }
nkeynes@1182
   638
                }
nkeynes@1182
   639
            }
nkeynes@1182
   640
        }
nkeynes@1182
   641
    }
nkeynes@1182
   642
}
nkeynes@1182
   643
nkeynes@1182
   644
static int xlat_compare_active_field( const void *a, const void *b )
nkeynes@1182
   645
{
nkeynes@1182
   646
    const block_sh4_entry *ptra = (const block_sh4_entry *)a;
nkeynes@1182
   647
    const block_sh4_entry *ptrb = (const block_sh4_entry *)b;
nkeynes@1182
   648
    return ptrb->block->active - ptra->block->active;
nkeynes@1182
   649
}
nkeynes@1182
   650
nkeynes@1182
   651
void xlat_dump_cache_by_activity( unsigned int topN )
nkeynes@1182
   652
{
nkeynes@1182
   653
    int i=0;
nkeynes@1182
   654
    int count = xlat_get_active_block_count();
nkeynes@1182
   655
nkeynes@1182
   656
    block_sh4_entry blocks[count];
nkeynes@1182
   657
    xlat_get_active_blocks(blocks, count);
nkeynes@1182
   658
    xlat_get_block_sh4addrs(blocks,count);
nkeynes@1182
   659
    qsort(blocks, count, sizeof(block_sh4_entry), xlat_compare_active_field);
nkeynes@1182
   660
nkeynes@1182
   661
    if( topN == 0 || topN > count )
nkeynes@1182
   662
        topN = count;
nkeynes@1182
   663
    for( unsigned int i=0; i<topN; i++ ) {
nkeynes@1182
   664
        fprintf(stderr, "0x%08X (%p): %d\n", blocks[i].sh4_pc, blocks[i].block->code, blocks[i].block->active);
nkeynes@1182
   665
        sh4_translate_disasm_block( stderr, blocks[i].block->code, blocks[i].sh4_pc, NULL );
nkeynes@1182
   666
        fprintf(stderr, "\n");
nkeynes@1182
   667
    }
nkeynes@1182
   668
}
.