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>
18 #include <asm/memory.h>
20 #include <asm/vfpmacros.h>
21 #include <asm/hardware.h> /* should be moved into entry-macro.S */
22 #include <asm/arch/irqs.h> /* should be moved into entry-macro.S */
23 #include <asm/arch/entry-macro.S>
25 #include "entry-header.S"
28 * Interrupt handling. Preserves r7, r8, r9
31 1: get_irqnr_and_base r0, r6, r5, lr
34 @ routine called with r0 = irq number, r1 = struct pt_regs *
43 * this macro assumes that irqstat (r6) and base (r5) are
44 * preserved from get_irqnr_and_base above
46 test_for_ipi r0, r6, r5, lr
51 #ifdef CONFIG_LOCAL_TIMERS
52 test_for_ltirq r0, r6, r5, lr
62 * Invalid mode handlers
64 .macro inv_entry, reason
65 sub sp, sp, #S_FRAME_SIZE
71 inv_entry BAD_PREFETCH
83 inv_entry BAD_UNDEFINSTR
86 @ XXX fall through to common_invalid
90 @ common_invalid - generic code for failed exception (re-entrant version of handlers)
96 add r0, sp, #S_PC @ here for interlock avoidance
97 mov r7, #-1 @ "" "" "" ""
98 str r4, [sp] @ save preserved r0
99 stmia r0, {r5 - r7} @ lr_<exception>,
100 @ cpsr_<exception>, "old_r0"
110 sub sp, sp, #S_FRAME_SIZE
114 add r5, sp, #S_SP @ here for interlock avoidance
115 mov r4, #-1 @ "" "" "" ""
116 add r0, sp, #S_FRAME_SIZE @ "" "" "" ""
117 str r1, [sp] @ save the "real" r0 copied
118 @ from the exception stack
123 @ We are now ready to fill in the remaining blanks on the stack:
127 @ r2 - lr_<exception>, already fixed up for correct return/restart
128 @ r3 - spsr_<exception>
129 @ r4 - orig_r0 (see pt_regs definition in ptrace.h)
139 @ get ready to re-enable interrupts if appropriate
143 biceq r9, r9, #PSR_I_BIT
146 @ Call the processor-specific abort handler:
148 @ r2 - aborted context pc
149 @ r3 - aborted context cpsr
151 @ The abort handler must return the aborted address in r0, and
152 @ the fault status register in r1. r9 must be preserved.
163 @ set desired IRQ state, then call main handler
170 @ IRQs off again before pulling preserved data off the stack
175 @ restore SPSR and restart the instruction
179 ldmia sp, {r0 - pc}^ @ load r0 - pc, cpsr
185 #ifdef CONFIG_PREEMPT
187 ldr r8, [tsk, #TI_PREEMPT] @ get preempt count
188 add r7, r8, #1 @ increment it
189 str r7, [tsk, #TI_PREEMPT]
193 #ifdef CONFIG_PREEMPT
194 ldr r0, [tsk, #TI_FLAGS] @ get flags
195 tst r0, #_TIF_NEED_RESCHED
198 ldr r0, [tsk, #TI_PREEMPT] @ read preempt value
199 str r8, [tsk, #TI_PREEMPT] @ restore preempt count
201 strne r0, [r0, -r0] @ bug()
203 ldr r0, [sp, #S_PSR] @ irqs are already disabled
205 ldmia sp, {r0 - pc}^ @ load r0 - pc, cpsr
209 #ifdef CONFIG_PREEMPT
211 teq r8, #0 @ was preempt count = 0
212 ldreq r6, .LCirq_stat
214 ldr r0, [r6, #4] @ local_irq_count
215 ldr r1, [r6, #8] @ local_bh_count
218 mov r7, #0 @ preempt_schedule_irq
219 str r7, [tsk, #TI_PREEMPT] @ expects preempt_count == 0
220 1: bl preempt_schedule_irq @ irq en/disable is done inside
221 ldr r0, [tsk, #TI_FLAGS] @ get new tasks TI_FLAGS
222 tst r0, #_TIF_NEED_RESCHED
223 beq preempt_return @ go again
232 @ call emulation code, which returns using r9 if it has emulated
233 @ the instruction, or the more conventional lr if we are to treat
234 @ this as a real undefined instruction
242 mov r0, sp @ struct pt_regs *regs
246 @ IRQs off again before pulling preserved data off the stack
251 @ restore SPSR and restart the instruction
253 ldr lr, [sp, #S_PSR] @ Get SVC cpsr
255 ldmia sp, {r0 - pc}^ @ Restore SVC registers
262 @ re-enable interrupts if appropriate
266 biceq r9, r9, #PSR_I_BIT
270 @ set args, then call main handler
272 @ r0 - address of faulting instruction
273 @ r1 - pointer to registers on stack
275 mov r0, r2 @ address (pc)
277 bl do_PrefetchAbort @ call abort handler
280 @ IRQs off again before pulling preserved data off the stack
285 @ restore SPSR and restart the instruction
289 ldmia sp, {r0 - pc}^ @ load r0 - pc, cpsr
300 #ifdef CONFIG_PREEMPT
309 sub sp, sp, #S_FRAME_SIZE
313 add r0, sp, #S_PC @ here for interlock avoidance
314 mov r4, #-1 @ "" "" "" ""
316 str r1, [sp] @ save the "real" r0 copied
317 @ from the exception stack
319 #if __LINUX_ARM_ARCH__ < 6 && !defined(CONFIG_NEEDS_SYSCALL_FOR_CMPXCHG)
320 @ make sure our user space atomic helper is aborted
322 bichs r3, r3, #PSR_Z_BIT
326 @ We are now ready to fill in the remaining blanks on the stack:
328 @ r2 - lr_<exception>, already fixed up for correct return/restart
329 @ r3 - spsr_<exception>
330 @ r4 - orig_r0 (see pt_regs definition in ptrace.h)
332 @ Also, separately save sp_usr and lr_usr
338 @ Enable the alignment trap while in kernel mode
343 @ Clear FP to mark the first stack frame
353 @ Call the processor-specific abort handler:
355 @ r2 - aborted context pc
356 @ r3 - aborted context cpsr
358 @ The abort handler must return the aborted address in r0, and
359 @ the fault status register in r1.
370 @ IRQs on, then call the main handler
374 adr lr, ret_from_exception
382 #ifdef CONFIG_PREEMPT
383 ldr r8, [tsk, #TI_PREEMPT] @ get preempt count
384 add r7, r8, #1 @ increment it
385 str r7, [tsk, #TI_PREEMPT]
389 #ifdef CONFIG_PREEMPT
390 ldr r0, [tsk, #TI_PREEMPT]
391 str r8, [tsk, #TI_PREEMPT]
405 tst r3, #PSR_T_BIT @ Thumb mode?
406 bne fpundefinstr @ ignore FP
410 @ fall through to the emulation code, which returns using r9 if
411 @ it has emulated the instruction, or the more conventional lr
412 @ if we are to treat this as a real undefined instruction
417 adr r9, ret_from_exception
420 @ fallthrough to call_fpe
424 * The out of line fixup for the ldrt above.
426 .section .fixup, "ax"
429 .section __ex_table,"a"
434 * Check whether the instruction is a co-processor instruction.
435 * If yes, we need to call the relevant co-processor handler.
437 * Note that we don't do a full check here for the co-processor
438 * instructions; all instructions with bit 27 set are well
439 * defined. The only instructions that should fault are the
440 * co-processor instructions. However, we have to watch out
441 * for the ARM6/ARM7 SWI bug.
443 * Emulators may wish to make use of the following registers:
444 * r0 = instruction opcode.
446 * r10 = this threads thread_info structure.
449 tst r0, #0x08000000 @ only CDP/CPRT/LDC/STC have bit 27
450 #if defined(CONFIG_CPU_ARM610) || defined(CONFIG_CPU_ARM710)
451 and r8, r0, #0x0f000000 @ mask out op-code bits
452 teqne r8, #0x0f000000 @ SWI (ARM6/7 bug)?
455 get_thread_info r10 @ get current thread
456 and r8, r0, #0x00000f00 @ mask out CP number
458 add r6, r10, #TI_USED_CP
459 strb r7, [r6, r8, lsr #8] @ set appropriate used_cp[]
461 @ Test if we need to give access to iWMMXt coprocessors
462 ldr r5, [r10, #TI_FLAGS]
463 rsbs r7, r8, #(1 << 8) @ CP 0 or 1 only
464 movcss r7, r5, lsr #(TIF_USING_IWMMXT + 1)
465 bcs iwmmxt_task_enable
468 add pc, pc, r8, lsr #6
472 b do_fpe @ CP#1 (FPE)
473 b do_fpe @ CP#2 (FPE)
482 b do_vfp @ CP#10 (VFP)
483 b do_vfp @ CP#11 (VFP)
485 mov pc, lr @ CP#10 (VFP)
486 mov pc, lr @ CP#11 (VFP)
490 mov pc, lr @ CP#14 (Debug)
491 mov pc, lr @ CP#15 (Control)
495 add r10, r10, #TI_FPSTATE @ r10 = workspace
496 ldr pc, [r4] @ Call FP module USR entry point
499 * The FP module is called with these registers set:
502 * r9 = normal "successful" return address
504 * lr = unrecognised FP instruction return address
514 adr lr, ret_from_exception
521 enable_irq @ Enable interrupts
522 mov r0, r2 @ address (pc)
524 bl do_PrefetchAbort @ call abort handler
527 * This is the return code to user mode for abort handlers
529 ENTRY(ret_from_exception)
535 * Register switch for ARMv3 and ARMv4 processors
536 * r0 = previous task_struct, r1 = previous thread_info, r2 = next thread_info
537 * previous and next are guaranteed not to be the same.
540 add ip, r1, #TI_CPU_SAVE
541 ldr r3, [r2, #TI_TP_VALUE]
542 stmia ip!, {r4 - sl, fp, sp, lr} @ Store most regs on stack
543 ldr r6, [r2, #TI_CPU_DOMAIN]!
544 #if __LINUX_ARM_ARCH__ >= 6
545 #ifdef CONFIG_CPU_MPCORE
548 strex r5, r4, [ip] @ Clear exclusive monitor
551 #if defined(CONFIG_CPU_XSCALE) && !defined(CONFIG_IWMMXT)
555 #if defined(CONFIG_HAS_TLS_REG)
556 mcr p15, 0, r3, c13, c0, 3 @ set TLS register
557 #elif !defined(CONFIG_TLS_REG_EMUL)
559 str r3, [r4, #-15] @ TLS val at 0xffff0ff0
561 mcr p15, 0, r6, c3, c0, 0 @ Set domain register
563 @ Always disable VFP so we can lazily save/restore the old
564 @ state. This occurs in the context of the previous thread.
566 bic r4, r4, #FPEXC_ENABLE
569 #if defined(CONFIG_IWMMXT)
570 bl iwmmxt_task_switch
571 #elif defined(CONFIG_CPU_XSCALE)
572 add r4, r2, #40 @ cpu_context_save->extra
576 ldmib r2, {r4 - sl, fp, sp, pc} @ Load all regs saved previously
583 * These are segment of kernel provided user code reachable from user space
584 * at a fixed address in kernel memory. This is used to provide user space
585 * with some operations which require kernel help because of unimplemented
586 * native feature and/or instructions in many ARM CPUs. The idea is for
587 * this code to be executed directly in user mode for best efficiency but
588 * which is too intimate with the kernel counter part to be left to user
589 * libraries. In fact this code might even differ from one CPU to another
590 * depending on the available instruction set and restrictions like on
591 * SMP systems. In other words, the kernel reserves the right to change
592 * this code as needed without warning. Only the entry points and their
593 * results are guaranteed to be stable.
595 * Each segment is 32-byte aligned and will be moved to the top of the high
596 * vector page. New segments (if ever needed) must be added in front of
597 * existing ones. This mechanism should be used only for things that are
598 * really small and justified, and not be abused freely.
600 * User space is expected to implement those things inline when optimizing
601 * for a processor that has the necessary native support, but only if such
602 * resulting binaries are already to be incompatible with earlier ARM
603 * processors due to the use of unsupported instructions other than what
604 * is provided here. In other words don't make binaries unable to run on
605 * earlier processors just for the sake of not using these kernel helpers
606 * if your compiled code is not going to use the new instructions for other
611 .globl __kuser_helper_start
612 __kuser_helper_start:
615 * Reference prototype:
617 * void __kernel_memory_barrier(void)
621 * lr = return address
629 * the Z flag might be lost
631 * Definition and user space usage example:
633 * typedef void (__kernel_dmb_t)(void);
634 * #define __kernel_dmb (*(__kernel_dmb_t *)0xffff0fa0)
636 * Apply any needed memory barrier to preserve consistency with data modified
637 * manually and __kuser_cmpxchg usage.
639 * This could be used as follows:
641 * #define __kernel_dmb() \
642 * asm volatile ( "mov r0, #0xffff0fff; mov lr, pc; sub pc, r0, #95" \
646 __kuser_memory_barrier: @ 0xffff0fa0
648 #if __LINUX_ARM_ARCH__ >= 6 && defined(CONFIG_SMP)
649 mcr p15, 0, r0, c7, c10, 5 @ dmb
656 * Reference prototype:
658 * int __kernel_cmpxchg(int oldval, int newval, int *ptr)
665 * lr = return address
669 * r0 = returned value (zero or non-zero)
670 * C flag = set if r0 == 0, clear if r0 != 0
676 * Definition and user space usage example:
678 * typedef int (__kernel_cmpxchg_t)(int oldval, int newval, int *ptr);
679 * #define __kernel_cmpxchg (*(__kernel_cmpxchg_t *)0xffff0fc0)
681 * Atomically store newval in *ptr if *ptr is equal to oldval for user space.
682 * Return zero if *ptr was changed or non-zero if no exchange happened.
683 * The C flag is also set if *ptr was changed to allow for assembly
684 * optimization in the calling code.
686 * Note: this routine already includes memory barriers as needed.
688 * For example, a user space atomic_add implementation could look like this:
690 * #define atomic_add(ptr, val) \
691 * ({ register unsigned int *__ptr asm("r2") = (ptr); \
692 * register unsigned int __result asm("r1"); \
694 * "1: @ atomic_add\n\t" \
695 * "ldr r0, [r2]\n\t" \
696 * "mov r3, #0xffff0fff\n\t" \
697 * "add lr, pc, #4\n\t" \
698 * "add r1, r0, %2\n\t" \
699 * "add pc, r3, #(0xffff0fc0 - 0xffff0fff)\n\t" \
701 * : "=&r" (__result) \
702 * : "r" (__ptr), "rIL" (val) \
703 * : "r0","r3","ip","lr","cc","memory" ); \
707 __kuser_cmpxchg: @ 0xffff0fc0
709 #if defined(CONFIG_NEEDS_SYSCALL_FOR_CMPXCHG)
712 * Poor you. No fast solution possible...
713 * The kernel itself must perform the operation.
714 * A special ghost syscall is used for that (see traps.c).
719 #elif __LINUX_ARM_ARCH__ < 6
722 * Theory of operation:
724 * We set the Z flag before loading oldval. If ever an exception
725 * occurs we can not be sure the loaded value will still be the same
726 * when the exception returns, therefore the user exception handler
727 * will clear the Z flag whenever the interrupted user code was
728 * actually from the kernel address space (see the usr_entry macro).
730 * The post-increment on the str is used to prevent a race with an
731 * exception happening just after the str instruction which would
732 * clear the Z flag although the exchange was done.
734 teq ip, ip @ set Z flag
735 ldr ip, [r2] @ load current val
736 add r3, r2, #1 @ prepare store ptr
737 teqeq ip, r0 @ compare with oldval if still allowed
738 streq r1, [r3, #-1]! @ store newval if still allowed
739 subs r0, r2, r3 @ if r2 == r3 the str occured
745 mcr p15, 0, r0, c7, c10, 5 @ dmb
752 mcr p15, 0, r0, c7, c10, 5 @ dmb
761 * Reference prototype:
763 * int __kernel_get_tls(void)
767 * lr = return address
775 * the Z flag might be lost
777 * Definition and user space usage example:
779 * typedef int (__kernel_get_tls_t)(void);
780 * #define __kernel_get_tls (*(__kernel_get_tls_t *)0xffff0fe0)
782 * Get the TLS value as previously set via the __ARM_NR_set_tls syscall.
784 * This could be used as follows:
786 * #define __kernel_get_tls() \
787 * ({ register unsigned int __val asm("r0"); \
788 * asm( "mov r0, #0xffff0fff; mov lr, pc; sub pc, r0, #31" \
789 * : "=r" (__val) : : "lr","cc" ); \
793 __kuser_get_tls: @ 0xffff0fe0
795 #if !defined(CONFIG_HAS_TLS_REG) && !defined(CONFIG_TLS_REG_EMUL)
797 ldr r0, [pc, #(16 - 8)] @ TLS stored at 0xffff0ff0
802 mrc p15, 0, r0, c13, c0, 3 @ read TLS register
808 .word 0 @ pad up to __kuser_helper_version
812 * Reference declaration:
814 * extern unsigned int __kernel_helper_version;
816 * Definition and user space usage example:
818 * #define __kernel_helper_version (*(unsigned int *)0xffff0ffc)
820 * User space may read this to determine the curent number of helpers
824 __kuser_helper_version: @ 0xffff0ffc
825 .word ((__kuser_helper_end - __kuser_helper_start) >> 5)
827 .globl __kuser_helper_end
834 * This code is copied to 0xffff0200 so we can use branches in the
835 * vectors, rather than ldr's. Note that this code must not
836 * exceed 0x300 bytes.
838 * Common stub entry macro:
839 * Enter in IRQ mode, spsr = SVC/USR CPSR, lr = SVC/USR PC
841 * SP points to a minimal amount of processor-private memory, the address
842 * of which is copied into r0 for the mode specific abort handler.
844 .macro vector_stub, name, mode, correction=0
849 sub lr, lr, #\correction
853 @ Save r0, lr_<exception> (parent PC) and spsr_<exception>
856 stmia sp, {r0, lr} @ save r0, lr
858 str lr, [sp, #8] @ save spsr
861 @ Prepare for SVC32 mode. IRQs remain disabled.
864 eor r0, r0, #(\mode ^ SVC_MODE)
868 @ the branch table must immediately follow this code
872 ldr lr, [pc, lr, lsl #2]
873 movs pc, lr @ branch to handler in SVC mode
879 * Interrupt dispatcher
881 vector_stub irq, IRQ_MODE, 4
883 .long __irq_usr @ 0 (USR_26 / USR_32)
884 .long __irq_invalid @ 1 (FIQ_26 / FIQ_32)
885 .long __irq_invalid @ 2 (IRQ_26 / IRQ_32)
886 .long __irq_svc @ 3 (SVC_26 / SVC_32)
887 .long __irq_invalid @ 4
888 .long __irq_invalid @ 5
889 .long __irq_invalid @ 6
890 .long __irq_invalid @ 7
891 .long __irq_invalid @ 8
892 .long __irq_invalid @ 9
893 .long __irq_invalid @ a
894 .long __irq_invalid @ b
895 .long __irq_invalid @ c
896 .long __irq_invalid @ d
897 .long __irq_invalid @ e
898 .long __irq_invalid @ f
901 * Data abort dispatcher
902 * Enter in ABT mode, spsr = USR CPSR, lr = USR PC
904 vector_stub dabt, ABT_MODE, 8
906 .long __dabt_usr @ 0 (USR_26 / USR_32)
907 .long __dabt_invalid @ 1 (FIQ_26 / FIQ_32)
908 .long __dabt_invalid @ 2 (IRQ_26 / IRQ_32)
909 .long __dabt_svc @ 3 (SVC_26 / SVC_32)
910 .long __dabt_invalid @ 4
911 .long __dabt_invalid @ 5
912 .long __dabt_invalid @ 6
913 .long __dabt_invalid @ 7
914 .long __dabt_invalid @ 8
915 .long __dabt_invalid @ 9
916 .long __dabt_invalid @ a
917 .long __dabt_invalid @ b
918 .long __dabt_invalid @ c
919 .long __dabt_invalid @ d
920 .long __dabt_invalid @ e
921 .long __dabt_invalid @ f
924 * Prefetch abort dispatcher
925 * Enter in ABT mode, spsr = USR CPSR, lr = USR PC
927 vector_stub pabt, ABT_MODE, 4
929 .long __pabt_usr @ 0 (USR_26 / USR_32)
930 .long __pabt_invalid @ 1 (FIQ_26 / FIQ_32)
931 .long __pabt_invalid @ 2 (IRQ_26 / IRQ_32)
932 .long __pabt_svc @ 3 (SVC_26 / SVC_32)
933 .long __pabt_invalid @ 4
934 .long __pabt_invalid @ 5
935 .long __pabt_invalid @ 6
936 .long __pabt_invalid @ 7
937 .long __pabt_invalid @ 8
938 .long __pabt_invalid @ 9
939 .long __pabt_invalid @ a
940 .long __pabt_invalid @ b
941 .long __pabt_invalid @ c
942 .long __pabt_invalid @ d
943 .long __pabt_invalid @ e
944 .long __pabt_invalid @ f
947 * Undef instr entry dispatcher
948 * Enter in UND mode, spsr = SVC/USR CPSR, lr = SVC/USR PC
950 vector_stub und, UND_MODE
952 .long __und_usr @ 0 (USR_26 / USR_32)
953 .long __und_invalid @ 1 (FIQ_26 / FIQ_32)
954 .long __und_invalid @ 2 (IRQ_26 / IRQ_32)
955 .long __und_svc @ 3 (SVC_26 / SVC_32)
956 .long __und_invalid @ 4
957 .long __und_invalid @ 5
958 .long __und_invalid @ 6
959 .long __und_invalid @ 7
960 .long __und_invalid @ 8
961 .long __und_invalid @ 9
962 .long __und_invalid @ a
963 .long __und_invalid @ b
964 .long __und_invalid @ c
965 .long __und_invalid @ d
966 .long __und_invalid @ e
967 .long __und_invalid @ f
971 /*=============================================================================
973 *-----------------------------------------------------------------------------
974 * Enter in FIQ mode, spsr = ANY CPSR, lr = ANY PC
975 * MUST PRESERVE SVC SPSR, but need to switch to SVC mode to show our msg.
976 * Basically to switch modes, we *HAVE* to clobber one register... brain
977 * damage alert! I don't think that we can execute any code in here in any
978 * other mode than FIQ... Ok you can switch to another mode, but you can't
979 * get out of that mode without clobbering one register.
985 /*=============================================================================
986 * Address exception handler
987 *-----------------------------------------------------------------------------
988 * These aren't too critical.
989 * (they're not supposed to happen, and won't happen in 32-bit data mode).
996 * We group all the following data together to optimise
997 * for CPUs with separate I & D caches.
1007 .equ stubs_offset, __vectors_start + 0x200 - __stubs_start
1009 .globl __vectors_start
1012 b vector_und + stubs_offset
1013 ldr pc, .LCvswi + stubs_offset
1014 b vector_pabt + stubs_offset
1015 b vector_dabt + stubs_offset
1016 b vector_addrexcptn + stubs_offset
1017 b vector_irq + stubs_offset
1018 b vector_fiq + stubs_offset
1020 .globl __vectors_end
1026 .globl cr_no_alignment