History

selftests: kvm: extract common functionality out of smm_test.c

Paolo Bonzini committed 8 months ago

Commit c52b5341 parent be5fa873 files · +45 −25Browse files

Unified · Split

3 files
  1. tools/testing/selftests/kvm/include/x86/smm.h +17 −0
  2. tools/testing/selftests/kvm/lib/x86/processor.c +26 −0
  3. tools/testing/selftests/kvm/x86/smm_test.c +2 −25
tools/testing/selftests/kvm/include/x86/smm.h+17−0
@@ -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 */
tools/testing/selftests/kvm/lib/x86/processor.c+26−0
@@ -8,6 +8,7 @@ · 7 unmodified lines
88 #include "kvm_util.h"
99 #include "pmu.h"
1010 #include "processor.h"
11+#include "smm.h"
1112 #include "svm_util.h"
1213 #include "sev.h"
1314 #include "vmx.h"
@@ -1444,3 +1445,28 @@ bool kvm_arch_has_default_irqchip(void) · 1430 unmodified lines
14441445 {
14451446 return true;
14461447 }
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+}
tools/testing/selftests/kvm/x86/smm_test.c+2−25
@@ -14,13 +14,11 @@ · 13 unmodified lines
1414 #include "test_util.h"
1515
1616 #include "kvm_util.h"
17+#include "smm.h"
1718
1819 #include "vmx.h"
1920 #include "svm_util.h"
2021
21−#define SMRAM_SIZE 65536
22−#define SMRAM_MEMSLOT ((1 << 16) | 1)
23−#define SMRAM_PAGES (SMRAM_SIZE / PAGE_SIZE)
2422 #define SMRAM_GPA 0x1000000
2523 #define SMRAM_STAGE 0xfe
2624
@@ -113,18 +111,6 @@ static void guest_code(void *arg) · 86 unmodified lines
113111 sync_with_host(DONE);
114112 }
115113
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−
128114 int main(int argc, char *argv[])
129115 {
130116 vm_vaddr_t nested_gva = 0;
@@ -140,16 +126,7 @@ int main(int argc, char *argv[]) · 9 unmodified lines
140126 /* Create VM */
141127 vm = vm_create_with_one_vcpu(&vcpu, guest_code);
142128
143− vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, SMRAM_GPA,
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);
129+ setup_smram(vm, vcpu, SMRAM_GPA, smi_handler, sizeof(smi_handler));
153130
154131 if (kvm_has_cap(KVM_CAP_NESTED_STATE)) {
155132 if (kvm_cpu_has(X86_FEATURE_SVM))