/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2001 Daniel M. Eischen <[email protected]>4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Neither the name of the author nor the names of its contributors12* may be used to endorse or promote products derived from this software13* without specific prior written permission.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728#include <sys/param.h>29#include <sys/signal.h>30#include <sys/ucontext.h>3132#include <errno.h>33#include <stdarg.h>34#include <stdlib.h>35#include <unistd.h>3637/* Prototypes */38extern void _ctx_start(ucontext_t *, int argc, ...);394041__weak_reference(__makecontext, makecontext);4243void44_ctx_done (ucontext_t *ucp)45{46if (ucp->uc_link == NULL)47exit(0);48else {49/*50* Since this context has finished, don't allow it51* to be restarted without being reinitialized (via52* setcontext or swapcontext).53*/54ucp->uc_mcontext.mc_len = 0;5556/* Set context to next one in link */57/* XXX - what to do for error, abort? */58setcontext((const ucontext_t *)ucp->uc_link);59abort(); /* should never get here */60}61}6263void64__makecontext(ucontext_t *ucp, void (*start)(void), int argc, ...)65{66va_list ap;67char *stack_top;68intptr_t *argp;69int i;7071if (ucp == NULL)72return;73else if ((ucp->uc_stack.ss_sp == NULL) ||74(ucp->uc_stack.ss_size < MINSIGSTKSZ)) {75/*76* This should really return -1 with errno set to ENOMEM77* or something, but the spec says that makecontext is78* a void function. At least make sure that the context79* isn't valid so it can't be used without an error.80*/81ucp->uc_mcontext.mc_len = 0;82}83/* XXX - Do we want to sanity check argc? */84else if (argc < 0) {85ucp->uc_mcontext.mc_len = 0;86}87/* Make sure the context is valid. */88else if (ucp->uc_mcontext.mc_len == sizeof(mcontext_t)) {89/*90* Arrange the stack as follows:91*92* _ctx_start() - context start wrapper93* start() - user start routine94* arg1 - first argument, aligned(16)95* ...96* argn97* ucp - this context, %ebp points here98*99* When the context is started, control will return to100* the context start wrapper which will pop the user101* start routine from the top of the stack. After that,102* the top of the stack will be setup with all arguments103* necessary for calling the start routine. When the104* start routine returns, the context wrapper then sets105* the stack pointer to %ebp which was setup to point to106* the base of the stack (and where ucp is stored). It107* will then call _ctx_done() to swap in the next context108* (uc_link != 0) or exit the program (uc_link == 0).109*/110stack_top = (char *)(ucp->uc_stack.ss_sp +111ucp->uc_stack.ss_size - sizeof(intptr_t));112113/*114* Adjust top of stack to allow for 3 pointers (return115* address, _ctx_start, and ucp) and argc arguments.116* We allow the arguments to be pointers also. The first117* argument to the user function must be properly aligned.118*/119stack_top = stack_top - (sizeof(intptr_t) * (1 + argc));120stack_top = (char *)((unsigned)stack_top & ~15);121stack_top = stack_top - (2 * sizeof(intptr_t));122argp = (intptr_t *)stack_top;123124/*125* Setup the top of the stack with the user start routine126* followed by all of its arguments and the pointer to the127* ucontext. We need to leave a spare spot at the top of128* the stack because setcontext will move eip to the top129* of the stack before returning.130*/131*argp = (intptr_t)_ctx_start; /* overwritten with same value */132argp++;133*argp = (intptr_t)start;134argp++;135136/* Add all the arguments: */137va_start(ap, argc);138for (i = 0; i < argc; i++) {139*argp = va_arg(ap, intptr_t);140argp++;141}142va_end(ap);143144/* The ucontext is placed at the bottom of the stack. */145*argp = (intptr_t)ucp;146147/*148* Set the machine context to point to the top of the149* stack and the program counter to the context start150* wrapper. Note that setcontext() pushes the return151* address onto the top of the stack, so allow for this152* by adjusting the stack downward 1 slot. Also set153* %esi to point to the base of the stack where ucp154* is stored.155*/156ucp->uc_mcontext.mc_esi = (int)argp;157ucp->uc_mcontext.mc_ebp = 0;158ucp->uc_mcontext.mc_esp = (int)stack_top + sizeof(caddr_t);159ucp->uc_mcontext.mc_eip = (int)_ctx_start;160}161}162163164