1 /*
2 * Copyright (c) 2015 Matthew Dillon, All rights reserved.
3 *
4 * 1. Redistributions of source code must retain the above copyright
5 * notice, this list of conditions and the following disclaimer.
6 * 2. Redistributions in binary form must reproduce the above copyright
7 * notice, this list of conditions and the following disclaimer in
8 * the documentation and/or other materials provided with the
9 * distribution.
10 *
11 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
12 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
13 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
14 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
15 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
16 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
17 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
19 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
20 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
21 * SUCH DAMAGE.
22 */
23
24 #include <sys/cdefs.h>
25 #include <sys/param.h>
26 #include <sys/signal.h>
27 #include <sys/ucontext.h>
28
29 #include <machine/frame.h>
30 #include <machine/tss.h>
31 #include <machine/segments.h>
32
33 #include <signal.h>
34 #include <errno.h>
35 #include <string.h>
36 #include <stdarg.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39
40 /* Prototypes */
41
42 void _makecontext_quick(ucontext_t *);
43
44 static void makectx_quick_wrapper(ucontext_t *ucp, uint64_t *stack_top);
45
46 /*
47 * makecontext_quick() associates a stack with a user thread context
48 * setup to execute a cofunc sequence. The caller only initializes the
49 * uc_stack.* fields, uc_cofunc, and uc_arg. This function will zero or
50 * initialize all other fields. Upon return the caller can optionally
51 * also initialize uc_link.
52 *
53 * These 'quick' calls do not mess with the signal mask and do not require
54 * kernel intervention. Scratch registers (including FP regs, which are also
55 * scratch registers) are not saved or restored. Cofunction loops also
56 * optimize cofunc call loops by not saving the register state when
57 * switching away to double performance. Of course, swapcontext_quick()
58 * still saves the register state. There is no getcontext_quick() call
59 * on purpose.
60 */
61 void
_makecontext_quick(ucontext_t * ucp)62 _makecontext_quick(ucontext_t *ucp)
63 {
64 uint64_t *stack_top;
65
66 if (ucp == NULL)
67 return;
68 bzero(&ucp->uc_sigmask, sizeof(ucp->uc_sigmask));
69 bzero(&ucp->uc_mcontext, sizeof(ucp->uc_mcontext));
70 ucp->uc_link = NULL;
71 ucp->uc_mcontext.mc_len = sizeof(mcontext_t);
72
73 stack_top =
74 (uint64_t *)((char *)ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size);
75 stack_top = (uint64_t *)((uint64_t)(stack_top) & ~63UL);
76 stack_top -= 1;
77
78 /*
79 * Set the machine context to point to the top of the
80 * stack and the program counter to the context start
81 * wrapper. Note that setcontext() pushes the return
82 * address onto the top of the stack, so allow for this
83 * by adjusting the stack downward 1 slot. Also set
84 * %rbp to point to the base of the stack where ucp
85 * is stored.
86 */
87 ucp->uc_mcontext.mc_rdi = (register_t)ucp;
88 ucp->uc_mcontext.mc_rsi = (register_t)stack_top;
89 ucp->uc_mcontext.mc_rsp = (register_t)stack_top;
90 ucp->uc_mcontext.mc_rip = (register_t)makectx_quick_wrapper;
91 ucp->uc_mcontext.mc_ownedfp = _MC_FPOWNED_NONE;
92 ucp->uc_mcontext.mc_fpformat = _MC_FPFMT_NODEV;
93 ucp->uc_mcontext.mc_cs = GSEL(GUCODE_SEL, SEL_UPL);
94 ucp->uc_mcontext.mc_ss = GSEL(GUDATA_SEL, SEL_UPL);
95 }
96
97 __weak_reference(_makecontext_quick, makecontext_quick);
98
99 /*
100 * If the cofunc call returns set the context up to re-execute the
101 * wrapper if the linkages eventually link back to this ucp. The
102 * cofunc can also change uc_cofunc and uc_arg as it desires, allowing
103 * cofunctions to be optimally linked together.
104 */
105 static void
makectx_quick_wrapper(ucontext_t * ucp,uint64_t * stack_top)106 makectx_quick_wrapper(ucontext_t *ucp, uint64_t *stack_top)
107 {
108 for (;;) {
109 ucp->uc_cofunc(ucp, ucp->uc_arg);
110 if (ucp->uc_link == ucp)
111 continue;
112 ucp->uc_mcontext.mc_rdi = (register_t)ucp;
113 ucp->uc_mcontext.mc_rsi = (register_t)stack_top;
114 ucp->uc_mcontext.mc_rsp = (register_t)stack_top;
115 ucp->uc_mcontext.mc_rip = (register_t)makectx_quick_wrapper;
116 if (ucp->uc_link)
117 setcontext_quick(ucp->uc_link);
118 exit(0);
119 }
120 }
121