xref: /freebsd/lib/libc/i386/gen/makecontext.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2001 Daniel M. Eischen <deischen@freebsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Neither the name of the author nor the names of its contributors
13  *    may be used to endorse or promote products derived from this software
14  *    without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/signal.h>
32 #include <sys/ucontext.h>
33 
34 #include <errno.h>
35 #include <stdarg.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 
39 /* Prototypes */
40 extern void _ctx_start(ucontext_t *, int argc, ...);
41 
42 
43 __weak_reference(__makecontext, makecontext);
44 
45 void
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_len = 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 
65 void
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_len = 0;
84 	}
85 	/* XXX - Do we want to sanity check argc? */
86 	else if (argc < 0) {
87 		ucp->uc_mcontext.mc_len = 0;
88 	}
89 	/* Make sure the context is valid. */
90 	else if (ucp->uc_mcontext.mc_len == sizeof(mcontext_t)) {
91 		/*
92 		 * Arrange the stack as follows:
93 		 *
94 		 *	_ctx_start()	- context start wrapper
95 		 *	start()		- user start routine
96 		 * 	arg1            - first argument, aligned(16)
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(intptr_t));
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.  The first
119 		 * argument to the user function must be properly aligned.
120 		 */
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;
125 
126 		/*
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.
132 		 */
133 		*argp = (intptr_t)_ctx_start;  /* overwritten with same value */
134 		argp++;
135 		*argp = (intptr_t)start;
136 		argp++;
137 
138 		/* Add all the arguments: */
139 		va_start(ap, argc);
140 		for (i = 0; i < argc; i++) {
141 			*argp = va_arg(ap, intptr_t);
142 			argp++;
143 		}
144 		va_end(ap);
145 
146 		/* The ucontext is placed at the bottom of the stack. */
147 		*argp = (intptr_t)ucp;
148 
149 		/*
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
156 		 * is stored.
157 		 */
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;
162 	}
163 }
164