i386: firmware parsing and sev setup for -bios loaded firmware
cover i386: firmware parsing and sev setup for -bios loaded firmware
Gerd Hoffmann (3):
i386: move bios load error message
i386: factor out x86_firmware_configure()
i386: firmware parsing and sev setup for -bios loaded firmware
include/hw/i386/x86.h | 3 +++ hw/i386/pc_sysfw.c | 36 ++++++++++++++++++++++-------------- hw/i386/x86.c | 32 ++++++++++++++++++++++++-------- 3 files changed, 49 insertions(+), 22 deletions(-)
Signature · 1 line
2.35.1
PATCH 1/3 i386: move bios load error message
Switch to usual goto-end-of-function error handling style. No functional change.
--- hw/i386/x86.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
hw/i386/x86.c +6 -3
@@ -1121,9 +1121,7 @@ void x86_bios_rom_init(MachineState *ms, const char *default_firmware, } ret =rom_add_file_fixed (bios_name, (uint32_t )(-bios_size),-1 );if (ret !=0 ) { - bios_error: -fprintf (stderr,"qemu: could not load PC BIOS '%s'\n" , bios_name); -exit (1 ); + goto bios_error; }g_free (filename);
@@ -1144,6 +1142,11 @@ void x86_bios_rom_init(MachineState *ms, const char *default_firmware,memory_region_add_subregion (rom_memory, (uint32_t )(-bios_size), bios); +return ; + +bios_error: +fprintf (stderr,"qemu: could not load PC BIOS '%s'\n" , bios_name); +exit (1 ); }bool x86_machine_is_smm_enabled (const X86MachineState *x86ms)
Signature · 1 line
2.35.1
Re: [PATCH 1/3] i386: move bios load error message
Quoted 8 lines · Gerd Hoffmann
Switch to usual goto-end-of-function error handling style. No functional change.
Signed-off-by Gerd Hoffmann <[email protected]>Tested-by Xiaoyao Li <[email protected]>--- hw/i386/x86.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH 1/3] i386: move bios load error message
In reply to Gerd HoffmannQuoted 8 lines · Gerd Hoffmann
Switch to usual goto-end-of-function error handling style. No functional change.
Signed-off-by Gerd Hoffmann <[email protected]>Tested-by Xiaoyao Li <[email protected]>--- hw/i386/x86.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
PATCH 2/3 i386: factor out x86_firmware_configure()
In reply to Gerd Hoffmannmove sev firmware setup to separate function so it can be used from other code paths. No functional change.
--- include/hw/i386/x86.h | 3 +++ hw/i386/pc_sysfw.c | 36 ++++++++++++++++++++++-------------- 2 files changed, 25 insertions(+), 14 deletions(-)
include/hw/i386/x86.h +3 -0
@@ -140,4 +140,7 @@ void gsi_handler(void *opaque, int n, int level);void ioapic_init_gsi (GSIState *gsi_state,const char *parent_name);DeviceState *ioapic_init_secondary (GSIState *gsi_state); +/* pc_sysfw.c */ +void x86_firmware_configure (void *ptr,int size); +#endif
hw/i386/pc_sysfw.c +22 -14
@@ -148,7 +148,6 @@ static void pc_system_flash_map(PCMachineState *pcms,MemoryRegion *flash_mem;void *flash_ptr;int flash_size; -int ret;assert (PC_MACHINE_GET_CLASS (pcms)->pci_enabled);
@@ -196,19 +195,7 @@ static void pc_system_flash_map(PCMachineState *pcms,if (sev_enabled ()) { flash_ptr =memory_region_get_ram_ptr (flash_mem); flash_size =memory_region_size (flash_mem); -/* -* OVMF places a GUIDed structures in the flash, so -* search for them -*/ -pc_system_parse_ovmf_flash (flash_ptr, flash_size); - - ret =sev_es_save_reset_vector (flash_ptr, flash_size); -if (ret) { -error_report ("failed to locate and/or save reset vector" ); -exit (1 ); - } - -sev_encrypt_flash (flash_ptr, flash_size, &error_fatal); +x86_firmware_configure (flash_ptr, flash_size); } } }
@@ -260,3 +247,24 @@ void pc_system_firmware_init(PCMachineState *pcms,pc_system_flash_cleanup_unused (pcms); } + +void x86_firmware_configure (void *ptr,int size) +{ +int ret; + +/* +* OVMF places a GUIDed structures in the flash, so +* search for them +*/ +pc_system_parse_ovmf_flash (ptr, size); + +if (sev_enabled ()) { + ret =sev_es_save_reset_vector (ptr, size); +if (ret) { +error_report ("failed to locate and/or save reset vector" ); +exit (1 ); + } + +sev_encrypt_flash (ptr, size, &error_fatal); + } +}
Signature · 1 line
2.35.1
Re: [PATCH 2/3] i386: factor out x86_firmware_configure()
Quoted 59 lines · Gerd Hoffmann
move sev firmware setup to separate function so it can be used from other code paths. No functional change.
Signed-off-by Gerd Hoffmann <[email protected]>Tested-by Xiaoyao Li <[email protected]>--- include/hw/i386/x86.h | 3 +++ hw/i386/pc_sysfw.c | 36 ++++++++++++++++++++++-------------- 2 files changed, 25 insertions(+), 14 deletions(-)include/hw/i386/x86.h +3
-0@@ -140,4 +140,7 @@ void gsi_handler(void *opaque, int n, int level);void ioapic_init_gsi (GSIState *gsi_state,const char *parent_name);DeviceState *ioapic_init_secondary (GSIState *gsi_state); +/* pc_sysfw.c */ +void x86_firmware_configure (void *ptr,int size); +#endif hw/i386/pc_sysfw.c +1
-14@@ -148,7 +148,6 @@ static void pc_system_flash_map(PCMachineState *pcms,MemoryRegion *flash_mem;void *flash_ptr;int flash_size; -int ret;assert (PC_MACHINE_GET_CLASS (pcms)->pci_enabled);@@ -196,19 +195,7 @@ static void pc_system_flash_map(PCMachineState *pcms,if (sev_enabled ()) { flash_ptr =memory_region_get_ram_ptr (flash_mem); flash_size =memory_region_size (flash_mem); -/* -* OVMF places a GUIDed structures in the flash, so -* search for them -*/ -pc_system_parse_ovmf_flash (flash_ptr, flash_size); - - ret =sev_es_save_reset_vector (flash_ptr, flash_size); -if (ret) { -error_report ("failed to locate and/or save reset vector" ); -exit (1 ); - } - -sev_encrypt_flash (flash_ptr, flash_size, &error_fatal); +x86_firmware_configure (flash_ptr, flash_size); } } }@@ -260,3 +247,24 @@ void pc_system_firmware_init(PCMachineState *pcms,pc_system_flash_cleanup_unused (pcms); }
hw/i386/pc_sysfw.c void pc_system_firmware_init(PCMachineState *pcms, · line 259
Quoted 10 lines
hw/i386/pc_sysfw.c +10
-0+ +void x86_firmware_configure (void *ptr,int size) +{ +int ret; + +/* +* OVMF places a GUIDed structures in the flash, so +* search for them +*/ +pc_system_parse_ovmf_flash (ptr, size);
Any reason you chose to put this outside the sev_enabled() check when you moved it, as that is a functional change ?
It ought to be harmless in theory, unless someone figures out a way to break pc_system_parse_ovmf_flash code with unexpected input.
Quoted 15 lines
hw/i386/pc_sysfw.c +11
-0+ +if (sev_enabled ()) { + ret =sev_es_save_reset_vector (ptr, size); +if (ret) { +error_report ("failed to locate and/or save reset vector" ); +exit (1 ); + } + +sev_encrypt_flash (ptr, size, &error_fatal); + } +}Signature · 1 line
2.35.1
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH 2/3] i386: factor out x86_firmware_configure()
Hi,
Quoted 4 lines
Quoted 4 lines
hw/i386/pc_sysfw.c +4
-0+void x86_firmware_configure (void *ptr,int size) +{ +int ret; +
Quoted 8 lines
hw/i386/pc_sysfw.c void pc_system_firmware_init(PCMachineState *pcms, · line 259
Quoted 5 lines
hw/i386/pc_sysfw.c +5
-0+/* +* OVMF places a GUIDed structures in the flash, so +* search for them +*/ +pc_system_parse_ovmf_flash (ptr, size);Any reason you chose to put this outside the
sev_enabled()check when you moved it, as that is a functional change ?
Well, we probably get a 'if (tdx_enabled())' branch soon, and pc_system_parse_ovmf_flash() will be needed for both sev and tdx.
Quoted 3 lines
It ought to be harmless in theory, unless someone figures out a way to break
pc_system_parse_ovmf_flashcode with unexpected input.
Yes, strictly speaking this is a functional change. Without sev the pc_system_parse_ovmf_flash() results will be ignored though, so there should be no change in qemu behavior ...
take care,
Gerd
Re: [PATCH 2/3] i386: factor out x86_firmware_configure()
In reply to Gerd HoffmannQuoted 9 lines · Gerd Hoffmann
move sev firmware setup to separate function so it can be used from other code paths. No functional change.
Signed-off-by Gerd Hoffmann <[email protected]>Tested-by Xiaoyao Li <[email protected]>--- include/hw/i386/x86.h | 3 +++ hw/i386/pc_sysfw.c | 36 ++++++++++++++++++++++-------------- 2 files changed, 25 insertions(+), 14 deletions(-)
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH 2/3] i386: factor out x86_firmware_configure()
In reply to Gerd HoffmannQuoted 35 lines · Gerd Hoffmann
move sev firmware setup to separate function so it can be used from other code paths. No functional change.
Signed-off-by Gerd Hoffmann <[email protected]>Tested-by Xiaoyao Li <[email protected]>--- include/hw/i386/x86.h | 3 +++ hw/i386/pc_sysfw.c | 36 ++++++++++++++++++++++-------------- 2 files changed, 25 insertions(+), 14 deletions(-)include/hw/i386/x86.h +3
-0@@ -140,4 +140,7 @@ void gsi_handler(void *opaque, int n, int level);void ioapic_init_gsi (GSIState *gsi_state,const char *parent_name);DeviceState *ioapic_init_secondary (GSIState *gsi_state); +/* pc_sysfw.c */ +void x86_firmware_configure (void *ptr,int size); +#endif hw/i386/pc_sysfw.c +0
-1@@ -148,7 +148,6 @@ static void pc_system_flash_map(PCMachineState *pcms,MemoryRegion *flash_mem;void *flash_ptr;int flash_size; -int ret;assert (PC_MACHINE_GET_CLASS (pcms)->pci_enabled);
hw/i386/pc_sysfw.c static void pc_system_flash_map(PCMachineState *pcms, · line 195
Quoted 1 line
hw/i386/pc_sysfw.c +0
-0@@ -196,19 +195,7 @@ static void pc_system_flash_map(PCMachineState *pcms,if (sev_enabled ()) {
^^^
hw/i386/pc_sysfw.c static void pc_system_flash_map(PCMachineState *pcms, · line 197
Quoted 2 lines
hw/i386/pc_sysfw.c +0
-0flash_ptr =memory_region_get_ram_ptr (flash_mem); flash_size =memory_region_size (flash_mem);
Can we remove the SEV check ...
Quoted 4 lines
hw/i386/pc_sysfw.c +0
-4-/* -* OVMF places a GUIDed structures in the flash, so -* search for them -*/
hw/i386/pc_sysfw.c static void pc_system_flash_map(PCMachineState *pcms, · line 198
Quoted 10 lines
hw/i386/pc_sysfw.c +1
-9-pc_system_parse_ovmf_flash (flash_ptr, flash_size); - - ret =sev_es_save_reset_vector (flash_ptr, flash_size); -if (ret) { -error_report ("failed to locate and/or save reset vector" ); -exit (1 ); - } - -sev_encrypt_flash (flash_ptr, flash_size, &error_fatal); +x86_firmware_configure (flash_ptr, flash_size);
... making this code generic ...?
Quoted 9 lines
hw/i386/pc_sysfw.c +2
-0} } }@@ -260,3 +247,24 @@ void pc_system_firmware_init(PCMachineState *pcms,pc_system_flash_cleanup_unused (pcms); } + +void x86_firmware_configure (void *ptr,int size)
hw/i386/pc_sysfw.c void pc_system_firmware_init(PCMachineState *pcms, · line 261
Quoted 10 lines
hw/i386/pc_sysfw.c +10
-0+{ +int ret; + +/* +* OVMF places a GUIDed structures in the flash, so +* search for them +*/ +pc_system_parse_ovmf_flash (ptr, size); + +if (sev_enabled ()) {
... because we are still checking SEV here.
Quoted 9 lines
hw/i386/pc_sysfw.c +9
-0+ ret =sev_es_save_reset_vector (ptr, size); +if (ret) { +error_report ("failed to locate and/or save reset vector" ); +exit (1 ); + } + +sev_encrypt_flash (ptr, size, &error_fatal); + } +}
Re: [PATCH 2/3] i386: factor out x86_firmware_configure()
Quoted 11 lines
Quoted 1 line
if (sev_enabled()) {^^^Can we remove the SEV check ...
hw/i386/pc_sysfw.c void pc_system_firmware_init(PCMachineState *pcms, · line 261
Quoted 3 lines
hw/i386/pc_sysfw.c +3
-0+pc_system_parse_ovmf_flash (ptr, size); + +if (sev_enabled ()) {... because we are still checking SEV here.
Well, the two checks have slightly different purposes. The first check will probably become "if (sev || tdx)" soon, whereas the second will become "if (sev) { ... } if (tdx) { ... }".
We could remove the first. pc_system_parse_ovmf_flash() would run unconditionally then. Not needed, but should not have any bad side effects.
take care,
Gerd
Re: [PATCH 2/3] i386: factor out x86_firmware_configure()
Quoted 6 lines · Gerd Hoffmann
Quoted 5 lines
Quoted 1 line
if (sev_enabled()) {^^^Can we remove the SEV check ...
Quoted 8 lines
Quoted 5 lines
hw/i386/pc_sysfw.c void pc_system_firmware_init(PCMachineState *pcms, · line 261
Quoted 3 lines
hw/i386/pc_sysfw.c +3
-0+pc_system_parse_ovmf_flash (ptr, size); + +if (sev_enabled ()) {... because we are still checking SEV here.
Well, the two checks have slightly different purposes. The first check will probably become "if (sev || tdx)" soon,
Not soon for TDX since the hacky pflash interface to load TDVF is rejected.
Quoted 10 lines
whereas the second will
become "if (sev) { ... } if (tdx) { ... }".We could remove the first.
pc_system_parse_ovmf_flash()would run unconditionally then. Not needed, but should not have any bad side effects.take care,
Gerd
Philippe Mathieu-Daudé
Reviewed-byRe: [PATCH 2/3] i386: factor out x86_firmware_configure()
Quoted 11 lines · Xiaoyao Li
Quoted 10 lines · Gerd Hoffmann
Quoted 10 lines
Quoted 1 line
if (
sev_enabled()) {^^^
Can we remove the SEV check ...
Quoted 3 lines
+
pc_system_parse_ovmf_flash(ptr, size);
+
+ if (sev_enabled()) {
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
... because we are still checking SEV here.
Well, the two checks have slightly different purposes. The first check will probably become "if (sev || tdx)" soon,
Not soon for TDX since the hacky pflash interface to load TDVF is rejected.
You can still convince us you need a pflash for TDX, and particularly "a pflash that doesn't behave like pflash". Also, see the comment in the next patch of this series:
+ * [...] there is no need to register + * the firmware as rom to properly re-initialize on reset. + * Just go for a straight file load instead. + */
Quoted 6 lines
Quoted 6 lines
whereas the second will
become "if (sev) { ... } if (tdx) { ... }".We could remove the first.
pc_system_parse_ovmf_flash()would run unconditionally then. Not needed, but should not have any bad side effects.
OK, then:
Re: [PATCH 2/3] i386: factor out x86_firmware_configure()
Quoted 14 lines · Philippe Mathieu-Daudé
Quoted 13 lines · Xiaoyao Li
Quoted 12 lines · Gerd Hoffmann
Quoted 11 lines
Quoted 1 line
if (
sev_enabled()) {^^^
Can we remove the SEV check ...
Quoted 3 lines
+
pc_system_parse_ovmf_flash(ptr, size);
+
+ if (sev_enabled()) {... because we are still checking SEV here.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Well, the two checks have slightly different purposes. The first check will probably become "if (sev || tdx)" soon,
Not soon for TDX since the hacky pflash interface to load TDVF is rejected.
You can still convince us you need a pflash for TDX, and particularly "a pflash that doesn't behave like pflash".
I'm fine with "-bios" option to load TDVF. :)
Quoted 7 lines
Also, see the comment in
the next patch of this series:+ * [...] there is no need to register
+ * the firmware as rom to properly re-initialize on reset. + * Just go for a straight file load instead.
+ */
Yes, Gerd's this series make it easier for TDX to load TDVF via -bios.
Quoted 11 lines
Quoted 6 lines
Quoted 6 lines
whereas the second will
become "if (sev) { ... } if (tdx) { ... }".We could remove the first.
pc_system_parse_ovmf_flash()would run unconditionally then. Not needed, but should not have any bad side effects.OK, then:
Reviewed-by Philippe Mathieu-Daudé <[email protected]>
PATCH 3/3 i386: firmware parsing and sev setup for -bios loaded firmware
In reply to Gerd HoffmannDon't register firmware as rom, not needed (see comment). Add x86_firmware_configure() call for proper sev initialization.
--- hw/i386/x86.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-)
hw/i386/x86.c +19 -6
@@ -1116,12 +1116,25 @@ void x86_bios_rom_init(MachineState *ms, const char *default_firmware, } bios =g_malloc (sizeof (*bios));memory_region_init_ram (bios,NULL ,"pc.bios" , bios_size, &error_fatal); -if (!isapc_ram_fw) { -memory_region_set_readonly (bios, true); - } - ret =rom_add_file_fixed (bios_name, (uint32_t )(-bios_size),-1 ); -if (ret !=0 ) { - goto bios_error; +if (sev_enabled ()) { +/* +* The concept of a "reset" simply doesn't exist for +* confidential computing guests, we have to destroy and +* re-launch them instead. So there is no need to register +* the firmware as rom to properly re-initialize on reset. +* Just go for a straight file load instead. +*/ +void *ptr =memory_region_get_ram_ptr (bios); +load_image_size (filename, ptr, bios_size); +x86_firmware_configure (ptr, bios_size); + }else { +if (!isapc_ram_fw) { +memory_region_set_readonly (bios, true); + } + ret =rom_add_file_fixed (bios_name, (uint32_t )(-bios_size),-1 ); +if (ret !=0 ) { + goto bios_error; + } }g_free (filename);
Signature · 1 line
2.35.1
Quoted 8 lines · Gerd Hoffmann
Don't register firmware as rom, not needed (see comment). Add
x86_firmware_configure()call for proper sev initialization.Signed-off-by Gerd Hoffmann <[email protected]>Tested-by Xiaoyao Li <[email protected]>--- hw/i386/x86.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-)
I validated that I could validate the measurement of a SEV guest with -bios, and see the firmware start at least.
Quoted 41 lines
hw/i386/x86.c +19
-6@@ -1116,12 +1116,25 @@ void x86_bios_rom_init(MachineState *ms, const char *default_firmware, } bios =g_malloc (sizeof (*bios));memory_region_init_ram (bios,NULL ,"pc.bios" , bios_size, &error_fatal); -if (!isapc_ram_fw) { -memory_region_set_readonly (bios, true); - } - ret =rom_add_file_fixed (bios_name, (uint32_t )(-bios_size),-1 ); -if (ret !=0 ) { - goto bios_error; +if (sev_enabled ()) { +/* +* The concept of a "reset" simply doesn't exist for +* confidential computing guests, we have to destroy and +* re-launch them instead. So there is no need to register +* the firmware as rom to properly re-initialize on reset. +* Just go for a straight file load instead. +*/ +void *ptr =memory_region_get_ram_ptr (bios); +load_image_size (filename, ptr, bios_size); +x86_firmware_configure (ptr, bios_size); + }else { +if (!isapc_ram_fw) { +memory_region_set_readonly (bios, true); + } + ret =rom_add_file_fixed (bios_name, (uint32_t )(-bios_size),-1 ); +if (ret !=0 ) { + goto bios_error; + } }g_free (filename);Signature · 1 line
2.35.1
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Quoted 10 lines · Daniel P. Berrangé
Quoted 8 lines · Gerd Hoffmann
Don't register firmware as rom, not needed (see comment). Add
x86_firmware_configure()call for proper sev initialization.Signed-off-by Gerd Hoffmann <[email protected]>Tested-by Xiaoyao Li <[email protected]>--- hw/i386/x86.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-)
Quoted 6 lines
Reviewed-by Daniel P. Berrangé <[email protected]>I validated that I could validate the measurement of a SEV guest with -bios, and see the firmware start at least.
Tested-by Daniel P. Berrangé <[email protected]>
Nice.
With that in place we should be pretty close to get sev going with microvm. Probably needs revert of edk2 commit 06fa1f1931e9 ("OvmfPkg/Microvm: no sev"), with some luck direct kernel boot works without that though.
take care,
Gerd
Quoted 8 lines · Gerd Hoffmann
Don't register firmware as rom, not needed (see comment). Add
x86_firmware_configure()call for proper sev initialization.Signed-off-by Gerd Hoffmann <[email protected]>Tested-by Xiaoyao Li <[email protected]>--- hw/i386/x86.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-)
Quoted 1 line · Gerd Hoffmann
Looks good.
Who's merging this? Yourself?
Quoted 13 lines
Gerd Hoffmann (3):
i386: move bios load error message
i386: factor outx86_firmware_configure()
i386: firmware parsing and sev setup for -bios loaded firmwareinclude/hw/i386/x86.h | 3 +++ hw/i386/pc_sysfw.c | 36 ++++++++++++++++++++++-------------- hw/i386/x86.c | 32 ++++++++++++++++++++++++-------- 3 files changed, 49 insertions(+), 22 deletions(-)Signature · 1 line
2.35.1
Quoted 9 lines · Michael S. Tsirkin
Quoted 1 line · Gerd Hoffmann
Looks good.
Who's merging this? Yourself?
Just posted v2 with all the reviews, tests and acks added.
Will probably send a pull request in a week or so. But I don't have anything else pending right now, so I wouldn't mind if someone else picks this up ...
take care,
Gerd