From nobody Sat Feb 7 22:55:00 2026 Received: from out-176.mta1.migadu.com (out-176.mta1.migadu.com [95.215.58.176]) (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 D9C11299944 for ; Fri, 24 Oct 2025 19:29:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761334194; cv=none; b=FRmzzMhT2nTegua0pMU64b1nrH3E2VXbI4c9l2AP25r+L6ZtoD/ofD5pRzeCoZtM0qmNyxltRb+tf7Ki4DTQgRQogavOWihaWGnP5rWDe1dIRxklmBHd3N8/t5W8vFT4vC4jJbrZ+UdUlTj5uY6hM+4ccArFjWCJYAx5DwBa/UQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761334194; c=relaxed/simple; bh=K6+rzxAr0QmCxsCJfzidaeVLSS/c5fhlUiVbT7WXwc0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TX00vs07wVySM6Cz3Ua5+gyRWqYV9MyFdQI2lck/Uo75SX0cQ/E5ePYtRm+I2PjtZ4gazQ+AiKCZiNLrYUes16nQZKec5zcelOie/6aKNX7zdgj2/z3ih4JL6GtKs4nHSIZFzzmv7KfG7xYKn9VaT27wPepkCbZv4giT3Tq1TwE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=Gf56mI78; arc=none smtp.client-ip=95.215.58.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="Gf56mI78" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1761334191; 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=CbMin659eysjqiUPMu3Iq2L5Q2iASvKh7BgwyydP+5Q=; b=Gf56mI78hRso2PGHoat9lzNVHWYfBBEJu9x4lTee4Uu7Oz09lh7JcRkgnYxXICvbnGGIqr kVDHar1unntZX1vovidCrtDXsf4AxRJQ2cyVT82xY26hMUjZ6TEuPK2ZnGyumkvBXoxGku 7PJ1QWLmxZBSEArhWRHubMHoreTkvJs= From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , Jim Mattson , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed Subject: [PATCH 1/3] KVM: nSVM: Remove redundant cases in nested_svm_intercept() Date: Fri, 24 Oct 2025 19:29:16 +0000 Message-ID: <20251024192918.3191141-2-yosry.ahmed@linux.dev> In-Reply-To: <20251024192918.3191141-1-yosry.ahmed@linux.dev> References: <20251024192918.3191141-1-yosry.ahmed@linux.dev> 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-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Both the CRx and DRx cases are doing exactly what the default case is doing, remove them. No functional change intended. Signed-off-by: Yosry Ahmed --- arch/x86/kvm/svm/nested.c | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index a6443feab2520..71272efc16609 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -1436,16 +1436,6 @@ static int nested_svm_intercept(struct vcpu_svm *svm) case SVM_EXIT_IOIO: vmexit =3D nested_svm_intercept_ioio(svm); break; - case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: { - if (vmcb12_is_intercept(&svm->nested.ctl, exit_code)) - vmexit =3D NESTED_EXIT_DONE; - break; - } - case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: { - if (vmcb12_is_intercept(&svm->nested.ctl, exit_code)) - vmexit =3D NESTED_EXIT_DONE; - break; - } case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: { /* * Host-intercepted exceptions have been checked already in --=20 2.51.1.821.gb6fe4d2222-goog From nobody Sat Feb 7 22:55:00 2026 Received: from out-174.mta1.migadu.com (out-174.mta1.migadu.com [95.215.58.174]) (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 F1E2E29B793 for ; Fri, 24 Oct 2025 19:29:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761334196; cv=none; b=G/XJtlGnoY8Llkkmb6hDfJd4vZ9txsRyLGSMacuWd1XcSTIEmNyZ3ADyjhP4OGm0IYlP8Gm98Ep5QtjyzZ0ZFT3FUi3FWc791RnvQ+0egPdXA0wVZ6LKRYqa7ybsGuK6O8uTbeuYReNKJi1KlT4V7+1uudFb9LFSnu0oPfCt5Bk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761334196; c=relaxed/simple; bh=SB5q6+0hwZ04VXp6V9khXsmBZaeHcCObNItM11WBnhQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=K/SLAvXcau0v1BjcKOgpSFE+5rraYW5J4GPP2/NoMYIxtDBwrlfkxVNYnUP1WGZINUaMHDtyuPKbYLvYPGdhipYjnXrQ5rxy5l427/hB48n+XocHmLRDKZbWALZFJXPfDPecrr1sZlfUAFeJXhENxLaV4Hf8TgQhrzfElofQ8d0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=GlLPWgMK; arc=none smtp.client-ip=95.215.58.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="GlLPWgMK" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1761334193; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=DnV9hP8q4zoQm9zSknFS2vlFz4b3nNad1qMbHV1P/c8=; b=GlLPWgMKF6ORhryWoBp3kaBoDjowU94NZ+ATwkedc8YjWVcofwmR5HSCGq5tioSfnrdKnG /zWn3YaQVZ+5VhAHWa7YLLR5TV7L8f1QRJarfclbLTLuHv+MX+Df0HbgTO/HpRD87esgwM jD2ig7Jy+mHk5mwYcq4sR+iSNyrRaqM= From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , Jim Mattson , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed , stable@vger.kernel, Matteo Rizzo Subject: [PATCH 2/3] KVM: nSVM: Propagate SVM_EXIT_CR0_SEL_WRITE correctly for LMSW emulation Date: Fri, 24 Oct 2025 19:29:17 +0000 Message-ID: <20251024192918.3191141-3-yosry.ahmed@linux.dev> In-Reply-To: <20251024192918.3191141-1-yosry.ahmed@linux.dev> References: <20251024192918.3191141-1-yosry.ahmed@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT When emulating L2 instructions, svm_check_intercept() checks whether a write to CR0 should trigger a synthesized #VMEXIT with SVM_EXIT_CR0_SEL_WRITE. For MOV-to-CR0, SVM_EXIT_CR0_SEL_WRITE is only triggered if any bit other than CR0.MP and CR0.TS is updated. However, according to the APM (24593=E2=80=94Rev. 3.42=E2=80=94March 2024, Table 15= -7): The LMSW instruction treats the selective CR0-write intercept as a non-selective intercept (i.e., it intercepts regardless of the value being written). Skip checking the changed bits for x86_intercept_lmsw and always inject SVM_EXIT_CR0_SEL_WRITE. Fixes: cfec82cb7d31 ("KVM: SVM: Add intercept check for emulated cr accesse= s") Cc: stable@vger.kernel Reported-by: Matteo Rizzo Signed-off-by: Yosry Ahmed --- arch/x86/kvm/svm/svm.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 153c12dbf3eb1..9ea0ff136e299 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4541,20 +4541,20 @@ static int svm_check_intercept(struct kvm_vcpu *vcp= u, INTERCEPT_SELECTIVE_CR0))) break; =20 - cr0 =3D vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK; - val =3D info->src_val & ~SVM_CR0_SELECTIVE_MASK; - + /* LMSW always triggers INTERCEPT_SELECTIVE_CR0 */ if (info->intercept =3D=3D x86_intercept_lmsw) { - cr0 &=3D 0xfUL; - val &=3D 0xfUL; - /* lmsw can't clear PE - catch this here */ - if (cr0 & X86_CR0_PE) - val |=3D X86_CR0_PE; + icpt_info.exit_code =3D SVM_EXIT_CR0_SEL_WRITE; + break; } =20 + /* + * MOV-to-CR0 only triggers INTERCEPT_SELECTIVE_CR0 if any bit + * other than SVM_CR0_SELECTIVE_MASK is changed. + */ + cr0 =3D vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK; + val =3D info->src_val & ~SVM_CR0_SELECTIVE_MASK; if (cr0 ^ val) icpt_info.exit_code =3D SVM_EXIT_CR0_SEL_WRITE; - break; } case SVM_EXIT_READ_DR0: --=20 2.51.1.821.gb6fe4d2222-goog From nobody Sat Feb 7 22:55:00 2026 Received: from out-181.mta1.migadu.com (out-181.mta1.migadu.com [95.215.58.181]) (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 B06402BE034 for ; Fri, 24 Oct 2025 19:29:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761334199; cv=none; b=iYLmJiy+85dnYFWpjAgP/yzEVxJcDZIPLYvwj5iAHkkOGqXtqm1E45ppWAV6zX2q7bwh5Fim021uyKXPJe03ldOKnmxS10MfO7nnYZjlEzckNTt9WlkDQy8ji+JTpbs5rrJazeXAcytGbRmqP20O9A47WCTSwpAGzCGqpbOj4GU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761334199; c=relaxed/simple; bh=Rbouwn/Fe2w8MIJjhC34nR8g2gP/sC74mPrcleYapB8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=DNGHBMAFWt6ph0hJVtIziGCPrCl68ZMh1fJ4GfbFHyskI0xX0qZJ6PktvPdWNMrtQjnq5XrCWle+sHIhRecDPNbDQ54JmDNHNFd2shIMOULm+cEvLjcKz3d6i1ZyMhhzC92E61oANjl4fSbv2biMKYn+8FUO02YleI/9L8JOyqY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=OXApD2RS; arc=none smtp.client-ip=95.215.58.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="OXApD2RS" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1761334194; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=c57tg/C/NUSYzi8pno9yKYmODzHakA7S21mwRLPVsP8=; b=OXApD2RSixprRjxTR+xY5rWGXrnujrl/zMgOloCvivOyq6iCwUqGVKjuyrQylniBTh8lrv kICCm5heYnBSMyA1sM5it4nkBl9usClqLkJxTAERU3kJRZu7DOUpDuhSrZn2QJhqmJMJnc XlAaxDE4xcLmjSRU+khppHIWgr9/e00= From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , Jim Mattson , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed , stable@vger.kernel Subject: [PATCH 3/3] KVM: nSVM: Avoid incorrect injection of SVM_EXIT_CR0_SEL_WRITE Date: Fri, 24 Oct 2025 19:29:18 +0000 Message-ID: <20251024192918.3191141-4-yosry.ahmed@linux.dev> In-Reply-To: <20251024192918.3191141-1-yosry.ahmed@linux.dev> References: <20251024192918.3191141-1-yosry.ahmed@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT When emulating L2 instructions, svm_check_intercept() checks whether a write to CR0 should trigger a synthesized #VMEXIT with SVM_EXIT_CR0_SEL_WRITE. However, it does not check whether L1 enabled the intercept for SVM_EXIT_WRITE_CR0, which has higher priority according to the APM (24593=E2=80=94Rev. 3.42=E2=80=94March 2024, Table 15= -7): When both selective and non-selective CR0-write intercepts are active at the same time, the non-selective intercept takes priority. With respect to exceptions, the priority of this inter Make sure L1 does NOT intercept SVM_EXIT_WRITE_CR0 before checking if SVM_EXIT_CR0_SEL_WRITE needs to be injected. Fixes: cfec82cb7d31 ("KVM: SVM: Add intercept check for emulated cr accesse= s") Cc: stable@vger.kernel Signed-off-by: Yosry Ahmed --- arch/x86/kvm/svm/svm.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 9ea0ff136e299..4f79c4d837535 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4533,12 +4533,22 @@ static int svm_check_intercept(struct kvm_vcpu *vcp= u, if (info->intercept =3D=3D x86_intercept_cr_write) icpt_info.exit_code +=3D info->modrm_reg; =20 + /* + * If the write is indeed to CR0, check whether the exit_code + * needs to be converted to SVM_EXIT_CR0_SEL_WRITE. Intercepting + * SVM_EXIT_WRITE_CR0 has higher priority than + * SVM_EXIT_CR0_SEL_WRITE, so this is only relevant if L1 sets + * INTERCEPT_SELECTIVE_CR0 but not INTERCEPT_CR0_WRITE. + */ if (icpt_info.exit_code !=3D SVM_EXIT_WRITE_CR0 || - info->intercept =3D=3D x86_intercept_clts) + vmcb12_is_intercept(&svm->nested.ctl, + INTERCEPT_CR0_WRITE) || + !(vmcb12_is_intercept(&svm->nested.ctl, + INTERCEPT_SELECTIVE_CR0))) break; =20 - if (!(vmcb12_is_intercept(&svm->nested.ctl, - INTERCEPT_SELECTIVE_CR0))) + /* CLTS never triggers INTERCEPT_SELECTIVE_CR0 */ + if (info->intercept =3D=3D x86_intercept_clts) break; =20 /* LMSW always triggers INTERCEPT_SELECTIVE_CR0 */ --=20 2.51.1.821.gb6fe4d2222-goog