Skip to contentSkip to conversation

Introduce support for IGVM files

PATCH Superseded Roy Hopkins 4 participants 35 messages Feb 27, 2024 – Mar 28, 2024 Arm/Amlogic Meson SoC support

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

cover Introduce support for IGVM files

Hi everyone,

This initial patch series submission adds the capability to configure confidential guests using files that conform to the Independent Guest Virtual Machine (IGVM) file format. The series is based on the master branch commit 1b330da. Alternatively, the series is available here:
https://github.com/roy-hopkins/qemu/tree/igvm_master_v1

I look forward to welcoming your comments!

Why do we need Independent Guest Virtual Machine (IGVM) files?
==============================================================

IGVM files describe, using a set of directives, the memory layout and initial configuration of a guest that supports isolation technologies such as AMD SEV-SNP and Intel TDX. By encapsulating all of this information in a single configuration file and applying the directives in the order they are specified when the guest is initialized, it becomes straightforward to pre-calculate the cryptographic measurement of the guest initial state, thus aiding in remote attestation processes.

IGVM files can also be used to configure non-standard guest memory layouts, payloads or startup configurations. A good example of this is to use IGVM to deploy and configure an SVSM module in the guest which supports running at multiple VMPLs. The SVSM can be configured to start directly into 32-bit or 64-bit code. This patch series was developed with this purpose in mind to support the COCONUT-SVSM project:
https://github.com/coconut-svsm/svsm

More information and background on the IGVM file format can be found on the project page at:
https://github.com/microsoft/igvm

What this patch series introduces
=================================

This series adds a build-time configuration option (--enable-igvm) to add support for launching a guest using an IGVM file. It extends the current ConfidentialGuestSupport object to allow an IGVM filename to be specified.

The directives in the IGVM file are parsed and the confidential guest is configured through new virtual methods added to the ConfidentialGuestSupport object. These virtual functions have been implemented for AMD SEV and AMD SEV-ES.

Many of the IGVM directives require capabilities that are not supported in SEV and SEV-ES, so support for IGVM directives will need to be considered when support for SEV-SNP, TDX or other technologies is introduced to QEMU. Any directive that is not currently supported results in an error report.

Dependencies
============

In order to enable IGVM support, you will need the IGVM library installed. Instructions on building and installing it can be found here:
https://github.com/microsoft/igvm/tree/main/igvm_c

As mentioned above, this series was developed as part of the effort for COCONUT-SVSM. COCONUT-SVSM requires support for AMD SEV-SNP which is not available in current QEMU. Therefore this series has also been applied on top of the AMD SEV-SNP branch (https://github.com/AMDESE/qemu/tree/snp-v3-wip). You can find that version of the series here:
https://github.com/roy-hopkins/qemu/commits/snp-v3-wip-igvm_v2/

Generating IGVM files
=====================

To try this out you will need to generate an IGVM file that is compatible with the SEV platform you are testing on. I've created a tool that can create a simple IGVM file that packages an OVMF binary for AMD SEV or AMD SEV-ES. The tool is available here:
https://github.com/roy-hopkins/buildigvm

I have tested this on an AMD EPYC Genoa system configured to support SEV. Both SEV and SEV-ES have been tested using IGVM files generated using the buildigvm tool. The SEV-SNP alternative patch set has also been tested using COCONUT-SVSM.

Roy Hopkins (9):
meson: Add optional dependency on IGVM library backends/confidential-guest-support: Add IGVM file parameter backends/confidential-guest-support: Add functions to support IGVM backends/igvm: Implement parsing and processing of IGVM files i386/pc: Process IGVM file during PC initialization if present i386/pc: Skip initialization of system FW when using IGVM i386/sev: Refactor setting of reset vector and initial CPU state i386/sev: Implement ConfidentialGuestSupport functions for SEV docs/system: Add documentation on support for IGVM

 backends/confidential-guest-support.c     |  69 +++
 backends/igvm.c                           | 718 ++++++++++++++++++++++
 backends/meson.build                      |   4 +
 docs/system/igvm.rst                      |  58 ++
 docs/system/index.rst                     |   1 +
 hw/i386/pc.c                              |  12 +-
 hw/i386/pc_piix.c                         |   4 +
 hw/i386/pc_q35.c                          |   4 +
 include/exec/confidential-guest-support.h | 107 ++++
 include/exec/igvm.h                       |  35 ++
 meson.build                               |   8 +
 meson_options.txt                         |   2 +
 qapi/qom.json                             |  13 +
 qemu-options.hx                           |   8 +-
 scripts/meson-buildoptions.sh             |   3 +
 target/i386/sev.c                         | 365 ++++++++++-
 target/i386/sev.h                         | 110 ++++
 17 files changed, 1489 insertions(+), 32 deletions(-)
 create mode 100644 backends/igvm.c
 create mode 100644 docs/system/igvm.rst
 create mode 100644 include/exec/igvm.h

--
2.43.0

#1 lore raw

PATCH 1/9 meson: Add optional dependency on IGVM library

The IGVM library allows Isolated Guest Virtual Machine files to be parsed and processed. IGVM files are used to configure guest memory layout, initial processor state and other configuration pertaining to secure virtual machines.

This adds the --enable-igvm configure option, enabled by default, which attempts to locate and link against the IGVM library via pkgconfig and sets CONFIG_IGVM if found.

The library is added to the system_ss target in backends/meson.build where the IGVM parsing will be performed by the ConfidentialGuestSupport object.

---
 backends/meson.build          | 3 +++
 meson.build                   | 8 ++++++++
 meson_options.txt             | 2 ++
 scripts/meson-buildoptions.sh | 3 +++
 4 files changed, 16 insertions(+)
backends/meson.build +3 -0
index 8b2b111497..d550ac19f7 100644
@@ -30,5 +30,8 @@ if have_vhost_user_crypto
 endif
 system_ss.add(when: gio, if_true: files('dbus-vmstate.c'))
 system_ss.add(when: 'CONFIG_SGX', if_true: files('hostmem-epc.c'))
+if igvm.found()
+  system_ss.add(igvm)
+endif
 
 subdir('tpm')
meson.build +8 -0
index 0ef1654e86..d4949693b0 100644
@@ -1230,6 +1230,12 @@ if host_os == 'linux' and (have_system or have_tools)
                        method: 'pkg-config',
                        required: get_option('libudev'))
 endif
+igvm = not_found
+if not get_option('igvm').auto() or have_system
+  igvm = dependency('igvm',
+                       method: 'pkg-config',
+                       required: get_option('igvm'))
+endif
 
 mpathlibs = [libudev]
 mpathpersist = not_found
@@ -2314,6 +2320,7 @@ config_host_data.set('CONFIG_CFI', get_option('cfi'))
 config_host_data.set('CONFIG_SELINUX', selinux.found())
 config_host_data.set('CONFIG_XEN_BACKEND', xen.found())
 config_host_data.set('CONFIG_LIBDW', libdw.found())
+config_host_data.set('CONFIG_IGVM', igvm.found())
 if xen.found()
   # protect from xen.version() having less than three components
   xen_version = xen.version().split('.') + ['0', '0']
@@ -4433,6 +4440,7 @@ summary_info += {'seccomp support':   seccomp}
 summary_info += {'GlusterFS support': glusterfs}
 summary_info += {'hv-balloon support': hv_balloon}
 summary_info += {'TPM support':       have_tpm}
+summary_info += {'IGVM support':      igvm}
 summary_info += {'libssh support':    libssh}
 summary_info += {'lzo support':       lzo}
 summary_info += {'snappy support':    snappy}
meson_options.txt +2 -0
index 0a99a059ec..4eaba64f4b 100644
@@ -109,6 +109,8 @@ option('dbus_display', type: 'feature', value: 'auto',
        description: '-display dbus support')
 option('tpm', type : 'feature', value : 'auto',
        description: 'TPM support')
+option('igvm', type: 'feature', value: 'auto',
+       description: 'Independent Guest Virtual Machine (IGVM) file support')
 
 # Do not enable it by default even for Mingw32, because it doesn't
 # work on Wine.
scripts/meson-buildoptions.sh +3 -0
index 680fa3f581..38a8183625 100644
@@ -126,6 +126,7 @@ meson_options_help() {
   printf "%s\n" '  hv-balloon      hv-balloon driver (requires Glib 2.68+ GTree API)'
   printf "%s\n" '  hvf             HVF acceleration support'
   printf "%s\n" '  iconv           Font glyph conversion support'
+  printf "%s\n" '  igvm            IGVM file support'
   printf "%s\n" '  jack            JACK sound support'
   printf "%s\n" '  keyring         Linux keyring support'
   printf "%s\n" '  kvm             KVM acceleration support'
@@ -342,6 +343,8 @@ _meson_option_parse() {
     --iasl=*) quote_sh "-Diasl=$2" ;;
     --enable-iconv) printf "%s" -Diconv=enabled ;;
     --disable-iconv) printf "%s" -Diconv=disabled ;;
+    --enable-igvm) printf "%s" -Digvm=enabled ;;
+    --disable-igvm) printf "%s" -Digvm=disabled ;;
     --includedir=*) quote_sh "-Dincludedir=$2" ;;
     --enable-install-blobs) printf "%s" -Dinstall_blobs=true ;;
     --disable-install-blobs) printf "%s" -Dinstall_blobs=false ;;
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/9 backends/confidential-guest-support: Add IGVM file parameter

In reply to Roy Hopkins

In order to add support for parsing IGVM files for secure virtual machines, a the path to an IGVM file needs to be specified as part of the guest configuration. It makes sense to add this to the ConfidentialGuestSupport object as this is common to all secure virtual machines that potentially could support IGVM based configuration.

This patch allows the filename to be configured via the QEMU object model in preparation for subsequent patches that will read and parse the IGVM file.

---
 backends/confidential-guest-support.c     | 21 +++++++++++++++++++++
 include/exec/confidential-guest-support.h |  9 +++++++++
 qapi/qom.json                             | 13 +++++++++++++
 qemu-options.hx                           |  8 +++++++-
 4 files changed, 50 insertions(+), 1 deletion(-)
backends/confidential-guest-support.c +21 -0
index 052fde8db0..da436fb736 100644
@@ -20,8 +20,29 @@ OBJECT_DEFINE_ABSTRACT_TYPE(ConfidentialGuestSupport,
                             CONFIDENTIAL_GUEST_SUPPORT,
                             OBJECT)
 
+#if defined(CONFIG_IGVM)
+static char *get_igvm(Object *obj, Error **errp)
+{
+    ConfidentialGuestSupport *cgs = CONFIDENTIAL_GUEST_SUPPORT(obj);
+    return g_strdup(cgs->igvm_filename);
+}
+
+static void set_igvm(Object *obj, const char *value, Error **errp)
+{
+    ConfidentialGuestSupport *cgs = CONFIDENTIAL_GUEST_SUPPORT(obj);
+    g_free(cgs->igvm_filename);
+    cgs->igvm_filename = g_strdup(value);
+}
+#endif
+
 static void confidential_guest_support_class_init(ObjectClass *oc, void *data)
 {
+#if defined(CONFIG_IGVM)
+    object_class_property_add_str(oc, "igvm-file",
+        get_igvm, set_igvm);
+    object_class_property_set_description(oc, "igvm-file",
+        "Set the IGVM filename to use");
+#endif
 }
 
 static void confidential_guest_support_init(Object *obj)
include/exec/confidential-guest-support.h +9 -0
index ba2dd4b5df..b08ad8de4d 100644
@@ -51,6 +51,15 @@ struct ConfidentialGuestSupport {
      * so 'ready' is not set, we'll abort.
      */
     bool ready;
+
+#if defined(CONFIG_IGVM)
+    /*
+     * igvm_filename: Optional filename that specifies a file that contains
+     *                the configuration of the guest in Isolated Guest
+     *                Virtual Machine (IGVM) format.
+     */
+    char *igvm_filename;
+#endif
 };
 
 typedef struct ConfidentialGuestSupportClass {
qapi/qom.json +13 -0
index 2a6e49365a..570bdd7d55 100644
@@ -859,6 +859,18 @@
   'base': 'RngProperties',
   'data': { '*filename': 'str' } }
 
+##
+# @ConfidentialGuestProperties:
+#
+# Properties common to objects that are derivatives of confidential-guest-support.
+#
+# @igvm-file: IGVM file to use to configure guest (default: none)
+#
+# Since: 8.2
+##
+{ 'struct': 'ConfidentialGuestProperties',
+  'data': { '*igvm-file': 'str' } }
+
 ##
 # @SevGuestProperties:
 #
@@ -886,6 +898,7 @@
 # Since: 2.12
 ##
 { 'struct': 'SevGuestProperties',
+  'base': 'ConfidentialGuestProperties',
   'data': { '*sev-device': 'str',
             '*dh-cert-file': 'str',
             '*session-file': 'str',
qemu-options.hx +7 -1
index 9be1e5817c..49d9226e35 100644
@@ -5640,7 +5640,7 @@ SRST
                  -object secret,id=sec0,keyid=secmaster0,format=base64,\\
                      data=$SECRET,iv=$(<iv.b64)
 
-    ``-object sev-guest,id=id,cbitpos=cbitpos,reduced-phys-bits=val,[sev-device=string,policy=policy,handle=handle,dh-cert-file=file,session-file=file,kernel-hashes=on|off]``
+    ``-object sev-guest,id=id,cbitpos=cbitpos,reduced-phys-bits=val,[sev-device=string,policy=policy,handle=handle,dh-cert-file=file,session-file=file,kernel-hashes=on|off,igvm-file=file]``
         Create a Secure Encrypted Virtualization (SEV) guest object,
         which can be used to provide the guest memory encryption support
         on AMD processors.
@@ -5684,6 +5684,12 @@ SRST
         cmdline to a designated guest firmware page for measured Linux
         boot with -kernel. The default is off. (Since 6.2)
 
+        The ``igvm-file`` is an optional parameter that, when specified,
+        allows an Independent Guest Virtual Machine (IGVM) file to be
+        specified that configures the secure virtual machine and can
+        include, for example, an SVSM module, system firmware, initial
+        boot state, etc.
+
         e.g to launch a SEV guest
 
         .. parsed-literal::
Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH 2/9] backends/confidential-guest-support: Add IGVM file parameter

Quoted 79 lines · Roy Hopkins

In order to add support for parsing IGVM files for secure virtual machines, a the path to an IGVM file needs to be specified as part of the guest configuration. It makes sense to add this to the ConfidentialGuestSupport object as this is common to all secure virtual machines that potentially could support IGVM based configuration.

This patch allows the filename to be configured via the QEMU object model in preparation for subsequent patches that will read and parse the IGVM file.

---

backends/confidential-guest-support.c     | 21 +++++++++++++++++++++
include/exec/confidential-guest-support.h |  9 +++++++++
qapi/qom.json                             | 13 +++++++++++++
qemu-options.hx                           |  8 +++++++-
4 files changed, 50 insertions(+), 1 deletion(-)
backends/confidential-guest-support.c +21 -0
index 052fde8db0..da436fb736 100644
@@ -20,8 +20,29 @@ OBJECT_DEFINE_ABSTRACT_TYPE(ConfidentialGuestSupport,
                            CONFIDENTIAL_GUEST_SUPPORT,
                            OBJECT)
 
+#if defined(CONFIG_IGVM)
+static char *get_igvm(Object *obj, Error **errp)
+{
+    ConfidentialGuestSupport *cgs = CONFIDENTIAL_GUEST_SUPPORT(obj);
+    return g_strdup(cgs->igvm_filename);
+}
+
+static void set_igvm(Object *obj, const char *value, Error **errp)
+{
+    ConfidentialGuestSupport *cgs = CONFIDENTIAL_GUEST_SUPPORT(obj);
+    g_free(cgs->igvm_filename);
+    cgs->igvm_filename = g_strdup(value);
+}
+#endif
+
 static void confidential_guest_support_class_init(ObjectClass *oc, void *data)
 {
+#if defined(CONFIG_IGVM)
+    object_class_property_add_str(oc, "igvm-file",
+        get_igvm, set_igvm);
+    object_class_property_set_description(oc, "igvm-file",
+        "Set the IGVM filename to use");
+#endif
 }
 
 static void confidential_guest_support_init(Object *obj)
include/exec/confidential-guest-support.h +9 -0
index ba2dd4b5df..b08ad8de4d 100644
@@ -51,6 +51,15 @@ struct ConfidentialGuestSupport {
     * so 'ready' is not set, we'll abort.
     */
    bool ready;
+
+#if defined(CONFIG_IGVM)
+    /*
+     * igvm_filename: Optional filename that specifies a file that contains
+     *                the configuration of the guest in Isolated Guest
+     *                Virtual Machine (IGVM) format.
+     */
+    char *igvm_filename;
+#endif
 };
 
 typedef struct ConfidentialGuestSupportClass {
qapi/qom.json +0 -0
index 2a6e49365a..570bdd7d55 100644
@@ -859,6 +859,18 @@
  'base': 'RngProperties',

qapi/qom.json line 869

Quoted 10 lines
qapi/qom.json +8 -0
  'data': { '*filename': 'str' } }
 
+##
+# @ConfidentialGuestProperties:
+#
+# Properties common to objects that are derivatives of confidential-guest-support.
+#
+# @igvm-file: IGVM file to use to configure guest (default: none)
+#
+# Since: 8.2

Should it be 9.0 or maybe 9.1 ?

Quoted 45 lines
qapi/qom.json +5 -0
+##
+{ 'struct': 'ConfidentialGuestProperties',
+  'data': { '*igvm-file': 'str' } }
+
 ##
 # @SevGuestProperties:
 #
@@ -886,6 +898,7 @@
 # Since: 2.12
 ##
 { 'struct': 'SevGuestProperties',
+  'base': 'ConfidentialGuestProperties',
  'data': { '*sev-device': 'str',
            '*dh-cert-file': 'str',
            '*session-file': 'str',
qemu-options.hx +7 -1
index 9be1e5817c..49d9226e35 100644
@@ -5640,7 +5640,7 @@ SRST
                 -object secret,id=sec0,keyid=secmaster0,format=base64,\\
                     data=$SECRET,iv=$(<iv.b64)
 
-    ``-object sev-guest,id=id,cbitpos=cbitpos,reduced-phys-bits=val,[sev-device=string,policy=policy,handle=handle,dh-cert-file=file,session-file=file,kernel-hashes=on|off]``
+    ``-object sev-guest,id=id,cbitpos=cbitpos,reduced-phys-bits=val,[sev-device=string,policy=policy,handle=handle,dh-cert-file=file,session-file=file,kernel-hashes=on|off,igvm-file=file]``
        Create a Secure Encrypted Virtualization (SEV) guest object,
        which can be used to provide the guest memory encryption support
        on AMD processors.
@@ -5684,6 +5684,12 @@ SRST
        cmdline to a designated guest firmware page for measured Linux
        boot with -kernel. The default is off. (Since 6.2)
 
+        The ``igvm-file`` is an optional parameter that, when specified,
+        allows an Independent Guest Virtual Machine (IGVM) file to be
+        specified that configures the secure virtual machine and can
+        include, for example, an SVSM module, system firmware, initial
+        boot state, etc.
+
        e.g to launch a SEV guest
 
        .. parsed-literal::
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH 2/9] backends/confidential-guest-support: Add IGVM file parameter

Quoted 85 lines · Stefano Garzarella
Quoted 84 lines · Roy Hopkins

In order to add support for parsing IGVM files for secure virtual machines, a the path to an IGVM file needs to be specified as part of the guest configuration. It makes sense to add this to the ConfidentialGuestSupport object as this is common to all secure virtual machines that potentially could support IGVM based configuration.

This patch allows the filename to be configured via the QEMU object model in preparation for subsequent patches that will read and parse the IGVM file.

---
backends/confidential-guest-support.c     | 21 +++++++++++++++++++++ include/exec/confidential-guest-support.h |  9 +++++++++ qapi/qom.json                             | 13 +++++++++++++ qemu-options.hx                           |  8 +++++++-

4 files changed, 50 insertions(+), 1 deletion(-)
backends/confidential- renamed from backends/confidential-guest-support.c +0 -0

guest-support.c
index 052fde8db0..da436fb736 100644

backends/confidential-guest-support.c +15 -0
@@ -20,8 +20,29 @@ OBJECT_DEFINE_ABSTRACT_TYPE(ConfidentialGuestSupport,
                             CONFIDENTIAL_GUEST_SUPPORT,
                             OBJECT)
 
+#if defined(CONFIG_IGVM)
+static char *get_igvm(Object *obj, Error **errp)
+{
+    ConfidentialGuestSupport *cgs = CONFIDENTIAL_GUEST_SUPPORT(obj);
+    return g_strdup(cgs->igvm_filename);
+}
+
+static void set_igvm(Object *obj, const char *value, Error **errp)
+{
+    ConfidentialGuestSupport *cgs = CONFIDENTIAL_GUEST_SUPPORT(obj);
+    g_free(cgs->igvm_filename);
+    cgs->igvm_filename = g_strdup(value);
+}
+#endif
+

static void confidential_guest_support_class_init(ObjectClass *oc, void *data)
{

backends/confidential-guest-support.c +6 -0
+#if defined(CONFIG_IGVM)
+    object_class_property_add_str(oc, "igvm-file",
+        get_igvm, set_igvm);
+    object_class_property_set_description(oc, "igvm-file",
+        "Set the IGVM filename to use");
+#endif

}

static void confidential_guest_support_init(Object *obj)

include/exec/confidential-guest-support.h +9 -0
index ba2dd4b5df..b08ad8de4d 100644
@@ -51,6 +51,15 @@ struct ConfidentialGuestSupport {
      * so 'ready' is not set, we'll abort.
      */
     bool ready;
+
+#if defined(CONFIG_IGVM)
+    /*
+     * igvm_filename: Optional filename that specifies a file that contains
+     *                the configuration of the guest in Isolated Guest
+     *                Virtual Machine (IGVM) format.
+     */
+    char *igvm_filename;
+#endif
 };
 
 typedef struct ConfidentialGuestSupportClass {
qapi/qom.json +0 -0
index 2a6e49365a..570bdd7d55 100644
@@ -859,6 +859,18 @@
   'base': 'RngProperties',
   'data': { '*filename': 'str' } }
 
Quoted 11 lines

qapi/qom.json line 869

Quoted 9 lines
qapi/qom.json +4 -0
+##
+# @ConfidentialGuestProperties:
+#
+# Properties common to objects that are derivatives of confidential-guest-

support.

qapi/qom.json +4 -0
+#
+# @igvm-file: IGVM file to use to configure guest (default: none)
+#
+# Since: 8.2

Should it be 9.0 or maybe 9.1 ?

Good question. Obviously it is hard to predict which version this will potentially land in. I can update it to 9.1 because it is unlikely to be in any version prior to this, but what is the normal convention for choosing a version number here?

Quoted 51 lines
Quoted 50 lines
qapi/qom.json +4 -0
+##
+{ 'struct': 'ConfidentialGuestProperties',
+  'data': { '*igvm-file': 'str' } }
+

##
# @SevGuestProperties:
#

qapi/qom.json +1 -0
@@ -886,6 +898,7 @@
 # Since: 2.12
 ##
 { 'struct': 'SevGuestProperties',
+  'base': 'ConfidentialGuestProperties',
   'data': { '*sev-device': 'str',
             '*dh-cert-file': 'str',
             '*session-file': 'str',
qemu-options.hx +0 -1
index 9be1e5817c..49d9226e35 100644
@@ -5640,7 +5640,7 @@ SRST
                  -object secret,id=sec0,keyid=secmaster0,format=base64,\\
                      data=$SECRET,iv=$(<iv.b64)
 
-    ``-object sev-guest,id=id,cbitpos=cbitpos,reduced-phys-bits=val,[sev-

device=string,policy=policy,handle=handle,dh-cert-file=file,session- file=file,kernel-hashes=on|off]``
+    ``-object sev-guest,id=id,cbitpos=cbitpos,reduced-phys-bits=val,[sev- device=string,policy=policy,handle=handle,dh-cert-file=file,session- file=file,kernel-hashes=on|off,igvm-file=file]``
        Create a Secure Encrypted Virtualization (SEV) guest object, which can be used to provide the guest memory encryption support on AMD processors.

qemu-options.hx +0 -0
@@ -5684,6 +5684,12 @@ SRST

        cmdline to a designated guest firmware page for measured Linux boot with -kernel. The default is off. (Since 6.2)

+        The ``igvm-file`` is an optional parameter that, when specified, +        allows an Independent Guest Virtual Machine (IGVM) file to be +        specified that configures the secure virtual machine and can +        include, for example, an SVSM module, system firmware, initial +        boot state, etc.
+
        e.g to launch a SEV guest

        .. parsed-literal::

Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH 2/9] backends/confidential-guest-support: Add IGVM file parameter

Quoted 98 lines · Roy Hopkins
Quoted 85 lines · Stefano Garzarella
Quoted 84 lines · Roy Hopkins

In order to add support for parsing IGVM files for secure virtual machines, a the path to an IGVM file needs to be specified as part of the guest configuration. It makes sense to add this to the ConfidentialGuestSupport object as this is common to all secure virtual machines that potentially could support IGVM based configuration.

This patch allows the filename to be configured via the QEMU object model in preparation for subsequent patches that will read and parse the IGVM file.

---
backends/confidential-guest-support.c     | 21 +++++++++++++++++++++ include/exec/confidential-guest-support.h |  9 +++++++++ qapi/qom.json                             | 13 +++++++++++++ qemu-options.hx                           |  8 +++++++-

4 files changed, 50 insertions(+), 1 deletion(-)
backends/confidential- renamed from backends/confidential-guest-support.c +0 -0

guest-support.c
index 052fde8db0..da436fb736 100644

backends/confidential-guest-support.c +15 -0
@@ -20,8 +20,29 @@ OBJECT_DEFINE_ABSTRACT_TYPE(ConfidentialGuestSupport,
                             CONFIDENTIAL_GUEST_SUPPORT,
                             OBJECT)
 
+#if defined(CONFIG_IGVM)
+static char *get_igvm(Object *obj, Error **errp)
+{
+    ConfidentialGuestSupport *cgs = CONFIDENTIAL_GUEST_SUPPORT(obj);
+    return g_strdup(cgs->igvm_filename);
+}
+
+static void set_igvm(Object *obj, const char *value, Error **errp)
+{
+    ConfidentialGuestSupport *cgs = CONFIDENTIAL_GUEST_SUPPORT(obj);
+    g_free(cgs->igvm_filename);
+    cgs->igvm_filename = g_strdup(value);
+}
+#endif
+

static void confidential_guest_support_class_init(ObjectClass *oc, void *data)
{

backends/confidential-guest-support.c +6 -0
+#if defined(CONFIG_IGVM)
+    object_class_property_add_str(oc, "igvm-file",
+        get_igvm, set_igvm);
+    object_class_property_set_description(oc, "igvm-file",
+        "Set the IGVM filename to use");
+#endif

}

static void confidential_guest_support_init(Object *obj)

include/exec/confidential-guest-support.h +9 -0
index ba2dd4b5df..b08ad8de4d 100644
@@ -51,6 +51,15 @@ struct ConfidentialGuestSupport {
      * so 'ready' is not set, we'll abort.
      */
     bool ready;
+
+#if defined(CONFIG_IGVM)
+    /*
+     * igvm_filename: Optional filename that specifies a file that contains
+     *                the configuration of the guest in Isolated Guest
+     *                Virtual Machine (IGVM) format.
+     */
+    char *igvm_filename;
+#endif
 };
 
 typedef struct ConfidentialGuestSupportClass {
qapi/qom.json +0 -0
index 2a6e49365a..570bdd7d55 100644
@@ -859,6 +859,18 @@
   'base': 'RngProperties',
   'data': { '*filename': 'str' } }
 
Quoted 11 lines

qapi/qom.json line 869

Quoted 9 lines
qapi/qom.json +4 -0
+##
+# @ConfidentialGuestProperties:
+#
+# Properties common to objects that are derivatives of confidential-guest-

support.

qapi/qom.json +4 -0
+#
+# @igvm-file: IGVM file to use to configure guest (default: none)
+#
+# Since: 8.2

Should it be 9.0 or maybe 9.1 ?

Quoted 4 lines

Good question. Obviously it is hard to predict which version this will potentially land in. I can update it to 9.1 because it is unlikely to be in any version prior to this, but what is the normal convention for choosing a version number here?

Pick the version number that is soonest release accepting merge of features.

IOW, most of the time you can pick the version number in current QEMU git master HEAD. During release freeze (ie right now), you have to pick the next version number, since features aren't accepted during freeze.

So choosing '9.1' right now is correct.

With regards,
Daniel

Signature · 3 lines
|: 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 :|
#6 lore raw

PATCH 3/9 backends/confidential-guest-support: Add functions to support IGVM

In reply to Roy Hopkins

In preparation for supporting the processing of IGVM files to configure guests, this adds a set of functions to ConfidentialGuestSupport allowing configuration of secure virtual machines that can be implemented for each supported isolation platform type such as Intel TDX or AMD SEV-SNP. These functions will be called by IGVM processing code in subsequent patches.

This commit provides a default implementation of the functions that either perform no action or generate a warning or error when they are called. Targets that support ConfidentalGuestSupport should override these implementations.

---
 backends/confidential-guest-support.c     | 26 ++++++++
 include/exec/confidential-guest-support.h | 76 +++++++++++++++++++++++
 2 files changed, 102 insertions(+)
backends/confidential-guest-support.c +26 -0
index da436fb736..42628be8d7 100644
@@ -14,6 +14,7 @@
 #include "qemu/osdep.h"
 
 #include "exec/confidential-guest-support.h"
+#include "qemu/error-report.h"
 
 OBJECT_DEFINE_ABSTRACT_TYPE(ConfidentialGuestSupport,
                             confidential_guest_support,
@@ -45,8 +46,33 @@ static void confidential_guest_support_class_init(ObjectClass *oc, void *data)
 #endif
 }
 
+static int check_support(ConfidentialGuestPlatformType platform,
+                         uint16_t platform_version, uint8_t highest_vtl,
+                         uint64_t shared_gpa_boundary)
+{
+    /* Default: no support. */
+    return 0;
+}
+
+static int set_guest_state(hwaddr gpa, uint8_t *ptr, uint64_t len,
+                                 ConfidentialGuestPageType memory_type,
+                                 uint16_t cpu_index)
+{
+    warn_report("Confidential guest memory not supported");
+    return -1;
+}
+
+static int get_mem_map_entry(int index, ConfidentialGuestMemoryMapEntry *entry)
+{
+    return 1;
+}
+
 static void confidential_guest_support_init(Object *obj)
 {
+    ConfidentialGuestSupport *cgs = CONFIDENTIAL_GUEST_SUPPORT(obj);
+    cgs->check_support = check_support;
+    cgs->set_guest_state = set_guest_state;
+    cgs->get_mem_map_entry = get_mem_map_entry;
 }
 
 static void confidential_guest_support_finalize(Object *obj)
include/exec/confidential-guest-support.h +76 -0

92 lines not on this page · raw message

Signature · 1 line
2.43.0
#7 lore raw

Re: [PATCH 3/9] backends/confidential-guest-support: Add functions to support IGVM

Quoted 44 lines · Roy Hopkins

In preparation for supporting the processing of IGVM files to configure guests, this adds a set of functions to ConfidentialGuestSupport allowing configuration of secure virtual machines that can be implemented for each supported isolation platform type such as Intel TDX or AMD SEV-SNP. These functions will be called by IGVM processing code in subsequent patches.

This commit provides a default implementation of the functions that either perform no action or generate a warning or error when they are called. Targets that support ConfidentalGuestSupport should override these implementations.

---
 backends/confidential-guest-support.c     | 26 ++++++++
 include/exec/confidential-guest-support.h | 76 +++++++++++++++++++++++
 2 files changed, 102 insertions(+)
backends/confidential-guest-support.c +11 -0
index da436fb736..42628be8d7 100644
@@ -14,6 +14,7 @@
 #include "qemu/osdep.h"
 
 #include "exec/confidential-guest-support.h"
+#include "qemu/error-report.h"
 
 OBJECT_DEFINE_ABSTRACT_TYPE(ConfidentialGuestSupport,
                             confidential_guest_support,
@@ -45,8 +46,33 @@ static void confidential_guest_support_class_init(ObjectClass *oc, void *data)
 #endif
 }
 
+static int check_support(ConfidentialGuestPlatformType platform,
+                         uint16_t platform_version, uint8_t highest_vtl,
+                         uint64_t shared_gpa_boundary)
+{
+    /* Default: no support. */
+    return 0;
+}
+
+static int set_guest_state(hwaddr gpa, uint8_t *ptr, uint64_t len,
+                                 ConfidentialGuestPageType memory_type,

backends/confidential-guest-support.c static void confidential_guest_support_class_init(ObjectClass *oc, void *data) · line 68

Quoted 10 lines
backends/confidential-guest-support.c +10 -0
+                                 uint16_t cpu_index)
+{
+    warn_report("Confidential guest memory not supported");
+    return -1;
+}
+
+static int get_mem_map_entry(int index, ConfidentialGuestMemoryMapEntry *entry)
+{
+    return 1;
+}

IIUC, all these can reports errors, and as such I think they should have an 'Error **errp' parameter, so we can report precise errors in these methods, rather than less useful generic errors in the caller.

The above 'warn_report' ought to be an error too, since it is returning an failure code (-1)

Quoted 22 lines
backends/confidential-guest-support.c +5 -0
+
 static void confidential_guest_support_init(Object *obj)
 {
+    ConfidentialGuestSupport *cgs = CONFIDENTIAL_GUEST_SUPPORT(obj);
+    cgs->check_support = check_support;
+    cgs->set_guest_state = set_guest_state;
+    cgs->get_mem_map_entry = get_mem_map_entry;
 }
 
 static void confidential_guest_support_finalize(Object *obj)
include/exec/confidential-guest-support.h +4 -0
index b08ad8de4d..c43a1a32f1 100644
@@ -21,10 +21,46 @@
 #ifndef CONFIG_USER_ONLY
 
 #include "qom/object.h"
+#include "exec/hwaddr.h"
+
+#if defined(CONFIG_IGVM)
+#include "igvm/igvm.h"

include/exec/confidential-guest-support.h line 37

Quoted 10 lines
include/exec/confidential-guest-support.h +6 -0
+#endif
 
 #define TYPE_CONFIDENTIAL_GUEST_SUPPORT "confidential-guest-support"
 OBJECT_DECLARE_SIMPLE_TYPE(ConfidentialGuestSupport, CONFIDENTIAL_GUEST_SUPPORT)
 
+typedef enum ConfidentialGuestPlatformType {
+    CGS_PLATFORM_SEV,
+    CGS_PLATFORM_SEV_ES,
+    CGS_PLATFORM_SEV_SNP,
+    CGS_PLATFORM_TDX,

QEMU only has support for SEV & SEV_ES today. We should leave the others until we actually get an impl of SEV-SNP/TDX in QEMU that supports those platforms.

Quoted 80 lines
include/exec/confidential-guest-support.h +66 -0
+} ConfidentialGuestPlatformType;
+
+typedef enum ConfidentialGuestMemoryType {
+    CGS_MEM_RAM,
+    CGS_MEM_RESERVED,
+    CGS_MEM_ACPI,
+    CGS_MEM_NVS,
+    CGS_MEM_UNUSABLE,
+} ConfidentialGuestMemoryType;
+
+typedef struct ConfidentialGuestMemoryMapEntry {
+    uint64_t gpa;
+    uint64_t size;
+    ConfidentialGuestMemoryType type;
+} ConfidentialGuestMemoryMapEntry;
+
+typedef enum ConfidentialGuestPageType {
+    CGS_PAGE_TYPE_NORMAL,
+    CGS_PAGE_TYPE_VMSA,
+    CGS_PAGE_TYPE_ZERO,
+    CGS_PAGE_TYPE_UNMEASURED,
+    CGS_PAGE_TYPE_SECRETS,
+    CGS_PAGE_TYPE_CPUID,
+    CGS_PAGE_TYPE_REQUIRED_MEMORY,
+} ConfidentialGuestPageType;
+
 struct ConfidentialGuestSupport {
     Object parent;
 
@@ -60,6 +96,46 @@ struct ConfidentialGuestSupport {
      */
     char *igvm_filename;
 #endif
+
+    /*
+     * The following virtual methods need to be implemented by systems that
+     * support confidential guests that can be configured with IGVM and are
+     * used during processing of the IGVM file with process_igvm().
+     */
+
+    /*
+     * Check for to see if this confidential guest supports a particular
+     * platform or configuration
+     */
+    int (*check_support)(ConfidentialGuestPlatformType platform,
+                         uint16_t platform_version, uint8_t highest_vtl,
+                         uint64_t shared_gpa_boundary);
+
+    /*
+     * Configure part of the state of a guest for a particular set of data, page
+     * type and gpa. This can be used for example to pre-populate and measure
+     * guest memory contents, define private ranges or set the initial CPU state
+     * for one or more CPUs.
+     *
+     * If memory_type is CGS_PAGE_TYPE_VMSA then ptr points to the initial CPU
+     * context for a virtual CPU. The format of the data depends on the type of
+     * confidential virtual machine. For example, for SEV-ES ptr will point to a
+     * vmcb_save_area structure that should be copied into guest memory at the
+     * address specified in gpa. The cpu_index parameter contains the index of
+     * the CPU the VMSA applies to.
+     */
+    int (*set_guest_state)(hwaddr gpa, uint8_t *ptr, uint64_t len,
+                                 ConfidentialGuestPageType memory_type,
+                                 uint16_t cpu_index);
+
+    /*
+     * Iterate the system memory map, getting the entry with the given index
+     * that can be populated into guest memory.
+     *
+     * Returns 1 if the index is out of range.
+     */
+    int (*get_mem_map_entry)(int index,
+                              ConfidentialGuestMemoryMapEntry *entry);
 };
 
 typedef struct ConfidentialGuestSupportClass {
Signature · 1 line
2.43.0

With regards,
Daniel

Signature · 3 lines
|: 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 :|
#8 lore raw

Re: [PATCH 3/9] backends/confidential-guest-support: Add functions to support IGVM

Quoted 64 lines · Daniel P. Berrangé
Quoted 54 lines · Roy Hopkins

In preparation for supporting the processing of IGVM files to configure guests, this adds a set of functions to ConfidentialGuestSupport allowing configuration of secure virtual machines that can be implemented for each supported isolation platform type such as Intel TDX or AMD SEV-SNP. These functions will be called by IGVM processing code in subsequent patches.

This commit provides a default implementation of the functions that either perform no action or generate a warning or error when they are called. Targets that support ConfidentalGuestSupport should override these implementations.

---
 backends/confidential-guest-support.c     | 26 ++++++++ include/exec/confidential-guest-support.h | 76 +++++++++++++++++++++++

 2 files changed, 102 insertions(+)
backends/confidential- renamed from backends/confidential-guest-support.c +0 -0

guest-support.c
index da436fb736..42628be8d7 100644

backends/confidential-guest-support.c +0 -0
@@ -14,6 +14,7 @@
  #include "qemu/osdep.h"


 #include "exec/confidential-guest-support.h" +#include "qemu/error-report.h"
OBJECT_DEFINE_ABSTRACT_TYPE(ConfidentialGuestSupport, confidential_guest_support,

backends/confidential-guest-support.c +0 -0
@@ -45,8 +46,33 @@ static void
 confidential_guest_support_class_init(ObjectClass *oc, void *data)
  #endif
  }

backends/confidential-guest-support.c +17 -0
+static int check_support(ConfidentialGuestPlatformType platform,
+                         uint16_t platform_version, uint8_t highest_vtl,
+                         uint64_t shared_gpa_boundary)
+{
+    /* Default: no support. */
+    return 0;
+}
+
+static int set_guest_state(hwaddr gpa, uint8_t *ptr, uint64_t len,
+                                 ConfidentialGuestPageType memory_type,
+                                 uint16_t cpu_index)
+{
+    warn_report("Confidential guest memory not supported");
+    return -1;
+}
+
+static int get_mem_map_entry(int index, ConfidentialGuestMemoryMapEntry

*entry)

backends/confidential-guest-support.c static void confidential_guest_support_class_init(ObjectClass *oc, void *data) · line 68

Quoted 3 lines
backends/confidential-guest-support.c +3 -0
+{
+    return 1;
+}

IIUC, all these can reports errors, and as such I think they should have an 'Error **errp' parameter, so we can report precise errors in these methods, rather than less useful generic errors in the caller.

Quoted 2 lines

The above 'warn_report' ought to be an error too, since it is returning an failure code (-1)

Yes, that makes sense. I've made a comprehensive rework of the error handling in the patch series which addresses this and your suggestions in the other patches regarding error handling. I'll submit these as a V2.

Quoted 31 lines
Quoted 31 lines

+
 static void confidential_guest_support_init(Object *obj) {
+    ConfidentialGuestSupport *cgs = CONFIDENTIAL_GUEST_SUPPORT(obj); +    cgs->check_support = check_support;
+    cgs->set_guest_state = set_guest_state;
+    cgs->get_mem_map_entry = get_mem_map_entry;
 }

 static void confidential_guest_support_finalize(Object *obj)

include/exec/confidential-guest-support.h +0 -0
index b08ad8de4d..c43a1a32f1 100644
@@ -21,10 +21,46 @@
  #ifndef CONFIG_USER_ONLY


 #include "qom/object.h"

include/exec/confidential-guest-support.h +5 -0
+#include "exec/hwaddr.h"
+
+#if defined(CONFIG_IGVM)
+#include "igvm/igvm.h"
+#endif


 #define TYPE_CONFIDENTIAL_GUEST_SUPPORT "confidential-guest-support" OBJECT_DECLARE_SIMPLE_TYPE(ConfidentialGuestSupport, CONFIDENTIAL_GUEST_SUPPORT)

+typedef enum ConfidentialGuestPlatformType {

Quoted 8 lines
Quoted 4 lines

+    CGS_PLATFORM_SEV,
+    CGS_PLATFORM_SEV_ES,
+    CGS_PLATFORM_SEV_SNP,
+    CGS_PLATFORM_TDX,

QEMU only has support for SEV & SEV_ES today. We should leave the others until we actually get an impl of SEV-SNP/TDX in QEMU that supports those platforms.

Ok. I'll remove the currently unsupported platforms.

Quoted 93 lines
Quoted 78 lines
include/exec/confidential-guest-support.h +26 -0
+} ConfidentialGuestPlatformType;
+
+typedef enum ConfidentialGuestMemoryType {
+    CGS_MEM_RAM,
+    CGS_MEM_RESERVED,
+    CGS_MEM_ACPI,
+    CGS_MEM_NVS,
+    CGS_MEM_UNUSABLE,
+} ConfidentialGuestMemoryType;
+
+typedef struct ConfidentialGuestMemoryMapEntry {
+    uint64_t gpa;
+    uint64_t size;
+    ConfidentialGuestMemoryType type;
+} ConfidentialGuestMemoryMapEntry;
+
+typedef enum ConfidentialGuestPageType {
+    CGS_PAGE_TYPE_NORMAL,
+    CGS_PAGE_TYPE_VMSA,
+    CGS_PAGE_TYPE_ZERO,
+    CGS_PAGE_TYPE_UNMEASURED,
+    CGS_PAGE_TYPE_SECRETS,
+    CGS_PAGE_TYPE_CPUID,
+    CGS_PAGE_TYPE_REQUIRED_MEMORY,
+} ConfidentialGuestPageType;
+

 struct ConfidentialGuestSupport {
     Object parent;

include/exec/confidential-guest-support.h +17 -0
@@ -60,6 +96,46 @@ struct ConfidentialGuestSupport {
       */
      char *igvm_filename;
  #endif
+
+    /*
+     * The following virtual methods need to be implemented by systems that
+     * support confidential guests that can be configured with IGVM and are
+     * used during processing of the IGVM file with process_igvm().
+     */
+
+    /*
+     * Check for to see if this confidential guest supports a particular
+     * platform or configuration
+     */
+    int (*check_support)(ConfidentialGuestPlatformType platform,
+                         uint16_t platform_version, uint8_t highest_vtl,
+                         uint64_t shared_gpa_boundary);
+
+    /*
+     * Configure part of the state of a guest for a particular set of data,

page
+     * type and gpa. This can be used for example to pre-populate and measure
+     * guest memory contents, define private ranges or set the initial CPU state
+     * for one or more CPUs.
+     *
+     * If memory_type is CGS_PAGE_TYPE_VMSA then ptr points to the initial CPU
+     * context for a virtual CPU. The format of the data depends on the type of
+     * confidential virtual machine. For example, for SEV-ES ptr will point to a
+     * vmcb_save_area structure that should be copied into guest memory at the
+     * address specified in gpa. The cpu_index parameter contains the index of
+     * the CPU the VMSA applies to.
+     */
+    int (*set_guest_state)(hwaddr gpa, uint8_t *ptr, uint64_t len, +                                 ConfidentialGuestPageType memory_type, +                                 uint16_t cpu_index);
+
+    /*
+     * Iterate the system memory map, getting the entry with the given index
+     * that can be populated into guest memory.

Quoted 12 lines

+     *
+     * Returns 1 if the index is out of range.
+     */
+    int (*get_mem_map_entry)(int index,
+                              ConfidentialGuestMemoryMapEntry *entry); };

 typedef struct ConfidentialGuestSupportClass {

Signature · 1 line
2.43.0

With regards,
Daniel

#9 lore raw

PATCH 4/9 backends/igvm: Implement parsing and processing of IGVM files

In reply to Roy Hopkins

This commit adds an implementation of an IGVM loader which parses the file specified as a pararameter to ConfidentialGuestSupport and provides a function that uses the interface in the same object to configure and populate guest memory based on the contents of the file.

The IGVM file is parsed when a filename is provided but the code to process the IGVM file is not yet hooked into target systems. This will follow in a later commit.

---
 backends/confidential-guest-support.c     |   4 +
 backends/igvm.c                           | 718 ++++++++++++++++++++++
 backends/meson.build                      |   1 +
 include/exec/confidential-guest-support.h |   5 +
 include/exec/igvm.h                       |  35 ++
 5 files changed, 763 insertions(+)
 create mode 100644 backends/igvm.c
 create mode 100644 include/exec/igvm.h
backends/confidential-guest-support.c +4 -0
index 42628be8d7..cb7651a8d0 100644
@@ -15,6 +15,7 @@
 
 #include "exec/confidential-guest-support.h"
 #include "qemu/error-report.h"
+#include "exec/igvm.h"
 
 OBJECT_DEFINE_ABSTRACT_TYPE(ConfidentialGuestSupport,
                             confidential_guest_support,
@@ -33,6 +34,9 @@ static void set_igvm(Object *obj, const char *value, Error **errp)
     ConfidentialGuestSupport *cgs = CONFIDENTIAL_GUEST_SUPPORT(obj);
     g_free(cgs->igvm_filename);
     cgs->igvm_filename = g_strdup(value);
+#if defined(CONFIG_IGVM)
+    igvm_file_init(cgs);
+#endif
 }
 #endif
 
backends/igvm.c new file +718 -0

718 lines not on this page · raw message

backends/meson.build +1 -0
index d550ac19f7..d092850a07 100644
@@ -32,6 +32,7 @@ system_ss.add(when: gio, if_true: files('dbus-vmstate.c'))
 system_ss.add(when: 'CONFIG_SGX', if_true: files('hostmem-epc.c'))
 if igvm.found()
   system_ss.add(igvm)
+  system_ss.add(files('igvm.c'))
 endif
 
 subdir('tpm')
include/exec/confidential-guest-support.h +5 -0
index c43a1a32f1..1a017a8fda 100644
@@ -27,6 +27,10 @@
 #include "igvm/igvm.h"
 #endif
 
+#if defined(CONFIG_IGVM)
+#include "igvm/igvm.h"
+#endif
+
 #define TYPE_CONFIDENTIAL_GUEST_SUPPORT "confidential-guest-support"
 OBJECT_DECLARE_SIMPLE_TYPE(ConfidentialGuestSupport, CONFIDENTIAL_GUEST_SUPPORT)
 
@@ -95,6 +99,7 @@ struct ConfidentialGuestSupport {
      *                Virtual Machine (IGVM) format.
      */
     char *igvm_filename;
+    IgvmHandle igvm;
 #endif
 
     /*
include/exec/igvm.h new file +35 -0
new file mode 100644
index 0000000000..6f40a3239c
@@ -0,0 +1,35 @@
+/*
+ * QEMU IGVM configuration backend for Confidential Guests
+ *
+ * Copyright (C) 2023-2024 SUSE
+ *
+ * Authors:
+ *  Roy Hopkins <[email protected]>
+ *
+ * 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 EXEC_IGVM_H
+#define EXEC_IGVM_H
+
+#include "exec/confidential-guest-support.h"
+
+#if defined(CONFIG_IGVM)
+
+void igvm_file_init(ConfidentialGuestSupport *cgs);
+void igvm_process(ConfidentialGuestSupport *cgs);
+
+#else
+
+static inline void igvm_file_init(ConfidentialGuestSupport *cgs)
+{
+}
+
+static inline void igvm_process(ConfidentialGuestSupport *cgs)
+{
+}
+
+#endif
+
+#endif
Signature · 1 line
2.43.0
#10 lore raw

Re: [PATCH 4/9] backends/igvm: Implement parsing and processing of IGVM files

Quoted 126 lines · Roy Hopkins

This commit adds an implementation of an IGVM loader which parses the file specified as a pararameter to ConfidentialGuestSupport and provides a function that uses the interface in the same object to configure and populate guest memory based on the contents of the file.

The IGVM file is parsed when a filename is provided but the code to process the IGVM file is not yet hooked into target systems. This will follow in a later commit.

---
 backends/confidential-guest-support.c     |   4 +
 backends/igvm.c                           | 718 ++++++++++++++++++++++
 backends/meson.build                      |   1 +
 include/exec/confidential-guest-support.h |   5 +
 include/exec/igvm.h                       |  35 ++
 5 files changed, 763 insertions(+)
 create mode 100644 backends/igvm.c
 create mode 100644 include/exec/igvm.h
backends/confidential-guest-support.c +4 -0
index 42628be8d7..cb7651a8d0 100644
@@ -15,6 +15,7 @@
 
 #include "exec/confidential-guest-support.h"
 #include "qemu/error-report.h"
+#include "exec/igvm.h"
 
 OBJECT_DEFINE_ABSTRACT_TYPE(ConfidentialGuestSupport,
                             confidential_guest_support,
@@ -33,6 +34,9 @@ static void set_igvm(Object *obj, const char *value, Error **errp)
     ConfidentialGuestSupport *cgs = CONFIDENTIAL_GUEST_SUPPORT(obj);
     g_free(cgs->igvm_filename);
     cgs->igvm_filename = g_strdup(value);
+#if defined(CONFIG_IGVM)
+    igvm_file_init(cgs);
+#endif
 }
 #endif
 
backends/igvm.c new file +78 -0
new file mode 100644
index 0000000000..a6261d796f
@@ -0,0 +1,718 @@
+/*
+ * QEMU IGVM configuration backend for Confidential Guests
+ *
+ * Copyright (C) 2023-2024 SUSE
+ *
+ * Authors:
+ *  Roy Hopkins <[email protected]>
+ *
+ * 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"
+
+#if defined(CONFIG_IGVM)
+
+#include "exec/confidential-guest-support.h"
+#include "qemu/queue.h"
+#include "qemu/typedefs.h"
+
+#include "exec/igvm.h"
+#include "qemu/error-report.h"
+#include "hw/boards.h"
+#include "qapi/error.h"
+#include "exec/address-spaces.h"
+
+#include <igvm/igvm.h>
+#include <igvm/igvm_defs.h>
+#include <linux/kvm.h>
+
+typedef struct IgvmParameterData {
+    QTAILQ_ENTRY(IgvmParameterData) next;
+    uint8_t *data;
+    uint32_t size;
+    uint32_t index;
+} IgvmParameterData;
+
+static QTAILQ_HEAD(, IgvmParameterData) parameter_data;
+
+static void directive_page_data(ConfidentialGuestSupport *cgs, int i,
+                                uint32_t compatibility_mask);
+static void directive_vp_context(ConfidentialGuestSupport *cgs, int i,
+                                 uint32_t compatibility_mask);
+static void directive_parameter_area(ConfidentialGuestSupport *cgs, int i,
+                                     uint32_t compatibility_mask);
+static void directive_parameter_insert(ConfidentialGuestSupport *cgs, int i,
+                                       uint32_t compatibility_mask);
+static void directive_memory_map(ConfidentialGuestSupport *cgs, int i,
+                                 uint32_t compatibility_mask);
+static void directive_vp_count(ConfidentialGuestSupport *cgs, int i,
+                               uint32_t compatibility_mask);
+static void directive_environment_info(ConfidentialGuestSupport *cgs, int i,
+                                       uint32_t compatibility_mask);
+static void directive_required_memory(ConfidentialGuestSupport *cgs, int i,
+                                      uint32_t compatibility_mask);
+
+struct IGVMDirectiveHandler {
+    uint32_t type;
+    void (*handler)(ConfidentialGuestSupport *cgs, int i,
+                    uint32_t compatibility_mask);
+};
+
+static struct IGVMDirectiveHandler directive_handlers[] = {
+    { IGVM_VHT_PAGE_DATA, directive_page_data },
+    { IGVM_VHT_VP_CONTEXT, directive_vp_context },
+    { IGVM_VHT_PARAMETER_AREA, directive_parameter_area },
+    { IGVM_VHT_PARAMETER_INSERT, directive_parameter_insert },
+    { IGVM_VHT_MEMORY_MAP, directive_memory_map },
+    { IGVM_VHT_VP_COUNT_PARAMETER, directive_vp_count },
+    { IGVM_VHT_ENVIRONMENT_INFO_PARAMETER, directive_environment_info },
+    { IGVM_VHT_REQUIRED_MEMORY, directive_required_memory },
+};
+
+static void directive(uint32_t type, ConfidentialGuestSupport *cgs, int i,
+                      uint32_t compatibility_mask)
+{
+    size_t handler;
+    for (handler = 0; handler < (sizeof(directive_handlers) /

backends/igvm.c line 88

Quoted 10 lines
backends/igvm.c new file +10 -0
+                                 sizeof(struct IGVMDirectiveHandler));
+         ++handler) {
+        if (directive_handlers[handler].type == type) {
+            directive_handlers[handler].handler(cgs, i, compatibility_mask);
+            return;
+        }
+    }
+    warn_report("IGVM: Unknown directive encountered when processing file: %X",
+                type);
+}

If a directive in a IGVM file has functional effects on the guest behaviour it does not feel right to just ignore it and carry on launching the guest in a likely incorrect state.

IOW, I think this should be a error, not a warning, and this method ought to have an 'Error **errp' parameter.

backends/igvm.c line 97

Quoted 9 lines
backends/igvm.c new file +9 -0
+
+static void igvm_handle_error(int32_t result, const char *msg)
+{
+    if (result < 0) {
+        error_report("Processing of IGVM file failed: %s (code: %d)", msg,
+                     (int)result);
+        exit(EXIT_FAILURE);
+    }
+}

IMHO all the code below should have "Error **errp" parameters to report and return to the caller. The top level CGS code that calls into IVGM can use 'error_report_err'. As such I think this method shouldn't exist, and code shoud directly call error_setg.

Quoted 27 lines
backends/igvm.c new file +27 -0
+
+static void *igvm_prepare_memory(uint64_t addr, uint64_t size,
+                                 int region_identifier)
+{
+    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_report(
+                "Processing of IGVM file failed: Could not prepare memory "
+                "at address 0x%lX due to existing non-RAM region",
+                addr);
+            exit(EXIT_FAILURE);
+        }
+
+        gpa_region_size = int128_make64(size);
+        if (int128_lt(mrs.size, gpa_region_size)) {
+            memory_region_unref(mrs.mr);
+            error_report(
+                "Processing of IGVM file failed: Could not prepare memory "
+                "at address 0x%lX: region size exceeded",
+                addr);
+            exit(EXIT_FAILURE);
+        }

backends/igvm.c line 134

Quoted 10 lines
backends/igvm.c new file +10 -0
+        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.
+         */
+        char region_name[22];
+        snprintf(region_name, sizeof(region_name), "igvm.%X",
+                 region_identifier);

IMO it is preferrable to use

  g_autofree char *region_name = g_strdup_printf("igvm.%X", region_identifier);
Quoted 39 lines
backends/igvm.c new file +39 -0
+        igvm_pages = g_malloc(sizeof(*igvm_pages));
+        memory_region_init_ram(igvm_pages, NULL, region_name, size,
+                               &error_fatal);
+        memory_region_add_subregion(get_system_memory(), addr, igvm_pages);
+        return memory_region_get_ram_ptr(igvm_pages);
+    }
+}
+
+static int igvm_type_to_cgs_type(IgvmPageDataType memory_type, bool unmeasured,
+                                 bool zero)
+{
+    switch (memory_type) {
+    case NORMAL: {
+        if (unmeasured) {
+            return CGS_PAGE_TYPE_UNMEASURED;
+        } else {
+            return zero ? CGS_PAGE_TYPE_ZERO :
+                          CGS_PAGE_TYPE_NORMAL;
+        }
+    }
+    case SECRETS:
+        return CGS_PAGE_TYPE_SECRETS;
+    case CPUID_DATA:
+        return CGS_PAGE_TYPE_CPUID;
+    case CPUID_XF:
+        return CGS_PAGE_TYPE_CPUID;
+    default:
+        return CGS_PAGE_TYPE_UNMEASURED;
+    }
+}
+
+static bool page_attrs_equal(const IGVM_VHS_PAGE_DATA *page_1,
+                             const IGVM_VHS_PAGE_DATA *page_2)
+{
+    return ((*(const uint32_t *)&page_1->flags ==
+             *(const uint32_t *)&page_2->flags) &&
+            (page_1->data_type == page_2->data_type) &&
+            (page_1->compatibility_mask == page_2->compatibility_mask));
+}

backends/igvm.c

Quoted 10 lines
backends/igvm.c new file +6 -0
 
 
 
 
+void igvm_file_init(ConfidentialGuestSupport *cgs)
+{
+    FILE *igvm_file = NULL;
+    uint8_t *igvm_buf = NULL;
+
+    if (cgs->igvm_filename) {

Invert this condition and return immediately, so we don't have the entire method body uneccessarily indented.

Quoted 35 lines
backends/igvm.c new file +35 -0
+        IgvmHandle igvm;
+        unsigned long igvm_length;
+
+        igvm_file = fopen(cgs->igvm_filename, "rb");
+        if (!igvm_file) {
+            error_report("IGVM file not found '%s'", cgs->igvm_filename);
+            goto error_out;
+        }
+
+        fseek(igvm_file, 0, SEEK_END);
+        igvm_length = ftell(igvm_file);
+        fseek(igvm_file, 0, SEEK_SET);
+
+        igvm_buf = g_new(uint8_t, igvm_length);
+        if (!igvm_buf) {
+            error_report(
+                "Could not allocate buffer to read file IGVM file '%s'",
+                cgs->igvm_filename);
+            goto error_out;
+        }
+        if (fread(igvm_buf, 1, igvm_length, igvm_file) != igvm_length) {
+            error_report("Unable to load IGVM file '%s'", cgs->igvm_filename);
+            goto error_out;
+        }
+
+        igvm = igvm_new_from_binary(igvm_buf, igvm_length);
+        if (igvm < 0) {
+            error_report("Parsing IGVM file '%s' failed with  error_code %d",
+                         cgs->igvm_filename, igvm);
+            goto error_out;
+        }
+        fclose(igvm_file);
+        g_free(igvm_buf);
+
+        cgs->igvm = igvm;

backends/igvm.c line 666

Quoted 10 lines
backends/igvm.c new file +10 -0
+    }
+    return;
+
+error_out:
+    free(igvm_buf);
+    if (igvm_file) {
+        fclose(igvm_file);
+    }
+    exit(EXIT_FAILURE);
+}

This can be massively simplified to:

    g_autofree uint8_t *buf = NULL;
    unsigned long len;
    g_autoptr(GError) gerr = NULL;

    if (!cgs->igvm_filename) {
        return 0;
    }

    if (!g_file_get_contents(cgs->igvm_filename, (gchar**)&buf, &len, &gerr)) {
        error_setg(errp, "Unable to load %s: %s", cgs->igvm_filename, gerr->message);
        return -1;
    }
    
    if ((cgs->igvm = igvm_new_from_binary(buf, len)) < 0) {
        error_setg(errp, "Unable to parse IGVM %s: %s", cgs->igvm_filename, cgs->igvm);
	return -1;
    }

    return 0;
Quoted 5 lines
backends/igvm.c new file +5 -0
+
+void igvm_process(ConfidentialGuestSupport *cgs)
+{
+    int32_t result;
+    int i;

backends/igvm.c line 681

Quoted 10 lines
backends/igvm.c new file +10 -0
+    uint32_t compatibility_mask;
+    IgvmParameterData *parameter;
+
+    /*
+     * If this is not a Confidential guest or no IGVM has been provided then
+     * this is a no-op.
+     */
+    if (!cgs || !cgs->igvm) {
+        return;
+    }

The caller should not be invoking this method if cgs is NULL.

Quoted 89 lines
backends/igvm.c new file +37 -0
+
+    QTAILQ_INIT(&parameter_data);
+
+    /*
+     * Check that the IGVM file provides configuration for the current
+     * platform
+     */
+    compatibility_mask = supported_platform_compat_mask(cgs);
+    if (compatibility_mask == 0) {
+        error_report(
+            "IGVM file does not describe a compatible supported platform");
+        exit(EXIT_FAILURE);
+    }
+
+    result = igvm_header_count(cgs->igvm, HEADER_SECTION_DIRECTIVE);
+    igvm_handle_error(result, "Failed to read directive header count");
+    for (i = 0; i < (int)result; ++i) {
+        IgvmVariableHeaderType type =
+            igvm_get_header_type(cgs->igvm, HEADER_SECTION_DIRECTIVE, i);
+        directive(type, cgs, i, compatibility_mask);
+    }
+
+    /*
+     * Contiguous pages of data with compatible flags are grouped together in
+     * order to reduce the number of memory regions we create. Make sure the
+     * last group is processed with this call.
+     */
+    process_mem_page(cgs, i, NULL);
+
+    QTAILQ_FOREACH(parameter, &parameter_data, next)
+    {
+        g_free(parameter->data);
+        parameter->data = NULL;
+    }
+}
+
+#endif
backends/meson.build +1 -0
index d550ac19f7..d092850a07 100644
@@ -32,6 +32,7 @@ system_ss.add(when: gio, if_true: files('dbus-vmstate.c'))
 system_ss.add(when: 'CONFIG_SGX', if_true: files('hostmem-epc.c'))
 if igvm.found()
   system_ss.add(igvm)
+  system_ss.add(files('igvm.c'))
 endif
 
 subdir('tpm')
include/exec/confidential-guest-support.h +5 -0
index c43a1a32f1..1a017a8fda 100644
@@ -27,6 +27,10 @@
 #include "igvm/igvm.h"
 #endif
 
+#if defined(CONFIG_IGVM)
+#include "igvm/igvm.h"
+#endif
+
 #define TYPE_CONFIDENTIAL_GUEST_SUPPORT "confidential-guest-support"
 OBJECT_DECLARE_SIMPLE_TYPE(ConfidentialGuestSupport, CONFIDENTIAL_GUEST_SUPPORT)
 
@@ -95,6 +99,7 @@ struct ConfidentialGuestSupport {
      *                Virtual Machine (IGVM) format.
      */
     char *igvm_filename;
+    IgvmHandle igvm;
 #endif
 
     /*
include/exec/igvm.h new file +11 -0
new file mode 100644
index 0000000000..6f40a3239c
@@ -0,0 +1,35 @@
+/*
+ * QEMU IGVM configuration backend for Confidential Guests
+ *
+ * Copyright (C) 2023-2024 SUSE
+ *
+ * Authors:
+ *  Roy Hopkins <[email protected]>
+ *
+ * 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/exec/igvm.h line 21

Quoted 10 lines
include/exec/igvm.h new file +10 -0
+
+#ifndef EXEC_IGVM_H
+#define EXEC_IGVM_H
+
+#include "exec/confidential-guest-support.h"
+
+#if defined(CONFIG_IGVM)
+
+void igvm_file_init(ConfidentialGuestSupport *cgs);
+void igvm_process(ConfidentialGuestSupport *cgs);

Both of these should gain an "Error *errp" parameter and 'int' return type and leave the error_report + exit to the caller.

Quoted 18 lines
include/exec/igvm.h new file +14 -0
+
+#else
+
+static inline void igvm_file_init(ConfidentialGuestSupport *cgs)
+{
+}
+
+static inline void igvm_process(ConfidentialGuestSupport *cgs)
+{
+}
+
+#endif
+
+#endif
Signature · 1 line
2.43.0

With regards,
Daniel

Signature · 3 lines
|: 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 :|
#11 lore raw

Re: [PATCH 4/9] backends/igvm: Implement parsing and processing of IGVM files

Quoted 149 lines · Daniel P. Berrangé
Quoted 135 lines · Roy Hopkins

This commit adds an implementation of an IGVM loader which parses the file specified as a pararameter to ConfidentialGuestSupport and provides a function that uses the interface in the same object to configure and populate guest memory based on the contents of the file.

The IGVM file is parsed when a filename is provided but the code to process the IGVM file is not yet hooked into target systems. This will follow in a later commit.

---
 backends/confidential-guest-support.c     |   4 + backends/igvm.c                           | 718 ++++++++++++++++++++++ backends/meson.build                      |   1 + include/exec/confidential-guest-support.h |   5 + include/exec/igvm.h                       |  35 ++

 5 files changed, 763 insertions(+)

 create mode 100644 backends/igvm.c
 create mode 100644 include/exec/igvm.h

backends/confidential- renamed from backends/confidential-guest-support.c +0 -0

guest-support.c
index 42628be8d7..cb7651a8d0 100644

backends/confidential-guest-support.c +0 -0
@@ -15,6 +15,7 @@


 #include "exec/confidential-guest-support.h" #include "qemu/error-report.h"
+#include "exec/igvm.h"
OBJECT_DEFINE_ABSTRACT_TYPE(ConfidentialGuestSupport, confidential_guest_support,

backends/confidential-guest-support.c +3 -0
@@ -33,6 +34,9 @@ static void set_igvm(Object *obj, const char *value, Error
 **errp)
      ConfidentialGuestSupport *cgs = CONFIDENTIAL_GUEST_SUPPORT(obj);
      g_free(cgs->igvm_filename);
      cgs->igvm_filename = g_strdup(value);
+#if defined(CONFIG_IGVM)
+    igvm_file_init(cgs);
+#endif
  }
  #endif

backends/igvm.c new file +9 -0
new file mode 100644
index 0000000000..a6261d796f
@@ -0,0 +1,718 @@
+/*
+ * QEMU IGVM configuration backend for Confidential Guests
+ *
+ * Copyright (C) 2023-2024 SUSE
+ *
+ * Authors:
+ *  Roy Hopkins <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or

later.
+ * See the COPYING file in the top-level directory.
+ */

backends/igvm.c new file +35 -0
+
+#include "qemu/osdep.h"
+
+#if defined(CONFIG_IGVM)
+
+#include "exec/confidential-guest-support.h"
+#include "qemu/queue.h"
+#include "qemu/typedefs.h"
+
+#include "exec/igvm.h"
+#include "qemu/error-report.h"
+#include "hw/boards.h"
+#include "qapi/error.h"
+#include "exec/address-spaces.h"
+
+#include <igvm/igvm.h>
+#include <igvm/igvm_defs.h>
+#include <linux/kvm.h>
+
+typedef struct IgvmParameterData {
+    QTAILQ_ENTRY(IgvmParameterData) next;
+    uint8_t *data;
+    uint32_t size;
+    uint32_t index;
+} IgvmParameterData;
+
+static QTAILQ_HEAD(, IgvmParameterData) parameter_data;
+
+static void directive_page_data(ConfidentialGuestSupport *cgs, int i,
+                                uint32_t compatibility_mask);
+static void directive_vp_context(ConfidentialGuestSupport *cgs, int i,
+                                 uint32_t compatibility_mask);
+static void directive_parameter_area(ConfidentialGuestSupport *cgs, int i,
+                                     uint32_t compatibility_mask);
+static void directive_parameter_insert(ConfidentialGuestSupport *cgs, int

i,
+                                       uint32_t compatibility_mask);

backends/igvm.c new file +5 -0
+static void directive_memory_map(ConfidentialGuestSupport *cgs, int i,
+                                 uint32_t compatibility_mask);
+static void directive_vp_count(ConfidentialGuestSupport *cgs, int i,
+                               uint32_t compatibility_mask);
+static void directive_environment_info(ConfidentialGuestSupport *cgs, int

i,
+                                       uint32_t compatibility_mask);

backends/igvm.c new file +29 -0
+static void directive_required_memory(ConfidentialGuestSupport *cgs, int i,
+                                      uint32_t compatibility_mask);
+
+struct IGVMDirectiveHandler {
+    uint32_t type;
+    void (*handler)(ConfidentialGuestSupport *cgs, int i,
+                    uint32_t compatibility_mask);
+};
+
+static struct IGVMDirectiveHandler directive_handlers[] = {
+    { IGVM_VHT_PAGE_DATA, directive_page_data },
+    { IGVM_VHT_VP_CONTEXT, directive_vp_context },
+    { IGVM_VHT_PARAMETER_AREA, directive_parameter_area },
+    { IGVM_VHT_PARAMETER_INSERT, directive_parameter_insert },
+    { IGVM_VHT_MEMORY_MAP, directive_memory_map },
+    { IGVM_VHT_VP_COUNT_PARAMETER, directive_vp_count },
+    { IGVM_VHT_ENVIRONMENT_INFO_PARAMETER, directive_environment_info },
+    { IGVM_VHT_REQUIRED_MEMORY, directive_required_memory },
+};
+
+static void directive(uint32_t type, ConfidentialGuestSupport *cgs, int i,
+                      uint32_t compatibility_mask)
+{
+    size_t handler;
+    for (handler = 0; handler < (sizeof(directive_handlers) /
+                                 sizeof(struct IGVMDirectiveHandler));
+         ++handler) {
+        if (directive_handlers[handler].type == type) {
+            directive_handlers[handler].handler(cgs, i,
Quoted 8 lines

compatibility_mask);
+            return;
+        }
+    }
+    warn_report("IGVM: Unknown directive encountered when processing file:
%X",
+                type);
+}

If a directive in a IGVM file has functional effects on the guest behaviour it does not feel right to just ignore it and carry on launching the guest in a likely incorrect state.

Quoted 2 lines

IOW, I think this should be a error, not a warning, and this method ought to have an 'Error **errp' parameter.

Agreed. I'll update the code to generate errors on any failure or unknown directives/configuration from the IGVM file.

Quoted 16 lines

backends/igvm.c line 97

Quoted 10 lines
backends/igvm.c new file +9 -0
+
+static void igvm_handle_error(int32_t result, const char *msg)
+{
+    if (result < 0) {
+        error_report("Processing of IGVM file failed: %s (code: %d)", msg,
+                     (int)result);
+        exit(EXIT_FAILURE);
+    }
+}

IMHO all the code below should have "Error **errp" parameters to report and return to the caller. The top level CGS code that calls into IVGM can use 'error_report_err'. As such I think this method shouldn't exist, and code shoud directly call error_setg.

Right, V2 will include a rework of the error handling using a combination of return codes and "Error **errp" as recommended in "error.h". This method has been removed as part of that rework.

Quoted 43 lines
Quoted 32 lines
backends/igvm.c new file +28 -0
+
+static void *igvm_prepare_memory(uint64_t addr, uint64_t size,
+                                 int region_identifier)
+{
+    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_report(
+                "Processing of IGVM file failed: Could not prepare memory "
+                "at address 0x%lX due to existing non-RAM region",
+                addr);
+            exit(EXIT_FAILURE);
+        }
+
+        gpa_region_size = int128_make64(size);
+        if (int128_lt(mrs.size, gpa_region_size)) {
+            memory_region_unref(mrs.mr);
+            error_report(
+                "Processing of IGVM file failed: Could not prepare memory "
+                "at address 0x%lX: region size exceeded",
+                addr);
+            exit(EXIT_FAILURE);
+        }
+        return qemu_map_ram_ptr(mrs.mr->ram_block,

mrs.offset_within_region);
+    } else {
+        /*

Quoted 8 lines

+         * 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.
+         */
+        char region_name[22];
+        snprintf(region_name, sizeof(region_name), "igvm.%X", +                 region_identifier);

IMO it is preferrable to use

Quoted 2 lines

  g_autofree char *region_name = g_strdup_printf("igvm.%X", region_identifier);

Ok, I'll make this change.

Quoted 45 lines
Quoted 41 lines

+        igvm_pages = g_malloc(sizeof(*igvm_pages));
+        memory_region_init_ram(igvm_pages, NULL, region_name, size, +                               &error_fatal);
+        memory_region_add_subregion(get_system_memory(), addr, igvm_pages); +        return memory_region_get_ram_ptr(igvm_pages);
+    }

backends/igvm.c new file +3 -0
+}
+
+static int igvm_type_to_cgs_type(IgvmPageDataType memory_type, bool

unmeasured,
+                                 bool zero)

backends/igvm.c new file +29 -0
+{
+    switch (memory_type) {
+    case NORMAL: {
+        if (unmeasured) {
+            return CGS_PAGE_TYPE_UNMEASURED;
+        } else {
+            return zero ? CGS_PAGE_TYPE_ZERO :
+                          CGS_PAGE_TYPE_NORMAL;
+        }
+    }
+    case SECRETS:
+        return CGS_PAGE_TYPE_SECRETS;
+    case CPUID_DATA:
+        return CGS_PAGE_TYPE_CPUID;
+    case CPUID_XF:
+        return CGS_PAGE_TYPE_CPUID;
+    default:
+        return CGS_PAGE_TYPE_UNMEASURED;
+    }
+}
+
+static bool page_attrs_equal(const IGVM_VHS_PAGE_DATA *page_1,
+                             const IGVM_VHS_PAGE_DATA *page_2)
+{
+    return ((*(const uint32_t *)&page_1->flags ==
+             *(const uint32_t *)&page_2->flags) &&
+            (page_1->data_type == page_2->data_type) &&
+            (page_1->compatibility_mask == page_2->compatibility_mask));
+}
Quoted 9 lines

backends/igvm.c line 621

Quoted 6 lines
backends/igvm.c new file +6 -0
+void igvm_file_init(ConfidentialGuestSupport *cgs)
+{
+    FILE *igvm_file = NULL;
+    uint8_t *igvm_buf = NULL;
+
+    if (cgs->igvm_filename) {

Invert this condition and return immediately, so we don't have the entire method body uneccessarily indented.

Ok.

Quoted 68 lines
Quoted 38 lines

+        IgvmHandle igvm;
+        unsigned long igvm_length;

backends/igvm.c new file +20 -0
+
+        igvm_file = fopen(cgs->igvm_filename, "rb");
+        if (!igvm_file) {
+            error_report("IGVM file not found '%s'", cgs->igvm_filename);
+            goto error_out;
+        }
+
+        fseek(igvm_file, 0, SEEK_END);
+        igvm_length = ftell(igvm_file);
+        fseek(igvm_file, 0, SEEK_SET);
+
+        igvm_buf = g_new(uint8_t, igvm_length);
+        if (!igvm_buf) {
+            error_report(
+                "Could not allocate buffer to read file IGVM file '%s'",
+                cgs->igvm_filename);
+            goto error_out;
+        }
+        if (fread(igvm_buf, 1, igvm_length, igvm_file) != igvm_length) {
+            error_report("Unable to load IGVM file '%s'", cgs-
Quoted 1 line

igvm_filename);

+            goto error_out;
+        }
+
+        igvm = igvm_new_from_binary(igvm_buf, igvm_length); +        if (igvm < 0) {
+            error_report("Parsing IGVM file '%s' failed with  error_code %d",
+                         cgs->igvm_filename, igvm);
+            goto error_out;
+        }
+        fclose(igvm_file);
+        g_free(igvm_buf);

backends/igvm.c new file +2 -0
+
+        cgs->igvm = igvm;

backends/igvm.c line 666

Quoted 10 lines
backends/igvm.c new file +10 -0
+    }
+    return;
+
+error_out:
+    free(igvm_buf);
+    if (igvm_file) {
+        fclose(igvm_file);
+    }
+    exit(EXIT_FAILURE);
+}

This can be massively simplified to:

    g_autofree uint8_t *buf = NULL;
    unsigned long len;
    g_autoptr(GError) gerr = NULL;

    if (!cgs->igvm_filename) {
        return 0;
    }

    if (!g_file_get_contents(cgs->igvm_filename, (gchar**)&buf, &len, &gerr)) {
        error_setg(errp, "Unable to load %s: %s", cgs->igvm_filename, gerr-

Quoted 1 line

message);

        return -1;
    }

    if ((cgs->igvm = igvm_new_from_binary(buf, len)) < 0) { error_setg(errp, "Unable to parse IGVM %s: %s", cgs->igvm_filename,

Quoted 7 lines
cgs->igvm);
	return -1;

    }

    return 0;

Much nicer! Thank you.

Quoted 10 lines
Quoted 10 lines
backends/igvm.c new file +10 -0
+
+void igvm_process(ConfidentialGuestSupport *cgs)
+{
+    int32_t result;
+    int i;
+    uint32_t compatibility_mask;
+    IgvmParameterData *parameter;
+
+    /*
+     * If this is not a Confidential guest or no IGVM has been provided
Quoted 8 lines
Quoted 6 lines

then
+     * this is a no-op.
+     */
+    if (!cgs || !cgs->igvm) {
+        return;
+    }

The caller should not be invoking this method if cgs is NULL.

Ok.

Quoted 100 lines
Quoted 100 lines
backends/igvm.c new file +24 -0
+
+    QTAILQ_INIT(&parameter_data);
+
+    /*
+     * Check that the IGVM file provides configuration for the current
+     * platform
+     */
+    compatibility_mask = supported_platform_compat_mask(cgs);
+    if (compatibility_mask == 0) {
+        error_report(
+            "IGVM file does not describe a compatible supported platform");
+        exit(EXIT_FAILURE);
+    }
+
+    result = igvm_header_count(cgs->igvm, HEADER_SECTION_DIRECTIVE);
+    igvm_handle_error(result, "Failed to read directive header count");
+    for (i = 0; i < (int)result; ++i) {
+        IgvmVariableHeaderType type =
+            igvm_get_header_type(cgs->igvm, HEADER_SECTION_DIRECTIVE, i);
+        directive(type, cgs, i, compatibility_mask);
+    }
+
+    /*
+     * Contiguous pages of data with compatible flags are grouped together

in
+     * order to reduce the number of memory regions we create. Make sure the
+     * last group is processed with this call.
+     */
+    process_mem_page(cgs, i, NULL);

backends/igvm.c new file +9 -0
+
+    QTAILQ_FOREACH(parameter, &parameter_data, next)
+    {
+        g_free(parameter->data);
+        parameter->data = NULL;
+    }
+}
+
+#endif
backends/meson.build +1 -0
index d550ac19f7..d092850a07 100644
@@ -32,6 +32,7 @@ system_ss.add(when: gio, if_true: files('dbus-vmstate.c'))
  system_ss.add(when: 'CONFIG_SGX', if_true: files('hostmem-epc.c'))
  if igvm.found()
    system_ss.add(igvm)
+  system_ss.add(files('igvm.c'))

 endif

 subdir('tpm')

include/exec/confidential-guest-support.h +0 -0
index c43a1a32f1..1a017a8fda 100644
@@ -27,6 +27,10 @@
  #include "igvm/igvm.h"
  #endif

include/exec/confidential-guest-support.h +4 -0
+#if defined(CONFIG_IGVM)
+#include "igvm/igvm.h"
+#endif
+

 #define TYPE_CONFIDENTIAL_GUEST_SUPPORT "confidential-guest-support" OBJECT_DECLARE_SIMPLE_TYPE(ConfidentialGuestSupport, CONFIDENTIAL_GUEST_SUPPORT)

include/exec/confidential-guest-support.h +1 -0
@@ -95,6 +99,7 @@ struct ConfidentialGuestSupport {
       *                Virtual Machine (IGVM) format.
       */
      char *igvm_filename;
+    IgvmHandle igvm;
  #endif


     /*

include/exec/igvm.h new file +9 -0
new file mode 100644
index 0000000000..6f40a3239c
@@ -0,0 +1,35 @@
+/*
+ * QEMU IGVM configuration backend for Confidential Guests
+ *
+ * Copyright (C) 2023-2024 SUSE
+ *
+ * Authors:
+ *  Roy Hopkins <[email protected]>
+ *
+ * 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/exec/igvm.h new file +5 -0
+
+#ifndef EXEC_IGVM_H
+#define EXEC_IGVM_H
+
+#include "exec/confidential-guest-support.h"
Quoted 9 lines

include/exec/igvm.h line 21

Quoted 5 lines
include/exec/igvm.h new file +5 -0
+
+#if defined(CONFIG_IGVM)
+
+void igvm_file_init(ConfidentialGuestSupport *cgs);
+void igvm_process(ConfidentialGuestSupport *cgs);

Both of these should gain an "Error *errp" parameter and 'int' return type and leave the error_report + exit to the caller.

This will be addressed as per my first comment above.

Quoted 13 lines
Quoted 13 lines
include/exec/igvm.h new file +13 -0
+
+#else
+
+static inline void igvm_file_init(ConfidentialGuestSupport *cgs)
+{
+}
+
+static inline void igvm_process(ConfidentialGuestSupport *cgs)
+{
+}
+
+#endif
+
Quoted 8 lines
Quoted 5 lines

+#endif

Signature · 1 line
2.43.0

With regards,
Daniel

Daniel - Many thanks for taking the time to look at this.

Regards,
Roy

#12 lore raw

PATCH 5/9 i386/pc: Process IGVM file during PC initialization if present

In reply to Roy Hopkins

An IGVM file contains configuration of a guest that supports confidential computing hardware. As part of the PC system initialisation, the IGVM needs to be processed to apply this configuration before the guest is started.

This patch introduces processing of a provided IGVM file at the end of the current PC initialization steps. If an IGVM file has been provided then the directives in the file are processed completing the initalization of the target.

If no IGVM file has been specified by the user then no there is no intended consequences in these changes.

---
 backends/confidential-guest-support.c     | 18 ++++++++++++++++++
 hw/i386/pc_piix.c                         |  4 ++++
 hw/i386/pc_q35.c                          |  4 ++++
 include/exec/confidential-guest-support.h | 17 +++++++++++++++++
 4 files changed, 43 insertions(+)
backends/confidential-guest-support.c +18 -0
index cb7651a8d0..881eef6b42 100644
@@ -82,3 +82,21 @@ static void confidential_guest_support_init(Object *obj)
 static void confidential_guest_support_finalize(Object *obj)
 {
 }
+
+bool cgs_is_igvm(ConfidentialGuestSupport *cgs)
+{
+#if defined(CONFIG_IGVM)
+    return cgs && cgs->igvm;
+#else
+    return false;
+#endif
+}
+
+void cgs_process_igvm(ConfidentialGuestSupport *cgs)
+{
+#if defined(CONFIG_IGVM)
+    if (cgs && cgs_is_igvm(cgs)) {
+        igvm_process(cgs);
+    }
+#endif
+}
hw/i386/pc_piix.c +4 -0
index ec7c07b362..b449944ee8 100644
@@ -66,6 +66,7 @@
 #include "hw/i386/acpi-build.h"
 #include "kvm/kvm-cpu.h"
 #include "target/i386/cpu.h"
+#include "exec/confidential-guest-support.h"
 
 #define XEN_IOAPIC_NUM_PIRQS 128ULL
 
@@ -370,6 +371,9 @@ static void pc_init1(MachineState *machine,
                                x86_nvdimm_acpi_dsmio,
                                x86ms->fw_cfg, OBJECT(pcms));
     }
+
+    /* Apply confidential guest state from IGVM if supplied */
+    cgs_process_igvm(machine->cgs);
 }
 
 typedef enum PCSouthBridgeOption {
hw/i386/pc_q35.c +4 -0
index 53fb3db26d..ecccf518e2 100644
@@ -58,6 +58,7 @@
 #include "hw/mem/nvdimm.h"
 #include "hw/i386/acpi-build.h"
 #include "target/i386/cpu.h"
+#include "exec/confidential-guest-support.h"
 
 /* ICH9 AHCI has 6 ports */
 #define MAX_SATA_PORTS     6
@@ -324,6 +325,9 @@ static void pc_q35_init(MachineState *machine)
                                x86_nvdimm_acpi_dsmio,
                                x86ms->fw_cfg, OBJECT(pcms));
     }
+
+    /* Apply confidential guest state from IGVM if supplied */
+    cgs_process_igvm(machine->cgs);
 }
 
 #define DEFINE_Q35_MACHINE(suffix, name, compatfn, optionfn) \
include/exec/confidential-guest-support.h +17 -0
index 1a017a8fda..2bc99d84cd 100644
@@ -147,6 +147,23 @@ typedef struct ConfidentialGuestSupportClass {
     ObjectClass parent;
 } ConfidentialGuestSupportClass;
 
+/*
+ * Check whether the configuration of the confidential guest is provided
+ * using an IGVM file. IGVM configuration can include the system firmware,
+ * initial CPU state and other configuration that should override standard
+ * system initialization. This function should be used by platforms to
+ * determine which devices and configuration to include during system
+ * initialization.
+ */
+bool cgs_is_igvm(ConfidentialGuestSupport *cgs);
+/*
+ * If IGVM is supported and an IGVM file has been specified then the
+ * configuration described in the file is applied to the guest.
+ * Configuration of a confidential guest includes the layout of the
+ * guest memory, including firmware and initial CPU state.
+ */
+void cgs_process_igvm(ConfidentialGuestSupport *cgs);
+
 #endif /* !CONFIG_USER_ONLY */
 
 #endif /* QEMU_CONFIDENTIAL_GUEST_SUPPORT_H */
Signature · 1 line
2.43.0
#13 lore raw

PATCH 6/9 i386/pc: Skip initialization of system FW when using IGVM

In reply to Roy Hopkins

When using an IGVM file the configuration of the system firmware is defined by IGVM directives contained in the file. Therefore the default system firmware should not be initialized when an IGVM file has been provided.

This commit checks to see if an IGVM file has been provided and, if it has then the standard system firmware initialization is skipped and any prepared flash devices are cleaned up.

---
 hw/i386/pc.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
hw/i386/pc.c +10 -2
index f8eb684a49..17bb211708 100644
@@ -63,6 +63,7 @@
 #include "e820_memory_layout.h"
 #include "trace.h"
 #include CONFIG_DEVICES
+#include "exec/confidential-guest-support.h"
 
 #ifdef CONFIG_XEN_EMU
 #include "hw/xen/xen-legacy-backend.h"
@@ -1023,8 +1024,15 @@ void pc_memory_init(PCMachineState *pcms,
         }
     }
 
-    /* Initialize PC system firmware */
-    pc_system_firmware_init(pcms, rom_memory);
+    /*
+     * If this is a confidential guest configured using IGVM then the IGVM
+     * configuration will include the system firmware. In this case do not
+     * initialise PC system firmware.
+     */
+    if (!cgs_is_igvm(machine->cgs)) {
+        /* Initialize PC system firmware */
+        pc_system_firmware_init(pcms, rom_memory);
+    }
 
     option_rom_mr = g_malloc(sizeof(*option_rom_mr));
     memory_region_init_ram(option_rom_mr, NULL, "pc.rom", PC_ROM_SIZE,
Signature · 1 line
2.43.0
#14 lore raw

Re: [PATCH 6/9] i386/pc: Skip initialization of system FW when using IGVM

Quoted 31 lines · Roy Hopkins

When using an IGVM file the configuration of the system firmware is defined by IGVM directives contained in the file. Therefore the default system firmware should not be initialized when an IGVM file has been provided.

This commit checks to see if an IGVM file has been provided and, if it has then the standard system firmware initialization is skipped and any prepared flash devices are cleaned up.

---
 hw/i386/pc.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
hw/i386/pc.c +1 -1
index f8eb684a49..17bb211708 100644
@@ -63,6 +63,7 @@
 #include "e820_memory_layout.h"
 #include "trace.h"
 #include CONFIG_DEVICES
+#include "exec/confidential-guest-support.h"
 
 #ifdef CONFIG_XEN_EMU
 #include "hw/xen/xen-legacy-backend.h"
@@ -1023,8 +1024,15 @@ void pc_memory_init(PCMachineState *pcms,
         }
     }
 
-    /* Initialize PC system firmware */

hw/i386/pc.c void pc_memory_init(PCMachineState *pcms, · line 1035

Quoted 10 lines
hw/i386/pc.c +9 -1
-    pc_system_firmware_init(pcms, rom_memory);
+    /*
+     * If this is a confidential guest configured using IGVM then the IGVM
+     * configuration will include the system firmware. In this case do not
+     * initialise PC system firmware.
+     */
+    if (!cgs_is_igvm(machine->cgs)) {
+        /* Initialize PC system firmware */
+        pc_system_firmware_init(pcms, rom_memory);
+    }

If the user has given explicit pflash config I think we should be reporting an error for the invalid configuration, rather than silently ignoring their mistake.

Quoted 7 lines
hw/i386/pc.c +0 -0
     option_rom_mr = g_malloc(sizeof(*option_rom_mr));
     memory_region_init_ram(option_rom_mr, NULL, "pc.rom", PC_ROM_SIZE,
Signature · 1 line
2.43.0

With regards,
Daniel

Signature · 3 lines
|: 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 :|
#15 lore raw

Re: [PATCH 6/9] i386/pc: Skip initialization of system FW when using IGVM

Quoted 38 lines · Daniel P. Berrangé
Quoted 37 lines · Roy Hopkins

When using an IGVM file the configuration of the system firmware is defined by IGVM directives contained in the file. Therefore the default system firmware should not be initialized when an IGVM file has been provided.

This commit checks to see if an IGVM file has been provided and, if it has then the standard system firmware initialization is skipped and any prepared flash devices are cleaned up.

---
 hw/i386/pc.c | 12 ++++++++++--

 1 file changed, 10 insertions(+), 2 deletions(-)
hw/i386/pc.c +1 -0
index f8eb684a49..17bb211708 100644
@@ -63,6 +63,7 @@
  #include "e820_memory_layout.h"
  #include "trace.h"
  #include CONFIG_DEVICES
+#include "exec/confidential-guest-support.h"


 #ifdef CONFIG_XEN_EMU
 #include "hw/xen/xen-legacy-backend.h"

hw/i386/pc.c +0 -0
@@ -1023,8 +1024,15 @@ void pc_memory_init(PCMachineState *pcms,
          }
      }


-    /* Initialize PC system firmware */
-    pc_system_firmware_init(pcms, rom_memory);
+    /*
+     * If this is a confidential guest configured using IGVM then the IGVM +     * configuration will include the system firmware. In this case do not +     * initialise PC system firmware.
+     */

Quoted 9 lines
Quoted 4 lines

+    if (!cgs_is_igvm(machine->cgs)) {
+        /* Initialize PC system firmware */ +        pc_system_firmware_init(pcms, rom_memory); +    }

If the user has given explicit pflash config I think we should be reporting an error for the invalid configuration, rather than silently ignoring their mistake.

Ok. In that case, I'll replace this patch and move the check into pc_system_firmware_init() in "pc_sysfw.c". I can then check for the presence of an IGVM file after the firmware configuration has completed, generating an error if pflash has been configured when an IGVM file is provided.

Quoted 10 lines
Quoted 7 lines


     option_rom_mr = g_malloc(sizeof(*option_rom_mr)); memory_region_init_ram(option_rom_mr, NULL, "pc.rom", PC_ROM_SIZE,

Signature · 1 line
2.43.0

With regards,
Daniel

Many thanks,
Roy

#16 lore raw

Re: [PATCH 6/9] i386/pc: Skip initialization of system FW when using IGVM

In reply to Roy Hopkins
Quoted 35 lines · Roy Hopkins

When using an IGVM file the configuration of the system firmware is defined by IGVM directives contained in the file. Therefore the default system firmware should not be initialized when an IGVM file has been provided.

This commit checks to see if an IGVM file has been provided and, if it has then the standard system firmware initialization is skipped and any prepared flash devices are cleaned up.

---
hw/i386/pc.c | 12 ++++++++++--

1 file changed, 10 insertions(+), 2 deletions(-)
hw/i386/pc.c +1 -1
index f8eb684a49..17bb211708 100644
@@ -63,6 +63,7 @@
 #include "e820_memory_layout.h"
 #include "trace.h"
 #include CONFIG_DEVICES
+#include "exec/confidential-guest-support.h"
 
 #ifdef CONFIG_XEN_EMU
 #include "hw/xen/xen-legacy-backend.h"
@@ -1023,8 +1024,15 @@ void pc_memory_init(PCMachineState *pcms,
        }
    }
 
-    /* Initialize PC system firmware */

hw/i386/pc.c void pc_memory_init(PCMachineState *pcms, · line 1035

Quoted 10 lines
hw/i386/pc.c +9 -1
-    pc_system_firmware_init(pcms, rom_memory);
+    /*
+     * If this is a confidential guest configured using IGVM then the IGVM
+     * configuration will include the system firmware. In this case do not
+     * initialise PC system firmware.
+     */
+    if (!cgs_is_igvm(machine->cgs)) {
+        /* Initialize PC system firmware */
+        pc_system_firmware_init(pcms, rom_memory);
+    }

Ok so this makes QEMU mot load the default fw as provided in the QEMU command line. It does not specify how the packaged fw specified within IGVM would be processed and loaded. Am I understanding this right?

#17 lore raw

Re: [PATCH 6/9] i386/pc: Skip initialization of system FW when using IGVM

Quoted 34 lines · Ani Sinha
Quoted 34 lines · Roy Hopkins

When using an IGVM file the configuration of the system firmware is defined by IGVM directives contained in the file. Therefore the default system firmware should not be initialized when an IGVM file has been provided.

This commit checks to see if an IGVM file has been provided and, if it has then the standard system firmware initialization is skipped and any prepared flash devices are cleaned up.

---
hw/i386/pc.c | 12 ++++++++++--

1 file changed, 10 insertions(+), 2 deletions(-)
hw/i386/pc.c +1 -0
index f8eb684a49..17bb211708 100644
@@ -63,6 +63,7 @@
 #include "e820_memory_layout.h"
 #include "trace.h"
 #include CONFIG_DEVICES
+#include "exec/confidential-guest-support.h"
 
 #ifdef CONFIG_XEN_EMU
 #include "hw/xen/xen-legacy-backend.h"
@@ -1023,8 +1024,15 @@ void pc_memory_init(PCMachineState *pcms,
         }
     }
 
Quoted 16 lines

hw/i386/pc.c void pc_memory_init(PCMachineState *pcms, · line 1035

Quoted 11 lines
hw/i386/pc.c +9 -2
-    /* Initialize PC system firmware */
-    pc_system_firmware_init(pcms, rom_memory);
+    /*
+     * If this is a confidential guest configured using IGVM then the IGVM
+     * configuration will include the system firmware. In this case do not
+     * initialise PC system firmware.
+     */
+    if (!cgs_is_igvm(machine->cgs)) {
+        /* Initialize PC system firmware */
+        pc_system_firmware_init(pcms, rom_memory);
+    }

Ok so this makes QEMU mot load the default fw as provided in the QEMU command line. It does not specify how the packaged fw specified within IGVM would be processed and loaded. Am I understanding this right?

Yes. Although as suggested by Daniel, I've now changed this so if firmware is specified on the command line in conflict with the IGVM file then an error is displayed. The IGVM file itself describes how the firmware binary is populated into guest memory and launched.

#18 lore raw

Re: [PATCH 6/9] i386/pc: Skip initialization of system FW when using IGVM

Quoted 55 lines · Roy Hopkins
Quoted 34 lines · Ani Sinha
Quoted 34 lines · Roy Hopkins

When using an IGVM file the configuration of the system firmware is defined by IGVM directives contained in the file. Therefore the default system firmware should not be initialized when an IGVM file has been provided.

This commit checks to see if an IGVM file has been provided and, if it has then the standard system firmware initialization is skipped and any prepared flash devices are cleaned up.

---
hw/i386/pc.c | 12 ++++++++++--

1 file changed, 10 insertions(+), 2 deletions(-)
hw/i386/pc.c +1 -0
index f8eb684a49..17bb211708 100644
@@ -63,6 +63,7 @@
 #include "e820_memory_layout.h"
 #include "trace.h"
 #include CONFIG_DEVICES
+#include "exec/confidential-guest-support.h"
 
 #ifdef CONFIG_XEN_EMU
 #include "hw/xen/xen-legacy-backend.h"
@@ -1023,8 +1024,15 @@ void pc_memory_init(PCMachineState *pcms,
        }
    }
 
Quoted 16 lines

hw/i386/pc.c void pc_memory_init(PCMachineState *pcms, · line 1035

Quoted 11 lines
hw/i386/pc.c +9 -2
-    /* Initialize PC system firmware */
-    pc_system_firmware_init(pcms, rom_memory);
+    /*
+     * If this is a confidential guest configured using IGVM then the IGVM
+     * configuration will include the system firmware. In this case do not
+     * initialise PC system firmware.
+     */
+    if (!cgs_is_igvm(machine->cgs)) {
+        /* Initialize PC system firmware */
+        pc_system_firmware_init(pcms, rom_memory);
+    }

Ok so this makes QEMU mot load the default fw as provided in the QEMU command line. It does not specify how the packaged fw specified within IGVM would be processed and loaded. Am I understanding this right?

Quoted 3 lines

Yes. Although as suggested by Daniel, I've now changed this so if firmware is specified on the command line in conflict with the IGVM file then an error is displayed.

Does IGVM _must_ mandatorily contain a firmware? If not, then before error is displayed, we should check if the file does have a firmware.

Quoted 2 lines

The IGVM file itself describes how the firmware binary is populated into guest memory and launched.

Ok so I looked at the doc here: https://docs.rs/igvm_defs/0.1.7/igvm_defs/
I do not see anything related to firmware in the spec.
Secondly, how the firmware is to be loaded is hypervisor specific. So there must be QEMU specific implementation to load the firmware from IGVM. Secondly, should a new directive (and associated definitions) be introduced that instructs hypervisors to load the firmware present in IGVM? Something is missing here it seems and I am willing to dive into filling the missing parts.

Quoted 1 line
#19 lore raw

Re: [PATCH 6/9] i386/pc: Skip initialization of system FW when using IGVM

Quoted 65 lines · Ani Sinha
Quoted 56 lines · Roy Hopkins
Quoted 34 lines · Ani Sinha
Quoted 34 lines · Roy Hopkins

When using an IGVM file the configuration of the system firmware is defined by IGVM directives contained in the file. Therefore the default system firmware should not be initialized when an IGVM file has been provided.

This commit checks to see if an IGVM file has been provided and, if it has then the standard system firmware initialization is skipped and any prepared flash devices are cleaned up.

---
hw/i386/pc.c | 12 ++++++++++--

1 file changed, 10 insertions(+), 2 deletions(-)
hw/i386/pc.c +1 -0
index f8eb684a49..17bb211708 100644
@@ -63,6 +63,7 @@
 #include "e820_memory_layout.h"
 #include "trace.h"
 #include CONFIG_DEVICES
+#include "exec/confidential-guest-support.h"
 
 #ifdef CONFIG_XEN_EMU
 #include "hw/xen/xen-legacy-backend.h"
@@ -1023,8 +1024,15 @@ void pc_memory_init(PCMachineState *pcms,
       }
   }
 
Quoted 16 lines

hw/i386/pc.c void pc_memory_init(PCMachineState *pcms, · line 1035

Quoted 11 lines
hw/i386/pc.c +9 -2
-    /* Initialize PC system firmware */
-    pc_system_firmware_init(pcms, rom_memory);
+    /*
+     * If this is a confidential guest configured using IGVM then the IGVM
+     * configuration will include the system firmware. In this case do not
+     * initialise PC system firmware.
+     */
+    if (!cgs_is_igvm(machine->cgs)) {
+        /* Initialize PC system firmware */
+        pc_system_firmware_init(pcms, rom_memory);
+    }

Ok so this makes QEMU mot load the default fw as provided in the QEMU command line. It does not specify how the packaged fw specified within IGVM would be processed and loaded. Am I understanding this right?

Quoted 3 lines

Yes. Although as suggested by Daniel, I've now changed this so if firmware is specified on the command line in conflict with the IGVM file then an error is displayed.

Does IGVM _must_ mandatorily contain a firmware? If not, then before error is displayed, we should check if the file does have a firmware.

Quoted 7 lines
Quoted 2 lines

The IGVM file itself describes how the firmware binary is populated into guest memory and launched.

Ok so I looked at the doc here: https://docs.rs/igvm_defs/0.1.7/igvm_defs/
I do not see anything related to firmware in the spec.
Secondly, how the firmware is to be loaded is hypervisor specific. So there must be QEMU specific implementation to load the firmware from IGVM. Secondly, should a new directive (and associated definitions) be introduced that instructs hypervisors to load the firmware present in IGVM? Something is missing here it seems and I am willing to dive into filling the missing parts.

I guess what I was missing was https://github.com/roy-hopkins/buildigvm/blob/main/src/ovmf_firmware.rs . Seems something like this is supposed to load ovmf image into memory.

#20 lore raw

PATCH 7/9 i386/sev: Refactor setting of reset vector and initial CPU state

In reply to Roy Hopkins

When an SEV guest is started, the reset vector and state are extracted from metadata that is contained in the firmware volume.

In preparation for using IGVM to setup the initial CPU state, the code has been refactored to populate vmcb_save_area for each CPU which is then applied during guest startup and CPU reset.

---
 target/i386/sev.c | 288 +++++++++++++++++++++++++++++++++++++++++-----
 target/i386/sev.h | 110 ++++++++++++++++++
 2 files changed, 369 insertions(+), 29 deletions(-)
target/i386/sev.c +259 -29
index 173de91afe..d6902432fd 100644
@@ -74,9 +74,7 @@ struct SevGuestState {
     SevState state;
     gchar *measurement;
 
-    uint32_t reset_cs;
-    uint32_t reset_ip;
-    bool reset_data_valid;
+    QTAILQ_HEAD(, SevLaunchVmsa) launch_vmsa;
 };
 
 #define DEFAULT_GUEST_POLICY    0x1 /* disable debug */
@@ -99,6 +97,12 @@ typedef struct QEMU_PACKED SevHashTableDescriptor {
 /* hard code sha256 digest size */
 #define HASH_SIZE 32
 
+/* Convert between SEV-ES VMSA and SegmentCache flags/attributes */
+#define FLAGS_VMSA_TO_SEGCACHE(flags) \
+    ((((flags) & 0xff00) << 12) | (((flags) & 0xff) << 8))
+#define FLAGS_SEGCACHE_TO_VMSA(flags) \
+    ((((flags) & 0xff00) >> 8) | (((flags) & 0xf00000) >> 12))
+
 typedef struct QEMU_PACKED SevHashTableEntry {
     QemuUUID guid;
     uint16_t len;
@@ -125,6 +129,15 @@ typedef struct QEMU_PACKED PaddedSevHashTable {
 QEMU_BUILD_BUG_ON(sizeof(PaddedSevHashTable) % 16 != 0);
 
 static SevGuestState *sev_guest;
+
+typedef struct SevLaunchVmsa {
+    QTAILQ_ENTRY(SevLaunchVmsa) next;
+
+    uint16_t cpu_index;
+    uint64_t gpa;
+    struct sev_es_save_area vmsa;
+} SevLaunchVmsa;
+
 static Error *sev_mig_blocker;
 
 static const char *const sev_fw_errlist[] = {
@@ -291,6 +304,149 @@ sev_guest_finalize(Object *obj)
 {
 }
 
+static void sev_apply_cpu_context(CPUState *cpu)
+{
+    SevGuestState *sev_guest = SEV_GUEST(MACHINE(qdev_get_machine())->cgs);
+    X86CPU *x86;
+    CPUX86State *env;
+    struct SevLaunchVmsa *launch_vmsa;
+
+    /* See if an initial VMSA has been provided for this CPU */
+    QTAILQ_FOREACH(launch_vmsa, &sev_guest->launch_vmsa, next)
+    {
+        if (cpu->cpu_index == launch_vmsa->cpu_index) {
+            x86 = X86_CPU(cpu);
+            env = &x86->env;
+
+            /*
+             * Ideally we would provide the VMSA directly to kvm which would
+             * ensure that the resulting initial VMSA measurement which is
+             * calculated during KVM_SEV_LAUNCH_UPDATE_VMSA is calculated from
+             * exactly what we provide here. Currently this is not possible so
+             * we need to copy the parts of the VMSA structure that we currently
+             * support into the CPU state.
+             */
+            cpu_load_efer(env, launch_vmsa->vmsa.efer);
+            cpu_x86_update_cr4(env, launch_vmsa->vmsa.cr4);
+            cpu_x86_update_cr0(env, launch_vmsa->vmsa.cr0);
+            cpu_x86_update_cr3(env, launch_vmsa->vmsa.cr3);
+
+            cpu_x86_load_seg_cache(
+                env, R_CS, launch_vmsa->vmsa.cs.selector,
+                launch_vmsa->vmsa.cs.base, launch_vmsa->vmsa.cs.limit,
+                FLAGS_VMSA_TO_SEGCACHE(launch_vmsa->vmsa.cs.attrib));
+            cpu_x86_load_seg_cache(
+                env, R_DS, launch_vmsa->vmsa.ds.selector,
+                launch_vmsa->vmsa.ds.base, launch_vmsa->vmsa.ds.limit,
+                FLAGS_VMSA_TO_SEGCACHE(launch_vmsa->vmsa.ds.attrib));
+            cpu_x86_load_seg_cache(
+                env, R_ES, launch_vmsa->vmsa.es.selector,
+                launch_vmsa->vmsa.es.base, launch_vmsa->vmsa.es.limit,
+                FLAGS_VMSA_TO_SEGCACHE(launch_vmsa->vmsa.es.attrib));
+            cpu_x86_load_seg_cache(
+                env, R_FS, launch_vmsa->vmsa.fs.selector,
+                launch_vmsa->vmsa.fs.base, launch_vmsa->vmsa.fs.limit,
+                FLAGS_VMSA_TO_SEGCACHE(launch_vmsa->vmsa.fs.attrib));
+            cpu_x86_load_seg_cache(
+                env, R_GS, launch_vmsa->vmsa.gs.selector,
+                launch_vmsa->vmsa.gs.base, launch_vmsa->vmsa.gs.limit,
+                FLAGS_VMSA_TO_SEGCACHE(launch_vmsa->vmsa.gs.attrib));
+            cpu_x86_load_seg_cache(
+                env, R_SS, launch_vmsa->vmsa.ss.selector,
+                launch_vmsa->vmsa.ss.base, launch_vmsa->vmsa.ss.limit,
+                FLAGS_VMSA_TO_SEGCACHE(launch_vmsa->vmsa.ss.attrib));
+
+            env->gdt.base = launch_vmsa->vmsa.gdtr.base;
+            env->gdt.limit = launch_vmsa->vmsa.gdtr.limit;
+            env->idt.base = launch_vmsa->vmsa.idtr.base;
+            env->idt.limit = launch_vmsa->vmsa.idtr.limit;
+
+            env->regs[R_EAX] = launch_vmsa->vmsa.rax;
+            env->regs[R_ECX] = launch_vmsa->vmsa.rcx;
+            env->regs[R_EDX] = launch_vmsa->vmsa.rdx;
+            env->regs[R_EBX] = launch_vmsa->vmsa.rbx;
+            env->regs[R_ESP] = launch_vmsa->vmsa.rsp;
+            env->regs[R_EBP] = launch_vmsa->vmsa.rbp;
+            env->regs[R_ESI] = launch_vmsa->vmsa.rsi;
+            env->regs[R_EDI] = launch_vmsa->vmsa.rdi;
+#ifdef TARGET_X86_64
+            env->regs[R_R8] = launch_vmsa->vmsa.r8;
+            env->regs[R_R9] = launch_vmsa->vmsa.r9;
+            env->regs[R_R10] = launch_vmsa->vmsa.r10;
+            env->regs[R_R11] = launch_vmsa->vmsa.r11;
+            env->regs[R_R12] = launch_vmsa->vmsa.r12;
+            env->regs[R_R13] = launch_vmsa->vmsa.r13;
+            env->regs[R_R14] = launch_vmsa->vmsa.r14;
+            env->regs[R_R15] = launch_vmsa->vmsa.r15;
+#endif
+            env->eip = launch_vmsa->vmsa.rip;
+            break;
+        }
+    }
+}
+
+static int sev_set_cpu_context(uint16_t cpu_index, const void *ctx,
+                               uint32_t ctx_len, hwaddr gpa)
+{
+    SevGuestState *sev_guest = SEV_GUEST(MACHINE(qdev_get_machine())->cgs);
+    SevLaunchVmsa *launch_vmsa;
+    CPUState *cpu;
+    bool exists = false;
+
+    /*
+     * Setting the CPU context is only supported for SEV-ES. The context buffer
+     * will contain a sev_es_save_area from the Linux kernel which is defined by
+     * "Table B-4. VMSA Layout, State Save Area for SEV-ES" in the AMD64 APM,
+     * Volume 2.
+     */
+
+    if (!sev_es_enabled()) {
+        error_report("SEV: unable to set CPU context: Not supported");
+        return 1;
+    }
+
+    if (ctx_len < sizeof(struct sev_es_save_area)) {
+        error_report("SEV: unable to set CPU context: "
+                     "Invalid context provided");
+        return 1;
+    }
+
+    cpu = qemu_get_cpu(cpu_index);
+    if (!cpu) {
+        error_report("SEV: unable to set CPU context for out of bounds "
+                     "CPU index %d", cpu_index);
+        return 1;
+    }
+
+    /*
+     * If the context of this VP has already been set then replace it with the
+     * new context.
+     */
+    QTAILQ_FOREACH(launch_vmsa, &sev_guest->launch_vmsa, next)
+    {
+        if (cpu_index == launch_vmsa->cpu_index) {
+            launch_vmsa->gpa = gpa;
+            memcpy(&launch_vmsa->vmsa, ctx, sizeof(launch_vmsa->vmsa));
+            exists = true;
+            break;
+        }
+    }
+
+    if (!exists) {
+        /* New VP context */
+        launch_vmsa = g_new0(SevLaunchVmsa, 1);
+        memcpy(&launch_vmsa->vmsa, ctx, sizeof(launch_vmsa->vmsa));
+        launch_vmsa->cpu_index = cpu_index;
+        launch_vmsa->gpa = gpa;
+        QTAILQ_INSERT_TAIL(&sev_guest->launch_vmsa, launch_vmsa, next);
+    }
+
+    /* Synchronise the VMSA with the current CPU state */
+    sev_apply_cpu_context(cpu);
+
+    return 0;
+}
+
 static char *
 sev_guest_get_session_file(Object *obj, Error **errp)
 {
@@ -394,6 +550,7 @@ sev_guest_instance_init(Object *obj)
     object_property_add_uint32_ptr(obj, "reduced-phys-bits",
                                    &sev->reduced_phys_bits,
                                    OBJ_PROP_FLAG_READWRITE);
+    QTAILQ_INIT(&sev->launch_vmsa);
 }
 
 /* sev guest info */
@@ -784,6 +941,16 @@ static int
 sev_launch_update_vmsa(SevGuestState *sev)
 {
     int ret, fw_error;
+    CPUState *cpu;
+
+    /*
+     * The initial CPU state is measured as part of KVM_SEV_LAUNCH_UPDATE_VMSA.
+     * Synchronise the CPU state to any provided launch VMSA structures.
+     */
+    CPU_FOREACH(cpu) {
+        sev_apply_cpu_context(cpu);
+    }
+
 
     ret = sev_ioctl(sev->sev_fd, KVM_SEV_LAUNCH_UPDATE_VMSA, NULL, &fw_error);
     if (ret) {
@@ -1196,34 +1363,99 @@ sev_es_find_reset_vector(void *flash_ptr, uint64_t flash_size,
     return sev_es_parse_reset_block(info, addr);
 }
 
-void sev_es_set_reset_vector(CPUState *cpu)
-{
-    X86CPU *x86;
-    CPUX86State *env;
 
-    /* Only update if we have valid reset information */
-    if (!sev_guest || !sev_guest->reset_data_valid) {
-        return;
-    }
+static void seg_to_vmsa(const SegmentCache *cpu_seg, struct vmcb_seg *vmsa_seg)
+{
+    vmsa_seg->selector = cpu_seg->selector;
+    vmsa_seg->base = cpu_seg->base;
+    vmsa_seg->limit = cpu_seg->limit;
+    vmsa_seg->attrib = FLAGS_SEGCACHE_TO_VMSA(cpu_seg->flags);
+}
 
-    /* Do not update the BSP reset state */
-    if (cpu->cpu_index == 0) {
-        return;
-    }
+static void initialize_vmsa(const CPUState *cpu, struct sev_es_save_area *vmsa)
+{
+    const X86CPU *x86 = X86_CPU(cpu);
+    const CPUX86State *env = &x86->env;
 
-    x86 = X86_CPU(cpu);
-    env = &x86->env;
+    /*
+     * Initialize the SEV-ES save area from the current state of
+     * the CPU. The entire state does not need to be copied, only the state
+     * that is copied back to the CPUState in sev_apply_cpu_context.
+     */
+    memset(vmsa, 0, sizeof(struct sev_es_save_area));
+    vmsa->efer = env->efer;
+    vmsa->cr0 = env->cr[0];
+    vmsa->cr3 = env->cr[3];
+    vmsa->cr4 = env->cr[4];
+
+    seg_to_vmsa(&env->segs[R_CS], &vmsa->cs);
+    seg_to_vmsa(&env->segs[R_DS], &vmsa->ds);
+    seg_to_vmsa(&env->segs[R_ES], &vmsa->es);
+    seg_to_vmsa(&env->segs[R_FS], &vmsa->fs);
+    seg_to_vmsa(&env->segs[R_GS], &vmsa->gs);
+    seg_to_vmsa(&env->segs[R_SS], &vmsa->ss);
+
+    seg_to_vmsa(&env->gdt, &vmsa->gdtr);
+    seg_to_vmsa(&env->idt, &vmsa->idtr);
+
+    vmsa->rax = env->regs[R_EAX];
+    vmsa->rcx = env->regs[R_ECX];
+    vmsa->rdx = env->regs[R_EDX];
+    vmsa->rbx = env->regs[R_EBX];
+    vmsa->rsp = env->regs[R_ESP];
+    vmsa->rbp = env->regs[R_EBP];
+    vmsa->rsi = env->regs[R_ESI];
+    vmsa->rdi = env->regs[R_EDI];
+
+#ifdef TARGET_X86_64
+    vmsa->r8 = env->regs[R_R8];
+    vmsa->r9 = env->regs[R_R9];
+    vmsa->r10 = env->regs[R_R10];
+    vmsa->r11 = env->regs[R_R11];
+    vmsa->r12 = env->regs[R_R12];
+    vmsa->r13 = env->regs[R_R13];
+    vmsa->r14 = env->regs[R_R14];
+    vmsa->r15 = env->regs[R_R15];
+#endif
+
+    vmsa->rip = env->eip;
+}
 
-    cpu_x86_load_seg_cache(env, R_CS, 0xf000, sev_guest->reset_cs, 0xffff,
-                           DESC_P_MASK | DESC_S_MASK | DESC_CS_MASK |
-                           DESC_R_MASK | DESC_A_MASK);
+static void sev_es_set_ap_context(uint32_t reset_addr)
+{
+    CPUState *cpu;
+    struct sev_es_save_area vmsa;
+    SegmentCache cs;
+
+    cs.selector = 0xf000;
+    cs.base = reset_addr & 0xffff0000;
+    cs.limit = 0xffff;
+    cs.flags = DESC_P_MASK | DESC_S_MASK | DESC_CS_MASK | DESC_R_MASK |
+               DESC_A_MASK;
+
+    CPU_FOREACH(cpu) {
+        if (cpu->cpu_index == 0) {
+            /* Do not update the BSP reset state */
+            continue;
+        }
+        initialize_vmsa(cpu, &vmsa);
+        seg_to_vmsa(&cs, &vmsa.cs);
+        vmsa.rip = reset_addr & 0x0000ffff;
+        sev_set_cpu_context(cpu->cpu_index, &vmsa,
+                            sizeof(struct sev_es_save_area), 0);
+        sev_apply_cpu_context(cpu);
+    }
+}
 
-    env->eip = sev_guest->reset_ip;
+void sev_es_set_reset_vector(CPUState *cpu)
+{
+    if (sev_enabled()) {
+        sev_apply_cpu_context(cpu);
+    }
 }
 
 int sev_es_save_reset_vector(void *flash_ptr, uint64_t flash_size)
 {
-    CPUState *cpu;
     uint32_t addr;
     int ret;
 
@@ -1238,14 +1470,12 @@ int sev_es_save_reset_vector(void *flash_ptr, uint64_t flash_size)
         return ret;
     }
 
+    /*
+     * The reset vector is saved into a CPU context for each AP but not for
+     * the BSP. This is applied during guest startup or when the CPU is reset.
+     */
     if (addr) {
-        sev_guest->reset_cs = addr & 0xffff0000;
-        sev_guest->reset_ip = addr & 0x0000ffff;
-        sev_guest->reset_data_valid = true;
-
-        CPU_FOREACH(cpu) {
-            sev_es_set_reset_vector(cpu);
-        }
+        sev_es_set_ap_context(addr);
     }
 
     return 0;
target/i386/sev.h +110 -0

116 lines not on this page · raw message

Signature · 1 line
2.43.0
#21 lore raw

Re: [PATCH 7/9] i386/sev: Refactor setting of reset vector and initial CPU state

Quoted 73 lines · Roy Hopkins

When an SEV guest is started, the reset vector and state are extracted from metadata that is contained in the firmware volume.

In preparation for using IGVM to setup the initial CPU state, the code has been refactored to populate vmcb_save_area for each CPU which is then applied during guest startup and CPU reset.

---
 target/i386/sev.c | 288 +++++++++++++++++++++++++++++++++++++++++-----
 target/i386/sev.h | 110 ++++++++++++++++++
 2 files changed, 369 insertions(+), 29 deletions(-)
target/i386/sev.c +28 -3
index 173de91afe..d6902432fd 100644
@@ -74,9 +74,7 @@ struct SevGuestState {
     SevState state;
     gchar *measurement;
 
-    uint32_t reset_cs;
-    uint32_t reset_ip;
-    bool reset_data_valid;
+    QTAILQ_HEAD(, SevLaunchVmsa) launch_vmsa;
 };
 
 #define DEFAULT_GUEST_POLICY    0x1 /* disable debug */
@@ -99,6 +97,12 @@ typedef struct QEMU_PACKED SevHashTableDescriptor {
 /* hard code sha256 digest size */
 #define HASH_SIZE 32
 
+/* Convert between SEV-ES VMSA and SegmentCache flags/attributes */
+#define FLAGS_VMSA_TO_SEGCACHE(flags) \
+    ((((flags) & 0xff00) << 12) | (((flags) & 0xff) << 8))
+#define FLAGS_SEGCACHE_TO_VMSA(flags) \
+    ((((flags) & 0xff00) >> 8) | (((flags) & 0xf00000) >> 12))
+
 typedef struct QEMU_PACKED SevHashTableEntry {
     QemuUUID guid;
     uint16_t len;
@@ -125,6 +129,15 @@ typedef struct QEMU_PACKED PaddedSevHashTable {
 QEMU_BUILD_BUG_ON(sizeof(PaddedSevHashTable) % 16 != 0);
 
 static SevGuestState *sev_guest;
+
+typedef struct SevLaunchVmsa {
+    QTAILQ_ENTRY(SevLaunchVmsa) next;
+
+    uint16_t cpu_index;
+    uint64_t gpa;
+    struct sev_es_save_area vmsa;
+} SevLaunchVmsa;
+
 static Error *sev_mig_blocker;
 
 static const char *const sev_fw_errlist[] = {
@@ -291,6 +304,149 @@ sev_guest_finalize(Object *obj)
 {
 }
 
+static void sev_apply_cpu_context(CPUState *cpu)
+{
+    SevGuestState *sev_guest = SEV_GUEST(MACHINE(qdev_get_machine())->cgs);
+    X86CPU *x86;
+    CPUX86State *env;
+    struct SevLaunchVmsa *launch_vmsa;
+
+    /* See if an initial VMSA has been provided for this CPU */
+    QTAILQ_FOREACH(launch_vmsa, &sev_guest->launch_vmsa, next)
+    {
+        if (cpu->cpu_index == launch_vmsa->cpu_index) {
+            x86 = X86_CPU(cpu);

target/i386/sev.c sev_guest_finalize(Object *obj) · line 328

Quoted 10 lines
target/i386/sev.c +10 -0
+            env = &x86->env;
+
+            /*
+             * Ideally we would provide the VMSA directly to kvm which would
+             * ensure that the resulting initial VMSA measurement which is
+             * calculated during KVM_SEV_LAUNCH_UPDATE_VMSA is calculated from
+             * exactly what we provide here. Currently this is not possible so
+             * we need to copy the parts of the VMSA structure that we currently
+             * support into the CPU state.
+             */

This sounds like it is saying that the code is not honouring everything in the VMSA defiend by the IGVM file ?

If so, that is pretty awkward. The VMSA is effectively an external ABI between QEMU and the guest owner (or whatever is validating guest attestation reports for them), and thus predictability and stability of this over time is critical.

We don't want the attestation process to be dependent/variable on the particular QEMU/KVM version, because any upgrade to QEMU/KVM could then alter the effective VMSA that the guest owner sees.

We've already suffered pain in this respect not long ago when the kernel arbitrarily changed a default setting which altered the VMSA it exposed, breaking existing apps that validate attestation.

What will it take to provide the full VMSA to KVM, so that we can guarantee to the guest owner than the VMSA for the guest is going to perfectly match what their IGVM defined ?

Quoted 48 lines
target/i386/sev.c +48 -0
+            cpu_load_efer(env, launch_vmsa->vmsa.efer);
+            cpu_x86_update_cr4(env, launch_vmsa->vmsa.cr4);
+            cpu_x86_update_cr0(env, launch_vmsa->vmsa.cr0);
+            cpu_x86_update_cr3(env, launch_vmsa->vmsa.cr3);
+
+            cpu_x86_load_seg_cache(
+                env, R_CS, launch_vmsa->vmsa.cs.selector,
+                launch_vmsa->vmsa.cs.base, launch_vmsa->vmsa.cs.limit,
+                FLAGS_VMSA_TO_SEGCACHE(launch_vmsa->vmsa.cs.attrib));
+            cpu_x86_load_seg_cache(
+                env, R_DS, launch_vmsa->vmsa.ds.selector,
+                launch_vmsa->vmsa.ds.base, launch_vmsa->vmsa.ds.limit,
+                FLAGS_VMSA_TO_SEGCACHE(launch_vmsa->vmsa.ds.attrib));
+            cpu_x86_load_seg_cache(
+                env, R_ES, launch_vmsa->vmsa.es.selector,
+                launch_vmsa->vmsa.es.base, launch_vmsa->vmsa.es.limit,
+                FLAGS_VMSA_TO_SEGCACHE(launch_vmsa->vmsa.es.attrib));
+            cpu_x86_load_seg_cache(
+                env, R_FS, launch_vmsa->vmsa.fs.selector,
+                launch_vmsa->vmsa.fs.base, launch_vmsa->vmsa.fs.limit,
+                FLAGS_VMSA_TO_SEGCACHE(launch_vmsa->vmsa.fs.attrib));
+            cpu_x86_load_seg_cache(
+                env, R_GS, launch_vmsa->vmsa.gs.selector,
+                launch_vmsa->vmsa.gs.base, launch_vmsa->vmsa.gs.limit,
+                FLAGS_VMSA_TO_SEGCACHE(launch_vmsa->vmsa.gs.attrib));
+            cpu_x86_load_seg_cache(
+                env, R_SS, launch_vmsa->vmsa.ss.selector,
+                launch_vmsa->vmsa.ss.base, launch_vmsa->vmsa.ss.limit,
+                FLAGS_VMSA_TO_SEGCACHE(launch_vmsa->vmsa.ss.attrib));
+
+            env->gdt.base = launch_vmsa->vmsa.gdtr.base;
+            env->gdt.limit = launch_vmsa->vmsa.gdtr.limit;
+            env->idt.base = launch_vmsa->vmsa.idtr.base;
+            env->idt.limit = launch_vmsa->vmsa.idtr.limit;
+
+            env->regs[R_EAX] = launch_vmsa->vmsa.rax;
+            env->regs[R_ECX] = launch_vmsa->vmsa.rcx;
+            env->regs[R_EDX] = launch_vmsa->vmsa.rdx;
+            env->regs[R_EBX] = launch_vmsa->vmsa.rbx;
+            env->regs[R_ESP] = launch_vmsa->vmsa.rsp;
+            env->regs[R_EBP] = launch_vmsa->vmsa.rbp;
+            env->regs[R_ESI] = launch_vmsa->vmsa.rsi;
+            env->regs[R_EDI] = launch_vmsa->vmsa.rdi;
+#ifdef TARGET_X86_64
+            env->regs[R_R8] = launch_vmsa->vmsa.r8;
+            env->regs[R_R9] = launch_vmsa->vmsa.r9;
+            env->regs[R_R10] = launch_vmsa->vmsa.r10;
+            env->regs[R_R11] = launch_vmsa->vmsa.r11;

target/i386/sev.c sev_guest_finalize(Object *obj) · line 386

Quoted 10 lines
target/i386/sev.c +10 -0
+            env->regs[R_R12] = launch_vmsa->vmsa.r12;
+            env->regs[R_R13] = launch_vmsa->vmsa.r13;
+            env->regs[R_R14] = launch_vmsa->vmsa.r14;
+            env->regs[R_R15] = launch_vmsa->vmsa.r15;
+#endif
+            env->eip = launch_vmsa->vmsa.rip;
+            break;
+        }
+    }
+}

With regards,
Daniel

Signature · 3 lines
|: 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 :|
#22 lore raw

Re: [PATCH 7/9] i386/sev: Refactor setting of reset vector and initial CPU state

Quoted 101 lines · Daniel P. Berrangé
Quoted 79 lines · Roy Hopkins

When an SEV guest is started, the reset vector and state are extracted from metadata that is contained in the firmware volume.

In preparation for using IGVM to setup the initial CPU state, the code has been refactored to populate vmcb_save_area for each CPU which is then applied during guest startup and CPU reset.

---
 target/i386/sev.c | 288 +++++++++++++++++++++++++++++++++++++++++----- target/i386/sev.h | 110 ++++++++++++++++++

 2 files changed, 369 insertions(+), 29 deletions(-)
target/i386/sev.c +0 -0
index 173de91afe..d6902432fd 100644
@@ -74,9 +74,7 @@ struct SevGuestState {
      SevState state;
      gchar *measurement;


-    uint32_t reset_cs;
-    uint32_t reset_ip;
-    bool reset_data_valid;
+    QTAILQ_HEAD(, SevLaunchVmsa) launch_vmsa; };

 #define DEFAULT_GUEST_POLICY    0x1 /* disable debug */

target/i386/sev.c +0 -0
@@ -99,6 +97,12 @@ typedef struct QEMU_PACKED SevHashTableDescriptor {
  /* hard code sha256 digest size */
  #define HASH_SIZE 32

target/i386/sev.c +6 -0
+/* Convert between SEV-ES VMSA and SegmentCache flags/attributes */
+#define FLAGS_VMSA_TO_SEGCACHE(flags) \
+    ((((flags) & 0xff00) << 12) | (((flags) & 0xff) << 8))
+#define FLAGS_SEGCACHE_TO_VMSA(flags) \
+    ((((flags) & 0xff00) >> 8) | (((flags) & 0xf00000) >> 12))
+

 typedef struct QEMU_PACKED SevHashTableEntry {
     QemuUUID guid;
     uint16_t len;

target/i386/sev.c +0 -0
@@ -125,6 +129,15 @@ typedef struct QEMU_PACKED PaddedSevHashTable {
  QEMU_BUILD_BUG_ON(sizeof(PaddedSevHashTable) % 16 != 0);


 static SevGuestState *sev_guest;

target/i386/sev.c +9 -0
+
+typedef struct SevLaunchVmsa {
+    QTAILQ_ENTRY(SevLaunchVmsa) next;
+
+    uint16_t cpu_index;
+    uint64_t gpa;
+    struct sev_es_save_area vmsa;
+} SevLaunchVmsa;
+

 static Error *sev_mig_blocker;

 static const char *const sev_fw_errlist[] = {

target/i386/sev.c +0 -0
@@ -291,6 +304,149 @@ sev_guest_finalize(Object *obj)
  {
  }

target/i386/sev.c +16 -0
+static void sev_apply_cpu_context(CPUState *cpu)
+{
+    SevGuestState *sev_guest = SEV_GUEST(MACHINE(qdev_get_machine())->cgs);
+    X86CPU *x86;
+    CPUX86State *env;
+    struct SevLaunchVmsa *launch_vmsa;
+
+    /* See if an initial VMSA has been provided for this CPU */
+    QTAILQ_FOREACH(launch_vmsa, &sev_guest->launch_vmsa, next)
+    {
+        if (cpu->cpu_index == launch_vmsa->cpu_index) {
+            x86 = X86_CPU(cpu);
+            env = &x86->env;
+
+            /*
+             * Ideally we would provide the VMSA directly to kvm which

would
+             * ensure that the resulting initial VMSA measurement which is

Quoted 8 lines

+             * calculated during KVM_SEV_LAUNCH_UPDATE_VMSA is calculated from
+             * exactly what we provide here. Currently this is not possible so
+             * we need to copy the parts of the VMSA structure that we currently
+             * support into the CPU state.
+             */

This sounds like it is saying that the code is not honouring everything in the VMSA defiend by the IGVM file ?

If so, that is pretty awkward. The VMSA is effectively an external ABI between QEMU and the guest owner (or whatever is validating guest attestation reports for them), and thus predictability and stability of this over time is critical.

We don't want the attestation process to be dependent/variable on the particular QEMU/KVM version, because any upgrade to QEMU/KVM could then alter the effective VMSA that the guest owner sees.

Quoted 8 lines

We've already suffered pain in this respect not long ago when the kernel arbitrarily changed a default setting which altered the VMSA it exposed, breaking existing apps that validate attestation.

What will it take to provide the full VMSA to KVM, so that we can guarantee to the guest owner than the VMSA for the guest is going to perfectly match what their IGVM defined ?

Yes, the fact that we have to copy the individual fields from the VMSA to "CPUX86State" is less than ideal - a problem made worse by the fact that the kernel does not allow direct control over some of the fields from userspace, "sev_features" being a good example here where "SVM_SEV_FEAT_DEBUG_SWAP" is unconditionally added by the kernel.

The kernel VMSA is at least predictable. So, although we cannot yet allow full flexibility in providing a complete VMSA from QEMU and guarantee it will be honoured, we could check to see if any settings conflict with those imposed by the kernel and exit with an error if this is the case. I chose not to implement for this first series but could easily add a patch to support this. The problem here is that it ties the version of QEMU to VMSA handling functionality in the kernel. Any change to the VMSA handling in the kernel would potentially invalidate the checks in QEMU. The one upside here is that this will easily be detectable by the attestation measurement not matching the expected measurement of the IGVM file. But it will be difficult for the user to determine what the discrepancy is.

The ideal solution is to add or modify a KVM ioctl to allow the VMSA to be set directly, overriding the state in "CPUX86State". The current KVM_SEV_LAUNCH_UPDATE_VMSA ioctl triggers the synchronisation of the VMSA but does not allow it to be specified directly. This could be modified for what we need. The SEV-SNP kernel patches add KVM_SEV_SNP_LAUNCH_UPDATE which allows a page type of VMSA to be updated, although the current patch series does not support using this to set the initial state of the VMSA:
https://lore.kernel.org/lkml/[email protected]/ I have experimented with this myself and have successfully modified the SEV-SNP kernel patches to support directly setting the VMSA from QEMU.

On the other hand, I have also verified that I can indeed measure an IGVM file loaded using the VMSA synchronisation method currently employed and get a matching measurement from the SEV attestation report.

What would you suggest is the best way forward for this?

Quoted 54 lines
Quoted 54 lines

+            cpu_load_efer(env, launch_vmsa->vmsa.efer); +            cpu_x86_update_cr4(env, launch_vmsa->vmsa.cr4); +            cpu_x86_update_cr0(env, launch_vmsa->vmsa.cr0); +            cpu_x86_update_cr3(env, launch_vmsa->vmsa.cr3);

target/i386/sev.c +50 -0
+
+            cpu_x86_load_seg_cache(
+                env, R_CS, launch_vmsa->vmsa.cs.selector,
+                launch_vmsa->vmsa.cs.base, launch_vmsa->vmsa.cs.limit,
+                FLAGS_VMSA_TO_SEGCACHE(launch_vmsa->vmsa.cs.attrib));
+            cpu_x86_load_seg_cache(
+                env, R_DS, launch_vmsa->vmsa.ds.selector,
+                launch_vmsa->vmsa.ds.base, launch_vmsa->vmsa.ds.limit,
+                FLAGS_VMSA_TO_SEGCACHE(launch_vmsa->vmsa.ds.attrib));
+            cpu_x86_load_seg_cache(
+                env, R_ES, launch_vmsa->vmsa.es.selector,
+                launch_vmsa->vmsa.es.base, launch_vmsa->vmsa.es.limit,
+                FLAGS_VMSA_TO_SEGCACHE(launch_vmsa->vmsa.es.attrib));
+            cpu_x86_load_seg_cache(
+                env, R_FS, launch_vmsa->vmsa.fs.selector,
+                launch_vmsa->vmsa.fs.base, launch_vmsa->vmsa.fs.limit,
+                FLAGS_VMSA_TO_SEGCACHE(launch_vmsa->vmsa.fs.attrib));
+            cpu_x86_load_seg_cache(
+                env, R_GS, launch_vmsa->vmsa.gs.selector,
+                launch_vmsa->vmsa.gs.base, launch_vmsa->vmsa.gs.limit,
+                FLAGS_VMSA_TO_SEGCACHE(launch_vmsa->vmsa.gs.attrib));
+            cpu_x86_load_seg_cache(
+                env, R_SS, launch_vmsa->vmsa.ss.selector,
+                launch_vmsa->vmsa.ss.base, launch_vmsa->vmsa.ss.limit,
+                FLAGS_VMSA_TO_SEGCACHE(launch_vmsa->vmsa.ss.attrib));
+
+            env->gdt.base = launch_vmsa->vmsa.gdtr.base;
+            env->gdt.limit = launch_vmsa->vmsa.gdtr.limit;
+            env->idt.base = launch_vmsa->vmsa.idtr.base;
+            env->idt.limit = launch_vmsa->vmsa.idtr.limit;
+
+            env->regs[R_EAX] = launch_vmsa->vmsa.rax;
+            env->regs[R_ECX] = launch_vmsa->vmsa.rcx;
+            env->regs[R_EDX] = launch_vmsa->vmsa.rdx;
+            env->regs[R_EBX] = launch_vmsa->vmsa.rbx;
+            env->regs[R_ESP] = launch_vmsa->vmsa.rsp;
+            env->regs[R_EBP] = launch_vmsa->vmsa.rbp;
+            env->regs[R_ESI] = launch_vmsa->vmsa.rsi;
+            env->regs[R_EDI] = launch_vmsa->vmsa.rdi;
+#ifdef TARGET_X86_64
+            env->regs[R_R8] = launch_vmsa->vmsa.r8;
+            env->regs[R_R9] = launch_vmsa->vmsa.r9;
+            env->regs[R_R10] = launch_vmsa->vmsa.r10;
+            env->regs[R_R11] = launch_vmsa->vmsa.r11;
+            env->regs[R_R12] = launch_vmsa->vmsa.r12;
+            env->regs[R_R13] = launch_vmsa->vmsa.r13;
+            env->regs[R_R14] = launch_vmsa->vmsa.r14;
+            env->regs[R_R15] = launch_vmsa->vmsa.r15;
+#endif
+            env->eip = launch_vmsa->vmsa.rip;
Quoted 8 lines
Quoted 4 lines

+            break;
+        }
+    }
+}

With regards,
Daniel

Regards,
Roy

#23 lore raw

Re: [PATCH 7/9] i386/sev: Refactor setting of reset vector and initial CPU state

Quoted 36 lines · Roy Hopkins
Quoted 26 lines · Daniel P. Berrangé
Quoted 4 lines · Roy Hopkins

+            /*
+             * Ideally we would provide the VMSA directly to kvm which would
+             * ensure that the resulting initial VMSA measurement which is

Quoted 8 lines

+             * calculated during KVM_SEV_LAUNCH_UPDATE_VMSA is calculated from
+             * exactly what we provide here. Currently this is not possible so
+             * we need to copy the parts of the VMSA structure that we currently
+             * support into the CPU state.
+             */

This sounds like it is saying that the code is not honouring everything in the VMSA defiend by the IGVM file ?

If so, that is pretty awkward. The VMSA is effectively an external ABI between QEMU and the guest owner (or whatever is validating guest attestation reports for them), and thus predictability and stability of this over time is critical.

We don't want the attestation process to be dependent/variable on the particular QEMU/KVM version, because any upgrade to QEMU/KVM could then alter the effective VMSA that the guest owner sees.

Quoted 8 lines

We've already suffered pain in this respect not long ago when the kernel arbitrarily changed a default setting which altered the VMSA it exposed, breaking existing apps that validate attestation.

What will it take to provide the full VMSA to KVM, so that we can guarantee to the guest owner than the VMSA for the guest is going to perfectly match what their IGVM defined ?

Quoted 5 lines

Yes, the fact that we have to copy the individual fields from the VMSA to "CPUX86State" is less than ideal - a problem made worse by the fact that the kernel does not allow direct control over some of the fields from userspace, "sev_features" being a good example here where "SVM_SEV_FEAT_DEBUG_SWAP" is unconditionally added by the kernel.

Ah yes, the SVM_SEV_FEAT_DEBUG_SWAP feature is the one I couldn't remember the name of in my quoted text above, that break our apps when the kernel suddenly set it by default (thankfully now reverted in Linux with 5abf6dceb066f2b02b225fd561440c98a8062681).

Quoted 11 lines

The kernel VMSA is at least predictable. So, although we cannot yet allow full flexibility in providing a complete VMSA from QEMU and guarantee it will be honoured, we could check to see if any settings conflict with those imposed by the kernel and exit with an error if this is the case. I chose not to implement for this first series but could easily add a patch to support this. The problem here is that it ties the version of QEMU to VMSA handling functionality in the kernel. Any change to the VMSA handling in the kernel would potentially invalidate the checks in QEMU. The one upside here is that this will easily be detectable by the attestation measurement not matching the expected measurement of the IGVM file. But it will be difficult for the user to determine what the discrepancy is.

Yes, the difficulty in diagnosis is the big thing I'm worried about from a distro supportability POV. The DEBUG_SWAP issue caused us a bunch of pain and that's before CVMs are even widely used.

I agree that hardcoding checks in QEMU is pretty unpleasant, and probably not something that I'd want us to do. I'd want QEMU to be able to live query the kernel's default initial VMSA, if it were to be reporting differences vs the IGVM provided VMSA. I dn't think there's a way to do that nicely though - i only know of ftrace probes to dump it informally.

I guess if we know & document what subset of the VMSA QEMU /can/ directly control, that at least narrows down where to look if something does change or go wrong.

Quoted 11 lines

The ideal solution is to add or modify a KVM ioctl to allow the VMSA to be set directly, overriding the state in "CPUX86State". The current KVM_SEV_LAUNCH_UPDATE_VMSA ioctl triggers the synchronisation of the VMSA but does not allow it to be specified directly. This could be modified for what we need. The SEV-SNP kernel patches add KVM_SEV_SNP_LAUNCH_UPDATE which allows a page type of VMSA to be updated, although the current patch series does not support using this to set the initial state of the VMSA:
https://lore.kernel.org/lkml/[email protected]/ I have experimented with this myself and have successfully modified the SEV-SNP kernel patches to support directly setting the VMSA from QEMU.

Quoted 5 lines

On the other hand, I have also verified that I can indeed measure an IGVM file loaded using the VMSA synchronisation method currently employed and get a matching measurement from the SEV attestation report.

What would you suggest is the best way forward for this?

I'll delegate to Paolo for an opinion on the possiblity of new (or updated) ioctls to provide the full VMSA data.

If we can't directly set the full VMSA, then next best option is a more formal way query to VMSA. That way libvirt could report on what the default initial kernel VMSA state is, which could be useful debug info for any bug reports.

With regards,
Daniel

Signature · 3 lines
|: 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 :|
#24 lore raw

Re: [PATCH 7/9] i386/sev: Refactor setting of reset vector and initial CPU state

Quoted 81 lines · Daniel P. Berrangé
Quoted 39 lines · Roy Hopkins
Quoted 29 lines · Daniel P. Berrangé
Quoted 7 lines · Roy Hopkins

+            /*
+             * Ideally we would provide the VMSA directly to kvm which would
+             * ensure that the resulting initial VMSA measurement which is
+             * calculated during KVM_SEV_LAUNCH_UPDATE_VMSA is calculated

Quoted 8 lines

from
+             * exactly what we provide here. Currently this is not possible
so
+             * we need to copy the parts of the VMSA structure that we currently
+             * support into the CPU state.
+             */

This sounds like it is saying that the code is not honouring everything in the VMSA defiend by the IGVM file ?

If so, that is pretty awkward. The VMSA is effectively an external ABI between QEMU and the guest owner (or whatever is validating guest attestation reports for them), and thus predictability and stability of this over time is critical.

We don't want the attestation process to be dependent/variable on the particular QEMU/KVM version, because any upgrade to QEMU/KVM could then alter the effective VMSA that the guest owner sees.

Quoted 8 lines

We've already suffered pain in this respect not long ago when the kernel arbitrarily changed a default setting which altered the VMSA it exposed, breaking existing apps that validate attestation.

What will it take to provide the full VMSA to KVM, so that we can guarantee to the guest owner than the VMSA for the guest is going to perfectly match what their IGVM defined ?

Quoted 5 lines

Yes, the fact that we have to copy the individual fields from the VMSA to "CPUX86State" is less than ideal - a problem made worse by the fact that the kernel does not allow direct control over some of the fields from userspace, "sev_features" being a good example here where "SVM_SEV_FEAT_DEBUG_SWAP" is unconditionally added by the kernel.

Ah yes, the SVM_SEV_FEAT_DEBUG_SWAP feature is the one I couldn't remember the name of in my quoted text above, that break our apps when the kernel suddenly set it by default (thankfully now reverted in Linux with 5abf6dceb066f2b02b225fd561440c98a8062681).

Quoted 11 lines

The kernel VMSA is at least predictable. So, although we cannot yet allow full
flexibility in providing a complete VMSA from QEMU and guarantee it will be honoured, we could check to see if any settings conflict with those imposed by
the kernel and exit with an error if this is the case. I chose not to implement
for this first series but could easily add a patch to support this. The problem
here is that it ties the version of QEMU to VMSA handling functionality in the

Quoted 8 lines

kernel. Any change to the VMSA handling in the kernel would potentially invalidate the checks in QEMU. The one upside here is that this will easily be
detectable by the attestation measurement not matching the expected measurement
of the IGVM file. But it will be difficult for the user to determine what the
discrepancy is.

Yes, the difficulty in diagnosis is the big thing I'm worried about from a distro supportability POV. The DEBUG_SWAP issue caused us a bunch of pain and that's before CVMs are even widely used.

I agree that hardcoding checks in QEMU is pretty unpleasant, and probably not something that I'd want us to do. I'd want QEMU to be able to live query the kernel's default initial VMSA, if it were to be reporting differences vs the IGVM provided VMSA. I dn't think there's a way to do that nicely though - i only know of ftrace probes to dump it informally.

Quoted 4 lines

I guess if we know & document what subset of the VMSA QEMU /can/ directly control, that at least narrows down where to look if something does change or go wrong.

Yes, it makes sense to document the subset that can be reliably set by QEMU, along with any modifications made byt the kernel. Perhaps I should go one step further and check that the VMSA does not contain any entries beyond what is copied in "sev_apply_cpu_context()"? If any field other than those explicitly copied by this function contain a non-zero value then an error is generated. As you suggest this will limit the scope of any measurement differences to the documented subset.

Quoted 24 lines
Quoted 17 lines

The ideal solution is to add or modify a KVM ioctl to allow the VMSA to be set
directly, overriding the state in "CPUX86State". The current KVM_SEV_LAUNCH_UPDATE_VMSA ioctl triggers the synchronisation of the VMSA but
does not allow it to be specified directly. This could be modified for what we
need. The SEV-SNP kernel patches add KVM_SEV_SNP_LAUNCH_UPDATE which allows a
page type of VMSA to be updated, although the current patch series does not support using this to set the initial state of the VMSA:
https://lore.kernel.org/lkml/[email protected]/
I
have experimented with this myself and have successfully modified the SEV- SNP
kernel patches to support directly setting the VMSA from QEMU.

Quoted 6 lines

On the other hand, I have also verified that I can indeed measure an IGVM file
loaded using the VMSA synchronisation method currently employed and get a matching measurement from the SEV attestation report.

What would you suggest is the best way forward for this?

Quoted 7 lines

I'll delegate to Paolo for an opinion on the possiblity of new (or updated) ioctls to provide the full VMSA data.

If we can't directly set the full VMSA, then next best option is a more formal way query to VMSA. That way libvirt could report on what the default initial kernel VMSA state is, which could be useful debug info for any bug reports.

Setting the full VMSA definitely seems like the right option here. Querying the VMSA that was actually measured would obviously give us the ability to diagnose problems with the measurement but does not allow full compatibility with the IGVM specification. This will potentially restrict the types of guests that can be packaged using IGVM.

Another thing to bear in mind is that with the incoming host kernel support for SEV-SNP, there are more constraints on how the VMSA is measured and populated. In particular, the current patches for SEV-SNP automatically sync and measure the VMSA as the final stage of guest measurement, requiring the IGVM file to provide the VMSA as the final directive for the measurement to match. Also, the kernel hardcodes the VMSA GPA, again requiring the IGVM file to match. If we have the ability to provide the VMSA directly (including the GPA of the VMSA) then these restrictions are removed.

I'd suggest that for SEV and SEV-ES, the current method of syncing certain fields (and updating the QEMU documentation to describe this) is sufficient for now. And perhaps this is ok for SEV-SNP too, but we should pursue the ability to provide the full VMSA at least in the SEV-SNP case.

Quoted 3 lines

With regards,
Daniel

Kind regards,
Roy

#25 lore raw

PATCH 8/9 i386/sev: Implement ConfidentialGuestSupport functions for SEV

In reply to Roy Hopkins

The ConfidentialGuestSupport object defines a number of virtual functions that are called during processing of IGVM directives to query or configure initial guest state. In order to support processing of IGVM files, these functions need to be implemented by relevant isolation hardware support code such as SEV.

This commit implements the required functions for SEV-ES and adds support for processing IGVM files for configuring the guest.

---
 target/i386/sev.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 77 insertions(+)
target/i386/sev.c +77 -0
index d6902432fd..b85ccff9db 100644
@@ -37,6 +37,7 @@
 #include "qapi/qapi-commands-misc-target.h"
 #include "exec/confidential-guest-support.h"
 #include "hw/i386/pc.h"
+#include "hw/i386/e820_memory_layout.h"
 #include "exec/address-spaces.h"
 
 #define TYPE_SEV_GUEST "sev-guest"
@@ -170,6 +171,9 @@ static const char *const sev_fw_errlist[] = {
 
 #define SEV_FW_MAX_ERROR      ARRAY_SIZE(sev_fw_errlist)
 
+static int sev_launch_update_data(SevGuestState *sev_guest, uint8_t *addr,
+                                  uint64_t len);
+
 static int
 sev_ioctl(int fd, int cmd, void *data, int *error)
 {
@@ -304,6 +308,14 @@ sev_guest_finalize(Object *obj)
 {
 }
 
+static int cgs_check_support(ConfidentialGuestPlatformType platform,
+                             uint16_t platform_version, uint8_t highest_vtl,
+                             uint64_t shared_gpa_boundary)
+{
+    return (((platform == CGS_PLATFORM_SEV_ES) && sev_es_enabled()) ||
+            ((platform == CGS_PLATFORM_SEV) && sev_enabled())) ? 1 : 0;
+}
+
 static void sev_apply_cpu_context(CPUState *cpu)
 {
     SevGuestState *sev_guest = SEV_GUEST(MACHINE(qdev_get_machine())->cgs);
@@ -447,6 +459,65 @@ static int sev_set_cpu_context(uint16_t cpu_index, const void *ctx,
     return 0;
 }
 
+static int cgs_set_guest_state(hwaddr gpa, uint8_t *ptr, uint64_t len,
+                               ConfidentialGuestPageType memory_type,
+                               uint16_t cpu_index)
+{
+    SevGuestState *sev = SEV_GUEST(MACHINE(qdev_get_machine())->cgs);
+    int ret = 1;
+
+    if (!sev_enabled()) {
+        error_report("%s: attempt to configure guest memory, but SEV "
+                     "is not enabled",
+                     __func__);
+    } else if (memory_type == CGS_PAGE_TYPE_VMSA) {
+        if (!sev_es_enabled()) {
+            error_report("%s: attempt to configure initial VMSA, but SEV-ES "
+                         "is not supported",
+                         __func__);
+        } else {
+            ret = sev_set_cpu_context(cpu_index, ptr, len, gpa);
+        }
+    } else if ((memory_type != CGS_PAGE_TYPE_REQUIRED_MEMORY) &&
+               (memory_type != CGS_PAGE_TYPE_UNMEASURED)) {
+        ret = sev_launch_update_data(sev, ptr, len);
+    } else {
+        error_report(
+            "%s: attempted to configure guest memory to use memory_type %d, "
+            "but this type is not supported by SEV-ES",
+            __func__, (int)memory_type);
+    }
+    return ret;
+}
+
+static int cgs_get_mem_map_entry(int index,
+                                  ConfidentialGuestMemoryMapEntry *entry)
+{
+    if ((index < 0) || (index >= e820_get_num_entries())) {
+        return 1;
+    }
+    entry->gpa = e820_table[index].address;
+    entry->size = e820_table[index].length;
+    switch (e820_table[index].type) {
+    case E820_RAM:
+        entry->type = CGS_MEM_RAM;
+        break;
+    case E820_RESERVED:
+        entry->type = CGS_MEM_RESERVED;
+        break;
+    case E820_ACPI:
+        entry->type = CGS_MEM_ACPI;
+        break;
+    case E820_NVS:
+        entry->type = CGS_MEM_NVS;
+        break;
+    case E820_UNUSABLE:
+        entry->type = CGS_MEM_UNUSABLE;
+        break;
+    }
+    return 0;
+}
+
 static char *
 sev_guest_get_session_file(Object *obj, Error **errp)
 {
@@ -538,6 +609,7 @@ static void
 sev_guest_instance_init(Object *obj)
 {
     SevGuestState *sev = SEV_GUEST(obj);
+    ConfidentialGuestSupport *cgs = CONFIDENTIAL_GUEST_SUPPORT(obj);
 
     sev->sev_device = g_strdup(DEFAULT_SEV_DEVICE);
     sev->policy = DEFAULT_GUEST_POLICY;
@@ -550,6 +622,11 @@ sev_guest_instance_init(Object *obj)
     object_property_add_uint32_ptr(obj, "reduced-phys-bits",
                                    &sev->reduced_phys_bits,
                                    OBJ_PROP_FLAG_READWRITE);
+
+    cgs->check_support = cgs_check_support;
+    cgs->set_guest_state = cgs_set_guest_state;
+    cgs->get_mem_map_entry = cgs_get_mem_map_entry;
+
     QTAILQ_INIT(&sev->launch_vmsa);
 }
 
Signature · 1 line
2.43.0
#26 lore raw

PATCH 9/9 docs/system: Add documentation on support for IGVM

In reply to Roy Hopkins

IGVM support has been implemented for Confidential Guests that support AMD SEV and AMD SEV-ES. Add some documentation that gives some background on the IGVM format and how to use it to configure a confidential guest.

---
 docs/system/igvm.rst  | 58 +++++++++++++++++++++++++++++++++++++++++++
 docs/system/index.rst |  1 +
 2 files changed, 59 insertions(+)
 create mode 100644 docs/system/igvm.rst
docs/system/igvm.rst new file +58 -0
new file mode 100644
index 0000000000..bb0c43f0ee
@@ -0,0 +1,58 @@
+Independent Guest Virtual Machine (IGVM) support
+================================================
+
+IGVM files are designed to encaspulate all the information required to launch a
+virtual machine on any given virtualization stack in a deterministic way. This
+allows the cryptographic measurement of initial guest state for Confidential
+Guests to be calculated when the IGVM file is built, allowing a relying party to
+verify the initial state of a guest via a remote attestation.
+
+QEMU supports IGVM files through the Confidential Guest Support object. An igvm
+filename can optionally be passed to the object which will subsequently be
+parsed and used to configure the guest state prior to launching the guest.
+
+Further Information on IGVM
+---------------------------
+
+Information about the IGVM format, including links to the format specification
+and documentation for the Rust and C libraries can be found at the project
+repository:
+
+https://github.com/microsoft/igvm
+
+
+Supported Confidential Guests
+-----------------------------
+
+Currently, IGVM files can be provided for Confidential Guests on host systems
+that support AMD SEV and SEV-ES.
+
+IGVM files contain a set of directives. Not every directive is supported by
+every Confidential Guest type. For example, setting the initial CPU state is not
+supported on AMD SEV due to the platform not supporting encrypted save state
+regions. However, this is supported on SEV-ES.
+
+When an IGVM file contains directives that are not supported for the active
+platform, an error is displayed and the guest launch is aborted.
+
+Firmware Images with IGVM
+-------------------------
+
+When an IGVM filename is specified for a Confidential Guest Support object it
+overrides the default handling of system firmware: the firmware image, such as
+an OVMF binary should be contained as a payload of the IGVM file and not
+provided as a flash drive. The default QEMU firmware is not automatically mapped
+into guest memory.
+
+Running a Confidential Guest configured using IGVM
+--------------------------------------------------
+
+To run a confidential guest configured with IGVM you need to add the
+``igvm-file`` parameter to the "confidential guest support" object:
+
+Example (for AMD SEV)::
+
+    qemu-system-x86_64 \
+        <other parameters> \
+        -machine ...,confidential-guest-support=sev0 \
+        -object sev-guest,id=sev0,cbitpos=47,reduced-phys-bits=1,igvm-file=/path/to/guest.igvm
docs/system/index.rst +1 -0
index c21065e519..6235dfab87 100644
@@ -38,4 +38,5 @@ or Hypervisor.Framework.
    security
    multi-process
    confidential-guest-support
+   igvm
    vm-templating
Signature · 1 line
2.43.0
#27 lore raw

Re: [PATCH 9/9] docs/system: Add documentation on support for IGVM

Quoted 13 lines · Roy Hopkins

IGVM support has been implemented for Confidential Guests that support AMD SEV and AMD SEV-ES. Add some documentation that gives some background on the IGVM format and how to use it to configure a confidential guest.

---
 docs/system/igvm.rst  | 58 +++++++++++++++++++++++++++++++++++++++++++
 docs/system/index.rst |  1 +
 2 files changed, 59 insertions(+)
 create mode 100644 docs/system/igvm.rst

docs/system/igvm.rst line 45

Quoted 8 lines
docs/system/igvm.rst new file +8 -0
+Firmware Images with IGVM
+-------------------------
+
+When an IGVM filename is specified for a Confidential Guest Support object it
+overrides the default handling of system firmware: the firmware image, such as
+an OVMF binary should be contained as a payload of the IGVM file and not
+provided as a flash drive. The default QEMU firmware is not automatically mapped
+into guest memory.

IIUC, in future the IGVM file could contain both the OVMF and SVSM binaries ?

I'm also wondering if there can be dependancies between the IGVM file and the broader QEMU configuration ? eg if SVSM gains suupport for data persistence, potentially we might need some pflash device exposed as storage for SVSM to use. Would such a dependancy be something expressed in the IGVM file, or would it be knowledge that is out of band ?

Finally, if we think of the IGVM file as simply yet another firmware file format, then it raises of question of integration into the QEMU firmware descriptors.

Right now when defining a guest in libvirt if you can say 'type=bios' or 'type=uefi', and libvirt consults the firmware descriptors to find the binary to use.

If the OS distro provides IGVM files instead of traditional raw OVMF binaries for SEV/TDX/etc, then from libvirt's POV I think having this expressed in the firmware descriptors is highly desirable.

With regards,
Daniel

Signature · 3 lines
|: 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 :|
#28 lore raw

Re: [PATCH 9/9] docs/system: Add documentation on support for IGVM

Quoted 29 lines · Daniel P. Berrangé
Quoted 22 lines · Roy Hopkins

IGVM support has been implemented for Confidential Guests that support AMD SEV and AMD SEV-ES. Add some documentation that gives some background on the IGVM format and how to use it to configure a confidential guest.

---
 docs/system/igvm.rst  | 58 +++++++++++++++++++++++++++++++++++++++++++ docs/system/index.rst |  1 +

 2 files changed, 59 insertions(+)

 create mode 100644 docs/system/igvm.rst

docs/system/igvm.rst new file +4 -0
+Firmware Images with IGVM
+-------------------------
+
+When an IGVM filename is specified for a Confidential Guest Support object

it
+overrides the default handling of system firmware: the firmware image, such as

(quoted diff) +2 -0
+an OVMF binary should be contained as a payload of the IGVM file and not
+provided as a flash drive. The default QEMU firmware is not automatically
Quoted 2 lines

mapped
+into guest memory.

IIUC, in future the IGVM file could contain both the OVMF and SVSM binaries ?

Quoted 7 lines

I'm also wondering if there can be dependancies between the IGVM file and the broader QEMU configuration ?  eg if SVSM gains suupport for data persistence, potentially we might need some pflash device exposed as storage for SVSM to use. Would such a dependancy be something expressed in the IGVM file, or would it be knowledge that is out of band ?

Yes, the IGVM file can indeed contain both OVMF and SVSM binaries. In fact, that is exactly what we are doing with the COCONUT-SVSM project. See [1] for the IGVM builder we use to package OVMF, bootloader components and the SVSM ELF binary.

Data persistence is something that is definitely going to be needed in the SVSM. At present, this cannot be configured using any of the directives in the IGVM specification but instead requires QEMU to be configured correctly to support the application embedded within the IGVM file itself. You could however populate metadata pages using IGVM that describe the storage that is _expected_ to be present, and validate that within the firmware itself.

The real value from IGVM comes from the ability to describe the initial memory and initial CPU state which all forms part of the launch measurement and initial boot procedure, allowing the expected launch measurement to be calculated from a single IGVM file for multiple virtualisation stacks or configurations. Thus, most of the directives in the IGVM file directly have an effect on the launch measurement. I'm not sure configuring a storage device or other hardware configuration fits well with this.

Quoted 4 lines

Finally, if we think of the IGVM file as simply yet another firmware file format, then it raises of question of integration into the QEMU firmware descriptors.

Quoted 8 lines

Right now when defining a guest in libvirt if you can say 'type=bios' or 'type=uefi', and libvirt consults the firmware descriptors to find the binary to use.

If the OS distro provides IGVM files instead of traditional raw OVMF binaries for SEV/TDX/etc, then from libvirt's POV I think having this expressed in the firmware descriptors is highly desirable.

Whether IGVM is just another firmware file format or not, it certainly is used mutually exclusively with other firmware files. Integration with firmware descriptors does seem to make sense.

One further question if this is the case, would we want to switch from specifying an "igvm-file" as a parameter on the "ConfidentialGuestSupport" object to providing the file using the "-bios" parameter, or maybe even a dedicated "-igvm" parameter?

Quoted 3 lines

With regards,
Daniel

[1] COCONUT IGVM builder:
https://github.com/coconut-svsm/svsm/tree/main/igvmbuilder

Regards,
Roy

#29 lore raw

Re: [PATCH 9/9] docs/system: Add documentation on support for IGVM

Quoted 48 lines · Roy Hopkins
Quoted 29 lines · Daniel P. Berrangé
Quoted 22 lines · Roy Hopkins

IGVM support has been implemented for Confidential Guests that support AMD SEV and AMD SEV-ES. Add some documentation that gives some background on the IGVM format and how to use it to configure a confidential guest.

---
 docs/system/igvm.rst  | 58 +++++++++++++++++++++++++++++++++++++++++++ docs/system/index.rst |  1 +

 2 files changed, 59 insertions(+)

 create mode 100644 docs/system/igvm.rst

docs/system/igvm.rst new file +4 -0
+Firmware Images with IGVM
+-------------------------
+
+When an IGVM filename is specified for a Confidential Guest Support object

it
+overrides the default handling of system firmware: the firmware image, such as

(quoted diff) +2 -0
+an OVMF binary should be contained as a payload of the IGVM file and not
+provided as a flash drive. The default QEMU firmware is not automatically
Quoted 2 lines

mapped
+into guest memory.

IIUC, in future the IGVM file could contain both the OVMF and SVSM binaries ?

Quoted 7 lines

I'm also wondering if there can be dependancies between the IGVM file and the broader QEMU configuration ?  eg if SVSM gains suupport for data persistence, potentially we might need some pflash device exposed as storage for SVSM to use. Would such a dependancy be something expressed in the IGVM file, or would it be knowledge that is out of band ?

Yes, the IGVM file can indeed contain both OVMF and SVSM binaries. In fact, that is exactly what we are doing with the COCONUT-SVSM project. See [1] for the IGVM builder we use to package OVMF, bootloader components and the SVSM ELF binary.

Data persistence is something that is definitely going to be needed in the SVSM. At present, this cannot be configured using any of the directives in the IGVM specification but instead requires QEMU to be configured correctly to support the application embedded within the IGVM file itself. You could however populate metadata pages using IGVM that describe the storage that is _expected_ to be present, and validate that within the firmware itself.

Quoted 7 lines

The real value from IGVM comes from the ability to describe the initial memory and initial CPU state which all forms part of the launch measurement and initial boot procedure, allowing the expected launch measurement to be calculated from a single IGVM file for multiple virtualisation stacks or configurations. Thus, most of the directives in the IGVM file directly have an effect on the launch measurement. I'm not sure configuring a storage device or other hardware configuration fits well with this.

Yeah, I can understand if IGVM scope should be limited to just memory and CPU setup.

If we use the firmeware descriptor files, we could define capabilities in that to express a need for a particular type of persistent storage to back the vTPM. So having this info in IGVM files isn't critical.

Quoted 13 lines
Quoted 4 lines

Finally, if we think of the IGVM file as simply yet another firmware file format, then it raises of question of integration into the QEMU firmware descriptors.

Quoted 8 lines

Right now when defining a guest in libvirt if you can say 'type=bios' or 'type=uefi', and libvirt consults the firmware descriptors to find the binary to use.

If the OS distro provides IGVM files instead of traditional raw OVMF binaries for SEV/TDX/etc, then from libvirt's POV I think having this expressed in the firmware descriptors is highly desirable.

Quoted 8 lines

Whether IGVM is just another firmware file format or not, it certainly is used mutually exclusively with other firmware files. Integration with firmware descriptors does seem to make sense.

One further question if this is the case, would we want to switch from specifying an "igvm-file" as a parameter on the "ConfidentialGuestSupport" object to providing the file using the "-bios" parameter, or maybe even a dedicated "-igvm" parameter?

If the IGVM format is flexible enough that it could be used for any VM type, even non-confidential VMs, then having its config be separate from ConfidentialGuestSUpport would make sense. If it is fundamentally tied to CVMs, then just a property is fine I guess.

Probably best to stay away from -bios, to avoid overloading new semantics onto a long standing argument.

With regards,
Daniel

Signature · 3 lines
|: 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 :|
#30 lore raw

Re: [PATCH 9/9] docs/system: Add documentation on support for IGVM

Quoted 72 lines · Daniel P. Berrangé
Quoted 59 lines · Roy Hopkins
Quoted 33 lines · Daniel P. Berrangé
Quoted 20 lines · Roy Hopkins

IGVM support has been implemented for Confidential Guests that support AMD SEV and AMD SEV-ES. Add some documentation that gives some background on the IGVM format and how to use it to configure a confidential guest.

---
 docs/system/igvm.rst  | 58 +++++++++++++++++++++++++++++++++++++++++++ docs/system/index.rst |  1 +

 2 files changed, 59 insertions(+)

 create mode 100644 docs/system/igvm.rst

docs/system/igvm.rst new file +4 -0
+Firmware Images with IGVM
+-------------------------
+
+When an IGVM filename is specified for a Confidential Guest Support

object
it
+overrides the default handling of system firmware: the firmware image,

Quoted 8 lines

such
as
+an OVMF binary should be contained as a payload of the IGVM file and not
+provided as a flash drive. The default QEMU firmware is not automatically
mapped
+into guest memory.

IIUC, in future the IGVM file could contain both the OVMF and SVSM binaries ?

Quoted 7 lines

I'm also wondering if there can be dependancies between the IGVM file and the broader QEMU configuration ?  eg if SVSM gains suupport for data persistence, potentially we might need some pflash device exposed as storage for SVSM to use. Would such a dependancy be something expressed in the IGVM file, or would it be knowledge that is out of band ?

Yes, the IGVM file can indeed contain both OVMF and SVSM binaries. In fact, that
is exactly what we are doing with the COCONUT-SVSM project. See [1] for the IGVM
builder we use to package OVMF, bootloader components and the SVSM ELF binary.

Data persistence is something that is definitely going to be needed in the SVSM.
At present, this cannot be configured using any of the directives in the IGVM
specification but instead requires QEMU to be configured correctly to support
the application embedded within the IGVM file itself. You could however populate
metadata pages using IGVM that describe the storage that is _expected_ to be present, and validate that within the firmware itself.

Quoted 11 lines

The real value from IGVM comes from the ability to describe the initial memory
and initial CPU state which all forms part of the launch measurement and initial
boot procedure, allowing the expected launch measurement to be calculated from a
single IGVM file for multiple virtualisation stacks or configurations. Thus, most of the directives in the IGVM file directly have an effect on the launch
measurement. I'm not sure configuring a storage device or other hardware configuration fits well with this.

Quoted 6 lines

Yeah, I can understand if IGVM scope should be limited to just memory and CPU setup.

If we use the firmeware descriptor files, we could define capabilities in that to express a need for a particular type of persistent storage to back the vTPM. So having this info in IGVM files isn't critical.

I'll need to look into firmware descriptor files as I'm unfamiliar with how they work. Would I need to make any additions to this patch series to support this in QEMU? Or is this all handled by libvirt?

Quoted 29 lines
Quoted 19 lines
Quoted 5 lines

Finally, if we think of the IGVM file as simply yet another firmware file format, then it raises of question of integration into the QEMU firmware descriptors.

Quoted 8 lines

Right now when defining a guest in libvirt if you can say 'type=bios' or 'type=uefi', and libvirt consults the firmware descriptors to find the binary to use.

If the OS distro provides IGVM files instead of traditional raw OVMF binaries for SEV/TDX/etc, then from libvirt's POV I think having this expressed in the firmware descriptors is highly desirable.

Whether IGVM is just another firmware file format or not, it certainly is used
mutually exclusively with other firmware files. Integration with firmware descriptors does seem to make sense.

Quoted 4 lines

One further question if this is the case, would we want to switch from specifying an "igvm-file" as a parameter on the "ConfidentialGuestSupport" object to providing the file using the "-bios" parameter, or maybe even a dedicated "-igvm" parameter?

If the IGVM format is flexible enough that it could be used for any VM type, even non-confidential VMs, then having its config be separate from ConfidentialGuestSUpport would make sense. If it is fundamentally tied to CVMs, then just a property is fine I guess.

Quoted 5 lines

Probably best to stay away from -bios, to avoid overloading new semantics onto a long standing argument.

With regards,
Daniel

Currently, the IGVM specification only contains support for confidential platforms. It could theoretically be used for non-confidential platforms but that would require changes to the IGVM specification itself to support this. I don't think it makes sense to extend this to non-confidential VMs until the specification supports this, so I'll leave it as a property of ConfidentialGuestSupport.

Regards,
Roy

#31 lore raw

Re: [PATCH 9/9] docs/system: Add documentation on support for IGVM

Quoted 76 lines · Roy Hopkins
Quoted 75 lines · Daniel P. Berrangé
Quoted 59 lines · Roy Hopkins
Quoted 33 lines · Daniel P. Berrangé
Quoted 20 lines · Roy Hopkins

IGVM support has been implemented for Confidential Guests that support AMD SEV and AMD SEV-ES. Add some documentation that gives some background on the IGVM format and how to use it to configure a confidential guest.

---
 docs/system/igvm.rst  | 58 +++++++++++++++++++++++++++++++++++++++++++ docs/system/index.rst |  1 +

 2 files changed, 59 insertions(+)

 create mode 100644 docs/system/igvm.rst

docs/system/igvm.rst new file +4 -0
+Firmware Images with IGVM
+-------------------------
+
+When an IGVM filename is specified for a Confidential Guest Support

object
it
+overrides the default handling of system firmware: the firmware image,

Quoted 8 lines

such
as
+an OVMF binary should be contained as a payload of the IGVM file and not
+provided as a flash drive. The default QEMU firmware is not automatically
mapped
+into guest memory.

IIUC, in future the IGVM file could contain both the OVMF and SVSM binaries ?

Quoted 7 lines

I'm also wondering if there can be dependancies between the IGVM file and the broader QEMU configuration ?  eg if SVSM gains suupport for data persistence, potentially we might need some pflash device exposed as storage for SVSM to use. Would such a dependancy be something expressed in the IGVM file, or would it be knowledge that is out of band ?

Yes, the IGVM file can indeed contain both OVMF and SVSM binaries. In fact, that
is exactly what we are doing with the COCONUT-SVSM project. See [1] for the IGVM
builder we use to package OVMF, bootloader components and the SVSM ELF binary.

Data persistence is something that is definitely going to be needed in the SVSM.
At present, this cannot be configured using any of the directives in the IGVM
specification but instead requires QEMU to be configured correctly to support
the application embedded within the IGVM file itself. You could however populate
metadata pages using IGVM that describe the storage that is _expected_ to be present, and validate that within the firmware itself.

Quoted 11 lines

The real value from IGVM comes from the ability to describe the initial memory
and initial CPU state which all forms part of the launch measurement and initial
boot procedure, allowing the expected launch measurement to be calculated from a
single IGVM file for multiple virtualisation stacks or configurations. Thus, most of the directives in the IGVM file directly have an effect on the launch
measurement. I'm not sure configuring a storage device or other hardware configuration fits well with this.

Yeah, I can understand if IGVM scope should be limited to just memory and CPU setup.

Quoted 7 lines
Quoted 3 lines

If we use the firmeware descriptor files, we could define capabilities in that to express a need for a particular type of persistent storage to back the vTPM. So having this info in IGVM files isn't critical.

I'll need to look into firmware descriptor files as I'm unfamiliar with how they work. Would I need to make any additions to this patch series to support this in QEMU? Or is this all handled by libvirt?

Probably little more than change docs/interop/firmware.json to add 'igvm' as a FirmwareDevice option to indicate this is a new way of exposing it to the guest.

At some point we might also want to expand FirmwareFeature eg to report a "vtpm" feature.

Quoted 18 lines
Quoted 10 lines
Quoted 9 lines

Whether IGVM is just another firmware file format or not, it certainly is used
mutually exclusively with other firmware files. Integration with firmware descriptors does seem to make sense.

One further question if this is the case, would we want to switch from specifying an "igvm-file" as a parameter on the "ConfidentialGuestSupport" object to providing the file using the "-bios" parameter, or maybe even a dedicated "-igvm" parameter?

Quoted 7 lines

If the IGVM format is flexible enough that it could be used for any VM type, even non-confidential VMs, then having its config be separate from ConfidentialGuestSUpport would make sense. If it is fundamentally tied to CVMs, then just a property is fine I guess.

Probably best to stay away from -bios, to avoid overloading new semantics onto a long standing argument.

Quoted 6 lines

Currently, the IGVM specification only contains support for confidential platforms. It could theoretically be used for non-confidential platforms but that would require changes to the IGVM specification itself to support this. I don't think it makes sense to extend this to non-confidential VMs until the specification supports this, so I'll leave it as a property of ConfidentialGuestSupport.

Sounds good.

With regards,
Daniel

Signature · 3 lines
|: 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 :|
#32 lore raw
In reply to Roy Hopkins

Hi Roy,
thanks for this series!

Quoted 9 lines · Roy Hopkins

Hi everyone,

This initial patch series submission adds the capability to configure confidential guests using files that conform to the Independent Guest Virtual Machine (IGVM) file format. The series is based on the master branch commit 1b330da. Alternatively, the series is available here:
https://github.com/roy-hopkins/qemu/tree/igvm_master_v1

I look forward to welcoming your comments!

I saw that the series has been a posted for a while and maybe you're going to send v2, so I'll bring back some little things that I saw, but I didn't do yet a deep review:

- We use "Isolated Guest Virtual Machine" or "Independent Guest Virtual Machine", are they interchangeable for IGVM?

- ./scripts/checkpatch.pl --codespell reported some warnings:

5/9 Checking commit 81f60e5cdd01 (i386/pc: Process IGVM file during PC initialization if present)
WARNING: 'initalization' may be misspelled - perhaps 'initialization'?
#15:
initalization of the target.
^^^^^^^^^^^^^

9/9 Checking commit 66745c0bb940 (docs/system: Add documentation on support for IGVM)

WARNING: 'encaspulate' may be misspelled - perhaps 'encapsulate'? #27: FILE: docs/system/igvm.rst:4:
+IGVM files are designed to encaspulate all the information required to launch a

                            ^^^^^^^^^^^

Thanks,
Stefano

Quoted 105 lines

Why do we need Independent Guest Virtual Machine (IGVM) files?
==============================================================

IGVM files describe, using a set of directives, the memory layout and initial configuration of a guest that supports isolation technologies such as AMD SEV-SNP and Intel TDX. By encapsulating all of this information in a single configuration file and applying the directives in the order they are specified when the guest is initialized, it becomes straightforward to pre-calculate the cryptographic measurement of the guest initial state, thus aiding in remote attestation processes.

IGVM files can also be used to configure non-standard guest memory layouts, payloads or startup configurations. A good example of this is to use IGVM to deploy and configure an SVSM module in the guest which supports running at multiple VMPLs. The SVSM can be configured to start directly into 32-bit or 64-bit code. This patch series was developed with this purpose in mind to support the COCONUT-SVSM project:
https://github.com/coconut-svsm/svsm

More information and background on the IGVM file format can be found on the project page at:
https://github.com/microsoft/igvm

What this patch series introduces
=================================

This series adds a build-time configuration option (--enable-igvm) to add support for launching a guest using an IGVM file. It extends the current ConfidentialGuestSupport object to allow an IGVM filename to be specified.

The directives in the IGVM file are parsed and the confidential guest is configured through new virtual methods added to the ConfidentialGuestSupport object. These virtual functions have been implemented for AMD SEV and AMD SEV-ES.

Many of the IGVM directives require capabilities that are not supported in SEV and SEV-ES, so support for IGVM directives will need to be considered when support for SEV-SNP, TDX or other technologies is introduced to QEMU. Any directive that is not currently supported results in an error report.

Dependencies
============

In order to enable IGVM support, you will need the IGVM library installed. Instructions on building and installing it can be found here:
https://github.com/microsoft/igvm/tree/main/igvm_c

As mentioned above, this series was developed as part of the effort for COCONUT-SVSM. COCONUT-SVSM requires support for AMD SEV-SNP which is not available in current QEMU. Therefore this series has also been applied on top of the AMD SEV-SNP branch (https://github.com/AMDESE/qemu/tree/snp-v3-wip). You can find that version of the series here:
https://github.com/roy-hopkins/qemu/commits/snp-v3-wip-igvm_v2/

Generating IGVM files
=====================

To try this out you will need to generate an IGVM file that is compatible with the SEV platform you are testing on. I've created a tool that can create a simple IGVM file that packages an OVMF binary for AMD SEV or AMD SEV-ES. The tool is available here:
https://github.com/roy-hopkins/buildigvm

I have tested this on an AMD EPYC Genoa system configured to support SEV. Both SEV and SEV-ES have been tested using IGVM files generated using the buildigvm tool. The SEV-SNP alternative patch set has also been tested using COCONUT-SVSM.

Roy Hopkins (9):
meson: Add optional dependency on IGVM library backends/confidential-guest-support: Add IGVM file parameter backends/confidential-guest-support: Add functions to support IGVM backends/igvm: Implement parsing and processing of IGVM files i386/pc: Process IGVM file during PC initialization if present i386/pc: Skip initialization of system FW when using IGVM i386/sev: Refactor setting of reset vector and initial CPU state i386/sev: Implement ConfidentialGuestSupport functions for SEV docs/system: Add documentation on support for IGVM

backends/confidential-guest-support.c     |  69 +++
backends/igvm.c                           | 718 ++++++++++++++++++++++
backends/meson.build                      |   4 +
docs/system/igvm.rst                      |  58 ++
docs/system/index.rst                     |   1 +
hw/i386/pc.c                              |  12 +-
hw/i386/pc_piix.c                         |   4 +
hw/i386/pc_q35.c                          |   4 +
include/exec/confidential-guest-support.h | 107 ++++
include/exec/igvm.h                       |  35 ++
meson.build                               |   8 +
meson_options.txt                         |   2 +
qapi/qom.json                             |  13 +
qemu-options.hx                           |   8 +-
scripts/meson-buildoptions.sh             |   3 +
target/i386/sev.c                         | 365 ++++++++++-
target/i386/sev.h                         | 110 ++++
17 files changed, 1489 insertions(+), 32 deletions(-)

create mode 100644 backends/igvm.c
create mode 100644 docs/system/igvm.rst
create mode 100644 include/exec/igvm.h

--
2.43.0

#33 lore raw
Quoted 34 lines · Stefano Garzarella

Hi Roy,
thanks for this series!

Quoted 10 lines · Roy Hopkins

Hi everyone,

This initial patch series submission adds the capability to configure confidential guests using files that conform to the Independent Guest Virtual
Machine (IGVM) file format. The series is based on the master branch commit 1b330da. Alternatively, the series is available here:
https://github.com/roy-hopkins/qemu/tree/igvm_master_v1

I look forward to welcoming your comments!

I saw that the series has been a posted for a while and maybe you're going to send v2, so I'll bring back some little things that I saw, but I didn't do yet a deep review:

- We use "Isolated Guest Virtual Machine" or "Independent Guest Virtual Machine", are they interchangeable for IGVM?

- ./scripts/checkpatch.pl --codespell reported some warnings:

5/9 Checking commit 81f60e5cdd01 (i386/pc: Process IGVM file during PC initialization if present)
WARNING: 'initalization' may be misspelled - perhaps 'initialization'? #15:
     initalization of the target.
     ^^^^^^^^^^^^^

9/9 Checking commit 66745c0bb940 (docs/system: Add documentation on support for IGVM)

Quoted 9 lines

WARNING: 'encaspulate' may be misspelled - perhaps 'encapsulate'? #27: FILE: docs/system/igvm.rst:4:
+IGVM files are designed to encaspulate all the information required to launch a
                             ^^^^^^^^^^^

Thanks,
Stefano

Thanks for the initial review Stefano. I'll be posting a v2 shortly and will address your comments.

Regards,
Roy

[snip]

Quoted 1 line
#34 lore raw
In reply to Roy Hopkins
Quoted 77 lines · Roy Hopkins

Hi everyone,

This initial patch series submission adds the capability to configure confidential guests using files that conform to the Independent Guest Virtual Machine (IGVM) file format. The series is based on the master branch commit 1b330da. Alternatively, the series is available here:
https://github.com/roy-hopkins/qemu/tree/igvm_master_v1

I look forward to welcoming your comments!

Why do we need Independent Guest Virtual Machine (IGVM) files?
==============================================================

IGVM files describe, using a set of directives, the memory layout and initial configuration of a guest that supports isolation technologies such as AMD SEV-SNP and Intel TDX. By encapsulating all of this information in a single configuration file and applying the directives in the order they are specified when the guest is initialized, it becomes straightforward to pre-calculate the cryptographic measurement of the guest initial state, thus aiding in remote attestation processes.

IGVM files can also be used to configure non-standard guest memory layouts, payloads or startup configurations. A good example of this is to use IGVM to deploy and configure an SVSM module in the guest which supports running at multiple VMPLs. The SVSM can be configured to start directly into 32-bit or 64-bit code. This patch series was developed with this purpose in mind to support the COCONUT-SVSM project:
https://github.com/coconut-svsm/svsm

More information and background on the IGVM file format can be found on the project page at:
https://github.com/microsoft/igvm

What this patch series introduces
=================================

This series adds a build-time configuration option (--enable-igvm) to add support for launching a guest using an IGVM file. It extends the current ConfidentialGuestSupport object to allow an IGVM filename to be specified.

The directives in the IGVM file are parsed and the confidential guest is configured through new virtual methods added to the ConfidentialGuestSupport object. These virtual functions have been implemented for AMD SEV and AMD SEV-ES.

Many of the IGVM directives require capabilities that are not supported in SEV and SEV-ES, so support for IGVM directives will need to be considered when support for SEV-SNP, TDX or other technologies is introduced to QEMU. Any directive that is not currently supported results in an error report.

Dependencies
============

In order to enable IGVM support, you will need the IGVM library installed. Instructions on building and installing it can be found here:
https://github.com/microsoft/igvm/tree/main/igvm_c

As mentioned above, this series was developed as part of the effort for COCONUT-SVSM. COCONUT-SVSM requires support for AMD SEV-SNP which is not available in current QEMU. Therefore this series has also been applied on top of the AMD SEV-SNP branch (https://github.com/AMDESE/qemu/tree/snp-v3-wip). You can find that version of the series here:
https://github.com/roy-hopkins/qemu/commits/snp-v3-wip-igvm_v2/

Generating IGVM files
=====================

To try this out you will need to generate an IGVM file that is compatible with the SEV platform you are testing on. I've created a tool that can create a simple IGVM file that packages an OVMF binary for AMD SEV or AMD SEV-ES. The tool is available here:
https://github.com/roy-hopkins/buildigvm

Quoted 3 lines

I have tested this on an AMD EPYC Genoa system configured to support SEV. Both SEV and SEV-ES have been tested using IGVM files generated using the buildigvm tool. The SEV-SNP alternative patch set has also been tested using COCONUT-SVSM.

Could you please also CC me in this patchset please? Thanks.

Quoted 39 lines

Roy Hopkins (9):
meson: Add optional dependency on IGVM library backends/confidential-guest-support: Add IGVM file parameter backends/confidential-guest-support: Add functions to support IGVM backends/igvm: Implement parsing and processing of IGVM files i386/pc: Process IGVM file during PC initialization if present i386/pc: Skip initialization of system FW when using IGVM i386/sev: Refactor setting of reset vector and initial CPU state i386/sev: Implement ConfidentialGuestSupport functions for SEV docs/system: Add documentation on support for IGVM

backends/confidential-guest-support.c     |  69 +++
backends/igvm.c                           | 718 ++++++++++++++++++++++
backends/meson.build                      |   4 +
docs/system/igvm.rst                      |  58 ++
docs/system/index.rst                     |   1 +
hw/i386/pc.c                              |  12 +-
hw/i386/pc_piix.c                         |   4 +
hw/i386/pc_q35.c                          |   4 +
include/exec/confidential-guest-support.h | 107 ++++
include/exec/igvm.h                       |  35 ++
meson.build                               |   8 +
meson_options.txt                         |   2 +
qapi/qom.json                             |  13 +
qemu-options.hx                           |   8 +-
scripts/meson-buildoptions.sh             |   3 +
target/i386/sev.c                         | 365 ++++++++++-
target/i386/sev.h                         | 110 ++++
17 files changed, 1489 insertions(+), 32 deletions(-)

create mode 100644 backends/igvm.c
create mode 100644 docs/system/igvm.rst
create mode 100644 include/exec/igvm.h

--
2.43.0