1 /* $OpenBSD: frame.h,v 1.10 2016/04/25 08:00:43 patrick 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 register_t tf_pad; 79 } trapframe_t; 80 81 /* Register numbers */ 82 #define tf_r13 tf_usr_sp 83 #define tf_r14 tf_usr_lr 84 #define tf_r15 tf_pc 85 86 /* 87 * Signal frame. Pushed onto user stack before calling sigcode. 88 */ 89 90 struct sigframe { 91 int sf_signum; 92 siginfo_t *sf_sip; 93 struct sigcontext *sf_scp; 94 sig_t sf_handler; 95 struct sigcontext sf_sc; 96 siginfo_t sf_si; 97 }; 98 99 /* the pointers are used in the trampoline code to locate the ucontext */ 100 #if 0 101 struct sigframe_siginfo { 102 siginfo_t sf_si; /* actual saved siginfo */ 103 ucontext_t sf_uc; /* actual saved ucontext */ 104 }; 105 #endif 106 107 #if 0 108 #ifdef _KERNEL 109 void sendsig_sigcontext(const ksiginfo_t *, const sigset_t *); 110 #endif 111 #endif 112 113 #endif /* _LOCORE */ 114 115 #ifndef _LOCORE 116 117 /* 118 * System stack frames. 119 */ 120 121 typedef struct irqframe { 122 unsigned int if_spsr; 123 unsigned int if_r0; 124 unsigned int if_r1; 125 unsigned int if_r2; 126 unsigned int if_r3; 127 unsigned int if_r4; 128 unsigned int if_r5; 129 unsigned int if_r6; 130 unsigned int if_r7; 131 unsigned int if_r8; 132 unsigned int if_r9; 133 unsigned int if_r10; 134 unsigned int if_r11; 135 unsigned int if_r12; 136 unsigned int if_usr_sp; 137 unsigned int if_usr_lr; 138 unsigned int if_svc_sp; 139 unsigned int if_svc_lr; 140 unsigned int if_pc; 141 unsigned int if_pad; 142 } irqframe_t; 143 144 #define clockframe irqframe 145 146 /* 147 * Switch frame 148 */ 149 150 struct switchframe { 151 u_int sf_pad; 152 u_int sf_r4; 153 u_int sf_r5; 154 u_int sf_r6; 155 u_int sf_r7; 156 u_int sf_pc; 157 }; 158 159 /* 160 * Stack frame. Used during stack traces (db_trace.c) 161 */ 162 struct frame { 163 u_int fr_fp; 164 u_int fr_sp; 165 u_int fr_lr; 166 u_int fr_pc; 167 }; 168 169 #else /* _LOCORE */ 170 171 #define AST_LOCALS \ 172 .Laflt_astpending: ;\ 173 .word _C_LABEL(astpending) 174 175 #define DO_AST \ 176 ldr r0, [sp] /* Get the SPSR from stack */ ;\ 177 mrs r4, cpsr /* save CPSR */ ;\ 178 and r0, r0, #(PSR_MODE) /* Returning to USR mode? */ ;\ 179 teq r0, #(PSR_USR32_MODE) ;\ 180 ldreq r5, .Laflt_astpending ;\ 181 bne 2f /* Nope, get out now */ ;\ 182 bic r4, r4, #(PSR_I) ;\ 183 1: orr r0, r4, #(PSR_I) /* Disable IRQs */ ;\ 184 msr cpsr_c, r0 ;\ 185 ldr r1, [r5] /* Pending AST? */ ;\ 186 teq r1, #0x00000000 ;\ 187 beq 2f /* Nope. Just bail */ ;\ 188 mov r1, #0x00000000 ;\ 189 str r1, [r5] /* Clear astpending */ ;\ 190 msr cpsr_c, r4 /* Restore interrupts */ ;\ 191 mov r0, sp ;\ 192 adr lr, 1b ;\ 193 b _C_LABEL(ast) /* ast(frame) */ ;\ 194 2: 195 196 /* 197 * ASM macros for pushing and pulling trapframes from the stack 198 * 199 * These macros are used to handle the irqframe and trapframe structures 200 * defined above. 201 */ 202 203 /* 204 * CLREX - On ARMv7 machines that support atomic instructions, we need 205 * to clear the exclusive monitors on kernel exit, so that a userland 206 * atomic store can't succeed due to an unrelated outstanding atomic 207 * operation. ARM also highly recommends clearing the monitor on data 208 * aborts, as the monitor state after taking a data abort is unknown. 209 * Issuing a clrex on kernel entry and on kernel exit is the easiest 210 * way to take care of both issues and to make sure that the kernel 211 * and userland do not leave any outstanding reserves active. 212 */ 213 #if defined(CPU_ARMv7) 214 #define CLREX clrex 215 #else 216 #define CLREX 217 #endif 218 219 /* 220 * PUSHFRAME - macro to push a trap frame on the stack in the current mode 221 * Since the current mode is used, the SVC lr field is not defined. 222 */ 223 224 #define PUSHFRAME \ 225 CLREX; \ 226 sub sp, sp, #4; /* Align the stack */ \ 227 str lr, [sp, #-4]!; /* Push the return address */ \ 228 sub sp, sp, #(4*17); /* Adjust the stack pointer */ \ 229 stmia sp, {r0-r14}^; /* Push the user mode registers */ \ 230 mov r0, r0; /* NOP for previous instruction */ \ 231 mrs r0, spsr; /* Put the SPSR on the stack */ \ 232 str r0, [sp, #-4]! 233 234 /* 235 * PULLFRAME - macro to pull a trap frame from the stack in the current mode 236 * Since the current mode is used, the SVC lr field is ignored. 237 */ 238 239 #define PULLFRAME \ 240 CLREX; \ 241 ldr r0, [sp], #0x0004; /* Get the SPSR from stack */ \ 242 msr spsr_fsxc, r0; \ 243 ldmia sp, {r0-r14}^; /* Restore registers (usr mode) */ \ 244 mov r0, r0; /* NOP for previous instruction */ \ 245 add sp, sp, #(4*17); /* Adjust the stack pointer */ \ 246 ldr lr, [sp], #0x0004; /* Pull the return address */ \ 247 add sp, sp, #4 /* Align the stack */ 248 249 /* 250 * PUSHFRAMEINSVC - macro to push a trap frame on the stack in SVC32 mode 251 * This should only be used if the processor is not currently in SVC32 252 * mode. The processor mode is switched to SVC mode and the trap frame is 253 * stored. The SVC lr field is used to store the previous value of 254 * lr in SVC mode. 255 */ 256 257 #define PUSHFRAMEINSVC \ 258 CLREX; \ 259 stmdb sp, {r0-r3}; /* Save 4 registers */ \ 260 mov r0, lr; /* Save xxx32 r14 */ \ 261 mov r1, sp; /* Save xxx32 sp */ \ 262 mrs r3, spsr; /* Save xxx32 spsr */ \ 263 mrs r2, cpsr; /* Get the CPSR */ \ 264 bic r2, r2, #(PSR_MODE); /* Fix for SVC mode */ \ 265 orr r2, r2, #(PSR_SVC32_MODE); \ 266 msr cpsr_c, r2; /* Punch into SVC mode */ \ 267 mov r2, sp; /* Save SVC sp */ \ 268 bic sp, sp, #7; /* Align sp to an 8-byte address */ \ 269 sub sp, sp, #4; /* Pad trapframe to keep alignment */ \ 270 str r0, [sp, #-4]!; /* Push return address */ \ 271 str lr, [sp, #-4]!; /* Push SVC lr */ \ 272 str r2, [sp, #-4]!; /* Push SVC sp */ \ 273 msr spsr_fsxc, r3; /* Restore correct spsr */ \ 274 ldmdb r1, {r0-r3}; /* Restore 4 regs from xxx mode */ \ 275 sub sp, sp, #(4*15); /* Adjust the stack pointer */ \ 276 stmia sp, {r0-r14}^; /* Push the user mode registers */ \ 277 mov r0, r0; /* NOP for previous instruction */ \ 278 mrs r0, spsr; /* Put the SPSR on the stack */ \ 279 str r0, [sp, #-4]! 280 281 /* 282 * PULLFRAMEFROMSVCANDEXIT - macro to pull a trap frame from the stack 283 * in SVC32 mode and restore the saved processor mode and PC. 284 * This should be used when the SVC lr register needs to be restored on 285 * exit. 286 */ 287 288 #define PULLFRAMEFROMSVCANDEXIT \ 289 CLREX; \ 290 ldr r0, [sp], #0x0004; /* Get the SPSR from stack */ \ 291 msr spsr_fsxc, r0; /* restore SPSR */ \ 292 ldmia sp, {r0-r14}^; /* Restore registers (usr mode) */ \ 293 mov r0, r0; /* NOP for previous instruction */ \ 294 add sp, sp, #(4*15); /* Adjust the stack pointer */ \ 295 ldmia sp, {sp, lr, pc}^ /* Restore lr and exit */ 296 297 #endif /* _LOCORE */ 298 299 #endif /* _ARM_FRAME_H_ */ 300 301 /* End of frame.h */ 302