Skip to contentSkip to conversation

hw: Add test device for unittests execution

PATCH In review Lucas Meneghel Rodrigues 7 participants 15 messages Aug 26, 2011 – Aug 30, 2011 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw: Add test device for unittests execution

Add a test device which supports the kvmctl ioports, for running the KVM test suite. This is a straight port from the latest version of the test device present on qemu-kvm, using the APIs currently in use by qemu.

With this we aim for daily execution of the KVM unittests to capture any problems with the KVM interface.

Usage:

qemu
-chardev file,path=/log/file/some/where,id=testlog -device testdev,chardev=testlog

---
 Makefile.target |    1 +
 hw/testdev.c    |  140 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 141 insertions(+), 0 deletions(-)
 create mode 100644 hw/testdev.c
Makefile.target +1 -0
index e280bf6..e095dd5 100644
@@ -232,6 +232,7 @@ obj-i386-y += debugcon.o multiboot.o
 obj-i386-y += pc_piix.o
 obj-i386-$(CONFIG_KVM) += kvmclock.o
 obj-i386-$(CONFIG_SPICE) += qxl.o qxl-logger.o qxl-render.o
+obj-i386-y += testdev.o
 
 # shared objects
 obj-ppc-y = ppc.o
hw/testdev.c new file +140 -0

140 lines not on this page · raw message

Signature · 1 line
1.7.6
#1 lore raw
Quoted 8 lines · Lucas Meneghel Rodrigues

Add a test device which supports the kvmctl ioports, for running the KVM test suite. This is a straight port from the latest version of the test device present on qemu-kvm, using the APIs currently in use by qemu.

With this we aim for daily execution of the KVM unittests to capture any problems with the KVM interface.

I know this has come up before so apologies if this is redundant.

Quoted 44 lines

Usage:

qemu
-chardev file,path=/log/file/some/where,id=testlog -device testdev,chardev=testlog

---
  Makefile.target |    1 +
  hw/testdev.c    |  140 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2 files changed, 141 insertions(+), 0 deletions(-)

create mode 100644 hw/testdev.c

Makefile.target +1 -0
index e280bf6..e095dd5 100644
@@ -232,6 +232,7 @@ obj-i386-y += debugcon.o multiboot.o
  obj-i386-y += pc_piix.o
  obj-i386-$(CONFIG_KVM) += kvmclock.o
  obj-i386-$(CONFIG_SPICE) += qxl.o qxl-logger.o qxl-render.o
+obj-i386-y += testdev.o
 
  # shared objects
  obj-ppc-y = ppc.o
hw/testdev.c new file +9 -0
new file mode 100644
index 0000000..e38c20e
@@ -0,0 +1,140 @@
+#include<sys/mman.h>
+#include "hw.h"
+#include "qdev.h"
+#include "isa.h"
+
+struct testdev {
+    ISADevice dev;
+    CharDriverState *chr;
+};

hw/testdev.c line 19

Quoted 10 lines
hw/testdev.c new file +10 -0
+
+static void test_device_serial_write(void *opaque, uint32_t addr, uint32_t data)
+{
+    struct testdev *dev = opaque;
+    uint8_t buf[1] = { data };
+
+    if (dev->chr) {
+        qemu_chr_fe_write(dev->chr, buf, 1);
+    }
+}

I think I posted patches at some point for kvm unittests to use a standard UART. Was there any reason not to do use a UART?

hw/testdev.c line 24

Quoted 4 lines
hw/testdev.c new file +4 -0
+static void test_device_exit(void *opaque, uint32_t addr, uint32_t data)
+{
+    exit(data);
+}

Port 501 can do this.

hw/testdev.c line 29

Quoted 5 lines
hw/testdev.c new file +5 -0
+
+static uint32_t test_device_memsize_read(void *opaque, uint32_t addr)
+{
+    return ram_size;
+}

This can be read through fw_cfg, any reason to do PIO for this?

Quoted 6 lines
hw/testdev.c new file +6 -0
+static void test_device_irq_line(void *opaque, uint32_t addr, uint32_t data)
+{
+    qemu_set_irq(isa_get_irq(addr - 0x2000), !!data);
+}
+
+static uint32 test_device_ioport_data;

hw/testdev.c line 46

Quoted 10 lines
hw/testdev.c new file +10 -0
+
+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;
+}

Would be nicer to do this via an opaque.

hw/testdev.c line 55

Quoted 8 lines
hw/testdev.c new file +8 -0
+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);

This hard codes page size (get it via sysconf). I think mprotect probably isn't available on windows either.

Quoted 68 lines
hw/testdev.c new file +68 -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 CPUReadMemoryFunc * const test_iomem_read[3] = {
+    test_iomem_readb,
+    test_iomem_readw,
+    test_iomem_readl,
+};
+
+static CPUWriteMemoryFunc * const test_iomem_write[3] = {
+    test_iomem_writeb,
+    test_iomem_writew,
+    test_iomem_writel,
+};
+
+static int init_test_device(ISADevice *isa)
+{
+    struct testdev *dev = DO_UPCAST(struct testdev, dev, isa);
+    int iomem;
+
+    register_ioport_write(0xf1, 1, 1, test_device_serial_write, dev);
+    register_ioport_write(0xf4, 1, 4, test_device_exit, dev);
+    register_ioport_read(0xd1, 1, 4, test_device_memsize_read, dev);
+    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);
+    iomem = cpu_register_io_memory(test_iomem_read, test_iomem_write, NULL,
+                                   DEVICE_NATIVE_ENDIAN);
+    cpu_register_physical_memory(0xff000000, 0x10000, iomem);
+    return 0;
+}

hw/testdev.c line 133

Quoted 10 lines
hw/testdev.c new file +10 -0
+
+static ISADeviceInfo testdev_info = {
+    .qdev.name  = "testdev",
+    .qdev.size  = sizeof(struct testdev),
+    .init       = init_test_device,
+    .qdev.props = (Property[]) {
+        DEFINE_PROP_CHR("chardev", struct testdev, chr),
+        DEFINE_PROP_END_OF_LIST(),
+    },
+};

Should this use MemoryRegion?

Regards,

Anthony Liguori

Quoted 7 lines
hw/testdev.c new file +7 -0
+
+static void testdev_register_devices(void)
+{
+    isa_qdev_register(&testdev_info);
+}
+
+device_init(testdev_register_devices)
#2 lore raw
Quoted 188 lines · Anthony Liguori
Quoted 8 lines · Lucas Meneghel Rodrigues

Add a test device which supports the kvmctl ioports, for running the KVM test suite. This is a straight port from the latest version of the test device present on qemu-kvm, using the APIs currently in use by qemu.

With this we aim for daily execution of the KVM unittests to capture any problems with the KVM interface.

I know this has come up before so apologies if this is redundant.

Quoted 44 lines

Usage:

qemu
-chardev file,path=/log/file/some/where,id=testlog -device testdev,chardev=testlog

---
 Makefile.target |    1 +
 hw/testdev.c    |  140 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 141 insertions(+), 0 deletions(-)
 create mode 100644 hw/testdev.c
Makefile.target +1 -0
index e280bf6..e095dd5 100644
@@ -232,6 +232,7 @@ obj-i386-y += debugcon.o multiboot.o
 obj-i386-y += pc_piix.o
 obj-i386-$(CONFIG_KVM) += kvmclock.o
 obj-i386-$(CONFIG_SPICE) += qxl.o qxl-logger.o qxl-render.o
+obj-i386-y += testdev.o
 
 # shared objects
 obj-ppc-y = ppc.o
hw/testdev.c new file +9 -0
new file mode 100644
index 0000000..e38c20e
@@ -0,0 +1,140 @@
+#include<sys/mman.h>
+#include "hw.h"
+#include "qdev.h"
+#include "isa.h"
+
+struct testdev {
+    ISADevice dev;
+    CharDriverState *chr;
+};

hw/testdev.c line 19

Quoted 10 lines
hw/testdev.c new file +10 -0
+
+static void test_device_serial_write(void *opaque, uint32_t addr, uint32_t data)
+{
+    struct testdev *dev = opaque;
+    uint8_t buf[1] = { data };
+
+    if (dev->chr) {
+        qemu_chr_fe_write(dev->chr, buf, 1);
+    }
+}

I think I posted patches at some point for kvm unittests to use a standard UART. Was there any reason not to do use a UART?

hw/testdev.c line 24

Quoted 4 lines
hw/testdev.c new file +4 -0
+static void test_device_exit(void *opaque, uint32_t addr, uint32_t data)
+{
+    exit(data);
+}

Port 501 can do this.

hw/testdev.c line 29

Quoted 5 lines
hw/testdev.c new file +5 -0
+
+static uint32_t test_device_memsize_read(void *opaque, uint32_t addr)
+{
+    return ram_size;
+}

This can be read through fw_cfg, any reason to do PIO for this?

Quoted 6 lines
hw/testdev.c new file +6 -0
+static void test_device_irq_line(void *opaque, uint32_t addr, uint32_t data)
+{
+    qemu_set_irq(isa_get_irq(addr - 0x2000), !!data);
+}
+
+static uint32 test_device_ioport_data;

hw/testdev.c line 46

Quoted 10 lines
hw/testdev.c new file +10 -0
+
+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;
+}

Would be nicer to do this via an opaque.

hw/testdev.c line 55

Quoted 8 lines
hw/testdev.c new file +8 -0
+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);

This hard codes page size (get it via sysconf). I think mprotect probably isn't available on windows either.

Quoted 72 lines
hw/testdev.c new file +72 -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 CPUReadMemoryFunc * const test_iomem_read[3] = {
+    test_iomem_readb,
+    test_iomem_readw,
+    test_iomem_readl,
+};
+
+static CPUWriteMemoryFunc * const test_iomem_write[3] = {
+    test_iomem_writeb,
+    test_iomem_writew,
+    test_iomem_writel,
+};
+
+static int init_test_device(ISADevice *isa)
+{
+    struct testdev *dev = DO_UPCAST(struct testdev, dev, isa);
+    int iomem;
+
+    register_ioport_write(0xf1, 1, 1, test_device_serial_write, dev);
+    register_ioport_write(0xf4, 1, 4, test_device_exit, dev);
+    register_ioport_read(0xd1, 1, 4, test_device_memsize_read, dev);
+    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);
+    iomem = cpu_register_io_memory(test_iomem_read, test_iomem_write, NULL,
+                                   DEVICE_NATIVE_ENDIAN);
+    cpu_register_physical_memory(0xff000000, 0x10000, iomem);
+    return 0;
+}
+
+static ISADeviceInfo testdev_info = {
+    .qdev.name  = "testdev",
+    .qdev.size  = sizeof(struct testdev),
Quoted 8 lines

hw/testdev.c line 133

Quoted 6 lines
hw/testdev.c new file +6 -0
+    .init       = init_test_device,
+    .qdev.props = (Property[]) {
+        DEFINE_PROP_CHR("chardev", struct testdev, chr),
+        DEFINE_PROP_END_OF_LIST(),
+    },
+};

Should this use MemoryRegion?

Yes. And what is the reason for using IO ports?
There are archs that dont have ioport connections out from the CPU.

If we are adding virtual devices for tests, they should preferably work for all archs.

Cheers

#3 lore raw
Quoted 198 lines · Edgar E. Iglesias
Quoted 188 lines · Anthony Liguori
Quoted 8 lines · Lucas Meneghel Rodrigues

Add a test device which supports the kvmctl ioports, for running the KVM test suite. This is a straight port from the latest version of the test device present on qemu-kvm, using the APIs currently in use by qemu.

With this we aim for daily execution of the KVM unittests to capture any problems with the KVM interface.

I know this has come up before so apologies if this is redundant.

Quoted 44 lines

Usage:

  qemu
     -chardev file,path=/log/file/some/where,id=testlog -device testdev,chardev=testlog

---
 Makefile.target |    1 +
 hw/testdev.c    |  140 +++++++++++++++++++++++++++++++++++++++++++++++++++++++

 2 files changed, 141 insertions(+), 0 deletions(-)

 create mode 100644 hw/testdev.c

Makefile.target +0 -0
index e280bf6..e095dd5 100644
@@ -232,6 +232,7 @@ obj-i386-y += debugcon.o multiboot.o

 obj-i386-y += pc_piix.o
 obj-i386-$(CONFIG_KVM) += kvmclock.o obj-i386-$(CONFIG_SPICE) += qxl.o qxl-logger.o qxl-render.o +obj-i386-y += testdev.o

 # shared objects
 obj-ppc-y = ppc.o
hw/testdev.c new file +9 -0
new file mode 100644
index 0000000..e38c20e
@@ -0,0 +1,140 @@
+#include<sys/mman.h>
+#include "hw.h"
+#include "qdev.h"
+#include "isa.h"
+
+struct testdev {
+    ISADevice dev;
+    CharDriverState *chr;
+};

hw/testdev.c line 19

Quoted 10 lines
hw/testdev.c new file +10 -0
+
+static void test_device_serial_write(void *opaque, uint32_t addr, uint32_t data)
+{
+    struct testdev *dev = opaque;
+    uint8_t buf[1] = { data };
+
+    if (dev->chr) {
+        qemu_chr_fe_write(dev->chr, buf, 1);
+    }
+}

I think I posted patches at some point for kvm unittests to use a standard UART.  Was there any reason not to do use a UART?

hw/testdev.c line 24

Quoted 4 lines
hw/testdev.c new file +4 -0
+static void test_device_exit(void *opaque, uint32_t addr, uint32_t data)
+{
+    exit(data);
+}

Port 501 can do this.

hw/testdev.c line 29

Quoted 5 lines
hw/testdev.c new file +5 -0
+
+static uint32_t test_device_memsize_read(void *opaque, uint32_t addr)
+{
+    return ram_size;
+}

This can be read through fw_cfg, any reason to do PIO for this?

Quoted 6 lines
hw/testdev.c new file +6 -0
+static void test_device_irq_line(void *opaque, uint32_t addr, uint32_t data)
+{
+    qemu_set_irq(isa_get_irq(addr - 0x2000), !!data);
+}
+
+static uint32 test_device_ioport_data;

hw/testdev.c line 46

Quoted 10 lines
hw/testdev.c new file +10 -0
+
+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;
+}

Would be nicer to do this via an opaque.

hw/testdev.c line 55

Quoted 8 lines
hw/testdev.c new file +8 -0
+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);

This hard codes page size (get it via sysconf).  I think mprotect probably isn't available on windows either.

Quoted 72 lines
hw/testdev.c new file +72 -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 CPUReadMemoryFunc * const test_iomem_read[3] = {
+    test_iomem_readb,
+    test_iomem_readw,
+    test_iomem_readl,
+};
+
+static CPUWriteMemoryFunc * const test_iomem_write[3] = {
+    test_iomem_writeb,
+    test_iomem_writew,
+    test_iomem_writel,
+};
+
+static int init_test_device(ISADevice *isa)
+{
+    struct testdev *dev = DO_UPCAST(struct testdev, dev, isa);
+    int iomem;
+
+    register_ioport_write(0xf1, 1, 1, test_device_serial_write, dev);
+    register_ioport_write(0xf4, 1, 4, test_device_exit, dev);
+    register_ioport_read(0xd1, 1, 4, test_device_memsize_read, dev);
+    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);
+    iomem = cpu_register_io_memory(test_iomem_read, test_iomem_write, NULL,
+                                   DEVICE_NATIVE_ENDIAN);
+    cpu_register_physical_memory(0xff000000, 0x10000, iomem);
+    return 0;
+}
+
+static ISADeviceInfo testdev_info = {
+    .qdev.name  = "testdev",
+    .qdev.size  = sizeof(struct testdev),
Quoted 8 lines

hw/testdev.c line 133

Quoted 6 lines
hw/testdev.c new file +6 -0
+    .init       = init_test_device,
+    .qdev.props = (Property[]) {
+        DEFINE_PROP_CHR("chardev", struct testdev, chr),
+        DEFINE_PROP_END_OF_LIST(),
+    },
+};

Should this use MemoryRegion?

Quoted 5 lines

Yes. And what is the reason for using IO ports?
There are archs that dont have ioport connections out from the CPU.

If we are adding virtual devices for tests, they should preferably work for all archs.

Fully agree.

In a way fw_cfg would be more suitable, but a separate device that combines all debug/test facilities is better since it can be removed from production builds.

I'd also remove port 501 since this makes it redundant, before 501 becomes too entrenched.

But I fear this kind of device may become over time a huge monster with all kinds of bells and whistles. There's no hardware specification that would limit the growth of these kind of hacks.
Next: poke port 0xf2 for buzzer sound to wake up the test person, port 0xf3 for the vibrator and finally port 0xf4 gives electrical shocks. In 2012, a comprehensive test report with charts and graphs with error bars in PDF format can be downloaded via port 0x3001, dial 0x3000+intl. country code for localized Mandarin Chinese version. Would that matter though?

At least, instead of using so many ports, please use only one or two and introduce some kind of multiplexing protocol like fw_cfg does.

#4 lore raw
In reply to Edgar E. Iglesias
Quoted 11 lines · Edgar E. Iglesias
Quoted 11 lines
Quoted 10 lines
hw/testdev.c new file +10 -0
+
+static ISADeviceInfo testdev_info = {
+    .qdev.name  = "testdev",
+    .qdev.size  = sizeof(struct testdev),
+    .init       = init_test_device,
+    .qdev.props = (Property[]) {
+        DEFINE_PROP_CHR("chardev", struct testdev, chr),
+        DEFINE_PROP_END_OF_LIST(),
+    },
+};
Quoted 3 lines
Quoted 1 line

Should this use MemoryRegion?

Yes. And what is the reason for using IO ports?

Mostly for ease of use. The tests were originally run under a separate kvm userspace that didn't emulate a full machine.

Quoted 5 lines

There are archs that dont have ioport connections out from the CPU.

If we are adding virtual devices for tests, they should preferably work for all archs.

I think all the functionallity here (apart from that which Anthony pointed out is available by other means) is x86 specific.

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#5 lore raw
In reply to Anthony Liguori
Quoted 3 lines · Anthony Liguori

I think I posted patches at some point for kvm unittests to use a standard UART. Was there any reason not to do use a UART?

No.

Quoted 7 lines

hw/testdev.c line 24

Quoted 5 lines

+static void test_device_exit(void *opaque, uint32_t addr, uint32_t data)

hw/testdev.c new file +3 -0
+{
+    exit(data);
+}

Port 501 can do this.

Right.

Quoted 8 lines

hw/testdev.c line 29

Quoted 6 lines
hw/testdev.c new file +5 -0
+
+static uint32_t test_device_memsize_read(void *opaque, uint32_t addr)
+{
+    return ram_size;
+}

This can be read through fw_cfg, any reason to do PIO for this?

Legacy. We even have a fw_cfg driver in k-u-t.git.

Quoted 11 lines

hw/testdev.c line 55

Quoted 9 lines

+static void test_device_flush_page(void *opaque, uint32_t addr, uint32_t data)

hw/testdev.c new file +7 -0
+{
+    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);

This hard codes page size (get it via sysconf).

(or getpagesize())

Quoted 1 line

I think mprotect probably isn't available on windows either.

Google thinks it is.

Quoted 2 lines

Should this use MemoryRegion?

Yes.

Lucas, do you want to tackle these yourself? I'll help with any questions. Otherwise I'll do it.

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#6 lore raw
In reply to Anthony Liguori
Quoted 63 lines · Anthony Liguori
Quoted 8 lines · Lucas Meneghel Rodrigues

Add a test device which supports the kvmctl ioports, for running the KVM test suite. This is a straight port from the latest version of the test device present on qemu-kvm, using the APIs currently in use by qemu.

With this we aim for daily execution of the KVM unittests to capture any problems with the KVM interface.

I know this has come up before so apologies if this is redundant.

Quoted 51 lines

Usage:

qemu

(quoted diff) +0 -2
-chardev file,path=/log/file/some/where,id=testlog
-device testdev,chardev=testlog

---
Makefile.target | 1 +
hw/testdev.c | 140
+++++++++++++++++++++++++++++++++++++++++++++++++++++++

2 files changed, 141 insertions(+), 0 deletions(-)

create mode 100644 hw/testdev.c

Makefile.target +0 -0
index e280bf6..e095dd5 100644
@@ -232,6 +232,7 @@ obj-i386-y += debugcon.o multiboot.o

obj-i386-y += pc_piix.o
obj-i386-$(CONFIG_KVM) += kvmclock.o obj-i386-$(CONFIG_SPICE) += qxl.o qxl-logger.o qxl-render.o +obj-i386-y += testdev.o

# shared objects
obj-ppc-y = ppc.o
hw/testdev.c new file +11 -0
new file mode 100644
index 0000000..e38c20e
@@ -0,0 +1,140 @@
+#include<sys/mman.h>
+#include "hw.h"
+#include "qdev.h"
+#include "isa.h"
+
+struct testdev {
+ ISADevice dev;
+ CharDriverState *chr;
+};
+
+static void test_device_serial_write(void *opaque, uint32_t addr,

uint32_t data)
+{
+ struct testdev *dev = opaque;
+ uint8_t buf[1] = { data };

Quoted 8 lines

hw/testdev.c line 19

Quoted 5 lines
hw/testdev.c new file +5 -0
+
+ if (dev->chr) {
+ qemu_chr_fe_write(dev->chr, buf, 1);
+ }
+}

I think I posted patches at some point for kvm unittests to use a standard UART. Was there any reason not to do use a UART?

I will look for your patches and adapt them to the current conditions.

Quoted 7 lines

hw/testdev.c line 24

Quoted 4 lines
hw/testdev.c new file +4 -0
+static void test_device_exit(void *opaque, uint32_t addr, uint32_t data)
+{
+ exit(data);
+}

Port 501 can do this.

Quoted 7 lines

hw/testdev.c line 29

Quoted 5 lines
hw/testdev.c new file +5 -0
+
+static uint32_t test_device_memsize_read(void *opaque, uint32_t addr)
+{
+ return ram_size;
+}

This can be read through fw_cfg, any reason to do PIO for this?

He already explained this, but this was an adaptation of an independent userspace program to run the unittests. I will convert to use fw_cfg then.

Quoted 12 lines
Quoted 12 lines

+static void test_device_irq_line(void *opaque, uint32_t addr, uint32_t data)

hw/testdev.c new file +7 -0
+{
+ qemu_set_irq(isa_get_irq(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;

Quoted 8 lines

hw/testdev.c line 46

Quoted 6 lines
hw/testdev.c new file +6 -0
+}
+
+static uint32_t test_device_ioport_read(void *opaque, uint32_t addr)
+{
+ return test_device_ioport_data;
+}

Would be nicer to do this via an opaque.

Ok.

Quoted 4 lines
Quoted 4 lines

+static void test_device_flush_page(void *opaque, uint32_t addr, uint32_t data)
+{
+ target_phys_addr_t len = 4096;

Quoted 8 lines
Quoted 5 lines

+ 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);

This hard codes page size (get it via sysconf). I think mprotect probably isn't available on windows either.

Sure, good point, will look into doing it with getpagesize

Quoted 75 lines
Quoted 75 lines
hw/testdev.c new file +20 -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)

hw/testdev.c new file +5 -0
+{
+ iomem_buf[addr] = val;
+}
+
+static void test_iomem_writew(void *opaque, target_phys_addr_t addr,

uint32_t val)

hw/testdev.c new file +5 -0
+{
+ *(uint16_t*)(iomem_buf + addr) = val;
+}
+
+static void test_iomem_writel(void *opaque, target_phys_addr_t addr,

uint32_t val)

hw/testdev.c new file +42 -0
+{
+ *(uint32_t*)(iomem_buf + addr) = val;
+}
+
+static CPUReadMemoryFunc * const test_iomem_read[3] = {
+ test_iomem_readb,
+ test_iomem_readw,
+ test_iomem_readl,
+};
+
+static CPUWriteMemoryFunc * const test_iomem_write[3] = {
+ test_iomem_writeb,
+ test_iomem_writew,
+ test_iomem_writel,
+};
+
+static int init_test_device(ISADevice *isa)
+{
+ struct testdev *dev = DO_UPCAST(struct testdev, dev, isa);
+ int iomem;
+
+ register_ioport_write(0xf1, 1, 1, test_device_serial_write, dev);
+ register_ioport_write(0xf4, 1, 4, test_device_exit, dev);
+ register_ioport_read(0xd1, 1, 4, test_device_memsize_read, dev);
+ 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);
+ iomem = cpu_register_io_memory(test_iomem_read, test_iomem_write, NULL,
+ DEVICE_NATIVE_ENDIAN);
+ cpu_register_physical_memory(0xff000000, 0x10000, iomem);
+ return 0;
+}
+
+static ISADeviceInfo testdev_info = {
+ .qdev.name = "testdev",
+ .qdev.size = sizeof(struct testdev),
Quoted 8 lines
Quoted 6 lines

+ .init = init_test_device,
+ .qdev.props = (Property[]) {
+ DEFINE_PROP_CHR("chardev", struct testdev, chr),
+ DEFINE_PROP_END_OF_LIST(),
+ },
+};

Should this use MemoryRegion?

Yep, will look into converting it.

Quoted 1 line

Regards,

Thanks!

#7 lore raw
In reply to Lucas Meneghel Rodrigues
Quoted 8 lines · Lucas Meneghel Rodrigues

Add a test device which supports the kvmctl ioports, for running the KVM test suite. This is a straight port from the latest version of the test device present on qemu-kvm, using the APIs currently in use by qemu.

With this we aim for daily execution of the KVM unittests to capture any problems with the KVM interface.

In principle this works with TCG as well.

Quoted 22 lines

Usage:

qemu
-chardev file,path=/log/file/some/where,id=testlog -device testdev,chardev=testlog

---
 Makefile.target |    1 +
 hw/testdev.c    |  140 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 141 insertions(+), 0 deletions(-)
 create mode 100644 hw/testdev.c
Makefile.target +0 -0
index e280bf6..e095dd5 100644

Makefile.target obj-i386-y += debugcon.o multiboot.o · line 235

Quoted 4 lines
Makefile.target +1 -0
@@ -232,6 +232,7 @@ obj-i386-y += debugcon.o multiboot.o
 obj-i386-y += pc_piix.o
 obj-i386-$(CONFIG_KVM) += kvmclock.o
 obj-i386-$(CONFIG_SPICE) += qxl.o qxl-logger.o qxl-render.o
+obj-i386-y += testdev.o

The name testdev is a bit too generic for this thing in its current form. I would suggest x86-testdev or pc-testdev - or a generalization that allows a PIO-free registration against the system bus.

Quoted 32 lines
Makefile.target +0 -0
 # shared objects
 obj-ppc-y = ppc.o
hw/testdev.c new file +23 -0
new file mode 100644
index 0000000..e38c20e
@@ -0,0 +1,140 @@
+#include <sys/mman.h>
+#include "hw.h"
+#include "qdev.h"
+#include "isa.h"
+
+struct testdev {
+    ISADevice dev;
+    CharDriverState *chr;
+};
+
+static void test_device_serial_write(void *opaque, uint32_t addr, uint32_t data)
+{
+    struct testdev *dev = opaque;
+    uint8_t buf[1] = { data };
+
+    if (dev->chr) {
+        qemu_chr_fe_write(dev->chr, buf, 1);
+    }
+}
+
+static void test_device_exit(void *opaque, uint32_t addr, uint32_t data)
+{
+    exit(data);

hw/testdev.c line 33

Quoted 10 lines
hw/testdev.c new file +10 -0
+}
+
+static uint32_t test_device_memsize_read(void *opaque, uint32_t addr)
+{
+    return ram_size;
+}
+
+static void test_device_irq_line(void *opaque, uint32_t addr, uint32_t data)
+{
+    qemu_set_irq(isa_get_irq(addr - 0x2000), !!data);

Note that qemu-kvm retrieves (due to a hack) GSIs via isa_get_irq while QEMU is and will remain confined to true ISA IRQs, thus provides no access to IRQs 16-23. May break existing tests. So we likely have to introduce a cleaner interface now if GSI is what you need here.

Quoted 78 lines
hw/testdev.c new file +78 -0
+}
+
+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 CPUReadMemoryFunc * const test_iomem_read[3] = {
+    test_iomem_readb,
+    test_iomem_readw,
+    test_iomem_readl,
+};
+
+static CPUWriteMemoryFunc * const test_iomem_write[3] = {
+    test_iomem_writeb,
+    test_iomem_writew,
+    test_iomem_writel,
+};
+
+static int init_test_device(ISADevice *isa)
+{
+    struct testdev *dev = DO_UPCAST(struct testdev, dev, isa);
+    int iomem;
+
+    register_ioport_write(0xf1, 1, 1, test_device_serial_write, dev);
+    register_ioport_write(0xf4, 1, 4, test_device_exit, dev);
+    register_ioport_read(0xd1, 1, 4, test_device_memsize_read, dev);
+    register_ioport_read(0xe0, 1, 1, test_device_ioport_read, dev);
+    register_ioport_write(0xe0, 1, 1, test_device_ioport_write, dev);

hw/testdev.c line 121

Quoted 10 lines
hw/testdev.c new file +10 -0
+    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);
+    iomem = cpu_register_io_memory(test_iomem_read, test_iomem_write, NULL,
+                                   DEVICE_NATIVE_ENDIAN);
+    cpu_register_physical_memory(0xff000000, 0x10000, iomem);

We have MemoryRegion for MMIO and PIO now.

Quoted 9 lines
hw/testdev.c new file +9 -0
+    return 0;
+}
+
+static ISADeviceInfo testdev_info = {
+    .qdev.name  = "testdev",
+    .qdev.size  = sizeof(struct testdev),
+    .init       = init_test_device,
+    .qdev.props = (Property[]) {
+        DEFINE_PROP_CHR("chardev", struct testdev, chr),

hw/testdev.c line 140

Quoted 10 lines
hw/testdev.c new file +10 -0
+        DEFINE_PROP_END_OF_LIST(),
+    },
+};
+
+static void testdev_register_devices(void)
+{
+    isa_qdev_register(&testdev_info);
+}
+
+device_init(testdev_register_devices)

Jan

#8 lore raw
Quoted 9 lines · Jan Kiszka
Quoted 4 lines

+
+static void test_device_irq_line(void *opaque, uint32_t addr, uint32_t data) +{
+ qemu_set_irq(isa_get_irq(addr - 0x2000), !!data);

Note that qemu-kvm retrieves (due to a hack) GSIs via isa_get_irq while QEMU is and will remain confined to true ISA IRQs, thus provides no access to IRQs 16-23. May break existing tests. So we likely have to introduce a cleaner interface now if GSI is what you need here.

IIRC, gsi is wanted so we can hit on unallocated gsis.

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#9 lore raw
In reply to Lucas Meneghel Rodrigues
Quoted 4 lines · Lucas Meneghel Rodrigues

Add a test device which supports the kvmctl ioports, for running the KVM test suite. This is a straight port from the latest version of the test device present on qemu-kvm, using the APIs currently in use by qemu.

Or rather before recent conversions.

Quoted 39 lines

With this we aim for daily execution of the KVM unittests to capture any problems with the KVM interface.

Usage:

 qemu
    -chardev file,path=/log/file/some/where,id=testlog -device testdev,chardev=testlog

---
 Makefile.target |    1 +
 hw/testdev.c    |  140 +++++++++++++++++++++++++++++++++++++++++++++++++++++++

 2 files changed, 141 insertions(+), 0 deletions(-)

 create mode 100644 hw/testdev.c

Makefile.target +0 -0
index e280bf6..e095dd5 100644
@@ -232,6 +232,7 @@ obj-i386-y += debugcon.o multiboot.o

 obj-i386-y += pc_piix.o
 obj-i386-$(CONFIG_KVM) += kvmclock.o obj-i386-$(CONFIG_SPICE) += qxl.o qxl-logger.o qxl-render.o +obj-i386-y += testdev.o

 # shared objects
 obj-ppc-y = ppc.o
hw/testdev.c new file +0 -0
new file mode 100644
index 0000000..e38c20e

hw/testdev.c line 9

Quoted 9 lines
hw/testdev.c new file +9 -0
@@ -0,0 +1,140 @@
+#include <sys/mman.h>
+#include "hw.h"
+#include "qdev.h"
+#include "isa.h"
+
+struct testdev {
+    ISADevice dev;
+    CharDriverState *chr;
+};

Please read CODING_STYLE: typedef and struct TestDev(ice).

Quoted 14 lines
hw/testdev.c new file +14 -0
+
+static void test_device_serial_write(void *opaque, uint32_t addr, uint32_t data)
+{
+    struct testdev *dev = opaque;
+    uint8_t buf[1] = { data };
+
+    if (dev->chr) {
+        qemu_chr_fe_write(dev->chr, buf, 1);
+    }
+}
+
+static void test_device_exit(void *opaque, uint32_t addr, uint32_t data)
+{
+    exit(data);

hw/testdev.c line 33

Quoted 10 lines
hw/testdev.c new file +10 -0
+}
+
+static uint32_t test_device_memsize_read(void *opaque, uint32_t addr)
+{
+    return ram_size;
+}
+
+static void test_device_irq_line(void *opaque, uint32_t addr, uint32_t data)
+{
+    qemu_set_irq(isa_get_irq(addr - 0x2000), !!data);

Where does 0x2000 come from?

Quoted 15 lines
hw/testdev.c new file +15 -0
+}
+
+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)

hw/testdev.c line 58

Quoted 10 lines
hw/testdev.c new file +10 -0
+{
+    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;

Please move this to state.

hw/testdev.c line 68

Quoted 10 lines
hw/testdev.c new file +10 -0
+
+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);
+}

This and the other functions assume that the memory is available and the guest and the host are of same endianness.

This looks like pure RAM, so MMIO is not the best way to do this. Please just map more RAM.

What's the use of this anyway, it doesn't affect QEMU in any way? Scratch space?

Quoted 39 lines
hw/testdev.c new file +39 -0
+
+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 CPUReadMemoryFunc * const test_iomem_read[3] = {
+    test_iomem_readb,
+    test_iomem_readw,
+    test_iomem_readl,
+};
+
+static CPUWriteMemoryFunc * const test_iomem_write[3] = {
+    test_iomem_writeb,
+    test_iomem_writew,
+    test_iomem_writel,
+};
+
+static int init_test_device(ISADevice *isa)
+{
+    struct testdev *dev = DO_UPCAST(struct testdev, dev, isa);
+    int iomem;
+
+    register_ioport_write(0xf1, 1, 1, test_device_serial_write, dev);

hw/testdev.c line 117

Quoted 10 lines
hw/testdev.c new file +10 -0
+    register_ioport_write(0xf4, 1, 4, test_device_exit, dev);
+    register_ioport_read(0xd1, 1, 4, test_device_memsize_read, dev);
+    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);

24? Doesn't ISA have only 16? Enums for all constants would be more readable.

hw/testdev.c line 121

Quoted 4 lines
hw/testdev.c new file +4 -0
+    iomem_buf = g_malloc0(0x10000);
+    iomem = cpu_register_io_memory(test_iomem_read, test_iomem_write, NULL,
+                                   DEVICE_NATIVE_ENDIAN);
+    cpu_register_physical_memory(0xff000000, 0x10000, iomem);

Devices may not map themselves, this should be done at board level. Doesn't this address also conflict with PCI for PC?

Quoted 24 lines
hw/testdev.c new file +19 -1
+    return 0;
+}
+
+static ISADeviceInfo testdev_info = {
+    .qdev.name  = "testdev",
+    .qdev.size  = sizeof(struct testdev),
+    .init       = init_test_device,
+    .qdev.props = (Property[]) {
+        DEFINE_PROP_CHR("chardev", struct testdev, chr),
+        DEFINE_PROP_END_OF_LIST(),
+    },
+};
+
+static void testdev_register_devices(void)
+{
+    isa_qdev_register(&testdev_info);
+}
+
+device_init(testdev_register_devices)
--

1.7.6

#10 lore raw
Quoted 6 lines · Blue Swirl
Quoted 4 lines

+
+static void test_device_irq_line(void *opaque, uint32_t addr, uint32_t data) +{
+ qemu_set_irq(isa_get_irq(addr - 0x2000), !!data);

Where does 0x2000 come from?

The base address of this range.

Quoted 11 lines
Quoted 5 lines

+
+static uint32_t test_iomem_readw(void *opaque, target_phys_addr_t addr) +{
+ return *(uint16_t*)(iomem_buf + addr);
+}

This and the other functions assume that the memory is available and the guest and the host are of same endianness.

This looks like pure RAM, so MMIO is not the best way to do this. Please just map more RAM.

The intent is to get the guest to fault and exercise kvm's instruction emulator. That won't happen with RAM.

(well, with a sub-page mapping, it should)

Quoted 20 lines

What's the use of this anyway, it doesn't affect QEMU in any way? Scratch space?

Quoted 5 lines

+static int init_test_device(ISADevice *isa)
+{
+ struct testdev *dev = DO_UPCAST(struct testdev, dev, isa);
+ int iomem;
+

Quoted 11 lines
 +    register_ioport_write(0xf1, 1, 1, test_device_serial_write, dev);
 +    register_ioport_write(0xf4, 1, 4, test_device_exit, dev);
 +    register_ioport_read(0xd1, 1, 4, test_device_memsize_read, dev);
 +    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);
Quoted 1 line

24? Doesn't ISA have only 16? Enums for all constants would be more readable.

GSI space - the ioapic pins. It's really a motherboard device.

Quoted 8 lines
Quoted 5 lines

+ iomem_buf = g_malloc0(0x10000);
+ iomem = cpu_register_io_memory(test_iomem_read, test_iomem_write, NULL,
+ DEVICE_NATIVE_ENDIAN);
+ cpu_register_physical_memory(0xff000000, 0x10000, iomem);

Devices may not map themselves, this should be done at board level. Doesn't this address also conflict with PCI for PC?

Probably. The whole thing is a hack!

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#11 lore raw
Quoted 64 lines · Avi Kivity
Quoted 8 lines · Blue Swirl
Quoted 6 lines

 +
 +static void test_device_irq_line(void *opaque, uint32_t addr, uint32_t data)
 +{
 +    qemu_set_irq(isa_get_irq(addr - 0x2000), !!data);

Where does 0x2000 come from?

The base address of this range.

Quoted 7 lines
Quoted 6 lines

 +
 +static uint32_t test_iomem_readw(void *opaque, target_phys_addr_t addr)
 +{
 +    return *(uint16_t*)(iomem_buf + addr);
 +}

Quoted 5 lines

This and the other functions assume that the memory is available and the guest and the host are of same endianness.

This looks like pure RAM, so MMIO is not the best way to do this. Please just map more RAM.

The intent is to get the guest to fault and exercise kvm's instruction emulator.  That won't happen with RAM.

(well, with a sub-page mapping, it should)

Quoted 21 lines

What's the use of this anyway, it doesn't affect QEMU in any way? Scratch space?

Quoted 8 lines

 +static int init_test_device(ISADevice *isa)
 +{
 +    struct testdev *dev = DO_UPCAST(struct testdev, dev, isa);
 +    int iomem;
 +
 +    register_ioport_write(0xf1, 1, 1, test_device_serial_write, dev);
 +    register_ioport_write(0xf4, 1, 4, test_device_exit, dev);
 +    register_ioport_read(0xd1, 1, 4, test_device_memsize_read, dev);

Quoted 8 lines

 +    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);

Quoted 2 lines

24? Doesn't ISA have only 16? Enums for all constants would be more readable.

GSI space - the ioapic pins.  It's really a motherboard device.

Quoted 7 lines
Quoted 6 lines

 +    iomem_buf = g_malloc0(0x10000);
 +    iomem = cpu_register_io_memory(test_iomem_read, test_iomem_write, NULL,
 +                                   DEVICE_NATIVE_ENDIAN);
 +    cpu_register_physical_memory(0xff000000, 0x10000, iomem);

Quoted 4 lines
Quoted 2 lines

Devices may not map themselves, this should be done at board level. Doesn't this address also conflict with PCI for PC?

Probably.  The whole thing is a hack!

The use case is valid, to help with testing and debugging. But this is already a bit baroque and I foresee an endless stream of hacks in this area.

Maybe the device should be as simple as possible but then connect to some kind of external test program or plugin? Then that one could include a beowulf cluster of PDF test report generators or even Eclipse and we wouldn't care less here.

Even better, taking the instrumentation approach, could the test device be left out completely, just use guest invisible methods (like watchpoints) to interact with the guest?

#12 lore raw
Quoted 3 lines · Blue Swirl

Even better, taking the instrumentation approach, could the test device be left out completely, just use guest invisible methods (like watchpoints) to interact with the guest?

I don't get it. Sorry but I've not been closely following the thread, and a quick look at it gave me no clues about what you mean.

Lluis

Signature · 4 lines
 "And it's much the same thing with knowledge, for whenever you learn
 something new, the whole world becomes that much richer."
 -- The Princess of Pure Reason, as told by Norton Juster in The Phantom
 Tollbooth
#13 lore raw
Quoted 8 lines · Lluís
Quoted 3 lines · Blue Swirl

Even better, taking the instrumentation approach, could the test device be left out completely, just use guest invisible methods (like watchpoints) to interact with the guest?

I don't get it. Sorry but I've not been closely following the thread, and a quick look at it gave me no clues about what you mean.

The use case for the proposed test device is that guest and host can interact during testing and debugging, preferably not for production. But I think instrumentation needs are not unlike this case. I think I proposed earlier to you that instead of intrusive code changes, guest invisible methods, for example watchpoints and virtual hardware could be used. Maybe we could be able to solve several problems at once? If the test device would provide a generic way to connect to an external program, wouldn't that be useful also for instrumentation?

#14 lore raw
Quoted 10 lines · Blue Swirl
Quoted 8 lines · Lluís
Quoted 3 lines · Blue Swirl

Even better, taking the instrumentation approach, could the test device be left out completely, just use guest invisible methods (like watchpoints) to interact with the guest?

I don't get it. Sorry but I've not been closely following the thread, and a quick look at it gave me no clues about what you mean.

Quoted 8 lines

The use case for the proposed test device is that guest and host can interact during testing and debugging, preferably not for production. But I think instrumentation needs are not unlike this case. I think I proposed earlier to you that instead of intrusive code changes, guest invisible methods, for example watchpoints and virtual hardware could be used. Maybe we could be able to solve several problems at once? If the test device would provide a generic way to connect to an external program, wouldn't that be useful also for instrumentation?

Ah, now I get it. Well, guest-host interaction is what I call the backdoor channel, which from my point of view is orthogonal to instrumentation. Of course, you could implement the former on top of the latter.

But I still believe using watchpoints is too heavyweight for the kind of instrumentation that I want (watchpoints go through the slow memory access path, while I instrument the guest during TCG code generation), and does not support all the kinds of information that I want to gather (i.e., the registers that a specific guest instruction is using).

On the other hand, you really convinced me that using virtual devices is the right thing to do to implement a backdoor channel, as opposed to my previous approach of adding per-target special instructions. Thus it will work in all modes (TCG and KVM) and in all targets.

Lluis

Signature · 4 lines
 "And it's much the same thing with knowledge, for whenever you learn
 something new, the whole world becomes that much richer."
 -- The Princess of Pure Reason, as told by Norton Juster in The Phantom
 Tollbooth