blob: 1a3eb4b74306051585c47dc6393ea924fe6a1a72 [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
Elliott Hughes460130b2018-01-31 09:05:26 -080045// 6 sigmask 64-bit signal mask (not used with _setjmp / _longjmp)
46// 7 " "
47// 8 sigflag/cookie setjmp cookie in top 31 bits, signal mask flag in low bit
48// 9 checksum checksum of the core registers, to give better error messages.
Josh Gao92607852016-03-29 14:03:09 -070049
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
Elliott Hughes460130b2018-01-31 09:05:26 -080057#define _JB_SIGFLAG 8
58#define _JB_CHECKSUM 9
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080059
Josh Gao92607852016-03-29 14:03:09 -070060.macro m_calculate_checksum dst, src
61 movl $0, \dst
62 .irp i,0,1,2,3,4,5
63 xorl (\i*4)(\src), \dst
64 .endr
65.endm
66
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080067ENTRY(setjmp)
dimitryfa432522017-10-25 13:07:45 +020068__BIONIC_WEAK_ASM_FOR_NATIVE_BRIDGE(setjmp)
Elliott Hughes8d4c55c2014-12-05 16:25:50 -080069 movl 4(%esp),%ecx
Josh Gao85c14fb2015-09-15 11:30:35 -070070 mov $1,%eax
71 jmp .L_sigsetjmp
Elliott Hughes67195002013-02-13 15:12:32 -080072END(setjmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080073
Elliott Hughes8d4c55c2014-12-05 16:25:50 -080074ENTRY(_setjmp)
dimitryfa432522017-10-25 13:07:45 +020075__BIONIC_WEAK_ASM_FOR_NATIVE_BRIDGE(_setjmp)
Elliott Hughes8d4c55c2014-12-05 16:25:50 -080076 movl 4(%esp),%ecx
Josh Gao85c14fb2015-09-15 11:30:35 -070077 movl $0,%eax
78 jmp .L_sigsetjmp
Elliott Hughes8d4c55c2014-12-05 16:25:50 -080079END(_setjmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080080
Elliott Hughes8d4c55c2014-12-05 16:25:50 -080081ENTRY(sigsetjmp)
dimitryfa432522017-10-25 13:07:45 +020082__BIONIC_WEAK_ASM_FOR_NATIVE_BRIDGE(sigsetjmp)
Elliott Hughes8d4c55c2014-12-05 16:25:50 -080083 movl 4(%esp),%ecx
84 movl 8(%esp),%eax
85
Josh Gao85c14fb2015-09-15 11:30:35 -070086.L_sigsetjmp:
87 PIC_PROLOGUE
88 pushl %eax
89 call PIC_PLT(__bionic_setjmp_cookie_get)
90 addl $4,%esp
91 PIC_EPILOGUE
92
93 // Record the setjmp cookie and whether or not we're saving the signal mask.
Elliott Hughes8d4c55c2014-12-05 16:25:50 -080094 movl %eax,(_JB_SIGFLAG * 4)(%ecx)
95
96 // Do we need to save the signal mask?
Josh Gao85c14fb2015-09-15 11:30:35 -070097 testl $1,%eax
Elliott Hughes8d4c55c2014-12-05 16:25:50 -080098 jz 1f
99
Elliott Hughes7ebafb32018-01-29 10:23:01 -0800100 // Save the current signal mask.
101 pushl %ecx
Elliott Hughes8d4c55c2014-12-05 16:25:50 -0800102 PIC_PROLOGUE
Elliott Hughes7ebafb32018-01-29 10:23:01 -0800103 leal (_JB_SIGMASK * 4)(%ecx),%eax
104 pushl %eax
105 pushl $0 // NULL
106 pushl $2 // SIG_SETMASK
Elliott Hughes460130b2018-01-31 09:05:26 -0800107 call PIC_PLT(sigprocmask64)
Elliott Hughes7ebafb32018-01-29 10:23:01 -0800108 addl $12,%esp
Elliott Hughes8d4c55c2014-12-05 16:25:50 -0800109 PIC_EPILOGUE
Elliott Hughes7ebafb32018-01-29 10:23:01 -0800110 popl %ecx
Elliott Hughes8d4c55c2014-12-05 16:25:50 -0800111
Elliott Hughes8d4c55c2014-12-05 16:25:50 -08001121:
Josh Gao85c14fb2015-09-15 11:30:35 -0700113 // Fetch the setjmp cookie and clear the signal flag bit.
114 movl (_JB_SIGFLAG * 4)(%ecx),%eax
115 andl $-2,%eax
116
Elliott Hughes8d4c55c2014-12-05 16:25:50 -0800117 // Save the callee-save registers.
118 movl 0(%esp),%edx
Elliott Hughesc0d41db2021-04-02 18:02:38 -0700119
120.macro m_mangle_register reg, offset
121 movl \reg,(\offset * 4)(%ecx)
122 xorl %eax,(\offset * 4)(%ecx)
123.endm
124 m_mangle_register %edx, _JB_EDX
125 m_mangle_register %ebx, _JB_EBX
126 m_mangle_register %esp, _JB_ESP
127 m_mangle_register %ebp, _JB_EBP
128 m_mangle_register %esi, _JB_ESI
129 m_mangle_register %edi, _JB_EDI
Elliott Hughes8d4c55c2014-12-05 16:25:50 -0800130
Josh Gao92607852016-03-29 14:03:09 -0700131 m_calculate_checksum %eax, %ecx
132 movl %eax, (_JB_CHECKSUM * 4)(%ecx)
133
Elliott Hughes8d4c55c2014-12-05 16:25:50 -0800134 xorl %eax,%eax
135 ret
136END(sigsetjmp)
137
138ENTRY(siglongjmp)
dimitryfa432522017-10-25 13:07:45 +0200139__BIONIC_WEAK_ASM_FOR_NATIVE_BRIDGE(siglongjmp)
Elliott Hughes8d4c55c2014-12-05 16:25:50 -0800140 movl 4(%esp),%edx
Josh Gao92607852016-03-29 14:03:09 -0700141
142 // Check the checksum before doing anything.
143 m_calculate_checksum %eax, %edx
144 xorl (_JB_CHECKSUM * 4)(%edx), %eax
145 jnz 3f
146
147 // Do we have a signal mask to restore?
Josh Gao85c14fb2015-09-15 11:30:35 -0700148 movl (_JB_SIGFLAG * 4)(%edx), %eax
149 testl $1,%eax
Elliott Hughes8d4c55c2014-12-05 16:25:50 -0800150 jz 1f
151
152 // Restore the signal mask.
Elliott Hughes7ebafb32018-01-29 10:23:01 -0800153 leal (_JB_SIGMASK * 4)(%edx),%eax
Elliott Hughes8d4c55c2014-12-05 16:25:50 -0800154 PIC_PROLOGUE
Elliott Hughes7ebafb32018-01-29 10:23:01 -0800155 pushl $0 // NULL
156 pushl %eax
157 pushl $2 // SIG_SETMASK
Elliott Hughes460130b2018-01-31 09:05:26 -0800158 call PIC_PLT(sigprocmask64)
Elliott Hughes7ebafb32018-01-29 10:23:01 -0800159 addl $12,%esp
Elliott Hughes8d4c55c2014-12-05 16:25:50 -0800160 PIC_EPILOGUE
161
1621:
163 // Restore the callee-save registers.
164 movl 4(%esp),%edx
165 movl 8(%esp),%eax
Josh Gao85c14fb2015-09-15 11:30:35 -0700166
Elliott Hughesc0d41db2021-04-02 18:02:38 -0700167 // Fetch the setjmp cookie and clear the signal flag bit.
Josh Gao85c14fb2015-09-15 11:30:35 -0700168 movl (_JB_SIGFLAG * 4)(%edx),%ecx
169 andl $-2,%ecx
170
Elliott Hughesc0d41db2021-04-02 18:02:38 -0700171 // Carefully unmangle esp/ebp without ever having an invalid value in the
172 // register (http://b/152210274).
173 movl (_JB_ESP * 4)(%edx),%edi
174 xorl %ecx,%edi
175 movl %edi,%esp
176 movl (_JB_EBP * 4)(%edx),%edi
177 xorl %ecx,%edi
178 movl %edi,%ebp
179
180 // The others don't matter as much, but we do need to finish using the cookie
181 // from %ecx before we clobber it, so we seed each register with the cookie.
Josh Gao85c14fb2015-09-15 11:30:35 -0700182 movl %ecx,%ebx
Josh Gao85c14fb2015-09-15 11:30:35 -0700183 movl %ecx,%esi
184 movl %ecx,%edi
185 xorl (_JB_EDX * 4)(%edx),%ecx
186 xorl (_JB_EBX * 4)(%edx),%ebx
Josh Gao85c14fb2015-09-15 11:30:35 -0700187 xorl (_JB_ESI * 4)(%edx),%esi
188 xorl (_JB_EDI * 4)(%edx),%edi
189
190 PIC_PROLOGUE
191 pushl %eax
Josh Gao8dbf02d2015-10-07 13:51:59 -0700192 pushl %ecx
Josh Gao85c14fb2015-09-15 11:30:35 -0700193 pushl (_JB_SIGFLAG * 4)(%edx)
194 call PIC_PLT(__bionic_setjmp_cookie_check)
195 addl $4,%esp
Josh Gao8dbf02d2015-10-07 13:51:59 -0700196 popl %ecx
Josh Gao85c14fb2015-09-15 11:30:35 -0700197 popl %eax
198 PIC_EPILOGUE
Elliott Hughes8d4c55c2014-12-05 16:25:50 -0800199
200 testl %eax,%eax
201 jnz 2f
202 incl %eax
2032:
204 movl %ecx,0(%esp)
205 ret
Josh Gao92607852016-03-29 14:03:09 -0700206
2073:
208 PIC_PROLOGUE
209 pushl (_JB_SIGMASK * 4)(%edx)
210 call PIC_PLT(__bionic_setjmp_checksum_mismatch)
Elliott Hughes8d4c55c2014-12-05 16:25:50 -0800211END(siglongjmp)
212
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)
Christopher Ferris24958512015-03-25 09:12:00 -0700215ALIAS_SYMBOL(_longjmp, siglongjmp)
dimitryfa432522017-10-25 13:07:45 +0200216__BIONIC_WEAK_ASM_FOR_NATIVE_BRIDGE(_longjmp)