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