xref: /freebsd/sys/arm/include/asmacros.h (revision 4f29da19bd44f0e99f021510460a81bf754c21d2)
1 /*	$NetBSD: frame.h,v 1.6 2003/10/05 19:44:58 matt Exp $	*/
2 
3 /*-
4  * Copyright (c) 1994-1997 Mark Brinicombe.
5  * Copyright (c) 1994 Brini.
6  * All rights reserved.
7  *
8  * This code is derived from software written for Brini by Mark Brinicombe
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by Brini.
21  * 4. The name of the company nor the name of the author may be used to
22  *    endorse or promote products derived from this software without specific
23  *    prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
26  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28  * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  * $FreeBSD$
38  */
39 
40 #ifndef	_MACHINE_ASMACROS_H_
41 #define	_MACHINE_ASMACROS_H_
42 
43 #ifdef _KERNEL
44 
45 #ifdef LOCORE
46 
47 /*
48  * ASM macros for pushing and pulling trapframes from the stack
49  *
50  * These macros are used to handle the irqframe and trapframe structures
51  * defined above.
52  */
53 
54 /*
55  * PUSHFRAME - macro to push a trap frame on the stack in the current mode
56  * Since the current mode is used, the SVC lr field is not defined.
57  *
58  * NOTE: r13 and r14 are stored separately as a work around for the
59  * SA110 rev 2 STM^ bug
60  */
61 
62 #define PUSHFRAME							   \
63 	str	lr, [sp, #-4]!;		/* Push the return address */	   \
64 	sub	sp, sp, #(4*17);	/* Adjust the stack pointer */	   \
65 	stmia	sp, {r0-r12};		/* Push the user mode registers */ \
66 	add	r0, sp, #(4*13);	/* Adjust the stack pointer */	   \
67 	stmia	r0, {r13-r14}^;		/* Push the user mode registers */ \
68         mov     r0, r0;                 /* NOP for previous instruction */ \
69 	mrs	r0, spsr_all;		/* Put the SPSR on the stack */	   \
70 	str	r0, [sp, #-4]!;
71 
72 /*
73  * PULLFRAME - macro to pull a trap frame from the stack in the current mode
74  * Since the current mode is used, the SVC lr field is ignored.
75  */
76 
77 #define PULLFRAME							   \
78         ldr     r0, [sp], #0x0004;      /* Get the SPSR from stack */	   \
79         msr     spsr_all, r0;						   \
80         ldmia   sp, {r0-r14}^;		/* Restore registers (usr mode) */ \
81         mov     r0, r0;                 /* NOP for previous instruction */ \
82 	add	sp, sp, #(4*17);	/* Adjust the stack pointer */	   \
83  	ldr	lr, [sp], #0x0004;	/* Pull the return address */
84 
85 /*
86  * PUSHFRAMEINSVC - macro to push a trap frame on the stack in SVC32 mode
87  * This should only be used if the processor is not currently in SVC32
88  * mode. The processor mode is switched to SVC mode and the trap frame is
89  * stored. The SVC lr field is used to store the previous value of
90  * lr in SVC mode.
91  *
92  * NOTE: r13 and r14 are stored separately as a work around for the
93  * SA110 rev 2 STM^ bug
94  */
95 
96 #define PUSHFRAMEINSVC							   \
97 	stmdb	sp, {r0-r3};		/* Save 4 registers */		   \
98 	mov	r0, lr;			/* Save xxx32 r14 */		   \
99 	mov	r1, sp;			/* Save xxx32 sp */		   \
100 	mrs	r3, spsr;		/* Save xxx32 spsr */		   \
101 	mrs     r2, cpsr; 		/* Get the CPSR */		   \
102 	bic     r2, r2, #(PSR_MODE);	/* Fix for SVC mode */		   \
103 	orr     r2, r2, #(PSR_SVC32_MODE);				   \
104 	msr     cpsr_c, r2;		/* Punch into SVC mode */	   \
105 	mov	r2, sp;			/* Save	SVC sp */		   \
106 	str	r0, [sp, #-4]!;		/* Push return address */	   \
107 	str	lr, [sp, #-4]!;		/* Push SVC lr */		   \
108 	str	r2, [sp, #-4]!;		/* Push SVC sp */		   \
109 	msr     spsr_all, r3;		/* Restore correct spsr */	   \
110 	ldmdb	r1, {r0-r3};		/* Restore 4 regs from xxx mode */ \
111 	sub	sp, sp, #(4*15);	/* Adjust the stack pointer */	   \
112 	stmia	sp, {r0-r12};		/* Push the user mode registers */ \
113 	add	r0, sp, #(4*13);	/* Adjust the stack pointer */	   \
114 	stmia	r0, {r13-r14}^;		/* Push the user mode registers */ \
115         mov     r0, r0;                 /* NOP for previous instruction */ \
116 	ldr	r5, =0xe0000004;	/* Check if there's any RAS */	   \
117 	ldr	r3, [r5];						   \
118 	cmp	r3, #0;			/* Is the update needed ? */	   \
119 	beq	1f;							   \
120 	ldr	lr, [r0, #16];						   \
121 	ldr	r1, =0xe0000008;					   \
122 	ldr	r4, [r1];		/* Get the end of the RAS */	   \
123 	mov	r2, #0;			/* Reset the magic addresses */	   \
124 	str	r2, [r5];						   \
125 	str	r2, [r1];						   \
126 	cmp	lr, r3;			/* Were we in the RAS ? */	   \
127 	blt	1f;							   \
128 	cmp	lr, r4;							   \
129 	strlt	r3, [r0, #16];		/* Yes, update the pc */	   \
130 	1:								   \
131 	mrs	r0, spsr_all;		/* Put the SPSR on the stack */	   \
132 	str	r0, [sp, #-4]!
133 
134 /*
135  * PULLFRAMEFROMSVCANDEXIT - macro to pull a trap frame from the stack
136  * in SVC32 mode and restore the saved processor mode and PC.
137  * This should be used when the SVC lr register needs to be restored on
138  * exit.
139  */
140 
141 #define PULLFRAMEFROMSVCANDEXIT						   \
142         ldr     r0, [sp], #0x0004;	/* Get the SPSR from stack */	   \
143         msr     spsr_all, r0;		/* restore SPSR */		   \
144         ldmia   sp, {r0-r14}^;		/* Restore registers (usr mode) */ \
145         mov     r0, r0;	  		/* NOP for previous instruction */ \
146 	add	sp, sp, #(4*15);	/* Adjust the stack pointer */	   \
147 	ldmia	sp, {sp, lr, pc}^	/* Restore lr and exit */
148 
149 #define	DATA(name) \
150 	.data ; \
151 	_ALIGN_DATA ; \
152 	.globl	name ; \
153 	.type	name, %object ; \
154 name:
155 
156 #define	EMPTY
157 
158 
159 #define	DO_AST								\
160 	ldr	r0, [sp]		/* Get the SPSR from stack */	;\
161 	mrs	r4, cpsr		/* save CPSR */			;\
162 	orr	r1, r4, #(I32_bit)					;\
163 	msr	cpsr_c, r1		/* Disable interrupts */	;\
164 	and	r0, r0, #(PSR_MODE)	/* Returning to USR mode? */	;\
165 	teq	r0, #(PSR_USR32_MODE)					;\
166 	bne	2f			/* Nope, get out now */		;\
167 	bic	r4, r4, #(I32_bit)					;\
168 1:	ldr	r5, .Lcurthread						;\
169 	ldr	r5, [r5]						;\
170 	ldr	r1, [r5, #(TD_FLAGS)]					;\
171 	and	r1, r1, #(TDF_ASTPENDING|TDF_NEEDRESCHED)		;\
172 	teq	r1, #0x00000000						;\
173 	beq	2f			/* Nope. Just bail */		;\
174 	msr	cpsr_c, r4		/* Restore interrupts */	;\
175 	mov	r0, sp							;\
176 	bl	_C_LABEL(ast)		/* ast(frame) */		;\
177 	orr	r0, r4, #(I32_bit)					;\
178 	msr	cpsr_c, r0						;\
179 	b	1b							;\
180 2:
181 
182 
183 #define	AST_LOCALS							;\
184 .Lcurthread:								;\
185 	.word	_C_LABEL(__pcpu) + PC_CURTHREAD
186 
187 #endif /* LOCORE */
188 
189 #endif /* _KERNEL */
190 
191 #endif /* !_MACHINE_ASMACROS_H_ */
192