RISC-V: KVM: Disable all hpmcounter access for VS/VU mode
authorAtish Patra <atishp@rivosinc.com>
Tue, 7 Feb 2023 09:55:25 +0000 (01:55 -0800)
committerAnup Patel <anup@brainfault.org>
Tue, 7 Feb 2023 15:05:58 +0000 (20:35 +0530)
Any guest must not get access to any hpmcounter including cycle/instret
without any checks. We achieve that by disabling all the bits except TM
bit in hcounteren.

However, instret and cycle access for guest user space can be enabled
upon explicit request (via ONE REG) or on first trap from VU mode
to maintain ABI requirement in the future. This patch doesn't support
that as ONE REG interface is not settled yet.

Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Signed-off-by: Atish Patra <atishp@rivosinc.com>
Signed-off-by: Anup Patel <anup@brainfault.org>
arch/riscv/kvm/main.c

index 58c5489d3031a4f7f042628d2b01811ea99fb74e..c5d400f36f8de3123b13b2a83a64794dcf70bbcf 100644 (file)
@@ -49,7 +49,8 @@ int kvm_arch_hardware_enable(void)
        hideleg |= (1UL << IRQ_VS_EXT);
        csr_write(CSR_HIDELEG, hideleg);
 
-       csr_write(CSR_HCOUNTEREN, -1UL);
+       /* VS should access only the time counter directly. Everything else should trap */
+       csr_write(CSR_HCOUNTEREN, 0x02);
 
        csr_write(CSR_HVIP, 0);