Skip to content

Commit f657fb2

Browse files
committed
Fix victim cache fill logic
Previous implementation did not correctly place the evicted I-cache block into the victim cache, leaving all victim entries empty and thus never hit. This patch properly stores the replaced I-cache block into the victim cache before refill, allowing victim hits to function as intended. Measurement shows that the number of virtual-to-physical translations during instruction fetch (mmu_translate() calls) decreased by ~7%.
1 parent 502d099 commit f657fb2

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

riscv.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,13 +384,16 @@ static void mmu_fetch(hart_t *vm, uint32_t addr, uint32_t *value)
384384
}
385385

386386
/* search the victim cache */
387+
uint32_t vcache_key = addr >> ICACHE_OFFSET_BITS;
387388
for (int i = 0; i < VCACHE_BLOCKS; i++) {
388389
victim_cache_block_t *vblk = &vm->icache.v_block[i];
389-
if (vblk->valid && vblk->tag == tag) {
390-
/* victim cache hit, block swap*/
390+
391+
/* victim cache hit, swap blocks */
392+
if (vblk->valid && vblk->tag == vcache_key) {
391393
icache_block_t tmp = *blk;
392394
*blk = *vblk;
393395
*vblk = tmp;
396+
blk->tag = tag;
394397

395398
uint32_t ofs = addr & ICACHE_BLOCK_MASK;
396399
*value = *(const uint32_t *) (blk->base + ofs);
@@ -402,7 +405,7 @@ static void mmu_fetch(hart_t *vm, uint32_t addr, uint32_t *value)
402405
vm->cache_fetch.misses++;
403406
#endif
404407

405-
/* cache miss, Continue using the original va->pa*/
408+
/* icache miss, Continue using the original va->pa*/
406409
uint32_t vpn = addr >> RV_PAGE_SHIFT;
407410
uint32_t index = __builtin_parity(vpn) & 0x1;
408411
if (unlikely(vpn != vm->cache_fetch[index].n_pages)) {
@@ -421,7 +424,16 @@ static void mmu_fetch(hart_t *vm, uint32_t addr, uint32_t *value)
421424
*value =
422425
vm->cache_fetch[index].page_addr[(addr >> 2) & MASK(RV_PAGE_SHIFT - 2)];
423426

424-
/* fill into the cache */
427+
/* Move the current icache block into the victim cache before replacement */
428+
if (blk->valid) {
429+
victim_cache_block_t *vblk = &vm->icache.v_block[vm->icache.v_next];
430+
*vblk = *blk;
431+
vblk->tag = (blk->tag << ICACHE_INDEX_BITS) | idx;
432+
vblk->valid = true;
433+
vm->icache.v_next = (vm->icache.v_next + 1) % VCACHE_BLOCKS;
434+
}
435+
436+
/* fill into the icache */
425437
uint32_t block_off = (addr & RV_PAGE_MASK) & ~ICACHE_BLOCK_MASK;
426438
blk->base = (const uint8_t *) vm->cache_fetch[index].page_addr + block_off;
427439
blk->tag = tag;

riscv.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ typedef icache_block_t victim_cache_block_t;
119119
typedef struct {
120120
icache_block_t i_block[ICACHE_BLOCKS];
121121
victim_cache_block_t v_block[VCACHE_BLOCKS];
122+
uint32_t v_next;
122123
} icache_t;
123124

124125
struct __hart_internal {

0 commit comments

Comments
 (0)