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 __weak_reference(_makecontext, makecontext); 50 51 /* 52 * makecontext() associates a stack with a user thread context and sets 53 * up to call the start function when switched to. The start function 54 * returns to _ctx_start which then calls _ctx_done to terminate the 55 * context. 56 */ 57 void 58 _makecontext(ucontext_t *ucp, void (*start)(void), int argc, ...) 59 { 60 va_list ap; 61 uint64_t *stack_top; 62 uint64_t *argp; 63 int i; 64 65 if (ucp == NULL) 66 return; 67 68 /* 69 * Invalidate a context which did not have a stack associated with 70 * it or for which the stack was too small. The stack check is 71 * kinda silly, though, since we have no control over the stack 72 * usage of the code being set up to run. 73 */ 74 if ((ucp->uc_stack.ss_sp == NULL) || 75 (ucp->uc_stack.ss_size < MINSIGSTKSZ)) { 76 ucp->uc_mcontext.mc_len = 0; 77 } 78 if (argc < 0 || argc > NCARGS) 79 ucp->uc_mcontext.mc_len = 0; 80 81 if (ucp->uc_mcontext.mc_len == sizeof(mcontext_t)) { 82 /* 83 */ 84 stack_top = (uint64_t *)(ucp->uc_stack.ss_sp + 85 ucp->uc_stack.ss_size); 86 stack_top = (uint64_t *)((uint64_t)(stack_top) & ~15UL); 87 88 argp = stack_top - 6; 89 stack_top -= 7; 90 91 /* Add all the arguments: */ 92 va_start(ap, argc); 93 for (i = 0; i < argc; i++) { 94 argp[i] = va_arg(ap, uint64_t); 95 } 96 va_end(ap); 97 /* The first six arguments are passed via registers. */ 98 for (i = argc; i < 6; i++) { 99 argp[i] = 0; 100 } 101 102 /* 103 * Set the machine context to point to the top of the 104 * stack and the program counter to the context start 105 * wrapper. Note that setcontext() pushes the return 106 * address onto the top of the stack, so allow for this 107 * by adjusting the stack downward 1 slot. Also set 108 * %rbp to point to the base of the stack where ucp 109 * is stored. 110 */ 111 ucp->uc_mcontext.mc_rdi = (register_t)ucp; 112 ucp->uc_mcontext.mc_rsi = (register_t)start; 113 ucp->uc_mcontext.mc_rdx = (register_t)argp; 114 ucp->uc_mcontext.mc_rbp = 0; 115 ucp->uc_mcontext.mc_rbx = (register_t)stack_top; 116 ucp->uc_mcontext.mc_rsp = (register_t)stack_top; 117 ucp->uc_mcontext.mc_rip = (register_t)makectx_wrapper; 118 ucp->uc_mcontext.mc_ownedfp = _MC_FPOWNED_NONE; 119 ucp->uc_mcontext.mc_fpformat = _MC_FPFMT_NODEV; 120 ucp->uc_mcontext.mc_cs = GSEL(GUCODE_SEL, SEL_UPL); 121 ucp->uc_mcontext.mc_ss = GSEL(GUDATA_SEL, SEL_UPL); 122 ucp->uc_mcontext.mc_onstack = 0; 123 ucp->uc_mcontext.mc_err = 0; 124 } 125 } 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