sys_lwp.c revision 1.1.2.8 1 1.1.2.8 ad /* $NetBSD: sys_lwp.c,v 1.1.2.8 2007/01/17 20:24:11 ad Exp $ */
2 1.1.2.1 ad
3 1.1.2.1 ad /*-
4 1.1.2.1 ad * Copyright (c) 2001, 2006 The NetBSD Foundation, Inc.
5 1.1.2.1 ad * All rights reserved.
6 1.1.2.1 ad *
7 1.1.2.1 ad * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.1 ad * by Nathan J. Williams, and Andrew Doran.
9 1.1.2.1 ad *
10 1.1.2.1 ad * Redistribution and use in source and binary forms, with or without
11 1.1.2.1 ad * modification, are permitted provided that the following conditions
12 1.1.2.1 ad * are met:
13 1.1.2.1 ad * 1. Redistributions of source code must retain the above copyright
14 1.1.2.1 ad * notice, this list of conditions and the following disclaimer.
15 1.1.2.1 ad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.1 ad * notice, this list of conditions and the following disclaimer in the
17 1.1.2.1 ad * documentation and/or other materials provided with the distribution.
18 1.1.2.1 ad * 3. All advertising materials mentioning features or use of this software
19 1.1.2.1 ad * must display the following acknowledgement:
20 1.1.2.1 ad * This product includes software developed by the NetBSD
21 1.1.2.1 ad * Foundation, Inc. and its contributors.
22 1.1.2.1 ad * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1.2.1 ad * contributors may be used to endorse or promote products derived
24 1.1.2.1 ad * from this software without specific prior written permission.
25 1.1.2.1 ad *
26 1.1.2.1 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1.2.1 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1.2.1 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1.2.1 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1.2.1 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1.2.1 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1.2.1 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1.2.1 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1.2.1 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1.2.1 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1.2.1 ad * POSSIBILITY OF SUCH DAMAGE.
37 1.1.2.1 ad */
38 1.1.2.1 ad
39 1.1.2.3 ad /*
40 1.1.2.3 ad * Lightweight process (LWP) system calls. See kern_lwp.c for a description
41 1.1.2.3 ad * of LWPs.
42 1.1.2.3 ad */
43 1.1.2.3 ad
44 1.1.2.1 ad #include <sys/cdefs.h>
45 1.1.2.8 ad __KERNEL_RCSID(0, "$NetBSD: sys_lwp.c,v 1.1.2.8 2007/01/17 20:24:11 ad Exp $");
46 1.1.2.1 ad
47 1.1.2.1 ad #include <sys/param.h>
48 1.1.2.1 ad #include <sys/systm.h>
49 1.1.2.1 ad #include <sys/pool.h>
50 1.1.2.1 ad #include <sys/proc.h>
51 1.1.2.1 ad #include <sys/sa.h>
52 1.1.2.1 ad #include <sys/savar.h>
53 1.1.2.1 ad #include <sys/types.h>
54 1.1.2.1 ad #include <sys/syscallargs.h>
55 1.1.2.2 ad #include <sys/kauth.h>
56 1.1.2.4 ad #include <sys/kmem.h>
57 1.1.2.4 ad #include <sys/sleepq.h>
58 1.1.2.1 ad
59 1.1.2.1 ad #include <uvm/uvm_extern.h>
60 1.1.2.1 ad
61 1.1.2.4 ad #define LWP_UNPARK_MAX 1024
62 1.1.2.4 ad
63 1.1.2.4 ad syncobj_t lwp_park_sobj = {
64 1.1.2.4 ad SOBJ_SLEEPQ_SORTED,
65 1.1.2.4 ad sleepq_unsleep,
66 1.1.2.4 ad sleepq_changepri
67 1.1.2.4 ad };
68 1.1.2.4 ad
69 1.1.2.4 ad sleeptab_t lwp_park_tab;
70 1.1.2.4 ad
71 1.1.2.4 ad #ifdef LWP_COUNTERS
72 1.1.2.4 ad struct evcnt lwp_ev_park_early = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
73 1.1.2.4 ad NULL, "_lwp_park", "unparked early");
74 1.1.2.4 ad struct evcnt lwp_ev_park_raced = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
75 1.1.2.4 ad NULL, "_lwp_park", "raced");
76 1.1.2.4 ad struct evcnt lwp_ev_park_miss = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
77 1.1.2.4 ad NULL, "_lwp_park", "not parked");
78 1.1.2.4 ad struct evcnt lwp_ev_park_bcast = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
79 1.1.2.4 ad NULL, "_lwp_park", "broadcast unpark");
80 1.1.2.4 ad struct evcnt lwp_ev_park_targ = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
81 1.1.2.4 ad NULL, "_lwp_park", "targeted unpark");
82 1.1.2.4 ad struct evcnt lwp_ev_park = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
83 1.1.2.4 ad NULL, "_lwp_park", "parked");
84 1.1.2.4 ad
85 1.1.2.4 ad #define LWP_COUNT(ev, val) (ev).ev_count += (val) /* XXXSMP */
86 1.1.2.4 ad #else
87 1.1.2.4 ad #define LWP_COUNT(ev, val) /* nothing */
88 1.1.2.4 ad #endif
89 1.1.2.4 ad
90 1.1.2.4 ad void
91 1.1.2.4 ad lwp_sys_init(void)
92 1.1.2.4 ad {
93 1.1.2.4 ad sleeptab_init(&lwp_park_tab);
94 1.1.2.4 ad #ifdef LWP_COUNTERS
95 1.1.2.4 ad evcnt_attach_static(&lwp_ev_park_early);
96 1.1.2.4 ad evcnt_attach_static(&lwp_ev_park_raced);
97 1.1.2.4 ad evcnt_attach_static(&lwp_ev_park_miss);
98 1.1.2.4 ad evcnt_attach_static(&lwp_ev_park_bcast);
99 1.1.2.4 ad evcnt_attach_static(&lwp_ev_park_targ);
100 1.1.2.4 ad evcnt_attach_static(&lwp_ev_park);
101 1.1.2.4 ad #endif
102 1.1.2.4 ad }
103 1.1.2.4 ad
104 1.1.2.1 ad /* ARGSUSED */
105 1.1.2.1 ad int
106 1.1.2.1 ad sys__lwp_create(struct lwp *l, void *v, register_t *retval)
107 1.1.2.1 ad {
108 1.1.2.1 ad struct sys__lwp_create_args /* {
109 1.1.2.1 ad syscallarg(const ucontext_t *) ucp;
110 1.1.2.1 ad syscallarg(u_long) flags;
111 1.1.2.1 ad syscallarg(lwpid_t *) new_lwp;
112 1.1.2.1 ad } */ *uap = v;
113 1.1.2.1 ad struct proc *p = l->l_proc;
114 1.1.2.1 ad struct lwp *l2;
115 1.1.2.1 ad vaddr_t uaddr;
116 1.1.2.1 ad boolean_t inmem;
117 1.1.2.1 ad ucontext_t *newuc;
118 1.1.2.1 ad int error, lid;
119 1.1.2.1 ad
120 1.1.2.1 ad mutex_enter(&p->p_smutex);
121 1.1.2.3 ad if ((p->p_sflag & (PS_SA | PS_WEXIT)) != 0 || p->p_sa != NULL) {
122 1.1.2.1 ad mutex_exit(&p->p_smutex);
123 1.1.2.1 ad return EINVAL;
124 1.1.2.1 ad }
125 1.1.2.3 ad p->p_sflag |= PS_NOSA;
126 1.1.2.1 ad mutex_exit(&p->p_smutex);
127 1.1.2.1 ad
128 1.1.2.1 ad newuc = pool_get(&lwp_uc_pool, PR_WAITOK);
129 1.1.2.1 ad
130 1.1.2.1 ad error = copyin(SCARG(uap, ucp), newuc,
131 1.1.2.1 ad l->l_proc->p_emul->e_sa->sae_ucsize);
132 1.1.2.1 ad if (error) {
133 1.1.2.1 ad pool_put(&lwp_uc_pool, newuc);
134 1.1.2.1 ad return (error);
135 1.1.2.1 ad }
136 1.1.2.1 ad
137 1.1.2.1 ad /* XXX check against resource limits */
138 1.1.2.1 ad
139 1.1.2.1 ad inmem = uvm_uarea_alloc(&uaddr);
140 1.1.2.1 ad if (__predict_false(uaddr == 0)) {
141 1.1.2.1 ad pool_put(&lwp_uc_pool, newuc);
142 1.1.2.1 ad return (ENOMEM);
143 1.1.2.1 ad }
144 1.1.2.1 ad
145 1.1.2.1 ad newlwp(l, p, uaddr, inmem,
146 1.1.2.1 ad SCARG(uap, flags) & LWP_DETACHED,
147 1.1.2.1 ad NULL, 0, startlwp, newuc, &l2);
148 1.1.2.1 ad
149 1.1.2.4 ad /*
150 1.1.2.4 ad * Set the new LWP running, unless the caller has requested that
151 1.1.2.4 ad * it be created in suspended state. If the process is stopping,
152 1.1.2.4 ad * then the LWP is created stopped.
153 1.1.2.4 ad */
154 1.1.2.1 ad mutex_enter(&p->p_smutex);
155 1.1.2.1 ad lwp_lock(l2);
156 1.1.2.1 ad lid = l2->l_lid;
157 1.1.2.3 ad if ((SCARG(uap, flags) & LWP_SUSPENDED) == 0 &&
158 1.1.2.6 ad (l->l_flag & (L_WREBOOT | L_WSUSPEND)) == 0) {
159 1.1.2.4 ad if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0)
160 1.1.2.4 ad l2->l_stat = LSSTOP;
161 1.1.2.4 ad else {
162 1.1.2.6 ad LOCK_ASSERT(lwp_locked(l2, &sched_mutex));
163 1.1.2.4 ad p->p_nrlwps++;
164 1.1.2.4 ad l2->l_stat = LSRUN;
165 1.1.2.4 ad setrunqueue(l2);
166 1.1.2.4 ad }
167 1.1.2.1 ad } else
168 1.1.2.1 ad l2->l_stat = LSSUSPENDED;
169 1.1.2.1 ad lwp_unlock(l2);
170 1.1.2.1 ad mutex_exit(&p->p_smutex);
171 1.1.2.1 ad
172 1.1.2.1 ad error = copyout(&lid, SCARG(uap, new_lwp), sizeof(lid));
173 1.1.2.4 ad if (error)
174 1.1.2.1 ad return (error);
175 1.1.2.1 ad
176 1.1.2.1 ad return (0);
177 1.1.2.1 ad }
178 1.1.2.1 ad
179 1.1.2.1 ad int
180 1.1.2.1 ad sys__lwp_exit(struct lwp *l, void *v, register_t *retval)
181 1.1.2.1 ad {
182 1.1.2.1 ad
183 1.1.2.4 ad lwp_exit(l);
184 1.1.2.4 ad return (0);
185 1.1.2.1 ad }
186 1.1.2.1 ad
187 1.1.2.1 ad int
188 1.1.2.1 ad sys__lwp_self(struct lwp *l, void *v, register_t *retval)
189 1.1.2.1 ad {
190 1.1.2.1 ad
191 1.1.2.1 ad *retval = l->l_lid;
192 1.1.2.1 ad
193 1.1.2.1 ad return (0);
194 1.1.2.1 ad }
195 1.1.2.1 ad
196 1.1.2.1 ad int
197 1.1.2.1 ad sys__lwp_getprivate(struct lwp *l, void *v, register_t *retval)
198 1.1.2.1 ad {
199 1.1.2.1 ad
200 1.1.2.1 ad mb_read();
201 1.1.2.1 ad *retval = (uintptr_t) l->l_private;
202 1.1.2.1 ad
203 1.1.2.1 ad return (0);
204 1.1.2.1 ad }
205 1.1.2.1 ad
206 1.1.2.1 ad int
207 1.1.2.1 ad sys__lwp_setprivate(struct lwp *l, void *v, register_t *retval)
208 1.1.2.1 ad {
209 1.1.2.1 ad struct sys__lwp_setprivate_args /* {
210 1.1.2.1 ad syscallarg(void *) ptr;
211 1.1.2.1 ad } */ *uap = v;
212 1.1.2.1 ad
213 1.1.2.1 ad l->l_private = SCARG(uap, ptr);
214 1.1.2.1 ad mb_write();
215 1.1.2.1 ad
216 1.1.2.1 ad return (0);
217 1.1.2.1 ad }
218 1.1.2.1 ad
219 1.1.2.1 ad int
220 1.1.2.1 ad sys__lwp_suspend(struct lwp *l, void *v, register_t *retval)
221 1.1.2.1 ad {
222 1.1.2.1 ad struct sys__lwp_suspend_args /* {
223 1.1.2.1 ad syscallarg(lwpid_t) target;
224 1.1.2.1 ad } */ *uap = v;
225 1.1.2.1 ad struct proc *p = l->l_proc;
226 1.1.2.1 ad struct lwp *t;
227 1.1.2.1 ad int error;
228 1.1.2.1 ad
229 1.1.2.1 ad mutex_enter(&p->p_smutex);
230 1.1.2.1 ad
231 1.1.2.4 ad if ((p->p_sflag & PS_SA) != 0 || p->p_sa != NULL) {
232 1.1.2.1 ad mutex_exit(&p->p_smutex);
233 1.1.2.1 ad return EINVAL;
234 1.1.2.1 ad }
235 1.1.2.1 ad
236 1.1.2.4 ad if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
237 1.1.2.1 ad mutex_exit(&p->p_smutex);
238 1.1.2.1 ad return (ESRCH);
239 1.1.2.1 ad }
240 1.1.2.1 ad
241 1.1.2.1 ad /*
242 1.1.2.1 ad * Check for deadlock, which is only possible when we're suspending
243 1.1.2.4 ad * ourself. XXX There is a short race here, as p_nrlwps is only
244 1.1.2.4 ad * incremented when an LWP suspends itself on the kernel/user
245 1.1.2.4 ad * boundary. It's still possible to kill -9 the process so we
246 1.1.2.4 ad * don't bother checking further.
247 1.1.2.1 ad */
248 1.1.2.4 ad lwp_lock(t);
249 1.1.2.4 ad if ((t == l && p->p_nrlwps == 1) ||
250 1.1.2.4 ad (l->l_flag & (L_WCORE | L_WEXIT)) != 0) {
251 1.1.2.4 ad lwp_unlock(t);
252 1.1.2.1 ad mutex_exit(&p->p_smutex);
253 1.1.2.1 ad return (EDEADLK);
254 1.1.2.1 ad }
255 1.1.2.1 ad
256 1.1.2.1 ad /*
257 1.1.2.5 ad * Suspend the LWP. XXX If it's on a different CPU, we should wait
258 1.1.2.5 ad * for it to be preempted, where it will put itself to sleep.
259 1.1.2.4 ad *
260 1.1.2.4 ad * Suspension of the current LWP will happen on return to userspace.
261 1.1.2.1 ad */
262 1.1.2.4 ad error = lwp_suspend(l, t);
263 1.1.2.1 ad mutex_exit(&p->p_smutex);
264 1.1.2.1 ad
265 1.1.2.1 ad return (error);
266 1.1.2.1 ad }
267 1.1.2.1 ad
268 1.1.2.1 ad int
269 1.1.2.1 ad sys__lwp_continue(struct lwp *l, void *v, register_t *retval)
270 1.1.2.1 ad {
271 1.1.2.1 ad struct sys__lwp_continue_args /* {
272 1.1.2.1 ad syscallarg(lwpid_t) target;
273 1.1.2.1 ad } */ *uap = v;
274 1.1.2.1 ad int error;
275 1.1.2.1 ad struct proc *p = l->l_proc;
276 1.1.2.1 ad struct lwp *t;
277 1.1.2.1 ad
278 1.1.2.1 ad error = 0;
279 1.1.2.1 ad
280 1.1.2.1 ad mutex_enter(&p->p_smutex);
281 1.1.2.1 ad
282 1.1.2.4 ad if ((p->p_sflag & PS_SA) != 0 || p->p_sa != NULL) {
283 1.1.2.4 ad mutex_exit(&p->p_smutex);
284 1.1.2.4 ad return EINVAL;
285 1.1.2.4 ad }
286 1.1.2.4 ad
287 1.1.2.4 ad if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
288 1.1.2.4 ad mutex_exit(&p->p_smutex);
289 1.1.2.4 ad return ESRCH;
290 1.1.2.4 ad }
291 1.1.2.1 ad
292 1.1.2.4 ad lwp_lock(t);
293 1.1.2.4 ad lwp_continue(t);
294 1.1.2.1 ad mutex_exit(&p->p_smutex);
295 1.1.2.4 ad
296 1.1.2.4 ad return error;
297 1.1.2.1 ad }
298 1.1.2.1 ad
299 1.1.2.1 ad int
300 1.1.2.1 ad sys__lwp_wakeup(struct lwp *l, void *v, register_t *retval)
301 1.1.2.1 ad {
302 1.1.2.1 ad struct sys__lwp_wakeup_args /* {
303 1.1.2.1 ad syscallarg(lwpid_t) target;
304 1.1.2.1 ad } */ *uap = v;
305 1.1.2.1 ad struct lwp *t;
306 1.1.2.1 ad struct proc *p;
307 1.1.2.1 ad int error;
308 1.1.2.1 ad
309 1.1.2.1 ad p = l->l_proc;
310 1.1.2.1 ad mutex_enter(&p->p_smutex);
311 1.1.2.1 ad
312 1.1.2.4 ad if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
313 1.1.2.1 ad mutex_exit(&p->p_smutex);
314 1.1.2.1 ad return ESRCH;
315 1.1.2.1 ad }
316 1.1.2.1 ad
317 1.1.2.6 ad lwp_lock(t);
318 1.1.2.4 ad
319 1.1.2.1 ad if (t->l_stat != LSSLEEP) {
320 1.1.2.1 ad error = ENODEV;
321 1.1.2.1 ad goto bad;
322 1.1.2.1 ad }
323 1.1.2.1 ad
324 1.1.2.1 ad if ((t->l_flag & L_SINTR) == 0) {
325 1.1.2.1 ad error = EBUSY;
326 1.1.2.1 ad goto bad;
327 1.1.2.1 ad }
328 1.1.2.1 ad
329 1.1.2.3 ad /* wake it up setrunnable() will release the LWP lock. */
330 1.1.2.1 ad t->l_flag |= L_CANCELLED;
331 1.1.2.1 ad setrunnable(t);
332 1.1.2.1 ad mutex_exit(&p->p_smutex);
333 1.1.2.1 ad return 0;
334 1.1.2.1 ad
335 1.1.2.1 ad bad:
336 1.1.2.6 ad lwp_unlock(t);
337 1.1.2.1 ad mutex_exit(&p->p_smutex);
338 1.1.2.1 ad return error;
339 1.1.2.1 ad }
340 1.1.2.1 ad
341 1.1.2.1 ad int
342 1.1.2.1 ad sys__lwp_wait(struct lwp *l, void *v, register_t *retval)
343 1.1.2.1 ad {
344 1.1.2.1 ad struct sys__lwp_wait_args /* {
345 1.1.2.1 ad syscallarg(lwpid_t) wait_for;
346 1.1.2.1 ad syscallarg(lwpid_t *) departed;
347 1.1.2.1 ad } */ *uap = v;
348 1.1.2.1 ad struct proc *p = l->l_proc;
349 1.1.2.1 ad int error;
350 1.1.2.1 ad lwpid_t dep;
351 1.1.2.1 ad
352 1.1.2.1 ad mutex_enter(&p->p_smutex);
353 1.1.2.1 ad error = lwp_wait1(l, SCARG(uap, wait_for), &dep, 0);
354 1.1.2.1 ad mutex_exit(&p->p_smutex);
355 1.1.2.4 ad
356 1.1.2.1 ad if (error)
357 1.1.2.1 ad return (error);
358 1.1.2.1 ad
359 1.1.2.1 ad if (SCARG(uap, departed)) {
360 1.1.2.1 ad error = copyout(&dep, SCARG(uap, departed),
361 1.1.2.1 ad sizeof(dep));
362 1.1.2.1 ad if (error)
363 1.1.2.1 ad return (error);
364 1.1.2.1 ad }
365 1.1.2.1 ad
366 1.1.2.1 ad return (0);
367 1.1.2.1 ad }
368 1.1.2.2 ad
369 1.1.2.2 ad /* ARGSUSED */
370 1.1.2.2 ad int
371 1.1.2.2 ad sys__lwp_kill(struct lwp *l, void *v, register_t *retval)
372 1.1.2.2 ad {
373 1.1.2.2 ad struct sys__lwp_kill_args /* {
374 1.1.2.2 ad syscallarg(lwpid_t) target;
375 1.1.2.2 ad syscallarg(int) signo;
376 1.1.2.2 ad } */ *uap = v;
377 1.1.2.2 ad struct proc *p = l->l_proc;
378 1.1.2.2 ad struct lwp *t;
379 1.1.2.2 ad ksiginfo_t ksi;
380 1.1.2.2 ad int signo = SCARG(uap, signo);
381 1.1.2.2 ad int error;
382 1.1.2.2 ad
383 1.1.2.2 ad if ((u_int)signo >= NSIG)
384 1.1.2.2 ad return (EINVAL);
385 1.1.2.2 ad
386 1.1.2.2 ad KSI_INIT(&ksi);
387 1.1.2.2 ad ksi.ksi_signo = signo;
388 1.1.2.2 ad ksi.ksi_code = SI_USER;
389 1.1.2.2 ad ksi.ksi_pid = p->p_pid;
390 1.1.2.2 ad ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
391 1.1.2.2 ad ksi.ksi_lid = SCARG(uap, target);
392 1.1.2.2 ad
393 1.1.2.4 ad mutex_enter(&proclist_mutex);
394 1.1.2.2 ad mutex_enter(&p->p_smutex);
395 1.1.2.4 ad if ((t = lwp_find(p, ksi.ksi_lid)) == NULL)
396 1.1.2.2 ad error = ESRCH;
397 1.1.2.2 ad else {
398 1.1.2.2 ad kpsignal2(p, &ksi);
399 1.1.2.2 ad error = 0;
400 1.1.2.2 ad }
401 1.1.2.2 ad mutex_exit(&p->p_smutex);
402 1.1.2.4 ad mutex_exit(&proclist_mutex);
403 1.1.2.2 ad
404 1.1.2.3 ad return (error);
405 1.1.2.2 ad }
406 1.1.2.4 ad
407 1.1.2.4 ad int
408 1.1.2.4 ad sys__lwp_detach(struct lwp *l, void *v, register_t *retval)
409 1.1.2.4 ad {
410 1.1.2.4 ad struct sys__lwp_detach_args /* {
411 1.1.2.4 ad syscallarg(lwpid_t) target;
412 1.1.2.4 ad } */ *uap = v;
413 1.1.2.4 ad struct proc *p;
414 1.1.2.4 ad struct lwp *t;
415 1.1.2.4 ad lwpid_t target;
416 1.1.2.4 ad
417 1.1.2.4 ad target = SCARG(uap, target);
418 1.1.2.4 ad p = l->l_proc;
419 1.1.2.4 ad
420 1.1.2.4 ad mutex_enter(&p->p_smutex);
421 1.1.2.4 ad if (l->l_lid == target)
422 1.1.2.4 ad t = l;
423 1.1.2.7 ad else {
424 1.1.2.7 ad /*
425 1.1.2.7 ad * We can't use lwp_find() here because the target might
426 1.1.2.7 ad * be a zombie.
427 1.1.2.7 ad */
428 1.1.2.7 ad LIST_FOREACH(t, &p->p_lwps, l_sibling)
429 1.1.2.7 ad if (t->l_lid == target)
430 1.1.2.7 ad break;
431 1.1.2.7 ad }
432 1.1.2.7 ad
433 1.1.2.7 ad /*
434 1.1.2.7 ad * If the LWP is already detached, there's nothing to do.
435 1.1.2.7 ad * If it's a zombie, we need to clean up after it. LSZOMB
436 1.1.2.7 ad * is visible with the proc mutex held.
437 1.1.2.7 ad *
438 1.1.2.7 ad * After we have detached or released the LWP, kick any
439 1.1.2.7 ad * other LWPs that may be sitting in _lwp_wait(), waiting
440 1.1.2.7 ad * for the target LWP to exit.
441 1.1.2.7 ad */
442 1.1.2.7 ad if (t != NULL && t->l_stat != LSIDL &&
443 1.1.2.7 ad (t->l_prflag & LPR_DETACHED) == 0) {
444 1.1.2.4 ad p->p_ndlwps++;
445 1.1.2.4 ad t->l_prflag |= LPR_DETACHED;
446 1.1.2.7 ad if (t->l_stat == LSZOMB) {
447 1.1.2.7 ad lwp_free(t, 0, 0); /* releases proc mutex */
448 1.1.2.7 ad cv_broadcast(&p->p_lwpcv);
449 1.1.2.7 ad return 0;
450 1.1.2.7 ad }
451 1.1.2.4 ad }
452 1.1.2.4 ad mutex_exit(&p->p_smutex);
453 1.1.2.8 ad cv_broadcast(&p->p_lwpcv);
454 1.1.2.4 ad
455 1.1.2.4 ad return (t == NULL ? ESRCH : 0);
456 1.1.2.4 ad }
457 1.1.2.4 ad
458 1.1.2.4 ad static inline wchan_t
459 1.1.2.6 ad lwp_park_wchan(struct proc *p, const void *hint)
460 1.1.2.4 ad {
461 1.1.2.6 ad return (wchan_t)((uintptr_t)p ^ (uintptr_t)hint);
462 1.1.2.4 ad }
463 1.1.2.4 ad
464 1.1.2.4 ad /*
465 1.1.2.4 ad * 'park' an LWP waiting on a user-level synchronisation object. The LWP
466 1.1.2.4 ad * will remain parked until another LWP in the same process calls in and
467 1.1.2.4 ad * requests that it be unparked.
468 1.1.2.4 ad */
469 1.1.2.4 ad int
470 1.1.2.4 ad sys__lwp_park(struct lwp *l, void *v, register_t *retval)
471 1.1.2.4 ad {
472 1.1.2.4 ad struct sys__lwp_park_args /* {
473 1.1.2.4 ad syscallarg(const struct timespec *) ts;
474 1.1.2.4 ad syscallarg(ucontext_t *) uc;
475 1.1.2.6 ad syscallarg(const void *) hint;
476 1.1.2.4 ad } */ *uap = v;
477 1.1.2.4 ad const struct timespec *tsp;
478 1.1.2.4 ad struct timespec ts, tsx;
479 1.1.2.4 ad struct timeval tv;
480 1.1.2.4 ad sleepq_t *sq;
481 1.1.2.4 ad wchan_t wchan;
482 1.1.2.4 ad int timo, error;
483 1.1.2.4 ad
484 1.1.2.4 ad /* Fix up the given timeout value. */
485 1.1.2.4 ad if ((tsp = SCARG(uap, ts)) != NULL) {
486 1.1.2.4 ad if ((error = copyin(tsp, &ts, sizeof(ts))) != 0)
487 1.1.2.4 ad return error;
488 1.1.2.4 ad getnanotime(&tsx);
489 1.1.2.4 ad timespecsub(&ts, &tsx, &ts);
490 1.1.2.4 ad tv.tv_sec = ts.tv_sec;
491 1.1.2.4 ad tv.tv_usec = ts.tv_nsec / 1000;
492 1.1.2.4 ad if (tv.tv_sec < 0 || (tv.tv_sec == 0 && tv.tv_usec < 0))
493 1.1.2.4 ad return ETIMEDOUT;
494 1.1.2.4 ad if ((error = itimerfix(&tv)) != 0)
495 1.1.2.4 ad return error;
496 1.1.2.4 ad timo = tvtohz(&tv);
497 1.1.2.4 ad } else
498 1.1.2.4 ad timo = 0;
499 1.1.2.4 ad
500 1.1.2.4 ad /* Find and lock the sleep queue. */
501 1.1.2.6 ad wchan = lwp_park_wchan(l->l_proc, SCARG(uap, hint));
502 1.1.2.4 ad sq = sleeptab_lookup(&lwp_park_tab, wchan);
503 1.1.2.4 ad
504 1.1.2.4 ad /*
505 1.1.2.4 ad * Before going the full route and blocking, check to see if an
506 1.1.2.4 ad * unpark op is pending.
507 1.1.2.4 ad */
508 1.1.2.4 ad if ((l->l_flag & L_CANCELLED) != 0) {
509 1.1.2.4 ad sleepq_lwp_lock(l);
510 1.1.2.4 ad l->l_flag &= ~L_CANCELLED;
511 1.1.2.4 ad sleepq_lwp_unlock(l);
512 1.1.2.4 ad sleepq_unlock(sq);
513 1.1.2.4 ad LWP_COUNT(lwp_ev_park_early, 1);
514 1.1.2.4 ad return EALREADY;
515 1.1.2.4 ad }
516 1.1.2.4 ad
517 1.1.2.4 ad /*
518 1.1.2.4 ad * For now we ignore the ucontext argument. In the future, we may
519 1.1.2.4 ad * put our stack up to be recycled. If it's binned, a trampoline
520 1.1.2.4 ad * function could call sleepq_unblock() on our behalf.
521 1.1.2.4 ad */
522 1.1.2.4 ad LWP_COUNT(lwp_ev_park, 1);
523 1.1.2.4 ad sleepq_enter(sq, l);
524 1.1.2.4 ad sleepq_block(sq, sched_kpri(l), wchan, "parked", timo, 1,
525 1.1.2.4 ad &lwp_park_sobj);
526 1.1.2.4 ad error = sleepq_unblock(timo, 1);
527 1.1.2.4 ad return (error == EWOULDBLOCK ? ETIMEDOUT : error);
528 1.1.2.4 ad }
529 1.1.2.4 ad
530 1.1.2.4 ad int
531 1.1.2.4 ad sys__lwp_unpark(struct lwp *l, void *v, register_t *retval)
532 1.1.2.4 ad {
533 1.1.2.4 ad struct sys__lwp_unpark_args /* {
534 1.1.2.4 ad syscallarg(lwpid_t) target;
535 1.1.2.6 ad syscallarg(const void *) hint;
536 1.1.2.4 ad } */ *uap = v;
537 1.1.2.4 ad struct proc *p;
538 1.1.2.4 ad struct lwp *t;
539 1.1.2.4 ad sleepq_t *sq;
540 1.1.2.4 ad lwpid_t target;
541 1.1.2.4 ad wchan_t wchan;
542 1.1.2.4 ad int swapin;
543 1.1.2.4 ad
544 1.1.2.4 ad p = l->l_proc;
545 1.1.2.4 ad target = SCARG(uap, target);
546 1.1.2.4 ad
547 1.1.2.4 ad /*
548 1.1.2.4 ad * Easy case: search for the LWP on the sleep queue. If
549 1.1.2.4 ad * it's parked, remove it from the queue and set running.
550 1.1.2.4 ad */
551 1.1.2.6 ad wchan = lwp_park_wchan(p, SCARG(uap, hint));
552 1.1.2.4 ad sq = sleeptab_lookup(&lwp_park_tab, wchan);
553 1.1.2.4 ad
554 1.1.2.4 ad TAILQ_FOREACH(t, &sq->sq_queue, l_sleepchain)
555 1.1.2.4 ad if (t->l_proc == p && t->l_lid == target)
556 1.1.2.4 ad break;
557 1.1.2.4 ad
558 1.1.2.4 ad if (t == NULL) {
559 1.1.2.4 ad /*
560 1.1.2.4 ad * The LWP hasn't parked yet. Take the hit
561 1.1.2.4 ad * and mark the operation as pending.
562 1.1.2.4 ad */
563 1.1.2.4 ad sleepq_unlock(sq);
564 1.1.2.4 ad mutex_enter(&p->p_smutex);
565 1.1.2.6 ad if ((t = lwp_find(p, target)) == NULL) {
566 1.1.2.6 ad mutex_exit(&p->p_smutex);
567 1.1.2.6 ad return ESRCH;
568 1.1.2.6 ad }
569 1.1.2.6 ad lwp_lock(t);
570 1.1.2.4 ad mutex_exit(&p->p_smutex);
571 1.1.2.4 ad
572 1.1.2.6 ad if (t->l_sleepq == sq) {
573 1.1.2.6 ad /*
574 1.1.2.6 ad * We have raced, and the LWP is now parked.
575 1.1.2.6 ad * Wake it in the usual way.
576 1.1.2.6 ad */
577 1.1.2.6 ad KASSERT(t->l_syncobj == &lwp_park_sobj);
578 1.1.2.6 ad LOCK_ASSERT(lwp_locked(t, sq->sq_mutex));
579 1.1.2.6 ad LWP_COUNT(lwp_ev_park_raced, 1);
580 1.1.2.6 ad } else {
581 1.1.2.6 ad /*
582 1.1.2.6 ad * It many not have parked yet, or is parked
583 1.1.2.6 ad * on a different user sync object. The
584 1.1.2.6 ad * latter is an application error.
585 1.1.2.6 ad */
586 1.1.2.4 ad t->l_flag |= L_CANCELLED;
587 1.1.2.4 ad lwp_unlock(t);
588 1.1.2.6 ad return 0;
589 1.1.2.4 ad }
590 1.1.2.4 ad }
591 1.1.2.4 ad
592 1.1.2.4 ad swapin = sleepq_remove(sq, t);
593 1.1.2.4 ad sleepq_unlock(sq);
594 1.1.2.4 ad if (swapin)
595 1.1.2.4 ad wakeup(&proc0);
596 1.1.2.4 ad LWP_COUNT(lwp_ev_park_targ, 1);
597 1.1.2.4 ad return 0;
598 1.1.2.4 ad }
599 1.1.2.4 ad
600 1.1.2.4 ad int
601 1.1.2.4 ad sys__lwp_unpark_all(struct lwp *l, void *v, register_t *retval)
602 1.1.2.4 ad {
603 1.1.2.4 ad struct sys__lwp_unpark_all_args /* {
604 1.1.2.4 ad syscallarg(const lwpid_t *) targets;
605 1.1.2.4 ad syscallarg(size_t) ntargets;
606 1.1.2.6 ad syscallarg(const void *) hint;
607 1.1.2.4 ad } */ *uap = v;
608 1.1.2.4 ad struct proc *p;
609 1.1.2.4 ad struct lwp *t;
610 1.1.2.4 ad sleepq_t *sq;
611 1.1.2.4 ad wchan_t wchan;
612 1.1.2.6 ad lwpid_t targets[32], *tp, *tpp, *tmax, target;
613 1.1.2.4 ad int swapin, error;
614 1.1.2.4 ad u_int ntargets, unparked;
615 1.1.2.4 ad size_t sz;
616 1.1.2.4 ad
617 1.1.2.4 ad p = l->l_proc;
618 1.1.2.4 ad ntargets = SCARG(uap, ntargets);
619 1.1.2.4 ad
620 1.1.2.4 ad if (SCARG(uap, targets) == NULL) {
621 1.1.2.4 ad /*
622 1.1.2.4 ad * Let the caller know how much we are willing to do, and
623 1.1.2.4 ad * let it unpark the LWPs in blocks.
624 1.1.2.4 ad */
625 1.1.2.4 ad *retval = LWP_UNPARK_MAX;
626 1.1.2.4 ad return 0;
627 1.1.2.4 ad }
628 1.1.2.4 ad if (ntargets > LWP_UNPARK_MAX || ntargets == 0)
629 1.1.2.4 ad return EINVAL;
630 1.1.2.4 ad
631 1.1.2.4 ad /*
632 1.1.2.4 ad * Copy in the target array. If it's a small number of LWPs, then
633 1.1.2.4 ad * place the numbers on the stack.
634 1.1.2.4 ad */
635 1.1.2.4 ad sz = sizeof(target) * ntargets;
636 1.1.2.4 ad if (sz <= sizeof(targets))
637 1.1.2.4 ad tp = targets;
638 1.1.2.4 ad else if ((tp = kmem_alloc(sz, KM_SLEEP)) == NULL)
639 1.1.2.4 ad return ENOMEM;
640 1.1.2.4 ad error = copyin(SCARG(uap, targets), tp, sz);
641 1.1.2.4 ad if (error != 0) {
642 1.1.2.4 ad if (tp != targets)
643 1.1.2.4 ad kmem_free(tp, sz);
644 1.1.2.4 ad return error;
645 1.1.2.4 ad }
646 1.1.2.4 ad
647 1.1.2.4 ad unparked = 0;
648 1.1.2.4 ad swapin = 0;
649 1.1.2.6 ad wchan = lwp_park_wchan(p, SCARG(uap, hint));
650 1.1.2.6 ad sq = sleeptab_lookup(&lwp_park_tab, wchan);
651 1.1.2.4 ad
652 1.1.2.6 ad for (tmax = tp + ntargets, tpp = tp; tpp < tmax; tpp++) {
653 1.1.2.6 ad target = *tpp;
654 1.1.2.4 ad
655 1.1.2.4 ad /*
656 1.1.2.4 ad * Easy case: search for the LWP on the sleep queue. If
657 1.1.2.4 ad * it's parked, remove it from the queue and set running.
658 1.1.2.4 ad */
659 1.1.2.4 ad TAILQ_FOREACH(t, &sq->sq_queue, l_sleepchain)
660 1.1.2.4 ad if (t->l_proc == p && t->l_lid == target)
661 1.1.2.4 ad break;
662 1.1.2.4 ad
663 1.1.2.6 ad if (t != NULL) {
664 1.1.2.6 ad swapin |= sleepq_remove(sq, t);
665 1.1.2.6 ad unparked++;
666 1.1.2.6 ad continue;
667 1.1.2.6 ad }
668 1.1.2.6 ad
669 1.1.2.6 ad /*
670 1.1.2.6 ad * The LWP hasn't parked yet. Take the hit and
671 1.1.2.6 ad * mark the operation as pending.
672 1.1.2.6 ad */
673 1.1.2.6 ad sleepq_unlock(sq);
674 1.1.2.6 ad mutex_enter(&p->p_smutex);
675 1.1.2.6 ad if ((t = lwp_find(p, target)) == NULL) {
676 1.1.2.4 ad mutex_exit(&p->p_smutex);
677 1.1.2.6 ad sleepq_lock(sq);
678 1.1.2.6 ad continue;
679 1.1.2.6 ad }
680 1.1.2.6 ad lwp_lock(t);
681 1.1.2.6 ad mutex_exit(&p->p_smutex);
682 1.1.2.4 ad
683 1.1.2.6 ad if (t->l_sleepq == sq) {
684 1.1.2.4 ad /*
685 1.1.2.4 ad * We have raced, and the LWP is now parked.
686 1.1.2.4 ad * Wake it in the usual way.
687 1.1.2.4 ad */
688 1.1.2.6 ad KASSERT(t->l_syncobj == &lwp_park_sobj);
689 1.1.2.4 ad LOCK_ASSERT(lwp_locked(t, sq->sq_mutex));
690 1.1.2.6 ad LWP_COUNT(lwp_ev_park_raced, 1);
691 1.1.2.6 ad swapin |= sleepq_remove(sq, t);
692 1.1.2.6 ad unparked++;
693 1.1.2.6 ad } else {
694 1.1.2.6 ad /*
695 1.1.2.6 ad * It many not have parked yet, or is parked
696 1.1.2.6 ad * on a different user sync object. The
697 1.1.2.6 ad * latter is an application error.
698 1.1.2.6 ad */
699 1.1.2.6 ad t->l_flag |= L_CANCELLED;
700 1.1.2.6 ad lwp_unlock(t);
701 1.1.2.6 ad sleepq_lock(sq);
702 1.1.2.4 ad }
703 1.1.2.4 ad }
704 1.1.2.4 ad
705 1.1.2.6 ad sleepq_unlock(sq);
706 1.1.2.4 ad if (tp != targets)
707 1.1.2.4 ad kmem_free(tp, sz);
708 1.1.2.4 ad if (swapin)
709 1.1.2.4 ad wakeup(&proc0);
710 1.1.2.4 ad LWP_COUNT(lwp_ev_park_bcast, unparked);
711 1.1.2.4 ad LWP_COUNT(lwp_ev_park_miss, (ntargets - unparked));
712 1.1.2.4 ad return 0;
713 1.1.2.4 ad }
714