lib: sbi: add Ssdbltrp and Smdbltrp ISA extensions
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 dev/cleger/dbltrp_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_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_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_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.
---
Cl?ment L?ger (5):
lib: sbi: factorize previous virtualization mode read from regs
lib: sbi: add Ssdbltrp ISA extension support
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 | 29 ++++++++++-- include/sbi/riscv_encoding.h | 8 +++- include/sbi/sbi_ecall_interface.h | 2 + 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 | 75 ++++++++++++++++++++++--------- lib/sbi/sbi_hart.c | 4 ++ lib/sbi/sbi_sse.c | 1 + lib/sbi/sbi_trap.c | 10 ++--- 13 files changed, 142 insertions(+), 40 deletions(-) create mode 100644 lib/sbi/sbi_double_trap.c
Signature · 1 line
2.45.2
PATCH 1/5 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
@@ -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
@@ -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 :
@@ -158,11 +154,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
@@ -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.45.2
PATCH 1/5 lib: sbi: factorize previous virtualization mode read from regs
Quoted 9 lines · Cl?ment L?ger
The same pattern is used at multiple places to verify in which mode the exception was actually taken. Factorize it.
Signed-off-by Cl?ment L?ger <[email protected]>--- 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(-)
PATCH 2/5 lib: sbi: add Ssdbltrp ISA extension support
In reply to Clément LégerAdd 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 | 2 ++ include/sbi/sbi_ecall_interface.h | 2 ++ 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 +2 -0
@@ -213,6 +213,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 +764,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 +2 -0
@@ -379,7 +379,9 @@ 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_START 0x00004000#define SBI_SSE_EVENT_LOCAL_PLAT_0_END 0x00007fff#define SBI_SSE_EVENT_GLOBAL_RAS 0x00008000#define SBI_SSE_EVENT_GLOBAL_PLAT_0_START 0x00004000
include/sbi/sbi_hart.h +2 -1
@@ -67,7 +67,8 @@ enum sbi_hart_extensions {SBI_HART_EXT_SVADE ,/** 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
@@ -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
@@ -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
@@ -0,0 +1,26 @@ +/* +* 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_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
@@ -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;
@@ -680,6 +683,7 @@ const struct sbi_hart_ext_data sbi_hart_ext[] = {__SBI_HART_EXT_DATA (ssccfg,SBI_HART_EXT_SSCCFG ),__SBI_HART_EXT_DATA (svade,SBI_HART_EXT_SVADE ),__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
@@ -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
@@ -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.45.2
PATCH 2/5 lib: sbi: add Ssdbltrp ISA extension support
Quoted 44 lines · 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.
Signed-off-by Cl?ment L?ger <[email protected]>--- include/sbi/riscv_encoding.h | 2 ++ include/sbi/sbi_ecall_interface.h | 2 ++ 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.cinclude/sbi/riscv_encoding.h +2
-0@@ -213,6 +213,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 +764,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 0x16include/sbi/sbi_ecall_interface.h +0
-0
include/sbi/sbi_ecall_interface.h enum sbi_sse_state { · line 384
Quoted 6 lines
include/sbi/sbi_ecall_interface.h +2
-0@@ -379,7 +379,9 @@ 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_START 0x00004000
This duplicate definition looks like it was added accidentally. With this line removed:
Quoted 121 lines
include/sbi/sbi_ecall_interface.h +0
-0#define SBI_SSE_EVENT_LOCAL_PLAT_0_END 0x00007fff#define SBI_SSE_EVENT_GLOBAL_RAS 0x00008000#define SBI_SSE_EVENT_GLOBAL_PLAT_0_START 0x00004000include/sbi/sbi_hart.h +2
-1@@ -67,7 +67,8 @@ enum sbi_hart_extensions {SBI_HART_EXT_SVADE ,/** 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@@ -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@@ -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.olib/sbi/sbi_double_trap.c new file +26
-0@@ -0,0 +1,26 @@ +/* +* 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_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@@ -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;@@ -680,6 +683,7 @@ const struct sbi_hart_ext_data sbi_hart_ext[] = {__SBI_HART_EXT_DATA (ssccfg,SBI_HART_EXT_SSCCFG ),__SBI_HART_EXT_DATA (svade,SBI_HART_EXT_SVADE ),__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@@ -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@@ -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" ;
PATCH 2/5 lib: sbi: add Ssdbltrp ISA extension support
Quoted 46 lines · Samuel Holland
Quoted 45 lines · 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.
Signed-off-by Cl?ment L?ger <[email protected]>--- include/sbi/riscv_encoding.h | 2 ++ include/sbi/sbi_ecall_interface.h | 2 ++ 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.cinclude/sbi/riscv_encoding.h +2
-0@@ -213,6 +213,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 +764,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 0x16include/sbi/sbi_ecall_interface.h +0
-0@@ -379,7 +379,9 @@ enum sbi_sse_state {
Quoted 8 lines
include/sbi/sbi_ecall_interface.h enum sbi_sse_state { · line 384
Quoted 5 lines
include/sbi/sbi_ecall_interface.h +2
-0/* 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_START 0x00004000This duplicate definition looks like it was added accidentally. With this line removed:
Missed that indeed. Saw aswell that I added SSE event definition in the same commit. I'll probably split that commit and add the SSE event definition + usage in the double trap handler in a separate commit.
Thanks,
Cl?ment
Quoted 125 lines
Reviewed-by Samuel Holland <[email protected]>Quoted 121 lines
include/sbi/sbi_ecall_interface.h +0
-0#define SBI_SSE_EVENT_LOCAL_PLAT_0_END 0x00007fff#define SBI_SSE_EVENT_GLOBAL_RAS 0x00008000#define SBI_SSE_EVENT_GLOBAL_PLAT_0_START 0x00004000include/sbi/sbi_hart.h +2
-1@@ -67,7 +67,8 @@ enum sbi_hart_extensions {SBI_HART_EXT_SVADE ,/** 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@@ -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@@ -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.olib/sbi/sbi_double_trap.c new file +26
-0@@ -0,0 +1,26 @@ +/* +* 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_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@@ -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;@@ -680,6 +683,7 @@ const struct sbi_hart_ext_data sbi_hart_ext[] = {__SBI_HART_EXT_DATA (ssccfg,SBI_HART_EXT_SSCCFG ),__SBI_HART_EXT_DATA (svade,SBI_HART_EXT_SVADE ),__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@@ -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@@ -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" ;
PATCH 3/5 lib: sbi: fwft: factorize menvcfg read/write
In reply to Clément LégerMENVCFG 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 | 3 ++- lib/sbi/sbi_fwft.c | 50 +++++++++++++++++++++--------------- 2 files changed, 32 insertions(+), 21 deletions(-)
include/sbi/riscv_encoding.h +2 -1
@@ -211,7 +211,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_CBZE (_UL(1) << 7)
lib/sbi/sbi_fwft.c +30 -20
70 lines not on this page · raw message
Signature · 1 line
2.45.2
PATCH 3/5 lib: sbi: fwft: factorize menvcfg read/write
Hi Cl?ment,
Quoted 28 lines · 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.
Signed-off-by Cl?ment L?ger <[email protected]>--- include/sbi/riscv_encoding.h | 3 ++- lib/sbi/sbi_fwft.c | 50 +++++++++++++++++++++--------------- 2 files changed, 32 insertions(+), 21 deletions(-)include/sbi/riscv_encoding.h +2
-1@@ -211,7 +211,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_CBZE (_UL(1) << 7)lib/sbi/sbi_fwft.c +0
-0
lib/sbi/sbi_fwft.c static int fwft_adue_supported(struct fwft_config *conf) · line 114
Quoted 5 lines
lib/sbi/sbi_fwft.c +1
-1@@ -111,40 +111,50 @@ static int fwft_adue_supported(struct fwft_config *conf)return SBI_OK ; } -static int fwft_set_adue (struct fwft_config *conf,unsigned long value) +static int fwft_menvcfg_set_bit (unsigned long value,unsigned long bit)
I would suggest moving these helper functions above fwft_misaligned_delegation_supported() so they are not in the middle of the ADUE feature. (And the functions need to be earlier in the file if the functions are sorted by feature ID.) Otherwise this change looks good.
Regards,
Samuel
Quoted 65 lines
lib/sbi/sbi_fwft.c +29
-19{ - if (value == 1) -#if __riscv_xlen ==32 -csr_set (CSR_MENVCFGH ,ENVCFG_ADUE >>32 ); -#else -csr_set (CSR_MENVCFG ,ENVCFG_ADUE ); -#endif - else if (value == 0) -#if __riscv_xlen ==32 -csr_clear (CSR_MENVCFGH ,ENVCFG_ADUE >>32 ); -#else -csr_clear (CSR_MENVCFG ,ENVCFG_ADUE ); -#endif - else +if (value ==1 ) { +if (bit >=32 && __riscv_xlen ==32 ) +csr_set (CSR_MENVCFGH ,_ULL (1 ) << (bit -32 )); +else +csr_set (CSR_MENVCFG ,_ULL (1 ) << bit); + + }else if (value ==0 ) { +if (bit >=32 && __riscv_xlen ==32 ) +csr_clear (CSR_MENVCFGH ,_ULL (1 ) << (bit -32 )); +else +csr_clear (CSR_MENVCFG ,_ULL (1 ) << bit); + }else {return SBI_EINVAL ; + }return SBI_OK ; } -static int fwft_get_adue (struct fwft_config *conf,unsigned long *value) +static int fwft_menvcfg_read_bit (unsigned long *value,unsigned long bit) {unsigned long cfg; -#if __riscv_xlen ==32 - cfg =csr_read (CSR_MENVCFGH ) & (ENVCFG_ADUE >>32 ); -#else - cfg =csr_read (CSR_MENVCFG ) &ENVCFG_ADUE ; -#endif +if (bit >=32 && __riscv_xlen ==32 ) + cfg =csr_read (CSR_MENVCFGH ) & (_ULL (1 ) << (bit -32 )); +else + cfg =csr_read (CSR_MENVCFG ) & (_ULL (1 ) << bit); + *value = cfg !=0 ;return SBI_OK ; } +static int fwft_set_adue (struct fwft_config *conf,unsigned long value) +{ +return fwft_menvcfg_set_bit (value,ENVCFG_ADUE_SHIFT ); +} + +static int fwft_get_adue (struct fwft_config *conf,unsigned long *value) +{ +return fwft_menvcfg_read_bit (value,ENVCFG_ADUE_SHIFT ); +} +static struct fwft_config *get_feature_config (enum sbi_fwft_feature_t feature) {int i;
PATCH 3/5 lib: sbi: fwft: factorize menvcfg read/write
Quoted 34 lines · Samuel Holland
Hi Cl?ment,
Quoted 31 lines · 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.
Signed-off-by Cl?ment L?ger <[email protected]>--- include/sbi/riscv_encoding.h | 3 ++- lib/sbi/sbi_fwft.c | 50 +++++++++++++++++++++--------------- 2 files changed, 32 insertions(+), 21 deletions(-)include/sbi/riscv_encoding.h +2
-1@@ -211,7 +211,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_CBZE (_UL(1) << 7)lib/sbi/sbi_fwft.c +0
-0@@ -111,40 +111,50 @@ static int fwft_adue_supported(struct fwft_config *conf)return SBI_OK ; }
Quoted 7 lines
lib/sbi/sbi_fwft.c static int fwft_adue_supported(struct fwft_config *conf) · line 114
Quoted 2 lines
lib/sbi/sbi_fwft.c +1
-1-static int fwft_set_adue (struct fwft_config *conf,unsigned long value) +static int fwft_menvcfg_set_bit (unsigned long value,unsigned long bit)I would suggest moving these helper functions above
fwft_misaligned_delegation_supported()so they are not in the middle of the ADUE feature. (And the functions need to be earlier in the file if the functions are sorted by feature ID.) Otherwise this change looks good.
Yes sure !
Thanks,
Cl?ment
Quoted 70 lines
Regards,
SamuelQuoted 65 lines
lib/sbi/sbi_fwft.c +29
-19{ - if (value == 1) -#if __riscv_xlen ==32 -csr_set (CSR_MENVCFGH ,ENVCFG_ADUE >>32 ); -#else -csr_set (CSR_MENVCFG ,ENVCFG_ADUE ); -#endif - else if (value == 0) -#if __riscv_xlen ==32 -csr_clear (CSR_MENVCFGH ,ENVCFG_ADUE >>32 ); -#else -csr_clear (CSR_MENVCFG ,ENVCFG_ADUE ); -#endif - else +if (value ==1 ) { +if (bit >=32 && __riscv_xlen ==32 ) +csr_set (CSR_MENVCFGH ,_ULL (1 ) << (bit -32 )); +else +csr_set (CSR_MENVCFG ,_ULL (1 ) << bit); + + }else if (value ==0 ) { +if (bit >=32 && __riscv_xlen ==32 ) +csr_clear (CSR_MENVCFGH ,_ULL (1 ) << (bit -32 )); +else +csr_clear (CSR_MENVCFG ,_ULL (1 ) << bit); + }else {return SBI_EINVAL ; + }return SBI_OK ; } -static int fwft_get_adue (struct fwft_config *conf,unsigned long *value) +static int fwft_menvcfg_read_bit (unsigned long *value,unsigned long bit) {unsigned long cfg; -#if __riscv_xlen ==32 - cfg =csr_read (CSR_MENVCFGH ) & (ENVCFG_ADUE >>32 ); -#else - cfg =csr_read (CSR_MENVCFG ) &ENVCFG_ADUE ; -#endif +if (bit >=32 && __riscv_xlen ==32 ) + cfg =csr_read (CSR_MENVCFGH ) & (_ULL (1 ) << (bit -32 )); +else + cfg =csr_read (CSR_MENVCFG ) & (_ULL (1 ) << bit); + *value = cfg !=0 ;return SBI_OK ; } +static int fwft_set_adue (struct fwft_config *conf,unsigned long value) +{ +return fwft_menvcfg_set_bit (value,ENVCFG_ADUE_SHIFT ); +} + +static int fwft_get_adue (struct fwft_config *conf,unsigned long *value) +{ +return fwft_menvcfg_read_bit (value,ENVCFG_ADUE_SHIFT ); +} +static struct fwft_config *get_feature_config (enum sbi_fwft_feature_t feature) {int i;
PATCH 4/5 lib: sbi: implement firmware feature SBI_FWFT_DOUBLE_TRAP
In reply to Clément LégerAdd support for double trap firmware feature.
NOTE: this commit depends on the ratification of the new SSE event id for double trap [1].
--- 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
@@ -214,7 +214,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_CBZE (_UL(1) << 7)#define ENVCFG_CBCFE (_UL(1) << 6)#define ENVCFG_CBIE_SHIFT 4
lib/sbi/sbi_fwft.c +25 -0
@@ -155,6 +155,25 @@ static int fwft_get_adue(struct fwft_config *conf, unsigned long *value)return fwft_menvcfg_read_bit (value,ENVCFG_ADUE_SHIFT ); } +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 struct fwft_config *get_feature_config (enum sbi_fwft_feature_t feature) {int i;
@@ -246,6 +265,12 @@ static const struct fwft_feature features[] = .set = fwft_set_adue, .get = fwft_get_adue, }, + { + .id = SBI_FWFT_DOUBLE_TRAP, + .supported = fwft_double_trap_supported, + .set = fwft_set_double_trap, + .get = fwft_get_double_trap, + }, };int sbi_fwft_init (struct sbi_scratch *scratch,bool cold_boot)
Signature · 1 line
2.45.2
PATCH 4/5 lib: sbi: implement firmware feature SBI_FWFT_DOUBLE_TRAP
Hi Cl?ment,
Quoted 57 lines · Cl?ment L?ger
Add support for double trap firmware feature.
NOTE: this commit depends on the ratification of the new SSE event id for double trap [1].
Signed-off-by Cl?ment L?ger <[email protected]>--- 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@@ -214,7 +214,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_CBZE (_UL(1) << 7)#define ENVCFG_CBCFE (_UL(1) << 6)#define ENVCFG_CBIE_SHIFT 4lib/sbi/sbi_fwft.c +19
-0@@ -155,6 +155,25 @@ static int fwft_get_adue(struct fwft_config *conf, unsigned long *value)return fwft_menvcfg_read_bit (value,ENVCFG_ADUE_SHIFT ); } +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 struct fwft_config *get_feature_config (enum sbi_fwft_feature_t feature) {int i;
lib/sbi/sbi_fwft.c static const struct fwft_feature features[] = · line 273
Quoted 9 lines
lib/sbi/sbi_fwft.c +6
-0@@ -246,6 +265,12 @@ static const struct fwft_feature features[] = .set = fwft_set_adue, .get = fwft_get_adue, }, + { + .id = SBI_FWFT_DOUBLE_TRAP, + .supported = fwft_double_trap_supported, + .set = fwft_set_double_trap, + .get = fwft_get_double_trap, + },
I would suggest sorting this entry and the above functions by feature ID.
Regards,
Samuel
Quoted 3 lines
lib/sbi/sbi_fwft.c +0
-0};int sbi_fwft_init (struct sbi_scratch *scratch,bool cold_boot)
PATCH 4/5 lib: sbi: implement firmware feature SBI_FWFT_DOUBLE_TRAP
Quoted 63 lines · Samuel Holland
Hi Cl?ment,
Quoted 60 lines · Cl?ment L?ger
Add support for double trap firmware feature.
NOTE: this commit depends on the ratification of the new SSE event id for double trap [1].
Signed-off-by Cl?ment L?ger <[email protected]>--- 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@@ -214,7 +214,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_CBZE (_UL(1) << 7)#define ENVCFG_CBCFE (_UL(1) << 6)#define ENVCFG_CBIE_SHIFT 4lib/sbi/sbi_fwft.c +19
-0@@ -155,6 +155,25 @@ static int fwft_get_adue(struct fwft_config *conf, unsigned long *value)return fwft_menvcfg_read_bit (value,ENVCFG_ADUE_SHIFT ); } +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 struct fwft_config *get_feature_config (enum sbi_fwft_feature_t feature) {int i;@@ -246,6 +265,12 @@ static const struct fwft_feature features[] = .set = fwft_set_adue, .get = fwft_get_adue, },
Quoted 8 lines
lib/sbi/sbi_fwft.c static const struct fwft_feature features[] = · line 273
Quoted 6 lines
lib/sbi/sbi_fwft.c +6
-0+ { + .id = SBI_FWFT_DOUBLE_TRAP, + .supported = fwft_double_trap_supported, + .set = fwft_set_double_trap, + .get = fwft_get_double_trap, + },I would suggest sorting this entry and the above functions by feature ID.
Will do !
Thanks,
Cl?ment
Quoted 8 lines
Regards,
SamuelQuoted 3 lines
lib/sbi/sbi_fwft.c +0
-0};int sbi_fwft_init (struct sbi_scratch *scratch,bool cold_boot)
PATCH 5/5 lib: sbi: add Smdbltrp ISA extension support
In reply to Clément LégerAdd 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 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
@@ -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
@@ -91,6 +101,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)
@@ -557,6 +574,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
@@ -599,15 +619,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
@@ -41,12 +41,14 @@#define MSTATUS_GVA _ULL(0x0000004000000000)#define MSTATUS_GVA_SHIFT 38#define MSTATUS_MPV _ULL(0x0000008000000000) +#define MSTATUS_MDT _ULL(0x0000040000000000)#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(0x00000400)#endif #define MSTATUS32_SD _UL(0x80000000)#define MSTATUS64_SD _ULL(0x8000000000000000)
Signature · 1 line
2.45.2
PATCH 5/5 lib: sbi: add Smdbltrp ISA extension support
Hi Cl?ment,
Quoted 19 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 before restoring MEPC to avoid taking another trap which would clobber it.
Signed-off-by Cl?ment L?ger <[email protected]>--- firmware/fw_base.S | 29 ++++++++++++++++++++++++++--- include/sbi/riscv_encoding.h | 2 ++ 2 files changed, 28 insertions(+), 3 deletions(-)firmware/fw_base.S +0
-0@@ -31,6 +31,16 @@ add \__d4, \__s4, zero
firmware/fw_base.S line 41
Quoted 10 lines
firmware/fw_base.S +8
-0.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
Why not put the __riscv_xlen check inside the macro? Then it doesn't need the have_mstatush parameter.
Quoted 6 lines
firmware/fw_base.S +2
-0+.endm + .section .entry, "ax", %progbits .align 3 .globl _start
firmware/fw_base.S _bss_zero: · line 109
Quoted 9 lines
firmware/fw_base.S +6
-0@@ -91,6 +101,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
I don't think we're really ready to take a trap here; the stack pointer is not even set yet. Also this needs to go after _start_warm so it gets run on all harts. So I think this should go after the other write to CSR_MTVEC, near the call to sbi_init().
Regards,
Samuel
Quoted 55 lines
firmware/fw_base.S +10
-3+ /* Setup temporary stack */ lla s4, _fw_end li s5, (SBI_SCRATCH_SIZE * 2)@@ -557,6 +574,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@@ -599,15 +619,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_T0include/sbi/riscv_encoding.h +2
-0@@ -41,12 +41,14 @@#define MSTATUS_GVA _ULL(0x0000004000000000)#define MSTATUS_GVA_SHIFT 38#define MSTATUS_MPV _ULL(0x0000008000000000) +#define MSTATUS_MDT _ULL(0x0000040000000000)#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(0x00000400)#endif #define MSTATUS32_SD _UL(0x80000000)#define MSTATUS64_SD _ULL(0x8000000000000000)
PATCH 5/5 lib: sbi: add Smdbltrp ISA extension support
Quoted 24 lines · Samuel Holland
Hi Cl?ment,
Quoted 21 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 before restoring MEPC to avoid taking another trap which would clobber it.
Signed-off-by Cl?ment L?ger <[email protected]>--- firmware/fw_base.S | 29 ++++++++++++++++++++++++++--- include/sbi/riscv_encoding.h | 2 ++ 2 files changed, 28 insertions(+), 3 deletions(-)firmware/fw_base.S +0
-0@@ -31,6 +31,16 @@ add \__d4, \__s4, zero .endm
Quoted 11 lines
firmware/fw_base.S line 41
Quoted 8 lines
firmware/fw_base.S +8
-0+.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 + .endifWhy not put the
__riscv_xlencheck inside the macro? Then it doesn't need thehave_mstatushparameter.
The rationale is that this macro is used in a place were have_mstatush has already been computed. It seems easier than adding yet another level of nesting in that function, but I can use __riscv_xlen if you prefer.
Thanks,
Cl?ment
Quoted 81 lines
Quoted 7 lines
firmware/fw_base.S +2
-0+.endm + .section .entry, "ax", %progbits .align 3 .globl _startfirmware/fw_base.S _bss_zero: · line 109
Quoted 9 lines
firmware/fw_base.S +6
-0@@ -91,6 +101,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 +#endifI don't think we're really ready to take a trap here; the stack pointer is not even set yet. Also this needs to go after
_start_warmso it gets run on all harts. So I think this should go after the other write toCSR_MTVEC, near the call tosbi_init().Regards,
SamuelQuoted 55 lines
firmware/fw_base.S +10
-3+ /* Setup temporary stack */ lla s4, _fw_end li s5, (SBI_SCRATCH_SIZE * 2)@@ -557,6 +574,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@@ -599,15 +619,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_T0include/sbi/riscv_encoding.h +2
-0@@ -41,12 +41,14 @@#define MSTATUS_GVA _ULL(0x0000004000000000)#define MSTATUS_GVA_SHIFT 38#define MSTATUS_MPV _ULL(0x0000008000000000) +#define MSTATUS_MDT _ULL(0x0000040000000000)#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(0x00000400)#endif #define MSTATUS32_SD _UL(0x80000000)#define MSTATUS64_SD _ULL(0x8000000000000000)
PATCH 5/5 lib: sbi: add Smdbltrp ISA extension support
Hi Cl?ment,
Quoted 35 lines · Cl?ment L?ger
Quoted 22 lines · Samuel Holland
Quoted 21 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 before restoring MEPC to avoid taking another trap which would clobber it.
Signed-off-by Cl?ment L?ger <[email protected]>--- firmware/fw_base.S | 29 ++++++++++++++++++++++++++--- include/sbi/riscv_encoding.h | 2 ++ 2 files changed, 28 insertions(+), 3 deletions(-)firmware/fw_base.S +0
-0@@ -31,6 +31,16 @@ add \__d4, \__s4, zero .endmQuoted 11 lines
firmware/fw_base.S line 41
Quoted 8 lines
firmware/fw_base.S +8
-0+.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 + .endifWhy not put the
__riscv_xlencheck inside the macro? Then it doesn't need thehave_mstatushparameter.
Quoted 3 lines
The rationale is that this macro is used in a place were
have_mstatushhas already been computed. It seems easier than adding yet another level of nesting in that function, but I can use__riscv_xlenif you prefer.
True, I suppose either way is fine for this patch. I could send a patch later to remove have_mstatush everywhere, if folks agree. It just seems weird to me to use an #ifdef to set a macro argument, when the underlying condition could be checked inside the macro--it could even be written as ".if __riscv_xlen == 32" if we want to avoid mixing macros and preprocessor conditions.
Regards,
Samuel
Quoted 86 lines
Thanks,
Cl?ment
Quoted 81 lines
Quoted 7 lines
firmware/fw_base.S +2
-0+.endm + .section .entry, "ax", %progbits .align 3 .globl _startfirmware/fw_base.S _bss_zero: · line 109
Quoted 9 lines
firmware/fw_base.S +6
-0@@ -91,6 +101,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 +#endifI don't think we're really ready to take a trap here; the stack pointer is not even set yet. Also this needs to go after
_start_warmso it gets run on all harts. So I think this should go after the other write toCSR_MTVEC, near the call tosbi_init().Regards,
SamuelQuoted 55 lines
firmware/fw_base.S +10
-3+ /* Setup temporary stack */ lla s4, _fw_end li s5, (SBI_SCRATCH_SIZE * 2)@@ -557,6 +574,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@@ -599,15 +619,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_T0include/sbi/riscv_encoding.h +2
-0@@ -41,12 +41,14 @@#define MSTATUS_GVA _ULL(0x0000004000000000)#define MSTATUS_GVA_SHIFT 38#define MSTATUS_MPV _ULL(0x0000008000000000) +#define MSTATUS_MDT _ULL(0x0000040000000000)#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(0x00000400)#endif #define MSTATUS32_SD _UL(0x80000000)#define MSTATUS64_SD _ULL(0x8000000000000000)
PATCH 5/5 lib: sbi: add Smdbltrp ISA extension support
Quoted 42 lines · Samuel Holland
Hi Cl?ment,
Quoted 35 lines · Cl?ment L?ger
Quoted 22 lines · Samuel Holland
Quoted 21 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 before restoring MEPC to avoid taking another trap which would clobber it.
Signed-off-by Cl?ment L?ger <[email protected]>--- firmware/fw_base.S | 29 ++++++++++++++++++++++++++--- include/sbi/riscv_encoding.h | 2 ++ 2 files changed, 28 insertions(+), 3 deletions(-)firmware/fw_base.S +0
-0@@ -31,6 +31,16 @@ add \__d4, \__s4, zero .endmQuoted 11 lines
firmware/fw_base.S line 41
Quoted 8 lines
firmware/fw_base.S +8
-0+.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 + .endifWhy not put the
__riscv_xlencheck inside the macro? Then it doesn't need thehave_mstatushparameter.Quoted 3 lines
The rationale is that this macro is used in a place were
have_mstatushhas already been computed. It seems easier than adding yet another level of nesting in that function, but I can use__riscv_xlenif you prefer.
Quoted 5 lines
True, I suppose either way is fine for this patch. I could send a patch later to remove
have_mstatusheverywhere, if folks agree. It just seems weird to me to use an #ifdef to set a macro argument, when the underlying condition could be checked inside the macro--it could even be written as ".if__riscv_xlen== 32" if we want to avoid mixing macros and preprocessor conditions.
You were actually right ! Having a check on riscv_xlen == 32 in the CLEAR_MDT macro is actually cleaner than this have_mstatus_h.
Thanks,
Cl?ment
Quoted 91 lines
Regards,
SamuelQuoted 86 lines
Thanks,
Cl?ment
Quoted 81 lines
Quoted 7 lines
firmware/fw_base.S +2
-0+.endm + .section .entry, "ax", %progbits .align 3 .globl _startfirmware/fw_base.S _bss_zero: · line 109
Quoted 9 lines
firmware/fw_base.S +6
-0@@ -91,6 +101,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 +#endifI don't think we're really ready to take a trap here; the stack pointer is not even set yet. Also this needs to go after
_start_warmso it gets run on all harts. So I think this should go after the other write toCSR_MTVEC, near the call tosbi_init().Regards,
SamuelQuoted 55 lines
firmware/fw_base.S +10
-3+ /* Setup temporary stack */ lla s4, _fw_end li s5, (SBI_SCRATCH_SIZE * 2)@@ -557,6 +574,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@@ -599,15 +619,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_T0include/sbi/riscv_encoding.h +2
-0@@ -41,12 +41,14 @@#define MSTATUS_GVA _ULL(0x0000004000000000)#define MSTATUS_GVA_SHIFT 38#define MSTATUS_MPV _ULL(0x0000008000000000) +#define MSTATUS_MDT _ULL(0x0000040000000000)#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(0x00000400)#endif #define MSTATUS32_SD _UL(0x80000000)#define MSTATUS64_SD _ULL(0x8000000000000000)
PATCH 5/5 lib: sbi: add Smdbltrp ISA extension support
In reply to Samuel HollandQuoted 45 lines · Samuel Holland
Hi Cl?ment,
Quoted 19 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 before restoring MEPC to avoid taking another trap which would clobber it.
Signed-off-by Cl?ment L?ger <[email protected]>--- firmware/fw_base.S | 29 ++++++++++++++++++++++++++--- include/sbi/riscv_encoding.h | 2 ++ 2 files changed, 28 insertions(+), 3 deletions(-)firmware/fw_base.S +0
-0@@ -31,6 +31,16 @@ add \__d4, \__s4, zerofirmware/fw_base.S line 41
Quoted 10 lines
firmware/fw_base.S +8
-0.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 + .endifWhy not put the
__riscv_xlencheck inside the macro? Then it doesn't need thehave_mstatushparameter.Quoted 9 lines
firmware/fw_base.S +2
-0+.endm + .section .entry, "ax", %progbits .align 3 .globl _start@@ -91,6 +101,13 @@ _bss_zero: lla s4, _start_hang csrw CSR_MTVEC, s4
Quoted 11 lines
firmware/fw_base.S _bss_zero: · line 109
Quoted 6 lines
firmware/fw_base.S +6
-0+ /* We are now ready to take a trap, clear MDT */ +#if __riscv_xlen > 32 + CLEAR_MDT t0, 0 +#else + CLEAR_MDT t0, 1 +#endifI don't think we're really ready to take a trap here; the stack pointer is not even set yet. Also this needs to go after
_start_warmso it gets run on all harts. So I think this should go after the other write toCSR_MTVEC, near the call tosbi_init().
Actually, if we don't clear MDT, if a trap happens, it will trigger a M-mode double trap. Since a temporary trap handler is set, we can let the trap happen without generating a M-mode double trap (which would end up in either a cpu_abort() (at least in qemu) or a NMI interrupt. I'm not sure which one is better though (either hang in the temporary trap handler or cpu abort.
Thanks,
Cl?ment
Quoted 60 lines
Regards,
SamuelQuoted 55 lines
firmware/fw_base.S +10
-3+ /* Setup temporary stack */ lla s4, _fw_end li s5, (SBI_SCRATCH_SIZE * 2)@@ -557,6 +574,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@@ -599,15 +619,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_T0include/sbi/riscv_encoding.h +2
-0@@ -41,12 +41,14 @@#define MSTATUS_GVA _ULL(0x0000004000000000)#define MSTATUS_GVA_SHIFT 38#define MSTATUS_MPV _ULL(0x0000008000000000) +#define MSTATUS_MDT _ULL(0x0000040000000000)#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(0x00000400)#endif #define MSTATUS32_SD _UL(0x80000000)#define MSTATUS64_SD _ULL(0x8000000000000000)
PATCH 5/5 lib: sbi: add Smdbltrp ISA extension support
Hi Cl?ment,
Quoted 56 lines · Cl?ment L?ger
Quoted 43 lines · Samuel Holland
Quoted 19 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 before restoring MEPC to avoid taking another trap which would clobber it.
Signed-off-by Cl?ment L?ger <[email protected]>--- firmware/fw_base.S | 29 ++++++++++++++++++++++++++--- include/sbi/riscv_encoding.h | 2 ++ 2 files changed, 28 insertions(+), 3 deletions(-)firmware/fw_base.S +0
-0@@ -31,6 +31,16 @@ add \__d4, \__s4, zerofirmware/fw_base.S line 41
Quoted 10 lines
firmware/fw_base.S +8
-0.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 + .endifWhy not put the
__riscv_xlencheck inside the macro? Then it doesn't need thehave_mstatushparameter.Quoted 9 lines
firmware/fw_base.S +2
-0+.endm + .section .entry, "ax", %progbits .align 3 .globl _start@@ -91,6 +101,13 @@ _bss_zero: lla s4, _start_hang csrw CSR_MTVEC, s4Quoted 11 lines
firmware/fw_base.S _bss_zero: · line 109
Quoted 6 lines
firmware/fw_base.S +6
-0+ /* We are now ready to take a trap, clear MDT */ +#if __riscv_xlen > 32 + CLEAR_MDT t0, 0 +#else + CLEAR_MDT t0, 1 +#endifI don't think we're really ready to take a trap here; the stack pointer is not even set yet. Also this needs to go after
_start_warmso it gets run on all harts. So I think this should go after the other write toCSR_MTVEC, near the call tosbi_init().
Quoted 6 lines
Actually, if we don't clear MDT, if a trap happens, it will trigger a M-mode double trap. Since a temporary trap handler is set, we can let the trap happen without generating a M-mode double trap (which would end up in either a
cpu_abort()(at least in qemu) or a NMI interrupt. I'm not sure which one is better though (either hang in the temporary trap handler or cpu abort.
Ah, you're right, the temporary trap handler doesn't use the stack, so this is OK for the boot hart. There is still a problem for other harts, or the boot hart after non-retentive suspend, which would jump to _start_warm and miss this code.
Regards,
Samuel
PATCH 5/5 lib: sbi: add Smdbltrp ISA extension support
Quoted 66 lines · Samuel Holland
Hi Cl?ment,
Quoted 56 lines · Cl?ment L?ger
Quoted 43 lines · Samuel Holland
Quoted 19 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 before restoring MEPC to avoid taking another trap which would clobber it.
Signed-off-by Cl?ment L?ger <[email protected]>--- firmware/fw_base.S | 29 ++++++++++++++++++++++++++--- include/sbi/riscv_encoding.h | 2 ++ 2 files changed, 28 insertions(+), 3 deletions(-)firmware/fw_base.S +0
-0@@ -31,6 +31,16 @@ add \__d4, \__s4, zerofirmware/fw_base.S line 41
Quoted 10 lines
firmware/fw_base.S +8
-0.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 + .endifWhy not put the
__riscv_xlencheck inside the macro? Then it doesn't need thehave_mstatushparameter.Quoted 9 lines
firmware/fw_base.S +2
-0+.endm + .section .entry, "ax", %progbits .align 3 .globl _start@@ -91,6 +101,13 @@ _bss_zero: lla s4, _start_hang csrw CSR_MTVEC, s4Quoted 11 lines
firmware/fw_base.S _bss_zero: · line 109
Quoted 6 lines
firmware/fw_base.S +6
-0+ /* We are now ready to take a trap, clear MDT */ +#if __riscv_xlen > 32 + CLEAR_MDT t0, 0 +#else + CLEAR_MDT t0, 1 +#endifI don't think we're really ready to take a trap here; the stack pointer is not even set yet. Also this needs to go after
_start_warmso it gets run on all harts. So I think this should go after the other write toCSR_MTVEC, near the call tosbi_init().Quoted 6 lines
Actually, if we don't clear MDT, if a trap happens, it will trigger a M-mode double trap. Since a temporary trap handler is set, we can let the trap happen without generating a M-mode double trap (which would end up in either a
cpu_abort()(at least in qemu) or a NMI interrupt. I'm not sure which one is better though (either hang in the temporary trap handler or cpu abort.
Quoted 3 lines
Ah, you're right, the temporary trap handler doesn't use the stack, so this is OK for the boot hart. There is still a problem for other harts, or the boot hart after non-retentive suspend, which would jump to
_start_warmand miss this code.
Yes totally and thanks for catching it. I added a call to CLEAR_MDT right after the second time MTVEC is written as you proposed.
Thanks,
Cl?ment
Quoted 4 lines
Regards,
Samuel