1 // See LICENSE for license details.
10 #include "processor.h"
11 #include "memtracer.h"
14 // virtual memory configuration
16 const reg_t LEVELS
= sizeof(pte_t
) == 8 ? 3 : 2;
17 const reg_t PTIDXBITS
= 10;
18 const reg_t PGSHIFT
= PTIDXBITS
+ (sizeof(pte_t
) == 8 ? 3 : 2);
19 const reg_t PGSIZE
= 1 << PGSHIFT
;
20 const reg_t VPN_BITS
= PTIDXBITS
* LEVELS
;
21 const reg_t PPN_BITS
= 8*sizeof(reg_t
) - PGSHIFT
;
22 const reg_t VA_BITS
= VPN_BITS
+ PGSHIFT
;
24 // this class implements a processor's port into the virtual memory system.
25 // an MMU and instruction cache are maintained for simulator performance.
29 mmu_t(char* _mem
, size_t _memsz
);
32 // template for functions that load an aligned value from memory
33 #define load_func(type) \
34 type##_t load_##type(reg_t addr) { \
35 if(unlikely(addr % sizeof(type##_t))) \
36 throw trap_load_address_misaligned(addr); \
37 reg_t paddr = translate(addr, sizeof(type##_t), false, false); \
38 return *(type##_t*)(mem + paddr); \
41 // load value from memory at aligned address; zero extend to register width
47 // load value from memory at aligned address; sign extend to register width
53 // template for functions that store an aligned value to memory
54 #define store_func(type) \
55 void store_##type(reg_t addr, type##_t val) { \
56 if(unlikely(addr % sizeof(type##_t))) \
57 throw trap_store_address_misaligned(addr); \
58 reg_t paddr = translate(addr, sizeof(type##_t), true, false); \
59 *(type##_t*)(mem + paddr) = val; \
62 // store value to memory at aligned address
77 // load instruction from memory at aligned address.
78 inline insn_fetch_t
load_insn(reg_t addr
)
80 reg_t idx
= (addr
/sizeof(insn_t
)) % ICACHE_ENTRIES
;
81 if (unlikely(icache_tag
[idx
] != addr
))
83 reg_t paddr
= translate(addr
, sizeof(insn_t
), false, true);
85 fetch
.insn
.insn
= *(insn_t
*)(mem
+ paddr
);
86 fetch
.func
= proc
->decode_insn(fetch
.insn
.insn
);
88 reg_t idx
= (paddr
/sizeof(insn_t
)) % ICACHE_ENTRIES
;
89 icache_tag
[idx
] = addr
;
90 icache_data
[idx
] = fetch
;
92 if (tracer
.interested_in_range(paddr
, paddr
+ sizeof(insn_t
), false, true))
95 tracer
.trace(paddr
, sizeof(insn_t
), false, true);
98 return icache_data
[idx
];
101 void set_processor(processor_t
* p
) { proc
= p
; flush_tlb(); }
106 void register_memtracer(memtracer_t
*);
112 memtracer_list_t tracer
;
114 // implement an instruction cache for simulator performance
115 static const reg_t ICACHE_ENTRIES
= 256;
116 insn_fetch_t icache_data
[ICACHE_ENTRIES
];
118 // implement a TLB for simulator performance
119 static const reg_t TLB_ENTRIES
= 256;
120 reg_t tlb_data
[TLB_ENTRIES
];
121 reg_t tlb_insn_tag
[TLB_ENTRIES
];
122 reg_t tlb_load_tag
[TLB_ENTRIES
];
123 reg_t tlb_store_tag
[TLB_ENTRIES
];
124 reg_t icache_tag
[ICACHE_ENTRIES
];
126 // finish translation on a TLB miss and upate the TLB
127 reg_t
refill_tlb(reg_t addr
, reg_t bytes
, bool store
, bool fetch
);
129 // perform a page table walk for a given virtual address
130 pte_t
walk(reg_t addr
);
132 // translate a virtual address to a physical address
133 reg_t
translate(reg_t addr
, reg_t bytes
, bool store
, bool fetch
)
135 reg_t idx
= (addr
>> PGSHIFT
) % TLB_ENTRIES
;
137 reg_t
* tlb_tag
= fetch
? tlb_insn_tag
: store
? tlb_store_tag
:tlb_load_tag
;
138 reg_t expected_tag
= addr
& ~(PGSIZE
-1);
139 if(likely(tlb_tag
[idx
] == expected_tag
))
140 return ((uintptr_t)addr
& (PGSIZE
-1)) + tlb_data
[idx
];
142 return refill_tlb(addr
, bytes
, store
, fetch
);
145 friend class processor_t
;