igvm: misc changes and device tree support
PATCH v6 1/3 igvm: Report error on missing parameter area in directive handlers
In reply to Luigi LeonardiParameter areas are how an IGVM file tells QEMU to allocate buffers for runtime information the guest needs — VP count, memory map, MADT and so on. Usage directives reference a parameter area by index to tell QEMU where to write each piece of data. If the index doesn't match any declared parameter area, the data has nowhere to go and should be treated as an error.
The directive handlers that look up a parameter area all return 0 (success) when qigvm_find_param_entry() can't find it. Therefore, the load succeeds but the guest never gets the expected parameters.
Note that the IGVM library already validates parameter area indices when the file is loaded, so this path should only be reachable with a malformed file that bypassed library validation. This is defensive programming against that case.
Report the error with error_setg() and return -1 instead.
--- backends/igvm.c | 26 ++++++++++++++++---------- include/system/igvm-internal.h | 3 ++- target/i386/igvm.c | 5 +++-- 3 files changed, 21 insertions(+), 13 deletions(-)
backends/igvm.c +16 -10
@@ -81,7 +81,8 @@ struct QEMU_PACKED sev_id_authentication {#define IGVM_SEV_ID_BLOCK_VERSION 1QIgvmParameterData * -qigvm_find_param_entry (QIgvm *igvm,uint32_t parameter_area_index) +qigvm_find_param_entry (QIgvm *igvm,uint32_t parameter_area_index, +Error **errp) {QIgvmParameterData *param_entry;QTAILQ_FOREACH (param_entry, &igvm->parameter_data, next)
@@ -90,7 +91,8 @@ qigvm_find_param_entry(QIgvm *igvm, uint32_t parameter_area_index)return param_entry; } } -warn_report ("IGVM: No parameter area for index %u" , parameter_area_index); +error_setg (errp,"IGVM: parameter area index %u not found" , + parameter_area_index);return NULL ; }
@@ -528,9 +530,10 @@ static int qigvm_directive_parameter_insert(QIgvm *ctx,return 0 ; } - param_entry =qigvm_find_param_entry (ctx, param->parameter_area_index); + param_entry =qigvm_find_param_entry (ctx, + param->parameter_area_index, errp);if (param_entry ==NULL ) { -return 0 ; +return -1 ; } region = qigvm_prepare_memory(ctx, param->gpa, param_entry->size,
@@ -601,9 +604,10 @@ static int qigvm_directive_memory_map(QIgvm *ctx, const uint8_t *header_data, }/* Find the parameter area that should hold the memory map */ - param_entry =qigvm_find_param_entry (ctx, param->parameter_area_index); + param_entry =qigvm_find_param_entry (ctx, + param->parameter_area_index, errp);if (param_entry ==NULL ) { -return 0 ; +return -1 ; } max_entry_count = param_entry->size /sizeof (IGVM_VHS_MEMORY_MAP_ENTRY );
@@ -660,9 +664,10 @@ static int qigvm_directive_vp_count(QIgvm *ctx, const uint8_t *header_data,uint32_t *vp_count;CPUState *cpu; - param_entry =qigvm_find_param_entry (ctx, param->parameter_area_index); + param_entry =qigvm_find_param_entry (ctx, + param->parameter_area_index, errp);if (param_entry ==NULL ) { -return 0 ; +return -1 ; } vp_count = (uint32_t *)(param_entry->data + param->byte_offset);
@@ -683,9 +688,10 @@ static int qigvm_directive_environment_info(QIgvm *ctx,QIgvmParameterData *param_entry;IgvmEnvironmentInfo *environmental_state; - param_entry =qigvm_find_param_entry (ctx, param->parameter_area_index); + param_entry =qigvm_find_param_entry (ctx, + param->parameter_area_index, errp);if (param_entry ==NULL ) { -return 0 ; +return -1 ; } environmental_state =
include/system/igvm-internal.h +2 -1
@@ -72,6 +72,7 @@ struct QIgvm {IgvmHandle qigvm_file_init (char *filename,Error **errp);QIgvmParameterData * -qigvm_find_param_entry (QIgvm *igvm,uint32_t parameter_area_index); +qigvm_find_param_entry (QIgvm *igvm,uint32_t parameter_area_index, +Error **errp);#endif
target/i386/igvm.c +3 -2
@@ -191,9 +191,10 @@ int qigvm_directive_madt(QIgvm *ctx, const uint8_t *header_data, Error **errp)int result =0 ;/* Find the parameter area that should hold the MADT data */ - param_entry =qigvm_find_param_entry (ctx, param->parameter_area_index); + param_entry =qigvm_find_param_entry (ctx, + param->parameter_area_index, errp);if (param_entry ==NULL ) { -return 0 ; +return -1 ; }GArray *madt =acpi_build_madt_standalone (ctx->machine_state);
Signature · 1 line
2.54.0
PATCH v6 3/3 igvm: add device tree parameter support
In reply to Luigi LeonardiCoconut SVSM, with the upcoming device tree support [1], will use the IGVM device tree parameter to discover virtio-mmio and ISA serial devices instead of relying on the fw_cfg interface, which is QEMU-specific.
The device tree is packed before copying into the IGVM parameter area to reduce its size, since IGVM files can define tighter memory constraints for parameter areas. Packing is done in the generic IGVM backend rather than in per-architecture device tree setup code, so that each architecture does not need to handle it individually.
[1] https://github.com/coconut-svsm/svsm/pull/1006
--- backends/igvm.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ backends/meson.build | 2 +- 2 files changed, 55 insertions(+), 1 deletion(-)
backends/igvm.c +54 -0
@@ -26,6 +26,10 @@#include <igvm/igvm.h> #include <igvm/igvm_defs.h> +#ifdef CONFIG_FDT +#include <libfdt.h> +#endif +#ifndef IGVM_VHT_OPTIONAL_BIT #define IGVM_VHT_OPTIONAL_BIT (1U << 31)#endif
@@ -121,6 +125,10 @@ static int qigvm_directive_snp_id_block(QIgvm *ctx, const uint8_t *header_data,static int qigvm_initialization_guest_policy (QIgvm *ctx,const uint8_t *header_data,Error **errp); +#ifdef CONFIG_FDT +static int qigvm_directive_device_tree (QIgvm *ctx,const uint8_t *header_data, +Error **errp); +#endif struct QIGVMHandler {IgvmVariableHeaderType type;
@@ -151,6 +159,10 @@ static struct QIGVMHandler handlers[] = { qigvm_initialization_guest_policy }, { IGVM_VHT_MADT, IGVM_HEADER_SECTION_DIRECTIVE, qigvm_directive_madt }, +#ifdef CONFIG_FDT + { IGVM_VHT_DEVICE_TREE, IGVM_HEADER_SECTION_DIRECTIVE, + qigvm_directive_device_tree }, +#endif }; static int qigvm_handler(QIgvm *ctx, IgvmVariableHeaderType raw_type,
@@ -786,6 +798,48 @@ static int qigvm_directive_snp_id_block(QIgvm *ctx, const uint8_t *header_data,return 0 ; } +#ifdef CONFIG_FDT +static int qigvm_directive_device_tree (QIgvm *ctx,const uint8_t *header_data, +Error **errp) +{ +const IGVM_VHS_PARAMETER *param = (const IGVM_VHS_PARAMETER *)header_data; + g_autofree void *fdt_packed = NULL; +QIgvmParameterData *param_entry; +uint32_t fdt_size; + + param_entry =qigvm_find_param_entry (ctx, + param->parameter_area_index, errp); +if (param_entry ==NULL ) { +return -1 ; + } + +if (ctx->machine_state->fdt ==NULL ) { +error_setg (errp,"IGVM: device tree not available" ); +return -1 ; + } + + fdt_size =fdt_totalsize (ctx->machine_state->fdt); + fdt_packed =g_memdup2 (ctx->machine_state->fdt, fdt_size); + +if (fdt_pack (fdt_packed)) { +error_setg (errp,"IGVM: failed to pack device tree" ); +return -1 ; + } + + fdt_size =fdt_totalsize (fdt_packed); +if (fdt_size > param_entry->size) { +error_setg (errp, +"IGVM: device tree size exceeds parameter area" +" defined in IGVM file" ); +return -1 ; + } + +memcpy (param_entry->data, fdt_packed, fdt_size); + +return 0 ; +} +#endif +static int qigvm_initialization_guest_policy (QIgvm *ctx,const uint8_t *header_data,Error **errp) {
backends/meson.build +1 -1
@@ -35,7 +35,7 @@ endif system_ss.add(when: gio, if_true: files('dbus-vmstate.c')) system_ss.add(when: 'CONFIG_SGX', if_true: files('hostmem-epc.c')) -system_ss.add(when: igvm, if_true: [files('igvm-cfg.c', 'igvm.c')]) +system_ss.add(when: igvm, if_true: [files('igvm-cfg.c', 'igvm.c'), fdt]) system_ss.add(when: 'CONFIG_SPDM_SOCKET', if_true: files('spdm-socket.c'))
Signature · 1 line
2.54.0
v6 cover igvm: misc changes and device tree support
This device tree will be consumed by Coconut SVSM with the upcoming device tree support [1]. This will allow SVSM to run on a normal CPU (not AMD SEV-SNP) and execute all its tests using upstream QEMU.
Eventually edk2 will also be able to consume DT via IGVM, but the work has not started yet.
This series contains one patch that simplifies meson logic and another one that changes the return behaviour of qigvm_find_param_entry.
This is to avoid conflicts with the device tree patch itself, that needs to modify the same lines.
[1] https://github.com/coconut-svsm/svsm/pull/1006
---
Changes in v6:
- Picked up https://lore.kernel.org/qemu-devel/[email protected]/
and fixed conflicts
- Link to v5: https://lore.kernel.org/qemu-devel/[email protected]
Changes in v5:
- Added a patch, suggested by Paolo, to simplify current igvm meson config
- Added missing fdt dependency for igvm in meson.
- Link to v4: https://lore.kernel.org/qemu-devel/[email protected]
Changes in v4:
- Using g_memdup2 [Stefano]
- madt directive is now protected by CONFIG_FDT [Stefano]
- Link to v3: https://lore.kernel.org/qemu-devel/[email protected]
Changes in v3:
- Device tree is packed in igvm core code. [Gerd]
- Dropped microvm patch.
- Link to v2: https://lore.kernel.org/qemu-devel/[email protected]
Changes in v2:
- First patch introduced a shadowed variable, fixed.
- When parameter area is not found return -1.
- Moved qigvm_directive_device_tree to backends/igvm.c
- Link to v1: https://lore.kernel.org/qemu-devel/[email protected]
---
Luigi Leonardi (3):
igvm: Report error on missing parameter area in directive handlers
igvm: use idiomatic meson conditional for IGVM build files
igvm: add device tree parameter support
backends/igvm.c | 80 ++++++++++++++++++++++++++++++++++++------ backends/meson.build | 7 ++-- include/system/igvm-internal.h | 3 +- target/i386/igvm.c | 5 +-- 4 files changed, 77 insertions(+), 18 deletions(-)
---
base-commit: 60533c6193ede6ce403e82d09d82ae2bc8fb423a
change-id: 20260608-microvm_device_tree-0263c3b1be86
Best regards,
Signature · 1 line
Luigi Leonardi <[email protected]>
PATCH v6 2/3 igvm: use idiomatic meson conditional for IGVM build files
Replace the explicit igvm.found() check with system_ss.add(when:, if_true:), matching the pattern used by all other optional backends.
--- backends/meson.build | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
backends/meson.build +2 -5
@@ -34,11 +34,8 @@ if have_vhost_user_crypto endif system_ss.add(when: gio, if_true: files('dbus-vmstate.c')) system_ss.add(when: 'CONFIG_SGX', if_true: files('hostmem-epc.c')) -if igvm.found() - system_ss.add(igvm) - system_ss.add(files('igvm-cfg.c'), igvm) - system_ss.add(files('igvm.c'), igvm) -endif + +system_ss.add(when: igvm, if_true: [files('igvm-cfg.c', 'igvm.c')]) system_ss.add(when: 'CONFIG_SPDM_SOCKET', if_true: files('spdm-socket.c'))
Signature · 1 line
2.54.0
Re: [PATCH v6 1/3] igvm: Report error on missing parameter area in directive handlers
In reply to Luigi LeonardiQuoted 16 lines · Luigi Leonardi
Parameter areas are how an IGVM file tells QEMU to allocate buffers for runtime information the guest needs — VP count, memory map, MADT and so on. Usage directives reference a parameter area by index to tell QEMU where to write each piece of data. If the index doesn't match any declared parameter area, the data has nowhere to go and should be treated as an error.
The directive handlers that look up a parameter area all return 0 (success) when
qigvm_find_param_entry()can't find it. Therefore, the load succeeds but the guest never gets the expected parameters.Note that the IGVM library already validates parameter area indices when the file is loaded, so this path should only be reachable with a malformed file that bypassed library validation. This is defensive programming against that case.
Quoted 8 lines
Report the error with
error_setg()and return -1 instead.Signed-off-by Luigi Leonardi <[email protected]>---
backends/igvm.c | 26 ++++++++++++++++---------- include/system/igvm-internal.h | 3 ++- target/i386/igvm.c | 5 +++-- 3 files changed, 21 insertions(+), 13 deletions(-)
Quoted 111 lines
backends/igvm.c +0
-0@@ -81,7 +81,8 @@ struct QEMU_PACKED sev_id_authentication {#define IGVM_SEV_ID_BLOCK_VERSION 1QIgvmParameterData*
backends/igvm.c +2
-1-qigvm_find_param_entry(QIgvm *igvm, uint32_t parameter_area_index) +qigvm_find_param_entry(QIgvm *igvm, uint32_t parameter_area_index, + Error **errp){ QIgvmParameterData *param_entry; QTAILQ_FOREACH(param_entry, &igvm->parameter_data, next)backends/igvm.c +14
-9@@ -90,7 +91,8 @@ qigvm_find_param_entry(QIgvm *igvm, uint32_t parameter_area_index)return param_entry; } } -warn_report ("IGVM: No parameter area for index %u" , parameter_area_index); +error_setg (errp,"IGVM: parameter area index %u not found" , + parameter_area_index);return NULL ; }@@ -528,9 +530,10 @@ static int qigvm_directive_parameter_insert(QIgvm *ctx,return 0 ; } - param_entry =qigvm_find_param_entry (ctx, param->parameter_area_index); + param_entry =qigvm_find_param_entry (ctx, + param->parameter_area_index, errp);if (param_entry ==NULL ) { -return 0 ; +return -1 ; } region = qigvm_prepare_memory(ctx, param->gpa, param_entry->size,@@ -601,9 +604,10 @@ static int qigvm_directive_memory_map(QIgvm *ctx, const uint8_t *header_data, }/* Find the parameter area that should hold the memory map */ - param_entry =qigvm_find_param_entry (ctx, param->parameter_area_index); + param_entry =qigvm_find_param_entry (ctx, + param->parameter_area_index, errp);if (param_entry ==NULL ) { -return 0 ; +return -1 ; } max_entry_count = param_entry->size /sizeof (IGVM_VHS_MEMORY_MAP_ENTRY );@@ -660,9 +664,10 @@ static int qigvm_directive_vp_count(QIgvm *ctx, const uint8_t *header_data,uint32_t *vp_count;CPUState *cpu; - param_entry =qigvm_find_param_entry (ctx, param->parameter_area_index); + param_entry =qigvm_find_param_entry (ctx, + param->parameter_area_index, errp);if (param_entry ==NULL ) { -return 0 ; +return -1 ; } vp_count = (uint32_t *)(param_entry->data + param->byte_offset);@@ -683,9 +688,10 @@ static int qigvm_directive_environment_info(QIgvm *ctx,QIgvmParameterData *param_entry;IgvmEnvironmentInfo *environmental_state; - param_entry =qigvm_find_param_entry (ctx, param->parameter_area_index); + param_entry =qigvm_find_param_entry (ctx, + param->parameter_area_index, errp);if (param_entry ==NULL ) { -return 0 ; +return -1 ; } environmental_state =include/system/igvm-internal.h +0
-0@@ -72,6 +72,7 @@ struct QIgvm {IgvmHandle qigvm_file_init (char *filename,Error **errp);QIgvmParameterData*
include/system/igvm-internal.h +2
-1-qigvm_find_param_entry(QIgvm *igvm, uint32_t parameter_area_index); +qigvm_find_param_entry(QIgvm *igvm, uint32_t parameter_area_index, + Error **errp);#endif
target/i386/igvm.c +3
-2@@ -191,9 +191,10 @@ int qigvm_directive_madt(QIgvm *ctx, const uint8_t *header_data, Error **errp)int result =0 ;/* Find the parameter area that should hold the MADT data */ - param_entry =qigvm_find_param_entry (ctx, param->parameter_area_index); + param_entry =qigvm_find_param_entry (ctx, + param->parameter_area_index, errp);if (param_entry ==NULL ) { -return 0 ; +return -1 ; }GArray *madt =acpi_build_madt_standalone (ctx->machine_state);Signature · 2 lines
-- 2.54.0
Re: [PATCH v6 2/3] igvm: use idiomatic meson conditional for IGVM build files
In reply to Luigi LeonardiQuoted 8 lines · Luigi Leonardi
Replace the explicit
igvm.found()check withsystem_ss.add(when:, if_true:), matching the pattern used by all other optional backends.Suggested-by Paolo Bonzini <[email protected]>Signed-off-by Luigi Leonardi <[email protected]>---
backends/meson.build | 7 ++-----1 file changed, 2 insertions(+), 5 deletions(-)
Quoted 23 lines
backends/meson.build +0
-0@@ -34,11 +34,8 @@ if have_vhost_user_cryptoendif
system_ss.add(when: gio, if_true: files('dbus-vmstate.c'))system_ss.add(when: 'CONFIG_SGX', if_true: files('hostmem-epc.c'))backends/meson.build +2
-5-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: igvm, if_true: [files('igvm-cfg.c', 'igvm.c')])
system_ss.add(when: 'CONFIG_SPDM_SOCKET', if_true: files('spdm-socket.c'))Signature · 1 line
2.54.0
Re: [PATCH v6 3/3] igvm: add device tree parameter support
In reply to Luigi LeonardiQuoted 11 lines · Luigi Leonardi
Coconut SVSM, with the upcoming device tree support [1], will use the IGVM device tree parameter to discover virtio-mmio and ISA serial devices instead of relying on the
fw_cfginterface, which is QEMU-specific.The device tree is packed before copying into the IGVM parameter area to reduce its size, since IGVM files can define tighter memory constraints for parameter areas. Packing is done in the generic IGVM backend rather than in per-architecture device tree setup code, so that each architecture does not need to handle it individually.
Quoted 7 lines
[1] https://github.com/coconut-svsm/svsm/pull/1006
Signed-off-by Luigi Leonardi <[email protected]>---
backends/igvm.c| 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++
backends/meson.build | 2 +-2 files changed, 55 insertions(+), 1 deletion(-)
Quoted 104 lines
backends/igvm.c +54
-0@@ -26,6 +26,10 @@#include <igvm/igvm.h> #include <igvm/igvm_defs.h> +#ifdef CONFIG_FDT +#include <libfdt.h> +#endif +#ifndef IGVM_VHT_OPTIONAL_BIT #define IGVM_VHT_OPTIONAL_BIT (1U << 31)#endif @@ -121,6 +125,10 @@ static int qigvm_directive_snp_id_block(QIgvm *ctx, const uint8_t *header_data,static int qigvm_initialization_guest_policy (QIgvm *ctx,const uint8_t *header_data,Error **errp); +#ifdef CONFIG_FDT +static int qigvm_directive_device_tree (QIgvm *ctx,const uint8_t *header_data, +Error **errp); +#endif struct QIGVMHandler {IgvmVariableHeaderType type;@@ -151,6 +159,10 @@ static struct QIGVMHandler handlers[] = { qigvm_initialization_guest_policy }, { IGVM_VHT_MADT, IGVM_HEADER_SECTION_DIRECTIVE, qigvm_directive_madt }, +#ifdef CONFIG_FDT + { IGVM_VHT_DEVICE_TREE, IGVM_HEADER_SECTION_DIRECTIVE, + qigvm_directive_device_tree }, +#endif }; static int qigvm_handler(QIgvm *ctx, IgvmVariableHeaderType raw_type,@@ -786,6 +798,48 @@ static int qigvm_directive_snp_id_block(QIgvm *ctx, const uint8_t *header_data,return 0 ; } +#ifdef CONFIG_FDT +static int qigvm_directive_device_tree (QIgvm *ctx,const uint8_t *header_data, +Error **errp) +{ +const IGVM_VHS_PARAMETER *param = (const IGVM_VHS_PARAMETER *)header_data; + g_autofree void *fdt_packed = NULL; +QIgvmParameterData *param_entry; +uint32_t fdt_size; + + param_entry =qigvm_find_param_entry (ctx, + param->parameter_area_index, errp); +if (param_entry ==NULL ) { +return -1 ; + } + +if (ctx->machine_state->fdt ==NULL ) { +error_setg (errp,"IGVM: device tree not available" ); +return -1 ; + } + + fdt_size =fdt_totalsize (ctx->machine_state->fdt); + fdt_packed =g_memdup2 (ctx->machine_state->fdt, fdt_size); + +if (fdt_pack (fdt_packed)) { +error_setg (errp,"IGVM: failed to pack device tree" ); +return -1 ; + } + + fdt_size =fdt_totalsize (fdt_packed); +if (fdt_size > param_entry->size) { +error_setg (errp, +"IGVM: device tree size exceeds parameter area" +" defined in IGVM file" ); +return -1 ; + } + +memcpy (param_entry->data, fdt_packed, fdt_size); + +return 0 ; +} +#endif +static int qigvm_initialization_guest_policy (QIgvm *ctx,const uint8_t *header_data,Error **errp) {backends/meson.build +1
-1@@ -35,7 +35,7 @@ endif system_ss.add(when: gio, if_true: files('dbus-vmstate.c')) system_ss.add(when: 'CONFIG_SGX', if_true: files('hostmem-epc.c')) -system_ss.add(when: igvm, if_true: [files('igvm-cfg.c', 'igvm.c')]) +system_ss.add(when: igvm, if_true: [files('igvm-cfg.c', 'igvm.c'), fdt]) system_ss.add(when: 'CONFIG_SPDM_SOCKET', if_true: files('spdm-socket.c'))Signature · 2 lines
-- 2.54.0