1 /* $NetBSD: sys_lwp.c,v 1.47 2009/10/22 13:12:47 rmind Exp $ */
4 * Copyright (c) 2001, 2006, 2007, 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nathan J. Williams, and Andrew Doran.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Lightweight process (LWP) system calls. See kern_lwp.c for a description
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: sys_lwp.c,v 1.47 2009/10/22 13:12:47 rmind Exp $");
40 #include <sys/param.h>
41 #include <sys/systm.h>
44 #include <sys/types.h>
45 #include <sys/syscallargs.h>
46 #include <sys/kauth.h>
48 #include <sys/sleepq.h>
49 #include <sys/lwpctl.h>
52 #include <uvm/uvm_extern.h>
56 #define LWP_UNPARK_MAX 1024
58 static syncobj_t lwp_park_sobj
= {
66 static sleeptab_t lwp_park_tab
;
71 sleeptab_init(&lwp_park_tab
);
75 sys__lwp_create(struct lwp
*l
, const struct sys__lwp_create_args
*uap
,
79 syscallarg(const ucontext_t *) ucp;
80 syscallarg(u_long) flags;
81 syscallarg(lwpid_t *) new_lwp;
83 struct proc
*p
= l
->l_proc
;
90 mutex_enter(p
->p_lock
);
91 if ((p
->p_sflag
& (PS_SA
| PS_WEXIT
)) != 0 || p
->p_sa
!= NULL
) {
92 mutex_exit(p
->p_lock
);
95 mutex_exit(p
->p_lock
);
98 newuc
= pool_get(&lwp_uc_pool
, PR_WAITOK
);
100 error
= copyin(SCARG(uap
, ucp
), newuc
, p
->p_emul
->e_ucsize
);
102 pool_put(&lwp_uc_pool
, newuc
);
106 /* XXX check against resource limits */
108 uaddr
= uvm_uarea_alloc();
109 if (__predict_false(uaddr
== 0)) {
110 pool_put(&lwp_uc_pool
, newuc
);
114 error
= lwp_create(l
, p
, uaddr
, SCARG(uap
, flags
) & LWP_DETACHED
,
115 NULL
, 0, p
->p_emul
->e_startlwp
, newuc
, &l2
, l
->l_class
);
116 if (__predict_false(error
)) {
117 uvm_uarea_free(uaddr
);
118 pool_put(&lwp_uc_pool
, newuc
);
123 error
= copyout(&lid
, SCARG(uap
, new_lwp
), sizeof(lid
));
126 pool_put(&lwp_uc_pool
, newuc
);
131 * Set the new LWP running, unless the caller has requested that
132 * it be created in suspended state. If the process is stopping,
133 * then the LWP is created stopped.
135 mutex_enter(p
->p_lock
);
137 if ((SCARG(uap
, flags
) & LWP_SUSPENDED
) == 0 &&
138 (l
->l_flag
& (LW_WREBOOT
| LW_WSUSPEND
| LW_WEXIT
)) == 0) {
139 if (p
->p_stat
== SSTOP
|| (p
->p_sflag
& PS_STOPPING
) != 0)
142 KASSERT(lwp_locked(l2
, l2
->l_cpu
->ci_schedstate
.spc_mutex
));
145 sched_enqueue(l2
, false);
149 l2
->l_stat
= LSSUSPENDED
;
150 lwp_unlock_to(l2
, l2
->l_cpu
->ci_schedstate
.spc_lwplock
);
152 mutex_exit(p
->p_lock
);
158 sys__lwp_exit(struct lwp
*l
, const void *v
, register_t
*retval
)
166 sys__lwp_self(struct lwp
*l
, const void *v
, register_t
*retval
)
174 sys__lwp_getprivate(struct lwp
*l
, const void *v
, register_t
*retval
)
177 *retval
= (uintptr_t)l
->l_private
;
182 sys__lwp_setprivate(struct lwp
*l
, const struct sys__lwp_setprivate_args
*uap
,
186 syscallarg(void *) ptr;
189 l
->l_private
= SCARG(uap
, ptr
);
190 #ifdef __HAVE_CPU_LWP_SETPRIVATE
191 cpu_lwp_setprivate(l
, SCARG(uap
, ptr
));
198 sys__lwp_suspend(struct lwp
*l
, const struct sys__lwp_suspend_args
*uap
,
202 syscallarg(lwpid_t) target;
204 struct proc
*p
= l
->l_proc
;
208 mutex_enter(p
->p_lock
);
211 if ((p
->p_sflag
& PS_SA
) != 0 || p
->p_sa
!= NULL
) {
212 mutex_exit(p
->p_lock
);
217 if ((t
= lwp_find(p
, SCARG(uap
, target
))) == NULL
) {
218 mutex_exit(p
->p_lock
);
223 * Check for deadlock, which is only possible when we're suspending
224 * ourself. XXX There is a short race here, as p_nrlwps is only
225 * incremented when an LWP suspends itself on the kernel/user
226 * boundary. It's still possible to kill -9 the process so we
227 * don't bother checking further.
230 if ((t
== l
&& p
->p_nrlwps
== 1) ||
231 (l
->l_flag
& (LW_WCORE
| LW_WEXIT
)) != 0) {
233 mutex_exit(p
->p_lock
);
238 * Suspend the LWP. XXX If it's on a different CPU, we should wait
239 * for it to be preempted, where it will put itself to sleep.
241 * Suspension of the current LWP will happen on return to userspace.
243 error
= lwp_suspend(l
, t
);
245 mutex_exit(p
->p_lock
);
252 * o target LWP suspended
253 * o target LWP not suspended and L_WSUSPEND clear
254 * o target LWP exited
257 error
= cv_wait_sig(&p
->p_lwpcv
, p
->p_lock
);
262 if (lwp_find(p
, SCARG(uap
, target
)) == NULL
) {
266 if ((l
->l_flag
| t
->l_flag
) & (LW_WCORE
| LW_WEXIT
)) {
270 if (t
->l_stat
== LSSUSPENDED
||
271 (t
->l_flag
& LW_WSUSPEND
) == 0)
274 mutex_exit(p
->p_lock
);
280 sys__lwp_continue(struct lwp
*l
, const struct sys__lwp_continue_args
*uap
,
284 syscallarg(lwpid_t) target;
287 struct proc
*p
= l
->l_proc
;
292 mutex_enter(p
->p_lock
);
293 if ((t
= lwp_find(p
, SCARG(uap
, target
))) == NULL
) {
294 mutex_exit(p
->p_lock
);
300 mutex_exit(p
->p_lock
);
306 sys__lwp_wakeup(struct lwp
*l
, const struct sys__lwp_wakeup_args
*uap
,
310 syscallarg(lwpid_t) target;
317 mutex_enter(p
->p_lock
);
319 if ((t
= lwp_find(p
, SCARG(uap
, target
))) == NULL
) {
320 mutex_exit(p
->p_lock
);
325 t
->l_flag
|= (LW_CANCELLED
| LW_UNPARKED
);
327 if (t
->l_stat
!= LSSLEEP
) {
330 } else if ((t
->l_flag
& LW_SINTR
) == 0) {
334 /* Wake it up. lwp_unsleep() will release the LWP lock. */
335 lwp_unsleep(t
, true);
339 mutex_exit(p
->p_lock
);
345 sys__lwp_wait(struct lwp
*l
, const struct sys__lwp_wait_args
*uap
,
349 syscallarg(lwpid_t) wait_for;
350 syscallarg(lwpid_t *) departed;
352 struct proc
*p
= l
->l_proc
;
356 mutex_enter(p
->p_lock
);
357 error
= lwp_wait1(l
, SCARG(uap
, wait_for
), &dep
, 0);
358 mutex_exit(p
->p_lock
);
363 if (SCARG(uap
, departed
)) {
364 error
= copyout(&dep
, SCARG(uap
, departed
), sizeof(dep
));
373 sys__lwp_kill(struct lwp
*l
, const struct sys__lwp_kill_args
*uap
,
377 syscallarg(lwpid_t) target;
378 syscallarg(int) signo;
380 struct proc
*p
= l
->l_proc
;
383 int signo
= SCARG(uap
, signo
);
386 if ((u_int
)signo
>= NSIG
)
390 ksi
.ksi_signo
= signo
;
391 ksi
.ksi_code
= SI_LWP
;
392 ksi
.ksi_pid
= p
->p_pid
;
393 ksi
.ksi_uid
= kauth_cred_geteuid(l
->l_cred
);
394 ksi
.ksi_lid
= SCARG(uap
, target
);
396 mutex_enter(proc_lock
);
397 mutex_enter(p
->p_lock
);
398 if ((t
= lwp_find(p
, ksi
.ksi_lid
)) == NULL
)
402 mutex_exit(p
->p_lock
);
403 mutex_exit(proc_lock
);
409 sys__lwp_detach(struct lwp
*l
, const struct sys__lwp_detach_args
*uap
,
413 syscallarg(lwpid_t) target;
420 target
= SCARG(uap
, target
);
423 mutex_enter(p
->p_lock
);
425 if (l
->l_lid
== target
)
429 * We can't use lwp_find() here because the target might
432 LIST_FOREACH(t
, &p
->p_lwps
, l_sibling
)
433 if (t
->l_lid
== target
)
438 * If the LWP is already detached, there's nothing to do.
439 * If it's a zombie, we need to clean up after it. LSZOMB
440 * is visible with the proc mutex held.
442 * After we have detached or released the LWP, kick any
443 * other LWPs that may be sitting in _lwp_wait(), waiting
444 * for the target LWP to exit.
446 if (t
!= NULL
&& t
->l_stat
!= LSIDL
) {
447 if ((t
->l_prflag
& LPR_DETACHED
) == 0) {
449 t
->l_prflag
|= LPR_DETACHED
;
450 if (t
->l_stat
== LSZOMB
) {
451 /* Releases proc mutex. */
452 lwp_free(t
, false, false);
458 * Have any LWPs sleeping in lwp_wait() recheck
461 cv_broadcast(&p
->p_lwpcv
);
467 mutex_exit(p
->p_lock
);
472 static inline wchan_t
473 lwp_park_wchan(struct proc
*p
, const void *hint
)
476 return (wchan_t
)((uintptr_t)p
^ (uintptr_t)hint
);
480 lwp_unpark(lwpid_t target
, const void *hint
)
489 * Easy case: search for the LWP on the sleep queue. If
490 * it's parked, remove it from the queue and set running.
493 wchan
= lwp_park_wchan(p
, hint
);
494 sq
= sleeptab_lookup(&lwp_park_tab
, wchan
, &mp
);
496 TAILQ_FOREACH(t
, sq
, l_sleepchain
)
497 if (t
->l_proc
== p
&& t
->l_lid
== target
)
500 if (__predict_true(t
!= NULL
)) {
501 sleepq_remove(sq
, t
);
507 * The LWP hasn't parked yet. Take the hit and mark the
508 * operation as pending.
512 mutex_enter(p
->p_lock
);
513 if ((t
= lwp_find(p
, target
)) == NULL
) {
514 mutex_exit(p
->p_lock
);
519 * It may not have parked yet, we may have raced, or it
520 * is parked on a different user sync object.
523 if (t
->l_syncobj
== &lwp_park_sobj
) {
524 /* Releases the LWP lock. */
525 lwp_unsleep(t
, true);
528 * Set the operation pending. The next call to _lwp_park
531 t
->l_flag
|= LW_UNPARKED
;
535 mutex_exit(p
->p_lock
);
540 lwp_park(struct timespec
*ts
, const void *hint
)
548 /* Fix up the given timeout value. */
550 error
= abstimeout2timo(ts
, &timo
);
559 /* Find and lock the sleep queue. */
561 wchan
= lwp_park_wchan(l
->l_proc
, hint
);
562 sq
= sleeptab_lookup(&lwp_park_tab
, wchan
, &mp
);
565 * Before going the full route and blocking, check to see if an
566 * unpark op is pending.
569 if ((l
->l_flag
& (LW_CANCELLED
| LW_UNPARKED
)) != 0) {
570 l
->l_flag
&= ~(LW_CANCELLED
| LW_UNPARKED
);
575 lwp_unlock_to(l
, mp
);
577 sleepq_enqueue(sq
, wchan
, "parked", &lwp_park_sobj
);
578 error
= sleepq_block(timo
, true);
594 * 'park' an LWP waiting on a user-level synchronisation object. The LWP
595 * will remain parked until another LWP in the same process calls in and
596 * requests that it be unparked.
599 sys____lwp_park50(struct lwp
*l
, const struct sys____lwp_park50_args
*uap
,
603 syscallarg(const struct timespec *) ts;
604 syscallarg(lwpid_t) unpark;
605 syscallarg(const void *) hint;
606 syscallarg(const void *) unparkhint;
608 struct timespec ts
, *tsp
;
611 if (SCARG(uap
, ts
) == NULL
)
614 error
= copyin(SCARG(uap
, ts
), &ts
, sizeof(ts
));
620 if (SCARG(uap
, unpark
) != 0) {
621 error
= lwp_unpark(SCARG(uap
, unpark
), SCARG(uap
, unparkhint
));
626 return lwp_park(tsp
, SCARG(uap
, hint
));
630 sys__lwp_unpark(struct lwp
*l
, const struct sys__lwp_unpark_args
*uap
,
634 syscallarg(lwpid_t) target;
635 syscallarg(const void *) hint;
638 return lwp_unpark(SCARG(uap
, target
), SCARG(uap
, hint
));
642 sys__lwp_unpark_all(struct lwp
*l
, const struct sys__lwp_unpark_all_args
*uap
,
646 syscallarg(const lwpid_t *) targets;
647 syscallarg(size_t) ntargets;
648 syscallarg(const void *) hint;
654 lwpid_t targets
[32], *tp
, *tpp
, *tmax
, target
;
661 ntargets
= SCARG(uap
, ntargets
);
663 if (SCARG(uap
, targets
) == NULL
) {
665 * Let the caller know how much we are willing to do, and
666 * let it unpark the LWPs in blocks.
668 *retval
= LWP_UNPARK_MAX
;
671 if (ntargets
> LWP_UNPARK_MAX
|| ntargets
== 0)
675 * Copy in the target array. If it's a small number of LWPs, then
676 * place the numbers on the stack.
678 sz
= sizeof(target
) * ntargets
;
679 if (sz
<= sizeof(targets
))
682 tp
= kmem_alloc(sz
, KM_SLEEP
);
686 error
= copyin(SCARG(uap
, targets
), tp
, sz
);
694 wchan
= lwp_park_wchan(p
, SCARG(uap
, hint
));
695 sq
= sleeptab_lookup(&lwp_park_tab
, wchan
, &mp
);
697 for (tmax
= tp
+ ntargets
, tpp
= tp
; tpp
< tmax
; tpp
++) {
701 * Easy case: search for the LWP on the sleep queue. If
702 * it's parked, remove it from the queue and set running.
704 TAILQ_FOREACH(t
, sq
, l_sleepchain
)
705 if (t
->l_proc
== p
&& t
->l_lid
== target
)
709 sleepq_remove(sq
, t
);
714 * The LWP hasn't parked yet. Take the hit and
715 * mark the operation as pending.
718 mutex_enter(p
->p_lock
);
719 if ((t
= lwp_find(p
, target
)) == NULL
) {
720 mutex_exit(p
->p_lock
);
721 mutex_spin_enter(mp
);
727 * It may not have parked yet, we may have raced, or
728 * it is parked on a different user sync object.
730 if (t
->l_syncobj
== &lwp_park_sobj
) {
731 /* Releases the LWP lock. */
732 lwp_unsleep(t
, true);
735 * Set the operation pending. The next call to
736 * _lwp_park will return early.
738 t
->l_flag
|= LW_UNPARKED
;
742 mutex_exit(p
->p_lock
);
743 mutex_spin_enter(mp
);
754 sys__lwp_setname(struct lwp
*l
, const struct sys__lwp_setname_args
*uap
,
758 syscallarg(lwpid_t) target;
759 syscallarg(const char *) name;
767 if ((target
= SCARG(uap
, target
)) == 0)
770 name
= kmem_alloc(MAXCOMLEN
, KM_SLEEP
);
773 error
= copyinstr(SCARG(uap
, name
), name
, MAXCOMLEN
, NULL
);
777 name
[MAXCOMLEN
- 1] = '\0';
780 kmem_free(name
, MAXCOMLEN
);
785 mutex_enter(p
->p_lock
);
786 if ((t
= lwp_find(p
, target
)) == NULL
) {
787 mutex_exit(p
->p_lock
);
788 kmem_free(name
, MAXCOMLEN
);
795 mutex_exit(p
->p_lock
);
798 kmem_free(oname
, MAXCOMLEN
);
804 sys__lwp_getname(struct lwp
*l
, const struct sys__lwp_getname_args
*uap
,
808 syscallarg(lwpid_t) target;
809 syscallarg(char *) name;
810 syscallarg(size_t) len;
812 char name
[MAXCOMLEN
];
817 if ((target
= SCARG(uap
, target
)) == 0)
821 mutex_enter(p
->p_lock
);
822 if ((t
= lwp_find(p
, target
)) == NULL
) {
823 mutex_exit(p
->p_lock
);
827 if (t
->l_name
== NULL
)
830 strcpy(name
, t
->l_name
);
832 mutex_exit(p
->p_lock
);
834 return copyoutstr(name
, SCARG(uap
, name
), SCARG(uap
, len
), NULL
);
838 sys__lwp_ctl(struct lwp
*l
, const struct sys__lwp_ctl_args
*uap
,
842 syscallarg(int) features;
843 syscallarg(struct lwpctl **) address;
848 features
= SCARG(uap
, features
);
849 features
&= ~(LWPCTL_FEATURE_CURCPU
| LWPCTL_FEATURE_PCTR
);
852 if ((error
= lwp_ctl_alloc(&vaddr
)) != 0)
854 return copyout(&vaddr
, SCARG(uap
, address
), sizeof(void *));