From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 DA6A232144D for ; Thu, 14 Aug 2025 16:12:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755187936; cv=none; b=RYrJxObRjsXBe8sqw9jwUQQf/xZxQlZjREey2zvN4FEc9Xh9OVE5rrgjfnYrsedMTlRdFa2KH8D+NpRg/fNgvhWV8mpEWS6c9IeCHFTeCL9z963akw6ecPIbz/Pd3EnFL61Xxmx2d+YRh8AQ8ZtTLFyyx2x9hf6db8tSrYGeebY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755187936; c=relaxed/simple; bh=dgM+1mMXqwlB9XBcmWRamko5eu3889pd7dc8/CdujKU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=BBKOazljNxCcTf0UTgjWeIjMIsyFvfk9GEOfiHeYLbTKsKS9XdQJ1nRfwcKGehgquqLNDVt0hiFcLUos/FWd05JyvdGIj6CED+ZSJfYKaE8895WzLiC1AS01YsBGBZqe0ILoEhC2wYabyH/Hys8WTUGZf49CcnrzmxSUCelzq+M= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=QUJpZ+Og; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="QUJpZ+Og" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E2926C4CEED; Thu, 14 Aug 2025 16:12:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755187936; bh=dgM+1mMXqwlB9XBcmWRamko5eu3889pd7dc8/CdujKU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QUJpZ+OggsyIb/fByil49HyG+OVLgEPzXOrfoWmquq5zzZI4xR7Hz+lFBaL2GbuSl rF01VvDxIYUXW9L/+sc67ro/yP/U8NrOObC0eDI0qA28BhNxOVRen4HP09bVeBDtsO yz9DMVefktsflTj05/5lnhWs6Lr1kcJyeEn5+KqSAZaoYvFKsFBgUVLCoAwdf3qVSn eU2Bb29W9pj/DfysWNrkR32717w/huQUtxPv4Btxbwc+abBUI/dJ2CsvPfwfHrD9so wRS1IGuWsR+AAQ6J71oXGx3RenIny6CUlV60uN047FhHPuR5GRy8ZMZzUo+LvaLCUQ 7fC2zP7G6gBPw== From: Sasha Levin To: stable@vger.kernel.org Cc: Sean Christopherson , Dapeng Mi , Sasha Levin Subject: [PATCH 6.16.y 3/6] KVM: VMX: Extract checking of guest's DEBUGCTL into helper Date: Thu, 14 Aug 2025 12:12:09 -0400 Message-Id: <20250814161212.2107674-3-sashal@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250814161212.2107674-1-sashal@kernel.org> References: <2025081231-vengeful-creasing-d789@gregkh> <20250814161212.2107674-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Sean Christopherson [ Upstream commit 8a4351ac302cd8c19729ba2636acfd0467c22ae8 ] Move VMX's logic to check DEBUGCTL values into a standalone helper so that the code can be used by nested VM-Enter to apply the same logic to the value being loaded from vmcs12. KVM needs to explicitly check vmcs12->guest_ia32_debugctl on nested VM-Enter, as hardware may support features that KVM does not, i.e. relying on hardware to detect invalid guest state will result in false negatives. Unfortunately, that means applying KVM's funky suppression of BTF and LBR to vmcs12 so as not to break existing guests. No functional change intended. Reviewed-by: Dapeng Mi Link: https://lore.kernel.org/r/20250610232010.162191-6-seanjc@google.com Signed-off-by: Sean Christopherson Stable-dep-of: 6b1dd26544d0 ("KVM: VMX: Preserve host's DEBUGCTLMSR_FREEZE_IN_SMM while running the guest") Signed-off-by: Sasha Levin --- arch/x86/kvm/vmx/vmx.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 309b0b400a5a..90bed99dfc12 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -2189,6 +2189,19 @@ static u64 vmx_get_supported_debugctl(struct kvm_vcpu *vcpu, bool host_initiated return debugctl; } +static bool vmx_is_valid_debugctl(struct kvm_vcpu *vcpu, u64 data, + bool host_initiated) +{ + u64 invalid; + + invalid = data & ~vmx_get_supported_debugctl(vcpu, host_initiated); + if (invalid & (DEBUGCTLMSR_BTF | DEBUGCTLMSR_LBR)) { + kvm_pr_unimpl_wrmsr(vcpu, MSR_IA32_DEBUGCTLMSR, data); + invalid &= ~(DEBUGCTLMSR_BTF | DEBUGCTLMSR_LBR); + } + return !invalid; +} + /* * Writes msr value into the appropriate "register". * Returns 0 on success, non-0 otherwise. @@ -2257,19 +2270,12 @@ int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) } vmcs_writel(GUEST_SYSENTER_ESP, data); break; - case MSR_IA32_DEBUGCTLMSR: { - u64 invalid; - - invalid = data & ~vmx_get_supported_debugctl(vcpu, msr_info->host_initiated); - if (invalid & (DEBUGCTLMSR_BTF|DEBUGCTLMSR_LBR)) { - kvm_pr_unimpl_wrmsr(vcpu, msr_index, data); - data &= ~(DEBUGCTLMSR_BTF|DEBUGCTLMSR_LBR); - invalid &= ~(DEBUGCTLMSR_BTF|DEBUGCTLMSR_LBR); - } - - if (invalid) + case MSR_IA32_DEBUGCTLMSR: + if (!vmx_is_valid_debugctl(vcpu, data, msr_info->host_initiated)) return 1; + data &= vmx_get_supported_debugctl(vcpu, msr_info->host_initiated); + if (is_guest_mode(vcpu) && get_vmcs12(vcpu)->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) get_vmcs12(vcpu)->guest_ia32_debugctl = data; @@ -2279,7 +2285,6 @@ int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) (data & DEBUGCTLMSR_LBR)) intel_pmu_create_guest_lbr_event(vcpu); return 0; - } case MSR_IA32_BNDCFGS: if (!kvm_mpx_supported() || (!msr_info->host_initiated && -- 2.39.5