Skip to content

Commit 24a7e94

Browse files
vittyvksean-jc
authored andcommitted
KVM: selftests: Move Hyper-V specific functions out of processor.c
Since there is 'hyperv.c' for Hyper-V specific functions already, move Hyper-V specific functions out of processor.c there. No functional change intended. Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com> Link: https://lore.kernel.org/r/20240816130139.286246-2-vkuznets@redhat.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 47ac09b commit 24a7e94

File tree

5 files changed

+68
-64
lines changed

5 files changed

+68
-64
lines changed

tools/testing/selftests/kvm/include/x86_64/hyperv.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,4 +343,8 @@ struct hyperv_test_pages *vcpu_alloc_hyperv_test_pages(struct kvm_vm *vm,
343343
/* HV_X64_MSR_TSC_INVARIANT_CONTROL bits */
344344
#define HV_INVARIANT_TSC_EXPOSED BIT_ULL(0)
345345

346+
const struct kvm_cpuid2 *kvm_get_supported_hv_cpuid(void);
347+
const struct kvm_cpuid2 *vcpu_get_supported_hv_cpuid(struct kvm_vcpu *vcpu);
348+
void vcpu_set_hv_cpuid(struct kvm_vcpu *vcpu);
349+
346350
#endif /* !SELFTEST_KVM_HYPERV_H */

tools/testing/selftests/kvm/include/x86_64/processor.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ extern bool host_cpu_is_intel;
2525
extern bool host_cpu_is_amd;
2626
extern uint64_t guest_tsc_khz;
2727

28+
#ifndef MAX_NR_CPUID_ENTRIES
29+
#define MAX_NR_CPUID_ENTRIES 100
30+
#endif
31+
2832
/* Forced emulation prefix, used to invoke the emulator unconditionally. */
2933
#define KVM_FEP "ud2; .byte 'k', 'v', 'm';"
3034

@@ -908,8 +912,6 @@ static inline void vcpu_xcrs_set(struct kvm_vcpu *vcpu, struct kvm_xcrs *xcrs)
908912
const struct kvm_cpuid_entry2 *get_cpuid_entry(const struct kvm_cpuid2 *cpuid,
909913
uint32_t function, uint32_t index);
910914
const struct kvm_cpuid2 *kvm_get_supported_cpuid(void);
911-
const struct kvm_cpuid2 *kvm_get_supported_hv_cpuid(void);
912-
const struct kvm_cpuid2 *vcpu_get_supported_hv_cpuid(struct kvm_vcpu *vcpu);
913915

914916
static inline uint32_t kvm_cpu_fms(void)
915917
{
@@ -1009,7 +1011,6 @@ static inline struct kvm_cpuid2 *allocate_kvm_cpuid2(int nr_entries)
10091011
}
10101012

10111013
void vcpu_init_cpuid(struct kvm_vcpu *vcpu, const struct kvm_cpuid2 *cpuid);
1012-
void vcpu_set_hv_cpuid(struct kvm_vcpu *vcpu);
10131014

10141015
static inline struct kvm_cpuid_entry2 *__vcpu_get_cpuid_entry(struct kvm_vcpu *vcpu,
10151016
uint32_t function,

tools/testing/selftests/kvm/lib/x86_64/hyperv.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,65 @@
88
#include "processor.h"
99
#include "hyperv.h"
1010

11+
const struct kvm_cpuid2 *kvm_get_supported_hv_cpuid(void)
12+
{
13+
static struct kvm_cpuid2 *cpuid;
14+
int kvm_fd;
15+
16+
if (cpuid)
17+
return cpuid;
18+
19+
cpuid = allocate_kvm_cpuid2(MAX_NR_CPUID_ENTRIES);
20+
kvm_fd = open_kvm_dev_path_or_exit();
21+
22+
kvm_ioctl(kvm_fd, KVM_GET_SUPPORTED_HV_CPUID, cpuid);
23+
24+
close(kvm_fd);
25+
return cpuid;
26+
}
27+
28+
void vcpu_set_hv_cpuid(struct kvm_vcpu *vcpu)
29+
{
30+
static struct kvm_cpuid2 *cpuid_full;
31+
const struct kvm_cpuid2 *cpuid_sys, *cpuid_hv;
32+
int i, nent = 0;
33+
34+
if (!cpuid_full) {
35+
cpuid_sys = kvm_get_supported_cpuid();
36+
cpuid_hv = kvm_get_supported_hv_cpuid();
37+
38+
cpuid_full = allocate_kvm_cpuid2(cpuid_sys->nent + cpuid_hv->nent);
39+
if (!cpuid_full) {
40+
perror("malloc");
41+
abort();
42+
}
43+
44+
/* Need to skip KVM CPUID leaves 0x400000xx */
45+
for (i = 0; i < cpuid_sys->nent; i++) {
46+
if (cpuid_sys->entries[i].function >= 0x40000000 &&
47+
cpuid_sys->entries[i].function < 0x40000100)
48+
continue;
49+
cpuid_full->entries[nent] = cpuid_sys->entries[i];
50+
nent++;
51+
}
52+
53+
memcpy(&cpuid_full->entries[nent], cpuid_hv->entries,
54+
cpuid_hv->nent * sizeof(struct kvm_cpuid_entry2));
55+
cpuid_full->nent = nent + cpuid_hv->nent;
56+
}
57+
58+
vcpu_init_cpuid(vcpu, cpuid_full);
59+
}
60+
61+
const struct kvm_cpuid2 *vcpu_get_supported_hv_cpuid(struct kvm_vcpu *vcpu)
62+
{
63+
struct kvm_cpuid2 *cpuid = allocate_kvm_cpuid2(MAX_NR_CPUID_ENTRIES);
64+
65+
vcpu_ioctl(vcpu, KVM_GET_SUPPORTED_HV_CPUID, cpuid);
66+
67+
return cpuid;
68+
}
69+
1170
struct hyperv_test_pages *vcpu_alloc_hyperv_test_pages(struct kvm_vm *vm,
1271
vm_vaddr_t *p_hv_pages_gva)
1372
{

tools/testing/selftests/kvm/lib/x86_64/processor.c

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
#define KERNEL_DS 0x10
2020
#define KERNEL_TSS 0x18
2121

22-
#define MAX_NR_CPUID_ENTRIES 100
23-
2422
vm_vaddr_t exception_handlers;
2523
bool host_cpu_is_amd;
2624
bool host_cpu_is_intel;
@@ -1195,65 +1193,6 @@ void xen_hypercall(uint64_t nr, uint64_t a0, void *a1)
11951193
GUEST_ASSERT(!__xen_hypercall(nr, a0, a1));
11961194
}
11971195

1198-
const struct kvm_cpuid2 *kvm_get_supported_hv_cpuid(void)
1199-
{
1200-
static struct kvm_cpuid2 *cpuid;
1201-
int kvm_fd;
1202-
1203-
if (cpuid)
1204-
return cpuid;
1205-
1206-
cpuid = allocate_kvm_cpuid2(MAX_NR_CPUID_ENTRIES);
1207-
kvm_fd = open_kvm_dev_path_or_exit();
1208-
1209-
kvm_ioctl(kvm_fd, KVM_GET_SUPPORTED_HV_CPUID, cpuid);
1210-
1211-
close(kvm_fd);
1212-
return cpuid;
1213-
}
1214-
1215-
void vcpu_set_hv_cpuid(struct kvm_vcpu *vcpu)
1216-
{
1217-
static struct kvm_cpuid2 *cpuid_full;
1218-
const struct kvm_cpuid2 *cpuid_sys, *cpuid_hv;
1219-
int i, nent = 0;
1220-
1221-
if (!cpuid_full) {
1222-
cpuid_sys = kvm_get_supported_cpuid();
1223-
cpuid_hv = kvm_get_supported_hv_cpuid();
1224-
1225-
cpuid_full = allocate_kvm_cpuid2(cpuid_sys->nent + cpuid_hv->nent);
1226-
if (!cpuid_full) {
1227-
perror("malloc");
1228-
abort();
1229-
}
1230-
1231-
/* Need to skip KVM CPUID leaves 0x400000xx */
1232-
for (i = 0; i < cpuid_sys->nent; i++) {
1233-
if (cpuid_sys->entries[i].function >= 0x40000000 &&
1234-
cpuid_sys->entries[i].function < 0x40000100)
1235-
continue;
1236-
cpuid_full->entries[nent] = cpuid_sys->entries[i];
1237-
nent++;
1238-
}
1239-
1240-
memcpy(&cpuid_full->entries[nent], cpuid_hv->entries,
1241-
cpuid_hv->nent * sizeof(struct kvm_cpuid_entry2));
1242-
cpuid_full->nent = nent + cpuid_hv->nent;
1243-
}
1244-
1245-
vcpu_init_cpuid(vcpu, cpuid_full);
1246-
}
1247-
1248-
const struct kvm_cpuid2 *vcpu_get_supported_hv_cpuid(struct kvm_vcpu *vcpu)
1249-
{
1250-
struct kvm_cpuid2 *cpuid = allocate_kvm_cpuid2(MAX_NR_CPUID_ENTRIES);
1251-
1252-
vcpu_ioctl(vcpu, KVM_GET_SUPPORTED_HV_CPUID, cpuid);
1253-
1254-
return cpuid;
1255-
}
1256-
12571196
unsigned long vm_compute_max_gfn(struct kvm_vm *vm)
12581197
{
12591198
const unsigned long num_ht_pages = 12 << (30 - vm->page_shift); /* 12 GiB */

tools/testing/selftests/kvm/x86_64/xen_vmcall_test.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "test_util.h"
1111
#include "kvm_util.h"
1212
#include "processor.h"
13+
#include "hyperv.h"
1314

1415
#define HCALL_REGION_GPA 0xc0000000ULL
1516
#define HCALL_REGION_SLOT 10

0 commit comments

Comments
 (0)