xref: /titanic_52/usr/src/lib/libc/amd64/gen/makectxt.c (revision 7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fe)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #pragma weak makecontext = _makecontext
34 
35 #include "synonyms.h"
36 #include <stdarg.h>
37 #include <ucontext.h>
38 #include <sys/stack.h>
39 
40 /*
41  * The ucontext_t that the user passes in must have been primed with a
42  * call to getcontext(2), have the uc_stack member set to reflect the
43  * stack which this context will use, and have the uc_link member set
44  * to the context which should be resumed when this context returns.
45  * When makecontext() returns, the ucontext_t will be set to run the
46  * given function with the given parameters on the stack specified by
47  * uc_stack, and which will return to the ucontext_t specified by uc_link.
48  */
49 
50 static void resumecontext(void);
51 
52 void
53 makecontext(ucontext_t *ucp, void (*func)(), int argc, ...)
54 {
55 	long *sp;
56 	long *tsp;
57 	va_list ap;
58 	size_t size;
59 	int pusharg = (argc > 6 ? argc - 6 : 0);
60 	greg_t tmp;
61 	int i;
62 
63 	ucp->uc_mcontext.gregs[REG_PC] = (greg_t)func;
64 
65 	size = sizeof (long) * (pusharg + 1);
66 
67 	/*
68 	 * Calculate new value for %rsp. On entry to a function,
69 	 * %rsp must be STACK_ENTRY_ALIGNed but not STACK_ALIGNed.
70 	 * This is because the pushq %rbp will correct the alignment.
71 	 */
72 
73 	sp = (long *)(((uintptr_t)ucp->uc_stack.ss_sp +
74 	    ucp->uc_stack.ss_size - size) & ~(STACK_ENTRY_ALIGN - 1));
75 
76 	if (((uintptr_t)sp & (STACK_ALIGN - 1ul)) == 0)
77 		sp -= STACK_ENTRY_ALIGN / sizeof (*sp);
78 
79 	tsp = sp + 1;
80 
81 	va_start(ap, argc);
82 
83 	for (i = 0; i < argc; i++) {
84 		tmp = va_arg(ap, long);
85 		switch (i) {
86 		case 0:
87 			ucp->uc_mcontext.gregs[REG_RDI] = tmp;
88 			break;
89 		case 1:
90 			ucp->uc_mcontext.gregs[REG_RSI] = tmp;
91 			break;
92 		case 2:
93 			ucp->uc_mcontext.gregs[REG_RDX] = tmp;
94 			break;
95 		case 3:
96 			ucp->uc_mcontext.gregs[REG_RCX] = tmp;
97 			break;
98 		case 4:
99 			ucp->uc_mcontext.gregs[REG_R8] = tmp;
100 			break;
101 		case 5:
102 			ucp->uc_mcontext.gregs[REG_R9] = tmp;
103 			break;
104 		default:
105 			*tsp++ = tmp;
106 			break;
107 		}
108 	}
109 
110 	va_end(ap);
111 
112 	*sp = (long)resumecontext;		/* return address */
113 
114 	ucp->uc_mcontext.gregs[REG_SP] = (greg_t)sp;
115 }
116 
117 
118 static void
119 resumecontext(void)
120 {
121 	ucontext_t uc;
122 
123 	getcontext(&uc);
124 	setcontext(uc.uc_link);
125 }
126