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