1/*
2 * Copyright (c) 2001 Wasabi Systems, Inc.
3 * All rights reserved.
4 *
5 * Written by Frank van der Linden for Wasabi Systems, Inc.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *      This product includes software developed for the NetBSD Project by
18 *      Wasabi Systems, Inc.
19 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
20 *    or promote products derived from this software without specific prior
21 *    written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36#include <private/bionic_asm.h>
37
38// These are only the callee-saved registers. Code calling setjmp
39// will expect the rest to be clobbered anyway.
40
41#define _JB_RBX 0
42#define _JB_RBP 1
43#define _JB_R12 2
44#define _JB_R13 3
45#define _JB_R14 4
46#define _JB_R15 5
47#define _JB_RSP 6
48#define _JB_PC 7
49#define _JB_SIGFLAG 8
50#define _JB_SIGMASK 9
51#define _JB_SIGMASK_RT 10 // sigprocmask will write here too.
52
53ENTRY(setjmp)
54  movl $1,%esi
55  jmp PIC_PLT(sigsetjmp)
56END(setjmp)
57
58ENTRY(_setjmp)
59  movl $0,%esi
60  jmp PIC_PLT(sigsetjmp)
61END(_setjmp)
62
63// int sigsetjmp(sigjmp_buf env, int save_signal_mask);
64ENTRY(sigsetjmp)
65  // Record whether or not we're saving the signal mask.
66  movl %esi,(_JB_SIGFLAG * 8)(%rdi)
67
68  // Do we need to save the signal mask?
69  testl %esi,%esi
70  jz 2f
71
72  // Save current signal mask.
73  pushq %rdi // Push 'env'.
74  // The 'how' argument is ignored if new_mask is NULL.
75  xorq %rsi,%rsi // NULL.
76  leaq (_JB_SIGMASK * 8)(%rdi),%rdx // old_mask.
77  call PIC_PLT(sigprocmask)
78  popq %rdi // Pop 'env'.
79
802:
81  // Save the callee-save registers.
82  movq (%rsp),%r11
83  movq %rbx,(_JB_RBX * 8)(%rdi)
84  movq %rbp,(_JB_RBP * 8)(%rdi)
85  movq %r12,(_JB_R12 * 8)(%rdi)
86  movq %r13,(_JB_R13 * 8)(%rdi)
87  movq %r14,(_JB_R14 * 8)(%rdi)
88  movq %r15,(_JB_R15 * 8)(%rdi)
89  movq %rsp,(_JB_RSP * 8)(%rdi)
90  movq %r11,(_JB_PC  * 8)(%rdi)
91
92  xorl %eax,%eax
93  ret
94END(sigsetjmp)
95
96// void siglongjmp(sigjmp_buf env, int value);
97ENTRY(siglongjmp)
98  movq %rdi,%r12
99  pushq %rsi // Push 'value'.
100
101  // Do we need to restore the signal mask?
102  cmpl $0,(_JB_SIGFLAG * 8)(%rdi)
103  jz 2f
104
105  // Restore the signal mask.
106  movq $2,%rdi // SIG_SETMASK.
107  leaq (_JB_SIGMASK * 8)(%r12),%rsi // new_mask.
108  xorq %rdx,%rdx // NULL.
109  call PIC_PLT(sigprocmask)
110
1112:
112  popq %rax // Pop 'value'.
113
114  // Restore the callee-save registers.
115  movq (_JB_RBX * 8)(%r12),%rbx
116  movq (_JB_RBP * 8)(%r12),%rbp
117  movq (_JB_R13 * 8)(%r12),%r13
118  movq (_JB_R14 * 8)(%r12),%r14
119  movq (_JB_R15 * 8)(%r12),%r15
120  movq (_JB_RSP * 8)(%r12),%rsp
121  movq (_JB_PC  * 8)(%r12),%r11
122  movq (_JB_R12 * 8)(%r12),%r12
123
124  testl %eax,%eax
125  jnz 1f
126  incl %eax
1271:
128  movq %r11,0(%rsp)
129  ret
130END(siglongjmp)
131
132ALIAS_SYMBOL(longjmp, siglongjmp)
133ALIAS_SYMBOL(_longjmp, siglongjmp)
134