[PATCH 20/27] RISC-V: KVM: Redirect nested WFI and WRS traps
Anup Patel
anup.patel at oss.qualcomm.com
Tue Jan 20 00:00:06 PST 2026
The WFI and WRS virtual instruction traps from Guest VS/VU-mode
(aka L2/nested guest) should be redirected to Guest HS-mode (aka
L1/guest hypervisor).
Signed-off-by: Anup Patel <anup.patel at oss.qualcomm.com>
---
arch/riscv/kvm/vcpu_insn.c | 28 ++++++++++++++++++++++++++++
1 file changed, 28 insertions(+)
diff --git a/arch/riscv/kvm/vcpu_insn.c b/arch/riscv/kvm/vcpu_insn.c
index 745cd654df94..ebd0cfc1bf30 100644
--- a/arch/riscv/kvm/vcpu_insn.c
+++ b/arch/riscv/kvm/vcpu_insn.c
@@ -76,6 +76,20 @@ void kvm_riscv_vcpu_wfi(struct kvm_vcpu *vcpu)
static int wfi_insn(struct kvm_vcpu *vcpu, struct kvm_run *run, ulong insn)
{
+ /*
+ * Trap from virtual-VS/VU modes should be forwarded to
+ * virtual-HS mode as a virtual instruction trap.
+ */
+ if (kvm_riscv_vcpu_nested_virt(vcpu))
+ return KVM_INSN_VIRTUAL_TRAP;
+
+ /*
+ * Trap from virtual-U mode should be forwarded to
+ * virtual-HS mode as illegal instruction trap.
+ */
+ if (!(vcpu->arch.guest_context.hstatus & HSTATUS_SPVP))
+ return KVM_INSN_ILLEGAL_TRAP;
+
vcpu->stat.wfi_exit_stat++;
kvm_riscv_vcpu_wfi(vcpu);
return KVM_INSN_CONTINUE_NEXT_SEPC;
@@ -83,6 +97,20 @@ static int wfi_insn(struct kvm_vcpu *vcpu, struct kvm_run *run, ulong insn)
static int wrs_insn(struct kvm_vcpu *vcpu, struct kvm_run *run, ulong insn)
{
+ /*
+ * Trap from virtual-VS/VU modes should be forwarded to
+ * virtual-HS mode as a virtual instruction trap.
+ */
+ if (kvm_riscv_vcpu_nested_virt(vcpu))
+ return KVM_INSN_VIRTUAL_TRAP;
+
+ /*
+ * Trap from virtual-U mode should be forwarded to
+ * virtual-HS mode as illegal instruction trap.
+ */
+ if (!(vcpu->arch.guest_context.hstatus & HSTATUS_SPVP))
+ return KVM_INSN_ILLEGAL_TRAP;
+
vcpu->stat.wrs_exit_stat++;
kvm_vcpu_on_spin(vcpu, vcpu->arch.guest_context.sstatus & SR_SPP);
return KVM_INSN_CONTINUE_NEXT_SEPC;
--
2.43.0
More information about the linux-riscv
mailing list