]> git.saurik.com Git - apple/libc.git/blob - sys/sigtramp.c
Libc-498.1.7.tar.gz
[apple/libc.git] / sys / sigtramp.c
1 /*
2 * Copyright (c) 1999, 2007 Apple 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 #import "sigcatch.h"
27 #import <sys/types.h>
28 #import <signal.h>
29 #import <strings.h>
30 #import <unistd.h>
31 #import <ucontext.h>
32 #import <mach/thread_status.h>
33
34 extern int __sigreturn(ucontext_t *, int);
35
36 /*
37 * sigvec registers _sigtramp as the handler for any signal requiring
38 * user-mode intervention. All _sigtramp does is find the real handler,
39 * calls it, then sigreturn's.
40 *
41 * Note that the kernel saves/restores all of our register state.
42 */
43
44 /* On i386, i386/sys/_sigtramp.s defines this. */
45 #if defined(__DYNAMIC__) && ! defined(__i386__)
46 int __in_sigtramp = 0;
47 #endif
48
49 /* These defn should match the kernel one */
50 #define UC_TRAD 1
51 #define UC_FLAVOR 30
52 #if defined(__ppc__) || defined(__ppc64__)
53 #define UC_TRAD64 20
54 #define UC_TRAD64_VEC 25
55 #define UC_FLAVOR_VEC 35
56 #define UC_FLAVOR64 40
57 #define UC_FLAVOR64_VEC 45
58 #define UC_DUAL 50
59 #define UC_DUAL_VEC 55
60
61 /* The following are valid mcontext sizes */
62 #define UC_FLAVOR_SIZE ((PPC_THREAD_STATE_COUNT + PPC_EXCEPTION_STATE_COUNT + PPC_FLOAT_STATE_COUNT) * sizeof(int))
63
64 #define UC_FLAVOR_VEC_SIZE ((PPC_THREAD_STATE_COUNT + PPC_EXCEPTION_STATE_COUNT + PPC_FLOAT_STATE_COUNT + PPC_VECTOR_STATE_COUNT) * sizeof(int))
65
66 #define UC_FLAVOR64_SIZE ((PPC_THREAD_STATE64_COUNT + PPC_EXCEPTION_STATE64_COUNT + PPC_FLOAT_STATE_COUNT) * sizeof(int))
67
68 #define UC_FLAVOR64_VEC_SIZE ((PPC_THREAD_STATE64_COUNT + PPC_EXCEPTION_STATE64_COUNT + PPC_FLOAT_STATE_COUNT + PPC_VECTOR_STATE_COUNT) * sizeof(int))
69 #endif
70
71 #define UC_SET_ALT_STACK 0x40000000
72 #define UC_RESET_ALT_STACK 0x80000000
73
74 #if defined(__ppc__)
75 /* This routine will be replaced by an assembly soon */
76 static int
77 restore64_state(mcontext_t mctx, mcontext64_t mctx64, int sigstyle)
78 {
79 if (mctx->ss.srr0 != (unsigned int)mctx64->ss.srr0)
80 return(0);
81 if (mctx->ss.srr1 != (unsigned int)mctx64->ss.srr1)
82 return(0);
83 if (mctx->ss.r0 != (unsigned int)mctx64->ss.r0)
84 return(0);
85 if (mctx->ss.r1 != (unsigned int)mctx64->ss.r1)
86 return(0);
87 if (mctx->ss.r2 != (unsigned int)mctx64->ss.r2)
88 return(0);
89 if (mctx->ss.r3 != (unsigned int)mctx64->ss.r3)
90 return(0);
91 if (mctx->ss.r4 != (unsigned int)mctx64->ss.r4)
92 return(0);
93 if (mctx->ss.r5 != (unsigned int)mctx64->ss.r5)
94 return(0);
95 if (mctx->ss.r6 != (unsigned int)mctx64->ss.r6)
96 return(0);
97 if (mctx->ss.r7 != (unsigned int)mctx64->ss.r7)
98 return(0);
99 if (mctx->ss.r8 != (unsigned int)mctx64->ss.r8)
100 return(0);
101 if (mctx->ss.r9 != (unsigned int)mctx64->ss.r9)
102 return(0);
103 if (mctx->ss.r10 != (unsigned int)mctx64->ss.r10)
104 return(0);
105 if (mctx->ss.r11 != (unsigned int)mctx64->ss.r11)
106 return(0);
107 if (mctx->ss.r12 != (unsigned int)mctx64->ss.r12)
108 return(0);
109 if (mctx->ss.r13 != (unsigned int)mctx64->ss.r13)
110 return(0);
111 if (mctx->ss.r14 != (unsigned int)mctx64->ss.r14)
112 return(0);
113 if (mctx->ss.r15 != (unsigned int)mctx64->ss.r15)
114 return(0);
115 if (mctx->ss.r16 != (unsigned int)mctx64->ss.r16)
116 return(0);
117 if (mctx->ss.r17 != (unsigned int)mctx64->ss.r17)
118 return(0);
119 if (mctx->ss.r18 != (unsigned int)mctx64->ss.r18)
120 return(0);
121 if (mctx->ss.r19 != (unsigned int)mctx64->ss.r19)
122 return(0);
123 if (mctx->ss.r20 != (unsigned int)mctx64->ss.r20)
124 return(0);
125 if (mctx->ss.r21 != (unsigned int)mctx64->ss.r21)
126 return(0);
127 if (mctx->ss.r22 != (unsigned int)mctx64->ss.r22)
128 return(0);
129 if (mctx->ss.r23 != (unsigned int)mctx64->ss.r23)
130 return(0);
131 if (mctx->ss.r24 != (unsigned int)mctx64->ss.r24)
132 return(0);
133 if (mctx->ss.r25 != (unsigned int)mctx64->ss.r25)
134 return(0);
135 if (mctx->ss.r26 != (unsigned int)mctx64->ss.r26)
136 return(0);
137 if (mctx->ss.r27 != (unsigned int)mctx64->ss.r27)
138 return(0);
139 if (mctx->ss.r28 != (unsigned int)mctx64->ss.r28)
140 return(0);
141 if (mctx->ss.r29 != (unsigned int)mctx64->ss.r29)
142 return(0);
143 if (mctx->ss.r30 != (unsigned int)mctx64->ss.r30)
144 return(0);
145 if (mctx->ss.r31 != (unsigned int)mctx64->ss.r31)
146 return(0);
147
148 if (mctx->ss.cr != mctx64->ss.cr)
149 return(0);
150 if (mctx->ss.xer != (unsigned int)mctx64->ss.xer)
151 return(0);
152 if (mctx->ss.lr != (unsigned int)mctx64->ss.lr)
153 return(0);
154 if (mctx->ss.ctr != (unsigned int)mctx64->ss.ctr)
155 return(0);
156
157 if (bcmp(&mctx->fs, &mctx64->fs, (PPC_FLOAT_STATE_COUNT * sizeof(int))))
158 return(0);
159 if ((sigstyle == UC_DUAL_VEC) && bcmp(&mctx->vs, &mctx64->vs, (PPC_VECTOR_STATE_COUNT * sizeof(int))))
160 return(0);
161
162 return(1);
163
164 }
165
166 /* This routine is called from ppc/sys/_sigtramp.s on return from
167 sa_handler. */
168
169 void
170 __finish_sigtramp(
171 ucontext_t *uctx,
172 int sigstyle
173 ) {
174 int ctxstyle = UC_FLAVOR;
175 mcontext_t mctx;
176 mcontext64_t mctx64;
177
178 if ((sigstyle == UC_DUAL) || (sigstyle == UC_DUAL_VEC)) {
179 mctx = uctx->uc_mcontext;
180 mctx64 = (mcontext64_t)((char *)(uctx->uc_mcontext) + sizeof(struct mcontext));
181 /* restore 64bit state ? */
182 if (restore64_state(mctx, mctx64, sigstyle)) {
183 uctx->uc_mcontext = (void *)mctx64;
184 if (sigstyle == UC_DUAL) {
185 uctx->uc_mcsize = UC_FLAVOR64_SIZE;
186 ctxstyle = UC_FLAVOR64;
187 } else {
188 uctx->uc_mcsize = UC_FLAVOR64_VEC_SIZE;
189 ctxstyle = UC_FLAVOR64_VEC;
190 }
191 } else {
192 if (sigstyle == UC_DUAL)
193 ctxstyle = UC_FLAVOR;
194 else
195 ctxstyle = UC_FLAVOR_VEC;
196 }
197 } else
198 ctxstyle = sigstyle;
199
200 #if defined(__DYNAMIC__)
201 __in_sigtramp--;
202 #endif
203
204 __sigreturn (uctx, ctxstyle);
205 }
206
207 #endif /* ppc */
208
209 /*
210 * Reset the kernel's idea of the use of an alternate stack; this is used by
211 * both longjmp() and siglongjmp(). Nothing other than this reset is needed,
212 * since restoring the registers and other operations that would normally be
213 * done by sigreturn() are handled in user space, so we do not pass a user
214 * context (in PPC, a user context is not the same as a jmpbuf mcontext, due
215 * to having more than one set of registers, etc., for the various 32/64 etc.
216 * contexts)..
217 */
218 void
219 _sigunaltstack(int set)
220 {
221 /* sigreturn(uctx, ctxstyle); */
222 /* syscall (SYS_SIGRETURN, uctx, ctxstyle); */
223 __sigreturn (NULL, (set == SS_ONSTACK) ? UC_SET_ALT_STACK : UC_RESET_ALT_STACK);
224 }
225
226 /* On these architectures, _sigtramp is implemented in assembly to
227 ensure it matches its DWARF unwind information. */
228 #if ! defined (__ppc__) && ! defined (__ppc64__) && ! defined (__i386__) \
229 && ! defined (__x86_64__)
230
231 void
232 _sigtramp(
233 union __sigaction_u __sigaction_u,
234 int sigstyle,
235 int sig,
236 siginfo_t *sinfo,
237 ucontext_t *uctx
238 ) {
239 int ctxstyle = UC_FLAVOR;
240
241 #if defined(__DYNAMIC__)
242 __in_sigtramp++;
243 #endif
244
245 if (sigstyle == UC_TRAD)
246 sa_handler(sig);
247 else {
248 sa_sigaction(sig, sinfo, uctx);
249 }
250
251 #if defined(__DYNAMIC__)
252 __in_sigtramp--;
253 #endif
254 /* sigreturn(uctx, ctxstyle); */
255 /* syscall (SYS_SIGRETURN, uctx, ctxstyle); */
256 __sigreturn (uctx, ctxstyle);
257 }
258
259 #endif /* not ppc nor ppc64 nor i386 nor x86_64 */