1/*	$NetBSD: makecontext.c,v 1.5 2009/12/14 01:07:42 matt Exp $	*/
2
3/*-
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Klaus Klein.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD$");
34#if defined(LIBC_SCCS) && !defined(lint)
35__RCSID("$NetBSD: makecontext.c,v 1.5 2009/12/14 01:07:42 matt Exp $");
36#endif
37
38#include <sys/param.h>
39#include <machine/regnum.h>
40
41#include <stdarg.h>
42#include <stdlib.h>
43#include <unistd.h>
44#include <stdio.h>
45#include <ucontext.h>
46
47__weak_reference(__makecontext, makecontext);
48
49void _ctx_done(ucontext_t *);
50void _ctx_start(void);
51
52void
53__makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...)
54{
55	mcontext_t *mc;
56	register_t *sp;
57	int i;
58	va_list ap;
59
60	/*
61	 * XXX/juli
62	 * We need an mc_len or mc_flags like other architectures
63	 * so that we can mark a context as invalid.  Store it in
64	 * mc->mc_regs[ZERO] perhaps?
65	 */
66	if (argc < 0 || argc > 6 || ucp == NULL ||
67	    ucp->uc_stack.ss_sp == NULL ||
68	    ucp->uc_stack.ss_size < MINSIGSTKSZ)
69		return;
70	mc = &ucp->uc_mcontext;
71
72	sp  = (register_t *)
73	    ((uintptr_t)ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size);
74#if defined(__mips_o32) || defined(__mips_o64)
75	sp -= (argc >= 4 ? argc : 4);	/* Make room for >=4 arguments. */
76	sp  = (register_t *)
77	    ((uintptr_t)sp & ~0x7);	/* Align on double-word boundary. */
78#elif defined(__mips_n32) || defined(__mips_n64)
79	sp -= (argc > 8 ? argc - 8 : 0); /* Make room for > 8 arguments. */
80	sp  = (register_t *)
81	    ((uintptr_t)sp & ~0xf);	/* Align on quad-word boundary. */
82#endif
83
84	mc->mc_regs[SP] = (intptr_t)sp;
85	mc->mc_regs[S0] = (intptr_t)ucp;
86	mc->mc_regs[T9] = (intptr_t)func;
87	mc->mc_pc = (intptr_t)_ctx_start;
88
89	/* Construct argument list. */
90	va_start(ap, argc);
91#if defined(__mips_o32) || defined(__mips_o64)
92	/* Up to the first four arguments are passed in $a0-3. */
93	for (i = 0; i < argc && i < 4; i++)
94		/* LINTED register_t is safe */
95		mc->mc_regs[A0 + i] = va_arg(ap, register_t);
96	/* Pass remaining arguments on the stack above the $a0-3 gap. */
97	sp += i;
98#endif
99#if defined(__mips_n32) || defined(__mips_n64)
100	/* Up to the first 8 arguments are passed in $a0-7. */
101	for (i = 0; i < argc && i < 8; i++)
102		/* LINTED register_t is safe */
103		mc->mc_regs[A0 + i] = va_arg(ap, register_t);
104	/* Pass remaining arguments on the stack above the $a0-3 gap. */
105#endif
106	/* Pass remaining arguments on the stack above the $a0-3 gap. */
107	for (; i < argc; i++)
108		/* LINTED uintptr_t is safe */
109		*sp++ = va_arg(ap, register_t);
110	va_end(ap);
111}
112
113void
114_ctx_done(ucontext_t *ucp)
115{
116
117	if (ucp->uc_link == NULL)
118		exit(0);
119	else {
120		setcontext((const ucontext_t *)ucp->uc_link);
121		abort();
122	}
123}
124