Skip to content

Commit

Permalink
selftests: kvm: split "launch" phase of SEV VM creation
Browse files Browse the repository at this point in the history
Allow the caller to set the initial state of the VM.  Doing this
before sev_vm_launch() matters for SEV-ES, since that is the
place where the VMSA is updated and after which the guest state
becomes sealed.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Message-ID: <20240404121327.3107131-17-pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
  • Loading branch information
Paolo Bonzini committed Apr 11, 2024
1 parent d18c864 commit 4c180a5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
3 changes: 2 additions & 1 deletion tools/testing/selftests/kvm/include/x86_64/sev.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ void sev_vm_launch(struct kvm_vm *vm, uint32_t policy);
void sev_vm_launch_measure(struct kvm_vm *vm, uint8_t *measurement);
void sev_vm_launch_finish(struct kvm_vm *vm);

struct kvm_vm *vm_sev_create_with_one_vcpu(uint32_t policy, void *guest_code,
struct kvm_vm *vm_sev_create_with_one_vcpu(uint32_t type, void *guest_code,
struct kvm_vcpu **cpu);
void vm_sev_launch(struct kvm_vm *vm, uint32_t policy, uint8_t *measurement);

kvm_static_assert(SEV_RET_SUCCESS == 0);

Expand Down
16 changes: 10 additions & 6 deletions tools/testing/selftests/kvm/lib/x86_64/sev.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,26 +113,30 @@ void sev_vm_launch_finish(struct kvm_vm *vm)
TEST_ASSERT_EQ(status.state, SEV_GUEST_STATE_RUNNING);
}

struct kvm_vm *vm_sev_create_with_one_vcpu(uint32_t policy, void *guest_code,
struct kvm_vm *vm_sev_create_with_one_vcpu(uint32_t type, void *guest_code,
struct kvm_vcpu **cpu)
{
struct vm_shape shape = {
.mode = VM_MODE_DEFAULT,
.type = policy & SEV_POLICY_ES ? KVM_X86_SEV_ES_VM : KVM_X86_SEV_VM,
.type = type,
};
struct kvm_vm *vm;
struct kvm_vcpu *cpus[1];
uint8_t measurement[512];

vm = __vm_create_with_vcpus(shape, 1, 0, guest_code, cpus);
*cpu = cpus[0];

return vm;
}

void vm_sev_launch(struct kvm_vm *vm, uint32_t policy, uint8_t *measurement)
{
sev_vm_launch(vm, policy);

/* TODO: Validate the measurement is as expected. */
if (!measurement)
measurement = alloca(256);

sev_vm_launch_measure(vm, measurement);

sev_vm_launch_finish(vm);

return vm;
}
7 changes: 6 additions & 1 deletion tools/testing/selftests/kvm/x86_64/sev_smoke_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ static void test_sev(void *guest_code, uint64_t policy)
struct kvm_vm *vm;
struct ucall uc;

vm = vm_sev_create_with_one_vcpu(policy, guest_code, &vcpu);
uint32_t type = policy & SEV_POLICY_ES ? KVM_X86_SEV_ES_VM : KVM_X86_SEV_VM;

vm = vm_sev_create_with_one_vcpu(type, guest_code, &vcpu);

/* TODO: Validate the measurement is as expected. */
vm_sev_launch(vm, policy, NULL);

for (;;) {
vcpu_run(vcpu);
Expand Down

0 comments on commit 4c180a5

Please sign in to comment.