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