]> git.saurik.com Git - apple/libc.git/blob - i386/sys/setjmp.s
Libc-391.2.3.tar.gz
[apple/libc.git] / i386 / sys / setjmp.s
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved
25 */
26 /*
27 * NeXT 386 setjmp/longjmp
28 *
29 * Written by Bruce Martin, NeXT Inc. 4/9/92
30 */
31
32 /*
33 * C library -- setjmp, longjmp
34 *
35 * longjmp(a,v)
36 * will generate a "return(v)" from
37 * the last call to
38 * setjmp(a)
39 * by restoring registers from the stack,
40 * The previous value of the signal mask is
41 * restored.
42 *
43 */
44
45 #include <architecture/i386/asm_help.h>
46 #include "SYS.h"
47
48 #define JB_ONSTACK 0
49 #define JB_MASK 4
50 #define JB_EAX 8
51 #define JB_EBX 12
52 #define JB_ECX 16
53 #define JB_EDX 20
54 #define JB_EDI 24
55 #define JB_ESI 28
56 #define JB_EBP 32
57 #define JB_ESP 36
58 #define JB_SS 40
59 #define JB_EFLAGS 44
60 #define JB_EIP 48
61 #define JB_CS 52
62 #define JB_DS 56
63 #define JB_ES 60
64 #define JB_FS 64
65 #define JB_GS 68
66 #define JB_SAVEMASK 72 // sigsetjmp/siglongjmp only
67
68 LEAF(_sigsetjmp, 0)
69 movl 4(%esp), %eax // sigjmp_buf * jmpbuf;
70 movl 8(%esp), %ecx // int savemask;
71 movl %ecx, JB_SAVEMASK(%eax) // jmpbuf[_JBLEN] = savemask;
72 cmpl $0, %ecx // if savemask != 0
73 jne _setjmp // setjmp(jmpbuf);
74 BRANCH_EXTERN(__setjmp) // else
75 // _setjmp(jmpbuf);
76
77 LEAF(_setjmp, 0)
78 movl 4(%esp), %ecx // jmp_buf (struct sigcontext *)
79 pushl %ecx // save ecx
80
81 // call sigstack to get the current signal stack
82 subl $12, %esp // space for return structure
83 pushl %esp
84 pushl $0
85 CALL_EXTERN(_sigaltstack)
86 movl 12(%esp), %eax // save stack pointer
87 movl %eax, JB_ONSTACK(%ecx)
88 addl $20, %esp
89
90 // call sigblock to get signal mask
91 pushl $0
92 CALL_EXTERN(_sigblock)
93 addl $4, %esp
94 popl %ecx // restore ecx
95 movl %eax, JB_MASK(%ecx)
96
97 // now build sigcontext
98 movl %ebx, JB_EBX(%ecx)
99 movl %edi, JB_EDI(%ecx)
100 movl %esi, JB_ESI(%ecx)
101 movl %ebp, JB_EBP(%ecx)
102
103 // EIP is set to the frame return address value
104 movl (%esp), %eax
105 movl %eax, JB_EIP(%ecx)
106 // ESP is set to the frame return address plus 4
107 movl %esp, %eax
108 addl $4, %eax
109 movl %eax, JB_ESP(%ecx)
110
111 // segment registers
112 movl $0, JB_SS(%ecx)
113 mov %ss, JB_SS(%ecx)
114 movl $0, JB_CS(%ecx)
115 mov %cs, JB_CS(%ecx)
116 movl $0, JB_DS(%ecx)
117 mov %ds, JB_DS(%ecx)
118 movl $0, JB_ES(%ecx)
119 mov %es, JB_ES(%ecx)
120 movl $0, JB_FS(%ecx)
121 mov %fs, JB_FS(%ecx)
122 movl $0, JB_GS(%ecx)
123 mov %gs, JB_GS(%ecx)
124
125 // save eflags - you can't use movl
126 pushf
127 popl %eax
128 movl %eax, JB_EFLAGS(%ecx)
129
130 // return 0
131 xorl %eax, %eax
132 ret
133
134 LEAF(_siglongjmp, 0)
135 movl 4(%esp), %eax // sigjmp_buf * jmpbuf;
136 cmpl $0, JB_SAVEMASK(%eax) // if jmpbuf[_JBLEN] != 0
137 jne _longjmp // longjmp(jmpbuf, var);
138 BRANCH_EXTERN(__longjmp) // else
139 // _longjmp(jmpbuf, var);
140
141 LEAF(_longjmp, 0)
142 subl $2,%esp
143 fnstcw (%esp) // save FP control word
144 fninit // reset FP coprocessor
145 fldcw (%esp) // restore FP control word
146 addl $2,%esp
147 movl 4(%esp), %eax // address of jmp_buf (saved context)
148 movl 8(%esp), %edx // return value
149 movl %edx, JB_EAX(%eax) // return value into saved context
150 movl $ SYS_sigreturn, %eax // sigreturn system call
151 UNIX_SYSCALL_TRAP
152 addl $8, %esp
153 CALL_EXTERN(_longjmperror)
154 CALL_EXTERN(_abort)
155 END(_longjmp)