Skip to content

Commit 0703b44

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 riscv-software-src#1777.
1 parent 451a7dc commit 0703b44

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
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/triggers.cc

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,32 @@ bool trigger_t::common_match(processor_t * const proc, bool use_prev_prv) const
7474
return false;
7575

7676
if (get_action() == ACTION_DEBUG_EXCEPTION) {
77-
const bool mstatus_mie = state->mstatus->read() & MSTATUS_MIE;
78-
if (prv == PRV_M && !mstatus_mie)
79-
return false;
80-
81-
const bool sstatus_sie = state->sstatus->read() & MSTATUS_SIE;
82-
const bool medeleg_breakpoint = (state->medeleg->read() >> CAUSE_BREAKPOINT) & 1;
83-
if (prv == PRV_S && !v && medeleg_breakpoint && !sstatus_sie)
84-
return false;
85-
86-
const bool vsstatus_sie = state->vsstatus->read() & MSTATUS_SIE;
87-
const bool hedeleg_breakpoint = (state->hedeleg->read() >> CAUSE_BREAKPOINT) & 1;
88-
if (prv == PRV_S && v && medeleg_breakpoint && hedeleg_breakpoint && !vsstatus_sie)
89-
return false;
77+
if (proc->extension_enabled('S')) {
78+
// The hardware prevents triggers with action=0 from matching or firing
79+
// while in M-mode and while MIE in mstatus is 0. If medeleg [3]=1 then it
80+
// prevents triggers with action=0 from matching or firing while in S-mode
81+
// and while SIE in sstatus is 0. If medeleg [3]=1 and hedeleg [3]=1 then
82+
// it prevents triggers with action=0 from matching or firing while in
83+
// VS-mode and while SIE in vstatus is 0.
84+
85+
const bool mstatus_mie = state->mstatus->read() & MSTATUS_MIE;
86+
if (prv == PRV_M && !mstatus_mie)
87+
return false;
88+
89+
const bool sstatus_sie = state->sstatus->read() & MSTATUS_SIE;
90+
const bool medeleg_breakpoint = (state->medeleg->read() >> CAUSE_BREAKPOINT) & 1;
91+
if (prv == PRV_S && !v && medeleg_breakpoint && !sstatus_sie)
92+
return false;
93+
94+
const bool vsstatus_sie = state->vsstatus->read() & MSTATUS_SIE;
95+
const bool hedeleg_breakpoint = (state->hedeleg->read() >> CAUSE_BREAKPOINT) & 1;
96+
if (prv == PRV_S && v && medeleg_breakpoint && hedeleg_breakpoint && !vsstatus_sie)
97+
return false;
98+
} else {
99+
// mte and mpte in tcontrol is implemented. medeleg [3] is hard-wired to 0.
100+
if (prv == PRV_M && !(tcontrol_value(state) & CSR_TCONTROL_MTE))
101+
return false;
102+
}
90103
}
91104

92105
return true;

0 commit comments

Comments
 (0)