From nobody Tue Dec 16 00:38:10 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 13EE417A2E1 for ; Thu, 22 May 2025 00:56:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747875378; cv=none; b=ZeXRVAUO7Q8JMSH3zlEx5bLTtQoz0K2nq+8/Qp6T1qlAI5VhZ84BYi/y6Us6Yl1xICfsgy7s34N+urdZTuQl/aH+Ed1pfrlYM/JEZL6RYTB56yS6wgptSPeAiX4Hi64Nno/xXFaLScyRumxdDTB+4ay5cE7hUPjwfvOC7lbo3kk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747875378; c=relaxed/simple; bh=qVt1iIPPogfcBBlIDh5BDoYfefDZoeCnXCt+00WLO7c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=J/Fr7Hl5z/wcXHYKen7UdxkHAOSoziFQJT0yClgzVcjAl27UqUPKGy0pSzmPYDko5+pBNpY1Ws+URzHrBsgqNvGs81o7p4rsb7Hh7bPf0cTpjIzQEnWsQ0bdibloMd8Klx1Fu0cgoK68ZAuWYcfN94kd1X1SqMtzQeeNc5mYZl4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Stq/0XO0; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Stq/0XO0" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1747875374; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=nr3LJuaZ3sTnYMRa12I7e2LfVKiux5OI0/JCWBFGFhM=; b=Stq/0XO0kU16Pcj9gn6x2tMeEU5AtFPPhdIU3NTMFOHxYGV1O0WhbjNmY2CRYvwrDNwjcG luELLxGahcLo6CwIo0QyJ/r3Jl5e55rGWfB7YfR5CX4p7dSQk2A8JgIAn1f3Dh93hyHtCz wx3z8VuC6oFkN/52SExQdPGdxWOUY2w= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-466-pO8D-c7QPqOhPzjgjpMNpg-1; Wed, 21 May 2025 20:56:13 -0400 X-MC-Unique: pO8D-c7QPqOhPzjgjpMNpg-1 X-Mimecast-MFC-AGG-ID: pO8D-c7QPqOhPzjgjpMNpg_1747875371 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 891CE1956089; Thu, 22 May 2025 00:56:11 +0000 (UTC) Received: from intellaptop.lan (unknown [10.22.80.5]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 63AC419560B7; Thu, 22 May 2025 00:56:09 +0000 (UTC) From: Maxim Levitsky To: kvm@vger.kernel.org Cc: "H. Peter Anvin" , Thomas Gleixner , Sean Christopherson , Dave Hansen , Borislav Petkov , Ingo Molnar , linux-kernel@vger.kernel.org, x86@kernel.org, Paolo Bonzini , Maxim Levitsky Subject: [PATCH v5 5/5] KVM: VMX: preserve DEBUGCTLMSR_FREEZE_IN_SMM Date: Wed, 21 May 2025 20:55:55 -0400 Message-ID: <20250522005555.55705-6-mlevitsk@redhat.com> In-Reply-To: <20250522005555.55705-1-mlevitsk@redhat.com> References: <20250522005555.55705-1-mlevitsk@redhat.com> 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 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 Content-Type: text/plain; charset="utf-8" Pass through the host's DEBUGCTL.DEBUGCTLMSR_FREEZE_IN_SMM to the guest GUEST_IA32_DEBUGCTL without the guest seeing this value. Since the value of the host DEBUGCTL can in theory change between VM runs, check if has changed, and if yes, then reload the GUEST_IA32_DEBUGCTL with the new value. Signed-off-by: Maxim Levitsky --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/vmx/vmx.c | 6 +++++- arch/x86/kvm/x86.c | 7 +++++-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 32ed568babcf..6bbde18a5783 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1674,6 +1674,7 @@ static inline u16 kvm_lapic_irq_dest_mode(bool dest_m= ode_logical) enum kvm_x86_run_flags { KVM_RUN_FORCE_IMMEDIATE_EXIT =3D BIT(0), KVM_RUN_LOAD_GUEST_DR6 =3D BIT(1), + KVM_RUN_LOAD_DEBUGCTL =3D BIT(2), }; =20 struct kvm_x86_ops { diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index cfab76b40780..c70fe7cbede6 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -2196,12 +2196,13 @@ u64 vmx_get_supported_debugctl(struct kvm_vcpu *vcp= u, bool host_initiated) =20 void vmx_guest_debugctl_write(struct kvm_vcpu *vcpu, u64 val) { + val |=3D vcpu->arch.host_debugctl & DEBUGCTLMSR_FREEZE_IN_SMM; vmcs_write64(GUEST_IA32_DEBUGCTL, val); } =20 u64 vmx_guest_debugctl_read(void) { - return vmcs_read64(GUEST_IA32_DEBUGCTL); + return vmcs_read64(GUEST_IA32_DEBUGCTL) & ~DEBUGCTLMSR_FREEZE_IN_SMM; } =20 /* @@ -7380,6 +7381,9 @@ fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu, u64 ru= n_flags) if (run_flags & KVM_RUN_LOAD_GUEST_DR6) set_debugreg(vcpu->arch.dr6, 6); =20 + if (run_flags & KVM_RUN_LOAD_DEBUGCTL) + vmx_guest_debugctl_write(vcpu, vmx_guest_debugctl_read()); + /* * Refresh vmcs.HOST_CR3 if necessary. This must be done immediately * prior to VM-Enter, as the kernel may load a new ASID (PCID) any time diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 38875a38be52..3663cd6721ae 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10752,7 +10752,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) dm_request_for_irq_injection(vcpu) && kvm_cpu_accept_dm_intr(vcpu); fastpath_t exit_fastpath; - u64 run_flags; + u64 run_flags, debug_ctl; =20 bool req_immediate_exit =3D false; =20 @@ -11024,7 +11024,10 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) set_debugreg(0, 7); } =20 - vcpu->arch.host_debugctl =3D get_debugctlmsr(); + debug_ctl =3D get_debugctlmsr(); + if (!vcpu->arch.guest_state_protected && (debug_ctl !=3D vcpu->arch.host_= debugctl)) + run_flags |=3D KVM_RUN_LOAD_DEBUGCTL; + vcpu->arch.host_debugctl =3D debug_ctl; =20 guest_timing_enter_irqoff(); =20 --=20 2.46.0