Re: [PATCH v5 15/30] arm64/sve: Signal handling support

2017-12-07 Thread Kees Cook
On Thu, Dec 7, 2017 at 2:49 AM, Will Deacon wrote: > Hi Kees, > > On Wed, Dec 06, 2017 at 11:56:50AM -0800, Kees Cook wrote: >> On Tue, Oct 31, 2017 at 8:51 AM, Dave Martin wrote: >> > Miscellaneous: >> > >> > * Change inconsistent copy_to_user() calls to __copy_to_user() in >> >preserve_sve

[PATCH v2 35/36] KVM: arm/arm64: Move VGIC APR save/restore to vgic put/load

2017-12-07 Thread Christoffer Dall
The APRs can only have bits set when the guest acknowledges an interrupt in the LR and can only have a bit cleared when the guest EOIs an interrupt in the LR. Therefore, if we have no LRs with any pending/active interrupts, the APR cannot change value and there is no need to clear it on every exit

[PATCH v2 27/36] KVM: arm64: Move common VHE/non-VHE trap config in separate functions

2017-12-07 Thread Christoffer Dall
As we are about to be more lazy with some of the trap configuration register read/writes for VHE systems, move the logic that is currently shared between VHE and non-VHE into a separate function which can be called from either the world-switch path or from vcpu_load/vcpu_put. Signed-off-by: Christ

[PATCH v2 28/36] KVM: arm64: Configure FPSIMD traps on vcpu load/put for VHE

2017-12-07 Thread Christoffer Dall
There is no need to enable/disable traps to FP registers on every switch to/from the VM, because the host kernel does not use this resource without calling vcpu_put. We can therefore move things around enough that we still always write FPEXC32_EL2 before programming CPTR_EL2 but only program these

[PATCH v2 25/36] KVM: arm64: Prepare to handle traps on deferred AArch32 sysregs

2017-12-07 Thread Christoffer Dall
Handle accesses to any AArch32 EL1 system registers where we can defer saving and restoring them to vcpu_load and vcpu_put, and which are stored in special EL2 registers only used support 32-bit guests. Signed-off-by: Christoffer Dall --- arch/arm64/include/asm/kvm_emulate.h | 9 - 1 fil

[PATCH v2 33/36] KVM: arm/arm64: Move arm64-only vgic-v2-sr.c file to arm64

2017-12-07 Thread Christoffer Dall
The vgic-v2-sr.c file now only contains the logic to replay unaligned accesses to the virtual CPU interface on 16K and 64K page systems, which is only relevant on 64-bit platforms. Therefore move this file to the arm64 KVM tree, remove the compile directive from the 32-bit side makefile, and remov

[PATCH v2 29/36] KVM: arm64: Configure c15, PMU, and debug register traps on cpu load/put for VHE

2017-12-07 Thread Christoffer Dall
We do not have to change the c15 trap setting on each switch to/from the guest on VHE systems, because this setting only affects EL0. The PMU and debug trap configuration can also be done on vcpu load/put instead, because they don't affect how the host kernel can access the debug registers while e

[PATCH v2 30/36] KVM: arm64: Separate activate_traps and deactive_traps for VHE and non-VHE

2017-12-07 Thread Christoffer Dall
To make the code more readable and to avoid the overhead of a function call, let's get rid of a pair of the alternative function selectors and explicitly call the VHE and non-VHE functions instead, telling the compiler to try to inline the static function if it can. Signed-off-by: Christoffer Dall

[PATCH v2 31/36] KVM: arm/arm64: Get rid of vgic_elrsr

2017-12-07 Thread Christoffer Dall
There is really no need to store the vgic_elrsr on the VGIC data structures as the only need we have for the elrsr is to figure out if an LR is inactive when we save the VGIC state upon returning from the guest. We can might as well store this in a temporary local variable. This also gets rid of

[PATCH v2 22/36] KVM: arm64: Prepare to handle traps on deferred VM sysregs

2017-12-07 Thread Christoffer Dall
When we defer the save/restore of system registers to vcpu_load and vcpu_put, we need to take care of the emulation code that handles traps to these registers, since simply reading the memory array will return stale data. Therefore, introduce two functions to directly read/write the registers from

[PATCH v2 32/36] KVM: arm/arm64: Handle VGICv2 save/restore from the main VGIC code

2017-12-07 Thread Christoffer Dall
We can program the GICv2 hypervisor control interface logic directly from the core vgic code and can instead do the save/restore directly from the flush/sync functions, which can lead to a number of future optimizations. Signed-off-by: Christoffer Dall --- Notes: Changes since v1: - Rem

[PATCH v2 23/36] KVM: arm64: Prepare to handle traps on deferred EL0 sysregs

2017-12-07 Thread Christoffer Dall
We can trap access to ACTLR_EL1 which we can later defer to only save/restore during vcpu_load and vcpu_put, so let's read the value directly from the CPU when necessary. Signed-off-by: Christoffer Dall --- Notes: Changes since v1: - Fix bug in access_actlr that read the actlr_el1 and t

[PATCH v2 24/36] KVM: arm64: Prepare to handle traps on remaining deferred EL1 sysregs

2017-12-07 Thread Christoffer Dall
Handle accesses during traps to any remaining EL1 registers which can be deferred to vcpu_load and vcpu_put, by either accessing them directly on the physical CPU when the latest version is stored there, or by synchronizing the memory representation with the CPU state. Signed-off-by: Christoffer D

[PATCH v2 26/36] KVM: arm64: Defer saving/restoring system registers to vcpu load/put on VHE

2017-12-07 Thread Christoffer Dall
Some system registers do not affect the host kernel's execution and can therefore be loaded when we are about to run a VCPU and we don't have to restore the host state to the hardware before the time when we are actually about to return to userspace or schedule out the VCPU thread. The EL1 system

[PATCH v2 34/36] KVM: arm/arm64: Handle VGICv3 save/restore from the main VGIC code on VHE

2017-12-07 Thread Christoffer Dall
Just like we can program the GICv2 hypervisor control interface directly from the core vgic code, we can do the same for the GICv3 hypervisor control interface on VHE systems. We do this by simply calling the save/restore functions when we have VHE and we can then get rid of the save/restore funct

[PATCH v2 36/36] KVM: arm/arm64: Avoid VGICv3 save/restore on VHE with no IRQs

2017-12-07 Thread Christoffer Dall
We can finally get completely rid of any calls to the VGICv3 save/restore functions when the AP lists are empty on VHE systems. This requires carefully factoring out trap configuration from saving and restoring state, and carefully choosing what to do on the VHE and non-VHE path. One of the chall

[PATCH v2 16/36] KVM: arm64: Rewrite sysreg alternatives to static keys

2017-12-07 Thread Christoffer Dall
As we are about to move calls around in the sysreg save/restore logic, let's first rewrite the alternative function callers, because it is going to make the next patches much easier to read. Signed-off-by: Christoffer Dall --- arch/arm64/kvm/hyp/sysreg-sr.c | 17 - 1 file changed

[PATCH v2 21/36] KVM: arm64: Change 32-bit handling of VM system registers

2017-12-07 Thread Christoffer Dall
We currently handle 32-bit accesses to trapped VM system registers using the 32-bit index into the coproc array on the vcpu structure, which is a union of the coproc array and the sysreg array. Since all the 32-bit coproc indicies are created to correspond to the architectural mapping between 64-b

[PATCH v2 17/36] KVM: arm64: Introduce separate VHE/non-VHE sysreg save/restore functions

2017-12-07 Thread Christoffer Dall
As we are about to handle system registers quite differently between VHE and non-VHE systems. In preparation for that, we need to split some of the handling functions between VHE and non-VHE functionality. For now, we simply copy the non-VHE functions, but we do change the use of static keys for

[PATCH v2 12/36] KVM: arm64: Remove kern_hyp_va() use in VHE switch function

2017-12-07 Thread Christoffer Dall
VHE kernels run completely in EL2 and therefore don't have a notion of kernel and hyp addresses, they are all just kernel addresses. Therefore don't call kern_hyp_va() in the VHE switch function. Reviewed-by: Andrew Jones Signed-off-by: Christoffer Dall --- arch/arm64/kvm/hyp/switch.c | 4 +---

[PATCH v2 14/36] KVM: arm64: Remove noop calls to timer save/restore from VHE switch

2017-12-07 Thread Christoffer Dall
The VHE switch function calls __timer_enable_traps and __timer_disable_traps which don't do anything on VHE systems. Therefore, simply remove these calls from the VHE switch function and make the functions non-conditional as they are now only called from the non-VHE switch path. Signed-off-by: Chr

[PATCH v2 13/36] KVM: arm64: Don't deactivate VM on VHE systems

2017-12-07 Thread Christoffer Dall
There is no need to reset the VTTBR to zero when exiting the guest on VHE systems. VHE systems don't use stage 2 translations for the EL2&0 translation regime used by the host. Reviewed-by: Andrew Jones Signed-off-by: Christoffer Dall --- Notes: Changes since v1: - Changed __activate_

[PATCH v2 18/36] KVM: arm/arm64: Remove leftover comment from kvm_vcpu_run_vhe

2017-12-07 Thread Christoffer Dall
The comment only applied to SPE on non-VHE systems, so we simply remove it. Suggested-by: Andrew Jones Signed-off-by: Christoffer Dall --- arch/arm64/kvm/hyp/switch.c | 4 1 file changed, 4 deletions(-) diff --git a/arch/arm64/kvm/hyp/switch.c b/arch/arm64/kvm/hyp/switch.c index 13c990c6e

[PATCH v2 19/36] KVM: arm64: Unify non-VHE host/guest sysreg save and restore functions

2017-12-07 Thread Christoffer Dall
There is no need to have multiple identical functions with different names for saving host and guest state. When saving and restoring state for the host and guest, the state is the same for both contexts, and that's why we have the kvm_cpu_context structure. Delete one version and rename the othe

[PATCH v2 15/36] KVM: arm64: Move userspace system registers into separate function

2017-12-07 Thread Christoffer Dall
There's a semantic difference between the EL1 registers that control operation of a kernel running in EL1 and EL1 registers that only control userspace execution in EL0. Since we can defer saving/restoring the latter, move them into their own function. We also take this chance to rename the funct

[PATCH v2 11/36] KVM: arm64: Introduce VHE-specific kvm_vcpu_run

2017-12-07 Thread Christoffer Dall
So far this is just a copy of the legacy non-VHE switch function, where we only change the existing calls to has_vhe() in both the original and new functions. Signed-off-by: Christoffer Dall --- Notes: Changes since v1: - Rename kvm_vcpu_run to kvm_vcpu_run_vhe and rename __kvm_vcpu_run

[PATCH v2 20/36] KVM: arm64: Don't save the host ELR_EL2 and SPSR_EL2 on VHE systems

2017-12-07 Thread Christoffer Dall
On non-VHE systems we need to save the ELR_EL2 and SPSR_EL2 so that we can return to the host in EL1 in the same state and location where we issued a hypercall to EL2, but these registers don't contain anything important on VHE, because all of the host runs in EL2. Therefore, factor out these regi

[PATCH v2 08/36] KVM: arm64: Slightly improve debug save/restore functions

2017-12-07 Thread Christoffer Dall
The debug save/restore functions can be improved by using the has_vhe() static key instead of the instruction alternative. Using the static key uses the same paradigm as we're going to use elsewhere, it makes the code more readable, and it generates slightly better code (no stack setups and functi

[PATCH v2 07/36] KVM: arm64: Move debug dirty flag calculation out of world switch

2017-12-07 Thread Christoffer Dall
There is no need to figure out inside the world-switch if we should save/restore the debug registers or not, we can might as well do that in the higher level debug setup code, making it easier to optimize down the line. Signed-off-by: Christoffer Dall --- arch/arm64/kvm/debug.c| 5 +

[PATCH v2 10/36] KVM: arm64: Factor out fault info population and gic workarounds

2017-12-07 Thread Christoffer Dall
The current world-switch function has functionality to detect a number of cases where we need to fixup some part of the exit condition and possibly run the guest again, before having restored the host state. This includes populating missing fault info, emulating GICv2 CPU interface accesses when m

[PATCH v2 06/36] KVM: arm64: Defer restoring host VFP state to vcpu_put

2017-12-07 Thread Christoffer Dall
Avoid saving the guest VFP registers and restoring the host VFP registers on every exit from the VM. Only when we're about to run userspace or other threads in the kernel do we really have to switch the state back to the host state. We still initially configure the VFP registers to trap when ente

[PATCH v2 05/36] KVM: arm/arm64: Add kvm_vcpu_load_sysregs and kvm_vcpu_put_sysregs

2017-12-07 Thread Christoffer Dall
As we are about to move a bunch of save/restore logic for VHE kernels to the load and put functions, we need some infrastructure to do this. Reviewed-by: Andrew Jones Signed-off-by: Christoffer Dall --- Notes: Changes since v1: - Reworded comments as suggested by Drew arch/arm/includ

[PATCH v2 09/36] KVM: arm64: Improve debug register save/restore flow

2017-12-07 Thread Christoffer Dall
Instead of having multiple calls from the world switch path to the debug logic, each figuring out if the dirty bit is set and if we should save/restore the debug registers, let's just provide two hooks to the debug save/restore functionality, one for switching to the guest context, and one for swit

[PATCH v2 04/36] KVM: arm/arm64: Get rid of vcpu->arch.irq_lines

2017-12-07 Thread Christoffer Dall
We currently have a separate read-modify-write of the HCR_EL2 on entry to the guest for the sole purpose of setting the VF and VI bits, if set. Since this is most rarely the case (only when using userspace IRQ chip and interrupts are in flight), let's get rid of this operation and instead modify th

[PATCH v2 03/36] KVM: arm64: Move HCR_INT_OVERRIDE to default HCR_EL2 guest flag

2017-12-07 Thread Christoffer Dall
From: Shih-Wei Li We always set the IMO and FMO bits in the HCR_EL2 when running the guest, regardless if we use the vgic or not. By moving these flags to HCR_GUEST_FLAGS we can avoid one of the extra save/restore operations of HCR_EL2 in the world switch code, and we can also soon get rid of th

[PATCH v2 00/36] Optimize KVM/ARM for VHE systems

2017-12-07 Thread Christoffer Dall
This series redesigns parts of KVM/ARM to optimize the performance on VHE systems. The general approach is to try to do as little work as possible when transitioning between the VM and the hypervisor. This has the benefit of lower latency when waiting for interrupts and delivering virtual interru

[PATCH v2 02/36] KVM: arm64: Rework hyp_panic for VHE and non-VHE

2017-12-07 Thread Christoffer Dall
VHE actually doesn't rely on clearing the VTTBR when returning to the host kernel, and that is the current key mechanism of hyp_panic to figure out how to attempt to return to a state good enough to print a panic statement. Therefore, we split the hyp_panic function into two functions, a VHE and a

[PATCH v2 01/36] KVM: arm64: Avoid storing the vcpu pointer on the stack

2017-12-07 Thread Christoffer Dall
We already have the percpu area for the host cpu state, which points to the VCPU, so there's no need to store the VCPU pointer on the stack on every context switch. We can be a little more clever and just use tpidr_el2 for the percpu offset and load the VCPU pointer from the host context. This do

Re: [PATCH] KVM: arm/arm64: timer: Don't set irq as forwarded if no usable GIC

2017-12-07 Thread Andre Przywara
Hi, On 07/12/17 11:46, Marc Zyngier wrote: > If we don't have a usable GIC, do not try to set the vcpu affinity > as this is guaranteed to fail. Yes, I can confirm that this fixes the problem. With this patch and a DT advertising only a 4K GICC region size KVM still initializes, but denies the in

Re: [PATCH] KVM: arm/arm64: Fix HYP unmapping going off limits

2017-12-07 Thread Andre Przywara
Hi, On 07/12/17 11:45, Marc Zyngier wrote: > When we unmap the HYP memory, we try to be clever and unmap one > PGD at a time. If we start with a non-PGD aligned address and try > to unmap a whole PGD, things go horribly wrong in unmap_hyp_range > (addr and end can never match, and it all goes real

Re: [PATCH v5 15/30] arm64/sve: Signal handling support

2017-12-07 Thread Dave Martin
On Thu, Dec 07, 2017 at 10:49:48AM +, Will Deacon wrote: > Hi Kees, > > On Wed, Dec 06, 2017 at 11:56:50AM -0800, Kees Cook wrote: > > On Tue, Oct 31, 2017 at 8:51 AM, Dave Martin wrote: > > > Miscellaneous: > > > > > > * Change inconsistent copy_to_user() calls to __copy_to_user() in > > >

[PATCH] KVM: arm/arm64: timer: Don't set irq as forwarded if no usable GIC

2017-12-07 Thread Marc Zyngier
If we don't have a usable GIC, do not try to set the vcpu affinity as this is guaranteed to fail. Reported-by: Andre Przywara Signed-off-by: Marc Zyngier --- include/kvm/arm_arch_timer.h | 2 +- virt/kvm/arm/arch_timer.c| 13 - virt/kvm/arm/arm.c | 2 +- 3 files chan

[PATCH] KVM: arm/arm64: Fix HYP unmapping going off limits

2017-12-07 Thread Marc Zyngier
When we unmap the HYP memory, we try to be clever and unmap one PGD at a time. If we start with a non-PGD aligned address and try to unmap a whole PGD, things go horribly wrong in unmap_hyp_range (addr and end can never match, and it all goes really badly as we keep incrementing pgd and parse rando

[PATCH v7 3/8] KVM: arm/arm64: Don't cache the timer IRQ level

2017-12-07 Thread Christoffer Dall
The timer was modeled after a strict idea of modelling an interrupt line level in software, meaning that only transitions in the level needed to be reported to the VGIC. This works well for the timer, because the arch timer code is in complete control of the device and can track the transitions of

[PATCH v7 6/8] KVM: arm/arm64: Support VGIC dist pend/active changes for mapped IRQs

2017-12-07 Thread Christoffer Dall
For mapped IRQs (with the HW bit set in the LR) we have to follow some rules of the architecture. One of these rules is that VM must not be allowed to deactivate a virtual interrupt with the HW bit set unless the physical interrupt is also active. This works fine when injecting mapped interrupts,

[PATCH v7 5/8] KVM: arm/arm64: Support a vgic interrupt line level sample function

2017-12-07 Thread Christoffer Dall
The GIC sometimes need to sample the physical line of a mapped interrupt. As we know this to be notoriously slow, provide a callback function for devices (such as the timer) which can do this much faster than talking to the distributor, for example by comparing a few in-memory values. Fall back t

[PATCH v7 8/8] KVM: arm/arm64: Avoid work when userspace iqchips are not used

2017-12-07 Thread Christoffer Dall
We currently check if the VM has a userspace irqchip on every exit from the VCPU, and if so, we do some work to ensure correct timer behavior. This is unfortunate, as we could avoid doing any work entirely, if we didn't have to support irqchip in userspace. Realizing the userspace irqchip on ARM i

[PATCH v7 7/8] KVM: arm/arm64: Provide a get_input_level for the arch timer

2017-12-07 Thread Christoffer Dall
The VGIC can now support the life-cycle of mapped level-triggered interrupts, and we no longer have to read back the timer state on every exit from the VM if we had an asserted timer interrupt signal, because the VGIC already knows if we hit the unlikely case where the guest disables the timer with

[PATCH v7 4/8] KVM: arm/arm64: vgic: Support level-triggered mapped interrupts

2017-12-07 Thread Christoffer Dall
Level-triggered mapped IRQs are special because we only observe rising edges as input to the VGIC, and we don't set the EOI flag and therefore are not told when the level goes down, so that we can re-queue a new interrupt when the level goes up. One way to solve this problem is to side-step the lo

[PATCH v7 2/8] KVM: arm/arm64: Factor out functionality to get vgic mmio requester_vcpu

2017-12-07 Thread Christoffer Dall
We are about to distinguish between userspace accesses and mmio traps for a number of the mmio handlers. When the requester vcpu is NULL, it means we are handling a userspace access. Factor out the functionality to get the request vcpu into its own function, mostly so we have a common place to do

[PATCH v7 0/8] Handle forwarded level-triggered interrupts

2017-12-07 Thread Christoffer Dall
This series is an alternative approach to Eric Auger's direct EOI setup patches [1] in terms of the KVM VGIC support. The idea is to maintain existing semantics for the VGIC for mapped level-triggered IRQs and also support the timer using mapped IRQs with the same VGIC support as VFIO interrupts.

[PATCH v7 1/8] KVM: arm/arm64: Remove redundant preemptible checks

2017-12-07 Thread Christoffer Dall
The __this_cpu_read() and __this_cpu_write() functions already implement checks for the required preemption levels when using CONFIG_DEBUG_PREEMPT which gives you nice error messages and such. Therefore there is no need to explicitly check this using a BUG_ON() in the code (which we don't do for ot

Re: [PATCH v5 08/16] arm64: Add vmap_stack header file

2017-12-07 Thread Mark Rutland
Hi James, On Wed, Dec 06, 2017 at 07:01:34PM +, James Morse wrote: > Today the arm64 arch code allocates an extra IRQ stack per-cpu. If we > also have SDEI and VMAP stacks we need two extra per-cpu VMAP stacks. > > Move the VMAP stack allocation out to a helper in a new header file. > This av

Re: [PATCH v5 15/30] arm64/sve: Signal handling support

2017-12-07 Thread Will Deacon
Hi Kees, On Wed, Dec 06, 2017 at 11:56:50AM -0800, Kees Cook wrote: > On Tue, Oct 31, 2017 at 8:51 AM, Dave Martin wrote: > > Miscellaneous: > > > > * Change inconsistent copy_to_user() calls to __copy_to_user() in > >preserve_sve_context(). > > > >There are already __put_user_error() ca

Re: [PATCH] arm64: kvm: Prevent restoring stale PMSCR_EL1 for vcpu

2017-12-07 Thread Christoffer Dall
On Wed, Dec 06, 2017 at 05:09:49PM +, Julien Thierry wrote: > When VHE is not present, KVM needs to save and restores PMSCR_EL1 when > possible. If SPE is used by the host, value of PMSCR_EL1 cannot be saved > for the guest. > If the host starts using SPE between two save+restore on the same vc