Skip to contentSkip to conversation

kvm-unit-tests/arm: initial drop

PATCH Superseded Andrew Jones 2 participants 34 messages Jul 11, 2014 – Jul 15, 2014

Subsystems (4)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v6 cover kvm-unit-tests/arm: initial drop

This is a v6 of a series that introduces arm to kvm-unit-tests. As the tidy-up patches from v5 have already been merged (thanks!), these are just the remaining patches. Although there are several new patches as well, as virtio-testdev was dropped in favor of the chr-testdev backend[1]. The new patches are

06/17 Introduce alloc_ops
08/17 lib: add asm/page.h and virt_to_phys/phys_to_virt 09/17 virtio: add minimal support for virtqueues 10/17 Introduce chr-testdev
15/17 arm: Add arch-specific asm/page.h and __va/__pa

and of course "Introduce virtio-testdev" was dropped.

So, this series first adds support for device trees (libfdt), and for chr-testdev (virtio). Next, it adds the basic infrastructure for booting a test case (guest), and adds a first test case, a self-test to confirm setup was completed successfully. Finally, it further prepares the framework for more complicated tests by adding vector support, and extends the self-test to test that too.

This initial drop doesn't require kvmarm. qemu-system-arm is enough, but qemu must have mach-virt, and the chr-testdev patch[1].

These patches (v6) are also available from a git repo here
https://github.com/rhdrjones/kvm-unit-tests/commits/arm/v6-initial-drop%2Cchr-testdev

The v5 patches are available for reference here
https://github.com/rhdrjones/kvm-unit-tests/commits/arm/v5-initial-drop

Almost all changes since v5 are for the virtio-testdev to chr-testdev switch. To see just the changes made for v6 that are unrelated to the testdev switch, then look here[2], which is an interdiff between v5 and a "v6" that does not include the testdev switch. Likewise to see just the changes made for the switch, look here[3] at an interdiff between "v6" and the real v6 "v6,chr-testdev". All details of patch changes are in their own changelogs. Note, even though there were some bigger changes needed for the testdev switch, I kept Christoffer's *-by's. Mainly the already Reviewed-by patches

07/17 add minimal virtio support for devtree virtio-mmio 12/17 arm: initial drop

should get a second look (or interdiffed).

Thanks in advance for reviews!

[1] http://lists.nongnu.org/archive/html/qemu-devel/2014-07/msg01960.html
[2] http://fpaste.org/117172/
[3] http://fpaste.org/117173/

Andrew Jones (15):
libfdt: Import libfdt source
libfdt: get libfdt to build
add support for Linux device trees
libcflat: add abort() and assert() Introduce asm-generic/*.h files
Introduce alloc_ops
add minimal virtio support for devtree virtio-mmio
lib: add asm/page.h and virt_to_phys/phys_to_virt
virtio: add minimal support for virtqueues Introduce chr-testdev
libcflat: clean up libcflat.h
arm: initial drop
arm: Add arch-specific asm/page.h and __va/__pa
arm: add useful headers from the Linux kernel
arm: vectors support

Christoffer Dall (2):
arm: Add spinlock implementation
arm: Add IO accessors to avoid register-writeback

 .gitignore                   |    1 +
 Makefile                     |   26 +-
 arm/cstart.S                 |  209 ++++++
 arm/flat.lds                 |   23 +
 arm/run                      |   46 ++
 arm/selftest.c               |  214 ++++++
 arm/unittests.cfg            |   30 +
 config/asm-offsets.mak       |   41 ++
 config/config-arm.mak        |   80 +++
 configure                    |   23 +-
 lib/abort.c                  |   20 +
 lib/alloc.c                  |    2 +
 lib/alloc.h                  |   31 +
 lib/argv.c                   |    6 +
 lib/arm/.gitignore           |    1 +
 lib/arm/asm-offsets.c        |   39 ++
 lib/arm/asm/asm-offsets.h    |    1 +
 lib/arm/asm/barrier.h        |   18 +
 lib/arm/asm/cp15.h           |   37 ++
 lib/arm/asm/io.h             |   94 +++
 lib/arm/asm/page.h           |   33 +
 lib/arm/asm/processor.h      |   39 ++
 lib/arm/asm/ptrace.h         |  100 +++
 lib/arm/asm/setup.h          |   63 ++
 lib/arm/asm/spinlock.h       |   11 +
 lib/arm/eabi_compat.c        |   20 +
 lib/arm/io.c                 |   65 ++
 lib/arm/processor.c          |  112 ++++
 lib/arm/setup.c              |  188 ++++++
 lib/arm/spinlock.c           |   28 +
 lib/asm-generic/io.h         |  175 +++++
 lib/asm-generic/page.h       |   28 +
 lib/asm-generic/spinlock.h   |    4 +
 lib/chr-testdev.c            |   72 ++
 lib/chr-testdev.h            |   14 +
 lib/devicetree.c             |  272 ++++++++
 lib/devicetree.h             |  236 +++++++
 lib/generated/.gitignore     |    1 +
 lib/libcflat.h               |   59 +-
 lib/libfdt/Makefile.libfdt   |   10 +
 lib/libfdt/README            |    4 +
 lib/libfdt/fdt.c             |  250 +++++++
 lib/libfdt/fdt.h             |  111 ++++
 lib/libfdt/fdt_empty_tree.c  |   84 +++
 lib/libfdt/fdt_ro.c          |  573 ++++++++++++++++
 lib/libfdt/fdt_rw.c          |  492 ++++++++++++++
 lib/libfdt/fdt_strerror.c    |   96 +++
 lib/libfdt/fdt_sw.c          |  256 +++++++
 lib/libfdt/fdt_wip.c         |  118 ++++
 lib/libfdt/libfdt.h          | 1514 ++++++++++++++++++++++++++++++++++++++++++
 lib/libfdt/libfdt_env.h      |  111 ++++
 lib/libfdt/libfdt_internal.h |   95 +++
 lib/libfdt/version.lds       |   60 ++
 lib/string.c                 |   54 ++
 lib/string.h                 |   15 +
 lib/virtio.c                 |  335 ++++++++++
 lib/virtio.h                 |  194 ++++++
 57 files changed, 6801 insertions(+), 33 deletions(-)
 create mode 100644 arm/cstart.S
 create mode 100644 arm/flat.lds
 create mode 100755 arm/run
 create mode 100644 arm/selftest.c
 create mode 100644 arm/unittests.cfg
 create mode 100644 config/asm-offsets.mak
 create mode 100644 config/config-arm.mak
 create mode 100644 lib/abort.c
 create mode 100644 lib/alloc.c
 create mode 100644 lib/alloc.h
 create mode 100644 lib/arm/.gitignore
 create mode 100644 lib/arm/asm-offsets.c
 create mode 100644 lib/arm/asm/asm-offsets.h
 create mode 100644 lib/arm/asm/barrier.h
 create mode 100644 lib/arm/asm/cp15.h
 create mode 100644 lib/arm/asm/io.h
 create mode 100644 lib/arm/asm/page.h
 create mode 100644 lib/arm/asm/processor.h
 create mode 100644 lib/arm/asm/ptrace.h
 create mode 100644 lib/arm/asm/setup.h
 create mode 100644 lib/arm/asm/spinlock.h
 create mode 100644 lib/arm/eabi_compat.c
 create mode 100644 lib/arm/io.c
 create mode 100644 lib/arm/processor.c
 create mode 100644 lib/arm/setup.c
 create mode 100644 lib/arm/spinlock.c
 create mode 100644 lib/asm-generic/io.h
 create mode 100644 lib/asm-generic/page.h
 create mode 100644 lib/asm-generic/spinlock.h
 create mode 100644 lib/chr-testdev.c
 create mode 100644 lib/chr-testdev.h
 create mode 100644 lib/devicetree.c
 create mode 100644 lib/devicetree.h
 create mode 100644 lib/generated/.gitignore
 create mode 100644 lib/libfdt/Makefile.libfdt
 create mode 100644 lib/libfdt/README
 create mode 100644 lib/libfdt/fdt.c
 create mode 100644 lib/libfdt/fdt.h
 create mode 100644 lib/libfdt/fdt_empty_tree.c
 create mode 100644 lib/libfdt/fdt_ro.c
 create mode 100644 lib/libfdt/fdt_rw.c
 create mode 100644 lib/libfdt/fdt_strerror.c
 create mode 100644 lib/libfdt/fdt_sw.c
 create mode 100644 lib/libfdt/fdt_wip.c
 create mode 100644 lib/libfdt/libfdt.h
 create mode 100644 lib/libfdt/libfdt_env.h
 create mode 100644 lib/libfdt/libfdt_internal.h
 create mode 100644 lib/libfdt/version.lds
 create mode 100644 lib/string.h
 create mode 100644 lib/virtio.c
 create mode 100644 lib/virtio.h
Signature · 1 line
1.9.3
#1 lore raw

PATCH v6 2/17 libfdt: get libfdt to build

Add string functions needed for libfdt, and add a make target for it. Also split string function declarations out to own string.h. While at it, add strcpy.

---
v6: split out string.h [Paolo Bonzini]
v4: rewrite strchr w/out use of strlen, drop libfdt_env.h modifications

---
 Makefile       | 21 +++++++++++++++++++--
 lib/libcflat.h |  8 +-------
 lib/string.c   | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/string.h   | 15 +++++++++++++++
 4 files changed, 89 insertions(+), 9 deletions(-)
 create mode 100644 lib/string.h
Makefile +19 -2
index 32ae9a59db5b0..fba58e36f272f 100644
@@ -21,6 +21,13 @@ cflatobjs := \
 	lib/string.o \
 	lib/report.o
 
+# libfdt paths
+LIBFDT_objdir = lib/libfdt
+LIBFDT_srcdir = lib/libfdt
+LIBFDT_archive = $(LIBFDT_objdir)/libfdt.a
+LIBFDT_include = $(addprefix $(LIBFDT_srcdir)/,$(LIBFDT_INCLUDES))
+LIBFDT_version = $(addprefix $(LIBFDT_srcdir)/,$(LIBFDT_VERSION))
+
 #include architecure specific make rules
 include config/config-$(ARCH).mak
 
@@ -46,6 +53,11 @@ LDFLAGS += -pthread -lrt
 $(libcflat): $(cflatobjs)
 	$(AR) rcs $@ $^
 
+include $(LIBFDT_srcdir)/Makefile.libfdt
+$(LIBFDT_archive): CFLAGS += -ffreestanding -I lib -I lib/libfdt -Wno-sign-compare
+$(LIBFDT_archive): $(addprefix $(LIBFDT_objdir)/,$(LIBFDT_OBJS))
+	$(AR) rcs $@ $^
+
 %.o: %.S
 	$(CC) $(CFLAGS) -c -nostdlib -o $@ $<
 
@@ -58,10 +70,15 @@ install:
 clean: arch_clean
 	$(RM) lib/.*.d $(libcflat) $(cflatobjs)
 
-distclean: clean
+libfdt_clean:
+	$(RM) $(LIBFDT_archive) \
+	$(addprefix $(LIBFDT_objdir)/,$(LIBFDT_OBJS)) \
+	$(LIBFDT_objdir)/.*.d
+
+distclean: clean libfdt_clean
 	$(RM) config.mak $(TEST_DIR)-run test.log msr.out cscope.*
 
-cscope: common_dirs = lib
+cscope: common_dirs = lib lib/libfdt
 cscope:
 	$(RM) ./cscope.*
 	find $(TEST_DIR) lib/$(TEST_DIR) $(common_dirs) -maxdepth 1 \
lib/libcflat.h +1 -7
index c9577350ec275..5acfee2d9818a 100644
@@ -21,6 +21,7 @@
 #define __LIBCFLAT_H
 
 #include <stdarg.h>
+#include <string.h>
 
 #define xstr(s) xxstr(s)
 #define xxstr(s) #s
@@ -42,19 +43,12 @@ typedef _Bool bool;
 
 extern void exit(int code);
 
-extern unsigned long strlen(const char *buf);
-extern char *strcat(char *dest, const char *src);
-extern int strcmp(const char *a, const char *b);
-
 extern int printf(const char *fmt, ...);
 extern int snprintf(char *buf, int size, const char *fmt, ...);
 extern int vsnprintf(char *buf, int size, const char *fmt, va_list va);
 
 extern void puts(const char *s);
 
-extern void *memset(void *s, int c, size_t n);
-extern void *memcpy(void *dest, const void *src, size_t n);
-
 extern long atol(const char *ptr);
 #define ARRAY_SIZE(_a)  (sizeof(_a)/sizeof((_a)[0]))
 
lib/string.c +54 -0

72 lines not on this page · raw message

lib/string.h new file +15 -0
new file mode 100644
index 0000000000000..dbab368b1b9e4
@@ -0,0 +1,15 @@
+#ifndef __STRING_H
+#define __STRING_H
+
+extern unsigned long strlen(const char *buf);
+extern char *strcat(char *dest, const char *src);
+extern char *strcpy(char *dest, const char *src);
+extern int strcmp(const char *a, const char *b);
+extern char *strchr(const char *s, int c);
+extern void *memset(void *s, int c, size_t n);
+extern void *memcpy(void *dest, const void *src, size_t n);
+extern int memcmp(const void *s1, const void *s2, size_t n);
+extern void *memmove(void *dest, const void *src, size_t n);
+extern void *memchr(const void *s, int c, size_t n);
+
+#endif /* _STRING_H */
Signature · 1 line
1.9.3
#2 lore raw

PATCH v6 3/17 add support for Linux device trees

In reply to Andrew Jones

Add some device tree functions, built on libfdt, to the arch-neutral lib code in order to facilitate the extraction of boot info and device base addresses. These functions should work on device trees conforming to section III of the kernel doc Documentation/devicetree/booting-without-of.txt.

---
v5:
- changed *get_baseaddr* helpers to *get_base* helpers
- a couple minor code changes [Christoffer Dall]
v4: reworked everything, added lots of comments to devicetree.h

---
 lib/devicetree.c | 272 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/devicetree.h | 236 +++++++++++++++++++++++++++++++++++++++++++++++
 lib/libcflat.h   |   2 +
 3 files changed, 510 insertions(+)
 create mode 100644 lib/devicetree.c
 create mode 100644 lib/devicetree.h
lib/devicetree.c new file +272 -0
new file mode 100644
index 0000000000000..57e7408f4c5a9
@@ -0,0 +1,272 @@
+/*
+ * Copyright (C) 2014, Red Hat Inc, Andrew Jones <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.
+ */
+#include "libcflat.h"
+#include "libfdt/libfdt.h"
+#include "devicetree.h"
+
+static const void *fdt;
+static u32 root_nr_address_cells, root_nr_size_cells;
+
+const void *dt_fdt(void)
+{
+	return fdt;
+}
+
+bool dt_available(void)
+{
+	return fdt_check_header(fdt) == 0;
+}
+
+int dt_get_nr_cells(int fdtnode, u32 *nr_address_cells, u32 *nr_size_cells)
+{
+	const struct fdt_property *prop;
+	u32 *nr_cells;
+	int len;
+
+	prop = fdt_get_property(fdt, fdtnode, "#address-cells", &len);
+	if (prop == NULL)
+		return len;
+
+	nr_cells = (u32 *)prop->data;
+	*nr_address_cells = fdt32_to_cpu(*nr_cells);
+
+	prop = fdt_get_property(fdt, fdtnode, "#size-cells", &len);
+	if (prop == NULL)
+		return len;
+
+	nr_cells = (u32 *)prop->data;
+	*nr_size_cells = fdt32_to_cpu(*nr_cells);
+
+	return 0;
+}
+
+void dt_reg_init(struct dt_reg *reg, u32 nr_address_cells, u32 nr_size_cells)
+{
+	memset(reg, 0, sizeof(struct dt_reg));
+	reg->nr_address_cells = nr_address_cells;
+	reg->nr_size_cells = nr_size_cells;
+}
+
+int dt_get_reg(int fdtnode, int regidx, struct dt_reg *reg)
+{
+	const struct fdt_property *prop;
+	u32 *cells, i;
+	unsigned nr_tuple_cells;
+	int len;
+
+	prop = fdt_get_property(fdt, fdtnode, "reg", &len);
+	if (prop == NULL)
+		return len;
+
+	cells = (u32 *)prop->data;
+	nr_tuple_cells = reg->nr_address_cells + reg->nr_size_cells;
+	regidx *= nr_tuple_cells;
+
+	if (regidx + nr_tuple_cells > len/sizeof(u32))
+		return -FDT_ERR_NOTFOUND;
+
+	for (i = 0; i < reg->nr_address_cells; ++i)
+		reg->address_cells[i] = fdt32_to_cpu(cells[regidx + i]);
+
+	regidx += reg->nr_address_cells;
+	for (i = 0; i < reg->nr_size_cells; ++i)
+		reg->size_cells[i] = fdt32_to_cpu(cells[regidx + i]);
+
+	return 0;
+}
+
+int dt_pbus_translate_node(int fdtnode, int regidx,
+			   struct dt_pbus_reg *pbus_reg)
+{
+	struct dt_reg raw_reg;
+	int ret;
+
+	dt_reg_init(&raw_reg, root_nr_address_cells, root_nr_size_cells);
+
+	ret = dt_get_reg(fdtnode, regidx, &raw_reg);
+	if (ret < 0)
+		return ret;
+
+	pbus_reg->addr = dt_pbus_read_cells(raw_reg.nr_address_cells,
+					    raw_reg.address_cells);
+	pbus_reg->size = dt_pbus_read_cells(raw_reg.nr_size_cells,
+					    raw_reg.size_cells);
+
+	return 0;
+}
+
+int dt_pbus_translate(const struct dt_device *dev, int regidx,
+		      void *reg)
+{
+	return dt_pbus_translate_node(dev->fdtnode, regidx, reg);
+}
+
+int dt_bus_match_any(const struct dt_device *dev __unused, int fdtnode)
+{
+	/* matches any device with a valid node */
+	return fdtnode < 0 ? fdtnode : 1;
+}
+
+static const struct dt_bus dt_default_bus = {
+	.match = dt_bus_match_any,
+	.translate = dt_pbus_translate,
+};
+
+void dt_bus_init_defaults(struct dt_bus *bus)
+{
+	memcpy(bus, &dt_default_bus, sizeof(struct dt_bus));
+}
+
+void dt_device_init(struct dt_device *dev, const struct dt_bus *bus,
+		    const void *info)
+{
+	memset(dev, 0, sizeof(struct dt_device));
+	dev->bus = bus;
+	dev->info = (void *)info;
+}
+
+int dt_device_find_compatible(const struct dt_device *dev,
+			      const char *compatible)
+{
+	int node, ret;
+
+	node = fdt_node_offset_by_compatible(fdt, -1, compatible);
+	while (node >= 0) {
+		ret = dev->bus->match(dev, node);
+		if (ret < 0)
+			return ret;
+		else if (ret)
+			break;
+		node = fdt_node_offset_by_compatible(fdt, node, compatible);
+	}
+	return node;
+}
+
+int dt_pbus_get_base_compatible(const char *compatible,
+				struct dt_pbus_reg *base)
+{
+	struct dt_device dev;
+	int node;
+
+	dt_device_init(&dev, &dt_default_bus, NULL);
+
+	node = dt_device_find_compatible(&dev, compatible);
+	if (node < 0)
+		return node;
+
+	dt_device_bind_node(&dev, node);
+
+	return dt_pbus_get_base(&dev, base);
+}
+
+int dt_get_memory_params(struct dt_pbus_reg *regs, int nr_regs)
+{
+	const char *pn = "device_type", *pv = "memory";
+	int node, ret, pl = strlen(pv) + 1, nr = 0;
+	struct dt_pbus_reg reg;
+
+	node = fdt_node_offset_by_prop_value(fdt, -1, pn, pv, pl);
+
+	while (node >= 0) {
+
+		while (nr < nr_regs) {
+			ret = dt_pbus_translate_node(node, nr, &reg);
+			if (ret == -FDT_ERR_NOTFOUND)
+				break;
+			if (ret < 0)
+				return ret;
+			regs[nr].addr = reg.addr;
+			regs[nr].size = reg.size;
+			++nr;
+		}
+
+		node = fdt_node_offset_by_prop_value(fdt, node, pn, pv, pl);
+	}
+
+	return node != -FDT_ERR_NOTFOUND ? node : nr;
+}
+
+int dt_for_each_cpu_node(void (*func)(int fdtnode, u32 regval, void *info),
+			 void *info)
+{
+	const struct fdt_property *prop;
+	int cpus, cpu, ret, len;
+	struct dt_reg raw_reg;
+	u32 nac, nsc;
+
+	cpus = fdt_path_offset(fdt, "/cpus");
+	if (cpus < 0)
+		return cpus;
+
+	ret = dt_get_nr_cells(cpus, &nac, &nsc);
+	if (ret < 0)
+		return ret;
+
+	dt_reg_init(&raw_reg, nac, nsc);
+
+	dt_for_each_subnode(cpus, cpu) {
+
+		prop = fdt_get_property(fdt, cpu, "device_type", &len);
+		if (prop == NULL)
+			return len;
+
+		if (len != 4 || strcmp((char *)prop->data, "cpu"))
+			continue;
+
+		ret = dt_get_reg(cpu, 0, &raw_reg);
+		if (ret < 0)
+			return ret;
+
+		func(cpu, raw_reg.address_cells[0], info);
+	}
+
+	return 0;
+}
+
+int dt_get_bootargs(const char **bootargs)
+{
+	const struct fdt_property *prop;
+	int node, len;
+
+	*bootargs = NULL;
+
+	node = fdt_path_offset(fdt, "/chosen");
+	if (node < 0)
+		return node;
+
+	prop = fdt_get_property(fdt, node, "bootargs", &len);
+	if (prop)
+		*bootargs = (char *)prop->data;
+	else if (len < 0 && len != -FDT_ERR_NOTFOUND)
+		return len;
+
+	return 0;
+}
+
+int dt_init(const void *fdt_ptr)
+{
+	struct dt_bus *defbus = (struct dt_bus *)&dt_default_bus;
+	int root, ret;
+
+	ret = fdt_check_header(fdt_ptr);
+	if (ret < 0)
+		return ret;
+	fdt = fdt_ptr;
+
+	root = fdt_path_offset(fdt, "/");
+	if (root < 0)
+		return root;
+
+	ret = dt_get_nr_cells(root, &root_nr_address_cells,
+				    &root_nr_size_cells);
+	if (ret < 0)
+		return ret;
+
+	defbus->nr_address_cells = root_nr_address_cells;
+	defbus->nr_size_cells = root_nr_size_cells;
+
+	return 0;
+}
lib/devicetree.h new file +236 -0

236 lines not on this page · raw message

lib/libcflat.h +2 -0
index 5acfee2d9818a..c9754695326df 100644
@@ -23,6 +23,8 @@
 #include <stdarg.h>
 #include <string.h>
 
+#define __unused __attribute__((__unused__))
+
 #define xstr(s) xxstr(s)
 #define xxstr(s) #s
 
Signature · 1 line
1.9.3
#3 lore raw

PATCH v6 4/17 libcflat: add abort() and assert()

In reply to Andrew Jones

The test framework may have external dependencies. assert() provides the ability to abort when those dependencies aren't met. However, assert() should only be used for unlikely conditions. We can provide more informative messages with printf() for the more likely problems.

---
v6: change abort() from macro to function [Paolo Bonzini]
don't use signal ambiguous status codes [Paolo Bonzini]

---
 Makefile       |  1 +
 lib/abort.c    | 20 ++++++++++++++++++++
 lib/libcflat.h |  9 +++++++++
 3 files changed, 30 insertions(+)
 create mode 100644 lib/abort.c
Makefile +1 -0
index fba58e36f272f..180189ecd6d8c 100644
@@ -19,6 +19,7 @@ cflatobjs := \
 	lib/argv.o \
 	lib/printf.o \
 	lib/string.o \
+	lib/abort.o \
 	lib/report.o
 
 # libfdt paths
lib/abort.c new file +20 -0
new file mode 100644
index 0000000000000..61f7f924aba4b
@@ -0,0 +1,20 @@
+/*
+ * Copyright (C) 2014, Red Hat Inc, Andrew Jones <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.
+ */
+#include "libcflat.h"
+
+/*
+ * When exit(code) is invoked, qemu will exit with ((code << 1) | 1),
+ * leaving us 128 exit status codes. To avoid confusion with signal
+ * status, we further limit exit codes to those resulting in qemu
+ * exiting with a status < 128. We give abort() the highest (127),
+ * leaving the lower status codes for unit tests.
+ */
+#define ABORT_EXIT_STATUS 63	/* 127 exit status from qemu */
+
+void abort(void)
+{
+	exit(ABORT_EXIT_STATUS);
+}
lib/libcflat.h +9 -0
index c9754695326df..9f76d6741344d 100644
@@ -44,6 +44,7 @@ typedef _Bool bool;
 #define false 0
 
 extern void exit(int code);
+extern void abort(void);
 
 extern int printf(const char *fmt, ...);
 extern int snprintf(char *buf, int size, const char *fmt, ...);
@@ -61,4 +62,12 @@ extern long atol(const char *ptr);
 void report(const char *msg_fmt, bool pass, ...);
 void report_xfail(const char *msg_fmt, bool xfail, bool pass, ...);
 int report_summary(void);
+
+#define assert(cond)							\
+do {									\
+	if (!(cond))							\
+		printf("%s:%d: assert failed\n", __FILE__, __LINE__),	\
+		abort();						\
+} while (0)
+
 #endif
Signature · 1 line
1.9.3
#4 lore raw

PATCH v6 5/17 Introduce asm-generic/*.h files

In reply to Andrew Jones

Architecture neutral code may need to call low-level io accessors, or use spinlocks. Create a generic io.h to ensure those accessors are defined, and a generic spinlock.h that complains when included, as we can't write a generic spinlock. These files can be overridden or extended by architecture specific versions placed in lib/$ARCH/asm/.

---
v5: added a trivial ioremap function [Christoffer Dall]
v4: introduce lib/asm symlink to get rid of #ifdef __arm__,
add spinlock.h too
v3: wrt to io.h (was libio.[ch]) only
- get rid of CONFIG_64BIT, replace with asserts
- get rid of {read,write}_len() [libio.c]
- fix bad *64_to_cpu macros

---
 .gitignore                 |   1 +
 Makefile                   |   6 +-
 configure                  |  11 +++
 lib/asm-generic/io.h       | 162 +++++++++++++++++++++++++++++++++++++++++++++
 lib/asm-generic/spinlock.h |   4 ++
 5 files changed, 181 insertions(+), 3 deletions(-)
 create mode 100644 lib/asm-generic/io.h
 create mode 100644 lib/asm-generic/spinlock.h
.gitignore +1 -0
index 775d0dfd8263e..e21939a8771e9 100644
@@ -9,6 +9,7 @@ patches
 .stgit-*
 cscope.*
 *.swp
+/lib/asm
 /config.mak
 /*-run
 /test.log
Makefile +3 -3
index 180189ecd6d8c..dd7e6e94bfe7b 100644
@@ -77,11 +77,11 @@ libfdt_clean:
 	$(LIBFDT_objdir)/.*.d
 
 distclean: clean libfdt_clean
-	$(RM) config.mak $(TEST_DIR)-run test.log msr.out cscope.*
+	$(RM) lib/asm config.mak $(TEST_DIR)-run test.log msr.out cscope.*
 
-cscope: common_dirs = lib lib/libfdt
+cscope: common_dirs = lib lib/libfdt lib/asm lib/asm-generic
 cscope:
 	$(RM) ./cscope.*
-	find $(TEST_DIR) lib/$(TEST_DIR) $(common_dirs) -maxdepth 1 \
+	find -L $(TEST_DIR) lib/$(TEST_DIR) $(common_dirs) -maxdepth 1 \
 		-name '*.[chsS]' -print | sed 's,^\./,,' > ./cscope.files
 	cscope -bk
configure +11 -0
index dbbc6045d214a..aaa1b50ab1b98 100755
@@ -91,6 +91,17 @@ if [ $exit -eq 0 ]; then
 fi
 rm -f lib_test.c
 
+# link lib/asm for the architecture
+rm -f lib/asm
+asm=asm-generic
+if [ -d lib/$arch/asm ]; then
+	asm=$arch/asm
+elif [ -d lib/$testdir/asm ]; then
+	asm=$testdir/asm
+fi
+ln -s $asm lib/asm
+
+# create the config
 cat <<EOF > config.mak
 PREFIX=$prefix
 KERNELDIR=$(readlink -f $kerneldir)
lib/asm-generic/io.h new file +162 -0

162 lines not on this page · raw message

lib/asm-generic/spinlock.h new file +4 -0
new file mode 100644
index 0000000000000..31417442e3330
@@ -0,0 +1,4 @@
+#ifndef _ASM_GENERIC_SPINLOCK_H_
+#define _ASM_GENERIC_SPINLOCK_H_
+#error need architecture specific asm/spinlock.h
+#endif
Signature · 1 line
1.9.3
#5 lore raw

PATCH v6 6/17 Introduce alloc_ops

In reply to Andrew Jones

alloc_ops provide interfaces for alloc(), free() and friends, allowing unit tests and common code to use dynamic memory allocation. arch-specific code must provide the implementations.

---
 lib/alloc.c |  2 ++
 lib/alloc.h | 31 +++++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+)
 create mode 100644 lib/alloc.c
 create mode 100644 lib/alloc.h
lib/alloc.c new file +2 -0
new file mode 100644
index 0000000000000..868664b4dcaa3
@@ -0,0 +1,2 @@
+#include "alloc.h"
+struct alloc_ops alloc_ops;
lib/alloc.h new file +31 -0
new file mode 100644
index 0000000000000..c8cd61b387a9a
@@ -0,0 +1,31 @@
+#ifndef _ALLOC_H_
+#define _ALLOC_H_
+#include "libcflat.h"
+
+struct alloc_ops {
+	void *(*alloc)(size_t size);
+	void *(*alloc_aligned)(size_t size, size_t align);
+	void (*free)(const void *addr);
+};
+
+extern struct alloc_ops alloc_ops;
+
+static inline void *alloc(size_t size)
+{
+	assert(alloc_ops.alloc);
+	return alloc_ops.alloc(size);
+}
+
+static inline void *alloc_aligned(size_t size, size_t align)
+{
+	assert(alloc_ops.alloc_aligned);
+	return alloc_ops.alloc_aligned(size, align);
+}
+
+static inline void free(const void *addr)
+{
+	assert(alloc_ops.free);
+	alloc_ops.free(addr);
+}
+
+#endif
Signature · 1 line
1.9.3
maintainer
#6 lore raw

Re: [PATCH v6 06/17] Introduce alloc_ops

Il 11/07/2014 10:19, Andrew Jones ha scritto:

Quoted 48 lines

alloc_ops provide interfaces for alloc(), free() and friends, allowing unit tests and common code to use dynamic memory allocation. arch-specific code must provide the implementations.

---
 lib/alloc.c |  2 ++
 lib/alloc.h | 31 +++++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+)
 create mode 100644 lib/alloc.c
 create mode 100644 lib/alloc.h
lib/alloc.c new file +2 -0
new file mode 100644
index 0000000000000..868664b4dcaa3
@@ -0,0 +1,2 @@
+#include "alloc.h"
+struct alloc_ops alloc_ops;
lib/alloc.h new file +21 -0
new file mode 100644
index 0000000000000..c8cd61b387a9a
@@ -0,0 +1,31 @@
+#ifndef _ALLOC_H_
+#define _ALLOC_H_
+#include "libcflat.h"
+
+struct alloc_ops {
+	void *(*alloc)(size_t size);
+	void *(*alloc_aligned)(size_t size, size_t align);
+	void (*free)(const void *addr);
+};
+
+extern struct alloc_ops alloc_ops;
+
+static inline void *alloc(size_t size)
+{
+	assert(alloc_ops.alloc);
+	return alloc_ops.alloc(size);
+}
+
+static inline void *alloc_aligned(size_t size, size_t align)
+{
+	assert(alloc_ops.alloc_aligned);

lib/alloc.h line 31

Quoted 10 lines
lib/alloc.h new file +10 -0
+	return alloc_ops.alloc_aligned(size, align);
+}
+
+static inline void free(const void *addr)
+{
+	assert(alloc_ops.free);
+	alloc_ops.free(addr);
+}
+
+#endif

Why do you need the wrappers? Could you just have lib/malloc.c define the three functions, with interface in lib/stdlib.h and lib/memregion.h?

Also, please call them with the "right" C names:

       void *malloc(size_t size);
       void free(void *ptr);
       void *calloc(size_t nmemb, size_t size);
       void *memalign(size_t alignment, size_t size);

Only calloc should do the memset.

Later, x86 could also add a memregion_init call and be able to use malloc/free.

Paolo

#7 lore raw

Re: [PATCH v6 06/17] Introduce alloc_ops

Quoted 53 lines · Paolo Bonzini

Il 11/07/2014 10:19, Andrew Jones ha scritto:

Quoted 52 lines

alloc_ops provide interfaces for alloc(), free() and friends, allowing unit tests and common code to use dynamic memory allocation. arch-specific code must provide the implementations.

---
lib/alloc.c | 2 ++
lib/alloc.h | 31 +++++++++++++++++++++++++++++++

2 files changed, 33 insertions(+)

create mode 100644 lib/alloc.c
create mode 100644 lib/alloc.h

lib/alloc.c new file +2 -0
new file mode 100644
index 0000000000000..868664b4dcaa3
@@ -0,0 +1,2 @@
+#include "alloc.h"
+struct alloc_ops alloc_ops;
lib/alloc.h new file +26 -0
new file mode 100644
index 0000000000000..c8cd61b387a9a
@@ -0,0 +1,31 @@
+#ifndef _ALLOC_H_
+#define _ALLOC_H_
+#include "libcflat.h"
+
+struct alloc_ops {
+	void *(*alloc)(size_t size);
+	void *(*alloc_aligned)(size_t size, size_t align);
+	void (*free)(const void *addr);
+};
+
+extern struct alloc_ops alloc_ops;
+
+static inline void *alloc(size_t size)
+{
+	assert(alloc_ops.alloc);
+	return alloc_ops.alloc(size);
+}
+
+static inline void *alloc_aligned(size_t size, size_t align)
+{
+	assert(alloc_ops.alloc_aligned);
+	return alloc_ops.alloc_aligned(size, align);
+}
+
+static inline void free(const void *addr)
+{
Quoted 8 lines

lib/alloc.h line 31

Quoted 6 lines
lib/alloc.h new file +5 -0
+	assert(alloc_ops.free);
+	alloc_ops.free(addr);
+}
+
+#endif

Why do you need the wrappers?

A unit test may want to change the allocator after setting up paging.

Quoted 2 lines

Could you just have lib/malloc.c define the
three functions, with interface in lib/stdlib.h and lib/memregion.h?

If memregions look useful outside of arm, then I can certainly move them and early_[m]alloc, etc. to common code.

Quoted 9 lines

Also, please call them with the "right" C names:

       void *malloc(size_t size);
       void free(void *ptr);
       void *calloc(size_t nmemb, size_t size);
       void *memalign(size_t alignment, size_t size);

Only calloc should do the memset.

Sure, but I still think they should just be wrappers. Initial implementations based on memregions would of course be named something else. I guess just "early_*" or "memregion_*".

Quoted 3 lines

Later, x86 could also add a memregion_init call and be able to use malloc/free.

sounds good.

drew

maintainer
#8 lore raw

Re: [PATCH v6 06/17] Introduce alloc_ops

Il 11/07/2014 10:55, Andrew Jones ha scritto:

Quoted 63 lines · Paolo Bonzini
Quoted 53 lines

Il 11/07/2014 10:19, Andrew Jones ha scritto:

Quoted 52 lines

alloc_ops provide interfaces for alloc(), free() and friends, allowing unit tests and common code to use dynamic memory allocation. arch-specific code must provide the implementations.

---
lib/alloc.c | 2 ++
lib/alloc.h | 31 +++++++++++++++++++++++++++++++

2 files changed, 33 insertions(+)

create mode 100644 lib/alloc.c
create mode 100644 lib/alloc.h

lib/alloc.c new file +2 -0
new file mode 100644
index 0000000000000..868664b4dcaa3
@@ -0,0 +1,2 @@
+#include "alloc.h"
+struct alloc_ops alloc_ops;
lib/alloc.h new file +26 -0
new file mode 100644
index 0000000000000..c8cd61b387a9a
@@ -0,0 +1,31 @@
+#ifndef _ALLOC_H_
+#define _ALLOC_H_
+#include "libcflat.h"
+
+struct alloc_ops {
+	void *(*alloc)(size_t size);
+	void *(*alloc_aligned)(size_t size, size_t align);
+	void (*free)(const void *addr);
+};
+
+extern struct alloc_ops alloc_ops;
+
+static inline void *alloc(size_t size)
+{
+	assert(alloc_ops.alloc);
+	return alloc_ops.alloc(size);
+}
+
+static inline void *alloc_aligned(size_t size, size_t align)
+{
+	assert(alloc_ops.alloc_aligned);
+	return alloc_ops.alloc_aligned(size, align);
+}
+
+static inline void free(const void *addr)
+{
Quoted 8 lines

lib/alloc.h line 31

Quoted 6 lines
lib/alloc.h new file +5 -0
+	assert(alloc_ops.free);
+	alloc_ops.free(addr);
+}
+
+#endif

Why do you need the wrappers?

Quoted 1 line

A unit test may want to change the allocator after setting up paging.

Ok, having actually read the code a bit more, it looks like (correct me if I'm wrong) you really need the early allocator only for the physical addresses of the vring. You are using it for the virtqueue structs too, that is not strictly necessary but I won't complain if you leave it that way.

All you need is a pointer to the bottom and top of the free area, then you can do a very simple allocator like sbrk.

So the API can look like this:

	void phys_alloc_init(uintptr_t base, uintptr_t top);
	uintptr_t phys_alloc(size_t size);
	uintptr_t phys_zalloc(size_t size);
	uintptr_t phys_alloc_aligned(size_t size, size_t align);
	uintptr_t phys_zalloc_aligned(size_t size, size_t align);

If you keep the code that allocates the virtqueue, you can just add a phys_to_virt() on the result.

In order to keep the debugging code, just have a

	struct phys_alloc_region {
		void *ptr;
		size_t size;
	};

	struct phys_alloc_region *first_region;

and place these structs at the end of the region passed to phys_alloc_init. That is, phys_alloc will allocate SIZE bytes from the BASE, and a single struct phys_alloc_region from the TOP. The FIRST_REGION is initialized with the same value as TOP in phys_alloc_init, and remains fixed there; the last region is represented by TOP. To dump all regions, just walk the array from first_region down to top.

When paging is enabled, it can itself allocate memory using phys_alloc for page tables and the like.

Quoted 2 lines

If memregions look useful outside of arm, then I can certainly move them and early_[m]alloc, etc. to common code.

Yeah, they can live in lib/. Only the call to phys_alloc_init needs to be in lib/arm.

Paolo

#9 lore raw

Re: [PATCH v6 06/17] Introduce alloc_ops

Quoted 102 lines · Paolo Bonzini

Il 11/07/2014 10:55, Andrew Jones ha scritto:

Quoted 63 lines · Paolo Bonzini
Quoted 53 lines

Il 11/07/2014 10:19, Andrew Jones ha scritto:

Quoted 52 lines

alloc_ops provide interfaces for alloc(), free() and friends, allowing unit tests and common code to use dynamic memory allocation. arch-specific code must provide the implementations.

---
lib/alloc.c | 2 ++
lib/alloc.h | 31 +++++++++++++++++++++++++++++++

2 files changed, 33 insertions(+)

create mode 100644 lib/alloc.c
create mode 100644 lib/alloc.h

lib/alloc.c new file +2 -0
new file mode 100644
index 0000000000000..868664b4dcaa3
@@ -0,0 +1,2 @@
+#include "alloc.h"
+struct alloc_ops alloc_ops;
lib/alloc.h new file +26 -0
new file mode 100644
index 0000000000000..c8cd61b387a9a
@@ -0,0 +1,31 @@
+#ifndef _ALLOC_H_
+#define _ALLOC_H_
+#include "libcflat.h"
+
+struct alloc_ops {
+	void *(*alloc)(size_t size);
+	void *(*alloc_aligned)(size_t size, size_t align);
+	void (*free)(const void *addr);
+};
+
+extern struct alloc_ops alloc_ops;
+
+static inline void *alloc(size_t size)
+{
+	assert(alloc_ops.alloc);
+	return alloc_ops.alloc(size);
+}
+
+static inline void *alloc_aligned(size_t size, size_t align)
+{
+	assert(alloc_ops.alloc_aligned);
+	return alloc_ops.alloc_aligned(size, align);
+}
+
+static inline void free(const void *addr)
+{
Quoted 8 lines

lib/alloc.h line 31

Quoted 6 lines
lib/alloc.h new file +5 -0
+	assert(alloc_ops.free);
+	alloc_ops.free(addr);
+}
+
+#endif

Why do you need the wrappers?

Quoted 1 line

A unit test may want to change the allocator after setting up paging.

Ok, having actually read the code a bit more, it looks like (correct me if I'm wrong) you really need the early allocator only for the physical addresses of the vring. You are using it for the virtqueue structs too, that is not strictly necessary but I won't complain if you leave it that way.

All you need is a pointer to the bottom and top of the free area, then you can do a very simple allocator like sbrk.

So the API can look like this:

	void phys_alloc_init(uintptr_t base, uintptr_t top);
	uintptr_t phys_alloc(size_t size);
	uintptr_t phys_zalloc(size_t size);
	uintptr_t phys_alloc_aligned(size_t size, size_t align);
	uintptr_t phys_zalloc_aligned(size_t size, size_t align);

If you keep the code that allocates the virtqueue, you can just add a phys_to_virt() on the result.

In order to keep the debugging code, just have a

	struct phys_alloc_region {
		void *ptr;
		size_t size;
	};

	struct phys_alloc_region *first_region;

and place these structs at the end of the region passed to phys_alloc_init. That is, phys_alloc will allocate SIZE bytes from the BASE, and a single struct phys_alloc_region from the TOP. The FIRST_REGION is initialized with the same value as TOP in phys_alloc_init, and remains fixed there; the last region is represented by TOP. To dump all regions, just walk the array from first_region down to top.

Quoted 9 lines

When paging is enabled, it can itself allocate memory using phys_alloc for page tables and the like.

Quoted 2 lines

If memregions look useful outside of arm, then I can certainly move them and early_[m]alloc, etc. to common code.

Yeah, they can live in lib/. Only the call to phys_alloc_init needs to be in lib/arm.

Yes, you are correct, and I like your design and API better, but with one change. We should introduce phys_addr_t=u64 to common code as well, and then use that instead of uintptr_t/size_t. I'll do this for v7.

Thanks,
drew

maintainer
#10 lore raw

Re: [PATCH v6 06/17] Introduce alloc_ops

Il 11/07/2014 12:07, Andrew Jones ha scritto:

Quoted 3 lines

Yes, you are correct, and I like your design and API better, but with one change. We should introduce phys_addr_t=u64 to common code as well, and then use that instead of uintptr_t/size_t. I'll do this for v7.

Of course.

Paolo

#11 lore raw

Re: [PATCH v6 06/17] Introduce alloc_ops

In reply to Andrew Jones
Quoted 109 lines · Andrew Jones
Quoted 108 lines · Paolo Bonzini

Il 11/07/2014 10:55, Andrew Jones ha scritto:

Quoted 63 lines · Paolo Bonzini
Quoted 53 lines

Il 11/07/2014 10:19, Andrew Jones ha scritto:

Quoted 52 lines

alloc_ops provide interfaces for alloc(), free() and friends, allowing unit tests and common code to use dynamic memory allocation. arch-specific code must provide the implementations.

---
lib/alloc.c | 2 ++
lib/alloc.h | 31 +++++++++++++++++++++++++++++++

2 files changed, 33 insertions(+)

create mode 100644 lib/alloc.c
create mode 100644 lib/alloc.h

lib/alloc.c new file +2 -0
new file mode 100644
index 0000000000000..868664b4dcaa3
@@ -0,0 +1,2 @@
+#include "alloc.h"
+struct alloc_ops alloc_ops;
lib/alloc.h new file +26 -0
new file mode 100644
index 0000000000000..c8cd61b387a9a
@@ -0,0 +1,31 @@
+#ifndef _ALLOC_H_
+#define _ALLOC_H_
+#include "libcflat.h"
+
+struct alloc_ops {
+	void *(*alloc)(size_t size);
+	void *(*alloc_aligned)(size_t size, size_t align);
+	void (*free)(const void *addr);
+};
+
+extern struct alloc_ops alloc_ops;
+
+static inline void *alloc(size_t size)
+{
+	assert(alloc_ops.alloc);
+	return alloc_ops.alloc(size);
+}
+
+static inline void *alloc_aligned(size_t size, size_t align)
+{
+	assert(alloc_ops.alloc_aligned);
+	return alloc_ops.alloc_aligned(size, align);
+}
+
+static inline void free(const void *addr)
+{
Quoted 8 lines

lib/alloc.h line 31

Quoted 6 lines
lib/alloc.h new file +5 -0
+	assert(alloc_ops.free);
+	alloc_ops.free(addr);
+}
+
+#endif

Why do you need the wrappers?

Quoted 1 line

A unit test may want to change the allocator after setting up paging.

Ok, having actually read the code a bit more, it looks like (correct me if I'm wrong) you really need the early allocator only for the physical addresses of the vring. You are using it for the virtqueue structs too, that is not strictly necessary but I won't complain if you leave it that way.

All you need is a pointer to the bottom and top of the free area, then you can do a very simple allocator like sbrk.

So the API can look like this:

	void phys_alloc_init(uintptr_t base, uintptr_t top);
	uintptr_t phys_alloc(size_t size);
	uintptr_t phys_zalloc(size_t size);
	uintptr_t phys_alloc_aligned(size_t size, size_t align);
	uintptr_t phys_zalloc_aligned(size_t size, size_t align);

If you keep the code that allocates the virtqueue, you can just add a phys_to_virt() on the result.

In order to keep the debugging code, just have a

	struct phys_alloc_region {
		void *ptr;
		size_t size;
	};

	struct phys_alloc_region *first_region;

and place these structs at the end of the region passed to phys_alloc_init. That is, phys_alloc will allocate SIZE bytes from the BASE, and a single struct phys_alloc_region from the TOP. The FIRST_REGION is initialized with the same value as TOP in phys_alloc_init, and remains fixed there; the last region is represented by TOP. To dump all regions, just walk the array from first_region down to top.

When paging is enabled, it can itself allocate memory using phys_alloc for page tables and the like.

Quoted 2 lines

If memregions look useful outside of arm, then I can certainly move them and early_[m]alloc, etc. to common code.

Quoted 8 lines
Quoted 3 lines

Yeah, they can live in lib/. Only the call to phys_alloc_init needs to be in lib/arm.

Yes, you are correct, and I like your design and API better, but with one change. We should introduce phys_addr_t=u64 to common code as well, and then use that instead of uintptr_t/size_t. I'll do this for v7.

I decided to merge the two designs (memregions and your proposal). I took your API (except for the phys_addr_t change I already mentioned), but kept the explicit array for the allocation logging. Keeping the array (which should be allocated in low memory) ensures we can always walk it. Using TOP as you suggest may not work in all cases. I set the array size to 256, which should be [way] more than enough, so we don't really need a "limitless" log anyway.

drew

#12 lore raw

PATCH v6 7/17 add minimal virtio support for devtree virtio-mmio

In reply to Andrew Jones

Support the bare minimum of virtio to enable access to the virtio-mmio config space of a device. Currently this implementation must use a device tree to find the device.

---
v6:
- switch to using alloc()
- s/vmdev/vm_dev/ to be consistent with kernel naming
- check for virtio magic in vm_dt_match
v5:
- use same virtio struct names as kernel
- no need to alloc a new virtio_config_ops for each virtio device
- use ioremap
v4:
- split from the virtio-testdev patch
- search a table to "discover" that the device must be DT/virtio-mmio, which doesn't change anything, but looks less hacky than comments saying the device must be DT/virtio-mmio...
- manage own pool of virtio-mmio pre-allocated device structures in order to avoid needing access to the heap

---
 lib/libcflat.h |   3 ++
 lib/virtio.c   | 158 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/virtio.h   |  89 ++++++++++++++++++++++++++++++++
 3 files changed, 250 insertions(+)
 create mode 100644 lib/virtio.c
 create mode 100644 lib/virtio.h
lib/libcflat.h +3 -0
index 9f76d6741344d..57bdb92a3e1b4 100644
@@ -56,6 +56,9 @@ extern long atol(const char *ptr);
 #define ARRAY_SIZE(_a)  (sizeof(_a)/sizeof((_a)[0]))
 
 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
+#define container_of(ptr, type, member) ({				\
+	const typeof( ((type *)0)->member ) *__mptr = (ptr);		\
+	(type *)( (char *)__mptr - offsetof(type,member) );})
 
 #define NULL ((void *)0UL)
 
lib/virtio.c new file +158 -0

158 lines not on this page · raw message

lib/virtio.h new file +89 -0

89 lines not on this page · raw message

Signature · 1 line
1.9.3
maintainer
#13 lore raw

Re: [PATCH v6 07/17] add minimal virtio support for devtree virtio-mmio

Il 11/07/2014 10:19, Andrew Jones ha scritto:

Quoted 19 lines
lib/virtio.c new file +19 -0
+enum virtio_hwdesc_type {
+	VIRTIO_HWDESC_TYPE_DT = 0,	/* device tree */
+	NR_VIRTIO_HWDESC_TYPES,
+};
+
+enum virtio_bus_type {
+	VIRTIO_BUS_TYPE_MMIO = 0,	/* virtio-mmio */
+	NR_VIRTIO_BUS_TYPES,
+};
+
+struct virtio_bind_bus {
+	bool (*hwdesc_probe)(void);
+	struct virtio_device *(*device_bind)(u32 devid);
+};
+
+static struct virtio_device *vm_dt_device_bind(u32 devid);
+
+static struct virtio_bind_bus
+virtio_bind_busses[NR_VIRTIO_HWDESC_TYPES][NR_VIRTIO_BUS_TYPES] = {

lib/virtio.c line 40

Quoted 10 lines
lib/virtio.c new file +10 -0
+
+[VIRTIO_HWDESC_TYPE_DT] = {
+
+	[VIRTIO_BUS_TYPE_MMIO] = {
+		.hwdesc_probe = dt_available,
+		.device_bind = vm_dt_device_bind,
+	},
+},
+};
+

If you put this in lib/virtio.c, it is overengineered. It would make sense if something else provided virtio_bind_busses[][].

I suggest that you drop it and split this file in four:

lib/virtio.c
lib/virtio-mmio.c
lib/virtio-mmio-dt.c
lib/arm/virtio.c

where virtio_bind is in lib/arm/virtio.c.

Paolo

#14 lore raw

Re: [PATCH v6 07/17] add minimal virtio support for devtree virtio-mmio

Quoted 25 lines · Paolo Bonzini

Il 11/07/2014 10:19, Andrew Jones ha scritto:

Quoted 24 lines
lib/virtio.c new file +24 -0
+enum virtio_hwdesc_type {
+	VIRTIO_HWDESC_TYPE_DT = 0,	/* device tree */
+	NR_VIRTIO_HWDESC_TYPES,
+};
+
+enum virtio_bus_type {
+	VIRTIO_BUS_TYPE_MMIO = 0,	/* virtio-mmio */
+	NR_VIRTIO_BUS_TYPES,
+};
+
+struct virtio_bind_bus {
+	bool (*hwdesc_probe)(void);
+	struct virtio_device *(*device_bind)(u32 devid);
+};
+
+static struct virtio_device *vm_dt_device_bind(u32 devid);
+
+static struct virtio_bind_bus
+virtio_bind_busses[NR_VIRTIO_HWDESC_TYPES][NR_VIRTIO_BUS_TYPES] = {
+
+[VIRTIO_HWDESC_TYPE_DT] = {
+
+	[VIRTIO_BUS_TYPE_MMIO] = {
+		.hwdesc_probe = dt_available,
Quoted 8 lines

lib/virtio.c line 40

Quoted 5 lines
lib/virtio.c new file +5 -0
+		.device_bind = vm_dt_device_bind,
+	},
+},
+};
+

If you put this in lib/virtio.c, it is overengineered. It would make sense if something else provided virtio_bind_busses[][].

yeah, I acknowledged in the v4 changelog that it was unnecessary at the moment, but it's there to reduce the ugly hardcodeding that this simplified version currently needs, and who knows what the future shall bring.

Quoted 5 lines

I suggest that you drop it and split this file in four:

lib/virtio.c
lib/virtio-mmio.c

This split already crossed my mind.

Quoted 1 line

lib/virtio-mmio-dt.c

I'm not sure we need this one, but OK.

Quoted 4 lines

lib/arm/virtio.c

where virtio_bind is in lib/arm/virtio.c.

Well, virtio_bind will still need to be in lib/virtio.c, but just as a wrapper to arch_virtio_bind. And, I'm inclined to keep virtio_bind_busses in arm's arch_virtio_bind.

drew

maintainer
#15 lore raw

Re: [PATCH v6 07/17] add minimal virtio support for devtree virtio-mmio

Il 11/07/2014 11:08, Andrew Jones ha scritto:

Quoted 6 lines
Quoted 4 lines
Quoted 4 lines

lib/arm/virtio.c

where virtio_bind is in lib/arm/virtio.c.

Well, virtio_bind will still need to be in lib/virtio.c, but just as a wrapper to arch_virtio_bind.

Ok, that's just a naming thing.

Quoted 2 lines

And, I'm inclined to keep virtio_bind_busses
in arm's arch_virtio_bind.

Why? To support virtio-pci in the future? It seems like a good thing to have (future support for virtio-pci) but even then you'd have only two tests and that's already the exception. The common case would be just one. You could write that as

    struct virtio_device *arch_virtio_bind(u32 devid)
    {
        struct virtio_device *vdev;

	vdev = arch_virtio_mmio_bind(devid);
	if (!vdev)
	    vdev = arch_virtio_pci_bind(devid);
	return vdev;
    }

(I don't see kvm-unit-tests using ACPI in the future. Having DT+ACPI x mmio+pci would be a good reason to have the array, but even then it's premature and these are unit tests not an OS...).

Paolo

#16 lore raw

Re: [PATCH v6 07/17] add minimal virtio support for devtree virtio-mmio

Quoted 28 lines · Paolo Bonzini

Il 11/07/2014 11:08, Andrew Jones ha scritto:

Quoted 6 lines
Quoted 4 lines
Quoted 4 lines

lib/arm/virtio.c

where virtio_bind is in lib/arm/virtio.c.

Well, virtio_bind will still need to be in lib/virtio.c, but just as a wrapper to arch_virtio_bind.

Ok, that's just a naming thing.

Quoted 2 lines

And, I'm inclined to keep virtio_bind_busses
in arm's arch_virtio_bind.

Why? To support virtio-pci in the future? It seems like a good thing to have (future support for virtio-pci) but even then you'd have only two tests and that's already the exception. The common case would be just one. You could write that as

    struct virtio_device *arch_virtio_bind(u32 devid)
    {
        struct virtio_device *vdev;

	vdev = arch_virtio_mmio_bind(devid);
	if (!vdev)
	    vdev = arch_virtio_pci_bind(devid);
	return vdev;
    }
Quoted 2 lines

(I don't see kvm-unit-tests using ACPI in the future. Having DT+ACPI x mmio+pci would be a good reason to have the array, but even then it's

Yes, that was the reason.

Quoted 1 line

premature and these are unit tests not an OS...).

But, true and true. I guess I'll drop the table for now.

drew

#17 lore raw

Re: [PATCH v6 07/17] add minimal virtio support for devtree virtio-mmio

In reply to Andrew Jones
Quoted 40 lines · Andrew Jones
Quoted 25 lines · Paolo Bonzini

Il 11/07/2014 10:19, Andrew Jones ha scritto:

Quoted 24 lines
lib/virtio.c new file +24 -0
+enum virtio_hwdesc_type {
+	VIRTIO_HWDESC_TYPE_DT = 0,	/* device tree */
+	NR_VIRTIO_HWDESC_TYPES,
+};
+
+enum virtio_bus_type {
+	VIRTIO_BUS_TYPE_MMIO = 0,	/* virtio-mmio */
+	NR_VIRTIO_BUS_TYPES,
+};
+
+struct virtio_bind_bus {
+	bool (*hwdesc_probe)(void);
+	struct virtio_device *(*device_bind)(u32 devid);
+};
+
+static struct virtio_device *vm_dt_device_bind(u32 devid);
+
+static struct virtio_bind_bus
+virtio_bind_busses[NR_VIRTIO_HWDESC_TYPES][NR_VIRTIO_BUS_TYPES] = {
+
+[VIRTIO_HWDESC_TYPE_DT] = {
+
+	[VIRTIO_BUS_TYPE_MMIO] = {
+		.hwdesc_probe = dt_available,
Quoted 8 lines

lib/virtio.c line 40

Quoted 5 lines
lib/virtio.c new file +5 -0
+		.device_bind = vm_dt_device_bind,
+	},
+},
+};
+

If you put this in lib/virtio.c, it is overengineered. It would make sense if something else provided virtio_bind_busses[][].

yeah, I acknowledged in the v4 changelog that it was unnecessary at the moment, but it's there to reduce the ugly hardcodeding that this simplified version currently needs, and who knows what the future shall bring.

Quoted 1 line
Quoted 6 lines
Quoted 4 lines

I suggest that you drop it and split this file in four:

lib/virtio.c
lib/virtio-mmio.c

This split already crossed my mind.

I made this split.

Quoted 4 lines
Quoted 2 lines

lib/virtio-mmio-dt.c

I'm not sure we need this one, but OK.

I opted not to make this one. To me it seems reasonable to keep virtio-mmio and virtio-mmio-dt code together, if for nothing else just to lean more on the reduce file count side of the trade-off.

Quoted 10 lines
Quoted 5 lines

lib/arm/virtio.c

where virtio_bind is in lib/arm/virtio.c.

Well, virtio_bind will still need to be in lib/virtio.c, but just as a wrapper to arch_virtio_bind. And, I'm inclined to keep virtio_bind_busses in arm's arch_virtio_bind.

I dropped the idea of depending on arch-specific implementations being necessary. As the whole reason to get rid of my table is to simplify things, then I think all we need for now is

virtio_bind
virtio_mmio_bind
virtio_mmio_dt_bind

to maintain abstractions and give room for expansions (virtio-pci).

drew

#18 lore raw

PATCH v6 8/17 lib: add asm/page.h and virt_to_phys/phys_to_virt

In reply to Andrew Jones
---
 lib/asm-generic/io.h   | 13 +++++++++++++
 lib/asm-generic/page.h | 28 ++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+)
 create mode 100644 lib/asm-generic/page.h
lib/asm-generic/io.h +13 -0
index f00f4d3e68fe1..a9939d3a5921f 100644
@@ -10,6 +10,7 @@
  * This work is licensed under the terms of the GNU LGPL, version 2.
  */
 #include "libcflat.h"
+#include "asm/page.h"
 
 #ifndef __raw_readb
 static inline u8 __raw_readb(const volatile void *addr)
@@ -159,4 +160,16 @@ static inline void *ioremap(u64 phys_addr, size_t size __unused)
 }
 #endif
 
+#ifndef virt_to_phys
+static inline unsigned long virt_to_phys(volatile void *address)
+{
+	return __pa((unsigned long)address);
+}
+
+static inline void *phys_to_virt(unsigned long address)
+{
+	return __va(address);
+}
+#endif
+
 #endif /* _ASM_GENERIC_IO_H_ */
lib/asm-generic/page.h new file +28 -0
new file mode 100644
index 0000000000000..559938fcf0b3f
@@ -0,0 +1,28 @@
+#ifndef _ASM_GENERIC_PAGE_H_
+#define _ASM_GENERIC_PAGE_H_
+/*
+ * asm-generic/page.h
+ *  adapted from the Linux kernel's include/asm-generic/page.h
+ *
+ * Copyright (C) 2014, Red Hat Inc, Andrew Jones <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.
+ */
+
+#define PAGE_SHIFT		12
+#ifndef __ASSEMBLY__
+#define PAGE_SIZE		(1UL << PAGE_SHIFT)
+#else
+#define PAGE_SIZE		(1 << PAGE_SHIFT)
+#endif
+#define PAGE_MASK		(~(PAGE_SIZE-1))
+#define PAGE_ALIGN(addr)	(((addr) + (PAGE_SIZE-1)) & PAGE_MASK)
+
+#ifndef __ASSEMBLY__
+#define __va(x)			((void *)((unsigned long) (x)))
+#define __pa(x)			((unsigned long) (x))
+#define virt_to_pfn(kaddr)	(__pa(kaddr) >> PAGE_SHIFT)
+#define pfn_to_virt(pfn)	__va((pfn) << PAGE_SHIFT)
+#endif
+
+#endif
Signature · 1 line
1.9.3
#19 lore raw

PATCH v6 9/17 virtio: add minimal support for virtqueues

In reply to Andrew Jones

Currently only supports sending (outbufs), doesn't have any bells or whistles.

---
 lib/virtio.c | 177 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/virtio.h | 107 +++++++++++++++++++++++++++++++++++-
 2 files changed, 282 insertions(+), 2 deletions(-)
lib/virtio.c +177 -0
index 8e48d364bec7e..5e61965738d9c 100644
@@ -6,6 +6,7 @@
 #include "libcflat.h"
 #include "alloc.h"
 #include "devicetree.h"
+#include "asm/page.h"
 #include "asm/io.h"
 #include "virtio.h"
 
@@ -61,6 +62,121 @@ struct virtio_device *virtio_bind(u32 devid)
 	return NULL;
 }
 
+static void vring_init(struct vring *vr, unsigned int num, void *p,
+		       unsigned long align)
+{
+	vr->num = num;
+	vr->desc = p;
+	vr->avail = p + num*sizeof(struct vring_desc);
+	vr->used = (void *)(((unsigned long)&vr->avail->ring[num] + sizeof(u16)
+		+ align-1) & ~(align - 1));
+}
+
+static void
+vring_init_virtqueue(struct vring_virtqueue *vq,
+		     unsigned index, unsigned num, unsigned vring_align,
+		     struct virtio_device *vdev, void *pages,
+		     bool (*notify)(struct virtqueue *),
+		     void (*callback)(struct virtqueue *),
+		     const char *name)
+{
+	unsigned i;
+
+	vring_init(&vq->vring, num, pages, vring_align);
+	vq->vq.callback = callback;
+	vq->vq.vdev = vdev;
+	vq->vq.name = name;
+	vq->vq.num_free = num;
+	vq->vq.index = index;
+	vq->notify = notify;
+	vq->last_used_idx = 0;
+	vq->num_added = 0;
+	vq->free_head = 0;
+
+	for (i = 0; i < num-1; i++) {
+		vq->vring.desc[i].next = i+1;
+		vq->data[i] = NULL;
+	}
+	vq->data[i] = NULL;
+}
+
+int virtqueue_add_outbuf(struct virtqueue *_vq, char *buf, size_t len)
+{
+	struct vring_virtqueue *vq = to_vvq(_vq);
+	unsigned avail;
+	int head;
+
+	assert(buf != NULL);
+	assert(len != 0);
+
+	if (!vq->vq.num_free)
+		return -1;
+
+	--vq->vq.num_free;
+
+	head = vq->free_head;
+
+	vq->vring.desc[head].flags = 0;
+	vq->vring.desc[head].addr = virt_to_phys(buf);
+	vq->vring.desc[head].len = len;
+
+	vq->free_head = vq->vring.desc[head].next;
+
+	vq->data[head] = buf;
+
+	avail = (vq->vring.avail->idx & (vq->vring.num-1));
+	vq->vring.avail->ring[avail] = head;
+	wmb();
+	vq->vring.avail->idx++;
+	vq->num_added++;
+
+	return 0;
+}
+
+bool virtqueue_kick(struct virtqueue *_vq)
+{
+	struct vring_virtqueue *vq = to_vvq(_vq);
+	mb();
+	return vq->notify(_vq);
+}
+
+static void detach_buf(struct vring_virtqueue *vq, unsigned head)
+{
+	unsigned i = head;
+
+	vq->data[head] = NULL;
+
+	while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
+		i = vq->vring.desc[i].next;
+		vq->vq.num_free++;
+	}
+
+	vq->vring.desc[i].next = vq->free_head;
+	vq->free_head = head;
+	vq->vq.num_free++;
+}
+
+void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
+{
+	struct vring_virtqueue *vq = to_vvq(_vq);
+	u16 last_used;
+	unsigned i;
+	void *ret;
+
+	rmb();
+
+	last_used = (vq->last_used_idx & (vq->vring.num-1));
+	i = vq->vring.used->ring[last_used].id;
+	*len = vq->vring.used->ring[last_used].len;
+
+	ret = vq->data[i];
+	detach_buf(vq, i);
+
+	vq->last_used_idx++;
+
+	return ret;
+}
+
 /******************************************************
  * virtio-mmio support (config space only)
  ******************************************************/
@@ -87,9 +203,68 @@ static void vm_set(struct virtio_device *vdev, unsigned offset,
 		writeb(p[i], vm_dev->base + VIRTIO_MMIO_CONFIG + offset + i);
 }
 
+static bool vm_notify(struct virtqueue *vq)
+{
+	struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev);
+	writel(vq->index, vm_dev->base + VIRTIO_MMIO_QUEUE_NOTIFY);
+	return true;
+}
+
+static struct virtqueue *vm_setup_vq(struct virtio_device *vdev,
+				     unsigned index,
+				     void (*callback)(struct virtqueue *vq),
+				     const char *name)
+{
+	struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
+	struct vring_virtqueue *vq;
+	void *queue;
+	unsigned num = VIRTIO_MMIO_QUEUE_NUM_MIN;
+
+	vq = alloc(sizeof(*vq));
+	queue = alloc_aligned(VIRTIO_MMIO_QUEUE_SIZE_MIN, PAGE_SIZE);
+	if (!vq || !queue)
+		return NULL;
+
+	writel(index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL);
+
+	assert(readl(vm_dev->base + VIRTIO_MMIO_QUEUE_NUM_MAX) >= num);
+
+	if (readl(vm_dev->base + VIRTIO_MMIO_QUEUE_PFN) != 0) {
+		printf("%s: virtqueue %d already setup! base=%p\n",
+				__func__, index, vm_dev->base);
+		return NULL;
+	}
+
+	writel(num, vm_dev->base + VIRTIO_MMIO_QUEUE_NUM);
+	writel(VIRTIO_MMIO_VRING_ALIGN,
+			vm_dev->base + VIRTIO_MMIO_QUEUE_ALIGN);
+	writel(virt_to_pfn(queue), vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
+
+	vring_init_virtqueue(vq, index, num, VIRTIO_MMIO_VRING_ALIGN,
+			     vdev, queue, vm_notify, callback, name);
+
+	return &vq->vq;
+}
+
+static int vm_find_vqs(struct virtio_device *vdev, unsigned nvqs,
+		       struct virtqueue *vqs[], vq_callback_t *callbacks[],
+		       const char *names[])
+{
+	unsigned i;
+
+	for (i = 0; i < nvqs; ++i) {
+		vqs[i] = vm_setup_vq(vdev, i, callbacks[i], names[i]);
+		if (vqs[i] == NULL)
+			return -1;
+	}
+
+	return 0;
+}
+
 static const struct virtio_config_ops vm_config_ops = {
 	.get = vm_get,
 	.set = vm_set,
+	.find_vqs = vm_find_vqs,
 };
 
 static void vm_device_init(struct virtio_mmio_device *vm_dev)
@@ -97,6 +272,8 @@ static void vm_device_init(struct virtio_mmio_device *vm_dev)
 	vm_dev->vdev.id.device = readl(vm_dev->base + VIRTIO_MMIO_DEVICE_ID);
 	vm_dev->vdev.id.vendor = readl(vm_dev->base + VIRTIO_MMIO_VENDOR_ID);
 	vm_dev->vdev.config = &vm_config_ops;
+
+	writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_GUEST_PAGE_SIZE);
 }
 
 /******************************************************
lib/virtio.h +105 -2

134 lines not on this page · raw message

Signature · 1 line
1.9.3
maintainer
#20 lore raw

Re: [PATCH v6 09/17] virtio: add minimal support for virtqueues

Il 11/07/2014 10:19, Andrew Jones ha scritto:

lib/virtio.c static void vm_set(struct virtio_device *vdev, unsigned offset, · line 223

Quoted 11 lines
lib/virtio.c +11 -0
+static struct virtqueue *vm_setup_vq(struct virtio_device *vdev,
+				     unsigned index,
+				     void (*callback)(struct virtqueue *vq),
+				     const char *name)
+{
+	struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
+	struct vring_virtqueue *vq;
+	void *queue;
+	unsigned num = VIRTIO_MMIO_QUEUE_NUM_MIN;
+
+	vq = alloc(sizeof(*vq));

You can move the allocation of vq to vm_find_vqs (interleaving alloc and alloc_aligned causes some fragmentation), allocating a single block instead of one per vq.

Paolo

Quoted 1 line
lib/virtio.c +1 -0
+	queue = alloc_aligned(VIRTIO_MMIO_QUEUE_SIZE_MIN, PAGE_SIZE);
maintainer
#21 lore raw

Re: [PATCH v6 09/17] virtio: add minimal support for virtqueues

Il 11/07/2014 11:23, Paolo Bonzini ha scritto:

Quoted 16 lines

lib/virtio.c static void vm_set(struct virtio_device *vdev, unsigned offset, · line 223

Quoted 12 lines
lib/virtio.c +11 -0
+static struct virtqueue *vm_setup_vq(struct virtio_device *vdev,
+                     unsigned index,
+                     void (*callback)(struct virtqueue *vq),
+                     const char *name)
+{
+    struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
+    struct vring_virtqueue *vq;
+    void *queue;
+    unsigned num = VIRTIO_MMIO_QUEUE_NUM_MIN;
+
+    vq = alloc(sizeof(*vq));

You can move the allocation of vq to vm_find_vqs (interleaving alloc and alloc_aligned causes some fragmentation), allocating a single block instead of one per vq.

... or even use static storage for the "struct virtqueue". You can just merge "struct vring_virtqueue" and "struct virtqueue", as the idea of other virtqueue transport never materialized. Then:

+static struct virtqueue in_vq;
+static struct virtqueue out_vq;
+
void chr_testdev_init(void)
{
const char *io_names[] = { "input", "output" };
- struct virtqueue *vqs[2];
+ struct virtqueue *vqs[2] = { &in_vq, &out_vq };
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];
}

Paolo

#22 lore raw

Re: [PATCH v6 09/17] virtio: add minimal support for virtqueues

Quoted 18 lines · Paolo Bonzini

Il 11/07/2014 11:23, Paolo Bonzini ha scritto:

Quoted 16 lines

lib/virtio.c static void vm_set(struct virtio_device *vdev, unsigned offset, · line 223

Quoted 12 lines
lib/virtio.c +11 -0
+static struct virtqueue *vm_setup_vq(struct virtio_device *vdev,
+                     unsigned index,
+                     void (*callback)(struct virtqueue *vq),
+                     const char *name)
+{
+    struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
+    struct vring_virtqueue *vq;
+    void *queue;
+    unsigned num = VIRTIO_MMIO_QUEUE_NUM_MIN;
+
+    vq = alloc(sizeof(*vq));

You can move the allocation of vq to vm_find_vqs (interleaving alloc and alloc_aligned causes some fragmentation), allocating a single block instead of one per vq.

Quoted 4 lines

... or even use static storage for the "struct virtqueue". You can just merge "struct vring_virtqueue" and "struct virtqueue", as the idea of other virtqueue transport never materialized. Then:

I'd rather keep the implementation as close as possible to the Linux implementation, allowing new features to be easily lifted from there. The fragmentation was expected with the early allocator (memregions), but didn't bother me much, as it's unlikely to ever be more than a handful of pages, and we're just writing unit tests. If a particular unit test or arch wants a better allocator than memregions provides, one that does a better job of limiting fragmentation, then it's just a matter of installing one with alloc_ops.

drew

#23 lore raw

PATCH v6 10/17 Introduce chr-testdev

In reply to Andrew Jones

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).

---
 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
lib/chr-testdev.c new file +72 -0
new file mode 100644
index 0000000000000..0c9a173a04886
@@ -0,0 +1,72 @@
+/*
+ * Copyright (C) 2014, Red Hat Inc, Andrew Jones <[email protected]>
+ *
+ * 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];
+}
lib/chr-testdev.h new file +14 -0
new file mode 100644
index 0000000000000..ffd9a851aa9b9
@@ -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 <[email protected]>
+ *
+ * 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
lib/virtio.h +2 -0
index 110a066c8591c..a5fd830dfd64b 100644
@@ -11,6 +11,8 @@
 #include "libcflat.h"
 #include "asm/page.h"
 
+#define VIRTIO_ID_CONSOLE 3
+
 struct virtio_device_id {
 	u32 device;
 	u32 vendor;
Signature · 1 line
1.9.3
#24 lore raw

PATCH v6 11/17 libcflat: clean up libcflat.h

In reply to Andrew Jones

Use libgcc's stddef.h and stdint.h, and then remove the redundant defines from libcflat.h. These changes have no affect on code including libcflat.h, but are needed in order to compile an unmodified libfdt for kvm-unit-tests using an arm cross-compiler.

---
v6: squashed string.h changes into "libfdt: get libfdt to build" [Paolo
Bonzini]

---
 lib/libcflat.h | 41 +++++++++++++++++++----------------------
 1 file changed, 19 insertions(+), 22 deletions(-)
lib/libcflat.h +19 -22
index 57bdb92a3e1b4..a43eba0329f8e 100644
@@ -21,6 +21,8 @@
 #define __LIBCFLAT_H
 
 #include <stdarg.h>
+#include <stddef.h>
+#include <stdint.h>
 #include <string.h>
 
 #define __unused __attribute__((__unused__))
@@ -28,44 +30,39 @@
 #define xstr(s) xxstr(s)
 #define xxstr(s) #s
 
-typedef unsigned char u8;
-typedef signed char s8;
-typedef unsigned short u16;
-typedef signed short s16;
-typedef unsigned u32;
-typedef signed s32;
-typedef unsigned long ulong;
-typedef unsigned long long u64;
-typedef signed long long s64;
-typedef unsigned long size_t;
-typedef _Bool bool;
+typedef uint8_t		u8;
+typedef int8_t		s8;
+typedef uint16_t	u16;
+typedef int16_t		s16;
+typedef uint32_t	u32;
+typedef int32_t		s32;
+typedef uint64_t	u64;
+typedef int64_t		s64;
+typedef unsigned long	ulong;
 
-#define true 1
+typedef _Bool		bool;
 #define false 0
+#define true  1
 
+extern void puts(const char *s);
 extern void exit(int code);
 extern void abort(void);
 
 extern int printf(const char *fmt, ...);
 extern int snprintf(char *buf, int size, const char *fmt, ...);
 extern int vsnprintf(char *buf, int size, const char *fmt, va_list va);
+extern long atol(const char *ptr);
 
-extern void puts(const char *s);
+void report(const char *msg_fmt, bool pass, ...);
+void report_xfail(const char *msg_fmt, bool xfail, bool pass, ...);
+int report_summary(void);
 
-extern long atol(const char *ptr);
-#define ARRAY_SIZE(_a)  (sizeof(_a)/sizeof((_a)[0]))
+#define ARRAY_SIZE(_a) (sizeof(_a)/sizeof((_a)[0]))
 
-#define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
 #define container_of(ptr, type, member) ({				\
 	const typeof( ((type *)0)->member ) *__mptr = (ptr);		\
 	(type *)( (char *)__mptr - offsetof(type,member) );})
 
-#define NULL ((void *)0UL)
-
-void report(const char *msg_fmt, bool pass, ...);
-void report_xfail(const char *msg_fmt, bool xfail, bool pass, ...);
-int report_summary(void);
-
 #define assert(cond)							\
 do {									\
 	if (!(cond))							\
Signature · 1 line
1.9.3
#25 lore raw

PATCH v6 12/17 arm: initial drop

In reply to Andrew Jones

This is the initial drop of the arm test framework and a first test that just checks that setup completed (a selftest). kvm isn't needed to run this test unless testing with smp > 1.

Try it out with
yum install gcc-arm-linux-gnu
export QEMU=[qemu with mach-virt and virtio-testdev] ./configure --cross-prefix=arm-linux-gnu- --arch=arm make
./run_tests.sh

---
v6:
- fixed setup.c comment [Christoffer Dall]
- changed arm/run to use chr-testdev instead of virtio-testdev
- add align parameter to memregion_new, setup alloc_ops
v5:
- memregions: check freemem_start is in bounds and document
- selftest: rename testnam => testname and properly init it
- io.c: use writeb instead of writel in puts() and use ioremap
- arm/run script update for new qemu ('-device ?' now requires -machine)
- couple other minor changes to setup.c and io.c [Christoffer Dall]
v4:
- moved fdt to just after stacktop (it was in the middle of free memory)
- switched from using heap to memregions
- get nr_cpus and added smp=<num> test
- added barrier.h
- use new report()/report_summary()
- config/config-arm.mak cleanup

---
 arm/cstart.S           |  35 +++++++++
 arm/flat.lds           |  18 +++++
 arm/run                |  46 ++++++++++++
 arm/selftest.c         |  89 +++++++++++++++++++++++
 arm/unittests.cfg      |  18 +++++
 config/config-arm.mak  |  73 +++++++++++++++++++
 configure              |  12 +++-
 lib/argv.c             |   6 ++
 lib/arm/asm/barrier.h  |  18 +++++
 lib/arm/asm/io.h       |  24 +++++++
 lib/arm/asm/page.h     |   1 +
 lib/arm/asm/setup.h    |  63 +++++++++++++++++
 lib/arm/asm/spinlock.h |  16 +++++
 lib/arm/eabi_compat.c  |  20 ++++++
 lib/arm/io.c           |  65 +++++++++++++++++
 lib/arm/setup.c        | 188 +++++++++++++++++++++++++++++++++++++++++++++++++
 16 files changed, 690 insertions(+), 2 deletions(-)
 create mode 100644 arm/cstart.S
 create mode 100644 arm/flat.lds
 create mode 100755 arm/run
 create mode 100644 arm/selftest.c
 create mode 100644 arm/unittests.cfg
 create mode 100644 config/config-arm.mak
 create mode 100644 lib/arm/asm/barrier.h
 create mode 100644 lib/arm/asm/io.h
 create mode 100644 lib/arm/asm/page.h
 create mode 100644 lib/arm/asm/setup.h
 create mode 100644 lib/arm/asm/spinlock.h
 create mode 100644 lib/arm/eabi_compat.c
 create mode 100644 lib/arm/io.c
 create mode 100644 lib/arm/setup.c
arm/cstart.S new file +35 -0
new file mode 100644
index 0000000000000..e28251db2950d
@@ -0,0 +1,35 @@
+/*
+ * Boot entry point and assembler functions for armv7 tests.
+ *
+ * Copyright (C) 2014, Red Hat Inc, Andrew Jones <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.
+ */
+
+.arm
+
+.section .init
+
+.globl start
+start:
+	/*
+	 * bootloader params are in r0-r2
+	 * See the kernel doc Documentation/arm/Booting
+	 */
+	ldr	sp, =stacktop
+	bl	setup
+
+	/* run the test */
+	ldr	r0, =__argc
+	ldr	r0, [r0]
+	ldr	r1, =__argv
+	bl	main
+	bl	exit
+	b	halt
+
+.text
+
+.globl halt
+halt:
+1:	wfi
+	b	1b
arm/flat.lds new file +18 -0
new file mode 100644
index 0000000000000..3e5d72e24989b
@@ -0,0 +1,18 @@
+
+SECTIONS
+{
+    .text : { *(.init) *(.text) *(.text.*) }
+    . = ALIGN(4K);
+    .data : { *(.data) }
+    . = ALIGN(16);
+    .rodata : { *(.rodata) }
+    . = ALIGN(16);
+    .bss : { *(.bss) }
+    . = ALIGN(4K);
+    edata = .;
+    . += 8K;
+    . = ALIGN(4K);
+    stacktop = .;
+}
+
+ENTRY(start)
arm/run new file +46 -0

46 lines not on this page · raw message

arm/selftest.c new file +89 -0

89 lines not on this page · raw message

arm/unittests.cfg new file +18 -0
new file mode 100644
index 0000000000000..da9dfd7b1f118
@@ -0,0 +1,18 @@
+# Define your new unittest following the convention:
+# [unittest_name]
+# file = foo.flat # Name of the flat file to be used
+# smp  = 2        # Number of processors the VM will use during this test
+# extra_params = -append <params...> # Additional parameters used
+# arch = arm/arm64                   # Only if test case is specific to one
+# groups = group1 group2 # Used to identify test cases with run_tests -g ...
+
+#
+# Test that the configured number of processors (smp = <num>), and
+# that the configured amount of memory (-m <MB>) are correctly setup
+# by the framework.
+#
+[selftest::setup]
+file = selftest.flat
+smp  = 1
+extra_params = -m 256 -append 'setup smp=1 mem=256'
+groups = selftest
config/config-arm.mak new file +73 -0

73 lines not on this page · raw message

configure +10 -2
index aaa1b50ab1b98..afdd62800f56e 100755
@@ -6,8 +6,7 @@ cc=gcc
 ld=ld
 objcopy=objcopy
 ar=ar
-arch=`uname -m | sed -e s/i.86/i386/`
-processor="$arch"
+arch=`uname -m | sed -e s/i.86/i386/ | sed -e 's/arm.*/arm/'`
 cross_prefix=
 
 usage() {
@@ -16,6 +15,7 @@ usage() {
 
 	Options include:
 	    --arch=ARCH            architecture to compile for ($arch)
+	    --processor=PROCESSOR  processor to compile for ($arch)
 	    --cross-prefix=PREFIX  cross compiler prefix
 	    --cc=CC		   c compiler to use ($cc)
 	    --ld=LD		   ld linker to use ($ld)
@@ -62,6 +62,12 @@ while [[ "$1" = -* ]]; do
 	    ;;
     esac
 done
+[ -z "$processor" ] && processor="$arch"
+
+if [ "$processor" = "arm" ]; then
+    processor="cortex-a15"
+fi
+
 if [ "$arch" = "i386" ] || [ "$arch" = "x86_64" ]; then
     testdir=x86
 else
@@ -76,6 +82,7 @@ if [ -f $testdir/run ]; then
 fi
 
 # check for dependent 32 bit libraries
+if [ "$arch" != "arm" ]; then
 cat << EOF > lib_test.c
 #include <stdc++.h>
 #include <boost_thread-mt.h>
@@ -90,6 +97,7 @@ if [ $exit -eq 0 ]; then
     api=true
 fi
 rm -f lib_test.c
+fi
 
 # link lib/asm for the architecture
 rm -f lib/asm
lib/argv.c +6 -0
index 4ee54a6eeac3e..078a05faffebf 100644
@@ -31,3 +31,9 @@ void __setup_args(void)
     }
     __argc = argv - __argv;
 }
+
+void setup_args(char *args)
+{
+    __args = args;
+    __setup_args();
+}
lib/arm/asm/barrier.h new file +18 -0
new file mode 100644
index 0000000000000..acaeab5123431
@@ -0,0 +1,18 @@
+#ifndef _ASMARM_BARRIER_H_
+#define _ASMARM_BARRIER_H_
+/*
+ * Adapted form arch/arm/include/asm/barrier.h
+ */
+
+#define isb(option) __asm__ __volatile__ ("isb " #option : : : "memory")
+#define dsb(option) __asm__ __volatile__ ("dsb " #option : : : "memory")
+#define dmb(option) __asm__ __volatile__ ("dmb " #option : : : "memory")
+
+#define mb()		dsb()
+#define rmb()		dsb()
+#define wmb()		dsb(st)
+#define smp_mb()	dmb(ish)
+#define smp_rmb()	smp_mb()
+#define smp_wmb()	dmb(ishst)
+
+#endif /* _ASMARM_BARRIER_H_ */
lib/arm/asm/io.h new file +24 -0
new file mode 100644
index 0000000000000..51ec6e9aa2e99
@@ -0,0 +1,24 @@
+#ifndef _ASMARM_IO_H_
+#define _ASMARM_IO_H_
+#include "libcflat.h"
+#include "asm/barrier.h"
+
+#define __bswap16 bswap16
+static inline u16 bswap16(u16 val)
+{
+	u16 ret;
+	asm volatile("rev16 %0, %1" : "=r" (ret) :  "r" (val));
+	return ret;
+}
+
+#define __bswap32 bswap32
+static inline u32 bswap32(u32 val)
+{
+	u32 ret;
+	asm volatile("rev %0, %1" : "=r" (ret) :  "r" (val));
+	return ret;
+}
+
+#include "asm-generic/io.h"
+
+#endif /* _ASMARM_IO_H_ */
lib/arm/asm/page.h new file +1 -0
new file mode 100644
index 0000000000000..91a4bc3b7f86e
@@ -0,0 +1 @@
+#include "asm-generic/page.h"
lib/arm/asm/setup.h new file +63 -0

63 lines not on this page · raw message

lib/arm/asm/spinlock.h new file +16 -0
new file mode 100644
index 0000000000000..04f5a1a5538e2
@@ -0,0 +1,16 @@
+#ifndef _ASMARM_SPINLOCK_H_
+#define _ASMARM_SPINLOCK_H_
+
+struct spinlock {
+	int v;
+};
+
+//TODO
+static inline void spin_lock(struct spinlock *lock __unused)
+{
+}
+static inline void spin_unlock(struct spinlock *lock __unused)
+{
+}
+
+#endif /* _ASMARM_SPINLOCK_H_ */
lib/arm/eabi_compat.c new file +20 -0
new file mode 100644
index 0000000000000..59d624dcd9277
@@ -0,0 +1,20 @@
+/*
+ * Adapted from u-boot's arch/arm/lib/eabi_compat.c
+ */
+#include "libcflat.h"
+
+int raise(int signum __unused)
+{
+	printf("Divide by zero!\n");
+	abort();
+	return 0;
+}
+
+/* Dummy functions to avoid linker complaints */
+void __aeabi_unwind_cpp_pr0(void)
+{
+}
+
+void __aeabi_unwind_cpp_pr1(void)
+{
+}
lib/arm/io.c new file +65 -0

65 lines not on this page · raw message

lib/arm/setup.c new file +188 -0

188 lines not on this page · raw message

Signature · 1 line
1.9.3
#26 lore raw

PATCH v6 13/17 arm: Add spinlock implementation

In reply to Andrew Jones

From: Christoffer Dall <[email protected]>

Add simple busy-wait spinlock implementation for ARM.

---
 config/config-arm.mak  |  3 ++-
 lib/arm/asm/spinlock.h |  9 ++-------
 lib/arm/spinlock.c     | 28 ++++++++++++++++++++++++++++
 3 files changed, 32 insertions(+), 8 deletions(-)
 create mode 100644 lib/arm/spinlock.c
config/config-arm.mak +2 -1
index 3225018faf933..fb160b6c65e2a 100644
@@ -36,7 +36,8 @@ cflatobjs += \
 	lib/virtio.o \
 	lib/chr-testdev.o \
 	lib/arm/io.o \
-	lib/arm/setup.o
+	lib/arm/setup.o \
+	lib/arm/spinlock.o
 
 libeabi = lib/arm/libeabi.a
 eabiobjs = lib/arm/eabi_compat.o
lib/arm/asm/spinlock.h +2 -7
index 04f5a1a5538e2..2118a4b3751e0 100644
@@ -5,12 +5,7 @@ struct spinlock {
 	int v;
 };
 
-//TODO
-static inline void spin_lock(struct spinlock *lock __unused)
-{
-}
-static inline void spin_unlock(struct spinlock *lock __unused)
-{
-}
+extern void spin_lock(struct spinlock *lock);
+extern void spin_unlock(struct spinlock *lock);
 
 #endif /* _ASMARM_SPINLOCK_H_ */
lib/arm/spinlock.c new file +28 -0
new file mode 100644
index 0000000000000..d8a6d4c3383d6
@@ -0,0 +1,28 @@
+#include "libcflat.h"
+#include "asm/spinlock.h"
+#include "asm/barrier.h"
+
+void spin_lock(struct spinlock *lock)
+{
+	u32 val, fail;
+
+	dmb();
+	do {
+		asm volatile(
+		"1:	ldrex	%0, [%2]\n"
+		"	teq	%0, #0\n"
+		"	bne	1b\n"
+		"	mov	%0, #1\n"
+		"	strex	%1, %0, [%2]\n"
+		: "=&r" (val), "=&r" (fail)
+		: "r" (&lock->v)
+		: "cc" );
+	} while (fail);
+	dmb();
+}
+
+void spin_unlock(struct spinlock *lock)
+{
+	lock->v = 0;
+	dmb();
+}
Signature · 1 line
1.9.3
#27 lore raw

PATCH v6 14/17 arm: Add IO accessors to avoid register-writeback

In reply to Andrew Jones

From: Christoffer Dall <[email protected]>

Add IO accessor functions to the arm library functions to avoid register-writeback IO accessors that are not yet supported by the kernel.

---
 lib/arm/asm/io.h | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)
lib/arm/asm/io.h +57 -0
index 51ec6e9aa2e99..1d0abb7d9f405 100644
@@ -3,6 +3,9 @@
 #include "libcflat.h"
 #include "asm/barrier.h"
 
+#define __iomem
+#define __force
+
 #define __bswap16 bswap16
 static inline u16 bswap16(u16 val)
 {
@@ -19,6 +22,60 @@ static inline u32 bswap32(u32 val)
 	return ret;
 }
 
+#define __raw_readb __raw_readb
+static inline u8 __raw_readb(const volatile void __iomem *addr)
+{
+	u8 val;
+	asm volatile("ldrb %1, %0"
+		     : "+Qo" (*(volatile u8 __force *)addr),
+		       "=r" (val));
+	return val;
+}
+
+#define __raw_readw __raw_readw
+static inline u16 __raw_readw(const volatile void __iomem *addr)
+{
+	u16 val;
+	asm volatile("ldrh %1, %0"
+		     : "+Q" (*(volatile u16 __force *)addr),
+		       "=r" (val));
+	return val;
+}
+
+#define __raw_readl __raw_readl
+static inline u32 __raw_readl(const volatile void __iomem *addr)
+{
+	u32 val;
+	asm volatile("ldr %1, %0"
+		     : "+Qo" (*(volatile u32 __force *)addr),
+		       "=r" (val));
+	return val;
+}
+
+#define __raw_writeb __raw_writeb
+static inline void __raw_writeb(u8 val, volatile void __iomem *addr)
+{
+	asm volatile("strb %1, %0"
+		     : "+Qo" (*(volatile u8 __force *)addr)
+		     : "r" (val));
+}
+
+#define __raw_writew __raw_writew
+static inline void __raw_writew(u16 val, volatile void __iomem *addr)
+{
+	asm volatile("strh %1, %0"
+		     : "+Q" (*(volatile u16 __force *)addr)
+		     : "r" (val));
+}
+
+#define __raw_writel __raw_writel
+static inline void __raw_writel(u32 val, volatile void __iomem *addr)
+{
+	asm volatile("str %1, %0"
+		     : "+Qo" (*(volatile u32 __force *)addr)
+		     : "r" (val));
+}
+
 #include "asm-generic/io.h"
 
 #endif /* _ASMARM_IO_H_ */
Signature · 1 line
1.9.3
#28 lore raw

PATCH v6 15/17 arm: Add arch-specific asm/page.h and __va/__pa

In reply to Andrew Jones

These are pretty much the same as the asm-generic version, but use phys_addr_t.

---
 lib/arm/asm/io.h   | 13 +++++++++++++
 lib/arm/asm/page.h | 34 +++++++++++++++++++++++++++++++++-
 2 files changed, 46 insertions(+), 1 deletion(-)
lib/arm/asm/io.h +13 -0
index 1d0abb7d9f405..bbcbcd0542490 100644
@@ -2,6 +2,7 @@
 #define _ASMARM_IO_H_
 #include "libcflat.h"
 #include "asm/barrier.h"
+#include "asm/page.h"
 
 #define __iomem
 #define __force
@@ -76,6 +77,18 @@ static inline void __raw_writel(u32 val, volatile void __iomem *addr)
 		     : "r" (val));
 }
 
+#define virt_to_phys virt_to_phys
+static inline phys_addr_t virt_to_phys(const volatile void *x)
+{
+	return __virt_to_phys((unsigned long)(x));
+}
+
+#define phys_to_virt phys_to_virt
+static inline void *phys_to_virt(phys_addr_t x)
+{
+	return (void *)__phys_to_virt(x);
+}
+
 #include "asm-generic/io.h"
 
 #endif /* _ASMARM_IO_H_ */
lib/arm/asm/page.h +33 -1
index 91a4bc3b7f86e..606d76f5775cf 100644
@@ -1 +1,33 @@
-#include "asm-generic/page.h"
+#ifndef _ASMARM_PAGE_H_
+#define _ASMARM_PAGE_H_
+/*
+ * Copyright (C) 2014, Red Hat Inc, Andrew Jones <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.
+ */
+
+#define PAGE_SHIFT		12
+#ifndef __ASSEMBLY__
+#define PAGE_SIZE		(1UL << PAGE_SHIFT)
+#else
+#define PAGE_SIZE		(1 << PAGE_SHIFT)
+#endif
+#define PAGE_MASK		(~(PAGE_SIZE-1))
+#define PAGE_ALIGN(addr)	(((addr) + (PAGE_SIZE-1)) & PAGE_MASK)
+
+#ifndef __ASSEMBLY__
+#include <asm/setup.h>
+
+#ifndef __virt_to_phys
+#define __phys_to_virt(x)	((unsigned long) (x))
+#define __virt_to_phys(x)	(x)
+#endif
+
+#define __va(x)			((void *)__phys_to_virt((phys_addr_t)(x)))
+#define __pa(x)			__virt_to_phys((unsigned long)(x))
+
+#define virt_to_pfn(kaddr)      (__pa(kaddr) >> PAGE_SHIFT)
+#define pfn_to_virt(pfn)        __va((pfn) << PAGE_SHIFT)
+#endif
+
+#endif
Signature · 1 line
1.9.3
#29 lore raw

PATCH v6 16/17 arm: add useful headers from the Linux kernel

In reply to Andrew Jones

We're going to need PSR bit defines and pt_regs. We'll also need pt_regs offsets in assembly code. This patch adapts the Linux kernel's ptrace.h and generated/asm-offsets.h to this framework. It also adapts cp15.h from the kernel, since we'll need bit defines from there too.

---
v4: much improved asm-offsets.h generation based on Kbuild

---
 config/asm-offsets.mak    |  41 +++++++++++++++++++
 config/config-arm.mak     |   9 ++++-
 lib/arm/.gitignore        |   1 +
 lib/arm/asm-offsets.c     |  39 ++++++++++++++++++
 lib/arm/asm/asm-offsets.h |   1 +
 lib/arm/asm/cp15.h        |  37 +++++++++++++++++
 lib/arm/asm/ptrace.h      | 100 ++++++++++++++++++++++++++++++++++++++++++++++
 lib/generated/.gitignore  |   1 +
 8 files changed, 227 insertions(+), 2 deletions(-)
 create mode 100644 config/asm-offsets.mak
 create mode 100644 lib/arm/.gitignore
 create mode 100644 lib/arm/asm-offsets.c
 create mode 100644 lib/arm/asm/asm-offsets.h
 create mode 100644 lib/arm/asm/cp15.h
 create mode 100644 lib/arm/asm/ptrace.h
 create mode 100644 lib/generated/.gitignore
config/asm-offsets.mak new file +41 -0
new file mode 100644
index 0000000000000..b2578a6692f33
@@ -0,0 +1,41 @@
+#
+# asm-offsets adapted from the kernel, see
+#   Kbuild
+#   scripts/Kbuild.include
+#   scripts/Makefile.build
+#
+#   Authors: Andrew Jones <[email protected]>
+#
+
+define sed-y
+	"/^->/{s:->#\(.*\):/* \1 */:; \
+	s:^->\([^ ]*\) [\$$#]*\([-0-9]*\) \(.*\):#define \1 \2 /* \3 */:; \
+	s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; \
+	s:->::; p;}"
+endef
+
+define make_asm_offsets
+	(set -e; \
+	 echo "#ifndef __ASM_OFFSETS_H__"; \
+	 echo "#define __ASM_OFFSETS_H__"; \
+	 echo "/*"; \
+	 echo " * Generated file. DO NOT MODIFY."; \
+	 echo " *"; \
+	 echo " */"; \
+	 echo ""; \
+	 sed -ne $(sed-y) $<; \
+	 echo ""; \
+	 echo "#endif" ) > $@
+endef
+
+$(asm-offsets:.h=.s): $(asm-offsets:.h=.c)
+	$(CC) $(CFLAGS) -fverbose-asm -S -o $@ $<
+
+$(asm-offsets): $(asm-offsets:.h=.s)
+	$(call make_asm_offsets)
+	cp -f $(asm-offsets) lib/generated
+
+asm_offsets_clean:
+	$(RM) $(asm-offsets) $(asm-offsets:.h=.s) \
+	      $(addprefix lib/generated/,$(notdir $(asm-offsets)))
+
config/config-arm.mak +7 -2
index fb160b6c65e2a..ca47b483e9ce9 100644
@@ -30,6 +30,9 @@ CFLAGS += -Wextra
 CFLAGS += -O2
 CFLAGS += -I lib -I lib/libfdt
 
+asm-offsets = lib/arm/asm-offsets.h
+include config/asm-offsets.mak
+
 cflatobjs += \
 	lib/alloc.o \
 	lib/devicetree.o \
@@ -58,7 +61,7 @@ FLATLIBS = $(libcflat) $(LIBFDT_archive) $(libgcc) $(libeabi)
 $(libeabi): $(eabiobjs)
 	$(AR) rcs $@ $^
 
-arch_clean: libfdt_clean
+arch_clean: libfdt_clean asm_offsets_clean
 	$(RM) $(TEST_DIR)/*.{o,flat,elf} $(libeabi) $(eabiobjs) \
 	      $(TEST_DIR)/.*.d lib/arm/.*.d
 
@@ -68,7 +71,9 @@ tests_and_config = $(TEST_DIR)/*.flat $(TEST_DIR)/unittests.cfg
 
 cstart.o = $(TEST_DIR)/cstart.o
 
-test_cases: $(tests-common) $(tests)
+generated_files = $(asm-offsets)
+
+test_cases: $(generated_files) $(tests-common) $(tests)
 
 $(TEST_DIR)/selftest.elf: $(cstart.o) $(TEST_DIR)/selftest.o
 
lib/arm/.gitignore new file +1 -0
new file mode 100644
index 0000000000000..84872bf197c67
@@ -0,0 +1 @@
+asm-offsets.[hs]
lib/arm/asm-offsets.c new file +39 -0
new file mode 100644
index 0000000000000..a9c349d2d427c
@@ -0,0 +1,39 @@
+/*
+ * Adapted from arch/arm/kernel/asm-offsets.c
+ *
+ * Copyright (C) 2014, Red Hat Inc, Andrew Jones <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.
+ */
+#include "libcflat.h"
+#include "asm/ptrace.h"
+
+#define DEFINE(sym, val) \
+	asm volatile("\n->" #sym " %0 " #val : : "i" (val))
+#define OFFSET(sym, str, mem)	DEFINE(sym, offsetof(struct str, mem))
+#define COMMENT(x)		asm volatile("\n->#" x)
+#define BLANK()			asm volatile("\n->" : : )
+
+int main(void)
+{
+	OFFSET(S_R0, pt_regs, ARM_r0);
+	OFFSET(S_R1, pt_regs, ARM_r1);
+	OFFSET(S_R2, pt_regs, ARM_r2);
+	OFFSET(S_R3, pt_regs, ARM_r3);
+	OFFSET(S_R4, pt_regs, ARM_r4);
+	OFFSET(S_R5, pt_regs, ARM_r5);
+	OFFSET(S_R6, pt_regs, ARM_r6);
+	OFFSET(S_R7, pt_regs, ARM_r7);
+	OFFSET(S_R8, pt_regs, ARM_r8);
+	OFFSET(S_R9, pt_regs, ARM_r9);
+	OFFSET(S_R10, pt_regs, ARM_r10);
+	OFFSET(S_FP, pt_regs, ARM_fp);
+	OFFSET(S_IP, pt_regs, ARM_ip);
+	OFFSET(S_SP, pt_regs, ARM_sp);
+	OFFSET(S_LR, pt_regs, ARM_lr);
+	OFFSET(S_PC, pt_regs, ARM_pc);
+	OFFSET(S_PSR, pt_regs, ARM_cpsr);
+	OFFSET(S_OLD_R0, pt_regs, ARM_ORIG_r0);
+	DEFINE(S_FRAME_SIZE, sizeof(struct pt_regs));
+	return 0;
+}
lib/arm/asm/asm-offsets.h new file +1 -0
new file mode 100644
index 0000000000000..c2ff2ba6ec417
@@ -0,0 +1 @@
+#include "generated/asm-offsets.h"
lib/arm/asm/cp15.h new file +37 -0
new file mode 100644
index 0000000000000..7690a48f17f1f
@@ -0,0 +1,37 @@
+#ifndef _ASMARM_CP15_H_
+#define _ASMARM_CP15_H_
+/*
+ * From the Linux kernel arch/arm/include/asm/cp15.h
+ *
+ * CR1 bits (CP#15 CR1)
+ */
+#define CR_M	(1 << 0)	/* MMU enable				*/
+#define CR_A	(1 << 1)	/* Alignment abort enable		*/
+#define CR_C	(1 << 2)	/* Dcache enable			*/
+#define CR_W	(1 << 3)	/* Write buffer enable			*/
+#define CR_P	(1 << 4)	/* 32-bit exception handler		*/
+#define CR_D	(1 << 5)	/* 32-bit data address range		*/
+#define CR_L	(1 << 6)	/* Implementation defined		*/
+#define CR_B	(1 << 7)	/* Big endian				*/
+#define CR_S	(1 << 8)	/* System MMU protection		*/
+#define CR_R	(1 << 9)	/* ROM MMU protection			*/
+#define CR_F	(1 << 10)	/* Implementation defined		*/
+#define CR_Z	(1 << 11)	/* Implementation defined		*/
+#define CR_I	(1 << 12)	/* Icache enable			*/
+#define CR_V	(1 << 13)	/* Vectors relocated to 0xffff0000	*/
+#define CR_RR	(1 << 14)	/* Round Robin cache replacement	*/
+#define CR_L4	(1 << 15)	/* LDR pc can set T bit			*/
+#define CR_DT	(1 << 16)
+#define CR_HA	(1 << 17)	/* Hardware management of Access Flag	*/
+#define CR_IT	(1 << 18)
+#define CR_ST	(1 << 19)
+#define CR_FI	(1 << 21)	/* Fast interrupt (lower latency mode)	*/
+#define CR_U	(1 << 22)	/* Unaligned access operation		*/
+#define CR_XP	(1 << 23)	/* Extended page tables			*/
+#define CR_VE	(1 << 24)	/* Vectored interrupts			*/
+#define CR_EE	(1 << 25)	/* Exception (Big) Endian		*/
+#define CR_TRE	(1 << 28)	/* TEX remap enable			*/
+#define CR_AFE	(1 << 29)	/* Access flag enable			*/
+#define CR_TE	(1 << 30)	/* Thumb exception enable		*/
+
+#endif /* _ASMARM_CP15_H_ */
lib/arm/asm/ptrace.h new file +100 -0

100 lines not on this page · raw message

lib/generated/.gitignore new file +1 -0
new file mode 100644
index 0000000000000..72e8ffc0db8aa
@@ -0,0 +1 @@
+*
Signature · 1 line
1.9.3
#30 lore raw

PATCH v6 17/17 arm: vectors support

In reply to Andrew Jones

Add support for tests to use exception handlers using install_exception_handler(). This patch also adds phys_start_usr(), which can be used to start a function in USR mode, using a stack set up in a new memregion. phys_start_usr() is used by new selftest tests that check the new vector support.

---
v6: use alloc() for start_usr
v5: rebase change: replace __stringify with libcflat's new xstr
macro
v4: a couple tweaks to fit changes in the other patches,
vectors-usr test now has an 8K usr stack
v3:
- squashed in 'arm: Simplify exceptions_init in cstart.S' [Christoffer Dall]
- suggested function name changes and comment additions [Christoffer Dall]
- fix a bug with stack restore from usr mode exceptions that Christoffer pointed out. Add a get_sp() accessor too.

---
 arm/cstart.S            | 174 ++++++++++++++++++++++++++++++++++++++++++++++++
 arm/flat.lds            |   7 +-
 arm/selftest.c          | 127 ++++++++++++++++++++++++++++++++++-
 arm/unittests.cfg       |  12 ++++
 config/config-arm.mak   |   3 +-
 lib/arm/asm/processor.h |  39 +++++++++++
 lib/arm/processor.c     | 112 +++++++++++++++++++++++++++++++
 7 files changed, 471 insertions(+), 3 deletions(-)
 create mode 100644 lib/arm/asm/processor.h
 create mode 100644 lib/arm/processor.c
arm/cstart.S +174 -0
index e28251db2950d..cc87ece4b6b40 100644
@@ -5,6 +5,10 @@
  *
  * This work is licensed under the terms of the GNU LGPL, version 2.
  */
+#define __ASSEMBLY__
+#include "asm/asm-offsets.h"
+#include "asm/ptrace.h"
+#include "asm/cp15.h"
 
 .arm
 
@@ -17,6 +21,13 @@ start:
 	 * See the kernel doc Documentation/arm/Booting
 	 */
 	ldr	sp, =stacktop
+	push	{r0-r3}
+
+	/* set up vector table and mode stacks */
+	bl	exceptions_init
+
+	/* complete setup */
+	pop	{r0-r3}
 	bl	setup
 
 	/* run the test */
@@ -27,9 +38,172 @@ start:
 	bl	exit
 	b	halt
 
+
+.macro set_mode_stack mode, stack
+	add	\stack, #S_FRAME_SIZE
+	msr	cpsr_c, #(\mode | PSR_I_BIT | PSR_F_BIT)
+	mov	sp, \stack
+.endm
+
+exceptions_init:
+	mrc	p15, 0, r2, c1, c0, 0	@ read SCTLR
+	bic	r2, #CR_V		@ SCTLR.V := 0
+	mcr	p15, 0, r2, c1, c0, 0	@ write SCTLR
+	ldr	r2, =vector_table
+	mcr	p15, 0, r2, c12, c0, 0	@ write VBAR
+
+	mrs	r2, cpsr
+	ldr	r1, =exception_stacks
+
+	/* first frame reserved for svc mode */
+	set_mode_stack	UND_MODE, r1
+	set_mode_stack	ABT_MODE, r1
+	set_mode_stack	IRQ_MODE, r1
+	set_mode_stack	FIQ_MODE, r1
+
+	msr	cpsr_cxsf, r2		@ back to svc mode
+	mov	pc, lr
+
 .text
 
 .globl halt
 halt:
 1:	wfi
 	b	1b
+
+/*
+ * Vector stubs
+ * Simplified version of the Linux kernel implementation
+ *   arch/arm/kernel/entry-armv.S
+ *
+ * Each mode has an S_FRAME_SIZE sized stack initialized
+ * in exceptions_init
+ */
+.macro vector_stub, name, vec, mode, correction=0
+.align 5
+vector_\name:
+.if \correction
+	sub	lr, lr, #\correction
+.endif
+	/*
+	 * Save r0, r1, lr_<exception> (parent PC)
+	 * and spsr_<exception> (parent CPSR)
+	 */
+	str	r0, [sp, #S_R0]
+	str	r1, [sp, #S_R1]
+	str	lr, [sp, #S_PC]
+	mrs	r0, spsr
+	str	r0, [sp, #S_PSR]
+
+	/* Prepare for SVC32 mode. */
+	mrs	r0, cpsr
+	bic	r0, #MODE_MASK
+	orr	r0, #SVC_MODE
+	msr	spsr_cxsf, r0
+
+	/* Branch to handler in SVC mode */
+	mov	r0, #\vec
+	mov	r1, sp
+	ldr	lr, =vector_common
+	movs	pc, lr
+.endm
+
+vector_stub 	rst,	0, UND_MODE
+vector_stub	und,	1, UND_MODE
+vector_stub	pabt,	3, ABT_MODE, 4
+vector_stub	dabt,	4, ABT_MODE, 8
+vector_stub	irq,	6, IRQ_MODE, 4
+vector_stub	fiq,	7, FIQ_MODE, 4
+
+.align 5
+vector_svc:
+	/*
+	 * Save r0, r1, lr_<exception> (parent PC)
+	 * and spsr_<exception> (parent CPSR)
+	 */
+	push	{ r1 }
+	ldr	r1, =exception_stacks
+	str	r0, [r1, #S_R0]
+	pop	{ r0 }
+	str	r0, [r1, #S_R1]
+	str	lr, [r1, #S_PC]
+	mrs	r0, spsr
+	str	r0, [r1, #S_PSR]
+
+	/*
+	 * Branch to handler, still in SVC mode.
+	 * r0 := 2 is the svc vector number.
+	 */
+	mov	r0, #2
+	ldr	lr, =vector_common
+	mov	pc, lr
+
+vector_common:
+	/* make room for pt_regs */
+	sub	sp, #S_FRAME_SIZE
+	tst	sp, #4			@ check stack alignment
+	subne	sp, #4
+
+	/* store registers r0-r12 */
+	stmia	sp, { r0-r12 }		@ stored wrong r0 and r1, fix later
+
+	/* get registers saved in the stub */
+	ldr	r2, [r1, #S_R0]		@ r0
+	ldr	r3, [r1, #S_R1]		@ r1
+	ldr	r4, [r1, #S_PC] 	@ lr_<exception> (parent PC)
+	ldr	r5, [r1, #S_PSR]	@ spsr_<exception> (parent CPSR)
+
+	/* fix r0 and r1 */
+	str	r2, [sp, #S_R0]
+	str	r3, [sp, #S_R1]
+
+	/* store sp_svc, if we were in usr mode we'll fix this later */
+	add	r6, sp, #S_FRAME_SIZE
+	addne	r6, #4			@ stack wasn't aligned
+	str	r6, [sp, #S_SP]
+
+	str	lr, [sp, #S_LR]		@ store lr_svc, fix later for usr mode
+	str	r4, [sp, #S_PC]		@ store lr_<exception>
+	str	r5, [sp, #S_PSR]	@ store spsr_<exception>
+
+	/* set ORIG_r0 */
+	mov	r2, #-1
+	str	r2, [sp, #S_OLD_R0]
+
+	/* if we were in usr mode then we need sp_usr and lr_usr instead */
+	and	r1, r5, #MODE_MASK
+	cmp	r1, #USR_MODE
+	bne	1f
+	add	r1, sp, #S_SP
+	stmia	r1, { sp,lr }^
+
+	/* Call the handler. r0 is the vector number, r1 := pt_regs */
+1:	mov	r1, sp
+	bl	do_handle_exception
+
+	/*
+	 * make sure we restore sp_svc on mode change. No need to
+	 * worry about lr_svc though, as that gets clobbered on
+	 * exception entry anyway.
+	 */
+	str	r6, [sp, #S_SP]
+
+	/* return from exception */
+	msr	spsr_cxsf, r5
+	ldmia	sp, { r0-pc }^
+
+.align 5
+vector_addrexcptn:
+	b	vector_addrexcptn
+
+.section .text.ex
+.align 5
+vector_table:
+	b	vector_rst
+	b	vector_und
+	b	vector_svc
+	b	vector_pabt
+	b	vector_dabt
+	b	vector_addrexcptn	@ should never happen
+	b	vector_irq
+	b	vector_fiq
arm/flat.lds +6 -1
index 3e5d72e24989b..ee9fc0ab79abc 100644
@@ -3,7 +3,12 @@ SECTIONS
 {
     .text : { *(.init) *(.text) *(.text.*) }
     . = ALIGN(4K);
-    .data : { *(.data) }
+    .data : {
+        exception_stacks = .;
+        . += 4K;
+        exception_stacks_end = .;
+        *(.data)
+    }
     . = ALIGN(16);
     .rodata : { *(.rodata) }
     . = ALIGN(16);
arm/selftest.c +126 -1

147 lines not on this page · raw message

arm/unittests.cfg +12 -0
index da9dfd7b1f118..57f5f90f3e808 100644
@@ -16,3 +16,15 @@ file = selftest.flat
 smp  = 1
 extra_params = -m 256 -append 'setup smp=1 mem=256'
 groups = selftest
+
+# Test vector setup and exception handling (svc mode).
+[selftest::vectors-svc]
+file = selftest.flat
+extra_params = -append 'vectors-svc'
+groups = selftest
+
+# Test vector setup and exception handling (usr mode).
+[selftest::vectors-usr]
+file = selftest.flat
+extra_params = -append 'vectors-usr'
+groups = selftest
config/config-arm.mak +2 -1
index ca47b483e9ce9..9243f77f8e5b2 100644
@@ -40,7 +40,8 @@ cflatobjs += \
 	lib/chr-testdev.o \
 	lib/arm/io.o \
 	lib/arm/setup.o \
-	lib/arm/spinlock.o
+	lib/arm/spinlock.o \
+	lib/arm/processor.o
 
 libeabi = lib/arm/libeabi.a
 eabiobjs = lib/arm/eabi_compat.o
lib/arm/asm/processor.h new file +39 -0
new file mode 100644
index 0000000000000..883cab89622f7
@@ -0,0 +1,39 @@
+#ifndef _ASMARM_PROCESSOR_H_
+#define _ASMARM_PROCESSOR_H_
+/*
+ * Copyright (C) 2014, Red Hat Inc, Andrew Jones <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.
+ */
+#include "ptrace.h"
+
+enum vector {
+	EXCPTN_RST,
+	EXCPTN_UND,
+	EXCPTN_SVC,
+	EXCPTN_PABT,
+	EXCPTN_DABT,
+	EXCPTN_ADDREXCPTN,
+	EXCPTN_IRQ,
+	EXCPTN_FIQ,
+	EXCPTN_MAX,
+};
+
+typedef void (*exception_fn)(struct pt_regs *);
+extern void install_exception_handler(enum vector v, exception_fn fn);
+
+extern void show_regs(struct pt_regs *regs);
+extern void *get_sp(void);
+
+static inline unsigned long current_cpsr(void)
+{
+	unsigned long cpsr;
+	asm volatile("mrs %0, cpsr" : "=r" (cpsr));
+	return cpsr;
+}
+
+#define current_mode() (current_cpsr() & MODE_MASK)
+
+extern void start_usr(void (*func)(void *arg), void *arg, unsigned long sp_usr);
+
+#endif /* _ASMARM_PROCESSOR_H_ */
lib/arm/processor.c new file +112 -0

112 lines not on this page · raw message

Signature · 1 line
1.9.3
maintainer
#31 lore raw
In reply to Andrew Jones

Il 11/07/2014 10:19, Andrew Jones ha scritto:

Quoted 5 lines

This is a v6 of a series that introduces arm to kvm-unit-tests. As the tidy-up patches from v5 have already been merged (thanks!), these are just the remaining patches. Although there are several new patches as well, as virtio-testdev was dropped in favor of the chr-testdev backend[1].

Another tiny step: I've applied patch 2 ("libfdt: get libfdt to build" minus the Makefile parts, you can squash them in patch 3), patch 4, patch 11.

The virtio and alloc interface can be simplified, I think. I'll merge chr-testdev at the beginning of the QEMU 2.2 development period.

Thanks,

Paolo

#32 lore raw
Quoted 9 lines · Paolo Bonzini

Il 11/07/2014 10:19, Andrew Jones ha scritto:

Quoted 5 lines

This is a v6 of a series that introduces arm to kvm-unit-tests. As the tidy-up patches from v5 have already been merged (thanks!), these are just the remaining patches. Although there are several new patches as well, as virtio-testdev was dropped in favor of the chr-testdev backend[1].

Another tiny step: I've applied patch 2 ("libfdt: get libfdt to build" minus the Makefile parts, you can squash them in patch 3), patch 4, patch 11.

Where'd you apply them? I pulled master, but don't see them. I'd like to rebase v7 on a master with them before posting, assuming you intend to push them there soon.

Thanks,
drew

maintainer
#33 lore raw

Il 15/07/2014 19:24, Andrew Jones ha scritto:

Quoted 8 lines · Paolo Bonzini
Quoted 7 lines

Il 11/07/2014 10:19, Andrew Jones ha scritto:

Quoted 5 lines

This is a v6 of a series that introduces arm to kvm-unit-tests. As the tidy-up patches from v5 have already been merged (thanks!), these are just the remaining patches. Although there are several new patches as well, as virtio-testdev was dropped in favor of the chr-testdev backend[1].

Quoted 6 lines
Quoted 2 lines

Another tiny step: I've applied patch 2 ("libfdt: get libfdt to build" minus the Makefile parts, you can squash them in patch 3), patch 4, patch 11.

Where'd you apply them? I pulled master, but don't see them. I'd like to rebase v7 on a master with them before posting, assuming you intend to push them there soon.

Here they are now.

Paolo