Skip to contentSkip to conversation

lib: sbi: add Ssdbltrp and Smdbltrp ISA extensions

PATCH New Clément Léger 3 participants 12 messages Oct 18, 2024 – Nov 4, 2024 RISC-V

lore
Share & tools
lore
#0 lore raw

v4 cover lib: sbi: add Ssdbltrp and Smdbltrp ISA 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].

Ssdbltrp can be tested using qemu[2], opensbi[3], linux[4] and kvm-unit-tests[5]. 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_v4 dev/cleger/dbltrp_v4
  $ mkdir build && cd build
  $ ../configure --target-list=riscv64-softmmu
  $ make

OpenSBI:

  $ git clone https://github.com/rivosinc/opensbi.git
  $ cd opensbi
  $ git switch -C dbltrp_v4 dev/cleger/dbltrp_v4
  $ 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.

---
V4:
- Rebased on master
- Move mstatusH restoration before mstatus since it contains MDT bit
- Factorize landing pad/shadow stack menvcfg usage in fwft

V3:
- Added Samuel Reviewed-by and fix some minor comments

V2:
- Add check for riscv_len inside CLEAR_MDT macro
- Order fwft entries by id order
- Move menvcfg generic access function to the top of sbi_fwft.c file
- Split SSE event support
- Add an additional MDT clear after MTVEC is set for all harts

Cl?ment L?ger (7):
lib: sbi: factorize previous virtualization mode read from regs
lib: sbi: factorize previous mode computation
lib: sbi: add Ssdbltrp ISA extension support
lib: sbi: send a double trap SSE event to supervisor
lib: sbi: fwft: factorize menvcfg read/write
lib: sbi: implement firmware feature SBI_FWFT_DOUBLE_TRAP
lib: sbi: add Smdbltrp ISA extension support

 firmware/fw_base.S                |  37 ++++++++--
 include/sbi/riscv_encoding.h      |  18 +++--
 include/sbi/sbi_ecall_interface.h |   1 +
 include/sbi/sbi_hart.h            |   2 +
 include/sbi/sbi_trap.h            |  14 ++++
 include/sbi/sbi_trap_ldst.h       |   2 +
 lib/sbi/objects.mk                |   1 +
 lib/sbi/sbi_double_trap.c         |  30 ++++++++
 lib/sbi/sbi_emulate_csr.c         |  16 +---
 lib/sbi/sbi_fwft.c                | 119 ++++++++++++++++--------------
 lib/sbi/sbi_hart.c                |   4 +
 lib/sbi/sbi_illegal_insn.c        |   2 +-
 lib/sbi/sbi_sse.c                 |   1 +
 lib/sbi/sbi_system.c              |   2 +-
 lib/sbi/sbi_trap.c                |  14 ++--
 lib/sbi/sbi_trap_ldst.c           |   4 +-
 16 files changed, 180 insertions(+), 87 deletions(-)
 create mode 100644 lib/sbi/sbi_double_trap.c
Signature · 1 line
2.45.2
#1 lore raw

PATCH v4 1/7 lib: sbi: factorize previous virtualization mode read from regs

The same pattern is used at multiple places to verify in which mode the exception was actually taken. Factorize it.

---
 include/sbi/sbi_trap.h    |  9 +++++++++
 lib/sbi/sbi_emulate_csr.c | 12 ++----------
 lib/sbi/sbi_trap.c        |  6 +-----
 3 files changed, 12 insertions(+), 15 deletions(-)
include/sbi/sbi_trap.h +9 -0
index f7c170e..b0c47ce 100644
@@ -236,6 +236,15 @@ static inline unsigned long sbi_regs_gva(const struct sbi_trap_regs *regs)
 #endif
 }
 
+static inline bool sbi_regs_from_virt(const struct sbi_trap_regs *regs)
+{
+#if __riscv_xlen == 32
+	return (regs->mstatusH & MSTATUSH_MPV) ? true : false;
+#else
+	return (regs->mstatus & MSTATUS_MPV) ? true : false;
+#endif
+}
+
 int sbi_trap_redirect(struct sbi_trap_regs *regs,
 		      const struct sbi_trap_info *trap);
 
lib/sbi/sbi_emulate_csr.c +2 -10
index 3adec78..a5a04cd 100644
@@ -47,11 +47,7 @@ int sbi_emulate_csr_read(int csr_num, struct sbi_trap_regs *regs,
 	int ret = 0;
 	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
 	ulong prev_mode = (regs->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT;
-#if __riscv_xlen == 32
-	bool virt = (regs->mstatusH & MSTATUSH_MPV) ? true : false;
-#else
-	bool virt = (regs->mstatus & MSTATUS_MPV) ? true : false;
-#endif
+	bool virt = sbi_regs_from_virt(regs);
 
 	switch (csr_num) {
 	case CSR_HTIMEDELTA:
@@ -157,11 +153,7 @@ int sbi_emulate_csr_write(int csr_num, struct sbi_trap_regs *regs,
 {
 	int ret = 0;
 	ulong prev_mode = (regs->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT;
-#if __riscv_xlen == 32
-	bool virt = (regs->mstatusH & MSTATUSH_MPV) ? true : false;
-#else
-	bool virt = (regs->mstatus & MSTATUS_MPV) ? true : false;
-#endif
+	bool virt = sbi_regs_from_virt(regs);
 
 	switch (csr_num) {
 	case CSR_HTIMEDELTA:
lib/sbi/sbi_trap.c +1 -5
index 176e174..ff0668e 100644
@@ -104,11 +104,7 @@ int sbi_trap_redirect(struct sbi_trap_regs *regs,
 {
 	ulong hstatus, vsstatus, prev_mode;
 	bool elp = false;
-#if __riscv_xlen == 32
-	bool prev_virt = (regs->mstatusH & MSTATUSH_MPV) ? true : false;
-#else
-	bool prev_virt = (regs->mstatus & MSTATUS_MPV) ? true : false;
-#endif
+	bool prev_virt = sbi_regs_from_virt(regs);
 	/* By default, we redirect to HS-mode */
 	bool next_virt = false;
 
Signature · 1 line
2.45.2
#2 lore raw

PATCH v4 2/7 lib: sbi: factorize previous mode computation

In reply to Clément Léger

Previous privilege mode retrieval from mstatus is done at different places, factorize it rather than copy/pasting it again.

---
 include/sbi/sbi_trap.h     | 5 +++++
 lib/sbi/sbi_emulate_csr.c  | 4 ++--
 lib/sbi/sbi_illegal_insn.c | 2 +-
 lib/sbi/sbi_system.c       | 2 +-
 lib/sbi/sbi_trap.c         | 4 ++--
 lib/sbi/sbi_trap_ldst.c    | 4 ++--
 6 files changed, 13 insertions(+), 8 deletions(-)
include/sbi/sbi_trap.h +5 -0
index b0c47ce..d5182bf 100644
@@ -245,6 +245,11 @@ static inline bool sbi_regs_from_virt(const struct sbi_trap_regs *regs)
 #endif
 }
 
+static inline int sbi_mstatus_prev_mode(unsigned long mstatus)
+{
+	return (mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT;
+}
+
 int sbi_trap_redirect(struct sbi_trap_regs *regs,
 		      const struct sbi_trap_info *trap);
 
lib/sbi/sbi_emulate_csr.c +2 -2
index a5a04cd..c2253c8 100644
@@ -46,7 +46,7 @@ int sbi_emulate_csr_read(int csr_num, struct sbi_trap_regs *regs,
 {
 	int ret = 0;
 	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
-	ulong prev_mode = (regs->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT;
+	ulong prev_mode = sbi_mstatus_prev_mode(regs->mstatus);
 	bool virt = sbi_regs_from_virt(regs);
 
 	switch (csr_num) {
@@ -152,7 +152,7 @@ int sbi_emulate_csr_write(int csr_num, struct sbi_trap_regs *regs,
 			  ulong csr_val)
 {
 	int ret = 0;
-	ulong prev_mode = (regs->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT;
+	ulong prev_mode = sbi_mstatus_prev_mode(regs->mstatus);
 	bool virt = sbi_regs_from_virt(regs);
 
 	switch (csr_num) {
lib/sbi/sbi_illegal_insn.c +1 -1
index e1d2cd3..784bc4c 100644
@@ -52,7 +52,7 @@ static int system_opcode_insn(ulong insn, struct sbi_trap_regs *regs)
 	int rs1_num	= GET_RS1_NUM(insn);
 	ulong rs1_val	= GET_RS1(insn, regs);
 	int csr_num	= GET_CSR_NUM((u32)insn);
-	ulong prev_mode = (regs->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT;
+	ulong prev_mode = sbi_mstatus_prev_mode(regs->mstatus);
 	ulong csr_val, new_csr_val;
 
 	if (prev_mode == PRV_M) {
lib/sbi/sbi_system.c +1 -1
index 8e624fd..cd0f4ba 100644
@@ -158,7 +158,7 @@ int sbi_system_suspend(u32 sleep_type, ulong resume_addr, ulong opaque)
 	if (ret != SBI_OK)
 		return ret;
 
-	prev_mode = (csr_read(CSR_MSTATUS) & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT;
+	prev_mode = sbi_mstatus_prev_mode(csr_read(CSR_MSTATUS));
 	if (prev_mode != PRV_S && prev_mode != PRV_U)
 		return SBI_EFAIL;
 
lib/sbi/sbi_trap.c +2 -2
index ff0668e..fd70e67 100644
@@ -109,7 +109,7 @@ int sbi_trap_redirect(struct sbi_trap_regs *regs,
 	bool next_virt = false;
 
 	/* Sanity check on previous mode */
-	prev_mode = (regs->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT;
+	prev_mode = sbi_mstatus_prev_mode(regs->mstatus);
 	if (prev_mode != PRV_S && prev_mode != PRV_U)
 		return SBI_ENOTSUPP;
 
@@ -361,7 +361,7 @@ trap_done:
 	if (rc)
 		sbi_trap_error(msg, rc, tcntx);
 
-	if (((regs->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT) != PRV_M)
+	if (sbi_mstatus_prev_mode(regs->mstatus) != PRV_M)
 		sbi_sse_process_pending_events(regs);
 
 	sbi_trap_set_context(scratch, tcntx->prev_context);
lib/sbi/sbi_trap_ldst.c +2 -2
index f9c0241..ebc4a92 100644
@@ -318,7 +318,7 @@ static int sbi_ld_access_emulator(int rlen, union sbi_ldst_data *out_val,
 	struct sbi_trap_regs *regs = &tcntx->regs;
 
 	/* If fault came from M mode, just fail */
-	if (((regs->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT) == PRV_M)
+	if (sbi_mstatus_prev_mode(regs->mstatus) == PRV_M)
 		return SBI_EINVAL;
 
 	/* If platform emulator failed, we redirect instead of fail */
@@ -341,7 +341,7 @@ static int sbi_st_access_emulator(int wlen, union sbi_ldst_data in_val,
 	struct sbi_trap_regs *regs = &tcntx->regs;
 
 	/* If fault came from M mode, just fail */
-	if (((regs->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT) == PRV_M)
+	if (sbi_mstatus_prev_mode(regs->mstatus) == PRV_M)
 		return SBI_EINVAL;
 
 	/* If platform emulator failed, we redirect instead of fail */
Signature · 1 line
2.45.2
#3 lore raw

PATCH v4 2/7 lib: sbi: factorize previous mode computation

Quoted 69 lines · Cl?ment L?ger

Previous privilege mode retrieval from mstatus is done at different places, factorize it rather than copy/pasting it again.

---
 include/sbi/sbi_trap.h     | 5 +++++
 lib/sbi/sbi_emulate_csr.c  | 4 ++--
 lib/sbi/sbi_illegal_insn.c | 2 +-
 lib/sbi/sbi_system.c       | 2 +-
 lib/sbi/sbi_trap.c         | 4 ++--
 lib/sbi/sbi_trap_ldst.c    | 4 ++--
 6 files changed, 13 insertions(+), 8 deletions(-)
include/sbi/sbi_trap.h +5 -0
index b0c47ce..d5182bf 100644
@@ -245,6 +245,11 @@ static inline bool sbi_regs_from_virt(const struct sbi_trap_regs *regs)
 #endif
 }
 
+static inline int sbi_mstatus_prev_mode(unsigned long mstatus)
+{
+	return (mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT;
+}
+
 int sbi_trap_redirect(struct sbi_trap_regs *regs,
 		      const struct sbi_trap_info *trap);
 
lib/sbi/sbi_emulate_csr.c +2 -2
index a5a04cd..c2253c8 100644
@@ -46,7 +46,7 @@ int sbi_emulate_csr_read(int csr_num, struct sbi_trap_regs *regs,
 {
 	int ret = 0;
 	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
-	ulong prev_mode = (regs->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT;
+	ulong prev_mode = sbi_mstatus_prev_mode(regs->mstatus);
 	bool virt = sbi_regs_from_virt(regs);
 
 	switch (csr_num) {
@@ -152,7 +152,7 @@ int sbi_emulate_csr_write(int csr_num, struct sbi_trap_regs *regs,
 			  ulong csr_val)
 {
 	int ret = 0;
-	ulong prev_mode = (regs->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT;
+	ulong prev_mode = sbi_mstatus_prev_mode(regs->mstatus);
 	bool virt = sbi_regs_from_virt(regs);
 
 	switch (csr_num) {
lib/sbi/sbi_illegal_insn.c +1 -1
index e1d2cd3..784bc4c 100644
@@ -52,7 +52,7 @@ static int system_opcode_insn(ulong insn, struct sbi_trap_regs *regs)
 	int rs1_num	= GET_RS1_NUM(insn);
 	ulong rs1_val	= GET_RS1(insn, regs);
 	int csr_num	= GET_CSR_NUM((u32)insn);
-	ulong prev_mode = (regs->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT;
+	ulong prev_mode = sbi_mstatus_prev_mode(regs->mstatus);
 	ulong csr_val, new_csr_val;
 
 	if (prev_mode == PRV_M) {
lib/sbi/sbi_system.c +0 -0
index 8e624fd..cd0f4ba 100644

lib/sbi/sbi_system.c int sbi_system_suspend(u32 sleep_type, ulong resume_addr, ulong opaque) · line 161

Quoted 5 lines
lib/sbi/sbi_system.c +1 -1
@@ -158,7 +158,7 @@ int sbi_system_suspend(u32 sleep_type, ulong resume_addr, ulong opaque)
 	if (ret != SBI_OK)
 		return ret;
 
-	prev_mode = (csr_read(CSR_MSTATUS) & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT;
+	prev_mode = sbi_mstatus_prev_mode(csr_read(CSR_MSTATUS));

I think this is a bug, since this field will get clobbered by a nested trap while in M-mode. But this is not your bug, so:

Quoted 47 lines
lib/sbi/sbi_system.c +0 -0
 	if (prev_mode != PRV_S && prev_mode != PRV_U)
 		return SBI_EFAIL;
 
lib/sbi/sbi_trap.c +2 -2
index ff0668e..fd70e67 100644
@@ -109,7 +109,7 @@ int sbi_trap_redirect(struct sbi_trap_regs *regs,
 	bool next_virt = false;
 
 	/* Sanity check on previous mode */
-	prev_mode = (regs->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT;
+	prev_mode = sbi_mstatus_prev_mode(regs->mstatus);
 	if (prev_mode != PRV_S && prev_mode != PRV_U)
 		return SBI_ENOTSUPP;
 
@@ -361,7 +361,7 @@ trap_done:
 	if (rc)
 		sbi_trap_error(msg, rc, tcntx);
 
-	if (((regs->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT) != PRV_M)
+	if (sbi_mstatus_prev_mode(regs->mstatus) != PRV_M)
 		sbi_sse_process_pending_events(regs);
 
 	sbi_trap_set_context(scratch, tcntx->prev_context);
lib/sbi/sbi_trap_ldst.c +2 -2
index f9c0241..ebc4a92 100644
@@ -318,7 +318,7 @@ static int sbi_ld_access_emulator(int rlen, union sbi_ldst_data *out_val,
 	struct sbi_trap_regs *regs = &tcntx->regs;
 
 	/* If fault came from M mode, just fail */
-	if (((regs->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT) == PRV_M)
+	if (sbi_mstatus_prev_mode(regs->mstatus) == PRV_M)
 		return SBI_EINVAL;
 
 	/* If platform emulator failed, we redirect instead of fail */
@@ -341,7 +341,7 @@ static int sbi_st_access_emulator(int wlen, union sbi_ldst_data in_val,
 	struct sbi_trap_regs *regs = &tcntx->regs;
 
 	/* If fault came from M mode, just fail */
-	if (((regs->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT) == PRV_M)
+	if (sbi_mstatus_prev_mode(regs->mstatus) == PRV_M)
 		return SBI_EINVAL;
 
 	/* If platform emulator failed, we redirect instead of fail */
#4 lore raw

PATCH v4 2/7 lib: sbi: factorize previous mode computation

Quoted 73 lines · Samuel Holland
Quoted 72 lines · Cl?ment L?ger

Previous privilege mode retrieval from mstatus is done at different places, factorize it rather than copy/pasting it again.

---
 include/sbi/sbi_trap.h     | 5 +++++
 lib/sbi/sbi_emulate_csr.c  | 4 ++--
 lib/sbi/sbi_illegal_insn.c | 2 +-
 lib/sbi/sbi_system.c       | 2 +-
 lib/sbi/sbi_trap.c         | 4 ++--
 lib/sbi/sbi_trap_ldst.c    | 4 ++--
 6 files changed, 13 insertions(+), 8 deletions(-)
include/sbi/sbi_trap.h +5 -0
index b0c47ce..d5182bf 100644
@@ -245,6 +245,11 @@ static inline bool sbi_regs_from_virt(const struct sbi_trap_regs *regs)
 #endif
 }
 
+static inline int sbi_mstatus_prev_mode(unsigned long mstatus)
+{
+	return (mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT;
+}
+
 int sbi_trap_redirect(struct sbi_trap_regs *regs,
 		      const struct sbi_trap_info *trap);
 
lib/sbi/sbi_emulate_csr.c +2 -2
index a5a04cd..c2253c8 100644
@@ -46,7 +46,7 @@ int sbi_emulate_csr_read(int csr_num, struct sbi_trap_regs *regs,
 {
 	int ret = 0;
 	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
-	ulong prev_mode = (regs->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT;
+	ulong prev_mode = sbi_mstatus_prev_mode(regs->mstatus);
 	bool virt = sbi_regs_from_virt(regs);
 
 	switch (csr_num) {
@@ -152,7 +152,7 @@ int sbi_emulate_csr_write(int csr_num, struct sbi_trap_regs *regs,
 			  ulong csr_val)
 {
 	int ret = 0;
-	ulong prev_mode = (regs->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT;
+	ulong prev_mode = sbi_mstatus_prev_mode(regs->mstatus);
 	bool virt = sbi_regs_from_virt(regs);
 
 	switch (csr_num) {
lib/sbi/sbi_illegal_insn.c +1 -1
index e1d2cd3..784bc4c 100644
@@ -52,7 +52,7 @@ static int system_opcode_insn(ulong insn, struct sbi_trap_regs *regs)
 	int rs1_num	= GET_RS1_NUM(insn);
 	ulong rs1_val	= GET_RS1(insn, regs);
 	int csr_num	= GET_CSR_NUM((u32)insn);
-	ulong prev_mode = (regs->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT;
+	ulong prev_mode = sbi_mstatus_prev_mode(regs->mstatus);
 	ulong csr_val, new_csr_val;
 
 	if (prev_mode == PRV_M) {
lib/sbi/sbi_system.c +0 -0
index 8e624fd..cd0f4ba 100644
@@ -158,7 +158,7 @@ int sbi_system_suspend(u32 sleep_type, ulong resume_addr, ulong opaque)
 	if (ret != SBI_OK)
 		return ret;
 
Quoted 5 lines

lib/sbi/sbi_system.c int sbi_system_suspend(u32 sleep_type, ulong resume_addr, ulong opaque) · line 161

Quoted 2 lines
lib/sbi/sbi_system.c +1 -1
-	prev_mode = (csr_read(CSR_MSTATUS) & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT;
+	prev_mode = sbi_mstatus_prev_mode(csr_read(CSR_MSTATUS));

I think this is a bug, since this field will get clobbered by a nested trap while in M-mode. But this is not your bug, so:

Hi Samuel,

Nice catch, I can take care of it if you don't already have a fix.

Thanks,

Cl?ment

Quoted 51 lines
Quoted 47 lines
lib/sbi/sbi_system.c +0 -0
 	if (prev_mode != PRV_S && prev_mode != PRV_U)
 		return SBI_EFAIL;
 
lib/sbi/sbi_trap.c +2 -2
index ff0668e..fd70e67 100644
@@ -109,7 +109,7 @@ int sbi_trap_redirect(struct sbi_trap_regs *regs,
 	bool next_virt = false;
 
 	/* Sanity check on previous mode */
-	prev_mode = (regs->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT;
+	prev_mode = sbi_mstatus_prev_mode(regs->mstatus);
 	if (prev_mode != PRV_S && prev_mode != PRV_U)
 		return SBI_ENOTSUPP;
 
@@ -361,7 +361,7 @@ trap_done:
 	if (rc)
 		sbi_trap_error(msg, rc, tcntx);
 
-	if (((regs->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT) != PRV_M)
+	if (sbi_mstatus_prev_mode(regs->mstatus) != PRV_M)
 		sbi_sse_process_pending_events(regs);
 
 	sbi_trap_set_context(scratch, tcntx->prev_context);
lib/sbi/sbi_trap_ldst.c +2 -2
index f9c0241..ebc4a92 100644
@@ -318,7 +318,7 @@ static int sbi_ld_access_emulator(int rlen, union sbi_ldst_data *out_val,
 	struct sbi_trap_regs *regs = &tcntx->regs;
 
 	/* If fault came from M mode, just fail */
-	if (((regs->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT) == PRV_M)
+	if (sbi_mstatus_prev_mode(regs->mstatus) == PRV_M)
 		return SBI_EINVAL;
 
 	/* If platform emulator failed, we redirect instead of fail */
@@ -341,7 +341,7 @@ static int sbi_st_access_emulator(int wlen, union sbi_ldst_data in_val,
 	struct sbi_trap_regs *regs = &tcntx->regs;
 
 	/* If fault came from M mode, just fail */
-	if (((regs->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT) == PRV_M)
+	if (sbi_mstatus_prev_mode(regs->mstatus) == PRV_M)
 		return SBI_EINVAL;
 
 	/* If platform emulator failed, we redirect instead of fail */
#5 lore raw

PATCH v4 3/7 lib: sbi: add Ssdbltrp ISA extension support

In reply to Clément Léger

Add Ssdbltrp trap handler support for S-mode double trap handling. If the trap is received while in VS-mode, then the trap is redirected to S-mode. If caught while in HS-mode, then an error is returned to the top trap handler which will panic.

---
 include/sbi/riscv_encoding.h |  4 +++-
 include/sbi/sbi_hart.h       |  2 ++
 include/sbi/sbi_trap_ldst.h  |  2 ++
 lib/sbi/objects.mk           |  1 +
 lib/sbi/sbi_double_trap.c    | 30 ++++++++++++++++++++++++++++++
 lib/sbi/sbi_hart.c           |  4 ++++
 lib/sbi/sbi_trap.c           |  4 ++++
 7 files changed, 46 insertions(+), 1 deletion(-)
 create mode 100644 lib/sbi/sbi_double_trap.c
include/sbi/riscv_encoding.h +3 -1
index 980abdb..2264427 100644
@@ -216,6 +216,7 @@
 #define ENVCFG_PBMTE			(_ULL(1) << 62)
 #define ENVCFG_ADUE			(_ULL(1) << 61)
 #define ENVCFG_CDE			(_ULL(1) << 60)
+#define ENVCFG_DTE			(_ULL(1) << 59)
 #define ENVCFG_PMM			(_ULL(0x3) << 32)
 #define ENVCFG_PMM_PMLEN_0		(_ULL(0x0) << 32)
 #define ENVCFG_PMM_PMLEN_7		(_ULL(0x2) << 32)
@@ -773,7 +774,8 @@
 #define CAUSE_FETCH_PAGE_FAULT		0xc
 #define CAUSE_LOAD_PAGE_FAULT		0xd
 #define CAUSE_STORE_PAGE_FAULT		0xf
-#define CAUSE_SW_CHECK_EXCP			0x12
+#define CAUSE_DOUBLE_TRAP		0x10
+#define CAUSE_SW_CHECK_EXCP		0x12
 #define CAUSE_FETCH_GUEST_PAGE_FAULT	0x14
 #define CAUSE_LOAD_GUEST_PAGE_FAULT	0x15
 #define CAUSE_VIRTUAL_INST_FAULT	0x16
include/sbi/sbi_hart.h +2 -0
index 22d8327..4c36c77 100644
@@ -73,6 +73,8 @@ enum sbi_hart_extensions {
 	SBI_HART_EXT_ZICFILP,
 	/** HART has zicfiss extension */
 	SBI_HART_EXT_ZICFISS,
+	/** Hart has Ssdbltrp extension */
+	SBI_HART_EXT_SSDBLTRP,
 
 	/** Maximum index of Hart extension */
 	SBI_HART_EXT_MAX,
include/sbi/sbi_trap_ldst.h +2 -0
index 8aee316..4c5cc37 100644
@@ -28,4 +28,6 @@ int sbi_load_access_handler(struct sbi_trap_context *tcntx);
 
 int sbi_store_access_handler(struct sbi_trap_context *tcntx);
 
+int sbi_double_trap_handler(struct sbi_trap_context *tcntx);
+
 #endif
lib/sbi/objects.mk +1 -0
index 535aa70..7be943f 100644
@@ -66,6 +66,7 @@ libsbi-objs-y += sbi_bitops.o
 libsbi-objs-y += sbi_console.o
 libsbi-objs-y += sbi_domain_context.o
 libsbi-objs-y += sbi_domain.o
+libsbi-objs-y += sbi_double_trap.o
 libsbi-objs-y += sbi_emulate_csr.o
 libsbi-objs-y += sbi_fifo.o
 libsbi-objs-y += sbi_fwft.o
lib/sbi/sbi_double_trap.c new file +30 -0
new file mode 100644
index 0000000..96f2fa5
@@ -0,0 +1,30 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2024 Rivos Inc.
+ *
+ * Authors:
+ *   Cl?ment L?ger <[email protected]>
+ */
+
+#include <sbi/sbi_console.h>
+#include <sbi/sbi_ecall_interface.h>
+#include <sbi/sbi_error.h>
+#include <sbi/sbi_sse.h>
+#include <sbi/sbi_trap.h>
+
+int sbi_double_trap_handler(struct sbi_trap_context *tcntx)
+{
+	struct sbi_trap_regs *regs = &tcntx->regs;
+	const struct sbi_trap_info *trap = &tcntx->trap;
+	bool prev_virt = sbi_regs_from_virt(regs);
+
+	if (sbi_mstatus_prev_mode(regs->mstatus) != PRV_S)
+		return SBI_ERR_INVALID_PARAM;
+
+	/* Exception was taken in VS-mode, redirect it to S-mode */
+	if (prev_virt)
+		return sbi_trap_redirect(regs, trap);
+
+	return SBI_ENOTSUPP;
+}
lib/sbi/sbi_hart.c +4 -0
index d9aef31..dc0863a 100644
@@ -117,6 +117,9 @@ static void mstatus_init(struct sbi_scratch *scratch)
 		menvcfg_val |= ((uint64_t)csr_read(CSR_MENVCFGH)) << 32;
 #endif
 
+		/* Disable double trap by default */
+		menvcfg_val &= ~ENVCFG_DTE;
+
 #define __set_menvcfg_ext(__ext, __bits)				\
 		if (sbi_hart_has_extension(scratch, __ext))		\
 			menvcfg_val |= __bits;
@@ -684,6 +687,7 @@ const struct sbi_hart_ext_data sbi_hart_ext[] = {
 	__SBI_HART_EXT_DATA(smnpm, SBI_HART_EXT_SMNPM),
 	__SBI_HART_EXT_DATA(zicfilp, SBI_HART_EXT_ZICFILP),
 	__SBI_HART_EXT_DATA(zicfiss, SBI_HART_EXT_ZICFISS),
+	__SBI_HART_EXT_DATA(ssdbltrp, SBI_HART_EXT_SSDBLTRP),
 };
 
 _Static_assert(SBI_HART_EXT_MAX == array_size(sbi_hart_ext),
lib/sbi/sbi_trap.c +4 -0
index fd70e67..d2de0c8 100644
@@ -350,6 +350,10 @@ struct sbi_trap_context *sbi_trap_handler(struct sbi_trap_context *tcntx)
 		rc  = sbi_store_access_handler(tcntx);
 		msg = "store fault handler failed";
 		break;
+	case CAUSE_DOUBLE_TRAP:
+		rc  = sbi_double_trap_handler(tcntx);
+		msg = "double trap handler failed";
+		break;
 	default:
 		/* If the trap came from S or U mode, redirect it there */
 		msg = "trap redirect failed";
Signature · 1 line
2.45.2
#6 lore raw

PATCH v4 4/7 lib: sbi: send a double trap SSE event to supervisor

In reply to Clément Léger

In case the double trap handler is called and the double trap happened in supervisor mode, send a double trap SSE event.

NOTE: this commit depends on the ratification of the new SSE event id for double trap [1].

---
 include/sbi/sbi_ecall_interface.h | 1 +
 lib/sbi/sbi_double_trap.c         | 2 +-
 lib/sbi/sbi_sse.c                 | 1 +
 3 files changed, 3 insertions(+), 1 deletion(-)
include/sbi/sbi_ecall_interface.h +1 -0
index 2958b41..e4275b7 100644
@@ -380,6 +380,7 @@ enum sbi_sse_state {
 
 /* SBI SSE Event IDs. */
 #define SBI_SSE_EVENT_LOCAL_RAS			0x00000000
+#define SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP		0x00000001
 #define SBI_SSE_EVENT_LOCAL_PLAT_0_START	0x00004000
 #define SBI_SSE_EVENT_LOCAL_PLAT_0_END		0x00007fff
 #define SBI_SSE_EVENT_GLOBAL_RAS		0x00008000
lib/sbi/sbi_double_trap.c +1 -1
index 96f2fa5..bab96fb 100644
@@ -26,5 +26,5 @@ int sbi_double_trap_handler(struct sbi_trap_context *tcntx)
 	if (prev_virt)
 		return sbi_trap_redirect(regs, trap);
 
-	return SBI_ENOTSUPP;
+	return sbi_sse_inject_event(SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP);
 }
lib/sbi/sbi_sse.c +1 -0
index ab03b9c..a2352a1 100644
@@ -41,6 +41,7 @@
 
 static const uint32_t supported_events[] = {
 	SBI_SSE_EVENT_LOCAL_RAS,
+	SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP,
 	SBI_SSE_EVENT_GLOBAL_RAS,
 	SBI_SSE_EVENT_LOCAL_PMU,
 	SBI_SSE_EVENT_LOCAL_SOFTWARE,
Signature · 1 line
2.45.2
#7 lore raw

PATCH v4 5/7 lib: sbi: fwft: factorize menvcfg read/write

In reply to Clément Léger

MENVCFG access will be used as well for double trap, landing pad and shadow stack fwft support. Factorize that in a common function.

---
 include/sbi/riscv_encoding.h |  9 ++--
 lib/sbi/sbi_fwft.c           | 94 +++++++++++++++---------------------
 2 files changed, 46 insertions(+), 57 deletions(-)
include/sbi/riscv_encoding.h +6 -3
index 2264427..fd34ba7 100644
@@ -214,7 +214,8 @@
 
 #define ENVCFG_STCE			(_ULL(1) << 63)
 #define ENVCFG_PBMTE			(_ULL(1) << 62)
-#define ENVCFG_ADUE			(_ULL(1) << 61)
+#define ENVCFG_ADUE_SHIFT		61
+#define ENVCFG_ADUE			(_ULL(1) << ENVCFG_ADUE_SHIFT)
 #define ENVCFG_CDE			(_ULL(1) << 60)
 #define ENVCFG_DTE			(_ULL(1) << 59)
 #define ENVCFG_PMM			(_ULL(0x3) << 32)
@@ -228,8 +229,10 @@
 #define ENVCFG_CBIE_ILL			_UL(0x0)
 #define ENVCFG_CBIE_FLUSH		_UL(0x1)
 #define ENVCFG_CBIE_INV			_UL(0x3)
-#define ENVCFG_SSE			(_UL(1) << 3)
-#define ENVCFG_LPE			(_UL(1) << 2)
+#define ENVCFG_SSE_SHIFT		3
+#define ENVCFG_SSE			(_UL(1) << ENVCFG_SSE_SHIFT)
+#define ENVCFG_LPE_SHIFT		2
+#define ENVCFG_LPE			(_UL(1) << ENVCFG_LPE_SHIFT)
 #define ENVCFG_FIOM			_UL(0x1)
 
 /* ===== User-level CSRs ===== */
lib/sbi/sbi_fwft.c +40 -54

130 lines not on this page · raw message

Signature · 1 line
2.45.2
#8 lore raw

PATCH v4 6/7 lib: sbi: implement firmware feature SBI_FWFT_DOUBLE_TRAP

In reply to Clément Léger

Add support for double trap firmware feature.

---
 include/sbi/riscv_encoding.h |  3 ++-
 lib/sbi/sbi_fwft.c           | 25 +++++++++++++++++++++++++
 2 files changed, 27 insertions(+), 1 deletion(-)
include/sbi/riscv_encoding.h +2 -1
index fd34ba7..174cc9c 100644
@@ -217,7 +217,8 @@
 #define ENVCFG_ADUE_SHIFT		61
 #define ENVCFG_ADUE			(_ULL(1) << ENVCFG_ADUE_SHIFT)
 #define ENVCFG_CDE			(_ULL(1) << 60)
-#define ENVCFG_DTE			(_ULL(1) << 59)
+#define ENVCFG_DTE_SHIFT		59
+#define ENVCFG_DTE			(_ULL(1) << ENVCFG_DTE_SHIFT)
 #define ENVCFG_PMM			(_ULL(0x3) << 32)
 #define ENVCFG_PMM_PMLEN_0		(_ULL(0x0) << 32)
 #define ENVCFG_PMM_PMLEN_7		(_ULL(0x2) << 32)
lib/sbi/sbi_fwft.c +25 -0
index 9ee0d07..9cf14c1 100644
@@ -137,6 +137,25 @@ static int fwft_get_misaligned_delegation(struct fwft_config *conf,
 	return SBI_OK;
 }
 
+static int fwft_double_trap_supported(struct fwft_config *conf)
+{
+	if (!sbi_hart_has_extension(sbi_scratch_thishart_ptr(),
+				    SBI_HART_EXT_SSDBLTRP))
+		return SBI_ENOTSUPP;
+
+	return SBI_OK;
+}
+
+static int fwft_set_double_trap(struct fwft_config *conf, unsigned long value)
+{
+	return fwft_menvcfg_set_bit(value, ENVCFG_DTE_SHIFT);
+}
+
+static int fwft_get_double_trap(struct fwft_config *conf, unsigned long *value)
+{
+	return fwft_menvcfg_read_bit(value, ENVCFG_DTE_SHIFT);
+}
+
 static int fwft_adue_supported(struct fwft_config *conf)
 {
 	if (!sbi_hart_has_extension(sbi_scratch_thishart_ptr(),
@@ -349,6 +368,12 @@ static const struct fwft_feature features[] =
 		.set = fwft_enable_sstack,
 		.get = fwft_get_sstack,
 	},
+	{
+		.id = SBI_FWFT_DOUBLE_TRAP,
+		.supported = fwft_double_trap_supported,
+		.set = fwft_set_double_trap,
+		.get = fwft_get_double_trap,
+	},
 	{
 		.id = SBI_FWFT_PTE_AD_HW_UPDATING,
 		.supported = fwft_adue_supported,
Signature · 1 line
2.45.2
#9 lore raw

PATCH v4 7/7 lib: sbi: add Smdbltrp ISA extension support

In reply to Clément Léger

Add support for the Smdbltrp[1] ISA extension. First thing to do is clearing MDT on entry after setting the first MTVEC (since MDT is reset to 1). Additionally, during trap handling, clear MDT once all critical CSRs have been saved and in return path, restore MSTATUS/H before restoring MEPC to avoid taking another trap which would clobber it.

---
 firmware/fw_base.S           | 37 +++++++++++++++++++++++++++++++-----
 include/sbi/riscv_encoding.h |  4 +++-
 2 files changed, 35 insertions(+), 6 deletions(-)
firmware/fw_base.S +32 -5
index 4a2652e..d027e5e 100644
@@ -31,6 +31,16 @@
 	add	\__d4, \__s4, zero
 .endm
 
+.macro CLEAR_MDT tmp
+#if __riscv_xlen == 32
+	li 	\tmp, MSTATUSH_MDT
+	csrc	CSR_MSTATUSH, \tmp
+#else
+	li 	\tmp, MSTATUS_MDT
+	csrc	CSR_MSTATUS, \tmp
+#endif
+.endm
+
 	.section .entry, "ax", %progbits
 	.align 3
 	.globl _start
@@ -91,6 +101,14 @@ _bss_zero:
 	lla	s4, _start_hang
 	csrw	CSR_MTVEC, s4
 
+	/*
+	 * While at this point, trap handling is rudimentary, if a trap happens,
+	 * it will end up in _start_hang which is enough to hook up a GDB. Clear
+	 * MDT to avoid generating a double trap and thus entering a
+	 * critical-error state.
+	 */
+	CLEAR_MDT t0
+
 	/* Setup temporary stack */
 	lla	s4, _fw_end
 	li	s5, (SBI_SCRATCH_SIZE * 2)
@@ -348,6 +366,9 @@ _start_warm:
 _skip_trap_handler_hyp:
 	csrw	CSR_MTVEC, a4
 
+	/* Clear MDT here again for all harts */
+	CLEAR_MDT t0
+
 	/* Initialize SBI runtime */
 	csrr	a0, CSR_MSCRATCH
 	call	sbi_init
@@ -545,6 +566,9 @@ memcmp:
 	li	t0, 0
 .endif
 	REG_S	t0, (SBI_TRAP_REGS_SIZE + SBI_TRAP_INFO_OFFSET(gva))(sp)
+
+	/* We are ready to take another trap, clear MDT */
+	CLEAR_MDT t0
 .endm
 
 .macro	TRAP_CALL_C_ROUTINE
@@ -587,15 +611,18 @@ memcmp:
 .endm
 
 .macro	TRAP_RESTORE_MEPC_MSTATUS have_mstatush
-	/* Restore MEPC and MSTATUS CSRs */
-	REG_L	t0, SBI_TRAP_REGS_OFFSET(mepc)(a0)
-	csrw	CSR_MEPC, t0
-	REG_L	t0, SBI_TRAP_REGS_OFFSET(mstatus)(a0)
-	csrw	CSR_MSTATUS, t0
+	/*
+	 * Restore MSTATUS and MEPC CSRs starting with MSTATUS/H to set MDT
+	 * flags since we can not take a trap now or MEPC would be cloberred
+	 */
 	.if \have_mstatush
 	REG_L	t0, SBI_TRAP_REGS_OFFSET(mstatusH)(a0)
 	csrw	CSR_MSTATUSH, t0
 	.endif
+	REG_L	t0, SBI_TRAP_REGS_OFFSET(mstatus)(a0)
+	csrw	CSR_MSTATUS, t0
+	REG_L	t0, SBI_TRAP_REGS_OFFSET(mepc)(a0)
+	csrw	CSR_MEPC, t0
 .endm
 
 .macro TRAP_RESTORE_A0_T0
include/sbi/riscv_encoding.h +3 -1
index 174cc9c..38997ef 100644
@@ -42,7 +42,8 @@
 #define MSTATUS_GVA			_ULL(0x0000004000000000)
 #define MSTATUS_GVA_SHIFT		38
 #define MSTATUS_MPV			_ULL(0x0000008000000000)
-#define MSTATUS_MPELP		_ULL(0x0000020000000000)
+#define MSTATUS_MPELP			_ULL(0x0000020000000000)
+#define MSTATUS_MDT			_ULL(0x0000040000000000)
 #else
 #define MSTATUSH_SBE			_UL(0x00000010)
 #define MSTATUSH_MBE			_UL(0x00000020)
@@ -50,6 +51,7 @@
 #define MSTATUSH_GVA_SHIFT		6
 #define MSTATUSH_MPV			_UL(0x00000080)
 #define MSTATUSH_MPELP			_UL(0x00000200)
+#define MSTATUSH_MDT			_UL(0x00000400)
 #endif
 #define MSTATUS32_SD			_UL(0x80000000)
 #define MSTATUS64_SD			_ULL(0x8000000000000000)
Signature · 1 line
2.45.2
#10 lore raw

PATCH v4 7/7 lib: sbi: add Smdbltrp ISA extension support

Quoted 7 lines · Cl?ment L?ger

Add support for the Smdbltrp[1] ISA extension. First thing to do is clearing MDT on entry after setting the first MTVEC (since MDT is reset to 1). Additionally, during trap handling, clear MDT once all critical CSRs have been saved and in return path, restore MSTATUS/H before restoring MEPC to avoid taking another trap which would clobber it.

Quoted 6 lines
---
 firmware/fw_base.S           | 37 +++++++++++++++++++++++++++++++-----
 include/sbi/riscv_encoding.h |  4 +++-
 2 files changed, 35 insertions(+), 6 deletions(-)
Applied
#11 lore raw

v4 cover lib: sbi: add Ssdbltrp and Smdbltrp ISA extensions

In reply to Clément Léger
Quoted 160 lines · On Fri, Oct 18, 2024 at 2:22?PM Cl?ment L?ger <[email protected]> wrote:

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].

Ssdbltrp can be tested using qemu[2], opensbi[3], linux[4] and kvm-unit-tests[5]. 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_v4 dev/cleger/dbltrp_v4
  $ mkdir build && cd build
  $ ../configure --target-list=riscv64-softmmu
  $ make

OpenSBI:

  $ git clone https://github.com/rivosinc/opensbi.git
  $ cd opensbi
  $ git switch -C dbltrp_v4 dev/cleger/dbltrp_v4
  $ 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.

---
V4:
- Rebased on master
- Move mstatusH restoration before mstatus since it contains MDT bit
- Factorize landing pad/shadow stack menvcfg usage in fwft

V3:
- Added Samuel Reviewed-by and fix some minor comments

V2:
- Add check for riscv_len inside CLEAR_MDT macro
- Order fwft entries by id order
- Move menvcfg generic access function to the top of sbi_fwft.c file
- Split SSE event support
- Add an additional MDT clear after MTVEC is set for all harts

Quoted 8 lines

Cl?ment L?ger (7):
lib: sbi: factorize previous virtualization mode read from regs
lib: sbi: factorize previous mode computation
lib: sbi: add Ssdbltrp ISA extension support
lib: sbi: send a double trap SSE event to supervisor
lib: sbi: fwft: factorize menvcfg read/write
lib: sbi: implement firmware feature SBI_FWFT_DOUBLE_TRAP
lib: sbi: add Smdbltrp ISA extension support

Applied this series to the riscv/opensbi repo.

Thanks,
Anup

Quoted 28 lines
 firmware/fw_base.S                |  37 ++++++++--
 include/sbi/riscv_encoding.h      |  18 +++--
 include/sbi/sbi_ecall_interface.h |   1 +
 include/sbi/sbi_hart.h            |   2 +
 include/sbi/sbi_trap.h            |  14 ++++
 include/sbi/sbi_trap_ldst.h       |   2 +
 lib/sbi/objects.mk                |   1 +
 lib/sbi/sbi_double_trap.c         |  30 ++++++++
 lib/sbi/sbi_emulate_csr.c         |  16 +---
 lib/sbi/sbi_fwft.c                | 119 ++++++++++++++++--------------
 lib/sbi/sbi_hart.c                |   4 +
 lib/sbi/sbi_illegal_insn.c        |   2 +-
 lib/sbi/sbi_sse.c                 |   1 +
 lib/sbi/sbi_system.c              |   2 +-
 lib/sbi/sbi_trap.c                |  14 ++--
 lib/sbi/sbi_trap_ldst.c           |   4 +-
 16 files changed, 180 insertions(+), 87 deletions(-)
 create mode 100644 lib/sbi/sbi_double_trap.c

--
2.45.2

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi