xref: /illumos-gate/usr/src/lib/libc/i386/gen/makectxt.c (revision 21227944c2bcc086121a5428f3f9d2496ba646f5)
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 weak _makecontext = makecontext
31 
32 #include "lint.h"
33 #include <stdarg.h>
34 #include <ucontext.h>
35 #include <sys/regset.h>
36 #include <sys/stack.h>
37 
38 /*
39  * The ucontext_t that the user passes in must have been primed with a
40  * call to getcontext(2), have the uc_stack member set to reflect the
41  * stack which this context will use, and have the uc_link member set
42  * to the context which should be resumed when this context returns.
43  * When makecontext() returns, the ucontext_t will be set to run the
44  * given function with the given parameters on the stack specified by
45  * uc_stack, and which will return to the ucontext_t specified by uc_link.
46  */
47 
48 static void resumecontext(void);
49 
50 void
51 makecontext(ucontext_t *ucp, void (*func)(), int argc, ...)
52 {
53 	long *sp;
54 	long *tsp;
55 	va_list ap;
56 	size_t size;
57 
58 	ucp->uc_mcontext.gregs[EIP] = (greg_t)func;
59 
60 	size = sizeof (long) * (argc + 1);
61 
62 	sp = (long *)(((uintptr_t)ucp->uc_stack.ss_sp +
63 	    ucp->uc_stack.ss_size - size) & ~(STACK_ALIGN - 1));
64 
65 	tsp = sp + 1;
66 
67 	va_start(ap, argc);
68 
69 	while (argc-- > 0) {
70 		*tsp++ = va_arg(ap, long);
71 	}
72 
73 	va_end(ap);
74 
75 	*sp = (long)resumecontext;		/* return address */
76 
77 	ucp->uc_mcontext.gregs[UESP] = (greg_t)sp;
78 }
79 
80 
81 static void
82 resumecontext(void)
83 {
84 	ucontext_t uc;
85 
86 	(void) getcontext(&uc);
87 	(void) setcontext(uc.uc_link);
88 }
89