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