lib: sbi: add Ssdbltrp and Smdbltrp ISA extensions
v2 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_v2 $ 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.
---
V2:
- Add check for riscv_len inside CLEAR_MDT macro
- Order fwft entries by id order
- Move menvcfg generic access function to the top of sbi_fwft.c file
- Split SSE event support
- Add an additional MDT clear after MTVEC is set for all harts
Cl?ment L?ger (6):
lib: sbi: factorize previous virtualization mode read from regs
lib: sbi: add Ssdbltrp ISA extension support
lib: sbi: send a double trap SSE event to supervisor
lib: sbi: fwft: factorize menvcfg read/write
lib: sbi: implement firmware feature SBI_FWFT_DOUBLE_TRAP
lib: sbi: add Smdbltrp ISA extension support
firmware/fw_base.S | 33 +++++++++++-- include/sbi/riscv_encoding.h | 8 ++- 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 | 27 +++++++++++ lib/sbi/sbi_emulate_csr.c | 12 +---- lib/sbi/sbi_fwft.c | 81 ++++++++++++++++++++++--------- lib/sbi/sbi_hart.c | 4 ++ lib/sbi/sbi_sse.c | 1 + lib/sbi/sbi_trap.c | 10 ++-- 13 files changed, 149 insertions(+), 43 deletions(-) create mode 100644 lib/sbi/sbi_double_trap.c
Signature · 1 line
2.45.2
PATCH v2 1/6 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 v2 2/6 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 an error is returned to the top trap handler which will panic.
--- include/sbi/riscv_encoding.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 | 27 +++++++++++++++++++++++++++ lib/sbi/sbi_hart.c | 4 ++++ lib/sbi/sbi_trap.c | 4 ++++ 7 files changed, 42 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_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 +27 -0
@@ -0,0 +1,27 @@ +/* +* SPDX-License-Identifier: BSD-2-Clause +* +* Copyright (c) 2024 Rivos Inc. +* +* Authors: +* Cl?ment L?ger <[email protected]> +*/ + +#include <sbi/sbi_console.h> +#include <sbi/sbi_ecall_interface.h> +#include <sbi/sbi_error.h> +#include <sbi/sbi_sse.h> +#include <sbi/sbi_trap.h> + +int sbi_double_trap_handler (struct sbi_trap_context *tcntx) +{ +struct sbi_trap_regs *regs = &tcntx->regs; +const struct sbi_trap_info *trap = &tcntx->trap; +bool prev_virt =sbi_regs_from_virt (regs); + +/* Exception was taken in VS-mode, redirect it to S-mode */ +if (prev_virt) +return sbi_trap_redirect (regs, trap); + +return SBI_ENOTSUPP ; +}
lib/sbi/sbi_hart.c +4 -0
@@ -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_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 v2 3/6 lib: sbi: send a double trap SSE event to supervisor
In reply to Clément LégerIn case the double trap handler is called and the double trap happened in supervisor mode, send a double trap SSE event.
NOTE: this commit depends on the ratification of the new SSE event id for double trap [1].
--- include/sbi/sbi_ecall_interface.h | 1 + lib/sbi/sbi_double_trap.c | 2 +- lib/sbi/sbi_sse.c | 1 + 3 files changed, 3 insertions(+), 1 deletion(-)
include/sbi/sbi_ecall_interface.h +1 -0
@@ -379,6 +379,7 @@ enum sbi_sse_state {/* SBI SSE Event IDs. */ #define SBI_SSE_EVENT_LOCAL_RAS 0x00000000 +#define SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP 0x00000001#define SBI_SSE_EVENT_LOCAL_PLAT_0_START 0x00004000#define SBI_SSE_EVENT_LOCAL_PLAT_0_END 0x00007fff#define SBI_SSE_EVENT_GLOBAL_RAS 0x00008000
lib/sbi/sbi_double_trap.c +1 -1
@@ -23,5 +23,5 @@ int sbi_double_trap_handler(struct sbi_trap_context *tcntx)if (prev_virt)return sbi_trap_redirect (regs, trap); -return SBI_ENOTSUPP ; +return sbi_sse_inject_event (SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP ); }
lib/sbi/sbi_sse.c +1 -0
@@ -41,6 +41,7 @@static const uint32_t supported_events[] = {SBI_SSE_EVENT_LOCAL_RAS , +SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP ,SBI_SSE_EVENT_GLOBAL_RAS ,SBI_SSE_EVENT_LOCAL_PMU ,SBI_SSE_EVENT_LOCAL_SOFTWARE ,
Signature · 1 line
2.45.2
PATCH v2 3/6 lib: sbi: send a double trap SSE event to supervisor
Quoted 31 lines · Cl?ment L?ger
In case the double trap handler is called and the double trap happened in supervisor mode, send a double trap SSE event.
NOTE: this commit depends on the ratification of the new SSE event id for double trap [1].
Signed-off-by Cl?ment L?ger <[email protected]>--- include/sbi/sbi_ecall_interface.h | 1 + lib/sbi/sbi_double_trap.c | 2 +- lib/sbi/sbi_sse.c | 1 + 3 files changed, 3 insertions(+), 1 deletion(-)include/sbi/sbi_ecall_interface.h +1
-0@@ -379,6 +379,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 0x00008000lib/sbi/sbi_double_trap.c +0
-0
lib/sbi/sbi_double_trap.c int sbi_double_trap_handler(struct sbi_trap_context *tcntx) · line 26
Quoted 5 lines
lib/sbi/sbi_double_trap.c +1
-1@@ -23,5 +23,5 @@ int sbi_double_trap_handler(struct sbi_trap_context *tcntx)if (prev_virt)return sbi_trap_redirect (regs, trap); -return SBI_ENOTSUPP ; +return sbi_sse_inject_event (SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP );
(sbi_sse_inject_event() doesn't check anywhere that the trap came from a lower privilege mode, like sbi_trap_redirect() does. But that's fine for double trap, because a double trap from M-mode doesn't go through the normal trap handler.)
Quoted 13 lines
lib/sbi/sbi_double_trap.c +0
-0}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 ,
PATCH v2 3/6 lib: sbi: send a double trap SSE event to supervisor
Quoted 35 lines · Samuel Holland
Quoted 34 lines · Cl?ment L?ger
In case the double trap handler is called and the double trap happened in supervisor mode, send a double trap SSE event.
NOTE: this commit depends on the ratification of the new SSE event id for double trap [1].
Signed-off-by Cl?ment L?ger <[email protected]>--- include/sbi/sbi_ecall_interface.h | 1 + lib/sbi/sbi_double_trap.c | 2 +- lib/sbi/sbi_sse.c | 1 + 3 files changed, 3 insertions(+), 1 deletion(-)include/sbi/sbi_ecall_interface.h +1
-0@@ -379,6 +379,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 0x00008000lib/sbi/sbi_double_trap.c +0
-0@@ -23,5 +23,5 @@ int sbi_double_trap_handler(struct sbi_trap_context *tcntx)if (prev_virt)return sbi_trap_redirect (regs, trap);
Quoted 8 lines
lib/sbi/sbi_double_trap.c int sbi_double_trap_handler(struct sbi_trap_context *tcntx) · line 26
Quoted 2 lines
lib/sbi/sbi_double_trap.c +1
-1-return SBI_ENOTSUPP ; +return sbi_sse_inject_event (SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP );Reviewed-by Samuel Holland <[email protected]>(
sbi_sse_inject_event()doesn't check anywhere that the trap came from a lower privilege mode, likesbi_trap_redirect()does. But that's fine for double trap, because a double trap from M-mode doesn't go through the normal trap handler.)
Maybe this one is ok but you are right, better be safe than sorry ! I'll add a check in the V3.
Thanks !
Cl?ment
Quoted 15 lines
Quoted 14 lines
lib/sbi/sbi_double_trap.c +0
-0}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 ,
PATCH v2 4/6 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 | 62 +++++++++++++++++++++--------------- 2 files changed, 38 insertions(+), 27 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 +36 -26
78 lines not on this page · raw message
Signature · 1 line
2.45.2
PATCH v2 4/6 lib: sbi: fwft: factorize menvcfg read/write
Quoted 8 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 | 62 +++++++++++++++++++++--------------- 2 files changed, 38 insertions(+), 27 deletions(-)
PATCH v2 5/6 lib: sbi: implement firmware feature SBI_FWFT_DOUBLE_TRAP
In reply to Clément LégerAdd support for double trap firmware feature.
--- include/sbi/riscv_encoding.h | 3 ++- lib/sbi/sbi_fwft.c | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-)
include/sbi/riscv_encoding.h +2 -1
@@ -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;
@@ -240,6 +259,12 @@ static const struct fwft_feature features[] = .set = fwft_set_misaligned_delegation, .get = fwft_get_misaligned_delegation, }, + { + .id = SBI_FWFT_DOUBLE_TRAP, + .supported = fwft_double_trap_supported, + .set = fwft_set_double_trap, + .get = fwft_get_double_trap, + }, { .id = SBI_FWFT_PTE_AD_HW_UPDATING, .supported = fwft_adue_supported,
Signature · 1 line
2.45.2
PATCH v2 5/6 lib: sbi: implement firmware feature SBI_FWFT_DOUBLE_TRAP
Quoted 40 lines · Cl?ment L?ger
Add support for double trap firmware feature.
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 +9
-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 ; +} +
lib/sbi/sbi_fwft.c static int fwft_get_adue(struct fwft_config *conf, unsigned long *value) · line 176
Quoted 10 lines
lib/sbi/sbi_fwft.c +10
-0+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 ); +} +
nit: to be sorted, these functions should go above fwft_adue_supported(). But it's a trivial concern, so:
Quoted 16 lines
lib/sbi/sbi_fwft.c +6
-0static struct fwft_config *get_feature_config (enum sbi_fwft_feature_t feature) {int i;@@ -240,6 +259,12 @@ static const struct fwft_feature features[] = .set = fwft_set_misaligned_delegation, .get = fwft_get_misaligned_delegation, }, + { + .id = SBI_FWFT_DOUBLE_TRAP, + .supported = fwft_double_trap_supported, + .set = fwft_set_double_trap, + .get = fwft_get_double_trap, + }, { .id = SBI_FWFT_PTE_AD_HW_UPDATING, .supported = fwft_adue_supported,
PATCH v2 5/6 lib: sbi: implement firmware feature SBI_FWFT_DOUBLE_TRAP
Quoted 46 lines · Samuel Holland
Quoted 45 lines · Cl?ment L?ger
Add support for double trap firmware feature.
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 +14
-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 ); +} +
Quoted 8 lines
lib/sbi/sbi_fwft.c static int fwft_get_adue(struct fwft_config *conf, unsigned long *value) · line 176
Quoted 5 lines
lib/sbi/sbi_fwft.c +5
-0+static int fwft_get_double_trap (struct fwft_config *conf,unsigned long *value) +{ +return fwft_menvcfg_read_bit (value,ENVCFG_DTE_SHIFT ); +} +nit: to be sorted, these functions should go above
fwft_adue_supported(). But it's a trivial concern, so:
Oh damn, I sorted the entries but not the functions. No worries, I'll resend a V3.
Thanks,
Cl?ment
Quoted 20 lines
Reviewed-by Samuel Holland <[email protected]>Quoted 16 lines
lib/sbi/sbi_fwft.c +6
-0static struct fwft_config *get_feature_config (enum sbi_fwft_feature_t feature) {int i;@@ -240,6 +259,12 @@ static const struct fwft_feature features[] = .set = fwft_set_misaligned_delegation, .get = fwft_get_misaligned_delegation, }, + { + .id = SBI_FWFT_DOUBLE_TRAP, + .supported = fwft_double_trap_supported, + .set = fwft_set_double_trap, + .get = fwft_get_double_trap, + }, { .id = SBI_FWFT_PTE_AD_HW_UPDATING, .supported = fwft_adue_supported,
PATCH v2 6/6 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 | 33 ++++++++++++++++++++++++++++++--- include/sbi/riscv_encoding.h | 2 ++ 2 files changed, 32 insertions(+), 3 deletions(-)
firmware/fw_base.S +30 -3
@@ -31,6 +31,16 @@ add \__d4, \__s4, zero .endm +.macro CLEAR_MDT tmp +#if __riscv_xlen == 32 + li \tmp, MSTATUSH_MDT + csrc CSR_MSTATUSH, \tmp +#else + li \tmp, MSTATUS_MDT + csrc CSR_MSTATUS, \tmp +#endif +.endm + .section .entry, "ax", %progbits .align 3 .globl _start
@@ -91,6 +101,14 @@ _bss_zero: lla s4, _start_hang csrw CSR_MTVEC, s4 + /* + * While at this point, trap handling is rudimentary, if a trap happens, + * it will end up in _start_hang which is enough to hook up a GDB. Clear + * MDT to avoid generating a double trap and thus entering a + * critical-error state. + */ + CLEAR_MDT t0 + /* Setup temporary stack */ lla s4, _fw_end li s5, (SBI_SCRATCH_SIZE * 2)
@@ -362,6 +380,9 @@ _start_warm: _skip_trap_handler_hyp: csrw CSR_MTVEC, a4 + /* Clear MDT here again for all harts */ + CLEAR_MDT t0 + /* Initialize SBI runtime */ csrr a0, CSR_MSCRATCH call sbi_init
@@ -557,6 +578,9 @@ memcmp: li t0, 0 .endif REG_S t0, (SBI_TRAP_REGS_SIZE + SBI_TRAP_INFO_OFFSET(gva))(sp) + + /* We are ready to take another trap, clear MDT */ + CLEAR_MDT t0 .endm .macro TRAP_CALL_C_ROUTINE
@@ -599,15 +623,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 v2 6/6 lib: sbi: add Smdbltrp ISA extension support
Quoted 6 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.
Quoted 6 lines
Signed-off-by Cl?ment L?ger <[email protected]>--- firmware/fw_base.S | 33 ++++++++++++++++++++++++++++++--- include/sbi/riscv_encoding.h | 2 ++ 2 files changed, 32 insertions(+), 3 deletions(-)
I didn't see you had sent v2 until after replying to the other thread. This looks good to me!
Quoted 98 lines
firmware/fw_base.S +30
-3@@ -31,6 +31,16 @@ add \__d4, \__s4, zero .endm +.macro CLEAR_MDT tmp +#if __riscv_xlen == 32 + li \tmp, MSTATUSH_MDT + csrc CSR_MSTATUSH, \tmp +#else + li \tmp, MSTATUS_MDT + csrc CSR_MSTATUS, \tmp +#endif +.endm + .section .entry, "ax", %progbits .align 3 .globl _start@@ -91,6 +101,14 @@ _bss_zero: lla s4, _start_hang csrw CSR_MTVEC, s4 + /* + * While at this point, trap handling is rudimentary, if a trap happens, + * it will end up in _start_hang which is enough to hook up a GDB. Clear + * MDT to avoid generating a double trap and thus entering a + * critical-error state. + */ + CLEAR_MDT t0 + /* Setup temporary stack */ lla s4, _fw_end li s5, (SBI_SCRATCH_SIZE * 2)@@ -362,6 +380,9 @@ _start_warm: _skip_trap_handler_hyp: csrw CSR_MTVEC, a4 + /* Clear MDT here again for all harts */ + CLEAR_MDT t0 + /* Initialize SBI runtime */ csrr a0, CSR_MSCRATCH call sbi_init@@ -557,6 +578,9 @@ memcmp: li t0, 0 .endif REG_S t0, (SBI_TRAP_REGS_SIZE + SBI_TRAP_INFO_OFFSET(gva))(sp) + + /* We are ready to take another trap, clear MDT */ + CLEAR_MDT t0 .endm .macro TRAP_CALL_C_ROUTINE@@ -599,15 +623,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)