blob: 6e6c3650ad55ede3c5c9fe6c2e4127cfb7c2fad6 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*-
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 Hughes851e68a2014-02-19 16:53:20 -080033#include <private/bionic_asm.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080034
Josh Gao92607852016-03-29 14:03:09 -070035// 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 Hughes8d4c55c2014-12-05 16:25:50 -080050#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 Gao92607852016-03-29 14:03:09 -070058#define _JB_CHECKSUM 8
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080059
Josh Gao85c14fb2015-09-15 11:30:35 -070060.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 Gao92607852016-03-29 14:03:09 -070073.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 Project1dc9e472009-03-03 19:28:35 -080080ENTRY(setjmp)
dimitryfa432522017-10-25 13:07:45 +020081__BIONIC_WEAK_ASM_FOR_NATIVE_BRIDGE(setjmp)
Elliott Hughes8d4c55c2014-12-05 16:25:50 -080082 movl 4(%esp),%ecx
Josh Gao85c14fb2015-09-15 11:30:35 -070083 mov $1,%eax
84 jmp .L_sigsetjmp
Elliott Hughes67195002013-02-13 15:12:32 -080085END(setjmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080086
Elliott Hughes8d4c55c2014-12-05 16:25:50 -080087ENTRY(_setjmp)
dimitryfa432522017-10-25 13:07:45 +020088__BIONIC_WEAK_ASM_FOR_NATIVE_BRIDGE(_setjmp)
Elliott Hughes8d4c55c2014-12-05 16:25:50 -080089 movl 4(%esp),%ecx
Josh Gao85c14fb2015-09-15 11:30:35 -070090 movl $0,%eax
91 jmp .L_sigsetjmp
Elliott Hughes8d4c55c2014-12-05 16:25:50 -080092END(_setjmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080093
Elliott Hughes8d4c55c2014-12-05 16:25:50 -080094ENTRY(sigsetjmp)
dimitryfa432522017-10-25 13:07:45 +020095__BIONIC_WEAK_ASM_FOR_NATIVE_BRIDGE(sigsetjmp)
Elliott Hughes8d4c55c2014-12-05 16:25:50 -080096 movl 4(%esp),%ecx
97 movl 8(%esp),%eax
98
Josh Gao85c14fb2015-09-15 11:30:35 -070099.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 Hughes8d4c55c2014-12-05 16:25:50 -0800107 movl %eax,(_JB_SIGFLAG * 4)(%ecx)
108
109 // Do we need to save the signal mask?
Josh Gao85c14fb2015-09-15 11:30:35 -0700110 testl $1,%eax
Elliott Hughes8d4c55c2014-12-05 16:25:50 -0800111 jz 1f
112
Elliott Hughes8d4c55c2014-12-05 16:25:50 -0800113 // Get the current signal mask.
114 PIC_PROLOGUE
115 pushl $0
116 call PIC_PLT(sigblock)
117 addl $4,%esp
118 PIC_EPILOGUE
119
120 // Save the signal mask.
121 movl 4(%esp),%ecx
122 movl %eax,(_JB_SIGMASK * 4)(%ecx)
123
Elliott Hughes8d4c55c2014-12-05 16:25:50 -08001241:
Josh Gao85c14fb2015-09-15 11:30:35 -0700125 // Fetch the setjmp cookie and clear the signal flag bit.
126 movl (_JB_SIGFLAG * 4)(%ecx),%eax
127 andl $-2,%eax
128
Elliott Hughes8d4c55c2014-12-05 16:25:50 -0800129 // Save the callee-save registers.
130 movl 0(%esp),%edx
Josh Gao85c14fb2015-09-15 11:30:35 -0700131 m_mangle_registers %eax
Elliott Hughes8d4c55c2014-12-05 16:25:50 -0800132 movl %edx,(_JB_EDX * 4)(%ecx)
133 movl %ebx,(_JB_EBX * 4)(%ecx)
134 movl %esp,(_JB_ESP * 4)(%ecx)
135 movl %ebp,(_JB_EBP * 4)(%ecx)
136 movl %esi,(_JB_ESI * 4)(%ecx)
137 movl %edi,(_JB_EDI * 4)(%ecx)
Josh Gao85c14fb2015-09-15 11:30:35 -0700138 m_unmangle_registers %eax
Elliott Hughes8d4c55c2014-12-05 16:25:50 -0800139
Josh Gao92607852016-03-29 14:03:09 -0700140 m_calculate_checksum %eax, %ecx
141 movl %eax, (_JB_CHECKSUM * 4)(%ecx)
142
Elliott Hughes8d4c55c2014-12-05 16:25:50 -0800143 xorl %eax,%eax
144 ret
145END(sigsetjmp)
146
147ENTRY(siglongjmp)
dimitryfa432522017-10-25 13:07:45 +0200148__BIONIC_WEAK_ASM_FOR_NATIVE_BRIDGE(siglongjmp)
Elliott Hughes8d4c55c2014-12-05 16:25:50 -0800149 movl 4(%esp),%edx
Josh Gao92607852016-03-29 14:03:09 -0700150
151 // Check the checksum before doing anything.
152 m_calculate_checksum %eax, %edx
153 xorl (_JB_CHECKSUM * 4)(%edx), %eax
154 jnz 3f
155
156 // Do we have a signal mask to restore?
Josh Gao85c14fb2015-09-15 11:30:35 -0700157 movl (_JB_SIGFLAG * 4)(%edx), %eax
158 testl $1,%eax
Elliott Hughes8d4c55c2014-12-05 16:25:50 -0800159 jz 1f
160
161 // Restore the signal mask.
162 PIC_PROLOGUE
163 pushl (_JB_SIGMASK * 4)(%edx)
164 call PIC_PLT(sigsetmask)
165 addl $4,%esp
166 PIC_EPILOGUE
167
1681:
169 // Restore the callee-save registers.
170 movl 4(%esp),%edx
171 movl 8(%esp),%eax
Josh Gao85c14fb2015-09-15 11:30:35 -0700172
173 movl (_JB_SIGFLAG * 4)(%edx),%ecx
174 andl $-2,%ecx
175
176 movl %ecx,%ebx
177 movl %ecx,%esp
178 movl %ecx,%ebp
179 movl %ecx,%esi
180 movl %ecx,%edi
181 xorl (_JB_EDX * 4)(%edx),%ecx
182 xorl (_JB_EBX * 4)(%edx),%ebx
183 xorl (_JB_ESP * 4)(%edx),%esp
184 xorl (_JB_EBP * 4)(%edx),%ebp
185 xorl (_JB_ESI * 4)(%edx),%esi
186 xorl (_JB_EDI * 4)(%edx),%edi
187
188 PIC_PROLOGUE
189 pushl %eax
Josh Gao8dbf02d2015-10-07 13:51:59 -0700190 pushl %ecx
Josh Gao85c14fb2015-09-15 11:30:35 -0700191 pushl (_JB_SIGFLAG * 4)(%edx)
192 call PIC_PLT(__bionic_setjmp_cookie_check)
193 addl $4,%esp
Josh Gao8dbf02d2015-10-07 13:51:59 -0700194 popl %ecx
Josh Gao85c14fb2015-09-15 11:30:35 -0700195 popl %eax
196 PIC_EPILOGUE
Elliott Hughes8d4c55c2014-12-05 16:25:50 -0800197
198 testl %eax,%eax
199 jnz 2f
200 incl %eax
2012:
202 movl %ecx,0(%esp)
203 ret
Josh Gao92607852016-03-29 14:03:09 -0700204
2053:
206 PIC_PROLOGUE
207 pushl (_JB_SIGMASK * 4)(%edx)
208 call PIC_PLT(__bionic_setjmp_checksum_mismatch)
Elliott Hughes8d4c55c2014-12-05 16:25:50 -0800209END(siglongjmp)
210
Christopher Ferris24958512015-03-25 09:12:00 -0700211ALIAS_SYMBOL(longjmp, siglongjmp)
dimitryfa432522017-10-25 13:07:45 +0200212__BIONIC_WEAK_ASM_FOR_NATIVE_BRIDGE(longjmp)
Christopher Ferris24958512015-03-25 09:12:00 -0700213ALIAS_SYMBOL(_longjmp, siglongjmp)
dimitryfa432522017-10-25 13:07:45 +0200214__BIONIC_WEAK_ASM_FOR_NATIVE_BRIDGE(_longjmp)