Path: blob/main/lib/libc/powerpc/gen/makecontext.c
39491 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2004 Suleiman Souhlal4* 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. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.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>2930#include <stdarg.h>31#include <stdlib.h>32#include <unistd.h>33#include <stdio.h>34#include <ucontext.h>3536__weak_reference(__makecontext, makecontext);3738void _ctx_done(ucontext_t *ucp);39void _ctx_start(void);4041void42_ctx_done(ucontext_t *ucp)43{44if (ucp->uc_link == NULL)45exit(0);46else {47/* invalidate context */48ucp->uc_mcontext.mc_len = 0;4950setcontext((const ucontext_t *)ucp->uc_link);5152abort(); /* should never return from above call */53}54}5556void57__makecontext(ucontext_t *ucp, void (*start)(void), int argc, ...)58{59mcontext_t *mc;60char *sp;61va_list ap;62int i, regargs, stackargs;6364/* Sanity checks */65if ((ucp == NULL) || (argc < 0)66|| (ucp->uc_stack.ss_sp == NULL)67|| (ucp->uc_stack.ss_size < MINSIGSTKSZ)) {68/* invalidate context */69ucp->uc_mcontext.mc_len = 0;70return;71}7273/*74* The stack must have space for the frame pointer, saved75* link register, overflow arguments, and be 16-byte76* aligned.77*/78stackargs = (argc > 8) ? argc - 8 : 0;79sp = (char *) ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size80- sizeof(uint32_t)*(stackargs + 2);81sp = (char *)((uint32_t)sp & ~0x1f);8283mc = &ucp->uc_mcontext;8485/*86* Up to 8 register args. Assumes all args are 32-bit and87* integer only. Not sure how to cater for floating point,88* although 64-bit args will work if aligned correctly89* in the arg list.90*/91regargs = (argc > 8) ? 8 : argc;92va_start(ap, argc);93for (i = 0; i < regargs; i++)94mc->mc_gpr[3 + i] = va_arg(ap, uint32_t);9596/*97* Overflow args go onto the stack98*/99if (argc > 8) {100uint32_t *argp;101102/* Skip past frame pointer and saved LR */103argp = (uint32_t *)sp + 2;104105for (i = 0; i < stackargs; i++)106*argp++ = va_arg(ap, uint32_t);107}108va_end(ap);109110/*111* Use caller-saved regs 14/15 to hold params that _ctx_start112* will use to invoke the user-supplied func113*/114mc->mc_srr0 = (uint32_t) _ctx_start;115mc->mc_gpr[1] = (uint32_t) sp; /* new stack pointer */116mc->mc_gpr[14] = (uint32_t) start; /* r14 <- start */117mc->mc_gpr[15] = (uint32_t) ucp; /* r15 <- ucp */118}119120121