kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Christoffer Dall <christoffer.dall@linaro.org>
To: Andrew Jones <drjones@redhat.com>
Cc: kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu, gleb@redhat.com
Subject: Re: [PATCH 6/9] Introduce virtio-testdev
Date: Wed, 16 Oct 2013 18:06:22 -0700	[thread overview]
Message-ID: <20131017010622.GI24837@cbox> (raw)
In-Reply-To: <1381767815-12510-7-git-send-email-drjones@redhat.com>

On Mon, Oct 14, 2013 at 06:23:32PM +0200, Andrew Jones wrote:
> virtio-testdev is a communication channel to qemu through virtio that
> can be used by test code to send commands. The only command currently
> implemented is EXIT, which allows the test code to exit with a given
> status code.
> 
> Signed-off-by: Andrew Jones <drjones@redhat.com>
> ---
>  lib/bswap.h          |   7 +++
>  lib/libcflat.h       |  10 ++++
>  lib/virtio-testdev.c | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++
>  lib/virtio-testdev.h |   9 ++++
>  4 files changed, 152 insertions(+)
>  create mode 100644 lib/bswap.h
>  create mode 100644 lib/virtio-testdev.c
>  create mode 100644 lib/virtio-testdev.h
> 
> diff --git a/lib/bswap.h b/lib/bswap.h
> new file mode 100644
> index 0000000000000..e63c4d37a8b9a
> --- /dev/null
> +++ b/lib/bswap.h
> @@ -0,0 +1,7 @@
> +#ifndef _BSWAP_H_
> +#define _BSWAP_H_
> +#define le32_to_cpu(x) (x)
> +#define cpu_to_le32(x) (x)
> +#define le16_to_cpu(x) (x)
> +#define cpu_to_le16(x) (x)
> +#endif
> diff --git a/lib/libcflat.h b/lib/libcflat.h
> index a1be635ab4ee9..41791194657d0 100644
> --- a/lib/libcflat.h
> +++ b/lib/libcflat.h
> @@ -37,6 +37,16 @@ typedef _Bool bool;
>  #define true 1
>  #define false 0
>  
> +typedef u32 compat_ptr_t;
> +static inline void *compat_ptr(compat_ptr_t ptr)
> +{
> +    return (void *)(unsigned long)ptr;
> +}
> +static inline compat_ptr_t ptr_to_compat(void *ptr)
> +{
> +    return (u32)(unsigned long)ptr;
> +}
> +
>  extern void halt(int code);
>  extern void exit(int code);
>  
> diff --git a/lib/virtio-testdev.c b/lib/virtio-testdev.c
> new file mode 100644
> index 0000000000000..900921302c344
> --- /dev/null
> +++ b/lib/virtio-testdev.c

There's an interesting mix of space and tab indentations in this file...

I assume using the kernel coding style would be approprate here...
Actually, that could be added to the readme too. (Or whichever format we
choose).

Could you fix?

> @@ -0,0 +1,126 @@
> +#include "libcflat.h"
> +#include "iomaps.h"
> +#include "bswap.h"
> +
> +#define TESTDEV_MAJOR_VER 1
> +#define TESTDEV_MINOR_VER 1
> +
> +#define VIRTIO_MMIO_DEVICEID 0x8
> +#define VIRTIO_ID_TESTDEV 0xffff
> +#define DEV_CONFIG_BASE 0x100
> +#define DEV_CONFIG_SIZE 0x100
> +
> +enum { VERSION = 1, CLEAR, EXIT, };
> +enum { TOKEN, NARGS, NRETS, ARG1, ARG2, ARG3, ARG4, };
> +
> +#define RET1(nargs) (ARG1 + (nargs) + 0)
> +#define RET2(nargs) (ARG1 + (nargs) + 1)
> +#define RET3(nargs) (ARG1 + (nargs) + 2)
> +#define RET4(nargs) (ARG1 + (nargs) + 3)
> +
> +static u32 *testdev_base;
> +
> +/*
> + * We have to write all args; nargs, nrets, ... first to
> + * avoid executing token's operation until all args are in
> + * place. Then issue the op, and then read the rets. Reading
> + * the rets (or just sanity checking by reading base[0]) will
> + * read a zero into qemu's copy of the token, which allows us
> + * to prepare additional ops without re-executing the last one.
> + */
> +void virtio_testdev(u32 token, u32 nargs, u32 nrets, ...)
> +{
> +    volatile u32 *base = testdev_base;
> +    volatile u32 *tdp = base + 1;
> +    va_list va;
> +
> +    if (!testdev_base)
> +	return;
> +
> +    *tdp++ = cpu_to_le32(nargs);
> +    *tdp++ = cpu_to_le32(nrets);
> +
> +    va_start(va, nrets);
> +
> +    while (nargs--)
> +	*tdp++ = cpu_to_le32(va_arg(va, unsigned));
> +
> +    /* this runs the op, but then resets base[0] to zero */
> +    base[0] = cpu_to_le32(token);
> +
> +    /* sanity check */
> +    if (base[0] != 0) {
> +	printf("virtio-testdev base[0] should always be zero!\n");
> +	halt(EIO);
> +    }
> +
> +    while (nrets--) {
> +	u32 *r = va_arg(va, unsigned *);
> +	*r = le32_to_cpu(*tdp++);
> +    }
> +
> +    va_end(va);
> +}
> +
> +void virtio_testdev_version(u32 *version)
> +{
> +    virtio_testdev(VERSION, 0, 1, version);
> +}
> +
> +void virtio_testdev_clear(void)
> +{
> +    virtio_testdev(CLEAR, 0, 0);
> +}
> +
> +void virtio_testdev_exit(int code)
> +{
> +    virtio_testdev(EXIT, 1, 0, code);
> +}
> +
> +void virtio_testdev_init(void)
> +{
> +    struct iomap *m;
> +    u32 version, i;
> +    u16 major, minor;
> +
> +    m = iomaps_find("virtio_mmio");
> +    if (!m) {
> +	printf("No virtio-mmio transports for virtio-testdev found!\n");
> +	halt(ENODEV);
> +    }
> +
> +    for (i = 0; i < m->nr; ++i) {
> +	volatile u32 *base = (u32 *)compat_ptr(m->addrs[i]);
> +	u32 devid32 = base[VIRTIO_MMIO_DEVICEID / 4];

do we have readl/writel in this framework?  If not, maybe we should to
indicate IO read/writes and ensure the compiler doesn't reorder things
and that we have the necessary memory barriers etc...?

That would apply to virtio_testdev above as well.

> +	u16 devid = devid32 & 0xffff;
> +	devid = le16_to_cpu(devid);
> +	if (devid == VIRTIO_ID_TESTDEV) {
> +	    testdev_base = (u32 *)compat_ptr(m->addrs[i] + DEV_CONFIG_BASE);
> +	    break;
> +	}
> +    }
> +
> +    if (!testdev_base) {
> +	printf("Can't find virtio-testdev. Is '-device virtio-testdev' "
> +	       "on the qemu command line?\n");
> +	halt(ENODEV);
> +    }
> +
> +    virtio_testdev_version(&version);
> +    major = version >> 16;
> +    minor = version & 0xffff;
> +
> +    if (major != TESTDEV_MAJOR_VER || minor < TESTDEV_MINOR_VER) {
> +	char *u = "qemu";
> +	if (major > TESTDEV_MAJOR_VER)
> +	    u = "kvm-unit-tests";
> +	printf("Refusing to run with virtio-testdev major = %d, minor = %d\n",
> +			major, minor);

"Refusing to run"?
How about "incompatible version of virtio-testdev"?

> +	printf("Update %s.\n", u);
> +	exit(ENODEV);
> +    }
> +
> +    if (minor > TESTDEV_MINOR_VER)
> +	printf("testdev has new features. "
> +	       "An update of kvm-unit-tests may be possible.\n");
> +}
> diff --git a/lib/virtio-testdev.h b/lib/virtio-testdev.h
> new file mode 100644
> index 0000000000000..c10bbfb591d0b
> --- /dev/null
> +++ b/lib/virtio-testdev.h
> @@ -0,0 +1,9 @@
> +#ifndef _VIRTIO_TESTDEV_H_
> +#define _VIRTIO_TESTDEV_H_
> +#include "libcflat.h"
> +
> +extern void virtio_testdev_init(void);
> +extern void virtio_testdev_version(u32 *version);
> +extern void virtio_testdev_clear(void);
> +extern void virtio_testdev_exit(int code);
> +#endif
> -- 
> 1.8.1.4
> 

  parent reply	other threads:[~2013-10-17  1:05 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-10-14 16:23 [PATCH 0/9] kvm-unit-tests/arm: initial drop Andrew Jones
2013-10-14 16:23 ` [PATCH 1/9] remove unused files Andrew Jones
2013-10-16 12:52   ` Gleb Natapov
2013-10-16 13:13     ` Alexander Graf
2013-10-16 13:18     ` Andrew Jones
2013-10-14 16:23 ` [PATCH 2/9] makefile and run_tests tweaks Andrew Jones
2013-10-14 16:23 ` [PATCH 3/9] clean root dir of all x86-ness Andrew Jones
2013-10-17  1:06   ` Christoffer Dall
2013-10-17  9:35     ` Andrew Jones
2013-10-17 19:01       ` Christoffer Dall
2013-10-20 16:37         ` Andrew Jones
2013-10-14 16:23 ` [PATCH 4/9] Introduce a simple iomap structure Andrew Jones
2013-10-14 16:23 ` [PATCH 5/9] Add halt() and some error codes Andrew Jones
2013-10-14 16:23 ` [PATCH 6/9] Introduce virtio-testdev Andrew Jones
2013-10-15  8:39   ` Andrew Jones
2013-10-17  1:06   ` Christoffer Dall
2013-10-17  9:51     ` Andrew Jones
2013-10-17 19:01       ` Christoffer Dall
2013-10-17  1:06   ` Christoffer Dall [this message]
2013-10-14 16:23 ` [PATCH 7/9] arm: replace arbitrary divisions Andrew Jones
2013-10-17  1:06   ` Christoffer Dall
2013-10-17 10:03     ` Andrew Jones
2013-10-17 18:59       ` Christoffer Dall
2013-10-14 16:23 ` [PATCH 8/9] arm: initial drop Andrew Jones
2013-10-17  1:06   ` Christoffer Dall
2013-10-17 10:16     ` Andrew Jones
2013-10-17 13:28       ` Andrew Jones
2013-10-17 18:39         ` Christoffer Dall
2013-10-14 16:23 ` [PATCH 9/9] arm: add vectors support Andrew Jones
2013-10-17  1:06   ` Christoffer Dall
2013-10-17 10:38     ` Andrew Jones
2013-10-17 18:58       ` Christoffer Dall
2013-10-20 16:35         ` Andrew Jones
2013-10-21  9:59           ` Christoffer Dall
2013-11-20 23:06 ` [PATCH 0/9] kvm-unit-tests/arm: initial drop María Soler Heredia
2013-11-26 17:23   ` Andrew Jones
2013-12-29  9:24 ` Christoffer Dall
2014-01-02 18:56   ` Andrew Jones

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=20131017010622.GI24837@cbox \
    --to=christoffer.dall@linaro.org \
    --cc=drjones@redhat.com \
    --cc=gleb@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=kvmarm@lists.cs.columbia.edu \
    /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;
as well as URLs for NNTP newsgroup(s).