xref: /onnv-gate/usr/src/lib/libc/i386/gen/makectxt.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*6812Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
32*6812Sraf #pragma weak _makecontext = makecontext
330Sstevel@tonic-gate 
34*6812Sraf #include "lint.h"
350Sstevel@tonic-gate #include <stdarg.h>
360Sstevel@tonic-gate #include <ucontext.h>
370Sstevel@tonic-gate #include <sys/stack.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate /*
400Sstevel@tonic-gate  * The ucontext_t that the user passes in must have been primed with a
410Sstevel@tonic-gate  * call to getcontext(2), have the uc_stack member set to reflect the
420Sstevel@tonic-gate  * stack which this context will use, and have the uc_link member set
430Sstevel@tonic-gate  * to the context which should be resumed when this context returns.
440Sstevel@tonic-gate  * When makecontext() returns, the ucontext_t will be set to run the
450Sstevel@tonic-gate  * given function with the given parameters on the stack specified by
460Sstevel@tonic-gate  * uc_stack, and which will return to the ucontext_t specified by uc_link.
470Sstevel@tonic-gate  */
480Sstevel@tonic-gate 
490Sstevel@tonic-gate static void resumecontext(void);
500Sstevel@tonic-gate 
510Sstevel@tonic-gate void
makecontext(ucontext_t * ucp,void (* func)(),int argc,...)520Sstevel@tonic-gate makecontext(ucontext_t *ucp, void (*func)(), int argc, ...)
530Sstevel@tonic-gate {
540Sstevel@tonic-gate 	long *sp;
550Sstevel@tonic-gate 	long *tsp;
560Sstevel@tonic-gate 	va_list ap;
570Sstevel@tonic-gate 	size_t size;
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EIP] = (greg_t)func;
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	size = sizeof (long) * (argc + 1);
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	sp = (long *)(((uintptr_t)ucp->uc_stack.ss_sp +
640Sstevel@tonic-gate 	    ucp->uc_stack.ss_size - size) & ~(STACK_ALIGN - 1));
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	tsp = sp + 1;
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	va_start(ap, argc);
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	while (argc-- > 0) {
710Sstevel@tonic-gate 		*tsp++ = va_arg(ap, long);
720Sstevel@tonic-gate 	}
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 	va_end(ap);
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	*sp = (long)resumecontext;		/* return address */
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[UESP] = (greg_t)sp;
790Sstevel@tonic-gate }
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 
820Sstevel@tonic-gate static void
resumecontext(void)830Sstevel@tonic-gate resumecontext(void)
840Sstevel@tonic-gate {
850Sstevel@tonic-gate 	ucontext_t uc;
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	(void) getcontext(&uc);
880Sstevel@tonic-gate 	(void) setcontext(uc.uc_link);
890Sstevel@tonic-gate }
90