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