From patchwork Thu Oct 4 17:31:25 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: 189213 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 74AFE2C03C4 for ; Fri, 5 Oct 2012 03:32:08 +1000 (EST) Received: from localhost ([::1]:48558 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TJpHI-00049t-15 for incoming@patchwork.ozlabs.org; Thu, 04 Oct 2012 13:32:04 -0400 Received: from eggs.gnu.org ([208.118.235.92]:47274) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TJpGu-0003xU-8p for qemu-devel@nongnu.org; Thu, 04 Oct 2012 13:31:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TJpGp-0008Hy-N1 for qemu-devel@nongnu.org; Thu, 04 Oct 2012 13:31:40 -0400 Received: from mx1.redhat.com ([209.132.183.28]:59209) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TJpGp-0008Ht-ES for qemu-devel@nongnu.org; Thu, 04 Oct 2012 13:31:35 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q94HVYro022130 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 4 Oct 2012 13:31:34 -0400 Received: from freedom.redhat.com (vpn-10-122.rdu.redhat.com [10.11.10.122]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id q94HVQ8t015108; Thu, 4 Oct 2012 13:31:32 -0400 From: Lucas Meneghel Rodrigues To: qemu-devel@nongnu.org Date: Thu, 4 Oct 2012 14:31:25 -0300 Message-Id: <1349371885-15091-3-git-send-email-lmr@redhat.com> In-Reply-To: <1349371885-15091-1-git-send-email-lmr@redhat.com> References: <1349371885-15091-1-git-send-email-lmr@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.132.183.28 Cc: Lucas Meneghel Rodrigues , jan.kiszka@siemens.com, Marcelo Tosatti , Avi Kivity , pbonzini@redhat.com, Gerd Hoffmann Subject: [Qemu-devel] [PATCH 2/2] hw: Add test device for unittests execution v2 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 I still didn't figure out how to port register_ioport_read to the memory API, but hopefully we'll get there. Changes from v2: 1) Removed unused testdev member 2) Renamed device to a less generic name, pc-testdev Initial changes from initial attempt: 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=4 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. 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/pc-testdev.c | 160 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 161 insertions(+) create mode 100644 hw/pc-testdev.c diff --git a/hw/i386/Makefile.objs b/hw/i386/Makefile.objs index b34c61c..3ef3b4a 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 += 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..0234adb --- /dev/null +++ b/hw/pc-testdev.c @@ -0,0 +1,160 @@ +/* + * 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 +*/ + +#include +#include "hw.h" +#include "qdev.h" +#include "isa.h" + +struct testdev { + ISADevice dev; + MemoryRegion iomem; +}; + +#define TYPE_TESTDEV "pc-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, + "pc-testdev", 0x10000); + 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 testdev), + .class_init = testdev_class_init, +}; + +static void testdev_register_types(void) +{ + type_register_static(&testdev_info); +} + +type_init(testdev_register_types)