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