Skip to contentSkip to conversation

Fix QEMU crashes when passing IGD to a guest VM under XEN

PATCH In review Grzegorz Uriasz 2 participants 6 messages Apr 29, 2020 – May 4, 2020

lore
Share & tools
lore
#0 lore raw

v2 cover Fix QEMU crashes when passing IGD to a guest VM under XEN

This is the v1 cover letter - the patches now include a detailed description of the changes.

Hi,

This patch series is a small subset of a bigger patch set spanning few projects aiming to isolate the GPU in QUBES OS to a dedicated security domain. I'm doing this together with 3 colleagues as part of our Bachelors thesis.

When passing an Intel Graphic Device to a HVM guest under XEN, QEMU sometimes crashes when starting the VM. It turns out that the code responsible for setting up the legacy VBIOS for the IGD contains a bug which results in a memcpy of an undefined size between the QEMU heap and the physical memory of the guest.

If the size of the memcpy is small enough qemu does not crash - this means that this bug is actually a small security issue - a hostile guest kernel might determine the memory layout of QEMU simply by looking at physical memory beyond 0xdffff - this defeats ASLR and might make exploitation easier if other issues were to be found.

The problem is the current mechanism for obtaining a copy of the ROM of the IGD.
We first allocate a buffer which holds the vbios - the size of which is obtained from sysfs.
We then try to read the rom from sysfs, if we fail then we just return without setting the size of the buffer. This would be ok if the size of the ROM reported by sysfs would be 0, but the size is always 32 pages as this corresponds to legacy memory ranges. It turns out that reading the ROM fails on every single device I've tested(spanning few generations of IGD), which means qemu never sets the size of the buffer and returns a valid pointer to code which basically does a memcpy of an undefined size.

I'm including two patches.
The first one fixes the security issue by making failing to read the ROM from sysfs fatal.
The second patch introduces a better method for obtaining the VBIOS. I've haven't yet seen a single device on which the old code was working, the new code basically creates a shadow copy directly by reading from /dev/mem - this should be fine as a quick grep of the codebase shows that this approach is already being used to handle MSI. I've tested the new code on few different laptops and it works fine and the guest VMS finally stopped complaining that the VBIOS tables are missing.

Grzegorz Uriasz (2):
Fix undefined behaviour
Improve legacy vbios handling

 hw/xen/xen_pt.c          |  8 +++++--
 hw/xen/xen_pt_graphics.c | 48 +++++++++++++++++++++++++++++++++++++---
 hw/xen/xen_pt_load_rom.c | 13 +++++------
 3 files changed, 57 insertions(+), 12 deletions(-)
Signature · 1 line
2.26.1
#1 lore raw

PATCH v2 1/2 Fix undefined behaviour

This patch fixes qemu crashes when passing through an IGD device to HVM guests under XEN. The problem is that on almost every laptop reading the IGD ROM from SYSFS will fail, the reason for it is that the IGD rom is polymorphic and it modifies itself during bootup - this results in an invalid rom image - the kernel checks whether the image is valid and when that's not the case it won't allow userspace to read it. It seems that when the code was first written(xen_pt_load_rom.c) the kernel's back then didn't check whether the rom is valid and just passed the contents to userspace - because of this qemu also tries to repair the rom later in the code. When stating the rom the kernel isn't validating the rom contents so it is returning the proper size of the rom(32 4kb pages).

This results in undefined behaviour - pci_assign_dev_load_option_rom is creating a buffer and then writes the size of the buffer to a pointer. In pci_assign_dev_load_option_rom under old kernels if the fstat would succeed then fread would also succeed - this means if the buffer is allocated the size of the buffer will be set. On newer kernels this is not the case - on all laptops I've tested(spanning a few generations of IGD) the fstat is successful and the buffer is allocated but the fread is failing - as the buffer is not deallocated the function is returning a valid pointer without setting the size of the buffer for the caller. The caller of pci_assign_dev_load_option_rom is holding the size of the buffer in an uninitialized variable and is just checking whether pci_assign_dev_load_option_rom returned a valid pointer. This later results in cpu_physical_memory_write(0xc0000, result_of_pci_assign_dev_load_option_rom, unitialized_variable) which depending on the compiler parameters, configure flags, etc... might crash qemu or might work - the xen 4.12 stable release with default configure parameters works but changing the compiler options slightly(for instance by enabling qemu debug) results in qemu crashing ¯\_(;-;)_/¯

The only situation when the original pci_assign_dev_load_option_rom works is when the IDG is not the boot gpu - this won't happen on any laptop and will be rare on desktops.

This patch deallocates the buffer and returns NULL if reading the VBIOS fails - the caller of the function then properly shuts down qemu. The next patch in the series introduces a better method for getting the vbios so qemu does not exit when pci_assign_dev_load_option_rom fails - this is the reason I've changed error_report to warn_report as whether a failure in pci_assign_dev_load_option_rom is fatal depends on the caller.

---
 hw/xen/xen_pt_load_rom.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
hw/xen/xen_pt_load_rom.c +5 -6
index a50a80837e..9f100dc159 100644
@@ -38,12 +38,12 @@ void *pci_assign_dev_load_option_rom(PCIDevice *dev,
     fp = fopen(rom_file, "r+");
     if (fp == NULL) {
         if (errno != ENOENT) {
-            error_report("pci-assign: Cannot open %s: %s", rom_file, strerror(errno));
+            warn_report("pci-assign: Cannot open %s: %s", rom_file, strerror(errno));
         }
         return NULL;
     }
     if (fstat(fileno(fp), &st) == -1) {
-        error_report("pci-assign: Cannot stat %s: %s", rom_file, strerror(errno));
+        warn_report("pci-assign: Cannot stat %s: %s", rom_file, strerror(errno));
         goto close_rom;
     }
 
@@ -59,10 +59,9 @@ void *pci_assign_dev_load_option_rom(PCIDevice *dev,
     memset(ptr, 0xff, st.st_size);
 
     if (!fread(ptr, 1, st.st_size, fp)) {
-        error_report("pci-assign: Cannot read from host %s", rom_file);
-        error_printf("Device option ROM contents are probably invalid "
-                     "(check dmesg).\nSkip option ROM probe with rombar=0, "
-                     "or load from file with romfile=\n");
+        warn_report("pci-assign: Cannot read from host %s", rom_file);
+        memory_region_unref(&dev->rom);
+        ptr = NULL;
         goto close_rom;
     }
 
Signature · 1 line
2.26.1
Reviewed-by on v2 1/2
#2 lore raw

RE: [PATCH v2 1/2] Fix undefined behaviour

Quoted 41 lines

-----Original Message-----
From: Grzegorz Uriasz <[email protected]>
Sent: 29 April 2020 04:04
To: [email protected]
Cc: Grzegorz Uriasz <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; Stefano Stabellini <[email protected]>; Anthony Perard <[email protected]>; Paul Durrant <[email protected]>; [email protected]
Subject: [PATCH v2 1/2] Fix undefined behaviour

This patch fixes qemu crashes when passing through an IGD device to HVM guests under XEN. The problem is that on almost every laptop
reading the IGD ROM from SYSFS will fail, the reason for it is that the IGD rom is polymorphic and it modifies itself
during bootup - this results in an invalid rom image - the kernel checks whether the image is valid and when that's not the case
it won't allow userspace to read it. It seems that when the code was first written(xen_pt_load_rom.c) the kernel's back then didn't check
whether the rom is valid and just passed the contents to userspace - because of this qemu also tries to repair the rom later in the code.
When stating the rom the kernel isn't validating the rom contents so it is returning the proper size of the rom(32 4kb pages).

This results in undefined behaviour - pci_assign_dev_load_option_rom is creating a buffer and then writes the size of the buffer to a pointer.
In pci_assign_dev_load_option_rom under old kernels if the fstat would succeed then fread would also succeed - this means if the buffer
is allocated the size of the buffer will be set. On newer kernels this is not the case - on all laptops I've tested(spanning a few
generations of IGD) the fstat is successful and the buffer is allocated but the fread is failing - as the buffer is not deallocated
the function is returning a valid pointer without setting the size of the buffer for the caller. The caller of pci_assign_dev_load_option_rom
is holding the size of the buffer in an uninitialized variable and is just checking whether pci_assign_dev_load_option_rom returned a valid pointer.
This later results in cpu_physical_memory_write(0xc0000, result_of_pci_assign_dev_load_option_rom, unitialized_variable) which
depending on the compiler parameters, configure flags, etc... might crash qemu or might work - the xen 4.12 stable release with default configure
parameters works but changing the compiler options slightly(for instance by enabling qemu debug) results in qemu crashing ¯\_(;-;)_/¯

Quoted 1 line

The only situation when the original pci_assign_dev_load_option_rom works is when the IDG is not the

I think you mean IGD

Quoted 11 lines

boot gpu - this won't happen on any laptop and
will be rare on desktops.

This patch deallocates the buffer and returns NULL if reading the VBIOS fails - the caller of the function then properly shuts down qemu.
The next patch in the series introduces a better method for getting the vbios so qemu does not exit when pci_assign_dev_load_option_rom fails -
this is the reason I've changed error_report to warn_report as whether a failure in pci_assign_dev_load_option_rom is fatal
depends on the caller.

Quoted 1 line

With the typo fixed...

#3 lore raw

PATCH v2 2/2 Improve legacy vbios handling

In reply to Grzegorz Uriasz

The current method of getting the vbios is broken - it just isn't working on any device I've tested - the reason for this is explained in the previous patch. The vbios is polymorphic and getting a proper unmodified copy is often not possible without reverse engineering the firmware. We don't need an unmodified copy for most purposes - an unmodified copy is only needed for initializing the bios framebuffer and providing the bios with a corrupted copy of the rom won't do any damage as the bios will just ignore the rom.

After the i915 driver takes over the vbios is only needed for reading some metadata/configuration stuff etc... I've tested that not having any kind of vbios in the guest actually works fine but on older generations of IGD there are some slight hiccups. To maximize compatibility the best approach is to just copy the results of the vbios execution directly to the guest. It turns out the vbios is always present on an hardcoded memory range in a reserved memory range from real mode - all we need to do is to memcpy it into the guest.

The following patch does 2 things:
1) When pci_assign_dev_load_option_rom fails to read the vbios from sysfs(this works only when the igd is not the
boot gpu - this is unlikely to happen) it falls back to using /dev/mem to copy the vbios directly to the guest.
Using /dev/mem should be fine as there is more xen specific pci code which also relies on /dev/mem.
2) When dealing with IGD in the more generic code we skip the allocation of the rom resource - the reason for this is to prevent
a malicious guest from modifying the vbios in the host -> this is needed as someone might try to pwn the i915 driver in the host by doing so (attach igd to guest, guest modifies vbios, the guest is terminated and the idg is reattached to the host, i915 driver in the host uses data from the modified vbios). This is also needed to not overwrite the proper shadow copy made before.

I've tested this patch and it works fine - the guest isn't complaining about the missing vbios tables and the pci config space in the guest looks fine.

---
 hw/xen/xen_pt.c          |  8 +++++--
 hw/xen/xen_pt_graphics.c | 48 +++++++++++++++++++++++++++++++++++++---
 hw/xen/xen_pt_load_rom.c |  2 +-
 3 files changed, 52 insertions(+), 6 deletions(-)
hw/xen/xen_pt.c +6 -2
index b91082cb8b..ffc3559dd4 100644
@@ -483,8 +483,12 @@ static int xen_pt_register_regions(XenPCIPassthroughState *s, uint16_t *cmd)
                    i, r->size, r->base_addr, type);
     }
 
-    /* Register expansion ROM address */
-    if (d->rom.base_addr && d->rom.size) {
+    /*
+     * Register expansion ROM address. If we are dealing with a ROM
+     * shadow copy for legacy vga devices then don't bother to map it
+     * as previous code creates a proper shadow copy
+     */
+    if (d->rom.base_addr && d->rom.size && !(is_igd_vga_passthrough(d))) {
         uint32_t bar_data = 0;
 
         /* Re-set BAR reported by OS, otherwise ROM can't be read. */
hw/xen/xen_pt_graphics.c +45 -3

73 lines not on this page · raw message

hw/xen/xen_pt_load_rom.c +1 -1
index 9f100dc159..8cd9aa84dc 100644
@@ -65,7 +65,7 @@ void *pci_assign_dev_load_option_rom(PCIDevice *dev,
         goto close_rom;
     }
 
-    pci_register_bar(dev, PCI_ROM_SLOT, 0, &dev->rom);
+    pci_register_bar(dev, PCI_ROM_SLOT, PCI_BASE_ADDRESS_SPACE_MEMORY, &dev->rom);
     dev->has_rom = true;
     *size = st.st_size;
 close_rom:
Signature · 1 line
2.26.1
#4 lore raw

RE: [PATCH v2 2/2] Improve legacy vbios handling

Quoted 29 lines

-----Original Message-----
From: Grzegorz Uriasz <[email protected]>
Sent: 29 April 2020 04:04
To: [email protected]
Cc: Grzegorz Uriasz <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; Stefano Stabellini <[email protected]>; Anthony Perard <[email protected]>; Paul Durrant <[email protected]>; [email protected]
Subject: [PATCH v2 2/2] Improve legacy vbios handling

The current method of getting the vbios is broken - it just isn't working on any device I've tested - the reason
for this is explained in the previous patch. The vbios is polymorphic and getting a proper unmodified copy is
often not possible without reverse engineering the firmware. We don't need an unmodified copy for most purposes -
an unmodified copy is only needed for initializing the bios framebuffer and providing the bios with a corrupted
copy of the rom won't do any damage as the bios will just ignore the rom.

After the i915 driver takes over the vbios is only needed for reading some metadata/configuration stuff etc...
I've tested that not having any kind of vbios in the guest actually works fine but on older generations of IGD
there are some slight hiccups. To maximize compatibility the best approach is to just copy the results of the vbios
execution directly to the guest. It turns out the vbios is always present on an hardcoded memory range in a reserved
memory range from real mode - all we need to do is to memcpy it into the guest.

Quoted 5 lines

The following patch does 2 things:
1) When pci_assign_dev_load_option_rom fails to read the vbios from sysfs(this works only when the igd is not the
boot gpu - this is unlikely to happen) it falls back to using /dev/mem to copy the vbios directly to the guest.

Why bother with sysfs if it is unlikely to work?

Quoted 26 lines

Using /dev/mem should be fine as there is more xen specific pci code which also relies on /dev/mem.
2) When dealing with IGD in the more generic code we skip the allocation of the rom resource - the reason for this is to prevent
a malicious guest from modifying the vbios in the host -> this is needed as someone might try to pwn the i915 driver in the host by doing so
(attach igd to guest, guest modifies vbios, the guest is terminated and the idg is reattached to the host, i915 driver in the host uses data from the modified vbios).
This is also needed to not overwrite the proper shadow copy made before.

I've tested this patch and it works fine - the guest isn't complaining about the missing vbios tables and the pci config
space in the guest looks fine.

---
 hw/xen/xen_pt.c          |  8 +++++--
 hw/xen/xen_pt_graphics.c | 48 +++++++++++++++++++++++++++++++++++++---
 hw/xen/xen_pt_load_rom.c |  2 +-
 3 files changed, 52 insertions(+), 6 deletions(-)
hw/xen/xen_pt.c +0 -0
index b91082cb8b..ffc3559dd4 100644
@@ -483,8 +483,12 @@ static int xen_pt_register_regions(XenPCIPassthroughState *s, uint16_t *cmd)
                    i, r->size, r->base_addr, type);

hw/xen/xen_pt.c static int xen_pt_register_regions(XenPCIPassthroughState *s, uint16_t *cmd) · line 491

Quoted 10 lines
hw/xen/xen_pt.c +6 -2
     }
 
-    /* Register expansion ROM address */
-    if (d->rom.base_addr && d->rom.size) {
+    /*
+     * Register expansion ROM address. If we are dealing with a ROM
+     * shadow copy for legacy vga devices then don't bother to map it
+     * as previous code creates a proper shadow copy
+     */
+    if (d->rom.base_addr && d->rom.size && !(is_igd_vga_passthrough(d))) {

You don't need brackets around the function call.

Paul

Quoted 99 lines
hw/xen/xen_pt.c +0 -0
         uint32_t bar_data = 0;
 
         /* Re-set BAR reported by OS, otherwise ROM can't be read. */
hw/xen/xen_pt_graphics.c +45 -3
index a3bc7e3921..fe0ef2685c 100644
@@ -129,7 +129,7 @@ int xen_pt_unregister_vga_regions(XenHostPCIDevice *dev)
     return 0;
 }
 
-static void *get_vgabios(XenPCIPassthroughState *s, int *size,
+static void *get_sysfs_vgabios(XenPCIPassthroughState *s, int *size,
                        XenHostPCIDevice *dev)
 {
     return pci_assign_dev_load_option_rom(&s->dev, size,
@@ -137,6 +137,45 @@ static void *get_vgabios(XenPCIPassthroughState *s, int *size,
                                           dev->dev, dev->func);
 }
 
+static void xen_pt_direct_vbios_copy(XenPCIPassthroughState *s, Error **errp)
+{
+    int fd = -1;
+    void *guest_bios = NULL;
+    void *host_vbios = NULL;
+    /* This is always 32 pages in the real mode reserved region */
+    int bios_size = 32 << XC_PAGE_SHIFT;
+    int vbios_addr = 0xc0000;
+
+    fd = open("/dev/mem", O_RDONLY);
+    if (fd == -1) {
+        error_setg(errp, "Can't open /dev/mem: %s", strerror(errno));
+        return;
+    }
+    host_vbios = mmap(NULL, bios_size,
+            PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, fd, vbios_addr);
+    close(fd);
+
+    if (host_vbios == MAP_FAILED) {
+        error_setg(errp, "Failed to mmap host vbios: %s", strerror(errno));
+        return;
+    }
+
+    memory_region_init_ram(&s->dev.rom, OBJECT(&s->dev),
+            "legacy_vbios.rom", bios_size, &error_abort);
+    guest_bios = memory_region_get_ram_ptr(&s->dev.rom);
+    memcpy(guest_bios, host_vbios, bios_size);
+
+    if (munmap(host_vbios, bios_size) == -1) {
+        XEN_PT_LOG(&s->dev, "Failed to unmap host vbios: %s\n", strerror(errno));
+    }
+
+    cpu_physical_memory_write(vbios_addr, guest_bios, bios_size);
+    memory_region_set_address(&s->dev.rom, vbios_addr);
+    pci_register_bar(&s->dev, PCI_ROM_SLOT, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->dev.rom);
+    s->dev.has_rom = true;
+    XEN_PT_LOG(&s->dev, "Legacy VBIOS registered\n");
+}
+
 /* Refer to Seabios. */
 struct rom_header {
     uint16_t signature;
@@ -179,9 +218,11 @@ void xen_pt_setup_vga(XenPCIPassthroughState *s, XenHostPCIDevice *dev,
         return;
     }
 
-    bios = get_vgabios(s, &bios_size, dev);
+    bios = get_sysfs_vgabios(s, &bios_size, dev);
     if (!bios) {
-        error_setg(errp, "VGA: Can't get VBIOS");
+        XEN_PT_LOG(&s->dev, "Unable to get host VBIOS from sysfs - "
+                            "falling back to a direct copy of memory ranges\n");
+        xen_pt_direct_vbios_copy(s, errp);
         return;
     }
 
@@ -223,6 +264,7 @@ void xen_pt_setup_vga(XenPCIPassthroughState *s, XenHostPCIDevice *dev,
 
     /* Currently we fixed this address as a primary for legacy BIOS. */
     cpu_physical_memory_write(0xc0000, bios, bios_size);
+    XEN_PT_LOG(&s->dev, "Legacy VBIOS registered\n");
 }
 
 uint32_t igd_read_opregion(XenPCIPassthroughState *s)
hw/xen/xen_pt_load_rom.c +1 -2
index 9f100dc159..8cd9aa84dc 100644
@@ -65,7 +65,7 @@ void *pci_assign_dev_load_option_rom(PCIDevice *dev,
         goto close_rom;
     }
 
-    pci_register_bar(dev, PCI_ROM_SLOT, 0, &dev->rom);
+    pci_register_bar(dev, PCI_ROM_SLOT, PCI_BASE_ADDRESS_SPACE_MEMORY, &dev->rom);
     dev->has_rom = true;
     *size = st.st_size;
 close_rom:
--

2.26.1

#5 lore raw

RE: [PATCH v2 2/2] Improve legacy vbios handling

In reply to Grzegorz Uriasz
Quoted 5 lines

-----Original Message-----
From: Grzegorz Uriasz <[email protected]>
Sent: 29 April 2020 04:04
To: [email protected]
Cc: Grzegorz Uriasz <[email protected]>;

[email protected]; [email protected];

Quoted 1 line

[email protected]; [email protected]; Stefano Stabellini

<[email protected]>; Anthony

Quoted 1 line

Perard <[email protected]>; Paul Durrant <[email protected]>;

[email protected]

Quoted 3 lines

Subject: [PATCH v2 2/2] Improve legacy vbios handling

The current method of getting the vbios is broken - it just isn't

working on any device I've tested -

Quoted 2 lines

the reason
for this is explained in the previous patch. The vbios is polymorphic

and getting a proper unmodified

Quoted 2 lines

copy is
often not possible without reverse engineering the firmware. We don't

need an unmodified copy for most

Quoted 2 lines

purposes -
an unmodified copy is only needed for initializing the bios framebuffer

and providing the bios with a

Quoted 2 lines

corrupted
copy of the rom won't do any damage as the bios will just ignore the

rom.

Quoted 2 lines

After the i915 driver takes over the vbios is only needed for reading

some metadata/configuration

Quoted 2 lines

stuff etc...
I've tested that not having any kind of vbios in the guest actually

works fine but on older

Quoted 2 lines

generations of IGD
there are some slight hiccups. To maximize compatibility the best

approach is to just copy the results

Quoted 2 lines

of the vbios
execution directly to the guest. It turns out the vbios is always

present on an hardcoded memory range

Quoted 2 lines

in a reserved
memory range from real mode - all we need to do is to memcpy it into the

guest.

Quoted 3 lines

The following patch does 2 things:
1) When pci_assign_dev_load_option_rom fails to read the vbios from

sysfs(this works only when the igd

Quoted 2 lines

is not the
boot gpu - this is unlikely to happen) it falls back to using /dev/mem

to copy the vbios directly to

Quoted 2 lines

the guest.
Using /dev/mem should be fine as there is more xen specific pci code

which also relies on /dev/mem.

Why bother with sysfs at all if it is unlikely to work?

Quoted 1 line

2) When dealing with IGD in the more generic code we skip the allocation

of the rom resource - the

Quoted 2 lines

reason for this is to prevent
a malicious guest from modifying the vbios in the host -> this is needed

as someone might try to pwn

Quoted 2 lines

the i915 driver in the host by doing so
(attach igd to guest, guest modifies vbios, the guest is terminated and

the idg is reattached to the

Quoted 4 lines

host, i915 driver in the host uses data from the modified vbios). This is also needed to not overwrite the proper shadow copy made before.

I've tested this patch and it works fine - the guest isn't complaining

about the missing vbios tables

hw/xen/xen_pt.c static int

Quoted 15 lines

and the pci config
space in the guest looks fine.

---
 hw/xen/xen_pt.c          |  8 +++++--
 hw/xen/xen_pt_graphics.c | 48 +++++++++++++++++++++++++++++++++++++---
 hw/xen/xen_pt_load_rom.c |  2 +-
 3 files changed, 52 insertions(+), 6 deletions(-)
hw/xen/xen_pt.c +0 -0
index b91082cb8b..ffc3559dd4 100644
@@ -483,8 +483,12 @@ static int

xen_pt_register_regions(XenPCIPassthroughState *s, uint16_t *cmd)

hw/xen/xen_pt.c static int xen_pt_register_regions(XenPCIPassthroughState *s, uint16_t *cmd)

Quoted 11 lines
hw/xen/xen_pt.c +6 -2
                    i, r->size, r->base_addr, type);
     }
 
-    /* Register expansion ROM address */
-    if (d->rom.base_addr && d->rom.size) {
+    /*
+     * Register expansion ROM address. If we are dealing with a ROM
+     * shadow copy for legacy vga devices then don't bother to map it
+     * as previous code creates a proper shadow copy
+     */
+    if (d->rom.base_addr && d->rom.size &&

!(is_igd_vga_passthrough(d))) {

You don't need the brackets around is_igd_vga_passthrough()

Paul

hw/xen/xen_pt_graphics.c int xen_pt_unregister_vga_regions(XenHostPCIDevice

Quoted 8 lines
hw/xen/xen_pt.c +0 -0
         uint32_t bar_data = 0;
 
         /* Re-set BAR reported by OS, otherwise ROM can't be read. */
hw/xen/xen_pt_graphics.c +0 -0
index a3bc7e3921..fe0ef2685c 100644
@@ -129,7 +129,7 @@ int xen_pt_unregister_vga_regions(XenHostPCIDevice

*dev)

hw/xen/xen_pt_graphics.c static void *get_vgabios(XenPCIPassthroughState *s,

Quoted 9 lines
hw/xen/xen_pt_graphics.c +1 -1
     return 0;
 }
 
-static void *get_vgabios(XenPCIPassthroughState *s, int *size,
+static void *get_sysfs_vgabios(XenPCIPassthroughState *s, int *size,
                        XenHostPCIDevice *dev)
 {
     return pci_assign_dev_load_option_rom(&s->dev, size,
@@ -137,6 +137,45 @@ static void *get_vgabios(XenPCIPassthroughState *s,

int *size,

hw/xen/xen_pt_graphics.c static void *get_vgabios(XenPCIPassthroughState *s, int *size,

Quoted 4 lines
hw/xen/xen_pt_graphics.c +1 -0
                                           dev->dev, dev->func);
 }
 
+static void xen_pt_direct_vbios_copy(XenPCIPassthroughState *s, Error

**errp)

Quoted 9 lines
hw/xen/xen_pt_graphics.c +9 -0
+{
+    int fd = -1;
+    void *guest_bios = NULL;
+    void *host_vbios = NULL;
+    /* This is always 32 pages in the real mode reserved region */
+    int bios_size = 32 << XC_PAGE_SHIFT;
+    int vbios_addr = 0xc0000;
+
+    fd = open("/dev/mem", O_RDONLY);

hw/xen/xen_pt_graphics.c static void *get_vgabios(XenPCIPassthroughState *s, int *size,

Quoted 10 lines
hw/xen/xen_pt_graphics.c +10 -0
+    if (fd == -1) {
+        error_setg(errp, "Can't open /dev/mem: %s", strerror(errno));
+        return;
+    }
+    host_vbios = mmap(NULL, bios_size,
+            PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, fd, vbios_addr);
+    close(fd);
+
+    if (host_vbios == MAP_FAILED) {
+        error_setg(errp, "Failed to mmap host vbios: %s",

strerror(errno));

hw/xen/xen_pt_graphics.c static void *get_vgabios(XenPCIPassthroughState *s, int *size,

Quoted 10 lines
hw/xen/xen_pt_graphics.c +10 -0
+        return;
+    }
+
+    memory_region_init_ram(&s->dev.rom, OBJECT(&s->dev),
+            "legacy_vbios.rom", bios_size, &error_abort);
+    guest_bios = memory_region_get_ram_ptr(&s->dev.rom);
+    memcpy(guest_bios, host_vbios, bios_size);
+
+    if (munmap(host_vbios, bios_size) == -1) {
+        XEN_PT_LOG(&s->dev, "Failed to unmap host vbios: %s\n",

strerror(errno));

hw/xen/xen_pt_graphics.c static void *get_vgabios(XenPCIPassthroughState *s, int *size,

Quoted 5 lines
hw/xen/xen_pt_graphics.c +5 -0
+    }
+
+    cpu_physical_memory_write(vbios_addr, guest_bios, bios_size);
+    memory_region_set_address(&s->dev.rom, vbios_addr);
+    pci_register_bar(&s->dev, PCI_ROM_SLOT,

PCI_BASE_ADDRESS_SPACE_MEMORY, &s->dev.rom);

hw/xen/xen_pt_graphics.c void xen_pt_setup_vga(XenPCIPassthroughState *s,

Quoted 8 lines
hw/xen/xen_pt_graphics.c +4 -0
+    s->dev.has_rom = true;
+    XEN_PT_LOG(&s->dev, "Legacy VBIOS registered\n");
+}
+
 /* Refer to Seabios. */
 struct rom_header {
     uint16_t signature;
@@ -179,9 +218,11 @@ void xen_pt_setup_vga(XenPCIPassthroughState *s,

XenHostPCIDevice *dev,

hw/xen/xen_pt_graphics.c void xen_pt_setup_vga(XenPCIPassthroughState *s, XenHostPCIDevice *dev,

Quoted 9 lines
hw/xen/xen_pt_graphics.c +3 -2
         return;
     }
 
-    bios = get_vgabios(s, &bios_size, dev);
+    bios = get_sysfs_vgabios(s, &bios_size, dev);
     if (!bios) {
-        error_setg(errp, "VGA: Can't get VBIOS");
+        XEN_PT_LOG(&s->dev, "Unable to get host VBIOS from sysfs - "
+                            "falling back to a direct copy of memory

ranges\n");

hw/xen/xen_pt_graphics.c void xen_pt_setup_vga(XenPCIPassthroughState *s,

Quoted 5 lines
hw/xen/xen_pt_graphics.c +1 -0
+        xen_pt_direct_vbios_copy(s, errp);
         return;
     }
 
@@ -223,6 +264,7 @@ void xen_pt_setup_vga(XenPCIPassthroughState *s,

XenHostPCIDevice *dev,

Quoted 12 lines
hw/xen/xen_pt_graphics.c +1 -0
     /* Currently we fixed this address as a primary for legacy BIOS. */
     cpu_physical_memory_write(0xc0000, bios, bios_size);
+    XEN_PT_LOG(&s->dev, "Legacy VBIOS registered\n");
 }
 
 uint32_t igd_read_opregion(XenPCIPassthroughState *s)
hw/xen/xen_pt_load_rom.c +0 -0
index 9f100dc159..8cd9aa84dc 100644

hw/xen/xen_pt_load_rom.c void *pci_assign_dev_load_option_rom(PCIDevice *dev, · line 68

Quoted 5 lines
hw/xen/xen_pt_load_rom.c +1 -1
@@ -65,7 +65,7 @@ void *pci_assign_dev_load_option_rom(PCIDevice *dev,
         goto close_rom;
     }
 
-    pci_register_bar(dev, PCI_ROM_SLOT, 0, &dev->rom);
+    pci_register_bar(dev, PCI_ROM_SLOT, PCI_BASE_ADDRESS_SPACE_MEMORY,

&dev->rom);

Quoted 5 lines
hw/xen/xen_pt_load_rom.c +0 -1
     dev->has_rom = true;
     *size = st.st_size;
 close_rom:
--

2.26.1