Introduce support for IGVM files
v7 cover Introduce support for IGVM files
Here is v7 of the set of patches to add support for IGVM files to QEMU. This is based on commit 40efe733e10cc00e4fb4f9f5790a28e744e63c62 of qemu.
Firstly, apologies for the amount of time between the last version and this one. I moved roles to a different company and, although I always planned to see this patch series to completion, it took a while before I found time to setup a development environment and be in a position to send a new version. I will continue this series using a personal email address for now, hence the change to the author and signed-off-by emails.
The only changes in this version are to rebase on the current master branch and update commit metadata, including Signed-Off-By and Author emails for my replacement email address, and to include the final Reviewed-By that were added in the last review. There were no requested changes on the previous version [1] so I believe this series is ready to merge.
As always, thanks to those that have been following along, reviewing and testing this series. This v7 patch series is also available on github: [2]
For testing IGVM support in QEMU you need to generate an IGVM file that is configured for the platform you want to launch. You can use the buildigvm test tool [3] to allow generation of IGVM files for all currently supported platforms. Patch 11/17 contains information on how to generate an IGVM file using this tool.
Changes in v7:
* Update version numbers for IGVM support to 10.0
* Add Reviewed-by to relevant commits.
* Update Author email and sign-offs to my new email address
Patch summary:
1-11: Add support and documentation for processing IGVM files for SEV, SEV-ES, SEV-SNP and native platforms.
12-15: Processing of policy and SEV-SNP ID_BLOCK from IGVM file.
16: Add pre-processing of IGVM file to support synchronization of 'SEV_FEATURES' from IGVM VMSA to KVM.
[1] Link to v6:
https://lore.kernel.org/qemu-devel/[email protected]/
[2] v7 patches also available here:
https://github.com/roy-hopkins/qemu/tree/igvm_master_v7
[3] buildigvm tool v0.2.0
https://github.com/roy-hopkins/buildigvm/releases/tag/v0.2.0
Roy Hopkins (16):
meson: Add optional dependency on IGVM library backends/confidential-guest-support: Add functions to support IGVM backends/igvm: Add IGVM loader and configuration
hw/i386: Add igvm-cfg object and processing for IGVM files i386/pc_sysfw: Ensure sysfw flash configuration does not conflict with
IGVM
sev: Update launch_update_data functions to use Error handling target/i386: Allow setting of R_LDTR and R_TR with
cpu_x86_load_seg_cache()
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 docs/interop/firmware.json: Add igvm to FirmwareDevice backends/confidential-guest-support: Add set_guest_policy() function backends/igvm: Process initialization sections in IGVM file backends/igvm: Handle policy for SEV guests
i386/sev: Add implementation of CGS set_guest_policy()
sev: Provide sev_features flags from IGVM VMSA to KVM_SEV_INIT2
backends/confidential-guest-support.c | 43 + backends/igvm-cfg.c | 52 ++ backends/igvm.c | 967 ++++++++++++++++++++ backends/igvm.h | 23 + backends/meson.build | 5 + docs/interop/firmware.json | 30 +- docs/system/i386/amd-memory-encryption.rst | 2 + docs/system/igvm.rst | 173 ++++ docs/system/index.rst | 1 + hw/i386/pc.c | 12 + hw/i386/pc_piix.c | 10 + hw/i386/pc_q35.c | 10 + hw/i386/pc_sysfw.c | 31 +- include/hw/i386/x86.h | 3 + include/system/confidential-guest-support.h | 88 ++ include/system/igvm-cfg.h | 47 + meson.build | 8 + meson_options.txt | 2 + qapi/qom.json | 17 + qemu-options.hx | 28 + scripts/meson-buildoptions.sh | 3 + target/i386/cpu.h | 9 +- target/i386/sev.c | 850 +++++++++++++++-- target/i386/sev.h | 124 +++ 24 files changed, 2454 insertions(+), 84 deletions(-) create mode 100644 backends/igvm-cfg.c create mode 100644 backends/igvm.c create mode 100644 backends/igvm.h create mode 100644 docs/system/igvm.rst create mode 100644 include/system/igvm-cfg.h
Signature · 1 line
2.43.0
PATCH v7 1/16 meson: Add optional dependency on IGVM library
The IGVM library allows Independent 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
@@ -32,6 +32,9 @@ 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 system_ss.add(when: 'CONFIG_SPDM_SOCKET', if_true: files('spdm-socket.c'))
meson.build +8 -0
@@ -1404,6 +1404,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', version: '>= 0.3.0', + method: 'pkg-config', + required: get_option('igvm')) +endif mpathlibs = [libudev] mpathpersist = not_found
@@ -2579,6 +2585,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']
@@ -4818,6 +4825,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
@@ -115,6 +115,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
@@ -132,6 +132,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'
@@ -350,6 +351,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
PATCH v7 2/16 backends/confidential-guest-support: Add functions to support IGVM
In reply to Roy HopkinsIn 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 an error when they are called. Targets that support ConfidentalGuestSupport should override these implementations.
--- backends/confidential-guest-support.c | 31 ++++++++++ include/system/confidential-guest-support.h | 67 +++++++++++++++++++++ 2 files changed, 98 insertions(+)
backends/confidential-guest-support.c +31 -0
@@ -14,14 +14,45 @@#include "qemu/osdep.h" #include "system/confidential-guest-support.h" +#include "qapi/error.h" OBJECT_DEFINE_ABSTRACT_TYPE (ConfidentialGuestSupport, confidential_guest_support,CONFIDENTIAL_GUEST_SUPPORT , OBJECT) +static bool check_support (ConfidentialGuestPlatformType platform, +uint16_t platform_version,uint8_t highest_vtl, +uint64_t shared_gpa_boundary) +{ +/* Default: no support. */ +return false; +} + +static int set_guest_state (hwaddr gpa,uint8_t *ptr,uint64_t len, +ConfidentialGuestPageType memory_type, +uint16_t cpu_index,Error **errp) +{ +error_setg (errp, +"Setting confidential guest state is not supported for this platform" ); +return -1 ; +} + +static int get_mem_map_entry (int index,ConfidentialGuestMemoryMapEntry *entry, +Error **errp) +{ +error_setg ( + errp, +"Obtaining the confidential guest memory map is not supported for this platform" ); +return -1 ; +} +static void confidential_guest_support_class_init (ObjectClass *oc,void *data) { +ConfidentialGuestSupportClass *cgsc =CONFIDENTIAL_GUEST_SUPPORT_CLASS (oc); + cgsc->check_support = check_support; + cgsc->set_guest_state = set_guest_state; + cgsc->get_mem_map_entry = get_mem_map_entry; }static void confidential_guest_support_init (Object *obj)
include/system/confidential-guest-support.h +67 -0
85 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v7 3/16 backends/igvm: Add IGVM loader and configuration
In reply to Roy HopkinsAdds an IGVM loader to QEMU which processes a given IGVM file and applies the directives within the file to the current guest configuration.
The IGVM loader can be used to configure both confidential and non-confidential guests. For confidential guests, the ConfidentialGuestSupport object for the system is used to encrypt memory, apply the initial CPU state and perform other confidential guest operations.
The loader is configured via a new IgvmCfg QOM object which allows the user to provide a path to the IGVM file to process.
--- backends/igvm-cfg.c | 52 +++ backends/igvm.c | 808 ++++++++++++++++++++++++++++++++++++++ backends/igvm.h | 23 ++ backends/meson.build | 2 + include/system/igvm-cfg.h | 47 +++ qapi/qom.json | 17 + 6 files changed, 949 insertions(+) create mode 100644 backends/igvm-cfg.c create mode 100644 backends/igvm.c create mode 100644 backends/igvm.h create mode 100644 include/system/igvm-cfg.h
backends/igvm-cfg.c new file +52 -0
@@ -0,0 +1,52 @@ +/* +* QEMU IGVM interface +* +* 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" + +#include "system/igvm-cfg.h" +#include "igvm.h" +#include "qom/object_interfaces.h" + +static char *get_igvm (Object *obj,Error **errp) +{ +IgvmCfg *igvm =IGVM_CFG (obj); +return g_strdup (igvm->filename); +} + +static void set_igvm (Object *obj,const char *value,Error **errp) +{ +IgvmCfg *igvm =IGVM_CFG (obj); +g_free (igvm->filename); + igvm->filename =g_strdup (value); +} + +OBJECT_DEFINE_TYPE_WITH_INTERFACES (IgvmCfg, igvm_cfg,IGVM_CFG ,OBJECT , + { TYPE_USER_CREATABLE }, { NULL }) + +static void igvm_cfg_class_init (ObjectClass *oc,void *data) +{ +IgvmCfgClass *igvmc =IGVM_CFG_CLASS (oc); + +object_class_property_add_str (oc,"file" , get_igvm, set_igvm); +object_class_property_set_description (oc,"file" , +"Set the IGVM filename to use" ); + + igvmc->process = qigvm_process_file; +} + +static void igvm_cfg_init (Object *obj) +{ +} + +static void igvm_cfg_finalize (Object *obj) +{ +}
backends/igvm.c new file +808 -0
808 lines not on this page · raw message
backends/igvm.h new file +23 -0
@@ -0,0 +1,23 @@ +/* +* 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 BACKENDS_IGVM_H +#define BACKENDS_IGVM_H + +#include "system/confidential-guest-support.h" +#include "system/igvm-cfg.h" +#include "qapi/error.h" + +int qigvm_process_file (IgvmCfg *igvm,ConfidentialGuestSupport *cgs, +Error **errp); + +#endif
backends/meson.build +2 -0
@@ -34,6 +34,8 @@ 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-cfg.c'), igvm) + system_ss.add(files('igvm.c'), igvm) endif system_ss.add(when: 'CONFIG_SPDM_SOCKET', if_true: files('spdm-socket.c'))
include/system/igvm-cfg.h new file +47 -0
47 lines not on this page · raw message
qapi/qom.json +17 -0
@@ -932,6 +932,19 @@ 'data': { '*filename': 'str' }, 'if': 'CONFIG_POSIX' } +## +# @IgvmCfgProperties: +# +# Properties common to objects that handle IGVM files. +# +# @file: IGVM file to use to configure guest +# +# Since: 10.0 +## +{ 'struct': 'IgvmCfgProperties', + 'if': 'CONFIG_IGVM', + 'data': { 'file': 'str' } } + ## # @SevCommonProperties: #
@@ -1103,6 +1116,8 @@ 'filter-redirector', 'filter-replay', 'filter-rewriter', + { 'name': 'igvm-cfg', + 'if': 'CONFIG_IGVM' }, 'input-barrier', { 'name': 'input-linux', 'if': 'CONFIG_LINUX' },
@@ -1178,6 +1193,8 @@ 'filter-redirector': 'FilterRedirectorProperties', 'filter-replay': 'NetfilterProperties', 'filter-rewriter': 'FilterRewriterProperties', + 'igvm-cfg': { 'type': 'IgvmCfgProperties', + 'if': 'CONFIG_IGVM' }, 'input-barrier': 'InputBarrierProperties', 'input-linux': { 'type': 'InputLinuxProperties', 'if': 'CONFIG_LINUX' },
Signature · 1 line
2.43.0
Re: [PATCH v7 03/16] backends/igvm: Add IGVM loader and configuration
Quoted 2 lines · Roy Hopkins
The IGVM loader can be used to configure both confidential and non-confidential guests.
For non-confidential guests the initial register state is not loaded.
https://github.com/coconut-svsm/qemu/pull/17 has a somewhat hackish fix.
take care,
Gerd
Re: [PATCH v7 03/16] backends/igvm: Add IGVM loader and configuration
Quoted 7 lines · Gerd Hoffmann
Quoted 2 lines · Roy Hopkins
The IGVM loader can be used to configure both confidential and non-confidential guests.
For non-confidential guests the initial register state is not loaded.
https://github.com/coconut-svsm/qemu/pull/17 has a somewhat hackish fix.
Thanks for this. I've taken a look at how this can be implemented based on the current patch series but it would require some non-trivial changes seeing as the IGVM implementation is handled as part of the confidential-guest-support code.
I think it best to leave this series as-is and I'll author patches to add support for setting non-confidential CPU state after this series has merged.
Regards,
Roy
Quoted 3 lines
take care,
Gerd
Re: [PATCH v7 03/16] backends/igvm: Add IGVM loader and configuration
In reply to Roy HopkinsQuoted 13 lines · Roy Hopkins
Adds an IGVM loader to QEMU which processes a given IGVM file and applies the directives within the file to the current guest configuration.
The IGVM loader can be used to configure both confidential and non-confidential guests. For confidential guests, the ConfidentialGuestSupport object for the system is used to encrypt memory, apply the initial CPU state and perform other confidential guest operations.
The loader is configured via a new IgvmCfg QOM object which allows the user to provide a path to the IGVM file to process.
Quoted 7 lines
Signed-off-by Roy Hopkins <[email protected]>Reviewed-by Stefano Garzarella <[email protected]>--- backends/igvm-cfg.c | 52 +++ backends/igvm.c | 808 ++++++++++++++++++++++++++++++++++++++ backends/igvm.h | 23 ++
Just a general comment that could also be considered at a later point, after this was moved: Is "backends" really the right directory, and if so, would it make sense to start adding sub-directories? For example, all "hostmem" stuff could be moved into a subdirectory.
Would an "igvm" directory also be the right place for backends/confidential-guest-support.c, or would we be looking for something more abstract?
Signature · 3 lines
Cheers, David / dhildenb
PATCH v7 4/16 hw/i386: Add igvm-cfg object and processing for IGVM files
In reply to Roy HopkinsAn IGVM file contains configuration of guest state that should be applied during configuration of the guest, before the guest is started.
This patch allows the user to add an igvm-cfg object to an X86 machine configuration that allows an IGVM file to be configured that will be applied to the guest before it is started.
If an IGVM configuration is provided then the IGVM file is processed at the end of the board initialization, before the state transition to PHASE_MACHINE_INITIALIZED.
--- hw/i386/pc.c | 12 ++++++++++++ hw/i386/pc_piix.c | 10 ++++++++++ hw/i386/pc_q35.c | 10 ++++++++++ include/hw/i386/x86.h | 3 +++ qemu-options.hx | 28 ++++++++++++++++++++++++++++ 5 files changed, 63 insertions(+)
hw/i386/pc.c +12 -0
@@ -1852,6 +1852,18 @@ static void pc_machine_class_init(ObjectClass *oc, void *data)object_class_property_add_bool (oc,"fd-bootchk" , pc_machine_get_fd_bootchk, pc_machine_set_fd_bootchk); + +#if defined(CONFIG_IGVM ) +object_class_property_add_link (oc,"igvm-cfg" , +TYPE_IGVM_CFG , + offsetof(X86MachineState , igvm), + object_property_allow_set_link, +OBJ_PROP_LINK_STRONG ); +object_class_property_set_description (oc,"igvm-cfg" , +"Set IGVM configuration" ); +#endif + + }static const TypeInfo pc_machine_info = {
hw/i386/pc_piix.c +10 -0
@@ -360,6 +360,16 @@ static void pc_init1(MachineState *machine, const char *pci_type) x86_nvdimm_acpi_dsmio, x86ms->fw_cfg, OBJECT(pcms)); } + +#if defined(CONFIG_IGVM ) +/* Apply guest state from IGVM if supplied */ +if (x86ms->igvm) { +if (IGVM_CFG_GET_CLASS (x86ms->igvm) + ->process (x86ms->igvm, machine->cgs, &error_fatal) <0 ) { +g_assert_not_reached (); + } + } +#endif } typedef enum PCSouthBridgeOption {
hw/i386/pc_q35.c +10 -0
@@ -324,6 +324,16 @@ static void pc_q35_init(MachineState *machine) x86_nvdimm_acpi_dsmio, x86ms->fw_cfg, OBJECT(pcms)); } + +#if defined(CONFIG_IGVM ) +/* Apply guest state from IGVM if supplied */ +if (x86ms->igvm) { +if (IGVM_CFG_GET_CLASS (x86ms->igvm) + ->process (x86ms->igvm, machine->cgs, &error_fatal) <0 ) { +g_assert_not_reached (); + } + } +#endif }#define DEFINE_Q35_MACHINE (major, minor) \
include/hw/i386/x86.h +3 -0
@@ -25,6 +25,7 @@#include "hw/intc/ioapic.h" #include "hw/isa/isa.h" #include "qom/object.h" +#include "system/igvm-cfg.h" struct X86MachineClass {/*< private >*/
@@ -97,6 +98,8 @@ struct X86MachineState {* which means no limitation on the guest's bus locks. */ uint64_t bus_lock_ratelimit; + +IgvmCfg *igvm; };#define X86_MACHINE_SMM "smm"
qemu-options.hx +28 -0
@@ -5964,6 +5964,34 @@ SRST -machine ...,memory-encryption=sev0 \\ ..... + ``-object igvm-cfg,file=file`` + Create an IGVM configuration object that defines the initial state + of the guest using a file in that conforms to the Independent Guest + Virtual Machine (IGVM) file format. + + This is currently only supported by ``-machine q35`` and + ``-machine pc``. + + The ``file`` parameter is used to specify the IGVM file to load. + When provided, the IGVM file is used to populate the initial + memory of the virtual machine and, depending on the platform, can + define the initial processor state, memory map and parameters. + + The IGVM file is expected to contain the firmware for the virtual + machine, therefore an ``igvm-cfg`` object cannot be provided along + with other ways of specifying firmware, such as the ``-bios`` + parameter on x86 machines. + + e.g to launch a machine providing the firmware in an IGVM file + + .. parsed-literal:: + + # |qemu_system_x86| \\ + ...... \\ + -object igvm-cfg,id=igvm0,file=bios.igvm \\ + -machine ...,igvm-cfg=igvm0 \\ + ..... + ``-object authz-simple,id=id,identity=string`` Create an authorization object that will control access to network services.
Signature · 1 line
2.43.0
PATCH v7 5/16 i386/pc_sysfw: Ensure sysfw flash configuration does not conflict with IGVM
In reply to Roy HopkinsWhen using an IGVM file the configuration of the system firmware is defined by IGVM directives contained in the file. In this case the user should not configure any pflash devices.
This commit skips initialization of the ROM mode when pflash0 is not set then checks to ensure no pflash devices have been configured when using IGVM, exiting with an error message if this is not the case.
--- hw/i386/pc_sysfw.c | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-)
hw/i386/pc_sysfw.c +28 -3
@@ -219,7 +219,13 @@ void pc_system_firmware_init(PCMachineState *pcms,BlockBackend *pflash_blk[ARRAY_SIZE (pcms->flash)];if (!pcmc->pci_enabled) { -x86_bios_rom_init (X86_MACHINE (pcms),"bios.bin" , rom_memory, true); +/* +* If an IGVM file is specified then the firmware must be provided +* in the IGVM file. +*/ +if (!X86_MACHINE (pcms)->igvm) { +x86_bios_rom_init (X86_MACHINE (pcms),"bios.bin" , rom_memory, true); + }return ; }
@@ -239,8 +245,13 @@ void pc_system_firmware_init(PCMachineState *pcms, }if (!pflash_blk[0 ]) { -/* Machine property pflash0 not set, use ROM mode */ -x86_bios_rom_init (X86_MACHINE (pcms),"bios.bin" , rom_memory, false); +/* +* Machine property pflash0 not set, use ROM mode unless using IGVM, +* in which case the firmware must be provided by the IGVM file. +*/ +if (!X86_MACHINE (pcms)->igvm) { +x86_bios_rom_init (X86_MACHINE (pcms),"bios.bin" , rom_memory, false); + } }else {if (kvm_enabled () && !kvm_readonly_mem_enabled ()) {/*
@@ -256,6 +267,20 @@ void pc_system_firmware_init(PCMachineState *pcms, }pc_system_flash_cleanup_unused (pcms); + +/* +* The user should not have specified any pflash devices when using IGVM +* to configure the guest. +*/ +if (X86_MACHINE (pcms)->igvm) { +for (i =0 ; i <ARRAY_SIZE (pcms->flash); i++) { +if (pcms->flash[i]) { +error_report ("pflash devices cannot be configured when " +"using IGVM" ); +exit (1 ); + } + } + } }void x86_firmware_configure (hwaddr gpa,void *ptr,int size)
Signature · 1 line
2.43.0
PATCH v7 6/16 sev: Update launch_update_data functions to use Error handling
In reply to Roy HopkinsThe class function and implementations for updating launch data return a code in case of error. In some cases an error message is generated and in other cases, just the error return value is used.
This small refactor adds an 'Error **errp' parameter to all functions which consistently set an error condition if a non-zero value is returned.
--- target/i386/sev.c | 68 +++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 35 deletions(-)
target/i386/sev.c +33 -35
@@ -121,7 +121,8 @@ struct SevCommonStateClass { Error **errp);int (*launch_start)(SevCommonState *sev_common);void (*launch_finish)(SevCommonState *sev_common); -int (*launch_update_data)(SevCommonState *sev_common,hwaddr gpa,uint8_t *ptr,size_t len); +int (*launch_update_data)(SevCommonState *sev_common,hwaddr gpa, +uint8_t *ptr,size_t len,Error **errp);int (*kvm_init)(ConfidentialGuestSupport *cgs,Error **errp); };
@@ -977,9 +978,8 @@ sev_snp_mask_cpuid_features(X86ConfidentialGuest *cg, uint32_t feature, uint32_treturn value; } -static int -sev_launch_update_data (SevCommonState *sev_common,hwaddr gpa, -uint8_t *addr,size_t len) +static int sev_launch_update_data (SevCommonState *sev_common,hwaddr gpa, +uint8_t *addr,size_t len,Error **errp) {int ret, fw_error;struct kvm_sev_launch_update_data update;
@@ -994,8 +994,8 @@ sev_launch_update_data(SevCommonState *sev_common, hwaddr gpa, ret =sev_ioctl (sev_common->sev_fd,KVM_SEV_LAUNCH_UPDATE_DATA , &update, &fw_error);if (ret) { -error_report ("%s: LAUNCH_UPDATE ret=%d fw_error=%d '%s'" , - __func__, ret, fw_error,fw_error_to_str (fw_error)); +error_setg (errp,"%s: LAUNCH_UPDATE ret=%d fw_error=%d '%s'" , __func__, + ret, fw_error,fw_error_to_str (fw_error)); }return ret;
@@ -1123,8 +1123,8 @@ sev_launch_finish(SevCommonState *sev_common)migrate_add_blocker (&sev_mig_blocker, &error_fatal); } -static int -snp_launch_update_data (uint64_t gpa,void *hva,size_t len,int type) +static int snp_launch_update_data (uint64_t gpa,void *hva,size_t len, +int type,Error **errp) {SevLaunchUpdateData *data;
@@ -1139,23 +1139,21 @@ snp_launch_update_data(uint64_t gpa, void *hva, size_t len, int type)return 0 ; } -static int -sev_snp_launch_update_data (SevCommonState *sev_common,hwaddr gpa, -uint8_t *ptr,size_t len) +static int sev_snp_launch_update_data (SevCommonState *sev_common,hwaddr gpa, +uint8_t *ptr,size_t len,Error **errp) { -int ret =snp_launch_update_data (gpa, ptr, len, -KVM_SEV_SNP_PAGE_TYPE_NORMAL ); -return ret; +return snp_launch_update_data (gpa, ptr, len, +KVM_SEV_SNP_PAGE_TYPE_NORMAL , errp); }static int sev_snp_cpuid_info_fill (SnpCpuidInfo *snp_cpuid_info, -const KvmCpuidInfo *kvm_cpuid_info) +const KvmCpuidInfo *kvm_cpuid_info,Error **errp) {size_t i;if (kvm_cpuid_info->cpuid.nent >SNP_CPUID_FUNCTION_MAXCOUNT ) { -error_report ("SEV-SNP: CPUID entry count (%d) exceeds max (%d)" , +error_setg (errp,"SEV-SNP: CPUID entry count (%d) exceeds max (%d)" , kvm_cpuid_info->cpuid.nent,SNP_CPUID_FUNCTION_MAXCOUNT );return -1 ; }
@@ -1197,8 +1195,8 @@ sev_snp_cpuid_info_fill(SnpCpuidInfo *snp_cpuid_info,return 0 ; } -static int -snp_launch_update_cpuid (uint32_t cpuid_addr,void *hva,size_t cpuid_len) +static int snp_launch_update_cpuid (uint32_t cpuid_addr,void *hva, +size_t cpuid_len,Error **errp) {KvmCpuidInfo kvm_cpuid_info = {0 };SnpCpuidInfo snp_cpuid_info;
@@ -1215,26 +1213,25 @@ snp_launch_update_cpuid(uint32_t cpuid_addr, void *hva, size_t cpuid_len) }while (ret == -E2BIG );if (ret) { -error_report ("SEV-SNP: unable to query CPUID values for CPU: '%s'" , -strerror (-ret)); -return 1 ; +error_setg (errp,"SEV-SNP: unable to query CPUID values for CPU: '%s'" , +strerror (-ret)); +return -1 ; } - ret =sev_snp_cpuid_info_fill (&snp_cpuid_info, &kvm_cpuid_info); -if (ret) { -error_report ("SEV-SNP: failed to generate CPUID table information" ); -return 1 ; + ret =sev_snp_cpuid_info_fill (&snp_cpuid_info, &kvm_cpuid_info, errp); +if (ret <0 ) { +return -1 ; }memcpy (hva, &snp_cpuid_info,sizeof (snp_cpuid_info));return snp_launch_update_data (cpuid_addr, hva, cpuid_len, -KVM_SEV_SNP_PAGE_TYPE_CPUID ); +KVM_SEV_SNP_PAGE_TYPE_CPUID , errp); } -static int -snp_launch_update_kernel_hashes (SevSnpGuestState *sev_snp,uint32_t addr, -void *hva,uint32_t len) +static int snp_launch_update_kernel_hashes (SevSnpGuestState *sev_snp, +uint32_t addr,void *hva, +uint32_t len,Error **errp) {int type =KVM_SEV_SNP_PAGE_TYPE_ZERO ;if (sev_snp->parent_obj.kernel_hashes) {
@@ -1246,7 +1243,7 @@ snp_launch_update_kernel_hashes(SevSnpGuestState *sev_snp, uint32_t addr, sizeof(*sev_snp->kernel_hashes_data)); type =KVM_SEV_SNP_PAGE_TYPE_NORMAL ; } -return snp_launch_update_data (addr, hva, len, type); +return snp_launch_update_data (addr, hva, len, type, errp); } static int
@@ -1284,12 +1281,14 @@ snp_populate_metadata_pages(SevSnpGuestState *sev_snp, }if (type ==KVM_SEV_SNP_PAGE_TYPE_CPUID ) { - ret =snp_launch_update_cpuid (desc->base, hva, desc->len); + ret =snp_launch_update_cpuid (desc->base, hva, desc->len, + &error_fatal); }else if (desc->type ==SEV_DESC_TYPE_SNP_KERNEL_HASHES ) { ret =snp_launch_update_kernel_hashes (sev_snp, desc->base, hva, - desc->len); + desc->len, &error_fatal); }else { - ret =snp_launch_update_data (desc->base, hva, desc->len, type); + ret =snp_launch_update_data (desc->base, hva, desc->len, type, + &error_fatal); }if (ret) {
@@ -1622,9 +1621,8 @@ sev_encrypt_flash(hwaddr gpa, uint8_t *ptr, uint64_t len, Error **errp)if (sev_check_state (sev_common,SEV_STATE_LAUNCH_UPDATE )) {int ret; - ret = klass->launch_update_data (sev_common, gpa, ptr, len); + ret = klass->launch_update_data (sev_common, gpa, ptr, len, errp);if (ret <0 ) { -error_setg (errp,"SEV: Failed to encrypt pflash rom" );return ret; } }
Signature · 1 line
2.43.0
PATCH v7 7/16 target/i386: Allow setting of R_LDTR and R_TR with cpu_x86_load_seg_cache()
In reply to Roy HopkinsThe x86 segment registers are identified by the X86Seg enumeration which includes LDTR and TR as well as the normal segment registers. The function 'cpu_x86_load_seg_cache()' uses the enum to determine which segment to set. However, specifying R_LDTR or R_TR results in an out-of-bounds access of the segment array.
Possibly by coincidence, the function does correctly set LDTR or TR in this case as the structures for these registers immediately follow the array which is accessed out of bounds.
This patch adds correct handling for R_LDTR and R_TR in the function.
--- target/i386/cpu.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
target/i386/cpu.h +8 -1
@@ -2359,7 +2359,14 @@ static inline void cpu_x86_load_seg_cache(CPUX86State *env,SegmentCache *sc;unsigned int new_hflags; - sc = &env->segs[seg_reg]; +if (seg_reg ==R_LDTR ) { + sc = &env->ldt; + }else if (seg_reg ==R_TR ) { + sc = &env->tr; + }else { + sc = &env->segs[seg_reg]; + } + sc->selector = selector; sc->base = base; sc->limit = limit;
Signature · 1 line
2.43.0
PATCH v7 8/16 i386/sev: Refactor setting of reset vector and initial CPU state
In reply to Roy HopkinsWhen 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 | 323 +++++++++++++++++++++++++++++++++++++++++----- target/i386/sev.h | 110 ++++++++++++++++ 2 files changed, 400 insertions(+), 33 deletions(-)
target/i386/sev.c +290 -33
@@ -49,6 +49,12 @@ OBJECT_DECLARE_TYPE(SevSnpGuestState, SevCommonStateClass, SEV_SNP_GUEST)/* 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;
@@ -88,6 +94,14 @@ typedef struct QEMU_PACKED SevHashTableDescriptor {uint32_t size; } SevHashTableDescriptor; +typedef struct SevLaunchVmsa { +QTAILQ_ENTRY (SevLaunchVmsa) next; + +uint16_t cpu_index; +uint64_t gpa; +struct sev_es_save_area vmsa; +}SevLaunchVmsa ; +struct SevCommonState {X86ConfidentialGuest parent_obj;
@@ -106,9 +120,7 @@ struct SevCommonState {int sev_fd;SevState state; -uint32_t reset_cs; -uint32_t reset_ip; -bool reset_data_valid; + QTAILQ_HEAD(, SevLaunchVmsa) launch_vmsa; };struct SevCommonStateClass {
@@ -371,6 +383,172 @@ static struct RAMBlockNotifier sev_ram_notifier = { .ram_block_removed = sev_ram_block_removed, }; +static void sev_apply_cpu_context (CPUState *cpu) +{ +SevCommonState *sev_common =SEV_COMMON (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_common->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); + env->xcr0 = launch_vmsa->vmsa.xcr0; + env->pat = launch_vmsa->vmsa.g_pat; + +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->gdt.flags = +FLAGS_VMSA_TO_SEGCACHE (launch_vmsa->vmsa.gdtr.attrib); + env->idt.base = launch_vmsa->vmsa.idtr.base; + env->idt.limit = launch_vmsa->vmsa.idtr.limit; + env->idt.flags = +FLAGS_VMSA_TO_SEGCACHE (launch_vmsa->vmsa.idtr.attrib); + +cpu_x86_load_seg_cache ( + env,R_LDTR , launch_vmsa->vmsa.ldtr.selector, + launch_vmsa->vmsa.ldtr.base, launch_vmsa->vmsa.ldtr.limit, +FLAGS_VMSA_TO_SEGCACHE (launch_vmsa->vmsa.ldtr.attrib)); +cpu_x86_load_seg_cache ( + env,R_TR , launch_vmsa->vmsa.tr.selector, + launch_vmsa->vmsa.ldtr.base, launch_vmsa->vmsa.tr.limit, +FLAGS_VMSA_TO_SEGCACHE (launch_vmsa->vmsa.tr.attrib)); + + env->dr[6 ] = launch_vmsa->vmsa.dr6; + env->dr[7 ] = launch_vmsa->vmsa.dr7; + + 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; + env->eflags = launch_vmsa->vmsa.rflags; + +cpu_set_fpuc (env, launch_vmsa->vmsa.x87_fcw); + env->mxcsr = launch_vmsa->vmsa.mxcsr; + +break ; + } + } +} + +static int sev_set_cpu_context (uint16_t cpu_index,const void *ctx, +uint32_t ctx_len,hwaddr gpa,Error **errp) +{ +SevCommonState *sev_common =SEV_COMMON (MACHINE (qdev_get_machine ())->cgs); +SevLaunchVmsa *launch_vmsa; +CPUState *cpu; +bool exists = false; + +/* +* Setting the CPU context is only supported for SEV-ES and SEV-SNP. 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_setg (errp,"SEV: unable to set CPU context: Not supported" ); +return -1 ; + } + +if (ctx_len <sizeof (struct sev_es_save_area )) { +error_setg (errp,"SEV: unable to set CPU context: " +"Invalid context provided" ); +return -1 ; + } + + cpu =qemu_get_cpu (cpu_index); +if (!cpu) { +error_setg (errp,"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_common->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_common->launch_vmsa, launch_vmsa, next); + } + +/* Synchronise the VMSA with the current CPU state */ +sev_apply_cpu_context (cpu); + +return 0 ; +} +bool sev_enabled (void ) {
@@ -1005,6 +1183,16 @@ static int sev_launch_update_vmsa(SevGuestState *sev_guest) {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_COMMON (sev_guest)->sev_fd,KVM_SEV_LAUNCH_UPDATE_VMSA ,NULL , &fw_error);
@@ -1787,40 +1975,110 @@ 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) + +static void seg_to_vmsa (const SegmentCache *cpu_seg,struct vmcb_seg *vmsa_seg) { -X86CPU *x86; -CPUX86State *env; -ConfidentialGuestSupport *cgs =MACHINE (qdev_get_machine ())->cgs; -SevCommonState *sev_common =SEV_COMMON ( -object_dynamic_cast (OBJECT (cgs),TYPE_SEV_COMMON )); + 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); +} -/* Only update if we have valid reset information */ -if (!sev_common || !sev_common->reset_data_valid) { -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; -/* Do not update the BSP reset state */ -if (cpu->cpu_index ==0 ) { -return ; +/* +* 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 ]; + vmsa->xcr0 = env->xcr0; + vmsa->g_pat = env->pat; + +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); +seg_to_vmsa (&env->ldt, &vmsa->ldtr); +seg_to_vmsa (&env->tr, &vmsa->tr); + + vmsa->dr6 = env->dr[6 ]; + vmsa->dr7 = env->dr[7 ]; + + 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; + vmsa->rflags = env->eflags; +} + +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 , &error_fatal); +sev_apply_cpu_context (cpu); } +} - x86 =X86_CPU (cpu); - env = &x86->env; - -cpu_x86_load_seg_cache (env,R_CS ,0xf000 , sev_common->reset_cs,0xffff , -DESC_P_MASK |DESC_S_MASK |DESC_CS_MASK | -DESC_R_MASK |DESC_A_MASK ); - - env->eip = sev_common->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; -SevCommonState *sev_common =SEV_COMMON (MACHINE (qdev_get_machine ())->cgs);if (!sev_es_enabled ()) {return 0 ;
@@ -1833,14 +2091,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_common->reset_cs = addr &0xffff0000 ; - sev_common->reset_ip = addr &0x0000ffff ; - sev_common->reset_data_valid = true; - -CPU_FOREACH (cpu) { -sev_es_set_reset_vector (cpu); - } +sev_es_set_ap_context (addr); }return 0 ;
@@ -2075,6 +2331,7 @@ sev_common_instance_init(Object *obj)object_property_add_uint32_ptr (obj,"reduced-phys-bits" , &sev_common->reduced_phys_bits,OBJ_PROP_FLAG_READWRITE ); +QTAILQ_INIT (&sev_common->launch_vmsa); }/* sev guest info common to sev/sev-es/sev-snp */
target/i386/sev.h +110 -0
116 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v7 08/16] i386/sev: Refactor setting of reset vector and initial CPU state
Quoted 10 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_areafor each CPU which is then applied during guest startup and CPU reset.Signed-off-by Roy Hopkins <[email protected]>
Quoted 521 lines
--- target/i386/sev.c | 323 +++++++++++++++++++++++++++++++++++++++++----- target/i386/sev.h | 110 ++++++++++++++++ 2 files changed, 400 insertions(+), 33 deletions(-)target/i386/sev.c +290
-33@@ -49,6 +49,12 @@ OBJECT_DECLARE_TYPE(SevSnpGuestState, SevCommonStateClass, SEV_SNP_GUEST)/* 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;@@ -88,6 +94,14 @@ typedef struct QEMU_PACKED SevHashTableDescriptor {uint32_t size; } SevHashTableDescriptor; +typedef struct SevLaunchVmsa { +QTAILQ_ENTRY (SevLaunchVmsa) next; + +uint16_t cpu_index; +uint64_t gpa; +struct sev_es_save_area vmsa; +}SevLaunchVmsa ; +struct SevCommonState {X86ConfidentialGuest parent_obj;@@ -106,9 +120,7 @@ struct SevCommonState {int sev_fd;SevState state; -uint32_t reset_cs; -uint32_t reset_ip; -bool reset_data_valid; + QTAILQ_HEAD(, SevLaunchVmsa) launch_vmsa; };struct SevCommonStateClass {@@ -371,6 +383,172 @@ static struct RAMBlockNotifier sev_ram_notifier = { .ram_block_removed = sev_ram_block_removed, }; +static void sev_apply_cpu_context (CPUState *cpu) +{ +SevCommonState *sev_common =SEV_COMMON (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_common->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); + env->xcr0 = launch_vmsa->vmsa.xcr0; + env->pat = launch_vmsa->vmsa.g_pat; + +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->gdt.flags = +FLAGS_VMSA_TO_SEGCACHE (launch_vmsa->vmsa.gdtr.attrib); + env->idt.base = launch_vmsa->vmsa.idtr.base; + env->idt.limit = launch_vmsa->vmsa.idtr.limit; + env->idt.flags = +FLAGS_VMSA_TO_SEGCACHE (launch_vmsa->vmsa.idtr.attrib); + +cpu_x86_load_seg_cache ( + env,R_LDTR , launch_vmsa->vmsa.ldtr.selector, + launch_vmsa->vmsa.ldtr.base, launch_vmsa->vmsa.ldtr.limit, +FLAGS_VMSA_TO_SEGCACHE (launch_vmsa->vmsa.ldtr.attrib)); +cpu_x86_load_seg_cache ( + env,R_TR , launch_vmsa->vmsa.tr.selector, + launch_vmsa->vmsa.ldtr.base, launch_vmsa->vmsa.tr.limit, +FLAGS_VMSA_TO_SEGCACHE (launch_vmsa->vmsa.tr.attrib)); + + env->dr[6 ] = launch_vmsa->vmsa.dr6; + env->dr[7 ] = launch_vmsa->vmsa.dr7; + + 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; + env->eflags = launch_vmsa->vmsa.rflags; + +cpu_set_fpuc (env, launch_vmsa->vmsa.x87_fcw); + env->mxcsr = launch_vmsa->vmsa.mxcsr; + +break ; + } + } +} + +static int sev_set_cpu_context (uint16_t cpu_index,const void *ctx, +uint32_t ctx_len,hwaddr gpa,Error **errp) +{ +SevCommonState *sev_common =SEV_COMMON (MACHINE (qdev_get_machine ())->cgs); +SevLaunchVmsa *launch_vmsa; +CPUState *cpu; +bool exists = false; + +/* +* Setting the CPU context is only supported for SEV-ES and SEV-SNP. 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_setg (errp,"SEV: unable to set CPU context: Not supported" ); +return -1 ; + } + +if (ctx_len <sizeof (struct sev_es_save_area )) { +error_setg (errp,"SEV: unable to set CPU context: " +"Invalid context provided" ); +return -1 ; + } + + cpu =qemu_get_cpu (cpu_index); +if (!cpu) { +error_setg (errp,"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_common->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_common->launch_vmsa, launch_vmsa, next); + } + +/* Synchronise the VMSA with the current CPU state */ +sev_apply_cpu_context (cpu); + +return 0 ; +} +bool sev_enabled (void ) {@@ -1005,6 +1183,16 @@ static int sev_launch_update_vmsa(SevGuestState *sev_guest) {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_COMMON (sev_guest)->sev_fd,KVM_SEV_LAUNCH_UPDATE_VMSA ,NULL , &fw_error);@@ -1787,40 +1975,110 @@ 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) + +static void seg_to_vmsa (const SegmentCache *cpu_seg,struct vmcb_seg *vmsa_seg) { -X86CPU *x86; -CPUX86State *env; -ConfidentialGuestSupport *cgs =MACHINE (qdev_get_machine ())->cgs; -SevCommonState *sev_common =SEV_COMMON ( -object_dynamic_cast (OBJECT (cgs),TYPE_SEV_COMMON )); + 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); +} -/* Only update if we have valid reset information */ -if (!sev_common || !sev_common->reset_data_valid) { -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; -/* Do not update the BSP reset state */ -if (cpu->cpu_index ==0 ) { -return ; +/* +* 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 ]; + vmsa->xcr0 = env->xcr0; + vmsa->g_pat = env->pat; + +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); +seg_to_vmsa (&env->ldt, &vmsa->ldtr); +seg_to_vmsa (&env->tr, &vmsa->tr); + + vmsa->dr6 = env->dr[6 ]; + vmsa->dr7 = env->dr[7 ]; + + 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; + vmsa->rflags = env->eflags; +} + +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 , &error_fatal); +sev_apply_cpu_context (cpu); } +} - x86 =X86_CPU (cpu); - env = &x86->env; - -cpu_x86_load_seg_cache (env,R_CS ,0xf000 , sev_common->reset_cs,0xffff , -DESC_P_MASK |DESC_S_MASK |DESC_CS_MASK | -DESC_R_MASK |DESC_A_MASK ); - - env->eip = sev_common->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; -SevCommonState *sev_common =SEV_COMMON (MACHINE (qdev_get_machine ())->cgs);if (!sev_es_enabled ()) {return 0 ;@@ -1833,14 +2091,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_common->reset_cs = addr &0xffff0000 ; - sev_common->reset_ip = addr &0x0000ffff ; - sev_common->reset_data_valid = true; - -CPU_FOREACH (cpu) { -sev_es_set_reset_vector (cpu); - } +sev_es_set_ap_context (addr); }return 0 ;@@ -2075,6 +2331,7 @@ sev_common_instance_init(Object *obj)object_property_add_uint32_ptr (obj,"reduced-phys-bits" , &sev_common->reduced_phys_bits,OBJ_PROP_FLAG_READWRITE ); +QTAILQ_INIT (&sev_common->launch_vmsa); }/* sev guest info common to sev/sev-es/sev-snp */ target/i386/sev.h +110
-0@@ -55,6 +55,116 @@ typedef struct SevKernelLoaderContext {size_t cmdline_size; } SevKernelLoaderContext; +/* Save area definition for SEV-ES and SEV-SNP guests */ +struct QEMU_PACKED sev_es_save_area { +struct vmcb_seg es; +struct vmcb_seg cs; +struct vmcb_seg ss; +struct vmcb_seg ds; +struct vmcb_seg fs; +struct vmcb_seg gs; +struct vmcb_seg gdtr; +struct vmcb_seg ldtr; +struct vmcb_seg idtr; +struct vmcb_seg tr; +uint64_t vmpl0_ssp; +uint64_t vmpl1_ssp; +uint64_t vmpl2_ssp; +uint64_t vmpl3_ssp; +uint64_t u_cet; +uint8_t reserved_0xc8[2 ]; +uint8_t vmpl; +uint8_t cpl; +uint8_t reserved_0xcc[4 ]; +uint64_t efer; +uint8_t reserved_0xd8[104 ]; +uint64_t xss; +uint64_t cr4; +uint64_t cr3; +uint64_t cr0; +uint64_t dr7; +uint64_t dr6; +uint64_t rflags; +uint64_t rip; +uint64_t dr0; +uint64_t dr1; +uint64_t dr2; +uint64_t dr3; +uint64_t dr0_addr_mask; +uint64_t dr1_addr_mask; +uint64_t dr2_addr_mask; +uint64_t dr3_addr_mask; +uint8_t reserved_0x1c0[24 ]; +uint64_t rsp; +uint64_t s_cet; +uint64_t ssp; +uint64_t isst_addr; +uint64_t rax; +uint64_t star; +uint64_t lstar; +uint64_t cstar; +uint64_t sfmask; +uint64_t kernel_gs_base; +uint64_t sysenter_cs; +uint64_t sysenter_esp; +uint64_t sysenter_eip; +uint64_t cr2; +uint8_t reserved_0x248[32 ]; +uint64_t g_pat; +uint64_t dbgctl; +uint64_t br_from; +uint64_t br_to; +uint64_t last_excp_from; +uint64_t last_excp_to; +uint8_t reserved_0x298[80 ]; +uint32_t pkru; +uint32_t tsc_aux; +uint8_t reserved_0x2f0[24 ]; +uint64_t rcx; +uint64_t rdx; +uint64_t rbx; +uint64_t reserved_0x320;/* rsp already available at 0x01d8 */ +uint64_t rbp; +uint64_t rsi; +uint64_t rdi; +uint64_t r8; +uint64_t r9; +uint64_t r10; +uint64_t r11; +uint64_t r12; +uint64_t r13; +uint64_t r14; +uint64_t r15; +uint8_t reserved_0x380[16 ]; +uint64_t guest_exit_info_1; +uint64_t guest_exit_info_2; +uint64_t guest_exit_int_info; +uint64_t guest_nrip; +uint64_t sev_features; +uint64_t vintr_ctrl; +uint64_t guest_exit_code; +uint64_t virtual_tom; +uint64_t tlb_id; +uint64_t pcpu_id; +uint64_t event_inj; +uint64_t xcr0; +uint8_t reserved_0x3f0[16 ]; + +/* Floating point area */ +uint64_t x87_dp; +uint32_t mxcsr; +uint16_t x87_ftw; +uint16_t x87_fsw; +uint16_t x87_fcw; +uint16_t x87_fop; +uint16_t x87_ds; +uint16_t x87_cs; +uint64_t x87_rip; +uint8_t fpreg_x87[80 ]; +uint8_t fpreg_xmm[256 ]; +uint8_t fpreg_ymm[256 ]; +}; +bool sev_add_kernel_loader_hashes (SevKernelLoaderContext *ctx,Error **errp);int sev_encrypt_flash (hwaddr gpa,uint8_t *ptr,uint64_t len,Error **errp);
PATCH v7 9/16 i386/sev: Implement ConfidentialGuestSupport functions for SEV
In reply to Roy HopkinsThe 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 | 254 ++++++++++++++++++++++++++++++++++++++++++++-- target/i386/sev.h | 2 + 2 files changed, 246 insertions(+), 10 deletions(-)
target/i386/sev.c +244 -10
@@ -39,8 +39,10 @@#include "qapi/qapi-commands-misc-target.h" #include "confidential-guest.h" #include "hw/i386/pc.h" +#include "hw/i386/e820_memory_layout.h" #include "exec/address-spaces.h" #include "qemu/queue.h" +#include "qemu/cutils.h" OBJECT_DECLARE_TYPE(SevCommonState, SevCommonStateClass, SEV_COMMON)OBJECT_DECLARE_TYPE (SevGuestState, SevCommonStateClass,SEV_GUEST )
@@ -49,6 +51,9 @@ OBJECT_DECLARE_TYPE(SevSnpGuestState, SevCommonStateClass, SEV_SNP_GUEST)/* hard code sha256 digest size */ #define HASH_SIZE 32 +/* Hard coded GPA that KVM uses for the VMSA */ +#define KVM_VMSA_GPA 0xFFFFFFFFF000 +/* Convert between SEV-ES VMSA and SegmentCache flags/attributes */ #define FLAGS_VMSA_TO_SEGCACHE (flags) \ ((((flags) & 0xff00) << 12) | (((flags) & 0xff) << 8))
@@ -487,6 +492,103 @@ static void sev_apply_cpu_context(CPUState *cpu) } } +static int check_vmsa_supported (hwaddr gpa,const struct sev_es_save_area *vmsa, +Error **errp) +{ +struct sev_es_save_area vmsa_check; + +/* +* KVM always populates the VMSA at a fixed GPA which cannot be modified +* from userspace. Specifying a different GPA will not prevent the guest +* from starting but will cause the launch measurement to be different +* from expected. Therefore check that the provided GPA matches the KVM +* hardcoded value. +*/ +if (gpa !=KVM_VMSA_GPA ) { +error_setg (errp, +"%s: The VMSA GPA must be %lX but is specified as %lX" , + __func__,KVM_VMSA_GPA , gpa); +return -1 ; + } + +/* +* Clear all supported fields so we can then check the entire structure +* is zero. +*/ +memcpy (&vmsa_check, vmsa,sizeof (struct sev_es_save_area )); +memset (&vmsa_check.es,0 ,sizeof (vmsa_check.es)); +memset (&vmsa_check.cs,0 ,sizeof (vmsa_check.cs)); +memset (&vmsa_check.ss,0 ,sizeof (vmsa_check.ss)); +memset (&vmsa_check.ds,0 ,sizeof (vmsa_check.ds)); +memset (&vmsa_check.fs,0 ,sizeof (vmsa_check.fs)); +memset (&vmsa_check.gs,0 ,sizeof (vmsa_check.gs)); +memset (&vmsa_check.gdtr,0 ,sizeof (vmsa_check.gdtr)); +memset (&vmsa_check.idtr,0 ,sizeof (vmsa_check.idtr)); +memset (&vmsa_check.ldtr,0 ,sizeof (vmsa_check.ldtr)); +memset (&vmsa_check.tr,0 ,sizeof (vmsa_check.tr)); + vmsa_check.efer =0 ; + vmsa_check.cr0 =0 ; + vmsa_check.cr3 =0 ; + vmsa_check.cr4 =0 ; + vmsa_check.xcr0 =0 ; + vmsa_check.dr6 =0 ; + vmsa_check.dr7 =0 ; + vmsa_check.rax =0 ; + vmsa_check.rcx =0 ; + vmsa_check.rdx =0 ; + vmsa_check.rbx =0 ; + vmsa_check.rsp =0 ; + vmsa_check.rbp =0 ; + vmsa_check.rsi =0 ; + vmsa_check.rdi =0 ; + vmsa_check.r8 =0 ; + vmsa_check.r9 =0 ; + vmsa_check.r10 =0 ; + vmsa_check.r11 =0 ; + vmsa_check.r12 =0 ; + vmsa_check.r13 =0 ; + vmsa_check.r14 =0 ; + vmsa_check.r15 =0 ; + vmsa_check.rip =0 ; + vmsa_check.rflags =0 ; + + vmsa_check.g_pat =0 ; + vmsa_check.xcr0 =0 ; + + vmsa_check.x87_fcw =0 ; + vmsa_check.mxcsr =0 ; + +if (sev_snp_enabled ()) { +if (vmsa_check.sev_features !=SVM_SEV_FEAT_SNP_ACTIVE ) { +error_setg (errp, +"%s: sev_features in the VMSA contains an unsupported " +"value. For SEV-SNP, sev_features must be set to %x." , + __func__,SVM_SEV_FEAT_SNP_ACTIVE ); +return -1 ; + } + vmsa_check.sev_features =0 ; + }else { +if (vmsa_check.sev_features !=0 ) { +error_setg (errp, +"%s: sev_features in the VMSA contains an unsupported " +"value. For SEV-ES and SEV, sev_features must be " +"set to 0." , __func__); +return -1 ; + } + } + +if (!buffer_is_zero (&vmsa_check,sizeof (vmsa_check))) { +error_setg (errp, +"%s: The VMSA contains fields that are not " +"synchronized with KVM. Continuing would result in " +"either unpredictable guest behavior, or a " +"mismatched launch measurement." , + __func__); +return -1 ; + } +return 0 ; +} +static int sev_set_cpu_context (uint16_t cpu_index,const void *ctx,uint32_t ctx_len,hwaddr gpa,Error **errp) {
@@ -1498,18 +1600,26 @@ sev_snp_launch_finish(SevCommonState *sev_common)struct kvm_sev_snp_launch_finish *finish = &sev_snp->kvm_finish_conf;/* -* To boot the SNP guest, the hypervisor is required to populate the CPUID -* and Secrets page before finalizing the launch flow. The location of -* the secrets and CPUID page is available through the OVMF metadata GUID. +* Populate all the metadata pages if not using an IGVM file. In the case +* where an IGVM file is provided it will be used to configure the metadata +* pages directly. */ - metadata =pc_system_get_ovmf_sev_metadata_ptr (); -if (metadata ==NULL ) { -error_report ("%s: Failed to locate SEV metadata header" , __func__); -exit (1 ); - } +if (!X86_MACHINE (qdev_get_machine ())->igvm) { +/* +* To boot the SNP guest, the hypervisor is required to populate the +* CPUID and Secrets page before finalizing the launch flow. The +* location of the secrets and CPUID page is available through the +* OVMF metadata GUID. +*/ + metadata =pc_system_get_ovmf_sev_metadata_ptr (); +if (metadata ==NULL ) { +error_report ("%s: Failed to locate SEV metadata header" , __func__); +exit (1 ); + } -/* Populate all the metadata pages */ -snp_populate_metadata_pages (sev_snp, metadata); +/* Populate all the metadata pages */ +snp_populate_metadata_pages (sev_snp, metadata); + } QTAILQ_FOREACH(data, &launch_update, next) { ret =sev_snp_launch_update (sev_snp, data);
@@ -2298,6 +2408,124 @@ static void sev_common_set_kernel_hashes(Object *obj, bool value, Error **errp)SEV_COMMON (obj)->kernel_hashes = value; } +static bool cgs_check_support (ConfidentialGuestPlatformType platform, +uint16_t platform_version,uint8_t highest_vtl, +uint64_t shared_gpa_boundary) +{ +return (((platform ==CGS_PLATFORM_SEV_SNP ) &&sev_snp_enabled ()) || + ((platform ==CGS_PLATFORM_SEV_ES ) &&sev_es_enabled ()) || + ((platform ==CGS_PLATFORM_SEV ) &&sev_enabled ())); +} + +static int cgs_set_guest_state (hwaddr gpa,uint8_t *ptr,uint64_t len, +ConfidentialGuestPageType memory_type, +uint16_t cpu_index,Error **errp) +{ +SevCommonState *sev_common =SEV_COMMON (MACHINE (qdev_get_machine ())->cgs); +SevCommonStateClass *klass =SEV_COMMON_GET_CLASS (sev_common); + +if (!sev_enabled ()) { +error_setg (errp,"%s: attempt to configure guest memory, but SEV " +"is not enabled" , __func__); +return -1 ; + } + +switch (memory_type) { +case CGS_PAGE_TYPE_NORMAL : +case CGS_PAGE_TYPE_ZERO : +return klass->launch_update_data (sev_common, gpa, ptr, len, errp); + +case CGS_PAGE_TYPE_VMSA : +if (!sev_es_enabled ()) { +error_setg (errp, +"%s: attempt to configure initial VMSA, but SEV-ES " +"is not supported" , + __func__); +return -1 ; + } +if (check_vmsa_supported (gpa, (const struct sev_es_save_area *)ptr, + errp) <0 ) { +return -1 ; + } +return sev_set_cpu_context (cpu_index, ptr, len, gpa, errp); + +case CGS_PAGE_TYPE_UNMEASURED : +if (sev_snp_enabled ()) { +return snp_launch_update_data ( + gpa, ptr, len,KVM_SEV_SNP_PAGE_TYPE_UNMEASURED , errp); + } +/* No action required if not SEV-SNP */ +return 0 ; + +case CGS_PAGE_TYPE_SECRETS : +if (!sev_snp_enabled ()) { +error_setg (errp, +"%s: attempt to configure secrets page, but SEV-SNP " +"is not supported" , + __func__); +return -1 ; + } +return snp_launch_update_data (gpa, ptr, len, +KVM_SEV_SNP_PAGE_TYPE_SECRETS , errp); + +case CGS_PAGE_TYPE_REQUIRED_MEMORY : +if (kvm_convert_memory (gpa, len, true) <0 ) { +error_setg ( + errp, +"%s: failed to configure required memory. gpa: %lX, type: %d" , + __func__, gpa, memory_type); +return -1 ; + } +return 0 ; + +case CGS_PAGE_TYPE_CPUID : +if (!sev_snp_enabled ()) { +error_setg (errp, +"%s: attempt to configure CPUID page, but SEV-SNP " +"is not supported" , + __func__); +return -1 ; + } +return snp_launch_update_cpuid (gpa, ptr, len, errp); + } +error_setg (errp,"%s: failed to update guest. gpa: %lX, type: %d" , __func__, + gpa, memory_type); +return -1 ; +} + +static int cgs_get_mem_map_entry (int index, +ConfidentialGuestMemoryMapEntry *entry, +Error **errp) +{ +struct e820_entry *table; +int num_entries; + + num_entries =e820_get_table (&table); +if ((index <0 ) || (index >= num_entries)) { +return 1 ; + } + entry->gpa = table[index].address; + entry->size = table[index].length; +switch (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 void sev_common_class_init (ObjectClass *oc,void *data) {
@@ -2321,6 +2549,8 @@ static void sev_common_instance_init(Object *obj) {SevCommonState *sev_common =SEV_COMMON (obj); +ConfidentialGuestSupportClass *cgs = +CONFIDENTIAL_GUEST_SUPPORT_GET_CLASS (obj); sev_common->kvm_type =-1 ;
@@ -2331,6 +2561,10 @@ sev_common_instance_init(Object *obj)object_property_add_uint32_ptr (obj,"reduced-phys-bits" , &sev_common->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_common->launch_vmsa); }
target/i386/sev.h +2 -0
@@ -44,6 +44,8 @@ bool sev_snp_enabled(void);#define SEV_SNP_POLICY_SMT 0x10000#define SEV_SNP_POLICY_DBG 0x80000 +#define SVM_SEV_FEAT_SNP_ACTIVE 1 + typedef struct SevKernelLoaderContext { char *setup_data; size_t setup_size;
Signature · 1 line
2.43.0
PATCH v7 10/16 docs/system: Add documentation on support for IGVM
In reply to Roy HopkinsIGVM 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/i386/amd-memory-encryption.rst | 2 + docs/system/igvm.rst | 173 +++++++++++++++++++++ docs/system/index.rst | 1 + 3 files changed, 176 insertions(+) create mode 100644 docs/system/igvm.rst
docs/system/i386/amd-memory-encryption.rst +2 -0
@@ -1,3 +1,5 @@ +.. _amd-sev: + AMD Secure Encrypted Virtualization (SEV) =========================================
docs/system/igvm.rst new file +173 -0
174 lines not on this page · raw message
docs/system/index.rst +1 -0
@@ -38,4 +38,5 @@ or Hypervisor.Framework. security multi-process confidential-guest-support + igvm vm-templating
Signature · 1 line
2.43.0
Re: [PATCH v7 10/16] docs/system: Add documentation on support for IGVM
Quoted 107 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.
Signed-off-by Roy Hopkins <[email protected]>Reviewed-by Daniel P. Berrangé <[email protected]>Reviewed-by Stefano Garzarella <[email protected]>--- docs/system/i386/amd-memory-encryption.rst | 2 + docs/system/igvm.rst | 173 +++++++++++++++++++++ docs/system/index.rst | 1 + 3 files changed, 176 insertions(+)create mode 100644
docs/system/igvm.rstdocs/system/i386/amd-memory-encryption.rst +2
-0@@ -1,3 +1,5 @@ +.. _amd-sev: + AMD Secure Encrypted Virtualization (SEV) =========================================docs/system/igvm.rst new file +75
-0@@ -0,0 +1,173 @@ +Independent Guest Virtual Machine (IGVM) support +================================================ + +IGVM files are designed to encapsulate 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. + +Although IGVM files are designed with Confidential Computing in mind, they can +also be used to configure non-confidential guests. Multiple platforms can be +defined by a single IGVM file, allowing a single IGVM file to configure a +virtual machine that can run on, for example, TDX, SEV and non-confidential +hosts. + +QEMU supports IGVM files through the user-creatable ``igvm-cfg`` object. This +object is used to define the filename of the IGVM file to process. A reference +to the object is added to the ``-machine`` to configure the virtual machine +to use the IGVM file for configuration. + +Confidential platform support is provided through the use of +the ``ConfidentialGuestSupport`` object. If the virtual machine provides an +instance of this object then this is used by the IGVM loader to configure the +isolation properties of the directives within the file. + +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 Platforms +------------------- + +Currently, IGVM files can be provided for Confidential Guests on host systems +that support AMD SEV, SEV-ES and SEV-SNP with KVM. IGVM files can also be +provided for non-confidential guests. + + +Limitations when using IGVM with AMD SEV, SEV-ES and SEV-SNP +------------------------------------------------------------ + +IGVM files configure the initial state of the guest using a set of directives. +Not every directive is supported by every Confidential Guest type. For example, +AMD SEV does not support encrypted save state regions, therefore setting the +initial CPU state using IGVM for SEV is not possible. When an IGVM file contains +directives that are not supported for the active platform, an error is generated +and the guest launch is aborted. + +The table below describes the list of directives that are supported for SEV, +SEV-ES, SEV-SNP and non-confidential platforms. + +.. list-table:: SEV, SEV-ES, SEV-SNP & non-confidential Supported Directives + :widths: 35 65 + :header-rows: 1 + + * - IGVM directive + - Notes + * - IGVM_VHT_PAGE_DATA + - ``NORMAL`` zero, measured and unmeasured page types are supported. Other + page types result in an error. + * - IGVM_VHT_PARAMETER_AREA + - + * - IGVM_VHT_PARAMETER_INSERT + - + * - IGVM_VHT_VP_COUNT_PARAMETER + - The guest parameter page is populated with the CPU count. + * - IGVM_VHT_ENVIRONMENT_INFO_PARAMETER + - The ``memory_is_shared`` parameter is set to 1 in the guest parameter + page. +
docs/system/igvm.rst line 85
Quoted 10 lines
docs/system/igvm.rst new file +10
-0+.. list-table:: Additional SEV, SEV-ES & SEV_SNP Supported Directives + :widths: 25 75 + :header-rows: 1 + + * - IGVM directive + - Notes + * - IGVM_VHT_MEMORY_MAP + - The memory map page is populated using entries from the E820 table. + * - IGVM_VHT_REQUIRED_MEMORY + -
Is this '-' superflous? Or maybe you want to describe it here?
Other than that:
Quoted 99 lines
docs/system/igvm.rst new file +88
-0+ +.. list-table:: Additional SEV-ES & SEV-SNP Supported Directives + :widths: 25 75 + :header-rows: 1 + + * - IGVM directive + - Notes + * - IGVM_VHT_VP_CONTEXT + - Setting of the initial CPU state for the boot CPU and additional CPUs is + supported with limitations on the fields that can be provided in the + VMSA. See below for details on which fields are supported. + +Initial CPU state with VMSA +--------------------------- + +The initial state of guest CPUs can be defined in the IGVM file for AMD SEV-ES +and SEV-SNP. The state data is provided as a VMSA structure as defined in Table +B-4 in the AMD64 Architecture Programmer's Manual, Volume 2 [1]. + +The IGVM VMSA is translated to CPU state in QEMU which is then synchronized +by KVM to the guest VMSA during the launch process where it contributes to the +launch measurement. See :ref:`amd-sev` for details on the launch process and +guest launch measurement. + +It is important that no information is lost or changed when translating the +VMSA provided by the IGVM file into the VSMA that is used to launch the guest. +Therefore, QEMU restricts the VMSA fields that can be provided in the IGVM +VMSA structure to the following registers: + +RAX, RCX, RDX, RBX, RBP, RSI, RDI, R8-R15, RSP, RIP, CS, DS, ES, FS, GS, SS, +CR0, CR3, CR4, XCR0, EFER, PAT, GDT, IDT, LDTR, TR, DR6, DR7, RFLAGS, X87_FCW, +MXCSR. + +When processing the IGVM file, QEMU will check if any fields other than the +above are non-zero and generate an error if this is the case. + +KVM uses a hardcoded GPA of 0xFFFFFFFFF000 for the VMSA. When an IGVM file +defines initial CPU state, the GPA for each VMSA must match this hardcoded +value. + +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 or via the ``-bios`` parameter. The default QEMU +firmware is not automatically populated into the guest memory space. + +If an IGVM file is provided along with either the ``-bios`` parameter or pflash +devices then an error is displayed and the guest startup is aborted. + +Running a guest configured using IGVM +------------------------------------- + +To run a guest configured with IGVM you firstly need to generate an IGVM file +that contains a guest configuration compatible with the platform you are +targeting. + +The ``buildigvm`` tool [2] is an example of a tool that can be used to generate +IGVM files for non-confidential X86 platforms as well as for SEV, SEV-ES and +SEV-SNP confidential platforms. + +Example using this tool to generate an IGVM file for AMD SEV-SNP:: + + buildigvm --firmware /path/to/OVMF.fd --output sev-snp.igvm \ + --cpucount 4 sev-snp + +To run a guest configured with the generated IGVM you need to add an +``igvm-cfg`` object and refer to it from the ``-machine`` parameter: + +Example (for AMD SEV):: + + qemu-system-x86_64 \ + <other parameters> \ + -machine ...,confidential-guest-support=sev0,igvm-cfg=igvm0 \ + -object sev-guest,id=sev0,cbitpos=47,reduced-phys-bits=1 \ + -object igvm-cfg,id=igvm0,file=/path/to/sev-snp.igvm + +References +---------- + +[1] AMD64 Architecture Programmer's Manual, Volume 2: System Programming + Rev 3.41 + https://www.amd.com/content/dam/amd/en/documents/processor-tech-docs/programmer-references/24593.pdf + +[2] ``buildigvm`` - A tool to build example IGVM files containing OVMF firmware + https://github.com/roy-hopkins/buildigvmdocs/system/index.rst +1
-0@@ -38,4 +38,5 @@ or Hypervisor.Framework. security multi-process confidential-guest-support + igvm vm-templating
PATCH v7 11/16 docs/interop/firmware.json: Add igvm to FirmwareDevice
In reply to Roy HopkinsCreate an enum entry within FirmwareDevice for 'igvm' to describe that an IGVM file can be used to map firmware into memory as an alternative to pre-existing firmware devices.
--- docs/interop/firmware.json | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-)
docs/interop/firmware.json +28 -2
@@ -57,10 +57,17 @@ # # @memory: The firmware is to be mapped into memory. # +# @igvm: The firmware is defined by a file conforming to the IGVM +# specification and mapped into memory according to directives +# defined in the file. This is similar to @memory but may +# include additional processing defined by the IGVM file +# including initial CPU state or population of metadata into +# the guest address space. Since: 10.0 +# # Since: 3.0 ## { 'enum' : 'FirmwareDevice', - 'data' : [ 'flash', 'kernel', 'memory' ] } + 'data' : [ 'flash', 'kernel', 'memory', 'igvm' ] } ## # @FirmwareArchitecture:
@@ -367,6 +374,24 @@ { 'struct' : 'FirmwareMappingMemory', 'data' : { 'filename' : 'str' } } +## +# @FirmwareMappingIgvm: +# +# Describes loading and mapping properties for the firmware executable, +# when @FirmwareDevice is @igvm. +# +# @filename: Identifies the IGVM file containing the firmware executable +# along with other information used to configure the initial +# state of the guest. The IGVM file may be shared by multiple +# virtual machine definitions. This corresponds to creating +# an object on the command line with "-object igvm-cfg, +# file=@filename". +# +# Since: 10.0 +## +{ 'struct' : 'FirmwareMappingIgvm', + 'data' : { 'filename' : 'str' } } + ## # @FirmwareMapping: #
@@ -383,7 +408,8 @@ 'discriminator' : 'device', 'data' : { 'flash' : 'FirmwareMappingFlash', 'kernel' : 'FirmwareMappingKernel', - 'memory' : 'FirmwareMappingMemory' } } + 'memory' : 'FirmwareMappingMemory', + 'igvm' : 'FirmwareMappingIgvm' } } ## # @Firmware:
Signature · 1 line
2.43.0
Re: [PATCH v7 11/16] docs/interop/firmware.json: Add igvm to FirmwareDevice
Quoted 7 lines · Roy Hopkins
Create an enum entry within FirmwareDevice for 'igvm' to describe that an IGVM file can be used to map firmware into memory as an alternative to pre-existing firmware devices.
Signed-off-by Roy Hopkins <[email protected]>Reviewed-by Stefano Garzarella <[email protected]>
This should Cc'ed to the libvirt list because libvirt consumes these files.
take care,
Gerd
PATCH v7 12/16 backends/confidential-guest-support: Add set_guest_policy() function
In reply to Roy HopkinsFor confidential guests a policy can be provided that defines the security level, debug status, expected launch measurement and other parameters that define the configuration of the confidential platform.
This commit adds a new function named set_guest_policy() that can be implemented by each confidential platform, such as AMD SEV to set the policy. This will allow configuration of the policy from a multi-platform resource such as an IGVM file without the IGVM processor requiring specific implementation details for each platform.
--- backends/confidential-guest-support.c | 12 ++++++++++++ include/system/confidential-guest-support.h | 21 +++++++++++++++++++++ 2 files changed, 33 insertions(+)
backends/confidential-guest-support.c +12 -0
@@ -38,6 +38,17 @@ static int set_guest_state(hwaddr gpa, uint8_t *ptr, uint64_t len,return -1 ; } +static int set_guest_policy (ConfidentialGuestPolicyType policy_type, +uint64_t policy, +void *policy_data1,uint32_t policy_data1_size, +void *policy_data2,uint32_t policy_data2_size, +Error **errp) +{ +error_setg (errp, +"Setting confidential guest policy is not supported for this platform" ); +return -1 ; +} +static int get_mem_map_entry (int index,ConfidentialGuestMemoryMapEntry *entry,Error **errp) {
@@ -52,6 +63,7 @@ static void confidential_guest_support_class_init(ObjectClass *oc, void *data)ConfidentialGuestSupportClass *cgsc =CONFIDENTIAL_GUEST_SUPPORT_CLASS (oc); cgsc->check_support = check_support; cgsc->set_guest_state = set_guest_state; + cgsc->set_guest_policy = set_guest_policy; cgsc->get_mem_map_entry = get_mem_map_entry; }
include/system/confidential-guest-support.h +21 -0
@@ -61,6 +61,10 @@ typedef enum ConfidentialGuestPageType { CGS_PAGE_TYPE_REQUIRED_MEMORY, } ConfidentialGuestPageType; +typedef enum ConfidentialGuestPolicyType { +GUEST_POLICY_SEV , +}ConfidentialGuestPolicyType ; +struct ConfidentialGuestSupport {Object parent;
@@ -127,6 +131,23 @@ typedef struct ConfidentialGuestSupportClass {ConfidentialGuestPageType memory_type, uint16_t cpu_index, Error **errp); +/* +* Set the guest policy. The policy can be used to configure the +* confidential platform, such as if debug is enabled or not and can contain +* information about expected launch measurements, signed verification of +* guest configuration and other platform data. +* +* The format of the policy data is specific to each platform. For example, +* SEV-SNP uses a policy bitfield in the 'policy' argument and provides an +* ID block and ID authentication in the 'policy_data' parameters. The type +* of policy data is identified by the 'policy_type' argument. +*/ +int (*set_guest_policy)(ConfidentialGuestPolicyType policy_type, +uint64_t policy, +void *policy_data1,uint32_t policy_data1_size, +void *policy_data2,uint32_t policy_data2_size, +Error **errp); +/* * Iterate the system memory map, getting the entry with the given index * that can be populated into guest memory.
Signature · 1 line
2.43.0
PATCH v7 13/16 backends/igvm: Process initialization sections in IGVM file
In reply to Roy HopkinsThe initialization sections in IGVM files contain configuration that should be applied to the guest platform before it is started. This includes guest policy and other information that can affect the security level and the startup measurement of a guest.
This commit introduces handling of the initialization sections during processing of the IGVM file.
--- backends/igvm.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
backends/igvm.c +21 -0
@@ -787,6 +787,27 @@ int qigvm_process_file(IgvmCfg *cfg, ConfidentialGuestSupport *cgs, } } + header_count = +igvm_header_count (ctx.file,IGVM_HEADER_SECTION_INITIALIZATION ); +if (header_count <0 ) { +error_setg ( + errp, +"Invalid initialization header count in IGVM file. Error code: %X" , + header_count); + goto cleanup_parameters; + } + +for (ctx.current_header_index =0 ; + ctx.current_header_index < (unsigned )header_count; + ctx.current_header_index++) { +IgvmVariableHeaderType type = +igvm_get_header_type (ctx.file,IGVM_HEADER_SECTION_INITIALIZATION , + ctx.current_header_index); +if (qigvm_handler (&ctx, type, errp) <0 ) { + goto cleanup_parameters; + } + } +/* * Contiguous pages of data with compatible flags are grouped together in * order to reduce the number of memory regions we create. Make sure the
Signature · 1 line
2.43.0
PATCH v7 14/16 backends/igvm: Handle policy for SEV guests
In reply to Roy HopkinsAdds a handler for the guest policy initialization IGVM section and builds an SEV policy based on this information and the ID block directive if present. The policy is applied using by calling 'set_guest_policy()' on the ConfidentialGuestSupport object.
--- backends/igvm.c | 138 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+)
backends/igvm.c +138 -0
@@ -28,6 +28,33 @@ typedef struct QIgvmParameterData {uint32_t index; } QIgvmParameterData; +/* +* Some directives are specific to particular confidential computing platforms. +* Define required types for each of those platforms here. +*/ + +/* SEV/SEV-ES/SEV-SNP */ +struct QEMU_PACKED sev_id_block { +uint8_t ld[48 ]; +uint8_t family_id[16 ]; +uint8_t image_id[16 ]; +uint32_t version; +uint32_t guest_svn; +uint64_t policy; +}; + +struct QEMU_PACKED sev_id_authentication { +uint32_t id_key_alg; +uint32_t auth_key_algo; +uint8_t reserved[56 ]; +uint8_t id_block_sig[512 ]; +uint8_t id_key[1028 ]; +uint8_t reserved2[60 ]; +uint8_t id_key_sig[512 ]; +uint8_t author_key[1028 ]; +uint8_t reserved3[892 ]; +}; +/* * QIgvm contains the information required during processing * of a single IGVM file.
@@ -39,6 +66,17 @@ typedef struct QIgvm {uint32_t compatibility_mask;unsigned current_header_index; QTAILQ_HEAD(, QIgvmParameterData) parameter_data; +IgvmPlatformType platform_type; + +/* +* SEV-SNP platforms can contain an ID block and authentication +* that should be verified by the guest. +*/ +struct sev_id_block *id_block; +struct sev_id_authentication *id_auth; + +/* Define the guest policy for SEV guests */ +uint64_t sev_policy;/* These variables keep track of contiguous page regions */ IGVM_VHS_PAGE_DATA region_prev_page_data;
@@ -68,6 +106,11 @@ static int qigvm_directive_environment_info(QIgvm *ctx,static int qigvm_directive_required_memory (QIgvm *ctx,const uint8_t *header_data,Error **errp); +static int qigvm_directive_snp_id_block (QIgvm *ctx,const uint8_t *header_data, +Error **errp); +static int qigvm_initialization_guest_policy (QIgvm *ctx, +const uint8_t *header_data, +Error **errp);struct QIGVMHandler {uint32_t type;
@@ -92,6 +135,10 @@ static struct QIGVMHandler handlers[] = { qigvm_directive_environment_info }, {IGVM_VHT_REQUIRED_MEMORY ,IGVM_HEADER_SECTION_DIRECTIVE , qigvm_directive_required_memory }, + { IGVM_VHT_SNP_ID_BLOCK, IGVM_HEADER_SECTION_DIRECTIVE, + qigvm_directive_snp_id_block }, + { IGVM_VHT_GUEST_POLICY, IGVM_HEADER_SECTION_INITIALIZATION, + qigvm_initialization_guest_policy }, };static int qigvm_handler (QIgvm *ctx,uint32_t type,Error **errp)
@@ -633,6 +680,70 @@ static int qigvm_directive_required_memory(QIgvm *ctx,return 0 ; } +static int qigvm_directive_snp_id_block (QIgvm *ctx,const uint8_t *header_data, +Error **errp) +{ +const IGVM_VHS_SNP_ID_BLOCK *igvm_id = + (const IGVM_VHS_SNP_ID_BLOCK *)header_data; + +if (!(igvm_id->compatibility_mask & ctx->compatibility_mask)) { +return 0 ; + } + +if (ctx->id_block) { +error_setg (errp,"IGVM: Multiple ID blocks encountered " +"in IGVM file." ); +return -1 ; + } + ctx->id_block = g_new0(struct sev_id_block, 1); + ctx->id_auth = g_new0(struct sev_id_authentication, 1); + +memcpy (ctx->id_block->family_id, igvm_id->family_id, +sizeof (ctx->id_block->family_id)); +memcpy (ctx->id_block->image_id, igvm_id->image_id, +sizeof (ctx->id_block->image_id)); + ctx->id_block->guest_svn = igvm_id->guest_svn; + ctx->id_block->version =1 ; +memcpy (ctx->id_block->ld, igvm_id->ld,sizeof (ctx->id_block->ld)); + + ctx->id_auth->id_key_alg = igvm_id->id_key_algorithm; +memcpy (ctx->id_auth->id_block_sig, &igvm_id->id_key_signature, +sizeof (igvm_id->id_key_signature)); + + ctx->id_auth->auth_key_algo = igvm_id->author_key_algorithm; +memcpy (ctx->id_auth->id_key_sig, &igvm_id->author_key_signature, +sizeof (igvm_id->author_key_signature)); + +/* +* SEV and IGVM public key structure population are slightly different. +* See SEV Secure Nested Paging Firmware ABI Specification, Chapter 10. +*/ + *((uint32_t *)ctx->id_auth->id_key) = igvm_id->id_public_key.curve; +memcpy (&ctx->id_auth->id_key[4 ], &igvm_id->id_public_key.qx,72 ); +memcpy (&ctx->id_auth->id_key[76 ], &igvm_id->id_public_key.qy,72 ); + + *((uint32_t *)ctx->id_auth->author_key) = + igvm_id->author_public_key.curve; +memcpy (&ctx->id_auth->author_key[4 ], &igvm_id->author_public_key.qx, +72 ); +memcpy (&ctx->id_auth->author_key[76 ], &igvm_id->author_public_key.qy, +72 ); + +return 0 ; +} + +static int qigvm_initialization_guest_policy (QIgvm *ctx, +const uint8_t *header_data,Error **errp) +{ +const IGVM_VHS_GUEST_POLICY *guest = + (const IGVM_VHS_GUEST_POLICY *)header_data; + +if (guest->compatibility_mask & ctx->compatibility_mask) { + ctx->sev_policy = guest->policy; + } +return 0 ; +} +static int qigvm_supported_platform_compat_mask (QIgvm *ctx,Error **errp) {int32_t header_count;
@@ -702,12 +813,16 @@ static int qigvm_supported_platform_compat_mask(QIgvm *ctx, Error **errp)/* Choose the strongest supported isolation technology */ if (compatibility_mask_sev_snp !=0 ) { ctx->compatibility_mask = compatibility_mask_sev_snp; + ctx->platform_type =IGVM_PLATFORM_TYPE_SEV_SNP ; }else if (compatibility_mask_sev_es !=0 ) { ctx->compatibility_mask = compatibility_mask_sev_es; + ctx->platform_type =IGVM_PLATFORM_TYPE_SEV_ES ; }else if (compatibility_mask_sev !=0 ) { ctx->compatibility_mask = compatibility_mask_sev; + ctx->platform_type =IGVM_PLATFORM_TYPE_SEV ; }else if (compatibility_mask !=0 ) { ctx->compatibility_mask = compatibility_mask; + ctx->platform_type =IGVM_PLATFORM_TYPE_NATIVE ; }else { error_setg( errp,
@@ -717,6 +832,23 @@ static int qigvm_supported_platform_compat_mask(QIgvm *ctx, Error **errp)return 0 ; } +static int qigvm_handle_policy (QIgvm *ctx,Error **errp) +{ +if (ctx->platform_type ==IGVM_PLATFORM_TYPE_SEV_SNP ) { +int id_block_len =0 ; +int id_auth_len =0 ; +if (ctx->id_block) { + ctx->id_block->policy = ctx->sev_policy; + id_block_len =sizeof (struct sev_id_block ); + id_auth_len =sizeof (struct sev_id_authentication ); + } +return ctx->cgsc->set_guest_policy (GUEST_POLICY_SEV , ctx->sev_policy, + ctx->id_block, id_block_len, + ctx->id_auth, id_auth_len, errp); + } +return 0 ; +} +static IgvmHandle qigvm_file_init (char *filename,Error **errp) {IgvmHandle igvm;
@@ -815,12 +947,18 @@ int qigvm_process_file(IgvmCfg *cfg, ConfidentialGuestSupport *cgs,*/ retval =qigvm_process_mem_page (&ctx,NULL , errp); +if (retval ==0 ) { + retval =qigvm_handle_policy (&ctx, errp); + } + cleanup_parameters: QTAILQ_FOREACH(parameter, &ctx.parameter_data, next) {g_free (parameter->data); parameter->data =NULL ; } +g_free (ctx.id_block); +g_free (ctx.id_auth); cleanup:igvm_free (ctx.file);
Signature · 1 line
2.43.0
PATCH v7 15/16 i386/sev: Add implementation of CGS set_guest_policy()
In reply to Roy HopkinsThe new cgs_set_guest_policy() function is provided to receive the guest policy flags, SNP ID block and SNP ID authentication from guest configuration such as an IGVM file and apply it to the platform prior to launching the guest.
The policy is used to populate values for the existing 'policy', 'id_block' and 'id_auth' parameters. When provided, the guest policy is applied and the ID block configuration is used to verify the launch measurement and signatures. The guest is only successfully started if the expected launch measurements match the actual measurements and the signatures are valid.
--- target/i386/sev.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++ target/i386/sev.h | 12 +++++++ 2 files changed, 95 insertions(+)
target/i386/sev.c +83 -0
@@ -2526,6 +2526,88 @@ static int cgs_get_mem_map_entry(int index,return 0 ; } +static int cgs_set_guest_policy (ConfidentialGuestPolicyType policy_type, +uint64_t policy,void *policy_data1, +uint32_t policy_data1_size,void *policy_data2, +uint32_t policy_data2_size,Error **errp) +{ +if (policy_type !=GUEST_POLICY_SEV ) { +error_setg (errp,"%s: Invalid guest policy type provided for SEV: %d" , + __func__, policy_type); +return -1 ; + } +/* +* SEV-SNP handles policy differently. The policy flags are defined in +* kvm_start_conf.policy and an ID block and ID auth can be provided. +*/ +if (sev_snp_enabled ()) { +SevSnpGuestState *sev_snp_guest = +SEV_SNP_GUEST (MACHINE (qdev_get_machine ())->cgs); +struct kvm_sev_snp_launch_finish *finish = + &sev_snp_guest->kvm_finish_conf; + +/* +* The policy consists of flags in 'policy' and optionally an ID block +* and ID auth in policy_data1 and policy_data2 respectively. The ID +* block and auth are optional so clear any previous ID block and auth +* and set them if provided, but always set the policy flags. +*/ +g_free (sev_snp_guest->id_block); +g_free ((guchar *)finish->id_block_uaddr); +g_free (sev_snp_guest->id_auth); +g_free ((guchar *)finish->id_auth_uaddr); + sev_snp_guest->id_block =NULL ; + finish->id_block_uaddr =0 ; + sev_snp_guest->id_auth =NULL ; + finish->id_auth_uaddr =0 ; + +if (policy_data1_size >0 ) { +struct sev_snp_id_authentication *id_auth = + (struct sev_snp_id_authentication *)policy_data2; + +if (policy_data1_size !=KVM_SEV_SNP_ID_BLOCK_SIZE ) { +error_setg (errp,"%s: Invalid SEV-SNP ID block: incorrect size" , + __func__); +return -1 ; + } +if (policy_data2_size !=KVM_SEV_SNP_ID_AUTH_SIZE ) { +error_setg (errp, +"%s: Invalid SEV-SNP ID auth block: incorrect size" , + __func__); +return -1 ; + } +assert (policy_data1 !=NULL ); +assert (policy_data2 !=NULL ); + + finish->id_block_uaddr = + (__u64 )g_memdup2 (policy_data1,KVM_SEV_SNP_ID_BLOCK_SIZE ); + finish->id_auth_uaddr = + (__u64 )g_memdup2 (policy_data2,KVM_SEV_SNP_ID_AUTH_SIZE ); + +/* +* Check if an author key has been provided and use that to flag +* whether the author key is enabled. The first of the author key +* must be non-zero to indicate the key type, which will currently +* always be 2. +*/ + sev_snp_guest->kvm_finish_conf.auth_key_en = + id_auth->author_key[0 ] ?1 :0 ; + finish->id_block_en =1 ; + } + sev_snp_guest->kvm_start_conf.policy = policy; + }else { +SevGuestState *sev_guest =SEV_GUEST (MACHINE (qdev_get_machine ())->cgs); +/* Only the policy flags are supported for SEV and SEV-ES */ +if ((policy_data1_size >0 ) || (policy_data2_size >0 ) || !sev_guest) { +error_setg (errp,"%s: An ID block/ID auth block has been provided " +"but SEV-SNP is not enabled" , __func__); +return -1 ; + } + sev_guest->policy = policy; + } +return 0 ; +} +static void sev_common_class_init (ObjectClass *oc,void *data) {
@@ -2564,6 +2646,7 @@ sev_common_instance_init(Object *obj) cgs->check_support = cgs_check_support; cgs->set_guest_state = cgs_set_guest_state; cgs->get_mem_map_entry = cgs_get_mem_map_entry; + cgs->set_guest_policy = cgs_set_guest_policy;QTAILQ_INIT (&sev_common->launch_vmsa); }
target/i386/sev.h +12 -0
@@ -167,6 +167,18 @@ struct QEMU_PACKED sev_es_save_area {uint8_t fpreg_ymm[256 ]; }; +struct QEMU_PACKED sev_snp_id_authentication { +uint32_t id_key_alg; +uint32_t auth_key_algo; +uint8_t reserved[56 ]; +uint8_t id_block_sig[512 ]; +uint8_t id_key[1028 ]; +uint8_t reserved2[60 ]; +uint8_t id_key_sig[512 ]; +uint8_t author_key[1028 ]; +uint8_t reserved3[892 ]; +}; +bool sev_add_kernel_loader_hashes (SevKernelLoaderContext *ctx,Error **errp);int sev_encrypt_flash (hwaddr gpa,uint8_t *ptr,uint64_t len,Error **errp);
Signature · 1 line
2.43.0
Hi Roy,
Quoted 2 lines · Roy Hopkins
Here is v7 of the set of patches to add support for IGVM files to QEMU. This is based on commit
40efe733e10cc00e4fb4f9f5790a28e744e63c62of qemu.
Thanks again for this work!
I noticed that the last patch for this series is missing, also patchew didn't receive it:
https://patchew.org/QEMU/[email protected]/
If you're using git-publish you can do:
$ git publish --skip 16 -S \
-R [email protected]Thanks,
Stefano
Quoted 102 lines
Firstly, apologies for the amount of time between the last version and this one. I moved roles to a different company and, although I always planned to see this patch series to completion, it took a while before I found time to setup a development environment and be in a position to send a new version. I will continue this series using a personal email address for now, hence the change to the author and signed-off-by emails.
The only changes in this version are to rebase on the current master branch and update commit metadata, including Signed-Off-By and Author emails for my replacement email address, and to include the final Reviewed-By that were added in the last review. There were no requested changes on the previous version [1] so I believe this series is ready to merge.
As always, thanks to those that have been following along, reviewing and testing this series. This v7 patch series is also available on github: [2]
For testing IGVM support in QEMU you need to generate an IGVM file that is configured for the platform you want to launch. You can use the
buildigvmtest tool [3] to allow generation of IGVM files for all currently supported platforms. Patch 11/17 contains information on how to generate an IGVM file using this tool.Changes in v7:
* Update version numbers for IGVM support to 10.0
* Add Reviewed-by to relevant commits.
* Update Author email and sign-offs to my new email addressPatch summary:
1-11: Add support and documentation for processing IGVM files for SEV, SEV-ES, SEV-SNP and native platforms.
12-15: Processing of policy and SEV-SNP
ID_BLOCKfrom IGVM file.16: Add pre-processing of IGVM file to support synchronization of '
SEV_FEATURES' from IGVM VMSA to KVM.[1] Link to v6:
https://lore.kernel.org/qemu-devel/[email protected]/[2] v7 patches also available here:
https://github.com/roy-hopkins/qemu/tree/igvm_master_v7[3]
buildigvmtool v0.2.0
https://github.com/roy-hopkins/buildigvm/releases/tag/v0.2.0Roy Hopkins (16):
meson: Add optional dependency on IGVM library backends/confidential-guest-support: Add functions to support IGVM backends/igvm: Add IGVM loader and configuration
hw/i386: Add igvm-cfg object and processing for IGVM files i386/pc_sysfw: Ensure sysfw flash configuration does not conflict with
IGVM
sev: Updatelaunch_update_datafunctions to use Error handling target/i386: Allow setting ofR_LDTRandR_TRwith
cpu_x86_load_seg_cache()
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 IGVMdocs/interop/firmware.json: Add igvm to FirmwareDevice backends/confidential-guest-support: Addset_guest_policy()function backends/igvm: Process initialization sections in IGVM file backends/igvm: Handle policy for SEV guests
i386/sev: Add implementation of CGSset_guest_policy()
sev: Providesev_featuresflags from IGVM VMSA toKVM_SEV_INIT2backends/confidential-guest-support.c | 43 + backends/igvm-cfg.c | 52 ++ backends/igvm.c | 967 ++++++++++++++++++++ backends/igvm.h | 23 + backends/meson.build | 5 + docs/interop/firmware.json | 30 +- docs/system/i386/amd-memory-encryption.rst | 2 + docs/system/igvm.rst | 173 ++++ docs/system/index.rst | 1 + hw/i386/pc.c | 12 + hw/i386/pc_piix.c | 10 + hw/i386/pc_q35.c | 10 + hw/i386/pc_sysfw.c | 31 +- include/hw/i386/x86.h | 3 + include/system/confidential-guest-support.h | 88 ++ include/system/igvm-cfg.h | 47 + meson.build | 8 + meson_options.txt | 2 + qapi/qom.json | 17 + qemu-options.hx | 28 + scripts/meson-buildoptions.sh | 3 + target/i386/cpu.h | 9 +- target/i386/sev.c | 850 +++++++++++++++-- target/i386/sev.h | 124 +++ 24 files changed, 2454 insertions(+), 84 deletions(-)create mode 100644
backends/igvm-cfg.c
create mode 100644backends/igvm.c
create mode 100644backends/igvm.h
create mode 100644docs/system/igvm.rst
create mode 100644include/system/igvm-cfg.hSignature · 1 line
2.43.0
Quoted 15 lines · Stefano Garzarella
Hi Roy,
Quoted 3 lines · Roy Hopkins
Here is v7 of the set of patches to add support for IGVM files to QEMU. This is
based on commit40efe733e10cc00e4fb4f9f5790a28e744e63c62of qemu.Thanks again for this work!
I noticed that the last patch for this series is missing, also patchew
didn't receive it:
Quoted 8 lines
If you're using git-publish you can do:
$ git publish --skip 16 -S \ -R [email protected]Thanks,
Stefano
Thanks Stefano. I had all sorts of problems getting git send-mail to send using my mail service provider. I've sorted it now and sent the missing patch.
Regards,
Roy
Quoted 122 lines
Quoted 121 lines
Firstly, apologies for the amount of time between the last version and this one.
I moved roles to a different company and, although I always planned to see this
patch series to completion, it took a while before I found time to setup a
development environment and be in a position to send a new version. I will
continue this series using a personal email address for now, hence the change
to the author and signed-off-by emails.The only changes in this version are to rebase on the current master branch and
update commit metadata, including Signed-Off-By and Author emails for my
replacement email address, and to include the final Reviewed-By that were added
in the last review. There were no requested changes on the previous version [1]
so I believe this series is ready to merge.As always, thanks to those that have been following along, reviewing and testing
this series. This v7 patch series is also available on github: [2]For testing IGVM support in QEMU you need to generate an IGVM file that is
configured for the platform you want to launch. You can use thebuildigvm
test tool [3] to allow generation of IGVM files for all currently supported
platforms. Patch 11/17 contains information on how to generate an IGVM file
using this tool.Changes in v7:
* Update version numbers for IGVM support to 10.0
* Add Reviewed-by to relevant commits.
* Update Author email and sign-offs to my new email addressPatch summary:
1-11: Add support and documentation for processing IGVM files for SEV, SEV-ES,
SEV-SNP and native platforms.12-15: Processing of policy and SEV-SNP
ID_BLOCKfrom IGVM file.16: Add pre-processing of IGVM file to support synchronization of '
SEV_FEATURES'
from IGVM VMSA to KVM.[1] Link to v6:
https://lore.kernel.org/qemu-devel/[email protected]/[2] v7 patches also available here:
https://github.com/roy-hopkins/qemu/tree/igvm_master_v7[3]
buildigvmtool v0.2.0
https://github.com/roy-hopkins/buildigvm/releases/tag/v0.2.0Roy Hopkins (16):
meson: Add optional dependency on IGVM library backends/confidential-guest-support: Add functions to support IGVM backends/igvm: Add IGVM loader and configuration hw/i386: Add igvm-cfg object and processing for IGVM files i386/pc_sysfw: Ensure sysfw flash configuration does not conflict with
IGVM
sev: Updatelaunch_update_datafunctions to use Error handling target/i386: Allow setting ofR_LDTRandR_TRwithcpu_x86_load_seg_cache()
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 IGVMdocs/interop/firmware.json: Add igvm to FirmwareDevice backends/confidential-guest-support: Addset_guest_policy()function
backends/igvm: Process initialization sections in IGVM file backends/igvm: Handle policy for SEV guests
i386/sev: Add implementation of CGSset_guest_policy()
sev: Providesev_featuresflags from IGVM VMSA toKVM_SEV_INIT2backends/confidential-guest-support.c | 43 + backends/igvm-cfg.c | 52 ++ backends/igvm.c | 967
++++++++++++++++++++
backends/igvm.h | 23 + backends/meson.build | 5 + docs/interop/firmware.json | 30 +- docs/system/i386/amd-memory-encryption.rst | 2 + docs/system/igvm.rst | 173 ++++ docs/system/index.rst | 1 + hw/i386/pc.c | 12 + hw/i386/pc_piix.c | 10 + hw/i386/pc_q35.c | 10 + hw/i386/pc_sysfw.c | 31 +- include/hw/i386/x86.h | 3 +include/system/confidential-guest-support.h| 88 ++ include/system/igvm-cfg.h | 47 + meson.build | 8 + meson_options.txt | 2 + qapi/qom.json | 17 + qemu-options.hx | 28 + scripts/meson-buildoptions.sh | 3 +target/i386/cpu.h | 9 +- target/i386/sev.c | 850 +++++++++++++++-- target/i386/sev.h | 124 +++ 24 files changed, 2454 insertions(+), 84 deletions(-)create mode 100644
backends/igvm-cfg.c
create mode 100644backends/igvm.c
create mode 100644backends/igvm.h
create mode 100644docs/system/igvm.rstcreate mode 100644include/system/igvm-cfg.hSignature · 1 line
2.43.0
Quoted 22 lines · Roy Hopkins
Quoted 20 lines · Stefano Garzarella
Hi Roy,
Quoted 3 lines · Roy Hopkins
Here is v7 of the set of patches to add support for IGVM files to QEMU. This is
based on commit40efe733e10cc00e4fb4f9f5790a28e744e63c62of qemu.Thanks again for this work!
I noticed that the last patch for this series is missing, also patchew
didn't receive it:https://patchew.org/QEMU/[email protected]/
If you're using git-publish you can do:
$ git publish --skip 16 -S \ -R [email protected]
Quoted 7 lines
Quoted 3 lines
Thanks,
StefanoThanks Stefano. I had all sorts of problems getting git send-mail to send using my mail service provider. I've sorted it now and sent the missing patch.
Yeah, now the other attempts of patch 16 have also arrived xD
On Monday I'll go through the series, test it and come back!
Thanks,
Stefano
Quoted 127 lines
Regards,
RoyQuoted 122 lines
Quoted 121 lines
Firstly, apologies for the amount of time between the last version and this one.
I moved roles to a different company and, although I always planned to see this
patch series to completion, it took a while before I found time to setup a
development environment and be in a position to send a new version. I will
continue this series using a personal email address for now, hence the change
to the author and signed-off-by emails.The only changes in this version are to rebase on the current master branch and
update commit metadata, including Signed-Off-By and Author emails for my
replacement email address, and to include the final Reviewed-By that were added
in the last review. There were no requested changes on the previous version [1]
so I believe this series is ready to merge.As always, thanks to those that have been following along, reviewing and testing
this series. This v7 patch series is also available on github: [2]For testing IGVM support in QEMU you need to generate an IGVM file that is
configured for the platform you want to launch. You can use thebuildigvm
test tool [3] to allow generation of IGVM files for all currently supported
platforms. Patch 11/17 contains information on how to generate an IGVM file
using this tool.Changes in v7:
* Update version numbers for IGVM support to 10.0
* Add Reviewed-by to relevant commits.
* Update Author email and sign-offs to my new email addressPatch summary:
1-11: Add support and documentation for processing IGVM files for SEV, SEV-ES,
SEV-SNP and native platforms.12-15: Processing of policy and SEV-SNP
ID_BLOCKfrom IGVM file.16: Add pre-processing of IGVM file to support synchronization of '
SEV_FEATURES'
from IGVM VMSA to KVM.[1] Link to v6:
https://lore.kernel.org/qemu-devel/[email protected]/[2] v7 patches also available here:
https://github.com/roy-hopkins/qemu/tree/igvm_master_v7[3]
buildigvmtool v0.2.0
https://github.com/roy-hopkins/buildigvm/releases/tag/v0.2.0Roy Hopkins (16):
meson: Add optional dependency on IGVM library backends/confidential-guest-support: Add functions to support IGVM backends/igvm: Add IGVM loader and configuration hw/i386: Add igvm-cfg object and processing for IGVM files i386/pc_sysfw: Ensure sysfw flash configuration does not conflict
with
IGVM
sev: Updatelaunch_update_datafunctions to use Error handling target/i386: Allow setting ofR_LDTRandR_TRwith
cpu_x86_load_seg_cache()
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 IGVMdocs/interop/firmware.json: Add igvm to FirmwareDevice backends/confidential-guest-support: Addset_guest_policy()
function
backends/igvm: Process initialization sections in IGVM file backends/igvm: Handle policy for SEV guests
i386/sev: Add implementation of CGSset_guest_policy()
sev: Providesev_featuresflags from IGVM VMSA toKVM_SEV_INIT2backends/confidential-guest-support.c | 43 + backends/igvm-cfg.c | 52 ++ backends/igvm.c | 967 ++++++++++++++++++++ backends/igvm.h | 23 + backends/meson.build | 5 + docs/interop/firmware.json | 30 +- docs/system/i386/amd-memory-encryption.rst | 2 + docs/system/igvm.rst | 173 ++++ docs/system/index.rst | 1 + hw/i386/pc.c | 12 + hw/i386/pc_piix.c | 10 + hw/i386/pc_q35.c | 10 + hw/i386/pc_sysfw.c | 31 +- include/hw/i386/x86.h | 3 + include/system/confidential-guest-support.h | 88 ++ include/system/igvm-cfg.h | 47 + meson.build | 8 + meson_options.txt | 2 + qapi/qom.json | 17 + qemu-options.hx | 28 + scripts/meson-buildoptions.sh | 3 + target/i386/cpu.h | 9 +- target/i386/sev.c | 850 +++++++++++++++-- target/i386/sev.h | 124 +++ 24 files changed, 2454 insertions(+), 84 deletions(-)create mode 100644
backends/igvm-cfg.c
create mode 100644backends/igvm.c
create mode 100644backends/igvm.h
create mode 100644docs/system/igvm.rst
create mode 100644include/system/igvm-cfg.h--
2.43.0
PATCH v7 16/16 sev: Provide sev_features flags from IGVM VMSA to KVM_SEV_INIT2
In reply to Roy HopkinsIGVM files can contain an initial VMSA that should be applied to each vcpu as part of the initial guest state. The sev_features flags are provided as part of the VMSA structure. However, KVM only allows sev_features to be set during initialization and not as the guest is being prepared for launch.
This patch queries KVM for the supported set of sev_features flags and processes the IGVM file during kvm_init to determine any sev_features flags set in the IGVM file. These are then provided in the call to KVM_SEV_INIT2 to ensure the guest state matches that specified in the IGVM file.
This does cause the IGVM file to be processed twice. Firstly to extract the sev_features then secondly to actually configure the guest. However, the first pass is largely ignored meaning the overhead is minimal.
--- target/i386/sev.c | 160 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 141 insertions(+), 19 deletions(-)
target/i386/sev.c +141 -19
@@ -117,6 +117,8 @@ struct SevCommonState {uint32_t cbitpos;uint32_t reduced_phys_bits;bool kernel_hashes; +uint64_t sev_features; +uint64_t supported_sev_features;/* runtime state */ uint8_t api_major;
@@ -492,7 +494,40 @@ static void sev_apply_cpu_context(CPUState *cpu) } } -static int check_vmsa_supported (hwaddr gpa,const struct sev_es_save_area *vmsa, +static int check_sev_features (SevCommonState *sev_common,uint64_t sev_features, +Error **errp) +{ +/* +* Ensure SEV_FEATURES is configured for correct SEV hardware and that +* the requested features are supported. If SEV-SNP is enabled then +* that feature must be enabled, otherwise it must be cleared. +*/ +if (sev_snp_enabled () && !(sev_features &SVM_SEV_FEAT_SNP_ACTIVE )) { +error_setg ( + errp, +"%s: SEV_SNP is enabled but is not enabled in VMSA sev_features" , + __func__); +return -1 ; + }else if (!sev_snp_enabled () && + (sev_features &SVM_SEV_FEAT_SNP_ACTIVE )) { +error_setg ( + errp, +"%s: SEV_SNP is not enabled but is enabled in VMSA sev_features" , + __func__); +return -1 ; + } +if (sev_features & ~sev_common->supported_sev_features) { +error_setg (errp, +"%s: VMSA contains unsupported sev_features: %lX, " +"supported features: %lX" , + __func__, sev_features, sev_common->supported_sev_features); +return -1 ; + } +return 0 ; +} + +static int check_vmsa_supported (SevCommonState *sev_common,hwaddr gpa, +const struct sev_es_save_area *vmsa,Error **errp) {struct sev_es_save_area vmsa_check;
@@ -558,24 +593,10 @@ static int check_vmsa_supported(hwaddr gpa, const struct sev_es_save_area *vmsa, vmsa_check.x87_fcw =0 ; vmsa_check.mxcsr =0 ; -if (sev_snp_enabled ()) { -if (vmsa_check.sev_features !=SVM_SEV_FEAT_SNP_ACTIVE ) { -error_setg (errp, -"%s: sev_features in the VMSA contains an unsupported " -"value. For SEV-SNP, sev_features must be set to %x." , - __func__,SVM_SEV_FEAT_SNP_ACTIVE ); -return -1 ; - } - vmsa_check.sev_features =0 ; - }else { -if (vmsa_check.sev_features !=0 ) { -error_setg (errp, -"%s: sev_features in the VMSA contains an unsupported " -"value. For SEV-ES and SEV, sev_features must be " -"set to 0." , __func__); -return -1 ; - } +if (check_sev_features (sev_common, vmsa_check.sev_features, errp) <0 ) { +return -1 ; } + vmsa_check.sev_features =0 ;if (!buffer_is_zero (&vmsa_check,sizeof (vmsa_check))) { error_setg(errp,
@@ -1729,6 +1750,39 @@ static int sev_snp_kvm_type(X86ConfidentialGuest *cg)return KVM_X86_SNP_VM ; } +static int sev_init_supported_features (ConfidentialGuestSupport *cgs, +SevCommonState *sev_common,Error **errp) +{ +X86ConfidentialGuestClass *x86_klass = +X86_CONFIDENTIAL_GUEST_GET_CLASS (cgs); +/* +* Older kernels do not support query or setting of sev_features. In this +* case the set of supported features must be zero to match the settings +* in the kernel. +*/ +if (x86_klass->kvm_type (X86_CONFIDENTIAL_GUEST (sev_common)) == +KVM_X86_DEFAULT_VM ) { + sev_common->supported_sev_features =0 ; +return 0 ; + } + +/* Query KVM for the supported set of sev_features */ +struct kvm_device_attr attr = { + .group =KVM_X86_GRP_SEV , + .attr =KVM_X86_SEV_VMSA_FEATURES , + .addr = (unsigned long )&sev_common->supported_sev_features, + }; +if (kvm_ioctl (kvm_state,KVM_GET_DEVICE_ATTR , &attr) <0 ) { +error_setg (errp,"%s: failed to query supported sev_features" , + __func__); +return -1 ; + } +if (sev_snp_enabled ()) { + sev_common->supported_sev_features |=SVM_SEV_FEAT_SNP_ACTIVE ; + } +return 0 ; +} +static int sev_common_kvm_init (ConfidentialGuestSupport *cgs,Error **errp) {char *devname;
@@ -1809,6 +1863,10 @@ static int sev_common_kvm_init(ConfidentialGuestSupport *cgs, Error **errp) } } +if (sev_init_supported_features (cgs, sev_common, errp) <0 ) { +return -1 ; + } +trace_kvm_sev_init ();switch (x86_klass->kvm_type (X86_CONFIDENTIAL_GUEST (sev_common))) {case KVM_X86_DEFAULT_VM :
@@ -1820,6 +1878,39 @@ static int sev_common_kvm_init(ConfidentialGuestSupport *cgs, Error **errp)case KVM_X86_SEV_ES_VM :case KVM_X86_SNP_VM : {struct kvm_sev_init args = {0 }; +MachineState *machine =MACHINE (qdev_get_machine ()); +X86MachineState *x86machine =X86_MACHINE (qdev_get_machine ()); + +/* +* If configuration is provided via an IGVM file then the IGVM file +* might contain configuration of the initial vcpu context. For SEV +* the vcpu context includes the sev_features which should be applied +* to the vcpu. +* +* KVM does not synchronize sev_features from CPU state. Instead it +* requires sev_features to be provided as part of this initialization +* call which is subsequently automatically applied to the VMSA of +* each vcpu. +* +* The IGVM file is normally processed after initialization. Therefore +* we need to pre-process it here to extract sev_features in order to +* provide it to KVM_SEV_INIT2. Each cgs_* function that is called by +* the IGVM processor detects this pre-process by observing the state +* as SEV_STATE_UNINIT. +*/ +if (x86machine->igvm) { +if (IGVM_CFG_GET_CLASS (x86machine->igvm) + ->process (x86machine->igvm, machine->cgs, errp) ==-1 ) { +return -1 ; + } +/* +* KVM maintains a bitmask of allowed sev_features. This does not +* include SVM_SEV_FEAT_SNP_ACTIVE which is set accordingly by KVM +* itself. Therefore we need to clear this flag. +*/ + args.vmsa_features = sev_common->sev_features & + ~SVM_SEV_FEAT_SNP_ACTIVE ; + } ret =sev_ioctl (sev_common->sev_fd,KVM_SEV_INIT2 , &args, &fw_error);break ;
@@ -2424,6 +2515,24 @@ static int cgs_set_guest_state(hwaddr gpa, uint8_t *ptr, uint64_t len,SevCommonState *sev_common =SEV_COMMON (MACHINE (qdev_get_machine ())->cgs);SevCommonStateClass *klass =SEV_COMMON_GET_CLASS (sev_common); +if (sev_common->state ==SEV_STATE_UNINIT ) { +/* Pre-processing of IGVM file called from sev_common_kvm_init() */ +if ((cpu_index ==0 ) && (memory_type ==CGS_PAGE_TYPE_VMSA )) { +const struct sev_es_save_area *sa = + (const struct sev_es_save_area *)ptr; +if (len <sizeof (*sa)) { +error_setg (errp,"%s: invalid VMSA length encountered" , + __func__); +return -1 ; + } +if (check_sev_features (sev_common, sa->sev_features, errp) <0 ) { +return -1 ; + } + sev_common->sev_features = sa->sev_features; + } +return 0 ; + } +if (!sev_enabled ()) {error_setg (errp,"%s: attempt to configure guest memory, but SEV " "is not enabled" , __func__);
@@ -2443,7 +2552,8 @@ static int cgs_set_guest_state(hwaddr gpa, uint8_t *ptr, uint64_t len, __func__);return -1 ; } -if (check_vmsa_supported (gpa, (const struct sev_es_save_area *)ptr, +if (check_vmsa_supported (sev_common, gpa, + (const struct sev_es_save_area *)ptr, errp) <0 ) {return -1 ; }
@@ -2500,6 +2610,12 @@ static int cgs_get_mem_map_entry(int index,struct e820_entry *table;int num_entries; +SevCommonState *sev_common =SEV_COMMON (MACHINE (qdev_get_machine ())->cgs); +if (sev_common->state ==SEV_STATE_UNINIT ) { +/* Pre-processing of IGVM file called from sev_common_kvm_init() */ +return 1 ; + } + num_entries =e820_get_table (&table);if ((index <0 ) || (index >= num_entries)) {return 1 ;
@@ -2531,6 +2647,12 @@ static int cgs_set_guest_policy(ConfidentialGuestPolicyType policy_type, uint32_t policy_data1_size, void *policy_data2, uint32_t policy_data2_size, Error **errp) { + SevCommonState *sev_common = SEV_COMMON(MACHINE(qdev_get_machine())->cgs); +if (sev_common->state ==SEV_STATE_UNINIT ) { +/* Pre-processing of IGVM file called from sev_common_kvm_init() */ +return 0 ; + } +if (policy_type !=GUEST_POLICY_SEV ) {error_setg (errp,"%s: Invalid guest policy type provided for SEV: %d" , __func__, policy_type);
Signature · 1 line
2.43.0
PATCH v7 16/16 sev: Provide sev_features flags from IGVM VMSA to KVM_SEV_INIT2
In reply to Roy HopkinsIGVM files can contain an initial VMSA that should be applied to each vcpu as part of the initial guest state. The sev_features flags are provided as part of the VMSA structure. However, KVM only allows sev_features to be set during initialization and not as the guest is being prepared for launch.
This patch queries KVM for the supported set of sev_features flags and processes the IGVM file during kvm_init to determine any sev_features flags set in the IGVM file. These are then provided in the call to KVM_SEV_INIT2 to ensure the guest state matches that specified in the IGVM file.
This does cause the IGVM file to be processed twice. Firstly to extract the sev_features then secondly to actually configure the guest. However, the first pass is largely ignored meaning the overhead is minimal.
--- target/i386/sev.c | 160 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 141 insertions(+), 19 deletions(-)
target/i386/sev.c +141 -19
@@ -117,6 +117,8 @@ struct SevCommonState {uint32_t cbitpos;uint32_t reduced_phys_bits;bool kernel_hashes; +uint64_t sev_features; +uint64_t supported_sev_features;/* runtime state */ uint8_t api_major;
@@ -492,7 +494,40 @@ static void sev_apply_cpu_context(CPUState *cpu) } } -static int check_vmsa_supported (hwaddr gpa,const struct sev_es_save_area *vmsa, +static int check_sev_features (SevCommonState *sev_common,uint64_t sev_features, +Error **errp) +{ +/* +* Ensure SEV_FEATURES is configured for correct SEV hardware and that +* the requested features are supported. If SEV-SNP is enabled then +* that feature must be enabled, otherwise it must be cleared. +*/ +if (sev_snp_enabled () && !(sev_features &SVM_SEV_FEAT_SNP_ACTIVE )) { +error_setg ( + errp, +"%s: SEV_SNP is enabled but is not enabled in VMSA sev_features" , + __func__); +return -1 ; + }else if (!sev_snp_enabled () && + (sev_features &SVM_SEV_FEAT_SNP_ACTIVE )) { +error_setg ( + errp, +"%s: SEV_SNP is not enabled but is enabled in VMSA sev_features" , + __func__); +return -1 ; + } +if (sev_features & ~sev_common->supported_sev_features) { +error_setg (errp, +"%s: VMSA contains unsupported sev_features: %lX, " +"supported features: %lX" , + __func__, sev_features, sev_common->supported_sev_features); +return -1 ; + } +return 0 ; +} + +static int check_vmsa_supported (SevCommonState *sev_common,hwaddr gpa, +const struct sev_es_save_area *vmsa,Error **errp) {struct sev_es_save_area vmsa_check;
@@ -558,24 +593,10 @@ static int check_vmsa_supported(hwaddr gpa, const struct sev_es_save_area *vmsa, vmsa_check.x87_fcw =0 ; vmsa_check.mxcsr =0 ; -if (sev_snp_enabled ()) { -if (vmsa_check.sev_features !=SVM_SEV_FEAT_SNP_ACTIVE ) { -error_setg (errp, -"%s: sev_features in the VMSA contains an unsupported " -"value. For SEV-SNP, sev_features must be set to %x." , - __func__,SVM_SEV_FEAT_SNP_ACTIVE ); -return -1 ; - } - vmsa_check.sev_features =0 ; - }else { -if (vmsa_check.sev_features !=0 ) { -error_setg (errp, -"%s: sev_features in the VMSA contains an unsupported " -"value. For SEV-ES and SEV, sev_features must be " -"set to 0." , __func__); -return -1 ; - } +if (check_sev_features (sev_common, vmsa_check.sev_features, errp) <0 ) { +return -1 ; } + vmsa_check.sev_features =0 ;if (!buffer_is_zero (&vmsa_check,sizeof (vmsa_check))) { error_setg(errp,
@@ -1729,6 +1750,39 @@ static int sev_snp_kvm_type(X86ConfidentialGuest *cg)return KVM_X86_SNP_VM ; } +static int sev_init_supported_features (ConfidentialGuestSupport *cgs, +SevCommonState *sev_common,Error **errp) +{ +X86ConfidentialGuestClass *x86_klass = +X86_CONFIDENTIAL_GUEST_GET_CLASS (cgs); +/* +* Older kernels do not support query or setting of sev_features. In this +* case the set of supported features must be zero to match the settings +* in the kernel. +*/ +if (x86_klass->kvm_type (X86_CONFIDENTIAL_GUEST (sev_common)) == +KVM_X86_DEFAULT_VM ) { + sev_common->supported_sev_features =0 ; +return 0 ; + } + +/* Query KVM for the supported set of sev_features */ +struct kvm_device_attr attr = { + .group =KVM_X86_GRP_SEV , + .attr =KVM_X86_SEV_VMSA_FEATURES , + .addr = (unsigned long )&sev_common->supported_sev_features, + }; +if (kvm_ioctl (kvm_state,KVM_GET_DEVICE_ATTR , &attr) <0 ) { +error_setg (errp,"%s: failed to query supported sev_features" , + __func__); +return -1 ; + } +if (sev_snp_enabled ()) { + sev_common->supported_sev_features |=SVM_SEV_FEAT_SNP_ACTIVE ; + } +return 0 ; +} +static int sev_common_kvm_init (ConfidentialGuestSupport *cgs,Error **errp) {char *devname;
@@ -1809,6 +1863,10 @@ static int sev_common_kvm_init(ConfidentialGuestSupport *cgs, Error **errp) } } +if (sev_init_supported_features (cgs, sev_common, errp) <0 ) { +return -1 ; + } +trace_kvm_sev_init ();switch (x86_klass->kvm_type (X86_CONFIDENTIAL_GUEST (sev_common))) {case KVM_X86_DEFAULT_VM :
@@ -1820,6 +1878,39 @@ static int sev_common_kvm_init(ConfidentialGuestSupport *cgs, Error **errp)case KVM_X86_SEV_ES_VM :case KVM_X86_SNP_VM : {struct kvm_sev_init args = {0 }; +MachineState *machine =MACHINE (qdev_get_machine ()); +X86MachineState *x86machine =X86_MACHINE (qdev_get_machine ()); + +/* +* If configuration is provided via an IGVM file then the IGVM file +* might contain configuration of the initial vcpu context. For SEV +* the vcpu context includes the sev_features which should be applied +* to the vcpu. +* +* KVM does not synchronize sev_features from CPU state. Instead it +* requires sev_features to be provided as part of this initialization +* call which is subsequently automatically applied to the VMSA of +* each vcpu. +* +* The IGVM file is normally processed after initialization. Therefore +* we need to pre-process it here to extract sev_features in order to +* provide it to KVM_SEV_INIT2. Each cgs_* function that is called by +* the IGVM processor detects this pre-process by observing the state +* as SEV_STATE_UNINIT. +*/ +if (x86machine->igvm) { +if (IGVM_CFG_GET_CLASS (x86machine->igvm) + ->process (x86machine->igvm, machine->cgs, errp) ==-1 ) { +return -1 ; + } +/* +* KVM maintains a bitmask of allowed sev_features. This does not +* include SVM_SEV_FEAT_SNP_ACTIVE which is set accordingly by KVM +* itself. Therefore we need to clear this flag. +*/ + args.vmsa_features = sev_common->sev_features & + ~SVM_SEV_FEAT_SNP_ACTIVE ; + } ret =sev_ioctl (sev_common->sev_fd,KVM_SEV_INIT2 , &args, &fw_error);break ;
@@ -2424,6 +2515,24 @@ static int cgs_set_guest_state(hwaddr gpa, uint8_t *ptr, uint64_t len,SevCommonState *sev_common =SEV_COMMON (MACHINE (qdev_get_machine ())->cgs);SevCommonStateClass *klass =SEV_COMMON_GET_CLASS (sev_common); +if (sev_common->state ==SEV_STATE_UNINIT ) { +/* Pre-processing of IGVM file called from sev_common_kvm_init() */ +if ((cpu_index ==0 ) && (memory_type ==CGS_PAGE_TYPE_VMSA )) { +const struct sev_es_save_area *sa = + (const struct sev_es_save_area *)ptr; +if (len <sizeof (*sa)) { +error_setg (errp,"%s: invalid VMSA length encountered" , + __func__); +return -1 ; + } +if (check_sev_features (sev_common, sa->sev_features, errp) <0 ) { +return -1 ; + } + sev_common->sev_features = sa->sev_features; + } +return 0 ; + } +if (!sev_enabled ()) {error_setg (errp,"%s: attempt to configure guest memory, but SEV " "is not enabled" , __func__);
@@ -2443,7 +2552,8 @@ static int cgs_set_guest_state(hwaddr gpa, uint8_t *ptr, uint64_t len, __func__);return -1 ; } -if (check_vmsa_supported (gpa, (const struct sev_es_save_area *)ptr, +if (check_vmsa_supported (sev_common, gpa, + (const struct sev_es_save_area *)ptr, errp) <0 ) {return -1 ; }
@@ -2500,6 +2610,12 @@ static int cgs_get_mem_map_entry(int index,struct e820_entry *table;int num_entries; +SevCommonState *sev_common =SEV_COMMON (MACHINE (qdev_get_machine ())->cgs); +if (sev_common->state ==SEV_STATE_UNINIT ) { +/* Pre-processing of IGVM file called from sev_common_kvm_init() */ +return 1 ; + } + num_entries =e820_get_table (&table);if ((index <0 ) || (index >= num_entries)) {return 1 ;
@@ -2531,6 +2647,12 @@ static int cgs_set_guest_policy(ConfidentialGuestPolicyType policy_type, uint32_t policy_data1_size, void *policy_data2, uint32_t policy_data2_size, Error **errp) { + SevCommonState *sev_common = SEV_COMMON(MACHINE(qdev_get_machine())->cgs); +if (sev_common->state ==SEV_STATE_UNINIT ) { +/* Pre-processing of IGVM file called from sev_common_kvm_init() */ +return 0 ; + } +if (policy_type !=GUEST_POLICY_SEV ) {error_setg (errp,"%s: Invalid guest policy type provided for SEV: %d" , __func__, policy_type);
Signature · 1 line
2.43.0
PATCH v7 16/16 sev: Provide sev_features flags from IGVM VMSA to KVM_SEV_INIT2
In reply to Roy HopkinsIGVM files can contain an initial VMSA that should be applied to each vcpu as part of the initial guest state. The sev_features flags are provided as part of the VMSA structure. However, KVM only allows sev_features to be set during initialization and not as the guest is being prepared for launch.
This patch queries KVM for the supported set of sev_features flags and processes the IGVM file during kvm_init to determine any sev_features flags set in the IGVM file. These are then provided in the call to KVM_SEV_INIT2 to ensure the guest state matches that specified in the IGVM file.
This does cause the IGVM file to be processed twice. Firstly to extract the sev_features then secondly to actually configure the guest. However, the first pass is largely ignored meaning the overhead is minimal.
--- target/i386/sev.c | 160 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 141 insertions(+), 19 deletions(-)
target/i386/sev.c +141 -19
@@ -117,6 +117,8 @@ struct SevCommonState {uint32_t cbitpos;uint32_t reduced_phys_bits;bool kernel_hashes; +uint64_t sev_features; +uint64_t supported_sev_features;/* runtime state */ uint8_t api_major;
@@ -492,7 +494,40 @@ static void sev_apply_cpu_context(CPUState *cpu) } } -static int check_vmsa_supported (hwaddr gpa,const struct sev_es_save_area *vmsa, +static int check_sev_features (SevCommonState *sev_common,uint64_t sev_features, +Error **errp) +{ +/* +* Ensure SEV_FEATURES is configured for correct SEV hardware and that +* the requested features are supported. If SEV-SNP is enabled then +* that feature must be enabled, otherwise it must be cleared. +*/ +if (sev_snp_enabled () && !(sev_features &SVM_SEV_FEAT_SNP_ACTIVE )) { +error_setg ( + errp, +"%s: SEV_SNP is enabled but is not enabled in VMSA sev_features" , + __func__); +return -1 ; + }else if (!sev_snp_enabled () && + (sev_features &SVM_SEV_FEAT_SNP_ACTIVE )) { +error_setg ( + errp, +"%s: SEV_SNP is not enabled but is enabled in VMSA sev_features" , + __func__); +return -1 ; + } +if (sev_features & ~sev_common->supported_sev_features) { +error_setg (errp, +"%s: VMSA contains unsupported sev_features: %lX, " +"supported features: %lX" , + __func__, sev_features, sev_common->supported_sev_features); +return -1 ; + } +return 0 ; +} + +static int check_vmsa_supported (SevCommonState *sev_common,hwaddr gpa, +const struct sev_es_save_area *vmsa,Error **errp) {struct sev_es_save_area vmsa_check;
@@ -558,24 +593,10 @@ static int check_vmsa_supported(hwaddr gpa, const struct sev_es_save_area *vmsa, vmsa_check.x87_fcw =0 ; vmsa_check.mxcsr =0 ; -if (sev_snp_enabled ()) { -if (vmsa_check.sev_features !=SVM_SEV_FEAT_SNP_ACTIVE ) { -error_setg (errp, -"%s: sev_features in the VMSA contains an unsupported " -"value. For SEV-SNP, sev_features must be set to %x." , - __func__,SVM_SEV_FEAT_SNP_ACTIVE ); -return -1 ; - } - vmsa_check.sev_features =0 ; - }else { -if (vmsa_check.sev_features !=0 ) { -error_setg (errp, -"%s: sev_features in the VMSA contains an unsupported " -"value. For SEV-ES and SEV, sev_features must be " -"set to 0." , __func__); -return -1 ; - } +if (check_sev_features (sev_common, vmsa_check.sev_features, errp) <0 ) { +return -1 ; } + vmsa_check.sev_features =0 ;if (!buffer_is_zero (&vmsa_check,sizeof (vmsa_check))) { error_setg(errp,
@@ -1729,6 +1750,39 @@ static int sev_snp_kvm_type(X86ConfidentialGuest *cg)return KVM_X86_SNP_VM ; } +static int sev_init_supported_features (ConfidentialGuestSupport *cgs, +SevCommonState *sev_common,Error **errp) +{ +X86ConfidentialGuestClass *x86_klass = +X86_CONFIDENTIAL_GUEST_GET_CLASS (cgs); +/* +* Older kernels do not support query or setting of sev_features. In this +* case the set of supported features must be zero to match the settings +* in the kernel. +*/ +if (x86_klass->kvm_type (X86_CONFIDENTIAL_GUEST (sev_common)) == +KVM_X86_DEFAULT_VM ) { + sev_common->supported_sev_features =0 ; +return 0 ; + } + +/* Query KVM for the supported set of sev_features */ +struct kvm_device_attr attr = { + .group =KVM_X86_GRP_SEV , + .attr =KVM_X86_SEV_VMSA_FEATURES , + .addr = (unsigned long )&sev_common->supported_sev_features, + }; +if (kvm_ioctl (kvm_state,KVM_GET_DEVICE_ATTR , &attr) <0 ) { +error_setg (errp,"%s: failed to query supported sev_features" , + __func__); +return -1 ; + } +if (sev_snp_enabled ()) { + sev_common->supported_sev_features |=SVM_SEV_FEAT_SNP_ACTIVE ; + } +return 0 ; +} +static int sev_common_kvm_init (ConfidentialGuestSupport *cgs,Error **errp) {char *devname;
@@ -1809,6 +1863,10 @@ static int sev_common_kvm_init(ConfidentialGuestSupport *cgs, Error **errp) } } +if (sev_init_supported_features (cgs, sev_common, errp) <0 ) { +return -1 ; + } +trace_kvm_sev_init ();switch (x86_klass->kvm_type (X86_CONFIDENTIAL_GUEST (sev_common))) {case KVM_X86_DEFAULT_VM :
@@ -1820,6 +1878,39 @@ static int sev_common_kvm_init(ConfidentialGuestSupport *cgs, Error **errp)case KVM_X86_SEV_ES_VM :case KVM_X86_SNP_VM : {struct kvm_sev_init args = {0 }; +MachineState *machine =MACHINE (qdev_get_machine ()); +X86MachineState *x86machine =X86_MACHINE (qdev_get_machine ()); + +/* +* If configuration is provided via an IGVM file then the IGVM file +* might contain configuration of the initial vcpu context. For SEV +* the vcpu context includes the sev_features which should be applied +* to the vcpu. +* +* KVM does not synchronize sev_features from CPU state. Instead it +* requires sev_features to be provided as part of this initialization +* call which is subsequently automatically applied to the VMSA of +* each vcpu. +* +* The IGVM file is normally processed after initialization. Therefore +* we need to pre-process it here to extract sev_features in order to +* provide it to KVM_SEV_INIT2. Each cgs_* function that is called by +* the IGVM processor detects this pre-process by observing the state +* as SEV_STATE_UNINIT. +*/ +if (x86machine->igvm) { +if (IGVM_CFG_GET_CLASS (x86machine->igvm) + ->process (x86machine->igvm, machine->cgs, errp) ==-1 ) { +return -1 ; + } +/* +* KVM maintains a bitmask of allowed sev_features. This does not +* include SVM_SEV_FEAT_SNP_ACTIVE which is set accordingly by KVM +* itself. Therefore we need to clear this flag. +*/ + args.vmsa_features = sev_common->sev_features & + ~SVM_SEV_FEAT_SNP_ACTIVE ; + } ret =sev_ioctl (sev_common->sev_fd,KVM_SEV_INIT2 , &args, &fw_error);break ;
@@ -2424,6 +2515,24 @@ static int cgs_set_guest_state(hwaddr gpa, uint8_t *ptr, uint64_t len,SevCommonState *sev_common =SEV_COMMON (MACHINE (qdev_get_machine ())->cgs);SevCommonStateClass *klass =SEV_COMMON_GET_CLASS (sev_common); +if (sev_common->state ==SEV_STATE_UNINIT ) { +/* Pre-processing of IGVM file called from sev_common_kvm_init() */ +if ((cpu_index ==0 ) && (memory_type ==CGS_PAGE_TYPE_VMSA )) { +const struct sev_es_save_area *sa = + (const struct sev_es_save_area *)ptr; +if (len <sizeof (*sa)) { +error_setg (errp,"%s: invalid VMSA length encountered" , + __func__); +return -1 ; + } +if (check_sev_features (sev_common, sa->sev_features, errp) <0 ) { +return -1 ; + } + sev_common->sev_features = sa->sev_features; + } +return 0 ; + } +if (!sev_enabled ()) {error_setg (errp,"%s: attempt to configure guest memory, but SEV " "is not enabled" , __func__);
@@ -2443,7 +2552,8 @@ static int cgs_set_guest_state(hwaddr gpa, uint8_t *ptr, uint64_t len, __func__);return -1 ; } -if (check_vmsa_supported (gpa, (const struct sev_es_save_area *)ptr, +if (check_vmsa_supported (sev_common, gpa, + (const struct sev_es_save_area *)ptr, errp) <0 ) {return -1 ; }
@@ -2500,6 +2610,12 @@ static int cgs_get_mem_map_entry(int index,struct e820_entry *table;int num_entries; +SevCommonState *sev_common =SEV_COMMON (MACHINE (qdev_get_machine ())->cgs); +if (sev_common->state ==SEV_STATE_UNINIT ) { +/* Pre-processing of IGVM file called from sev_common_kvm_init() */ +return 1 ; + } + num_entries =e820_get_table (&table);if ((index <0 ) || (index >= num_entries)) {return 1 ;
@@ -2531,6 +2647,12 @@ static int cgs_set_guest_policy(ConfidentialGuestPolicyType policy_type, uint32_t policy_data1_size, void *policy_data2, uint32_t policy_data2_size, Error **errp) { + SevCommonState *sev_common = SEV_COMMON(MACHINE(qdev_get_machine())->cgs); +if (sev_common->state ==SEV_STATE_UNINIT ) { +/* Pre-processing of IGVM file called from sev_common_kvm_init() */ +return 0 ; + } +if (policy_type !=GUEST_POLICY_SEV ) {error_setg (errp,"%s: Invalid guest policy type provided for SEV: %d" , __func__, policy_type);
Signature · 1 line
2.43.0
PATCH v7 16/16 sev: Provide sev_features flags from IGVM VMSA to KVM_SEV_INIT2
In reply to Roy HopkinsIGVM files can contain an initial VMSA that should be applied to each vcpu as part of the initial guest state. The sev_features flags are provided as part of the VMSA structure. However, KVM only allows sev_features to be set during initialization and not as the guest is being prepared for launch.
This patch queries KVM for the supported set of sev_features flags and processes the IGVM file during kvm_init to determine any sev_features flags set in the IGVM file. These are then provided in the call to KVM_SEV_INIT2 to ensure the guest state matches that specified in the IGVM file.
This does cause the IGVM file to be processed twice. Firstly to extract the sev_features then secondly to actually configure the guest. However, the first pass is largely ignored meaning the overhead is minimal.
--- target/i386/sev.c | 160 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 141 insertions(+), 19 deletions(-)
target/i386/sev.c +141 -19
@@ -117,6 +117,8 @@ struct SevCommonState {uint32_t cbitpos;uint32_t reduced_phys_bits;bool kernel_hashes; +uint64_t sev_features; +uint64_t supported_sev_features;/* runtime state */ uint8_t api_major;
@@ -492,7 +494,40 @@ static void sev_apply_cpu_context(CPUState *cpu) } } -static int check_vmsa_supported (hwaddr gpa,const struct sev_es_save_area *vmsa, +static int check_sev_features (SevCommonState *sev_common,uint64_t sev_features, +Error **errp) +{ +/* +* Ensure SEV_FEATURES is configured for correct SEV hardware and that +* the requested features are supported. If SEV-SNP is enabled then +* that feature must be enabled, otherwise it must be cleared. +*/ +if (sev_snp_enabled () && !(sev_features &SVM_SEV_FEAT_SNP_ACTIVE )) { +error_setg ( + errp, +"%s: SEV_SNP is enabled but is not enabled in VMSA sev_features" , + __func__); +return -1 ; + }else if (!sev_snp_enabled () && + (sev_features &SVM_SEV_FEAT_SNP_ACTIVE )) { +error_setg ( + errp, +"%s: SEV_SNP is not enabled but is enabled in VMSA sev_features" , + __func__); +return -1 ; + } +if (sev_features & ~sev_common->supported_sev_features) { +error_setg (errp, +"%s: VMSA contains unsupported sev_features: %lX, " +"supported features: %lX" , + __func__, sev_features, sev_common->supported_sev_features); +return -1 ; + } +return 0 ; +} + +static int check_vmsa_supported (SevCommonState *sev_common,hwaddr gpa, +const struct sev_es_save_area *vmsa,Error **errp) {struct sev_es_save_area vmsa_check;
@@ -558,24 +593,10 @@ static int check_vmsa_supported(hwaddr gpa, const struct sev_es_save_area *vmsa, vmsa_check.x87_fcw =0 ; vmsa_check.mxcsr =0 ; -if (sev_snp_enabled ()) { -if (vmsa_check.sev_features !=SVM_SEV_FEAT_SNP_ACTIVE ) { -error_setg (errp, -"%s: sev_features in the VMSA contains an unsupported " -"value. For SEV-SNP, sev_features must be set to %x." , - __func__,SVM_SEV_FEAT_SNP_ACTIVE ); -return -1 ; - } - vmsa_check.sev_features =0 ; - }else { -if (vmsa_check.sev_features !=0 ) { -error_setg (errp, -"%s: sev_features in the VMSA contains an unsupported " -"value. For SEV-ES and SEV, sev_features must be " -"set to 0." , __func__); -return -1 ; - } +if (check_sev_features (sev_common, vmsa_check.sev_features, errp) <0 ) { +return -1 ; } + vmsa_check.sev_features =0 ;if (!buffer_is_zero (&vmsa_check,sizeof (vmsa_check))) { error_setg(errp,
@@ -1729,6 +1750,39 @@ static int sev_snp_kvm_type(X86ConfidentialGuest *cg)return KVM_X86_SNP_VM ; } +static int sev_init_supported_features (ConfidentialGuestSupport *cgs, +SevCommonState *sev_common,Error **errp) +{ +X86ConfidentialGuestClass *x86_klass = +X86_CONFIDENTIAL_GUEST_GET_CLASS (cgs); +/* +* Older kernels do not support query or setting of sev_features. In this +* case the set of supported features must be zero to match the settings +* in the kernel. +*/ +if (x86_klass->kvm_type (X86_CONFIDENTIAL_GUEST (sev_common)) == +KVM_X86_DEFAULT_VM ) { + sev_common->supported_sev_features =0 ; +return 0 ; + } + +/* Query KVM for the supported set of sev_features */ +struct kvm_device_attr attr = { + .group =KVM_X86_GRP_SEV , + .attr =KVM_X86_SEV_VMSA_FEATURES , + .addr = (unsigned long )&sev_common->supported_sev_features, + }; +if (kvm_ioctl (kvm_state,KVM_GET_DEVICE_ATTR , &attr) <0 ) { +error_setg (errp,"%s: failed to query supported sev_features" , + __func__); +return -1 ; + } +if (sev_snp_enabled ()) { + sev_common->supported_sev_features |=SVM_SEV_FEAT_SNP_ACTIVE ; + } +return 0 ; +} +static int sev_common_kvm_init (ConfidentialGuestSupport *cgs,Error **errp) {char *devname;
@@ -1809,6 +1863,10 @@ static int sev_common_kvm_init(ConfidentialGuestSupport *cgs, Error **errp) } } +if (sev_init_supported_features (cgs, sev_common, errp) <0 ) { +return -1 ; + } +trace_kvm_sev_init ();switch (x86_klass->kvm_type (X86_CONFIDENTIAL_GUEST (sev_common))) {case KVM_X86_DEFAULT_VM :
@@ -1820,6 +1878,39 @@ static int sev_common_kvm_init(ConfidentialGuestSupport *cgs, Error **errp)case KVM_X86_SEV_ES_VM :case KVM_X86_SNP_VM : {struct kvm_sev_init args = {0 }; +MachineState *machine =MACHINE (qdev_get_machine ()); +X86MachineState *x86machine =X86_MACHINE (qdev_get_machine ()); + +/* +* If configuration is provided via an IGVM file then the IGVM file +* might contain configuration of the initial vcpu context. For SEV +* the vcpu context includes the sev_features which should be applied +* to the vcpu. +* +* KVM does not synchronize sev_features from CPU state. Instead it +* requires sev_features to be provided as part of this initialization +* call which is subsequently automatically applied to the VMSA of +* each vcpu. +* +* The IGVM file is normally processed after initialization. Therefore +* we need to pre-process it here to extract sev_features in order to +* provide it to KVM_SEV_INIT2. Each cgs_* function that is called by +* the IGVM processor detects this pre-process by observing the state +* as SEV_STATE_UNINIT. +*/ +if (x86machine->igvm) { +if (IGVM_CFG_GET_CLASS (x86machine->igvm) + ->process (x86machine->igvm, machine->cgs, errp) ==-1 ) { +return -1 ; + } +/* +* KVM maintains a bitmask of allowed sev_features. This does not +* include SVM_SEV_FEAT_SNP_ACTIVE which is set accordingly by KVM +* itself. Therefore we need to clear this flag. +*/ + args.vmsa_features = sev_common->sev_features & + ~SVM_SEV_FEAT_SNP_ACTIVE ; + } ret =sev_ioctl (sev_common->sev_fd,KVM_SEV_INIT2 , &args, &fw_error);break ;
@@ -2424,6 +2515,24 @@ static int cgs_set_guest_state(hwaddr gpa, uint8_t *ptr, uint64_t len,SevCommonState *sev_common =SEV_COMMON (MACHINE (qdev_get_machine ())->cgs);SevCommonStateClass *klass =SEV_COMMON_GET_CLASS (sev_common); +if (sev_common->state ==SEV_STATE_UNINIT ) { +/* Pre-processing of IGVM file called from sev_common_kvm_init() */ +if ((cpu_index ==0 ) && (memory_type ==CGS_PAGE_TYPE_VMSA )) { +const struct sev_es_save_area *sa = + (const struct sev_es_save_area *)ptr; +if (len <sizeof (*sa)) { +error_setg (errp,"%s: invalid VMSA length encountered" , + __func__); +return -1 ; + } +if (check_sev_features (sev_common, sa->sev_features, errp) <0 ) { +return -1 ; + } + sev_common->sev_features = sa->sev_features; + } +return 0 ; + } +if (!sev_enabled ()) {error_setg (errp,"%s: attempt to configure guest memory, but SEV " "is not enabled" , __func__);
@@ -2443,7 +2552,8 @@ static int cgs_set_guest_state(hwaddr gpa, uint8_t *ptr, uint64_t len, __func__);return -1 ; } -if (check_vmsa_supported (gpa, (const struct sev_es_save_area *)ptr, +if (check_vmsa_supported (sev_common, gpa, + (const struct sev_es_save_area *)ptr, errp) <0 ) {return -1 ; }
@@ -2500,6 +2610,12 @@ static int cgs_get_mem_map_entry(int index,struct e820_entry *table;int num_entries; +SevCommonState *sev_common =SEV_COMMON (MACHINE (qdev_get_machine ())->cgs); +if (sev_common->state ==SEV_STATE_UNINIT ) { +/* Pre-processing of IGVM file called from sev_common_kvm_init() */ +return 1 ; + } + num_entries =e820_get_table (&table);if ((index <0 ) || (index >= num_entries)) {return 1 ;
@@ -2531,6 +2647,12 @@ static int cgs_set_guest_policy(ConfidentialGuestPolicyType policy_type, uint32_t policy_data1_size, void *policy_data2, uint32_t policy_data2_size, Error **errp) { + SevCommonState *sev_common = SEV_COMMON(MACHINE(qdev_get_machine())->cgs); +if (sev_common->state ==SEV_STATE_UNINIT ) { +/* Pre-processing of IGVM file called from sev_common_kvm_init() */ +return 0 ; + } +if (policy_type !=GUEST_POLICY_SEV ) {error_setg (errp,"%s: Invalid guest policy type provided for SEV: %d" , __func__, policy_type);
Signature · 1 line
2.43.0
Quoted 16 lines · Roy Hopkins
Here is v7 of the set of patches to add support for IGVM files to QEMU. This is based on commit
40efe733e10cc00e4fb4f9f5790a28e744e63c62of qemu.Firstly, apologies for the amount of time between the last version and this one. I moved roles to a different company and, although I always planned to see this patch series to completion, it took a while before I found time to setup a development environment and be in a position to send a new version. I will continue this series using a personal email address for now, hence the change to the author and signed-off-by emails.
The only changes in this version are to rebase on the current master branch and update commit metadata, including Signed-Off-By and Author emails for my replacement email address, and to include the final Reviewed-By that were added in the last review. There were no requested changes on the previous version [1] so I believe this series is ready to merge.
Quoted 8 lines
As always, thanks to those that have been following along, reviewing and testing this series. This v7 patch series is also available on github: [2]
For testing IGVM support in QEMU you need to generate an IGVM file that is configured for the platform you want to launch. You can use the
buildigvmtest tool [3] to allow generation of IGVM files for all currently supported platforms. Patch 11/17 contains information on how to generate an IGVM file using this tool.
Looks good to me overall, although I don't know SEV good enough to review these changes in detail.
take care,
Gerd
Hi Roy,
Quoted 17 lines · Roy Hopkins
Here is v7 of the set of patches to add support for IGVM files to QEMU. This is based on commit
40efe733e10cc00e4fb4f9f5790a28e744e63c62of qemu.Firstly, apologies for the amount of time between the last version and this one. I moved roles to a different company and, although I always planned to see this patch series to completion, it took a while before I found time to setup a development environment and be in a position to send a new version. I will continue this series using a personal email address for now, hence the change to the author and signed-off-by emails.
The only changes in this version are to rebase on the current master branch and update commit metadata, including Signed-Off-By and Author emails for my replacement email address, and to include the final Reviewed-By that were added in the last review. There were no requested changes on the previous version [1] so I believe this series is ready to merge.
Quoted 8 lines
As always, thanks to those that have been following along, reviewing and testing this series. This v7 patch series is also available on github: [2]
For testing IGVM support in QEMU you need to generate an IGVM file that is configured for the platform you want to launch. You can use the
buildigvmtest tool [3] to allow generation of IGVM files for all currently supported platforms. Patch 11/17 contains information on how to generate an IGVM file using this tool.
I was testing this series with the IGVM file generated by COCONUT SVSM, but QEMU was failing in this way:
qemu-system-x86_64: KVM does not support guest_memfd
qemu-system-x86_64: failed to initialize kvm: Operation not permitted
After spending some time debugging, I found that IGVM is parsed in kvm_arch_init(). One of the handler called during the parsing is qigvm_prepare_memory(), which adds a new memory region calling memory_region_init_ram_guest_memfd(), but it fails:
kvm_arch_init()
-> qigvm_prepare_memory()
-> memory_region_init_ram_guest_memfd()
-> kvm_create_guest_memfd()
...
if (!kvm_guest_memfd_supported) {
error_setg(errp, "KVM does not support guest_memfd");
return -1;
}So, I applied the following change and SVSM booted!
accel/kvm/kvm-all.c +5 -5
@@ -2722,17 +2722,17 @@ static int kvm_init(MachineState *ms) kvm_state = s; - ret =kvm_arch_init (ms, s); -if (ret <0 ) { - goto err; - } - kvm_supported_memory_attributes =kvm_vm_check_extension (s,KVM_CAP_MEMORY_ATTRIBUTES ); kvm_guest_memfd_supported =kvm_check_extension (s,KVM_CAP_GUEST_MEMFD ) &&kvm_check_extension (s,KVM_CAP_USER_MEMORY2 ) && (kvm_supported_memory_attributes &KVM_MEMORY_ATTRIBUTE_PRIVATE ); + ret =kvm_arch_init (ms, s); +if (ret <0 ) { + goto err; + } +if (s->kernel_irqchip_split ==ON_OFF_AUTO_AUTO ) { s->kernel_irqchip_split = mc->default_kernel_irqchip_split ?ON_OFF_AUTO_ON :ON_OFF_AUTO_OFF ; }
Checking, I discovered that it was done on purpose by Paolo, so not sure if my fix is valid:
commit 586d708c1e3e5e29a0b3c05c347290aed9478854
Author: Paolo Bonzini <[email protected]> Date: Fri Oct 11 10:39:58 2024 +0200
accel/kvm: check for KVM_CAP_MEMORY_ATTRIBUTES on vm
The exact set of available memory attributes can vary by VM. In the future it might vary depending on enabled capabilities, too. Query the extension on the VM level instead of on the KVM level, and only after architecture-specific initialization.
Inspired by an analogous patch by Tom Dohrmann.
@Paolo any suggestion?
Thanks,
Stefano
Quoted 69 lines · Stefano Garzarella
Hi Roy,
I was testing this series with the IGVM file generated by COCONUT SVSM, but QEMU was failing in this way:
qemu-system-x86_64: KVM does not support
guest_memfd
qemu-system-x86_64: failed to initialize kvm: Operation not permittedAfter spending some time debugging, I found that IGVM is parsed in
kvm_arch_init(). One of the handler called during the parsing isqigvm_prepare_memory(), which adds a new memory region callingmemory_region_init_ram_guest_memfd(), but it fails:
kvm_arch_init()
->qigvm_prepare_memory()
->memory_region_init_ram_guest_memfd()
->kvm_create_guest_memfd()
...
if (!kvm_guest_memfd_supported) {error_setg(errp, "KVM does not support guest_memfd"); return -1;
}So, I applied the following change and SVSM booted!
accel/kvm/kvm-all.c +0
-0@@ -2722,17 +2722,17 @@ static int kvm_init(MachineState *ms)
kvm_state= s;
- ret =kvm_arch_init(ms, s);
- if (ret < 0) {
- goto err;
- }
-
kvm_supported_memory_attributes=kvm_vm_check_extension(s, KVM_CAP_MEMORY_ATTRIBUTES);kvm_guest_memfd_supported=
kvm_check_extension(s, KVM_CAP_GUEST_MEMFD)&&
kvm_check_extension(s, KVM_CAP_USER_MEMORY2)&& (kvm_supported_memory_attributes&KVM_MEMORY_ATTRIBUTE_PRIVATE);
+ ret =kvm_arch_init(ms, s);
+ if (ret < 0) {
+ goto err;
+ }
+
if (s->kernel_irqchip_split==ON_OFF_AUTO_AUTO) {
s->kernel_irqchip_split=mc->default_kernel_irqchip_split?ON_OFF_AUTO_ON:ON_OFF_AUTO_OFF; }Checking, I discovered that it was done on purpose by Paolo, so not sure if my fix is valid:
commit
586d708c1e3e5e29a0b3c05c347290aed9478854
Author: Paolo Bonzini <[email protected]>
Date: Fri Oct 11 10:39:58 2024 +0200accel/kvm: check for
KVM_CAP_MEMORY_ATTRIBUTESon vm
The exact set of available memory attributes can vary by VM. In the future it might vary depending on enabled capabilities, too. Query the extension on the VM level instead of on the KVM level, and only after architecture-specific initialization.
Inspired by an analogous patch by Tom Dohrmann.
Quoted 7 lines
Signed-off-by Paolo Bonzini <[email protected]>@Paolo any suggestion?
Thanks,
Stefano
Hi Stefano,
Thanks for testing this. The problem seems to be down to the fact that I had to introduce an initial parsing of the IGVM file during initialization to extract sev_features. I was parsing all directives in the file but it appears this has some unwanted side effects.
Please could you try the patch below to see if it fixes the issue? If it does I'll incorporate it into the patch series and resubmit.
From 3590460ec3945b02a679ad79735681a642596d60 Mon Sep 17 00:00:00 2001
From: Roy Hopkins <[email protected]>
Date: Thu, 6 Mar 2025 11:25:07 +0000
Subject: [PATCH 1/1] backends/igvm: Add function to process only VP context
When initializing kvm for SEV, the sev_features need to be passed to the initialization function. When using IGVM files, sev_features is provided in the VP context definintions in the file. Currently this is handled in sev.c by processing the entire file to extract the VP context, however this has unwanted side-effects. Therefore this commit adds a new function that allows only the VP context definitions to be parsed in the IGVM file.
--- backends/igvm-cfg.c | 1 + backends/igvm.c | 51 +++++++++++++++++++++++++++++++++++++++ backends/igvm.h | 3 +++ include/system/igvm-cfg.h | 10 ++++++++ target/i386/sev.c | 10 ++++---- 5 files changed, 70 insertions(+), 5 deletions(-)
backends/igvm-cfg.c +1 -0
@@ -41,6 +41,7 @@ static void igvm_cfg_class_init(ObjectClass *oc, void *data) "Set the IGVM filename to use"); igvmc->process = qigvm_process_file; + igvmc->process_vp_context = qigvm_process_vp_context; }static void igvm_cfg_init (Object *obj)
backends/igvm.c +51 -0
54 lines not on this page · raw message
backends/igvm.h +3 -0
@@ -20,4 +20,7 @@int qigvm_process_file (IgvmCfg *igvm,ConfidentialGuestSupport *cgs,Error **errp); +int qigvm_process_vp_context (IgvmCfg *igvm,ConfidentialGuestSupport *cgs, +Error **errp); +#endif
include/system/igvm-cfg.h +10 -0
@@ -38,6 +38,16 @@ typedef struct IgvmCfgClass {int (*process)(IgvmCfg *cfg,ConfidentialGuestSupport *cgs,Error **errp); +/* +* If an IGVM filename has been specified then only process +* the VMSA sections in the IGVM file. +* Performs a no-op if no filename has been specified. +* +* Returns 0 for ok and -1 on error. +*/ +int (*process_vp_context)(IgvmCfg *cfg,ConfidentialGuestSupport *cgs, +Error **errp); + } IgvmCfgClass;#define TYPE_IGVM_CFG "igvm-cfg"
target/i386/sev.c +5 -5
@@ -1893,14 +1893,14 @@ static int sev_common_kvm_init(ConfidentialGuestSupport *cgs, Error **errp)* each vcpu. * * The IGVM file is normally processed after initialization. Therefore -* we need to pre-process it here to extract sev_features in order to -* provide it to KVM_SEV_INIT2. Each cgs_* function that is called by -* the IGVM processor detects this pre-process by observing the state -* as SEV_STATE_UNINIT. +* we need to pre-process it here, just looking for the vp_context to +* extract sev_features in order to provide it to KVM_SEV_INIT2. Each +* cgs_* function that is called by the IGVM processor detects this +* pre-process by observing the state as SEV_STATE_UNINIT. */ if (x86machine->igvm) {if (IGVM_CFG_GET_CLASS (x86machine->igvm) - ->process (x86machine->igvm, machine->cgs, errp) ==-1 ) { + ->process_vp_context (x86machine->igvm, machine->cgs, errp) ==-1 ) {return -1 ; }/*
Signature · 1 line
2.43.0
Stefano Garzarella
Tested-byHi Roy,
Quoted 1 line · Roy Hopkins
On Wed, 2025-03-05 at 16:47 +0100, Stefano Garzarella wrote:
[...]
Quoted 8 lines
Thanks for testing this. The problem seems to be down to the fact that I had to introduce an initial parsing of the IGVM file during initialization to extract
sev_features. I was parsing all directives in the file but it appears this has some unwanted side effects.Please could you try the patch below to see if it fixes the issue? If it does I'll incorporate it into the patch series and resubmit.
Great, it worked, so feel free to add:
About the patch, I have some comments below:
Quoted 41 lines
From
3590460ec3945b02a679ad79735681a642596d60Mon Sep 17 00:00:00 2001
From: Roy Hopkins <[email protected]>
Date: Thu, 6 Mar 2025 11:25:07 +0000
Subject: [PATCH 1/1] backends/igvm: Add function to process only VP contextWhen initializing kvm for SEV, the
sev_featuresneed to be passed to the initialization function. When using IGVM files,sev_featuresis provided in the VP context definintions in the file. Currently this is handled insev.cby processing the entire file to extract the VP context, however this has unwanted side-effects. Therefore this commit adds a new function that allows only the VP context definitions to be parsed in the IGVM file.Signed-off-by Roy Hopkins <[email protected]>---
backends/igvm-cfg.c | 1 + backends/igvm.c | 51 +++++++++++++++++++++++++++++++++++++++ backends/igvm.h | 3 +++ include/system/igvm-cfg.h | 10 ++++++++ target/i386/sev.c | 10 ++++---- 5 files changed, 70 insertions(+), 5 deletions(-)backends/igvm-cfg.c +1
-0@@ -41,6 +41,7 @@ static void igvm_cfg_class_init(ObjectClass *oc, void *data) "Set the IGVM filename to use"); igvmc->process = qigvm_process_file; + igvmc->process_vp_context = qigvm_process_vp_context; }static void igvm_cfg_init (Object *obj)backends/igvm.c +0
-0
backends/igvm.c cleanup: · line 971
Quoted 7 lines
backends/igvm.c +4
-0@@ -965,3 +965,54 @@ cleanup:return retval; } + +int qigvm_process_vp_context (IgvmCfg *cfg,ConfidentialGuestSupport *cgs, +Error **errp) +{
This new function share a lot of code with qigvm_process_file(), can we avoid that? e.g. adding a parameter to the process callback, or just factoring out common code?
Quoted 26 lines
backends/igvm.c +26
-0+int32_t header_count; +int retval =-1 ; +QIgvm ctx; + +memset (&ctx,0 ,sizeof (ctx)); + ctx.file =qigvm_file_init (cfg->filename, errp); +if (ctx.file <0 ) { +return -1 ; + } + + ctx.cgs = cgs; + ctx.cgsc = cgs ?CONFIDENTIAL_GUEST_SUPPORT_GET_CLASS (cgs) :NULL ; + +/* +* Check that the IGVM file provides configuration for the current +* platform +*/ +if (qigvm_supported_platform_compat_mask (&ctx, errp) <0 ) { + goto cleanup; + } + + header_count =igvm_header_count (ctx.file,IGVM_HEADER_SECTION_DIRECTIVE ); +if (header_count <=0 ) { +error_setg ( + errp,"Invalid directive header count in IGVM file. Error code: %X" , + header_count);
backends/igvm.c cleanup: · line 1007
Quoted 10 lines
backends/igvm.c +10
-0+ goto cleanup; + } + +for (ctx.current_header_index =0 ; + ctx.current_header_index < (unsigned )header_count; + ctx.current_header_index++) { +IgvmVariableHeaderType type =igvm_get_header_type ( + ctx.file,IGVM_HEADER_SECTION_DIRECTIVE , ctx.current_header_index); +if (type ==IGVM_VHT_VP_CONTEXT ) { +if (qigvm_handler (&ctx, type, errp) <0 ) {
Understanding the error I had was a bit tricky, since it didn't mention IGVM at all.
Can we add an error_prepend() here or ...
Quoted 55 lines
backends/igvm.c +11
-0+ goto cleanup; + } + } + } + retval =0 ; + +cleanup: +igvm_free (ctx.file); + +return retval; +}backends/igvm.h +3
-0@@ -20,4 +20,7 @@int qigvm_process_file (IgvmCfg *igvm,ConfidentialGuestSupport *cgs,Error **errp); +int qigvm_process_vp_context (IgvmCfg *igvm,ConfidentialGuestSupport *cgs, +Error **errp); +#endif include/system/igvm-cfg.h +10
-0@@ -38,6 +38,16 @@ typedef struct IgvmCfgClass {int (*process)(IgvmCfg *cfg,ConfidentialGuestSupport *cgs,Error **errp); +/* +* If an IGVM filename has been specified then only process +* the VMSA sections in the IGVM file. +* Performs a no-op if no filename has been specified. +* +* Returns 0 for ok and -1 on error. +*/ +int (*process_vp_context)(IgvmCfg *cfg,ConfidentialGuestSupport *cgs, +Error **errp); + } IgvmCfgClass;#define TYPE_IGVM_CFG "igvm-cfg"target/i386/sev.c +0
-3@@ -1893,14 +1893,14 @@ static int sev_common_kvm_init(ConfidentialGuestSupport *cgs, Error **errp)* each vcpu. * * The IGVM file is normally processed after initialization. Therefore -* we need to pre-process it here to extract sev_features in order to -* provide it to KVM_SEV_INIT2. Each cgs_* function that is called by -* the IGVM processor detects this pre-process by observing the state
target/i386/sev.c static int sev_common_kvm_init(ConfidentialGuestSupport *cgs, Error **errp) · line 1903
Quoted 10 lines
target/i386/sev.c +5
-2-* as SEV_STATE_UNINIT. +* we need to pre-process it here, just looking for the vp_context to +* extract sev_features in order to provide it to KVM_SEV_INIT2. Each +* cgs_* function that is called by the IGVM processor detects this +* pre-process by observing the state as SEV_STATE_UNINIT. */ if (x86machine->igvm) {if (IGVM_CFG_GET_CLASS (x86machine->igvm) - ->process (x86machine->igvm, machine->cgs, errp) ==-1 ) { + ->process_vp_context (x86machine->igvm, machine->cgs, errp) ==-1 ) {
... here?
BTW we can fix it later, but since you have to repost, I pointed it out.
Thanks,
Stefano
Quoted 7 lines
target/i386/sev.c +0
-0return -1 ; }/* Signature · 1 line
2.43.0
Quoted 23 lines · Roy Hopkins
Here is v7 of the set of patches to add support for IGVM files to QEMU. This is based on commit
40efe733e10cc00e4fb4f9f5790a28e744e63c62of qemu.Firstly, apologies for the amount of time between the last version and this one. I moved roles to a different company and, although I always planned to see this patch series to completion, it took a while before I found time to setup a development environment and be in a position to send a new version. I will continue this series using a personal email address for now, hence the change to the author and signed-off-by emails.
The only changes in this version are to rebase on the current master branch and update commit metadata, including Signed-Off-By and Author emails for my replacement email address, and to include the final Reviewed-By that were added in the last review. There were no requested changes on the previous version [1] so I believe this series is ready to merge.
As always, thanks to those that have been following along, reviewing and testing this series. This v7 patch series is also available on github: [2]
Quoted 7 lines
For testing IGVM support in QEMU you need to generate an IGVM file that is configured for the platform you want to launch. You can use the
buildigvmtest tool [3] to allow generation of IGVM files for all currently supported platforms. Patch 11/17 contains information on how to generate an IGVM file using this tool.Changes in v7:
It seems no v8 was sent out?
https://patchew.org/search?q=project%3AQEMU+Introduce+support+for+IGVM+files
What is the status of this patch set to support IGVM in QEMU?
Quoted 78 lines
* Update version numbers for IGVM support to 10.0
* Add Reviewed-by to relevant commits.
* Update Author email and sign-offs to my new email addressPatch summary:
1-11: Add support and documentation for processing IGVM files for SEV, SEV-ES, SEV-SNP and native platforms.
12-15: Processing of policy and SEV-SNP
ID_BLOCKfrom IGVM file.16: Add pre-processing of IGVM file to support synchronization of '
SEV_FEATURES' from IGVM VMSA to KVM.[1] Link to v6:
https://lore.kernel.org/qemu-devel/[email protected]/[2] v7 patches also available here:
https://github.com/roy-hopkins/qemu/tree/igvm_master_v7[3]
buildigvmtool v0.2.0
https://github.com/roy-hopkins/buildigvm/releases/tag/v0.2.0Roy Hopkins (16):
meson: Add optional dependency on IGVM library backends/confidential-guest-support: Add functions to support IGVM backends/igvm: Add IGVM loader and configuration
hw/i386: Add igvm-cfg object and processing for IGVM files i386/pc_sysfw: Ensure sysfw flash configuration does not conflict with
IGVM
sev: Updatelaunch_update_datafunctions to use Error handling target/i386: Allow setting ofR_LDTRandR_TRwith
cpu_x86_load_seg_cache()
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 IGVMdocs/interop/firmware.json: Add igvm to FirmwareDevice backends/confidential-guest-support: Addset_guest_policy()function backends/igvm: Process initialization sections in IGVM file backends/igvm: Handle policy for SEV guests
i386/sev: Add implementation of CGSset_guest_policy()
sev: Providesev_featuresflags from IGVM VMSA toKVM_SEV_INIT2backends/confidential-guest-support.c | 43 + backends/igvm-cfg.c | 52 ++ backends/igvm.c | 967 ++++++++++++++++++++ backends/igvm.h | 23 + backends/meson.build | 5 + docs/interop/firmware.json | 30 +- docs/system/i386/amd-memory-encryption.rst | 2 + docs/system/igvm.rst | 173 ++++ docs/system/index.rst | 1 + hw/i386/pc.c | 12 + hw/i386/pc_piix.c | 10 + hw/i386/pc_q35.c | 10 + hw/i386/pc_sysfw.c | 31 +- include/hw/i386/x86.h | 3 + include/system/confidential-guest-support.h | 88 ++ include/system/igvm-cfg.h | 47 + meson.build | 8 + meson_options.txt | 2 + qapi/qom.json | 17 + qemu-options.hx | 28 + scripts/meson-buildoptions.sh | 3 + target/i386/cpu.h | 9 +- target/i386/sev.c | 850 +++++++++++++++-- target/i386/sev.h | 124 +++ 24 files changed, 2454 insertions(+), 84 deletions(-)create mode 100644
backends/igvm-cfg.c
create mode 100644backends/igvm.c
create mode 100644backends/igvm.h
create mode 100644docs/system/igvm.rst
create mode 100644include/system/igvm-cfg.hSignature · 1 line
2.43.0
A message that is not in the archive (sent off-list or not yet received): b43aa3fa.AUoAAGOlyUYAAAAAAAAAA9cBm3AAAYKJZwAAAAAAAC5ATwBnwHdy@mailjet.com
Re: [PATCH v7 08/16] i386/sev: Refactor setting of reset vector and initial CPU state
Quoted 7 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.
Quoted 7 lines
In preparation for using IGVM to setup the initial CPU state, the code has been refactored to populate
vmcb_save_areafor each CPU which is then applied during guest startup and CPU reset.Signed-off-by Roy Hopkins <[email protected]>
I am not sure if I am responding to the latest version of this patch series, but ...
Quoted 204 lines
---
target/i386/sev.c| 323 +++++++++++++++++++++++++++++++++++++++++-----target/i386/sev.h| 110 ++++++++++++++++2 files changed, 400 insertions(+), 33 deletions(-)target/i386/sev.c +167
-3@@ -49,6 +49,12 @@ OBJECT_DECLARE_TYPE(SevSnpGuestState, SevCommonStateClass, SEV_SNP_GUEST)/* 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;@@ -88,6 +94,14 @@ typedef struct QEMU_PACKED SevHashTableDescriptor {uint32_t size; } SevHashTableDescriptor; +typedef struct SevLaunchVmsa { +QTAILQ_ENTRY (SevLaunchVmsa) next; + +uint16_t cpu_index; +uint64_t gpa; +struct sev_es_save_area vmsa; +}SevLaunchVmsa ; +struct SevCommonState {X86ConfidentialGuest parent_obj;@@ -106,9 +120,7 @@ struct SevCommonState {int sev_fd;SevState state; -uint32_t reset_cs; -uint32_t reset_ip; -bool reset_data_valid; + QTAILQ_HEAD(, SevLaunchVmsa) launch_vmsa; };struct SevCommonStateClass {@@ -371,6 +383,172 @@ static struct RAMBlockNotifier sev_ram_notifier = { .ram_block_removed = sev_ram_block_removed, }; +static void sev_apply_cpu_context (CPUState *cpu) +{ +SevCommonState *sev_common =SEV_COMMON (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_common->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); + env->xcr0 = launch_vmsa->vmsa.xcr0; + env->pat = launch_vmsa->vmsa.g_pat; + +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->gdt.flags = +FLAGS_VMSA_TO_SEGCACHE (launch_vmsa->vmsa.gdtr.attrib); + env->idt.base = launch_vmsa->vmsa.idtr.base; + env->idt.limit = launch_vmsa->vmsa.idtr.limit; + env->idt.flags = +FLAGS_VMSA_TO_SEGCACHE (launch_vmsa->vmsa.idtr.attrib); + +cpu_x86_load_seg_cache ( + env,R_LDTR , launch_vmsa->vmsa.ldtr.selector, + launch_vmsa->vmsa.ldtr.base, launch_vmsa->vmsa.ldtr.limit, +FLAGS_VMSA_TO_SEGCACHE (launch_vmsa->vmsa.ldtr.attrib)); +cpu_x86_load_seg_cache ( + env,R_TR , launch_vmsa->vmsa.tr.selector, + launch_vmsa->vmsa.ldtr.base, launch_vmsa->vmsa.tr.limit, +FLAGS_VMSA_TO_SEGCACHE (launch_vmsa->vmsa.tr.attrib)); + + env->dr[6 ] = launch_vmsa->vmsa.dr6; + env->dr[7 ] = launch_vmsa->vmsa.dr7; + + 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; + env->eflags = launch_vmsa->vmsa.rflags; + +cpu_set_fpuc (env, launch_vmsa->vmsa.x87_fcw); + env->mxcsr = launch_vmsa->vmsa.mxcsr; + +break ; + } + } +} + +static int sev_set_cpu_context (uint16_t cpu_index,const void *ctx, +uint32_t ctx_len,hwaddr gpa,Error **errp) +{ +SevCommonState *sev_common =SEV_COMMON (MACHINE (qdev_get_machine ())->cgs); +SevLaunchVmsa *launch_vmsa; +CPUState *cpu; +bool exists = false; + +/* +* Setting the CPU context is only supported for SEV-ES and SEV-SNP. 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_setg (errp,"SEV: unable to set CPU context: Not supported" ); +return -1 ; + } + +if (ctx_len <sizeof (struct sev_es_save_area )) { +error_setg (errp,"SEV: unable to set CPU context: " +"Invalid context provided" ); +return -1 ; + } + + cpu =qemu_get_cpu (cpu_index); +if (!cpu) { +error_setg (errp,"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_common->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) {
target/i386/sev.c static struct RAMBlockNotifier sev_ram_notifier = { · line 547
Quoted 10 lines
target/i386/sev.c +10
-0+/* 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_common->launch_vmsa, launch_vmsa, next); + } + +/* Synchronise the VMSA with the current CPU state */ +sev_apply_cpu_context (cpu);
Ok so here you are not only saving the vmsa but also applying it.
Quoted 121 lines
target/i386/sev.c +4
-0+ +return 0 ; +} +bool
sev_enabled(void)
{target/i386/sev.c +87
-13@@ -1005,6 +1183,16 @@ static int sev_launch_update_vmsa(SevGuestState *sev_guest) {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_COMMON (sev_guest)->sev_fd,KVM_SEV_LAUNCH_UPDATE_VMSA ,NULL , &fw_error);@@ -1787,40 +1975,110 @@ 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) + +static void seg_to_vmsa (const SegmentCache *cpu_seg,struct vmcb_seg *vmsa_seg) { -X86CPU *x86; -CPUX86State *env; -ConfidentialGuestSupport *cgs =MACHINE (qdev_get_machine ())->cgs; -SevCommonState *sev_common =SEV_COMMON ( -object_dynamic_cast (OBJECT (cgs),TYPE_SEV_COMMON )); + 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); +} -/* Only update if we have valid reset information */ -if (!sev_common || !sev_common->reset_data_valid) { -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; -/* Do not update the BSP reset state */ -if (cpu->cpu_index ==0 ) { -return ; +/* +* 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 ]; + vmsa->xcr0 = env->xcr0; + vmsa->g_pat = env->pat; + +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); +seg_to_vmsa (&env->ldt, &vmsa->ldtr); +seg_to_vmsa (&env->tr, &vmsa->tr); + + vmsa->dr6 = env->dr[6 ]; + vmsa->dr7 = env->dr[7 ]; + + 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; + vmsa->rflags = env->eflags; +} + +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 ) {
target/i386/sev.c sev_es_find_reset_vector(void *flash_ptr, uint64_t flash_size, · line 2067
Quoted 10 lines
target/i386/sev.c +10
-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 , &error_fatal); +sev_apply_cpu_context (cpu);
Is the above call ^^^^^^ sev_apply_cpu_context() needed? Since in sev_set_cpu_context() you already call it?
Quoted 1 line
target/i386/sev.c +0
-0}
Re: [PATCH v7 08/16] i386/sev: Refactor setting of reset vector and initial CPU state
Quoted 363 lines · Ani Sinha
Quoted 8 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.
Quoted 7 lines
In preparation for using IGVM to setup the initial CPU state, the code has been refactored to populate
vmcb_save_areafor each CPU which is then applied during guest startup and CPU reset.Signed-off-by Roy Hopkins <[email protected]>I am not sure if I am responding to the latest version of this patch series, but ...
Quoted 204 lines
---
target/i386/sev.c| 323 +++++++++++++++++++++++++++++++++++++++++-----target/i386/sev.h| 110 ++++++++++++++++2 files changed, 400 insertions(+), 33 deletions(-)target/i386/sev.c +167
-3@@ -49,6 +49,12 @@ OBJECT_DECLARE_TYPE(SevSnpGuestState, SevCommonStateClass, SEV_SNP_GUEST)/* 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;@@ -88,6 +94,14 @@ typedef struct QEMU_PACKED SevHashTableDescriptor {uint32_t size; } SevHashTableDescriptor; +typedef struct SevLaunchVmsa { +QTAILQ_ENTRY (SevLaunchVmsa) next; + +uint16_t cpu_index; +uint64_t gpa; +struct sev_es_save_area vmsa; +}SevLaunchVmsa ; +struct SevCommonState {X86ConfidentialGuest parent_obj;@@ -106,9 +120,7 @@ struct SevCommonState {int sev_fd;SevState state; -uint32_t reset_cs; -uint32_t reset_ip; -bool reset_data_valid; + QTAILQ_HEAD(, SevLaunchVmsa) launch_vmsa; };struct SevCommonStateClass {@@ -371,6 +383,172 @@ static struct RAMBlockNotifier sev_ram_notifier = { .ram_block_removed = sev_ram_block_removed, }; +static void sev_apply_cpu_context (CPUState *cpu) +{ +SevCommonState *sev_common =SEV_COMMON (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_common->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); + env->xcr0 = launch_vmsa->vmsa.xcr0; + env->pat = launch_vmsa->vmsa.g_pat; + +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->gdt.flags = +FLAGS_VMSA_TO_SEGCACHE (launch_vmsa->vmsa.gdtr.attrib); + env->idt.base = launch_vmsa->vmsa.idtr.base; + env->idt.limit = launch_vmsa->vmsa.idtr.limit; + env->idt.flags = +FLAGS_VMSA_TO_SEGCACHE (launch_vmsa->vmsa.idtr.attrib); + +cpu_x86_load_seg_cache ( + env,R_LDTR , launch_vmsa->vmsa.ldtr.selector, + launch_vmsa->vmsa.ldtr.base, launch_vmsa->vmsa.ldtr.limit, +FLAGS_VMSA_TO_SEGCACHE (launch_vmsa->vmsa.ldtr.attrib)); +cpu_x86_load_seg_cache ( + env,R_TR , launch_vmsa->vmsa.tr.selector, + launch_vmsa->vmsa.ldtr.base, launch_vmsa->vmsa.tr.limit, +FLAGS_VMSA_TO_SEGCACHE (launch_vmsa->vmsa.tr.attrib)); + + env->dr[6 ] = launch_vmsa->vmsa.dr6; + env->dr[7 ] = launch_vmsa->vmsa.dr7; + + 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; + env->eflags = launch_vmsa->vmsa.rflags; + +cpu_set_fpuc (env, launch_vmsa->vmsa.x87_fcw); + env->mxcsr = launch_vmsa->vmsa.mxcsr; + +break ; + } + } +} + +static int sev_set_cpu_context (uint16_t cpu_index,const void *ctx, +uint32_t ctx_len,hwaddr gpa,Error **errp) +{ +SevCommonState *sev_common =SEV_COMMON (MACHINE (qdev_get_machine ())->cgs); +SevLaunchVmsa *launch_vmsa; +CPUState *cpu; +bool exists = false; + +/* +* Setting the CPU context is only supported for SEV-ES and SEV-SNP. 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_setg (errp,"SEV: unable to set CPU context: Not supported" ); +return -1 ; + } + +if (ctx_len <sizeof (struct sev_es_save_area )) { +error_setg (errp,"SEV: unable to set CPU context: " +"Invalid context provided" ); +return -1 ; + } + + cpu =qemu_get_cpu (cpu_index); +if (!cpu) { +error_setg (errp,"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_common->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) {target/i386/sev.c static struct RAMBlockNotifier sev_ram_notifier = { · line 547
Quoted 10 lines
target/i386/sev.c +10
-0+/* 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_common->launch_vmsa, launch_vmsa, next); + } + +/* Synchronise the VMSA with the current CPU state */ +sev_apply_cpu_context (cpu);Ok so here you are not only saving the vmsa but also applying it.
Quoted 125 lines
target/i386/sev.c +4
-0+ +return 0 ; +} +bool
sev_enabled(void)
{target/i386/sev.c +91
-13@@ -1005,6 +1183,16 @@ static int sev_launch_update_vmsa(SevGuestState *sev_guest) {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_COMMON (sev_guest)->sev_fd,KVM_SEV_LAUNCH_UPDATE_VMSA ,NULL , &fw_error);@@ -1787,40 +1975,110 @@ 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) + +static void seg_to_vmsa (const SegmentCache *cpu_seg,struct vmcb_seg *vmsa_seg) { -X86CPU *x86; -CPUX86State *env; -ConfidentialGuestSupport *cgs =MACHINE (qdev_get_machine ())->cgs; -SevCommonState *sev_common =SEV_COMMON ( -object_dynamic_cast (OBJECT (cgs),TYPE_SEV_COMMON )); + 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); +} -/* Only update if we have valid reset information */ -if (!sev_common || !sev_common->reset_data_valid) { -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; -/* Do not update the BSP reset state */ -if (cpu->cpu_index ==0 ) { -return ; +/* +* 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 ]; + vmsa->xcr0 = env->xcr0; + vmsa->g_pat = env->pat; + +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); +seg_to_vmsa (&env->ldt, &vmsa->ldtr); +seg_to_vmsa (&env->tr, &vmsa->tr); + + vmsa->dr6 = env->dr[6 ]; + vmsa->dr7 = env->dr[7 ]; + + 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; + vmsa->rflags = env->eflags; +} + +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);
Quoted 8 lines
target/i386/sev.c sev_es_find_reset_vector(void *flash_ptr, uint64_t flash_size, · line 2067
Quoted 6 lines
target/i386/sev.c +6
-0+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 , &error_fatal); +sev_apply_cpu_context (cpu);Is the above call ^^^^^^
sev_apply_cpu_context()needed? Since insev_set_cpu_context()you already call it?
It seems I would have the same question for v8.
Quoted 2 lines
Quoted 2 lines
target/i386/sev.c +0
-0}
A message that is not in the archive (sent off-list or not yet received): ec4fb8e4.AUUAAGN5T_UAAAAAAAAAA9cBm3AAAYKJZwAAAAAAAC5ATwBnwULW@mailjet.com
Re: [PATCH v7 15/16] i386/sev: Add implementation of CGS set_guest_policy()
Quoted 92 lines · Roy Hopkins
The new
cgs_set_guest_policy()function is provided to receive the guest policy flags, SNP ID block and SNP ID authentication from guest configuration such as an IGVM file and apply it to the platform prior to launching the guest.The policy is used to populate values for the existing 'policy', '
id_block' and 'id_auth' parameters. When provided, the guest policy is applied and the ID block configuration is used to verify the launch measurement and signatures. The guest is only successfully started if the expected launch measurements match the actual measurements and the signatures are valid.Signed-off-by Roy Hopkins <[email protected]>---
target/i386/sev.c| 83 +++++++++++++++++++++++++++++++++++++++++++++++target/i386/sev.h| 12 +++++++2 files changed, 95 insertions(+)target/i386/sev.c +60
-0@@ -2526,6 +2526,88 @@ static int cgs_get_mem_map_entry(int index,return 0 ; } +static int cgs_set_guest_policy (ConfidentialGuestPolicyType policy_type, +uint64_t policy,void *policy_data1, +uint32_t policy_data1_size,void *policy_data2, +uint32_t policy_data2_size,Error **errp) +{ +if (policy_type !=GUEST_POLICY_SEV ) { +error_setg (errp,"%s: Invalid guest policy type provided for SEV: %d" , + __func__, policy_type); +return -1 ; + } +/* +* SEV-SNP handles policy differently. The policy flags are defined in +* kvm_start_conf.policy and an ID block and ID auth can be provided. +*/ +if (sev_snp_enabled ()) { +SevSnpGuestState *sev_snp_guest = +SEV_SNP_GUEST (MACHINE (qdev_get_machine ())->cgs); +struct kvm_sev_snp_launch_finish *finish = + &sev_snp_guest->kvm_finish_conf; + +/* +* The policy consists of flags in 'policy' and optionally an ID block +* and ID auth in policy_data1 and policy_data2 respectively. The ID +* block and auth are optional so clear any previous ID block and auth +* and set them if provided, but always set the policy flags. +*/ +g_free (sev_snp_guest->id_block); +g_free ((guchar *)finish->id_block_uaddr); +g_free (sev_snp_guest->id_auth); +g_free ((guchar *)finish->id_auth_uaddr); + sev_snp_guest->id_block =NULL ; + finish->id_block_uaddr =0 ; + sev_snp_guest->id_auth =NULL ; + finish->id_auth_uaddr =0 ; + +if (policy_data1_size >0 ) { +struct sev_snp_id_authentication *id_auth = + (struct sev_snp_id_authentication *)policy_data2; + +if (policy_data1_size !=KVM_SEV_SNP_ID_BLOCK_SIZE ) { +error_setg (errp,"%s: Invalid SEV-SNP ID block: incorrect size" , + __func__); +return -1 ; + } +if (policy_data2_size !=KVM_SEV_SNP_ID_AUTH_SIZE ) { +error_setg (errp, +"%s: Invalid SEV-SNP ID auth block: incorrect size" , + __func__); +return -1 ; + } +assert (policy_data1 !=NULL ); +assert (policy_data2 !=NULL ); + + finish->id_block_uaddr = + (__u64 )g_memdup2 (policy_data1,KVM_SEV_SNP_ID_BLOCK_SIZE ); + finish->id_auth_uaddr = + (__u64 )g_memdup2 (policy_data2,KVM_SEV_SNP_ID_AUTH_SIZE ); + +/* +* Check if an author key has been provided and use that to flag
target/i386/sev.c static int cgs_get_mem_map_entry(int index, · line 2598
Quoted 10 lines
target/i386/sev.c +10
-0+* whether the author key is enabled. The first of the author key +* must be non-zero to indicate the key type, which will currently +* always be 2. +*/ + sev_snp_guest->kvm_finish_conf.auth_key_en = + id_auth->author_key[0 ] ?1 :0 ; + finish->id_block_en =1 ; + } + sev_snp_guest->kvm_start_conf.policy = policy; + }else {
I do not see how this “else” part (sev and sev-es) will ever be executed since qigvm_handle_policy() in patch #14 only calls set_guest_policy() if its SEV-SNP.
Re: [PATCH v7 15/16] i386/sev: Add implementation of CGS set_guest_policy()
Quoted 97 lines · Ani Sinha
Quoted 97 lines · Roy Hopkins
The new
cgs_set_guest_policy()function is provided to receive the guest policy flags, SNP ID block and SNP ID authentication from guest configuration such as an IGVM file and apply it to the platform prior to launching the guest.The policy is used to populate values for the existing 'policy', '
id_block' and 'id_auth' parameters. When provided, the guest policy is applied and the ID block configuration is used to verify the launch measurement and signatures. The guest is only successfully started if the expected launch measurements match the actual measurements and the signatures are valid.Signed-off-by Roy Hopkins <[email protected]>---
target/i386/sev.c| 83 +++++++++++++++++++++++++++++++++++++++++++++++target/i386/sev.h| 12 +++++++2 files changed, 95 insertions(+)target/i386/sev.c +65
-0@@ -2526,6 +2526,88 @@ static int cgs_get_mem_map_entry(int index, return 0; } +static int cgs_set_guest_policy (ConfidentialGuestPolicyType policy_type, + uint64_t policy, void *policy_data1, + uint32_t policy_data1_size, void *policy_data2, + uint32_t policy_data2_size, Error **errp) +{ + if (policy_type != GUEST_POLICY_SEV) { + error_setg(errp, "%s: Invalid guest policy type provided for SEV: %d", + __func__, policy_type); + return -1; + } + /* + * SEV-SNP handles policy differently. The policy flags are defined in + * kvm_start_conf.policy and an ID block and ID auth can be provided. + */ + if (sev_snp_enabled()) { + SevSnpGuestState *sev_snp_guest = + SEV_SNP_GUEST(MACHINE(qdev_get_machine())->cgs); + struct kvm_sev_snp_launch_finish *finish = + &sev_snp_guest->kvm_finish_conf; + + /* + * The policy consists of flags in 'policy' and optionally an ID block + * and ID auth in policy_data1 and policy_data2 respectively. The ID + * block and auth are optional so clear any previous ID block and auth + * and set them if provided, but always set the policy flags. + */ + g_free(sev_snp_guest->id_block); + g_free((guchar *)finish->id_block_uaddr); + g_free(sev_snp_guest->id_auth); + g_free((guchar *)finish->id_auth_uaddr); + sev_snp_guest->id_block = NULL; + finish->id_block_uaddr = 0; + sev_snp_guest->id_auth = NULL; + finish->id_auth_uaddr = 0; + + if (policy_data1_size > 0) { + struct sev_snp_id_authentication *id_auth = + (struct sev_snp_id_authentication *)policy_data2; + + if (policy_data1_size != KVM_SEV_SNP_ID_BLOCK_SIZE) { + error_setg(errp, "%s: Invalid SEV-SNP ID block: incorrect size", + __func__); + return -1; + } + if (policy_data2_size != KVM_SEV_SNP_ID_AUTH_SIZE) { + error_setg(errp, + "%s: Invalid SEV-SNP ID auth block: incorrect size", + __func__); + return -1; + } + assert(policy_data1 != NULL); + assert(policy_data2 != NULL); + + finish->id_block_uaddr = + (__u64)g_memdup2(policy_data1, KVM_SEV_SNP_ID_BLOCK_SIZE); + finish->id_auth_uaddr = + (__u64)g_memdup2(policy_data2, KVM_SEV_SNP_ID_AUTH_SIZE); + + /* + * Check if an author key has been provided and use that to flag + * whether the author key is enabled. The first of the author key + * must be non-zero to indicate the key type, which will currently + * always be 2. + */ + sev_snp_guest->kvm_finish_conf.auth_key_en =
Quoted 9 lines
target/i386/sev.c static int cgs_get_mem_map_entry(int index, · line 2598
Quoted 5 lines
target/i386/sev.c +5
-0+ id_auth->author_key[0] ? 1 : 0; + finish->id_block_en = 1; + } + sev_snp_guest->kvm_start_conf.policy = policy; + } else {I do not see how this “else” part (sev and sev-es) will ever be executed since
qigvm_handle_policy()in patch #14 only callsset_guest_policy()if its SEV- SNP.
You're correct. There is currently no way for this code to be be called. But as it is an interface function on confidential-guest-support, it could be called erroneously in the future so I don't see any harm in leaving the error check there.
Thanks,
Roy