* [Qemu-devel] [PATCH 0/9] qtest: a testing framework for devices (v3)
@ 2012-03-15 13:37 Anthony Liguori
2012-03-15 13:37 ` [Qemu-devel] [PATCH 1/9] w32: Support tests (make check) Anthony Liguori
` (8 more replies)
0 siblings, 9 replies; 15+ messages in thread
From: Anthony Liguori @ 2012-03-15 13:37 UTC (permalink / raw)
To: qemu-devel
Hi,
This is a combination of Paolo's qtest series and my original qtest series.
qtest provides a way to write tests against specific devices using the host
environment to interact with guests.
This also includes a patch from Stefan Weil that he asked me to include in this
series.
^ permalink raw reply [flat|nested] 15+ messages in thread
* [Qemu-devel] [PATCH 1/9] w32: Support tests (make check)
2012-03-15 13:37 [Qemu-devel] [PATCH 0/9] qtest: a testing framework for devices (v3) Anthony Liguori
@ 2012-03-15 13:37 ` Anthony Liguori
2012-03-15 13:37 ` [Qemu-devel] [PATCH 2/9] qtest: add test framework (v3) Anthony Liguori
` (7 subsequent siblings)
8 siblings, 0 replies; 15+ messages in thread
From: Anthony Liguori @ 2012-03-15 13:37 UTC (permalink / raw)
To: qemu-devel; +Cc: Stefan Weil, Anthony Liguori
From: Stefan Weil <sw@weilnetz.de>
Adding $(EXESUF) is needed to make those tests work on w32 hosts, too.
v2:
Rebased, added new tests, tests sorted alphabetically.
Signed-off-by: Stefan Weil <sw@weilnetz.de>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
---
tests/Makefile | 38 +++++++++++++++++++++++---------------
1 files changed, 23 insertions(+), 15 deletions(-)
diff --git a/tests/Makefile b/tests/Makefile
index c78ade1..4fd09f2 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -1,19 +1,27 @@
export SRC_PATH
-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
+CHECKS = check-qdict$(EXESUF)
+CHECKS += check-qfloat$(EXESUF)
+CHECKS += check-qint$(EXESUF)
+CHECKS += check-qjson$(EXESUF)
+CHECKS += check-qlist$(EXESUF)
+CHECKS += check-qstring$(EXESUF)
+CHECKS += test-coroutine$(EXESUF)
+CHECKS += test-qmp-input-visitor$(EXESUF)
+CHECKS += test-qmp-output-visitor$(EXESUF)
+CHECKS += test-string-input-visitor$(EXESUF)
+CHECKS += test-string-output-visitor$(EXESUF)
CHECKS += $(SRC_PATH)/tests/qemu-iotests-quick.sh
check-qint.o check-qstring.o check-qdict.o check-qlist.o check-qfloat.o check-qjson.o test-coroutine.o: $(GENERATED_HEADERS)
-check-qint: check-qint.o qint.o $(tools-obj-y)
-check-qstring: check-qstring.o qstring.o $(tools-obj-y)
-check-qdict: check-qdict.o qdict.o qfloat.o qint.o qstring.o qbool.o qlist.o $(tools-obj-y)
-check-qlist: check-qlist.o qlist.o qint.o $(tools-obj-y)
-check-qfloat: check-qfloat.o qfloat.o $(tools-obj-y)
-check-qjson: check-qjson.o $(qobject-obj-y) $(tools-obj-y)
-test-coroutine: test-coroutine.o qemu-timer-common.o async.o $(coroutine-obj-y) $(tools-obj-y)
+check-qint$(EXESUF): check-qint.o qint.o $(tools-obj-y)
+check-qstring$(EXESUF): check-qstring.o qstring.o $(tools-obj-y)
+check-qdict$(EXESUF): check-qdict.o qdict.o qfloat.o qint.o qstring.o qbool.o qlist.o $(tools-obj-y)
+check-qlist$(EXESUF): check-qlist.o qlist.o qint.o $(tools-obj-y)
+check-qfloat$(EXESUF): check-qfloat.o qfloat.o $(tools-obj-y)
+check-qjson$(EXESUF): check-qjson.o $(qobject-obj-y) $(tools-obj-y)
+test-coroutine$(EXESUF): test-coroutine.o qemu-timer-common.o async.o $(coroutine-obj-y) $(tools-obj-y)
test-qmp-input-visitor.o test-qmp-output-visitor.o \
test-string-input-visitor.o test-string-output-visitor.o \
@@ -31,19 +39,19 @@ $(SRC_PATH)/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-commands.py
test-string-output-visitor.o: $(addprefix $(qapi-dir)/, test-qapi-types.c test-qapi-types.h test-qapi-visit.c test-qapi-visit.h) $(qapi-obj-y)
-test-string-output-visitor: test-string-output-visitor.o $(qobject-obj-y) $(qapi-obj-y) $(tools-obj-y) $(qapi-dir)/test-qapi-visit.o $(qapi-dir)/test-qapi-types.o
+test-string-output-visitor$(EXESUF): test-string-output-visitor.o $(qobject-obj-y) $(qapi-obj-y) $(tools-obj-y) $(qapi-dir)/test-qapi-visit.o $(qapi-dir)/test-qapi-types.o
test-string-input-visitor.o: $(addprefix $(qapi-dir)/, test-qapi-types.c test-qapi-types.h test-qapi-visit.c test-qapi-visit.h) $(qapi-obj-y)
-test-string-input-visitor: test-string-input-visitor.o $(qobject-obj-y) $(qapi-obj-y) $(tools-obj-y) $(qapi-dir)/test-qapi-visit.o $(qapi-dir)/test-qapi-types.o
+test-string-input-visitor$(EXESUF): test-string-input-visitor.o $(qobject-obj-y) $(qapi-obj-y) $(tools-obj-y) $(qapi-dir)/test-qapi-visit.o $(qapi-dir)/test-qapi-types.o
test-qmp-output-visitor.o: $(addprefix $(qapi-dir)/, test-qapi-types.c test-qapi-types.h test-qapi-visit.c test-qapi-visit.h) $(qapi-obj-y)
-test-qmp-output-visitor: test-qmp-output-visitor.o $(qobject-obj-y) $(qapi-obj-y) $(tools-obj-y) $(qapi-dir)/test-qapi-visit.o $(qapi-dir)/test-qapi-types.o
+test-qmp-output-visitor$(EXESUF): test-qmp-output-visitor.o $(qobject-obj-y) $(qapi-obj-y) $(tools-obj-y) $(qapi-dir)/test-qapi-visit.o $(qapi-dir)/test-qapi-types.o
test-qmp-input-visitor.o: $(addprefix $(qapi-dir)/, test-qapi-types.c test-qapi-types.h test-qapi-visit.c test-qapi-visit.h) $(qapi-obj-y)
-test-qmp-input-visitor: test-qmp-input-visitor.o $(qobject-obj-y) $(qapi-obj-y) $(tools-obj-y) $(qapi-dir)/test-qapi-visit.o $(qapi-dir)/test-qapi-types.o
+test-qmp-input-visitor$(EXESUF): test-qmp-input-visitor.o $(qobject-obj-y) $(qapi-obj-y) $(tools-obj-y) $(qapi-dir)/test-qapi-visit.o $(qapi-dir)/test-qapi-types.o
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
+test-qmp-commands$(EXESUF): 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
$(SRC_PATH)/tests/qemu-iotests-quick.sh: qemu-img qemu-io
--
1.7.5.4
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [Qemu-devel] [PATCH 2/9] qtest: add test framework (v3)
2012-03-15 13:37 [Qemu-devel] [PATCH 0/9] qtest: a testing framework for devices (v3) Anthony Liguori
2012-03-15 13:37 ` [Qemu-devel] [PATCH 1/9] w32: Support tests (make check) Anthony Liguori
@ 2012-03-15 13:37 ` Anthony Liguori
2012-03-15 13:37 ` [Qemu-devel] [PATCH 3/9] qtest: add C version of test infrastructure Anthony Liguori
` (6 subsequent siblings)
8 siblings, 0 replies; 15+ messages in thread
From: Anthony Liguori @ 2012-03-15 13:37 UTC (permalink / raw)
To: qemu-devel; +Cc: Paolo Bonzini, Anthony Liguori
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.
qtest 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.
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
v1 -> v2
- always send a response (Paolo)
- enable echo (Paolo)
- do not use TCG CPU threads (Paolo)
v2 -> v3
- no need for synthetic interupt controller (Paolo)
---
Makefile.objs | 2 +
cpu-exec.c | 1 +
cpus.c | 62 +++++++++-
qemu-options.hx | 8 ++
qtest.c | 354 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
qtest.h | 35 ++++++
vl.c | 8 ++
7 files changed, 467 insertions(+), 3 deletions(-)
create mode 100644 qtest.c
create mode 100644 qtest.h
diff --git a/Makefile.objs b/Makefile.objs
index 226b01d..e9842b0 100644
--- a/Makefile.objs
+++ b/Makefile.objs
@@ -322,6 +322,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
diff --git a/cpu-exec.c b/cpu-exec.c
index bd5791f..d8354b8 100644
--- a/cpu-exec.c
+++ b/cpu-exec.c
@@ -21,6 +21,7 @@
#include "disas.h"
#include "tcg.h"
#include "qemu-barrier.h"
+#include "qtest.h"
int tb_invalidated_flag;
diff --git a/cpus.c b/cpus.c
index 25ba621..010047e 100644
--- a/cpus.c
+++ b/cpus.c
@@ -741,6 +741,48 @@ static void *qemu_kvm_cpu_thread_fn(void *arg)
return NULL;
}
+static void *qemu_dummy_cpu_thread_fn(void *arg)
+{
+#ifdef _WIN32
+ fprintf(stderr, "qtest is not supported under Windows\n");
+ exit(1);
+#else
+ CPUArchState *env = arg;
+ sigset_t waitset;
+ int r;
+
+ qemu_mutex_lock_iothread();
+ qemu_thread_get_self(env->thread);
+ env->thread_id = qemu_get_thread_id();
+
+ sigemptyset(&waitset);
+ sigaddset(&waitset, SIG_IPI);
+
+ /* signal CPU creation */
+ env->created = 1;
+ qemu_cond_signal(&qemu_cpu_cond);
+
+ cpu_single_env = env;
+ while (1) {
+ cpu_single_env = NULL;
+ qemu_mutex_unlock_iothread();
+ do {
+ int sig;
+ r = sigwait(&waitset, &sig);
+ } while (r == -1 && (errno == EAGAIN || errno == EINTR));
+ if (r == -1) {
+ perror("sigwait");
+ exit(1);
+ }
+ qemu_mutex_lock_iothread();
+ cpu_single_env = env;
+ qemu_wait_io_event_common(env);
+ }
+
+ return NULL;
+#endif
+}
+
static void tcg_exec_all(void);
static void *qemu_tcg_cpu_thread_fn(void *arg)
@@ -803,7 +845,7 @@ void qemu_cpu_kick(void *_env)
CPUArchState *env = _env;
qemu_cond_broadcast(env->halt_cond);
- if (kvm_enabled() && !env->thread_kicked) {
+ if (!tcg_enabled() && !env->thread_kicked) {
qemu_cpu_kick_thread(env);
env->thread_kicked = true;
}
@@ -832,7 +874,7 @@ int qemu_cpu_is_self(void *_env)
void qemu_mutex_lock_iothread(void)
{
- if (kvm_enabled()) {
+ if (!tcg_enabled()) {
qemu_mutex_lock(&qemu_global_mutex);
} else {
iothread_requesting_mutex = true;
@@ -947,6 +989,18 @@ static void qemu_kvm_start_vcpu(CPUArchState *env)
}
}
+static void qemu_dummy_start_vcpu(CPUArchState *env)
+{
+ env->thread = g_malloc0(sizeof(QemuThread));
+ env->halt_cond = g_malloc0(sizeof(QemuCond));
+ qemu_cond_init(env->halt_cond);
+ qemu_thread_create(env->thread, qemu_dummy_cpu_thread_fn, env,
+ QEMU_THREAD_JOINABLE);
+ while (env->created == 0) {
+ qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
+ }
+}
+
void qemu_init_vcpu(void *_env)
{
CPUArchState *env = _env;
@@ -956,8 +1010,10 @@ void qemu_init_vcpu(void *_env)
env->stopped = 1;
if (kvm_enabled()) {
qemu_kvm_start_vcpu(env);
- } else {
+ } else if (tcg_enabled()) {
qemu_tcg_init_vcpu(env);
+ } else {
+ qemu_dummy_start_vcpu(env);
}
}
diff --git a/qemu-options.hx b/qemu-options.hx
index daefce3..dae3407 100644
--- a/qemu-options.hx
+++ b/qemu-options.hx
@@ -2707,6 +2707,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
diff --git a/qtest.c b/qtest.c
new file mode 100644
index 0000000..46ebda1
--- /dev/null
+++ b/qtest.c
@@ -0,0 +1,354 @@
+/*
+ * Test Server
+ *
+ * Copyright IBM, Corp. 2011
+ *
+ * Authors:
+ * Anthony Liguori <aliguori@us.ibm.com>
+ *
+ * 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 "qtest.h"
+#include "qemu-char.h"
+#include "ioport.h"
+#include "memory.h"
+#include "hw/irq.h"
+#include "sysemu.h"
+
+#define MAX_IRQ 256
+
+const char *qtest_chrdev;
+const char *qtest_log;
+int qtest_allowed = 0;
+
+static FILE *qtest_log_fp;
+static CharDriverState *qtest_chr;
+static GString *inbuf;
+static int irq_levels[MAX_IRQ];
+static struct timeval start_time;
+static bool qtest_opened;
+
+#define FMT_timeval "%" PRId64 ".%06" PRId64
+
+/**
+ * QTest Protocol
+ *
+ * Line based protocol, request/response based. Server can send async messages
+ * so clients should always handle many async messages before the response
+ * comes in.
+ *
+ * Valid requests
+ *
+ * > outb ADDR VALUE
+ * < OK
+ *
+ * > outw ADDR VALUE
+ * < OK
+ *
+ * > outl ADDR VALUE
+ * < OK
+ *
+ * > inb ADDR
+ * < OK VALUE
+ *
+ * > inw ADDR
+ * < OK VALUE
+ *
+ * > inl ADDR
+ * < OK VALUE
+ *
+ * > read ADDR SIZE
+ * < OK DATA
+ *
+ * > write ADDR SIZE DATA
+ * < OK
+ *
+ * Valid async messages:
+ *
+ * IRQ raise NUM
+ * IRQ lower NUM
+ *
+ * ADDR, SIZE, VALUE are all integers parsed with strtoul() with a base of 0.
+ *
+ * DATA is an arbitrarily long hex number prefixed with '0x'. If it's smaller
+ * than the expected size, the value will be zero filled at the end of the data
+ * sequence.
+ *
+ * NUM is an IRQ number.
+ */
+
+static int hex2nib(char ch)
+{
+ if (ch >= '0' && ch <= '9') {
+ return ch - '0';
+ } else if (ch >= 'a' && ch <= 'f') {
+ return 10 + (ch - 'a');
+ } else if (ch >= 'A' && ch <= 'F') {
+ return 10 + (ch - 'a');
+ } else {
+ return -1;
+ }
+}
+
+static void qtest_get_time(struct timeval *tv)
+{
+ gettimeofday(tv, NULL);
+ tv->tv_sec -= start_time.tv_sec;
+ tv->tv_usec -= start_time.tv_usec;
+ if (tv->tv_usec < 0) {
+ tv->tv_usec += 1000000;
+ tv->tv_sec -= 1;
+ }
+}
+
+static void qtest_send_prefix(CharDriverState *chr)
+{
+ struct timeval tv;
+
+ if (!qtest_log_fp || !qtest_opened) {
+ return;
+ }
+
+ qtest_get_time(&tv);
+ fprintf(qtest_log_fp, "[S +" FMT_timeval "] ",
+ tv.tv_sec, tv.tv_usec);
+}
+
+static void qtest_send(CharDriverState *chr, const char *fmt, ...)
+{
+ va_list ap;
+ char buffer[1024];
+ size_t len;
+
+ va_start(ap, fmt);
+ len = vsnprintf(buffer, sizeof(buffer), fmt, ap);
+ va_end(ap);
+
+ qemu_chr_fe_write(chr, (uint8_t *)buffer, len);
+ if (qtest_log_fp && qtest_opened) {
+ fprintf(qtest_log_fp, "%s", buffer);
+ }
+}
+
+static void qtest_process_command(CharDriverState *chr, gchar **words)
+{
+ const gchar *command;
+
+ g_assert(words);
+
+ command = words[0];
+
+ if (qtest_log_fp) {
+ struct timeval tv;
+ int i;
+
+ qtest_get_time(&tv);
+ fprintf(qtest_log_fp, "[R +" FMT_timeval "]",
+ tv.tv_sec, tv.tv_usec);
+ for (i = 0; words[i]; i++) {
+ fprintf(qtest_log_fp, " %s", words[i]);
+ }
+ fprintf(qtest_log_fp, "\n");
+ }
+
+ g_assert(command);
+ if (strcmp(words[0], "outb") == 0 ||
+ strcmp(words[0], "outw") == 0 ||
+ strcmp(words[0], "outl") == 0) {
+ uint16_t addr;
+ uint32_t value;
+
+ g_assert(words[1] && words[2]);
+ addr = strtol(words[1], NULL, 0);
+ value = strtol(words[2], NULL, 0);
+
+ if (words[0][3] == 'b') {
+ cpu_outb(addr, value);
+ } else if (words[0][3] == 'w') {
+ cpu_outw(addr, value);
+ } else if (words[0][3] == 'l') {
+ cpu_outl(addr, value);
+ }
+ qtest_send_prefix(chr);
+ qtest_send(chr, "OK\n");
+ } else if (strcmp(words[0], "inb") == 0 ||
+ strcmp(words[0], "inw") == 0 ||
+ strcmp(words[0], "inl") == 0) {
+ uint16_t addr;
+ uint32_t value = -1U;
+
+ g_assert(words[1]);
+ addr = strtol(words[1], NULL, 0);
+
+ if (words[0][2] == 'b') {
+ value = cpu_inb(addr);
+ } else if (words[0][2] == 'w') {
+ value = cpu_inw(addr);
+ } else if (words[0][2] == 'l') {
+ value = cpu_inl(addr);
+ }
+ qtest_send_prefix(chr);
+ qtest_send(chr, "OK 0x%04x\n", value);
+ } else if (strcmp(words[0], "read") == 0) {
+ uint64_t addr, len, i;
+ uint8_t *data;
+
+ g_assert(words[1] && words[2]);
+ addr = strtoul(words[1], NULL, 0);
+ len = strtoul(words[2], NULL, 0);
+
+ data = g_malloc(len);
+ cpu_physical_memory_read(addr, data, len);
+
+ qtest_send_prefix(chr);
+ qtest_send(chr, "OK 0x");
+ for (i = 0; i < len; i++) {
+ qtest_send(chr, "%02x", data[i]);
+ }
+ qtest_send(chr, "\n");
+
+ g_free(data);
+ } else if (strcmp(words[0], "write") == 0) {
+ uint64_t addr, len, i;
+ uint8_t *data;
+ size_t data_len;
+
+ g_assert(words[1] && words[2] && words[3]);
+ addr = strtoul(words[1], NULL, 0);
+ len = strtoul(words[2], NULL, 0);
+
+ data_len = strlen(words[3]);
+ if (data_len < 3) {
+ qtest_send(chr, "ERR invalid argument size\n");
+ return;
+ }
+
+ data = g_malloc(len);
+ for (i = 0; i < len; i++) {
+ if ((i * 2 + 4) <= data_len) {
+ data[i] = hex2nib(words[3][i * 2 + 2]) << 4;
+ data[i] |= hex2nib(words[3][i * 2 + 3]);
+ } else {
+ data[i] = 0;
+ }
+ }
+ cpu_physical_memory_write(addr, data, len);
+ g_free(data);
+
+ qtest_send_prefix(chr);
+ qtest_send(chr, "OK\n");
+ } else {
+ qtest_send_prefix(chr);
+ qtest_send(chr, "FAIL Unknown command `%s'\n", words[0]);
+ }
+}
+
+static void qtest_process_inbuf(CharDriverState *chr, GString *inbuf)
+{
+ char *end;
+
+ while ((end = strchr(inbuf->str, '\n')) != NULL) {
+ size_t offset;
+ GString *cmd;
+ gchar **words;
+
+ offset = end - inbuf->str;
+
+ cmd = g_string_new_len(inbuf->str, offset);
+ g_string_erase(inbuf, 0, offset + 1);
+
+ words = g_strsplit(cmd->str, " ", 0);
+ qtest_process_command(chr, words);
+ g_strfreev(words);
+
+ g_string_free(cmd, TRUE);
+ }
+}
+
+static void qtest_read(void *opaque, const uint8_t *buf, int size)
+{
+ CharDriverState *chr = opaque;
+
+ g_string_append_len(inbuf, (const gchar *)buf, size);
+ qtest_process_inbuf(chr, inbuf);
+}
+
+static int qtest_can_read(void *opaque)
+{
+ return 1024;
+}
+
+static void qtest_event(void *opaque, int event)
+{
+ int i;
+
+ switch (event) {
+ case CHR_EVENT_OPENED:
+ qemu_system_reset(false);
+ for (i = 0; i < ARRAY_SIZE(irq_levels); i++) {
+ irq_levels[i] = 0;
+ }
+ gettimeofday(&start_time, NULL);
+ qtest_opened = true;
+ if (qtest_log_fp) {
+ fprintf(qtest_log_fp, "[I " FMT_timeval "] OPENED\n",
+ start_time.tv_sec, start_time.tv_usec);
+ }
+ break;
+ case CHR_EVENT_CLOSED:
+ qtest_opened = false;
+ if (qtest_log_fp) {
+ struct timeval tv;
+ qtest_get_time(&tv);
+ fprintf(qtest_log_fp, "[I +" FMT_timeval "] CLOSED\n",
+ tv.tv_sec, tv.tv_usec);
+ }
+ break;
+ default:
+ break;
+ }
+}
+
+static void qtest_set_irq(void *opaque, int irq, int level)
+{
+ CharDriverState *chr = qtest_chr;
+ bool changed;
+
+ changed = (irq_levels[irq] != level);
+ irq_levels[irq] = level;
+
+ if (changed) {
+ qtest_send_prefix(chr);
+ qtest_send(chr, "IRQ %s %d\n",
+ level ? "raise" : "lower", irq);
+ }
+}
+
+int qtest_init(void)
+{
+ CharDriverState *chr;
+
+ g_assert(qtest_chrdev != NULL);
+
+ chr = qemu_chr_new("qtest", qtest_chrdev, NULL);
+
+ qemu_chr_add_handlers(chr, qtest_can_read, qtest_read, qtest_event, chr);
+ qemu_chr_fe_set_echo(chr, true);
+
+ inbuf = g_string_new("");
+
+ if (qtest_log) {
+ if (strcmp(qtest_log, "none") != 0) {
+ qtest_log_fp = fopen(qtest_log, "w+");
+ }
+ } else {
+ qtest_log_fp = stderr;
+ }
+
+ qtest_chr = chr;
+
+ return 0;
+}
diff --git a/qtest.h b/qtest.h
new file mode 100644
index 0000000..1478343
--- /dev/null
+++ b/qtest.h
@@ -0,0 +1,35 @@
+/*
+ * Test Server
+ *
+ * Copyright IBM, Corp. 2011
+ *
+ * Authors:
+ * Anthony Liguori <aliguori@us.ibm.com>
+ *
+ * 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);
+
+#endif
diff --git a/vl.c b/vl.c
index bd95539..30e6c3a 100644
--- a/vl.c
+++ b/vl.c
@@ -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"
@@ -2098,6 +2099,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);
}
--
1.7.5.4
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [Qemu-devel] [PATCH 3/9] qtest: add C version of test infrastructure
2012-03-15 13:37 [Qemu-devel] [PATCH 0/9] qtest: a testing framework for devices (v3) Anthony Liguori
2012-03-15 13:37 ` [Qemu-devel] [PATCH 1/9] w32: Support tests (make check) Anthony Liguori
2012-03-15 13:37 ` [Qemu-devel] [PATCH 2/9] qtest: add test framework (v3) Anthony Liguori
@ 2012-03-15 13:37 ` Anthony Liguori
2012-03-15 14:42 ` Stefan Hajnoczi
2012-03-15 13:37 ` [Qemu-devel] [PATCH 4/9] make: add check targets based on gtester (v2) Anthony Liguori
` (5 subsequent siblings)
8 siblings, 1 reply; 15+ messages in thread
From: Anthony Liguori @ 2012-03-15 13:37 UTC (permalink / raw)
To: qemu-devel; +Cc: Anthony Liguori
This also includes a qtest wrapper script to make it easier to launch qtest
tests directly.
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
---
scripts/qtest | 5 +
tests/Makefile | 1 +
tests/libqtest.c | 334 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
tests/libqtest.h | 63 ++++++++++
4 files changed, 403 insertions(+), 0 deletions(-)
create mode 100755 scripts/qtest
create mode 100644 tests/libqtest.c
create mode 100644 tests/libqtest.h
diff --git a/scripts/qtest b/scripts/qtest
new file mode 100755
index 0000000..5cff3d4
--- /dev/null
+++ b/scripts/qtest
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+export QTEST_QEMU_BINARY=$1
+shift
+eval "$@"
diff --git a/tests/Makefile b/tests/Makefile
index 4fd09f2..9d7cfb3 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -55,6 +55,7 @@ test-qmp-commands$(EXESUF): test-qmp-commands.o $(qobject-obj-y) $(qapi-obj-y) $
$(SRC_PATH)/tests/qemu-iotests-quick.sh: qemu-img qemu-io
+tests/rtc-test: tests/rtc-test.o tests/libqtest.o
.PHONY: check check-block
diff --git a/tests/libqtest.c b/tests/libqtest.c
new file mode 100644
index 0000000..dd07b07
--- /dev/null
+++ b/tests/libqtest.c
@@ -0,0 +1,334 @@
+/*
+ * QTest
+ *
+ * Copyright IBM, Corp. 2012
+ *
+ * Authors:
+ * Anthony Liguori <aliguori@us.ibm.com>
+ *
+ * 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 <glib.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/wait.h>
+#include <sys/un.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+#define MAX_IRQ 256
+
+QTestState *global_qtest;
+
+struct QTestState
+{
+ int fd;
+ bool irq_level[MAX_IRQ];
+ GString *rx;
+ gchar *pid_file;
+};
+
+#define g_assert_no_errno(ret) do { \
+ g_assert_cmpint(ret, !=, -1); \
+} while (0)
+
+QTestState *qtest_init(const char *extra_args)
+{
+ QTestState *s;
+ struct sockaddr_un addr;
+ int sock, ret, i;
+ gchar *socket_path;
+ gchar *pid_file;
+ gchar *command;
+ const char *qemu_binary;
+ pid_t pid;
+
+ qemu_binary = getenv("QTEST_QEMU_BINARY");
+ g_assert(qemu_binary != NULL);
+
+ socket_path = g_strdup_printf("/tmp/qtest-%d.sock", getpid());
+ pid_file = g_strdup_printf("/tmp/qtest-%d.pid", getpid());
+
+ s = g_malloc(sizeof(*s));
+
+ sock = socket(PF_UNIX, SOCK_STREAM, 0);
+ g_assert_no_errno(sock);
+
+ addr.sun_family = AF_UNIX;
+ snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", socket_path);
+
+ pid = fork();
+ if (pid == 0) {
+ command = g_strdup_printf("%s "
+ "-qtest unix:%s,server,nowait "
+ "-qtest-log /dev/null "
+ "-pidfile %s "
+ "-machine accel=qtest "
+ "%s", qemu_binary, socket_path,
+ pid_file,
+ extra_args ?: "");
+
+ ret = system(command);
+ exit(ret);
+ g_free(command);
+ }
+
+ do {
+ sleep(1);
+ ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
+ } while (ret == -1);
+ g_assert_no_errno(ret);
+
+ s->fd = sock;
+ s->rx = g_string_new("");
+ s->pid_file = pid_file;
+ for (i = 0; i < MAX_IRQ; i++) {
+ s->irq_level[i] = false;
+ }
+
+ g_free(socket_path);
+
+ return s;
+}
+
+void qtest_quit(QTestState *s)
+{
+ FILE *f;
+ char buffer[1024];
+
+ f = fopen(s->pid_file, "r");
+ if (f) {
+ if (fgets(buffer, sizeof(buffer), f)) {
+ pid_t pid = atoi(buffer);
+ int status = 0;
+
+ kill(pid, SIGTERM);
+ waitpid(pid, &status, 0);
+ }
+
+ fclose(f);
+ }
+}
+
+static void qtest_sendf(QTestState *s, const char *fmt, ...)
+{
+ va_list ap;
+ gchar *str;
+ size_t size, offset;
+
+ va_start(ap, fmt);
+ str = g_strdup_vprintf(fmt, ap);
+ va_end(ap);
+ size = strlen(str);
+
+ offset = 0;
+ while (offset < size) {
+ ssize_t len;
+
+ len = write(s->fd, str + offset, size - offset);
+ if (len == -1 && errno == EINTR) {
+ continue;
+ }
+
+ g_assert_no_errno(len);
+ g_assert_cmpint(len, >, 0);
+
+ offset += len;
+ }
+}
+
+static GString *qtest_recv_line(QTestState *s)
+{
+ GString *line;
+ size_t offset;
+ char *eol;
+
+ while ((eol = strchr(s->rx->str, '\n')) == NULL) {
+ ssize_t len;
+ char buffer[1024];
+
+ len = read(s->fd, buffer, sizeof(buffer));
+ if (len == -1 && errno == EINTR) {
+ continue;
+ }
+
+ if (len == -1 || len == 0) {
+ fprintf(stderr, "Broken pipe\n");
+ exit(1);
+ }
+
+ g_string_append_len(s->rx, buffer, len);
+ }
+
+ offset = eol - s->rx->str;
+ line = g_string_new_len(s->rx->str, offset);
+ g_string_erase(s->rx, 0, offset + 1);
+
+ return line;
+}
+
+static gchar **qtest_rsp(QTestState *s, int expected_args)
+{
+ GString *line;
+ gchar **words;
+ int i;
+
+redo:
+ line = qtest_recv_line(s);
+ words = g_strsplit(line->str, " ", 0);
+ g_string_free(line, TRUE);
+
+ if (strcmp(words[0], "IRQ") == 0) {
+ int irq;
+
+ g_assert(words[1] != NULL);
+ g_assert(words[2] != NULL);
+
+ irq = strtoul(words[2], NULL, 0);
+ g_assert_cmpint(irq, >=, 0);
+ g_assert_cmpint(irq, <, MAX_IRQ);
+
+ if (strcmp(words[1], "raise") == 0) {
+ s->irq_level[irq] = true;
+ } else {
+ s->irq_level[irq] = false;
+ }
+
+ g_strfreev(words);
+ goto redo;
+ }
+
+ g_assert(words[0] != NULL);
+ g_assert_cmpstr(words[0], ==, "OK");
+
+ if (expected_args) {
+ for (i = 0; i < expected_args; i++) {
+ g_assert(words[i] != NULL);
+ }
+ } else {
+ g_strfreev(words);
+ }
+
+ return words;
+}
+
+const char *qtest_get_arch(void)
+{
+ const char *qemu = getenv("QTEST_QEMU_BINARY");
+ const char *end = strrchr(qemu, '/');
+
+ return end + strlen("/qemu-system-");
+}
+
+bool qtest_get_irq(QTestState *s, int num)
+{
+ /* dummy operation in order to make sure irq is up to date */
+ qtest_inb(s, 0);
+
+ return s->irq_level[num];
+}
+
+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);
+ qtest_rsp(s, 0);
+}
+
+void qtest_outb(QTestState *s, uint16_t addr, uint8_t value)
+{
+ qtest_out(s, "outb", addr, value);
+}
+
+void qtest_outw(QTestState *s, uint16_t addr, uint16_t value)
+{
+ qtest_out(s, "outw", addr, value);
+}
+
+void qtest_outl(QTestState *s, uint16_t addr, uint32_t value)
+{
+ qtest_out(s, "outl", addr, value);
+}
+
+static uint32_t qtest_in(QTestState *s, const char *cmd, uint16_t addr)
+{
+ gchar **args;
+ uint32_t value;
+
+ qtest_sendf(s, "%s 0x%x\n", cmd, addr);
+ args = qtest_rsp(s, 2);
+ value = strtoul(args[1], NULL, 0);
+ g_strfreev(args);
+
+ return value;
+}
+
+uint8_t qtest_inb(QTestState *s, uint16_t addr)
+{
+ return qtest_in(s, "inb", addr);
+}
+
+uint16_t qtest_inw(QTestState *s, uint16_t addr)
+{
+ return qtest_in(s, "inw", addr);
+}
+
+uint32_t qtest_inl(QTestState *s, uint16_t addr)
+{
+ return qtest_in(s, "inl", addr);
+}
+
+static int hex2nib(char ch)
+{
+ if (ch >= '0' && ch <= '9') {
+ return ch - '0';
+ } else if (ch >= 'a' && ch <= 'f') {
+ return 10 + (ch - 'a');
+ } else if (ch >= 'A' && ch <= 'F') {
+ return 10 + (ch - 'a');
+ } else {
+ return -1;
+ }
+}
+
+void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size)
+{
+ uint8_t *ptr = data;
+ gchar **args;
+ size_t i;
+
+ qtest_sendf(s, "read 0x%x 0x%x\n", addr, size);
+ args = qtest_rsp(s, 2);
+
+ for (i = 0; i < size; i++) {
+ ptr[i] = hex2nib(args[1][2 + (i * 2)]) << 4;
+ ptr[i] |= hex2nib(args[1][2 + (i * 2) + 1]);
+ }
+
+ g_strfreev(args);
+}
+
+void qtest_add_func(const char *str, void (*fn))
+{
+ gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
+ g_test_add_func(path, fn);
+}
+
+void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
+{
+ const uint8_t *ptr = data;
+ size_t i;
+
+ qtest_sendf(s, "write 0x%x 0x%x 0x", addr, size);
+ for (i = 0; i < size; i++) {
+ qtest_sendf(s, "%02x", ptr[i]);
+ }
+ qtest_sendf(s, "\n");
+ qtest_rsp(s, 0);
+}
diff --git a/tests/libqtest.h b/tests/libqtest.h
new file mode 100644
index 0000000..dd82926
--- /dev/null
+++ b/tests/libqtest.h
@@ -0,0 +1,63 @@
+/*
+ * QTest
+ *
+ * Copyright IBM, Corp. 2012
+ *
+ * Authors:
+ * Anthony Liguori <aliguori@us.ibm.com>
+ *
+ * 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 LIBQTEST_H
+#define LIBQTEST_H
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <sys/types.h>
+
+typedef struct QTestState QTestState;
+
+extern QTestState *global_qtest;
+
+QTestState *qtest_init(const char *extra_args);
+void qtest_quit(QTestState *s);
+
+bool qtest_get_irq(QTestState *s, int num);
+
+void qtest_outb(QTestState *s, uint16_t addr, uint8_t value);
+
+void qtest_outw(QTestState *s, uint16_t addr, uint16_t value);
+
+void qtest_outl(QTestState *s, uint16_t addr, uint32_t value);
+
+uint8_t qtest_inb(QTestState *s, uint16_t addr);
+
+uint16_t qtest_inw(QTestState *s, uint16_t addr);
+
+uint32_t qtest_inl(QTestState *s, uint16_t addr);
+
+void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size);
+
+void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size);
+
+const char *qtest_get_arch(void);
+
+void qtest_add_func(const char *str, void (*fn));
+
+#define qtest_start(args) ( \
+ global_qtest = qtest_init((args)) \
+ )
+
+#define get_irq(num) qtest_get_irq(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))
+#define inb(addr) qtest_inb(global_qtest, (addr))
+#define inw(addr) qtest_inw(global_qtest, (addr))
+#define inl(addr) qtest_inl(global_qtest, (addr))
+#define memread(addr, data, size) qtest_memread(global_qtest, (addr), (data), (size))
+#define memwrite(addr, data, size) qtest_memwrite(global_qtest, (addr), (data), (size))
+
+#endif
--
1.7.5.4
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [Qemu-devel] [PATCH 4/9] make: add check targets based on gtester (v2)
2012-03-15 13:37 [Qemu-devel] [PATCH 0/9] qtest: a testing framework for devices (v3) Anthony Liguori
` (2 preceding siblings ...)
2012-03-15 13:37 ` [Qemu-devel] [PATCH 3/9] qtest: add C version of test infrastructure Anthony Liguori
@ 2012-03-15 13:37 ` Anthony Liguori
2012-03-15 15:08 ` Paolo Bonzini
2012-03-15 13:37 ` [Qemu-devel] [PATCH 5/9] qtest: IRQ interception infrastructure (v2) Anthony Liguori
` (4 subsequent siblings)
8 siblings, 1 reply; 15+ messages in thread
From: Anthony Liguori @ 2012-03-15 13:37 UTC (permalink / raw)
To: qemu-devel; +Cc: Paolo Bonzini, 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.
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
v1 -> v2
- fix Makefile (Paolo)
- add stubs to work around bug in recent gtester (Paolo)
gtester-cat: work around recent bug
merge into check command
---
scripts/gtester-cat | 37 +++++++++++++++++++++++++++++
tests/Makefile | 64 +++++++++++++++++++++++++++++++++++++++++++++++---
2 files changed, 97 insertions(+), 4 deletions(-)
create mode 100755 scripts/gtester-cat
diff --git a/scripts/gtester-cat b/scripts/gtester-cat
new file mode 100755
index 0000000..de57e63
--- /dev/null
+++ b/scripts/gtester-cat
@@ -0,0 +1,37 @@
+#!/bin/sh
+#
+# Copyright IBM, Corp. 2012
+#
+# Authors:
+# Anthony Liguori <aliguori@us.ibm.com>
+#
+# 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>
+ <info>
+ <package>qemu</package>
+ <version>0.0</version>
+ <revision>rev</revision>
+ </info>
+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
diff --git a/tests/Makefile b/tests/Makefile
index 9d7cfb3..254c06d 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -11,7 +11,13 @@ CHECKS += test-qmp-input-visitor$(EXESUF)
CHECKS += test-qmp-output-visitor$(EXESUF)
CHECKS += test-string-input-visitor$(EXESUF)
CHECKS += test-string-output-visitor$(EXESUF)
-CHECKS += $(SRC_PATH)/tests/qemu-iotests-quick.sh
+
+TEST_SCRIPTS = $(SRC_PATH)/tests/qemu-iotests-quick.sh
+
+
+HW_TESTS=
+
+TARGETS=$(patsubst %-softmmu,%, $(filter %-softmmu,$(TARGET_DIRS)))
check-qint.o check-qstring.o check-qdict.o check-qlist.o check-qfloat.o check-qjson.o test-coroutine.o: $(GENERATED_HEADERS)
@@ -59,8 +65,58 @@ tests/rtc-test: tests/rtc-test.o tests/libqtest.o
.PHONY: check check-block
-check: $(CHECKS)
- $(call quiet-command, gtester $(CHECKS), " CHECK")
-
check-block:
$(call quiet-command, $(SHELL) $(SRC_PATH)/tests/check-block.sh , " CHECK")
+
+check-help:
+ @echo "Regression targets:"
+ @echo
+ @echo " make check Run all tests"
+ @echo " make check-qtest Run qtest tests"
+ @echo " make check-unit Run qobject tests"
+ @echo " make check-block Run block tests"
+ @echo " make check-report.html Generates an HTML test report"
+ @echo
+ @echo "Please note that HTML reports do not regenerate if the unit tests"
+ @echo "has not changed."
+ @echo
+ @echo "The variable SPEED can be set to control the gtester speed setting"
+
+.SECONDARY:
+
+SPEED ?= quick
+
+# Reports
+check-report-qtest-%.log: $(HW_TESTS)
+ $(call quiet-command,QTEST_QEMU_BINARY=`basename $@ .log | cut -f4 -d-`-softmmu/qemu-system-`basename $@ .log | cut -f4 -d-` \
+ gtester -k -q -o $@ -m=$(SPEED) $(HW_TESTS)," TEST $^ (`basename $@ .log | cut -f4 -d-`)")
+
+check-report-unit.log: $(CHECKS)
+ $(call quiet-command,gtester -k -q -m=$(SPEED) -o $@ $^, " TEST $^")
+
+check-report.log: check-report-unit.log $(patsubst %,check-report-qtest-%.log, $(TARGETS))
+ $(call quiet-command,$(SRC_PATH)/scripts/gtester-cat $^ > $@, " GEN $@")
+
+check-report.html: check-report.log
+ $(call quiet-command,gtester-report $< > $@, " GEN $@")
+
+# Check tests
+
+check-qtest-%: $(HW_TESTS)
+ @for test in $^; do \
+ arch=`echo $@ | cut -f3- -d-`; \
+ echo "Running '$$test' with qemu-system-$$arch..."; \
+ $(SRC_PATH)/scripts/qtest $$arch-softmmu/qemu-system-$$arch $$test || exit $$?; \
+ done
+
+check-qtest: $(patsubst %,check-qtest-%, $(TARGETS))
+
+check-unit: $(addprefix $(shell pwd)/, $(CHECKS)) $(TEST_SCRIPTS)
+ @for test in $^; do \
+ echo "Running '$$test'..."; \
+ $$test || exit $?; \
+ done
+
+check: check-unit check-qtest
+
+.PHONY: check-help check-qtest check-unit check
--
1.7.5.4
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [Qemu-devel] [PATCH 5/9] qtest: IRQ interception infrastructure (v2)
2012-03-15 13:37 [Qemu-devel] [PATCH 0/9] qtest: a testing framework for devices (v3) Anthony Liguori
` (3 preceding siblings ...)
2012-03-15 13:37 ` [Qemu-devel] [PATCH 4/9] make: add check targets based on gtester (v2) Anthony Liguori
@ 2012-03-15 13:37 ` Anthony Liguori
2012-03-15 13:37 ` [Qemu-devel] [PATCH 6/9] libqtest: add IRQ intercept commands Anthony Liguori
` (3 subsequent siblings)
8 siblings, 0 replies; 15+ messages in thread
From: Anthony Liguori @ 2012-03-15 13:37 UTC (permalink / raw)
To: qemu-devel; +Cc: Paolo Bonzini, Anthony Liguori
From: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
---
v1 -> v2
- rebased to latest (aliguori)
---
hw/irq.c | 17 +++++++++++
hw/irq.h | 5 +++
hw/pc_piix.c | 7 +++-
qtest.c | 92 ++++++++++++++++++++++++++++++++++++++++++---------------
4 files changed, 95 insertions(+), 26 deletions(-)
diff --git a/hw/irq.c b/hw/irq.c
index 62f766e..d413a0b 100644
--- a/hw/irq.c
+++ b/hw/irq.c
@@ -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);
+}
diff --git a/hw/irq.h b/hw/irq.h
index 64da2fd..56c55f0 100644
--- a/hw/irq.h
+++ b/hw/irq.h
@@ -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
diff --git a/hw/pc_piix.c b/hw/pc_piix.c
index 3f99f9a..22c0ad2 100644
--- a/hw/pc_piix.c
+++ b/hw/pc_piix.c
@@ -96,7 +96,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;
@@ -114,6 +114,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 */
@@ -219,7 +220,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]);
diff --git a/qtest.c b/qtest.c
index 46ebda1..a1eca49 100644
--- a/qtest.c
+++ b/qtest.c
@@ -12,6 +12,7 @@
*/
#include "qtest.h"
+#include "hw/qdev.h"
#include "qemu-char.h"
#include "ioport.h"
#include "memory.h"
@@ -24,6 +25,7 @@ const char *qtest_chrdev;
const char *qtest_log;
int qtest_allowed = 0;
+static DeviceState *irq_intercept_dev;
static FILE *qtest_log_fp;
static CharDriverState *qtest_chr;
static GString *inbuf;
@@ -66,18 +68,30 @@ static bool qtest_opened;
* > write ADDR SIZE DATA
* < OK
*
- * Valid async messages:
- *
- * IRQ raise NUM
- * IRQ lower NUM
- *
* ADDR, SIZE, VALUE are all integers parsed with strtoul() with a base of 0.
*
* DATA is an arbitrarily long hex number prefixed with '0x'. If it's smaller
* than the expected size, the value will be zero filled at the end of the data
* sequence.
*
- * NUM is an IRQ number.
+ * IRQ management:
+ *
+ * > irq_intercept_in QOM-PATH
+ * < OK
+ *
+ * > irq_intercept_out QOM-PATH
+ * < OK
+ *
+ * Attach to the gpio-in (resp. gpio-out) pins exported by the device at
+ * QOM-PATH. When the pin is triggered, one of the following async messages
+ * will be printed to the qtest stream:
+ *
+ * IRQ raise NUM
+ * IRQ lower NUM
+ *
+ * where NUM is an IRQ number. For the PC, interrupts can be intercepted
+ * simply with "irq_intercept_in ioapic" (note that IRQ0 comes out with
+ * NUM=0 even though it is remapped to GSI 2).
*/
static int hex2nib(char ch)
@@ -133,6 +147,20 @@ static void qtest_send(CharDriverState *chr, const char *fmt, ...)
}
}
+static void qtest_irq_handler(void *opaque, int n, int level)
+{
+ qemu_irq *old_irqs = opaque;
+ qemu_set_irq(old_irqs[n], level);
+
+ if (irq_levels[n] != level) {
+ CharDriverState *chr = qtest_chr;
+ irq_levels[n] = level;
+ qtest_send_prefix(chr);
+ qtest_send(chr, "IRQ %s %d\n",
+ level ? "raise" : "lower", n);
+ }
+}
+
static void qtest_process_command(CharDriverState *chr, gchar **words)
{
const gchar *command;
@@ -155,9 +183,40 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
}
g_assert(command);
- if (strcmp(words[0], "outb") == 0 ||
- strcmp(words[0], "outw") == 0 ||
- strcmp(words[0], "outl") == 0) {
+ if (strcmp(words[0], "irq_intercept_out") == 0
+ || strcmp(words[0], "irq_intercept_in") == 0) {
+ DeviceState *dev;
+
+ g_assert(words[1]);
+ dev = DEVICE(object_resolve_path(words[1], NULL));
+ if (!dev) {
+ qtest_send_prefix(chr);
+ qtest_send(chr, "FAIL Unknown device\n");
+ return;
+ }
+
+ if (irq_intercept_dev) {
+ qtest_send_prefix(chr);
+ if (irq_intercept_dev != dev) {
+ qtest_send(chr, "FAIL IRQ intercept already enabled\n");
+ } else {
+ qtest_send(chr, "OK\n");
+ }
+ return;
+ }
+
+ if (words[0][14] == 'o') {
+ qemu_irq_intercept_out(&dev->gpio_out, qtest_irq_handler, dev->num_gpio_out);
+ } else {
+ qemu_irq_intercept_in(dev->gpio_in, qtest_irq_handler, dev->num_gpio_in);
+ }
+ irq_intercept_dev = dev;
+ qtest_send_prefix(chr);
+ qtest_send(chr, "OK\n");
+
+ } else if (strcmp(words[0], "outb") == 0 ||
+ strcmp(words[0], "outw") == 0 ||
+ strcmp(words[0], "outl") == 0) {
uint16_t addr;
uint32_t value;
@@ -312,21 +371,6 @@ static void qtest_event(void *opaque, int event)
}
}
-static void qtest_set_irq(void *opaque, int irq, int level)
-{
- CharDriverState *chr = qtest_chr;
- bool changed;
-
- changed = (irq_levels[irq] != level);
- irq_levels[irq] = level;
-
- if (changed) {
- qtest_send_prefix(chr);
- qtest_send(chr, "IRQ %s %d\n",
- level ? "raise" : "lower", irq);
- }
-}
-
int qtest_init(void)
{
CharDriverState *chr;
--
1.7.5.4
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [Qemu-devel] [PATCH 6/9] libqtest: add IRQ intercept commands
2012-03-15 13:37 [Qemu-devel] [PATCH 0/9] qtest: a testing framework for devices (v3) Anthony Liguori
` (4 preceding siblings ...)
2012-03-15 13:37 ` [Qemu-devel] [PATCH 5/9] qtest: IRQ interception infrastructure (v2) Anthony Liguori
@ 2012-03-15 13:37 ` Anthony Liguori
2012-03-15 13:37 ` [Qemu-devel] [PATCH 7/9] rtc: split out macros into a header file and use in test case Anthony Liguori
` (2 subsequent siblings)
8 siblings, 0 replies; 15+ messages in thread
From: Anthony Liguori @ 2012-03-15 13:37 UTC (permalink / raw)
To: qemu-devel; +Cc: Paolo Bonzini, Anthony Liguori
From: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
---
tests/libqtest.c | 12 ++++++++++++
tests/libqtest.h | 6 ++++++
2 files changed, 18 insertions(+), 0 deletions(-)
diff --git a/tests/libqtest.c b/tests/libqtest.c
index dd07b07..1d1b06e 100644
--- a/tests/libqtest.c
+++ b/tests/libqtest.c
@@ -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);
diff --git a/tests/libqtest.h b/tests/libqtest.h
index dd82926..b5ca04e 100644
--- a/tests/libqtest.h
+++ b/tests/libqtest.h
@@ -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))
--
1.7.5.4
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [Qemu-devel] [PATCH 7/9] rtc: split out macros into a header file and use in test case
2012-03-15 13:37 [Qemu-devel] [PATCH 0/9] qtest: a testing framework for devices (v3) Anthony Liguori
` (5 preceding siblings ...)
2012-03-15 13:37 ` [Qemu-devel] [PATCH 6/9] libqtest: add IRQ intercept commands Anthony Liguori
@ 2012-03-15 13:37 ` Anthony Liguori
2012-03-15 13:37 ` [Qemu-devel] [PATCH 8/9] qtest: add rtc-test test-case (v3) Anthony Liguori
2012-03-15 13:37 ` [Qemu-devel] [PATCH 9/9] qtest: add clock management Anthony Liguori
8 siblings, 0 replies; 15+ messages in thread
From: Anthony Liguori @ 2012-03-15 13:37 UTC (permalink / raw)
To: qemu-devel; +Cc: Anthony Liguori
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
---
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
diff --git a/hw/mc146818rtc.c b/hw/mc146818rtc.c
index 2b59c36..9c64e0a 100644
--- a/hw/mc146818rtc.c
+++ b/hw/mc146818rtc.c
@@ -47,39 +47,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;
diff --git a/hw/mc146818rtc.h b/hw/mc146818rtc.h
index f119930..f286b6a 100644
--- a/hw/mc146818rtc.h
+++ b/hw/mc146818rtc.h
@@ -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);
diff --git a/hw/mc146818rtc_regs.h b/hw/mc146818rtc_regs.h
new file mode 100644
index 0000000..3ab3770
--- /dev/null
+++ b/hw/mc146818rtc_regs.h
@@ -0,0 +1,62 @@
+/*
+ * QEMU MC146818 RTC emulation
+ *
+ * Copyright (c) 2003-2004 Fabrice Bellard
+ *
+ * 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.
+ */
+#ifndef RTC_REGS_H
+#define RTC_REGS_H
+
+#define RTC_ISA_IRQ 8
+
+#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
+
+#endif
--
1.7.5.4
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [Qemu-devel] [PATCH 8/9] qtest: add rtc-test test-case (v3)
2012-03-15 13:37 [Qemu-devel] [PATCH 0/9] qtest: a testing framework for devices (v3) Anthony Liguori
` (6 preceding siblings ...)
2012-03-15 13:37 ` [Qemu-devel] [PATCH 7/9] rtc: split out macros into a header file and use in test case Anthony Liguori
@ 2012-03-15 13:37 ` Anthony Liguori
2012-03-15 13:37 ` [Qemu-devel] [PATCH 9/9] qtest: add clock management Anthony Liguori
8 siblings, 0 replies; 15+ messages in thread
From: Anthony Liguori @ 2012-03-15 13:37 UTC (permalink / raw)
To: qemu-devel; +Cc: Paolo Bonzini, Anthony Liguori
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
v1 -> v2
- fix set_alarm_time (Paolo)
v2 -> v3
- merge IRQ intercept support (Paolo)
---
tests/Makefile | 2 +-
tests/rtc-test.c | 268 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 269 insertions(+), 1 deletions(-)
create mode 100644 tests/rtc-test.c
diff --git a/tests/Makefile b/tests/Makefile
index 254c06d..8c83207 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -15,7 +15,7 @@ CHECKS += test-string-output-visitor$(EXESUF)
TEST_SCRIPTS = $(SRC_PATH)/tests/qemu-iotests-quick.sh
-HW_TESTS=
+HW_TESTS=tests/rtc-test
TARGETS=$(patsubst %-softmmu,%, $(filter %-softmmu,$(TARGET_DIRS)))
diff --git a/tests/rtc-test.c b/tests/rtc-test.c
new file mode 100644
index 0000000..8280f45
--- /dev/null
+++ b/tests/rtc-test.c
@@ -0,0 +1,268 @@
+/*
+ * QTest
+ *
+ * Copyright IBM, Corp. 2012
+ *
+ * Authors:
+ * Anthony Liguori <aliguori@us.ibm.com>
+ *
+ * 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_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);
+ qtest_add_func("/rtc/alarm-time", alarm_time);
+ } else {
+ g_test_message("Skipping unsupported arch `%s'\n", arch);
+ }
+
+ ret = g_test_run();
+
+ if (s) {
+ qtest_quit(s);
+ }
+
+ return ret;
+}
--
1.7.5.4
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [Qemu-devel] [PATCH 9/9] qtest: add clock management
2012-03-15 13:37 [Qemu-devel] [PATCH 0/9] qtest: a testing framework for devices (v3) Anthony Liguori
` (7 preceding siblings ...)
2012-03-15 13:37 ` [Qemu-devel] [PATCH 8/9] qtest: add rtc-test test-case (v3) Anthony Liguori
@ 2012-03-15 13:37 ` Anthony Liguori
8 siblings, 0 replies; 15+ messages in thread
From: Anthony Liguori @ 2012-03-15 13:37 UTC (permalink / raw)
To: qemu-devel; +Cc: Paolo Bonzini, Anthony Liguori
From: Paolo Bonzini <pbonzini@redhat.com>
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.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
---
cpus.c | 20 ++++++++++++++++++++
cpus.h | 2 ++
qemu-timer.c | 2 +-
qemu-timer.h | 1 +
qtest.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 69 insertions(+), 1 deletions(-)
diff --git a/cpus.c b/cpus.c
index 010047e..107b2ca 100644
--- a/cpus.c
+++ b/cpus.c
@@ -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) {
diff --git a/cpus.h b/cpus.h
index 4ea2fe2..81bd817 100644
--- a/cpus.h
+++ b/cpus.h
@@ -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;
diff --git a/qemu-timer.c b/qemu-timer.c
index d7f56e5..80bcc56 100644
--- a/qemu-timer.c
+++ b/qemu-timer.c
@@ -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;
diff --git a/qemu-timer.h b/qemu-timer.h
index de17f3b..661bbe7 100644
--- a/qemu-timer.h
+++ b/qemu-timer.h
@@ -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);
diff --git a/qtest.c b/qtest.c
index a1eca49..53e2b79 100644
--- a/qtest.c
+++ b/qtest.c
@@ -18,6 +18,7 @@
#include "memory.h"
#include "hw/irq.h"
#include "sysemu.h"
+#include "cpus.h"
#define MAX_IRQ 256
@@ -44,6 +45,30 @@ static bool qtest_opened;
*
* Valid requests
*
+ * Clock management:
+ *
+ * The qtest client is completely in charge of the vm_clock. qtest commands
+ * let you adjust the value of the clock (monotonically). All the commands
+ * return the current value of the clock in nanoseconds.
+ *
+ * > clock_step
+ * < OK VALUE
+ *
+ * Advance the clock to the next deadline. Useful when waiting for
+ * asynchronous events.
+ *
+ * > clock_step NS
+ * < OK VALUE
+ *
+ * Advance the clock by NS nanoseconds.
+ *
+ * > clock_set NS
+ * < OK VALUE
+ *
+ * Advance the clock to NS nanoseconds (do nothing if it's already past).
+ *
+ * PIO and memory access:
+ *
* > outb ADDR VALUE
* < OK
*
@@ -299,6 +324,25 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
qtest_send_prefix(chr);
qtest_send(chr, "OK\n");
+ } else if (strcmp(words[0], "clock_step") == 0) {
+ int64_t ns;
+
+ if (words[1]) {
+ ns = strtoll(words[1], NULL, 0);
+ } else {
+ ns = qemu_clock_deadline(vm_clock);
+ }
+ qtest_clock_warp(qemu_get_clock_ns(vm_clock) + ns);
+ qtest_send_prefix(chr);
+ qtest_send(chr, "OK %"PRIi64"\n", (int64_t)qemu_get_clock_ns(vm_clock));
+ } else if (strcmp(words[0], "clock_set") == 0) {
+ int64_t ns;
+
+ g_assert(words[1]);
+ ns = strtoll(words[1], NULL, 0);
+ qtest_clock_warp(ns);
+ qtest_send_prefix(chr);
+ qtest_send(chr, "OK %"PRIi64"\n", (int64_t)qemu_get_clock_ns(vm_clock));
} else {
qtest_send_prefix(chr);
qtest_send(chr, "FAIL Unknown command `%s'\n", words[0]);
@@ -377,6 +421,7 @@ int qtest_init(void)
g_assert(qtest_chrdev != NULL);
+ configure_icount("0");
chr = qemu_chr_new("qtest", qtest_chrdev, NULL);
qemu_chr_add_handlers(chr, qtest_can_read, qtest_read, qtest_event, chr);
--
1.7.5.4
^ permalink raw reply related [flat|nested] 15+ messages in thread
* Re: [Qemu-devel] [PATCH 3/9] qtest: add C version of test infrastructure
2012-03-15 13:37 ` [Qemu-devel] [PATCH 3/9] qtest: add C version of test infrastructure Anthony Liguori
@ 2012-03-15 14:42 ` Stefan Hajnoczi
2012-03-15 14:58 ` Kevin Wolf
2012-03-15 15:03 ` Anthony Liguori
0 siblings, 2 replies; 15+ messages in thread
From: Stefan Hajnoczi @ 2012-03-15 14:42 UTC (permalink / raw)
To: Anthony Liguori; +Cc: Kevin Wolf, qemu-devel
On Thu, Mar 15, 2012 at 1:37 PM, Anthony Liguori <aliguori@us.ibm.com> wrote:
> + sock = socket(PF_UNIX, SOCK_STREAM, 0);
> + g_assert_no_errno(sock);
> +
> + addr.sun_family = AF_UNIX;
> + snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", socket_path);
> +
> + pid = fork();
> + if (pid == 0) {
> + command = g_strdup_printf("%s "
> + "-qtest unix:%s,server,nowait "
> + "-qtest-log /dev/null "
> + "-pidfile %s "
> + "-machine accel=qtest "
> + "%s", qemu_binary, socket_path,
> + pid_file,
> + extra_args ?: "");
> +
> + ret = system(command);
> + exit(ret);
> + g_free(command);
> + }
> +
> + do {
> + sleep(1);
> + ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
> + } while (ret == -1);
I believe Kevin suggested using -qtest unix:%s and creating the listen
socket in the test program rather than inside QEMU. The advantage is
that we never sleep(3), instead we accept(2) the connection from QEMU
and get going right away.
This can be added as a patch later.
Kevin: Do you already have a patch or does someone need to write it?
Stefan
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [Qemu-devel] [PATCH 3/9] qtest: add C version of test infrastructure
2012-03-15 14:42 ` Stefan Hajnoczi
@ 2012-03-15 14:58 ` Kevin Wolf
2012-03-15 15:04 ` Paolo Bonzini
2012-03-15 15:03 ` Anthony Liguori
1 sibling, 1 reply; 15+ messages in thread
From: Kevin Wolf @ 2012-03-15 14:58 UTC (permalink / raw)
To: Stefan Hajnoczi; +Cc: Paolo Bonzini, Anthony Liguori, qemu-devel
Am 15.03.2012 15:42, schrieb Stefan Hajnoczi:
> On Thu, Mar 15, 2012 at 1:37 PM, Anthony Liguori <aliguori@us.ibm.com> wrote:
>> + sock = socket(PF_UNIX, SOCK_STREAM, 0);
>> + g_assert_no_errno(sock);
>> +
>> + addr.sun_family = AF_UNIX;
>> + snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", socket_path);
>> +
>> + pid = fork();
>> + if (pid == 0) {
>> + command = g_strdup_printf("%s "
>> + "-qtest unix:%s,server,nowait "
>> + "-qtest-log /dev/null "
>> + "-pidfile %s "
>> + "-machine accel=qtest "
>> + "%s", qemu_binary, socket_path,
>> + pid_file,
>> + extra_args ?: "");
>> +
>> + ret = system(command);
>> + exit(ret);
>> + g_free(command);
>> + }
>> +
>> + do {
>> + sleep(1);
>> + ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
>> + } while (ret == -1);
>
> I believe Kevin suggested using -qtest unix:%s and creating the listen
> socket in the test program rather than inside QEMU. The advantage is
> that we never sleep(3), instead we accept(2) the connection from QEMU
> and get going right away.
>
> This can be added as a patch later.
>
> Kevin: Do you already have a patch or does someone need to write it?
I just complained (and reduced the delay locally). Paolo suggested a
real solution, which may have been what you describe, I don't remember
the details.
Kevin
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [Qemu-devel] [PATCH 3/9] qtest: add C version of test infrastructure
2012-03-15 14:42 ` Stefan Hajnoczi
2012-03-15 14:58 ` Kevin Wolf
@ 2012-03-15 15:03 ` Anthony Liguori
1 sibling, 0 replies; 15+ messages in thread
From: Anthony Liguori @ 2012-03-15 15:03 UTC (permalink / raw)
To: Stefan Hajnoczi; +Cc: Kevin Wolf, qemu-devel
On 03/15/2012 09:42 AM, Stefan Hajnoczi wrote:
> On Thu, Mar 15, 2012 at 1:37 PM, Anthony Liguori<aliguori@us.ibm.com> wrote:
>> + sock = socket(PF_UNIX, SOCK_STREAM, 0);
>> + g_assert_no_errno(sock);
>> +
>> + addr.sun_family = AF_UNIX;
>> + snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", socket_path);
>> +
>> + pid = fork();
>> + if (pid == 0) {
>> + command = g_strdup_printf("%s "
>> + "-qtest unix:%s,server,nowait "
>> + "-qtest-log /dev/null "
>> + "-pidfile %s "
>> + "-machine accel=qtest "
>> + "%s", qemu_binary, socket_path,
>> + pid_file,
>> + extra_args ?: "");
>> +
>> + ret = system(command);
>> + exit(ret);
>> + g_free(command);
>> + }
>> +
>> + do {
>> + sleep(1);
>> + ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
>> + } while (ret == -1);
>
> I believe Kevin suggested using -qtest unix:%s and creating the listen
> socket in the test program rather than inside QEMU. The advantage is
> that we never sleep(3), instead we accept(2) the connection from QEMU
> and get going right away.
Yeah, I missed that, it's an easy change to make. I'll update.
Regards,
Anthony Liguori
>
> This can be added as a patch later.
>
> Kevin: Do you already have a patch or does someone need to write it?
>
> Stefan
>
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [Qemu-devel] [PATCH 3/9] qtest: add C version of test infrastructure
2012-03-15 14:58 ` Kevin Wolf
@ 2012-03-15 15:04 ` Paolo Bonzini
0 siblings, 0 replies; 15+ messages in thread
From: Paolo Bonzini @ 2012-03-15 15:04 UTC (permalink / raw)
To: Kevin Wolf; +Cc: Stefan Hajnoczi, Anthony Liguori, qemu-devel
Il 15/03/2012 15:58, Kevin Wolf ha scritto:
>> > I believe Kevin suggested using -qtest unix:%s and creating the listen
>> > socket in the test program rather than inside QEMU. The advantage is
>> > that we never sleep(3), instead we accept(2) the connection from QEMU
>> > and get going right away.
>> >
>> > This can be added as a patch later.
>> >
>> > Kevin: Do you already have a patch or does someone need to write it?
> I just complained (and reduced the delay locally). Paolo suggested a
> real solution, which may have been what you describe, I don't remember
> the details.
Yes, that was me.
I implemented it in Python too, but the Python harness is
(understandably, since it's otherwise unused) not part of this series.
I definitely can fix it before 1.1, though not immediately.
Paolo
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [Qemu-devel] [PATCH 4/9] make: add check targets based on gtester (v2)
2012-03-15 13:37 ` [Qemu-devel] [PATCH 4/9] make: add check targets based on gtester (v2) Anthony Liguori
@ 2012-03-15 15:08 ` Paolo Bonzini
0 siblings, 0 replies; 15+ messages in thread
From: Paolo Bonzini @ 2012-03-15 15:08 UTC (permalink / raw)
To: Anthony Liguori; +Cc: qemu-devel
Il 15/03/2012 14:37, Anthony Liguori ha scritto:
> + echo $LINE
You probably want echo "$LINE" here, or you could just replace the whole
loop with this:
sed -e '/<?xml/d' -e '/gtester>/d' "$@"
Paolo
^ permalink raw reply [flat|nested] 15+ messages in thread
end of thread, other threads:[~2012-03-15 15:09 UTC | newest]
Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-03-15 13:37 [Qemu-devel] [PATCH 0/9] qtest: a testing framework for devices (v3) Anthony Liguori
2012-03-15 13:37 ` [Qemu-devel] [PATCH 1/9] w32: Support tests (make check) Anthony Liguori
2012-03-15 13:37 ` [Qemu-devel] [PATCH 2/9] qtest: add test framework (v3) Anthony Liguori
2012-03-15 13:37 ` [Qemu-devel] [PATCH 3/9] qtest: add C version of test infrastructure Anthony Liguori
2012-03-15 14:42 ` Stefan Hajnoczi
2012-03-15 14:58 ` Kevin Wolf
2012-03-15 15:04 ` Paolo Bonzini
2012-03-15 15:03 ` Anthony Liguori
2012-03-15 13:37 ` [Qemu-devel] [PATCH 4/9] make: add check targets based on gtester (v2) Anthony Liguori
2012-03-15 15:08 ` Paolo Bonzini
2012-03-15 13:37 ` [Qemu-devel] [PATCH 5/9] qtest: IRQ interception infrastructure (v2) Anthony Liguori
2012-03-15 13:37 ` [Qemu-devel] [PATCH 6/9] libqtest: add IRQ intercept commands Anthony Liguori
2012-03-15 13:37 ` [Qemu-devel] [PATCH 7/9] rtc: split out macros into a header file and use in test case Anthony Liguori
2012-03-15 13:37 ` [Qemu-devel] [PATCH 8/9] qtest: add rtc-test test-case (v3) Anthony Liguori
2012-03-15 13:37 ` [Qemu-devel] [PATCH 9/9] qtest: add clock management Anthony Liguori
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).