cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

inject_fault.c (5837B)


      1// SPDX-License-Identifier: GPL-2.0-only
      2/*
      3 * Fault injection for both 32 and 64bit guests.
      4 *
      5 * Copyright (C) 2012,2013 - ARM Ltd
      6 * Author: Marc Zyngier <marc.zyngier@arm.com>
      7 *
      8 * Based on arch/arm/kvm/emulate.c
      9 * Copyright (C) 2012 - Virtual Open Systems and Columbia University
     10 * Author: Christoffer Dall <c.dall@virtualopensystems.com>
     11 */
     12
     13#include <linux/kvm_host.h>
     14#include <asm/kvm_emulate.h>
     15#include <asm/esr.h>
     16
     17static void inject_abt64(struct kvm_vcpu *vcpu, bool is_iabt, unsigned long addr)
     18{
     19	unsigned long cpsr = *vcpu_cpsr(vcpu);
     20	bool is_aarch32 = vcpu_mode_is_32bit(vcpu);
     21	u64 esr = 0;
     22
     23	vcpu->arch.flags |= (KVM_ARM64_EXCEPT_AA64_EL1		|
     24			     KVM_ARM64_EXCEPT_AA64_ELx_SYNC	|
     25			     KVM_ARM64_PENDING_EXCEPTION);
     26
     27	vcpu_write_sys_reg(vcpu, addr, FAR_EL1);
     28
     29	/*
     30	 * Build an {i,d}abort, depending on the level and the
     31	 * instruction set. Report an external synchronous abort.
     32	 */
     33	if (kvm_vcpu_trap_il_is32bit(vcpu))
     34		esr |= ESR_ELx_IL;
     35
     36	/*
     37	 * Here, the guest runs in AArch64 mode when in EL1. If we get
     38	 * an AArch32 fault, it means we managed to trap an EL0 fault.
     39	 */
     40	if (is_aarch32 || (cpsr & PSR_MODE_MASK) == PSR_MODE_EL0t)
     41		esr |= (ESR_ELx_EC_IABT_LOW << ESR_ELx_EC_SHIFT);
     42	else
     43		esr |= (ESR_ELx_EC_IABT_CUR << ESR_ELx_EC_SHIFT);
     44
     45	if (!is_iabt)
     46		esr |= ESR_ELx_EC_DABT_LOW << ESR_ELx_EC_SHIFT;
     47
     48	vcpu_write_sys_reg(vcpu, esr | ESR_ELx_FSC_EXTABT, ESR_EL1);
     49}
     50
     51static void inject_undef64(struct kvm_vcpu *vcpu)
     52{
     53	u64 esr = (ESR_ELx_EC_UNKNOWN << ESR_ELx_EC_SHIFT);
     54
     55	vcpu->arch.flags |= (KVM_ARM64_EXCEPT_AA64_EL1		|
     56			     KVM_ARM64_EXCEPT_AA64_ELx_SYNC	|
     57			     KVM_ARM64_PENDING_EXCEPTION);
     58
     59	/*
     60	 * Build an unknown exception, depending on the instruction
     61	 * set.
     62	 */
     63	if (kvm_vcpu_trap_il_is32bit(vcpu))
     64		esr |= ESR_ELx_IL;
     65
     66	vcpu_write_sys_reg(vcpu, esr, ESR_EL1);
     67}
     68
     69#define DFSR_FSC_EXTABT_LPAE	0x10
     70#define DFSR_FSC_EXTABT_nLPAE	0x08
     71#define DFSR_LPAE		BIT(9)
     72#define TTBCR_EAE		BIT(31)
     73
     74static void inject_undef32(struct kvm_vcpu *vcpu)
     75{
     76	vcpu->arch.flags |= (KVM_ARM64_EXCEPT_AA32_UND |
     77			     KVM_ARM64_PENDING_EXCEPTION);
     78}
     79
     80/*
     81 * Modelled after TakeDataAbortException() and TakePrefetchAbortException
     82 * pseudocode.
     83 */
     84static void inject_abt32(struct kvm_vcpu *vcpu, bool is_pabt, u32 addr)
     85{
     86	u64 far;
     87	u32 fsr;
     88
     89	/* Give the guest an IMPLEMENTATION DEFINED exception */
     90	if (vcpu_read_sys_reg(vcpu, TCR_EL1) & TTBCR_EAE) {
     91		fsr = DFSR_LPAE | DFSR_FSC_EXTABT_LPAE;
     92	} else {
     93		/* no need to shuffle FS[4] into DFSR[10] as its 0 */
     94		fsr = DFSR_FSC_EXTABT_nLPAE;
     95	}
     96
     97	far = vcpu_read_sys_reg(vcpu, FAR_EL1);
     98
     99	if (is_pabt) {
    100		vcpu->arch.flags |= (KVM_ARM64_EXCEPT_AA32_IABT |
    101				     KVM_ARM64_PENDING_EXCEPTION);
    102		far &= GENMASK(31, 0);
    103		far |= (u64)addr << 32;
    104		vcpu_write_sys_reg(vcpu, fsr, IFSR32_EL2);
    105	} else { /* !iabt */
    106		vcpu->arch.flags |= (KVM_ARM64_EXCEPT_AA32_DABT |
    107				     KVM_ARM64_PENDING_EXCEPTION);
    108		far &= GENMASK(63, 32);
    109		far |= addr;
    110		vcpu_write_sys_reg(vcpu, fsr, ESR_EL1);
    111	}
    112
    113	vcpu_write_sys_reg(vcpu, far, FAR_EL1);
    114}
    115
    116/**
    117 * kvm_inject_dabt - inject a data abort into the guest
    118 * @vcpu: The VCPU to receive the data abort
    119 * @addr: The address to report in the DFAR
    120 *
    121 * It is assumed that this code is called from the VCPU thread and that the
    122 * VCPU therefore is not currently executing guest code.
    123 */
    124void kvm_inject_dabt(struct kvm_vcpu *vcpu, unsigned long addr)
    125{
    126	if (vcpu_el1_is_32bit(vcpu))
    127		inject_abt32(vcpu, false, addr);
    128	else
    129		inject_abt64(vcpu, false, addr);
    130}
    131
    132/**
    133 * kvm_inject_pabt - inject a prefetch abort into the guest
    134 * @vcpu: The VCPU to receive the prefetch abort
    135 * @addr: The address to report in the DFAR
    136 *
    137 * It is assumed that this code is called from the VCPU thread and that the
    138 * VCPU therefore is not currently executing guest code.
    139 */
    140void kvm_inject_pabt(struct kvm_vcpu *vcpu, unsigned long addr)
    141{
    142	if (vcpu_el1_is_32bit(vcpu))
    143		inject_abt32(vcpu, true, addr);
    144	else
    145		inject_abt64(vcpu, true, addr);
    146}
    147
    148void kvm_inject_size_fault(struct kvm_vcpu *vcpu)
    149{
    150	unsigned long addr, esr;
    151
    152	addr  = kvm_vcpu_get_fault_ipa(vcpu);
    153	addr |= kvm_vcpu_get_hfar(vcpu) & GENMASK(11, 0);
    154
    155	if (kvm_vcpu_trap_is_iabt(vcpu))
    156		kvm_inject_pabt(vcpu, addr);
    157	else
    158		kvm_inject_dabt(vcpu, addr);
    159
    160	/*
    161	 * If AArch64 or LPAE, set FSC to 0 to indicate an Address
    162	 * Size Fault at level 0, as if exceeding PARange.
    163	 *
    164	 * Non-LPAE guests will only get the external abort, as there
    165	 * is no way to to describe the ASF.
    166	 */
    167	if (vcpu_el1_is_32bit(vcpu) &&
    168	    !(vcpu_read_sys_reg(vcpu, TCR_EL1) & TTBCR_EAE))
    169		return;
    170
    171	esr = vcpu_read_sys_reg(vcpu, ESR_EL1);
    172	esr &= ~GENMASK_ULL(5, 0);
    173	vcpu_write_sys_reg(vcpu, esr, ESR_EL1);
    174}
    175
    176/**
    177 * kvm_inject_undefined - inject an undefined instruction into the guest
    178 * @vcpu: The vCPU in which to inject the exception
    179 *
    180 * It is assumed that this code is called from the VCPU thread and that the
    181 * VCPU therefore is not currently executing guest code.
    182 */
    183void kvm_inject_undefined(struct kvm_vcpu *vcpu)
    184{
    185	if (vcpu_el1_is_32bit(vcpu))
    186		inject_undef32(vcpu);
    187	else
    188		inject_undef64(vcpu);
    189}
    190
    191void kvm_set_sei_esr(struct kvm_vcpu *vcpu, u64 esr)
    192{
    193	vcpu_set_vsesr(vcpu, esr & ESR_ELx_ISS_MASK);
    194	*vcpu_hcr(vcpu) |= HCR_VSE;
    195}
    196
    197/**
    198 * kvm_inject_vabt - inject an async abort / SError into the guest
    199 * @vcpu: The VCPU to receive the exception
    200 *
    201 * It is assumed that this code is called from the VCPU thread and that the
    202 * VCPU therefore is not currently executing guest code.
    203 *
    204 * Systems with the RAS Extensions specify an imp-def ESR (ISV/IDS = 1) with
    205 * the remaining ISS all-zeros so that this error is not interpreted as an
    206 * uncategorized RAS error. Without the RAS Extensions we can't specify an ESR
    207 * value, so the CPU generates an imp-def value.
    208 */
    209void kvm_inject_vabt(struct kvm_vcpu *vcpu)
    210{
    211	kvm_set_sei_esr(vcpu, ESR_ELx_ISV);
    212}