xref: /openbsd-src/sys/arch/arm/include/frame.h (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: frame.h,v 1.4 2011/09/20 22:02:13 miod Exp $	*/
2 /*	$NetBSD: frame.h,v 1.9 2003/12/01 08:48:33 scw Exp $	*/
3 
4 /*
5  * Copyright (c) 1994-1997 Mark Brinicombe.
6  * Copyright (c) 1994 Brini.
7  * All rights reserved.
8  *
9  * This code is derived from software written for Brini by Mark Brinicombe
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by Brini.
22  * 4. The name of the company nor the name of the author may be used to
23  *    endorse or promote products derived from this software without specific
24  *    prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
27  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29  * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
30  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  * RiscBSD kernel project
39  *
40  * frame.h
41  *
42  * Stack frames structures
43  *
44  * Created      : 30/09/94
45  */
46 
47 #ifndef _ARM_FRAME_H_
48 #define _ARM_FRAME_H_
49 
50 #ifndef _LOCORE
51 
52 #include <sys/signal.h>
53 
54 /*
55  * Trap frame.  Pushed onto the kernel stack on a trap (synchronous exception).
56  */
57 
58 typedef struct trapframe {
59 	register_t tf_spsr;
60 	register_t tf_r0;
61 	register_t tf_r1;
62 	register_t tf_r2;
63 	register_t tf_r3;
64 	register_t tf_r4;
65 	register_t tf_r5;
66 	register_t tf_r6;
67 	register_t tf_r7;
68 	register_t tf_r8;
69 	register_t tf_r9;
70 	register_t tf_r10;
71 	register_t tf_r11;
72 	register_t tf_r12;
73 	register_t tf_usr_sp;
74 	register_t tf_usr_lr;
75 	register_t tf_svc_sp;
76 	register_t tf_svc_lr;
77 	register_t tf_pc;
78 } trapframe_t;
79 
80 /* Register numbers */
81 #define tf_r13 tf_usr_sp
82 #define tf_r14 tf_usr_lr
83 #define tf_r15 tf_pc
84 
85 /*
86  * Signal frame.  Pushed onto user stack before calling sigcode.
87  */
88 
89 struct sigframe {
90 	int	sf_signum;
91 	siginfo_t *sf_sip;
92 	struct	sigcontext *sf_scp;
93 	sig_t	sf_handler;
94 	struct	sigcontext sf_sc;
95 	siginfo_t sf_si;
96 };
97 
98 /* the pointers are used in the trampoline code to locate the ucontext */
99 #if 0
100 struct sigframe_siginfo {
101 	siginfo_t	sf_si;		/* actual saved siginfo */
102 	ucontext_t	sf_uc;		/* actual saved ucontext */
103 };
104 #endif
105 
106 #if 0
107 #ifdef _KERNEL
108 void sendsig_sigcontext(const ksiginfo_t *, const sigset_t *);
109 #endif
110 #endif
111 
112 #endif /* _LOCORE */
113 
114 #ifndef _LOCORE
115 
116 /*
117  * System stack frames.
118  */
119 
120 typedef struct irqframe {
121 	unsigned int if_spsr;
122 	unsigned int if_r0;
123 	unsigned int if_r1;
124 	unsigned int if_r2;
125 	unsigned int if_r3;
126 	unsigned int if_r4;
127 	unsigned int if_r5;
128 	unsigned int if_r6;
129 	unsigned int if_r7;
130 	unsigned int if_r8;
131 	unsigned int if_r9;
132 	unsigned int if_r10;
133 	unsigned int if_r11;
134 	unsigned int if_r12;
135 	unsigned int if_usr_sp;
136 	unsigned int if_usr_lr;
137 	unsigned int if_svc_sp;
138 	unsigned int if_svc_lr;
139 	unsigned int if_pc;
140 } irqframe_t;
141 
142 #define clockframe irqframe
143 
144 /*
145  * Switch frame
146  */
147 
148 struct switchframe {
149 	u_int	sf_r4;
150 	u_int	sf_r5;
151 	u_int	sf_r6;
152 	u_int	sf_r7;
153 	u_int	sf_pc;
154 };
155 
156 /*
157  * Stack frame. Used during stack traces (db_trace.c)
158  */
159 struct frame {
160 	u_int	fr_fp;
161 	u_int	fr_sp;
162 	u_int	fr_lr;
163 	u_int	fr_pc;
164 };
165 
166 #else /* _LOCORE */
167 
168 #define	AST_LOCALS							 \
169 .Laflt_astpending:							;\
170 	.word	_C_LABEL(astpending)
171 
172 #define	DO_AST								 \
173 	ldr	r0, [sp]		/* Get the SPSR from stack */	;\
174 	mrs	r4, cpsr		/* save CPSR */			;\
175 	and	r0, r0, #(PSR_MODE)	/* Returning to USR mode? */	;\
176 	teq	r0, #(PSR_USR32_MODE)					;\
177 	ldreq	r5, .Laflt_astpending					;\
178 	bne	2f			/* Nope, get out now */		;\
179 	bic	r4, r4, #(I32_bit)					;\
180 1:	orr	r0, r4, #(I32_bit)	/* Disable IRQs */		;\
181 	msr	cpsr_c, r0						;\
182 	ldr	r1, [r5]		/* Pending AST? */		;\
183 	teq	r1, #0x00000000						;\
184 	beq	2f			/* Nope. Just bail */		;\
185 	mov	r1, #0x00000000						;\
186 	str	r1, [r5]		/* Clear astpending */		;\
187 	msr	cpsr_c, r4		/* Restore interrupts */	;\
188 	mov	r0, sp							;\
189 	adr	lr, 1b							;\
190 	b	_C_LABEL(ast)		/* ast(frame) */		;\
191 2:
192 
193 /*
194  * ASM macros for pushing and pulling trapframes from the stack
195  *
196  * These macros are used to handle the irqframe and trapframe structures
197  * defined above.
198  */
199 
200 /*
201  * PUSHFRAME - macro to push a trap frame on the stack in the current mode
202  * Since the current mode is used, the SVC lr field is not defined.
203  */
204 
205 #define PUSHFRAME							   \
206 	str	lr, [sp, #-4]!;		/* Push the return address */	   \
207 	sub	sp, sp, #(4*17);	/* Adjust the stack pointer */	   \
208 	stmia	sp, {r0-r14}^;		/* Push the user mode registers */ \
209         mov     r0, r0;                 /* NOP for previous instruction */ \
210 	mrs	r0, spsr_all;		/* Put the SPSR on the stack */	   \
211 	str	r0, [sp, #-4]!
212 
213 /*
214  * PULLFRAME - macro to pull a trap frame from the stack in the current mode
215  * Since the current mode is used, the SVC lr field is ignored.
216  */
217 
218 #define PULLFRAME							   \
219         ldr     r0, [sp], #0x0004;      /* Get the SPSR from stack */	   \
220         msr     spsr_all, r0;						   \
221         ldmia   sp, {r0-r14}^;		/* Restore registers (usr mode) */ \
222         mov     r0, r0;                 /* NOP for previous instruction */ \
223 	add	sp, sp, #(4*17);	/* Adjust the stack pointer */	   \
224  	ldr	lr, [sp], #0x0004	/* Pull the return address */
225 
226 /*
227  * PUSHFRAMEINSVC - macro to push a trap frame on the stack in SVC32 mode
228  * This should only be used if the processor is not currently in SVC32
229  * mode. The processor mode is switched to SVC mode and the trap frame is
230  * stored. The SVC lr field is used to store the previous value of
231  * lr in SVC mode.
232  */
233 
234 #define PUSHFRAMEINSVC							   \
235 	stmdb	sp, {r0-r3};		/* Save 4 registers */		   \
236 	mov	r0, lr;			/* Save xxx32 r14 */		   \
237 	mov	r1, sp;			/* Save xxx32 sp */		   \
238 	mrs	r3, spsr;		/* Save xxx32 spsr */		   \
239 	mrs     r2, cpsr; 		/* Get the CPSR */		   \
240 	bic     r2, r2, #(PSR_MODE);	/* Fix for SVC mode */		   \
241 	orr     r2, r2, #(PSR_SVC32_MODE);				   \
242 	msr     cpsr_c, r2;		/* Punch into SVC mode */	   \
243 	mov	r2, sp;			/* Save	SVC sp */		   \
244 	str	r0, [sp, #-4]!;		/* Push return address */	   \
245 	str	lr, [sp, #-4]!;		/* Push SVC lr */		   \
246 	str	r2, [sp, #-4]!;		/* Push SVC sp */		   \
247 	msr     spsr_all, r3;		/* Restore correct spsr */	   \
248 	ldmdb	r1, {r0-r3};		/* Restore 4 regs from xxx mode */ \
249 	sub	sp, sp, #(4*15);	/* Adjust the stack pointer */	   \
250 	stmia	sp, {r0-r14}^;		/* Push the user mode registers */ \
251         mov     r0, r0;                 /* NOP for previous instruction */ \
252 	mrs	r0, spsr_all;		/* Put the SPSR on the stack */	   \
253 	str	r0, [sp, #-4]!
254 
255 /*
256  * PULLFRAMEFROMSVCANDEXIT - macro to pull a trap frame from the stack
257  * in SVC32 mode and restore the saved processor mode and PC.
258  * This should be used when the SVC lr register needs to be restored on
259  * exit.
260  */
261 
262 #define PULLFRAMEFROMSVCANDEXIT						   \
263         ldr     r0, [sp], #0x0004;	/* Get the SPSR from stack */	   \
264         msr     spsr_all, r0;		/* restore SPSR */		   \
265         ldmia   sp, {r0-r14}^;		/* Restore registers (usr mode) */ \
266         mov     r0, r0;	  		/* NOP for previous instruction */ \
267 	add	sp, sp, #(4*15);	/* Adjust the stack pointer */	   \
268 	ldmia	sp, {sp, lr, pc}^	/* Restore lr and exit */
269 
270 #endif /* _LOCORE */
271 
272 #endif /* _ARM_FRAME_H_ */
273 
274 /* End of frame.h */
275