From nobody Sat Feb 7 18:28:54 2026 Received: from out-174.mta0.migadu.com (out-174.mta0.migadu.com [91.218.175.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 4928D3563FD for ; Tue, 28 Oct 2025 22:12:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761689568; cv=none; b=vGJXbv32KcyRDwJgqjCjvcq6KqEREOz9GTsNUfomkVNarQHBn2nLU35XSrmonlKEzOq+YfQtRT8t86I5gEbkbNsEqJcuF25rpquDXp/whLvjFjofIS9Mqo2K5U+9KMcMRLyW3HGsdK6za8KvHFIxF6s0pBdhtOQi9KeE0KZhcUA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761689568; c=relaxed/simple; bh=xxeKlF8mwwnlrrgJ5iUG+Sr9g1freJAFqFCx5sFfPto=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aYVpBNVxzJNEjakpLW53SxCe8XId5uuzslm4j7EsgqrT6E7v0Pwp1L+SUdJ4+Up3Q76baMkeO8k9vrs2OWLtqsBBXiKLEGV/cxtSGWEm7YUb9zY+L5Uc2ONkbGY1wij9LX6uFg64wFsTxy1J2I2IpXvuw7MWX5x2hfa2ALSZdEo= 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=StekrSeU; arc=none smtp.client-ip=91.218.175.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="StekrSeU" 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=1761689561; 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=1Peddc7Q36WMJTpzb2qo2jbZSMjh5dJyVyT5u+gHEIs=; b=StekrSeUZvXHyXuPrkb3ATeaqN/iQxe8KQqTsltCdgUXGfOz6F4laz22aypepTdqxelqTG Reef7DZTx+uzJW1SzUwAlI+4p1ej7E89S9kZlRWfIIXBM7pNdEbv0BDNVKiBnAFUJBF6cV vVJszKBS+6Fm48VkAnyslWxIAx3dORU= From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , Jim Mattson , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed Subject: [kvm-unit-tests v2 1/8] scripts: Always return '2' when skipping tests Date: Tue, 28 Oct 2025 22:12:06 +0000 Message-ID: <20251028221213.1937120-2-yosry.ahmed@linux.dev> In-Reply-To: <20251028221213.1937120-1-yosry.ahmed@linux.dev> References: <20251028221213.1937120-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" runtime.bash always returns 2 (or 77 in one case) when a test is skipped. But two cases are missed and return 0. Fix them. Signed-off-by: Yosry Ahmed --- scripts/runtime.bash | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/runtime.bash b/scripts/runtime.bash index 6805e97f90c8f..0cbe2695948b8 100644 --- a/scripts/runtime.bash +++ b/scripts/runtime.bash @@ -103,13 +103,13 @@ function run() =20 if [ -z "$GEN_SE_HEADER" ] && find_word "pv-host" "$groups"; then print_result "SKIP" $testname "" "no gen-se-header available for p= v-host test" - return + return 2 fi =20 if [ -z "$only_group" ] && find_word nodefault "$groups" && skip_nodefault; then print_result "SKIP" $testname "" "test marked as manual run only" - return; + return 2 fi =20 if [ -n "$arch" ] && [ "$arch" !=3D "$ARCH" ]; then --=20 2.51.1.851.g4ebd6896fd-goog From nobody Sat Feb 7 18:28:54 2026 Received: from out-182.mta0.migadu.com (out-182.mta0.migadu.com [91.218.175.182]) (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 3FE133570D9 for ; Tue, 28 Oct 2025 22:12:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761689570; cv=none; b=kbuJbZxPSJ08XfNSReyD/pBVYyQr8nninaNOsDcGqpiMZIbiQ0yi5h/dUYbhWKD883RXL07hGYsCSwhy23l3bP9DzfVeFQv8pzhP9e9gj2T+Nc0vm+MGbLbQND8CJqA/H3OkGV8KvIncFx6wRnDHXu3pTRpJPmhe6SQZrYS7nm8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761689570; c=relaxed/simple; bh=vJh+S3CgEPIc6Aa6El12XCOuqGg4J1P9NaFdpLOjBXI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KoJiQxDPuzh0hKkLASXeOvq8cX21M3tbTk2/e7kRuut2RkTu8IhrBuYRXfpN17+mRUtK2Ox7Ka74l30gbAxG4x6+b+3W/t7PKtvPoktSGuJLjGJHtf8hEo7EVK6fUL8TKxx9nn++/ckEKyRb4Z17NzJO5/m+UJa/+e2P3Ur8CFI= 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=Ni25ZmVj; arc=none smtp.client-ip=91.218.175.182 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="Ni25ZmVj" 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=1761689563; 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=1kVP3FxBT54Ty/cM/5sjNG3Lv4LKxVmh1cwe5DD5L7w=; b=Ni25ZmVj6fZoxDElrOQFIS2aUbwBOJMhb6EYORMWCMoTee/RIggEnSFBZKwKpYG3hOqKmi mreesfxOpbtwXB3iJuLRvSX3H6odJuLkzpJZmVjaCUbGZWHjeRc29lf0D9KdzysPwpFpCe ltBFF7fqb3ZVaaOqg8+EzICZQoQtl5g= From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , Jim Mattson , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed Subject: [kvm-unit-tests v2 2/8] x86/svm: Cleanup selective cr0 write intercept test Date: Tue, 28 Oct 2025 22:12:07 +0000 Message-ID: <20251028221213.1937120-3-yosry.ahmed@linux.dev> In-Reply-To: <20251028221213.1937120-1-yosry.ahmed@linux.dev> References: <20251028221213.1937120-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" Rename the test and functions to more general names describing the test more accurately. Use X86_CR0_CD instead of hardcoding the bitmask, and explicitly clear the bit in the prepare() function to make it clearer that it would only be set by the test. Signed-off-by: Yosry Ahmed --- x86/svm_tests.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/x86/svm_tests.c b/x86/svm_tests.c index 80d5aeb108650..e911659194b3d 100644 --- a/x86/svm_tests.c +++ b/x86/svm_tests.c @@ -793,23 +793,19 @@ static bool check_asid_zero(struct svm_test *test) return vmcb->control.exit_code =3D=3D SVM_EXIT_ERR; } =20 -static void sel_cr0_bug_prepare(struct svm_test *test) +static void prepare_sel_cr0_intercept(struct svm_test *test) { + vmcb->save.cr0 &=3D ~X86_CR0_CD; vmcb->control.intercept |=3D (1ULL << INTERCEPT_SELECTIVE_CR0); } =20 -static bool sel_cr0_bug_finished(struct svm_test *test) -{ - return true; -} - -static void sel_cr0_bug_test(struct svm_test *test) +static void test_sel_cr0_write_intercept(struct svm_test *test) { unsigned long cr0; =20 - /* read cr0, clear CD, and write back */ + /* read cr0, set CD, and write back */ cr0 =3D read_cr0(); - cr0 |=3D (1UL << 30); + cr0 |=3D X86_CR0_CD; write_cr0(cr0); =20 /* @@ -821,7 +817,7 @@ static void sel_cr0_bug_test(struct svm_test *test) exit(report_summary()); } =20 -static bool sel_cr0_bug_check(struct svm_test *test) +static bool check_sel_cr0_intercept(struct svm_test *test) { return vmcb->control.exit_code =3D=3D SVM_EXIT_CR0_SEL_WRITE; } @@ -3486,9 +3482,9 @@ struct svm_test svm_tests[] =3D { { "asid_zero", default_supported, prepare_asid_zero, default_prepare_gif_clear, test_asid_zero, default_finished, check_asid_zero }, - { "sel_cr0_bug", default_supported, sel_cr0_bug_prepare, - default_prepare_gif_clear, sel_cr0_bug_test, - sel_cr0_bug_finished, sel_cr0_bug_check }, + { "sel cr0 write intercept", default_supported, + prepare_sel_cr0_intercept, default_prepare_gif_clear, + test_sel_cr0_write_intercept, default_finished, check_sel_cr0_intercept= }, { "tsc_adjust", tsc_adjust_supported, tsc_adjust_prepare, default_prepare_gif_clear, tsc_adjust_test, default_finished, tsc_adjust_check }, --=20 2.51.1.851.g4ebd6896fd-goog From nobody Sat Feb 7 18:28:54 2026 Received: from out-177.mta0.migadu.com (out-177.mta0.migadu.com [91.218.175.177]) (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 8ADF0357A51 for ; Tue, 28 Oct 2025 22:12:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761689568; cv=none; b=NYNOz5ofvESlbO1hqTKvH32Tr7gOxkTVGtxlG14fKF21rPBV2LT+SpAXTAhTFdcaraj+QFg9vSd3fWmv3uaRFDXtU40SFnPKnXkG+Arz7lcIcQEfHBlBelhCgiRlNeSUMNLtGUtXkV//L35xXT0SZdt6S5FgrqGOPT8pgBTQIg4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761689568; c=relaxed/simple; bh=3AsyydydOXbsdoj9A11Lj94OBCmTJtyHkTud6c3h9r8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FhHSbZsqyC0rILErudSKv9/TWUHP7EZoHuv2494goukpUJiDiwPtI8YYKJzNqVOKY6EzEGSrCd5oAaPTNvX3AE/efnjK58pdbKz5R/FhcZqJLh7x50uKjjkFdXtYlZsRG6N62rCw1gTIrywSRJijR0paasQO2GDhAUGHjm+xsGM= 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=doL6rfXw; arc=none smtp.client-ip=91.218.175.177 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="doL6rfXw" 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=1761689564; 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=h0602KOF+ye2x1sgP+VcEDcnugWogmMFJ8XHS655fQ4=; b=doL6rfXwwNmzZn55GInLQCxRxSjQetUx9M/mUh3urkjNYmViem62/1zi5OXMBiOzQNsZ1F EUh0EkGPhBD0NF3WGy5Zd9skD6tzkdPJsmc6rsSsMwl/3mFdwfCIiDrnNInyZngmLM+tcR AS0Rs1w2+x5Z1tok/kxiUHABoLSGfLg= From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , Jim Mattson , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed Subject: [kvm-unit-tests v2 3/8] x86/svm: Move CR0 selective write intercept test near CR3 intercept Date: Tue, 28 Oct 2025 22:12:08 +0000 Message-ID: <20251028221213.1937120-4-yosry.ahmed@linux.dev> In-Reply-To: <20251028221213.1937120-1-yosry.ahmed@linux.dev> References: <20251028221213.1937120-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" It makes more semantic sense for these tests to be in close proximity. Signed-off-by: Yosry Ahmed --- x86/svm_tests.c | 64 ++++++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/x86/svm_tests.c b/x86/svm_tests.c index e911659194b3d..feeb27d61435b 100644 --- a/x86/svm_tests.c +++ b/x86/svm_tests.c @@ -112,6 +112,35 @@ static bool finished_rsm_intercept(struct svm_test *te= st) return get_test_stage(test) =3D=3D 2; } =20 +static void prepare_sel_cr0_intercept(struct svm_test *test) +{ + vmcb->save.cr0 &=3D ~X86_CR0_CD; + vmcb->control.intercept |=3D (1ULL << INTERCEPT_SELECTIVE_CR0); +} + +static void test_sel_cr0_write_intercept(struct svm_test *test) +{ + unsigned long cr0; + + /* read cr0, set CD, and write back */ + cr0 =3D read_cr0(); + cr0 |=3D X86_CR0_CD; + write_cr0(cr0); + + /* + * If we are here the test failed, not sure what to do now because we + * are not in guest-mode anymore so we can't trigger an intercept. + * Trigger a tripple-fault for now. + */ + report_fail("sel_cr0 test. Can not recover from this - exiting"); + exit(report_summary()); +} + +static bool check_sel_cr0_intercept(struct svm_test *test) +{ + return vmcb->control.exit_code =3D=3D SVM_EXIT_CR0_SEL_WRITE; +} + static void prepare_cr3_intercept(struct svm_test *test) { default_prepare(test); @@ -793,35 +822,6 @@ static bool check_asid_zero(struct svm_test *test) return vmcb->control.exit_code =3D=3D SVM_EXIT_ERR; } =20 -static void prepare_sel_cr0_intercept(struct svm_test *test) -{ - vmcb->save.cr0 &=3D ~X86_CR0_CD; - vmcb->control.intercept |=3D (1ULL << INTERCEPT_SELECTIVE_CR0); -} - -static void test_sel_cr0_write_intercept(struct svm_test *test) -{ - unsigned long cr0; - - /* read cr0, set CD, and write back */ - cr0 =3D read_cr0(); - cr0 |=3D X86_CR0_CD; - write_cr0(cr0); - - /* - * If we are here the test failed, not sure what to do now because we - * are not in guest-mode anymore so we can't trigger an intercept. - * Trigger a tripple-fault for now. - */ - report_fail("sel_cr0 test. Can not recover from this - exiting"); - exit(report_summary()); -} - -static bool check_sel_cr0_intercept(struct svm_test *test) -{ - return vmcb->control.exit_code =3D=3D SVM_EXIT_CR0_SEL_WRITE; -} - #define TSC_ADJUST_VALUE (1ll << 32) #define TSC_OFFSET_VALUE (~0ull << 48) static bool ok; @@ -3458,6 +3458,9 @@ struct svm_test svm_tests[] =3D { { "rsm", default_supported, prepare_rsm_intercept, default_prepare_gif_clear, test_rsm_intercept, finished_rsm_intercept, check_rsm_intercept }, + { "sel cr0 write intercept", default_supported, + prepare_sel_cr0_intercept, default_prepare_gif_clear, + test_sel_cr0_write_intercept, default_finished, check_sel_cr0_intercept= }, { "cr3 read intercept", default_supported, prepare_cr3_intercept, default_prepare_gif_clear, test_cr3_intercept, default_finished, check_cr3_intercept }, @@ -3482,9 +3485,6 @@ struct svm_test svm_tests[] =3D { { "asid_zero", default_supported, prepare_asid_zero, default_prepare_gif_clear, test_asid_zero, default_finished, check_asid_zero }, - { "sel cr0 write intercept", default_supported, - prepare_sel_cr0_intercept, default_prepare_gif_clear, - test_sel_cr0_write_intercept, default_finished, check_sel_cr0_intercept= }, { "tsc_adjust", tsc_adjust_supported, tsc_adjust_prepare, default_prepare_gif_clear, tsc_adjust_test, default_finished, tsc_adjust_check }, --=20 2.51.1.851.g4ebd6896fd-goog From nobody Sat Feb 7 18:28:54 2026 Received: from out-188.mta0.migadu.com (out-188.mta0.migadu.com [91.218.175.188]) (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 71F903587A5 for ; Tue, 28 Oct 2025 22:12:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.188 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761689570; cv=none; b=HHnNUEJTLCS8xbq6sU5yj+YaiTypwHKIvRdNthb7M/bTFkEMPZJitMFQ0aUEwU4+ySsZwPhAcvwWIzdFgpexIG49v7OhYmYTi295WDzzT99zegVgXENLhqaNNhCzk2mwGpjWXZecBhfoSFkaQTG48pKoXUBOF1YWgeZYrDdkoeQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761689570; c=relaxed/simple; bh=prKO2rIazuH1Qiy1nQlxsof9l3kB4sniNcFZXUE3EVI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fRhjJZaGxmgghy/26IW7rpfpWHVHQwso3CWhua1SnNuoPDQcxEhQCQh7WgF/akt/S614ydilHZnut705kXnME4HCcN/ofm2rLbD22GhMKL21UgGMtxdW6/5464R1P6L9EBihkZKPjwVkTdvP0EMpHpO/PjQThBTdXtTz8EfHfmE= 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=kOq/QL92; arc=none smtp.client-ip=91.218.175.188 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="kOq/QL92" 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=1761689566; 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=bZMmiyueSg6H0q/S7Z8qr8ZAZHcjubOiIA7bS8SwUfU=; b=kOq/QL92PuWiHebRvJyCyZ6iZey9gA8j0CVB6BHPsPPeSWEYQUZswsywvKfhB2PT72HPst ZJLvtVTCKaP1O3AVMbvdiS6WtVF9PM9fVAN9ADMSpaKG7xJhQqrSlHpm0239+p47IfPIZ3 4fgzx6zCAF8zbNUONH18vMF6xFLNNoA= From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , Jim Mattson , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed Subject: [kvm-unit-tests v2 4/8] x86/svm: Add FEP helpers for SVM tests Date: Tue, 28 Oct 2025 22:12:09 +0000 Message-ID: <20251028221213.1937120-5-yosry.ahmed@linux.dev> In-Reply-To: <20251028221213.1937120-1-yosry.ahmed@linux.dev> References: <20251028221213.1937120-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" Add helpers to check if FEP is enabled to use as supported() callbacks in SVM tests for the emulator. Also add a macro that executes an assembly instruction conditionally with FEP, which will make writing SVM tests that run with and without FEP more convenient. Signed-off-by: Yosry Ahmed --- lib/x86/desc.h | 8 ++++++++ x86/svm.c | 5 +++++ x86/svm.h | 1 + 3 files changed, 14 insertions(+) diff --git a/lib/x86/desc.h b/lib/x86/desc.h index 68f38f3d75333..06c8be65221dc 100644 --- a/lib/x86/desc.h +++ b/lib/x86/desc.h @@ -284,6 +284,14 @@ extern unsigned long get_gdt_entry_limit(gdt_entry_t *= entry); #define asm_fep_safe(insn, inputs...) \ __asm_safe_out1(KVM_FEP, insn,, inputs) =20 +#define asm_conditional_fep_safe(fep, insn, inputs...) \ +({ \ + if (fep) \ + asm_fep_safe(insn, inputs); \ + else \ + asm_safe(insn, inputs); \ +}) + #define __asm_safe_out1(fep, insn, output, inputs...) \ ({ \ asm volatile(__ASM_TRY(fep, "1f") \ diff --git a/x86/svm.c b/x86/svm.c index e715e270bc5b7..035367a1e90cf 100644 --- a/x86/svm.c +++ b/x86/svm.c @@ -53,6 +53,11 @@ bool default_supported(void) return true; } =20 +bool fep_supported(void) +{ + return is_fep_available; +} + bool vgif_supported(void) { return this_cpu_has(X86_FEATURE_VGIF); diff --git a/x86/svm.h b/x86/svm.h index c1dd84afb25eb..264583a6547ef 100644 --- a/x86/svm.h +++ b/x86/svm.h @@ -417,6 +417,7 @@ u64 *npt_get_pdpe(u64 address); u64 *npt_get_pml4e(void); bool smp_supported(void); bool default_supported(void); +bool fep_supported(void); bool vgif_supported(void); bool lbrv_supported(void); bool tsc_scale_supported(void); --=20 2.51.1.851.g4ebd6896fd-goog From nobody Sat Feb 7 18:28:54 2026 Received: from out-188.mta0.migadu.com (out-188.mta0.migadu.com [91.218.175.188]) (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 E15853587CA for ; Tue, 28 Oct 2025 22:12:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.188 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761689571; cv=none; b=KQSef2Coh014M5Xb9aZOUtRds91TqiN3hgPAQBvWB7QApa6piEID4+w9q2UqRN1pmJTe4Ja1ngvBTaX2tEwgLxFDs9na9xg9q1Qv2LUQDAq1UyskIH0x7543Qxr+EYN/ErLUIebXyXRHnnip29IXun1Ypw0tkumMO7Vvgo4Hj3M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761689571; c=relaxed/simple; bh=9WJOVfUVt6JAy1/UQov7Gl5mNjlNFcOn/orYhjsBu8s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=H+9/3FzyLFOgmEqTznsHLBGc1/tLn1BTTBOu8roVhcxkdYnsRwPeEgtY3QCWCRUgzLtnF7N2U8/zgayU5Z35FgbcxG/dRd2A7JWvBWHoBOSVtYqfm0oDaUa6ywPz6RHf2KsxZushouA8bdqabowBfpMUETLTmxmXnYFKxljgGe0= 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=mVZDqsZ0; arc=none smtp.client-ip=91.218.175.188 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="mVZDqsZ0" 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=1761689568; 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=pghCsaBoSG4WPtJyc67VHSNRWgOinnAutbnbmUGrr2A=; b=mVZDqsZ0MItI0fecjzUjLd9vzB5MALq7VxBkc+C8PpRZSkxnKgHFLyAgRWOotQcdKkE91J IyYYdKZlM536l4jtEIuaAFreIUKffsaP/geDWTJbRvu36EMzWYv/RGIQDLXnaJ6IKtxG5W Axv/H7mErzgAdQ7A0npYN7Mkx2OusRs= From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , Jim Mattson , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed Subject: [kvm-unit-tests v2 5/8] x86/svm: Report unsupported SVM tests Date: Tue, 28 Oct 2025 22:12:10 +0000 Message-ID: <20251028221213.1937120-6-yosry.ahmed@linux.dev> In-Reply-To: <20251028221213.1937120-1-yosry.ahmed@linux.dev> References: <20251028221213.1937120-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" Print a message when a test is skipped due to being unsupported for better visibility. Signed-off-by: Yosry Ahmed --- x86/svm.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/x86/svm.c b/x86/svm.c index 035367a1e90cf..5015339ddb657 100644 --- a/x86/svm.c +++ b/x86/svm.c @@ -403,8 +403,10 @@ int run_svm_tests(int ac, char **av, struct svm_test *= svm_tests) for (; svm_tests[i].name !=3D NULL; i++) { if (!test_wanted(svm_tests[i].name, av, ac)) continue; - if (svm_tests[i].supported && !svm_tests[i].supported()) + if (svm_tests[i].supported && !svm_tests[i].supported()) { + report_skip("%s (not supported)", svm_tests[i].name); continue; + } if (svm_tests[i].v2 =3D=3D NULL) { if (svm_tests[i].on_vcpu) { if (cpu_count() <=3D svm_tests[i].on_vcpu) --=20 2.51.1.851.g4ebd6896fd-goog From nobody Sat Feb 7 18:28:54 2026 Received: from out-189.mta0.migadu.com (out-189.mta0.migadu.com [91.218.175.189]) (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 784A1358D1F; Tue, 28 Oct 2025 22:12:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.189 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761689573; cv=none; b=CATtMKOHH1H+pGEpxwWW4NDEtdYKEoApeIzMKVtgUaSDzB2pCtjDSlyE1FxvCSru1vYUNHGZa8kGDM1e5IK8DP4Fpn7Z9iWYZ7n1/mpXdLAj7O8dP4cLKnusVcZ0utu0BvEk3EBeWhN4XvklzRNWN1nKYsLS7dd/KzhJxR8xj0o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761689573; c=relaxed/simple; bh=4LtXR1MFrLpUgKdjFe2eFpVq+CsdRnoldBQt2gwLI2o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HbF7EanfdPTEpjyXpz9JHTSmYzVHztAIZ8Nor8mZ6doG0hY/Sczi9LYIbO6xgTG4FyqxsW5MpRS6VLqPL4UXRSKAr3yKQnaWhbzBwoTCUJLFOR+2TL0zPI33HfhHA5F9Qnz+8cmQ/tt1Do4RUtuYFmXATbNtyM5+a0O9f+ZXfzE= 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=G6bN35oQ; arc=none smtp.client-ip=91.218.175.189 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="G6bN35oQ" 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=1761689569; 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=kV7XKkm5h17HxE9fn8bhOfc3i9LVxpvvTJ47xHYqFec=; b=G6bN35oQPMUMoBQyQn5HGfAQ6UD71mTQ4Hn9EzjUyt6l6LiqsjQUyR9/3zOVZOZnXeLB37 laKlrPITSFdkYzK4dCd0x8mQ23kiZ9ztqa51t2HAr1J2dwvye7oQ6TawY0tZJhJBNZx6L5 jpks3PKVllA1lvSPH2nK3NULTRMUdKs= From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , Jim Mattson , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed Subject: [kvm-unit-tests v2 6/8] x86/svm: Move report_svm_guest() to the top of svm_tests.c Date: Tue, 28 Oct 2025 22:12:11 +0000 Message-ID: <20251028221213.1937120-7-yosry.ahmed@linux.dev> In-Reply-To: <20251028221213.1937120-1-yosry.ahmed@linux.dev> References: <20251028221213.1937120-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" Move the macro ahead of other tests that will start using it. Signed-off-by: Yosry Ahmed --- x86/svm_tests.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/x86/svm_tests.c b/x86/svm_tests.c index feeb27d61435b..61ab63db462dc 100644 --- a/x86/svm_tests.c +++ b/x86/svm_tests.c @@ -37,6 +37,21 @@ u64 latclgi_max; u64 latclgi_min; u64 runs; =20 +/* + * Report failures from SVM guest code, and on failure, set the stage to -= 1 and + * do VMMCALL to terminate the test (host side must treat -1 as "finished"= ). + * TODO: fix the tests that don't play nice with a straight report, e.g. t= he + * V_TPR test fails if report() is invoked. + */ +#define report_svm_guest(cond, test, fmt, args...) \ +do { \ + if (!(cond)) { \ + report_fail(fmt, ##args); \ + set_test_stage(test, -1); \ + vmmcall(); \ + } \ +} while (0) + static void null_test(struct svm_test *test) { } @@ -1074,21 +1089,6 @@ static bool lat_svm_insn_check(struct svm_test *test) return true; } =20 -/* - * Report failures from SVM guest code, and on failure, set the stage to -= 1 and - * do VMMCALL to terminate the test (host side must treat -1 as "finished"= ). - * TODO: fix the tests that don't play nice with a straight report, e.g. t= he - * V_TPR test fails if report() is invoked. - */ -#define report_svm_guest(cond, test, fmt, args...) \ -do { \ - if (!(cond)) { \ - report_fail(fmt, ##args); \ - set_test_stage(test, -1); \ - vmmcall(); \ - } \ -} while (0) - bool pending_event_ipi_fired; bool pending_event_guest_run; =20 --=20 2.51.1.851.g4ebd6896fd-goog From nobody Sat Feb 7 18:28:54 2026 Received: from out-178.mta0.migadu.com (out-178.mta0.migadu.com [91.218.175.178]) (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 44F5C3590C0 for ; Tue, 28 Oct 2025 22:12:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761689575; cv=none; b=W1Kl4nRD6yY5N01aWotQ3qJ29ODQ65jD3asp3zmNnHRjJZoaGVM1DYKi0ppOPYaILquaW61DtEL6b7nfVOyHBUind4RMged+5dTg9fPg8QuYO8SB59VU+nWJIP2tIAwla3hk4eyB3J+YLEfnEKuBW82XqITsyUpE8D7zvxvlb9o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761689575; c=relaxed/simple; bh=2KOqFe/9ywxf2VubQofKKM9JVnvvenwVR6CUKjZk940=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=W3CP+bFjaCEDCcerGcnaV1mtB+CVTodQimlsh6z2H7CSmSX1Pn2RckFlXHvZHA2PdWE/yx3dl6wxZg+BW4ktDEwxLr15RpUBwiNqXDmS0f/p1PeHrXus4YM68ln0vb4zaQUqEMboMJUtI3c16WCzLVlfFcBhJVkV9WedYYCHmzg= 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=LmwGKFjf; arc=none smtp.client-ip=91.218.175.178 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="LmwGKFjf" 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=1761689571; 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=aDTx3g7TAH7WgGVbrG2EoZFG27+OVORrqavzf7CNek4=; b=LmwGKFjf6LOyTJ7nm/XKQH51X+1aEtTSPa4+LKg0t0DyLSTv8cvcJI9UZrhBV3KAMps8fg r++v4rAWwlEyvwdz/VzRHJGm55LIrmze6Yw/vWj8ZeAw+kt6IFkiMaNNuOkYLLxINeEnty nxVvcUdqPqYBSATIvWSmooypsbclqEQ= From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , Jim Mattson , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed Subject: [kvm-unit-tests v2 7/8] x86/svm: Generalize and improve selective CR0 write intercept test Date: Tue, 28 Oct 2025 22:12:12 +0000 Message-ID: <20251028221213.1937120-8-yosry.ahmed@linux.dev> In-Reply-To: <20251028221213.1937120-1-yosry.ahmed@linux.dev> References: <20251028221213.1937120-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" In preparation for adding more test cases, make the test easier to extend. Create a generic helper that sets an arbitrary bit in CR0, optionally using FEP. The helper also stores the value to be written in test->scratch, making it possible to double check if the write was actually executed or not. Use report_svm_guest() instead of report_fail() + exit(). Make test_sel_cr0_write_intercept() use the generic helper, and add another test case that sets FEP to exercise the interception path in the emulator. Finally, in check_sel_cr0_intercept() also check that the write was not executed by comparing CR0 value in the VMCB12 with the value-to-be-written stored in test->scratch. Signed-off-by: Yosry Ahmed --- x86/svm_tests.c | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/x86/svm_tests.c b/x86/svm_tests.c index 61ab63db462dc..71afb38a3928d 100644 --- a/x86/svm_tests.c +++ b/x86/svm_tests.c @@ -133,27 +133,36 @@ static void prepare_sel_cr0_intercept(struct svm_test= *test) vmcb->control.intercept |=3D (1ULL << INTERCEPT_SELECTIVE_CR0); } =20 -static void test_sel_cr0_write_intercept(struct svm_test *test) +static void __test_cr0_write_bit(struct svm_test *test, unsigned long bit, + bool intercept, bool fep) { unsigned long cr0; =20 - /* read cr0, set CD, and write back */ - cr0 =3D read_cr0(); - cr0 |=3D X86_CR0_CD; - write_cr0(cr0); + cr0 =3D read_cr0(); + cr0 |=3D bit; + test->scratch =3D cr0; =20 - /* - * If we are here the test failed, not sure what to do now because we - * are not in guest-mode anymore so we can't trigger an intercept. - * Trigger a tripple-fault for now. - */ - report_fail("sel_cr0 test. Can not recover from this - exiting"); - exit(report_summary()); + asm_conditional_fep_safe(fep, "mov %0,%%cr0", "r"(cr0)); + + /* This code should be unreachable when an intercept is expected */ + report_svm_guest(!intercept, test, "Expected intercept on CR0 write"); +} + +/* MOV-to-CR0 updating CR0.CD is intercepted by the selective intercept */ +static void test_sel_cr0_write_intercept(struct svm_test *test) +{ + __test_cr0_write_bit(test, X86_CR0_CD, true, false); +} + +static void test_sel_cr0_write_intercept_emul(struct svm_test *test) +{ + __test_cr0_write_bit(test, X86_CR0_CD, true, true); } =20 static bool check_sel_cr0_intercept(struct svm_test *test) { - return vmcb->control.exit_code =3D=3D SVM_EXIT_CR0_SEL_WRITE; + return vmcb->control.exit_code =3D=3D SVM_EXIT_CR0_SEL_WRITE && + vmcb->save.cr0 !=3D test->scratch; } =20 static void prepare_cr3_intercept(struct svm_test *test) @@ -3461,6 +3470,9 @@ struct svm_test svm_tests[] =3D { { "sel cr0 write intercept", default_supported, prepare_sel_cr0_intercept, default_prepare_gif_clear, test_sel_cr0_write_intercept, default_finished, check_sel_cr0_intercept= }, + { "sel cr0 write intercept emulate", fep_supported, + prepare_sel_cr0_intercept, default_prepare_gif_clear, + test_sel_cr0_write_intercept_emul, default_finished, check_sel_cr0_inte= rcept}, { "cr3 read intercept", default_supported, prepare_cr3_intercept, default_prepare_gif_clear, test_cr3_intercept, default_finished, check_cr3_intercept }, --=20 2.51.1.851.g4ebd6896fd-goog From nobody Sat Feb 7 18:28:54 2026 Received: from out-172.mta0.migadu.com (out-172.mta0.migadu.com [91.218.175.172]) (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 5DF503596F8 for ; Tue, 28 Oct 2025 22:12:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761689581; cv=none; b=DAd/zrMA/Uwq1OQ9QRMFx2M3wwHC2GpeF7UScz7czjv/dRXsZ5ci3rBGtyMZL9z/MASjuRkZVkfrUrOz6qe6/tmoEMOoZUKnam8IrX0SwlZH0gAYBt97y/r3uK/9/YuB60LHu7J7Zlf4lGcdAf1VA5TlmDA4bdF7+oV/hopYY9g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761689581; c=relaxed/simple; bh=GHWb51/LZqdZkItu3qtzEt5jNBHLFHrIajzD8Bkh1w8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RU0n1DOVawMwuwTmSteTU03Wgcu2wHgL1rJH62l5F8oB5y4cLjG3Q7pmijhkD7klXAQ7ntj2QKCIqdVKDp5nu4I/ZQKUTt2zilb7PKf03GVr+JhKaiXwqLPU3V0SqcTp+lMMGWfWV2T5mMdyerXr3FirPcxOImIEFkRBK6+QD8c= 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=v1ZoiGhA; arc=none smtp.client-ip=91.218.175.172 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="v1ZoiGhA" 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=1761689573; 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=CoM4XFkPY77V8Y9t0f2H5TV0K04vGajsPcV+ESHU9TA=; b=v1ZoiGhAw8QztEFla2JV00D6qtzXa3QwWvznxxRL20foVPElM9m3bPSn3DGarsFgFGrqr1 GgA4chizUgF43b5okDTNjuMqWttibBHBXVBfODbGyj9nZX89f5OOFX8sj3L8tMbAPFhXVm Y1xBnM9oAsViuIfePMSkv2V2n5seKCU= From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , Jim Mattson , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed Subject: [kvm-unit-tests v2 8/8] x86/svm: Add more selective CR0 write and LMSW test cases Date: Tue, 28 Oct 2025 22:12:13 +0000 Message-ID: <20251028221213.1937120-9-yosry.ahmed@linux.dev> In-Reply-To: <20251028221213.1937120-1-yosry.ahmed@linux.dev> References: <20251028221213.1937120-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" Add more test cases that cover: - The priority between selective and non-selective CR0 intercepts. - Writes to CR0 that should not intercept (e.g. CR0.MP). - Writes to CR0 using LMSW, which should always intercept (even when updating CR0.MP). Emulator variants of all test cases are added as well. The new tests exercises bugs fixed by: https://lore.kernel.org/kvm/20251024192918.3191141-1-yosry.ahmed@linux.dev/. Signed-off-by: Yosry Ahmed --- x86/svm_tests.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 4 deletions(-) diff --git a/x86/svm_tests.c b/x86/svm_tests.c index 71afb38a3928d..2981f459032cb 100644 --- a/x86/svm_tests.c +++ b/x86/svm_tests.c @@ -129,20 +129,36 @@ static bool finished_rsm_intercept(struct svm_test *t= est) =20 static void prepare_sel_cr0_intercept(struct svm_test *test) { + /* Clear CR0.MP and CR0.CD as the tests will set either of them */ + vmcb->save.cr0 &=3D ~X86_CR0_MP; vmcb->save.cr0 &=3D ~X86_CR0_CD; vmcb->control.intercept |=3D (1ULL << INTERCEPT_SELECTIVE_CR0); } =20 +static void prepare_sel_nonsel_cr0_intercepts(struct svm_test *test) +{ + /* Clear CR0.MP and CR0.CD as the tests will set either of them */ + vmcb->save.cr0 &=3D ~X86_CR0_MP; + vmcb->save.cr0 &=3D ~X86_CR0_CD; + vmcb->control.intercept_cr_write |=3D (1ULL << 0); + vmcb->control.intercept |=3D (1ULL << INTERCEPT_SELECTIVE_CR0); +} + static void __test_cr0_write_bit(struct svm_test *test, unsigned long bit, - bool intercept, bool fep) + bool is_lmsw, bool intercept, bool fep) { + unsigned short msw; unsigned long cr0; =20 cr0 =3D read_cr0(); cr0 |=3D bit; + msw =3D cr0 & 0xfUL; test->scratch =3D cr0; =20 - asm_conditional_fep_safe(fep, "mov %0,%%cr0", "r"(cr0)); + if (is_lmsw) + asm_conditional_fep_safe(fep, "lmsw %0", "r"(msw)); + else + asm_conditional_fep_safe(fep, "mov %0,%%cr0", "r"(cr0)); =20 /* This code should be unreachable when an intercept is expected */ report_svm_guest(!intercept, test, "Expected intercept on CR0 write"); @@ -151,12 +167,34 @@ static void __test_cr0_write_bit(struct svm_test *tes= t, unsigned long bit, /* MOV-to-CR0 updating CR0.CD is intercepted by the selective intercept */ static void test_sel_cr0_write_intercept(struct svm_test *test) { - __test_cr0_write_bit(test, X86_CR0_CD, true, false); + __test_cr0_write_bit(test, X86_CR0_CD, false, true, false); } =20 static void test_sel_cr0_write_intercept_emul(struct svm_test *test) { - __test_cr0_write_bit(test, X86_CR0_CD, true, true); + __test_cr0_write_bit(test, X86_CR0_CD, false, true, true); +} + +/* MOV-to-CR0 updating CR0.MP is NOT intercepted by the selective intercep= t */ +static void test_sel_cr0_write_nointercept(struct svm_test *test) +{ + __test_cr0_write_bit(test, X86_CR0_MP, false, false, false); +} + +static void test_sel_cr0_write_nointercept_emul(struct svm_test *test) +{ + __test_cr0_write_bit(test, X86_CR0_MP, false, false, true); +} + +/* LMSW updating CR0.MP is intercepted by the selective intercept */ +static void test_sel_cr0_lmsw_intercept(struct svm_test *test) +{ + __test_cr0_write_bit(test, X86_CR0_MP, true, false, false); +} + +static void test_sel_cr0_lmsw_intercept_emul(struct svm_test *test) +{ + __test_cr0_write_bit(test, X86_CR0_MP, true, false, true); } =20 static bool check_sel_cr0_intercept(struct svm_test *test) @@ -165,6 +203,18 @@ static bool check_sel_cr0_intercept(struct svm_test *t= est) vmcb->save.cr0 !=3D test->scratch; } =20 +static bool check_nonsel_cr0_intercept(struct svm_test *test) +{ + return vmcb->control.exit_code =3D=3D SVM_EXIT_WRITE_CR0 && + vmcb->save.cr0 !=3D test->scratch; +} + +static bool check_cr0_nointercept(struct svm_test *test) +{ + return vmcb->control.exit_code =3D=3D SVM_EXIT_VMMCALL && + vmcb->save.cr0 =3D=3D test->scratch; +} + static void prepare_cr3_intercept(struct svm_test *test) { default_prepare(test); @@ -3473,6 +3523,24 @@ struct svm_test svm_tests[] =3D { { "sel cr0 write intercept emulate", fep_supported, prepare_sel_cr0_intercept, default_prepare_gif_clear, test_sel_cr0_write_intercept_emul, default_finished, check_sel_cr0_inte= rcept}, + { "sel cr0 write intercept priority", default_supported, + prepare_sel_nonsel_cr0_intercepts, default_prepare_gif_clear, + test_sel_cr0_write_intercept, default_finished, check_nonsel_cr0_interc= ept}, + { "sel cr0 write intercept priority emulate", fep_supported, + prepare_sel_nonsel_cr0_intercepts, default_prepare_gif_clear, + test_sel_cr0_write_intercept_emul, default_finished, check_nonsel_cr0_i= ntercept}, + { "sel cr0 write nointercept", default_supported, + prepare_sel_cr0_intercept, default_prepare_gif_clear, + test_sel_cr0_write_nointercept, default_finished, check_cr0_nointercept= }, + { "sel cr0 write nointercept emulate", fep_supported, + prepare_sel_cr0_intercept, default_prepare_gif_clear, + test_sel_cr0_write_nointercept_emul, default_finished, check_cr0_nointe= rcept}, + { "sel cr0 lmsw intercept", default_supported, + prepare_sel_cr0_intercept, default_prepare_gif_clear, + test_sel_cr0_lmsw_intercept, default_finished, check_sel_cr0_intercept}, + { "sel cr0 lmsw intercept emulate", fep_supported, + prepare_sel_cr0_intercept, default_prepare_gif_clear, + test_sel_cr0_lmsw_intercept_emul, default_finished, check_sel_cr0_inter= cept}, { "cr3 read intercept", default_supported, prepare_cr3_intercept, default_prepare_gif_clear, test_cr3_intercept, default_finished, check_cr3_intercept }, --=20 2.51.1.851.g4ebd6896fd-goog