makecontext.c revision 101915
1/*
2 * Copyright (c) 2001 Daniel M. Eischen <deischen@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/lib/libc/i386/gen/makecontext.c 101915 2002-08-15 11:58:24Z robert $");
29
30#include <sys/param.h>
31#include <sys/signal.h>
32
33#include <errno.h>
34#include <stdarg.h>
35#include <stdlib.h>
36#include <ucontext.h>
37#include <unistd.h>
38
39/* Prototypes */
40extern void _ctx_start(ucontext_t *, int argc, ...);
41
42
43__weak_reference(__makecontext, makecontext);
44
45void
46_ctx_done (ucontext_t *ucp)
47{
48	if (ucp->uc_link == NULL)
49		exit(0);
50	else {
51		/*
52		 * Since this context has finished, don't allow it
53		 * to be restarted without being reinitialized (via
54		 * setcontext or swapcontext).
55		 */
56		ucp->uc_mcontext.mc_flags = 0;
57
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 */
62	}
63}
64
65void
66__makecontext(ucontext_t *ucp, void (*start)(void), int argc, ...)
67{
68	va_list		ap;
69	char		*stack_top;
70	intptr_t	*argp;
71	int		i;
72
73	if (ucp == NULL)
74		return;
75	else if ((ucp->uc_stack.ss_sp == NULL) ||
76	    (ucp->uc_stack.ss_size < MINSIGSTKSZ)) {
77		/*
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.
82		 */
83		ucp->uc_mcontext.mc_flags = 0;
84	}
85	/* XXX - Do we want to sanity check argc? */
86	else if ((argc < 0) || (argc > NCARGS)) {
87		ucp->uc_mcontext.mc_flags = 0;
88	}
89	/* Make sure the context is valid. */
90	else if ((ucp->uc_mcontext.mc_flags & __UC_MC_VALID) != 0) {
91		/*
92		 * Arrange the stack as follows:
93		 *
94		 *	_ctx_start()	- context start wrapper
95		 *	start()		- user start routine
96		 * 	arg1
97		 *	...
98		 *	argn
99		 *	ucp		- this context, %ebp points here
100		 *
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).
111		 */
112		stack_top = (char *)(ucp->uc_stack.ss_sp +
113		    ucp->uc_stack.ss_size - sizeof(double));
114		stack_top = (char *)ALIGN(stack_top);
115
116		/*
117		 * Adjust top of stack to allow for 3 pointers (return
118		 * address, _ctx_start, and ucp) and argc arguments.
119		 * We allow the arguments to be pointers also.
120		 */
121		stack_top = stack_top - (sizeof(intptr_t) * (3 + argc));
122		argp = (intptr_t *)stack_top;
123
124		/*
125		 * Setup the top of the stack with the user start routine
126		 * followed by all of its aguments and the pointer to the
127		 * ucontext.  We need to leave a spare spot at the top of
128		 * the stack because setcontext will move eip to the top
129		 * of the stack before returning.
130		 */
131		*argp = (intptr_t)_ctx_start;  /* overwritten with same value */
132		argp++;
133		*argp = (intptr_t)start;
134		argp++;
135
136		/* Add all the arguments: */
137		va_start(ap, argc);
138		for (i = 0; i < argc; i++) {
139			*argp = va_arg(ap, intptr_t);
140			argp++;
141		}
142		va_end(ap);
143
144		/* The ucontext is placed at the bottom of the stack. */
145		*argp = (intptr_t)ucp;
146
147		/*
148		 * Set the machine context to point to the top of the
149		 * stack and the program counter to the context start
150		 * wrapper.  Note that setcontext() pushes the return
151		 * address onto the top of the stack, so allow for this
152		 * by adjusting the stack downward 1 slot.  Also set
153		 * %ebp to point to the base of the stack where ucp
154		 * is stored.
155		 */
156		ucp->uc_mcontext.mc_ebp = (int)argp;
157		ucp->uc_mcontext.mc_esp = (int)stack_top + sizeof(caddr_t);
158		ucp->uc_mcontext.mc_eip = (int)_ctx_start;
159	}
160}
161