Merge branch 'vendor/AWK'
[dragonfly.git] / lib / libc / i386 / gen / makecontext.c
1 /*
2  * Copyright (c) 2001 Daniel M. Eischen <deischen@freebsd.org>
3  * All rights reserved.
4  * Copyright (c) 2007 Matthew Dillon <dillon@backplane.com>
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  * $FreeBSD: src/lib/libc/i386/gen/makecontext.c,v 1.5 2004/12/05 21:22:08 deischen Exp $
29  * $DragonFly: src/lib/libc/i386/gen/makecontext.c,v 1.1 2007/01/16 07:16:23 dillon Exp $
30  */
31
32 #include <sys/cdefs.h>
33 #include <sys/param.h>
34 #include <sys/signal.h>
35 #include <sys/ucontext.h>
36
37 #include <errno.h>
38 #include <stdarg.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41
42 /* Prototypes */
43 extern void _ctx_start(ucontext_t *, int argc, ...);
44
45 __weak_reference(_makecontext, makecontext);
46
47 /*
48  * _ctx_done - terminate a context
49  *
50  * The function specified by makecontext() is called by _ctx_start,
51  * returns, and then _ctx_start calls _ctx_done to terminate the context.
52  */
53 void
54 _ctx_done (ucontext_t *ucp)
55 {
56         if (ucp->uc_link == NULL) {
57                 exit(0);
58         } else {
59                 /*
60                  * Since this context has finished, don't allow it
61                  * to be restarted without being reinitialized (via
62                  * setcontext or swapcontext).
63                  */
64                 ucp->uc_mcontext.mc_len = 0;
65
66                 /* Set context to next one in link */
67                 /* XXX - what to do for error, abort? */
68                 setcontext((const ucontext_t *)ucp->uc_link);
69                 abort();        /* should never get here */
70         }
71 }
72
73 /*
74  * makecontext() associates a stack with a user thread context and sets
75  * up to call the start function when switched to.  The start function
76  * returns to _ctx_start which then calls _ctx_done to terminate the
77  * context.
78  */
79 void
80 _makecontext(ucontext_t *ucp, void (*start)(void), int argc, ...)
81 {
82         va_list         ap;
83         char            *stack_top;
84         intptr_t        *argp;
85         int             i;
86
87         if (ucp == NULL)
88                 return;
89
90         /*
91          * Invalidate a context which did not have a stack associated with
92          * it or for which the stack was too small.  The stack check is
93          * kinda silly, though, since we have no control over the stack
94          * usage of the code being set up to run.
95          */
96         if ((ucp->uc_stack.ss_sp == NULL) ||
97             (ucp->uc_stack.ss_size < MINSIGSTKSZ)) {
98                 ucp->uc_mcontext.mc_len = 0;
99         }
100         if (argc < 0 || argc > NCARGS)
101                 ucp->uc_mcontext.mc_len = 0;
102
103         if (ucp->uc_mcontext.mc_len == sizeof(mcontext_t)) {
104                 /*
105                  * Arrange the stack as follows:
106                  *
107                  *      _ctx_start      - dummy return frame for stack trace
108                  *      start_ptr       - user start routine  <<<< ESP PTR
109                  *      arg1            - first argument, aligned(16)
110                  *      ...
111                  *      argn
112                  *      ucp             - this context, %ebp points here
113                  *
114                  * When the context is started, control will return to
115                  * the context start wrapper _ctx_start which will pop the
116                  * user start routine from the top of the stack.  After that,
117                  * the top of the stack will be setup with all arguments
118                  * necessary for calling the start routine.  When the
119                  * start routine returns, the context wrapper then sets
120                  * the stack pointer to %ebp which was setup to point to
121                  * the base of the stack (and where ucp is stored).  It
122                  * will then call _ctx_done() to swap in the next context
123                  * (uc_link != 0) or exit the program (uc_link == 0).
124                  */
125                 stack_top = (char *)(ucp->uc_stack.ss_sp +
126                             ucp->uc_stack.ss_size - sizeof(intptr_t));
127
128                 /*
129                  * Adjust top of stack to allow for 3 pointers (return
130                  * address, _ctx_start, and ucp) and argc arguments.
131                  * We allow the arguments to be pointers also.  The first
132                  * argument to the user function must be properly aligned.
133                  */
134                 stack_top = stack_top - (sizeof(intptr_t) * (1 + argc));
135                 stack_top = (char *)((unsigned)stack_top & ~15);
136                 stack_top = stack_top - (2 * sizeof(intptr_t));
137                 argp = (intptr_t *)stack_top;
138
139                 /*
140                  * Setup the top of the stack with the user start routine
141                  * followed by all of its aguments and the pointer to the
142                  * ucontext.  We need to leave a spare spot at the top of
143                  * the stack because setcontext will move eip to the top
144                  * of the stack before returning.
145                  */
146                 *argp = (intptr_t)_ctx_start;  /* overwritten with same value */
147                 argp++;
148                 *argp = (intptr_t)start;
149                 argp++;
150
151                 /* Add all the arguments: */
152                 va_start(ap, argc);
153                 for (i = 0; i < argc; i++) {
154                         *argp = va_arg(ap, intptr_t);
155                         argp++;
156                 }
157                 va_end(ap);
158
159                 /* The ucontext is placed at the bottom of the stack. */
160                 *argp = (intptr_t)ucp;
161
162                 /*
163                  * Set the machine context to point to the top of the
164                  * stack and the program counter to the context start
165                  * wrapper.  Note that setcontext() pushes the return
166                  * address onto the top of the stack, so allow for this
167                  * by adjusting the stack downward 1 slot.  Also set
168                  * %esi to point to the base of the stack where ucp
169                  * is stored.
170                  */
171                 ucp->uc_mcontext.mc_esi = (int)argp;
172                 ucp->uc_mcontext.mc_ebp = 0;
173                 ucp->uc_mcontext.mc_esp = (int)stack_top + sizeof(caddr_t);
174                 ucp->uc_mcontext.mc_eip = (int)_ctx_start;
175         }
176 }
177