vfio/igd: Fix garbled screen on IGD passthrough with legacy VBIOS
v3 cover vfio/igd: Fix garbled screen on IGD passthrough with legacy VBIOS
This series fixes the regression that on IGD passthrough with legacy BIOS boot and VBIOS, the screen is garbled during BIOS POST and GRUB (which uses standard VGA output routines), starting from QEMU 10.0. Though the kernel i915 driver still works, it reports an error about the initial GTT programmed by VBIOS is using invalid address.
i915 0000:00:02.0: [drm] *ERROR* Initial plane programming using invalid range, dma_addr=0x00000000db200000 ((null) [0x00000000baf00000-0x00000000beefffff])
With the help of AI disassembling the VBIOS image dumped from host, it is found that the VBIOS itself implements a routine like:
uint32_t get_BDSM() {
static uint32_t saved = 0;
if (saved != 0) {
return saved;
}
return read_pci_config(BDSM_REG);
}And the saved value is not cleared after initialization. Given that IGD devices don't have a real ROM BAR, the VBIOS image read by default from host is actually the VBIOS shadow RAM region, containing host-side modifications like the saved BDSM value above during POST. When the image is executed in guest, it still uses the saved host BDSM (HPA) instead of the value programmed by SeaBIOS in config space (GPA). This address mismatch leads to the garbled screen and i915 error.
The previous solution, c4c45e943e51 ("vfio/pci: Intel graphics legacy mode assignment"), adjusts GTT entry addresses to (addr - host BDSM + guest BDSM) to workaround that. But it is removed in 5aed8b0f0be2 ("vfio/igd: Remove GTT write quirk in IO BAR 4") due to inconsistent values in MMIO BAR0 and IO BAR4. Considering it's unsafe to expose HPA to guest, a ROM quirk clearing the saved value in VBIOS image is introduced to fix the issue.
During debugging, it is also found that IGD VBIOS ROM doesn't always match the actual IGD device ID, due to the fact that IGD of the same CPU family has multiple device IDs but shares the same ROM image. However, SeaBIOS checks the device ID strictly and refuses to run if IDs does not match. Currently only the default path, reading ROM from kernel patches the device ID, but the romfile path doesn't. So the ROM ID patching logic is also refactored in this patch series to also handle the romfile path.
These changes are tested on Haswell platform with legacy BIOS boot, by K S Maan. Thanks to K S Maan for continuous help on locating and testing the issue!
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3093
Changelog:
v3:
* Refactor ROM checksum calculation and patching logic as Alex's comment
* Fix boundary checks as comments in v2.
v2:
* New patch 2/7 to fix regression with EFI option ROMs
* Refine logic in ROM ID and checksum patching
* Reorder patch 4 and 5 for cleaner bisection
* Address comments from v1
Tomita Moeko (7):
hw/pci: Recalculate option ROM checksum before patching ID hw/pci: Skip EFI option ROM in pci_patch_ids() hw/pci: Introduce rom_need_patch_id flag in PCIDevice hw/pci: Promote pci_patch_ids() to public pci_rom_patch_ids() vfio/igd: Toggle rom_need_patch_id flag on IGD devices vfio/pci: Use pci_rom_patch_ids() for IGD ROM ID patching vfio/igd: Clear saved BDSM in legacy VBIOS ROM at load time
hw/pci/pci.c | 57 ++++++++++------ hw/vfio/igd-stubs.c | 5 ++ hw/vfio/igd.c | 132 ++++++++++++++++++++++++++++++++++-- hw/vfio/pci-quirks.c | 5 ++ hw/vfio/pci.c | 33 ++------- hw/vfio/pci.h | 3 + hw/vfio/trace-events | 1 + include/hw/pci/pci.h | 3 + include/hw/pci/pci_device.h | 1 + 9 files changed, 186 insertions(+), 54 deletions(-)
Signature · 1 line
2.53.0
PATCH v3 1/7 hw/pci: Recalculate option ROM checksum before patching ID
pci_patch_ids() only adjusts checksum based on the new IDs. For an option ROM with invalid checksum, the patched one will still have an invalid checksum. Always calculate the checksum and patch it if necessary to ensure the option ROM is valid.
This is intended for fixing the romfile used in IGD passthrough as multiple IGD devices share the same rom with possible non-matching device ID, and its checksum is known to be bogus [1].
A helper function pci_rom_calculate_checksum() is added and exported for reusing in IGD-specific quirk later.
[1] hw/vfio/pci.c:1090
--- hw/pci/pci.c | 32 +++++++++++++++++++++++--------- include/hw/pci/pci.h | 2 ++ 2 files changed, 25 insertions(+), 9 deletions(-)
hw/pci/pci.c +23 -9
@@ -2479,6 +2479,18 @@ static uint8_t pci_find_capability_at_offset(PCIDevice *pdev, uint8_t offset)return found; } +uint8_t pci_rom_calculate_checksum (const uint8_t *ptr,uint32_t size) +{ +uint8_t checksum =0 ; +uint32_t i; + +for (i =0 ; i < size; i++) { + checksum += ptr[i]; + } + +return checksum; +} +/* Patch the PCI vendor and device ids in a PCI rom image if necessary. This is needed for an option rom which is used for more than one device. */ static void pci_patch_ids (PCIDevice *pdev,uint8_t *ptr,uint32_t size)
@@ -2514,25 +2526,27 @@ static void pci_patch_ids(PCIDevice *pdev, uint8_t *ptr, uint32_t size)trace_pci_rom_and_pci_ids (pdev->romfile, vendor_id, device_id, rom_vendor_id, rom_device_id); - checksum = ptr[6 ]; +/* In case the checksum is bogus */ + checksum =pci_rom_calculate_checksum (ptr, size);if (vendor_id != rom_vendor_id) {/* Patch vendor id and checksum (at offset 6 for etherboot roms). */ - checksum += (uint8_t )rom_vendor_id + (uint8_t )(rom_vendor_id >>8 ); - checksum -= (uint8_t )vendor_id + (uint8_t )(vendor_id >>8 ); -trace_pci_rom_checksum_change (ptr[6 ], checksum); - ptr[6 ] = checksum; + checksum += (uint8_t )vendor_id + (uint8_t )(vendor_id >>8 ); + checksum -= (uint8_t )rom_vendor_id + (uint8_t )(rom_vendor_id >>8 );pci_set_word (ptr + pcir_offset +4 , vendor_id); }if (device_id != rom_device_id) {/* Patch device id and checksum (at offset 6 for etherboot roms). */ - checksum += (uint8_t )rom_device_id + (uint8_t )(rom_device_id >>8 ); - checksum -= (uint8_t )device_id + (uint8_t )(device_id >>8 ); -trace_pci_rom_checksum_change (ptr[6 ], checksum); - ptr[6 ] = checksum; + checksum += (uint8_t )device_id + (uint8_t )(device_id >>8 ); + checksum -= (uint8_t )rom_device_id + (uint8_t )(rom_device_id >>8 );pci_set_word (ptr + pcir_offset +6 , device_id); } + +if (checksum) { +trace_pci_rom_checksum_change (ptr[6 ], ptr[6 ] - checksum); + ptr[6 ] -= checksum; + } }/* Add an option rom for the device */
include/hw/pci/pci.h +2 -0
@@ -1103,4 +1103,6 @@ void pci_set_enabled(PCIDevice *pci_dev, bool state);void pci_set_power (PCIDevice *pci_dev,bool state);int pci_pm_init (PCIDevice *pci_dev,uint8_t offset,Error **errp); +uint8_t pci_rom_calculate_checksum (const uint8_t *ptr,uint32_t size); +#endif
Signature · 1 line
2.53.0
Re: [PATCH v3 1/7] hw/pci: Recalculate option ROM checksum before patching ID
Quoted 9 lines · Tomita Moeko
pci_patch_ids()only adjusts checksum based on the new IDs. For an option ROM with invalid checksum, the patched one will still have an invalid checksum. Always calculate the checksum and patch it if necessary to ensure the option ROM is valid.This is intended for fixing the romfile used in IGD passthrough as multiple IGD devices share the same rom with possible non-matching device ID, and its checksum is known to be bogus [1].
Quoted 7 lines
A helper function
pci_rom_calculate_checksum()is added and exported for reusing in IGD-specific quirk later.[1]
hw/vfio/pci.c:1090Reported-by K S Maan <[email protected]>Signed-off-by Tomita Moeko <[email protected]>
This seems very aggressive.
Why not recalculate in vfio then?
It has a chance to do a targeted fix.
Quoted 78 lines
--- hw/pci/pci.c | 32 +++++++++++++++++++++++--------- include/hw/pci/pci.h | 2 ++ 2 files changed, 25 insertions(+), 9 deletions(-)hw/pci/pci.c +23
-9@@ -2479,6 +2479,18 @@ static uint8_t pci_find_capability_at_offset(PCIDevice *pdev, uint8_t offset)return found; } +uint8_t pci_rom_calculate_checksum (const uint8_t *ptr,uint32_t size) +{ +uint8_t checksum =0 ; +uint32_t i; + +for (i =0 ; i < size; i++) { + checksum += ptr[i]; + } + +return checksum; +} +/* Patch the PCI vendor and device ids in a PCI rom image if necessary. This is needed for an option rom which is used for more than one device. */ static void pci_patch_ids (PCIDevice *pdev,uint8_t *ptr,uint32_t size)@@ -2514,25 +2526,27 @@ static void pci_patch_ids(PCIDevice *pdev, uint8_t *ptr, uint32_t size)trace_pci_rom_and_pci_ids (pdev->romfile, vendor_id, device_id, rom_vendor_id, rom_device_id); - checksum = ptr[6 ]; +/* In case the checksum is bogus */ + checksum =pci_rom_calculate_checksum (ptr, size);if (vendor_id != rom_vendor_id) {/* Patch vendor id and checksum (at offset 6 for etherboot roms). */ - checksum += (uint8_t )rom_vendor_id + (uint8_t )(rom_vendor_id >>8 ); - checksum -= (uint8_t )vendor_id + (uint8_t )(vendor_id >>8 ); -trace_pci_rom_checksum_change (ptr[6 ], checksum); - ptr[6 ] = checksum; + checksum += (uint8_t )vendor_id + (uint8_t )(vendor_id >>8 ); + checksum -= (uint8_t )rom_vendor_id + (uint8_t )(rom_vendor_id >>8 );pci_set_word (ptr + pcir_offset +4 , vendor_id); }if (device_id != rom_device_id) {/* Patch device id and checksum (at offset 6 for etherboot roms). */ - checksum += (uint8_t )rom_device_id + (uint8_t )(rom_device_id >>8 ); - checksum -= (uint8_t )device_id + (uint8_t )(device_id >>8 ); -trace_pci_rom_checksum_change (ptr[6 ], checksum); - ptr[6 ] = checksum; + checksum += (uint8_t )device_id + (uint8_t )(device_id >>8 ); + checksum -= (uint8_t )rom_device_id + (uint8_t )(rom_device_id >>8 );pci_set_word (ptr + pcir_offset +6 , device_id); } + +if (checksum) { +trace_pci_rom_checksum_change (ptr[6 ], ptr[6 ] - checksum); + ptr[6 ] -= checksum; + } }/* Add an option rom for the device */ include/hw/pci/pci.h +2
-0@@ -1103,4 +1103,6 @@ void pci_set_enabled(PCIDevice *pci_dev, bool state);void pci_set_power (PCIDevice *pci_dev,bool state);int pci_pm_init (PCIDevice *pci_dev,uint8_t offset,Error **errp); +uint8_t pci_rom_calculate_checksum (const uint8_t *ptr,uint32_t size); +#endif Signature · 1 line
2.53.0
Re: [PATCH v3 1/7] hw/pci: Recalculate option ROM checksum before patching ID
pci_patch_ids() is only called for ROMs flagged rom_need_patch_id (only ipxe roms
and IGD), the scope is still limited and won't potentially impact other existing functions,
it's safe doing it here.
The fix is targeted on IGD romfile, which can't easily go in vfio. vfio_pci_size_rom()
simply lets PCI core to handle the romfile and never touches or sees the romfile itself. Adding a hook in vfio brings extra code duplication and complexity.
Moeko
Quoted 102 lines · Michael S. Tsirkin
Quoted 9 lines · Tomita Moeko
pci_patch_ids()only adjusts checksum based on the new IDs. For an option ROM with invalid checksum, the patched one will still have an invalid checksum. Always calculate the checksum and patch it if necessary to ensure the option ROM is valid.This is intended for fixing the romfile used in IGD passthrough as multiple IGD devices share the same rom with possible non-matching device ID, and its checksum is known to be bogus [1].
Quoted 7 lines
A helper function
pci_rom_calculate_checksum()is added and exported for reusing in IGD-specific quirk later.[1]
hw/vfio/pci.c:1090Reported-by K S Maan <[email protected]>Signed-off-by Tomita Moeko <[email protected]>This seems very aggressive.
Why not recalculate in vfio then?
It has a chance to do a targeted fix.Quoted 78 lines
--- hw/pci/pci.c | 32 +++++++++++++++++++++++--------- include/hw/pci/pci.h | 2 ++ 2 files changed, 25 insertions(+), 9 deletions(-)hw/pci/pci.c +23
-9@@ -2479,6 +2479,18 @@ static uint8_t pci_find_capability_at_offset(PCIDevice *pdev, uint8_t offset)return found; } +uint8_t pci_rom_calculate_checksum (const uint8_t *ptr,uint32_t size) +{ +uint8_t checksum =0 ; +uint32_t i; + +for (i =0 ; i < size; i++) { + checksum += ptr[i]; + } + +return checksum; +} +/* Patch the PCI vendor and device ids in a PCI rom image if necessary. This is needed for an option rom which is used for more than one device. */ static void pci_patch_ids (PCIDevice *pdev,uint8_t *ptr,uint32_t size)@@ -2514,25 +2526,27 @@ static void pci_patch_ids(PCIDevice *pdev, uint8_t *ptr, uint32_t size)trace_pci_rom_and_pci_ids (pdev->romfile, vendor_id, device_id, rom_vendor_id, rom_device_id); - checksum = ptr[6 ]; +/* In case the checksum is bogus */ + checksum =pci_rom_calculate_checksum (ptr, size);if (vendor_id != rom_vendor_id) {/* Patch vendor id and checksum (at offset 6 for etherboot roms). */ - checksum += (uint8_t )rom_vendor_id + (uint8_t )(rom_vendor_id >>8 ); - checksum -= (uint8_t )vendor_id + (uint8_t )(vendor_id >>8 ); -trace_pci_rom_checksum_change (ptr[6 ], checksum); - ptr[6 ] = checksum; + checksum += (uint8_t )vendor_id + (uint8_t )(vendor_id >>8 ); + checksum -= (uint8_t )rom_vendor_id + (uint8_t )(rom_vendor_id >>8 );pci_set_word (ptr + pcir_offset +4 , vendor_id); }if (device_id != rom_device_id) {/* Patch device id and checksum (at offset 6 for etherboot roms). */ - checksum += (uint8_t )rom_device_id + (uint8_t )(rom_device_id >>8 ); - checksum -= (uint8_t )device_id + (uint8_t )(device_id >>8 ); -trace_pci_rom_checksum_change (ptr[6 ], checksum); - ptr[6 ] = checksum; + checksum += (uint8_t )device_id + (uint8_t )(device_id >>8 ); + checksum -= (uint8_t )rom_device_id + (uint8_t )(rom_device_id >>8 );pci_set_word (ptr + pcir_offset +6 , device_id); } + +if (checksum) { +trace_pci_rom_checksum_change (ptr[6 ], ptr[6 ] - checksum); + ptr[6 ] -= checksum; + } }/* Add an option rom for the device */ include/hw/pci/pci.h +2
-1@@ -1103,4 +1103,6 @@ void pci_set_enabled(PCIDevice *pci_dev, bool state);void pci_set_power (PCIDevice *pci_dev,bool state);int pci_pm_init (PCIDevice *pci_dev,uint8_t offset,Error **errp); +uint8_t pci_rom_calculate_checksum (const uint8_t *ptr,uint32_t size); +#endif --2.53.0
Re: [PATCH v3 1/7] hw/pci: Recalculate option ROM checksum before patching ID
Quoted 6 lines · Moeko Tomita
pci_patch_ids()is only called for ROMs flaggedrom_need_patch_id(only ipxe roms
and IGD), the scope is still limited and won't potentially impact other existing functions,
it's safe doing it here.
Quoted 6 lines
The fix is targeted on IGD romfile, which can't easily go in vfio.
vfio_pci_size_rom()
simply lets PCI core to handle the romfile and never touches or sees the romfile itself. Adding a hook in vfio brings extra code duplication and complexity.Moeko
IMHO this can all too easily break working setups. What about multi-image roms, for example? What about a padded rom file?
If someone gave me a broken rom I would normally just say "so don't do this".
If vfio wants to work around broken firmware, it's up to vfio. Maybe it makes sense, I would not know.
You can keep the patching code in pci, just invoke from vfio after pci_register_bar.
Not a big deal imho, and safer.
Signature · 1 line
MST
PATCH v3 2/7 hw/pci: Skip EFI option ROM in pci_patch_ids()
In reply to Tomita Moekopci_patch_ids() patches the checksum at the reserved 0x06 byte, but for EFI option ROMs the 32 bits at 0x04 are the EFI signature and must be 0x00000EF1. Since OVMF does not check vendor/device IDs in the PCIR header or the checksum, skip patching for EFI ROMs.
--- hw/pci/pci.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
hw/pci/pci.c +6 -1
@@ -2513,11 +2513,16 @@ static void pci_patch_ids(PCIDevice *pdev, uint8_t *ptr, uint32_t size)return ; } pcir_offset =pci_get_word (ptr +0x18 ); -if (pcir_offset +8 >= size ||memcmp (ptr + pcir_offset,"PCIR" ,4 )) { +if (pcir_offset +0x14 >= size ||memcmp (ptr + pcir_offset,"PCIR" ,4 )) {trace_pci_bad_pcir_offset (pcir_offset);return ; } +/* OVMF won't check IDs in PCIR header, skip EFI roms */ +if (pci_get_byte (ptr + pcir_offset +0x14 ) ==0x03 ) { +return ; + } + vendor_id =pci_get_word (pdev->config +PCI_VENDOR_ID ); device_id =pci_get_word (pdev->config +PCI_DEVICE_ID ); rom_vendor_id =pci_get_word (ptr + pcir_offset +4 );
Signature · 1 line
2.53.0
Re: [PATCH v3 2/7] hw/pci: Skip EFI option ROM in pci_patch_ids()
Quoted 7 lines · Tomita Moeko
pci_patch_ids()patches the checksum at the reserved0x06byte, but for EFI option ROMs the 32 bits at0x04are the EFI signature and must be0x00000EF1. Since OVMF does not check vendor/device IDs in the PCIR header or the checksum, skip patching for EFI ROMs.Reported-by K S Maan <[email protected]>Signed-off-by Tomita Moeko <[email protected]>
Quoted 28 lines
--- hw/pci/pci.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)hw/pci/pci.c +6
-1@@ -2513,11 +2513,16 @@ static void pci_patch_ids(PCIDevice *pdev, uint8_t *ptr, uint32_t size)return ; } pcir_offset =pci_get_word (ptr +0x18 ); -if (pcir_offset +8 >= size ||memcmp (ptr + pcir_offset,"PCIR" ,4 )) { +if (pcir_offset +0x14 >= size ||memcmp (ptr + pcir_offset,"PCIR" ,4 )) {trace_pci_bad_pcir_offset (pcir_offset);return ; } +/* OVMF won't check IDs in PCIR header, skip EFI roms */ +if (pci_get_byte (ptr + pcir_offset +0x14 ) ==0x03 ) { +return ; + } + vendor_id =pci_get_word (pdev->config +PCI_VENDOR_ID ); device_id =pci_get_word (pdev->config +PCI_DEVICE_ID ); rom_vendor_id =pci_get_word (ptr + pcir_offset +4 );Signature · 1 line
2.53.0
PATCH v3 3/7 hw/pci: Introduce rom_need_patch_id flag in PCIDevice
In reply to Tomita MoekoAllow external code (e.g., VFIO IGD passthrough) to request PCI ID patching for a custom romfile, replacing the existing is_default_rom flag for this purpose.
--- hw/pci/pci.c | 14 ++++++-------- include/hw/pci/pci_device.h | 1 + 2 files changed, 7 insertions(+), 8 deletions(-)
hw/pci/pci.c +6 -8
@@ -352,7 +352,7 @@ static const TypeInfo cxl_bus_info = {static void pci_update_mappings (PCIDevice *d);static void pci_irq_handler (void *opaque,int irq_num,int level); -static void pci_add_option_rom (PCIDevice *pdev,bool is_default_rom,Error **); +static void pci_add_option_rom (PCIDevice *pdev,Error **);static void pci_del_option_rom (PCIDevice *pdev);static uint16_t pci_default_sub_vendor_id =PCI_SUBVENDOR_ID_REDHAT_QUMRANET ;
@@ -2257,7 +2257,6 @@ static void pci_qdev_realize(DeviceState *qdev, Error **errp)PCIDeviceClass *pc =PCI_DEVICE_GET_CLASS (pci_dev);ObjectClass *klass =OBJECT_CLASS (pc);Error *local_err =NULL ; -bool is_default_rom;uint16_t class_id;/*
@@ -2370,13 +2369,13 @@ static void pci_qdev_realize(DeviceState *qdev, Error **errp) }/* rom loading */ - is_default_rom = false;if (pci_dev->romfile ==NULL && pc->romfile !=NULL ) { +/* using a built-in default rom */ pci_dev->romfile =g_strdup (pc->romfile); - is_default_rom = true; + pci_dev->rom_need_patch_id = true; } -pci_add_option_rom (pci_dev, is_default_rom, &local_err); +pci_add_option_rom (pci_dev, &local_err);if (local_err) {error_propagate (errp, local_err);pci_qdev_unrealize (DEVICE (pci_dev));
@@ -2555,8 +2554,7 @@ static void pci_patch_ids(PCIDevice *pdev, uint8_t *ptr, uint32_t size) }/* Add an option rom for the device */ -static void pci_add_option_rom (PCIDevice *pdev,bool is_default_rom, -Error **errp) +static void pci_add_option_rom (PCIDevice *pdev,Error **errp) {int64_t size =0 ; g_autofree char *path = NULL;
@@ -2654,7 +2652,7 @@ static void pci_add_option_rom(PCIDevice *pdev, bool is_default_rom,return ; } -if (is_default_rom) { +if (pdev->rom_need_patch_id) {/* Only the default rom images will be patched (if needed). */ pci_patch_ids (pdev, ptr, size); }
include/hw/pci/pci_device.h +1 -0
@@ -157,6 +157,7 @@ struct PCIDevice {char *romfile;uint32_t romsize;bool has_rom; +bool rom_need_patch_id;MemoryRegion rom;int32_t rom_bar;
Signature · 1 line
2.53.0
Re: [PATCH v3 3/7] hw/pci: Introduce rom_need_patch_id flag in PCIDevice
Quoted 6 lines · Tomita Moeko
Allow external code (e.g., VFIO IGD passthrough) to request PCI ID patching for a custom romfile, replacing the existing
is_default_romflag for this purpose.Reported-by K S Maan <[email protected]>Signed-off-by Tomita Moeko <[email protected]>
Quoted 76 lines
--- hw/pci/pci.c | 14 ++++++-------- include/hw/pci/pci_device.h | 1 + 2 files changed, 7 insertions(+), 8 deletions(-)hw/pci/pci.c +6
-8@@ -352,7 +352,7 @@ static const TypeInfo cxl_bus_info = {static void pci_update_mappings (PCIDevice *d);static void pci_irq_handler (void *opaque,int irq_num,int level); -static void pci_add_option_rom (PCIDevice *pdev,bool is_default_rom,Error **); +static void pci_add_option_rom (PCIDevice *pdev,Error **);static void pci_del_option_rom (PCIDevice *pdev);static uint16_t pci_default_sub_vendor_id =PCI_SUBVENDOR_ID_REDHAT_QUMRANET ;@@ -2257,7 +2257,6 @@ static void pci_qdev_realize(DeviceState *qdev, Error **errp)PCIDeviceClass *pc =PCI_DEVICE_GET_CLASS (pci_dev);ObjectClass *klass =OBJECT_CLASS (pc);Error *local_err =NULL ; -bool is_default_rom;uint16_t class_id;/* @@ -2370,13 +2369,13 @@ static void pci_qdev_realize(DeviceState *qdev, Error **errp) }/* rom loading */ - is_default_rom = false;if (pci_dev->romfile ==NULL && pc->romfile !=NULL ) { +/* using a built-in default rom */ pci_dev->romfile =g_strdup (pc->romfile); - is_default_rom = true; + pci_dev->rom_need_patch_id = true; } -pci_add_option_rom (pci_dev, is_default_rom, &local_err); +pci_add_option_rom (pci_dev, &local_err);if (local_err) {error_propagate (errp, local_err);pci_qdev_unrealize (DEVICE (pci_dev));@@ -2555,8 +2554,7 @@ static void pci_patch_ids(PCIDevice *pdev, uint8_t *ptr, uint32_t size) }/* Add an option rom for the device */ -static void pci_add_option_rom (PCIDevice *pdev,bool is_default_rom, -Error **errp) +static void pci_add_option_rom (PCIDevice *pdev,Error **errp) {int64_t size =0 ; g_autofree char *path = NULL;@@ -2654,7 +2652,7 @@ static void pci_add_option_rom(PCIDevice *pdev, bool is_default_rom,return ; } -if (is_default_rom) { +if (pdev->rom_need_patch_id) {/* Only the default rom images will be patched (if needed). */ pci_patch_ids (pdev, ptr, size); }include/hw/pci/pci_device.h +1
-0@@ -157,6 +157,7 @@ struct PCIDevice {char *romfile;uint32_t romsize;bool has_rom; +bool rom_need_patch_id;MemoryRegion rom;int32_t rom_bar;Signature · 1 line
2.53.0
PATCH v3 4/7 hw/pci: Promote pci_patch_ids() to public pci_rom_patch_ids()
In reply to Tomita MoekoRemove the static qualifier from pci_patch_ids() and rename it to pci_rom_patch_ids(), adding a declaration in include/hw/pci/pci.h so external callers can reuse it.
--- hw/pci/pci.c | 4 ++-- include/hw/pci/pci.h | 1 + 2 files changed, 3 insertions(+), 2 deletions(-)
hw/pci/pci.c +2 -2
@@ -2492,7 +2492,7 @@ uint8_t pci_rom_calculate_checksum(const uint8_t *ptr, uint32_t size)/* Patch the PCI vendor and device ids in a PCI rom image if necessary. This is needed for an option rom which is used for more than one device. */ -static void pci_patch_ids (PCIDevice *pdev,uint8_t *ptr,uint32_t size) +void pci_rom_patch_ids (PCIDevice *pdev,uint8_t *ptr,uint32_t size) {uint16_t vendor_id;uint16_t device_id;
@@ -2654,7 +2654,7 @@ static void pci_add_option_rom(PCIDevice *pdev, Error **errp)if (pdev->rom_need_patch_id) {/* Only the default rom images will be patched (if needed). */ -pci_patch_ids (pdev, ptr, size); +pci_rom_patch_ids (pdev, ptr, size); } }
include/hw/pci/pci.h +1 -0
@@ -1104,5 +1104,6 @@ void pci_set_power(PCIDevice *pci_dev, bool state);int pci_pm_init (PCIDevice *pci_dev,uint8_t offset,Error **errp);uint8_t pci_rom_calculate_checksum (const uint8_t *ptr,uint32_t size); +void pci_rom_patch_ids (PCIDevice *pdev,uint8_t *ptr,uint32_t size);#endif
Signature · 1 line
2.53.0
Re: [PATCH v3 4/7] hw/pci: Promote pci_patch_ids() to public pci_rom_patch_ids()
Quoted 6 lines · Tomita Moeko
Remove the static qualifier from
pci_patch_ids()and rename it topci_rom_patch_ids(), adding a declaration ininclude/hw/pci/pci.hso external callers can reuse it.Reported-by K S Maan <[email protected]>Signed-off-by Tomita Moeko <[email protected]>
Quoted 40 lines
--- hw/pci/pci.c | 4 ++-- include/hw/pci/pci.h | 1 + 2 files changed, 3 insertions(+), 2 deletions(-)hw/pci/pci.c +2
-2@@ -2492,7 +2492,7 @@ uint8_t pci_rom_calculate_checksum(const uint8_t *ptr, uint32_t size)/* Patch the PCI vendor and device ids in a PCI rom image if necessary. This is needed for an option rom which is used for more than one device. */ -static void pci_patch_ids (PCIDevice *pdev,uint8_t *ptr,uint32_t size) +void pci_rom_patch_ids (PCIDevice *pdev,uint8_t *ptr,uint32_t size) {uint16_t vendor_id;uint16_t device_id;@@ -2654,7 +2654,7 @@ static void pci_add_option_rom(PCIDevice *pdev, Error **errp)if (pdev->rom_need_patch_id) {/* Only the default rom images will be patched (if needed). */ -pci_patch_ids (pdev, ptr, size); +pci_rom_patch_ids (pdev, ptr, size); } }include/hw/pci/pci.h +1
-0@@ -1104,5 +1104,6 @@ void pci_set_power(PCIDevice *pci_dev, bool state);int pci_pm_init (PCIDevice *pci_dev,uint8_t offset,Error **errp);uint8_t pci_rom_calculate_checksum (const uint8_t *ptr,uint32_t size); +void pci_rom_patch_ids (PCIDevice *pdev,uint8_t *ptr,uint32_t size);#endif Signature · 1 line
2.53.0
PATCH v3 5/7 vfio/igd: Toggle rom_need_patch_id flag on IGD devices
In reply to Tomita MoekoIGD ROMs are known to have wrong device IDs and bogus checksums. Toggle the rom_need_patch_id flag when the IGD has ROM BAR or custom romfile so that pci_rom_patch_ids() will correct the vendor/device IDs and checksum.
--- hw/vfio/igd.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-)
hw/vfio/igd.c +16 -6
@@ -512,12 +512,14 @@ void vfio_probe_igd_bar0_quirk(VFIOPCIDevice *vdev, int nr)static bool vfio_pci_igd_config_quirk (VFIOPCIDevice *vdev,Error **errp) {struct vfio_region_info *opregion =NULL ; +struct vfio_region_info *rom =NULL ;PCIDevice *pdev =PCI_DEVICE (vdev); -int ret, gen; +int gen;uint64_t gms_size =0 ;uint64_t *bdsm_size;uint32_t gmch;bool legacy_mode_enabled = false; +bool has_rombar = false;Error *err =NULL ; if (!vfio_pci_is(vdev, PCI_VENDOR_ID_INTEL, PCI_ANY_ID) ||
@@ -534,6 +536,10 @@ static bool vfio_pci_igd_config_quirk(VFIOPCIDevice *vdev, Error **errp) gen =igd_gen (vdev); gmch =vfio_pci_read_config (pdev,IGD_GMCH ,4 ); + has_rombar = !vfio_device_get_region_info (&vdev->vbasedev, +VFIO_PCI_ROM_REGION_INDEX , + &rom) && rom->size; +/* * For backward compatibility, enable legacy mode when * - Device geneation is 6 to 9 (including both)
@@ -556,8 +562,6 @@ static bool vfio_pci_igd_config_quirk(VFIOPCIDevice *vdev, Error **errp)* - OpRegion * - Same LPC bridge and Host bridge VID/DID/SVID/SSID as host */ -struct vfio_region_info *rom =NULL ; - legacy_mode_enabled = true;info_report ("IGD legacy mode enabled, " "use x-igd-legacy-mode=off to disable it if unwanted." );
@@ -567,9 +571,7 @@ static bool vfio_pci_igd_config_quirk(VFIOPCIDevice *vdev, Error **errp)* there's no ROM, there's no point in setting up this quirk. * NB. We only seem to get BIOS ROMs, so UEFI VM would need CSM support. */ - ret =vfio_device_get_region_info (&vdev->vbasedev, -VFIO_PCI_ROM_REGION_INDEX , &rom); -if ((ret || !rom->size) && !pdev->romfile) { +if (!has_rombar && !pdev->romfile) {error_setg (&err,"Device has no ROM" ); goto error; }
@@ -610,6 +612,14 @@ static bool vfio_pci_igd_config_quirk(VFIOPCIDevice *vdev, Error **errp) goto error; } +/* +* IGD are known to have bad checksums and wrong device ID in its rom, +* request to patch it. +*/ +if (has_rombar || pdev->romfile) { + pdev->rom_need_patch_id = true; + } +/* * ASLS (OpRegion address) is read-only, emulated * It contains HPA, guest firmware need to reprogram it with GPA.
Signature · 1 line
2.53.0
PATCH v3 6/7 vfio/pci: Use pci_rom_patch_ids() for IGD ROM ID patching
In reply to Tomita MoekoRemove the duplicate inline logic in vfio_pci_load_rom() that patched the device ID in an IGD option ROM and replace it with a call to pci_rom_patch_ids(), conditioned on the rom_need_patch_id flag.
--- hw/vfio/pci.c | 31 +++---------------------------- 1 file changed, 3 insertions(+), 28 deletions(-)
hw/vfio/pci.c +3 -28
@@ -1029,6 +1029,7 @@ static void vfio_update_msi(VFIOPCIDevice *vdev)static void vfio_pci_load_rom (VFIOPCIDevice *vdev) { +PCIDevice *pdev =PCI_DEVICE (vdev);VFIODevice *vbasedev = &vdev->vbasedev;struct vfio_region_info *reg_info =NULL ;uint64_t size;
@@ -1084,34 +1085,8 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev) } } -/* -* Test the ROM signature against our device, if the vendor is correct -* but the device ID doesn't match, store the correct device ID and -* recompute the checksum. Intel IGD devices need this and are known -* to have bogus checksums so we can't simply adjust the checksum. -*/ -if (pci_get_word (vdev->rom) ==0xaa55 && -pci_get_word (vdev->rom +0x18 ) +8 < vdev->rom_size && - !memcmp (vdev->rom +pci_get_word (vdev->rom +0x18 ),"PCIR" ,4 )) { -uint16_t vid, did; - - vid =pci_get_word (vdev->rom +pci_get_word (vdev->rom +0x18 ) +4 ); - did =pci_get_word (vdev->rom +pci_get_word (vdev->rom +0x18 ) +6 ); - -if (vid == vdev->vendor_id && did != vdev->device_id) { -int i; -uint8_t csum, *data = vdev->rom; - -pci_set_word (vdev->rom +pci_get_word (vdev->rom +0x18 ) +6 , - vdev->device_id); - data[6 ] =0 ; - -for (csum =0 , i =0 ; i < vdev->rom_size; i++) { - csum += data[i]; - } - - data[6 ] = -csum; - } +if (pdev->rom_need_patch_id) { +pci_rom_patch_ids (pdev, vdev->rom, vdev->rom_size); } }
Signature · 1 line
2.53.0
PATCH v3 7/7 vfio/igd: Clear saved BDSM in legacy VBIOS ROM at load time
In reply to Tomita MoekoIGD does not come with a ROM BAR [1], the ROM BAR read by default from kernel is actually the host VBIOS shadow RAM region that contains host modifications on boot. With AI-assisted reverse engineering on VBIOS binaries, it is observed that VBIOS saves BDSM register value on first access and uses saved value if present.
When the image is executed in guest, since there is already a saved HPA in VBIOS, it keeps using that value instead of the GPA programmed by SeaBIOS in BDSM register in PCI config space, causing VBIOS to program GTT entries with wrong address, resulting in garbled output in BIOS POST and the error below detected by i915 driver.
i915 0000:00:02.0: [drm] *ERROR* Initial plane programming using invalid range, dma_addr=0x00000000db200000 ((null) [0x00000000baf00000-0x00000000beefffff])
The previous solution, c4c45e943e51 ("vfio/pci: Intel graphics legacy mode assignment"), adjusts GTT entry addresses to (addr - host BDSM + guest BDSM) to workaround that. But it is removed in 5aed8b0f0be2 ("vfio/igd: Remove GTT write quirk in IO BAR 4") due to inconsistent values in MMIO BAR0 and IO BAR4.
Considering it's unsafe to expose HPA to guest, a ROM quirk clearing the saved value in VBIOS image is introduced. It searches the BDSM accessor routine by matching a 19-byte signature anchored on the unique mov $0x105e,%ax instruction, then locate the offset of saved BDSM and clears it. This makes the routine fall through to the PCI config read on the first call inside the guest.
The quirk is invoked in vfio_pci_load_rom(), and is gated on Gen 6-9 IGD devices with VGA access enabled and legacy (non-UEFI) PCIR code type in the ROM header. A new trace event vfio_pci_igd_vbios_patched is also introduced.
[1] 3.5.15, 4th Generation Intel Core Processor Family Datasheet Vol. 2
https://www.intel.com/content/dam/www/public/us/en/documents/datasheets/4th-gen-core-family-desktop-vol-2-datasheet.pdf
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3093
--- hw/vfio/igd-stubs.c | 5 ++ hw/vfio/igd.c | 110 +++++++++++++++++++++++++++++++++++++++++++ hw/vfio/pci-quirks.c | 5 ++ hw/vfio/pci.c | 2 + hw/vfio/pci.h | 3 ++ hw/vfio/trace-events | 1 + 6 files changed, 126 insertions(+)
hw/vfio/igd-stubs.c +5 -0
@@ -18,3 +18,8 @@ bool vfio_probe_igd_config_quirk(VFIOPCIDevice *vdev, Error **errp) {return true; } + +void vfio_probe_igd_legacy_rom_quirk (VFIOPCIDevice *vdev) +{ +return ; +}
hw/vfio/igd.c +110 -0
113 lines not on this page · raw message
hw/vfio/pci-quirks.c +5 -0
@@ -1592,3 +1592,8 @@ bool vfio_add_virt_caps(VFIOPCIDevice *vdev, Error **errp)return true; } + +void vfio_rom_quirk_setup (VFIOPCIDevice *vdev) +{ +vfio_probe_igd_legacy_rom_quirk (vdev); +}
hw/vfio/pci.c +2 -0
@@ -1088,6 +1088,8 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)if (pdev->rom_need_patch_id) {pci_rom_patch_ids (pdev, vdev->rom, vdev->rom_size); } + +vfio_rom_quirk_setup (vdev); }/* "Raw" read of underlying config space. */
hw/vfio/pci.h +3 -0
@@ -251,10 +251,13 @@ void vfio_bar_quirk_exit(VFIOPCIDevice *vdev, int nr);void vfio_bar_quirk_finalize (VFIOPCIDevice *vdev,int nr);void vfio_setup_resetfn_quirk (VFIOPCIDevice *vdev);bool vfio_add_virt_caps (VFIOPCIDevice *vdev,Error **errp); +void vfio_rom_quirk_setup (VFIOPCIDevice *vdev);void vfio_quirk_reset (VFIOPCIDevice *vdev);VFIOQuirk *vfio_quirk_alloc (int nr_mem); +void vfio_probe_igd_bar0_quirk (VFIOPCIDevice *vdev,int nr);bool vfio_probe_igd_config_quirk (VFIOPCIDevice *vdev,Error **errp); +void vfio_probe_igd_legacy_rom_quirk (VFIOPCIDevice *vdev);extern const PropertyInfo qdev_prop_nv_gpudirect_clique;
hw/vfio/trace-events +1 -0
@@ -90,6 +90,7 @@ vfio_pci_igd_bar4_write(const char *name, uint32_t index, uint32_t data, uint32_ vfio_pci_igd_bdsm_enabled(const char *name, int size) "%s %dMB" vfio_pci_igd_host_bridge_enabled(const char *name) "%s" vfio_pci_igd_lpc_bridge_enabled(const char *name) "%s" +vfio_pci_igd_vbios_patched(const char *name) "%s" # listener.c vfio_iommu_map_notify(const char *op, uint64_t iova_start, uint64_t iova_end) "iommu %s @ 0x%"PRIx64" - 0x%"PRIx64
Signature · 1 line
2.53.0
Quoted 73 lines · Tomita Moeko
This series fixes the regression that on IGD passthrough with legacy BIOS boot and VBIOS, the screen is garbled during BIOS POST and GRUB (which uses standard VGA output routines), starting from QEMU 10.0. Though the kernel i915 driver still works, it reports an error about the initial GTT programmed by VBIOS is using invalid address.
i915 0000:00:02.0: [drm] *ERROR* Initial plane programming using invalid range, dma_addr=0x00000000db200000 ((null) [0x00000000baf00000-0x00000000beefffff])
With the help of AI disassembling the VBIOS image dumped from host, it is found that the VBIOS itself implements a routine like:
uint32_t get_BDSM() { static uint32_t saved = 0; if (saved != 0) { return saved; } return read_pci_config(BDSM_REG); }And the saved value is not cleared after initialization. Given that IGD devices don't have a real ROM BAR, the VBIOS image read by default from host is actually the VBIOS shadow RAM region, containing host-side modifications like the saved BDSM value above during POST. When the image is executed in guest, it still uses the saved host BDSM (HPA) instead of the value programmed by SeaBIOS in config space (GPA). This address mismatch leads to the garbled screen and i915 error.
The previous solution,
c4c45e943e51("vfio/pci: Intel graphics legacy mode assignment"), adjusts GTT entry addresses to (addr - host BDSM + guest BDSM) to workaround that. But it is removed in5aed8b0f0be2("vfio/igd: Remove GTT write quirk in IO BAR 4") due to inconsistent values in MMIO BAR0 and IO BAR4. Considering it's unsafe to expose HPA to guest, a ROM quirk clearing the saved value in VBIOS image is introduced to fix the issue.During debugging, it is also found that IGD VBIOS ROM doesn't always match the actual IGD device ID, due to the fact that IGD of the same CPU family has multiple device IDs but shares the same ROM image. However, SeaBIOS checks the device ID strictly and refuses to run if IDs does not match. Currently only the default path, reading ROM from kernel patches the device ID, but the romfile path doesn't. So the ROM ID patching logic is also refactored in this patch series to also handle the romfile path.
These changes are tested on Haswell platform with legacy BIOS boot, by K S Maan. Thanks to K S Maan for continuous help on locating and testing the issue!
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3093
Reported-by K S Maan <[email protected]>Changelog:
v3:
* Refactor ROM checksum calculation and patching logic as Alex's comment
* Fix boundary checks as comments in v2.v2:
* New patch 2/7 to fix regression with EFI option ROMs
* Refine logic in ROM ID and checksum patching
* Reorder patch 4 and 5 for cleaner bisection
* Address comments from v1Tomita Moeko (7):
hw/pci: Recalculate option ROM checksum before patching ID hw/pci: Skip EFI option ROM inpci_patch_ids()hw/pci: Introducerom_need_patch_idflag in PCIDevice hw/pci: Promotepci_patch_ids()to publicpci_rom_patch_ids()vfio/igd: Togglerom_need_patch_idflag on IGD devices vfio/pci: Usepci_rom_patch_ids()for IGD ROM ID patching vfio/igd: Clear saved BDSM in legacy VBIOS ROM at load time
Quoted 11 lines
hw/pci/pci.c | 57 ++++++++++------ hw/vfio/igd-stubs.c | 5 ++ hw/vfio/igd.c | 132 ++++++++++++++++++++++++++++++++++-- hw/vfio/pci-quirks.c | 5 ++ hw/vfio/pci.c | 33 ++------- hw/vfio/pci.h | 3 + hw/vfio/trace-events | 1 + include/hw/pci/pci.h | 3 + include/hw/pci/pci_device.h | 1 + 9 files changed, 186 insertions(+), 54 deletions(-)
This looks ready.
K S Maan,
Could we have your Tested-by: ?
Thanks,
C.
Quoted 84 lines · Tomita Moeko
This series fixes the regression that on IGD passthrough with legacy BIOS boot and VBIOS, the screen is garbled during BIOS POST and GRUB (which uses standard VGA output routines), starting from QEMU 10.0. Though the kernel i915 driver still works, it reports an error about the initial GTT programmed by VBIOS is using invalid address.
i915 0000:00:02.0: [drm] *ERROR* Initial plane programming using invalid range, dma_addr=0x00000000db200000 ((null) [0x00000000baf00000-0x00000000beefffff])
With the help of AI disassembling the VBIOS image dumped from host, it is found that the VBIOS itself implements a routine like:
uint32_t get_BDSM() { static uint32_t saved = 0; if (saved != 0) { return saved; } return read_pci_config(BDSM_REG); }And the saved value is not cleared after initialization. Given that IGD devices don't have a real ROM BAR, the VBIOS image read by default from host is actually the VBIOS shadow RAM region, containing host-side modifications like the saved BDSM value above during POST. When the image is executed in guest, it still uses the saved host BDSM (HPA) instead of the value programmed by SeaBIOS in config space (GPA). This address mismatch leads to the garbled screen and i915 error.
The previous solution,
c4c45e943e51("vfio/pci: Intel graphics legacy mode assignment"), adjusts GTT entry addresses to (addr - host BDSM + guest BDSM) to workaround that. But it is removed in5aed8b0f0be2("vfio/igd: Remove GTT write quirk in IO BAR 4") due to inconsistent values in MMIO BAR0 and IO BAR4. Considering it's unsafe to expose HPA to guest, a ROM quirk clearing the saved value in VBIOS image is introduced to fix the issue.During debugging, it is also found that IGD VBIOS ROM doesn't always match the actual IGD device ID, due to the fact that IGD of the same CPU family has multiple device IDs but shares the same ROM image. However, SeaBIOS checks the device ID strictly and refuses to run if IDs does not match. Currently only the default path, reading ROM from kernel patches the device ID, but the romfile path doesn't. So the ROM ID patching logic is also refactored in this patch series to also handle the romfile path.
These changes are tested on Haswell platform with legacy BIOS boot, by K S Maan. Thanks to K S Maan for continuous help on locating and testing the issue!
Resolves:https://gitlab.com/qemu-project/qemu/-/work_items/3093
Reported-by K S Maan<[email protected]>Changelog:
v3:
* Refactor ROM checksum calculation and patching logic as Alex's comment
* Fix boundary checks as comments in v2.v2:
* New patch 2/7 to fix regression with EFI option ROMs
* Refine logic in ROM ID and checksum patching
* Reorder patch 4 and 5 for cleaner bisection
* Address comments from v1Tomita Moeko (7):
hw/pci: Recalculate option ROM checksum before patching ID hw/pci: Skip EFI option ROM inpci_patch_ids()hw/pci: Introducerom_need_patch_idflag in PCIDevice hw/pci: Promotepci_patch_ids()to publicpci_rom_patch_ids()vfio/igd: Togglerom_need_patch_idflag on IGD devices vfio/pci: Usepci_rom_patch_ids()for IGD ROM ID patching vfio/igd: Clear saved BDSM in legacy VBIOS ROM at load timehw/pci/pci.c | 57 ++++++++++------ hw/vfio/igd-stubs.c | 5 ++ hw/vfio/igd.c | 132 ++++++++++++++++++++++++++++++++++-- hw/vfio/pci-quirks.c | 5 ++ hw/vfio/pci.c | 33 ++------- hw/vfio/pci.h | 3 + hw/vfio/trace-events | 1 + include/hw/pci/pci.h | 3 + include/hw/pci/pci_device.h | 1 + 9 files changed, 186 insertions(+), 54 deletions(-)
Quoted 1 line · K S Maan
Tested-by K S Maan<[email protected]>
Thanks,
Shouldn't this series include a Resolves trailer for :
https://gitlab.com/qemu-project/qemu/-/work_items/3093
What about :
https://gitlab.com/qemu-project/qemu/-/work_items/3535
Should it be closed ?
C.
Quoted 86 lines · Tomita Moeko
Quoted 84 lines
This series fixes the regression that on IGD passthrough with legacy BIOS boot and VBIOS, the screen is garbled during BIOS POST and GRUB (which uses standard VGA output routines), starting from QEMU 10.0. Though the kernel i915 driver still works, it reports an error about the initial GTT programmed by VBIOS is using invalid address.
i915 0000:00:02.0: [drm] *ERROR* Initial plane programming using invalid range, dma_addr=0x00000000db200000 ((null) [0x00000000baf00000-0x00000000beefffff])
With the help of AI disassembling the VBIOS image dumped from host, it is found that the VBIOS itself implements a routine like:
uint32_t get_BDSM() { static uint32_t saved = 0; if (saved != 0) { return saved; } return read_pci_config(BDSM_REG); }And the saved value is not cleared after initialization. Given that IGD devices don't have a real ROM BAR, the VBIOS image read by default from host is actually the VBIOS shadow RAM region, containing host-side modifications like the saved BDSM value above during POST. When the image is executed in guest, it still uses the saved host BDSM (HPA) instead of the value programmed by SeaBIOS in config space (GPA). This address mismatch leads to the garbled screen and i915 error.
The previous solution,
c4c45e943e51("vfio/pci: Intel graphics legacy mode assignment"), adjusts GTT entry addresses to (addr - host BDSM + guest BDSM) to workaround that. But it is removed in5aed8b0f0be2("vfio/igd: Remove GTT write quirk in IO BAR 4") due to inconsistent values in MMIO BAR0 and IO BAR4. Considering it's unsafe to expose HPA to guest, a ROM quirk clearing the saved value in VBIOS image is introduced to fix the issue.During debugging, it is also found that IGD VBIOS ROM doesn't always match the actual IGD device ID, due to the fact that IGD of the same CPU family has multiple device IDs but shares the same ROM image. However, SeaBIOS checks the device ID strictly and refuses to run if IDs does not match. Currently only the default path, reading ROM from kernel patches the device ID, but the romfile path doesn't. So the ROM ID patching logic is also refactored in this patch series to also handle the romfile path.
These changes are tested on Haswell platform with legacy BIOS boot, by K S Maan. Thanks to K S Maan for continuous help on locating and testing the issue!
Resolves:https://gitlab.com/qemu-project/qemu/-/work_items/3093
Reported-by K S Maan<[email protected]>Changelog:
v3:
* Refactor ROM checksum calculation and patching logic as Alex's comment
* Fix boundary checks as comments in v2.v2:
* New patch 2/7 to fix regression with EFI option ROMs
* Refine logic in ROM ID and checksum patching
* Reorder patch 4 and 5 for cleaner bisection
* Address comments from v1Tomita Moeko (7):
hw/pci: Recalculate option ROM checksum before patching ID hw/pci: Skip EFI option ROM inpci_patch_ids()hw/pci: Introducerom_need_patch_idflag in PCIDevice hw/pci: Promotepci_patch_ids()to publicpci_rom_patch_ids()vfio/igd: Togglerom_need_patch_idflag on IGD devices vfio/pci: Usepci_rom_patch_ids()for IGD ROM ID patching vfio/igd: Clear saved BDSM in legacy VBIOS ROM at load timehw/pci/pci.c | 57 ++++++++++------ hw/vfio/igd-stubs.c | 5 ++ hw/vfio/igd.c | 132 ++++++++++++++++++++++++++++++++++-- hw/vfio/pci-quirks.c | 5 ++ hw/vfio/pci.c | 33 ++------- hw/vfio/pci.h | 3 + hw/vfio/trace-events | 1 + include/hw/pci/pci.h | 3 + include/hw/pci/pci_device.h | 1 + 9 files changed, 186 insertions(+), 54 deletions(-)
Hi,
Yes these patches fix garbled screen issue for :
https://gitlab.com/qemu-project/qemu/-/work_items/3093
While following issue is unrelated and canbe close due to mismatch firmware between host and guest create other problems :
https://gitlab.com/qemu-project/qemu/-/work_items/3535
Thanks,
K S Maan
Quoted 112 lines · Cédric Le Goater
Quoted 1 line · K S Maan
Tested-by K S Maan<[email protected]>Thanks,
Shouldn't this series include a Resolves trailer for :
https://gitlab.com/qemu-project/qemu/-/work_items/3093
What about :
https://gitlab.com/qemu-project/qemu/-/work_items/3535
Should it be closed ?
C.
Quoted 89 lines · Tomita Moeko
Quoted 87 lines
This series fixes the regression that on IGD passthrough with legacy BIOS boot and VBIOS, the screen is garbled during BIOS POST and GRUB (which uses standard VGA output routines), starting from QEMU 10.0. Though the kernel i915 driver still works, it reports an error about the initial GTT programmed by VBIOS is using invalid address.
i915 0000:00:02.0: [drm] *ERROR* Initial plane programming using invalid range, dma_addr=0x00000000db200000 ((null) [0x00000000baf00000-0x00000000beefffff])
With the help of AI disassembling the VBIOS image dumped from host, it is found that the VBIOS itself implements a routine like:
uint32_tget_BDSM(){
staticuint32_tsaved = 0;
if (saved != 0) {
return saved;
}
returnread_pci_config(BDSM_REG);
}And the saved value is not cleared after initialization. Given that IGD devices don't have a real ROM BAR, the VBIOS image read by default from host is actually the VBIOS shadow RAM region, containing host-side modifications like the saved BDSM value above during POST. When the image is executed in guest, it still uses the saved host BDSM (HPA) instead of the value programmed by SeaBIOS in config space (GPA). This address mismatch leads to the garbled screen and i915 error.
The previous solution,
c4c45e943e51("vfio/pci: Intel graphics legacy mode assignment"), adjusts GTT entry addresses to (addr - host BDSM + guest BDSM) to workaround that. But it is removed in5aed8b0f0be2("vfio/igd: Remove GTT write quirk in IO BAR 4") due to inconsistent values in MMIO BAR0 and IO BAR4. Considering it's unsafe to expose HPA to guest, a ROM quirk clearing the saved value in VBIOS image is introduced to fix the issue.During debugging, it is also found that IGD VBIOS ROM doesn't always match the actual IGD device ID, due to the fact that IGD of the same CPU family has multiple device IDs but shares the same ROM image. However, SeaBIOS checks the device ID strictly and refuses to run if IDs does not match. Currently only the default path, reading ROM from kernel patches the device ID, but the romfile path doesn't. So the ROM ID patching logic is also refactored in this patch series to also handle the romfile path.
These changes are tested on Haswell platform with legacy BIOS boot, by K S Maan. Thanks to K S Maan for continuous help on locating and testing the issue!
Resolves:https://gitlab.com/qemu-project/qemu/-/work_items/3093
Reported-by K S Maan<[email protected]>Changelog:
v3:
* Refactor ROM checksum calculation and patching logic as Alex's comment
* Fix boundary checks as comments in v2.v2:
* New patch 2/7 to fix regression with EFI option ROMs
* Refine logic in ROM ID and checksum patching
* Reorder patch 4 and 5 for cleaner bisection
* Address comments from v1Tomita Moeko (7):
hw/pci: Recalculate option ROM checksum before patching ID hw/pci: Skip EFI option ROM inpci_patch_ids()hw/pci: Introducerom_need_patch_idflag in PCIDevice hw/pci: Promotepci_patch_ids()to publicpci_rom_patch_ids()vfio/igd: Togglerom_need_patch_idflag on IGD devices vfio/pci: Usepci_rom_patch_ids()for IGD ROM ID patching vfio/igd: Clear saved BDSM in legacy VBIOS ROM at load timehw/pci/pci.c | 57 ++++++++++------ hw/vfio/igd-stubs.c | 5 ++ hw/vfio/igd.c | 132
++++++++++++++++++++++++++++++++++--
hw/vfio/pci-quirks.c | 5 ++ hw/vfio/pci.c | 33 ++------- hw/vfio/pci.h | 3 + hw/vfio/trace-events | 1 + include/hw/pci/pci.h | 3 +include/hw/pci/pci_device.h| 1 +9 files changed, 186 insertions(+), 54 deletions(-)
Quoted 3 lines
While following issue is unrelated and can be close due to mismatch firmware between host and guest create other problems :
https://gitlab.com/qemu-project/qemu/-/work_items/3535
Please do.
Thanks,
C.
Quoted 4 lines
Shouldn't this series include a Resolves trailer for :
It's in the last patch. All good.
Thanks,
C.
Quoted 73 lines · On Wed, 17 Jun 2026 18:06:37 +0800 Tomita Moeko <[email protected]> wrote:
This series fixes the regression that on IGD passthrough with legacy BIOS boot and VBIOS, the screen is garbled during BIOS POST and GRUB (which uses standard VGA output routines), starting from QEMU 10.0. Though the kernel i915 driver still works, it reports an error about the initial GTT programmed by VBIOS is using invalid address.
i915 0000:00:02.0: [drm] *ERROR* Initial plane programming using invalid range, dma_addr=0x00000000db200000 ((null) [0x00000000baf00000-0x00000000beefffff])
With the help of AI disassembling the VBIOS image dumped from host, it is found that the VBIOS itself implements a routine like:
uint32_t get_BDSM() { static uint32_t saved = 0; if (saved != 0) { return saved; } return read_pci_config(BDSM_REG); }And the saved value is not cleared after initialization. Given that IGD devices don't have a real ROM BAR, the VBIOS image read by default from host is actually the VBIOS shadow RAM region, containing host-side modifications like the saved BDSM value above during POST. When the image is executed in guest, it still uses the saved host BDSM (HPA) instead of the value programmed by SeaBIOS in config space (GPA). This address mismatch leads to the garbled screen and i915 error.
The previous solution,
c4c45e943e51("vfio/pci: Intel graphics legacy mode assignment"), adjusts GTT entry addresses to (addr - host BDSM + guest BDSM) to workaround that. But it is removed in5aed8b0f0be2("vfio/igd: Remove GTT write quirk in IO BAR 4") due to inconsistent values in MMIO BAR0 and IO BAR4. Considering it's unsafe to expose HPA to guest, a ROM quirk clearing the saved value in VBIOS image is introduced to fix the issue.During debugging, it is also found that IGD VBIOS ROM doesn't always match the actual IGD device ID, due to the fact that IGD of the same CPU family has multiple device IDs but shares the same ROM image. However, SeaBIOS checks the device ID strictly and refuses to run if IDs does not match. Currently only the default path, reading ROM from kernel patches the device ID, but the romfile path doesn't. So the ROM ID patching logic is also refactored in this patch series to also handle the romfile path.
These changes are tested on Haswell platform with legacy BIOS boot, by K S Maan. Thanks to K S Maan for continuous help on locating and testing the issue!
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3093
Reported-by K S Maan <[email protected]>Changelog:
v3:
* Refactor ROM checksum calculation and patching logic as Alex's comment
* Fix boundary checks as comments in v2.v2:
* New patch 2/7 to fix regression with EFI option ROMs
* Refine logic in ROM ID and checksum patching
* Reorder patch 4 and 5 for cleaner bisection
* Address comments from v1Tomita Moeko (7):
hw/pci: Recalculate option ROM checksum before patching ID hw/pci: Skip EFI option ROM inpci_patch_ids()hw/pci: Introducerom_need_patch_idflag in PCIDevice hw/pci: Promotepci_patch_ids()to publicpci_rom_patch_ids()vfio/igd: Togglerom_need_patch_idflag on IGD devices vfio/pci: Usepci_rom_patch_ids()for IGD ROM ID patching vfio/igd: Clear saved BDSM in legacy VBIOS ROM at load time
Quoted 10 lines
hw/pci/pci.c | 57 ++++++++++------ hw/vfio/igd-stubs.c | 5 ++ hw/vfio/igd.c | 132 ++++++++++++++++++++++++++++++++++-- hw/vfio/pci-quirks.c | 5 ++ hw/vfio/pci.c | 33 ++------- hw/vfio/pci.h | 3 + hw/vfio/trace-events | 1 + include/hw/pci/pci.h | 3 + include/hw/pci/pci_device.h | 1 + 9 files changed, 186 insertions(+), 54 deletions(-)
Hello Michael,
From a VFIO perspective, this series seems ready. However it does impact the PCI subsystem. QEMU will now calculate and correct the checksum of a ROM of any PCI devices and it's a behavior change.
Could you please have a quick look and possibly ack ?
Thanks,
C.
Quoted 84 lines · Tomita Moeko
This series fixes the regression that on IGD passthrough with legacy BIOS boot and VBIOS, the screen is garbled during BIOS POST and GRUB (which uses standard VGA output routines), starting from QEMU 10.0. Though the kernel i915 driver still works, it reports an error about the initial GTT programmed by VBIOS is using invalid address.
i915 0000:00:02.0: [drm] *ERROR* Initial plane programming using invalid range, dma_addr=0x00000000db200000 ((null) [0x00000000baf00000-0x00000000beefffff])
With the help of AI disassembling the VBIOS image dumped from host, it is found that the VBIOS itself implements a routine like:
uint32_t get_BDSM() { static uint32_t saved = 0; if (saved != 0) { return saved; } return read_pci_config(BDSM_REG); }And the saved value is not cleared after initialization. Given that IGD devices don't have a real ROM BAR, the VBIOS image read by default from host is actually the VBIOS shadow RAM region, containing host-side modifications like the saved BDSM value above during POST. When the image is executed in guest, it still uses the saved host BDSM (HPA) instead of the value programmed by SeaBIOS in config space (GPA). This address mismatch leads to the garbled screen and i915 error.
The previous solution,
c4c45e943e51("vfio/pci: Intel graphics legacy mode assignment"), adjusts GTT entry addresses to (addr - host BDSM + guest BDSM) to workaround that. But it is removed in5aed8b0f0be2("vfio/igd: Remove GTT write quirk in IO BAR 4") due to inconsistent values in MMIO BAR0 and IO BAR4. Considering it's unsafe to expose HPA to guest, a ROM quirk clearing the saved value in VBIOS image is introduced to fix the issue.During debugging, it is also found that IGD VBIOS ROM doesn't always match the actual IGD device ID, due to the fact that IGD of the same CPU family has multiple device IDs but shares the same ROM image. However, SeaBIOS checks the device ID strictly and refuses to run if IDs does not match. Currently only the default path, reading ROM from kernel patches the device ID, but the romfile path doesn't. So the ROM ID patching logic is also refactored in this patch series to also handle the romfile path.
These changes are tested on Haswell platform with legacy BIOS boot, by K S Maan. Thanks to K S Maan for continuous help on locating and testing the issue!
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3093
Reported-by K S Maan <[email protected]>Changelog:
v3:
* Refactor ROM checksum calculation and patching logic as Alex's comment
* Fix boundary checks as comments in v2.v2:
* New patch 2/7 to fix regression with EFI option ROMs
* Refine logic in ROM ID and checksum patching
* Reorder patch 4 and 5 for cleaner bisection
* Address comments from v1Tomita Moeko (7):
hw/pci: Recalculate option ROM checksum before patching ID hw/pci: Skip EFI option ROM inpci_patch_ids()hw/pci: Introducerom_need_patch_idflag in PCIDevice hw/pci: Promotepci_patch_ids()to publicpci_rom_patch_ids()vfio/igd: Togglerom_need_patch_idflag on IGD devices vfio/pci: Usepci_rom_patch_ids()for IGD ROM ID patching vfio/igd: Clear saved BDSM in legacy VBIOS ROM at load timehw/pci/pci.c | 57 ++++++++++------ hw/vfio/igd-stubs.c | 5 ++ hw/vfio/igd.c | 132 ++++++++++++++++++++++++++++++++++-- hw/vfio/pci-quirks.c | 5 ++ hw/vfio/pci.c | 33 ++------- hw/vfio/pci.h | 3 + hw/vfio/trace-events | 1 + include/hw/pci/pci.h | 3 + include/hw/pci/pci_device.h | 1 + 9 files changed, 186 insertions(+), 54 deletions(-)