xref: /dragonfly/lib/libc/x86_64/gen/makecontext.c (revision ffe53622)
1 /*
2  * Copyright (c) 2003 Marcel Moolenaar
3  * All rights reserved.
4  * Copyright (c) 2012 Markus Pfeiffer <markus.pfeiffer@morphism.de>
5  * All rights reserved.
6  *
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in
11  *    the documentation and/or other materials provided with the
12  *    distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27 
28 #include <sys/cdefs.h>
29 #include <sys/param.h>
30 #include <sys/signal.h>
31 #include <sys/ucontext.h>
32 
33 #include <machine/frame.h>
34 #include <machine/tss.h>
35 #include <machine/segments.h>
36 
37 #include <signal.h>
38 #include <errno.h>
39 #include <string.h>
40 #include <stdarg.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 
44 typedef void (*func_t)(uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t);
45 
46 /* Prototypes */
47 static void makectx_wrapper(ucontext_t *ucp, func_t func, uint64_t *args);
48 
49 /*
50  * makecontext() associates a stack with a user thread context and sets
51  * up to call the start function when switched to.  The start function
52  * returns to _ctx_start which then calls _ctx_done to terminate the
53  * context.
54  */
55 void
56 _makecontext(ucontext_t *ucp, void (*start)(void), int argc, ...)
57 {
58 	va_list		ap;
59 	uint64_t	*stack_top;
60 	uint64_t	*argp;
61 	int		i;
62 
63 	if (ucp == NULL)
64 		return;
65 
66 	/*
67 	 * Invalidate a context which did not have a stack associated with
68 	 * it or for which the stack was too small.  The stack check is
69 	 * kinda silly, though, since we have no control over the stack
70 	 * usage of the code being set up to run.
71 	 */
72 	if ((ucp->uc_stack.ss_sp == NULL) ||
73 	    (ucp->uc_stack.ss_size < MINSIGSTKSZ)) {
74 		ucp->uc_mcontext.mc_len = 0;
75 	}
76 	if (argc < 0 || argc > NCARGS)
77 		ucp->uc_mcontext.mc_len = 0;
78 
79 	if (ucp->uc_mcontext.mc_len == sizeof(mcontext_t)) {
80 		/*
81 		 */
82 		stack_top = (uint64_t *)(ucp->uc_stack.ss_sp +
83                                          ucp->uc_stack.ss_size);
84 		stack_top = (uint64_t *)((uint64_t)(stack_top) & ~15UL);
85 
86 		argp = stack_top - 6;
87 		stack_top -= 7;
88 
89 		/* Add all the arguments: */
90 		va_start(ap, argc);
91 		for (i = 0; i < argc; i++) {
92 			argp[i] = va_arg(ap, uint64_t);
93 		}
94 		va_end(ap);
95 		/* The first six arguments are passed via registers. */
96 		for (i = argc; i < 6; i++) {
97 			argp[i] = 0;
98 		}
99 
100 		/*
101 		 * Set the machine context to point to the top of the
102 		 * stack and the program counter to the context start
103 		 * wrapper.  Note that setcontext() pushes the return
104 		 * address onto the top of the stack, so allow for this
105 		 * by adjusting the stack downward 1 slot.  Also set
106 		 * %rbp to point to the base of the stack where ucp
107 		 * is stored.
108 		 */
109 		ucp->uc_mcontext.mc_rdi = (register_t)ucp;
110             	ucp->uc_mcontext.mc_rsi = (register_t)start;
111             	ucp->uc_mcontext.mc_rdx = (register_t)argp;
112             	ucp->uc_mcontext.mc_rbp = 0;
113             	ucp->uc_mcontext.mc_rbx = (register_t)stack_top;
114 		ucp->uc_mcontext.mc_rsp = (register_t)stack_top;
115 		ucp->uc_mcontext.mc_rip = (register_t)makectx_wrapper;
116 		ucp->uc_mcontext.mc_ownedfp = _MC_FPOWNED_NONE;
117 		ucp->uc_mcontext.mc_fpformat = _MC_FPFMT_NODEV;
118 		ucp->uc_mcontext.mc_cs = GSEL(GUCODE_SEL, SEL_UPL);
119 		ucp->uc_mcontext.mc_ss = GSEL(GUDATA_SEL, SEL_UPL);
120 		ucp->uc_mcontext.mc_onstack = 0;
121 		ucp->uc_mcontext.mc_err = 0;
122 	}
123 }
124 
125 __weak_reference(_makecontext, makecontext);
126 
127 /* */
128 static void
129 makectx_wrapper(ucontext_t *ucp, func_t func, uint64_t *args)
130 {
131 	(*func)(args[0], args[1], args[2], args[3], args[4], args[5]);
132 	if (ucp->uc_link == NULL)
133 		exit(0);
134 
135 	setcontext((const ucontext_t *)ucp->uc_link);
136 
137 	/* should never reach this */
138 	abort();
139 }
140