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