Skip to content

Commit 967760f

Browse files
clementlegeralistair23
authored andcommitted
target/riscv: Implement Ssdbltrp exception handling
When the Ssdbltrp ISA extension is enabled, if a trap happens in S-mode while SSTATUS.SDT isn't cleared, generate a double trap exception to M-mode. Signed-off-by: Clément Léger <[email protected]> Reviewed-by: Alistair Francis <[email protected]> Message-ID: <[email protected]> Signed-off-by: Alistair Francis <[email protected]>
1 parent 72d71d8 commit 967760f

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

target/riscv/cpu.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ static const char * const riscv_excp_names[] = {
303303
"load_page_fault",
304304
"reserved",
305305
"store_page_fault",
306-
"reserved",
306+
"double_trap",
307307
"reserved",
308308
"reserved",
309309
"reserved",

target/riscv/cpu_bits.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,7 @@ typedef enum RISCVException {
701701
RISCV_EXCP_INST_PAGE_FAULT = 0xc, /* since: priv-1.10.0 */
702702
RISCV_EXCP_LOAD_PAGE_FAULT = 0xd, /* since: priv-1.10.0 */
703703
RISCV_EXCP_STORE_PAGE_FAULT = 0xf, /* since: priv-1.10.0 */
704+
RISCV_EXCP_DOUBLE_TRAP = 0x10,
704705
RISCV_EXCP_SW_CHECK = 0x12, /* since: priv-1.13.0 */
705706
RISCV_EXCP_HW_ERR = 0x13, /* since: priv-1.13.0 */
706707
RISCV_EXCP_INST_GUEST_PAGE_FAULT = 0x14,

target/riscv/cpu_helper.c

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,6 +1951,7 @@ void riscv_cpu_do_interrupt(CPUState *cs)
19511951
bool virt = env->virt_enabled;
19521952
bool write_gva = false;
19531953
bool always_storeamo = (env->excp_uw2 & RISCV_UW2_ALWAYS_STORE_AMO);
1954+
bool vsmode_exc;
19541955
uint64_t s;
19551956
int mode;
19561957

@@ -1965,6 +1966,8 @@ void riscv_cpu_do_interrupt(CPUState *cs)
19651966
!(env->mip & (1ULL << cause));
19661967
bool vs_injected = env->hvip & (1ULL << cause) & env->hvien &&
19671968
!(env->mip & (1ULL << cause));
1969+
bool smode_double_trap = false;
1970+
uint64_t hdeleg = async ? env->hideleg : env->hedeleg;
19681971
target_ulong tval = 0;
19691972
target_ulong tinst = 0;
19701973
target_ulong htval = 0;
@@ -2088,6 +2091,30 @@ void riscv_cpu_do_interrupt(CPUState *cs)
20882091
mode = env->priv <= PRV_S && cause < 64 &&
20892092
(((deleg >> cause) & 1) || s_injected || vs_injected) ? PRV_S : PRV_M;
20902093

2094+
vsmode_exc = env->virt_enabled && (((hdeleg >> cause) & 1) || vs_injected);
2095+
/*
2096+
* Check double trap condition only if already in S-mode and targeting
2097+
* S-mode
2098+
*/
2099+
if (cpu->cfg.ext_ssdbltrp && env->priv == PRV_S && mode == PRV_S) {
2100+
bool dte = (env->menvcfg & MENVCFG_DTE) != 0;
2101+
bool sdt = (env->mstatus & MSTATUS_SDT) != 0;
2102+
/* In VS or HS */
2103+
if (riscv_has_ext(env, RVH)) {
2104+
if (vsmode_exc) {
2105+
/* VS -> VS, use henvcfg instead of menvcfg*/
2106+
dte = (env->henvcfg & HENVCFG_DTE) != 0;
2107+
} else if (env->virt_enabled) {
2108+
/* VS -> HS, use mstatus_hs */
2109+
sdt = (env->mstatus_hs & MSTATUS_SDT) != 0;
2110+
}
2111+
}
2112+
smode_double_trap = dte && sdt;
2113+
if (smode_double_trap) {
2114+
mode = PRV_M;
2115+
}
2116+
}
2117+
20912118
if (mode == PRV_S) {
20922119
/* handle the trap in S-mode */
20932120
/* save elp status */
@@ -2096,10 +2123,7 @@ void riscv_cpu_do_interrupt(CPUState *cs)
20962123
}
20972124

20982125
if (riscv_has_ext(env, RVH)) {
2099-
uint64_t hdeleg = async ? env->hideleg : env->hedeleg;
2100-
2101-
if (env->virt_enabled &&
2102-
(((hdeleg >> cause) & 1) || vs_injected)) {
2126+
if (vsmode_exc) {
21032127
/* Trap to VS mode */
21042128
/*
21052129
* See if we need to adjust cause. Yes if its VS mode interrupt
@@ -2132,6 +2156,9 @@ void riscv_cpu_do_interrupt(CPUState *cs)
21322156
s = set_field(s, MSTATUS_SPIE, get_field(s, MSTATUS_SIE));
21332157
s = set_field(s, MSTATUS_SPP, env->priv);
21342158
s = set_field(s, MSTATUS_SIE, 0);
2159+
if (riscv_env_smode_dbltrp_enabled(env, virt)) {
2160+
s = set_field(s, MSTATUS_SDT, 1);
2161+
}
21352162
env->mstatus = s;
21362163
sxlen = 16 << riscv_cpu_sxl(env);
21372164
env->scause = cause | ((target_ulong)async << (sxlen - 1));
@@ -2184,9 +2211,14 @@ void riscv_cpu_do_interrupt(CPUState *cs)
21842211
s = set_field(s, MSTATUS_MIE, 0);
21852212
env->mstatus = s;
21862213
env->mcause = cause | ((target_ulong)async << (mxlen - 1));
2214+
if (smode_double_trap) {
2215+
env->mtval2 = env->mcause;
2216+
env->mcause = RISCV_EXCP_DOUBLE_TRAP;
2217+
} else {
2218+
env->mtval2 = mtval2;
2219+
}
21872220
env->mepc = env->pc;
21882221
env->mtval = tval;
2189-
env->mtval2 = mtval2;
21902222
env->mtinst = tinst;
21912223

21922224
/*

0 commit comments

Comments
 (0)