kvm-unit-tests/arm: initial drop
Subsystems (3)
v7 cover kvm-unit-tests/arm: initial drop
This is v7 of a series that introduces arm to kvm-unit-tests. Three of the v6 patches were already merged, so this is the remainder. No new patches have been added, but some of the summaries have changed.
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 (v7) are also available from a git repo here
https://github.com/rhdrjones/kvm-unit-tests/commits/arm/v7-initial-drop
The v6 patches are also available from a git repo here
https://github.com/rhdrjones/kvm-unit-tests/commits/arm/v6-initial-drop%2Cchr-testdev
and, the v5 patches are still available here
https://github.com/rhdrjones/kvm-unit-tests/commits/arm/v5-initial-drop
The main changes since v6 are the moving/redesigning/reAPIing of arm's memregions to common code as phys_alloc, and the splitting of virtio and virtio-mmio code into separate files. The main change since v5 (as stated in the v6 cover letter) is the switch from virtio-testdev to chr-testdev. Also, as stated in the v6 cover letter, I've kept Christoffer's *-by's, and mainly the already Reviewed-by patches
05/14 add minimal virtio support for devtree virtio-mmio 09/14 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
Andrew Jones (12):
libfdt: Import libfdt source
add support for Linux device trees
Introduce asm-generic/*.h files
Introduce lib/alloc
add minimal virtio support for devtree virtio-mmio lib/asm-generic: add page.h and virt_to_phys/phys_to_virt
virtio: add minimal support for virtqueues Introduce chr-testdev
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 | 25 +- arm/cstart.S | 209 ++++++ arm/flat.lds | 23 + arm/run | 46 ++ arm/selftest.c | 210 ++++++ arm/unittests.cfg | 30 + config/asm-offsets.mak | 41 ++ config/config-arm.mak | 81 +++ configure | 23 +- lib/alloc.c | 176 +++++ lib/alloc.h | 123 ++++ lib/argv.c | 9 + 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 | 27 + lib/arm/asm/spinlock.h | 11 + lib/arm/eabi_compat.c | 20 + lib/arm/io.c | 65 ++ lib/arm/processor.c | 111 ++++ lib/arm/setup.c | 82 +++ 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 | 2 + 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/virtio-mmio.c | 175 +++++ lib/virtio-mmio.h | 65 ++ lib/virtio.c | 130 ++++ lib/virtio.h | 149 +++++ 56 files changed, 6794 insertions(+), 6 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/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/virtio-mmio.c create mode 100644 lib/virtio-mmio.h create mode 100644 lib/virtio.c create mode 100644 lib/virtio.h
Signature · 1 line
1.9.3
PATCH v7 2/14 add support for Linux device trees
Build libfdt and add some device tree functions built on it 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.
---
v7:
- squashed the "add a make target" bit of v6's "libfdt: get libfdt to build" (now dropped) patch. The rest of that dropped patch has already been merged under "libcflat: add more string functions"
- no need for info to be const in dt_device_init
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
--- Makefile | 21 ++++- lib/devicetree.c | 272 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/devicetree.h | 236 +++++++++++++++++++++++++++++++++++++++++++++++ lib/libcflat.h | 2 + 4 files changed, 529 insertions(+), 2 deletions(-) create mode 100644 lib/devicetree.c create mode 100644 lib/devicetree.h
Makefile +19 -2
@@ -22,6 +22,13 @@ cflatobjs := \ lib/abort.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
@@ -47,6 +54,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 $@ $<
@@ -59,10 +71,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/devicetree.c new file +272 -0
272 lines not on this page · raw message
lib/devicetree.h new file +236 -0
236 lines not on this page · raw message
lib/libcflat.h +2 -0
@@ -25,6 +25,8 @@#include <stdint.h> #include <string.h> +#define __unused __attribute__((__unused__)) +#define xstr (s) xxstr(s)#define xxstr (s) #s
Signature · 1 line
1.9.3
PATCH v7 3/14 Introduce asm-generic/*.h files
In reply to Andrew JonesArchitecture 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
@@ -9,6 +9,7 @@ patches .stgit-* cscope.* *.swp +/lib/asm /config.mak /*-run /test.log
Makefile +3 -3
@@ -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
@@ -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
@@ -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
PATCH v7 4/14 Introduce lib/alloc
In reply to Andrew Jonesalloc supplies three ingredients to the test framework that are all related to the support of dynamic memory allocation.
The first is a set of alloc function wrappers for malloc and its friends. Using wrappers allows test code and common code to use the same interface for memory allocation at all stages, even though the implementations may change with the stage, e.g. pre/post paging.
The second is a set of implementations for the alloc function interfaces. These implementations are named early_*, as they can be used almost immediately by the test framework.
The third is a very simple physical memory allocator, which the early_* alloc functions build on.
---
v7: expanded from only supplying the alloc function wrappers to
including early_* and phys_alloc [Paolo Bonzini]
--- lib/alloc.c | 176 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/alloc.h | 123 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 299 insertions(+) create mode 100644 lib/alloc.c create mode 100644 lib/alloc.h
lib/alloc.c new file +176 -0
@@ -0,0 +1,176 @@ +/* +* Copyright (C) 2014, Red Hat Inc, Andrew Jones <[email protected]> +* +* This work is licensed under the terms of the GNU LGPL, version 2. +*/ +#include "alloc.h" +#include "asm/spinlock.h" +#include "asm/io.h" + +#define ALIGN_UP_MASK (x, mask) (((x) + (mask)) & ~(mask)) +#define ALIGN_UP (x, a) ALIGN_UP_MASK(x, (typeof(x))(a) - 1) +#define MIN (a, b) ((a) < (b) ? (a) : (b)) +#define MAX (a, b) ((a) > (b) ? (a) : (b)) + +#define PHYS_ALLOC_NR_REGIONS 256 + +struct phys_alloc_region { +phys_addr_t base; +phys_addr_t size; +}; + +static struct phys_alloc_region regions[PHYS_ALLOC_NR_REGIONS ]; +static int nr_regions; + +static struct spinlock lock; +static phys_addr_t base, top, align_min; + +void phys_alloc_show (void ) +{ +int i; + +spin_lock (&lock); +printf ("phys_alloc minimum alignment: 0x%llx\n" , align_min); +for (i =0 ; i < nr_regions; ++i) +printf ("%016llx-%016llx [%s]\n" , + regions[i].base, + regions[i].base + regions[i].size -1 , +"USED" ); +printf ("%016llx-%016llx [%s]\n" , base, top -1 ,"FREE" ); +spin_unlock (&lock); +} + +void phys_alloc_init (phys_addr_t base_addr,phys_addr_t size) +{ +spin_lock (&lock); + base = base_addr; + top = base + size; + align_min =DEFAULT_MINIMUM_ALIGNMENT ; +spin_unlock (&lock); +} + +void phys_alloc_set_minimum_alignment (phys_addr_t align) +{ +assert (align && !(align & (align -1 ))); +spin_lock (&lock); + align_min = align; +spin_unlock (&lock); +} + +static phys_addr_t phys_alloc_aligned_safe (phys_addr_t size, +phys_addr_t align,bool safe) +{ +phys_addr_t addr, size_orig = size; +u64 top_safe = top; + +if (safe &&sizeof (long ) ==4 ) + top_safe =MIN (top,1ULL <<32 ); + + align =MAX (align, align_min); + +spin_lock (&lock); + + addr =ALIGN_UP (base, align); + size += addr - base; + +if ((top_safe - base) < size) { +printf ("%s: requested=0x%llx (align=0x%llx), " +"need=0x%llx, but free=0x%llx. " +"top=0x%llx, top_safe=0x%llx\n" , __func__, + size_orig, align, size, top_safe - base, + top, top_safe); +spin_unlock (&lock); +return INVALID_PHYS_ADDR ; + } + + base += size; + +if (nr_regions <PHYS_ALLOC_NR_REGIONS ) { + regions[nr_regions].base = addr; + regions[nr_regions].size = size_orig; + ++nr_regions; + }else { +printf ("%s: WARNING: no free log entries, " +"can't log allocation...\n" , __func__); + } + +spin_unlock (&lock); + +return addr; +} + +static phys_addr_t phys_zalloc_aligned_safe (phys_addr_t size, +phys_addr_t align,bool safe) +{ +phys_addr_t addr =phys_alloc_aligned_safe (size, align, safe); +if (addr ==INVALID_PHYS_ADDR ) +return addr; + +memset (phys_to_virt (addr),0 , size); +return addr; +} + +phys_addr_t phys_alloc_aligned (phys_addr_t size,phys_addr_t align) +{ +return phys_alloc_aligned_safe (size, align, false); +} + +phys_addr_t phys_zalloc_aligned (phys_addr_t size,phys_addr_t align) +{ +return phys_zalloc_aligned_safe (size, align, false); +} + +phys_addr_t phys_alloc (phys_addr_t size) +{ +return phys_alloc_aligned (size, align_min); +} + +phys_addr_t phys_zalloc (phys_addr_t size) +{ +return phys_zalloc_aligned (size, align_min); +} + +static void *early_malloc (size_t size) +{ +phys_addr_t addr =phys_alloc_aligned_safe (size, align_min, true); +if (addr ==INVALID_PHYS_ADDR ) +return NULL ; + +return phys_to_virt (addr); +} + +static void *early_calloc (size_t nmemb,size_t size) +{ +phys_addr_t addr =phys_zalloc_aligned_safe (nmemb * size, + align_min, true); +if (addr ==INVALID_PHYS_ADDR ) +return NULL ; + +return phys_to_virt (addr); +} + +static void early_free(void *ptr __unused) +{ +} + +static void *early_memalign (size_t alignment,size_t size) +{ +phys_addr_t addr; + +assert (alignment && !(alignment & (alignment -1 ))); + + addr =phys_alloc_aligned_safe (size, alignment, true); +if (addr ==INVALID_PHYS_ADDR ) +return NULL ; + +return phys_to_virt (addr); +} + +static struct alloc_ops early_alloc_ops = { + .malloc = early_malloc, + .calloc = early_calloc, + .free = early_free, + .memalign = early_memalign, +}; + +struct alloc_ops *alloc_ops = &early_alloc_ops;
lib/alloc.h new file +123 -0
123 lines not on this page · raw message
Signature · 1 line
1.9.3
PATCH v7 5/14 add minimal virtio support for devtree virtio-mmio
In reply to Andrew JonesSupport 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.
---
v7:
- s/alloc/calloc/
- split into virtio.[ch] and virtio-mmio.[ch] [Paolo Bonzini]
- dump virtio_bind_busses table [Paolo Bonzini]
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/virtio-mmio.c | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/virtio-mmio.h | 47 +++++++++++++++++++++++ lib/virtio.c | 13 +++++++ lib/virtio.h | 74 ++++++++++++++++++++++++++++++++++++ 4 files changed, 245 insertions(+) create mode 100644 lib/virtio-mmio.c create mode 100644 lib/virtio-mmio.h create mode 100644 lib/virtio.c create mode 100644 lib/virtio.h
lib/virtio-mmio.c new file +111 -0
@@ -0,0 +1,111 @@ +/* +* 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 "devicetree.h" +#include "alloc.h" +#include "asm/io.h" +#include "virtio.h" +#include "virtio-mmio.h" + +static void vm_get (struct virtio_device *vdev,unsigned offset, +void *buf,unsigned len) +{ +struct virtio_mmio_device *vm_dev =to_virtio_mmio_device (vdev); +u8 *p = buf; +unsigned i; + +for (i =0 ; i < len; ++i) + p[i] =readb (vm_dev->base +VIRTIO_MMIO_CONFIG + offset + i); +} + +static void vm_set (struct virtio_device *vdev,unsigned offset, +const void *buf,unsigned len) +{ +struct virtio_mmio_device *vm_dev =to_virtio_mmio_device (vdev); +const u8 *p = buf; +unsigned i; + +for (i =0 ; i < len; ++i) +writeb (p[i], vm_dev->base +VIRTIO_MMIO_CONFIG + offset + i); +} + +static const struct virtio_config_ops vm_config_ops = { + .get = vm_get, + .set = vm_set, +}; + +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; +} + +/****************************************************** +* virtio-mmio device tree support +******************************************************/ + +struct vm_dt_info { +u32 devid; +void *base; +}; + +static int vm_dt_match (const struct dt_device *dev,int fdtnode) +{ +struct vm_dt_info *info = (struct vm_dt_info *)dev->info; +struct dt_pbus_reg base; +u32 magic; + +dt_device_bind_node ((struct dt_device *)dev, fdtnode); + +assert (dt_pbus_get_base (dev, &base) ==0 ); + info->base =ioremap (base.addr, base.size); + + magic =readl (info->base +VIRTIO_MMIO_MAGIC_VALUE ); +if (magic != ('v' |'i' <<8 |'r' <<16 |'t' <<24 )) +return false; + +return readl (info->base +VIRTIO_MMIO_DEVICE_ID ) == info->devid; +} + +static struct virtio_device *virtio_mmio_dt_bind (u32 devid) +{ +struct virtio_mmio_device *vm_dev; +struct dt_device dt_dev; +struct dt_bus dt_bus; +struct vm_dt_info info; +int node; + +if (!dt_available ()) +return NULL ; + +dt_bus_init_defaults (&dt_bus); + dt_bus.match = vm_dt_match; + + info.devid = devid; + +dt_device_init (&dt_dev, &dt_bus, &info); + + node =dt_device_find_compatible (&dt_dev,"virtio,mmio" ); +assert (node >=0 || node == -FDT_ERR_NOTFOUND ); + +if (node == -FDT_ERR_NOTFOUND ) +return NULL ; + + vm_dev =calloc (1 ,sizeof (*vm_dev)); +if (!vm_dev) +return NULL ; + + vm_dev->base = info.base; +vm_device_init (vm_dev); + +return &vm_dev->vdev; +} + +struct virtio_device *virtio_mmio_bind (u32 devid) +{ +return virtio_mmio_dt_bind (devid); +}
lib/virtio-mmio.h new file +47 -0
47 lines not on this page · raw message
lib/virtio.c new file +13 -0
@@ -0,0 +1,13 @@ +/* +* 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 "virtio-mmio.h" + +struct virtio_device *virtio_bind (u32 devid) +{ +return virtio_mmio_bind (devid); +}
lib/virtio.h new file +74 -0
74 lines not on this page · raw message
Signature · 1 line
1.9.3
PATCH v7 6/14 lib/asm-generic: add 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
@@ -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_readbstatic 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
@@ -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
PATCH v7 7/14 virtio: add minimal support for virtqueues
In reply to Andrew JonesCurrently only supports sending (outbufs), doesn't have any bells or whistles. Code adapted from the Linux Kernel.
---
v7:
- {alloc,alloc_aligned} -> {calloc,memalign}
- changes now split between virtio.* and virtio-mmio.* files
--- lib/virtio-mmio.c | 64 +++++++++++++++++++++++++++++ lib/virtio-mmio.h | 18 +++++++++ lib/virtio.c | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/virtio.h | 73 ++++++++++++++++++++++++++++++++++ 4 files changed, 272 insertions(+)
lib/virtio-mmio.c +64 -0
@@ -1,4 +1,6 @@/* +* virtqueue support adapted from the Linux kernel. +* * Copyright (C) 2014, Red Hat Inc, Andrew Jones <[email protected]> * * This work is licensed under the terms of the GNU LGPL, version 2.
@@ -6,6 +8,7 @@#include "libcflat.h" #include "devicetree.h" #include "alloc.h" +#include "asm/page.h" #include "asm/io.h" #include "virtio.h" #include "virtio-mmio.h"
@@ -32,9 +35,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 =calloc (1 ,sizeof (*vq)); + queue =memalign (PAGE_SIZE ,VIRTIO_MMIO_QUEUE_SIZE_MIN ); +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)
@@ -42,6 +104,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-mmio.h +18 -0
@@ -8,6 +8,7 @@* This work is licensed under the terms of the GNU LGPL, version 2. */ #include "libcflat.h" +#include "asm/page.h" #include "virtio.h" #define VIRTIO_MMIO_MAGIC_VALUE 0x000
@@ -33,6 +34,23 @@#define VIRTIO_MMIO_INT_VRING (1 << 0)#define VIRTIO_MMIO_INT_CONFIG (1 << 1) +#define VIRTIO_MMIO_VRING_ALIGN PAGE_SIZE + +/* +* The minimum queue size is 2*VIRTIO_MMIO_VRING_ALIGN, which +* means the largest queue num for the minimum queue size is 128, i.e. +* 2*VIRTIO_MMIO_VRING_ALIGN = vring_size(128, VIRTIO_MMIO_VRING_ALIGN), +* where vring_size is +* +* unsigned vring_size(unsigned num, unsigned long align) +* { +* return ((sizeof(struct vring_desc) * num + sizeof(u16) * (3 + num) +* + align - 1) & ~(align - 1)) +* + sizeof(u16) * 3 + sizeof(struct vring_used_elem) * num; +* } +*/ +#define VIRTIO_MMIO_QUEUE_SIZE_MIN (2*VIRTIO_MMIO_VRING_ALIGN) +#define VIRTIO_MMIO_QUEUE_NUM_MIN 128#define to_virtio_mmio_device (vdev_ptr) \ container_of(vdev_ptr, struct virtio_mmio_device, vdev)
lib/virtio.c +117 -0
129 lines not on this page · raw message
lib/virtio.h +73 -0
90 lines not on this page · raw message
Signature · 1 line
1.9.3
PATCH v7 8/14 Introduce chr-testdev
In reply to Andrew Joneschr-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
@@ -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
@@ -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
@@ -10,6 +10,8 @@*/ #include "libcflat.h" +#define VIRTIO_ID_CONSOLE 3 +struct virtio_device_id {u32 device;u32 vendor;
Signature · 1 line
1.9.3
Re: [PATCH v7 08/14] Introduce chr-testdev
----- Original Message -----
lib/chr-testdev.c line 36
Quoted 8 lines
[...]
lib/chr-testdev.c new file +7
-0+void chr_testdev_exit (int code) +{ +char buf[8 ]; +int len; + +snprintf (buf,sizeof (buf),"%dq" , code); + len =strlen (buf);
AFAIK, snprintf returns the number of characters written, so these two statements can be merged into one.
Thanks,
Levente Kurusa
Re: [PATCH v7 08/14] Introduce chr-testdev
Quoted 12 lines · Levente Kurusa
----- Original Message -----
lib/chr-testdev.c line 36
Quoted 8 lines
[...]
lib/chr-testdev.c new file +7
-0+void chr_testdev_exit (int code) +{ +char buf[8 ]; +int len; + +snprintf (buf,sizeof (buf),"%dq" , code); + len =strlen (buf);AFAIK, snprintf returns the number of characters written, so these two statements can be merged into one.
You are correct. I'll do this for v8 (if a v8 is needed).
Thanks,
drew
Re: [PATCH v7 08/14] Introduce chr-testdev
2014-07-16 11:33+0200, Andrew Jones:
Quoted 14 lines · Levente Kurusa
Quoted 12 lines
----- Original Message -----
lib/chr-testdev.c line 36
Quoted 8 lines
[...]
lib/chr-testdev.c new file +7
-0+void chr_testdev_exit (int code) +{ +char buf[8 ]; +int len; + +snprintf (buf,sizeof (buf),"%dq" , code); + len =strlen (buf);AFAIK, snprintf returns the number of characters written, so these two statements can be merged into one.
Quoted 1 line
You are correct. I'll do this for v8 (if a v8 is needed).
snprintf returns the number of characters that would be written without truncation, so it wouldn't handle a very long code ;)
I'd prefer the obvious v7, to finding a minimum with 'sizeof(buf) - 1'.
PATCH v7 9/14 arm: initial drop
In reply to Andrew JonesThis 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 chr-testdev] ./configure --cross-prefix=arm-linux-gnu- --arch=arm make
./run_tests.sh
---
v7:
- remove memregions (reworked them as phys_alloc in lib/alloc)
- selftest: non-functional change: s/argv[i]/var/
- lib/argv:setup_args don't dereference NULL
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 | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++ arm/unittests.cfg | 18 +++++++++++ config/config-arm.mak | 74 +++++++++++++++++++++++++++++++++++++++++++ configure | 12 +++++-- lib/argv.c | 9 ++++++ lib/arm/asm/barrier.h | 18 +++++++++++ lib/arm/asm/io.h | 24 ++++++++++++++ lib/arm/asm/page.h | 1 + lib/arm/asm/setup.h | 27 ++++++++++++++++ lib/arm/asm/spinlock.h | 16 ++++++++++ lib/arm/eabi_compat.c | 20 ++++++++++++ lib/arm/io.c | 65 ++++++++++++++++++++++++++++++++++++++ lib/arm/setup.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++ 16 files changed, 549 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
@@ -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
@@ -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 +86 -0
86 lines not on this page · raw message
arm/unittests.cfg new file +18 -0
@@ -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 +74 -0
74 lines not on this page · raw message
configure +10 -2
@@ -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 +9 -0
@@ -31,3 +31,12 @@ void __setup_args(void) } __argc = argv - __argv; } + +void setup_args (char *args) +{ +if (!args) +return ; + + __args = args; +__setup_args (); +}
lib/arm/asm/barrier.h new file +18 -0
@@ -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
@@ -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; + asmvolatile ("rev16 %0, %1" :"=r" (ret) :"r" (val)); +return ret; +} + +#define __bswap32 bswap32 +static inline u32 bswap32 (u32 val) +{ +u32 ret; + asmvolatile ("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
@@ -0,0 +1 @@ +#include "asm-generic/page.h"
lib/arm/asm/setup.h new file +27 -0
@@ -0,0 +1,27 @@ +#ifndef _ASMARM_SETUP_H_ +#define _ASMARM_SETUP_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 "libcflat.h" +#include "alloc.h" + +#define NR_CPUS 8 +extern u32 cpus[NR_CPUS ]; +extern int nr_cpus; + +extern phys_addr_t __phys_offset, __phys_end; + +#define PHYS_OFFSET (__phys_offset) +#define PHYS_END (__phys_end) +#define PHYS_SHIFT 40 +#define PHYS_SIZE (1ULL << PHYS_SHIFT) +#define PHYS_MASK (PHYS_SIZE - 1ULL) + +#define L1_CACHE_SHIFT 6 +#define L1_CACHE_BYTES (1 << L1_CACHE_SHIFT) +#define SMP_CACHE_BYTES L1_CACHE_BYTES + +#endif /* _ASMARM_SETUP_H_ */
lib/arm/asm/spinlock.h new file +16 -0
@@ -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
@@ -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 +82 -0
82 lines not on this page · raw message
Signature · 1 line
1.9.3
Paolo Bonzini
maintainerRe: [PATCH v7 09/14] arm: initial drop
Quoted 15 lines
arm/unittests.cfg new file +8
-0@@ -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 ... +
arm/unittests.cfg line 18
Quoted 10 lines
arm/unittests.cfg new file +10
-0+# +# 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
Nice. :)
Quoted 24 lines
lib/arm/asm/page.h new file +1
-0@@ -0,0 +1 @@ +#include "asm-generic/page.h" lib/arm/asm/setup.h new file +11
-0@@ -0,0 +1,27 @@ +#ifndef _ASMARM_SETUP_H_ +#define _ASMARM_SETUP_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 "libcflat.h" +#include "alloc.h" + +#define NR_CPUS 8
lib/arm/asm/setup.h line 21
Quoted 10 lines
lib/arm/asm/setup.h new file +10
-0+extern u32 cpus[NR_CPUS ]; +extern int nr_cpus; + +extern phys_addr_t __phys_offset, __phys_end; + +#define PHYS_OFFSET (__phys_offset) +#define PHYS_END (__phys_end) +#define PHYS_SHIFT 40 +#define PHYS_SIZE (1ULL << PHYS_SHIFT) +#define PHYS_MASK (PHYS_SIZE - 1ULL)
Can you explain these? I'm not sure I understand this:
lib/arm/asm/setup.h
Quoted 8 lines
lib/arm/asm/setup.h new file +8
-0+ mem_start = regs[0 ].addr; + mem_end = mem_start + regs[0 ].size; + +assert (!(mem_start & ~PHYS_MASK ) && !((mem_end-1 ) & ~PHYS_MASK )); +assert (freemem_start >= mem_start && freemem_start < mem_end); + + __phys_offset = mem_start;/* PHYS_OFFSET */ + __phys_end = mem_end;/* PHYS_END */
and I think the macro indirection (__phys_offset vs. PHYS_OFFSET, __phys_end vs. PHYS_END) is unnecessary: just call the variables phys_offset and phys_end.
Paolo
Re: [PATCH v7 09/14] arm: initial drop
Quoted 56 lines · Paolo Bonzini
Quoted 15 lines
arm/unittests.cfg new file +8
-0@@ -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 ... +arm/unittests.cfg line 18
Quoted 10 lines
arm/unittests.cfg new file +10
-0+# +# 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 = selftestNice. :)
Quoted 28 lines
lib/arm/asm/page.h new file +1
-0@@ -0,0 +1 @@ +#include "asm-generic/page.h" lib/arm/asm/setup.h new file +15
-0@@ -0,0 +1,27 @@ +#ifndef _ASMARM_SETUP_H_ +#define _ASMARM_SETUP_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 "libcflat.h" +#include "alloc.h" + +#define NR_CPUS 8 +extern u32 cpus[NR_CPUS ]; +extern int nr_cpus; + +extern phys_addr_t __phys_offset, __phys_end;
Quoted 8 lines
lib/arm/asm/setup.h line 21
Quoted 6 lines
lib/arm/asm/setup.h new file +6
-0+ +#define PHYS_OFFSET (__phys_offset) +#define PHYS_END (__phys_end) +#define PHYS_SHIFT 40 +#define PHYS_SIZE (1ULL << PHYS_SHIFT) +#define PHYS_MASK (PHYS_SIZE - 1ULL)Can you explain these? I'm not sure I understand this:
arm with LPAE can address 40-bit addrs. PHYS_MASK is handy to assert all addresses we expect to be addressable, are.
Quoted 14 lines
lib/arm/asm/setup.h
Quoted 9 lines
lib/arm/asm/setup.h new file +8
-0+ mem_start = regs[0 ].addr; + mem_end = mem_start + regs[0 ].size; + +assert (!(mem_start & ~PHYS_MASK ) && !((mem_end-1 ) & ~PHYS_MASK )); +assert (freemem_start >= mem_start && freemem_start < mem_end); + + __phys_offset = mem_start;/* PHYS_OFFSET */ + __phys_end = mem_end;/* PHYS_END */ and I think the macro indirection (
__phys_offsetvs.PHYS_OFFSET,__phys_endvs.PHYS_END) is unnecessary: just call the variablesphys_offsetandphys_end.
PHYS_OFFSET is consistent with the kernel naming, so I'd like to keep that. I invented PHYS_END, as it can serve a nice utility (mem_size = PHYS_END - PHYS_OFFSET), and I wouldn't want to leave it as the odd one out by not granting it the privilege of capital letters.
drew
Paolo Bonzini
maintainerRe: [PATCH v7 09/14] arm: initial drop
Il 16/07/2014 11:39, Andrew Jones ha scritto:
Quoted 4 lines
PHYS_OFFSETis consistent with the kernel naming, so I'd like to keep that. I inventedPHYS_END, as it can serve a nice utility (mem_size=PHYS_END-PHYS_OFFSET), and I wouldn't want to leave it as the odd one out by not granting it the privilege of capital letters.
Ok, I see some de-Linuxization coming to the ARM kvm-unit-tests sooner or later, but getting things moving is more important. I'll push the tests as soon as I can try them on the cubietruck.
Paolo
PATCH v7 10/14 arm: Add spinlock implementation
In reply to Andrew JonesFrom: 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
@@ -37,7 +37,8 @@ cflatobjs += \ lib/virtio-mmio.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
@@ -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
@@ -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 { + asmvolatile ( +"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
PATCH v7 11/14 arm: Add IO accessors to avoid register-writeback
In reply to Andrew JonesFrom: 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
@@ -3,6 +3,9 @@#include "libcflat.h" #include "asm/barrier.h" +#define __iomem +#define __force +#define __bswap16 bswap16static 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; + asmvolatile ("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; + asmvolatile ("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; + asmvolatile ("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) +{ + asmvolatile ("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) +{ + asmvolatile ("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) +{ + asmvolatile ("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
PATCH v7 12/14 arm: Add arch-specific asm/page.h and __va/__pa
In reply to Andrew JonesThese 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
@@ -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
@@ -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
PATCH v7 13/14 arm: add useful headers from the Linux kernel
In reply to Andrew JonesWe'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
@@ -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
@@ -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 \
@@ -59,7 +62,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
@@ -69,7 +72,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
@@ -0,0 +1 @@ +asm-offsets.[hs]
lib/arm/asm-offsets.c new file +39 -0
@@ -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
@@ -0,0 +1 @@ +#include "generated/asm-offsets.h"
lib/arm/asm/cp15.h new file +37 -0
@@ -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
@@ -0,0 +1 @@ +*
Signature · 1 line
1.9.3
PATCH v7 14/14 arm: vectors support
In reply to Andrew JonesAdd support for tests to use exception handlers using install_exception_handler(). This patch also adds start_usr(), which can be used to start a function in USR mode, using a given stack pointer. start_usr() is used by a new selftest test that checks the new vector support.
---
v7:
- selftest.c: s/alloc_aligned/memalign/
- lib/arm/processor.c remove unnecessary include "asm/setup.h"
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 | 126 ++++++++++++++++++++++++++++++++++- arm/unittests.cfg | 12 ++++ config/config-arm.mak | 3 +- lib/arm/asm/processor.h | 39 +++++++++++ lib/arm/processor.c | 111 ++++++++++++++++++++++++++++++ 7 files changed, 469 insertions(+), 3 deletions(-) create mode 100644 lib/arm/asm/processor.h create mode 100644 lib/arm/processor.c
arm/cstart.S +174 -0
@@ -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
@@ -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 +125 -1
146 lines not on this page · raw message
arm/unittests.cfg +12 -0
@@ -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
@@ -41,7 +41,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
@@ -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; + asmvolatile ("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 +111 -0
111 lines not on this page · raw message
Signature · 1 line
1.9.3
Paolo Bonzini
maintainerIl 16/07/2014 10:47, Andrew Jones ha scritto:
Quoted 2 lines
These patches (v7) are also available from a git repo here
https://github.com/rhdrjones/kvm-unit-tests/commits/arm/v7-initial-drop
Thanks, I pulled from here -- it's not yet on kernel.org, but it should get there on Monday.
Paolo