[PATCH v3 12/15] KVM: SVM: Support CR0 and CR4 write filters
From: David Woodhouse
Date: Fri Nov 27 2026 - 10:29:02 EST
From: David Woodhouse <dwmw@xxxxxxxxxxxx>
Keep INTERCEPT_CR0_WRITE set in vmcb01 while a CR0 filter is armed,
and check the filter in cr_interception(). Without NextRIP the write
goes to the emulator, which knows the instruction length. L2 is not
filtered.
Signed-off-by: David Woodhouse <dwmw@xxxxxxxxxxxx>
---
arch/x86/kvm/svm/svm.c | 66 +++++++++++++++++++++++++++++++++++++++++-
1 file changed, 65 insertions(+), 1 deletion(-)
diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index 4ffb41234dc2..c91467a59e20 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -1135,6 +1135,22 @@ static void svm_recalc_intercepts(struct kvm_vcpu *vcpu)
svm_recalc_msr_intercepts(vcpu);
}
+static void svm_update_reg_write_filter(struct kvm_vcpu *vcpu)
+{
+ struct vcpu_svm *svm = to_svm(vcpu);
+
+ /*
+ * CR4 writes are always intercepted. CR0 writes are not when the
+ * guest and host views agree (hcr0 == cr0): force the intercept in
+ * vmcb01 while a CR0 filter is armed. Disarming leaves the
+ * intercept in place - a forced intercept only costs exits, which
+ * cr_interception() handles as usual - and the next svm_set_cr0(),
+ * which respects the filter, restores its own state.
+ */
+ if (vcpu->arch.reg_write_filter_armed & BIT(KVM_X86_REG_FILTER_CR0))
+ svm_set_intercept(svm, INTERCEPT_CR0_WRITE);
+}
+
static void init_vmcb(struct kvm_vcpu *vcpu, bool init_event)
{
struct vcpu_svm *svm = to_svm(vcpu);
@@ -1867,7 +1883,13 @@ void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
if (hcr0 == cr0) {
/* Selective CR0 write remains on. */
svm_clr_intercept(svm, INTERCEPT_CR0_READ);
- svm_clr_intercept(svm, INTERCEPT_CR0_WRITE);
+ /*
+ * The selective intercept misses writes that change only
+ * TS or MP, and CLTS: a CR0 write filter needs them all.
+ */
+ if (!(vcpu->arch.reg_write_filter_armed &
+ BIT(KVM_X86_REG_FILTER_CR0)))
+ svm_clr_intercept(svm, INTERCEPT_CR0_WRITE);
} else {
svm_set_intercept(svm, INTERCEPT_CR0_READ);
svm_set_intercept(svm, INTERCEPT_CR0_WRITE);
@@ -2647,6 +2669,21 @@ static bool check_selective_cr0_intercepted(struct kvm_vcpu *vcpu,
#define CR_VALID (1ULL << 63)
+/*
+ * Send an intercepted write which a register write filter catches to
+ * userspace instead of performing it. The caller has checked that the
+ * length of the instruction is known.
+ */
+static bool svm_reg_write_filtered(struct kvm_vcpu *vcpu, u32 reg,
+ unsigned long old, unsigned long val)
+{
+ if (!kvm_reg_write_filtered(vcpu, reg, old, val))
+ return false;
+
+ kvm_reg_write_user_space(vcpu, reg, old, val, svm_exit_insn_len(vcpu));
+ return true;
+}
+
static int cr_interception(struct kvm_vcpu *vcpu)
{
struct vcpu_svm *svm = to_svm(vcpu);
@@ -2660,6 +2697,16 @@ static int cr_interception(struct kvm_vcpu *vcpu)
if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
return emulate_on_interception(vcpu);
+ /*
+ * A filtered write reports the length of its instruction, which only
+ * NextRIP provides here. Without it, let the emulator raise the exit,
+ * as it does without Decode Assists.
+ */
+ if ((vcpu->arch.reg_write_filter_armed &
+ (BIT(KVM_X86_REG_FILTER_CR0) | BIT(KVM_X86_REG_FILTER_CR4))) &&
+ !svm_exit_insn_len(vcpu))
+ return emulate_on_interception(vcpu);
+
reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
@@ -2673,6 +2720,14 @@ static int cr_interception(struct kvm_vcpu *vcpu)
trace_kvm_cr_write(cr, val);
switch (cr) {
case 0:
+ /*
+ * The filter never triggers in guest mode and the
+ * selective-CR0 reflection only applies there, so
+ * the order of the two checks does not matter.
+ */
+ if (svm_reg_write_filtered(vcpu, KVM_X86_REG_FILTER_CR0,
+ kvm_read_cr0(vcpu), val))
+ return 0;
if (!check_selective_cr0_intercepted(vcpu, val))
err = kvm_set_cr0(vcpu, val);
else
@@ -2683,6 +2738,9 @@ static int cr_interception(struct kvm_vcpu *vcpu)
err = kvm_set_cr3(vcpu, val);
break;
case 4:
+ if (svm_reg_write_filtered(vcpu, KVM_X86_REG_FILTER_CR4,
+ kvm_read_cr4(vcpu), val))
+ return 0;
err = kvm_set_cr4(vcpu, val);
break;
case 8:
@@ -5617,6 +5675,8 @@ struct kvm_x86_ops svm_x86_ops __initdata = {
.apic_init_signal_blocked = svm_apic_init_signal_blocked,
+ .update_reg_write_filter = svm_update_reg_write_filter,
+
.recalc_intercepts = svm_recalc_intercepts,
.complete_emulated_msr = svm_complete_emulated_msr,
@@ -5722,6 +5782,10 @@ static __init void svm_set_cpu_caps(void)
if (cpu_feature_enabled(X86_FEATURE_BUS_LOCK_THRESHOLD))
kvm_caps.has_bus_lock_exit = true;
+ /* CR0/CR4 write interception can always be forced. */
+ kvm_caps.supported_reg_write_filter |= BIT(KVM_X86_REG_FILTER_CR0) |
+ BIT(KVM_X86_REG_FILTER_CR4);
+
/* CPUID 0x80000008 */
if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) ||
boot_cpu_has(X86_FEATURE_AMD_SSBD))
--
2.50.1