linux/arch/riscv/kvm
Naohiko Shimizu 75870639bf riscv: kvm: Fix vstimecmp update hazard on RV32
On RV32, updating the 64-bit stimecmp (or vstimecmp) CSR requires two
separate 32-bit writes. A race condition exists if the timer triggers
during these two writes.

The RISC-V Privileged Specification (e.g., Section 3.2.1 for mtimecmp)
recommends a specific 3-step sequence to avoid spurious interrupts
when updating 64-bit comparison registers on 32-bit systems:

1. Set the low-order bits (stimecmp) to all ones (ULONG_MAX).
2. Set the high-order bits (stimecmph) to the desired value.
3. Set the low-order bits (stimecmp) to the desired value.

Current implementation writes the LSB first without ensuring a future
value, which may lead to a transient state where the 64-bit comparison
is incorrectly evaluated as "expired" by the hardware. This results in
spurious timer interrupts.

This patch adopts the spec-recommended 3-step sequence to ensure the
intermediate 64-bit state is never smaller than the current time.

Fixes: 8f5cb44b1b ("RISC-V: KVM: Support sstc extension")
Signed-off-by: Naohiko Shimizu <naohiko.shimizu@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://patch.msgid.link/20260104135938.524-3-naohiko.shimizu@gmail.com
Signed-off-by: Paul Walmsley <pjw@kernel.org>
History 2026-01-14 17:43:07 -07:00
..
aia.c RISC-V: KVM: Move HGEI[E|P] CSR access to IMSIC virtualization 2025-07-11 18:33:27 +05:30
aia_aplic.c RISC-V: KVM: Fix APLIC in_clrip and clripnum write emulation 2024-11-05 13:27:28 +05:30
aia_device.c RISC-V: KVM: Drop the return value of kvm_riscv_vcpu_aia_init() 2025-07-28 22:27:07 +05:30
aia_imsic.c RISC-V: KVM: Introduce KVM_EXIT_FAIL_ENTRY_NO_VSFILE 2025-11-24 09:55:36 +05:30
gstage.c RISC-V: KVM: Prevent HGATP_MODE_BARE passed 2025-09-16 10:54:05 +05:30
Kconfig KVM: Make support for kvm_arch_vcpu_async_ioctl() mandatory 2025-11-05 11:03:10 -08:00
main.c RISC-V: KVM: Flush VS-stage TLB after VCPU migration for Andes cores 2025-11-24 09:55:36 +05:30
Makefile RISC-V: KVM: Add separate source for forwarded SBI extensions 2025-11-24 09:55:36 +05:30
mmu.c KVM: riscv: Support enabling dirty log gradually in small chunks 2025-11-24 09:55:36 +05:30
nacl.c RISC-V: KVM: Add common nested acceleration support 2024-10-28 16:43:57 +05:30
tlb.c RISC-V: KVM: Flush VS-stage TLB after VCPU migration for Andes cores 2025-11-24 09:55:36 +05:30
trace.h RISCV: KVM: add tracepoints for entry and exit events 2024-06-26 18:37:36 +05:30
vcpu.c KVM/riscv changes for 6.19 2025-12-02 18:35:25 +01:00
vcpu_exit.c RISC-V: KVM: Factor-out g-stage page table management 2025-07-28 22:27:30 +05:30
vcpu_fp.c riscv: Rearrange hwcap.h and cpufeature.h 2023-11-09 10:15:51 -08:00
vcpu_insn.c RISC-V: KVM: Fix guest page fault within HLV* instructions 2025-11-24 09:55:36 +05:30
vcpu_onereg.c RISC-V: KVM: Move copy_sbi_ext_reg_indices() to SBI implementation 2025-09-16 10:54:18 +05:30
vcpu_pmu.c RISC-V: KVM: Implement get event info function 2025-09-16 11:49:31 +05:30
vcpu_sbi.c RISC-V: KVM: Add SBI MPXY extension support for Guest 2025-11-24 09:55:36 +05:30
vcpu_sbi_base.c RISC-V: KVM: Add separate source for forwarded SBI extensions 2025-11-24 09:55:36 +05:30
vcpu_sbi_forward.c RISC-V: KVM: Add SBI MPXY extension support for Guest 2025-11-24 09:55:36 +05:30
vcpu_sbi_fwft.c RISC-V: KVM: Implement ONE_REG interface for SBI FWFT state 2025-09-16 10:54:21 +05:30
vcpu_sbi_hsm.c KVM: RISC-V: refactor sbi reset request 2025-05-21 09:34:49 +05:30
vcpu_sbi_pmu.c RISC-V: KVM: Implement get event info function 2025-09-16 11:49:31 +05:30
vcpu_sbi_replace.c RISC-V: KVM: Add separate source for forwarded SBI extensions 2025-11-24 09:55:36 +05:30
vcpu_sbi_sta.c RISC-V: KVM: No need of explicit writable slot check 2025-09-16 11:49:31 +05:30
vcpu_sbi_system.c RISC-V: KVM: Convert kvm_riscv_vcpu_sbi_forward() into extension handler 2025-11-24 09:55:36 +05:30
vcpu_sbi_v01.c RISC-V: KVM: Convert kvm_riscv_vcpu_sbi_forward() into extension handler 2025-11-24 09:55:36 +05:30
vcpu_switch.S RISC-V: KVM: Use SBI sync SRET call when available 2024-10-28 16:44:03 +05:30
vcpu_timer.c riscv: kvm: Fix vstimecmp update hazard on RV32 2026-01-14 17:43:07 -07:00
vcpu_vector.c RISC-V: KVM: fix stack overrun when loading vlenb 2025-08-25 10:26:20 +05:30
vm.c RISC-V: KVM: Factor-out g-stage page table management 2025-07-28 22:27:30 +05:30
vmid.c RISC-V: KVM: Flush VS-stage TLB after VCPU migration for Andes cores 2025-11-24 09:55:36 +05:30