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