mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Woodhouse <dwmw2@infradead.org>
To: kvm@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, linux-hyperv@vger.kernel.org,
	linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Sean Christopherson" <seanjc@google.com>,
	"Vitaly Kuznetsov" <vkuznets@redhat.com>,
	"Marcelo Tosatti" <mtosatti@redhat.com>,
	"Joerg Roedel" <joro@8bytes.org>,
	"Tom Lendacky" <thomas.lendacky@amd.com>,
	"James Bottomley" <James.Bottomley@HansenPartnership.com>,
	"Thara Gopinath" <tgopinath@linux.microsoft.com>,
	"Wei Liu" <wei.liu@kernel.org>,
	"Michael Kelley" <mhklinux@outlook.com>,
	"Nicolas Saenz Julienne" <nsaenz@amazon.com>,
	"Nikolas Wipper" <nik.wipper@gmx.de>,
	"Mushahid Hussain" <hmushi@amazon.co.uk>,
	"Sriram Nambakam" <snambakam@linux.microsoft.com>,
	"Zack Rusin" <zack.rusin@broadcom.com>,
	"Doug Covelli" <doug.covelli@broadcom.com>,
	"Jim Mattson" <jmattson@google.com>,
	"Thomas Gleixner" <tglx@kernel.org>,
	"Ingo Molnar" <mingo@redhat.com>,
	"Borislav Petkov" <bp@alien8.de>,
	"Dave Hansen" <dave.hansen@linux.intel.com>,
	x86@kernel.org, "H . Peter Anvin" <hpa@zytor.com>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Randy Dunlap" <rdunlap@infradead.org>,
	"Shuah Khan" <skhan@linuxfoundation.org>,
	"Paul Durrant" <paul@xen.org>, "Xin Li" <xin@zytor.com>,
	"Francesco Lavra" <flavra@baylibre.com>,
	"Radim Krčmář" <rkrcmar@ventanamicro.com>,
	"Liran Alon" <liran.alon@oracle.com>,
	"Arbel Moshe" <arbel.moshe@oracle.com>,
	"David Woodhouse" <dwmw@amazon.co.uk>
Subject: [PATCH v3 10/15] KVM: x86: Filter emulated CR0 and CR4 writes
Date: Fri, 27 Nov 2026 15:27:25 +0000	[thread overview]
Message-ID: <20261127152730.99319-11-dwmw2@infradead.org> (raw)
In-Reply-To: <20261127152730.99319-1-dwmw2@infradead.org>

From: David Woodhouse <dwmw@amazon.co.uk>

MOV to CR, CLTS and LMSW can reach the emulator instead of the vendor
fast paths.  Check the filter in those three handlers, not in
->set_cr(), which RSM and task switches use too.

The emulator retires what it raised, as it does for XSETBV.  Nothing
can arm a CR0 or CR4 filter yet: the vendor patches which follow
advertise them, and SVM leans on the emulator where it lacks decode
assists or NextRIP.

Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
---
 arch/x86/kvm/emulate.c     | 21 +++++++++++++++++++--
 arch/x86/kvm/kvm_emulate.h | 10 ++++++++++
 arch/x86/kvm/x86.c         | 37 +++++++++++++++++++++++++++++++++++++
 3 files changed, 66 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
index 465b7d402f75..9a4aebcccc95 100644
--- a/arch/x86/kvm/emulate.c
+++ b/arch/x86/kvm/emulate.c
@@ -3252,6 +3252,10 @@ static int em_cr_write(struct x86_emulate_ctxt *ctxt)
 	int cr_num = ctxt->modrm_reg;
 	int r;
 
+	r = ctxt->ops->filter_cr_write(ctxt, cr_num, ctxt->src.val);
+	if (r != X86EMUL_CONTINUE)
+		return r;
+
 	if (ctxt->ops->set_cr(ctxt, cr_num, ctxt->src.val))
 		return emulate_gp(ctxt, 0);
 
@@ -3409,9 +3413,15 @@ static int em_invlpg(struct x86_emulate_ctxt *ctxt)
 static int em_clts(struct x86_emulate_ctxt *ctxt)
 {
 	ulong cr0;
+	int r;
 
 	cr0 = ctxt->ops->get_cr(ctxt, 0);
 	cr0 &= ~X86_CR0_TS;
+
+	r = ctxt->ops->filter_cr_write(ctxt, 0, cr0);
+	if (r != X86EMUL_CONTINUE)
+		return r;
+
 	ctxt->ops->set_cr(ctxt, 0, cr0);
 	return X86EMUL_CONTINUE;
 }
@@ -3512,8 +3522,15 @@ static int em_smsw(struct x86_emulate_ctxt *ctxt)
 
 static int em_lmsw(struct x86_emulate_ctxt *ctxt)
 {
-	ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
-			  | (ctxt->src.val & 0x0f));
+	ulong cr0 = (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul) |
+		    (ctxt->src.val & 0x0f);
+	int r;
+
+	r = ctxt->ops->filter_cr_write(ctxt, 0, cr0);
+	if (r != X86EMUL_CONTINUE)
+		return r;
+
+	ctxt->ops->set_cr(ctxt, 0, cr0);
 	ctxt->dst.type = OP_NONE;
 	return X86EMUL_CONTINUE;
 }
diff --git a/arch/x86/kvm/kvm_emulate.h b/arch/x86/kvm/kvm_emulate.h
index 5e18c0cc7f60..311ff6a01079 100644
--- a/arch/x86/kvm/kvm_emulate.h
+++ b/arch/x86/kvm/kvm_emulate.h
@@ -214,6 +214,16 @@ struct x86_emulate_ops {
 	void (*set_idt)(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt);
 	ulong (*get_cr)(struct x86_emulate_ctxt *ctxt, int cr);
 	int (*set_cr)(struct x86_emulate_ctxt *ctxt, int cr, ulong val);
+	/*
+	 * Check a guest control register write against the vCPU's write
+	 * filters (KVM_X86_SET_REG_WRITE_FILTER) before performing it.
+	 * Returns X86EMUL_CONTINUE when the write is to be applied, or
+	 * X86EMUL_IO_NEEDED after setting up a KVM_EXIT_X86_REG_WRITE exit;
+	 * the emulation is then abandoned with no architectural effect, and
+	 * the exit's completion retires the instruction.
+	 */
+	int (*filter_cr_write)(struct x86_emulate_ctxt *ctxt, int cr,
+			       ulong val);
 	int (*cpl)(struct x86_emulate_ctxt *ctxt);
 	ulong (*get_effective_dr7)(struct x86_emulate_ctxt *ctxt);
 	ulong (*get_dr)(struct x86_emulate_ctxt *ctxt, int dr);
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index b559c7880e54..4e6f0cb80288 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -5846,6 +5846,42 @@ static int emulator_filter_xcr_write(struct x86_emulate_ctxt *ctxt, u32 index,
 				       vcpu->arch.xcr0, xcr);
 }
 
+/*
+ * Divert an emulated CR0/CR4 write into a KVM_EXIT_X86_REG_WRITE exit when
+ * it hits a write filter.  Called by the instruction handlers (MOV to CR,
+ * CLTS, LMSW) before any architectural effect; state loads that reach
+ * ->set_cr() on other instructions' behalf (RSM, task switches) are not
+ * guest register writes and stay unfiltered.
+ */
+static int emulator_filter_cr_write(struct x86_emulate_ctxt *ctxt, int cr,
+				    ulong val)
+{
+	struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
+	u64 old_value, new_value;
+	u32 reg;
+
+	switch (cr) {
+	case 0:
+		reg = KVM_X86_REG_FILTER_CR0;
+		old_value = kvm_read_cr0(vcpu);
+		break;
+	case 4:
+		reg = KVM_X86_REG_FILTER_CR4;
+		old_value = kvm_read_cr4(vcpu);
+		break;
+	default:
+		return X86EMUL_CONTINUE;
+	}
+
+	/* Match the merge emulator_set_cr() will do with the same value. */
+	new_value = mk_cr_64(old_value, val);
+
+	if (!kvm_reg_write_filtered(vcpu, reg, old_value, new_value))
+		return X86EMUL_CONTINUE;
+
+	return emulator_reg_write_exit(ctxt, reg, old_value, new_value);
+}
+
 static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt)
 {
 	return kvm_x86_call(get_cpl)(emul_to_vcpu(ctxt));
@@ -6118,6 +6154,7 @@ static const struct x86_emulate_ops emulate_ops = {
 	.set_idt	     = emulator_set_idt,
 	.get_cr              = emulator_get_cr,
 	.set_cr              = emulator_set_cr,
+	.filter_cr_write     = emulator_filter_cr_write,
 	.cpl                 = emulator_get_cpl,
 	.get_effective_dr7   = emulator_get_effective_dr7,
 	.get_dr              = emulator_get_dr,
-- 
2.50.1


  parent reply	other threads:[~2026-11-27 15:27 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27 15:27 [PATCH v3 00/15] KVM: x86: Exit handling for Hyper-V hcalls, register intercept David Woodhouse
2026-11-27 15:27 ` [PATCH v3 01/15] KVM: selftests: Make the x86 SSE register helpers volatile David Woodhouse
2026-11-27 15:27 ` [PATCH v3 02/15] KVM: Define what completing an exit does with its instruction David Woodhouse
2026-11-27 15:27 ` [PATCH v3 03/15] KVM: x86: Let userspace leave an MSR access alone David Woodhouse
2026-11-27 15:27 ` [PATCH v3 04/15] KVM: selftests: Test MSR accesses which userspace leaves alone David Woodhouse
2026-11-27 15:27 ` [PATCH v3 05/15] KVM: x86: hyper-v: Add KVM_CAP_HYPERV_FORWARD_HCALL David Woodhouse
2026-11-27 15:27 ` [PATCH v3 06/15] KVM: x86: hyper-v: Let userspace leave a hypercall uncompleted David Woodhouse
2026-11-27 15:27 ` [PATCH v3 07/15] KVM: x86: hyper-v: Advertise XMM fast hypercall output David Woodhouse
2026-11-27 15:27 ` [PATCH v3 08/15] KVM: selftests: Add Hyper-V hypercall forwarding test David Woodhouse
2026-11-27 15:27 ` [PATCH v3 09/15] KVM: x86: Add a per-vCPU register write filter David Woodhouse
2026-11-27 15:27 ` David Woodhouse [this message]
2026-11-27 15:27 ` [PATCH v3 11/15] KVM: VMX: Support CR0 and CR4 write filters David Woodhouse
2026-11-27 15:27 ` [PATCH v3 12/15] KVM: SVM: " David Woodhouse
2026-11-27 15:27 ` [PATCH v3 13/15] KVM: x86: Add descriptor-table registers to the write filter David Woodhouse
2026-11-27 15:27 ` [PATCH v3 14/15] KVM: selftests: Add x86 register write filter test David Woodhouse
2026-11-27 15:27 ` [PATCH v3 15/15] KVM: selftests: Test the register write filter with a nested guest David Woodhouse

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261127152730.99319-11-dwmw2@infradead.org \
    --to=dwmw2@infradead.org \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=arbel.moshe@oracle.com \
    --cc=bp@alien8.de \
    --cc=corbet@lwn.net \
    --cc=dave.hansen@linux.intel.com \
    --cc=doug.covelli@broadcom.com \
    --cc=dwmw@amazon.co.uk \
    --cc=flavra@baylibre.com \
    --cc=hmushi@amazon.co.uk \
    --cc=hpa@zytor.com \
    --cc=jmattson@google.com \
    --cc=joro@8bytes.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-hyperv@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=liran.alon@oracle.com \
    --cc=mhklinux@outlook.com \
    --cc=mingo@redhat.com \
    --cc=mtosatti@redhat.com \
    --cc=nik.wipper@gmx.de \
    --cc=nsaenz@amazon.com \
    --cc=paul@xen.org \
    --cc=pbonzini@redhat.com \
    --cc=rdunlap@infradead.org \
    --cc=rkrcmar@ventanamicro.com \
    --cc=seanjc@google.com \
    --cc=skhan@linuxfoundation.org \
    --cc=snambakam@linux.microsoft.com \
    --cc=tglx@kernel.org \
    --cc=tgopinath@linux.microsoft.com \
    --cc=thomas.lendacky@amd.com \
    --cc=vkuznets@redhat.com \
    --cc=wei.liu@kernel.org \
    --cc=x86@kernel.org \
    --cc=xin@zytor.com \
    --cc=zack.rusin@broadcom.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®