Skip to content

Commit c52b534

Browse files
committed
selftests: kvm: extract common functionality out of smm_test.c
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent be5fa87 commit c52b534

3 files changed

Lines changed: 45 additions & 25 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
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)
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

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@
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)
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[])
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))

0 commit comments

Comments
 (0)