setjmp.S revision 1.13
1/* $OpenBSD: setjmp.S,v 1.13 2020/12/06 18:13:15 bluhm Exp $ */ 2/*- 3 * Copyright (c) 1990 The Regents of the University of California. 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * William Jolitz. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#include "SYS.h" 35#include <machine/setjmp.h> 36 37 .section .openbsd.randomdata,"aw",@progbits 38 .balign 4 39 .globl __jmpxor 40 .hidden __jmpxor 41__jmpxor: 42 .zero 4*3 # (eip, esp, ebp) 43 END(__jmpxor) 44 .type __jmpxor,@object 45 46 47/* 48 * C library -- setjmp, longjmp 49 * 50 * longjmp(a,v) 51 * will generate a "return(v)" from the last call to 52 * setjmp(a) 53 * by restoring registers from the stack. 54 * The previous signal state is restored. 55 */ 56 57ENTRY(setjmp) 58 pushl $0 /* mask = empty */ 59 pushl $1 /* how = SIG_BLOCK */ 60 call 1f 611: movl $(SYS_sigprocmask),%eax 62 int $0x80 /* leave oset in %eax */ 63 popl %edx 64 addl $8,%esp 65 addl $__jmpxor-1b,%edx # load cookie address 66 67 movl 4(%esp),%ecx # parameter, pointer to env 68 movl %eax,(_JB_SIGMASK * 4)(%ecx) 69 movl %ebx,(_JB_EBX * 4)(%ecx) 70 movl %esp,%eax 71 xorl 0(%edx),%eax # use esp cookie 72 movl %eax,(_JB_ESP * 4)(%ecx) 73 movl %ebp,%eax 74 xorl 4(%edx),%eax # use ebp cookie 75 movl %eax,(_JB_EBP * 4)(%ecx) 76 movl %esi,(_JB_ESI * 4)(%ecx) 77 movl %edi,(_JB_EDI * 4)(%ecx) 78 movl 8(%edx),%edx # load eip cookie over cookie address 79 xorl 0(%esp),%edx # caller address 80 movl %edx,(_JB_EIP * 4)(%ecx) 81 stmxcsr (_JB_MXCSR * 4)(%ecx) 82 fnstcw (_JB_FCW * 4)(%ecx) 83 xorl %eax,%eax 84 ret 85END(setjmp) 86 87ENTRY(longjmp) 88 movl 4(%esp),%edx # parameter, pointer to env 89 pushl (_JB_SIGMASK * 4)(%edx) /* mask from sc_mask */ 90 pushl $3 /* how = SIG_SETMASK */ 91 call 1f /* get our eip */ 921: movl $(SYS_sigprocmask),%eax 93 int $0x80 94 popl %ecx 95 addl $8,%esp 96 addl $__jmpxor-1b,%ecx # load cookie address 97 98 movl 4(%esp),%edx # parameter, pointer to env 99 movl 8(%esp),%eax # parameter, val 100 ldmxcsr (_JB_MXCSR * 4)(%edx) 101 fldcw (_JB_FCW * 4)(%edx) 102 movl (_JB_EBX * 4)(%edx),%ebx 103 movl (_JB_ESP * 4)(%edx),%esi 104 xorl 0(%ecx),%esi # use esp cookie 105 movl %esi,%esp # un-xor'ed esp is safe to use 106 movl (_JB_EBP * 4)(%edx),%ebp 107 xorl 4(%ecx),%ebp # use ebp cookie 108 movl (_JB_ESI * 4)(%edx),%esi 109 movl (_JB_EDI * 4)(%edx),%edi 110 111 movl 8(%ecx),%ecx # load eip cookie over cookie address 112 xorl (_JB_EIP * 4)(%edx),%ecx 113 testl %eax,%eax 114 jnz 1f 115 incl %eax 1161: movl %ecx,0(%esp) 117 ret 118END(longjmp) 119