Skip to content

lib: sbi: add Ssdbltrp and Smdbltrp ISA extensions

Clément Léger opensbi 4 versions – New

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v4 Latest7 patches 0 repliesNew
  2. v37 patches 0 repliesSuperseded
  3. v26 patches 0 repliesSuperseded
Earlier versions · 1
VersionPostedPatchesRepliesStatus
v15 patches0 repliesSuperseded

Changes in v4 from the cover letter

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

Patches in v4 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/7lib: sbi: factorize previous virtualization mode read from regsinclude/sbi/sbi_trap.h lib/sbi/sbi_emulate_csr.c lib/sbi/sbi_trap.c+12 -150 replies
2/7lib: sbi: factorize previous mode computationinclude/sbi/sbi_trap.h lib/sbi/sbi_emulate_csr.c lib/sbi/sbi_illegal_insn.c +3 more+13 -81 reply
2/7lib: sbi: factorize previous mode computation+0 -00 replies
3/7lib: sbi: add Ssdbltrp ISA extension supportinclude/sbi/riscv_encoding.h include/sbi/sbi_hart.h include/sbi/sbi_trap_ldst.h +4 more+46 -10 replies
4/7lib: sbi: send a double trap SSE event to supervisorinclude/sbi/sbi_ecall_interface.h lib/sbi/sbi_double_trap.c lib/sbi/sbi_sse.c+3 -10 replies
5/7lib: sbi: fwft: factorize menvcfg read/writeinclude/sbi/riscv_encoding.h lib/sbi/sbi_fwft.c+46 -570 replies
6/7lib: sbi: implement firmware feature SBI_FWFT_DOUBLE_TRAPinclude/sbi/riscv_encoding.h lib/sbi/sbi_fwft.c+27 -10 replies
7/7lib: sbi: add Smdbltrp ISA extension supportfirmware/fw_base.S include/sbi/riscv_encoding.h+35 -61 reply

Reviews trailers per patch

All 8 patches have a Reviewed-by or Acked-by.

PatchSamuel Holland
1/7R-b
2/7R-b
2/7R-b
3/7R-b
4/7R-b
5/7R-b
6/7R-b
7/7R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v4 thread

Cover letter [PATCH v4 0/7] lib: sbi: add Ssdbltrp and Smdbltrp ISA extensions

A double trap typically arises during a sensitive phase in trap handling operations when an exception or interrupt occurs while the trap handler (the component responsible for managing these events) is in a non-reentrant state. This non-reentrancy usually occurs in the early phase of trap handling, wherein the trap handler has not yet preserved the necessary state to handle and resume from the trap. The occurrence of such event is unlikely but can happen when dealing with hardware errors.

This series adds support for Ssdbltrp and Smdbltrp ratified ISA extensions [1].

Ssdbltrp can be tested using qemu[2], opensbi[3], linux[4] and kvm-unit-tests[5]. Assuming you have a riscv environment available and configured (CROSS_COMPILE), it can be built for riscv64 using the following instructions:

Qemu:

  $ git clone https://github.com/rivosinc/qemu.git
  $ cd qemu
  $ git switch -C dbltrp_v4 dev/cleger/dbltrp_v4
  $ mkdir build && cd build
  $ ../configure --target-list=riscv64-softmmu
  $ make

OpenSBI:

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

Linux:

  $ git clone https://github.com/rivosinc/linux.git
  $ cd linux
  $ git switch -C dbltrp_v1 dev/cleger/dbltrp_v1
  $ export ARCH=riscv
  $ make O=build defconfig
  $ ./script/config --file build/.config --enable RISCV_DBLTRP
  $ make O=build

kvm-unit-tests:

  $ git clone https://github.com/clementleger/kvm-unit-tests.git
  $ cd kvm-unit-tests
  $ git switch -C dbltrp_v1 dev/cleger/dbltrp_v1
  $ ./configure --arch=riscv64 --cross-prefix=$CROSS_COMPILE
  $ make

You will also need kvmtool in your rootfs.

Run with kvm-unit-test test as kernel:

  $ qemu-system-riscv64 \
    -M virt \
    -cpu rv64,ssdbltrp=true,smdbltrp=true \
    -nographic \
    -serial mon:stdio \
    -bios opensbi/build/platform/generic/firmware/fw_jump.bin \
    -kernel kvm-unit-tests-dbltrp/riscv/sbi_dbltrp.flat
  ...
  [OpenSBI boot partially elided]
  Boot HART ISA Extensions  : sscofpmf,sstc,zicntr,zihpm,zicboz,zicbom,sdtrig,svadu,ssdbltrp
  ...
  ##########################################################################
  #    kvm-unit-tests
  ##########################################################################

PASS: sbi: fwft: FWFT extension probing no error
PASS: sbi: fwft: FWFT extension is present
PASS: sbi: fwft: dbltrp: Get double trap enable feature value
PASS: sbi: fwft: dbltrp: Set double trap enable feature value == 0
PASS: sbi: fwft: dbltrp: Get double trap enable feature value == 0
PASS: sbi: fwft: dbltrp: Double trap disabled, trap first time ok
PASS: sbi: fwft: dbltrp: Set double trap enable feature value == 1
PASS: sbi: fwft: dbltrp: Get double trap enable feature value == 1
PASS: sbi: fwft: dbltrp: Trapped twice allowed ok
INFO: sbi: fwft: dbltrp: Should generate a double trap and crash !

sbi_trap_error: hart0: trap0: double trap handler failed (error -10)

sbi_trap_error: hart0: trap0: mcause=0x0000000000000010 mtval=0x0000000000000000
sbi_trap_error: hart0: trap0: mtval2=0x0000000000000003 mtinst=0x0000000000000000
sbi_trap_error: hart0: trap0: mepc=0x00000000802000d8 mstatus=0x8000000a01006900
sbi_trap_error: hart0: trap0: ra=0x00000000802001fc sp=0x0000000080213e70
sbi_trap_error: hart0: trap0: gp=0x0000000000000000 tp=0x0000000080088000
sbi_trap_error: hart0: trap0: s0=0x0000000080213e80 s1=0x0000000000000001
sbi_trap_error: hart0: trap0: a0=0x0000000080213e80 a1=0x0000000080208193
sbi_trap_error: hart0: trap0: a2=0x000000008020dc20 a3=0x000000000000000f
sbi_trap_error: hart0: trap0: a4=0x0000000080210cd8 a5=0x00000000802110d0
sbi_trap_error: hart0: trap0: a6=0x00000000802136e4 a7=0x0000000046574654
sbi_trap_error: hart0: trap0: s2=0x0000000080210cd9 s3=0x0000000000000000
sbi_trap_error: hart0: trap0: s4=0x0000000000000000 s5=0x0000000000000000
sbi_trap_error: hart0: trap0: s6=0x0000000000000000 s7=0x0000000000000001
sbi_trap_error: hart0: trap0: s8=0x0000000000002000 s9=0x0000000080083700
sbi_trap_error: hart0: trap0: s10=0x0000000000000000 s11=0x0000000000000000
sbi_trap_error: hart0: trap0: t0=0x0000000000000000 t1=0x0000000080213ed8
sbi_trap_error: hart0: trap0: t2=0x0000000000001000 t3=0x0000000080213ee0
sbi_trap_error: hart0: trap0: t4=0x0000000000000000 t5=0x000000008020f8d0
sbi_trap_error: hart0: trap0: t6=0x0000000000000000

Run with linux and kvm-unit-test test in kvm (testing VS-mode):

  $ qemu-system-riscv64 \
    -M virt \
    -cpu rv64,ssdbltrp=true,smdbltrp=true \
    -nographic \
    -serial mon:stdio \
    -bios opensbi/build/platform/generic/firmware/fw_jump.bin \
    -kernel linux/build/arch/riscv/boot/Image
  ...
  [Linux boot partially elided]
  [    0.735079] riscv-dbltrp: Double trap handling registered
  ...
  $ lkvm run -k sbi_dbltrp.flat -m 128 -c 2
  ##########################################################################
  #    kvm-unit-tests
  ##########################################################################

PASS: sbi: fwft: FWFT extension probing no error
PASS: sbi: fwft: FWFT extension is present
PASS: sbi: fwft: dbltrp: Get double trap enable feature value
PASS: sbi: fwft: dbltrp: Set double trap enable feature value == 0
PASS: sbi: fwft: dbltrp: Get double trap enable feature value == 0
PASS: sbi: fwft: dbltrp: Double trap disabled, trap first time ok
PASS: sbi: fwft: dbltrp: Set double trap enable feature value == 1
PASS: sbi: fwft: dbltrp: Get double trap enable feature value == 1
PASS: sbi: fwft: dbltrp: Trapped twice allowed ok
INFO: sbi: fwft: dbltrp: Should generate a double trap and crash !

  [   51.939077] Guest double trap
  [   51.939323] kvm [93]: VCPU exit error -95
  [   51.939683] kvm [93]: SEPC=0x802000d8 SSTATUS=0x200004520 HSTATUS=0x200200180
  [   51.939947] kvm [93]: SCAUSE=0x10 STVAL=0x0 HTVAL=0x3 HTINST=0x0
  KVM_RUN failed: Operation not supported
  $

Testing Smbdbltrp can be done using gdb and trigger some trap. For instance, interrupt M-mode firmware at some point, set mstatus.mdt = 1 and corrupt some register to generate a NULL pointer exception.

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

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

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

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

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