xref: /netbsd-src/lib/libc/arch/powerpc64/gen/makecontext.c (revision ce099b40997c43048fb78bd578195f81d2456523)
1*ce099b40Smartin /*	$NetBSD: makecontext.c,v 1.3 2008/04/28 20:22:57 martin Exp $	*/
2d48f1466Sross 
3d48f1466Sross /*-
4d48f1466Sross  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5d48f1466Sross  * All rights reserved.
6d48f1466Sross  *
7d48f1466Sross  * This code is derived from software contributed to The NetBSD Foundation
8d48f1466Sross  * by Klaus Klein.
9d48f1466Sross  *
10d48f1466Sross  * Redistribution and use in source and binary forms, with or without
11d48f1466Sross  * modification, are permitted provided that the following conditions
12d48f1466Sross  * are met:
13d48f1466Sross  * 1. Redistributions of source code must retain the above copyright
14d48f1466Sross  *    notice, this list of conditions and the following disclaimer.
15d48f1466Sross  * 2. Redistributions in binary form must reproduce the above copyright
16d48f1466Sross  *    notice, this list of conditions and the following disclaimer in the
17d48f1466Sross  *    documentation and/or other materials provided with the distribution.
18d48f1466Sross  *
19d48f1466Sross  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20d48f1466Sross  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21d48f1466Sross  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22d48f1466Sross  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23d48f1466Sross  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24d48f1466Sross  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25d48f1466Sross  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26d48f1466Sross  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27d48f1466Sross  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28d48f1466Sross  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29d48f1466Sross  * POSSIBILITY OF SUCH DAMAGE.
30d48f1466Sross  */
31d48f1466Sross 
32d48f1466Sross #include <sys/cdefs.h>
33d48f1466Sross #if defined(LIBC_SCCS) && !defined(lint)
34*ce099b40Smartin __RCSID("$NetBSD: makecontext.c,v 1.3 2008/04/28 20:22:57 martin Exp $");
35d48f1466Sross #endif
36d48f1466Sross 
37d48f1466Sross #include <inttypes.h>
38d48f1466Sross #include <stddef.h>
39d48f1466Sross #include <ucontext.h>
40d48f1466Sross #include "extern.h"
41d48f1466Sross 
42d48f1466Sross #if __STDC__
43d48f1466Sross #include <stdarg.h>
44d48f1466Sross #else
45d48f1466Sross #include <varargs.h>
46d48f1466Sross #endif
47d48f1466Sross 
48d48f1466Sross void
makecontext(ucontext_t * ucp,void (* func)(void),int argc,...)49d48f1466Sross makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...)
50d48f1466Sross {
51d48f1466Sross 	__greg_t *gr = ucp->uc_mcontext.__gregs;
52a9ea5c11Sross 	__greg_t sp, *spp;
53d48f1466Sross 	int i;
54d48f1466Sross 	va_list ap;
55d48f1466Sross 
56a9ea5c11Sross 	sp  = (__greg_t) ((uintptr_t)ucp->uc_stack.ss_sp
57a9ea5c11Sross 	    + ucp->uc_stack.ss_size);
58d48f1466Sross 	sp -= 2 + (argc > 8 ? argc - 8: 0); /* Make room for call frame. */
59a9ea5c11Sross 	sp  = (__greg_t) ((uintptr_t)sp & ~0xf);
60d48f1466Sross 
61d48f1466Sross 	/*
62d48f1466Sross 	 * Start executing at <func> -- when <func> completes, return to
63d48f1466Sross 	 * <_resumecontext>.
64d48f1466Sross 	 */
65a9ea5c11Sross 	gr[_REG_R1]  = sp;
66d48f1466Sross 	gr[_REG_LR] = (__greg_t)_resumecontext;
67d48f1466Sross 	gr[_REG_PC] = (__greg_t)func; // XXX -- this is the descriptor address!
68d48f1466Sross 
69d48f1466Sross 	/* Wipe out stack frame backchain pointer. */
70a9ea5c11Sross 	spp = (__greg_t *)sp;
71a9ea5c11Sross 	*spp = 0;
72d48f1466Sross 
73d48f1466Sross 	/* Construct argument list. */
74d48f1466Sross 	va_start(ap, argc);
75d48f1466Sross 	/* Up to the first eight arguments are passed in r3-10. */
76d48f1466Sross 	for (i = 0; i < argc && i < 8; i++)
77d48f1466Sross 		gr[_REG_R3 + i] = va_arg(ap, long);
78d48f1466Sross 	/* Pass remaining arguments on the stack above the backchain/lr gap. */
79a9ea5c11Sross 	for (spp += 2; i < argc; i++)
80a9ea5c11Sross 		*spp++ = va_arg(ap, long);
81d48f1466Sross 	va_end(ap);
82d48f1466Sross }
83