blob: 4d19e681edc80b9d603f3a8e55656aa6441707c5 [file] [log] [blame]
sewardj6c591e12011-04-11 16:17:51 +00001
2/*--------------------------------------------------------------------*/
3/*--- A minimal setjmp/longjmp implementation. m_libcsetjmp.c ---*/
4/*--------------------------------------------------------------------*/
5
6/*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
9
10 Copyright (C) 2010-2010 Mozilla Inc
11
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of the
15 License, or (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 02111-1307, USA.
26
27 The GNU General Public License is contained in the file COPYING.
28*/
29
30/* Contributed by Julian Seward <jseward@acm.org> */
31
32
33#include "pub_core_basics.h"
34#include "pub_core_libcsetjmp.h" /* self */
35
36
37/* See include/pub_tool_libcsetjmp.h for background and rationale. */
38
sewardj97d3ebb2011-04-11 18:36:34 +000039/* The only alternative implementations are for ppc{32,64}-linux. See
40 #259977. */
41
sewardj2a88a012011-04-11 21:26:27 +000042/* ------------ ppc32-linux ------------ */
43
sewardj97d3ebb2011-04-11 18:36:34 +000044#if defined(VGP_ppc32_linux)
45
46__asm__(
47".text" "\n"
sewardj2a88a012011-04-11 21:26:27 +000048"" "\n"
sewardj97d3ebb2011-04-11 18:36:34 +000049".global VG_MINIMAL_SETJMP" "\n" // r3 = jmp_buf
50"VG_MINIMAL_SETJMP:" "\n"
51" stw 0, 0(3)" "\n"
52" stw 1, 4(3)" "\n"
53" stw 2, 8(3)" "\n"
54" stw 3, 12(3)" "\n"
55" stw 4, 16(3)" "\n"
56" stw 5, 20(3)" "\n"
57" stw 6, 24(3)" "\n"
58" stw 7, 28(3)" "\n"
59" stw 8, 32(3)" "\n"
60" stw 9, 36(3)" "\n"
61" stw 10, 40(3)" "\n"
62" stw 11, 44(3)" "\n"
63" stw 12, 48(3)" "\n"
64" stw 13, 52(3)" "\n"
65" stw 14, 56(3)" "\n"
66" stw 15, 60(3)" "\n"
67" stw 16, 64(3)" "\n"
68" stw 17, 68(3)" "\n"
69" stw 18, 72(3)" "\n"
70" stw 19, 76(3)" "\n"
71" stw 20, 80(3)" "\n"
72" stw 21, 84(3)" "\n"
73" stw 22, 88(3)" "\n"
74" stw 23, 92(3)" "\n"
75" stw 24, 96(3)" "\n"
76" stw 25, 100(3)" "\n"
77" stw 26, 104(3)" "\n"
78" stw 27, 108(3)" "\n"
79" stw 28, 112(3)" "\n"
80" stw 29, 116(3)" "\n"
81" stw 30, 120(3)" "\n"
82" stw 31, 124(3)" "\n"
83 // must use a caller-save register here as scratch, hence r4
84" mflr 4" "\n"
85" stw 4, 128(3)" "\n"
86" mfcr 4" "\n"
87" stw 4, 132(3)" "\n"
88" li 3, 0" "\n"
89" blr" "\n"
sewardj2a88a012011-04-11 21:26:27 +000090"" "\n"
sewardj97d3ebb2011-04-11 18:36:34 +000091
92
93".global VG_MINIMAL_LONGJMP" "\n"
94"VG_MINIMAL_LONGJMP:" "\n" // r3 = jmp_buf
95 // do r4 = 1
96 // and park it in the restore slot for r3 (the ret reg)
97" li 4, 1" "\n"
98" stw 4, 12(3)" "\n"
99 // restore everything except r3
100 // then r3 last of all
101 // then blr
102" lwz 0, 128(3)" "\n"
103" mtlr 0" "\n"
104" lwz 0, 132(3)" "\n"
105" mtcr 0" "\n"
106" lwz 0, 0(3)" "\n"
107" lwz 1, 4(3)" "\n"
108" lwz 2, 8(3)" "\n"
109 // r3 is done at the end
110" lwz 4, 16(3)" "\n"
111" lwz 5, 20(3)" "\n"
112" lwz 6, 24(3)" "\n"
113" lwz 7, 28(3)" "\n"
114" lwz 8, 32(3)" "\n"
115" lwz 9, 36(3)" "\n"
116" lwz 10, 40(3)" "\n"
117" lwz 11, 44(3)" "\n"
118" lwz 12, 48(3)" "\n"
119" lwz 13, 52(3)" "\n"
120" lwz 14, 56(3)" "\n"
121" lwz 15, 60(3)" "\n"
122" lwz 16, 64(3)" "\n"
123" lwz 17, 68(3)" "\n"
124" lwz 18, 72(3)" "\n"
125" lwz 19, 76(3)" "\n"
126" lwz 20, 80(3)" "\n"
127" lwz 21, 84(3)" "\n"
128" lwz 22, 88(3)" "\n"
129" lwz 23, 92(3)" "\n"
130" lwz 24, 96(3)" "\n"
131" lwz 25, 100(3)" "\n"
132" lwz 26, 104(3)" "\n"
133" lwz 27, 108(3)" "\n"
134" lwz 28, 112(3)" "\n"
135" lwz 29, 116(3)" "\n"
136" lwz 30, 120(3)" "\n"
137" lwz 31, 124(3)" "\n"
138" lwz 3, 12(3)" "\n"
139" blr" "\n"
sewardj2a88a012011-04-11 21:26:27 +0000140"" "\n"
sewardj97d3ebb2011-04-11 18:36:34 +0000141
142".previous" "\n"
143);
144
145#endif /* VGP_ppc32_linux */
sewardj6c591e12011-04-11 16:17:51 +0000146
147
sewardj2a88a012011-04-11 21:26:27 +0000148/* ------------ ppc64-linux ------------ */
149
150#if defined(VGP_ppc64_linux)
151
152__asm__(
153".section \".toc\",\"aw\"" "\n"
154
155".section \".text\"" "\n"
156".align 2" "\n"
157".p2align 4,,15" "\n"
158".globl VG_MINIMAL_SETJMP" "\n"
159
160".section \".opd\",\"aw\"" "\n"
161".align 3" "\n"
162"VG_MINIMAL_SETJMP:" "\n"
163".quad .L.VG_MINIMAL_SETJMP,.TOC.@tocbase,0" "\n"
164".previous" "\n"
165
166".type VG_MINIMAL_SETJMP, @function" "\n"
167".L.VG_MINIMAL_SETJMP:" "\n"
168" std 0, 0(3)" "\n"
169" std 1, 8(3)" "\n"
170" std 2, 16(3)" "\n"
171" std 3, 24(3)" "\n"
172" std 4, 32(3)" "\n"
173" std 5, 40(3)" "\n"
174" std 6, 48(3)" "\n"
175" std 7, 56(3)" "\n"
176" std 8, 64(3)" "\n"
177" std 9, 72(3)" "\n"
178" std 10, 80(3)" "\n"
179" std 11, 88(3)" "\n"
180" std 12, 96(3)" "\n"
181" std 13, 104(3)" "\n"
182" std 14, 112(3)" "\n"
183" std 15, 120(3)" "\n"
184" std 16, 128(3)" "\n"
185" std 17, 136(3)" "\n"
186" std 18, 144(3)" "\n"
187" std 19, 152(3)" "\n"
188" std 20, 160(3)" "\n"
189" std 21, 168(3)" "\n"
190" std 22, 176(3)" "\n"
191" std 23, 184(3)" "\n"
192" std 24, 192(3)" "\n"
193" std 25, 200(3)" "\n"
194" std 26, 208(3)" "\n"
195" std 27, 216(3)" "\n"
196" std 28, 224(3)" "\n"
197" std 29, 232(3)" "\n"
198" std 30, 240(3)" "\n"
199" std 31, 248(3)" "\n"
200 // must use a caller-save register here as scratch, hence r4
201" mflr 4" "\n"
202" std 4, 256(3)" "\n"
203" mfcr 4" "\n"
204" std 4, 264(3)" "\n"
205" li 3, 0" "\n"
206" blr" "\n"
207"" "\n"
208
209
210".globl VG_MINIMAL_LONGJMP" "\n"
211
212".section \".opd\",\"aw\"" "\n"
213".align 3" "\n"
214"VG_MINIMAL_LONGJMP:" "\n"
215".quad .L.VG_MINIMAL_LONGJMP,.TOC.@tocbase,0" "\n"
216".previous" "\n"
217
218".type VG_MINIMAL_LONGJMP, @function" "\n"
219".L.VG_MINIMAL_LONGJMP:" "\n"
220 // do r4 = 1
221 // and park it in the restore slot for r3 (the ret reg)
222" li 4, 1" "\n"
223" std 4, 24(3)" "\n"
224 // restore everything except r3
225 // then r3 last of all
226 // then blr
227" ld 0, 256(3)" "\n"
228" mtlr 0" "\n"
229" ld 0, 264(3)" "\n"
230" mtcr 0" "\n"
231" ld 0, 0(3)" "\n"
232" ld 1, 8(3)" "\n"
233" ld 2, 16(3)" "\n"
234 // r3 is done at the end
235" ld 4, 32(3)" "\n"
236" ld 5, 40(3)" "\n"
237" ld 6, 48(3)" "\n"
238" ld 7, 56(3)" "\n"
239" ld 8, 64(3)" "\n"
240" ld 9, 72(3)" "\n"
241" ld 10, 80(3)" "\n"
242" ld 11, 88(3)" "\n"
243" ld 12, 96(3)" "\n"
244" ld 13, 104(3)" "\n"
245" ld 14, 112(3)" "\n"
246" ld 15, 120(3)" "\n"
247" ld 16, 128(3)" "\n"
248" ld 17, 136(3)" "\n"
249" ld 18, 144(3)" "\n"
250" ld 19, 152(3)" "\n"
251" ld 20, 160(3)" "\n"
252" ld 21, 168(3)" "\n"
253" ld 22, 176(3)" "\n"
254" ld 23, 184(3)" "\n"
255" ld 24, 192(3)" "\n"
256" ld 25, 200(3)" "\n"
257" ld 26, 208(3)" "\n"
258" ld 27, 216(3)" "\n"
259" ld 28, 224(3)" "\n"
260" ld 29, 232(3)" "\n"
261" ld 30, 240(3)" "\n"
262" ld 31, 248(3)" "\n"
263" ld 3, 24(3)" "\n"
264" blr" "\n"
265"" "\n"
266
267".previous" "\n"
268".previous" "\n"
269);
270
sewardj2a88a012011-04-11 21:26:27 +0000271#endif /* VGP_ppc64_linux */
272
sewardjfc824cb2011-09-29 17:33:58 +0000273
274/* ------------ amd64-linux ------------ */
275
276#if defined(VGP_amd64_linux)
277
278__asm__(
279".text" "\n"
280"" "\n"
281".global VG_MINIMAL_SETJMP" "\n" // rdi = jmp_buf
282"VG_MINIMAL_SETJMP:" "\n"
283" movq %rax, 0(%rdi)" "\n"
284" movq %rbx, 8(%rdi)" "\n"
285" movq %rcx, 16(%rdi)" "\n"
286" movq %rdx, 24(%rdi)" "\n"
287" movq %rdi, 32(%rdi)" "\n"
288" movq %rsi, 40(%rdi)" "\n"
289" movq %rbp, 48(%rdi)" "\n"
290" movq %rsp, 56(%rdi)" "\n"
291" movq %r8, 64(%rdi)" "\n"
292" movq %r9, 72(%rdi)" "\n"
293" movq %r10, 80(%rdi)" "\n"
294" movq %r11, 88(%rdi)" "\n"
295" movq %r12, 96(%rdi)" "\n"
296" movq %r13, 104(%rdi)" "\n"
297" movq %r14, 112(%rdi)" "\n"
298" movq %r15, 120(%rdi)" "\n"
299 // store the return address
300" movq 0(%rsp), %rax" "\n"
301" movq %rax, 128(%rdi)" "\n"
302 // and return zero
303" movq $0, %rax" "\n"
304" ret" "\n"
305"" "\n"
306
307".globl VG_MINIMAL_LONGJMP" "\n"
308"VG_MINIMAL_LONGJMP:" "\n" // rdi = jmp_buf
309 // skip restoring rax; it's pointless
310" movq 8(%rdi), %rbx" "\n"
311" movq 16(%rdi), %rcx" "\n"
312" movq 24(%rdi), %rdx" "\n"
313 // defer restoring rdi; we still need it
314" movq 40(%rdi), %rsi" "\n"
315" movq 48(%rdi), %rbp" "\n"
316" movq 56(%rdi), %rsp" "\n"
317" movq 64(%rdi), %r8" "\n"
318" movq 72(%rdi), %r9" "\n"
319" movq 80(%rdi), %r10" "\n"
320" movq 88(%rdi), %r11" "\n"
321" movq 96(%rdi), %r12" "\n"
322" movq 104(%rdi), %r13" "\n"
323" movq 112(%rdi), %r14" "\n"
324" movq 120(%rdi), %r15" "\n"
325 // restore the return address
326" movq 128(%rdi), %rax" "\n"
327 // restore rdi; this is the last use
328" movq 32(%rdi), %rdi" "\n"
329 // make %rsp look like we really did a return
330" addq $8, %rsp" "\n"
331 // continue at RA of original call. Note: this is a
332 // nasty trick. We assume that %rax is nonzero, and so the
333 // caller can differentiate this case from the normal _SETJMP
334 // return case. If the return address ever is zero, then
335 // we're hosed; but that seems pretty unlikely given that it
336 // would mean we'd be executing at the wraparound point of the
337 // address space.
338" jmp *%rax" "\n"
339"" "\n"
340
341".previous" "\n"
342);
343
344#endif /* VGP_amd64_linux */
345
346
347/* ------------ x86-linux ------------ */
348
349#if defined(VGP_x86_linux)
350
351__asm__(
352".text" "\n"
353"" "\n"
354".global VG_MINIMAL_SETJMP" "\n" // eax = jmp_buf
355"VG_MINIMAL_SETJMP:" "\n"
356" movl %eax, 0(%eax)" "\n"
357" movl %ebx, 4(%eax)" "\n"
358" movl %ecx, 8(%eax)" "\n"
359" movl %edx, 12(%eax)" "\n"
360" movl %edi, 16(%eax)" "\n"
361" movl %esi, 20(%eax)" "\n"
362" movl %ebp, 24(%eax)" "\n"
363" movl %esp, 28(%eax)" "\n"
364 // store the return address
365" movl 0(%esp), %ebx" "\n"
366" movl %ebx, 32(%eax)" "\n"
367 // un-trash ebx (necessary? i don't know)
368" movl 4(%eax), %ebx" "\n"
369 // and return zero
370" movl $0, %eax" "\n"
371" ret" "\n"
372"" "\n"
373
374".globl VG_MINIMAL_LONGJMP" "\n"
375"VG_MINIMAL_LONGJMP:" "\n" // eax = jmp_buf
376 // skip restoring eax; it's pointless
377" movl 4(%eax), %ebx" "\n"
378" movl 8(%eax), %ecx" "\n"
379" movl 12(%eax), %edx" "\n"
380" movl 16(%eax), %edi" "\n"
381" movl 20(%eax), %esi" "\n"
382" movl 24(%eax), %ebp" "\n"
383" movl 28(%eax), %esp" "\n"
384 // restore the return address
385" movl 32(%eax), %eax" "\n"
386 // make %esp look like we really did a return
387" addl $4, %esp" "\n"
388 // continue at RA of original call. Same zero-vs-nonzero
389 // trick/assumption as documented for the amd64-linux case.
390" jmp *%eax" "\n"
391"" "\n"
392
393".previous" "\n"
394);
395
396#endif /* VGP_x86_linux */
397
sewardj6c591e12011-04-11 16:17:51 +0000398/*--------------------------------------------------------------------*/
399/*--- end ---*/
400/*--------------------------------------------------------------------*/