xref: /titanic_52/usr/src/lib/libc/sparcv9/gen/makectxt.c (revision bc0e91320069f0bcaee43e80a7ea686d9efa2d08)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57257d1b4Sraf  * Common Development and Distribution License (the "License").
67257d1b4Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217257d1b4Sraf 
227c478bd9Sstevel@tonic-gate /*
237257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307257d1b4Sraf #pragma weak _makecontext = makecontext
317257d1b4Sraf #pragma weak ___makecontext_v2 = __makecontext_v2
327c478bd9Sstevel@tonic-gate 
337257d1b4Sraf #include "lint.h"
347c478bd9Sstevel@tonic-gate #include <stdarg.h>
357c478bd9Sstevel@tonic-gate #include <strings.h>
36*bc0e9132SGordon Ross /* Can't just use <ucontext.h> due to redefine_extname stuff. */
377c478bd9Sstevel@tonic-gate #include <sys/ucontext.h>
38*bc0e9132SGordon Ross #include <sys/regset.h>
397c478bd9Sstevel@tonic-gate #include <sys/stack.h>
407c478bd9Sstevel@tonic-gate #include <sys/frame.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * The ucontext_t that the user passes in must have been primed with a
447c478bd9Sstevel@tonic-gate  * call to getcontext(2), have the uc_stack member set to reflect the
457c478bd9Sstevel@tonic-gate  * stack which this context will use, and have the uc_link member set
467c478bd9Sstevel@tonic-gate  * to the context which should be resumed when this context returns.
477c478bd9Sstevel@tonic-gate  * When makecontext() returns, the ucontext_t will be set to run the
487c478bd9Sstevel@tonic-gate  * given function with the given parameters on the stack specified by
497c478bd9Sstevel@tonic-gate  * uc_stack, and which will return to the ucontext_t specified by uc_link.
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate static void resumecontext(void);
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate void
557257d1b4Sraf makecontext(ucontext_t *ucp, void (*func)(), int argc, ...)
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate 	greg_t *reg;
587c478bd9Sstevel@tonic-gate 	long *tsp;
597c478bd9Sstevel@tonic-gate 	char *sp;
607c478bd9Sstevel@tonic-gate 	int argno;
617c478bd9Sstevel@tonic-gate 	va_list ap;
627c478bd9Sstevel@tonic-gate 	size_t size;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	reg = ucp->uc_mcontext.gregs;
657c478bd9Sstevel@tonic-gate 	reg[REG_PC] = (greg_t)func;
667c478bd9Sstevel@tonic-gate 	reg[REG_nPC] = reg[REG_PC] + 0x4;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	/*
697c478bd9Sstevel@tonic-gate 	 * Reserve enough space for a frame and the arguments beyond the
707c478bd9Sstevel@tonic-gate 	 * sixth; round to stack alignment.
717c478bd9Sstevel@tonic-gate 	 */
727c478bd9Sstevel@tonic-gate 	size = sizeof (struct frame);
737c478bd9Sstevel@tonic-gate 	size += (argc > 6 ? argc - 6 : 0) * sizeof (long);
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	/*
767c478bd9Sstevel@tonic-gate 	 * The legacy implemenation of makecontext() on sparc has been to
777c478bd9Sstevel@tonic-gate 	 * interpret the uc_stack.ss_sp member incorrectly as the top of the
787c478bd9Sstevel@tonic-gate 	 * stack rather than the base. We preserve this behavior here, but
797c478bd9Sstevel@tonic-gate 	 * provide the correct semantics in __makecontext_v2().
807c478bd9Sstevel@tonic-gate 	 */
817c478bd9Sstevel@tonic-gate 	sp = (char *)(((uintptr_t)ucp->uc_stack.ss_sp - size) &
827c478bd9Sstevel@tonic-gate 	    ~(STACK_ALIGN - 1));
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	/*
857c478bd9Sstevel@tonic-gate 	 * Copy all args to the stack, and put the first 6 args into the
867c478bd9Sstevel@tonic-gate 	 * ucontext_t. Zero the other fields of the frame.
877c478bd9Sstevel@tonic-gate 	 */
887c478bd9Sstevel@tonic-gate 	/* LINTED pointer cast may result in improper alignment */
897c478bd9Sstevel@tonic-gate 	tsp = &((struct frame *)sp)->fr_argd[0];
907c478bd9Sstevel@tonic-gate 	bzero(sp, sizeof (struct frame));
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	va_start(ap, argc);
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	for (argno = 0; argno < argc; argno++) {
957c478bd9Sstevel@tonic-gate 		if (argno < 6)
967c478bd9Sstevel@tonic-gate 			*tsp++ = reg[REG_O0 + argno] = va_arg(ap, long);
977c478bd9Sstevel@tonic-gate 		else
987c478bd9Sstevel@tonic-gate 			*tsp++ = va_arg(ap, long);
997c478bd9Sstevel@tonic-gate 	}
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	va_end(ap);
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	reg[REG_SP] = (greg_t)sp - STACK_BIAS;		/* sp (when done) */
1047c478bd9Sstevel@tonic-gate 	reg[REG_O7] = (greg_t)resumecontext - 8;	/* return pc */
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate void
1087c478bd9Sstevel@tonic-gate __makecontext_v2(ucontext_t *ucp, void (*func)(), int argc, ...)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	greg_t *reg;
1117c478bd9Sstevel@tonic-gate 	long *tsp;
1127c478bd9Sstevel@tonic-gate 	char *sp;
1137c478bd9Sstevel@tonic-gate 	int argno;
1147c478bd9Sstevel@tonic-gate 	va_list ap;
1157c478bd9Sstevel@tonic-gate 	size_t size;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	reg = ucp->uc_mcontext.gregs;
1187c478bd9Sstevel@tonic-gate 	reg[REG_PC] = (greg_t)func;
1197c478bd9Sstevel@tonic-gate 	reg[REG_nPC] = reg[REG_PC] + 0x4;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	/*
1227c478bd9Sstevel@tonic-gate 	 * Reserve enough space for a frame and the arguments beyond the
1237c478bd9Sstevel@tonic-gate 	 * sixth; round to stack alignment.
1247c478bd9Sstevel@tonic-gate 	 */
1257c478bd9Sstevel@tonic-gate 	size = sizeof (struct frame);
1267c478bd9Sstevel@tonic-gate 	size += (argc > 6 ? argc - 6 : 0) * sizeof (long);
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	sp = (char *)(((uintptr_t)ucp->uc_stack.ss_sp +
1297c478bd9Sstevel@tonic-gate 	    ucp->uc_stack.ss_size - size) & ~(STACK_ALIGN - 1));
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	/*
1327c478bd9Sstevel@tonic-gate 	 * Copy all args to the stack, and put the first 6 args into the
1337c478bd9Sstevel@tonic-gate 	 * ucontext_t. Zero the other fields of the frame.
1347c478bd9Sstevel@tonic-gate 	 */
1357c478bd9Sstevel@tonic-gate 	/* LINTED pointer cast may result in improper alignment */
1367c478bd9Sstevel@tonic-gate 	tsp = &((struct frame *)sp)->fr_argd[0];
1377c478bd9Sstevel@tonic-gate 	bzero(sp, sizeof (struct frame));
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	va_start(ap, argc);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	for (argno = 0; argno < argc; argno++) {
1427c478bd9Sstevel@tonic-gate 		if (argno < 6)
1437c478bd9Sstevel@tonic-gate 			*tsp++ = reg[REG_O0 + argno] = va_arg(ap, long);
1447c478bd9Sstevel@tonic-gate 		else
1457c478bd9Sstevel@tonic-gate 			*tsp++ = va_arg(ap, long);
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	va_end(ap);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	reg[REG_SP] = (greg_t)sp - STACK_BIAS;		/* sp (when done) */
1517c478bd9Sstevel@tonic-gate 	reg[REG_O7] = (greg_t)resumecontext - 8;	/* return pc */
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate static void
1557c478bd9Sstevel@tonic-gate resumecontext(void)
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate 	/*
1587c478bd9Sstevel@tonic-gate 	 * We can't include ucontext.h (where these functions are defined)
1597c478bd9Sstevel@tonic-gate 	 * because it remaps the symbol makecontext.
1607c478bd9Sstevel@tonic-gate 	 */
1617c478bd9Sstevel@tonic-gate 	extern int getcontext(ucontext_t *);
1627c478bd9Sstevel@tonic-gate 	extern int setcontext(const ucontext_t *);
1637c478bd9Sstevel@tonic-gate 	ucontext_t uc;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	(void) getcontext(&uc);
1667c478bd9Sstevel@tonic-gate 	(void) setcontext(uc.uc_link);
1677c478bd9Sstevel@tonic-gate }
168