sys_lwp.c revision 1.24 1 /* $NetBSD: sys_lwp.c,v 1.24 2007/08/07 19:00:42 ad 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.24 2007/08/07 19:00:42 ad 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 lid = l2->l_lid;
119 error = copyout(&lid, SCARG(uap, new_lwp), sizeof(lid));
120 if (error) {
121 lwp_exit(l2);
122 pool_put(&lwp_uc_pool, newuc);
123 return error;
124 }
125
126 /*
127 * Set the new LWP running, unless the caller has requested that
128 * it be created in suspended state. If the process is stopping,
129 * then the LWP is created stopped.
130 */
131 mutex_enter(&p->p_smutex);
132 lwp_lock(l2);
133 if ((SCARG(uap, flags) & LWP_SUSPENDED) == 0 &&
134 (l->l_flag & (LW_WREBOOT | LW_WSUSPEND | LW_WEXIT)) == 0) {
135 if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0)
136 l2->l_stat = LSSTOP;
137 else {
138 KASSERT(lwp_locked(l2, l2->l_cpu->ci_schedstate.spc_mutex));
139 p->p_nrlwps++;
140 l2->l_stat = LSRUN;
141 sched_enqueue(l2, false);
142 }
143 } else
144 l2->l_stat = LSSUSPENDED;
145 lwp_unlock(l2);
146 mutex_exit(&p->p_smutex);
147
148 return 0;
149 }
150
151 int
152 sys__lwp_exit(struct lwp *l, void *v, register_t *retval)
153 {
154
155 lwp_exit(l);
156 return 0;
157 }
158
159 int
160 sys__lwp_self(struct lwp *l, void *v, register_t *retval)
161 {
162
163 *retval = l->l_lid;
164 return 0;
165 }
166
167 int
168 sys__lwp_getprivate(struct lwp *l, void *v, register_t *retval)
169 {
170
171 *retval = (uintptr_t)l->l_private;
172 return 0;
173 }
174
175 int
176 sys__lwp_setprivate(struct lwp *l, void *v, register_t *retval)
177 {
178 struct sys__lwp_setprivate_args /* {
179 syscallarg(void *) ptr;
180 } */ *uap = v;
181
182 l->l_private = SCARG(uap, ptr);
183 return 0;
184 }
185
186 int
187 sys__lwp_suspend(struct lwp *l, void *v, register_t *retval)
188 {
189 struct sys__lwp_suspend_args /* {
190 syscallarg(lwpid_t) target;
191 } */ *uap = v;
192 struct proc *p = l->l_proc;
193 struct lwp *t;
194 int error;
195
196 mutex_enter(&p->p_smutex);
197 if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
198 mutex_exit(&p->p_smutex);
199 return ESRCH;
200 }
201
202 /*
203 * Check for deadlock, which is only possible when we're suspending
204 * ourself. XXX There is a short race here, as p_nrlwps is only
205 * incremented when an LWP suspends itself on the kernel/user
206 * boundary. It's still possible to kill -9 the process so we
207 * don't bother checking further.
208 */
209 lwp_lock(t);
210 if ((t == l && p->p_nrlwps == 1) ||
211 (l->l_flag & (LW_WCORE | LW_WEXIT)) != 0) {
212 lwp_unlock(t);
213 mutex_exit(&p->p_smutex);
214 return EDEADLK;
215 }
216
217 /*
218 * Suspend the LWP. XXX If it's on a different CPU, we should wait
219 * for it to be preempted, where it will put itself to sleep.
220 *
221 * Suspension of the current LWP will happen on return to userspace.
222 */
223 error = lwp_suspend(l, t);
224 if (error) {
225 mutex_exit(&p->p_smutex);
226 return error;
227 }
228
229 /*
230 * Wait for:
231 * o process exiting
232 * o target LWP suspended
233 * o target LWP not suspended and L_WSUSPEND clear
234 * o target LWP exited
235 */
236 for (;;) {
237 error = cv_wait_sig(&p->p_lwpcv, &p->p_smutex);
238 if (error) {
239 error = ERESTART;
240 break;
241 }
242 if ((l->l_flag | t->l_flag) & (LW_WCORE | LW_WEXIT)) {
243 error = ERESTART;
244 break;
245 }
246 if (t->l_stat == LSSUSPENDED ||
247 (t->l_flag & LW_WSUSPEND) == 0)
248 break;
249 }
250 mutex_exit(&p->p_smutex);
251
252 return error;
253 }
254
255 int
256 sys__lwp_continue(struct lwp *l, void *v, register_t *retval)
257 {
258 struct sys__lwp_continue_args /* {
259 syscallarg(lwpid_t) target;
260 } */ *uap = v;
261 int error;
262 struct proc *p = l->l_proc;
263 struct lwp *t;
264
265 error = 0;
266
267 mutex_enter(&p->p_smutex);
268 if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
269 mutex_exit(&p->p_smutex);
270 return ESRCH;
271 }
272
273 lwp_lock(t);
274 lwp_continue(t);
275 mutex_exit(&p->p_smutex);
276
277 return error;
278 }
279
280 int
281 sys__lwp_wakeup(struct lwp *l, void *v, register_t *retval)
282 {
283 struct sys__lwp_wakeup_args /* {
284 syscallarg(lwpid_t) target;
285 } */ *uap = v;
286 struct lwp *t;
287 struct proc *p;
288 int error;
289
290 p = l->l_proc;
291 mutex_enter(&p->p_smutex);
292
293 if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
294 mutex_exit(&p->p_smutex);
295 return ESRCH;
296 }
297
298 lwp_lock(t);
299 t->l_flag |= (LW_CANCELLED | LW_UNPARKED);
300
301 if (t->l_stat != LSSLEEP) {
302 lwp_unlock(t);
303 error = ENODEV;
304 } else if ((t->l_flag & LW_SINTR) == 0) {
305 lwp_unlock(t);
306 error = EBUSY;
307 } else {
308 /* Wake it up. lwp_unsleep() will release the LWP lock. */
309 lwp_unsleep(t);
310 error = 0;
311 }
312
313 mutex_exit(&p->p_smutex);
314
315 return error;
316 }
317
318 int
319 sys__lwp_wait(struct lwp *l, void *v, register_t *retval)
320 {
321 struct sys__lwp_wait_args /* {
322 syscallarg(lwpid_t) wait_for;
323 syscallarg(lwpid_t *) departed;
324 } */ *uap = v;
325 struct proc *p = l->l_proc;
326 int error;
327 lwpid_t dep;
328
329 mutex_enter(&p->p_smutex);
330 error = lwp_wait1(l, SCARG(uap, wait_for), &dep, 0);
331 mutex_exit(&p->p_smutex);
332
333 if (error)
334 return error;
335
336 if (SCARG(uap, departed)) {
337 error = copyout(&dep, SCARG(uap, departed), sizeof(dep));
338 if (error)
339 return error;
340 }
341
342 return 0;
343 }
344
345 /* ARGSUSED */
346 int
347 sys__lwp_kill(struct lwp *l, void *v, register_t *retval)
348 {
349 struct sys__lwp_kill_args /* {
350 syscallarg(lwpid_t) target;
351 syscallarg(int) signo;
352 } */ *uap = v;
353 struct proc *p = l->l_proc;
354 struct lwp *t;
355 ksiginfo_t ksi;
356 int signo = SCARG(uap, signo);
357 int error = 0;
358
359 if ((u_int)signo >= NSIG)
360 return EINVAL;
361
362 KSI_INIT(&ksi);
363 ksi.ksi_signo = signo;
364 ksi.ksi_code = SI_USER;
365 ksi.ksi_pid = p->p_pid;
366 ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
367 ksi.ksi_lid = SCARG(uap, target);
368
369 mutex_enter(&proclist_mutex);
370 mutex_enter(&p->p_smutex);
371 if ((t = lwp_find(p, ksi.ksi_lid)) == NULL)
372 error = ESRCH;
373 else if (signo != 0)
374 kpsignal2(p, &ksi);
375 mutex_exit(&p->p_smutex);
376 mutex_exit(&proclist_mutex);
377
378 return error;
379 }
380
381 int
382 sys__lwp_detach(struct lwp *l, void *v, register_t *retval)
383 {
384 struct sys__lwp_detach_args /* {
385 syscallarg(lwpid_t) target;
386 } */ *uap = v;
387 struct proc *p;
388 struct lwp *t;
389 lwpid_t target;
390 int error;
391
392 target = SCARG(uap, target);
393 p = l->l_proc;
394
395 mutex_enter(&p->p_smutex);
396
397 if (l->l_lid == target)
398 t = l;
399 else {
400 /*
401 * We can't use lwp_find() here because the target might
402 * be a zombie.
403 */
404 LIST_FOREACH(t, &p->p_lwps, l_sibling)
405 if (t->l_lid == target)
406 break;
407 }
408
409 /*
410 * If the LWP is already detached, there's nothing to do.
411 * If it's a zombie, we need to clean up after it. LSZOMB
412 * is visible with the proc mutex held.
413 *
414 * After we have detached or released the LWP, kick any
415 * other LWPs that may be sitting in _lwp_wait(), waiting
416 * for the target LWP to exit.
417 */
418 if (t != NULL && t->l_stat != LSIDL) {
419 if ((t->l_prflag & LPR_DETACHED) == 0) {
420 p->p_ndlwps++;
421 t->l_prflag |= LPR_DETACHED;
422 if (t->l_stat == LSZOMB) {
423 /* Releases proc mutex. */
424 lwp_free(t, false, false);
425 return 0;
426 }
427 error = 0;
428
429 /*
430 * Have any LWPs sleeping in lwp_wait() recheck
431 * for deadlock.
432 */
433 cv_broadcast(&p->p_lwpcv);
434 } else
435 error = EINVAL;
436 } else
437 error = ESRCH;
438
439 mutex_exit(&p->p_smutex);
440
441 return error;
442 }
443
444 static inline wchan_t
445 lwp_park_wchan(struct proc *p, const void *hint)
446 {
447
448 return (wchan_t)((uintptr_t)p ^ (uintptr_t)hint);
449 }
450
451 int
452 lwp_unpark(lwpid_t target, const void *hint)
453 {
454 sleepq_t *sq;
455 wchan_t wchan;
456 int swapin;
457 proc_t *p;
458 lwp_t *t;
459
460 /*
461 * Easy case: search for the LWP on the sleep queue. If
462 * it's parked, remove it from the queue and set running.
463 */
464 p = curproc;
465 wchan = lwp_park_wchan(p, hint);
466 sq = sleeptab_lookup(&lwp_park_tab, wchan);
467
468 TAILQ_FOREACH(t, &sq->sq_queue, l_sleepchain)
469 if (t->l_proc == p && t->l_lid == target)
470 break;
471
472 if (__predict_true(t != NULL)) {
473 swapin = sleepq_remove(sq, t);
474 sleepq_unlock(sq);
475 if (swapin)
476 uvm_kick_scheduler();
477 return 0;
478 }
479
480 /*
481 * The LWP hasn't parked yet. Take the hit and mark the
482 * operation as pending.
483 */
484 sleepq_unlock(sq);
485
486 mutex_enter(&p->p_smutex);
487 if ((t = lwp_find(p, target)) == NULL) {
488 mutex_exit(&p->p_smutex);
489 return ESRCH;
490 }
491
492 /*
493 * It may not have parked yet, we may have raced, or it
494 * is parked on a different user sync object.
495 */
496 lwp_lock(t);
497 if (t->l_syncobj == &lwp_park_sobj) {
498 /* Releases the LWP lock. */
499 lwp_unsleep(t);
500 } else {
501 /*
502 * Set the operation pending. The next call to _lwp_park
503 * will return early.
504 */
505 t->l_flag |= LW_UNPARKED;
506 lwp_unlock(t);
507 }
508
509 mutex_exit(&p->p_smutex);
510 return 0;
511 }
512
513 int
514 lwp_park(struct timespec *ts, const void *hint)
515 {
516 struct timespec tsx;
517 sleepq_t *sq;
518 wchan_t wchan;
519 int timo, error;
520 lwp_t *l;
521
522 /* Fix up the given timeout value. */
523 if (ts != NULL) {
524 getnanotime(&tsx);
525 timespecsub(ts, &tsx, &tsx);
526 if (tsx.tv_sec < 0 || (tsx.tv_sec == 0 && tsx.tv_nsec <= 0))
527 return ETIMEDOUT;
528 if ((error = itimespecfix(&tsx)) != 0)
529 return error;
530 timo = tstohz(&tsx);
531 KASSERT(timo != 0);
532 } else
533 timo = 0;
534
535 /* Find and lock the sleep queue. */
536 l = curlwp;
537 wchan = lwp_park_wchan(l->l_proc, hint);
538 sq = sleeptab_lookup(&lwp_park_tab, wchan);
539
540 /*
541 * Before going the full route and blocking, check to see if an
542 * unpark op is pending.
543 */
544 lwp_lock(l);
545 if ((l->l_flag & (LW_CANCELLED | LW_UNPARKED)) != 0) {
546 l->l_flag &= ~(LW_CANCELLED | LW_UNPARKED);
547 lwp_unlock(l);
548 sleepq_unlock(sq);
549 return EALREADY;
550 }
551 lwp_unlock_to(l, sq->sq_mutex);
552 l->l_biglocks = 0;
553 sleepq_enqueue(sq, sched_kpri(l), wchan, "parked", &lwp_park_sobj);
554 error = sleepq_block(timo, true);
555 switch (error) {
556 case EWOULDBLOCK:
557 error = ETIMEDOUT;
558 break;
559 case ERESTART:
560 error = EINTR;
561 break;
562 default:
563 /* nothing */
564 break;
565 }
566 return error;
567 }
568
569 /*
570 * 'park' an LWP waiting on a user-level synchronisation object. The LWP
571 * will remain parked until another LWP in the same process calls in and
572 * requests that it be unparked.
573 */
574 int
575 sys__lwp_park(struct lwp *l, void *v, register_t *retval)
576 {
577 struct sys__lwp_park_args /* {
578 syscallarg(const struct timespec *) ts;
579 syscallarg(lwpid_t) unpark;
580 syscallarg(const void *) hint;
581 syscallarg(const void *) unparkhint;
582 } */ *uap = v;
583 struct timespec ts, *tsp;
584 int error;
585
586 if (SCARG(uap, ts) == NULL)
587 tsp = NULL;
588 else {
589 error = copyin(SCARG(uap, ts), &ts, sizeof(ts));
590 if (error != 0)
591 return error;
592 tsp = &ts;
593 }
594
595 if (SCARG(uap, unpark) != 0) {
596 error = lwp_unpark(SCARG(uap, unpark), SCARG(uap, unparkhint));
597 if (error != 0)
598 return error;
599 }
600
601 return lwp_park(tsp, SCARG(uap, hint));
602 }
603
604 int
605 sys__lwp_unpark(struct lwp *l, void *v, register_t *retval)
606 {
607 struct sys__lwp_unpark_args /* {
608 syscallarg(lwpid_t) target;
609 syscallarg(const void *) hint;
610 } */ *uap = v;
611
612 return lwp_unpark(SCARG(uap, target), SCARG(uap, hint));
613 }
614
615 int
616 sys__lwp_unpark_all(struct lwp *l, void *v, register_t *retval)
617 {
618 struct sys__lwp_unpark_all_args /* {
619 syscallarg(const lwpid_t *) targets;
620 syscallarg(size_t) ntargets;
621 syscallarg(const void *) hint;
622 } */ *uap = v;
623 struct proc *p;
624 struct lwp *t;
625 sleepq_t *sq;
626 wchan_t wchan;
627 lwpid_t targets[32], *tp, *tpp, *tmax, target;
628 int swapin, error;
629 u_int ntargets;
630 size_t sz;
631
632 p = l->l_proc;
633 ntargets = SCARG(uap, ntargets);
634
635 if (SCARG(uap, targets) == NULL) {
636 /*
637 * Let the caller know how much we are willing to do, and
638 * let it unpark the LWPs in blocks.
639 */
640 *retval = LWP_UNPARK_MAX;
641 return 0;
642 }
643 if (ntargets > LWP_UNPARK_MAX || ntargets == 0)
644 return EINVAL;
645
646 /*
647 * Copy in the target array. If it's a small number of LWPs, then
648 * place the numbers on the stack.
649 */
650 sz = sizeof(target) * ntargets;
651 if (sz <= sizeof(targets))
652 tp = targets;
653 else {
654 KERNEL_LOCK(1, l); /* XXXSMP */
655 tp = kmem_alloc(sz, KM_SLEEP);
656 KERNEL_UNLOCK_ONE(l); /* XXXSMP */
657 if (tp == NULL)
658 return ENOMEM;
659 }
660 error = copyin(SCARG(uap, targets), tp, sz);
661 if (error != 0) {
662 if (tp != targets) {
663 KERNEL_LOCK(1, l); /* XXXSMP */
664 kmem_free(tp, sz);
665 KERNEL_UNLOCK_ONE(l); /* XXXSMP */
666 }
667 return error;
668 }
669
670 swapin = 0;
671 wchan = lwp_park_wchan(p, SCARG(uap, hint));
672 sq = sleeptab_lookup(&lwp_park_tab, wchan);
673
674 for (tmax = tp + ntargets, tpp = tp; tpp < tmax; tpp++) {
675 target = *tpp;
676
677 /*
678 * Easy case: search for the LWP on the sleep queue. If
679 * it's parked, remove it from the queue and set running.
680 */
681 TAILQ_FOREACH(t, &sq->sq_queue, l_sleepchain)
682 if (t->l_proc == p && t->l_lid == target)
683 break;
684
685 if (t != NULL) {
686 swapin |= sleepq_remove(sq, t);
687 continue;
688 }
689
690 /*
691 * The LWP hasn't parked yet. Take the hit and
692 * mark the operation as pending.
693 */
694 sleepq_unlock(sq);
695 mutex_enter(&p->p_smutex);
696 if ((t = lwp_find(p, target)) == NULL) {
697 mutex_exit(&p->p_smutex);
698 sleepq_lock(sq);
699 continue;
700 }
701 lwp_lock(t);
702
703 /*
704 * It may not have parked yet, we may have raced, or
705 * it is parked on a different user sync object.
706 */
707 if (t->l_syncobj == &lwp_park_sobj) {
708 /* Releases the LWP lock. */
709 lwp_unsleep(t);
710 } else {
711 /*
712 * Set the operation pending. The next call to
713 * _lwp_park will return early.
714 */
715 t->l_flag |= LW_UNPARKED;
716 lwp_unlock(t);
717 }
718
719 mutex_exit(&p->p_smutex);
720 sleepq_lock(sq);
721 }
722
723 sleepq_unlock(sq);
724 if (tp != targets) {
725 KERNEL_LOCK(1, l); /* XXXSMP */
726 kmem_free(tp, sz);
727 KERNEL_UNLOCK_ONE(l); /* XXXSMP */
728 }
729 if (swapin)
730 uvm_kick_scheduler();
731
732 return 0;
733 }
734