Print this page
6507 i386 makecontext(3c) needs to 16-byte align the stack
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libc/i386/threads/machdep.c
+++ new/usr/src/lib/libc/i386/threads/machdep.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
↓ open down ↓ |
21 lines elided |
↑ open up ↑ |
22 22 /*
23 23 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 */
25 25
26 26 #include "thr_uberdata.h"
27 27 #include <procfs.h>
28 28 #include <ucontext.h>
29 29 #include <setjmp.h>
30 30
31 31 /*
32 - * The i386 ABI says that the stack pointer need be only 4-byte aligned
33 - * before a function call (STACK_ALIGN == 4). We use a 16-byte stack
34 - * alignment for the benefit of floating point code compiled using sse2.
35 - * Even though the i386 ABI doesn't require it, both cc and gcc
36 - * assume this alignment on entry to a function and maintain it
37 - * for calls made from that function. If the stack is initially
38 - * aligned on a 16-byte boundary, it will continue to be so aligned.
39 - * If it is not initially so aligned, it will never become so aligned.
32 + * The stack needs to be 16-byte aligned with a 4-byte bias. See comment in
33 + * lib/libc/i386/gen/makectxt.c.
34 + *
35 + * Note: If you change it, you need to change it in the following files as
36 + * well:
37 + *
38 + * - lib/libc/i386/gen/makectxt.c
39 + * - lib/common/i386/crti.s
40 + * - lib/common/i386/crt1.s
40 41 */
41 42 #undef STACK_ALIGN
42 43 #define STACK_ALIGN 16
43 44
44 45 extern int getlwpstatus(thread_t, lwpstatus_t *);
45 46 extern int putlwpregs(thread_t, prgregset_t);
46 47
47 48 void *
48 49 setup_top_frame(void *stk, size_t stksize, ulwp_t *ulwp)
49 50 {
50 51 uint32_t *stack;
51 52 struct {
52 53 uint32_t rpc;
53 54 uint32_t arg;
54 55 uint32_t pad;
55 56 uint32_t fp;
56 57 uint32_t pc;
57 58 } frame;
58 59
59 60 /*
60 61 * Top-of-stack must be rounded down to STACK_ALIGN and
61 62 * there must be a minimum frame. Note: 'frame' is not a true
62 63 * stack frame (see <sys/frame.h>) but a construction made here to
63 64 * make it look like _lwp_start called the thread start function
64 65 * with a 16-byte aligned stack pointer (the address of frame.arg
65 66 * is the address that muet be aligned on a 16-byte boundary).
66 67 */
67 68 stack = (uint32_t *)(((uintptr_t)stk + stksize) & ~(STACK_ALIGN-1));
68 69
69 70 /*
70 71 * This will return NULL if the kernel cannot allocate
71 72 * a page for the top page of the stack. This will cause
72 73 * thr_create(), pthread_create() or pthread_attr_setstack()
73 74 * to fail, passing the problem up to the application.
74 75 */
75 76 stack -= 5; /* make the address of frame.arg be 16-byte aligned */
76 77 frame.pc = 0;
77 78 frame.fp = 0; /* initial address for %ebp (see EBP below) */
78 79 frame.pad = 0;
79 80 frame.arg = (uint32_t)ulwp;
80 81 frame.rpc = (uint32_t)_lwp_start;
81 82 if (uucopy(&frame, (void *)stack, sizeof (frame)) == 0)
82 83 return (stack);
83 84 return (NULL);
84 85 }
85 86
86 87 int
87 88 setup_context(ucontext_t *ucp, void *(*func)(ulwp_t *),
88 89 ulwp_t *ulwp, caddr_t stk, size_t stksize)
89 90 {
90 91 static int initialized;
91 92 static greg_t fs, es, ds, cs, ss;
92 93
93 94 uint32_t *stack;
94 95
95 96 if (!initialized) {
96 97 ucontext_t uc;
97 98
98 99 /* do this once to load the segment registers */
99 100 uc.uc_flags = UC_CPU;
100 101 (void) __getcontext(&uc);
101 102 fs = uc.uc_mcontext.gregs[FS];
102 103 es = uc.uc_mcontext.gregs[ES];
103 104 ds = uc.uc_mcontext.gregs[DS];
104 105 cs = uc.uc_mcontext.gregs[CS];
105 106 ss = uc.uc_mcontext.gregs[SS];
106 107 initialized = 1;
107 108 }
108 109 /* clear the context and set the segment registers */
109 110 (void) memset(ucp, 0, sizeof (*ucp));
110 111 ucp->uc_mcontext.gregs[FS] = fs;
111 112 ucp->uc_mcontext.gregs[ES] = es;
112 113 ucp->uc_mcontext.gregs[DS] = ds;
113 114 ucp->uc_mcontext.gregs[CS] = cs;
114 115 ucp->uc_mcontext.gregs[SS] = ss;
115 116
116 117 /*
117 118 * Yuck.
118 119 * Use unused kernel pointer field in ucontext
119 120 * to pass down self pointer and set %gs selector
120 121 * value so __lwp_create() can setup %gs atomically.
121 122 * Without this we would need to block all signals
122 123 * and directly call ___lwp_private() in _thrp_setup
123 124 * on the other side of __lwp_create().
124 125 */
125 126 ucp->uc_mcontext.gregs[ESP] = (greg_t)ulwp;
126 127 ucp->uc_mcontext.gregs[GS] = (greg_t)LWPGS_SEL;
127 128
128 129 /*
129 130 * Setup the top stack frame.
130 131 * If this fails, pass the problem up to the application.
131 132 */
132 133 if ((stack = setup_top_frame(stk, stksize, ulwp)) == NULL)
133 134 return (ENOMEM);
134 135
135 136 /* fill in registers of interest */
136 137 ucp->uc_flags |= UC_CPU;
137 138 ucp->uc_mcontext.gregs[EIP] = (greg_t)func;
138 139 ucp->uc_mcontext.gregs[UESP] = (greg_t)stack;
139 140 ucp->uc_mcontext.gregs[EBP] = (greg_t)(stack + 3);
140 141
141 142 return (0);
142 143 }
143 144
144 145 /*
145 146 * Machine-dependent startup code for a newly-created thread.
146 147 */
147 148 void *
148 149 _thrp_setup(ulwp_t *self)
149 150 {
150 151 self->ul_ustack.ss_sp = (void *)(self->ul_stktop - self->ul_stksiz);
151 152 self->ul_ustack.ss_size = self->ul_stksiz;
152 153 self->ul_ustack.ss_flags = 0;
153 154 (void) setustack(&self->ul_ustack);
154 155
155 156 update_sched(self);
156 157 tls_setup();
157 158
158 159 /* signals have been deferred until now */
159 160 sigon(self);
160 161
161 162 if (self->ul_cancel_pending == 2 && !self->ul_cancel_disabled)
162 163 return (NULL); /* cancelled by pthread_create() */
163 164 return (self->ul_startpc(self->ul_startarg));
164 165 }
165 166
166 167 void
167 168 _fpinherit(ulwp_t *ulwp)
168 169 {
169 170 ulwp->ul_fpuenv.ftag = 0xffffffff;
170 171 }
171 172
172 173 void
173 174 getgregs(ulwp_t *ulwp, gregset_t rs)
174 175 {
175 176 lwpstatus_t status;
176 177
177 178 if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
178 179 rs[EIP] = status.pr_reg[EIP];
179 180 rs[EDI] = status.pr_reg[EDI];
180 181 rs[ESI] = status.pr_reg[ESI];
181 182 rs[EBP] = status.pr_reg[EBP];
182 183 rs[EBX] = status.pr_reg[EBX];
183 184 rs[UESP] = status.pr_reg[UESP];
184 185 } else {
185 186 rs[EIP] = 0;
186 187 rs[EDI] = 0;
187 188 rs[ESI] = 0;
188 189 rs[EBP] = 0;
189 190 rs[EBX] = 0;
190 191 rs[UESP] = 0;
191 192 }
192 193 }
193 194
194 195 void
195 196 setgregs(ulwp_t *ulwp, gregset_t rs)
196 197 {
197 198 lwpstatus_t status;
198 199
199 200 if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
200 201 status.pr_reg[EIP] = rs[EIP];
201 202 status.pr_reg[EDI] = rs[EDI];
202 203 status.pr_reg[ESI] = rs[ESI];
203 204 status.pr_reg[EBP] = rs[EBP];
204 205 status.pr_reg[EBX] = rs[EBX];
205 206 status.pr_reg[UESP] = rs[UESP];
206 207 (void) putlwpregs(ulwp->ul_lwpid, status.pr_reg);
207 208 }
208 209 }
209 210
210 211 int
211 212 __csigsetjmp(greg_t cs, greg_t ss, greg_t gs,
212 213 greg_t fs, greg_t es, greg_t ds,
213 214 greg_t edi, greg_t esi, greg_t ebp, greg_t esp,
214 215 greg_t ebx, greg_t edx, greg_t ecx, greg_t eax, greg_t eip,
215 216 sigjmp_buf env, int savemask)
216 217 {
217 218 ucontext_t *ucp = (ucontext_t *)env;
218 219 ulwp_t *self = curthread;
219 220
220 221 ucp->uc_link = self->ul_siglink;
221 222 if (self->ul_ustack.ss_flags & SS_ONSTACK)
222 223 ucp->uc_stack = self->ul_ustack;
223 224 else {
224 225 ucp->uc_stack.ss_sp =
225 226 (void *)(self->ul_stktop - self->ul_stksiz);
226 227 ucp->uc_stack.ss_size = self->ul_stksiz;
227 228 ucp->uc_stack.ss_flags = 0;
228 229 }
229 230 ucp->uc_flags = UC_STACK | UC_CPU;
230 231 if (savemask) {
231 232 ucp->uc_flags |= UC_SIGMASK;
232 233 enter_critical(self);
233 234 ucp->uc_sigmask = self->ul_sigmask;
234 235 exit_critical(self);
235 236 }
236 237 ucp->uc_mcontext.gregs[GS] = gs;
237 238 ucp->uc_mcontext.gregs[FS] = fs;
238 239 ucp->uc_mcontext.gregs[ES] = es;
239 240 ucp->uc_mcontext.gregs[DS] = ds;
240 241 ucp->uc_mcontext.gregs[EDI] = edi;
241 242 ucp->uc_mcontext.gregs[ESI] = esi;
242 243 ucp->uc_mcontext.gregs[EBP] = ebp;
243 244 ucp->uc_mcontext.gregs[ESP] = esp + 4;
244 245 ucp->uc_mcontext.gregs[EBX] = ebx;
245 246 ucp->uc_mcontext.gregs[EDX] = edx;
246 247 ucp->uc_mcontext.gregs[ECX] = ecx;
247 248 ucp->uc_mcontext.gregs[EAX] = eax;
248 249 ucp->uc_mcontext.gregs[TRAPNO] = 0;
249 250 ucp->uc_mcontext.gregs[ERR] = 0;
250 251 ucp->uc_mcontext.gregs[EIP] = eip;
251 252 ucp->uc_mcontext.gregs[CS] = cs;
252 253 ucp->uc_mcontext.gregs[EFL] = 0;
253 254 ucp->uc_mcontext.gregs[UESP] = esp + 4;
254 255 ucp->uc_mcontext.gregs[SS] = ss;
255 256
256 257 return (0);
257 258 }
258 259
259 260 void
260 261 smt_pause(void)
261 262 {
262 263 SMT_PAUSE();
263 264 }
↓ open down ↓ |
214 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX