Skip to contentSkip to conversation

qtest: a testing framework for devices (v2)

PATCH In review Anthony Liguori 6 participants 24 messages Feb 25, 2012 – Mar 2, 2012 Kbuild

lore
Share & tools
lore
#0 lore raw

cover qtest: a testing framework for devices (v2)

Hi,

This is an updated version of the qtest patches I posted about a month ago. I've included the RFC that Paolo posted in this series too.

#1 lore raw

PATCH 1/10 qtest: add test framework (v2)

The idea behind qtest is pretty simple. Instead of executing a CPU via TCG or KVM, rely on an external process to send events to the device model that the CPU would normally generate.

qtest presents itself as an accelerator. In addition, a new option is added to establish a qtest server (-qtest) that takes a character device. This is what allows the external process to send CPU events to the device model.

This is currently modelled after Xen since the Xen device model does something very similar. Instead of hooking cpu_exec, Xen sticks the CPU in the halted state making sure it never gets to execute. In addition, Xen replaces the LAPIC with a dummy interrupt controller that forwards interrupt requests.

qtest does the exact same thing and uses a simple line based protocol to send the events. Documentation of that protocol is in qtest.c.

I considered reusing the monitor for this job. Adding interrupts would be a bit difficult. In addition, logging would also be difficult.

qtest has extensive logging support. All protocol commands are logged with time stamps using a new command line option (-qtest-log). Logging is important since ultimately, this is a feature for debugging.

---
v1 -> v2
- always send a response (Paolo)
- enable echo (Paolo)
- do not use TCG CPU threads (Paolo)

---
 Makefile.objs   |    2 +
 cpu-exec.c      |    1 +
 cpus.c          |   62 +++++++++-
 qemu-options.hx |    8 ++
 qtest.c         |  359 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qtest.h         |   37 ++++++
 vl.c            |    8 ++
 7 files changed, 474 insertions(+), 3 deletions(-)
 create mode 100644 qtest.c
 create mode 100644 qtest.h
Makefile.objs +2 -0
index 808de6a..3372d9b 100644
@@ -302,6 +302,8 @@ hw-obj-$(CONFIG_DP8393X) += dp8393x.o
 hw-obj-$(CONFIG_DS1225Y) += ds1225y.o
 hw-obj-$(CONFIG_MIPSNET) += mipsnet.o
 
+hw-obj-y += qtest.o
+
 # Sound
 sound-obj-y =
 sound-obj-$(CONFIG_SB16) += sb16.o
cpu-exec.c +1 -0
index 2c2d24e..d476616 100644
@@ -21,6 +21,7 @@
 #include "disas.h"
 #include "tcg.h"
 #include "qemu-barrier.h"
+#include "qtest.h"
 
 int tb_invalidated_flag;
 
cpus.c +59 -3

93 lines not on this page · raw message

qemu-options.hx +8 -0
index b129996..3dca2dc 100644
@@ -2698,6 +2698,14 @@ the @var{simple} tracing backend.
 @end table
 ETEXI
 
+DEF("qtest", HAS_ARG, QEMU_OPTION_qtest,
+    "-qtest CHR      specify tracing options\n",
+    QEMU_ARCH_ALL)
+
+DEF("qtest-log", HAS_ARG, QEMU_OPTION_qtest_log,
+    "-qtest-log LOG  specify tracing options\n",
+    QEMU_ARCH_ALL)
+
 HXCOMM This is the last statement. Insert new options before this line!
 STEXI
 @end table
qtest.c new file +359 -0

359 lines not on this page · raw message

qtest.h new file +37 -0
new file mode 100644
index 0000000..f0e1377
@@ -0,0 +1,37 @@
+/*
+ * Test Server
+ *
+ * Copyright IBM, Corp. 2011
+ *
+ * Authors:
+ *  Anthony Liguori   <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+
+#ifndef QTEST_H
+#define QTEST_H
+
+#include "qemu-common.h"
+
+extern int qtest_allowed;
+extern const char *qtest_chrdev;
+extern const char *qtest_log;
+
+static inline bool qtest_enabled(void)
+{
+    return qtest_allowed;
+}
+
+static inline int qtest_available(void)
+{
+    return 1;
+}
+
+int qtest_init(void);
+
+qemu_irq *qtest_interrupt_controller_init(void);
+
+#endif
vl.c +8 -0
index 1d4c350..1473016 100644
@@ -152,6 +152,7 @@ int main(int argc, char **argv)
 #ifdef CONFIG_VIRTFS
 #include "fsdev/qemu-fsdev.h"
 #endif
+#include "qtest.h"
 
 #include "disas.h"
 
@@ -2097,6 +2098,7 @@ static struct {
     { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
     { "xen", "Xen", xen_available, xen_init, &xen_allowed },
     { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
+    { "qtest", "QTest", qtest_available, qtest_init, &qtest_allowed },
 };
 
 static int configure_accelerator(void)
@@ -3180,6 +3182,12 @@ int main(int argc, char **argv, char **envp)
                     fclose(fp);
                     break;
                 }
+            case QEMU_OPTION_qtest:
+                qtest_chrdev = optarg;
+                break;
+            case QEMU_OPTION_qtest_log:
+                qtest_log = optarg;
+                break;
             default:
                 os_parse_cmd_args(popt->index, optarg);
             }
Signature · 1 line
1.7.4.1
#2 lore raw

Re: [Qemu-devel] [PATCH 01/10] qtest: add test framework (v2)

qtest.c line 333

Quoted 5 lines · Anthony Liguori
qtest.c new file +5 -0
+
+qemu_irq *qtest_interrupt_controller_init(void)
+{
+    return qemu_allocate_irqs(qtest_set_irq, NULL, MAX_IRQ);
+}

This should not be needed anymore.

Paolo

#3 lore raw

PATCH 2/10 qtest: add support for -M pc

In reply to Anthony Liguori

This involves replacing the local APIC with the qtest interrupt controller.

It should be pretty straight forward to do the same for other machine types.

---
 hw/pc_piix.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
hw/pc_piix.c +3 -0
index 5e11d15..2c0881e 100644
@@ -46,6 +46,7 @@
 #ifdef CONFIG_XEN
 #  include <xen/hvm/hvm_info_table.h>
 #endif
+#include "qtest.h"
 
 #define MAX_IDE_BUS 2
 
@@ -212,6 +213,8 @@ static void pc_init1(MemoryRegion *system_memory,
         i8259 = kvm_i8259_init(isa_bus);
     } else if (xen_enabled()) {
         i8259 = xen_interrupt_controller_init();
+    } else if (qtest_enabled()) {
+        i8259 = qtest_interrupt_controller_init();
     } else {
         cpu_irq = pc_allocate_cpu_irq();
         i8259 = i8259_init(isa_bus, cpu_irq[0]);
Signature · 1 line
1.7.4.1
#4 lore raw

Re: [Qemu-devel] [PATCH 02/10] qtest: add support for -M pc

Quoted 22 lines · Anthony Liguori

This involves replacing the local APIC with the qtest interrupt controller.

It should be pretty straight forward to do the same for other machine types.

---
 hw/pc_piix.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
hw/pc_piix.c +1 -0
index 5e11d15..2c0881e 100644
@@ -46,6 +46,7 @@
 #ifdef CONFIG_XEN
 #  include <xen/hvm/hvm_info_table.h>
 #endif
+#include "qtest.h"
 
 #define MAX_IDE_BUS 2
 

hw/pc_piix.c static void pc_init1(MemoryRegion *system_memory, · line 220

Quoted 8 lines
hw/pc_piix.c +2 -0
@@ -212,6 +213,8 @@ static void pc_init1(MemoryRegion *system_memory,
         i8259 = kvm_i8259_init(isa_bus);
     } else if (xen_enabled()) {
         i8259 = xen_interrupt_controller_init();
+    } else if (qtest_enabled()) {
+        i8259 = qtest_interrupt_controller_init();
     } else {
         cpu_irq = pc_allocate_cpu_irq();
         i8259 = i8259_init(isa_bus, cpu_irq[0]);

This is not needed anymore.

Paolo

#5 lore raw

Re: [Qemu-devel] [PATCH 02/10] qtest: add support for -M pc

Quoted 22 lines · Paolo Bonzini
Quoted 21 lines · Anthony Liguori

This involves replacing the local APIC with the qtest interrupt controller.

It should be pretty straight forward to do the same for other machine types.

---
  hw/pc_piix.c |    3 +++
  1 files changed, 3 insertions(+), 0 deletions(-)
hw/pc_piix.c +1 -0
index 5e11d15..2c0881e 100644
@@ -46,6 +46,7 @@
  #ifdef CONFIG_XEN
  #  include<xen/hvm/hvm_info_table.h>
  #endif
+#include "qtest.h"
 
  #define MAX_IDE_BUS 2
 
Quoted 11 lines

hw/pc_piix.c static void pc_init1(MemoryRegion *system_memory, · line 220

Quoted 9 lines
hw/pc_piix.c +2 -0
@@ -212,6 +213,8 @@ static void pc_init1(MemoryRegion *system_memory,
          i8259 = kvm_i8259_init(isa_bus);
      } else if (xen_enabled()) {
          i8259 = xen_interrupt_controller_init();
+    } else if (qtest_enabled()) {
+        i8259 = qtest_interrupt_controller_init();
      } else {
          cpu_irq = pc_allocate_cpu_irq();
          i8259 = i8259_init(isa_bus, cpu_irq[0]);

This is not needed anymore.

Why? This is necessary for IRQ to work.

You mean, if you use irq_intercept, this isn't needed?

Regards,

Anthony Liguori

Quoted 4 lines

Paolo

#6 lore raw

Re: [Qemu-devel] [PATCH 02/10] qtest: add support for -M pc

Am 25.02.2012 22:12, schrieb Anthony Liguori:

Quoted 37 lines · Paolo Bonzini
Quoted 24 lines · Anthony Liguori
Quoted 23 lines

This involves replacing the local APIC with the qtest interrupt controller.

It should be pretty straight forward to do the same for other machine types.

---
  hw/pc_piix.c |    3 +++
  1 files changed, 3 insertions(+), 0 deletions(-)
hw/pc_piix.c +1 -0
index 5e11d15..2c0881e 100644
@@ -46,6 +46,7 @@
  #ifdef CONFIG_XEN
  #  include<xen/hvm/hvm_info_table.h>
  #endif
+#include "qtest.h"
 
  #define MAX_IDE_BUS 2
 
Quoted 11 lines

hw/pc_piix.c static void pc_init1(MemoryRegion *system_memory, · line 220

Quoted 9 lines
hw/pc_piix.c +2 -0
@@ -212,6 +213,8 @@ static void pc_init1(MemoryRegion *system_memory,
          i8259 = kvm_i8259_init(isa_bus);
      } else if (xen_enabled()) {
          i8259 = xen_interrupt_controller_init();
+    } else if (qtest_enabled()) {
+        i8259 = qtest_interrupt_controller_init();
      } else {
          cpu_irq = pc_allocate_cpu_irq();
          i8259 = i8259_init(isa_bus, cpu_irq[0]);

This is not needed anymore.

Quoted 3 lines

Why? This is necessary for IRQ to work.

You mean, if you use irq_intercept, this isn't needed?

The reason for Paolo's RFC was to _avoid_ having to touch every target with code such as the above, no?

Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#7 lore raw

Re: [Qemu-devel] [PATCH 02/10] qtest: add support for -M pc

Quoted 38 lines · Andreas Färber

Am 25.02.2012 22:12, schrieb Anthony Liguori:

Quoted 37 lines · Paolo Bonzini
Quoted 35 lines · Anthony Liguori
Quoted 24 lines

This involves replacing the local APIC with the qtest interrupt controller.

It should be pretty straight forward to do the same for other machine types.

---
   hw/pc_piix.c |    3 +++
   1 files changed, 3 insertions(+), 0 deletions(-)
hw/pc_piix.c +1 -0
index 5e11d15..2c0881e 100644
@@ -46,6 +46,7 @@
   #ifdef CONFIG_XEN
   #  include<xen/hvm/hvm_info_table.h>
   #endif
+#include "qtest.h"
 
   #define MAX_IDE_BUS 2
 

hw/pc_piix.c static void pc_init1(MemoryRegion *system_memory, · line 220

Quoted 8 lines
hw/pc_piix.c +2 -0
@@ -212,6 +213,8 @@ static void pc_init1(MemoryRegion *system_memory,
           i8259 = kvm_i8259_init(isa_bus);
       } else if (xen_enabled()) {
           i8259 = xen_interrupt_controller_init();
+    } else if (qtest_enabled()) {
+        i8259 = qtest_interrupt_controller_init();
       } else {
           cpu_irq = pc_allocate_cpu_irq();
           i8259 = i8259_init(isa_bus, cpu_irq[0]);

This is not needed anymore.

Quoted 6 lines
Quoted 3 lines

Why? This is necessary for IRQ to work.

You mean, if you use irq_intercept, this isn't needed?

The reason for Paolo's RFC was to _avoid_ having to touch every target with code such as the above, no?

I misunderstood. I thought irq_intercept was intercepting the RTC IRQ. I now see it's intercepting the I/O APIC irqs.

Regards,

Anthony Liguori

Quoted 3 lines

Andreas

#8 lore raw

Re: [Qemu-devel] [PATCH 02/10] qtest: add support for -M pc

In reply to Anthony Liguori
Quoted 2 lines · Anthony Liguori

Why? This is necessary for IRQ to work.

Actually it's reverted later on in the series, so if you want to commit it as is the end result is surely fine by me. :)

Paolo

#9 lore raw

PATCH 3/10 qtest: add C version of test infrastructure

In reply to Anthony Liguori

This also includes a qtest wrapper script to make it easier to launch qtest tests directly.

---
 scripts/qtest    |    5 +
 tests/Makefile   |    2 +
 tests/libqtest.c |  334 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/libqtest.h |   63 ++++++++++
 4 files changed, 404 insertions(+), 0 deletions(-)
 create mode 100644 scripts/qtest
 create mode 100644 tests/libqtest.c
 create mode 100644 tests/libqtest.h
scripts/qtest new file +5 -0
new file mode 100644
index 0000000..5cff3d4
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+export QTEST_QEMU_BINARY=$1
+shift
+eval "$@"
tests/Makefile +2 -0
index 74b29dc..3c554f0 100644
@@ -42,6 +42,8 @@ test-qmp-input-visitor: test-qmp-input-visitor.o $(qobject-obj-y) $(qapi-obj-y)
 test-qmp-commands.o: $(addprefix $(qapi-dir)/, test-qapi-types.c test-qapi-types.h test-qapi-visit.c test-qapi-visit.h test-qmp-marshal.c test-qmp-commands.h) $(qapi-obj-y)
 test-qmp-commands: test-qmp-commands.o $(qobject-obj-y) $(qapi-obj-y) $(tools-obj-y) $(qapi-dir)/test-qapi-visit.o $(qapi-dir)/test-qapi-types.o $(qapi-dir)/test-qmp-marshal.o module.o
 
+tests/rtc-test: tests/rtc-test.o tests/libqtest.o
+
 .PHONY: check
 check: $(CHECKS)
 	$(call quiet-command, gtester $(CHECKS), "  CHECK")
tests/libqtest.c new file +334 -0

334 lines not on this page · raw message

tests/libqtest.h new file +63 -0

63 lines not on this page · raw message

Signature · 1 line
1.7.4.1
#10 lore raw

Re: [Qemu-devel] [PATCH 03/10] qtest: add C version of test infrastructure

scripts/qtest

Quoted 19 lines · Anthony Liguori

This also includes a qtest wrapper script to make it easier to launch qtest tests directly.

---
 scripts/qtest    |    5 +
 tests/Makefile   |    2 +
 tests/libqtest.c |  334 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/libqtest.h |   63 ++++++++++
 4 files changed, 404 insertions(+), 0 deletions(-)
 create mode 100644 scripts/qtest
 create mode 100644 tests/libqtest.c
 create mode 100644 tests/libqtest.h
scripts/qtest new file +0 -0
new file mode 100644
index 0000000..5cff3d4

The script is missing the executable bit. When trying to run 'make check' after applying this series, I get:

Running 'tests/rtc-test' with qemu-system-x86_64...
/bin/sh: line 3: /home/ehabkost/pessoal/proj/virt/qemu/scripts/qtest: Permission denied
make: *** [check-qtest-x86_64] Error 126

Signature · 1 line
Eduardo
#11 lore raw

PATCH 4/10 make: add check targets based on gtester (v2)

In reply to Anthony Liguori

This will run all tests through gtester. The main targets are:

$ make check

Which will run each unit test and:

$ make check-report.html

Which will generate a nice HTML report of the test status.

---
v1 -> v2
- fix Makefile (Paolo)

---
 scripts/gtester-cat |   32 ++++++++++++++++++++++++++++
 tests/Makefile      |   58 ++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 87 insertions(+), 3 deletions(-)
 create mode 100644 scripts/gtester-cat
scripts/gtester-cat new file +32 -0
new file mode 100644
index 0000000..afd8c3e
@@ -0,0 +1,32 @@
+#!/bin/sh
+#
+# Copyright IBM, Corp. 2012
+#
+# Authors:
+#  Anthony Liguori <[email protected]>
+#
+# This work is licensed under the terms of the GNU GPLv2 or later.
+# See the COPYING file in the top-level directory.
+
+cat <<EOF
+<?xml version="1.0"?>
+<gtester>
+EOF
+
+for file in "$@"; do
+    first="yes"
+    cat $file | while read LINE; do
+	if test "$first" = "yes"; then
+	    first="no"
+	    continue
+	fi
+	if test "$LINE" = "<gtester>" -o "$LINE" = "</gtester>"; then
+	    continue
+	fi
+	echo $LINE
+    done
+done
+
+cat<<EOF
+</gtester>
+EOF
tests/Makefile +55 -3

67 lines not on this page · raw message

Signature · 1 line
1.7.4.1
#12 lore raw

PATCH 5/10 rtc: split out macros into a header file and use in test case

In reply to Anthony Liguori
---
 hw/mc146818rtc.c      |   33 --------------------------
 hw/mc146818rtc.h      |    3 +-
 hw/mc146818rtc_regs.h |   62 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 63 insertions(+), 35 deletions(-)
 create mode 100644 hw/mc146818rtc_regs.h
hw/mc146818rtc.c +0 -33
index a46fdfc..e7c080c 100644
@@ -46,39 +46,6 @@
 
 #define RTC_REINJECT_ON_ACK_COUNT 20
 
-#define RTC_SECONDS             0
-#define RTC_SECONDS_ALARM       1
-#define RTC_MINUTES             2
-#define RTC_MINUTES_ALARM       3
-#define RTC_HOURS               4
-#define RTC_HOURS_ALARM         5
-#define RTC_ALARM_DONT_CARE    0xC0
-
-#define RTC_DAY_OF_WEEK         6
-#define RTC_DAY_OF_MONTH        7
-#define RTC_MONTH               8
-#define RTC_YEAR                9
-
-#define RTC_REG_A               10
-#define RTC_REG_B               11
-#define RTC_REG_C               12
-#define RTC_REG_D               13
-
-#define REG_A_UIP 0x80
-
-#define REG_B_SET  0x80
-#define REG_B_PIE  0x40
-#define REG_B_AIE  0x20
-#define REG_B_UIE  0x10
-#define REG_B_SQWE 0x08
-#define REG_B_DM   0x04
-#define REG_B_24H  0x02
-
-#define REG_C_UF   0x10
-#define REG_C_IRQF 0x80
-#define REG_C_PF   0x40
-#define REG_C_AF   0x20
-
 typedef struct RTCState {
     ISADevice dev;
     MemoryRegion io;
hw/mc146818rtc.h +1 -2
index f119930..f286b6a 100644
@@ -2,8 +2,7 @@
 #define MC146818RTC_H
 
 #include "isa.h"
-
-#define RTC_ISA_IRQ 8
+#include "mc146818rtc_regs.h"
 
 ISADevice *rtc_init(ISABus *bus, int base_year, qemu_irq intercept_irq);
 void rtc_set_memory(ISADevice *dev, int addr, int val);
hw/mc146818rtc_regs.h new file +62 -0

62 lines not on this page · raw message

Signature · 1 line
1.7.4.1
#13 lore raw

PATCH 6/10 qtest: add rtc-test test-case (v2)

In reply to Anthony Liguori

---
v1 -> v2
- fix set_alarm_time (Paolo)

---
 tests/Makefile   |    2 +-
 tests/rtc-test.c |  267 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 268 insertions(+), 1 deletions(-)
 create mode 100644 tests/rtc-test.c
tests/Makefile +1 -1
index f41a00b..feacbf0 100644
@@ -2,7 +2,7 @@ CHECKS = check-qdict check-qfloat check-qint check-qstring check-qlist
 CHECKS += check-qjson test-qmp-output-visitor test-qmp-input-visitor
 CHECKS += test-string-input-visitor test-string-output-visitor test-coroutine
 
-HW_TESTS=
+HW_TESTS=tests/rtc-test
 
 TARGETS=$(patsubst %-softmmu,%, $(filter %-softmmu,$(TARGET_DIRS)))
 
tests/rtc-test.c new file +267 -0

267 lines not on this page · raw message

Signature · 1 line
1.7.4.1
#14 lore raw

Re: [Qemu-devel] [PATCH 06/10] qtest: add rtc-test test-case (v2)

Quoted 287 lines · Anthony Liguori

---
v1 -> v2
- fix set_alarm_time (Paolo)

---
 tests/Makefile   |    2 +-
 tests/rtc-test.c |  267 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 268 insertions(+), 1 deletions(-)
 create mode 100644 tests/rtc-test.c
tests/Makefile +1 -1
index f41a00b..feacbf0 100644
@@ -2,7 +2,7 @@ CHECKS = check-qdict check-qfloat check-qint check-qstring check-qlist
 CHECKS += check-qjson test-qmp-output-visitor test-qmp-input-visitor
 CHECKS += test-string-input-visitor test-string-output-visitor test-coroutine
 
-HW_TESTS=
+HW_TESTS=tests/rtc-test
 
 TARGETS=$(patsubst %-softmmu,%, $(filter %-softmmu,$(TARGET_DIRS)))
 
tests/rtc-test.c new file +257 -0
new file mode 100644
index 0000000..1645b34
@@ -0,0 +1,267 @@
+/*
+ * QTest
+ *
+ * Copyright IBM, Corp. 2012
+ *
+ * Authors:
+ *  Anthony Liguori   <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+#include "libqtest.h"
+#include "hw/mc146818rtc_regs.h"
+
+#include <glib.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+static uint8_t base = 0x70;
+
+static int bcd2dec(int value)
+{
+    return (((value >> 4) & 0x0F) * 10) + (value & 0x0F);
+}
+
+static int dec2bcd(int value)
+{
+    return ((value / 10) << 4) | (value % 10);
+}
+
+static uint8_t cmos_read(uint8_t reg)
+{
+    outb(base + 0, reg);
+    return inb(base + 1);
+}
+
+static void cmos_write(uint8_t reg, uint8_t val)
+{
+    outb(base + 0, reg);
+    outb(base + 1, val);
+}
+
+static int tm_cmp(struct tm *lhs, struct tm *rhs)
+{
+    time_t a, b;
+    struct tm d1, d2;
+
+    memcpy(&d1, lhs, sizeof(d1));
+    memcpy(&d2, rhs, sizeof(d2));
+
+    a = mktime(&d1);
+    b = mktime(&d2);
+
+    if (a < b) {
+        return -1;
+    } else if (a > b) {
+        return 1;
+    }
+
+    return 0;
+}
+
+#if 0
+static void print_tm(struct tm *tm)
+{
+    printf("%04d-%02d-%02d %02d:%02d:%02d\n",
+           tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, 
+           tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_gmtoff);
+}
+#endif
+
+static void cmos_get_date_time(struct tm *date)
+{
+    int base_year = 2000, hour_offset;
+    int sec, min, hour, mday, mon, year;
+    time_t ts;
+    struct tm dummy;
+
+    sec = cmos_read(RTC_SECONDS);
+    min = cmos_read(RTC_MINUTES);
+    hour = cmos_read(RTC_HOURS);
+    mday = cmos_read(RTC_DAY_OF_MONTH);
+    mon = cmos_read(RTC_MONTH);
+    year = cmos_read(RTC_YEAR);
+
+    if ((cmos_read(RTC_REG_B) & REG_B_DM) == 0) {
+        sec = bcd2dec(sec);
+        min = bcd2dec(min);
+        hour = bcd2dec(hour);
+        mday = bcd2dec(mday);
+        mon = bcd2dec(mon);
+        year = bcd2dec(year);
+        hour_offset = 80;
+    } else {
+        hour_offset = 0x80;
+    }
+
+    if ((cmos_read(0x0B) & REG_B_24H) == 0) {
+        if (hour >= hour_offset) {
+            hour -= hour_offset;
+            hour += 12;
+        }
+    }
+
+    ts = time(NULL);
+    localtime_r(&ts, &dummy);
+
+    date->tm_isdst = dummy.tm_isdst;
+    date->tm_sec = sec;
+    date->tm_min = min;
+    date->tm_hour = hour;
+    date->tm_mday = mday;
+    date->tm_mon = mon - 1;
+    date->tm_year = base_year + year - 1900;
+    date->tm_gmtoff = 0;
+
+    ts = mktime(date);
+}
+
+static void check_time(int wiggle)
+{
+    struct tm start, date[4], end;
+    struct tm *datep;
+    time_t ts;
+
+    /*
+     * This check assumes a few things.  First, we cannot guarantee that we get
+     * a consistent reading from the wall clock because we may hit an edge of
+     * the clock while reading.  To work around this, we read four clock readings
+     * such that at least two of them should match.  We need to assume that one
+     * reading is corrupt so we need four readings to ensure that we have at
+     * least two consecutive identical readings
+     *
+     * It's also possible that we'll cross an edge reading the host clock so
+     * simply check to make sure that the clock reading is within the period of
+     * when we expect it to be.
+     */
+
+    ts = time(NULL);
+    gmtime_r(&ts, &start);
+
+    cmos_get_date_time(&date[0]);
+    cmos_get_date_time(&date[1]);
+    cmos_get_date_time(&date[2]);
+    cmos_get_date_time(&date[3]);
+
+    ts = time(NULL);
+    gmtime_r(&ts, &end);
+
+    if (tm_cmp(&date[0], &date[1]) == 0) {
+        datep = &date[0];
+    } else if (tm_cmp(&date[1], &date[2]) == 0) {
+        datep = &date[1];
+    } else if (tm_cmp(&date[2], &date[3]) == 0) {
+        datep = &date[2];
+    } else {
+        g_assert_not_reached();
+    }
+
+    if (!(tm_cmp(&start, datep) <= 0 && tm_cmp(datep, &end) <= 0)) {
+        time_t t, s;
+
+        start.tm_isdst = datep->tm_isdst;
+
+        t = mktime(datep);
+        s = mktime(&start);
+        if (t < s) {
+            g_test_message("RTC is %ld second(s) behind wall-clock\n", (s - t));
+        } else {
+            g_test_message("RTC is %ld second(s) ahead of wall-clock\n", (t - s));
+        }
+
+        g_assert_cmpint(ABS(t - s), <=, wiggle);
+    }
+}
+
+static int wiggle = 2;
+
+static void bcd_check_time(void)
+{
+    /* Set BCD mode */
+    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) & ~REG_B_DM);
+    check_time(wiggle);
+}
+
+static void dec_check_time(void)
+{
+    /* Set DEC mode */
+    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_DM);
+    check_time(wiggle);
+}
+
+static void set_alarm_time(struct tm *tm)
+{
+    int sec;
+
+    sec = tm->tm_sec;
+
+    if ((cmos_read(RTC_REG_B) & REG_B_DM) == 0) {
+        sec = dec2bcd(sec);
+    }
+
+    cmos_write(RTC_SECONDS_ALARM, sec);
+    cmos_write(RTC_MINUTES_ALARM, RTC_ALARM_DONT_CARE);
+    cmos_write(RTC_HOURS_ALARM, RTC_ALARM_DONT_CARE);
+}
+
+static void alarm_time(void)
+{
+    struct tm now;
+    time_t ts;
+    int i;
+
+    ts = time(NULL);
+    gmtime_r(&ts, &now);
+
+    /* set DEC mode */
+    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_DM);
+
+    g_assert(!get_irq(RTC_ISA_IRQ));
+
+    now.tm_sec = (now.tm_sec + 2) % 60;
+    set_alarm_time(&now);
+    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_AIE);
+
+    for (i = 0; i < 2 + wiggle; i++) {
+        if (get_irq(RTC_ISA_IRQ)) {
+            break;
+        }
+
+        sleep(1);
+    }
+
+    g_assert(get_irq(RTC_ISA_IRQ));
+}
+
+int main(int argc, char **argv)
+{
+    const char *arch;
+    QTestState *s = NULL;
+    int ret;
+
+    g_test_init(&argc, &argv, NULL);
+
+    arch = qtest_get_arch();
+    /* These tests only work on i386 and x86_64 */
+    if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
+        s = qtest_start("-vnc none");
+
+        qtest_add_func("/rtc/bcd/check-time", bcd_check_time);
+        qtest_add_func("/rtc/dec/check-time", dec_check_time);
+        qtest_add_func("/rtc/alarm-time", alarm_time);
+    } else {
+        g_test_message("Skipping unsupported arch `%s'\n", arch);

tests/rtc-test.c line 267

Quoted 10 lines
tests/rtc-test.c new file +10 -0
+    }
+
+    ret = g_test_run();
+
+    if (s) {
+        qtest_quit(s);
+    }
+
+    return ret;
+}

Please squash this into patch 9 (i.e. move it after 7 and 8).

Paolo

#15 lore raw

Re: [Qemu-devel] [PATCH 06/10] qtest: add rtc-test test-case (v2)

In reply to Anthony Liguori

Am 25.02.2012 20:42, schrieb Anthony Liguori:

Quoted 21 lines

---
v1 -> v2
- fix set_alarm_time (Paolo)
---
tests/Makefile | 2 +-
tests/rtc-test.c | 267
++++++++++++++++++++++++++++++++++++++++++++++++++++++

2 files changed, 268 insertions(+), 1 deletions(-)

create mode 100644 tests/rtc-test.c

tests/Makefile +0 -0
index f41a00b..feacbf0 100644
@@ -2,7 +2,7 @@ CHECKS = check-qdict check-qfloat check-qint 

check-qstring check-qlist
CHECKS += check-qjson test-qmp-output-visitor test-qmp-input-visitor CHECKS += test-string-input-visitor test-string-output-visitor test-coroutine

tests/Makefile CHECKS = check-qdict check-qfloat check-qint check-qstring check-qlist · line 5

Quoted 2 lines
tests/Makefile +1 -1
-HW_TESTS=
+HW_TESTS=tests/rtc-test

Would you mind adding $(EXESUF) to all new executables? And could you please apply my patch which adds $(EXESUF) to the existing test executables? Otherwise I'd have to rebase it again. See
http://lists.nongnu.org/archive/html/qemu-devel/2012-02/msg03096.html

Running the tests on w32 hosts is required because there are w32 specific implementations for coroutines, timers, ... whichneed tests, too.

Thanks,

Stefan Weil

#16 lore raw

PATCH 7/10 qtest: IRQ interception infrastructure (v2)

In reply to Anthony Liguori

From: Paolo Bonzini <[email protected]>

---
v1 -> v2
- rebased to latest (aliguori)

---
 hw/irq.c     |   17 ++++++++++
 hw/irq.h     |    5 +++
 hw/pc_piix.c |    9 +++--
 qtest.c      |   97 ++++++++++++++++++++++++++++++++++++++++-----------------
 qtest.h      |    2 -
 5 files changed, 95 insertions(+), 35 deletions(-)
hw/irq.c +17 -0
index 62f766e..d413a0b 100644
@@ -104,3 +104,20 @@ qemu_irq *qemu_irq_proxy(qemu_irq **target, int n)
 {
     return qemu_allocate_irqs(proxy_irq_handler, target, n);
 }
+
+void qemu_irq_intercept_in(qemu_irq *gpio_in, qemu_irq_handler handler, int n)
+{
+    int i;
+    qemu_irq *old_irqs = qemu_allocate_irqs(NULL, NULL, n);
+    for (i = 0; i < n; i++) {
+        *old_irqs[i] = *gpio_in[i];
+        gpio_in[i]->handler = handler;
+        gpio_in[i]->opaque = old_irqs;
+    }
+}
+
+void qemu_irq_intercept_out(qemu_irq **gpio_out, qemu_irq_handler handler, int n)
+{
+    qemu_irq *old_irqs = *gpio_out;
+    *gpio_out = qemu_allocate_irqs(handler, old_irqs, n);
+}
hw/irq.h +5 -0
index 64da2fd..56c55f0 100644
@@ -38,4 +38,9 @@ qemu_irq qemu_irq_split(qemu_irq irq1, qemu_irq irq2);
  */
 qemu_irq *qemu_irq_proxy(qemu_irq **target, int n);
 
+/* For internal use in qtest.  Similar to qemu_irq_split, but operating
+   on an existing vector of qemu_irq.  */
+void qemu_irq_intercept_in(qemu_irq *gpio_in, qemu_irq_handler handler, int n);
+void qemu_irq_intercept_out(qemu_irq **gpio_out, qemu_irq_handler handler, int n);
+
 #endif
hw/pc_piix.c +5 -4
index 2c0881e..36c06d5 100644
@@ -99,7 +99,7 @@ static void kvm_piix3_gsi_handler(void *opaque, int n, int level)
     }
 }
 
-static void ioapic_init(GSIState *gsi_state)
+static DeviceState *ioapic_init(GSIState *gsi_state)
 {
     DeviceState *dev;
     SysBusDevice *d;
@@ -117,6 +117,7 @@ static void ioapic_init(GSIState *gsi_state)
     for (i = 0; i < IOAPIC_NUM_PINS; i++) {
         gsi_state->ioapic_irq[i] = qdev_get_gpio_in(dev, i);
     }
+    return dev;
 }
 
 /* PC hardware initialisation */
@@ -213,8 +214,6 @@ static void pc_init1(MemoryRegion *system_memory,
         i8259 = kvm_i8259_init(isa_bus);
     } else if (xen_enabled()) {
         i8259 = xen_interrupt_controller_init();
-    } else if (qtest_enabled()) {
-        i8259 = qtest_interrupt_controller_init();
     } else {
         cpu_irq = pc_allocate_cpu_irq();
         i8259 = i8259_init(isa_bus, cpu_irq[0]);
@@ -224,7 +223,9 @@ static void pc_init1(MemoryRegion *system_memory,
         gsi_state->i8259_irq[i] = i8259[i];
     }
     if (pci_enabled) {
-        ioapic_init(gsi_state);
+        dev = ioapic_init(gsi_state);
+        object_property_add_child(object_resolve_path("/i440fx/piix3", NULL),
+                                  "ioapic", OBJECT(dev), NULL);
     }
 
     pc_register_ferr_irq(gsi[13]);
qtest.c +68 -29

139 lines not on this page · raw message

qtest.h +0 -2
index f0e1377..1478343 100644
@@ -32,6 +32,4 @@ static inline int qtest_available(void)
 
 int qtest_init(void);
 
-qemu_irq *qtest_interrupt_controller_init(void);
-
 #endif
Signature · 1 line
1.7.4.1
#17 lore raw

Re: [Qemu-devel] [PATCH 07/10] qtest: IRQ interception infrastructure (v2)

hw/pc_piix.c static void pc_init1(MemoryRegion *system_memory, · line 231

Quoted 11 lines · Anthony Liguori
hw/pc_piix.c +3 -1
@@ -224,7 +223,9 @@ static void pc_init1(MemoryRegion *system_memory,
         gsi_state->i8259_irq[i] = i8259[i];
     }
     if (pci_enabled) {
-        ioapic_init(gsi_state);
+        dev = ioapic_init(gsi_state);
+        object_property_add_child(object_resolve_path("/i440fx/piix3", NULL),
+                                  "ioapic", OBJECT(dev), NULL);
     }
 
     pc_register_ferr_irq(gsi[13]);

Jan objected to putting this under /i440fx/piix3.

Paolo

#18 lore raw

Re: [Qemu-devel] [PATCH 07/10] qtest: IRQ interception infrastructure (v2)

Quoted 11 lines · Paolo Bonzini
Quoted 10 lines · Anthony Liguori
hw/pc_piix.c +3 -1
@@ -224,7 +223,9 @@ static void pc_init1(MemoryRegion *system_memory,
          gsi_state->i8259_irq[i] = i8259[i];
      }
      if (pci_enabled) {
-        ioapic_init(gsi_state);
+        dev = ioapic_init(gsi_state);
+        object_property_add_child(object_resolve_path("/i440fx/piix3", NULL),
+                                  "ioapic", OBJECT(dev), NULL);
      }
 
Quoted 3 lines

hw/pc_piix.c static void pc_init1(MemoryRegion *system_memory, · line 231

Quoted 1 line
hw/pc_piix.c +0 -0
      pc_register_ferr_irq(gsi[13]);

Jan objected to putting this under /i440fx/piix3.

It's not technically part of the PIIX3 packaging, but the I/O APIC is attached to the PIIX3 and it's I/O requests propagate through the I/O APIC. See section 8.12 of the PIIX4 manual.

I think for our model, having it as a child property makes quite a lot of sense.

Regards,

Anthony Liguori

Quoted 3 lines

Paolo

#19 lore raw

Re: [Qemu-devel] [PATCH 07/10] qtest: IRQ interception infrastructure (v2)

Am 25.02.2012 22:16, schrieb Anthony Liguori:

Quoted 10 lines · Paolo Bonzini
Quoted 6 lines · Anthony Liguori
Quoted 3 lines

+ object_property_add_child(object_resolve_path("/i440fx/piix3", NULL),
+ "ioapic", OBJECT(dev), NULL);

Jan objected to putting this under /i440fx/piix3.

I think for our model, having it as a child property makes quite a lot of sense.

Weren't you discussing rearranging the tree so that devices are under a separate node from block devices etc. (e.g., /devices)? Would be a good idea to introduce a (dummy) object_resolve_device_path() to avoid having to change hardcoded absolute string paths like the above later. Or do that change first so that new paths like this one at least don't need to be touched again.

Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#20 lore raw

Re: [Qemu-devel] [PATCH 07/10] qtest: IRQ interception infrastructure (v2)

Quoted 9 lines · Andreas Färber

Am 25.02.2012 22:16, schrieb Anthony Liguori:

Quoted 8 lines · Paolo Bonzini
Quoted 6 lines · Anthony Liguori
Quoted 3 lines

+ object_property_add_child(object_resolve_path("/i440fx/piix3", NULL),
+ "ioapic", OBJECT(dev), NULL);

Jan objected to putting this under /i440fx/piix3.

Quoted 7 lines
Quoted 2 lines

I think for our model, having it as a child property makes quite a lot of sense.

Weren't you discussing rearranging the tree so that devices are under a separate node from block devices etc. (e.g., /devices)? Would be a good idea to introduce a (dummy) object_resolve_device_path() to avoid having to change hardcoded absolute string paths like the above later.

No need. object_resolve_path("i440fx/piix3", NULL) would have the same effect.

But I also don't think it's necessary. Direct calls to object_resolve_path almost always indicates the need to refactor code.

Regards,

Anthony Liguori

Quoted 5 lines

Or do that change first so that new paths like this one at least don't need to be touched again.

Andreas

#21 lore raw

PATCH 8/10 libqtest: add IRQ intercept commands

In reply to Anthony Liguori

From: Paolo Bonzini <[email protected]>

---
 tests/libqtest.c |   12 ++++++++++++
 tests/libqtest.h |    6 ++++++
 2 files changed, 18 insertions(+), 0 deletions(-)
tests/libqtest.c +12 -0
index dd07b07..1d1b06e 100644
@@ -235,6 +235,18 @@ bool qtest_get_irq(QTestState *s, int num)
     return s->irq_level[num];
 }
 
+void qtest_irq_intercept_out(QTestState *s, const char *qom_path)
+{
+    qtest_sendf(s, "irq_intercept_out %s\n", qom_path);
+    qtest_rsp(s, 0);
+}
+
+void qtest_irq_intercept_in(QTestState *s, const char *qom_path)
+{
+    qtest_sendf(s, "irq_intercept_in %s\n", qom_path);
+    qtest_rsp(s, 0);
+}
+
 static void qtest_out(QTestState *s, const char *cmd, uint16_t addr, uint32_t value)
 {
     qtest_sendf(s, "%s 0x%x 0x%x\n", cmd, addr, value);
tests/libqtest.h +6 -0
index dd82926..b5ca04e 100644
@@ -26,6 +26,10 @@ void qtest_quit(QTestState *s);
 
 bool qtest_get_irq(QTestState *s, int num);
 
+void qtest_irq_intercept_in(QTestState *s, const char *string);
+
+void qtest_irq_intercept_out(QTestState *s, const char *string);
+
 void qtest_outb(QTestState *s, uint16_t addr, uint8_t value);
 
 void qtest_outw(QTestState *s, uint16_t addr, uint16_t value);
@@ -51,6 +55,8 @@ void qtest_add_func(const char *str, void (*fn));
         )
 
 #define get_irq(num) qtest_get_irq(global_qtest, (num))
+#define irq_intercept_in(num) qtest_irq_intercept_in(global_qtest, (num))
+#define irq_intercept_out(num) qtest_irq_intercept_out(global_qtest, (num))
 #define outb(addr, val) qtest_outb(global_qtest, (addr), (val))
 #define outw(addr, val) qtest_outw(global_qtest, (addr), (val))
 #define outl(addr, val) qtest_outl(global_qtest, (addr), (val))
Signature · 1 line
1.7.4.1
#22 lore raw

PATCH 9/10 rtc-test: add IRQ intercept

In reply to Anthony Liguori

From: Paolo Bonzini <[email protected]>

---
 tests/rtc-test.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
tests/rtc-test.c +1 -0
index 1645b34..8280f45 100644
@@ -249,6 +249,7 @@ int main(int argc, char **argv)
     /* These tests only work on i386 and x86_64 */
     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
         s = qtest_start("-vnc none");
+        qtest_irq_intercept_in(s, "ioapic");
 
         qtest_add_func("/rtc/bcd/check-time", bcd_check_time);
         qtest_add_func("/rtc/dec/check-time", dec_check_time);
Signature · 1 line
1.7.4.1
#23 lore raw

PATCH 10/10 qtest: add clock management

In reply to Anthony Liguori

From: Paolo Bonzini <[email protected]>

This patch combines qtest and -icount together to turn the vm_clock into a source that can be fully managed by the client. To this end new commands clock_step and clock_set are added. Hooking them with libqtest is left as an exercise to the reader.

---
 cpus.c       |   20 ++++++++++++++++++++
 cpus.h       |    2 ++
 qemu-timer.c |    2 +-
 qemu-timer.h |    1 +
 qtest.c      |   45 +++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 69 insertions(+), 1 deletions(-)
cpus.c +20 -0
index c77e649..875984a 100644
@@ -34,6 +34,7 @@
 
 #include "qemu-thread.h"
 #include "cpus.h"
+#include "qtest.h"
 #include "main-loop.h"
 
 #ifndef _WIN32
@@ -238,6 +239,20 @@ static void icount_warp_rt(void *opaque)
     vm_clock_warp_start = -1;
 }
 
+void qtest_clock_warp(int64_t dest)
+{
+    int64_t clock = qemu_get_clock_ns(vm_clock);
+    assert(qtest_enabled());
+    while (clock < dest) {
+        int64_t deadline = qemu_clock_deadline(vm_clock);
+        int64_t warp = MIN(dest - clock, deadline);
+        qemu_icount_bias += warp;
+        qemu_run_timers(vm_clock);
+        clock = qemu_get_clock_ns(vm_clock);
+    }
+    qemu_notify_event();
+}
+
 void qemu_clock_warp(QEMUClock *clock)
 {
     int64_t deadline;
@@ -264,6 +279,11 @@ void qemu_clock_warp(QEMUClock *clock)
         return;
     }
 
+    if (qtest_enabled()) {
+        /* When testing, qtest commands advance icount.  */
+	return;
+    }
+
     vm_clock_warp_start = qemu_get_clock_ns(rt_clock);
     deadline = qemu_clock_deadline(vm_clock);
     if (deadline > 0) {
cpus.h +2 -0
index 4ea2fe2..81bd817 100644
@@ -11,6 +11,8 @@ void cpu_synchronize_all_states(void);
 void cpu_synchronize_all_post_reset(void);
 void cpu_synchronize_all_post_init(void);
 
+void qtest_clock_warp(int64_t dest);
+
 /* vl.c */
 extern int smp_cores;
 extern int smp_threads;
qemu-timer.c +1 -1
index d7f56e5..80bcc56 100644
@@ -397,7 +397,7 @@ int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
     return qemu_timer_expired_ns(timer_head, current_time * timer_head->scale);
 }
 
-static void qemu_run_timers(QEMUClock *clock)
+void qemu_run_timers(QEMUClock *clock)
 {
     QEMUTimer **ptimer_head, *ts;
     int64_t current_time;
qemu-timer.h +1 -0
index de17f3b..661bbe7 100644
@@ -59,6 +59,7 @@ int qemu_timer_pending(QEMUTimer *ts);
 int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time);
 uint64_t qemu_timer_expire_time_ns(QEMUTimer *ts);
 
+void qemu_run_timers(QEMUClock *clock);
 void qemu_run_all_timers(void);
 int qemu_alarm_pending(void);
 void configure_alarms(char const *opt);
qtest.c +45 -0

69 lines not on this page · raw message

Signature · 1 line
1.7.4.1