blob: efb645993ca075900ee017a00764c650e33eb4fd [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)
Elliott Hughes8d4c55c2014-12-05 16:25:50 -080081 movl 4(%esp),%ecx
Josh Gao85c14fb2015-09-15 11:30:35 -070082 mov $1,%eax
83 jmp .L_sigsetjmp
Elliott Hughes67195002013-02-13 15:12:32 -080084END(setjmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080085
Elliott Hughes8d4c55c2014-12-05 16:25:50 -080086ENTRY(_setjmp)
87 movl 4(%esp),%ecx
Josh Gao85c14fb2015-09-15 11:30:35 -070088 movl $0,%eax
89 jmp .L_sigsetjmp
Elliott Hughes8d4c55c2014-12-05 16:25:50 -080090END(_setjmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080091
Elliott Hughes8d4c55c2014-12-05 16:25:50 -080092ENTRY(sigsetjmp)
93 movl 4(%esp),%ecx
94 movl 8(%esp),%eax
95
Josh Gao85c14fb2015-09-15 11:30:35 -070096.L_sigsetjmp:
97 PIC_PROLOGUE
98 pushl %eax
99 call PIC_PLT(__bionic_setjmp_cookie_get)
100 addl $4,%esp
101 PIC_EPILOGUE
102
103 // Record the setjmp cookie and whether or not we're saving the signal mask.
Elliott Hughes8d4c55c2014-12-05 16:25:50 -0800104 movl %eax,(_JB_SIGFLAG * 4)(%ecx)
105
106 // Do we need to save the signal mask?
Josh Gao85c14fb2015-09-15 11:30:35 -0700107 testl $1,%eax
Elliott Hughes8d4c55c2014-12-05 16:25:50 -0800108 jz 1f
109
Elliott Hughes8d4c55c2014-12-05 16:25:50 -0800110 // Get the current signal mask.
111 PIC_PROLOGUE
112 pushl $0
113 call PIC_PLT(sigblock)
114 addl $4,%esp
115 PIC_EPILOGUE
116
117 // Save the signal mask.
118 movl 4(%esp),%ecx
119 movl %eax,(_JB_SIGMASK * 4)(%ecx)
120
Elliott Hughes8d4c55c2014-12-05 16:25:50 -08001211:
Josh Gao85c14fb2015-09-15 11:30:35 -0700122 // Fetch the setjmp cookie and clear the signal flag bit.
123 movl (_JB_SIGFLAG * 4)(%ecx),%eax
124 andl $-2,%eax
125
Elliott Hughes8d4c55c2014-12-05 16:25:50 -0800126 // Save the callee-save registers.
127 movl 0(%esp),%edx
Josh Gao85c14fb2015-09-15 11:30:35 -0700128 m_mangle_registers %eax
Elliott Hughes8d4c55c2014-12-05 16:25:50 -0800129 movl %edx,(_JB_EDX * 4)(%ecx)
130 movl %ebx,(_JB_EBX * 4)(%ecx)
131 movl %esp,(_JB_ESP * 4)(%ecx)
132 movl %ebp,(_JB_EBP * 4)(%ecx)
133 movl %esi,(_JB_ESI * 4)(%ecx)
134 movl %edi,(_JB_EDI * 4)(%ecx)
Josh Gao85c14fb2015-09-15 11:30:35 -0700135 m_unmangle_registers %eax
Elliott Hughes8d4c55c2014-12-05 16:25:50 -0800136
Josh Gao92607852016-03-29 14:03:09 -0700137 m_calculate_checksum %eax, %ecx
138 movl %eax, (_JB_CHECKSUM * 4)(%ecx)
139
Elliott Hughes8d4c55c2014-12-05 16:25:50 -0800140 xorl %eax,%eax
141 ret
142END(sigsetjmp)
143
144ENTRY(siglongjmp)
Elliott Hughes8d4c55c2014-12-05 16:25:50 -0800145 movl 4(%esp),%edx
Josh Gao92607852016-03-29 14:03:09 -0700146
147 // Check the checksum before doing anything.
148 m_calculate_checksum %eax, %edx
149 xorl (_JB_CHECKSUM * 4)(%edx), %eax
150 jnz 3f
151
152 // Do we have a signal mask to restore?
Josh Gao85c14fb2015-09-15 11:30:35 -0700153 movl (_JB_SIGFLAG * 4)(%edx), %eax
154 testl $1,%eax
Elliott Hughes8d4c55c2014-12-05 16:25:50 -0800155 jz 1f
156
157 // Restore the signal mask.
158 PIC_PROLOGUE
159 pushl (_JB_SIGMASK * 4)(%edx)
160 call PIC_PLT(sigsetmask)
161 addl $4,%esp
162 PIC_EPILOGUE
163
1641:
165 // Restore the callee-save registers.
166 movl 4(%esp),%edx
167 movl 8(%esp),%eax
Josh Gao85c14fb2015-09-15 11:30:35 -0700168
169 movl (_JB_SIGFLAG * 4)(%edx),%ecx
170 andl $-2,%ecx
171
172 movl %ecx,%ebx
173 movl %ecx,%esp
174 movl %ecx,%ebp
175 movl %ecx,%esi
176 movl %ecx,%edi
177 xorl (_JB_EDX * 4)(%edx),%ecx
178 xorl (_JB_EBX * 4)(%edx),%ebx
179 xorl (_JB_ESP * 4)(%edx),%esp
180 xorl (_JB_EBP * 4)(%edx),%ebp
181 xorl (_JB_ESI * 4)(%edx),%esi
182 xorl (_JB_EDI * 4)(%edx),%edi
183
184 PIC_PROLOGUE
185 pushl %eax
Josh Gao8dbf02d2015-10-07 13:51:59 -0700186 pushl %ecx
Josh Gao85c14fb2015-09-15 11:30:35 -0700187 pushl (_JB_SIGFLAG * 4)(%edx)
188 call PIC_PLT(__bionic_setjmp_cookie_check)
189 addl $4,%esp
Josh Gao8dbf02d2015-10-07 13:51:59 -0700190 popl %ecx
Josh Gao85c14fb2015-09-15 11:30:35 -0700191 popl %eax
192 PIC_EPILOGUE
Elliott Hughes8d4c55c2014-12-05 16:25:50 -0800193
194 testl %eax,%eax
195 jnz 2f
196 incl %eax
1972:
198 movl %ecx,0(%esp)
199 ret
Josh Gao92607852016-03-29 14:03:09 -0700200
2013:
202 PIC_PROLOGUE
203 pushl (_JB_SIGMASK * 4)(%edx)
204 call PIC_PLT(__bionic_setjmp_checksum_mismatch)
Elliott Hughes8d4c55c2014-12-05 16:25:50 -0800205END(siglongjmp)
206
Christopher Ferris24958512015-03-25 09:12:00 -0700207ALIAS_SYMBOL(longjmp, siglongjmp)
208ALIAS_SYMBOL(_longjmp, siglongjmp)