1*84d9c625SLionel Sambuc/* $NetBSD: setjmp.S,v 1.17 2013/11/30 20:20:42 joerg Exp $ */ 22fe8fb19SBen Gras 32fe8fb19SBen Gras/* 42fe8fb19SBen Gras * Copyright (c) 1997 Mark Brinicombe 52fe8fb19SBen Gras * All rights reserved. 62fe8fb19SBen Gras * 72fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without 82fe8fb19SBen Gras * modification, are permitted provided that the following conditions 92fe8fb19SBen Gras * are met: 102fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright 112fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer. 122fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright 132fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the 142fe8fb19SBen Gras * documentation and/or other materials provided with the distribution. 152fe8fb19SBen Gras * 3. All advertising materials mentioning features or use of this software 162fe8fb19SBen Gras * must display the following acknowledgement: 172fe8fb19SBen Gras * This product includes software developed by Mark Brinicombe 182fe8fb19SBen Gras * 4. Neither the name of the University nor the names of its contributors 192fe8fb19SBen Gras * may be used to endorse or promote products derived from this software 202fe8fb19SBen Gras * without specific prior written permission. 212fe8fb19SBen Gras * 222fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 232fe8fb19SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 242fe8fb19SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 252fe8fb19SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 262fe8fb19SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 272fe8fb19SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 282fe8fb19SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 292fe8fb19SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 302fe8fb19SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 312fe8fb19SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 322fe8fb19SBen Gras * SUCH DAMAGE. 332fe8fb19SBen Gras */ 342fe8fb19SBen Gras 35*84d9c625SLionel Sambuc#if !defined(__SOFTFP__) && !defined(__VFP_FP__) && !defined(__ARM_PCS) 36*84d9c625SLionel Sambuc#error FPA is not supported anymore 37*84d9c625SLionel Sambuc#endif 38*84d9c625SLionel Sambuc 39*84d9c625SLionel Sambuc#if defined(__ARM_EABI__) && !defined(__ARM_PCS_VFP) 40*84d9c625SLionel Sambuc .fpu vfp 41*84d9c625SLionel Sambuc#endif 42*84d9c625SLionel Sambuc 432fe8fb19SBen Gras#include <machine/asm.h> 442fe8fb19SBen Gras#include <machine/setjmp.h> 452fe8fb19SBen Gras 462fe8fb19SBen Gras/* 472fe8fb19SBen Gras * C library -- setjmp, longjmp 482fe8fb19SBen Gras * 492fe8fb19SBen Gras * longjmp(a,v) 502fe8fb19SBen Gras * will generate a "return(v)" from the last call to 512fe8fb19SBen Gras * setjmp(a) 522fe8fb19SBen Gras * by restoring registers from the stack. 532fe8fb19SBen Gras * The previous signal state is restored. 542fe8fb19SBen Gras */ 552fe8fb19SBen Gras 562fe8fb19SBen GrasENTRY(__setjmp14) 572fe8fb19SBen Gras /* Get the signal mask. */ 58*84d9c625SLionel Sambuc push {r0-r2, lr} 59*84d9c625SLionel Sambuc#if !defined(__thumb__) 60*84d9c625SLionel Sambuc adds r2, r0, #(_JB_SIGMASK * 4) 61*84d9c625SLionel Sambuc#else 62*84d9c625SLionel Sambuc mov r2, r0 63*84d9c625SLionel Sambuc adds r2, r2, #(_JB_SIGMASK * 4) 64*84d9c625SLionel Sambuc#endif 65*84d9c625SLionel Sambuc movs r1, #0 66*84d9c625SLionel Sambuc movs r0, #0 67*84d9c625SLionel Sambuc bl PLT_SYM(_C_LABEL(__sigprocmask14)) 68*84d9c625SLionel Sambuc#if !defined(__thumb__) || defined(_ARM_ARCH_T2) 69*84d9c625SLionel Sambuc pop {r0-r2, lr} 70*84d9c625SLionel Sambuc#else 71*84d9c625SLionel Sambuc pop {r0-r3} 72*84d9c625SLionel Sambuc mov lr, r3 73*84d9c625SLionel Sambuc#endif 742fe8fb19SBen Gras 752fe8fb19SBen Gras ldr r1, .Lsetjmp_magic 762fe8fb19SBen Gras 77*84d9c625SLionel Sambuc#if defined(__ARM_EABI__) && (!defined(__thumb__) || defined(_ARM_ARCH_T2)) 78*84d9c625SLionel Sambuc ldr r2, .Lfpu_present 79*84d9c625SLionel Sambuc#ifdef __PIC__ 80*84d9c625SLionel Sambuc add r2, r2, pc /* pc = &.LPIC0 */ 81*84d9c625SLionel Sambuc#endif 82*84d9c625SLionel Sambuc ldr r2, [r2] 83*84d9c625SLionel Sambuc.LPIC0: 84*84d9c625SLionel Sambuc#if defined(__thumb__) && defined(_ARM_ARCH_T2) 85*84d9c625SLionel Sambuc cbz r2, 1f /* don't save if we don't have a FPU */ 862fe8fb19SBen Gras#else 87*84d9c625SLionel Sambuc cmp r2, #0 /* do we have a FPU? */ 88*84d9c625SLionel Sambuc beq 1f /* no, don't save VFP registers */ 89*84d9c625SLionel Sambuc#endif 90*84d9c625SLionel Sambuc 91*84d9c625SLionel Sambuc orrs r1, r1, #(_JB_MAGIC_SETJMP ^ _JB_MAGIC_SETJMP_VFP) 92*84d9c625SLionel Sambuc /* change magic to VFP magic */ 93*84d9c625SLionel Sambuc adds r2, r0, #(_JB_REG_D8 * 4) 94*84d9c625SLionel Sambuc vstmia r2, {d8-d15} 95*84d9c625SLionel Sambuc vmrs r2, fpscr 96*84d9c625SLionel Sambuc str r2, [r0, #(_JB_REG_FPSCR * 4)] 97*84d9c625SLionel Sambuc1: 98*84d9c625SLionel Sambuc#endif /* __ARM_EABI__ && (!__thumb__ || _ARM_ARCH_T2) */ 99*84d9c625SLionel Sambuc 100*84d9c625SLionel Sambuc str r1, [r0] /* store magic */ 101*84d9c625SLionel Sambuc 1022fe8fb19SBen Gras /* Store integer registers */ 103*84d9c625SLionel Sambuc adds r0, r0, #(_JB_REG_R4 * 4) 104*84d9c625SLionel Sambuc#if !defined(__thumb__) 105*84d9c625SLionel Sambuc stmia r0!, {r4-lr} 106*84d9c625SLionel Sambuc#else 107*84d9c625SLionel Sambuc stmia r0!, {r4-r7} 108*84d9c625SLionel Sambuc mov r2, r8 109*84d9c625SLionel Sambuc mov r3, r9 110*84d9c625SLionel Sambuc stmia r0!, {r2-r3} 111*84d9c625SLionel Sambuc mov r2, r10 112*84d9c625SLionel Sambuc mov r3, r11 113*84d9c625SLionel Sambuc stmia r0!, {r2-r3} 114*84d9c625SLionel Sambuc mov r2, sp 115*84d9c625SLionel Sambuc mov r3, lr 116*84d9c625SLionel Sambuc str r2, [r0, #4] 117*84d9c625SLionel Sambuc str r3, [r0, #8] 118*84d9c625SLionel Sambuc#endif 119*84d9c625SLionel Sambuc movs r0, #0 1202fe8fb19SBen Gras RET 1212fe8fb19SBen Gras 122*84d9c625SLionel Sambuc#if defined(__ARM_EABI__) && (!defined(__thumb__) || defined(_ARM_ARCH_T2)) 123*84d9c625SLionel Sambuc .align 0 124*84d9c625SLionel Sambuc.Lfpu_present: 125*84d9c625SLionel Sambuc .word REL_SYM(_libc_arm_fpu_present, .LPIC0) 126*84d9c625SLionel Sambuc#endif /* __ARM_EABI__ && (!__thumb__ || _ARM_ARCH_T2) */ 127*84d9c625SLionel SambucEND(__setjmp14) 1282fe8fb19SBen Gras 1292fe8fb19SBen GrasENTRY(__longjmp14) 130*84d9c625SLionel Sambuc#if !defined(__thumb__) || defined(_ARM_ARCH_T2) 131*84d9c625SLionel Sambuc ldr r2, [r0] 132*84d9c625SLionel Sambuc ldr ip, .Lsetjmp_magic 133*84d9c625SLionel Sambuc bic r3, r2, #(_JB_MAGIC_SETJMP ^ _JB_MAGIC_SETJMP_VFP) 134*84d9c625SLionel Sambuc#else 135*84d9c625SLionel Sambuc ldr r3, .Lsetjmp_magic 136*84d9c625SLionel Sambuc mov ip, r3 1372fe8fb19SBen Gras ldr r3, [r0] 138*84d9c625SLionel Sambuc movs r2, #(_JB_MAGIC_SETJMP ^ _JB_MAGIC_SETJMP_VFP) 139*84d9c625SLionel Sambuc bics r3, r2 140*84d9c625SLionel Sambuc /* r2 is not the magic but we don't need it since we can't do VFP */ 141*84d9c625SLionel Sambuc#endif 142*84d9c625SLionel Sambuc cmp r3, ip 1432fe8fb19SBen Gras bne .Lbotch 1442fe8fb19SBen Gras 1452fe8fb19SBen Gras /* Restore the signal mask. */ 146*84d9c625SLionel Sambuc push {r0-r2, lr} 147*84d9c625SLionel Sambuc movs r2, #0 148*84d9c625SLionel Sambuc#if !defined(__thumb__) 149*84d9c625SLionel Sambuc adds r1, r0, #(_JB_SIGMASK * 4) 1502fe8fb19SBen Gras#else 151*84d9c625SLionel Sambuc mov r1, r0 152*84d9c625SLionel Sambuc adds r1, r1, #(_JB_SIGMASK * 4) 153*84d9c625SLionel Sambuc#endif 154*84d9c625SLionel Sambuc movs r0, #3 /* SIG_SETMASK */ 155*84d9c625SLionel Sambuc bl PLT_SYM(_C_LABEL(__sigprocmask14)) 156*84d9c625SLionel Sambuc#if !defined(__thumb__) || defined(_ARM_ARCH_T2) 157*84d9c625SLionel Sambuc pop {r0-r2, lr} 158*84d9c625SLionel Sambuc#else 159*84d9c625SLionel Sambuc pop {r0-r3} 160*84d9c625SLionel Sambuc mov lr, r3 161*84d9c625SLionel Sambuc#endif 1622fe8fb19SBen Gras 163*84d9c625SLionel Sambuc#if defined(__ARM_EABI__) && (!defined(__thumb__) || defined(_ARM_ARCH_T2)) 164*84d9c625SLionel Sambuc tst r2, #(_JB_MAGIC_SETJMP ^ _JB_MAGIC_SETJMP_VFP) 165*84d9c625SLionel Sambuc /* is this a VFP magic? */ 166*84d9c625SLionel Sambuc beq 1f /* no, don't restore VFP */ 167*84d9c625SLionel Sambuc adds r2, r0, #(_JB_REG_D8 * 4) 168*84d9c625SLionel Sambuc vldmia r2, {d8-d15} 169*84d9c625SLionel Sambuc ldr r2, [r0, #(_JB_REG_FPSCR * 4)] 170*84d9c625SLionel Sambuc vmsr fpscr, r2 171*84d9c625SLionel Sambuc1: 172*84d9c625SLionel Sambuc#endif /* __ARM_EABI__ && (!__thumb__ || _ARM_ARCH_T2) */ 173*84d9c625SLionel Sambuc 174*84d9c625SLionel Sambuc adds r0, r0, #(_JB_REG_R4 * 4) 175*84d9c625SLionel Sambuc /* Restore integer registers */ 176*84d9c625SLionel Sambuc#if !defined(__thumb__) || defined(_ARM_ARCH_T2) 177*84d9c625SLionel Sambuc ldmia r0!, {r4-r12} 178*84d9c625SLionel Sambuc#else 179*84d9c625SLionel Sambuc ldmia r0!, {r4-r7} 180*84d9c625SLionel Sambuc ldmia r0!, {r2-r3} 181*84d9c625SLionel Sambuc mov r8, r2 182*84d9c625SLionel Sambuc mov r9, r3 183*84d9c625SLionel Sambuc ldmia r0!, {r2-r3} 184*84d9c625SLionel Sambuc mov r10, r2 185*84d9c625SLionel Sambuc mov r11, r3 186*84d9c625SLionel Sambuc adds r0, r0, #4 /* skip ip(r12) */ 187*84d9c625SLionel Sambuc#endif 188*84d9c625SLionel Sambuc 189*84d9c625SLionel Sambuc ldmia r0!, {r2-r3} 190*84d9c625SLionel Sambuc 191*84d9c625SLionel Sambuc /* Validate sp and lr */ 192*84d9c625SLionel Sambuc#if defined(__thumb__) && defined(_ARM_ARCH_T2) 193*84d9c625SLionel Sambuc cbz r2, .Lbotch 194*84d9c625SLionel Sambuc#else 195*84d9c625SLionel Sambuc cmp r2, #0 1962fe8fb19SBen Gras beq .Lbotch 197*84d9c625SLionel Sambuc#endif 198*84d9c625SLionel Sambuc mov sp, r2 199*84d9c625SLionel Sambuc 200*84d9c625SLionel Sambuc#if defined(__thumb__) && defined(_ARM_ARCH_T2) 201*84d9c625SLionel Sambuc cbz r3, .Lbotch 202*84d9c625SLionel Sambuc#else 203*84d9c625SLionel Sambuc cmp r3, #0 204*84d9c625SLionel Sambuc beq .Lbotch 205*84d9c625SLionel Sambuc#endif 206*84d9c625SLionel Sambuc mov lr, r3 2072fe8fb19SBen Gras 2082fe8fb19SBen Gras /* Set return value */ 209*84d9c625SLionel Sambuc movs r0, r1 210*84d9c625SLionel Sambuc#if !defined(__thumb__) 211*84d9c625SLionel Sambuc moveq r0, #1 212*84d9c625SLionel Sambuc#else 213*84d9c625SLionel Sambuc bne 1f 214*84d9c625SLionel Sambuc movs r0, #1 215*84d9c625SLionel Sambuc1: 216*84d9c625SLionel Sambuc#endif 2172fe8fb19SBen Gras RET 2182fe8fb19SBen Gras 2192fe8fb19SBen Gras /* validation failed, die die die. */ 2202fe8fb19SBen Gras.Lbotch: 221*84d9c625SLionel Sambuc bl PLT_SYM(_C_LABEL(longjmperror)) 222*84d9c625SLionel Sambuc bl PLT_SYM(_C_LABEL(abort)) 223*84d9c625SLionel Sambuc1: b 1b /* Cannot get here */ 224*84d9c625SLionel Sambuc 225*84d9c625SLionel Sambuc .align 0 226*84d9c625SLionel Sambuc.Lsetjmp_magic: 227*84d9c625SLionel Sambuc .word _JB_MAGIC_SETJMP 228*84d9c625SLionel SambucEND(__longjmp14) 229