xref: /netbsd-src/lib/libc/arch/i386/gen/makecontext.c (revision ce099b40997c43048fb78bd578195f81d2456523)
1*ce099b40Smartin /*	$NetBSD: makecontext.c,v 1.4 2008/04/28 20:22:56 martin Exp $	*/
23fdac2b8Sthorpej 
33fdac2b8Sthorpej /*-
43fdac2b8Sthorpej  * Copyright (c) 1999 The NetBSD Foundation, Inc.
53fdac2b8Sthorpej  * All rights reserved.
63fdac2b8Sthorpej  *
73fdac2b8Sthorpej  * This code is derived from software contributed to The NetBSD Foundation
83fdac2b8Sthorpej  * by Klaus Klein.
93fdac2b8Sthorpej  *
103fdac2b8Sthorpej  * Redistribution and use in source and binary forms, with or without
113fdac2b8Sthorpej  * modification, are permitted provided that the following conditions
123fdac2b8Sthorpej  * are met:
133fdac2b8Sthorpej  * 1. Redistributions of source code must retain the above copyright
143fdac2b8Sthorpej  *    notice, this list of conditions and the following disclaimer.
153fdac2b8Sthorpej  * 2. Redistributions in binary form must reproduce the above copyright
163fdac2b8Sthorpej  *    notice, this list of conditions and the following disclaimer in the
173fdac2b8Sthorpej  *    documentation and/or other materials provided with the distribution.
183fdac2b8Sthorpej  *
193fdac2b8Sthorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
203fdac2b8Sthorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
213fdac2b8Sthorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
223fdac2b8Sthorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
233fdac2b8Sthorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
243fdac2b8Sthorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
253fdac2b8Sthorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
263fdac2b8Sthorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
273fdac2b8Sthorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
283fdac2b8Sthorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
293fdac2b8Sthorpej  * POSSIBILITY OF SUCH DAMAGE.
303fdac2b8Sthorpej  */
313fdac2b8Sthorpej 
323fdac2b8Sthorpej #include <sys/cdefs.h>
333fdac2b8Sthorpej #if defined(LIBC_SCCS) && !defined(lint)
34*ce099b40Smartin __RCSID("$NetBSD: makecontext.c,v 1.4 2008/04/28 20:22:56 martin Exp $");
353fdac2b8Sthorpej #endif
363fdac2b8Sthorpej 
373fdac2b8Sthorpej #include <inttypes.h>
383fdac2b8Sthorpej #include <stddef.h>
393fdac2b8Sthorpej #include <ucontext.h>
403fdac2b8Sthorpej #include "extern.h"
413fdac2b8Sthorpej 
423fdac2b8Sthorpej #if __STDC__
433fdac2b8Sthorpej #include <stdarg.h>
443fdac2b8Sthorpej #else
453fdac2b8Sthorpej #include <varargs.h>
463fdac2b8Sthorpej #endif
473fdac2b8Sthorpej 
483fdac2b8Sthorpej 
493fdac2b8Sthorpej void
503fdac2b8Sthorpej #if __STDC__
makecontext(ucontext_t * ucp,void (* func)(void),int argc,...)513fdac2b8Sthorpej makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...)
523fdac2b8Sthorpej #else
533fdac2b8Sthorpej makecontext(ucp, func, argc, va_alist)
543fdac2b8Sthorpej 	ucontext_t *ucp;
553fdac2b8Sthorpej 	void (*func)();
563fdac2b8Sthorpej 	int argc;
573fdac2b8Sthorpej 	va_dcl
583fdac2b8Sthorpej #endif
593fdac2b8Sthorpej {
603fdac2b8Sthorpej 	__greg_t *gr = ucp->uc_mcontext.__gregs;
613fdac2b8Sthorpej 	unsigned int *sp;
623fdac2b8Sthorpej 	va_list ap;
633fdac2b8Sthorpej 
643fdac2b8Sthorpej 	/* LINTED __greg_t is safe */
653fdac2b8Sthorpej 	gr[_REG_EIP] = (__greg_t)func;
663fdac2b8Sthorpej 
673fdac2b8Sthorpej 	/* LINTED uintptr_t is safe */
68fd075455Smrg 	sp  = (unsigned int *)((uintptr_t)ucp->uc_stack.ss_sp +
69fd075455Smrg 	    ucp->uc_stack.ss_size);
70fd075455Smrg 	/* Align on word boundary. */
713fdac2b8Sthorpej 	/* LINTED uintptr_t is safe */
72fd075455Smrg 	sp  = (unsigned int *)((uintptr_t)sp & ~0x3);
733fdac2b8Sthorpej 	sp -= argc + 1;			/* Make room for ret and args. */
743fdac2b8Sthorpej 	/* LINTED __greg_t is safe */
753fdac2b8Sthorpej 	gr[_REG_UESP] = (__greg_t)sp;
763fdac2b8Sthorpej 	gr[_REG_EBP] = (__greg_t)0;	/* Wipe out frame pointer. */
773fdac2b8Sthorpej 
783fdac2b8Sthorpej 	/* Put return address on top of stack. */
793fdac2b8Sthorpej 	/* LINTED uintptr_t is safe */
803fdac2b8Sthorpej 	*sp++ = (uintptr_t)_resumecontext;
813fdac2b8Sthorpej 
823fdac2b8Sthorpej 	/* Construct argument list. */
833fdac2b8Sthorpej #if __STDC__
843fdac2b8Sthorpej 	va_start(ap, argc);
853fdac2b8Sthorpej #else
863fdac2b8Sthorpej 	va_start(ap);
873fdac2b8Sthorpej #endif
883fdac2b8Sthorpej 	while (argc-- > 0) {
893fdac2b8Sthorpej 		/* LINTED uintptr_t is safe */
903fdac2b8Sthorpej 		*sp++ = va_arg(ap, uintptr_t);
913fdac2b8Sthorpej 	}
923fdac2b8Sthorpej 	va_end(ap);
933fdac2b8Sthorpej }
94