2019-06-03 05:44:50 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
2012-12-10 13:27:52 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012,2013 - ARM Ltd
|
|
|
|
* Author: Marc Zyngier <marc.zyngier@arm.com>
|
|
|
|
*
|
|
|
|
* Derived from arch/arm/include/kvm_emulate.h
|
|
|
|
* Copyright (C) 2012 - Virtual Open Systems and Columbia University
|
|
|
|
* Author: Christoffer Dall <c.dall@virtualopensystems.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __ARM64_KVM_EMULATE_H__
|
|
|
|
#define __ARM64_KVM_EMULATE_H__
|
|
|
|
|
|
|
|
#include <linux/kvm_host.h>
|
2014-11-24 13:59:30 +00:00
|
|
|
|
2018-11-09 15:07:11 +00:00
|
|
|
#include <asm/debug-monitors.h>
|
2014-11-24 13:59:30 +00:00
|
|
|
#include <asm/esr.h>
|
2012-12-10 13:27:52 +00:00
|
|
|
#include <asm/kvm_arm.h>
|
2017-12-27 19:01:52 +00:00
|
|
|
#include <asm/kvm_hyp.h>
|
2012-12-10 13:27:52 +00:00
|
|
|
#include <asm/ptrace.h>
|
2014-06-02 13:37:13 +00:00
|
|
|
#include <asm/cputype.h>
|
2015-01-29 15:47:55 +00:00
|
|
|
#include <asm/virt.h>
|
2012-12-10 13:27:52 +00:00
|
|
|
|
2013-02-06 19:40:29 +00:00
|
|
|
unsigned long *vcpu_reg32(const struct kvm_vcpu *vcpu, u8 reg_num);
|
2017-12-27 20:59:09 +00:00
|
|
|
unsigned long vcpu_read_spsr32(const struct kvm_vcpu *vcpu);
|
|
|
|
void vcpu_write_spsr32(struct kvm_vcpu *vcpu, unsigned long v);
|
2013-02-06 19:40:29 +00:00
|
|
|
|
2013-02-06 19:54:04 +00:00
|
|
|
bool kvm_condition_valid32(const struct kvm_vcpu *vcpu);
|
|
|
|
void kvm_skip_instr32(struct kvm_vcpu *vcpu, bool is_wide_instr);
|
|
|
|
|
2012-12-10 13:27:52 +00:00
|
|
|
void kvm_inject_undefined(struct kvm_vcpu *vcpu);
|
2016-09-06 13:02:01 +00:00
|
|
|
void kvm_inject_vabt(struct kvm_vcpu *vcpu);
|
2012-12-10 13:27:52 +00:00
|
|
|
void kvm_inject_dabt(struct kvm_vcpu *vcpu, unsigned long addr);
|
|
|
|
void kvm_inject_pabt(struct kvm_vcpu *vcpu, unsigned long addr);
|
2017-10-29 02:18:09 +00:00
|
|
|
void kvm_inject_undef32(struct kvm_vcpu *vcpu);
|
|
|
|
void kvm_inject_dabt32(struct kvm_vcpu *vcpu, unsigned long addr);
|
|
|
|
void kvm_inject_pabt32(struct kvm_vcpu *vcpu, unsigned long addr);
|
2012-12-10 13:27:52 +00:00
|
|
|
|
2020-02-20 16:58:37 +00:00
|
|
|
static __always_inline bool vcpu_el1_is_32bit(struct kvm_vcpu *vcpu)
|
2017-12-13 21:56:48 +00:00
|
|
|
{
|
|
|
|
return !(vcpu->arch.hcr_el2 & HCR_RW);
|
|
|
|
}
|
|
|
|
|
2014-10-16 15:21:16 +00:00
|
|
|
static inline void vcpu_reset_hcr(struct kvm_vcpu *vcpu)
|
|
|
|
{
|
|
|
|
vcpu->arch.hcr_el2 = HCR_GUEST_FLAGS;
|
2015-01-29 15:47:55 +00:00
|
|
|
if (is_kernel_in_hyp_mode())
|
|
|
|
vcpu->arch.hcr_el2 |= HCR_E2H;
|
2018-01-15 19:39:06 +00:00
|
|
|
if (cpus_have_const_cap(ARM64_HAS_RAS_EXTN)) {
|
|
|
|
/* route synchronous external abort exceptions to EL2 */
|
|
|
|
vcpu->arch.hcr_el2 |= HCR_TEA;
|
|
|
|
/* trap error record accesses */
|
|
|
|
vcpu->arch.hcr_el2 |= HCR_TERR;
|
|
|
|
}
|
KVM: arm64: Don't set HCR_EL2.TVM when S2FWB is supported
On CPUs that support S2FWB (Armv8.4+), KVM configures the stage 2 page
tables to override the memory attributes of memory accesses, regardless
of the stage 1 page table configurations, and also when the stage 1 MMU
is turned off. This results in all memory accesses to RAM being
cacheable, including during early boot of the guest.
On CPUs without this feature, memory accesses were non-cacheable during
boot until the guest turned on the stage 1 MMU, and we had to detect
when the guest turned on the MMU, such that we could invalidate all cache
entries and ensure a consistent view of memory with the MMU turned on.
When the guest turned on the caches, we would call stage2_flush_vm()
from kvm_toggle_cache().
However, stage2_flush_vm() walks all the stage 2 tables, and calls
__kvm_flush-dcache_pte, which on a system with S2FWB does ... absolutely
nothing.
We can avoid that whole song and dance, and simply not set TVM when
creating a VM on a system that has S2FWB.
Signed-off-by: Christoffer Dall <christoffer.dall@arm.com>
Signed-off-by: Marc Zyngier <maz@kernel.org>
Reviewed-by: Mark Rutland <mark.rutland@arm.com>
Link: https://lore.kernel.org/r/20191028130541.30536-1-christoffer.dall@arm.com
2019-10-28 13:05:41 +00:00
|
|
|
|
|
|
|
if (cpus_have_const_cap(ARM64_HAS_STAGE2_FWB)) {
|
2018-04-06 11:27:28 +00:00
|
|
|
vcpu->arch.hcr_el2 |= HCR_FWB;
|
KVM: arm64: Don't set HCR_EL2.TVM when S2FWB is supported
On CPUs that support S2FWB (Armv8.4+), KVM configures the stage 2 page
tables to override the memory attributes of memory accesses, regardless
of the stage 1 page table configurations, and also when the stage 1 MMU
is turned off. This results in all memory accesses to RAM being
cacheable, including during early boot of the guest.
On CPUs without this feature, memory accesses were non-cacheable during
boot until the guest turned on the stage 1 MMU, and we had to detect
when the guest turned on the MMU, such that we could invalidate all cache
entries and ensure a consistent view of memory with the MMU turned on.
When the guest turned on the caches, we would call stage2_flush_vm()
from kvm_toggle_cache().
However, stage2_flush_vm() walks all the stage 2 tables, and calls
__kvm_flush-dcache_pte, which on a system with S2FWB does ... absolutely
nothing.
We can avoid that whole song and dance, and simply not set TVM when
creating a VM on a system that has S2FWB.
Signed-off-by: Christoffer Dall <christoffer.dall@arm.com>
Signed-off-by: Marc Zyngier <maz@kernel.org>
Reviewed-by: Mark Rutland <mark.rutland@arm.com>
Link: https://lore.kernel.org/r/20191028130541.30536-1-christoffer.dall@arm.com
2019-10-28 13:05:41 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* For non-FWB CPUs, we trap VM ops (HCR_EL2.TVM) until M+C
|
|
|
|
* get set in SCTLR_EL1 such that we can detect when the guest
|
|
|
|
* MMU gets turned on and do the necessary cache maintenance
|
|
|
|
* then.
|
|
|
|
*/
|
|
|
|
vcpu->arch.hcr_el2 |= HCR_TVM;
|
|
|
|
}
|
2018-01-15 19:39:06 +00:00
|
|
|
|
2015-01-11 13:10:11 +00:00
|
|
|
if (test_bit(KVM_ARM_VCPU_EL1_32BIT, vcpu->arch.features))
|
|
|
|
vcpu->arch.hcr_el2 &= ~HCR_RW;
|
2017-12-01 15:19:40 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* TID3: trap feature register accesses that we virtualise.
|
|
|
|
* For now this is conditional, since no AArch32 feature regs
|
|
|
|
* are currently virtualised.
|
|
|
|
*/
|
2017-12-13 21:56:48 +00:00
|
|
|
if (!vcpu_el1_is_32bit(vcpu))
|
2017-12-01 15:19:40 +00:00
|
|
|
vcpu->arch.hcr_el2 |= HCR_TID3;
|
2019-01-31 13:17:17 +00:00
|
|
|
|
2019-01-31 13:17:18 +00:00
|
|
|
if (cpus_have_const_cap(ARM64_MISMATCHED_CACHE_TYPE) ||
|
|
|
|
vcpu_el1_is_32bit(vcpu))
|
2019-01-31 13:17:17 +00:00
|
|
|
vcpu->arch.hcr_el2 |= HCR_TID2;
|
2014-10-16 15:21:16 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 10:09:05 +00:00
|
|
|
static inline unsigned long *vcpu_hcr(struct kvm_vcpu *vcpu)
|
2014-12-19 16:05:31 +00:00
|
|
|
{
|
2017-08-03 10:09:05 +00:00
|
|
|
return (unsigned long *)&vcpu->arch.hcr_el2;
|
2014-12-19 16:05:31 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 16:04:12 +00:00
|
|
|
static inline void vcpu_clear_wfx_traps(struct kvm_vcpu *vcpu)
|
2018-06-21 09:43:59 +00:00
|
|
|
{
|
|
|
|
vcpu->arch.hcr_el2 &= ~HCR_TWE;
|
2020-03-04 20:33:29 +00:00
|
|
|
if (atomic_read(&vcpu->arch.vgic_cpu.vgic_v3.its_vpe.vlpi_count) ||
|
|
|
|
vcpu->kvm->arch.vgic.nassgireq)
|
2019-11-07 16:04:12 +00:00
|
|
|
vcpu->arch.hcr_el2 &= ~HCR_TWI;
|
|
|
|
else
|
|
|
|
vcpu->arch.hcr_el2 |= HCR_TWI;
|
2018-06-21 09:43:59 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 16:04:12 +00:00
|
|
|
static inline void vcpu_set_wfx_traps(struct kvm_vcpu *vcpu)
|
2018-06-21 09:43:59 +00:00
|
|
|
{
|
|
|
|
vcpu->arch.hcr_el2 |= HCR_TWE;
|
2019-11-07 16:04:12 +00:00
|
|
|
vcpu->arch.hcr_el2 |= HCR_TWI;
|
2018-06-21 09:43:59 +00:00
|
|
|
}
|
|
|
|
|
KVM: arm/arm64: Context-switch ptrauth registers
When pointer authentication is supported, a guest may wish to use it.
This patch adds the necessary KVM infrastructure for this to work, with
a semi-lazy context switch of the pointer auth state.
Pointer authentication feature is only enabled when VHE is built
in the kernel and present in the CPU implementation so only VHE code
paths are modified.
When we schedule a vcpu, we disable guest usage of pointer
authentication instructions and accesses to the keys. While these are
disabled, we avoid context-switching the keys. When we trap the guest
trying to use pointer authentication functionality, we change to eagerly
context-switching the keys, and enable the feature. The next time the
vcpu is scheduled out/in, we start again. However the host key save is
optimized and implemented inside ptrauth instruction/register access
trap.
Pointer authentication consists of address authentication and generic
authentication, and CPUs in a system might have varied support for
either. Where support for either feature is not uniform, it is hidden
from guests via ID register emulation, as a result of the cpufeature
framework in the host.
Unfortunately, address authentication and generic authentication cannot
be trapped separately, as the architecture provides a single EL2 trap
covering both. If we wish to expose one without the other, we cannot
prevent a (badly-written) guest from intermittently using a feature
which is not uniformly supported (when scheduled on a physical CPU which
supports the relevant feature). Hence, this patch expects both type of
authentication to be present in a cpu.
This switch of key is done from guest enter/exit assembly as preparation
for the upcoming in-kernel pointer authentication support. Hence, these
key switching routines are not implemented in C code as they may cause
pointer authentication key signing error in some situations.
Signed-off-by: Mark Rutland <mark.rutland@arm.com>
[Only VHE, key switch in full assembly, vcpu_has_ptrauth checks
, save host key in ptrauth exception trap]
Signed-off-by: Amit Daniel Kachhap <amit.kachhap@arm.com>
Reviewed-by: Julien Thierry <julien.thierry@arm.com>
Cc: Christoffer Dall <christoffer.dall@arm.com>
Cc: kvmarm@lists.cs.columbia.edu
[maz: various fixups]
Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
2019-04-23 04:42:35 +00:00
|
|
|
static inline void vcpu_ptrauth_enable(struct kvm_vcpu *vcpu)
|
|
|
|
{
|
|
|
|
vcpu->arch.hcr_el2 |= (HCR_API | HCR_APK);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void vcpu_ptrauth_disable(struct kvm_vcpu *vcpu)
|
|
|
|
{
|
|
|
|
vcpu->arch.hcr_el2 &= ~(HCR_API | HCR_APK);
|
|
|
|
}
|
|
|
|
|
2018-07-19 15:24:22 +00:00
|
|
|
static inline unsigned long vcpu_get_vsesr(struct kvm_vcpu *vcpu)
|
|
|
|
{
|
|
|
|
return vcpu->arch.vsesr_el2;
|
|
|
|
}
|
|
|
|
|
2018-01-15 19:39:01 +00:00
|
|
|
static inline void vcpu_set_vsesr(struct kvm_vcpu *vcpu, u64 vsesr)
|
|
|
|
{
|
|
|
|
vcpu->arch.vsesr_el2 = vsesr;
|
|
|
|
}
|
|
|
|
|
2020-02-20 16:58:37 +00:00
|
|
|
static __always_inline unsigned long *vcpu_pc(const struct kvm_vcpu *vcpu)
|
2012-12-10 13:27:52 +00:00
|
|
|
{
|
2019-06-28 21:40:58 +00:00
|
|
|
return (unsigned long *)&vcpu_gp_regs(vcpu)->pc;
|
2012-12-10 13:27:52 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 16:58:37 +00:00
|
|
|
static __always_inline unsigned long *vcpu_cpsr(const struct kvm_vcpu *vcpu)
|
2012-12-10 13:27:52 +00:00
|
|
|
{
|
2019-06-28 21:40:58 +00:00
|
|
|
return (unsigned long *)&vcpu_gp_regs(vcpu)->pstate;
|
2012-12-10 13:27:52 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 16:58:37 +00:00
|
|
|
static __always_inline bool vcpu_mode_is_32bit(const struct kvm_vcpu *vcpu)
|
2012-12-10 13:27:52 +00:00
|
|
|
{
|
2013-02-06 19:40:29 +00:00
|
|
|
return !!(*vcpu_cpsr(vcpu) & PSR_MODE32_BIT);
|
2012-12-10 13:27:52 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 16:58:37 +00:00
|
|
|
static __always_inline bool kvm_condition_valid(const struct kvm_vcpu *vcpu)
|
2012-12-10 13:27:52 +00:00
|
|
|
{
|
2013-02-06 19:54:04 +00:00
|
|
|
if (vcpu_mode_is_32bit(vcpu))
|
|
|
|
return kvm_condition_valid32(vcpu);
|
|
|
|
|
|
|
|
return true;
|
2012-12-10 13:27:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void vcpu_set_thumb(struct kvm_vcpu *vcpu)
|
|
|
|
{
|
2018-07-05 14:16:53 +00:00
|
|
|
*vcpu_cpsr(vcpu) |= PSR_AA32_T_BIT;
|
2012-12-10 13:27:52 +00:00
|
|
|
}
|
|
|
|
|
2015-11-16 10:28:17 +00:00
|
|
|
/*
|
2015-12-04 12:03:14 +00:00
|
|
|
* vcpu_get_reg and vcpu_set_reg should always be passed a register number
|
|
|
|
* coming from a read of ESR_EL2. Otherwise, it may give the wrong result on
|
|
|
|
* AArch32 with banked registers.
|
2015-11-16 10:28:17 +00:00
|
|
|
*/
|
2020-02-20 16:58:37 +00:00
|
|
|
static __always_inline unsigned long vcpu_get_reg(const struct kvm_vcpu *vcpu,
|
2015-12-04 12:03:11 +00:00
|
|
|
u8 reg_num)
|
|
|
|
{
|
2019-06-28 21:40:58 +00:00
|
|
|
return (reg_num == 31) ? 0 : vcpu_gp_regs(vcpu)->regs[reg_num];
|
2015-12-04 12:03:11 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 16:58:37 +00:00
|
|
|
static __always_inline void vcpu_set_reg(struct kvm_vcpu *vcpu, u8 reg_num,
|
2015-12-04 12:03:11 +00:00
|
|
|
unsigned long val)
|
|
|
|
{
|
|
|
|
if (reg_num != 31)
|
2019-06-28 21:40:58 +00:00
|
|
|
vcpu_gp_regs(vcpu)->regs[reg_num] = val;
|
2015-12-04 12:03:11 +00:00
|
|
|
}
|
|
|
|
|
2017-12-27 19:01:52 +00:00
|
|
|
static inline unsigned long vcpu_read_spsr(const struct kvm_vcpu *vcpu)
|
2012-12-10 13:27:52 +00:00
|
|
|
{
|
2017-12-27 20:59:09 +00:00
|
|
|
if (vcpu_mode_is_32bit(vcpu))
|
|
|
|
return vcpu_read_spsr32(vcpu);
|
2017-12-27 19:01:52 +00:00
|
|
|
|
|
|
|
if (vcpu->arch.sysregs_loaded_on_cpu)
|
KVM: arm64: Migrate _elx sysreg accessors to msr_s/mrs_s
Currently, the {read,write}_sysreg_el*() accessors for accessing
particular ELs' sysregs in the presence of VHE rely on some local
hacks and define their system register encodings in a way that is
inconsistent with the core definitions in <asm/sysreg.h>.
As a result, it is necessary to add duplicate definitions for any
system register that already needs a definition in sysreg.h for
other reasons.
This is a bit of a maintenance headache, and the reasons for the
_el*() accessors working the way they do is a bit historical.
This patch gets rid of the shadow sysreg definitions in
<asm/kvm_hyp.h>, converts the _el*() accessors to use the core
__msr_s/__mrs_s interface, and converts all call sites to use the
standard sysreg #define names (i.e., upper case, with SYS_ prefix).
This patch will conflict heavily anyway, so the opportunity
to clean up some bad whitespace in the context of the changes is
taken.
The change exposes a few system registers that have no sysreg.h
definition, due to msr_s/mrs_s being used in place of msr/mrs:
additions are made in order to fill in the gaps.
Signed-off-by: Dave Martin <Dave.Martin@arm.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Christoffer Dall <christoffer.dall@arm.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Will Deacon <will.deacon@arm.com>
Link: https://www.spinics.net/lists/kvm-arm/msg31717.html
[Rebased to v4.21-rc1]
Signed-off-by: Sudeep Holla <sudeep.holla@arm.com>
[Rebased to v5.2-rc5, changelog updates]
Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
2019-04-06 10:29:40 +00:00
|
|
|
return read_sysreg_el1(SYS_SPSR);
|
2017-12-27 19:01:52 +00:00
|
|
|
else
|
2019-06-28 22:05:38 +00:00
|
|
|
return __vcpu_sys_reg(vcpu, SPSR_EL1);
|
2017-12-27 19:01:52 +00:00
|
|
|
}
|
2013-02-06 19:40:29 +00:00
|
|
|
|
2017-12-27 20:59:09 +00:00
|
|
|
static inline void vcpu_write_spsr(struct kvm_vcpu *vcpu, unsigned long v)
|
2017-12-27 19:01:52 +00:00
|
|
|
{
|
|
|
|
if (vcpu_mode_is_32bit(vcpu)) {
|
2017-12-27 20:59:09 +00:00
|
|
|
vcpu_write_spsr32(vcpu, v);
|
|
|
|
return;
|
2017-12-27 19:01:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (vcpu->arch.sysregs_loaded_on_cpu)
|
KVM: arm64: Migrate _elx sysreg accessors to msr_s/mrs_s
Currently, the {read,write}_sysreg_el*() accessors for accessing
particular ELs' sysregs in the presence of VHE rely on some local
hacks and define their system register encodings in a way that is
inconsistent with the core definitions in <asm/sysreg.h>.
As a result, it is necessary to add duplicate definitions for any
system register that already needs a definition in sysreg.h for
other reasons.
This is a bit of a maintenance headache, and the reasons for the
_el*() accessors working the way they do is a bit historical.
This patch gets rid of the shadow sysreg definitions in
<asm/kvm_hyp.h>, converts the _el*() accessors to use the core
__msr_s/__mrs_s interface, and converts all call sites to use the
standard sysreg #define names (i.e., upper case, with SYS_ prefix).
This patch will conflict heavily anyway, so the opportunity
to clean up some bad whitespace in the context of the changes is
taken.
The change exposes a few system registers that have no sysreg.h
definition, due to msr_s/mrs_s being used in place of msr/mrs:
additions are made in order to fill in the gaps.
Signed-off-by: Dave Martin <Dave.Martin@arm.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Christoffer Dall <christoffer.dall@arm.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Will Deacon <will.deacon@arm.com>
Link: https://www.spinics.net/lists/kvm-arm/msg31717.html
[Rebased to v4.21-rc1]
Signed-off-by: Sudeep Holla <sudeep.holla@arm.com>
[Rebased to v5.2-rc5, changelog updates]
Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
2019-04-06 10:29:40 +00:00
|
|
|
write_sysreg_el1(v, SYS_SPSR);
|
2017-12-27 19:01:52 +00:00
|
|
|
else
|
2019-06-28 22:05:38 +00:00
|
|
|
__vcpu_sys_reg(vcpu, SPSR_EL1) = v;
|
2012-12-10 13:27:52 +00:00
|
|
|
}
|
|
|
|
|
KVM: arm/arm64: Correct AArch32 SPSR on exception entry
Confusingly, there are three SPSR layouts that a kernel may need to deal
with:
(1) An AArch64 SPSR_ELx view of an AArch64 pstate
(2) An AArch64 SPSR_ELx view of an AArch32 pstate
(3) An AArch32 SPSR_* view of an AArch32 pstate
When the KVM AArch32 support code deals with SPSR_{EL2,HYP}, it's either
dealing with #2 or #3 consistently. On arm64 the PSR_AA32_* definitions
match the AArch64 SPSR_ELx view, and on arm the PSR_AA32_* definitions
match the AArch32 SPSR_* view.
However, when we inject an exception into an AArch32 guest, we have to
synthesize the AArch32 SPSR_* that the guest will see. Thus, an AArch64
host needs to synthesize layout #3 from layout #2.
This patch adds a new host_spsr_to_spsr32() helper for this, and makes
use of it in the KVM AArch32 support code. For arm64 we need to shuffle
the DIT bit around, and remove the SS bit, while for arm we can use the
value as-is.
I've open-coded the bit manipulation for now to avoid having to rework
the existing PSR_* definitions into PSR64_AA32_* and PSR32_AA32_*
definitions. I hope to perform a more thorough refactoring in future so
that we can handle pstate view manipulation more consistently across the
kernel tree.
Signed-off-by: Mark Rutland <mark.rutland@arm.com>
Signed-off-by: Marc Zyngier <maz@kernel.org>
Reviewed-by: Alexandru Elisei <alexandru.elisei@arm.com>
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/r/20200108134324.46500-4-mark.rutland@arm.com
2020-01-08 13:43:24 +00:00
|
|
|
/*
|
|
|
|
* The layout of SPSR for an AArch32 state is different when observed from an
|
|
|
|
* AArch64 SPSR_ELx or an AArch32 SPSR_*. This function generates the AArch32
|
|
|
|
* view given an AArch64 view.
|
|
|
|
*
|
|
|
|
* In ARM DDI 0487E.a see:
|
|
|
|
*
|
|
|
|
* - The AArch64 view (SPSR_EL2) in section C5.2.18, page C5-426
|
|
|
|
* - The AArch32 view (SPSR_abt) in section G8.2.126, page G8-6256
|
|
|
|
* - The AArch32 view (SPSR_und) in section G8.2.132, page G8-6280
|
|
|
|
*
|
|
|
|
* Which show the following differences:
|
|
|
|
*
|
|
|
|
* | Bit | AA64 | AA32 | Notes |
|
|
|
|
* +-----+------+------+-----------------------------|
|
|
|
|
* | 24 | DIT | J | J is RES0 in ARMv8 |
|
|
|
|
* | 21 | SS | DIT | SS doesn't exist in AArch32 |
|
|
|
|
*
|
|
|
|
* ... and all other bits are (currently) common.
|
|
|
|
*/
|
|
|
|
static inline unsigned long host_spsr_to_spsr32(unsigned long spsr)
|
|
|
|
{
|
|
|
|
const unsigned long overlap = BIT(24) | BIT(21);
|
|
|
|
unsigned long dit = !!(spsr & PSR_AA32_DIT_BIT);
|
|
|
|
|
|
|
|
spsr &= ~overlap;
|
|
|
|
|
|
|
|
spsr |= dit << 21;
|
|
|
|
|
|
|
|
return spsr;
|
|
|
|
}
|
|
|
|
|
2012-12-10 13:27:52 +00:00
|
|
|
static inline bool vcpu_mode_priv(const struct kvm_vcpu *vcpu)
|
|
|
|
{
|
2016-01-13 09:16:39 +00:00
|
|
|
u32 mode;
|
2012-12-10 13:27:52 +00:00
|
|
|
|
2016-01-13 09:16:39 +00:00
|
|
|
if (vcpu_mode_is_32bit(vcpu)) {
|
2018-07-05 14:16:53 +00:00
|
|
|
mode = *vcpu_cpsr(vcpu) & PSR_AA32_MODE_MASK;
|
|
|
|
return mode > PSR_AA32_MODE_USR;
|
2016-01-13 09:16:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mode = *vcpu_cpsr(vcpu) & PSR_MODE_MASK;
|
2013-02-06 19:40:29 +00:00
|
|
|
|
2012-12-10 13:27:52 +00:00
|
|
|
return mode != PSR_MODE_EL0t;
|
|
|
|
}
|
|
|
|
|
2020-06-30 01:57:05 +00:00
|
|
|
static __always_inline u32 kvm_vcpu_get_esr(const struct kvm_vcpu *vcpu)
|
2012-12-10 13:27:52 +00:00
|
|
|
{
|
|
|
|
return vcpu->arch.fault.esr_el2;
|
|
|
|
}
|
|
|
|
|
2020-02-20 16:58:37 +00:00
|
|
|
static __always_inline int kvm_vcpu_get_condition(const struct kvm_vcpu *vcpu)
|
2016-09-06 08:28:41 +00:00
|
|
|
{
|
2020-06-30 01:57:05 +00:00
|
|
|
u32 esr = kvm_vcpu_get_esr(vcpu);
|
2016-09-06 08:28:41 +00:00
|
|
|
|
|
|
|
if (esr & ESR_ELx_CV)
|
|
|
|
return (esr & ESR_ELx_COND_MASK) >> ESR_ELx_COND_SHIFT;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-02-20 16:58:37 +00:00
|
|
|
static __always_inline unsigned long kvm_vcpu_get_hfar(const struct kvm_vcpu *vcpu)
|
2012-12-10 13:27:52 +00:00
|
|
|
{
|
|
|
|
return vcpu->arch.fault.far_el2;
|
|
|
|
}
|
|
|
|
|
2020-02-20 16:58:37 +00:00
|
|
|
static __always_inline phys_addr_t kvm_vcpu_get_fault_ipa(const struct kvm_vcpu *vcpu)
|
2012-12-10 13:27:52 +00:00
|
|
|
{
|
|
|
|
return ((phys_addr_t)vcpu->arch.fault.hpfar_el2 & HPFAR_MASK) << 8;
|
|
|
|
}
|
|
|
|
|
KVM: arm64: Handle RAS SErrors from EL2 on guest exit
We expect to have firmware-first handling of RAS SErrors, with errors
notified via an APEI method. For systems without firmware-first, add
some minimal handling to KVM.
There are two ways KVM can take an SError due to a guest, either may be a
RAS error: we exit the guest due to an SError routed to EL2 by HCR_EL2.AMO,
or we take an SError from EL2 when we unmask PSTATE.A from __guest_exit.
The current SError from EL2 code unmasks SError and tries to fence any
pending SError into a single instruction window. It then leaves SError
unmasked.
With the v8.2 RAS Extensions we may take an SError for a 'corrected'
error, but KVM is only able to handle SError from EL2 if they occur
during this single instruction window...
The RAS Extensions give us a new instruction to synchronise and
consume SErrors. The RAS Extensions document (ARM DDI0587),
'2.4.1 ESB and Unrecoverable errors' describes ESB as synchronising
SError interrupts generated by 'instructions, translation table walks,
hardware updates to the translation tables, and instruction fetches on
the same PE'. This makes ESB equivalent to KVMs existing
'dsb, mrs-daifclr, isb' sequence.
Use the alternatives to synchronise and consume any SError using ESB
instead of unmasking and taking the SError. Set ARM_EXIT_WITH_SERROR_BIT
in the exit_code so that we can restart the vcpu if it turns out this
SError has no impact on the vcpu.
Reviewed-by: Marc Zyngier <marc.zyngier@arm.com>
Signed-off-by: James Morse <james.morse@arm.com>
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
2018-01-15 19:39:05 +00:00
|
|
|
static inline u64 kvm_vcpu_get_disr(const struct kvm_vcpu *vcpu)
|
|
|
|
{
|
|
|
|
return vcpu->arch.fault.disr_el1;
|
|
|
|
}
|
|
|
|
|
2015-01-12 16:53:36 +00:00
|
|
|
static inline u32 kvm_vcpu_hvc_get_imm(const struct kvm_vcpu *vcpu)
|
|
|
|
{
|
2020-06-30 01:57:05 +00:00
|
|
|
return kvm_vcpu_get_esr(vcpu) & ESR_ELx_xVC_IMM_MASK;
|
2015-01-12 16:53:36 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 16:58:37 +00:00
|
|
|
static __always_inline bool kvm_vcpu_dabt_isvalid(const struct kvm_vcpu *vcpu)
|
2012-12-10 13:27:52 +00:00
|
|
|
{
|
2020-06-30 01:57:05 +00:00
|
|
|
return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_ISV);
|
2012-12-10 13:27:52 +00:00
|
|
|
}
|
|
|
|
|
KVM: arm/arm64: Allow reporting non-ISV data aborts to userspace
For a long time, if a guest accessed memory outside of a memslot using
any of the load/store instructions in the architecture which doesn't
supply decoding information in the ESR_EL2 (the ISV bit is not set), the
kernel would print the following message and terminate the VM as a
result of returning -ENOSYS to userspace:
load/store instruction decoding not implemented
The reason behind this message is that KVM assumes that all accesses
outside a memslot is an MMIO access which should be handled by
userspace, and we originally expected to eventually implement some sort
of decoding of load/store instructions where the ISV bit was not set.
However, it turns out that many of the instructions which don't provide
decoding information on abort are not safe to use for MMIO accesses, and
the remaining few that would potentially make sense to use on MMIO
accesses, such as those with register writeback, are not used in
practice. It also turns out that fetching an instruction from guest
memory can be a pretty horrible affair, involving stopping all CPUs on
SMP systems, handling multiple corner cases of address translation in
software, and more. It doesn't appear likely that we'll ever implement
this in the kernel.
What is much more common is that a user has misconfigured his/her guest
and is actually not accessing an MMIO region, but just hitting some
random hole in the IPA space. In this scenario, the error message above
is almost misleading and has led to a great deal of confusion over the
years.
It is, nevertheless, ABI to userspace, and we therefore need to
introduce a new capability that userspace explicitly enables to change
behavior.
This patch introduces KVM_CAP_ARM_NISV_TO_USER (NISV meaning Non-ISV)
which does exactly that, and introduces a new exit reason to report the
event to userspace. User space can then emulate an exception to the
guest, restart the guest, suspend the guest, or take any other
appropriate action as per the policy of the running system.
Reported-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Christoffer Dall <christoffer.dall@arm.com>
Reviewed-by: Alexander Graf <graf@amazon.com>
Signed-off-by: Marc Zyngier <maz@kernel.org>
2019-10-11 11:07:05 +00:00
|
|
|
static inline unsigned long kvm_vcpu_dabt_iss_nisv_sanitized(const struct kvm_vcpu *vcpu)
|
|
|
|
{
|
2020-06-30 01:57:05 +00:00
|
|
|
return kvm_vcpu_get_esr(vcpu) & (ESR_ELx_CM | ESR_ELx_WNR | ESR_ELx_FSC);
|
KVM: arm/arm64: Allow reporting non-ISV data aborts to userspace
For a long time, if a guest accessed memory outside of a memslot using
any of the load/store instructions in the architecture which doesn't
supply decoding information in the ESR_EL2 (the ISV bit is not set), the
kernel would print the following message and terminate the VM as a
result of returning -ENOSYS to userspace:
load/store instruction decoding not implemented
The reason behind this message is that KVM assumes that all accesses
outside a memslot is an MMIO access which should be handled by
userspace, and we originally expected to eventually implement some sort
of decoding of load/store instructions where the ISV bit was not set.
However, it turns out that many of the instructions which don't provide
decoding information on abort are not safe to use for MMIO accesses, and
the remaining few that would potentially make sense to use on MMIO
accesses, such as those with register writeback, are not used in
practice. It also turns out that fetching an instruction from guest
memory can be a pretty horrible affair, involving stopping all CPUs on
SMP systems, handling multiple corner cases of address translation in
software, and more. It doesn't appear likely that we'll ever implement
this in the kernel.
What is much more common is that a user has misconfigured his/her guest
and is actually not accessing an MMIO region, but just hitting some
random hole in the IPA space. In this scenario, the error message above
is almost misleading and has led to a great deal of confusion over the
years.
It is, nevertheless, ABI to userspace, and we therefore need to
introduce a new capability that userspace explicitly enables to change
behavior.
This patch introduces KVM_CAP_ARM_NISV_TO_USER (NISV meaning Non-ISV)
which does exactly that, and introduces a new exit reason to report the
event to userspace. User space can then emulate an exception to the
guest, restart the guest, suspend the guest, or take any other
appropriate action as per the policy of the running system.
Reported-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Christoffer Dall <christoffer.dall@arm.com>
Reviewed-by: Alexander Graf <graf@amazon.com>
Signed-off-by: Marc Zyngier <maz@kernel.org>
2019-10-11 11:07:05 +00:00
|
|
|
}
|
|
|
|
|
2012-12-10 13:27:52 +00:00
|
|
|
static inline bool kvm_vcpu_dabt_issext(const struct kvm_vcpu *vcpu)
|
|
|
|
{
|
2020-06-30 01:57:05 +00:00
|
|
|
return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_SSE);
|
2012-12-10 13:27:52 +00:00
|
|
|
}
|
|
|
|
|
2019-12-12 19:50:55 +00:00
|
|
|
static inline bool kvm_vcpu_dabt_issf(const struct kvm_vcpu *vcpu)
|
|
|
|
{
|
2020-06-30 01:57:05 +00:00
|
|
|
return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_SF);
|
2019-12-12 19:50:55 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 16:58:37 +00:00
|
|
|
static __always_inline int kvm_vcpu_dabt_get_rd(const struct kvm_vcpu *vcpu)
|
2012-12-10 13:27:52 +00:00
|
|
|
{
|
2020-06-30 01:57:05 +00:00
|
|
|
return (kvm_vcpu_get_esr(vcpu) & ESR_ELx_SRT_MASK) >> ESR_ELx_SRT_SHIFT;
|
2012-12-10 13:27:52 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 16:58:37 +00:00
|
|
|
static __always_inline bool kvm_vcpu_dabt_iss1tw(const struct kvm_vcpu *vcpu)
|
2012-12-10 13:27:52 +00:00
|
|
|
{
|
2020-06-30 01:57:05 +00:00
|
|
|
return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_S1PTW);
|
2012-12-10 13:27:52 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 16:58:37 +00:00
|
|
|
static __always_inline bool kvm_vcpu_dabt_iswrite(const struct kvm_vcpu *vcpu)
|
arm64: KVM: Take S1 walks into account when determining S2 write faults
The WnR bit in the HSR/ESR_EL2 indicates whether a data abort was
generated by a read or a write instruction. For stage 2 data aborts
generated by a stage 1 translation table walk (i.e. the actual page
table access faults at EL2), the WnR bit therefore reports whether the
instruction generating the walk was a load or a store, *not* whether the
page table walker was reading or writing the entry.
For page tables marked as read-only at stage 2 (e.g. due to KSM merging
them with the tables from another guest), this could result in livelock,
where a page table walk generated by a load instruction attempts to
set the access flag in the stage 1 descriptor, but fails to trigger
CoW in the host since only a read fault is reported.
This patch modifies the arm64 kvm_vcpu_dabt_iswrite function to
take into account stage 2 faults in stage 1 walks. Since DBM cannot be
disabled at EL2 for CPUs that implement it, we assume that these faults
are always causes by writes, avoiding the livelock situation at the
expense of occasional, spurious CoWs.
We could, in theory, do a bit better by checking the guest TCR
configuration and inspecting the page table to see why the PTE faulted.
However, I doubt this is measurable in practice, and the threat of
livelock is real.
Cc: <stable@vger.kernel.org>
Cc: Julien Grall <julien.grall@arm.com>
Reviewed-by: Marc Zyngier <marc.zyngier@arm.com>
Reviewed-by: Christoffer Dall <christoffer.dall@linaro.org>
Signed-off-by: Will Deacon <will.deacon@arm.com>
2016-09-29 11:37:01 +00:00
|
|
|
{
|
2020-06-30 01:57:05 +00:00
|
|
|
return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_WNR) ||
|
arm64: KVM: Take S1 walks into account when determining S2 write faults
The WnR bit in the HSR/ESR_EL2 indicates whether a data abort was
generated by a read or a write instruction. For stage 2 data aborts
generated by a stage 1 translation table walk (i.e. the actual page
table access faults at EL2), the WnR bit therefore reports whether the
instruction generating the walk was a load or a store, *not* whether the
page table walker was reading or writing the entry.
For page tables marked as read-only at stage 2 (e.g. due to KSM merging
them with the tables from another guest), this could result in livelock,
where a page table walk generated by a load instruction attempts to
set the access flag in the stage 1 descriptor, but fails to trigger
CoW in the host since only a read fault is reported.
This patch modifies the arm64 kvm_vcpu_dabt_iswrite function to
take into account stage 2 faults in stage 1 walks. Since DBM cannot be
disabled at EL2 for CPUs that implement it, we assume that these faults
are always causes by writes, avoiding the livelock situation at the
expense of occasional, spurious CoWs.
We could, in theory, do a bit better by checking the guest TCR
configuration and inspecting the page table to see why the PTE faulted.
However, I doubt this is measurable in practice, and the threat of
livelock is real.
Cc: <stable@vger.kernel.org>
Cc: Julien Grall <julien.grall@arm.com>
Reviewed-by: Marc Zyngier <marc.zyngier@arm.com>
Reviewed-by: Christoffer Dall <christoffer.dall@linaro.org>
Signed-off-by: Will Deacon <will.deacon@arm.com>
2016-09-29 11:37:01 +00:00
|
|
|
kvm_vcpu_dabt_iss1tw(vcpu); /* AF/DBM update */
|
|
|
|
}
|
|
|
|
|
2016-01-29 15:01:28 +00:00
|
|
|
static inline bool kvm_vcpu_dabt_is_cm(const struct kvm_vcpu *vcpu)
|
|
|
|
{
|
2020-06-30 01:57:05 +00:00
|
|
|
return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_CM);
|
2016-01-29 15:01:28 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 16:58:37 +00:00
|
|
|
static __always_inline unsigned int kvm_vcpu_dabt_get_as(const struct kvm_vcpu *vcpu)
|
2012-12-10 13:27:52 +00:00
|
|
|
{
|
2020-06-30 01:57:05 +00:00
|
|
|
return 1 << ((kvm_vcpu_get_esr(vcpu) & ESR_ELx_SAS) >> ESR_ELx_SAS_SHIFT);
|
2012-12-10 13:27:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This one is not specific to Data Abort */
|
2020-02-20 16:58:37 +00:00
|
|
|
static __always_inline bool kvm_vcpu_trap_il_is32bit(const struct kvm_vcpu *vcpu)
|
2012-12-10 13:27:52 +00:00
|
|
|
{
|
2020-06-30 01:57:05 +00:00
|
|
|
return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_IL);
|
2012-12-10 13:27:52 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 16:58:37 +00:00
|
|
|
static __always_inline u8 kvm_vcpu_trap_get_class(const struct kvm_vcpu *vcpu)
|
2012-12-10 13:27:52 +00:00
|
|
|
{
|
2020-06-30 01:57:05 +00:00
|
|
|
return ESR_ELx_EC(kvm_vcpu_get_esr(vcpu));
|
2012-12-10 13:27:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool kvm_vcpu_trap_is_iabt(const struct kvm_vcpu *vcpu)
|
|
|
|
{
|
2014-11-24 13:59:30 +00:00
|
|
|
return kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_IABT_LOW;
|
2012-12-10 13:27:52 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 16:58:37 +00:00
|
|
|
static __always_inline u8 kvm_vcpu_trap_get_fault(const struct kvm_vcpu *vcpu)
|
2014-09-26 10:29:34 +00:00
|
|
|
{
|
2020-06-30 01:57:05 +00:00
|
|
|
return kvm_vcpu_get_esr(vcpu) & ESR_ELx_FSC;
|
2014-09-26 10:29:34 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 16:58:37 +00:00
|
|
|
static __always_inline u8 kvm_vcpu_trap_get_fault_type(const struct kvm_vcpu *vcpu)
|
2012-12-10 13:27:52 +00:00
|
|
|
{
|
2020-06-30 01:57:05 +00:00
|
|
|
return kvm_vcpu_get_esr(vcpu) & ESR_ELx_FSC_TYPE;
|
2012-12-10 13:27:52 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 10:28:18 +00:00
|
|
|
static __always_inline bool kvm_vcpu_abt_issea(const struct kvm_vcpu *vcpu)
|
2017-07-18 12:37:41 +00:00
|
|
|
{
|
2017-10-30 06:05:18 +00:00
|
|
|
switch (kvm_vcpu_trap_get_fault(vcpu)) {
|
2017-07-18 12:37:41 +00:00
|
|
|
case FSC_SEA:
|
|
|
|
case FSC_SEA_TTW0:
|
|
|
|
case FSC_SEA_TTW1:
|
|
|
|
case FSC_SEA_TTW2:
|
|
|
|
case FSC_SEA_TTW3:
|
|
|
|
case FSC_SECC:
|
|
|
|
case FSC_SECC_TTW0:
|
|
|
|
case FSC_SECC_TTW1:
|
|
|
|
case FSC_SECC_TTW2:
|
|
|
|
case FSC_SECC_TTW3:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 16:58:37 +00:00
|
|
|
static __always_inline int kvm_vcpu_sys_get_rt(struct kvm_vcpu *vcpu)
|
2017-04-27 18:06:48 +00:00
|
|
|
{
|
2020-06-30 01:57:05 +00:00
|
|
|
u32 esr = kvm_vcpu_get_esr(vcpu);
|
2018-09-20 04:06:19 +00:00
|
|
|
return ESR_ELx_SYS64_ISS_RT(esr);
|
2017-04-27 18:06:48 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 20:29:58 +00:00
|
|
|
static inline bool kvm_is_write_fault(struct kvm_vcpu *vcpu)
|
|
|
|
{
|
|
|
|
if (kvm_vcpu_trap_is_iabt(vcpu))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return kvm_vcpu_dabt_iswrite(vcpu);
|
|
|
|
}
|
|
|
|
|
2014-06-02 13:37:13 +00:00
|
|
|
static inline unsigned long kvm_vcpu_get_mpidr_aff(struct kvm_vcpu *vcpu)
|
2013-10-18 17:19:03 +00:00
|
|
|
{
|
2016-03-16 14:38:53 +00:00
|
|
|
return vcpu_read_sys_reg(vcpu, MPIDR_EL1) & MPIDR_HWID_BITMASK;
|
2013-10-18 17:19:03 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 14:27:49 +00:00
|
|
|
static inline bool kvm_arm_get_vcpu_workaround_2_flag(struct kvm_vcpu *vcpu)
|
|
|
|
{
|
|
|
|
return vcpu->arch.workaround_flags & VCPU_WORKAROUND_2_FLAG;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void kvm_arm_set_vcpu_workaround_2_flag(struct kvm_vcpu *vcpu,
|
|
|
|
bool flag)
|
|
|
|
{
|
|
|
|
if (flag)
|
|
|
|
vcpu->arch.workaround_flags |= VCPU_WORKAROUND_2_FLAG;
|
|
|
|
else
|
|
|
|
vcpu->arch.workaround_flags &= ~VCPU_WORKAROUND_2_FLAG;
|
|
|
|
}
|
|
|
|
|
2013-11-05 14:12:15 +00:00
|
|
|
static inline void kvm_vcpu_set_be(struct kvm_vcpu *vcpu)
|
|
|
|
{
|
2016-03-16 14:38:53 +00:00
|
|
|
if (vcpu_mode_is_32bit(vcpu)) {
|
2018-07-05 14:16:53 +00:00
|
|
|
*vcpu_cpsr(vcpu) |= PSR_AA32_E_BIT;
|
2016-03-16 14:38:53 +00:00
|
|
|
} else {
|
|
|
|
u64 sctlr = vcpu_read_sys_reg(vcpu, SCTLR_EL1);
|
|
|
|
sctlr |= (1 << 25);
|
KVM: arm64: Fix order of vcpu_write_sys_reg() arguments
A typo in kvm_vcpu_set_be()'s call:
| vcpu_write_sys_reg(vcpu, SCTLR_EL1, sctlr)
causes us to use the 32bit register value as an index into the sys_reg[]
array, and sail off the end of the linear map when we try to bring up
big-endian secondaries.
| Unable to handle kernel paging request at virtual address ffff80098b982c00
| Mem abort info:
| ESR = 0x96000045
| Exception class = DABT (current EL), IL = 32 bits
| SET = 0, FnV = 0
| EA = 0, S1PTW = 0
| Data abort info:
| ISV = 0, ISS = 0x00000045
| CM = 0, WnR = 1
| swapper pgtable: 4k pages, 48-bit VAs, pgdp = 000000002ea0571a
| [ffff80098b982c00] pgd=00000009ffff8803, pud=0000000000000000
| Internal error: Oops: 96000045 [#1] PREEMPT SMP
| Modules linked in:
| CPU: 2 PID: 1561 Comm: kvm-vcpu-0 Not tainted 4.17.0-rc3-00001-ga912e2261ca6-dirty #1323
| Hardware name: ARM Juno development board (r1) (DT)
| pstate: 60000005 (nZCv daif -PAN -UAO)
| pc : vcpu_write_sys_reg+0x50/0x134
| lr : vcpu_write_sys_reg+0x50/0x134
| Process kvm-vcpu-0 (pid: 1561, stack limit = 0x000000006df4728b)
| Call trace:
| vcpu_write_sys_reg+0x50/0x134
| kvm_psci_vcpu_on+0x14c/0x150
| kvm_psci_0_2_call+0x244/0x2a4
| kvm_hvc_call_handler+0x1cc/0x258
| handle_hvc+0x20/0x3c
| handle_exit+0x130/0x1ec
| kvm_arch_vcpu_ioctl_run+0x340/0x614
| kvm_vcpu_ioctl+0x4d0/0x840
| do_vfs_ioctl+0xc8/0x8d0
| ksys_ioctl+0x78/0xa8
| sys_ioctl+0xc/0x18
| el0_svc_naked+0x30/0x34
| Code: 73620291 604d00b0 00201891 1ab10194 (957a33f8)
|---[ end trace 4b4a4f9628596602 ]---
Fix the order of the arguments.
Fixes: 8d404c4c24613 ("KVM: arm64: Rewrite system register accessors to read/write functions")
CC: Christoffer Dall <cdall@cs.columbia.edu>
Signed-off-by: James Morse <james.morse@arm.com>
Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
2018-05-02 11:17:02 +00:00
|
|
|
vcpu_write_sys_reg(vcpu, sctlr, SCTLR_EL1);
|
2016-03-16 14:38:53 +00:00
|
|
|
}
|
2013-11-05 14:12:15 +00:00
|
|
|
}
|
|
|
|
|
2013-02-12 12:40:22 +00:00
|
|
|
static inline bool kvm_vcpu_is_be(struct kvm_vcpu *vcpu)
|
|
|
|
{
|
|
|
|
if (vcpu_mode_is_32bit(vcpu))
|
2018-07-05 14:16:53 +00:00
|
|
|
return !!(*vcpu_cpsr(vcpu) & PSR_AA32_E_BIT);
|
2013-02-12 12:40:22 +00:00
|
|
|
|
2016-03-16 14:38:53 +00:00
|
|
|
return !!(vcpu_read_sys_reg(vcpu, SCTLR_EL1) & (1 << 25));
|
2013-02-12 12:40:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned long vcpu_data_guest_to_host(struct kvm_vcpu *vcpu,
|
|
|
|
unsigned long data,
|
|
|
|
unsigned int len)
|
|
|
|
{
|
|
|
|
if (kvm_vcpu_is_be(vcpu)) {
|
|
|
|
switch (len) {
|
|
|
|
case 1:
|
|
|
|
return data & 0xff;
|
|
|
|
case 2:
|
|
|
|
return be16_to_cpu(data & 0xffff);
|
|
|
|
case 4:
|
|
|
|
return be32_to_cpu(data & 0xffffffff);
|
|
|
|
default:
|
|
|
|
return be64_to_cpu(data);
|
|
|
|
}
|
2014-06-12 16:30:08 +00:00
|
|
|
} else {
|
|
|
|
switch (len) {
|
|
|
|
case 1:
|
|
|
|
return data & 0xff;
|
|
|
|
case 2:
|
|
|
|
return le16_to_cpu(data & 0xffff);
|
|
|
|
case 4:
|
|
|
|
return le32_to_cpu(data & 0xffffffff);
|
|
|
|
default:
|
|
|
|
return le64_to_cpu(data);
|
|
|
|
}
|
2013-02-12 12:40:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return data; /* Leave LE untouched */
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned long vcpu_data_host_to_guest(struct kvm_vcpu *vcpu,
|
|
|
|
unsigned long data,
|
|
|
|
unsigned int len)
|
|
|
|
{
|
|
|
|
if (kvm_vcpu_is_be(vcpu)) {
|
|
|
|
switch (len) {
|
|
|
|
case 1:
|
|
|
|
return data & 0xff;
|
|
|
|
case 2:
|
|
|
|
return cpu_to_be16(data & 0xffff);
|
|
|
|
case 4:
|
|
|
|
return cpu_to_be32(data & 0xffffffff);
|
|
|
|
default:
|
|
|
|
return cpu_to_be64(data);
|
|
|
|
}
|
2014-06-12 16:30:08 +00:00
|
|
|
} else {
|
|
|
|
switch (len) {
|
|
|
|
case 1:
|
|
|
|
return data & 0xff;
|
|
|
|
case 2:
|
|
|
|
return cpu_to_le16(data & 0xffff);
|
|
|
|
case 4:
|
|
|
|
return cpu_to_le32(data & 0xffffffff);
|
|
|
|
default:
|
|
|
|
return cpu_to_le64(data);
|
|
|
|
}
|
2013-02-12 12:40:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return data; /* Leave LE untouched */
|
|
|
|
}
|
|
|
|
|
2020-02-20 16:58:37 +00:00
|
|
|
static __always_inline void kvm_skip_instr(struct kvm_vcpu *vcpu, bool is_wide_instr)
|
2018-11-09 15:07:11 +00:00
|
|
|
{
|
2020-03-16 16:50:52 +00:00
|
|
|
if (vcpu_mode_is_32bit(vcpu)) {
|
2018-11-09 15:07:11 +00:00
|
|
|
kvm_skip_instr32(vcpu, is_wide_instr);
|
2020-03-16 16:50:52 +00:00
|
|
|
} else {
|
2018-11-09 15:07:11 +00:00
|
|
|
*vcpu_pc(vcpu) += 4;
|
2020-03-16 16:50:52 +00:00
|
|
|
*vcpu_cpsr(vcpu) &= ~PSR_BTYPE_MASK;
|
|
|
|
}
|
2018-11-09 15:07:11 +00:00
|
|
|
|
|
|
|
/* advance the singlestep state machine */
|
|
|
|
*vcpu_cpsr(vcpu) &= ~DBG_SPSR_SS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Skip an instruction which has been emulated at hyp while most guest sysregs
|
|
|
|
* are live.
|
|
|
|
*/
|
2020-06-25 13:14:19 +00:00
|
|
|
static __always_inline void __kvm_skip_instr(struct kvm_vcpu *vcpu)
|
2018-11-09 15:07:11 +00:00
|
|
|
{
|
KVM: arm64: Migrate _elx sysreg accessors to msr_s/mrs_s
Currently, the {read,write}_sysreg_el*() accessors for accessing
particular ELs' sysregs in the presence of VHE rely on some local
hacks and define their system register encodings in a way that is
inconsistent with the core definitions in <asm/sysreg.h>.
As a result, it is necessary to add duplicate definitions for any
system register that already needs a definition in sysreg.h for
other reasons.
This is a bit of a maintenance headache, and the reasons for the
_el*() accessors working the way they do is a bit historical.
This patch gets rid of the shadow sysreg definitions in
<asm/kvm_hyp.h>, converts the _el*() accessors to use the core
__msr_s/__mrs_s interface, and converts all call sites to use the
standard sysreg #define names (i.e., upper case, with SYS_ prefix).
This patch will conflict heavily anyway, so the opportunity
to clean up some bad whitespace in the context of the changes is
taken.
The change exposes a few system registers that have no sysreg.h
definition, due to msr_s/mrs_s being used in place of msr/mrs:
additions are made in order to fill in the gaps.
Signed-off-by: Dave Martin <Dave.Martin@arm.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Christoffer Dall <christoffer.dall@arm.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Will Deacon <will.deacon@arm.com>
Link: https://www.spinics.net/lists/kvm-arm/msg31717.html
[Rebased to v4.21-rc1]
Signed-off-by: Sudeep Holla <sudeep.holla@arm.com>
[Rebased to v5.2-rc5, changelog updates]
Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
2019-04-06 10:29:40 +00:00
|
|
|
*vcpu_pc(vcpu) = read_sysreg_el2(SYS_ELR);
|
2019-06-28 21:40:58 +00:00
|
|
|
vcpu_gp_regs(vcpu)->pstate = read_sysreg_el2(SYS_SPSR);
|
2018-11-09 15:07:11 +00:00
|
|
|
|
|
|
|
kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
|
|
|
|
|
2019-06-28 21:40:58 +00:00
|
|
|
write_sysreg_el2(vcpu_gp_regs(vcpu)->pstate, SYS_SPSR);
|
KVM: arm64: Migrate _elx sysreg accessors to msr_s/mrs_s
Currently, the {read,write}_sysreg_el*() accessors for accessing
particular ELs' sysregs in the presence of VHE rely on some local
hacks and define their system register encodings in a way that is
inconsistent with the core definitions in <asm/sysreg.h>.
As a result, it is necessary to add duplicate definitions for any
system register that already needs a definition in sysreg.h for
other reasons.
This is a bit of a maintenance headache, and the reasons for the
_el*() accessors working the way they do is a bit historical.
This patch gets rid of the shadow sysreg definitions in
<asm/kvm_hyp.h>, converts the _el*() accessors to use the core
__msr_s/__mrs_s interface, and converts all call sites to use the
standard sysreg #define names (i.e., upper case, with SYS_ prefix).
This patch will conflict heavily anyway, so the opportunity
to clean up some bad whitespace in the context of the changes is
taken.
The change exposes a few system registers that have no sysreg.h
definition, due to msr_s/mrs_s being used in place of msr/mrs:
additions are made in order to fill in the gaps.
Signed-off-by: Dave Martin <Dave.Martin@arm.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Christoffer Dall <christoffer.dall@arm.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Will Deacon <will.deacon@arm.com>
Link: https://www.spinics.net/lists/kvm-arm/msg31717.html
[Rebased to v4.21-rc1]
Signed-off-by: Sudeep Holla <sudeep.holla@arm.com>
[Rebased to v5.2-rc5, changelog updates]
Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
2019-04-06 10:29:40 +00:00
|
|
|
write_sysreg_el2(*vcpu_pc(vcpu), SYS_ELR);
|
2018-11-09 15:07:11 +00:00
|
|
|
}
|
|
|
|
|
2012-12-10 13:27:52 +00:00
|
|
|
#endif /* __ARM64_KVM_EMULATE_H__ */
|