xref: /openbmc/linux/arch/arm64/kernel/entry.S (revision 7d7ae873b5e0f46d19e5dc818d1a7809e4b7cc81)
1caab277bSThomas Gleixner/* SPDX-License-Identifier: GPL-2.0-only */
260ffc30dSCatalin Marinas/*
360ffc30dSCatalin Marinas * Low-level exception handling code
460ffc30dSCatalin Marinas *
560ffc30dSCatalin Marinas * Copyright (C) 2012 ARM Ltd.
660ffc30dSCatalin Marinas * Authors:	Catalin Marinas <catalin.marinas@arm.com>
760ffc30dSCatalin Marinas *		Will Deacon <will.deacon@arm.com>
860ffc30dSCatalin Marinas */
960ffc30dSCatalin Marinas
108e290624SMarc Zyngier#include <linux/arm-smccc.h>
1160ffc30dSCatalin Marinas#include <linux/init.h>
1260ffc30dSCatalin Marinas#include <linux/linkage.h>
1360ffc30dSCatalin Marinas
148d883b23SMarc Zyngier#include <asm/alternative.h>
1560ffc30dSCatalin Marinas#include <asm/assembler.h>
1660ffc30dSCatalin Marinas#include <asm/asm-offsets.h>
17be129842SKristina Martsenko#include <asm/asm_pointer_auth.h>
185f1f7f6cSWill Deacon#include <asm/bug.h>
19905e8c5dSWill Deacon#include <asm/cpufeature.h>
2060ffc30dSCatalin Marinas#include <asm/errno.h>
215c1ce6f7SMarc Zyngier#include <asm/esr.h>
228e23dacdSJames Morse#include <asm/irq.h>
23c7b9adafSWill Deacon#include <asm/memory.h>
24c7b9adafSWill Deacon#include <asm/mmu.h>
25eef94a3dSYury Norov#include <asm/processor.h>
2639bc88e5SCatalin Marinas#include <asm/ptrace.h>
275287569aSSami Tolvanen#include <asm/scs.h>
2860ffc30dSCatalin Marinas#include <asm/thread_info.h>
29b4b8664dSAl Viro#include <asm/asm-uaccess.h>
3060ffc30dSCatalin Marinas#include <asm/unistd.h>
3160ffc30dSCatalin Marinas
32baaa7237SMark Rutland	.macro	clear_gp_regs
33baaa7237SMark Rutland	.irp	n,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29
34baaa7237SMark Rutland	mov	x\n, xzr
35baaa7237SMark Rutland	.endr
36baaa7237SMark Rutland	.endm
37baaa7237SMark Rutland
38ec841aabSMark Rutland	.macro kernel_ventry, el:req, ht:req, regsize:req, label:req
39b11e5759SMark Rutland	.align 7
404330e2c5SJames Morse.Lventry_start\@:
414bf3286dSWill Deacon	.if	\el == 0
42d739da16SJames Morse	/*
43d739da16SJames Morse	 * This must be the first instruction of the EL0 vector entries. It is
44d739da16SJames Morse	 * skipped by the trampoline vectors, to trigger the cleanup.
45d739da16SJames Morse	 */
46d739da16SJames Morse	b	.Lskip_tramp_vectors_cleanup\@
474bf3286dSWill Deacon	.if	\regsize == 64
484bf3286dSWill Deacon	mrs	x30, tpidrro_el0
494bf3286dSWill Deacon	msr	tpidrro_el0, xzr
504bf3286dSWill Deacon	.else
514bf3286dSWill Deacon	mov	x30, xzr
524bf3286dSWill Deacon	.endif
53d739da16SJames Morse.Lskip_tramp_vectors_cleanup\@:
54108eae2dSJulien Thierry	.endif
554bf3286dSWill Deacon
5671e70184SJianlin Lv	sub	sp, sp, #PT_REGS_SIZE
57872d8327SMark Rutland#ifdef CONFIG_VMAP_STACK
58872d8327SMark Rutland	/*
59872d8327SMark Rutland	 * Test whether the SP has overflowed, without corrupting a GPR.
60de858040SHeyi Guo	 * Task and IRQ stacks are aligned so that SP & (1 << THREAD_SHIFT)
61de858040SHeyi Guo	 * should always be zero.
62872d8327SMark Rutland	 */
63872d8327SMark Rutland	add	sp, sp, x0			// sp' = sp + x0
64872d8327SMark Rutland	sub	x0, sp, x0			// x0' = sp' - x0 = (sp + x0) - x0 = sp
65872d8327SMark Rutland	tbnz	x0, #THREAD_SHIFT, 0f
66872d8327SMark Rutland	sub	x0, sp, x0			// x0'' = sp' - x0' = (sp + x0) - sp = x0
67872d8327SMark Rutland	sub	sp, sp, x0			// sp'' = sp' - x0 = (sp + x0) - x0 = sp
68ec841aabSMark Rutland	b	el\el\ht\()_\regsize\()_\label
69872d8327SMark Rutland
70872d8327SMark Rutland0:
71872d8327SMark Rutland	/*
72872d8327SMark Rutland	 * Either we've just detected an overflow, or we've taken an exception
73872d8327SMark Rutland	 * while on the overflow stack. Either way, we won't return to
74872d8327SMark Rutland	 * userspace, and can clobber EL0 registers to free up GPRs.
75872d8327SMark Rutland	 */
76872d8327SMark Rutland
7771e70184SJianlin Lv	/* Stash the original SP (minus PT_REGS_SIZE) in tpidr_el0. */
78872d8327SMark Rutland	msr	tpidr_el0, x0
79872d8327SMark Rutland
80872d8327SMark Rutland	/* Recover the original x0 value and stash it in tpidrro_el0 */
81872d8327SMark Rutland	sub	x0, sp, x0
82872d8327SMark Rutland	msr	tpidrro_el0, x0
83872d8327SMark Rutland
84872d8327SMark Rutland	/* Switch to the overflow stack */
85872d8327SMark Rutland	adr_this_cpu sp, overflow_stack + OVERFLOW_STACK_SIZE, x0
86872d8327SMark Rutland
87872d8327SMark Rutland	/*
88872d8327SMark Rutland	 * Check whether we were already on the overflow stack. This may happen
89872d8327SMark Rutland	 * after panic() re-enables interrupts.
90872d8327SMark Rutland	 */
91872d8327SMark Rutland	mrs	x0, tpidr_el0			// sp of interrupted context
92872d8327SMark Rutland	sub	x0, sp, x0			// delta with top of overflow stack
93872d8327SMark Rutland	tst	x0, #~(OVERFLOW_STACK_SIZE - 1)	// within range?
94872d8327SMark Rutland	b.ne	__bad_stack			// no? -> bad stack pointer
95872d8327SMark Rutland
96872d8327SMark Rutland	/* We were already on the overflow stack. Restore sp/x0 and carry on. */
97872d8327SMark Rutland	sub	sp, sp, x0
98872d8327SMark Rutland	mrs	x0, tpidrro_el0
99872d8327SMark Rutland#endif
100ec841aabSMark Rutland	b	el\el\ht\()_\regsize\()_\label
1014330e2c5SJames Morse.org .Lventry_start\@ + 128	// Did we overflow the ventry slot?
102b11e5759SMark Rutland	.endm
103b11e5759SMark Rutland
104211ceca3SArd Biesheuvel	.macro	tramp_alias, dst, sym
105211ceca3SArd Biesheuvel	.set	.Lalias\@, TRAMP_VALIAS + \sym - .entry.tramp.text
106211ceca3SArd Biesheuvel	movz	\dst, :abs_g2_s:.Lalias\@
107211ceca3SArd Biesheuvel	movk	\dst, :abs_g1_nc:.Lalias\@
108211ceca3SArd Biesheuvel	movk	\dst, :abs_g0_nc:.Lalias\@
109b11e5759SMark Rutland	.endm
110b11e5759SMark Rutland
1118c3001b9SWill Deacon	/*
1128c3001b9SWill Deacon	 * This macro corrupts x0-x3. It is the caller's duty  to save/restore
1138c3001b9SWill Deacon	 * them if required.
1148c3001b9SWill Deacon	 */
11599ed3ed0SMark Rutland	.macro	apply_ssbd, state, tmp1, tmp2
1164c0bd995SMark Rutlandalternative_cb	ARM64_ALWAYS_SYSTEM, spectre_v4_patch_fw_mitigation_enable
117c2876207SWill Deacon	b	.L__asm_ssbd_skip\@		// Patched to NOP
118986372c4SMarc Zyngieralternative_cb_end
1195cf9ce6eSMarc Zyngier	ldr_this_cpu	\tmp2, arm64_ssbd_callback_required, \tmp1
12099ed3ed0SMark Rutland	cbz	\tmp2,	.L__asm_ssbd_skip\@
1219dd9614fSMarc Zyngier	ldr	\tmp2, [tsk, #TSK_TI_FLAGS]
12299ed3ed0SMark Rutland	tbnz	\tmp2, #TIF_SSBD, .L__asm_ssbd_skip\@
1238e290624SMarc Zyngier	mov	w0, #ARM_SMCCC_ARCH_WORKAROUND_2
1248e290624SMarc Zyngier	mov	w1, #\state
1254c0bd995SMark Rutlandalternative_cb	ARM64_ALWAYS_SYSTEM, smccc_patch_fw_mitigation_conduit
1268e290624SMarc Zyngier	nop					// Patched to SMC/HVC #0
1278e290624SMarc Zyngieralternative_cb_end
12899ed3ed0SMark Rutland.L__asm_ssbd_skip\@:
1298e290624SMarc Zyngier	.endm
1308e290624SMarc Zyngier
131637ec831SVincenzo Frascino	/* Check for MTE asynchronous tag check faults */
13242b6b10aSPeter Collingbourne	.macro check_mte_async_tcf, tmp, ti_flags, thread_sctlr
133637ec831SVincenzo Frascino#ifdef CONFIG_ARM64_MTE
1342decad92SCatalin Marinas	.arch_extension lse
135637ec831SVincenzo Frascinoalternative_if_not ARM64_MTE
136637ec831SVincenzo Frascino	b	1f
137637ec831SVincenzo Frascinoalternative_else_nop_endif
13842b6b10aSPeter Collingbourne	/*
13942b6b10aSPeter Collingbourne	 * Asynchronous tag check faults are only possible in ASYNC (2) or
14042b6b10aSPeter Collingbourne	 * ASYM (3) modes. In each of these modes bit 1 of SCTLR_EL1.TCF0 is
14142b6b10aSPeter Collingbourne	 * set, so skip the check if it is unset.
14242b6b10aSPeter Collingbourne	 */
14342b6b10aSPeter Collingbourne	tbz	\thread_sctlr, #(SCTLR_EL1_TCF0_SHIFT + 1), 1f
144637ec831SVincenzo Frascino	mrs_s	\tmp, SYS_TFSRE0_EL1
145637ec831SVincenzo Frascino	tbz	\tmp, #SYS_TFSR_EL1_TF0_SHIFT, 1f
146637ec831SVincenzo Frascino	/* Asynchronous TCF occurred for TTBR0 access, set the TI flag */
1472decad92SCatalin Marinas	mov	\tmp, #_TIF_MTE_ASYNC_FAULT
1482decad92SCatalin Marinas	add	\ti_flags, tsk, #TSK_TI_FLAGS
1492decad92SCatalin Marinas	stset	\tmp, [\ti_flags]
150637ec831SVincenzo Frascino1:
151637ec831SVincenzo Frascino#endif
152637ec831SVincenzo Frascino	.endm
153637ec831SVincenzo Frascino
154637ec831SVincenzo Frascino	/* Clear the MTE asynchronous tag check faults */
15542b6b10aSPeter Collingbourne	.macro clear_mte_async_tcf thread_sctlr
156637ec831SVincenzo Frascino#ifdef CONFIG_ARM64_MTE
157637ec831SVincenzo Frascinoalternative_if ARM64_MTE
15842b6b10aSPeter Collingbourne	/* See comment in check_mte_async_tcf above. */
15942b6b10aSPeter Collingbourne	tbz	\thread_sctlr, #(SCTLR_EL1_TCF0_SHIFT + 1), 1f
160637ec831SVincenzo Frascino	dsb	ish
161637ec831SVincenzo Frascino	msr_s	SYS_TFSRE0_EL1, xzr
16242b6b10aSPeter Collingbourne1:
163637ec831SVincenzo Frascinoalternative_else_nop_endif
164637ec831SVincenzo Frascino#endif
165637ec831SVincenzo Frascino	.endm
166637ec831SVincenzo Frascino
167afdfd93aSPeter Collingbourne	.macro mte_set_gcr, mte_ctrl, tmp
168bad1e1c6SVincenzo Frascino#ifdef CONFIG_ARM64_MTE
169afdfd93aSPeter Collingbourne	ubfx	\tmp, \mte_ctrl, #MTE_CTRL_GCR_USER_EXCL_SHIFT, #16
170afdfd93aSPeter Collingbourne	orr	\tmp, \tmp, #SYS_GCR_EL1_RRND
171afdfd93aSPeter Collingbourne	msr_s	SYS_GCR_EL1, \tmp
172bad1e1c6SVincenzo Frascino#endif
173bad1e1c6SVincenzo Frascino	.endm
174bad1e1c6SVincenzo Frascino
175bad1e1c6SVincenzo Frascino	.macro mte_set_kernel_gcr, tmp, tmp2
176bad1e1c6SVincenzo Frascino#ifdef CONFIG_KASAN_HW_TAGS
1774c0bd995SMark Rutlandalternative_cb	ARM64_ALWAYS_SYSTEM, kasan_hw_tags_enable
178bad1e1c6SVincenzo Frascino	b	1f
179e5af50a5SPeter Collingbournealternative_cb_end
18082868247SMark Rutland	mov	\tmp, KERNEL_GCR_EL1
18182868247SMark Rutland	msr_s	SYS_GCR_EL1, \tmp
182bad1e1c6SVincenzo Frascino1:
183bad1e1c6SVincenzo Frascino#endif
184bad1e1c6SVincenzo Frascino	.endm
185bad1e1c6SVincenzo Frascino
186bad1e1c6SVincenzo Frascino	.macro mte_set_user_gcr, tsk, tmp, tmp2
187e5af50a5SPeter Collingbourne#ifdef CONFIG_KASAN_HW_TAGS
1884c0bd995SMark Rutlandalternative_cb	ARM64_ALWAYS_SYSTEM, kasan_hw_tags_enable
189bad1e1c6SVincenzo Frascino	b	1f
190e5af50a5SPeter Collingbournealternative_cb_end
191638982a0SPeter Collingbourne	ldr	\tmp, [\tsk, #THREAD_MTE_CTRL]
192bad1e1c6SVincenzo Frascino
193bad1e1c6SVincenzo Frascino	mte_set_gcr \tmp, \tmp2
194bad1e1c6SVincenzo Frascino1:
195bad1e1c6SVincenzo Frascino#endif
196bad1e1c6SVincenzo Frascino	.endm
197bad1e1c6SVincenzo Frascino
198b11e5759SMark Rutland	.macro	kernel_entry, el, regsize = 64
19901ab991fSArd Biesheuvel	.if	\el == 0
20001ab991fSArd Biesheuvel	alternative_insn nop, SET_PSTATE_DIT(1), ARM64_HAS_DIT
20101ab991fSArd Biesheuvel	.endif
20260ffc30dSCatalin Marinas	.if	\regsize == 32
20360ffc30dSCatalin Marinas	mov	w0, w0				// zero upper 32 bits of x0
20460ffc30dSCatalin Marinas	.endif
20563648dd2SWill Deacon	stp	x0, x1, [sp, #16 * 0]
20663648dd2SWill Deacon	stp	x2, x3, [sp, #16 * 1]
20763648dd2SWill Deacon	stp	x4, x5, [sp, #16 * 2]
20863648dd2SWill Deacon	stp	x6, x7, [sp, #16 * 3]
20963648dd2SWill Deacon	stp	x8, x9, [sp, #16 * 4]
21063648dd2SWill Deacon	stp	x10, x11, [sp, #16 * 5]
21163648dd2SWill Deacon	stp	x12, x13, [sp, #16 * 6]
21263648dd2SWill Deacon	stp	x14, x15, [sp, #16 * 7]
21363648dd2SWill Deacon	stp	x16, x17, [sp, #16 * 8]
21463648dd2SWill Deacon	stp	x18, x19, [sp, #16 * 9]
21563648dd2SWill Deacon	stp	x20, x21, [sp, #16 * 10]
21663648dd2SWill Deacon	stp	x22, x23, [sp, #16 * 11]
21763648dd2SWill Deacon	stp	x24, x25, [sp, #16 * 12]
21863648dd2SWill Deacon	stp	x26, x27, [sp, #16 * 13]
21963648dd2SWill Deacon	stp	x28, x29, [sp, #16 * 14]
22063648dd2SWill Deacon
22160ffc30dSCatalin Marinas	.if	\el == 0
222baaa7237SMark Rutland	clear_gp_regs
22360ffc30dSCatalin Marinas	mrs	x21, sp_el0
2243e393417SMark Rutland	ldr_this_cpu	tsk, __entry_task, x20
2253e393417SMark Rutland	msr	sp_el0, tsk
2263e393417SMark Rutland
2278c3001b9SWill Deacon	/*
2288c3001b9SWill Deacon	 * Ensure MDSCR_EL1.SS is clear, since we can unmask debug exceptions
2298c3001b9SWill Deacon	 * when scheduling.
2308c3001b9SWill Deacon	 */
2313e393417SMark Rutland	ldr	x19, [tsk, #TSK_TI_FLAGS]
2323e393417SMark Rutland	disable_step_tsk x19, x20
23349003a8dSJames Morse
234637ec831SVincenzo Frascino	/* Check for asynchronous tag check faults in user space */
23542b6b10aSPeter Collingbourne	ldr	x0, [tsk, THREAD_SCTLR_USER]
23642b6b10aSPeter Collingbourne	check_mte_async_tcf x22, x23, x0
2378e290624SMarc Zyngier
23820169862SPeter Collingbourne#ifdef CONFIG_ARM64_PTR_AUTH
23920169862SPeter Collingbournealternative_if ARM64_HAS_ADDRESS_AUTH
24020169862SPeter Collingbourne	/*
24120169862SPeter Collingbourne	 * Enable IA for in-kernel PAC if the task had it disabled. Although
24220169862SPeter Collingbourne	 * this could be implemented with an unconditional MRS which would avoid
24320169862SPeter Collingbourne	 * a load, this was measured to be slower on Cortex-A75 and Cortex-A76.
244b90e4839SPeter Collingbourne	 *
245b90e4839SPeter Collingbourne	 * Install the kernel IA key only if IA was enabled in the task. If IA
246b90e4839SPeter Collingbourne	 * was disabled on kernel exit then we would have left the kernel IA
247b90e4839SPeter Collingbourne	 * installed so there is no need to install it again.
24820169862SPeter Collingbourne	 */
249b90e4839SPeter Collingbourne	tbz	x0, SCTLR_ELx_ENIA_SHIFT, 1f
250b90e4839SPeter Collingbourne	__ptrauth_keys_install_kernel_nosync tsk, x20, x22, x23
251b90e4839SPeter Collingbourne	b	2f
252b90e4839SPeter Collingbourne1:
25320169862SPeter Collingbourne	mrs	x0, sctlr_el1
25420169862SPeter Collingbourne	orr	x0, x0, SCTLR_ELx_ENIA
25520169862SPeter Collingbourne	msr	sctlr_el1, x0
256b90e4839SPeter Collingbourne2:
25720169862SPeter Collingbournealternative_else_nop_endif
25820169862SPeter Collingbourne#endif
2595287569aSSami Tolvanen
26042b6b10aSPeter Collingbourne	apply_ssbd 1, x22, x23
26142b6b10aSPeter Collingbourne
262bad1e1c6SVincenzo Frascino	mte_set_kernel_gcr x22, x23
263bad1e1c6SVincenzo Frascino
264d914b80aSPeter Collingbourne	/*
265d914b80aSPeter Collingbourne	 * Any non-self-synchronizing system register updates required for
266d914b80aSPeter Collingbourne	 * kernel entry should be placed before this point.
267d914b80aSPeter Collingbourne	 */
268d914b80aSPeter Collingbournealternative_if ARM64_MTE
269d914b80aSPeter Collingbourne	isb
270d914b80aSPeter Collingbourne	b	1f
271d914b80aSPeter Collingbournealternative_else_nop_endif
272d914b80aSPeter Collingbournealternative_if ARM64_HAS_ADDRESS_AUTH
273d914b80aSPeter Collingbourne	isb
274d914b80aSPeter Collingbournealternative_else_nop_endif
275d914b80aSPeter Collingbourne1:
276d914b80aSPeter Collingbourne
2772198d07cSArd Biesheuvel	scs_load_current
27860ffc30dSCatalin Marinas	.else
27971e70184SJianlin Lv	add	x21, sp, #PT_REGS_SIZE
2804caf8758SJulien Thierry	get_current_task tsk
281e19a6ee2SJames Morse	.endif /* \el == 0 */
28260ffc30dSCatalin Marinas	mrs	x22, elr_el1
28360ffc30dSCatalin Marinas	mrs	x23, spsr_el1
28460ffc30dSCatalin Marinas	stp	lr, x21, [sp, #S_LR]
28539bc88e5SCatalin Marinas
28673267498SArd Biesheuvel	/*
2877d7b720aSMadhavan T. Venkataraman	 * For exceptions from EL0, create a final frame record.
2886106e111SMark Rutland	 * For exceptions from EL1, create a synthetic frame record so the
2896106e111SMark Rutland	 * interrupted code shows up in the backtrace.
29073267498SArd Biesheuvel	 */
29173267498SArd Biesheuvel	.if \el == 0
2928533d5bfSMark Rutland	stp	xzr, xzr, [sp, #S_STACKFRAME]
29373267498SArd Biesheuvel	.else
29473267498SArd Biesheuvel	stp	x29, x22, [sp, #S_STACKFRAME]
2956106e111SMark Rutland	.endif
2968533d5bfSMark Rutland	add	x29, sp, #S_STACKFRAME
29773267498SArd Biesheuvel
29839bc88e5SCatalin Marinas#ifdef CONFIG_ARM64_SW_TTBR0_PAN
2990ae3b13aSArd Biesheuvelalternative_if_not ARM64_HAS_PAN
3000ae3b13aSArd Biesheuvel	bl	__swpan_entry_el\el
30139bc88e5SCatalin Marinasalternative_else_nop_endif
30239bc88e5SCatalin Marinas#endif
30339bc88e5SCatalin Marinas
30460ffc30dSCatalin Marinas	stp	x22, x23, [sp, #S_PC]
30560ffc30dSCatalin Marinas
30617c28958SDave Martin	/* Not in a syscall by default (el0_svc overwrites for real syscall) */
30760ffc30dSCatalin Marinas	.if	\el == 0
30817c28958SDave Martin	mov	w21, #NO_SYSCALL
30935d0e6fbSDave Martin	str	w21, [sp, #S_SYSCALLNO]
31060ffc30dSCatalin Marinas	.endif
31160ffc30dSCatalin Marinas
3123352a555SHe Ying#ifdef CONFIG_ARM64_PSEUDO_NMI
3138bf0a804SMark Rutlandalternative_if_not ARM64_HAS_GIC_PRIO_MASKING
3148bf0a804SMark Rutland	b	.Lskip_pmr_save\@
3158bf0a804SMark Rutlandalternative_else_nop_endif
3168bf0a804SMark Rutland
317133d0518SJulien Thierry	mrs_s	x20, SYS_ICC_PMR_EL1
318133d0518SJulien Thierry	str	x20, [sp, #S_PMR_SAVE]
3194d6a38daSMark Rutland	mov	x20, #GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET
3204d6a38daSMark Rutland	msr_s	SYS_ICC_PMR_EL1, x20
3218bf0a804SMark Rutland
3228bf0a804SMark Rutland.Lskip_pmr_save\@:
3233352a555SHe Ying#endif
324133d0518SJulien Thierry
3256cdf9c7cSJungseok Lee	/*
32660ffc30dSCatalin Marinas	 * Registers that may be useful after this macro is invoked:
32760ffc30dSCatalin Marinas	 *
328bd82d4bdSJulien Thierry	 * x20 - ICC_PMR_EL1
32960ffc30dSCatalin Marinas	 * x21 - aborted SP
33060ffc30dSCatalin Marinas	 * x22 - aborted PC
33160ffc30dSCatalin Marinas	 * x23 - aborted PSTATE
33260ffc30dSCatalin Marinas	*/
33360ffc30dSCatalin Marinas	.endm
33460ffc30dSCatalin Marinas
335412fcb6cSWill Deacon	.macro	kernel_exit, el
336e19a6ee2SJames Morse	.if	\el != 0
3378d66772eSJames Morse	disable_daif
338e19a6ee2SJames Morse	.endif
339e19a6ee2SJames Morse
3403352a555SHe Ying#ifdef CONFIG_ARM64_PSEUDO_NMI
3418bf0a804SMark Rutlandalternative_if_not ARM64_HAS_GIC_PRIO_MASKING
3428bf0a804SMark Rutland	b	.Lskip_pmr_restore\@
3438bf0a804SMark Rutlandalternative_else_nop_endif
3448bf0a804SMark Rutland
345133d0518SJulien Thierry	ldr	x20, [sp, #S_PMR_SAVE]
346133d0518SJulien Thierry	msr_s	SYS_ICC_PMR_EL1, x20
3478bf0a804SMark Rutland
3488bf0a804SMark Rutland	/* Ensure priority change is seen by redistributor */
3498bf0a804SMark Rutlandalternative_if_not ARM64_HAS_GIC_PRIO_RELAXED_SYNC
3508bf0a804SMark Rutland	dsb	sy
351133d0518SJulien Thierryalternative_else_nop_endif
3528bf0a804SMark Rutland
3538bf0a804SMark Rutland.Lskip_pmr_restore\@:
3543352a555SHe Ying#endif
355133d0518SJulien Thierry
35660ffc30dSCatalin Marinas	ldp	x21, x22, [sp, #S_PC]		// load ELR, SPSR
35739bc88e5SCatalin Marinas
35839bc88e5SCatalin Marinas#ifdef CONFIG_ARM64_SW_TTBR0_PAN
3590ae3b13aSArd Biesheuvelalternative_if_not ARM64_HAS_PAN
3600ae3b13aSArd Biesheuvel	bl	__swpan_exit_el\el
36139bc88e5SCatalin Marinasalternative_else_nop_endif
36239bc88e5SCatalin Marinas#endif
36339bc88e5SCatalin Marinas
36439bc88e5SCatalin Marinas	.if	\el == 0
36560ffc30dSCatalin Marinas	ldr	x23, [sp, #S_SP]		// load return stack pointer
36660ffc30dSCatalin Marinas	msr	sp_el0, x23
3674bf3286dSWill Deacon	tst	x22, #PSR_MODE32_BIT		// native task?
3684bf3286dSWill Deacon	b.eq	3f
3694bf3286dSWill Deacon
370905e8c5dSWill Deacon#ifdef CONFIG_ARM64_ERRATUM_845719
3716ba3b554SMark Rutlandalternative_if ARM64_WORKAROUND_845719
372e28cabf1SDaniel Thompson#ifdef CONFIG_PID_IN_CONTEXTIDR
373e28cabf1SDaniel Thompson	mrs	x29, contextidr_el1
374e28cabf1SDaniel Thompson	msr	contextidr_el1, x29
375e28cabf1SDaniel Thompson#else
376e28cabf1SDaniel Thompson	msr contextidr_el1, xzr
377e28cabf1SDaniel Thompson#endif
3786ba3b554SMark Rutlandalternative_else_nop_endif
379905e8c5dSWill Deacon#endif
3804bf3286dSWill Deacon3:
38116c230b3SWill Deacon	scs_save tsk
3825287569aSSami Tolvanen
38342b6b10aSPeter Collingbourne	/* Ignore asynchronous tag check faults in the uaccess routines */
38442b6b10aSPeter Collingbourne	ldr	x0, [tsk, THREAD_SCTLR_USER]
38542b6b10aSPeter Collingbourne	clear_mte_async_tcf x0
38642b6b10aSPeter Collingbourne
38720169862SPeter Collingbourne#ifdef CONFIG_ARM64_PTR_AUTH
38820169862SPeter Collingbournealternative_if ARM64_HAS_ADDRESS_AUTH
38920169862SPeter Collingbourne	/*
390b90e4839SPeter Collingbourne	 * IA was enabled for in-kernel PAC. Disable it now if needed, or
391b90e4839SPeter Collingbourne	 * alternatively install the user's IA. All other per-task keys and
392b90e4839SPeter Collingbourne	 * SCTLR bits were updated on task switch.
393b90e4839SPeter Collingbourne	 *
394b90e4839SPeter Collingbourne	 * No kernel C function calls after this.
39520169862SPeter Collingbourne	 */
396b90e4839SPeter Collingbourne	tbz	x0, SCTLR_ELx_ENIA_SHIFT, 1f
397b90e4839SPeter Collingbourne	__ptrauth_keys_install_user tsk, x0, x1, x2
398b90e4839SPeter Collingbourne	b	2f
399b90e4839SPeter Collingbourne1:
40020169862SPeter Collingbourne	mrs	x0, sctlr_el1
40120169862SPeter Collingbourne	bic	x0, x0, SCTLR_ELx_ENIA
40220169862SPeter Collingbourne	msr	sctlr_el1, x0
403b90e4839SPeter Collingbourne2:
40420169862SPeter Collingbournealternative_else_nop_endif
40520169862SPeter Collingbourne#endif
406be129842SKristina Martsenko
407bad1e1c6SVincenzo Frascino	mte_set_user_gcr tsk, x0, x1
408bad1e1c6SVincenzo Frascino
40999ed3ed0SMark Rutland	apply_ssbd 0, x0, x1
41060ffc30dSCatalin Marinas	.endif
41139bc88e5SCatalin Marinas
41263648dd2SWill Deacon	msr	elr_el1, x21			// set up the return data
41363648dd2SWill Deacon	msr	spsr_el1, x22
41463648dd2SWill Deacon	ldp	x0, x1, [sp, #16 * 0]
41563648dd2SWill Deacon	ldp	x2, x3, [sp, #16 * 1]
41663648dd2SWill Deacon	ldp	x4, x5, [sp, #16 * 2]
41763648dd2SWill Deacon	ldp	x6, x7, [sp, #16 * 3]
41863648dd2SWill Deacon	ldp	x8, x9, [sp, #16 * 4]
41963648dd2SWill Deacon	ldp	x10, x11, [sp, #16 * 5]
42063648dd2SWill Deacon	ldp	x12, x13, [sp, #16 * 6]
42163648dd2SWill Deacon	ldp	x14, x15, [sp, #16 * 7]
42263648dd2SWill Deacon	ldp	x16, x17, [sp, #16 * 8]
42363648dd2SWill Deacon	ldp	x18, x19, [sp, #16 * 9]
42463648dd2SWill Deacon	ldp	x20, x21, [sp, #16 * 10]
42563648dd2SWill Deacon	ldp	x22, x23, [sp, #16 * 11]
42663648dd2SWill Deacon	ldp	x24, x25, [sp, #16 * 12]
42763648dd2SWill Deacon	ldp	x26, x27, [sp, #16 * 13]
42863648dd2SWill Deacon	ldp	x28, x29, [sp, #16 * 14]
4294bf3286dSWill Deacon
4304bf3286dSWill Deacon	.if	\el == 0
431ea1e3de8SWill Deacon#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
432*58eb5c07SMark Rutland	alternative_insn "b .L_skip_tramp_exit_\@", nop, ARM64_UNMAP_KERNEL_AT_EL0
433*58eb5c07SMark Rutland
43403aff3a7SJames Morse	msr	far_el1, x29
435211ceca3SArd Biesheuvel
436211ceca3SArd Biesheuvel	ldr_this_cpu	x30, this_cpu_vector, x29
437211ceca3SArd Biesheuvel	tramp_alias	x29, tramp_exit
438211ceca3SArd Biesheuvel	msr		vbar_el1, x30		// install vector table
439211ceca3SArd Biesheuvel	ldr		lr, [sp, #S_LR]		// restore x30
440211ceca3SArd Biesheuvel	add		sp, sp, #PT_REGS_SIZE	// restore sp
441211ceca3SArd Biesheuvel	br		x29
442*58eb5c07SMark Rutland
443*58eb5c07SMark Rutland.L_skip_tramp_exit_\@:
444ea1e3de8SWill Deacon#endif
445*58eb5c07SMark Rutland	ldr	lr, [sp, #S_LR]
446*58eb5c07SMark Rutland	add	sp, sp, #PT_REGS_SIZE		// restore sp
447*58eb5c07SMark Rutland
448*58eb5c07SMark Rutland	/* This must be after the last explicit memory access */
449*58eb5c07SMark Rutlandalternative_if ARM64_WORKAROUND_SPECULATIVE_UNPRIV_LOAD
450*58eb5c07SMark Rutland	tlbi	vale1, xzr
451*58eb5c07SMark Rutland	dsb	nsh
452*58eb5c07SMark Rutlandalternative_else_nop_endif
453*58eb5c07SMark Rutland	eret
4544bf3286dSWill Deacon	.else
45503aff3a7SJames Morse	ldr	lr, [sp, #S_LR]
45603aff3a7SJames Morse	add	sp, sp, #PT_REGS_SIZE		// restore sp
45703aff3a7SJames Morse
45896d389caSRob Herring	/* Ensure any device/NC reads complete */
45996d389caSRob Herring	alternative_insn nop, "dmb sy", ARM64_WORKAROUND_1508412
46096d389caSRob Herring
4614bf3286dSWill Deacon	eret
4624bf3286dSWill Deacon	.endif
463679db708SWill Deacon	sb
46460ffc30dSCatalin Marinas	.endm
46560ffc30dSCatalin Marinas
4660ae3b13aSArd Biesheuvel#ifdef CONFIG_ARM64_SW_TTBR0_PAN
4670ae3b13aSArd Biesheuvel	/*
4680ae3b13aSArd Biesheuvel	 * Set the TTBR0 PAN bit in SPSR. When the exception is taken from
4690ae3b13aSArd Biesheuvel	 * EL0, there is no need to check the state of TTBR0_EL1 since
4700ae3b13aSArd Biesheuvel	 * accesses are always enabled.
4710ae3b13aSArd Biesheuvel	 * Note that the meaning of this bit differs from the ARMv8.1 PAN
4720ae3b13aSArd Biesheuvel	 * feature as all TTBR0_EL1 accesses are disabled, not just those to
4730ae3b13aSArd Biesheuvel	 * user mappings.
4740ae3b13aSArd Biesheuvel	 */
4750ae3b13aSArd BiesheuvelSYM_CODE_START_LOCAL(__swpan_entry_el1)
4760ae3b13aSArd Biesheuvel	mrs	x21, ttbr0_el1
4770ae3b13aSArd Biesheuvel	tst	x21, #TTBR_ASID_MASK		// Check for the reserved ASID
4780ae3b13aSArd Biesheuvel	orr	x23, x23, #PSR_PAN_BIT		// Set the emulated PAN in the saved SPSR
4790ae3b13aSArd Biesheuvel	b.eq	1f				// TTBR0 access already disabled
4800ae3b13aSArd Biesheuvel	and	x23, x23, #~PSR_PAN_BIT		// Clear the emulated PAN in the saved SPSR
4810ae3b13aSArd BiesheuvelSYM_INNER_LABEL(__swpan_entry_el0, SYM_L_LOCAL)
4820ae3b13aSArd Biesheuvel	__uaccess_ttbr0_disable x21
4830ae3b13aSArd Biesheuvel1:	ret
4840ae3b13aSArd BiesheuvelSYM_CODE_END(__swpan_entry_el1)
4850ae3b13aSArd Biesheuvel
4860ae3b13aSArd Biesheuvel	/*
4870ae3b13aSArd Biesheuvel	 * Restore access to TTBR0_EL1. If returning to EL0, no need for SPSR
4880ae3b13aSArd Biesheuvel	 * PAN bit checking.
4890ae3b13aSArd Biesheuvel	 */
4900ae3b13aSArd BiesheuvelSYM_CODE_START_LOCAL(__swpan_exit_el1)
4910ae3b13aSArd Biesheuvel	tbnz	x22, #22, 1f			// Skip re-enabling TTBR0 access if the PSR_PAN_BIT is set
4920ae3b13aSArd Biesheuvel	__uaccess_ttbr0_enable x0, x1
4930ae3b13aSArd Biesheuvel1:	and	x22, x22, #~PSR_PAN_BIT		// ARMv8.0 CPUs do not understand this bit
4940ae3b13aSArd Biesheuvel	ret
4950ae3b13aSArd BiesheuvelSYM_CODE_END(__swpan_exit_el1)
4960ae3b13aSArd Biesheuvel
4970ae3b13aSArd BiesheuvelSYM_CODE_START_LOCAL(__swpan_exit_el0)
4980ae3b13aSArd Biesheuvel	__uaccess_ttbr0_enable x0, x1
4990ae3b13aSArd Biesheuvel	/*
5000ae3b13aSArd Biesheuvel	 * Enable errata workarounds only if returning to user. The only
5010ae3b13aSArd Biesheuvel	 * workaround currently required for TTBR0_EL1 changes are for the
5020ae3b13aSArd Biesheuvel	 * Cavium erratum 27456 (broadcast TLBI instructions may cause I-cache
5030ae3b13aSArd Biesheuvel	 * corruption).
5040ae3b13aSArd Biesheuvel	 */
5050ae3b13aSArd Biesheuvel	b	post_ttbr_update_workaround
5060ae3b13aSArd BiesheuvelSYM_CODE_END(__swpan_exit_el0)
5070ae3b13aSArd Biesheuvel#endif
5080ae3b13aSArd Biesheuvel
5098c2c596fSMark Rutland/* GPRs used by entry code */
51060ffc30dSCatalin Marinastsk	.req	x28		// current thread_info
51160ffc30dSCatalin Marinas
51260ffc30dSCatalin Marinas	.text
51360ffc30dSCatalin Marinas
51460ffc30dSCatalin Marinas/*
51560ffc30dSCatalin Marinas * Exception vectors.
51660ffc30dSCatalin Marinas */
517888b3c87SPratyush Anand	.pushsection ".entry.text", "ax"
51860ffc30dSCatalin Marinas
51960ffc30dSCatalin Marinas	.align	11
5200ccbd98aSMark BrownSYM_CODE_START(vectors)
521ec841aabSMark Rutland	kernel_ventry	1, t, 64, sync		// Synchronous EL1t
522ec841aabSMark Rutland	kernel_ventry	1, t, 64, irq		// IRQ EL1t
523729a9165SKuan-Ying Lee	kernel_ventry	1, t, 64, fiq		// FIQ EL1t
524ec841aabSMark Rutland	kernel_ventry	1, t, 64, error		// Error EL1t
52560ffc30dSCatalin Marinas
526ec841aabSMark Rutland	kernel_ventry	1, h, 64, sync		// Synchronous EL1h
527ec841aabSMark Rutland	kernel_ventry	1, h, 64, irq		// IRQ EL1h
528ec841aabSMark Rutland	kernel_ventry	1, h, 64, fiq		// FIQ EL1h
529ec841aabSMark Rutland	kernel_ventry	1, h, 64, error		// Error EL1h
53060ffc30dSCatalin Marinas
531ec841aabSMark Rutland	kernel_ventry	0, t, 64, sync		// Synchronous 64-bit EL0
532ec841aabSMark Rutland	kernel_ventry	0, t, 64, irq		// IRQ 64-bit EL0
533ec841aabSMark Rutland	kernel_ventry	0, t, 64, fiq		// FIQ 64-bit EL0
534ec841aabSMark Rutland	kernel_ventry	0, t, 64, error		// Error 64-bit EL0
53560ffc30dSCatalin Marinas
536ec841aabSMark Rutland	kernel_ventry	0, t, 32, sync		// Synchronous 32-bit EL0
537ec841aabSMark Rutland	kernel_ventry	0, t, 32, irq		// IRQ 32-bit EL0
538ec841aabSMark Rutland	kernel_ventry	0, t, 32, fiq		// FIQ 32-bit EL0
539ec841aabSMark Rutland	kernel_ventry	0, t, 32, error		// Error 32-bit EL0
5400ccbd98aSMark BrownSYM_CODE_END(vectors)
54160ffc30dSCatalin Marinas
542872d8327SMark Rutland#ifdef CONFIG_VMAP_STACK
543ede3241aSMark BrownSYM_CODE_START_LOCAL(__bad_stack)
544872d8327SMark Rutland	/*
545872d8327SMark Rutland	 * We detected an overflow in kernel_ventry, which switched to the
546872d8327SMark Rutland	 * overflow stack. Stash the exception regs, and head to our overflow
547872d8327SMark Rutland	 * handler.
548872d8327SMark Rutland	 */
549ede3241aSMark Brown
550872d8327SMark Rutland	/* Restore the original x0 value */
551872d8327SMark Rutland	mrs	x0, tpidrro_el0
552872d8327SMark Rutland
553872d8327SMark Rutland	/*
554872d8327SMark Rutland	 * Store the original GPRs to the new stack. The orginal SP (minus
55571e70184SJianlin Lv	 * PT_REGS_SIZE) was stashed in tpidr_el0 by kernel_ventry.
556872d8327SMark Rutland	 */
55771e70184SJianlin Lv	sub	sp, sp, #PT_REGS_SIZE
558872d8327SMark Rutland	kernel_entry 1
559872d8327SMark Rutland	mrs	x0, tpidr_el0
56071e70184SJianlin Lv	add	x0, x0, #PT_REGS_SIZE
561872d8327SMark Rutland	str	x0, [sp, #S_SP]
562872d8327SMark Rutland
563872d8327SMark Rutland	/* Stash the regs for handle_bad_stack */
564872d8327SMark Rutland	mov	x0, sp
565872d8327SMark Rutland
566872d8327SMark Rutland	/* Time to die */
567872d8327SMark Rutland	bl	handle_bad_stack
568872d8327SMark Rutland	ASM_BUG()
569ede3241aSMark BrownSYM_CODE_END(__bad_stack)
570872d8327SMark Rutland#endif /* CONFIG_VMAP_STACK */
571872d8327SMark Rutland
572ec841aabSMark Rutland
573ec841aabSMark Rutland	.macro entry_handler el:req, ht:req, regsize:req, label:req
574ec841aabSMark RutlandSYM_CODE_START_LOCAL(el\el\ht\()_\regsize\()_\label)
575b660950cSArd Biesheuvel	kernel_entry \el, \regsize
57660ffc30dSCatalin Marinas	mov	x0, sp
577ec841aabSMark Rutland	bl	el\el\ht\()_\regsize\()_\label\()_handler
578a5b43a87SMark Rutland	.if \el == 0
579a5b43a87SMark Rutland	b	ret_to_user
580a5b43a87SMark Rutland	.else
581a5b43a87SMark Rutland	b	ret_to_kernel
582a5b43a87SMark Rutland	.endif
583ec841aabSMark RutlandSYM_CODE_END(el\el\ht\()_\regsize\()_\label)
58460ffc30dSCatalin Marinas	.endm
58560ffc30dSCatalin Marinas
58660ffc30dSCatalin Marinas/*
587a5b43a87SMark Rutland * Early exception handlers
58860ffc30dSCatalin Marinas */
589ec841aabSMark Rutland	entry_handler	1, t, 64, sync
590ec841aabSMark Rutland	entry_handler	1, t, 64, irq
591ec841aabSMark Rutland	entry_handler	1, t, 64, fiq
592ec841aabSMark Rutland	entry_handler	1, t, 64, error
59360ffc30dSCatalin Marinas
594ec841aabSMark Rutland	entry_handler	1, h, 64, sync
595ec841aabSMark Rutland	entry_handler	1, h, 64, irq
596ec841aabSMark Rutland	entry_handler	1, h, 64, fiq
597ec841aabSMark Rutland	entry_handler	1, h, 64, error
59860ffc30dSCatalin Marinas
599ec841aabSMark Rutland	entry_handler	0, t, 64, sync
600ec841aabSMark Rutland	entry_handler	0, t, 64, irq
601ec841aabSMark Rutland	entry_handler	0, t, 64, fiq
602ec841aabSMark Rutland	entry_handler	0, t, 64, error
603ec841aabSMark Rutland
604ec841aabSMark Rutland	entry_handler	0, t, 32, sync
605ec841aabSMark Rutland	entry_handler	0, t, 32, irq
606ec841aabSMark Rutland	entry_handler	0, t, 32, fiq
607ec841aabSMark Rutland	entry_handler	0, t, 32, error
608a92d4d14SXie XiuQi
609af541cbbSMark RutlandSYM_CODE_START_LOCAL(ret_to_kernel)
61060ffc30dSCatalin Marinas	kernel_exit 1
611af541cbbSMark RutlandSYM_CODE_END(ret_to_kernel)
612a92d4d14SXie XiuQi
61306607c7eSMark BrownSYM_CODE_START_LOCAL(ret_to_user)
6144d1c2ee2SMark Rutland	ldr	x19, [tsk, #TSK_TI_FLAGS]	// re-check for single-step
6153cb5ed4dSMark Rutland	enable_step_tsk x19, x2
6160b3e3366SLaura Abbott#ifdef CONFIG_GCC_PLUGIN_STACKLEAK
61788959a39SMark Rutland	bl	stackleak_erase_on_task_stack
6180b3e3366SLaura Abbott#endif
619412fcb6cSWill Deacon	kernel_exit 0
62006607c7eSMark BrownSYM_CODE_END(ret_to_user)
62160ffc30dSCatalin Marinas
622888b3c87SPratyush Anand	.popsection				// .entry.text
623888b3c87SPratyush Anand
624833be850SMark Rutland	// Move from tramp_pg_dir to swapper_pg_dir
625c7b9adafSWill Deacon	.macro tramp_map_kernel, tmp
626c7b9adafSWill Deacon	mrs	\tmp, ttbr1_el1
6270188a894SJoey Gouly	add	\tmp, \tmp, #TRAMP_SWAPPER_OFFSET
628c7b9adafSWill Deacon	bic	\tmp, \tmp, #USER_ASID_FLAG
629c7b9adafSWill Deacon	msr	ttbr1_el1, \tmp
630d1777e68SWill Deacon#ifdef CONFIG_QCOM_FALKOR_ERRATUM_1003
631d1777e68SWill Deaconalternative_if ARM64_WORKAROUND_QCOM_FALKOR_E1003
632d1777e68SWill Deacon	/* ASID already in \tmp[63:48] */
633d1777e68SWill Deacon	movk	\tmp, #:abs_g2_nc:(TRAMP_VALIAS >> 12)
634d1777e68SWill Deacon	movk	\tmp, #:abs_g1_nc:(TRAMP_VALIAS >> 12)
635d1777e68SWill Deacon	/* 2MB boundary containing the vectors, so we nobble the walk cache */
636d1777e68SWill Deacon	movk	\tmp, #:abs_g0_nc:((TRAMP_VALIAS & ~(SZ_2M - 1)) >> 12)
637d1777e68SWill Deacon	isb
638d1777e68SWill Deacon	tlbi	vae1, \tmp
639d1777e68SWill Deacon	dsb	nsh
640d1777e68SWill Deaconalternative_else_nop_endif
641d1777e68SWill Deacon#endif /* CONFIG_QCOM_FALKOR_ERRATUM_1003 */
642c7b9adafSWill Deacon	.endm
643c7b9adafSWill Deacon
644833be850SMark Rutland	// Move from swapper_pg_dir to tramp_pg_dir
645c7b9adafSWill Deacon	.macro tramp_unmap_kernel, tmp
646c7b9adafSWill Deacon	mrs	\tmp, ttbr1_el1
6470188a894SJoey Gouly	sub	\tmp, \tmp, #TRAMP_SWAPPER_OFFSET
648c7b9adafSWill Deacon	orr	\tmp, \tmp, #USER_ASID_FLAG
649c7b9adafSWill Deacon	msr	ttbr1_el1, \tmp
650c7b9adafSWill Deacon	/*
651f167211aSWill Deacon	 * We avoid running the post_ttbr_update_workaround here because
652f167211aSWill Deacon	 * it's only needed by Cavium ThunderX, which requires KPTI to be
653f167211aSWill Deacon	 * disabled.
654c7b9adafSWill Deacon	 */
655c7b9adafSWill Deacon	.endm
656c7b9adafSWill Deacon
657b28a8eebSJames Morse	.macro		tramp_data_read_var	dst, var
6581c9a8e87SArd Biesheuvel#ifdef CONFIG_RELOCATABLE
6591c9a8e87SArd Biesheuvel	ldr		\dst, .L__tramp_data_\var
6601c9a8e87SArd Biesheuvel	.ifndef		.L__tramp_data_\var
6611c9a8e87SArd Biesheuvel	.pushsection	".entry.tramp.rodata", "a", %progbits
6621c9a8e87SArd Biesheuvel	.align		3
6631c9a8e87SArd Biesheuvel.L__tramp_data_\var:
6641c9a8e87SArd Biesheuvel	.quad		\var
6651c9a8e87SArd Biesheuvel	.popsection
6661c9a8e87SArd Biesheuvel	.endif
667b28a8eebSJames Morse#else
6681c9a8e87SArd Biesheuvel	/*
6691c9a8e87SArd Biesheuvel	 * As !RELOCATABLE implies !RANDOMIZE_BASE the address is always a
6701c9a8e87SArd Biesheuvel	 * compile time constant (and hence not secret and not worth hiding).
6711c9a8e87SArd Biesheuvel	 *
6721c9a8e87SArd Biesheuvel	 * As statically allocated kernel code and data always live in the top
6731c9a8e87SArd Biesheuvel	 * 47 bits of the address space we can sign-extend bit 47 and avoid an
6741c9a8e87SArd Biesheuvel	 * instruction to load the upper 16 bits (which must be 0xFFFF).
6751c9a8e87SArd Biesheuvel	 */
6761c9a8e87SArd Biesheuvel	movz		\dst, :abs_g2_s:\var
6771c9a8e87SArd Biesheuvel	movk		\dst, :abs_g1_nc:\var
6781c9a8e87SArd Biesheuvel	movk		\dst, :abs_g0_nc:\var
679b28a8eebSJames Morse#endif
680b28a8eebSJames Morse	.endm
681ba268923SJames Morse
682ba268923SJames Morse#define BHB_MITIGATION_NONE	0
683ba268923SJames Morse#define BHB_MITIGATION_LOOP	1
684ba268923SJames Morse#define BHB_MITIGATION_FW	2
685228a26b9SJames Morse#define BHB_MITIGATION_INSN	3
686ba268923SJames Morse
687ba268923SJames Morse	.macro tramp_ventry, vector_start, regsize, kpti, bhb
688c7b9adafSWill Deacon	.align	7
689c7b9adafSWill Deacon1:
690c7b9adafSWill Deacon	.if	\regsize == 64
691c7b9adafSWill Deacon	msr	tpidrro_el0, x30	// Restored in kernel_ventry
692c7b9adafSWill Deacon	.endif
693aff65393SJames Morse
694ba268923SJames Morse	.if	\bhb == BHB_MITIGATION_LOOP
695ba268923SJames Morse	/*
696ba268923SJames Morse	 * This sequence must appear before the first indirect branch. i.e. the
697ba268923SJames Morse	 * ret out of tramp_ventry. It appears here because x30 is free.
698ba268923SJames Morse	 */
699ba268923SJames Morse	__mitigate_spectre_bhb_loop	x30
700ba268923SJames Morse	.endif // \bhb == BHB_MITIGATION_LOOP
701ba268923SJames Morse
702228a26b9SJames Morse	.if	\bhb == BHB_MITIGATION_INSN
703228a26b9SJames Morse	clearbhb
704228a26b9SJames Morse	isb
705228a26b9SJames Morse	.endif // \bhb == BHB_MITIGATION_INSN
706228a26b9SJames Morse
707aff65393SJames Morse	.if	\kpti == 1
708be04a6d1SWill Deacon	/*
709be04a6d1SWill Deacon	 * Defend against branch aliasing attacks by pushing a dummy
710be04a6d1SWill Deacon	 * entry onto the return stack and using a RET instruction to
711be04a6d1SWill Deacon	 * enter the full-fat kernel vectors.
712be04a6d1SWill Deacon	 */
713be04a6d1SWill Deacon	bl	2f
714be04a6d1SWill Deacon	b	.
715be04a6d1SWill Deacon2:
716c7b9adafSWill Deacon	tramp_map_kernel	x30
7176c27c408SWill Deaconalternative_insn isb, nop, ARM64_WORKAROUND_QCOM_FALKOR_E1003
718b28a8eebSJames Morse	tramp_data_read_var	x30, vectors
7199405447eSMarc Zyngieralternative_if_not ARM64_WORKAROUND_CAVIUM_TX2_219_PRFM
720ed50da77SJames Morse	prfm	plil1strm, [x30, #(1b - \vector_start)]
7219405447eSMarc Zyngieralternative_else_nop_endif
722c47e4d04SJames Morse
723c7b9adafSWill Deacon	msr	vbar_el1, x30
724c7b9adafSWill Deacon	isb
725c47e4d04SJames Morse	.else
7261c9a8e87SArd Biesheuvel	adr_l	x30, vectors
727c47e4d04SJames Morse	.endif // \kpti == 1
728c47e4d04SJames Morse
729ba268923SJames Morse	.if	\bhb == BHB_MITIGATION_FW
730ba268923SJames Morse	/*
731ba268923SJames Morse	 * The firmware sequence must appear before the first indirect branch.
732ba268923SJames Morse	 * i.e. the ret out of tramp_ventry. But it also needs the stack to be
733ba268923SJames Morse	 * mapped to save/restore the registers the SMC clobbers.
734ba268923SJames Morse	 */
735ba268923SJames Morse	__mitigate_spectre_bhb_fw
736ba268923SJames Morse	.endif // \bhb == BHB_MITIGATION_FW
737ba268923SJames Morse
738c47e4d04SJames Morse	add	x30, x30, #(1b - \vector_start + 4)
739be04a6d1SWill Deacon	ret
7404330e2c5SJames Morse.org 1b + 128	// Did we overflow the ventry slot?
741c7b9adafSWill Deacon	.endm
742c7b9adafSWill Deacon
743ba268923SJames Morse	.macro	generate_tramp_vector,	kpti, bhb
744ed50da77SJames Morse.Lvector_start\@:
745c7b9adafSWill Deacon	.space	0x400
746c7b9adafSWill Deacon
747ed50da77SJames Morse	.rept	4
748ba268923SJames Morse	tramp_ventry	.Lvector_start\@, 64, \kpti, \bhb
749ed50da77SJames Morse	.endr
750ed50da77SJames Morse	.rept	4
751ba268923SJames Morse	tramp_ventry	.Lvector_start\@, 32, \kpti, \bhb
752ed50da77SJames Morse	.endr
753ed50da77SJames Morse	.endm
754c7b9adafSWill Deacon
75513d7a083SJames Morse#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
75613d7a083SJames Morse/*
75713d7a083SJames Morse * Exception vectors trampoline.
758ba268923SJames Morse * The order must match __bp_harden_el1_vectors and the
759ba268923SJames Morse * arm64_bp_harden_el1_vectors enum.
76013d7a083SJames Morse */
76113d7a083SJames Morse	.pushsection ".entry.tramp.text", "ax"
762ed50da77SJames Morse	.align	11
763211ceca3SArd BiesheuvelSYM_CODE_START_LOCAL_NOALIGN(tramp_vectors)
764ba268923SJames Morse#ifdef CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY
765ba268923SJames Morse	generate_tramp_vector	kpti=1, bhb=BHB_MITIGATION_LOOP
766ba268923SJames Morse	generate_tramp_vector	kpti=1, bhb=BHB_MITIGATION_FW
767228a26b9SJames Morse	generate_tramp_vector	kpti=1, bhb=BHB_MITIGATION_INSN
768ba268923SJames Morse#endif /* CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY */
769ba268923SJames Morse	generate_tramp_vector	kpti=1, bhb=BHB_MITIGATION_NONE
770e7bf6972SMark BrownSYM_CODE_END(tramp_vectors)
771c7b9adafSWill Deacon
772211ceca3SArd BiesheuvelSYM_CODE_START_LOCAL(tramp_exit)
773211ceca3SArd Biesheuvel	tramp_unmap_kernel	x29
774211ceca3SArd Biesheuvel	mrs		x29, far_el1		// restore x29
775211ceca3SArd Biesheuvel	eret
776211ceca3SArd Biesheuvel	sb
777211ceca3SArd BiesheuvelSYM_CODE_END(tramp_exit)
778c7b9adafSWill Deacon	.popsection				// .entry.tramp.text
779c7b9adafSWill Deacon#endif /* CONFIG_UNMAP_KERNEL_AT_EL0 */
780c7b9adafSWill Deacon
78160ffc30dSCatalin Marinas/*
782aff65393SJames Morse * Exception vectors for spectre mitigations on entry from EL1 when
783aff65393SJames Morse * kpti is not in use.
784aff65393SJames Morse */
785ba268923SJames Morse	.macro generate_el1_vector, bhb
786aff65393SJames Morse.Lvector_start\@:
787aff65393SJames Morse	kernel_ventry	1, t, 64, sync		// Synchronous EL1t
788aff65393SJames Morse	kernel_ventry	1, t, 64, irq		// IRQ EL1t
789aff65393SJames Morse	kernel_ventry	1, t, 64, fiq		// FIQ EL1h
790aff65393SJames Morse	kernel_ventry	1, t, 64, error		// Error EL1t
791aff65393SJames Morse
792aff65393SJames Morse	kernel_ventry	1, h, 64, sync		// Synchronous EL1h
793aff65393SJames Morse	kernel_ventry	1, h, 64, irq		// IRQ EL1h
794aff65393SJames Morse	kernel_ventry	1, h, 64, fiq		// FIQ EL1h
795aff65393SJames Morse	kernel_ventry	1, h, 64, error		// Error EL1h
796aff65393SJames Morse
797aff65393SJames Morse	.rept	4
798ba268923SJames Morse	tramp_ventry	.Lvector_start\@, 64, 0, \bhb
799aff65393SJames Morse	.endr
800aff65393SJames Morse	.rept 4
801ba268923SJames Morse	tramp_ventry	.Lvector_start\@, 32, 0, \bhb
802aff65393SJames Morse	.endr
803aff65393SJames Morse	.endm
804aff65393SJames Morse
805ba268923SJames Morse/* The order must match tramp_vecs and the arm64_bp_harden_el1_vectors enum. */
806aff65393SJames Morse	.pushsection ".entry.text", "ax"
807aff65393SJames Morse	.align	11
808aff65393SJames MorseSYM_CODE_START(__bp_harden_el1_vectors)
809ba268923SJames Morse#ifdef CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY
810ba268923SJames Morse	generate_el1_vector	bhb=BHB_MITIGATION_LOOP
811ba268923SJames Morse	generate_el1_vector	bhb=BHB_MITIGATION_FW
812228a26b9SJames Morse	generate_el1_vector	bhb=BHB_MITIGATION_INSN
813ba268923SJames Morse#endif /* CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY */
814aff65393SJames MorseSYM_CODE_END(__bp_harden_el1_vectors)
815aff65393SJames Morse	.popsection
816aff65393SJames Morse
817aff65393SJames Morse
818aff65393SJames Morse/*
819ed84b4e9SMark Rutland * Register switch for AArch64. The callee-saved registers need to be saved
820ed84b4e9SMark Rutland * and restored. On entry:
821ed84b4e9SMark Rutland *   x0 = previous task_struct (must be preserved across the switch)
822ed84b4e9SMark Rutland *   x1 = next task_struct
823ed84b4e9SMark Rutland * Previous and next are guaranteed not to be the same.
824ed84b4e9SMark Rutland *
825ed84b4e9SMark Rutland */
826e7bf6972SMark BrownSYM_FUNC_START(cpu_switch_to)
827ed84b4e9SMark Rutland	mov	x10, #THREAD_CPU_CONTEXT
828ed84b4e9SMark Rutland	add	x8, x0, x10
829ed84b4e9SMark Rutland	mov	x9, sp
830ed84b4e9SMark Rutland	stp	x19, x20, [x8], #16		// store callee-saved registers
831ed84b4e9SMark Rutland	stp	x21, x22, [x8], #16
832ed84b4e9SMark Rutland	stp	x23, x24, [x8], #16
833ed84b4e9SMark Rutland	stp	x25, x26, [x8], #16
834ed84b4e9SMark Rutland	stp	x27, x28, [x8], #16
835ed84b4e9SMark Rutland	stp	x29, x9, [x8], #16
836ed84b4e9SMark Rutland	str	lr, [x8]
837ed84b4e9SMark Rutland	add	x8, x1, x10
838ed84b4e9SMark Rutland	ldp	x19, x20, [x8], #16		// restore callee-saved registers
839ed84b4e9SMark Rutland	ldp	x21, x22, [x8], #16
840ed84b4e9SMark Rutland	ldp	x23, x24, [x8], #16
841ed84b4e9SMark Rutland	ldp	x25, x26, [x8], #16
842ed84b4e9SMark Rutland	ldp	x27, x28, [x8], #16
843ed84b4e9SMark Rutland	ldp	x29, x9, [x8], #16
844ed84b4e9SMark Rutland	ldr	lr, [x8]
845ed84b4e9SMark Rutland	mov	sp, x9
846ed84b4e9SMark Rutland	msr	sp_el0, x1
847d0055da5SMark Rutland	ptrauth_keys_install_kernel x1, x8, x9, x10
84816c230b3SWill Deacon	scs_save x0
8492198d07cSArd Biesheuvel	scs_load_current
850ed84b4e9SMark Rutland	ret
851e7bf6972SMark BrownSYM_FUNC_END(cpu_switch_to)
852ed84b4e9SMark RutlandNOKPROBE(cpu_switch_to)
853ed84b4e9SMark Rutland
854ed84b4e9SMark Rutland/*
855ed84b4e9SMark Rutland * This is how we return from a fork.
856ed84b4e9SMark Rutland */
857c3357fc5SMark BrownSYM_CODE_START(ret_from_fork)
858ed84b4e9SMark Rutland	bl	schedule_tail
859ed84b4e9SMark Rutland	cbz	x19, 1f				// not a kernel thread
860ed84b4e9SMark Rutland	mov	x0, x20
861ed84b4e9SMark Rutland	blr	x19
8624caf8758SJulien Thierry1:	get_current_task tsk
863e130338eSMark Rutland	mov	x0, sp
864e130338eSMark Rutland	bl	asm_exit_to_user_mode
865ed84b4e9SMark Rutland	b	ret_to_user
866c3357fc5SMark BrownSYM_CODE_END(ret_from_fork)
867ed84b4e9SMark RutlandNOKPROBE(ret_from_fork)
868f5df2696SJames Morse
869f8049488SMark Rutland/*
870f8049488SMark Rutland * void call_on_irq_stack(struct pt_regs *regs,
871f8049488SMark Rutland * 		          void (*func)(struct pt_regs *));
872f8049488SMark Rutland *
873f8049488SMark Rutland * Calls func(regs) using this CPU's irq stack and shadow irq stack.
874f8049488SMark Rutland */
875f8049488SMark RutlandSYM_FUNC_START(call_on_irq_stack)
876f8049488SMark Rutland#ifdef CONFIG_SHADOW_CALL_STACK
87759b37fe5SArd Biesheuvel	get_current_task x16
87859b37fe5SArd Biesheuvel	scs_save x16
879f8049488SMark Rutland	ldr_this_cpu scs_sp, irq_shadow_call_stack_ptr, x17
880f8049488SMark Rutland#endif
88159b37fe5SArd Biesheuvel
882f8049488SMark Rutland	/* Create a frame record to save our LR and SP (implicit in FP) */
883f8049488SMark Rutland	stp	x29, x30, [sp, #-16]!
884f8049488SMark Rutland	mov	x29, sp
885f8049488SMark Rutland
886f8049488SMark Rutland	ldr_this_cpu x16, irq_stack_ptr, x17
887f8049488SMark Rutland
888f8049488SMark Rutland	/* Move to the new stack and call the function there */
88959b37fe5SArd Biesheuvel	add	sp, x16, #IRQ_STACK_SIZE
890f8049488SMark Rutland	blr	x1
891f8049488SMark Rutland
892f8049488SMark Rutland	/*
893f8049488SMark Rutland	 * Restore the SP from the FP, and restore the FP and LR from the frame
894f8049488SMark Rutland	 * record.
895f8049488SMark Rutland	 */
896f8049488SMark Rutland	mov	sp, x29
897f8049488SMark Rutland	ldp	x29, x30, [sp], #16
89859b37fe5SArd Biesheuvel	scs_load_current
899f8049488SMark Rutland	ret
900f8049488SMark RutlandSYM_FUNC_END(call_on_irq_stack)
901f8049488SMark RutlandNOKPROBE(call_on_irq_stack)
902f8049488SMark Rutland
903f5df2696SJames Morse#ifdef CONFIG_ARM_SDE_INTERFACE
904f5df2696SJames Morse
905f5df2696SJames Morse#include <asm/sdei.h>
906f5df2696SJames Morse#include <uapi/linux/arm_sdei.h>
907f5df2696SJames Morse
90879e9aa59SJames Morse.macro sdei_handler_exit exit_mode
90979e9aa59SJames Morse	/* On success, this call never returns... */
91079e9aa59SJames Morse	cmp	\exit_mode, #SDEI_EXIT_SMC
91179e9aa59SJames Morse	b.ne	99f
91279e9aa59SJames Morse	smc	#0
91379e9aa59SJames Morse	b	.
91479e9aa59SJames Morse99:	hvc	#0
91579e9aa59SJames Morse	b	.
91679e9aa59SJames Morse.endm
91779e9aa59SJames Morse
91879e9aa59SJames Morse#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
91979e9aa59SJames Morse/*
92079e9aa59SJames Morse * The regular SDEI entry point may have been unmapped along with the rest of
92179e9aa59SJames Morse * the kernel. This trampoline restores the kernel mapping to make the x1 memory
92279e9aa59SJames Morse * argument accessible.
92379e9aa59SJames Morse *
92479e9aa59SJames Morse * This clobbers x4, __sdei_handler() will restore this from firmware's
92579e9aa59SJames Morse * copy.
92679e9aa59SJames Morse */
92779e9aa59SJames Morse.pushsection ".entry.tramp.text", "ax"
9281242b9b3SMark BrownSYM_CODE_START(__sdei_asm_entry_trampoline)
92979e9aa59SJames Morse	mrs	x4, ttbr1_el1
93079e9aa59SJames Morse	tbz	x4, #USER_ASID_BIT, 1f
93179e9aa59SJames Morse
93279e9aa59SJames Morse	tramp_map_kernel tmp=x4
93379e9aa59SJames Morse	isb
93479e9aa59SJames Morse	mov	x4, xzr
93579e9aa59SJames Morse
93679e9aa59SJames Morse	/*
9373d2403fdSMark Rutland	 * Remember whether to unmap the kernel on exit.
93879e9aa59SJames Morse	 */
9393d2403fdSMark Rutland1:	str	x4, [x1, #(SDEI_EVENT_INTREGS + S_SDEI_TTBR1)]
940b28a8eebSJames Morse	tramp_data_read_var     x4, __sdei_asm_handler
94179e9aa59SJames Morse	br	x4
9421242b9b3SMark BrownSYM_CODE_END(__sdei_asm_entry_trampoline)
94379e9aa59SJames MorseNOKPROBE(__sdei_asm_entry_trampoline)
94479e9aa59SJames Morse
94579e9aa59SJames Morse/*
94679e9aa59SJames Morse * Make the exit call and restore the original ttbr1_el1
94779e9aa59SJames Morse *
94879e9aa59SJames Morse * x0 & x1: setup for the exit API call
94979e9aa59SJames Morse * x2: exit_mode
95079e9aa59SJames Morse * x4: struct sdei_registered_event argument from registration time.
95179e9aa59SJames Morse */
9521242b9b3SMark BrownSYM_CODE_START(__sdei_asm_exit_trampoline)
9533d2403fdSMark Rutland	ldr	x4, [x4, #(SDEI_EVENT_INTREGS + S_SDEI_TTBR1)]
95479e9aa59SJames Morse	cbnz	x4, 1f
95579e9aa59SJames Morse
95679e9aa59SJames Morse	tramp_unmap_kernel	tmp=x4
95779e9aa59SJames Morse
95879e9aa59SJames Morse1:	sdei_handler_exit exit_mode=x2
9591242b9b3SMark BrownSYM_CODE_END(__sdei_asm_exit_trampoline)
96079e9aa59SJames MorseNOKPROBE(__sdei_asm_exit_trampoline)
96179e9aa59SJames Morse.popsection		// .entry.tramp.text
96279e9aa59SJames Morse#endif /* CONFIG_UNMAP_KERNEL_AT_EL0 */
96379e9aa59SJames Morse
964f5df2696SJames Morse/*
965f5df2696SJames Morse * Software Delegated Exception entry point.
966f5df2696SJames Morse *
967f5df2696SJames Morse * x0: Event number
968f5df2696SJames Morse * x1: struct sdei_registered_event argument from registration time.
969f5df2696SJames Morse * x2: interrupted PC
970f5df2696SJames Morse * x3: interrupted PSTATE
97179e9aa59SJames Morse * x4: maybe clobbered by the trampoline
972f5df2696SJames Morse *
973f5df2696SJames Morse * Firmware has preserved x0->x17 for us, we must save/restore the rest to
974f5df2696SJames Morse * follow SMC-CC. We save (or retrieve) all the registers as the handler may
975f5df2696SJames Morse * want them.
976f5df2696SJames Morse */
9771242b9b3SMark BrownSYM_CODE_START(__sdei_asm_handler)
978f5df2696SJames Morse	stp     x2, x3, [x1, #SDEI_EVENT_INTREGS + S_PC]
979f5df2696SJames Morse	stp     x4, x5, [x1, #SDEI_EVENT_INTREGS + 16 * 2]
980f5df2696SJames Morse	stp     x6, x7, [x1, #SDEI_EVENT_INTREGS + 16 * 3]
981f5df2696SJames Morse	stp     x8, x9, [x1, #SDEI_EVENT_INTREGS + 16 * 4]
982f5df2696SJames Morse	stp     x10, x11, [x1, #SDEI_EVENT_INTREGS + 16 * 5]
983f5df2696SJames Morse	stp     x12, x13, [x1, #SDEI_EVENT_INTREGS + 16 * 6]
984f5df2696SJames Morse	stp     x14, x15, [x1, #SDEI_EVENT_INTREGS + 16 * 7]
985f5df2696SJames Morse	stp     x16, x17, [x1, #SDEI_EVENT_INTREGS + 16 * 8]
986f5df2696SJames Morse	stp     x18, x19, [x1, #SDEI_EVENT_INTREGS + 16 * 9]
987f5df2696SJames Morse	stp     x20, x21, [x1, #SDEI_EVENT_INTREGS + 16 * 10]
988f5df2696SJames Morse	stp     x22, x23, [x1, #SDEI_EVENT_INTREGS + 16 * 11]
989f5df2696SJames Morse	stp     x24, x25, [x1, #SDEI_EVENT_INTREGS + 16 * 12]
990f5df2696SJames Morse	stp     x26, x27, [x1, #SDEI_EVENT_INTREGS + 16 * 13]
991f5df2696SJames Morse	stp     x28, x29, [x1, #SDEI_EVENT_INTREGS + 16 * 14]
992f5df2696SJames Morse	mov	x4, sp
993f5df2696SJames Morse	stp     lr, x4, [x1, #SDEI_EVENT_INTREGS + S_LR]
994f5df2696SJames Morse
995f5df2696SJames Morse	mov	x19, x1
996f5df2696SJames Morse
9975cd474e5SD Scott Phillips	/* Store the registered-event for crash_smp_send_stop() */
998439dc2a1SSami Tolvanen	ldrb	w4, [x19, #SDEI_EVENT_PRIORITY]
9995cd474e5SD Scott Phillips	cbnz	w4, 1f
10005cd474e5SD Scott Phillips	adr_this_cpu dst=x5, sym=sdei_active_normal_event, tmp=x6
10015cd474e5SD Scott Phillips	b	2f
10025cd474e5SD Scott Phillips1:	adr_this_cpu dst=x5, sym=sdei_active_critical_event, tmp=x6
10035cd474e5SD Scott Phillips2:	str	x19, [x5]
1004439dc2a1SSami Tolvanen
1005f5df2696SJames Morse#ifdef CONFIG_VMAP_STACK
1006f5df2696SJames Morse	/*
1007f5df2696SJames Morse	 * entry.S may have been using sp as a scratch register, find whether
1008f5df2696SJames Morse	 * this is a normal or critical event and switch to the appropriate
1009f5df2696SJames Morse	 * stack for this CPU.
1010f5df2696SJames Morse	 */
1011f5df2696SJames Morse	cbnz	w4, 1f
1012f5df2696SJames Morse	ldr_this_cpu dst=x5, sym=sdei_stack_normal_ptr, tmp=x6
1013f5df2696SJames Morse	b	2f
1014f5df2696SJames Morse1:	ldr_this_cpu dst=x5, sym=sdei_stack_critical_ptr, tmp=x6
1015f5df2696SJames Morse2:	mov	x6, #SDEI_STACK_SIZE
1016f5df2696SJames Morse	add	x5, x5, x6
1017f5df2696SJames Morse	mov	sp, x5
1018f5df2696SJames Morse#endif
1019f5df2696SJames Morse
1020439dc2a1SSami Tolvanen#ifdef CONFIG_SHADOW_CALL_STACK
1021439dc2a1SSami Tolvanen	/* Use a separate shadow call stack for normal and critical events */
1022439dc2a1SSami Tolvanen	cbnz	w4, 3f
1023ac20ffbbSSami Tolvanen	ldr_this_cpu dst=scs_sp, sym=sdei_shadow_call_stack_normal_ptr, tmp=x6
1024439dc2a1SSami Tolvanen	b	4f
1025ac20ffbbSSami Tolvanen3:	ldr_this_cpu dst=scs_sp, sym=sdei_shadow_call_stack_critical_ptr, tmp=x6
1026439dc2a1SSami Tolvanen4:
1027439dc2a1SSami Tolvanen#endif
1028439dc2a1SSami Tolvanen
1029f5df2696SJames Morse	/*
1030f5df2696SJames Morse	 * We may have interrupted userspace, or a guest, or exit-from or
1031f5df2696SJames Morse	 * return-to either of these. We can't trust sp_el0, restore it.
1032f5df2696SJames Morse	 */
1033f5df2696SJames Morse	mrs	x28, sp_el0
1034f5df2696SJames Morse	ldr_this_cpu	dst=x0, sym=__entry_task, tmp=x1
1035f5df2696SJames Morse	msr	sp_el0, x0
1036f5df2696SJames Morse
1037f5df2696SJames Morse	/* If we interrupted the kernel point to the previous stack/frame. */
1038f5df2696SJames Morse	and     x0, x3, #0xc
1039f5df2696SJames Morse	mrs     x1, CurrentEL
1040f5df2696SJames Morse	cmp     x0, x1
1041f5df2696SJames Morse	csel	x29, x29, xzr, eq	// fp, or zero
1042f5df2696SJames Morse	csel	x4, x2, xzr, eq		// elr, or zero
1043f5df2696SJames Morse
1044f5df2696SJames Morse	stp	x29, x4, [sp, #-16]!
1045f5df2696SJames Morse	mov	x29, sp
1046f5df2696SJames Morse
1047f5df2696SJames Morse	add	x0, x19, #SDEI_EVENT_INTREGS
1048f5df2696SJames Morse	mov	x1, x19
1049f5df2696SJames Morse	bl	__sdei_handler
1050f5df2696SJames Morse
1051f5df2696SJames Morse	msr	sp_el0, x28
1052f5df2696SJames Morse	/* restore regs >x17 that we clobbered */
105379e9aa59SJames Morse	mov	x4, x19         // keep x4 for __sdei_asm_exit_trampoline
105479e9aa59SJames Morse	ldp	x28, x29, [x4, #SDEI_EVENT_INTREGS + 16 * 14]
105579e9aa59SJames Morse	ldp	x18, x19, [x4, #SDEI_EVENT_INTREGS + 16 * 9]
105679e9aa59SJames Morse	ldp	lr, x1, [x4, #SDEI_EVENT_INTREGS + S_LR]
105779e9aa59SJames Morse	mov	sp, x1
1058f5df2696SJames Morse
1059f5df2696SJames Morse	mov	x1, x0			// address to complete_and_resume
1060c9f5ea08SFlorian Fainelli	/* x0 = (x0 <= SDEI_EV_FAILED) ?
1061c9f5ea08SFlorian Fainelli	 * EVENT_COMPLETE:EVENT_COMPLETE_AND_RESUME
1062c9f5ea08SFlorian Fainelli	 */
1063c9f5ea08SFlorian Fainelli	cmp	x0, #SDEI_EV_FAILED
1064f5df2696SJames Morse	mov_q	x2, SDEI_1_0_FN_SDEI_EVENT_COMPLETE
1065f5df2696SJames Morse	mov_q	x3, SDEI_1_0_FN_SDEI_EVENT_COMPLETE_AND_RESUME
1066f5df2696SJames Morse	csel	x0, x2, x3, ls
1067f5df2696SJames Morse
1068f5df2696SJames Morse	ldr_l	x2, sdei_exit_mode
106979e9aa59SJames Morse
10705cd474e5SD Scott Phillips	/* Clear the registered-event seen by crash_smp_send_stop() */
10715cd474e5SD Scott Phillips	ldrb	w3, [x4, #SDEI_EVENT_PRIORITY]
10725cd474e5SD Scott Phillips	cbnz	w3, 1f
10735cd474e5SD Scott Phillips	adr_this_cpu dst=x5, sym=sdei_active_normal_event, tmp=x6
10745cd474e5SD Scott Phillips	b	2f
10755cd474e5SD Scott Phillips1:	adr_this_cpu dst=x5, sym=sdei_active_critical_event, tmp=x6
10765cd474e5SD Scott Phillips2:	str	xzr, [x5]
10775cd474e5SD Scott Phillips
107879e9aa59SJames Morsealternative_if_not ARM64_UNMAP_KERNEL_AT_EL0
107979e9aa59SJames Morse	sdei_handler_exit exit_mode=x2
108079e9aa59SJames Morsealternative_else_nop_endif
108179e9aa59SJames Morse
108279e9aa59SJames Morse#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
1083211ceca3SArd Biesheuvel	tramp_alias	dst=x5, sym=__sdei_asm_exit_trampoline
108479e9aa59SJames Morse	br	x5
108579e9aa59SJames Morse#endif
10861242b9b3SMark BrownSYM_CODE_END(__sdei_asm_handler)
1087f5df2696SJames MorseNOKPROBE(__sdei_asm_handler)
10885cd474e5SD Scott Phillips
10895cd474e5SD Scott PhillipsSYM_CODE_START(__sdei_handler_abort)
10905cd474e5SD Scott Phillips	mov_q	x0, SDEI_1_0_FN_SDEI_EVENT_COMPLETE_AND_RESUME
10915cd474e5SD Scott Phillips	adr	x1, 1f
10925cd474e5SD Scott Phillips	ldr_l	x2, sdei_exit_mode
10935cd474e5SD Scott Phillips	sdei_handler_exit exit_mode=x2
10945cd474e5SD Scott Phillips	// exit the handler and jump to the next instruction.
10955cd474e5SD Scott Phillips	// Exit will stomp x0-x17, PSTATE, ELR_ELx, and SPSR_ELx.
10965cd474e5SD Scott Phillips1:	ret
10975cd474e5SD Scott PhillipsSYM_CODE_END(__sdei_handler_abort)
10985cd474e5SD Scott PhillipsNOKPROBE(__sdei_handler_abort)
1099f5df2696SJames Morse#endif /* CONFIG_ARM_SDE_INTERFACE */
1100