RISC-V: KVM: Handle software-check exits for VCPU

The extension Zicfiss/Zicfilp introduces a software-check exception
to the privilege architecture. Redirect this exception to VS mode
when VCPU enable Zicfiss/Zicfilp so the CFI violations in VU/VS could
be reported.

Also, update kvm_riscv_vcpu_trap_redirect() to forward the SPELP
state to the VS mode.

Assisted-by: YuanSheng:claude-4.7-opus
Co-developed-by: Quan Zhou <zhouquan@iscas.ac.cn>
Signed-off-by: Quan Zhou <zhouquan@iscas.ac.cn>
Signed-off-by: Inochi Amaoto <inochiama@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260713010302.303278-6-inochiama@gmail.com
Signed-off-by: Anup Patel <anup@brainfault.org>
This commit is contained in:
Inochi Amaoto 2026-07-13 09:02:58 +08:00 committed by Anup Patel
parent 43a8f19117
commit 864260d71d
2 changed files with 12 additions and 0 deletions

View File

@ -117,6 +117,7 @@
#define EXC_INST_PAGE_FAULT 12
#define EXC_LOAD_PAGE_FAULT 13
#define EXC_STORE_PAGE_FAULT 15
#define EXC_SOFTWARE_CHECK 18
#define EXC_INST_GUEST_PAGE_FAULT 20
#define EXC_LOAD_GUEST_PAGE_FAULT 21
#define EXC_VIRTUAL_INST_FAULT 22

View File

@ -173,6 +173,13 @@ void kvm_riscv_vcpu_trap_redirect(struct kvm_vcpu *vcpu,
/* Clear Guest SSTATUS.SIE bit */
vsstatus &= ~SR_SIE;
/* Change Guest SSTATUS.SPELP bit */
if (vcpu->arch.cfg.henvcfg & ENVCFG_LPE) {
vsstatus &= ~SR_SPELP;
vsstatus |= vcpu->arch.guest_context.sstatus & SR_SPELP;
vcpu->arch.guest_context.sstatus &= ~SR_SPELP;
}
/* Update Guest SSTATUS */
ncsr_write(CSR_VSSTATUS, vsstatus);
@ -262,6 +269,10 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,
run->exit_reason = KVM_EXIT_DEBUG;
ret = 0;
break;
case EXC_SOFTWARE_CHECK:
if (vcpu->arch.cfg.henvcfg & (ENVCFG_LPE | ENVCFG_SSE))
ret = vcpu_redirect(vcpu, trap);
break;
default:
break;
}