Skip to content

Qemu: implement readonly memory

Xiao Guangrong kvm qemu-devel 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v2 Latest5 patches 11 repliesIn review

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/5KVM: define KVM_CAP_READONLY_MEM unconditionallyarch/x86/kvm/x86.c include/linux/kvm.h virt/kvm/kvm_main.c+3 -31 reply
2/5Qemu: update header fileslinux-headers/asm-powerpc/kvm.h linux-headers/asm-powerpc/kvm_para.h linux-headers/asm-x86/kvm.h +1 more+112 -121 reply
3/5Qemu: do not mark bios readonlyhw/pc_sysfw.c+0 -29 replies
4/5Qemu: implement readonly memorykvm-all.c+17 -70 replies
5/5Qemu: mark pci rom readonlyhw/pci.c+1 -00 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v2 yet.

Read the v2 thread

Cover letter [PATCH v2 0/5] Qemu: implement readonly memory

This patch set make the readonly memory in qemu really readonly by using readonly memory slots feature in kvm to make qemu-kvm safer. Memory regions with readonly property would be plug into kvm as readonly memory slots.

Below module can test this feature:

static int rom_tester_probe(struct pci_dev *dev, const struct pci_device_id *id)

{
	struct resource *res = &dev->resource[PCI_ROM_RESOURCE];
	char buf[6];
	size_t rom_size;
	char * __iomem map;
	int i;

	if (res->flags & (IORESOURCE_ROM_SHADOW | IORESOURCE_ROM_COPY |
	      IORESOURCE_ROM_BIOS_COPY)) {
		dev_printk(KERN_INFO, &dev->dev, "skip ROM COPY.\n");
		return 0;
	}

	if (res->flags &
			(IORESOURCE_ROM_COPY | IORESOURCE_ROM_BIOS_COPY))
	dev_printk(KERN_INFO, &dev->dev, "rom tester\n");

	if (pci_enable_rom(dev)) {
		dev_printk(KERN_INFO, &dev->dev, "do not found Rom\n");
		goto exit;
	}

	map = pci_map_rom(dev, &rom_size);
	if (!map) {
		dev_printk(KERN_INFO, &dev->dev, "map rom fail.\n");
		goto disable_exit;
	}

	dev_printk(KERN_INFO, &dev->dev, "Rom map: %p [size: %lx], phsyc:%llx.\n",
		   map, rom_size, pci_resource_start(dev, PCI_ROM_RESOURCE));

	if (rom_size < 6) {
		printk("map size < 6.\n");
		goto unmap_exit;
	}

	printk("The first 6 bytes:\n");
	for (i = 0; i < 6; i++) {
		buf[i] = map[i];
		printk("%x ", buf[i]);
	}
	printk("\n\n");

	memcpy(map, "KVMKVM", 6);
	if (!memcmp(map, "KVMKVM", 6)) {
		printk("Rom Test: fail.\n");
		goto unmap_exit;
	}

	for (i = 0; i < 6; i++)
		if (buf[i] != ((char *)map)[i]) {
			printk("The %d byte is changed: %x -> %x.\n",
			       i, buf[i], map[i]);
			printk("Rom Test: fail.\n");
			goto unmap_exit;
		}

	printk("Rom Test: Okay.\n");

unmap_exit:

	pci_unmap_rom(dev, map);
disable_exit:
	pci_disable_rom(dev);
exit:
	return 0;
}
static DEFINE_PCI_DEVICE_TABLE(rom_tester_tbl) = {
	{ PCI_DEVICE(PCI_ANY_ID, PCI_ANY_ID)},

	{0,}						/* 0 terminated list. */
};

MODULE_DEVICE_TABLE(pci, rom_tester_tbl);

static struct pci_driver rom_tester = {
	.name		= "pci-rom-tester",
	.id_table	= rom_tester_tbl,
	.probe		= rom_tester_probe,
};

static int __init pci_rom_test_init(void)

{
	int rc;

	rc = pci_register_driver(&rom_tester);
	if (rc)
		return rc;

	return 0;
}

static void __exit pci_rom_test_exit(void)

{
	pci_unregister_driver(&rom_tester);
}

module_init(pci_rom_test_init);
module_exit(pci_rom_test_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Xiao Guangrong <[email protected]>");

we test it with the rom of Intel 82540EM Gigabit Ethernet Controller.
0. start qemu:

    qemu-system-x86_64 -enable-kvm -m 1G -smp 2 -hda fedora16.qcow2 \
        -nic nic,model=e1000,vlan=1,macadrr=52:00:12:34:56 \
        -net user,vlan=1
1. unbind the device:
    echo "0000:00:03.0" > /sys/bus/pci/devices/0000\:00\:03.0/driver/unbind

2. install the test kernel module, here we name it write_rom:
modprobe write_rom
3. print dmesg to verify the result is ok or fail:
dmesg
4. remove the test kernel module.
rmmod write_rom
5. rebind the device to its driver, test if the nic still works:
echo "0000:00:03.0" > /sys/bus/pci/drivers/e1000/bind open firefox and try some web page.

when I use kvm without readonly memory slot, in step 2 it reports:
Rom Test: fail. this means we can write to the memory region of a rom, which is quite not safe for the guest and host.

when I use kvm with readonly memory slot, in step 2 it reports:
Rom Test: Okay.

this means write operation is not successful, and return back to qemu from kvm. thus we can make the rom real rom.