2 * linux/arch/arm/kernel/entry-armv.S
4 * Copyright (C) 1996,1997,1998 Russell King.
5 * ARM700 fix by Matthew Godbolt (linux-user@willothewisp.demon.co.uk)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * Low-level vector interface routines
13 * Note: there is a StrongARM bug in the STMIA rn, {regs}^ instruction that causes
14 * it to save wrong values... Be aware!
16 #include <linux/config.h>
19 #include <asm/vfpmacros.h>
20 #include <asm/hardware.h> /* should be moved into entry-macro.S */
21 #include <asm/arch/irqs.h> /* should be moved into entry-macro.S */
22 #include <asm/arch/entry-macro.S>
24 #include "entry-header.S"
27 * Interrupt handling. Preserves r7, r8, r9
30 1: get_irqnr_and_base r0, r6, r5, lr
33 @ routine called with r0 = irq number, r1 = struct pt_regs *
42 * this macro assumes that irqstat (r6) and base (r5) are
43 * preserved from get_irqnr_and_base above
45 test_for_ipi r0, r6, r5, lr
54 * Invalid mode handlers
56 .macro inv_entry, sym, reason
57 sub sp, sp, #S_FRAME_SIZE @ Allocate frame size in one go
58 stmia sp, {r0 - lr} @ Save XXX r0 - lr
64 inv_entry abt, BAD_PREFETCH
68 inv_entry abt, BAD_DATA
72 inv_entry irq, BAD_IRQ
76 inv_entry und, BAD_UNDEFINSTR
79 ldmia r4, {r5 - r7} @ Get XXX pc, cpsr, old_r0
81 stmia r4, {r5 - r7} @ Save XXX pc, cpsr, old_r0
83 and r2, r6, #31 @ int mode
90 sub sp, sp, #S_FRAME_SIZE
91 stmia sp, {r0 - r12} @ save r0 - r12
93 add r0, sp, #S_FRAME_SIZE
94 ldmia r2, {r2 - r4} @ get pc, cpsr
99 @ We are now ready to fill in the remaining blanks on the stack:
103 @ r2 - lr_<exception>, already fixed up for correct return/restart
104 @ r3 - spsr_<exception>
105 @ r4 - orig_r0 (see pt_regs definition in ptrace.h)
115 @ get ready to re-enable interrupts if appropriate
119 biceq r9, r9, #PSR_I_BIT
122 @ Call the processor-specific abort handler:
124 @ r2 - aborted context pc
125 @ r3 - aborted context cpsr
127 @ The abort handler must return the aborted address in r0, and
128 @ the fault status register in r1. r9 must be preserved.
139 @ set desired IRQ state, then call main handler
146 @ IRQs off again before pulling preserved data off the stack
151 @ restore SPSR and restart the instruction
155 ldmia sp, {r0 - pc}^ @ load r0 - pc, cpsr
160 #ifdef CONFIG_PREEMPT
162 ldr r8, [tsk, #TI_PREEMPT] @ get preempt count
163 add r7, r8, #1 @ increment it
164 str r7, [tsk, #TI_PREEMPT]
167 #ifdef CONFIG_PREEMPT
168 ldr r0, [tsk, #TI_FLAGS] @ get flags
169 tst r0, #_TIF_NEED_RESCHED
172 ldr r0, [tsk, #TI_PREEMPT] @ read preempt value
173 str r8, [tsk, #TI_PREEMPT] @ restore preempt count
175 strne r0, [r0, -r0] @ bug()
177 ldr r0, [sp, #S_PSR] @ irqs are already disabled
179 ldmia sp, {r0 - pc}^ @ load r0 - pc, cpsr
183 #ifdef CONFIG_PREEMPT
185 teq r8, #0 @ was preempt count = 0
186 ldreq r6, .LCirq_stat
188 ldr r0, [r6, #4] @ local_irq_count
189 ldr r1, [r6, #8] @ local_bh_count
192 mov r7, #0 @ preempt_schedule_irq
193 str r7, [tsk, #TI_PREEMPT] @ expects preempt_count == 0
194 1: bl preempt_schedule_irq @ irq en/disable is done inside
195 ldr r0, [tsk, #TI_FLAGS] @ get new tasks TI_FLAGS
196 tst r0, #_TIF_NEED_RESCHED
197 beq preempt_return @ go again
206 @ call emulation code, which returns using r9 if it has emulated
207 @ the instruction, or the more conventional lr if we are to treat
208 @ this as a real undefined instruction
216 mov r0, sp @ struct pt_regs *regs
220 @ IRQs off again before pulling preserved data off the stack
225 @ restore SPSR and restart the instruction
227 ldr lr, [sp, #S_PSR] @ Get SVC cpsr
229 ldmia sp, {r0 - pc}^ @ Restore SVC registers
236 @ re-enable interrupts if appropriate
240 biceq r9, r9, #PSR_I_BIT
244 @ set args, then call main handler
246 @ r0 - address of faulting instruction
247 @ r1 - pointer to registers on stack
249 mov r0, r2 @ address (pc)
251 bl do_PrefetchAbort @ call abort handler
254 @ IRQs off again before pulling preserved data off the stack
259 @ restore SPSR and restart the instruction
263 ldmia sp, {r0 - pc}^ @ load r0 - pc, cpsr
278 #ifdef CONFIG_PREEMPT
286 .macro usr_entry, sym
287 sub sp, sp, #S_FRAME_SIZE @ Allocate frame size in one go
288 stmia sp, {r0 - r12} @ save r0 - r12
291 ldmia r7, {r2 - r4} @ Get USR pc, cpsr
293 #if __LINUX_ARM_ARCH__ < 6
294 @ make sure our user space atomic helper is aborted
296 bichs r3, r3, #PSR_Z_BIT
300 @ We are now ready to fill in the remaining blanks on the stack:
302 @ r2 - lr_<exception>, already fixed up for correct return/restart
303 @ r3 - spsr_<exception>
304 @ r4 - orig_r0 (see pt_regs definition in ptrace.h)
306 @ Also, separately save sp_usr and lr_usr
312 @ Enable the alignment trap while in kernel mode
314 alignment_trap r7, r0, __temp_\sym
317 @ Clear FP to mark the first stack frame
327 @ Call the processor-specific abort handler:
329 @ r2 - aborted context pc
330 @ r3 - aborted context cpsr
332 @ The abort handler must return the aborted address in r0, and
333 @ the fault status register in r1.
344 @ IRQs on, then call the main handler
348 adr lr, ret_from_exception
356 #ifdef CONFIG_PREEMPT
357 ldr r8, [tsk, #TI_PREEMPT] @ get preempt count
358 add r7, r8, #1 @ increment it
359 str r7, [tsk, #TI_PREEMPT]
362 #ifdef CONFIG_PREEMPT
363 ldr r0, [tsk, #TI_PREEMPT]
364 str r8, [tsk, #TI_PREEMPT]
377 tst r3, #PSR_T_BIT @ Thumb mode?
378 bne fpundefinstr @ ignore FP
382 @ fall through to the emulation code, which returns using r9 if
383 @ it has emulated the instruction, or the more conventional lr
384 @ if we are to treat this as a real undefined instruction
389 adr r9, ret_from_exception
392 @ fallthrough to call_fpe
396 * The out of line fixup for the ldrt above.
398 .section .fixup, "ax"
401 .section __ex_table,"a"
406 * Check whether the instruction is a co-processor instruction.
407 * If yes, we need to call the relevant co-processor handler.
409 * Note that we don't do a full check here for the co-processor
410 * instructions; all instructions with bit 27 set are well
411 * defined. The only instructions that should fault are the
412 * co-processor instructions. However, we have to watch out
413 * for the ARM6/ARM7 SWI bug.
415 * Emulators may wish to make use of the following registers:
416 * r0 = instruction opcode.
418 * r10 = this threads thread_info structure.
421 tst r0, #0x08000000 @ only CDP/CPRT/LDC/STC have bit 27
422 #if defined(CONFIG_CPU_ARM610) || defined(CONFIG_CPU_ARM710)
423 and r8, r0, #0x0f000000 @ mask out op-code bits
424 teqne r8, #0x0f000000 @ SWI (ARM6/7 bug)?
427 get_thread_info r10 @ get current thread
428 and r8, r0, #0x00000f00 @ mask out CP number
430 add r6, r10, #TI_USED_CP
431 strb r7, [r6, r8, lsr #8] @ set appropriate used_cp[]
433 @ Test if we need to give access to iWMMXt coprocessors
434 ldr r5, [r10, #TI_FLAGS]
435 rsbs r7, r8, #(1 << 8) @ CP 0 or 1 only
436 movcss r7, r5, lsr #(TIF_USING_IWMMXT + 1)
437 bcs iwmmxt_task_enable
440 add pc, pc, r8, lsr #6
444 b do_fpe @ CP#1 (FPE)
445 b do_fpe @ CP#2 (FPE)
454 b do_vfp @ CP#10 (VFP)
455 b do_vfp @ CP#11 (VFP)
457 mov pc, lr @ CP#10 (VFP)
458 mov pc, lr @ CP#11 (VFP)
462 mov pc, lr @ CP#14 (Debug)
463 mov pc, lr @ CP#15 (Control)
467 add r10, r10, #TI_FPSTATE @ r10 = workspace
468 ldr pc, [r4] @ Call FP module USR entry point
471 * The FP module is called with these registers set:
474 * r9 = normal "successful" return address
476 * lr = unrecognised FP instruction return address
486 adr lr, ret_from_exception
493 enable_irq @ Enable interrupts
494 mov r0, r2 @ address (pc)
496 bl do_PrefetchAbort @ call abort handler
499 * This is the return code to user mode for abort handlers
501 ENTRY(ret_from_exception)
507 * Register switch for ARMv3 and ARMv4 processors
508 * r0 = previous task_struct, r1 = previous thread_info, r2 = next thread_info
509 * previous and next are guaranteed not to be the same.
512 add ip, r1, #TI_CPU_SAVE
513 ldr r3, [r2, #TI_TP_VALUE]
514 stmia ip!, {r4 - sl, fp, sp, lr} @ Store most regs on stack
515 ldr r6, [r2, #TI_CPU_DOMAIN]!
516 #if defined(CONFIG_CPU_XSCALE) && !defined(CONFIG_IWMMXT)
520 #if defined(CONFIG_HAS_TLS_REG)
521 mcr p15, 0, r3, c13, c0, 3 @ set TLS register
522 #elif !defined(CONFIG_TLS_REG_EMUL)
524 str r3, [r4, #-15] @ TLS val at 0xffff0ff0
526 mcr p15, 0, r6, c3, c0, 0 @ Set domain register
528 @ Always disable VFP so we can lazily save/restore the old
529 @ state. This occurs in the context of the previous thread.
531 bic r4, r4, #FPEXC_ENABLE
534 #if defined(CONFIG_IWMMXT)
535 bl iwmmxt_task_switch
536 #elif defined(CONFIG_CPU_XSCALE)
537 add r4, r2, #40 @ cpu_context_save->extra
541 ldmib r2, {r4 - sl, fp, sp, pc} @ Load all regs saved previously
548 * These are segment of kernel provided user code reachable from user space
549 * at a fixed address in kernel memory. This is used to provide user space
550 * with some operations which require kernel help because of unimplemented
551 * native feature and/or instructions in many ARM CPUs. The idea is for
552 * this code to be executed directly in user mode for best efficiency but
553 * which is too intimate with the kernel counter part to be left to user
554 * libraries. In fact this code might even differ from one CPU to another
555 * depending on the available instruction set and restrictions like on
556 * SMP systems. In other words, the kernel reserves the right to change
557 * this code as needed without warning. Only the entry points and their
558 * results are guaranteed to be stable.
560 * Each segment is 32-byte aligned and will be moved to the top of the high
561 * vector page. New segments (if ever needed) must be added in front of
562 * existing ones. This mechanism should be used only for things that are
563 * really small and justified, and not be abused freely.
565 * User space is expected to implement those things inline when optimizing
566 * for a processor that has the necessary native support, but only if such
567 * resulting binaries are already to be incompatible with earlier ARM
568 * processors due to the use of unsupported instructions other than what
569 * is provided here. In other words don't make binaries unable to run on
570 * earlier processors just for the sake of not using these kernel helpers
571 * if your compiled code is not going to use the new instructions for other
576 .globl __kuser_helper_start
577 __kuser_helper_start:
580 * Reference prototype:
582 * int __kernel_cmpxchg(int oldval, int newval, int *ptr)
589 * lr = return address
593 * r0 = returned value (zero or non-zero)
594 * C flag = set if r0 == 0, clear if r0 != 0
600 * Definition and user space usage example:
602 * typedef int (__kernel_cmpxchg_t)(int oldval, int newval, int *ptr);
603 * #define __kernel_cmpxchg (*(__kernel_cmpxchg_t *)0xffff0fc0)
605 * Atomically store newval in *ptr if *ptr is equal to oldval for user space.
606 * Return zero if *ptr was changed or non-zero if no exchange happened.
607 * The C flag is also set if *ptr was changed to allow for assembly
608 * optimization in the calling code.
610 * For example, a user space atomic_add implementation could look like this:
612 * #define atomic_add(ptr, val) \
613 * ({ register unsigned int *__ptr asm("r2") = (ptr); \
614 * register unsigned int __result asm("r1"); \
616 * "1: @ atomic_add\n\t" \
617 * "ldr r0, [r2]\n\t" \
618 * "mov r3, #0xffff0fff\n\t" \
619 * "add lr, pc, #4\n\t" \
620 * "add r1, r0, %2\n\t" \
621 * "add pc, r3, #(0xffff0fc0 - 0xffff0fff)\n\t" \
623 * : "=&r" (__result) \
624 * : "r" (__ptr), "rIL" (val) \
625 * : "r0","r3","ip","lr","cc","memory" ); \
629 __kuser_cmpxchg: @ 0xffff0fc0
631 #if __LINUX_ARM_ARCH__ < 6
633 #ifdef CONFIG_SMP /* sanity check */
634 #error "CONFIG_SMP on a machine supporting pre-ARMv6 processors?"
638 * Theory of operation:
640 * We set the Z flag before loading oldval. If ever an exception
641 * occurs we can not be sure the loaded value will still be the same
642 * when the exception returns, therefore the user exception handler
643 * will clear the Z flag whenever the interrupted user code was
644 * actually from the kernel address space (see the usr_entry macro).
646 * The post-increment on the str is used to prevent a race with an
647 * exception happening just after the str instruction which would
648 * clear the Z flag although the exchange was done.
650 teq ip, ip @ set Z flag
651 ldr ip, [r2] @ load current val
652 add r3, r2, #1 @ prepare store ptr
653 teqeq ip, r0 @ compare with oldval if still allowed
654 streq r1, [r3, #-1]! @ store newval if still allowed
655 subs r0, r2, r3 @ if r2 == r3 the str occured
671 * Reference prototype:
673 * int __kernel_get_tls(void)
677 * lr = return address
685 * the Z flag might be lost
687 * Definition and user space usage example:
689 * typedef int (__kernel_get_tls_t)(void);
690 * #define __kernel_get_tls (*(__kernel_get_tls_t *)0xffff0fe0)
692 * Get the TLS value as previously set via the __ARM_NR_set_tls syscall.
694 * This could be used as follows:
696 * #define __kernel_get_tls() \
697 * ({ register unsigned int __val asm("r0"); \
698 * asm( "mov r0, #0xffff0fff; mov lr, pc; sub pc, r0, #31" \
699 * : "=r" (__val) : : "lr","cc" ); \
703 __kuser_get_tls: @ 0xffff0fe0
705 #if !defined(CONFIG_HAS_TLS_REG) && !defined(CONFIG_TLS_REG_EMUL)
707 ldr r0, [pc, #(16 - 8)] @ TLS stored at 0xffff0ff0
712 mrc p15, 0, r0, c13, c0, 3 @ read TLS register
718 .word 0 @ pad up to __kuser_helper_version
722 * Reference declaration:
724 * extern unsigned int __kernel_helper_version;
726 * Definition and user space usage example:
728 * #define __kernel_helper_version (*(unsigned int *)0xffff0ffc)
730 * User space may read this to determine the curent number of helpers
734 __kuser_helper_version: @ 0xffff0ffc
735 .word ((__kuser_helper_end - __kuser_helper_start) >> 5)
737 .globl __kuser_helper_end
744 * This code is copied to 0xffff0200 so we can use branches in the
745 * vectors, rather than ldr's. Note that this code must not
746 * exceed 0x300 bytes.
748 * Common stub entry macro:
749 * Enter in IRQ mode, spsr = SVC/USR CPSR, lr = SVC/USR PC
751 .macro vector_stub, name, sym, correction=0
757 sub lr, lr, #\correction
759 str lr, [r13] @ save lr_IRQ
761 str lr, [r13, #4] @ save spsr_IRQ
763 @ now branch to the relevant MODE handling routine
766 bic r13, r13, #MODE_MASK
767 orr r13, r13, #SVC_MODE
768 msr spsr_cxsf, r13 @ switch to SVC_32 mode
771 ldr lr, [pc, lr, lsl #2]
772 movs pc, lr @ Changes mode and branches
778 * Interrupt dispatcher
780 vector_stub irq, irq, 4
782 .long __irq_usr @ 0 (USR_26 / USR_32)
783 .long __irq_invalid @ 1 (FIQ_26 / FIQ_32)
784 .long __irq_invalid @ 2 (IRQ_26 / IRQ_32)
785 .long __irq_svc @ 3 (SVC_26 / SVC_32)
786 .long __irq_invalid @ 4
787 .long __irq_invalid @ 5
788 .long __irq_invalid @ 6
789 .long __irq_invalid @ 7
790 .long __irq_invalid @ 8
791 .long __irq_invalid @ 9
792 .long __irq_invalid @ a
793 .long __irq_invalid @ b
794 .long __irq_invalid @ c
795 .long __irq_invalid @ d
796 .long __irq_invalid @ e
797 .long __irq_invalid @ f
800 * Data abort dispatcher
801 * Enter in ABT mode, spsr = USR CPSR, lr = USR PC
803 vector_stub dabt, abt, 8
805 .long __dabt_usr @ 0 (USR_26 / USR_32)
806 .long __dabt_invalid @ 1 (FIQ_26 / FIQ_32)
807 .long __dabt_invalid @ 2 (IRQ_26 / IRQ_32)
808 .long __dabt_svc @ 3 (SVC_26 / SVC_32)
809 .long __dabt_invalid @ 4
810 .long __dabt_invalid @ 5
811 .long __dabt_invalid @ 6
812 .long __dabt_invalid @ 7
813 .long __dabt_invalid @ 8
814 .long __dabt_invalid @ 9
815 .long __dabt_invalid @ a
816 .long __dabt_invalid @ b
817 .long __dabt_invalid @ c
818 .long __dabt_invalid @ d
819 .long __dabt_invalid @ e
820 .long __dabt_invalid @ f
823 * Prefetch abort dispatcher
824 * Enter in ABT mode, spsr = USR CPSR, lr = USR PC
826 vector_stub pabt, abt, 4
828 .long __pabt_usr @ 0 (USR_26 / USR_32)
829 .long __pabt_invalid @ 1 (FIQ_26 / FIQ_32)
830 .long __pabt_invalid @ 2 (IRQ_26 / IRQ_32)
831 .long __pabt_svc @ 3 (SVC_26 / SVC_32)
832 .long __pabt_invalid @ 4
833 .long __pabt_invalid @ 5
834 .long __pabt_invalid @ 6
835 .long __pabt_invalid @ 7
836 .long __pabt_invalid @ 8
837 .long __pabt_invalid @ 9
838 .long __pabt_invalid @ a
839 .long __pabt_invalid @ b
840 .long __pabt_invalid @ c
841 .long __pabt_invalid @ d
842 .long __pabt_invalid @ e
843 .long __pabt_invalid @ f
846 * Undef instr entry dispatcher
847 * Enter in UND mode, spsr = SVC/USR CPSR, lr = SVC/USR PC
851 .long __und_usr @ 0 (USR_26 / USR_32)
852 .long __und_invalid @ 1 (FIQ_26 / FIQ_32)
853 .long __und_invalid @ 2 (IRQ_26 / IRQ_32)
854 .long __und_svc @ 3 (SVC_26 / SVC_32)
855 .long __und_invalid @ 4
856 .long __und_invalid @ 5
857 .long __und_invalid @ 6
858 .long __und_invalid @ 7
859 .long __und_invalid @ 8
860 .long __und_invalid @ 9
861 .long __und_invalid @ a
862 .long __und_invalid @ b
863 .long __und_invalid @ c
864 .long __und_invalid @ d
865 .long __und_invalid @ e
866 .long __und_invalid @ f
870 /*=============================================================================
872 *-----------------------------------------------------------------------------
873 * Enter in FIQ mode, spsr = ANY CPSR, lr = ANY PC
874 * MUST PRESERVE SVC SPSR, but need to switch to SVC mode to show our msg.
875 * Basically to switch modes, we *HAVE* to clobber one register... brain
876 * damage alert! I don't think that we can execute any code in here in any
877 * other mode than FIQ... Ok you can switch to another mode, but you can't
878 * get out of that mode without clobbering one register.
884 /*=============================================================================
885 * Address exception handler
886 *-----------------------------------------------------------------------------
887 * These aren't too critical.
888 * (they're not supposed to happen, and won't happen in 32-bit data mode).
895 * We group all the following data together to optimise
896 * for CPUs with separate I & D caches.
913 .equ stubs_offset, __vectors_start + 0x200 - __stubs_start
915 .globl __vectors_start
918 b vector_und + stubs_offset
919 ldr pc, .LCvswi + stubs_offset
920 b vector_pabt + stubs_offset
921 b vector_dabt + stubs_offset
922 b vector_addrexcptn + stubs_offset
923 b vector_irq + stubs_offset
924 b vector_fiq + stubs_offset
932 * Do not reorder these, and do not insert extra data between...
936 .word 0 @ saved lr_irq
937 .word 0 @ saved spsr_irq
940 .word 0 @ Saved lr_und
941 .word 0 @ Saved spsr_und
944 .word 0 @ Saved lr_abt
945 .word 0 @ Saved spsr_abt
949 .globl cr_no_alignment