libfdt: Import libfdt source

This adds the applicable libfdt source files (unmodified) and a
README to explain where the source came from. The README says

  The code in this directory is originally imported from the libfdt
  directory of git://git.jdl.com/software/dtc.git - version 1.4.0.

Signed-off-by: Andrew Jones <drjones@redhat.com>
Acked-by: Christoffer Dall <christoffer.dall@linaro.org>
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.

Signed-off-by: Andrew Jones <drjones@redhat.com>
Reviewed-by: Christoffer Dall <christoffer.dall@linaro.org>
---
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
Introduce asm-generic/*.h files

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

Signed-off-by: Andrew Jones <drjones@redhat.com>
Acked-by: Christoffer Dall <christoffer.dall@linaro.org>
---
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
Introduce lib/alloc

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

Signed-off-by: Andrew Jones <drjones@redhat.com>
---
v7: expanded from only supplying the alloc function wrappers to
    including early_* and phys_alloc [Paolo Bonzini]
add minimal virtio support for devtree virtio-mmio

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.

Signed-off-by: Andrew Jones <drjones@redhat.com>
Reviewed-by: Christoffer Dall <christoffer.dall@linaro.org>
---
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/asm-generic: add page.h and virt_to_phys/phys_to_virt

Signed-off-by: Andrew Jones <drjones@redhat.com>
virtio: add minimal support for virtqueues

Currently only supports sending (outbufs), doesn't have any
bells or whistles. Code adapted from the Linux Kernel.

Signed-off-by: Andrew Jones <drjones@redhat.com>

---
v7:
 - {alloc,alloc_aligned} -> {calloc,memalign}
 - changes now split between virtio.* and virtio-mmio.* files
Introduce chr-testdev

chr-testdev is a qemu backend that can be used by test code to send qemu
commands. It communicates with qemu through a virtio-console device. The
only command currently implemented is "quit", which allows the test code
to exit with a given status code, i.e. chr_testdev_exit(code).

Signed-off-by: Andrew Jones <drjones@redhat.com>
arm: initial drop

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 chr-testdev]
  ./configure --cross-prefix=arm-linux-gnu- --arch=arm
  make
  ./run_tests.sh

Signed-off-by: Andrew Jones <drjones@redhat.com>
Reviewed-by: Christoffer Dall <christoffer.dall@linaro.org>
---
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: Add spinlock implementation

Add simple busy-wait spinlock implementation for ARM.

Signed-off-by: Christoffer Dall <christoffer.dall@linaro.org>
Signed-off-by: Andrew Jones <drjones@redhat.com>
arm: Add IO accessors to avoid register-writeback

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

Signed-off-by: Christoffer Dall <christoffer.dall@linaro.org>
Signed-off-by: Andrew Jones <drjones@redhat.com>
arm: Add arch-specific asm/page.h and __va/__pa

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

Signed-off-by: Andrew Jones <drjones@redhat.com>
arm: add useful headers from the Linux kernel

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.

Signed-off-by: Andrew Jones <drjones@redhat.com>
Acked-by: Christoffer Dall <christoffer.dall@linaro.org>
---
v4: much improved asm-offsets.h generation based on Kbuild
arm: vectors support

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

Signed-off-by: Andrew Jones <drjones@redhat.com>
Reviewed-by: Christoffer Dall <christoffer.dall@linaro.org>
---
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.
x86: Check rflags.rf is cleared after emulation

RFLAGS.RF should be cleared after every instruction emulation. Recently
discovered bug indicated this is not the case. This patch adds a test to check
this behavior. It is done by setting RF, executing IRET and checking whether
the saved RF is cleared. Since the flags are saved several instructions after
IRET is executed, RF should be cleared.

Signed-off-by: Nadav Amit <namit@cs.technion.ac.il>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
x86: msr: remove invalid test

The APICBASE register is already tested by the apic testcase,
and the simple-minded tests in msr.flat actually trigger a
general protection fault nowadays.  Just drop the tests, and
the test now passes.

Reported-by: Chris J Arges <chris.j.arges@canonical.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
x86: unittests.cfg: add -cpu host to pmu and tsc_adjust

vPMU is only available with "-cpu host", and so is TSC_ADJUST because
the QEMU CPU models forgot about it.

Adjust these two tests to use the option.

Reported-by: Chris J Arges <chris.j.arges@canonical.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
x86: Check DR6.RTM is writable

Recently discovered bug shows DR6.RTM is fixed to one. The bug is only apparent
when the host emulates the MOV-DR instruction or when the host debugs the
guest kernel. This patch tests whether DR6.RTM is indeed accessible according
to RTM support as reported by cpuid.

Signed-off-by: Nadav Amit <namit@cs.technion.ac.il>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
x86: Test rflags.rf is set upon faults

This patch tests whether rflags.rf is set upon #UD and #GP faults as it should,
according to Intel SDM 17.3.1.1.  The patch saves rflags.rf in an unused bit of
the value which is saved during exception handling to save rflags.rf.

Signed-off-by: Nadav Amit <namit@cs.technion.ac.il>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
x86: emulator: correctly escape % in first argument of report

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>