From patchwork Thu Feb 27 22:24:06 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13995256 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 1C1BF27291F for ; Thu, 27 Feb 2025 22:24:15 +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=1740695057; cv=none; b=mE2MfFiDAHJ8XXyHacQ9ww6igj82gzvNk5YjHpeTp6g3ZVeyvLzO4EQNBpKUIbnCS/E8plh+yXqLoRLlSeuRLnzXcFLiUxypMUZs2b+UVJOTB9vTD4ES9af3pDlgC4mzen3qOdQyRodLVy/MmKjLTJHaP3/qMdHBQn+bB9cw9Jc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740695057; c=relaxed/simple; bh=zm2QaR360KIl6iSEbDz2zLo3X3lN7lh4lpL7RxCk81I=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=CnpLpxBtBiyc0MuVQTgwPmJveL1lCRlreR1ehH9cAyeLTMsMdRNj7+8HN5olJjwDQ9EQiuVfhGSkLfnXrJkD8HXc+YmGMqVpZtegvH637TiLST1BB+xfyM57/oKWb2SDiey7XClThN20DOlOK+jSCFjpz6lAedW1l30VyeIw+nw= 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.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="tUaPzjcW" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-220ff7d7b67so26920535ad.2 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=fmxuwdoEprd1/SqWtTYBbb2bBaZ9v8e6O6i4fNieKJa/aXP4aU5ILR5ToXdAezv3Ov yGvKUEceOrq69Iyk43QuCFLb3/xbXDgzIUdx1CtAqgjpVBSNAH3F4MpXcfLlnBvgrL3y IKFBP6w/6i0vFVlFErxPJMHNpOhGtFDvBDfhkeurks0kpqJOgB0JdvR/iUbmhUdnpHeW lt18fNOm0Eu02GXPYuKanN3Ow6puUiACK26yEUv/K8EcHzBL23EdR8qvO+jsAf0VIe/H fVbolcKjGO7wLHldwaaadL6qkUGrnYwtaqPaIrxVYwQ1J78CPIDb2hlXOBVTC+/72OzZ GPOQ== X-Gm-Message-State: AOJu0YyVcf4hANRd36YTUKuRJesTvmz95z+nRriGfXX+HA6/w/fyiQ+j EvjERw5FhZ5hIhWiIx4BMQGNUNkp/91isGLLFEktP3mTV88CJN6mHJVZgVdcrwuRulVcYZE3uKa HNQ== 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: kvm@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 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)—Bits 5:2, read/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)—Bit 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 &= ~GENMASK(5, 2); + if (data & DEBUGCTL_RESERVED_BITS) return 1; 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 -#define DEBUGCTL_RESERVED_BITS (~(0x3fULL)) +#define DEBUGCTL_RESERVED_BITS (~(DEBUGCTLMSR_BTF | DEBUGCTLMSR_LBR)) extern bool dump_invalid_vmcb; From patchwork Thu Feb 27 22:24:07 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13995257 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 ACB5427FE6E 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-2f816a85facso3105347a91.3 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=ouM1ipTFeqUbImx6/N8m5n0vDS1Xbhb2JxPT+NDlA/K1Ee8iWbbe6P3XlHc96LNIh+ JALjWoyIZQhcJLEA0zQiLXY/YGApnRjrSzY0TNEBnJfvZ6NS+4C4l1N+p10QI3LJQ+KO /VKxJhuWyivt67Fqj3pPLk8hKZRPsgX/TSBp+2OuRl5yo4MYGqGJfrp8ZvP6ELxwgtxU Q/+ILG7+mQ63aermDO5UGoZNbz/bYd57LxYpo3ZL2J/5XH5PicHQAuSoaXxsH2TAXqCO K7G2YMfZlpYO2xQoMTP/4tZn9GkA7NUcXg6wE3+BugcIpBY8cDvh0EGEj6ehzbzNCpic yqmA== X-Gm-Message-State: AOJu0YzASg67ufvfX70QsTyB6sZKbVtBoQ221+hLmuI8p/tx3xM+vhBR WXs25go2gR9X6UaWZhMVX95L7A/MEVDfvlJ7EmxqNcvrQwzm1d5KVvOXlvnXJ6F8/pxa8raBl4z +uA== 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: kvm@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 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 &= ~GENMASK(5, 2); + /* + * 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 &= ~DEBUGCTLMSR_BTF; + } + if (data & DEBUGCTL_RESERVED_BITS) return 1; 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 -#define DEBUGCTL_RESERVED_BITS (~(DEBUGCTLMSR_BTF | DEBUGCTLMSR_LBR)) +#define DEBUGCTL_RESERVED_BITS (~DEBUGCTLMSR_LBR) extern bool dump_invalid_vmcb; From patchwork Thu Feb 27 22:24:08 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13995258 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 C1A35280A2A 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=1740695062; cv=none; b=p0jG8No2yW2NgwAP5LFGLFb+HMrDnitkBga2cjMKkjPNjc1D3bla0uzGqSpiDAxn7QozYD417wELOsj87MsyXv4zoB9k27BAf98e9WjfpeZG2JP2MhKiF+oEbVGmX8fZJJlt5FNp9TgBlkIgo4MWjBnspoXnp9QhMQiRUJ3k1Xk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740695062; c=relaxed/simple; bh=pUJzhfVuP8YDCZyv6pEaIsEIbkiixYOoWJyO/nE7JWM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=NAjbXCbgxYM1iGoHqwQ6HNkswubR+T/HKmxFJOS0Xry8kgqx0PV2zTrxi2C9OXrdGHEHK6DT94rILL7X4dLFpUrN4pve1YXJVRRo+d7oEt7AndHMJaLSkf3pAbwTLIKuI1RG4SI8Yy/jdjQ48Frndps08dAmuopaZoJNV+MAQyc= 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-2fc43be27f8so5270921a91.1 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=JAP6jZekvZVS9W6Or9zX0Ao4+2myshsVLgG/rj59We9uvHq1ak+a0pR17JUbhUnG7t N+OEDrY0pzo7Hl+R/qViAoufDVQnB8Ej0Kyjy2wa4WSmbS1kvpnHZeEV6iulQeCNJ3Qf WDIKjGEaHLbwnPeTK3tSsAUOM/9brPnG2S0kMAusHVyAbHTgt2p2+iba5FptdEOIl60C lvGK01SFSTbcVCXZsmOcioG9Iqjwhfurfe7vpb0nwoBCYFt8pwoqEbVc+QG+8YX+XIys l1yjqcnXxBj9mNsujuWaT2aOfFWB75JRwGlO4A7SzRhRq9YJ05tTcKmpRpjHE0HuKkGP ooRg== X-Gm-Message-State: AOJu0Yy18MC0kvnVugaAHeRlZLC1nlEGSlc2ldf9kFQFz8GNHe3WdG3U C3vInN07ptsZ0Sp6gh7QJviW9P3Z421wbirwz2ESY5EsFg21bHfe+3Om4qGsGED1tgpDEyh4IV+ g9g== 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: kvm@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 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_host.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 = to_vmx(vcpu); - if (vcpu->scheduled_out && !kvm_pause_in_guest(vcpu->kvm)) shrink_ple_window(vcpu); vmx_vcpu_load_vmcs(vcpu, cpu, NULL); vmx_vcpu_pi_load(vcpu, cpu); - - vmx->host_debugctlmsr = get_debugctlmsr(); } void vmx_vcpu_put(struct kvm_vcpu *vcpu) @@ -7458,8 +7454,8 @@ fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu, bool force_immediate_exit) } /* 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); #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; - 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 cpu) /* Save host pkru register if supported */ vcpu->arch.host_pkru = read_pkru(); + vcpu->arch.host_debugctl = get_debugctlmsr(); /* Apply any externally detected TSC adjustments (due to suspend) */ if (unlikely(vcpu->arch.tsc_offset_adjustment)) { From patchwork Thu Feb 27 22:24:09 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13995259 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 7ADC1280A3B 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-2fe8de1297eso3108081a91.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=dGEsEMU7bovVJUX8BFbYIEKCnkHF0CzSaTHJpzNsRbukKYtgshIops1uunUPX/QZCJ L2GTs6E+cDxkZ7HWhFcQck9hkvnSzUxCcVHAr1gSGfdZxk5UBXLsGW3GcISCRuwHDilk hPcghqcftPmGGCLrNBd/g/yre1IVHNealXFyjUPPw32+t5j3tARaYVh2BS1MH55gLaJi Hx8L4pDw/sNlVDNS+wYH+VD/gW7h86sNUw0dZt2nSWGeKZ0FPRysK1mZtJmaOEXBSZXv g0MjbPSjy03kdnbb968iDMooyUx0AXIR4cys32dakefBpH3wZ7r0zFcuF4m0JG0zq6g8 kN+A== X-Gm-Message-State: AOJu0YyNz0Ml35pduj4KmAo2SP7tjSDqIS7hq/zXW0lsL3l408DN7TpR dgSPTltL/3x3hbESJ+BPAy1Im7k66NgJf4plrnuFqKEoIXbx/yZv9G9Llqq00IR+yvGL8CeRMSA Efg== 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: kvm@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 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=219787 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); + /* + * 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 != svm->vmcb->save.dbgctl) + update_debugctlmsr(svm->vmcb->save.dbgctl); + kvm_wait_lapic_expire(vcpu); /* @@ -4301,6 +4311,10 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu, if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI)) kvm_before_interrupt(vcpu, KVM_HANDLING_NMI); + if (!(svm->vmcb->control.virt_ext & LBR_CTL_ENABLE_MASK) && + vcpu->arch.host_debugctl != svm->vmcb->save.dbgctl) + update_debugctlmsr(vcpu->arch.host_debugctl); + kvm_load_host_xsave_state(vcpu); stgi(); From patchwork Thu Feb 27 22:24:10 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13995260 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 47B2B281353 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-2234c09240fso27690175ad.0 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=R3qUhOsqiDS9geILPI35cygWgAoi/zDZ6ejQ/E/DKR5jfqZv4Q4dPS3FfyMjXwtnAT VR/MJ5ml1D0ydCL/hQ9IhNvpsv07IS5i1j/aFTFoZA3Zv54ZdVRDZnOfY/07Nh6EKTwE dcQO0gLQbAHu/ZDu9++LyH3euZXDULEi6gKFl6E3Rcalc377pfXBVgIJLhnQyJXDfZUt eOb9dX/1OOp1ie6Qiw2hLdGZTRoX0Pk25+aHh1DuEBNGkO0vkYSROWOLF1GrPU1i+smz MG3LiSwRrGCo9353a1UKdoXfbhFRgoQCuxT5FJgpq3yclbP3rmFet2eDSGMwu3VOE8Ul Br5w== X-Gm-Message-State: AOJu0YxiTyHGJu8TMkSpyw6NOrzlxM0RCNUcp52/EAvGf5cSwM66lZU/ NZrg5VFiO7ws0jlOLqntQSp4Pub9lQxcpi+xygPvMI1aYfn0LybSZk8eaB2jJPZiDrXSf3gMXBG Ncw== 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: kvm@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 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 cpu) /* Save host pkru register if supported */ vcpu->arch.host_pkru = read_pkru(); - vcpu->arch.host_debugctl = get_debugctlmsr(); /* 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); } + vcpu->arch.host_debugctl = get_debugctlmsr(); + guest_timing_enter_irqoff(); for (;;) { From patchwork Thu Feb 27 22:24:11 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13995261 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 8C719281377 for ; Thu, 27 Feb 2025 22:24:24 +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=1740695065; cv=none; b=f6ghrtFR050EIXd0bKuqywwzWjV/zlzGhYxRhtva/RU9WWxcF3oCLXMAvf/6IH5vO+x8P6/r8jNAT8H95QWh008vwOr8oByLDSQXDxx1F5jBMxDI5E0L6RuA41hI9LF8RhAWUmJEX9Pcf+1tJjmggiljp/qWfykYysN+UNSDPg0= 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.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="iTs1T47X" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2fc43be27f8so5271073a91.1 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=BGBd48OlP/HGuhZWyrk41gkzwqxyYO8RF2Jq2cRxCdm8wFLKJKyl0eUKvvWlR59IBV CGva5/LsZeyKln8CSrxYgQgAJtNh8PqmBgaowTX+5kWEOgE6T9zoum4jTx4zghb2Tpr+ fGFzNFxJ1k3HaoVlRD79GKaEupo8T+ef4nTsu5Hx/8NChbI4kXoVOit9vPh3mXBbKqh6 m/RknQFGW3iiWW1dPMJX83Jsa9bqeLndYnv1ybBebhb6H4/pGaCrGJnHEgjKFZTipjjp DU8nXZGc0wa1GGo0ghIC8dH6NNXY77GiPEUMilIiuuE7imb9iTVjzpvGzsnh59wBs14G N2mg== X-Gm-Message-State: AOJu0YzbU/s7QXAGYBfadCAH+7ock8cyLNMiBksmWal+qqKHQGAps3Fe NysHhT+1cg4mnMBzNV5IdERsCUiRGj8tU1Sl9Ez08F3G0MyCOcVOrk2tcL/CRMZBXlHmI4faO31 njQ== 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: kvm@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 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; } - /* - * 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 &= ~GENMASK(5, 2); - /* * Suppress BTF as KVM doesn't virtualize BTF, but there's no * way to communicate lack of support to the guest.