Commit d18c8648 authored by Paolo Bonzini's avatar Paolo Bonzini

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: default avatarPaolo Bonzini <pbonzini@redhat.com>
Message-ID: <20240404121327.3107131-16-pbonzini@redhat.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent dfc083a1
...@@ -93,7 +93,6 @@ enum kvm_mem_region_type { ...@@ -93,7 +93,6 @@ enum kvm_mem_region_type {
struct kvm_vm { struct kvm_vm {
int mode; int mode;
unsigned long type; unsigned long type;
uint8_t subtype;
int kvm_fd; int kvm_fd;
int fd; int fd;
unsigned int pgtable_levels; unsigned int pgtable_levels;
...@@ -200,8 +199,8 @@ enum vm_guest_mode { ...@@ -200,8 +199,8 @@ enum vm_guest_mode {
struct vm_shape { struct vm_shape {
uint32_t type; uint32_t type;
uint8_t mode; uint8_t mode;
uint8_t subtype; uint8_t pad0;
uint16_t padding; uint16_t pad1;
}; };
kvm_static_assert(sizeof(struct vm_shape) == sizeof(uint64_t)); kvm_static_assert(sizeof(struct vm_shape) == sizeof(uint64_t));
......
...@@ -23,12 +23,6 @@ ...@@ -23,12 +23,6 @@
extern bool host_cpu_is_intel; extern bool host_cpu_is_intel;
extern bool host_cpu_is_amd; extern bool host_cpu_is_amd;
enum vm_guest_x86_subtype {
VM_SUBTYPE_NONE = 0,
VM_SUBTYPE_SEV,
VM_SUBTYPE_SEV_ES,
};
/* Forced emulation prefix, used to invoke the emulator unconditionally. */ /* Forced emulation prefix, used to invoke the emulator unconditionally. */
#define KVM_FEP "ud2; .byte 'k', 'v', 'm';" #define KVM_FEP "ud2; .byte 'k', 'v', 'm';"
......
...@@ -67,20 +67,8 @@ kvm_static_assert(SEV_RET_SUCCESS == 0); ...@@ -67,20 +67,8 @@ kvm_static_assert(SEV_RET_SUCCESS == 0);
__TEST_ASSERT_VM_VCPU_IOCTL(!ret, #cmd, ret, vm); \ __TEST_ASSERT_VM_VCPU_IOCTL(!ret, #cmd, ret, vm); \
}) })
static inline void sev_vm_init(struct kvm_vm *vm) void sev_vm_init(struct kvm_vm *vm);
{ void sev_es_vm_init(struct kvm_vm *vm);
vm->arch.sev_fd = open_sev_dev_path_or_exit();
vm_sev_ioctl(vm, KVM_SEV_INIT, NULL);
}
static inline void sev_es_vm_init(struct kvm_vm *vm)
{
vm->arch.sev_fd = open_sev_dev_path_or_exit();
vm_sev_ioctl(vm, KVM_SEV_ES_INIT, NULL);
}
static inline void sev_register_encrypted_memory(struct kvm_vm *vm, static inline void sev_register_encrypted_memory(struct kvm_vm *vm,
struct userspace_mem_region *region) struct userspace_mem_region *region)
......
...@@ -276,7 +276,6 @@ struct kvm_vm *____vm_create(struct vm_shape shape) ...@@ -276,7 +276,6 @@ struct kvm_vm *____vm_create(struct vm_shape shape)
vm->mode = shape.mode; vm->mode = shape.mode;
vm->type = shape.type; vm->type = shape.type;
vm->subtype = shape.subtype;
vm->pa_bits = vm_guest_mode_params[vm->mode].pa_bits; vm->pa_bits = vm_guest_mode_params[vm->mode].pa_bits;
vm->va_bits = vm_guest_mode_params[vm->mode].va_bits; vm->va_bits = vm_guest_mode_params[vm->mode].va_bits;
......
...@@ -578,10 +578,11 @@ void kvm_arch_vm_post_create(struct kvm_vm *vm) ...@@ -578,10 +578,11 @@ void kvm_arch_vm_post_create(struct kvm_vm *vm)
sync_global_to_guest(vm, host_cpu_is_intel); sync_global_to_guest(vm, host_cpu_is_intel);
sync_global_to_guest(vm, host_cpu_is_amd); sync_global_to_guest(vm, host_cpu_is_amd);
if (vm->subtype == VM_SUBTYPE_SEV) if (vm->type == KVM_X86_SEV_VM || vm->type == KVM_X86_SEV_ES_VM) {
sev_vm_init(vm); struct kvm_sev_init init = { 0 };
else if (vm->subtype == VM_SUBTYPE_SEV_ES)
sev_es_vm_init(vm); vm_sev_ioctl(vm, KVM_SEV_INIT2, &init);
}
} }
void vcpu_arch_set_entry_point(struct kvm_vcpu *vcpu, void *guest_code) 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) ...@@ -1081,9 +1082,12 @@ void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits)
void kvm_init_vm_address_properties(struct kvm_vm *vm) void kvm_init_vm_address_properties(struct kvm_vm *vm)
{ {
if (vm->subtype == VM_SUBTYPE_SEV || vm->subtype == VM_SUBTYPE_SEV_ES) { if (vm->type == KVM_X86_SEV_VM || vm->type == KVM_X86_SEV_ES_VM) {
vm->arch.sev_fd = open_sev_dev_path_or_exit();
vm->arch.c_bit = BIT_ULL(this_cpu_property(X86_PROPERTY_SEV_C_BIT)); vm->arch.c_bit = BIT_ULL(this_cpu_property(X86_PROPERTY_SEV_C_BIT));
vm->gpa_tag_mask = vm->arch.c_bit; vm->gpa_tag_mask = vm->arch.c_bit;
} else {
vm->arch.sev_fd = -1;
} }
} }
......
...@@ -35,6 +35,32 @@ static void encrypt_region(struct kvm_vm *vm, struct userspace_mem_region *regio ...@@ -35,6 +35,32 @@ static void encrypt_region(struct kvm_vm *vm, struct userspace_mem_region *regio
} }
} }
void sev_vm_init(struct kvm_vm *vm)
{
if (vm->type == KVM_X86_DEFAULT_VM) {
assert(vm->arch.sev_fd == -1);
vm->arch.sev_fd = open_sev_dev_path_or_exit();
vm_sev_ioctl(vm, KVM_SEV_INIT, NULL);
} else {
struct kvm_sev_init init = { 0 };
assert(vm->type == KVM_X86_SEV_VM);
vm_sev_ioctl(vm, KVM_SEV_INIT2, &init);
}
}
void sev_es_vm_init(struct kvm_vm *vm)
{
if (vm->type == KVM_X86_DEFAULT_VM) {
assert(vm->arch.sev_fd == -1);
vm->arch.sev_fd = open_sev_dev_path_or_exit();
vm_sev_ioctl(vm, KVM_SEV_ES_INIT, NULL);
} else {
struct kvm_sev_init init = { 0 };
assert(vm->type == KVM_X86_SEV_ES_VM);
vm_sev_ioctl(vm, KVM_SEV_INIT2, &init);
}
}
void sev_vm_launch(struct kvm_vm *vm, uint32_t policy) void sev_vm_launch(struct kvm_vm *vm, uint32_t policy)
{ {
struct kvm_sev_launch_start launch_start = { 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, ...@@ -91,10 +117,8 @@ struct kvm_vm *vm_sev_create_with_one_vcpu(uint32_t policy, void *guest_code,
struct kvm_vcpu **cpu) struct kvm_vcpu **cpu)
{ {
struct vm_shape shape = { struct vm_shape shape = {
.type = VM_TYPE_DEFAULT,
.mode = VM_MODE_DEFAULT, .mode = VM_MODE_DEFAULT,
.subtype = policy & SEV_POLICY_ES ? VM_SUBTYPE_SEV_ES : .type = policy & SEV_POLICY_ES ? KVM_X86_SEV_ES_VM : KVM_X86_SEV_VM,
VM_SUBTYPE_SEV,
}; };
struct kvm_vm *vm; struct kvm_vm *vm;
struct kvm_vcpu *cpus[1]; struct kvm_vcpu *cpus[1];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment