From patchwork Thu Oct 4 03:49:53 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lucas Meneghel Rodrigues X-Patchwork-Id: 1544571 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id A31963FC1A for ; Thu, 4 Oct 2012 03:50:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751754Ab2JDDuC (ORCPT ); Wed, 3 Oct 2012 23:50:02 -0400 Received: from mx1.redhat.com ([209.132.183.28]:45680 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751590Ab2JDDuB (ORCPT ); Wed, 3 Oct 2012 23:50:01 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q943nxap028970 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 3 Oct 2012 23:49:59 -0400 Received: from freedom.redhat.com (vpn-9-210.rdu.redhat.com [10.11.9.210]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id q943nutd029724; Wed, 3 Oct 2012 23:49:57 -0400 From: Lucas Meneghel Rodrigues To: qemu-devel@nongnu.org Cc: kvm@vger.kernel.org, Lucas Meneghel Rodrigues , Paolo Bonzini , Gerd Hoffmann , Avi Kivity , Marcelo Tosatti Subject: [PATCH] hw: Add test device for unittests execution Date: Thu, 4 Oct 2012 00:49:53 -0300 Message-Id: <1349322593-10142-1-git-send-email-lmr@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Add a test device which supports the kvmctl ioports, so one can run the KVM unittest suite [1]. Usage: qemu -device testdev 1) Removed port 0xf1, since now kvm-unit-tests use serial 2) Removed exit code port 0xf4, since that can be replaced by -device isa-debugexit,iobase=0xf4,access-size=2 3) 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. [1] Preliminary versions of this patch were posted to the mailing list about a year ago, I re-read the comments of the thread, and had guidance from Paolo about which ports to remove from the test device. CC: Paolo Bonzini Signed-off-by: Gerd Hoffmann Signed-off-by: Avi Kivity Signed-off-by: Marcelo Tosatti Signed-off-by: Lucas Meneghel Rodrigues --- hw/i386/Makefile.objs | 1 + hw/testdev.c | 131 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 hw/testdev.c diff --git a/hw/i386/Makefile.objs b/hw/i386/Makefile.objs index 8c764bb..64d2787 100644 --- a/hw/i386/Makefile.objs +++ b/hw/i386/Makefile.objs @@ -11,5 +11,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 += testdev.o obj-y := $(addprefix ../,$(obj-y)) diff --git a/hw/testdev.c b/hw/testdev.c new file mode 100644 index 0000000..44070f2 --- /dev/null +++ b/hw/testdev.c @@ -0,0 +1,131 @@ +#include +#include "hw.h" +#include "qdev.h" +#include "isa.h" + +struct testdev { + ISADevice dev; + MemoryRegion iomem; + CharDriverState *chr; +}; + +#define TYPE_TESTDEV "testdev" +#define TESTDEV(obj) \ + OBJECT_CHECK(struct testdev, (obj), TYPE_TESTDEV) + +static void test_device_irq_line(void *opaque, uint32_t addr, uint32_t data) +{ + struct testdev *dev = opaque; + + qemu_set_irq(isa_get_irq(&dev->dev, addr - 0x2000), !!data); +} + +static uint32 test_device_ioport_data; + +static void test_device_ioport_write(void *opaque, uint32_t addr, uint32_t data) +{ + test_device_ioport_data = data; +} + +static uint32_t test_device_ioport_read(void *opaque, uint32_t addr) +{ + return test_device_ioport_data; +} + +static void test_device_flush_page(void *opaque, uint32_t addr, uint32_t data) +{ + target_phys_addr_t len = 4096; + void *a = cpu_physical_memory_map(data & ~0xffful, &len, 0); + + mprotect(a, 4096, PROT_NONE); + mprotect(a, 4096, PROT_READ|PROT_WRITE); + cpu_physical_memory_unmap(a, len, 0, 0); +} + +static char *iomem_buf; + +static uint32_t test_iomem_readb(void *opaque, target_phys_addr_t addr) +{ + return iomem_buf[addr]; +} + +static uint32_t test_iomem_readw(void *opaque, target_phys_addr_t addr) +{ + return *(uint16_t*)(iomem_buf + addr); +} + +static uint32_t test_iomem_readl(void *opaque, target_phys_addr_t addr) +{ + return *(uint32_t*)(iomem_buf + addr); +} + +static void test_iomem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val) +{ + iomem_buf[addr] = val; +} + +static void test_iomem_writew(void *opaque, target_phys_addr_t addr, uint32_t val) +{ + *(uint16_t*)(iomem_buf + addr) = val; +} + +static void test_iomem_writel(void *opaque, target_phys_addr_t addr, uint32_t val) +{ + *(uint32_t*)(iomem_buf + addr) = val; +} + +static const MemoryRegionOps test_iomem_ops = { + .old_mmio = { + .read = { test_iomem_readb, test_iomem_readw, test_iomem_readl, }, + .write = { test_iomem_writeb, test_iomem_writew, test_iomem_writel, }, + }, + .endianness = DEVICE_LITTLE_ENDIAN, +}; + +static int init_test_device(ISADevice *isa) +{ + struct testdev *dev = DO_UPCAST(struct testdev, dev, isa); + + register_ioport_read(0xe0, 1, 1, test_device_ioport_read, dev); + register_ioport_write(0xe0, 1, 1, test_device_ioport_write, dev); + register_ioport_read(0xe0, 1, 2, test_device_ioport_read, dev); + register_ioport_write(0xe0, 1, 2, test_device_ioport_write, dev); + register_ioport_read(0xe0, 1, 4, test_device_ioport_read, dev); + register_ioport_write(0xe0, 1, 4, test_device_ioport_write, dev); + register_ioport_write(0xe4, 1, 4, test_device_flush_page, dev); + register_ioport_write(0x2000, 24, 1, test_device_irq_line, NULL); + iomem_buf = g_malloc0(0x10000); + memory_region_init_io(&dev->iomem, &test_iomem_ops, dev, + "testdev", 0x10000); + memory_region_add_subregion(isa_address_space(&dev->dev), 0xff000000, + &dev->iomem); + return 0; +} + +static Property testdev_isa_properties[] = { + DEFINE_PROP_CHR("chardev", struct testdev, chr), + DEFINE_PROP_END_OF_LIST(), +}; + +static void testdev_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc = DEVICE_CLASS(klass); + ISADeviceClass *k = ISA_DEVICE_CLASS(klass); + + k->init = init_test_device; + dc->props = testdev_isa_properties; +} + +static TypeInfo testdev_info = { + .name = "testdev", + .parent = TYPE_ISA_DEVICE, + .instance_size = sizeof(struct testdev), + .class_init = testdev_class_init, +}; + +static void testdev_register_types(void) +{ + type_register_static(&testdev_info); +} + +type_init(testdev_register_types)