xref: /openbmc/linux/arch/arm/kernel/entry-armv.S (revision 831a469bc1674b7898984ce96f2902534445c0cf)
1d2912cb1SThomas Gleixner/* SPDX-License-Identifier: GPL-2.0-only */
21da177e4SLinus Torvalds/*
31da177e4SLinus Torvalds *  linux/arch/arm/kernel/entry-armv.S
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds *  Copyright (C) 1996,1997,1998 Russell King.
61da177e4SLinus Torvalds *  ARM700 fix by Matthew Godbolt (linux-user@willothewisp.demon.co.uk)
7afeb90caSHyok S. Choi *  nommu support by Hyok S. Choi (hyok.choi@samsung.com)
81da177e4SLinus Torvalds *
91da177e4SLinus Torvalds *  Low-level vector interface routines
101da177e4SLinus Torvalds *
1170b6f2b4SNicolas Pitre *  Note:  there is a StrongARM bug in the STMIA rn, {regs}^ instruction
1270b6f2b4SNicolas Pitre *  that causes it to save wrong values...  Be aware!
131da177e4SLinus Torvalds */
141da177e4SLinus Torvalds
159b9cf81aSPaul Gortmaker#include <linux/init.h>
169b9cf81aSPaul Gortmaker
176f6f6a70SRob Herring#include <asm/assembler.h>
18f09b9979SNicolas Pitre#include <asm/memory.h>
19753790e7SRussell King#include <asm/glue-df.h>
20753790e7SRussell King#include <asm/glue-pf.h>
211da177e4SLinus Torvalds#include <asm/vfpmacros.h>
22d6551e88SRussell King#include <asm/thread_notify.h>
23c4c5716eSCatalin Marinas#include <asm/unwind.h>
24cc20d429SRussell King#include <asm/unistd.h>
25f159f4edSTony Lindgren#include <asm/tls.h>
269f97da78SDavid Howells#include <asm/system_info.h>
27747ffc2fSRussell King#include <asm/uaccess-asm.h>
281da177e4SLinus Torvalds
291da177e4SLinus Torvalds#include "entry-header.S"
30a0266c21SWang Nan#include <asm/probes.h>
311da177e4SLinus Torvalds
321da177e4SLinus Torvalds/*
33d9600c99SRussell King * Interrupt handling.
34187a51adSRussell King */
35d4664b6cSArd Biesheuvel	.macro	irq_handler, from_user:req
3652108641Seric miao	mov	r0, sp
37d4664b6cSArd Biesheuvel#ifdef CONFIG_IRQSTACKS
38d4664b6cSArd Biesheuvel	mov_l	r2, irq_stack_ptr	@ Take base address
39d4664b6cSArd Biesheuvel	mrc	p15, 0, r3, c13, c0, 4	@ Get CPU offset
40d4664b6cSArd Biesheuvel#ifdef CONFIG_UNWINDER_ARM
41d4664b6cSArd Biesheuvel	mov	fpreg, sp		@ Preserve original SP
42d4664b6cSArd Biesheuvel#else
43d4664b6cSArd Biesheuvel	mov	r8, fp			@ Preserve original FP
44d4664b6cSArd Biesheuvel	mov	r9, sp			@ Preserve original SP
45d4664b6cSArd Biesheuvel#endif
46d4664b6cSArd Biesheuvel	ldr	sp, [r2, r3]		@ Load SP from per-CPU var
47d4664b6cSArd Biesheuvel	.if	\from_user == 0
48d4664b6cSArd BiesheuvelUNWIND(	.setfp	fpreg, sp		)
49d4664b6cSArd Biesheuvel	@
50d4664b6cSArd Biesheuvel	@ If we took the interrupt while running in the kernel, we may already
51d4664b6cSArd Biesheuvel	@ be using the IRQ stack, so revert to the original value in that case.
52d4664b6cSArd Biesheuvel	@
53d4664b6cSArd Biesheuvel	subs	r2, sp, r0		@ SP above bottom of IRQ stack?
54d4664b6cSArd Biesheuvel	rsbscs	r2, r2, #THREAD_SIZE	@ ... and below the top?
55a1c510d0SArd Biesheuvel#ifdef CONFIG_VMAP_STACK
56a1c510d0SArd Biesheuvel	ldr_l	r2, high_memory, cc	@ End of the linear region
57a1c510d0SArd Biesheuvel	cmpcc	r2, r0			@ Stack pointer was below it?
58a1c510d0SArd Biesheuvel#endif
59d4664b6cSArd Biesheuvel	movcs	sp, r0			@ If so, revert to incoming SP
60d4664b6cSArd Biesheuvel
61d4664b6cSArd Biesheuvel#ifndef CONFIG_UNWINDER_ARM
62d4664b6cSArd Biesheuvel	@
63d4664b6cSArd Biesheuvel	@ Inform the frame pointer unwinder where the next frame lives
64d4664b6cSArd Biesheuvel	@
65d4664b6cSArd Biesheuvel	movcc	lr, pc			@ Make LR point into .entry.text so
66d4664b6cSArd Biesheuvel					@ that we will get a dump of the
67d4664b6cSArd Biesheuvel					@ exception stack for this frame.
68d4664b6cSArd Biesheuvel#ifdef CONFIG_CC_IS_GCC
69d4664b6cSArd Biesheuvel	movcc	ip, r0			@ Store the old SP in the frame record.
70d4664b6cSArd Biesheuvel	stmdbcc	sp!, {fp, ip, lr, pc}	@ Push frame record
71d4664b6cSArd Biesheuvel	addcc	fp, sp, #12
72d4664b6cSArd Biesheuvel#else
73d4664b6cSArd Biesheuvel	stmdbcc	sp!, {fp, lr}		@ Push frame record
74d4664b6cSArd Biesheuvel	movcc	fp, sp
75d4664b6cSArd Biesheuvel#endif // CONFIG_CC_IS_GCC
76d4664b6cSArd Biesheuvel#endif // CONFIG_UNWINDER_ARM
77d4664b6cSArd Biesheuvel	.endif
78d4664b6cSArd Biesheuvel#endif // CONFIG_IRQSTACKS
79d4664b6cSArd Biesheuvel
80a7b0872eSMark Rutland	bl	generic_handle_arch_irq
81d4664b6cSArd Biesheuvel
82d4664b6cSArd Biesheuvel#ifdef CONFIG_IRQSTACKS
83d4664b6cSArd Biesheuvel#ifdef CONFIG_UNWINDER_ARM
84d4664b6cSArd Biesheuvel	mov	sp, fpreg		@ Restore original SP
85d4664b6cSArd Biesheuvel#else
86d4664b6cSArd Biesheuvel	mov	fp, r8			@ Restore original FP
87d4664b6cSArd Biesheuvel	mov	sp, r9			@ Restore original SP
88d4664b6cSArd Biesheuvel#endif // CONFIG_UNWINDER_ARM
89d4664b6cSArd Biesheuvel#endif // CONFIG_IRQSTACKS
90187a51adSRussell King	.endm
91187a51adSRussell King
92ac8b9c1cSRussell King	.macro	pabt_helper
938dfe7ac9SRussell King	@ PABORT handler takes pt_regs in r2, fault address in r4 and psr in r5
94ac8b9c1cSRussell King#ifdef MULTI_PABORT
950402beceSRussell King	ldr	ip, .LCprocfns
96ac8b9c1cSRussell King	mov	lr, pc
970402beceSRussell King	ldr	pc, [ip, #PROCESSOR_PABT_FUNC]
98ac8b9c1cSRussell King#else
99ac8b9c1cSRussell King	bl	CPU_PABORT_HANDLER
100ac8b9c1cSRussell King#endif
101ac8b9c1cSRussell King	.endm
102ac8b9c1cSRussell King
103ac8b9c1cSRussell King	.macro	dabt_helper
104ac8b9c1cSRussell King
105ac8b9c1cSRussell King	@
106ac8b9c1cSRussell King	@ Call the processor-specific abort handler:
107ac8b9c1cSRussell King	@
108da740472SRussell King	@  r2 - pt_regs
1093e287becSRussell King	@  r4 - aborted context pc
1103e287becSRussell King	@  r5 - aborted context psr
111ac8b9c1cSRussell King	@
112ac8b9c1cSRussell King	@ The abort handler must return the aborted address in r0, and
113ac8b9c1cSRussell King	@ the fault status register in r1.  r9 must be preserved.
114ac8b9c1cSRussell King	@
115ac8b9c1cSRussell King#ifdef MULTI_DABORT
1160402beceSRussell King	ldr	ip, .LCprocfns
117ac8b9c1cSRussell King	mov	lr, pc
1180402beceSRussell King	ldr	pc, [ip, #PROCESSOR_DABT_FUNC]
119ac8b9c1cSRussell King#else
120ac8b9c1cSRussell King	bl	CPU_DABORT_HANDLER
121ac8b9c1cSRussell King#endif
122ac8b9c1cSRussell King	.endm
123ac8b9c1cSRussell King
124c6089061SRussell King	.section	.entry.text,"ax",%progbits
125785d3cd2SNicolas Pitre
126187a51adSRussell King/*
1271da177e4SLinus Torvalds * Invalid mode handlers
1281da177e4SLinus Torvalds */
129ccea7a19SRussell King	.macro	inv_entry, reason
1305745eef6SRussell King	sub	sp, sp, #PT_REGS_SIZE
131b86040a5SCatalin Marinas ARM(	stmib	sp, {r1 - lr}		)
132b86040a5SCatalin Marinas THUMB(	stmia	sp, {r0 - r12}		)
133b86040a5SCatalin Marinas THUMB(	str	sp, [sp, #S_SP]		)
134b86040a5SCatalin Marinas THUMB(	str	lr, [sp, #S_LR]		)
1351da177e4SLinus Torvalds	mov	r1, #\reason
1361da177e4SLinus Torvalds	.endm
1371da177e4SLinus Torvalds
1381da177e4SLinus Torvalds__pabt_invalid:
139ccea7a19SRussell King	inv_entry BAD_PREFETCH
140ccea7a19SRussell King	b	common_invalid
14193ed3970SCatalin MarinasENDPROC(__pabt_invalid)
1421da177e4SLinus Torvalds
1431da177e4SLinus Torvalds__dabt_invalid:
144ccea7a19SRussell King	inv_entry BAD_DATA
145ccea7a19SRussell King	b	common_invalid
14693ed3970SCatalin MarinasENDPROC(__dabt_invalid)
1471da177e4SLinus Torvalds
1481da177e4SLinus Torvalds__irq_invalid:
149ccea7a19SRussell King	inv_entry BAD_IRQ
150ccea7a19SRussell King	b	common_invalid
15193ed3970SCatalin MarinasENDPROC(__irq_invalid)
1521da177e4SLinus Torvalds
1531da177e4SLinus Torvalds__und_invalid:
154ccea7a19SRussell King	inv_entry BAD_UNDEFINSTR
1551da177e4SLinus Torvalds
156ccea7a19SRussell King	@
157ccea7a19SRussell King	@ XXX fall through to common_invalid
158ccea7a19SRussell King	@
159ccea7a19SRussell King
160ccea7a19SRussell King@
161ccea7a19SRussell King@ common_invalid - generic code for failed exception (re-entrant version of handlers)
162ccea7a19SRussell King@
163ccea7a19SRussell Kingcommon_invalid:
164ccea7a19SRussell King	zero_fp
165ccea7a19SRussell King
166ccea7a19SRussell King	ldmia	r0, {r4 - r6}
167ccea7a19SRussell King	add	r0, sp, #S_PC		@ here for interlock avoidance
168ccea7a19SRussell King	mov	r7, #-1			@  ""   ""    ""        ""
169ccea7a19SRussell King	str	r4, [sp]		@ save preserved r0
170ccea7a19SRussell King	stmia	r0, {r5 - r7}		@ lr_<exception>,
171ccea7a19SRussell King					@ cpsr_<exception>, "old_r0"
172ccea7a19SRussell King
1731da177e4SLinus Torvalds	mov	r0, sp
1741da177e4SLinus Torvalds	b	bad_mode
17593ed3970SCatalin MarinasENDPROC(__und_invalid)
1761da177e4SLinus Torvalds
1771da177e4SLinus Torvalds/*
1781da177e4SLinus Torvalds * SVC mode handlers
1791da177e4SLinus Torvalds */
1802dede2d8SNicolas Pitre
1812dede2d8SNicolas Pitre#if defined(CONFIG_AEABI) && (__LINUX_ARM_ARCH__ >= 5)
1822dede2d8SNicolas Pitre#define SPFIX(code...) code
1832dede2d8SNicolas Pitre#else
1842dede2d8SNicolas Pitre#define SPFIX(code...)
1852dede2d8SNicolas Pitre#endif
1862dede2d8SNicolas Pitre
187a1c510d0SArd Biesheuvel	.macro	svc_entry, stack_hole=0, trace=1, uaccess=1, overflow_check=1
188c4c5716eSCatalin Marinas UNWIND(.fnstart		)
189ae5cc07dSArd Biesheuvel	sub	sp, sp, #(SVC_REGS_SIZE + \stack_hole)
190a1c510d0SArd Biesheuvel THUMB(	add	sp, r1		)	@ get SP in a GPR without
191a1c510d0SArd Biesheuvel THUMB(	sub	r1, sp, r1	)	@ using a temp register
192a1c510d0SArd Biesheuvel
193a1c510d0SArd Biesheuvel	.if	\overflow_check
194a1c510d0SArd Biesheuvel UNWIND(.save	{r0 - pc}	)
195a1c510d0SArd Biesheuvel	do_overflow_check (SVC_REGS_SIZE + \stack_hole)
196a1c510d0SArd Biesheuvel	.endif
197a1c510d0SArd Biesheuvel
198b86040a5SCatalin Marinas#ifdef CONFIG_THUMB2_KERNEL
199ae5cc07dSArd Biesheuvel	tst	r1, #4			@ test stack pointer alignment
200ae5cc07dSArd Biesheuvel	sub	r1, sp, r1		@ restore original R1
201ae5cc07dSArd Biesheuvel	sub	sp, r1			@ restore original SP
202b86040a5SCatalin Marinas#else
2032dede2d8SNicolas Pitre SPFIX(	tst	sp, #4		)
204b86040a5SCatalin Marinas#endif
205ae5cc07dSArd Biesheuvel SPFIX(	subne	sp, sp, #4	)
206ae5cc07dSArd Biesheuvel
207ae5cc07dSArd Biesheuvel ARM(	stmib	sp, {r1 - r12}	)
208ae5cc07dSArd Biesheuvel THUMB(	stmia	sp, {r0 - r12}	)	@ No STMIB in Thumb-2
209ccea7a19SRussell King
210b059bdc3SRussell King	ldmia	r0, {r3 - r5}
211ae5cc07dSArd Biesheuvel	add	r7, sp, #S_SP		@ here for interlock avoidance
212b059bdc3SRussell King	mov	r6, #-1			@  ""  ""      ""       ""
213ae5cc07dSArd Biesheuvel	add	r2, sp, #(SVC_REGS_SIZE + \stack_hole)
214ae5cc07dSArd Biesheuvel SPFIX(	addne	r2, r2, #4	)
215ae5cc07dSArd Biesheuvel	str	r3, [sp]		@ save the "real" r0 copied
216ccea7a19SRussell King					@ from the exception stack
217ccea7a19SRussell King
218b059bdc3SRussell King	mov	r3, lr
2191da177e4SLinus Torvalds
2201da177e4SLinus Torvalds	@
2211da177e4SLinus Torvalds	@ We are now ready to fill in the remaining blanks on the stack:
2221da177e4SLinus Torvalds	@
223b059bdc3SRussell King	@  r2 - sp_svc
224b059bdc3SRussell King	@  r3 - lr_svc
225b059bdc3SRussell King	@  r4 - lr_<exception>, already fixed up for correct return/restart
226b059bdc3SRussell King	@  r5 - spsr_<exception>
227b059bdc3SRussell King	@  r6 - orig_r0 (see pt_regs definition in ptrace.h)
2281da177e4SLinus Torvalds	@
229b059bdc3SRussell King	stmia	r7, {r2 - r6}
230f2741b78SRussell King
231e6978e4bSRussell King	get_thread_info tsk
232747ffc2fSRussell King	uaccess_entry tsk, r0, r1, r2, \uaccess
2332190fed6SRussell King
234c0e7f7eeSDaniel Thompson	.if \trace
235f2741b78SRussell King#ifdef CONFIG_TRACE_IRQFLAGS
236f2741b78SRussell King	bl	trace_hardirqs_off
237f2741b78SRussell King#endif
238c0e7f7eeSDaniel Thompson	.endif
2391da177e4SLinus Torvalds	.endm
2401da177e4SLinus Torvalds
2411da177e4SLinus Torvalds	.align	5
2421da177e4SLinus Torvalds__dabt_svc:
2432190fed6SRussell King	svc_entry uaccess=0
2441da177e4SLinus Torvalds	mov	r2, sp
245da740472SRussell King	dabt_helper
246e16b31bfSMarc Zyngier THUMB(	ldr	r5, [sp, #S_PSR]	)	@ potentially updated CPSR
247b059bdc3SRussell King	svc_exit r5				@ return from exception
248c4c5716eSCatalin Marinas UNWIND(.fnend		)
24993ed3970SCatalin MarinasENDPROC(__dabt_svc)
2501da177e4SLinus Torvalds
2511da177e4SLinus Torvalds	.align	5
2521da177e4SLinus Torvalds__irq_svc:
253ccea7a19SRussell King	svc_entry
254d4664b6cSArd Biesheuvel	irq_handler from_user=0
2551613cc11SRussell King
256e7289c6dSThomas Gleixner#ifdef CONFIG_PREEMPTION
257706fdd9fSRussell King	ldr	r8, [tsk, #TI_PREEMPT]		@ get preempt count
258706fdd9fSRussell King	ldr	r0, [tsk, #TI_FLAGS]		@ get flags
25928fab1a2SRussell King	teq	r8, #0				@ if preempt count != 0
26028fab1a2SRussell King	movne	r0, #0				@ force flags to 0
2611da177e4SLinus Torvalds	tst	r0, #_TIF_NEED_RESCHED
2621da177e4SLinus Torvalds	blne	svc_preempt
2631da177e4SLinus Torvalds#endif
26430891c90SRussell King
2659b56febeSRussell King	svc_exit r5, irq = 1			@ return from exception
266c4c5716eSCatalin Marinas UNWIND(.fnend		)
26793ed3970SCatalin MarinasENDPROC(__irq_svc)
2681da177e4SLinus Torvalds
2691da177e4SLinus Torvalds	.ltorg
2701da177e4SLinus Torvalds
271e7289c6dSThomas Gleixner#ifdef CONFIG_PREEMPTION
2721da177e4SLinus Torvaldssvc_preempt:
27328fab1a2SRussell King	mov	r8, lr
2741da177e4SLinus Torvalds1:	bl	preempt_schedule_irq		@ irq en/disable is done inside
275706fdd9fSRussell King	ldr	r0, [tsk, #TI_FLAGS]		@ get new tasks TI_FLAGS
2761da177e4SLinus Torvalds	tst	r0, #_TIF_NEED_RESCHED
2776ebbf2ceSRussell King	reteq	r8				@ go again
2781da177e4SLinus Torvalds	b	1b
2791da177e4SLinus Torvalds#endif
2801da177e4SLinus Torvalds
28115ac49b6SRussell King__und_fault:
28215ac49b6SRussell King	@ Correct the PC such that it is pointing at the instruction
28315ac49b6SRussell King	@ which caused the fault.  If the faulting instruction was ARM
28415ac49b6SRussell King	@ the PC will be pointing at the next instruction, and have to
28515ac49b6SRussell King	@ subtract 4.  Otherwise, it is Thumb, and the PC will be
28615ac49b6SRussell King	@ pointing at the second half of the Thumb instruction.  We
28715ac49b6SRussell King	@ have to subtract 2.
28815ac49b6SRussell King	ldr	r2, [r0, #S_PC]
28915ac49b6SRussell King	sub	r2, r2, r1
29015ac49b6SRussell King	str	r2, [r0, #S_PC]
29115ac49b6SRussell King	b	do_undefinstr
29215ac49b6SRussell KingENDPROC(__und_fault)
29315ac49b6SRussell King
2941da177e4SLinus Torvalds	.align	5
2951da177e4SLinus Torvalds__und_svc:
296d30a0c8bSNicolas Pitre#ifdef CONFIG_KPROBES
297d30a0c8bSNicolas Pitre	@ If a kprobe is about to simulate a "stmdb sp..." instruction,
298d30a0c8bSNicolas Pitre	@ it obviously needs free stack space which then will belong to
299d30a0c8bSNicolas Pitre	@ the saved context.
300a0266c21SWang Nan	svc_entry MAX_STACK_SIZE
301d30a0c8bSNicolas Pitre#else
302ccea7a19SRussell King	svc_entry
303d30a0c8bSNicolas Pitre#endif
3041da177e4SLinus Torvalds
30515ac49b6SRussell King	mov	r1, #4				@ PC correction to apply
306f77ac2e3SArd Biesheuvel THUMB(	tst	r5, #PSR_T_BIT		)	@ exception taken in Thumb mode?
307f77ac2e3SArd Biesheuvel THUMB(	movne	r1, #2			)	@ if so, fix up PC correction
3081da177e4SLinus Torvalds	mov	r0, sp				@ struct pt_regs *regs
30915ac49b6SRussell King	bl	__und_fault
3101da177e4SLinus Torvalds
31115ac49b6SRussell King__und_svc_finish:
31287eed3c7SRussell King	get_thread_info tsk
313b059bdc3SRussell King	ldr	r5, [sp, #S_PSR]		@ Get SVC cpsr
314b059bdc3SRussell King	svc_exit r5				@ return from exception
315c4c5716eSCatalin Marinas UNWIND(.fnend		)
31693ed3970SCatalin MarinasENDPROC(__und_svc)
3171da177e4SLinus Torvalds
3181da177e4SLinus Torvalds	.align	5
3191da177e4SLinus Torvalds__pabt_svc:
320ccea7a19SRussell King	svc_entry
3214fb28474SKirill A. Shutemov	mov	r2, sp				@ regs
3228dfe7ac9SRussell King	pabt_helper
323b059bdc3SRussell King	svc_exit r5				@ return from exception
324c4c5716eSCatalin Marinas UNWIND(.fnend		)
32593ed3970SCatalin MarinasENDPROC(__pabt_svc)
3261da177e4SLinus Torvalds
3271da177e4SLinus Torvalds	.align	5
328c0e7f7eeSDaniel Thompson__fiq_svc:
329c0e7f7eeSDaniel Thompson	svc_entry trace=0
330c0e7f7eeSDaniel Thompson	mov	r0, sp				@ struct pt_regs *regs
331c0e7f7eeSDaniel Thompson	bl	handle_fiq_as_nmi
332c0e7f7eeSDaniel Thompson	svc_exit_via_fiq
333c0e7f7eeSDaniel Thompson UNWIND(.fnend		)
334c0e7f7eeSDaniel ThompsonENDPROC(__fiq_svc)
335c0e7f7eeSDaniel Thompson
336c0e7f7eeSDaniel Thompson	.align	5
33749f680eaSRussell King.LCcralign:
33849f680eaSRussell King	.word	cr_alignment
33948d7927bSPaul Brook#ifdef MULTI_DABORT
3401da177e4SLinus Torvalds.LCprocfns:
3411da177e4SLinus Torvalds	.word	processor
3421da177e4SLinus Torvalds#endif
3431da177e4SLinus Torvalds.LCfp:
3441da177e4SLinus Torvalds	.word	fp_enter
3451da177e4SLinus Torvalds
3461da177e4SLinus Torvalds/*
347c0e7f7eeSDaniel Thompson * Abort mode handlers
348c0e7f7eeSDaniel Thompson */
349c0e7f7eeSDaniel Thompson
350c0e7f7eeSDaniel Thompson@
351c0e7f7eeSDaniel Thompson@ Taking a FIQ in abort mode is similar to taking a FIQ in SVC mode
352c0e7f7eeSDaniel Thompson@ and reuses the same macros. However in abort mode we must also
353c0e7f7eeSDaniel Thompson@ save/restore lr_abt and spsr_abt to make nested aborts safe.
354c0e7f7eeSDaniel Thompson@
355c0e7f7eeSDaniel Thompson	.align 5
356c0e7f7eeSDaniel Thompson__fiq_abt:
357c0e7f7eeSDaniel Thompson	svc_entry trace=0
358c0e7f7eeSDaniel Thompson
359c0e7f7eeSDaniel Thompson ARM(	msr	cpsr_c, #ABT_MODE | PSR_I_BIT | PSR_F_BIT )
360c0e7f7eeSDaniel Thompson THUMB( mov	r0, #ABT_MODE | PSR_I_BIT | PSR_F_BIT )
361c0e7f7eeSDaniel Thompson THUMB( msr	cpsr_c, r0 )
362c0e7f7eeSDaniel Thompson	mov	r1, lr		@ Save lr_abt
363c0e7f7eeSDaniel Thompson	mrs	r2, spsr	@ Save spsr_abt, abort is now safe
364c0e7f7eeSDaniel Thompson ARM(	msr	cpsr_c, #SVC_MODE | PSR_I_BIT | PSR_F_BIT )
365c0e7f7eeSDaniel Thompson THUMB( mov	r0, #SVC_MODE | PSR_I_BIT | PSR_F_BIT )
366c0e7f7eeSDaniel Thompson THUMB( msr	cpsr_c, r0 )
367c0e7f7eeSDaniel Thompson	stmfd	sp!, {r1 - r2}
368c0e7f7eeSDaniel Thompson
369c0e7f7eeSDaniel Thompson	add	r0, sp, #8			@ struct pt_regs *regs
370c0e7f7eeSDaniel Thompson	bl	handle_fiq_as_nmi
371c0e7f7eeSDaniel Thompson
372c0e7f7eeSDaniel Thompson	ldmfd	sp!, {r1 - r2}
373c0e7f7eeSDaniel Thompson ARM(	msr	cpsr_c, #ABT_MODE | PSR_I_BIT | PSR_F_BIT )
374c0e7f7eeSDaniel Thompson THUMB( mov	r0, #ABT_MODE | PSR_I_BIT | PSR_F_BIT )
375c0e7f7eeSDaniel Thompson THUMB( msr	cpsr_c, r0 )
376c0e7f7eeSDaniel Thompson	mov	lr, r1		@ Restore lr_abt, abort is unsafe
377c0e7f7eeSDaniel Thompson	msr	spsr_cxsf, r2	@ Restore spsr_abt
378c0e7f7eeSDaniel Thompson ARM(	msr	cpsr_c, #SVC_MODE | PSR_I_BIT | PSR_F_BIT )
379c0e7f7eeSDaniel Thompson THUMB( mov	r0, #SVC_MODE | PSR_I_BIT | PSR_F_BIT )
380c0e7f7eeSDaniel Thompson THUMB( msr	cpsr_c, r0 )
381c0e7f7eeSDaniel Thompson
382c0e7f7eeSDaniel Thompson	svc_exit_via_fiq
383c0e7f7eeSDaniel Thompson UNWIND(.fnend		)
384c0e7f7eeSDaniel ThompsonENDPROC(__fiq_abt)
385c0e7f7eeSDaniel Thompson
386c0e7f7eeSDaniel Thompson/*
3871da177e4SLinus Torvalds * User mode handlers
3882dede2d8SNicolas Pitre *
3895745eef6SRussell King * EABI note: sp_svc is always 64-bit aligned here, so should PT_REGS_SIZE
3901da177e4SLinus Torvalds */
3912dede2d8SNicolas Pitre
3925745eef6SRussell King#if defined(CONFIG_AEABI) && (__LINUX_ARM_ARCH__ >= 5) && (PT_REGS_SIZE & 7)
3932dede2d8SNicolas Pitre#error "sizeof(struct pt_regs) must be a multiple of 8"
3942dede2d8SNicolas Pitre#endif
3952dede2d8SNicolas Pitre
3962190fed6SRussell King	.macro	usr_entry, trace=1, uaccess=1
397c4c5716eSCatalin Marinas UNWIND(.fnstart	)
398c4c5716eSCatalin Marinas UNWIND(.cantunwind	)	@ don't unwind the user space
3995745eef6SRussell King	sub	sp, sp, #PT_REGS_SIZE
400b86040a5SCatalin Marinas ARM(	stmib	sp, {r1 - r12}	)
401b86040a5SCatalin Marinas THUMB(	stmia	sp, {r0 - r12}	)
402ccea7a19SRussell King
403195b58adSRussell King ATRAP(	mrc	p15, 0, r7, c1, c0, 0)
404195b58adSRussell King ATRAP(	ldr	r8, .LCcralign)
405195b58adSRussell King
406b059bdc3SRussell King	ldmia	r0, {r3 - r5}
407ccea7a19SRussell King	add	r0, sp, #S_PC		@ here for interlock avoidance
408b059bdc3SRussell King	mov	r6, #-1			@  ""  ""     ""        ""
409ccea7a19SRussell King
410b059bdc3SRussell King	str	r3, [sp]		@ save the "real" r0 copied
411ccea7a19SRussell King					@ from the exception stack
4121da177e4SLinus Torvalds
413195b58adSRussell King ATRAP(	ldr	r8, [r8, #0])
414195b58adSRussell King
4151da177e4SLinus Torvalds	@
4161da177e4SLinus Torvalds	@ We are now ready to fill in the remaining blanks on the stack:
4171da177e4SLinus Torvalds	@
418b059bdc3SRussell King	@  r4 - lr_<exception>, already fixed up for correct return/restart
419b059bdc3SRussell King	@  r5 - spsr_<exception>
420b059bdc3SRussell King	@  r6 - orig_r0 (see pt_regs definition in ptrace.h)
4211da177e4SLinus Torvalds	@
4221da177e4SLinus Torvalds	@ Also, separately save sp_usr and lr_usr
4231da177e4SLinus Torvalds	@
424b059bdc3SRussell King	stmia	r0, {r4 - r6}
425b86040a5SCatalin Marinas ARM(	stmdb	r0, {sp, lr}^			)
426b86040a5SCatalin Marinas THUMB(	store_user_sp_lr r0, r1, S_SP - S_PC	)
4271da177e4SLinus Torvalds
4282190fed6SRussell King	.if \uaccess
4292190fed6SRussell King	uaccess_disable ip
4302190fed6SRussell King	.endif
4312190fed6SRussell King
4321da177e4SLinus Torvalds	@ Enable the alignment trap while in kernel mode
433195b58adSRussell King ATRAP(	teq	r8, r7)
434195b58adSRussell King ATRAP( mcrne	p15, 0, r8, c1, c0, 0)
4351da177e4SLinus Torvalds
43650596b75SArd Biesheuvel	reload_current r7, r8
43750596b75SArd Biesheuvel
4381da177e4SLinus Torvalds	@
4391da177e4SLinus Torvalds	@ Clear FP to mark the first stack frame
4401da177e4SLinus Torvalds	@
4411da177e4SLinus Torvalds	zero_fp
442f2741b78SRussell King
443c0e7f7eeSDaniel Thompson	.if	\trace
44411b8b25cSRussell King#ifdef CONFIG_TRACE_IRQFLAGS
445f2741b78SRussell King	bl	trace_hardirqs_off
446f2741b78SRussell King#endif
447b0088480SKevin Hilman	ct_user_exit save = 0
448c0e7f7eeSDaniel Thompson	.endif
4491da177e4SLinus Torvalds	.endm
4501da177e4SLinus Torvalds
451b49c0f24SNicolas Pitre	.macro	kuser_cmpxchg_check
452db695c05SRussell King#if !defined(CONFIG_CPU_32v6K) && defined(CONFIG_KUSER_HELPERS)
453b49c0f24SNicolas Pitre#ifndef CONFIG_MMU
454b49c0f24SNicolas Pitre#warning "NPTL on non MMU needs fixing"
455b49c0f24SNicolas Pitre#else
456b49c0f24SNicolas Pitre	@ Make sure our user space atomic helper is restarted
457b49c0f24SNicolas Pitre	@ if it was interrupted in a critical region.  Here we
458b49c0f24SNicolas Pitre	@ perform a quick test inline since it should be false
459b49c0f24SNicolas Pitre	@ 99.9999% of the time.  The rest is done out of line.
460c12366baSLinus Walleij	ldr	r0, =TASK_SIZE
461c12366baSLinus Walleij	cmp	r4, r0
46240fb79c8SNicolas Pitre	blhs	kuser_cmpxchg64_fixup
463b49c0f24SNicolas Pitre#endif
464b49c0f24SNicolas Pitre#endif
465b49c0f24SNicolas Pitre	.endm
466b49c0f24SNicolas Pitre
4671da177e4SLinus Torvalds	.align	5
4681da177e4SLinus Torvalds__dabt_usr:
4692190fed6SRussell King	usr_entry uaccess=0
470b49c0f24SNicolas Pitre	kuser_cmpxchg_check
4711da177e4SLinus Torvalds	mov	r2, sp
472da740472SRussell King	dabt_helper
473da740472SRussell King	b	ret_from_exception
474c4c5716eSCatalin Marinas UNWIND(.fnend		)
47593ed3970SCatalin MarinasENDPROC(__dabt_usr)
4761da177e4SLinus Torvalds
4771da177e4SLinus Torvalds	.align	5
4781da177e4SLinus Torvalds__irq_usr:
479ccea7a19SRussell King	usr_entry
480bc089602SRussell King	kuser_cmpxchg_check
481d4664b6cSArd Biesheuvel	irq_handler from_user=1
4821613cc11SRussell King	get_thread_info tsk
4831da177e4SLinus Torvalds	mov	why, #0
4849fc2552aSMing Lei	b	ret_to_user_from_irq
485c4c5716eSCatalin Marinas UNWIND(.fnend		)
48693ed3970SCatalin MarinasENDPROC(__irq_usr)
4871da177e4SLinus Torvalds
4881da177e4SLinus Torvalds	.ltorg
4891da177e4SLinus Torvalds
4901da177e4SLinus Torvalds	.align	5
4911da177e4SLinus Torvalds__und_usr:
4922190fed6SRussell King	usr_entry uaccess=0
493bc089602SRussell King
494b059bdc3SRussell King	mov	r2, r4
495b059bdc3SRussell King	mov	r3, r5
4961da177e4SLinus Torvalds
49715ac49b6SRussell King	@ r2 = regs->ARM_pc, which is either 2 or 4 bytes ahead of the
49815ac49b6SRussell King	@      faulting instruction depending on Thumb mode.
49915ac49b6SRussell King	@ r3 = regs->ARM_cpsr
5001da177e4SLinus Torvalds	@
50115ac49b6SRussell King	@ The emulation code returns using r9 if it has emulated the
50215ac49b6SRussell King	@ instruction, or the more conventional lr if we are to treat
50315ac49b6SRussell King	@ this as a real undefined instruction
5041da177e4SLinus Torvalds	@
50514327c66SRussell King	badr	r9, ret_from_exception
50615ac49b6SRussell King
5071417a6b8SCatalin Marinas	@ IRQs must be enabled before attempting to read the instruction from
5081417a6b8SCatalin Marinas	@ user space since that could cause a page/translation fault if the
5091417a6b8SCatalin Marinas	@ page table was modified by another CPU.
5101417a6b8SCatalin Marinas	enable_irq
5111417a6b8SCatalin Marinas
512cb170a45SPaul Brook	tst	r3, #PSR_T_BIT			@ Thumb mode?
51315ac49b6SRussell King	bne	__und_usr_thumb
51415ac49b6SRussell King	sub	r4, r2, #4			@ ARM instr at LR - 4
51515ac49b6SRussell King1:	ldrt	r0, [r4]
516457c2403SBen Dooks ARM_BE8(rev	r0, r0)				@ little endian instruction
517457c2403SBen Dooks
5182190fed6SRussell King	uaccess_disable ip
5192190fed6SRussell King
52015ac49b6SRussell King	@ r0 = 32-bit ARM instruction which caused the exception
52115ac49b6SRussell King	@ r2 = PC value for the following instruction (:= regs->ARM_pc)
52215ac49b6SRussell King	@ r4 = PC value for the faulting instruction
52315ac49b6SRussell King	@ lr = 32-bit undefined instruction function
52414327c66SRussell King	badr	lr, __und_usr_fault_32
52515ac49b6SRussell King	b	call_fpe
52615ac49b6SRussell King
52715ac49b6SRussell King__und_usr_thumb:
528cb170a45SPaul Brook	@ Thumb instruction
52915ac49b6SRussell King	sub	r4, r2, #2			@ First half of thumb instr at LR - 2
530ef4c5368SDave Martin#if CONFIG_ARM_THUMB && __LINUX_ARM_ARCH__ >= 6 && CONFIG_CPU_V7
531ef4c5368SDave Martin/*
532ef4c5368SDave Martin * Thumb-2 instruction handling.  Note that because pre-v6 and >= v6 platforms
533ef4c5368SDave Martin * can never be supported in a single kernel, this code is not applicable at
534ef4c5368SDave Martin * all when __LINUX_ARM_ARCH__ < 6.  This allows simplifying assumptions to be
535ef4c5368SDave Martin * made about .arch directives.
536ef4c5368SDave Martin */
537ef4c5368SDave Martin#if __LINUX_ARM_ARCH__ < 7
538ef4c5368SDave Martin/* If the target CPU may not be Thumb-2-capable, a run-time check is needed: */
539ef4c5368SDave Martin#define NEED_CPU_ARCHITECTURE
540ef4c5368SDave Martin	ldr	r5, .LCcpu_architecture
541ef4c5368SDave Martin	ldr	r5, [r5]
542ef4c5368SDave Martin	cmp	r5, #CPU_ARCH_ARMv7
54315ac49b6SRussell King	blo	__und_usr_fault_16		@ 16bit undefined instruction
544ef4c5368SDave Martin/*
545ef4c5368SDave Martin * The following code won't get run unless the running CPU really is v7, so
546ef4c5368SDave Martin * coding round the lack of ldrht on older arches is pointless.  Temporarily
547ef4c5368SDave Martin * override the assembler target arch with the minimum required instead:
548ef4c5368SDave Martin */
549ef4c5368SDave Martin	.arch	armv6t2
550ef4c5368SDave Martin#endif
55115ac49b6SRussell King2:	ldrht	r5, [r4]
552f8fe23ecSVictor KamenskyARM_BE8(rev16	r5, r5)				@ little endian instruction
55385519189SDave Martin	cmp	r5, #0xe800			@ 32bit instruction if xx != 0
5542190fed6SRussell King	blo	__und_usr_fault_16_pan		@ 16bit undefined instruction
55515ac49b6SRussell King3:	ldrht	r0, [r2]
556f8fe23ecSVictor KamenskyARM_BE8(rev16	r0, r0)				@ little endian instruction
5572190fed6SRussell King	uaccess_disable ip
558cb170a45SPaul Brook	add	r2, r2, #2			@ r2 is PC + 2, make it PC + 4
55915ac49b6SRussell King	str	r2, [sp, #S_PC]			@ it's a 2x16bit instr, update
560cb170a45SPaul Brook	orr	r0, r0, r5, lsl #16
56114327c66SRussell King	badr	lr, __und_usr_fault_32
56215ac49b6SRussell King	@ r0 = the two 16-bit Thumb instructions which caused the exception
56315ac49b6SRussell King	@ r2 = PC value for the following Thumb instruction (:= regs->ARM_pc)
56415ac49b6SRussell King	@ r4 = PC value for the first 16-bit Thumb instruction
56515ac49b6SRussell King	@ lr = 32bit undefined instruction function
566ef4c5368SDave Martin
567ef4c5368SDave Martin#if __LINUX_ARM_ARCH__ < 7
568ef4c5368SDave Martin/* If the target arch was overridden, change it back: */
569ef4c5368SDave Martin#ifdef CONFIG_CPU_32v6K
570ef4c5368SDave Martin	.arch	armv6k
571cb170a45SPaul Brook#else
572ef4c5368SDave Martin	.arch	armv6
573ef4c5368SDave Martin#endif
574ef4c5368SDave Martin#endif /* __LINUX_ARM_ARCH__ < 7 */
575ef4c5368SDave Martin#else /* !(CONFIG_ARM_THUMB && __LINUX_ARM_ARCH__ >= 6 && CONFIG_CPU_V7) */
57615ac49b6SRussell King	b	__und_usr_fault_16
577cb170a45SPaul Brook#endif
578c4c5716eSCatalin Marinas UNWIND(.fnend)
57993ed3970SCatalin MarinasENDPROC(__und_usr)
580cb170a45SPaul Brook
5811da177e4SLinus Torvalds/*
58215ac49b6SRussell King * The out of line fixup for the ldrt instructions above.
5831da177e4SLinus Torvalds */
584c4a84ae3SArd Biesheuvel	.pushsection .text.fixup, "ax"
585667d1b48SWill Deacon	.align	2
5863780f7abSArun K S4:	str     r4, [sp, #S_PC]			@ retry current instruction
5876ebbf2ceSRussell King	ret	r9
5884260415fSRussell King	.popsection
5894260415fSRussell King	.pushsection __ex_table,"a"
590cb170a45SPaul Brook	.long	1b, 4b
591c89cefedSGuennadi Liakhovetski#if CONFIG_ARM_THUMB && __LINUX_ARM_ARCH__ >= 6 && CONFIG_CPU_V7
592cb170a45SPaul Brook	.long	2b, 4b
593cb170a45SPaul Brook	.long	3b, 4b
594cb170a45SPaul Brook#endif
5954260415fSRussell King	.popsection
5961da177e4SLinus Torvalds
5971da177e4SLinus Torvalds/*
5981da177e4SLinus Torvalds * Check whether the instruction is a co-processor instruction.
5991da177e4SLinus Torvalds * If yes, we need to call the relevant co-processor handler.
6001da177e4SLinus Torvalds *
6011da177e4SLinus Torvalds * Note that we don't do a full check here for the co-processor
6021da177e4SLinus Torvalds * instructions; all instructions with bit 27 set are well
6031da177e4SLinus Torvalds * defined.  The only instructions that should fault are the
6041da177e4SLinus Torvalds * co-processor instructions.  However, we have to watch out
6051da177e4SLinus Torvalds * for the ARM6/ARM7 SWI bug.
6061da177e4SLinus Torvalds *
607b5872db4SCatalin Marinas * NEON is a special case that has to be handled here. Not all
608b5872db4SCatalin Marinas * NEON instructions are co-processor instructions, so we have
609b5872db4SCatalin Marinas * to make a special case of checking for them. Plus, there's
610b5872db4SCatalin Marinas * five groups of them, so we have a table of mask/opcode pairs
611b5872db4SCatalin Marinas * to check against, and if any match then we branch off into the
612b5872db4SCatalin Marinas * NEON handler code.
613b5872db4SCatalin Marinas *
6141da177e4SLinus Torvalds * Emulators may wish to make use of the following registers:
61515ac49b6SRussell King *  r0  = instruction opcode (32-bit ARM or two 16-bit Thumb)
61615ac49b6SRussell King *  r2  = PC value to resume execution after successful emulation
617db6ccbb6SRussell King *  r9  = normal "successful" return address
61815ac49b6SRussell King *  r10 = this threads thread_info structure
619db6ccbb6SRussell King *  lr  = unrecognised instruction return address
6201417a6b8SCatalin Marinas * IRQs enabled, FIQs enabled.
6211da177e4SLinus Torvalds */
622cb170a45SPaul Brook	@
623cb170a45SPaul Brook	@ Fall-through from Thumb-2 __und_usr
624cb170a45SPaul Brook	@
625cb170a45SPaul Brook#ifdef CONFIG_NEON
626d3f79584SRussell King	get_thread_info r10			@ get current thread
627cb170a45SPaul Brook	adr	r6, .LCneon_thumb_opcodes
628cb170a45SPaul Brook	b	2f
629cb170a45SPaul Brook#endif
6301da177e4SLinus Torvaldscall_fpe:
631d3f79584SRussell King	get_thread_info r10			@ get current thread
632b5872db4SCatalin Marinas#ifdef CONFIG_NEON
633cb170a45SPaul Brook	adr	r6, .LCneon_arm_opcodes
634d3f79584SRussell King2:	ldr	r5, [r6], #4			@ mask value
635b5872db4SCatalin Marinas	ldr	r7, [r6], #4			@ opcode bits matching in mask
636d3f79584SRussell King	cmp	r5, #0				@ end mask?
637d3f79584SRussell King	beq	1f
638d3f79584SRussell King	and	r8, r0, r5
639b5872db4SCatalin Marinas	cmp	r8, r7				@ NEON instruction?
640b5872db4SCatalin Marinas	bne	2b
641b5872db4SCatalin Marinas	mov	r7, #1
642b5872db4SCatalin Marinas	strb	r7, [r10, #TI_USED_CP + 10]	@ mark CP#10 as used
643b5872db4SCatalin Marinas	strb	r7, [r10, #TI_USED_CP + 11]	@ mark CP#11 as used
644b5872db4SCatalin Marinas	b	do_vfp				@ let VFP handler handle this
645b5872db4SCatalin Marinas1:
646b5872db4SCatalin Marinas#endif
6471da177e4SLinus Torvalds	tst	r0, #0x08000000			@ only CDP/CPRT/LDC/STC have bit 27
648cb170a45SPaul Brook	tstne	r0, #0x04000000			@ bit 26 set on both ARM and Thumb-2
6496ebbf2ceSRussell King	reteq	lr
6501da177e4SLinus Torvalds	and	r8, r0, #0x00000f00		@ mask out CP number
651b86040a5SCatalin Marinas THUMB(	lsr	r8, r8, #8		)
6521da177e4SLinus Torvalds	mov	r7, #1
6531da177e4SLinus Torvalds	add	r6, r10, #TI_USED_CP
654b86040a5SCatalin Marinas ARM(	strb	r7, [r6, r8, lsr #8]	)	@ set appropriate used_cp[]
655b86040a5SCatalin Marinas THUMB(	strb	r7, [r6, r8]		)	@ set appropriate used_cp[]
6561da177e4SLinus Torvalds#ifdef CONFIG_IWMMXT
6571da177e4SLinus Torvalds	@ Test if we need to give access to iWMMXt coprocessors
6581da177e4SLinus Torvalds	ldr	r5, [r10, #TI_FLAGS]
6591da177e4SLinus Torvalds	rsbs	r7, r8, #(1 << 8)		@ CP 0 or 1 only
660e44fc388SStefan Agner	movscs	r7, r5, lsr #(TIF_USING_IWMMXT + 1)
6611da177e4SLinus Torvalds	bcs	iwmmxt_task_enable
6621da177e4SLinus Torvalds#endif
663b86040a5SCatalin Marinas ARM(	add	pc, pc, r8, lsr #6	)
664b86040a5SCatalin Marinas THUMB(	lsl	r8, r8, #2		)
665b86040a5SCatalin Marinas THUMB(	add	pc, r8			)
666b86040a5SCatalin Marinas	nop
6671da177e4SLinus Torvalds
6686ebbf2ceSRussell King	ret.w	lr				@ CP#0
669b86040a5SCatalin Marinas	W(b)	do_fpe				@ CP#1 (FPE)
670b86040a5SCatalin Marinas	W(b)	do_fpe				@ CP#2 (FPE)
6716ebbf2ceSRussell King	ret.w	lr				@ CP#3
6726ebbf2ceSRussell King	ret.w	lr				@ CP#4
6736ebbf2ceSRussell King	ret.w	lr				@ CP#5
6746ebbf2ceSRussell King	ret.w	lr				@ CP#6
6756ebbf2ceSRussell King	ret.w	lr				@ CP#7
6766ebbf2ceSRussell King	ret.w	lr				@ CP#8
6776ebbf2ceSRussell King	ret.w	lr				@ CP#9
6781da177e4SLinus Torvalds#ifdef CONFIG_VFP
679b86040a5SCatalin Marinas	W(b)	do_vfp				@ CP#10 (VFP)
680b86040a5SCatalin Marinas	W(b)	do_vfp				@ CP#11 (VFP)
6811da177e4SLinus Torvalds#else
6826ebbf2ceSRussell King	ret.w	lr				@ CP#10 (VFP)
6836ebbf2ceSRussell King	ret.w	lr				@ CP#11 (VFP)
6841da177e4SLinus Torvalds#endif
6856ebbf2ceSRussell King	ret.w	lr				@ CP#12
6866ebbf2ceSRussell King	ret.w	lr				@ CP#13
6876ebbf2ceSRussell King	ret.w	lr				@ CP#14 (Debug)
6886ebbf2ceSRussell King	ret.w	lr				@ CP#15 (Control)
6891da177e4SLinus Torvalds
690ef4c5368SDave Martin#ifdef NEED_CPU_ARCHITECTURE
691ef4c5368SDave Martin	.align	2
692ef4c5368SDave Martin.LCcpu_architecture:
693ef4c5368SDave Martin	.word	__cpu_architecture
694ef4c5368SDave Martin#endif
695ef4c5368SDave Martin
696b5872db4SCatalin Marinas#ifdef CONFIG_NEON
697b5872db4SCatalin Marinas	.align	6
698b5872db4SCatalin Marinas
699cb170a45SPaul Brook.LCneon_arm_opcodes:
700b5872db4SCatalin Marinas	.word	0xfe000000			@ mask
701b5872db4SCatalin Marinas	.word	0xf2000000			@ opcode
702b5872db4SCatalin Marinas
703b5872db4SCatalin Marinas	.word	0xff100000			@ mask
704b5872db4SCatalin Marinas	.word	0xf4000000			@ opcode
705b5872db4SCatalin Marinas
706b5872db4SCatalin Marinas	.word	0x00000000			@ mask
707b5872db4SCatalin Marinas	.word	0x00000000			@ opcode
708cb170a45SPaul Brook
709cb170a45SPaul Brook.LCneon_thumb_opcodes:
710cb170a45SPaul Brook	.word	0xef000000			@ mask
711cb170a45SPaul Brook	.word	0xef000000			@ opcode
712cb170a45SPaul Brook
713cb170a45SPaul Brook	.word	0xff100000			@ mask
714cb170a45SPaul Brook	.word	0xf9000000			@ opcode
715cb170a45SPaul Brook
716cb170a45SPaul Brook	.word	0x00000000			@ mask
717cb170a45SPaul Brook	.word	0x00000000			@ opcode
718b5872db4SCatalin Marinas#endif
719b5872db4SCatalin Marinas
7201da177e4SLinus Torvaldsdo_fpe:
7211da177e4SLinus Torvalds	ldr	r4, .LCfp
7221da177e4SLinus Torvalds	add	r10, r10, #TI_FPSTATE		@ r10 = workspace
7231da177e4SLinus Torvalds	ldr	pc, [r4]			@ Call FP module USR entry point
7241da177e4SLinus Torvalds
7251da177e4SLinus Torvalds/*
7261da177e4SLinus Torvalds * The FP module is called with these registers set:
7271da177e4SLinus Torvalds *  r0  = instruction
7281da177e4SLinus Torvalds *  r2  = PC+4
7291da177e4SLinus Torvalds *  r9  = normal "successful" return address
7301da177e4SLinus Torvalds *  r10 = FP workspace
7311da177e4SLinus Torvalds *  lr  = unrecognised FP instruction return address
7321da177e4SLinus Torvalds */
7331da177e4SLinus Torvalds
734124efc27SSantosh Shilimkar	.pushsection .data
7351abd3502SRussell King	.align	2
7361da177e4SLinus TorvaldsENTRY(fp_enter)
737db6ccbb6SRussell King	.word	no_fp
738124efc27SSantosh Shilimkar	.popsection
7391da177e4SLinus Torvalds
74083e686eaSCatalin MarinasENTRY(no_fp)
7416ebbf2ceSRussell King	ret	lr
74283e686eaSCatalin MarinasENDPROC(no_fp)
743db6ccbb6SRussell King
74415ac49b6SRussell King__und_usr_fault_32:
74515ac49b6SRussell King	mov	r1, #4
74615ac49b6SRussell King	b	1f
7472190fed6SRussell King__und_usr_fault_16_pan:
7482190fed6SRussell King	uaccess_disable ip
74915ac49b6SRussell King__und_usr_fault_16:
75015ac49b6SRussell King	mov	r1, #2
7511417a6b8SCatalin Marinas1:	mov	r0, sp
75214327c66SRussell King	badr	lr, ret_from_exception
75315ac49b6SRussell King	b	__und_fault
75415ac49b6SRussell KingENDPROC(__und_usr_fault_32)
75515ac49b6SRussell KingENDPROC(__und_usr_fault_16)
7561da177e4SLinus Torvalds
7571da177e4SLinus Torvalds	.align	5
7581da177e4SLinus Torvalds__pabt_usr:
759ccea7a19SRussell King	usr_entry
7604fb28474SKirill A. Shutemov	mov	r2, sp				@ regs
7618dfe7ac9SRussell King	pabt_helper
762c4c5716eSCatalin Marinas UNWIND(.fnend		)
7631da177e4SLinus Torvalds	/* fall through */
7641da177e4SLinus Torvalds/*
7651da177e4SLinus Torvalds * This is the return code to user mode for abort handlers
7661da177e4SLinus Torvalds */
7671da177e4SLinus TorvaldsENTRY(ret_from_exception)
768c4c5716eSCatalin Marinas UNWIND(.fnstart	)
769c4c5716eSCatalin Marinas UNWIND(.cantunwind	)
7701da177e4SLinus Torvalds	get_thread_info tsk
7711da177e4SLinus Torvalds	mov	why, #0
7721da177e4SLinus Torvalds	b	ret_to_user
773c4c5716eSCatalin Marinas UNWIND(.fnend		)
77493ed3970SCatalin MarinasENDPROC(__pabt_usr)
77593ed3970SCatalin MarinasENDPROC(ret_from_exception)
7761da177e4SLinus Torvalds
777c0e7f7eeSDaniel Thompson	.align	5
778c0e7f7eeSDaniel Thompson__fiq_usr:
779c0e7f7eeSDaniel Thompson	usr_entry trace=0
780c0e7f7eeSDaniel Thompson	kuser_cmpxchg_check
781c0e7f7eeSDaniel Thompson	mov	r0, sp				@ struct pt_regs *regs
782c0e7f7eeSDaniel Thompson	bl	handle_fiq_as_nmi
783c0e7f7eeSDaniel Thompson	get_thread_info tsk
784c0e7f7eeSDaniel Thompson	restore_user_regs fast = 0, offset = 0
785c0e7f7eeSDaniel Thompson UNWIND(.fnend		)
786c0e7f7eeSDaniel ThompsonENDPROC(__fiq_usr)
787c0e7f7eeSDaniel Thompson
7881da177e4SLinus Torvalds/*
7891da177e4SLinus Torvalds * Register switch for ARMv3 and ARMv4 processors
7901da177e4SLinus Torvalds * r0 = previous task_struct, r1 = previous thread_info, r2 = next thread_info
7911da177e4SLinus Torvalds * previous and next are guaranteed not to be the same.
7921da177e4SLinus Torvalds */
7931da177e4SLinus TorvaldsENTRY(__switch_to)
794c4c5716eSCatalin Marinas UNWIND(.fnstart	)
795c4c5716eSCatalin Marinas UNWIND(.cantunwind	)
7961da177e4SLinus Torvalds	add	ip, r1, #TI_CPU_SAVE
797b86040a5SCatalin Marinas ARM(	stmia	ip!, {r4 - sl, fp, sp, lr} )	@ Store most regs on stack
798b86040a5SCatalin Marinas THUMB(	stmia	ip!, {r4 - sl, fp}	   )	@ Store most regs on stack
799b86040a5SCatalin Marinas THUMB(	str	sp, [ip], #4		   )
800b86040a5SCatalin Marinas THUMB(	str	lr, [ip], #4		   )
801a4780adeSAndré Hentschel	ldr	r4, [r2, #TI_TP_VALUE]
802a4780adeSAndré Hentschel	ldr	r5, [r2, #TI_TP_VALUE + 4]
803247055aaSCatalin Marinas#ifdef CONFIG_CPU_USE_DOMAINS
8041eef5d2fSRussell King	mrc	p15, 0, r6, c3, c0, 0		@ Get domain register
8051eef5d2fSRussell King	str	r6, [r1, #TI_CPU_DOMAIN]	@ Save old domain register
806d6551e88SRussell King	ldr	r6, [r2, #TI_CPU_DOMAIN]
807afeb90caSHyok S. Choi#endif
808a4780adeSAndré Hentschel	switch_tls r1, r4, r5, r3, r7
809*831a469bSArd Biesheuvel#if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_SMP) && \
810*831a469bSArd Biesheuvel    !defined(CONFIG_STACKPROTECTOR_PER_TASK)
811*831a469bSArd Biesheuvel	ldr	r9, [r2, #TI_TASK]
812df0698beSNicolas Pitre	ldr	r8, =__stack_chk_guard
813ffa47aa6SArnd Bergmann	.if (TSK_STACK_CANARY > IMM12_MASK)
814*831a469bSArd Biesheuvel	add	r9, r9, #TSK_STACK_CANARY & ~IMM12_MASK
815ffa47aa6SArnd Bergmann	.endif
816*831a469bSArd Biesheuvel	ldr	r9, [r9, #TSK_STACK_CANARY & IMM12_MASK]
817df0698beSNicolas Pitre#endif
818*831a469bSArd Biesheuvel	mov	r7, r2				@ Preserve 'next'
819247055aaSCatalin Marinas#ifdef CONFIG_CPU_USE_DOMAINS
8201da177e4SLinus Torvalds	mcr	p15, 0, r6, c3, c0, 0		@ Set domain register
821afeb90caSHyok S. Choi#endif
822d6551e88SRussell King	mov	r5, r0
823d6551e88SRussell King	add	r4, r2, #TI_CPU_SAVE
824d6551e88SRussell King	ldr	r0, =thread_notify_head
825d6551e88SRussell King	mov	r1, #THREAD_NOTIFY_SWITCH
826d6551e88SRussell King	bl	atomic_notifier_call_chain
827*831a469bSArd Biesheuvel#if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_SMP) && \
828*831a469bSArd Biesheuvel    !defined(CONFIG_STACKPROTECTOR_PER_TASK)
829*831a469bSArd Biesheuvel	str	r9, [r8]
830df0698beSNicolas Pitre#endif
831d6551e88SRussell King	mov	r0, r5
832a1c510d0SArd Biesheuvel#if !defined(CONFIG_THUMB2_KERNEL) && !defined(CONFIG_VMAP_STACK)
83350596b75SArd Biesheuvel	set_current r7
834b832faecSArd Biesheuvel	ldmia	r4, {r4 - sl, fp, sp, pc}	@ Load all regs saved previously
835b832faecSArd Biesheuvel#else
836b832faecSArd Biesheuvel	mov	r1, r7
837b832faecSArd Biesheuvel	ldmia	r4, {r4 - sl, fp, ip, lr}	@ Load all regs saved previously
838a1c510d0SArd Biesheuvel#ifdef CONFIG_VMAP_STACK
839a1c510d0SArd Biesheuvel	@
840a1c510d0SArd Biesheuvel	@ Do a dummy read from the new stack while running from the old one so
841a1c510d0SArd Biesheuvel	@ that we can rely on do_translation_fault() to fix up any stale PMD
842a1c510d0SArd Biesheuvel	@ entries covering the vmalloc region.
843a1c510d0SArd Biesheuvel	@
844a1c510d0SArd Biesheuvel	ldr	r2, [ip]
845a1c510d0SArd Biesheuvel#endif
846b832faecSArd Biesheuvel
847b832faecSArd Biesheuvel	@ When CONFIG_THREAD_INFO_IN_TASK=n, the update of SP itself is what
848b832faecSArd Biesheuvel	@ effectuates the task switch, as that is what causes the observable
849b832faecSArd Biesheuvel	@ values of current and current_thread_info to change. When
850b832faecSArd Biesheuvel	@ CONFIG_THREAD_INFO_IN_TASK=y, setting current (and therefore
851b832faecSArd Biesheuvel	@ current_thread_info) is done explicitly, and the update of SP just
852b832faecSArd Biesheuvel	@ switches us to another stack, with few other side effects. In order
853b832faecSArd Biesheuvel	@ to prevent this distinction from causing any inconsistencies, let's
854b832faecSArd Biesheuvel	@ keep the 'set_current' call as close as we can to the update of SP.
855b832faecSArd Biesheuvel	set_current r1
856b832faecSArd Biesheuvel	mov	sp, ip
857b832faecSArd Biesheuvel	ret	lr
858b832faecSArd Biesheuvel#endif
859c4c5716eSCatalin Marinas UNWIND(.fnend		)
86093ed3970SCatalin MarinasENDPROC(__switch_to)
8611da177e4SLinus Torvalds
862a1c510d0SArd Biesheuvel#ifdef CONFIG_VMAP_STACK
863a1c510d0SArd Biesheuvel	.text
864a1c510d0SArd Biesheuvel	.align	2
865a1c510d0SArd Biesheuvel__bad_stack:
866a1c510d0SArd Biesheuvel	@
867a1c510d0SArd Biesheuvel	@ We've just detected an overflow. We need to load the address of this
868a1c510d0SArd Biesheuvel	@ CPU's overflow stack into the stack pointer register. We have only one
869a1c510d0SArd Biesheuvel	@ scratch register so let's use a sequence of ADDs including one
870a1c510d0SArd Biesheuvel	@ involving the PC, and decorate them with PC-relative group
871a1c510d0SArd Biesheuvel	@ relocations. As these are ARM only, switch to ARM mode first.
872a1c510d0SArd Biesheuvel	@
873a1c510d0SArd Biesheuvel	@ We enter here with IP clobbered and its value stashed on the mode
874a1c510d0SArd Biesheuvel	@ stack.
875a1c510d0SArd Biesheuvel	@
876a1c510d0SArd BiesheuvelTHUMB(	bx	pc		)
877a1c510d0SArd BiesheuvelTHUMB(	nop			)
878a1c510d0SArd BiesheuvelTHUMB(	.arm			)
879a1c510d0SArd Biesheuvel	mrc	p15, 0, ip, c13, c0, 4		@ Get per-CPU offset
880a1c510d0SArd Biesheuvel
881a1c510d0SArd Biesheuvel	.globl	overflow_stack_ptr
882a1c510d0SArd Biesheuvel	.reloc	0f, R_ARM_ALU_PC_G0_NC, overflow_stack_ptr
883a1c510d0SArd Biesheuvel	.reloc	1f, R_ARM_ALU_PC_G1_NC, overflow_stack_ptr
884a1c510d0SArd Biesheuvel	.reloc	2f, R_ARM_LDR_PC_G2, overflow_stack_ptr
885a1c510d0SArd Biesheuvel	add	ip, ip, pc
886a1c510d0SArd Biesheuvel0:	add	ip, ip, #-4
887a1c510d0SArd Biesheuvel1:	add	ip, ip, #0
888a1c510d0SArd Biesheuvel2:	ldr	ip, [ip, #4]
889a1c510d0SArd Biesheuvel
890a1c510d0SArd Biesheuvel	str	sp, [ip, #-4]!			@ Preserve original SP value
891a1c510d0SArd Biesheuvel	mov	sp, ip				@ Switch to overflow stack
892a1c510d0SArd Biesheuvel	pop	{ip}				@ Original SP in IP
893a1c510d0SArd Biesheuvel
894a1c510d0SArd Biesheuvel#if defined(CONFIG_UNWINDER_FRAME_POINTER) && defined(CONFIG_CC_IS_GCC)
895a1c510d0SArd Biesheuvel	mov	ip, ip				@ mov expected by unwinder
896a1c510d0SArd Biesheuvel	push	{fp, ip, lr, pc}		@ GCC flavor frame record
897a1c510d0SArd Biesheuvel#else
898a1c510d0SArd Biesheuvel	str	ip, [sp, #-8]!			@ store original SP
899a1c510d0SArd Biesheuvel	push	{fpreg, lr}			@ Clang flavor frame record
900a1c510d0SArd Biesheuvel#endif
901a1c510d0SArd BiesheuvelUNWIND( ldr	ip, [r0, #4]	)		@ load exception LR
902a1c510d0SArd BiesheuvelUNWIND( str	ip, [sp, #12]	)		@ store in the frame record
903a1c510d0SArd Biesheuvel	ldr	ip, [r0, #12]			@ reload IP
904a1c510d0SArd Biesheuvel
905a1c510d0SArd Biesheuvel	@ Store the original GPRs to the new stack.
906a1c510d0SArd Biesheuvel	svc_entry uaccess=0, overflow_check=0
907a1c510d0SArd Biesheuvel
908a1c510d0SArd BiesheuvelUNWIND( .save   {sp, pc}	)
909a1c510d0SArd BiesheuvelUNWIND( .save   {fpreg, lr}	)
910a1c510d0SArd BiesheuvelUNWIND( .setfp  fpreg, sp	)
911a1c510d0SArd Biesheuvel
912a1c510d0SArd Biesheuvel	ldr	fpreg, [sp, #S_SP]		@ Add our frame record
913a1c510d0SArd Biesheuvel						@ to the linked list
914a1c510d0SArd Biesheuvel#if defined(CONFIG_UNWINDER_FRAME_POINTER) && defined(CONFIG_CC_IS_GCC)
915a1c510d0SArd Biesheuvel	ldr	r1, [fp, #4]			@ reload SP at entry
916a1c510d0SArd Biesheuvel	add	fp, fp, #12
917a1c510d0SArd Biesheuvel#else
918a1c510d0SArd Biesheuvel	ldr	r1, [fpreg, #8]
919a1c510d0SArd Biesheuvel#endif
920a1c510d0SArd Biesheuvel	str	r1, [sp, #S_SP]			@ store in pt_regs
921a1c510d0SArd Biesheuvel
922a1c510d0SArd Biesheuvel	@ Stash the regs for handle_bad_stack
923a1c510d0SArd Biesheuvel	mov	r0, sp
924a1c510d0SArd Biesheuvel
925a1c510d0SArd Biesheuvel	@ Time to die
926a1c510d0SArd Biesheuvel	bl	handle_bad_stack
927a1c510d0SArd Biesheuvel	nop
928a1c510d0SArd BiesheuvelUNWIND( .fnend			)
929a1c510d0SArd BiesheuvelENDPROC(__bad_stack)
930a1c510d0SArd Biesheuvel#endif
931a1c510d0SArd Biesheuvel
9321da177e4SLinus Torvalds	__INIT
9332d2669b6SNicolas Pitre
9342d2669b6SNicolas Pitre/*
9352d2669b6SNicolas Pitre * User helpers.
9362d2669b6SNicolas Pitre *
9372d2669b6SNicolas Pitre * Each segment is 32-byte aligned and will be moved to the top of the high
9382d2669b6SNicolas Pitre * vector page.  New segments (if ever needed) must be added in front of
9392d2669b6SNicolas Pitre * existing ones.  This mechanism should be used only for things that are
9402d2669b6SNicolas Pitre * really small and justified, and not be abused freely.
9412d2669b6SNicolas Pitre *
942dc7a12bdSMauro Carvalho Chehab * See Documentation/arm/kernel_user_helpers.rst for formal definitions.
9432d2669b6SNicolas Pitre */
944b86040a5SCatalin Marinas THUMB(	.arm	)
9452d2669b6SNicolas Pitre
946ba9b5d76SNicolas Pitre	.macro	usr_ret, reg
947ba9b5d76SNicolas Pitre#ifdef CONFIG_ARM_THUMB
948ba9b5d76SNicolas Pitre	bx	\reg
949ba9b5d76SNicolas Pitre#else
9506ebbf2ceSRussell King	ret	\reg
951ba9b5d76SNicolas Pitre#endif
952ba9b5d76SNicolas Pitre	.endm
953ba9b5d76SNicolas Pitre
9545b43e7a3SRussell King	.macro	kuser_pad, sym, size
9555b43e7a3SRussell King	.if	(. - \sym) & 3
9565b43e7a3SRussell King	.rept	4 - (. - \sym) & 3
9575b43e7a3SRussell King	.byte	0
9585b43e7a3SRussell King	.endr
9595b43e7a3SRussell King	.endif
9605b43e7a3SRussell King	.rept	(\size - (. - \sym)) / 4
9615b43e7a3SRussell King	.word	0xe7fddef1
9625b43e7a3SRussell King	.endr
9635b43e7a3SRussell King	.endm
9645b43e7a3SRussell King
965f6f91b0dSRussell King#ifdef CONFIG_KUSER_HELPERS
9662d2669b6SNicolas Pitre	.align	5
9672d2669b6SNicolas Pitre	.globl	__kuser_helper_start
9682d2669b6SNicolas Pitre__kuser_helper_start:
9692d2669b6SNicolas Pitre
9702d2669b6SNicolas Pitre/*
97140fb79c8SNicolas Pitre * Due to the length of some sequences, __kuser_cmpxchg64 spans 2 regular
97240fb79c8SNicolas Pitre * kuser "slots", therefore 0xffff0f80 is not used as a valid entry point.
9737c612bfdSNicolas Pitre */
9747c612bfdSNicolas Pitre
97540fb79c8SNicolas Pitre__kuser_cmpxchg64:				@ 0xffff0f60
97640fb79c8SNicolas Pitre
977db695c05SRussell King#if defined(CONFIG_CPU_32v6K)
97840fb79c8SNicolas Pitre
97940fb79c8SNicolas Pitre	stmfd	sp!, {r4, r5, r6, r7}
98040fb79c8SNicolas Pitre	ldrd	r4, r5, [r0]			@ load old val
98140fb79c8SNicolas Pitre	ldrd	r6, r7, [r1]			@ load new val
98240fb79c8SNicolas Pitre	smp_dmb	arm
98340fb79c8SNicolas Pitre1:	ldrexd	r0, r1, [r2]			@ load current val
98440fb79c8SNicolas Pitre	eors	r3, r0, r4			@ compare with oldval (1)
985e44fc388SStefan Agner	eorseq	r3, r1, r5			@ compare with oldval (2)
98640fb79c8SNicolas Pitre	strexdeq r3, r6, r7, [r2]		@ store newval if eq
98740fb79c8SNicolas Pitre	teqeq	r3, #1				@ success?
98840fb79c8SNicolas Pitre	beq	1b				@ if no then retry
98940fb79c8SNicolas Pitre	smp_dmb	arm
99040fb79c8SNicolas Pitre	rsbs	r0, r3, #0			@ set returned val and C flag
99140fb79c8SNicolas Pitre	ldmfd	sp!, {r4, r5, r6, r7}
9925a97d0aeSWill Deacon	usr_ret	lr
99340fb79c8SNicolas Pitre
99440fb79c8SNicolas Pitre#elif !defined(CONFIG_SMP)
99540fb79c8SNicolas Pitre
99640fb79c8SNicolas Pitre#ifdef CONFIG_MMU
99740fb79c8SNicolas Pitre
99840fb79c8SNicolas Pitre	/*
99940fb79c8SNicolas Pitre	 * The only thing that can break atomicity in this cmpxchg64
100040fb79c8SNicolas Pitre	 * implementation is either an IRQ or a data abort exception
100140fb79c8SNicolas Pitre	 * causing another process/thread to be scheduled in the middle of
100240fb79c8SNicolas Pitre	 * the critical sequence.  The same strategy as for cmpxchg is used.
100340fb79c8SNicolas Pitre	 */
100440fb79c8SNicolas Pitre	stmfd	sp!, {r4, r5, r6, lr}
100540fb79c8SNicolas Pitre	ldmia	r0, {r4, r5}			@ load old val
100640fb79c8SNicolas Pitre	ldmia	r1, {r6, lr}			@ load new val
100740fb79c8SNicolas Pitre1:	ldmia	r2, {r0, r1}			@ load current val
100840fb79c8SNicolas Pitre	eors	r3, r0, r4			@ compare with oldval (1)
1009e44fc388SStefan Agner	eorseq	r3, r1, r5			@ compare with oldval (2)
1010e44fc388SStefan Agner2:	stmiaeq	r2, {r6, lr}			@ store newval if eq
101140fb79c8SNicolas Pitre	rsbs	r0, r3, #0			@ set return val and C flag
101240fb79c8SNicolas Pitre	ldmfd	sp!, {r4, r5, r6, pc}
101340fb79c8SNicolas Pitre
101440fb79c8SNicolas Pitre	.text
101540fb79c8SNicolas Pitrekuser_cmpxchg64_fixup:
101640fb79c8SNicolas Pitre	@ Called from kuser_cmpxchg_fixup.
10173ad55155SRussell King	@ r4 = address of interrupted insn (must be preserved).
101840fb79c8SNicolas Pitre	@ sp = saved regs. r7 and r8 are clobbered.
101940fb79c8SNicolas Pitre	@ 1b = first critical insn, 2b = last critical insn.
10203ad55155SRussell King	@ If r4 >= 1b and r4 <= 2b then saved pc_usr is set to 1b.
102140fb79c8SNicolas Pitre	mov	r7, #0xffff0fff
102240fb79c8SNicolas Pitre	sub	r7, r7, #(0xffff0fff - (0xffff0f60 + (1b - __kuser_cmpxchg64)))
10233ad55155SRussell King	subs	r8, r4, r7
1024e44fc388SStefan Agner	rsbscs	r8, r8, #(2b - 1b)
102540fb79c8SNicolas Pitre	strcs	r7, [sp, #S_PC]
102640fb79c8SNicolas Pitre#if __LINUX_ARM_ARCH__ < 6
102740fb79c8SNicolas Pitre	bcc	kuser_cmpxchg32_fixup
102840fb79c8SNicolas Pitre#endif
10296ebbf2ceSRussell King	ret	lr
103040fb79c8SNicolas Pitre	.previous
103140fb79c8SNicolas Pitre
103240fb79c8SNicolas Pitre#else
103340fb79c8SNicolas Pitre#warning "NPTL on non MMU needs fixing"
103440fb79c8SNicolas Pitre	mov	r0, #-1
103540fb79c8SNicolas Pitre	adds	r0, r0, #0
103640fb79c8SNicolas Pitre	usr_ret	lr
103740fb79c8SNicolas Pitre#endif
103840fb79c8SNicolas Pitre
103940fb79c8SNicolas Pitre#else
104040fb79c8SNicolas Pitre#error "incoherent kernel configuration"
104140fb79c8SNicolas Pitre#endif
104240fb79c8SNicolas Pitre
10435b43e7a3SRussell King	kuser_pad __kuser_cmpxchg64, 64
104440fb79c8SNicolas Pitre
10457c612bfdSNicolas Pitre__kuser_memory_barrier:				@ 0xffff0fa0
1046ed3768a8SDave Martin	smp_dmb	arm
1047ba9b5d76SNicolas Pitre	usr_ret	lr
10487c612bfdSNicolas Pitre
10495b43e7a3SRussell King	kuser_pad __kuser_memory_barrier, 32
10507c612bfdSNicolas Pitre
10512d2669b6SNicolas Pitre__kuser_cmpxchg:				@ 0xffff0fc0
10522d2669b6SNicolas Pitre
1053db695c05SRussell King#if __LINUX_ARM_ARCH__ < 6
10542d2669b6SNicolas Pitre
105549bca4c2SNicolas Pitre#ifdef CONFIG_MMU
1056b49c0f24SNicolas Pitre
1057b49c0f24SNicolas Pitre	/*
1058b49c0f24SNicolas Pitre	 * The only thing that can break atomicity in this cmpxchg
1059b49c0f24SNicolas Pitre	 * implementation is either an IRQ or a data abort exception
1060b49c0f24SNicolas Pitre	 * causing another process/thread to be scheduled in the middle
1061b49c0f24SNicolas Pitre	 * of the critical sequence.  To prevent this, code is added to
1062b49c0f24SNicolas Pitre	 * the IRQ and data abort exception handlers to set the pc back
1063b49c0f24SNicolas Pitre	 * to the beginning of the critical section if it is found to be
1064b49c0f24SNicolas Pitre	 * within that critical section (see kuser_cmpxchg_fixup).
1065b49c0f24SNicolas Pitre	 */
1066b49c0f24SNicolas Pitre1:	ldr	r3, [r2]			@ load current val
1067b49c0f24SNicolas Pitre	subs	r3, r3, r0			@ compare with oldval
1068b49c0f24SNicolas Pitre2:	streq	r1, [r2]			@ store newval if eq
1069b49c0f24SNicolas Pitre	rsbs	r0, r3, #0			@ set return val and C flag
1070b49c0f24SNicolas Pitre	usr_ret	lr
1071b49c0f24SNicolas Pitre
1072b49c0f24SNicolas Pitre	.text
107340fb79c8SNicolas Pitrekuser_cmpxchg32_fixup:
1074b49c0f24SNicolas Pitre	@ Called from kuser_cmpxchg_check macro.
1075b059bdc3SRussell King	@ r4 = address of interrupted insn (must be preserved).
1076b49c0f24SNicolas Pitre	@ sp = saved regs. r7 and r8 are clobbered.
1077b49c0f24SNicolas Pitre	@ 1b = first critical insn, 2b = last critical insn.
1078b059bdc3SRussell King	@ If r4 >= 1b and r4 <= 2b then saved pc_usr is set to 1b.
1079b49c0f24SNicolas Pitre	mov	r7, #0xffff0fff
1080b49c0f24SNicolas Pitre	sub	r7, r7, #(0xffff0fff - (0xffff0fc0 + (1b - __kuser_cmpxchg)))
1081b059bdc3SRussell King	subs	r8, r4, r7
1082e44fc388SStefan Agner	rsbscs	r8, r8, #(2b - 1b)
1083b49c0f24SNicolas Pitre	strcs	r7, [sp, #S_PC]
10846ebbf2ceSRussell King	ret	lr
1085b49c0f24SNicolas Pitre	.previous
1086b49c0f24SNicolas Pitre
108749bca4c2SNicolas Pitre#else
108849bca4c2SNicolas Pitre#warning "NPTL on non MMU needs fixing"
108949bca4c2SNicolas Pitre	mov	r0, #-1
109049bca4c2SNicolas Pitre	adds	r0, r0, #0
1091ba9b5d76SNicolas Pitre	usr_ret	lr
1092b49c0f24SNicolas Pitre#endif
10932d2669b6SNicolas Pitre
10942d2669b6SNicolas Pitre#else
10952d2669b6SNicolas Pitre
1096ed3768a8SDave Martin	smp_dmb	arm
1097b49c0f24SNicolas Pitre1:	ldrex	r3, [r2]
10982d2669b6SNicolas Pitre	subs	r3, r3, r0
10992d2669b6SNicolas Pitre	strexeq	r3, r1, [r2]
1100b49c0f24SNicolas Pitre	teqeq	r3, #1
1101b49c0f24SNicolas Pitre	beq	1b
11022d2669b6SNicolas Pitre	rsbs	r0, r3, #0
1103b49c0f24SNicolas Pitre	/* beware -- each __kuser slot must be 8 instructions max */
1104f00ec48fSRussell King	ALT_SMP(b	__kuser_memory_barrier)
1105f00ec48fSRussell King	ALT_UP(usr_ret	lr)
11062d2669b6SNicolas Pitre
11072d2669b6SNicolas Pitre#endif
11082d2669b6SNicolas Pitre
11095b43e7a3SRussell King	kuser_pad __kuser_cmpxchg, 32
11102d2669b6SNicolas Pitre
11112d2669b6SNicolas Pitre__kuser_get_tls:				@ 0xffff0fe0
1112f159f4edSTony Lindgren	ldr	r0, [pc, #(16 - 8)]	@ read TLS, set in kuser_get_tls_init
1113ba9b5d76SNicolas Pitre	usr_ret	lr
1114f159f4edSTony Lindgren	mrc	p15, 0, r0, c13, c0, 3	@ 0xffff0fe8 hardware TLS code
11155b43e7a3SRussell King	kuser_pad __kuser_get_tls, 16
11165b43e7a3SRussell King	.rep	3
1117f159f4edSTony Lindgren	.word	0			@ 0xffff0ff0 software TLS value, then
1118f159f4edSTony Lindgren	.endr				@ pad up to __kuser_helper_version
11192d2669b6SNicolas Pitre
11202d2669b6SNicolas Pitre__kuser_helper_version:				@ 0xffff0ffc
11212d2669b6SNicolas Pitre	.word	((__kuser_helper_end - __kuser_helper_start) >> 5)
11222d2669b6SNicolas Pitre
11232d2669b6SNicolas Pitre	.globl	__kuser_helper_end
11242d2669b6SNicolas Pitre__kuser_helper_end:
11252d2669b6SNicolas Pitre
1126f6f91b0dSRussell King#endif
1127f6f91b0dSRussell King
1128b86040a5SCatalin Marinas THUMB(	.thumb	)
11292d2669b6SNicolas Pitre
11301da177e4SLinus Torvalds/*
11311da177e4SLinus Torvalds * Vector stubs.
11321da177e4SLinus Torvalds *
113319accfd3SRussell King * This code is copied to 0xffff1000 so we can use branches in the
113419accfd3SRussell King * vectors, rather than ldr's.  Note that this code must not exceed
113519accfd3SRussell King * a page size.
11361da177e4SLinus Torvalds *
11371da177e4SLinus Torvalds * Common stub entry macro:
11381da177e4SLinus Torvalds *   Enter in IRQ mode, spsr = SVC/USR CPSR, lr = SVC/USR PC
1139ccea7a19SRussell King *
1140ccea7a19SRussell King * SP points to a minimal amount of processor-private memory, the address
1141ccea7a19SRussell King * of which is copied into r0 for the mode specific abort handler.
11421da177e4SLinus Torvalds */
1143b7ec4795SNicolas Pitre	.macro	vector_stub, name, mode, correction=0
11441da177e4SLinus Torvalds	.align	5
11451da177e4SLinus Torvalds
11461da177e4SLinus Torvaldsvector_\name:
11471da177e4SLinus Torvalds	.if \correction
11481da177e4SLinus Torvalds	sub	lr, lr, #\correction
11491da177e4SLinus Torvalds	.endif
11501da177e4SLinus Torvalds
1151ccea7a19SRussell King	@
1152ccea7a19SRussell King	@ Save r0, lr_<exception> (parent PC) and spsr_<exception>
1153ccea7a19SRussell King	@ (parent CPSR)
1154ccea7a19SRussell King	@
1155ccea7a19SRussell King	stmia	sp, {r0, lr}		@ save r0, lr
1156ccea7a19SRussell King	mrs	lr, spsr
1157ccea7a19SRussell King	str	lr, [sp, #8]		@ save spsr
1158ccea7a19SRussell King
1159ccea7a19SRussell King	@
1160ccea7a19SRussell King	@ Prepare for SVC32 mode.  IRQs remain disabled.
1161ccea7a19SRussell King	@
1162ccea7a19SRussell King	mrs	r0, cpsr
1163b86040a5SCatalin Marinas	eor	r0, r0, #(\mode ^ SVC_MODE | PSR_ISETSTATE)
1164ccea7a19SRussell King	msr	spsr_cxsf, r0
1165ccea7a19SRussell King
1166ccea7a19SRussell King	@
1167ccea7a19SRussell King	@ the branch table must immediately follow this code
1168ccea7a19SRussell King	@
1169ccea7a19SRussell King	and	lr, lr, #0x0f
1170b86040a5SCatalin Marinas THUMB(	adr	r0, 1f			)
1171b86040a5SCatalin Marinas THUMB(	ldr	lr, [r0, lr, lsl #2]	)
1172b7ec4795SNicolas Pitre	mov	r0, sp
1173b86040a5SCatalin Marinas ARM(	ldr	lr, [pc, lr, lsl #2]	)
1174ccea7a19SRussell King	movs	pc, lr			@ branch to handler in SVC mode
117593ed3970SCatalin MarinasENDPROC(vector_\name)
117688987ef9SCatalin Marinas
117788987ef9SCatalin Marinas	.align	2
117888987ef9SCatalin Marinas	@ handler addresses follow this label
117988987ef9SCatalin Marinas1:
11801da177e4SLinus Torvalds	.endm
11811da177e4SLinus Torvalds
1182b9b32bf7SRussell King	.section .stubs, "ax", %progbits
118319accfd3SRussell King	@ This must be the first word
118419accfd3SRussell King	.word	vector_swi
118519accfd3SRussell King
118619accfd3SRussell Kingvector_rst:
118719accfd3SRussell King ARM(	swi	SYS_ERROR0	)
118819accfd3SRussell King THUMB(	svc	#0		)
118919accfd3SRussell King THUMB(	nop			)
119019accfd3SRussell King	b	vector_und
119119accfd3SRussell King
11921da177e4SLinus Torvalds/*
11931da177e4SLinus Torvalds * Interrupt dispatcher
11941da177e4SLinus Torvalds */
1195b7ec4795SNicolas Pitre	vector_stub	irq, IRQ_MODE, 4
11961da177e4SLinus Torvalds
11971da177e4SLinus Torvalds	.long	__irq_usr			@  0  (USR_26 / USR_32)
11981da177e4SLinus Torvalds	.long	__irq_invalid			@  1  (FIQ_26 / FIQ_32)
11991da177e4SLinus Torvalds	.long	__irq_invalid			@  2  (IRQ_26 / IRQ_32)
12001da177e4SLinus Torvalds	.long	__irq_svc			@  3  (SVC_26 / SVC_32)
12011da177e4SLinus Torvalds	.long	__irq_invalid			@  4
12021da177e4SLinus Torvalds	.long	__irq_invalid			@  5
12031da177e4SLinus Torvalds	.long	__irq_invalid			@  6
12041da177e4SLinus Torvalds	.long	__irq_invalid			@  7
12051da177e4SLinus Torvalds	.long	__irq_invalid			@  8
12061da177e4SLinus Torvalds	.long	__irq_invalid			@  9
12071da177e4SLinus Torvalds	.long	__irq_invalid			@  a
12081da177e4SLinus Torvalds	.long	__irq_invalid			@  b
12091da177e4SLinus Torvalds	.long	__irq_invalid			@  c
12101da177e4SLinus Torvalds	.long	__irq_invalid			@  d
12111da177e4SLinus Torvalds	.long	__irq_invalid			@  e
12121da177e4SLinus Torvalds	.long	__irq_invalid			@  f
12131da177e4SLinus Torvalds
12141da177e4SLinus Torvalds/*
12151da177e4SLinus Torvalds * Data abort dispatcher
12161da177e4SLinus Torvalds * Enter in ABT mode, spsr = USR CPSR, lr = USR PC
12171da177e4SLinus Torvalds */
1218b7ec4795SNicolas Pitre	vector_stub	dabt, ABT_MODE, 8
12191da177e4SLinus Torvalds
12201da177e4SLinus Torvalds	.long	__dabt_usr			@  0  (USR_26 / USR_32)
12211da177e4SLinus Torvalds	.long	__dabt_invalid			@  1  (FIQ_26 / FIQ_32)
12221da177e4SLinus Torvalds	.long	__dabt_invalid			@  2  (IRQ_26 / IRQ_32)
12231da177e4SLinus Torvalds	.long	__dabt_svc			@  3  (SVC_26 / SVC_32)
12241da177e4SLinus Torvalds	.long	__dabt_invalid			@  4
12251da177e4SLinus Torvalds	.long	__dabt_invalid			@  5
12261da177e4SLinus Torvalds	.long	__dabt_invalid			@  6
12271da177e4SLinus Torvalds	.long	__dabt_invalid			@  7
12281da177e4SLinus Torvalds	.long	__dabt_invalid			@  8
12291da177e4SLinus Torvalds	.long	__dabt_invalid			@  9
12301da177e4SLinus Torvalds	.long	__dabt_invalid			@  a
12311da177e4SLinus Torvalds	.long	__dabt_invalid			@  b
12321da177e4SLinus Torvalds	.long	__dabt_invalid			@  c
12331da177e4SLinus Torvalds	.long	__dabt_invalid			@  d
12341da177e4SLinus Torvalds	.long	__dabt_invalid			@  e
12351da177e4SLinus Torvalds	.long	__dabt_invalid			@  f
12361da177e4SLinus Torvalds
12371da177e4SLinus Torvalds/*
12381da177e4SLinus Torvalds * Prefetch abort dispatcher
12391da177e4SLinus Torvalds * Enter in ABT mode, spsr = USR CPSR, lr = USR PC
12401da177e4SLinus Torvalds */
1241b7ec4795SNicolas Pitre	vector_stub	pabt, ABT_MODE, 4
12421da177e4SLinus Torvalds
12431da177e4SLinus Torvalds	.long	__pabt_usr			@  0 (USR_26 / USR_32)
12441da177e4SLinus Torvalds	.long	__pabt_invalid			@  1 (FIQ_26 / FIQ_32)
12451da177e4SLinus Torvalds	.long	__pabt_invalid			@  2 (IRQ_26 / IRQ_32)
12461da177e4SLinus Torvalds	.long	__pabt_svc			@  3 (SVC_26 / SVC_32)
12471da177e4SLinus Torvalds	.long	__pabt_invalid			@  4
12481da177e4SLinus Torvalds	.long	__pabt_invalid			@  5
12491da177e4SLinus Torvalds	.long	__pabt_invalid			@  6
12501da177e4SLinus Torvalds	.long	__pabt_invalid			@  7
12511da177e4SLinus Torvalds	.long	__pabt_invalid			@  8
12521da177e4SLinus Torvalds	.long	__pabt_invalid			@  9
12531da177e4SLinus Torvalds	.long	__pabt_invalid			@  a
12541da177e4SLinus Torvalds	.long	__pabt_invalid			@  b
12551da177e4SLinus Torvalds	.long	__pabt_invalid			@  c
12561da177e4SLinus Torvalds	.long	__pabt_invalid			@  d
12571da177e4SLinus Torvalds	.long	__pabt_invalid			@  e
12581da177e4SLinus Torvalds	.long	__pabt_invalid			@  f
12591da177e4SLinus Torvalds
12601da177e4SLinus Torvalds/*
12611da177e4SLinus Torvalds * Undef instr entry dispatcher
12621da177e4SLinus Torvalds * Enter in UND mode, spsr = SVC/USR CPSR, lr = SVC/USR PC
12631da177e4SLinus Torvalds */
1264b7ec4795SNicolas Pitre	vector_stub	und, UND_MODE
12651da177e4SLinus Torvalds
12661da177e4SLinus Torvalds	.long	__und_usr			@  0 (USR_26 / USR_32)
12671da177e4SLinus Torvalds	.long	__und_invalid			@  1 (FIQ_26 / FIQ_32)
12681da177e4SLinus Torvalds	.long	__und_invalid			@  2 (IRQ_26 / IRQ_32)
12691da177e4SLinus Torvalds	.long	__und_svc			@  3 (SVC_26 / SVC_32)
12701da177e4SLinus Torvalds	.long	__und_invalid			@  4
12711da177e4SLinus Torvalds	.long	__und_invalid			@  5
12721da177e4SLinus Torvalds	.long	__und_invalid			@  6
12731da177e4SLinus Torvalds	.long	__und_invalid			@  7
12741da177e4SLinus Torvalds	.long	__und_invalid			@  8
12751da177e4SLinus Torvalds	.long	__und_invalid			@  9
12761da177e4SLinus Torvalds	.long	__und_invalid			@  a
12771da177e4SLinus Torvalds	.long	__und_invalid			@  b
12781da177e4SLinus Torvalds	.long	__und_invalid			@  c
12791da177e4SLinus Torvalds	.long	__und_invalid			@  d
12801da177e4SLinus Torvalds	.long	__und_invalid			@  e
12811da177e4SLinus Torvalds	.long	__und_invalid			@  f
12821da177e4SLinus Torvalds
12831da177e4SLinus Torvalds	.align	5
12841da177e4SLinus Torvalds
12851da177e4SLinus Torvalds/*=============================================================================
128619accfd3SRussell King * Address exception handler
128719accfd3SRussell King *-----------------------------------------------------------------------------
128819accfd3SRussell King * These aren't too critical.
128919accfd3SRussell King * (they're not supposed to happen, and won't happen in 32-bit data mode).
129019accfd3SRussell King */
129119accfd3SRussell King
129219accfd3SRussell Kingvector_addrexcptn:
129319accfd3SRussell King	b	vector_addrexcptn
129419accfd3SRussell King
129519accfd3SRussell King/*=============================================================================
1296c0e7f7eeSDaniel Thompson * FIQ "NMI" handler
12971da177e4SLinus Torvalds *-----------------------------------------------------------------------------
1298c0e7f7eeSDaniel Thompson * Handle a FIQ using the SVC stack allowing FIQ act like NMI on x86
1299c0e7f7eeSDaniel Thompson * systems.
13001da177e4SLinus Torvalds */
1301c0e7f7eeSDaniel Thompson	vector_stub	fiq, FIQ_MODE, 4
1302c0e7f7eeSDaniel Thompson
1303c0e7f7eeSDaniel Thompson	.long	__fiq_usr			@  0  (USR_26 / USR_32)
1304c0e7f7eeSDaniel Thompson	.long	__fiq_svc			@  1  (FIQ_26 / FIQ_32)
1305c0e7f7eeSDaniel Thompson	.long	__fiq_svc			@  2  (IRQ_26 / IRQ_32)
1306c0e7f7eeSDaniel Thompson	.long	__fiq_svc			@  3  (SVC_26 / SVC_32)
1307c0e7f7eeSDaniel Thompson	.long	__fiq_svc			@  4
1308c0e7f7eeSDaniel Thompson	.long	__fiq_svc			@  5
1309c0e7f7eeSDaniel Thompson	.long	__fiq_svc			@  6
1310c0e7f7eeSDaniel Thompson	.long	__fiq_abt			@  7
1311c0e7f7eeSDaniel Thompson	.long	__fiq_svc			@  8
1312c0e7f7eeSDaniel Thompson	.long	__fiq_svc			@  9
1313c0e7f7eeSDaniel Thompson	.long	__fiq_svc			@  a
1314c0e7f7eeSDaniel Thompson	.long	__fiq_svc			@  b
1315c0e7f7eeSDaniel Thompson	.long	__fiq_svc			@  c
1316c0e7f7eeSDaniel Thompson	.long	__fiq_svc			@  d
1317c0e7f7eeSDaniel Thompson	.long	__fiq_svc			@  e
1318c0e7f7eeSDaniel Thompson	.long	__fiq_svc			@  f
13191da177e4SLinus Torvalds
132031b96caeSArd Biesheuvel	.globl	vector_fiq
1321e39e3f3eSRussell King
1322b9b32bf7SRussell King	.section .vectors, "ax", %progbits
1323b48da558SArd Biesheuvel.L__vectors_start:
1324b9b32bf7SRussell King	W(b)	vector_rst
1325b9b32bf7SRussell King	W(b)	vector_und
1326b48da558SArd Biesheuvel	W(ldr)	pc, .L__vectors_start + 0x1000
1327b9b32bf7SRussell King	W(b)	vector_pabt
1328b9b32bf7SRussell King	W(b)	vector_dabt
1329b9b32bf7SRussell King	W(b)	vector_addrexcptn
1330b9b32bf7SRussell King	W(b)	vector_irq
1331b9b32bf7SRussell King	W(b)	vector_fiq
13321da177e4SLinus Torvalds
13331da177e4SLinus Torvalds	.data
13341abd3502SRussell King	.align	2
13351da177e4SLinus Torvalds
13361da177e4SLinus Torvalds	.globl	cr_alignment
13371da177e4SLinus Torvaldscr_alignment:
13381da177e4SLinus Torvalds	.space	4
1339