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/resourcevar.h>
40 #include <sys/sched.h>
41 #include <sys/sysctl.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/sysctl.h>
49 #include <sys/ucontext.h>
51 #include <sys/rtprio.h>
53 #include <sys/limits.h>
55 #include <machine/frame.h>
57 #include <security/audit/audit.h>
59 SYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW, 0, "thread allocation");
61 static int max_threads_per_proc = 1500;
62 SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_per_proc, CTLFLAG_RW,
63 &max_threads_per_proc, 0, "Limit on threads per proc");
65 static int max_threads_hits;
66 SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_hits, CTLFLAG_RD,
67 &max_threads_hits, 0, "");
69 #ifdef COMPAT_FREEBSD32
72 suword_lwpid(void *addr, lwpid_t lwpid)
76 if (SV_CURPROC_FLAG(SV_LP64))
77 error = suword(addr, lwpid);
79 error = suword32(addr, lwpid);
84 #define suword_lwpid suword
87 static int create_thread(struct thread *td, mcontext_t *ctx,
88 void (*start_func)(void *), void *arg,
89 char *stack_base, size_t stack_size,
91 long *child_tid, long *parent_tid,
92 int flags, struct rtprio *rtp);
95 * System call interface.
98 thr_create(struct thread *td, struct thr_create_args *uap)
99 /* ucontext_t *ctx, long *id, int flags */
104 if ((error = copyin(uap->ctx, &ctx, sizeof(ctx))))
107 error = create_thread(td, &ctx.uc_mcontext, NULL, NULL,
108 NULL, 0, NULL, uap->id, NULL, uap->flags, NULL);
113 thr_new(struct thread *td, struct thr_new_args *uap)
114 /* struct thr_param * */
116 struct thr_param param;
119 if (uap->param_size < 0 || uap->param_size > sizeof(param))
121 bzero(¶m, sizeof(param));
122 if ((error = copyin(uap->param, ¶m, uap->param_size)))
124 return (kern_thr_new(td, ¶m));
128 kern_thr_new(struct thread *td, struct thr_param *param)
130 struct rtprio rtp, *rtpp;
134 if (param->rtp != 0) {
135 error = copyin(param->rtp, &rtp, sizeof(struct rtprio));
140 error = create_thread(td, NULL, param->start_func, param->arg,
141 param->stack_base, param->stack_size, param->tls_base,
142 param->child_tid, param->parent_tid, param->flags,
148 create_thread(struct thread *td, mcontext_t *ctx,
149 void (*start_func)(void *), void *arg,
150 char *stack_base, size_t stack_size,
152 long *child_tid, long *parent_tid,
153 int flags, struct rtprio *rtp)
156 struct thread *newtd;
162 /* Have race condition but it is cheap. */
163 if (p->p_numthreads >= max_threads_per_proc) {
170 case RTP_PRIO_REALTIME:
172 /* Only root can set scheduler policy */
173 if (priv_check(td, PRIV_SCHED_SETPOLICY) != 0)
175 if (rtp->prio > RTP_PRIO_MAX)
178 case RTP_PRIO_NORMAL:
186 /* Initialize our td */
187 newtd = thread_alloc(0);
191 cpu_set_upcall(newtd, td);
194 * Try the copyout as soon as we allocate the td so we don't
195 * have to tear things down in a failure case below.
196 * Here we copy out tid to two places, one for child and one
197 * for parent, because pthread can create a detached thread,
198 * if parent wants to safely access child tid, it has to provide
199 * its storage, because child thread may exit quickly and
200 * memory is freed before parent thread can access it.
202 if ((child_tid != NULL &&
203 suword_lwpid(child_tid, newtd->td_tid)) ||
204 (parent_tid != NULL &&
205 suword_lwpid(parent_tid, newtd->td_tid))) {
210 bzero(&newtd->td_startzero,
211 __rangeof(struct thread, td_startzero, td_endzero));
212 bzero(&newtd->td_rux, sizeof(newtd->td_rux));
213 newtd->td_map_def_user = NULL;
214 newtd->td_dbg_forked = 0;
215 bcopy(&td->td_startcopy, &newtd->td_startcopy,
216 __rangeof(struct thread, td_startcopy, td_endcopy));
217 newtd->td_proc = td->td_proc;
218 newtd->td_ucred = crhold(td->td_ucred);
220 if (ctx != NULL) { /* old way to set user context */
221 error = set_mcontext(newtd, ctx);
224 crfree(td->td_ucred);
228 /* Set up our machine context. */
229 stack.ss_sp = stack_base;
230 stack.ss_size = stack_size;
231 /* Set upcall address to user thread entry function. */
232 cpu_set_upcall_kse(newtd, start_func, arg, &stack);
233 /* Setup user TLS address and TLS pointer register. */
234 error = cpu_set_user_tls(newtd, tls_base);
237 crfree(td->td_ucred);
242 PROC_LOCK(td->td_proc);
243 td->td_proc->p_flag |= P_HADTHREADS;
244 newtd->td_sigmask = td->td_sigmask;
245 thread_link(newtd, p);
246 bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
248 /* let the scheduler know about these things. */
249 sched_fork_thread(td, newtd);
252 newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
256 if (!(td->td_pri_class == PRI_TIMESHARE &&
257 rtp->type == RTP_PRIO_NORMAL)) {
258 rtp_to_pri(rtp, newtd);
259 sched_prio(newtd, newtd->td_user_pri);
260 } /* ignore timesharing class */
262 TD_SET_CAN_RUN(newtd);
263 sched_add(newtd, SRQ_BORING);
264 thread_unlock(newtd);
270 thr_self(struct thread *td, struct thr_self_args *uap)
275 error = suword_lwpid(uap->id, (unsigned)td->td_tid);
282 thr_exit(struct thread *td, struct thr_exit_args *uap)
289 /* Signal userland that it can free the stack. */
290 if ((void *)uap->state != NULL) {
291 suword_lwpid(uap->state, 1);
292 kern_umtx_wake(td, uap->state, INT_MAX, 0);
300 * Shutting down last thread in the proc. This will actually
301 * call exit() in the trampoline when it returns.
303 if (p->p_numthreads != 1) {
314 thr_kill(struct thread *td, struct thr_kill_args *uap)
315 /* long id, int sig */
325 ksi.ksi_signo = uap->sig;
326 ksi.ksi_code = SI_USER;
327 ksi.ksi_pid = p->p_pid;
328 ksi.ksi_uid = td->td_ucred->cr_ruid;
331 if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
335 FOREACH_THREAD_IN_PROC(p, ttd) {
340 tdsignal(p, ttd, uap->sig, &ksi);
345 if (uap->id != td->td_tid)
346 ttd = thread_find(p, uap->id);
351 else if (uap->sig == 0)
353 else if (!_SIG_VALID(uap->sig))
356 tdsignal(p, ttd, uap->sig, &ksi);
363 thr_kill2(struct thread *td, struct thr_kill2_args *uap)
364 /* pid_t pid, long id, int sig */
371 AUDIT_ARG_SIGNUM(uap->sig);
373 if (uap->pid == td->td_proc->p_pid) {
376 } else if ((p = pfind(uap->pid)) == NULL) {
379 AUDIT_ARG_PROCESS(p);
381 error = p_cansignal(td, p, uap->sig);
384 ksi.ksi_signo = uap->sig;
385 ksi.ksi_code = SI_USER;
386 ksi.ksi_pid = td->td_proc->p_pid;
387 ksi.ksi_uid = td->td_ucred->cr_ruid;
389 if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
393 FOREACH_THREAD_IN_PROC(p, ttd) {
398 tdsignal(p, ttd, uap->sig,
404 if (uap->id != td->td_tid)
405 ttd = thread_find(p, uap->id);
410 else if (uap->sig == 0)
412 else if (!_SIG_VALID(uap->sig))
415 tdsignal(p, ttd, uap->sig, &ksi);
423 thr_suspend(struct thread *td, struct thr_suspend_args *uap)
424 /* const struct timespec *timeout */
426 struct timespec ts, *tsp;
430 if (uap->timeout != NULL) {
431 error = umtx_copyin_timeout(uap->timeout, &ts);
437 return (kern_thr_suspend(td, tsp));
441 kern_thr_suspend(struct thread *td, struct timespec *tsp)
444 int error = 0, hz = 0;
447 if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
449 TIMESPEC_TO_TIMEVAL(&tv, tsp);
453 if (td->td_pflags & TDP_WAKEUP) {
454 td->td_pflags &= ~TDP_WAKEUP;
458 PROC_LOCK(td->td_proc);
459 if ((td->td_flags & TDF_THRWAKEUP) == 0)
460 error = msleep((void *)td, &td->td_proc->p_mtx, PCATCH, "lthr",
462 if (td->td_flags & TDF_THRWAKEUP) {
464 td->td_flags &= ~TDF_THRWAKEUP;
466 PROC_UNLOCK(td->td_proc);
469 PROC_UNLOCK(td->td_proc);
470 if (error == EWOULDBLOCK)
472 else if (error == ERESTART) {
480 thr_wake(struct thread *td, struct thr_wake_args *uap)
486 if (uap->id == td->td_tid) {
487 td->td_pflags |= TDP_WAKEUP;
493 ttd = thread_find(p, uap->id);
499 ttd->td_flags |= TDF_THRWAKEUP;
507 thr_set_name(struct thread *td, struct thr_set_name_args *uap)
509 struct proc *p = td->td_proc;
510 char name[MAXCOMLEN + 1];
516 if (uap->name != NULL) {
517 error = copyinstr(uap->name, name, sizeof(name),
523 if (uap->id == td->td_tid)
526 ttd = thread_find(p, uap->id);
528 strcpy(ttd->td_name, name);
532 sched_clear_tdname(ttd);