xref: /freebsd/sys/kern/kern_thr.c (revision 5fcfab6e32aa642e52f98512301679704031f789)
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;
256*5fcfab6eSJohn Baldwin 	if (p->p_flag2 & P2_LWP_EVENTS)
257*5fcfab6eSJohn 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 
311401901acSMike Makonnen 	/* Signal userland that it can free the stack. */
3124938faa6SDavid Xu 	if ((void *)uap->state != NULL) {
313cda9a0d1SDavid Xu 		suword_lwpid(uap->state, 1);
3143bba58f2SDavid Xu 		kern_umtx_wake(td, uap->state, INT_MAX, 0);
3154938faa6SDavid Xu 	}
316401901acSMike Makonnen 
31795be6d2bSDmitry Chagin 	return (kern_thr_exit(td));
31895be6d2bSDmitry Chagin }
31958c77a9dSEdward Tomasz Napierala 
32095be6d2bSDmitry Chagin int
32195be6d2bSDmitry Chagin kern_thr_exit(struct thread *td)
32295be6d2bSDmitry Chagin {
32395be6d2bSDmitry Chagin 	struct proc *p;
32495be6d2bSDmitry Chagin 
32595be6d2bSDmitry Chagin 	p = td->td_proc;
32695be6d2bSDmitry Chagin 
327*5fcfab6eSJohn Baldwin 	/*
328*5fcfab6eSJohn Baldwin 	 * If all of the threads in a process call this routine to
329*5fcfab6eSJohn Baldwin 	 * exit (e.g. all threads call pthread_exit()), exactly one
330*5fcfab6eSJohn Baldwin 	 * thread should return to the caller to terminate the process
331*5fcfab6eSJohn Baldwin 	 * instead of the thread.
332*5fcfab6eSJohn Baldwin 	 *
333*5fcfab6eSJohn Baldwin 	 * Checking p_numthreads alone is not sufficient since threads
334*5fcfab6eSJohn Baldwin 	 * might be committed to terminating while the PROC_LOCK is
335*5fcfab6eSJohn Baldwin 	 * dropped in either ptracestop() or while removing this thread
336*5fcfab6eSJohn Baldwin 	 * from the tidhash.  Instead, the p_pendingexits field holds
337*5fcfab6eSJohn Baldwin 	 * the count of threads in either of those states and a thread
338*5fcfab6eSJohn Baldwin 	 * is considered the "last" thread if all of the other threads
339*5fcfab6eSJohn Baldwin 	 * in a process are already terminating.
340*5fcfab6eSJohn Baldwin 	 */
34189bb1cefSJeff Roberson 	PROC_LOCK(p);
342*5fcfab6eSJohn Baldwin 	if (p->p_numthreads == p->p_pendingexits + 1) {
343*5fcfab6eSJohn Baldwin 		/*
344*5fcfab6eSJohn Baldwin 		 * Ignore attempts to shut down last thread in the
345*5fcfab6eSJohn Baldwin 		 * proc.  This will actually call _exit(2) in the
346*5fcfab6eSJohn Baldwin 		 * usermode trampoline when it returns.
347*5fcfab6eSJohn Baldwin 		 */
348*5fcfab6eSJohn Baldwin 		PROC_UNLOCK(p);
349*5fcfab6eSJohn Baldwin 		return (0);
350*5fcfab6eSJohn Baldwin 	}
35158c77a9dSEdward Tomasz Napierala 
352*5fcfab6eSJohn Baldwin 	p->p_pendingexits++;
353*5fcfab6eSJohn Baldwin 	td->td_dbgflags |= TDB_EXIT;
354*5fcfab6eSJohn Baldwin 	if (p->p_flag & P_TRACED && p->p_flag2 & P2_LWP_EVENTS)
355*5fcfab6eSJohn Baldwin 		ptracestop(td, SIGTRAP);
356*5fcfab6eSJohn Baldwin 	PROC_UNLOCK(p);
357*5fcfab6eSJohn Baldwin 	tidhash_remove(td);
358*5fcfab6eSJohn Baldwin 	PROC_LOCK(p);
359*5fcfab6eSJohn Baldwin 	p->p_pendingexits--;
360*5fcfab6eSJohn Baldwin 
361*5fcfab6eSJohn Baldwin 	/*
362*5fcfab6eSJohn Baldwin 	 * The check above should prevent all other threads from this
363*5fcfab6eSJohn Baldwin 	 * process from exiting while the PROC_LOCK is dropped, so
364*5fcfab6eSJohn Baldwin 	 * there must be at least one other thread other than the
365*5fcfab6eSJohn Baldwin 	 * current thread.
366*5fcfab6eSJohn Baldwin 	 */
367*5fcfab6eSJohn Baldwin 	KASSERT(p->p_numthreads > 1, ("too few threads"));
36847f6635cSEdward Tomasz Napierala 	racct_sub(p, RACCT_NTHR, 1);
3690d036d55SDavid Xu 	tdsigcleanup(td);
37013dad108SKonstantin Belousov 	umtx_thread_exit(td);
3710d036d55SDavid Xu 	PROC_SLOCK(p);
37271b7afb2SDavid Xu 	thread_stopped(p);
373ed062c8dSJulian Elischer 	thread_exit();
374ed062c8dSJulian Elischer 	/* NOTREACHED */
375ed062c8dSJulian Elischer }
37674d5b4afSKonstantin Belousov 
37789bb1cefSJeff Roberson int
3788451d0ddSKip Macy sys_thr_kill(struct thread *td, struct thr_kill_args *uap)
379cd28f17dSMarcel Moolenaar     /* long id, int sig */
38089bb1cefSJeff Roberson {
3815f73a7ebSBruno Ducrot 	ksiginfo_t ksi;
38289bb1cefSJeff Roberson 	struct thread *ttd;
38389bb1cefSJeff Roberson 	struct proc *p;
38489bb1cefSJeff Roberson 	int error;
38589bb1cefSJeff Roberson 
38689bb1cefSJeff Roberson 	p = td->td_proc;
3875f73a7ebSBruno Ducrot 	ksiginfo_init(&ksi);
3885f73a7ebSBruno Ducrot 	ksi.ksi_signo = uap->sig;
389baf28b69SDavid Xu 	ksi.ksi_code = SI_LWP;
3905f73a7ebSBruno Ducrot 	ksi.ksi_pid = p->p_pid;
3915f73a7ebSBruno Ducrot 	ksi.ksi_uid = td->td_ucred->cr_ruid;
3920a5cd498SDavid Xu 	if (uap->id == -1) {
3930a5cd498SDavid Xu 		if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
39489bb1cefSJeff Roberson 			error = EINVAL;
3950a5cd498SDavid Xu 		} else {
3960a5cd498SDavid Xu 			error = ESRCH;
397cf7d9a8cSDavid Xu 			PROC_LOCK(p);
3980a5cd498SDavid Xu 			FOREACH_THREAD_IN_PROC(p, ttd) {
3990a5cd498SDavid Xu 				if (ttd != td) {
4000a5cd498SDavid Xu 					error = 0;
4010a5cd498SDavid Xu 					if (uap->sig == 0)
4020a5cd498SDavid Xu 						break;
403ad6eec7bSJohn Baldwin 					tdksignal(ttd, uap->sig, &ksi);
4040a5cd498SDavid Xu 				}
4050a5cd498SDavid Xu 			}
406cf7d9a8cSDavid Xu 			PROC_UNLOCK(p);
4070a5cd498SDavid Xu 		}
4080a5cd498SDavid Xu 	} else {
409cf7d9a8cSDavid Xu 		error = 0;
410cf7d9a8cSDavid Xu 		ttd = tdfind((lwpid_t)uap->id, p->p_pid);
4110a5cd498SDavid Xu 		if (ttd == NULL)
412cf7d9a8cSDavid Xu 			return (ESRCH);
413cf7d9a8cSDavid Xu 		if (uap->sig == 0)
4140a5cd498SDavid Xu 			;
4150a5cd498SDavid Xu 		else if (!_SIG_VALID(uap->sig))
4160a5cd498SDavid Xu 			error = EINVAL;
4170a5cd498SDavid Xu 		else
418ad6eec7bSJohn Baldwin 			tdksignal(ttd, uap->sig, &ksi);
419cf7d9a8cSDavid Xu 		PROC_UNLOCK(ttd->td_proc);
4200a5cd498SDavid Xu 	}
42189bb1cefSJeff Roberson 	return (error);
42289bb1cefSJeff Roberson }
4231713a516SMike Makonnen 
4241713a516SMike Makonnen int
4258451d0ddSKip Macy sys_thr_kill2(struct thread *td, struct thr_kill2_args *uap)
4260b1f0611SDavid Xu     /* pid_t pid, long id, int sig */
4270b1f0611SDavid Xu {
4285f73a7ebSBruno Ducrot 	ksiginfo_t ksi;
4290b1f0611SDavid Xu 	struct thread *ttd;
4300b1f0611SDavid Xu 	struct proc *p;
4310b1f0611SDavid Xu 	int error;
4320b1f0611SDavid Xu 
43314961ba7SRobert Watson 	AUDIT_ARG_SIGNUM(uap->sig);
4340b1f0611SDavid Xu 
4355f73a7ebSBruno Ducrot 	ksiginfo_init(&ksi);
4365f73a7ebSBruno Ducrot 	ksi.ksi_signo = uap->sig;
437baf28b69SDavid Xu 	ksi.ksi_code = SI_LWP;
4385f73a7ebSBruno Ducrot 	ksi.ksi_pid = td->td_proc->p_pid;
4395f73a7ebSBruno Ducrot 	ksi.ksi_uid = td->td_ucred->cr_ruid;
4400b1f0611SDavid Xu 	if (uap->id == -1) {
441cf7d9a8cSDavid Xu 		if ((p = pfind(uap->pid)) == NULL)
442cf7d9a8cSDavid Xu 			return (ESRCH);
443cf7d9a8cSDavid Xu 		AUDIT_ARG_PROCESS(p);
444cf7d9a8cSDavid Xu 		error = p_cansignal(td, p, uap->sig);
445cf7d9a8cSDavid Xu 		if (error) {
446cf7d9a8cSDavid Xu 			PROC_UNLOCK(p);
447cf7d9a8cSDavid Xu 			return (error);
448cf7d9a8cSDavid Xu 		}
4490b1f0611SDavid Xu 		if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
4500b1f0611SDavid Xu 			error = EINVAL;
4510b1f0611SDavid Xu 		} else {
4520b1f0611SDavid Xu 			error = ESRCH;
4530b1f0611SDavid Xu 			FOREACH_THREAD_IN_PROC(p, ttd) {
4540b1f0611SDavid Xu 				if (ttd != td) {
4550b1f0611SDavid Xu 					error = 0;
4560b1f0611SDavid Xu 					if (uap->sig == 0)
4570b1f0611SDavid Xu 						break;
458ad6eec7bSJohn Baldwin 					tdksignal(ttd, uap->sig, &ksi);
4590b1f0611SDavid Xu 				}
4600b1f0611SDavid Xu 			}
4610b1f0611SDavid Xu 		}
462cf7d9a8cSDavid Xu 		PROC_UNLOCK(p);
4630b1f0611SDavid Xu 	} else {
464cf7d9a8cSDavid Xu 		ttd = tdfind((lwpid_t)uap->id, uap->pid);
4650b1f0611SDavid Xu 		if (ttd == NULL)
466cf7d9a8cSDavid Xu 			return (ESRCH);
467cf7d9a8cSDavid Xu 		p = ttd->td_proc;
468cf7d9a8cSDavid Xu 		AUDIT_ARG_PROCESS(p);
469cf7d9a8cSDavid Xu 		error = p_cansignal(td, p, uap->sig);
470cf7d9a8cSDavid Xu 		if (uap->sig == 0)
4710b1f0611SDavid Xu 			;
4720b1f0611SDavid Xu 		else if (!_SIG_VALID(uap->sig))
4730b1f0611SDavid Xu 			error = EINVAL;
4740b1f0611SDavid Xu 		else
475ad6eec7bSJohn Baldwin 			tdksignal(ttd, uap->sig, &ksi);
4760b1f0611SDavid Xu 		PROC_UNLOCK(p);
477cf7d9a8cSDavid Xu 	}
4780b1f0611SDavid Xu 	return (error);
4790b1f0611SDavid Xu }
4800b1f0611SDavid Xu 
4810b1f0611SDavid Xu int
4828451d0ddSKip Macy sys_thr_suspend(struct thread *td, struct thr_suspend_args *uap)
4831713a516SMike Makonnen 	/* const struct timespec *timeout */
4841713a516SMike Makonnen {
485cda9a0d1SDavid Xu 	struct timespec ts, *tsp;
4861713a516SMike Makonnen 	int error;
4871713a516SMike Makonnen 
488cda9a0d1SDavid Xu 	tsp = NULL;
4891713a516SMike Makonnen 	if (uap->timeout != NULL) {
4909a1d0cf6SPeter Holm 		error = umtx_copyin_timeout(uap->timeout, &ts);
4911713a516SMike Makonnen 		if (error != 0)
4921713a516SMike Makonnen 			return (error);
493cda9a0d1SDavid Xu 		tsp = &ts;
494cda9a0d1SDavid Xu 	}
495cda9a0d1SDavid Xu 
496cda9a0d1SDavid Xu 	return (kern_thr_suspend(td, tsp));
497cda9a0d1SDavid Xu }
498cda9a0d1SDavid Xu 
499cda9a0d1SDavid Xu int
500cda9a0d1SDavid Xu kern_thr_suspend(struct thread *td, struct timespec *tsp)
501cda9a0d1SDavid Xu {
502cfca8a18SDavid Xu 	struct proc *p = td->td_proc;
503cda9a0d1SDavid Xu 	struct timeval tv;
5042961a782SDavid Xu 	int error = 0;
5052961a782SDavid Xu 	int timo = 0;
506cda9a0d1SDavid Xu 
507745fbd3aSDavid Xu 	if (td->td_pflags & TDP_WAKEUP) {
508745fbd3aSDavid Xu 		td->td_pflags &= ~TDP_WAKEUP;
509745fbd3aSDavid Xu 		return (0);
510745fbd3aSDavid Xu 	}
511745fbd3aSDavid Xu 
512cfca8a18SDavid Xu 	if (tsp != NULL) {
5132961a782SDavid Xu 		if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
5142961a782SDavid Xu 			error = EWOULDBLOCK;
5152961a782SDavid Xu 		else {
5162961a782SDavid Xu 			TIMESPEC_TO_TIMEVAL(&tv, tsp);
5172961a782SDavid Xu 			timo = tvtohz(&tv);
518cfca8a18SDavid Xu 		}
519cfca8a18SDavid Xu 	}
520cfca8a18SDavid Xu 
521cfca8a18SDavid Xu 	PROC_LOCK(p);
522cfca8a18SDavid Xu 	if (error == 0 && (td->td_flags & TDF_THRWAKEUP) == 0)
523cfca8a18SDavid Xu 		error = msleep((void *)td, &p->p_mtx,
5242961a782SDavid Xu 			 PCATCH, "lthr", timo);
5252961a782SDavid Xu 
526c1df5a1aSDavid Xu 	if (td->td_flags & TDF_THRWAKEUP) {
527982d11f8SJeff Roberson 		thread_lock(td);
5281713a516SMike Makonnen 		td->td_flags &= ~TDF_THRWAKEUP;
529982d11f8SJeff Roberson 		thread_unlock(td);
530cfca8a18SDavid Xu 		PROC_UNLOCK(p);
531c1df5a1aSDavid Xu 		return (0);
532c1df5a1aSDavid Xu 	}
533cfca8a18SDavid Xu 	PROC_UNLOCK(p);
534c1df5a1aSDavid Xu 	if (error == EWOULDBLOCK)
535c1df5a1aSDavid Xu 		error = ETIMEDOUT;
536c1df5a1aSDavid Xu 	else if (error == ERESTART) {
5372961a782SDavid Xu 		if (timo != 0)
538c1df5a1aSDavid Xu 			error = EINTR;
539c1df5a1aSDavid Xu 	}
540c1df5a1aSDavid Xu 	return (error);
5411713a516SMike Makonnen }
5421713a516SMike Makonnen 
5431713a516SMike Makonnen int
5448451d0ddSKip Macy sys_thr_wake(struct thread *td, struct thr_wake_args *uap)
545cd28f17dSMarcel Moolenaar 	/* long id */
5461713a516SMike Makonnen {
54744355392SDavid Xu 	struct proc *p;
548cd28f17dSMarcel Moolenaar 	struct thread *ttd;
5491713a516SMike Makonnen 
550745fbd3aSDavid Xu 	if (uap->id == td->td_tid) {
551745fbd3aSDavid Xu 		td->td_pflags |= TDP_WAKEUP;
552745fbd3aSDavid Xu 		return (0);
553745fbd3aSDavid Xu 	}
554745fbd3aSDavid Xu 
55544355392SDavid Xu 	p = td->td_proc;
556cf7d9a8cSDavid Xu 	ttd = tdfind((lwpid_t)uap->id, p->p_pid);
557cf7d9a8cSDavid Xu 	if (ttd == NULL)
5581713a516SMike Makonnen 		return (ESRCH);
559982d11f8SJeff Roberson 	thread_lock(ttd);
560cd28f17dSMarcel Moolenaar 	ttd->td_flags |= TDF_THRWAKEUP;
561982d11f8SJeff Roberson 	thread_unlock(ttd);
562c1df5a1aSDavid Xu 	wakeup((void *)ttd);
56344355392SDavid Xu 	PROC_UNLOCK(p);
5641713a516SMike Makonnen 	return (0);
5651713a516SMike Makonnen }
5669e7d7224SDavid Xu 
5679e7d7224SDavid Xu int
5688451d0ddSKip Macy sys_thr_set_name(struct thread *td, struct thr_set_name_args *uap)
5699e7d7224SDavid Xu {
570cf7d9a8cSDavid Xu 	struct proc *p;
5719e7d7224SDavid Xu 	char name[MAXCOMLEN + 1];
5729e7d7224SDavid Xu 	struct thread *ttd;
5739e7d7224SDavid Xu 	int error;
5749e7d7224SDavid Xu 
5759e7d7224SDavid Xu 	error = 0;
5769e7d7224SDavid Xu 	name[0] = '\0';
5779e7d7224SDavid Xu 	if (uap->name != NULL) {
5789e7d7224SDavid Xu 		error = copyinstr(uap->name, name, sizeof(name),
5799e7d7224SDavid Xu 			NULL);
5809e7d7224SDavid Xu 		if (error)
5819e7d7224SDavid Xu 			return (error);
5829e7d7224SDavid Xu 	}
583cf7d9a8cSDavid Xu 	p = td->td_proc;
584cf7d9a8cSDavid Xu 	ttd = tdfind((lwpid_t)uap->id, p->p_pid);
585cf7d9a8cSDavid Xu 	if (ttd == NULL)
586cf7d9a8cSDavid Xu 		return (ESRCH);
5879e7d7224SDavid Xu 	strcpy(ttd->td_name, name);
58844ad5475SJohn Baldwin #ifdef KTR
58944ad5475SJohn Baldwin 	sched_clear_tdname(ttd);
59044ad5475SJohn Baldwin #endif
5919e7d7224SDavid Xu 	PROC_UNLOCK(p);
5929e7d7224SDavid Xu 	return (error);
5939e7d7224SDavid Xu }
59409baafb4SDmitry Chagin 
59509baafb4SDmitry Chagin int
59609baafb4SDmitry Chagin kern_thr_alloc(struct proc *p, int pages, struct thread **ntd)
59709baafb4SDmitry Chagin {
59809baafb4SDmitry Chagin 
59909baafb4SDmitry Chagin 	/* Have race condition but it is cheap. */
60009baafb4SDmitry Chagin 	if (p->p_numthreads >= max_threads_per_proc) {
60109baafb4SDmitry Chagin 		++max_threads_hits;
60209baafb4SDmitry Chagin 		return (EPROCLIM);
60309baafb4SDmitry Chagin 	}
60409baafb4SDmitry Chagin 
60509baafb4SDmitry Chagin 	*ntd = thread_alloc(pages);
60609baafb4SDmitry Chagin 	if (*ntd == NULL)
60709baafb4SDmitry Chagin 		return (ENOMEM);
60809baafb4SDmitry Chagin 
60909baafb4SDmitry Chagin 	return (0);
61009baafb4SDmitry Chagin }
611