Skip to contentSkip to conversation

QEMU: virtio should register power of two sized regions

PATCH Applied Marcelo Tosatti 2 participants 5 messages Mar 17, 2008 – Mar 18, 2008 KVM

lore
Share & tools
lore
#0 lore raw

cover QEMU: virtio should register power of two sized regions

Otherwise the guest will miscalculate the region size.

Signature · 4 lines
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
#1 lore raw

PATCH 1/3 QEMU/KVM: add fls()

Find Last Set, in accordance with glibc's ffs.

qemu/cutils.c +11 -0
Index: kvm-userspace.hotplug3/qemu/cutils.c
===================================================================
@@ -135,3 +135,14 @@ char *urldecode(const char *ptr)
     return ret;
 }
 
+int fls(int i)
+{
+    int bit;
+
+    for (bit=31; bit >= 0; bit--)
+        if (i & (1 << bit))
+            return bit+1;
+
+    return 0;
+}
+
qemu/qemu-common.h +1 -0
Index: kvm-userspace.hotplug3/qemu/qemu-common.h
===================================================================
@@ -87,6 +87,7 @@ int stristart(const char *str, const cha
 time_t mktimegm(struct tm *tm);
 int hex2bin(char ch);
 char *urldecode(const char *ptr);
+int fls(int i);
 
 /* Error handling.  */
 
Signature · 4 lines
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
#2 lore raw

PATCH 2/3 QEMU/KVM: virtio register pow2 memory regions

In reply to Marcelo Tosatti

Otherwise the PCI size for such regions will be calculated erroneously.

qemu/hw/virtio.c +6 -1
Index: kvm-userspace.hotplug3/qemu/hw/virtio.c
===================================================================
@@ -404,6 +404,7 @@ VirtIODevice *virtio_init_pci(PCIBus *bu
     VirtIODevice *vdev;
     PCIDevice *pci_dev;
     uint8_t *config;
+    uint32_t size;
 
     pci_dev = pci_register_device(bus, name, struct_size,
 				  -1, NULL, NULL);
@@ -441,7 +442,11 @@ VirtIODevice *virtio_init_pci(PCIBus *bu
     else
 	vdev->config = NULL;
 
-    pci_register_io_region(pci_dev, 0, 20 + config_size, PCI_ADDRESS_SPACE_IO,
+    size = 20 + config_size;
+    if (size & (size-1))
+        size = 1 << fls(size);
+
+    pci_register_io_region(pci_dev, 0, size, PCI_ADDRESS_SPACE_IO,
 			   virtio_map);
     qemu_register_reset(virtio_reset, vdev);
 
Signature · 4 lines
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
#3 lore raw

PATCH 3/3 QEMU/KVM: bail out if PCI region is not power of two

In reply to Marcelo Tosatti
qemu/hw/pci.c +5 -0
Index: kvm-userspace.hotplug3/qemu/hw/pci.c
===================================================================
@@ -236,6 +236,11 @@ void pci_register_io_region(PCIDevice *p
 
     if ((unsigned int)region_num >= PCI_NUM_REGIONS)
         return;
+
+    if (size & (size-1))
+        term_printf("WARNING: PCI region size must be pow2 "
+                    "type=0x%x, size=0x%x\n", type, size);
+
     r = &pci_dev->io_regions[region_num];
     r->addr = -1;
     r->size = size;
Signature · 4 lines
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Applied
#4 lore raw
In reply to Marcelo Tosatti
Quoted 3 lines · Marcelo Tosatti

Otherwise the guest will miscalculate the region size.

Applied all, thanks.

Signature · 7 lines
Do not meddle in the internals of kernels, for they are subtle and quick to panic.


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/