Re: [RFC PATCH v2 02/17] KVM: selftest: Add helper functions to create TDX VMs

From: Isaku Yamahata
Date: Wed Aug 31 2022 - 21:20:43 EST


On Tue, Aug 30, 2022 at 10:19:45PM +0000,
Sagi Shahar <sagis@xxxxxxxxxx> wrote:

> diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c
> index f35626df1dea..2a6e28c769f2 100644
> --- a/tools/testing/selftests/kvm/lib/x86_64/processor.c
> +++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c
> @@ -8,6 +8,7 @@
> #include "test_util.h"
> #include "kvm_util.h"
> #include "processor.h"
> +#include "tdx.h"
>
> #ifndef NUM_INTERRUPTS
> #define NUM_INTERRUPTS 256
> @@ -641,6 +642,32 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,
> return vcpu;
> }
>
> +/*
> + * Adds a vCPU to a TD (Trusted Domain) with minimum defaults. It will not set
> + * up any general purpose registers as they will be initialized by the TDX. In
> + * TDX, vCPUs RIP is set to 0xFFFFFFF0. See Intel TDX EAS Section "Initial State
> + * of Guest GPRs" for more information on vCPUs initial register values when
> + * entering the TD first time.
> + *
> + * Input Args:
> + * vm - Virtual Machine
> + * vcpuid - The id of the VCPU to add to the VM.
> + */
> +struct kvm_vcpu *vm_vcpu_add_tdx(struct kvm_vm *vm, uint32_t vcpu_id)
> +{
> + struct kvm_mp_state mp_state;
> + struct kvm_vcpu *vcpu;
> +
> + vcpu = __vm_vcpu_add(vm, vcpu_id);
> + initialize_td_vcpu(vcpu);
> +
> + /* Setup the MP state */
> + mp_state.mp_state = 0;
> + vcpu_mp_state_set(vcpu, &mp_state);
> +
> + return vcpu;
> +}
> +

It's better to use symbolic value. I know this is copied from vmx version, though.

diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c
index 3bb7dc5a55ea..4009bc926e33 100644
--- a/tools/testing/selftests/kvm/lib/x86_64/processor.c
+++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c
@@ -636,7 +636,7 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,
vcpu_regs_set(vcpu, &regs);

/* Setup the MP state */
- mp_state.mp_state = 0;
+ mp_state.mp_state = KVM_MP_STATE_RUNNABLE;
vcpu_mp_state_set(vcpu, &mp_state);

return vcpu;
@@ -662,7 +662,7 @@ struct kvm_vcpu *vm_vcpu_add_tdx(struct kvm_vm *vm, uint32_t vcpu_id)
initialize_td_vcpu(vcpu);

/* Setup the MP state */
- mp_state.mp_state = 0;
+ mp_state.mp_state = KVM_MP_STATE_RUNNABLE;
vcpu_mp_state_set(vcpu, &mp_state);

return vcpu;


--
Isaku Yamahata <isaku.yamahata@xxxxxxxxx>