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/racct.h>
40 #include <sys/resourcevar.h>
41 #include <sys/rwlock.h>
42 #include <sys/sched.h>
43 #include <sys/sysctl.h>
45 #include <sys/syscallsubr.h>
46 #include <sys/sysent.h>
47 #include <sys/systm.h>
48 #include <sys/sysproto.h>
49 #include <sys/signalvar.h>
50 #include <sys/sysctl.h>
51 #include <sys/ucontext.h>
53 #include <sys/rtprio.h>
55 #include <sys/limits.h>
57 #include <machine/frame.h>
59 #include <security/audit/audit.h>
61 static SYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW, 0,
64 static int max_threads_per_proc = 1500;
65 SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_per_proc, CTLFLAG_RW,
66 &max_threads_per_proc, 0, "Limit on threads per proc");
68 static int max_threads_hits;
69 SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_hits, CTLFLAG_RD,
70 &max_threads_hits, 0, "kern.threads.max_threads_per_proc hit count");
72 #ifdef COMPAT_FREEBSD32
75 suword_lwpid(void *addr, lwpid_t lwpid)
79 if (SV_CURPROC_FLAG(SV_LP64))
80 error = suword(addr, lwpid);
82 error = suword32(addr, lwpid);
87 #define suword_lwpid suword
91 * System call interface.
94 struct thr_create_initthr_args {
100 thr_create_initthr(struct thread *td, void *thunk)
102 struct thr_create_initthr_args *args;
104 /* Copy out the child tid. */
106 if (args->tid != NULL && suword_lwpid(args->tid, td->td_tid))
109 return (set_mcontext(td, &args->ctx.uc_mcontext));
113 sys_thr_create(struct thread *td, struct thr_create_args *uap)
114 /* ucontext_t *ctx, long *id, int flags */
116 struct thr_create_initthr_args args;
119 if ((error = copyin(uap->ctx, &args.ctx, sizeof(args.ctx))))
122 return (thread_create(td, NULL, thr_create_initthr, &args));
126 sys_thr_new(struct thread *td, struct thr_new_args *uap)
127 /* struct thr_param * */
129 struct thr_param param;
132 if (uap->param_size < 0 || uap->param_size > sizeof(param))
134 bzero(¶m, sizeof(param));
135 if ((error = copyin(uap->param, ¶m, uap->param_size)))
137 return (kern_thr_new(td, ¶m));
141 thr_new_initthr(struct thread *td, void *thunk)
144 struct thr_param *param;
147 * Here we copy out tid to two places, one for child and one
148 * for parent, because pthread can create a detached thread,
149 * if parent wants to safely access child tid, it has to provide
150 * its storage, because child thread may exit quickly and
151 * memory is freed before parent thread can access it.
154 if ((param->child_tid != NULL &&
155 suword_lwpid(param->child_tid, td->td_tid)) ||
156 (param->parent_tid != NULL &&
157 suword_lwpid(param->parent_tid, td->td_tid)))
160 /* Set up our machine context. */
161 stack.ss_sp = param->stack_base;
162 stack.ss_size = param->stack_size;
163 /* Set upcall address to user thread entry function. */
164 cpu_set_upcall_kse(td, param->start_func, param->arg, &stack);
165 /* Setup user TLS address and TLS pointer register. */
166 return (cpu_set_user_tls(td, param->tls_base));
170 kern_thr_new(struct thread *td, struct thr_param *param)
172 struct rtprio rtp, *rtpp;
176 if (param->rtp != 0) {
177 error = copyin(param->rtp, &rtp, sizeof(struct rtprio));
182 return (thread_create(td, rtpp, thr_new_initthr, param));
186 thread_create(struct thread *td, struct rtprio *rtp,
187 int (*initialize_thread)(struct thread *, void *), void *thunk)
189 struct thread *newtd;
197 case RTP_PRIO_REALTIME:
199 /* Only root can set scheduler policy */
200 if (priv_check(td, PRIV_SCHED_SETPOLICY) != 0)
202 if (rtp->prio > RTP_PRIO_MAX)
205 case RTP_PRIO_NORMAL:
214 PROC_LOCK(td->td_proc);
215 error = racct_add(p, RACCT_NTHR, 1);
216 PROC_UNLOCK(td->td_proc);
221 /* Initialize our td */
222 error = kern_thr_alloc(p, 0, &newtd);
226 cpu_set_upcall(newtd, td);
228 bzero(&newtd->td_startzero,
229 __rangeof(struct thread, td_startzero, td_endzero));
231 bcopy(&td->td_startcopy, &newtd->td_startcopy,
232 __rangeof(struct thread, td_startcopy, td_endcopy));
233 newtd->td_proc = td->td_proc;
234 newtd->td_ucred = crhold(td->td_ucred);
235 newtd->td_dbg_sc_code = td->td_dbg_sc_code;
236 newtd->td_dbg_sc_narg = td->td_dbg_sc_narg;
238 error = initialize_thread(newtd, thunk);
241 crfree(td->td_ucred);
245 PROC_LOCK(td->td_proc);
246 td->td_proc->p_flag |= P_HADTHREADS;
247 thread_link(newtd, p);
248 bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
250 /* let the scheduler know about these things. */
251 sched_fork_thread(td, newtd);
254 newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
261 if (!(td->td_pri_class == PRI_TIMESHARE &&
262 rtp->type == RTP_PRIO_NORMAL)) {
263 rtp_to_pri(rtp, newtd);
264 sched_prio(newtd, newtd->td_user_pri);
265 } /* ignore timesharing class */
267 TD_SET_CAN_RUN(newtd);
268 sched_add(newtd, SRQ_BORING);
269 thread_unlock(newtd);
277 racct_sub(p, RACCT_NTHR, 1);
285 sys_thr_self(struct thread *td, struct thr_self_args *uap)
290 error = suword_lwpid(uap->id, (unsigned)td->td_tid);
297 sys_thr_exit(struct thread *td, struct thr_exit_args *uap)
301 /* Signal userland that it can free the stack. */
302 if ((void *)uap->state != NULL) {
303 suword_lwpid(uap->state, 1);
304 kern_umtx_wake(td, uap->state, INT_MAX, 0);
307 return (kern_thr_exit(td));
311 kern_thr_exit(struct thread *td)
317 rw_wlock(&tidhash_lock);
320 if (p->p_numthreads != 1) {
321 racct_sub(p, RACCT_NTHR, 1);
322 LIST_REMOVE(td, td_hash);
323 rw_wunlock(&tidhash_lock);
325 umtx_thread_exit(td);
333 * Ignore attempts to shut down last thread in the proc. This
334 * will actually call _exit(2) in the usermode trampoline when
338 rw_wunlock(&tidhash_lock);
343 sys_thr_kill(struct thread *td, struct thr_kill_args *uap)
344 /* long id, int sig */
353 ksi.ksi_signo = uap->sig;
354 ksi.ksi_code = SI_LWP;
355 ksi.ksi_pid = p->p_pid;
356 ksi.ksi_uid = td->td_ucred->cr_ruid;
358 if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
363 FOREACH_THREAD_IN_PROC(p, ttd) {
368 tdksignal(ttd, uap->sig, &ksi);
375 ttd = tdfind((lwpid_t)uap->id, p->p_pid);
380 else if (!_SIG_VALID(uap->sig))
383 tdksignal(ttd, uap->sig, &ksi);
384 PROC_UNLOCK(ttd->td_proc);
390 sys_thr_kill2(struct thread *td, struct thr_kill2_args *uap)
391 /* pid_t pid, long id, int sig */
398 AUDIT_ARG_SIGNUM(uap->sig);
401 ksi.ksi_signo = uap->sig;
402 ksi.ksi_code = SI_LWP;
403 ksi.ksi_pid = td->td_proc->p_pid;
404 ksi.ksi_uid = td->td_ucred->cr_ruid;
406 if ((p = pfind(uap->pid)) == NULL)
408 AUDIT_ARG_PROCESS(p);
409 error = p_cansignal(td, p, uap->sig);
414 if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
418 FOREACH_THREAD_IN_PROC(p, ttd) {
423 tdksignal(ttd, uap->sig, &ksi);
429 ttd = tdfind((lwpid_t)uap->id, uap->pid);
433 AUDIT_ARG_PROCESS(p);
434 error = p_cansignal(td, p, uap->sig);
437 else if (!_SIG_VALID(uap->sig))
440 tdksignal(ttd, uap->sig, &ksi);
447 sys_thr_suspend(struct thread *td, struct thr_suspend_args *uap)
448 /* const struct timespec *timeout */
450 struct timespec ts, *tsp;
454 if (uap->timeout != NULL) {
455 error = umtx_copyin_timeout(uap->timeout, &ts);
461 return (kern_thr_suspend(td, tsp));
465 kern_thr_suspend(struct thread *td, struct timespec *tsp)
467 struct proc *p = td->td_proc;
472 if (td->td_pflags & TDP_WAKEUP) {
473 td->td_pflags &= ~TDP_WAKEUP;
478 if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
481 TIMESPEC_TO_TIMEVAL(&tv, tsp);
487 if (error == 0 && (td->td_flags & TDF_THRWAKEUP) == 0)
488 error = msleep((void *)td, &p->p_mtx,
489 PCATCH, "lthr", timo);
491 if (td->td_flags & TDF_THRWAKEUP) {
493 td->td_flags &= ~TDF_THRWAKEUP;
499 if (error == EWOULDBLOCK)
501 else if (error == ERESTART) {
509 sys_thr_wake(struct thread *td, struct thr_wake_args *uap)
515 if (uap->id == td->td_tid) {
516 td->td_pflags |= TDP_WAKEUP;
521 ttd = tdfind((lwpid_t)uap->id, p->p_pid);
525 ttd->td_flags |= TDF_THRWAKEUP;
533 sys_thr_set_name(struct thread *td, struct thr_set_name_args *uap)
536 char name[MAXCOMLEN + 1];
542 if (uap->name != NULL) {
543 error = copyinstr(uap->name, name, sizeof(name),
549 ttd = tdfind((lwpid_t)uap->id, p->p_pid);
552 strcpy(ttd->td_name, name);
554 sched_clear_tdname(ttd);
561 kern_thr_alloc(struct proc *p, int pages, struct thread **ntd)
564 /* Have race condition but it is cheap. */
565 if (p->p_numthreads >= max_threads_per_proc) {
570 *ntd = thread_alloc(pages);