hyp-entry.S 5.36 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0-only */
2
/*
3
 * Copyright (C) 2015-2018 - ARM Ltd
4 5 6
 * Author: Marc Zyngier <marc.zyngier@arm.com>
 */

7
#include <linux/arm-smccc.h>
8 9 10 11 12 13 14
#include <linux/linkage.h>

#include <asm/alternative.h>
#include <asm/assembler.h>
#include <asm/cpufeature.h>
#include <asm/kvm_arm.h>
#include <asm/kvm_asm.h>
15
#include <asm/mmu.h>
16
#include <asm/spectre.h>
17

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
.macro save_caller_saved_regs_vect
	/* x0 and x1 were saved in the vector entry */
	stp	x2, x3,   [sp, #-16]!
	stp	x4, x5,   [sp, #-16]!
	stp	x6, x7,   [sp, #-16]!
	stp	x8, x9,   [sp, #-16]!
	stp	x10, x11, [sp, #-16]!
	stp	x12, x13, [sp, #-16]!
	stp	x14, x15, [sp, #-16]!
	stp	x16, x17, [sp, #-16]!
.endm

.macro restore_caller_saved_regs_vect
	ldp	x16, x17, [sp], #16
	ldp	x14, x15, [sp], #16
	ldp	x12, x13, [sp], #16
	ldp	x10, x11, [sp], #16
	ldp	x8, x9,   [sp], #16
	ldp	x6, x7,   [sp], #16
	ldp	x4, x5,   [sp], #16
	ldp	x2, x3,   [sp], #16
	ldp	x0, x1,   [sp], #16
.endm

42 43 44 45
	.text

el1_sync:				// Guest trapped into EL2

46 47
	mrs	x0, esr_el2
	lsr	x0, x0, #ESR_ELx_EC_SHIFT
48
	cmp	x0, #ESR_ELx_EC_HVC64
49
	ccmp	x0, #ESR_ELx_EC_HVC32, #4, ne
50 51
	b.ne	el1_trap

52 53 54 55 56 57 58 59
	/*
	 * Fastest possible path for ARM_SMCCC_ARCH_WORKAROUND_1.
	 * The workaround has already been applied on the host,
	 * so let's quickly get back to the guest. We don't bother
	 * restoring x1, as it can be clobbered anyway.
	 */
	ldr	x1, [sp]				// Guest's x0
	eor	w1, w1, #ARM_SMCCC_ARCH_WORKAROUND_1
60 61 62 63 64
	cbz	w1, wa_epilogue

	/* ARM_SMCCC_ARCH_WORKAROUND_2 handling */
	eor	w1, w1, #(ARM_SMCCC_ARCH_WORKAROUND_1 ^ \
			  ARM_SMCCC_ARCH_WORKAROUND_2)
65
	cbnz	w1, el1_trap
66 67 68

wa_epilogue:
	mov	x0, xzr
69 70
	add	sp, sp, #16
	eret
71
	sb
72

73
el1_trap:
74
	get_vcpu_ptr	x1, x0
75
	mov	x0, #ARM_EXCEPTION_TRAP
76 77 78
	b	__guest_exit

el1_irq:
79
el1_fiq:
80
	get_vcpu_ptr	x1, x0
81
	mov	x0, #ARM_EXCEPTION_IRQ
82 83
	b	__guest_exit

84
el1_error:
85
	get_vcpu_ptr	x1, x0
86 87 88
	mov	x0, #ARM_EXCEPTION_EL1_SERROR
	b	__guest_exit

89
el2_sync:
90
	/* Check for illegal exception return */
91
	mrs	x0, spsr_el2
92
	tbnz	x0, #20, 1f
93

94 95 96 97 98 99 100
	save_caller_saved_regs_vect
	stp     x29, x30, [sp, #-16]!
	bl	kvm_unexpected_el2_exception
	ldp     x29, x30, [sp], #16
	restore_caller_saved_regs_vect

	eret
101

102
1:
103 104 105 106 107 108
	/* Let's attempt a recovery from the illegal exception return */
	get_vcpu_ptr	x1, x0
	mov	x0, #ARM_EXCEPTION_IL
	b	__guest_exit


109
el2_error:
110 111 112 113 114 115 116
	save_caller_saved_regs_vect
	stp     x29, x30, [sp, #-16]!

	bl	kvm_unexpected_el2_exception

	ldp     x29, x30, [sp], #16
	restore_caller_saved_regs_vect
117

118
	eret
119
	sb
120

121
.macro invalid_vector	label, target = __guest_exit_panic
122
	.align	2
123
SYM_CODE_START_LOCAL(\label)
124
	b \target
125
SYM_CODE_END(\label)
126 127 128 129 130 131 132 133 134 135 136 137 138 139
.endm

	/* None of these should ever happen */
	invalid_vector	el2t_sync_invalid
	invalid_vector	el2t_irq_invalid
	invalid_vector	el2t_fiq_invalid
	invalid_vector	el2t_error_invalid
	invalid_vector	el2h_irq_invalid
	invalid_vector	el2h_fiq_invalid

	.ltorg

	.align 11

140 141 142 143 144 145 146
.macro check_preamble_length start, end
/* kvm_patch_vector_branch() generates code that jumps over the preamble. */
.if ((\end-\start) != KVM_VECTOR_PREAMBLE)
	.error "KVM vector preamble length mismatch"
.endif
.endm

147 148
.macro valid_vect target
	.align 7
149
661:
150
	esb
151
	stp	x0, x1, [sp, #-16]!
152
662:
153
	b	\target
154 155

check_preamble_length 661b, 662b
156 157 158 159
.endm

.macro invalid_vect target
	.align 7
160
661:
161
	nop
162
	stp	x0, x1, [sp, #-16]!
163
662:
164
	b	\target
165 166

check_preamble_length 661b, 662b
167 168
.endm

169
SYM_CODE_START(__kvm_hyp_vector)
170 171 172 173 174
	invalid_vect	el2t_sync_invalid	// Synchronous EL2t
	invalid_vect	el2t_irq_invalid	// IRQ EL2t
	invalid_vect	el2t_fiq_invalid	// FIQ EL2t
	invalid_vect	el2t_error_invalid	// Error EL2t

175
	valid_vect	el2_sync		// Synchronous EL2h
176 177 178 179 180 181
	invalid_vect	el2h_irq_invalid	// IRQ EL2h
	invalid_vect	el2h_fiq_invalid	// FIQ EL2h
	valid_vect	el2_error		// Error EL2h

	valid_vect	el1_sync		// Synchronous 64-bit EL1
	valid_vect	el1_irq			// IRQ 64-bit EL1
182
	valid_vect	el1_fiq			// FIQ 64-bit EL1
183 184 185 186
	valid_vect	el1_error		// Error 64-bit EL1

	valid_vect	el1_sync		// Synchronous 32-bit EL1
	valid_vect	el1_irq			// IRQ 32-bit EL1
187
	valid_vect	el1_fiq			// FIQ 32-bit EL1
188
	valid_vect	el1_error		// Error 32-bit EL1
189
SYM_CODE_END(__kvm_hyp_vector)
190

191 192 193 194 195 196 197 198 199 200 201 202
.macro spectrev2_smccc_wa1_smc
	sub	sp, sp, #(8 * 4)
	stp	x2, x3, [sp, #(8 * 0)]
	stp	x0, x1, [sp, #(8 * 2)]
	mov	w0, #ARM_SMCCC_ARCH_WORKAROUND_1
	smc	#0
	ldp	x2, x3, [sp, #(8 * 0)]
	add	sp, sp, #(8 * 2)
.endm

.macro hyp_ventry	indirect, spectrev2
	.align	7
203
1:	esb
204 205 206
	.if \spectrev2 != 0
	spectrev2_smccc_wa1_smc
	.else
207
	stp	x0, x1, [sp, #-16]!
208 209 210 211
	.endif
	.if \indirect != 0
	alternative_cb  kvm_patch_vector_branch
	/*
212
	 * For ARM64_SPECTRE_V3A configurations, these NOPs get replaced with:
213 214 215 216 217 218 219 220 221 222
	 *
	 * movz	x0, #(addr & 0xffff)
	 * movk	x0, #((addr >> 16) & 0xffff), lsl #16
	 * movk	x0, #((addr >> 32) & 0xffff), lsl #32
	 * br	x0
	 *
	 * Where:
	 * addr = kern_hyp_va(__kvm_hyp_vector) + vector-offset + KVM_VECTOR_PREAMBLE.
	 * See kvm_patch_vector_branch for details.
	 */
223 224 225
	nop
	nop
	nop
226 227 228 229
	nop
	alternative_cb_end
	.endif
	b	__kvm_hyp_vector + (1b - 0b + KVM_VECTOR_PREAMBLE)
230 231
.endm

232
.macro generate_vectors	indirect, spectrev2
233 234
0:
	.rept 16
235
	hyp_ventry	\indirect, \spectrev2
236 237 238 239 240
	.endr
	.org 0b + SZ_2K		// Safety measure
.endm

	.align	11
241
SYM_CODE_START(__bp_harden_hyp_vecs)
242 243 244
	generate_vectors indirect = 0, spectrev2 = 1 // HYP_VECTOR_SPECTRE_DIRECT
	generate_vectors indirect = 1, spectrev2 = 0 // HYP_VECTOR_INDIRECT
	generate_vectors indirect = 1, spectrev2 = 1 // HYP_VECTOR_SPECTRE_INDIRECT
245 246 247
1:	.org __bp_harden_hyp_vecs + __BP_HARDEN_HYP_VECS_SZ
	.org 1b
SYM_CODE_END(__bp_harden_hyp_vecs)