]>
git.saurik.com Git - apple/libc.git/blob - i386/gen/makecontext.c
2 * Copyright (c) 2007, 2009 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 2001 Daniel M. Eischen <deischen@freebsd.org>
26 * All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
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.
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
52 #include <sys/cdefs.h>
54 #include <sys/param.h>
55 #include <sys/signal.h>
56 #include <sys/ucontext.h>
65 extern void _ctx_start(ucontext_t
*, int argc
, ...);
68 _ctx_done (ucontext_t
*ucp
)
70 if (ucp
->uc_link
== NULL
)
74 * Since this context has finished, don't allow it
75 * to be restarted without being reinitialized (via
76 * setcontext or swapcontext).
80 /* Set context to next one in link */
81 /* XXX - what to do for error, abort? */
82 setcontext((const ucontext_t
*)ucp
->uc_link
);
83 LIBC_ABORT("setcontext failed"); /* should never get here */
88 makecontext(ucontext_t
*ucp
, void (*start
)(), int argc
, ...)
97 else if ((ucp
->uc_stack
.ss_sp
== NULL
) ||
98 (ucp
->uc_stack
.ss_size
< MINSIGSTKSZ
)) {
100 * This should really return -1 with errno set to ENOMEM
101 * or something, but the spec says that makecontext is
102 * a void function. At least make sure that the context
103 * isn't valid so it can't be used without an error.
107 /* XXX - Do we want to sanity check argc? */
108 else if ((argc
< 0) || (argc
> NCARGS
)) {
111 /* Make sure the context is valid. */
114 * Arrange the stack as follows:
116 * _ctx_start() - context start wrapper
117 * start() - user start routine
118 * arg1 - first argument, aligned(16)
121 * ucp - this context, %ebp points here
123 * When the context is started, control will return to
124 * the context start wrapper which will pop the user
125 * start routine from the top of the stack. After that,
126 * the top of the stack will be setup with all arguments
127 * necessary for calling the start routine. When the
128 * start routine returns, the context wrapper then sets
129 * the stack pointer to %ebp which was setup to point to
130 * the base of the stack (and where ucp is stored). It
131 * will then call _ctx_done() to swap in the next context
132 * (uc_link != 0) or exit the program (uc_link == 0).
136 stack_top
= (char *)(ucp
->uc_stack
.ss_sp
+
137 ucp
->uc_stack
.ss_size
- sizeof(intptr_t));
140 * Adjust top of stack to allow for 3 pointers (return
141 * address, _ctx_start, and ucp) and argc arguments.
142 * We allow the arguments to be pointers also. The first
143 * argument to the user function must be properly aligned.
145 stack_top
= stack_top
- (sizeof(intptr_t) * (1 + argc
));
146 stack_top
= (char *)((unsigned)stack_top
& ~15);
147 stack_top
= stack_top
- (2 * sizeof(intptr_t));
148 argp
= (intptr_t *)stack_top
;
151 * Setup the top of the stack with the user start routine
152 * followed by all of its aguments and the pointer to the
153 * ucontext. We need to leave a spare spot at the top of
154 * the stack because setcontext will move eip to the top
155 * of the stack before returning.
157 *argp
= (intptr_t)_ctx_start
; /* overwritten with same value */
159 *argp
= (intptr_t)start
;
162 /* Add all the arguments: */
164 for (i
= 0; i
< argc
; i
++) {
165 *argp
= va_arg(ap
, intptr_t);
170 /* The ucontext is placed at the bottom of the stack. */
171 *argp
= (intptr_t)ucp
;
174 * Set the machine context to point to the top of the
175 * stack and the program counter to the context start
176 * wrapper. Note that setcontext() pushes the return
177 * address onto the top of the stack, so allow for this
178 * by adjusting the stack downward 1 slot. Also set
179 * %esi to point to the base of the stack where ucp
182 mc
= ucp
->uc_mcontext
;
184 mc
->__ss
.__esi
= (int)argp
;
186 mc
->__ss
.__esp
= (int)stack_top
+ sizeof(caddr_t
);
187 mc
->__ss
.__eip
= (int)_ctx_start
;
191 #endif /* __i386__ */