xref: /freebsd/sys/kern/kern_thr.c (revision a9148abd9da5db2f1c682fb17bed791845fc41c9)
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 extern int max_threads_hits;
80 
81 static int create_thread(struct thread *td, mcontext_t *ctx,
82 			 void (*start_func)(void *), void *arg,
83 			 char *stack_base, size_t stack_size,
84 			 char *tls_base,
85 			 long *child_tid, long *parent_tid,
86 			 int flags, struct rtprio *rtp);
87 
88 /*
89  * System call interface.
90  */
91 int
92 thr_create(struct thread *td, struct thr_create_args *uap)
93     /* ucontext_t *ctx, long *id, int flags */
94 {
95 	ucontext_t ctx;
96 	int error;
97 
98 	if ((error = copyin(uap->ctx, &ctx, sizeof(ctx))))
99 		return (error);
100 
101 	error = create_thread(td, &ctx.uc_mcontext, NULL, NULL,
102 		NULL, 0, NULL, uap->id, NULL, uap->flags, NULL);
103 	return (error);
104 }
105 
106 int
107 thr_new(struct thread *td, struct thr_new_args *uap)
108     /* struct thr_param * */
109 {
110 	struct thr_param param;
111 	int error;
112 
113 	if (uap->param_size < 0 || uap->param_size > sizeof(param))
114 		return (EINVAL);
115 	bzero(&param, sizeof(param));
116 	if ((error = copyin(uap->param, &param, uap->param_size)))
117 		return (error);
118 	return (kern_thr_new(td, &param));
119 }
120 
121 int
122 kern_thr_new(struct thread *td, struct thr_param *param)
123 {
124 	struct rtprio rtp, *rtpp;
125 	int error;
126 
127 	rtpp = NULL;
128 	if (param->rtp != 0) {
129 		error = copyin(param->rtp, &rtp, sizeof(struct rtprio));
130 		if (error)
131 			return (error);
132 		rtpp = &rtp;
133 	}
134 	error = create_thread(td, NULL, param->start_func, param->arg,
135 		param->stack_base, param->stack_size, param->tls_base,
136 		param->child_tid, param->parent_tid, param->flags,
137 		rtpp);
138 	return (error);
139 }
140 
141 static int
142 create_thread(struct thread *td, mcontext_t *ctx,
143 	    void (*start_func)(void *), void *arg,
144 	    char *stack_base, size_t stack_size,
145 	    char *tls_base,
146 	    long *child_tid, long *parent_tid,
147 	    int flags, struct rtprio *rtp)
148 {
149 	stack_t stack;
150 	struct thread *newtd;
151 	struct proc *p;
152 	int error;
153 
154 	error = 0;
155 	p = td->td_proc;
156 
157 	/* Have race condition but it is cheap. */
158 	if (p->p_numthreads >= max_threads_per_proc) {
159 		++max_threads_hits;
160 		return (EPROCLIM);
161 	}
162 
163 	if (rtp != NULL) {
164 		switch(rtp->type) {
165 		case RTP_PRIO_REALTIME:
166 		case RTP_PRIO_FIFO:
167 			/* Only root can set scheduler policy */
168 			if (priv_check(td, PRIV_SCHED_SETPOLICY) != 0)
169 				return (EPERM);
170 			if (rtp->prio > RTP_PRIO_MAX)
171 				return (EINVAL);
172 			break;
173 		case RTP_PRIO_NORMAL:
174 			rtp->prio = 0;
175 			break;
176 		default:
177 			return (EINVAL);
178 		}
179 	}
180 
181 	/* Initialize our td */
182 	newtd = thread_alloc();
183 	if (newtd == NULL)
184 		return (ENOMEM);
185 
186 	/*
187 	 * Try the copyout as soon as we allocate the td so we don't
188 	 * have to tear things down in a failure case below.
189 	 * Here we copy out tid to two places, one for child and one
190 	 * for parent, because pthread can create a detached thread,
191 	 * if parent wants to safely access child tid, it has to provide
192 	 * its storage, because child thread may exit quickly and
193 	 * memory is freed before parent thread can access it.
194 	 */
195 	if ((child_tid != NULL &&
196 	    suword_lwpid(child_tid, newtd->td_tid)) ||
197 	    (parent_tid != NULL &&
198 	    suword_lwpid(parent_tid, newtd->td_tid))) {
199 		thread_free(newtd);
200 		return (EFAULT);
201 	}
202 
203 	bzero(&newtd->td_startzero,
204 	    __rangeof(struct thread, td_startzero, td_endzero));
205 	bcopy(&td->td_startcopy, &newtd->td_startcopy,
206 	    __rangeof(struct thread, td_startcopy, td_endcopy));
207 	newtd->td_proc = td->td_proc;
208 	newtd->td_ucred = crhold(td->td_ucred);
209 
210 	cpu_set_upcall(newtd, td);
211 
212 	if (ctx != NULL) { /* old way to set user context */
213 		error = set_mcontext(newtd, ctx);
214 		if (error != 0) {
215 			thread_free(newtd);
216 			crfree(td->td_ucred);
217 			return (error);
218 		}
219 	} else {
220 		/* Set up our machine context. */
221 		stack.ss_sp = stack_base;
222 		stack.ss_size = stack_size;
223 		/* Set upcall address to user thread entry function. */
224 		cpu_set_upcall_kse(newtd, start_func, arg, &stack);
225 		/* Setup user TLS address and TLS pointer register. */
226 		error = cpu_set_user_tls(newtd, tls_base);
227 		if (error != 0) {
228 			thread_free(newtd);
229 			crfree(td->td_ucred);
230 			return (error);
231 		}
232 	}
233 
234 	PROC_LOCK(td->td_proc);
235 	td->td_proc->p_flag |= P_HADTHREADS;
236 	newtd->td_sigmask = td->td_sigmask;
237 	thread_link(newtd, p);
238 	bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
239 	thread_lock(td);
240 	/* let the scheduler know about these things. */
241 	sched_fork_thread(td, newtd);
242 	thread_unlock(td);
243 	if (P_SHOULDSTOP(p))
244 		newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
245 	PROC_UNLOCK(p);
246 	thread_lock(newtd);
247 	if (rtp != NULL) {
248 		if (!(td->td_pri_class == PRI_TIMESHARE &&
249 		      rtp->type == RTP_PRIO_NORMAL)) {
250 			rtp_to_pri(rtp, newtd);
251 			sched_prio(newtd, newtd->td_user_pri);
252 		} /* ignore timesharing class */
253 	}
254 	TD_SET_CAN_RUN(newtd);
255 	sched_add(newtd, SRQ_BORING);
256 	thread_unlock(newtd);
257 
258 	return (error);
259 }
260 
261 int
262 thr_self(struct thread *td, struct thr_self_args *uap)
263     /* long *id */
264 {
265 	int error;
266 
267 	error = suword_lwpid(uap->id, (unsigned)td->td_tid);
268 	if (error == -1)
269 		return (EFAULT);
270 	return (0);
271 }
272 
273 int
274 thr_exit(struct thread *td, struct thr_exit_args *uap)
275     /* long *state */
276 {
277 	struct proc *p;
278 
279 	p = td->td_proc;
280 
281 	/* Signal userland that it can free the stack. */
282 	if ((void *)uap->state != NULL) {
283 		suword_lwpid(uap->state, 1);
284 		kern_umtx_wake(td, uap->state, INT_MAX, 0);
285 	}
286 
287 	PROC_LOCK(p);
288 	sigqueue_flush(&td->td_sigqueue);
289 
290 	/*
291 	 * Shutting down last thread in the proc.  This will actually
292 	 * call exit() in the trampoline when it returns.
293 	 */
294 	if (p->p_numthreads != 1) {
295 		thread_stopped(p);
296 		PROC_SLOCK(p);
297 		thread_exit();
298 		/* NOTREACHED */
299 	}
300 	PROC_UNLOCK(p);
301 	return (0);
302 }
303 
304 int
305 thr_kill(struct thread *td, struct thr_kill_args *uap)
306     /* long id, int sig */
307 {
308 	struct thread *ttd;
309 	struct proc *p;
310 	int error;
311 
312 	p = td->td_proc;
313 	error = 0;
314 	PROC_LOCK(p);
315 	if (uap->id == -1) {
316 		if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
317 			error = EINVAL;
318 		} else {
319 			error = ESRCH;
320 			FOREACH_THREAD_IN_PROC(p, ttd) {
321 				if (ttd != td) {
322 					error = 0;
323 					if (uap->sig == 0)
324 						break;
325 					tdsignal(p, ttd, uap->sig, NULL);
326 				}
327 			}
328 		}
329 	} else {
330 		if (uap->id != td->td_tid)
331 			ttd = thread_find(p, uap->id);
332 		else
333 			ttd = td;
334 		if (ttd == NULL)
335 			error = ESRCH;
336 		else if (uap->sig == 0)
337 			;
338 		else if (!_SIG_VALID(uap->sig))
339 			error = EINVAL;
340 		else
341 			tdsignal(p, ttd, uap->sig, NULL);
342 	}
343 	PROC_UNLOCK(p);
344 	return (error);
345 }
346 
347 int
348 thr_kill2(struct thread *td, struct thr_kill2_args *uap)
349     /* pid_t pid, long id, int sig */
350 {
351 	struct thread *ttd;
352 	struct proc *p;
353 	int error;
354 
355 	AUDIT_ARG(signum, uap->sig);
356 
357 	if (uap->pid == td->td_proc->p_pid) {
358 		p = td->td_proc;
359 		PROC_LOCK(p);
360 	} else if ((p = pfind(uap->pid)) == NULL) {
361 		return (ESRCH);
362 	}
363 	AUDIT_ARG(process, p);
364 
365 	error = p_cansignal(td, p, uap->sig);
366 	if (error == 0) {
367 		if (uap->id == -1) {
368 			if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
369 				error = EINVAL;
370 			} else {
371 				error = ESRCH;
372 				FOREACH_THREAD_IN_PROC(p, ttd) {
373 					if (ttd != td) {
374 						error = 0;
375 						if (uap->sig == 0)
376 							break;
377 						tdsignal(p, ttd, uap->sig, NULL);
378 					}
379 				}
380 			}
381 		} else {
382 			if (uap->id != td->td_tid)
383 				ttd = thread_find(p, uap->id);
384 			else
385 				ttd = td;
386 			if (ttd == NULL)
387 				error = ESRCH;
388 			else if (uap->sig == 0)
389 				;
390 			else if (!_SIG_VALID(uap->sig))
391 				error = EINVAL;
392 			else
393 				tdsignal(p, ttd, uap->sig, NULL);
394 		}
395 	}
396 	PROC_UNLOCK(p);
397 	return (error);
398 }
399 
400 int
401 thr_suspend(struct thread *td, struct thr_suspend_args *uap)
402 	/* const struct timespec *timeout */
403 {
404 	struct timespec ts, *tsp;
405 	int error;
406 
407 	error = 0;
408 	tsp = NULL;
409 	if (uap->timeout != NULL) {
410 		error = copyin((const void *)uap->timeout, (void *)&ts,
411 		    sizeof(struct timespec));
412 		if (error != 0)
413 			return (error);
414 		tsp = &ts;
415 	}
416 
417 	return (kern_thr_suspend(td, tsp));
418 }
419 
420 int
421 kern_thr_suspend(struct thread *td, struct timespec *tsp)
422 {
423 	struct timeval tv;
424 	int error = 0, hz = 0;
425 
426 	if (tsp != NULL) {
427 		if (tsp->tv_nsec < 0 || tsp->tv_nsec > 1000000000)
428 			return (EINVAL);
429 		if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
430 			return (ETIMEDOUT);
431 		TIMESPEC_TO_TIMEVAL(&tv, tsp);
432 		hz = tvtohz(&tv);
433 	}
434 
435 	if (td->td_pflags & TDP_WAKEUP) {
436 		td->td_pflags &= ~TDP_WAKEUP;
437 		return (0);
438 	}
439 
440 	PROC_LOCK(td->td_proc);
441 	if ((td->td_flags & TDF_THRWAKEUP) == 0)
442 		error = msleep((void *)td, &td->td_proc->p_mtx, PCATCH, "lthr",
443 		    hz);
444 	if (td->td_flags & TDF_THRWAKEUP) {
445 		thread_lock(td);
446 		td->td_flags &= ~TDF_THRWAKEUP;
447 		thread_unlock(td);
448 		PROC_UNLOCK(td->td_proc);
449 		return (0);
450 	}
451 	PROC_UNLOCK(td->td_proc);
452 	if (error == EWOULDBLOCK)
453 		error = ETIMEDOUT;
454 	else if (error == ERESTART) {
455 		if (hz != 0)
456 			error = EINTR;
457 	}
458 	return (error);
459 }
460 
461 int
462 thr_wake(struct thread *td, struct thr_wake_args *uap)
463 	/* long id */
464 {
465 	struct proc *p;
466 	struct thread *ttd;
467 
468 	if (uap->id == td->td_tid) {
469 		td->td_pflags |= TDP_WAKEUP;
470 		return (0);
471 	}
472 
473 	p = td->td_proc;
474 	PROC_LOCK(p);
475 	ttd = thread_find(p, uap->id);
476 	if (ttd == NULL) {
477 		PROC_UNLOCK(p);
478 		return (ESRCH);
479 	}
480 	thread_lock(ttd);
481 	ttd->td_flags |= TDF_THRWAKEUP;
482 	thread_unlock(ttd);
483 	wakeup((void *)ttd);
484 	PROC_UNLOCK(p);
485 	return (0);
486 }
487 
488 int
489 thr_set_name(struct thread *td, struct thr_set_name_args *uap)
490 {
491 	struct proc *p = td->td_proc;
492 	char name[MAXCOMLEN + 1];
493 	struct thread *ttd;
494 	int error;
495 
496 	error = 0;
497 	name[0] = '\0';
498 	if (uap->name != NULL) {
499 		error = copyinstr(uap->name, name, sizeof(name),
500 			NULL);
501 		if (error)
502 			return (error);
503 	}
504 	PROC_LOCK(p);
505 	if (uap->id == td->td_tid)
506 		ttd = td;
507 	else
508 		ttd = thread_find(p, uap->id);
509 	if (ttd != NULL)
510 		strcpy(ttd->td_name, name);
511 	else
512 		error = ESRCH;
513 	PROC_UNLOCK(p);
514 	return (error);
515 }
516