Skip to contentSkip to conversation

RISC-V: KVM: Delegate illegal instruction fault to VS mode

PATCH Superseded Xu Lu 2 participants 2 messages Jul 11, 2025 – Jul 14, 2025

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Xu Lu

author
#0 lore raw

PATCH v3 1/1 RISC-V: KVM: Delegate illegal instruction fault to VS mode

Delegate illegal instruction fault to VS mode in default to avoid such exceptions being trapped to HS and redirected back to VS.

The delegation of illegal instruction fault is particularly important to guest applications that use vector instructions frequently. In such cases, an illegal instruction fault will be raised when guest user thread uses vector instruction the first time and then guest kernel will enable user thread to execute following vector instructions.

The fw pmu event counter remains undeleted so that guest can still query illegal instruction events via sbi call. Guest will only see zero count on illegal instruction faults and know 'firmware' has delegated it.

---
 arch/riscv/include/asm/kvm_host.h | 1 +
 arch/riscv/kvm/vcpu_exit.c        | 5 -----
 2 files changed, 1 insertion(+), 5 deletions(-)
arch/riscv/include/asm/kvm_host.h +1 -0
index 85cfebc32e4cf..3f6b9270f366a 100644
@@ -44,6 +44,7 @@
 #define KVM_REQ_STEAL_UPDATE		KVM_ARCH_REQ(6)
 
 #define KVM_HEDELEG_DEFAULT		(BIT(EXC_INST_MISALIGNED) | \
+					 BIT(EXC_INST_ILLEGAL)     | \
 					 BIT(EXC_BREAKPOINT)      | \
 					 BIT(EXC_SYSCALL)         | \
 					 BIT(EXC_INST_PAGE_FAULT) | \
arch/riscv/kvm/vcpu_exit.c +0 -5
index 6e0c184127956..cd8fa68f3642c 100644
@@ -193,11 +193,6 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,
 	ret = -EFAULT;
 	run->exit_reason = KVM_EXIT_UNKNOWN;
 	switch (trap->scause) {
-	case EXC_INST_ILLEGAL:
-		kvm_riscv_vcpu_pmu_incr_fw(vcpu, SBI_PMU_FW_ILLEGAL_INSN);
-		vcpu->stat.instr_illegal_exits++;
-		ret = vcpu_redirect(vcpu, trap);
-		break;
 	case EXC_LOAD_MISALIGNED:
 		kvm_riscv_vcpu_pmu_incr_fw(vcpu, SBI_PMU_FW_MISALIGNED_LOAD);
 		vcpu->stat.load_misaligned_exits++;
Signature · 1 line
2.20.1
maintainer
#1 lore raw
Quoted 2 lines · Xu Lu

Delegate illegal instruction fault to VS mode in default to avoid such

s/in default/by default/

Quoted 35 lines

exceptions being trapped to HS and redirected back to VS.

The delegation of illegal instruction fault is particularly important to guest applications that use vector instructions frequently. In such cases, an illegal instruction fault will be raised when guest user thread uses vector instruction the first time and then guest kernel will enable user thread to execute following vector instructions.

The fw pmu event counter remains undeleted so that guest can still query illegal instruction events via sbi call. Guest will only see zero count on illegal instruction faults and know 'firmware' has delegated it.

---
 arch/riscv/include/asm/kvm_host.h | 1 +
 arch/riscv/kvm/vcpu_exit.c        | 5 -----
 2 files changed, 1 insertion(+), 5 deletions(-)
arch/riscv/include/asm/kvm_host.h +1 -0
index 85cfebc32e4cf..3f6b9270f366a 100644
@@ -44,6 +44,7 @@
 #define KVM_REQ_STEAL_UPDATE           KVM_ARCH_REQ(6)
 
 #define KVM_HEDELEG_DEFAULT            (BIT(EXC_INST_MISALIGNED) | \
+                                        BIT(EXC_INST_ILLEGAL)     | \
                                         BIT(EXC_BREAKPOINT)      | \
                                         BIT(EXC_SYSCALL)         | \
                                         BIT(EXC_INST_PAGE_FAULT) | \
arch/riscv/kvm/vcpu_exit.c +0 -0
index 6e0c184127956..cd8fa68f3642c 100644

arch/riscv/kvm/vcpu_exit.c int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run, · line 200

Quoted 8 lines
arch/riscv/kvm/vcpu_exit.c +0 -5
@@ -193,11 +193,6 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,
        ret = -EFAULT;
        run->exit_reason = KVM_EXIT_UNKNOWN;
        switch (trap->scause) {
-       case EXC_INST_ILLEGAL:
-               kvm_riscv_vcpu_pmu_incr_fw(vcpu, SBI_PMU_FW_ILLEGAL_INSN);
-               vcpu->stat.instr_illegal_exits++;
-               ret = vcpu_redirect(vcpu, trap);
-               break;

Lets keep the illegal instruction trap handling so that if SBI implementation forwards VS-mode illegal instruction trap to HS-mode ignoring hedeleg due to unknown reasons then we still forward this trap to Guest otherwise such an illegal instruction trap will cause exit to user-space and eventually kill the Guest.

Quoted 6 lines
arch/riscv/kvm/vcpu_exit.c +0 -1
        case EXC_LOAD_MISALIGNED:
                kvm_riscv_vcpu_pmu_incr_fw(vcpu, SBI_PMU_FW_MISALIGNED_LOAD);
                vcpu->stat.load_misaligned_exits++;
--

2.20.1

Regards,
Anup