From: Andrew Jones Subject: [PATCH v6 10/17] Introduce chr-testdev Date: Fri, 11 Jul 2014 10:19:40 +0200 Message-ID: <1405066787-5793-11-git-send-email-drjones@redhat.com> References: <1405066787-5793-1-git-send-email-drjones@redhat.com> Cc: christoffer.dall@linaro.org, pbonzini@redhat.com To: kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org Return-path: Received: from mx1.redhat.com ([209.132.183.28]:64204 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751274AbaGKIUO (ORCPT ); Fri, 11 Jul 2014 04:20:14 -0400 In-Reply-To: <1405066787-5793-1-git-send-email-drjones@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: chr-testdev is a qemu backend that can be used by test code to send qemu commands. It communicates with qemu through a virtio-console device. The only command currently implemented is "quit", which allows the test code to exit with a given status code, i.e. chr_testdev_exit(code). Signed-off-by: Andrew Jones --- lib/chr-testdev.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/chr-testdev.h | 14 +++++++++++ lib/virtio.h | 2 ++ 3 files changed, 88 insertions(+) create mode 100644 lib/chr-testdev.c create mode 100644 lib/chr-testdev.h diff --git a/lib/chr-testdev.c b/lib/chr-testdev.c new file mode 100644 index 0000000000000..0c9a173a04886 --- /dev/null +++ b/lib/chr-testdev.c @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2014, Red Hat Inc, Andrew Jones + * + * This work is licensed under the terms of the GNU LGPL, version 2. + */ +#include "libcflat.h" +#include "virtio.h" +#include "asm/spinlock.h" + +#define TESTDEV_NAME "chr-testdev" + +static struct virtio_device *vcon; +static struct virtqueue *in_vq, *out_vq; +static struct spinlock lock; + +static void __testdev_send(char *buf, size_t len) +{ + int ret; + + ret = virtqueue_add_outbuf(out_vq, buf, len); + virtqueue_kick(out_vq); + + if (ret < 0) + return; + + while (!virtqueue_get_buf(out_vq, &len)) + ; +} + +void chr_testdev_exit(int code) +{ + char buf[8]; + int len; + + snprintf(buf, sizeof(buf), "%dq", code); + len = strlen(buf); + + spin_lock(&lock); + + if (!vcon) + goto out; + + __testdev_send(buf, len); + +out: + spin_unlock(&lock); +} + +void chr_testdev_init(void) +{ + const char *io_names[] = { "input", "output" }; + struct virtqueue *vqs[2]; + int ret; + + vcon = virtio_bind(VIRTIO_ID_CONSOLE); + if (vcon == NULL) { + printf("%s: %s: can't find a virtio-console\n", + __func__, TESTDEV_NAME); + return; + } + + ret = vcon->config->find_vqs(vcon, 2, vqs, NULL, io_names); + if (ret < 0) { + printf("%s: %s: can't init virtqueues\n", + __func__, TESTDEV_NAME); + vcon = NULL; + return; + } + + in_vq = vqs[0]; + out_vq = vqs[1]; +} diff --git a/lib/chr-testdev.h b/lib/chr-testdev.h new file mode 100644 index 0000000000000..ffd9a851aa9b9 --- /dev/null +++ b/lib/chr-testdev.h @@ -0,0 +1,14 @@ +#ifndef _CHR_TESTDEV_H_ +#define _CHR_TESTDEV_H_ +/* + * chr-testdev is a driver for the chr-testdev qemu backend. + * The chr-testdev backend exposes a simple control interface to + * qemu for kvm-unit-tests accessible through virtio-console. + * + * Copyright (C) 2014, Red Hat Inc, Andrew Jones + * + * This work is licensed under the terms of the GNU LGPL, version 2. + */ +extern void chr_testdev_init(void); +extern void chr_testdev_exit(int code); +#endif diff --git a/lib/virtio.h b/lib/virtio.h index 110a066c8591c..a5fd830dfd64b 100644 --- a/lib/virtio.h +++ b/lib/virtio.h @@ -11,6 +11,8 @@ #include "libcflat.h" #include "asm/page.h" +#define VIRTIO_ID_CONSOLE 3 + struct virtio_device_id { u32 device; u32 vendor; -- 1.9.3