Skip to content

Commit d18c864

Browse files
committed
selftests: kvm: switch to using KVM_X86_*_VM
This removes the concept of "subtypes", instead letting the tests use proper VM types that were recently added. While the sev_init_vm() and sev_es_init_vm() are still able to operate with the legacy KVM_SEV_INIT and KVM_SEV_ES_INIT ioctls, this is limited to VMs that are created manually with vm_create_barebones(). Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Message-ID: <20240404121327.3107131-16-pbonzini@redhat.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent dfc083a commit d18c864

6 files changed

Lines changed: 40 additions & 32 deletions

File tree

tools/testing/selftests/kvm/include/kvm_util_base.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ enum kvm_mem_region_type {
9393
struct kvm_vm {
9494
int mode;
9595
unsigned long type;
96-
uint8_t subtype;
9796
int kvm_fd;
9897
int fd;
9998
unsigned int pgtable_levels;
@@ -200,8 +199,8 @@ enum vm_guest_mode {
200199
struct vm_shape {
201200
uint32_t type;
202201
uint8_t mode;
203-
uint8_t subtype;
204-
uint16_t padding;
202+
uint8_t pad0;
203+
uint16_t pad1;
205204
};
206205

207206
kvm_static_assert(sizeof(struct vm_shape) == sizeof(uint64_t));

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@
2323
extern bool host_cpu_is_intel;
2424
extern bool host_cpu_is_amd;
2525

26-
enum vm_guest_x86_subtype {
27-
VM_SUBTYPE_NONE = 0,
28-
VM_SUBTYPE_SEV,
29-
VM_SUBTYPE_SEV_ES,
30-
};
31-
3226
/* Forced emulation prefix, used to invoke the emulator unconditionally. */
3327
#define KVM_FEP "ud2; .byte 'k', 'v', 'm';"
3428

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

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,8 @@ kvm_static_assert(SEV_RET_SUCCESS == 0);
6767
__TEST_ASSERT_VM_VCPU_IOCTL(!ret, #cmd, ret, vm); \
6868
})
6969

70-
static inline void sev_vm_init(struct kvm_vm *vm)
71-
{
72-
vm->arch.sev_fd = open_sev_dev_path_or_exit();
73-
74-
vm_sev_ioctl(vm, KVM_SEV_INIT, NULL);
75-
}
76-
77-
78-
static inline void sev_es_vm_init(struct kvm_vm *vm)
79-
{
80-
vm->arch.sev_fd = open_sev_dev_path_or_exit();
81-
82-
vm_sev_ioctl(vm, KVM_SEV_ES_INIT, NULL);
83-
}
70+
void sev_vm_init(struct kvm_vm *vm);
71+
void sev_es_vm_init(struct kvm_vm *vm);
8472

8573
static inline void sev_register_encrypted_memory(struct kvm_vm *vm,
8674
struct userspace_mem_region *region)

tools/testing/selftests/kvm/lib/kvm_util.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,6 @@ struct kvm_vm *____vm_create(struct vm_shape shape)
276276

277277
vm->mode = shape.mode;
278278
vm->type = shape.type;
279-
vm->subtype = shape.subtype;
280279

281280
vm->pa_bits = vm_guest_mode_params[vm->mode].pa_bits;
282281
vm->va_bits = vm_guest_mode_params[vm->mode].va_bits;

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -578,10 +578,11 @@ void kvm_arch_vm_post_create(struct kvm_vm *vm)
578578
sync_global_to_guest(vm, host_cpu_is_intel);
579579
sync_global_to_guest(vm, host_cpu_is_amd);
580580

581-
if (vm->subtype == VM_SUBTYPE_SEV)
582-
sev_vm_init(vm);
583-
else if (vm->subtype == VM_SUBTYPE_SEV_ES)
584-
sev_es_vm_init(vm);
581+
if (vm->type == KVM_X86_SEV_VM || vm->type == KVM_X86_SEV_ES_VM) {
582+
struct kvm_sev_init init = { 0 };
583+
584+
vm_sev_ioctl(vm, KVM_SEV_INIT2, &init);
585+
}
585586
}
586587

587588
void vcpu_arch_set_entry_point(struct kvm_vcpu *vcpu, void *guest_code)
@@ -1081,9 +1082,12 @@ void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits)
10811082

10821083
void kvm_init_vm_address_properties(struct kvm_vm *vm)
10831084
{
1084-
if (vm->subtype == VM_SUBTYPE_SEV || vm->subtype == VM_SUBTYPE_SEV_ES) {
1085+
if (vm->type == KVM_X86_SEV_VM || vm->type == KVM_X86_SEV_ES_VM) {
1086+
vm->arch.sev_fd = open_sev_dev_path_or_exit();
10851087
vm->arch.c_bit = BIT_ULL(this_cpu_property(X86_PROPERTY_SEV_C_BIT));
10861088
vm->gpa_tag_mask = vm->arch.c_bit;
1089+
} else {
1090+
vm->arch.sev_fd = -1;
10871091
}
10881092
}
10891093

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,32 @@ static void encrypt_region(struct kvm_vm *vm, struct userspace_mem_region *regio
3535
}
3636
}
3737

38+
void sev_vm_init(struct kvm_vm *vm)
39+
{
40+
if (vm->type == KVM_X86_DEFAULT_VM) {
41+
assert(vm->arch.sev_fd == -1);
42+
vm->arch.sev_fd = open_sev_dev_path_or_exit();
43+
vm_sev_ioctl(vm, KVM_SEV_INIT, NULL);
44+
} else {
45+
struct kvm_sev_init init = { 0 };
46+
assert(vm->type == KVM_X86_SEV_VM);
47+
vm_sev_ioctl(vm, KVM_SEV_INIT2, &init);
48+
}
49+
}
50+
51+
void sev_es_vm_init(struct kvm_vm *vm)
52+
{
53+
if (vm->type == KVM_X86_DEFAULT_VM) {
54+
assert(vm->arch.sev_fd == -1);
55+
vm->arch.sev_fd = open_sev_dev_path_or_exit();
56+
vm_sev_ioctl(vm, KVM_SEV_ES_INIT, NULL);
57+
} else {
58+
struct kvm_sev_init init = { 0 };
59+
assert(vm->type == KVM_X86_SEV_ES_VM);
60+
vm_sev_ioctl(vm, KVM_SEV_INIT2, &init);
61+
}
62+
}
63+
3864
void sev_vm_launch(struct kvm_vm *vm, uint32_t policy)
3965
{
4066
struct kvm_sev_launch_start launch_start = {
@@ -91,10 +117,8 @@ struct kvm_vm *vm_sev_create_with_one_vcpu(uint32_t policy, void *guest_code,
91117
struct kvm_vcpu **cpu)
92118
{
93119
struct vm_shape shape = {
94-
.type = VM_TYPE_DEFAULT,
95120
.mode = VM_MODE_DEFAULT,
96-
.subtype = policy & SEV_POLICY_ES ? VM_SUBTYPE_SEV_ES :
97-
VM_SUBTYPE_SEV,
121+
.type = policy & SEV_POLICY_ES ? KVM_X86_SEV_ES_VM : KVM_X86_SEV_VM,
98122
};
99123
struct kvm_vm *vm;
100124
struct kvm_vcpu *cpus[1];

0 commit comments

Comments
 (0)