1 // See LICENSE for license details.
11 #include "processor.h"
12 #include "memtracer.h"
16 // virtual memory configuration
18 const reg_t PGSIZE
= 1 << PGSHIFT
;
26 struct icache_entry_t
{
32 // this class implements a processor's port into the virtual memory system.
33 // an MMU and instruction cache are maintained for simulator performance.
37 mmu_t(sim_t
* sim
, processor_t
* proc
);
40 // template for functions that load an aligned value from memory
41 #define load_func(type) \
42 type##_t load_##type(reg_t addr) __attribute__((always_inline)) { \
43 if (addr & (sizeof(type##_t)-1)) \
44 throw trap_load_address_misaligned(addr); \
45 reg_t vpn = addr >> PGSHIFT; \
46 if (likely(tlb_load_tag[vpn % TLB_ENTRIES] == vpn)) \
47 return *(type##_t*)(tlb_data[vpn % TLB_ENTRIES] + addr); \
49 load_slow_path(addr, sizeof(type##_t), (uint8_t*)&res); \
53 // load value from memory at aligned address; zero extend to register width
59 // load value from memory at aligned address; sign extend to register width
65 // template for functions that store an aligned value to memory
66 #define store_func(type) \
67 void store_##type(reg_t addr, type##_t val) { \
68 if (addr & (sizeof(type##_t)-1)) \
69 throw trap_store_address_misaligned(addr); \
70 reg_t vpn = addr >> PGSHIFT; \
71 if (likely(tlb_store_tag[vpn % TLB_ENTRIES] == vpn)) \
72 *(type##_t*)(tlb_data[vpn % TLB_ENTRIES] + addr) = val; \
74 store_slow_path(addr, sizeof(type##_t), (const uint8_t*)&val); \
77 // store value to memory at aligned address
83 static const reg_t ICACHE_ENTRIES
= 1024;
85 inline size_t icache_index(reg_t addr
)
87 return (addr
/ PC_ALIGN
) % ICACHE_ENTRIES
;
90 inline icache_entry_t
* refill_icache(reg_t addr
, icache_entry_t
* entry
)
92 const uint16_t* iaddr
= translate_insn_addr(addr
);
93 insn_bits_t insn
= *iaddr
;
94 int length
= insn_length(insn
);
96 if (likely(length
== 4)) {
97 insn
|= (insn_bits_t
)*(const int16_t*)translate_insn_addr(addr
+ 2) << 16;
98 } else if (length
== 2) {
100 } else if (length
== 6) {
101 insn
|= (insn_bits_t
)*(const int16_t*)translate_insn_addr(addr
+ 4) << 32;
102 insn
|= (insn_bits_t
)*(const uint16_t*)translate_insn_addr(addr
+ 2) << 16;
104 static_assert(sizeof(insn_bits_t
) == 8, "insn_bits_t must be uint64_t");
105 insn
|= (insn_bits_t
)*(const int16_t*)translate_insn_addr(addr
+ 6) << 48;
106 insn
|= (insn_bits_t
)*(const uint16_t*)translate_insn_addr(addr
+ 4) << 32;
107 insn
|= (insn_bits_t
)*(const uint16_t*)translate_insn_addr(addr
+ 2) << 16;
110 insn_fetch_t fetch
= {proc
->decode_insn(insn
), insn
};
114 reg_t paddr
= sim
->mem_to_addr((char*)iaddr
);
115 if (tracer
.interested_in_range(paddr
, paddr
+ 1, FETCH
)) {
117 tracer
.trace(paddr
, length
, FETCH
);
122 inline icache_entry_t
* access_icache(reg_t addr
)
124 icache_entry_t
* entry
= &icache
[icache_index(addr
)];
125 if (likely(entry
->tag
== addr
))
127 return refill_icache(addr
, entry
);
130 inline insn_fetch_t
load_insn(reg_t addr
)
132 return access_icache(addr
)->data
;
138 void register_memtracer(memtracer_t
*);
143 memtracer_list_t tracer
;
146 // implement an instruction cache for simulator performance
147 icache_entry_t icache
[ICACHE_ENTRIES
];
149 // implement a TLB for simulator performance
150 static const reg_t TLB_ENTRIES
= 256;
151 char* tlb_data
[TLB_ENTRIES
];
152 reg_t tlb_insn_tag
[TLB_ENTRIES
];
153 reg_t tlb_load_tag
[TLB_ENTRIES
];
154 reg_t tlb_store_tag
[TLB_ENTRIES
];
156 // finish translation on a TLB miss and upate the TLB
157 void refill_tlb(reg_t vaddr
, reg_t paddr
, access_type type
);
159 // perform a page table walk for a given VA; set referenced/dirty bits
160 reg_t
walk(reg_t addr
, access_type type
, bool supervisor
, bool pum
);
162 // handle uncommon cases: TLB misses, page faults, MMIO
163 const uint16_t* fetch_slow_path(reg_t addr
);
164 void load_slow_path(reg_t addr
, reg_t len
, uint8_t* bytes
);
165 void store_slow_path(reg_t addr
, reg_t len
, const uint8_t* bytes
);
166 reg_t
translate(reg_t addr
, access_type type
);
169 const uint16_t* translate_insn_addr(reg_t addr
) __attribute__((always_inline
)) {
170 reg_t vpn
= addr
>> PGSHIFT
;
171 if (likely(tlb_insn_tag
[vpn
% TLB_ENTRIES
] == vpn
))
172 return (uint16_t*)(tlb_data
[vpn
% TLB_ENTRIES
] + addr
);
173 return fetch_slow_path(addr
);
176 friend class processor_t
;