1/* $OpenBSD: setjmp.S,v 1.11 2016/05/30 02:11:21 guenther 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 36 .section .openbsd.randomdata,"aw",@progbits 37 .balign 4 38 .globl __jmpxor 39 .hidden __jmpxor 40__jmpxor: 41 .zero 4*3 # (eip, esp, ebp) 42 END(__jmpxor) 43 .type __jmpxor,@object 44 45 46/* 47 * C library -- setjmp, longjmp 48 * 49 * longjmp(a,v) 50 * will generate a "return(v)" from the last call to 51 * setjmp(a) 52 * by restoring registers from the stack. 53 * The previous signal state is restored. 54 */ 55 56ENTRY(setjmp) 57 pushl $0 /* mask = empty */ 58 pushl $1 /* how = SIG_BLOCK */ 59 call 1f 601: movl $(SYS_sigprocmask),%eax 61 int $0x80 /* leave oset in %eax */ 62 popl %edx 63 addl $8,%esp 64 addl $__jmpxor-1b,%edx # load cookie address 65 66 movl 4(%esp),%ecx 67 movl %eax,24(%ecx) 68 movl %ebx, 4(%ecx) 69 movl %esp, %eax 70 xorl 0(%edx),%eax # use esp cookie 71 movl %eax, 8(%ecx) 72 movl %ebp, %eax 73 xorl 4(%edx),%eax # use ebp cookie 74 movl %eax,12(%ecx) 75 movl %esi,16(%ecx) 76 movl %edi,20(%ecx) 77 movl 8(%edx),%edx # load eip cookie over cookie address 78 xorl 0(%esp),%edx 79 movl %edx, 0(%ecx) 80 xorl %eax,%eax 81 ret 82END(setjmp) 83 84ENTRY(longjmp) 85 movl 4(%esp),%edx 86 pushl 24(%edx) /* mask from sc_mask */ 87 pushl $3 /* how = SIG_SETMASK */ 88 call 1f /* get our eip */ 891: movl $(SYS_sigprocmask),%eax 90 int $0x80 91 popl %ecx 92 addl $8,%esp 93 addl $__jmpxor-1b,%ecx # load cookie address 94 95 movl 4(%esp),%edx 96 movl 8(%esp),%eax 97 movl 4(%edx),%ebx 98 movl 8(%edx),%esi # load xor'ed esp into safe register 99 xorl 0(%ecx),%esi # use esp cookie 100 movl %esi, %esp # un-xor'ed esp is safe to use 101 movl 12(%edx),%ebp 102 xorl 4(%ecx),%ebp # use ebp cookie 103 movl 16(%edx),%esi 104 movl 20(%edx),%edi 105 106 movl 8(%ecx),%ecx # load eip cookie over cookie address 107 xorl 0(%edx),%ecx 108 testl %eax,%eax 109 jnz 1f 110 incl %eax 1111: movl %ecx,0(%esp) 112 ret 113END(longjmp) 114