OSDN Git Service

Merge 4.9.78 into android-4.9
[android-x86/kernel.git] / arch / arm64 / kernel / entry.S
1 /*
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
24 #include <asm/alternative.h>
25 #include <asm/assembler.h>
26 #include <asm/asm-offsets.h>
27 #include <asm/cpufeature.h>
28 #include <asm/errno.h>
29 #include <asm/esr.h>
30 #include <asm/irq.h>
31 #include <asm/memory.h>
32 #include <asm/mmu.h>
33 #include <asm/ptrace.h>
34 #include <asm/thread_info.h>
35 #include <asm/uaccess.h>
36 #include <asm/asm-uaccess.h>
37 #include <asm/unistd.h>
38
39 /*
40  * Context tracking subsystem.  Used to instrument transitions
41  * between user and kernel mode.
42  */
43         .macro ct_user_exit, syscall = 0
44 #ifdef CONFIG_CONTEXT_TRACKING
45         bl      context_tracking_user_exit
46         .if \syscall == 1
47         /*
48          * Save/restore needed during syscalls.  Restore syscall arguments from
49          * the values already saved on stack during kernel_entry.
50          */
51         ldp     x0, x1, [sp]
52         ldp     x2, x3, [sp, #S_X2]
53         ldp     x4, x5, [sp, #S_X4]
54         ldp     x6, x7, [sp, #S_X6]
55         .endif
56 #endif
57         .endm
58
59         .macro ct_user_enter
60 #ifdef CONFIG_CONTEXT_TRACKING
61         bl      context_tracking_user_enter
62 #endif
63         .endm
64
65 /*
66  * Bad Abort numbers
67  *-----------------
68  */
69 #define BAD_SYNC        0
70 #define BAD_IRQ         1
71 #define BAD_FIQ         2
72 #define BAD_ERROR       3
73
74         .macro kernel_ventry, el, label, regsize = 64
75         .align 7
76 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
77 alternative_if ARM64_UNMAP_KERNEL_AT_EL0
78         .if     \el == 0
79         .if     \regsize == 64
80         mrs     x30, tpidrro_el0
81         msr     tpidrro_el0, xzr
82         .else
83         mov     x30, xzr
84         .endif
85         .endif
86 alternative_else_nop_endif
87 #endif
88
89         sub     sp, sp, #S_FRAME_SIZE
90         b       el\()\el\()_\label
91         .endm
92
93         .macro tramp_alias, dst, sym
94         mov_q   \dst, TRAMP_VALIAS
95         add     \dst, \dst, #(\sym - .entry.tramp.text)
96         .endm
97
98         .macro  kernel_entry, el, regsize = 64
99         .if     \regsize == 32
100         mov     w0, w0                          // zero upper 32 bits of x0
101         .endif
102         stp     x0, x1, [sp, #16 * 0]
103         stp     x2, x3, [sp, #16 * 1]
104         stp     x4, x5, [sp, #16 * 2]
105         stp     x6, x7, [sp, #16 * 3]
106         stp     x8, x9, [sp, #16 * 4]
107         stp     x10, x11, [sp, #16 * 5]
108         stp     x12, x13, [sp, #16 * 6]
109         stp     x14, x15, [sp, #16 * 7]
110         stp     x16, x17, [sp, #16 * 8]
111         stp     x18, x19, [sp, #16 * 9]
112         stp     x20, x21, [sp, #16 * 10]
113         stp     x22, x23, [sp, #16 * 11]
114         stp     x24, x25, [sp, #16 * 12]
115         stp     x26, x27, [sp, #16 * 13]
116         stp     x28, x29, [sp, #16 * 14]
117
118         .if     \el == 0
119         mrs     x21, sp_el0
120         mov     tsk, sp
121         and     tsk, tsk, #~(THREAD_SIZE - 1)   // Ensure MDSCR_EL1.SS is clear,
122         ldr     x19, [tsk, #TI_FLAGS]           // since we can unmask debug
123         disable_step_tsk x19, x20               // exceptions when scheduling.
124
125         mov     x29, xzr                        // fp pointed to user-space
126         .else
127         add     x21, sp, #S_FRAME_SIZE
128         get_thread_info tsk
129         /* Save the task's original addr_limit and set USER_DS (TASK_SIZE_64) */
130         ldr     x20, [tsk, #TI_ADDR_LIMIT]
131         str     x20, [sp, #S_ORIG_ADDR_LIMIT]
132         mov     x20, #TASK_SIZE_64
133         str     x20, [tsk, #TI_ADDR_LIMIT]
134         /* No need to reset PSTATE.UAO, hardware's already set it to 0 for us */
135         .endif /* \el == 0 */
136         mrs     x22, elr_el1
137         mrs     x23, spsr_el1
138         stp     lr, x21, [sp, #S_LR]
139
140 #ifdef CONFIG_ARM64_SW_TTBR0_PAN
141         /*
142          * Set the TTBR0 PAN bit in SPSR. When the exception is taken from
143          * EL0, there is no need to check the state of TTBR0_EL1 since
144          * accesses are always enabled.
145          * Note that the meaning of this bit differs from the ARMv8.1 PAN
146          * feature as all TTBR0_EL1 accesses are disabled, not just those to
147          * user mappings.
148          */
149 alternative_if ARM64_HAS_PAN
150         b       1f                              // skip TTBR0 PAN
151 alternative_else_nop_endif
152
153         .if     \el != 0
154         mrs     x21, ttbr0_el1
155         tst     x21, #TTBR_ASID_MASK            // Check for the reserved ASID
156         orr     x23, x23, #PSR_PAN_BIT          // Set the emulated PAN in the saved SPSR
157         b.eq    1f                              // TTBR0 access already disabled
158         and     x23, x23, #~PSR_PAN_BIT         // Clear the emulated PAN in the saved SPSR
159         .endif
160
161         __uaccess_ttbr0_disable x21
162 1:
163 #endif
164
165         stp     x22, x23, [sp, #S_PC]
166
167         /*
168          * Set syscallno to -1 by default (overridden later if real syscall).
169          */
170         .if     \el == 0
171         mvn     x21, xzr
172         str     x21, [sp, #S_SYSCALLNO]
173         .endif
174
175         /*
176          * Set sp_el0 to current thread_info.
177          */
178         .if     \el == 0
179         msr     sp_el0, tsk
180         .endif
181
182         /*
183          * Registers that may be useful after this macro is invoked:
184          *
185          * x21 - aborted SP
186          * x22 - aborted PC
187          * x23 - aborted PSTATE
188         */
189         .endm
190
191         .macro  kernel_exit, el
192         .if     \el != 0
193         /* Restore the task's original addr_limit. */
194         ldr     x20, [sp, #S_ORIG_ADDR_LIMIT]
195         str     x20, [tsk, #TI_ADDR_LIMIT]
196
197         /* No need to restore UAO, it will be restored from SPSR_EL1 */
198         .endif
199
200         ldp     x21, x22, [sp, #S_PC]           // load ELR, SPSR
201         .if     \el == 0
202         ct_user_enter
203         .endif
204
205 #ifdef CONFIG_ARM64_SW_TTBR0_PAN
206         /*
207          * Restore access to TTBR0_EL1. If returning to EL0, no need for SPSR
208          * PAN bit checking.
209          */
210 alternative_if ARM64_HAS_PAN
211         b       2f                              // skip TTBR0 PAN
212 alternative_else_nop_endif
213
214         .if     \el != 0
215         tbnz    x22, #22, 1f                    // Skip re-enabling TTBR0 access if the PSR_PAN_BIT is set
216         .endif
217
218         __uaccess_ttbr0_enable x0, x1
219
220         .if     \el == 0
221         /*
222          * Enable errata workarounds only if returning to user. The only
223          * workaround currently required for TTBR0_EL1 changes are for the
224          * Cavium erratum 27456 (broadcast TLBI instructions may cause I-cache
225          * corruption).
226          */
227         bl      post_ttbr_update_workaround
228         .endif
229 1:
230         .if     \el != 0
231         and     x22, x22, #~PSR_PAN_BIT         // ARMv8.0 CPUs do not understand this bit
232         .endif
233 2:
234 #endif
235
236         .if     \el == 0
237         ldr     x23, [sp, #S_SP]                // load return stack pointer
238         msr     sp_el0, x23
239         tst     x22, #PSR_MODE32_BIT            // native task?
240         b.eq    3f
241
242 #ifdef CONFIG_ARM64_ERRATUM_845719
243 alternative_if ARM64_WORKAROUND_845719
244 #ifdef CONFIG_PID_IN_CONTEXTIDR
245         mrs     x29, contextidr_el1
246         msr     contextidr_el1, x29
247 #else
248         msr contextidr_el1, xzr
249 #endif
250 alternative_else_nop_endif
251 #endif
252 3:
253         .endif
254
255         msr     elr_el1, x21                    // set up the return data
256         msr     spsr_el1, x22
257         ldp     x0, x1, [sp, #16 * 0]
258         ldp     x2, x3, [sp, #16 * 1]
259         ldp     x4, x5, [sp, #16 * 2]
260         ldp     x6, x7, [sp, #16 * 3]
261         ldp     x8, x9, [sp, #16 * 4]
262         ldp     x10, x11, [sp, #16 * 5]
263         ldp     x12, x13, [sp, #16 * 6]
264         ldp     x14, x15, [sp, #16 * 7]
265         ldp     x16, x17, [sp, #16 * 8]
266         ldp     x18, x19, [sp, #16 * 9]
267         ldp     x20, x21, [sp, #16 * 10]
268         ldp     x22, x23, [sp, #16 * 11]
269         ldp     x24, x25, [sp, #16 * 12]
270         ldp     x26, x27, [sp, #16 * 13]
271         ldp     x28, x29, [sp, #16 * 14]
272         ldr     lr, [sp, #S_LR]
273         add     sp, sp, #S_FRAME_SIZE           // restore sp
274
275         .if     \el == 0
276 alternative_insn eret, nop, ARM64_UNMAP_KERNEL_AT_EL0
277 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
278         bne     4f
279         msr     far_el1, x30
280         tramp_alias     x30, tramp_exit_native
281         br      x30
282 4:
283         tramp_alias     x30, tramp_exit_compat
284         br      x30
285 #endif
286         .else
287         eret
288         .endif
289         .endm
290
291         .macro  irq_stack_entry
292         mov     x19, sp                 // preserve the original sp
293
294         /*
295          * Compare sp with the current thread_info, if the top
296          * ~(THREAD_SIZE - 1) bits match, we are on a task stack, and
297          * should switch to the irq stack.
298          */
299         and     x25, x19, #~(THREAD_SIZE - 1)
300         cmp     x25, tsk
301         b.ne    9998f
302
303         this_cpu_ptr irq_stack, x25, x26
304         mov     x26, #IRQ_STACK_START_SP
305         add     x26, x25, x26
306
307         /* switch to the irq stack */
308         mov     sp, x26
309
310         /*
311          * Add a dummy stack frame, this non-standard format is fixed up
312          * by unwind_frame()
313          */
314         stp     x29, x19, [sp, #-16]!
315         mov     x29, sp
316
317 9998:
318         .endm
319
320         /*
321          * x19 should be preserved between irq_stack_entry and
322          * irq_stack_exit.
323          */
324         .macro  irq_stack_exit
325         mov     sp, x19
326         .endm
327
328 /*
329  * These are the registers used in the syscall handler, and allow us to
330  * have in theory up to 7 arguments to a function - x0 to x6.
331  *
332  * x7 is reserved for the system call number in 32-bit mode.
333  */
334 sc_nr   .req    x25             // number of system calls
335 scno    .req    x26             // syscall number
336 stbl    .req    x27             // syscall table pointer
337 tsk     .req    x28             // current thread_info
338
339 /*
340  * Interrupt handling.
341  */
342         .macro  irq_handler
343         ldr_l   x1, handle_arch_irq
344         mov     x0, sp
345         irq_stack_entry
346         blr     x1
347         irq_stack_exit
348         .endm
349
350         .text
351
352 /*
353  * Exception vectors.
354  */
355         .pushsection ".entry.text", "ax"
356
357         .align  11
358 ENTRY(vectors)
359         kernel_ventry   1, sync_invalid                 // Synchronous EL1t
360         kernel_ventry   1, irq_invalid                  // IRQ EL1t
361         kernel_ventry   1, fiq_invalid                  // FIQ EL1t
362         kernel_ventry   1, error_invalid                // Error EL1t
363
364         kernel_ventry   1, sync                         // Synchronous EL1h
365         kernel_ventry   1, irq                          // IRQ EL1h
366         kernel_ventry   1, fiq_invalid                  // FIQ EL1h
367         kernel_ventry   1, error_invalid                // Error EL1h
368
369         kernel_ventry   0, sync                         // Synchronous 64-bit EL0
370         kernel_ventry   0, irq                          // IRQ 64-bit EL0
371         kernel_ventry   0, fiq_invalid                  // FIQ 64-bit EL0
372         kernel_ventry   0, error_invalid                // Error 64-bit EL0
373
374 #ifdef CONFIG_COMPAT
375         kernel_ventry   0, sync_compat, 32              // Synchronous 32-bit EL0
376         kernel_ventry   0, irq_compat, 32               // IRQ 32-bit EL0
377         kernel_ventry   0, fiq_invalid_compat, 32       // FIQ 32-bit EL0
378         kernel_ventry   0, error_invalid_compat, 32     // Error 32-bit EL0
379 #else
380         kernel_ventry   0, sync_invalid, 32             // Synchronous 32-bit EL0
381         kernel_ventry   0, irq_invalid, 32              // IRQ 32-bit EL0
382         kernel_ventry   0, fiq_invalid, 32              // FIQ 32-bit EL0
383         kernel_ventry   0, error_invalid, 32            // Error 32-bit EL0
384 #endif
385 END(vectors)
386
387 /*
388  * Invalid mode handlers
389  */
390         .macro  inv_entry, el, reason, regsize = 64
391         kernel_entry \el, \regsize
392         mov     x0, sp
393         mov     x1, #\reason
394         mrs     x2, esr_el1
395         b       bad_mode
396         .endm
397
398 el0_sync_invalid:
399         inv_entry 0, BAD_SYNC
400 ENDPROC(el0_sync_invalid)
401
402 el0_irq_invalid:
403         inv_entry 0, BAD_IRQ
404 ENDPROC(el0_irq_invalid)
405
406 el0_fiq_invalid:
407         inv_entry 0, BAD_FIQ
408 ENDPROC(el0_fiq_invalid)
409
410 el0_error_invalid:
411         inv_entry 0, BAD_ERROR
412 ENDPROC(el0_error_invalid)
413
414 #ifdef CONFIG_COMPAT
415 el0_fiq_invalid_compat:
416         inv_entry 0, BAD_FIQ, 32
417 ENDPROC(el0_fiq_invalid_compat)
418
419 el0_error_invalid_compat:
420         inv_entry 0, BAD_ERROR, 32
421 ENDPROC(el0_error_invalid_compat)
422 #endif
423
424 el1_sync_invalid:
425         inv_entry 1, BAD_SYNC
426 ENDPROC(el1_sync_invalid)
427
428 el1_irq_invalid:
429         inv_entry 1, BAD_IRQ
430 ENDPROC(el1_irq_invalid)
431
432 el1_fiq_invalid:
433         inv_entry 1, BAD_FIQ
434 ENDPROC(el1_fiq_invalid)
435
436 el1_error_invalid:
437         inv_entry 1, BAD_ERROR
438 ENDPROC(el1_error_invalid)
439
440 /*
441  * EL1 mode handlers.
442  */
443         .align  6
444 el1_sync:
445         kernel_entry 1
446         mrs     x1, esr_el1                     // read the syndrome register
447         lsr     x24, x1, #ESR_ELx_EC_SHIFT      // exception class
448         cmp     x24, #ESR_ELx_EC_DABT_CUR       // data abort in EL1
449         b.eq    el1_da
450         cmp     x24, #ESR_ELx_EC_IABT_CUR       // instruction abort in EL1
451         b.eq    el1_ia
452         cmp     x24, #ESR_ELx_EC_SYS64          // configurable trap
453         b.eq    el1_undef
454         cmp     x24, #ESR_ELx_EC_SP_ALIGN       // stack alignment exception
455         b.eq    el1_sp_pc
456         cmp     x24, #ESR_ELx_EC_PC_ALIGN       // pc alignment exception
457         b.eq    el1_sp_pc
458         cmp     x24, #ESR_ELx_EC_UNKNOWN        // unknown exception in EL1
459         b.eq    el1_undef
460         cmp     x24, #ESR_ELx_EC_BREAKPT_CUR    // debug exception in EL1
461         b.ge    el1_dbg
462         b       el1_inv
463
464 el1_ia:
465         /*
466          * Fall through to the Data abort case
467          */
468 el1_da:
469         /*
470          * Data abort handling
471          */
472         mrs     x3, far_el1
473         enable_dbg
474         // re-enable interrupts if they were enabled in the aborted context
475         tbnz    x23, #7, 1f                     // PSR_I_BIT
476         enable_irq
477 1:
478         clear_address_tag x0, x3
479         mov     x2, sp                          // struct pt_regs
480         bl      do_mem_abort
481
482         // disable interrupts before pulling preserved data off the stack
483         disable_irq
484         kernel_exit 1
485 el1_sp_pc:
486         /*
487          * Stack or PC alignment exception handling
488          */
489         mrs     x0, far_el1
490         enable_dbg
491         mov     x2, sp
492         b       do_sp_pc_abort
493 el1_undef:
494         /*
495          * Undefined instruction
496          */
497         enable_dbg
498         mov     x0, sp
499         b       do_undefinstr
500 el1_dbg:
501         /*
502          * Debug exception handling
503          */
504         cmp     x24, #ESR_ELx_EC_BRK64          // if BRK64
505         cinc    x24, x24, eq                    // set bit '0'
506         tbz     x24, #0, el1_inv                // EL1 only
507         mrs     x0, far_el1
508         mov     x2, sp                          // struct pt_regs
509         bl      do_debug_exception
510         kernel_exit 1
511 el1_inv:
512         // TODO: add support for undefined instructions in kernel mode
513         enable_dbg
514         mov     x0, sp
515         mov     x2, x1
516         mov     x1, #BAD_SYNC
517         b       bad_mode
518 ENDPROC(el1_sync)
519
520         .align  6
521 el1_irq:
522         kernel_entry 1
523         enable_dbg
524 #ifdef CONFIG_TRACE_IRQFLAGS
525         bl      trace_hardirqs_off
526 #endif
527
528         irq_handler
529
530 #ifdef CONFIG_PREEMPT
531         ldr     w24, [tsk, #TI_PREEMPT]         // get preempt count
532         cbnz    w24, 1f                         // preempt count != 0
533         ldr     x0, [tsk, #TI_FLAGS]            // get flags
534         tbz     x0, #TIF_NEED_RESCHED, 1f       // needs rescheduling?
535         bl      el1_preempt
536 1:
537 #endif
538 #ifdef CONFIG_TRACE_IRQFLAGS
539         bl      trace_hardirqs_on
540 #endif
541         kernel_exit 1
542 ENDPROC(el1_irq)
543
544 #ifdef CONFIG_PREEMPT
545 el1_preempt:
546         mov     x24, lr
547 1:      bl      preempt_schedule_irq            // irq en/disable is done inside
548         ldr     x0, [tsk, #TI_FLAGS]            // get new tasks TI_FLAGS
549         tbnz    x0, #TIF_NEED_RESCHED, 1b       // needs rescheduling?
550         ret     x24
551 #endif
552
553 /*
554  * EL0 mode handlers.
555  */
556         .align  6
557 el0_sync:
558         kernel_entry 0
559         mrs     x25, esr_el1                    // read the syndrome register
560         lsr     x24, x25, #ESR_ELx_EC_SHIFT     // exception class
561         cmp     x24, #ESR_ELx_EC_SVC64          // SVC in 64-bit state
562         b.eq    el0_svc
563         cmp     x24, #ESR_ELx_EC_DABT_LOW       // data abort in EL0
564         b.eq    el0_da
565         cmp     x24, #ESR_ELx_EC_IABT_LOW       // instruction abort in EL0
566         b.eq    el0_ia
567         cmp     x24, #ESR_ELx_EC_FP_ASIMD       // FP/ASIMD access
568         b.eq    el0_fpsimd_acc
569         cmp     x24, #ESR_ELx_EC_FP_EXC64       // FP/ASIMD exception
570         b.eq    el0_fpsimd_exc
571         cmp     x24, #ESR_ELx_EC_SYS64          // configurable trap
572         b.eq    el0_sys
573         cmp     x24, #ESR_ELx_EC_SP_ALIGN       // stack alignment exception
574         b.eq    el0_sp_pc
575         cmp     x24, #ESR_ELx_EC_PC_ALIGN       // pc alignment exception
576         b.eq    el0_sp_pc
577         cmp     x24, #ESR_ELx_EC_UNKNOWN        // unknown exception in EL0
578         b.eq    el0_undef
579         cmp     x24, #ESR_ELx_EC_BREAKPT_LOW    // debug exception in EL0
580         b.ge    el0_dbg
581         b       el0_inv
582
583 #ifdef CONFIG_COMPAT
584         .align  6
585 el0_sync_compat:
586         kernel_entry 0, 32
587         mrs     x25, esr_el1                    // read the syndrome register
588         lsr     x24, x25, #ESR_ELx_EC_SHIFT     // exception class
589         cmp     x24, #ESR_ELx_EC_SVC32          // SVC in 32-bit state
590         b.eq    el0_svc_compat
591         cmp     x24, #ESR_ELx_EC_DABT_LOW       // data abort in EL0
592         b.eq    el0_da
593         cmp     x24, #ESR_ELx_EC_IABT_LOW       // instruction abort in EL0
594         b.eq    el0_ia
595         cmp     x24, #ESR_ELx_EC_FP_ASIMD       // FP/ASIMD access
596         b.eq    el0_fpsimd_acc
597         cmp     x24, #ESR_ELx_EC_FP_EXC32       // FP/ASIMD exception
598         b.eq    el0_fpsimd_exc
599         cmp     x24, #ESR_ELx_EC_PC_ALIGN       // pc alignment exception
600         b.eq    el0_sp_pc
601         cmp     x24, #ESR_ELx_EC_UNKNOWN        // unknown exception in EL0
602         b.eq    el0_undef
603         cmp     x24, #ESR_ELx_EC_CP15_32        // CP15 MRC/MCR trap
604         b.eq    el0_undef
605         cmp     x24, #ESR_ELx_EC_CP15_64        // CP15 MRRC/MCRR trap
606         b.eq    el0_undef
607         cmp     x24, #ESR_ELx_EC_CP14_MR        // CP14 MRC/MCR trap
608         b.eq    el0_undef
609         cmp     x24, #ESR_ELx_EC_CP14_LS        // CP14 LDC/STC trap
610         b.eq    el0_undef
611         cmp     x24, #ESR_ELx_EC_CP14_64        // CP14 MRRC/MCRR trap
612         b.eq    el0_undef
613         cmp     x24, #ESR_ELx_EC_BREAKPT_LOW    // debug exception in EL0
614         b.ge    el0_dbg
615         b       el0_inv
616 el0_svc_compat:
617         /*
618          * AArch32 syscall handling
619          */
620         adrp    stbl, compat_sys_call_table     // load compat syscall table pointer
621         uxtw    scno, w7                        // syscall number in w7 (r7)
622         mov     sc_nr, #__NR_compat_syscalls
623         b       el0_svc_naked
624
625         .align  6
626 el0_irq_compat:
627         kernel_entry 0, 32
628         b       el0_irq_naked
629 #endif
630
631 el0_da:
632         /*
633          * Data abort handling
634          */
635         mrs     x26, far_el1
636         // enable interrupts before calling the main handler
637         enable_dbg_and_irq
638         ct_user_exit
639         clear_address_tag x0, x26
640         mov     x1, x25
641         mov     x2, sp
642         bl      do_mem_abort
643         b       ret_to_user
644 el0_ia:
645         /*
646          * Instruction abort handling
647          */
648         mrs     x26, far_el1
649         // enable interrupts before calling the main handler
650         enable_dbg_and_irq
651         ct_user_exit
652         mov     x0, x26
653         mov     x1, x25
654         mov     x2, sp
655         bl      do_mem_abort
656         b       ret_to_user
657 el0_fpsimd_acc:
658         /*
659          * Floating Point or Advanced SIMD access
660          */
661         enable_dbg
662         ct_user_exit
663         mov     x0, x25
664         mov     x1, sp
665         bl      do_fpsimd_acc
666         b       ret_to_user
667 el0_fpsimd_exc:
668         /*
669          * Floating Point or Advanced SIMD exception
670          */
671         enable_dbg
672         ct_user_exit
673         mov     x0, x25
674         mov     x1, sp
675         bl      do_fpsimd_exc
676         b       ret_to_user
677 el0_sp_pc:
678         /*
679          * Stack or PC alignment exception handling
680          */
681         mrs     x26, far_el1
682         // enable interrupts before calling the main handler
683         enable_dbg_and_irq
684         ct_user_exit
685         mov     x0, x26
686         mov     x1, x25
687         mov     x2, sp
688         bl      do_sp_pc_abort
689         b       ret_to_user
690 el0_undef:
691         /*
692          * Undefined instruction
693          */
694         // enable interrupts before calling the main handler
695         enable_dbg_and_irq
696         ct_user_exit
697         mov     x0, sp
698         bl      do_undefinstr
699         b       ret_to_user
700 el0_sys:
701         /*
702          * System instructions, for trapped cache maintenance instructions
703          */
704         enable_dbg_and_irq
705         ct_user_exit
706         mov     x0, x25
707         mov     x1, sp
708         bl      do_sysinstr
709         b       ret_to_user
710 el0_dbg:
711         /*
712          * Debug exception handling
713          */
714         tbnz    x24, #0, el0_inv                // EL0 only
715         mrs     x0, far_el1
716         mov     x1, x25
717         mov     x2, sp
718         bl      do_debug_exception
719         enable_dbg
720         ct_user_exit
721         b       ret_to_user
722 el0_inv:
723         enable_dbg
724         ct_user_exit
725         mov     x0, sp
726         mov     x1, #BAD_SYNC
727         mov     x2, x25
728         bl      bad_el0_sync
729         b       ret_to_user
730 ENDPROC(el0_sync)
731
732         .align  6
733 el0_irq:
734         kernel_entry 0
735 el0_irq_naked:
736         enable_dbg
737 #ifdef CONFIG_TRACE_IRQFLAGS
738         bl      trace_hardirqs_off
739 #endif
740
741         ct_user_exit
742         irq_handler
743
744 #ifdef CONFIG_TRACE_IRQFLAGS
745         bl      trace_hardirqs_on
746 #endif
747         b       ret_to_user
748 ENDPROC(el0_irq)
749
750 /*
751  * Register switch for AArch64. The callee-saved registers need to be saved
752  * and restored. On entry:
753  *   x0 = previous task_struct (must be preserved across the switch)
754  *   x1 = next task_struct
755  * Previous and next are guaranteed not to be the same.
756  *
757  */
758 ENTRY(cpu_switch_to)
759         mov     x10, #THREAD_CPU_CONTEXT
760         add     x8, x0, x10
761         mov     x9, sp
762         stp     x19, x20, [x8], #16             // store callee-saved registers
763         stp     x21, x22, [x8], #16
764         stp     x23, x24, [x8], #16
765         stp     x25, x26, [x8], #16
766         stp     x27, x28, [x8], #16
767         stp     x29, x9, [x8], #16
768         str     lr, [x8]
769         add     x8, x1, x10
770         ldp     x19, x20, [x8], #16             // restore callee-saved registers
771         ldp     x21, x22, [x8], #16
772         ldp     x23, x24, [x8], #16
773         ldp     x25, x26, [x8], #16
774         ldp     x27, x28, [x8], #16
775         ldp     x29, x9, [x8], #16
776         ldr     lr, [x8]
777         mov     sp, x9
778         and     x9, x9, #~(THREAD_SIZE - 1)
779         msr     sp_el0, x9
780         ret
781 ENDPROC(cpu_switch_to)
782
783 /*
784  * This is the fast syscall return path.  We do as little as possible here,
785  * and this includes saving x0 back into the kernel stack.
786  */
787 ret_fast_syscall:
788         disable_irq                             // disable interrupts
789         str     x0, [sp, #S_X0]                 // returned x0
790         ldr     x1, [tsk, #TI_FLAGS]            // re-check for syscall tracing
791         and     x2, x1, #_TIF_SYSCALL_WORK
792         cbnz    x2, ret_fast_syscall_trace
793         and     x2, x1, #_TIF_WORK_MASK
794         cbnz    x2, work_pending
795         enable_step_tsk x1, x2
796         kernel_exit 0
797 ret_fast_syscall_trace:
798         enable_irq                              // enable interrupts
799         b       __sys_trace_return_skipped      // we already saved x0
800
801 /*
802  * Ok, we need to do extra processing, enter the slow path.
803  */
804 work_pending:
805         mov     x0, sp                          // 'regs'
806         bl      do_notify_resume
807 #ifdef CONFIG_TRACE_IRQFLAGS
808         bl      trace_hardirqs_on               // enabled while in userspace
809 #endif
810         ldr     x1, [tsk, #TI_FLAGS]            // re-check for single-step
811         b       finish_ret_to_user
812 /*
813  * "slow" syscall return path.
814  */
815 ret_to_user:
816         disable_irq                             // disable interrupts
817         ldr     x1, [tsk, #TI_FLAGS]
818         and     x2, x1, #_TIF_WORK_MASK
819         cbnz    x2, work_pending
820 finish_ret_to_user:
821         enable_step_tsk x1, x2
822         kernel_exit 0
823 ENDPROC(ret_to_user)
824
825 /*
826  * This is how we return from a fork.
827  */
828 ENTRY(ret_from_fork)
829         bl      schedule_tail
830         cbz     x19, 1f                         // not a kernel thread
831         mov     x0, x20
832         blr     x19
833 1:      get_thread_info tsk
834         b       ret_to_user
835 ENDPROC(ret_from_fork)
836
837 /*
838  * SVC handler.
839  */
840         .align  6
841 el0_svc:
842         adrp    stbl, sys_call_table            // load syscall table pointer
843         uxtw    scno, w8                        // syscall number in w8
844         mov     sc_nr, #__NR_syscalls
845 el0_svc_naked:                                  // compat entry point
846         stp     x0, scno, [sp, #S_ORIG_X0]      // save the original x0 and syscall number
847         enable_dbg_and_irq
848         ct_user_exit 1
849
850         ldr     x16, [tsk, #TI_FLAGS]           // check for syscall hooks
851         tst     x16, #_TIF_SYSCALL_WORK
852         b.ne    __sys_trace
853         cmp     scno, sc_nr                     // check upper syscall limit
854         b.hs    ni_sys
855         ldr     x16, [stbl, scno, lsl #3]       // address in the syscall table
856         blr     x16                             // call sys_* routine
857         b       ret_fast_syscall
858 ni_sys:
859         mov     x0, sp
860         bl      do_ni_syscall
861         b       ret_fast_syscall
862 ENDPROC(el0_svc)
863
864         /*
865          * This is the really slow path.  We're going to be doing context
866          * switches, and waiting for our parent to respond.
867          */
868 __sys_trace:
869         mov     w0, #-1                         // set default errno for
870         cmp     scno, x0                        // user-issued syscall(-1)
871         b.ne    1f
872         mov     x0, #-ENOSYS
873         str     x0, [sp, #S_X0]
874 1:      mov     x0, sp
875         bl      syscall_trace_enter
876         cmp     w0, #-1                         // skip the syscall?
877         b.eq    __sys_trace_return_skipped
878         uxtw    scno, w0                        // syscall number (possibly new)
879         mov     x1, sp                          // pointer to regs
880         cmp     scno, sc_nr                     // check upper syscall limit
881         b.hs    __ni_sys_trace
882         ldp     x0, x1, [sp]                    // restore the syscall args
883         ldp     x2, x3, [sp, #S_X2]
884         ldp     x4, x5, [sp, #S_X4]
885         ldp     x6, x7, [sp, #S_X6]
886         ldr     x16, [stbl, scno, lsl #3]       // address in the syscall table
887         blr     x16                             // call sys_* routine
888
889 __sys_trace_return:
890         str     x0, [sp, #S_X0]                 // save returned x0
891 __sys_trace_return_skipped:
892         mov     x0, sp
893         bl      syscall_trace_exit
894         b       ret_to_user
895
896 __ni_sys_trace:
897         mov     x0, sp
898         bl      do_ni_syscall
899         b       __sys_trace_return
900
901         .popsection                             // .entry.text
902
903 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
904 /*
905  * Exception vectors trampoline.
906  */
907         .pushsection ".entry.tramp.text", "ax"
908
909         .macro tramp_map_kernel, tmp
910         mrs     \tmp, ttbr1_el1
911         sub     \tmp, \tmp, #(SWAPPER_DIR_SIZE + RESERVED_TTBR0_SIZE)
912         bic     \tmp, \tmp, #USER_ASID_FLAG
913         msr     ttbr1_el1, \tmp
914 #ifdef CONFIG_ARCH_MSM8996
915         /* ASID already in \tmp[63:48] */
916         movk    \tmp, #:abs_g2_nc:(TRAMP_VALIAS >> 12)
917         movk    \tmp, #:abs_g1_nc:(TRAMP_VALIAS >> 12)
918         /* 2MB boundary containing the vectors, so we nobble the walk cache */
919         movk    \tmp, #:abs_g0_nc:((TRAMP_VALIAS & ~(SZ_2M - 1)) >> 12)
920         isb
921         tlbi    vae1, \tmp
922         dsb     nsh
923 #endif /* CONFIG_ARCH_MSM8996 */
924         .endm
925
926         .macro tramp_unmap_kernel, tmp
927         mrs     \tmp, ttbr1_el1
928         add     \tmp, \tmp, #(SWAPPER_DIR_SIZE + RESERVED_TTBR0_SIZE)
929         orr     \tmp, \tmp, #USER_ASID_FLAG
930         msr     ttbr1_el1, \tmp
931         /*
932          * We avoid running the post_ttbr_update_workaround here because the
933          * user and kernel ASIDs don't have conflicting mappings, so any
934          * "blessing" as described in:
935          *
936          *   http://lkml.kernel.org/r/56BB848A.6060603@caviumnetworks.com
937          *
938          * will not hurt correctness. Whilst this may partially defeat the
939          * point of using split ASIDs in the first place, it avoids
940          * the hit of invalidating the entire I-cache on every return to
941          * userspace.
942          */
943         .endm
944
945         .macro tramp_ventry, regsize = 64
946         .align  7
947 1:
948         .if     \regsize == 64
949         msr     tpidrro_el0, x30        // Restored in kernel_ventry
950         .endif
951         bl      2f
952         b       .
953 2:
954         tramp_map_kernel        x30
955 #ifdef CONFIG_RANDOMIZE_BASE
956         adr     x30, tramp_vectors + PAGE_SIZE
957 #ifndef CONFIG_ARCH_MSM8996
958         isb
959 #endif
960         ldr     x30, [x30]
961 #else
962         ldr     x30, =vectors
963 #endif
964         prfm    plil1strm, [x30, #(1b - tramp_vectors)]
965         msr     vbar_el1, x30
966         add     x30, x30, #(1b - tramp_vectors)
967         isb
968         ret
969         .endm
970
971         .macro tramp_exit, regsize = 64
972         adr     x30, tramp_vectors
973         msr     vbar_el1, x30
974         tramp_unmap_kernel      x30
975         .if     \regsize == 64
976         mrs     x30, far_el1
977         .endif
978         eret
979         .endm
980
981         .align  11
982 ENTRY(tramp_vectors)
983         .space  0x400
984
985         tramp_ventry
986         tramp_ventry
987         tramp_ventry
988         tramp_ventry
989
990         tramp_ventry    32
991         tramp_ventry    32
992         tramp_ventry    32
993         tramp_ventry    32
994 END(tramp_vectors)
995
996 ENTRY(tramp_exit_native)
997         tramp_exit
998 END(tramp_exit_native)
999
1000 ENTRY(tramp_exit_compat)
1001         tramp_exit      32
1002 END(tramp_exit_compat)
1003
1004         .ltorg
1005         .popsection                             // .entry.tramp.text
1006 #ifdef CONFIG_RANDOMIZE_BASE
1007         .pushsection ".rodata", "a"
1008         .align PAGE_SHIFT
1009         .globl  __entry_tramp_data_start
1010 __entry_tramp_data_start:
1011         .quad   vectors
1012         .popsection                             // .rodata
1013 #endif /* CONFIG_RANDOMIZE_BASE */
1014 #endif /* CONFIG_UNMAP_KERNEL_AT_EL0 */
1015
1016 /*
1017  * Special system call wrappers.
1018  */
1019 ENTRY(sys_rt_sigreturn_wrapper)
1020         mov     x0, sp
1021         b       sys_rt_sigreturn
1022 ENDPROC(sys_rt_sigreturn_wrapper)