]> git.saurik.com Git - apple/libplatform.git/blob - src/setjmp/generic/sigtramp.c
9a1bf762c142c3aa403b134e5a7f822d892ee94d
[apple/libplatform.git] / src / setjmp / generic / 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
27 #define _XOPEN_SOURCE 600
28
29 #import <sys/types.h>
30 #import <signal.h>
31 #import <unistd.h>
32 #import <ucontext.h>
33 #import <mach/thread_status.h>
34 #include <TargetConditionals.h>
35
36 extern int __sigreturn(ucontext_t *, int);
37
38 /*
39 * sigvec registers _sigtramp as the handler for any signal requiring
40 * user-mode intervention. All _sigtramp does is find the real handler,
41 * calls it, then sigreturn's.
42 *
43 * Note that the kernel saves/restores all of our register state.
44 */
45
46 /* On i386, i386/sys/_sigtramp.s defines this. There is no in_sigtramp on arm */
47 #if defined(__DYNAMIC__) && defined(__x86_64__)
48 __attribute__((visibility("hidden")))
49 int __in_sigtramp = 0;
50 #endif
51
52 /* These defn should match the kernel one */
53 #define UC_TRAD 1
54 #define UC_FLAVOR 30
55 #if defined(__ppc__) || defined(__ppc64__)
56 #define UC_TRAD64 20
57 #define UC_TRAD64_VEC 25
58 #define UC_FLAVOR_VEC 35
59 #define UC_FLAVOR64 40
60 #define UC_FLAVOR64_VEC 45
61 #define UC_DUAL 50
62 #define UC_DUAL_VEC 55
63
64 /* The following are valid mcontext sizes */
65 #define UC_FLAVOR_SIZE ((PPC_THREAD_STATE_COUNT + PPC_EXCEPTION_STATE_COUNT + PPC_FLOAT_STATE_COUNT) * sizeof(int))
66
67 #define UC_FLAVOR_VEC_SIZE ((PPC_THREAD_STATE_COUNT + PPC_EXCEPTION_STATE_COUNT + PPC_FLOAT_STATE_COUNT + PPC_VECTOR_STATE_COUNT) * sizeof(int))
68
69 #define UC_FLAVOR64_SIZE ((PPC_THREAD_STATE64_COUNT + PPC_EXCEPTION_STATE64_COUNT + PPC_FLOAT_STATE_COUNT) * sizeof(int))
70
71 #define UC_FLAVOR64_VEC_SIZE ((PPC_THREAD_STATE64_COUNT + PPC_EXCEPTION_STATE64_COUNT + PPC_FLOAT_STATE_COUNT + PPC_VECTOR_STATE_COUNT) * sizeof(int))
72 #endif
73
74 #define UC_SET_ALT_STACK 0x40000000
75 #define UC_RESET_ALT_STACK 0x80000000
76
77 /*
78 * Reset the kernel's idea of the use of an alternate stack; this is used by
79 * both longjmp() and siglongjmp(). Nothing other than this reset is needed,
80 * since restoring the registers and other operations that would normally be
81 * done by sigreturn() are handled in user space, so we do not pass a user
82 * context (in PPC, a user context is not the same as a jmpbuf mcontext, due
83 * to having more than one set of registers, etc., for the various 32/64 etc.
84 * contexts)..
85 */
86 void
87 _sigunaltstack(int set)
88 {
89 /* sigreturn(uctx, ctxstyle); */
90 /* syscall (SYS_SIGRETURN, uctx, ctxstyle); */
91 __sigreturn (NULL, (set == SS_ONSTACK) ? UC_SET_ALT_STACK : UC_RESET_ALT_STACK);
92 }
93
94 /* On these architectures, _sigtramp is implemented in assembly to
95 ensure it matches its DWARF unwind information. */
96 #if !defined (__i386__) && !defined (__x86_64__)
97
98 void
99 _sigtramp(
100 union __sigaction_u __sigaction_u,
101 int sigstyle,
102 int sig,
103 siginfo_t *sinfo,
104 ucontext_t *uctx
105 ) {
106 int ctxstyle = UC_FLAVOR;
107
108 if (sigstyle == UC_TRAD)
109 sa_handler(sig);
110 else {
111 #if TARGET_OS_WATCH
112 // <rdar://problem/22016014>
113 sa_sigaction(sig, sinfo, NULL);
114 #else
115 sa_sigaction(sig, sinfo, uctx);
116 #endif
117 }
118
119 /* sigreturn(uctx, ctxstyle); */
120 /* syscall (SYS_SIGRETURN, uctx, ctxstyle); */
121 __sigreturn (uctx, ctxstyle);
122 }
123
124 #endif /* not ppc nor ppc64 nor i386 nor x86_64 */