1209233Sjchandra/*	$NetBSD: makecontext.c,v 1.5 2009/12/14 01:07:42 matt Exp $	*/
2178580Simp
3178580Simp/*-
4178580Simp * Copyright (c) 2001 The NetBSD Foundation, Inc.
5178580Simp * All rights reserved.
6178580Simp *
7178580Simp * This code is derived from software contributed to The NetBSD Foundation
8178580Simp * by Klaus Klein.
9178580Simp *
10178580Simp * Redistribution and use in source and binary forms, with or without
11178580Simp * modification, are permitted provided that the following conditions
12178580Simp * are met:
13178580Simp * 1. Redistributions of source code must retain the above copyright
14178580Simp *    notice, this list of conditions and the following disclaimer.
15178580Simp * 2. Redistributions in binary form must reproduce the above copyright
16178580Simp *    notice, this list of conditions and the following disclaimer in the
17178580Simp *    documentation and/or other materials provided with the distribution.
18178580Simp *
19178580Simp * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20178580Simp * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21178580Simp * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22178580Simp * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23178580Simp * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24178580Simp * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25178580Simp * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26178580Simp * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27178580Simp * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28178580Simp * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29178580Simp * POSSIBILITY OF SUCH DAMAGE.
30178580Simp */
31178580Simp
32178580Simp#include <sys/cdefs.h>
33178580Simp__FBSDID("$FreeBSD$");
34178580Simp#if defined(LIBC_SCCS) && !defined(lint)
35209233Sjchandra__RCSID("$NetBSD: makecontext.c,v 1.5 2009/12/14 01:07:42 matt Exp $");
36178580Simp#endif
37178580Simp
38209233Sjchandra#include <sys/param.h>
39209233Sjchandra#include <machine/regnum.h>
40209233Sjchandra
41209233Sjchandra#include <stdarg.h>
42209233Sjchandra#include <stdlib.h>
43209233Sjchandra#include <unistd.h>
44209233Sjchandra#include <stdio.h>
45178580Simp#include <ucontext.h>
46178580Simp
47209233Sjchandra__weak_reference(__makecontext, makecontext);
48209233Sjchandra
49209233Sjchandravoid _ctx_done(ucontext_t *);
50209233Sjchandravoid _ctx_start(void);
51209233Sjchandra
52178580Simpvoid
53209233Sjchandra__makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...)
54178580Simp{
55209233Sjchandra	mcontext_t *mc;
56209233Sjchandra	register_t *sp;
57178580Simp	int i;
58178580Simp	va_list ap;
59178580Simp
60209233Sjchandra	/*
61209233Sjchandra	 * XXX/juli
62209233Sjchandra	 * We need an mc_len or mc_flags like other architectures
63209233Sjchandra	 * so that we can mark a context as invalid.  Store it in
64209233Sjchandra	 * mc->mc_regs[ZERO] perhaps?
65209233Sjchandra	 */
66209233Sjchandra	if (argc < 0 || argc > 6 || ucp == NULL ||
67209233Sjchandra	    ucp->uc_stack.ss_sp == NULL ||
68209233Sjchandra	    ucp->uc_stack.ss_size < MINSIGSTKSZ)
69209233Sjchandra		return;
70209233Sjchandra	mc = &ucp->uc_mcontext;
71178580Simp
72209233Sjchandra	sp  = (register_t *)
73178580Simp	    ((uintptr_t)ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size);
74209233Sjchandra#if defined(__mips_o32) || defined(__mips_o64)
75178580Simp	sp -= (argc >= 4 ? argc : 4);	/* Make room for >=4 arguments. */
76209233Sjchandra	sp  = (register_t *)
77209233Sjchandra	    ((uintptr_t)sp & ~0x7);	/* Align on double-word boundary. */
78209233Sjchandra#elif defined(__mips_n32) || defined(__mips_n64)
79209233Sjchandra	sp -= (argc > 8 ? argc - 8 : 0); /* Make room for > 8 arguments. */
80209233Sjchandra	sp  = (register_t *)
81209233Sjchandra	    ((uintptr_t)sp & ~0xf);	/* Align on quad-word boundary. */
82209233Sjchandra#endif
83178580Simp
84209233Sjchandra	mc->mc_regs[SP] = (intptr_t)sp;
85209233Sjchandra	mc->mc_regs[S0] = (intptr_t)ucp;
86209233Sjchandra	mc->mc_regs[T9] = (intptr_t)func;
87209233Sjchandra	mc->mc_pc = (intptr_t)_ctx_start;
88178580Simp
89178580Simp	/* Construct argument list. */
90178580Simp	va_start(ap, argc);
91209233Sjchandra#if defined(__mips_o32) || defined(__mips_o64)
92178580Simp	/* Up to the first four arguments are passed in $a0-3. */
93178580Simp	for (i = 0; i < argc && i < 4; i++)
94209233Sjchandra		/* LINTED register_t is safe */
95209233Sjchandra		mc->mc_regs[A0 + i] = va_arg(ap, register_t);
96178580Simp	/* Pass remaining arguments on the stack above the $a0-3 gap. */
97209233Sjchandra	sp += i;
98209233Sjchandra#endif
99209233Sjchandra#if defined(__mips_n32) || defined(__mips_n64)
100209233Sjchandra	/* Up to the first 8 arguments are passed in $a0-7. */
101209233Sjchandra	for (i = 0; i < argc && i < 8; i++)
102209233Sjchandra		/* LINTED register_t is safe */
103209233Sjchandra		mc->mc_regs[A0 + i] = va_arg(ap, register_t);
104209233Sjchandra	/* Pass remaining arguments on the stack above the $a0-3 gap. */
105209233Sjchandra#endif
106209233Sjchandra	/* Pass remaining arguments on the stack above the $a0-3 gap. */
107209233Sjchandra	for (; i < argc; i++)
108178580Simp		/* LINTED uintptr_t is safe */
109209233Sjchandra		*sp++ = va_arg(ap, register_t);
110178580Simp	va_end(ap);
111178580Simp}
112209233Sjchandra
113209233Sjchandravoid
114209233Sjchandra_ctx_done(ucontext_t *ucp)
115209233Sjchandra{
116209233Sjchandra
117209233Sjchandra	if (ucp->uc_link == NULL)
118209233Sjchandra		exit(0);
119209233Sjchandra	else {
120209233Sjchandra		setcontext((const ucontext_t *)ucp->uc_link);
121209233Sjchandra		abort();
122209233Sjchandra	}
123209233Sjchandra}
124