2 * Fault injection for 64bit guests.
4 * Copyright (C) 2012,2013 - ARM Ltd
5 * Author: Marc Zyngier <marc.zyngier@arm.com>
7 * Based on arch/arm/kvm/emulate.c
8 * Copyright (C) 2012 - Virtual Open Systems and Columbia University
9 * Author: Christoffer Dall <c.dall@virtualopensystems.com>
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include <linux/kvm_host.h>
25 #include <asm/kvm_emulate.h>
28 #define PSTATE_FAULT_BITS_64 (PSR_MODE_EL1h | PSR_A_BIT | PSR_F_BIT | \
29 PSR_I_BIT | PSR_D_BIT)
30 #define EL1_EXCEPT_SYNC_OFFSET 0x200
32 static void inject_abt64(struct kvm_vcpu *vcpu, bool is_iabt, unsigned long addr)
34 unsigned long cpsr = *vcpu_cpsr(vcpu);
38 is_aarch32 = vcpu_mode_is_32bit(vcpu);
40 *vcpu_spsr(vcpu) = cpsr;
41 *vcpu_elr_el1(vcpu) = *vcpu_pc(vcpu);
43 *vcpu_cpsr(vcpu) = PSTATE_FAULT_BITS_64;
44 *vcpu_pc(vcpu) = vcpu_sys_reg(vcpu, VBAR_EL1) + EL1_EXCEPT_SYNC_OFFSET;
46 vcpu_sys_reg(vcpu, FAR_EL1) = addr;
49 * Build an {i,d}abort, depending on the level and the
50 * instruction set. Report an external synchronous abort.
52 if (kvm_vcpu_trap_il_is32bit(vcpu))
56 * Here, the guest runs in AArch64 mode when in EL1. If we get
57 * an AArch32 fault, it means we managed to trap an EL0 fault.
59 if (is_aarch32 || (cpsr & PSR_MODE_MASK) == PSR_MODE_EL0t)
60 esr |= (ESR_EL1_EC_IABT_EL0 << ESR_EL1_EC_SHIFT);
62 esr |= (ESR_EL1_EC_IABT_EL1 << ESR_EL1_EC_SHIFT);
65 esr |= ESR_EL1_EC_DABT_EL0;
67 vcpu_sys_reg(vcpu, ESR_EL1) = esr | ESR_EL2_EC_xABT_xFSR_EXTABT;
70 static void inject_undef64(struct kvm_vcpu *vcpu)
72 unsigned long cpsr = *vcpu_cpsr(vcpu);
73 u32 esr = (ESR_EL1_EC_UNKNOWN << ESR_EL1_EC_SHIFT);
75 *vcpu_spsr(vcpu) = cpsr;
76 *vcpu_elr_el1(vcpu) = *vcpu_pc(vcpu);
78 *vcpu_cpsr(vcpu) = PSTATE_FAULT_BITS_64;
79 *vcpu_pc(vcpu) = vcpu_sys_reg(vcpu, VBAR_EL1) + EL1_EXCEPT_SYNC_OFFSET;
82 * Build an unknown exception, depending on the instruction
85 if (kvm_vcpu_trap_il_is32bit(vcpu))
88 vcpu_sys_reg(vcpu, ESR_EL1) = esr;
92 * kvm_inject_dabt - inject a data abort into the guest
93 * @vcpu: The VCPU to receive the undefined exception
94 * @addr: The address to report in the DFAR
96 * It is assumed that this code is called from the VCPU thread and that the
97 * VCPU therefore is not currently executing guest code.
99 void kvm_inject_dabt(struct kvm_vcpu *vcpu, unsigned long addr)
101 inject_abt64(vcpu, false, addr);
105 * kvm_inject_pabt - inject a prefetch abort into the guest
106 * @vcpu: The VCPU to receive the undefined exception
107 * @addr: The address to report in the DFAR
109 * It is assumed that this code is called from the VCPU thread and that the
110 * VCPU therefore is not currently executing guest code.
112 void kvm_inject_pabt(struct kvm_vcpu *vcpu, unsigned long addr)
114 inject_abt64(vcpu, true, addr);
118 * kvm_inject_undefined - inject an undefined instruction into the guest
120 * It is assumed that this code is called from the VCPU thread and that the
121 * VCPU therefore is not currently executing guest code.
123 void kvm_inject_undefined(struct kvm_vcpu *vcpu)
125 inject_undef64(vcpu);