]> git.saurik.com Git - apple/libc.git/blob - sys/sigtramp.c
abb488f7316980e117185b7c5e7888e8e1fa102a
[apple/libc.git] / sys / sigtramp.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
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
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved
27 */
28 #import "sigcatch.h"
29 #import <sys/types.h>
30 #import <signal.h>
31 #import <ucontext.h>
32
33 /*
34 * sigvec registers _sigtramp as the handler for any signal requiring
35 * user-mode intervention. All _sigtramp does is find the real handler,
36 * calls it, then sigreturn's.
37 *
38 * Note that the kernel saves/restores all of our register state.
39 */
40 #if defined(__DYNAMIC__)
41 int __in_sigtramp = 0;
42 #endif
43
44 /* These defn should match the kernel one */
45 #define UC_TRAD 1
46 #if defined(__ppc__) || defined(__ppc64__)
47 #define UC_TRAD64 20
48 #define UC_TRAD64_VEC 25
49 #define UC_FLAVOR 30
50 #define UC_FLAVOR_VEC 35
51 #define UC_FLAVOR64 40
52 #define UC_FLAVOR64_VEC 45
53 #define UC_DUAL 50
54 #define UC_DUAL_VEC 55
55
56 /* The following are valid mcontext sizes */
57 #define UC_FLAVOR_SIZE ((PPC_THREAD_STATE_COUNT + PPC_EXCEPTION_STATE_COUNT + PPC_FLOAT_STATE_COUNT) * sizeof(int))
58
59 #define UC_FLAVOR_VEC_SIZE ((PPC_THREAD_STATE_COUNT + PPC_EXCEPTION_STATE_COUNT + PPC_FLOAT_STATE_COUNT + PPC_VECTOR_STATE_COUNT) * sizeof(int))
60
61 #define UC_FLAVOR64_SIZE ((PPC_THREAD_STATE64_COUNT + PPC_EXCEPTION_STATE64_COUNT + PPC_FLOAT_STATE_COUNT) * sizeof(int))
62
63 #define UC_FLAVOR64_VEC_SIZE ((PPC_THREAD_STATE64_COUNT + PPC_EXCEPTION_STATE64_COUNT + PPC_FLOAT_STATE_COUNT + PPC_VECTOR_STATE_COUNT) * sizeof(int))
64 #endif
65
66 #if defined(__ppc__) || defined(__ppc64__)
67 /* This routine will be replaced by an assembly soon */
68 static int
69 restore64_state(mcontext_t mctx, mcontext64_t mctx64)
70 {
71 if (mctx->ss.srr0 != (unsigned int)mctx64->ss.srr0)
72 return(0);
73 if (mctx->ss.srr1 != (unsigned int)mctx64->ss.srr1)
74 return(0);
75 if (mctx->ss.r0 != (unsigned int)mctx64->ss.r0)
76 return(0);
77 if (mctx->ss.r1 != (unsigned int)mctx->ss.r1)
78 return(0);
79 if (mctx->ss.r2 != (unsigned int)mctx->ss.r2)
80 return(0);
81 if (mctx->ss.r3 != (unsigned int)mctx->ss.r3)
82 return(0);
83 if (mctx->ss.r4 != (unsigned int)mctx->ss.r4)
84 return(0);
85 if (mctx->ss.r5 != (unsigned int)mctx->ss.r5)
86 return(0);
87 if (mctx->ss.r6 != (unsigned int)mctx->ss.r6)
88 return(0);
89 if (mctx->ss.r7 != (unsigned int)mctx->ss.r7)
90 return(0);
91 if (mctx->ss.r8 != (unsigned int)mctx->ss.r8)
92 return(0);
93 if (mctx->ss.r9 != (unsigned int)mctx->ss.r9)
94 return(0);
95 if (mctx->ss.r10 != (unsigned int)mctx->ss.r10)
96 return(0);
97 if (mctx->ss.r11 != (unsigned int)mctx->ss.r11)
98 return(0);
99 if (mctx->ss.r12 != (unsigned int)mctx->ss.r12)
100 return(0);
101 if (mctx->ss.r13 != (unsigned int)mctx->ss.r13)
102 return(0);
103 if (mctx->ss.r14 != (unsigned int)mctx->ss.r14)
104 return(0);
105 if (mctx->ss.r15 != (unsigned int)mctx->ss.r15)
106 return(0);
107 if (mctx->ss.r16 != (unsigned int)mctx->ss.r16)
108 return(0);
109 if (mctx->ss.r17 != (unsigned int)mctx->ss.r17)
110 return(0);
111 if (mctx->ss.r18 != (unsigned int)mctx->ss.r18)
112 return(0);
113 if (mctx->ss.r19 != (unsigned int)mctx->ss.r19)
114 return(0);
115 if (mctx->ss.r20 != (unsigned int)mctx->ss.r20)
116 return(0);
117 if (mctx->ss.r21 != (unsigned int)mctx->ss.r21)
118 return(0);
119 if (mctx->ss.r22 != (unsigned int)mctx64->ss.r22)
120 return(0);
121 if (mctx->ss.r23 != (unsigned int)mctx64->ss.r23)
122 return(0);
123 if (mctx->ss.r24 != (unsigned int)mctx64->ss.r24)
124 return(0);
125 if (mctx->ss.r25 != (unsigned int)mctx64->ss.r25)
126 return(0);
127 if (mctx->ss.r26 != (unsigned int)mctx64->ss.r26)
128 return(0);
129 if (mctx->ss.r27 != (unsigned int)mctx64->ss.r27)
130 return(0);
131 if (mctx->ss.r28 != (unsigned int)mctx64->ss.r28)
132 return(0);
133 if (mctx->ss.r29 != (unsigned int)mctx64->ss.r29)
134 return(0);
135 if (mctx->ss.r30 != (unsigned int)mctx64->ss.r30)
136 return(0);
137 if (mctx->ss.r31 != (unsigned int)mctx64->ss.r31)
138 return(0);
139
140 if (mctx->ss.cr != mctx64->ss.cr)
141 return(0);
142 if (mctx->ss.xer != (unsigned int)mctx64->ss.xer)
143 return(0);
144 if (mctx->ss.lr != (unsigned int)mctx64->ss.lr)
145 return(0);
146 if (mctx->ss.ctr != (unsigned int)mctx64->ss.ctr)
147 return(0);
148
149 return(1);
150
151 }
152
153 #endif
154
155 void
156 _sigtramp(
157 union __sigaction_u __sigaction_u,
158 int sigstyle,
159 int sig,
160 siginfo_t *sinfo,
161 struct ucontext *uctx
162 ) {
163 #if defined(__ppc__) || defined(__ppc64__)
164 int ctxstyle = UC_FLAVOR;
165 #endif
166 mcontext_t mctx;
167 mcontext64_t mctx64;
168
169 #if defined(__DYNAMIC__)
170 __in_sigtramp++;
171 #endif
172 #ifdef __i386__
173 if (sigstyle == UC_TRAD)
174 sa_handler(sig);
175 #elif defined(__ppc__) || defined(__ppc64__)
176 if ((sigstyle == UC_TRAD) || (sigstyle == UC_TRAD64) || (sigstyle == UC_TRAD64_VEC))
177 sa_handler(sig);
178
179 else
180 sa_sigaction(sig, sinfo, uctx);
181
182 if ((sigstyle == UC_DUAL) || (sigstyle == UC_DUAL_VEC)) {
183 mctx = uctx->uc_mcontext;
184 mctx64 = (mcontext64_t)((char *)(uctx->uc_mcontext) + sizeof(struct mcontext));
185 /* restore 64bit state ? */
186 if (restore64_state(mctx, mctx64)) {
187 uctx->uc_mcontext = (void *)mctx64;
188 if (sigstyle == UC_DUAL) {
189 uctx->uc_mcsize = UC_FLAVOR64_SIZE;
190 ctxstyle = UC_FLAVOR64;
191 } else {
192 uctx->uc_mcsize = UC_FLAVOR64_VEC_SIZE;
193 ctxstyle = UC_FLAVOR64_VEC;
194 }
195 } else {
196 if (sigstyle == UC_DUAL)
197 ctxstyle = UC_FLAVOR;
198 else
199 ctxstyle = UC_FLAVOR_VEC;
200 }
201 } else
202 ctxstyle = sigstyle;
203 #endif /* __ppc__ || __ppc64__ */
204
205 #if defined(__DYNAMIC__)
206 __in_sigtramp--;
207 #endif
208 #if defined(__ppc__) || defined(__ppc64__)
209 {
210 /* sigreturn(uctx, ctxstyle); */
211 /* syscall (SYS_SIGRETURN, uctx, ctxstyle); */
212 syscall (184, uctx, ctxstyle);
213 }
214 #else
215 sigreturn(uctx);
216 #endif /* __ppc__ || __ppc64__ */
217 }
218