2 * Copyright (c) 2003, Jeffrey Roberson <jeff@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include "opt_compat.h"
31 #include "opt_posix.h"
32 #include <sys/param.h>
33 #include <sys/kernel.h>
35 #include <sys/mutex.h>
38 #include <sys/posix4.h>
39 #include <sys/ptrace.h>
40 #include <sys/racct.h>
41 #include <sys/resourcevar.h>
42 #include <sys/rwlock.h>
43 #include <sys/sched.h>
44 #include <sys/sysctl.h>
46 #include <sys/syscallsubr.h>
47 #include <sys/sysent.h>
48 #include <sys/systm.h>
49 #include <sys/sysproto.h>
50 #include <sys/signalvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/ucontext.h>
54 #include <sys/rtprio.h>
56 #include <sys/limits.h>
58 #include <machine/frame.h>
60 #include <security/audit/audit.h>
62 static SYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW, 0,
65 static int max_threads_per_proc = 1500;
66 SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_per_proc, CTLFLAG_RW,
67 &max_threads_per_proc, 0, "Limit on threads per proc");
69 static int max_threads_hits;
70 SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_hits, CTLFLAG_RD,
71 &max_threads_hits, 0, "kern.threads.max_threads_per_proc hit count");
73 #ifdef COMPAT_FREEBSD32
76 suword_lwpid(void *addr, lwpid_t lwpid)
80 if (SV_CURPROC_FLAG(SV_LP64))
81 error = suword(addr, lwpid);
83 error = suword32(addr, lwpid);
88 #define suword_lwpid suword
92 * System call interface.
95 struct thr_create_initthr_args {
101 thr_create_initthr(struct thread *td, void *thunk)
103 struct thr_create_initthr_args *args;
105 /* Copy out the child tid. */
107 if (args->tid != NULL && suword_lwpid(args->tid, td->td_tid))
110 return (set_mcontext(td, &args->ctx.uc_mcontext));
114 sys_thr_create(struct thread *td, struct thr_create_args *uap)
115 /* ucontext_t *ctx, long *id, int flags */
117 struct thr_create_initthr_args args;
120 if ((error = copyin(uap->ctx, &args.ctx, sizeof(args.ctx))))
123 return (thread_create(td, NULL, thr_create_initthr, &args));
127 sys_thr_new(struct thread *td, struct thr_new_args *uap)
128 /* struct thr_param * */
130 struct thr_param param;
133 if (uap->param_size < 0 || uap->param_size > sizeof(param))
135 bzero(¶m, sizeof(param));
136 if ((error = copyin(uap->param, ¶m, uap->param_size)))
138 return (kern_thr_new(td, ¶m));
142 thr_new_initthr(struct thread *td, void *thunk)
145 struct thr_param *param;
148 * Here we copy out tid to two places, one for child and one
149 * for parent, because pthread can create a detached thread,
150 * if parent wants to safely access child tid, it has to provide
151 * its storage, because child thread may exit quickly and
152 * memory is freed before parent thread can access it.
155 if ((param->child_tid != NULL &&
156 suword_lwpid(param->child_tid, td->td_tid)) ||
157 (param->parent_tid != NULL &&
158 suword_lwpid(param->parent_tid, td->td_tid)))
161 /* Set up our machine context. */
162 stack.ss_sp = param->stack_base;
163 stack.ss_size = param->stack_size;
164 /* Set upcall address to user thread entry function. */
165 cpu_set_upcall_kse(td, param->start_func, param->arg, &stack);
166 /* Setup user TLS address and TLS pointer register. */
167 return (cpu_set_user_tls(td, param->tls_base));
171 kern_thr_new(struct thread *td, struct thr_param *param)
173 struct rtprio rtp, *rtpp;
177 if (param->rtp != 0) {
178 error = copyin(param->rtp, &rtp, sizeof(struct rtprio));
183 return (thread_create(td, rtpp, thr_new_initthr, param));
187 thread_create(struct thread *td, struct rtprio *rtp,
188 int (*initialize_thread)(struct thread *, void *), void *thunk)
190 struct thread *newtd;
198 case RTP_PRIO_REALTIME:
200 /* Only root can set scheduler policy */
201 if (priv_check(td, PRIV_SCHED_SETPOLICY) != 0)
203 if (rtp->prio > RTP_PRIO_MAX)
206 case RTP_PRIO_NORMAL:
215 PROC_LOCK(td->td_proc);
216 error = racct_add(p, RACCT_NTHR, 1);
217 PROC_UNLOCK(td->td_proc);
222 /* Initialize our td */
223 error = kern_thr_alloc(p, 0, &newtd);
227 cpu_set_upcall(newtd, td);
229 bzero(&newtd->td_startzero,
230 __rangeof(struct thread, td_startzero, td_endzero));
232 bcopy(&td->td_startcopy, &newtd->td_startcopy,
233 __rangeof(struct thread, td_startcopy, td_endcopy));
234 newtd->td_proc = td->td_proc;
235 newtd->td_ucred = crhold(td->td_ucred);
236 newtd->td_dbg_sc_code = td->td_dbg_sc_code;
237 newtd->td_dbg_sc_narg = td->td_dbg_sc_narg;
239 error = initialize_thread(newtd, thunk);
242 crfree(td->td_ucred);
246 PROC_LOCK(td->td_proc);
247 td->td_proc->p_flag |= P_HADTHREADS;
248 thread_link(newtd, p);
249 bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
251 /* let the scheduler know about these things. */
252 sched_fork_thread(td, newtd);
255 newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
256 if (p->p_ptevents & PTRACE_LWP)
257 newtd->td_dbgflags |= TDB_BORN;
264 if (!(td->td_pri_class == PRI_TIMESHARE &&
265 rtp->type == RTP_PRIO_NORMAL)) {
266 rtp_to_pri(rtp, newtd);
267 sched_prio(newtd, newtd->td_user_pri);
268 } /* ignore timesharing class */
270 TD_SET_CAN_RUN(newtd);
271 sched_add(newtd, SRQ_BORING);
272 thread_unlock(newtd);
280 racct_sub(p, RACCT_NTHR, 1);
288 sys_thr_self(struct thread *td, struct thr_self_args *uap)
293 error = suword_lwpid(uap->id, (unsigned)td->td_tid);
300 sys_thr_exit(struct thread *td, struct thr_exit_args *uap)
304 /* Signal userland that it can free the stack. */
305 if ((void *)uap->state != NULL) {
306 suword_lwpid(uap->state, 1);
307 kern_umtx_wake(td, uap->state, INT_MAX, 0);
310 return (kern_thr_exit(td));
314 kern_thr_exit(struct thread *td)
321 * If all of the threads in a process call this routine to
322 * exit (e.g. all threads call pthread_exit()), exactly one
323 * thread should return to the caller to terminate the process
324 * instead of the thread.
326 * Checking p_numthreads alone is not sufficient since threads
327 * might be committed to terminating while the PROC_LOCK is
328 * dropped in either ptracestop() or while removing this thread
329 * from the tidhash. Instead, the p_pendingexits field holds
330 * the count of threads in either of those states and a thread
331 * is considered the "last" thread if all of the other threads
332 * in a process are already terminating.
335 if (p->p_numthreads == p->p_pendingexits + 1) {
337 * Ignore attempts to shut down last thread in the
338 * proc. This will actually call _exit(2) in the
339 * usermode trampoline when it returns.
346 td->td_dbgflags |= TDB_EXIT;
347 if (p->p_ptevents & PTRACE_LWP)
348 ptracestop(td, SIGTRAP);
355 * The check above should prevent all other threads from this
356 * process from exiting while the PROC_LOCK is dropped, so
357 * there must be at least one other thread other than the
360 KASSERT(p->p_numthreads > 1, ("too few threads"));
361 racct_sub(p, RACCT_NTHR, 1);
363 umtx_thread_exit(td);
371 sys_thr_kill(struct thread *td, struct thr_kill_args *uap)
372 /* long id, int sig */
381 ksi.ksi_signo = uap->sig;
382 ksi.ksi_code = SI_LWP;
383 ksi.ksi_pid = p->p_pid;
384 ksi.ksi_uid = td->td_ucred->cr_ruid;
386 if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
391 FOREACH_THREAD_IN_PROC(p, ttd) {
396 tdksignal(ttd, uap->sig, &ksi);
403 ttd = tdfind((lwpid_t)uap->id, p->p_pid);
408 else if (!_SIG_VALID(uap->sig))
411 tdksignal(ttd, uap->sig, &ksi);
412 PROC_UNLOCK(ttd->td_proc);
418 sys_thr_kill2(struct thread *td, struct thr_kill2_args *uap)
419 /* pid_t pid, long id, int sig */
426 AUDIT_ARG_SIGNUM(uap->sig);
429 ksi.ksi_signo = uap->sig;
430 ksi.ksi_code = SI_LWP;
431 ksi.ksi_pid = td->td_proc->p_pid;
432 ksi.ksi_uid = td->td_ucred->cr_ruid;
434 if ((p = pfind(uap->pid)) == NULL)
436 AUDIT_ARG_PROCESS(p);
437 error = p_cansignal(td, p, uap->sig);
442 if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
446 FOREACH_THREAD_IN_PROC(p, ttd) {
451 tdksignal(ttd, uap->sig, &ksi);
457 ttd = tdfind((lwpid_t)uap->id, uap->pid);
461 AUDIT_ARG_PROCESS(p);
462 error = p_cansignal(td, p, uap->sig);
465 else if (!_SIG_VALID(uap->sig))
468 tdksignal(ttd, uap->sig, &ksi);
475 sys_thr_suspend(struct thread *td, struct thr_suspend_args *uap)
476 /* const struct timespec *timeout */
478 struct timespec ts, *tsp;
482 if (uap->timeout != NULL) {
483 error = umtx_copyin_timeout(uap->timeout, &ts);
489 return (kern_thr_suspend(td, tsp));
493 kern_thr_suspend(struct thread *td, struct timespec *tsp)
495 struct proc *p = td->td_proc;
500 if (td->td_pflags & TDP_WAKEUP) {
501 td->td_pflags &= ~TDP_WAKEUP;
506 if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
509 TIMESPEC_TO_TIMEVAL(&tv, tsp);
515 if (error == 0 && (td->td_flags & TDF_THRWAKEUP) == 0)
516 error = msleep((void *)td, &p->p_mtx,
517 PCATCH, "lthr", timo);
519 if (td->td_flags & TDF_THRWAKEUP) {
521 td->td_flags &= ~TDF_THRWAKEUP;
527 if (error == EWOULDBLOCK)
529 else if (error == ERESTART) {
537 sys_thr_wake(struct thread *td, struct thr_wake_args *uap)
543 if (uap->id == td->td_tid) {
544 td->td_pflags |= TDP_WAKEUP;
549 ttd = tdfind((lwpid_t)uap->id, p->p_pid);
553 ttd->td_flags |= TDF_THRWAKEUP;
561 sys_thr_set_name(struct thread *td, struct thr_set_name_args *uap)
564 char name[MAXCOMLEN + 1];
570 if (uap->name != NULL) {
571 error = copyinstr(uap->name, name, sizeof(name),
577 ttd = tdfind((lwpid_t)uap->id, p->p_pid);
580 strcpy(ttd->td_name, name);
582 sched_clear_tdname(ttd);
589 kern_thr_alloc(struct proc *p, int pages, struct thread **ntd)
592 /* Have race condition but it is cheap. */
593 if (p->p_numthreads >= max_threads_per_proc) {
598 *ntd = thread_alloc(pages);