]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
selftests: kvm: extract common functionality out of smm_test.c
authorPaolo Bonzini <pbonzini@redhat.com>
Mon, 9 Mar 2026 12:43:57 +0000 (13:43 +0100)
committerPaolo Bonzini <pbonzini@redhat.com>
Wed, 11 Mar 2026 17:41:12 +0000 (18:41 +0100)
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
tools/testing/selftests/kvm/include/x86/smm.h [new file with mode: 0644]
tools/testing/selftests/kvm/lib/x86/processor.c
tools/testing/selftests/kvm/x86/smm_test.c

diff --git a/tools/testing/selftests/kvm/include/x86/smm.h b/tools/testing/selftests/kvm/include/x86/smm.h
new file mode 100644 (file)
index 0000000..19337c3
--- /dev/null
@@ -0,0 +1,17 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#ifndef SELFTEST_KVM_SMM_H
+#define SELFTEST_KVM_SMM_H
+
+#include "kvm_util.h"
+
+#define SMRAM_SIZE     65536
+#define SMRAM_MEMSLOT  ((1 << 16) | 1)
+#define SMRAM_PAGES    (SMRAM_SIZE / PAGE_SIZE)
+
+void setup_smram(struct kvm_vm *vm, struct kvm_vcpu *vcpu,
+                uint64_t smram_gpa,
+                const void *smi_handler, size_t handler_size);
+
+void inject_smi(struct kvm_vcpu *vcpu);
+
+#endif /* SELFTEST_KVM_SMM_H */
index fab18e9be66c9821bf064ce70f99bbf10f369534..23a44941e2837d428b7faed29e248934c6f8f855 100644 (file)
@@ -8,6 +8,7 @@
 #include "kvm_util.h"
 #include "pmu.h"
 #include "processor.h"
+#include "smm.h"
 #include "svm_util.h"
 #include "sev.h"
 #include "vmx.h"
@@ -1444,3 +1445,28 @@ bool kvm_arch_has_default_irqchip(void)
 {
        return true;
 }
+
+void setup_smram(struct kvm_vm *vm, struct kvm_vcpu *vcpu,
+                uint64_t smram_gpa,
+                const void *smi_handler, size_t handler_size)
+{
+       vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, smram_gpa,
+                                   SMRAM_MEMSLOT, SMRAM_PAGES, 0);
+       TEST_ASSERT(vm_phy_pages_alloc(vm, SMRAM_PAGES, smram_gpa,
+                                      SMRAM_MEMSLOT) == smram_gpa,
+                   "Could not allocate guest physical addresses for SMRAM");
+
+       memset(addr_gpa2hva(vm, smram_gpa), 0x0, SMRAM_SIZE);
+       memcpy(addr_gpa2hva(vm, smram_gpa) + 0x8000, smi_handler, handler_size);
+       vcpu_set_msr(vcpu, MSR_IA32_SMBASE, smram_gpa);
+}
+
+void inject_smi(struct kvm_vcpu *vcpu)
+{
+       struct kvm_vcpu_events events;
+
+       vcpu_events_get(vcpu, &events);
+       events.smi.pending = 1;
+       events.flags |= KVM_VCPUEVENT_VALID_SMM;
+       vcpu_events_set(vcpu, &events);
+}
index 55c88d664a945630532ecc23f1266de2eae89c6f..ade8412bf94aac5b2452ba2b6422a39d08fda47c 100644 (file)
 #include "test_util.h"
 
 #include "kvm_util.h"
+#include "smm.h"
 
 #include "vmx.h"
 #include "svm_util.h"
 
-#define SMRAM_SIZE 65536
-#define SMRAM_MEMSLOT ((1 << 16) | 1)
-#define SMRAM_PAGES (SMRAM_SIZE / PAGE_SIZE)
 #define SMRAM_GPA 0x1000000
 #define SMRAM_STAGE 0xfe
 
@@ -113,18 +111,6 @@ static void guest_code(void *arg)
        sync_with_host(DONE);
 }
 
-void inject_smi(struct kvm_vcpu *vcpu)
-{
-       struct kvm_vcpu_events events;
-
-       vcpu_events_get(vcpu, &events);
-
-       events.smi.pending = 1;
-       events.flags |= KVM_VCPUEVENT_VALID_SMM;
-
-       vcpu_events_set(vcpu, &events);
-}
-
 int main(int argc, char *argv[])
 {
        vm_vaddr_t nested_gva = 0;
@@ -140,16 +126,7 @@ int main(int argc, char *argv[])
        /* Create VM */
        vm = vm_create_with_one_vcpu(&vcpu, guest_code);
 
-       vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, SMRAM_GPA,
-                                   SMRAM_MEMSLOT, SMRAM_PAGES, 0);
-       TEST_ASSERT(vm_phy_pages_alloc(vm, SMRAM_PAGES, SMRAM_GPA, SMRAM_MEMSLOT)
-                   == SMRAM_GPA, "could not allocate guest physical addresses?");
-
-       memset(addr_gpa2hva(vm, SMRAM_GPA), 0x0, SMRAM_SIZE);
-       memcpy(addr_gpa2hva(vm, SMRAM_GPA) + 0x8000, smi_handler,
-              sizeof(smi_handler));
-
-       vcpu_set_msr(vcpu, MSR_IA32_SMBASE, SMRAM_GPA);
+       setup_smram(vm, vcpu, SMRAM_GPA, smi_handler, sizeof(smi_handler));
 
        if (kvm_has_cap(KVM_CAP_NESTED_STATE)) {
                if (kvm_cpu_has(X86_FEATURE_SVM))