2 * Copyright (c) 2001 Daniel M. Eischen <deischen@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Neither the name of the author nor the names of its contributors
11 * may be used to endorse or promote products derived from this software
12 * without specific prior written permission.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/signal.h>
32 #include <sys/ucontext.h>
40 extern void _ctx_start(ucontext_t *, int argc, ...);
43 __weak_reference(__makecontext, makecontext);
46 _ctx_done (ucontext_t *ucp)
48 if (ucp->uc_link == NULL)
52 * Since this context has finished, don't allow it
53 * to be restarted without being reinitialized (via
54 * setcontext or swapcontext).
56 ucp->uc_mcontext.mc_len = 0;
58 /* Set context to next one in link */
59 /* XXX - what to do for error, abort? */
60 setcontext((const ucontext_t *)ucp->uc_link);
61 abort(); /* should never get here */
66 __makecontext(ucontext_t *ucp, void (*start)(void), int argc, ...)
75 else if ((ucp->uc_stack.ss_sp == NULL) ||
76 (ucp->uc_stack.ss_size < MINSIGSTKSZ)) {
78 * This should really return -1 with errno set to ENOMEM
79 * or something, but the spec says that makecontext is
80 * a void function. At least make sure that the context
81 * isn't valid so it can't be used without an error.
83 ucp->uc_mcontext.mc_len = 0;
85 /* XXX - Do we want to sanity check argc? */
86 else if ((argc < 0) || (argc > NCARGS)) {
87 ucp->uc_mcontext.mc_len = 0;
89 /* Make sure the context is valid. */
90 else if (ucp->uc_mcontext.mc_len == sizeof(mcontext_t)) {
92 * Arrange the stack as follows:
94 * _ctx_start() - context start wrapper
95 * start() - user start routine
96 * arg1 - first argument, aligned(16)
99 * ucp - this context, %ebp points here
101 * When the context is started, control will return to
102 * the context start wrapper which will pop the user
103 * start routine from the top of the stack. After that,
104 * the top of the stack will be setup with all arguments
105 * necessary for calling the start routine. When the
106 * start routine returns, the context wrapper then sets
107 * the stack pointer to %ebp which was setup to point to
108 * the base of the stack (and where ucp is stored). It
109 * will then call _ctx_done() to swap in the next context
110 * (uc_link != 0) or exit the program (uc_link == 0).
112 stack_top = (char *)(ucp->uc_stack.ss_sp +
113 ucp->uc_stack.ss_size - sizeof(intptr_t));
116 * Adjust top of stack to allow for 3 pointers (return
117 * address, _ctx_start, and ucp) and argc arguments.
118 * We allow the arguments to be pointers also. The first
119 * argument to the user function must be properly aligned.
121 stack_top = stack_top - (sizeof(intptr_t) * (1 + argc));
122 stack_top = (char *)((unsigned)stack_top & ~15);
123 stack_top = stack_top - (2 * sizeof(intptr_t));
124 argp = (intptr_t *)stack_top;
127 * Setup the top of the stack with the user start routine
128 * followed by all of its aguments and the pointer to the
129 * ucontext. We need to leave a spare spot at the top of
130 * the stack because setcontext will move eip to the top
131 * of the stack before returning.
133 *argp = (intptr_t)_ctx_start; /* overwritten with same value */
135 *argp = (intptr_t)start;
138 /* Add all the arguments: */
140 for (i = 0; i < argc; i++) {
141 *argp = va_arg(ap, intptr_t);
146 /* The ucontext is placed at the bottom of the stack. */
147 *argp = (intptr_t)ucp;
150 * Set the machine context to point to the top of the
151 * stack and the program counter to the context start
152 * wrapper. Note that setcontext() pushes the return
153 * address onto the top of the stack, so allow for this
154 * by adjusting the stack downward 1 slot. Also set
155 * %esi to point to the base of the stack where ucp
158 ucp->uc_mcontext.mc_esi = (int)argp;
159 ucp->uc_mcontext.mc_ebp = 0;
160 ucp->uc_mcontext.mc_esp = (int)stack_top + sizeof(caddr_t);
161 ucp->uc_mcontext.mc_eip = (int)_ctx_start;