qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Daniel P. Berrangé" <berrange@redhat.com>
To: Roy Hopkins <roy.hopkins@suse.com>
Cc: qemu-devel@nongnu.org, "Paolo Bonzini" <pbonzini@redhat.com>,
	"Stefano Garzarella" <sgarzare@redhat.com>,
	"Marcelo Tosatti" <mtosatti@redhat.com>,
	"Michael S . Tsirkin" <mst@redhat.com>,
	"Cornelia Huck" <cohuck@redhat.com>,
	"Marcel Apfelbaum" <marcel.apfelbaum@gmail.com>,
	"Sergio Lopez" <slp@redhat.com>,
	"Eduardo Habkost" <eduardo@habkost.net>,
	"Alistair Francis" <alistair@alistair23.me>,
	"Peter Xu" <peterx@redhat.com>,
	"David Hildenbrand" <david@redhat.com>,
	"Igor Mammedov" <imammedo@redhat.com>,
	"Tom Lendacky" <thomas.lendacky@amd.com>,
	"Michael Roth" <michael.roth@amd.com>,
	"Ani Sinha" <anisinha@redhat.com>,
	"Jörg Roedel" <jroedel@suse.com>
Subject: Re: [PATCH v4 03/17] backends/igvm: Add IGVM loader and configuration
Date: Wed, 24 Jul 2024 17:59:13 +0100	[thread overview]
Message-ID: <ZqEy4Q9hK7kCnva_@redhat.com> (raw)
In-Reply-To: <50a0ccaf47c0106daccfbb7ed8b7dc27ca45f8ff.1720004383.git.roy.hopkins@suse.com>

On Wed, Jul 03, 2024 at 12:05:41PM +0100, Roy Hopkins wrote:
> Adds an IGVM loader to QEMU which processes a given IGVM file and
> applies the directives within the file to the current guest
> configuration.
> 
> The IGVM loader can be used to configure both confidential and
> non-confidential guests. For confidential guests, the
> ConfidentialGuestSupport object for the system is used to encrypt
> memory, apply the initial CPU state and perform other confidential guest
> operations.
> 
> The loader is configured via a new IgvmCfg QOM object which allows the
> user to provide a path to the IGVM file to process.
> 
> Signed-off-by: Roy Hopkins <roy.hopkins@suse.com>
> ---
>  qapi/qom.json             |  17 +
>  backends/igvm.h           |  23 ++
>  include/sysemu/igvm-cfg.h |  54 +++
>  backends/igvm-cfg.c       |  66 ++++
>  backends/igvm.c           | 799 ++++++++++++++++++++++++++++++++++++++
>  backends/meson.build      |   2 +
>  6 files changed, 961 insertions(+)
>  create mode 100644 backends/igvm.h
>  create mode 100644 include/sysemu/igvm-cfg.h
>  create mode 100644 backends/igvm-cfg.c
>  create mode 100644 backends/igvm.c
> 
> diff --git a/qapi/qom.json b/qapi/qom.json
> index 8bd299265e..93b416e697 100644
> --- a/qapi/qom.json
> +++ b/qapi/qom.json
> @@ -874,6 +874,19 @@
>    'base': 'RngProperties',
>    'data': { '*filename': 'str' } }
>  
> +##
> +# @IgvmCfgProperties:
> +#
> +# Properties common to objects that handle IGVM files.
> +#
> +# @file: IGVM file to use to configure guest (default: none)
> +#
> +# Since: 9.1
> +##
> +{ 'struct': 'IgvmCfgProperties',
> +  'if': 'CONFIG_IGVM',
> +  'data': { '*file': 'str' } }

I feel like 'file' should be mandatory rather than optional.

It doesn't make sense to use "-object igvm-cfg" without any
filename, since you can simply omit creating the object
entirely in that case.

> +
>  ##
>  # @SevCommonProperties:
>  #
> @@ -1039,6 +1052,8 @@
>      'filter-redirector',
>      'filter-replay',
>      'filter-rewriter',
> +    { 'name': 'igvm-cfg',
> +      'if': 'CONFIG_IGVM' },
>      'input-barrier',
>      { 'name': 'input-linux',
>        'if': 'CONFIG_LINUX' },
> @@ -1111,6 +1126,8 @@
>        'filter-redirector':          'FilterRedirectorProperties',
>        'filter-replay':              'NetfilterProperties',
>        'filter-rewriter':            'FilterRewriterProperties',
> +      'igvm-cfg':                   { 'type': 'IgvmCfgProperties',
> +                                      'if': 'CONFIG_IGVM' },
>        'input-barrier':              'InputBarrierProperties',
>        'input-linux':                { 'type': 'InputLinuxProperties',
>                                        'if': 'CONFIG_LINUX' },

> diff --git a/include/sysemu/igvm-cfg.h b/include/sysemu/igvm-cfg.h
> new file mode 100644
> index 0000000000..8ac8b33d8d
> --- /dev/null
> +++ b/include/sysemu/igvm-cfg.h
> @@ -0,0 +1,54 @@
> +/*
> + * QEMU IGVM interface
> + *
> + * Copyright (C) 2024 SUSE
> + *
> + * Authors:
> + *  Roy Hopkins <roy.hopkins@suse.com>
> + *
> + * This work is licensed under the terms of the GNU GPL, version 2 or later.
> + * See the COPYING file in the top-level directory.
> + */
> +
> +#ifndef QEMU_IGVM_CFG_H
> +#define QEMU_IGVM_CFG_H
> +
> +#include "qom/object.h"
> +
> +typedef struct IgvmCfgState {
> +    ObjectClass parent_class;
> +
> +    /*
> +     * filename: Filename that specifies a file that contains the configuration
> +     *           of the guest in Independent Guest Virtual Machine (IGVM)
> +     *           format.
> +     */
> +    char *filename;
> +} IgvmCfgState;
> +
> +typedef struct IgvmCfgClass {
> +    ObjectClass parent_class;
> +
> +    /*
> +     * If an IGVM filename has been specified then process the IGVM file.
> +     * Performs a no-op if no filename has been specified.
> +     *
> +     * Returns 0 for ok and -1 on error.
> +     */
> +    int (*process)(IgvmCfgState *cfg, ConfidentialGuestSupport *cgs,
> +                   Error **errp);
> +
> +} IgvmCfgClass;
> +
> +#define TYPE_IGVM_CFG "igvm-cfg"
> +
> +#define IGVM_CFG_CLASS_SUFFIX "-" TYPE_IGVM_CFG
> +#define IGVM_CFG_CLASS_NAME(a) (a IGVM_CFG_CLASS_SUFFIX)
> +
> +#define IGVM_CFG_CLASS(klass) \
> +    OBJECT_CLASS_CHECK(IgvmCfgClass, (klass), TYPE_IGVM_CFG)
> +#define IGVM_CFG(obj) OBJECT_CHECK(IgvmCfgState, (obj), TYPE_IGVM_CFG)
> +#define IGVM_CFG_GET_CLASS(obj) \
> +    OBJECT_GET_CLASS(IgvmCfgClass, (obj), TYPE_IGVM_CFG)

Everything after 'TYPE_IGVM_CFG' can be replaced by OBJECT_DECLARE_TYPE()
I believe.

> +
> +#endif
> diff --git a/backends/igvm-cfg.c b/backends/igvm-cfg.c
> new file mode 100644
> index 0000000000..5e18f3fd5f
> --- /dev/null
> +++ b/backends/igvm-cfg.c
> @@ -0,0 +1,66 @@
> +/*
> + * QEMU IGVM interface
> + *
> + * Copyright (C) 2023-2024 SUSE
> + *
> + * Authors:
> + *  Roy Hopkins <roy.hopkins@suse.com>
> + *
> + * This work is licensed under the terms of the GNU GPL, version 2 or later.
> + * See the COPYING file in the top-level directory.
> + */
> +
> +#include "qemu/osdep.h"
> +
> +#include "sysemu/igvm-cfg.h"
> +#include "igvm.h"
> +#include "qom/object_interfaces.h"
> +
> +static char *get_igvm(Object *obj, Error **errp)
> +{
> +    IgvmCfgState *igvm = IGVM_CFG(obj);
> +    return g_strdup(igvm->filename);
> +}
> +
> +static void set_igvm(Object *obj, const char *value, Error **errp)
> +{
> +    IgvmCfgState *igvm = IGVM_CFG(obj);
> +    g_free(igvm->filename);
> +    igvm->filename = g_strdup(value);
> +}
> +
> +static int igvm_process(IgvmCfgState *cfg, ConfidentialGuestSupport *cgs,
> +                        Error **errp)
> +{
> +    if (!cfg->filename) {
> +        return 0;
> +    }

If filename is made mandatory, then I think this check is
redundant.

> +    return igvm_process_file(cfg, cgs, errp);
> +}
> +
> +static void igvm_cfg_class_init(ObjectClass *oc, void *data)
> +{
> +    IgvmCfgClass *igvmc = IGVM_CFG_CLASS(oc);
> +
> +    object_class_property_add_str(oc, "file", get_igvm, set_igvm);
> +    object_class_property_set_description(oc, "file",
> +                                          "Set the IGVM filename to use");
> +
> +    igvmc->process = igvm_process;
> +}
> +
> +static const TypeInfo igvm_cfg_type = {
> +    .name = TYPE_IGVM_CFG,
> +    .parent = TYPE_OBJECT,
> +    .class_init = igvm_cfg_class_init,
> +    .class_size = sizeof(IgvmCfgClass),
> +    .instance_size = sizeof(IgvmCfgState),
> +    .interfaces = (InterfaceInfo[]){ { TYPE_USER_CREATABLE }, {} }
> +};
> +
> +static void igvm_cfg_type_init(void)
> +{
> +    type_register_static(&igvm_cfg_type);
> +}
>
> +type_init(igvm_cfg_type_init);

The boilerplate type definition can be replaced with the macro
OBJECT_DEFINE_TYPE-WITH_INTERFACES.

> diff --git a/backends/igvm.c b/backends/igvm.c
> new file mode 100644
> index 0000000000..97af1a6cb3
> --- /dev/null
> +++ b/backends/igvm.c
> @@ -0,0 +1,799 @@
> +/*
> + * QEMU IGVM configuration backend for guests
> + *
> + * Copyright (C) 2023-2024 SUSE
> + *
> + * Authors:
> + *  Roy Hopkins <roy.hopkins@suse.com>
> + *
> + * This work is licensed under the terms of the GNU GPL, version 2 or later.
> + * See the COPYING file in the top-level directory.
> + */
> +
> +#include "qemu/osdep.h"
> +
> +#include "igvm.h"
> +#include "qapi/error.h"
> +#include "exec/memory.h"
> +#include "exec/address-spaces.h"
> +#include "hw/core/cpu.h"
> +
> +#include <igvm/igvm.h>
> +#include <igvm/igvm_defs.h>
> +
> +typedef struct IgvmParameterData {
> +    QTAILQ_ENTRY(IgvmParameterData) next;
> +    uint8_t *data;
> +    uint32_t size;
> +    uint32_t index;
> +} IgvmParameterData;
> +
> +/*
> + * QemuIgvm contains the information required during processing
> + * of a single IGVM file.
> + */
> +typedef struct QemuIgvm {
> +    IgvmHandle file;
> +    ConfidentialGuestSupport *cgs;
> +    ConfidentialGuestSupportClass *cgsc;
> +    uint32_t compatibility_mask;
> +    unsigned current_header_index;
> +    QTAILQ_HEAD(, IgvmParameterData) parameter_data;
> +
> +    /* These variables keep track of contiguous page regions */
> +    IGVM_VHS_PAGE_DATA region_prev_page_data;
> +    uint64_t region_start;
> +    unsigned region_start_index;
> +    unsigned region_last_index;
> +    unsigned region_page_count;
> +} QemuIgvm;
> +
> +static int directive_page_data(QemuIgvm *ctx, const uint8_t *header_data,
> +                               Error **errp);
> +static int directive_vp_context(QemuIgvm *ctx, const uint8_t *header_data,
> +                                Error **errp);
> +static int directive_parameter_area(QemuIgvm *ctx, const uint8_t *header_data,
> +                                    Error **errp);
> +static int directive_parameter_insert(QemuIgvm *ctx, const uint8_t *header_data,
> +                                      Error **errp);
> +static int directive_memory_map(QemuIgvm *ctx, const uint8_t *header_data,
> +                                Error **errp);
> +static int directive_vp_count(QemuIgvm *ctx, const uint8_t *header_data,
> +                              Error **errp);
> +static int directive_environment_info(QemuIgvm *ctx, const uint8_t *header_data,
> +                                      Error **errp);
> +static int directive_required_memory(QemuIgvm *ctx, const uint8_t *header_data,
> +                                     Error **errp);

I'd suggest that every single method in this file, even the
static ones, get an 'qigvm_' prefix, and all structs get
an QIGVM name prefix.  This will ensure we avoid any risk
of clashes with code from the igvm C headers, which will
all be using 'igvm' / 'IGVM' prefixes. Will also make it
clearer to the reader, which methods are from this file
vs igvm library.

> +static void *igvm_prepare_memory(QemuIgvm *ctx, uint64_t addr, uint64_t size,
> +                                 int region_identifier, Error **errp)
> +{
> +    ERRP_GUARD();
> +    MemoryRegion *igvm_pages = NULL;
> +    Int128 gpa_region_size;
> +    MemoryRegionSection mrs =
> +        memory_region_find(get_system_memory(), addr, size);
> +    if (mrs.mr) {
> +        if (!memory_region_is_ram(mrs.mr)) {
> +            memory_region_unref(mrs.mr);
> +            error_setg(
> +                errp,
> +                "Processing of IGVM file failed: Could not prepare memory "
> +                "at address 0x%lX due to existing non-RAM region",
> +                addr);
> +            return NULL;
> +        }
> +
> +        gpa_region_size = int128_make64(size);
> +        if (int128_lt(mrs.size, gpa_region_size)) {
> +            memory_region_unref(mrs.mr);
> +            error_setg(
> +                errp,
> +                "Processing of IGVM file failed: Could not prepare memory "
> +                "at address 0x%lX: region size exceeded",
> +                addr);
> +            return NULL;
> +        }
> +        return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
> +    } else {
> +        /*
> +         * The region_identifier is the is the index of the IGVM directive that
> +         * contains the page with the lowest GPA in the region. This will
> +         * generate a unique region name.
> +         */
> +        g_autofree char *region_name =
> +            g_strdup_printf("igvm.%X", region_identifier);
> +        igvm_pages = g_malloc(sizeof(*igvm_pages));

Mild preference for doing   g_new0(MemoryRegion, 1);

> +        if (ctx->cgs && ctx->cgs->require_guest_memfd) {
> +            if (!memory_region_init_ram_guest_memfd(igvm_pages, NULL,
> +                                                    region_name, size, errp)) {
> +                return NULL;
> +            }
> +        } else {
> +            if (!memory_region_init_ram(igvm_pages, NULL, region_name, size,
> +                                        errp)) {
> +                return NULL;
> +            }
> +        }
> +        memory_region_add_subregion(get_system_memory(), addr, igvm_pages);
> +        return memory_region_get_ram_ptr(igvm_pages);
> +    }
> +}
> +


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	other threads:[~2024-07-24 17:01 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-03 11:05 [PATCH v4 00/17] Introduce support for IGVM files Roy Hopkins
2024-07-03 11:05 ` [PATCH v4 01/17] meson: Add optional dependency on IGVM library Roy Hopkins
2024-07-24 16:26   ` Daniel P. Berrangé
2024-07-03 11:05 ` [PATCH v4 02/17] backends/confidential-guest-support: Add functions to support IGVM Roy Hopkins
2024-07-24 16:47   ` Daniel P. Berrangé
2024-07-03 11:05 ` [PATCH v4 03/17] backends/igvm: Add IGVM loader and configuration Roy Hopkins
2024-07-24 16:59   ` Daniel P. Berrangé [this message]
2024-07-29 13:35   ` Stefano Garzarella
2024-07-03 11:05 ` [PATCH v4 04/17] hw/i386: Add igvm-cfg object and processing for IGVM files Roy Hopkins
2024-07-24 17:08   ` Daniel P. Berrangé
2024-07-03 11:05 ` [PATCH v4 05/17] i386/pc_sysfw: Ensure sysfw flash configuration does not conflict with IGVM Roy Hopkins
2024-07-24 17:13   ` Daniel P. Berrangé
2024-08-13 10:42     ` Roy Hopkins
2024-07-03 11:05 ` [PATCH v4 06/17] sev: Fix error handling in sev_encrypt_flash() Roy Hopkins
2024-07-24 17:19   ` Daniel P. Berrangé
2024-07-03 11:05 ` [PATCH v4 07/17] sev: Update launch_update_data functions to use Error handling Roy Hopkins
2024-07-24 17:21   ` Daniel P. Berrangé
2024-07-03 11:05 ` [PATCH v4 08/17] target/i386: Allow setting of R_LDTR and R_TR with cpu_x86_load_seg_cache() Roy Hopkins
2024-07-03 11:05 ` [PATCH v4 09/17] i386/sev: Refactor setting of reset vector and initial CPU state Roy Hopkins
2024-07-03 11:05 ` [PATCH v4 10/17] i386/sev: Implement ConfidentialGuestSupport functions for SEV Roy Hopkins
2024-07-03 11:05 ` [PATCH v4 11/17] docs/system: Add documentation on support for IGVM Roy Hopkins
2024-07-24 17:25   ` Daniel P. Berrangé
2024-07-29 13:41   ` Stefano Garzarella
2024-07-03 11:05 ` [PATCH v4 12/17] docs/interop/firmware.json: Add igvm to FirmwareDevice Roy Hopkins
2024-07-24 17:27   ` Daniel P. Berrangé
2024-07-03 11:05 ` [PATCH v4 13/17] backends/confidential-guest-support: Add set_guest_policy() function Roy Hopkins
2024-07-24 17:30   ` Daniel P. Berrangé
2024-07-03 11:05 ` [PATCH v4 14/17] backends/igvm: Process initialization sections in IGVM file Roy Hopkins
2024-07-03 11:05 ` [PATCH v4 15/17] backends/igvm: Handle policy for SEV guests Roy Hopkins
2024-07-03 11:05 ` [PATCH v4 16/17] i386/sev: Add implementation of CGS set_guest_policy() Roy Hopkins
2024-07-03 11:05 ` [PATCH v4 17/17] sev: Provide sev_features flags from IGVM VMSA to KVM_SEV_INIT2 Roy Hopkins
2024-07-20 18:26 ` [PATCH v4 00/17] Introduce support for IGVM files Michael S. Tsirkin
2024-08-13  9:53   ` Roy Hopkins
2024-08-13 10:21     ` Michael S. Tsirkin
2024-07-24 16:29 ` Daniel P. Berrangé
2024-08-02 15:57   ` Roy Hopkins
2024-08-02 16:03     ` Daniel P. Berrangé

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZqEy4Q9hK7kCnva_@redhat.com \
    --to=berrange@redhat.com \
    --cc=alistair@alistair23.me \
    --cc=anisinha@redhat.com \
    --cc=cohuck@redhat.com \
    --cc=david@redhat.com \
    --cc=eduardo@habkost.net \
    --cc=imammedo@redhat.com \
    --cc=jroedel@suse.com \
    --cc=marcel.apfelbaum@gmail.com \
    --cc=michael.roth@amd.com \
    --cc=mst@redhat.com \
    --cc=mtosatti@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peterx@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=roy.hopkins@suse.com \
    --cc=sgarzare@redhat.com \
    --cc=slp@redhat.com \
    --cc=thomas.lendacky@amd.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).