add comment
[riscv-isa-sim.git] / riscv / execute.cc
1 // See LICENSE for license details.
2
3 #include "processor.h"
4 #include "mmu.h"
5 #include <cassert>
6
7
8 static void commit_log_stash_privilege(processor_t* p)
9 {
10 #ifdef RISCV_ENABLE_COMMITLOG
11 state_t* state = p->get_state();
12 state->last_inst_priv = state->prv;
13 state->last_inst_xlen = p->get_xlen();
14 state->last_inst_flen = p->get_flen();
15 #endif
16 }
17
18 static void commit_log_print_value(int width, uint64_t hi, uint64_t lo)
19 {
20 switch (width) {
21 case 16:
22 fprintf(stderr, "0x%04" PRIx16, (uint16_t)lo);
23 break;
24 case 32:
25 fprintf(stderr, "0x%08" PRIx32, (uint32_t)lo);
26 break;
27 case 64:
28 fprintf(stderr, "0x%016" PRIx64, lo);
29 break;
30 case 128:
31 fprintf(stderr, "0x%016" PRIx64 "%016" PRIx64, hi, lo);
32 break;
33 default:
34 abort();
35 }
36 }
37
38 static void commit_log_print_insn(state_t* state, reg_t pc, insn_t insn)
39 {
40 #ifdef RISCV_ENABLE_COMMITLOG
41 auto& reg = state->log_reg_write;
42 int priv = state->last_inst_priv;
43 int xlen = state->last_inst_xlen;
44 int flen = state->last_inst_flen;
45
46 fprintf(stderr, "%1d ", priv);
47 commit_log_print_value(xlen, 0, pc);
48 fprintf(stderr, " (");
49 commit_log_print_value(insn.length() * 8, 0, insn.bits());
50
51 if (reg.addr) {
52 bool fp = reg.addr & 1;
53 int rd = reg.addr >> 1;
54 int size = fp ? flen : xlen;
55 fprintf(stderr, ") %c%2d ", fp ? 'f' : 'x', rd);
56 commit_log_print_value(size, reg.data.v[1], reg.data.v[0]);
57 fprintf(stderr, "\n");
58 } else {
59 fprintf(stderr, ")\n");
60 }
61 reg.addr = 0;
62 #endif
63 }
64
65 inline void processor_t::update_histogram(reg_t pc)
66 {
67 #ifdef RISCV_ENABLE_HISTOGRAM
68 pc_histogram[pc]++;
69 #endif
70 }
71
72 // This is expected to be inlined by the compiler so each use of execute_insn
73 // includes a duplicated body of the function to get separate fetch.func
74 // function calls.
75 static reg_t execute_insn(processor_t* p, reg_t pc, insn_fetch_t fetch)
76 {
77 commit_log_stash_privilege(p);
78 reg_t npc = fetch.func(p, fetch.insn, pc);
79 if (npc != PC_SERIALIZE_BEFORE) {
80 commit_log_print_insn(p->get_state(), pc, fetch.insn);
81 p->update_histogram(pc);
82 }
83 return npc;
84 }
85
86 bool processor_t::slow_path()
87 {
88 return debug || state.single_step != state.STEP_NONE || state.dcsr.cause;
89 }
90
91 // fetch/decode/execute loop
92 void processor_t::step(size_t n)
93 {
94 if (state.dcsr.cause == DCSR_CAUSE_NONE) {
95 if (halt_request) {
96 enter_debug_mode(DCSR_CAUSE_DEBUGINT);
97 } // !!!The halt bit in DCSR is deprecated.
98 else if (state.dcsr.halt) {
99 enter_debug_mode(DCSR_CAUSE_HALT);
100 }
101 }
102
103 while (n > 0) {
104 size_t instret = 0;
105 reg_t pc = state.pc;
106 #ifdef SPIKE_SIMPLEV
107 sv_mmu_t* _mmu = mmu;
108 #else
109 mmu_t* _mmu = mmu;
110 #endif
111
112 #define advance_pc() \
113 if (unlikely(invalid_pc(pc))) { \
114 switch (pc) { \
115 case PC_SERIALIZE_BEFORE: state.serialized = true; break; \
116 case PC_SERIALIZE_AFTER: ++instret; break; \
117 case PC_SERIALIZE_WFI: n = ++instret; break; \
118 default: abort(); \
119 } \
120 pc = state.pc; \
121 break; \
122 } else { \
123 state.pc = pc; \
124 instret++; \
125 }
126
127 try
128 {
129 take_pending_interrupt();
130
131 if (unlikely(slow_path()))
132 {
133 while (instret < n)
134 {
135 if (unlikely(!state.serialized && state.single_step == state.STEP_STEPPED)) {
136 state.single_step = state.STEP_NONE;
137 if (state.dcsr.cause == DCSR_CAUSE_NONE) {
138 enter_debug_mode(DCSR_CAUSE_STEP);
139 // enter_debug_mode changed state.pc, so we can't just continue.
140 break;
141 }
142 }
143
144 if (unlikely(state.single_step == state.STEP_STEPPING)) {
145 state.single_step = state.STEP_STEPPED;
146 }
147
148 insn_fetch_t fetch = mmu->load_insn(pc);
149 if (debug && !state.serialized)
150 disasm(fetch.insn);
151 pc = execute_insn(this, pc, fetch);
152
153 advance_pc();
154
155 if (unlikely(state.pc >= DEBUG_ROM_ENTRY &&
156 state.pc < DEBUG_END)) {
157 // We're waiting for the debugger to tell us something.
158 return;
159 }
160
161 }
162 }
163 else while (instret < n)
164 {
165 // This code uses a modified Duff's Device to improve the performance
166 // of executing instructions. While typical Duff's Devices are used
167 // for software pipelining, the switch statement below primarily
168 // benefits from separate call points for the fetch.func function call
169 // found in each execute_insn. This function call is an indirect jump
170 // that depends on the current instruction. By having an indirect jump
171 // dedicated for each icache entry, you improve the performance of the
172 // host's next address predictor. Each case in the switch statement
173 // allows for the program flow to contine to the next case if it
174 // corresponds to the next instruction in the program and instret is
175 // still less than n.
176 //
177 // According to Andrew Waterman's recollection, this optimization
178 // resulted in approximately a 2x performance increase.
179
180 // This figures out where to jump to in the switch statement
181 size_t idx = _mmu->icache_index(pc);
182
183 // This gets the cached decoded instruction from the MMU. If the MMU
184 // does not have the current pc cached, it will refill the MMU and
185 // return the correct entry. ic_entry->data.func is the C++ function
186 // corresponding to the instruction.
187 auto ic_entry = _mmu->access_icache(pc);
188
189 // This macro is included in "icache.h" included within the switch
190 // statement below. The indirect jump corresponding to the instruction
191 // is located within the execute_insn() function call.
192 #define ICACHE_ACCESS(i) { \
193 insn_fetch_t fetch = ic_entry->data; \
194 pc = execute_insn(this, pc, fetch); \
195 ic_entry = ic_entry->next; \
196 if (i == mmu_t::ICACHE_ENTRIES-1) break; \
197 if (unlikely(ic_entry->tag != pc)) break; \
198 if (unlikely(instret+1 == n)) break; \
199 instret++; \
200 state.pc = pc; \
201 }
202
203 // This switch statement implements the modified Duff's device as
204 // explained above.
205 switch (idx) {
206 // "icache.h" is generated by the gen_icache script
207 #include "icache.h"
208 }
209
210 advance_pc();
211 }
212 }
213 catch(trap_t& t)
214 {
215 take_trap(t, pc);
216 n = instret;
217
218 if (unlikely(state.single_step == state.STEP_STEPPED)) {
219 state.single_step = state.STEP_NONE;
220 enter_debug_mode(DCSR_CAUSE_STEP);
221 }
222 }
223 catch (trigger_matched_t& t)
224 {
225 if (mmu->matched_trigger) {
226 // This exception came from the MMU. That means the instruction hasn't
227 // fully executed yet. We start it again, but this time it won't throw
228 // an exception because matched_trigger is already set. (All memory
229 // instructions are idempotent so restarting is safe.)
230
231 insn_fetch_t fetch = mmu->load_insn(pc);
232 pc = execute_insn(this, pc, fetch);
233 advance_pc();
234
235 delete mmu->matched_trigger;
236 mmu->matched_trigger = NULL;
237 }
238 switch (state.mcontrol[t.index].action) {
239 case ACTION_DEBUG_MODE:
240 enter_debug_mode(DCSR_CAUSE_HWBP);
241 break;
242 case ACTION_DEBUG_EXCEPTION: {
243 mem_trap_t trap(CAUSE_BREAKPOINT, t.address);
244 take_trap(trap, pc);
245 break;
246 }
247 default:
248 abort();
249 }
250 }
251
252 state.minstret += instret;
253 n -= instret;
254 }
255 }