xref: /freebsd/sys/kern/kern_thr.c (revision 8a06de9e92937f2cb4d2a01d9c51d2485c958646)
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. */
1665c2cf818SKonstantin Belousov 	cpu_set_upcall(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 
2305c2cf818SKonstantin Belousov 	cpu_copy_thread(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;
237*8a06de9eSKonstantin Belousov 	newtd->td_rb_list = newtd->td_rbp_list = newtd->td_rb_inact = 0;
2384ea6a9a2SMateusz Guzik 	thread_cow_get(newtd, td);
23989bb1cefSJeff Roberson 
2405a170c1bSEd Schouten 	error = initialize_thread(newtd, thunk);
241c4bd610fSDavid Xu 	if (error != 0) {
2424ea6a9a2SMateusz Guzik 		thread_cow_free(newtd);
243c4bd610fSDavid Xu 		thread_free(newtd);
24458c77a9dSEdward Tomasz Napierala 		goto fail;
245c4bd610fSDavid Xu 	}
246c4bd610fSDavid Xu 
247203322f9SMateusz Guzik 	PROC_LOCK(p);
248203322f9SMateusz Guzik 	p->p_flag |= P_HADTHREADS;
2498460a577SJohn Birrell 	thread_link(newtd, p);
250c67ddc21SJulian Elischer 	bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
251982d11f8SJeff Roberson 	thread_lock(td);
252ed062c8dSJulian Elischer 	/* let the scheduler know about these things. */
253ed062c8dSJulian Elischer 	sched_fork_thread(td, newtd);
254982d11f8SJeff Roberson 	thread_unlock(td);
255b7edba77SJeff Roberson 	if (P_SHOULDSTOP(p))
256b7edba77SJeff Roberson 		newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
2575fcfab6eSJohn Baldwin 	if (p->p_flag2 & P2_LWP_EVENTS)
2585fcfab6eSJohn Baldwin 		newtd->td_dbgflags |= TDB_BORN;
2596520495aSAdrian Chadd 
2606520495aSAdrian Chadd 	/*
2616520495aSAdrian Chadd 	 * Copy the existing thread VM policy into the new thread.
2626520495aSAdrian Chadd 	 */
2636520495aSAdrian Chadd 	vm_domain_policy_localcopy(&newtd->td_vm_dom_policy,
2646520495aSAdrian Chadd 	    &td->td_vm_dom_policy);
2656520495aSAdrian Chadd 
266982d11f8SJeff Roberson 	PROC_UNLOCK(p);
267cf7d9a8cSDavid Xu 
268cf7d9a8cSDavid Xu 	tidhash_add(newtd);
269cf7d9a8cSDavid Xu 
270982d11f8SJeff Roberson 	thread_lock(newtd);
27173fa3e5bSDavid Xu 	if (rtp != NULL) {
2728460a577SJohn Birrell 		if (!(td->td_pri_class == PRI_TIMESHARE &&
2738460a577SJohn Birrell 		      rtp->type == RTP_PRIO_NORMAL)) {
2748460a577SJohn Birrell 			rtp_to_pri(rtp, newtd);
2758460a577SJohn Birrell 			sched_prio(newtd, newtd->td_user_pri);
2768460a577SJohn Birrell 		} /* ignore timesharing class */
2778460a577SJohn Birrell 	}
278ed062c8dSJulian Elischer 	TD_SET_CAN_RUN(newtd);
279f0393f06SJeff Roberson 	sched_add(newtd, SRQ_BORING);
280982d11f8SJeff Roberson 	thread_unlock(newtd);
28189bb1cefSJeff Roberson 
282c90c9021SEd Schouten 	return (0);
28358c77a9dSEdward Tomasz Napierala 
28458c77a9dSEdward Tomasz Napierala fail:
285afcc55f3SEdward Tomasz Napierala #ifdef RACCT
2864b5c9cf6SEdward Tomasz Napierala 	if (racct_enable) {
28758c77a9dSEdward Tomasz Napierala 		PROC_LOCK(p);
28858c77a9dSEdward Tomasz Napierala 		racct_sub(p, RACCT_NTHR, 1);
28958c77a9dSEdward Tomasz Napierala 		PROC_UNLOCK(p);
2904b5c9cf6SEdward Tomasz Napierala 	}
291afcc55f3SEdward Tomasz Napierala #endif
29258c77a9dSEdward Tomasz Napierala 	return (error);
29389bb1cefSJeff Roberson }
29489bb1cefSJeff Roberson 
29589bb1cefSJeff Roberson int
2968451d0ddSKip Macy sys_thr_self(struct thread *td, struct thr_self_args *uap)
297cd28f17dSMarcel Moolenaar     /* long *id */
29889bb1cefSJeff Roberson {
29989bb1cefSJeff Roberson 	int error;
30089bb1cefSJeff Roberson 
301cda9a0d1SDavid Xu 	error = suword_lwpid(uap->id, (unsigned)td->td_tid);
302cda9a0d1SDavid Xu 	if (error == -1)
303cda9a0d1SDavid Xu 		return (EFAULT);
30489bb1cefSJeff Roberson 	return (0);
30589bb1cefSJeff Roberson }
30689bb1cefSJeff Roberson 
30789bb1cefSJeff Roberson int
3088451d0ddSKip Macy sys_thr_exit(struct thread *td, struct thr_exit_args *uap)
309401901acSMike Makonnen     /* long *state */
31089bb1cefSJeff Roberson {
31189bb1cefSJeff Roberson 
3122a339d9eSKonstantin Belousov 	umtx_thread_exit(td);
3132a339d9eSKonstantin Belousov 
314401901acSMike Makonnen 	/* Signal userland that it can free the stack. */
3154938faa6SDavid Xu 	if ((void *)uap->state != NULL) {
316cda9a0d1SDavid Xu 		suword_lwpid(uap->state, 1);
3173bba58f2SDavid Xu 		kern_umtx_wake(td, uap->state, INT_MAX, 0);
3184938faa6SDavid Xu 	}
319401901acSMike Makonnen 
32095be6d2bSDmitry Chagin 	return (kern_thr_exit(td));
32195be6d2bSDmitry Chagin }
32258c77a9dSEdward Tomasz Napierala 
32395be6d2bSDmitry Chagin int
32495be6d2bSDmitry Chagin kern_thr_exit(struct thread *td)
32595be6d2bSDmitry Chagin {
32695be6d2bSDmitry Chagin 	struct proc *p;
32795be6d2bSDmitry Chagin 
32895be6d2bSDmitry Chagin 	p = td->td_proc;
32995be6d2bSDmitry Chagin 
3305fcfab6eSJohn Baldwin 	/*
3315fcfab6eSJohn Baldwin 	 * If all of the threads in a process call this routine to
3325fcfab6eSJohn Baldwin 	 * exit (e.g. all threads call pthread_exit()), exactly one
3335fcfab6eSJohn Baldwin 	 * thread should return to the caller to terminate the process
3345fcfab6eSJohn Baldwin 	 * instead of the thread.
3355fcfab6eSJohn Baldwin 	 *
3365fcfab6eSJohn Baldwin 	 * Checking p_numthreads alone is not sufficient since threads
3375fcfab6eSJohn Baldwin 	 * might be committed to terminating while the PROC_LOCK is
3385fcfab6eSJohn Baldwin 	 * dropped in either ptracestop() or while removing this thread
3395fcfab6eSJohn Baldwin 	 * from the tidhash.  Instead, the p_pendingexits field holds
3405fcfab6eSJohn Baldwin 	 * the count of threads in either of those states and a thread
3415fcfab6eSJohn Baldwin 	 * is considered the "last" thread if all of the other threads
3425fcfab6eSJohn Baldwin 	 * in a process are already terminating.
3435fcfab6eSJohn Baldwin 	 */
34489bb1cefSJeff Roberson 	PROC_LOCK(p);
3455fcfab6eSJohn Baldwin 	if (p->p_numthreads == p->p_pendingexits + 1) {
3465fcfab6eSJohn Baldwin 		/*
3475fcfab6eSJohn Baldwin 		 * Ignore attempts to shut down last thread in the
3485fcfab6eSJohn Baldwin 		 * proc.  This will actually call _exit(2) in the
3495fcfab6eSJohn Baldwin 		 * usermode trampoline when it returns.
3505fcfab6eSJohn Baldwin 		 */
3515fcfab6eSJohn Baldwin 		PROC_UNLOCK(p);
3525fcfab6eSJohn Baldwin 		return (0);
3535fcfab6eSJohn Baldwin 	}
35458c77a9dSEdward Tomasz Napierala 
3555fcfab6eSJohn Baldwin 	p->p_pendingexits++;
3565fcfab6eSJohn Baldwin 	td->td_dbgflags |= TDB_EXIT;
3575fcfab6eSJohn Baldwin 	if (p->p_flag & P_TRACED && p->p_flag2 & P2_LWP_EVENTS)
3585fcfab6eSJohn Baldwin 		ptracestop(td, SIGTRAP);
3595fcfab6eSJohn Baldwin 	PROC_UNLOCK(p);
3605fcfab6eSJohn Baldwin 	tidhash_remove(td);
3615fcfab6eSJohn Baldwin 	PROC_LOCK(p);
3625fcfab6eSJohn Baldwin 	p->p_pendingexits--;
3635fcfab6eSJohn Baldwin 
3645fcfab6eSJohn Baldwin 	/*
3655fcfab6eSJohn Baldwin 	 * The check above should prevent all other threads from this
3665fcfab6eSJohn Baldwin 	 * process from exiting while the PROC_LOCK is dropped, so
3675fcfab6eSJohn Baldwin 	 * there must be at least one other thread other than the
3685fcfab6eSJohn Baldwin 	 * current thread.
3695fcfab6eSJohn Baldwin 	 */
3705fcfab6eSJohn Baldwin 	KASSERT(p->p_numthreads > 1, ("too few threads"));
37147f6635cSEdward Tomasz Napierala 	racct_sub(p, RACCT_NTHR, 1);
3720d036d55SDavid Xu 	tdsigcleanup(td);
3730d036d55SDavid Xu 	PROC_SLOCK(p);
37471b7afb2SDavid Xu 	thread_stopped(p);
375ed062c8dSJulian Elischer 	thread_exit();
376ed062c8dSJulian Elischer 	/* NOTREACHED */
377ed062c8dSJulian Elischer }
37874d5b4afSKonstantin Belousov 
37989bb1cefSJeff Roberson int
3808451d0ddSKip Macy sys_thr_kill(struct thread *td, struct thr_kill_args *uap)
381cd28f17dSMarcel Moolenaar     /* long id, int sig */
38289bb1cefSJeff Roberson {
3835f73a7ebSBruno Ducrot 	ksiginfo_t ksi;
38489bb1cefSJeff Roberson 	struct thread *ttd;
38589bb1cefSJeff Roberson 	struct proc *p;
38689bb1cefSJeff Roberson 	int error;
38789bb1cefSJeff Roberson 
38889bb1cefSJeff Roberson 	p = td->td_proc;
3895f73a7ebSBruno Ducrot 	ksiginfo_init(&ksi);
3905f73a7ebSBruno Ducrot 	ksi.ksi_signo = uap->sig;
391baf28b69SDavid Xu 	ksi.ksi_code = SI_LWP;
3925f73a7ebSBruno Ducrot 	ksi.ksi_pid = p->p_pid;
3935f73a7ebSBruno Ducrot 	ksi.ksi_uid = td->td_ucred->cr_ruid;
3940a5cd498SDavid Xu 	if (uap->id == -1) {
3950a5cd498SDavid Xu 		if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
39689bb1cefSJeff Roberson 			error = EINVAL;
3970a5cd498SDavid Xu 		} else {
3980a5cd498SDavid Xu 			error = ESRCH;
399cf7d9a8cSDavid Xu 			PROC_LOCK(p);
4000a5cd498SDavid Xu 			FOREACH_THREAD_IN_PROC(p, ttd) {
4010a5cd498SDavid Xu 				if (ttd != td) {
4020a5cd498SDavid Xu 					error = 0;
4030a5cd498SDavid Xu 					if (uap->sig == 0)
4040a5cd498SDavid Xu 						break;
405ad6eec7bSJohn Baldwin 					tdksignal(ttd, uap->sig, &ksi);
4060a5cd498SDavid Xu 				}
4070a5cd498SDavid Xu 			}
408cf7d9a8cSDavid Xu 			PROC_UNLOCK(p);
4090a5cd498SDavid Xu 		}
4100a5cd498SDavid Xu 	} else {
411cf7d9a8cSDavid Xu 		error = 0;
412cf7d9a8cSDavid Xu 		ttd = tdfind((lwpid_t)uap->id, p->p_pid);
4130a5cd498SDavid Xu 		if (ttd == NULL)
414cf7d9a8cSDavid Xu 			return (ESRCH);
415cf7d9a8cSDavid Xu 		if (uap->sig == 0)
4160a5cd498SDavid Xu 			;
4170a5cd498SDavid Xu 		else if (!_SIG_VALID(uap->sig))
4180a5cd498SDavid Xu 			error = EINVAL;
4190a5cd498SDavid Xu 		else
420ad6eec7bSJohn Baldwin 			tdksignal(ttd, uap->sig, &ksi);
421cf7d9a8cSDavid Xu 		PROC_UNLOCK(ttd->td_proc);
4220a5cd498SDavid Xu 	}
42389bb1cefSJeff Roberson 	return (error);
42489bb1cefSJeff Roberson }
4251713a516SMike Makonnen 
4261713a516SMike Makonnen int
4278451d0ddSKip Macy sys_thr_kill2(struct thread *td, struct thr_kill2_args *uap)
4280b1f0611SDavid Xu     /* pid_t pid, long id, int sig */
4290b1f0611SDavid Xu {
4305f73a7ebSBruno Ducrot 	ksiginfo_t ksi;
4310b1f0611SDavid Xu 	struct thread *ttd;
4320b1f0611SDavid Xu 	struct proc *p;
4330b1f0611SDavid Xu 	int error;
4340b1f0611SDavid Xu 
43514961ba7SRobert Watson 	AUDIT_ARG_SIGNUM(uap->sig);
4360b1f0611SDavid Xu 
4375f73a7ebSBruno Ducrot 	ksiginfo_init(&ksi);
4385f73a7ebSBruno Ducrot 	ksi.ksi_signo = uap->sig;
439baf28b69SDavid Xu 	ksi.ksi_code = SI_LWP;
4405f73a7ebSBruno Ducrot 	ksi.ksi_pid = td->td_proc->p_pid;
4415f73a7ebSBruno Ducrot 	ksi.ksi_uid = td->td_ucred->cr_ruid;
4420b1f0611SDavid Xu 	if (uap->id == -1) {
443cf7d9a8cSDavid Xu 		if ((p = pfind(uap->pid)) == NULL)
444cf7d9a8cSDavid Xu 			return (ESRCH);
445cf7d9a8cSDavid Xu 		AUDIT_ARG_PROCESS(p);
446cf7d9a8cSDavid Xu 		error = p_cansignal(td, p, uap->sig);
447cf7d9a8cSDavid Xu 		if (error) {
448cf7d9a8cSDavid Xu 			PROC_UNLOCK(p);
449cf7d9a8cSDavid Xu 			return (error);
450cf7d9a8cSDavid Xu 		}
4510b1f0611SDavid Xu 		if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
4520b1f0611SDavid Xu 			error = EINVAL;
4530b1f0611SDavid Xu 		} else {
4540b1f0611SDavid Xu 			error = ESRCH;
4550b1f0611SDavid Xu 			FOREACH_THREAD_IN_PROC(p, ttd) {
4560b1f0611SDavid Xu 				if (ttd != td) {
4570b1f0611SDavid Xu 					error = 0;
4580b1f0611SDavid Xu 					if (uap->sig == 0)
4590b1f0611SDavid Xu 						break;
460ad6eec7bSJohn Baldwin 					tdksignal(ttd, uap->sig, &ksi);
4610b1f0611SDavid Xu 				}
4620b1f0611SDavid Xu 			}
4630b1f0611SDavid Xu 		}
464cf7d9a8cSDavid Xu 		PROC_UNLOCK(p);
4650b1f0611SDavid Xu 	} else {
466cf7d9a8cSDavid Xu 		ttd = tdfind((lwpid_t)uap->id, uap->pid);
4670b1f0611SDavid Xu 		if (ttd == NULL)
468cf7d9a8cSDavid Xu 			return (ESRCH);
469cf7d9a8cSDavid Xu 		p = ttd->td_proc;
470cf7d9a8cSDavid Xu 		AUDIT_ARG_PROCESS(p);
471cf7d9a8cSDavid Xu 		error = p_cansignal(td, p, uap->sig);
472cf7d9a8cSDavid Xu 		if (uap->sig == 0)
4730b1f0611SDavid Xu 			;
4740b1f0611SDavid Xu 		else if (!_SIG_VALID(uap->sig))
4750b1f0611SDavid Xu 			error = EINVAL;
4760b1f0611SDavid Xu 		else
477ad6eec7bSJohn Baldwin 			tdksignal(ttd, uap->sig, &ksi);
4780b1f0611SDavid Xu 		PROC_UNLOCK(p);
479cf7d9a8cSDavid Xu 	}
4800b1f0611SDavid Xu 	return (error);
4810b1f0611SDavid Xu }
4820b1f0611SDavid Xu 
4830b1f0611SDavid Xu int
4848451d0ddSKip Macy sys_thr_suspend(struct thread *td, struct thr_suspend_args *uap)
4851713a516SMike Makonnen 	/* const struct timespec *timeout */
4861713a516SMike Makonnen {
487cda9a0d1SDavid Xu 	struct timespec ts, *tsp;
4881713a516SMike Makonnen 	int error;
4891713a516SMike Makonnen 
490cda9a0d1SDavid Xu 	tsp = NULL;
4911713a516SMike Makonnen 	if (uap->timeout != NULL) {
4929a1d0cf6SPeter Holm 		error = umtx_copyin_timeout(uap->timeout, &ts);
4931713a516SMike Makonnen 		if (error != 0)
4941713a516SMike Makonnen 			return (error);
495cda9a0d1SDavid Xu 		tsp = &ts;
496cda9a0d1SDavid Xu 	}
497cda9a0d1SDavid Xu 
498cda9a0d1SDavid Xu 	return (kern_thr_suspend(td, tsp));
499cda9a0d1SDavid Xu }
500cda9a0d1SDavid Xu 
501cda9a0d1SDavid Xu int
502cda9a0d1SDavid Xu kern_thr_suspend(struct thread *td, struct timespec *tsp)
503cda9a0d1SDavid Xu {
504cfca8a18SDavid Xu 	struct proc *p = td->td_proc;
505cda9a0d1SDavid Xu 	struct timeval tv;
5062961a782SDavid Xu 	int error = 0;
5072961a782SDavid Xu 	int timo = 0;
508cda9a0d1SDavid Xu 
509745fbd3aSDavid Xu 	if (td->td_pflags & TDP_WAKEUP) {
510745fbd3aSDavid Xu 		td->td_pflags &= ~TDP_WAKEUP;
511745fbd3aSDavid Xu 		return (0);
512745fbd3aSDavid Xu 	}
513745fbd3aSDavid Xu 
514cfca8a18SDavid Xu 	if (tsp != NULL) {
5152961a782SDavid Xu 		if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
5162961a782SDavid Xu 			error = EWOULDBLOCK;
5172961a782SDavid Xu 		else {
5182961a782SDavid Xu 			TIMESPEC_TO_TIMEVAL(&tv, tsp);
5192961a782SDavid Xu 			timo = tvtohz(&tv);
520cfca8a18SDavid Xu 		}
521cfca8a18SDavid Xu 	}
522cfca8a18SDavid Xu 
523cfca8a18SDavid Xu 	PROC_LOCK(p);
524cfca8a18SDavid Xu 	if (error == 0 && (td->td_flags & TDF_THRWAKEUP) == 0)
525cfca8a18SDavid Xu 		error = msleep((void *)td, &p->p_mtx,
5262961a782SDavid Xu 			 PCATCH, "lthr", timo);
5272961a782SDavid Xu 
528c1df5a1aSDavid Xu 	if (td->td_flags & TDF_THRWAKEUP) {
529982d11f8SJeff Roberson 		thread_lock(td);
5301713a516SMike Makonnen 		td->td_flags &= ~TDF_THRWAKEUP;
531982d11f8SJeff Roberson 		thread_unlock(td);
532cfca8a18SDavid Xu 		PROC_UNLOCK(p);
533c1df5a1aSDavid Xu 		return (0);
534c1df5a1aSDavid Xu 	}
535cfca8a18SDavid Xu 	PROC_UNLOCK(p);
536c1df5a1aSDavid Xu 	if (error == EWOULDBLOCK)
537c1df5a1aSDavid Xu 		error = ETIMEDOUT;
538c1df5a1aSDavid Xu 	else if (error == ERESTART) {
5392961a782SDavid Xu 		if (timo != 0)
540c1df5a1aSDavid Xu 			error = EINTR;
541c1df5a1aSDavid Xu 	}
542c1df5a1aSDavid Xu 	return (error);
5431713a516SMike Makonnen }
5441713a516SMike Makonnen 
5451713a516SMike Makonnen int
5468451d0ddSKip Macy sys_thr_wake(struct thread *td, struct thr_wake_args *uap)
547cd28f17dSMarcel Moolenaar 	/* long id */
5481713a516SMike Makonnen {
54944355392SDavid Xu 	struct proc *p;
550cd28f17dSMarcel Moolenaar 	struct thread *ttd;
5511713a516SMike Makonnen 
552745fbd3aSDavid Xu 	if (uap->id == td->td_tid) {
553745fbd3aSDavid Xu 		td->td_pflags |= TDP_WAKEUP;
554745fbd3aSDavid Xu 		return (0);
555745fbd3aSDavid Xu 	}
556745fbd3aSDavid Xu 
55744355392SDavid Xu 	p = td->td_proc;
558cf7d9a8cSDavid Xu 	ttd = tdfind((lwpid_t)uap->id, p->p_pid);
559cf7d9a8cSDavid Xu 	if (ttd == NULL)
5601713a516SMike Makonnen 		return (ESRCH);
561982d11f8SJeff Roberson 	thread_lock(ttd);
562cd28f17dSMarcel Moolenaar 	ttd->td_flags |= TDF_THRWAKEUP;
563982d11f8SJeff Roberson 	thread_unlock(ttd);
564c1df5a1aSDavid Xu 	wakeup((void *)ttd);
56544355392SDavid Xu 	PROC_UNLOCK(p);
5661713a516SMike Makonnen 	return (0);
5671713a516SMike Makonnen }
5689e7d7224SDavid Xu 
5699e7d7224SDavid Xu int
5708451d0ddSKip Macy sys_thr_set_name(struct thread *td, struct thr_set_name_args *uap)
5719e7d7224SDavid Xu {
572cf7d9a8cSDavid Xu 	struct proc *p;
5739e7d7224SDavid Xu 	char name[MAXCOMLEN + 1];
5749e7d7224SDavid Xu 	struct thread *ttd;
5759e7d7224SDavid Xu 	int error;
5769e7d7224SDavid Xu 
5779e7d7224SDavid Xu 	error = 0;
5789e7d7224SDavid Xu 	name[0] = '\0';
5799e7d7224SDavid Xu 	if (uap->name != NULL) {
5809e7d7224SDavid Xu 		error = copyinstr(uap->name, name, sizeof(name),
5819e7d7224SDavid Xu 			NULL);
5829e7d7224SDavid Xu 		if (error)
5839e7d7224SDavid Xu 			return (error);
5849e7d7224SDavid Xu 	}
585cf7d9a8cSDavid Xu 	p = td->td_proc;
586cf7d9a8cSDavid Xu 	ttd = tdfind((lwpid_t)uap->id, p->p_pid);
587cf7d9a8cSDavid Xu 	if (ttd == NULL)
588cf7d9a8cSDavid Xu 		return (ESRCH);
5899e7d7224SDavid Xu 	strcpy(ttd->td_name, name);
59044ad5475SJohn Baldwin #ifdef KTR
59144ad5475SJohn Baldwin 	sched_clear_tdname(ttd);
59244ad5475SJohn Baldwin #endif
5939e7d7224SDavid Xu 	PROC_UNLOCK(p);
5949e7d7224SDavid Xu 	return (error);
5959e7d7224SDavid Xu }
59609baafb4SDmitry Chagin 
59709baafb4SDmitry Chagin int
59809baafb4SDmitry Chagin kern_thr_alloc(struct proc *p, int pages, struct thread **ntd)
59909baafb4SDmitry Chagin {
60009baafb4SDmitry Chagin 
60109baafb4SDmitry Chagin 	/* Have race condition but it is cheap. */
60209baafb4SDmitry Chagin 	if (p->p_numthreads >= max_threads_per_proc) {
60309baafb4SDmitry Chagin 		++max_threads_hits;
60409baafb4SDmitry Chagin 		return (EPROCLIM);
60509baafb4SDmitry Chagin 	}
60609baafb4SDmitry Chagin 
60709baafb4SDmitry Chagin 	*ntd = thread_alloc(pages);
60809baafb4SDmitry Chagin 	if (*ntd == NULL)
60909baafb4SDmitry Chagin 		return (ENOMEM);
61009baafb4SDmitry Chagin 
61109baafb4SDmitry Chagin 	return (0);
61209baafb4SDmitry Chagin }
613