blob: ecebb48c7cf465115030e0617e8eb2b6801f59f1 [file] [log] [blame]
Catalin Marinas60ffc302012-03-05 11:49:27 +00001/*
2 * Low-level exception handling code
3 *
4 * Copyright (C) 2012 ARM Ltd.
5 * Authors: Catalin Marinas <catalin.marinas@arm.com>
6 * Will Deacon <will.deacon@arm.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
Marc Zyngierbe3316302018-07-20 10:56:22 +010021#include <linux/arm-smccc.h>
Catalin Marinas60ffc302012-03-05 11:49:27 +000022#include <linux/init.h>
23#include <linux/linkage.h>
24
Marc Zyngier8d883b22015-06-01 10:47:41 +010025#include <asm/alternative.h>
Catalin Marinas60ffc302012-03-05 11:49:27 +000026#include <asm/assembler.h>
27#include <asm/asm-offsets.h>
Will Deacon905e8c52015-03-23 19:07:02 +000028#include <asm/cpufeature.h>
Catalin Marinas60ffc302012-03-05 11:49:27 +000029#include <asm/errno.h>
Marc Zyngier5c1ce6f2013-04-08 17:17:03 +010030#include <asm/esr.h>
James Morse8e23dac2015-12-04 11:02:27 +000031#include <asm/irq.h>
James Morsee19a6ee2016-06-20 18:28:01 +010032#include <asm/memory.h>
Will Deacona329b062017-11-14 14:07:40 +000033#include <asm/mmu.h>
Mark Rutland27eeced2018-04-12 12:10:59 +010034#include <asm/processor.h>
Catalin Marinascfa93772016-09-02 14:54:03 +010035#include <asm/ptrace.h>
Catalin Marinas60ffc302012-03-05 11:49:27 +000036#include <asm/thread_info.h>
Catalin Marinascfa93772016-09-02 14:54:03 +010037#include <asm/uaccess.h>
Kristina Martsenko9e09d902017-06-06 20:14:10 +010038#include <asm/asm-uaccess.h>
Catalin Marinas60ffc302012-03-05 11:49:27 +000039#include <asm/unistd.h>
Will Deacon78a0cec2018-04-03 12:09:04 +010040#include <asm/kernel-pgtable.h>
Catalin Marinas60ffc302012-03-05 11:49:27 +000041
42/*
Larry Bassel6c81fe72014-05-30 12:34:15 -070043 * Context tracking subsystem. Used to instrument transitions
44 * between user and kernel mode.
45 */
46 .macro ct_user_exit, syscall = 0
47#ifdef CONFIG_CONTEXT_TRACKING
48 bl context_tracking_user_exit
49 .if \syscall == 1
50 /*
51 * Save/restore needed during syscalls. Restore syscall arguments from
52 * the values already saved on stack during kernel_entry.
53 */
54 ldp x0, x1, [sp]
55 ldp x2, x3, [sp, #S_X2]
56 ldp x4, x5, [sp, #S_X4]
57 ldp x6, x7, [sp, #S_X6]
58 .endif
59#endif
60 .endm
61
62 .macro ct_user_enter
63#ifdef CONFIG_CONTEXT_TRACKING
64 bl context_tracking_user_enter
65#endif
66 .endm
67
68/*
Catalin Marinas60ffc302012-03-05 11:49:27 +000069 * Bad Abort numbers
70 *-----------------
71 */
72#define BAD_SYNC 0
73#define BAD_IRQ 1
74#define BAD_FIQ 2
75#define BAD_ERROR 3
76
Will Deacon8fdbffb2017-11-14 14:20:21 +000077 .macro kernel_ventry, el, label, regsize = 64
Mark Rutland17d35922017-07-19 17:24:49 +010078 .align 7
Will Deaconc27a22582017-11-14 14:24:29 +000079#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
Will Deaconf79ff2d2017-11-14 14:38:19 +000080alternative_if ARM64_UNMAP_KERNEL_AT_EL0
Will Deaconc27a22582017-11-14 14:24:29 +000081 .if \el == 0
82 .if \regsize == 64
83 mrs x30, tpidrro_el0
84 msr tpidrro_el0, xzr
85 .else
86 mov x30, xzr
87 .endif
88 .endif
Will Deaconf79ff2d2017-11-14 14:38:19 +000089alternative_else_nop_endif
Will Deaconc27a22582017-11-14 14:24:29 +000090#endif
91
Will Deacon63648dd2014-09-29 12:26:41 +010092 sub sp, sp, #S_FRAME_SIZE
Will Deacon8fdbffb2017-11-14 14:20:21 +000093 b el\()\el\()_\label
Mark Rutland17d35922017-07-19 17:24:49 +010094 .endm
95
Will Deaconc27a22582017-11-14 14:24:29 +000096 .macro tramp_alias, dst, sym
97 mov_q \dst, TRAMP_VALIAS
98 add \dst, \dst, #(\sym - .entry.tramp.text)
99 .endm
100
Marc Zyngierbe3316302018-07-20 10:56:22 +0100101 // This macro corrupts x0-x3. It is the caller's duty
102 // to save/restore them if required.
Marc Zyngierd8174bd2018-07-20 10:56:23 +0100103 .macro apply_ssbd, state, targ, tmp1, tmp2
Marc Zyngierbe3316302018-07-20 10:56:22 +0100104#ifdef CONFIG_ARM64_SSBD
Marc Zyngier42f967d2018-07-20 10:56:27 +0100105alternative_cb arm64_enable_wa2_handling
106 b \targ
107alternative_cb_end
Marc Zyngierd8174bd2018-07-20 10:56:23 +0100108 ldr_this_cpu \tmp2, arm64_ssbd_callback_required, \tmp1
109 cbz \tmp2, \targ
Amit Pundir0137ea22018-08-01 10:20:28 +0530110 ldr \tmp2, [tsk, #TSK_TI_FLAGS]
Marc Zyngiercf14b892018-07-20 10:56:29 +0100111 tbnz \tmp2, #TIF_SSBD, \targ
Marc Zyngierbe3316302018-07-20 10:56:22 +0100112 mov w0, #ARM_SMCCC_ARCH_WORKAROUND_2
113 mov w1, #\state
114alternative_cb arm64_update_smccc_conduit
115 nop // Patched to SMC/HVC #0
116alternative_cb_end
117#endif
118 .endm
119
Mark Rutland17d35922017-07-19 17:24:49 +0100120 .macro kernel_entry, el, regsize = 64
Catalin Marinas60ffc302012-03-05 11:49:27 +0000121 .if \regsize == 32
122 mov w0, w0 // zero upper 32 bits of x0
123 .endif
Will Deacon63648dd2014-09-29 12:26:41 +0100124 stp x0, x1, [sp, #16 * 0]
125 stp x2, x3, [sp, #16 * 1]
126 stp x4, x5, [sp, #16 * 2]
127 stp x6, x7, [sp, #16 * 3]
128 stp x8, x9, [sp, #16 * 4]
129 stp x10, x11, [sp, #16 * 5]
130 stp x12, x13, [sp, #16 * 6]
131 stp x14, x15, [sp, #16 * 7]
132 stp x16, x17, [sp, #16 * 8]
133 stp x18, x19, [sp, #16 * 9]
134 stp x20, x21, [sp, #16 * 10]
135 stp x22, x23, [sp, #16 * 11]
136 stp x24, x25, [sp, #16 * 12]
137 stp x26, x27, [sp, #16 * 13]
138 stp x28, x29, [sp, #16 * 14]
139
Catalin Marinas60ffc302012-03-05 11:49:27 +0000140 .if \el == 0
141 mrs x21, sp_el0
Mark Rutland5b7e8f72016-11-03 20:23:13 +0000142 ldr_this_cpu tsk, __entry_task, x20 // Ensure MDSCR_EL1.SS is clear,
143 ldr x19, [tsk, #TSK_TI_FLAGS] // since we can unmask debug
Will Deacon2a283072014-04-29 19:04:06 +0100144 disable_step_tsk x19, x20 // exceptions when scheduling.
James Morse49003a82015-12-10 10:22:41 +0000145
Marc Zyngierd8174bd2018-07-20 10:56:23 +0100146 apply_ssbd 1, 1f, x22, x23
Marc Zyngierbe3316302018-07-20 10:56:22 +0100147
148#ifdef CONFIG_ARM64_SSBD
149 ldp x0, x1, [sp, #16 * 0]
150 ldp x2, x3, [sp, #16 * 1]
151#endif
Marc Zyngierd8174bd2018-07-20 10:56:23 +01001521:
Marc Zyngierbe3316302018-07-20 10:56:22 +0100153
James Morse49003a82015-12-10 10:22:41 +0000154 mov x29, xzr // fp pointed to user-space
Catalin Marinas60ffc302012-03-05 11:49:27 +0000155 .else
156 add x21, sp, #S_FRAME_SIZE
James Morsee19a6ee2016-06-20 18:28:01 +0100157 get_thread_info tsk
Mark Rutlandc9100862018-04-12 12:11:00 +0100158 /* Save the task's original addr_limit and set USER_DS */
Mark Rutland5b7e8f72016-11-03 20:23:13 +0000159 ldr x20, [tsk, #TSK_TI_ADDR_LIMIT]
James Morsee19a6ee2016-06-20 18:28:01 +0100160 str x20, [sp, #S_ORIG_ADDR_LIMIT]
Mark Rutlandc9100862018-04-12 12:11:00 +0100161 mov x20, #USER_DS
Mark Rutland5b7e8f72016-11-03 20:23:13 +0000162 str x20, [tsk, #TSK_TI_ADDR_LIMIT]
Vladimir Murzin563cada2016-09-01 14:35:59 +0100163 /* No need to reset PSTATE.UAO, hardware's already set it to 0 for us */
James Morsee19a6ee2016-06-20 18:28:01 +0100164 .endif /* \el == 0 */
Catalin Marinas60ffc302012-03-05 11:49:27 +0000165 mrs x22, elr_el1
166 mrs x23, spsr_el1
167 stp lr, x21, [sp, #S_LR]
Catalin Marinascfa93772016-09-02 14:54:03 +0100168
169#ifdef CONFIG_ARM64_SW_TTBR0_PAN
170 /*
171 * Set the TTBR0 PAN bit in SPSR. When the exception is taken from
172 * EL0, there is no need to check the state of TTBR0_EL1 since
173 * accesses are always enabled.
174 * Note that the meaning of this bit differs from the ARMv8.1 PAN
175 * feature as all TTBR0_EL1 accesses are disabled, not just those to
176 * user mappings.
177 */
178alternative_if ARM64_HAS_PAN
179 b 1f // skip TTBR0 PAN
180alternative_else_nop_endif
181
182 .if \el != 0
Catalin Marinas87883132018-01-10 13:18:30 +0000183 mrs x21, ttbr0_el1
Will Deacond7013ed2017-12-01 17:33:48 +0000184 tst x21, #TTBR_ASID_MASK // Check for the reserved ASID
Catalin Marinascfa93772016-09-02 14:54:03 +0100185 orr x23, x23, #PSR_PAN_BIT // Set the emulated PAN in the saved SPSR
186 b.eq 1f // TTBR0 access already disabled
187 and x23, x23, #~PSR_PAN_BIT // Clear the emulated PAN in the saved SPSR
188 .endif
189
190 __uaccess_ttbr0_disable x21
1911:
192#endif
193
Catalin Marinas60ffc302012-03-05 11:49:27 +0000194 stp x22, x23, [sp, #S_PC]
195
196 /*
197 * Set syscallno to -1 by default (overridden later if real syscall).
198 */
199 .if \el == 0
200 mvn x21, xzr
201 str x21, [sp, #S_SYSCALLNO]
202 .endif
203
204 /*
Jungseok Lee6cdf9c72015-12-04 11:02:25 +0000205 * Set sp_el0 to current thread_info.
206 */
207 .if \el == 0
208 msr sp_el0, tsk
209 .endif
210
211 /*
Catalin Marinas60ffc302012-03-05 11:49:27 +0000212 * Registers that may be useful after this macro is invoked:
213 *
214 * x21 - aborted SP
215 * x22 - aborted PC
216 * x23 - aborted PSTATE
217 */
218 .endm
219
Will Deacon412fcb62015-08-19 15:57:09 +0100220 .macro kernel_exit, el
James Morsee19a6ee2016-06-20 18:28:01 +0100221 .if \el != 0
222 /* Restore the task's original addr_limit. */
223 ldr x20, [sp, #S_ORIG_ADDR_LIMIT]
Mark Rutland5b7e8f72016-11-03 20:23:13 +0000224 str x20, [tsk, #TSK_TI_ADDR_LIMIT]
James Morsee19a6ee2016-06-20 18:28:01 +0100225
226 /* No need to restore UAO, it will be restored from SPSR_EL1 */
227 .endif
228
Catalin Marinas60ffc302012-03-05 11:49:27 +0000229 ldp x21, x22, [sp, #S_PC] // load ELR, SPSR
230 .if \el == 0
Larry Bassel6c81fe72014-05-30 12:34:15 -0700231 ct_user_enter
Catalin Marinascfa93772016-09-02 14:54:03 +0100232 .endif
233
234#ifdef CONFIG_ARM64_SW_TTBR0_PAN
235 /*
236 * Restore access to TTBR0_EL1. If returning to EL0, no need for SPSR
237 * PAN bit checking.
238 */
239alternative_if ARM64_HAS_PAN
240 b 2f // skip TTBR0 PAN
241alternative_else_nop_endif
242
243 .if \el != 0
244 tbnz x22, #22, 1f // Skip re-enabling TTBR0 access if the PSR_PAN_BIT is set
245 .endif
246
Will Deacon599c71f2017-08-10 13:58:16 +0100247 __uaccess_ttbr0_enable x0, x1
Catalin Marinascfa93772016-09-02 14:54:03 +0100248
249 .if \el == 0
250 /*
251 * Enable errata workarounds only if returning to user. The only
252 * workaround currently required for TTBR0_EL1 changes are for the
253 * Cavium erratum 27456 (broadcast TLBI instructions may cause I-cache
254 * corruption).
255 */
Marc Zyngier95bfec62018-01-02 18:19:39 +0000256 bl post_ttbr_update_workaround
Catalin Marinascfa93772016-09-02 14:54:03 +0100257 .endif
2581:
259 .if \el != 0
260 and x22, x22, #~PSR_PAN_BIT // ARMv8.0 CPUs do not understand this bit
261 .endif
2622:
263#endif
264
265 .if \el == 0
Catalin Marinas60ffc302012-03-05 11:49:27 +0000266 ldr x23, [sp, #S_SP] // load return stack pointer
Catalin Marinas60ffc302012-03-05 11:49:27 +0000267 msr sp_el0, x23
Will Deaconc27a22582017-11-14 14:24:29 +0000268 tst x22, #PSR_MODE32_BIT // native task?
269 b.eq 3f
270
Will Deacon905e8c52015-03-23 19:07:02 +0000271#ifdef CONFIG_ARM64_ERRATUM_845719
Mark Rutland6ba3b552016-09-07 11:07:09 +0100272alternative_if ARM64_WORKAROUND_845719
Daniel Thompsone28cabf2015-07-22 12:21:03 +0100273#ifdef CONFIG_PID_IN_CONTEXTIDR
274 mrs x29, contextidr_el1
275 msr contextidr_el1, x29
276#else
277 msr contextidr_el1, xzr
278#endif
Mark Rutland6ba3b552016-09-07 11:07:09 +0100279alternative_else_nop_endif
Will Deacon905e8c52015-03-23 19:07:02 +0000280#endif
Will Deaconc27a22582017-11-14 14:24:29 +00002813:
Marc Zyngierd8174bd2018-07-20 10:56:23 +0100282 apply_ssbd 0, 5f, x0, x1
2835:
Catalin Marinas60ffc302012-03-05 11:49:27 +0000284 .endif
Catalin Marinascfa93772016-09-02 14:54:03 +0100285
Will Deacon63648dd2014-09-29 12:26:41 +0100286 msr elr_el1, x21 // set up the return data
287 msr spsr_el1, x22
Will Deacon63648dd2014-09-29 12:26:41 +0100288 ldp x0, x1, [sp, #16 * 0]
Will Deacon63648dd2014-09-29 12:26:41 +0100289 ldp x2, x3, [sp, #16 * 1]
290 ldp x4, x5, [sp, #16 * 2]
291 ldp x6, x7, [sp, #16 * 3]
292 ldp x8, x9, [sp, #16 * 4]
293 ldp x10, x11, [sp, #16 * 5]
294 ldp x12, x13, [sp, #16 * 6]
295 ldp x14, x15, [sp, #16 * 7]
296 ldp x16, x17, [sp, #16 * 8]
297 ldp x18, x19, [sp, #16 * 9]
298 ldp x20, x21, [sp, #16 * 10]
299 ldp x22, x23, [sp, #16 * 11]
300 ldp x24, x25, [sp, #16 * 12]
301 ldp x26, x27, [sp, #16 * 13]
302 ldp x28, x29, [sp, #16 * 14]
303 ldr lr, [sp, #S_LR]
304 add sp, sp, #S_FRAME_SIZE // restore sp
Will Deaconc27a22582017-11-14 14:24:29 +0000305
Will Deaconc27a22582017-11-14 14:24:29 +0000306 .if \el == 0
Will Deaconf79ff2d2017-11-14 14:38:19 +0000307alternative_insn eret, nop, ARM64_UNMAP_KERNEL_AT_EL0
308#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
Will Deaconc27a22582017-11-14 14:24:29 +0000309 bne 4f
310 msr far_el1, x30
311 tramp_alias x30, tramp_exit_native
312 br x30
3134:
314 tramp_alias x30, tramp_exit_compat
315 br x30
Will Deaconf79ff2d2017-11-14 14:38:19 +0000316#endif
Will Deaconc27a22582017-11-14 14:24:29 +0000317 .else
318 eret
319 .endif
Catalin Marinas60ffc302012-03-05 11:49:27 +0000320 .endm
321
James Morse971c67c2015-12-15 11:21:25 +0000322 .macro irq_stack_entry
James Morse8e23dac2015-12-04 11:02:27 +0000323 mov x19, sp // preserve the original sp
324
James Morse8e23dac2015-12-04 11:02:27 +0000325 /*
Mark Rutland5b7e8f72016-11-03 20:23:13 +0000326 * Compare sp with the base of the task stack.
327 * If the top ~(THREAD_SIZE - 1) bits match, we are on a task stack,
328 * and should switch to the irq stack.
James Morse8e23dac2015-12-04 11:02:27 +0000329 */
Mark Rutland5b7e8f72016-11-03 20:23:13 +0000330 ldr x25, [tsk, TSK_STACK]
331 eor x25, x25, x19
332 and x25, x25, #~(THREAD_SIZE - 1)
333 cbnz x25, 9998f
James Morse8e23dac2015-12-04 11:02:27 +0000334
Mark Rutland8b6c9c92016-11-03 20:23:12 +0000335 adr_this_cpu x25, irq_stack, x26
James Morse8e23dac2015-12-04 11:02:27 +0000336 mov x26, #IRQ_STACK_START_SP
337 add x26, x25, x26
James Morsed224a692015-12-18 16:01:47 +0000338
339 /* switch to the irq stack */
James Morse8e23dac2015-12-04 11:02:27 +0000340 mov sp, x26
341
James Morse971c67c2015-12-15 11:21:25 +0000342 /*
343 * Add a dummy stack frame, this non-standard format is fixed up
344 * by unwind_frame()
345 */
346 stp x29, x19, [sp, #-16]!
James Morse8e23dac2015-12-04 11:02:27 +0000347 mov x29, sp
James Morse8e23dac2015-12-04 11:02:27 +0000348
3499998:
350 .endm
351
352 /*
353 * x19 should be preserved between irq_stack_entry and
354 * irq_stack_exit.
355 */
356 .macro irq_stack_exit
357 mov sp, x19
358 .endm
359
Catalin Marinas60ffc302012-03-05 11:49:27 +0000360/*
361 * These are the registers used in the syscall handler, and allow us to
362 * have in theory up to 7 arguments to a function - x0 to x6.
363 *
364 * x7 is reserved for the system call number in 32-bit mode.
365 */
366sc_nr .req x25 // number of system calls
367scno .req x26 // syscall number
368stbl .req x27 // syscall table pointer
369tsk .req x28 // current thread_info
370
371/*
372 * Interrupt handling.
373 */
374 .macro irq_handler
James Morse8e23dac2015-12-04 11:02:27 +0000375 ldr_l x1, handle_arch_irq
Catalin Marinas60ffc302012-03-05 11:49:27 +0000376 mov x0, sp
James Morse971c67c2015-12-15 11:21:25 +0000377 irq_stack_entry
Catalin Marinas60ffc302012-03-05 11:49:27 +0000378 blr x1
James Morse8e23dac2015-12-04 11:02:27 +0000379 irq_stack_exit
Catalin Marinas60ffc302012-03-05 11:49:27 +0000380 .endm
381
382 .text
383
384/*
385 * Exception vectors.
386 */
Pratyush Anand888b3c82016-07-08 12:35:50 -0400387 .pushsection ".entry.text", "ax"
Catalin Marinas60ffc302012-03-05 11:49:27 +0000388
389 .align 11
390ENTRY(vectors)
Will Deacon8fdbffb2017-11-14 14:20:21 +0000391 kernel_ventry 1, sync_invalid // Synchronous EL1t
392 kernel_ventry 1, irq_invalid // IRQ EL1t
393 kernel_ventry 1, fiq_invalid // FIQ EL1t
394 kernel_ventry 1, error_invalid // Error EL1t
Catalin Marinas60ffc302012-03-05 11:49:27 +0000395
Will Deacon8fdbffb2017-11-14 14:20:21 +0000396 kernel_ventry 1, sync // Synchronous EL1h
397 kernel_ventry 1, irq // IRQ EL1h
398 kernel_ventry 1, fiq_invalid // FIQ EL1h
399 kernel_ventry 1, error_invalid // Error EL1h
Catalin Marinas60ffc302012-03-05 11:49:27 +0000400
Will Deacon8fdbffb2017-11-14 14:20:21 +0000401 kernel_ventry 0, sync // Synchronous 64-bit EL0
402 kernel_ventry 0, irq // IRQ 64-bit EL0
403 kernel_ventry 0, fiq_invalid // FIQ 64-bit EL0
404 kernel_ventry 0, error_invalid // Error 64-bit EL0
Catalin Marinas60ffc302012-03-05 11:49:27 +0000405
406#ifdef CONFIG_COMPAT
Will Deacon8fdbffb2017-11-14 14:20:21 +0000407 kernel_ventry 0, sync_compat, 32 // Synchronous 32-bit EL0
408 kernel_ventry 0, irq_compat, 32 // IRQ 32-bit EL0
409 kernel_ventry 0, fiq_invalid_compat, 32 // FIQ 32-bit EL0
410 kernel_ventry 0, error_invalid_compat, 32 // Error 32-bit EL0
Catalin Marinas60ffc302012-03-05 11:49:27 +0000411#else
Will Deacon8fdbffb2017-11-14 14:20:21 +0000412 kernel_ventry 0, sync_invalid, 32 // Synchronous 32-bit EL0
413 kernel_ventry 0, irq_invalid, 32 // IRQ 32-bit EL0
414 kernel_ventry 0, fiq_invalid, 32 // FIQ 32-bit EL0
415 kernel_ventry 0, error_invalid, 32 // Error 32-bit EL0
Catalin Marinas60ffc302012-03-05 11:49:27 +0000416#endif
417END(vectors)
418
419/*
420 * Invalid mode handlers
421 */
422 .macro inv_entry, el, reason, regsize = 64
Ard Biesheuvelb6609502016-03-18 10:58:09 +0100423 kernel_entry \el, \regsize
Catalin Marinas60ffc302012-03-05 11:49:27 +0000424 mov x0, sp
425 mov x1, #\reason
426 mrs x2, esr_el1
427 b bad_mode
428 .endm
429
430el0_sync_invalid:
431 inv_entry 0, BAD_SYNC
432ENDPROC(el0_sync_invalid)
433
434el0_irq_invalid:
435 inv_entry 0, BAD_IRQ
436ENDPROC(el0_irq_invalid)
437
438el0_fiq_invalid:
439 inv_entry 0, BAD_FIQ
440ENDPROC(el0_fiq_invalid)
441
442el0_error_invalid:
443 inv_entry 0, BAD_ERROR
444ENDPROC(el0_error_invalid)
445
446#ifdef CONFIG_COMPAT
447el0_fiq_invalid_compat:
448 inv_entry 0, BAD_FIQ, 32
449ENDPROC(el0_fiq_invalid_compat)
450
451el0_error_invalid_compat:
452 inv_entry 0, BAD_ERROR, 32
453ENDPROC(el0_error_invalid_compat)
454#endif
455
456el1_sync_invalid:
457 inv_entry 1, BAD_SYNC
458ENDPROC(el1_sync_invalid)
459
460el1_irq_invalid:
461 inv_entry 1, BAD_IRQ
462ENDPROC(el1_irq_invalid)
463
464el1_fiq_invalid:
465 inv_entry 1, BAD_FIQ
466ENDPROC(el1_fiq_invalid)
467
468el1_error_invalid:
469 inv_entry 1, BAD_ERROR
470ENDPROC(el1_error_invalid)
471
472/*
473 * EL1 mode handlers.
474 */
475 .align 6
476el1_sync:
477 kernel_entry 1
478 mrs x1, esr_el1 // read the syndrome register
Mark Rutlandaed40e02014-11-24 12:31:40 +0000479 lsr x24, x1, #ESR_ELx_EC_SHIFT // exception class
480 cmp x24, #ESR_ELx_EC_DABT_CUR // data abort in EL1
Catalin Marinas60ffc302012-03-05 11:49:27 +0000481 b.eq el1_da
Laura Abbott9adeb8e2016-08-09 18:25:26 -0700482 cmp x24, #ESR_ELx_EC_IABT_CUR // instruction abort in EL1
483 b.eq el1_ia
Mark Rutlandaed40e02014-11-24 12:31:40 +0000484 cmp x24, #ESR_ELx_EC_SYS64 // configurable trap
Catalin Marinas60ffc302012-03-05 11:49:27 +0000485 b.eq el1_undef
Mark Rutlandaed40e02014-11-24 12:31:40 +0000486 cmp x24, #ESR_ELx_EC_SP_ALIGN // stack alignment exception
Catalin Marinas60ffc302012-03-05 11:49:27 +0000487 b.eq el1_sp_pc
Mark Rutlandaed40e02014-11-24 12:31:40 +0000488 cmp x24, #ESR_ELx_EC_PC_ALIGN // pc alignment exception
Catalin Marinas60ffc302012-03-05 11:49:27 +0000489 b.eq el1_sp_pc
Mark Rutlandaed40e02014-11-24 12:31:40 +0000490 cmp x24, #ESR_ELx_EC_UNKNOWN // unknown exception in EL1
Catalin Marinas60ffc302012-03-05 11:49:27 +0000491 b.eq el1_undef
Mark Rutlandaed40e02014-11-24 12:31:40 +0000492 cmp x24, #ESR_ELx_EC_BREAKPT_CUR // debug exception in EL1
Catalin Marinas60ffc302012-03-05 11:49:27 +0000493 b.ge el1_dbg
494 b el1_inv
Laura Abbott9adeb8e2016-08-09 18:25:26 -0700495
496el1_ia:
497 /*
498 * Fall through to the Data abort case
499 */
Catalin Marinas60ffc302012-03-05 11:49:27 +0000500el1_da:
501 /*
502 * Data abort handling
503 */
Kristina Martsenko9e09d902017-06-06 20:14:10 +0100504 mrs x3, far_el1
Will Deacon2a283072014-04-29 19:04:06 +0100505 enable_dbg
Catalin Marinas60ffc302012-03-05 11:49:27 +0000506 // re-enable interrupts if they were enabled in the aborted context
507 tbnz x23, #7, 1f // PSR_I_BIT
508 enable_irq
5091:
Kristina Martsenko9e09d902017-06-06 20:14:10 +0100510 clear_address_tag x0, x3
Catalin Marinas60ffc302012-03-05 11:49:27 +0000511 mov x2, sp // struct pt_regs
512 bl do_mem_abort
513
514 // disable interrupts before pulling preserved data off the stack
515 disable_irq
516 kernel_exit 1
517el1_sp_pc:
518 /*
519 * Stack or PC alignment exception handling
520 */
521 mrs x0, far_el1
Will Deacon2a283072014-04-29 19:04:06 +0100522 enable_dbg
Catalin Marinas60ffc302012-03-05 11:49:27 +0000523 mov x2, sp
524 b do_sp_pc_abort
525el1_undef:
526 /*
527 * Undefined instruction
528 */
Will Deacon2a283072014-04-29 19:04:06 +0100529 enable_dbg
Catalin Marinas60ffc302012-03-05 11:49:27 +0000530 mov x0, sp
531 b do_undefinstr
532el1_dbg:
533 /*
534 * Debug exception handling
535 */
Mark Rutlandaed40e02014-11-24 12:31:40 +0000536 cmp x24, #ESR_ELx_EC_BRK64 // if BRK64
Sandeepa Prabhuee6214c2013-12-04 05:50:20 +0000537 cinc x24, x24, eq // set bit '0'
Catalin Marinas60ffc302012-03-05 11:49:27 +0000538 tbz x24, #0, el1_inv // EL1 only
539 mrs x0, far_el1
540 mov x2, sp // struct pt_regs
541 bl do_debug_exception
Catalin Marinas60ffc302012-03-05 11:49:27 +0000542 kernel_exit 1
543el1_inv:
544 // TODO: add support for undefined instructions in kernel mode
Will Deacon2a283072014-04-29 19:04:06 +0100545 enable_dbg
Catalin Marinas60ffc302012-03-05 11:49:27 +0000546 mov x0, sp
Mark Rutland1b428042015-07-07 18:00:49 +0100547 mov x2, x1
Catalin Marinas60ffc302012-03-05 11:49:27 +0000548 mov x1, #BAD_SYNC
Catalin Marinas60ffc302012-03-05 11:49:27 +0000549 b bad_mode
550ENDPROC(el1_sync)
551
552 .align 6
553el1_irq:
554 kernel_entry 1
Will Deacon2a283072014-04-29 19:04:06 +0100555 enable_dbg
Catalin Marinas60ffc302012-03-05 11:49:27 +0000556#ifdef CONFIG_TRACE_IRQFLAGS
557 bl trace_hardirqs_off
558#endif
Marc Zyngier64681782013-11-12 17:11:53 +0000559
560 irq_handler
561
Catalin Marinas60ffc302012-03-05 11:49:27 +0000562#ifdef CONFIG_PREEMPT
Mark Rutland5b7e8f72016-11-03 20:23:13 +0000563 ldr w24, [tsk, #TSK_TI_PREEMPT] // get preempt count
Marc Zyngier717321f2013-11-04 20:14:58 +0000564 cbnz w24, 1f // preempt count != 0
Mark Rutland5b7e8f72016-11-03 20:23:13 +0000565 ldr x0, [tsk, #TSK_TI_FLAGS] // get flags
Catalin Marinas60ffc302012-03-05 11:49:27 +0000566 tbz x0, #TIF_NEED_RESCHED, 1f // needs rescheduling?
567 bl el1_preempt
5681:
569#endif
570#ifdef CONFIG_TRACE_IRQFLAGS
571 bl trace_hardirqs_on
572#endif
573 kernel_exit 1
574ENDPROC(el1_irq)
575
576#ifdef CONFIG_PREEMPT
577el1_preempt:
578 mov x24, lr
Will Deacon2a283072014-04-29 19:04:06 +01005791: bl preempt_schedule_irq // irq en/disable is done inside
Mark Rutland5b7e8f72016-11-03 20:23:13 +0000580 ldr x0, [tsk, #TSK_TI_FLAGS] // get new tasks TI_FLAGS
Catalin Marinas60ffc302012-03-05 11:49:27 +0000581 tbnz x0, #TIF_NEED_RESCHED, 1b // needs rescheduling?
582 ret x24
583#endif
584
585/*
586 * EL0 mode handlers.
587 */
588 .align 6
589el0_sync:
590 kernel_entry 0
591 mrs x25, esr_el1 // read the syndrome register
Mark Rutlandaed40e02014-11-24 12:31:40 +0000592 lsr x24, x25, #ESR_ELx_EC_SHIFT // exception class
593 cmp x24, #ESR_ELx_EC_SVC64 // SVC in 64-bit state
Catalin Marinas60ffc302012-03-05 11:49:27 +0000594 b.eq el0_svc
Mark Rutlandaed40e02014-11-24 12:31:40 +0000595 cmp x24, #ESR_ELx_EC_DABT_LOW // data abort in EL0
Catalin Marinas60ffc302012-03-05 11:49:27 +0000596 b.eq el0_da
Mark Rutlandaed40e02014-11-24 12:31:40 +0000597 cmp x24, #ESR_ELx_EC_IABT_LOW // instruction abort in EL0
Catalin Marinas60ffc302012-03-05 11:49:27 +0000598 b.eq el0_ia
Mark Rutlandaed40e02014-11-24 12:31:40 +0000599 cmp x24, #ESR_ELx_EC_FP_ASIMD // FP/ASIMD access
Catalin Marinas60ffc302012-03-05 11:49:27 +0000600 b.eq el0_fpsimd_acc
Mark Rutlandaed40e02014-11-24 12:31:40 +0000601 cmp x24, #ESR_ELx_EC_FP_EXC64 // FP/ASIMD exception
Catalin Marinas60ffc302012-03-05 11:49:27 +0000602 b.eq el0_fpsimd_exc
Mark Rutlandaed40e02014-11-24 12:31:40 +0000603 cmp x24, #ESR_ELx_EC_SYS64 // configurable trap
Andre Przywara7dd01ae2016-06-28 18:07:32 +0100604 b.eq el0_sys
Mark Rutlandaed40e02014-11-24 12:31:40 +0000605 cmp x24, #ESR_ELx_EC_SP_ALIGN // stack alignment exception
Catalin Marinas60ffc302012-03-05 11:49:27 +0000606 b.eq el0_sp_pc
Mark Rutlandaed40e02014-11-24 12:31:40 +0000607 cmp x24, #ESR_ELx_EC_PC_ALIGN // pc alignment exception
Catalin Marinas60ffc302012-03-05 11:49:27 +0000608 b.eq el0_sp_pc
Mark Rutlandaed40e02014-11-24 12:31:40 +0000609 cmp x24, #ESR_ELx_EC_UNKNOWN // unknown exception in EL0
Catalin Marinas60ffc302012-03-05 11:49:27 +0000610 b.eq el0_undef
Mark Rutlandaed40e02014-11-24 12:31:40 +0000611 cmp x24, #ESR_ELx_EC_BREAKPT_LOW // debug exception in EL0
Catalin Marinas60ffc302012-03-05 11:49:27 +0000612 b.ge el0_dbg
613 b el0_inv
614
615#ifdef CONFIG_COMPAT
616 .align 6
617el0_sync_compat:
618 kernel_entry 0, 32
619 mrs x25, esr_el1 // read the syndrome register
Mark Rutlandaed40e02014-11-24 12:31:40 +0000620 lsr x24, x25, #ESR_ELx_EC_SHIFT // exception class
621 cmp x24, #ESR_ELx_EC_SVC32 // SVC in 32-bit state
Catalin Marinas60ffc302012-03-05 11:49:27 +0000622 b.eq el0_svc_compat
Mark Rutlandaed40e02014-11-24 12:31:40 +0000623 cmp x24, #ESR_ELx_EC_DABT_LOW // data abort in EL0
Catalin Marinas60ffc302012-03-05 11:49:27 +0000624 b.eq el0_da
Mark Rutlandaed40e02014-11-24 12:31:40 +0000625 cmp x24, #ESR_ELx_EC_IABT_LOW // instruction abort in EL0
Catalin Marinas60ffc302012-03-05 11:49:27 +0000626 b.eq el0_ia
Mark Rutlandaed40e02014-11-24 12:31:40 +0000627 cmp x24, #ESR_ELx_EC_FP_ASIMD // FP/ASIMD access
Catalin Marinas60ffc302012-03-05 11:49:27 +0000628 b.eq el0_fpsimd_acc
Mark Rutlandaed40e02014-11-24 12:31:40 +0000629 cmp x24, #ESR_ELx_EC_FP_EXC32 // FP/ASIMD exception
Catalin Marinas60ffc302012-03-05 11:49:27 +0000630 b.eq el0_fpsimd_exc
Mark Salyzyn77f3228f2015-10-13 14:30:51 -0700631 cmp x24, #ESR_ELx_EC_PC_ALIGN // pc alignment exception
632 b.eq el0_sp_pc
Mark Rutlandaed40e02014-11-24 12:31:40 +0000633 cmp x24, #ESR_ELx_EC_UNKNOWN // unknown exception in EL0
Catalin Marinas60ffc302012-03-05 11:49:27 +0000634 b.eq el0_undef
Mark Rutlandaed40e02014-11-24 12:31:40 +0000635 cmp x24, #ESR_ELx_EC_CP15_32 // CP15 MRC/MCR trap
Mark Rutland381cc2b2013-05-24 12:02:35 +0100636 b.eq el0_undef
Mark Rutlandaed40e02014-11-24 12:31:40 +0000637 cmp x24, #ESR_ELx_EC_CP15_64 // CP15 MRRC/MCRR trap
Mark Rutland381cc2b2013-05-24 12:02:35 +0100638 b.eq el0_undef
Mark Rutlandaed40e02014-11-24 12:31:40 +0000639 cmp x24, #ESR_ELx_EC_CP14_MR // CP14 MRC/MCR trap
Mark Rutland381cc2b2013-05-24 12:02:35 +0100640 b.eq el0_undef
Mark Rutlandaed40e02014-11-24 12:31:40 +0000641 cmp x24, #ESR_ELx_EC_CP14_LS // CP14 LDC/STC trap
Mark Rutland381cc2b2013-05-24 12:02:35 +0100642 b.eq el0_undef
Mark Rutlandaed40e02014-11-24 12:31:40 +0000643 cmp x24, #ESR_ELx_EC_CP14_64 // CP14 MRRC/MCRR trap
Mark Rutland381cc2b2013-05-24 12:02:35 +0100644 b.eq el0_undef
Mark Rutlandaed40e02014-11-24 12:31:40 +0000645 cmp x24, #ESR_ELx_EC_BREAKPT_LOW // debug exception in EL0
Catalin Marinas60ffc302012-03-05 11:49:27 +0000646 b.ge el0_dbg
647 b el0_inv
648el0_svc_compat:
649 /*
650 * AArch32 syscall handling
651 */
Catalin Marinas01564112015-01-06 16:42:32 +0000652 adrp stbl, compat_sys_call_table // load compat syscall table pointer
Catalin Marinas60ffc302012-03-05 11:49:27 +0000653 uxtw scno, w7 // syscall number in w7 (r7)
654 mov sc_nr, #__NR_compat_syscalls
655 b el0_svc_naked
656
657 .align 6
658el0_irq_compat:
659 kernel_entry 0, 32
660 b el0_irq_naked
661#endif
662
663el0_da:
664 /*
665 * Data abort handling
666 */
Larry Bassel6ab64632014-05-30 20:34:14 +0100667 mrs x26, far_el1
Catalin Marinas60ffc302012-03-05 11:49:27 +0000668 // enable interrupts before calling the main handler
Will Deacon2a283072014-04-29 19:04:06 +0100669 enable_dbg_and_irq
Larry Bassel6c81fe72014-05-30 12:34:15 -0700670 ct_user_exit
Kristina Martsenko9e09d902017-06-06 20:14:10 +0100671 clear_address_tag x0, x26
Catalin Marinas60ffc302012-03-05 11:49:27 +0000672 mov x1, x25
673 mov x2, sp
Will Deacond54e81f2014-09-29 11:44:01 +0100674 bl do_mem_abort
675 b ret_to_user
Catalin Marinas60ffc302012-03-05 11:49:27 +0000676el0_ia:
677 /*
678 * Instruction abort handling
679 */
Larry Bassel6ab64632014-05-30 20:34:14 +0100680 mrs x26, far_el1
Mark Rutland47320012018-04-12 12:11:13 +0100681 msr daifclr, #(8 | 4 | 1)
682#ifdef CONFIG_TRACE_IRQFLAGS
683 bl trace_hardirqs_off
684#endif
Larry Bassel6c81fe72014-05-30 12:34:15 -0700685 ct_user_exit
Larry Bassel6ab64632014-05-30 20:34:14 +0100686 mov x0, x26
Mark Rutland541ec872016-05-31 12:33:03 +0100687 mov x1, x25
Catalin Marinas60ffc302012-03-05 11:49:27 +0000688 mov x2, sp
Mark Rutland47320012018-04-12 12:11:13 +0100689 bl do_el0_ia_bp_hardening
Will Deacond54e81f2014-09-29 11:44:01 +0100690 b ret_to_user
Catalin Marinas60ffc302012-03-05 11:49:27 +0000691el0_fpsimd_acc:
692 /*
693 * Floating Point or Advanced SIMD access
694 */
Will Deacon2a283072014-04-29 19:04:06 +0100695 enable_dbg
Larry Bassel6c81fe72014-05-30 12:34:15 -0700696 ct_user_exit
Catalin Marinas60ffc302012-03-05 11:49:27 +0000697 mov x0, x25
698 mov x1, sp
Will Deacond54e81f2014-09-29 11:44:01 +0100699 bl do_fpsimd_acc
700 b ret_to_user
Catalin Marinas60ffc302012-03-05 11:49:27 +0000701el0_fpsimd_exc:
702 /*
703 * Floating Point or Advanced SIMD exception
704 */
Will Deacon2a283072014-04-29 19:04:06 +0100705 enable_dbg
Larry Bassel6c81fe72014-05-30 12:34:15 -0700706 ct_user_exit
Catalin Marinas60ffc302012-03-05 11:49:27 +0000707 mov x0, x25
708 mov x1, sp
Will Deacond54e81f2014-09-29 11:44:01 +0100709 bl do_fpsimd_exc
710 b ret_to_user
Catalin Marinas60ffc302012-03-05 11:49:27 +0000711el0_sp_pc:
712 /*
713 * Stack or PC alignment exception handling
714 */
Larry Bassel6ab64632014-05-30 20:34:14 +0100715 mrs x26, far_el1
Mark Rutlande7c3b242018-04-12 12:11:17 +0100716 enable_dbg
717#ifdef CONFIG_TRACE_IRQFLAGS
718 bl trace_hardirqs_off
719#endif
Mark Rutland46b05672015-06-15 16:40:27 +0100720 ct_user_exit
Larry Bassel6ab64632014-05-30 20:34:14 +0100721 mov x0, x26
Catalin Marinas60ffc302012-03-05 11:49:27 +0000722 mov x1, x25
723 mov x2, sp
Will Deacond54e81f2014-09-29 11:44:01 +0100724 bl do_sp_pc_abort
725 b ret_to_user
Catalin Marinas60ffc302012-03-05 11:49:27 +0000726el0_undef:
727 /*
728 * Undefined instruction
729 */
Catalin Marinas2600e132013-08-22 11:47:37 +0100730 // enable interrupts before calling the main handler
Will Deacon2a283072014-04-29 19:04:06 +0100731 enable_dbg_and_irq
Larry Bassel6c81fe72014-05-30 12:34:15 -0700732 ct_user_exit
Will Deacon2a283072014-04-29 19:04:06 +0100733 mov x0, sp
Will Deacond54e81f2014-09-29 11:44:01 +0100734 bl do_undefinstr
735 b ret_to_user
Andre Przywara7dd01ae2016-06-28 18:07:32 +0100736el0_sys:
737 /*
738 * System instructions, for trapped cache maintenance instructions
739 */
740 enable_dbg_and_irq
741 ct_user_exit
742 mov x0, x25
743 mov x1, sp
744 bl do_sysinstr
745 b ret_to_user
Catalin Marinas60ffc302012-03-05 11:49:27 +0000746el0_dbg:
747 /*
748 * Debug exception handling
749 */
750 tbnz x24, #0, el0_inv // EL0 only
751 mrs x0, far_el1
Catalin Marinas60ffc302012-03-05 11:49:27 +0000752 mov x1, x25
753 mov x2, sp
Will Deacon2a283072014-04-29 19:04:06 +0100754 bl do_debug_exception
755 enable_dbg
Larry Bassel6c81fe72014-05-30 12:34:15 -0700756 ct_user_exit
Will Deacon2a283072014-04-29 19:04:06 +0100757 b ret_to_user
Catalin Marinas60ffc302012-03-05 11:49:27 +0000758el0_inv:
Will Deacon2a283072014-04-29 19:04:06 +0100759 enable_dbg
Larry Bassel6c81fe72014-05-30 12:34:15 -0700760 ct_user_exit
Catalin Marinas60ffc302012-03-05 11:49:27 +0000761 mov x0, sp
762 mov x1, #BAD_SYNC
Mark Rutland1b428042015-07-07 18:00:49 +0100763 mov x2, x25
Mark Rutlandde327942017-01-18 17:23:41 +0000764 bl bad_el0_sync
Will Deacond54e81f2014-09-29 11:44:01 +0100765 b ret_to_user
Catalin Marinas60ffc302012-03-05 11:49:27 +0000766ENDPROC(el0_sync)
767
768 .align 6
769el0_irq:
770 kernel_entry 0
771el0_irq_naked:
Catalin Marinas60ffc302012-03-05 11:49:27 +0000772 enable_dbg
773#ifdef CONFIG_TRACE_IRQFLAGS
774 bl trace_hardirqs_off
775#endif
Marc Zyngier64681782013-11-12 17:11:53 +0000776
Larry Bassel6c81fe72014-05-30 12:34:15 -0700777 ct_user_exit
Mark Rutland34dc20b2018-04-12 12:11:18 +0100778#ifdef CONFIG_HARDEN_BRANCH_PREDICTOR
779 tbz x22, #55, 1f
780 bl do_el0_irq_bp_hardening
7811:
782#endif
Catalin Marinas60ffc302012-03-05 11:49:27 +0000783 irq_handler
Marc Zyngier64681782013-11-12 17:11:53 +0000784
Catalin Marinas60ffc302012-03-05 11:49:27 +0000785#ifdef CONFIG_TRACE_IRQFLAGS
786 bl trace_hardirqs_on
787#endif
788 b ret_to_user
789ENDPROC(el0_irq)
790
791/*
Catalin Marinas60ffc302012-03-05 11:49:27 +0000792 * Register switch for AArch64. The callee-saved registers need to be saved
793 * and restored. On entry:
794 * x0 = previous task_struct (must be preserved across the switch)
795 * x1 = next task_struct
796 * Previous and next are guaranteed not to be the same.
797 *
798 */
799ENTRY(cpu_switch_to)
Will Deaconc0d3fce2015-07-20 15:14:53 +0100800 mov x10, #THREAD_CPU_CONTEXT
801 add x8, x0, x10
Catalin Marinas60ffc302012-03-05 11:49:27 +0000802 mov x9, sp
803 stp x19, x20, [x8], #16 // store callee-saved registers
804 stp x21, x22, [x8], #16
805 stp x23, x24, [x8], #16
806 stp x25, x26, [x8], #16
807 stp x27, x28, [x8], #16
808 stp x29, x9, [x8], #16
809 str lr, [x8]
Will Deaconc0d3fce2015-07-20 15:14:53 +0100810 add x8, x1, x10
Catalin Marinas60ffc302012-03-05 11:49:27 +0000811 ldp x19, x20, [x8], #16 // restore callee-saved registers
812 ldp x21, x22, [x8], #16
813 ldp x23, x24, [x8], #16
814 ldp x25, x26, [x8], #16
815 ldp x27, x28, [x8], #16
816 ldp x29, x9, [x8], #16
817 ldr lr, [x8]
818 mov sp, x9
Mark Rutland5b7e8f72016-11-03 20:23:13 +0000819 msr sp_el0, x1
Catalin Marinas60ffc302012-03-05 11:49:27 +0000820 ret
821ENDPROC(cpu_switch_to)
822
823/*
824 * This is the fast syscall return path. We do as little as possible here,
825 * and this includes saving x0 back into the kernel stack.
826 */
827ret_fast_syscall:
828 disable_irq // disable interrupts
Will Deacon412fcb62015-08-19 15:57:09 +0100829 str x0, [sp, #S_X0] // returned x0
Mark Rutland5b7e8f72016-11-03 20:23:13 +0000830 ldr x1, [tsk, #TSK_TI_FLAGS] // re-check for syscall tracing
Josh Stone04d7e092015-06-05 14:28:03 -0700831 and x2, x1, #_TIF_SYSCALL_WORK
832 cbnz x2, ret_fast_syscall_trace
Catalin Marinas60ffc302012-03-05 11:49:27 +0000833 and x2, x1, #_TIF_WORK_MASK
Will Deacon412fcb62015-08-19 15:57:09 +0100834 cbnz x2, work_pending
Will Deacon2a283072014-04-29 19:04:06 +0100835 enable_step_tsk x1, x2
Will Deacon412fcb62015-08-19 15:57:09 +0100836 kernel_exit 0
Josh Stone04d7e092015-06-05 14:28:03 -0700837ret_fast_syscall_trace:
838 enable_irq // enable interrupts
Will Deacon412fcb62015-08-19 15:57:09 +0100839 b __sys_trace_return_skipped // we already saved x0
Catalin Marinas60ffc302012-03-05 11:49:27 +0000840
841/*
842 * Ok, we need to do extra processing, enter the slow path.
843 */
Catalin Marinas60ffc302012-03-05 11:49:27 +0000844work_pending:
Catalin Marinas60ffc302012-03-05 11:49:27 +0000845 mov x0, sp // 'regs'
Catalin Marinas60ffc302012-03-05 11:49:27 +0000846 bl do_notify_resume
Catalin Marinasdb3899a2015-12-04 12:42:29 +0000847#ifdef CONFIG_TRACE_IRQFLAGS
Chris Metcalf421dd6f2016-07-14 16:48:14 -0400848 bl trace_hardirqs_on // enabled while in userspace
Catalin Marinasdb3899a2015-12-04 12:42:29 +0000849#endif
Mark Rutland5b7e8f72016-11-03 20:23:13 +0000850 ldr x1, [tsk, #TSK_TI_FLAGS] // re-check for single-step
Chris Metcalf421dd6f2016-07-14 16:48:14 -0400851 b finish_ret_to_user
Catalin Marinas60ffc302012-03-05 11:49:27 +0000852/*
853 * "slow" syscall return path.
854 */
Catalin Marinas59dc67b2012-09-10 16:11:46 +0100855ret_to_user:
Catalin Marinas60ffc302012-03-05 11:49:27 +0000856 disable_irq // disable interrupts
Mark Rutland5b7e8f72016-11-03 20:23:13 +0000857 ldr x1, [tsk, #TSK_TI_FLAGS]
Catalin Marinas60ffc302012-03-05 11:49:27 +0000858 and x2, x1, #_TIF_WORK_MASK
859 cbnz x2, work_pending
Chris Metcalf421dd6f2016-07-14 16:48:14 -0400860finish_ret_to_user:
Will Deacon2a283072014-04-29 19:04:06 +0100861 enable_step_tsk x1, x2
Will Deacon412fcb62015-08-19 15:57:09 +0100862 kernel_exit 0
Catalin Marinas60ffc302012-03-05 11:49:27 +0000863ENDPROC(ret_to_user)
864
865/*
866 * This is how we return from a fork.
867 */
868ENTRY(ret_from_fork)
869 bl schedule_tail
Catalin Marinasc34501d2012-10-05 12:31:20 +0100870 cbz x19, 1f // not a kernel thread
871 mov x0, x20
872 blr x19
8731: get_thread_info tsk
Catalin Marinas60ffc302012-03-05 11:49:27 +0000874 b ret_to_user
875ENDPROC(ret_from_fork)
876
877/*
878 * SVC handler.
879 */
880 .align 6
881el0_svc:
882 adrp stbl, sys_call_table // load syscall table pointer
883 uxtw scno, w8 // syscall number in w8
884 mov sc_nr, #__NR_syscalls
885el0_svc_naked: // compat entry point
886 stp x0, scno, [sp, #S_ORIG_X0] // save the original x0 and syscall number
Will Deacon2a283072014-04-29 19:04:06 +0100887 enable_dbg_and_irq
Larry Bassel6c81fe72014-05-30 12:34:15 -0700888 ct_user_exit 1
Catalin Marinas60ffc302012-03-05 11:49:27 +0000889
Mark Rutland5b7e8f72016-11-03 20:23:13 +0000890 ldr x16, [tsk, #TSK_TI_FLAGS] // check for syscall hooks
AKASHI Takahiro449f81a2014-04-30 10:51:29 +0100891 tst x16, #_TIF_SYSCALL_WORK
892 b.ne __sys_trace
Catalin Marinas60ffc302012-03-05 11:49:27 +0000893 cmp scno, sc_nr // check upper syscall limit
894 b.hs ni_sys
Mark Rutlandf3ed64a2018-04-12 12:11:02 +0100895 mask_nospec64 scno, sc_nr, x19 // enforce bounds for syscall number
Catalin Marinas60ffc302012-03-05 11:49:27 +0000896 ldr x16, [stbl, scno, lsl #3] // address in the syscall table
Will Deacond54e81f2014-09-29 11:44:01 +0100897 blr x16 // call sys_* routine
898 b ret_fast_syscall
Catalin Marinas60ffc302012-03-05 11:49:27 +0000899ni_sys:
900 mov x0, sp
Will Deacond54e81f2014-09-29 11:44:01 +0100901 bl do_ni_syscall
902 b ret_fast_syscall
Catalin Marinas60ffc302012-03-05 11:49:27 +0000903ENDPROC(el0_svc)
904
905 /*
906 * This is the really slow path. We're going to be doing context
907 * switches, and waiting for our parent to respond.
908 */
909__sys_trace:
AKASHI Takahiro1014c812014-11-28 05:26:35 +0000910 mov w0, #-1 // set default errno for
911 cmp scno, x0 // user-issued syscall(-1)
912 b.ne 1f
913 mov x0, #-ENOSYS
914 str x0, [sp, #S_X0]
9151: mov x0, sp
AKASHI Takahiro31578582014-04-30 10:51:30 +0100916 bl syscall_trace_enter
AKASHI Takahiro1014c812014-11-28 05:26:35 +0000917 cmp w0, #-1 // skip the syscall?
918 b.eq __sys_trace_return_skipped
Catalin Marinas60ffc302012-03-05 11:49:27 +0000919 uxtw scno, w0 // syscall number (possibly new)
920 mov x1, sp // pointer to regs
921 cmp scno, sc_nr // check upper syscall limit
Will Deacond54e81f2014-09-29 11:44:01 +0100922 b.hs __ni_sys_trace
Catalin Marinas60ffc302012-03-05 11:49:27 +0000923 ldp x0, x1, [sp] // restore the syscall args
924 ldp x2, x3, [sp, #S_X2]
925 ldp x4, x5, [sp, #S_X4]
926 ldp x6, x7, [sp, #S_X6]
927 ldr x16, [stbl, scno, lsl #3] // address in the syscall table
Will Deacond54e81f2014-09-29 11:44:01 +0100928 blr x16 // call sys_* routine
Catalin Marinas60ffc302012-03-05 11:49:27 +0000929
930__sys_trace_return:
AKASHI Takahiro1014c812014-11-28 05:26:35 +0000931 str x0, [sp, #S_X0] // save returned x0
932__sys_trace_return_skipped:
AKASHI Takahiro31578582014-04-30 10:51:30 +0100933 mov x0, sp
934 bl syscall_trace_exit
Catalin Marinas60ffc302012-03-05 11:49:27 +0000935 b ret_to_user
936
Will Deacond54e81f2014-09-29 11:44:01 +0100937__ni_sys_trace:
938 mov x0, sp
939 bl do_ni_syscall
940 b __sys_trace_return
941
Pratyush Anand888b3c82016-07-08 12:35:50 -0400942 .popsection // .entry.text
943
Will Deacona329b062017-11-14 14:07:40 +0000944#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
945/*
946 * Exception vectors trampoline.
947 */
948 .pushsection ".entry.tramp.text", "ax"
949
950 .macro tramp_map_kernel, tmp
951 mrs \tmp, ttbr1_el1
952 sub \tmp, \tmp, #(SWAPPER_DIR_SIZE + RESERVED_TTBR0_SIZE)
953 bic \tmp, \tmp, #USER_ASID_FLAG
954 msr ttbr1_el1, \tmp
Will Deacon04b77fe2017-11-14 14:29:19 +0000955#ifdef CONFIG_ARCH_MSM8996
956 /* ASID already in \tmp[63:48] */
957 movk \tmp, #:abs_g2_nc:(TRAMP_VALIAS >> 12)
958 movk \tmp, #:abs_g1_nc:(TRAMP_VALIAS >> 12)
959 /* 2MB boundary containing the vectors, so we nobble the walk cache */
960 movk \tmp, #:abs_g0_nc:((TRAMP_VALIAS & ~(SZ_2M - 1)) >> 12)
961 isb
962 tlbi vae1, \tmp
963 dsb nsh
964#endif /* CONFIG_ARCH_MSM8996 */
Will Deacona329b062017-11-14 14:07:40 +0000965 .endm
966
967 .macro tramp_unmap_kernel, tmp
968 mrs \tmp, ttbr1_el1
969 add \tmp, \tmp, #(SWAPPER_DIR_SIZE + RESERVED_TTBR0_SIZE)
970 orr \tmp, \tmp, #USER_ASID_FLAG
971 msr ttbr1_el1, \tmp
972 /*
Will Deacona80d8e22018-04-03 12:09:22 +0100973 * We avoid running the post_ttbr_update_workaround here because
974 * it's only needed by Cavium ThunderX, which requires KPTI to be
975 * disabled.
Will Deacona329b062017-11-14 14:07:40 +0000976 */
977 .endm
978
979 .macro tramp_ventry, regsize = 64
980 .align 7
9811:
982 .if \regsize == 64
983 msr tpidrro_el0, x30 // Restored in kernel_ventry
984 .endif
Will Deaconf6af5322018-04-03 12:09:11 +0100985 /*
986 * Defend against branch aliasing attacks by pushing a dummy
987 * entry onto the return stack and using a RET instruction to
988 * enter the full-fat kernel vectors.
989 */
Will Deacon75359362017-11-14 16:15:59 +0000990 bl 2f
991 b .
9922:
Will Deacona329b062017-11-14 14:07:40 +0000993 tramp_map_kernel x30
Will Deacon06fe41f2017-12-06 11:24:02 +0000994#ifdef CONFIG_RANDOMIZE_BASE
995 adr x30, tramp_vectors + PAGE_SIZE
Todd Poynorb328e522018-01-08 12:22:41 -0800996#ifndef CONFIG_ARCH_MSM8996
997 isb
998#endif
Will Deacon06fe41f2017-12-06 11:24:02 +0000999 ldr x30, [x30]
1000#else
Will Deacona329b062017-11-14 14:07:40 +00001001 ldr x30, =vectors
Will Deacon06fe41f2017-12-06 11:24:02 +00001002#endif
Will Deacona329b062017-11-14 14:07:40 +00001003 prfm plil1strm, [x30, #(1b - tramp_vectors)]
1004 msr vbar_el1, x30
1005 add x30, x30, #(1b - tramp_vectors)
1006 isb
Will Deacon75359362017-11-14 16:15:59 +00001007 ret
Will Deacona329b062017-11-14 14:07:40 +00001008 .endm
1009
1010 .macro tramp_exit, regsize = 64
1011 adr x30, tramp_vectors
1012 msr vbar_el1, x30
1013 tramp_unmap_kernel x30
1014 .if \regsize == 64
1015 mrs x30, far_el1
1016 .endif
1017 eret
1018 .endm
1019
1020 .align 11
1021ENTRY(tramp_vectors)
1022 .space 0x400
1023
1024 tramp_ventry
1025 tramp_ventry
1026 tramp_ventry
1027 tramp_ventry
1028
1029 tramp_ventry 32
1030 tramp_ventry 32
1031 tramp_ventry 32
1032 tramp_ventry 32
1033END(tramp_vectors)
1034
1035ENTRY(tramp_exit_native)
1036 tramp_exit
1037END(tramp_exit_native)
1038
1039ENTRY(tramp_exit_compat)
1040 tramp_exit 32
1041END(tramp_exit_compat)
1042
1043 .ltorg
1044 .popsection // .entry.tramp.text
Will Deacon06fe41f2017-12-06 11:24:02 +00001045#ifdef CONFIG_RANDOMIZE_BASE
1046 .pushsection ".rodata", "a"
1047 .align PAGE_SHIFT
1048 .globl __entry_tramp_data_start
1049__entry_tramp_data_start:
1050 .quad vectors
1051 .popsection // .rodata
1052#endif /* CONFIG_RANDOMIZE_BASE */
Will Deacona329b062017-11-14 14:07:40 +00001053#endif /* CONFIG_UNMAP_KERNEL_AT_EL0 */
1054
Catalin Marinas60ffc302012-03-05 11:49:27 +00001055/*
1056 * Special system call wrappers.
1057 */
Catalin Marinas60ffc302012-03-05 11:49:27 +00001058ENTRY(sys_rt_sigreturn_wrapper)
1059 mov x0, sp
1060 b sys_rt_sigreturn
1061ENDPROC(sys_rt_sigreturn_wrapper)