xref: /freebsd/lib/libc/i386/gen/makecontext.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
1d915a14eSPedro F. Giffuni /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3d915a14eSPedro F. Giffuni  *
4b66b8326SDaniel Eischen  * Copyright (c) 2001 Daniel M. Eischen <deischen@freebsd.org>
5b66b8326SDaniel Eischen  * All rights reserved.
6b66b8326SDaniel Eischen  *
7b66b8326SDaniel Eischen  * Redistribution and use in source and binary forms, with or without
8b66b8326SDaniel Eischen  * modification, are permitted provided that the following conditions
9b66b8326SDaniel Eischen  * are met:
10b66b8326SDaniel Eischen  * 1. Redistributions of source code must retain the above copyright
11b66b8326SDaniel Eischen  *    notice, this list of conditions and the following disclaimer.
12b66b8326SDaniel Eischen  * 2. Neither the name of the author nor the names of its contributors
13b66b8326SDaniel Eischen  *    may be used to endorse or promote products derived from this software
14b66b8326SDaniel Eischen  *    without specific prior written permission.
15b66b8326SDaniel Eischen  *
16b66b8326SDaniel Eischen  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17b66b8326SDaniel Eischen  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18b66b8326SDaniel Eischen  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19b66b8326SDaniel Eischen  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20b66b8326SDaniel Eischen  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21b66b8326SDaniel Eischen  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22b66b8326SDaniel Eischen  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23b66b8326SDaniel Eischen  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24b66b8326SDaniel Eischen  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25b66b8326SDaniel Eischen  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26b66b8326SDaniel Eischen  * SUCH DAMAGE.
27b66b8326SDaniel Eischen  */
28b66b8326SDaniel Eischen 
29b66b8326SDaniel Eischen #include <sys/param.h>
30b66b8326SDaniel Eischen #include <sys/signal.h>
31f9905836SJonathan Mini #include <sys/ucontext.h>
32b66b8326SDaniel Eischen 
33b66b8326SDaniel Eischen #include <errno.h>
34b66b8326SDaniel Eischen #include <stdarg.h>
35b78411b1SRobert Drehmel #include <stdlib.h>
36b66b8326SDaniel Eischen #include <unistd.h>
37b66b8326SDaniel Eischen 
38b66b8326SDaniel Eischen /* Prototypes */
39b66b8326SDaniel Eischen extern void _ctx_start(ucontext_t *, int argc, ...);
40b66b8326SDaniel Eischen 
41b66b8326SDaniel Eischen 
42b66b8326SDaniel Eischen __weak_reference(__makecontext, makecontext);
43b66b8326SDaniel Eischen 
44b66b8326SDaniel Eischen void
_ctx_done(ucontext_t * ucp)45b66b8326SDaniel Eischen _ctx_done (ucontext_t *ucp)
46b66b8326SDaniel Eischen {
47b66b8326SDaniel Eischen 	if (ucp->uc_link == NULL)
48b66b8326SDaniel Eischen 		exit(0);
49b66b8326SDaniel Eischen 	else {
50b66b8326SDaniel Eischen 		/*
51b66b8326SDaniel Eischen 		 * Since this context has finished, don't allow it
52b66b8326SDaniel Eischen 		 * to be restarted without being reinitialized (via
53b66b8326SDaniel Eischen 		 * setcontext or swapcontext).
54b66b8326SDaniel Eischen 		 */
55f9905836SJonathan Mini 		ucp->uc_mcontext.mc_len = 0;
56b66b8326SDaniel Eischen 
57b66b8326SDaniel Eischen 		/* Set context to next one in link */
58b66b8326SDaniel Eischen 		/* XXX - what to do for error, abort? */
59b66b8326SDaniel Eischen 		setcontext((const ucontext_t *)ucp->uc_link);
60b66b8326SDaniel Eischen 		abort();	/* should never get here */
61b66b8326SDaniel Eischen 	}
62b66b8326SDaniel Eischen }
63b66b8326SDaniel Eischen 
64b66b8326SDaniel Eischen void
__makecontext(ucontext_t * ucp,void (* start)(void),int argc,...)65b66b8326SDaniel Eischen __makecontext(ucontext_t *ucp, void (*start)(void), int argc, ...)
66b66b8326SDaniel Eischen {
67b66b8326SDaniel Eischen 	va_list		ap;
68b66b8326SDaniel Eischen 	char		*stack_top;
69b66b8326SDaniel Eischen 	intptr_t	*argp;
70b66b8326SDaniel Eischen 	int		i;
71b66b8326SDaniel Eischen 
72b66b8326SDaniel Eischen 	if (ucp == NULL)
73b66b8326SDaniel Eischen 		return;
74b66b8326SDaniel Eischen 	else if ((ucp->uc_stack.ss_sp == NULL) ||
75b66b8326SDaniel Eischen 	    (ucp->uc_stack.ss_size < MINSIGSTKSZ)) {
76b66b8326SDaniel Eischen 		/*
77b66b8326SDaniel Eischen 		 * This should really return -1 with errno set to ENOMEM
78b66b8326SDaniel Eischen 		 * or something, but the spec says that makecontext is
79b66b8326SDaniel Eischen 		 * a void function.   At least make sure that the context
80b66b8326SDaniel Eischen 		 * isn't valid so it can't be used without an error.
81b66b8326SDaniel Eischen 		 */
82f9905836SJonathan Mini 		ucp->uc_mcontext.mc_len = 0;
83b66b8326SDaniel Eischen 	}
84b66b8326SDaniel Eischen 	/* XXX - Do we want to sanity check argc? */
8580996ef8SJohn Baldwin 	else if (argc < 0) {
86f9905836SJonathan Mini 		ucp->uc_mcontext.mc_len = 0;
87b66b8326SDaniel Eischen 	}
88b66b8326SDaniel Eischen 	/* Make sure the context is valid. */
89f9905836SJonathan Mini 	else if (ucp->uc_mcontext.mc_len == sizeof(mcontext_t)) {
90b66b8326SDaniel Eischen 		/*
91b66b8326SDaniel Eischen 		 * Arrange the stack as follows:
92b66b8326SDaniel Eischen 		 *
93b66b8326SDaniel Eischen 		 *	_ctx_start()	- context start wrapper
94b66b8326SDaniel Eischen 		 *	start()		- user start routine
956c68cb59SDaniel Eischen 		 * 	arg1            - first argument, aligned(16)
96b66b8326SDaniel Eischen 		 *	...
97b66b8326SDaniel Eischen 		 *	argn
98b66b8326SDaniel Eischen 		 *	ucp		- this context, %ebp points here
99b66b8326SDaniel Eischen 		 *
100b66b8326SDaniel Eischen 		 * When the context is started, control will return to
101b66b8326SDaniel Eischen 		 * the context start wrapper which will pop the user
102b66b8326SDaniel Eischen 		 * start routine from the top of the stack.  After that,
103b66b8326SDaniel Eischen 		 * the top of the stack will be setup with all arguments
104b66b8326SDaniel Eischen 		 * necessary for calling the start routine.  When the
105b66b8326SDaniel Eischen 		 * start routine returns, the context wrapper then sets
106b66b8326SDaniel Eischen 		 * the stack pointer to %ebp which was setup to point to
107b66b8326SDaniel Eischen 		 * the base of the stack (and where ucp is stored).  It
108b66b8326SDaniel Eischen 		 * will then call _ctx_done() to swap in the next context
109b66b8326SDaniel Eischen 		 * (uc_link != 0) or exit the program (uc_link == 0).
110b66b8326SDaniel Eischen 		 */
111b66b8326SDaniel Eischen 		stack_top = (char *)(ucp->uc_stack.ss_sp +
1126c68cb59SDaniel Eischen 		    ucp->uc_stack.ss_size - sizeof(intptr_t));
113b66b8326SDaniel Eischen 
114b66b8326SDaniel Eischen 		/*
115b66b8326SDaniel Eischen 		 * Adjust top of stack to allow for 3 pointers (return
116b66b8326SDaniel Eischen 		 * address, _ctx_start, and ucp) and argc arguments.
1176c68cb59SDaniel Eischen 		 * We allow the arguments to be pointers also.  The first
1186c68cb59SDaniel Eischen 		 * argument to the user function must be properly aligned.
119b66b8326SDaniel Eischen 		 */
1206c68cb59SDaniel Eischen 		stack_top = stack_top - (sizeof(intptr_t) * (1 + argc));
1216c68cb59SDaniel Eischen 		stack_top = (char *)((unsigned)stack_top & ~15);
1226c68cb59SDaniel Eischen 		stack_top = stack_top - (2 * sizeof(intptr_t));
123b66b8326SDaniel Eischen 		argp = (intptr_t *)stack_top;
124b66b8326SDaniel Eischen 
125b66b8326SDaniel Eischen 		/*
126b66b8326SDaniel Eischen 		 * Setup the top of the stack with the user start routine
127b66b8326SDaniel Eischen 		 * followed by all of its aguments and the pointer to the
128b66b8326SDaniel Eischen 		 * ucontext.  We need to leave a spare spot at the top of
129b66b8326SDaniel Eischen 		 * the stack because setcontext will move eip to the top
130b66b8326SDaniel Eischen 		 * of the stack before returning.
131b66b8326SDaniel Eischen 		 */
132b66b8326SDaniel Eischen 		*argp = (intptr_t)_ctx_start;  /* overwritten with same value */
133b66b8326SDaniel Eischen 		argp++;
134b66b8326SDaniel Eischen 		*argp = (intptr_t)start;
135b66b8326SDaniel Eischen 		argp++;
136b66b8326SDaniel Eischen 
137b66b8326SDaniel Eischen 		/* Add all the arguments: */
138b66b8326SDaniel Eischen 		va_start(ap, argc);
139b66b8326SDaniel Eischen 		for (i = 0; i < argc; i++) {
140b66b8326SDaniel Eischen 			*argp = va_arg(ap, intptr_t);
141b66b8326SDaniel Eischen 			argp++;
142b66b8326SDaniel Eischen 		}
143b66b8326SDaniel Eischen 		va_end(ap);
144b66b8326SDaniel Eischen 
145b66b8326SDaniel Eischen 		/* The ucontext is placed at the bottom of the stack. */
146b66b8326SDaniel Eischen 		*argp = (intptr_t)ucp;
147b66b8326SDaniel Eischen 
148b66b8326SDaniel Eischen 		/*
149b66b8326SDaniel Eischen 		 * Set the machine context to point to the top of the
150b66b8326SDaniel Eischen 		 * stack and the program counter to the context start
151b66b8326SDaniel Eischen 		 * wrapper.  Note that setcontext() pushes the return
152b66b8326SDaniel Eischen 		 * address onto the top of the stack, so allow for this
153b66b8326SDaniel Eischen 		 * by adjusting the stack downward 1 slot.  Also set
154a5a5d847SDavid Xu 		 * %esi to point to the base of the stack where ucp
155b66b8326SDaniel Eischen 		 * is stored.
156b66b8326SDaniel Eischen 		 */
157a5a5d847SDavid Xu 		ucp->uc_mcontext.mc_esi = (int)argp;
158a5a5d847SDavid Xu 		ucp->uc_mcontext.mc_ebp = 0;
159b66b8326SDaniel Eischen 		ucp->uc_mcontext.mc_esp = (int)stack_top + sizeof(caddr_t);
160b66b8326SDaniel Eischen 		ucp->uc_mcontext.mc_eip = (int)_ctx_start;
161b66b8326SDaniel Eischen 	}
162b66b8326SDaniel Eischen }
163