27 #include <sys/cdefs.h>
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,
79 if (SV_CURPROC_FLAG(SV_LP64))
80 error =
suword(addr, lwpid);
82 error = suword32(addr, lwpid);
87 #define suword_lwpid suword
91 void (*start_func)(
void *),
void *arg,
92 char *stack_base,
size_t stack_size,
94 long *child_tid,
long *parent_tid,
95 int flags,
struct rtprio *rtp);
107 if ((error = copyin(uap->ctx, &ctx,
sizeof(ctx))))
111 NULL, 0, NULL, uap->id, NULL, uap->flags, NULL);
119 struct thr_param param;
122 if (uap->param_size < 0 || uap->param_size >
sizeof(param))
124 bzero(¶m,
sizeof(param));
125 if ((error = copyin(uap->param, ¶m, uap->param_size)))
133 struct rtprio rtp, *rtpp;
137 if (param->rtp != 0) {
138 error = copyin(param->rtp, &rtp,
sizeof(
struct rtprio));
143 error =
create_thread(td, NULL, param->start_func, param->arg,
144 param->stack_base, param->stack_size, param->tls_base,
145 param->child_tid, param->parent_tid, param->flags,
152 void (*start_func)(
void *),
void *arg,
153 char *stack_base,
size_t stack_size,
155 long *child_tid,
long *parent_tid,
156 int flags,
struct rtprio *rtp)
159 struct thread *newtd;
173 case RTP_PRIO_REALTIME:
176 if (
priv_check(td, PRIV_SCHED_SETPOLICY) != 0)
178 if (rtp->prio > RTP_PRIO_MAX)
181 case RTP_PRIO_NORMAL:
190 PROC_LOCK(td->td_proc);
192 PROC_UNLOCK(td->td_proc);
204 cpu_set_upcall(newtd, td);
215 if ((child_tid != NULL &&
217 (parent_tid != NULL &&
224 bzero(&newtd->td_startzero,
225 __rangeof(
struct thread, td_startzero, td_endzero));
226 bcopy(&td->td_startcopy, &newtd->td_startcopy,
227 __rangeof(
struct thread, td_startcopy, td_endcopy));
228 newtd->td_proc = td->td_proc;
229 newtd->td_ucred =
crhold(td->td_ucred);
230 newtd->td_dbg_sc_code = td->td_dbg_sc_code;
231 newtd->td_dbg_sc_narg = td->td_dbg_sc_narg;
234 error = set_mcontext(newtd, ctx);
242 stack.ss_sp = stack_base;
243 stack.ss_size = stack_size;
245 cpu_set_upcall_kse(newtd, start_func, arg, &stack);
247 error = cpu_set_user_tls(newtd, tls_base);
255 PROC_LOCK(td->td_proc);
256 td->td_proc->p_flag |= P_HADTHREADS;
257 newtd->td_sigmask = td->td_sigmask;
259 bcopy(p->p_comm, newtd->td_name,
sizeof(newtd->td_name));
265 newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
272 if (!(td->td_pri_class == PRI_TIMESHARE &&
273 rtp->type == RTP_PRIO_NORMAL)) {
278 TD_SET_CAN_RUN(newtd);
280 thread_unlock(newtd);
314 if ((
void *)uap->state != NULL) {
319 rw_wlock(&tidhash_lock);
327 if (p->p_numthreads != 1) {
329 LIST_REMOVE(td, td_hash);
330 rw_wunlock(&tidhash_lock);
338 rw_wunlock(&tidhash_lock);
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) {
375 ttd =
tdfind((lwpid_t)uap->id, p->p_pid);
380 else if (!_SIG_VALID(uap->sig))
384 PROC_UNLOCK(ttd->td_proc);
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);
414 if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
418 FOREACH_THREAD_IN_PROC(p, ttd) {
429 ttd =
tdfind((lwpid_t)uap->id, uap->pid);
433 AUDIT_ARG_PROCESS(p);
437 else if (!_SIG_VALID(uap->sig))
450 struct timespec ts, *tsp;
454 if (uap->timeout != NULL) {
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) {
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;
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);
void thread_link(struct thread *td, struct proc *p)
int tvtohz(struct timeval *tv)
static int max_threads_per_proc
void sched_prio(struct thread *td, u_char prio)
int racct_add(struct proc *p, int resource, uint64_t amount)
int sys_thr_suspend(struct thread *td, struct thr_suspend_args *uap)
int p_cansignal(struct thread *td, struct proc *p, int signum)
int kern_thr_suspend(struct thread *td, struct timespec *tsp)
void tdsigcleanup(struct thread *td)
void thread_stopped(struct proc *p)
void sched_fork_thread(struct thread *td, struct thread *childtd)
static int max_threads_hits
int sys_thr_kill2(struct thread *td, struct thr_kill2_args *uap)
void thread_free(struct thread *td)
int priv_check(struct thread *td, int priv)
struct proc * pfind(pid_t pid)
void sched_add(struct thread *td, int flags)
void racct_sub(struct proc *p, int resource, uint64_t amount)
struct thread * thread_alloc(int pages)
SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_per_proc, CTLFLAG_RW,&max_threads_per_proc, 0,"Limit on threads per proc")
int sys_thr_exit(struct thread *td, struct thr_exit_args *uap)
int rtp_to_pri(struct rtprio *rtp, struct thread *td)
int kern_thr_new(struct thread *td, struct thr_param *param)
static SYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW, 0,"thread allocation")
void crfree(struct ucred *cr)
int kern_umtx_wake(struct thread *td, void *uaddr, int n_wake, int is_private)
int umtx_copyin_timeout(const void *addr, struct timespec *tsp)
void tidhash_add(struct thread *td)
int sys_thr_create(struct thread *td, struct thr_create_args *uap)
int sys_thr_self(struct thread *td, struct thr_self_args *uap)
struct ucred * crhold(struct ucred *cr)
int sys_thr_kill(struct thread *td, struct thr_kill_args *uap)
int sys_thr_wake(struct thread *td, struct thr_wake_args *uap)
void tdksignal(struct thread *td, int sig, ksiginfo_t *ksi)
int sys_thr_new(struct thread *td, struct thr_new_args *uap)
struct thread * tdfind(lwpid_t tid, pid_t pid)
int sys_thr_set_name(struct thread *td, struct thr_set_name_args *uap)
static int create_thread(struct thread *td, mcontext_t *ctx, void(*start_func)(void *), void *arg, char *stack_base, size_t stack_size, char *tls_base, long *child_tid, long *parent_tid, int flags, struct rtprio *rtp)