xref: /openbmc/linux/arch/arm/kernel/entry-armv.S (revision a1c510d0adc604bb143c86052bc5be48cbcfa17c)
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>
224c301f9bSPalmer Dabbelt#ifndef CONFIG_GENERIC_IRQ_MULTI_HANDLER
23a09e64fbSRussell King#include <mach/entry-macro.S>
24243c8654SRob Herring#endif
25d6551e88SRussell King#include <asm/thread_notify.h>
26c4c5716eSCatalin Marinas#include <asm/unwind.h>
27cc20d429SRussell King#include <asm/unistd.h>
28f159f4edSTony Lindgren#include <asm/tls.h>
299f97da78SDavid Howells#include <asm/system_info.h>
30747ffc2fSRussell King#include <asm/uaccess-asm.h>
311da177e4SLinus Torvalds
321da177e4SLinus Torvalds#include "entry-header.S"
33cd544ce7SMagnus Damm#include <asm/entry-macro-multi.S>
34a0266c21SWang Nan#include <asm/probes.h>
351da177e4SLinus Torvalds
361da177e4SLinus Torvalds/*
37d9600c99SRussell King * Interrupt handling.
38187a51adSRussell King */
39d4664b6cSArd Biesheuvel	.macro	irq_handler, from_user:req
404c301f9bSPalmer Dabbelt#ifdef CONFIG_GENERIC_IRQ_MULTI_HANDLER
4152108641Seric miao	mov	r0, sp
42d4664b6cSArd Biesheuvel#ifdef CONFIG_IRQSTACKS
43d4664b6cSArd Biesheuvel	mov_l	r2, irq_stack_ptr	@ Take base address
44d4664b6cSArd Biesheuvel	mrc	p15, 0, r3, c13, c0, 4	@ Get CPU offset
45d4664b6cSArd Biesheuvel#ifdef CONFIG_UNWINDER_ARM
46d4664b6cSArd Biesheuvel	mov	fpreg, sp		@ Preserve original SP
47d4664b6cSArd Biesheuvel#else
48d4664b6cSArd Biesheuvel	mov	r8, fp			@ Preserve original FP
49d4664b6cSArd Biesheuvel	mov	r9, sp			@ Preserve original SP
50d4664b6cSArd Biesheuvel#endif
51d4664b6cSArd Biesheuvel	ldr	sp, [r2, r3]		@ Load SP from per-CPU var
52d4664b6cSArd Biesheuvel	.if	\from_user == 0
53d4664b6cSArd BiesheuvelUNWIND(	.setfp	fpreg, sp		)
54d4664b6cSArd Biesheuvel	@
55d4664b6cSArd Biesheuvel	@ If we took the interrupt while running in the kernel, we may already
56d4664b6cSArd Biesheuvel	@ be using the IRQ stack, so revert to the original value in that case.
57d4664b6cSArd Biesheuvel	@
58d4664b6cSArd Biesheuvel	subs	r2, sp, r0		@ SP above bottom of IRQ stack?
59d4664b6cSArd Biesheuvel	rsbscs	r2, r2, #THREAD_SIZE	@ ... and below the top?
60*a1c510d0SArd Biesheuvel#ifdef CONFIG_VMAP_STACK
61*a1c510d0SArd Biesheuvel	ldr_l	r2, high_memory, cc	@ End of the linear region
62*a1c510d0SArd Biesheuvel	cmpcc	r2, r0			@ Stack pointer was below it?
63*a1c510d0SArd Biesheuvel#endif
64d4664b6cSArd Biesheuvel	movcs	sp, r0			@ If so, revert to incoming SP
65d4664b6cSArd Biesheuvel
66d4664b6cSArd Biesheuvel#ifndef CONFIG_UNWINDER_ARM
67d4664b6cSArd Biesheuvel	@
68d4664b6cSArd Biesheuvel	@ Inform the frame pointer unwinder where the next frame lives
69d4664b6cSArd Biesheuvel	@
70d4664b6cSArd Biesheuvel	movcc	lr, pc			@ Make LR point into .entry.text so
71d4664b6cSArd Biesheuvel					@ that we will get a dump of the
72d4664b6cSArd Biesheuvel					@ exception stack for this frame.
73d4664b6cSArd Biesheuvel#ifdef CONFIG_CC_IS_GCC
74d4664b6cSArd Biesheuvel	movcc	ip, r0			@ Store the old SP in the frame record.
75d4664b6cSArd Biesheuvel	stmdbcc	sp!, {fp, ip, lr, pc}	@ Push frame record
76d4664b6cSArd Biesheuvel	addcc	fp, sp, #12
77d4664b6cSArd Biesheuvel#else
78d4664b6cSArd Biesheuvel	stmdbcc	sp!, {fp, lr}		@ Push frame record
79d4664b6cSArd Biesheuvel	movcc	fp, sp
80d4664b6cSArd Biesheuvel#endif // CONFIG_CC_IS_GCC
81d4664b6cSArd Biesheuvel#endif // CONFIG_UNWINDER_ARM
82d4664b6cSArd Biesheuvel	.endif
83d4664b6cSArd Biesheuvel#endif // CONFIG_IRQSTACKS
84d4664b6cSArd Biesheuvel
85a7b0872eSMark Rutland	bl	generic_handle_arch_irq
86d4664b6cSArd Biesheuvel
87d4664b6cSArd Biesheuvel#ifdef CONFIG_IRQSTACKS
88d4664b6cSArd Biesheuvel#ifdef CONFIG_UNWINDER_ARM
89d4664b6cSArd Biesheuvel	mov	sp, fpreg		@ Restore original SP
90d4664b6cSArd Biesheuvel#else
91d4664b6cSArd Biesheuvel	mov	fp, r8			@ Restore original FP
92d4664b6cSArd Biesheuvel	mov	sp, r9			@ Restore original SP
93d4664b6cSArd Biesheuvel#endif // CONFIG_UNWINDER_ARM
94d4664b6cSArd Biesheuvel#endif // CONFIG_IRQSTACKS
95abeb24aeSMarc Zyngier#else
96cd544ce7SMagnus Damm	arch_irq_handler_default
97abeb24aeSMarc Zyngier#endif
98187a51adSRussell King	.endm
99187a51adSRussell King
100ac8b9c1cSRussell King	.macro	pabt_helper
1018dfe7ac9SRussell King	@ PABORT handler takes pt_regs in r2, fault address in r4 and psr in r5
102ac8b9c1cSRussell King#ifdef MULTI_PABORT
1030402beceSRussell King	ldr	ip, .LCprocfns
104ac8b9c1cSRussell King	mov	lr, pc
1050402beceSRussell King	ldr	pc, [ip, #PROCESSOR_PABT_FUNC]
106ac8b9c1cSRussell King#else
107ac8b9c1cSRussell King	bl	CPU_PABORT_HANDLER
108ac8b9c1cSRussell King#endif
109ac8b9c1cSRussell King	.endm
110ac8b9c1cSRussell King
111ac8b9c1cSRussell King	.macro	dabt_helper
112ac8b9c1cSRussell King
113ac8b9c1cSRussell King	@
114ac8b9c1cSRussell King	@ Call the processor-specific abort handler:
115ac8b9c1cSRussell King	@
116da740472SRussell King	@  r2 - pt_regs
1173e287becSRussell King	@  r4 - aborted context pc
1183e287becSRussell King	@  r5 - aborted context psr
119ac8b9c1cSRussell King	@
120ac8b9c1cSRussell King	@ The abort handler must return the aborted address in r0, and
121ac8b9c1cSRussell King	@ the fault status register in r1.  r9 must be preserved.
122ac8b9c1cSRussell King	@
123ac8b9c1cSRussell King#ifdef MULTI_DABORT
1240402beceSRussell King	ldr	ip, .LCprocfns
125ac8b9c1cSRussell King	mov	lr, pc
1260402beceSRussell King	ldr	pc, [ip, #PROCESSOR_DABT_FUNC]
127ac8b9c1cSRussell King#else
128ac8b9c1cSRussell King	bl	CPU_DABORT_HANDLER
129ac8b9c1cSRussell King#endif
130ac8b9c1cSRussell King	.endm
131ac8b9c1cSRussell King
132c6089061SRussell King	.section	.entry.text,"ax",%progbits
133785d3cd2SNicolas Pitre
134187a51adSRussell King/*
1351da177e4SLinus Torvalds * Invalid mode handlers
1361da177e4SLinus Torvalds */
137ccea7a19SRussell King	.macro	inv_entry, reason
1385745eef6SRussell King	sub	sp, sp, #PT_REGS_SIZE
139b86040a5SCatalin Marinas ARM(	stmib	sp, {r1 - lr}		)
140b86040a5SCatalin Marinas THUMB(	stmia	sp, {r0 - r12}		)
141b86040a5SCatalin Marinas THUMB(	str	sp, [sp, #S_SP]		)
142b86040a5SCatalin Marinas THUMB(	str	lr, [sp, #S_LR]		)
1431da177e4SLinus Torvalds	mov	r1, #\reason
1441da177e4SLinus Torvalds	.endm
1451da177e4SLinus Torvalds
1461da177e4SLinus Torvalds__pabt_invalid:
147ccea7a19SRussell King	inv_entry BAD_PREFETCH
148ccea7a19SRussell King	b	common_invalid
14993ed3970SCatalin MarinasENDPROC(__pabt_invalid)
1501da177e4SLinus Torvalds
1511da177e4SLinus Torvalds__dabt_invalid:
152ccea7a19SRussell King	inv_entry BAD_DATA
153ccea7a19SRussell King	b	common_invalid
15493ed3970SCatalin MarinasENDPROC(__dabt_invalid)
1551da177e4SLinus Torvalds
1561da177e4SLinus Torvalds__irq_invalid:
157ccea7a19SRussell King	inv_entry BAD_IRQ
158ccea7a19SRussell King	b	common_invalid
15993ed3970SCatalin MarinasENDPROC(__irq_invalid)
1601da177e4SLinus Torvalds
1611da177e4SLinus Torvalds__und_invalid:
162ccea7a19SRussell King	inv_entry BAD_UNDEFINSTR
1631da177e4SLinus Torvalds
164ccea7a19SRussell King	@
165ccea7a19SRussell King	@ XXX fall through to common_invalid
166ccea7a19SRussell King	@
167ccea7a19SRussell King
168ccea7a19SRussell King@
169ccea7a19SRussell King@ common_invalid - generic code for failed exception (re-entrant version of handlers)
170ccea7a19SRussell King@
171ccea7a19SRussell Kingcommon_invalid:
172ccea7a19SRussell King	zero_fp
173ccea7a19SRussell King
174ccea7a19SRussell King	ldmia	r0, {r4 - r6}
175ccea7a19SRussell King	add	r0, sp, #S_PC		@ here for interlock avoidance
176ccea7a19SRussell King	mov	r7, #-1			@  ""   ""    ""        ""
177ccea7a19SRussell King	str	r4, [sp]		@ save preserved r0
178ccea7a19SRussell King	stmia	r0, {r5 - r7}		@ lr_<exception>,
179ccea7a19SRussell King					@ cpsr_<exception>, "old_r0"
180ccea7a19SRussell King
1811da177e4SLinus Torvalds	mov	r0, sp
1821da177e4SLinus Torvalds	b	bad_mode
18393ed3970SCatalin MarinasENDPROC(__und_invalid)
1841da177e4SLinus Torvalds
1851da177e4SLinus Torvalds/*
1861da177e4SLinus Torvalds * SVC mode handlers
1871da177e4SLinus Torvalds */
1882dede2d8SNicolas Pitre
1892dede2d8SNicolas Pitre#if defined(CONFIG_AEABI) && (__LINUX_ARM_ARCH__ >= 5)
1902dede2d8SNicolas Pitre#define SPFIX(code...) code
1912dede2d8SNicolas Pitre#else
1922dede2d8SNicolas Pitre#define SPFIX(code...)
1932dede2d8SNicolas Pitre#endif
1942dede2d8SNicolas Pitre
195*a1c510d0SArd Biesheuvel	.macro	svc_entry, stack_hole=0, trace=1, uaccess=1, overflow_check=1
196c4c5716eSCatalin Marinas UNWIND(.fnstart		)
197ae5cc07dSArd Biesheuvel	sub	sp, sp, #(SVC_REGS_SIZE + \stack_hole)
198*a1c510d0SArd Biesheuvel THUMB(	add	sp, r1		)	@ get SP in a GPR without
199*a1c510d0SArd Biesheuvel THUMB(	sub	r1, sp, r1	)	@ using a temp register
200*a1c510d0SArd Biesheuvel
201*a1c510d0SArd Biesheuvel	.if	\overflow_check
202*a1c510d0SArd Biesheuvel UNWIND(.save	{r0 - pc}	)
203*a1c510d0SArd Biesheuvel	do_overflow_check (SVC_REGS_SIZE + \stack_hole)
204*a1c510d0SArd Biesheuvel	.endif
205*a1c510d0SArd Biesheuvel
206b86040a5SCatalin Marinas#ifdef CONFIG_THUMB2_KERNEL
207ae5cc07dSArd Biesheuvel	tst	r1, #4			@ test stack pointer alignment
208ae5cc07dSArd Biesheuvel	sub	r1, sp, r1		@ restore original R1
209ae5cc07dSArd Biesheuvel	sub	sp, r1			@ restore original SP
210b86040a5SCatalin Marinas#else
2112dede2d8SNicolas Pitre SPFIX(	tst	sp, #4		)
212b86040a5SCatalin Marinas#endif
213ae5cc07dSArd Biesheuvel SPFIX(	subne	sp, sp, #4	)
214ae5cc07dSArd Biesheuvel
215ae5cc07dSArd Biesheuvel ARM(	stmib	sp, {r1 - r12}	)
216ae5cc07dSArd Biesheuvel THUMB(	stmia	sp, {r0 - r12}	)	@ No STMIB in Thumb-2
217ccea7a19SRussell King
218b059bdc3SRussell King	ldmia	r0, {r3 - r5}
219ae5cc07dSArd Biesheuvel	add	r7, sp, #S_SP		@ here for interlock avoidance
220b059bdc3SRussell King	mov	r6, #-1			@  ""  ""      ""       ""
221ae5cc07dSArd Biesheuvel	add	r2, sp, #(SVC_REGS_SIZE + \stack_hole)
222ae5cc07dSArd Biesheuvel SPFIX(	addne	r2, r2, #4	)
223ae5cc07dSArd Biesheuvel	str	r3, [sp]		@ save the "real" r0 copied
224ccea7a19SRussell King					@ from the exception stack
225ccea7a19SRussell King
226b059bdc3SRussell King	mov	r3, lr
2271da177e4SLinus Torvalds
2281da177e4SLinus Torvalds	@
2291da177e4SLinus Torvalds	@ We are now ready to fill in the remaining blanks on the stack:
2301da177e4SLinus Torvalds	@
231b059bdc3SRussell King	@  r2 - sp_svc
232b059bdc3SRussell King	@  r3 - lr_svc
233b059bdc3SRussell King	@  r4 - lr_<exception>, already fixed up for correct return/restart
234b059bdc3SRussell King	@  r5 - spsr_<exception>
235b059bdc3SRussell King	@  r6 - orig_r0 (see pt_regs definition in ptrace.h)
2361da177e4SLinus Torvalds	@
237b059bdc3SRussell King	stmia	r7, {r2 - r6}
238f2741b78SRussell King
239e6978e4bSRussell King	get_thread_info tsk
240747ffc2fSRussell King	uaccess_entry tsk, r0, r1, r2, \uaccess
2412190fed6SRussell King
242c0e7f7eeSDaniel Thompson	.if \trace
243f2741b78SRussell King#ifdef CONFIG_TRACE_IRQFLAGS
244f2741b78SRussell King	bl	trace_hardirqs_off
245f2741b78SRussell King#endif
246c0e7f7eeSDaniel Thompson	.endif
2471da177e4SLinus Torvalds	.endm
2481da177e4SLinus Torvalds
2491da177e4SLinus Torvalds	.align	5
2501da177e4SLinus Torvalds__dabt_svc:
2512190fed6SRussell King	svc_entry uaccess=0
2521da177e4SLinus Torvalds	mov	r2, sp
253da740472SRussell King	dabt_helper
254e16b31bfSMarc Zyngier THUMB(	ldr	r5, [sp, #S_PSR]	)	@ potentially updated CPSR
255b059bdc3SRussell King	svc_exit r5				@ return from exception
256c4c5716eSCatalin Marinas UNWIND(.fnend		)
25793ed3970SCatalin MarinasENDPROC(__dabt_svc)
2581da177e4SLinus Torvalds
2591da177e4SLinus Torvalds	.align	5
2601da177e4SLinus Torvalds__irq_svc:
261ccea7a19SRussell King	svc_entry
262d4664b6cSArd Biesheuvel	irq_handler from_user=0
2631613cc11SRussell King
264e7289c6dSThomas Gleixner#ifdef CONFIG_PREEMPTION
265706fdd9fSRussell King	ldr	r8, [tsk, #TI_PREEMPT]		@ get preempt count
266706fdd9fSRussell King	ldr	r0, [tsk, #TI_FLAGS]		@ get flags
26728fab1a2SRussell King	teq	r8, #0				@ if preempt count != 0
26828fab1a2SRussell King	movne	r0, #0				@ force flags to 0
2691da177e4SLinus Torvalds	tst	r0, #_TIF_NEED_RESCHED
2701da177e4SLinus Torvalds	blne	svc_preempt
2711da177e4SLinus Torvalds#endif
27230891c90SRussell King
2739b56febeSRussell King	svc_exit r5, irq = 1			@ return from exception
274c4c5716eSCatalin Marinas UNWIND(.fnend		)
27593ed3970SCatalin MarinasENDPROC(__irq_svc)
2761da177e4SLinus Torvalds
2771da177e4SLinus Torvalds	.ltorg
2781da177e4SLinus Torvalds
279e7289c6dSThomas Gleixner#ifdef CONFIG_PREEMPTION
2801da177e4SLinus Torvaldssvc_preempt:
28128fab1a2SRussell King	mov	r8, lr
2821da177e4SLinus Torvalds1:	bl	preempt_schedule_irq		@ irq en/disable is done inside
283706fdd9fSRussell King	ldr	r0, [tsk, #TI_FLAGS]		@ get new tasks TI_FLAGS
2841da177e4SLinus Torvalds	tst	r0, #_TIF_NEED_RESCHED
2856ebbf2ceSRussell King	reteq	r8				@ go again
2861da177e4SLinus Torvalds	b	1b
2871da177e4SLinus Torvalds#endif
2881da177e4SLinus Torvalds
28915ac49b6SRussell King__und_fault:
29015ac49b6SRussell King	@ Correct the PC such that it is pointing at the instruction
29115ac49b6SRussell King	@ which caused the fault.  If the faulting instruction was ARM
29215ac49b6SRussell King	@ the PC will be pointing at the next instruction, and have to
29315ac49b6SRussell King	@ subtract 4.  Otherwise, it is Thumb, and the PC will be
29415ac49b6SRussell King	@ pointing at the second half of the Thumb instruction.  We
29515ac49b6SRussell King	@ have to subtract 2.
29615ac49b6SRussell King	ldr	r2, [r0, #S_PC]
29715ac49b6SRussell King	sub	r2, r2, r1
29815ac49b6SRussell King	str	r2, [r0, #S_PC]
29915ac49b6SRussell King	b	do_undefinstr
30015ac49b6SRussell KingENDPROC(__und_fault)
30115ac49b6SRussell King
3021da177e4SLinus Torvalds	.align	5
3031da177e4SLinus Torvalds__und_svc:
304d30a0c8bSNicolas Pitre#ifdef CONFIG_KPROBES
305d30a0c8bSNicolas Pitre	@ If a kprobe is about to simulate a "stmdb sp..." instruction,
306d30a0c8bSNicolas Pitre	@ it obviously needs free stack space which then will belong to
307d30a0c8bSNicolas Pitre	@ the saved context.
308a0266c21SWang Nan	svc_entry MAX_STACK_SIZE
309d30a0c8bSNicolas Pitre#else
310ccea7a19SRussell King	svc_entry
311d30a0c8bSNicolas Pitre#endif
3121da177e4SLinus Torvalds
31315ac49b6SRussell King	mov	r1, #4				@ PC correction to apply
314f77ac2e3SArd Biesheuvel THUMB(	tst	r5, #PSR_T_BIT		)	@ exception taken in Thumb mode?
315f77ac2e3SArd Biesheuvel THUMB(	movne	r1, #2			)	@ if so, fix up PC correction
3161da177e4SLinus Torvalds	mov	r0, sp				@ struct pt_regs *regs
31715ac49b6SRussell King	bl	__und_fault
3181da177e4SLinus Torvalds
31915ac49b6SRussell King__und_svc_finish:
32087eed3c7SRussell King	get_thread_info tsk
321b059bdc3SRussell King	ldr	r5, [sp, #S_PSR]		@ Get SVC cpsr
322b059bdc3SRussell King	svc_exit r5				@ return from exception
323c4c5716eSCatalin Marinas UNWIND(.fnend		)
32493ed3970SCatalin MarinasENDPROC(__und_svc)
3251da177e4SLinus Torvalds
3261da177e4SLinus Torvalds	.align	5
3271da177e4SLinus Torvalds__pabt_svc:
328ccea7a19SRussell King	svc_entry
3294fb28474SKirill A. Shutemov	mov	r2, sp				@ regs
3308dfe7ac9SRussell King	pabt_helper
331b059bdc3SRussell King	svc_exit r5				@ return from exception
332c4c5716eSCatalin Marinas UNWIND(.fnend		)
33393ed3970SCatalin MarinasENDPROC(__pabt_svc)
3341da177e4SLinus Torvalds
3351da177e4SLinus Torvalds	.align	5
336c0e7f7eeSDaniel Thompson__fiq_svc:
337c0e7f7eeSDaniel Thompson	svc_entry trace=0
338c0e7f7eeSDaniel Thompson	mov	r0, sp				@ struct pt_regs *regs
339c0e7f7eeSDaniel Thompson	bl	handle_fiq_as_nmi
340c0e7f7eeSDaniel Thompson	svc_exit_via_fiq
341c0e7f7eeSDaniel Thompson UNWIND(.fnend		)
342c0e7f7eeSDaniel ThompsonENDPROC(__fiq_svc)
343c0e7f7eeSDaniel Thompson
344c0e7f7eeSDaniel Thompson	.align	5
34549f680eaSRussell King.LCcralign:
34649f680eaSRussell King	.word	cr_alignment
34748d7927bSPaul Brook#ifdef MULTI_DABORT
3481da177e4SLinus Torvalds.LCprocfns:
3491da177e4SLinus Torvalds	.word	processor
3501da177e4SLinus Torvalds#endif
3511da177e4SLinus Torvalds.LCfp:
3521da177e4SLinus Torvalds	.word	fp_enter
3531da177e4SLinus Torvalds
3541da177e4SLinus Torvalds/*
355c0e7f7eeSDaniel Thompson * Abort mode handlers
356c0e7f7eeSDaniel Thompson */
357c0e7f7eeSDaniel Thompson
358c0e7f7eeSDaniel Thompson@
359c0e7f7eeSDaniel Thompson@ Taking a FIQ in abort mode is similar to taking a FIQ in SVC mode
360c0e7f7eeSDaniel Thompson@ and reuses the same macros. However in abort mode we must also
361c0e7f7eeSDaniel Thompson@ save/restore lr_abt and spsr_abt to make nested aborts safe.
362c0e7f7eeSDaniel Thompson@
363c0e7f7eeSDaniel Thompson	.align 5
364c0e7f7eeSDaniel Thompson__fiq_abt:
365c0e7f7eeSDaniel Thompson	svc_entry trace=0
366c0e7f7eeSDaniel Thompson
367c0e7f7eeSDaniel Thompson ARM(	msr	cpsr_c, #ABT_MODE | PSR_I_BIT | PSR_F_BIT )
368c0e7f7eeSDaniel Thompson THUMB( mov	r0, #ABT_MODE | PSR_I_BIT | PSR_F_BIT )
369c0e7f7eeSDaniel Thompson THUMB( msr	cpsr_c, r0 )
370c0e7f7eeSDaniel Thompson	mov	r1, lr		@ Save lr_abt
371c0e7f7eeSDaniel Thompson	mrs	r2, spsr	@ Save spsr_abt, abort is now safe
372c0e7f7eeSDaniel Thompson ARM(	msr	cpsr_c, #SVC_MODE | PSR_I_BIT | PSR_F_BIT )
373c0e7f7eeSDaniel Thompson THUMB( mov	r0, #SVC_MODE | PSR_I_BIT | PSR_F_BIT )
374c0e7f7eeSDaniel Thompson THUMB( msr	cpsr_c, r0 )
375c0e7f7eeSDaniel Thompson	stmfd	sp!, {r1 - r2}
376c0e7f7eeSDaniel Thompson
377c0e7f7eeSDaniel Thompson	add	r0, sp, #8			@ struct pt_regs *regs
378c0e7f7eeSDaniel Thompson	bl	handle_fiq_as_nmi
379c0e7f7eeSDaniel Thompson
380c0e7f7eeSDaniel Thompson	ldmfd	sp!, {r1 - r2}
381c0e7f7eeSDaniel Thompson ARM(	msr	cpsr_c, #ABT_MODE | PSR_I_BIT | PSR_F_BIT )
382c0e7f7eeSDaniel Thompson THUMB( mov	r0, #ABT_MODE | PSR_I_BIT | PSR_F_BIT )
383c0e7f7eeSDaniel Thompson THUMB( msr	cpsr_c, r0 )
384c0e7f7eeSDaniel Thompson	mov	lr, r1		@ Restore lr_abt, abort is unsafe
385c0e7f7eeSDaniel Thompson	msr	spsr_cxsf, r2	@ Restore spsr_abt
386c0e7f7eeSDaniel Thompson ARM(	msr	cpsr_c, #SVC_MODE | PSR_I_BIT | PSR_F_BIT )
387c0e7f7eeSDaniel Thompson THUMB( mov	r0, #SVC_MODE | PSR_I_BIT | PSR_F_BIT )
388c0e7f7eeSDaniel Thompson THUMB( msr	cpsr_c, r0 )
389c0e7f7eeSDaniel Thompson
390c0e7f7eeSDaniel Thompson	svc_exit_via_fiq
391c0e7f7eeSDaniel Thompson UNWIND(.fnend		)
392c0e7f7eeSDaniel ThompsonENDPROC(__fiq_abt)
393c0e7f7eeSDaniel Thompson
394c0e7f7eeSDaniel Thompson/*
3951da177e4SLinus Torvalds * User mode handlers
3962dede2d8SNicolas Pitre *
3975745eef6SRussell King * EABI note: sp_svc is always 64-bit aligned here, so should PT_REGS_SIZE
3981da177e4SLinus Torvalds */
3992dede2d8SNicolas Pitre
4005745eef6SRussell King#if defined(CONFIG_AEABI) && (__LINUX_ARM_ARCH__ >= 5) && (PT_REGS_SIZE & 7)
4012dede2d8SNicolas Pitre#error "sizeof(struct pt_regs) must be a multiple of 8"
4022dede2d8SNicolas Pitre#endif
4032dede2d8SNicolas Pitre
4042190fed6SRussell King	.macro	usr_entry, trace=1, uaccess=1
405c4c5716eSCatalin Marinas UNWIND(.fnstart	)
406c4c5716eSCatalin Marinas UNWIND(.cantunwind	)	@ don't unwind the user space
4075745eef6SRussell King	sub	sp, sp, #PT_REGS_SIZE
408b86040a5SCatalin Marinas ARM(	stmib	sp, {r1 - r12}	)
409b86040a5SCatalin Marinas THUMB(	stmia	sp, {r0 - r12}	)
410ccea7a19SRussell King
411195b58adSRussell King ATRAP(	mrc	p15, 0, r7, c1, c0, 0)
412195b58adSRussell King ATRAP(	ldr	r8, .LCcralign)
413195b58adSRussell King
414b059bdc3SRussell King	ldmia	r0, {r3 - r5}
415ccea7a19SRussell King	add	r0, sp, #S_PC		@ here for interlock avoidance
416b059bdc3SRussell King	mov	r6, #-1			@  ""  ""     ""        ""
417ccea7a19SRussell King
418b059bdc3SRussell King	str	r3, [sp]		@ save the "real" r0 copied
419ccea7a19SRussell King					@ from the exception stack
4201da177e4SLinus Torvalds
421195b58adSRussell King ATRAP(	ldr	r8, [r8, #0])
422195b58adSRussell King
4231da177e4SLinus Torvalds	@
4241da177e4SLinus Torvalds	@ We are now ready to fill in the remaining blanks on the stack:
4251da177e4SLinus Torvalds	@
426b059bdc3SRussell King	@  r4 - lr_<exception>, already fixed up for correct return/restart
427b059bdc3SRussell King	@  r5 - spsr_<exception>
428b059bdc3SRussell King	@  r6 - orig_r0 (see pt_regs definition in ptrace.h)
4291da177e4SLinus Torvalds	@
4301da177e4SLinus Torvalds	@ Also, separately save sp_usr and lr_usr
4311da177e4SLinus Torvalds	@
432b059bdc3SRussell King	stmia	r0, {r4 - r6}
433b86040a5SCatalin Marinas ARM(	stmdb	r0, {sp, lr}^			)
434b86040a5SCatalin Marinas THUMB(	store_user_sp_lr r0, r1, S_SP - S_PC	)
4351da177e4SLinus Torvalds
4362190fed6SRussell King	.if \uaccess
4372190fed6SRussell King	uaccess_disable ip
4382190fed6SRussell King	.endif
4392190fed6SRussell King
4401da177e4SLinus Torvalds	@ Enable the alignment trap while in kernel mode
441195b58adSRussell King ATRAP(	teq	r8, r7)
442195b58adSRussell King ATRAP( mcrne	p15, 0, r8, c1, c0, 0)
4431da177e4SLinus Torvalds
44450596b75SArd Biesheuvel	reload_current r7, r8
44550596b75SArd Biesheuvel
4461da177e4SLinus Torvalds	@
4471da177e4SLinus Torvalds	@ Clear FP to mark the first stack frame
4481da177e4SLinus Torvalds	@
4491da177e4SLinus Torvalds	zero_fp
450f2741b78SRussell King
451c0e7f7eeSDaniel Thompson	.if	\trace
45211b8b25cSRussell King#ifdef CONFIG_TRACE_IRQFLAGS
453f2741b78SRussell King	bl	trace_hardirqs_off
454f2741b78SRussell King#endif
455b0088480SKevin Hilman	ct_user_exit save = 0
456c0e7f7eeSDaniel Thompson	.endif
4571da177e4SLinus Torvalds	.endm
4581da177e4SLinus Torvalds
459b49c0f24SNicolas Pitre	.macro	kuser_cmpxchg_check
460db695c05SRussell King#if !defined(CONFIG_CPU_32v6K) && defined(CONFIG_KUSER_HELPERS)
461b49c0f24SNicolas Pitre#ifndef CONFIG_MMU
462b49c0f24SNicolas Pitre#warning "NPTL on non MMU needs fixing"
463b49c0f24SNicolas Pitre#else
464b49c0f24SNicolas Pitre	@ Make sure our user space atomic helper is restarted
465b49c0f24SNicolas Pitre	@ if it was interrupted in a critical region.  Here we
466b49c0f24SNicolas Pitre	@ perform a quick test inline since it should be false
467b49c0f24SNicolas Pitre	@ 99.9999% of the time.  The rest is done out of line.
468c12366baSLinus Walleij	ldr	r0, =TASK_SIZE
469c12366baSLinus Walleij	cmp	r4, r0
47040fb79c8SNicolas Pitre	blhs	kuser_cmpxchg64_fixup
471b49c0f24SNicolas Pitre#endif
472b49c0f24SNicolas Pitre#endif
473b49c0f24SNicolas Pitre	.endm
474b49c0f24SNicolas Pitre
4751da177e4SLinus Torvalds	.align	5
4761da177e4SLinus Torvalds__dabt_usr:
4772190fed6SRussell King	usr_entry uaccess=0
478b49c0f24SNicolas Pitre	kuser_cmpxchg_check
4791da177e4SLinus Torvalds	mov	r2, sp
480da740472SRussell King	dabt_helper
481da740472SRussell King	b	ret_from_exception
482c4c5716eSCatalin Marinas UNWIND(.fnend		)
48393ed3970SCatalin MarinasENDPROC(__dabt_usr)
4841da177e4SLinus Torvalds
4851da177e4SLinus Torvalds	.align	5
4861da177e4SLinus Torvalds__irq_usr:
487ccea7a19SRussell King	usr_entry
488bc089602SRussell King	kuser_cmpxchg_check
489d4664b6cSArd Biesheuvel	irq_handler from_user=1
4901613cc11SRussell King	get_thread_info tsk
4911da177e4SLinus Torvalds	mov	why, #0
4929fc2552aSMing Lei	b	ret_to_user_from_irq
493c4c5716eSCatalin Marinas UNWIND(.fnend		)
49493ed3970SCatalin MarinasENDPROC(__irq_usr)
4951da177e4SLinus Torvalds
4961da177e4SLinus Torvalds	.ltorg
4971da177e4SLinus Torvalds
4981da177e4SLinus Torvalds	.align	5
4991da177e4SLinus Torvalds__und_usr:
5002190fed6SRussell King	usr_entry uaccess=0
501bc089602SRussell King
502b059bdc3SRussell King	mov	r2, r4
503b059bdc3SRussell King	mov	r3, r5
5041da177e4SLinus Torvalds
50515ac49b6SRussell King	@ r2 = regs->ARM_pc, which is either 2 or 4 bytes ahead of the
50615ac49b6SRussell King	@      faulting instruction depending on Thumb mode.
50715ac49b6SRussell King	@ r3 = regs->ARM_cpsr
5081da177e4SLinus Torvalds	@
50915ac49b6SRussell King	@ The emulation code returns using r9 if it has emulated the
51015ac49b6SRussell King	@ instruction, or the more conventional lr if we are to treat
51115ac49b6SRussell King	@ this as a real undefined instruction
5121da177e4SLinus Torvalds	@
51314327c66SRussell King	badr	r9, ret_from_exception
51415ac49b6SRussell King
5151417a6b8SCatalin Marinas	@ IRQs must be enabled before attempting to read the instruction from
5161417a6b8SCatalin Marinas	@ user space since that could cause a page/translation fault if the
5171417a6b8SCatalin Marinas	@ page table was modified by another CPU.
5181417a6b8SCatalin Marinas	enable_irq
5191417a6b8SCatalin Marinas
520cb170a45SPaul Brook	tst	r3, #PSR_T_BIT			@ Thumb mode?
52115ac49b6SRussell King	bne	__und_usr_thumb
52215ac49b6SRussell King	sub	r4, r2, #4			@ ARM instr at LR - 4
52315ac49b6SRussell King1:	ldrt	r0, [r4]
524457c2403SBen Dooks ARM_BE8(rev	r0, r0)				@ little endian instruction
525457c2403SBen Dooks
5262190fed6SRussell King	uaccess_disable ip
5272190fed6SRussell King
52815ac49b6SRussell King	@ r0 = 32-bit ARM instruction which caused the exception
52915ac49b6SRussell King	@ r2 = PC value for the following instruction (:= regs->ARM_pc)
53015ac49b6SRussell King	@ r4 = PC value for the faulting instruction
53115ac49b6SRussell King	@ lr = 32-bit undefined instruction function
53214327c66SRussell King	badr	lr, __und_usr_fault_32
53315ac49b6SRussell King	b	call_fpe
53415ac49b6SRussell King
53515ac49b6SRussell King__und_usr_thumb:
536cb170a45SPaul Brook	@ Thumb instruction
53715ac49b6SRussell King	sub	r4, r2, #2			@ First half of thumb instr at LR - 2
538ef4c5368SDave Martin#if CONFIG_ARM_THUMB && __LINUX_ARM_ARCH__ >= 6 && CONFIG_CPU_V7
539ef4c5368SDave Martin/*
540ef4c5368SDave Martin * Thumb-2 instruction handling.  Note that because pre-v6 and >= v6 platforms
541ef4c5368SDave Martin * can never be supported in a single kernel, this code is not applicable at
542ef4c5368SDave Martin * all when __LINUX_ARM_ARCH__ < 6.  This allows simplifying assumptions to be
543ef4c5368SDave Martin * made about .arch directives.
544ef4c5368SDave Martin */
545ef4c5368SDave Martin#if __LINUX_ARM_ARCH__ < 7
546ef4c5368SDave Martin/* If the target CPU may not be Thumb-2-capable, a run-time check is needed: */
547ef4c5368SDave Martin#define NEED_CPU_ARCHITECTURE
548ef4c5368SDave Martin	ldr	r5, .LCcpu_architecture
549ef4c5368SDave Martin	ldr	r5, [r5]
550ef4c5368SDave Martin	cmp	r5, #CPU_ARCH_ARMv7
55115ac49b6SRussell King	blo	__und_usr_fault_16		@ 16bit undefined instruction
552ef4c5368SDave Martin/*
553ef4c5368SDave Martin * The following code won't get run unless the running CPU really is v7, so
554ef4c5368SDave Martin * coding round the lack of ldrht on older arches is pointless.  Temporarily
555ef4c5368SDave Martin * override the assembler target arch with the minimum required instead:
556ef4c5368SDave Martin */
557ef4c5368SDave Martin	.arch	armv6t2
558ef4c5368SDave Martin#endif
55915ac49b6SRussell King2:	ldrht	r5, [r4]
560f8fe23ecSVictor KamenskyARM_BE8(rev16	r5, r5)				@ little endian instruction
56185519189SDave Martin	cmp	r5, #0xe800			@ 32bit instruction if xx != 0
5622190fed6SRussell King	blo	__und_usr_fault_16_pan		@ 16bit undefined instruction
56315ac49b6SRussell King3:	ldrht	r0, [r2]
564f8fe23ecSVictor KamenskyARM_BE8(rev16	r0, r0)				@ little endian instruction
5652190fed6SRussell King	uaccess_disable ip
566cb170a45SPaul Brook	add	r2, r2, #2			@ r2 is PC + 2, make it PC + 4
56715ac49b6SRussell King	str	r2, [sp, #S_PC]			@ it's a 2x16bit instr, update
568cb170a45SPaul Brook	orr	r0, r0, r5, lsl #16
56914327c66SRussell King	badr	lr, __und_usr_fault_32
57015ac49b6SRussell King	@ r0 = the two 16-bit Thumb instructions which caused the exception
57115ac49b6SRussell King	@ r2 = PC value for the following Thumb instruction (:= regs->ARM_pc)
57215ac49b6SRussell King	@ r4 = PC value for the first 16-bit Thumb instruction
57315ac49b6SRussell King	@ lr = 32bit undefined instruction function
574ef4c5368SDave Martin
575ef4c5368SDave Martin#if __LINUX_ARM_ARCH__ < 7
576ef4c5368SDave Martin/* If the target arch was overridden, change it back: */
577ef4c5368SDave Martin#ifdef CONFIG_CPU_32v6K
578ef4c5368SDave Martin	.arch	armv6k
579cb170a45SPaul Brook#else
580ef4c5368SDave Martin	.arch	armv6
581ef4c5368SDave Martin#endif
582ef4c5368SDave Martin#endif /* __LINUX_ARM_ARCH__ < 7 */
583ef4c5368SDave Martin#else /* !(CONFIG_ARM_THUMB && __LINUX_ARM_ARCH__ >= 6 && CONFIG_CPU_V7) */
58415ac49b6SRussell King	b	__und_usr_fault_16
585cb170a45SPaul Brook#endif
586c4c5716eSCatalin Marinas UNWIND(.fnend)
58793ed3970SCatalin MarinasENDPROC(__und_usr)
588cb170a45SPaul Brook
5891da177e4SLinus Torvalds/*
59015ac49b6SRussell King * The out of line fixup for the ldrt instructions above.
5911da177e4SLinus Torvalds */
592c4a84ae3SArd Biesheuvel	.pushsection .text.fixup, "ax"
593667d1b48SWill Deacon	.align	2
5943780f7abSArun K S4:	str     r4, [sp, #S_PC]			@ retry current instruction
5956ebbf2ceSRussell King	ret	r9
5964260415fSRussell King	.popsection
5974260415fSRussell King	.pushsection __ex_table,"a"
598cb170a45SPaul Brook	.long	1b, 4b
599c89cefedSGuennadi Liakhovetski#if CONFIG_ARM_THUMB && __LINUX_ARM_ARCH__ >= 6 && CONFIG_CPU_V7
600cb170a45SPaul Brook	.long	2b, 4b
601cb170a45SPaul Brook	.long	3b, 4b
602cb170a45SPaul Brook#endif
6034260415fSRussell King	.popsection
6041da177e4SLinus Torvalds
6051da177e4SLinus Torvalds/*
6061da177e4SLinus Torvalds * Check whether the instruction is a co-processor instruction.
6071da177e4SLinus Torvalds * If yes, we need to call the relevant co-processor handler.
6081da177e4SLinus Torvalds *
6091da177e4SLinus Torvalds * Note that we don't do a full check here for the co-processor
6101da177e4SLinus Torvalds * instructions; all instructions with bit 27 set are well
6111da177e4SLinus Torvalds * defined.  The only instructions that should fault are the
6121da177e4SLinus Torvalds * co-processor instructions.  However, we have to watch out
6131da177e4SLinus Torvalds * for the ARM6/ARM7 SWI bug.
6141da177e4SLinus Torvalds *
615b5872db4SCatalin Marinas * NEON is a special case that has to be handled here. Not all
616b5872db4SCatalin Marinas * NEON instructions are co-processor instructions, so we have
617b5872db4SCatalin Marinas * to make a special case of checking for them. Plus, there's
618b5872db4SCatalin Marinas * five groups of them, so we have a table of mask/opcode pairs
619b5872db4SCatalin Marinas * to check against, and if any match then we branch off into the
620b5872db4SCatalin Marinas * NEON handler code.
621b5872db4SCatalin Marinas *
6221da177e4SLinus Torvalds * Emulators may wish to make use of the following registers:
62315ac49b6SRussell King *  r0  = instruction opcode (32-bit ARM or two 16-bit Thumb)
62415ac49b6SRussell King *  r2  = PC value to resume execution after successful emulation
625db6ccbb6SRussell King *  r9  = normal "successful" return address
62615ac49b6SRussell King *  r10 = this threads thread_info structure
627db6ccbb6SRussell King *  lr  = unrecognised instruction return address
6281417a6b8SCatalin Marinas * IRQs enabled, FIQs enabled.
6291da177e4SLinus Torvalds */
630cb170a45SPaul Brook	@
631cb170a45SPaul Brook	@ Fall-through from Thumb-2 __und_usr
632cb170a45SPaul Brook	@
633cb170a45SPaul Brook#ifdef CONFIG_NEON
634d3f79584SRussell King	get_thread_info r10			@ get current thread
635cb170a45SPaul Brook	adr	r6, .LCneon_thumb_opcodes
636cb170a45SPaul Brook	b	2f
637cb170a45SPaul Brook#endif
6381da177e4SLinus Torvaldscall_fpe:
639d3f79584SRussell King	get_thread_info r10			@ get current thread
640b5872db4SCatalin Marinas#ifdef CONFIG_NEON
641cb170a45SPaul Brook	adr	r6, .LCneon_arm_opcodes
642d3f79584SRussell King2:	ldr	r5, [r6], #4			@ mask value
643b5872db4SCatalin Marinas	ldr	r7, [r6], #4			@ opcode bits matching in mask
644d3f79584SRussell King	cmp	r5, #0				@ end mask?
645d3f79584SRussell King	beq	1f
646d3f79584SRussell King	and	r8, r0, r5
647b5872db4SCatalin Marinas	cmp	r8, r7				@ NEON instruction?
648b5872db4SCatalin Marinas	bne	2b
649b5872db4SCatalin Marinas	mov	r7, #1
650b5872db4SCatalin Marinas	strb	r7, [r10, #TI_USED_CP + 10]	@ mark CP#10 as used
651b5872db4SCatalin Marinas	strb	r7, [r10, #TI_USED_CP + 11]	@ mark CP#11 as used
652b5872db4SCatalin Marinas	b	do_vfp				@ let VFP handler handle this
653b5872db4SCatalin Marinas1:
654b5872db4SCatalin Marinas#endif
6551da177e4SLinus Torvalds	tst	r0, #0x08000000			@ only CDP/CPRT/LDC/STC have bit 27
656cb170a45SPaul Brook	tstne	r0, #0x04000000			@ bit 26 set on both ARM and Thumb-2
6576ebbf2ceSRussell King	reteq	lr
6581da177e4SLinus Torvalds	and	r8, r0, #0x00000f00		@ mask out CP number
659b86040a5SCatalin Marinas THUMB(	lsr	r8, r8, #8		)
6601da177e4SLinus Torvalds	mov	r7, #1
6611da177e4SLinus Torvalds	add	r6, r10, #TI_USED_CP
662b86040a5SCatalin Marinas ARM(	strb	r7, [r6, r8, lsr #8]	)	@ set appropriate used_cp[]
663b86040a5SCatalin Marinas THUMB(	strb	r7, [r6, r8]		)	@ set appropriate used_cp[]
6641da177e4SLinus Torvalds#ifdef CONFIG_IWMMXT
6651da177e4SLinus Torvalds	@ Test if we need to give access to iWMMXt coprocessors
6661da177e4SLinus Torvalds	ldr	r5, [r10, #TI_FLAGS]
6671da177e4SLinus Torvalds	rsbs	r7, r8, #(1 << 8)		@ CP 0 or 1 only
668e44fc388SStefan Agner	movscs	r7, r5, lsr #(TIF_USING_IWMMXT + 1)
6691da177e4SLinus Torvalds	bcs	iwmmxt_task_enable
6701da177e4SLinus Torvalds#endif
671b86040a5SCatalin Marinas ARM(	add	pc, pc, r8, lsr #6	)
672b86040a5SCatalin Marinas THUMB(	lsl	r8, r8, #2		)
673b86040a5SCatalin Marinas THUMB(	add	pc, r8			)
674b86040a5SCatalin Marinas	nop
6751da177e4SLinus Torvalds
6766ebbf2ceSRussell King	ret.w	lr				@ CP#0
677b86040a5SCatalin Marinas	W(b)	do_fpe				@ CP#1 (FPE)
678b86040a5SCatalin Marinas	W(b)	do_fpe				@ CP#2 (FPE)
6796ebbf2ceSRussell King	ret.w	lr				@ CP#3
6806ebbf2ceSRussell King	ret.w	lr				@ CP#4
6816ebbf2ceSRussell King	ret.w	lr				@ CP#5
6826ebbf2ceSRussell King	ret.w	lr				@ CP#6
6836ebbf2ceSRussell King	ret.w	lr				@ CP#7
6846ebbf2ceSRussell King	ret.w	lr				@ CP#8
6856ebbf2ceSRussell King	ret.w	lr				@ CP#9
6861da177e4SLinus Torvalds#ifdef CONFIG_VFP
687b86040a5SCatalin Marinas	W(b)	do_vfp				@ CP#10 (VFP)
688b86040a5SCatalin Marinas	W(b)	do_vfp				@ CP#11 (VFP)
6891da177e4SLinus Torvalds#else
6906ebbf2ceSRussell King	ret.w	lr				@ CP#10 (VFP)
6916ebbf2ceSRussell King	ret.w	lr				@ CP#11 (VFP)
6921da177e4SLinus Torvalds#endif
6936ebbf2ceSRussell King	ret.w	lr				@ CP#12
6946ebbf2ceSRussell King	ret.w	lr				@ CP#13
6956ebbf2ceSRussell King	ret.w	lr				@ CP#14 (Debug)
6966ebbf2ceSRussell King	ret.w	lr				@ CP#15 (Control)
6971da177e4SLinus Torvalds
698ef4c5368SDave Martin#ifdef NEED_CPU_ARCHITECTURE
699ef4c5368SDave Martin	.align	2
700ef4c5368SDave Martin.LCcpu_architecture:
701ef4c5368SDave Martin	.word	__cpu_architecture
702ef4c5368SDave Martin#endif
703ef4c5368SDave Martin
704b5872db4SCatalin Marinas#ifdef CONFIG_NEON
705b5872db4SCatalin Marinas	.align	6
706b5872db4SCatalin Marinas
707cb170a45SPaul Brook.LCneon_arm_opcodes:
708b5872db4SCatalin Marinas	.word	0xfe000000			@ mask
709b5872db4SCatalin Marinas	.word	0xf2000000			@ opcode
710b5872db4SCatalin Marinas
711b5872db4SCatalin Marinas	.word	0xff100000			@ mask
712b5872db4SCatalin Marinas	.word	0xf4000000			@ opcode
713b5872db4SCatalin Marinas
714b5872db4SCatalin Marinas	.word	0x00000000			@ mask
715b5872db4SCatalin Marinas	.word	0x00000000			@ opcode
716cb170a45SPaul Brook
717cb170a45SPaul Brook.LCneon_thumb_opcodes:
718cb170a45SPaul Brook	.word	0xef000000			@ mask
719cb170a45SPaul Brook	.word	0xef000000			@ opcode
720cb170a45SPaul Brook
721cb170a45SPaul Brook	.word	0xff100000			@ mask
722cb170a45SPaul Brook	.word	0xf9000000			@ opcode
723cb170a45SPaul Brook
724cb170a45SPaul Brook	.word	0x00000000			@ mask
725cb170a45SPaul Brook	.word	0x00000000			@ opcode
726b5872db4SCatalin Marinas#endif
727b5872db4SCatalin Marinas
7281da177e4SLinus Torvaldsdo_fpe:
7291da177e4SLinus Torvalds	ldr	r4, .LCfp
7301da177e4SLinus Torvalds	add	r10, r10, #TI_FPSTATE		@ r10 = workspace
7311da177e4SLinus Torvalds	ldr	pc, [r4]			@ Call FP module USR entry point
7321da177e4SLinus Torvalds
7331da177e4SLinus Torvalds/*
7341da177e4SLinus Torvalds * The FP module is called with these registers set:
7351da177e4SLinus Torvalds *  r0  = instruction
7361da177e4SLinus Torvalds *  r2  = PC+4
7371da177e4SLinus Torvalds *  r9  = normal "successful" return address
7381da177e4SLinus Torvalds *  r10 = FP workspace
7391da177e4SLinus Torvalds *  lr  = unrecognised FP instruction return address
7401da177e4SLinus Torvalds */
7411da177e4SLinus Torvalds
742124efc27SSantosh Shilimkar	.pushsection .data
7431abd3502SRussell King	.align	2
7441da177e4SLinus TorvaldsENTRY(fp_enter)
745db6ccbb6SRussell King	.word	no_fp
746124efc27SSantosh Shilimkar	.popsection
7471da177e4SLinus Torvalds
74883e686eaSCatalin MarinasENTRY(no_fp)
7496ebbf2ceSRussell King	ret	lr
75083e686eaSCatalin MarinasENDPROC(no_fp)
751db6ccbb6SRussell King
75215ac49b6SRussell King__und_usr_fault_32:
75315ac49b6SRussell King	mov	r1, #4
75415ac49b6SRussell King	b	1f
7552190fed6SRussell King__und_usr_fault_16_pan:
7562190fed6SRussell King	uaccess_disable ip
75715ac49b6SRussell King__und_usr_fault_16:
75815ac49b6SRussell King	mov	r1, #2
7591417a6b8SCatalin Marinas1:	mov	r0, sp
76014327c66SRussell King	badr	lr, ret_from_exception
76115ac49b6SRussell King	b	__und_fault
76215ac49b6SRussell KingENDPROC(__und_usr_fault_32)
76315ac49b6SRussell KingENDPROC(__und_usr_fault_16)
7641da177e4SLinus Torvalds
7651da177e4SLinus Torvalds	.align	5
7661da177e4SLinus Torvalds__pabt_usr:
767ccea7a19SRussell King	usr_entry
7684fb28474SKirill A. Shutemov	mov	r2, sp				@ regs
7698dfe7ac9SRussell King	pabt_helper
770c4c5716eSCatalin Marinas UNWIND(.fnend		)
7711da177e4SLinus Torvalds	/* fall through */
7721da177e4SLinus Torvalds/*
7731da177e4SLinus Torvalds * This is the return code to user mode for abort handlers
7741da177e4SLinus Torvalds */
7751da177e4SLinus TorvaldsENTRY(ret_from_exception)
776c4c5716eSCatalin Marinas UNWIND(.fnstart	)
777c4c5716eSCatalin Marinas UNWIND(.cantunwind	)
7781da177e4SLinus Torvalds	get_thread_info tsk
7791da177e4SLinus Torvalds	mov	why, #0
7801da177e4SLinus Torvalds	b	ret_to_user
781c4c5716eSCatalin Marinas UNWIND(.fnend		)
78293ed3970SCatalin MarinasENDPROC(__pabt_usr)
78393ed3970SCatalin MarinasENDPROC(ret_from_exception)
7841da177e4SLinus Torvalds
785c0e7f7eeSDaniel Thompson	.align	5
786c0e7f7eeSDaniel Thompson__fiq_usr:
787c0e7f7eeSDaniel Thompson	usr_entry trace=0
788c0e7f7eeSDaniel Thompson	kuser_cmpxchg_check
789c0e7f7eeSDaniel Thompson	mov	r0, sp				@ struct pt_regs *regs
790c0e7f7eeSDaniel Thompson	bl	handle_fiq_as_nmi
791c0e7f7eeSDaniel Thompson	get_thread_info tsk
792c0e7f7eeSDaniel Thompson	restore_user_regs fast = 0, offset = 0
793c0e7f7eeSDaniel Thompson UNWIND(.fnend		)
794c0e7f7eeSDaniel ThompsonENDPROC(__fiq_usr)
795c0e7f7eeSDaniel Thompson
7961da177e4SLinus Torvalds/*
7971da177e4SLinus Torvalds * Register switch for ARMv3 and ARMv4 processors
7981da177e4SLinus Torvalds * r0 = previous task_struct, r1 = previous thread_info, r2 = next thread_info
7991da177e4SLinus Torvalds * previous and next are guaranteed not to be the same.
8001da177e4SLinus Torvalds */
8011da177e4SLinus TorvaldsENTRY(__switch_to)
802c4c5716eSCatalin Marinas UNWIND(.fnstart	)
803c4c5716eSCatalin Marinas UNWIND(.cantunwind	)
8041da177e4SLinus Torvalds	add	ip, r1, #TI_CPU_SAVE
805b86040a5SCatalin Marinas ARM(	stmia	ip!, {r4 - sl, fp, sp, lr} )	@ Store most regs on stack
806b86040a5SCatalin Marinas THUMB(	stmia	ip!, {r4 - sl, fp}	   )	@ Store most regs on stack
807b86040a5SCatalin Marinas THUMB(	str	sp, [ip], #4		   )
808b86040a5SCatalin Marinas THUMB(	str	lr, [ip], #4		   )
809a4780adeSAndré Hentschel	ldr	r4, [r2, #TI_TP_VALUE]
810a4780adeSAndré Hentschel	ldr	r5, [r2, #TI_TP_VALUE + 4]
811247055aaSCatalin Marinas#ifdef CONFIG_CPU_USE_DOMAINS
8121eef5d2fSRussell King	mrc	p15, 0, r6, c3, c0, 0		@ Get domain register
8131eef5d2fSRussell King	str	r6, [r1, #TI_CPU_DOMAIN]	@ Save old domain register
814d6551e88SRussell King	ldr	r6, [r2, #TI_CPU_DOMAIN]
815afeb90caSHyok S. Choi#endif
816a4780adeSAndré Hentschel	switch_tls r1, r4, r5, r3, r7
817050e9baaSLinus Torvalds#if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_SMP)
818df0698beSNicolas Pitre	ldr	r7, [r2, #TI_TASK]
819df0698beSNicolas Pitre	ldr	r8, =__stack_chk_guard
820ffa47aa6SArnd Bergmann	.if (TSK_STACK_CANARY > IMM12_MASK)
821ffa47aa6SArnd Bergmann	add	r7, r7, #TSK_STACK_CANARY & ~IMM12_MASK
822ffa47aa6SArnd Bergmann	.endif
823ffa47aa6SArnd Bergmann	ldr	r7, [r7, #TSK_STACK_CANARY & IMM12_MASK]
82450596b75SArd Biesheuvel#elif defined(CONFIG_CURRENT_POINTER_IN_TPIDRURO)
82518ed1c01SArd Biesheuvel	mov	r7, r2				@ Preserve 'next'
826df0698beSNicolas Pitre#endif
827247055aaSCatalin Marinas#ifdef CONFIG_CPU_USE_DOMAINS
8281da177e4SLinus Torvalds	mcr	p15, 0, r6, c3, c0, 0		@ Set domain register
829afeb90caSHyok S. Choi#endif
830d6551e88SRussell King	mov	r5, r0
831d6551e88SRussell King	add	r4, r2, #TI_CPU_SAVE
832d6551e88SRussell King	ldr	r0, =thread_notify_head
833d6551e88SRussell King	mov	r1, #THREAD_NOTIFY_SWITCH
834d6551e88SRussell King	bl	atomic_notifier_call_chain
835050e9baaSLinus Torvalds#if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_SMP)
836df0698beSNicolas Pitre	str	r7, [r8]
837df0698beSNicolas Pitre#endif
838d6551e88SRussell King	mov	r0, r5
839*a1c510d0SArd Biesheuvel#if !defined(CONFIG_THUMB2_KERNEL) && !defined(CONFIG_VMAP_STACK)
84050596b75SArd Biesheuvel	set_current r7
841b832faecSArd Biesheuvel	ldmia	r4, {r4 - sl, fp, sp, pc}	@ Load all regs saved previously
842b832faecSArd Biesheuvel#else
843b832faecSArd Biesheuvel	mov	r1, r7
844b832faecSArd Biesheuvel	ldmia	r4, {r4 - sl, fp, ip, lr}	@ Load all regs saved previously
845*a1c510d0SArd Biesheuvel#ifdef CONFIG_VMAP_STACK
846*a1c510d0SArd Biesheuvel	@
847*a1c510d0SArd Biesheuvel	@ Do a dummy read from the new stack while running from the old one so
848*a1c510d0SArd Biesheuvel	@ that we can rely on do_translation_fault() to fix up any stale PMD
849*a1c510d0SArd Biesheuvel	@ entries covering the vmalloc region.
850*a1c510d0SArd Biesheuvel	@
851*a1c510d0SArd Biesheuvel	ldr	r2, [ip]
852*a1c510d0SArd Biesheuvel#endif
853b832faecSArd Biesheuvel
854b832faecSArd Biesheuvel	@ When CONFIG_THREAD_INFO_IN_TASK=n, the update of SP itself is what
855b832faecSArd Biesheuvel	@ effectuates the task switch, as that is what causes the observable
856b832faecSArd Biesheuvel	@ values of current and current_thread_info to change. When
857b832faecSArd Biesheuvel	@ CONFIG_THREAD_INFO_IN_TASK=y, setting current (and therefore
858b832faecSArd Biesheuvel	@ current_thread_info) is done explicitly, and the update of SP just
859b832faecSArd Biesheuvel	@ switches us to another stack, with few other side effects. In order
860b832faecSArd Biesheuvel	@ to prevent this distinction from causing any inconsistencies, let's
861b832faecSArd Biesheuvel	@ keep the 'set_current' call as close as we can to the update of SP.
862b832faecSArd Biesheuvel	set_current r1
863b832faecSArd Biesheuvel	mov	sp, ip
864b832faecSArd Biesheuvel	ret	lr
865b832faecSArd Biesheuvel#endif
866c4c5716eSCatalin Marinas UNWIND(.fnend		)
86793ed3970SCatalin MarinasENDPROC(__switch_to)
8681da177e4SLinus Torvalds
869*a1c510d0SArd Biesheuvel#ifdef CONFIG_VMAP_STACK
870*a1c510d0SArd Biesheuvel	.text
871*a1c510d0SArd Biesheuvel	.align	2
872*a1c510d0SArd Biesheuvel__bad_stack:
873*a1c510d0SArd Biesheuvel	@
874*a1c510d0SArd Biesheuvel	@ We've just detected an overflow. We need to load the address of this
875*a1c510d0SArd Biesheuvel	@ CPU's overflow stack into the stack pointer register. We have only one
876*a1c510d0SArd Biesheuvel	@ scratch register so let's use a sequence of ADDs including one
877*a1c510d0SArd Biesheuvel	@ involving the PC, and decorate them with PC-relative group
878*a1c510d0SArd Biesheuvel	@ relocations. As these are ARM only, switch to ARM mode first.
879*a1c510d0SArd Biesheuvel	@
880*a1c510d0SArd Biesheuvel	@ We enter here with IP clobbered and its value stashed on the mode
881*a1c510d0SArd Biesheuvel	@ stack.
882*a1c510d0SArd Biesheuvel	@
883*a1c510d0SArd BiesheuvelTHUMB(	bx	pc		)
884*a1c510d0SArd BiesheuvelTHUMB(	nop			)
885*a1c510d0SArd BiesheuvelTHUMB(	.arm			)
886*a1c510d0SArd Biesheuvel	mrc	p15, 0, ip, c13, c0, 4		@ Get per-CPU offset
887*a1c510d0SArd Biesheuvel
888*a1c510d0SArd Biesheuvel	.globl	overflow_stack_ptr
889*a1c510d0SArd Biesheuvel	.reloc	0f, R_ARM_ALU_PC_G0_NC, overflow_stack_ptr
890*a1c510d0SArd Biesheuvel	.reloc	1f, R_ARM_ALU_PC_G1_NC, overflow_stack_ptr
891*a1c510d0SArd Biesheuvel	.reloc	2f, R_ARM_LDR_PC_G2, overflow_stack_ptr
892*a1c510d0SArd Biesheuvel	add	ip, ip, pc
893*a1c510d0SArd Biesheuvel0:	add	ip, ip, #-4
894*a1c510d0SArd Biesheuvel1:	add	ip, ip, #0
895*a1c510d0SArd Biesheuvel2:	ldr	ip, [ip, #4]
896*a1c510d0SArd Biesheuvel
897*a1c510d0SArd Biesheuvel	str	sp, [ip, #-4]!			@ Preserve original SP value
898*a1c510d0SArd Biesheuvel	mov	sp, ip				@ Switch to overflow stack
899*a1c510d0SArd Biesheuvel	pop	{ip}				@ Original SP in IP
900*a1c510d0SArd Biesheuvel
901*a1c510d0SArd Biesheuvel#if defined(CONFIG_UNWINDER_FRAME_POINTER) && defined(CONFIG_CC_IS_GCC)
902*a1c510d0SArd Biesheuvel	mov	ip, ip				@ mov expected by unwinder
903*a1c510d0SArd Biesheuvel	push	{fp, ip, lr, pc}		@ GCC flavor frame record
904*a1c510d0SArd Biesheuvel#else
905*a1c510d0SArd Biesheuvel	str	ip, [sp, #-8]!			@ store original SP
906*a1c510d0SArd Biesheuvel	push	{fpreg, lr}			@ Clang flavor frame record
907*a1c510d0SArd Biesheuvel#endif
908*a1c510d0SArd BiesheuvelUNWIND( ldr	ip, [r0, #4]	)		@ load exception LR
909*a1c510d0SArd BiesheuvelUNWIND( str	ip, [sp, #12]	)		@ store in the frame record
910*a1c510d0SArd Biesheuvel	ldr	ip, [r0, #12]			@ reload IP
911*a1c510d0SArd Biesheuvel
912*a1c510d0SArd Biesheuvel	@ Store the original GPRs to the new stack.
913*a1c510d0SArd Biesheuvel	svc_entry uaccess=0, overflow_check=0
914*a1c510d0SArd Biesheuvel
915*a1c510d0SArd BiesheuvelUNWIND( .save   {sp, pc}	)
916*a1c510d0SArd BiesheuvelUNWIND( .save   {fpreg, lr}	)
917*a1c510d0SArd BiesheuvelUNWIND( .setfp  fpreg, sp	)
918*a1c510d0SArd Biesheuvel
919*a1c510d0SArd Biesheuvel	ldr	fpreg, [sp, #S_SP]		@ Add our frame record
920*a1c510d0SArd Biesheuvel						@ to the linked list
921*a1c510d0SArd Biesheuvel#if defined(CONFIG_UNWINDER_FRAME_POINTER) && defined(CONFIG_CC_IS_GCC)
922*a1c510d0SArd Biesheuvel	ldr	r1, [fp, #4]			@ reload SP at entry
923*a1c510d0SArd Biesheuvel	add	fp, fp, #12
924*a1c510d0SArd Biesheuvel#else
925*a1c510d0SArd Biesheuvel	ldr	r1, [fpreg, #8]
926*a1c510d0SArd Biesheuvel#endif
927*a1c510d0SArd Biesheuvel	str	r1, [sp, #S_SP]			@ store in pt_regs
928*a1c510d0SArd Biesheuvel
929*a1c510d0SArd Biesheuvel	@ Stash the regs for handle_bad_stack
930*a1c510d0SArd Biesheuvel	mov	r0, sp
931*a1c510d0SArd Biesheuvel
932*a1c510d0SArd Biesheuvel	@ Time to die
933*a1c510d0SArd Biesheuvel	bl	handle_bad_stack
934*a1c510d0SArd Biesheuvel	nop
935*a1c510d0SArd BiesheuvelUNWIND( .fnend			)
936*a1c510d0SArd BiesheuvelENDPROC(__bad_stack)
937*a1c510d0SArd Biesheuvel#endif
938*a1c510d0SArd Biesheuvel
9391da177e4SLinus Torvalds	__INIT
9402d2669b6SNicolas Pitre
9412d2669b6SNicolas Pitre/*
9422d2669b6SNicolas Pitre * User helpers.
9432d2669b6SNicolas Pitre *
9442d2669b6SNicolas Pitre * Each segment is 32-byte aligned and will be moved to the top of the high
9452d2669b6SNicolas Pitre * vector page.  New segments (if ever needed) must be added in front of
9462d2669b6SNicolas Pitre * existing ones.  This mechanism should be used only for things that are
9472d2669b6SNicolas Pitre * really small and justified, and not be abused freely.
9482d2669b6SNicolas Pitre *
949dc7a12bdSMauro Carvalho Chehab * See Documentation/arm/kernel_user_helpers.rst for formal definitions.
9502d2669b6SNicolas Pitre */
951b86040a5SCatalin Marinas THUMB(	.arm	)
9522d2669b6SNicolas Pitre
953ba9b5d76SNicolas Pitre	.macro	usr_ret, reg
954ba9b5d76SNicolas Pitre#ifdef CONFIG_ARM_THUMB
955ba9b5d76SNicolas Pitre	bx	\reg
956ba9b5d76SNicolas Pitre#else
9576ebbf2ceSRussell King	ret	\reg
958ba9b5d76SNicolas Pitre#endif
959ba9b5d76SNicolas Pitre	.endm
960ba9b5d76SNicolas Pitre
9615b43e7a3SRussell King	.macro	kuser_pad, sym, size
9625b43e7a3SRussell King	.if	(. - \sym) & 3
9635b43e7a3SRussell King	.rept	4 - (. - \sym) & 3
9645b43e7a3SRussell King	.byte	0
9655b43e7a3SRussell King	.endr
9665b43e7a3SRussell King	.endif
9675b43e7a3SRussell King	.rept	(\size - (. - \sym)) / 4
9685b43e7a3SRussell King	.word	0xe7fddef1
9695b43e7a3SRussell King	.endr
9705b43e7a3SRussell King	.endm
9715b43e7a3SRussell King
972f6f91b0dSRussell King#ifdef CONFIG_KUSER_HELPERS
9732d2669b6SNicolas Pitre	.align	5
9742d2669b6SNicolas Pitre	.globl	__kuser_helper_start
9752d2669b6SNicolas Pitre__kuser_helper_start:
9762d2669b6SNicolas Pitre
9772d2669b6SNicolas Pitre/*
97840fb79c8SNicolas Pitre * Due to the length of some sequences, __kuser_cmpxchg64 spans 2 regular
97940fb79c8SNicolas Pitre * kuser "slots", therefore 0xffff0f80 is not used as a valid entry point.
9807c612bfdSNicolas Pitre */
9817c612bfdSNicolas Pitre
98240fb79c8SNicolas Pitre__kuser_cmpxchg64:				@ 0xffff0f60
98340fb79c8SNicolas Pitre
984db695c05SRussell King#if defined(CONFIG_CPU_32v6K)
98540fb79c8SNicolas Pitre
98640fb79c8SNicolas Pitre	stmfd	sp!, {r4, r5, r6, r7}
98740fb79c8SNicolas Pitre	ldrd	r4, r5, [r0]			@ load old val
98840fb79c8SNicolas Pitre	ldrd	r6, r7, [r1]			@ load new val
98940fb79c8SNicolas Pitre	smp_dmb	arm
99040fb79c8SNicolas Pitre1:	ldrexd	r0, r1, [r2]			@ load current val
99140fb79c8SNicolas Pitre	eors	r3, r0, r4			@ compare with oldval (1)
992e44fc388SStefan Agner	eorseq	r3, r1, r5			@ compare with oldval (2)
99340fb79c8SNicolas Pitre	strexdeq r3, r6, r7, [r2]		@ store newval if eq
99440fb79c8SNicolas Pitre	teqeq	r3, #1				@ success?
99540fb79c8SNicolas Pitre	beq	1b				@ if no then retry
99640fb79c8SNicolas Pitre	smp_dmb	arm
99740fb79c8SNicolas Pitre	rsbs	r0, r3, #0			@ set returned val and C flag
99840fb79c8SNicolas Pitre	ldmfd	sp!, {r4, r5, r6, r7}
9995a97d0aeSWill Deacon	usr_ret	lr
100040fb79c8SNicolas Pitre
100140fb79c8SNicolas Pitre#elif !defined(CONFIG_SMP)
100240fb79c8SNicolas Pitre
100340fb79c8SNicolas Pitre#ifdef CONFIG_MMU
100440fb79c8SNicolas Pitre
100540fb79c8SNicolas Pitre	/*
100640fb79c8SNicolas Pitre	 * The only thing that can break atomicity in this cmpxchg64
100740fb79c8SNicolas Pitre	 * implementation is either an IRQ or a data abort exception
100840fb79c8SNicolas Pitre	 * causing another process/thread to be scheduled in the middle of
100940fb79c8SNicolas Pitre	 * the critical sequence.  The same strategy as for cmpxchg is used.
101040fb79c8SNicolas Pitre	 */
101140fb79c8SNicolas Pitre	stmfd	sp!, {r4, r5, r6, lr}
101240fb79c8SNicolas Pitre	ldmia	r0, {r4, r5}			@ load old val
101340fb79c8SNicolas Pitre	ldmia	r1, {r6, lr}			@ load new val
101440fb79c8SNicolas Pitre1:	ldmia	r2, {r0, r1}			@ load current val
101540fb79c8SNicolas Pitre	eors	r3, r0, r4			@ compare with oldval (1)
1016e44fc388SStefan Agner	eorseq	r3, r1, r5			@ compare with oldval (2)
1017e44fc388SStefan Agner2:	stmiaeq	r2, {r6, lr}			@ store newval if eq
101840fb79c8SNicolas Pitre	rsbs	r0, r3, #0			@ set return val and C flag
101940fb79c8SNicolas Pitre	ldmfd	sp!, {r4, r5, r6, pc}
102040fb79c8SNicolas Pitre
102140fb79c8SNicolas Pitre	.text
102240fb79c8SNicolas Pitrekuser_cmpxchg64_fixup:
102340fb79c8SNicolas Pitre	@ Called from kuser_cmpxchg_fixup.
10243ad55155SRussell King	@ r4 = address of interrupted insn (must be preserved).
102540fb79c8SNicolas Pitre	@ sp = saved regs. r7 and r8 are clobbered.
102640fb79c8SNicolas Pitre	@ 1b = first critical insn, 2b = last critical insn.
10273ad55155SRussell King	@ If r4 >= 1b and r4 <= 2b then saved pc_usr is set to 1b.
102840fb79c8SNicolas Pitre	mov	r7, #0xffff0fff
102940fb79c8SNicolas Pitre	sub	r7, r7, #(0xffff0fff - (0xffff0f60 + (1b - __kuser_cmpxchg64)))
10303ad55155SRussell King	subs	r8, r4, r7
1031e44fc388SStefan Agner	rsbscs	r8, r8, #(2b - 1b)
103240fb79c8SNicolas Pitre	strcs	r7, [sp, #S_PC]
103340fb79c8SNicolas Pitre#if __LINUX_ARM_ARCH__ < 6
103440fb79c8SNicolas Pitre	bcc	kuser_cmpxchg32_fixup
103540fb79c8SNicolas Pitre#endif
10366ebbf2ceSRussell King	ret	lr
103740fb79c8SNicolas Pitre	.previous
103840fb79c8SNicolas Pitre
103940fb79c8SNicolas Pitre#else
104040fb79c8SNicolas Pitre#warning "NPTL on non MMU needs fixing"
104140fb79c8SNicolas Pitre	mov	r0, #-1
104240fb79c8SNicolas Pitre	adds	r0, r0, #0
104340fb79c8SNicolas Pitre	usr_ret	lr
104440fb79c8SNicolas Pitre#endif
104540fb79c8SNicolas Pitre
104640fb79c8SNicolas Pitre#else
104740fb79c8SNicolas Pitre#error "incoherent kernel configuration"
104840fb79c8SNicolas Pitre#endif
104940fb79c8SNicolas Pitre
10505b43e7a3SRussell King	kuser_pad __kuser_cmpxchg64, 64
105140fb79c8SNicolas Pitre
10527c612bfdSNicolas Pitre__kuser_memory_barrier:				@ 0xffff0fa0
1053ed3768a8SDave Martin	smp_dmb	arm
1054ba9b5d76SNicolas Pitre	usr_ret	lr
10557c612bfdSNicolas Pitre
10565b43e7a3SRussell King	kuser_pad __kuser_memory_barrier, 32
10577c612bfdSNicolas Pitre
10582d2669b6SNicolas Pitre__kuser_cmpxchg:				@ 0xffff0fc0
10592d2669b6SNicolas Pitre
1060db695c05SRussell King#if __LINUX_ARM_ARCH__ < 6
10612d2669b6SNicolas Pitre
106249bca4c2SNicolas Pitre#ifdef CONFIG_MMU
1063b49c0f24SNicolas Pitre
1064b49c0f24SNicolas Pitre	/*
1065b49c0f24SNicolas Pitre	 * The only thing that can break atomicity in this cmpxchg
1066b49c0f24SNicolas Pitre	 * implementation is either an IRQ or a data abort exception
1067b49c0f24SNicolas Pitre	 * causing another process/thread to be scheduled in the middle
1068b49c0f24SNicolas Pitre	 * of the critical sequence.  To prevent this, code is added to
1069b49c0f24SNicolas Pitre	 * the IRQ and data abort exception handlers to set the pc back
1070b49c0f24SNicolas Pitre	 * to the beginning of the critical section if it is found to be
1071b49c0f24SNicolas Pitre	 * within that critical section (see kuser_cmpxchg_fixup).
1072b49c0f24SNicolas Pitre	 */
1073b49c0f24SNicolas Pitre1:	ldr	r3, [r2]			@ load current val
1074b49c0f24SNicolas Pitre	subs	r3, r3, r0			@ compare with oldval
1075b49c0f24SNicolas Pitre2:	streq	r1, [r2]			@ store newval if eq
1076b49c0f24SNicolas Pitre	rsbs	r0, r3, #0			@ set return val and C flag
1077b49c0f24SNicolas Pitre	usr_ret	lr
1078b49c0f24SNicolas Pitre
1079b49c0f24SNicolas Pitre	.text
108040fb79c8SNicolas Pitrekuser_cmpxchg32_fixup:
1081b49c0f24SNicolas Pitre	@ Called from kuser_cmpxchg_check macro.
1082b059bdc3SRussell King	@ r4 = address of interrupted insn (must be preserved).
1083b49c0f24SNicolas Pitre	@ sp = saved regs. r7 and r8 are clobbered.
1084b49c0f24SNicolas Pitre	@ 1b = first critical insn, 2b = last critical insn.
1085b059bdc3SRussell King	@ If r4 >= 1b and r4 <= 2b then saved pc_usr is set to 1b.
1086b49c0f24SNicolas Pitre	mov	r7, #0xffff0fff
1087b49c0f24SNicolas Pitre	sub	r7, r7, #(0xffff0fff - (0xffff0fc0 + (1b - __kuser_cmpxchg)))
1088b059bdc3SRussell King	subs	r8, r4, r7
1089e44fc388SStefan Agner	rsbscs	r8, r8, #(2b - 1b)
1090b49c0f24SNicolas Pitre	strcs	r7, [sp, #S_PC]
10916ebbf2ceSRussell King	ret	lr
1092b49c0f24SNicolas Pitre	.previous
1093b49c0f24SNicolas Pitre
109449bca4c2SNicolas Pitre#else
109549bca4c2SNicolas Pitre#warning "NPTL on non MMU needs fixing"
109649bca4c2SNicolas Pitre	mov	r0, #-1
109749bca4c2SNicolas Pitre	adds	r0, r0, #0
1098ba9b5d76SNicolas Pitre	usr_ret	lr
1099b49c0f24SNicolas Pitre#endif
11002d2669b6SNicolas Pitre
11012d2669b6SNicolas Pitre#else
11022d2669b6SNicolas Pitre
1103ed3768a8SDave Martin	smp_dmb	arm
1104b49c0f24SNicolas Pitre1:	ldrex	r3, [r2]
11052d2669b6SNicolas Pitre	subs	r3, r3, r0
11062d2669b6SNicolas Pitre	strexeq	r3, r1, [r2]
1107b49c0f24SNicolas Pitre	teqeq	r3, #1
1108b49c0f24SNicolas Pitre	beq	1b
11092d2669b6SNicolas Pitre	rsbs	r0, r3, #0
1110b49c0f24SNicolas Pitre	/* beware -- each __kuser slot must be 8 instructions max */
1111f00ec48fSRussell King	ALT_SMP(b	__kuser_memory_barrier)
1112f00ec48fSRussell King	ALT_UP(usr_ret	lr)
11132d2669b6SNicolas Pitre
11142d2669b6SNicolas Pitre#endif
11152d2669b6SNicolas Pitre
11165b43e7a3SRussell King	kuser_pad __kuser_cmpxchg, 32
11172d2669b6SNicolas Pitre
11182d2669b6SNicolas Pitre__kuser_get_tls:				@ 0xffff0fe0
1119f159f4edSTony Lindgren	ldr	r0, [pc, #(16 - 8)]	@ read TLS, set in kuser_get_tls_init
1120ba9b5d76SNicolas Pitre	usr_ret	lr
1121f159f4edSTony Lindgren	mrc	p15, 0, r0, c13, c0, 3	@ 0xffff0fe8 hardware TLS code
11225b43e7a3SRussell King	kuser_pad __kuser_get_tls, 16
11235b43e7a3SRussell King	.rep	3
1124f159f4edSTony Lindgren	.word	0			@ 0xffff0ff0 software TLS value, then
1125f159f4edSTony Lindgren	.endr				@ pad up to __kuser_helper_version
11262d2669b6SNicolas Pitre
11272d2669b6SNicolas Pitre__kuser_helper_version:				@ 0xffff0ffc
11282d2669b6SNicolas Pitre	.word	((__kuser_helper_end - __kuser_helper_start) >> 5)
11292d2669b6SNicolas Pitre
11302d2669b6SNicolas Pitre	.globl	__kuser_helper_end
11312d2669b6SNicolas Pitre__kuser_helper_end:
11322d2669b6SNicolas Pitre
1133f6f91b0dSRussell King#endif
1134f6f91b0dSRussell King
1135b86040a5SCatalin Marinas THUMB(	.thumb	)
11362d2669b6SNicolas Pitre
11371da177e4SLinus Torvalds/*
11381da177e4SLinus Torvalds * Vector stubs.
11391da177e4SLinus Torvalds *
114019accfd3SRussell King * This code is copied to 0xffff1000 so we can use branches in the
114119accfd3SRussell King * vectors, rather than ldr's.  Note that this code must not exceed
114219accfd3SRussell King * a page size.
11431da177e4SLinus Torvalds *
11441da177e4SLinus Torvalds * Common stub entry macro:
11451da177e4SLinus Torvalds *   Enter in IRQ mode, spsr = SVC/USR CPSR, lr = SVC/USR PC
1146ccea7a19SRussell King *
1147ccea7a19SRussell King * SP points to a minimal amount of processor-private memory, the address
1148ccea7a19SRussell King * of which is copied into r0 for the mode specific abort handler.
11491da177e4SLinus Torvalds */
1150b7ec4795SNicolas Pitre	.macro	vector_stub, name, mode, correction=0
11511da177e4SLinus Torvalds	.align	5
11521da177e4SLinus Torvalds
11531da177e4SLinus Torvaldsvector_\name:
11541da177e4SLinus Torvalds	.if \correction
11551da177e4SLinus Torvalds	sub	lr, lr, #\correction
11561da177e4SLinus Torvalds	.endif
11571da177e4SLinus Torvalds
1158ccea7a19SRussell King	@
1159ccea7a19SRussell King	@ Save r0, lr_<exception> (parent PC) and spsr_<exception>
1160ccea7a19SRussell King	@ (parent CPSR)
1161ccea7a19SRussell King	@
1162ccea7a19SRussell King	stmia	sp, {r0, lr}		@ save r0, lr
1163ccea7a19SRussell King	mrs	lr, spsr
1164ccea7a19SRussell King	str	lr, [sp, #8]		@ save spsr
1165ccea7a19SRussell King
1166ccea7a19SRussell King	@
1167ccea7a19SRussell King	@ Prepare for SVC32 mode.  IRQs remain disabled.
1168ccea7a19SRussell King	@
1169ccea7a19SRussell King	mrs	r0, cpsr
1170b86040a5SCatalin Marinas	eor	r0, r0, #(\mode ^ SVC_MODE | PSR_ISETSTATE)
1171ccea7a19SRussell King	msr	spsr_cxsf, r0
1172ccea7a19SRussell King
1173ccea7a19SRussell King	@
1174ccea7a19SRussell King	@ the branch table must immediately follow this code
1175ccea7a19SRussell King	@
1176ccea7a19SRussell King	and	lr, lr, #0x0f
1177b86040a5SCatalin Marinas THUMB(	adr	r0, 1f			)
1178b86040a5SCatalin Marinas THUMB(	ldr	lr, [r0, lr, lsl #2]	)
1179b7ec4795SNicolas Pitre	mov	r0, sp
1180b86040a5SCatalin Marinas ARM(	ldr	lr, [pc, lr, lsl #2]	)
1181ccea7a19SRussell King	movs	pc, lr			@ branch to handler in SVC mode
118293ed3970SCatalin MarinasENDPROC(vector_\name)
118388987ef9SCatalin Marinas
118488987ef9SCatalin Marinas	.align	2
118588987ef9SCatalin Marinas	@ handler addresses follow this label
118688987ef9SCatalin Marinas1:
11871da177e4SLinus Torvalds	.endm
11881da177e4SLinus Torvalds
1189b9b32bf7SRussell King	.section .stubs, "ax", %progbits
119019accfd3SRussell King	@ This must be the first word
119119accfd3SRussell King	.word	vector_swi
119219accfd3SRussell King
119319accfd3SRussell Kingvector_rst:
119419accfd3SRussell King ARM(	swi	SYS_ERROR0	)
119519accfd3SRussell King THUMB(	svc	#0		)
119619accfd3SRussell King THUMB(	nop			)
119719accfd3SRussell King	b	vector_und
119819accfd3SRussell King
11991da177e4SLinus Torvalds/*
12001da177e4SLinus Torvalds * Interrupt dispatcher
12011da177e4SLinus Torvalds */
1202b7ec4795SNicolas Pitre	vector_stub	irq, IRQ_MODE, 4
12031da177e4SLinus Torvalds
12041da177e4SLinus Torvalds	.long	__irq_usr			@  0  (USR_26 / USR_32)
12051da177e4SLinus Torvalds	.long	__irq_invalid			@  1  (FIQ_26 / FIQ_32)
12061da177e4SLinus Torvalds	.long	__irq_invalid			@  2  (IRQ_26 / IRQ_32)
12071da177e4SLinus Torvalds	.long	__irq_svc			@  3  (SVC_26 / SVC_32)
12081da177e4SLinus Torvalds	.long	__irq_invalid			@  4
12091da177e4SLinus Torvalds	.long	__irq_invalid			@  5
12101da177e4SLinus Torvalds	.long	__irq_invalid			@  6
12111da177e4SLinus Torvalds	.long	__irq_invalid			@  7
12121da177e4SLinus Torvalds	.long	__irq_invalid			@  8
12131da177e4SLinus Torvalds	.long	__irq_invalid			@  9
12141da177e4SLinus Torvalds	.long	__irq_invalid			@  a
12151da177e4SLinus Torvalds	.long	__irq_invalid			@  b
12161da177e4SLinus Torvalds	.long	__irq_invalid			@  c
12171da177e4SLinus Torvalds	.long	__irq_invalid			@  d
12181da177e4SLinus Torvalds	.long	__irq_invalid			@  e
12191da177e4SLinus Torvalds	.long	__irq_invalid			@  f
12201da177e4SLinus Torvalds
12211da177e4SLinus Torvalds/*
12221da177e4SLinus Torvalds * Data abort dispatcher
12231da177e4SLinus Torvalds * Enter in ABT mode, spsr = USR CPSR, lr = USR PC
12241da177e4SLinus Torvalds */
1225b7ec4795SNicolas Pitre	vector_stub	dabt, ABT_MODE, 8
12261da177e4SLinus Torvalds
12271da177e4SLinus Torvalds	.long	__dabt_usr			@  0  (USR_26 / USR_32)
12281da177e4SLinus Torvalds	.long	__dabt_invalid			@  1  (FIQ_26 / FIQ_32)
12291da177e4SLinus Torvalds	.long	__dabt_invalid			@  2  (IRQ_26 / IRQ_32)
12301da177e4SLinus Torvalds	.long	__dabt_svc			@  3  (SVC_26 / SVC_32)
12311da177e4SLinus Torvalds	.long	__dabt_invalid			@  4
12321da177e4SLinus Torvalds	.long	__dabt_invalid			@  5
12331da177e4SLinus Torvalds	.long	__dabt_invalid			@  6
12341da177e4SLinus Torvalds	.long	__dabt_invalid			@  7
12351da177e4SLinus Torvalds	.long	__dabt_invalid			@  8
12361da177e4SLinus Torvalds	.long	__dabt_invalid			@  9
12371da177e4SLinus Torvalds	.long	__dabt_invalid			@  a
12381da177e4SLinus Torvalds	.long	__dabt_invalid			@  b
12391da177e4SLinus Torvalds	.long	__dabt_invalid			@  c
12401da177e4SLinus Torvalds	.long	__dabt_invalid			@  d
12411da177e4SLinus Torvalds	.long	__dabt_invalid			@  e
12421da177e4SLinus Torvalds	.long	__dabt_invalid			@  f
12431da177e4SLinus Torvalds
12441da177e4SLinus Torvalds/*
12451da177e4SLinus Torvalds * Prefetch abort dispatcher
12461da177e4SLinus Torvalds * Enter in ABT mode, spsr = USR CPSR, lr = USR PC
12471da177e4SLinus Torvalds */
1248b7ec4795SNicolas Pitre	vector_stub	pabt, ABT_MODE, 4
12491da177e4SLinus Torvalds
12501da177e4SLinus Torvalds	.long	__pabt_usr			@  0 (USR_26 / USR_32)
12511da177e4SLinus Torvalds	.long	__pabt_invalid			@  1 (FIQ_26 / FIQ_32)
12521da177e4SLinus Torvalds	.long	__pabt_invalid			@  2 (IRQ_26 / IRQ_32)
12531da177e4SLinus Torvalds	.long	__pabt_svc			@  3 (SVC_26 / SVC_32)
12541da177e4SLinus Torvalds	.long	__pabt_invalid			@  4
12551da177e4SLinus Torvalds	.long	__pabt_invalid			@  5
12561da177e4SLinus Torvalds	.long	__pabt_invalid			@  6
12571da177e4SLinus Torvalds	.long	__pabt_invalid			@  7
12581da177e4SLinus Torvalds	.long	__pabt_invalid			@  8
12591da177e4SLinus Torvalds	.long	__pabt_invalid			@  9
12601da177e4SLinus Torvalds	.long	__pabt_invalid			@  a
12611da177e4SLinus Torvalds	.long	__pabt_invalid			@  b
12621da177e4SLinus Torvalds	.long	__pabt_invalid			@  c
12631da177e4SLinus Torvalds	.long	__pabt_invalid			@  d
12641da177e4SLinus Torvalds	.long	__pabt_invalid			@  e
12651da177e4SLinus Torvalds	.long	__pabt_invalid			@  f
12661da177e4SLinus Torvalds
12671da177e4SLinus Torvalds/*
12681da177e4SLinus Torvalds * Undef instr entry dispatcher
12691da177e4SLinus Torvalds * Enter in UND mode, spsr = SVC/USR CPSR, lr = SVC/USR PC
12701da177e4SLinus Torvalds */
1271b7ec4795SNicolas Pitre	vector_stub	und, UND_MODE
12721da177e4SLinus Torvalds
12731da177e4SLinus Torvalds	.long	__und_usr			@  0 (USR_26 / USR_32)
12741da177e4SLinus Torvalds	.long	__und_invalid			@  1 (FIQ_26 / FIQ_32)
12751da177e4SLinus Torvalds	.long	__und_invalid			@  2 (IRQ_26 / IRQ_32)
12761da177e4SLinus Torvalds	.long	__und_svc			@  3 (SVC_26 / SVC_32)
12771da177e4SLinus Torvalds	.long	__und_invalid			@  4
12781da177e4SLinus Torvalds	.long	__und_invalid			@  5
12791da177e4SLinus Torvalds	.long	__und_invalid			@  6
12801da177e4SLinus Torvalds	.long	__und_invalid			@  7
12811da177e4SLinus Torvalds	.long	__und_invalid			@  8
12821da177e4SLinus Torvalds	.long	__und_invalid			@  9
12831da177e4SLinus Torvalds	.long	__und_invalid			@  a
12841da177e4SLinus Torvalds	.long	__und_invalid			@  b
12851da177e4SLinus Torvalds	.long	__und_invalid			@  c
12861da177e4SLinus Torvalds	.long	__und_invalid			@  d
12871da177e4SLinus Torvalds	.long	__und_invalid			@  e
12881da177e4SLinus Torvalds	.long	__und_invalid			@  f
12891da177e4SLinus Torvalds
12901da177e4SLinus Torvalds	.align	5
12911da177e4SLinus Torvalds
12921da177e4SLinus Torvalds/*=============================================================================
129319accfd3SRussell King * Address exception handler
129419accfd3SRussell King *-----------------------------------------------------------------------------
129519accfd3SRussell King * These aren't too critical.
129619accfd3SRussell King * (they're not supposed to happen, and won't happen in 32-bit data mode).
129719accfd3SRussell King */
129819accfd3SRussell King
129919accfd3SRussell Kingvector_addrexcptn:
130019accfd3SRussell King	b	vector_addrexcptn
130119accfd3SRussell King
130219accfd3SRussell King/*=============================================================================
1303c0e7f7eeSDaniel Thompson * FIQ "NMI" handler
13041da177e4SLinus Torvalds *-----------------------------------------------------------------------------
1305c0e7f7eeSDaniel Thompson * Handle a FIQ using the SVC stack allowing FIQ act like NMI on x86
1306c0e7f7eeSDaniel Thompson * systems.
13071da177e4SLinus Torvalds */
1308c0e7f7eeSDaniel Thompson	vector_stub	fiq, FIQ_MODE, 4
1309c0e7f7eeSDaniel Thompson
1310c0e7f7eeSDaniel Thompson	.long	__fiq_usr			@  0  (USR_26 / USR_32)
1311c0e7f7eeSDaniel Thompson	.long	__fiq_svc			@  1  (FIQ_26 / FIQ_32)
1312c0e7f7eeSDaniel Thompson	.long	__fiq_svc			@  2  (IRQ_26 / IRQ_32)
1313c0e7f7eeSDaniel Thompson	.long	__fiq_svc			@  3  (SVC_26 / SVC_32)
1314c0e7f7eeSDaniel Thompson	.long	__fiq_svc			@  4
1315c0e7f7eeSDaniel Thompson	.long	__fiq_svc			@  5
1316c0e7f7eeSDaniel Thompson	.long	__fiq_svc			@  6
1317c0e7f7eeSDaniel Thompson	.long	__fiq_abt			@  7
1318c0e7f7eeSDaniel Thompson	.long	__fiq_svc			@  8
1319c0e7f7eeSDaniel Thompson	.long	__fiq_svc			@  9
1320c0e7f7eeSDaniel Thompson	.long	__fiq_svc			@  a
1321c0e7f7eeSDaniel Thompson	.long	__fiq_svc			@  b
1322c0e7f7eeSDaniel Thompson	.long	__fiq_svc			@  c
1323c0e7f7eeSDaniel Thompson	.long	__fiq_svc			@  d
1324c0e7f7eeSDaniel Thompson	.long	__fiq_svc			@  e
1325c0e7f7eeSDaniel Thompson	.long	__fiq_svc			@  f
13261da177e4SLinus Torvalds
132731b96caeSArd Biesheuvel	.globl	vector_fiq
1328e39e3f3eSRussell King
1329b9b32bf7SRussell King	.section .vectors, "ax", %progbits
1330b48da558SArd Biesheuvel.L__vectors_start:
1331b9b32bf7SRussell King	W(b)	vector_rst
1332b9b32bf7SRussell King	W(b)	vector_und
1333b48da558SArd Biesheuvel	W(ldr)	pc, .L__vectors_start + 0x1000
1334b9b32bf7SRussell King	W(b)	vector_pabt
1335b9b32bf7SRussell King	W(b)	vector_dabt
1336b9b32bf7SRussell King	W(b)	vector_addrexcptn
1337b9b32bf7SRussell King	W(b)	vector_irq
1338b9b32bf7SRussell King	W(b)	vector_fiq
13391da177e4SLinus Torvalds
13401da177e4SLinus Torvalds	.data
13411abd3502SRussell King	.align	2
13421da177e4SLinus Torvalds
13431da177e4SLinus Torvalds	.globl	cr_alignment
13441da177e4SLinus Torvaldscr_alignment:
13451da177e4SLinus Torvalds	.space	4
1346