From nobody Thu Oct 2 22:40:03 2025 Received: from out198-15.us.a.mail.aliyun.com (out198-15.us.a.mail.aliyun.com [47.90.198.15]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CD56A248F5E; Wed, 10 Sep 2025 02:55:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=47.90.198.15 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757472903; cv=none; b=n7KlqcDcMQapANtT4Ef2LMYjP8FLMHWwrV3wEeXoGgYJV3UHdB3XXpGwtxgjqEeGIV9z5Z/iVO8dgU7BmBBeN6zdXox9OdCUH4Lrxm26D4dmbKqT2KYt8TZkYCHdIJY256fH1WiqMn4VbC8MX4aJixYDTDBx4R7FAFPLo3Xp0Qc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757472903; c=relaxed/simple; bh=ELenBpY/5KgZte93vWBfudD9K2sGtS9FzpmRBYNgrcU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Vl+Eg/Fy0eJvPMqtUH8hBfrNw2tEcfDHSqjQWsUE+LFtD8G7v9YUTaNSTyDCqwD4XeRCsaqC7cvYZl72pHc/y4GHGiuaJEiOicGQTffJD8SvcX3RLZoU/xVRBx7O6hsh/rpKDThkdskMQFXEuD7Ar6M7Ra6Hj4Xq1z2AQIPmyI8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=antgroup.com; spf=pass smtp.mailfrom=antgroup.com; dkim=pass (1024-bit key) header.d=antgroup.com header.i=@antgroup.com header.b=d2wXsRxt; arc=none smtp.client-ip=47.90.198.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=antgroup.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=antgroup.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=antgroup.com header.i=@antgroup.com header.b="d2wXsRxt" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=antgroup.com; s=default; t=1757472883; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=YXYjSPwBEzyTiJUkKnb+LmPlxLCJZLMXoTgZ0VFZOcg=; b=d2wXsRxtWBnGdlA8lITyJaS91t2Ioh+U22y9eR8OzsEsPlXD45vUAC1IAnQ9+wZ2u7cxVu+zLFZACVngXXDcmu1DSH1nNjs00GenSBskac7SMv383dx+IjwcsJebv6CmC8L66B52m+DgmqpkIq+OwVqxyEMvLybTu371c/BcJrg= Received: from localhost(mailfrom:houwenlong.hwl@antgroup.com fp:SMTPD_---.ebfmCoJ_1757472563 cluster:ay29) by smtp.aliyun-inc.com; Wed, 10 Sep 2025 10:49:24 +0800 From: Hou Wenlong To: kvm@vger.kernel.org Cc: Lai Jiangshan , Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , linux-kernel@vger.kernel.org Subject: [PATCH 1/7] KVM: x86: Set guest DR6 by kvm_queue_exception_p() in instruction emulation Date: Wed, 10 Sep 2025 10:49:13 +0800 Message-Id: <14773b9e2387eaeee3af9fe0c6ca28a0b8fcee69.1757416809.git.houwenlong.hwl@antgroup.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Record DR6 in emulate_db() and use kvm_queue_exception_p() to set DR6 instead of directly using kvm_set_dr6() in emulation, which keeps the handling of DR6 during #DB injection consistent with other code paths. No functional change intended. Signed-off-by: Hou Wenlong --- arch/x86/kvm/emulate.c | 14 ++++---------- arch/x86/kvm/kvm_emulate.h | 6 +++++- arch/x86/kvm/x86.c | 3 +++ 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c index 542d3664afa3..18e3a732d106 100644 --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c @@ -593,8 +593,9 @@ static int emulate_exception(struct x86_emulate_ctxt *c= txt, int vec, return X86EMUL_PROPAGATE_FAULT; } =20 -static int emulate_db(struct x86_emulate_ctxt *ctxt) +static int emulate_db(struct x86_emulate_ctxt *ctxt, unsigned long dr6) { + ctxt->exception.dr6 =3D dr6; return emulate_exception(ctxt, DB_VECTOR, 0, false); } =20 @@ -3857,15 +3858,8 @@ static int check_dr_read(struct x86_emulate_ctxt *ct= xt) if ((cr4 & X86_CR4_DE) && (dr =3D=3D 4 || dr =3D=3D 5)) return emulate_ud(ctxt); =20 - if (ctxt->ops->get_dr(ctxt, 7) & DR7_GD) { - ulong dr6; - - dr6 =3D ctxt->ops->get_dr(ctxt, 6); - dr6 &=3D ~DR_TRAP_BITS; - dr6 |=3D DR6_BD | DR6_ACTIVE_LOW; - ctxt->ops->set_dr(ctxt, 6, dr6); - return emulate_db(ctxt); - } + if (ctxt->ops->get_dr(ctxt, 7) & DR7_GD) + return emulate_db(ctxt, DR6_BD); =20 return X86EMUL_CONTINUE; } diff --git a/arch/x86/kvm/kvm_emulate.h b/arch/x86/kvm/kvm_emulate.h index 7b5ddb787a25..a6fad7b938e3 100644 --- a/arch/x86/kvm/kvm_emulate.h +++ b/arch/x86/kvm/kvm_emulate.h @@ -24,7 +24,11 @@ struct x86_exception { bool error_code_valid; u16 error_code; bool nested_page_fault; - u64 address; /* cr2 or nested page fault gpa */ + union { + u64 address; /* cr2 or nested page fault gpa */ + unsigned long dr6; + u64 payload; + }; u8 async_page_fault; unsigned long exit_qualification; }; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 7ba2cdfdac44..b2e8322aeca7 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -8612,6 +8612,8 @@ static void inject_emulated_exception(struct kvm_vcpu= *vcpu) =20 if (ctxt->exception.vector =3D=3D PF_VECTOR) kvm_inject_emulated_page_fault(vcpu, &ctxt->exception); + else if (ctxt->exception.vector =3D=3D DB_VECTOR) + kvm_queue_exception_p(vcpu, DB_VECTOR, ctxt->exception.dr6); else if (ctxt->exception.error_code_valid) kvm_queue_exception_e(vcpu, ctxt->exception.vector, ctxt->exception.error_code); @@ -8656,6 +8658,7 @@ static void init_emulate_ctxt(struct kvm_vcpu *vcpu) ctxt->interruptibility =3D 0; ctxt->have_exception =3D false; ctxt->exception.vector =3D -1; + ctxt->exception.payload =3D 0; ctxt->perm_ok =3D false; =20 init_decode_cache(ctxt); --=20 2.31.1 From nobody Thu Oct 2 22:40:03 2025 Received: from out28-52.mail.aliyun.com (out28-52.mail.aliyun.com [115.124.28.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A3C9318E1F; Wed, 10 Sep 2025 02:54:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.28.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757472890; cv=none; b=Zfy8n0zOqsVfgymeWF1Q1ULr7M+lpRHgGaWPVtf+TxS0Tf51DLTsYthpM5I8g2F95oOPiV51GviJWDt/Zq4KlZmP5EAidB5Wi0oipja4IZK3ffGJyYlJbimgJs3B68cMSSZ04xN4vT7UJE5FSYB/VMxkKb2e8jJRIEE+9bbAsmE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757472890; c=relaxed/simple; bh=B4xvjjI1rrLCW6sI7GSnMtzM776TeMbBZrnd2OKF/A0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hxkdPuCV+6NwwI7jw+siLaQVZ4OhW7AJ0RT3OggfaBbcL7BaSF6FQO9c6AY3tS4H2bUtjtud99HiaNYzMINosZo0+PWkOFHuQpzmsra3YcwmcmykXfKGIN3vYN4VcynmoeoJJZloL+mCIZrigFdIUMuqs7r57122+3XMp7zAJvw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=antgroup.com; spf=pass smtp.mailfrom=antgroup.com; dkim=pass (1024-bit key) header.d=antgroup.com header.i=@antgroup.com header.b=Esjrdf3z; arc=none smtp.client-ip=115.124.28.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=antgroup.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=antgroup.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=antgroup.com header.i=@antgroup.com header.b="Esjrdf3z" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=antgroup.com; s=default; t=1757472884; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=28FlJ5PhUPR1yuFNdYQOxlXS7iJPbikWqlooFHuZXaI=; b=Esjrdf3zVoypVoE31+lesII6s3/JrUDsvtZdMnaYoS2soUTgBUqitThEp7yd16ErVrMfLVezTv4LPBh/hLTVfr6AuK5IacFgNRJNUGbeW3Mnp6DMUX3rzwEQp2ycY5plqIFWkg3xz/3vDyOwbBMR5pPsHPrOFkgVu8KeZ+LmrGg= Received: from localhost(mailfrom:houwenlong.hwl@antgroup.com fp:SMTPD_---.ebgggze_1757472565 cluster:ay29) by smtp.aliyun-inc.com; Wed, 10 Sep 2025 10:49:25 +0800 From: Hou Wenlong To: kvm@vger.kernel.org Cc: Lai Jiangshan , Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , linux-kernel@vger.kernel.org Subject: [PATCH 2/7] KVM: x86: Check guest debug in DR access instruction emulation Date: Wed, 10 Sep 2025 10:49:14 +0800 Message-Id: <6d375ab3edb54645ac16e0446dc7516105ed4b04.1757416809.git.houwenlong.hwl@antgroup.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When a DR access instruction is emulated by the x86 instruction emulator, only the guest DR7.GD is checked. Since the instruction emulation path has already performed some guest debug checks, add a guest debug check in the DR access instruction emulation to improve the guest debug logic in the instruction emulator. Suggested-by: Lai Jiangshan Signed-off-by: Hou Wenlong --- arch/x86/kvm/emulate.c | 2 +- arch/x86/kvm/kvm_emulate.h | 1 + arch/x86/kvm/x86.c | 51 +++++++++++++++++++++++++++++++++----- arch/x86/kvm/x86.h | 7 ++++++ 4 files changed, 54 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c index 18e3a732d106..87d98ffd7d2d 100644 --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c @@ -3858,7 +3858,7 @@ static int check_dr_read(struct x86_emulate_ctxt *ctx= t) if ((cr4 & X86_CR4_DE) && (dr =3D=3D 4 || dr =3D=3D 5)) return emulate_ud(ctxt); =20 - if (ctxt->ops->get_dr(ctxt, 7) & DR7_GD) + if (ctxt->ops->get_eff_dr(ctxt, 7) & DR7_GD) return emulate_db(ctxt, DR6_BD); =20 return X86EMUL_CONTINUE; diff --git a/arch/x86/kvm/kvm_emulate.h b/arch/x86/kvm/kvm_emulate.h index a6fad7b938e3..b971b2947094 100644 --- a/arch/x86/kvm/kvm_emulate.h +++ b/arch/x86/kvm/kvm_emulate.h @@ -216,6 +216,7 @@ struct x86_emulate_ops { int (*set_cr)(struct x86_emulate_ctxt *ctxt, int cr, ulong val); int (*cpl)(struct x86_emulate_ctxt *ctxt); ulong (*get_dr)(struct x86_emulate_ctxt *ctxt, int dr); + ulong (*get_eff_dr)(struct x86_emulate_ctxt *ctxt, int dr); int (*set_dr)(struct x86_emulate_ctxt *ctxt, int dr, ulong value); int (*set_msr_with_filter)(struct x86_emulate_ctxt *ctxt, u32 msr_index, = u64 data); int (*get_msr_with_filter)(struct x86_emulate_ctxt *ctxt, u32 msr_index, = u64 *pdata); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index b2e8322aeca7..cf289d04b104 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1571,6 +1571,22 @@ unsigned long kvm_get_dr(struct kvm_vcpu *vcpu, int = dr) } EXPORT_SYMBOL_GPL(kvm_get_dr); =20 +static unsigned long kvm_get_eff_dr(struct kvm_vcpu *vcpu, int dr) +{ + size_t size =3D ARRAY_SIZE(vcpu->arch.eff_db); + + switch (dr) { + case 0 ... 3: + return vcpu->arch.eff_db[array_index_nospec(dr, size)]; + case 4: + case 6: + return vcpu->arch.dr6; + case 5: + default: /* 7 */ + return kvm_get_eff_dr7(vcpu); + } +} + int kvm_emulate_rdpmc(struct kvm_vcpu *vcpu) { u32 pmc =3D kvm_rcx_read(vcpu); @@ -8207,6 +8223,11 @@ static unsigned long emulator_get_dr(struct x86_emul= ate_ctxt *ctxt, int dr) return kvm_get_dr(emul_to_vcpu(ctxt), dr); } =20 +static unsigned long emulator_get_eff_dr(struct x86_emulate_ctxt *ctxt, in= t dr) +{ + return kvm_get_eff_dr(emul_to_vcpu(ctxt), dr); +} + static int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value) { @@ -8563,6 +8584,7 @@ static const struct x86_emulate_ops emulate_ops =3D { .set_cr =3D emulator_set_cr, .cpl =3D emulator_get_cpl, .get_dr =3D emulator_get_dr, + .get_eff_dr =3D emulator_get_eff_dr, .set_dr =3D emulator_set_dr, .set_msr_with_filter =3D emulator_set_msr_with_filter, .get_msr_with_filter =3D emulator_get_msr_with_filter, @@ -8606,19 +8628,38 @@ static void toggle_interruptibility(struct kvm_vcpu= *vcpu, u32 mask) } } =20 -static void inject_emulated_exception(struct kvm_vcpu *vcpu) +static int kvm_inject_emulated_db(struct kvm_vcpu *vcpu, unsigned long dr6) +{ + struct kvm_run *kvm_run =3D vcpu->run; + + if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { + kvm_run->debug.arch.dr6 =3D dr6 | DR6_ACTIVE_LOW; + kvm_run->debug.arch.pc =3D kvm_get_linear_rip(vcpu); + kvm_run->debug.arch.exception =3D DB_VECTOR; + kvm_run->exit_reason =3D KVM_EXIT_DEBUG; + return 0; + } + + kvm_queue_exception_p(vcpu, DB_VECTOR, dr6); + return 1; +} + +static int inject_emulated_exception(struct kvm_vcpu *vcpu) { + int r =3D 1; struct x86_emulate_ctxt *ctxt =3D vcpu->arch.emulate_ctxt; =20 if (ctxt->exception.vector =3D=3D PF_VECTOR) kvm_inject_emulated_page_fault(vcpu, &ctxt->exception); else if (ctxt->exception.vector =3D=3D DB_VECTOR) - kvm_queue_exception_p(vcpu, DB_VECTOR, ctxt->exception.dr6); + r =3D kvm_inject_emulated_db(vcpu, ctxt->exception.dr6); else if (ctxt->exception.error_code_valid) kvm_queue_exception_e(vcpu, ctxt->exception.vector, ctxt->exception.error_code); else kvm_queue_exception(vcpu, ctxt->exception.vector); + + return r; } =20 static struct x86_emulate_ctxt *alloc_emulate_ctxt(struct kvm_vcpu *vcpu) @@ -9098,8 +9139,7 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, gp= a_t cr2_or_gpa, */ WARN_ON_ONCE(ctxt->exception.vector =3D=3D UD_VECTOR || exception_type(ctxt->exception.vector) =3D=3D EXCPT_TRAP); - inject_emulated_exception(vcpu); - return 1; + return inject_emulated_exception(vcpu); } return handle_emulation_failure(vcpu, emulation_type); } @@ -9190,8 +9230,7 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, gp= a_t cr2_or_gpa, if (ctxt->have_exception) { WARN_ON_ONCE(vcpu->mmio_needed && !vcpu->mmio_is_write); vcpu->mmio_needed =3D false; - r =3D 1; - inject_emulated_exception(vcpu); + r =3D inject_emulated_exception(vcpu); } else if (vcpu->arch.pio.count) { if (!vcpu->arch.pio.in) { /* FIXME: return into emulator if single-stepping. */ diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h index eb3088684e8a..7ad3b9645ea3 100644 --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h @@ -593,6 +593,13 @@ static inline bool kvm_dr6_valid(u64 data) return !(data >> 32); } =20 +static inline unsigned long kvm_get_eff_dr7(struct kvm_vcpu *vcpu) +{ + if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) + return vcpu->arch.guest_debug_dr7; + return vcpu->arch.dr7; +} + /* * Trigger machine check on the host. We assume all the MSRs are already s= et up * by the CPU and that we still run on the same CPU as the MCE occurred on. --=20 2.31.1 From nobody Thu Oct 2 22:40:03 2025 Received: from out28-52.mail.aliyun.com (out28-52.mail.aliyun.com [115.124.28.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DE1711F91E3; Wed, 10 Sep 2025 02:49:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.28.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757472577; cv=none; b=D0AcBG8FJnSLJ0l2zUL4aNwoisRyoGkX1yClHWrvCRTZfq6Dk9kNaiC6o740VZ0kEvlg7w34Yn6lGhBvG0P2f1zpRO0RPtyf8iSWmMZEOzFC6sARYO6Vwh18F+1tyuJtcQHEW5aiAF9W5VG+K1equj6t0sXqOXPO9MAA47bQYZ0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757472577; c=relaxed/simple; bh=Tnna44QehBkN4uomOmV1T5siUdJ4l9NkIBpvBlJqAMc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=MGZOFxpl26nTxUimHjqgHzQKk0ZuqkWgNRnHJVCZ8zI8mY95DSdGh6ieuuImxqOGA0OL2uCociFI95uqVMZiyKJ6GbwSouJiVmKloZkKpoT555KYBWHAUnNmAXLSNuIYEiF/pkcJwfsFUniwkCKcQxC2Z2YaYM/YzDBYp36DWDA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=antgroup.com; spf=pass smtp.mailfrom=antgroup.com; dkim=pass (1024-bit key) header.d=antgroup.com header.i=@antgroup.com header.b=sVXRQlv2; arc=none smtp.client-ip=115.124.28.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=antgroup.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=antgroup.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=antgroup.com header.i=@antgroup.com header.b="sVXRQlv2" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=antgroup.com; s=default; t=1757472567; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=8ZPH5RhranyasvKwjlXf14i0q2sAaVJOLqLubug0iBk=; b=sVXRQlv24dkp6ppPJvS5ei+HecrL/Or5NztMQDwvhDauQlsGiNFXr+ven2/L29dmhnSGccn1WiRfB+ObRFlBxLDIgZXwt6qG9N1AD03SLzLgfwUu6hK0PD16uUHj3er9dK/Vtr8PwaqTMwVxsB4HA6TBy8kKIEUB2DCQ3qW47aU= Received: from localhost(mailfrom:houwenlong.hwl@antgroup.com fp:SMTPD_---.ebfE1in_1757472566 cluster:ay29) by smtp.aliyun-inc.com; Wed, 10 Sep 2025 10:49:26 +0800 From: Hou Wenlong To: kvm@vger.kernel.org Cc: Lai Jiangshan , Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , linux-kernel@vger.kernel.org Subject: [PATCH 3/7] KVM: x86: Only check effective code breakpoint in emulation Date: Wed, 10 Sep 2025 10:49:15 +0800 Message-Id: <7cf5f98526f07ceb91135cde0253ed9209fd5269.1757416809.git.houwenlong.hwl@antgroup.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When guest debug is enabled, the effective breakpoints are controlled by guest debug rather than by the guest itself. Therefore, only check the code breakpoints of guest debug in emulation if guest debug is enabled, in order to maintain consistency with hardware behavior. Fixes: 4a1e10d5b5d8 ("KVM: x86: handle hardware breakpoints during emulatio= n") Signed-off-by: Hou Wenlong --- arch/x86/kvm/x86.c | 35 ++++++++++------------------------- 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index cf289d04b104..5af652916a19 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -8947,6 +8947,9 @@ EXPORT_SYMBOL_GPL(kvm_skip_emulated_instruction); =20 static bool kvm_is_code_breakpoint_inhibited(struct kvm_vcpu *vcpu) { + if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) + return false; + if (kvm_get_rflags(vcpu) & X86_EFLAGS_RF) return true; =20 @@ -8963,6 +8966,8 @@ static bool kvm_is_code_breakpoint_inhibited(struct k= vm_vcpu *vcpu) static bool kvm_vcpu_check_code_breakpoint(struct kvm_vcpu *vcpu, int emulation_type, int *r) { + unsigned long dr7 =3D kvm_get_eff_dr7(vcpu); + WARN_ON_ONCE(emulation_type & EMULTYPE_NO_DECODE); =20 /* @@ -8983,34 +8988,14 @@ static bool kvm_vcpu_check_code_breakpoint(struct k= vm_vcpu *vcpu, EMULTYPE_TRAP_UD | EMULTYPE_VMWARE_GP | EMULTYPE_PF)) return false; =20 - if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) && - (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) { - struct kvm_run *kvm_run =3D vcpu->run; - unsigned long eip =3D kvm_get_linear_rip(vcpu); - u32 dr6 =3D kvm_vcpu_check_hw_bp(eip, 0, - vcpu->arch.guest_debug_dr7, - vcpu->arch.eff_db); - - if (dr6 !=3D 0) { - kvm_run->debug.arch.dr6 =3D dr6 | DR6_ACTIVE_LOW; - kvm_run->debug.arch.pc =3D eip; - kvm_run->debug.arch.exception =3D DB_VECTOR; - kvm_run->exit_reason =3D KVM_EXIT_DEBUG; - *r =3D 0; - return true; - } - } - - if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) && + if (unlikely(dr7 & DR7_BP_EN_MASK) && !kvm_is_code_breakpoint_inhibited(vcpu)) { unsigned long eip =3D kvm_get_linear_rip(vcpu); - u32 dr6 =3D kvm_vcpu_check_hw_bp(eip, 0, - vcpu->arch.dr7, - vcpu->arch.db); + u32 dr6 =3D kvm_vcpu_check_hw_bp(eip, 0, dr7, + vcpu->arch.eff_db); =20 - if (dr6 !=3D 0) { - kvm_queue_exception_p(vcpu, DB_VECTOR, dr6); - *r =3D 1; + if (dr6) { + *r =3D kvm_inject_emulated_db(vcpu, dr6); return true; } } --=20 2.31.1 From nobody Thu Oct 2 22:40:03 2025 Received: from out198-17.us.a.mail.aliyun.com (out198-17.us.a.mail.aliyun.com [47.90.198.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C4333248F5C; Wed, 10 Sep 2025 02:54:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=47.90.198.17 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757472901; cv=none; b=pgKMBhDJPknE6gjaxHXGs0U2jvtvNt49/dFT4AUZLaRMDoNybLtg2pEu5SFo3DHQuOEFAuKqCd/HjhLHOqItRvHSKEOb6EsCbriekuFl8PPCxrT1CY4mwMXDWUZUarbDCrJvbEQzjwgonG2rK8WdA6u6XqZJUEa/dasW+qvFVcE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757472901; c=relaxed/simple; bh=kYZbqIr5MidBn0HJ2lhzmxRbTeiwTgdazPiuPuescF8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=iLuumOCe09XGyfBApzAgon8IQuW8wDfZ7vQzCEkj0RZS6SkigGnfjLventAl+56uFR4oBQ94ucT+tc+bbqT68ciZR3My3QTJHYHxfpstTeykqvA2sO5PseAuqwkjKsrH/XonOuDTpl9f3m1GUqOrbjIAYr0/srf5cvAqWMK0Dv0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=antgroup.com; spf=pass smtp.mailfrom=antgroup.com; dkim=pass (1024-bit key) header.d=antgroup.com header.i=@antgroup.com header.b=WaGWB7fm; arc=none smtp.client-ip=47.90.198.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=antgroup.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=antgroup.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=antgroup.com header.i=@antgroup.com header.b="WaGWB7fm" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=antgroup.com; s=default; t=1757472887; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=JnJ9k+tsKVXHxmYDXdwSc1SeFCgYSHPpp2qWvdPYtD0=; b=WaGWB7fmPYdLcdMYQ6HzymjGivZ49jkqLShmu8xk9eSwLwArI3CmUM+bxRiuTpX/mQE8NEbMDqZnCLclU2Q1NM7Omnp3rhrT6LSR9I2KrcWPUkGdP9ID8UJ6I45j75o5iUtruxQac8rMMf/VnkCKduD4GWkbA87I8Gxg2mUesFU= Received: from localhost(mailfrom:houwenlong.hwl@antgroup.com fp:SMTPD_---.ebdO4aZ_1757472567 cluster:ay29) by smtp.aliyun-inc.com; Wed, 10 Sep 2025 10:49:27 +0800 From: Hou Wenlong To: kvm@vger.kernel.org Cc: Lai Jiangshan , Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , linux-kernel@vger.kernel.org Subject: [PATCH 4/7] KVM: x86: Consolidate KVM_GUESTDBG_SINGLESTEP check into the kvm_inject_emulated_db() Date: Wed, 10 Sep 2025 10:49:16 +0800 Message-Id: <45cbc005e14ea2a4b9ec803a91af63e364aeb71a.1757416809.git.houwenlong.hwl@antgroup.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Use kvm_inject_emulated_db() in kvm_vcpu_do_singlestep() to consolidate 'KVM_GUESTDBG_SINGLESTEP' check into kvm_inject_emulated_db() during emulation. No functional change intended. Suggested-by: Lai Jiangshan Signed-off-by: Hou Wenlong --- arch/x86/kvm/x86.c | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 5af652916a19..83960214d5d8 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -8632,7 +8632,10 @@ static int kvm_inject_emulated_db(struct kvm_vcpu *v= cpu, unsigned long dr6) { struct kvm_run *kvm_run =3D vcpu->run; =20 - if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { + /* Data breakpoints are not supported in emulation for now. */ + WARN_ON((dr6 & DR6_BS) && (dr6 & DR_TRAP_BITS)); + + if (vcpu->guest_debug & (KVM_GUESTDBG_USE_HW_BP | KVM_GUESTDBG_SINGLESTEP= )) { kvm_run->debug.arch.dr6 =3D dr6 | DR6_ACTIVE_LOW; kvm_run->debug.arch.pc =3D kvm_get_linear_rip(vcpu); kvm_run->debug.arch.exception =3D DB_VECTOR; @@ -8907,17 +8910,7 @@ static int kvm_vcpu_check_hw_bp(unsigned long addr, = u32 type, u32 dr7, =20 static int kvm_vcpu_do_singlestep(struct kvm_vcpu *vcpu) { - struct kvm_run *kvm_run =3D vcpu->run; - - if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) { - kvm_run->debug.arch.dr6 =3D DR6_BS | DR6_ACTIVE_LOW; - kvm_run->debug.arch.pc =3D kvm_get_linear_rip(vcpu); - kvm_run->debug.arch.exception =3D DB_VECTOR; - kvm_run->exit_reason =3D KVM_EXIT_DEBUG; - return 0; - } - kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BS); - return 1; + return kvm_inject_emulated_db(vcpu, DR6_BS); } =20 int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu) --=20 2.31.1 From nobody Thu Oct 2 22:40:03 2025 Received: from out28-2.mail.aliyun.com (out28-2.mail.aliyun.com [115.124.28.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 48907233D7B; Wed, 10 Sep 2025 02:54:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.28.2 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757472893; cv=none; b=AVFklSF5iMwgk08yImI303WPgLvN9pw8hY0zUwkJAjIXvlsTCLi2IXTCd5pa0EP7lklsHGPSNgWClqgoh4CMAirHXEE5dZvHyTmFgA4/mn1DIGnXO28s0EuPNInakkM2VJfDccBe2a7+3hAKsM1zinXU5lVtEFLb7ltDGFyjj08= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757472893; c=relaxed/simple; bh=0WIDcgjZuXIJk7pDetcgQ74hoJJf5T6G/Kb2qcOc08Q=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=dPiWa+C3wkJMHZMFEGy4A70QODEAyAk0A1riCQ2EUzAMNxVSat6jrPCoOIwwOTm5BAp2ZCU9Y3w2fmjto2FMEISOmTbyMtWXJdxXXoVkxDsQmRy5kqIDJVAZ4LYAeAh+1ODHOCQCLUG6dCKNhdW2qKydRvGgEcYjuKVgYx8LOkE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=antgroup.com; spf=pass smtp.mailfrom=antgroup.com; dkim=pass (1024-bit key) header.d=antgroup.com header.i=@antgroup.com header.b=K0zWByDA; arc=none smtp.client-ip=115.124.28.2 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=antgroup.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=antgroup.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=antgroup.com header.i=@antgroup.com header.b="K0zWByDA" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=antgroup.com; s=default; t=1757472888; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=IkAb+os3P2Jf4Ct8/iKChVCSjH0a3tMadZqmTNIXI7Q=; b=K0zWByDARb2QBQl4jkbaruaAsbGTKophi8FSDsop9hp40HJ5AyZIg2WgB7Yg+s6yp/TTipmFBhfnvUZWY9TS5b/1QOviPYxN8CRlAnOdWIcPqES4UfRV25A9+a0yKqL8hZLke86iwLv4k357syZ3zltBTKroQchknb9NIaWv9JQ= Received: from localhost(mailfrom:houwenlong.hwl@antgroup.com fp:SMTPD_---.ebfNx90_1757472569 cluster:ay29) by smtp.aliyun-inc.com; Wed, 10 Sep 2025 10:49:29 +0800 From: Hou Wenlong To: kvm@vger.kernel.org Cc: Lai Jiangshan , Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , linux-kernel@vger.kernel.org Subject: [PATCH 5/7] KVM: VMX: Set 'BS' bit in pending debug exceptions during instruction emulation Date: Wed, 10 Sep 2025 10:49:17 +0800 Message-Id: X-Mailer: git-send-email 2.31.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" If 'STI' or 'MOV SS' with 'X86_EFLAGS_TF' set is emulated by the emulator (e.g., using the 'force emulation' prefix), the check for pending debug exceptions during VM entry would fail, as #UD clears the pending debug exceptions. Therefore, set the 'BS' bit in such situations to make instruction emulation more robust. Signed-off-by: Hou Wenlong --- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/vmx/main.c | 9 +++++++++ arch/x86/kvm/vmx/vmx.c | 14 +++++++++----- arch/x86/kvm/vmx/x86_ops.h | 1 + arch/x86/kvm/x86.c | 7 +++++-- 6 files changed, 26 insertions(+), 7 deletions(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-= x86-ops.h index 18a5c3119e1a..3a0ab1683f17 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -50,6 +50,7 @@ KVM_X86_OP(get_gdt) KVM_X86_OP(set_gdt) KVM_X86_OP(sync_dirty_debug_regs) KVM_X86_OP(set_dr7) +KVM_X86_OP_OPTIONAL(set_pending_dbg) KVM_X86_OP(cache_reg) KVM_X86_OP(get_rflags) KVM_X86_OP(set_rflags) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 0d3cc0fc27af..a36ca751ee2e 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1765,6 +1765,7 @@ struct kvm_x86_ops { void (*set_gdt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt); void (*sync_dirty_debug_regs)(struct kvm_vcpu *vcpu); void (*set_dr7)(struct kvm_vcpu *vcpu, unsigned long value); + void (*set_pending_dbg)(struct kvm_vcpu *vcpu); void (*cache_reg)(struct kvm_vcpu *vcpu, enum kvm_reg reg); unsigned long (*get_rflags)(struct kvm_vcpu *vcpu); void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags); diff --git a/arch/x86/kvm/vmx/main.c b/arch/x86/kvm/vmx/main.c index dbab1c15b0cd..23adff73f90b 100644 --- a/arch/x86/kvm/vmx/main.c +++ b/arch/x86/kvm/vmx/main.c @@ -465,6 +465,14 @@ static void vt_set_dr7(struct kvm_vcpu *vcpu, unsigned= long val) vmx_set_dr7(vcpu, val); } =20 +static void vt_set_pending_dbg(struct kvm_vcpu *vcpu) +{ + if (is_td_vcpu(vcpu)) + return; + + vmx_set_pending_dbg(vcpu); +} + static void vt_sync_dirty_debug_regs(struct kvm_vcpu *vcpu) { /* @@ -906,6 +914,7 @@ struct kvm_x86_ops vt_x86_ops __initdata =3D { .get_gdt =3D vt_op(get_gdt), .set_gdt =3D vt_op(set_gdt), .set_dr7 =3D vt_op(set_dr7), + .set_pending_dbg =3D vt_op(set_pending_dbg), .sync_dirty_debug_regs =3D vt_op(sync_dirty_debug_regs), .cache_reg =3D vt_op(cache_reg), .get_rflags =3D vt_op(get_rflags), diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 227b45430ad8..e861a0edb3f4 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -5243,11 +5243,7 @@ static int handle_exception_nmi(struct kvm_vcpu *vcp= u) */ if (is_icebp(intr_info)) WARN_ON(!skip_emulated_instruction(vcpu)); - else if ((vmx_get_rflags(vcpu) & X86_EFLAGS_TF) && - (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & - (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS))) - vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, - vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS) | DR6_BS); + vmx_set_pending_dbg(vcpu); =20 kvm_queue_exception_p(vcpu, DB_VECTOR, dr6); return 1; @@ -5554,6 +5550,14 @@ void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned lon= g val) vmcs_writel(GUEST_DR7, val); } =20 +void vmx_set_pending_dbg(struct kvm_vcpu *vcpu) +{ + if ((vmx_get_rflags(vcpu) & X86_EFLAGS_TF) && + vmx_get_interrupt_shadow(vcpu)) + vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, + vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS) | DR6_BS); +} + static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu) { kvm_apic_update_ppr(vcpu); diff --git a/arch/x86/kvm/vmx/x86_ops.h b/arch/x86/kvm/vmx/x86_ops.h index 2b3424f638db..2913648cfe4f 100644 --- a/arch/x86/kvm/vmx/x86_ops.h +++ b/arch/x86/kvm/vmx/x86_ops.h @@ -75,6 +75,7 @@ void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *= dt); void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt); void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val); void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val); +void vmx_set_pending_dbg(struct kvm_vcpu *vcpu); void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu); void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg); unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 83960214d5d8..464e9649cb54 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -9250,10 +9250,13 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, = gpa_t cr2_or_gpa, if (ctxt->is_branch) kvm_pmu_branch_retired(vcpu); kvm_rip_write(vcpu, ctxt->eip); - if (r && (ctxt->tf || (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP))) + __kvm_set_rflags(vcpu, ctxt->eflags); + if (r && (ctxt->tf || (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP))) { r =3D kvm_vcpu_do_singlestep(vcpu); + if (r) + kvm_x86_call(set_pending_dbg)(vcpu); + } kvm_x86_call(update_emulated_instruction)(vcpu); - __kvm_set_rflags(vcpu, ctxt->eflags); } =20 /* --=20 2.31.1 From nobody Thu Oct 2 22:40:03 2025 Received: from out198-20.us.a.mail.aliyun.com (out198-20.us.a.mail.aliyun.com [47.90.198.20]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9413424728E; Wed, 10 Sep 2025 02:55:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=47.90.198.20 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757472913; cv=none; b=RIgOk5EwB8CcRapV6oMfNnB9PrrFJIBVGFtRh9sW8QEjTPfjBfa5IvoaevqsCEENLirKL4Ql24PMSitVjLMtGiS0y1Rruwr0oRhsrW8ZbfPKoVH9InGmFUo+KRv0aFbTNHx9qRdRIScRe57j9SaYRXjhV1iV/KR3ZO/q89E4nGA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757472913; c=relaxed/simple; bh=2ARSwHb0F7d03WhyJZCkYnd61/DkGRfq+i6rjunIz38=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=RlafJ/9BlNNddQjeiYhq/5XmB8q8vG0FVi3C3A15kJ+2Pe/6S5aQsPqg1zofOYvfMi52W2YuT8rT5fQ6596XWQc7V6seSBgknEJX/3HClAnjKBko59Rgnhq1/0kAQX+AdNsCu1vFNMjT7WwdBf/f/FeftcB+QRxZccDZ6i2xxEA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=antgroup.com; spf=pass smtp.mailfrom=antgroup.com; dkim=pass (1024-bit key) header.d=antgroup.com header.i=@antgroup.com header.b=Xg2y/RGc; arc=none smtp.client-ip=47.90.198.20 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=antgroup.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=antgroup.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=antgroup.com header.i=@antgroup.com header.b="Xg2y/RGc" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=antgroup.com; s=default; t=1757472892; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=/bBvVN2boAr4G21eP2LiBWEfhWBiFGpA3TTPe04AoTc=; b=Xg2y/RGc+i+fRSIYg9+XWTzbR9FQTRDsJzRbek7USw7HadAnnycJRsYfqYTe7ljdB3eCsz5v5RmAG82kTwjXyaPkTsWt5iByHMOQMVQaTL773oL1ha9iNGjzuUIwYTJxBfClsDaDI0HTKy16lsXCjqv6Bu+UQ1FI3PdXe6lXqRQ= Received: from localhost(mailfrom:houwenlong.hwl@antgroup.com fp:SMTPD_---.ebZ4e-x_1757472570 cluster:ay29) by smtp.aliyun-inc.com; Wed, 10 Sep 2025 10:49:30 +0800 From: Hou Wenlong To: kvm@vger.kernel.org Cc: Lai Jiangshan , Sean Christopherson , Paolo Bonzini , Shuah Khan , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 6/7] KVM: selftests: Verify guest debug DR7.GD checking during instruction emulation Date: Wed, 10 Sep 2025 10:49:18 +0800 Message-Id: <0fc4f9d4aa0b1ed41cdeea1788d1d8907326d9b4.1757416809.git.houwenlong.hwl@antgroup.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Similar to the global disable test case in x86's debug_regs test, use 'KVM_FEP' to trigger instruction emulation in order to verify the guest debug DR7.GD checking during instruction emulation. Signed-off-by: Hou Wenlong --- tools/testing/selftests/kvm/x86/debug_regs.c | 25 +++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/x86/debug_regs.c b/tools/testing/s= elftests/kvm/x86/debug_regs.c index 2d814c1d1dc4..ba80b77c2869 100644 --- a/tools/testing/selftests/kvm/x86/debug_regs.c +++ b/tools/testing/selftests/kvm/x86/debug_regs.c @@ -19,6 +19,7 @@ uint32_t guest_value; =20 extern unsigned char sw_bp, hw_bp, write_data, ss_start, bd_start; +extern unsigned char fep_bd_start; =20 static void guest_code(void) { @@ -64,6 +65,12 @@ static void guest_code(void) =20 /* DR6.BD test */ asm volatile("bd_start: mov %%dr0, %%rax" : : : "rax"); + + if (is_forced_emulation_enabled) { + /* DR6.BD test for emulation */ + asm volatile(KVM_FEP "fep_bd_start: mov %%dr0, %%rax" : : : "rax"); + } + GUEST_DONE(); } =20 @@ -185,7 +192,7 @@ int main(void) target_dr6); } =20 - /* Finally test global disable */ + /* test global disable */ memset(&debug, 0, sizeof(debug)); debug.control =3D KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP; debug.arch.debugreg[7] =3D 0x400 | DR7_GD; @@ -202,6 +209,22 @@ int main(void) run->debug.arch.pc, target_rip, run->debug.arch.dr6, target_dr6); =20 + /* test global disable in emulation */ + if (is_forced_emulation_enabled) { + /* Skip the 3-bytes "mov dr0" */ + vcpu_skip_insn(vcpu, 3); + vcpu_run(vcpu); + TEST_ASSERT(run->exit_reason =3D=3D KVM_EXIT_DEBUG && + run->debug.arch.exception =3D=3D DB_VECTOR && + run->debug.arch.pc =3D=3D CAST_TO_RIP(fep_bd_start) && + run->debug.arch.dr6 =3D=3D target_dr6, + "DR7.GD: exit %d exception %d rip 0x%llx " + "(should be 0x%llx) dr6 0x%llx (should be 0x%llx)", + run->exit_reason, run->debug.arch.exception, + run->debug.arch.pc, target_rip, run->debug.arch.dr6, + target_dr6); + } + /* Disable all debug controls, run to the end */ memset(&debug, 0, sizeof(debug)); vcpu_guest_debug_set(vcpu, &debug); --=20 2.31.1 From nobody Thu Oct 2 22:40:03 2025 Received: from out198-20.us.a.mail.aliyun.com (out198-20.us.a.mail.aliyun.com [47.90.198.20]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C604925B2F4; Wed, 10 Sep 2025 03:05:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=47.90.198.20 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757473531; cv=none; b=A5sg+dCG4axTr8y/+u9GZfVDg3xS9yRzbmmSlU+K+7Bw5ZTj4VBK7+eN8NFT0Qcijo6UYX4kAEfyoaj/q8tJLNzUk/kMD4Pr/rLpg/t93ZjTy+COW3Ksl7ldXPYfjYpJiqZ98kwiS2aU9fU7tFExOD/2ok3fXoWohHgXMuOx6jg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757473531; c=relaxed/simple; bh=OPf2QR2k6DAiKqVX0oVySRjxE6j2hrfdjMNsYF+M5Xs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=fC/Vk1OZkY0ihNrpe4SIYBIvXIapWcQnvkucGqfFbxZ03+vDYRREzL4r36KwTW2r+5/b8gz2hYPw7llcbog1ttaeWcYFrytZcvJFAlCTumctZIDpeXoB/rHr+1wIJZ8mWJbsob5OX5zlTJYYgLBcVUIj79WQV9KtrbbLrJgC1TE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=antgroup.com; spf=pass smtp.mailfrom=antgroup.com; dkim=pass (1024-bit key) header.d=antgroup.com header.i=@antgroup.com header.b=V63k7WyG; arc=none smtp.client-ip=47.90.198.20 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=antgroup.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=antgroup.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=antgroup.com header.i=@antgroup.com header.b="V63k7WyG" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=antgroup.com; s=default; t=1757473514; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=E93cY9FHGBGF8PLSftp1VfXTr3kQA09+IbltBz6T4yc=; b=V63k7WyG1wUTbdCfT7qVAta4EaGh01O9+eyOwslzy6DaWNpodzj4qOcmDF7XdCkdK2RpdfYxlw36TG05AW0tZUt2jN9F6IJrWMUMaHXUL/ZYKjY1UBRZ0dcVfZyxX2uDGR0Q+AZveTJFbmGHH+3e/pe/DrnMHmj4oXWNDEfeWn4= Received: from localhost(mailfrom:houwenlong.hwl@antgroup.com fp:SMTPD_---.ebeulmh_1757472571 cluster:ay29) by smtp.aliyun-inc.com; Wed, 10 Sep 2025 10:49:31 +0800 From: Hou Wenlong To: kvm@vger.kernel.org Cc: Lai Jiangshan , Sean Christopherson , Paolo Bonzini , Shuah Khan , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 7/7] KVM: selftests: Verify 'BS' bit checking in pending debug exception during VM entry Date: Wed, 10 Sep 2025 10:49:19 +0800 Message-Id: <3c0686934fc33ebb484aa5cc71443a22504df7ca.1757416809.git.houwenlong.hwl@antgroup.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In the x86's debug_regs test, add a test case to cover the scenario where single-step with STI in VMX sets the 'BS' bit in pending debug exceptions for #DB interception and instruction emulation in both cases. Signed-off-by: Hou Wenlong --- .../selftests/kvm/include/x86/processor.h | 3 +- tools/testing/selftests/kvm/x86/debug_regs.c | 41 +++++++++++++++++-- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/kvm/include/x86/processor.h b/tools/te= sting/selftests/kvm/include/x86/processor.h index 488d516c4f6f..f5827cca813e 100644 --- a/tools/testing/selftests/kvm/include/x86/processor.h +++ b/tools/testing/selftests/kvm/include/x86/processor.h @@ -34,7 +34,8 @@ extern uint64_t guest_tsc_khz; =20 #define NMI_VECTOR 0x02 =20 -#define X86_EFLAGS_FIXED (1u << 1) +#define X86_EFLAGS_FIXED (1u << 1) +#define X86_EFLAGS_TF (1u << 8) =20 #define X86_CR4_VME (1ul << 0) #define X86_CR4_PVI (1ul << 1) diff --git a/tools/testing/selftests/kvm/x86/debug_regs.c b/tools/testing/s= elftests/kvm/x86/debug_regs.c index ba80b77c2869..60dea0116b21 100644 --- a/tools/testing/selftests/kvm/x86/debug_regs.c +++ b/tools/testing/selftests/kvm/x86/debug_regs.c @@ -15,11 +15,31 @@ =20 #define IRQ_VECTOR 0xAA =20 +#define CAST_TO_RIP(v) ((unsigned long long)&(v)) + /* For testing data access debug BP */ uint32_t guest_value; =20 extern unsigned char sw_bp, hw_bp, write_data, ss_start, bd_start; -extern unsigned char fep_bd_start; +extern unsigned char fep_bd_start, fep_sti_start, fep_sti_end; + +static void guest_db_handler(struct ex_regs *regs) +{ + static int count; + unsigned long target_rips[2] =3D { + CAST_TO_RIP(fep_sti_start), + CAST_TO_RIP(fep_sti_end), + }; + + __GUEST_ASSERT(regs->rip =3D=3D target_rips[count], "STI: unexpected rip = 0x%lx (should be 0x%lx)", + regs->rip, target_rips[count]); + regs->rflags &=3D ~X86_EFLAGS_TF; + count++; +} + +static void guest_irq_handler(struct ex_regs *regs) +{ +} =20 static void guest_code(void) { @@ -69,13 +89,25 @@ static void guest_code(void) if (is_forced_emulation_enabled) { /* DR6.BD test for emulation */ asm volatile(KVM_FEP "fep_bd_start: mov %%dr0, %%rax" : : : "rax"); + + /* pending debug exceptions for emulation */ + asm volatile("pushf\n\t" + "orq $" __stringify(X86_EFLAGS_TF) ", (%rsp)\n\t" + "popf\n\t" + "sti\n\t" + "fep_sti_start:" + "cli\n\t" + "pushf\n\t" + "orq $" __stringify(X86_EFLAGS_TF) ", (%rsp)\n\t" + "popf\n\t" + KVM_FEP "sti\n\t" + "fep_sti_end:" + "cli\n\t"); } =20 GUEST_DONE(); } =20 -#define CAST_TO_RIP(v) ((unsigned long long)&(v)) - static void vcpu_skip_insn(struct kvm_vcpu *vcpu, int insn_len) { struct kvm_regs regs; @@ -110,6 +142,9 @@ int main(void) vm =3D vm_create_with_one_vcpu(&vcpu, guest_code); run =3D vcpu->run; =20 + vm_install_exception_handler(vm, DB_VECTOR, guest_db_handler); + vm_install_exception_handler(vm, IRQ_VECTOR, guest_irq_handler); + /* Test software BPs - int3 */ memset(&debug, 0, sizeof(debug)); debug.control =3D KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP; --=20 2.31.1