xref: /freebsd/sys/kern/kern_thr.c (revision 2a339d9e3dc129f0b0b79c2cb8d2bb0386fb0f5f)
19454b2d8SWarner Losh /*-
289bb1cefSJeff Roberson  * Copyright (c) 2003, Jeffrey Roberson <jeff@freebsd.org>
389bb1cefSJeff Roberson  * All rights reserved.
489bb1cefSJeff Roberson  *
589bb1cefSJeff Roberson  * Redistribution and use in source and binary forms, with or without
689bb1cefSJeff Roberson  * modification, are permitted provided that the following conditions
789bb1cefSJeff Roberson  * are met:
889bb1cefSJeff Roberson  * 1. Redistributions of source code must retain the above copyright
989bb1cefSJeff Roberson  *    notice unmodified, this list of conditions, and the following
1089bb1cefSJeff Roberson  *    disclaimer.
1189bb1cefSJeff Roberson  * 2. Redistributions in binary form must reproduce the above copyright
1289bb1cefSJeff Roberson  *    notice, this list of conditions and the following disclaimer in the
1389bb1cefSJeff Roberson  *    documentation and/or other materials provided with the distribution.
1489bb1cefSJeff Roberson  *
1589bb1cefSJeff Roberson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1689bb1cefSJeff Roberson  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1789bb1cefSJeff Roberson  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1889bb1cefSJeff Roberson  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1989bb1cefSJeff Roberson  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2089bb1cefSJeff Roberson  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2189bb1cefSJeff Roberson  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2289bb1cefSJeff Roberson  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2389bb1cefSJeff Roberson  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2489bb1cefSJeff Roberson  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2589bb1cefSJeff Roberson  */
2689bb1cefSJeff Roberson 
27677b542eSDavid E. O'Brien #include <sys/cdefs.h>
28677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
29677b542eSDavid E. O'Brien 
30cda9a0d1SDavid Xu #include "opt_compat.h"
3160088160SDavid Xu #include "opt_posix.h"
3289bb1cefSJeff Roberson #include <sys/param.h>
3389bb1cefSJeff Roberson #include <sys/kernel.h>
3489bb1cefSJeff Roberson #include <sys/lock.h>
3589bb1cefSJeff Roberson #include <sys/mutex.h>
36acd3428bSRobert Watson #include <sys/priv.h>
3789bb1cefSJeff Roberson #include <sys/proc.h>
38c4f7f0fdSTom Rhodes #include <sys/posix4.h>
3958c77a9dSEdward Tomasz Napierala #include <sys/racct.h>
4089bb1cefSJeff Roberson #include <sys/resourcevar.h>
410d036d55SDavid Xu #include <sys/rwlock.h>
42a22ec9d8SJeff Roberson #include <sys/sched.h>
43a8b491c1SJulian Elischer #include <sys/sysctl.h>
44ed062c8dSJulian Elischer #include <sys/smp.h>
45a66fde8dSJohn Baldwin #include <sys/syscallsubr.h>
4689bb1cefSJeff Roberson #include <sys/sysent.h>
4789bb1cefSJeff Roberson #include <sys/systm.h>
4889bb1cefSJeff Roberson #include <sys/sysproto.h>
4989bb1cefSJeff Roberson #include <sys/signalvar.h>
5025a9cfc9SKonstantin Belousov #include <sys/sysctl.h>
5189bb1cefSJeff Roberson #include <sys/ucontext.h>
5289bb1cefSJeff Roberson #include <sys/thr.h>
53a0712c99SDavid Xu #include <sys/rtprio.h>
544938faa6SDavid Xu #include <sys/umtx.h>
554938faa6SDavid Xu #include <sys/limits.h>
5689bb1cefSJeff Roberson 
576520495aSAdrian Chadd #include <vm/vm_domain.h>
586520495aSAdrian Chadd 
5989bb1cefSJeff Roberson #include <machine/frame.h>
6089bb1cefSJeff Roberson 
610b1f0611SDavid Xu #include <security/audit/audit.h>
620b1f0611SDavid Xu 
636472ac3dSEd Schouten static SYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW, 0,
646472ac3dSEd Schouten     "thread allocation");
6525a9cfc9SKonstantin Belousov 
6625a9cfc9SKonstantin Belousov static int max_threads_per_proc = 1500;
6725a9cfc9SKonstantin Belousov SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_per_proc, CTLFLAG_RW,
6825a9cfc9SKonstantin Belousov     &max_threads_per_proc, 0, "Limit on threads per proc");
6925a9cfc9SKonstantin Belousov 
7025a9cfc9SKonstantin Belousov static int max_threads_hits;
7125a9cfc9SKonstantin Belousov SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_hits, CTLFLAG_RD,
723eb9ab52SEitan Adler     &max_threads_hits, 0, "kern.threads.max_threads_per_proc hit count");
7325a9cfc9SKonstantin Belousov 
74841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32
75cda9a0d1SDavid Xu 
76cda9a0d1SDavid Xu static inline int
77cda9a0d1SDavid Xu suword_lwpid(void *addr, lwpid_t lwpid)
78cda9a0d1SDavid Xu {
79cda9a0d1SDavid Xu 	int error;
80cda9a0d1SDavid Xu 
81b4cf0e62SKonstantin Belousov 	if (SV_CURPROC_FLAG(SV_LP64))
82cda9a0d1SDavid Xu 		error = suword(addr, lwpid);
83cda9a0d1SDavid Xu 	else
84cda9a0d1SDavid Xu 		error = suword32(addr, lwpid);
85cda9a0d1SDavid Xu 	return (error);
86cda9a0d1SDavid Xu }
87cda9a0d1SDavid Xu 
88cda9a0d1SDavid Xu #else
89cda9a0d1SDavid Xu #define suword_lwpid	suword
90cda9a0d1SDavid Xu #endif
91cda9a0d1SDavid Xu 
9289bb1cefSJeff Roberson /*
9389bb1cefSJeff Roberson  * System call interface.
9489bb1cefSJeff Roberson  */
955a170c1bSEd Schouten 
965a170c1bSEd Schouten struct thr_create_initthr_args {
975a170c1bSEd Schouten 	ucontext_t ctx;
985a170c1bSEd Schouten 	long *tid;
995a170c1bSEd Schouten };
1005a170c1bSEd Schouten 
1015a170c1bSEd Schouten static int
1025a170c1bSEd Schouten thr_create_initthr(struct thread *td, void *thunk)
1035a170c1bSEd Schouten {
1045a170c1bSEd Schouten 	struct thr_create_initthr_args *args;
1055a170c1bSEd Schouten 
1065a170c1bSEd Schouten 	/* Copy out the child tid. */
1075a170c1bSEd Schouten 	args = thunk;
1085a170c1bSEd Schouten 	if (args->tid != NULL && suword_lwpid(args->tid, td->td_tid))
1095a170c1bSEd Schouten 		return (EFAULT);
1105a170c1bSEd Schouten 
1115a170c1bSEd Schouten 	return (set_mcontext(td, &args->ctx.uc_mcontext));
1125a170c1bSEd Schouten }
1135a170c1bSEd Schouten 
11489bb1cefSJeff Roberson int
1158451d0ddSKip Macy sys_thr_create(struct thread *td, struct thr_create_args *uap)
116cd28f17dSMarcel Moolenaar     /* ucontext_t *ctx, long *id, int flags */
11789bb1cefSJeff Roberson {
1185a170c1bSEd Schouten 	struct thr_create_initthr_args args;
11989bb1cefSJeff Roberson 	int error;
12089bb1cefSJeff Roberson 
1215a170c1bSEd Schouten 	if ((error = copyin(uap->ctx, &args.ctx, sizeof(args.ctx))))
12289bb1cefSJeff Roberson 		return (error);
1235a170c1bSEd Schouten 	args.tid = uap->id;
1245a170c1bSEd Schouten 	return (thread_create(td, NULL, thr_create_initthr, &args));
125c4bd610fSDavid Xu }
126c4bd610fSDavid Xu 
127c4bd610fSDavid Xu int
1288451d0ddSKip Macy sys_thr_new(struct thread *td, struct thr_new_args *uap)
129c4bd610fSDavid Xu     /* struct thr_param * */
130c4bd610fSDavid Xu {
131c4bd610fSDavid Xu 	struct thr_param param;
132cda9a0d1SDavid Xu 	int error;
133cda9a0d1SDavid Xu 
134cda9a0d1SDavid Xu 	if (uap->param_size < 0 || uap->param_size > sizeof(param))
135cda9a0d1SDavid Xu 		return (EINVAL);
136cda9a0d1SDavid Xu 	bzero(&param, sizeof(param));
137cda9a0d1SDavid Xu 	if ((error = copyin(uap->param, &param, uap->param_size)))
138cda9a0d1SDavid Xu 		return (error);
139cda9a0d1SDavid Xu 	return (kern_thr_new(td, &param));
140cda9a0d1SDavid Xu }
141cda9a0d1SDavid Xu 
1425a170c1bSEd Schouten static int
1435a170c1bSEd Schouten thr_new_initthr(struct thread *td, void *thunk)
1445a170c1bSEd Schouten {
1455a170c1bSEd Schouten 	stack_t stack;
1465a170c1bSEd Schouten 	struct thr_param *param;
1475a170c1bSEd Schouten 
1485a170c1bSEd Schouten 	/*
1495a170c1bSEd Schouten 	 * Here we copy out tid to two places, one for child and one
1505a170c1bSEd Schouten 	 * for parent, because pthread can create a detached thread,
1515a170c1bSEd Schouten 	 * if parent wants to safely access child tid, it has to provide
1525a170c1bSEd Schouten 	 * its storage, because child thread may exit quickly and
1535a170c1bSEd Schouten 	 * memory is freed before parent thread can access it.
1545a170c1bSEd Schouten 	 */
1555a170c1bSEd Schouten 	param = thunk;
1565a170c1bSEd Schouten 	if ((param->child_tid != NULL &&
1575a170c1bSEd Schouten 	    suword_lwpid(param->child_tid, td->td_tid)) ||
1585a170c1bSEd Schouten 	    (param->parent_tid != NULL &&
1595a170c1bSEd Schouten 	    suword_lwpid(param->parent_tid, td->td_tid)))
1605a170c1bSEd Schouten 		return (EFAULT);
1615a170c1bSEd Schouten 
1625a170c1bSEd Schouten 	/* Set up our machine context. */
1635a170c1bSEd Schouten 	stack.ss_sp = param->stack_base;
1645a170c1bSEd Schouten 	stack.ss_size = param->stack_size;
1655a170c1bSEd Schouten 	/* Set upcall address to user thread entry function. */
1665a170c1bSEd Schouten 	cpu_set_upcall_kse(td, param->start_func, param->arg, &stack);
1675a170c1bSEd Schouten 	/* Setup user TLS address and TLS pointer register. */
1685a170c1bSEd Schouten 	return (cpu_set_user_tls(td, param->tls_base));
1695a170c1bSEd Schouten }
1705a170c1bSEd Schouten 
171cda9a0d1SDavid Xu int
172cda9a0d1SDavid Xu kern_thr_new(struct thread *td, struct thr_param *param)
173cda9a0d1SDavid Xu {
17473fa3e5bSDavid Xu 	struct rtprio rtp, *rtpp;
175c4bd610fSDavid Xu 	int error;
176c4bd610fSDavid Xu 
17773fa3e5bSDavid Xu 	rtpp = NULL;
178cda9a0d1SDavid Xu 	if (param->rtp != 0) {
179cda9a0d1SDavid Xu 		error = copyin(param->rtp, &rtp, sizeof(struct rtprio));
180e4866772SRoman Divacky 		if (error)
181e4866772SRoman Divacky 			return (error);
18273fa3e5bSDavid Xu 		rtpp = &rtp;
183a0712c99SDavid Xu 	}
1845a170c1bSEd Schouten 	return (thread_create(td, rtpp, thr_new_initthr, param));
185c4bd610fSDavid Xu }
186c4bd610fSDavid Xu 
1875a170c1bSEd Schouten int
1885a170c1bSEd Schouten thread_create(struct thread *td, struct rtprio *rtp,
1895a170c1bSEd Schouten     int (*initialize_thread)(struct thread *, void *), void *thunk)
190c4bd610fSDavid Xu {
191c4bd610fSDavid Xu 	struct thread *newtd;
192c4bd610fSDavid Xu 	struct proc *p;
193adc9c950SDavid Xu 	int error;
194c4bd610fSDavid Xu 
195c4bd610fSDavid Xu 	p = td->td_proc;
196c4bd610fSDavid Xu 
19773fa3e5bSDavid Xu 	if (rtp != NULL) {
19873fa3e5bSDavid Xu 		switch(rtp->type) {
19973fa3e5bSDavid Xu 		case RTP_PRIO_REALTIME:
20073fa3e5bSDavid Xu 		case RTP_PRIO_FIFO:
201a0712c99SDavid Xu 			/* Only root can set scheduler policy */
202acd3428bSRobert Watson 			if (priv_check(td, PRIV_SCHED_SETPOLICY) != 0)
203a0712c99SDavid Xu 				return (EPERM);
20473fa3e5bSDavid Xu 			if (rtp->prio > RTP_PRIO_MAX)
205a0712c99SDavid Xu 				return (EINVAL);
2062dca4ca7SDavid Xu 			break;
20773fa3e5bSDavid Xu 		case RTP_PRIO_NORMAL:
20873fa3e5bSDavid Xu 			rtp->prio = 0;
2092dca4ca7SDavid Xu 			break;
2102dca4ca7SDavid Xu 		default:
2112dca4ca7SDavid Xu 			return (EINVAL);
212a0712c99SDavid Xu 		}
213a0712c99SDavid Xu 	}
214a0712c99SDavid Xu 
215afcc55f3SEdward Tomasz Napierala #ifdef RACCT
2164b5c9cf6SEdward Tomasz Napierala 	if (racct_enable) {
217203322f9SMateusz Guzik 		PROC_LOCK(p);
21858c77a9dSEdward Tomasz Napierala 		error = racct_add(p, RACCT_NTHR, 1);
219203322f9SMateusz Guzik 		PROC_UNLOCK(p);
22058c77a9dSEdward Tomasz Napierala 		if (error != 0)
22158c77a9dSEdward Tomasz Napierala 			return (EPROCLIM);
2224b5c9cf6SEdward Tomasz Napierala 	}
223afcc55f3SEdward Tomasz Napierala #endif
22458c77a9dSEdward Tomasz Napierala 
225ad1e7d28SJulian Elischer 	/* Initialize our td */
22609baafb4SDmitry Chagin 	error = kern_thr_alloc(p, 0, &newtd);
22709baafb4SDmitry Chagin 	if (error)
22858c77a9dSEdward Tomasz Napierala 		goto fail;
229c4bd610fSDavid Xu 
230cdea31e3SPeter Holm 	cpu_set_upcall(newtd, td);
231cdea31e3SPeter Holm 
232ed062c8dSJulian Elischer 	bzero(&newtd->td_startzero,
2336db36923SDavid Schultz 	    __rangeof(struct thread, td_startzero, td_endzero));
234ed062c8dSJulian Elischer 	bcopy(&td->td_startcopy, &newtd->td_startcopy,
2356db36923SDavid Schultz 	    __rangeof(struct thread, td_startcopy, td_endcopy));
236c4bd610fSDavid Xu 	newtd->td_proc = td->td_proc;
2374ea6a9a2SMateusz Guzik 	thread_cow_get(newtd, td);
23889bb1cefSJeff Roberson 
2395a170c1bSEd Schouten 	error = initialize_thread(newtd, thunk);
240c4bd610fSDavid Xu 	if (error != 0) {
2414ea6a9a2SMateusz Guzik 		thread_cow_free(newtd);
242c4bd610fSDavid Xu 		thread_free(newtd);
24358c77a9dSEdward Tomasz Napierala 		goto fail;
244c4bd610fSDavid Xu 	}
245c4bd610fSDavid Xu 
246203322f9SMateusz Guzik 	PROC_LOCK(p);
247203322f9SMateusz Guzik 	p->p_flag |= P_HADTHREADS;
2488460a577SJohn Birrell 	thread_link(newtd, p);
249c67ddc21SJulian Elischer 	bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
250982d11f8SJeff Roberson 	thread_lock(td);
251ed062c8dSJulian Elischer 	/* let the scheduler know about these things. */
252ed062c8dSJulian Elischer 	sched_fork_thread(td, newtd);
253982d11f8SJeff Roberson 	thread_unlock(td);
254b7edba77SJeff Roberson 	if (P_SHOULDSTOP(p))
255b7edba77SJeff Roberson 		newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
2565fcfab6eSJohn Baldwin 	if (p->p_flag2 & P2_LWP_EVENTS)
2575fcfab6eSJohn Baldwin 		newtd->td_dbgflags |= TDB_BORN;
2586520495aSAdrian Chadd 
2596520495aSAdrian Chadd 	/*
2606520495aSAdrian Chadd 	 * Copy the existing thread VM policy into the new thread.
2616520495aSAdrian Chadd 	 */
2626520495aSAdrian Chadd 	vm_domain_policy_localcopy(&newtd->td_vm_dom_policy,
2636520495aSAdrian Chadd 	    &td->td_vm_dom_policy);
2646520495aSAdrian Chadd 
265982d11f8SJeff Roberson 	PROC_UNLOCK(p);
266cf7d9a8cSDavid Xu 
267cf7d9a8cSDavid Xu 	tidhash_add(newtd);
268cf7d9a8cSDavid Xu 
269982d11f8SJeff Roberson 	thread_lock(newtd);
27073fa3e5bSDavid Xu 	if (rtp != NULL) {
2718460a577SJohn Birrell 		if (!(td->td_pri_class == PRI_TIMESHARE &&
2728460a577SJohn Birrell 		      rtp->type == RTP_PRIO_NORMAL)) {
2738460a577SJohn Birrell 			rtp_to_pri(rtp, newtd);
2748460a577SJohn Birrell 			sched_prio(newtd, newtd->td_user_pri);
2758460a577SJohn Birrell 		} /* ignore timesharing class */
2768460a577SJohn Birrell 	}
277ed062c8dSJulian Elischer 	TD_SET_CAN_RUN(newtd);
278f0393f06SJeff Roberson 	sched_add(newtd, SRQ_BORING);
279982d11f8SJeff Roberson 	thread_unlock(newtd);
28089bb1cefSJeff Roberson 
281c90c9021SEd Schouten 	return (0);
28258c77a9dSEdward Tomasz Napierala 
28358c77a9dSEdward Tomasz Napierala fail:
284afcc55f3SEdward Tomasz Napierala #ifdef RACCT
2854b5c9cf6SEdward Tomasz Napierala 	if (racct_enable) {
28658c77a9dSEdward Tomasz Napierala 		PROC_LOCK(p);
28758c77a9dSEdward Tomasz Napierala 		racct_sub(p, RACCT_NTHR, 1);
28858c77a9dSEdward Tomasz Napierala 		PROC_UNLOCK(p);
2894b5c9cf6SEdward Tomasz Napierala 	}
290afcc55f3SEdward Tomasz Napierala #endif
29158c77a9dSEdward Tomasz Napierala 	return (error);
29289bb1cefSJeff Roberson }
29389bb1cefSJeff Roberson 
29489bb1cefSJeff Roberson int
2958451d0ddSKip Macy sys_thr_self(struct thread *td, struct thr_self_args *uap)
296cd28f17dSMarcel Moolenaar     /* long *id */
29789bb1cefSJeff Roberson {
29889bb1cefSJeff Roberson 	int error;
29989bb1cefSJeff Roberson 
300cda9a0d1SDavid Xu 	error = suword_lwpid(uap->id, (unsigned)td->td_tid);
301cda9a0d1SDavid Xu 	if (error == -1)
302cda9a0d1SDavid Xu 		return (EFAULT);
30389bb1cefSJeff Roberson 	return (0);
30489bb1cefSJeff Roberson }
30589bb1cefSJeff Roberson 
30689bb1cefSJeff Roberson int
3078451d0ddSKip Macy sys_thr_exit(struct thread *td, struct thr_exit_args *uap)
308401901acSMike Makonnen     /* long *state */
30989bb1cefSJeff Roberson {
31089bb1cefSJeff Roberson 
311*2a339d9eSKonstantin Belousov 	umtx_thread_exit(td);
312*2a339d9eSKonstantin Belousov 
313401901acSMike Makonnen 	/* Signal userland that it can free the stack. */
3144938faa6SDavid Xu 	if ((void *)uap->state != NULL) {
315cda9a0d1SDavid Xu 		suword_lwpid(uap->state, 1);
3163bba58f2SDavid Xu 		kern_umtx_wake(td, uap->state, INT_MAX, 0);
3174938faa6SDavid Xu 	}
318401901acSMike Makonnen 
31995be6d2bSDmitry Chagin 	return (kern_thr_exit(td));
32095be6d2bSDmitry Chagin }
32158c77a9dSEdward Tomasz Napierala 
32295be6d2bSDmitry Chagin int
32395be6d2bSDmitry Chagin kern_thr_exit(struct thread *td)
32495be6d2bSDmitry Chagin {
32595be6d2bSDmitry Chagin 	struct proc *p;
32695be6d2bSDmitry Chagin 
32795be6d2bSDmitry Chagin 	p = td->td_proc;
32895be6d2bSDmitry Chagin 
3295fcfab6eSJohn Baldwin 	/*
3305fcfab6eSJohn Baldwin 	 * If all of the threads in a process call this routine to
3315fcfab6eSJohn Baldwin 	 * exit (e.g. all threads call pthread_exit()), exactly one
3325fcfab6eSJohn Baldwin 	 * thread should return to the caller to terminate the process
3335fcfab6eSJohn Baldwin 	 * instead of the thread.
3345fcfab6eSJohn Baldwin 	 *
3355fcfab6eSJohn Baldwin 	 * Checking p_numthreads alone is not sufficient since threads
3365fcfab6eSJohn Baldwin 	 * might be committed to terminating while the PROC_LOCK is
3375fcfab6eSJohn Baldwin 	 * dropped in either ptracestop() or while removing this thread
3385fcfab6eSJohn Baldwin 	 * from the tidhash.  Instead, the p_pendingexits field holds
3395fcfab6eSJohn Baldwin 	 * the count of threads in either of those states and a thread
3405fcfab6eSJohn Baldwin 	 * is considered the "last" thread if all of the other threads
3415fcfab6eSJohn Baldwin 	 * in a process are already terminating.
3425fcfab6eSJohn Baldwin 	 */
34389bb1cefSJeff Roberson 	PROC_LOCK(p);
3445fcfab6eSJohn Baldwin 	if (p->p_numthreads == p->p_pendingexits + 1) {
3455fcfab6eSJohn Baldwin 		/*
3465fcfab6eSJohn Baldwin 		 * Ignore attempts to shut down last thread in the
3475fcfab6eSJohn Baldwin 		 * proc.  This will actually call _exit(2) in the
3485fcfab6eSJohn Baldwin 		 * usermode trampoline when it returns.
3495fcfab6eSJohn Baldwin 		 */
3505fcfab6eSJohn Baldwin 		PROC_UNLOCK(p);
3515fcfab6eSJohn Baldwin 		return (0);
3525fcfab6eSJohn Baldwin 	}
35358c77a9dSEdward Tomasz Napierala 
3545fcfab6eSJohn Baldwin 	p->p_pendingexits++;
3555fcfab6eSJohn Baldwin 	td->td_dbgflags |= TDB_EXIT;
3565fcfab6eSJohn Baldwin 	if (p->p_flag & P_TRACED && p->p_flag2 & P2_LWP_EVENTS)
3575fcfab6eSJohn Baldwin 		ptracestop(td, SIGTRAP);
3585fcfab6eSJohn Baldwin 	PROC_UNLOCK(p);
3595fcfab6eSJohn Baldwin 	tidhash_remove(td);
3605fcfab6eSJohn Baldwin 	PROC_LOCK(p);
3615fcfab6eSJohn Baldwin 	p->p_pendingexits--;
3625fcfab6eSJohn Baldwin 
3635fcfab6eSJohn Baldwin 	/*
3645fcfab6eSJohn Baldwin 	 * The check above should prevent all other threads from this
3655fcfab6eSJohn Baldwin 	 * process from exiting while the PROC_LOCK is dropped, so
3665fcfab6eSJohn Baldwin 	 * there must be at least one other thread other than the
3675fcfab6eSJohn Baldwin 	 * current thread.
3685fcfab6eSJohn Baldwin 	 */
3695fcfab6eSJohn Baldwin 	KASSERT(p->p_numthreads > 1, ("too few threads"));
37047f6635cSEdward Tomasz Napierala 	racct_sub(p, RACCT_NTHR, 1);
3710d036d55SDavid Xu 	tdsigcleanup(td);
3720d036d55SDavid Xu 	PROC_SLOCK(p);
37371b7afb2SDavid Xu 	thread_stopped(p);
374ed062c8dSJulian Elischer 	thread_exit();
375ed062c8dSJulian Elischer 	/* NOTREACHED */
376ed062c8dSJulian Elischer }
37774d5b4afSKonstantin Belousov 
37889bb1cefSJeff Roberson int
3798451d0ddSKip Macy sys_thr_kill(struct thread *td, struct thr_kill_args *uap)
380cd28f17dSMarcel Moolenaar     /* long id, int sig */
38189bb1cefSJeff Roberson {
3825f73a7ebSBruno Ducrot 	ksiginfo_t ksi;
38389bb1cefSJeff Roberson 	struct thread *ttd;
38489bb1cefSJeff Roberson 	struct proc *p;
38589bb1cefSJeff Roberson 	int error;
38689bb1cefSJeff Roberson 
38789bb1cefSJeff Roberson 	p = td->td_proc;
3885f73a7ebSBruno Ducrot 	ksiginfo_init(&ksi);
3895f73a7ebSBruno Ducrot 	ksi.ksi_signo = uap->sig;
390baf28b69SDavid Xu 	ksi.ksi_code = SI_LWP;
3915f73a7ebSBruno Ducrot 	ksi.ksi_pid = p->p_pid;
3925f73a7ebSBruno Ducrot 	ksi.ksi_uid = td->td_ucred->cr_ruid;
3930a5cd498SDavid Xu 	if (uap->id == -1) {
3940a5cd498SDavid Xu 		if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
39589bb1cefSJeff Roberson 			error = EINVAL;
3960a5cd498SDavid Xu 		} else {
3970a5cd498SDavid Xu 			error = ESRCH;
398cf7d9a8cSDavid Xu 			PROC_LOCK(p);
3990a5cd498SDavid Xu 			FOREACH_THREAD_IN_PROC(p, ttd) {
4000a5cd498SDavid Xu 				if (ttd != td) {
4010a5cd498SDavid Xu 					error = 0;
4020a5cd498SDavid Xu 					if (uap->sig == 0)
4030a5cd498SDavid Xu 						break;
404ad6eec7bSJohn Baldwin 					tdksignal(ttd, uap->sig, &ksi);
4050a5cd498SDavid Xu 				}
4060a5cd498SDavid Xu 			}
407cf7d9a8cSDavid Xu 			PROC_UNLOCK(p);
4080a5cd498SDavid Xu 		}
4090a5cd498SDavid Xu 	} else {
410cf7d9a8cSDavid Xu 		error = 0;
411cf7d9a8cSDavid Xu 		ttd = tdfind((lwpid_t)uap->id, p->p_pid);
4120a5cd498SDavid Xu 		if (ttd == NULL)
413cf7d9a8cSDavid Xu 			return (ESRCH);
414cf7d9a8cSDavid Xu 		if (uap->sig == 0)
4150a5cd498SDavid Xu 			;
4160a5cd498SDavid Xu 		else if (!_SIG_VALID(uap->sig))
4170a5cd498SDavid Xu 			error = EINVAL;
4180a5cd498SDavid Xu 		else
419ad6eec7bSJohn Baldwin 			tdksignal(ttd, uap->sig, &ksi);
420cf7d9a8cSDavid Xu 		PROC_UNLOCK(ttd->td_proc);
4210a5cd498SDavid Xu 	}
42289bb1cefSJeff Roberson 	return (error);
42389bb1cefSJeff Roberson }
4241713a516SMike Makonnen 
4251713a516SMike Makonnen int
4268451d0ddSKip Macy sys_thr_kill2(struct thread *td, struct thr_kill2_args *uap)
4270b1f0611SDavid Xu     /* pid_t pid, long id, int sig */
4280b1f0611SDavid Xu {
4295f73a7ebSBruno Ducrot 	ksiginfo_t ksi;
4300b1f0611SDavid Xu 	struct thread *ttd;
4310b1f0611SDavid Xu 	struct proc *p;
4320b1f0611SDavid Xu 	int error;
4330b1f0611SDavid Xu 
43414961ba7SRobert Watson 	AUDIT_ARG_SIGNUM(uap->sig);
4350b1f0611SDavid Xu 
4365f73a7ebSBruno Ducrot 	ksiginfo_init(&ksi);
4375f73a7ebSBruno Ducrot 	ksi.ksi_signo = uap->sig;
438baf28b69SDavid Xu 	ksi.ksi_code = SI_LWP;
4395f73a7ebSBruno Ducrot 	ksi.ksi_pid = td->td_proc->p_pid;
4405f73a7ebSBruno Ducrot 	ksi.ksi_uid = td->td_ucred->cr_ruid;
4410b1f0611SDavid Xu 	if (uap->id == -1) {
442cf7d9a8cSDavid Xu 		if ((p = pfind(uap->pid)) == NULL)
443cf7d9a8cSDavid Xu 			return (ESRCH);
444cf7d9a8cSDavid Xu 		AUDIT_ARG_PROCESS(p);
445cf7d9a8cSDavid Xu 		error = p_cansignal(td, p, uap->sig);
446cf7d9a8cSDavid Xu 		if (error) {
447cf7d9a8cSDavid Xu 			PROC_UNLOCK(p);
448cf7d9a8cSDavid Xu 			return (error);
449cf7d9a8cSDavid Xu 		}
4500b1f0611SDavid Xu 		if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
4510b1f0611SDavid Xu 			error = EINVAL;
4520b1f0611SDavid Xu 		} else {
4530b1f0611SDavid Xu 			error = ESRCH;
4540b1f0611SDavid Xu 			FOREACH_THREAD_IN_PROC(p, ttd) {
4550b1f0611SDavid Xu 				if (ttd != td) {
4560b1f0611SDavid Xu 					error = 0;
4570b1f0611SDavid Xu 					if (uap->sig == 0)
4580b1f0611SDavid Xu 						break;
459ad6eec7bSJohn Baldwin 					tdksignal(ttd, uap->sig, &ksi);
4600b1f0611SDavid Xu 				}
4610b1f0611SDavid Xu 			}
4620b1f0611SDavid Xu 		}
463cf7d9a8cSDavid Xu 		PROC_UNLOCK(p);
4640b1f0611SDavid Xu 	} else {
465cf7d9a8cSDavid Xu 		ttd = tdfind((lwpid_t)uap->id, uap->pid);
4660b1f0611SDavid Xu 		if (ttd == NULL)
467cf7d9a8cSDavid Xu 			return (ESRCH);
468cf7d9a8cSDavid Xu 		p = ttd->td_proc;
469cf7d9a8cSDavid Xu 		AUDIT_ARG_PROCESS(p);
470cf7d9a8cSDavid Xu 		error = p_cansignal(td, p, uap->sig);
471cf7d9a8cSDavid Xu 		if (uap->sig == 0)
4720b1f0611SDavid Xu 			;
4730b1f0611SDavid Xu 		else if (!_SIG_VALID(uap->sig))
4740b1f0611SDavid Xu 			error = EINVAL;
4750b1f0611SDavid Xu 		else
476ad6eec7bSJohn Baldwin 			tdksignal(ttd, uap->sig, &ksi);
4770b1f0611SDavid Xu 		PROC_UNLOCK(p);
478cf7d9a8cSDavid Xu 	}
4790b1f0611SDavid Xu 	return (error);
4800b1f0611SDavid Xu }
4810b1f0611SDavid Xu 
4820b1f0611SDavid Xu int
4838451d0ddSKip Macy sys_thr_suspend(struct thread *td, struct thr_suspend_args *uap)
4841713a516SMike Makonnen 	/* const struct timespec *timeout */
4851713a516SMike Makonnen {
486cda9a0d1SDavid Xu 	struct timespec ts, *tsp;
4871713a516SMike Makonnen 	int error;
4881713a516SMike Makonnen 
489cda9a0d1SDavid Xu 	tsp = NULL;
4901713a516SMike Makonnen 	if (uap->timeout != NULL) {
4919a1d0cf6SPeter Holm 		error = umtx_copyin_timeout(uap->timeout, &ts);
4921713a516SMike Makonnen 		if (error != 0)
4931713a516SMike Makonnen 			return (error);
494cda9a0d1SDavid Xu 		tsp = &ts;
495cda9a0d1SDavid Xu 	}
496cda9a0d1SDavid Xu 
497cda9a0d1SDavid Xu 	return (kern_thr_suspend(td, tsp));
498cda9a0d1SDavid Xu }
499cda9a0d1SDavid Xu 
500cda9a0d1SDavid Xu int
501cda9a0d1SDavid Xu kern_thr_suspend(struct thread *td, struct timespec *tsp)
502cda9a0d1SDavid Xu {
503cfca8a18SDavid Xu 	struct proc *p = td->td_proc;
504cda9a0d1SDavid Xu 	struct timeval tv;
5052961a782SDavid Xu 	int error = 0;
5062961a782SDavid Xu 	int timo = 0;
507cda9a0d1SDavid Xu 
508745fbd3aSDavid Xu 	if (td->td_pflags & TDP_WAKEUP) {
509745fbd3aSDavid Xu 		td->td_pflags &= ~TDP_WAKEUP;
510745fbd3aSDavid Xu 		return (0);
511745fbd3aSDavid Xu 	}
512745fbd3aSDavid Xu 
513cfca8a18SDavid Xu 	if (tsp != NULL) {
5142961a782SDavid Xu 		if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
5152961a782SDavid Xu 			error = EWOULDBLOCK;
5162961a782SDavid Xu 		else {
5172961a782SDavid Xu 			TIMESPEC_TO_TIMEVAL(&tv, tsp);
5182961a782SDavid Xu 			timo = tvtohz(&tv);
519cfca8a18SDavid Xu 		}
520cfca8a18SDavid Xu 	}
521cfca8a18SDavid Xu 
522cfca8a18SDavid Xu 	PROC_LOCK(p);
523cfca8a18SDavid Xu 	if (error == 0 && (td->td_flags & TDF_THRWAKEUP) == 0)
524cfca8a18SDavid Xu 		error = msleep((void *)td, &p->p_mtx,
5252961a782SDavid Xu 			 PCATCH, "lthr", timo);
5262961a782SDavid Xu 
527c1df5a1aSDavid Xu 	if (td->td_flags & TDF_THRWAKEUP) {
528982d11f8SJeff Roberson 		thread_lock(td);
5291713a516SMike Makonnen 		td->td_flags &= ~TDF_THRWAKEUP;
530982d11f8SJeff Roberson 		thread_unlock(td);
531cfca8a18SDavid Xu 		PROC_UNLOCK(p);
532c1df5a1aSDavid Xu 		return (0);
533c1df5a1aSDavid Xu 	}
534cfca8a18SDavid Xu 	PROC_UNLOCK(p);
535c1df5a1aSDavid Xu 	if (error == EWOULDBLOCK)
536c1df5a1aSDavid Xu 		error = ETIMEDOUT;
537c1df5a1aSDavid Xu 	else if (error == ERESTART) {
5382961a782SDavid Xu 		if (timo != 0)
539c1df5a1aSDavid Xu 			error = EINTR;
540c1df5a1aSDavid Xu 	}
541c1df5a1aSDavid Xu 	return (error);
5421713a516SMike Makonnen }
5431713a516SMike Makonnen 
5441713a516SMike Makonnen int
5458451d0ddSKip Macy sys_thr_wake(struct thread *td, struct thr_wake_args *uap)
546cd28f17dSMarcel Moolenaar 	/* long id */
5471713a516SMike Makonnen {
54844355392SDavid Xu 	struct proc *p;
549cd28f17dSMarcel Moolenaar 	struct thread *ttd;
5501713a516SMike Makonnen 
551745fbd3aSDavid Xu 	if (uap->id == td->td_tid) {
552745fbd3aSDavid Xu 		td->td_pflags |= TDP_WAKEUP;
553745fbd3aSDavid Xu 		return (0);
554745fbd3aSDavid Xu 	}
555745fbd3aSDavid Xu 
55644355392SDavid Xu 	p = td->td_proc;
557cf7d9a8cSDavid Xu 	ttd = tdfind((lwpid_t)uap->id, p->p_pid);
558cf7d9a8cSDavid Xu 	if (ttd == NULL)
5591713a516SMike Makonnen 		return (ESRCH);
560982d11f8SJeff Roberson 	thread_lock(ttd);
561cd28f17dSMarcel Moolenaar 	ttd->td_flags |= TDF_THRWAKEUP;
562982d11f8SJeff Roberson 	thread_unlock(ttd);
563c1df5a1aSDavid Xu 	wakeup((void *)ttd);
56444355392SDavid Xu 	PROC_UNLOCK(p);
5651713a516SMike Makonnen 	return (0);
5661713a516SMike Makonnen }
5679e7d7224SDavid Xu 
5689e7d7224SDavid Xu int
5698451d0ddSKip Macy sys_thr_set_name(struct thread *td, struct thr_set_name_args *uap)
5709e7d7224SDavid Xu {
571cf7d9a8cSDavid Xu 	struct proc *p;
5729e7d7224SDavid Xu 	char name[MAXCOMLEN + 1];
5739e7d7224SDavid Xu 	struct thread *ttd;
5749e7d7224SDavid Xu 	int error;
5759e7d7224SDavid Xu 
5769e7d7224SDavid Xu 	error = 0;
5779e7d7224SDavid Xu 	name[0] = '\0';
5789e7d7224SDavid Xu 	if (uap->name != NULL) {
5799e7d7224SDavid Xu 		error = copyinstr(uap->name, name, sizeof(name),
5809e7d7224SDavid Xu 			NULL);
5819e7d7224SDavid Xu 		if (error)
5829e7d7224SDavid Xu 			return (error);
5839e7d7224SDavid Xu 	}
584cf7d9a8cSDavid Xu 	p = td->td_proc;
585cf7d9a8cSDavid Xu 	ttd = tdfind((lwpid_t)uap->id, p->p_pid);
586cf7d9a8cSDavid Xu 	if (ttd == NULL)
587cf7d9a8cSDavid Xu 		return (ESRCH);
5889e7d7224SDavid Xu 	strcpy(ttd->td_name, name);
58944ad5475SJohn Baldwin #ifdef KTR
59044ad5475SJohn Baldwin 	sched_clear_tdname(ttd);
59144ad5475SJohn Baldwin #endif
5929e7d7224SDavid Xu 	PROC_UNLOCK(p);
5939e7d7224SDavid Xu 	return (error);
5949e7d7224SDavid Xu }
59509baafb4SDmitry Chagin 
59609baafb4SDmitry Chagin int
59709baafb4SDmitry Chagin kern_thr_alloc(struct proc *p, int pages, struct thread **ntd)
59809baafb4SDmitry Chagin {
59909baafb4SDmitry Chagin 
60009baafb4SDmitry Chagin 	/* Have race condition but it is cheap. */
60109baafb4SDmitry Chagin 	if (p->p_numthreads >= max_threads_per_proc) {
60209baafb4SDmitry Chagin 		++max_threads_hits;
60309baafb4SDmitry Chagin 		return (EPROCLIM);
60409baafb4SDmitry Chagin 	}
60509baafb4SDmitry Chagin 
60609baafb4SDmitry Chagin 	*ntd = thread_alloc(pages);
60709baafb4SDmitry Chagin 	if (*ntd == NULL)
60809baafb4SDmitry Chagin 		return (ENOMEM);
60909baafb4SDmitry Chagin 
61009baafb4SDmitry Chagin 	return (0);
61109baafb4SDmitry Chagin }
612