xref: /onnv-gate/usr/src/lib/libc/sparc/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
33*6812Sraf #pragma weak ___makecontext_v2 = __makecontext_v2
340Sstevel@tonic-gate 
35*6812Sraf #include "lint.h"
360Sstevel@tonic-gate #include <stdarg.h>
370Sstevel@tonic-gate #include <strings.h>
380Sstevel@tonic-gate #include <sys/ucontext.h>
390Sstevel@tonic-gate #include <sys/stack.h>
400Sstevel@tonic-gate #include <sys/frame.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate  * The ucontext_t that the user passes in must have been primed with a
440Sstevel@tonic-gate  * call to getcontext(2), have the uc_stack member set to reflect the
450Sstevel@tonic-gate  * stack which this context will use, and have the uc_link member set
460Sstevel@tonic-gate  * to the context which should be resumed when this context returns.
470Sstevel@tonic-gate  * When makecontext() returns, the ucontext_t will be set to run the
480Sstevel@tonic-gate  * given function with the given parameters on the stack specified by
490Sstevel@tonic-gate  * uc_stack, and which will return to the ucontext_t specified by uc_link.
500Sstevel@tonic-gate  */
510Sstevel@tonic-gate 
520Sstevel@tonic-gate static void resumecontext(void);
530Sstevel@tonic-gate 
540Sstevel@tonic-gate void
makecontext(ucontext_t * ucp,void (* func)(),int argc,...)55*6812Sraf makecontext(ucontext_t *ucp, void (*func)(), int argc, ...)
560Sstevel@tonic-gate {
570Sstevel@tonic-gate 	greg_t *reg;
580Sstevel@tonic-gate 	long *tsp;
590Sstevel@tonic-gate 	char *sp;
600Sstevel@tonic-gate 	int argno;
610Sstevel@tonic-gate 	va_list ap;
620Sstevel@tonic-gate 	size_t size;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	reg = ucp->uc_mcontext.gregs;
650Sstevel@tonic-gate 	reg[REG_PC] = (greg_t)func;
660Sstevel@tonic-gate 	reg[REG_nPC] = reg[REG_PC] + 0x4;
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	/*
690Sstevel@tonic-gate 	 * Reserve enough space for a frame and the arguments beyond the
700Sstevel@tonic-gate 	 * sixth; round to stack alignment.
710Sstevel@tonic-gate 	 */
720Sstevel@tonic-gate 	size = sizeof (struct frame);
730Sstevel@tonic-gate 	size += (argc > 6 ? argc - 6 : 0) * sizeof (long);
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	/*
760Sstevel@tonic-gate 	 * The legacy implemenation of makecontext() on sparc has been to
770Sstevel@tonic-gate 	 * interpret the uc_stack.ss_sp member incorrectly as the top of the
780Sstevel@tonic-gate 	 * stack rather than the base. We preserve this behavior here, but
790Sstevel@tonic-gate 	 * provide the correct semantics in __makecontext_v2().
800Sstevel@tonic-gate 	 */
810Sstevel@tonic-gate 	sp = (char *)(((uintptr_t)ucp->uc_stack.ss_sp - size) &
820Sstevel@tonic-gate 	    ~(STACK_ALIGN - 1));
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	/*
850Sstevel@tonic-gate 	 * Copy all args to the stack, and put the first 6 args into the
860Sstevel@tonic-gate 	 * ucontext_t. Zero the other fields of the frame.
870Sstevel@tonic-gate 	 */
880Sstevel@tonic-gate 	/* LINTED pointer cast may result in improper alignment */
890Sstevel@tonic-gate 	tsp = &((struct frame *)sp)->fr_argd[0];
900Sstevel@tonic-gate 	bzero(sp, sizeof (struct frame));
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	va_start(ap, argc);
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	for (argno = 0; argno < argc; argno++) {
950Sstevel@tonic-gate 		if (argno < 6)
960Sstevel@tonic-gate 			*tsp++ = reg[REG_O0 + argno] = va_arg(ap, long);
970Sstevel@tonic-gate 		else
980Sstevel@tonic-gate 			*tsp++ = va_arg(ap, long);
990Sstevel@tonic-gate 	}
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	va_end(ap);
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	reg[REG_SP] = (greg_t)sp - STACK_BIAS;		/* sp (when done) */
1040Sstevel@tonic-gate 	reg[REG_O7] = (greg_t)resumecontext - 8;	/* return pc */
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate void
__makecontext_v2(ucontext_t * ucp,void (* func)(),int argc,...)1080Sstevel@tonic-gate __makecontext_v2(ucontext_t *ucp, void (*func)(), int argc, ...)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate 	greg_t *reg;
1110Sstevel@tonic-gate 	long *tsp;
1120Sstevel@tonic-gate 	char *sp;
1130Sstevel@tonic-gate 	int argno;
1140Sstevel@tonic-gate 	va_list ap;
1150Sstevel@tonic-gate 	size_t size;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	reg = ucp->uc_mcontext.gregs;
1180Sstevel@tonic-gate 	reg[REG_PC] = (greg_t)func;
1190Sstevel@tonic-gate 	reg[REG_nPC] = reg[REG_PC] + 0x4;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	/*
1220Sstevel@tonic-gate 	 * Reserve enough space for a frame and the arguments beyond the
1230Sstevel@tonic-gate 	 * sixth; round to stack alignment.
1240Sstevel@tonic-gate 	 */
1250Sstevel@tonic-gate 	size = sizeof (struct frame);
1260Sstevel@tonic-gate 	size += (argc > 6 ? argc - 6 : 0) * sizeof (long);
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	sp = (char *)(((uintptr_t)ucp->uc_stack.ss_sp +
1290Sstevel@tonic-gate 	    ucp->uc_stack.ss_size - size) & ~(STACK_ALIGN - 1));
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	/*
1320Sstevel@tonic-gate 	 * Copy all args to the stack, and put the first 6 args into the
1330Sstevel@tonic-gate 	 * ucontext_t. Zero the other fields of the frame.
1340Sstevel@tonic-gate 	 */
1350Sstevel@tonic-gate 	/* LINTED pointer cast may result in improper alignment */
1360Sstevel@tonic-gate 	tsp = &((struct frame *)sp)->fr_argd[0];
1370Sstevel@tonic-gate 	bzero(sp, sizeof (struct frame));
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	va_start(ap, argc);
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	for (argno = 0; argno < argc; argno++) {
1420Sstevel@tonic-gate 		if (argno < 6)
1430Sstevel@tonic-gate 			*tsp++ = reg[REG_O0 + argno] = va_arg(ap, long);
1440Sstevel@tonic-gate 		else
1450Sstevel@tonic-gate 			*tsp++ = va_arg(ap, long);
1460Sstevel@tonic-gate 	}
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	va_end(ap);
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	reg[REG_SP] = (greg_t)sp - STACK_BIAS;		/* sp (when done) */
1510Sstevel@tonic-gate 	reg[REG_O7] = (greg_t)resumecontext - 8;	/* return pc */
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate static void
resumecontext(void)1550Sstevel@tonic-gate resumecontext(void)
1560Sstevel@tonic-gate {
1570Sstevel@tonic-gate 	/*
1580Sstevel@tonic-gate 	 * We can't include ucontext.h (where these functions are defined)
1590Sstevel@tonic-gate 	 * because it remaps the symbol makecontext.
1600Sstevel@tonic-gate 	 */
1610Sstevel@tonic-gate 	extern int getcontext(ucontext_t *);
1620Sstevel@tonic-gate 	extern int setcontext(const ucontext_t *);
1630Sstevel@tonic-gate 	ucontext_t uc;
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	(void) getcontext(&uc);
1660Sstevel@tonic-gate 	(void) setcontext(uc.uc_link);
1670Sstevel@tonic-gate }
168