Elliott Hughes | 4906e56 | 2013-10-04 14:55:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2001 Wasabi Systems, Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Written by Frank van der Linden for Wasabi Systems, Inc. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * 3. All advertising materials mentioning features or use of this software |
| 16 | * must display the following acknowledgement: |
| 17 | * This product includes software developed for the NetBSD Project by |
| 18 | * Wasabi Systems, Inc. |
| 19 | * 4. The name of Wasabi Systems, Inc. may not be used to endorse |
| 20 | * or promote products derived from this software without specific prior |
| 21 | * written permission. |
| 22 | * |
| 23 | * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND |
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 25 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 26 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC |
| 27 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 33 | * POSSIBILITY OF SUCH DAMAGE. |
| 34 | */ |
| 35 | |
Elliott Hughes | 851e68a | 2014-02-19 16:53:20 -0800 | [diff] [blame] | 36 | #include <private/bionic_asm.h> |
Elliott Hughes | 4906e56 | 2013-10-04 14:55:30 -0700 | [diff] [blame] | 37 | |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 38 | // These are only the callee-saved registers. Code calling setjmp |
| 39 | // will expect the rest to be clobbered anyway. |
| 40 | |
| 41 | #define _JB_RBX 0 |
| 42 | #define _JB_RBP 1 |
| 43 | #define _JB_R12 2 |
| 44 | #define _JB_R13 3 |
| 45 | #define _JB_R14 4 |
| 46 | #define _JB_R15 5 |
| 47 | #define _JB_RSP 6 |
| 48 | #define _JB_PC 7 |
| 49 | #define _JB_SIGFLAG 8 |
| 50 | #define _JB_SIGMASK 9 |
Elliott Hughes | 1c0c0ed | 2014-12-05 22:24:49 -0800 | [diff] [blame] | 51 | #define _JB_SIGMASK_RT 10 // sigprocmask will write here too. |
Elliott Hughes | 4906e56 | 2013-10-04 14:55:30 -0700 | [diff] [blame] | 52 | |
Josh Gao | 2342e64 | 2015-09-16 18:42:45 -0700 | [diff] [blame] | 53 | #define MANGLE_REGISTERS 1 |
| 54 | .macro m_mangle_registers reg |
| 55 | #if MANGLE_REGISTERS |
| 56 | xorq \reg,%rbx |
| 57 | xorq \reg,%rbp |
| 58 | xorq \reg,%r12 |
| 59 | xorq \reg,%r13 |
| 60 | xorq \reg,%r14 |
| 61 | xorq \reg,%r15 |
| 62 | xorq \reg,%rsp |
| 63 | xorq \reg,%r11 |
| 64 | #endif |
| 65 | .endm |
| 66 | |
| 67 | .macro m_unmangle_registers reg |
| 68 | m_mangle_registers \reg |
| 69 | .endm |
| 70 | |
| 71 | |
Elliott Hughes | 4906e56 | 2013-10-04 14:55:30 -0700 | [diff] [blame] | 72 | ENTRY(setjmp) |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 73 | movl $1,%esi |
| 74 | jmp PIC_PLT(sigsetjmp) |
Christopher Ferris | 507cfe2 | 2013-11-19 13:45:27 -0800 | [diff] [blame] | 75 | END(setjmp) |
Elliott Hughes | 4906e56 | 2013-10-04 14:55:30 -0700 | [diff] [blame] | 76 | |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 77 | ENTRY(_setjmp) |
| 78 | movl $0,%esi |
| 79 | jmp PIC_PLT(sigsetjmp) |
| 80 | END(_setjmp) |
Elliott Hughes | 4906e56 | 2013-10-04 14:55:30 -0700 | [diff] [blame] | 81 | |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 82 | // int sigsetjmp(sigjmp_buf env, int save_signal_mask); |
| 83 | ENTRY(sigsetjmp) |
Josh Gao | 2342e64 | 2015-09-16 18:42:45 -0700 | [diff] [blame] | 84 | pushq %rdi |
| 85 | movq %rsi,%rdi |
| 86 | call PIC_PLT(__bionic_setjmp_cookie_get) |
| 87 | popq %rdi |
| 88 | |
| 89 | // Record setjmp cookie and whether or not we're saving the signal mask. |
| 90 | movq %rax,(_JB_SIGFLAG * 8)(%rdi) |
| 91 | pushq %rax |
Elliott Hughes | 4906e56 | 2013-10-04 14:55:30 -0700 | [diff] [blame] | 92 | |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 93 | // Do we need to save the signal mask? |
Josh Gao | 2342e64 | 2015-09-16 18:42:45 -0700 | [diff] [blame] | 94 | testq $1,%rax |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 95 | jz 2f |
| 96 | |
Elliott Hughes | 1c0c0ed | 2014-12-05 22:24:49 -0800 | [diff] [blame] | 97 | // Save current signal mask. |
| 98 | pushq %rdi // Push 'env'. |
| 99 | // The 'how' argument is ignored if new_mask is NULL. |
| 100 | xorq %rsi,%rsi // NULL. |
| 101 | leaq (_JB_SIGMASK * 8)(%rdi),%rdx // old_mask. |
| 102 | call PIC_PLT(sigprocmask) |
| 103 | popq %rdi // Pop 'env'. |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 104 | |
| 105 | 2: |
| 106 | // Save the callee-save registers. |
Josh Gao | 2342e64 | 2015-09-16 18:42:45 -0700 | [diff] [blame] | 107 | popq %rax |
| 108 | andq $-2,%rax |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 109 | movq (%rsp),%r11 |
Josh Gao | 2342e64 | 2015-09-16 18:42:45 -0700 | [diff] [blame] | 110 | m_mangle_registers %rax |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 111 | movq %rbx,(_JB_RBX * 8)(%rdi) |
| 112 | movq %rbp,(_JB_RBP * 8)(%rdi) |
| 113 | movq %r12,(_JB_R12 * 8)(%rdi) |
| 114 | movq %r13,(_JB_R13 * 8)(%rdi) |
| 115 | movq %r14,(_JB_R14 * 8)(%rdi) |
| 116 | movq %r15,(_JB_R15 * 8)(%rdi) |
| 117 | movq %rsp,(_JB_RSP * 8)(%rdi) |
| 118 | movq %r11,(_JB_PC * 8)(%rdi) |
Josh Gao | 2342e64 | 2015-09-16 18:42:45 -0700 | [diff] [blame] | 119 | m_unmangle_registers %rax |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 120 | |
| 121 | xorl %eax,%eax |
| 122 | ret |
| 123 | END(sigsetjmp) |
| 124 | |
| 125 | // void siglongjmp(sigjmp_buf env, int value); |
| 126 | ENTRY(siglongjmp) |
| 127 | movq %rdi,%r12 |
| 128 | pushq %rsi // Push 'value'. |
| 129 | |
| 130 | // Do we need to restore the signal mask? |
Josh Gao | 2342e64 | 2015-09-16 18:42:45 -0700 | [diff] [blame] | 131 | movq (_JB_SIGFLAG * 8)(%rdi), %rdi |
| 132 | pushq %rdi // Push cookie |
| 133 | testq $1, %rdi |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 134 | jz 2f |
| 135 | |
| 136 | // Restore the signal mask. |
Elliott Hughes | 1c0c0ed | 2014-12-05 22:24:49 -0800 | [diff] [blame] | 137 | movq $2,%rdi // SIG_SETMASK. |
| 138 | leaq (_JB_SIGMASK * 8)(%r12),%rsi // new_mask. |
| 139 | xorq %rdx,%rdx // NULL. |
| 140 | call PIC_PLT(sigprocmask) |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 141 | |
| 142 | 2: |
Josh Gao | 2342e64 | 2015-09-16 18:42:45 -0700 | [diff] [blame] | 143 | // Fetch the setjmp cookie and clear the signal flag bit. |
| 144 | popq %rcx |
| 145 | andq $-2, %rcx |
| 146 | |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 147 | popq %rax // Pop 'value'. |
| 148 | |
| 149 | // Restore the callee-save registers. |
| 150 | movq (_JB_RBX * 8)(%r12),%rbx |
| 151 | movq (_JB_RBP * 8)(%r12),%rbp |
| 152 | movq (_JB_R13 * 8)(%r12),%r13 |
| 153 | movq (_JB_R14 * 8)(%r12),%r14 |
| 154 | movq (_JB_R15 * 8)(%r12),%r15 |
| 155 | movq (_JB_RSP * 8)(%r12),%rsp |
| 156 | movq (_JB_PC * 8)(%r12),%r11 |
| 157 | movq (_JB_R12 * 8)(%r12),%r12 |
Josh Gao | 2342e64 | 2015-09-16 18:42:45 -0700 | [diff] [blame] | 158 | m_unmangle_registers %rcx |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 159 | |
Josh Gao | 2342e64 | 2015-09-16 18:42:45 -0700 | [diff] [blame] | 160 | // Check the cookie. |
| 161 | pushq %rax |
Josh Gao | 8dbf02d | 2015-10-07 13:51:59 -0700 | [diff] [blame] | 162 | pushq %r11 |
Josh Gao | 2342e64 | 2015-09-16 18:42:45 -0700 | [diff] [blame] | 163 | movq %rcx, %rdi |
| 164 | call PIC_PLT(__bionic_setjmp_cookie_check) |
Josh Gao | 8dbf02d | 2015-10-07 13:51:59 -0700 | [diff] [blame] | 165 | popq %r11 |
Josh Gao | 2342e64 | 2015-09-16 18:42:45 -0700 | [diff] [blame] | 166 | popq %rax |
| 167 | |
| 168 | // Return 1 if value is 0. |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 169 | testl %eax,%eax |
| 170 | jnz 1f |
| 171 | incl %eax |
| 172 | 1: |
| 173 | movq %r11,0(%rsp) |
| 174 | ret |
| 175 | END(siglongjmp) |
| 176 | |
Christopher Ferris | 2495851 | 2015-03-25 09:12:00 -0700 | [diff] [blame] | 177 | ALIAS_SYMBOL(longjmp, siglongjmp) |
| 178 | ALIAS_SYMBOL(_longjmp, siglongjmp) |