KVM: ARM: Inject data abort on weird MMIO instrucitons

Since there are MMIO instructions that we just don't want to emulate
because the guest would be crazy to use them, we want to tell the guest
that this is what happened and not kill the VM aprubtly.

Signed-off-by: Christoffer Dall <c.dall@virtualopensystems.com>
ARM: opcodes: Don't define the thumb32 byteswapping macros for BE32

The existing __mem_to_opcode_thumb32() is incorrect for BE32
platforms.  However, these don't support Thumb-2 kernels, so this
option is not so relevant for those platforms anyway.

This operation is complicated by the lack of unaligned memory
access support prior to ARMv6.

Rather than provide a "working" macro which will probably won't get
used (or worse, will get misused), this patch removes the macro for
BE32 kernels.  People manipulating Thumb opcodes prior to ARMv6
should almost certainly be splitting these operations into
halfwords anyway, using __opcode_thumb32_{first,second,compose}()
and the 16-bit opcode transformations.

Signed-off-by: Dave Martin <dave.martin@linaro.org>
Acked-by: Nicolas Pitre <nico@linaro.org>
ARM: opcodes: Make opcode byteswapping macros assembly-compatible

Most of the existing macros don't work with assembler, due to the
use of type casts and C functions from <linux/swab.h>.

This patch abstracts out those operations and provides simple
explicit versions for use in assembly code.

__opcode_is_thumb32() and __opcode_is_thumb16() are also converted
to do bitmask-based testing to avoid confusion if these are used in
assembly code (the assembler typically treats all arithmetic values
as signed).

These changes avoid the need for the compiler to pre-evaluate
constant expressions used to generate opcodes.  By ensuring that
the forms of these expressions can be evaluated directly by the
assembler, we can just stringify the expressions directly into the
asm during the preprocessing pass.  The alternative approach
(passing the evaluated expression via an inline asm "i" constraint)
gets painful because the contents of the asm and the constraints
must be kept in sync.  This makes the resulting macros awkward to
use.

Retaining the C forms of the macros allows more efficient code to
be generated when opcodes are generated programmatically at run-
time, but there is no way to embed run-time-generated opcodes in
asm() blocks.

Signed-off-by: Dave Martin <dave.martin@linaro.org>
Acked-by: Nicolas Pitre <nico@linaro.org>
ARM: opcodes: Add helpers for emitting custom opcodes

This patch adds some __inst_() macros for injecting custom opcodes
in assembler (both inline and in .S files).  They should make it
easier and cleaner to get things right in little-/big-
endian/ARM/Thumb-2 kernels without a lot of #ifdefs.

This pure-preprocessor approach is preferred over the alternative
method of wedging extra assembler directives into the assembler
input using top-level asm() blocks, since there is no way to
guarantee that the compiler won't reorder those with respect to
each other or with respect to non-toplevel asm() blocks, unless
-fno-toplevel-reorder is passed (which is in itself somewhat
undesirable because it defeats some potential optimisations).

Currently <asm/unified.h> _does_ silently rely on the compiler not
reordering at the top level, but it seems better to avoid adding
extra code which depends on this if the same result can be achieved
in another way.

Signed-off-by: Dave Martin <dave.martin@linaro.org>
Acked-by: Nicolas Pitre <nico@linaro.org>
ARM: opcodes: Opcode definitions for the Virtualization Extensions

For now, this patch just adds a definition for the HVC instruction.
More can be added here later, as needed.

Now that we have a real example of how to use the opcode injection
macros properly, this patch also adds a cross-reference from the
explanation in opcodes.h (since without an example, figuring out
how to use the macros is not that easy).

Signed-off-by: Dave Martin <dave.martin@linaro.org>
Acked-by: Nicolas Pitre <nico@linaro.org>
ARM: opcodes: add __ERET/__MSR_ELR_HYP instruction encoding

Enabling boot from HYP mode requires the use of some more
virt-specific instructions ("eret" and "msr elr_hyp, reg").

Add the necessary encoding to asm/opcode-virt.h.

Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
ARM: virt: allow the kernel to be entered in HYP mode

This patch does two things:

  * Ensure that asynchronous aborts are masked at kernel entry.
    The bootloader should be masking these anyway, but this reduces
    the damage window just in case it doesn't.

  * Enter svc mode via exception return to ensure that CPU state is
    properly serialised.  This does not matter when switching from
    an ordinary privileged mode ("PL1" modes in ARMv7-AR rev C
    parlance), but it potentially does matter when switching from a
    another privileged mode such as hyp mode.

This should allow the kernel to boot safely either from svc mode or
hyp mode, even if no support for use of the ARM Virtualization
Extensions is built into the kernel.

Signed-off-by: Dave Martin <dave.martin@linaro.org>
Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
ARM: zImage/virt: hyp mode entry support for the zImage loader

The zImage loader needs to turn on the MMU in order to take
advantage of caching while decompressing the zImage.  Running this
in hyp mode would require the LPAE pagetable format to be
supported; to avoid this complexity, this patch switches out of hyp
mode, and returns back to hyp mode just before booting the kernel.

This implementation assumes that the Hyp mode view of memory and the
PL1 view of memory are coherent, providing that the MMU and caches
are off in both, as required by the boot protocol.  The zImage
decompression code must drain the write buffer on completion anyway, and
entry into Hyp mode should flush any prefetch buffer, avoiding hazards
associated with local write buffers and the pipeline.

Signed-off-by: Dave Martin <dave.martin@linaro.org>
Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
ARM: virt: Update documentation for hyp mode entry support

Document the possibility of the kernel being entered in HYP mode.

Signed-off-by: Dave Martin <dave.martin@linaro.org>
Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
ARM: virt: Add boot-time diagnostics

In order to easily detect pathological cases, print some diagnostics
when the kernel boots.

This also provides helpers to detect that HYP mode is actually available,
which can be used by other subsystems to enable HYP specific features.

Signed-off-by: Dave Martin <dave.martin@linaro.org>
Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
ARM: virt: Add CONFIG_ARM_VIRT_EXT option

It is now possible to enable the virtualization extention support.

Signed-off-by: Dave Martin <dave.martin@linaro.org>
Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
ARM: virt: arch_timers: enable access to physical timers

If booting in HYP mode, it makes sense to enable the use of the
physical timers, so the kernel can use them directly.

Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
ARM: KVM: remove CONFIG_ARM_VIRT_EXT

Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
ARM: KVM: Change KVM/ARM dependencies

We used to have ARM_VIRT_EXT depending on ARM_LPAE, and KVM
depending on CPU_V7 && ARM_VIRT_EXT.

ARM_VIRT_EXT doesn't depend on ARM_LPAE anymore (no reason it
should, this is a KVM limitation), but now depends on CPU_v7 & MMU.

To preserve the dependencies, change KVM to depend on ARM_VIRT_EXT &
ARM_LPAE.

Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
ARM: KVM: Switch HVBAR setting to be HVC-based

Instead of relying on SMC calls to setup HVBAR, introduce a new
HVC-based API. This of course requires a hypervisor stub to be
installed already. At the moment, this is implemented as part of the
boot wrapper, but will be moved to the kernel itself when it actually
understands being booted in HYP mode.

The kernel now uses "hvc #0xff", with r0 containing the pointer to
the new vectors.

Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
ARM: KVM: make init.S Thumb compliant

Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
ARM: idmap: only initialize HYP idmap when HYP mode is available

Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
fixup! ARM: KVM: add page accounting and guest page table eviction

Just a few cosmetic fixes that were in my tree and didn't make it
into the v10 tree yet.

Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
ARM: KVM: simplify stack layout in HYP mode

On world switch, we save the pointer to the current vcpu on the top
of the stack. This tends to complicate the exit path a bit as we then
need to dig into the stack to restore it (we've already pushed a number
of registers on top of it at that time), not to mention error prone.

A obvious way to simplify this is to use the HTPIDR register, which is an
architected method to store per-CPU data in HYP mode, and this is
exactly what this patch does.

Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
ARM: KVM: don't choke when unmapping a page without a stage2 translation

stage2_clear_pte() has a few BUG_ON too many. It should be perfectly
legal to unmap pages from the host, even when they do not have a 2nd
stage translation in place.

Reported-by: Andreas Sandberg <andreas.sandberg@arm.com>
Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>