Complete irq tracing support for ARM
[platform/kernel/linux-starfive.git] / arch / arm / kernel / entry-armv.S
1 /*
2  *  linux/arch/arm/kernel/entry-armv.S
3  *
4  *  Copyright (C) 1996,1997,1998 Russell King.
5  *  ARM700 fix by Matthew Godbolt (linux-user@willothewisp.demon.co.uk)
6  *  nommu support by Hyok S. Choi (hyok.choi@samsung.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  *  Low-level vector interface routines
13  *
14  *  Note:  there is a StrongARM bug in the STMIA rn, {regs}^ instruction
15  *  that causes it to save wrong values...  Be aware!
16  */
17
18 #include <asm/memory.h>
19 #include <asm/glue.h>
20 #include <asm/vfpmacros.h>
21 #include <mach/entry-macro.S>
22 #include <asm/thread_notify.h>
23 #include <asm/unwind.h>
24
25 #include "entry-header.S"
26
27 /*
28  * Interrupt handling.  Preserves r7, r8, r9
29  */
30         .macro  irq_handler
31         get_irqnr_preamble r5, lr
32 1:      get_irqnr_and_base r0, r6, r5, lr
33         movne   r1, sp
34         @
35         @ routine called with r0 = irq number, r1 = struct pt_regs *
36         @
37         adrne   lr, 1b
38         bne     asm_do_IRQ
39
40 #ifdef CONFIG_SMP
41         /*
42          * XXX
43          *
44          * this macro assumes that irqstat (r6) and base (r5) are
45          * preserved from get_irqnr_and_base above
46          */
47         test_for_ipi r0, r6, r5, lr
48         movne   r0, sp
49         adrne   lr, 1b
50         bne     do_IPI
51
52 #ifdef CONFIG_LOCAL_TIMERS
53         test_for_ltirq r0, r6, r5, lr
54         movne   r0, sp
55         adrne   lr, 1b
56         bne     do_local_timer
57 #endif
58 #endif
59
60         .endm
61
62 #ifdef CONFIG_KPROBES
63         .section        .kprobes.text,"ax",%progbits
64 #else
65         .text
66 #endif
67
68 /*
69  * Invalid mode handlers
70  */
71         .macro  inv_entry, reason
72         sub     sp, sp, #S_FRAME_SIZE
73         stmib   sp, {r1 - lr}
74         mov     r1, #\reason
75         .endm
76
77 __pabt_invalid:
78         inv_entry BAD_PREFETCH
79         b       common_invalid
80 ENDPROC(__pabt_invalid)
81
82 __dabt_invalid:
83         inv_entry BAD_DATA
84         b       common_invalid
85 ENDPROC(__dabt_invalid)
86
87 __irq_invalid:
88         inv_entry BAD_IRQ
89         b       common_invalid
90 ENDPROC(__irq_invalid)
91
92 __und_invalid:
93         inv_entry BAD_UNDEFINSTR
94
95         @
96         @ XXX fall through to common_invalid
97         @
98
99 @
100 @ common_invalid - generic code for failed exception (re-entrant version of handlers)
101 @
102 common_invalid:
103         zero_fp
104
105         ldmia   r0, {r4 - r6}
106         add     r0, sp, #S_PC           @ here for interlock avoidance
107         mov     r7, #-1                 @  ""   ""    ""        ""
108         str     r4, [sp]                @ save preserved r0
109         stmia   r0, {r5 - r7}           @ lr_<exception>,
110                                         @ cpsr_<exception>, "old_r0"
111
112         mov     r0, sp
113         b       bad_mode
114 ENDPROC(__und_invalid)
115
116 /*
117  * SVC mode handlers
118  */
119
120 #if defined(CONFIG_AEABI) && (__LINUX_ARM_ARCH__ >= 5)
121 #define SPFIX(code...) code
122 #else
123 #define SPFIX(code...)
124 #endif
125
126         .macro  svc_entry, stack_hole=0
127  UNWIND(.fnstart                )
128  UNWIND(.save {r0 - pc}         )
129         sub     sp, sp, #(S_FRAME_SIZE + \stack_hole)
130  SPFIX( tst     sp, #4          )
131  SPFIX( bicne   sp, sp, #4      )
132         stmib   sp, {r1 - r12}
133
134         ldmia   r0, {r1 - r3}
135         add     r5, sp, #S_SP           @ here for interlock avoidance
136         mov     r4, #-1                 @  ""  ""      ""       ""
137         add     r0, sp, #(S_FRAME_SIZE + \stack_hole)
138  SPFIX( addne   r0, r0, #4      )
139         str     r1, [sp]                @ save the "real" r0 copied
140                                         @ from the exception stack
141
142         mov     r1, lr
143
144         @
145         @ We are now ready to fill in the remaining blanks on the stack:
146         @
147         @  r0 - sp_svc
148         @  r1 - lr_svc
149         @  r2 - lr_<exception>, already fixed up for correct return/restart
150         @  r3 - spsr_<exception>
151         @  r4 - orig_r0 (see pt_regs definition in ptrace.h)
152         @
153         stmia   r5, {r0 - r4}
154
155         asm_trace_hardirqs_off
156         .endm
157
158         .align  5
159 __dabt_svc:
160         svc_entry
161
162         @
163         @ get ready to re-enable interrupts if appropriate
164         @
165         mrs     r9, cpsr
166         tst     r3, #PSR_I_BIT
167         biceq   r9, r9, #PSR_I_BIT
168
169         @
170         @ Call the processor-specific abort handler:
171         @
172         @  r2 - aborted context pc
173         @  r3 - aborted context cpsr
174         @
175         @ The abort handler must return the aborted address in r0, and
176         @ the fault status register in r1.  r9 must be preserved.
177         @
178 #ifdef MULTI_DABORT
179         ldr     r4, .LCprocfns
180         mov     lr, pc
181         ldr     pc, [r4, #PROCESSOR_DABT_FUNC]
182 #else
183         bl      CPU_DABORT_HANDLER
184 #endif
185
186         @
187         @ set desired IRQ state, then call main handler
188         @
189         msr     cpsr_c, r9
190         mov     r2, sp
191         bl      do_DataAbort
192
193         @
194         @ IRQs off again before pulling preserved data off the stack
195         @
196         disable_irq
197
198         @
199         @ restore SPSR and restart the instruction
200         @
201         ldr     r0, [sp, #S_PSR]
202         msr     spsr_cxsf, r0
203         ldmia   sp, {r0 - pc}^                  @ load r0 - pc, cpsr
204  UNWIND(.fnend          )
205 ENDPROC(__dabt_svc)
206
207         .align  5
208 __irq_svc:
209         svc_entry
210
211 #ifdef CONFIG_PREEMPT
212         get_thread_info tsk
213         ldr     r8, [tsk, #TI_PREEMPT]          @ get preempt count
214         add     r7, r8, #1                      @ increment it
215         str     r7, [tsk, #TI_PREEMPT]
216 #endif
217
218         irq_handler
219 #ifdef CONFIG_PREEMPT
220         str     r8, [tsk, #TI_PREEMPT]          @ restore preempt count
221         ldr     r0, [tsk, #TI_FLAGS]            @ get flags
222         teq     r8, #0                          @ if preempt count != 0
223         movne   r0, #0                          @ force flags to 0
224         tst     r0, #_TIF_NEED_RESCHED
225         blne    svc_preempt
226 #endif
227         ldr     r0, [sp, #S_PSR]                @ irqs are already disabled
228         msr     spsr_cxsf, r0
229 #ifdef CONFIG_TRACE_IRQFLAGS
230         tst     r0, #PSR_I_BIT
231         bleq    trace_hardirqs_on
232 #endif
233         ldmia   sp, {r0 - pc}^                  @ load r0 - pc, cpsr
234  UNWIND(.fnend          )
235 ENDPROC(__irq_svc)
236
237         .ltorg
238
239 #ifdef CONFIG_PREEMPT
240 svc_preempt:
241         mov     r8, lr
242 1:      bl      preempt_schedule_irq            @ irq en/disable is done inside
243         ldr     r0, [tsk, #TI_FLAGS]            @ get new tasks TI_FLAGS
244         tst     r0, #_TIF_NEED_RESCHED
245         moveq   pc, r8                          @ go again
246         b       1b
247 #endif
248
249         .align  5
250 __und_svc:
251 #ifdef CONFIG_KPROBES
252         @ If a kprobe is about to simulate a "stmdb sp..." instruction,
253         @ it obviously needs free stack space which then will belong to
254         @ the saved context.
255         svc_entry 64
256 #else
257         svc_entry
258 #endif
259
260         @
261         @ call emulation code, which returns using r9 if it has emulated
262         @ the instruction, or the more conventional lr if we are to treat
263         @ this as a real undefined instruction
264         @
265         @  r0 - instruction
266         @
267         ldr     r0, [r2, #-4]
268         adr     r9, 1f
269         bl      call_fpe
270
271         mov     r0, sp                          @ struct pt_regs *regs
272         bl      do_undefinstr
273
274         @
275         @ IRQs off again before pulling preserved data off the stack
276         @
277 1:      disable_irq
278
279         @
280         @ restore SPSR and restart the instruction
281         @
282         ldr     lr, [sp, #S_PSR]                @ Get SVC cpsr
283         msr     spsr_cxsf, lr
284         ldmia   sp, {r0 - pc}^                  @ Restore SVC registers
285  UNWIND(.fnend          )
286 ENDPROC(__und_svc)
287
288         .align  5
289 __pabt_svc:
290         svc_entry
291
292         @
293         @ re-enable interrupts if appropriate
294         @
295         mrs     r9, cpsr
296         tst     r3, #PSR_I_BIT
297         biceq   r9, r9, #PSR_I_BIT
298
299         @
300         @ set args, then call main handler
301         @
302         @  r0 - address of faulting instruction
303         @  r1 - pointer to registers on stack
304         @
305 #ifdef MULTI_PABORT
306         mov     r0, r2                  @ pass address of aborted instruction.
307         ldr     r4, .LCprocfns
308         mov     lr, pc
309         ldr     pc, [r4, #PROCESSOR_PABT_FUNC]
310 #else
311         CPU_PABORT_HANDLER(r0, r2)
312 #endif
313         msr     cpsr_c, r9                      @ Maybe enable interrupts
314         mov     r1, sp                          @ regs
315         bl      do_PrefetchAbort                @ call abort handler
316
317         @
318         @ IRQs off again before pulling preserved data off the stack
319         @
320         disable_irq
321
322         @
323         @ restore SPSR and restart the instruction
324         @
325         ldr     r0, [sp, #S_PSR]
326         msr     spsr_cxsf, r0
327         ldmia   sp, {r0 - pc}^                  @ load r0 - pc, cpsr
328  UNWIND(.fnend          )
329 ENDPROC(__pabt_svc)
330
331         .align  5
332 .LCcralign:
333         .word   cr_alignment
334 #ifdef MULTI_DABORT
335 .LCprocfns:
336         .word   processor
337 #endif
338 .LCfp:
339         .word   fp_enter
340
341 /*
342  * User mode handlers
343  *
344  * EABI note: sp_svc is always 64-bit aligned here, so should S_FRAME_SIZE
345  */
346
347 #if defined(CONFIG_AEABI) && (__LINUX_ARM_ARCH__ >= 5) && (S_FRAME_SIZE & 7)
348 #error "sizeof(struct pt_regs) must be a multiple of 8"
349 #endif
350
351         .macro  usr_entry
352  UNWIND(.fnstart        )
353  UNWIND(.cantunwind     )       @ don't unwind the user space
354         sub     sp, sp, #S_FRAME_SIZE
355         stmib   sp, {r1 - r12}
356
357         ldmia   r0, {r1 - r3}
358         add     r0, sp, #S_PC           @ here for interlock avoidance
359         mov     r4, #-1                 @  ""  ""     ""        ""
360
361         str     r1, [sp]                @ save the "real" r0 copied
362                                         @ from the exception stack
363
364         @
365         @ We are now ready to fill in the remaining blanks on the stack:
366         @
367         @  r2 - lr_<exception>, already fixed up for correct return/restart
368         @  r3 - spsr_<exception>
369         @  r4 - orig_r0 (see pt_regs definition in ptrace.h)
370         @
371         @ Also, separately save sp_usr and lr_usr
372         @
373         stmia   r0, {r2 - r4}
374         stmdb   r0, {sp, lr}^
375
376         @
377         @ Enable the alignment trap while in kernel mode
378         @
379         alignment_trap r0
380
381         @
382         @ Clear FP to mark the first stack frame
383         @
384         zero_fp
385
386         asm_trace_hardirqs_off
387         .endm
388
389         .macro  kuser_cmpxchg_check
390 #if __LINUX_ARM_ARCH__ < 6 && !defined(CONFIG_NEEDS_SYSCALL_FOR_CMPXCHG)
391 #ifndef CONFIG_MMU
392 #warning "NPTL on non MMU needs fixing"
393 #else
394         @ Make sure our user space atomic helper is restarted
395         @ if it was interrupted in a critical region.  Here we
396         @ perform a quick test inline since it should be false
397         @ 99.9999% of the time.  The rest is done out of line.
398         cmp     r2, #TASK_SIZE
399         blhs    kuser_cmpxchg_fixup
400 #endif
401 #endif
402         .endm
403
404         .align  5
405 __dabt_usr:
406         usr_entry
407         kuser_cmpxchg_check
408
409         @
410         @ Call the processor-specific abort handler:
411         @
412         @  r2 - aborted context pc
413         @  r3 - aborted context cpsr
414         @
415         @ The abort handler must return the aborted address in r0, and
416         @ the fault status register in r1.
417         @
418 #ifdef MULTI_DABORT
419         ldr     r4, .LCprocfns
420         mov     lr, pc
421         ldr     pc, [r4, #PROCESSOR_DABT_FUNC]
422 #else
423         bl      CPU_DABORT_HANDLER
424 #endif
425
426         @
427         @ IRQs on, then call the main handler
428         @
429         enable_irq
430         mov     r2, sp
431         adr     lr, ret_from_exception
432         b       do_DataAbort
433  UNWIND(.fnend          )
434 ENDPROC(__dabt_usr)
435
436         .align  5
437 __irq_usr:
438         usr_entry
439         kuser_cmpxchg_check
440
441         get_thread_info tsk
442 #ifdef CONFIG_PREEMPT
443         ldr     r8, [tsk, #TI_PREEMPT]          @ get preempt count
444         add     r7, r8, #1                      @ increment it
445         str     r7, [tsk, #TI_PREEMPT]
446 #endif
447
448         irq_handler
449 #ifdef CONFIG_PREEMPT
450         ldr     r0, [tsk, #TI_PREEMPT]
451         str     r8, [tsk, #TI_PREEMPT]
452         teq     r0, r7
453         strne   r0, [r0, -r0]
454 #endif
455 #ifdef CONFIG_TRACE_IRQFLAGS
456         bl      trace_hardirqs_on
457 #endif
458
459         mov     why, #0
460         b       ret_to_user
461  UNWIND(.fnend          )
462 ENDPROC(__irq_usr)
463
464         .ltorg
465
466         .align  5
467 __und_usr:
468         usr_entry
469
470         @
471         @ fall through to the emulation code, which returns using r9 if
472         @ it has emulated the instruction, or the more conventional lr
473         @ if we are to treat this as a real undefined instruction
474         @
475         @  r0 - instruction
476         @
477         adr     r9, ret_from_exception
478         adr     lr, __und_usr_unknown
479         tst     r3, #PSR_T_BIT                  @ Thumb mode?
480         subeq   r4, r2, #4                      @ ARM instr at LR - 4
481         subne   r4, r2, #2                      @ Thumb instr at LR - 2
482 1:      ldreqt  r0, [r4]
483 #ifdef CONFIG_CPU_ENDIAN_BE8
484         reveq   r0, r0                          @ little endian instruction
485 #endif
486         beq     call_fpe
487         @ Thumb instruction
488 #if __LINUX_ARM_ARCH__ >= 7
489 2:      ldrht   r5, [r4], #2
490         and     r0, r5, #0xf800                 @ mask bits 111x x... .... ....
491         cmp     r0, #0xe800                     @ 32bit instruction if xx != 0
492         blo     __und_usr_unknown
493 3:      ldrht   r0, [r4]
494         add     r2, r2, #2                      @ r2 is PC + 2, make it PC + 4
495         orr     r0, r0, r5, lsl #16
496 #else
497         b       __und_usr_unknown
498 #endif
499  UNWIND(.fnend          )
500 ENDPROC(__und_usr)
501
502         @
503         @ fallthrough to call_fpe
504         @
505
506 /*
507  * The out of line fixup for the ldrt above.
508  */
509         .section .fixup, "ax"
510 4:      mov     pc, r9
511         .previous
512         .section __ex_table,"a"
513         .long   1b, 4b
514 #if __LINUX_ARM_ARCH__ >= 7
515         .long   2b, 4b
516         .long   3b, 4b
517 #endif
518         .previous
519
520 /*
521  * Check whether the instruction is a co-processor instruction.
522  * If yes, we need to call the relevant co-processor handler.
523  *
524  * Note that we don't do a full check here for the co-processor
525  * instructions; all instructions with bit 27 set are well
526  * defined.  The only instructions that should fault are the
527  * co-processor instructions.  However, we have to watch out
528  * for the ARM6/ARM7 SWI bug.
529  *
530  * NEON is a special case that has to be handled here. Not all
531  * NEON instructions are co-processor instructions, so we have
532  * to make a special case of checking for them. Plus, there's
533  * five groups of them, so we have a table of mask/opcode pairs
534  * to check against, and if any match then we branch off into the
535  * NEON handler code.
536  *
537  * Emulators may wish to make use of the following registers:
538  *  r0  = instruction opcode.
539  *  r2  = PC+4
540  *  r9  = normal "successful" return address
541  *  r10 = this threads thread_info structure.
542  *  lr  = unrecognised instruction return address
543  */
544         @
545         @ Fall-through from Thumb-2 __und_usr
546         @
547 #ifdef CONFIG_NEON
548         adr     r6, .LCneon_thumb_opcodes
549         b       2f
550 #endif
551 call_fpe:
552 #ifdef CONFIG_NEON
553         adr     r6, .LCneon_arm_opcodes
554 2:
555         ldr     r7, [r6], #4                    @ mask value
556         cmp     r7, #0                          @ end mask?
557         beq     1f
558         and     r8, r0, r7
559         ldr     r7, [r6], #4                    @ opcode bits matching in mask
560         cmp     r8, r7                          @ NEON instruction?
561         bne     2b
562         get_thread_info r10
563         mov     r7, #1
564         strb    r7, [r10, #TI_USED_CP + 10]     @ mark CP#10 as used
565         strb    r7, [r10, #TI_USED_CP + 11]     @ mark CP#11 as used
566         b       do_vfp                          @ let VFP handler handle this
567 1:
568 #endif
569         tst     r0, #0x08000000                 @ only CDP/CPRT/LDC/STC have bit 27
570         tstne   r0, #0x04000000                 @ bit 26 set on both ARM and Thumb-2
571 #if defined(CONFIG_CPU_ARM610) || defined(CONFIG_CPU_ARM710)
572         and     r8, r0, #0x0f000000             @ mask out op-code bits
573         teqne   r8, #0x0f000000                 @ SWI (ARM6/7 bug)?
574 #endif
575         moveq   pc, lr
576         get_thread_info r10                     @ get current thread
577         and     r8, r0, #0x00000f00             @ mask out CP number
578         mov     r7, #1
579         add     r6, r10, #TI_USED_CP
580         strb    r7, [r6, r8, lsr #8]            @ set appropriate used_cp[]
581 #ifdef CONFIG_IWMMXT
582         @ Test if we need to give access to iWMMXt coprocessors
583         ldr     r5, [r10, #TI_FLAGS]
584         rsbs    r7, r8, #(1 << 8)               @ CP 0 or 1 only
585         movcss  r7, r5, lsr #(TIF_USING_IWMMXT + 1)
586         bcs     iwmmxt_task_enable
587 #endif
588         add     pc, pc, r8, lsr #6
589         mov     r0, r0
590
591         mov     pc, lr                          @ CP#0
592         b       do_fpe                          @ CP#1 (FPE)
593         b       do_fpe                          @ CP#2 (FPE)
594         mov     pc, lr                          @ CP#3
595 #ifdef CONFIG_CRUNCH
596         b       crunch_task_enable              @ CP#4 (MaverickCrunch)
597         b       crunch_task_enable              @ CP#5 (MaverickCrunch)
598         b       crunch_task_enable              @ CP#6 (MaverickCrunch)
599 #else
600         mov     pc, lr                          @ CP#4
601         mov     pc, lr                          @ CP#5
602         mov     pc, lr                          @ CP#6
603 #endif
604         mov     pc, lr                          @ CP#7
605         mov     pc, lr                          @ CP#8
606         mov     pc, lr                          @ CP#9
607 #ifdef CONFIG_VFP
608         b       do_vfp                          @ CP#10 (VFP)
609         b       do_vfp                          @ CP#11 (VFP)
610 #else
611         mov     pc, lr                          @ CP#10 (VFP)
612         mov     pc, lr                          @ CP#11 (VFP)
613 #endif
614         mov     pc, lr                          @ CP#12
615         mov     pc, lr                          @ CP#13
616         mov     pc, lr                          @ CP#14 (Debug)
617         mov     pc, lr                          @ CP#15 (Control)
618
619 #ifdef CONFIG_NEON
620         .align  6
621
622 .LCneon_arm_opcodes:
623         .word   0xfe000000                      @ mask
624         .word   0xf2000000                      @ opcode
625
626         .word   0xff100000                      @ mask
627         .word   0xf4000000                      @ opcode
628
629         .word   0x00000000                      @ mask
630         .word   0x00000000                      @ opcode
631
632 .LCneon_thumb_opcodes:
633         .word   0xef000000                      @ mask
634         .word   0xef000000                      @ opcode
635
636         .word   0xff100000                      @ mask
637         .word   0xf9000000                      @ opcode
638
639         .word   0x00000000                      @ mask
640         .word   0x00000000                      @ opcode
641 #endif
642
643 do_fpe:
644         enable_irq
645         ldr     r4, .LCfp
646         add     r10, r10, #TI_FPSTATE           @ r10 = workspace
647         ldr     pc, [r4]                        @ Call FP module USR entry point
648
649 /*
650  * The FP module is called with these registers set:
651  *  r0  = instruction
652  *  r2  = PC+4
653  *  r9  = normal "successful" return address
654  *  r10 = FP workspace
655  *  lr  = unrecognised FP instruction return address
656  */
657
658         .data
659 ENTRY(fp_enter)
660         .word   no_fp
661         .previous
662
663 no_fp:  mov     pc, lr
664
665 __und_usr_unknown:
666         enable_irq
667         mov     r0, sp
668         adr     lr, ret_from_exception
669         b       do_undefinstr
670 ENDPROC(__und_usr_unknown)
671
672         .align  5
673 __pabt_usr:
674         usr_entry
675
676 #ifdef MULTI_PABORT
677         mov     r0, r2                  @ pass address of aborted instruction.
678         ldr     r4, .LCprocfns
679         mov     lr, pc
680         ldr     pc, [r4, #PROCESSOR_PABT_FUNC]
681 #else
682         CPU_PABORT_HANDLER(r0, r2)
683 #endif
684         enable_irq                              @ Enable interrupts
685         mov     r1, sp                          @ regs
686         bl      do_PrefetchAbort                @ call abort handler
687  UNWIND(.fnend          )
688         /* fall through */
689 /*
690  * This is the return code to user mode for abort handlers
691  */
692 ENTRY(ret_from_exception)
693  UNWIND(.fnstart        )
694  UNWIND(.cantunwind     )
695         get_thread_info tsk
696         mov     why, #0
697         b       ret_to_user
698  UNWIND(.fnend          )
699 ENDPROC(__pabt_usr)
700 ENDPROC(ret_from_exception)
701
702 /*
703  * Register switch for ARMv3 and ARMv4 processors
704  * r0 = previous task_struct, r1 = previous thread_info, r2 = next thread_info
705  * previous and next are guaranteed not to be the same.
706  */
707 ENTRY(__switch_to)
708  UNWIND(.fnstart        )
709  UNWIND(.cantunwind     )
710         add     ip, r1, #TI_CPU_SAVE
711         ldr     r3, [r2, #TI_TP_VALUE]
712         stmia   ip!, {r4 - sl, fp, sp, lr}      @ Store most regs on stack
713 #ifdef CONFIG_MMU
714         ldr     r6, [r2, #TI_CPU_DOMAIN]
715 #endif
716 #if __LINUX_ARM_ARCH__ >= 6
717 #ifdef CONFIG_CPU_32v6K
718         clrex
719 #else
720         strex   r5, r4, [ip]                    @ Clear exclusive monitor
721 #endif
722 #endif
723 #if defined(CONFIG_HAS_TLS_REG)
724         mcr     p15, 0, r3, c13, c0, 3          @ set TLS register
725 #elif !defined(CONFIG_TLS_REG_EMUL)
726         mov     r4, #0xffff0fff
727         str     r3, [r4, #-15]                  @ TLS val at 0xffff0ff0
728 #endif
729 #ifdef CONFIG_MMU
730         mcr     p15, 0, r6, c3, c0, 0           @ Set domain register
731 #endif
732         mov     r5, r0
733         add     r4, r2, #TI_CPU_SAVE
734         ldr     r0, =thread_notify_head
735         mov     r1, #THREAD_NOTIFY_SWITCH
736         bl      atomic_notifier_call_chain
737         mov     r0, r5
738         ldmia   r4, {r4 - sl, fp, sp, pc}       @ Load all regs saved previously
739  UNWIND(.fnend          )
740 ENDPROC(__switch_to)
741
742         __INIT
743
744 /*
745  * User helpers.
746  *
747  * These are segment of kernel provided user code reachable from user space
748  * at a fixed address in kernel memory.  This is used to provide user space
749  * with some operations which require kernel help because of unimplemented
750  * native feature and/or instructions in many ARM CPUs. The idea is for
751  * this code to be executed directly in user mode for best efficiency but
752  * which is too intimate with the kernel counter part to be left to user
753  * libraries.  In fact this code might even differ from one CPU to another
754  * depending on the available  instruction set and restrictions like on
755  * SMP systems.  In other words, the kernel reserves the right to change
756  * this code as needed without warning. Only the entry points and their
757  * results are guaranteed to be stable.
758  *
759  * Each segment is 32-byte aligned and will be moved to the top of the high
760  * vector page.  New segments (if ever needed) must be added in front of
761  * existing ones.  This mechanism should be used only for things that are
762  * really small and justified, and not be abused freely.
763  *
764  * User space is expected to implement those things inline when optimizing
765  * for a processor that has the necessary native support, but only if such
766  * resulting binaries are already to be incompatible with earlier ARM
767  * processors due to the use of unsupported instructions other than what
768  * is provided here.  In other words don't make binaries unable to run on
769  * earlier processors just for the sake of not using these kernel helpers
770  * if your compiled code is not going to use the new instructions for other
771  * purpose.
772  */
773
774         .macro  usr_ret, reg
775 #ifdef CONFIG_ARM_THUMB
776         bx      \reg
777 #else
778         mov     pc, \reg
779 #endif
780         .endm
781
782         .align  5
783         .globl  __kuser_helper_start
784 __kuser_helper_start:
785
786 /*
787  * Reference prototype:
788  *
789  *      void __kernel_memory_barrier(void)
790  *
791  * Input:
792  *
793  *      lr = return address
794  *
795  * Output:
796  *
797  *      none
798  *
799  * Clobbered:
800  *
801  *      none
802  *
803  * Definition and user space usage example:
804  *
805  *      typedef void (__kernel_dmb_t)(void);
806  *      #define __kernel_dmb (*(__kernel_dmb_t *)0xffff0fa0)
807  *
808  * Apply any needed memory barrier to preserve consistency with data modified
809  * manually and __kuser_cmpxchg usage.
810  *
811  * This could be used as follows:
812  *
813  * #define __kernel_dmb() \
814  *         asm volatile ( "mov r0, #0xffff0fff; mov lr, pc; sub pc, r0, #95" \
815  *              : : : "r0", "lr","cc" )
816  */
817
818 __kuser_memory_barrier:                         @ 0xffff0fa0
819         smp_dmb
820         usr_ret lr
821
822         .align  5
823
824 /*
825  * Reference prototype:
826  *
827  *      int __kernel_cmpxchg(int oldval, int newval, int *ptr)
828  *
829  * Input:
830  *
831  *      r0 = oldval
832  *      r1 = newval
833  *      r2 = ptr
834  *      lr = return address
835  *
836  * Output:
837  *
838  *      r0 = returned value (zero or non-zero)
839  *      C flag = set if r0 == 0, clear if r0 != 0
840  *
841  * Clobbered:
842  *
843  *      r3, ip, flags
844  *
845  * Definition and user space usage example:
846  *
847  *      typedef int (__kernel_cmpxchg_t)(int oldval, int newval, int *ptr);
848  *      #define __kernel_cmpxchg (*(__kernel_cmpxchg_t *)0xffff0fc0)
849  *
850  * Atomically store newval in *ptr if *ptr is equal to oldval for user space.
851  * Return zero if *ptr was changed or non-zero if no exchange happened.
852  * The C flag is also set if *ptr was changed to allow for assembly
853  * optimization in the calling code.
854  *
855  * Notes:
856  *
857  *    - This routine already includes memory barriers as needed.
858  *
859  * For example, a user space atomic_add implementation could look like this:
860  *
861  * #define atomic_add(ptr, val) \
862  *      ({ register unsigned int *__ptr asm("r2") = (ptr); \
863  *         register unsigned int __result asm("r1"); \
864  *         asm volatile ( \
865  *             "1: @ atomic_add\n\t" \
866  *             "ldr     r0, [r2]\n\t" \
867  *             "mov     r3, #0xffff0fff\n\t" \
868  *             "add     lr, pc, #4\n\t" \
869  *             "add     r1, r0, %2\n\t" \
870  *             "add     pc, r3, #(0xffff0fc0 - 0xffff0fff)\n\t" \
871  *             "bcc     1b" \
872  *             : "=&r" (__result) \
873  *             : "r" (__ptr), "rIL" (val) \
874  *             : "r0","r3","ip","lr","cc","memory" ); \
875  *         __result; })
876  */
877
878 __kuser_cmpxchg:                                @ 0xffff0fc0
879
880 #if defined(CONFIG_NEEDS_SYSCALL_FOR_CMPXCHG)
881
882         /*
883          * Poor you.  No fast solution possible...
884          * The kernel itself must perform the operation.
885          * A special ghost syscall is used for that (see traps.c).
886          */
887         stmfd   sp!, {r7, lr}
888         mov     r7, #0xff00             @ 0xfff0 into r7 for EABI
889         orr     r7, r7, #0xf0
890         swi     #0x9ffff0
891         ldmfd   sp!, {r7, pc}
892
893 #elif __LINUX_ARM_ARCH__ < 6
894
895 #ifdef CONFIG_MMU
896
897         /*
898          * The only thing that can break atomicity in this cmpxchg
899          * implementation is either an IRQ or a data abort exception
900          * causing another process/thread to be scheduled in the middle
901          * of the critical sequence.  To prevent this, code is added to
902          * the IRQ and data abort exception handlers to set the pc back
903          * to the beginning of the critical section if it is found to be
904          * within that critical section (see kuser_cmpxchg_fixup).
905          */
906 1:      ldr     r3, [r2]                        @ load current val
907         subs    r3, r3, r0                      @ compare with oldval
908 2:      streq   r1, [r2]                        @ store newval if eq
909         rsbs    r0, r3, #0                      @ set return val and C flag
910         usr_ret lr
911
912         .text
913 kuser_cmpxchg_fixup:
914         @ Called from kuser_cmpxchg_check macro.
915         @ r2 = address of interrupted insn (must be preserved).
916         @ sp = saved regs. r7 and r8 are clobbered.
917         @ 1b = first critical insn, 2b = last critical insn.
918         @ If r2 >= 1b and r2 <= 2b then saved pc_usr is set to 1b.
919         mov     r7, #0xffff0fff
920         sub     r7, r7, #(0xffff0fff - (0xffff0fc0 + (1b - __kuser_cmpxchg)))
921         subs    r8, r2, r7
922         rsbcss  r8, r8, #(2b - 1b)
923         strcs   r7, [sp, #S_PC]
924         mov     pc, lr
925         .previous
926
927 #else
928 #warning "NPTL on non MMU needs fixing"
929         mov     r0, #-1
930         adds    r0, r0, #0
931         usr_ret lr
932 #endif
933
934 #else
935
936 #ifdef CONFIG_SMP
937         mcr     p15, 0, r0, c7, c10, 5  @ dmb
938 #endif
939 1:      ldrex   r3, [r2]
940         subs    r3, r3, r0
941         strexeq r3, r1, [r2]
942         teqeq   r3, #1
943         beq     1b
944         rsbs    r0, r3, #0
945         /* beware -- each __kuser slot must be 8 instructions max */
946 #ifdef CONFIG_SMP
947         b       __kuser_memory_barrier
948 #else
949         usr_ret lr
950 #endif
951
952 #endif
953
954         .align  5
955
956 /*
957  * Reference prototype:
958  *
959  *      int __kernel_get_tls(void)
960  *
961  * Input:
962  *
963  *      lr = return address
964  *
965  * Output:
966  *
967  *      r0 = TLS value
968  *
969  * Clobbered:
970  *
971  *      none
972  *
973  * Definition and user space usage example:
974  *
975  *      typedef int (__kernel_get_tls_t)(void);
976  *      #define __kernel_get_tls (*(__kernel_get_tls_t *)0xffff0fe0)
977  *
978  * Get the TLS value as previously set via the __ARM_NR_set_tls syscall.
979  *
980  * This could be used as follows:
981  *
982  * #define __kernel_get_tls() \
983  *      ({ register unsigned int __val asm("r0"); \
984  *         asm( "mov r0, #0xffff0fff; mov lr, pc; sub pc, r0, #31" \
985  *              : "=r" (__val) : : "lr","cc" ); \
986  *         __val; })
987  */
988
989 __kuser_get_tls:                                @ 0xffff0fe0
990
991 #if !defined(CONFIG_HAS_TLS_REG) && !defined(CONFIG_TLS_REG_EMUL)
992         ldr     r0, [pc, #(16 - 8)]             @ TLS stored at 0xffff0ff0
993 #else
994         mrc     p15, 0, r0, c13, c0, 3          @ read TLS register
995 #endif
996         usr_ret lr
997
998         .rep    5
999         .word   0                       @ pad up to __kuser_helper_version
1000         .endr
1001
1002 /*
1003  * Reference declaration:
1004  *
1005  *      extern unsigned int __kernel_helper_version;
1006  *
1007  * Definition and user space usage example:
1008  *
1009  *      #define __kernel_helper_version (*(unsigned int *)0xffff0ffc)
1010  *
1011  * User space may read this to determine the curent number of helpers
1012  * available.
1013  */
1014
1015 __kuser_helper_version:                         @ 0xffff0ffc
1016         .word   ((__kuser_helper_end - __kuser_helper_start) >> 5)
1017
1018         .globl  __kuser_helper_end
1019 __kuser_helper_end:
1020
1021
1022 /*
1023  * Vector stubs.
1024  *
1025  * This code is copied to 0xffff0200 so we can use branches in the
1026  * vectors, rather than ldr's.  Note that this code must not
1027  * exceed 0x300 bytes.
1028  *
1029  * Common stub entry macro:
1030  *   Enter in IRQ mode, spsr = SVC/USR CPSR, lr = SVC/USR PC
1031  *
1032  * SP points to a minimal amount of processor-private memory, the address
1033  * of which is copied into r0 for the mode specific abort handler.
1034  */
1035         .macro  vector_stub, name, mode, correction=0
1036         .align  5
1037
1038 vector_\name:
1039         .if \correction
1040         sub     lr, lr, #\correction
1041         .endif
1042
1043         @
1044         @ Save r0, lr_<exception> (parent PC) and spsr_<exception>
1045         @ (parent CPSR)
1046         @
1047         stmia   sp, {r0, lr}            @ save r0, lr
1048         mrs     lr, spsr
1049         str     lr, [sp, #8]            @ save spsr
1050
1051         @
1052         @ Prepare for SVC32 mode.  IRQs remain disabled.
1053         @
1054         mrs     r0, cpsr
1055         eor     r0, r0, #(\mode ^ SVC_MODE)
1056         msr     spsr_cxsf, r0
1057
1058         @
1059         @ the branch table must immediately follow this code
1060         @
1061         and     lr, lr, #0x0f
1062         mov     r0, sp
1063         ldr     lr, [pc, lr, lsl #2]
1064         movs    pc, lr                  @ branch to handler in SVC mode
1065 ENDPROC(vector_\name)
1066         .endm
1067
1068         .globl  __stubs_start
1069 __stubs_start:
1070 /*
1071  * Interrupt dispatcher
1072  */
1073         vector_stub     irq, IRQ_MODE, 4
1074
1075         .long   __irq_usr                       @  0  (USR_26 / USR_32)
1076         .long   __irq_invalid                   @  1  (FIQ_26 / FIQ_32)
1077         .long   __irq_invalid                   @  2  (IRQ_26 / IRQ_32)
1078         .long   __irq_svc                       @  3  (SVC_26 / SVC_32)
1079         .long   __irq_invalid                   @  4
1080         .long   __irq_invalid                   @  5
1081         .long   __irq_invalid                   @  6
1082         .long   __irq_invalid                   @  7
1083         .long   __irq_invalid                   @  8
1084         .long   __irq_invalid                   @  9
1085         .long   __irq_invalid                   @  a
1086         .long   __irq_invalid                   @  b
1087         .long   __irq_invalid                   @  c
1088         .long   __irq_invalid                   @  d
1089         .long   __irq_invalid                   @  e
1090         .long   __irq_invalid                   @  f
1091
1092 /*
1093  * Data abort dispatcher
1094  * Enter in ABT mode, spsr = USR CPSR, lr = USR PC
1095  */
1096         vector_stub     dabt, ABT_MODE, 8
1097
1098         .long   __dabt_usr                      @  0  (USR_26 / USR_32)
1099         .long   __dabt_invalid                  @  1  (FIQ_26 / FIQ_32)
1100         .long   __dabt_invalid                  @  2  (IRQ_26 / IRQ_32)
1101         .long   __dabt_svc                      @  3  (SVC_26 / SVC_32)
1102         .long   __dabt_invalid                  @  4
1103         .long   __dabt_invalid                  @  5
1104         .long   __dabt_invalid                  @  6
1105         .long   __dabt_invalid                  @  7
1106         .long   __dabt_invalid                  @  8
1107         .long   __dabt_invalid                  @  9
1108         .long   __dabt_invalid                  @  a
1109         .long   __dabt_invalid                  @  b
1110         .long   __dabt_invalid                  @  c
1111         .long   __dabt_invalid                  @  d
1112         .long   __dabt_invalid                  @  e
1113         .long   __dabt_invalid                  @  f
1114
1115 /*
1116  * Prefetch abort dispatcher
1117  * Enter in ABT mode, spsr = USR CPSR, lr = USR PC
1118  */
1119         vector_stub     pabt, ABT_MODE, 4
1120
1121         .long   __pabt_usr                      @  0 (USR_26 / USR_32)
1122         .long   __pabt_invalid                  @  1 (FIQ_26 / FIQ_32)
1123         .long   __pabt_invalid                  @  2 (IRQ_26 / IRQ_32)
1124         .long   __pabt_svc                      @  3 (SVC_26 / SVC_32)
1125         .long   __pabt_invalid                  @  4
1126         .long   __pabt_invalid                  @  5
1127         .long   __pabt_invalid                  @  6
1128         .long   __pabt_invalid                  @  7
1129         .long   __pabt_invalid                  @  8
1130         .long   __pabt_invalid                  @  9
1131         .long   __pabt_invalid                  @  a
1132         .long   __pabt_invalid                  @  b
1133         .long   __pabt_invalid                  @  c
1134         .long   __pabt_invalid                  @  d
1135         .long   __pabt_invalid                  @  e
1136         .long   __pabt_invalid                  @  f
1137
1138 /*
1139  * Undef instr entry dispatcher
1140  * Enter in UND mode, spsr = SVC/USR CPSR, lr = SVC/USR PC
1141  */
1142         vector_stub     und, UND_MODE
1143
1144         .long   __und_usr                       @  0 (USR_26 / USR_32)
1145         .long   __und_invalid                   @  1 (FIQ_26 / FIQ_32)
1146         .long   __und_invalid                   @  2 (IRQ_26 / IRQ_32)
1147         .long   __und_svc                       @  3 (SVC_26 / SVC_32)
1148         .long   __und_invalid                   @  4
1149         .long   __und_invalid                   @  5
1150         .long   __und_invalid                   @  6
1151         .long   __und_invalid                   @  7
1152         .long   __und_invalid                   @  8
1153         .long   __und_invalid                   @  9
1154         .long   __und_invalid                   @  a
1155         .long   __und_invalid                   @  b
1156         .long   __und_invalid                   @  c
1157         .long   __und_invalid                   @  d
1158         .long   __und_invalid                   @  e
1159         .long   __und_invalid                   @  f
1160
1161         .align  5
1162
1163 /*=============================================================================
1164  * Undefined FIQs
1165  *-----------------------------------------------------------------------------
1166  * Enter in FIQ mode, spsr = ANY CPSR, lr = ANY PC
1167  * MUST PRESERVE SVC SPSR, but need to switch to SVC mode to show our msg.
1168  * Basically to switch modes, we *HAVE* to clobber one register...  brain
1169  * damage alert!  I don't think that we can execute any code in here in any
1170  * other mode than FIQ...  Ok you can switch to another mode, but you can't
1171  * get out of that mode without clobbering one register.
1172  */
1173 vector_fiq:
1174         disable_fiq
1175         subs    pc, lr, #4
1176
1177 /*=============================================================================
1178  * Address exception handler
1179  *-----------------------------------------------------------------------------
1180  * These aren't too critical.
1181  * (they're not supposed to happen, and won't happen in 32-bit data mode).
1182  */
1183
1184 vector_addrexcptn:
1185         b       vector_addrexcptn
1186
1187 /*
1188  * We group all the following data together to optimise
1189  * for CPUs with separate I & D caches.
1190  */
1191         .align  5
1192
1193 .LCvswi:
1194         .word   vector_swi
1195
1196         .globl  __stubs_end
1197 __stubs_end:
1198
1199         .equ    stubs_offset, __vectors_start + 0x200 - __stubs_start
1200
1201         .globl  __vectors_start
1202 __vectors_start:
1203         swi     SYS_ERROR0
1204         b       vector_und + stubs_offset
1205         ldr     pc, .LCvswi + stubs_offset
1206         b       vector_pabt + stubs_offset
1207         b       vector_dabt + stubs_offset
1208         b       vector_addrexcptn + stubs_offset
1209         b       vector_irq + stubs_offset
1210         b       vector_fiq + stubs_offset
1211
1212         .globl  __vectors_end
1213 __vectors_end:
1214
1215         .data
1216
1217         .globl  cr_alignment
1218         .globl  cr_no_alignment
1219 cr_alignment:
1220         .space  4
1221 cr_no_alignment:
1222         .space  4