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 576520495aSAdrian Chadd #include <vm/vm_domain.h> 586520495aSAdrian Chadd 5989bb1cefSJeff Roberson #include <machine/frame.h> 6089bb1cefSJeff Roberson 610b1f0611SDavid Xu #include <security/audit/audit.h> 620b1f0611SDavid Xu 636472ac3dSEd Schouten static SYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW, 0, 646472ac3dSEd Schouten "thread allocation"); 6525a9cfc9SKonstantin Belousov 6625a9cfc9SKonstantin Belousov static int max_threads_per_proc = 1500; 6725a9cfc9SKonstantin Belousov SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_per_proc, CTLFLAG_RW, 6825a9cfc9SKonstantin Belousov &max_threads_per_proc, 0, "Limit on threads per proc"); 6925a9cfc9SKonstantin Belousov 7025a9cfc9SKonstantin Belousov static int max_threads_hits; 7125a9cfc9SKonstantin Belousov SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_hits, CTLFLAG_RD, 723eb9ab52SEitan Adler &max_threads_hits, 0, "kern.threads.max_threads_per_proc hit count"); 7325a9cfc9SKonstantin Belousov 74841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32 75cda9a0d1SDavid Xu 76cda9a0d1SDavid Xu static inline int 77cda9a0d1SDavid Xu suword_lwpid(void *addr, lwpid_t lwpid) 78cda9a0d1SDavid Xu { 79cda9a0d1SDavid Xu int error; 80cda9a0d1SDavid Xu 81b4cf0e62SKonstantin Belousov if (SV_CURPROC_FLAG(SV_LP64)) 82cda9a0d1SDavid Xu error = suword(addr, lwpid); 83cda9a0d1SDavid Xu else 84cda9a0d1SDavid Xu error = suword32(addr, lwpid); 85cda9a0d1SDavid Xu return (error); 86cda9a0d1SDavid Xu } 87cda9a0d1SDavid Xu 88cda9a0d1SDavid Xu #else 89cda9a0d1SDavid Xu #define suword_lwpid suword 90cda9a0d1SDavid Xu #endif 91cda9a0d1SDavid Xu 9289bb1cefSJeff Roberson /* 9389bb1cefSJeff Roberson * System call interface. 9489bb1cefSJeff Roberson */ 95*5a170c1bSEd Schouten 96*5a170c1bSEd Schouten struct thr_create_initthr_args { 97*5a170c1bSEd Schouten ucontext_t ctx; 98*5a170c1bSEd Schouten long *tid; 99*5a170c1bSEd Schouten }; 100*5a170c1bSEd Schouten 101*5a170c1bSEd Schouten static int 102*5a170c1bSEd Schouten thr_create_initthr(struct thread *td, void *thunk) 103*5a170c1bSEd Schouten { 104*5a170c1bSEd Schouten struct thr_create_initthr_args *args; 105*5a170c1bSEd Schouten 106*5a170c1bSEd Schouten /* Copy out the child tid. */ 107*5a170c1bSEd Schouten args = thunk; 108*5a170c1bSEd Schouten if (args->tid != NULL && suword_lwpid(args->tid, td->td_tid)) 109*5a170c1bSEd Schouten return (EFAULT); 110*5a170c1bSEd Schouten 111*5a170c1bSEd Schouten return (set_mcontext(td, &args->ctx.uc_mcontext)); 112*5a170c1bSEd Schouten } 113*5a170c1bSEd Schouten 11489bb1cefSJeff Roberson int 1158451d0ddSKip Macy sys_thr_create(struct thread *td, struct thr_create_args *uap) 116cd28f17dSMarcel Moolenaar /* ucontext_t *ctx, long *id, int flags */ 11789bb1cefSJeff Roberson { 118*5a170c1bSEd Schouten struct thr_create_initthr_args args; 11989bb1cefSJeff Roberson int error; 12089bb1cefSJeff Roberson 121*5a170c1bSEd Schouten if ((error = copyin(uap->ctx, &args.ctx, sizeof(args.ctx)))) 12289bb1cefSJeff Roberson return (error); 123*5a170c1bSEd Schouten args.tid = uap->id; 124*5a170c1bSEd Schouten return (thread_create(td, NULL, thr_create_initthr, &args)); 125c4bd610fSDavid Xu } 126c4bd610fSDavid Xu 127c4bd610fSDavid Xu int 1288451d0ddSKip Macy sys_thr_new(struct thread *td, struct thr_new_args *uap) 129c4bd610fSDavid Xu /* struct thr_param * */ 130c4bd610fSDavid Xu { 131c4bd610fSDavid Xu struct thr_param param; 132cda9a0d1SDavid Xu int error; 133cda9a0d1SDavid Xu 134cda9a0d1SDavid Xu if (uap->param_size < 0 || uap->param_size > sizeof(param)) 135cda9a0d1SDavid Xu return (EINVAL); 136cda9a0d1SDavid Xu bzero(¶m, sizeof(param)); 137cda9a0d1SDavid Xu if ((error = copyin(uap->param, ¶m, uap->param_size))) 138cda9a0d1SDavid Xu return (error); 139cda9a0d1SDavid Xu return (kern_thr_new(td, ¶m)); 140cda9a0d1SDavid Xu } 141cda9a0d1SDavid Xu 142*5a170c1bSEd Schouten static int 143*5a170c1bSEd Schouten thr_new_initthr(struct thread *td, void *thunk) 144*5a170c1bSEd Schouten { 145*5a170c1bSEd Schouten stack_t stack; 146*5a170c1bSEd Schouten struct thr_param *param; 147*5a170c1bSEd Schouten 148*5a170c1bSEd Schouten /* 149*5a170c1bSEd Schouten * Here we copy out tid to two places, one for child and one 150*5a170c1bSEd Schouten * for parent, because pthread can create a detached thread, 151*5a170c1bSEd Schouten * if parent wants to safely access child tid, it has to provide 152*5a170c1bSEd Schouten * its storage, because child thread may exit quickly and 153*5a170c1bSEd Schouten * memory is freed before parent thread can access it. 154*5a170c1bSEd Schouten */ 155*5a170c1bSEd Schouten param = thunk; 156*5a170c1bSEd Schouten if ((param->child_tid != NULL && 157*5a170c1bSEd Schouten suword_lwpid(param->child_tid, td->td_tid)) || 158*5a170c1bSEd Schouten (param->parent_tid != NULL && 159*5a170c1bSEd Schouten suword_lwpid(param->parent_tid, td->td_tid))) 160*5a170c1bSEd Schouten return (EFAULT); 161*5a170c1bSEd Schouten 162*5a170c1bSEd Schouten /* Set up our machine context. */ 163*5a170c1bSEd Schouten stack.ss_sp = param->stack_base; 164*5a170c1bSEd Schouten stack.ss_size = param->stack_size; 165*5a170c1bSEd Schouten /* Set upcall address to user thread entry function. */ 166*5a170c1bSEd Schouten cpu_set_upcall_kse(td, param->start_func, param->arg, &stack); 167*5a170c1bSEd Schouten /* Setup user TLS address and TLS pointer register. */ 168*5a170c1bSEd Schouten return (cpu_set_user_tls(td, param->tls_base)); 169*5a170c1bSEd Schouten } 170*5a170c1bSEd Schouten 171cda9a0d1SDavid Xu int 172cda9a0d1SDavid Xu kern_thr_new(struct thread *td, struct thr_param *param) 173cda9a0d1SDavid Xu { 17473fa3e5bSDavid Xu struct rtprio rtp, *rtpp; 175c4bd610fSDavid Xu int error; 176c4bd610fSDavid Xu 17773fa3e5bSDavid Xu rtpp = NULL; 178cda9a0d1SDavid Xu if (param->rtp != 0) { 179cda9a0d1SDavid Xu error = copyin(param->rtp, &rtp, sizeof(struct rtprio)); 180e4866772SRoman Divacky if (error) 181e4866772SRoman Divacky return (error); 18273fa3e5bSDavid Xu rtpp = &rtp; 183a0712c99SDavid Xu } 184*5a170c1bSEd Schouten return (thread_create(td, rtpp, thr_new_initthr, param)); 185c4bd610fSDavid Xu } 186c4bd610fSDavid Xu 187*5a170c1bSEd Schouten int 188*5a170c1bSEd Schouten thread_create(struct thread *td, struct rtprio *rtp, 189*5a170c1bSEd Schouten int (*initialize_thread)(struct thread *, void *), void *thunk) 190c4bd610fSDavid Xu { 191c4bd610fSDavid Xu struct thread *newtd; 192c4bd610fSDavid Xu struct proc *p; 193adc9c950SDavid Xu int error; 194c4bd610fSDavid Xu 195c4bd610fSDavid Xu p = td->td_proc; 196c4bd610fSDavid Xu 19773fa3e5bSDavid Xu if (rtp != NULL) { 19873fa3e5bSDavid Xu switch(rtp->type) { 19973fa3e5bSDavid Xu case RTP_PRIO_REALTIME: 20073fa3e5bSDavid Xu case RTP_PRIO_FIFO: 201a0712c99SDavid Xu /* Only root can set scheduler policy */ 202acd3428bSRobert Watson if (priv_check(td, PRIV_SCHED_SETPOLICY) != 0) 203a0712c99SDavid Xu return (EPERM); 20473fa3e5bSDavid Xu if (rtp->prio > RTP_PRIO_MAX) 205a0712c99SDavid Xu return (EINVAL); 2062dca4ca7SDavid Xu break; 20773fa3e5bSDavid Xu case RTP_PRIO_NORMAL: 20873fa3e5bSDavid Xu rtp->prio = 0; 2092dca4ca7SDavid Xu break; 2102dca4ca7SDavid Xu default: 2112dca4ca7SDavid Xu return (EINVAL); 212a0712c99SDavid Xu } 213a0712c99SDavid Xu } 214a0712c99SDavid Xu 215afcc55f3SEdward Tomasz Napierala #ifdef RACCT 2164b5c9cf6SEdward Tomasz Napierala if (racct_enable) { 217203322f9SMateusz Guzik PROC_LOCK(p); 21858c77a9dSEdward Tomasz Napierala error = racct_add(p, RACCT_NTHR, 1); 219203322f9SMateusz Guzik PROC_UNLOCK(p); 22058c77a9dSEdward Tomasz Napierala if (error != 0) 22158c77a9dSEdward Tomasz Napierala return (EPROCLIM); 2224b5c9cf6SEdward Tomasz Napierala } 223afcc55f3SEdward Tomasz Napierala #endif 22458c77a9dSEdward Tomasz Napierala 225ad1e7d28SJulian Elischer /* Initialize our td */ 22609baafb4SDmitry Chagin error = kern_thr_alloc(p, 0, &newtd); 22709baafb4SDmitry Chagin if (error) 22858c77a9dSEdward Tomasz Napierala goto fail; 229c4bd610fSDavid Xu 230cdea31e3SPeter Holm cpu_set_upcall(newtd, td); 231cdea31e3SPeter Holm 232ed062c8dSJulian Elischer bzero(&newtd->td_startzero, 2336db36923SDavid Schultz __rangeof(struct thread, td_startzero, td_endzero)); 234ed062c8dSJulian Elischer bcopy(&td->td_startcopy, &newtd->td_startcopy, 2356db36923SDavid Schultz __rangeof(struct thread, td_startcopy, td_endcopy)); 236c4bd610fSDavid Xu newtd->td_proc = td->td_proc; 2374ea6a9a2SMateusz Guzik thread_cow_get(newtd, td); 23889bb1cefSJeff Roberson 239*5a170c1bSEd Schouten error = initialize_thread(newtd, thunk); 240c4bd610fSDavid Xu if (error != 0) { 2414ea6a9a2SMateusz Guzik thread_cow_free(newtd); 242c4bd610fSDavid Xu thread_free(newtd); 24358c77a9dSEdward Tomasz Napierala goto fail; 244c4bd610fSDavid Xu } 245c4bd610fSDavid Xu 246203322f9SMateusz Guzik PROC_LOCK(p); 247203322f9SMateusz Guzik p->p_flag |= P_HADTHREADS; 2488460a577SJohn Birrell thread_link(newtd, p); 249c67ddc21SJulian Elischer bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name)); 250982d11f8SJeff Roberson thread_lock(td); 251ed062c8dSJulian Elischer /* let the scheduler know about these things. */ 252ed062c8dSJulian Elischer sched_fork_thread(td, newtd); 253982d11f8SJeff Roberson thread_unlock(td); 254b7edba77SJeff Roberson if (P_SHOULDSTOP(p)) 255b7edba77SJeff Roberson newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK; 2566520495aSAdrian Chadd 2576520495aSAdrian Chadd /* 2586520495aSAdrian Chadd * Copy the existing thread VM policy into the new thread. 2596520495aSAdrian Chadd */ 2606520495aSAdrian Chadd vm_domain_policy_localcopy(&newtd->td_vm_dom_policy, 2616520495aSAdrian Chadd &td->td_vm_dom_policy); 2626520495aSAdrian Chadd 263982d11f8SJeff Roberson PROC_UNLOCK(p); 264cf7d9a8cSDavid Xu 265cf7d9a8cSDavid Xu tidhash_add(newtd); 266cf7d9a8cSDavid Xu 267982d11f8SJeff Roberson thread_lock(newtd); 26873fa3e5bSDavid Xu if (rtp != NULL) { 2698460a577SJohn Birrell if (!(td->td_pri_class == PRI_TIMESHARE && 2708460a577SJohn Birrell rtp->type == RTP_PRIO_NORMAL)) { 2718460a577SJohn Birrell rtp_to_pri(rtp, newtd); 2728460a577SJohn Birrell sched_prio(newtd, newtd->td_user_pri); 2738460a577SJohn Birrell } /* ignore timesharing class */ 2748460a577SJohn Birrell } 275ed062c8dSJulian Elischer TD_SET_CAN_RUN(newtd); 276f0393f06SJeff Roberson sched_add(newtd, SRQ_BORING); 277982d11f8SJeff Roberson thread_unlock(newtd); 27889bb1cefSJeff Roberson 279c90c9021SEd Schouten return (0); 28058c77a9dSEdward Tomasz Napierala 28158c77a9dSEdward Tomasz Napierala fail: 282afcc55f3SEdward Tomasz Napierala #ifdef RACCT 2834b5c9cf6SEdward Tomasz Napierala if (racct_enable) { 28458c77a9dSEdward Tomasz Napierala PROC_LOCK(p); 28558c77a9dSEdward Tomasz Napierala racct_sub(p, RACCT_NTHR, 1); 28658c77a9dSEdward Tomasz Napierala PROC_UNLOCK(p); 2874b5c9cf6SEdward Tomasz Napierala } 288afcc55f3SEdward Tomasz Napierala #endif 28958c77a9dSEdward Tomasz Napierala return (error); 29089bb1cefSJeff Roberson } 29189bb1cefSJeff Roberson 29289bb1cefSJeff Roberson int 2938451d0ddSKip Macy sys_thr_self(struct thread *td, struct thr_self_args *uap) 294cd28f17dSMarcel Moolenaar /* long *id */ 29589bb1cefSJeff Roberson { 29689bb1cefSJeff Roberson int error; 29789bb1cefSJeff Roberson 298cda9a0d1SDavid Xu error = suword_lwpid(uap->id, (unsigned)td->td_tid); 299cda9a0d1SDavid Xu if (error == -1) 300cda9a0d1SDavid Xu return (EFAULT); 30189bb1cefSJeff Roberson return (0); 30289bb1cefSJeff Roberson } 30389bb1cefSJeff Roberson 30489bb1cefSJeff Roberson int 3058451d0ddSKip Macy sys_thr_exit(struct thread *td, struct thr_exit_args *uap) 306401901acSMike Makonnen /* long *state */ 30789bb1cefSJeff Roberson { 30889bb1cefSJeff Roberson 309401901acSMike Makonnen /* Signal userland that it can free the stack. */ 3104938faa6SDavid Xu if ((void *)uap->state != NULL) { 311cda9a0d1SDavid Xu suword_lwpid(uap->state, 1); 3123bba58f2SDavid Xu kern_umtx_wake(td, uap->state, INT_MAX, 0); 3134938faa6SDavid Xu } 314401901acSMike Makonnen 31595be6d2bSDmitry Chagin return (kern_thr_exit(td)); 31695be6d2bSDmitry Chagin } 31758c77a9dSEdward Tomasz Napierala 31895be6d2bSDmitry Chagin int 31995be6d2bSDmitry Chagin kern_thr_exit(struct thread *td) 32095be6d2bSDmitry Chagin { 32195be6d2bSDmitry Chagin struct proc *p; 32295be6d2bSDmitry Chagin 32395be6d2bSDmitry Chagin p = td->td_proc; 32495be6d2bSDmitry Chagin 32595be6d2bSDmitry Chagin rw_wlock(&tidhash_lock); 32689bb1cefSJeff Roberson PROC_LOCK(p); 32758c77a9dSEdward Tomasz Napierala 328ed062c8dSJulian Elischer if (p->p_numthreads != 1) { 32947f6635cSEdward Tomasz Napierala racct_sub(p, RACCT_NTHR, 1); 3300d036d55SDavid Xu LIST_REMOVE(td, td_hash); 3310d036d55SDavid Xu rw_wunlock(&tidhash_lock); 3320d036d55SDavid Xu tdsigcleanup(td); 33313dad108SKonstantin Belousov umtx_thread_exit(td); 3340d036d55SDavid Xu PROC_SLOCK(p); 33571b7afb2SDavid Xu thread_stopped(p); 336ed062c8dSJulian Elischer thread_exit(); 337ed062c8dSJulian Elischer /* NOTREACHED */ 338ed062c8dSJulian Elischer } 33974d5b4afSKonstantin Belousov 34074d5b4afSKonstantin Belousov /* 34174d5b4afSKonstantin Belousov * Ignore attempts to shut down last thread in the proc. This 34274d5b4afSKonstantin Belousov * will actually call _exit(2) in the usermode trampoline when 34374d5b4afSKonstantin Belousov * it returns. 34474d5b4afSKonstantin Belousov */ 345ed062c8dSJulian Elischer PROC_UNLOCK(p); 3460d036d55SDavid Xu rw_wunlock(&tidhash_lock); 34789bb1cefSJeff Roberson return (0); 34889bb1cefSJeff Roberson } 34989bb1cefSJeff Roberson 35089bb1cefSJeff Roberson int 3518451d0ddSKip Macy sys_thr_kill(struct thread *td, struct thr_kill_args *uap) 352cd28f17dSMarcel Moolenaar /* long id, int sig */ 35389bb1cefSJeff Roberson { 3545f73a7ebSBruno Ducrot ksiginfo_t ksi; 35589bb1cefSJeff Roberson struct thread *ttd; 35689bb1cefSJeff Roberson struct proc *p; 35789bb1cefSJeff Roberson int error; 35889bb1cefSJeff Roberson 35989bb1cefSJeff Roberson p = td->td_proc; 3605f73a7ebSBruno Ducrot ksiginfo_init(&ksi); 3615f73a7ebSBruno Ducrot ksi.ksi_signo = uap->sig; 362baf28b69SDavid Xu ksi.ksi_code = SI_LWP; 3635f73a7ebSBruno Ducrot ksi.ksi_pid = p->p_pid; 3645f73a7ebSBruno Ducrot ksi.ksi_uid = td->td_ucred->cr_ruid; 3650a5cd498SDavid Xu if (uap->id == -1) { 3660a5cd498SDavid Xu if (uap->sig != 0 && !_SIG_VALID(uap->sig)) { 36789bb1cefSJeff Roberson error = EINVAL; 3680a5cd498SDavid Xu } else { 3690a5cd498SDavid Xu error = ESRCH; 370cf7d9a8cSDavid Xu PROC_LOCK(p); 3710a5cd498SDavid Xu FOREACH_THREAD_IN_PROC(p, ttd) { 3720a5cd498SDavid Xu if (ttd != td) { 3730a5cd498SDavid Xu error = 0; 3740a5cd498SDavid Xu if (uap->sig == 0) 3750a5cd498SDavid Xu break; 376ad6eec7bSJohn Baldwin tdksignal(ttd, uap->sig, &ksi); 3770a5cd498SDavid Xu } 3780a5cd498SDavid Xu } 379cf7d9a8cSDavid Xu PROC_UNLOCK(p); 3800a5cd498SDavid Xu } 3810a5cd498SDavid Xu } else { 382cf7d9a8cSDavid Xu error = 0; 383cf7d9a8cSDavid Xu ttd = tdfind((lwpid_t)uap->id, p->p_pid); 3840a5cd498SDavid Xu if (ttd == NULL) 385cf7d9a8cSDavid Xu return (ESRCH); 386cf7d9a8cSDavid Xu if (uap->sig == 0) 3870a5cd498SDavid Xu ; 3880a5cd498SDavid Xu else if (!_SIG_VALID(uap->sig)) 3890a5cd498SDavid Xu error = EINVAL; 3900a5cd498SDavid Xu else 391ad6eec7bSJohn Baldwin tdksignal(ttd, uap->sig, &ksi); 392cf7d9a8cSDavid Xu PROC_UNLOCK(ttd->td_proc); 3930a5cd498SDavid Xu } 39489bb1cefSJeff Roberson return (error); 39589bb1cefSJeff Roberson } 3961713a516SMike Makonnen 3971713a516SMike Makonnen int 3988451d0ddSKip Macy sys_thr_kill2(struct thread *td, struct thr_kill2_args *uap) 3990b1f0611SDavid Xu /* pid_t pid, long id, int sig */ 4000b1f0611SDavid Xu { 4015f73a7ebSBruno Ducrot ksiginfo_t ksi; 4020b1f0611SDavid Xu struct thread *ttd; 4030b1f0611SDavid Xu struct proc *p; 4040b1f0611SDavid Xu int error; 4050b1f0611SDavid Xu 40614961ba7SRobert Watson AUDIT_ARG_SIGNUM(uap->sig); 4070b1f0611SDavid Xu 4085f73a7ebSBruno Ducrot ksiginfo_init(&ksi); 4095f73a7ebSBruno Ducrot ksi.ksi_signo = uap->sig; 410baf28b69SDavid Xu ksi.ksi_code = SI_LWP; 4115f73a7ebSBruno Ducrot ksi.ksi_pid = td->td_proc->p_pid; 4125f73a7ebSBruno Ducrot ksi.ksi_uid = td->td_ucred->cr_ruid; 4130b1f0611SDavid Xu if (uap->id == -1) { 414cf7d9a8cSDavid Xu if ((p = pfind(uap->pid)) == NULL) 415cf7d9a8cSDavid Xu return (ESRCH); 416cf7d9a8cSDavid Xu AUDIT_ARG_PROCESS(p); 417cf7d9a8cSDavid Xu error = p_cansignal(td, p, uap->sig); 418cf7d9a8cSDavid Xu if (error) { 419cf7d9a8cSDavid Xu PROC_UNLOCK(p); 420cf7d9a8cSDavid Xu return (error); 421cf7d9a8cSDavid Xu } 4220b1f0611SDavid Xu if (uap->sig != 0 && !_SIG_VALID(uap->sig)) { 4230b1f0611SDavid Xu error = EINVAL; 4240b1f0611SDavid Xu } else { 4250b1f0611SDavid Xu error = ESRCH; 4260b1f0611SDavid Xu FOREACH_THREAD_IN_PROC(p, ttd) { 4270b1f0611SDavid Xu if (ttd != td) { 4280b1f0611SDavid Xu error = 0; 4290b1f0611SDavid Xu if (uap->sig == 0) 4300b1f0611SDavid Xu break; 431ad6eec7bSJohn Baldwin tdksignal(ttd, uap->sig, &ksi); 4320b1f0611SDavid Xu } 4330b1f0611SDavid Xu } 4340b1f0611SDavid Xu } 435cf7d9a8cSDavid Xu PROC_UNLOCK(p); 4360b1f0611SDavid Xu } else { 437cf7d9a8cSDavid Xu ttd = tdfind((lwpid_t)uap->id, uap->pid); 4380b1f0611SDavid Xu if (ttd == NULL) 439cf7d9a8cSDavid Xu return (ESRCH); 440cf7d9a8cSDavid Xu p = ttd->td_proc; 441cf7d9a8cSDavid Xu AUDIT_ARG_PROCESS(p); 442cf7d9a8cSDavid Xu error = p_cansignal(td, p, uap->sig); 443cf7d9a8cSDavid Xu if (uap->sig == 0) 4440b1f0611SDavid Xu ; 4450b1f0611SDavid Xu else if (!_SIG_VALID(uap->sig)) 4460b1f0611SDavid Xu error = EINVAL; 4470b1f0611SDavid Xu else 448ad6eec7bSJohn Baldwin tdksignal(ttd, uap->sig, &ksi); 4490b1f0611SDavid Xu PROC_UNLOCK(p); 450cf7d9a8cSDavid Xu } 4510b1f0611SDavid Xu return (error); 4520b1f0611SDavid Xu } 4530b1f0611SDavid Xu 4540b1f0611SDavid Xu int 4558451d0ddSKip Macy sys_thr_suspend(struct thread *td, struct thr_suspend_args *uap) 4561713a516SMike Makonnen /* const struct timespec *timeout */ 4571713a516SMike Makonnen { 458cda9a0d1SDavid Xu struct timespec ts, *tsp; 4591713a516SMike Makonnen int error; 4601713a516SMike Makonnen 461cda9a0d1SDavid Xu tsp = NULL; 4621713a516SMike Makonnen if (uap->timeout != NULL) { 4639a1d0cf6SPeter Holm error = umtx_copyin_timeout(uap->timeout, &ts); 4641713a516SMike Makonnen if (error != 0) 4651713a516SMike Makonnen return (error); 466cda9a0d1SDavid Xu tsp = &ts; 467cda9a0d1SDavid Xu } 468cda9a0d1SDavid Xu 469cda9a0d1SDavid Xu return (kern_thr_suspend(td, tsp)); 470cda9a0d1SDavid Xu } 471cda9a0d1SDavid Xu 472cda9a0d1SDavid Xu int 473cda9a0d1SDavid Xu kern_thr_suspend(struct thread *td, struct timespec *tsp) 474cda9a0d1SDavid Xu { 475cfca8a18SDavid Xu struct proc *p = td->td_proc; 476cda9a0d1SDavid Xu struct timeval tv; 4772961a782SDavid Xu int error = 0; 4782961a782SDavid Xu int timo = 0; 479cda9a0d1SDavid Xu 480745fbd3aSDavid Xu if (td->td_pflags & TDP_WAKEUP) { 481745fbd3aSDavid Xu td->td_pflags &= ~TDP_WAKEUP; 482745fbd3aSDavid Xu return (0); 483745fbd3aSDavid Xu } 484745fbd3aSDavid Xu 485cfca8a18SDavid Xu if (tsp != NULL) { 4862961a782SDavid Xu if (tsp->tv_sec == 0 && tsp->tv_nsec == 0) 4872961a782SDavid Xu error = EWOULDBLOCK; 4882961a782SDavid Xu else { 4892961a782SDavid Xu TIMESPEC_TO_TIMEVAL(&tv, tsp); 4902961a782SDavid Xu timo = tvtohz(&tv); 491cfca8a18SDavid Xu } 492cfca8a18SDavid Xu } 493cfca8a18SDavid Xu 494cfca8a18SDavid Xu PROC_LOCK(p); 495cfca8a18SDavid Xu if (error == 0 && (td->td_flags & TDF_THRWAKEUP) == 0) 496cfca8a18SDavid Xu error = msleep((void *)td, &p->p_mtx, 4972961a782SDavid Xu PCATCH, "lthr", timo); 4982961a782SDavid Xu 499c1df5a1aSDavid Xu if (td->td_flags & TDF_THRWAKEUP) { 500982d11f8SJeff Roberson thread_lock(td); 5011713a516SMike Makonnen td->td_flags &= ~TDF_THRWAKEUP; 502982d11f8SJeff Roberson thread_unlock(td); 503cfca8a18SDavid Xu PROC_UNLOCK(p); 504c1df5a1aSDavid Xu return (0); 505c1df5a1aSDavid Xu } 506cfca8a18SDavid Xu PROC_UNLOCK(p); 507c1df5a1aSDavid Xu if (error == EWOULDBLOCK) 508c1df5a1aSDavid Xu error = ETIMEDOUT; 509c1df5a1aSDavid Xu else if (error == ERESTART) { 5102961a782SDavid Xu if (timo != 0) 511c1df5a1aSDavid Xu error = EINTR; 512c1df5a1aSDavid Xu } 513c1df5a1aSDavid Xu return (error); 5141713a516SMike Makonnen } 5151713a516SMike Makonnen 5161713a516SMike Makonnen int 5178451d0ddSKip Macy sys_thr_wake(struct thread *td, struct thr_wake_args *uap) 518cd28f17dSMarcel Moolenaar /* long id */ 5191713a516SMike Makonnen { 52044355392SDavid Xu struct proc *p; 521cd28f17dSMarcel Moolenaar struct thread *ttd; 5221713a516SMike Makonnen 523745fbd3aSDavid Xu if (uap->id == td->td_tid) { 524745fbd3aSDavid Xu td->td_pflags |= TDP_WAKEUP; 525745fbd3aSDavid Xu return (0); 526745fbd3aSDavid Xu } 527745fbd3aSDavid Xu 52844355392SDavid Xu p = td->td_proc; 529cf7d9a8cSDavid Xu ttd = tdfind((lwpid_t)uap->id, p->p_pid); 530cf7d9a8cSDavid Xu if (ttd == NULL) 5311713a516SMike Makonnen return (ESRCH); 532982d11f8SJeff Roberson thread_lock(ttd); 533cd28f17dSMarcel Moolenaar ttd->td_flags |= TDF_THRWAKEUP; 534982d11f8SJeff Roberson thread_unlock(ttd); 535c1df5a1aSDavid Xu wakeup((void *)ttd); 53644355392SDavid Xu PROC_UNLOCK(p); 5371713a516SMike Makonnen return (0); 5381713a516SMike Makonnen } 5399e7d7224SDavid Xu 5409e7d7224SDavid Xu int 5418451d0ddSKip Macy sys_thr_set_name(struct thread *td, struct thr_set_name_args *uap) 5429e7d7224SDavid Xu { 543cf7d9a8cSDavid Xu struct proc *p; 5449e7d7224SDavid Xu char name[MAXCOMLEN + 1]; 5459e7d7224SDavid Xu struct thread *ttd; 5469e7d7224SDavid Xu int error; 5479e7d7224SDavid Xu 5489e7d7224SDavid Xu error = 0; 5499e7d7224SDavid Xu name[0] = '\0'; 5509e7d7224SDavid Xu if (uap->name != NULL) { 5519e7d7224SDavid Xu error = copyinstr(uap->name, name, sizeof(name), 5529e7d7224SDavid Xu NULL); 5539e7d7224SDavid Xu if (error) 5549e7d7224SDavid Xu return (error); 5559e7d7224SDavid Xu } 556cf7d9a8cSDavid Xu p = td->td_proc; 557cf7d9a8cSDavid Xu ttd = tdfind((lwpid_t)uap->id, p->p_pid); 558cf7d9a8cSDavid Xu if (ttd == NULL) 559cf7d9a8cSDavid Xu return (ESRCH); 5609e7d7224SDavid Xu strcpy(ttd->td_name, name); 56144ad5475SJohn Baldwin #ifdef KTR 56244ad5475SJohn Baldwin sched_clear_tdname(ttd); 56344ad5475SJohn Baldwin #endif 5649e7d7224SDavid Xu PROC_UNLOCK(p); 5659e7d7224SDavid Xu return (error); 5669e7d7224SDavid Xu } 56709baafb4SDmitry Chagin 56809baafb4SDmitry Chagin int 56909baafb4SDmitry Chagin kern_thr_alloc(struct proc *p, int pages, struct thread **ntd) 57009baafb4SDmitry Chagin { 57109baafb4SDmitry Chagin 57209baafb4SDmitry Chagin /* Have race condition but it is cheap. */ 57309baafb4SDmitry Chagin if (p->p_numthreads >= max_threads_per_proc) { 57409baafb4SDmitry Chagin ++max_threads_hits; 57509baafb4SDmitry Chagin return (EPROCLIM); 57609baafb4SDmitry Chagin } 57709baafb4SDmitry Chagin 57809baafb4SDmitry Chagin *ntd = thread_alloc(pages); 57909baafb4SDmitry Chagin if (*ntd == NULL) 58009baafb4SDmitry Chagin return (ENOMEM); 58109baafb4SDmitry Chagin 58209baafb4SDmitry Chagin return (0); 58309baafb4SDmitry Chagin } 584