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