blob: 314e8a5436d1bbe00467ca967364a07d11594d12 [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
21#include <linux/init.h>
22#include <linux/linkage.h>
23
Marc Zyngier8d883b22015-06-01 10:47:41 +010024#include <asm/alternative.h>
Catalin Marinas60ffc302012-03-05 11:49:27 +000025#include <asm/assembler.h>
26#include <asm/asm-offsets.h>
Will Deacon905e8c52015-03-23 19:07:02 +000027#include <asm/cpufeature.h>
Catalin Marinas60ffc302012-03-05 11:49:27 +000028#include <asm/errno.h>
Marc Zyngier5c1ce6f2013-04-08 17:17:03 +010029#include <asm/esr.h>
James Morse8e23dac2015-12-04 11:02:27 +000030#include <asm/irq.h>
James Morsee19a6ee2016-06-20 18:28:01 +010031#include <asm/memory.h>
Will Deacona329b062017-11-14 14:07:40 +000032#include <asm/mmu.h>
Mark Rutland27eeced2018-04-12 12:10:59 +010033#include <asm/processor.h>
Catalin Marinascfa93772016-09-02 14:54:03 +010034#include <asm/ptrace.h>
Catalin Marinas60ffc302012-03-05 11:49:27 +000035#include <asm/thread_info.h>
Catalin Marinascfa93772016-09-02 14:54:03 +010036#include <asm/uaccess.h>
Kristina Martsenko9e09d902017-06-06 20:14:10 +010037#include <asm/asm-uaccess.h>
Catalin Marinas60ffc302012-03-05 11:49:27 +000038#include <asm/unistd.h>
Will Deacon78a0cec2018-04-03 12:09:04 +010039#include <asm/kernel-pgtable.h>
Catalin Marinas60ffc302012-03-05 11:49:27 +000040
41/*
Larry Bassel6c81fe72014-05-30 12:34:15 -070042 * Context tracking subsystem. Used to instrument transitions
43 * between user and kernel mode.
44 */
45 .macro ct_user_exit, syscall = 0
46#ifdef CONFIG_CONTEXT_TRACKING
47 bl context_tracking_user_exit
48 .if \syscall == 1
49 /*
50 * Save/restore needed during syscalls. Restore syscall arguments from
51 * the values already saved on stack during kernel_entry.
52 */
53 ldp x0, x1, [sp]
54 ldp x2, x3, [sp, #S_X2]
55 ldp x4, x5, [sp, #S_X4]
56 ldp x6, x7, [sp, #S_X6]
57 .endif
58#endif
59 .endm
60
61 .macro ct_user_enter
62#ifdef CONFIG_CONTEXT_TRACKING
63 bl context_tracking_user_enter
64#endif
65 .endm
66
67/*
Catalin Marinas60ffc302012-03-05 11:49:27 +000068 * Bad Abort numbers
69 *-----------------
70 */
71#define BAD_SYNC 0
72#define BAD_IRQ 1
73#define BAD_FIQ 2
74#define BAD_ERROR 3
75
Will Deacon8fdbffb2017-11-14 14:20:21 +000076 .macro kernel_ventry, el, label, regsize = 64
Mark Rutland17d35922017-07-19 17:24:49 +010077 .align 7
Will Deaconc27a22582017-11-14 14:24:29 +000078#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
Will Deaconf79ff2d2017-11-14 14:38:19 +000079alternative_if ARM64_UNMAP_KERNEL_AT_EL0
Will Deaconc27a22582017-11-14 14:24:29 +000080 .if \el == 0
81 .if \regsize == 64
82 mrs x30, tpidrro_el0
83 msr tpidrro_el0, xzr
84 .else
85 mov x30, xzr
86 .endif
87 .endif
Will Deaconf79ff2d2017-11-14 14:38:19 +000088alternative_else_nop_endif
Will Deaconc27a22582017-11-14 14:24:29 +000089#endif
90
Will Deacon63648dd2014-09-29 12:26:41 +010091 sub sp, sp, #S_FRAME_SIZE
Will Deacon8fdbffb2017-11-14 14:20:21 +000092 b el\()\el\()_\label
Mark Rutland17d35922017-07-19 17:24:49 +010093 .endm
94
Will Deaconc27a22582017-11-14 14:24:29 +000095 .macro tramp_alias, dst, sym
96 mov_q \dst, TRAMP_VALIAS
97 add \dst, \dst, #(\sym - .entry.tramp.text)
98 .endm
99
Mark Rutland17d35922017-07-19 17:24:49 +0100100 .macro kernel_entry, el, regsize = 64
Catalin Marinas60ffc302012-03-05 11:49:27 +0000101 .if \regsize == 32
102 mov w0, w0 // zero upper 32 bits of x0
103 .endif
Will Deacon63648dd2014-09-29 12:26:41 +0100104 stp x0, x1, [sp, #16 * 0]
105 stp x2, x3, [sp, #16 * 1]
106 stp x4, x5, [sp, #16 * 2]
107 stp x6, x7, [sp, #16 * 3]
108 stp x8, x9, [sp, #16 * 4]
109 stp x10, x11, [sp, #16 * 5]
110 stp x12, x13, [sp, #16 * 6]
111 stp x14, x15, [sp, #16 * 7]
112 stp x16, x17, [sp, #16 * 8]
113 stp x18, x19, [sp, #16 * 9]
114 stp x20, x21, [sp, #16 * 10]
115 stp x22, x23, [sp, #16 * 11]
116 stp x24, x25, [sp, #16 * 12]
117 stp x26, x27, [sp, #16 * 13]
118 stp x28, x29, [sp, #16 * 14]
119
Catalin Marinas60ffc302012-03-05 11:49:27 +0000120 .if \el == 0
121 mrs x21, sp_el0
Mark Rutland5b7e8f72016-11-03 20:23:13 +0000122 ldr_this_cpu tsk, __entry_task, x20 // Ensure MDSCR_EL1.SS is clear,
123 ldr x19, [tsk, #TSK_TI_FLAGS] // since we can unmask debug
Will Deacon2a283072014-04-29 19:04:06 +0100124 disable_step_tsk x19, x20 // exceptions when scheduling.
James Morse49003a82015-12-10 10:22:41 +0000125
126 mov x29, xzr // fp pointed to user-space
Catalin Marinas60ffc302012-03-05 11:49:27 +0000127 .else
128 add x21, sp, #S_FRAME_SIZE
James Morsee19a6ee2016-06-20 18:28:01 +0100129 get_thread_info tsk
Mark Rutlandc9100862018-04-12 12:11:00 +0100130 /* Save the task's original addr_limit and set USER_DS */
Mark Rutland5b7e8f72016-11-03 20:23:13 +0000131 ldr x20, [tsk, #TSK_TI_ADDR_LIMIT]
James Morsee19a6ee2016-06-20 18:28:01 +0100132 str x20, [sp, #S_ORIG_ADDR_LIMIT]
Mark Rutlandc9100862018-04-12 12:11:00 +0100133 mov x20, #USER_DS
Mark Rutland5b7e8f72016-11-03 20:23:13 +0000134 str x20, [tsk, #TSK_TI_ADDR_LIMIT]
Vladimir Murzin563cada2016-09-01 14:35:59 +0100135 /* No need to reset PSTATE.UAO, hardware's already set it to 0 for us */
James Morsee19a6ee2016-06-20 18:28:01 +0100136 .endif /* \el == 0 */
Catalin Marinas60ffc302012-03-05 11:49:27 +0000137 mrs x22, elr_el1
138 mrs x23, spsr_el1
139 stp lr, x21, [sp, #S_LR]
Catalin Marinascfa93772016-09-02 14:54:03 +0100140
141#ifdef CONFIG_ARM64_SW_TTBR0_PAN
142 /*
143 * Set the TTBR0 PAN bit in SPSR. When the exception is taken from
144 * EL0, there is no need to check the state of TTBR0_EL1 since
145 * accesses are always enabled.
146 * Note that the meaning of this bit differs from the ARMv8.1 PAN
147 * feature as all TTBR0_EL1 accesses are disabled, not just those to
148 * user mappings.
149 */
150alternative_if ARM64_HAS_PAN
151 b 1f // skip TTBR0 PAN
152alternative_else_nop_endif
153
154 .if \el != 0
Catalin Marinas87883132018-01-10 13:18:30 +0000155 mrs x21, ttbr0_el1
Will Deacond7013ed2017-12-01 17:33:48 +0000156 tst x21, #TTBR_ASID_MASK // Check for the reserved ASID
Catalin Marinascfa93772016-09-02 14:54:03 +0100157 orr x23, x23, #PSR_PAN_BIT // Set the emulated PAN in the saved SPSR
158 b.eq 1f // TTBR0 access already disabled
159 and x23, x23, #~PSR_PAN_BIT // Clear the emulated PAN in the saved SPSR
160 .endif
161
162 __uaccess_ttbr0_disable x21
1631:
164#endif
165
Catalin Marinas60ffc302012-03-05 11:49:27 +0000166 stp x22, x23, [sp, #S_PC]
167
168 /*
169 * Set syscallno to -1 by default (overridden later if real syscall).
170 */
171 .if \el == 0
172 mvn x21, xzr
173 str x21, [sp, #S_SYSCALLNO]
174 .endif
175
176 /*
Jungseok Lee6cdf9c72015-12-04 11:02:25 +0000177 * Set sp_el0 to current thread_info.
178 */
179 .if \el == 0
180 msr sp_el0, tsk
181 .endif
182
183 /*
Catalin Marinas60ffc302012-03-05 11:49:27 +0000184 * Registers that may be useful after this macro is invoked:
185 *
186 * x21 - aborted SP
187 * x22 - aborted PC
188 * x23 - aborted PSTATE
189 */
190 .endm
191
Will Deacon412fcb62015-08-19 15:57:09 +0100192 .macro kernel_exit, el
James Morsee19a6ee2016-06-20 18:28:01 +0100193 .if \el != 0
194 /* Restore the task's original addr_limit. */
195 ldr x20, [sp, #S_ORIG_ADDR_LIMIT]
Mark Rutland5b7e8f72016-11-03 20:23:13 +0000196 str x20, [tsk, #TSK_TI_ADDR_LIMIT]
James Morsee19a6ee2016-06-20 18:28:01 +0100197
198 /* No need to restore UAO, it will be restored from SPSR_EL1 */
199 .endif
200
Catalin Marinas60ffc302012-03-05 11:49:27 +0000201 ldp x21, x22, [sp, #S_PC] // load ELR, SPSR
202 .if \el == 0
Larry Bassel6c81fe72014-05-30 12:34:15 -0700203 ct_user_enter
Catalin Marinascfa93772016-09-02 14:54:03 +0100204 .endif
205
206#ifdef CONFIG_ARM64_SW_TTBR0_PAN
207 /*
208 * Restore access to TTBR0_EL1. If returning to EL0, no need for SPSR
209 * PAN bit checking.
210 */
211alternative_if ARM64_HAS_PAN
212 b 2f // skip TTBR0 PAN
213alternative_else_nop_endif
214
215 .if \el != 0
216 tbnz x22, #22, 1f // Skip re-enabling TTBR0 access if the PSR_PAN_BIT is set
217 .endif
218
Will Deacon599c71f2017-08-10 13:58:16 +0100219 __uaccess_ttbr0_enable x0, x1
Catalin Marinascfa93772016-09-02 14:54:03 +0100220
221 .if \el == 0
222 /*
223 * Enable errata workarounds only if returning to user. The only
224 * workaround currently required for TTBR0_EL1 changes are for the
225 * Cavium erratum 27456 (broadcast TLBI instructions may cause I-cache
226 * corruption).
227 */
Marc Zyngier95bfec62018-01-02 18:19:39 +0000228 bl post_ttbr_update_workaround
Catalin Marinascfa93772016-09-02 14:54:03 +0100229 .endif
2301:
231 .if \el != 0
232 and x22, x22, #~PSR_PAN_BIT // ARMv8.0 CPUs do not understand this bit
233 .endif
2342:
235#endif
236
237 .if \el == 0
Catalin Marinas60ffc302012-03-05 11:49:27 +0000238 ldr x23, [sp, #S_SP] // load return stack pointer
Catalin Marinas60ffc302012-03-05 11:49:27 +0000239 msr sp_el0, x23
Will Deaconc27a22582017-11-14 14:24:29 +0000240 tst x22, #PSR_MODE32_BIT // native task?
241 b.eq 3f
242
Will Deacon905e8c52015-03-23 19:07:02 +0000243#ifdef CONFIG_ARM64_ERRATUM_845719
Mark Rutland6ba3b552016-09-07 11:07:09 +0100244alternative_if ARM64_WORKAROUND_845719
Daniel Thompsone28cabf2015-07-22 12:21:03 +0100245#ifdef CONFIG_PID_IN_CONTEXTIDR
246 mrs x29, contextidr_el1
247 msr contextidr_el1, x29
248#else
249 msr contextidr_el1, xzr
250#endif
Mark Rutland6ba3b552016-09-07 11:07:09 +0100251alternative_else_nop_endif
Will Deacon905e8c52015-03-23 19:07:02 +0000252#endif
Will Deaconc27a22582017-11-14 14:24:29 +00002533:
Catalin Marinas60ffc302012-03-05 11:49:27 +0000254 .endif
Catalin Marinascfa93772016-09-02 14:54:03 +0100255
Will Deacon63648dd2014-09-29 12:26:41 +0100256 msr elr_el1, x21 // set up the return data
257 msr spsr_el1, x22
Will Deacon63648dd2014-09-29 12:26:41 +0100258 ldp x0, x1, [sp, #16 * 0]
Will Deacon63648dd2014-09-29 12:26:41 +0100259 ldp x2, x3, [sp, #16 * 1]
260 ldp x4, x5, [sp, #16 * 2]
261 ldp x6, x7, [sp, #16 * 3]
262 ldp x8, x9, [sp, #16 * 4]
263 ldp x10, x11, [sp, #16 * 5]
264 ldp x12, x13, [sp, #16 * 6]
265 ldp x14, x15, [sp, #16 * 7]
266 ldp x16, x17, [sp, #16 * 8]
267 ldp x18, x19, [sp, #16 * 9]
268 ldp x20, x21, [sp, #16 * 10]
269 ldp x22, x23, [sp, #16 * 11]
270 ldp x24, x25, [sp, #16 * 12]
271 ldp x26, x27, [sp, #16 * 13]
272 ldp x28, x29, [sp, #16 * 14]
273 ldr lr, [sp, #S_LR]
274 add sp, sp, #S_FRAME_SIZE // restore sp
Will Deaconc27a22582017-11-14 14:24:29 +0000275
Will Deaconc27a22582017-11-14 14:24:29 +0000276 .if \el == 0
Will Deaconf79ff2d2017-11-14 14:38:19 +0000277alternative_insn eret, nop, ARM64_UNMAP_KERNEL_AT_EL0
278#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
Will Deaconc27a22582017-11-14 14:24:29 +0000279 bne 4f
280 msr far_el1, x30
281 tramp_alias x30, tramp_exit_native
282 br x30
2834:
284 tramp_alias x30, tramp_exit_compat
285 br x30
Will Deaconf79ff2d2017-11-14 14:38:19 +0000286#endif
Will Deaconc27a22582017-11-14 14:24:29 +0000287 .else
288 eret
289 .endif
Catalin Marinas60ffc302012-03-05 11:49:27 +0000290 .endm
291
James Morse971c67c2015-12-15 11:21:25 +0000292 .macro irq_stack_entry
James Morse8e23dac2015-12-04 11:02:27 +0000293 mov x19, sp // preserve the original sp
294
James Morse8e23dac2015-12-04 11:02:27 +0000295 /*
Mark Rutland5b7e8f72016-11-03 20:23:13 +0000296 * Compare sp with the base of the task stack.
297 * If the top ~(THREAD_SIZE - 1) bits match, we are on a task stack,
298 * and should switch to the irq stack.
James Morse8e23dac2015-12-04 11:02:27 +0000299 */
Mark Rutland5b7e8f72016-11-03 20:23:13 +0000300 ldr x25, [tsk, TSK_STACK]
301 eor x25, x25, x19
302 and x25, x25, #~(THREAD_SIZE - 1)
303 cbnz x25, 9998f
James Morse8e23dac2015-12-04 11:02:27 +0000304
Mark Rutland8b6c9c92016-11-03 20:23:12 +0000305 adr_this_cpu x25, irq_stack, x26
James Morse8e23dac2015-12-04 11:02:27 +0000306 mov x26, #IRQ_STACK_START_SP
307 add x26, x25, x26
James Morsed224a692015-12-18 16:01:47 +0000308
309 /* switch to the irq stack */
James Morse8e23dac2015-12-04 11:02:27 +0000310 mov sp, x26
311
James Morse971c67c2015-12-15 11:21:25 +0000312 /*
313 * Add a dummy stack frame, this non-standard format is fixed up
314 * by unwind_frame()
315 */
316 stp x29, x19, [sp, #-16]!
James Morse8e23dac2015-12-04 11:02:27 +0000317 mov x29, sp
James Morse8e23dac2015-12-04 11:02:27 +0000318
3199998:
320 .endm
321
322 /*
323 * x19 should be preserved between irq_stack_entry and
324 * irq_stack_exit.
325 */
326 .macro irq_stack_exit
327 mov sp, x19
328 .endm
329
Catalin Marinas60ffc302012-03-05 11:49:27 +0000330/*
331 * These are the registers used in the syscall handler, and allow us to
332 * have in theory up to 7 arguments to a function - x0 to x6.
333 *
334 * x7 is reserved for the system call number in 32-bit mode.
335 */
336sc_nr .req x25 // number of system calls
337scno .req x26 // syscall number
338stbl .req x27 // syscall table pointer
339tsk .req x28 // current thread_info
340
341/*
342 * Interrupt handling.
343 */
344 .macro irq_handler
James Morse8e23dac2015-12-04 11:02:27 +0000345 ldr_l x1, handle_arch_irq
Catalin Marinas60ffc302012-03-05 11:49:27 +0000346 mov x0, sp
James Morse971c67c2015-12-15 11:21:25 +0000347 irq_stack_entry
Catalin Marinas60ffc302012-03-05 11:49:27 +0000348 blr x1
James Morse8e23dac2015-12-04 11:02:27 +0000349 irq_stack_exit
Catalin Marinas60ffc302012-03-05 11:49:27 +0000350 .endm
351
352 .text
353
354/*
355 * Exception vectors.
356 */
Pratyush Anand888b3c82016-07-08 12:35:50 -0400357 .pushsection ".entry.text", "ax"
Catalin Marinas60ffc302012-03-05 11:49:27 +0000358
359 .align 11
360ENTRY(vectors)
Will Deacon8fdbffb2017-11-14 14:20:21 +0000361 kernel_ventry 1, sync_invalid // Synchronous EL1t
362 kernel_ventry 1, irq_invalid // IRQ EL1t
363 kernel_ventry 1, fiq_invalid // FIQ EL1t
364 kernel_ventry 1, error_invalid // Error EL1t
Catalin Marinas60ffc302012-03-05 11:49:27 +0000365
Will Deacon8fdbffb2017-11-14 14:20:21 +0000366 kernel_ventry 1, sync // Synchronous EL1h
367 kernel_ventry 1, irq // IRQ EL1h
368 kernel_ventry 1, fiq_invalid // FIQ EL1h
369 kernel_ventry 1, error_invalid // Error EL1h
Catalin Marinas60ffc302012-03-05 11:49:27 +0000370
Will Deacon8fdbffb2017-11-14 14:20:21 +0000371 kernel_ventry 0, sync // Synchronous 64-bit EL0
372 kernel_ventry 0, irq // IRQ 64-bit EL0
373 kernel_ventry 0, fiq_invalid // FIQ 64-bit EL0
374 kernel_ventry 0, error_invalid // Error 64-bit EL0
Catalin Marinas60ffc302012-03-05 11:49:27 +0000375
376#ifdef CONFIG_COMPAT
Will Deacon8fdbffb2017-11-14 14:20:21 +0000377 kernel_ventry 0, sync_compat, 32 // Synchronous 32-bit EL0
378 kernel_ventry 0, irq_compat, 32 // IRQ 32-bit EL0
379 kernel_ventry 0, fiq_invalid_compat, 32 // FIQ 32-bit EL0
380 kernel_ventry 0, error_invalid_compat, 32 // Error 32-bit EL0
Catalin Marinas60ffc302012-03-05 11:49:27 +0000381#else
Will Deacon8fdbffb2017-11-14 14:20:21 +0000382 kernel_ventry 0, sync_invalid, 32 // Synchronous 32-bit EL0
383 kernel_ventry 0, irq_invalid, 32 // IRQ 32-bit EL0
384 kernel_ventry 0, fiq_invalid, 32 // FIQ 32-bit EL0
385 kernel_ventry 0, error_invalid, 32 // Error 32-bit EL0
Catalin Marinas60ffc302012-03-05 11:49:27 +0000386#endif
387END(vectors)
388
389/*
390 * Invalid mode handlers
391 */
392 .macro inv_entry, el, reason, regsize = 64
Ard Biesheuvelb6609502016-03-18 10:58:09 +0100393 kernel_entry \el, \regsize
Catalin Marinas60ffc302012-03-05 11:49:27 +0000394 mov x0, sp
395 mov x1, #\reason
396 mrs x2, esr_el1
397 b bad_mode
398 .endm
399
400el0_sync_invalid:
401 inv_entry 0, BAD_SYNC
402ENDPROC(el0_sync_invalid)
403
404el0_irq_invalid:
405 inv_entry 0, BAD_IRQ
406ENDPROC(el0_irq_invalid)
407
408el0_fiq_invalid:
409 inv_entry 0, BAD_FIQ
410ENDPROC(el0_fiq_invalid)
411
412el0_error_invalid:
413 inv_entry 0, BAD_ERROR
414ENDPROC(el0_error_invalid)
415
416#ifdef CONFIG_COMPAT
417el0_fiq_invalid_compat:
418 inv_entry 0, BAD_FIQ, 32
419ENDPROC(el0_fiq_invalid_compat)
420
421el0_error_invalid_compat:
422 inv_entry 0, BAD_ERROR, 32
423ENDPROC(el0_error_invalid_compat)
424#endif
425
426el1_sync_invalid:
427 inv_entry 1, BAD_SYNC
428ENDPROC(el1_sync_invalid)
429
430el1_irq_invalid:
431 inv_entry 1, BAD_IRQ
432ENDPROC(el1_irq_invalid)
433
434el1_fiq_invalid:
435 inv_entry 1, BAD_FIQ
436ENDPROC(el1_fiq_invalid)
437
438el1_error_invalid:
439 inv_entry 1, BAD_ERROR
440ENDPROC(el1_error_invalid)
441
442/*
443 * EL1 mode handlers.
444 */
445 .align 6
446el1_sync:
447 kernel_entry 1
448 mrs x1, esr_el1 // read the syndrome register
Mark Rutlandaed40e02014-11-24 12:31:40 +0000449 lsr x24, x1, #ESR_ELx_EC_SHIFT // exception class
450 cmp x24, #ESR_ELx_EC_DABT_CUR // data abort in EL1
Catalin Marinas60ffc302012-03-05 11:49:27 +0000451 b.eq el1_da
Laura Abbott9adeb8e2016-08-09 18:25:26 -0700452 cmp x24, #ESR_ELx_EC_IABT_CUR // instruction abort in EL1
453 b.eq el1_ia
Mark Rutlandaed40e02014-11-24 12:31:40 +0000454 cmp x24, #ESR_ELx_EC_SYS64 // configurable trap
Catalin Marinas60ffc302012-03-05 11:49:27 +0000455 b.eq el1_undef
Mark Rutlandaed40e02014-11-24 12:31:40 +0000456 cmp x24, #ESR_ELx_EC_SP_ALIGN // stack alignment exception
Catalin Marinas60ffc302012-03-05 11:49:27 +0000457 b.eq el1_sp_pc
Mark Rutlandaed40e02014-11-24 12:31:40 +0000458 cmp x24, #ESR_ELx_EC_PC_ALIGN // pc alignment exception
Catalin Marinas60ffc302012-03-05 11:49:27 +0000459 b.eq el1_sp_pc
Mark Rutlandaed40e02014-11-24 12:31:40 +0000460 cmp x24, #ESR_ELx_EC_UNKNOWN // unknown exception in EL1
Catalin Marinas60ffc302012-03-05 11:49:27 +0000461 b.eq el1_undef
Mark Rutlandaed40e02014-11-24 12:31:40 +0000462 cmp x24, #ESR_ELx_EC_BREAKPT_CUR // debug exception in EL1
Catalin Marinas60ffc302012-03-05 11:49:27 +0000463 b.ge el1_dbg
464 b el1_inv
Laura Abbott9adeb8e2016-08-09 18:25:26 -0700465
466el1_ia:
467 /*
468 * Fall through to the Data abort case
469 */
Catalin Marinas60ffc302012-03-05 11:49:27 +0000470el1_da:
471 /*
472 * Data abort handling
473 */
Kristina Martsenko9e09d902017-06-06 20:14:10 +0100474 mrs x3, far_el1
Will Deacon2a283072014-04-29 19:04:06 +0100475 enable_dbg
Catalin Marinas60ffc302012-03-05 11:49:27 +0000476 // re-enable interrupts if they were enabled in the aborted context
477 tbnz x23, #7, 1f // PSR_I_BIT
478 enable_irq
4791:
Kristina Martsenko9e09d902017-06-06 20:14:10 +0100480 clear_address_tag x0, x3
Catalin Marinas60ffc302012-03-05 11:49:27 +0000481 mov x2, sp // struct pt_regs
482 bl do_mem_abort
483
484 // disable interrupts before pulling preserved data off the stack
485 disable_irq
486 kernel_exit 1
487el1_sp_pc:
488 /*
489 * Stack or PC alignment exception handling
490 */
491 mrs x0, far_el1
Will Deacon2a283072014-04-29 19:04:06 +0100492 enable_dbg
Catalin Marinas60ffc302012-03-05 11:49:27 +0000493 mov x2, sp
494 b do_sp_pc_abort
495el1_undef:
496 /*
497 * Undefined instruction
498 */
Will Deacon2a283072014-04-29 19:04:06 +0100499 enable_dbg
Catalin Marinas60ffc302012-03-05 11:49:27 +0000500 mov x0, sp
501 b do_undefinstr
502el1_dbg:
503 /*
504 * Debug exception handling
505 */
Mark Rutlandaed40e02014-11-24 12:31:40 +0000506 cmp x24, #ESR_ELx_EC_BRK64 // if BRK64
Sandeepa Prabhuee6214c2013-12-04 05:50:20 +0000507 cinc x24, x24, eq // set bit '0'
Catalin Marinas60ffc302012-03-05 11:49:27 +0000508 tbz x24, #0, el1_inv // EL1 only
509 mrs x0, far_el1
510 mov x2, sp // struct pt_regs
511 bl do_debug_exception
Catalin Marinas60ffc302012-03-05 11:49:27 +0000512 kernel_exit 1
513el1_inv:
514 // TODO: add support for undefined instructions in kernel mode
Will Deacon2a283072014-04-29 19:04:06 +0100515 enable_dbg
Catalin Marinas60ffc302012-03-05 11:49:27 +0000516 mov x0, sp
Mark Rutland1b428042015-07-07 18:00:49 +0100517 mov x2, x1
Catalin Marinas60ffc302012-03-05 11:49:27 +0000518 mov x1, #BAD_SYNC
Catalin Marinas60ffc302012-03-05 11:49:27 +0000519 b bad_mode
520ENDPROC(el1_sync)
521
522 .align 6
523el1_irq:
524 kernel_entry 1
Will Deacon2a283072014-04-29 19:04:06 +0100525 enable_dbg
Catalin Marinas60ffc302012-03-05 11:49:27 +0000526#ifdef CONFIG_TRACE_IRQFLAGS
527 bl trace_hardirqs_off
528#endif
Marc Zyngier64681782013-11-12 17:11:53 +0000529
530 irq_handler
531
Catalin Marinas60ffc302012-03-05 11:49:27 +0000532#ifdef CONFIG_PREEMPT
Mark Rutland5b7e8f72016-11-03 20:23:13 +0000533 ldr w24, [tsk, #TSK_TI_PREEMPT] // get preempt count
Marc Zyngier717321f2013-11-04 20:14:58 +0000534 cbnz w24, 1f // preempt count != 0
Mark Rutland5b7e8f72016-11-03 20:23:13 +0000535 ldr x0, [tsk, #TSK_TI_FLAGS] // get flags
Catalin Marinas60ffc302012-03-05 11:49:27 +0000536 tbz x0, #TIF_NEED_RESCHED, 1f // needs rescheduling?
537 bl el1_preempt
5381:
539#endif
540#ifdef CONFIG_TRACE_IRQFLAGS
541 bl trace_hardirqs_on
542#endif
543 kernel_exit 1
544ENDPROC(el1_irq)
545
546#ifdef CONFIG_PREEMPT
547el1_preempt:
548 mov x24, lr
Will Deacon2a283072014-04-29 19:04:06 +01005491: bl preempt_schedule_irq // irq en/disable is done inside
Mark Rutland5b7e8f72016-11-03 20:23:13 +0000550 ldr x0, [tsk, #TSK_TI_FLAGS] // get new tasks TI_FLAGS
Catalin Marinas60ffc302012-03-05 11:49:27 +0000551 tbnz x0, #TIF_NEED_RESCHED, 1b // needs rescheduling?
552 ret x24
553#endif
554
555/*
556 * EL0 mode handlers.
557 */
558 .align 6
559el0_sync:
560 kernel_entry 0
561 mrs x25, esr_el1 // read the syndrome register
Mark Rutlandaed40e02014-11-24 12:31:40 +0000562 lsr x24, x25, #ESR_ELx_EC_SHIFT // exception class
563 cmp x24, #ESR_ELx_EC_SVC64 // SVC in 64-bit state
Catalin Marinas60ffc302012-03-05 11:49:27 +0000564 b.eq el0_svc
Mark Rutlandaed40e02014-11-24 12:31:40 +0000565 cmp x24, #ESR_ELx_EC_DABT_LOW // data abort in EL0
Catalin Marinas60ffc302012-03-05 11:49:27 +0000566 b.eq el0_da
Mark Rutlandaed40e02014-11-24 12:31:40 +0000567 cmp x24, #ESR_ELx_EC_IABT_LOW // instruction abort in EL0
Catalin Marinas60ffc302012-03-05 11:49:27 +0000568 b.eq el0_ia
Mark Rutlandaed40e02014-11-24 12:31:40 +0000569 cmp x24, #ESR_ELx_EC_FP_ASIMD // FP/ASIMD access
Catalin Marinas60ffc302012-03-05 11:49:27 +0000570 b.eq el0_fpsimd_acc
Mark Rutlandaed40e02014-11-24 12:31:40 +0000571 cmp x24, #ESR_ELx_EC_FP_EXC64 // FP/ASIMD exception
Catalin Marinas60ffc302012-03-05 11:49:27 +0000572 b.eq el0_fpsimd_exc
Mark Rutlandaed40e02014-11-24 12:31:40 +0000573 cmp x24, #ESR_ELx_EC_SYS64 // configurable trap
Andre Przywara7dd01ae2016-06-28 18:07:32 +0100574 b.eq el0_sys
Mark Rutlandaed40e02014-11-24 12:31:40 +0000575 cmp x24, #ESR_ELx_EC_SP_ALIGN // stack alignment exception
Catalin Marinas60ffc302012-03-05 11:49:27 +0000576 b.eq el0_sp_pc
Mark Rutlandaed40e02014-11-24 12:31:40 +0000577 cmp x24, #ESR_ELx_EC_PC_ALIGN // pc alignment exception
Catalin Marinas60ffc302012-03-05 11:49:27 +0000578 b.eq el0_sp_pc
Mark Rutlandaed40e02014-11-24 12:31:40 +0000579 cmp x24, #ESR_ELx_EC_UNKNOWN // unknown exception in EL0
Catalin Marinas60ffc302012-03-05 11:49:27 +0000580 b.eq el0_undef
Mark Rutlandaed40e02014-11-24 12:31:40 +0000581 cmp x24, #ESR_ELx_EC_BREAKPT_LOW // debug exception in EL0
Catalin Marinas60ffc302012-03-05 11:49:27 +0000582 b.ge el0_dbg
583 b el0_inv
584
585#ifdef CONFIG_COMPAT
586 .align 6
587el0_sync_compat:
588 kernel_entry 0, 32
589 mrs x25, esr_el1 // read the syndrome register
Mark Rutlandaed40e02014-11-24 12:31:40 +0000590 lsr x24, x25, #ESR_ELx_EC_SHIFT // exception class
591 cmp x24, #ESR_ELx_EC_SVC32 // SVC in 32-bit state
Catalin Marinas60ffc302012-03-05 11:49:27 +0000592 b.eq el0_svc_compat
Mark Rutlandaed40e02014-11-24 12:31:40 +0000593 cmp x24, #ESR_ELx_EC_DABT_LOW // data abort in EL0
Catalin Marinas60ffc302012-03-05 11:49:27 +0000594 b.eq el0_da
Mark Rutlandaed40e02014-11-24 12:31:40 +0000595 cmp x24, #ESR_ELx_EC_IABT_LOW // instruction abort in EL0
Catalin Marinas60ffc302012-03-05 11:49:27 +0000596 b.eq el0_ia
Mark Rutlandaed40e02014-11-24 12:31:40 +0000597 cmp x24, #ESR_ELx_EC_FP_ASIMD // FP/ASIMD access
Catalin Marinas60ffc302012-03-05 11:49:27 +0000598 b.eq el0_fpsimd_acc
Mark Rutlandaed40e02014-11-24 12:31:40 +0000599 cmp x24, #ESR_ELx_EC_FP_EXC32 // FP/ASIMD exception
Catalin Marinas60ffc302012-03-05 11:49:27 +0000600 b.eq el0_fpsimd_exc
Mark Salyzyn77f3228f2015-10-13 14:30:51 -0700601 cmp x24, #ESR_ELx_EC_PC_ALIGN // pc alignment exception
602 b.eq el0_sp_pc
Mark Rutlandaed40e02014-11-24 12:31:40 +0000603 cmp x24, #ESR_ELx_EC_UNKNOWN // unknown exception in EL0
Catalin Marinas60ffc302012-03-05 11:49:27 +0000604 b.eq el0_undef
Mark Rutlandaed40e02014-11-24 12:31:40 +0000605 cmp x24, #ESR_ELx_EC_CP15_32 // CP15 MRC/MCR trap
Mark Rutland381cc2b2013-05-24 12:02:35 +0100606 b.eq el0_undef
Mark Rutlandaed40e02014-11-24 12:31:40 +0000607 cmp x24, #ESR_ELx_EC_CP15_64 // CP15 MRRC/MCRR trap
Mark Rutland381cc2b2013-05-24 12:02:35 +0100608 b.eq el0_undef
Mark Rutlandaed40e02014-11-24 12:31:40 +0000609 cmp x24, #ESR_ELx_EC_CP14_MR // CP14 MRC/MCR trap
Mark Rutland381cc2b2013-05-24 12:02:35 +0100610 b.eq el0_undef
Mark Rutlandaed40e02014-11-24 12:31:40 +0000611 cmp x24, #ESR_ELx_EC_CP14_LS // CP14 LDC/STC trap
Mark Rutland381cc2b2013-05-24 12:02:35 +0100612 b.eq el0_undef
Mark Rutlandaed40e02014-11-24 12:31:40 +0000613 cmp x24, #ESR_ELx_EC_CP14_64 // CP14 MRRC/MCRR trap
Mark Rutland381cc2b2013-05-24 12:02:35 +0100614 b.eq el0_undef
Mark Rutlandaed40e02014-11-24 12:31:40 +0000615 cmp x24, #ESR_ELx_EC_BREAKPT_LOW // debug exception in EL0
Catalin Marinas60ffc302012-03-05 11:49:27 +0000616 b.ge el0_dbg
617 b el0_inv
618el0_svc_compat:
619 /*
620 * AArch32 syscall handling
621 */
Catalin Marinas01564112015-01-06 16:42:32 +0000622 adrp stbl, compat_sys_call_table // load compat syscall table pointer
Catalin Marinas60ffc302012-03-05 11:49:27 +0000623 uxtw scno, w7 // syscall number in w7 (r7)
624 mov sc_nr, #__NR_compat_syscalls
625 b el0_svc_naked
626
627 .align 6
628el0_irq_compat:
629 kernel_entry 0, 32
630 b el0_irq_naked
631#endif
632
633el0_da:
634 /*
635 * Data abort handling
636 */
Larry Bassel6ab64632014-05-30 20:34:14 +0100637 mrs x26, far_el1
Catalin Marinas60ffc302012-03-05 11:49:27 +0000638 // enable interrupts before calling the main handler
Will Deacon2a283072014-04-29 19:04:06 +0100639 enable_dbg_and_irq
Larry Bassel6c81fe72014-05-30 12:34:15 -0700640 ct_user_exit
Kristina Martsenko9e09d902017-06-06 20:14:10 +0100641 clear_address_tag x0, x26
Catalin Marinas60ffc302012-03-05 11:49:27 +0000642 mov x1, x25
643 mov x2, sp
Will Deacond54e81f2014-09-29 11:44:01 +0100644 bl do_mem_abort
645 b ret_to_user
Catalin Marinas60ffc302012-03-05 11:49:27 +0000646el0_ia:
647 /*
648 * Instruction abort handling
649 */
Larry Bassel6ab64632014-05-30 20:34:14 +0100650 mrs x26, far_el1
Mark Rutland47320012018-04-12 12:11:13 +0100651 msr daifclr, #(8 | 4 | 1)
652#ifdef CONFIG_TRACE_IRQFLAGS
653 bl trace_hardirqs_off
654#endif
Larry Bassel6c81fe72014-05-30 12:34:15 -0700655 ct_user_exit
Larry Bassel6ab64632014-05-30 20:34:14 +0100656 mov x0, x26
Mark Rutland541ec872016-05-31 12:33:03 +0100657 mov x1, x25
Catalin Marinas60ffc302012-03-05 11:49:27 +0000658 mov x2, sp
Mark Rutland47320012018-04-12 12:11:13 +0100659 bl do_el0_ia_bp_hardening
Will Deacond54e81f2014-09-29 11:44:01 +0100660 b ret_to_user
Catalin Marinas60ffc302012-03-05 11:49:27 +0000661el0_fpsimd_acc:
662 /*
663 * Floating Point or Advanced SIMD access
664 */
Will Deacon2a283072014-04-29 19:04:06 +0100665 enable_dbg
Larry Bassel6c81fe72014-05-30 12:34:15 -0700666 ct_user_exit
Catalin Marinas60ffc302012-03-05 11:49:27 +0000667 mov x0, x25
668 mov x1, sp
Will Deacond54e81f2014-09-29 11:44:01 +0100669 bl do_fpsimd_acc
670 b ret_to_user
Catalin Marinas60ffc302012-03-05 11:49:27 +0000671el0_fpsimd_exc:
672 /*
673 * Floating Point or Advanced SIMD exception
674 */
Will Deacon2a283072014-04-29 19:04:06 +0100675 enable_dbg
Larry Bassel6c81fe72014-05-30 12:34:15 -0700676 ct_user_exit
Catalin Marinas60ffc302012-03-05 11:49:27 +0000677 mov x0, x25
678 mov x1, sp
Will Deacond54e81f2014-09-29 11:44:01 +0100679 bl do_fpsimd_exc
680 b ret_to_user
Catalin Marinas60ffc302012-03-05 11:49:27 +0000681el0_sp_pc:
682 /*
683 * Stack or PC alignment exception handling
684 */
Larry Bassel6ab64632014-05-30 20:34:14 +0100685 mrs x26, far_el1
Mark Rutlande7c3b242018-04-12 12:11:17 +0100686 enable_dbg
687#ifdef CONFIG_TRACE_IRQFLAGS
688 bl trace_hardirqs_off
689#endif
Mark Rutland46b05672015-06-15 16:40:27 +0100690 ct_user_exit
Larry Bassel6ab64632014-05-30 20:34:14 +0100691 mov x0, x26
Catalin Marinas60ffc302012-03-05 11:49:27 +0000692 mov x1, x25
693 mov x2, sp
Will Deacond54e81f2014-09-29 11:44:01 +0100694 bl do_sp_pc_abort
695 b ret_to_user
Catalin Marinas60ffc302012-03-05 11:49:27 +0000696el0_undef:
697 /*
698 * Undefined instruction
699 */
Catalin Marinas2600e132013-08-22 11:47:37 +0100700 // enable interrupts before calling the main handler
Will Deacon2a283072014-04-29 19:04:06 +0100701 enable_dbg_and_irq
Larry Bassel6c81fe72014-05-30 12:34:15 -0700702 ct_user_exit
Will Deacon2a283072014-04-29 19:04:06 +0100703 mov x0, sp
Will Deacond54e81f2014-09-29 11:44:01 +0100704 bl do_undefinstr
705 b ret_to_user
Andre Przywara7dd01ae2016-06-28 18:07:32 +0100706el0_sys:
707 /*
708 * System instructions, for trapped cache maintenance instructions
709 */
710 enable_dbg_and_irq
711 ct_user_exit
712 mov x0, x25
713 mov x1, sp
714 bl do_sysinstr
715 b ret_to_user
Catalin Marinas60ffc302012-03-05 11:49:27 +0000716el0_dbg:
717 /*
718 * Debug exception handling
719 */
720 tbnz x24, #0, el0_inv // EL0 only
721 mrs x0, far_el1
Catalin Marinas60ffc302012-03-05 11:49:27 +0000722 mov x1, x25
723 mov x2, sp
Will Deacon2a283072014-04-29 19:04:06 +0100724 bl do_debug_exception
725 enable_dbg
Larry Bassel6c81fe72014-05-30 12:34:15 -0700726 ct_user_exit
Will Deacon2a283072014-04-29 19:04:06 +0100727 b ret_to_user
Catalin Marinas60ffc302012-03-05 11:49:27 +0000728el0_inv:
Will Deacon2a283072014-04-29 19:04:06 +0100729 enable_dbg
Larry Bassel6c81fe72014-05-30 12:34:15 -0700730 ct_user_exit
Catalin Marinas60ffc302012-03-05 11:49:27 +0000731 mov x0, sp
732 mov x1, #BAD_SYNC
Mark Rutland1b428042015-07-07 18:00:49 +0100733 mov x2, x25
Mark Rutlandde327942017-01-18 17:23:41 +0000734 bl bad_el0_sync
Will Deacond54e81f2014-09-29 11:44:01 +0100735 b ret_to_user
Catalin Marinas60ffc302012-03-05 11:49:27 +0000736ENDPROC(el0_sync)
737
738 .align 6
739el0_irq:
740 kernel_entry 0
741el0_irq_naked:
Catalin Marinas60ffc302012-03-05 11:49:27 +0000742 enable_dbg
743#ifdef CONFIG_TRACE_IRQFLAGS
744 bl trace_hardirqs_off
745#endif
Marc Zyngier64681782013-11-12 17:11:53 +0000746
Larry Bassel6c81fe72014-05-30 12:34:15 -0700747 ct_user_exit
Mark Rutland34dc20b2018-04-12 12:11:18 +0100748#ifdef CONFIG_HARDEN_BRANCH_PREDICTOR
749 tbz x22, #55, 1f
750 bl do_el0_irq_bp_hardening
7511:
752#endif
Catalin Marinas60ffc302012-03-05 11:49:27 +0000753 irq_handler
Marc Zyngier64681782013-11-12 17:11:53 +0000754
Catalin Marinas60ffc302012-03-05 11:49:27 +0000755#ifdef CONFIG_TRACE_IRQFLAGS
756 bl trace_hardirqs_on
757#endif
758 b ret_to_user
759ENDPROC(el0_irq)
760
761/*
Catalin Marinas60ffc302012-03-05 11:49:27 +0000762 * Register switch for AArch64. The callee-saved registers need to be saved
763 * and restored. On entry:
764 * x0 = previous task_struct (must be preserved across the switch)
765 * x1 = next task_struct
766 * Previous and next are guaranteed not to be the same.
767 *
768 */
769ENTRY(cpu_switch_to)
Will Deaconc0d3fce2015-07-20 15:14:53 +0100770 mov x10, #THREAD_CPU_CONTEXT
771 add x8, x0, x10
Catalin Marinas60ffc302012-03-05 11:49:27 +0000772 mov x9, sp
773 stp x19, x20, [x8], #16 // store callee-saved registers
774 stp x21, x22, [x8], #16
775 stp x23, x24, [x8], #16
776 stp x25, x26, [x8], #16
777 stp x27, x28, [x8], #16
778 stp x29, x9, [x8], #16
779 str lr, [x8]
Will Deaconc0d3fce2015-07-20 15:14:53 +0100780 add x8, x1, x10
Catalin Marinas60ffc302012-03-05 11:49:27 +0000781 ldp x19, x20, [x8], #16 // restore callee-saved registers
782 ldp x21, x22, [x8], #16
783 ldp x23, x24, [x8], #16
784 ldp x25, x26, [x8], #16
785 ldp x27, x28, [x8], #16
786 ldp x29, x9, [x8], #16
787 ldr lr, [x8]
788 mov sp, x9
Mark Rutland5b7e8f72016-11-03 20:23:13 +0000789 msr sp_el0, x1
Catalin Marinas60ffc302012-03-05 11:49:27 +0000790 ret
791ENDPROC(cpu_switch_to)
792
793/*
794 * This is the fast syscall return path. We do as little as possible here,
795 * and this includes saving x0 back into the kernel stack.
796 */
797ret_fast_syscall:
798 disable_irq // disable interrupts
Will Deacon412fcb62015-08-19 15:57:09 +0100799 str x0, [sp, #S_X0] // returned x0
Mark Rutland5b7e8f72016-11-03 20:23:13 +0000800 ldr x1, [tsk, #TSK_TI_FLAGS] // re-check for syscall tracing
Josh Stone04d7e092015-06-05 14:28:03 -0700801 and x2, x1, #_TIF_SYSCALL_WORK
802 cbnz x2, ret_fast_syscall_trace
Catalin Marinas60ffc302012-03-05 11:49:27 +0000803 and x2, x1, #_TIF_WORK_MASK
Will Deacon412fcb62015-08-19 15:57:09 +0100804 cbnz x2, work_pending
Will Deacon2a283072014-04-29 19:04:06 +0100805 enable_step_tsk x1, x2
Will Deacon412fcb62015-08-19 15:57:09 +0100806 kernel_exit 0
Josh Stone04d7e092015-06-05 14:28:03 -0700807ret_fast_syscall_trace:
808 enable_irq // enable interrupts
Will Deacon412fcb62015-08-19 15:57:09 +0100809 b __sys_trace_return_skipped // we already saved x0
Catalin Marinas60ffc302012-03-05 11:49:27 +0000810
811/*
812 * Ok, we need to do extra processing, enter the slow path.
813 */
Catalin Marinas60ffc302012-03-05 11:49:27 +0000814work_pending:
Catalin Marinas60ffc302012-03-05 11:49:27 +0000815 mov x0, sp // 'regs'
Catalin Marinas60ffc302012-03-05 11:49:27 +0000816 bl do_notify_resume
Catalin Marinasdb3899a2015-12-04 12:42:29 +0000817#ifdef CONFIG_TRACE_IRQFLAGS
Chris Metcalf421dd6f2016-07-14 16:48:14 -0400818 bl trace_hardirqs_on // enabled while in userspace
Catalin Marinasdb3899a2015-12-04 12:42:29 +0000819#endif
Mark Rutland5b7e8f72016-11-03 20:23:13 +0000820 ldr x1, [tsk, #TSK_TI_FLAGS] // re-check for single-step
Chris Metcalf421dd6f2016-07-14 16:48:14 -0400821 b finish_ret_to_user
Catalin Marinas60ffc302012-03-05 11:49:27 +0000822/*
823 * "slow" syscall return path.
824 */
Catalin Marinas59dc67b2012-09-10 16:11:46 +0100825ret_to_user:
Catalin Marinas60ffc302012-03-05 11:49:27 +0000826 disable_irq // disable interrupts
Mark Rutland5b7e8f72016-11-03 20:23:13 +0000827 ldr x1, [tsk, #TSK_TI_FLAGS]
Catalin Marinas60ffc302012-03-05 11:49:27 +0000828 and x2, x1, #_TIF_WORK_MASK
829 cbnz x2, work_pending
Chris Metcalf421dd6f2016-07-14 16:48:14 -0400830finish_ret_to_user:
Will Deacon2a283072014-04-29 19:04:06 +0100831 enable_step_tsk x1, x2
Will Deacon412fcb62015-08-19 15:57:09 +0100832 kernel_exit 0
Catalin Marinas60ffc302012-03-05 11:49:27 +0000833ENDPROC(ret_to_user)
834
835/*
836 * This is how we return from a fork.
837 */
838ENTRY(ret_from_fork)
839 bl schedule_tail
Catalin Marinasc34501d2012-10-05 12:31:20 +0100840 cbz x19, 1f // not a kernel thread
841 mov x0, x20
842 blr x19
8431: get_thread_info tsk
Catalin Marinas60ffc302012-03-05 11:49:27 +0000844 b ret_to_user
845ENDPROC(ret_from_fork)
846
847/*
848 * SVC handler.
849 */
850 .align 6
851el0_svc:
852 adrp stbl, sys_call_table // load syscall table pointer
853 uxtw scno, w8 // syscall number in w8
854 mov sc_nr, #__NR_syscalls
855el0_svc_naked: // compat entry point
856 stp x0, scno, [sp, #S_ORIG_X0] // save the original x0 and syscall number
Will Deacon2a283072014-04-29 19:04:06 +0100857 enable_dbg_and_irq
Larry Bassel6c81fe72014-05-30 12:34:15 -0700858 ct_user_exit 1
Catalin Marinas60ffc302012-03-05 11:49:27 +0000859
Mark Rutland5b7e8f72016-11-03 20:23:13 +0000860 ldr x16, [tsk, #TSK_TI_FLAGS] // check for syscall hooks
AKASHI Takahiro449f81a2014-04-30 10:51:29 +0100861 tst x16, #_TIF_SYSCALL_WORK
862 b.ne __sys_trace
Catalin Marinas60ffc302012-03-05 11:49:27 +0000863 cmp scno, sc_nr // check upper syscall limit
864 b.hs ni_sys
Mark Rutlandf3ed64a2018-04-12 12:11:02 +0100865 mask_nospec64 scno, sc_nr, x19 // enforce bounds for syscall number
Catalin Marinas60ffc302012-03-05 11:49:27 +0000866 ldr x16, [stbl, scno, lsl #3] // address in the syscall table
Will Deacond54e81f2014-09-29 11:44:01 +0100867 blr x16 // call sys_* routine
868 b ret_fast_syscall
Catalin Marinas60ffc302012-03-05 11:49:27 +0000869ni_sys:
870 mov x0, sp
Will Deacond54e81f2014-09-29 11:44:01 +0100871 bl do_ni_syscall
872 b ret_fast_syscall
Catalin Marinas60ffc302012-03-05 11:49:27 +0000873ENDPROC(el0_svc)
874
875 /*
876 * This is the really slow path. We're going to be doing context
877 * switches, and waiting for our parent to respond.
878 */
879__sys_trace:
AKASHI Takahiro1014c812014-11-28 05:26:35 +0000880 mov w0, #-1 // set default errno for
881 cmp scno, x0 // user-issued syscall(-1)
882 b.ne 1f
883 mov x0, #-ENOSYS
884 str x0, [sp, #S_X0]
8851: mov x0, sp
AKASHI Takahiro31578582014-04-30 10:51:30 +0100886 bl syscall_trace_enter
AKASHI Takahiro1014c812014-11-28 05:26:35 +0000887 cmp w0, #-1 // skip the syscall?
888 b.eq __sys_trace_return_skipped
Catalin Marinas60ffc302012-03-05 11:49:27 +0000889 uxtw scno, w0 // syscall number (possibly new)
890 mov x1, sp // pointer to regs
891 cmp scno, sc_nr // check upper syscall limit
Will Deacond54e81f2014-09-29 11:44:01 +0100892 b.hs __ni_sys_trace
Catalin Marinas60ffc302012-03-05 11:49:27 +0000893 ldp x0, x1, [sp] // restore the syscall args
894 ldp x2, x3, [sp, #S_X2]
895 ldp x4, x5, [sp, #S_X4]
896 ldp x6, x7, [sp, #S_X6]
897 ldr x16, [stbl, scno, lsl #3] // address in the syscall table
Will Deacond54e81f2014-09-29 11:44:01 +0100898 blr x16 // call sys_* routine
Catalin Marinas60ffc302012-03-05 11:49:27 +0000899
900__sys_trace_return:
AKASHI Takahiro1014c812014-11-28 05:26:35 +0000901 str x0, [sp, #S_X0] // save returned x0
902__sys_trace_return_skipped:
AKASHI Takahiro31578582014-04-30 10:51:30 +0100903 mov x0, sp
904 bl syscall_trace_exit
Catalin Marinas60ffc302012-03-05 11:49:27 +0000905 b ret_to_user
906
Will Deacond54e81f2014-09-29 11:44:01 +0100907__ni_sys_trace:
908 mov x0, sp
909 bl do_ni_syscall
910 b __sys_trace_return
911
Pratyush Anand888b3c82016-07-08 12:35:50 -0400912 .popsection // .entry.text
913
Will Deacona329b062017-11-14 14:07:40 +0000914#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
915/*
916 * Exception vectors trampoline.
917 */
918 .pushsection ".entry.tramp.text", "ax"
919
920 .macro tramp_map_kernel, tmp
921 mrs \tmp, ttbr1_el1
922 sub \tmp, \tmp, #(SWAPPER_DIR_SIZE + RESERVED_TTBR0_SIZE)
923 bic \tmp, \tmp, #USER_ASID_FLAG
924 msr ttbr1_el1, \tmp
Will Deacon04b77fe2017-11-14 14:29:19 +0000925#ifdef CONFIG_ARCH_MSM8996
926 /* ASID already in \tmp[63:48] */
927 movk \tmp, #:abs_g2_nc:(TRAMP_VALIAS >> 12)
928 movk \tmp, #:abs_g1_nc:(TRAMP_VALIAS >> 12)
929 /* 2MB boundary containing the vectors, so we nobble the walk cache */
930 movk \tmp, #:abs_g0_nc:((TRAMP_VALIAS & ~(SZ_2M - 1)) >> 12)
931 isb
932 tlbi vae1, \tmp
933 dsb nsh
934#endif /* CONFIG_ARCH_MSM8996 */
Will Deacona329b062017-11-14 14:07:40 +0000935 .endm
936
937 .macro tramp_unmap_kernel, tmp
938 mrs \tmp, ttbr1_el1
939 add \tmp, \tmp, #(SWAPPER_DIR_SIZE + RESERVED_TTBR0_SIZE)
940 orr \tmp, \tmp, #USER_ASID_FLAG
941 msr ttbr1_el1, \tmp
942 /*
Will Deacona80d8e22018-04-03 12:09:22 +0100943 * We avoid running the post_ttbr_update_workaround here because
944 * it's only needed by Cavium ThunderX, which requires KPTI to be
945 * disabled.
Will Deacona329b062017-11-14 14:07:40 +0000946 */
947 .endm
948
949 .macro tramp_ventry, regsize = 64
950 .align 7
9511:
952 .if \regsize == 64
953 msr tpidrro_el0, x30 // Restored in kernel_ventry
954 .endif
Will Deaconf6af5322018-04-03 12:09:11 +0100955 /*
956 * Defend against branch aliasing attacks by pushing a dummy
957 * entry onto the return stack and using a RET instruction to
958 * enter the full-fat kernel vectors.
959 */
Will Deacon75359362017-11-14 16:15:59 +0000960 bl 2f
961 b .
9622:
Will Deacona329b062017-11-14 14:07:40 +0000963 tramp_map_kernel x30
Will Deacon06fe41f2017-12-06 11:24:02 +0000964#ifdef CONFIG_RANDOMIZE_BASE
965 adr x30, tramp_vectors + PAGE_SIZE
Todd Poynorb328e522018-01-08 12:22:41 -0800966#ifndef CONFIG_ARCH_MSM8996
967 isb
968#endif
Will Deacon06fe41f2017-12-06 11:24:02 +0000969 ldr x30, [x30]
970#else
Will Deacona329b062017-11-14 14:07:40 +0000971 ldr x30, =vectors
Will Deacon06fe41f2017-12-06 11:24:02 +0000972#endif
Will Deacona329b062017-11-14 14:07:40 +0000973 prfm plil1strm, [x30, #(1b - tramp_vectors)]
974 msr vbar_el1, x30
975 add x30, x30, #(1b - tramp_vectors)
976 isb
Will Deacon75359362017-11-14 16:15:59 +0000977 ret
Will Deacona329b062017-11-14 14:07:40 +0000978 .endm
979
980 .macro tramp_exit, regsize = 64
981 adr x30, tramp_vectors
982 msr vbar_el1, x30
983 tramp_unmap_kernel x30
984 .if \regsize == 64
985 mrs x30, far_el1
986 .endif
987 eret
988 .endm
989
990 .align 11
991ENTRY(tramp_vectors)
992 .space 0x400
993
994 tramp_ventry
995 tramp_ventry
996 tramp_ventry
997 tramp_ventry
998
999 tramp_ventry 32
1000 tramp_ventry 32
1001 tramp_ventry 32
1002 tramp_ventry 32
1003END(tramp_vectors)
1004
1005ENTRY(tramp_exit_native)
1006 tramp_exit
1007END(tramp_exit_native)
1008
1009ENTRY(tramp_exit_compat)
1010 tramp_exit 32
1011END(tramp_exit_compat)
1012
1013 .ltorg
1014 .popsection // .entry.tramp.text
Will Deacon06fe41f2017-12-06 11:24:02 +00001015#ifdef CONFIG_RANDOMIZE_BASE
1016 .pushsection ".rodata", "a"
1017 .align PAGE_SHIFT
1018 .globl __entry_tramp_data_start
1019__entry_tramp_data_start:
1020 .quad vectors
1021 .popsection // .rodata
1022#endif /* CONFIG_RANDOMIZE_BASE */
Will Deacona329b062017-11-14 14:07:40 +00001023#endif /* CONFIG_UNMAP_KERNEL_AT_EL0 */
1024
Catalin Marinas60ffc302012-03-05 11:49:27 +00001025/*
1026 * Special system call wrappers.
1027 */
Catalin Marinas60ffc302012-03-05 11:49:27 +00001028ENTRY(sys_rt_sigreturn_wrapper)
1029 mov x0, sp
1030 b sys_rt_sigreturn
1031ENDPROC(sys_rt_sigreturn_wrapper)