Merge branch 'kvm-arm64/el2-pc' into kvmarm-master/next
[linux-2.6-microblaze.git] / arch / arm64 / kvm / inject_fault.c
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
17 static 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         u32 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
51 static void inject_undef64(struct kvm_vcpu *vcpu)
52 {
53         u32 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
73 static bool pre_fault_synchronize(struct kvm_vcpu *vcpu)
74 {
75         preempt_disable();
76         if (vcpu->arch.sysregs_loaded_on_cpu) {
77                 kvm_arch_vcpu_put(vcpu);
78                 return true;
79         }
80
81         preempt_enable();
82         return false;
83 }
84
85 static void post_fault_synchronize(struct kvm_vcpu *vcpu, bool loaded)
86 {
87         if (loaded) {
88                 kvm_arch_vcpu_load(vcpu, smp_processor_id());
89                 preempt_enable();
90         }
91 }
92
93 static void inject_undef32(struct kvm_vcpu *vcpu)
94 {
95         vcpu->arch.flags |= (KVM_ARM64_EXCEPT_AA32_UND |
96                              KVM_ARM64_PENDING_EXCEPTION);
97 }
98
99 /*
100  * Modelled after TakeDataAbortException() and TakePrefetchAbortException
101  * pseudocode.
102  */
103 static void inject_abt32(struct kvm_vcpu *vcpu, bool is_pabt,
104                          unsigned long addr)
105 {
106         u32 *far, *fsr;
107         bool is_lpae;
108         bool loaded;
109
110         loaded = pre_fault_synchronize(vcpu);
111
112         if (is_pabt) {
113                 vcpu->arch.flags |= (KVM_ARM64_EXCEPT_AA32_IABT |
114                                      KVM_ARM64_PENDING_EXCEPTION);
115                 far = &vcpu_cp15(vcpu, c6_IFAR);
116                 fsr = &vcpu_cp15(vcpu, c5_IFSR);
117         } else { /* !iabt */
118                 vcpu->arch.flags |= (KVM_ARM64_EXCEPT_AA32_DABT |
119                                      KVM_ARM64_PENDING_EXCEPTION);
120                 far = &vcpu_cp15(vcpu, c6_DFAR);
121                 fsr = &vcpu_cp15(vcpu, c5_DFSR);
122         }
123
124         *far = addr;
125
126         /* Give the guest an IMPLEMENTATION DEFINED exception */
127         is_lpae = (vcpu_cp15(vcpu, c2_TTBCR) >> 31);
128         if (is_lpae) {
129                 *fsr = DFSR_LPAE | DFSR_FSC_EXTABT_LPAE;
130         } else {
131                 /* no need to shuffle FS[4] into DFSR[10] as its 0 */
132                 *fsr = DFSR_FSC_EXTABT_nLPAE;
133         }
134
135         post_fault_synchronize(vcpu, loaded);
136 }
137
138 /**
139  * kvm_inject_dabt - inject a data abort into the guest
140  * @vcpu: The VCPU to receive the data abort
141  * @addr: The address to report in the DFAR
142  *
143  * It is assumed that this code is called from the VCPU thread and that the
144  * VCPU therefore is not currently executing guest code.
145  */
146 void kvm_inject_dabt(struct kvm_vcpu *vcpu, unsigned long addr)
147 {
148         if (vcpu_el1_is_32bit(vcpu))
149                 inject_abt32(vcpu, false, addr);
150         else
151                 inject_abt64(vcpu, false, addr);
152 }
153
154 /**
155  * kvm_inject_pabt - inject a prefetch abort into the guest
156  * @vcpu: The VCPU to receive the prefetch abort
157  * @addr: The address to report in the DFAR
158  *
159  * It is assumed that this code is called from the VCPU thread and that the
160  * VCPU therefore is not currently executing guest code.
161  */
162 void kvm_inject_pabt(struct kvm_vcpu *vcpu, unsigned long addr)
163 {
164         if (vcpu_el1_is_32bit(vcpu))
165                 inject_abt32(vcpu, true, addr);
166         else
167                 inject_abt64(vcpu, true, addr);
168 }
169
170 /**
171  * kvm_inject_undefined - inject an undefined instruction into the guest
172  * @vcpu: The vCPU in which to inject the exception
173  *
174  * It is assumed that this code is called from the VCPU thread and that the
175  * VCPU therefore is not currently executing guest code.
176  */
177 void kvm_inject_undefined(struct kvm_vcpu *vcpu)
178 {
179         if (vcpu_el1_is_32bit(vcpu))
180                 inject_undef32(vcpu);
181         else
182                 inject_undef64(vcpu);
183 }
184
185 void kvm_set_sei_esr(struct kvm_vcpu *vcpu, u64 esr)
186 {
187         vcpu_set_vsesr(vcpu, esr & ESR_ELx_ISS_MASK);
188         *vcpu_hcr(vcpu) |= HCR_VSE;
189 }
190
191 /**
192  * kvm_inject_vabt - inject an async abort / SError into the guest
193  * @vcpu: The VCPU to receive the exception
194  *
195  * It is assumed that this code is called from the VCPU thread and that the
196  * VCPU therefore is not currently executing guest code.
197  *
198  * Systems with the RAS Extensions specify an imp-def ESR (ISV/IDS = 1) with
199  * the remaining ISS all-zeros so that this error is not interpreted as an
200  * uncategorized RAS error. Without the RAS Extensions we can't specify an ESR
201  * value, so the CPU generates an imp-def value.
202  */
203 void kvm_inject_vabt(struct kvm_vcpu *vcpu)
204 {
205         kvm_set_sei_esr(vcpu, ESR_ELx_ISV);
206 }