Print this page
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/lib/libc/amd64/gen/makectxt.c
+++ new/usr/src/lib/libc/amd64/gen/makectxt.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 */
26 26
27 27 /* Copyright (c) 1988 AT&T */
28 28 /* All Rights Reserved */
29 29
30 30 /*
31 31 * Copyright 2023 Oxide Computer Company
32 32 */
33 33
34 34 #pragma weak _makecontext = makecontext
35 35
36 36 #include "lint.h"
37 37 #include <stdarg.h>
38 38 #include <ucontext.h>
39 39 #include <sys/stack.h>
40 40 #include <sys/auxv.h>
41 41 #include <errno.h>
42 42 #include "libc.h"
43 43
44 44 /*
45 45 * The ucontext_t that the user passes in must have been primed with a
46 46 * call to getcontext(2), have the uc_stack member set to reflect the
47 47 * stack which this context will use, and have the uc_link member set
48 48 * to the context which should be resumed when this context returns.
49 49 * When makecontext() returns, the ucontext_t will be set to run the
50 50 * given function with the given parameters on the stack specified by
51 51 * uc_stack, and which will return to the ucontext_t specified by uc_link.
52 52 */
53 53
54 54 static void resumecontext(void);
55 55
56 56 void
57 57 makecontext(ucontext_t *ucp, void (*func)(), int argc, ...)
58 58 {
59 59 long *sp;
60 60 long *tsp;
61 61 va_list ap;
62 62 size_t size;
63 63 int pusharg = (argc > 6 ? argc - 6 : 0);
64 64 greg_t tmp;
65 65 int i;
66 66
67 67 ucp->uc_mcontext.gregs[REG_PC] = (greg_t)func;
68 68
69 69 size = sizeof (long) * (pusharg + 1);
70 70
71 71 /*
72 72 * Calculate new value for %rsp. On entry to a function,
73 73 * %rsp must be STACK_ENTRY_ALIGNed but not STACK_ALIGNed.
74 74 * This is because the pushq %rbp will correct the alignment.
75 75 */
76 76
77 77 sp = (long *)(((uintptr_t)ucp->uc_stack.ss_sp +
78 78 ucp->uc_stack.ss_size - size) & ~(STACK_ENTRY_ALIGN - 1));
79 79
80 80 if (((uintptr_t)sp & (STACK_ALIGN - 1ul)) == 0)
81 81 sp -= STACK_ENTRY_ALIGN / sizeof (*sp);
82 82
83 83 tsp = sp + 1;
84 84
85 85 va_start(ap, argc);
86 86
87 87 for (i = 0; i < argc; i++) {
88 88 tmp = va_arg(ap, long);
89 89 switch (i) {
90 90 case 0:
91 91 ucp->uc_mcontext.gregs[REG_RDI] = tmp;
92 92 break;
93 93 case 1:
94 94 ucp->uc_mcontext.gregs[REG_RSI] = tmp;
95 95 break;
96 96 case 2:
97 97 ucp->uc_mcontext.gregs[REG_RDX] = tmp;
98 98 break;
99 99 case 3:
100 100 ucp->uc_mcontext.gregs[REG_RCX] = tmp;
101 101 break;
102 102 case 4:
103 103 ucp->uc_mcontext.gregs[REG_R8] = tmp;
104 104 break;
105 105 case 5:
106 106 ucp->uc_mcontext.gregs[REG_R9] = tmp;
107 107 break;
108 108 default:
109 109 *tsp++ = tmp;
110 110 break;
111 111 }
112 112 }
113 113
114 114 va_end(ap);
115 115
116 116 *sp = (long)resumecontext; /* return address */
117 117
118 118 ucp->uc_mcontext.gregs[REG_SP] = (greg_t)sp;
119 119 }
120 120
121 121
122 122 static void
123 123 resumecontext(void)
124 124 {
125 125 ucontext_t uc;
126 126
127 127 (void) getcontext(&uc);
128 128 (void) setcontext(uc.uc_link);
129 129 }
130 130
131 131 /*
132 132 * This is the ISA-specific allocation logic for allocating and setting up an
133 133 * extended ucontext_t. In particular, right now we need to allocate and add
134 134 * space for the UC_XSAVE member if we have the appropriate hardware support.
135 135 * The i386 / amd64 versions could be consolidated in a single x86 impl, but we
136 136 * don't have that right now.
137 137 */
138 138 ucontext_t *
139 139 ucontext_alloc(uint32_t flags)
140 140 {
141 141 boolean_t do_xsave = B_FALSE;
142 142 size_t to_alloc = sizeof (ucontext_t);
143 143 ucontext_t *ucp;
144 144
145 145 if (flags != 0) {
146 146 errno = EINVAL;
147 147 return (NULL);
148 148 }
149 149
150 150 /*
151 151 * This value isn't really 100% accurate. The xsave size is basically
152 152 * the worst case that we can have. The XMM / xsave structures aren't
153 153 * included in here, but are going to be enough to cover this. We can
154 154 * probably try to do a little better and should consider asking the
155 155 * kernel for something more accurate. In particular, the problem with
156 156 * this is that it doesn't account for the right size of future-looking
157 157 * dynamic things, but then again neither does rtld. We'll deal with
158 158 * this when we have support for the xfd MSR and actually use it. For
159 159 * more information see uts/intel/os/fpu.c's big theory statement.
160 160 */
161 161 switch (___getauxval(AT_SUN_FPTYPE)) {
162 162 case AT_386_FPINFO_XSAVE:
163 163 case AT_386_FPINFO_XSAVE_AMD:
164 164 do_xsave = B_TRUE;
165 165 to_alloc += ___getauxval(AT_SUN_FPSIZE);
166 166 break;
167 167 default:
168 168 break;
169 169 }
170 170
171 171 ucp = calloc(1, to_alloc);
172 172 if (ucp == NULL) {
173 173 return (NULL);
174 174 }
175 175
176 176 if (do_xsave) {
177 177 uintptr_t addr = (uintptr_t)ucp;
178 178 ucp->uc_xsave = addr + sizeof (ucontext_t);
179 179 }
180 180
181 181 return (ucp);
182 182 }
183 183
184 184 void
185 185 ucontext_free(ucontext_t *ucp)
186 186 {
187 187 free(ucp);
188 188 }
|
↓ open down ↓ |
188 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX