]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/mips/gen/makecontext.c
MFV r361936:
[FreeBSD/FreeBSD.git] / lib / libc / mips / gen / makecontext.c
1 /*      $NetBSD: makecontext.c,v 1.5 2009/12/14 01:07:42 matt Exp $     */
2
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
5  *
6  * Copyright (c) 2001 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Klaus Klein.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
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.
20  *
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.
32  */
33
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 $");
38 #endif
39
40 #include <sys/param.h>
41 #include <machine/abi.h>
42 #define _WANT_MIPS_REGNUM
43 #include <machine/regnum.h>
44
45 #include <stdarg.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48 #include <stdio.h>
49 #include <ucontext.h>
50
51 __weak_reference(__makecontext, makecontext);
52
53 void _ctx_done(ucontext_t *);
54 void _ctx_start(void);
55
56 void
57 __makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...)
58 {
59         mcontext_t *mc;
60         register_t *sp;
61         int i;
62         va_list ap;
63
64         /*
65          * XXX/juli
66          * We need an mc_len or mc_flags like other architectures
67          * so that we can mark a context as invalid.  Store it in
68          * mc->mc_regs[ZERO] perhaps?
69          */
70         if (argc < 0 || ucp == NULL ||
71             ucp->uc_stack.ss_sp == NULL ||
72             ucp->uc_stack.ss_size < MINSIGSTKSZ)
73                 return;
74         mc = &ucp->uc_mcontext;
75
76         sp  = (register_t *)
77             ((uintptr_t)ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size);
78 #if defined(__mips_o32) || defined(__mips_o64)
79         sp -= (argc >= 4 ? argc : 4);   /* Make room for >=4 arguments. */
80 #elif defined(__mips_n32) || defined(__mips_n64)
81         sp -= (argc > 8 ? argc - 8 : 0); /* Make room for > 8 arguments. */
82 #endif
83         sp  = (register_t *)((uintptr_t)sp & ~(STACK_ALIGN - 1));
84
85         mc->mc_regs[SP] = (intptr_t)sp;
86         mc->mc_regs[S0] = (intptr_t)ucp;
87         mc->mc_regs[T9] = (intptr_t)func;
88         mc->mc_pc = (intptr_t)_ctx_start;
89
90         /* Construct argument list. */
91         va_start(ap, argc);
92 #if defined(__mips_o32) || defined(__mips_o64)
93         /* Up to the first four arguments are passed in $a0-3. */
94         for (i = 0; i < argc && i < 4; i++)
95                 /* LINTED register_t is safe */
96                 mc->mc_regs[A0 + i] = va_arg(ap, register_t);
97         /* Skip over the $a0-3 gap. */
98         sp += 4;
99 #endif
100 #if defined(__mips_n32) || defined(__mips_n64)
101         /* Up to the first 8 arguments are passed in $a0-7. */
102         for (i = 0; i < argc && i < 8; i++)
103                 /* LINTED register_t is safe */
104                 mc->mc_regs[A0 + i] = va_arg(ap, register_t);
105 #endif
106         /* Pass remaining arguments on the stack. */
107         for (; i < argc; i++)
108                 /* LINTED register_t is safe */
109                 *sp++ = va_arg(ap, register_t);
110         va_end(ap);
111 }
112
113 void
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 }