/*-
* Copyright (c) 2001, 2006, 2007, 2008, 2019, 2020, 2023
* The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Nathan J. Williams, and Andrew Doran.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Lightweight process (LWP) system calls. See kern_lwp.c for a description
* of LWPs.
*/
/* If the process is traced, report lwp creation to a debugger */
if ((p->p_slflag & (PSL_TRACED|PSL_TRACELWP_CREATE)) ==
(PSL_TRACED|PSL_TRACELWP_CREATE)) {
/* Paranoid check */
mutex_enter(&proc_lock);
if ((p->p_slflag & (PSL_TRACED|PSL_TRACELWP_CREATE)) !=
(PSL_TRACED|PSL_TRACELWP_CREATE)) {
mutex_exit(&proc_lock);
return;
}
/*
* Check for deadlock, which is only possible when we're suspending
* ourself. XXX There is a short race here, as p_nrlwps is only
* incremented when an LWP suspends itself on the kernel/user
* boundary. It's still possible to kill -9 the process so we
* don't bother checking further.
*/
lwp_lock(t);
if ((t == l && p->p_nrlwps == 1) ||
(l->l_flag & (LW_WCORE | LW_WEXIT)) != 0) {
lwp_unlock(t);
mutex_exit(p->p_lock);
return EDEADLK;
}
/*
* Suspend the LWP. XXX If it's on a different CPU, we should wait
* for it to be preempted, where it will put itself to sleep.
*
* Suspension of the current LWP will happen on return to userspace.
*/
error = lwp_suspend(l, t);
if (error) {
mutex_exit(p->p_lock);
return error;
}
/*
* Wait for:
* o process exiting
* o target LWP suspended
* o target LWP not suspended and L_WSUSPEND clear
* o target LWP exited
*/
for (;;) {
error = cv_wait_sig(&p->p_lwpcv, p->p_lock);
if (error) {
error = ERESTART;
break;
}
if (lwp_find(p, SCARG(uap, target)) == NULL) {
error = ESRCH;
break;
}
if ((l->l_flag | t->l_flag) & (LW_WCORE | LW_WEXIT)) {
error = ERESTART;
break;
}
if (t->l_stat == LSSUSPENDED ||
(t->l_flag & LW_WSUSPEND) == 0)
break;
}
mutex_exit(p->p_lock);
if (l->l_lid == target)
t = l;
else {
/*
* We can't use lwp_find() here because the target might
* be a zombie.
*/
t = proc_find_lwp(p, target);
KASSERT(t == NULL || t->l_lid == target);
}
/*
* If the LWP is already detached, there's nothing to do.
* If it's a zombie, we need to clean up after it. LSZOMB
* is visible with the proc mutex held.
*
* After we have detached or released the LWP, kick any
* other LWPs that may be sitting in _lwp_wait(), waiting
* for the target LWP to exit.
*/
if (t != NULL && t->l_stat != LSIDL) {
if ((t->l_prflag & LPR_DETACHED) == 0) {
p->p_ndlwps++;
t->l_prflag |= LPR_DETACHED;
if (t->l_stat == LSZOMB) {
/* Releases proc mutex. */
lwp_free(t, false, false);
return 0;
}
error = 0;
/*
* Have any LWPs sleeping in lwp_wait() recheck
* for deadlock.
*/
cv_broadcast(&p->p_lwpcv);
} else
error = EINVAL;
} else
error = ESRCH;
mutex_exit(p->p_lock);
return error;
}
int
lwp_unpark(const lwpid_t *tp, const u_int ntargets)
{
u_int target;
kmutex_t *mp;
int error, s;
proc_t *p;
lwp_t *t;
p = curproc;
error = 0;
s = pserialize_read_enter();
for (target = 0; target < ntargets; target++) {
t = proc_find_lwp_unlocked(p, tp[target]);
if (__predict_false(t == NULL)) {
error = ESRCH;
continue;
}
KASSERT(lwp_locked(t, NULL));
if (__predict_true(t->l_syncobj == &lwp_park_syncobj)) {
/* As expected it's parked, so wake it up. */
mp = t->l_mutex;
sleepq_remove(NULL, t, true);
mutex_spin_exit(mp);
} else if (__predict_false(t->l_stat == LSZOMB)) {
lwp_unlock(t);
error = ESRCH;
} else {
/*
* It hasn't parked yet because the wakeup side won
* the race, or something else has happened to make
* the thread not park. Why doesn't really matter.
* Set the operation pending, so that the next call
* to _lwp_park() in the LWP returns early. If it
* turns out to be a spurious wakeup, no harm done.
*/
t->l_flag |= LW_UNPARKED;
lwp_unlock(t);
}
}
pserialize_read_exit(s);
return error;
}
int
lwp_park(clockid_t clock_id, int flags, struct timespec *ts)
{
int timo, error;
struct timespec start;
lwp_t *l;
bool timeremain = !(flags & TIMER_ABSTIME) && ts;
/*
* Before going the full route and blocking, check to see if an
* unpark op is pending.
*/
l = curlwp;
lwp_lock(l);
if ((l->l_flag & (LW_CANCELLED | LW_UNPARKED)) != 0) {
l->l_flag &= ~(LW_CANCELLED | LW_UNPARKED);
lwp_unlock(l);
return EALREADY;
}
sleepq_enqueue(NULL, l, "parked", &lwp_park_syncobj, true);
error = sleepq_block(timo, true, &lwp_park_syncobj, 0);
switch (error) {
case EWOULDBLOCK:
error = ETIMEDOUT;
if (timeremain)
memset(ts, 0, sizeof(*ts));
break;
case ERESTART:
error = EINTR;
/*FALLTHROUGH*/
default:
if (timeremain)
clock_timeleft(clock_id, ts, &start);
break;
}
return error;
}
/*
* 'park' an LWP waiting on a user-level synchronisation object. The LWP
* will remain parked until another LWP in the same process calls in and
* requests that it be unparked.
*/
int
sys____lwp_park60(struct lwp *l, const struct sys____lwp_park60_args *uap,
register_t *retval)
{
/* {
syscallarg(clockid_t) clock_id;
syscallarg(int) flags;
syscallarg(struct timespec *) ts;
syscallarg(lwpid_t) unpark;
syscallarg(const void *) hint;
syscallarg(const void *) unparkhint;
} */
struct timespec ts, *tsp;
int error;
ntargets = SCARG(uap, ntargets);
if (SCARG(uap, targets) == NULL) {
/*
* Let the caller know how much we are willing to do, and
* let it unpark the LWPs in blocks.
*/
*retval = LWP_UNPARK_MAX;
return 0;
}
if (ntargets > LWP_UNPARK_MAX || ntargets == 0)
return EINVAL;
/*
* Copy in the target array. If it's a small number of LWPs, then
* place the numbers on the stack.
*/
sz = sizeof(lwpid_t) * ntargets;
if (sz <= sizeof(targets))
tp = targets;
else
tp = kmem_alloc(sz, KM_SLEEP);
error = copyin(SCARG(uap, targets), tp, sz);
if (error != 0) {
if (tp != targets) {
kmem_free(tp, sz);
}
return error;
}
error = lwp_unpark(tp, ntargets);
if (tp != targets)
kmem_free(tp, sz);
return error;
}