.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * Copyright (C) 2012,2013 - ARM Ltd |
---|
3 | 4 | * Author: Marc Zyngier <marc.zyngier@arm.com> |
---|
.. | .. |
---|
5 | 6 | * Derived from arch/arm/kvm/handle_exit.c: |
---|
6 | 7 | * Copyright (C) 2012 - Virtual Open Systems and Columbia University |
---|
7 | 8 | * Author: Christoffer Dall <c.dall@virtualopensystems.com> |
---|
8 | | - * |
---|
9 | | - * This program is free software; you can redistribute it and/or modify |
---|
10 | | - * it under the terms of the GNU General Public License version 2 as |
---|
11 | | - * published by the Free Software Foundation. |
---|
12 | | - * |
---|
13 | | - * This program is distributed in the hope that it will be useful, |
---|
14 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | | - * GNU General Public License for more details. |
---|
17 | | - * |
---|
18 | | - * You should have received a copy of the GNU General Public License |
---|
19 | | - * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
20 | 9 | */ |
---|
21 | 10 | |
---|
22 | 11 | #include <linux/kvm.h> |
---|
23 | 12 | #include <linux/kvm_host.h> |
---|
24 | 13 | |
---|
25 | | -#include <kvm/arm_psci.h> |
---|
26 | | - |
---|
27 | 14 | #include <asm/esr.h> |
---|
28 | 15 | #include <asm/exception.h> |
---|
29 | 16 | #include <asm/kvm_asm.h> |
---|
30 | | -#include <asm/kvm_coproc.h> |
---|
31 | 17 | #include <asm/kvm_emulate.h> |
---|
32 | 18 | #include <asm/kvm_mmu.h> |
---|
33 | 19 | #include <asm/debug-monitors.h> |
---|
34 | 20 | #include <asm/traps.h> |
---|
35 | 21 | |
---|
36 | | -#define CREATE_TRACE_POINTS |
---|
37 | | -#include "trace.h" |
---|
| 22 | +#include <kvm/arm_hypercalls.h> |
---|
38 | 23 | |
---|
39 | | -typedef int (*exit_handle_fn)(struct kvm_vcpu *, struct kvm_run *); |
---|
| 24 | +#define CREATE_TRACE_POINTS |
---|
| 25 | +#include "trace_handle_exit.h" |
---|
| 26 | + |
---|
| 27 | +typedef int (*exit_handle_fn)(struct kvm_vcpu *); |
---|
40 | 28 | |
---|
41 | 29 | static void kvm_handle_guest_serror(struct kvm_vcpu *vcpu, u32 esr) |
---|
42 | 30 | { |
---|
.. | .. |
---|
44 | 32 | kvm_inject_vabt(vcpu); |
---|
45 | 33 | } |
---|
46 | 34 | |
---|
47 | | -static int handle_hvc(struct kvm_vcpu *vcpu, struct kvm_run *run) |
---|
| 35 | +static int handle_hvc(struct kvm_vcpu *vcpu) |
---|
48 | 36 | { |
---|
49 | 37 | int ret; |
---|
50 | 38 | |
---|
.. | .. |
---|
61 | 49 | return ret; |
---|
62 | 50 | } |
---|
63 | 51 | |
---|
64 | | -static int handle_smc(struct kvm_vcpu *vcpu, struct kvm_run *run) |
---|
| 52 | +static int handle_smc(struct kvm_vcpu *vcpu) |
---|
65 | 53 | { |
---|
66 | 54 | /* |
---|
67 | 55 | * "If an SMC instruction executed at Non-secure EL1 is |
---|
.. | .. |
---|
72 | 60 | * otherwise return to the same address... |
---|
73 | 61 | */ |
---|
74 | 62 | vcpu_set_reg(vcpu, 0, ~0UL); |
---|
75 | | - kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu)); |
---|
| 63 | + kvm_incr_pc(vcpu); |
---|
76 | 64 | return 1; |
---|
77 | 65 | } |
---|
78 | 66 | |
---|
.. | .. |
---|
80 | 68 | * Guest access to FP/ASIMD registers are routed to this handler only |
---|
81 | 69 | * when the system doesn't support FP/ASIMD. |
---|
82 | 70 | */ |
---|
83 | | -static int handle_no_fpsimd(struct kvm_vcpu *vcpu, struct kvm_run *run) |
---|
| 71 | +static int handle_no_fpsimd(struct kvm_vcpu *vcpu) |
---|
84 | 72 | { |
---|
85 | 73 | kvm_inject_undefined(vcpu); |
---|
86 | 74 | return 1; |
---|
.. | .. |
---|
98 | 86 | * world-switches and schedule other host processes until there is an |
---|
99 | 87 | * incoming IRQ or FIQ to the VM. |
---|
100 | 88 | */ |
---|
101 | | -static int kvm_handle_wfx(struct kvm_vcpu *vcpu, struct kvm_run *run) |
---|
| 89 | +static int kvm_handle_wfx(struct kvm_vcpu *vcpu) |
---|
102 | 90 | { |
---|
103 | | - if (kvm_vcpu_get_hsr(vcpu) & ESR_ELx_WFx_ISS_WFE) { |
---|
| 91 | + if (kvm_vcpu_get_esr(vcpu) & ESR_ELx_WFx_ISS_WFE) { |
---|
104 | 92 | trace_kvm_wfx_arm64(*vcpu_pc(vcpu), true); |
---|
105 | 93 | vcpu->stat.wfe_exit_stat++; |
---|
106 | 94 | kvm_vcpu_on_spin(vcpu, vcpu_mode_priv(vcpu)); |
---|
.. | .. |
---|
111 | 99 | kvm_clear_request(KVM_REQ_UNHALT, vcpu); |
---|
112 | 100 | } |
---|
113 | 101 | |
---|
114 | | - kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu)); |
---|
| 102 | + kvm_incr_pc(vcpu); |
---|
115 | 103 | |
---|
116 | 104 | return 1; |
---|
117 | 105 | } |
---|
.. | .. |
---|
120 | 108 | * kvm_handle_guest_debug - handle a debug exception instruction |
---|
121 | 109 | * |
---|
122 | 110 | * @vcpu: the vcpu pointer |
---|
123 | | - * @run: access to the kvm_run structure for results |
---|
124 | 111 | * |
---|
125 | 112 | * We route all debug exceptions through the same handler. If both the |
---|
126 | 113 | * guest and host are using the same debug facilities it will be up to |
---|
127 | 114 | * userspace to re-inject the correct exception for guest delivery. |
---|
128 | 115 | * |
---|
129 | | - * @return: 0 (while setting run->exit_reason), -1 for error |
---|
| 116 | + * @return: 0 (while setting vcpu->run->exit_reason), -1 for error |
---|
130 | 117 | */ |
---|
131 | | -static int kvm_handle_guest_debug(struct kvm_vcpu *vcpu, struct kvm_run *run) |
---|
| 118 | +static int kvm_handle_guest_debug(struct kvm_vcpu *vcpu) |
---|
132 | 119 | { |
---|
133 | | - u32 hsr = kvm_vcpu_get_hsr(vcpu); |
---|
| 120 | + struct kvm_run *run = vcpu->run; |
---|
| 121 | + u32 esr = kvm_vcpu_get_esr(vcpu); |
---|
134 | 122 | int ret = 0; |
---|
135 | 123 | |
---|
136 | 124 | run->exit_reason = KVM_EXIT_DEBUG; |
---|
137 | | - run->debug.arch.hsr = hsr; |
---|
| 125 | + run->debug.arch.hsr = esr; |
---|
138 | 126 | |
---|
139 | | - switch (ESR_ELx_EC(hsr)) { |
---|
| 127 | + switch (ESR_ELx_EC(esr)) { |
---|
140 | 128 | case ESR_ELx_EC_WATCHPT_LOW: |
---|
141 | 129 | run->debug.arch.far = vcpu->arch.fault.far_el2; |
---|
142 | | - /* fall through */ |
---|
| 130 | + fallthrough; |
---|
143 | 131 | case ESR_ELx_EC_SOFTSTP_LOW: |
---|
144 | 132 | case ESR_ELx_EC_BREAKPT_LOW: |
---|
145 | 133 | case ESR_ELx_EC_BKPT32: |
---|
146 | 134 | case ESR_ELx_EC_BRK64: |
---|
147 | 135 | break; |
---|
148 | 136 | default: |
---|
149 | | - kvm_err("%s: un-handled case hsr: %#08x\n", |
---|
150 | | - __func__, (unsigned int) hsr); |
---|
| 137 | + kvm_err("%s: un-handled case esr: %#08x\n", |
---|
| 138 | + __func__, (unsigned int) esr); |
---|
151 | 139 | ret = -1; |
---|
152 | 140 | break; |
---|
153 | 141 | } |
---|
.. | .. |
---|
155 | 143 | return ret; |
---|
156 | 144 | } |
---|
157 | 145 | |
---|
158 | | -static int kvm_handle_unknown_ec(struct kvm_vcpu *vcpu, struct kvm_run *run) |
---|
| 146 | +static int kvm_handle_unknown_ec(struct kvm_vcpu *vcpu) |
---|
159 | 147 | { |
---|
160 | | - u32 hsr = kvm_vcpu_get_hsr(vcpu); |
---|
| 148 | + u32 esr = kvm_vcpu_get_esr(vcpu); |
---|
161 | 149 | |
---|
162 | | - kvm_pr_unimpl("Unknown exception class: hsr: %#08x -- %s\n", |
---|
163 | | - hsr, esr_get_class_string(hsr)); |
---|
| 150 | + kvm_pr_unimpl("Unknown exception class: esr: %#08x -- %s\n", |
---|
| 151 | + esr, esr_get_class_string(esr)); |
---|
164 | 152 | |
---|
165 | 153 | kvm_inject_undefined(vcpu); |
---|
166 | 154 | return 1; |
---|
167 | 155 | } |
---|
168 | 156 | |
---|
169 | | -static int handle_sve(struct kvm_vcpu *vcpu, struct kvm_run *run) |
---|
| 157 | +static int handle_sve(struct kvm_vcpu *vcpu) |
---|
170 | 158 | { |
---|
171 | 159 | /* Until SVE is supported for guests: */ |
---|
| 160 | + kvm_inject_undefined(vcpu); |
---|
| 161 | + return 1; |
---|
| 162 | +} |
---|
| 163 | + |
---|
| 164 | +/* |
---|
| 165 | + * Guest usage of a ptrauth instruction (which the guest EL1 did not turn into |
---|
| 166 | + * a NOP). If we get here, it is that we didn't fixup ptrauth on exit, and all |
---|
| 167 | + * that we can do is give the guest an UNDEF. |
---|
| 168 | + */ |
---|
| 169 | +static int kvm_handle_ptrauth(struct kvm_vcpu *vcpu) |
---|
| 170 | +{ |
---|
172 | 171 | kvm_inject_undefined(vcpu); |
---|
173 | 172 | return 1; |
---|
174 | 173 | } |
---|
.. | .. |
---|
195 | 194 | [ESR_ELx_EC_BKPT32] = kvm_handle_guest_debug, |
---|
196 | 195 | [ESR_ELx_EC_BRK64] = kvm_handle_guest_debug, |
---|
197 | 196 | [ESR_ELx_EC_FP_ASIMD] = handle_no_fpsimd, |
---|
| 197 | + [ESR_ELx_EC_PAC] = kvm_handle_ptrauth, |
---|
198 | 198 | }; |
---|
199 | 199 | |
---|
200 | 200 | static exit_handle_fn kvm_get_exit_handler(struct kvm_vcpu *vcpu) |
---|
201 | 201 | { |
---|
202 | | - u32 hsr = kvm_vcpu_get_hsr(vcpu); |
---|
203 | | - u8 hsr_ec = ESR_ELx_EC(hsr); |
---|
| 202 | + u32 esr = kvm_vcpu_get_esr(vcpu); |
---|
| 203 | + u8 esr_ec = ESR_ELx_EC(esr); |
---|
204 | 204 | |
---|
205 | | - return arm_exit_handlers[hsr_ec]; |
---|
| 205 | + return arm_exit_handlers[esr_ec]; |
---|
206 | 206 | } |
---|
207 | 207 | |
---|
208 | 208 | /* |
---|
.. | .. |
---|
211 | 211 | * KVM_EXIT_DEBUG, otherwise userspace needs to complete its |
---|
212 | 212 | * emulation first. |
---|
213 | 213 | */ |
---|
214 | | -static int handle_trap_exceptions(struct kvm_vcpu *vcpu, struct kvm_run *run) |
---|
| 214 | +static int handle_trap_exceptions(struct kvm_vcpu *vcpu) |
---|
215 | 215 | { |
---|
216 | 216 | int handled; |
---|
217 | 217 | |
---|
.. | .. |
---|
220 | 220 | * that fail their condition code check" |
---|
221 | 221 | */ |
---|
222 | 222 | if (!kvm_condition_valid(vcpu)) { |
---|
223 | | - kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu)); |
---|
| 223 | + kvm_incr_pc(vcpu); |
---|
224 | 224 | handled = 1; |
---|
225 | 225 | } else { |
---|
226 | 226 | exit_handle_fn exit_handler; |
---|
227 | 227 | |
---|
228 | 228 | exit_handler = kvm_get_exit_handler(vcpu); |
---|
229 | | - handled = exit_handler(vcpu, run); |
---|
| 229 | + handled = exit_handler(vcpu); |
---|
230 | 230 | } |
---|
231 | | - |
---|
232 | | - /* |
---|
233 | | - * kvm_arm_handle_step_debug() sets the exit_reason on the kvm_run |
---|
234 | | - * structure if we need to return to userspace. |
---|
235 | | - */ |
---|
236 | | - if (handled > 0 && kvm_arm_handle_step_debug(vcpu, run)) |
---|
237 | | - handled = 0; |
---|
238 | 231 | |
---|
239 | 232 | return handled; |
---|
240 | 233 | } |
---|
.. | .. |
---|
243 | 236 | * Return > 0 to return to guest, < 0 on error, 0 (and set exit_reason) on |
---|
244 | 237 | * proper exit to userspace. |
---|
245 | 238 | */ |
---|
246 | | -int handle_exit(struct kvm_vcpu *vcpu, struct kvm_run *run, |
---|
247 | | - int exception_index) |
---|
| 239 | +int handle_exit(struct kvm_vcpu *vcpu, int exception_index) |
---|
248 | 240 | { |
---|
| 241 | + struct kvm_run *run = vcpu->run; |
---|
| 242 | + |
---|
249 | 243 | if (ARM_SERROR_PENDING(exception_index)) { |
---|
250 | | - u8 hsr_ec = ESR_ELx_EC(kvm_vcpu_get_hsr(vcpu)); |
---|
251 | | - |
---|
252 | 244 | /* |
---|
253 | | - * HVC/SMC already have an adjusted PC, which we need |
---|
254 | | - * to correct in order to return to after having |
---|
255 | | - * injected the SError. |
---|
| 245 | + * The SError is handled by handle_exit_early(). If the guest |
---|
| 246 | + * survives it will re-execute the original instruction. |
---|
256 | 247 | */ |
---|
257 | | - if (hsr_ec == ESR_ELx_EC_HVC32 || hsr_ec == ESR_ELx_EC_HVC64 || |
---|
258 | | - hsr_ec == ESR_ELx_EC_SMC32 || hsr_ec == ESR_ELx_EC_SMC64) { |
---|
259 | | - u32 adj = kvm_vcpu_trap_il_is32bit(vcpu) ? 4 : 2; |
---|
260 | | - *vcpu_pc(vcpu) -= adj; |
---|
261 | | - } |
---|
262 | | - |
---|
263 | 248 | return 1; |
---|
264 | 249 | } |
---|
265 | 250 | |
---|
.. | .. |
---|
269 | 254 | case ARM_EXCEPTION_IRQ: |
---|
270 | 255 | return 1; |
---|
271 | 256 | case ARM_EXCEPTION_EL1_SERROR: |
---|
272 | | - /* We may still need to return for single-step */ |
---|
273 | | - if (!(*vcpu_cpsr(vcpu) & DBG_SPSR_SS) |
---|
274 | | - && kvm_arm_handle_step_debug(vcpu, run)) |
---|
275 | | - return 0; |
---|
276 | | - else |
---|
277 | | - return 1; |
---|
| 257 | + return 1; |
---|
278 | 258 | case ARM_EXCEPTION_TRAP: |
---|
279 | | - return handle_trap_exceptions(vcpu, run); |
---|
| 259 | + return handle_trap_exceptions(vcpu); |
---|
280 | 260 | case ARM_EXCEPTION_HYP_GONE: |
---|
281 | 261 | /* |
---|
282 | 262 | * EL2 has been reset to the hyp-stub. This happens when a guest |
---|
.. | .. |
---|
284 | 264 | */ |
---|
285 | 265 | run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
---|
286 | 266 | return 0; |
---|
| 267 | + case ARM_EXCEPTION_IL: |
---|
| 268 | + /* |
---|
| 269 | + * We attempted an illegal exception return. Guest state must |
---|
| 270 | + * have been corrupted somehow. Give up. |
---|
| 271 | + */ |
---|
| 272 | + run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
---|
| 273 | + return -EINVAL; |
---|
287 | 274 | default: |
---|
288 | 275 | kvm_pr_unimpl("Unsupported exception type: %d", |
---|
289 | 276 | exception_index); |
---|
.. | .. |
---|
293 | 280 | } |
---|
294 | 281 | |
---|
295 | 282 | /* For exit types that need handling before we can be preempted */ |
---|
296 | | -void handle_exit_early(struct kvm_vcpu *vcpu, struct kvm_run *run, |
---|
297 | | - int exception_index) |
---|
| 283 | +void handle_exit_early(struct kvm_vcpu *vcpu, int exception_index) |
---|
298 | 284 | { |
---|
299 | 285 | if (ARM_SERROR_PENDING(exception_index)) { |
---|
300 | 286 | if (this_cpu_has_cap(ARM64_HAS_RAS_EXTN)) { |
---|
.. | .. |
---|
311 | 297 | exception_index = ARM_EXCEPTION_CODE(exception_index); |
---|
312 | 298 | |
---|
313 | 299 | if (exception_index == ARM_EXCEPTION_EL1_SERROR) |
---|
314 | | - kvm_handle_guest_serror(vcpu, kvm_vcpu_get_hsr(vcpu)); |
---|
| 300 | + kvm_handle_guest_serror(vcpu, kvm_vcpu_get_esr(vcpu)); |
---|
315 | 301 | } |
---|