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 /*
31 * Copyright 2023 Oxide Computer Company
32 */
33
34 #pragma weak _makecontext = makecontext
35
36 #include "lint.h"
37 #include <stdarg.h>
38 #include <ucontext.h>
39 #include <sys/stack.h>
40 #include <sys/auxv.h>
41 #include <errno.h>
42 #include "libc.h"
43
44 /*
45 * The ucontext_t that the user passes in must have been primed with a
46 * call to getcontext(2), have the uc_stack member set to reflect the
47 * stack which this context will use, and have the uc_link member set
48 * to the context which should be resumed when this context returns.
49 * When makecontext() returns, the ucontext_t will be set to run the
50 * given function with the given parameters on the stack specified by
51 * uc_stack, and which will return to the ucontext_t specified by uc_link.
52 */
53
54 static void resumecontext(void);
55
56 void
57 makecontext(ucontext_t *ucp, void (*func)(), int argc, ...)
58 {
59 long *sp;
60 long *tsp;
61 va_list ap;
62 size_t size;
63 int pusharg = (argc > 6 ? argc - 6 : 0);
64 greg_t tmp;
65 int i;
66
67 ucp->uc_mcontext.gregs[REG_PC] = (greg_t)func;
68
69 size = sizeof (long) * (pusharg + 1);
70
71 /*
72 * Calculate new value for %rsp. On entry to a function,
73 * %rsp must be STACK_ENTRY_ALIGNed but not STACK_ALIGNed.
74 * This is because the pushq %rbp will correct the alignment.
75 */
76
77 sp = (long *)(((uintptr_t)ucp->uc_stack.ss_sp +
78 ucp->uc_stack.ss_size - size) & ~(STACK_ENTRY_ALIGN - 1));
79
80 if (((uintptr_t)sp & (STACK_ALIGN - 1ul)) == 0)
81 sp -= STACK_ENTRY_ALIGN / sizeof (*sp);
82
83 tsp = sp + 1;
84
85 va_start(ap, argc);
86
87 for (i = 0; i < argc; i++) {
88 tmp = va_arg(ap, long);
89 switch (i) {
90 case 0:
91 ucp->uc_mcontext.gregs[REG_RDI] = tmp;
92 break;
93 case 1:
94 ucp->uc_mcontext.gregs[REG_RSI] = tmp;
95 break;
96 case 2:
97 ucp->uc_mcontext.gregs[REG_RDX] = tmp;
98 break;
99 case 3:
100 ucp->uc_mcontext.gregs[REG_RCX] = tmp;
101 break;
102 case 4:
103 ucp->uc_mcontext.gregs[REG_R8] = tmp;
104 break;
105 case 5:
106 ucp->uc_mcontext.gregs[REG_R9] = tmp;
107 break;
108 default:
109 *tsp++ = tmp;
110 break;
111 }
112 }
113
114 va_end(ap);
115
116 *sp = (long)resumecontext; /* return address */
117
118 ucp->uc_mcontext.gregs[REG_SP] = (greg_t)sp;
119 }
120
121
122 static void
123 resumecontext(void)
124 {
125 ucontext_t uc;
126
127 (void) getcontext(&uc);
128 (void) setcontext(uc.uc_link);
129 }
130
131 /*
132 * This is the ISA-specific allocation logic for allocating and setting up an
133 * extended ucontext_t. In particular, right now we need to allocate and add
134 * space for the UC_XSAVE member if we have the appropriate hardware support.
135 * The i386 / amd64 versions could be consolidated in a single x86 impl, but we
136 * don't have that right now.
137 */
138 ucontext_t *
139 ucontext_alloc(uint32_t flags)
140 {
141 boolean_t do_xsave = B_FALSE;
142 size_t to_alloc = sizeof (ucontext_t);
143 ucontext_t *ucp;
144
145 if (flags != 0) {
146 errno = EINVAL;
147 return (NULL);
148 }
149
150 /*
151 * This value isn't really 100% accurate. The xsave size is basically
152 * the worst case that we can have. The XMM / xsave structures aren't
153 * included in here, but are going to be enough to cover this. We can
154 * probably try to do a little better and should consider asking the
155 * kernel for something more accurate. In particular, the problem with
156 * this is that it doesn't account for the right size of future-looking
157 * dynamic things, but then again neither does rtld. We'll deal with
158 * this when we have support for the xfd MSR and actually use it. For
159 * more information see uts/intel/os/fpu.c's big theory statement.
160 */
161 switch (___getauxval(AT_SUN_FPTYPE)) {
162 case AT_386_FPINFO_XSAVE:
163 case AT_386_FPINFO_XSAVE_AMD:
164 do_xsave = B_TRUE;
165 to_alloc += ___getauxval(AT_SUN_FPSIZE);
166 break;
167 default:
168 break;
169 }
170
171 ucp = calloc(1, to_alloc);
172 if (ucp == NULL) {
173 return (NULL);
174 }
175
176 if (do_xsave) {
177 uintptr_t addr = (uintptr_t)ucp;
178 ucp->uc_xsave = addr + sizeof (ucontext_t);
179 }
180
181 return (ucp);
182 }
183
184 void
185 ucontext_free(ucontext_t *ucp)
186 {
187 free(ucp);
188 }