19454b2d8SWarner Losh /*- 289bb1cefSJeff Roberson * Copyright (c) 2003, Jeffrey Roberson <jeff@freebsd.org> 389bb1cefSJeff Roberson * All rights reserved. 489bb1cefSJeff Roberson * 589bb1cefSJeff Roberson * Redistribution and use in source and binary forms, with or without 689bb1cefSJeff Roberson * modification, are permitted provided that the following conditions 789bb1cefSJeff Roberson * are met: 889bb1cefSJeff Roberson * 1. Redistributions of source code must retain the above copyright 989bb1cefSJeff Roberson * notice unmodified, this list of conditions, and the following 1089bb1cefSJeff Roberson * disclaimer. 1189bb1cefSJeff Roberson * 2. Redistributions in binary form must reproduce the above copyright 1289bb1cefSJeff Roberson * notice, this list of conditions and the following disclaimer in the 1389bb1cefSJeff Roberson * documentation and/or other materials provided with the distribution. 1489bb1cefSJeff Roberson * 1589bb1cefSJeff Roberson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1689bb1cefSJeff Roberson * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1789bb1cefSJeff Roberson * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1889bb1cefSJeff Roberson * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1989bb1cefSJeff Roberson * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2089bb1cefSJeff Roberson * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2189bb1cefSJeff Roberson * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2289bb1cefSJeff Roberson * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2389bb1cefSJeff Roberson * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2489bb1cefSJeff Roberson * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2589bb1cefSJeff Roberson */ 2689bb1cefSJeff Roberson 27677b542eSDavid E. O'Brien #include <sys/cdefs.h> 28677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 29677b542eSDavid E. O'Brien 30cda9a0d1SDavid Xu #include "opt_compat.h" 3160088160SDavid Xu #include "opt_posix.h" 3289bb1cefSJeff Roberson #include <sys/param.h> 3389bb1cefSJeff Roberson #include <sys/kernel.h> 3489bb1cefSJeff Roberson #include <sys/lock.h> 3589bb1cefSJeff Roberson #include <sys/mutex.h> 36acd3428bSRobert Watson #include <sys/priv.h> 3789bb1cefSJeff Roberson #include <sys/proc.h> 38c4f7f0fdSTom Rhodes #include <sys/posix4.h> 3958c77a9dSEdward Tomasz Napierala #include <sys/racct.h> 4089bb1cefSJeff Roberson #include <sys/resourcevar.h> 410d036d55SDavid Xu #include <sys/rwlock.h> 42a22ec9d8SJeff Roberson #include <sys/sched.h> 43a8b491c1SJulian Elischer #include <sys/sysctl.h> 44ed062c8dSJulian Elischer #include <sys/smp.h> 45a66fde8dSJohn Baldwin #include <sys/syscallsubr.h> 4689bb1cefSJeff Roberson #include <sys/sysent.h> 4789bb1cefSJeff Roberson #include <sys/systm.h> 4889bb1cefSJeff Roberson #include <sys/sysproto.h> 4989bb1cefSJeff Roberson #include <sys/signalvar.h> 5025a9cfc9SKonstantin Belousov #include <sys/sysctl.h> 5189bb1cefSJeff Roberson #include <sys/ucontext.h> 5289bb1cefSJeff Roberson #include <sys/thr.h> 53a0712c99SDavid Xu #include <sys/rtprio.h> 544938faa6SDavid Xu #include <sys/umtx.h> 554938faa6SDavid Xu #include <sys/limits.h> 5689bb1cefSJeff Roberson 5789bb1cefSJeff Roberson #include <machine/frame.h> 5889bb1cefSJeff Roberson 590b1f0611SDavid Xu #include <security/audit/audit.h> 600b1f0611SDavid Xu 616472ac3dSEd Schouten static SYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW, 0, 626472ac3dSEd Schouten "thread allocation"); 6325a9cfc9SKonstantin Belousov 6425a9cfc9SKonstantin Belousov static int max_threads_per_proc = 1500; 6525a9cfc9SKonstantin Belousov SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_per_proc, CTLFLAG_RW, 6625a9cfc9SKonstantin Belousov &max_threads_per_proc, 0, "Limit on threads per proc"); 6725a9cfc9SKonstantin Belousov 6825a9cfc9SKonstantin Belousov static int max_threads_hits; 6925a9cfc9SKonstantin Belousov SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_hits, CTLFLAG_RD, 703eb9ab52SEitan Adler &max_threads_hits, 0, "kern.threads.max_threads_per_proc hit count"); 7125a9cfc9SKonstantin Belousov 72841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32 73cda9a0d1SDavid Xu 74cda9a0d1SDavid Xu static inline int 75cda9a0d1SDavid Xu suword_lwpid(void *addr, lwpid_t lwpid) 76cda9a0d1SDavid Xu { 77cda9a0d1SDavid Xu int error; 78cda9a0d1SDavid Xu 79b4cf0e62SKonstantin Belousov if (SV_CURPROC_FLAG(SV_LP64)) 80cda9a0d1SDavid Xu error = suword(addr, lwpid); 81cda9a0d1SDavid Xu else 82cda9a0d1SDavid Xu error = suword32(addr, lwpid); 83cda9a0d1SDavid Xu return (error); 84cda9a0d1SDavid Xu } 85cda9a0d1SDavid Xu 86cda9a0d1SDavid Xu #else 87cda9a0d1SDavid Xu #define suword_lwpid suword 88cda9a0d1SDavid Xu #endif 89cda9a0d1SDavid Xu 90c4bd610fSDavid Xu static int create_thread(struct thread *td, mcontext_t *ctx, 91c4bd610fSDavid Xu void (*start_func)(void *), void *arg, 92c4bd610fSDavid Xu char *stack_base, size_t stack_size, 93c4bd610fSDavid Xu char *tls_base, 94c4bd610fSDavid Xu long *child_tid, long *parent_tid, 9573fa3e5bSDavid Xu int flags, struct rtprio *rtp); 96c4bd610fSDavid Xu 9789bb1cefSJeff Roberson /* 9889bb1cefSJeff Roberson * System call interface. 9989bb1cefSJeff Roberson */ 10089bb1cefSJeff Roberson int 1018451d0ddSKip Macy sys_thr_create(struct thread *td, struct thr_create_args *uap) 102cd28f17dSMarcel Moolenaar /* ucontext_t *ctx, long *id, int flags */ 10389bb1cefSJeff Roberson { 10489bb1cefSJeff Roberson ucontext_t ctx; 10589bb1cefSJeff Roberson int error; 10689bb1cefSJeff Roberson 10789bb1cefSJeff Roberson if ((error = copyin(uap->ctx, &ctx, sizeof(ctx)))) 10889bb1cefSJeff Roberson return (error); 10989bb1cefSJeff Roberson 110c4bd610fSDavid Xu error = create_thread(td, &ctx.uc_mcontext, NULL, NULL, 111a0712c99SDavid Xu NULL, 0, NULL, uap->id, NULL, uap->flags, NULL); 112c4bd610fSDavid Xu return (error); 113c4bd610fSDavid Xu } 114c4bd610fSDavid Xu 115c4bd610fSDavid Xu int 1168451d0ddSKip Macy sys_thr_new(struct thread *td, struct thr_new_args *uap) 117c4bd610fSDavid Xu /* struct thr_param * */ 118c4bd610fSDavid Xu { 119c4bd610fSDavid Xu struct thr_param param; 120cda9a0d1SDavid Xu int error; 121cda9a0d1SDavid Xu 122cda9a0d1SDavid Xu if (uap->param_size < 0 || uap->param_size > sizeof(param)) 123cda9a0d1SDavid Xu return (EINVAL); 124cda9a0d1SDavid Xu bzero(¶m, sizeof(param)); 125cda9a0d1SDavid Xu if ((error = copyin(uap->param, ¶m, uap->param_size))) 126cda9a0d1SDavid Xu return (error); 127cda9a0d1SDavid Xu return (kern_thr_new(td, ¶m)); 128cda9a0d1SDavid Xu } 129cda9a0d1SDavid Xu 130cda9a0d1SDavid Xu int 131cda9a0d1SDavid Xu kern_thr_new(struct thread *td, struct thr_param *param) 132cda9a0d1SDavid Xu { 13373fa3e5bSDavid Xu struct rtprio rtp, *rtpp; 134c4bd610fSDavid Xu int error; 135c4bd610fSDavid Xu 13673fa3e5bSDavid Xu rtpp = NULL; 137cda9a0d1SDavid Xu if (param->rtp != 0) { 138cda9a0d1SDavid Xu error = copyin(param->rtp, &rtp, sizeof(struct rtprio)); 139e4866772SRoman Divacky if (error) 140e4866772SRoman Divacky return (error); 14173fa3e5bSDavid Xu rtpp = &rtp; 142a0712c99SDavid Xu } 143cda9a0d1SDavid Xu error = create_thread(td, NULL, param->start_func, param->arg, 144cda9a0d1SDavid Xu param->stack_base, param->stack_size, param->tls_base, 145cda9a0d1SDavid Xu param->child_tid, param->parent_tid, param->flags, 14673fa3e5bSDavid Xu rtpp); 147c4bd610fSDavid Xu return (error); 148c4bd610fSDavid Xu } 149c4bd610fSDavid Xu 150c4bd610fSDavid Xu static int 151c4bd610fSDavid Xu create_thread(struct thread *td, mcontext_t *ctx, 152c4bd610fSDavid Xu void (*start_func)(void *), void *arg, 153c4bd610fSDavid Xu char *stack_base, size_t stack_size, 154c4bd610fSDavid Xu char *tls_base, 155c4bd610fSDavid Xu long *child_tid, long *parent_tid, 15673fa3e5bSDavid Xu int flags, struct rtprio *rtp) 157c4bd610fSDavid Xu { 158c4bd610fSDavid Xu stack_t stack; 159c4bd610fSDavid Xu struct thread *newtd; 160c4bd610fSDavid Xu struct proc *p; 161adc9c950SDavid Xu int error; 162c4bd610fSDavid Xu 163c4bd610fSDavid Xu p = td->td_proc; 164c4bd610fSDavid Xu 165c4bd610fSDavid Xu /* Have race condition but it is cheap. */ 166a328d535SJohn Baldwin if (p->p_numthreads >= max_threads_per_proc) { 16794ec9c02SDavid Xu ++max_threads_hits; 168ed062c8dSJulian Elischer return (EPROCLIM); 16994ec9c02SDavid Xu } 170c4bd610fSDavid Xu 17173fa3e5bSDavid Xu if (rtp != NULL) { 17273fa3e5bSDavid Xu switch(rtp->type) { 17373fa3e5bSDavid Xu case RTP_PRIO_REALTIME: 17473fa3e5bSDavid Xu case RTP_PRIO_FIFO: 175a0712c99SDavid Xu /* Only root can set scheduler policy */ 176acd3428bSRobert Watson if (priv_check(td, PRIV_SCHED_SETPOLICY) != 0) 177a0712c99SDavid Xu return (EPERM); 17873fa3e5bSDavid Xu if (rtp->prio > RTP_PRIO_MAX) 179a0712c99SDavid Xu return (EINVAL); 1802dca4ca7SDavid Xu break; 18173fa3e5bSDavid Xu case RTP_PRIO_NORMAL: 18273fa3e5bSDavid Xu rtp->prio = 0; 1832dca4ca7SDavid Xu break; 1842dca4ca7SDavid Xu default: 1852dca4ca7SDavid Xu return (EINVAL); 186a0712c99SDavid Xu } 187a0712c99SDavid Xu } 188a0712c99SDavid Xu 189afcc55f3SEdward Tomasz Napierala #ifdef RACCT 1904b5c9cf6SEdward Tomasz Napierala if (racct_enable) { 191203322f9SMateusz Guzik PROC_LOCK(p); 19258c77a9dSEdward Tomasz Napierala error = racct_add(p, RACCT_NTHR, 1); 193203322f9SMateusz Guzik PROC_UNLOCK(p); 19458c77a9dSEdward Tomasz Napierala if (error != 0) 19558c77a9dSEdward Tomasz Napierala return (EPROCLIM); 1964b5c9cf6SEdward Tomasz Napierala } 197afcc55f3SEdward Tomasz Napierala #endif 19858c77a9dSEdward Tomasz Napierala 199ad1e7d28SJulian Elischer /* Initialize our td */ 2008a945d10SKonstantin Belousov newtd = thread_alloc(0); 20158c77a9dSEdward Tomasz Napierala if (newtd == NULL) { 20258c77a9dSEdward Tomasz Napierala error = ENOMEM; 20358c77a9dSEdward Tomasz Napierala goto fail; 20458c77a9dSEdward Tomasz Napierala } 205c4bd610fSDavid Xu 206cdea31e3SPeter Holm cpu_set_upcall(newtd, td); 207cdea31e3SPeter Holm 20889bb1cefSJeff Roberson /* 209c4bd610fSDavid Xu * Try the copyout as soon as we allocate the td so we don't 210c4bd610fSDavid Xu * have to tear things down in a failure case below. 211c4bd610fSDavid Xu * Here we copy out tid to two places, one for child and one 212c4bd610fSDavid Xu * for parent, because pthread can create a detached thread, 213c4bd610fSDavid Xu * if parent wants to safely access child tid, it has to provide 214c4bd610fSDavid Xu * its storage, because child thread may exit quickly and 215c4bd610fSDavid Xu * memory is freed before parent thread can access it. 21689bb1cefSJeff Roberson */ 217c4bd610fSDavid Xu if ((child_tid != NULL && 218cda9a0d1SDavid Xu suword_lwpid(child_tid, newtd->td_tid)) || 219c4bd610fSDavid Xu (parent_tid != NULL && 220cda9a0d1SDavid Xu suword_lwpid(parent_tid, newtd->td_tid))) { 221ed062c8dSJulian Elischer thread_free(newtd); 22258c77a9dSEdward Tomasz Napierala error = EFAULT; 22358c77a9dSEdward Tomasz Napierala goto fail; 22489bb1cefSJeff Roberson } 225cda9a0d1SDavid Xu 226ed062c8dSJulian Elischer bzero(&newtd->td_startzero, 2276db36923SDavid Schultz __rangeof(struct thread, td_startzero, td_endzero)); 228ed062c8dSJulian Elischer bcopy(&td->td_startcopy, &newtd->td_startcopy, 2296db36923SDavid Schultz __rangeof(struct thread, td_startcopy, td_endcopy)); 230c4bd610fSDavid Xu newtd->td_proc = td->td_proc; 231c4bd610fSDavid Xu newtd->td_ucred = crhold(td->td_ucred); 23289bb1cefSJeff Roberson 233c4bd610fSDavid Xu if (ctx != NULL) { /* old way to set user context */ 234c4bd610fSDavid Xu error = set_mcontext(newtd, ctx); 235c4bd610fSDavid Xu if (error != 0) { 236c4bd610fSDavid Xu thread_free(newtd); 237c4bd610fSDavid Xu crfree(td->td_ucred); 23858c77a9dSEdward Tomasz Napierala goto fail; 239c4bd610fSDavid Xu } 240c4bd610fSDavid Xu } else { 241c4bd610fSDavid Xu /* Set up our machine context. */ 242c4bd610fSDavid Xu stack.ss_sp = stack_base; 243c4bd610fSDavid Xu stack.ss_size = stack_size; 244c4bd610fSDavid Xu /* Set upcall address to user thread entry function. */ 245c4bd610fSDavid Xu cpu_set_upcall_kse(newtd, start_func, arg, &stack); 246c4bd610fSDavid Xu /* Setup user TLS address and TLS pointer register. */ 247740fd64dSDavid Xu error = cpu_set_user_tls(newtd, tls_base); 248740fd64dSDavid Xu if (error != 0) { 249740fd64dSDavid Xu thread_free(newtd); 250740fd64dSDavid Xu crfree(td->td_ucred); 25158c77a9dSEdward Tomasz Napierala goto fail; 252740fd64dSDavid Xu } 253c4bd610fSDavid Xu } 254c4bd610fSDavid Xu 255203322f9SMateusz Guzik PROC_LOCK(p); 256203322f9SMateusz Guzik p->p_flag |= P_HADTHREADS; 2578460a577SJohn Birrell thread_link(newtd, p); 258c67ddc21SJulian Elischer bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name)); 259982d11f8SJeff Roberson thread_lock(td); 260ed062c8dSJulian Elischer /* let the scheduler know about these things. */ 261ed062c8dSJulian Elischer sched_fork_thread(td, newtd); 262982d11f8SJeff Roberson thread_unlock(td); 263b7edba77SJeff Roberson if (P_SHOULDSTOP(p)) 264b7edba77SJeff Roberson newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK; 265982d11f8SJeff Roberson PROC_UNLOCK(p); 266cf7d9a8cSDavid Xu 267cf7d9a8cSDavid Xu tidhash_add(newtd); 268cf7d9a8cSDavid Xu 269982d11f8SJeff Roberson thread_lock(newtd); 27073fa3e5bSDavid Xu if (rtp != NULL) { 2718460a577SJohn Birrell if (!(td->td_pri_class == PRI_TIMESHARE && 2728460a577SJohn Birrell rtp->type == RTP_PRIO_NORMAL)) { 2738460a577SJohn Birrell rtp_to_pri(rtp, newtd); 2748460a577SJohn Birrell sched_prio(newtd, newtd->td_user_pri); 2758460a577SJohn Birrell } /* ignore timesharing class */ 2768460a577SJohn Birrell } 277ed062c8dSJulian Elischer TD_SET_CAN_RUN(newtd); 278f0393f06SJeff Roberson sched_add(newtd, SRQ_BORING); 279982d11f8SJeff Roberson thread_unlock(newtd); 28089bb1cefSJeff Roberson 281c90c9021SEd Schouten return (0); 28258c77a9dSEdward Tomasz Napierala 28358c77a9dSEdward Tomasz Napierala fail: 284afcc55f3SEdward Tomasz Napierala #ifdef RACCT 2854b5c9cf6SEdward Tomasz Napierala if (racct_enable) { 28658c77a9dSEdward Tomasz Napierala PROC_LOCK(p); 28758c77a9dSEdward Tomasz Napierala racct_sub(p, RACCT_NTHR, 1); 28858c77a9dSEdward Tomasz Napierala PROC_UNLOCK(p); 2894b5c9cf6SEdward Tomasz Napierala } 290afcc55f3SEdward Tomasz Napierala #endif 29158c77a9dSEdward Tomasz Napierala return (error); 29289bb1cefSJeff Roberson } 29389bb1cefSJeff Roberson 29489bb1cefSJeff Roberson int 2958451d0ddSKip Macy sys_thr_self(struct thread *td, struct thr_self_args *uap) 296cd28f17dSMarcel Moolenaar /* long *id */ 29789bb1cefSJeff Roberson { 29889bb1cefSJeff Roberson int error; 29989bb1cefSJeff Roberson 300cda9a0d1SDavid Xu error = suword_lwpid(uap->id, (unsigned)td->td_tid); 301cda9a0d1SDavid Xu if (error == -1) 302cda9a0d1SDavid Xu return (EFAULT); 30389bb1cefSJeff Roberson return (0); 30489bb1cefSJeff Roberson } 30589bb1cefSJeff Roberson 30689bb1cefSJeff Roberson int 3078451d0ddSKip Macy sys_thr_exit(struct thread *td, struct thr_exit_args *uap) 308401901acSMike Makonnen /* long *state */ 30989bb1cefSJeff Roberson { 31089bb1cefSJeff Roberson 311401901acSMike Makonnen /* Signal userland that it can free the stack. */ 3124938faa6SDavid Xu if ((void *)uap->state != NULL) { 313cda9a0d1SDavid Xu suword_lwpid(uap->state, 1); 3143bba58f2SDavid Xu kern_umtx_wake(td, uap->state, INT_MAX, 0); 3154938faa6SDavid Xu } 316401901acSMike Makonnen 317*95be6d2bSDmitry Chagin return (kern_thr_exit(td)); 318*95be6d2bSDmitry Chagin } 31958c77a9dSEdward Tomasz Napierala 320*95be6d2bSDmitry Chagin int 321*95be6d2bSDmitry Chagin kern_thr_exit(struct thread *td) 322*95be6d2bSDmitry Chagin { 323*95be6d2bSDmitry Chagin struct proc *p; 324*95be6d2bSDmitry Chagin 325*95be6d2bSDmitry Chagin p = td->td_proc; 326*95be6d2bSDmitry Chagin 327*95be6d2bSDmitry Chagin rw_wlock(&tidhash_lock); 32889bb1cefSJeff Roberson PROC_LOCK(p); 32958c77a9dSEdward Tomasz Napierala 330ed062c8dSJulian Elischer if (p->p_numthreads != 1) { 33147f6635cSEdward Tomasz Napierala racct_sub(p, RACCT_NTHR, 1); 3320d036d55SDavid Xu LIST_REMOVE(td, td_hash); 3330d036d55SDavid Xu rw_wunlock(&tidhash_lock); 3340d036d55SDavid Xu tdsigcleanup(td); 33513dad108SKonstantin Belousov umtx_thread_exit(td); 3360d036d55SDavid Xu PROC_SLOCK(p); 33771b7afb2SDavid Xu thread_stopped(p); 338ed062c8dSJulian Elischer thread_exit(); 339ed062c8dSJulian Elischer /* NOTREACHED */ 340ed062c8dSJulian Elischer } 34174d5b4afSKonstantin Belousov 34274d5b4afSKonstantin Belousov /* 34374d5b4afSKonstantin Belousov * Ignore attempts to shut down last thread in the proc. This 34474d5b4afSKonstantin Belousov * will actually call _exit(2) in the usermode trampoline when 34574d5b4afSKonstantin Belousov * it returns. 34674d5b4afSKonstantin Belousov */ 347ed062c8dSJulian Elischer PROC_UNLOCK(p); 3480d036d55SDavid Xu rw_wunlock(&tidhash_lock); 34989bb1cefSJeff Roberson return (0); 35089bb1cefSJeff Roberson } 35189bb1cefSJeff Roberson 35289bb1cefSJeff Roberson int 3538451d0ddSKip Macy sys_thr_kill(struct thread *td, struct thr_kill_args *uap) 354cd28f17dSMarcel Moolenaar /* long id, int sig */ 35589bb1cefSJeff Roberson { 3565f73a7ebSBruno Ducrot ksiginfo_t ksi; 35789bb1cefSJeff Roberson struct thread *ttd; 35889bb1cefSJeff Roberson struct proc *p; 35989bb1cefSJeff Roberson int error; 36089bb1cefSJeff Roberson 36189bb1cefSJeff Roberson p = td->td_proc; 3625f73a7ebSBruno Ducrot ksiginfo_init(&ksi); 3635f73a7ebSBruno Ducrot ksi.ksi_signo = uap->sig; 364baf28b69SDavid Xu ksi.ksi_code = SI_LWP; 3655f73a7ebSBruno Ducrot ksi.ksi_pid = p->p_pid; 3665f73a7ebSBruno Ducrot ksi.ksi_uid = td->td_ucred->cr_ruid; 3670a5cd498SDavid Xu if (uap->id == -1) { 3680a5cd498SDavid Xu if (uap->sig != 0 && !_SIG_VALID(uap->sig)) { 36989bb1cefSJeff Roberson error = EINVAL; 3700a5cd498SDavid Xu } else { 3710a5cd498SDavid Xu error = ESRCH; 372cf7d9a8cSDavid Xu PROC_LOCK(p); 3730a5cd498SDavid Xu FOREACH_THREAD_IN_PROC(p, ttd) { 3740a5cd498SDavid Xu if (ttd != td) { 3750a5cd498SDavid Xu error = 0; 3760a5cd498SDavid Xu if (uap->sig == 0) 3770a5cd498SDavid Xu break; 378ad6eec7bSJohn Baldwin tdksignal(ttd, uap->sig, &ksi); 3790a5cd498SDavid Xu } 3800a5cd498SDavid Xu } 381cf7d9a8cSDavid Xu PROC_UNLOCK(p); 3820a5cd498SDavid Xu } 3830a5cd498SDavid Xu } else { 384cf7d9a8cSDavid Xu error = 0; 385cf7d9a8cSDavid Xu ttd = tdfind((lwpid_t)uap->id, p->p_pid); 3860a5cd498SDavid Xu if (ttd == NULL) 387cf7d9a8cSDavid Xu return (ESRCH); 388cf7d9a8cSDavid Xu if (uap->sig == 0) 3890a5cd498SDavid Xu ; 3900a5cd498SDavid Xu else if (!_SIG_VALID(uap->sig)) 3910a5cd498SDavid Xu error = EINVAL; 3920a5cd498SDavid Xu else 393ad6eec7bSJohn Baldwin tdksignal(ttd, uap->sig, &ksi); 394cf7d9a8cSDavid Xu PROC_UNLOCK(ttd->td_proc); 3950a5cd498SDavid Xu } 39689bb1cefSJeff Roberson return (error); 39789bb1cefSJeff Roberson } 3981713a516SMike Makonnen 3991713a516SMike Makonnen int 4008451d0ddSKip Macy sys_thr_kill2(struct thread *td, struct thr_kill2_args *uap) 4010b1f0611SDavid Xu /* pid_t pid, long id, int sig */ 4020b1f0611SDavid Xu { 4035f73a7ebSBruno Ducrot ksiginfo_t ksi; 4040b1f0611SDavid Xu struct thread *ttd; 4050b1f0611SDavid Xu struct proc *p; 4060b1f0611SDavid Xu int error; 4070b1f0611SDavid Xu 40814961ba7SRobert Watson AUDIT_ARG_SIGNUM(uap->sig); 4090b1f0611SDavid Xu 4105f73a7ebSBruno Ducrot ksiginfo_init(&ksi); 4115f73a7ebSBruno Ducrot ksi.ksi_signo = uap->sig; 412baf28b69SDavid Xu ksi.ksi_code = SI_LWP; 4135f73a7ebSBruno Ducrot ksi.ksi_pid = td->td_proc->p_pid; 4145f73a7ebSBruno Ducrot ksi.ksi_uid = td->td_ucred->cr_ruid; 4150b1f0611SDavid Xu if (uap->id == -1) { 416cf7d9a8cSDavid Xu if ((p = pfind(uap->pid)) == NULL) 417cf7d9a8cSDavid Xu return (ESRCH); 418cf7d9a8cSDavid Xu AUDIT_ARG_PROCESS(p); 419cf7d9a8cSDavid Xu error = p_cansignal(td, p, uap->sig); 420cf7d9a8cSDavid Xu if (error) { 421cf7d9a8cSDavid Xu PROC_UNLOCK(p); 422cf7d9a8cSDavid Xu return (error); 423cf7d9a8cSDavid Xu } 4240b1f0611SDavid Xu if (uap->sig != 0 && !_SIG_VALID(uap->sig)) { 4250b1f0611SDavid Xu error = EINVAL; 4260b1f0611SDavid Xu } else { 4270b1f0611SDavid Xu error = ESRCH; 4280b1f0611SDavid Xu FOREACH_THREAD_IN_PROC(p, ttd) { 4290b1f0611SDavid Xu if (ttd != td) { 4300b1f0611SDavid Xu error = 0; 4310b1f0611SDavid Xu if (uap->sig == 0) 4320b1f0611SDavid Xu break; 433ad6eec7bSJohn Baldwin tdksignal(ttd, uap->sig, &ksi); 4340b1f0611SDavid Xu } 4350b1f0611SDavid Xu } 4360b1f0611SDavid Xu } 437cf7d9a8cSDavid Xu PROC_UNLOCK(p); 4380b1f0611SDavid Xu } else { 439cf7d9a8cSDavid Xu ttd = tdfind((lwpid_t)uap->id, uap->pid); 4400b1f0611SDavid Xu if (ttd == NULL) 441cf7d9a8cSDavid Xu return (ESRCH); 442cf7d9a8cSDavid Xu p = ttd->td_proc; 443cf7d9a8cSDavid Xu AUDIT_ARG_PROCESS(p); 444cf7d9a8cSDavid Xu error = p_cansignal(td, p, uap->sig); 445cf7d9a8cSDavid Xu if (uap->sig == 0) 4460b1f0611SDavid Xu ; 4470b1f0611SDavid Xu else if (!_SIG_VALID(uap->sig)) 4480b1f0611SDavid Xu error = EINVAL; 4490b1f0611SDavid Xu else 450ad6eec7bSJohn Baldwin tdksignal(ttd, uap->sig, &ksi); 4510b1f0611SDavid Xu PROC_UNLOCK(p); 452cf7d9a8cSDavid Xu } 4530b1f0611SDavid Xu return (error); 4540b1f0611SDavid Xu } 4550b1f0611SDavid Xu 4560b1f0611SDavid Xu int 4578451d0ddSKip Macy sys_thr_suspend(struct thread *td, struct thr_suspend_args *uap) 4581713a516SMike Makonnen /* const struct timespec *timeout */ 4591713a516SMike Makonnen { 460cda9a0d1SDavid Xu struct timespec ts, *tsp; 4611713a516SMike Makonnen int error; 4621713a516SMike Makonnen 463cda9a0d1SDavid Xu tsp = NULL; 4641713a516SMike Makonnen if (uap->timeout != NULL) { 4659a1d0cf6SPeter Holm error = umtx_copyin_timeout(uap->timeout, &ts); 4661713a516SMike Makonnen if (error != 0) 4671713a516SMike Makonnen return (error); 468cda9a0d1SDavid Xu tsp = &ts; 469cda9a0d1SDavid Xu } 470cda9a0d1SDavid Xu 471cda9a0d1SDavid Xu return (kern_thr_suspend(td, tsp)); 472cda9a0d1SDavid Xu } 473cda9a0d1SDavid Xu 474cda9a0d1SDavid Xu int 475cda9a0d1SDavid Xu kern_thr_suspend(struct thread *td, struct timespec *tsp) 476cda9a0d1SDavid Xu { 477cfca8a18SDavid Xu struct proc *p = td->td_proc; 478cda9a0d1SDavid Xu struct timeval tv; 4792961a782SDavid Xu int error = 0; 4802961a782SDavid Xu int timo = 0; 481cda9a0d1SDavid Xu 482745fbd3aSDavid Xu if (td->td_pflags & TDP_WAKEUP) { 483745fbd3aSDavid Xu td->td_pflags &= ~TDP_WAKEUP; 484745fbd3aSDavid Xu return (0); 485745fbd3aSDavid Xu } 486745fbd3aSDavid Xu 487cfca8a18SDavid Xu if (tsp != NULL) { 4882961a782SDavid Xu if (tsp->tv_sec == 0 && tsp->tv_nsec == 0) 4892961a782SDavid Xu error = EWOULDBLOCK; 4902961a782SDavid Xu else { 4912961a782SDavid Xu TIMESPEC_TO_TIMEVAL(&tv, tsp); 4922961a782SDavid Xu timo = tvtohz(&tv); 493cfca8a18SDavid Xu } 494cfca8a18SDavid Xu } 495cfca8a18SDavid Xu 496cfca8a18SDavid Xu PROC_LOCK(p); 497cfca8a18SDavid Xu if (error == 0 && (td->td_flags & TDF_THRWAKEUP) == 0) 498cfca8a18SDavid Xu error = msleep((void *)td, &p->p_mtx, 4992961a782SDavid Xu PCATCH, "lthr", timo); 5002961a782SDavid Xu 501c1df5a1aSDavid Xu if (td->td_flags & TDF_THRWAKEUP) { 502982d11f8SJeff Roberson thread_lock(td); 5031713a516SMike Makonnen td->td_flags &= ~TDF_THRWAKEUP; 504982d11f8SJeff Roberson thread_unlock(td); 505cfca8a18SDavid Xu PROC_UNLOCK(p); 506c1df5a1aSDavid Xu return (0); 507c1df5a1aSDavid Xu } 508cfca8a18SDavid Xu PROC_UNLOCK(p); 509c1df5a1aSDavid Xu if (error == EWOULDBLOCK) 510c1df5a1aSDavid Xu error = ETIMEDOUT; 511c1df5a1aSDavid Xu else if (error == ERESTART) { 5122961a782SDavid Xu if (timo != 0) 513c1df5a1aSDavid Xu error = EINTR; 514c1df5a1aSDavid Xu } 515c1df5a1aSDavid Xu return (error); 5161713a516SMike Makonnen } 5171713a516SMike Makonnen 5181713a516SMike Makonnen int 5198451d0ddSKip Macy sys_thr_wake(struct thread *td, struct thr_wake_args *uap) 520cd28f17dSMarcel Moolenaar /* long id */ 5211713a516SMike Makonnen { 52244355392SDavid Xu struct proc *p; 523cd28f17dSMarcel Moolenaar struct thread *ttd; 5241713a516SMike Makonnen 525745fbd3aSDavid Xu if (uap->id == td->td_tid) { 526745fbd3aSDavid Xu td->td_pflags |= TDP_WAKEUP; 527745fbd3aSDavid Xu return (0); 528745fbd3aSDavid Xu } 529745fbd3aSDavid Xu 53044355392SDavid Xu p = td->td_proc; 531cf7d9a8cSDavid Xu ttd = tdfind((lwpid_t)uap->id, p->p_pid); 532cf7d9a8cSDavid Xu if (ttd == NULL) 5331713a516SMike Makonnen return (ESRCH); 534982d11f8SJeff Roberson thread_lock(ttd); 535cd28f17dSMarcel Moolenaar ttd->td_flags |= TDF_THRWAKEUP; 536982d11f8SJeff Roberson thread_unlock(ttd); 537c1df5a1aSDavid Xu wakeup((void *)ttd); 53844355392SDavid Xu PROC_UNLOCK(p); 5391713a516SMike Makonnen return (0); 5401713a516SMike Makonnen } 5419e7d7224SDavid Xu 5429e7d7224SDavid Xu int 5438451d0ddSKip Macy sys_thr_set_name(struct thread *td, struct thr_set_name_args *uap) 5449e7d7224SDavid Xu { 545cf7d9a8cSDavid Xu struct proc *p; 5469e7d7224SDavid Xu char name[MAXCOMLEN + 1]; 5479e7d7224SDavid Xu struct thread *ttd; 5489e7d7224SDavid Xu int error; 5499e7d7224SDavid Xu 5509e7d7224SDavid Xu error = 0; 5519e7d7224SDavid Xu name[0] = '\0'; 5529e7d7224SDavid Xu if (uap->name != NULL) { 5539e7d7224SDavid Xu error = copyinstr(uap->name, name, sizeof(name), 5549e7d7224SDavid Xu NULL); 5559e7d7224SDavid Xu if (error) 5569e7d7224SDavid Xu return (error); 5579e7d7224SDavid Xu } 558cf7d9a8cSDavid Xu p = td->td_proc; 559cf7d9a8cSDavid Xu ttd = tdfind((lwpid_t)uap->id, p->p_pid); 560cf7d9a8cSDavid Xu if (ttd == NULL) 561cf7d9a8cSDavid Xu return (ESRCH); 5629e7d7224SDavid Xu strcpy(ttd->td_name, name); 56344ad5475SJohn Baldwin #ifdef KTR 56444ad5475SJohn Baldwin sched_clear_tdname(ttd); 56544ad5475SJohn Baldwin #endif 5669e7d7224SDavid Xu PROC_UNLOCK(p); 5679e7d7224SDavid Xu return (error); 5689e7d7224SDavid Xu } 569