public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Andrew Jones <drjones@redhat.com>
To: kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org
Cc: christoffer.dall@linaro.org, pbonzini@redhat.com
Subject: [PATCH v7 08/14] Introduce chr-testdev
Date: Wed, 16 Jul 2014 10:47:37 +0200	[thread overview]
Message-ID: <1405500463-20713-9-git-send-email-drjones@redhat.com> (raw)
In-Reply-To: <1405500463-20713-1-git-send-email-drjones@redhat.com>

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 <drjones@redhat.com>
---
 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 <drjones@redhat.com>
+ *
+ * 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 <drjones@redhat.com>
+ *
+ * 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 37ce028b2c2bb..b51899ab998b6 100644
--- a/lib/virtio.h
+++ b/lib/virtio.h
@@ -10,6 +10,8 @@
  */
 #include "libcflat.h"
 
+#define VIRTIO_ID_CONSOLE 3
+
 struct virtio_device_id {
 	u32 device;
 	u32 vendor;
-- 
1.9.3


  parent reply	other threads:[~2014-07-16  8:48 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-16  8:47 [PATCH v7 00/14] kvm-unit-tests/arm: initial drop Andrew Jones
2014-07-16  8:47 ` [PATCH v7 02/14] add support for Linux device trees Andrew Jones
2014-07-16  8:47 ` [PATCH v7 03/14] Introduce asm-generic/*.h files Andrew Jones
2014-07-16  8:47 ` [PATCH v7 04/14] Introduce lib/alloc Andrew Jones
2014-07-16  8:47 ` [PATCH v7 05/14] add minimal virtio support for devtree virtio-mmio Andrew Jones
2014-07-16  8:47 ` [PATCH v7 06/14] lib/asm-generic: add page.h and virt_to_phys/phys_to_virt Andrew Jones
2014-07-16  8:47 ` [PATCH v7 07/14] virtio: add minimal support for virtqueues Andrew Jones
2014-07-16  8:47 ` Andrew Jones [this message]
2014-07-16  9:31   ` [PATCH v7 08/14] Introduce chr-testdev Levente Kurusa
2014-07-16  9:33     ` Andrew Jones
2014-07-22 18:06       ` Radim Krčmář
2014-07-16  8:47 ` [PATCH v7 09/14] arm: initial drop Andrew Jones
2014-07-16  9:22   ` Paolo Bonzini
2014-07-16  9:39     ` Andrew Jones
2014-07-16 10:59       ` Paolo Bonzini
2014-07-16  8:47 ` [PATCH v7 10/14] arm: Add spinlock implementation Andrew Jones
2014-07-16  8:47 ` [PATCH v7 11/14] arm: Add IO accessors to avoid register-writeback Andrew Jones
2014-07-16  8:47 ` [PATCH v7 12/14] arm: Add arch-specific asm/page.h and __va/__pa Andrew Jones
2014-07-16  8:47 ` [PATCH v7 13/14] arm: add useful headers from the Linux kernel Andrew Jones
2014-07-16  8:47 ` [PATCH v7 14/14] arm: vectors support Andrew Jones
2014-08-22 18:51 ` [PATCH v7 00/14] kvm-unit-tests/arm: initial drop Paolo Bonzini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1405500463-20713-9-git-send-email-drjones@redhat.com \
    --to=drjones@redhat.com \
    --cc=christoffer.dall@linaro.org \
    --cc=kvm@vger.kernel.org \
    --cc=kvmarm@lists.cs.columbia.edu \
    --cc=pbonzini@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox