xref: /openbmc/linux/arch/arm/kernel/entry-armv.S (revision d30a0c8bf99f0e6a7d8c57bd4524039585ffbced)
11da177e4SLinus Torvalds/*
21da177e4SLinus Torvalds *  linux/arch/arm/kernel/entry-armv.S
31da177e4SLinus Torvalds *
41da177e4SLinus Torvalds *  Copyright (C) 1996,1997,1998 Russell King.
51da177e4SLinus Torvalds *  ARM700 fix by Matthew Godbolt (linux-user@willothewisp.demon.co.uk)
6afeb90caSHyok S. Choi *  nommu support by Hyok S. Choi (hyok.choi@samsung.com)
71da177e4SLinus Torvalds *
81da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify
91da177e4SLinus Torvalds * it under the terms of the GNU General Public License version 2 as
101da177e4SLinus Torvalds * published by the Free Software Foundation.
111da177e4SLinus Torvalds *
121da177e4SLinus Torvalds *  Low-level vector interface routines
131da177e4SLinus Torvalds *
141da177e4SLinus Torvalds *  Note:  there is a StrongARM bug in the STMIA rn, {regs}^ instruction that causes
151da177e4SLinus Torvalds *  it to save wrong values...  Be aware!
161da177e4SLinus Torvalds */
171da177e4SLinus Torvalds
18f09b9979SNicolas Pitre#include <asm/memory.h>
191da177e4SLinus Torvalds#include <asm/glue.h>
201da177e4SLinus Torvalds#include <asm/vfpmacros.h>
21bce495d8SRussell King#include <asm/arch/entry-macro.S>
22d6551e88SRussell King#include <asm/thread_notify.h>
231da177e4SLinus Torvalds
241da177e4SLinus Torvalds#include "entry-header.S"
251da177e4SLinus Torvalds
261da177e4SLinus Torvalds/*
27187a51adSRussell King * Interrupt handling.  Preserves r7, r8, r9
28187a51adSRussell King */
29187a51adSRussell King	.macro	irq_handler
30f80dff9dSDan Williams	get_irqnr_preamble r5, lr
31187a51adSRussell King1:	get_irqnr_and_base r0, r6, r5, lr
32187a51adSRussell King	movne	r1, sp
33187a51adSRussell King	@
34187a51adSRussell King	@ routine called with r0 = irq number, r1 = struct pt_regs *
35187a51adSRussell King	@
36187a51adSRussell King	adrne	lr, 1b
37187a51adSRussell King	bne	asm_do_IRQ
38791be9b9SRussell King
39791be9b9SRussell King#ifdef CONFIG_SMP
40791be9b9SRussell King	/*
41791be9b9SRussell King	 * XXX
42791be9b9SRussell King	 *
43791be9b9SRussell King	 * this macro assumes that irqstat (r6) and base (r5) are
44791be9b9SRussell King	 * preserved from get_irqnr_and_base above
45791be9b9SRussell King	 */
46791be9b9SRussell King	test_for_ipi r0, r6, r5, lr
47791be9b9SRussell King	movne	r0, sp
48791be9b9SRussell King	adrne	lr, 1b
49791be9b9SRussell King	bne	do_IPI
5037ee16aeSRussell King
5137ee16aeSRussell King#ifdef CONFIG_LOCAL_TIMERS
5237ee16aeSRussell King	test_for_ltirq r0, r6, r5, lr
5337ee16aeSRussell King	movne	r0, sp
5437ee16aeSRussell King	adrne	lr, 1b
5537ee16aeSRussell King	bne	do_local_timer
5637ee16aeSRussell King#endif
57791be9b9SRussell King#endif
58791be9b9SRussell King
59187a51adSRussell King	.endm
60187a51adSRussell King
61187a51adSRussell King/*
621da177e4SLinus Torvalds * Invalid mode handlers
631da177e4SLinus Torvalds */
64ccea7a19SRussell King	.macro	inv_entry, reason
65ccea7a19SRussell King	sub	sp, sp, #S_FRAME_SIZE
66ccea7a19SRussell King	stmib	sp, {r1 - lr}
671da177e4SLinus Torvalds	mov	r1, #\reason
681da177e4SLinus Torvalds	.endm
691da177e4SLinus Torvalds
701da177e4SLinus Torvalds__pabt_invalid:
71ccea7a19SRussell King	inv_entry BAD_PREFETCH
72ccea7a19SRussell King	b	common_invalid
731da177e4SLinus Torvalds
741da177e4SLinus Torvalds__dabt_invalid:
75ccea7a19SRussell King	inv_entry BAD_DATA
76ccea7a19SRussell King	b	common_invalid
771da177e4SLinus Torvalds
781da177e4SLinus Torvalds__irq_invalid:
79ccea7a19SRussell King	inv_entry BAD_IRQ
80ccea7a19SRussell King	b	common_invalid
811da177e4SLinus Torvalds
821da177e4SLinus Torvalds__und_invalid:
83ccea7a19SRussell King	inv_entry BAD_UNDEFINSTR
841da177e4SLinus Torvalds
85ccea7a19SRussell King	@
86ccea7a19SRussell King	@ XXX fall through to common_invalid
87ccea7a19SRussell King	@
88ccea7a19SRussell King
89ccea7a19SRussell King@
90ccea7a19SRussell King@ common_invalid - generic code for failed exception (re-entrant version of handlers)
91ccea7a19SRussell King@
92ccea7a19SRussell Kingcommon_invalid:
93ccea7a19SRussell King	zero_fp
94ccea7a19SRussell King
95ccea7a19SRussell King	ldmia	r0, {r4 - r6}
96ccea7a19SRussell King	add	r0, sp, #S_PC		@ here for interlock avoidance
97ccea7a19SRussell King	mov	r7, #-1			@  ""   ""    ""        ""
98ccea7a19SRussell King	str	r4, [sp]		@ save preserved r0
99ccea7a19SRussell King	stmia	r0, {r5 - r7}		@ lr_<exception>,
100ccea7a19SRussell King					@ cpsr_<exception>, "old_r0"
101ccea7a19SRussell King
1021da177e4SLinus Torvalds	mov	r0, sp
1031da177e4SLinus Torvalds	b	bad_mode
1041da177e4SLinus Torvalds
1051da177e4SLinus Torvalds/*
1061da177e4SLinus Torvalds * SVC mode handlers
1071da177e4SLinus Torvalds */
1082dede2d8SNicolas Pitre
1092dede2d8SNicolas Pitre#if defined(CONFIG_AEABI) && (__LINUX_ARM_ARCH__ >= 5)
1102dede2d8SNicolas Pitre#define SPFIX(code...) code
1112dede2d8SNicolas Pitre#else
1122dede2d8SNicolas Pitre#define SPFIX(code...)
1132dede2d8SNicolas Pitre#endif
1142dede2d8SNicolas Pitre
115*d30a0c8bSNicolas Pitre	.macro	svc_entry, stack_hole=0
116*d30a0c8bSNicolas Pitre	sub	sp, sp, #(S_FRAME_SIZE + \stack_hole)
1172dede2d8SNicolas Pitre SPFIX(	tst	sp, #4		)
1182dede2d8SNicolas Pitre SPFIX(	bicne	sp, sp, #4	)
119ccea7a19SRussell King	stmib	sp, {r1 - r12}
120ccea7a19SRussell King
121ccea7a19SRussell King	ldmia	r0, {r1 - r3}
122ccea7a19SRussell King	add	r5, sp, #S_SP		@ here for interlock avoidance
123ccea7a19SRussell King	mov	r4, #-1			@  ""  ""      ""       ""
124*d30a0c8bSNicolas Pitre	add	r0, sp, #(S_FRAME_SIZE + \stack_hole)
1252dede2d8SNicolas Pitre SPFIX(	addne	r0, r0, #4	)
126ccea7a19SRussell King	str	r1, [sp]		@ save the "real" r0 copied
127ccea7a19SRussell King					@ from the exception stack
128ccea7a19SRussell King
1291da177e4SLinus Torvalds	mov	r1, lr
1301da177e4SLinus Torvalds
1311da177e4SLinus Torvalds	@
1321da177e4SLinus Torvalds	@ We are now ready to fill in the remaining blanks on the stack:
1331da177e4SLinus Torvalds	@
1341da177e4SLinus Torvalds	@  r0 - sp_svc
1351da177e4SLinus Torvalds	@  r1 - lr_svc
1361da177e4SLinus Torvalds	@  r2 - lr_<exception>, already fixed up for correct return/restart
1371da177e4SLinus Torvalds	@  r3 - spsr_<exception>
1381da177e4SLinus Torvalds	@  r4 - orig_r0 (see pt_regs definition in ptrace.h)
1391da177e4SLinus Torvalds	@
1401da177e4SLinus Torvalds	stmia	r5, {r0 - r4}
1411da177e4SLinus Torvalds	.endm
1421da177e4SLinus Torvalds
1431da177e4SLinus Torvalds	.align	5
1441da177e4SLinus Torvalds__dabt_svc:
145ccea7a19SRussell King	svc_entry
1461da177e4SLinus Torvalds
1471da177e4SLinus Torvalds	@
1481da177e4SLinus Torvalds	@ get ready to re-enable interrupts if appropriate
1491da177e4SLinus Torvalds	@
1501da177e4SLinus Torvalds	mrs	r9, cpsr
1511da177e4SLinus Torvalds	tst	r3, #PSR_I_BIT
1521da177e4SLinus Torvalds	biceq	r9, r9, #PSR_I_BIT
1531da177e4SLinus Torvalds
1541da177e4SLinus Torvalds	@
1551da177e4SLinus Torvalds	@ Call the processor-specific abort handler:
1561da177e4SLinus Torvalds	@
1571da177e4SLinus Torvalds	@  r2 - aborted context pc
1581da177e4SLinus Torvalds	@  r3 - aborted context cpsr
1591da177e4SLinus Torvalds	@
1601da177e4SLinus Torvalds	@ The abort handler must return the aborted address in r0, and
1611da177e4SLinus Torvalds	@ the fault status register in r1.  r9 must be preserved.
1621da177e4SLinus Torvalds	@
1631da177e4SLinus Torvalds#ifdef MULTI_ABORT
1641da177e4SLinus Torvalds	ldr	r4, .LCprocfns
1651da177e4SLinus Torvalds	mov	lr, pc
1661da177e4SLinus Torvalds	ldr	pc, [r4]
1671da177e4SLinus Torvalds#else
1681da177e4SLinus Torvalds	bl	CPU_ABORT_HANDLER
1691da177e4SLinus Torvalds#endif
1701da177e4SLinus Torvalds
1711da177e4SLinus Torvalds	@
1721da177e4SLinus Torvalds	@ set desired IRQ state, then call main handler
1731da177e4SLinus Torvalds	@
1741da177e4SLinus Torvalds	msr	cpsr_c, r9
1751da177e4SLinus Torvalds	mov	r2, sp
1761da177e4SLinus Torvalds	bl	do_DataAbort
1771da177e4SLinus Torvalds
1781da177e4SLinus Torvalds	@
1791da177e4SLinus Torvalds	@ IRQs off again before pulling preserved data off the stack
1801da177e4SLinus Torvalds	@
1811ec42c0cSRussell King	disable_irq
1821da177e4SLinus Torvalds
1831da177e4SLinus Torvalds	@
1841da177e4SLinus Torvalds	@ restore SPSR and restart the instruction
1851da177e4SLinus Torvalds	@
1861da177e4SLinus Torvalds	ldr	r0, [sp, #S_PSR]
1871da177e4SLinus Torvalds	msr	spsr_cxsf, r0
1881da177e4SLinus Torvalds	ldmia	sp, {r0 - pc}^			@ load r0 - pc, cpsr
1891da177e4SLinus Torvalds
1901da177e4SLinus Torvalds	.align	5
1911da177e4SLinus Torvalds__irq_svc:
192ccea7a19SRussell King	svc_entry
193ccea7a19SRussell King
1947ad1bcb2SRussell King#ifdef CONFIG_TRACE_IRQFLAGS
1957ad1bcb2SRussell King	bl	trace_hardirqs_off
1967ad1bcb2SRussell King#endif
1971da177e4SLinus Torvalds#ifdef CONFIG_PREEMPT
198706fdd9fSRussell King	get_thread_info tsk
199706fdd9fSRussell King	ldr	r8, [tsk, #TI_PREEMPT]		@ get preempt count
200706fdd9fSRussell King	add	r7, r8, #1			@ increment it
201706fdd9fSRussell King	str	r7, [tsk, #TI_PREEMPT]
2021da177e4SLinus Torvalds#endif
203ccea7a19SRussell King
204187a51adSRussell King	irq_handler
2051da177e4SLinus Torvalds#ifdef CONFIG_PREEMPT
206706fdd9fSRussell King	ldr	r0, [tsk, #TI_FLAGS]		@ get flags
2071da177e4SLinus Torvalds	tst	r0, #_TIF_NEED_RESCHED
2081da177e4SLinus Torvalds	blne	svc_preempt
2091da177e4SLinus Torvaldspreempt_return:
210706fdd9fSRussell King	ldr	r0, [tsk, #TI_PREEMPT]		@ read preempt value
211706fdd9fSRussell King	str	r8, [tsk, #TI_PREEMPT]		@ restore preempt count
2121da177e4SLinus Torvalds	teq	r0, r7
2131da177e4SLinus Torvalds	strne	r0, [r0, -r0]			@ bug()
2141da177e4SLinus Torvalds#endif
2151da177e4SLinus Torvalds	ldr	r0, [sp, #S_PSR]		@ irqs are already disabled
2161da177e4SLinus Torvalds	msr	spsr_cxsf, r0
2177ad1bcb2SRussell King#ifdef CONFIG_TRACE_IRQFLAGS
2187ad1bcb2SRussell King	tst	r0, #PSR_I_BIT
2197ad1bcb2SRussell King	bleq	trace_hardirqs_on
2207ad1bcb2SRussell King#endif
2211da177e4SLinus Torvalds	ldmia	sp, {r0 - pc}^			@ load r0 - pc, cpsr
2221da177e4SLinus Torvalds
2231da177e4SLinus Torvalds	.ltorg
2241da177e4SLinus Torvalds
2251da177e4SLinus Torvalds#ifdef CONFIG_PREEMPT
2261da177e4SLinus Torvaldssvc_preempt:
227706fdd9fSRussell King	teq	r8, #0				@ was preempt count = 0
2281da177e4SLinus Torvalds	ldreq	r6, .LCirq_stat
2291da177e4SLinus Torvalds	movne	pc, lr				@ no
2301da177e4SLinus Torvalds	ldr	r0, [r6, #4]			@ local_irq_count
2311da177e4SLinus Torvalds	ldr	r1, [r6, #8]			@ local_bh_count
2321da177e4SLinus Torvalds	adds	r0, r0, r1
2331da177e4SLinus Torvalds	movne	pc, lr
2341da177e4SLinus Torvalds	mov	r7, #0				@ preempt_schedule_irq
235706fdd9fSRussell King	str	r7, [tsk, #TI_PREEMPT]		@ expects preempt_count == 0
2361da177e4SLinus Torvalds1:	bl	preempt_schedule_irq		@ irq en/disable is done inside
237706fdd9fSRussell King	ldr	r0, [tsk, #TI_FLAGS]		@ get new tasks TI_FLAGS
2381da177e4SLinus Torvalds	tst	r0, #_TIF_NEED_RESCHED
2391da177e4SLinus Torvalds	beq	preempt_return			@ go again
2401da177e4SLinus Torvalds	b	1b
2411da177e4SLinus Torvalds#endif
2421da177e4SLinus Torvalds
2431da177e4SLinus Torvalds	.align	5
2441da177e4SLinus Torvalds__und_svc:
245*d30a0c8bSNicolas Pitre#ifdef CONFIG_KPROBES
246*d30a0c8bSNicolas Pitre	@ If a kprobe is about to simulate a "stmdb sp..." instruction,
247*d30a0c8bSNicolas Pitre	@ it obviously needs free stack space which then will belong to
248*d30a0c8bSNicolas Pitre	@ the saved context.
249*d30a0c8bSNicolas Pitre	svc_entry 64
250*d30a0c8bSNicolas Pitre#else
251ccea7a19SRussell King	svc_entry
252*d30a0c8bSNicolas Pitre#endif
2531da177e4SLinus Torvalds
2541da177e4SLinus Torvalds	@
2551da177e4SLinus Torvalds	@ call emulation code, which returns using r9 if it has emulated
2561da177e4SLinus Torvalds	@ the instruction, or the more conventional lr if we are to treat
2571da177e4SLinus Torvalds	@ this as a real undefined instruction
2581da177e4SLinus Torvalds	@
2591da177e4SLinus Torvalds	@  r0 - instruction
2601da177e4SLinus Torvalds	@
2611da177e4SLinus Torvalds	ldr	r0, [r2, #-4]
2621da177e4SLinus Torvalds	adr	r9, 1f
2631da177e4SLinus Torvalds	bl	call_fpe
2641da177e4SLinus Torvalds
2651da177e4SLinus Torvalds	mov	r0, sp				@ struct pt_regs *regs
2661da177e4SLinus Torvalds	bl	do_undefinstr
2671da177e4SLinus Torvalds
2681da177e4SLinus Torvalds	@
2691da177e4SLinus Torvalds	@ IRQs off again before pulling preserved data off the stack
2701da177e4SLinus Torvalds	@
2711ec42c0cSRussell King1:	disable_irq
2721da177e4SLinus Torvalds
2731da177e4SLinus Torvalds	@
2741da177e4SLinus Torvalds	@ restore SPSR and restart the instruction
2751da177e4SLinus Torvalds	@
2761da177e4SLinus Torvalds	ldr	lr, [sp, #S_PSR]		@ Get SVC cpsr
2771da177e4SLinus Torvalds	msr	spsr_cxsf, lr
2781da177e4SLinus Torvalds	ldmia	sp, {r0 - pc}^			@ Restore SVC registers
2791da177e4SLinus Torvalds
2801da177e4SLinus Torvalds	.align	5
2811da177e4SLinus Torvalds__pabt_svc:
282ccea7a19SRussell King	svc_entry
2831da177e4SLinus Torvalds
2841da177e4SLinus Torvalds	@
2851da177e4SLinus Torvalds	@ re-enable interrupts if appropriate
2861da177e4SLinus Torvalds	@
2871da177e4SLinus Torvalds	mrs	r9, cpsr
2881da177e4SLinus Torvalds	tst	r3, #PSR_I_BIT
2891da177e4SLinus Torvalds	biceq	r9, r9, #PSR_I_BIT
2901da177e4SLinus Torvalds	msr	cpsr_c, r9
2911da177e4SLinus Torvalds
2921da177e4SLinus Torvalds	@
2931da177e4SLinus Torvalds	@ set args, then call main handler
2941da177e4SLinus Torvalds	@
2951da177e4SLinus Torvalds	@  r0 - address of faulting instruction
2961da177e4SLinus Torvalds	@  r1 - pointer to registers on stack
2971da177e4SLinus Torvalds	@
2981da177e4SLinus Torvalds	mov	r0, r2				@ address (pc)
2991da177e4SLinus Torvalds	mov	r1, sp				@ regs
3001da177e4SLinus Torvalds	bl	do_PrefetchAbort		@ call abort handler
3011da177e4SLinus Torvalds
3021da177e4SLinus Torvalds	@
3031da177e4SLinus Torvalds	@ IRQs off again before pulling preserved data off the stack
3041da177e4SLinus Torvalds	@
3051ec42c0cSRussell King	disable_irq
3061da177e4SLinus Torvalds
3071da177e4SLinus Torvalds	@
3081da177e4SLinus Torvalds	@ restore SPSR and restart the instruction
3091da177e4SLinus Torvalds	@
3101da177e4SLinus Torvalds	ldr	r0, [sp, #S_PSR]
3111da177e4SLinus Torvalds	msr	spsr_cxsf, r0
3121da177e4SLinus Torvalds	ldmia	sp, {r0 - pc}^			@ load r0 - pc, cpsr
3131da177e4SLinus Torvalds
3141da177e4SLinus Torvalds	.align	5
31549f680eaSRussell King.LCcralign:
31649f680eaSRussell King	.word	cr_alignment
3171da177e4SLinus Torvalds#ifdef MULTI_ABORT
3181da177e4SLinus Torvalds.LCprocfns:
3191da177e4SLinus Torvalds	.word	processor
3201da177e4SLinus Torvalds#endif
3211da177e4SLinus Torvalds.LCfp:
3221da177e4SLinus Torvalds	.word	fp_enter
3231da177e4SLinus Torvalds#ifdef CONFIG_PREEMPT
3241da177e4SLinus Torvalds.LCirq_stat:
3251da177e4SLinus Torvalds	.word	irq_stat
3261da177e4SLinus Torvalds#endif
3271da177e4SLinus Torvalds
3281da177e4SLinus Torvalds/*
3291da177e4SLinus Torvalds * User mode handlers
3302dede2d8SNicolas Pitre *
3312dede2d8SNicolas Pitre * EABI note: sp_svc is always 64-bit aligned here, so should S_FRAME_SIZE
3321da177e4SLinus Torvalds */
3332dede2d8SNicolas Pitre
3342dede2d8SNicolas Pitre#if defined(CONFIG_AEABI) && (__LINUX_ARM_ARCH__ >= 5) && (S_FRAME_SIZE & 7)
3352dede2d8SNicolas Pitre#error "sizeof(struct pt_regs) must be a multiple of 8"
3362dede2d8SNicolas Pitre#endif
3372dede2d8SNicolas Pitre
338ccea7a19SRussell King	.macro	usr_entry
339ccea7a19SRussell King	sub	sp, sp, #S_FRAME_SIZE
340ccea7a19SRussell King	stmib	sp, {r1 - r12}
341ccea7a19SRussell King
342ccea7a19SRussell King	ldmia	r0, {r1 - r3}
343ccea7a19SRussell King	add	r0, sp, #S_PC		@ here for interlock avoidance
344ccea7a19SRussell King	mov	r4, #-1			@  ""  ""     ""        ""
345ccea7a19SRussell King
346ccea7a19SRussell King	str	r1, [sp]		@ save the "real" r0 copied
347ccea7a19SRussell King					@ from the exception stack
3481da177e4SLinus Torvalds
3491da177e4SLinus Torvalds	@
3501da177e4SLinus Torvalds	@ We are now ready to fill in the remaining blanks on the stack:
3511da177e4SLinus Torvalds	@
3521da177e4SLinus Torvalds	@  r2 - lr_<exception>, already fixed up for correct return/restart
3531da177e4SLinus Torvalds	@  r3 - spsr_<exception>
3541da177e4SLinus Torvalds	@  r4 - orig_r0 (see pt_regs definition in ptrace.h)
3551da177e4SLinus Torvalds	@
3561da177e4SLinus Torvalds	@ Also, separately save sp_usr and lr_usr
3571da177e4SLinus Torvalds	@
358ccea7a19SRussell King	stmia	r0, {r2 - r4}
359ccea7a19SRussell King	stmdb	r0, {sp, lr}^
3601da177e4SLinus Torvalds
3611da177e4SLinus Torvalds	@
3621da177e4SLinus Torvalds	@ Enable the alignment trap while in kernel mode
3631da177e4SLinus Torvalds	@
36449f680eaSRussell King	alignment_trap r0
3651da177e4SLinus Torvalds
3661da177e4SLinus Torvalds	@
3671da177e4SLinus Torvalds	@ Clear FP to mark the first stack frame
3681da177e4SLinus Torvalds	@
3691da177e4SLinus Torvalds	zero_fp
3701da177e4SLinus Torvalds	.endm
3711da177e4SLinus Torvalds
372b49c0f24SNicolas Pitre	.macro	kuser_cmpxchg_check
373b49c0f24SNicolas Pitre#if __LINUX_ARM_ARCH__ < 6 && !defined(CONFIG_NEEDS_SYSCALL_FOR_CMPXCHG)
374b49c0f24SNicolas Pitre#ifndef CONFIG_MMU
375b49c0f24SNicolas Pitre#warning "NPTL on non MMU needs fixing"
376b49c0f24SNicolas Pitre#else
377b49c0f24SNicolas Pitre	@ Make sure our user space atomic helper is restarted
378b49c0f24SNicolas Pitre	@ if it was interrupted in a critical region.  Here we
379b49c0f24SNicolas Pitre	@ perform a quick test inline since it should be false
380b49c0f24SNicolas Pitre	@ 99.9999% of the time.  The rest is done out of line.
381b49c0f24SNicolas Pitre	cmp	r2, #TASK_SIZE
382b49c0f24SNicolas Pitre	blhs	kuser_cmpxchg_fixup
383b49c0f24SNicolas Pitre#endif
384b49c0f24SNicolas Pitre#endif
385b49c0f24SNicolas Pitre	.endm
386b49c0f24SNicolas Pitre
3871da177e4SLinus Torvalds	.align	5
3881da177e4SLinus Torvalds__dabt_usr:
389ccea7a19SRussell King	usr_entry
390b49c0f24SNicolas Pitre	kuser_cmpxchg_check
3911da177e4SLinus Torvalds
3921da177e4SLinus Torvalds	@
3931da177e4SLinus Torvalds	@ Call the processor-specific abort handler:
3941da177e4SLinus Torvalds	@
3951da177e4SLinus Torvalds	@  r2 - aborted context pc
3961da177e4SLinus Torvalds	@  r3 - aborted context cpsr
3971da177e4SLinus Torvalds	@
3981da177e4SLinus Torvalds	@ The abort handler must return the aborted address in r0, and
3991da177e4SLinus Torvalds	@ the fault status register in r1.
4001da177e4SLinus Torvalds	@
4011da177e4SLinus Torvalds#ifdef MULTI_ABORT
4021da177e4SLinus Torvalds	ldr	r4, .LCprocfns
4031da177e4SLinus Torvalds	mov	lr, pc
4041da177e4SLinus Torvalds	ldr	pc, [r4]
4051da177e4SLinus Torvalds#else
4061da177e4SLinus Torvalds	bl	CPU_ABORT_HANDLER
4071da177e4SLinus Torvalds#endif
4081da177e4SLinus Torvalds
4091da177e4SLinus Torvalds	@
4101da177e4SLinus Torvalds	@ IRQs on, then call the main handler
4111da177e4SLinus Torvalds	@
4121ec42c0cSRussell King	enable_irq
4131da177e4SLinus Torvalds	mov	r2, sp
4141da177e4SLinus Torvalds	adr	lr, ret_from_exception
4151da177e4SLinus Torvalds	b	do_DataAbort
4161da177e4SLinus Torvalds
4171da177e4SLinus Torvalds	.align	5
4181da177e4SLinus Torvalds__irq_usr:
419ccea7a19SRussell King	usr_entry
420b49c0f24SNicolas Pitre	kuser_cmpxchg_check
4211da177e4SLinus Torvalds
4227ad1bcb2SRussell King#ifdef CONFIG_TRACE_IRQFLAGS
4237ad1bcb2SRussell King	bl	trace_hardirqs_off
4247ad1bcb2SRussell King#endif
4251da177e4SLinus Torvalds	get_thread_info tsk
4261da177e4SLinus Torvalds#ifdef CONFIG_PREEMPT
427706fdd9fSRussell King	ldr	r8, [tsk, #TI_PREEMPT]		@ get preempt count
428706fdd9fSRussell King	add	r7, r8, #1			@ increment it
429706fdd9fSRussell King	str	r7, [tsk, #TI_PREEMPT]
4301da177e4SLinus Torvalds#endif
431ccea7a19SRussell King
432187a51adSRussell King	irq_handler
4331da177e4SLinus Torvalds#ifdef CONFIG_PREEMPT
434706fdd9fSRussell King	ldr	r0, [tsk, #TI_PREEMPT]
435706fdd9fSRussell King	str	r8, [tsk, #TI_PREEMPT]
4361da177e4SLinus Torvalds	teq	r0, r7
4371da177e4SLinus Torvalds	strne	r0, [r0, -r0]
4381da177e4SLinus Torvalds#endif
4397ad1bcb2SRussell King#ifdef CONFIG_TRACE_IRQFLAGS
4407ad1bcb2SRussell King	bl	trace_hardirqs_on
4417ad1bcb2SRussell King#endif
442ccea7a19SRussell King
4431da177e4SLinus Torvalds	mov	why, #0
4441da177e4SLinus Torvalds	b	ret_to_user
4451da177e4SLinus Torvalds
4461da177e4SLinus Torvalds	.ltorg
4471da177e4SLinus Torvalds
4481da177e4SLinus Torvalds	.align	5
4491da177e4SLinus Torvalds__und_usr:
450ccea7a19SRussell King	usr_entry
4511da177e4SLinus Torvalds
4521da177e4SLinus Torvalds	tst	r3, #PSR_T_BIT			@ Thumb mode?
453db6ccbb6SRussell King	bne	__und_usr_unknown		@ ignore FP
4541da177e4SLinus Torvalds	sub	r4, r2, #4
4551da177e4SLinus Torvalds
4561da177e4SLinus Torvalds	@
4571da177e4SLinus Torvalds	@ fall through to the emulation code, which returns using r9 if
4581da177e4SLinus Torvalds	@ it has emulated the instruction, or the more conventional lr
4591da177e4SLinus Torvalds	@ if we are to treat this as a real undefined instruction
4601da177e4SLinus Torvalds	@
4611da177e4SLinus Torvalds	@  r0 - instruction
4621da177e4SLinus Torvalds	@
4631da177e4SLinus Torvalds	adr	r9, ret_from_exception
464db6ccbb6SRussell King	adr	lr, __und_usr_unknown
465d28a170dSNicolas Pitre1:	ldrt	r0, [r4]
4661da177e4SLinus Torvalds	@
4671da177e4SLinus Torvalds	@ fallthrough to call_fpe
4681da177e4SLinus Torvalds	@
4691da177e4SLinus Torvalds
4701da177e4SLinus Torvalds/*
4711da177e4SLinus Torvalds * The out of line fixup for the ldrt above.
4721da177e4SLinus Torvalds */
4731da177e4SLinus Torvalds	.section .fixup, "ax"
4741da177e4SLinus Torvalds2:	mov	pc, r9
4751da177e4SLinus Torvalds	.previous
4761da177e4SLinus Torvalds	.section __ex_table,"a"
4771da177e4SLinus Torvalds	.long	1b, 2b
4781da177e4SLinus Torvalds	.previous
4791da177e4SLinus Torvalds
4801da177e4SLinus Torvalds/*
4811da177e4SLinus Torvalds * Check whether the instruction is a co-processor instruction.
4821da177e4SLinus Torvalds * If yes, we need to call the relevant co-processor handler.
4831da177e4SLinus Torvalds *
4841da177e4SLinus Torvalds * Note that we don't do a full check here for the co-processor
4851da177e4SLinus Torvalds * instructions; all instructions with bit 27 set are well
4861da177e4SLinus Torvalds * defined.  The only instructions that should fault are the
4871da177e4SLinus Torvalds * co-processor instructions.  However, we have to watch out
4881da177e4SLinus Torvalds * for the ARM6/ARM7 SWI bug.
4891da177e4SLinus Torvalds *
4901da177e4SLinus Torvalds * Emulators may wish to make use of the following registers:
4911da177e4SLinus Torvalds *  r0  = instruction opcode.
4921da177e4SLinus Torvalds *  r2  = PC+4
493db6ccbb6SRussell King *  r9  = normal "successful" return address
4941da177e4SLinus Torvalds *  r10 = this threads thread_info structure.
495db6ccbb6SRussell King *  lr  = unrecognised instruction return address
4961da177e4SLinus Torvalds */
4971da177e4SLinus Torvaldscall_fpe:
4981da177e4SLinus Torvalds	tst	r0, #0x08000000			@ only CDP/CPRT/LDC/STC have bit 27
4991da177e4SLinus Torvalds#if defined(CONFIG_CPU_ARM610) || defined(CONFIG_CPU_ARM710)
5001da177e4SLinus Torvalds	and	r8, r0, #0x0f000000		@ mask out op-code bits
5011da177e4SLinus Torvalds	teqne	r8, #0x0f000000			@ SWI (ARM6/7 bug)?
5021da177e4SLinus Torvalds#endif
5031da177e4SLinus Torvalds	moveq	pc, lr
5041da177e4SLinus Torvalds	get_thread_info r10			@ get current thread
5051da177e4SLinus Torvalds	and	r8, r0, #0x00000f00		@ mask out CP number
5061da177e4SLinus Torvalds	mov	r7, #1
5071da177e4SLinus Torvalds	add	r6, r10, #TI_USED_CP
5081da177e4SLinus Torvalds	strb	r7, [r6, r8, lsr #8]		@ set appropriate used_cp[]
5091da177e4SLinus Torvalds#ifdef CONFIG_IWMMXT
5101da177e4SLinus Torvalds	@ Test if we need to give access to iWMMXt coprocessors
5111da177e4SLinus Torvalds	ldr	r5, [r10, #TI_FLAGS]
5121da177e4SLinus Torvalds	rsbs	r7, r8, #(1 << 8)		@ CP 0 or 1 only
5131da177e4SLinus Torvalds	movcss	r7, r5, lsr #(TIF_USING_IWMMXT + 1)
5141da177e4SLinus Torvalds	bcs	iwmmxt_task_enable
5151da177e4SLinus Torvalds#endif
5161da177e4SLinus Torvalds	add	pc, pc, r8, lsr #6
5171da177e4SLinus Torvalds	mov	r0, r0
5181da177e4SLinus Torvalds
5191da177e4SLinus Torvalds	mov	pc, lr				@ CP#0
5201da177e4SLinus Torvalds	b	do_fpe				@ CP#1 (FPE)
5211da177e4SLinus Torvalds	b	do_fpe				@ CP#2 (FPE)
5221da177e4SLinus Torvalds	mov	pc, lr				@ CP#3
523c17fad11SLennert Buytenhek#ifdef CONFIG_CRUNCH
524c17fad11SLennert Buytenhek	b	crunch_task_enable		@ CP#4 (MaverickCrunch)
525c17fad11SLennert Buytenhek	b	crunch_task_enable		@ CP#5 (MaverickCrunch)
526c17fad11SLennert Buytenhek	b	crunch_task_enable		@ CP#6 (MaverickCrunch)
527c17fad11SLennert Buytenhek#else
5281da177e4SLinus Torvalds	mov	pc, lr				@ CP#4
5291da177e4SLinus Torvalds	mov	pc, lr				@ CP#5
5301da177e4SLinus Torvalds	mov	pc, lr				@ CP#6
531c17fad11SLennert Buytenhek#endif
5321da177e4SLinus Torvalds	mov	pc, lr				@ CP#7
5331da177e4SLinus Torvalds	mov	pc, lr				@ CP#8
5341da177e4SLinus Torvalds	mov	pc, lr				@ CP#9
5351da177e4SLinus Torvalds#ifdef CONFIG_VFP
5361da177e4SLinus Torvalds	b	do_vfp				@ CP#10 (VFP)
5371da177e4SLinus Torvalds	b	do_vfp				@ CP#11 (VFP)
5381da177e4SLinus Torvalds#else
5391da177e4SLinus Torvalds	mov	pc, lr				@ CP#10 (VFP)
5401da177e4SLinus Torvalds	mov	pc, lr				@ CP#11 (VFP)
5411da177e4SLinus Torvalds#endif
5421da177e4SLinus Torvalds	mov	pc, lr				@ CP#12
5431da177e4SLinus Torvalds	mov	pc, lr				@ CP#13
5441da177e4SLinus Torvalds	mov	pc, lr				@ CP#14 (Debug)
5451da177e4SLinus Torvalds	mov	pc, lr				@ CP#15 (Control)
5461da177e4SLinus Torvalds
5471da177e4SLinus Torvaldsdo_fpe:
5485d25ac03SRussell King	enable_irq
5491da177e4SLinus Torvalds	ldr	r4, .LCfp
5501da177e4SLinus Torvalds	add	r10, r10, #TI_FPSTATE		@ r10 = workspace
5511da177e4SLinus Torvalds	ldr	pc, [r4]			@ Call FP module USR entry point
5521da177e4SLinus Torvalds
5531da177e4SLinus Torvalds/*
5541da177e4SLinus Torvalds * The FP module is called with these registers set:
5551da177e4SLinus Torvalds *  r0  = instruction
5561da177e4SLinus Torvalds *  r2  = PC+4
5571da177e4SLinus Torvalds *  r9  = normal "successful" return address
5581da177e4SLinus Torvalds *  r10 = FP workspace
5591da177e4SLinus Torvalds *  lr  = unrecognised FP instruction return address
5601da177e4SLinus Torvalds */
5611da177e4SLinus Torvalds
5621da177e4SLinus Torvalds	.data
5631da177e4SLinus TorvaldsENTRY(fp_enter)
564db6ccbb6SRussell King	.word	no_fp
5651da177e4SLinus Torvalds	.text
5661da177e4SLinus Torvalds
567db6ccbb6SRussell Kingno_fp:	mov	pc, lr
568db6ccbb6SRussell King
569db6ccbb6SRussell King__und_usr_unknown:
5701da177e4SLinus Torvalds	mov	r0, sp
5711da177e4SLinus Torvalds	adr	lr, ret_from_exception
5721da177e4SLinus Torvalds	b	do_undefinstr
5731da177e4SLinus Torvalds
5741da177e4SLinus Torvalds	.align	5
5751da177e4SLinus Torvalds__pabt_usr:
576ccea7a19SRussell King	usr_entry
5771da177e4SLinus Torvalds
5781ec42c0cSRussell King	enable_irq				@ Enable interrupts
5791da177e4SLinus Torvalds	mov	r0, r2				@ address (pc)
5801da177e4SLinus Torvalds	mov	r1, sp				@ regs
5811da177e4SLinus Torvalds	bl	do_PrefetchAbort		@ call abort handler
5821da177e4SLinus Torvalds	/* fall through */
5831da177e4SLinus Torvalds/*
5841da177e4SLinus Torvalds * This is the return code to user mode for abort handlers
5851da177e4SLinus Torvalds */
5861da177e4SLinus TorvaldsENTRY(ret_from_exception)
5871da177e4SLinus Torvalds	get_thread_info tsk
5881da177e4SLinus Torvalds	mov	why, #0
5891da177e4SLinus Torvalds	b	ret_to_user
5901da177e4SLinus Torvalds
5911da177e4SLinus Torvalds/*
5921da177e4SLinus Torvalds * Register switch for ARMv3 and ARMv4 processors
5931da177e4SLinus Torvalds * r0 = previous task_struct, r1 = previous thread_info, r2 = next thread_info
5941da177e4SLinus Torvalds * previous and next are guaranteed not to be the same.
5951da177e4SLinus Torvalds */
5961da177e4SLinus TorvaldsENTRY(__switch_to)
5971da177e4SLinus Torvalds	add	ip, r1, #TI_CPU_SAVE
5981da177e4SLinus Torvalds	ldr	r3, [r2, #TI_TP_VALUE]
5991da177e4SLinus Torvalds	stmia	ip!, {r4 - sl, fp, sp, lr}	@ Store most regs on stack
600d6551e88SRussell King#ifdef CONFIG_MMU
601d6551e88SRussell King	ldr	r6, [r2, #TI_CPU_DOMAIN]
602afeb90caSHyok S. Choi#endif
603b876386eSRussell King#if __LINUX_ARM_ARCH__ >= 6
60443cc1981SRussell King#ifdef CONFIG_CPU_32v6K
605b876386eSRussell King	clrex
606b876386eSRussell King#else
60773394322SRussell King	strex	r5, r4, [ip]			@ Clear exclusive monitor
608b876386eSRussell King#endif
609b876386eSRussell King#endif
6104b0e07a5SNicolas Pitre#if defined(CONFIG_HAS_TLS_REG)
6112d2669b6SNicolas Pitre	mcr	p15, 0, r3, c13, c0, 3		@ set TLS register
6124b0e07a5SNicolas Pitre#elif !defined(CONFIG_TLS_REG_EMUL)
6131da177e4SLinus Torvalds	mov	r4, #0xffff0fff
6142d2669b6SNicolas Pitre	str	r3, [r4, #-15]			@ TLS val at 0xffff0ff0
6152d2669b6SNicolas Pitre#endif
616afeb90caSHyok S. Choi#ifdef CONFIG_MMU
6171da177e4SLinus Torvalds	mcr	p15, 0, r6, c3, c0, 0		@ Set domain register
618afeb90caSHyok S. Choi#endif
619d6551e88SRussell King	mov	r5, r0
620d6551e88SRussell King	add	r4, r2, #TI_CPU_SAVE
621d6551e88SRussell King	ldr	r0, =thread_notify_head
622d6551e88SRussell King	mov	r1, #THREAD_NOTIFY_SWITCH
623d6551e88SRussell King	bl	atomic_notifier_call_chain
624d6551e88SRussell King	mov	r0, r5
625d6551e88SRussell King	ldmia	r4, {r4 - sl, fp, sp, pc}	@ Load all regs saved previously
6261da177e4SLinus Torvalds
6271da177e4SLinus Torvalds	__INIT
6282d2669b6SNicolas Pitre
6292d2669b6SNicolas Pitre/*
6302d2669b6SNicolas Pitre * User helpers.
6312d2669b6SNicolas Pitre *
6322d2669b6SNicolas Pitre * These are segment of kernel provided user code reachable from user space
6332d2669b6SNicolas Pitre * at a fixed address in kernel memory.  This is used to provide user space
6342d2669b6SNicolas Pitre * with some operations which require kernel help because of unimplemented
6352d2669b6SNicolas Pitre * native feature and/or instructions in many ARM CPUs. The idea is for
6362d2669b6SNicolas Pitre * this code to be executed directly in user mode for best efficiency but
6372d2669b6SNicolas Pitre * which is too intimate with the kernel counter part to be left to user
6382d2669b6SNicolas Pitre * libraries.  In fact this code might even differ from one CPU to another
6392d2669b6SNicolas Pitre * depending on the available  instruction set and restrictions like on
6402d2669b6SNicolas Pitre * SMP systems.  In other words, the kernel reserves the right to change
6412d2669b6SNicolas Pitre * this code as needed without warning. Only the entry points and their
6422d2669b6SNicolas Pitre * results are guaranteed to be stable.
6432d2669b6SNicolas Pitre *
6442d2669b6SNicolas Pitre * Each segment is 32-byte aligned and will be moved to the top of the high
6452d2669b6SNicolas Pitre * vector page.  New segments (if ever needed) must be added in front of
6462d2669b6SNicolas Pitre * existing ones.  This mechanism should be used only for things that are
6472d2669b6SNicolas Pitre * really small and justified, and not be abused freely.
6482d2669b6SNicolas Pitre *
6492d2669b6SNicolas Pitre * User space is expected to implement those things inline when optimizing
6502d2669b6SNicolas Pitre * for a processor that has the necessary native support, but only if such
6512d2669b6SNicolas Pitre * resulting binaries are already to be incompatible with earlier ARM
6522d2669b6SNicolas Pitre * processors due to the use of unsupported instructions other than what
6532d2669b6SNicolas Pitre * is provided here.  In other words don't make binaries unable to run on
6542d2669b6SNicolas Pitre * earlier processors just for the sake of not using these kernel helpers
6552d2669b6SNicolas Pitre * if your compiled code is not going to use the new instructions for other
6562d2669b6SNicolas Pitre * purpose.
6572d2669b6SNicolas Pitre */
6582d2669b6SNicolas Pitre
659ba9b5d76SNicolas Pitre	.macro	usr_ret, reg
660ba9b5d76SNicolas Pitre#ifdef CONFIG_ARM_THUMB
661ba9b5d76SNicolas Pitre	bx	\reg
662ba9b5d76SNicolas Pitre#else
663ba9b5d76SNicolas Pitre	mov	pc, \reg
664ba9b5d76SNicolas Pitre#endif
665ba9b5d76SNicolas Pitre	.endm
666ba9b5d76SNicolas Pitre
6672d2669b6SNicolas Pitre	.align	5
6682d2669b6SNicolas Pitre	.globl	__kuser_helper_start
6692d2669b6SNicolas Pitre__kuser_helper_start:
6702d2669b6SNicolas Pitre
6712d2669b6SNicolas Pitre/*
6722d2669b6SNicolas Pitre * Reference prototype:
6732d2669b6SNicolas Pitre *
6747c612bfdSNicolas Pitre *	void __kernel_memory_barrier(void)
6757c612bfdSNicolas Pitre *
6767c612bfdSNicolas Pitre * Input:
6777c612bfdSNicolas Pitre *
6787c612bfdSNicolas Pitre *	lr = return address
6797c612bfdSNicolas Pitre *
6807c612bfdSNicolas Pitre * Output:
6817c612bfdSNicolas Pitre *
6827c612bfdSNicolas Pitre *	none
6837c612bfdSNicolas Pitre *
6847c612bfdSNicolas Pitre * Clobbered:
6857c612bfdSNicolas Pitre *
686b49c0f24SNicolas Pitre *	none
6877c612bfdSNicolas Pitre *
6887c612bfdSNicolas Pitre * Definition and user space usage example:
6897c612bfdSNicolas Pitre *
6907c612bfdSNicolas Pitre *	typedef void (__kernel_dmb_t)(void);
6917c612bfdSNicolas Pitre *	#define __kernel_dmb (*(__kernel_dmb_t *)0xffff0fa0)
6927c612bfdSNicolas Pitre *
6937c612bfdSNicolas Pitre * Apply any needed memory barrier to preserve consistency with data modified
6947c612bfdSNicolas Pitre * manually and __kuser_cmpxchg usage.
6957c612bfdSNicolas Pitre *
6967c612bfdSNicolas Pitre * This could be used as follows:
6977c612bfdSNicolas Pitre *
6987c612bfdSNicolas Pitre * #define __kernel_dmb() \
6997c612bfdSNicolas Pitre *         asm volatile ( "mov r0, #0xffff0fff; mov lr, pc; sub pc, r0, #95" \
7006896eec0SPaul Brook *	        : : : "r0", "lr","cc" )
7017c612bfdSNicolas Pitre */
7027c612bfdSNicolas Pitre
7037c612bfdSNicolas Pitre__kuser_memory_barrier:				@ 0xffff0fa0
7047c612bfdSNicolas Pitre
7057c612bfdSNicolas Pitre#if __LINUX_ARM_ARCH__ >= 6 && defined(CONFIG_SMP)
7067c612bfdSNicolas Pitre	mcr	p15, 0, r0, c7, c10, 5	@ dmb
7077c612bfdSNicolas Pitre#endif
708ba9b5d76SNicolas Pitre	usr_ret	lr
7097c612bfdSNicolas Pitre
7107c612bfdSNicolas Pitre	.align	5
7117c612bfdSNicolas Pitre
7127c612bfdSNicolas Pitre/*
7137c612bfdSNicolas Pitre * Reference prototype:
7147c612bfdSNicolas Pitre *
7152d2669b6SNicolas Pitre *	int __kernel_cmpxchg(int oldval, int newval, int *ptr)
7162d2669b6SNicolas Pitre *
7172d2669b6SNicolas Pitre * Input:
7182d2669b6SNicolas Pitre *
7192d2669b6SNicolas Pitre *	r0 = oldval
7202d2669b6SNicolas Pitre *	r1 = newval
7212d2669b6SNicolas Pitre *	r2 = ptr
7222d2669b6SNicolas Pitre *	lr = return address
7232d2669b6SNicolas Pitre *
7242d2669b6SNicolas Pitre * Output:
7252d2669b6SNicolas Pitre *
7262d2669b6SNicolas Pitre *	r0 = returned value (zero or non-zero)
7272d2669b6SNicolas Pitre *	C flag = set if r0 == 0, clear if r0 != 0
7282d2669b6SNicolas Pitre *
7292d2669b6SNicolas Pitre * Clobbered:
7302d2669b6SNicolas Pitre *
7312d2669b6SNicolas Pitre *	r3, ip, flags
7322d2669b6SNicolas Pitre *
7332d2669b6SNicolas Pitre * Definition and user space usage example:
7342d2669b6SNicolas Pitre *
7352d2669b6SNicolas Pitre *	typedef int (__kernel_cmpxchg_t)(int oldval, int newval, int *ptr);
7362d2669b6SNicolas Pitre *	#define __kernel_cmpxchg (*(__kernel_cmpxchg_t *)0xffff0fc0)
7372d2669b6SNicolas Pitre *
7382d2669b6SNicolas Pitre * Atomically store newval in *ptr if *ptr is equal to oldval for user space.
7392d2669b6SNicolas Pitre * Return zero if *ptr was changed or non-zero if no exchange happened.
7402d2669b6SNicolas Pitre * The C flag is also set if *ptr was changed to allow for assembly
7412d2669b6SNicolas Pitre * optimization in the calling code.
7422d2669b6SNicolas Pitre *
7435964eae8SNicolas Pitre * Notes:
7445964eae8SNicolas Pitre *
7455964eae8SNicolas Pitre *    - This routine already includes memory barriers as needed.
7465964eae8SNicolas Pitre *
7472d2669b6SNicolas Pitre * For example, a user space atomic_add implementation could look like this:
7482d2669b6SNicolas Pitre *
7492d2669b6SNicolas Pitre * #define atomic_add(ptr, val) \
7502d2669b6SNicolas Pitre *	({ register unsigned int *__ptr asm("r2") = (ptr); \
7512d2669b6SNicolas Pitre *	   register unsigned int __result asm("r1"); \
7522d2669b6SNicolas Pitre *	   asm volatile ( \
7532d2669b6SNicolas Pitre *	       "1: @ atomic_add\n\t" \
7542d2669b6SNicolas Pitre *	       "ldr	r0, [r2]\n\t" \
7552d2669b6SNicolas Pitre *	       "mov	r3, #0xffff0fff\n\t" \
7562d2669b6SNicolas Pitre *	       "add	lr, pc, #4\n\t" \
7572d2669b6SNicolas Pitre *	       "add	r1, r0, %2\n\t" \
7582d2669b6SNicolas Pitre *	       "add	pc, r3, #(0xffff0fc0 - 0xffff0fff)\n\t" \
7592d2669b6SNicolas Pitre *	       "bcc	1b" \
7602d2669b6SNicolas Pitre *	       : "=&r" (__result) \
7612d2669b6SNicolas Pitre *	       : "r" (__ptr), "rIL" (val) \
7622d2669b6SNicolas Pitre *	       : "r0","r3","ip","lr","cc","memory" ); \
7632d2669b6SNicolas Pitre *	   __result; })
7642d2669b6SNicolas Pitre */
7652d2669b6SNicolas Pitre
7662d2669b6SNicolas Pitre__kuser_cmpxchg:				@ 0xffff0fc0
7672d2669b6SNicolas Pitre
768dcef1f63SNicolas Pitre#if defined(CONFIG_NEEDS_SYSCALL_FOR_CMPXCHG)
7692d2669b6SNicolas Pitre
770dcef1f63SNicolas Pitre	/*
771dcef1f63SNicolas Pitre	 * Poor you.  No fast solution possible...
772dcef1f63SNicolas Pitre	 * The kernel itself must perform the operation.
773dcef1f63SNicolas Pitre	 * A special ghost syscall is used for that (see traps.c).
774dcef1f63SNicolas Pitre	 */
7755e097445SNicolas Pitre	stmfd	sp!, {r7, lr}
7765e097445SNicolas Pitre	mov	r7, #0xff00		@ 0xfff0 into r7 for EABI
7775e097445SNicolas Pitre	orr	r7, r7, #0xf0
778dcef1f63SNicolas Pitre	swi	#0x9ffff0
7795e097445SNicolas Pitre	ldmfd	sp!, {r7, pc}
780dcef1f63SNicolas Pitre
781dcef1f63SNicolas Pitre#elif __LINUX_ARM_ARCH__ < 6
7822d2669b6SNicolas Pitre
78349bca4c2SNicolas Pitre#ifdef CONFIG_MMU
784b49c0f24SNicolas Pitre
785b49c0f24SNicolas Pitre	/*
786b49c0f24SNicolas Pitre	 * The only thing that can break atomicity in this cmpxchg
787b49c0f24SNicolas Pitre	 * implementation is either an IRQ or a data abort exception
788b49c0f24SNicolas Pitre	 * causing another process/thread to be scheduled in the middle
789b49c0f24SNicolas Pitre	 * of the critical sequence.  To prevent this, code is added to
790b49c0f24SNicolas Pitre	 * the IRQ and data abort exception handlers to set the pc back
791b49c0f24SNicolas Pitre	 * to the beginning of the critical section if it is found to be
792b49c0f24SNicolas Pitre	 * within that critical section (see kuser_cmpxchg_fixup).
793b49c0f24SNicolas Pitre	 */
794b49c0f24SNicolas Pitre1:	ldr	r3, [r2]			@ load current val
795b49c0f24SNicolas Pitre	subs	r3, r3, r0			@ compare with oldval
796b49c0f24SNicolas Pitre2:	streq	r1, [r2]			@ store newval if eq
797b49c0f24SNicolas Pitre	rsbs	r0, r3, #0			@ set return val and C flag
798b49c0f24SNicolas Pitre	usr_ret	lr
799b49c0f24SNicolas Pitre
800b49c0f24SNicolas Pitre	.text
801b49c0f24SNicolas Pitrekuser_cmpxchg_fixup:
802b49c0f24SNicolas Pitre	@ Called from kuser_cmpxchg_check macro.
803b49c0f24SNicolas Pitre	@ r2 = address of interrupted insn (must be preserved).
804b49c0f24SNicolas Pitre	@ sp = saved regs. r7 and r8 are clobbered.
805b49c0f24SNicolas Pitre	@ 1b = first critical insn, 2b = last critical insn.
806b49c0f24SNicolas Pitre	@ If r2 >= 1b and r2 <= 2b then saved pc_usr is set to 1b.
807b49c0f24SNicolas Pitre	mov	r7, #0xffff0fff
808b49c0f24SNicolas Pitre	sub	r7, r7, #(0xffff0fff - (0xffff0fc0 + (1b - __kuser_cmpxchg)))
809b49c0f24SNicolas Pitre	subs	r8, r2, r7
810b49c0f24SNicolas Pitre	rsbcss	r8, r8, #(2b - 1b)
811b49c0f24SNicolas Pitre	strcs	r7, [sp, #S_PC]
812b49c0f24SNicolas Pitre	mov	pc, lr
813b49c0f24SNicolas Pitre	.previous
814b49c0f24SNicolas Pitre
81549bca4c2SNicolas Pitre#else
81649bca4c2SNicolas Pitre#warning "NPTL on non MMU needs fixing"
81749bca4c2SNicolas Pitre	mov	r0, #-1
81849bca4c2SNicolas Pitre	adds	r0, r0, #0
819ba9b5d76SNicolas Pitre	usr_ret	lr
820b49c0f24SNicolas Pitre#endif
8212d2669b6SNicolas Pitre
8222d2669b6SNicolas Pitre#else
8232d2669b6SNicolas Pitre
8247c612bfdSNicolas Pitre#ifdef CONFIG_SMP
8257c612bfdSNicolas Pitre	mcr	p15, 0, r0, c7, c10, 5	@ dmb
8267c612bfdSNicolas Pitre#endif
827b49c0f24SNicolas Pitre1:	ldrex	r3, [r2]
8282d2669b6SNicolas Pitre	subs	r3, r3, r0
8292d2669b6SNicolas Pitre	strexeq	r3, r1, [r2]
830b49c0f24SNicolas Pitre	teqeq	r3, #1
831b49c0f24SNicolas Pitre	beq	1b
8322d2669b6SNicolas Pitre	rsbs	r0, r3, #0
833b49c0f24SNicolas Pitre	/* beware -- each __kuser slot must be 8 instructions max */
8347c612bfdSNicolas Pitre#ifdef CONFIG_SMP
835b49c0f24SNicolas Pitre	b	__kuser_memory_barrier
836b49c0f24SNicolas Pitre#else
837ba9b5d76SNicolas Pitre	usr_ret	lr
838b49c0f24SNicolas Pitre#endif
8392d2669b6SNicolas Pitre
8402d2669b6SNicolas Pitre#endif
8412d2669b6SNicolas Pitre
8422d2669b6SNicolas Pitre	.align	5
8432d2669b6SNicolas Pitre
8442d2669b6SNicolas Pitre/*
8452d2669b6SNicolas Pitre * Reference prototype:
8462d2669b6SNicolas Pitre *
8472d2669b6SNicolas Pitre *	int __kernel_get_tls(void)
8482d2669b6SNicolas Pitre *
8492d2669b6SNicolas Pitre * Input:
8502d2669b6SNicolas Pitre *
8512d2669b6SNicolas Pitre *	lr = return address
8522d2669b6SNicolas Pitre *
8532d2669b6SNicolas Pitre * Output:
8542d2669b6SNicolas Pitre *
8552d2669b6SNicolas Pitre *	r0 = TLS value
8562d2669b6SNicolas Pitre *
8572d2669b6SNicolas Pitre * Clobbered:
8582d2669b6SNicolas Pitre *
859b49c0f24SNicolas Pitre *	none
8602d2669b6SNicolas Pitre *
8612d2669b6SNicolas Pitre * Definition and user space usage example:
8622d2669b6SNicolas Pitre *
8632d2669b6SNicolas Pitre *	typedef int (__kernel_get_tls_t)(void);
8642d2669b6SNicolas Pitre *	#define __kernel_get_tls (*(__kernel_get_tls_t *)0xffff0fe0)
8652d2669b6SNicolas Pitre *
8662d2669b6SNicolas Pitre * Get the TLS value as previously set via the __ARM_NR_set_tls syscall.
8672d2669b6SNicolas Pitre *
8682d2669b6SNicolas Pitre * This could be used as follows:
8692d2669b6SNicolas Pitre *
8702d2669b6SNicolas Pitre * #define __kernel_get_tls() \
8712d2669b6SNicolas Pitre *	({ register unsigned int __val asm("r0"); \
8722d2669b6SNicolas Pitre *         asm( "mov r0, #0xffff0fff; mov lr, pc; sub pc, r0, #31" \
8732d2669b6SNicolas Pitre *	        : "=r" (__val) : : "lr","cc" ); \
8742d2669b6SNicolas Pitre *	   __val; })
8752d2669b6SNicolas Pitre */
8762d2669b6SNicolas Pitre
8772d2669b6SNicolas Pitre__kuser_get_tls:				@ 0xffff0fe0
8782d2669b6SNicolas Pitre
8794b0e07a5SNicolas Pitre#if !defined(CONFIG_HAS_TLS_REG) && !defined(CONFIG_TLS_REG_EMUL)
8802d2669b6SNicolas Pitre	ldr	r0, [pc, #(16 - 8)]		@ TLS stored at 0xffff0ff0
8812d2669b6SNicolas Pitre#else
8822d2669b6SNicolas Pitre	mrc	p15, 0, r0, c13, c0, 3		@ read TLS register
8832d2669b6SNicolas Pitre#endif
884ba9b5d76SNicolas Pitre	usr_ret	lr
8852d2669b6SNicolas Pitre
8862d2669b6SNicolas Pitre	.rep	5
8872d2669b6SNicolas Pitre	.word	0			@ pad up to __kuser_helper_version
8882d2669b6SNicolas Pitre	.endr
8892d2669b6SNicolas Pitre
8902d2669b6SNicolas Pitre/*
8912d2669b6SNicolas Pitre * Reference declaration:
8922d2669b6SNicolas Pitre *
8932d2669b6SNicolas Pitre *	extern unsigned int __kernel_helper_version;
8942d2669b6SNicolas Pitre *
8952d2669b6SNicolas Pitre * Definition and user space usage example:
8962d2669b6SNicolas Pitre *
8972d2669b6SNicolas Pitre *	#define __kernel_helper_version (*(unsigned int *)0xffff0ffc)
8982d2669b6SNicolas Pitre *
8992d2669b6SNicolas Pitre * User space may read this to determine the curent number of helpers
9002d2669b6SNicolas Pitre * available.
9012d2669b6SNicolas Pitre */
9022d2669b6SNicolas Pitre
9032d2669b6SNicolas Pitre__kuser_helper_version:				@ 0xffff0ffc
9042d2669b6SNicolas Pitre	.word	((__kuser_helper_end - __kuser_helper_start) >> 5)
9052d2669b6SNicolas Pitre
9062d2669b6SNicolas Pitre	.globl	__kuser_helper_end
9072d2669b6SNicolas Pitre__kuser_helper_end:
9082d2669b6SNicolas Pitre
9092d2669b6SNicolas Pitre
9101da177e4SLinus Torvalds/*
9111da177e4SLinus Torvalds * Vector stubs.
9121da177e4SLinus Torvalds *
9137933523dSRussell King * This code is copied to 0xffff0200 so we can use branches in the
9147933523dSRussell King * vectors, rather than ldr's.  Note that this code must not
9157933523dSRussell King * exceed 0x300 bytes.
9161da177e4SLinus Torvalds *
9171da177e4SLinus Torvalds * Common stub entry macro:
9181da177e4SLinus Torvalds *   Enter in IRQ mode, spsr = SVC/USR CPSR, lr = SVC/USR PC
919ccea7a19SRussell King *
920ccea7a19SRussell King * SP points to a minimal amount of processor-private memory, the address
921ccea7a19SRussell King * of which is copied into r0 for the mode specific abort handler.
9221da177e4SLinus Torvalds */
923b7ec4795SNicolas Pitre	.macro	vector_stub, name, mode, correction=0
9241da177e4SLinus Torvalds	.align	5
9251da177e4SLinus Torvalds
9261da177e4SLinus Torvaldsvector_\name:
9271da177e4SLinus Torvalds	.if \correction
9281da177e4SLinus Torvalds	sub	lr, lr, #\correction
9291da177e4SLinus Torvalds	.endif
9301da177e4SLinus Torvalds
931ccea7a19SRussell King	@
932ccea7a19SRussell King	@ Save r0, lr_<exception> (parent PC) and spsr_<exception>
933ccea7a19SRussell King	@ (parent CPSR)
934ccea7a19SRussell King	@
935ccea7a19SRussell King	stmia	sp, {r0, lr}		@ save r0, lr
936ccea7a19SRussell King	mrs	lr, spsr
937ccea7a19SRussell King	str	lr, [sp, #8]		@ save spsr
938ccea7a19SRussell King
939ccea7a19SRussell King	@
940ccea7a19SRussell King	@ Prepare for SVC32 mode.  IRQs remain disabled.
941ccea7a19SRussell King	@
942ccea7a19SRussell King	mrs	r0, cpsr
943b7ec4795SNicolas Pitre	eor	r0, r0, #(\mode ^ SVC_MODE)
944ccea7a19SRussell King	msr	spsr_cxsf, r0
945ccea7a19SRussell King
946ccea7a19SRussell King	@
947ccea7a19SRussell King	@ the branch table must immediately follow this code
948ccea7a19SRussell King	@
949ccea7a19SRussell King	and	lr, lr, #0x0f
950b7ec4795SNicolas Pitre	mov	r0, sp
9511da177e4SLinus Torvalds	ldr	lr, [pc, lr, lsl #2]
952ccea7a19SRussell King	movs	pc, lr			@ branch to handler in SVC mode
9531da177e4SLinus Torvalds	.endm
9541da177e4SLinus Torvalds
9557933523dSRussell King	.globl	__stubs_start
9561da177e4SLinus Torvalds__stubs_start:
9571da177e4SLinus Torvalds/*
9581da177e4SLinus Torvalds * Interrupt dispatcher
9591da177e4SLinus Torvalds */
960b7ec4795SNicolas Pitre	vector_stub	irq, IRQ_MODE, 4
9611da177e4SLinus Torvalds
9621da177e4SLinus Torvalds	.long	__irq_usr			@  0  (USR_26 / USR_32)
9631da177e4SLinus Torvalds	.long	__irq_invalid			@  1  (FIQ_26 / FIQ_32)
9641da177e4SLinus Torvalds	.long	__irq_invalid			@  2  (IRQ_26 / IRQ_32)
9651da177e4SLinus Torvalds	.long	__irq_svc			@  3  (SVC_26 / SVC_32)
9661da177e4SLinus Torvalds	.long	__irq_invalid			@  4
9671da177e4SLinus Torvalds	.long	__irq_invalid			@  5
9681da177e4SLinus Torvalds	.long	__irq_invalid			@  6
9691da177e4SLinus Torvalds	.long	__irq_invalid			@  7
9701da177e4SLinus Torvalds	.long	__irq_invalid			@  8
9711da177e4SLinus Torvalds	.long	__irq_invalid			@  9
9721da177e4SLinus Torvalds	.long	__irq_invalid			@  a
9731da177e4SLinus Torvalds	.long	__irq_invalid			@  b
9741da177e4SLinus Torvalds	.long	__irq_invalid			@  c
9751da177e4SLinus Torvalds	.long	__irq_invalid			@  d
9761da177e4SLinus Torvalds	.long	__irq_invalid			@  e
9771da177e4SLinus Torvalds	.long	__irq_invalid			@  f
9781da177e4SLinus Torvalds
9791da177e4SLinus Torvalds/*
9801da177e4SLinus Torvalds * Data abort dispatcher
9811da177e4SLinus Torvalds * Enter in ABT mode, spsr = USR CPSR, lr = USR PC
9821da177e4SLinus Torvalds */
983b7ec4795SNicolas Pitre	vector_stub	dabt, ABT_MODE, 8
9841da177e4SLinus Torvalds
9851da177e4SLinus Torvalds	.long	__dabt_usr			@  0  (USR_26 / USR_32)
9861da177e4SLinus Torvalds	.long	__dabt_invalid			@  1  (FIQ_26 / FIQ_32)
9871da177e4SLinus Torvalds	.long	__dabt_invalid			@  2  (IRQ_26 / IRQ_32)
9881da177e4SLinus Torvalds	.long	__dabt_svc			@  3  (SVC_26 / SVC_32)
9891da177e4SLinus Torvalds	.long	__dabt_invalid			@  4
9901da177e4SLinus Torvalds	.long	__dabt_invalid			@  5
9911da177e4SLinus Torvalds	.long	__dabt_invalid			@  6
9921da177e4SLinus Torvalds	.long	__dabt_invalid			@  7
9931da177e4SLinus Torvalds	.long	__dabt_invalid			@  8
9941da177e4SLinus Torvalds	.long	__dabt_invalid			@  9
9951da177e4SLinus Torvalds	.long	__dabt_invalid			@  a
9961da177e4SLinus Torvalds	.long	__dabt_invalid			@  b
9971da177e4SLinus Torvalds	.long	__dabt_invalid			@  c
9981da177e4SLinus Torvalds	.long	__dabt_invalid			@  d
9991da177e4SLinus Torvalds	.long	__dabt_invalid			@  e
10001da177e4SLinus Torvalds	.long	__dabt_invalid			@  f
10011da177e4SLinus Torvalds
10021da177e4SLinus Torvalds/*
10031da177e4SLinus Torvalds * Prefetch abort dispatcher
10041da177e4SLinus Torvalds * Enter in ABT mode, spsr = USR CPSR, lr = USR PC
10051da177e4SLinus Torvalds */
1006b7ec4795SNicolas Pitre	vector_stub	pabt, ABT_MODE, 4
10071da177e4SLinus Torvalds
10081da177e4SLinus Torvalds	.long	__pabt_usr			@  0 (USR_26 / USR_32)
10091da177e4SLinus Torvalds	.long	__pabt_invalid			@  1 (FIQ_26 / FIQ_32)
10101da177e4SLinus Torvalds	.long	__pabt_invalid			@  2 (IRQ_26 / IRQ_32)
10111da177e4SLinus Torvalds	.long	__pabt_svc			@  3 (SVC_26 / SVC_32)
10121da177e4SLinus Torvalds	.long	__pabt_invalid			@  4
10131da177e4SLinus Torvalds	.long	__pabt_invalid			@  5
10141da177e4SLinus Torvalds	.long	__pabt_invalid			@  6
10151da177e4SLinus Torvalds	.long	__pabt_invalid			@  7
10161da177e4SLinus Torvalds	.long	__pabt_invalid			@  8
10171da177e4SLinus Torvalds	.long	__pabt_invalid			@  9
10181da177e4SLinus Torvalds	.long	__pabt_invalid			@  a
10191da177e4SLinus Torvalds	.long	__pabt_invalid			@  b
10201da177e4SLinus Torvalds	.long	__pabt_invalid			@  c
10211da177e4SLinus Torvalds	.long	__pabt_invalid			@  d
10221da177e4SLinus Torvalds	.long	__pabt_invalid			@  e
10231da177e4SLinus Torvalds	.long	__pabt_invalid			@  f
10241da177e4SLinus Torvalds
10251da177e4SLinus Torvalds/*
10261da177e4SLinus Torvalds * Undef instr entry dispatcher
10271da177e4SLinus Torvalds * Enter in UND mode, spsr = SVC/USR CPSR, lr = SVC/USR PC
10281da177e4SLinus Torvalds */
1029b7ec4795SNicolas Pitre	vector_stub	und, UND_MODE
10301da177e4SLinus Torvalds
10311da177e4SLinus Torvalds	.long	__und_usr			@  0 (USR_26 / USR_32)
10321da177e4SLinus Torvalds	.long	__und_invalid			@  1 (FIQ_26 / FIQ_32)
10331da177e4SLinus Torvalds	.long	__und_invalid			@  2 (IRQ_26 / IRQ_32)
10341da177e4SLinus Torvalds	.long	__und_svc			@  3 (SVC_26 / SVC_32)
10351da177e4SLinus Torvalds	.long	__und_invalid			@  4
10361da177e4SLinus Torvalds	.long	__und_invalid			@  5
10371da177e4SLinus Torvalds	.long	__und_invalid			@  6
10381da177e4SLinus Torvalds	.long	__und_invalid			@  7
10391da177e4SLinus Torvalds	.long	__und_invalid			@  8
10401da177e4SLinus Torvalds	.long	__und_invalid			@  9
10411da177e4SLinus Torvalds	.long	__und_invalid			@  a
10421da177e4SLinus Torvalds	.long	__und_invalid			@  b
10431da177e4SLinus Torvalds	.long	__und_invalid			@  c
10441da177e4SLinus Torvalds	.long	__und_invalid			@  d
10451da177e4SLinus Torvalds	.long	__und_invalid			@  e
10461da177e4SLinus Torvalds	.long	__und_invalid			@  f
10471da177e4SLinus Torvalds
10481da177e4SLinus Torvalds	.align	5
10491da177e4SLinus Torvalds
10501da177e4SLinus Torvalds/*=============================================================================
10511da177e4SLinus Torvalds * Undefined FIQs
10521da177e4SLinus Torvalds *-----------------------------------------------------------------------------
10531da177e4SLinus Torvalds * Enter in FIQ mode, spsr = ANY CPSR, lr = ANY PC
10541da177e4SLinus Torvalds * MUST PRESERVE SVC SPSR, but need to switch to SVC mode to show our msg.
10551da177e4SLinus Torvalds * Basically to switch modes, we *HAVE* to clobber one register...  brain
10561da177e4SLinus Torvalds * damage alert!  I don't think that we can execute any code in here in any
10571da177e4SLinus Torvalds * other mode than FIQ...  Ok you can switch to another mode, but you can't
10581da177e4SLinus Torvalds * get out of that mode without clobbering one register.
10591da177e4SLinus Torvalds */
10601da177e4SLinus Torvaldsvector_fiq:
10611da177e4SLinus Torvalds	disable_fiq
10621da177e4SLinus Torvalds	subs	pc, lr, #4
10631da177e4SLinus Torvalds
10641da177e4SLinus Torvalds/*=============================================================================
10651da177e4SLinus Torvalds * Address exception handler
10661da177e4SLinus Torvalds *-----------------------------------------------------------------------------
10671da177e4SLinus Torvalds * These aren't too critical.
10681da177e4SLinus Torvalds * (they're not supposed to happen, and won't happen in 32-bit data mode).
10691da177e4SLinus Torvalds */
10701da177e4SLinus Torvalds
10711da177e4SLinus Torvaldsvector_addrexcptn:
10721da177e4SLinus Torvalds	b	vector_addrexcptn
10731da177e4SLinus Torvalds
10741da177e4SLinus Torvalds/*
10751da177e4SLinus Torvalds * We group all the following data together to optimise
10761da177e4SLinus Torvalds * for CPUs with separate I & D caches.
10771da177e4SLinus Torvalds */
10781da177e4SLinus Torvalds	.align	5
10791da177e4SLinus Torvalds
10801da177e4SLinus Torvalds.LCvswi:
10811da177e4SLinus Torvalds	.word	vector_swi
10821da177e4SLinus Torvalds
10837933523dSRussell King	.globl	__stubs_end
10841da177e4SLinus Torvalds__stubs_end:
10851da177e4SLinus Torvalds
10867933523dSRussell King	.equ	stubs_offset, __vectors_start + 0x200 - __stubs_start
10871da177e4SLinus Torvalds
10887933523dSRussell King	.globl	__vectors_start
10897933523dSRussell King__vectors_start:
10901da177e4SLinus Torvalds	swi	SYS_ERROR0
10917933523dSRussell King	b	vector_und + stubs_offset
10927933523dSRussell King	ldr	pc, .LCvswi + stubs_offset
10937933523dSRussell King	b	vector_pabt + stubs_offset
10947933523dSRussell King	b	vector_dabt + stubs_offset
10957933523dSRussell King	b	vector_addrexcptn + stubs_offset
10967933523dSRussell King	b	vector_irq + stubs_offset
10977933523dSRussell King	b	vector_fiq + stubs_offset
10981da177e4SLinus Torvalds
10997933523dSRussell King	.globl	__vectors_end
11007933523dSRussell King__vectors_end:
11011da177e4SLinus Torvalds
11021da177e4SLinus Torvalds	.data
11031da177e4SLinus Torvalds
11041da177e4SLinus Torvalds	.globl	cr_alignment
11051da177e4SLinus Torvalds	.globl	cr_no_alignment
11061da177e4SLinus Torvaldscr_alignment:
11071da177e4SLinus Torvalds	.space	4
11081da177e4SLinus Torvaldscr_no_alignment:
11091da177e4SLinus Torvalds	.space	4
1110