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 6125a9cfc9SKonstantin Belousov SYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW, 0, "thread allocation"); 6225a9cfc9SKonstantin Belousov 6325a9cfc9SKonstantin Belousov static int max_threads_per_proc = 1500; 6425a9cfc9SKonstantin Belousov SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_per_proc, CTLFLAG_RW, 6525a9cfc9SKonstantin Belousov &max_threads_per_proc, 0, "Limit on threads per proc"); 6625a9cfc9SKonstantin Belousov 6725a9cfc9SKonstantin Belousov static int max_threads_hits; 6825a9cfc9SKonstantin Belousov SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_hits, CTLFLAG_RD, 6925a9cfc9SKonstantin Belousov &max_threads_hits, 0, ""); 7025a9cfc9SKonstantin Belousov 71841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32 72cda9a0d1SDavid Xu 73cda9a0d1SDavid Xu static inline int 74cda9a0d1SDavid Xu suword_lwpid(void *addr, lwpid_t lwpid) 75cda9a0d1SDavid Xu { 76cda9a0d1SDavid Xu int error; 77cda9a0d1SDavid Xu 78b4cf0e62SKonstantin Belousov if (SV_CURPROC_FLAG(SV_LP64)) 79cda9a0d1SDavid Xu error = suword(addr, lwpid); 80cda9a0d1SDavid Xu else 81cda9a0d1SDavid Xu error = suword32(addr, lwpid); 82cda9a0d1SDavid Xu return (error); 83cda9a0d1SDavid Xu } 84cda9a0d1SDavid Xu 85cda9a0d1SDavid Xu #else 86cda9a0d1SDavid Xu #define suword_lwpid suword 87cda9a0d1SDavid Xu #endif 88cda9a0d1SDavid Xu 89c4bd610fSDavid Xu static int create_thread(struct thread *td, mcontext_t *ctx, 90c4bd610fSDavid Xu void (*start_func)(void *), void *arg, 91c4bd610fSDavid Xu char *stack_base, size_t stack_size, 92c4bd610fSDavid Xu char *tls_base, 93c4bd610fSDavid Xu long *child_tid, long *parent_tid, 9473fa3e5bSDavid Xu int flags, struct rtprio *rtp); 95c4bd610fSDavid Xu 9689bb1cefSJeff Roberson /* 9789bb1cefSJeff Roberson * System call interface. 9889bb1cefSJeff Roberson */ 9989bb1cefSJeff Roberson int 10089bb1cefSJeff Roberson thr_create(struct thread *td, struct thr_create_args *uap) 101cd28f17dSMarcel Moolenaar /* ucontext_t *ctx, long *id, int flags */ 10289bb1cefSJeff Roberson { 10389bb1cefSJeff Roberson ucontext_t ctx; 10489bb1cefSJeff Roberson int error; 10589bb1cefSJeff Roberson 10689bb1cefSJeff Roberson if ((error = copyin(uap->ctx, &ctx, sizeof(ctx)))) 10789bb1cefSJeff Roberson return (error); 10889bb1cefSJeff Roberson 109c4bd610fSDavid Xu error = create_thread(td, &ctx.uc_mcontext, NULL, NULL, 110a0712c99SDavid Xu NULL, 0, NULL, uap->id, NULL, uap->flags, NULL); 111c4bd610fSDavid Xu return (error); 112c4bd610fSDavid Xu } 113c4bd610fSDavid Xu 114c4bd610fSDavid Xu int 115c4bd610fSDavid Xu thr_new(struct thread *td, struct thr_new_args *uap) 116c4bd610fSDavid Xu /* struct thr_param * */ 117c4bd610fSDavid Xu { 118c4bd610fSDavid Xu struct thr_param param; 119cda9a0d1SDavid Xu int error; 120cda9a0d1SDavid Xu 121cda9a0d1SDavid Xu if (uap->param_size < 0 || uap->param_size > sizeof(param)) 122cda9a0d1SDavid Xu return (EINVAL); 123cda9a0d1SDavid Xu bzero(¶m, sizeof(param)); 124cda9a0d1SDavid Xu if ((error = copyin(uap->param, ¶m, uap->param_size))) 125cda9a0d1SDavid Xu return (error); 126cda9a0d1SDavid Xu return (kern_thr_new(td, ¶m)); 127cda9a0d1SDavid Xu } 128cda9a0d1SDavid Xu 129cda9a0d1SDavid Xu int 130cda9a0d1SDavid Xu kern_thr_new(struct thread *td, struct thr_param *param) 131cda9a0d1SDavid Xu { 13273fa3e5bSDavid Xu struct rtprio rtp, *rtpp; 133c4bd610fSDavid Xu int error; 134c4bd610fSDavid Xu 13573fa3e5bSDavid Xu rtpp = NULL; 136cda9a0d1SDavid Xu if (param->rtp != 0) { 137cda9a0d1SDavid Xu error = copyin(param->rtp, &rtp, sizeof(struct rtprio)); 138e4866772SRoman Divacky if (error) 139e4866772SRoman Divacky return (error); 14073fa3e5bSDavid Xu rtpp = &rtp; 141a0712c99SDavid Xu } 142cda9a0d1SDavid Xu error = create_thread(td, NULL, param->start_func, param->arg, 143cda9a0d1SDavid Xu param->stack_base, param->stack_size, param->tls_base, 144cda9a0d1SDavid Xu param->child_tid, param->parent_tid, param->flags, 14573fa3e5bSDavid Xu rtpp); 146c4bd610fSDavid Xu return (error); 147c4bd610fSDavid Xu } 148c4bd610fSDavid Xu 149c4bd610fSDavid Xu static int 150c4bd610fSDavid Xu create_thread(struct thread *td, mcontext_t *ctx, 151c4bd610fSDavid Xu void (*start_func)(void *), void *arg, 152c4bd610fSDavid Xu char *stack_base, size_t stack_size, 153c4bd610fSDavid Xu char *tls_base, 154c4bd610fSDavid Xu long *child_tid, long *parent_tid, 15573fa3e5bSDavid Xu int flags, struct rtprio *rtp) 156c4bd610fSDavid Xu { 157c4bd610fSDavid Xu stack_t stack; 158c4bd610fSDavid Xu struct thread *newtd; 159c4bd610fSDavid Xu struct proc *p; 160adc9c950SDavid Xu int error; 161c4bd610fSDavid Xu 162c4bd610fSDavid Xu p = td->td_proc; 163c4bd610fSDavid Xu 164c4bd610fSDavid Xu /* Have race condition but it is cheap. */ 165a328d535SJohn Baldwin if (p->p_numthreads >= max_threads_per_proc) { 16694ec9c02SDavid Xu ++max_threads_hits; 167ed062c8dSJulian Elischer return (EPROCLIM); 16894ec9c02SDavid Xu } 169c4bd610fSDavid Xu 17073fa3e5bSDavid Xu if (rtp != NULL) { 17173fa3e5bSDavid Xu switch(rtp->type) { 17273fa3e5bSDavid Xu case RTP_PRIO_REALTIME: 17373fa3e5bSDavid Xu case RTP_PRIO_FIFO: 174a0712c99SDavid Xu /* Only root can set scheduler policy */ 175acd3428bSRobert Watson if (priv_check(td, PRIV_SCHED_SETPOLICY) != 0) 176a0712c99SDavid Xu return (EPERM); 17773fa3e5bSDavid Xu if (rtp->prio > RTP_PRIO_MAX) 178a0712c99SDavid Xu return (EINVAL); 1792dca4ca7SDavid Xu break; 18073fa3e5bSDavid Xu case RTP_PRIO_NORMAL: 18173fa3e5bSDavid Xu rtp->prio = 0; 1822dca4ca7SDavid Xu break; 1832dca4ca7SDavid Xu default: 1842dca4ca7SDavid Xu return (EINVAL); 185a0712c99SDavid Xu } 186a0712c99SDavid Xu } 187a0712c99SDavid Xu 188*afcc55f3SEdward Tomasz Napierala #ifdef RACCT 18958c77a9dSEdward Tomasz Napierala PROC_LOCK(td->td_proc); 19058c77a9dSEdward Tomasz Napierala error = racct_add(p, RACCT_NTHR, 1); 19158c77a9dSEdward Tomasz Napierala PROC_UNLOCK(td->td_proc); 19258c77a9dSEdward Tomasz Napierala if (error != 0) 19358c77a9dSEdward Tomasz Napierala return (EPROCLIM); 194*afcc55f3SEdward Tomasz Napierala #endif 19558c77a9dSEdward Tomasz Napierala 196ad1e7d28SJulian Elischer /* Initialize our td */ 1978a945d10SKonstantin Belousov newtd = thread_alloc(0); 19858c77a9dSEdward Tomasz Napierala if (newtd == NULL) { 19958c77a9dSEdward Tomasz Napierala error = ENOMEM; 20058c77a9dSEdward Tomasz Napierala goto fail; 20158c77a9dSEdward Tomasz Napierala } 202c4bd610fSDavid Xu 20389bb1cefSJeff Roberson /* 204c4bd610fSDavid Xu * Try the copyout as soon as we allocate the td so we don't 205c4bd610fSDavid Xu * have to tear things down in a failure case below. 206c4bd610fSDavid Xu * Here we copy out tid to two places, one for child and one 207c4bd610fSDavid Xu * for parent, because pthread can create a detached thread, 208c4bd610fSDavid Xu * if parent wants to safely access child tid, it has to provide 209c4bd610fSDavid Xu * its storage, because child thread may exit quickly and 210c4bd610fSDavid Xu * memory is freed before parent thread can access it. 21189bb1cefSJeff Roberson */ 212c4bd610fSDavid Xu if ((child_tid != NULL && 213cda9a0d1SDavid Xu suword_lwpid(child_tid, newtd->td_tid)) || 214c4bd610fSDavid Xu (parent_tid != NULL && 215cda9a0d1SDavid Xu suword_lwpid(parent_tid, newtd->td_tid))) { 216ed062c8dSJulian Elischer thread_free(newtd); 21758c77a9dSEdward Tomasz Napierala error = EFAULT; 21858c77a9dSEdward Tomasz Napierala goto fail; 21989bb1cefSJeff Roberson } 220cda9a0d1SDavid Xu 221ed062c8dSJulian Elischer bzero(&newtd->td_startzero, 2226db36923SDavid Schultz __rangeof(struct thread, td_startzero, td_endzero)); 223ed062c8dSJulian Elischer bcopy(&td->td_startcopy, &newtd->td_startcopy, 2246db36923SDavid Schultz __rangeof(struct thread, td_startcopy, td_endcopy)); 225c4bd610fSDavid Xu newtd->td_proc = td->td_proc; 226c4bd610fSDavid Xu newtd->td_ucred = crhold(td->td_ucred); 22789bb1cefSJeff Roberson 228c4bd610fSDavid Xu cpu_set_upcall(newtd, td); 229c4bd610fSDavid Xu 230c4bd610fSDavid Xu if (ctx != NULL) { /* old way to set user context */ 231c4bd610fSDavid Xu error = set_mcontext(newtd, ctx); 232c4bd610fSDavid Xu if (error != 0) { 233c4bd610fSDavid Xu thread_free(newtd); 234c4bd610fSDavid Xu crfree(td->td_ucred); 23558c77a9dSEdward Tomasz Napierala goto fail; 236c4bd610fSDavid Xu } 237c4bd610fSDavid Xu } else { 238c4bd610fSDavid Xu /* Set up our machine context. */ 239c4bd610fSDavid Xu stack.ss_sp = stack_base; 240c4bd610fSDavid Xu stack.ss_size = stack_size; 241c4bd610fSDavid Xu /* Set upcall address to user thread entry function. */ 242c4bd610fSDavid Xu cpu_set_upcall_kse(newtd, start_func, arg, &stack); 243c4bd610fSDavid Xu /* Setup user TLS address and TLS pointer register. */ 244740fd64dSDavid Xu error = cpu_set_user_tls(newtd, tls_base); 245740fd64dSDavid Xu if (error != 0) { 246740fd64dSDavid Xu thread_free(newtd); 247740fd64dSDavid Xu crfree(td->td_ucred); 24858c77a9dSEdward Tomasz Napierala goto fail; 249740fd64dSDavid Xu } 250c4bd610fSDavid Xu } 251c4bd610fSDavid Xu 2528460a577SJohn Birrell PROC_LOCK(td->td_proc); 2538460a577SJohn Birrell td->td_proc->p_flag |= P_HADTHREADS; 2548460a577SJohn Birrell newtd->td_sigmask = td->td_sigmask; 2558460a577SJohn Birrell thread_link(newtd, p); 256c67ddc21SJulian Elischer bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name)); 257982d11f8SJeff Roberson thread_lock(td); 258ed062c8dSJulian Elischer /* let the scheduler know about these things. */ 259ed062c8dSJulian Elischer sched_fork_thread(td, newtd); 260982d11f8SJeff Roberson thread_unlock(td); 261b7edba77SJeff Roberson if (P_SHOULDSTOP(p)) 262b7edba77SJeff Roberson newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK; 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: 282*afcc55f3SEdward Tomasz Napierala #ifdef RACCT 28358c77a9dSEdward Tomasz Napierala PROC_LOCK(p); 28458c77a9dSEdward Tomasz Napierala racct_sub(p, RACCT_NTHR, 1); 28558c77a9dSEdward Tomasz Napierala PROC_UNLOCK(p); 286*afcc55f3SEdward Tomasz Napierala #endif 28758c77a9dSEdward Tomasz Napierala return (error); 28889bb1cefSJeff Roberson } 28989bb1cefSJeff Roberson 29089bb1cefSJeff Roberson int 29189bb1cefSJeff Roberson thr_self(struct thread *td, struct thr_self_args *uap) 292cd28f17dSMarcel Moolenaar /* long *id */ 29389bb1cefSJeff Roberson { 29489bb1cefSJeff Roberson int error; 29589bb1cefSJeff Roberson 296cda9a0d1SDavid Xu error = suword_lwpid(uap->id, (unsigned)td->td_tid); 297cda9a0d1SDavid Xu if (error == -1) 298cda9a0d1SDavid Xu return (EFAULT); 29989bb1cefSJeff Roberson return (0); 30089bb1cefSJeff Roberson } 30189bb1cefSJeff Roberson 30289bb1cefSJeff Roberson int 30389bb1cefSJeff Roberson thr_exit(struct thread *td, struct thr_exit_args *uap) 304401901acSMike Makonnen /* long *state */ 30589bb1cefSJeff Roberson { 30689bb1cefSJeff Roberson struct proc *p; 30789bb1cefSJeff Roberson 30889bb1cefSJeff Roberson p = td->td_proc; 30989bb1cefSJeff Roberson 310401901acSMike Makonnen /* Signal userland that it can free the stack. */ 3114938faa6SDavid Xu if ((void *)uap->state != NULL) { 312cda9a0d1SDavid Xu suword_lwpid(uap->state, 1); 3133bba58f2SDavid Xu kern_umtx_wake(td, uap->state, INT_MAX, 0); 3144938faa6SDavid Xu } 315401901acSMike Makonnen 3160d036d55SDavid Xu rw_wlock(&tidhash_lock); 31758c77a9dSEdward Tomasz Napierala 31889bb1cefSJeff Roberson PROC_LOCK(p); 31958c77a9dSEdward Tomasz Napierala racct_sub(p, RACCT_NTHR, 1); 32058c77a9dSEdward Tomasz Napierala 32189bb1cefSJeff Roberson /* 322ed062c8dSJulian Elischer * Shutting down last thread in the proc. This will actually 323ed062c8dSJulian Elischer * call exit() in the trampoline when it returns. 32489bb1cefSJeff Roberson */ 325ed062c8dSJulian Elischer if (p->p_numthreads != 1) { 3260d036d55SDavid Xu LIST_REMOVE(td, td_hash); 3270d036d55SDavid Xu rw_wunlock(&tidhash_lock); 3280d036d55SDavid Xu tdsigcleanup(td); 3290d036d55SDavid Xu PROC_SLOCK(p); 33071b7afb2SDavid Xu thread_stopped(p); 331ed062c8dSJulian Elischer thread_exit(); 332ed062c8dSJulian Elischer /* NOTREACHED */ 333ed062c8dSJulian Elischer } 334ed062c8dSJulian Elischer PROC_UNLOCK(p); 3350d036d55SDavid Xu rw_wunlock(&tidhash_lock); 33689bb1cefSJeff Roberson return (0); 33789bb1cefSJeff Roberson } 33889bb1cefSJeff Roberson 33989bb1cefSJeff Roberson int 34089bb1cefSJeff Roberson thr_kill(struct thread *td, struct thr_kill_args *uap) 341cd28f17dSMarcel Moolenaar /* long id, int sig */ 34289bb1cefSJeff Roberson { 3435f73a7ebSBruno Ducrot ksiginfo_t ksi; 34489bb1cefSJeff Roberson struct thread *ttd; 34589bb1cefSJeff Roberson struct proc *p; 34689bb1cefSJeff Roberson int error; 34789bb1cefSJeff Roberson 34889bb1cefSJeff Roberson p = td->td_proc; 3495f73a7ebSBruno Ducrot ksiginfo_init(&ksi); 3505f73a7ebSBruno Ducrot ksi.ksi_signo = uap->sig; 351baf28b69SDavid Xu ksi.ksi_code = SI_LWP; 3525f73a7ebSBruno Ducrot ksi.ksi_pid = p->p_pid; 3535f73a7ebSBruno Ducrot ksi.ksi_uid = td->td_ucred->cr_ruid; 3540a5cd498SDavid Xu if (uap->id == -1) { 3550a5cd498SDavid Xu if (uap->sig != 0 && !_SIG_VALID(uap->sig)) { 35689bb1cefSJeff Roberson error = EINVAL; 3570a5cd498SDavid Xu } else { 3580a5cd498SDavid Xu error = ESRCH; 359cf7d9a8cSDavid Xu PROC_LOCK(p); 3600a5cd498SDavid Xu FOREACH_THREAD_IN_PROC(p, ttd) { 3610a5cd498SDavid Xu if (ttd != td) { 3620a5cd498SDavid Xu error = 0; 3630a5cd498SDavid Xu if (uap->sig == 0) 3640a5cd498SDavid Xu break; 365ad6eec7bSJohn Baldwin tdksignal(ttd, uap->sig, &ksi); 3660a5cd498SDavid Xu } 3670a5cd498SDavid Xu } 368cf7d9a8cSDavid Xu PROC_UNLOCK(p); 3690a5cd498SDavid Xu } 3700a5cd498SDavid Xu } else { 371cf7d9a8cSDavid Xu error = 0; 372cf7d9a8cSDavid Xu ttd = tdfind((lwpid_t)uap->id, p->p_pid); 3730a5cd498SDavid Xu if (ttd == NULL) 374cf7d9a8cSDavid Xu return (ESRCH); 375cf7d9a8cSDavid Xu if (uap->sig == 0) 3760a5cd498SDavid Xu ; 3770a5cd498SDavid Xu else if (!_SIG_VALID(uap->sig)) 3780a5cd498SDavid Xu error = EINVAL; 3790a5cd498SDavid Xu else 380ad6eec7bSJohn Baldwin tdksignal(ttd, uap->sig, &ksi); 381cf7d9a8cSDavid Xu PROC_UNLOCK(ttd->td_proc); 3820a5cd498SDavid Xu } 38389bb1cefSJeff Roberson return (error); 38489bb1cefSJeff Roberson } 3851713a516SMike Makonnen 3861713a516SMike Makonnen int 3870b1f0611SDavid Xu thr_kill2(struct thread *td, struct thr_kill2_args *uap) 3880b1f0611SDavid Xu /* pid_t pid, long id, int sig */ 3890b1f0611SDavid Xu { 3905f73a7ebSBruno Ducrot ksiginfo_t ksi; 3910b1f0611SDavid Xu struct thread *ttd; 3920b1f0611SDavid Xu struct proc *p; 3930b1f0611SDavid Xu int error; 3940b1f0611SDavid Xu 39514961ba7SRobert Watson AUDIT_ARG_SIGNUM(uap->sig); 3960b1f0611SDavid Xu 3975f73a7ebSBruno Ducrot ksiginfo_init(&ksi); 3985f73a7ebSBruno Ducrot ksi.ksi_signo = uap->sig; 399baf28b69SDavid Xu ksi.ksi_code = SI_LWP; 4005f73a7ebSBruno Ducrot ksi.ksi_pid = td->td_proc->p_pid; 4015f73a7ebSBruno Ducrot ksi.ksi_uid = td->td_ucred->cr_ruid; 4020b1f0611SDavid Xu if (uap->id == -1) { 403cf7d9a8cSDavid Xu if ((p = pfind(uap->pid)) == NULL) 404cf7d9a8cSDavid Xu return (ESRCH); 405cf7d9a8cSDavid Xu AUDIT_ARG_PROCESS(p); 406cf7d9a8cSDavid Xu error = p_cansignal(td, p, uap->sig); 407cf7d9a8cSDavid Xu if (error) { 408cf7d9a8cSDavid Xu PROC_UNLOCK(p); 409cf7d9a8cSDavid Xu return (error); 410cf7d9a8cSDavid Xu } 4110b1f0611SDavid Xu if (uap->sig != 0 && !_SIG_VALID(uap->sig)) { 4120b1f0611SDavid Xu error = EINVAL; 4130b1f0611SDavid Xu } else { 4140b1f0611SDavid Xu error = ESRCH; 4150b1f0611SDavid Xu FOREACH_THREAD_IN_PROC(p, ttd) { 4160b1f0611SDavid Xu if (ttd != td) { 4170b1f0611SDavid Xu error = 0; 4180b1f0611SDavid Xu if (uap->sig == 0) 4190b1f0611SDavid Xu break; 420ad6eec7bSJohn Baldwin tdksignal(ttd, uap->sig, &ksi); 4210b1f0611SDavid Xu } 4220b1f0611SDavid Xu } 4230b1f0611SDavid Xu } 424cf7d9a8cSDavid Xu PROC_UNLOCK(p); 4250b1f0611SDavid Xu } else { 426cf7d9a8cSDavid Xu ttd = tdfind((lwpid_t)uap->id, uap->pid); 4270b1f0611SDavid Xu if (ttd == NULL) 428cf7d9a8cSDavid Xu return (ESRCH); 429cf7d9a8cSDavid Xu p = ttd->td_proc; 430cf7d9a8cSDavid Xu AUDIT_ARG_PROCESS(p); 431cf7d9a8cSDavid Xu error = p_cansignal(td, p, uap->sig); 432cf7d9a8cSDavid Xu if (uap->sig == 0) 4330b1f0611SDavid Xu ; 4340b1f0611SDavid Xu else if (!_SIG_VALID(uap->sig)) 4350b1f0611SDavid Xu error = EINVAL; 4360b1f0611SDavid Xu else 437ad6eec7bSJohn Baldwin tdksignal(ttd, uap->sig, &ksi); 4380b1f0611SDavid Xu PROC_UNLOCK(p); 439cf7d9a8cSDavid Xu } 4400b1f0611SDavid Xu return (error); 4410b1f0611SDavid Xu } 4420b1f0611SDavid Xu 4430b1f0611SDavid Xu int 4441713a516SMike Makonnen thr_suspend(struct thread *td, struct thr_suspend_args *uap) 4451713a516SMike Makonnen /* const struct timespec *timeout */ 4461713a516SMike Makonnen { 447cda9a0d1SDavid Xu struct timespec ts, *tsp; 4481713a516SMike Makonnen int error; 4491713a516SMike Makonnen 450cda9a0d1SDavid Xu tsp = NULL; 4511713a516SMike Makonnen if (uap->timeout != NULL) { 4521713a516SMike Makonnen error = copyin((const void *)uap->timeout, (void *)&ts, 4531713a516SMike Makonnen sizeof(struct timespec)); 4541713a516SMike Makonnen if (error != 0) 4551713a516SMike Makonnen return (error); 456cda9a0d1SDavid Xu tsp = &ts; 457cda9a0d1SDavid Xu } 458cda9a0d1SDavid Xu 459cda9a0d1SDavid Xu return (kern_thr_suspend(td, tsp)); 460cda9a0d1SDavid Xu } 461cda9a0d1SDavid Xu 462cda9a0d1SDavid Xu int 463cda9a0d1SDavid Xu kern_thr_suspend(struct thread *td, struct timespec *tsp) 464cda9a0d1SDavid Xu { 465cfca8a18SDavid Xu struct proc *p = td->td_proc; 466cda9a0d1SDavid Xu struct timeval tv; 4672961a782SDavid Xu int error = 0; 4682961a782SDavid Xu int timo = 0; 469cda9a0d1SDavid Xu 470745fbd3aSDavid Xu if (td->td_pflags & TDP_WAKEUP) { 471745fbd3aSDavid Xu td->td_pflags &= ~TDP_WAKEUP; 472745fbd3aSDavid Xu return (0); 473745fbd3aSDavid Xu } 474745fbd3aSDavid Xu 475cfca8a18SDavid Xu if (tsp != NULL) { 476cfca8a18SDavid Xu if (tsp->tv_nsec < 0 || tsp->tv_nsec > 1000000000) 477cfca8a18SDavid Xu return (EINVAL); 4782961a782SDavid Xu if (tsp->tv_sec == 0 && tsp->tv_nsec == 0) 4792961a782SDavid Xu error = EWOULDBLOCK; 4802961a782SDavid Xu else { 4812961a782SDavid Xu TIMESPEC_TO_TIMEVAL(&tv, tsp); 4822961a782SDavid Xu timo = tvtohz(&tv); 483cfca8a18SDavid Xu } 484cfca8a18SDavid Xu } 485cfca8a18SDavid Xu 486cfca8a18SDavid Xu PROC_LOCK(p); 487cfca8a18SDavid Xu if (error == 0 && (td->td_flags & TDF_THRWAKEUP) == 0) 488cfca8a18SDavid Xu error = msleep((void *)td, &p->p_mtx, 4892961a782SDavid Xu PCATCH, "lthr", timo); 4902961a782SDavid Xu 491c1df5a1aSDavid Xu if (td->td_flags & TDF_THRWAKEUP) { 492982d11f8SJeff Roberson thread_lock(td); 4931713a516SMike Makonnen td->td_flags &= ~TDF_THRWAKEUP; 494982d11f8SJeff Roberson thread_unlock(td); 495cfca8a18SDavid Xu PROC_UNLOCK(p); 496c1df5a1aSDavid Xu return (0); 497c1df5a1aSDavid Xu } 498cfca8a18SDavid Xu PROC_UNLOCK(p); 499c1df5a1aSDavid Xu if (error == EWOULDBLOCK) 500c1df5a1aSDavid Xu error = ETIMEDOUT; 501c1df5a1aSDavid Xu else if (error == ERESTART) { 5022961a782SDavid Xu if (timo != 0) 503c1df5a1aSDavid Xu error = EINTR; 504c1df5a1aSDavid Xu } 505c1df5a1aSDavid Xu return (error); 5061713a516SMike Makonnen } 5071713a516SMike Makonnen 5081713a516SMike Makonnen int 5091713a516SMike Makonnen thr_wake(struct thread *td, struct thr_wake_args *uap) 510cd28f17dSMarcel Moolenaar /* long id */ 5111713a516SMike Makonnen { 51244355392SDavid Xu struct proc *p; 513cd28f17dSMarcel Moolenaar struct thread *ttd; 5141713a516SMike Makonnen 515745fbd3aSDavid Xu if (uap->id == td->td_tid) { 516745fbd3aSDavid Xu td->td_pflags |= TDP_WAKEUP; 517745fbd3aSDavid Xu return (0); 518745fbd3aSDavid Xu } 519745fbd3aSDavid Xu 52044355392SDavid Xu p = td->td_proc; 521cf7d9a8cSDavid Xu ttd = tdfind((lwpid_t)uap->id, p->p_pid); 522cf7d9a8cSDavid Xu if (ttd == NULL) 5231713a516SMike Makonnen return (ESRCH); 524982d11f8SJeff Roberson thread_lock(ttd); 525cd28f17dSMarcel Moolenaar ttd->td_flags |= TDF_THRWAKEUP; 526982d11f8SJeff Roberson thread_unlock(ttd); 527c1df5a1aSDavid Xu wakeup((void *)ttd); 52844355392SDavid Xu PROC_UNLOCK(p); 5291713a516SMike Makonnen return (0); 5301713a516SMike Makonnen } 5319e7d7224SDavid Xu 5329e7d7224SDavid Xu int 5339e7d7224SDavid Xu thr_set_name(struct thread *td, struct thr_set_name_args *uap) 5349e7d7224SDavid Xu { 535cf7d9a8cSDavid Xu struct proc *p; 5369e7d7224SDavid Xu char name[MAXCOMLEN + 1]; 5379e7d7224SDavid Xu struct thread *ttd; 5389e7d7224SDavid Xu int error; 5399e7d7224SDavid Xu 5409e7d7224SDavid Xu error = 0; 5419e7d7224SDavid Xu name[0] = '\0'; 5429e7d7224SDavid Xu if (uap->name != NULL) { 5439e7d7224SDavid Xu error = copyinstr(uap->name, name, sizeof(name), 5449e7d7224SDavid Xu NULL); 5459e7d7224SDavid Xu if (error) 5469e7d7224SDavid Xu return (error); 5479e7d7224SDavid Xu } 548cf7d9a8cSDavid Xu p = td->td_proc; 549cf7d9a8cSDavid Xu ttd = tdfind((lwpid_t)uap->id, p->p_pid); 550cf7d9a8cSDavid Xu if (ttd == NULL) 551cf7d9a8cSDavid Xu return (ESRCH); 5529e7d7224SDavid Xu strcpy(ttd->td_name, name); 5539e7d7224SDavid Xu PROC_UNLOCK(p); 5549e7d7224SDavid Xu return (error); 5559e7d7224SDavid Xu } 556