1/* $OpenBSD: setjmp.S,v 1.5 2023/12/10 16:45:52 deraadt Exp $ */ 2/* 3 * Copyright (c) 2020 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#include <machine/asm.h> 29 30/* int setjmp(jmp_buf env) */ 31 32#define JMP_r1 0x08 33#define JMP_r14 0x10 34#define JMP_r15 0x18 35#define JMP_r16 0x20 36#define JMP_r17 0x28 37#define JMP_r18 0x30 38#define JMP_r19 0x38 39#define JMP_r20 0x40 40#define JMP_r21 0x48 41#define JMP_r22 0x50 42#define JMP_r23 0x58 43#define JMP_r24 0x60 44#define JMP_r25 0x68 45#define JMP_r26 0x70 46#define JMP_r27 0x78 47#define JMP_r28 0x80 48#define JMP_r29 0x88 49#define JMP_r30 0x90 50#define JMP_r31 0x98 51#define JMP_lr 0xa0 52#define JMP_cr 0xa8 53#define JMP_ctr 0xb0 54#define JMP_xer 0xb8 55#define JMP_sig 0xc0 56 57 .section .openbsd.randomdata,"aw",@progbits 58 .balign 4 59 .globl __jmpxor 60 .hidden __jmpxor 61__jmpxor: 62 .zero 8*2 # (r1, lr) 63 END(__jmpxor) 64 .type __jmpxor,@object 65 66 67/* int setjmp(jmp_buf env); */ 68ENTRY(setjmp) 69 mr %r5, %r3 /* save jmpbuf addr in r5 */ 70 li %r3, 1 /* how = SIG_BLOCK */ 71 li %r4, 0 /* oset = empty */ 72 li %r0, SYS_sigprocmask 7399: sc 74 PINSYSCALL(SYS_sigprocmask, 99b) 75 std %r3, JMP_sig(%r5) 76 b 1f 77 nop 78 79ENTRY(_setjmp) 80 mr %r5, %r3 /* save jmpbuf addr in r5 */ 811: 82 RETGUARD_SETUP(setjmp, %r11) 83 addis %r7, %r2, __jmpxor@toc@ha 84 addi %r7, %r7, __jmpxor@toc@l 85 ld %r0, 0(%r7) /* xor for r1 */ 86 ld %r7, 8(%r7) /* xor for lr, overwrite addr */ 87 88 /* r1, r14-r31 */ 89 xor %r0, %r0, %r1 /* use and overwrite the r1 xor */ 90 std %r0, JMP_r1(%r5) 91 std %r14, JMP_r14(%r5) 92 std %r15, JMP_r15(%r5) 93 std %r16, JMP_r16(%r5) 94 std %r17, JMP_r17(%r5) 95 std %r18, JMP_r18(%r5) 96 std %r19, JMP_r19(%r5) 97 std %r20, JMP_r20(%r5) 98 std %r21, JMP_r21(%r5) 99 std %r22, JMP_r22(%r5) 100 std %r23, JMP_r23(%r5) 101 std %r24, JMP_r24(%r5) 102 std %r25, JMP_r25(%r5) 103 std %r26, JMP_r26(%r5) 104 std %r27, JMP_r27(%r5) 105 std %r28, JMP_r28(%r5) 106 std %r29, JMP_r29(%r5) 107 std %r30, JMP_r30(%r5) 108 std %r31, JMP_r31(%r5) 109 /* lr, cr, ctr, xer */ 110 mflr %r6 111 xor %r7, %r6, %r7 /* use and overwrite the lr xor */ 112 std %r7, JMP_lr(%r5) 113 mfcr %r0 114 std %r0, JMP_cr(%r5) 115 mfctr %r0 116 std %r0, JMP_ctr(%r5) 117 mfxer %r0 118 std %r0, JMP_xer(%r5) 119 /* floating point is all caller save */ 120 li %r3, 0 121 RETGUARD_CHECK(setjmp, %r11) 122 blr 123END(_setjmp) 124END(setjmp) 125 126 127/* void longjmp(jmp_buf env, int val); */ 128ENTRY(longjmp) 129 mr %r5, %r3 /* save jmpbuf addr in r5 */ 130 mr %r6, %r4 /* save val in r6 */ 131 li %r3, 3 /* how = SIG_SETMASK */ 132 ld %r4, JMP_sig(%r5) /* oset from the jmpbuf */ 133 li %r0, SYS_sigprocmask 13498: sc 135 PINSYSCALL(SYS_sigprocmask, 98b) 136 nop 137 b 1f 138 nop 139 140/* _longjmp(jmp_buf env, int val); */ 141 142ENTRY(_longjmp) 143 mr %r5, %r3 /* save jmpbuf addr in r5 */ 144 mr %r6, %r4 /* save val in r6 */ 1451: 146 addis %r9, %r2, __jmpxor@toc@ha 147 addi %r9, %r9, __jmpxor@toc@l 148 ld %r8, 0(%r9) /* xor for r1 */ 149 ld %r9, 8(%r9) /* xor for lr, overwrite addr */ 150 ld %r0, JMP_lr(%r5) 151 xor %r0, %r0, %r9 /* use the lr xor */ 152 mtlr %r0 153 RETGUARD_SETUP(longjmp, %r11) 154 155 /* r1, r14-r31 */ 156 ld %r0, JMP_r1(%r5) 157 xor %r1, %r0, %r8 /* use the r1 xor */ 158 ld %r14, JMP_r14(%r5) 159 ld %r15, JMP_r15(%r5) 160 ld %r16, JMP_r16(%r5) 161 ld %r17, JMP_r17(%r5) 162 ld %r18, JMP_r18(%r5) 163 ld %r19, JMP_r19(%r5) 164 ld %r20, JMP_r20(%r5) 165 ld %r21, JMP_r21(%r5) 166 ld %r22, JMP_r22(%r5) 167 ld %r23, JMP_r23(%r5) 168 ld %r24, JMP_r24(%r5) 169 ld %r25, JMP_r25(%r5) 170 ld %r26, JMP_r26(%r5) 171 ld %r27, JMP_r27(%r5) 172 ld %r28, JMP_r28(%r5) 173 ld %r29, JMP_r29(%r5) 174 ld %r30, JMP_r30(%r5) 175 ld %r31, JMP_r31(%r5) 176 /* cr, lr, ctr, xer */ 177 ld %r8, JMP_cr(%r5) /* overwrite the r1 xor */ 178 mtcr %r8 179 ld %r9, JMP_ctr(%r5) /* overwrite the lr xor */ 180 mtctr %r9 181 ld %r0, JMP_xer(%r5) 182 mtxer %r0 183 /* floating point is all caller save */ 184 185 /* if return val in r6 == 0, return 1, not 0 */ 186 mr %r3, %r6 187 cmpwi %r6, 0 188 bne 2f 189 li %r3, 1 1902: 191 RETGUARD_CHECK(longjmp, %r11) 192 blr 193END(_longjmp) 194END(longjmp) 195