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