Add a test device which supports the kvmctl ioports, so one can run the KVM unittest suite.
Intended Usage: qemu-system-x86_64\ -device pc-testdev -serial stdio \ -device isa-debugexit,iobase=0xf4,access-size=4 \ -kernel /path/to/kvm/unittests/msr.flat Where msr.flat is one of the KVM unittests, present on a separate repo, git://git.kernel.org/pub/scm/virt/kvm/kvm-unit-tests.git Changes from v7: * Fixed rebase mistake... again. Changes from v6: * Only use sys/mman.h and mprotect on non windows platforms Changes from v5: * Fixed rebase mistake Changes from v4: * Pass iomem_buf as an opaque, as requested by Blue * Ported to new MMIO schema, as requested by Jan Changes from v3: * Ported all register_ioport functions to memory API Changes from v2: * Rename struct testdev to PCTestdev * Embed ioport_data into PCTestdev struct * Porting most register_ioport functions to memory API Changes from v1: * Removed unused testdev member * Renamed device to a less generic name, pc-testdev Initial changes from initial attempt: * Removed port 0xf1, since now kvm-unit-tests use serial * Removed exit code port 0xf4, since that can be replaced by -device isa-debugexit,iobase=0xf4,access-size=4 * Removed ram size port 0xd1, since guest memory size can be retrieved from firmware, there's a patch for kvm-unit-tests including an API to retrieve that value. CC: Paolo Bonzini <pbonz...@redhat.com> Signed-off-by: Alexander Graf <ag...@suse.de> Signed-off-by: Gerd Hoffmann <kra...@redhat.com> Signed-off-by: Marcelo Tosatti <mtosa...@redhat.com> Signed-off-by: Lucas Meneghel Rodrigues <l...@redhat.com> --- hw/i386/Makefile.objs | 1 + hw/pc-testdev.c | 161 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 162 insertions(+) create mode 100644 hw/pc-testdev.c diff --git a/hw/i386/Makefile.objs b/hw/i386/Makefile.objs index 56aaa9d..1ac5fc5 100644 --- a/hw/i386/Makefile.objs +++ b/hw/i386/Makefile.objs @@ -12,5 +12,6 @@ obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen-host-pci-device.o obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pt.o xen_pt_config_init.o xen_pt_msi.o obj-y += kvm/ obj-$(CONFIG_SPICE) += qxl.o qxl-logger.o qxl-render.o +obj-y += pc-testdev.o obj-y := $(addprefix ../,$(obj-y)) diff --git a/hw/pc-testdev.c b/hw/pc-testdev.c new file mode 100644 index 0000000..1bea8ea --- /dev/null +++ b/hw/pc-testdev.c @@ -0,0 +1,161 @@ +/* + * QEMU x86 ISA testdev + * + * Copyright (c) 2012 Avi Kivity, Gerd Hoffmann, Marcelo Tosatti + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/* + * This device is used to test KVM features specific to the x86 port, such + * as emulation, power management, interrupt routing, among others. It's meant + * to be used like: + * + * qemu-system-x86_64 -device pc-testdev -serial stdio \ + * -device isa-debugexit,iobase=0xf4,access-size=2 \ + * -kernel /home/lmr/Code/virt-test.git/kvm/unittests/msr.flat + * + * Where msr.flat is one of the KVM unittests, present on a separate repo, + * git://git.kernel.org/pub/scm/virt/kvm/kvm-unit-tests.git + */ + +#ifndef _WIN32 +#include <sys/mman.h> +#endif + +#include "hw.h" +#include "qdev.h" +#include "isa.h" +#include "exec-memory.h" + +#define IOMEM_LEN 0x10000 + +typedef struct PCTestdev { + ISADevice dev; + MemoryRegion iomem; + uint32_t ioport_data; + char iomem_buf[IOMEM_LEN]; +} PCTestdev; + +#define TYPE_TESTDEV "pc-testdev" +#define TESTDEV(obj) \ + OBJECT_CHECK(struct PCTestdev, (obj), TYPE_TESTDEV) + +static void test_device_irq_line(void *opaque, uint32_t addr, uint32_t data) +{ + struct PCTestdev *dev = opaque; + qemu_set_irq(isa_get_irq(&dev->dev, addr - 0x2000), !!data); +} + +static void test_device_ioport_write(void *opaque, uint32_t addr, uint32_t data) +{ + struct PCTestdev *dev = opaque; + dev->ioport_data = data; +} + +static uint32_t test_device_ioport_read(void *opaque, uint32_t addr) +{ + struct PCTestdev *dev = opaque; + return dev->ioport_data; +} + +static void test_device_flush_page(void *opaque, uint32_t addr, uint32_t data) +{ + hwaddr len = 4096; + void *a = cpu_physical_memory_map(data & ~0xffful, &len, 0); + +#ifndef _WIN32 + /* We might not be able to get the full page, only mprotect what we actually + have mapped */ + mprotect(a, len, PROT_NONE); + mprotect(a, len, PROT_READ|PROT_WRITE); +#endif + cpu_physical_memory_unmap(a, len, 0, 0); +} + +static uint64_t test_iomem_read(void *opaque, hwaddr addr, unsigned len) +{ + struct PCTestdev *dev = opaque; + uint64_t ret = 0; + memcpy(&ret, &dev->iomem_buf[addr], len); + ret = le64_to_cpu(ret); + + return ret; +} + +static void test_iomem_write(void *opaque, hwaddr addr, uint64_t val, + unsigned len) +{ + struct PCTestdev *dev = opaque; + val = cpu_to_le64(val); + memcpy(&dev->iomem_buf[addr], &val, len); + dev->iomem_buf[addr] = val; +} + +static const MemoryRegionOps test_iomem_ops = { + .read = test_iomem_read, + .write = test_iomem_write, + .endianness = DEVICE_LITTLE_ENDIAN, +}; + +static const MemoryRegionPortio test_dev_portio_list[] = { + { 0xe0, 1, 1, .read = test_device_ioport_read, .write = test_device_ioport_write, }, + { 0xe0, 1, 2, .read = test_device_ioport_read, .write = test_device_ioport_write, }, + { 0xe0, 1, 4, .read = test_device_ioport_read, .write = test_device_ioport_write, }, + { 0xe4, 1, 4, .write = test_device_flush_page, }, + { 0x2000, 24, 1, .write = test_device_irq_line, }, + PORTIO_END_OF_LIST(), +}; + +static int init_test_device(ISADevice *isa) +{ + struct PCTestdev *dev = DO_UPCAST(struct PCTestdev, dev, isa); + + PortioList *test_dev_port_list = g_new(PortioList, 1); + portio_list_init(test_dev_port_list, test_dev_portio_list, + NULL, "pc-testdev"); + portio_list_add(test_dev_port_list, get_system_io(), 0x0); + + memory_region_init_io(&dev->iomem, &test_iomem_ops, dev, + "pc-testdev", IOMEM_LEN); + memory_region_add_subregion(isa_address_space(&dev->dev), 0xff000000, + &dev->iomem); + return 0; +} + +static void testdev_class_init(ObjectClass *klass, void *data) +{ + ISADeviceClass *k = ISA_DEVICE_CLASS(klass); + + k->init = init_test_device; +} + +static TypeInfo testdev_info = { + .name = "pc-testdev", + .parent = TYPE_ISA_DEVICE, + .instance_size = sizeof(struct PCTestdev), + .class_init = testdev_class_init, +}; + +static void testdev_register_types(void) +{ + type_register_static(&testdev_info); +} + +type_init(testdev_register_types) -- 1.7.10.4