1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2015-2018 - ARM Ltd
4 * Author: Marc Zyngier <marc.zyngier@arm.com>
7 #include <linux/arm-smccc.h>
8 #include <linux/linkage.h>
10 #include <asm/alternative.h>
11 #include <asm/assembler.h>
12 #include <asm/cpufeature.h>
13 #include <asm/kvm_arm.h>
14 #include <asm/kvm_asm.h>
16 #include <asm/spectre.h>
18 .macro save_caller_saved_regs_vect
19 /* x0 and x1 were saved in the vector entry */
20 stp x2, x3, [sp, #-16]!
21 stp x4, x5, [sp, #-16]!
22 stp x6, x7, [sp, #-16]!
23 stp x8, x9, [sp, #-16]!
24 stp x10, x11, [sp, #-16]!
25 stp x12, x13, [sp, #-16]!
26 stp x14, x15, [sp, #-16]!
27 stp x16, x17, [sp, #-16]!
30 .macro restore_caller_saved_regs_vect
31 ldp x16, x17, [sp], #16
32 ldp x14, x15, [sp], #16
33 ldp x12, x13, [sp], #16
34 ldp x10, x11, [sp], #16
44 el1_sync: // Guest trapped into EL2
47 ubfx x0, x0, #ESR_ELx_EC_SHIFT, #ESR_ELx_EC_WIDTH
48 cmp x0, #ESR_ELx_EC_HVC64
49 ccmp x0, #ESR_ELx_EC_HVC32, #4, ne
53 * Fastest possible path for ARM_SMCCC_ARCH_WORKAROUND_1.
54 * The workaround has already been applied on the host,
55 * so let's quickly get back to the guest. We don't bother
56 * restoring x1, as it can be clobbered anyway.
58 ldr x1, [sp] // Guest's x0
59 eor w1, w1, #ARM_SMCCC_ARCH_WORKAROUND_1
62 /* ARM_SMCCC_ARCH_WORKAROUND_2 handling */
63 eor w1, w1, #(ARM_SMCCC_ARCH_WORKAROUND_1 ^ \
64 ARM_SMCCC_ARCH_WORKAROUND_2)
67 eor w1, w1, #(ARM_SMCCC_ARCH_WORKAROUND_2 ^ \
68 ARM_SMCCC_ARCH_WORKAROUND_3)
79 mov x0, #ARM_EXCEPTION_TRAP
85 mov x0, #ARM_EXCEPTION_IRQ
90 mov x0, #ARM_EXCEPTION_EL1_SERROR
94 /* Check for illegal exception return */
98 save_caller_saved_regs_vect
99 stp x29, x30, [sp, #-16]!
100 bl kvm_unexpected_el2_exception
101 ldp x29, x30, [sp], #16
102 restore_caller_saved_regs_vect
107 /* Let's attempt a recovery from the illegal exception return */
109 mov x0, #ARM_EXCEPTION_IL
114 save_caller_saved_regs_vect
115 stp x29, x30, [sp, #-16]!
117 bl kvm_unexpected_el2_exception
119 ldp x29, x30, [sp], #16
120 restore_caller_saved_regs_vect
125 .macro invalid_vector label, target = __guest_exit_panic
127 SYM_CODE_START_LOCAL(\label)
132 /* None of these should ever happen */
133 invalid_vector el2t_sync_invalid
134 invalid_vector el2t_irq_invalid
135 invalid_vector el2t_fiq_invalid
136 invalid_vector el2t_error_invalid
137 invalid_vector el2h_irq_invalid
138 invalid_vector el2h_fiq_invalid
144 .macro check_preamble_length start, end
145 /* kvm_patch_vector_branch() generates code that jumps over the preamble. */
146 .if ((\end-\start) != KVM_VECTOR_PREAMBLE)
147 .error "KVM vector preamble length mismatch"
151 .macro valid_vect target
155 stp x0, x1, [sp, #-16]!
159 check_preamble_length 661b, 662b
162 .macro invalid_vect target
166 stp x0, x1, [sp, #-16]!
170 check_preamble_length 661b, 662b
173 SYM_CODE_START(__kvm_hyp_vector)
174 invalid_vect el2t_sync_invalid // Synchronous EL2t
175 invalid_vect el2t_irq_invalid // IRQ EL2t
176 invalid_vect el2t_fiq_invalid // FIQ EL2t
177 invalid_vect el2t_error_invalid // Error EL2t
179 valid_vect el2_sync // Synchronous EL2h
180 invalid_vect el2h_irq_invalid // IRQ EL2h
181 invalid_vect el2h_fiq_invalid // FIQ EL2h
182 valid_vect el2_error // Error EL2h
184 valid_vect el1_sync // Synchronous 64-bit EL1
185 valid_vect el1_irq // IRQ 64-bit EL1
186 valid_vect el1_fiq // FIQ 64-bit EL1
187 valid_vect el1_error // Error 64-bit EL1
189 valid_vect el1_sync // Synchronous 32-bit EL1
190 valid_vect el1_irq // IRQ 32-bit EL1
191 valid_vect el1_fiq // FIQ 32-bit EL1
192 valid_vect el1_error // Error 32-bit EL1
193 SYM_CODE_END(__kvm_hyp_vector)
195 .macro spectrev2_smccc_wa1_smc
197 stp x2, x3, [sp, #(8 * 0)]
198 stp x0, x1, [sp, #(8 * 2)]
199 alternative_cb ARM64_ALWAYS_SYSTEM, spectre_bhb_patch_wa3
200 /* Patched to mov WA3 when supported */
201 mov w0, #ARM_SMCCC_ARCH_WORKAROUND_1
204 ldp x2, x3, [sp, #(8 * 0)]
208 .macro hyp_ventry indirect, spectrev2
212 spectrev2_smccc_wa1_smc
214 stp x0, x1, [sp, #-16]!
215 mitigate_spectre_bhb_loop x0
216 mitigate_spectre_bhb_clear_insn
219 alternative_cb ARM64_ALWAYS_SYSTEM, kvm_patch_vector_branch
221 * For ARM64_SPECTRE_V3A configurations, these NOPs get replaced with:
223 * movz x0, #(addr & 0xffff)
224 * movk x0, #((addr >> 16) & 0xffff), lsl #16
225 * movk x0, #((addr >> 32) & 0xffff), lsl #32
229 * addr = kern_hyp_va(__kvm_hyp_vector) + vector-offset + KVM_VECTOR_PREAMBLE.
230 * See kvm_patch_vector_branch for details.
238 b __kvm_hyp_vector + (1b - 0b + KVM_VECTOR_PREAMBLE)
241 .macro generate_vectors indirect, spectrev2
244 hyp_ventry \indirect, \spectrev2
246 .org 0b + SZ_2K // Safety measure
250 SYM_CODE_START(__bp_harden_hyp_vecs)
251 generate_vectors indirect = 0, spectrev2 = 1 // HYP_VECTOR_SPECTRE_DIRECT
252 generate_vectors indirect = 1, spectrev2 = 0 // HYP_VECTOR_INDIRECT
253 generate_vectors indirect = 1, spectrev2 = 1 // HYP_VECTOR_SPECTRE_INDIRECT
254 1: .org __bp_harden_hyp_vecs + __BP_HARDEN_HYP_VECS_SZ
256 SYM_CODE_END(__bp_harden_hyp_vecs)