/*-1* Copyright (c) 2015 Ruslan Bukin <[email protected]>2* All rights reserved.3*4* Portions of this software were developed by SRI International and the5* University of Cambridge Computer Laboratory under DARPA/AFRL contract6* FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.7*8* Portions of this software were developed by the University of Cambridge9* Computer Laboratory as part of the CTSRD Project, with support from the10* UK Higher Education Innovation Fund (HEIF).11*12* Redistribution and use in source and binary forms, with or without13* modification, are permitted provided that the following conditions14* are met:15* 1. Redistributions of source code must retain the above copyright16* notice, this list of conditions and the following disclaimer.17* 2. Redistributions in binary form must reproduce the above copyright18* notice, this list of conditions and the following disclaimer in the19* documentation and/or other materials provided with the distribution.20*21* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334#include <sys/param.h>3536#include <machine/riscvreg.h>3738#include <inttypes.h>39#include <stdarg.h>40#include <stdlib.h>41#include <ucontext.h>4243void _ctx_start(void);4445void46ctx_done(ucontext_t *ucp)47{4849if (ucp->uc_link == NULL) {50exit(0);51} else {52setcontext((const ucontext_t *)ucp->uc_link);53abort();54}55}5657__weak_reference(__makecontext, makecontext);5859void60__makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...)61{62struct gpregs *gp;63va_list ap;64int i;6566/* A valid context is required. */67if (ucp == NULL)68return;6970if ((argc < 0) || (argc > 8))71return;7273gp = &ucp->uc_mcontext.mc_gpregs;7475va_start(ap, argc);76/* Pass up to eight arguments in a0-7. */77for (i = 0; i < argc && i < 8; i++)78gp->gp_a[i] = va_arg(ap, uint64_t);79va_end(ap);8081/* Set the stack */82gp->gp_sp = STACKALIGN(ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size);83/* Arrange for return via the trampoline code. */84gp->gp_sepc = (__register_t)_ctx_start;85gp->gp_s[0] = (__register_t)func;86gp->gp_s[1] = (__register_t)ucp;87}888990