xref: /freebsd/lib/libc/powerpc/gen/makecontext.c (revision 0629b152762b06325dd75a41bcb0a2789514141b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2004 Suleiman Souhlal
5  * All rights reserved.
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/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 
34 #include <stdarg.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <stdio.h>
38 #include <ucontext.h>
39 
40 __weak_reference(__makecontext, makecontext);
41 
42 void _ctx_done(ucontext_t *ucp);
43 void _ctx_start(void);
44 
45 void
46 _ctx_done(ucontext_t *ucp)
47 {
48 	if (ucp->uc_link == NULL)
49 		exit(0);
50 	else {
51 		/* invalidate context */
52 		ucp->uc_mcontext.mc_len = 0;
53 
54 		setcontext((const ucontext_t *)ucp->uc_link);
55 
56 		abort(); /* should never return from above call */
57 	}
58 }
59 
60 void
61 __makecontext(ucontext_t *ucp, void (*start)(void), int argc, ...)
62 {
63 	mcontext_t *mc;
64 	char *sp;
65 	va_list ap;
66 	int i, regargs, stackargs;
67 
68 	/* Sanity checks */
69 	if ((ucp == NULL) || (argc < 0)
70 	    || (ucp->uc_stack.ss_sp == NULL)
71 	    || (ucp->uc_stack.ss_size < MINSIGSTKSZ)) {
72 		/* invalidate context */
73 		ucp->uc_mcontext.mc_len = 0;
74 		return;
75 	}
76 
77 	/*
78 	 * The stack must have space for the frame pointer, saved
79 	 * link register, overflow arguments, and be 16-byte
80 	 * aligned.
81 	 */
82 	stackargs = (argc > 8) ? argc - 8 : 0;
83 	sp = (char *) ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size
84 		- sizeof(uint32_t)*(stackargs + 2);
85 	sp = (char *)((uint32_t)sp & ~0x1f);
86 
87 	mc = &ucp->uc_mcontext;
88 
89 	/*
90 	 * Up to 8 register args. Assumes all args are 32-bit and
91 	 * integer only. Not sure how to cater for floating point,
92 	 * although 64-bit args will work if aligned correctly
93 	 * in the arg list.
94 	 */
95 	regargs = (argc > 8) ? 8 : argc;
96 	va_start(ap, argc);
97 	for (i = 0; i < regargs; i++)
98 		mc->mc_gpr[3 + i] = va_arg(ap, uint32_t);
99 
100 	/*
101 	 * Overflow args go onto the stack
102 	 */
103 	if (argc > 8) {
104 		uint32_t *argp;
105 
106 		/* Skip past frame pointer and saved LR */
107 		argp = (uint32_t *)sp + 2;
108 
109 		for (i = 0; i < stackargs; i++)
110 			*argp++ = va_arg(ap, uint32_t);
111 	}
112 	va_end(ap);
113 
114 	/*
115 	 * Use caller-saved regs 14/15 to hold params that _ctx_start
116 	 * will use to invoke the user-supplied func
117 	 */
118 	mc->mc_srr0 = (uint32_t) _ctx_start;
119 	mc->mc_gpr[1] = (uint32_t) sp;		/* new stack pointer */
120 	mc->mc_gpr[14] = (uint32_t) start;	/* r14 <- start */
121 	mc->mc_gpr[15] = (uint32_t) ucp;	/* r15 <- ucp */
122 }
123