xref: /freebsd/sys/kern/kern_thr.c (revision c90c9021e94dd95b34bb48c3f8bb29761379a8d8)
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>
3989bb1cefSJeff Roberson #include <sys/resourcevar.h>
40a22ec9d8SJeff Roberson #include <sys/sched.h>
41a8b491c1SJulian Elischer #include <sys/sysctl.h>
42ed062c8dSJulian Elischer #include <sys/smp.h>
43a66fde8dSJohn Baldwin #include <sys/syscallsubr.h>
4489bb1cefSJeff Roberson #include <sys/sysent.h>
4589bb1cefSJeff Roberson #include <sys/systm.h>
4689bb1cefSJeff Roberson #include <sys/sysproto.h>
4789bb1cefSJeff Roberson #include <sys/signalvar.h>
4889bb1cefSJeff Roberson #include <sys/ucontext.h>
4989bb1cefSJeff Roberson #include <sys/thr.h>
50a0712c99SDavid Xu #include <sys/rtprio.h>
514938faa6SDavid Xu #include <sys/umtx.h>
524938faa6SDavid Xu #include <sys/limits.h>
5389bb1cefSJeff Roberson 
5489bb1cefSJeff Roberson #include <machine/frame.h>
5589bb1cefSJeff Roberson 
560b1f0611SDavid Xu #include <security/audit/audit.h>
570b1f0611SDavid Xu 
58cda9a0d1SDavid Xu #ifdef COMPAT_IA32
59cda9a0d1SDavid Xu 
60cda9a0d1SDavid Xu static inline int
61cda9a0d1SDavid Xu suword_lwpid(void *addr, lwpid_t lwpid)
62cda9a0d1SDavid Xu {
63cda9a0d1SDavid Xu 	int error;
64cda9a0d1SDavid Xu 
65b4cf0e62SKonstantin Belousov 	if (SV_CURPROC_FLAG(SV_LP64))
66cda9a0d1SDavid Xu 		error = suword(addr, lwpid);
67cda9a0d1SDavid Xu 	else
68cda9a0d1SDavid Xu 		error = suword32(addr, lwpid);
69cda9a0d1SDavid Xu 	return (error);
70cda9a0d1SDavid Xu }
71cda9a0d1SDavid Xu 
72cda9a0d1SDavid Xu #else
73cda9a0d1SDavid Xu #define suword_lwpid	suword
74cda9a0d1SDavid Xu #endif
75cda9a0d1SDavid Xu 
76ed062c8dSJulian Elischer extern int max_threads_per_proc;
7794ec9c02SDavid Xu extern int max_threads_hits;
78a8b491c1SJulian Elischer 
79c4bd610fSDavid Xu static int create_thread(struct thread *td, mcontext_t *ctx,
80c4bd610fSDavid Xu 			 void (*start_func)(void *), void *arg,
81c4bd610fSDavid Xu 			 char *stack_base, size_t stack_size,
82c4bd610fSDavid Xu 			 char *tls_base,
83c4bd610fSDavid Xu 			 long *child_tid, long *parent_tid,
8473fa3e5bSDavid Xu 			 int flags, struct rtprio *rtp);
85c4bd610fSDavid Xu 
8689bb1cefSJeff Roberson /*
8789bb1cefSJeff Roberson  * System call interface.
8889bb1cefSJeff Roberson  */
8989bb1cefSJeff Roberson int
9089bb1cefSJeff Roberson thr_create(struct thread *td, struct thr_create_args *uap)
91cd28f17dSMarcel Moolenaar     /* ucontext_t *ctx, long *id, int flags */
9289bb1cefSJeff Roberson {
9389bb1cefSJeff Roberson 	ucontext_t ctx;
9489bb1cefSJeff Roberson 	int error;
9589bb1cefSJeff Roberson 
9689bb1cefSJeff Roberson 	if ((error = copyin(uap->ctx, &ctx, sizeof(ctx))))
9789bb1cefSJeff Roberson 		return (error);
9889bb1cefSJeff Roberson 
99c4bd610fSDavid Xu 	error = create_thread(td, &ctx.uc_mcontext, NULL, NULL,
100a0712c99SDavid Xu 		NULL, 0, NULL, uap->id, NULL, uap->flags, NULL);
101c4bd610fSDavid Xu 	return (error);
102c4bd610fSDavid Xu }
103c4bd610fSDavid Xu 
104c4bd610fSDavid Xu int
105c4bd610fSDavid Xu thr_new(struct thread *td, struct thr_new_args *uap)
106c4bd610fSDavid Xu     /* struct thr_param * */
107c4bd610fSDavid Xu {
108c4bd610fSDavid Xu 	struct thr_param param;
109cda9a0d1SDavid Xu 	int error;
110cda9a0d1SDavid Xu 
111cda9a0d1SDavid Xu 	if (uap->param_size < 0 || uap->param_size > sizeof(param))
112cda9a0d1SDavid Xu 		return (EINVAL);
113cda9a0d1SDavid Xu 	bzero(&param, sizeof(param));
114cda9a0d1SDavid Xu 	if ((error = copyin(uap->param, &param, uap->param_size)))
115cda9a0d1SDavid Xu 		return (error);
116cda9a0d1SDavid Xu 	return (kern_thr_new(td, &param));
117cda9a0d1SDavid Xu }
118cda9a0d1SDavid Xu 
119cda9a0d1SDavid Xu int
120cda9a0d1SDavid Xu kern_thr_new(struct thread *td, struct thr_param *param)
121cda9a0d1SDavid Xu {
12273fa3e5bSDavid Xu 	struct rtprio rtp, *rtpp;
123c4bd610fSDavid Xu 	int error;
124c4bd610fSDavid Xu 
12573fa3e5bSDavid Xu 	rtpp = NULL;
126cda9a0d1SDavid Xu 	if (param->rtp != 0) {
127cda9a0d1SDavid Xu 		error = copyin(param->rtp, &rtp, sizeof(struct rtprio));
128e4866772SRoman Divacky 		if (error)
129e4866772SRoman Divacky 			return (error);
13073fa3e5bSDavid Xu 		rtpp = &rtp;
131a0712c99SDavid Xu 	}
132cda9a0d1SDavid Xu 	error = create_thread(td, NULL, param->start_func, param->arg,
133cda9a0d1SDavid Xu 		param->stack_base, param->stack_size, param->tls_base,
134cda9a0d1SDavid Xu 		param->child_tid, param->parent_tid, param->flags,
13573fa3e5bSDavid Xu 		rtpp);
136c4bd610fSDavid Xu 	return (error);
137c4bd610fSDavid Xu }
138c4bd610fSDavid Xu 
139c4bd610fSDavid Xu static int
140c4bd610fSDavid Xu create_thread(struct thread *td, mcontext_t *ctx,
141c4bd610fSDavid Xu 	    void (*start_func)(void *), void *arg,
142c4bd610fSDavid Xu 	    char *stack_base, size_t stack_size,
143c4bd610fSDavid Xu 	    char *tls_base,
144c4bd610fSDavid Xu 	    long *child_tid, long *parent_tid,
14573fa3e5bSDavid Xu 	    int flags, struct rtprio *rtp)
146c4bd610fSDavid Xu {
147c4bd610fSDavid Xu 	stack_t stack;
148c4bd610fSDavid Xu 	struct thread *newtd;
149c4bd610fSDavid Xu 	struct proc *p;
150adc9c950SDavid Xu 	int error;
151c4bd610fSDavid Xu 
152c4bd610fSDavid Xu 	p = td->td_proc;
153c4bd610fSDavid Xu 
154c4bd610fSDavid Xu 	/* Have race condition but it is cheap. */
15594ec9c02SDavid Xu 	if (p->p_numthreads >= max_threads_per_proc) {
15694ec9c02SDavid Xu 		++max_threads_hits;
157ed062c8dSJulian Elischer 		return (EPROCLIM);
15894ec9c02SDavid Xu 	}
159c4bd610fSDavid Xu 
16073fa3e5bSDavid Xu 	if (rtp != NULL) {
16173fa3e5bSDavid Xu 		switch(rtp->type) {
16273fa3e5bSDavid Xu 		case RTP_PRIO_REALTIME:
16373fa3e5bSDavid Xu 		case RTP_PRIO_FIFO:
164a0712c99SDavid Xu 			/* Only root can set scheduler policy */
165acd3428bSRobert Watson 			if (priv_check(td, PRIV_SCHED_SETPOLICY) != 0)
166a0712c99SDavid Xu 				return (EPERM);
16773fa3e5bSDavid Xu 			if (rtp->prio > RTP_PRIO_MAX)
168a0712c99SDavid Xu 				return (EINVAL);
1692dca4ca7SDavid Xu 			break;
17073fa3e5bSDavid Xu 		case RTP_PRIO_NORMAL:
17173fa3e5bSDavid Xu 			rtp->prio = 0;
1722dca4ca7SDavid Xu 			break;
1732dca4ca7SDavid Xu 		default:
1742dca4ca7SDavid Xu 			return (EINVAL);
175a0712c99SDavid Xu 		}
176a0712c99SDavid Xu 	}
177a0712c99SDavid Xu 
178ad1e7d28SJulian Elischer 	/* Initialize our td */
179ed062c8dSJulian Elischer 	newtd = thread_alloc();
18089b57fcfSKonstantin Belousov 	if (newtd == NULL)
18189b57fcfSKonstantin Belousov 		return (ENOMEM);
182c4bd610fSDavid Xu 
18389bb1cefSJeff Roberson 	/*
184c4bd610fSDavid Xu 	 * Try the copyout as soon as we allocate the td so we don't
185c4bd610fSDavid Xu 	 * have to tear things down in a failure case below.
186c4bd610fSDavid Xu 	 * Here we copy out tid to two places, one for child and one
187c4bd610fSDavid Xu 	 * for parent, because pthread can create a detached thread,
188c4bd610fSDavid Xu 	 * if parent wants to safely access child tid, it has to provide
189c4bd610fSDavid Xu 	 * its storage, because child thread may exit quickly and
190c4bd610fSDavid Xu 	 * memory is freed before parent thread can access it.
19189bb1cefSJeff Roberson 	 */
192c4bd610fSDavid Xu 	if ((child_tid != NULL &&
193cda9a0d1SDavid Xu 	    suword_lwpid(child_tid, newtd->td_tid)) ||
194c4bd610fSDavid Xu 	    (parent_tid != NULL &&
195cda9a0d1SDavid Xu 	    suword_lwpid(parent_tid, newtd->td_tid))) {
196ed062c8dSJulian Elischer 		thread_free(newtd);
197cda9a0d1SDavid Xu 		return (EFAULT);
19889bb1cefSJeff Roberson 	}
199cda9a0d1SDavid Xu 
200ed062c8dSJulian Elischer 	bzero(&newtd->td_startzero,
2016db36923SDavid Schultz 	    __rangeof(struct thread, td_startzero, td_endzero));
202ed062c8dSJulian Elischer 	bcopy(&td->td_startcopy, &newtd->td_startcopy,
2036db36923SDavid Schultz 	    __rangeof(struct thread, td_startcopy, td_endcopy));
204c4bd610fSDavid Xu 	newtd->td_proc = td->td_proc;
205c4bd610fSDavid Xu 	newtd->td_ucred = crhold(td->td_ucred);
20689bb1cefSJeff Roberson 
207c4bd610fSDavid Xu 	cpu_set_upcall(newtd, td);
208c4bd610fSDavid Xu 
209c4bd610fSDavid Xu 	if (ctx != NULL) { /* old way to set user context */
210c4bd610fSDavid Xu 		error = set_mcontext(newtd, ctx);
211c4bd610fSDavid Xu 		if (error != 0) {
212c4bd610fSDavid Xu 			thread_free(newtd);
213c4bd610fSDavid Xu 			crfree(td->td_ucred);
214c4bd610fSDavid Xu 			return (error);
215c4bd610fSDavid Xu 		}
216c4bd610fSDavid Xu 	} else {
217c4bd610fSDavid Xu 		/* Set up our machine context. */
218c4bd610fSDavid Xu 		stack.ss_sp = stack_base;
219c4bd610fSDavid Xu 		stack.ss_size = stack_size;
220c4bd610fSDavid Xu 		/* Set upcall address to user thread entry function. */
221c4bd610fSDavid Xu 		cpu_set_upcall_kse(newtd, start_func, arg, &stack);
222c4bd610fSDavid Xu 		/* Setup user TLS address and TLS pointer register. */
223740fd64dSDavid Xu 		error = cpu_set_user_tls(newtd, tls_base);
224740fd64dSDavid Xu 		if (error != 0) {
225740fd64dSDavid Xu 			thread_free(newtd);
226740fd64dSDavid Xu 			crfree(td->td_ucred);
227740fd64dSDavid Xu 			return (error);
228740fd64dSDavid Xu 		}
229c4bd610fSDavid Xu 	}
230c4bd610fSDavid Xu 
2318460a577SJohn Birrell 	PROC_LOCK(td->td_proc);
2328460a577SJohn Birrell 	td->td_proc->p_flag |= P_HADTHREADS;
2338460a577SJohn Birrell 	newtd->td_sigmask = td->td_sigmask;
2348460a577SJohn Birrell 	thread_link(newtd, p);
235c67ddc21SJulian Elischer 	bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
236982d11f8SJeff Roberson 	thread_lock(td);
237ed062c8dSJulian Elischer 	/* let the scheduler know about these things. */
238ed062c8dSJulian Elischer 	sched_fork_thread(td, newtd);
239982d11f8SJeff Roberson 	thread_unlock(td);
240b7edba77SJeff Roberson 	if (P_SHOULDSTOP(p))
241b7edba77SJeff Roberson 		newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
242982d11f8SJeff Roberson 	PROC_UNLOCK(p);
243982d11f8SJeff Roberson 	thread_lock(newtd);
24473fa3e5bSDavid Xu 	if (rtp != NULL) {
2458460a577SJohn Birrell 		if (!(td->td_pri_class == PRI_TIMESHARE &&
2468460a577SJohn Birrell 		      rtp->type == RTP_PRIO_NORMAL)) {
2478460a577SJohn Birrell 			rtp_to_pri(rtp, newtd);
2488460a577SJohn Birrell 			sched_prio(newtd, newtd->td_user_pri);
2498460a577SJohn Birrell 		} /* ignore timesharing class */
2508460a577SJohn Birrell 	}
251ed062c8dSJulian Elischer 	TD_SET_CAN_RUN(newtd);
252f0393f06SJeff Roberson 	sched_add(newtd, SRQ_BORING);
253982d11f8SJeff Roberson 	thread_unlock(newtd);
25489bb1cefSJeff Roberson 
255c90c9021SEd Schouten 	return (0);
25689bb1cefSJeff Roberson }
25789bb1cefSJeff Roberson 
25889bb1cefSJeff Roberson int
25989bb1cefSJeff Roberson thr_self(struct thread *td, struct thr_self_args *uap)
260cd28f17dSMarcel Moolenaar     /* long *id */
26189bb1cefSJeff Roberson {
26289bb1cefSJeff Roberson 	int error;
26389bb1cefSJeff Roberson 
264cda9a0d1SDavid Xu 	error = suword_lwpid(uap->id, (unsigned)td->td_tid);
265cda9a0d1SDavid Xu 	if (error == -1)
266cda9a0d1SDavid Xu 		return (EFAULT);
26789bb1cefSJeff Roberson 	return (0);
26889bb1cefSJeff Roberson }
26989bb1cefSJeff Roberson 
27089bb1cefSJeff Roberson int
27189bb1cefSJeff Roberson thr_exit(struct thread *td, struct thr_exit_args *uap)
272401901acSMike Makonnen     /* long *state */
27389bb1cefSJeff Roberson {
27489bb1cefSJeff Roberson 	struct proc *p;
27589bb1cefSJeff Roberson 
27689bb1cefSJeff Roberson 	p = td->td_proc;
27789bb1cefSJeff Roberson 
278401901acSMike Makonnen 	/* Signal userland that it can free the stack. */
2794938faa6SDavid Xu 	if ((void *)uap->state != NULL) {
280cda9a0d1SDavid Xu 		suword_lwpid(uap->state, 1);
2813bba58f2SDavid Xu 		kern_umtx_wake(td, uap->state, INT_MAX, 0);
2824938faa6SDavid Xu 	}
283401901acSMike Makonnen 
28489bb1cefSJeff Roberson 	PROC_LOCK(p);
2859104847fSDavid Xu 	sigqueue_flush(&td->td_sigqueue);
2867b4a950aSDavid Xu 	PROC_SLOCK(p);
28789bb1cefSJeff Roberson 
28889bb1cefSJeff Roberson 	/*
289ed062c8dSJulian Elischer 	 * Shutting down last thread in the proc.  This will actually
290ed062c8dSJulian Elischer 	 * call exit() in the trampoline when it returns.
29189bb1cefSJeff Roberson 	 */
292ed062c8dSJulian Elischer 	if (p->p_numthreads != 1) {
29371b7afb2SDavid Xu 		thread_stopped(p);
294ed062c8dSJulian Elischer 		thread_exit();
295ed062c8dSJulian Elischer 		/* NOTREACHED */
296ed062c8dSJulian Elischer 	}
2977b4a950aSDavid Xu 	PROC_SUNLOCK(p);
298ed062c8dSJulian Elischer 	PROC_UNLOCK(p);
29989bb1cefSJeff Roberson 	return (0);
30089bb1cefSJeff Roberson }
30189bb1cefSJeff Roberson 
30289bb1cefSJeff Roberson int
30389bb1cefSJeff Roberson thr_kill(struct thread *td, struct thr_kill_args *uap)
304cd28f17dSMarcel Moolenaar     /* long id, int sig */
30589bb1cefSJeff Roberson {
30689bb1cefSJeff Roberson 	struct thread *ttd;
30789bb1cefSJeff Roberson 	struct proc *p;
30889bb1cefSJeff Roberson 	int error;
30989bb1cefSJeff Roberson 
31089bb1cefSJeff Roberson 	p = td->td_proc;
31189bb1cefSJeff Roberson 	error = 0;
31289bb1cefSJeff Roberson 	PROC_LOCK(p);
3130a5cd498SDavid Xu 	if (uap->id == -1) {
3140a5cd498SDavid Xu 		if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
31589bb1cefSJeff Roberson 			error = EINVAL;
3160a5cd498SDavid Xu 		} else {
3170a5cd498SDavid Xu 			error = ESRCH;
3180a5cd498SDavid Xu 			FOREACH_THREAD_IN_PROC(p, ttd) {
3190a5cd498SDavid Xu 				if (ttd != td) {
3200a5cd498SDavid Xu 					error = 0;
3210a5cd498SDavid Xu 					if (uap->sig == 0)
3220a5cd498SDavid Xu 						break;
3236d7b314bSDavid Xu 					tdsignal(p, ttd, uap->sig, NULL);
3240a5cd498SDavid Xu 				}
3250a5cd498SDavid Xu 			}
3260a5cd498SDavid Xu 		}
3270a5cd498SDavid Xu 	} else {
3280a5cd498SDavid Xu 		if (uap->id != td->td_tid)
3290a5cd498SDavid Xu 			ttd = thread_find(p, uap->id);
3300a5cd498SDavid Xu 		else
3310a5cd498SDavid Xu 			ttd = td;
3320a5cd498SDavid Xu 		if (ttd == NULL)
3330a5cd498SDavid Xu 			error = ESRCH;
3340a5cd498SDavid Xu 		else if (uap->sig == 0)
3350a5cd498SDavid Xu 			;
3360a5cd498SDavid Xu 		else if (!_SIG_VALID(uap->sig))
3370a5cd498SDavid Xu 			error = EINVAL;
3380a5cd498SDavid Xu 		else
3390a5cd498SDavid Xu 			tdsignal(p, ttd, uap->sig, NULL);
3400a5cd498SDavid Xu 	}
34189bb1cefSJeff Roberson 	PROC_UNLOCK(p);
34289bb1cefSJeff Roberson 	return (error);
34389bb1cefSJeff Roberson }
3441713a516SMike Makonnen 
3451713a516SMike Makonnen int
3460b1f0611SDavid Xu thr_kill2(struct thread *td, struct thr_kill2_args *uap)
3470b1f0611SDavid Xu     /* pid_t pid, long id, int sig */
3480b1f0611SDavid Xu {
3490b1f0611SDavid Xu 	struct thread *ttd;
3500b1f0611SDavid Xu 	struct proc *p;
3510b1f0611SDavid Xu 	int error;
3520b1f0611SDavid Xu 
3530b1f0611SDavid Xu 	AUDIT_ARG(signum, uap->sig);
3540b1f0611SDavid Xu 
3550b1f0611SDavid Xu 	if (uap->pid == td->td_proc->p_pid) {
3560b1f0611SDavid Xu 		p = td->td_proc;
3570b1f0611SDavid Xu 		PROC_LOCK(p);
3580b1f0611SDavid Xu 	} else if ((p = pfind(uap->pid)) == NULL) {
3590b1f0611SDavid Xu 		return (ESRCH);
3600b1f0611SDavid Xu 	}
3610b1f0611SDavid Xu 	AUDIT_ARG(process, p);
3620b1f0611SDavid Xu 
3630b1f0611SDavid Xu 	error = p_cansignal(td, p, uap->sig);
3640b1f0611SDavid Xu 	if (error == 0) {
3650b1f0611SDavid Xu 		if (uap->id == -1) {
3660b1f0611SDavid Xu 			if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
3670b1f0611SDavid Xu 				error = EINVAL;
3680b1f0611SDavid Xu 			} else {
3690b1f0611SDavid Xu 				error = ESRCH;
3700b1f0611SDavid Xu 				FOREACH_THREAD_IN_PROC(p, ttd) {
3710b1f0611SDavid Xu 					if (ttd != td) {
3720b1f0611SDavid Xu 						error = 0;
3730b1f0611SDavid Xu 						if (uap->sig == 0)
3740b1f0611SDavid Xu 							break;
3750b1f0611SDavid Xu 						tdsignal(p, ttd, uap->sig, NULL);
3760b1f0611SDavid Xu 					}
3770b1f0611SDavid Xu 				}
3780b1f0611SDavid Xu 			}
3790b1f0611SDavid Xu 		} else {
3800b1f0611SDavid Xu 			if (uap->id != td->td_tid)
3810b1f0611SDavid Xu 				ttd = thread_find(p, uap->id);
3820b1f0611SDavid Xu 			else
3830b1f0611SDavid Xu 				ttd = td;
3840b1f0611SDavid Xu 			if (ttd == NULL)
3850b1f0611SDavid Xu 				error = ESRCH;
3860b1f0611SDavid Xu 			else if (uap->sig == 0)
3870b1f0611SDavid Xu 				;
3880b1f0611SDavid Xu 			else if (!_SIG_VALID(uap->sig))
3890b1f0611SDavid Xu 				error = EINVAL;
3900b1f0611SDavid Xu 			else
3910b1f0611SDavid Xu 				tdsignal(p, ttd, uap->sig, NULL);
3920b1f0611SDavid Xu 		}
3930b1f0611SDavid Xu 	}
3940b1f0611SDavid Xu 	PROC_UNLOCK(p);
3950b1f0611SDavid Xu 	return (error);
3960b1f0611SDavid Xu }
3970b1f0611SDavid Xu 
3980b1f0611SDavid Xu int
3991713a516SMike Makonnen thr_suspend(struct thread *td, struct thr_suspend_args *uap)
4001713a516SMike Makonnen 	/* const struct timespec *timeout */
4011713a516SMike Makonnen {
402cda9a0d1SDavid Xu 	struct timespec ts, *tsp;
4031713a516SMike Makonnen 	int error;
4041713a516SMike Makonnen 
405cda9a0d1SDavid Xu 	tsp = NULL;
4061713a516SMike Makonnen 	if (uap->timeout != NULL) {
4071713a516SMike Makonnen 		error = copyin((const void *)uap->timeout, (void *)&ts,
4081713a516SMike Makonnen 		    sizeof(struct timespec));
4091713a516SMike Makonnen 		if (error != 0)
4101713a516SMike Makonnen 			return (error);
411cda9a0d1SDavid Xu 		tsp = &ts;
412cda9a0d1SDavid Xu 	}
413cda9a0d1SDavid Xu 
414cda9a0d1SDavid Xu 	return (kern_thr_suspend(td, tsp));
415cda9a0d1SDavid Xu }
416cda9a0d1SDavid Xu 
417cda9a0d1SDavid Xu int
418cda9a0d1SDavid Xu kern_thr_suspend(struct thread *td, struct timespec *tsp)
419cda9a0d1SDavid Xu {
420cda9a0d1SDavid Xu 	struct timeval tv;
421cda9a0d1SDavid Xu 	int error = 0, hz = 0;
422cda9a0d1SDavid Xu 
423cda9a0d1SDavid Xu 	if (tsp != NULL) {
424cda9a0d1SDavid Xu 		if (tsp->tv_nsec < 0 || tsp->tv_nsec > 1000000000)
4251713a516SMike Makonnen 			return (EINVAL);
426cda9a0d1SDavid Xu 		if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
4271713a516SMike Makonnen 			return (ETIMEDOUT);
428cda9a0d1SDavid Xu 		TIMESPEC_TO_TIMEVAL(&tv, tsp);
4291713a516SMike Makonnen 		hz = tvtohz(&tv);
4301713a516SMike Makonnen 	}
431745fbd3aSDavid Xu 
432745fbd3aSDavid Xu 	if (td->td_pflags & TDP_WAKEUP) {
433745fbd3aSDavid Xu 		td->td_pflags &= ~TDP_WAKEUP;
434745fbd3aSDavid Xu 		return (0);
435745fbd3aSDavid Xu 	}
436745fbd3aSDavid Xu 
4371713a516SMike Makonnen 	PROC_LOCK(td->td_proc);
438c21e3b38SMike Makonnen 	if ((td->td_flags & TDF_THRWAKEUP) == 0)
4390f180a7cSJohn Baldwin 		error = msleep((void *)td, &td->td_proc->p_mtx, PCATCH, "lthr",
4400f180a7cSJohn Baldwin 		    hz);
441c1df5a1aSDavid Xu 	if (td->td_flags & TDF_THRWAKEUP) {
442982d11f8SJeff Roberson 		thread_lock(td);
4431713a516SMike Makonnen 		td->td_flags &= ~TDF_THRWAKEUP;
444982d11f8SJeff Roberson 		thread_unlock(td);
4451713a516SMike Makonnen 		PROC_UNLOCK(td->td_proc);
446c1df5a1aSDavid Xu 		return (0);
447c1df5a1aSDavid Xu 	}
448c1df5a1aSDavid Xu 	PROC_UNLOCK(td->td_proc);
449c1df5a1aSDavid Xu 	if (error == EWOULDBLOCK)
450c1df5a1aSDavid Xu 		error = ETIMEDOUT;
451c1df5a1aSDavid Xu 	else if (error == ERESTART) {
452c1df5a1aSDavid Xu 		if (hz != 0)
453c1df5a1aSDavid Xu 			error = EINTR;
454c1df5a1aSDavid Xu 	}
455c1df5a1aSDavid Xu 	return (error);
4561713a516SMike Makonnen }
4571713a516SMike Makonnen 
4581713a516SMike Makonnen int
4591713a516SMike Makonnen thr_wake(struct thread *td, struct thr_wake_args *uap)
460cd28f17dSMarcel Moolenaar 	/* long id */
4611713a516SMike Makonnen {
46244355392SDavid Xu 	struct proc *p;
463cd28f17dSMarcel Moolenaar 	struct thread *ttd;
4641713a516SMike Makonnen 
465745fbd3aSDavid Xu 	if (uap->id == td->td_tid) {
466745fbd3aSDavid Xu 		td->td_pflags |= TDP_WAKEUP;
467745fbd3aSDavid Xu 		return (0);
468745fbd3aSDavid Xu 	}
469745fbd3aSDavid Xu 
47044355392SDavid Xu 	p = td->td_proc;
47144355392SDavid Xu 	PROC_LOCK(p);
47244355392SDavid Xu 	ttd = thread_find(p, uap->id);
4731713a516SMike Makonnen 	if (ttd == NULL) {
47444355392SDavid Xu 		PROC_UNLOCK(p);
4751713a516SMike Makonnen 		return (ESRCH);
4761713a516SMike Makonnen 	}
477982d11f8SJeff Roberson 	thread_lock(ttd);
478cd28f17dSMarcel Moolenaar 	ttd->td_flags |= TDF_THRWAKEUP;
479982d11f8SJeff Roberson 	thread_unlock(ttd);
480c1df5a1aSDavid Xu 	wakeup((void *)ttd);
48144355392SDavid Xu 	PROC_UNLOCK(p);
4821713a516SMike Makonnen 	return (0);
4831713a516SMike Makonnen }
4849e7d7224SDavid Xu 
4859e7d7224SDavid Xu int
4869e7d7224SDavid Xu thr_set_name(struct thread *td, struct thr_set_name_args *uap)
4879e7d7224SDavid Xu {
4889e7d7224SDavid Xu 	struct proc *p = td->td_proc;
4899e7d7224SDavid Xu 	char name[MAXCOMLEN + 1];
4909e7d7224SDavid Xu 	struct thread *ttd;
4919e7d7224SDavid Xu 	int error;
4929e7d7224SDavid Xu 
4939e7d7224SDavid Xu 	error = 0;
4949e7d7224SDavid Xu 	name[0] = '\0';
4959e7d7224SDavid Xu 	if (uap->name != NULL) {
4969e7d7224SDavid Xu 		error = copyinstr(uap->name, name, sizeof(name),
4979e7d7224SDavid Xu 			NULL);
4989e7d7224SDavid Xu 		if (error)
4999e7d7224SDavid Xu 			return (error);
5009e7d7224SDavid Xu 	}
5019e7d7224SDavid Xu 	PROC_LOCK(p);
5029e7d7224SDavid Xu 	if (uap->id == td->td_tid)
5039e7d7224SDavid Xu 		ttd = td;
5049e7d7224SDavid Xu 	else
5059e7d7224SDavid Xu 		ttd = thread_find(p, uap->id);
5069e7d7224SDavid Xu 	if (ttd != NULL)
5079e7d7224SDavid Xu 		strcpy(ttd->td_name, name);
5089e7d7224SDavid Xu 	else
5099e7d7224SDavid Xu 		error = ESRCH;
5109e7d7224SDavid Xu 	PROC_UNLOCK(p);
5119e7d7224SDavid Xu 	return (error);
5129e7d7224SDavid Xu }
513