xref: /freebsd/lib/libc/i386/gen/makecontext.c (revision b52b9d56d4e96089873a75f9e29062eec19fabba)
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$");
29 
30 #include <sys/param.h>
31 #include <sys/signal.h>
32 
33 #include <errno.h>
34 #include <stdarg.h>
35 #include <ucontext.h>
36 #include <unistd.h>
37 
38 /* Prototypes */
39 extern void _ctx_start(ucontext_t *, int argc, ...);
40 
41 
42 __weak_reference(__makecontext, makecontext);
43 
44 void
45 _ctx_done (ucontext_t *ucp)
46 {
47 	if (ucp->uc_link == NULL)
48 		exit(0);
49 	else {
50 		/*
51 		 * Since this context has finished, don't allow it
52 		 * to be restarted without being reinitialized (via
53 		 * setcontext or swapcontext).
54 		 */
55 		ucp->uc_mcontext.mc_flags = 0;
56 
57 		/* Set context to next one in link */
58 		/* XXX - what to do for error, abort? */
59 		setcontext((const ucontext_t *)ucp->uc_link);
60 		abort();	/* should never get here */
61 	}
62 }
63 
64 void
65 __makecontext(ucontext_t *ucp, void (*start)(void), int argc, ...)
66 {
67 	va_list		ap;
68 	char		*stack_top;
69 	intptr_t	*argp;
70 	int		i;
71 
72 	if (ucp == NULL)
73 		return;
74 	else if ((ucp->uc_stack.ss_sp == NULL) ||
75 	    (ucp->uc_stack.ss_size < MINSIGSTKSZ)) {
76 		/*
77 		 * This should really return -1 with errno set to ENOMEM
78 		 * or something, but the spec says that makecontext is
79 		 * a void function.   At least make sure that the context
80 		 * isn't valid so it can't be used without an error.
81 		 */
82 		ucp->uc_mcontext.mc_flags = 0;
83 	}
84 	/* XXX - Do we want to sanity check argc? */
85 	else if ((argc < 0) || (argc > NCARGS)) {
86 		ucp->uc_mcontext.mc_flags = 0;
87 	}
88 	/* Make sure the context is valid. */
89 	else if ((ucp->uc_mcontext.mc_flags & __UC_MC_VALID) != 0) {
90 		/*
91 		 * Arrange the stack as follows:
92 		 *
93 		 *	_ctx_start()	- context start wrapper
94 		 *	start()		- user start routine
95 		 * 	arg1
96 		 *	...
97 		 *	argn
98 		 *	ucp		- this context, %ebp points here
99 		 *
100 		 * When the context is started, control will return to
101 		 * the context start wrapper which will pop the user
102 		 * start routine from the top of the stack.  After that,
103 		 * the top of the stack will be setup with all arguments
104 		 * necessary for calling the start routine.  When the
105 		 * start routine returns, the context wrapper then sets
106 		 * the stack pointer to %ebp which was setup to point to
107 		 * the base of the stack (and where ucp is stored).  It
108 		 * will then call _ctx_done() to swap in the next context
109 		 * (uc_link != 0) or exit the program (uc_link == 0).
110 		 */
111 		stack_top = (char *)(ucp->uc_stack.ss_sp +
112 		    ucp->uc_stack.ss_size - sizeof(double));
113 		stack_top = (char *)ALIGN(stack_top);
114 
115 		/*
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.
119 		 */
120 		stack_top = stack_top - (sizeof(intptr_t) * (3 + argc));
121 		argp = (intptr_t *)stack_top;
122 
123 		/*
124 		 * Setup the top of the stack with the user start routine
125 		 * followed by all of its aguments and the pointer to the
126 		 * ucontext.  We need to leave a spare spot at the top of
127 		 * the stack because setcontext will move eip to the top
128 		 * of the stack before returning.
129 		 */
130 		*argp = (intptr_t)_ctx_start;  /* overwritten with same value */
131 		argp++;
132 		*argp = (intptr_t)start;
133 		argp++;
134 
135 		/* Add all the arguments: */
136 		va_start(ap, argc);
137 		for (i = 0; i < argc; i++) {
138 			*argp = va_arg(ap, intptr_t);
139 			argp++;
140 		}
141 		va_end(ap);
142 
143 		/* The ucontext is placed at the bottom of the stack. */
144 		*argp = (intptr_t)ucp;
145 
146 		/*
147 		 * Set the machine context to point to the top of the
148 		 * stack and the program counter to the context start
149 		 * wrapper.  Note that setcontext() pushes the return
150 		 * address onto the top of the stack, so allow for this
151 		 * by adjusting the stack downward 1 slot.  Also set
152 		 * %ebp to point to the base of the stack where ucp
153 		 * is stored.
154 		 */
155 		ucp->uc_mcontext.mc_ebp = (int)argp;
156 		ucp->uc_mcontext.mc_esp = (int)stack_top + sizeof(caddr_t);
157 		ucp->uc_mcontext.mc_eip = (int)_ctx_start;
158 	}
159 }
160