33#include "opt_hwpmc_hooks.h"
35#include <sys/kernel.h>
40#include <sys/posix4.h>
41#include <sys/ptrace.h>
43#include <sys/resourcevar.h>
44#include <sys/rwlock.h>
46#include <sys/sysctl.h>
48#include <sys/syscallsubr.h>
49#include <sys/sysent.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>
57#include <sys/umtxvar.h>
58#include <sys/limits.h>
60#include <sys/pmckern.h>
63#include <machine/frame.h>
65#include <security/audit/audit.h>
67static SYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
78#ifdef COMPAT_FREEBSD32
85 if (SV_CURPROC_FLAG(SV_LP64))
86 error = suword(
addr, lwpid);
88 error = suword32(
addr, lwpid);
93#define suword_lwpid suword
115 return (set_mcontext(td, &args->
ctx.uc_mcontext));
125 if ((error = copyin(uap->ctx, &args.
ctx,
sizeof(args.
ctx))))
135 struct thr_param param;
138 if (uap->param_size < 0 || uap->param_size >
sizeof(param))
140 bzero(¶m,
sizeof(param));
141 if ((error = copyin(uap->param, ¶m, uap->param_size)))
150 struct thr_param *param;
160 if ((param->child_tid != NULL &&
162 (param->parent_tid != NULL &&
167 stack.ss_sp = param->stack_base;
168 stack.ss_size = param->stack_size;
170 cpu_set_upcall(td, param->start_func, param->arg, &stack);
172 return (cpu_set_user_tls(td, param->tls_base));
178 struct rtprio rtp, *rtpp;
182 if (param->rtp != 0) {
183 error = copyin(param->rtp, &rtp,
sizeof(
struct rtprio));
193 int (*initialize_thread)(
struct thread *,
void *),
void *thunk)
195 struct thread *newtd;
203 case RTP_PRIO_REALTIME:
206 if (
priv_check(td, PRIV_SCHED_SETPOLICY) != 0)
208 if (rtp->prio > RTP_PRIO_MAX)
211 case RTP_PRIO_NORMAL:
222 error = racct_add(p, RACCT_NTHR, 1);
234 cpu_copy_thread(newtd, td);
236 bzero(&newtd->td_startzero,
237 __rangeof(
struct thread, td_startzero, td_endzero));
238 bcopy(&td->td_startcopy, &newtd->td_startcopy,
239 __rangeof(
struct thread, td_startcopy, td_endcopy));
240 newtd->td_proc = td->td_proc;
241 newtd->td_rb_list = newtd->td_rbp_list = newtd->td_rb_inact = 0;
244 error = initialize_thread(newtd, thunk);
252 p->p_flag |= P_HADTHREADS;
254 bcopy(p->p_comm, newtd->td_name,
sizeof(newtd->td_name));
260 newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
261 if (p->p_ptevents & PTRACE_LWP)
262 newtd->td_dbgflags |= TDB_BORN;
266 if (PMC_PROC_IS_USING_PMCS(p))
267 PMC_CALL_HOOK(newtd, PMC_FN_THR_CREATE, NULL);
268 else if (PMC_SYSTEM_SAMPLING_ACTIVE())
269 PMC_CALL_HOOK_UNLOCKED(newtd, PMC_FN_THR_CREATE_LOG, NULL);
275 if (rtp != NULL && !(td->td_pri_class == PRI_TIMESHARE &&
276 rtp->type == RTP_PRIO_NORMAL))
280 TD_SET_CAN_RUN(newtd);
289 racct_sub(p, RACCT_NTHR, 1);
316 if ((
void *)uap->state != NULL) {
346 if (p->p_numthreads == p->p_pendingexits + 1) {
356 if (p->p_sysent->sv_ontdexit != NULL)
357 p->p_sysent->sv_ontdexit(td);
359 td->td_dbgflags |= TDB_EXIT;
360 if (p->p_ptevents & PTRACE_LWP) {
373 KASSERT(p->p_numthreads > 1, (
"too few threads"));
374 racct_sub(p, RACCT_NTHR, 1);
378 AUDIT_SYSCALL_EXIT(0, td);
398 ksi.ksi_signo = uap->sig;
399 ksi.ksi_code = SI_LWP;
400 ksi.ksi_pid = p->p_pid;
401 ksi.ksi_uid = td->td_ucred->cr_ruid;
403 if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
408 FOREACH_THREAD_IN_PROC(p, ttd) {
420 ttd =
tdfind((lwpid_t)uap->id, p->p_pid);
425 else if (!_SIG_VALID(uap->sig))
429 PROC_UNLOCK(ttd->td_proc);
443 AUDIT_ARG_SIGNUM(uap->sig);
446 ksi.ksi_signo = uap->sig;
447 ksi.ksi_code = SI_LWP;
448 ksi.ksi_pid = td->td_proc->p_pid;
449 ksi.ksi_uid = td->td_ucred->cr_ruid;
451 if ((p =
pfind(uap->pid)) == NULL)
453 AUDIT_ARG_PROCESS(p);
459 if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
463 FOREACH_THREAD_IN_PROC(p, ttd) {
474 ttd =
tdfind((lwpid_t)uap->id, uap->pid);
478 AUDIT_ARG_PROCESS(p);
482 else if (!_SIG_VALID(uap->sig))
495 struct timespec
ts, *tsp;
499 if (uap->timeout != NULL) {
512 struct proc *p = td->td_proc;
517 if (td->td_pflags & TDP_WAKEUP) {
518 td->td_pflags &= ~TDP_WAKEUP;
523 if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
526 TIMESPEC_TO_TIMEVAL(&tv, tsp);
532 if (error == 0 && (td->td_flags & TDF_THRWAKEUP) == 0)
533 error = msleep((
void *)td, &p->p_mtx,
534 PCATCH,
"lthr", timo);
536 if (td->td_flags & TDF_THRWAKEUP) {
538 td->td_flags &= ~TDF_THRWAKEUP;
544 if (error == EWOULDBLOCK)
546 else if (error == ERESTART) {
560 if (uap->id == td->td_tid) {
561 td->td_pflags |= TDP_WAKEUP;
566 ttd =
tdfind((lwpid_t)uap->id, p->p_pid);
570 ttd->td_flags |= TDF_THRWAKEUP;
581 char name[MAXCOMLEN + 1];
587 if (uap->name != NULL) {
588 error = copyinstr(uap->name,
name,
sizeof(
name), NULL);
589 if (error == ENAMETOOLONG) {
590 error = copyin(uap->name,
name,
sizeof(
name) - 1);
597 ttd =
tdfind((lwpid_t)uap->id, p->p_pid);
600 strcpy(ttd->td_name,
name);
602 if (PMC_PROC_IS_USING_PMCS(p) || PMC_SYSTEM_SAMPLING_ACTIVE())
603 PMC_CALL_HOOK_UNLOCKED(ttd, PMC_FN_THR_CREATE_LOG, NULL);
606 sched_clear_tdname(ttd);
int tvtohz(struct timeval *tv)
int priv_check(struct thread *td, int priv)
struct proc * pfind(pid_t pid)
int p_cansignal(struct thread *td, struct proc *p, int signum)
int rtp_to_pri(struct rtprio *rtp, struct thread *td)
void tdsigcleanup(struct thread *td)
void tdksignal(struct thread *td, int sig, ksiginfo_t *ksi)
void thread_stopped(struct proc *p)
int ptracestop(struct thread *td, int sig, ksiginfo_t *si)
void wakeup(const void *ident)
int sys_thr_wake(struct thread *td, struct thr_wake_args *uap)
int sys_thr_kill2(struct thread *td, struct thr_kill2_args *uap)
int sys_thr_exit(struct thread *td, struct thr_exit_args *uap)
int kern_thr_exit(struct thread *td)
int sys_thr_self(struct thread *td, struct thr_self_args *uap)
int sys_thr_suspend(struct thread *td, struct thr_suspend_args *uap)
int kern_thr_suspend(struct thread *td, struct timespec *tsp)
int sys_thr_kill(struct thread *td, struct thr_kill_args *uap)
static SYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW|CTLFLAG_MPSAFE, 0, "thread allocation")
SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_per_proc, CTLFLAG_RW, &max_threads_per_proc, 0, "Limit on threads per proc")
int thread_create(struct thread *td, struct rtprio *rtp, int(*initialize_thread)(struct thread *, void *), void *thunk)
int sys_thr_new(struct thread *td, struct thr_new_args *uap)
static int max_threads_hits
int kern_thr_new(struct thread *td, struct thr_param *param)
int kern_thr_alloc(struct proc *p, int pages, struct thread **ntd)
int sys_thr_set_name(struct thread *td, struct thr_set_name_args *uap)
static int thr_new_initthr(struct thread *td, void *thunk)
int sys_thr_create(struct thread *td, struct thr_create_args *uap)
static int thr_create_initthr(struct thread *td, void *thunk)
void tidhash_add(struct thread *td)
void thread_cow_get(struct thread *newtd, struct thread *td)
void tidhash_remove(struct thread *td)
void thread_link(struct thread *td, struct proc *p)
struct thread * thread_alloc(int pages)
struct thread * tdfind(lwpid_t tid, pid_t pid)
void thread_cow_free(struct thread *td)
void thread_free(struct thread *td)
int kern_umtx_wake(struct thread *td, void *uaddr, int n_wake, int is_private)
int umtx_copyin_timeout(const void *uaddr, struct timespec *tsp)
void umtx_thread_exit(struct thread *td)
void sched_fork_thread(struct thread *td, struct thread *childtd)
void sched_add(struct thread *td, int flags)