From nobody Fri Dec 19 16:05:17 2025 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 326CD27561F for ; Thu, 27 Feb 2025 22:24:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740695058; cv=none; b=HdSEBrLpOGMQHt7unL9xxcr/sgNyHQEt7jgV+0PukWnFiEElk+KwOZwFpfWuEF9MFZrFRSFnZUQPBTR8P8c4bVph0SP6TSBd29Ea/EKbA+xWAFHhSGa8sbQjosFKZb4YRet4Fk1dsb7EoyYSHoIGcJcLcro2tMMOmRfn0EsQ6f4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740695058; c=relaxed/simple; bh=zm2QaR360KIl6iSEbDz2zLo3X3lN7lh4lpL7RxCk81I=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Jw0nOnV6spyDVvCKgQ63opMYRnlWNGeun47E+c1f3TrTR2pBXeLQ70d2Q05sCGyY+6AUskUuRpFwVrA12yP19m8rO1GKiwDQNEAKQdjlOqDqvJfteXXAbtFxCG5l3d9G1iiglFdRy8i9j9lKlZS1WEvNFn6dRx2DAQUI2pypAro= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=tUaPzjcW; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="tUaPzjcW" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-22367f4e9b9so5298235ad.0 for ; Thu, 27 Feb 2025 14:24:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740695055; x=1741299855; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:reply-to:from:to:cc:subject:date :message-id:reply-to; bh=0b+q4LTl32D7RG/ClIETiGyEI98jipo9UuVZ1dxYatM=; b=tUaPzjcWqgl2XhWRsI+XfcNA41k5CIfDP0i5RHMCaHBH8zxT82YAzF5I2C8Liq57ek 9bZju1xT6lVnFeo7q+4dkRxrcs76b1QFxmY/edJv8OAjjUfhBnGnFtxj2JGJWabQABX/ 6dRmgsCWxtlD9GdEHJ3wvjMXCw9HjXaKDURNCJRIywN8p+Q5VZWUcwvYZfpnhsyovArQ RCDdhsvWG9lFMA+8nUEpUGyxxXqt5z3vexQjKFwmWGpk/RCwXLV+EIUJ6l9oztSD/e+b YK93z13N1GGYvg+EOuuje783GDKtkTRSStnodsgBdKcHZylEcpgkDI0iyaJlFu5+psue Pqew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740695055; x=1741299855; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:reply-to:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=0b+q4LTl32D7RG/ClIETiGyEI98jipo9UuVZ1dxYatM=; b=Fw+0xogvMEsy88x2JvSOQFpCD1Iu2C+cJ/UKxJbZhvX2Py8hL0thq/8iXbdBNzOpx+ tPNJpVc0/BYZyJGEL5Ds6v4zfENdgcZzNiOrqvaje6Me4v6LO4IeEJ/ehSXP/B7Evo6u r4/Gtppeb5l0N7bVgld/LA8ZluDPUhoSkEMdFYzh8kCsrB1t9owVZ9An3oViqorYP78+ APObGkVV/GYikfd6on9bM1NOUhcGL5FglIjTtX5To9lM5y2ZWL6yS/uGBVAZDGbxMhkK Rx1s2lECtBLqif7LO1XTb8cchtOlqc/TQTLv0JBmtMl/nYQb2Qc0Mxab69xrxNy2Xcwg XEbA== X-Forwarded-Encrypted: i=1; AJvYcCV4bSPOzX0ZKe44JghAWIt6e9oYQdI34Cz6M10YycrPcfdSxXDHbptlYIDi6Ew2jJssdEXGmba7XIOfHj4=@vger.kernel.org X-Gm-Message-State: AOJu0YwECS0c4Y+Gljj8iBC0BLA3mAanACTZaOqZtSwW4fJG+wvbiaGj jFp566Ktt8uFmB6fXrQunh+QV2o5jTKc/akeHQgQptBwaxxU5vHDa/mkHjOXJegl5p86EC/bBZ/ TUQ== X-Google-Smtp-Source: AGHT+IEVfhWJbCHzhv945nE23qQtlQGHQDAfLrpb39fV+tlQVN+EJ3Y0HE+Vr2OulijTWrxhfARiJuURpvQ= X-Received: from pfld22.prod.google.com ([2002:a05:6a00:1996:b0:725:e4b6:901f]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:4b53:b0:730:7970:1f8f with SMTP id d2e1a72fcca58-734ac376faamr1574376b3a.12.1740695055322; Thu, 27 Feb 2025 14:24:15 -0800 (PST) Reply-To: Sean Christopherson Date: Thu, 27 Feb 2025 14:24:06 -0800 In-Reply-To: <20250227222411.3490595-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250227222411.3490595-1-seanjc@google.com> X-Mailer: git-send-email 2.48.1.711.g2feabab25a-goog Message-ID: <20250227222411.3490595-2-seanjc@google.com> Subject: [PATCH v3 1/6] KVM: SVM: Drop DEBUGCTL[5:2] from guest's effective value From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Ravi Bangoria , Xiaoyao Li , rangemachine@gmail.com, whanos@sergal.fun Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Drop bits 5:2 from the guest's effective DEBUGCTL value, as AMD changed the architectural behavior of the bits and broke backwards compatibility. On CPUs without BusLockTrap (or at least, in APMs from before ~2023), bits 5:2 controlled the behavior of external pins: Performance-Monitoring/Breakpoint Pin-Control (PBi)=E2=80=94Bits 5:2, rea= d/write. Software uses thesebits to control the type of information reported by the four external performance-monitoring/breakpoint pins on the processor. When a PBi bit is cleared to 0, the corresponding external pin (BPi) reports performance-monitor information. When a PBi bit is set to 1, the corresponding external pin (BPi) reports breakpoint information. With the introduction of BusLockTrap, presumably to be compatible with Intel CPUs, AMD redefined bit 2 to be BLCKDB: Bus Lock #DB Trap (BLCKDB)=E2=80=94Bit 2, read/write. Software sets this = bit to enable generation of a #DB trap following successful execution of a bus lock when CPL is > 0. and redefined bits 5:3 (and bit 6) as "6:3 Reserved MBZ". Ideally, KVM would treat bits 5:2 as reserved. Defer that change to a feature cleanup to avoid breaking existing guest in LTS kernels. For now, drop the bits to retain backwards compatibility (of a sort). Note, dropping bits 5:2 is still a guest-visible change, e.g. if the guest is enabling LBRs *and* the legacy PBi bits, then the state of the PBi bits is visible to the guest, whereas now the guest will always see '0'. Reported-by: Ravi Bangoria Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/svm.c | 12 ++++++++++++ arch/x86/kvm/svm/svm.h | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index b8aa0f36850f..2280bd1d0863 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -3165,6 +3165,18 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct= msr_data *msr) kvm_pr_unimpl_wrmsr(vcpu, ecx, data); break; } + + /* + * AMD changed the architectural behavior of bits 5:2. On CPUs + * without BusLockTrap, bits 5:2 control "external pins", but + * on CPUs that support BusLockDetect, bit 2 enables BusLockTrap + * and bits 5:3 are reserved-to-zero. Sadly, old KVM allowed + * the guest to set bits 5:2 despite not actually virtualizing + * Performance-Monitoring/Breakpoint external pins. Drop bits + * 5:2 for backwards compatibility. + */ + data &=3D ~GENMASK(5, 2); + if (data & DEBUGCTL_RESERVED_BITS) return 1; =20 diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 5b159f017055..f573548b7b41 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -582,7 +582,7 @@ static inline bool is_vnmi_enabled(struct vcpu_svm *svm) /* svm.c */ #define MSR_INVALID 0xffffffffU =20 -#define DEBUGCTL_RESERVED_BITS (~(0x3fULL)) +#define DEBUGCTL_RESERVED_BITS (~(DEBUGCTLMSR_BTF | DEBUGCTLMSR_LBR)) =20 extern bool dump_invalid_vmcb; =20 --=20 2.48.1.711.g2feabab25a-goog From nobody Fri Dec 19 16:05:17 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EEAEF27FE70 for ; Thu, 27 Feb 2025 22:24:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740695059; cv=none; b=Gy423NTdP4DIkEtDGYwsQswpTGsVedqy+Cbi7h7mnwZhuDO+pHzka1+FJqiPmXASIwYHxBki53MER5Af+ismg6FT26aXg9Pf8ttvnyD0xzLM0Kip2vjVsVWgzI6Z28OMJJtQT0onvWD3RhQsXmGTkNqxsP0pP3N+nJlJhB+h/50= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740695059; c=relaxed/simple; bh=A+xW3ArMj2O0ny2rQkTwOSaqGEXJoe2ywH7RHyWWChE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RUyeFl2tmyuKIm5BxVc0oTKdQJXiNA3TKMp20MadEBSnv0zUDSqDg+awElsetFHhNJPL0XzUEGZnpakR4c9LSZQ2iotKHSc5TsxGaG0dSl5cceX9JFXjkkqlcDBoxBcevjBbJia3Z2PhE5Il1/y9fZ+rdeiZCVMTeVrjKrGfSSI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=RCJn+y4i; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="RCJn+y4i" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2fe916ba298so3124356a91.1 for ; Thu, 27 Feb 2025 14:24:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740695057; x=1741299857; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=5pcq9kolZSMyw9f1FbDzVx3m1SaU12zTFrW/2ux2I3g=; b=RCJn+y4ib0/rZXGchCSLBIGbKENG8tmMBKn7HeuRZIpnT2AKWLohcODL/Fy409Vn3+ iHyoDJOlTQlmZKirjh6Rv2+V+AcdCvQnIcTELfo5ofeEDdatXDIUUjewDvypIx1dTTCn j2g7bF+Xbx8XJIHS9r0uDcxGC/gq2ETIWFTczkXynjdhWIfmLEDlekqOKO2gpKFPwDE7 WJaVlgqpN7C3F2MAk3O6kqH/8GrAtGG5vKmqwmfi2pTIJHiNetMB9KYTxkVCi0kBatrO 64nVz4X+ZnKT0/ihQNJGvF8ZlXabsr4gPY/zqKndjWKL96cUYIjDDyFJXf9SEJUwZ4Ze 2X6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740695057; x=1741299857; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=5pcq9kolZSMyw9f1FbDzVx3m1SaU12zTFrW/2ux2I3g=; b=YGfguCjzhYcc3LYT1xKjQqnlgIgAs+dZ1LHmuZuL216Fmtm+gL/MCLFX+Dot3dNgwK mQGjbc69Du3aLL3qdviS9N8AXMb9AsDtjY3FBniYVTp1C9mMznOE3XgQ8vMs9aEjstOx B1bo6QOtCFCW+fspMy2og/87k4jSnSipRimBXPR/oE6eCzdzfSCUa043JUag2D/hKMro s2vA9f1mDd+GPpNjHHc3w6PC2OZBN+RcmmwRGqzxvNmNHPcP4AV0Hy5uC76Q+/VDpvVr sFq+rocEDQPNCDTAjIW0FlLbKYBgzjsyzHrCSvQ75KTFo+g7pGj0QuxJmEmq4KxSkYXo /n+A== X-Forwarded-Encrypted: i=1; AJvYcCUqJr9/slBAZtsxmlZWgZ+TlbCt0wVHcXjleXNuDahSa+nlC6XP+m86iZ4hytJYpUHVdq+YYHt6syOcLdU=@vger.kernel.org X-Gm-Message-State: AOJu0YyhU6Hgyq9vWs8vVr/t/5I1sSgJN4Cm1zKoXzsGOlYOSsnkpiDU W5LqsULsHjY/Rln+AlrglQl+0vmVqGrHY7wUAJ6qhJEuKCL0FJxDGItbuhPwfBSISxELjaWNcEb /wg== X-Google-Smtp-Source: AGHT+IE/05e47U/0MoTU+fc1epyaOWAkZsyeFaOdMRg65lN/797YYJNtUm1LAMgiUsF/4RFrIxCv5qp+yow= X-Received: from pjbpm13.prod.google.com ([2002:a17:90b:3c4d:b0:2ef:9b30:69d3]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3890:b0:2ee:aed2:c15c with SMTP id 98e67ed59e1d1-2febabf19a6mr1509906a91.28.1740695057184; Thu, 27 Feb 2025 14:24:17 -0800 (PST) Reply-To: Sean Christopherson Date: Thu, 27 Feb 2025 14:24:07 -0800 In-Reply-To: <20250227222411.3490595-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250227222411.3490595-1-seanjc@google.com> X-Mailer: git-send-email 2.48.1.711.g2feabab25a-goog Message-ID: <20250227222411.3490595-3-seanjc@google.com> Subject: [PATCH v3 2/6] KVM: SVM: Suppress DEBUGCTL.BTF on AMD From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Ravi Bangoria , Xiaoyao Li , rangemachine@gmail.com, whanos@sergal.fun Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Mark BTF as reserved in DEBUGCTL on AMD, as KVM doesn't actually support BTF, and fully enabling BTF virtualization is non-trivial due to interactions with the emulator, guest_debug, #DB interception, nested SVM, etc. Don't inject #GP if the guest attempts to set BTF, as there's no way to communicate lack of support to the guest, and instead suppress the flag and treat the WRMSR as (partially) unsupported. In short, make KVM behave the same on AMD and Intel (VMX already squashes BTF). Note, due to other bugs in KVM's handling of DEBUGCTL, the only way BTF has "worked" in any capacity is if the guest simultaneously enables LBRs. Reported-by: Ravi Bangoria Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/svm.c | 9 +++++++++ arch/x86/kvm/svm/svm.h | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 2280bd1d0863..b70c754686c4 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -3177,6 +3177,15 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct= msr_data *msr) */ data &=3D ~GENMASK(5, 2); =20 + /* + * Suppress BTF as KVM doesn't virtualize BTF, but there's no + * way to communicate lack of support to the guest. + */ + if (data & DEBUGCTLMSR_BTF) { + kvm_pr_unimpl_wrmsr(vcpu, MSR_IA32_DEBUGCTLMSR, data); + data &=3D ~DEBUGCTLMSR_BTF; + } + if (data & DEBUGCTL_RESERVED_BITS) return 1; =20 diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index f573548b7b41..798c11e755e2 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -582,7 +582,7 @@ static inline bool is_vnmi_enabled(struct vcpu_svm *svm) /* svm.c */ #define MSR_INVALID 0xffffffffU =20 -#define DEBUGCTL_RESERVED_BITS (~(DEBUGCTLMSR_BTF | DEBUGCTLMSR_LBR)) +#define DEBUGCTL_RESERVED_BITS (~DEBUGCTLMSR_LBR) =20 extern bool dump_invalid_vmcb; =20 --=20 2.48.1.711.g2feabab25a-goog From nobody Fri Dec 19 16:05:17 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9FFA527FE90 for ; Thu, 27 Feb 2025 22:24:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740695061; cv=none; b=VlXAjq1DuhIr828tX42r5il1pfMDSZA4oO+I8Ke0jwVbiGwEhGuZExuSqJYC+X9zDUPRW2CSXFwhVvtNRHhEs+KzW7s8TvWOwyiONNA98MkVXcjYbb8jCvcJW0TrzYeypjhok+w2v+Ew56Mt76LJ7l54iUoMTEeBORFkQy0uytg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740695061; c=relaxed/simple; bh=pUJzhfVuP8YDCZyv6pEaIsEIbkiixYOoWJyO/nE7JWM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=IwEqjIY04Pjc41cu9qrjYipwnGd3qX5Fsc+3o580BAru887gP0dZZ5p0ZlLyiWf1ht6di0X9izq6Ugj4U0VWI+lqgcJEBxwgziTLj9bLOs03XMBlAhOAM9TtE1BydfV39tqN250ZEclBGCUSDWTDIw9HifY9ScFOjjPm7LFlsEo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=2PRxpsan; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="2PRxpsan" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2f83e54432dso5245772a91.2 for ; Thu, 27 Feb 2025 14:24:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740695059; x=1741299859; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=+yfVH+oFZLBrscDapM9GBCwxFCs059IFc9U134nCSeM=; b=2PRxpsanpkey1y2ME9wPe3pRF1Qua87RJYvMK/i6XVno/WyPO+0UyMnA63nbVpSCH4 gdx2ZRv+d7fEZrQBQ9tQbPHni6caJ0PqqagFOhNnB+iIpv6NXT6+VWJF46Imo1M1pxd9 nrc43kxgS3jQUOumBY+7H9NJ4G95STegElXpyAIBpNZISQKSPfrkS5LLEEu2eXkvo8L/ TJ/X/QATPLwvC3b3Bpwvvg7Vj4lw4fnl9ZIwxbpSoZjgmAVXKlhT80CI+Oz3AyCTAkRk /fMxwS+8rHo1Mm0C2hJm92WOeQRbMdX9EYJCpqZxaF+TEg/7d+WsUk0Ig4qn2xyF9oJA oknw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740695059; x=1741299859; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=+yfVH+oFZLBrscDapM9GBCwxFCs059IFc9U134nCSeM=; b=OKxE2/HWYXBLHVsWDUfJEsBthogtjelY7qnSjdeUb4+5tya/RVJLR1gPoZhWs5GNCC PPsl3gT3VPsUII66QgExGEIR8OI2iSC8tI3i6HEwcoHP1XKgW8efx2Z9TqkOHoT4ztB4 8t6jraSmi4hCta0MofSsyJqG215JTp8wqlb1l9SqoSDJQlUoib0T7sSrcFmx+d2NK9HI k0JElu1YPaLgiY3AkecAHEZkAm5sJ8OaNnIZLZcjsgCAuXIV2Hnv/RSKcdKmO87iolaU CtfBWDhxAMOHGKoPGiWmfTYS+ILQm+qIQE1Bro13mU304w78rfWaynHLFrw/l4iu64xa FlDw== X-Forwarded-Encrypted: i=1; AJvYcCXm+WPdJeJ8lPHkkM4lcdN413Aap3d+jNTy8jC9TqJ2d0NkvtQL9QWh2fhoTMIr7JglDa6PRWHjGHjLXAs=@vger.kernel.org X-Gm-Message-State: AOJu0YwPEHX56Ppk1xq/GC3+/FSylxmQ42M6NCsFCQap9Qwr7GC1RhW9 Otl2hAHlubD7TkvrgxbqOUOzQc83N16Hj5Zcy3DsJ0cW2+hb3jRky+cvP76hc1n8OVEO0hlGgS/ 4tQ== X-Google-Smtp-Source: AGHT+IEMbPI0IcnCivmO2KjUlLnmbO74NhIVMvoAV5zc96bgDzprvjGqzWCj1/wKqim/zPzkZAzCkp0qImg= X-Received: from pgbb23.prod.google.com ([2002:a63:1b17:0:b0:ad5:5920:367]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6300:6141:b0:1ee:cf13:d4b5 with SMTP id adf61e73a8af0-1f2f4e4c90amr1228447637.39.1740695059033; Thu, 27 Feb 2025 14:24:19 -0800 (PST) Reply-To: Sean Christopherson Date: Thu, 27 Feb 2025 14:24:08 -0800 In-Reply-To: <20250227222411.3490595-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250227222411.3490595-1-seanjc@google.com> X-Mailer: git-send-email 2.48.1.711.g2feabab25a-goog Message-ID: <20250227222411.3490595-4-seanjc@google.com> Subject: [PATCH v3 3/6] KVM: x86: Snapshot the host's DEBUGCTL in common x86 From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Ravi Bangoria , Xiaoyao Li , rangemachine@gmail.com, whanos@sergal.fun Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move KVM's snapshot of DEBUGCTL to kvm_vcpu_arch and take the snapshot in common x86, so that SVM can also use the snapshot. Opportunistically change the field to a u64. While bits 63:32 are reserved on AMD, not mentioned at all in Intel's SDM, and managed as an "unsigned long" by the kernel, DEBUGCTL is an MSR and therefore a 64-bit value. Reviewed-by: Xiaoyao Li Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/vmx/vmx.c | 8 ++------ arch/x86/kvm/vmx/vmx.h | 2 -- arch/x86/kvm/x86.c | 1 + 4 files changed, 4 insertions(+), 8 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 3506f497741b..02bffe6b54c8 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -781,6 +781,7 @@ struct kvm_vcpu_arch { u32 pkru; u32 hflags; u64 efer; + u64 host_debugctl; u64 apic_base; struct kvm_lapic *apic; /* kernel irqchip context */ bool load_eoi_exitmap_pending; diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index b71392989609..729c224b72dd 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -1514,16 +1514,12 @@ void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int = cpu, */ void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) { - struct vcpu_vmx *vmx =3D to_vmx(vcpu); - if (vcpu->scheduled_out && !kvm_pause_in_guest(vcpu->kvm)) shrink_ple_window(vcpu); =20 vmx_vcpu_load_vmcs(vcpu, cpu, NULL); =20 vmx_vcpu_pi_load(vcpu, cpu); - - vmx->host_debugctlmsr =3D get_debugctlmsr(); } =20 void vmx_vcpu_put(struct kvm_vcpu *vcpu) @@ -7458,8 +7454,8 @@ fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu, bool f= orce_immediate_exit) } =20 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */ - if (vmx->host_debugctlmsr) - update_debugctlmsr(vmx->host_debugctlmsr); + if (vcpu->arch.host_debugctl) + update_debugctlmsr(vcpu->arch.host_debugctl); =20 #ifndef CONFIG_X86_64 /* diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index 8b111ce1087c..951e44dc9d0e 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -340,8 +340,6 @@ struct vcpu_vmx { /* apic deadline value in host tsc */ u64 hv_deadline_tsc; =20 - unsigned long host_debugctlmsr; - /* * Only bits masked by msr_ia32_feature_control_valid_bits can be set in * msr_ia32_feature_control. FEAT_CTL_LOCKED is always included diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 58b82d6fd77c..09c3d27cc01a 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4991,6 +4991,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cp= u) =20 /* Save host pkru register if supported */ vcpu->arch.host_pkru =3D read_pkru(); + vcpu->arch.host_debugctl =3D get_debugctlmsr(); =20 /* Apply any externally detected TSC adjustments (due to suspend) */ if (unlikely(vcpu->arch.tsc_offset_adjustment)) { --=20 2.48.1.711.g2feabab25a-goog From nobody Fri Dec 19 16:05:17 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 90F5B280A3D for ; Thu, 27 Feb 2025 22:24:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740695063; cv=none; b=JBJMe3g45IseI+bIw7Ao9zPVNKcb9JJFqWRaEUq3T29eOSZz18Lqe0O3vJ9EBYl0eLRGSr1wwJTK3h3GLcGDOHhov7cPqYsv++z6mVSPJy6JQSK2WXiBMovkgRMqm4ro12f80bsQQie3Vj8fX/4taORAtqQiDwt1BMMx6rj8klY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740695063; c=relaxed/simple; bh=rM+piIbm6fPPUQU3tS/TNtc7JXxdSH4MOK9KtgfsdwA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=FMrmPqWtd4OLBHIWFVltNlxTMhe68r1gXRqjoT/EkFubn3Ah/zHqvuimU0+YJ3y4T4c31nv1feLVMgbwMg6pksHmmLsVNxvvK81sPb0pciNglmjO9Ab1SN5zt78qj0d2N/z1oEepoJ5qZ5mJkiFFa+37JuPJpdmuTezCfsQHr2g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=bZBNcmXt; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="bZBNcmXt" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2fea1685337so2716938a91.0 for ; Thu, 27 Feb 2025 14:24:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740695061; x=1741299861; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=kC2GVKQNy/500VxIVTCNd9Tr/l/Ea5Ufkhz74/uHUys=; b=bZBNcmXt4lgyDUlJQMUKsMIG58AVtq7rOXftXCRqSIlBBQGm1NoFpRsSwDG08araWK /WdyhOIO3gUs52R1xCW5X4SBtRbcMVb1dQ8vkQEew2oA3vJ7aJUvovcbR786flB20z1I xxrTpZgJwBd4oEA7Or99JUsGP4vBDEnRRHg7QsZQ0ndQNzbofYH4cSqRhEMHKG23SagM j2qg95wgr0L2ZLcGy+tmb26tJyGsVrBA2gGLfaIqXo472ZeOKhHXMJuxDG4nD1vosz27 xqIhiQpY57yh8uuS/FvUBA8o2JGoGfS92NLxz+DxoA07A2inPwElxw3+N1LzRJZ7xBVG JIXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740695061; x=1741299861; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=kC2GVKQNy/500VxIVTCNd9Tr/l/Ea5Ufkhz74/uHUys=; b=JT+Psd4ZpTuu2C2rWY2SYG2H9xr8pDr3gHsKaSI+tNMhF43CVaJ2trYKntno8vNloK oRgV/szvNgn55fccBQ+sgiqIxwCIO05VnK/IEnuMJBbiV/1sbZOQuVc1annWcyj08m2C lhFdRF8KEL7aAf1S0t1jO+ST8IX+56EXaJdcN3uFbVspoYzFDbUAnfSObuxeEWCcPG6M IGKoA6m0zqmKAkaDltkk9F1yDgkVAEHQVRkBvESN2Os83hqz4dBjP27VrW9LLrcFDrZE 9fpBWInrCxeskJYdq3pPS2qeYEB4y/bks56/hZ+jsBXHhZVS4FQbnN02hZvW98odIf9U mlZg== X-Forwarded-Encrypted: i=1; AJvYcCUx+t4P5BfwWM3brip1ypqJoHrChVdbhs7vnMpAk19rKLJx0P7kQ+T1BUSxpLsWtC1pnvbeRyAGyG19NVM=@vger.kernel.org X-Gm-Message-State: AOJu0YyCV1U/8XhZLOaE0zFE8S3bAJMkcGlot+PcZUyrgMXgqXgZsUZY J2oXBtl4DZM1O7dmF3V8bQT0QBy8rZwbVIK+tr8NHkv7wU17CuRgUSDoUaK3TWP0n61iAkURAwZ uTw== X-Google-Smtp-Source: AGHT+IHvOmBlb4arTT0QJ+YI/scwXxLdSS+NaBMhoz0+AlnE6vAOu60sbh/h88+WVTh3zJ2MPG+B9L13bHQ= X-Received: from pjbpl6.prod.google.com ([2002:a17:90b:2686:b0:2e9:38ea:ca0f]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3cc6:b0:2fa:9c9:20a3 with SMTP id 98e67ed59e1d1-2feb9ac1836mr2002841a91.0.1740695060756; Thu, 27 Feb 2025 14:24:20 -0800 (PST) Reply-To: Sean Christopherson Date: Thu, 27 Feb 2025 14:24:09 -0800 In-Reply-To: <20250227222411.3490595-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250227222411.3490595-1-seanjc@google.com> X-Mailer: git-send-email 2.48.1.711.g2feabab25a-goog Message-ID: <20250227222411.3490595-5-seanjc@google.com> Subject: [PATCH v3 4/6] KVM: SVM: Manually context switch DEBUGCTL if LBR virtualization is disabled From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Ravi Bangoria , Xiaoyao Li , rangemachine@gmail.com, whanos@sergal.fun Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Manually load the guest's DEBUGCTL prior to VMRUN (and restore the host's value on #VMEXIT) if it diverges from the host's value and LBR virtualization is disabled, as hardware only context switches DEBUGCTL if LBR virtualization is fully enabled. Running the guest with the host's value has likely been mildly problematic for quite some time, e.g. it will result in undesirable behavior if BTF diverges (with the caveat that KVM now suppresses guest BTF due to lack of support). But the bug became fatal with the introduction of Bus Lock Trap ("Detect" in kernel paralance) support for AMD (commit 408eb7417a92 ("x86/bus_lock: Add support for AMD")), as a bus lock in the guest will trigger an unexpected #DB. Note, suppressing the bus lock #DB, i.e. simply resuming the guest without injecting a #DB, is not an option. It wouldn't address the general issue with DEBUGCTL, e.g. for things like BTF, and there are other guest-visible side effects if BusLockTrap is left enabled. If BusLockTrap is disabled, then DR6.BLD is reserved-to-1; any attempts to clear it by software are ignored. But if BusLockTrap is enabled, software can clear DR6.BLD: Software enables bus lock trap by setting DebugCtl MSR[BLCKDB] (bit 2) to 1. When bus lock trap is enabled, ... The processor indicates that this #DB was caused by a bus lock by clearing DR6[BLD] (bit 11). DR6[11] previously had been defined to be always 1. and clearing DR6.BLD is "sticky" in that it's not set (i.e. lowered) by other #DBs: All other #DB exceptions leave DR6[BLD] unmodified E.g. leaving BusLockTrap enable can confuse a legacy guest that writes '0' to reset DR6. Reported-by: rangemachine@gmail.com Reported-by: whanos@sergal.fun Closes: https://bugzilla.kernel.org/show_bug.cgi?id=3D219787 Closes: https://lore.kernel.org/all/bug-219787-28872@https.bugzilla.kernel.= org%2F Cc: Ravi Bangoria Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/svm.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index b70c754686c4..78664f9b45c5 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4274,6 +4274,16 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm= _vcpu *vcpu, clgi(); kvm_load_guest_xsave_state(vcpu); =20 + /* + * Hardware only context switches DEBUGCTL if LBR virtualization is + * enabled. Manually load DEBUGCTL if necessary (and restore it after + * VM-Exit), as running with the host's DEBUGCTL can negatively affect + * guest state and can even be fatal, e.g. due to Bus Lock Detect. + */ + if (!(svm->vmcb->control.virt_ext & LBR_CTL_ENABLE_MASK) && + vcpu->arch.host_debugctl !=3D svm->vmcb->save.dbgctl) + update_debugctlmsr(svm->vmcb->save.dbgctl); + kvm_wait_lapic_expire(vcpu); =20 /* @@ -4301,6 +4311,10 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm= _vcpu *vcpu, if (unlikely(svm->vmcb->control.exit_code =3D=3D SVM_EXIT_NMI)) kvm_before_interrupt(vcpu, KVM_HANDLING_NMI); =20 + if (!(svm->vmcb->control.virt_ext & LBR_CTL_ENABLE_MASK) && + vcpu->arch.host_debugctl !=3D svm->vmcb->save.dbgctl) + update_debugctlmsr(vcpu->arch.host_debugctl); + kvm_load_host_xsave_state(vcpu); stgi(); =20 --=20 2.48.1.711.g2feabab25a-goog From nobody Fri Dec 19 16:05:17 2025 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 47BE5281354 for ; Thu, 27 Feb 2025 22:24:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740695064; cv=none; b=sRvbTclzJ8/qWEHUwBdvA+qBzD6/btD1mAh/Pv1c65/X8vq2QY2s4yTBIxP4kynq3DMpkxPkUywUi8swkGY1lcvwDgaxZmPewDXp57Xcv9gxZ/1QZ3Hu/f37ZblUalcmiQ1i4wh5WNy9IV53kfO7GKWkTyhnq/G1VsKdHRiaehs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740695064; c=relaxed/simple; bh=uaIIITJgUawWH54PuijoRuV64Rx6tfBFy6BV8graBMw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=VTTuydgzjgQvtDcJZwbbgDmJLPbLofVB0M+QfU1Sodqgqr88WOtOMoRSKJytxPul1yLA4mm9pbRQMMaJSUkbltBhMppmRfWUlI+xEAIi80QWHToFg3SQeSrUPVwPaFB4ueH+mYL4kpvZOT4T42kvlQWfwR3Ff38tEyC0GJulFjc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=abkAOfxW; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="abkAOfxW" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2235a1f8aadso15981535ad.2 for ; Thu, 27 Feb 2025 14:24:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740695062; x=1741299862; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=4G+4qHSVJnaTG8aqakqnf5oS6XZxipCyWxhYCrPdMqU=; b=abkAOfxWRZcu4NRNZREt5bSe9KbvOD5QN5aQFG0qL8Aj9kos5xCL7mNITnpRu+1DbQ Wuvj33poeMCtoj8vXNAlClXU59J7czJkkcXGjW1XagJmaNq4n5e8igRiHARCcHk580h8 bxRmoM4LPfDR/EJwx+fA3D2wHVdVmkwPRLLl+gdCHcWjy1Jt5XwTSv0yBC+L9QK/TfRM mnD8YdBzRp6q8MYROO5ttHF1icj33+aM/TLwtsSR/rEo17WRdHo7n5tqsFszGyzaT1Ak WrLJx/UnJAl4L1mk/F+2muHCDPtU05yjfkuBOjVnWIk9KquDQHrazosTus2qyJOaoWU2 uEug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740695062; x=1741299862; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=4G+4qHSVJnaTG8aqakqnf5oS6XZxipCyWxhYCrPdMqU=; b=kVPs0nzAtjt8887if44giLDjlilnbDy+VOobG8ol8X9DmJP65rr8I28RfGRiFsI/Bz sjSezMKkP1L5GDtO1FnuZZ/vIvETdAidiB1FRbtGLjgd/4o0RU6ePjJhBy3ybaCZmXHZ F0VIvKjAc+iOiJos86bMuR6T5jbmGM1bX3E/SEstowmWCiyDmITjCIk4UElrYRVlBgys d1EU5p3rOt1KqPg77dVbgHTN9jqt/bY/f5wJT3Ecs3uJ0jrru8pS4k4OTHikHx0V4JBi MhSWcPOrce5SO/4RF4bZEdRAC/BNyJA1hiGJRxsWumEVSj8sANvZi07Q52u+km0ZvqBa XWjw== X-Forwarded-Encrypted: i=1; AJvYcCXIFIfs+CnQ68w70GE6BJeXNfBMb/LPtyqawHKGwRoLAbadTinD1kHb9/71E478//RLZtAwgB0PNXjCBkk=@vger.kernel.org X-Gm-Message-State: AOJu0YzlBQ6zwOZr021aazvNWNa3RJ5ULYn5Eynf/caq0yoxxhF2Jzd4 tkz5Xfh7R8I9KISrg2dxcqzBl2g0lmfcB3JXx7nqDAYiNxPV0mC6qA2w3CVMvmKvV05gHrQHHKF JPg== X-Google-Smtp-Source: AGHT+IEgwwtyjm9FH0NZoHUXAAKG79/J8WSFp39HQkew/kpXx6ars2ZD8qvS6jLycvRG3qg04LFPzNBgvnI= X-Received: from pfjd20.prod.google.com ([2002:a05:6a00:2454:b0:730:8a55:44fd]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:929b:b0:732:a24:7351 with SMTP id d2e1a72fcca58-734ac35dbbbmr1768936b3a.6.1740695062583; Thu, 27 Feb 2025 14:24:22 -0800 (PST) Reply-To: Sean Christopherson Date: Thu, 27 Feb 2025 14:24:10 -0800 In-Reply-To: <20250227222411.3490595-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250227222411.3490595-1-seanjc@google.com> X-Mailer: git-send-email 2.48.1.711.g2feabab25a-goog Message-ID: <20250227222411.3490595-6-seanjc@google.com> Subject: [PATCH v3 5/6] KVM: x86: Snapshot the host's DEBUGCTL after disabling IRQs From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Ravi Bangoria , Xiaoyao Li , rangemachine@gmail.com, whanos@sergal.fun Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Snapshot the host's DEBUGCTL after disabling IRQs, as perf can toggle debugctl bits from IRQ context, e.g. when enabling/disabling events via smp_call_function_single(). Taking the snapshot (long) before IRQs are disabled could result in KVM effectively clobbering DEBUGCTL due to using a stale snapshot. Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 09c3d27cc01a..a2cd734beef5 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4991,7 +4991,6 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cp= u) =20 /* Save host pkru register if supported */ vcpu->arch.host_pkru =3D read_pkru(); - vcpu->arch.host_debugctl =3D get_debugctlmsr(); =20 /* Apply any externally detected TSC adjustments (due to suspend) */ if (unlikely(vcpu->arch.tsc_offset_adjustment)) { @@ -10984,6 +10983,8 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) set_debugreg(0, 7); } =20 + vcpu->arch.host_debugctl =3D get_debugctlmsr(); + guest_timing_enter_irqoff(); =20 for (;;) { --=20 2.48.1.711.g2feabab25a-goog From nobody Fri Dec 19 16:05:17 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 77361281375 for ; Thu, 27 Feb 2025 22:24:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740695065; cv=none; b=b0OBGE+J0+EIFRut0onrD5OQhoimXrdXQ4xZl5pnL3ILPNCmyBfyMFm8UdRddVDAWWaVrb+eQdR8y03gchibRdrv4TPuWIILo8WOXbAUkbd8rooLfcpUz56AgtsvEeVx9zIRtfqFq0MlFR5yPsEVu8pHafJDr3ihHwy2qAnTsZo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740695065; c=relaxed/simple; bh=l1iaY/46ebxN7Pxlf+bVDyGkotDM42snv/jC9ZbuKW8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=WvA5tT5KXU0W7ZmH0TckXvnD3SHhAnpLRsm7jD2TbPxPaTX7VS7YnZl6+L7eZdU0fhqOleabVddYYmf+mkS0FvNdieH2X9q+2Do0DMURxVwx/ji2Vj5txqM4YwCcDp73vOoNDVSWrQLqB29H8Go8neIRBwzChuip4mPTBe6GhC4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=iTs1T47X; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="iTs1T47X" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2fea8e4a655so3853601a91.0 for ; Thu, 27 Feb 2025 14:24:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740695064; x=1741299864; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=0qlBu3O+qgW1GVmSpLZOxIdN+UM1bvdDqizmqLkAsDo=; b=iTs1T47X2tYRwXd1yfZQAQ56agGwF4G9gaopbB7+ECzCa/slfOTia0fF2n8scE5qoN zITmJmlA6c5ZmfMHip2HiUMY+GDJ5B8p2cabEEQ+ayqs2yFw8SDanyME+1PR2lNtibON v6yd19RAYYMoTQtZkaa8UJz6Sw5ktGfWh4uY3rPAngPKrWh3Eewlf1/2Ng7KuphCu24l 8OHX80ADP2J2DUTT0n3846sVyNhNg9VNPPxw6WnGpeH5htuUw5zeU0GfTNJ2UhVX9oZ4 YtGPX2bSUgAT6Bon4lYYGQSAxkciUwmIq2CdwtPr+1tTEakPgu/oZAQgjOAHvSP8bi7j ndQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740695064; x=1741299864; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=0qlBu3O+qgW1GVmSpLZOxIdN+UM1bvdDqizmqLkAsDo=; b=CQ41WtT5VhB11e03w+kcSEQssv8HrsqzcO9pG71GiTtQ6h1LKPvVvJsXmNTiza+hl9 LK7631wV0yEbZAKdECYyxLiwmxzrCwSUX/rlM7gZsIgTC5E4RHiass2soySkjmE/ecdb 1pgnMml+VEL+mEa0JpW3BHD967pLO7U1+P9UT9SxC7Q+nNEfz0tKkz4qRvULGl5Hvu7U Kqri9u1HVj0Yd9N+jNornEMOlIZ1FU64styOgEVPNYZFGfQVb+2IRJT3ZxjizZm2M56T DKK9SD+he29f7sYQIqLmV3dJ5euTBsb+k5Fo/cKRdx1MthHaJ+r3ECaI8RtsBuXovBc5 2qyg== X-Forwarded-Encrypted: i=1; AJvYcCWvZpx47vC5cX0FQVI84VD6xGIPAPp9hQAGZTkwac1DmbjBoA8DgC1jGk6iA/RLpR+BP9YyqZm0hN1f0Uk=@vger.kernel.org X-Gm-Message-State: AOJu0YxFy99gLyPXqHUTERjbqN6yF7I2HGunMBBdoL/2mHNaxuQJCFvt IJdHnj8E88H7ktyBzYwAZ+TThUTzIYc51F3ds7YSQlno5SgaE/0BGeAKltPH26xqZbRQkdgYZN7 uzA== X-Google-Smtp-Source: AGHT+IEAR4cGG2gi7Xmxt0qd1akR25wwXozuUf+4YWN9m+W0e/uXX0xINJ03FxGe0avbJCicY+EqJQBZUo4= X-Received: from pjl3.prod.google.com ([2002:a17:90b:2f83:b0:2ef:d283:5089]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3845:b0:2ee:c9b6:c267 with SMTP id 98e67ed59e1d1-2febab403a5mr1668829a91.9.1740695064085; Thu, 27 Feb 2025 14:24:24 -0800 (PST) Reply-To: Sean Christopherson Date: Thu, 27 Feb 2025 14:24:11 -0800 In-Reply-To: <20250227222411.3490595-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250227222411.3490595-1-seanjc@google.com> X-Mailer: git-send-email 2.48.1.711.g2feabab25a-goog Message-ID: <20250227222411.3490595-7-seanjc@google.com> Subject: [PATCH v3 6/6] KVM: SVM: Treat DEBUGCTL[5:2] as reserved From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Ravi Bangoria , Xiaoyao Li , rangemachine@gmail.com, whanos@sergal.fun Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Stop ignoring DEBUGCTL[5:2] on AMD CPUs and instead treat them as reserved. KVM has never properly virtualized AMD's legacy PBi bits, but did allow the guest (and host userspace) to set the bits. To avoid breaking guests when running on CPUs with BusLockTrap, which redefined bit 2 to BLCKDB and made bits 5:3 reserved, a previous KVM change ignored bits 5:3, e.g. so that legacy guest software wouldn't inadvertently enable BusLockTrap or hit a VMRUN failure due to setting reserved. To allow for virtualizing BusLockTrap and whatever future features may use bits 5:3, treat bits 5:2 as reserved (and hope that doing so doesn't break any existing guests). Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/svm.c | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 78664f9b45c5..fc9f9a624d93 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -3166,17 +3166,6 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct= msr_data *msr) break; } =20 - /* - * AMD changed the architectural behavior of bits 5:2. On CPUs - * without BusLockTrap, bits 5:2 control "external pins", but - * on CPUs that support BusLockDetect, bit 2 enables BusLockTrap - * and bits 5:3 are reserved-to-zero. Sadly, old KVM allowed - * the guest to set bits 5:2 despite not actually virtualizing - * Performance-Monitoring/Breakpoint external pins. Drop bits - * 5:2 for backwards compatibility. - */ - data &=3D ~GENMASK(5, 2); - /* * Suppress BTF as KVM doesn't virtualize BTF, but there's no * way to communicate lack of support to the guest. --=20 2.48.1.711.g2feabab25a-goog