xref: /dflybsd-src/lib/libc/x86_64/gen/quickcontext.c (revision 588042b53209e1a0825c7d71916378c967cbfb4b)
1819d0c16SMatthew Dillon /*
2819d0c16SMatthew Dillon  * Copyright (c) 2015 Matthew Dillon, All rights reserved.
3819d0c16SMatthew Dillon  *
4819d0c16SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
5819d0c16SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
6819d0c16SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
7819d0c16SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
8819d0c16SMatthew Dillon  *    the documentation and/or other materials provided with the
9819d0c16SMatthew Dillon  *    distribution.
10819d0c16SMatthew Dillon  *
11819d0c16SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
12819d0c16SMatthew Dillon  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
13819d0c16SMatthew Dillon  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
14819d0c16SMatthew Dillon  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
15819d0c16SMatthew Dillon  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
16819d0c16SMatthew Dillon  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
17819d0c16SMatthew Dillon  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18819d0c16SMatthew Dillon  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
19819d0c16SMatthew Dillon  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
20819d0c16SMatthew Dillon  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
21819d0c16SMatthew Dillon  * SUCH DAMAGE.
22819d0c16SMatthew Dillon  */
23819d0c16SMatthew Dillon 
24819d0c16SMatthew Dillon #include <sys/cdefs.h>
25819d0c16SMatthew Dillon #include <sys/param.h>
26819d0c16SMatthew Dillon #include <sys/signal.h>
27819d0c16SMatthew Dillon #include <sys/ucontext.h>
28819d0c16SMatthew Dillon 
29819d0c16SMatthew Dillon #include <machine/frame.h>
30819d0c16SMatthew Dillon #include <machine/tss.h>
31819d0c16SMatthew Dillon #include <machine/segments.h>
32819d0c16SMatthew Dillon 
33819d0c16SMatthew Dillon #include <signal.h>
34819d0c16SMatthew Dillon #include <errno.h>
35819d0c16SMatthew Dillon #include <string.h>
36819d0c16SMatthew Dillon #include <stdarg.h>
37819d0c16SMatthew Dillon #include <stdlib.h>
38819d0c16SMatthew Dillon #include <unistd.h>
39819d0c16SMatthew Dillon 
40819d0c16SMatthew Dillon /* Prototypes */
41819d0c16SMatthew Dillon 
42a32e3ba6SSascha Wildner void _makecontext_quick(ucontext_t *);
43a32e3ba6SSascha Wildner 
44819d0c16SMatthew Dillon static void makectx_quick_wrapper(ucontext_t *ucp, uint64_t *stack_top);
45819d0c16SMatthew Dillon 
46819d0c16SMatthew Dillon /*
47819d0c16SMatthew Dillon  * makecontext_quick() associates a stack with a user thread context
48819d0c16SMatthew Dillon  * setup to execute a cofunc sequence.  The caller only initializes the
49819d0c16SMatthew Dillon  * uc_stack.* fields, uc_cofunc, and uc_arg.  This function will zero or
50819d0c16SMatthew Dillon  * initialize all other fields.  Upon return the caller can optionally
51819d0c16SMatthew Dillon  * also initialize uc_link.
52819d0c16SMatthew Dillon  *
53819d0c16SMatthew Dillon  * These 'quick' calls do not mess with the signal mask and do not require
54819d0c16SMatthew Dillon  * kernel intervention.  Scratch registers (including FP regs, which are also
55819d0c16SMatthew Dillon  * scratch registers) are not saved or restored.  Cofunction loops also
56819d0c16SMatthew Dillon  * optimize cofunc call loops by not saving the register state when
57819d0c16SMatthew Dillon  * switching away to double performance.  Of course, swapcontext_quick()
58819d0c16SMatthew Dillon  * still saves the register state.  There is no getcontext_quick() call
59819d0c16SMatthew Dillon  * on purpose.
60819d0c16SMatthew Dillon  */
61819d0c16SMatthew Dillon void
_makecontext_quick(ucontext_t * ucp)62819d0c16SMatthew Dillon _makecontext_quick(ucontext_t *ucp)
63819d0c16SMatthew Dillon {
64819d0c16SMatthew Dillon 	uint64_t *stack_top;
65819d0c16SMatthew Dillon 
66819d0c16SMatthew Dillon 	if (ucp == NULL)
67819d0c16SMatthew Dillon 		return;
68819d0c16SMatthew Dillon 	bzero(&ucp->uc_sigmask, sizeof(ucp->uc_sigmask));
69819d0c16SMatthew Dillon 	bzero(&ucp->uc_mcontext, sizeof(ucp->uc_mcontext));
70819d0c16SMatthew Dillon 	ucp->uc_link = NULL;
71819d0c16SMatthew Dillon 	ucp->uc_mcontext.mc_len = sizeof(mcontext_t);
72819d0c16SMatthew Dillon 
73*588042b5SSascha Wildner 	stack_top =
74*588042b5SSascha Wildner 	    (uint64_t *)((char *)ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size);
75819d0c16SMatthew Dillon 	stack_top = (uint64_t *)((uint64_t)(stack_top) & ~63UL);
76819d0c16SMatthew Dillon 	stack_top -= 1;
77819d0c16SMatthew Dillon 
78819d0c16SMatthew Dillon 	/*
79819d0c16SMatthew Dillon 	 * Set the machine context to point to the top of the
80819d0c16SMatthew Dillon 	 * stack and the program counter to the context start
81819d0c16SMatthew Dillon 	 * wrapper.  Note that setcontext() pushes the return
82819d0c16SMatthew Dillon 	 * address onto the top of the stack, so allow for this
83819d0c16SMatthew Dillon 	 * by adjusting the stack downward 1 slot.  Also set
84819d0c16SMatthew Dillon 	 * %rbp to point to the base of the stack where ucp
85819d0c16SMatthew Dillon 	 * is stored.
86819d0c16SMatthew Dillon 	 */
87819d0c16SMatthew Dillon 	ucp->uc_mcontext.mc_rdi = (register_t)ucp;
88819d0c16SMatthew Dillon 	ucp->uc_mcontext.mc_rsi = (register_t)stack_top;
89819d0c16SMatthew Dillon 	ucp->uc_mcontext.mc_rsp = (register_t)stack_top;
90819d0c16SMatthew Dillon 	ucp->uc_mcontext.mc_rip = (register_t)makectx_quick_wrapper;
91819d0c16SMatthew Dillon 	ucp->uc_mcontext.mc_ownedfp = _MC_FPOWNED_NONE;
92819d0c16SMatthew Dillon 	ucp->uc_mcontext.mc_fpformat = _MC_FPFMT_NODEV;
93819d0c16SMatthew Dillon 	ucp->uc_mcontext.mc_cs = GSEL(GUCODE_SEL, SEL_UPL);
94819d0c16SMatthew Dillon 	ucp->uc_mcontext.mc_ss = GSEL(GUDATA_SEL, SEL_UPL);
95819d0c16SMatthew Dillon }
96819d0c16SMatthew Dillon 
97f8406b33Szrj __weak_reference(_makecontext_quick, makecontext_quick);
98f8406b33Szrj 
99819d0c16SMatthew Dillon /*
100819d0c16SMatthew Dillon  * If the cofunc call returns set the context up to re-execute the
101819d0c16SMatthew Dillon  * wrapper if the linkages eventually link back to this ucp.  The
102819d0c16SMatthew Dillon  * cofunc can also change uc_cofunc and uc_arg as it desires, allowing
103819d0c16SMatthew Dillon  * cofunctions to be optimally linked together.
104819d0c16SMatthew Dillon  */
105819d0c16SMatthew Dillon static void
makectx_quick_wrapper(ucontext_t * ucp,uint64_t * stack_top)106819d0c16SMatthew Dillon makectx_quick_wrapper(ucontext_t *ucp, uint64_t *stack_top)
107819d0c16SMatthew Dillon {
108819d0c16SMatthew Dillon 	for (;;) {
109819d0c16SMatthew Dillon 		ucp->uc_cofunc(ucp, ucp->uc_arg);
110819d0c16SMatthew Dillon 		if (ucp->uc_link == ucp)
111819d0c16SMatthew Dillon 			continue;
112819d0c16SMatthew Dillon 		ucp->uc_mcontext.mc_rdi = (register_t)ucp;
113819d0c16SMatthew Dillon 		ucp->uc_mcontext.mc_rsi = (register_t)stack_top;
114819d0c16SMatthew Dillon 		ucp->uc_mcontext.mc_rsp = (register_t)stack_top;
115819d0c16SMatthew Dillon 		ucp->uc_mcontext.mc_rip = (register_t)makectx_quick_wrapper;
116819d0c16SMatthew Dillon 		if (ucp->uc_link)
117819d0c16SMatthew Dillon 			setcontext_quick(ucp->uc_link);
118819d0c16SMatthew Dillon 		exit(0);
119819d0c16SMatthew Dillon 	}
120819d0c16SMatthew Dillon }
121