The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /*- |
| 2 | * Copyright (c) 1990 The Regents of the University of California. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This code is derived from software contributed to Berkeley by |
| 6 | * William Jolitz. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * 3. Neither the name of the University nor the names of its contributors |
| 17 | * may be used to endorse or promote products derived from this software |
| 18 | * without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 30 | * SUCH DAMAGE. |
| 31 | */ |
| 32 | |
Elliott Hughes | 851e68a | 2014-02-19 16:53:20 -0800 | [diff] [blame] | 33 | #include <private/bionic_asm.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 34 | |
Josh Gao | 9260785 | 2016-03-29 14:03:09 -0700 | [diff] [blame] | 35 | // The internal structure of a jmp_buf is totally private. |
| 36 | // Current layout (changes from release to release): |
| 37 | // |
| 38 | // word name description |
| 39 | // 0 edx registers |
| 40 | // 1 ebx |
| 41 | // 2 esp |
| 42 | // 3 ebp |
| 43 | // 4 esi |
| 44 | // 5 edi |
| 45 | // 6 sigmask signal mask (not used with _setjmp / _longjmp) |
| 46 | // 7 sigflag/cookie setjmp cookie in top 31 bits, signal mask flag in low bit |
| 47 | // 8 checksum checksum of the core registers, to give better error messages. |
| 48 | // 9 reserved |
| 49 | |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 50 | #define _JB_EDX 0 |
| 51 | #define _JB_EBX 1 |
| 52 | #define _JB_ESP 2 |
| 53 | #define _JB_EBP 3 |
| 54 | #define _JB_ESI 4 |
| 55 | #define _JB_EDI 5 |
| 56 | #define _JB_SIGMASK 6 |
| 57 | #define _JB_SIGFLAG 7 |
Josh Gao | 9260785 | 2016-03-29 14:03:09 -0700 | [diff] [blame] | 58 | #define _JB_CHECKSUM 8 |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 59 | |
Josh Gao | 85c14fb | 2015-09-15 11:30:35 -0700 | [diff] [blame] | 60 | .macro m_mangle_registers reg |
| 61 | xorl \reg,%edx |
| 62 | xorl \reg,%ebx |
| 63 | xorl \reg,%esp |
| 64 | xorl \reg,%ebp |
| 65 | xorl \reg,%esi |
| 66 | xorl \reg,%edi |
| 67 | .endm |
| 68 | |
| 69 | .macro m_unmangle_registers reg |
| 70 | m_mangle_registers \reg |
| 71 | .endm |
| 72 | |
Josh Gao | 9260785 | 2016-03-29 14:03:09 -0700 | [diff] [blame] | 73 | .macro m_calculate_checksum dst, src |
| 74 | movl $0, \dst |
| 75 | .irp i,0,1,2,3,4,5 |
| 76 | xorl (\i*4)(\src), \dst |
| 77 | .endr |
| 78 | .endm |
| 79 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 80 | ENTRY(setjmp) |
dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 81 | __BIONIC_WEAK_ASM_FOR_NATIVE_BRIDGE(setjmp) |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 82 | movl 4(%esp),%ecx |
Josh Gao | 85c14fb | 2015-09-15 11:30:35 -0700 | [diff] [blame] | 83 | mov $1,%eax |
| 84 | jmp .L_sigsetjmp |
Elliott Hughes | 6719500 | 2013-02-13 15:12:32 -0800 | [diff] [blame] | 85 | END(setjmp) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 86 | |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 87 | ENTRY(_setjmp) |
dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 88 | __BIONIC_WEAK_ASM_FOR_NATIVE_BRIDGE(_setjmp) |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 89 | movl 4(%esp),%ecx |
Josh Gao | 85c14fb | 2015-09-15 11:30:35 -0700 | [diff] [blame] | 90 | movl $0,%eax |
| 91 | jmp .L_sigsetjmp |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 92 | END(_setjmp) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 93 | |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 94 | ENTRY(sigsetjmp) |
dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 95 | __BIONIC_WEAK_ASM_FOR_NATIVE_BRIDGE(sigsetjmp) |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 96 | movl 4(%esp),%ecx |
| 97 | movl 8(%esp),%eax |
| 98 | |
Josh Gao | 85c14fb | 2015-09-15 11:30:35 -0700 | [diff] [blame] | 99 | .L_sigsetjmp: |
| 100 | PIC_PROLOGUE |
| 101 | pushl %eax |
| 102 | call PIC_PLT(__bionic_setjmp_cookie_get) |
| 103 | addl $4,%esp |
| 104 | PIC_EPILOGUE |
| 105 | |
| 106 | // Record the setjmp cookie and whether or not we're saving the signal mask. |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 107 | movl %eax,(_JB_SIGFLAG * 4)(%ecx) |
| 108 | |
| 109 | // Do we need to save the signal mask? |
Josh Gao | 85c14fb | 2015-09-15 11:30:35 -0700 | [diff] [blame] | 110 | testl $1,%eax |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 111 | jz 1f |
| 112 | |
Elliott Hughes | 7ebafb3 | 2018-01-29 10:23:01 -0800 | [diff] [blame^] | 113 | // Save the current signal mask. |
| 114 | pushl %ecx |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 115 | PIC_PROLOGUE |
Elliott Hughes | 7ebafb3 | 2018-01-29 10:23:01 -0800 | [diff] [blame^] | 116 | leal (_JB_SIGMASK * 4)(%ecx),%eax |
| 117 | pushl %eax |
| 118 | pushl $0 // NULL |
| 119 | pushl $2 // SIG_SETMASK |
| 120 | call PIC_PLT(sigprocmask) |
| 121 | addl $12,%esp |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 122 | PIC_EPILOGUE |
Elliott Hughes | 7ebafb3 | 2018-01-29 10:23:01 -0800 | [diff] [blame^] | 123 | popl %ecx |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 124 | |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 125 | 1: |
Josh Gao | 85c14fb | 2015-09-15 11:30:35 -0700 | [diff] [blame] | 126 | // Fetch the setjmp cookie and clear the signal flag bit. |
| 127 | movl (_JB_SIGFLAG * 4)(%ecx),%eax |
| 128 | andl $-2,%eax |
| 129 | |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 130 | // Save the callee-save registers. |
| 131 | movl 0(%esp),%edx |
Josh Gao | 85c14fb | 2015-09-15 11:30:35 -0700 | [diff] [blame] | 132 | m_mangle_registers %eax |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 133 | movl %edx,(_JB_EDX * 4)(%ecx) |
| 134 | movl %ebx,(_JB_EBX * 4)(%ecx) |
| 135 | movl %esp,(_JB_ESP * 4)(%ecx) |
| 136 | movl %ebp,(_JB_EBP * 4)(%ecx) |
| 137 | movl %esi,(_JB_ESI * 4)(%ecx) |
| 138 | movl %edi,(_JB_EDI * 4)(%ecx) |
Josh Gao | 85c14fb | 2015-09-15 11:30:35 -0700 | [diff] [blame] | 139 | m_unmangle_registers %eax |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 140 | |
Josh Gao | 9260785 | 2016-03-29 14:03:09 -0700 | [diff] [blame] | 141 | m_calculate_checksum %eax, %ecx |
| 142 | movl %eax, (_JB_CHECKSUM * 4)(%ecx) |
| 143 | |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 144 | xorl %eax,%eax |
| 145 | ret |
| 146 | END(sigsetjmp) |
| 147 | |
| 148 | ENTRY(siglongjmp) |
dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 149 | __BIONIC_WEAK_ASM_FOR_NATIVE_BRIDGE(siglongjmp) |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 150 | movl 4(%esp),%edx |
Josh Gao | 9260785 | 2016-03-29 14:03:09 -0700 | [diff] [blame] | 151 | |
| 152 | // Check the checksum before doing anything. |
| 153 | m_calculate_checksum %eax, %edx |
| 154 | xorl (_JB_CHECKSUM * 4)(%edx), %eax |
| 155 | jnz 3f |
| 156 | |
| 157 | // Do we have a signal mask to restore? |
Josh Gao | 85c14fb | 2015-09-15 11:30:35 -0700 | [diff] [blame] | 158 | movl (_JB_SIGFLAG * 4)(%edx), %eax |
| 159 | testl $1,%eax |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 160 | jz 1f |
| 161 | |
| 162 | // Restore the signal mask. |
Elliott Hughes | 7ebafb3 | 2018-01-29 10:23:01 -0800 | [diff] [blame^] | 163 | leal (_JB_SIGMASK * 4)(%edx),%eax |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 164 | PIC_PROLOGUE |
Elliott Hughes | 7ebafb3 | 2018-01-29 10:23:01 -0800 | [diff] [blame^] | 165 | pushl $0 // NULL |
| 166 | pushl %eax |
| 167 | pushl $2 // SIG_SETMASK |
| 168 | call PIC_PLT(sigprocmask) |
| 169 | addl $12,%esp |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 170 | PIC_EPILOGUE |
| 171 | |
| 172 | 1: |
| 173 | // Restore the callee-save registers. |
| 174 | movl 4(%esp),%edx |
| 175 | movl 8(%esp),%eax |
Josh Gao | 85c14fb | 2015-09-15 11:30:35 -0700 | [diff] [blame] | 176 | |
| 177 | movl (_JB_SIGFLAG * 4)(%edx),%ecx |
| 178 | andl $-2,%ecx |
| 179 | |
| 180 | movl %ecx,%ebx |
| 181 | movl %ecx,%esp |
| 182 | movl %ecx,%ebp |
| 183 | movl %ecx,%esi |
| 184 | movl %ecx,%edi |
| 185 | xorl (_JB_EDX * 4)(%edx),%ecx |
| 186 | xorl (_JB_EBX * 4)(%edx),%ebx |
| 187 | xorl (_JB_ESP * 4)(%edx),%esp |
| 188 | xorl (_JB_EBP * 4)(%edx),%ebp |
| 189 | xorl (_JB_ESI * 4)(%edx),%esi |
| 190 | xorl (_JB_EDI * 4)(%edx),%edi |
| 191 | |
| 192 | PIC_PROLOGUE |
| 193 | pushl %eax |
Josh Gao | 8dbf02d | 2015-10-07 13:51:59 -0700 | [diff] [blame] | 194 | pushl %ecx |
Josh Gao | 85c14fb | 2015-09-15 11:30:35 -0700 | [diff] [blame] | 195 | pushl (_JB_SIGFLAG * 4)(%edx) |
| 196 | call PIC_PLT(__bionic_setjmp_cookie_check) |
| 197 | addl $4,%esp |
Josh Gao | 8dbf02d | 2015-10-07 13:51:59 -0700 | [diff] [blame] | 198 | popl %ecx |
Josh Gao | 85c14fb | 2015-09-15 11:30:35 -0700 | [diff] [blame] | 199 | popl %eax |
| 200 | PIC_EPILOGUE |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 201 | |
| 202 | testl %eax,%eax |
| 203 | jnz 2f |
| 204 | incl %eax |
| 205 | 2: |
| 206 | movl %ecx,0(%esp) |
| 207 | ret |
Josh Gao | 9260785 | 2016-03-29 14:03:09 -0700 | [diff] [blame] | 208 | |
| 209 | 3: |
| 210 | PIC_PROLOGUE |
| 211 | pushl (_JB_SIGMASK * 4)(%edx) |
| 212 | call PIC_PLT(__bionic_setjmp_checksum_mismatch) |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 213 | END(siglongjmp) |
| 214 | |
Christopher Ferris | 2495851 | 2015-03-25 09:12:00 -0700 | [diff] [blame] | 215 | ALIAS_SYMBOL(longjmp, siglongjmp) |
dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 216 | __BIONIC_WEAK_ASM_FOR_NATIVE_BRIDGE(longjmp) |
Christopher Ferris | 2495851 | 2015-03-25 09:12:00 -0700 | [diff] [blame] | 217 | ALIAS_SYMBOL(_longjmp, siglongjmp) |
dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 218 | __BIONIC_WEAK_ASM_FOR_NATIVE_BRIDGE(_longjmp) |