xref: /titanic_51/usr/src/lib/libc/i386/gen/makectxt.c (revision 4201a95e0468170d576f82c3aa63afecf718497a)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #pragma weak _makecontext = makecontext
33 
34 #include "lint.h"
35 #include <stdarg.h>
36 #include <ucontext.h>
37 #include <sys/stack.h>
38 
39 /*
40  * The ucontext_t that the user passes in must have been primed with a
41  * call to getcontext(2), have the uc_stack member set to reflect the
42  * stack which this context will use, and have the uc_link member set
43  * to the context which should be resumed when this context returns.
44  * When makecontext() returns, the ucontext_t will be set to run the
45  * given function with the given parameters on the stack specified by
46  * uc_stack, and which will return to the ucontext_t specified by uc_link.
47  */
48 
49 static void resumecontext(void);
50 
51 void
52 makecontext(ucontext_t *ucp, void (*func)(), int argc, ...)
53 {
54 	long *sp;
55 	long *tsp;
56 	va_list ap;
57 	size_t size;
58 
59 	ucp->uc_mcontext.gregs[EIP] = (greg_t)func;
60 
61 	size = sizeof (long) * (argc + 1);
62 
63 	sp = (long *)(((uintptr_t)ucp->uc_stack.ss_sp +
64 	    ucp->uc_stack.ss_size - size) & ~(STACK_ALIGN - 1));
65 
66 	tsp = sp + 1;
67 
68 	va_start(ap, argc);
69 
70 	while (argc-- > 0) {
71 		*tsp++ = va_arg(ap, long);
72 	}
73 
74 	va_end(ap);
75 
76 	*sp = (long)resumecontext;		/* return address */
77 
78 	ucp->uc_mcontext.gregs[UESP] = (greg_t)sp;
79 }
80 
81 
82 static void
83 resumecontext(void)
84 {
85 	ucontext_t uc;
86 
87 	(void) getcontext(&uc);
88 	(void) setcontext(uc.uc_link);
89 }
90