Loading tools/testing/selftests/kvm/include/x86/smm.h 0 → 100644 +17 −0 Original line number Diff line number Diff line // 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 */ tools/testing/selftests/kvm/lib/x86/processor.c +26 −0 Original line number Diff line number Diff line Loading @@ -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" Loading Loading @@ -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); } tools/testing/selftests/kvm/x86/smm_test.c +2 −25 Original line number Diff line number Diff line Loading @@ -14,13 +14,11 @@ #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 Loading Loading @@ -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; Loading @@ -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)) Loading Loading
tools/testing/selftests/kvm/include/x86/smm.h 0 → 100644 +17 −0 Original line number Diff line number Diff line // 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 */
tools/testing/selftests/kvm/lib/x86/processor.c +26 −0 Original line number Diff line number Diff line Loading @@ -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" Loading Loading @@ -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); }
tools/testing/selftests/kvm/x86/smm_test.c +2 −25 Original line number Diff line number Diff line Loading @@ -14,13 +14,11 @@ #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 Loading Loading @@ -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; Loading @@ -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)) Loading