From: Manali Shukla <manali.shukla@amd.com>
To: <kvm@vger.kernel.org>, <linux-kselftest@vger.kernel.org>
Cc: <pbonzini@redhat.com>, <seanjc@google.com>, <shuah@kernel.org>,
<nikunj@amd.com>, <thomas.lendacky@amd.com>,
<vkuznets@redhat.com>, <manali.shukla@amd.com>, <bp@alien8.de>,
<ajones@ventanamicro.com>
Subject: [PATCH v3 5/5] KVM: selftests: KVM: SVM: Add Idle HLT intercept test
Date: Tue, 28 May 2024 04:19:26 +0000 [thread overview]
Message-ID: <20240528041926.3989-6-manali.shukla@amd.com> (raw)
In-Reply-To: <20240528041926.3989-1-manali.shukla@amd.com>
From: Manali Shukla <Manali.Shukla@amd.com>
Execution of the HLT instruction results in VMEXIT. Hypervisor observes
pending V_INTR and V_NMI events just after VMEXIT generated by HLT for
the vCPU and causes VM entry to service the pending events. The Idle
HLT intercept feature avoids the wasteful VMEXIT during halt if there
are pending V_INTR and V_NMI events for the vCPU.
The selftest for Idle HLT intercept instruments above-mentioned scenario.
Signed-off-by: Manali Shukla <Manali.Shukla@amd.com>
---
tools/testing/selftests/kvm/Makefile | 1 +
.../selftests/kvm/include/x86_64/processor.h | 1 +
.../selftests/kvm/x86_64/svm_idle_hlt_test.c | 89 +++++++++++++++++++
3 files changed, 91 insertions(+)
create mode 100644 tools/testing/selftests/kvm/x86_64/svm_idle_hlt_test.c
diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile
index 6de9994971c9..bd97586d7c04 100644
--- a/tools/testing/selftests/kvm/Makefile
+++ b/tools/testing/selftests/kvm/Makefile
@@ -93,6 +93,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/smaller_maxphyaddr_emulation_test
TEST_GEN_PROGS_x86_64 += x86_64/smm_test
TEST_GEN_PROGS_x86_64 += x86_64/state_test
TEST_GEN_PROGS_x86_64 += x86_64/vmx_preemption_timer_test
+TEST_GEN_PROGS_x86_64 += x86_64/svm_idle_hlt_test
TEST_GEN_PROGS_x86_64 += x86_64/svm_vmcall_test
TEST_GEN_PROGS_x86_64 += x86_64/svm_int_ctl_test
TEST_GEN_PROGS_x86_64 += x86_64/svm_nested_shutdown_test
diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h
index f74f31df96d2..0036937b1be4 100644
--- a/tools/testing/selftests/kvm/include/x86_64/processor.h
+++ b/tools/testing/selftests/kvm/include/x86_64/processor.h
@@ -192,6 +192,7 @@ struct kvm_x86_cpu_feature {
#define X86_FEATURE_PAUSEFILTER KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 10)
#define X86_FEATURE_PFTHRESHOLD KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 12)
#define X86_FEATURE_VGIF KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 16)
+#define X86_FEATURE_IDLE_HLT KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 30)
#define X86_FEATURE_SEV KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 1)
#define X86_FEATURE_SEV_ES KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 3)
diff --git a/tools/testing/selftests/kvm/x86_64/svm_idle_hlt_test.c b/tools/testing/selftests/kvm/x86_64/svm_idle_hlt_test.c
new file mode 100644
index 000000000000..594caac7194b
--- /dev/null
+++ b/tools/testing/selftests/kvm/x86_64/svm_idle_hlt_test.c
@@ -0,0 +1,89 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2024 Advanced Micro Devices, Inc.
+ *
+ */
+#include <kvm_util.h>
+#include <processor.h>
+#include <test_util.h>
+#include "svm_util.h"
+#include "apic.h"
+
+#define VINTR_VECTOR 0x30
+#define NUM_ITERATIONS 1000
+
+static bool irq_received;
+
+/*
+ * The guest code instruments the scenario where there is a V_INTR pending
+ * event available while hlt instruction is executed. The HLT VM Exit doesn't
+ * occur in above-mentioned scenario if Idle HLT intercept feature is enabled.
+ */
+
+static void guest_code(void)
+{
+ uint32_t icr_val;
+ int i;
+
+ xapic_enable();
+
+ icr_val = (APIC_DEST_SELF | APIC_INT_ASSERT | VINTR_VECTOR);
+
+ for (i = 0; i < NUM_ITERATIONS; i++) {
+ cli();
+ xapic_write_reg(APIC_ICR, icr_val);
+ safe_halt();
+ GUEST_ASSERT(READ_ONCE(irq_received));
+ WRITE_ONCE(irq_received, false);
+ }
+ GUEST_DONE();
+}
+
+static void guest_vintr_handler(struct ex_regs *regs)
+{
+ WRITE_ONCE(irq_received, true);
+ xapic_write_reg(APIC_EOI, 0x00);
+}
+
+int main(int argc, char *argv[])
+{
+ struct kvm_vm *vm;
+ struct kvm_vcpu *vcpu;
+ struct ucall uc;
+ uint64_t halt_exits, vintr_exits;
+
+ /* Check the extension for binary stats */
+ TEST_REQUIRE(this_cpu_has(X86_FEATURE_IDLE_HLT));
+ TEST_REQUIRE(kvm_has_cap(KVM_CAP_BINARY_STATS_FD));
+
+ vm = vm_create_with_one_vcpu(&vcpu, guest_code);
+
+ vm_install_exception_handler(vm, VINTR_VECTOR, guest_vintr_handler);
+ virt_pg_map(vm, APIC_DEFAULT_GPA, APIC_DEFAULT_GPA);
+
+ vcpu_run(vcpu);
+ TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
+
+ halt_exits = vcpu_get_stat(vcpu, HALT_EXITS);
+ vintr_exits = vcpu_get_stat(vcpu, IRQ_WINDOW_EXITS);
+
+ switch (get_ucall(vcpu, &uc)) {
+ case UCALL_ABORT:
+ REPORT_GUEST_ASSERT(uc);
+ /* NOT REACHED */
+ case UCALL_DONE:
+ break;
+
+ default:
+ TEST_FAIL("Unknown ucall 0x%lx.", uc.cmd);
+ }
+
+ TEST_ASSERT_EQ(halt_exits, 0);
+ pr_debug("Guest executed VINTR followed by halts: %d times.\n"
+ "The guest exited due to halt: %ld times and number\n"
+ "of vintr exits: %ld.\n",
+ NUM_ITERATIONS, halt_exits, vintr_exits);
+
+ kvm_vm_free(vm);
+ return 0;
+}
--
2.34.1
next prev parent reply other threads:[~2024-05-28 4:19 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-05-28 4:19 [PATCH v3 0/5] Add support for the Idle HLT intercept feature Manali Shukla
2024-05-28 4:19 ` [PATCH v3 1/5] x86/cpufeatures: Add CPUID feature bit for Idle HLT intercept Manali Shukla
2024-05-28 7:42 ` Borislav Petkov
2024-05-28 4:19 ` [PATCH v3 2/5] KVM: SVM: Add Idle HLT intercept support Manali Shukla
2024-05-28 4:19 ` [PATCH v3 3/5] KVM: selftests: Add safe_halt() and cli() helpers to common code Manali Shukla
2024-05-28 4:19 ` [PATCH v3 4/5] KVM: selftests: Add an interface to read the data of named vcpu stat Manali Shukla
2024-08-13 16:37 ` Sean Christopherson
2024-10-22 5:49 ` Manali Shukla
2024-05-28 4:19 ` Manali Shukla [this message]
2024-05-28 7:46 ` [PATCH v3 5/5] KVM: selftests: KVM: SVM: Add Idle HLT intercept test Chao Gao
2024-05-30 13:19 ` Manali Shukla
2024-05-31 6:49 ` Chao Gao
2024-06-19 17:09 ` Manali Shukla
2024-08-13 15:38 ` Sean Christopherson
2024-08-13 16:03 ` Sean Christopherson
2024-05-28 10:22 ` [PATCH v3 0/5] Add support for the Idle HLT intercept feature Paolo Bonzini
2024-06-04 0:47 ` Sean Christopherson
2024-06-04 13:21 ` Manali Shukla
2024-06-04 12:23 ` Manali Shukla
2024-08-13 16:19 ` Sean Christopherson
2024-10-22 10:35 ` Manali Shukla
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=20240528041926.3989-6-manali.shukla@amd.com \
--to=manali.shukla@amd.com \
--cc=ajones@ventanamicro.com \
--cc=bp@alien8.de \
--cc=kvm@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=nikunj@amd.com \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
--cc=shuah@kernel.org \
--cc=thomas.lendacky@amd.com \
--cc=vkuznets@redhat.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