Unified · Split
| @@ -0,0 +1,17 @@ | |||
| 1 | // SPDX-License-Identifier: GPL-2.0-only | ||
| 2 | #ifndef SELFTEST_KVM_SMM_H | ||
| 3 | #define SELFTEST_KVM_SMM_H | ||
| 4 | |||
| 5 | #include "kvm_util.h" | ||
| 6 | |||
| 7 | #define SMRAM_SIZE 65536 | ||
| 8 | #define SMRAM_MEMSLOT ((1 << 16) | 1) | ||
| 9 | #define SMRAM_PAGES (SMRAM_SIZE / PAGE_SIZE) | ||
| 10 | |||
| 11 | void setup_smram(struct kvm_vm *vm, struct kvm_vcpu *vcpu, | ||
| 12 | uint64_t smram_gpa, | ||
| 13 | const void *smi_handler, size_t handler_size); | ||
| 14 | |||
| 15 | void inject_smi(struct kvm_vcpu *vcpu); | ||
| 16 | |||
| 17 | #endif /* SELFTEST_KVM_SMM_H */ | ||
| @@ -8,6 +8,7 @@ · 7 unmodified lines | |||
| 8 | #include "kvm_util.h" | 8 | #include "kvm_util.h" |
| 9 | #include "pmu.h" | 9 | #include "pmu.h" |
| 10 | #include "processor.h" | 10 | #include "processor.h" |
| 11 | #include "smm.h" | ||
| 11 | #include "svm_util.h" | 12 | #include "svm_util.h" |
| 12 | #include "sev.h" | 13 | #include "sev.h" |
| 13 | #include "vmx.h" | 14 | #include "vmx.h" |
| @@ -1444,3 +1445,28 @@ bool kvm_arch_has_default_irqchip(void) · 1430 unmodified lines | |||
| 1444 | { | 1445 | { |
| 1445 | return true; | 1446 | return true; |
| 1446 | } | 1447 | } |
| 1448 | |||
| 1449 | void setup_smram(struct kvm_vm *vm, struct kvm_vcpu *vcpu, | ||
| 1450 | uint64_t smram_gpa, | ||
| 1451 | const void *smi_handler, size_t handler_size) | ||
| 1452 | { | ||
| 1453 | vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, smram_gpa, | ||
| 1454 | SMRAM_MEMSLOT, SMRAM_PAGES, 0); | ||
| 1455 | TEST_ASSERT(vm_phy_pages_alloc(vm, SMRAM_PAGES, smram_gpa, | ||
| 1456 | SMRAM_MEMSLOT) == smram_gpa, | ||
| 1457 | "Could not allocate guest physical addresses for SMRAM"); | ||
| 1458 | |||
| 1459 | memset(addr_gpa2hva(vm, smram_gpa), 0x0, SMRAM_SIZE); | ||
| 1460 | memcpy(addr_gpa2hva(vm, smram_gpa) + 0x8000, smi_handler, handler_size); | ||
| 1461 | vcpu_set_msr(vcpu, MSR_IA32_SMBASE, smram_gpa); | ||
| 1462 | } | ||
| 1463 | |||
| 1464 | void inject_smi(struct kvm_vcpu *vcpu) | ||
| 1465 | { | ||
| 1466 | struct kvm_vcpu_events events; | ||
| 1467 | |||
| 1468 | vcpu_events_get(vcpu, &events); | ||
| 1469 | events.smi.pending = 1; | ||
| 1470 | events.flags |= KVM_VCPUEVENT_VALID_SMM; | ||
| 1471 | vcpu_events_set(vcpu, &events); | ||
| 1472 | } | ||
| @@ -14,13 +14,11 @@ · 13 unmodified lines | |||
| 14 | #include "test_util.h" | 14 | #include "test_util.h" |
| 15 | 15 | ||
| 16 | #include "kvm_util.h" | 16 | #include "kvm_util.h" |
| 17 | #include "smm.h" | ||
| 17 | 18 | ||
| 18 | #include "vmx.h" | 19 | #include "vmx.h" |
| 19 | #include "svm_util.h" | 20 | #include "svm_util.h" |
| 20 | 21 | ||
| 21 | #define SMRAM_SIZE 65536 | ||
| 22 | #define SMRAM_MEMSLOT ((1 << 16) | 1) | ||
| 23 | #define SMRAM_PAGES (SMRAM_SIZE / PAGE_SIZE) | ||
| 24 | #define SMRAM_GPA 0x1000000 | 22 | #define SMRAM_GPA 0x1000000 |
| 25 | #define SMRAM_STAGE 0xfe | 23 | #define SMRAM_STAGE 0xfe |
| 26 | 24 | ||
| @@ -113,18 +111,6 @@ static void guest_code(void *arg) · 86 unmodified lines | |||
| 113 | sync_with_host(DONE); | 111 | sync_with_host(DONE); |
| 114 | } | 112 | } |
| 115 | 113 | ||
| 116 | void inject_smi(struct kvm_vcpu *vcpu) | ||
| 117 | { | ||
| 118 | struct kvm_vcpu_events events; | ||
| 119 | |||
| 120 | vcpu_events_get(vcpu, &events); | ||
| 121 | |||
| 122 | events.smi.pending = 1; | ||
| 123 | events.flags |= KVM_VCPUEVENT_VALID_SMM; | ||
| 124 | |||
| 125 | vcpu_events_set(vcpu, &events); | ||
| 126 | } | ||
| 127 | |||
| 128 | int main(int argc, char *argv[]) | 114 | int main(int argc, char *argv[]) |
| 129 | { | 115 | { |
| 130 | vm_vaddr_t nested_gva = 0; | 116 | vm_vaddr_t nested_gva = 0; |
| @@ -140,16 +126,7 @@ int main(int argc, char *argv[]) · 9 unmodified lines | |||
| 140 | /* Create VM */ | 126 | /* Create VM */ |
| 141 | vm = vm_create_with_one_vcpu(&vcpu, guest_code); | 127 | vm = vm_create_with_one_vcpu(&vcpu, guest_code); |
| 142 | 128 | ||
| 143 | vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, SMRAM_GPA, | 129 | setup_smram(vm, vcpu, SMRAM_GPA, smi_handler, sizeof(smi_handler)); |
| 144 | SMRAM_MEMSLOT, SMRAM_PAGES, 0); | ||
| 145 | TEST_ASSERT(vm_phy_pages_alloc(vm, SMRAM_PAGES, SMRAM_GPA, SMRAM_MEMSLOT) | ||
| 146 | == SMRAM_GPA, "could not allocate guest physical addresses?"); | ||
| 147 | |||
| 148 | memset(addr_gpa2hva(vm, SMRAM_GPA), 0x0, SMRAM_SIZE); | ||
| 149 | memcpy(addr_gpa2hva(vm, SMRAM_GPA) + 0x8000, smi_handler, | ||
| 150 | sizeof(smi_handler)); | ||
| 151 | |||
| 152 | vcpu_set_msr(vcpu, MSR_IA32_SMBASE, SMRAM_GPA); | ||
| 153 | 130 | ||
| 154 | if (kvm_has_cap(KVM_CAP_NESTED_STATE)) { | 131 | if (kvm_has_cap(KVM_CAP_NESTED_STATE)) { |
| 155 | if (kvm_cpu_has(X86_FEATURE_SVM)) | 132 | if (kvm_cpu_has(X86_FEATURE_SVM)) |