Skip to content

Commit cfcbffd

Browse files
committed
Only implement one solution for native triggers.
When S-mode is present, use option 1 (disable triggers in M-mode unless MIE is set) from the Debug Spec. When S-mode is not present, use option 2 (implement mte and mpte bits in tcontrol). See discussion in #1777.
1 parent cb78f09 commit cfcbffd

File tree

5 files changed

+39
-19
lines changed

5 files changed

+39
-19
lines changed

riscv/csr_init.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,14 @@ void state_t::csr_init(processor_t* const proc, reg_t max_isa)
205205
add_csr(CSR_TDATA2, tdata2 = std::make_shared<tdata2_csr_t>(proc, CSR_TDATA2));
206206
add_csr(CSR_TDATA3, std::make_shared<tdata3_csr_t>(proc, CSR_TDATA3));
207207
add_csr(CSR_TINFO, std::make_shared<tinfo_csr_t>(proc, CSR_TINFO));
208-
add_csr(CSR_TCONTROL, tcontrol = std::make_shared<masked_csr_t>(proc, CSR_TCONTROL, CSR_TCONTROL_MPTE | CSR_TCONTROL_MTE, 0));
208+
if (!proc->extension_enabled_const('S')) {
209+
add_csr(CSR_TCONTROL, tcontrol = std::make_shared<masked_csr_t>(proc, CSR_TCONTROL, CSR_TCONTROL_MPTE | CSR_TCONTROL_MTE, 0));
210+
}
209211
} else {
210212
add_csr(CSR_TDATA1, std::make_shared<const_csr_t>(proc, CSR_TDATA1, 0));
211213
add_csr(CSR_TDATA2, tdata2 = std::make_shared<const_csr_t>(proc, CSR_TDATA2, 0));
212214
add_csr(CSR_TDATA3, std::make_shared<const_csr_t>(proc, CSR_TDATA3, 0));
213215
add_csr(CSR_TINFO, std::make_shared<const_csr_t>(proc, CSR_TINFO, 0));
214-
add_csr(CSR_TCONTROL, tcontrol = std::make_shared<const_csr_t>(proc, CSR_TCONTROL, 0));
215216
}
216217
unsigned scontext_length = (xlen == 32 ? 16 : 32); // debug spec suggests 16-bit for RV32 and 32-bit for RV64
217218
add_supervisor_csr(CSR_SCONTEXT, scontext = std::make_shared<masked_csr_t>(proc, CSR_SCONTEXT, (reg_t(1) << scontext_length) - 1, 0));

riscv/insns/mret.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ if (prev_virt && prev_prv == PRV_U)
2020
STATE.vsstatus->write(STATE.vsstatus->read() & ~SSTATUS_SDT);
2121
STATE.mstatus->write(s);
2222
if (STATE.mstatush) STATE.mstatush->write(s >> 32); // log mstatush change
23-
STATE.tcontrol->write((STATE.tcontrol->read() & CSR_TCONTROL_MPTE) ? (CSR_TCONTROL_MPTE | CSR_TCONTROL_MTE) : 0);
23+
if (STATE.tcontrol) STATE.tcontrol->write((STATE.tcontrol->read() & CSR_TCONTROL_MPTE) ? (CSR_TCONTROL_MPTE | CSR_TCONTROL_MTE) : 0);
2424
p->set_privilege(prev_prv, prev_virt);

riscv/processor.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ void processor_t::take_trap(trap_t& t, reg_t epc)
535535
state.elp = elp_t::NO_LP_EXPECTED;
536536
state.mstatus->write(s);
537537
if (state.mstatush) state.mstatush->write(s >> 32); // log mstatush change
538-
state.tcontrol->write((state.tcontrol->read() & CSR_TCONTROL_MTE) ? CSR_TCONTROL_MPTE : 0);
538+
if (state.tcontrol) state.tcontrol->write((state.tcontrol->read() & CSR_TCONTROL_MTE) ? CSR_TCONTROL_MPTE : 0);
539539
set_privilege(PRV_M, false);
540540
}
541541
}

riscv/triggers.cc

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,18 @@ void trigger_t::tdata3_write(processor_t * const proc, const reg_t val) noexcept
5555
sselect = (sselect_t)((proc->extension_enabled_const('S') && get_field(val, CSR_TEXTRA_SSELECT(xlen)) <= SSELECT_MAXVAL) ? get_field(val, CSR_TEXTRA_SSELECT(xlen)) : SSELECT_IGNORE);
5656
}
5757

58+
static reg_t tcontrol_value(const state_t * state) {
59+
if (state->tcontrol)
60+
return state->tcontrol->read();
61+
else
62+
return 0;
63+
}
64+
5865
bool trigger_t::common_match(processor_t * const proc, bool use_prev_prv) const noexcept {
5966
auto state = proc->get_state();
6067
auto prv = use_prev_prv ? state->prev_prv : state->prv;
6168
auto v = use_prev_prv ? state->prev_v : state->v;
62-
auto m_enabled = get_action() != 0 || (state->tcontrol->read() & CSR_TCONTROL_MTE);
69+
auto m_enabled = get_action() != 0 || (tcontrol_value(state) & CSR_TCONTROL_MTE);
6370
return (prv < PRV_M || m_enabled) && mode_match(prv, v) && textra_match(proc);
6471
}
6572

@@ -110,17 +117,29 @@ bool trigger_t::textra_match(processor_t * const proc) const noexcept
110117
return true;
111118
}
112119

113-
bool trigger_t::allow_action(const state_t * const state) const
120+
bool trigger_t::allow_action(processor_t * const proc) const
114121
{
122+
const state_t *state = proc->get_state();
115123
if (get_action() == ACTION_DEBUG_EXCEPTION) {
116-
const bool mstatus_mie = state->mstatus->read() & MSTATUS_MIE;
117-
const bool sstatus_sie = state->sstatus->read() & MSTATUS_SIE;
118-
const bool vsstatus_sie = state->vsstatus->read() & MSTATUS_SIE;
119-
const bool medeleg_breakpoint = (state->medeleg->read() >> CAUSE_BREAKPOINT) & 1;
120-
const bool hedeleg_breakpoint = (state->hedeleg->read() >> CAUSE_BREAKPOINT) & 1;
121-
return (state->prv != PRV_M || mstatus_mie) &&
122-
(state->prv != PRV_S || state->v || !medeleg_breakpoint || sstatus_sie) &&
123-
(state->prv != PRV_S || !state->v || !medeleg_breakpoint || !hedeleg_breakpoint || vsstatus_sie);
124+
if (proc->extension_enabled('S')) {
125+
// The hardware prevents triggers with action=0 from matching or firing
126+
// while in M-mode and while MIE in mstatus is 0. If medeleg [3]=1 then it
127+
// prevents triggers with action=0 from matching or firing while in S-mode
128+
// and while SIE in sstatus is 0. If medeleg [3]=1 and hedeleg [3]=1 then
129+
// it prevents triggers with action=0 from matching or firing while in
130+
// VS-mode and while SIE in vstatus is 0.
131+
const bool mstatus_mie = state->mstatus->read() & MSTATUS_MIE;
132+
const bool sstatus_sie = state->sstatus->read() & MSTATUS_SIE;
133+
const bool vsstatus_sie = state->vsstatus->read() & MSTATUS_SIE;
134+
const bool medeleg_breakpoint = (state->medeleg->read() >> CAUSE_BREAKPOINT) & 1;
135+
const bool hedeleg_breakpoint = (state->hedeleg->read() >> CAUSE_BREAKPOINT) & 1;
136+
return (state->prv != PRV_M || mstatus_mie) &&
137+
(state->prv != PRV_S || state->v || !medeleg_breakpoint || sstatus_sie) &&
138+
(state->prv != PRV_S || !state->v || !medeleg_breakpoint || !hedeleg_breakpoint || vsstatus_sie);
139+
} else {
140+
// mte and mpte in tcontrol is implemented. medeleg [3] is hard-wired to 0.
141+
return (state->prv != PRV_M) || (tcontrol_value(state) & CSR_TCONTROL_MTE);
142+
}
124143
}
125144
return true;
126145
}
@@ -235,7 +254,7 @@ std::optional<match_result_t> mcontrol_common_t::detect_memory_access_match(proc
235254
value &= 0xffffffff;
236255
}
237256

238-
if (simple_match(xlen, value) && allow_action(proc->get_state())) {
257+
if (simple_match(xlen, value) && allow_action(proc)) {
239258
/* This is OK because this function is only called if the trigger was not
240259
* inhibited by the previous trigger in the chain. */
241260
set_hit(timing ? HIT_IMMEDIATELY_AFTER : HIT_BEFORE);
@@ -324,7 +343,7 @@ void mcontrol6_t::tdata1_write(processor_t * const proc, const reg_t val, const
324343

325344
std::optional<match_result_t> icount_t::detect_icount_fire(processor_t * const proc) noexcept
326345
{
327-
if (!common_match(proc) || !allow_action(proc->get_state()))
346+
if (!common_match(proc) || !allow_action(proc))
328347
return std::nullopt;
329348

330349
std::optional<match_result_t> ret = std::nullopt;
@@ -339,7 +358,7 @@ std::optional<match_result_t> icount_t::detect_icount_fire(processor_t * const p
339358

340359
void icount_t::detect_icount_decrement(processor_t * const proc) noexcept
341360
{
342-
if (!common_match(proc) || !allow_action(proc->get_state()))
361+
if (!common_match(proc) || !allow_action(proc))
343362
return;
344363

345364
if (count >= 1) {
@@ -431,7 +450,7 @@ std::optional<match_result_t> trap_common_t::detect_trap_match(processor_t * con
431450
bool interrupt = (t.cause() & ((reg_t)1 << (xlen - 1))) != 0;
432451
reg_t bit = t.cause() & ~((reg_t)1 << (xlen - 1));
433452
assert(bit < xlen);
434-
if (simple_match(interrupt, bit) && allow_action(proc->get_state())) {
453+
if (simple_match(interrupt, bit) && allow_action(proc)) {
435454
hit = true;
436455
return match_result_t(TIMING_AFTER, action);
437456
}

riscv/triggers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class trigger_t {
9999
protected:
100100
static action_t legalize_action(reg_t val, reg_t action_mask, reg_t dmode_mask) noexcept;
101101
bool common_match(processor_t * const proc, bool use_prev_prv = false) const noexcept;
102-
bool allow_action(const state_t * const state) const;
102+
bool allow_action(processor_t * const proc) const;
103103
reg_t tdata2;
104104

105105
bool vs = false;

0 commit comments

Comments
 (0)