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
300Sstevel@tonic-gate #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 int pusharg = (argc > 6 ? argc - 6 : 0);
590Sstevel@tonic-gate greg_t tmp;
600Sstevel@tonic-gate int i;
610Sstevel@tonic-gate
620Sstevel@tonic-gate ucp->uc_mcontext.gregs[REG_PC] = (greg_t)func;
630Sstevel@tonic-gate
640Sstevel@tonic-gate size = sizeof (long) * (pusharg + 1);
650Sstevel@tonic-gate
660Sstevel@tonic-gate /*
670Sstevel@tonic-gate * Calculate new value for %rsp. On entry to a function,
680Sstevel@tonic-gate * %rsp must be STACK_ENTRY_ALIGNed but not STACK_ALIGNed.
690Sstevel@tonic-gate * This is because the pushq %rbp will correct the alignment.
700Sstevel@tonic-gate */
710Sstevel@tonic-gate
720Sstevel@tonic-gate sp = (long *)(((uintptr_t)ucp->uc_stack.ss_sp +
730Sstevel@tonic-gate ucp->uc_stack.ss_size - size) & ~(STACK_ENTRY_ALIGN - 1));
740Sstevel@tonic-gate
750Sstevel@tonic-gate if (((uintptr_t)sp & (STACK_ALIGN - 1ul)) == 0)
760Sstevel@tonic-gate sp -= STACK_ENTRY_ALIGN / sizeof (*sp);
770Sstevel@tonic-gate
780Sstevel@tonic-gate tsp = sp + 1;
790Sstevel@tonic-gate
800Sstevel@tonic-gate va_start(ap, argc);
810Sstevel@tonic-gate
820Sstevel@tonic-gate for (i = 0; i < argc; i++) {
830Sstevel@tonic-gate tmp = va_arg(ap, long);
840Sstevel@tonic-gate switch (i) {
850Sstevel@tonic-gate case 0:
860Sstevel@tonic-gate ucp->uc_mcontext.gregs[REG_RDI] = tmp;
870Sstevel@tonic-gate break;
880Sstevel@tonic-gate case 1:
890Sstevel@tonic-gate ucp->uc_mcontext.gregs[REG_RSI] = tmp;
900Sstevel@tonic-gate break;
910Sstevel@tonic-gate case 2:
920Sstevel@tonic-gate ucp->uc_mcontext.gregs[REG_RDX] = tmp;
930Sstevel@tonic-gate break;
940Sstevel@tonic-gate case 3:
950Sstevel@tonic-gate ucp->uc_mcontext.gregs[REG_RCX] = tmp;
960Sstevel@tonic-gate break;
970Sstevel@tonic-gate case 4:
980Sstevel@tonic-gate ucp->uc_mcontext.gregs[REG_R8] = tmp;
990Sstevel@tonic-gate break;
1000Sstevel@tonic-gate case 5:
1010Sstevel@tonic-gate ucp->uc_mcontext.gregs[REG_R9] = tmp;
1020Sstevel@tonic-gate break;
1030Sstevel@tonic-gate default:
1040Sstevel@tonic-gate *tsp++ = tmp;
1050Sstevel@tonic-gate break;
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate va_end(ap);
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate *sp = (long)resumecontext; /* return address */
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate ucp->uc_mcontext.gregs[REG_SP] = (greg_t)sp;
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate static void
resumecontext(void)1180Sstevel@tonic-gate resumecontext(void)
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate ucontext_t uc;
1210Sstevel@tonic-gate
122*6812Sraf (void) getcontext(&uc);
123*6812Sraf (void) setcontext(uc.uc_link);
1240Sstevel@tonic-gate }
125