1*0bfee928SRuslan Bukin /*-
2*0bfee928SRuslan Bukin * Copyright (c) 2015 Ruslan Bukin <br@bsdpad.com>
3*0bfee928SRuslan Bukin * All rights reserved.
4*0bfee928SRuslan Bukin *
5*0bfee928SRuslan Bukin * Portions of this software were developed by SRI International and the
6*0bfee928SRuslan Bukin * University of Cambridge Computer Laboratory under DARPA/AFRL contract
7*0bfee928SRuslan Bukin * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.
8*0bfee928SRuslan Bukin *
9*0bfee928SRuslan Bukin * Portions of this software were developed by the University of Cambridge
10*0bfee928SRuslan Bukin * Computer Laboratory as part of the CTSRD Project, with support from the
11*0bfee928SRuslan Bukin * UK Higher Education Innovation Fund (HEIF).
12*0bfee928SRuslan Bukin *
13*0bfee928SRuslan Bukin * Redistribution and use in source and binary forms, with or without
14*0bfee928SRuslan Bukin * modification, are permitted provided that the following conditions
15*0bfee928SRuslan Bukin * are met:
16*0bfee928SRuslan Bukin * 1. Redistributions of source code must retain the above copyright
17*0bfee928SRuslan Bukin * notice, this list of conditions and the following disclaimer.
18*0bfee928SRuslan Bukin * 2. Redistributions in binary form must reproduce the above copyright
19*0bfee928SRuslan Bukin * notice, this list of conditions and the following disclaimer in the
20*0bfee928SRuslan Bukin * documentation and/or other materials provided with the distribution.
21*0bfee928SRuslan Bukin *
22*0bfee928SRuslan Bukin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23*0bfee928SRuslan Bukin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*0bfee928SRuslan Bukin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*0bfee928SRuslan Bukin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26*0bfee928SRuslan Bukin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*0bfee928SRuslan Bukin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*0bfee928SRuslan Bukin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*0bfee928SRuslan Bukin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*0bfee928SRuslan Bukin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*0bfee928SRuslan Bukin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*0bfee928SRuslan Bukin * SUCH DAMAGE.
33*0bfee928SRuslan Bukin */
34*0bfee928SRuslan Bukin
35*0bfee928SRuslan Bukin #include <sys/param.h>
36*0bfee928SRuslan Bukin
37*0bfee928SRuslan Bukin #include <machine/riscvreg.h>
38*0bfee928SRuslan Bukin
39*0bfee928SRuslan Bukin #include <inttypes.h>
40*0bfee928SRuslan Bukin #include <stdarg.h>
41*0bfee928SRuslan Bukin #include <stdlib.h>
42*0bfee928SRuslan Bukin #include <ucontext.h>
43*0bfee928SRuslan Bukin
44*0bfee928SRuslan Bukin void _ctx_start(void);
45*0bfee928SRuslan Bukin
46*0bfee928SRuslan Bukin void
ctx_done(ucontext_t * ucp)47*0bfee928SRuslan Bukin ctx_done(ucontext_t *ucp)
48*0bfee928SRuslan Bukin {
49*0bfee928SRuslan Bukin
50*0bfee928SRuslan Bukin if (ucp->uc_link == NULL) {
51*0bfee928SRuslan Bukin exit(0);
52*0bfee928SRuslan Bukin } else {
53*0bfee928SRuslan Bukin setcontext((const ucontext_t *)ucp->uc_link);
54*0bfee928SRuslan Bukin abort();
55*0bfee928SRuslan Bukin }
56*0bfee928SRuslan Bukin }
57*0bfee928SRuslan Bukin
58*0bfee928SRuslan Bukin __weak_reference(__makecontext, makecontext);
59*0bfee928SRuslan Bukin
60*0bfee928SRuslan Bukin void
__makecontext(ucontext_t * ucp,void (* func)(void),int argc,...)61*0bfee928SRuslan Bukin __makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...)
62*0bfee928SRuslan Bukin {
63*0bfee928SRuslan Bukin struct gpregs *gp;
64*0bfee928SRuslan Bukin va_list ap;
65*0bfee928SRuslan Bukin int i;
66*0bfee928SRuslan Bukin
67*0bfee928SRuslan Bukin /* A valid context is required. */
68*0bfee928SRuslan Bukin if (ucp == NULL)
69*0bfee928SRuslan Bukin return;
70*0bfee928SRuslan Bukin
71*0bfee928SRuslan Bukin if ((argc < 0) || (argc > 8))
72*0bfee928SRuslan Bukin return;
73*0bfee928SRuslan Bukin
74*0bfee928SRuslan Bukin gp = &ucp->uc_mcontext.mc_gpregs;
75*0bfee928SRuslan Bukin
76*0bfee928SRuslan Bukin va_start(ap, argc);
77*0bfee928SRuslan Bukin /* Pass up to eight arguments in a0-7. */
78*0bfee928SRuslan Bukin for (i = 0; i < argc && i < 8; i++)
79*0bfee928SRuslan Bukin gp->gp_a[i] = va_arg(ap, uint64_t);
80*0bfee928SRuslan Bukin va_end(ap);
81*0bfee928SRuslan Bukin
82*0bfee928SRuslan Bukin /* Set the stack */
83*0bfee928SRuslan Bukin gp->gp_sp = STACKALIGN(ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size);
84*0bfee928SRuslan Bukin /* Arrange for return via the trampoline code. */
85*0bfee928SRuslan Bukin gp->gp_sepc = (__register_t)_ctx_start;
86*0bfee928SRuslan Bukin gp->gp_s[0] = (__register_t)func;
87*0bfee928SRuslan Bukin gp->gp_s[1] = (__register_t)ucp;
88*0bfee928SRuslan Bukin }
89