xref: /freebsd/lib/libc/i386/gen/makecontext.c (revision 2e3f49888ec8851bafb22011533217487764fdb0)
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/param.h>
30 #include <sys/signal.h>
31 #include <sys/ucontext.h>
32 
33 #include <errno.h>
34 #include <stdarg.h>
35 #include <stdlib.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_len = 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_len = 0;
83 	}
84 	/* XXX - Do we want to sanity check argc? */
85 	else if (argc < 0) {
86 		ucp->uc_mcontext.mc_len = 0;
87 	}
88 	/* Make sure the context is valid. */
89 	else if (ucp->uc_mcontext.mc_len == sizeof(mcontext_t)) {
90 		/*
91 		 * Arrange the stack as follows:
92 		 *
93 		 *	_ctx_start()	- context start wrapper
94 		 *	start()		- user start routine
95 		 * 	arg1            - first argument, aligned(16)
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(intptr_t));
113 
114 		/*
115 		 * Adjust top of stack to allow for 3 pointers (return
116 		 * address, _ctx_start, and ucp) and argc arguments.
117 		 * We allow the arguments to be pointers also.  The first
118 		 * argument to the user function must be properly aligned.
119 		 */
120 		stack_top = stack_top - (sizeof(intptr_t) * (1 + argc));
121 		stack_top = (char *)((unsigned)stack_top & ~15);
122 		stack_top = stack_top - (2 * sizeof(intptr_t));
123 		argp = (intptr_t *)stack_top;
124 
125 		/*
126 		 * Setup the top of the stack with the user start routine
127 		 * followed by all of its aguments and the pointer to the
128 		 * ucontext.  We need to leave a spare spot at the top of
129 		 * the stack because setcontext will move eip to the top
130 		 * of the stack before returning.
131 		 */
132 		*argp = (intptr_t)_ctx_start;  /* overwritten with same value */
133 		argp++;
134 		*argp = (intptr_t)start;
135 		argp++;
136 
137 		/* Add all the arguments: */
138 		va_start(ap, argc);
139 		for (i = 0; i < argc; i++) {
140 			*argp = va_arg(ap, intptr_t);
141 			argp++;
142 		}
143 		va_end(ap);
144 
145 		/* The ucontext is placed at the bottom of the stack. */
146 		*argp = (intptr_t)ucp;
147 
148 		/*
149 		 * Set the machine context to point to the top of the
150 		 * stack and the program counter to the context start
151 		 * wrapper.  Note that setcontext() pushes the return
152 		 * address onto the top of the stack, so allow for this
153 		 * by adjusting the stack downward 1 slot.  Also set
154 		 * %esi to point to the base of the stack where ucp
155 		 * is stored.
156 		 */
157 		ucp->uc_mcontext.mc_esi = (int)argp;
158 		ucp->uc_mcontext.mc_ebp = 0;
159 		ucp->uc_mcontext.mc_esp = (int)stack_top + sizeof(caddr_t);
160 		ucp->uc_mcontext.mc_eip = (int)_ctx_start;
161 	}
162 }
163