On Wed, Apr 02, 2025 at 10:52:38PM +0800, Xiaoyao Li wrote:
> On 4/2/2025 7:00 PM, Daniel P. Berrangé wrote:
> > On Tue, Apr 01, 2025 at 09:01:15AM -0400, Xiaoyao Li wrote:
> > > KVM provides TDX capabilities via sub command KVM_TDX_CAPABILITIES of
> > > IOCTL(KVM_MEMORY_ENCRYPT_OP). Get the capabilities when initializing
> > > TDX context. It will be used to validate user's setting later.
> > > 
> > > Since there is no interface reporting how many cpuid configs contains in
> > > KVM_TDX_CAPABILITIES, QEMU chooses to try starting with a known number
> > > and abort when it exceeds KVM_MAX_CPUID_ENTRIES.
> > > 
> > > Besides, introduce the interfaces to invoke TDX "ioctls" at VCPU scope
> > > in preparation.
> > > 
> > > Signed-off-by: Xiaoyao Li <[email protected]>
> > > ---
> > > Changes in v7:
> > > - refine and unifiy the error handling; (Daniel)
> > > 
> > > Changes in v6:
> > > - Pass CPUState * to tdx_vcpu_ioctl();
> > > - update commit message to remove platform scope thing;
> > > - dump hw_error when it's non-zero to help debug;
> > > 
> > > Changes in v4:
> > > - use {} to initialize struct kvm_tdx_cmd, to avoid memset();
> > > - remove tdx_platform_ioctl() because no user;
> > > 
> > > Changes in v3:
> > > - rename __tdx_ioctl() to tdx_ioctl_internal()
> > > - Pass errp in get_tdx_capabilities();
> > > 
> > > changes in v2:
> > >    - Make the error message more clear;
> > > 
> > > changes in v1:
> > >    - start from nr_cpuid_configs = 6 for the loop;
> > >    - stop the loop when nr_cpuid_configs exceeds KVM_MAX_CPUID_ENTRIES;
> > > ---
> > >   target/i386/kvm/kvm.c      |   2 -
> > >   target/i386/kvm/kvm_i386.h |   2 +
> > >   target/i386/kvm/tdx.c      | 107 ++++++++++++++++++++++++++++++++++++-
> > >   3 files changed, 108 insertions(+), 3 deletions(-)
> > > 
> > > diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c
> > > index 1af4710556ad..b4fa35405fe1 100644
> > > --- a/target/i386/kvm/kvm.c
> > > +++ b/target/i386/kvm/kvm.c
> > > @@ -1779,8 +1779,6 @@ static int hyperv_init_vcpu(X86CPU *cpu)
> > >   static Error *invtsc_mig_blocker;
> > > -#define KVM_MAX_CPUID_ENTRIES  100
> > > -
> > >   static void kvm_init_xsave(CPUX86State *env)
> > >   {
> > >       if (has_xsave2) {
> > > diff --git a/target/i386/kvm/kvm_i386.h b/target/i386/kvm/kvm_i386.h
> > > index 88565e8dbac1..ed1e61fb8ba9 100644
> > > --- a/target/i386/kvm/kvm_i386.h
> > > +++ b/target/i386/kvm/kvm_i386.h
> > > @@ -13,6 +13,8 @@
> > >   #include "system/kvm.h"
> > > +#define KVM_MAX_CPUID_ENTRIES  100
> > > +
> > >   /* always false if !CONFIG_KVM */
> > >   #define kvm_pit_in_kernel() \
> > >       (kvm_irqchip_in_kernel() && !kvm_irqchip_is_split())
> > > diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c
> > > index 4ff94860815d..c67be5e618e2 100644
> > > --- a/target/i386/kvm/tdx.c
> > > +++ b/target/i386/kvm/tdx.c
> > > @@ -10,17 +10,122 @@
> > >    */
> > >   #include "qemu/osdep.h"
> > > +#include "qemu/error-report.h"
> > > +#include "qapi/error.h"
> > >   #include "qom/object_interfaces.h"
> > >   #include "hw/i386/x86.h"
> > >   #include "kvm_i386.h"
> > >   #include "tdx.h"
> > > +static struct kvm_tdx_capabilities *tdx_caps;
> > > +
> > > +enum tdx_ioctl_level {
> > > +    TDX_VM_IOCTL,
> > > +    TDX_VCPU_IOCTL,
> > > +};
> > > +
> > > +static int tdx_ioctl_internal(enum tdx_ioctl_level level, void *state,
> > > +                              int cmd_id, __u32 flags, void *data,
> > > +                              Error **errp)
> > > +{
> > > +    struct kvm_tdx_cmd tdx_cmd = {};
> > > +    int r;
> > > +
> > > +    const char *tdx_ioctl_name[] = {
> > > +        [KVM_TDX_CAPABILITIES] = "KVM_TDX_CAPABILITIES",
> > > +        [KVM_TDX_INIT_VM] = "KVM_TDX_INIT_VM",
> > > +        [KVM_TDX_INIT_VCPU] = "KVM_TDX_INIT_VCPU",
> > > +        [KVM_TDX_INIT_MEM_REGION] = "KVM_TDX_INIT_MEM_REGION",
> > > +        [KVM_TDX_FINALIZE_VM] = "KVM_TDX_FINALIZE_VM",
> > > +        [KVM_TDX_GET_CPUID] = "KVM_TDX_GET_CPUID",
> > > +    };
> > > +
> > > +    tdx_cmd.id = cmd_id;
> > > +    tdx_cmd.flags = flags;
> > > +    tdx_cmd.data = (__u64)(unsigned long)data;
> > > +
> > > +    switch (level) {
> > > +    case TDX_VM_IOCTL:
> > > +        r = kvm_vm_ioctl(kvm_state, KVM_MEMORY_ENCRYPT_OP, &tdx_cmd);
> > > +        break;
> > > +    case TDX_VCPU_IOCTL:
> > > +        r = kvm_vcpu_ioctl(state, KVM_MEMORY_ENCRYPT_OP, &tdx_cmd);
> > > +        break;
> > > +    default:
> > > +        error_setg(errp, "Invalid tdx_ioctl_level %d", level);
> > > +        return -EINVAL;
> > > +    }
> > > +
> > > +    if (r < 0) {
> > > +        error_setg_errno(errp, -r, "TDX ioctl %s failed, hw_errors: 
> > > 0x%llx",
> > > +                         tdx_ioctl_name[cmd_id], tdx_cmd.hw_error);
> > > +    }
> > > +    return r;
> > > +}
> > > +
> > > +static inline int tdx_vm_ioctl(int cmd_id, __u32 flags, void *data,
> > > +                               Error **errp)
> > > +{
> > > +    return tdx_ioctl_internal(TDX_VM_IOCTL, NULL, cmd_id, flags, data, 
> > > errp);
> > > +}
> > > +
> > > +static inline int tdx_vcpu_ioctl(CPUState *cpu, int cmd_id, __u32 flags,
> > > +                                 void *data, Error **errp)
> > > +{
> > > +    return  tdx_ioctl_internal(TDX_VCPU_IOCTL, cpu, cmd_id, flags, data, 
> > > errp);
> > > +}
> > > +
> > > +static int get_tdx_capabilities(Error **errp)
> > > +{
> > > +    struct kvm_tdx_capabilities *caps;
> > > +    /* 1st generation of TDX reports 6 cpuid configs */
> > > +    int nr_cpuid_configs = 6;
> > > +    size_t size;
> > > +    int r;
> > > +
> > > +    do {
> > > +        Error *local_err = NULL;
> > > +        size = sizeof(struct kvm_tdx_capabilities) +
> > > +                      nr_cpuid_configs * sizeof(struct kvm_cpuid_entry2);
> > > +        caps = g_malloc0(size);
> > > +        caps->cpuid.nent = nr_cpuid_configs;
> > > +
> > > +        r = tdx_vm_ioctl(KVM_TDX_CAPABILITIES, 0, caps, &local_err);
> > > +        if (r == -E2BIG) {
> > > +            g_free(caps);
> > > +            nr_cpuid_configs *= 2;
> > > +            if (nr_cpuid_configs > KVM_MAX_CPUID_ENTRIES) {
> > > +                error_report("KVM TDX seems broken that number of CPUID 
> > > entries"
> > > +                             " in kvm_tdx_capabilities exceeds limit: 
> > > %d",
> > > +                             KVM_MAX_CPUID_ENTRIES);
> > > +                error_propagate(errp, local_err);
> > > +                return r;
> > > +            }
> > > +            error_free(local_err);
> > 
> > IIRC, you'll need  'local_err = NULL' here, otherwise next time around
> > the loop 'local_err' will be pointing to a free'd error object which
> > 'error_setg' will think is still valid & won't overwrite.
> 
> it's set to NULL at the beginning of the do loop.

Oh yes, i missed that, your code is fine as is then.

> (It looks I need to declare Error *local_err outside the do loop.)

Nah, it is fine within the loop given nothing outside needs it.

> 
> > > +        } else if (r < 0) {
> > > +            g_free(caps);
> > > +            error_propagate(errp, local_err);
> > > +            return r;
> > > +        }
> > > +    } while (r == -E2BIG);
> > > +
> > > +    tdx_caps = caps;
> > > +
> > > +    return 0;
> > > +}
> > > +
> > 
> > 
> > With regards,
> > Daniel
> 

With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|


Reply via email to