]> git.saurik.com Git - apple/libplatform.git/blob - src/ucontext/generic/makecontext.c
libplatform-220.tar.gz
[apple/libplatform.git] / src / ucontext / generic / makecontext.c
1 /*
2 * Copyright (c) 2007, 2009 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 /*
25 * Copyright (c) 2001 Daniel M. Eischen <deischen@freebsd.org>
26 * All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Neither the name of the author nor the names of its contributors
34 * may be used to endorse or promote products derived from this software
35 * without specific prior written permission.
36 *
37 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
38 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * SUCH DAMAGE.
48 */
49
50 #define _XOPEN_SOURCE 600L
51 #include <ucontext.h>
52 #include <errno.h>
53
54 #if defined(__x86_64__) || defined(__i386__)
55 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
56
57 #include <sys/param.h>
58 #include <stddef.h>
59 #include <stdarg.h>
60 #include <unistd.h>
61
62 /* Prototypes */
63 extern void _ctx_start(ucontext_t *, int argc, ...);
64
65 __attribute__((visibility("hidden")))
66 void
67 _ctx_done (ucontext_t *ucp)
68 {
69 if (ucp->uc_link == NULL)
70 _exit(0);
71 else {
72 /*
73 * Since this context has finished, don't allow it
74 * to be restarted without being reinitialized (via
75 * setcontext or swapcontext).
76 */
77 ucp->uc_mcsize = 0;
78
79 /* Set context to next one in link */
80 /* XXX - what to do for error, abort? */
81 setcontext((const ucontext_t *)ucp->uc_link);
82 __builtin_trap(); /* should never get here */
83 }
84 }
85
86 void
87 makecontext(ucontext_t *ucp, void (*start)(), int argc, ...)
88 {
89 va_list ap;
90 char *stack_top;
91 intptr_t *argp;
92 int i;
93
94 if (ucp == NULL)
95 return;
96 else if ((ucp->uc_stack.ss_sp == NULL) ||
97 (ucp->uc_stack.ss_size < MINSIGSTKSZ)) {
98 /*
99 * This should really return -1 with errno set to ENOMEM
100 * or something, but the spec says that makecontext is
101 * a void function. At least make sure that the context
102 * isn't valid so it can't be used without an error.
103 */
104 ucp->uc_mcsize = 0;
105 }
106 /* XXX - Do we want to sanity check argc? */
107 else if ((argc < 0) || (argc > NCARGS)) {
108 ucp->uc_mcsize = 0;
109 }
110 /* Make sure the context is valid. */
111 else {
112 /*
113 * Arrange the stack as follows:
114 *
115 * _ctx_start() - context start wrapper
116 * start() - user start routine
117 * arg1 - first argument, aligned(16)
118 * ...
119 * argn
120 * ucp - this context, %rbp/%ebp points here
121 *
122 * When the context is started, control will return to
123 * the context start wrapper which will pop the user
124 * start routine from the top of the stack. After that,
125 * the top of the stack will be setup with all arguments
126 * necessary for calling the start routine. When the
127 * start routine returns, the context wrapper then sets
128 * the stack pointer to %rbp/%ebp which was setup to point to
129 * the base of the stack (and where ucp is stored). It
130 * will then call _ctx_done() to swap in the next context
131 * (uc_link != 0) or exit the program (uc_link == 0).
132 */
133 mcontext_t mc;
134
135 stack_top = (char *)(ucp->uc_stack.ss_sp +
136 ucp->uc_stack.ss_size - sizeof(intptr_t));
137
138 int minargc = argc;
139 #if defined(__x86_64__)
140 /* Give 6 stack slots to _ctx_start */
141 if (minargc < 6)
142 minargc = 6;
143 #endif
144 /*
145 * Adjust top of stack to allow for 3 pointers (return
146 * address, _ctx_start, and ucp) and argc arguments.
147 * We allow the arguments to be pointers also. The first
148 * argument to the user function must be properly aligned.
149 */
150
151 stack_top = stack_top - (sizeof(intptr_t) * (1 + minargc));
152 stack_top = (char *)((intptr_t)stack_top & ~15);
153 stack_top = stack_top - (2 * sizeof(intptr_t));
154 argp = (intptr_t *)stack_top;
155
156 /*
157 * Setup the top of the stack with the user start routine
158 * followed by all of its aguments and the pointer to the
159 * ucontext. We need to leave a spare spot at the top of
160 * the stack because setcontext will move rip/eip to the top
161 * of the stack before returning.
162 */
163 *argp = (intptr_t)_ctx_start; /* overwritten with same value */
164 argp++;
165 *argp = (intptr_t)start;
166 argp++;
167
168 /* Add all the arguments: */
169 va_start(ap, argc);
170 for (i = 0; i < argc; i++) {
171 *argp = va_arg(ap, intptr_t);
172 argp++;
173 }
174 va_end(ap);
175
176 #if defined(__x86_64__)
177 /* Always provide space for ctx_start to pop the parameter registers */
178 for (;argc < minargc; argc++) {
179 *argp++ = 0;
180 }
181
182 /* Keep stack aligned */
183 if (argc & 1) {
184 *argp++ = 0;
185 }
186 #endif
187
188 /* The ucontext is placed at the bottom of the stack. */
189 *argp = (intptr_t)ucp;
190
191 /*
192 * Set the machine context to point to the top of the
193 * stack and the program counter to the context start
194 * wrapper. Note that setcontext() pushes the return
195 * address onto the top of the stack, so allow for this
196 * by adjusting the stack downward 1 slot. Also set
197 * %r12/%esi to point to the base of the stack where ucp
198 * is stored.
199 */
200 mc = ucp->uc_mcontext;
201 #if defined(__x86_64__)
202 /* Use callee-save and match _ctx_start implementation */
203 mc->__ss.__r12 = (intptr_t)argp;
204 mc->__ss.__rbp = 0;
205 mc->__ss.__rsp = (intptr_t)stack_top + sizeof(caddr_t);
206 mc->__ss.__rip = (intptr_t)_ctx_start;
207 #else
208 mc->__ss.__esi = (int)argp;
209 mc->__ss.__ebp = 0;
210 mc->__ss.__esp = (int)stack_top + sizeof(caddr_t);
211 mc->__ss.__eip = (int)_ctx_start;
212 #endif
213 }
214 }
215
216 #else
217
218 void
219 makecontext(ucontext_t *u, void (*f)(void), int a, ...)
220 {
221 }
222
223 #endif