setjmp.S revision 24958514b92c9b9e111223e4e4c56ef1a52b6403
14906e5653c57d49f94940f28556009a88c42a583Elliott Hughes/*
24906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * Copyright (c) 2001 Wasabi Systems, Inc.
34906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * All rights reserved.
44906e5653c57d49f94940f28556009a88c42a583Elliott Hughes *
54906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * Written by Frank van der Linden for Wasabi Systems, Inc.
64906e5653c57d49f94940f28556009a88c42a583Elliott Hughes *
74906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * Redistribution and use in source and binary forms, with or without
84906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * modification, are permitted provided that the following conditions
94906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * are met:
104906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * 1. Redistributions of source code must retain the above copyright
114906e5653c57d49f94940f28556009a88c42a583Elliott Hughes *    notice, this list of conditions and the following disclaimer.
124906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * 2. Redistributions in binary form must reproduce the above copyright
134906e5653c57d49f94940f28556009a88c42a583Elliott Hughes *    notice, this list of conditions and the following disclaimer in the
144906e5653c57d49f94940f28556009a88c42a583Elliott Hughes *    documentation and/or other materials provided with the distribution.
154906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * 3. All advertising materials mentioning features or use of this software
164906e5653c57d49f94940f28556009a88c42a583Elliott Hughes *    must display the following acknowledgement:
174906e5653c57d49f94940f28556009a88c42a583Elliott Hughes *      This product includes software developed for the NetBSD Project by
184906e5653c57d49f94940f28556009a88c42a583Elliott Hughes *      Wasabi Systems, Inc.
194906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * 4. The name of Wasabi Systems, Inc. may not be used to endorse
204906e5653c57d49f94940f28556009a88c42a583Elliott Hughes *    or promote products derived from this software without specific prior
214906e5653c57d49f94940f28556009a88c42a583Elliott Hughes *    written permission.
224906e5653c57d49f94940f28556009a88c42a583Elliott Hughes *
234906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
244906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
254906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
264906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
274906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
284906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
294906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
304906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
314906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
324906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
334906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * POSSIBILITY OF SUCH DAMAGE.
344906e5653c57d49f94940f28556009a88c42a583Elliott Hughes */
354906e5653c57d49f94940f28556009a88c42a583Elliott Hughes
36851e68a2402fa414544e66650e09dfdaac813e51Elliott Hughes#include <private/bionic_asm.h>
374906e5653c57d49f94940f28556009a88c42a583Elliott Hughes
388d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes// These are only the callee-saved registers. Code calling setjmp
398d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes// will expect the rest to be clobbered anyway.
408d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes
418d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes#define _JB_RBX 0
428d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes#define _JB_RBP 1
438d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes#define _JB_R12 2
448d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes#define _JB_R13 3
458d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes#define _JB_R14 4
468d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes#define _JB_R15 5
478d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes#define _JB_RSP 6
488d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes#define _JB_PC 7
498d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes#define _JB_SIGFLAG 8
508d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes#define _JB_SIGMASK 9
511c0c0ede573e3caf86b6fc395ba933bfb7235afaElliott Hughes#define _JB_SIGMASK_RT 10 // sigprocmask will write here too.
524906e5653c57d49f94940f28556009a88c42a583Elliott Hughes
534906e5653c57d49f94940f28556009a88c42a583Elliott HughesENTRY(setjmp)
548d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  movl $1,%esi
558d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  jmp PIC_PLT(sigsetjmp)
56507cfe2e10a6c4ad61b9638820ba10bfe881a18cChristopher FerrisEND(setjmp)
574906e5653c57d49f94940f28556009a88c42a583Elliott Hughes
588d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott HughesENTRY(_setjmp)
598d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  movl $0,%esi
608d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  jmp PIC_PLT(sigsetjmp)
618d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott HughesEND(_setjmp)
628d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes
638d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes// int sigsetjmp(sigjmp_buf env, int save_signal_mask);
648d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott HughesENTRY(sigsetjmp)
658d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  // Record whether or not we're saving the signal mask.
661c0c0ede573e3caf86b6fc395ba933bfb7235afaElliott Hughes  movl %esi,(_JB_SIGFLAG * 8)(%rdi)
678d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes
688d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  // Do we need to save the signal mask?
698d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  testl %esi,%esi
708d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  jz 2f
718d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes
721c0c0ede573e3caf86b6fc395ba933bfb7235afaElliott Hughes  // Save current signal mask.
731c0c0ede573e3caf86b6fc395ba933bfb7235afaElliott Hughes  pushq %rdi // Push 'env'.
741c0c0ede573e3caf86b6fc395ba933bfb7235afaElliott Hughes  // The 'how' argument is ignored if new_mask is NULL.
751c0c0ede573e3caf86b6fc395ba933bfb7235afaElliott Hughes  xorq %rsi,%rsi // NULL.
761c0c0ede573e3caf86b6fc395ba933bfb7235afaElliott Hughes  leaq (_JB_SIGMASK * 8)(%rdi),%rdx // old_mask.
771c0c0ede573e3caf86b6fc395ba933bfb7235afaElliott Hughes  call PIC_PLT(sigprocmask)
781c0c0ede573e3caf86b6fc395ba933bfb7235afaElliott Hughes  popq %rdi // Pop 'env'.
798d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes
808d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes2:
818d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  // Save the callee-save registers.
828d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  movq (%rsp),%r11
838d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  movq %rbx,(_JB_RBX * 8)(%rdi)
848d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  movq %rbp,(_JB_RBP * 8)(%rdi)
858d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  movq %r12,(_JB_R12 * 8)(%rdi)
868d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  movq %r13,(_JB_R13 * 8)(%rdi)
878d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  movq %r14,(_JB_R14 * 8)(%rdi)
888d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  movq %r15,(_JB_R15 * 8)(%rdi)
898d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  movq %rsp,(_JB_RSP * 8)(%rdi)
908d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  movq %r11,(_JB_PC  * 8)(%rdi)
918d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes
928d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  xorl %eax,%eax
938d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  ret
948d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott HughesEND(sigsetjmp)
958d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes
968d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes// void siglongjmp(sigjmp_buf env, int value);
978d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott HughesENTRY(siglongjmp)
988d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  movq %rdi,%r12
998d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  pushq %rsi // Push 'value'.
1008d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes
1018d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  // Do we need to restore the signal mask?
1021c0c0ede573e3caf86b6fc395ba933bfb7235afaElliott Hughes  cmpl $0,(_JB_SIGFLAG * 8)(%rdi)
1038d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  jz 2f
1048d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes
1058d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  // Restore the signal mask.
1061c0c0ede573e3caf86b6fc395ba933bfb7235afaElliott Hughes  movq $2,%rdi // SIG_SETMASK.
1071c0c0ede573e3caf86b6fc395ba933bfb7235afaElliott Hughes  leaq (_JB_SIGMASK * 8)(%r12),%rsi // new_mask.
1081c0c0ede573e3caf86b6fc395ba933bfb7235afaElliott Hughes  xorq %rdx,%rdx // NULL.
1091c0c0ede573e3caf86b6fc395ba933bfb7235afaElliott Hughes  call PIC_PLT(sigprocmask)
1108d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes
1118d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes2:
1128d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  popq %rax // Pop 'value'.
1138d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes
1148d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  // Restore the callee-save registers.
1158d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  movq (_JB_RBX * 8)(%r12),%rbx
1168d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  movq (_JB_RBP * 8)(%r12),%rbp
1178d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  movq (_JB_R13 * 8)(%r12),%r13
1188d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  movq (_JB_R14 * 8)(%r12),%r14
1198d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  movq (_JB_R15 * 8)(%r12),%r15
1208d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  movq (_JB_RSP * 8)(%r12),%rsp
1218d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  movq (_JB_PC  * 8)(%r12),%r11
1228d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  movq (_JB_R12 * 8)(%r12),%r12
1238d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes
1248d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  testl %eax,%eax
1258d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  jnz 1f
1268d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  incl %eax
1278d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes1:
1288d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  movq %r11,0(%rsp)
1298d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes  ret
1308d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott HughesEND(siglongjmp)
1318d4c55cc741c4107f8a0fba16e5c178c9feb5d81Elliott Hughes
13224958514b92c9b9e111223e4e4c56ef1a52b6403Christopher FerrisALIAS_SYMBOL(longjmp, siglongjmp)
13324958514b92c9b9e111223e4e4c56ef1a52b6403Christopher FerrisALIAS_SYMBOL(_longjmp, siglongjmp)
134