target/riscv: Add support for Smdbltrp and Ssdbltrp extensions
v6 cover target/riscv: Add support for Smdbltrp and Ssdbltrp extensions
A double trap typically arises during a sensitive phase in trap handling operations — when an exception or interrupt occurs while the trap handler (the component responsible for managing these events) is in a non-reentrant state. This non-reentrancy usually occurs in the early phase of trap handling, wherein the trap handler has not yet preserved the necessary state to handle and resume from the trap. The occurrence of such event is unlikely but can happen when dealing with hardware errors.
This series adds support for Ssdbltrp and Smdbltrp ratified ISA extensions [1]. It is based on the Smrnmi series [5].
Ssdbltrp can be tested using qemu[2], opensbi (master branch), linux[3] and kvm-unit-tests[4]. Assuming you have a riscv environment available and configured (CROSS_COMPILE), it can be built for riscv64 using the following instructions:
Qemu:
$ git clone https://github.com/rivosinc/qemu.git $ cd qemu $ git switch -C dbltrp_v6 dev/cleger/dbltrp_v6 $ mkdir build && cd build $ ../configure --target-list=riscv64-softmmu $ make
OpenSBI:
$ git clone https://github.com/rivosinc/opensbi.git $ cd opensbi $ make O=build PLATFORM_RISCV_XLEN=64 PLATFORM=generic
Linux:
$ git clone https://github.com/rivosinc/linux.git $ cd linux $ git switch -C dbltrp_v1 dev/cleger/dbltrp_v1 $ export ARCH=riscv $ make O=build defconfig $ ./script/config --file build/.config --enable RISCV_DBLTRP $ make O=build
kvm-unit-tests:
$ git clone https://github.com/clementleger/kvm-unit-tests.git $ cd kvm-unit-tests $ git switch -C dbltrp_v1 dev/cleger/dbltrp_v1 $ ./configure --arch=riscv64 --cross-prefix=$CROSS_COMPILE $ make
You will also need kvmtool in your rootfs.
Run with kvm-unit-test test as kernel:
$ qemu-system-riscv64 \
-M virt \
-cpu rv64,ssdbltrp=true,smdbltrp=true \
-nographic \
-serial mon:stdio \
-bios opensbi/build/platform/generic/firmware/fw_jump.bin \
-kernel kvm-unit-tests-dbltrp/riscv/sbi_dbltrp.flat
...
[OpenSBI boot partially elided]
Boot HART ISA Extensions : sscofpmf,sstc,zicntr,zihpm,zicboz,zicbom,sdtrig,svadu,ssdbltrp
...
##########################################################################
# kvm-unit-tests
########################################################################## PASS: sbi: fwft: FWFT extension probing no error
PASS: sbi: fwft: FWFT extension is present
PASS: sbi: fwft: dbltrp: Get double trap enable feature value
PASS: sbi: fwft: dbltrp: Set double trap enable feature value == 0
PASS: sbi: fwft: dbltrp: Get double trap enable feature value == 0
PASS: sbi: fwft: dbltrp: Double trap disabled, trap first time ok
PASS: sbi: fwft: dbltrp: Set double trap enable feature value == 1
PASS: sbi: fwft: dbltrp: Get double trap enable feature value == 1
PASS: sbi: fwft: dbltrp: Trapped twice allowed ok
INFO: sbi: fwft: dbltrp: Should generate a double trap and crash !
sbi_trap_error: hart0: trap0: double trap handler failed (error -10)
sbi_trap_error: hart0: trap0: mcause=0x0000000000000010 mtval=0x0000000000000000
sbi_trap_error: hart0: trap0: mtval2=0x0000000000000003 mtinst=0x0000000000000000
sbi_trap_error: hart0: trap0: mepc=0x00000000802000d8 mstatus=0x8000000a01006900
sbi_trap_error: hart0: trap0: ra=0x00000000802001fc sp=0x0000000080213e70
sbi_trap_error: hart0: trap0: gp=0x0000000000000000 tp=0x0000000080088000
sbi_trap_error: hart0: trap0: s0=0x0000000080213e80 s1=0x0000000000000001
sbi_trap_error: hart0: trap0: a0=0x0000000080213e80 a1=0x0000000080208193
sbi_trap_error: hart0: trap0: a2=0x000000008020dc20 a3=0x000000000000000f
sbi_trap_error: hart0: trap0: a4=0x0000000080210cd8 a5=0x00000000802110d0
sbi_trap_error: hart0: trap0: a6=0x00000000802136e4 a7=0x0000000046574654
sbi_trap_error: hart0: trap0: s2=0x0000000080210cd9 s3=0x0000000000000000
sbi_trap_error: hart0: trap0: s4=0x0000000000000000 s5=0x0000000000000000
sbi_trap_error: hart0: trap0: s6=0x0000000000000000 s7=0x0000000000000001
sbi_trap_error: hart0: trap0: s8=0x0000000000002000 s9=0x0000000080083700
sbi_trap_error: hart0: trap0: s10=0x0000000000000000 s11=0x0000000000000000
sbi_trap_error: hart0: trap0: t0=0x0000000000000000 t1=0x0000000080213ed8
sbi_trap_error: hart0: trap0: t2=0x0000000000001000 t3=0x0000000080213ee0
sbi_trap_error: hart0: trap0: t4=0x0000000000000000 t5=0x000000008020f8d0
sbi_trap_error: hart0: trap0: t6=0x0000000000000000
Run with linux and kvm-unit-test test in kvm (testing VS-mode):
$ qemu-system-riscv64 \
-M virt \
-cpu rv64,ssdbltrp=true,smdbltrp=true \
-nographic \
-serial mon:stdio \
-bios opensbi/build/platform/generic/firmware/fw_jump.bin \
-kernel linux/build/arch/riscv/boot/Image
...
[Linux boot partially elided]
[ 0.735079] riscv-dbltrp: Double trap handling registered
...$ lkvm run -k sbi_dbltrp.flat -m 128 -c 2 ########################################################################## # kvm-unit-tests ##########################################################################
PASS: sbi: fwft: FWFT extension probing no error
PASS: sbi: fwft: FWFT extension is present
PASS: sbi: fwft: dbltrp: Get double trap enable feature value
PASS: sbi: fwft: dbltrp: Set double trap enable feature value == 0
PASS: sbi: fwft: dbltrp: Get double trap enable feature value == 0
PASS: sbi: fwft: dbltrp: Double trap disabled, trap first time ok
PASS: sbi: fwft: dbltrp: Set double trap enable feature value == 1
PASS: sbi: fwft: dbltrp: Get double trap enable feature value == 1
PASS: sbi: fwft: dbltrp: Trapped twice allowed ok
INFO: sbi: fwft: dbltrp: Should generate a double trap and crash !
[ 51.939077] Guest double trap [ 51.939323] kvm [93]: VCPU exit error -95 [ 51.939683] kvm [93]: SEPC=0x802000d8 SSTATUS=0x200004520 HSTATUS=0x200200180 [ 51.939947] kvm [93]: SCAUSE=0x10 STVAL=0x0 HTVAL=0x3 HTINST=0x0 KVM_RUN failed: Operation not supported $
Testing Smbdbltrp can be done using gdb and trigger some trap. For instance, interrupt M-mode firmware at some point, set mstatus.mdt = 1 and corrupt some register to generate a NULL pointer exception.
---
V6:
- Simplify and fix write_henvcfg() masking by assigning the written value to henvcfg and mask the value to be written as well as clearing the upper part of henvcfgh upon writing.
- Rebased on RNMI v9 series.
V5:
- Use 0 instead of false to set MSTATUS_MDT in helper_mnret()
- Added explicit comments about henvcfg write mask being tricky.
- Fixed a invalid menvcfg_mask in write_henvcfgh
V4:
- Remove DTE from sstatus_v1_10_mask variable and add specific if for DTE masking where it's used.
- Use mstatus_hs.sdt field rather than setting DTE to 0 in riscv_do_cpu_interrupt().
- Add a fix for henvcfg value which was incorrectly set after changing menvcfg
- Remove useless ext_ssdbltrp check in riscv_env_smode_dbltrp_enabled().
- Remove useless mstatus clear in write_mstatus().
- Add proper handling of SDT writing to vsstatus.
- Add clearing of vsstatus//mstatus SDT field when DTE is disabled.
- Fix wrong value being written for MDT/MIE in write_mstatush().
- Rebased on Frank Snrnmi v7
V3:
- Fix spec version from 1.12 to 1.13 for Smdbltrp and Ssdbltrp
- Add better comments for dte/sdt computation in riscv_cpu_do_interrupt().
- Move some CSR related changes to the CSRs related commits.
V2:
- Squashed commits that added ext_s{s|m}dbltrp as suggested by Daniel
Clément Léger (9):
target/riscv: fix henvcfg potentially containing stale bits target/riscv: Add Ssdbltrp CSRs handling target/riscv: Implement Ssdbltrp sret, mret and mnret behavior target/riscv: Implement Ssdbltrp exception handling target/riscv: Add Ssdbltrp ISA extension enable switch target/riscv: Add Smdbltrp CSRs handling target/riscv: Implement Smdbltrp sret, mret and mnret behavior target/riscv: Implement Smdbltrp behavior target/riscv: Add Smdbltrp ISA extension enable switch
target/riscv/cpu.c | 9 ++- target/riscv/cpu.h | 1 + target/riscv/cpu_bits.h | 8 +++ target/riscv/cpu_cfg.h | 2 + target/riscv/cpu_helper.c | 115 +++++++++++++++++++++++++++++++------- target/riscv/csr.c | 95 ++++++++++++++++++++++++++----- target/riscv/op_helper.c | 47 +++++++++++++++- 7 files changed, 239 insertions(+), 38 deletions(-)
Signature · 1 line
2.45.2
PATCH v6 1/9 target/riscv: fix henvcfg potentially containing stale bits
With the current implementation, if we had the following scenario:
- Set bit x in menvcfg
- Set bit x in henvcfg
- Clear bit x in menvcfg
then, the internal variable env->henvcfg would still contain bit x due to both a wrong menvcfg mask used in write_henvcfg() as well as a missing update of henvcfg upon menvcfg update.
This can lead to some wrong interpretation of the context. In order to update henvcfg upon menvcfg writing, call write_henvcfg() after writing menvcfg. Clearing henvcfg upon writing the new value is also needed in write_henvcfg() as well as clearing henvcfg upper part when writing it with write_henvcfgh().
--- target/riscv/csr.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
target/riscv/csr.c +8 -2
@@ -2383,6 +2383,8 @@ static RISCVException read_menvcfg(CPURISCVState *env, int csrno,return RISCV_EXCP_NONE ; } +static RISCVException write_henvcfg (CPURISCVState *env,int csrno, +target_ulong val);static RISCVException write_menvcfg (CPURISCVState *env,int csrno,target_ulong val) {
@@ -2403,6 +2405,7 @@ static RISCVException write_menvcfg(CPURISCVState *env, int csrno, } } env->menvcfg = (env->menvcfg & ~mask) | (val & mask); +write_henvcfg (env,CSR_HENVCFG , env->henvcfg);return RISCV_EXCP_NONE ; }
@@ -2414,6 +2417,8 @@ static RISCVException read_menvcfgh(CPURISCVState *env, int csrno,return RISCV_EXCP_NONE ; } +static RISCVException write_henvcfgh (CPURISCVState *env,int csrno, +target_ulong val);static RISCVException write_menvcfgh (CPURISCVState *env,int csrno,target_ulong val) {
@@ -2424,6 +2429,7 @@ static RISCVException write_menvcfgh(CPURISCVState *env, int csrno,uint64_t valh = (uint64_t )val <<32 ; env->menvcfg = (env->menvcfg & ~mask) | (valh & mask); +write_henvcfgh (env,CSR_HENVCFGH , env->henvcfg >>32 );return RISCV_EXCP_NONE ; }
@@ -2513,7 +2519,7 @@ static RISCVException write_henvcfg(CPURISCVState *env, int csrno, } } - env->henvcfg = (env->henvcfg & ~mask) | (val & mask); + env->henvcfg = val & mask;return RISCV_EXCP_NONE ; }
@@ -2546,7 +2552,7 @@ static RISCVException write_henvcfgh(CPURISCVState *env, int csrno,return ret; } - env->henvcfg = (env->henvcfg & ~mask) | (valh & mask); + env->henvcfg = (env->henvcfg &0xFFFFFFFF ) | (valh & mask);return RISCV_EXCP_NONE ; }
Signature · 1 line
2.45.2
Re: [PATCH v6 1/9] target/riscv: fix henvcfg potentially containing stale bits
Quoted 13 lines · Clément Léger
With the current implementation, if we had the following scenario:
- Set bit x in menvcfg
- Set bit x in henvcfg
- Clear bit x in menvcfg
then, the internal variableenv->henvcfgwould still contain bit x due to both a wrong menvcfg mask used inwrite_henvcfg()as well as a missing update of henvcfg upon menvcfg update.
This can lead to some wrong interpretation of the context. In order to update henvcfg upon menvcfg writing, callwrite_henvcfg()after writing menvcfg. Clearing henvcfg upon writing the new value is also needed inwrite_henvcfg()as well as clearing henvcfg upper part when writing it withwrite_henvcfgh().
Quoted 2 lines
Signed-off-by Clément Léger <[email protected]>---
Quoted 59 lines
target/riscv/csr.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)target/riscv/csr.c +8
-2@@ -2383,6 +2383,8 @@ static RISCVException read_menvcfg(CPURISCVState *env, int csrno,return RISCV_EXCP_NONE ; } +static RISCVException write_henvcfg (CPURISCVState *env,int csrno, +target_ulong val);static RISCVException write_menvcfg (CPURISCVState *env,int csrno,target_ulong val) {@@ -2403,6 +2405,7 @@ static RISCVException write_menvcfg(CPURISCVState *env, int csrno, } } env->menvcfg = (env->menvcfg & ~mask) | (val & mask); +write_henvcfg (env,CSR_HENVCFG , env->henvcfg);return RISCV_EXCP_NONE ; }@@ -2414,6 +2417,8 @@ static RISCVException read_menvcfgh(CPURISCVState *env, int csrno,return RISCV_EXCP_NONE ; } +static RISCVException write_henvcfgh (CPURISCVState *env,int csrno, +target_ulong val);static RISCVException write_menvcfgh (CPURISCVState *env,int csrno,target_ulong val) {@@ -2424,6 +2429,7 @@ static RISCVException write_menvcfgh(CPURISCVState *env, int csrno,uint64_t valh = (uint64_t )val <<32 ; env->menvcfg = (env->menvcfg & ~mask) | (valh & mask); +write_henvcfgh (env,CSR_HENVCFGH , env->henvcfg >>32 );return RISCV_EXCP_NONE ; }@@ -2513,7 +2519,7 @@ static RISCVException write_henvcfg(CPURISCVState *env, int csrno, } } - env->henvcfg = (env->henvcfg & ~mask) | (val & mask); + env->henvcfg = val & mask;return RISCV_EXCP_NONE ; }@@ -2546,7 +2552,7 @@ static RISCVException write_henvcfgh(CPURISCVState *env, int csrno,return ret; } - env->henvcfg = (env->henvcfg & ~mask) | (valh & mask); + env->henvcfg = (env->henvcfg &0xFFFFFFFF ) | (valh & mask);return RISCV_EXCP_NONE ; }
Re: [PATCH v6 1/9] target/riscv: fix henvcfg potentially containing stale bits
In reply to Clément LégerQuoted 14 lines · Clément Léger
With the current implementation, if we had the following scenario:
- Set bit x in menvcfg
- Set bit x in henvcfg
- Clear bit x in menvcfg
then, the internal variableenv->henvcfgwould still contain bit x due to both a wrong menvcfg mask used inwrite_henvcfg()as well as a missing update of henvcfg upon menvcfg update.
This can lead to some wrong interpretation of the context. In order to update henvcfg upon menvcfg writing, callwrite_henvcfg()after writing menvcfg. Clearing henvcfg upon writing the new value is also needed inwrite_henvcfg()as well as clearing henvcfg upper part when writing it withwrite_henvcfgh().
Quoted 1 line
Signed-off-by Clément Léger <[email protected]>
Alistair
Quoted 64 lines
--- target/riscv/csr.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)target/riscv/csr.c +8
-3@@ -2383,6 +2383,8 @@ static RISCVException read_menvcfg(CPURISCVState *env, int csrno,return RISCV_EXCP_NONE ; } +static RISCVException write_henvcfg (CPURISCVState *env,int csrno, +target_ulong val);static RISCVException write_menvcfg (CPURISCVState *env,int csrno,target_ulong val) {@@ -2403,6 +2405,7 @@ static RISCVException write_menvcfg(CPURISCVState *env, int csrno, } } env->menvcfg = (env->menvcfg & ~mask) | (val & mask); +write_henvcfg (env,CSR_HENVCFG , env->henvcfg);return RISCV_EXCP_NONE ; }@@ -2414,6 +2417,8 @@ static RISCVException read_menvcfgh(CPURISCVState *env, int csrno,return RISCV_EXCP_NONE ; } +static RISCVException write_henvcfgh (CPURISCVState *env,int csrno, +target_ulong val);static RISCVException write_menvcfgh (CPURISCVState *env,int csrno,target_ulong val) {@@ -2424,6 +2429,7 @@ static RISCVException write_menvcfgh(CPURISCVState *env, int csrno,uint64_t valh = (uint64_t )val <<32 ; env->menvcfg = (env->menvcfg & ~mask) | (valh & mask); +write_henvcfgh (env,CSR_HENVCFGH , env->henvcfg >>32 );return RISCV_EXCP_NONE ; }@@ -2513,7 +2519,7 @@ static RISCVException write_henvcfg(CPURISCVState *env, int csrno, } } - env->henvcfg = (env->henvcfg & ~mask) | (val & mask); + env->henvcfg = val & mask;return RISCV_EXCP_NONE ; }@@ -2546,7 +2552,7 @@ static RISCVException write_henvcfgh(CPURISCVState *env, int csrno,return ret; } - env->henvcfg = (env->henvcfg & ~mask) | (valh & mask); + env->henvcfg = (env->henvcfg &0xFFFFFFFF ) | (valh & mask);return RISCV_EXCP_NONE ; } --2.45.2
PATCH v6 2/9 target/riscv: Add Ssdbltrp CSRs handling
In reply to Clément LégerAdd ext_ssdbltrp in RISCVCPUConfig and implement MSTATUS.SDT, {H|M}ENVCFG.DTE and modify the availability of MTVAL2 based on the presence of the Ssdbltrp ISA extension.
--- target/riscv/cpu.h | 1 + target/riscv/cpu_bits.h | 6 ++++ target/riscv/cpu_cfg.h | 1 + target/riscv/cpu_helper.c | 17 +++++++++ target/riscv/csr.c | 72 ++++++++++++++++++++++++++++++++------- 5 files changed, 84 insertions(+), 13 deletions(-)
target/riscv/cpu.h +1 -0
@@ -569,6 +569,7 @@ void riscv_cpu_set_virt_enabled(CPURISCVState *env, bool enable);int riscv_env_mmu_index (CPURISCVState *env,bool ifetch);bool cpu_get_fcfien (CPURISCVState *env);bool cpu_get_bcfien (CPURISCVState *env); +bool riscv_env_smode_dbltrp_enabled (CPURISCVState *env,bool virt); G_NORETURN void riscv_cpu_do_unaligned_access(CPUState *cs, vaddr addr,MMUAccessType access_type,int mmu_idx,uintptr_t retaddr);
target/riscv/cpu_bits.h +6 -0
@@ -562,6 +562,7 @@#define MSTATUS_TW 0x00200000/* since: priv-1.10 */ #define MSTATUS_TSR 0x00400000/* since: priv-1.10 */ #define MSTATUS_SPELP 0x00800000/* zicfilp */ +#define MSTATUS_SDT 0x01000000#define MSTATUS_MPELP 0x020000000000/* zicfilp */ #define MSTATUS_GVA 0x4000000000ULL#define MSTATUS_MPV 0x8000000000ULL
@@ -594,6 +595,7 @@ typedef enum {#define SSTATUS_SUM 0x00040000/* since: priv-1.10 */ #define SSTATUS_MXR 0x00080000#define SSTATUS_SPELP MSTATUS_SPELP/* zicfilp */ +#define SSTATUS_SDT MSTATUS_SDT#define SSTATUS64_UXL 0x0000000300000000ULL
@@ -791,11 +793,13 @@ typedef enum RISCVException {#define MENVCFG_CBIE (3UL << 4)#define MENVCFG_CBCFE BIT(6)#define MENVCFG_CBZE BIT(7) +#define MENVCFG_DTE (1ULL << 59)#define MENVCFG_ADUE (1ULL << 61)#define MENVCFG_PBMTE (1ULL << 62)#define MENVCFG_STCE (1ULL << 63)/* For RV32 */ +#define MENVCFGH_DTE BIT(27)#define MENVCFGH_ADUE BIT(29)#define MENVCFGH_PBMTE BIT(30)#define MENVCFGH_STCE BIT(31)
@@ -813,11 +817,13 @@ typedef enum RISCVException {#define HENVCFG_CBIE MENVCFG_CBIE#define HENVCFG_CBCFE MENVCFG_CBCFE#define HENVCFG_CBZE MENVCFG_CBZE +#define HENVCFG_DTE MENVCFG_DTE#define HENVCFG_ADUE MENVCFG_ADUE#define HENVCFG_PBMTE MENVCFG_PBMTE#define HENVCFG_STCE MENVCFG_STCE/* For RV32 */ +#define HENVCFGH_DTE MENVCFGH_DTE#define HENVCFGH_ADUE MENVCFGH_ADUE#define HENVCFGH_PBMTE MENVCFGH_PBMTE#define HENVCFGH_STCE MENVCFGH_STCE
target/riscv/cpu_cfg.h +1 -0
@@ -79,6 +79,7 @@ struct RISCVCPUConfig {bool ext_smstateen;bool ext_sstc;bool ext_smcntrpmf; +bool ext_ssdbltrp;bool ext_svadu;bool ext_svinval;bool ext_svnapot;
target/riscv/cpu_helper.c +17 -0
@@ -120,6 +120,19 @@ bool cpu_get_bcfien(CPURISCVState *env) } } +bool riscv_env_smode_dbltrp_enabled (CPURISCVState *env,bool virt) +{ +#ifdef CONFIG_USER_ONLY +return false; +#else +if (virt) { +return (env->henvcfg &HENVCFG_DTE ) !=0 ; + }else { +return (env->menvcfg &MENVCFG_DTE ) !=0 ; + } +#endif +} +void cpu_get_tb_cpu_state (CPURISCVState *env,vaddr *pc,uint64_t *cs_base,uint32_t *pflags) {
@@ -642,6 +655,10 @@ void riscv_cpu_swap_hypervisor_regs(CPURISCVState *env)g_assert (riscv_has_ext (env,RVH )); +if (riscv_env_smode_dbltrp_enabled (env, current_virt)) { + mstatus_mask |=MSTATUS_SDT ; + } +if (current_virt) {/* Current V=1 and we are about to change to V=0 */ env->vsstatus = env->mstatus & mstatus_mask;
target/riscv/csr.c +59 -13
175 lines not on this page · raw message
Signature · 1 line
2.45.2
Re: [PATCH v6 2/9] target/riscv: Add Ssdbltrp CSRs handling
Quoted 6 lines · Clément Léger
Add
ext_ssdbltrpin RISCVCPUConfig and implement MSTATUS.SDT, {H|M}ENVCFG.DTE and modify the availability of MTVAL2 based on the presence of the Ssdbltrp ISA extension.Signed-off-by Clément Léger <[email protected]>---
Quoted 309 lines
target/riscv/cpu.h | 1 + target/riscv/cpu_bits.h | 6 ++++ target/riscv/cpu_cfg.h | 1 + target/riscv/cpu_helper.c | 17 +++++++++ target/riscv/csr.c | 72 ++++++++++++++++++++++++++++++++------- 5 files changed, 84 insertions(+), 13 deletions(-)target/riscv/cpu.h +1
-0@@ -569,6 +569,7 @@ void riscv_cpu_set_virt_enabled(CPURISCVState *env, bool enable);int riscv_env_mmu_index (CPURISCVState *env,bool ifetch);bool cpu_get_fcfien (CPURISCVState *env);bool cpu_get_bcfien (CPURISCVState *env); +bool riscv_env_smode_dbltrp_enabled (CPURISCVState *env,bool virt); G_NORETURN void riscv_cpu_do_unaligned_access(CPUState *cs, vaddr addr,MMUAccessType access_type,int mmu_idx,uintptr_t retaddr);target/riscv/cpu_bits.h +6
-0@@ -562,6 +562,7 @@#define MSTATUS_TW 0x00200000/* since: priv-1.10 */ #define MSTATUS_TSR 0x00400000/* since: priv-1.10 */ #define MSTATUS_SPELP 0x00800000/* zicfilp */ +#define MSTATUS_SDT 0x01000000#define MSTATUS_MPELP 0x020000000000/* zicfilp */ #define MSTATUS_GVA 0x4000000000ULL#define MSTATUS_MPV 0x8000000000ULL@@ -594,6 +595,7 @@ typedef enum {#define SSTATUS_SUM 0x00040000/* since: priv-1.10 */ #define SSTATUS_MXR 0x00080000#define SSTATUS_SPELP MSTATUS_SPELP/* zicfilp */ +#define SSTATUS_SDT MSTATUS_SDT#define SSTATUS64_UXL 0x0000000300000000ULL@@ -791,11 +793,13 @@ typedef enum RISCVException {#define MENVCFG_CBIE (3UL << 4)#define MENVCFG_CBCFE BIT(6)#define MENVCFG_CBZE BIT(7) +#define MENVCFG_DTE (1ULL << 59)#define MENVCFG_ADUE (1ULL << 61)#define MENVCFG_PBMTE (1ULL << 62)#define MENVCFG_STCE (1ULL << 63)/* For RV32 */ +#define MENVCFGH_DTE BIT(27)#define MENVCFGH_ADUE BIT(29)#define MENVCFGH_PBMTE BIT(30)#define MENVCFGH_STCE BIT(31)@@ -813,11 +817,13 @@ typedef enum RISCVException {#define HENVCFG_CBIE MENVCFG_CBIE#define HENVCFG_CBCFE MENVCFG_CBCFE#define HENVCFG_CBZE MENVCFG_CBZE +#define HENVCFG_DTE MENVCFG_DTE#define HENVCFG_ADUE MENVCFG_ADUE#define HENVCFG_PBMTE MENVCFG_PBMTE#define HENVCFG_STCE MENVCFG_STCE/* For RV32 */ +#define HENVCFGH_DTE MENVCFGH_DTE#define HENVCFGH_ADUE MENVCFGH_ADUE#define HENVCFGH_PBMTE MENVCFGH_PBMTE#define HENVCFGH_STCE MENVCFGH_STCEtarget/riscv/cpu_cfg.h +1
-0@@ -79,6 +79,7 @@ struct RISCVCPUConfig {bool ext_smstateen;bool ext_sstc;bool ext_smcntrpmf; +bool ext_ssdbltrp;bool ext_svadu;bool ext_svinval;bool ext_svnapot;target/riscv/cpu_helper.c +17
-0@@ -120,6 +120,19 @@ bool cpu_get_bcfien(CPURISCVState *env) } } +bool riscv_env_smode_dbltrp_enabled (CPURISCVState *env,bool virt) +{ +#ifdef CONFIG_USER_ONLY +return false; +#else +if (virt) { +return (env->henvcfg &HENVCFG_DTE ) !=0 ; + }else { +return (env->menvcfg &MENVCFG_DTE ) !=0 ; + } +#endif +} +void cpu_get_tb_cpu_state (CPURISCVState *env,vaddr *pc,uint64_t *cs_base,uint32_t *pflags) {@@ -642,6 +655,10 @@ void riscv_cpu_swap_hypervisor_regs(CPURISCVState *env)g_assert (riscv_has_ext (env,RVH )); +if (riscv_env_smode_dbltrp_enabled (env, current_virt)) { + mstatus_mask |=MSTATUS_SDT ; + } +if (current_virt) {/* Current V=1 and we are about to change to V=0 */ env->vsstatus = env->mstatus & mstatus_mask;target/riscv/csr.c +59
-13@@ -559,6 +559,15 @@ static RISCVException aia_hmode32(CPURISCVState *env, int csrno)return hmode32 (env, csrno); } +static RISCVException dbltrp_hmode (CPURISCVState *env,int csrno) +{ +if (riscv_cpu_cfg (env)->ext_ssdbltrp) { +return RISCV_EXCP_NONE ; + } + +return hmode (env, csrno); +} +static RISCVException pmp (CPURISCVState *env,int csrno) {if (riscv_cpu_cfg (env)->pmp) {@@ -1633,6 +1642,13 @@ static RISCVException write_mstatus(CPURISCVState *env, int csrno, mask |=MSTATUS_VS ; } +if (riscv_env_smode_dbltrp_enabled (env, env->virt_enabled)) { + mask |=MSTATUS_SDT ; +if ((val &MSTATUS_SDT ) !=0 ) { + val &= ~MSTATUS_SIE ; + } + } +if (xl !=MXL_RV32 || env->debugger) {if (riscv_has_ext (env,RVH )) { mask |=MSTATUS_MPV |MSTATUS_GVA ;@@ -2394,7 +2410,8 @@ static RISCVException write_menvcfg(CPURISCVState *env, int csrno,if (riscv_cpu_mxl (env) ==MXL_RV64 ) { mask |= (cfg->ext_svpbmt ?MENVCFG_PBMTE :0 ) | (cfg->ext_sstc ?MENVCFG_STCE :0 ) | - (cfg->ext_svadu ?MENVCFG_ADUE :0 ); + (cfg->ext_svadu ?MENVCFG_ADUE :0 ) | + (cfg->ext_ssdbltrp ?MENVCFG_DTE :0 );if (env_archcpu (env)->cfg.ext_zicfilp) { mask |=MENVCFG_LPE ;@@ -2403,6 +2420,10 @@ static RISCVException write_menvcfg(CPURISCVState *env, int csrno,if (env_archcpu (env)->cfg.ext_zicfiss) { mask |=MENVCFG_SSE ; } + +if ((val &MENVCFG_DTE ) ==0 ) { + env->mstatus &= ~MSTATUS_SDT ; + } } env->menvcfg = (env->menvcfg & ~mask) | (val & mask);write_henvcfg (env,CSR_HENVCFG , env->henvcfg);@@ -2425,9 +2446,14 @@ static RISCVException write_menvcfgh(CPURISCVState *env, int csrno,const RISCVCPUConfig *cfg =riscv_cpu_cfg (env);uint64_t mask = (cfg->ext_svpbmt ?MENVCFG_PBMTE :0 ) | (cfg->ext_sstc ?MENVCFG_STCE :0 ) | - (cfg->ext_svadu ?MENVCFG_ADUE :0 ); + (cfg->ext_svadu ?MENVCFG_ADUE :0 ) | + (cfg->ext_ssdbltrp ?MENVCFG_DTE :0 );uint64_t valh = (uint64_t )val <<32 ; +if ((valh &MENVCFG_DTE ) ==0 ) { + env->mstatus &= ~MSTATUS_SDT ; + } + env->menvcfg = (env->menvcfg & ~mask) | (valh & mask);write_henvcfgh (env,CSR_HENVCFGH , env->henvcfg >>32 );@@ -2488,9 +2514,10 @@ static RISCVException read_henvcfg(CPURISCVState *env, int csrno,* henvcfg.pbmte is read_only 0 when menvcfg.pbmte = 0 * henvcfg.stce is read_only 0 when menvcfg.stce = 0 * henvcfg.adue is read_only 0 when menvcfg.adue = 0 +* henvcfg.dte is read_only 0 when menvcfg.dte = 0 */ - *val = env->henvcfg & (~(HENVCFG_PBMTE |HENVCFG_STCE |HENVCFG_ADUE ) | - env->menvcfg); + *val = env->henvcfg & (~(HENVCFG_PBMTE |HENVCFG_STCE |HENVCFG_ADUE | +HENVCFG_DTE ) | env->menvcfg);return RISCV_EXCP_NONE ; }@@ -2506,7 +2533,8 @@ static RISCVException write_henvcfg(CPURISCVState *env, int csrno, }if (riscv_cpu_mxl (env) ==MXL_RV64 ) { - mask |= env->menvcfg & (HENVCFG_PBMTE |HENVCFG_STCE |HENVCFG_ADUE ); + mask |= env->menvcfg & (HENVCFG_PBMTE |HENVCFG_STCE |HENVCFG_ADUE | +HENVCFG_DTE );if (env_archcpu (env)->cfg.ext_zicfilp) { mask |=HENVCFG_LPE ;@@ -2520,6 +2548,9 @@ static RISCVException write_henvcfg(CPURISCVState *env, int csrno, } env->henvcfg = val & mask; +if ((env->henvcfg &HENVCFG_DTE ) ==0 ) { + env->vsstatus &= ~MSTATUS_SDT ; + }return RISCV_EXCP_NONE ; }@@ -2534,8 +2565,8 @@ static RISCVException read_henvcfgh(CPURISCVState *env, int csrno,return ret; } - *val = (env->henvcfg & (~(HENVCFG_PBMTE |HENVCFG_STCE |HENVCFG_ADUE ) | - env->menvcfg)) >>32 ; + *val = (env->henvcfg & (~(HENVCFG_PBMTE |HENVCFG_STCE |HENVCFG_ADUE | +HENVCFG_DTE ) | env->menvcfg)) >>32 ;return RISCV_EXCP_NONE ; }@@ -2543,7 +2574,7 @@ static RISCVException write_henvcfgh(CPURISCVState *env, int csrno, target_ulong val) {uint64_t mask = env->menvcfg & (HENVCFG_PBMTE |HENVCFG_STCE | -HENVCFG_ADUE ); +HENVCFG_ADUE |HENVCFG_DTE );uint64_t valh = (uint64_t )val <<32 ;RISCVException ret;@@ -2551,8 +2582,10 @@ static RISCVException write_henvcfgh(CPURISCVState *env, int csrno,if (ret !=RISCV_EXCP_NONE ) {return ret; } - env->henvcfg = (env->henvcfg &0xFFFFFFFF ) | (valh & mask); +if ((env->henvcfg &HENVCFG_DTE ) ==0 ) { + env->vsstatus &= ~MSTATUS_SDT ; + }return RISCV_EXCP_NONE ; }@@ -2980,6 +3013,9 @@ static RISCVException read_sstatus_i128(CPURISCVState *env, int csrno,if (env->xl !=MXL_RV32 || env->debugger) { mask |=SSTATUS64_UXL ; } +if (riscv_cpu_cfg (env)->ext_ssdbltrp) { + mask |=SSTATUS_SDT ; + }if (env_archcpu (env)->cfg.ext_zicfilp) { mask |=SSTATUS_SPELP ;@@ -2996,11 +3032,12 @@ static RISCVException read_sstatus(CPURISCVState *env, int csrno,if (env->xl !=MXL_RV32 || env->debugger) { mask |=SSTATUS64_UXL ; } -if (env_archcpu (env)->cfg.ext_zicfilp) { mask |=SSTATUS_SPELP ; } - +if (riscv_cpu_cfg (env)->ext_ssdbltrp) { + mask |=SSTATUS_SDT ; + }/* TODO: Use SXL not MXL. */ *val =add_status_sd (riscv_cpu_mxl (env), env->mstatus & mask);return RISCV_EXCP_NONE ;@@ -3020,7 +3057,9 @@ static RISCVException write_sstatus(CPURISCVState *env, int csrno,if (env_archcpu (env)->cfg.ext_zicfilp) { mask |=SSTATUS_SPELP ; } - +if (riscv_cpu_cfg (env)->ext_ssdbltrp) { + mask |=SSTATUS_SDT ; + }target_ulong newval = (env->mstatus & ~mask) | (val & mask);return write_mstatus (env,CSR_MSTATUS , newval); }@@ -4126,6 +4165,13 @@ static RISCVException write_vsstatus(CPURISCVState *env, int csrno,if ((val &VSSTATUS64_UXL ) ==0 ) { mask &= ~VSSTATUS64_UXL ; } +if ((env->henvcfg &HENVCFG_DTE )) { +if ((val &SSTATUS_SDT ) !=0 ) { + val &= ~SSTATUS_SIE ; + } + }else { + val &= ~SSTATUS_SDT ; + } env->vsstatus = (env->vsstatus & ~mask) | (uint64_t )val;return RISCV_EXCP_NONE ; }@@ -5336,7 +5382,7 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_VSATP] = { "vsatp", hmode, read_vsatp, write_vsatp, .min_priv_ver = PRIV_VERSION_1_12_0 }, - [CSR_MTVAL2] = { "mtval2", hmode, read_mtval2, write_mtval2, + [CSR_MTVAL2] = { "mtval2", dbltrp_hmode, read_mtval2, write_mtval2, .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MTINST] = { "mtinst", hmode, read_mtinst, write_mtinst, .min_priv_ver = PRIV_VERSION_1_12_0 },
Re: [PATCH v6 2/9] target/riscv: Add Ssdbltrp CSRs handling
In reply to Clément LégerQuoted 6 lines · Clément Léger
Add
ext_ssdbltrpin RISCVCPUConfig and implement MSTATUS.SDT, {H|M}ENVCFG.DTE and modify the availability of MTVAL2 based on the presence of the Ssdbltrp ISA extension.Signed-off-by Clément Léger <[email protected]>
Alistair
Quoted 314 lines
--- target/riscv/cpu.h | 1 + target/riscv/cpu_bits.h | 6 ++++ target/riscv/cpu_cfg.h | 1 + target/riscv/cpu_helper.c | 17 +++++++++ target/riscv/csr.c | 72 ++++++++++++++++++++++++++++++++------- 5 files changed, 84 insertions(+), 13 deletions(-)target/riscv/cpu.h +1
-0@@ -569,6 +569,7 @@ void riscv_cpu_set_virt_enabled(CPURISCVState *env, bool enable);int riscv_env_mmu_index (CPURISCVState *env,bool ifetch);bool cpu_get_fcfien (CPURISCVState *env);bool cpu_get_bcfien (CPURISCVState *env); +bool riscv_env_smode_dbltrp_enabled (CPURISCVState *env,bool virt); G_NORETURN void riscv_cpu_do_unaligned_access(CPUState *cs, vaddr addr,MMUAccessType access_type,int mmu_idx,uintptr_t retaddr);target/riscv/cpu_bits.h +6
-0@@ -562,6 +562,7 @@#define MSTATUS_TW 0x00200000/* since: priv-1.10 */ #define MSTATUS_TSR 0x00400000/* since: priv-1.10 */ #define MSTATUS_SPELP 0x00800000/* zicfilp */ +#define MSTATUS_SDT 0x01000000#define MSTATUS_MPELP 0x020000000000/* zicfilp */ #define MSTATUS_GVA 0x4000000000ULL#define MSTATUS_MPV 0x8000000000ULL@@ -594,6 +595,7 @@ typedef enum {#define SSTATUS_SUM 0x00040000/* since: priv-1.10 */ #define SSTATUS_MXR 0x00080000#define SSTATUS_SPELP MSTATUS_SPELP/* zicfilp */ +#define SSTATUS_SDT MSTATUS_SDT#define SSTATUS64_UXL 0x0000000300000000ULL@@ -791,11 +793,13 @@ typedef enum RISCVException {#define MENVCFG_CBIE (3UL << 4)#define MENVCFG_CBCFE BIT(6)#define MENVCFG_CBZE BIT(7) +#define MENVCFG_DTE (1ULL << 59)#define MENVCFG_ADUE (1ULL << 61)#define MENVCFG_PBMTE (1ULL << 62)#define MENVCFG_STCE (1ULL << 63)/* For RV32 */ +#define MENVCFGH_DTE BIT(27)#define MENVCFGH_ADUE BIT(29)#define MENVCFGH_PBMTE BIT(30)#define MENVCFGH_STCE BIT(31)@@ -813,11 +817,13 @@ typedef enum RISCVException {#define HENVCFG_CBIE MENVCFG_CBIE#define HENVCFG_CBCFE MENVCFG_CBCFE#define HENVCFG_CBZE MENVCFG_CBZE +#define HENVCFG_DTE MENVCFG_DTE#define HENVCFG_ADUE MENVCFG_ADUE#define HENVCFG_PBMTE MENVCFG_PBMTE#define HENVCFG_STCE MENVCFG_STCE/* For RV32 */ +#define HENVCFGH_DTE MENVCFGH_DTE#define HENVCFGH_ADUE MENVCFGH_ADUE#define HENVCFGH_PBMTE MENVCFGH_PBMTE#define HENVCFGH_STCE MENVCFGH_STCEtarget/riscv/cpu_cfg.h +1
-0@@ -79,6 +79,7 @@ struct RISCVCPUConfig {bool ext_smstateen;bool ext_sstc;bool ext_smcntrpmf; +bool ext_ssdbltrp;bool ext_svadu;bool ext_svinval;bool ext_svnapot;target/riscv/cpu_helper.c +17
-0@@ -120,6 +120,19 @@ bool cpu_get_bcfien(CPURISCVState *env) } } +bool riscv_env_smode_dbltrp_enabled (CPURISCVState *env,bool virt) +{ +#ifdef CONFIG_USER_ONLY +return false; +#else +if (virt) { +return (env->henvcfg &HENVCFG_DTE ) !=0 ; + }else { +return (env->menvcfg &MENVCFG_DTE ) !=0 ; + } +#endif +} +void cpu_get_tb_cpu_state (CPURISCVState *env,vaddr *pc,uint64_t *cs_base,uint32_t *pflags) {@@ -642,6 +655,10 @@ void riscv_cpu_swap_hypervisor_regs(CPURISCVState *env)g_assert (riscv_has_ext (env,RVH )); +if (riscv_env_smode_dbltrp_enabled (env, current_virt)) { + mstatus_mask |=MSTATUS_SDT ; + } +if (current_virt) {/* Current V=1 and we are about to change to V=0 */ env->vsstatus = env->mstatus & mstatus_mask;target/riscv/csr.c +59
-14@@ -559,6 +559,15 @@ static RISCVException aia_hmode32(CPURISCVState *env, int csrno)return hmode32 (env, csrno); } +static RISCVException dbltrp_hmode (CPURISCVState *env,int csrno) +{ +if (riscv_cpu_cfg (env)->ext_ssdbltrp) { +return RISCV_EXCP_NONE ; + } + +return hmode (env, csrno); +} +static RISCVException pmp (CPURISCVState *env,int csrno) {if (riscv_cpu_cfg (env)->pmp) {@@ -1633,6 +1642,13 @@ static RISCVException write_mstatus(CPURISCVState *env, int csrno, mask |=MSTATUS_VS ; } +if (riscv_env_smode_dbltrp_enabled (env, env->virt_enabled)) { + mask |=MSTATUS_SDT ; +if ((val &MSTATUS_SDT ) !=0 ) { + val &= ~MSTATUS_SIE ; + } + } +if (xl !=MXL_RV32 || env->debugger) {if (riscv_has_ext (env,RVH )) { mask |=MSTATUS_MPV |MSTATUS_GVA ;@@ -2394,7 +2410,8 @@ static RISCVException write_menvcfg(CPURISCVState *env, int csrno,if (riscv_cpu_mxl (env) ==MXL_RV64 ) { mask |= (cfg->ext_svpbmt ?MENVCFG_PBMTE :0 ) | (cfg->ext_sstc ?MENVCFG_STCE :0 ) | - (cfg->ext_svadu ?MENVCFG_ADUE :0 ); + (cfg->ext_svadu ?MENVCFG_ADUE :0 ) | + (cfg->ext_ssdbltrp ?MENVCFG_DTE :0 );if (env_archcpu (env)->cfg.ext_zicfilp) { mask |=MENVCFG_LPE ;@@ -2403,6 +2420,10 @@ static RISCVException write_menvcfg(CPURISCVState *env, int csrno,if (env_archcpu (env)->cfg.ext_zicfiss) { mask |=MENVCFG_SSE ; } + +if ((val &MENVCFG_DTE ) ==0 ) { + env->mstatus &= ~MSTATUS_SDT ; + } } env->menvcfg = (env->menvcfg & ~mask) | (val & mask);write_henvcfg (env,CSR_HENVCFG , env->henvcfg);@@ -2425,9 +2446,14 @@ static RISCVException write_menvcfgh(CPURISCVState *env, int csrno,const RISCVCPUConfig *cfg =riscv_cpu_cfg (env);uint64_t mask = (cfg->ext_svpbmt ?MENVCFG_PBMTE :0 ) | (cfg->ext_sstc ?MENVCFG_STCE :0 ) | - (cfg->ext_svadu ?MENVCFG_ADUE :0 ); + (cfg->ext_svadu ?MENVCFG_ADUE :0 ) | + (cfg->ext_ssdbltrp ?MENVCFG_DTE :0 );uint64_t valh = (uint64_t )val <<32 ; +if ((valh &MENVCFG_DTE ) ==0 ) { + env->mstatus &= ~MSTATUS_SDT ; + } + env->menvcfg = (env->menvcfg & ~mask) | (valh & mask);write_henvcfgh (env,CSR_HENVCFGH , env->henvcfg >>32 );@@ -2488,9 +2514,10 @@ static RISCVException read_henvcfg(CPURISCVState *env, int csrno,* henvcfg.pbmte is read_only 0 when menvcfg.pbmte = 0 * henvcfg.stce is read_only 0 when menvcfg.stce = 0 * henvcfg.adue is read_only 0 when menvcfg.adue = 0 +* henvcfg.dte is read_only 0 when menvcfg.dte = 0 */ - *val = env->henvcfg & (~(HENVCFG_PBMTE |HENVCFG_STCE |HENVCFG_ADUE ) | - env->menvcfg); + *val = env->henvcfg & (~(HENVCFG_PBMTE |HENVCFG_STCE |HENVCFG_ADUE | +HENVCFG_DTE ) | env->menvcfg);return RISCV_EXCP_NONE ; }@@ -2506,7 +2533,8 @@ static RISCVException write_henvcfg(CPURISCVState *env, int csrno, }if (riscv_cpu_mxl (env) ==MXL_RV64 ) { - mask |= env->menvcfg & (HENVCFG_PBMTE |HENVCFG_STCE |HENVCFG_ADUE ); + mask |= env->menvcfg & (HENVCFG_PBMTE |HENVCFG_STCE |HENVCFG_ADUE | +HENVCFG_DTE );if (env_archcpu (env)->cfg.ext_zicfilp) { mask |=HENVCFG_LPE ;@@ -2520,6 +2548,9 @@ static RISCVException write_henvcfg(CPURISCVState *env, int csrno, } env->henvcfg = val & mask; +if ((env->henvcfg &HENVCFG_DTE ) ==0 ) { + env->vsstatus &= ~MSTATUS_SDT ; + }return RISCV_EXCP_NONE ; }@@ -2534,8 +2565,8 @@ static RISCVException read_henvcfgh(CPURISCVState *env, int csrno,return ret; } - *val = (env->henvcfg & (~(HENVCFG_PBMTE |HENVCFG_STCE |HENVCFG_ADUE ) | - env->menvcfg)) >>32 ; + *val = (env->henvcfg & (~(HENVCFG_PBMTE |HENVCFG_STCE |HENVCFG_ADUE | +HENVCFG_DTE ) | env->menvcfg)) >>32 ;return RISCV_EXCP_NONE ; }@@ -2543,7 +2574,7 @@ static RISCVException write_henvcfgh(CPURISCVState *env, int csrno, target_ulong val) {uint64_t mask = env->menvcfg & (HENVCFG_PBMTE |HENVCFG_STCE | -HENVCFG_ADUE ); +HENVCFG_ADUE |HENVCFG_DTE );uint64_t valh = (uint64_t )val <<32 ;RISCVException ret;@@ -2551,8 +2582,10 @@ static RISCVException write_henvcfgh(CPURISCVState *env, int csrno,if (ret !=RISCV_EXCP_NONE ) {return ret; } - env->henvcfg = (env->henvcfg &0xFFFFFFFF ) | (valh & mask); +if ((env->henvcfg &HENVCFG_DTE ) ==0 ) { + env->vsstatus &= ~MSTATUS_SDT ; + }return RISCV_EXCP_NONE ; }@@ -2980,6 +3013,9 @@ static RISCVException read_sstatus_i128(CPURISCVState *env, int csrno,if (env->xl !=MXL_RV32 || env->debugger) { mask |=SSTATUS64_UXL ; } +if (riscv_cpu_cfg (env)->ext_ssdbltrp) { + mask |=SSTATUS_SDT ; + }if (env_archcpu (env)->cfg.ext_zicfilp) { mask |=SSTATUS_SPELP ;@@ -2996,11 +3032,12 @@ static RISCVException read_sstatus(CPURISCVState *env, int csrno,if (env->xl !=MXL_RV32 || env->debugger) { mask |=SSTATUS64_UXL ; } -if (env_archcpu (env)->cfg.ext_zicfilp) { mask |=SSTATUS_SPELP ; } - +if (riscv_cpu_cfg (env)->ext_ssdbltrp) { + mask |=SSTATUS_SDT ; + }/* TODO: Use SXL not MXL. */ *val =add_status_sd (riscv_cpu_mxl (env), env->mstatus & mask);return RISCV_EXCP_NONE ;@@ -3020,7 +3057,9 @@ static RISCVException write_sstatus(CPURISCVState *env, int csrno,if (env_archcpu (env)->cfg.ext_zicfilp) { mask |=SSTATUS_SPELP ; } - +if (riscv_cpu_cfg (env)->ext_ssdbltrp) { + mask |=SSTATUS_SDT ; + }target_ulong newval = (env->mstatus & ~mask) | (val & mask);return write_mstatus (env,CSR_MSTATUS , newval); }@@ -4126,6 +4165,13 @@ static RISCVException write_vsstatus(CPURISCVState *env, int csrno,if ((val &VSSTATUS64_UXL ) ==0 ) { mask &= ~VSSTATUS64_UXL ; } +if ((env->henvcfg &HENVCFG_DTE )) { +if ((val &SSTATUS_SDT ) !=0 ) { + val &= ~SSTATUS_SIE ; + } + }else { + val &= ~SSTATUS_SDT ; + } env->vsstatus = (env->vsstatus & ~mask) | (uint64_t )val;return RISCV_EXCP_NONE ; }@@ -5336,7 +5382,7 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_VSATP] = { "vsatp", hmode, read_vsatp, write_vsatp, .min_priv_ver = PRIV_VERSION_1_12_0 }, - [CSR_MTVAL2 ] = {"mtval2" , hmode, read_mtval2, write_mtval2, + [CSR_MTVAL2] = { "mtval2", dbltrp_hmode, read_mtval2, write_mtval2, .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MTINST] = { "mtinst", hmode, read_mtinst, write_mtinst, .min_priv_ver = PRIV_VERSION_1_12_0 }, --2.45.2
PATCH v6 3/9 target/riscv: Implement Ssdbltrp sret, mret and mnret behavior
In reply to Clément LégerWhen the Ssdbltrp extension is enabled, SSTATUS.SDT field is cleared when executing sret. When executing mret/mnret, SSTATUS.SDT is cleared when returning to U, VS or VU and VSSTATUS.SDT is cleared when returning to VU from HS.
--- target/riscv/op_helper.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-)
target/riscv/op_helper.c +34 -1
@@ -287,6 +287,18 @@ target_ulong helper_sret(CPURISCVState *env) get_field(mstatus, MSTATUS_SPIE)); mstatus =set_field (mstatus,MSTATUS_SPIE ,1 ); mstatus =set_field (mstatus,MSTATUS_SPP ,PRV_U ); + +if (riscv_cpu_cfg (env)->ext_ssdbltrp) { +if (riscv_has_ext (env,RVH )) { +target_ulong prev_vu =get_field (env->hstatus,HSTATUS_SPV ) && + prev_priv ==PRV_U ; +/* Returning to VU from HS, vsstatus.sdt = 0 */ +if (!env->virt_enabled && prev_vu) { + env->vsstatus =set_field (env->vsstatus,MSTATUS_SDT ,0 ); + } + } + mstatus =set_field (mstatus,MSTATUS_SDT ,0 ); + }if (env->priv_ver >=PRIV_VERSION_1_12_0 ) { mstatus =set_field (mstatus,MSTATUS_MPRV ,0 ); }
@@ -297,7 +309,6 @@ target_ulong helper_sret(CPURISCVState *env)target_ulong hstatus = env->hstatus; prev_virt =get_field (hstatus,HSTATUS_SPV ); - hstatus =set_field (hstatus,HSTATUS_SPV ,0 ); env->hstatus = hstatus;
@@ -337,6 +348,22 @@ static void check_ret_from_m_mode(CPURISCVState *env, target_ulong retpc,riscv_raise_exception (env,RISCV_EXCP_INST_ACCESS_FAULT ,GETPC ()); } } +static target_ulong ssdbltrp_mxret (CPURISCVState *env,target_ulong mstatus, +target_ulong prev_priv, +target_ulong prev_virt) +{ +/* If returning to U, VS or VU, sstatus.sdt = 0 */ +if (prev_priv ==PRV_U || (prev_virt && + (prev_priv ==PRV_S || prev_priv ==PRV_U ))) { + mstatus =set_field (mstatus,MSTATUS_SDT ,0 ); +/* If returning to VU, vsstatus.sdt = 0 */ +if (prev_virt && prev_priv ==PRV_U ) { + env->vsstatus =set_field (env->vsstatus,MSTATUS_SDT ,0 ); + } + } + +return mstatus; +}target_ulong helper_mret (CPURISCVState *env) {
@@ -354,6 +381,9 @@ target_ulong helper_mret(CPURISCVState *env) mstatus =set_field (mstatus,MSTATUS_MPP ,riscv_has_ext (env,RVU ) ?PRV_U :PRV_M ); mstatus =set_field (mstatus,MSTATUS_MPV ,0 ); +if (riscv_cpu_cfg (env)->ext_ssdbltrp) { + mstatus =ssdbltrp_mxret (env, mstatus, prev_priv, prev_virt); + }if ((env->priv_ver >=PRIV_VERSION_1_12_0 ) && (prev_priv !=PRV_M )) { mstatus =set_field (mstatus,MSTATUS_MPRV ,0 ); }
@@ -395,6 +425,9 @@ target_ulong helper_mnret(CPURISCVState *env)if (prev_priv <PRV_M ) { env->mstatus =set_field (env->mstatus,MSTATUS_MPRV , false); } +if (riscv_cpu_cfg (env)->ext_ssdbltrp) { + env->mstatus =ssdbltrp_mxret (env, env->mstatus, prev_priv, prev_virt); + }if (riscv_has_ext (env,RVH ) && prev_virt) {riscv_cpu_swap_hypervisor_regs (env);
Signature · 1 line
2.45.2
PATCH v6 4/9 target/riscv: Implement Ssdbltrp exception handling
In reply to Clément LégerWhen 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.
--- target/riscv/cpu.c | 2 +- target/riscv/cpu_bits.h | 1 + target/riscv/cpu_helper.c | 42 ++++++++++++++++++++++++++++++++++----- 3 files changed, 39 insertions(+), 6 deletions(-)
target/riscv/cpu.c +1 -1
@@ -287,7 +287,7 @@ static const char * const riscv_excp_names[] = {"load_page_fault" ,"reserved" ,"store_page_fault" , -"reserved" , +"double_trap" ,"reserved" ,"reserved" , "reserved",
target/riscv/cpu_bits.h +1 -0
@@ -706,6 +706,7 @@ typedef enum RISCVException {RISCV_EXCP_INST_PAGE_FAULT =0xc ,/* since: priv-1.10.0 */ RISCV_EXCP_LOAD_PAGE_FAULT =0xd ,/* since: priv-1.10.0 */ RISCV_EXCP_STORE_PAGE_FAULT =0xf ,/* since: priv-1.10.0 */ +RISCV_EXCP_DOUBLE_TRAP =0x10 ,RISCV_EXCP_SW_CHECK =0x12 ,/* since: priv-1.13.0 */ RISCV_EXCP_HW_ERR =0x13 ,/* since: priv-1.13.0 */ RISCV_EXCP_INST_GUEST_PAGE_FAULT = 0x14,
target/riscv/cpu_helper.c +37 -5
80 lines not on this page · raw message
Signature · 1 line
2.45.2
PATCH v6 5/9 target/riscv: Add Ssdbltrp ISA extension enable switch
In reply to Clément LégerAdd the switch to enable the Ssdbltrp ISA extension.
--- target/riscv/cpu.c | 2 ++ 1 file changed, 2 insertions(+)
target/riscv/cpu.c +2 -0
@@ -192,6 +192,7 @@ const RISCVIsaExtData isa_edata_arr[] = {ISA_EXT_DATA_ENTRY (ssccptr,PRIV_VERSION_1_11_0 , has_priv_1_11),ISA_EXT_DATA_ENTRY (sscofpmf,PRIV_VERSION_1_12_0 , ext_sscofpmf),ISA_EXT_DATA_ENTRY (sscounterenw,PRIV_VERSION_1_12_0 , has_priv_1_12), +ISA_EXT_DATA_ENTRY (ssdbltrp,PRIV_VERSION_1_13_0 , ext_ssdbltrp),ISA_EXT_DATA_ENTRY (sstc,PRIV_VERSION_1_12_0 , ext_sstc),ISA_EXT_DATA_ENTRY (sstvala,PRIV_VERSION_1_12_0 , has_priv_1_12), ISA_EXT_DATA_ENTRY(sstvecd, PRIV_VERSION_1_12_0, has_priv_1_12),
@@ -1586,6 +1587,7 @@ const RISCVCPUMultiExtConfig riscv_cpu_extensions[] = {MULTI_EXT_CFG_BOOL ("smrnmi" , ext_smrnmi, false),MULTI_EXT_CFG_BOOL ("smstateen" , ext_smstateen, false),MULTI_EXT_CFG_BOOL ("ssaia" , ext_ssaia, false), +MULTI_EXT_CFG_BOOL ("ssdbltrp" , ext_ssdbltrp, false),MULTI_EXT_CFG_BOOL ("svade" , ext_svade, false),MULTI_EXT_CFG_BOOL ("svadu" , ext_svadu, true), MULTI_EXT_CFG_BOOL("svinval", ext_svinval, false),
Signature · 1 line
2.45.2
PATCH v6 6/9 target/riscv: Add Smdbltrp CSRs handling
In reply to Clément LégerAdd ext_smdbltrpin RISCVCPUConfig and implement MSTATUS.MDT behavior. Also set MDT to 1 at reset according to the specification.
--- target/riscv/cpu.c | 3 +++ target/riscv/cpu_bits.h | 1 + target/riscv/cpu_cfg.h | 1 + target/riscv/csr.c | 13 +++++++++++++ 4 files changed, 18 insertions(+)
target/riscv/cpu.c +3 -0
@@ -1027,6 +1027,9 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type) env->mstatus_hs =set_field (env->mstatus_hs,MSTATUS64_UXL , env->misa_mxl); } +if (riscv_cpu_cfg (env)->ext_smdbltrp) { + env->mstatus =set_field (env->mstatus,MSTATUS_MDT ,1 ); + } } env->mcause =0 ; env->miclaim =MIP_SGEIP ;
target/riscv/cpu_bits.h +1 -0
@@ -566,6 +566,7 @@#define MSTATUS_MPELP 0x020000000000/* zicfilp */ #define MSTATUS_GVA 0x4000000000ULL#define MSTATUS_MPV 0x8000000000ULL +#define MSTATUS_MDT 0x40000000000ULL/* Smdbltrp extension */ #define MSTATUS64_UXL 0x0000000300000000ULL#define MSTATUS64_SXL 0x0000000C00000000ULL
target/riscv/cpu_cfg.h +1 -0
@@ -80,6 +80,7 @@ struct RISCVCPUConfig {bool ext_sstc;bool ext_smcntrpmf;bool ext_ssdbltrp; +bool ext_smdbltrp;bool ext_svadu;bool ext_svinval;bool ext_svnapot;
target/riscv/csr.c +13 -0
@@ -1649,6 +1649,13 @@ static RISCVException write_mstatus(CPURISCVState *env, int csrno, } } +if (riscv_cpu_cfg (env)->ext_smdbltrp) { + mask |=MSTATUS_MDT ; +if ((val &MSTATUS_MDT ) !=0 ) { + val &= ~MSTATUS_MIE ; + } + } +if (xl !=MXL_RV32 || env->debugger) {if (riscv_has_ext (env,RVH )) { mask |=MSTATUS_MPV |MSTATUS_GVA ;
@@ -1692,6 +1699,12 @@ static RISCVException write_mstatush(CPURISCVState *env, int csrno,uint64_t valh = (uint64_t )val <<32 ;uint64_t mask =riscv_has_ext (env,RVH ) ?MSTATUS_MPV |MSTATUS_GVA :0 ; +if (riscv_cpu_cfg (env)->ext_smdbltrp) { + mask |=MSTATUS_MDT ; +if ((valh &MSTATUS_MDT ) !=0 ) { + mask |=MSTATUS_MIE ; + } + } env->mstatus = (env->mstatus & ~mask) | (valh & mask);return RISCV_EXCP_NONE ;
Signature · 1 line
2.45.2
PATCH v6 7/9 target/riscv: Implement Smdbltrp sret, mret and mnret behavior
In reply to Clément LégerWhen the Ssdbltrp extension is enabled, SSTATUS.MDT field is cleared when executing sret if executed in M-mode. When executing mret/mnret, SSTATUS.MDT is cleared.
--- target/riscv/op_helper.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
target/riscv/op_helper.c +12 -0
@@ -299,6 +299,9 @@ target_ulong helper_sret(CPURISCVState *env) } mstatus =set_field (mstatus,MSTATUS_SDT ,0 ); } +if (riscv_cpu_cfg (env)->ext_smdbltrp && env->priv >=PRV_M ) { + mstatus =set_field (mstatus,MSTATUS_MDT ,0 ); + }if (env->priv_ver >=PRIV_VERSION_1_12_0 ) { mstatus =set_field (mstatus,MSTATUS_MPRV ,0 ); }
@@ -384,6 +387,9 @@ target_ulong helper_mret(CPURISCVState *env)if (riscv_cpu_cfg (env)->ext_ssdbltrp) { mstatus =ssdbltrp_mxret (env, mstatus, prev_priv, prev_virt); } +if (riscv_cpu_cfg (env)->ext_smdbltrp) { + mstatus =set_field (mstatus,MSTATUS_MDT ,0 ); + }if ((env->priv_ver >=PRIV_VERSION_1_12_0 ) && (prev_priv !=PRV_M )) { mstatus =set_field (mstatus,MSTATUS_MPRV ,0 ); }
@@ -429,6 +435,12 @@ target_ulong helper_mnret(CPURISCVState *env) env->mstatus =ssdbltrp_mxret (env, env->mstatus, prev_priv, prev_virt); } +if (riscv_cpu_cfg (env)->ext_smdbltrp) { +if (prev_priv <PRV_M ) { + env->mstatus =set_field (env->mstatus,MSTATUS_MDT ,0 ); + } + } +if (riscv_has_ext (env,RVH ) && prev_virt) {riscv_cpu_swap_hypervisor_regs (env); }
Signature · 1 line
2.45.2
PATCH v6 8/9 target/riscv: Implement Smdbltrp behavior
In reply to Clément LégerWhen the Smsdbltrp ISA extension is enabled, if a trap happens while MSTATUS.MDT is already set, it will trigger an abort or an NMI is the Smrnmi extension is available.
--- target/riscv/cpu_helper.c | 56 ++++++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 16 deletions(-)
target/riscv/cpu_helper.c +40 -16
@@ -1835,6 +1835,23 @@ static target_ulong promote_load_fault(target_ulong orig_cause)/* if no promotion, return original cause */ return orig_cause; } + +static void riscv_do_nmi (CPURISCVState *env,target_ulong cause,bool virt) +{ + env->mnstatus =set_field (env->mnstatus,MNSTATUS_NMIE , false); + env->mnstatus =set_field (env->mnstatus,MNSTATUS_MNPV , virt); + env->mnstatus =set_field (env->mnstatus,MNSTATUS_MNPP , env->priv); + env->mncause = cause; + env->mnepc = env->pc; + env->pc = env->rnmi_irqvec; + +if (cpu_get_fcfien (env)) { + env->mnstatus =set_field (env->mnstatus,MNSTATUS_MNPELP , env->elp); + } + +riscv_cpu_set_mode (env,PRV_M , false); +} +/* * Handle Traps *
@@ -1874,22 +1891,8 @@ void riscv_cpu_do_interrupt(CPUState *cs)bool nnmi_excep = false;if (cpu->cfg.ext_smrnmi && env->rnmip && async) { - env->mnstatus =set_field (env->mnstatus,MNSTATUS_NMIE , false); - env->mnstatus =set_field (env->mnstatus,MNSTATUS_MNPV , - env->virt_enabled); - env->mnstatus =set_field (env->mnstatus,MNSTATUS_MNPP , - env->priv); - env->mncause = cause | ((target_ulong )1U << (TARGET_LONG_BITS -1 )); - env->mnepc = env->pc; - env->pc = env->rnmi_irqvec; - -if (cpu_get_fcfien (env)) { - env->mnstatus =set_field (env->mnstatus,MNSTATUS_MNPELP , env->elp); - } - -/* Trapping to M mode, virt is disabled */ -riscv_cpu_set_mode (env,PRV_M , false); - +riscv_do_nmi (env, cause | ((target_ulong )1U << (TARGET_LONG_BITS -1 )), + virt);return ; }
@@ -2101,11 +2104,32 @@ void riscv_cpu_do_interrupt(CPUState *cs)/* Trapping to M mode, virt is disabled */ virt = false; } +/* +* If the hart encounters an exception while executing in M-mode, +* with the mnstatus.NMIE bit clear, the program counter is set to +* the RNMI exception trap handler address. +*/ + nnmi_excep = cpu->cfg.ext_smrnmi && + !get_field (env->mnstatus,MNSTATUS_NMIE ) && + !async; s = env->mstatus; s =set_field (s,MSTATUS_MPIE ,get_field (s,MSTATUS_MIE )); s =set_field (s,MSTATUS_MPP , env->priv); s =set_field (s,MSTATUS_MIE ,0 ); +if (cpu->cfg.ext_smdbltrp) { +if (env->mstatus &MSTATUS_MDT ) { +assert (env->priv ==PRV_M ); +if (!cpu->cfg.ext_smrnmi || nnmi_excep) { +cpu_abort (CPU (cpu),"M-mode double trap\n" ); + }else { +riscv_do_nmi (env, cause, false); +return ; + } + } + + s =set_field (s,MSTATUS_MDT ,1 ); + } env->mstatus = s; mxlen =16 <<riscv_cpu_mxl (env); env->mcause = cause | ((target_ulong )async << (mxlen -1 ));
Signature · 1 line
2.45.2
PATCH v6 9/9 target/riscv: Add Smdbltrp ISA extension enable switch
In reply to Clément LégerAdd the switch to enable the Smdbltrp ISA extension.
--- target/riscv/cpu.c | 2 ++ 1 file changed, 2 insertions(+)
target/riscv/cpu.c +2 -0
@@ -185,6 +185,7 @@ const RISCVIsaExtData isa_edata_arr[] = {ISA_EXT_DATA_ENTRY (zhinxmin,PRIV_VERSION_1_12_0 , ext_zhinxmin),ISA_EXT_DATA_ENTRY (smaia,PRIV_VERSION_1_12_0 , ext_smaia),ISA_EXT_DATA_ENTRY (smcntrpmf,PRIV_VERSION_1_12_0 , ext_smcntrpmf), +ISA_EXT_DATA_ENTRY (smdbltrp,PRIV_VERSION_1_13_0 , ext_smdbltrp),ISA_EXT_DATA_ENTRY (smepmp,PRIV_VERSION_1_12_0 , ext_smepmp),ISA_EXT_DATA_ENTRY (smrnmi,PRIV_VERSION_1_12_0 , ext_smrnmi), ISA_EXT_DATA_ENTRY(smstateen, PRIV_VERSION_1_12_0, ext_smstateen),
@@ -1586,6 +1587,7 @@ const RISCVCPUMultiExtConfig riscv_cpu_extensions[] = {MULTI_EXT_CFG_BOOL ("sstc" , ext_sstc, true),MULTI_EXT_CFG_BOOL ("smaia" , ext_smaia, false), +MULTI_EXT_CFG_BOOL ("smdbltrp" , ext_smdbltrp, false),MULTI_EXT_CFG_BOOL ("smepmp" , ext_smepmp, false),MULTI_EXT_CFG_BOOL ("smrnmi" , ext_smrnmi, false), MULTI_EXT_CFG_BOOL("smstateen", ext_smstateen, false),
Signature · 1 line
2.45.2
Quoted 180 lines · Clément Léger
A double trap typically arises during a sensitive phase in trap handling operations — when an exception or interrupt occurs while the trap handler (the component responsible for managing these events) is in a non-reentrant state. This non-reentrancy usually occurs in the early phase of trap handling, wherein the trap handler has not yet preserved the necessary state to handle and resume from the trap. The occurrence of such event is unlikely but can happen when dealing with hardware errors.
This series adds support for Ssdbltrp and Smdbltrp ratified ISA extensions [1]. It is based on the Smrnmi series [5].
Ssdbltrp can be tested using qemu[2], opensbi (master branch), linux[3] and kvm-unit-tests[4]. Assuming you have a riscv environment available and configured (
CROSS_COMPILE), it can be built for riscv64 using the following instructions:Qemu:
$ git clone https://github.com/rivosinc/qemu.git $ cd qemu $ git switch -C dbltrp_v6 dev/cleger/dbltrp_v6 $ mkdir build && cd build $ ../configure --target-list=riscv64-softmmu $ makeOpenSBI:
$ git clone https://github.com/rivosinc/opensbi.git $ cd opensbi $ make O=build PLATFORM_RISCV_XLEN=64 PLATFORM=genericLinux:
$ git clone https://github.com/rivosinc/linux.git $ cd linux $ git switch -C dbltrp_v1 dev/cleger/dbltrp_v1 $ export ARCH=riscv $ make O=build defconfig $ ./script/config --file build/.config --enable RISCV_DBLTRP $ make O=buildkvm-unit-tests:
$ git clone https://github.com/clementleger/kvm-unit-tests.git $ cd kvm-unit-tests $ git switch -C dbltrp_v1 dev/cleger/dbltrp_v1 $ ./configure --arch=riscv64 --cross-prefix=$CROSS_COMPILE $ makeYou will also need kvmtool in your rootfs.
Run with kvm-unit-test test as kernel:
$ qemu-system-riscv64 \ -M virt \ -cpu rv64,ssdbltrp=true,smdbltrp=true \ -nographic \ -serial mon:stdio \ -bios opensbi/build/platform/generic/firmware/fw_jump.bin \ -kernel kvm-unit-tests-dbltrp/riscv/sbi_dbltrp.flat ... [OpenSBI boot partially elided] Boot HART ISA Extensions : sscofpmf,sstc,zicntr,zihpm,zicboz,zicbom,sdtrig,svadu,ssdbltrp ... ########################################################################## # kvm-unit-tests ##########################################################################PASS: sbi: fwft: FWFT extension probing no error
PASS: sbi: fwft: FWFT extension is present
PASS: sbi: fwft: dbltrp: Get double trap enable feature value
PASS: sbi: fwft: dbltrp: Set double trap enable feature value == 0
PASS: sbi: fwft: dbltrp: Get double trap enable feature value == 0
PASS: sbi: fwft: dbltrp: Double trap disabled, trap first time ok
PASS: sbi: fwft: dbltrp: Set double trap enable feature value == 1
PASS: sbi: fwft: dbltrp: Get double trap enable feature value == 1
PASS: sbi: fwft: dbltrp: Trapped twice allowed ok
INFO: sbi: fwft: dbltrp: Should generate a double trap and crash !
sbi_trap_error: hart0: trap0: double trap handler failed (error -10)
sbi_trap_error: hart0: trap0: mcause=0x0000000000000010 mtval=0x0000000000000000
sbi_trap_error: hart0: trap0: mtval2=0x0000000000000003 mtinst=0x0000000000000000
sbi_trap_error: hart0: trap0: mepc=0x00000000802000d8 mstatus=0x8000000a01006900
sbi_trap_error: hart0: trap0: ra=0x00000000802001fc sp=0x0000000080213e70
sbi_trap_error: hart0: trap0: gp=0x0000000000000000 tp=0x0000000080088000
sbi_trap_error: hart0: trap0: s0=0x0000000080213e80 s1=0x0000000000000001
sbi_trap_error: hart0: trap0: a0=0x0000000080213e80 a1=0x0000000080208193
sbi_trap_error: hart0: trap0: a2=0x000000008020dc20 a3=0x000000000000000f
sbi_trap_error: hart0: trap0: a4=0x0000000080210cd8 a5=0x00000000802110d0
sbi_trap_error: hart0: trap0: a6=0x00000000802136e4 a7=0x0000000046574654
sbi_trap_error: hart0: trap0: s2=0x0000000080210cd9 s3=0x0000000000000000
sbi_trap_error: hart0: trap0: s4=0x0000000000000000 s5=0x0000000000000000
sbi_trap_error: hart0: trap0: s6=0x0000000000000000 s7=0x0000000000000001
sbi_trap_error: hart0: trap0: s8=0x0000000000002000 s9=0x0000000080083700
sbi_trap_error: hart0: trap0: s10=0x0000000000000000 s11=0x0000000000000000
sbi_trap_error: hart0: trap0: t0=0x0000000000000000 t1=0x0000000080213ed8
sbi_trap_error: hart0: trap0: t2=0x0000000000001000 t3=0x0000000080213ee0
sbi_trap_error: hart0: trap0: t4=0x0000000000000000 t5=0x000000008020f8d0
sbi_trap_error: hart0: trap0: t6=0x0000000000000000Run with linux and kvm-unit-test test in kvm (testing VS-mode):
$ qemu-system-riscv64 \ -M virt \ -cpu rv64,ssdbltrp=true,smdbltrp=true \ -nographic \ -serial mon:stdio \ -bios opensbi/build/platform/generic/firmware/fw_jump.bin \ -kernel linux/build/arch/riscv/boot/Image ... [Linux boot partially elided] [ 0.735079] riscv-dbltrp: Double trap handling registered ...$ lkvm run -k sbi_dbltrp.flat -m 128 -c 2 ########################################################################## # kvm-unit-tests ##########################################################################PASS: sbi: fwft: FWFT extension probing no error
PASS: sbi: fwft: FWFT extension is present
PASS: sbi: fwft: dbltrp: Get double trap enable feature value
PASS: sbi: fwft: dbltrp: Set double trap enable feature value == 0
PASS: sbi: fwft: dbltrp: Get double trap enable feature value == 0
PASS: sbi: fwft: dbltrp: Double trap disabled, trap first time ok
PASS: sbi: fwft: dbltrp: Set double trap enable feature value == 1
PASS: sbi: fwft: dbltrp: Get double trap enable feature value == 1
PASS: sbi: fwft: dbltrp: Trapped twice allowed ok
INFO: sbi: fwft: dbltrp: Should generate a double trap and crash ![ 51.939077] Guest double trap [ 51.939323] kvm [93]: VCPU exit error -95 [ 51.939683] kvm [93]: SEPC=0x802000d8 SSTATUS=0x200004520 HSTATUS=0x200200180 [ 51.939947] kvm [93]: SCAUSE=0x10 STVAL=0x0 HTVAL=0x3 HTINST=0x0 KVM_RUN failed: Operation not supported $Testing Smbdbltrp can be done using gdb and trigger some trap. For instance, interrupt M-mode firmware at some point, set mstatus.mdt = 1 and corrupt some register to generate a NULL pointer exception.
---
V6:
- Simplify and fixwrite_henvcfg()masking by assigning the written value to henvcfg and mask the value to be written as well as clearing the upper part of henvcfgh upon writing.
- Rebased on RNMI v9 series.V5:
- Use 0 instead of false to setMSTATUS_MDTinhelper_mnret()
- Added explicit comments about henvcfg write mask being tricky.
- Fixed a invalidmenvcfg_maskinwrite_henvcfghV4:
- Remove DTE fromsstatus_v1_10_maskvariable and add specific if for DTE masking where it's used.
- Use mstatus_hs.sdt field rather than setting DTE to 0 inriscv_do_cpu_interrupt().
- Add a fix for henvcfg value which was incorrectly set after changing menvcfg
- Remove uselessext_ssdbltrpcheck inriscv_env_smode_dbltrp_enabled().
- Remove useless mstatus clear inwrite_mstatus().
- Add proper handling of SDT writing to vsstatus.
- Add clearing of vsstatus//mstatus SDT field when DTE is disabled.
- Fix wrong value being written for MDT/MIE inwrite_mstatush().
- Rebased on Frank Snrnmi v7V3:
- Fix spec version from 1.12 to 1.13 for Smdbltrp and Ssdbltrp
- Add better comments for dte/sdt computation inriscv_cpu_do_interrupt().
- Move some CSR related changes to the CSRs related commits.V2:
- Squashed commits that added ext_s{s|m}dbltrp as suggested by Daniel
Quoted 10 lines
Clément Léger (9):
target/riscv: fix henvcfg potentially containing stale bits target/riscv: Add Ssdbltrp CSRs handling target/riscv: Implement Ssdbltrp sret, mret and mnret behavior target/riscv: Implement Ssdbltrp exception handling target/riscv: Add Ssdbltrp ISA extension enable switch target/riscv: Add Smdbltrp CSRs handling target/riscv: Implement Smdbltrp sret, mret and mnret behavior target/riscv: Implement Smdbltrp behavior target/riscv: Add Smdbltrp ISA extension enable switch
Do you mind rebasing this on:
https://github.com/alistair23/qemu/tree/riscv-to-apply.next
Alistair
Quoted 14 lines
target/riscv/cpu.c | 9 ++- target/riscv/cpu.h | 1 + target/riscv/cpu_bits.h | 8 +++ target/riscv/cpu_cfg.h | 2 + target/riscv/cpu_helper.c | 115 +++++++++++++++++++++++++++++++------- target/riscv/csr.c | 95 ++++++++++++++++++++++++++----- target/riscv/op_helper.c | 47 +++++++++++++++- 7 files changed, 239 insertions(+), 38 deletions(-)--
2.45.2
Quoted 192 lines · Alistair Francis
Quoted 180 lines · Clément Léger
A double trap typically arises during a sensitive phase in trap handling operations — when an exception or interrupt occurs while the trap handler (the component responsible for managing these events) is in a non-reentrant state. This non-reentrancy usually occurs in the early phase of trap handling, wherein the trap handler has not yet preserved the necessary state to handle and resume from the trap. The occurrence of such event is unlikely but can happen when dealing with hardware errors.
This series adds support for Ssdbltrp and Smdbltrp ratified ISA extensions [1]. It is based on the Smrnmi series [5].
Ssdbltrp can be tested using qemu[2], opensbi (master branch), linux[3] and kvm-unit-tests[4]. Assuming you have a riscv environment available and configured (
CROSS_COMPILE), it can be built for riscv64 using the following instructions:Qemu:
$ git clone https://github.com/rivosinc/qemu.git $ cd qemu $ git switch -C dbltrp_v6 dev/cleger/dbltrp_v6 $ mkdir build && cd build $ ../configure --target-list=riscv64-softmmu $ makeOpenSBI:
$ git clone https://github.com/rivosinc/opensbi.git $ cd opensbi $ make O=build PLATFORM_RISCV_XLEN=64 PLATFORM=genericLinux:
$ git clone https://github.com/rivosinc/linux.git $ cd linux $ git switch -C dbltrp_v1 dev/cleger/dbltrp_v1 $ export ARCH=riscv $ make O=build defconfig $ ./script/config --file build/.config --enable RISCV_DBLTRP $ make O=buildkvm-unit-tests:
$ git clone https://github.com/clementleger/kvm-unit-tests.git $ cd kvm-unit-tests $ git switch -C dbltrp_v1 dev/cleger/dbltrp_v1 $ ./configure --arch=riscv64 --cross-prefix=$CROSS_COMPILE $ makeYou will also need kvmtool in your rootfs.
Run with kvm-unit-test test as kernel:
$ qemu-system-riscv64 \ -M virt \ -cpu rv64,ssdbltrp=true,smdbltrp=true \ -nographic \ -serial mon:stdio \ -bios opensbi/build/platform/generic/firmware/fw_jump.bin \ -kernel kvm-unit-tests-dbltrp/riscv/sbi_dbltrp.flat ... [OpenSBI boot partially elided] Boot HART ISA Extensions : sscofpmf,sstc,zicntr,zihpm,zicboz,zicbom,sdtrig,svadu,ssdbltrp ... ########################################################################## # kvm-unit-tests ##########################################################################PASS: sbi: fwft: FWFT extension probing no error
PASS: sbi: fwft: FWFT extension is present
PASS: sbi: fwft: dbltrp: Get double trap enable feature value
PASS: sbi: fwft: dbltrp: Set double trap enable feature value == 0
PASS: sbi: fwft: dbltrp: Get double trap enable feature value == 0
PASS: sbi: fwft: dbltrp: Double trap disabled, trap first time ok
PASS: sbi: fwft: dbltrp: Set double trap enable feature value == 1
PASS: sbi: fwft: dbltrp: Get double trap enable feature value == 1
PASS: sbi: fwft: dbltrp: Trapped twice allowed ok
INFO: sbi: fwft: dbltrp: Should generate a double trap and crash !
sbi_trap_error: hart0: trap0: double trap handler failed (error -10)
sbi_trap_error: hart0: trap0: mcause=0x0000000000000010 mtval=0x0000000000000000
sbi_trap_error: hart0: trap0: mtval2=0x0000000000000003 mtinst=0x0000000000000000
sbi_trap_error: hart0: trap0: mepc=0x00000000802000d8 mstatus=0x8000000a01006900
sbi_trap_error: hart0: trap0: ra=0x00000000802001fc sp=0x0000000080213e70
sbi_trap_error: hart0: trap0: gp=0x0000000000000000 tp=0x0000000080088000
sbi_trap_error: hart0: trap0: s0=0x0000000080213e80 s1=0x0000000000000001
sbi_trap_error: hart0: trap0: a0=0x0000000080213e80 a1=0x0000000080208193
sbi_trap_error: hart0: trap0: a2=0x000000008020dc20 a3=0x000000000000000f
sbi_trap_error: hart0: trap0: a4=0x0000000080210cd8 a5=0x00000000802110d0
sbi_trap_error: hart0: trap0: a6=0x00000000802136e4 a7=0x0000000046574654
sbi_trap_error: hart0: trap0: s2=0x0000000080210cd9 s3=0x0000000000000000
sbi_trap_error: hart0: trap0: s4=0x0000000000000000 s5=0x0000000000000000
sbi_trap_error: hart0: trap0: s6=0x0000000000000000 s7=0x0000000000000001
sbi_trap_error: hart0: trap0: s8=0x0000000000002000 s9=0x0000000080083700
sbi_trap_error: hart0: trap0: s10=0x0000000000000000 s11=0x0000000000000000
sbi_trap_error: hart0: trap0: t0=0x0000000000000000 t1=0x0000000080213ed8
sbi_trap_error: hart0: trap0: t2=0x0000000000001000 t3=0x0000000080213ee0
sbi_trap_error: hart0: trap0: t4=0x0000000000000000 t5=0x000000008020f8d0
sbi_trap_error: hart0: trap0: t6=0x0000000000000000Run with linux and kvm-unit-test test in kvm (testing VS-mode):
$ qemu-system-riscv64 \ -M virt \ -cpu rv64,ssdbltrp=true,smdbltrp=true \ -nographic \ -serial mon:stdio \ -bios opensbi/build/platform/generic/firmware/fw_jump.bin \ -kernel linux/build/arch/riscv/boot/Image ... [Linux boot partially elided] [ 0.735079] riscv-dbltrp: Double trap handling registered ...$ lkvm run -k sbi_dbltrp.flat -m 128 -c 2 ########################################################################## # kvm-unit-tests ##########################################################################PASS: sbi: fwft: FWFT extension probing no error
PASS: sbi: fwft: FWFT extension is present
PASS: sbi: fwft: dbltrp: Get double trap enable feature value
PASS: sbi: fwft: dbltrp: Set double trap enable feature value == 0
PASS: sbi: fwft: dbltrp: Get double trap enable feature value == 0
PASS: sbi: fwft: dbltrp: Double trap disabled, trap first time ok
PASS: sbi: fwft: dbltrp: Set double trap enable feature value == 1
PASS: sbi: fwft: dbltrp: Get double trap enable feature value == 1
PASS: sbi: fwft: dbltrp: Trapped twice allowed ok
INFO: sbi: fwft: dbltrp: Should generate a double trap and crash ![ 51.939077] Guest double trap [ 51.939323] kvm [93]: VCPU exit error -95 [ 51.939683] kvm [93]: SEPC=0x802000d8 SSTATUS=0x200004520 HSTATUS=0x200200180 [ 51.939947] kvm [93]: SCAUSE=0x10 STVAL=0x0 HTVAL=0x3 HTINST=0x0 KVM_RUN failed: Operation not supported $Testing Smbdbltrp can be done using gdb and trigger some trap. For instance, interrupt M-mode firmware at some point, set mstatus.mdt = 1 and corrupt some register to generate a NULL pointer exception.
---
V6:
- Simplify and fixwrite_henvcfg()masking by assigning the written value to henvcfg and mask the value to be written as well as clearing the upper part of henvcfgh upon writing.
- Rebased on RNMI v9 series.V5:
- Use 0 instead of false to setMSTATUS_MDTinhelper_mnret()
- Added explicit comments about henvcfg write mask being tricky.
- Fixed a invalidmenvcfg_maskinwrite_henvcfghV4:
- Remove DTE fromsstatus_v1_10_maskvariable and add specific if for DTE masking where it's used.
- Use mstatus_hs.sdt field rather than setting DTE to 0 inriscv_do_cpu_interrupt().
- Add a fix for henvcfg value which was incorrectly set after changing menvcfg
- Remove uselessext_ssdbltrpcheck inriscv_env_smode_dbltrp_enabled().
- Remove useless mstatus clear inwrite_mstatus().
- Add proper handling of SDT writing to vsstatus.
- Add clearing of vsstatus//mstatus SDT field when DTE is disabled.
- Fix wrong value being written for MDT/MIE inwrite_mstatush().
- Rebased on Frank Snrnmi v7V3:
- Fix spec version from 1.12 to 1.13 for Smdbltrp and Ssdbltrp
- Add better comments for dte/sdt computation inriscv_cpu_do_interrupt().
- Move some CSR related changes to the CSRs related commits.V2:
- Squashed commits that added ext_s{s|m}dbltrp as suggested by DanielQuoted 10 lines
Clément Léger (9):
target/riscv: fix henvcfg potentially containing stale bits target/riscv: Add Ssdbltrp CSRs handling target/riscv: Implement Ssdbltrp sret, mret and mnret behavior target/riscv: Implement Ssdbltrp exception handling target/riscv: Add Ssdbltrp ISA extension enable switch target/riscv: Add Smdbltrp CSRs handling target/riscv: Implement Smdbltrp sret, mret and mnret behavior target/riscv: Implement Smdbltrp behavior target/riscv: Add Smdbltrp ISA extension enable switch
Quoted 2 lines
Do you mind rebasing this on:
https://github.com/alistair23/qemu/tree/riscv-to-apply.next
Hi Alistair,
Yes sure, let me do that and I'll send a v7
Thanks,
Clément
Quoted 17 lines
Alistair
Quoted 14 lines
target/riscv/cpu.c | 9 ++- target/riscv/cpu.h | 1 + target/riscv/cpu_bits.h | 8 +++ target/riscv/cpu_cfg.h | 2 + target/riscv/cpu_helper.c | 115 +++++++++++++++++++++++++++++++------- target/riscv/csr.c | 95 ++++++++++++++++++++++++++----- target/riscv/op_helper.c | 47 +++++++++++++++- 7 files changed, 239 insertions(+), 38 deletions(-)--
2.45.2