2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2003, Jeffrey Roberson <jeff@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "opt_compat.h"
33 #include "opt_posix.h"
34 #include <sys/param.h>
35 #include <sys/kernel.h>
37 #include <sys/mutex.h>
40 #include <sys/posix4.h>
41 #include <sys/ptrace.h>
42 #include <sys/racct.h>
43 #include <sys/resourcevar.h>
44 #include <sys/rwlock.h>
45 #include <sys/sched.h>
46 #include <sys/sysctl.h>
48 #include <sys/syscallsubr.h>
49 #include <sys/sysent.h>
50 #include <sys/systm.h>
51 #include <sys/sysproto.h>
52 #include <sys/signalvar.h>
53 #include <sys/sysctl.h>
54 #include <sys/ucontext.h>
56 #include <sys/rtprio.h>
58 #include <sys/limits.h>
60 #include <machine/frame.h>
62 #include <security/audit/audit.h>
64 static SYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW, 0,
67 static int max_threads_per_proc = 1500;
68 SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_per_proc, CTLFLAG_RW,
69 &max_threads_per_proc, 0, "Limit on threads per proc");
71 static int max_threads_hits;
72 SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_hits, CTLFLAG_RD,
73 &max_threads_hits, 0, "kern.threads.max_threads_per_proc hit count");
75 #ifdef COMPAT_FREEBSD32
78 suword_lwpid(void *addr, lwpid_t lwpid)
82 if (SV_CURPROC_FLAG(SV_LP64))
83 error = suword(addr, lwpid);
85 error = suword32(addr, lwpid);
90 #define suword_lwpid suword
94 * System call interface.
97 struct thr_create_initthr_args {
103 thr_create_initthr(struct thread *td, void *thunk)
105 struct thr_create_initthr_args *args;
107 /* Copy out the child tid. */
109 if (args->tid != NULL && suword_lwpid(args->tid, td->td_tid))
112 return (set_mcontext(td, &args->ctx.uc_mcontext));
116 sys_thr_create(struct thread *td, struct thr_create_args *uap)
117 /* ucontext_t *ctx, long *id, int flags */
119 struct thr_create_initthr_args args;
122 if ((error = copyin(uap->ctx, &args.ctx, sizeof(args.ctx))))
125 return (thread_create(td, NULL, thr_create_initthr, &args));
129 sys_thr_new(struct thread *td, struct thr_new_args *uap)
130 /* struct thr_param * */
132 struct thr_param param;
135 if (uap->param_size < 0 || uap->param_size > sizeof(param))
137 bzero(¶m, sizeof(param));
138 if ((error = copyin(uap->param, ¶m, uap->param_size)))
140 return (kern_thr_new(td, ¶m));
144 thr_new_initthr(struct thread *td, void *thunk)
147 struct thr_param *param;
150 * Here we copy out tid to two places, one for child and one
151 * for parent, because pthread can create a detached thread,
152 * if parent wants to safely access child tid, it has to provide
153 * its storage, because child thread may exit quickly and
154 * memory is freed before parent thread can access it.
157 if ((param->child_tid != NULL &&
158 suword_lwpid(param->child_tid, td->td_tid)) ||
159 (param->parent_tid != NULL &&
160 suword_lwpid(param->parent_tid, td->td_tid)))
163 /* Set up our machine context. */
164 stack.ss_sp = param->stack_base;
165 stack.ss_size = param->stack_size;
166 /* Set upcall address to user thread entry function. */
167 cpu_set_upcall(td, param->start_func, param->arg, &stack);
168 /* Setup user TLS address and TLS pointer register. */
169 return (cpu_set_user_tls(td, param->tls_base));
173 kern_thr_new(struct thread *td, struct thr_param *param)
175 struct rtprio rtp, *rtpp;
179 if (param->rtp != 0) {
180 error = copyin(param->rtp, &rtp, sizeof(struct rtprio));
185 return (thread_create(td, rtpp, thr_new_initthr, param));
189 thread_create(struct thread *td, struct rtprio *rtp,
190 int (*initialize_thread)(struct thread *, void *), void *thunk)
192 struct thread *newtd;
200 case RTP_PRIO_REALTIME:
202 /* Only root can set scheduler policy */
203 if (priv_check(td, PRIV_SCHED_SETPOLICY) != 0)
205 if (rtp->prio > RTP_PRIO_MAX)
208 case RTP_PRIO_NORMAL:
219 error = racct_add(p, RACCT_NTHR, 1);
226 /* Initialize our td */
227 error = kern_thr_alloc(p, 0, &newtd);
231 cpu_copy_thread(newtd, td);
233 bzero(&newtd->td_startzero,
234 __rangeof(struct thread, td_startzero, td_endzero));
235 bcopy(&td->td_startcopy, &newtd->td_startcopy,
236 __rangeof(struct thread, td_startcopy, td_endcopy));
237 newtd->td_proc = td->td_proc;
238 newtd->td_rb_list = newtd->td_rbp_list = newtd->td_rb_inact = 0;
239 thread_cow_get(newtd, td);
241 error = initialize_thread(newtd, thunk);
243 thread_cow_free(newtd);
249 p->p_flag |= P_HADTHREADS;
250 thread_link(newtd, p);
251 bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
253 /* let the scheduler know about these things. */
254 sched_fork_thread(td, newtd);
257 newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
258 if (p->p_ptevents & PTRACE_LWP)
259 newtd->td_dbgflags |= TDB_BORN;
267 if (!(td->td_pri_class == PRI_TIMESHARE &&
268 rtp->type == RTP_PRIO_NORMAL)) {
269 rtp_to_pri(rtp, newtd);
270 sched_prio(newtd, newtd->td_user_pri);
271 } /* ignore timesharing class */
273 TD_SET_CAN_RUN(newtd);
274 sched_add(newtd, SRQ_BORING);
275 thread_unlock(newtd);
283 racct_sub(p, RACCT_NTHR, 1);
291 sys_thr_self(struct thread *td, struct thr_self_args *uap)
296 error = suword_lwpid(uap->id, (unsigned)td->td_tid);
303 sys_thr_exit(struct thread *td, struct thr_exit_args *uap)
307 umtx_thread_exit(td);
309 /* Signal userland that it can free the stack. */
310 if ((void *)uap->state != NULL) {
311 suword_lwpid(uap->state, 1);
312 kern_umtx_wake(td, uap->state, INT_MAX, 0);
315 return (kern_thr_exit(td));
319 kern_thr_exit(struct thread *td)
326 * If all of the threads in a process call this routine to
327 * exit (e.g. all threads call pthread_exit()), exactly one
328 * thread should return to the caller to terminate the process
329 * instead of the thread.
331 * Checking p_numthreads alone is not sufficient since threads
332 * might be committed to terminating while the PROC_LOCK is
333 * dropped in either ptracestop() or while removing this thread
334 * from the tidhash. Instead, the p_pendingexits field holds
335 * the count of threads in either of those states and a thread
336 * is considered the "last" thread if all of the other threads
337 * in a process are already terminating.
340 if (p->p_numthreads == p->p_pendingexits + 1) {
342 * Ignore attempts to shut down last thread in the
343 * proc. This will actually call _exit(2) in the
344 * usermode trampoline when it returns.
351 td->td_dbgflags |= TDB_EXIT;
352 if (p->p_ptevents & PTRACE_LWP)
353 ptracestop(td, SIGTRAP, NULL);
360 * The check above should prevent all other threads from this
361 * process from exiting while the PROC_LOCK is dropped, so
362 * there must be at least one other thread other than the
365 KASSERT(p->p_numthreads > 1, ("too few threads"));
366 racct_sub(p, RACCT_NTHR, 1);
375 sys_thr_kill(struct thread *td, struct thr_kill_args *uap)
376 /* long id, int sig */
385 ksi.ksi_signo = uap->sig;
386 ksi.ksi_code = SI_LWP;
387 ksi.ksi_pid = p->p_pid;
388 ksi.ksi_uid = td->td_ucred->cr_ruid;
390 if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
395 FOREACH_THREAD_IN_PROC(p, ttd) {
400 tdksignal(ttd, uap->sig, &ksi);
407 ttd = tdfind((lwpid_t)uap->id, p->p_pid);
412 else if (!_SIG_VALID(uap->sig))
415 tdksignal(ttd, uap->sig, &ksi);
416 PROC_UNLOCK(ttd->td_proc);
422 sys_thr_kill2(struct thread *td, struct thr_kill2_args *uap)
423 /* pid_t pid, long id, int sig */
430 AUDIT_ARG_SIGNUM(uap->sig);
433 ksi.ksi_signo = uap->sig;
434 ksi.ksi_code = SI_LWP;
435 ksi.ksi_pid = td->td_proc->p_pid;
436 ksi.ksi_uid = td->td_ucred->cr_ruid;
438 if ((p = pfind(uap->pid)) == NULL)
440 AUDIT_ARG_PROCESS(p);
441 error = p_cansignal(td, p, uap->sig);
446 if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
450 FOREACH_THREAD_IN_PROC(p, ttd) {
455 tdksignal(ttd, uap->sig, &ksi);
461 ttd = tdfind((lwpid_t)uap->id, uap->pid);
465 AUDIT_ARG_PROCESS(p);
466 error = p_cansignal(td, p, uap->sig);
469 else if (!_SIG_VALID(uap->sig))
472 tdksignal(ttd, uap->sig, &ksi);
479 sys_thr_suspend(struct thread *td, struct thr_suspend_args *uap)
480 /* const struct timespec *timeout */
482 struct timespec ts, *tsp;
486 if (uap->timeout != NULL) {
487 error = umtx_copyin_timeout(uap->timeout, &ts);
493 return (kern_thr_suspend(td, tsp));
497 kern_thr_suspend(struct thread *td, struct timespec *tsp)
499 struct proc *p = td->td_proc;
504 if (td->td_pflags & TDP_WAKEUP) {
505 td->td_pflags &= ~TDP_WAKEUP;
510 if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
513 TIMESPEC_TO_TIMEVAL(&tv, tsp);
519 if (error == 0 && (td->td_flags & TDF_THRWAKEUP) == 0)
520 error = msleep((void *)td, &p->p_mtx,
521 PCATCH, "lthr", timo);
523 if (td->td_flags & TDF_THRWAKEUP) {
525 td->td_flags &= ~TDF_THRWAKEUP;
531 if (error == EWOULDBLOCK)
533 else if (error == ERESTART) {
541 sys_thr_wake(struct thread *td, struct thr_wake_args *uap)
547 if (uap->id == td->td_tid) {
548 td->td_pflags |= TDP_WAKEUP;
553 ttd = tdfind((lwpid_t)uap->id, p->p_pid);
557 ttd->td_flags |= TDF_THRWAKEUP;
565 sys_thr_set_name(struct thread *td, struct thr_set_name_args *uap)
568 char name[MAXCOMLEN + 1];
574 if (uap->name != NULL) {
575 error = copyinstr(uap->name, name, sizeof(name), NULL);
576 if (error == ENAMETOOLONG) {
577 error = copyin(uap->name, name, sizeof(name) - 1);
578 name[sizeof(name) - 1] = '\0';
584 ttd = tdfind((lwpid_t)uap->id, p->p_pid);
587 strcpy(ttd->td_name, name);
589 sched_clear_tdname(ttd);
596 kern_thr_alloc(struct proc *p, int pages, struct thread **ntd)
599 /* Have race condition but it is cheap. */
600 if (p->p_numthreads >= max_threads_per_proc) {
605 *ntd = thread_alloc(pages);