From 164c13f0effa55d8b4c87c17d8c49de3b4ede134 Mon Sep 17 00:00:00 2001 From: Jose Martins Date: Fri, 17 Nov 2023 17:57:53 +0000 Subject: [PATCH] ref(riscv): centralize check for sstc extension Signed-off-by: Jose Martins --- src/arch/riscv/vm.c | 3 --- src/arch/riscv/vmm.c | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/arch/riscv/vm.c b/src/arch/riscv/vm.c index e29b46316..caa35491d 100644 --- a/src/arch/riscv/vm.c +++ b/src/arch/riscv/vm.c @@ -44,9 +44,6 @@ void vcpu_arch_reset(struct vcpu* vcpu, vaddr_t entry) if (CPU_HAS_EXTENSION(CPU_EXT_SSTC)) { CSRW(CSR_STIMECMP, -1); - CSRS(CSR_HENVCFG, HENVCFG_STCE); - } else { - CSRC(CSR_HENVCFG, HENVCFG_STCE); } CSRW(CSR_HCOUNTEREN, HCOUNTEREN_TM); CSRW(CSR_HTIMEDELTA, 0); diff --git a/src/arch/riscv/vmm.c b/src/arch/riscv/vmm.c index 45826ee04..243176f94 100644 --- a/src/arch/riscv/vmm.c +++ b/src/arch/riscv/vmm.c @@ -21,6 +21,21 @@ void vmm_arch_init() CSRW(CSR_HIDELEG, HIDELEG_VSSI | HIDELEG_VSTI | HIDELEG_VSEI); CSRW(CSR_HEDELEG, HEDELEG_ECU | HEDELEG_IPF | HEDELEG_LPF | HEDELEG_SPF); + /** + * Enable and sanity check presence of Sstc extension if the hypervisor was + * configured to use it (via the CPU_EXT_SSTC macro). Otherwise, make sure + * it is disabled. + */ + if (CPU_HAS_EXTENSION(CPU_EXT_SSTC)) { + CSRS(CSR_HENVCFG, HENVCFG_STCE); + bool sttc_present = (CSRR(CSR_HENVCFG) & HENVCFG_STCE) != 0; + if (cpu_is_master() && !sttc_present) { + ERROR("Platform configured to use Sstc extension, but extension not present."); + } + } else { + CSRC(CSR_HENVCFG, HENVCFG_STCE); + } + /** * TODO: consider delegating other exceptions e.g. breakpoint or ins misaligned */