From: Yosry Ahmed <yosry.ahmed@linux.dev>
To: Sean Christopherson <seanjc@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
Kevin Cheng <chengkev@google.com>,
kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
Yosry Ahmed <yosry.ahmed@linux.dev>
Subject: [PATCH v3 13/14] x86/svm: Add more LBRV test cases
Date: Mon, 10 Nov 2025 23:26:41 +0000 [thread overview]
Message-ID: <20251110232642.633672-14-yosry.ahmed@linux.dev> (raw)
In-Reply-To: <20251110232642.633672-1-yosry.ahmed@linux.dev>
Add tests exercising using LBR, disabling it, then running a guest which
enables and uses LBR but does not disable it.
Make sure that when LBRV is disabled by virtual host, the guest state
correctly leaks into virtual host, but not when LBRV is enabled. This
also exercises KVM disabling intercepts for LBRs in L2 but re-enabling
them when exiting to L1.
Signed-off-by: Yosry Ahmed <yosry.ahmed@linux.dev>
---
x86/svm_tests.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 80 insertions(+)
diff --git a/x86/svm_tests.c b/x86/svm_tests.c
index 33c92b17c87db..47a2edfbb6c9b 100644
--- a/x86/svm_tests.c
+++ b/x86/svm_tests.c
@@ -3031,11 +3031,14 @@ static __always_inline void get_lbr_ips(u64 *from, u64 *to)
extern u64 guest_branch0_from, guest_branch0_to;
extern u64 guest_branch2_from, guest_branch2_to;
+extern u64 guest_branch3_from, guest_branch3_to;
extern u64 host_branch0_from, host_branch0_to;
extern u64 host_branch2_from, host_branch2_to;
extern u64 host_branch3_from, host_branch3_to;
extern u64 host_branch4_from, host_branch4_to;
+extern u64 host_branch5_from, host_branch5_to;
+extern u64 host_branch6_from, host_branch6_to;
u64 dbgctl;
@@ -3095,6 +3098,23 @@ static void svm_lbrv_test_guest2(void)
asm volatile ("vmmcall\n");
}
+static void svm_lbrv_test_guest3(void)
+{
+ /*
+ * This guest expects LBR to be disabled, it enables LBR and does a
+ * branch, then exits to L1 without disabling LBR or doing more
+ * branches.
+ */
+ dbgctl = rdmsr(MSR_IA32_DEBUGCTLMSR);
+ TEST_EXPECT_EQ(dbgctl, 0);
+
+ wrmsr(MSR_IA32_DEBUGCTLMSR, DEBUGCTLMSR_LBR);
+ DO_BRANCH(guest_branch3);
+
+ /* Do not call the vmmcall() fn to avoid overriding the last branch */
+ asm volatile ("vmmcall\n\t");
+}
+
static void svm_lbrv_test0(void)
{
u64 from_ip, to_ip;
@@ -3156,6 +3176,33 @@ static void svm_lbrv_test2(void)
TEST_EXPECT_EQ((u64)&guest_branch2_to, to_ip);
}
+/*
+ * Test that without LBRV enabled, enabling LBR in the guest then exiting will
+ * keep LBR enabled and 'leak' state to the host correctly.
+ */
+static void svm_lbrv_test3(void)
+{
+ u64 from_ip, to_ip;
+
+ svm_setup_vmrun((u64)svm_lbrv_test_guest3);
+ vmcb->control.virt_ext = 0;
+
+ wrmsr(MSR_IA32_DEBUGCTLMSR, DEBUGCTLMSR_LBR);
+ DO_BRANCH(host_branch5);
+ wrmsr(MSR_IA32_DEBUGCTLMSR, 0);
+
+ SVM_BARE_VMRUN;
+ dbgctl = rdmsr(MSR_IA32_DEBUGCTLMSR);
+ wrmsr(MSR_IA32_DEBUGCTLMSR, 0);
+ TEST_EXPECT_EQ(dbgctl, DEBUGCTLMSR_LBR);
+
+ TEST_EXPECT_EQ(vmcb->control.exit_code, SVM_EXIT_VMMCALL);
+
+ get_lbr_ips(&from_ip, &to_ip);
+ TEST_EXPECT_EQ((u64)&guest_branch3_from, from_ip);
+ TEST_EXPECT_EQ((u64)&guest_branch3_to, to_ip);
+}
+
/* Test that with LBRV enabled, guest LBR state doesn't leak (1) */
static void svm_lbrv_nested_test1(void)
{
@@ -3217,6 +3264,37 @@ static void svm_lbrv_nested_test2(void)
TEST_EXPECT_EQ((u64)&host_branch4_to, to_ip);
}
+/*
+ * Test that with LBRV enabled, enabling LBR in the guest then exiting does not
+ * 'leak' state to the host.
+ */
+static void svm_lbrv_nested_test3(void)
+{
+ u64 from_ip, to_ip;
+
+ if (!lbrv_supported()) {
+ report_skip("LBRV not supported in the guest");
+ return;
+ }
+
+ svm_setup_vmrun((u64)svm_lbrv_test_guest3);
+ vmcb->control.virt_ext = LBR_CTL_ENABLE_MASK;
+ vmcb->save.dbgctl = 0;
+
+ wrmsr(MSR_IA32_DEBUGCTLMSR, DEBUGCTLMSR_LBR);
+ DO_BRANCH(host_branch6);
+ wrmsr(MSR_IA32_DEBUGCTLMSR, 0);
+
+ SVM_BARE_VMRUN;
+ dbgctl = rdmsr(MSR_IA32_DEBUGCTLMSR);
+ TEST_EXPECT_EQ(dbgctl, 0);
+
+ TEST_EXPECT_EQ(vmcb->control.exit_code, SVM_EXIT_VMMCALL);
+
+ get_lbr_ips(&from_ip, &to_ip);
+ TEST_EXPECT_EQ((u64)&host_branch6_from, from_ip);
+ TEST_EXPECT_EQ((u64)&host_branch6_to, to_ip);
+}
// test that a nested guest which does enable INTR interception
// but doesn't enable virtual interrupt masking works
@@ -3622,8 +3700,10 @@ struct svm_test svm_tests[] = {
TEST(svm_lbrv_test0),
TEST(svm_lbrv_test1),
TEST(svm_lbrv_test2),
+ TEST(svm_lbrv_test3),
TEST(svm_lbrv_nested_test1),
TEST(svm_lbrv_nested_test2),
+ TEST(svm_lbrv_nested_test3),
TEST(svm_intr_intercept_mix_if),
TEST(svm_intr_intercept_mix_gif),
TEST(svm_intr_intercept_mix_gif2),
--
2.51.2.1041.gc1ab5b90ca-goog
next prev parent reply other threads:[~2025-11-10 23:27 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-10 23:26 [PATCH v3 00/14] Improvements for (nested) SVM testing Yosry Ahmed
2025-11-10 23:26 ` [PATCH v3 01/14] scripts: Always return '2' when skipping tests Yosry Ahmed
2025-11-10 23:26 ` [PATCH v3 02/14] x86/vmx: Skip vmx_pf_exception_test_fep early if FEP is not available Yosry Ahmed
2025-11-14 0:40 ` Sean Christopherson
2025-11-14 0:47 ` Yosry Ahmed
2025-11-10 23:26 ` [PATCH v3 03/14] x86/svm: Cleanup selective cr0 write intercept test Yosry Ahmed
2025-11-12 13:48 ` Manali Shukla
2025-11-10 23:26 ` [PATCH v3 04/14] x86/svm: Move CR0 selective write intercept test near CR3 intercept Yosry Ahmed
2025-11-12 13:52 ` Manali Shukla
2025-11-10 23:26 ` [PATCH v3 05/14] x86/svm: Add FEP helpers for SVM tests Yosry Ahmed
2025-11-10 23:26 ` [PATCH v3 06/14] x86/svm: Report unsupported " Yosry Ahmed
2025-11-10 23:26 ` [PATCH v3 07/14] x86/svm: Move report_svm_guest() to the top of svm_tests.c Yosry Ahmed
2025-11-10 23:26 ` [PATCH v3 08/14] x86/svm: Print SVM test names before running tests Yosry Ahmed
2025-11-10 23:26 ` [PATCH v3 09/14] x86/svm: Deflake svm_tsc_scale_test Yosry Ahmed
2025-11-14 0:34 ` Sean Christopherson
2025-11-14 5:46 ` Yosry Ahmed
2025-11-10 23:26 ` [PATCH v3 10/14] x86/svm: Generalize and improve selective CR0 write intercept test Yosry Ahmed
2025-11-10 23:26 ` [PATCH v3 11/14] x86/svm: Add more selective CR0 write and LMSW test cases Yosry Ahmed
2025-11-10 23:26 ` [PATCH v3 12/14] x86/svm: Cleanup LBRV tests Yosry Ahmed
2025-11-13 11:58 ` Shivansh Dhiman
2025-11-13 14:59 ` Yosry Ahmed
2025-11-14 4:57 ` Shivansh Dhiman
2025-11-14 5:40 ` Yosry Ahmed
2025-11-10 23:26 ` Yosry Ahmed [this message]
2025-11-10 23:26 ` [PATCH v3 14/14] x86/svm: Rename VMCB fields to match KVM Yosry Ahmed
2025-11-14 0:40 ` Sean Christopherson
2025-11-11 0:52 ` [PATCH v3 00/14] Improvements for (nested) SVM testing Sean Christopherson
2025-11-11 0:58 ` Yosry Ahmed
2025-11-14 0:46 ` Sean Christopherson
2025-11-14 5:42 ` Yosry Ahmed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251110232642.633672-14-yosry.ahmed@linux.dev \
--to=yosry.ahmed@linux.dev \
--cc=chengkev@google.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®