From: Hajime Tazaki <thehajime@gmail.com>
To: linux-um@lists.infradead.org
Cc: Octavian Purdila <tavi.purdila@gmail.com>,
Akira Moroo <retrage01@gmail.com>,
linux-kernel-library@freelists.org, linux-arch@vger.kernel.org,
Michael Zimmermann <sigmaepsilon92@gmail.com>,
Petros Angelatos <petrosagg@gmail.com>
Subject: [RFC v2 18/37] lkl tools: host lib: virtio block device
Date: Fri, 8 Nov 2019 14:02:33 +0900 [thread overview]
Message-ID: <73d0e15d1429a089442c932d1b3fddc724a43bb6.1573179553.git.thehajime@gmail.com> (raw)
In-Reply-To: <cover.1573179553.git.thehajime@gmail.com>
From: Octavian Purdila <tavi.purdila@gmail.com>
Host independent implementation for virtio block devices. The host
dependent part of the host library must provide an implementation for
lkl_dev_block_ops.
Disks can be added to the LKL configuration via lkl_disk_add(), a new
LKL application API.
Signed-off-by: Michael Zimmermann <sigmaepsilon92@gmail.com>
Signed-off-by: Petros Angelatos <petrosagg@gmail.com>
Signed-off-by: Octavian Purdila <tavi.purdila@gmail.com>
---
tools/lkl/include/lkl.h | 39 +++++++++++
tools/lkl/include/lkl_host.h | 57 +++++++++++++++
tools/lkl/lib/Build | 1 +
tools/lkl/lib/virtio_blk.c | 132 +++++++++++++++++++++++++++++++++++
4 files changed, 229 insertions(+)
create mode 100644 tools/lkl/lib/virtio_blk.c
diff --git a/tools/lkl/include/lkl.h b/tools/lkl/include/lkl.h
index 76da534a85f1..967fbe4dbc26 100644
--- a/tools/lkl/include/lkl.h
+++ b/tools/lkl/include/lkl.h
@@ -350,6 +350,45 @@ const char *lkl_strerror(int err);
*/
void lkl_perror(char *msg, int err);
+/**
+ * struct lkl_dev_blk_ops - block device host operations, defined in lkl_host.h.
+ */
+struct lkl_dev_blk_ops;
+
+/**
+ * lkl_disk - host disk handle
+ *
+ * @dev - a pointer to 'virtio_blk_dev' structure for this disk
+ * @fd - a POSIX file descriptor that can be used by preadv/pwritev
+ * @handle - an NT file handle that can be used by ReadFile/WriteFile
+ */
+struct lkl_disk {
+ void *dev;
+ union {
+ int fd;
+ void *handle;
+ };
+ struct lkl_dev_blk_ops *ops;
+};
+
+/**
+ * lkl_disk_add - add a new disk
+ *
+ * @disk - the host disk handle
+ * @returns a disk id (0 is valid) or a strictly negative value in case of error
+ */
+int lkl_disk_add(struct lkl_disk *disk);
+
+/**
+ * lkl_disk_remove - remove a disk
+ *
+ * This function makes a cleanup of the @disk's virtio_dev structure
+ * that was initialized by lkl_disk_add before.
+ *
+ * @disk - the host disk handle
+ */
+int lkl_disk_remove(struct lkl_disk disk);
+
#ifdef __cplusplus
}
diff --git a/tools/lkl/include/lkl_host.h b/tools/lkl/include/lkl_host.h
index 81239e2b556f..a630efc95f0f 100644
--- a/tools/lkl/include/lkl_host.h
+++ b/tools/lkl/include/lkl_host.h
@@ -20,6 +20,63 @@ int lkl_printf(const char *fmt, ...);
extern char lkl_virtio_devs[4096];
+#ifdef LKL_HOST_CONFIG_POSIX
+#include <sys/uio.h>
+#else
+struct iovec {
+ void *iov_base;
+ size_t iov_len;
+};
+#endif
+
+extern struct lkl_dev_blk_ops lkl_dev_blk_ops;
+
+/**
+ * struct lkl_blk_req - block device request
+ *
+ * @type: type of request
+ * @prio: priority of request - currently unused
+ * @sector: offset in units 512 bytes for read / write requests
+ * @buf: an array of buffers to be used for read / write requests
+ * @count: the number of buffers
+ */
+struct lkl_blk_req {
+#define LKL_DEV_BLK_TYPE_READ 0
+#define LKL_DEV_BLK_TYPE_WRITE 1
+#define LKL_DEV_BLK_TYPE_FLUSH 4
+#define LKL_DEV_BLK_TYPE_FLUSH_OUT 5
+ unsigned int type;
+ unsigned int prio;
+ unsigned long long sector;
+ struct iovec *buf;
+ int count;
+};
+
+/**
+ * struct lkl_dev_blk_ops - block device host operations
+ */
+struct lkl_dev_blk_ops {
+ /**
+ * @get_capacity: returns the disk capacity in bytes
+ *
+ * @disk - the disk for which the capacity is requested;
+ * @res - pointer to receive the capacity, in bytes;
+ * @returns - 0 in case of success, negative value in case of error
+ */
+ int (*get_capacity)(struct lkl_disk disk, unsigned long long *res);
+#define LKL_DEV_BLK_STATUS_OK 0
+#define LKL_DEV_BLK_STATUS_IOERR 1
+#define LKL_DEV_BLK_STATUS_UNSUP 2
+ /**
+ * @request: issue a block request
+ *
+ * @disk - the disk the request is issued to;
+ * @req - a request described by &struct lkl_blk_req
+ */
+ int (*request)(struct lkl_disk disk, struct lkl_blk_req *req);
+};
+
+
#ifdef __cplusplus
}
#endif
diff --git a/tools/lkl/lib/Build b/tools/lkl/lib/Build
index 5fd1843b51d1..d3154cfa4952 100644
--- a/tools/lkl/lib/Build
+++ b/tools/lkl/lib/Build
@@ -3,6 +3,7 @@ CFLAGS_config.o += -I$(srctree)/tools/perf/pmu-events
liblkl-y += iomem.o
liblkl-y += jmp_buf.o
liblkl-y += utils.o
+liblkl-y += virtio_blk.o
liblkl-y += virtio.o
liblkl-y += dbg.o
liblkl-y += dbg_handler.o
diff --git a/tools/lkl/lib/virtio_blk.c b/tools/lkl/lib/virtio_blk.c
new file mode 100644
index 000000000000..9e23316c5d99
--- /dev/null
+++ b/tools/lkl/lib/virtio_blk.c
@@ -0,0 +1,132 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <lkl_host.h>
+#include "virtio.h"
+#include "endian.h"
+
+struct virtio_blk_dev {
+ struct virtio_dev dev;
+ struct lkl_virtio_blk_config config;
+ struct lkl_dev_blk_ops *ops;
+ struct lkl_disk disk;
+};
+
+struct virtio_blk_req_trailer {
+ uint8_t status;
+};
+
+static int blk_check_features(struct virtio_dev *dev)
+{
+ if (dev->driver_features == dev->device_features)
+ return 0;
+
+ return -LKL_EINVAL;
+}
+
+static int blk_enqueue(struct virtio_dev *dev, int q, struct virtio_req *req)
+{
+ struct virtio_blk_dev *blk_dev;
+ struct lkl_virtio_blk_outhdr *h;
+ struct virtio_blk_req_trailer *t;
+ struct lkl_blk_req lkl_req;
+
+ if (req->buf_count < 3) {
+ lkl_printf("virtio_blk: no status buf\n");
+ goto out;
+ }
+
+ h = req->buf[0].iov_base;
+ t = req->buf[req->buf_count - 1].iov_base;
+ blk_dev = container_of(dev, struct virtio_blk_dev, dev);
+
+ t->status = LKL_DEV_BLK_STATUS_IOERR;
+
+ if (req->buf[0].iov_len != sizeof(*h)) {
+ lkl_printf("virtio_blk: bad header buf\n");
+ goto out;
+ }
+
+ if (req->buf[req->buf_count - 1].iov_len != sizeof(*t)) {
+ lkl_printf("virtio_blk: bad status buf\n");
+ goto out;
+ }
+
+ lkl_req.type = le32toh(h->type);
+ lkl_req.prio = le32toh(h->ioprio);
+ lkl_req.sector = le32toh(h->sector);
+ lkl_req.buf = &req->buf[1];
+ lkl_req.count = req->buf_count - 2;
+
+ t->status = blk_dev->ops->request(blk_dev->disk, &lkl_req);
+
+out:
+ virtio_req_complete(req, 0);
+ return 0;
+}
+
+static struct virtio_dev_ops blk_ops = {
+ .check_features = blk_check_features,
+ .enqueue = blk_enqueue,
+};
+
+
+int lkl_disk_add(struct lkl_disk *disk)
+{
+ struct virtio_blk_dev *dev;
+ unsigned long long capacity;
+ int ret;
+
+ dev = lkl_host_ops.mem_alloc(sizeof(*dev));
+ if (!dev)
+ return -LKL_ENOMEM;
+
+ disk->dev = dev;
+
+ dev->dev.device_id = LKL_VIRTIO_ID_BLOCK;
+ dev->dev.vendor_id = 0;
+ dev->dev.device_features = 0;
+ dev->dev.config_gen = 0;
+ dev->dev.config_data = &dev->config;
+ dev->dev.config_len = sizeof(dev->config);
+ dev->dev.ops = &blk_ops;
+ if (disk->ops)
+ dev->ops = disk->ops;
+ else
+ dev->ops = &lkl_dev_blk_ops;
+ dev->disk = *disk;
+
+ ret = dev->ops->get_capacity(*disk, &capacity);
+ if (ret) {
+ ret = -LKL_ENOMEM;
+ goto out_free;
+ }
+ dev->config.capacity = capacity / 512;
+
+ ret = virtio_dev_setup(&dev->dev, 1, 32);
+ if (ret)
+ goto out_free;
+
+ return dev->dev.virtio_mmio_id;
+
+out_free:
+ lkl_host_ops.mem_free(dev);
+
+ return ret;
+}
+
+int lkl_disk_remove(struct lkl_disk disk)
+{
+ struct virtio_blk_dev *dev;
+ int ret;
+
+ dev = (struct virtio_blk_dev *)disk.dev;
+ if (!dev)
+ return -LKL_EINVAL;
+
+ ret = virtio_dev_cleanup(&dev->dev);
+ if (ret < 0)
+ return ret;
+
+ lkl_host_ops.mem_free(dev);
+
+ return 0;
+}
--
2.20.1 (Apple Git-117)
next prev parent reply other threads:[~2019-11-08 5:03 UTC|newest]
Thread overview: 75+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <cover.1571798507.git.thehajime@gmail.com>
2019-10-25 21:34 ` [RFC PATCH 00/47] Unifying LKL into UML Richard Weinberger
2019-10-27 2:34 ` Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 00/37] " Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 01/37] asm-generic: atomic64: allow using generic atomic64 on 64bit platforms Hajime Tazaki
2019-11-25 22:02 ` Richard Weinberger
2019-11-26 14:02 ` Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 02/37] arch: add __SYSCALL_DEFINE_ARCH Hajime Tazaki
2019-11-25 22:02 ` Richard Weinberger
2019-11-27 4:15 ` Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 03/37] lkl: architecture skeleton for Linux kernel library Hajime Tazaki
2019-11-25 22:00 ` Richard Weinberger
2019-11-26 11:42 ` Octavian Purdila
2019-11-26 14:17 ` Hajime Tazaki
2019-11-26 16:02 ` Richard Weinberger
2020-02-05 7:37 ` Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 04/37] lkl: host interface Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 05/37] lkl: memory handling Hajime Tazaki
2019-11-25 22:10 ` Richard Weinberger
2020-02-05 7:38 ` Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 06/37] lkl: kernel threads support Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 07/37] lkl: interrupt support Hajime Tazaki
2019-11-25 22:13 ` Richard Weinberger
2020-02-05 7:38 ` Hajime Tazaki
2020-02-05 10:49 ` Anton Ivanov
2020-02-05 14:24 ` Hajime Tazaki
2020-02-18 8:18 ` Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 08/37] lkl: system call interface and application API Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 09/37] lkl: timers, time and delay support Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 10/37] lkl: memory mapped I/O support Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 11/37] lkl: basic kernel console support Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 12/37] lkl: initialization and cleanup Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 13/37] lkl: plug in the build system Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 14/37] lkl tools: skeleton for host side library, tests and tools Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 15/37] lkl tools: host lib: add utilities functions Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 16/37] lkl tools: host lib: memory mapped I/O helpers Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 17/37] lkl tools: host lib: virtio devices Hajime Tazaki
2019-11-25 22:07 ` Richard Weinberger
2019-11-26 8:43 ` Johannes Berg
2019-11-26 8:50 ` Richard Weinberger
2019-11-26 8:52 ` Johannes Berg
2019-11-26 10:09 ` Richard Weinberger
2019-11-26 10:16 ` Johannes Berg
2019-11-26 10:42 ` Octavian Purdila
2019-11-26 10:49 ` Anton Ivanov
2019-11-27 4:06 ` Hajime Tazaki
2019-11-26 16:04 ` Richard Weinberger
2019-11-27 4:08 ` Hajime Tazaki
2019-11-27 14:28 ` Richard Weinberger
2019-11-28 9:53 ` Hajime Tazaki
2019-11-08 5:02 ` Hajime Tazaki [this message]
2019-11-08 5:02 ` [RFC v2 19/37] lkl tools: host lib: filesystem helpers Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 20/37] lkl tools: host lib: posix host operations Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 21/37] lkl tools: "boot" test Hajime Tazaki
2020-01-23 19:33 ` Brendan Higgins
2020-01-24 4:32 ` Hajime Tazaki
2020-03-02 19:51 ` Luis Chamberlain
2020-03-02 22:25 ` Brendan Higgins
2019-11-08 5:02 ` [RFC v2 22/37] lkl tools: tool that reads/writes to/from a filesystem image Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 23/37] lkl tools: tool that converts a filesystem image to tar Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 24/37] lkl tools: virtio: add network device support Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 25/37] checkpatch: avoid showing BIT_ULL warnings for tools/ files Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 26/37] tools: Add the lkl host library to the common tools Makefile Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 27/37] lkl tools: add lklfuse Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 28/37] lkl: add system call hijack support Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 29/37] lkl: add documentation Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 30/37] scripts: revert CONFIG_HAVE_UNDERSCORE_SYMBOL_PREFIX patches Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 31/37] lkl: add support for Windows hosts Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 32/37] lkl tools: add support for Windows host Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 33/37] kallsyms: Add a config option to select section for kallsyms Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 34/37] lkl: Android ARM (arm/arm64) support Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 35/37] um lkl: add CI tests Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 36/37] um: use lkl virtio_net_tap device as UML device Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 37/37] um: add lkl virtio-blk device Hajime Tazaki
2019-11-08 9:13 ` [RFC v2 00/37] Unifying LKL into UML Anton Ivanov
2019-11-08 11:17 ` Octavian Purdila
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=73d0e15d1429a089442c932d1b3fddc724a43bb6.1573179553.git.thehajime@gmail.com \
--to=thehajime@gmail.com \
--cc=linux-arch@vger.kernel.org \
--cc=linux-kernel-library@freelists.org \
--cc=linux-um@lists.infradead.org \
--cc=petrosagg@gmail.com \
--cc=retrage01@gmail.com \
--cc=sigmaepsilon92@gmail.com \
--cc=tavi.purdila@gmail.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;
as well as URLs for NNTP newsgroup(s).