summaryrefslogtreecommitdiffstats
path: root/lib/libc/alpha/gen/makecontext.c
blob: bc8ea037731aab24f9f5cbe2e49249acc7e79594 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Copyright (c) 2001 Daniel M. Eischen <deischen@freebsd.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Neither the name of the author nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/signal.h>

#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <ucontext.h>
#include <unistd.h>


/* Prototypes */
extern void _ctx_start(int argc, ...);


__weak_reference(__makecontext, makecontext);

void
_ctx_done (ucontext_t *ucp)
{
	if (ucp->uc_link == NULL)
		exit(0);
	else {
		/*
		 * Since this context has finished, don't allow it
		 * to be restarted without being reinitialized (via
		 * setcontext or swapcontext).
		 */
		ucp->uc_mcontext.mc_format = 0;

		/* Set context to next one in link */
		/* XXX - what to do for error, abort? */
		setcontext((const ucontext_t *)ucp->uc_link);
		abort();	/* should never get here */
	}
}

void
__makecontext(ucontext_t *ucp, void (*start)(void), int argc, ...)
{
	va_list 	ap;
	char		*stack_top;
	intptr_t	*argp;
	int		i;

	if (ucp == NULL)
		return;
	else if ((ucp->uc_stack.ss_sp == NULL) ||
	    (ucp->uc_stack.ss_size < MINSIGSTKSZ)) {
		/*
		 * This should really return -1 with errno set to ENOMEM
		 * or something, but the spec says that makecontext is
		 * a void function.   At least make sure that the context
		 * isn't valid so it can't be used without an error.
		 */
		ucp->uc_mcontext.mc_format = 0;
	}
	/* XXX - Do we want to sanity check argc? */
	else if ((argc < 0) || (argc > NCARGS)) {
		ucp->uc_mcontext.mc_format = 0;
	}
	/*
	 * Make sure the context is valid.  For now, we only allow
	 * trapframe format contexts to be used for makecontext.
	 */
	else if (ucp->uc_mcontext.mc_format == _MC_REV0_TRAPFRAME) {
		/*
		 * Alpha passes the first 6 parameters in registers and
		 * remaining parameters on the stack.  Set up the context
		 * accordingly, with the user start routine in register
		 * S0, and the context start wrapper (_ctx_start) in the
		 * program counter and return address.  The context must
		 * be in trapframe format.
		 *
		 * Note: The context start wrapper needs to retrieve the
		 *       ucontext pointer.  Place this in register S1
		 *       which must be saved by the callee.
		 */
		stack_top = (char *)(ucp->uc_stack.ss_sp +
		    ucp->uc_stack.ss_size - sizeof(double));
		stack_top = (char *)ALIGN(stack_top);

		/*
		 * Adjust top of stack to allow for any additional integer
		 * arguments beyond 6.
		 */
		if (argc > 6)
			stack_top = stack_top - (sizeof(intptr_t) * (argc - 6));

		argp = (intptr_t *)stack_top;

		va_start(ap, argc);
		for (i = 0; i < argc; i++) {
			switch (i) {
			case 0:	ucp->uc_mcontext.mc_regs[FRAME_A0] =
				    (unsigned long)va_arg(ap, intptr_t);
				break;

			case 1: ucp->uc_mcontext.mc_regs[FRAME_A1] =
				    (unsigned long)va_arg(ap, intptr_t);
				break;

			case 2:	ucp->uc_mcontext.mc_regs[FRAME_A2] =
				    (unsigned long)va_arg(ap, intptr_t);
				break;

			case 3:	ucp->uc_mcontext.mc_regs[FRAME_A3] =
				    (unsigned long)va_arg(ap, intptr_t);
				break;

			case 4:	ucp->uc_mcontext.mc_regs[FRAME_A4] =
				    (unsigned long)va_arg(ap, intptr_t);
				break;

			case 5:	ucp->uc_mcontext.mc_regs[FRAME_A5] =
				    (unsigned long)va_arg(ap, intptr_t);
				break;

			default:
				*argp = va_arg(ap, intptr_t);
				argp++;
				break;
			}
		}
		va_end(ap);

		/*
		 * The start routine and ucontext are placed in registers
		 * S0 and S1 respectively.
		 */
		ucp->uc_mcontext.mc_regs[FRAME_S0] = (unsigned long)start;
		ucp->uc_mcontext.mc_regs[FRAME_S1] = (unsigned long)ucp;

		/*
		 * Set the machine context to point to the top of the stack,
		 * and the program counter and return address to the context
		 * start wrapper.
		 */
		ucp->uc_mcontext.mc_regs[FRAME_SP] = (unsigned long)stack_top;
		ucp->uc_mcontext.mc_regs[FRAME_PC] = (unsigned long)_ctx_start;
		ucp->uc_mcontext.mc_regs[FRAME_RA] = (unsigned long)_ctx_start;
		ucp->uc_mcontext.mc_regs[FRAME_T12] = (unsigned long)_ctx_start;
	}
}
OpenPOWER on IntegriCloud