frame.h revision 1.7 1 /* $NetBSD: frame.h,v 1.7 2003/10/30 08:57:24 scw 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 * RiscBSD kernel project
38 *
39 * frame.h
40 *
41 * Stack frames structures
42 *
43 * Created : 30/09/94
44 */
45
46 #ifndef _ARM32_FRAME_H_
47 #define _ARM32_FRAME_H_
48
49 #include <arm/frame.h> /* Common ARM stack frames */
50
51 #ifndef _LOCORE
52
53 /*
54 * System stack frames.
55 */
56
57 typedef struct irqframe {
58 unsigned int if_spsr;
59 unsigned int if_r0;
60 unsigned int if_r1;
61 unsigned int if_r2;
62 unsigned int if_r3;
63 unsigned int if_r4;
64 unsigned int if_r5;
65 unsigned int if_r6;
66 unsigned int if_r7;
67 unsigned int if_r8;
68 unsigned int if_r9;
69 unsigned int if_r10;
70 unsigned int if_r11;
71 unsigned int if_r12;
72 unsigned int if_usr_sp;
73 unsigned int if_usr_lr;
74 unsigned int if_svc_sp;
75 unsigned int if_svc_lr;
76 unsigned int if_pc;
77 } irqframe_t;
78
79 #define clockframe irqframe
80
81 /*
82 * Switch frame
83 */
84
85 struct switchframe {
86 u_int sf_r4;
87 u_int sf_r5;
88 u_int sf_r6;
89 u_int sf_r7;
90 u_int sf_pc;
91 };
92
93 /*
94 * Stack frame. Used during stack traces (db_trace.c)
95 */
96 struct frame {
97 u_int fr_fp;
98 u_int fr_sp;
99 u_int fr_lr;
100 u_int fr_pc;
101 };
102
103 #ifdef _KERNEL
104 void validate_trapframe __P((trapframe_t *, int));
105 #endif /* _KERNEL */
106
107 #else /* _LOCORE */
108
109 #include "opt_compat_netbsd.h"
110 #include "opt_execfmt.h"
111 #include "opt_multiprocessor.h"
112
113 /*
114 * AST_ALIGNMENT_FAULT_LOCALS and ENABLE_ALIGNMENT_FAULTS
115 * These are used in order to support dynamic enabling/disabling of
116 * alignment faults when executing old a.out ARM binaries.
117 */
118 #if defined(COMPAT_15) && defined(EXEC_AOUT)
119 #ifndef MULTIPROCESSOR
120
121 /*
122 * Local variables needed by the AST/Alignment Fault macroes
123 */
124 #define AST_ALIGNMENT_FAULT_LOCALS \
125 .Laflt_astpending: ;\
126 .word _C_LABEL(astpending) ;\
127 .Laflt_cpufuncs: ;\
128 .word _C_LABEL(cpufuncs) ;\
129 .Laflt_curpcb: ;\
130 .word _C_LABEL(curpcb) ;\
131 .Laflt_cpu_info_store: ;\
132 .word _C_LABEL(cpu_info_store)
133
134 #define GET_CURPCB_ENTER \
135 ldr r1, .Laflt_curpcb ;\
136 ldr r1, [r1]
137
138 #define GET_CPUINFO_ENTER \
139 ldr r0, .Laflt_cpu_info_store
140
141 #define GET_CURPCB_EXIT \
142 ldr r1, .Laflt_curpcb ;\
143 ldr r2, .Laflt_cpu_info_store ;\
144 ldr r1, [r1]
145
146 #else /* !MULTIPROCESSOR */
147
148 #define AST_ALIGNMENT_FAULT_LOCALS \
149 .Laflt_astpending: ;\
150 .word _C_LABEL(astpending) ;\
151 .Laflt_cpufuncs: ;\
152 .word _C_LABEL(cpufuncs) ;\
153 .Laflt_cpu_info: ;\
154 .word _C_LABEL(cpu_info)
155
156 #define GET_CURPCB_ENTER \
157 ldr r4, .Laflt_cpu_info ;\
158 bl _C_LABEL(cpu_number) ;\
159 ldr r0, [r4, r0, lsl #2] ;\
160 ldr r1, [r0, #CI_CURPCB]
161
162 #define GET_CPUINFO_ENTER /* nothing to do */
163
164 #define GET_CURPCB_EXIT \
165 ldr r7, .Laflt_cpu_info ;\
166 bl _C_LABEL(cpu_number) ;\
167 ldr r2, [r7, r0, lsl #2] ;\
168 ldr r1, [r2, #CI_CURPCB]
169 #endif /* MULTIPROCESSOR */
170
171 /*
172 * This macro must be invoked following PUSHFRAMEINSVC or PUSHFRAME at
173 * the top of interrupt/exception handlers.
174 *
175 * When invoked, r0 *must* contain the value of SPSR on the current
176 * trap/interrupt frame. This is always the case if ENABLE_ALIGNMENT_FAULTS
177 * is invoked immediately after PUSHFRAMEINSVC or PUSHFRAME.
178 */
179 #define ENABLE_ALIGNMENT_FAULTS \
180 and r0, r0, #(PSR_MODE) /* Test for USR32 mode */ ;\
181 teq r0, #(PSR_USR32_MODE) ;\
182 bne 1f /* Not USR mode skip AFLT */ ;\
183 GET_CURPCB_ENTER /* r1 = curpcb */ ;\
184 cmp r1, #0x00 /* curpcb NULL? */ ;\
185 ldrne r1, [r1, #PCB_FLAGS] /* Fetch curpcb->pcb_flags */ ;\
186 tstne r1, #PCB_NOALIGNFLT ;\
187 beq 1f /* AFLTs already enabled */ ;\
188 GET_CPUINFO_ENTER /* r0 = cpuinfo */ ;\
189 ldr r2, .Laflt_cpufuncs ;\
190 ldr r1, [r0, #CI_CTRL] /* Fetch control register */ ;\
191 mov r0, #-1 ;\
192 mov lr, pc ;\
193 ldr pc, [r2, #CF_CONTROL] /* Enable alignment faults */ ;\
194 1:
195
196 /*
197 * This macro must be invoked just before PULLFRAMEFROMSVCANDEXIT or
198 * PULLFRAME at the end of interrupt/exception handlers.
199 */
200 #define DO_AST_AND_RESTORE_ALIGNMENT_FAULTS \
201 mrs r4, cpsr /* save CPSR */ ;\
202 ldr r0, [sp] /* Get the SPSR from stack */ ;\
203 orr r4, r4, #(I32_bit) /* Disable IRQs */ ;\
204 msr cpsr_c, r4 ;\
205 and r0, r0, #(PSR_MODE) /* Returning to USR mode? */ ;\
206 teq r0, #(PSR_USR32_MODE) ;\
207 ldreq r5, .Laflt_astpending ;\
208 bne 3f /* Nope, get out now */ ;\
209 1: ldr r1, [r5] /* Pending AST? */ ;\
210 teq r1, #0x00000000 ;\
211 bne 2f /* Yup. Go deal with it */ ;\
212 GET_CURPCB_EXIT /* r1 = curpcb, r2 = cpuinfo */ ;\
213 cmp r1, #0x00 /* curpcb NULL? */ ;\
214 ldrne r1, [r1, #PCB_FLAGS] /* Fetch curpcb->pcb_flags */ ;\
215 tstne r1, #PCB_NOALIGNFLT ;\
216 beq 3f /* Keep AFLTs enabled */ ;\
217 ldr r1, [r2, #CI_CTRL] /* Fetch control register */ ;\
218 ldr r2, .Laflt_cpufuncs ;\
219 mov r0, #-1 ;\
220 bic r1, r1, #CPU_CONTROL_AFLT_ENABLE /* Disable AFLTs */ ;\
221 adr lr, 3f ;\
222 ldr pc, [r2, #CF_CONTROL] /* Set new CTRL reg value */ ;\
223 2: mov r1, #0x00000000 ;\
224 str r1, [r5] /* Clear astpending */ ;\
225 bic r0, r4, #(I32_bit) /* Enable IRQs */ ;\
226 msr cpsr_c, r0 ;\
227 mov r0, sp ;\
228 bl _C_LABEL(ast) /* ast(frame) */ ;\
229 msr cpsr_c, r4 /* Disable IRQs */ ;\
230 b 1b /* Check for more ASTs */ ;\
231 3:
232
233 #else /* !(COMPAT_15 && EXEC_AOUT) */
234
235 #define AST_ALIGNMENT_FAULT_LOCALS ;\
236 .Laflt_astpending: ;\
237 .word _C_LABEL(astpending)
238
239 #define ENABLE_ALIGNMENT_FAULTS /* nothing */
240
241 #define DO_AST_AND_RESTORE_ALIGNMENT_FAULTS \
242 mrs r4, cpsr /* save CPSR */ ;\
243 ldr r0, [sp] /* Get the SPSR from stack */ ;\
244 orr r4, r4, #(I32_bit) /* Disable IRQs */ ;\
245 msr cpsr_c, r4 ;\
246 and r0, r0, #(PSR_MODE) /* Returning to USR mode? */ ;\
247 teq r0, #(PSR_USR32_MODE) ;\
248 ldreq r5, .Laflt_astpending ;\
249 bne 2f /* Nope, get out now */ ;\
250 1: ldr r1, [r5] /* Pending AST? */ ;\
251 teq r1, #0x00000000 ;\
252 beq 2f /* Nope. Just bail */ ;\
253 mov r1, #0x00000000 ;\
254 str r1, [r5] /* Clear astpending */ ;\
255 bic r0, r4, #(I32_bit) /* Enable IRQs */ ;\
256 msr cpsr_c, r0 ;\
257 mov r0, sp ;\
258 bl _C_LABEL(ast) /* ast(frame) */ ;\
259 msr cpsr_c, r4 /* Disable IRQs */ ;\
260 b 1b /* Check for more ASTs */ ;\
261 2:
262 #endif /* COMPAT_15 && EXEC_AOUT */
263
264 /*
265 * ASM macros for pushing and pulling trapframes from the stack
266 *
267 * These macros are used to handle the irqframe and trapframe structures
268 * defined above.
269 */
270
271 /*
272 * PUSHFRAME - macro to push a trap frame on the stack in the current mode
273 * Since the current mode is used, the SVC lr field is not defined.
274 *
275 * NOTE: r13 and r14 are stored separately as a work around for the
276 * SA110 rev 2 STM^ bug
277 */
278
279 #define PUSHFRAME \
280 str lr, [sp, #-4]!; /* Push the return address */ \
281 sub sp, sp, #(4*17); /* Adjust the stack pointer */ \
282 stmia sp, {r0-r12}; /* Push the user mode registers */ \
283 add r0, sp, #(4*13); /* Adjust the stack pointer */ \
284 stmia r0, {r13-r14}^; /* Push the user mode registers */ \
285 mov r0, r0; /* NOP for previous instruction */ \
286 mrs r0, spsr_all; /* Put the SPSR on the stack */ \
287 str r0, [sp, #-4]!
288
289 /*
290 * PULLFRAME - macro to pull a trap frame from the stack in the current mode
291 * Since the current mode is used, the SVC lr field is ignored.
292 */
293
294 #define PULLFRAME \
295 ldr r0, [sp], #0x0004; /* Get the SPSR from stack */ \
296 msr spsr_all, r0; \
297 ldmia sp, {r0-r14}^; /* Restore registers (usr mode) */ \
298 mov r0, r0; /* NOP for previous instruction */ \
299 add sp, sp, #(4*17); /* Adjust the stack pointer */ \
300 ldr lr, [sp], #0x0004 /* Pull the return address */
301
302 /*
303 * PUSHFRAMEINSVC - macro to push a trap frame on the stack in SVC32 mode
304 * This should only be used if the processor is not currently in SVC32
305 * mode. The processor mode is switched to SVC mode and the trap frame is
306 * stored. The SVC lr field is used to store the previous value of
307 * lr in SVC mode.
308 *
309 * NOTE: r13 and r14 are stored separately as a work around for the
310 * SA110 rev 2 STM^ bug
311 */
312
313 #define PUSHFRAMEINSVC \
314 stmdb sp, {r0-r3}; /* Save 4 registers */ \
315 mov r0, lr; /* Save xxx32 r14 */ \
316 mov r1, sp; /* Save xxx32 sp */ \
317 mrs r3, spsr; /* Save xxx32 spsr */ \
318 mrs r2, cpsr; /* Get the CPSR */ \
319 bic r2, r2, #(PSR_MODE); /* Fix for SVC mode */ \
320 orr r2, r2, #(PSR_SVC32_MODE); \
321 msr cpsr_c, r2; /* Punch into SVC mode */ \
322 mov r2, sp; /* Save SVC sp */ \
323 str r0, [sp, #-4]!; /* Push return address */ \
324 str lr, [sp, #-4]!; /* Push SVC lr */ \
325 str r2, [sp, #-4]!; /* Push SVC sp */ \
326 msr spsr_all, r3; /* Restore correct spsr */ \
327 ldmdb r1, {r0-r3}; /* Restore 4 regs from xxx mode */ \
328 sub sp, sp, #(4*15); /* Adjust the stack pointer */ \
329 stmia sp, {r0-r12}; /* Push the user mode registers */ \
330 add r0, sp, #(4*13); /* Adjust the stack pointer */ \
331 stmia r0, {r13-r14}^; /* Push the user mode registers */ \
332 mov r0, r0; /* NOP for previous instruction */ \
333 mrs r0, spsr_all; /* Put the SPSR on the stack */ \
334 str r0, [sp, #-4]!
335
336 /*
337 * PULLFRAMEFROMSVCANDEXIT - macro to pull a trap frame from the stack
338 * in SVC32 mode and restore the saved processor mode and PC.
339 * This should be used when the SVC lr register needs to be restored on
340 * exit.
341 */
342
343 #define PULLFRAMEFROMSVCANDEXIT \
344 ldr r0, [sp], #0x0004; /* Get the SPSR from stack */ \
345 msr spsr_all, r0; /* restore SPSR */ \
346 ldmia sp, {r0-r14}^; /* Restore registers (usr mode) */ \
347 mov r0, r0; /* NOP for previous instruction */ \
348 add sp, sp, #(4*15); /* Adjust the stack pointer */ \
349 ldmia sp, {sp, lr, pc}^ /* Restore lr and exit */
350
351 #endif /* _LOCORE */
352
353 #endif /* _ARM32_FRAME_H_ */
354
355 /* End of frame.h */
356