1 // See LICENSE for license details.
9 static void commit_log_stash_privilege(state_t
* state
)
11 #ifdef RISCV_ENABLE_COMMITLOG
12 state
->last_inst_priv
= state
->prv
;
16 static void commit_log_print_insn(state_t
* state
, reg_t pc
, insn_t insn
)
18 #ifdef RISCV_ENABLE_COMMITLOG
19 int32_t priv
= state
->last_inst_priv
;
20 uint64_t mask
= (insn
.length() == 8 ? uint64_t(0) : (uint64_t(1) << (insn
.length() * 8))) - 1;
21 if (state
->log_reg_write
.addr
) {
22 fprintf(stderr
, "%1d 0x%016" PRIx64
" (0x%08" PRIx64
") %c%2" PRIu64
" 0x%016" PRIx64
"\n",
26 state
->log_reg_write
.addr
& 1 ? 'f' : 'x',
27 state
->log_reg_write
.addr
>> 1,
28 state
->log_reg_write
.data
);
30 fprintf(stderr
, "%1d 0x%016" PRIx64
" (0x%08" PRIx64
")\n", priv
, pc
, insn
.bits() & mask
);
32 state
->log_reg_write
.addr
= 0;
36 inline void processor_t::update_histogram(reg_t pc
)
38 #ifdef RISCV_ENABLE_HISTOGRAM
43 // This is expected to be inlined by the compiler so each use of execute_insn
44 // includes a duplicated body of the function to get separate fetch.func
46 static reg_t
execute_insn(processor_t
* p
, reg_t pc
, insn_fetch_t fetch
)
48 commit_log_stash_privilege(p
->get_state());
49 reg_t npc
= fetch
.func(p
, fetch
.insn
, pc
);
50 if (!invalid_pc(npc
)) {
51 commit_log_print_insn(p
->get_state(), pc
, fetch
.insn
);
52 p
->update_histogram(pc
);
57 bool processor_t::slow_path()
59 return debug
|| state
.single_step
!= state
.STEP_NONE
|| state
.dcsr
.cause
;
62 // fetch/decode/execute loop
63 void processor_t::step(size_t n
)
65 if (state
.dcsr
.cause
== DCSR_CAUSE_NONE
) {
67 enter_debug_mode(DCSR_CAUSE_DEBUGINT
);
68 } else if (state
.dcsr
.halt
) {
69 enter_debug_mode(DCSR_CAUSE_HALT
);
78 #define advance_pc() \
79 if (unlikely(invalid_pc(pc))) { \
81 case PC_SERIALIZE_BEFORE: state.serialized = true; break; \
82 case PC_SERIALIZE_AFTER: n = ++instret; break; \
94 take_pending_interrupt();
96 if (unlikely(slow_path()))
100 if (unlikely(state
.single_step
== state
.STEP_STEPPING
)) {
101 state
.single_step
= state
.STEP_STEPPED
;
104 insn_fetch_t fetch
= mmu
->load_insn(pc
);
105 if (debug
&& !state
.serialized
)
107 pc
= execute_insn(this, pc
, fetch
);
108 bool serialize_before
= (pc
== PC_SERIALIZE_BEFORE
);
112 if (unlikely(state
.single_step
== state
.STEP_STEPPED
) && !serialize_before
) {
113 state
.single_step
= state
.STEP_NONE
;
114 enter_debug_mode(DCSR_CAUSE_STEP
);
115 // enter_debug_mode changed state.pc, so we can't just continue.
119 if (unlikely(state
.pc
>= DEBUG_ROM_ENTRY
&&
120 state
.pc
< DEBUG_ROM_ENTRY
+ DEBUG_ROM_ENTRY_SIZE
)) {
121 // We're spinning waiting for the debugger to tell us something.
122 // Let's go talk to the debugger.
127 else while (instret
< n
)
129 // This code uses a modified Duff's Device to improve the performance
130 // of executing instructions. While typical Duff's Devices are used
131 // for software pipelining, the switch statement below primarily
132 // benefits from separate call points for the fetch.func function call
133 // found in each execute_insn. This function call is an indirect jump
134 // that depends on the current instruction. By having an indirect jump
135 // dedicated for each icache entry, you improve the performance of the
136 // host's next address predictor. Each case in the switch statement
137 // allows for the program flow to contine to the next case if it
138 // corresponds to the next instruction in the program and instret is
139 // still less than n.
141 // According to Andrew Waterman's recollection, this optimization
142 // resulted in approximately a 2x performance increase.
144 // If there is support for compressed instructions, the mmu and the
145 // switch statement get more complicated. Each branch target is stored
146 // in the index corresponding to mmu->icache_index(), but consecutive
147 // non-branching instructions are stored in consecutive indices even if
148 // mmu->icache_index() specifies a different index (which is the case
149 // for 32-bit instructions in the presence of compressed instructions).
151 // This figures out where to jump to in the switch statement
152 size_t idx
= _mmu
->icache_index(pc
);
154 // This gets the cached decoded instruction form the MMU. If the MMU
155 // does not have the current pc cached, it will refill the MMU and
156 // return the correct entry. ic_entry->data.func is the C++ function
157 // corresponding to the instruction.
158 auto ic_entry
= _mmu
->access_icache(pc
);
160 // This macro is included in "icache.h" included within the switch
161 // statement below. The indirect jump corresponding to the instruction
162 // is located within the execute_insn() function call.
163 #define ICACHE_ACCESS(i) { \
164 insn_fetch_t fetch = ic_entry->data; \
166 pc = execute_insn(this, pc, fetch); \
167 if (i == mmu_t::ICACHE_ENTRIES-1) break; \
168 if (unlikely(ic_entry->tag != pc)) goto miss; \
169 if (unlikely(instret+1 == n)) break; \
174 // This switch statement implements the modified Duff's device as
177 // "icache.h" is generated by the gen_icache script
186 // refill I$ if it looks like there wasn't a taken branch
187 if (pc
> (ic_entry
-1)->tag
&& pc
<= (ic_entry
-1)->tag
+ MAX_INSN_LENGTH
)
188 _mmu
->refill_icache(pc
, ic_entry
);
196 if (unlikely(state
.single_step
== state
.STEP_STEPPED
)) {
197 state
.single_step
= state
.STEP_NONE
;
198 enter_debug_mode(DCSR_CAUSE_STEP
);
201 catch (trigger_matched_t
& t
)
203 if (mmu
->matched_trigger
) {
204 // This exception came from the MMU. That means the instruction hasn't
205 // fully executed yet. We start it again, but this time it won't throw
206 // an exception because matched_trigger is already set. (All memory
207 // instructions are idempotent so restarting is safe.)
209 insn_fetch_t fetch
= mmu
->load_insn(pc
);
210 pc
= execute_insn(this, pc
, fetch
);
213 delete mmu
->matched_trigger
;
214 mmu
->matched_trigger
= NULL
;
216 switch (state
.mcontrol
[t
.index
].action
) {
217 case ACTION_DEBUG_MODE
:
218 enter_debug_mode(DCSR_CAUSE_HWBP
);
220 case ACTION_DEBUG_EXCEPTION
: {
221 mem_trap_t
trap(CAUSE_BREAKPOINT
, t
.address
);
230 state
.minstret
+= instret
;