xref: /freebsd/sys/kern/kern_thr.c (revision 2f6a179eb910129fb812c1ad1bdc300da1203dc0)
1 /*-
2  * Copyright (c) 2003, Jeffrey Roberson <jeff@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include "opt_compat.h"
31 #include "opt_posix.h"
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/lock.h>
35 #include <sys/mutex.h>
36 #include <sys/priv.h>
37 #include <sys/proc.h>
38 #include <sys/posix4.h>
39 #include <sys/resourcevar.h>
40 #include <sys/sched.h>
41 #include <sys/sysctl.h>
42 #include <sys/smp.h>
43 #include <sys/syscallsubr.h>
44 #include <sys/sysent.h>
45 #include <sys/systm.h>
46 #include <sys/sysproto.h>
47 #include <sys/signalvar.h>
48 #include <sys/ucontext.h>
49 #include <sys/thr.h>
50 #include <sys/rtprio.h>
51 #include <sys/umtx.h>
52 #include <sys/limits.h>
53 
54 #include <machine/frame.h>
55 
56 #include <security/audit/audit.h>
57 
58 #ifdef COMPAT_IA32
59 
60 extern struct sysentvec ia32_freebsd_sysvec;
61 
62 static inline int
63 suword_lwpid(void *addr, lwpid_t lwpid)
64 {
65 	int error;
66 
67 	if (curproc->p_sysent != &ia32_freebsd_sysvec)
68 		error = suword(addr, lwpid);
69 	else
70 		error = suword32(addr, lwpid);
71 	return (error);
72 }
73 
74 #else
75 #define suword_lwpid	suword
76 #endif
77 
78 extern int max_threads_per_proc;
79 
80 static int create_thread(struct thread *td, mcontext_t *ctx,
81 			 void (*start_func)(void *), void *arg,
82 			 char *stack_base, size_t stack_size,
83 			 char *tls_base,
84 			 long *child_tid, long *parent_tid,
85 			 int flags, struct rtprio *rtp);
86 
87 /*
88  * System call interface.
89  */
90 int
91 thr_create(struct thread *td, struct thr_create_args *uap)
92     /* ucontext_t *ctx, long *id, int flags */
93 {
94 	ucontext_t ctx;
95 	int error;
96 
97 	if ((error = copyin(uap->ctx, &ctx, sizeof(ctx))))
98 		return (error);
99 
100 	error = create_thread(td, &ctx.uc_mcontext, NULL, NULL,
101 		NULL, 0, NULL, uap->id, NULL, uap->flags, NULL);
102 	return (error);
103 }
104 
105 int
106 thr_new(struct thread *td, struct thr_new_args *uap)
107     /* struct thr_param * */
108 {
109 	struct thr_param param;
110 	int error;
111 
112 	if (uap->param_size < 0 || uap->param_size > sizeof(param))
113 		return (EINVAL);
114 	bzero(&param, sizeof(param));
115 	if ((error = copyin(uap->param, &param, uap->param_size)))
116 		return (error);
117 	return (kern_thr_new(td, &param));
118 }
119 
120 int
121 kern_thr_new(struct thread *td, struct thr_param *param)
122 {
123 	struct rtprio rtp, *rtpp;
124 	int error;
125 
126 	rtpp = NULL;
127 	if (param->rtp != 0) {
128 		error = copyin(param->rtp, &rtp, sizeof(struct rtprio));
129 		rtpp = &rtp;
130 	}
131 	error = create_thread(td, NULL, param->start_func, param->arg,
132 		param->stack_base, param->stack_size, param->tls_base,
133 		param->child_tid, param->parent_tid, param->flags,
134 		rtpp);
135 	return (error);
136 }
137 
138 static int
139 create_thread(struct thread *td, mcontext_t *ctx,
140 	    void (*start_func)(void *), void *arg,
141 	    char *stack_base, size_t stack_size,
142 	    char *tls_base,
143 	    long *child_tid, long *parent_tid,
144 	    int flags, struct rtprio *rtp)
145 {
146 	stack_t stack;
147 	struct thread *newtd;
148 	struct proc *p;
149 	int error;
150 
151 	error = 0;
152 	p = td->td_proc;
153 
154 	/* Have race condition but it is cheap. */
155 	if (p->p_numthreads >= max_threads_per_proc)
156 		return (EPROCLIM);
157 
158 	if (rtp != NULL) {
159 		switch(rtp->type) {
160 		case RTP_PRIO_REALTIME:
161 		case RTP_PRIO_FIFO:
162 			/* Only root can set scheduler policy */
163 			if (priv_check(td, PRIV_SCHED_SETPOLICY) != 0)
164 				return (EPERM);
165 			if (rtp->prio > RTP_PRIO_MAX)
166 				return (EINVAL);
167 			break;
168 		case RTP_PRIO_NORMAL:
169 			rtp->prio = 0;
170 			break;
171 		default:
172 			return (EINVAL);
173 		}
174 	}
175 
176 	/* Initialize our td */
177 	newtd = thread_alloc();
178 	if (newtd == NULL)
179 		return (ENOMEM);
180 
181 	/*
182 	 * Try the copyout as soon as we allocate the td so we don't
183 	 * have to tear things down in a failure case below.
184 	 * Here we copy out tid to two places, one for child and one
185 	 * for parent, because pthread can create a detached thread,
186 	 * if parent wants to safely access child tid, it has to provide
187 	 * its storage, because child thread may exit quickly and
188 	 * memory is freed before parent thread can access it.
189 	 */
190 	if ((child_tid != NULL &&
191 	    suword_lwpid(child_tid, newtd->td_tid)) ||
192 	    (parent_tid != NULL &&
193 	    suword_lwpid(parent_tid, newtd->td_tid))) {
194 		thread_free(newtd);
195 		return (EFAULT);
196 	}
197 
198 	bzero(&newtd->td_startzero,
199 	    __rangeof(struct thread, td_startzero, td_endzero));
200 	bcopy(&td->td_startcopy, &newtd->td_startcopy,
201 	    __rangeof(struct thread, td_startcopy, td_endcopy));
202 	newtd->td_proc = td->td_proc;
203 	newtd->td_ucred = crhold(td->td_ucred);
204 
205 	cpu_set_upcall(newtd, td);
206 
207 	if (ctx != NULL) { /* old way to set user context */
208 		error = set_mcontext(newtd, ctx);
209 		if (error != 0) {
210 			thread_free(newtd);
211 			crfree(td->td_ucred);
212 			return (error);
213 		}
214 	} else {
215 		/* Set up our machine context. */
216 		stack.ss_sp = stack_base;
217 		stack.ss_size = stack_size;
218 		/* Set upcall address to user thread entry function. */
219 		cpu_set_upcall_kse(newtd, start_func, arg, &stack);
220 		/* Setup user TLS address and TLS pointer register. */
221 		error = cpu_set_user_tls(newtd, tls_base);
222 		if (error != 0) {
223 			thread_free(newtd);
224 			crfree(td->td_ucred);
225 			return (error);
226 		}
227 	}
228 
229 	PROC_LOCK(td->td_proc);
230 	td->td_proc->p_flag |= P_HADTHREADS;
231 	newtd->td_sigmask = td->td_sigmask;
232 	thread_link(newtd, p);
233 	bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
234 	thread_lock(td);
235 	/* let the scheduler know about these things. */
236 	sched_fork_thread(td, newtd);
237 	thread_unlock(td);
238 	if (P_SHOULDSTOP(p))
239 		newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
240 	PROC_UNLOCK(p);
241 	thread_lock(newtd);
242 	if (rtp != NULL) {
243 		if (!(td->td_pri_class == PRI_TIMESHARE &&
244 		      rtp->type == RTP_PRIO_NORMAL)) {
245 			rtp_to_pri(rtp, newtd);
246 			sched_prio(newtd, newtd->td_user_pri);
247 		} /* ignore timesharing class */
248 	}
249 	TD_SET_CAN_RUN(newtd);
250 	sched_add(newtd, SRQ_BORING);
251 	thread_unlock(newtd);
252 
253 	return (error);
254 }
255 
256 int
257 thr_self(struct thread *td, struct thr_self_args *uap)
258     /* long *id */
259 {
260 	int error;
261 
262 	error = suword_lwpid(uap->id, (unsigned)td->td_tid);
263 	if (error == -1)
264 		return (EFAULT);
265 	return (0);
266 }
267 
268 int
269 thr_exit(struct thread *td, struct thr_exit_args *uap)
270     /* long *state */
271 {
272 	struct proc *p;
273 
274 	p = td->td_proc;
275 
276 	/* Signal userland that it can free the stack. */
277 	if ((void *)uap->state != NULL) {
278 		suword_lwpid(uap->state, 1);
279 		kern_umtx_wake(td, uap->state, INT_MAX, 0);
280 	}
281 
282 	PROC_LOCK(p);
283 	sigqueue_flush(&td->td_sigqueue);
284 	PROC_SLOCK(p);
285 
286 	/*
287 	 * Shutting down last thread in the proc.  This will actually
288 	 * call exit() in the trampoline when it returns.
289 	 */
290 	if (p->p_numthreads != 1) {
291 		thread_stopped(p);
292 		thread_exit();
293 		/* NOTREACHED */
294 	}
295 	PROC_SUNLOCK(p);
296 	PROC_UNLOCK(p);
297 	return (0);
298 }
299 
300 int
301 thr_kill(struct thread *td, struct thr_kill_args *uap)
302     /* long id, int sig */
303 {
304 	struct thread *ttd;
305 	struct proc *p;
306 	int error;
307 
308 	p = td->td_proc;
309 	error = 0;
310 	PROC_LOCK(p);
311 	if (uap->id == -1) {
312 		if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
313 			error = EINVAL;
314 		} else {
315 			error = ESRCH;
316 			FOREACH_THREAD_IN_PROC(p, ttd) {
317 				if (ttd != td) {
318 					error = 0;
319 					if (uap->sig == 0)
320 						break;
321 					tdsignal(p, ttd, uap->sig, NULL);
322 				}
323 			}
324 		}
325 	} else {
326 		if (uap->id != td->td_tid)
327 			ttd = thread_find(p, uap->id);
328 		else
329 			ttd = td;
330 		if (ttd == NULL)
331 			error = ESRCH;
332 		else if (uap->sig == 0)
333 			;
334 		else if (!_SIG_VALID(uap->sig))
335 			error = EINVAL;
336 		else
337 			tdsignal(p, ttd, uap->sig, NULL);
338 	}
339 	PROC_UNLOCK(p);
340 	return (error);
341 }
342 
343 int
344 thr_kill2(struct thread *td, struct thr_kill2_args *uap)
345     /* pid_t pid, long id, int sig */
346 {
347 	struct thread *ttd;
348 	struct proc *p;
349 	int error;
350 
351 	AUDIT_ARG(signum, uap->sig);
352 
353 	if (uap->pid == td->td_proc->p_pid) {
354 		p = td->td_proc;
355 		PROC_LOCK(p);
356 	} else if ((p = pfind(uap->pid)) == NULL) {
357 		return (ESRCH);
358 	}
359 	AUDIT_ARG(process, p);
360 
361 	error = p_cansignal(td, p, uap->sig);
362 	if (error == 0) {
363 		if (uap->id == -1) {
364 			if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
365 				error = EINVAL;
366 			} else {
367 				error = ESRCH;
368 				FOREACH_THREAD_IN_PROC(p, ttd) {
369 					if (ttd != td) {
370 						error = 0;
371 						if (uap->sig == 0)
372 							break;
373 						tdsignal(p, ttd, uap->sig, NULL);
374 					}
375 				}
376 			}
377 		} else {
378 			if (uap->id != td->td_tid)
379 				ttd = thread_find(p, uap->id);
380 			else
381 				ttd = td;
382 			if (ttd == NULL)
383 				error = ESRCH;
384 			else if (uap->sig == 0)
385 				;
386 			else if (!_SIG_VALID(uap->sig))
387 				error = EINVAL;
388 			else
389 				tdsignal(p, ttd, uap->sig, NULL);
390 		}
391 	}
392 	PROC_UNLOCK(p);
393 	return (error);
394 }
395 
396 int
397 thr_suspend(struct thread *td, struct thr_suspend_args *uap)
398 	/* const struct timespec *timeout */
399 {
400 	struct timespec ts, *tsp;
401 	int error;
402 
403 	error = 0;
404 	tsp = NULL;
405 	if (uap->timeout != NULL) {
406 		error = copyin((const void *)uap->timeout, (void *)&ts,
407 		    sizeof(struct timespec));
408 		if (error != 0)
409 			return (error);
410 		tsp = &ts;
411 	}
412 
413 	return (kern_thr_suspend(td, tsp));
414 }
415 
416 int
417 kern_thr_suspend(struct thread *td, struct timespec *tsp)
418 {
419 	struct timeval tv;
420 	int error = 0, hz = 0;
421 
422 	if (tsp != NULL) {
423 		if (tsp->tv_nsec < 0 || tsp->tv_nsec > 1000000000)
424 			return (EINVAL);
425 		if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
426 			return (ETIMEDOUT);
427 		TIMESPEC_TO_TIMEVAL(&tv, tsp);
428 		hz = tvtohz(&tv);
429 	}
430 
431 	if (td->td_pflags & TDP_WAKEUP) {
432 		td->td_pflags &= ~TDP_WAKEUP;
433 		return (0);
434 	}
435 
436 	PROC_LOCK(td->td_proc);
437 	if ((td->td_flags & TDF_THRWAKEUP) == 0)
438 		error = msleep((void *)td, &td->td_proc->p_mtx, PCATCH, "lthr",
439 		    hz);
440 	if (td->td_flags & TDF_THRWAKEUP) {
441 		thread_lock(td);
442 		td->td_flags &= ~TDF_THRWAKEUP;
443 		thread_unlock(td);
444 		PROC_UNLOCK(td->td_proc);
445 		return (0);
446 	}
447 	PROC_UNLOCK(td->td_proc);
448 	if (error == EWOULDBLOCK)
449 		error = ETIMEDOUT;
450 	else if (error == ERESTART) {
451 		if (hz != 0)
452 			error = EINTR;
453 	}
454 	return (error);
455 }
456 
457 int
458 thr_wake(struct thread *td, struct thr_wake_args *uap)
459 	/* long id */
460 {
461 	struct proc *p;
462 	struct thread *ttd;
463 
464 	if (uap->id == td->td_tid) {
465 		td->td_pflags |= TDP_WAKEUP;
466 		return (0);
467 	}
468 
469 	p = td->td_proc;
470 	PROC_LOCK(p);
471 	ttd = thread_find(p, uap->id);
472 	if (ttd == NULL) {
473 		PROC_UNLOCK(p);
474 		return (ESRCH);
475 	}
476 	thread_lock(ttd);
477 	ttd->td_flags |= TDF_THRWAKEUP;
478 	thread_unlock(ttd);
479 	wakeup((void *)ttd);
480 	PROC_UNLOCK(p);
481 	return (0);
482 }
483 
484 int
485 thr_set_name(struct thread *td, struct thr_set_name_args *uap)
486 {
487 	struct proc *p = td->td_proc;
488 	char name[MAXCOMLEN + 1];
489 	struct thread *ttd;
490 	int error;
491 
492 	error = 0;
493 	name[0] = '\0';
494 	if (uap->name != NULL) {
495 		error = copyinstr(uap->name, name, sizeof(name),
496 			NULL);
497 		if (error)
498 			return (error);
499 	}
500 	PROC_LOCK(p);
501 	if (uap->id == td->td_tid)
502 		ttd = td;
503 	else
504 		ttd = thread_find(p, uap->id);
505 	if (ttd != NULL)
506 		strcpy(ttd->td_name, name);
507 	else
508 		error = ESRCH;
509 	PROC_UNLOCK(p);
510 	return (error);
511 }
512