xref: /freebsd/sys/kern/kern_thr.c (revision cfca8a186287d7920036f3b83fb41cf157c42dd1)
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 
58841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32
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 */
1798a945d10SKonstantin Belousov 	newtd = thread_alloc(0);
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);
243cf7d9a8cSDavid Xu 
244cf7d9a8cSDavid Xu 	tidhash_add(newtd);
245cf7d9a8cSDavid Xu 
246982d11f8SJeff Roberson 	thread_lock(newtd);
24773fa3e5bSDavid Xu 	if (rtp != NULL) {
2488460a577SJohn Birrell 		if (!(td->td_pri_class == PRI_TIMESHARE &&
2498460a577SJohn Birrell 		      rtp->type == RTP_PRIO_NORMAL)) {
2508460a577SJohn Birrell 			rtp_to_pri(rtp, newtd);
2518460a577SJohn Birrell 			sched_prio(newtd, newtd->td_user_pri);
2528460a577SJohn Birrell 		} /* ignore timesharing class */
2538460a577SJohn Birrell 	}
254ed062c8dSJulian Elischer 	TD_SET_CAN_RUN(newtd);
255f0393f06SJeff Roberson 	sched_add(newtd, SRQ_BORING);
256982d11f8SJeff Roberson 	thread_unlock(newtd);
25789bb1cefSJeff Roberson 
258c90c9021SEd Schouten 	return (0);
25989bb1cefSJeff Roberson }
26089bb1cefSJeff Roberson 
26189bb1cefSJeff Roberson int
26289bb1cefSJeff Roberson thr_self(struct thread *td, struct thr_self_args *uap)
263cd28f17dSMarcel Moolenaar     /* long *id */
26489bb1cefSJeff Roberson {
26589bb1cefSJeff Roberson 	int error;
26689bb1cefSJeff Roberson 
267cda9a0d1SDavid Xu 	error = suword_lwpid(uap->id, (unsigned)td->td_tid);
268cda9a0d1SDavid Xu 	if (error == -1)
269cda9a0d1SDavid Xu 		return (EFAULT);
27089bb1cefSJeff Roberson 	return (0);
27189bb1cefSJeff Roberson }
27289bb1cefSJeff Roberson 
27389bb1cefSJeff Roberson int
27489bb1cefSJeff Roberson thr_exit(struct thread *td, struct thr_exit_args *uap)
275401901acSMike Makonnen     /* long *state */
27689bb1cefSJeff Roberson {
27789bb1cefSJeff Roberson 	struct proc *p;
27889bb1cefSJeff Roberson 
27989bb1cefSJeff Roberson 	p = td->td_proc;
28089bb1cefSJeff Roberson 
281401901acSMike Makonnen 	/* Signal userland that it can free the stack. */
2824938faa6SDavid Xu 	if ((void *)uap->state != NULL) {
283cda9a0d1SDavid Xu 		suword_lwpid(uap->state, 1);
2843bba58f2SDavid Xu 		kern_umtx_wake(td, uap->state, INT_MAX, 0);
2854938faa6SDavid Xu 	}
286401901acSMike Makonnen 
287cf7d9a8cSDavid Xu 	tidhash_remove(td);
288cf7d9a8cSDavid Xu 
28989bb1cefSJeff Roberson 	PROC_LOCK(p);
2906b286ee8SKonstantin Belousov 	tdsigcleanup(td);
2917b4a950aSDavid Xu 	PROC_SLOCK(p);
29289bb1cefSJeff Roberson 
29389bb1cefSJeff Roberson 	/*
294ed062c8dSJulian Elischer 	 * Shutting down last thread in the proc.  This will actually
295ed062c8dSJulian Elischer 	 * call exit() in the trampoline when it returns.
29689bb1cefSJeff Roberson 	 */
297ed062c8dSJulian Elischer 	if (p->p_numthreads != 1) {
29871b7afb2SDavid Xu 		thread_stopped(p);
299ed062c8dSJulian Elischer 		thread_exit();
300ed062c8dSJulian Elischer 		/* NOTREACHED */
301ed062c8dSJulian Elischer 	}
3027b4a950aSDavid Xu 	PROC_SUNLOCK(p);
303ed062c8dSJulian Elischer 	PROC_UNLOCK(p);
30489bb1cefSJeff Roberson 	return (0);
30589bb1cefSJeff Roberson }
30689bb1cefSJeff Roberson 
30789bb1cefSJeff Roberson int
30889bb1cefSJeff Roberson thr_kill(struct thread *td, struct thr_kill_args *uap)
309cd28f17dSMarcel Moolenaar     /* long id, int sig */
31089bb1cefSJeff Roberson {
3115f73a7ebSBruno Ducrot 	ksiginfo_t ksi;
31289bb1cefSJeff Roberson 	struct thread *ttd;
31389bb1cefSJeff Roberson 	struct proc *p;
31489bb1cefSJeff Roberson 	int error;
31589bb1cefSJeff Roberson 
31689bb1cefSJeff Roberson 	p = td->td_proc;
3175f73a7ebSBruno Ducrot 	ksiginfo_init(&ksi);
3185f73a7ebSBruno Ducrot 	ksi.ksi_signo = uap->sig;
319baf28b69SDavid Xu 	ksi.ksi_code = SI_LWP;
3205f73a7ebSBruno Ducrot 	ksi.ksi_pid = p->p_pid;
3215f73a7ebSBruno Ducrot 	ksi.ksi_uid = td->td_ucred->cr_ruid;
3220a5cd498SDavid Xu 	if (uap->id == -1) {
3230a5cd498SDavid Xu 		if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
32489bb1cefSJeff Roberson 			error = EINVAL;
3250a5cd498SDavid Xu 		} else {
3260a5cd498SDavid Xu 			error = ESRCH;
327cf7d9a8cSDavid Xu 			PROC_LOCK(p);
3280a5cd498SDavid Xu 			FOREACH_THREAD_IN_PROC(p, ttd) {
3290a5cd498SDavid Xu 				if (ttd != td) {
3300a5cd498SDavid Xu 					error = 0;
3310a5cd498SDavid Xu 					if (uap->sig == 0)
3320a5cd498SDavid Xu 						break;
333ad6eec7bSJohn Baldwin 					tdksignal(ttd, uap->sig, &ksi);
3340a5cd498SDavid Xu 				}
3350a5cd498SDavid Xu 			}
336cf7d9a8cSDavid Xu 			PROC_UNLOCK(p);
3370a5cd498SDavid Xu 		}
3380a5cd498SDavid Xu 	} else {
339cf7d9a8cSDavid Xu 		error = 0;
340cf7d9a8cSDavid Xu 		ttd = tdfind((lwpid_t)uap->id, p->p_pid);
3410a5cd498SDavid Xu 		if (ttd == NULL)
342cf7d9a8cSDavid Xu 			return (ESRCH);
343cf7d9a8cSDavid Xu 		if (uap->sig == 0)
3440a5cd498SDavid Xu 			;
3450a5cd498SDavid Xu 		else if (!_SIG_VALID(uap->sig))
3460a5cd498SDavid Xu 			error = EINVAL;
3470a5cd498SDavid Xu 		else
348ad6eec7bSJohn Baldwin 			tdksignal(ttd, uap->sig, &ksi);
349cf7d9a8cSDavid Xu 		PROC_UNLOCK(ttd->td_proc);
3500a5cd498SDavid Xu 	}
35189bb1cefSJeff Roberson 	return (error);
35289bb1cefSJeff Roberson }
3531713a516SMike Makonnen 
3541713a516SMike Makonnen int
3550b1f0611SDavid Xu thr_kill2(struct thread *td, struct thr_kill2_args *uap)
3560b1f0611SDavid Xu     /* pid_t pid, long id, int sig */
3570b1f0611SDavid Xu {
3585f73a7ebSBruno Ducrot 	ksiginfo_t ksi;
3590b1f0611SDavid Xu 	struct thread *ttd;
3600b1f0611SDavid Xu 	struct proc *p;
3610b1f0611SDavid Xu 	int error;
3620b1f0611SDavid Xu 
36314961ba7SRobert Watson 	AUDIT_ARG_SIGNUM(uap->sig);
3640b1f0611SDavid Xu 
3655f73a7ebSBruno Ducrot 	ksiginfo_init(&ksi);
3665f73a7ebSBruno Ducrot 	ksi.ksi_signo = uap->sig;
367baf28b69SDavid Xu 	ksi.ksi_code = SI_LWP;
3685f73a7ebSBruno Ducrot 	ksi.ksi_pid = td->td_proc->p_pid;
3695f73a7ebSBruno Ducrot 	ksi.ksi_uid = td->td_ucred->cr_ruid;
3700b1f0611SDavid Xu 	if (uap->id == -1) {
371cf7d9a8cSDavid Xu 		if ((p = pfind(uap->pid)) == NULL)
372cf7d9a8cSDavid Xu 			return (ESRCH);
373cf7d9a8cSDavid Xu 		AUDIT_ARG_PROCESS(p);
374cf7d9a8cSDavid Xu 		error = p_cansignal(td, p, uap->sig);
375cf7d9a8cSDavid Xu 		if (error) {
376cf7d9a8cSDavid Xu 			PROC_UNLOCK(p);
377cf7d9a8cSDavid Xu 			return (error);
378cf7d9a8cSDavid Xu 		}
3790b1f0611SDavid Xu 		if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
3800b1f0611SDavid Xu 			error = EINVAL;
3810b1f0611SDavid Xu 		} else {
3820b1f0611SDavid Xu 			error = ESRCH;
3830b1f0611SDavid Xu 			FOREACH_THREAD_IN_PROC(p, ttd) {
3840b1f0611SDavid Xu 				if (ttd != td) {
3850b1f0611SDavid Xu 					error = 0;
3860b1f0611SDavid Xu 					if (uap->sig == 0)
3870b1f0611SDavid Xu 						break;
388ad6eec7bSJohn Baldwin 					tdksignal(ttd, uap->sig, &ksi);
3890b1f0611SDavid Xu 				}
3900b1f0611SDavid Xu 			}
3910b1f0611SDavid Xu 		}
392cf7d9a8cSDavid Xu 		PROC_UNLOCK(p);
3930b1f0611SDavid Xu 	} else {
394cf7d9a8cSDavid Xu 		ttd = tdfind((lwpid_t)uap->id, uap->pid);
3950b1f0611SDavid Xu 		if (ttd == NULL)
396cf7d9a8cSDavid Xu 			return (ESRCH);
397cf7d9a8cSDavid Xu 		p = ttd->td_proc;
398cf7d9a8cSDavid Xu 		AUDIT_ARG_PROCESS(p);
399cf7d9a8cSDavid Xu 		error = p_cansignal(td, p, uap->sig);
400cf7d9a8cSDavid Xu 		if (uap->sig == 0)
4010b1f0611SDavid Xu 			;
4020b1f0611SDavid Xu 		else if (!_SIG_VALID(uap->sig))
4030b1f0611SDavid Xu 			error = EINVAL;
4040b1f0611SDavid Xu 		else
405ad6eec7bSJohn Baldwin 			tdksignal(ttd, uap->sig, &ksi);
4060b1f0611SDavid Xu 		PROC_UNLOCK(p);
407cf7d9a8cSDavid Xu 	}
4080b1f0611SDavid Xu 	return (error);
4090b1f0611SDavid Xu }
4100b1f0611SDavid Xu 
4110b1f0611SDavid Xu int
4121713a516SMike Makonnen thr_suspend(struct thread *td, struct thr_suspend_args *uap)
4131713a516SMike Makonnen 	/* const struct timespec *timeout */
4141713a516SMike Makonnen {
415cda9a0d1SDavid Xu 	struct timespec ts, *tsp;
4161713a516SMike Makonnen 	int error;
4171713a516SMike Makonnen 
418cda9a0d1SDavid Xu 	tsp = NULL;
4191713a516SMike Makonnen 	if (uap->timeout != NULL) {
4201713a516SMike Makonnen 		error = copyin((const void *)uap->timeout, (void *)&ts,
4211713a516SMike Makonnen 		    sizeof(struct timespec));
4221713a516SMike Makonnen 		if (error != 0)
4231713a516SMike Makonnen 			return (error);
424cda9a0d1SDavid Xu 		tsp = &ts;
425cda9a0d1SDavid Xu 	}
426cda9a0d1SDavid Xu 
427cda9a0d1SDavid Xu 	return (kern_thr_suspend(td, tsp));
428cda9a0d1SDavid Xu }
429cda9a0d1SDavid Xu 
430cda9a0d1SDavid Xu int
431cda9a0d1SDavid Xu kern_thr_suspend(struct thread *td, struct timespec *tsp)
432cda9a0d1SDavid Xu {
433*cfca8a18SDavid Xu 	struct proc *p = td->td_proc;
434cda9a0d1SDavid Xu 	struct timeval tv;
4352961a782SDavid Xu 	int error = 0;
4362961a782SDavid Xu 	int timo = 0;
437cda9a0d1SDavid Xu 
438745fbd3aSDavid Xu 	if (td->td_pflags & TDP_WAKEUP) {
439745fbd3aSDavid Xu 		td->td_pflags &= ~TDP_WAKEUP;
440745fbd3aSDavid Xu 		return (0);
441745fbd3aSDavid Xu 	}
442745fbd3aSDavid Xu 
443*cfca8a18SDavid Xu 	if (tsp != NULL) {
444*cfca8a18SDavid Xu 		if (tsp->tv_nsec < 0 || tsp->tv_nsec > 1000000000)
445*cfca8a18SDavid Xu 			return (EINVAL);
4462961a782SDavid Xu 		if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
4472961a782SDavid Xu 			error = EWOULDBLOCK;
4482961a782SDavid Xu 		else {
4492961a782SDavid Xu 			TIMESPEC_TO_TIMEVAL(&tv, tsp);
4502961a782SDavid Xu 			timo = tvtohz(&tv);
451*cfca8a18SDavid Xu 		}
452*cfca8a18SDavid Xu 	}
453*cfca8a18SDavid Xu 
454*cfca8a18SDavid Xu 	PROC_LOCK(p);
455*cfca8a18SDavid Xu 	if (error == 0 && (td->td_flags & TDF_THRWAKEUP) == 0)
456*cfca8a18SDavid Xu 		error = msleep((void *)td, &p->p_mtx,
4572961a782SDavid Xu 			 PCATCH, "lthr", timo);
4582961a782SDavid Xu 
459c1df5a1aSDavid Xu 	if (td->td_flags & TDF_THRWAKEUP) {
460982d11f8SJeff Roberson 		thread_lock(td);
4611713a516SMike Makonnen 		td->td_flags &= ~TDF_THRWAKEUP;
462982d11f8SJeff Roberson 		thread_unlock(td);
463*cfca8a18SDavid Xu 		PROC_UNLOCK(p);
464c1df5a1aSDavid Xu 		return (0);
465c1df5a1aSDavid Xu 	}
466*cfca8a18SDavid Xu 	PROC_UNLOCK(p);
467c1df5a1aSDavid Xu 	if (error == EWOULDBLOCK)
468c1df5a1aSDavid Xu 		error = ETIMEDOUT;
469c1df5a1aSDavid Xu 	else if (error == ERESTART) {
4702961a782SDavid Xu 		if (timo != 0)
471c1df5a1aSDavid Xu 			error = EINTR;
472c1df5a1aSDavid Xu 	}
473c1df5a1aSDavid Xu 	return (error);
4741713a516SMike Makonnen }
4751713a516SMike Makonnen 
4761713a516SMike Makonnen int
4771713a516SMike Makonnen thr_wake(struct thread *td, struct thr_wake_args *uap)
478cd28f17dSMarcel Moolenaar 	/* long id */
4791713a516SMike Makonnen {
48044355392SDavid Xu 	struct proc *p;
481cd28f17dSMarcel Moolenaar 	struct thread *ttd;
4821713a516SMike Makonnen 
483745fbd3aSDavid Xu 	if (uap->id == td->td_tid) {
484745fbd3aSDavid Xu 		td->td_pflags |= TDP_WAKEUP;
485745fbd3aSDavid Xu 		return (0);
486745fbd3aSDavid Xu 	}
487745fbd3aSDavid Xu 
48844355392SDavid Xu 	p = td->td_proc;
489cf7d9a8cSDavid Xu 	ttd = tdfind((lwpid_t)uap->id, p->p_pid);
490cf7d9a8cSDavid Xu 	if (ttd == NULL)
4911713a516SMike Makonnen 		return (ESRCH);
492982d11f8SJeff Roberson 	thread_lock(ttd);
493cd28f17dSMarcel Moolenaar 	ttd->td_flags |= TDF_THRWAKEUP;
494982d11f8SJeff Roberson 	thread_unlock(ttd);
495c1df5a1aSDavid Xu 	wakeup((void *)ttd);
49644355392SDavid Xu 	PROC_UNLOCK(p);
4971713a516SMike Makonnen 	return (0);
4981713a516SMike Makonnen }
4999e7d7224SDavid Xu 
5009e7d7224SDavid Xu int
5019e7d7224SDavid Xu thr_set_name(struct thread *td, struct thr_set_name_args *uap)
5029e7d7224SDavid Xu {
503cf7d9a8cSDavid Xu 	struct proc *p;
5049e7d7224SDavid Xu 	char name[MAXCOMLEN + 1];
5059e7d7224SDavid Xu 	struct thread *ttd;
5069e7d7224SDavid Xu 	int error;
5079e7d7224SDavid Xu 
5089e7d7224SDavid Xu 	error = 0;
5099e7d7224SDavid Xu 	name[0] = '\0';
5109e7d7224SDavid Xu 	if (uap->name != NULL) {
5119e7d7224SDavid Xu 		error = copyinstr(uap->name, name, sizeof(name),
5129e7d7224SDavid Xu 			NULL);
5139e7d7224SDavid Xu 		if (error)
5149e7d7224SDavid Xu 			return (error);
5159e7d7224SDavid Xu 	}
516cf7d9a8cSDavid Xu 	p = td->td_proc;
517cf7d9a8cSDavid Xu 	ttd = tdfind((lwpid_t)uap->id, p->p_pid);
518cf7d9a8cSDavid Xu 	if (ttd == NULL)
519cf7d9a8cSDavid Xu 		return (ESRCH);
5209e7d7224SDavid Xu 	strcpy(ttd->td_name, name);
5219e7d7224SDavid Xu 	PROC_UNLOCK(p);
5229e7d7224SDavid Xu 	return (error);
5239e7d7224SDavid Xu }
524