Skip to contentSkip to conversation

lib: sbi: Add support for Smdbltrp and Ssdbltrp extensions

RFC PATCH New Clément Léger 3 participants 14 messages Apr 18, 2024 – Jun 17, 2024 RISC-V

lore
Share & tools
lore
#0 lore raw

cover lib: sbi: 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 as well as Smdbltrp [1]. This series is based on top of the firmware feature implementation (SBI FWFT extension).

Ssdbltrp can be tested using qemu[1], opensbi[2], linux[3] 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 dev/cleger/dbltrp_rfc_v1
  $ mkdir build && cd build
  $ ../configure --target-list=riscv64-softmmu
  $ make

OpenSBI:

  $ git clone https://github.com/rivosinc/opensbi.git
  $ cd opensbi
  $ git switch dev/cleger/dbltrp_rfc_v1
  $ make O=build PLATFORM_RISCV_XLEN=64 PLATFORM=generic

Linux:

  $ git clone https://github.com/rivosinc/linux.git
  $ cd linux
  $ git switch dev/cleger/dbltrp_rfc_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 dev/cleger/dbltrp_rfc_v1
  $ ./configure --arch=riscv64 --cross-prefix=$CROSS_COMPILE
  $ make

You will also need kvmtool in your rootfs. One can build a buildroot rootfs using the buildroot provided at [6] (which contains an update of kvmtool with riscv support).

Run with kvm-unit-tests test as kernel:

  $ qemu-system-riscv64 \
    -M virt \
    -cpu rv64,x-ssdbltrp=true,x-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-tests test in kvm (testing VS-mode):

  $ qemu-system-riscv64 \
    -M virt \
    -cpu rv64,x-ssdbltrp=true,x-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
  $

---

Cl?ment L?ger (4):
lib: sbi: factorize previous virtualization mode read from regs
lib: riscv: add Ssdbltrp support
lib: sbi: implement SBI_FWFT_DOUBLE_TRAP_ENABLE
lib: sbi: add support for the Smdbltrp extension

 firmware/fw_base.S                | 29 +++++++++++++++--
 include/sbi/riscv_encoding.h      |  5 +++
 include/sbi/sbi_ecall_interface.h |  1 +
 include/sbi/sbi_hart.h            |  3 +-
 include/sbi/sbi_trap.h            |  9 ++++++
 include/sbi/sbi_trap_ldst.h       |  2 ++
 lib/sbi/objects.mk                |  1 +
 lib/sbi/sbi_double_trap.c         | 26 +++++++++++++++
 lib/sbi/sbi_emulate_csr.c         | 12 ++-----
 lib/sbi/sbi_fwft.c                | 53 ++++++++++++++++++++++++++++++-
 lib/sbi/sbi_hart.c                |  4 +++
 lib/sbi/sbi_sse.c                 |  1 +
 lib/sbi/sbi_trap.c                | 10 +++---
 13 files changed, 136 insertions(+), 20 deletions(-)
 create mode 100644 lib/sbi/sbi_double_trap.c
Signature · 1 line
2.43.0
#1 lore raw

RFC PATCH 1/4 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 5f3b111..4036946 100644
@@ -48,11 +48,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:
@@ -163,11 +159,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 b4f3a17..8bd2183 100644
@@ -103,11 +103,7 @@ int sbi_trap_redirect(struct sbi_trap_regs *regs,
 		      const struct sbi_trap_info *trap)
 {
 	ulong hstatus, vsstatus, prev_mode;
-#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.43.0
#2 lore raw

RFC PATCH 1/4 lib: sbi: factorize previous virtualization mode read from regs

Quoted 5 lines · On Thu, Apr 18, 2024 at 7:25?PM Cl?ment L?ger <[email protected]> wrote:

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

LGTM.

Regards,
Anup

Quoted 81 lines
---
 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 5f3b111..4036946 100644
@@ -48,11 +48,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:
@@ -163,11 +159,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 -6
index b4f3a17..8bd2183 100644
@@ -103,11 +103,7 @@ int sbi_trap_redirect(struct sbi_trap_regs *regs,
                      const struct sbi_trap_info *trap)
 {
        ulong hstatus, vsstatus, prev_mode;
-#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;
 
--

2.43.0

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

#3 lore raw

RFC PATCH 2/4 lib: riscv: add Ssdbltrp 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 a double trap SSE event is delivered.

---
 include/sbi/riscv_encoding.h      |  3 +++
 include/sbi/sbi_ecall_interface.h |  1 +
 include/sbi/sbi_hart.h            |  3 ++-
 include/sbi/sbi_trap_ldst.h       |  2 ++
 lib/sbi/objects.mk                |  1 +
 lib/sbi/sbi_double_trap.c         | 26 ++++++++++++++++++++++++++
 lib/sbi/sbi_hart.c                |  4 ++++
 lib/sbi/sbi_sse.c                 |  1 +
 lib/sbi/sbi_trap.c                |  4 ++++
 9 files changed, 44 insertions(+), 1 deletion(-)
 create mode 100644 lib/sbi/sbi_double_trap.c
include/sbi/riscv_encoding.h +3 -0
index 477fa3a..8628e5a 100644
@@ -32,6 +32,7 @@
 #define MSTATUS_TVM			_UL(0x00100000)
 #define MSTATUS_TW			_UL(0x00200000)
 #define MSTATUS_TSR			_UL(0x00400000)
+#define MSTATUS_SDT			_UL(0x01000000)
 #define MSTATUS32_SD			_UL(0x80000000)
 #if __riscv_xlen == 64
 #define MSTATUS_UXL			_ULL(0x0000000300000000)
@@ -213,6 +214,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_CBZE			(_UL(1) << 7)
 #define ENVCFG_CBCFE			(_UL(1) << 6)
 #define ENVCFG_CBIE_SHIFT		4
@@ -763,6 +765,7 @@
 #define CAUSE_FETCH_PAGE_FAULT		0xc
 #define CAUSE_LOAD_PAGE_FAULT		0xd
 #define CAUSE_STORE_PAGE_FAULT		0xf
+#define CAUSE_DOUBLE_TRAP		0x10
 #define CAUSE_FETCH_GUEST_PAGE_FAULT	0x14
 #define CAUSE_LOAD_GUEST_PAGE_FAULT	0x15
 #define CAUSE_VIRTUAL_INST_FAULT	0x16
include/sbi/sbi_ecall_interface.h +1 -0
index 0a3b77e..fd1f93d 100644
@@ -382,6 +382,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
include/sbi/sbi_hart.h +2 -1
index 10dd90c..a89e6f0 100644
@@ -65,7 +65,8 @@ enum sbi_hart_extensions {
 	SBI_HART_EXT_SSCCFG,
 	/** Hart has Svadu extension */
 	SBI_HART_EXT_SVADU,
-
+	/** 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 211abad..88bcf10 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 +26 -0
new file mode 100644
index 0000000..15311ee
@@ -0,0 +1,26 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2019 Western Digital Corporation or its affiliates.
+ *
+ * Authors:
+ *   Anup Patel <[email protected]>
+ */
+
+#include <sbi/sbi_console.h>
+#include <sbi/sbi_ecall_interface.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);
+
+	/* Exception was taken in VS-mode, redirect it to S-mode */
+	if (prev_virt)
+		return sbi_trap_redirect(regs, trap);
+
+	return sbi_sse_inject_event(SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP);
+}
lib/sbi/sbi_hart.c +4 -0
index 6908d22..8e4b730 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;
@@ -677,6 +680,7 @@ const struct sbi_hart_ext_data sbi_hart_ext[] = {
 	__SBI_HART_EXT_DATA(sscsrind, SBI_HART_EXT_SSCSRIND),
 	__SBI_HART_EXT_DATA(ssccfg, SBI_HART_EXT_SSCCFG),
 	__SBI_HART_EXT_DATA(svadu, SBI_HART_EXT_SVADU),
+	__SBI_HART_EXT_DATA(ssdbltrp, SBI_HART_EXT_SSDBLTRP),
 };
 
 _Static_assert(SBI_HART_EXT_MAX == array_size(sbi_hart_ext),
lib/sbi/sbi_sse.c +1 -0
index f0729f9..daaeb60 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,
lib/sbi/sbi_trap.c +4 -0
index 8bd2183..188f2a5 100644
@@ -330,6 +330,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.43.0
#4 lore raw

RFC PATCH 2/4 lib: riscv: add Ssdbltrp support

Quoted 53 lines · On Thu, Apr 18, 2024 at 7:25?PM Cl?ment L?ger <[email protected]> wrote:

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 a double trap SSE event is delivered.

---
 include/sbi/riscv_encoding.h      |  3 +++
 include/sbi/sbi_ecall_interface.h |  1 +
 include/sbi/sbi_hart.h            |  3 ++-
 include/sbi/sbi_trap_ldst.h       |  2 ++
 lib/sbi/objects.mk                |  1 +
 lib/sbi/sbi_double_trap.c         | 26 ++++++++++++++++++++++++++
 lib/sbi/sbi_hart.c                |  4 ++++
 lib/sbi/sbi_sse.c                 |  1 +
 lib/sbi/sbi_trap.c                |  4 ++++
 9 files changed, 44 insertions(+), 1 deletion(-)
 create mode 100644 lib/sbi/sbi_double_trap.c
include/sbi/riscv_encoding.h +3 -0
index 477fa3a..8628e5a 100644
@@ -32,6 +32,7 @@
 #define MSTATUS_TVM                    _UL(0x00100000)
 #define MSTATUS_TW                     _UL(0x00200000)
 #define MSTATUS_TSR                    _UL(0x00400000)
+#define MSTATUS_SDT                    _UL(0x01000000)
 #define MSTATUS32_SD                   _UL(0x80000000)
 #if __riscv_xlen == 64
 #define MSTATUS_UXL                    _ULL(0x0000000300000000)
@@ -213,6 +214,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_CBZE                    (_UL(1) << 7)
 #define ENVCFG_CBCFE                   (_UL(1) << 6)
 #define ENVCFG_CBIE_SHIFT              4
@@ -763,6 +765,7 @@
 #define CAUSE_FETCH_PAGE_FAULT         0xc
 #define CAUSE_LOAD_PAGE_FAULT          0xd
 #define CAUSE_STORE_PAGE_FAULT         0xf
+#define CAUSE_DOUBLE_TRAP              0x10
 #define CAUSE_FETCH_GUEST_PAGE_FAULT   0x14
 #define CAUSE_LOAD_GUEST_PAGE_FAULT    0x15
 #define CAUSE_VIRTUAL_INST_FAULT       0x16
include/sbi/sbi_ecall_interface.h +0 -0
index 0a3b77e..fd1f93d 100644

include/sbi/sbi_ecall_interface.h enum sbi_sse_state { · line 385

Quoted 4 lines
include/sbi/sbi_ecall_interface.h +1 -0
@@ -382,6 +382,7 @@ enum sbi_sse_state {
 
 /* SBI SSE Event IDs. */
 #define SBI_SSE_EVENT_LOCAL_RAS                        0x00000000
+#define SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP                0x00000001

Please send a patch for SBI spec first.

Quoted 62 lines
include/sbi/sbi_ecall_interface.h +0 -0
 #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
include/sbi/sbi_hart.h +2 -1
index 10dd90c..a89e6f0 100644
@@ -65,7 +65,8 @@ enum sbi_hart_extensions {
        SBI_HART_EXT_SSCCFG,
        /** Hart has Svadu extension */
        SBI_HART_EXT_SVADU,
-
+       /** 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 211abad..88bcf10 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 +16 -0
new file mode 100644
index 0000000..15311ee
@@ -0,0 +1,26 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2019 Western Digital Corporation or its affiliates.
+ *
+ * Authors:
+ *   Anup Patel <[email protected]>
+ */
+
+#include <sbi/sbi_console.h>
+#include <sbi/sbi_ecall_interface.h>
+#include <sbi/sbi_sse.h>
+#include <sbi/sbi_trap.h>
+
+int sbi_double_trap_handler(struct sbi_trap_context *tcntx)
+{

lib/sbi/sbi_double_trap.c line 26

Quoted 10 lines
lib/sbi/sbi_double_trap.c new file +10 -0
+       struct sbi_trap_regs *regs = &tcntx->regs;
+       const struct sbi_trap_info *trap = &tcntx->trap;
+       bool prev_virt = sbi_regs_from_virt(regs);
+
+       /* Exception was taken in VS-mode, redirect it to S-mode */
+       if (prev_virt)
+               return sbi_trap_redirect(regs, trap);
+
+       return sbi_sse_inject_event(SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP);
+}

No need for a separate file, we can have this function directly in sbi_trap.c as a static function.

Quoted 53 lines
lib/sbi/sbi_hart.c +4 -0
index 6908d22..8e4b730 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;
@@ -677,6 +680,7 @@ const struct sbi_hart_ext_data sbi_hart_ext[] = {
        __SBI_HART_EXT_DATA(sscsrind, SBI_HART_EXT_SSCSRIND),
        __SBI_HART_EXT_DATA(ssccfg, SBI_HART_EXT_SSCCFG),
        __SBI_HART_EXT_DATA(svadu, SBI_HART_EXT_SVADU),
+       __SBI_HART_EXT_DATA(ssdbltrp, SBI_HART_EXT_SSDBLTRP),
 };
 
 _Static_assert(SBI_HART_EXT_MAX == array_size(sbi_hart_ext),
lib/sbi/sbi_sse.c +1 -0
index f0729f9..daaeb60 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,
lib/sbi/sbi_trap.c +4 -1
index 8bd2183..188f2a5 100644
@@ -330,6 +330,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";
--

2.43.0

Quoted 4 lines

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

Regards,
Anup

#5 lore raw

RFC PATCH 2/4 lib: riscv: add Ssdbltrp support

Quoted 128 lines · Anup Patel
Quoted 53 lines · On Thu, Apr 18, 2024 at 7:25?PM Cl?ment L?ger <[email protected]> wrote:

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 a double trap SSE event is delivered.

---
 include/sbi/riscv_encoding.h      |  3 +++
 include/sbi/sbi_ecall_interface.h |  1 +
 include/sbi/sbi_hart.h            |  3 ++-
 include/sbi/sbi_trap_ldst.h       |  2 ++
 lib/sbi/objects.mk                |  1 +
 lib/sbi/sbi_double_trap.c         | 26 ++++++++++++++++++++++++++
 lib/sbi/sbi_hart.c                |  4 ++++
 lib/sbi/sbi_sse.c                 |  1 +
 lib/sbi/sbi_trap.c                |  4 ++++
 9 files changed, 44 insertions(+), 1 deletion(-)
 create mode 100644 lib/sbi/sbi_double_trap.c
include/sbi/riscv_encoding.h +3 -0
index 477fa3a..8628e5a 100644
@@ -32,6 +32,7 @@
 #define MSTATUS_TVM                    _UL(0x00100000)
 #define MSTATUS_TW                     _UL(0x00200000)
 #define MSTATUS_TSR                    _UL(0x00400000)
+#define MSTATUS_SDT                    _UL(0x01000000)
 #define MSTATUS32_SD                   _UL(0x80000000)
 #if __riscv_xlen == 64
 #define MSTATUS_UXL                    _ULL(0x0000000300000000)
@@ -213,6 +214,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_CBZE                    (_UL(1) << 7)
 #define ENVCFG_CBCFE                   (_UL(1) << 6)
 #define ENVCFG_CBIE_SHIFT              4
@@ -763,6 +765,7 @@
 #define CAUSE_FETCH_PAGE_FAULT         0xc
 #define CAUSE_LOAD_PAGE_FAULT          0xd
 #define CAUSE_STORE_PAGE_FAULT         0xf
+#define CAUSE_DOUBLE_TRAP              0x10
 #define CAUSE_FETCH_GUEST_PAGE_FAULT   0x14
 #define CAUSE_LOAD_GUEST_PAGE_FAULT    0x15
 #define CAUSE_VIRTUAL_INST_FAULT       0x16
include/sbi/sbi_ecall_interface.h +0 -0
index 0a3b77e..fd1f93d 100644

include/sbi/sbi_ecall_interface.h enum sbi_sse_state { · line 385

Quoted 4 lines
include/sbi/sbi_ecall_interface.h +1 -0
@@ -382,6 +382,7 @@ enum sbi_sse_state {
 
 /* SBI SSE Event IDs. */
 #define SBI_SSE_EVENT_LOCAL_RAS                        0x00000000
+#define SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP                0x00000001

Please send a patch for SBI spec first.

Quoted 67 lines
include/sbi/sbi_ecall_interface.h +0 -0
 #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
include/sbi/sbi_hart.h +2 -1
index 10dd90c..a89e6f0 100644
@@ -65,7 +65,8 @@ enum sbi_hart_extensions {
        SBI_HART_EXT_SSCCFG,
        /** Hart has Svadu extension */
        SBI_HART_EXT_SVADU,
-
+       /** 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 211abad..88bcf10 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 +21 -0
new file mode 100644
index 0000000..15311ee
@@ -0,0 +1,26 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2019 Western Digital Corporation or its affiliates.
+ *
+ * Authors:
+ *   Anup Patel <[email protected]>
+ */
+
+#include <sbi/sbi_console.h>
+#include <sbi/sbi_ecall_interface.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);
+
+       /* Exception was taken in VS-mode, redirect it to S-mode */
Quoted 8 lines

lib/sbi/sbi_double_trap.c line 26

Quoted 5 lines
lib/sbi/sbi_double_trap.c new file +5 -0
+       if (prev_virt)
+               return sbi_trap_redirect(regs, trap);
+
+       return sbi_sse_inject_event(SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP);
+}

No need for a separate file, we can have this function directly in sbi_trap.c as a static function.

Acked. This was actually a PoC for double trap support but I'll fix that when the spec will be released.

Quoted 61 lines
Quoted 54 lines
lib/sbi/sbi_hart.c +4 -0
index 6908d22..8e4b730 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;
@@ -677,6 +680,7 @@ const struct sbi_hart_ext_data sbi_hart_ext[] = {
        __SBI_HART_EXT_DATA(sscsrind, SBI_HART_EXT_SSCSRIND),
        __SBI_HART_EXT_DATA(ssccfg, SBI_HART_EXT_SSCCFG),
        __SBI_HART_EXT_DATA(svadu, SBI_HART_EXT_SVADU),
+       __SBI_HART_EXT_DATA(ssdbltrp, SBI_HART_EXT_SSDBLTRP),
 };
 
 _Static_assert(SBI_HART_EXT_MAX == array_size(sbi_hart_ext),
lib/sbi/sbi_sse.c +1 -0
index f0729f9..daaeb60 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,
lib/sbi/sbi_trap.c +4 -1
index 8bd2183..188f2a5 100644
@@ -330,6 +330,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";
--

2.43.0

Quoted 4 lines

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

Regards,
Anup

#6 lore raw

RFC PATCH 3/4 lib: sbi: implement SBI_FWFT_DOUBLE_TRAP_ENABLE

In reply to Clément Léger

Add support for double trap firmware feature

---
 lib/sbi/sbi_fwft.c | 53 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 52 insertions(+), 1 deletion(-)
lib/sbi/sbi_fwft.c +52 -1
index 78fbd45..965d47a 100644
@@ -34,7 +34,7 @@ static unsigned long fwft_ptr_offset;
 
 #define MIS_DELEG (1UL << CAUSE_MISALIGNED_LOAD | 1UL << CAUSE_MISALIGNED_STORE)
 
-#define SUPPORTED_FEATURE_COUNT	3
+#define SUPPORTED_FEATURE_COUNT	4
 
 struct fwft_config;
 
@@ -149,6 +149,51 @@ static int sbi_get_adue(struct fwft_config *conf, unsigned long *value)
 	return SBI_OK;
 }
 
+#if __riscv_xlen == 32
+# define CSR_MENVCFG_DBLTRP	CSR_MENVCFGH
+# define DBLTRP_DTE	(ENVCFG_DTE >> 32)
+#else
+# define CSR_MENVCFG_DBLTRP	CSR_MENVCFG
+# define DBLTRP_DTE	ENVCFG_DTE
+#endif
+
+static int sbi_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 sbi_set_double_trap_enable(struct fwft_config *conf,
+				      unsigned long value)
+{
+	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+
+	if (!sbi_hart_has_extension(scratch, SBI_HART_EXT_SSDBLTRP))
+		return SBI_ENOTSUPP;
+
+	if (value)
+		csr_set(CSR_MENVCFG_DBLTRP, DBLTRP_DTE);
+	else
+		csr_clear(CSR_MENVCFG_DBLTRP, DBLTRP_DTE);
+
+	return SBI_SUCCESS;
+}
+
+static int sbi_get_double_trap_enable(struct fwft_config *conf,
+				      unsigned long *value)
+{
+	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+
+	if (!sbi_hart_has_extension(scratch, SBI_HART_EXT_SSDBLTRP))
+		return SBI_ENOTSUPP;
+
+	*value = (csr_read(CSR_MENVCFG_DBLTRP) & DBLTRP_DTE) != 0;
+
+	return SBI_SUCCESS;
+}
 
 static struct fwft_config* get_feature_config(enum sbi_fwft_feature_t feature)
 {
@@ -223,6 +268,12 @@ static const struct fwft_feature features[] =
 		.set = sbi_set_misaligned_delegation,
 		.get = sbi_get_misaligned_delegation,
 	},
+	{
+		.id = SBI_FWFT_DOUBLE_TRAP,
+		.supported = sbi_double_trap_supported,
+		.set = sbi_set_double_trap_enable,
+		.get = sbi_get_double_trap_enable,
+	},
 	{
 		.id = SBI_FWFT_USEED,
 		.supported = sbi_useed_supported,
Signature · 1 line
2.43.0
#7 lore raw

RFC PATCH 3/4 lib: sbi: implement SBI_FWFT_DOUBLE_TRAP_ENABLE

Hi Cl?ment,

Quoted 43 lines · Cl?ment L?ger

Add support for double trap firmware feature

---
 lib/sbi/sbi_fwft.c | 53 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 52 insertions(+), 1 deletion(-)
lib/sbi/sbi_fwft.c +20 -1
index 78fbd45..965d47a 100644
@@ -34,7 +34,7 @@ static unsigned long fwft_ptr_offset;
 
 #define MIS_DELEG (1UL << CAUSE_MISALIGNED_LOAD | 1UL << CAUSE_MISALIGNED_STORE)
 
-#define SUPPORTED_FEATURE_COUNT	3
+#define SUPPORTED_FEATURE_COUNT	4
 
 struct fwft_config;
 
@@ -149,6 +149,51 @@ static int sbi_get_adue(struct fwft_config *conf, unsigned long *value)
 	return SBI_OK;
 }
 
+#if __riscv_xlen == 32
+# define CSR_MENVCFG_DBLTRP	CSR_MENVCFGH
+# define DBLTRP_DTE	(ENVCFG_DTE >> 32)
+#else
+# define CSR_MENVCFG_DBLTRP	CSR_MENVCFG
+# define DBLTRP_DTE	ENVCFG_DTE
+#endif
+
+static int sbi_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 sbi_set_double_trap_enable(struct fwft_config *conf,
+				      unsigned long value)

lib/sbi/sbi_fwft.c static int sbi_get_adue(struct fwft_config *conf, unsigned long *value) · line 180

Quoted 10 lines
lib/sbi/sbi_fwft.c +10 -0
+{
+	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+
+	if (!sbi_hart_has_extension(scratch, SBI_HART_EXT_SSDBLTRP))
+		return SBI_ENOTSUPP;
+
+	if (value)
+		csr_set(CSR_MENVCFG_DBLTRP, DBLTRP_DTE);
+	else
+		csr_clear(CSR_MENVCFG_DBLTRP, DBLTRP_DTE);

This configuration will be lost after hart non-retentive suspend. Probably mstatus_init() should only be called during boot and the registers saved/restored across hart suspend. Looking at your branch, the other FWFT features have the same problem.

Regards,
Samuel

Quoted 32 lines
lib/sbi/sbi_fwft.c +22 -0
+
+	return SBI_SUCCESS;
+}
+
+static int sbi_get_double_trap_enable(struct fwft_config *conf,
+				      unsigned long *value)
+{
+	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+
+	if (!sbi_hart_has_extension(scratch, SBI_HART_EXT_SSDBLTRP))
+		return SBI_ENOTSUPP;
+
+	*value = (csr_read(CSR_MENVCFG_DBLTRP) & DBLTRP_DTE) != 0;
+
+	return SBI_SUCCESS;
+}
 
 static struct fwft_config* get_feature_config(enum sbi_fwft_feature_t feature)
 {
@@ -223,6 +268,12 @@ static const struct fwft_feature features[] =
 		.set = sbi_set_misaligned_delegation,
 		.get = sbi_get_misaligned_delegation,
 	},
+	{
+		.id = SBI_FWFT_DOUBLE_TRAP,
+		.supported = sbi_double_trap_supported,
+		.set = sbi_set_double_trap_enable,
+		.get = sbi_get_double_trap_enable,
+	},
 	{
 		.id = SBI_FWFT_USEED,
 		.supported = sbi_useed_supported,
#8 lore raw

RFC PATCH 3/4 lib: sbi: implement SBI_FWFT_DOUBLE_TRAP_ENABLE

Quoted 53 lines · Samuel Holland

Hi Cl?ment,

Quoted 50 lines · Cl?ment L?ger

Add support for double trap firmware feature

---
 lib/sbi/sbi_fwft.c | 53 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 52 insertions(+), 1 deletion(-)
lib/sbi/sbi_fwft.c +27 -1
index 78fbd45..965d47a 100644
@@ -34,7 +34,7 @@ static unsigned long fwft_ptr_offset;
 
 #define MIS_DELEG (1UL << CAUSE_MISALIGNED_LOAD | 1UL << CAUSE_MISALIGNED_STORE)
 
-#define SUPPORTED_FEATURE_COUNT	3
+#define SUPPORTED_FEATURE_COUNT	4
 
 struct fwft_config;
 
@@ -149,6 +149,51 @@ static int sbi_get_adue(struct fwft_config *conf, unsigned long *value)
 	return SBI_OK;
 }
 
+#if __riscv_xlen == 32
+# define CSR_MENVCFG_DBLTRP	CSR_MENVCFGH
+# define DBLTRP_DTE	(ENVCFG_DTE >> 32)
+#else
+# define CSR_MENVCFG_DBLTRP	CSR_MENVCFG
+# define DBLTRP_DTE	ENVCFG_DTE
+#endif
+
+static int sbi_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 sbi_set_double_trap_enable(struct fwft_config *conf,
+				      unsigned long value)
+{
+	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+
+	if (!sbi_hart_has_extension(scratch, SBI_HART_EXT_SSDBLTRP))
+		return SBI_ENOTSUPP;
+
+	if (value)
Quoted 8 lines

lib/sbi/sbi_fwft.c static int sbi_get_adue(struct fwft_config *conf, unsigned long *value) · line 180

Quoted 3 lines
lib/sbi/sbi_fwft.c +3 -0
+		csr_set(CSR_MENVCFG_DBLTRP, DBLTRP_DTE);
+	else
+		csr_clear(CSR_MENVCFG_DBLTRP, DBLTRP_DTE);

This configuration will be lost after hart non-retentive suspend. Probably mstatus_init() should only be called during boot and the registers saved/restored across hart suspend. Looking at your branch, the other FWFT features have the same problem.

Hi Samuel, Yes indeed (And I think you already mentioned that on some other series/meeting, sorry for that). I'll modify it on the fwft branch.

Thanks,

Cl?ment

Quoted 37 lines

Regards,
Samuel

Quoted 32 lines
lib/sbi/sbi_fwft.c +22 -0
+
+	return SBI_SUCCESS;
+}
+
+static int sbi_get_double_trap_enable(struct fwft_config *conf,
+				      unsigned long *value)
+{
+	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+
+	if (!sbi_hart_has_extension(scratch, SBI_HART_EXT_SSDBLTRP))
+		return SBI_ENOTSUPP;
+
+	*value = (csr_read(CSR_MENVCFG_DBLTRP) & DBLTRP_DTE) != 0;
+
+	return SBI_SUCCESS;
+}
 
 static struct fwft_config* get_feature_config(enum sbi_fwft_feature_t feature)
 {
@@ -223,6 +268,12 @@ static const struct fwft_feature features[] =
 		.set = sbi_set_misaligned_delegation,
 		.get = sbi_get_misaligned_delegation,
 	},
+	{
+		.id = SBI_FWFT_DOUBLE_TRAP,
+		.supported = sbi_double_trap_supported,
+		.set = sbi_set_double_trap_enable,
+		.get = sbi_get_double_trap_enable,
+	},
 	{
 		.id = SBI_FWFT_USEED,
 		.supported = sbi_useed_supported,
#9 lore raw

RFC PATCH 3/4 lib: sbi: implement SBI_FWFT_DOUBLE_TRAP_ENABLE

Quoted 65 lines · Cl?ment L?ger
Quoted 53 lines · Samuel Holland

Hi Cl?ment,

Quoted 50 lines · Cl?ment L?ger

Add support for double trap firmware feature

---
 lib/sbi/sbi_fwft.c | 53 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 52 insertions(+), 1 deletion(-)
lib/sbi/sbi_fwft.c +27 -1
index 78fbd45..965d47a 100644
@@ -34,7 +34,7 @@ static unsigned long fwft_ptr_offset;
 
 #define MIS_DELEG (1UL << CAUSE_MISALIGNED_LOAD | 1UL << CAUSE_MISALIGNED_STORE)
 
-#define SUPPORTED_FEATURE_COUNT	3
+#define SUPPORTED_FEATURE_COUNT	4
 
 struct fwft_config;
 
@@ -149,6 +149,51 @@ static int sbi_get_adue(struct fwft_config *conf, unsigned long *value)
 	return SBI_OK;
 }
 
+#if __riscv_xlen == 32
+# define CSR_MENVCFG_DBLTRP	CSR_MENVCFGH
+# define DBLTRP_DTE	(ENVCFG_DTE >> 32)
+#else
+# define CSR_MENVCFG_DBLTRP	CSR_MENVCFG
+# define DBLTRP_DTE	ENVCFG_DTE
+#endif
+
+static int sbi_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 sbi_set_double_trap_enable(struct fwft_config *conf,
+				      unsigned long value)
+{
+	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+
+	if (!sbi_hart_has_extension(scratch, SBI_HART_EXT_SSDBLTRP))
+		return SBI_ENOTSUPP;
+
+	if (value)
Quoted 8 lines

lib/sbi/sbi_fwft.c static int sbi_get_adue(struct fwft_config *conf, unsigned long *value) · line 180

Quoted 3 lines
lib/sbi/sbi_fwft.c +3 -0
+		csr_set(CSR_MENVCFG_DBLTRP, DBLTRP_DTE);
+	else
+		csr_clear(CSR_MENVCFG_DBLTRP, DBLTRP_DTE);

This configuration will be lost after hart non-retentive suspend. Probably mstatus_init() should only be called during boot and the registers saved/restored across hart suspend. Looking at your branch, the other FWFT features have the same problem.

Quoted 2 lines

Hi Samuel, Yes indeed (And I think you already mentioned that on some other series/meeting, sorry for that). I'll modify it on the fwft branch.

I actually believe that we said it was going the responsibility of S-mode software to request the features at suspend/resume time. Typically, on Linux, the delegation request is done using a cpuhotplug callback which will be called each time the CPU is suspended/resumed.

Cl?ment

Quoted 42 lines

Thanks,

Cl?ment

Quoted 37 lines

Regards,
Samuel

Quoted 32 lines
lib/sbi/sbi_fwft.c +22 -0
+
+	return SBI_SUCCESS;
+}
+
+static int sbi_get_double_trap_enable(struct fwft_config *conf,
+				      unsigned long *value)
+{
+	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+
+	if (!sbi_hart_has_extension(scratch, SBI_HART_EXT_SSDBLTRP))
+		return SBI_ENOTSUPP;
+
+	*value = (csr_read(CSR_MENVCFG_DBLTRP) & DBLTRP_DTE) != 0;
+
+	return SBI_SUCCESS;
+}
 
 static struct fwft_config* get_feature_config(enum sbi_fwft_feature_t feature)
 {
@@ -223,6 +268,12 @@ static const struct fwft_feature features[] =
 		.set = sbi_set_misaligned_delegation,
 		.get = sbi_get_misaligned_delegation,
 	},
+	{
+		.id = SBI_FWFT_DOUBLE_TRAP,
+		.supported = sbi_double_trap_supported,
+		.set = sbi_set_double_trap_enable,
+		.get = sbi_get_double_trap_enable,
+	},
 	{
 		.id = SBI_FWFT_USEED,
 		.supported = sbi_useed_supported,
#10 lore raw

RFC PATCH 3/4 lib: sbi: implement SBI_FWFT_DOUBLE_TRAP_ENABLE

Hi Cl?ment,

Quoted 62 lines · Cl?ment L?ger
Quoted 61 lines · Cl?ment L?ger
Quoted 59 lines · Samuel Holland
Quoted 43 lines · Cl?ment L?ger

Add support for double trap firmware feature

---
 lib/sbi/sbi_fwft.c | 53 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 52 insertions(+), 1 deletion(-)
lib/sbi/sbi_fwft.c +20 -1
index 78fbd45..965d47a 100644
@@ -34,7 +34,7 @@ static unsigned long fwft_ptr_offset;
 
 #define MIS_DELEG (1UL << CAUSE_MISALIGNED_LOAD | 1UL << CAUSE_MISALIGNED_STORE)
 
-#define SUPPORTED_FEATURE_COUNT	3
+#define SUPPORTED_FEATURE_COUNT	4
 
 struct fwft_config;
 
@@ -149,6 +149,51 @@ static int sbi_get_adue(struct fwft_config *conf, unsigned long *value)
 	return SBI_OK;
 }
 
+#if __riscv_xlen == 32
+# define CSR_MENVCFG_DBLTRP	CSR_MENVCFGH
+# define DBLTRP_DTE	(ENVCFG_DTE >> 32)
+#else
+# define CSR_MENVCFG_DBLTRP	CSR_MENVCFG
+# define DBLTRP_DTE	ENVCFG_DTE
+#endif
+
+static int sbi_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 sbi_set_double_trap_enable(struct fwft_config *conf,
+				      unsigned long value)

lib/sbi/sbi_fwft.c static int sbi_get_adue(struct fwft_config *conf, unsigned long *value) · line 180

Quoted 10 lines
lib/sbi/sbi_fwft.c +10 -0
+{
+	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+
+	if (!sbi_hart_has_extension(scratch, SBI_HART_EXT_SSDBLTRP))
+		return SBI_ENOTSUPP;
+
+	if (value)
+		csr_set(CSR_MENVCFG_DBLTRP, DBLTRP_DTE);
+	else
+		csr_clear(CSR_MENVCFG_DBLTRP, DBLTRP_DTE);

This configuration will be lost after hart non-retentive suspend. Probably mstatus_init() should only be called during boot and the registers saved/restored across hart suspend. Looking at your branch, the other FWFT features have the same problem.

Quoted 7 lines
Quoted 2 lines

Hi Samuel, Yes indeed (And I think you already mentioned that on some other series/meeting, sorry for that). I'll modify it on the fwft branch.

I actually believe that we said it was going the responsibility of S-mode software to request the features at suspend/resume time. Typically, on Linux, the delegation request is done using a cpuhotplug callback which will be called each time the CPU is suspended/resumed.

It would be S-mode software's responsibility to configure hart-local features after HSM hart stop/start, which corresponds to Linux CPU hotplug. Above I am referring to HSM hart suspend, which corresponds to Linux cpuidle. It doesn't make sense to lose the firmware context in the hart suspend case, especially considering that hart suspend is latency sensitive.

Regards,
Samuel

#11 lore raw

RFC PATCH 3/4 lib: sbi: implement SBI_FWFT_DOUBLE_TRAP_ENABLE

Quoted 73 lines · Samuel Holland

Hi Cl?ment,

Quoted 62 lines · Cl?ment L?ger
Quoted 61 lines · Cl?ment L?ger
Quoted 59 lines · Samuel Holland
Quoted 43 lines · Cl?ment L?ger

Add support for double trap firmware feature

---
 lib/sbi/sbi_fwft.c | 53 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 52 insertions(+), 1 deletion(-)
lib/sbi/sbi_fwft.c +20 -1
index 78fbd45..965d47a 100644
@@ -34,7 +34,7 @@ static unsigned long fwft_ptr_offset;
 
 #define MIS_DELEG (1UL << CAUSE_MISALIGNED_LOAD | 1UL << CAUSE_MISALIGNED_STORE)
 
-#define SUPPORTED_FEATURE_COUNT	3
+#define SUPPORTED_FEATURE_COUNT	4
 
 struct fwft_config;
 
@@ -149,6 +149,51 @@ static int sbi_get_adue(struct fwft_config *conf, unsigned long *value)
 	return SBI_OK;
 }
 
+#if __riscv_xlen == 32
+# define CSR_MENVCFG_DBLTRP	CSR_MENVCFGH
+# define DBLTRP_DTE	(ENVCFG_DTE >> 32)
+#else
+# define CSR_MENVCFG_DBLTRP	CSR_MENVCFG
+# define DBLTRP_DTE	ENVCFG_DTE
+#endif
+
+static int sbi_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 sbi_set_double_trap_enable(struct fwft_config *conf,
+				      unsigned long value)

lib/sbi/sbi_fwft.c static int sbi_get_adue(struct fwft_config *conf, unsigned long *value) · line 180

Quoted 10 lines
lib/sbi/sbi_fwft.c +10 -0
+{
+	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+
+	if (!sbi_hart_has_extension(scratch, SBI_HART_EXT_SSDBLTRP))
+		return SBI_ENOTSUPP;
+
+	if (value)
+		csr_set(CSR_MENVCFG_DBLTRP, DBLTRP_DTE);
+	else
+		csr_clear(CSR_MENVCFG_DBLTRP, DBLTRP_DTE);

This configuration will be lost after hart non-retentive suspend. Probably mstatus_init() should only be called during boot and the registers saved/restored across hart suspend. Looking at your branch, the other FWFT features have the same problem.

Quoted 7 lines
Quoted 2 lines

Hi Samuel, Yes indeed (And I think you already mentioned that on some other series/meeting, sorry for that). I'll modify it on the fwft branch.

I actually believe that we said it was going the responsibility of S-mode software to request the features at suspend/resume time. Typically, on Linux, the delegation request is done using a cpuhotplug callback which will be called each time the CPU is suspended/resumed.

Quoted 5 lines

It would be S-mode software's responsibility to configure hart-local features after HSM hart stop/start, which corresponds to Linux CPU hotplug. Above I am referring to HSM hart suspend, which corresponds to Linux cpuidle. It doesn't make sense to lose the firmware context in the hart suspend case, especially considering that hart suspend is latency sensitive.

Acked, I actually confused myself between suspend/resume & stop/start... Thanks, I'll definitely looked into that.

Cl?ment

Quoted 4 lines

Regards,
Samuel

#12 lore raw

RFC PATCH 4/4 lib: sbi: add support for the Smdbltrp extension

In reply to Clément Léger

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

---
 firmware/fw_base.S           | 29 ++++++++++++++++++++++++++---
 include/sbi/riscv_encoding.h |  2 ++
 2 files changed, 28 insertions(+), 3 deletions(-)
firmware/fw_base.S +26 -3
index 9f995a2..cd71ef4 100644
@@ -31,6 +31,16 @@
 	add	\__d4, \__s4, zero
 .endm
 
+.macro CLEAR_MDT tmp, have_mstatush
+	.if \have_mstatush
+	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
@@ -89,6 +99,13 @@ _bss_zero:
 	lla	s4, _start_hang
 	csrw	CSR_MTVEC, s4
 
+	/* We are now ready to take a trap, clear MDT */
+#if __riscv_xlen > 32
+	CLEAR_MDT t0, 0
+#else
+	CLEAR_MDT t0, 1
+#endif
+
 	/* Setup temporary stack */
 	lla	s4, _fw_end
 	li	s5, (SBI_SCRATCH_SIZE * 2)
@@ -555,6 +572,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, \have_mstatush
 .endm
 
 .macro	TRAP_CALL_C_ROUTINE
@@ -597,15 +617,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
+	/*
+	 * Restore MSTATUS and MEPC CSRs starting with MSTATUS to set MDT
+	 * flags since we can not take a trap now or MEPC would be cloberred
+	 */
 	REG_L	t0, SBI_TRAP_REGS_OFFSET(mstatus)(a0)
 	csrw	CSR_MSTATUS, t0
 	.if \have_mstatush
 	REG_L	t0, SBI_TRAP_REGS_OFFSET(mstatusH)(a0)
 	csrw	CSR_MSTATUSH, t0
 	.endif
+	REG_L	t0, SBI_TRAP_REGS_OFFSET(mepc)(a0)
+	csrw	CSR_MEPC, t0
 .endm
 
 .macro TRAP_RESTORE_A0_T0
include/sbi/riscv_encoding.h +2 -0
index 8628e5a..eed28f2 100644
@@ -42,12 +42,14 @@
 #define MSTATUS_GVA			_ULL(0x0000004000000000)
 #define MSTATUS_GVA_SHIFT		38
 #define MSTATUS_MPV			_ULL(0x0000008000000000)
+#define MSTATUS_MDT			_ULL(0x0000200000000000)
 #else
 #define MSTATUSH_SBE			_UL(0x00000010)
 #define MSTATUSH_MBE			_UL(0x00000020)
 #define MSTATUSH_GVA			_UL(0x00000040)
 #define MSTATUSH_GVA_SHIFT		6
 #define MSTATUSH_MPV			_UL(0x00000080)
+#define MSTATUSH_MDT			_UL(0x00002000)
 #endif
 #define MSTATUS32_SD			_UL(0x80000000)
 #define MSTATUS64_SD			_ULL(0x8000000000000000)
Signature · 1 line
2.43.0
#13 lore raw

RFC PATCH 4/4 lib: sbi: add support for the Smdbltrp extension

Quoted 36 lines · On Thu, Apr 18, 2024 at 7:25?PM Cl?ment L?ger <[email protected]> wrote:

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

---
 firmware/fw_base.S           | 29 ++++++++++++++++++++++++++---
 include/sbi/riscv_encoding.h |  2 ++
 2 files changed, 28 insertions(+), 3 deletions(-)
firmware/fw_base.S +10 -0
index 9f995a2..cd71ef4 100644
@@ -31,6 +31,16 @@
        add     \__d4, \__s4, zero
 .endm
 
+.macro CLEAR_MDT tmp, have_mstatush
+       .if \have_mstatush
+       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

firmware/fw_base.S _bss_zero: · line 108

Quoted 10 lines
firmware/fw_base.S +7 -0
@@ -89,6 +99,13 @@ _bss_zero:
        lla     s4, _start_hang
        csrw    CSR_MTVEC, s4
 
+       /* We are now ready to take a trap, clear MDT */
+#if __riscv_xlen > 32
+       CLEAR_MDT t0, 0
+#else
+       CLEAR_MDT t0, 1
+#endif
+

This should be done just before calling sbi_init() so that we clear MDT even when the CPU is power-down and power-up at runtime.

firmware/fw_base.S memcmp: · line 577

Quoted 10 lines
firmware/fw_base.S +3 -0
        /* Setup temporary stack */
        lla     s4, _fw_end
        li      s5, (SBI_SCRATCH_SIZE * 2)
@@ -555,6 +572,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, \have_mstatush

Better to do this in TRAP_CALL_C_ROUTINE() before entering sbi_trap_handler().

firmware/fw_base.S memcmp: · line 622

Quoted 13 lines
firmware/fw_base.S +3 -3
 .endm
 
 .macro TRAP_CALL_C_ROUTINE
@@ -597,15 +617,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
+       /*
+        * Restore MSTATUS and MEPC CSRs starting with MSTATUS to set MDT
+        * flags since we can not take a trap now or MEPC would be cloberred

s/cloberred/clobbered/

Quoted 35 lines
firmware/fw_base.S +3 -0
+        */
        REG_L   t0, SBI_TRAP_REGS_OFFSET(mstatus)(a0)
        csrw    CSR_MSTATUS, t0
        .if \have_mstatush
        REG_L   t0, SBI_TRAP_REGS_OFFSET(mstatusH)(a0)
        csrw    CSR_MSTATUSH, t0
        .endif
+       REG_L   t0, SBI_TRAP_REGS_OFFSET(mepc)(a0)
+       csrw    CSR_MEPC, t0
 .endm
 
 .macro TRAP_RESTORE_A0_T0
include/sbi/riscv_encoding.h +2 -1
index 8628e5a..eed28f2 100644
@@ -42,12 +42,14 @@
 #define MSTATUS_GVA                    _ULL(0x0000004000000000)
 #define MSTATUS_GVA_SHIFT              38
 #define MSTATUS_MPV                    _ULL(0x0000008000000000)
+#define MSTATUS_MDT                    _ULL(0x0000200000000000)
 #else
 #define MSTATUSH_SBE                   _UL(0x00000010)
 #define MSTATUSH_MBE                   _UL(0x00000020)
 #define MSTATUSH_GVA                   _UL(0x00000040)
 #define MSTATUSH_GVA_SHIFT             6
 #define MSTATUSH_MPV                   _UL(0x00000080)
+#define MSTATUSH_MDT                   _UL(0x00002000)
 #endif
 #define MSTATUS32_SD                   _UL(0x80000000)
 #define MSTATUS64_SD                   _ULL(0x8000000000000000)
--

2.43.0

Quoted 4 lines

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

Regards,
Anup