2005-01-06 23:35:40 +00:00
|
|
|
/*-
|
2003-04-01 00:30:30 +00:00
|
|
|
* Copyright (c) 2003, Jeffrey Roberson <jeff@freebsd.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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 unmodified, 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 AUTHOR ``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 AUTHOR 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.
|
|
|
|
*/
|
|
|
|
|
2003-06-11 00:56:59 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2006-09-22 15:04:28 +00:00
|
|
|
#include "opt_compat.h"
|
2006-07-13 06:26:43 +00:00
|
|
|
#include "opt_posix.h"
|
2003-04-01 00:30:30 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/mutex.h>
|
2006-11-06 13:42:10 +00:00
|
|
|
#include <sys/priv.h>
|
2003-04-01 00:30:30 +00:00
|
|
|
#include <sys/proc.h>
|
2006-11-11 16:46:31 +00:00
|
|
|
#include <sys/posix4.h>
|
2011-03-31 19:22:11 +00:00
|
|
|
#include <sys/racct.h>
|
2003-04-01 00:30:30 +00:00
|
|
|
#include <sys/resourcevar.h>
|
2010-10-23 13:16:39 +00:00
|
|
|
#include <sys/rwlock.h>
|
2003-04-11 19:24:37 +00:00
|
|
|
#include <sys/sched.h>
|
2004-09-07 06:33:39 +00:00
|
|
|
#include <sys/sysctl.h>
|
2004-09-05 02:09:54 +00:00
|
|
|
#include <sys/smp.h>
|
2007-06-07 19:45:19 +00:00
|
|
|
#include <sys/syscallsubr.h>
|
2003-04-01 00:30:30 +00:00
|
|
|
#include <sys/sysent.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/sysproto.h>
|
|
|
|
#include <sys/signalvar.h>
|
2011-02-23 13:50:24 +00:00
|
|
|
#include <sys/sysctl.h>
|
2003-04-01 00:30:30 +00:00
|
|
|
#include <sys/ucontext.h>
|
|
|
|
#include <sys/thr.h>
|
2006-07-11 05:34:35 +00:00
|
|
|
#include <sys/rtprio.h>
|
2005-10-26 06:55:46 +00:00
|
|
|
#include <sys/umtx.h>
|
|
|
|
#include <sys/limits.h>
|
2003-04-01 00:30:30 +00:00
|
|
|
|
|
|
|
#include <machine/frame.h>
|
|
|
|
|
2007-08-16 05:26:42 +00:00
|
|
|
#include <security/audit/audit.h>
|
|
|
|
|
2011-11-07 15:43:11 +00:00
|
|
|
static SYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW, 0,
|
|
|
|
"thread allocation");
|
2011-02-23 13:50:24 +00:00
|
|
|
|
|
|
|
static int max_threads_per_proc = 1500;
|
|
|
|
SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_per_proc, CTLFLAG_RW,
|
2011-12-13 00:38:50 +00:00
|
|
|
&max_threads_per_proc, 0, "Limit on threads per proc");
|
2011-02-23 13:50:24 +00:00
|
|
|
|
|
|
|
static int max_threads_hits;
|
|
|
|
SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_hits, CTLFLAG_RD,
|
2011-12-13 00:38:50 +00:00
|
|
|
&max_threads_hits, 0, "kern.threads.max_threads_per_proc hit count");
|
2011-02-23 13:50:24 +00:00
|
|
|
|
2010-03-11 14:49:06 +00:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
2006-09-22 15:04:28 +00:00
|
|
|
|
|
|
|
static inline int
|
|
|
|
suword_lwpid(void *addr, lwpid_t lwpid)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2008-11-22 12:36:15 +00:00
|
|
|
if (SV_CURPROC_FLAG(SV_LP64))
|
2006-09-22 15:04:28 +00:00
|
|
|
error = suword(addr, lwpid);
|
|
|
|
else
|
|
|
|
error = suword32(addr, lwpid);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
#define suword_lwpid suword
|
|
|
|
#endif
|
|
|
|
|
2005-04-23 02:36:07 +00:00
|
|
|
static int create_thread(struct thread *td, mcontext_t *ctx,
|
|
|
|
void (*start_func)(void *), void *arg,
|
|
|
|
char *stack_base, size_t stack_size,
|
|
|
|
char *tls_base,
|
|
|
|
long *child_tid, long *parent_tid,
|
2006-09-21 04:18:46 +00:00
|
|
|
int flags, struct rtprio *rtp);
|
2005-04-23 02:36:07 +00:00
|
|
|
|
2003-04-01 00:30:30 +00:00
|
|
|
/*
|
|
|
|
* System call interface.
|
|
|
|
*/
|
|
|
|
int
|
2011-09-16 13:58:51 +00:00
|
|
|
sys_thr_create(struct thread *td, struct thr_create_args *uap)
|
2004-07-02 00:40:07 +00:00
|
|
|
/* ucontext_t *ctx, long *id, int flags */
|
2003-04-01 00:30:30 +00:00
|
|
|
{
|
|
|
|
ucontext_t ctx;
|
|
|
|
int error;
|
2005-04-23 02:36:07 +00:00
|
|
|
|
|
|
|
if ((error = copyin(uap->ctx, &ctx, sizeof(ctx))))
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
error = create_thread(td, &ctx.uc_mcontext, NULL, NULL,
|
2006-07-11 05:34:35 +00:00
|
|
|
NULL, 0, NULL, uap->id, NULL, uap->flags, NULL);
|
2005-04-23 02:36:07 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-09-16 13:58:51 +00:00
|
|
|
sys_thr_new(struct thread *td, struct thr_new_args *uap)
|
2005-04-23 02:36:07 +00:00
|
|
|
/* struct thr_param * */
|
|
|
|
{
|
|
|
|
struct thr_param param;
|
|
|
|
int error;
|
|
|
|
|
2006-09-22 15:04:28 +00:00
|
|
|
if (uap->param_size < 0 || uap->param_size > sizeof(param))
|
2005-04-23 02:36:07 +00:00
|
|
|
return (EINVAL);
|
2006-09-21 04:18:46 +00:00
|
|
|
bzero(¶m, sizeof(param));
|
2006-09-22 15:04:28 +00:00
|
|
|
if ((error = copyin(uap->param, ¶m, uap->param_size)))
|
2005-04-23 02:36:07 +00:00
|
|
|
return (error);
|
2006-09-22 15:04:28 +00:00
|
|
|
return (kern_thr_new(td, ¶m));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
kern_thr_new(struct thread *td, struct thr_param *param)
|
|
|
|
{
|
|
|
|
struct rtprio rtp, *rtpp;
|
|
|
|
int error;
|
|
|
|
|
2006-09-21 04:18:46 +00:00
|
|
|
rtpp = NULL;
|
2006-09-22 15:04:28 +00:00
|
|
|
if (param->rtp != 0) {
|
|
|
|
error = copyin(param->rtp, &rtp, sizeof(struct rtprio));
|
2008-10-13 21:04:52 +00:00
|
|
|
if (error)
|
|
|
|
return (error);
|
2006-09-21 04:18:46 +00:00
|
|
|
rtpp = &rtp;
|
2006-07-11 05:34:35 +00:00
|
|
|
}
|
2006-09-22 15:04:28 +00:00
|
|
|
error = create_thread(td, NULL, param->start_func, param->arg,
|
|
|
|
param->stack_base, param->stack_size, param->tls_base,
|
|
|
|
param->child_tid, param->parent_tid, param->flags,
|
2006-09-21 04:18:46 +00:00
|
|
|
rtpp);
|
2005-04-23 02:36:07 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
create_thread(struct thread *td, mcontext_t *ctx,
|
|
|
|
void (*start_func)(void *), void *arg,
|
|
|
|
char *stack_base, size_t stack_size,
|
|
|
|
char *tls_base,
|
|
|
|
long *child_tid, long *parent_tid,
|
2006-09-21 04:18:46 +00:00
|
|
|
int flags, struct rtprio *rtp)
|
2005-04-23 02:36:07 +00:00
|
|
|
{
|
|
|
|
stack_t stack;
|
|
|
|
struct thread *newtd;
|
2004-09-05 02:09:54 +00:00
|
|
|
struct proc *p;
|
2006-07-10 23:14:07 +00:00
|
|
|
int error;
|
2003-04-01 00:30:30 +00:00
|
|
|
|
2004-09-05 02:09:54 +00:00
|
|
|
p = td->td_proc;
|
2003-04-01 00:30:30 +00:00
|
|
|
|
2006-09-21 04:18:46 +00:00
|
|
|
if (rtp != NULL) {
|
|
|
|
switch(rtp->type) {
|
|
|
|
case RTP_PRIO_REALTIME:
|
|
|
|
case RTP_PRIO_FIFO:
|
2006-07-11 08:19:57 +00:00
|
|
|
/* Only root can set scheduler policy */
|
2006-11-06 13:42:10 +00:00
|
|
|
if (priv_check(td, PRIV_SCHED_SETPOLICY) != 0)
|
2006-07-11 05:34:35 +00:00
|
|
|
return (EPERM);
|
2006-09-21 04:18:46 +00:00
|
|
|
if (rtp->prio > RTP_PRIO_MAX)
|
2006-07-11 05:34:35 +00:00
|
|
|
return (EINVAL);
|
2006-07-11 08:19:57 +00:00
|
|
|
break;
|
2006-09-21 04:18:46 +00:00
|
|
|
case RTP_PRIO_NORMAL:
|
|
|
|
rtp->prio = 0;
|
2006-07-11 08:19:57 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (EINVAL);
|
2006-07-11 05:34:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-06 20:06:44 +00:00
|
|
|
#ifdef RACCT
|
2015-04-29 10:23:02 +00:00
|
|
|
if (racct_enable) {
|
|
|
|
PROC_LOCK(p);
|
|
|
|
error = racct_add(p, RACCT_NTHR, 1);
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
if (error != 0)
|
|
|
|
return (EPROCLIM);
|
|
|
|
}
|
2011-07-06 20:06:44 +00:00
|
|
|
#endif
|
2011-03-31 19:22:11 +00:00
|
|
|
|
2006-12-06 06:34:57 +00:00
|
|
|
/* Initialize our td */
|
2015-05-24 14:37:45 +00:00
|
|
|
error = kern_thr_alloc(p, 0, &newtd);
|
|
|
|
if (error)
|
2011-03-31 19:22:11 +00:00
|
|
|
goto fail;
|
2005-04-23 02:36:07 +00:00
|
|
|
|
2011-12-09 17:19:41 +00:00
|
|
|
cpu_set_upcall(newtd, td);
|
|
|
|
|
2003-04-01 00:30:30 +00:00
|
|
|
/*
|
2005-04-23 02:36:07 +00:00
|
|
|
* Try the copyout as soon as we allocate the td so we don't
|
|
|
|
* have to tear things down in a failure case below.
|
|
|
|
* Here we copy out tid to two places, one for child and one
|
|
|
|
* for parent, because pthread can create a detached thread,
|
|
|
|
* if parent wants to safely access child tid, it has to provide
|
|
|
|
* its storage, because child thread may exit quickly and
|
|
|
|
* memory is freed before parent thread can access it.
|
2003-04-01 00:30:30 +00:00
|
|
|
*/
|
2005-04-23 02:36:07 +00:00
|
|
|
if ((child_tid != NULL &&
|
2006-09-22 15:04:28 +00:00
|
|
|
suword_lwpid(child_tid, newtd->td_tid)) ||
|
2005-04-23 02:36:07 +00:00
|
|
|
(parent_tid != NULL &&
|
2006-09-22 15:04:28 +00:00
|
|
|
suword_lwpid(parent_tid, newtd->td_tid))) {
|
|
|
|
thread_free(newtd);
|
2011-03-31 19:22:11 +00:00
|
|
|
error = EFAULT;
|
|
|
|
goto fail;
|
2003-04-01 00:30:30 +00:00
|
|
|
}
|
2006-09-22 15:04:28 +00:00
|
|
|
|
2004-09-05 02:09:54 +00:00
|
|
|
bzero(&newtd->td_startzero,
|
2004-11-20 23:00:59 +00:00
|
|
|
__rangeof(struct thread, td_startzero, td_endzero));
|
2004-09-05 02:09:54 +00:00
|
|
|
bcopy(&td->td_startcopy, &newtd->td_startcopy,
|
2004-11-20 23:00:59 +00:00
|
|
|
__rangeof(struct thread, td_startcopy, td_endcopy));
|
2004-09-05 02:09:54 +00:00
|
|
|
newtd->td_proc = td->td_proc;
|
2015-06-10 10:43:59 +00:00
|
|
|
thread_cow_get(newtd, td);
|
2003-04-01 00:30:30 +00:00
|
|
|
|
2005-04-23 02:36:07 +00:00
|
|
|
if (ctx != NULL) { /* old way to set user context */
|
|
|
|
error = set_mcontext(newtd, ctx);
|
|
|
|
if (error != 0) {
|
2015-06-10 10:43:59 +00:00
|
|
|
thread_cow_free(newtd);
|
2005-04-23 02:36:07 +00:00
|
|
|
thread_free(newtd);
|
2011-03-31 19:22:11 +00:00
|
|
|
goto fail;
|
2005-04-23 02:36:07 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Set up our machine context. */
|
|
|
|
stack.ss_sp = stack_base;
|
|
|
|
stack.ss_size = stack_size;
|
|
|
|
/* Set upcall address to user thread entry function. */
|
|
|
|
cpu_set_upcall_kse(newtd, start_func, arg, &stack);
|
|
|
|
/* Setup user TLS address and TLS pointer register. */
|
2005-07-10 23:31:11 +00:00
|
|
|
error = cpu_set_user_tls(newtd, tls_base);
|
|
|
|
if (error != 0) {
|
2015-06-10 10:43:59 +00:00
|
|
|
thread_cow_free(newtd);
|
2005-07-10 23:31:11 +00:00
|
|
|
thread_free(newtd);
|
2011-03-31 19:22:11 +00:00
|
|
|
goto fail;
|
2005-07-10 23:31:11 +00:00
|
|
|
}
|
2004-07-02 00:40:07 +00:00
|
|
|
}
|
2003-04-01 00:30:30 +00:00
|
|
|
|
2015-04-26 17:22:59 +00:00
|
|
|
PROC_LOCK(p);
|
|
|
|
p->p_flag |= P_HADTHREADS;
|
|
|
|
thread_link(newtd, p);
|
2007-11-15 06:35:26 +00:00
|
|
|
bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
|
Commit 14/14 of sched_lock decomposition.
- Use thread_lock() rather than sched_lock for per-thread scheduling
sychronization.
- Use the per-process spinlock rather than the sched_lock for per-process
scheduling synchronization.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-05 00:00:57 +00:00
|
|
|
thread_lock(td);
|
2004-09-05 02:09:54 +00:00
|
|
|
/* let the scheduler know about these things. */
|
|
|
|
sched_fork_thread(td, newtd);
|
Commit 14/14 of sched_lock decomposition.
- Use thread_lock() rather than sched_lock for per-thread scheduling
sychronization.
- Use the per-process spinlock rather than the sched_lock for per-process
scheduling synchronization.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-05 00:00:57 +00:00
|
|
|
thread_unlock(td);
|
2008-03-21 08:23:25 +00:00
|
|
|
if (P_SHOULDSTOP(p))
|
|
|
|
newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
|
Commit 14/14 of sched_lock decomposition.
- Use thread_lock() rather than sched_lock for per-thread scheduling
sychronization.
- Use the per-process spinlock rather than the sched_lock for per-process
scheduling synchronization.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-05 00:00:57 +00:00
|
|
|
PROC_UNLOCK(p);
|
2010-10-09 02:50:23 +00:00
|
|
|
|
|
|
|
tidhash_add(newtd);
|
|
|
|
|
Commit 14/14 of sched_lock decomposition.
- Use thread_lock() rather than sched_lock for per-thread scheduling
sychronization.
- Use the per-process spinlock rather than the sched_lock for per-process
scheduling synchronization.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-05 00:00:57 +00:00
|
|
|
thread_lock(newtd);
|
2006-10-26 21:42:22 +00:00
|
|
|
if (rtp != NULL) {
|
|
|
|
if (!(td->td_pri_class == PRI_TIMESHARE &&
|
|
|
|
rtp->type == RTP_PRIO_NORMAL)) {
|
|
|
|
rtp_to_pri(rtp, newtd);
|
|
|
|
sched_prio(newtd, newtd->td_user_pri);
|
|
|
|
} /* ignore timesharing class */
|
|
|
|
}
|
2004-09-05 02:09:54 +00:00
|
|
|
TD_SET_CAN_RUN(newtd);
|
2008-03-23 02:03:06 +00:00
|
|
|
sched_add(newtd, SRQ_BORING);
|
Commit 14/14 of sched_lock decomposition.
- Use thread_lock() rather than sched_lock for per-thread scheduling
sychronization.
- Use the per-process spinlock rather than the sched_lock for per-process
scheduling synchronization.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-05 00:00:57 +00:00
|
|
|
thread_unlock(newtd);
|
2003-04-01 00:30:30 +00:00
|
|
|
|
2009-02-26 15:51:54 +00:00
|
|
|
return (0);
|
2011-03-31 19:22:11 +00:00
|
|
|
|
|
|
|
fail:
|
2011-07-06 20:06:44 +00:00
|
|
|
#ifdef RACCT
|
2015-04-29 10:23:02 +00:00
|
|
|
if (racct_enable) {
|
|
|
|
PROC_LOCK(p);
|
|
|
|
racct_sub(p, RACCT_NTHR, 1);
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
}
|
2011-07-06 20:06:44 +00:00
|
|
|
#endif
|
2011-03-31 19:22:11 +00:00
|
|
|
return (error);
|
2003-04-01 00:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-09-16 13:58:51 +00:00
|
|
|
sys_thr_self(struct thread *td, struct thr_self_args *uap)
|
2004-07-02 00:40:07 +00:00
|
|
|
/* long *id */
|
2003-04-01 00:30:30 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2006-09-22 15:04:28 +00:00
|
|
|
error = suword_lwpid(uap->id, (unsigned)td->td_tid);
|
|
|
|
if (error == -1)
|
|
|
|
return (EFAULT);
|
2003-04-01 00:30:30 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-09-16 13:58:51 +00:00
|
|
|
sys_thr_exit(struct thread *td, struct thr_exit_args *uap)
|
2004-10-06 14:23:00 +00:00
|
|
|
/* long *state */
|
2003-04-01 00:30:30 +00:00
|
|
|
{
|
|
|
|
|
2004-10-06 14:23:00 +00:00
|
|
|
/* Signal userland that it can free the stack. */
|
2005-10-26 06:55:46 +00:00
|
|
|
if ((void *)uap->state != NULL) {
|
2006-09-22 15:04:28 +00:00
|
|
|
suword_lwpid(uap->state, 1);
|
2008-04-29 05:48:05 +00:00
|
|
|
kern_umtx_wake(td, uap->state, INT_MAX, 0);
|
2005-10-26 06:55:46 +00:00
|
|
|
}
|
2004-10-06 14:23:00 +00:00
|
|
|
|
2015-05-24 14:36:33 +00:00
|
|
|
return (kern_thr_exit(td));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
kern_thr_exit(struct thread *td)
|
|
|
|
{
|
|
|
|
struct proc *p;
|
2011-03-31 19:22:11 +00:00
|
|
|
|
2015-05-24 14:36:33 +00:00
|
|
|
p = td->td_proc;
|
|
|
|
|
|
|
|
rw_wlock(&tidhash_lock);
|
2003-04-01 00:30:30 +00:00
|
|
|
PROC_LOCK(p);
|
2011-03-31 19:22:11 +00:00
|
|
|
|
2004-09-05 02:09:54 +00:00
|
|
|
if (p->p_numthreads != 1) {
|
2012-04-17 13:44:40 +00:00
|
|
|
racct_sub(p, RACCT_NTHR, 1);
|
2010-10-23 13:16:39 +00:00
|
|
|
LIST_REMOVE(td, td_hash);
|
|
|
|
rw_wunlock(&tidhash_lock);
|
|
|
|
tdsigcleanup(td);
|
2015-02-28 04:19:02 +00:00
|
|
|
umtx_thread_exit(td);
|
2010-10-23 13:16:39 +00:00
|
|
|
PROC_SLOCK(p);
|
2006-02-10 03:34:29 +00:00
|
|
|
thread_stopped(p);
|
2004-09-05 02:09:54 +00:00
|
|
|
thread_exit();
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
2014-11-03 11:29:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Ignore attempts to shut down last thread in the proc. This
|
|
|
|
* will actually call _exit(2) in the usermode trampoline when
|
|
|
|
* it returns.
|
|
|
|
*/
|
2004-09-05 02:09:54 +00:00
|
|
|
PROC_UNLOCK(p);
|
2010-10-23 13:16:39 +00:00
|
|
|
rw_wunlock(&tidhash_lock);
|
2003-04-01 00:30:30 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-09-16 13:58:51 +00:00
|
|
|
sys_thr_kill(struct thread *td, struct thr_kill_args *uap)
|
2004-07-02 00:40:07 +00:00
|
|
|
/* long id, int sig */
|
2003-04-01 00:30:30 +00:00
|
|
|
{
|
2010-03-01 14:27:16 +00:00
|
|
|
ksiginfo_t ksi;
|
2003-04-01 00:30:30 +00:00
|
|
|
struct thread *ttd;
|
|
|
|
struct proc *p;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
p = td->td_proc;
|
2010-03-01 14:27:16 +00:00
|
|
|
ksiginfo_init(&ksi);
|
|
|
|
ksi.ksi_signo = uap->sig;
|
2010-08-24 07:22:24 +00:00
|
|
|
ksi.ksi_code = SI_LWP;
|
2010-03-01 14:27:16 +00:00
|
|
|
ksi.ksi_pid = p->p_pid;
|
|
|
|
ksi.ksi_uid = td->td_ucred->cr_ruid;
|
2006-01-07 03:15:21 +00:00
|
|
|
if (uap->id == -1) {
|
|
|
|
if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
|
|
|
|
error = EINVAL;
|
|
|
|
} else {
|
|
|
|
error = ESRCH;
|
2010-10-09 02:50:23 +00:00
|
|
|
PROC_LOCK(p);
|
2006-01-07 03:15:21 +00:00
|
|
|
FOREACH_THREAD_IN_PROC(p, ttd) {
|
|
|
|
if (ttd != td) {
|
|
|
|
error = 0;
|
|
|
|
if (uap->sig == 0)
|
|
|
|
break;
|
2010-06-29 20:41:52 +00:00
|
|
|
tdksignal(ttd, uap->sig, &ksi);
|
2006-01-07 03:15:21 +00:00
|
|
|
}
|
|
|
|
}
|
2010-10-09 02:50:23 +00:00
|
|
|
PROC_UNLOCK(p);
|
2006-01-07 03:15:21 +00:00
|
|
|
}
|
|
|
|
} else {
|
2010-10-09 02:50:23 +00:00
|
|
|
error = 0;
|
|
|
|
ttd = tdfind((lwpid_t)uap->id, p->p_pid);
|
2006-01-07 03:15:21 +00:00
|
|
|
if (ttd == NULL)
|
2010-10-09 02:50:23 +00:00
|
|
|
return (ESRCH);
|
|
|
|
if (uap->sig == 0)
|
2006-01-07 03:15:21 +00:00
|
|
|
;
|
|
|
|
else if (!_SIG_VALID(uap->sig))
|
|
|
|
error = EINVAL;
|
2010-10-09 02:50:23 +00:00
|
|
|
else
|
2010-06-29 20:41:52 +00:00
|
|
|
tdksignal(ttd, uap->sig, &ksi);
|
2010-10-09 02:50:23 +00:00
|
|
|
PROC_UNLOCK(ttd->td_proc);
|
2003-04-01 00:30:30 +00:00
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|
2004-03-27 14:30:43 +00:00
|
|
|
|
2007-08-16 05:26:42 +00:00
|
|
|
int
|
2011-09-16 13:58:51 +00:00
|
|
|
sys_thr_kill2(struct thread *td, struct thr_kill2_args *uap)
|
2007-08-16 05:26:42 +00:00
|
|
|
/* pid_t pid, long id, int sig */
|
|
|
|
{
|
2010-03-01 14:27:16 +00:00
|
|
|
ksiginfo_t ksi;
|
2007-08-16 05:26:42 +00:00
|
|
|
struct thread *ttd;
|
|
|
|
struct proc *p;
|
|
|
|
int error;
|
|
|
|
|
2009-06-27 13:58:44 +00:00
|
|
|
AUDIT_ARG_SIGNUM(uap->sig);
|
2007-08-16 05:26:42 +00:00
|
|
|
|
2010-10-09 02:50:23 +00:00
|
|
|
ksiginfo_init(&ksi);
|
|
|
|
ksi.ksi_signo = uap->sig;
|
|
|
|
ksi.ksi_code = SI_LWP;
|
|
|
|
ksi.ksi_pid = td->td_proc->p_pid;
|
|
|
|
ksi.ksi_uid = td->td_ucred->cr_ruid;
|
|
|
|
if (uap->id == -1) {
|
|
|
|
if ((p = pfind(uap->pid)) == NULL)
|
|
|
|
return (ESRCH);
|
|
|
|
AUDIT_ARG_PROCESS(p);
|
|
|
|
error = p_cansignal(td, p, uap->sig);
|
|
|
|
if (error) {
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
|
|
|
|
error = EINVAL;
|
|
|
|
} else {
|
|
|
|
error = ESRCH;
|
|
|
|
FOREACH_THREAD_IN_PROC(p, ttd) {
|
|
|
|
if (ttd != td) {
|
|
|
|
error = 0;
|
|
|
|
if (uap->sig == 0)
|
|
|
|
break;
|
|
|
|
tdksignal(ttd, uap->sig, &ksi);
|
2007-08-16 05:26:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-10-09 02:50:23 +00:00
|
|
|
PROC_UNLOCK(p);
|
|
|
|
} else {
|
|
|
|
ttd = tdfind((lwpid_t)uap->id, uap->pid);
|
|
|
|
if (ttd == NULL)
|
|
|
|
return (ESRCH);
|
|
|
|
p = ttd->td_proc;
|
|
|
|
AUDIT_ARG_PROCESS(p);
|
|
|
|
error = p_cansignal(td, p, uap->sig);
|
|
|
|
if (uap->sig == 0)
|
|
|
|
;
|
|
|
|
else if (!_SIG_VALID(uap->sig))
|
|
|
|
error = EINVAL;
|
|
|
|
else
|
|
|
|
tdksignal(ttd, uap->sig, &ksi);
|
|
|
|
PROC_UNLOCK(p);
|
2007-08-16 05:26:42 +00:00
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2004-03-27 14:30:43 +00:00
|
|
|
int
|
2011-09-16 13:58:51 +00:00
|
|
|
sys_thr_suspend(struct thread *td, struct thr_suspend_args *uap)
|
2004-03-27 14:30:43 +00:00
|
|
|
/* const struct timespec *timeout */
|
|
|
|
{
|
2006-09-22 15:04:28 +00:00
|
|
|
struct timespec ts, *tsp;
|
2004-03-27 14:30:43 +00:00
|
|
|
int error;
|
|
|
|
|
2006-09-22 15:04:28 +00:00
|
|
|
tsp = NULL;
|
2004-03-27 14:30:43 +00:00
|
|
|
if (uap->timeout != NULL) {
|
2011-12-03 12:35:13 +00:00
|
|
|
error = umtx_copyin_timeout(uap->timeout, &ts);
|
2004-03-27 14:30:43 +00:00
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
2006-09-22 15:04:28 +00:00
|
|
|
tsp = &ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (kern_thr_suspend(td, tsp));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
kern_thr_suspend(struct thread *td, struct timespec *tsp)
|
|
|
|
{
|
2010-10-20 00:41:38 +00:00
|
|
|
struct proc *p = td->td_proc;
|
2006-09-22 15:04:28 +00:00
|
|
|
struct timeval tv;
|
2010-08-24 07:29:55 +00:00
|
|
|
int error = 0;
|
|
|
|
int timo = 0;
|
2006-09-22 15:04:28 +00:00
|
|
|
|
2006-12-04 14:15:12 +00:00
|
|
|
if (td->td_pflags & TDP_WAKEUP) {
|
|
|
|
td->td_pflags &= ~TDP_WAKEUP;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2010-10-20 00:41:38 +00:00
|
|
|
if (tsp != NULL) {
|
2010-08-24 07:29:55 +00:00
|
|
|
if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
|
|
|
|
error = EWOULDBLOCK;
|
|
|
|
else {
|
|
|
|
TIMESPEC_TO_TIMEVAL(&tv, tsp);
|
|
|
|
timo = tvtohz(&tv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-20 00:41:38 +00:00
|
|
|
PROC_LOCK(p);
|
|
|
|
if (error == 0 && (td->td_flags & TDF_THRWAKEUP) == 0)
|
|
|
|
error = msleep((void *)td, &p->p_mtx,
|
|
|
|
PCATCH, "lthr", timo);
|
|
|
|
|
2004-12-01 13:50:04 +00:00
|
|
|
if (td->td_flags & TDF_THRWAKEUP) {
|
Commit 14/14 of sched_lock decomposition.
- Use thread_lock() rather than sched_lock for per-thread scheduling
sychronization.
- Use the per-process spinlock rather than the sched_lock for per-process
scheduling synchronization.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-05 00:00:57 +00:00
|
|
|
thread_lock(td);
|
2004-12-01 13:50:04 +00:00
|
|
|
td->td_flags &= ~TDF_THRWAKEUP;
|
Commit 14/14 of sched_lock decomposition.
- Use thread_lock() rather than sched_lock for per-thread scheduling
sychronization.
- Use the per-process spinlock rather than the sched_lock for per-process
scheduling synchronization.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-05 00:00:57 +00:00
|
|
|
thread_unlock(td);
|
2010-10-20 00:41:38 +00:00
|
|
|
PROC_UNLOCK(p);
|
2004-12-01 13:50:04 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2010-10-20 00:41:38 +00:00
|
|
|
PROC_UNLOCK(p);
|
2004-12-01 13:50:04 +00:00
|
|
|
if (error == EWOULDBLOCK)
|
|
|
|
error = ETIMEDOUT;
|
|
|
|
else if (error == ERESTART) {
|
2010-08-24 07:29:55 +00:00
|
|
|
if (timo != 0)
|
2004-12-01 13:50:04 +00:00
|
|
|
error = EINTR;
|
|
|
|
}
|
|
|
|
return (error);
|
2004-03-27 14:30:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-09-16 13:58:51 +00:00
|
|
|
sys_thr_wake(struct thread *td, struct thr_wake_args *uap)
|
2004-07-02 00:40:07 +00:00
|
|
|
/* long id */
|
2004-03-27 14:30:43 +00:00
|
|
|
{
|
2005-11-03 01:34:08 +00:00
|
|
|
struct proc *p;
|
2004-07-02 00:40:07 +00:00
|
|
|
struct thread *ttd;
|
2004-03-27 14:30:43 +00:00
|
|
|
|
2006-12-04 14:15:12 +00:00
|
|
|
if (uap->id == td->td_tid) {
|
|
|
|
td->td_pflags |= TDP_WAKEUP;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2005-11-03 01:34:08 +00:00
|
|
|
p = td->td_proc;
|
2010-10-09 02:50:23 +00:00
|
|
|
ttd = tdfind((lwpid_t)uap->id, p->p_pid);
|
|
|
|
if (ttd == NULL)
|
2004-03-27 14:30:43 +00:00
|
|
|
return (ESRCH);
|
Commit 14/14 of sched_lock decomposition.
- Use thread_lock() rather than sched_lock for per-thread scheduling
sychronization.
- Use the per-process spinlock rather than the sched_lock for per-process
scheduling synchronization.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-05 00:00:57 +00:00
|
|
|
thread_lock(ttd);
|
2004-07-02 00:40:07 +00:00
|
|
|
ttd->td_flags |= TDF_THRWAKEUP;
|
Commit 14/14 of sched_lock decomposition.
- Use thread_lock() rather than sched_lock for per-thread scheduling
sychronization.
- Use the per-process spinlock rather than the sched_lock for per-process
scheduling synchronization.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-05 00:00:57 +00:00
|
|
|
thread_unlock(ttd);
|
2004-12-01 13:50:04 +00:00
|
|
|
wakeup((void *)ttd);
|
2005-11-03 01:34:08 +00:00
|
|
|
PROC_UNLOCK(p);
|
2004-03-27 14:30:43 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2006-02-05 02:18:46 +00:00
|
|
|
|
|
|
|
int
|
2011-09-16 13:58:51 +00:00
|
|
|
sys_thr_set_name(struct thread *td, struct thr_set_name_args *uap)
|
2006-02-05 02:18:46 +00:00
|
|
|
{
|
2010-10-09 02:50:23 +00:00
|
|
|
struct proc *p;
|
2006-02-05 02:18:46 +00:00
|
|
|
char name[MAXCOMLEN + 1];
|
|
|
|
struct thread *ttd;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
error = 0;
|
|
|
|
name[0] = '\0';
|
|
|
|
if (uap->name != NULL) {
|
|
|
|
error = copyinstr(uap->name, name, sizeof(name),
|
|
|
|
NULL);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
}
|
2010-10-09 02:50:23 +00:00
|
|
|
p = td->td_proc;
|
|
|
|
ttd = tdfind((lwpid_t)uap->id, p->p_pid);
|
|
|
|
if (ttd == NULL)
|
|
|
|
return (ESRCH);
|
|
|
|
strcpy(ttd->td_name, name);
|
2012-03-08 19:41:05 +00:00
|
|
|
#ifdef KTR
|
|
|
|
sched_clear_tdname(ttd);
|
|
|
|
#endif
|
2006-02-05 02:18:46 +00:00
|
|
|
PROC_UNLOCK(p);
|
|
|
|
return (error);
|
|
|
|
}
|
2015-05-24 14:37:45 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
kern_thr_alloc(struct proc *p, int pages, struct thread **ntd)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* Have race condition but it is cheap. */
|
|
|
|
if (p->p_numthreads >= max_threads_per_proc) {
|
|
|
|
++max_threads_hits;
|
|
|
|
return (EPROCLIM);
|
|
|
|
}
|
|
|
|
|
|
|
|
*ntd = thread_alloc(pages);
|
|
|
|
if (*ntd == NULL)
|
|
|
|
return (ENOMEM);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|