1/* $OpenBSD: setjmp.S,v 1.15 2023/12/10 16:45:51 deraadt Exp $ */ 2/* 3 * Copyright (c) 1996 Dale Rahn. All rights reserved. 4 * 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include "SYS.h" 28 29/* int setjmp(jmp_buf env) */ 30 31#define JMP_r1 0x04 32#define JMP_r14 0x08 33#define JMP_r15 0x0c 34#define JMP_r16 0x10 35#define JMP_r17 0x14 36#define JMP_r18 0x18 37#define JMP_r19 0x1c 38#define JMP_r20 0x20 39#define JMP_r21 0x24 40#define JMP_r22 0x28 41#define JMP_r23 0x2c 42#define JMP_r24 0x30 43#define JMP_r25 0x34 44#define JMP_r26 0x38 45#define JMP_r27 0x3c 46#define JMP_r28 0x40 47#define JMP_r29 0x44 48#define JMP_r30 0x48 49#define JMP_r31 0x4c 50#define JMP_lr 0x50 51#define JMP_cr 0x54 52#define JMP_ctr 0x58 53#define JMP_xer 0x5c 54#define JMP_sig 0x60 55 56 .section .openbsd.randomdata,"aw",@progbits 57 .balign 4 58 .globl __jmpxor 59 .hidden __jmpxor 60__jmpxor: 61 .zero 4*2 # (r1, lr) 62 END(__jmpxor) 63 .type __jmpxor,@object 64 65 66ENTRY(setjmp) 67 mr 5, 3 /* save jmpbuf addr in r5 */ 68 li 3, 1 /* how = SIG_BLOCK */ 69 li 4, 0 /* oset = empty */ 70 li 0, SYS_sigprocmask 7199: sc 72 PINSYSCALL(SYS_sigprocmask, 99b) 73 74 stw 3, JMP_sig(5) 75 mr 3, 5 76ENTRY(_setjmp) 77 mflr 6 78 bcl 20, 31, 1f 791: mflr 7 80 addis 7, 7, __jmpxor-1b@ha 81 addi 7, 7, __jmpxor-1b@l 82 mtlr 6 83 lwz 0, 0(7) /* xor for r1 */ 84 lwz 7, 4(7) /* xor for lr, overwrite addr */ 85 86 /* r1, r14-r31 */ 87 xor 0, 0, 1 /* use and overwrite the r1 xor */ 88 stw 0, JMP_r1 (3) 89 stw 14, JMP_r14(3) 90 stw 15, JMP_r15(3) 91 stw 16, JMP_r16(3) 92 stw 17, JMP_r17(3) 93 stw 18, JMP_r18(3) 94 stw 19, JMP_r19(3) 95 stw 20, JMP_r20(3) 96 stw 21, JMP_r21(3) 97 stw 22, JMP_r22(3) 98 stw 23, JMP_r23(3) 99 stw 24, JMP_r24(3) 100 stw 25, JMP_r25(3) 101 stw 26, JMP_r26(3) 102 stw 27, JMP_r27(3) 103 stw 28, JMP_r28(3) 104 stw 29, JMP_r29(3) 105 stw 30, JMP_r30(3) 106 stw 31, JMP_r31(3) 107 /* cr, lr, ctr, xer */ 108 mfcr 0 109 stw 0, JMP_cr(3) 110 /* "mflr 6" done at start of _setjmp() */ 111 xor 7, 6, 7 /* use and overwrite the lr xor */ 112 stw 7, JMP_lr(3) 113 mfctr 0 114 stw 0, JMP_ctr(3) 115 mfxer 0 116 stw 0, JMP_xer(3) 117 /* f14-f31, fpscr */ 118 li 3, 0 119 blr 120END(_setjmp) 121END(setjmp) 122 123 124ENTRY(longjmp) 125 mr 5, 3 /* save jmpbuf addr in r5 */ 126 mr 6, 4 /* save val in r6 */ 127 li 3, 3 /* how = SIG_SETMASK */ 128 lwz 4, JMP_sig(5) /* oset from the jmpbuf */ 129 li 0, SYS_sigprocmask 13098: sc 131 PINSYSCALL(SYS_sigprocmask, 98b) 132 mr 3, 5 /* restore jmpbuf and val to r3,r4 */ 133 mr 4, 6 134 135ENTRY(_longjmp) 136 bcl 20, 31, 1f 1371: mflr 9 138 addis 9, 9, __jmpxor-1b@ha 139 addi 9, 9, __jmpxor-1b@l 140 lwz 8, 0(9) /* xor for r1 */ 141 lwz 9, 4(9) /* xor for lr, overwrite addr */ 142 143 /* r1, r14-r30 */ 144 lwz 0, JMP_r1 (3) 145 xor 1, 0, 8 /* use the r1 xor */ 146 lwz 14, JMP_r14(3) 147 lwz 15, JMP_r15(3) 148 lwz 16, JMP_r16(3) 149 lwz 17, JMP_r17(3) 150 lwz 18, JMP_r18(3) 151 lwz 19, JMP_r19(3) 152 lwz 20, JMP_r20(3) 153 lwz 21, JMP_r21(3) 154 lwz 22, JMP_r22(3) 155 lwz 23, JMP_r23(3) 156 lwz 24, JMP_r24(3) 157 lwz 25, JMP_r25(3) 158 lwz 26, JMP_r26(3) 159 lwz 27, JMP_r27(3) 160 lwz 28, JMP_r28(3) 161 lwz 29, JMP_r29(3) 162 lwz 30, JMP_r30(3) 163 lwz 31, JMP_r31(3) 164 /* cr, lr, ctr, xer */ 165 lwz 8, JMP_cr(3) /* overwrite the r1 xor */ 166 mtcr 8 167 lwz 0, JMP_lr(3) 168 xor 0, 0, 9 /* use the lr xor */ 169 mtlr 0 170 lwz 9, JMP_ctr(3) /* overwrite the lr xor */ 171 mtctr 9 172 lwz 0, JMP_xer(3) 173 mtxer 0 174 /* f14-f31, fpscr */ 175 176 /* if r4 == 0, return 1, not 0 */ 177 mr 3, 4 178 cmpwi 4, 0 179 bnelr 180 li 3, 1 181 blr 182END(_longjmp) 183END(longjmp) 184