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> 3989bb1cefSJeff Roberson #include <sys/resourcevar.h> 40a22ec9d8SJeff Roberson #include <sys/sched.h> 41a8b491c1SJulian Elischer #include <sys/sysctl.h> 42ed062c8dSJulian Elischer #include <sys/smp.h> 43a66fde8dSJohn Baldwin #include <sys/syscallsubr.h> 4489bb1cefSJeff Roberson #include <sys/sysent.h> 4589bb1cefSJeff Roberson #include <sys/systm.h> 4689bb1cefSJeff Roberson #include <sys/sysproto.h> 4789bb1cefSJeff Roberson #include <sys/signalvar.h> 4889bb1cefSJeff Roberson #include <sys/ucontext.h> 4989bb1cefSJeff Roberson #include <sys/thr.h> 50a0712c99SDavid Xu #include <sys/rtprio.h> 514938faa6SDavid Xu #include <sys/umtx.h> 524938faa6SDavid Xu #include <sys/limits.h> 5389bb1cefSJeff Roberson 5489bb1cefSJeff Roberson #include <machine/frame.h> 5589bb1cefSJeff Roberson 560b1f0611SDavid Xu #include <security/audit/audit.h> 570b1f0611SDavid Xu 58cda9a0d1SDavid Xu #ifdef COMPAT_IA32 59cda9a0d1SDavid Xu 60cda9a0d1SDavid Xu static inline int 61cda9a0d1SDavid Xu suword_lwpid(void *addr, lwpid_t lwpid) 62cda9a0d1SDavid Xu { 63cda9a0d1SDavid Xu int error; 64cda9a0d1SDavid Xu 65b4cf0e62SKonstantin Belousov if (SV_CURPROC_FLAG(SV_LP64)) 66cda9a0d1SDavid Xu error = suword(addr, lwpid); 67cda9a0d1SDavid Xu else 68cda9a0d1SDavid Xu error = suword32(addr, lwpid); 69cda9a0d1SDavid Xu return (error); 70cda9a0d1SDavid Xu } 71cda9a0d1SDavid Xu 72cda9a0d1SDavid Xu #else 73cda9a0d1SDavid Xu #define suword_lwpid suword 74cda9a0d1SDavid Xu #endif 75cda9a0d1SDavid Xu 76ed062c8dSJulian Elischer extern int max_threads_per_proc; 7794ec9c02SDavid Xu extern int max_threads_hits; 78a8b491c1SJulian Elischer 79c4bd610fSDavid Xu static int create_thread(struct thread *td, mcontext_t *ctx, 80c4bd610fSDavid Xu void (*start_func)(void *), void *arg, 81c4bd610fSDavid Xu char *stack_base, size_t stack_size, 82c4bd610fSDavid Xu char *tls_base, 83c4bd610fSDavid Xu long *child_tid, long *parent_tid, 8473fa3e5bSDavid Xu int flags, struct rtprio *rtp); 85c4bd610fSDavid Xu 8689bb1cefSJeff Roberson /* 8789bb1cefSJeff Roberson * System call interface. 8889bb1cefSJeff Roberson */ 8989bb1cefSJeff Roberson int 9089bb1cefSJeff Roberson thr_create(struct thread *td, struct thr_create_args *uap) 91cd28f17dSMarcel Moolenaar /* ucontext_t *ctx, long *id, int flags */ 9289bb1cefSJeff Roberson { 9389bb1cefSJeff Roberson ucontext_t ctx; 9489bb1cefSJeff Roberson int error; 9589bb1cefSJeff Roberson 9689bb1cefSJeff Roberson if ((error = copyin(uap->ctx, &ctx, sizeof(ctx)))) 9789bb1cefSJeff Roberson return (error); 9889bb1cefSJeff Roberson 99c4bd610fSDavid Xu error = create_thread(td, &ctx.uc_mcontext, NULL, NULL, 100a0712c99SDavid Xu NULL, 0, NULL, uap->id, NULL, uap->flags, NULL); 101c4bd610fSDavid Xu return (error); 102c4bd610fSDavid Xu } 103c4bd610fSDavid Xu 104c4bd610fSDavid Xu int 105c4bd610fSDavid Xu thr_new(struct thread *td, struct thr_new_args *uap) 106c4bd610fSDavid Xu /* struct thr_param * */ 107c4bd610fSDavid Xu { 108c4bd610fSDavid Xu struct thr_param param; 109cda9a0d1SDavid Xu int error; 110cda9a0d1SDavid Xu 111cda9a0d1SDavid Xu if (uap->param_size < 0 || uap->param_size > sizeof(param)) 112cda9a0d1SDavid Xu return (EINVAL); 113cda9a0d1SDavid Xu bzero(¶m, sizeof(param)); 114cda9a0d1SDavid Xu if ((error = copyin(uap->param, ¶m, uap->param_size))) 115cda9a0d1SDavid Xu return (error); 116cda9a0d1SDavid Xu return (kern_thr_new(td, ¶m)); 117cda9a0d1SDavid Xu } 118cda9a0d1SDavid Xu 119cda9a0d1SDavid Xu int 120cda9a0d1SDavid Xu kern_thr_new(struct thread *td, struct thr_param *param) 121cda9a0d1SDavid Xu { 12273fa3e5bSDavid Xu struct rtprio rtp, *rtpp; 123c4bd610fSDavid Xu int error; 124c4bd610fSDavid Xu 12573fa3e5bSDavid Xu rtpp = NULL; 126cda9a0d1SDavid Xu if (param->rtp != 0) { 127cda9a0d1SDavid Xu error = copyin(param->rtp, &rtp, sizeof(struct rtprio)); 128e4866772SRoman Divacky if (error) 129e4866772SRoman Divacky return (error); 13073fa3e5bSDavid Xu rtpp = &rtp; 131a0712c99SDavid Xu } 132cda9a0d1SDavid Xu error = create_thread(td, NULL, param->start_func, param->arg, 133cda9a0d1SDavid Xu param->stack_base, param->stack_size, param->tls_base, 134cda9a0d1SDavid Xu param->child_tid, param->parent_tid, param->flags, 13573fa3e5bSDavid Xu rtpp); 136c4bd610fSDavid Xu return (error); 137c4bd610fSDavid Xu } 138c4bd610fSDavid Xu 139c4bd610fSDavid Xu static int 140c4bd610fSDavid Xu create_thread(struct thread *td, mcontext_t *ctx, 141c4bd610fSDavid Xu void (*start_func)(void *), void *arg, 142c4bd610fSDavid Xu char *stack_base, size_t stack_size, 143c4bd610fSDavid Xu char *tls_base, 144c4bd610fSDavid Xu long *child_tid, long *parent_tid, 14573fa3e5bSDavid Xu int flags, struct rtprio *rtp) 146c4bd610fSDavid Xu { 147c4bd610fSDavid Xu stack_t stack; 148c4bd610fSDavid Xu struct thread *newtd; 149c4bd610fSDavid Xu struct proc *p; 150adc9c950SDavid Xu int error; 151c4bd610fSDavid Xu 152c4bd610fSDavid Xu error = 0; 153c4bd610fSDavid Xu p = td->td_proc; 154c4bd610fSDavid Xu 155c4bd610fSDavid Xu /* Have race condition but it is cheap. */ 15694ec9c02SDavid Xu if (p->p_numthreads >= max_threads_per_proc) { 15794ec9c02SDavid Xu ++max_threads_hits; 158ed062c8dSJulian Elischer return (EPROCLIM); 15994ec9c02SDavid Xu } 160c4bd610fSDavid Xu 16173fa3e5bSDavid Xu if (rtp != NULL) { 16273fa3e5bSDavid Xu switch(rtp->type) { 16373fa3e5bSDavid Xu case RTP_PRIO_REALTIME: 16473fa3e5bSDavid Xu case RTP_PRIO_FIFO: 165a0712c99SDavid Xu /* Only root can set scheduler policy */ 166acd3428bSRobert Watson if (priv_check(td, PRIV_SCHED_SETPOLICY) != 0) 167a0712c99SDavid Xu return (EPERM); 16873fa3e5bSDavid Xu if (rtp->prio > RTP_PRIO_MAX) 169a0712c99SDavid Xu return (EINVAL); 1702dca4ca7SDavid Xu break; 17173fa3e5bSDavid Xu case RTP_PRIO_NORMAL: 17273fa3e5bSDavid Xu rtp->prio = 0; 1732dca4ca7SDavid Xu break; 1742dca4ca7SDavid Xu default: 1752dca4ca7SDavid Xu return (EINVAL); 176a0712c99SDavid Xu } 177a0712c99SDavid Xu } 178a0712c99SDavid Xu 179ad1e7d28SJulian Elischer /* Initialize our td */ 180ed062c8dSJulian Elischer newtd = thread_alloc(); 18189b57fcfSKonstantin Belousov if (newtd == NULL) 18289b57fcfSKonstantin Belousov return (ENOMEM); 183c4bd610fSDavid Xu 18489bb1cefSJeff Roberson /* 185c4bd610fSDavid Xu * Try the copyout as soon as we allocate the td so we don't 186c4bd610fSDavid Xu * have to tear things down in a failure case below. 187c4bd610fSDavid Xu * Here we copy out tid to two places, one for child and one 188c4bd610fSDavid Xu * for parent, because pthread can create a detached thread, 189c4bd610fSDavid Xu * if parent wants to safely access child tid, it has to provide 190c4bd610fSDavid Xu * its storage, because child thread may exit quickly and 191c4bd610fSDavid Xu * memory is freed before parent thread can access it. 19289bb1cefSJeff Roberson */ 193c4bd610fSDavid Xu if ((child_tid != NULL && 194cda9a0d1SDavid Xu suword_lwpid(child_tid, newtd->td_tid)) || 195c4bd610fSDavid Xu (parent_tid != NULL && 196cda9a0d1SDavid Xu suword_lwpid(parent_tid, newtd->td_tid))) { 197ed062c8dSJulian Elischer thread_free(newtd); 198cda9a0d1SDavid Xu return (EFAULT); 19989bb1cefSJeff Roberson } 200cda9a0d1SDavid Xu 201ed062c8dSJulian Elischer bzero(&newtd->td_startzero, 2026db36923SDavid Schultz __rangeof(struct thread, td_startzero, td_endzero)); 203ed062c8dSJulian Elischer bcopy(&td->td_startcopy, &newtd->td_startcopy, 2046db36923SDavid Schultz __rangeof(struct thread, td_startcopy, td_endcopy)); 205c4bd610fSDavid Xu newtd->td_proc = td->td_proc; 206c4bd610fSDavid Xu newtd->td_ucred = crhold(td->td_ucred); 20789bb1cefSJeff Roberson 208c4bd610fSDavid Xu cpu_set_upcall(newtd, td); 209c4bd610fSDavid Xu 210c4bd610fSDavid Xu if (ctx != NULL) { /* old way to set user context */ 211c4bd610fSDavid Xu error = set_mcontext(newtd, ctx); 212c4bd610fSDavid Xu if (error != 0) { 213c4bd610fSDavid Xu thread_free(newtd); 214c4bd610fSDavid Xu crfree(td->td_ucred); 215c4bd610fSDavid Xu return (error); 216c4bd610fSDavid Xu } 217c4bd610fSDavid Xu } else { 218c4bd610fSDavid Xu /* Set up our machine context. */ 219c4bd610fSDavid Xu stack.ss_sp = stack_base; 220c4bd610fSDavid Xu stack.ss_size = stack_size; 221c4bd610fSDavid Xu /* Set upcall address to user thread entry function. */ 222c4bd610fSDavid Xu cpu_set_upcall_kse(newtd, start_func, arg, &stack); 223c4bd610fSDavid Xu /* Setup user TLS address and TLS pointer register. */ 224740fd64dSDavid Xu error = cpu_set_user_tls(newtd, tls_base); 225740fd64dSDavid Xu if (error != 0) { 226740fd64dSDavid Xu thread_free(newtd); 227740fd64dSDavid Xu crfree(td->td_ucred); 228740fd64dSDavid Xu return (error); 229740fd64dSDavid Xu } 230c4bd610fSDavid Xu } 231c4bd610fSDavid Xu 2328460a577SJohn Birrell PROC_LOCK(td->td_proc); 2338460a577SJohn Birrell td->td_proc->p_flag |= P_HADTHREADS; 2348460a577SJohn Birrell newtd->td_sigmask = td->td_sigmask; 2358460a577SJohn Birrell thread_link(newtd, p); 236c67ddc21SJulian Elischer bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name)); 237982d11f8SJeff Roberson thread_lock(td); 238ed062c8dSJulian Elischer /* let the scheduler know about these things. */ 239ed062c8dSJulian Elischer sched_fork_thread(td, newtd); 240982d11f8SJeff Roberson thread_unlock(td); 241b7edba77SJeff Roberson if (P_SHOULDSTOP(p)) 242b7edba77SJeff Roberson newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK; 243982d11f8SJeff Roberson PROC_UNLOCK(p); 244982d11f8SJeff Roberson thread_lock(newtd); 24573fa3e5bSDavid Xu if (rtp != NULL) { 2468460a577SJohn Birrell if (!(td->td_pri_class == PRI_TIMESHARE && 2478460a577SJohn Birrell rtp->type == RTP_PRIO_NORMAL)) { 2488460a577SJohn Birrell rtp_to_pri(rtp, newtd); 2498460a577SJohn Birrell sched_prio(newtd, newtd->td_user_pri); 2508460a577SJohn Birrell } /* ignore timesharing class */ 2518460a577SJohn Birrell } 252ed062c8dSJulian Elischer TD_SET_CAN_RUN(newtd); 253f0393f06SJeff Roberson sched_add(newtd, SRQ_BORING); 254982d11f8SJeff Roberson thread_unlock(newtd); 25589bb1cefSJeff Roberson 25689bb1cefSJeff Roberson return (error); 25789bb1cefSJeff Roberson } 25889bb1cefSJeff Roberson 25989bb1cefSJeff Roberson int 26089bb1cefSJeff Roberson thr_self(struct thread *td, struct thr_self_args *uap) 261cd28f17dSMarcel Moolenaar /* long *id */ 26289bb1cefSJeff Roberson { 26389bb1cefSJeff Roberson int error; 26489bb1cefSJeff Roberson 265cda9a0d1SDavid Xu error = suword_lwpid(uap->id, (unsigned)td->td_tid); 266cda9a0d1SDavid Xu if (error == -1) 267cda9a0d1SDavid Xu return (EFAULT); 26889bb1cefSJeff Roberson return (0); 26989bb1cefSJeff Roberson } 27089bb1cefSJeff Roberson 27189bb1cefSJeff Roberson int 27289bb1cefSJeff Roberson thr_exit(struct thread *td, struct thr_exit_args *uap) 273401901acSMike Makonnen /* long *state */ 27489bb1cefSJeff Roberson { 27589bb1cefSJeff Roberson struct proc *p; 27689bb1cefSJeff Roberson 27789bb1cefSJeff Roberson p = td->td_proc; 27889bb1cefSJeff Roberson 279401901acSMike Makonnen /* Signal userland that it can free the stack. */ 2804938faa6SDavid Xu if ((void *)uap->state != NULL) { 281cda9a0d1SDavid Xu suword_lwpid(uap->state, 1); 2823bba58f2SDavid Xu kern_umtx_wake(td, uap->state, INT_MAX, 0); 2834938faa6SDavid Xu } 284401901acSMike Makonnen 28589bb1cefSJeff Roberson PROC_LOCK(p); 2869104847fSDavid Xu sigqueue_flush(&td->td_sigqueue); 2877b4a950aSDavid Xu PROC_SLOCK(p); 28889bb1cefSJeff Roberson 28989bb1cefSJeff Roberson /* 290ed062c8dSJulian Elischer * Shutting down last thread in the proc. This will actually 291ed062c8dSJulian Elischer * call exit() in the trampoline when it returns. 29289bb1cefSJeff Roberson */ 293ed062c8dSJulian Elischer if (p->p_numthreads != 1) { 29471b7afb2SDavid Xu thread_stopped(p); 295ed062c8dSJulian Elischer thread_exit(); 296ed062c8dSJulian Elischer /* NOTREACHED */ 297ed062c8dSJulian Elischer } 2987b4a950aSDavid Xu PROC_SUNLOCK(p); 299ed062c8dSJulian Elischer PROC_UNLOCK(p); 30089bb1cefSJeff Roberson return (0); 30189bb1cefSJeff Roberson } 30289bb1cefSJeff Roberson 30389bb1cefSJeff Roberson int 30489bb1cefSJeff Roberson thr_kill(struct thread *td, struct thr_kill_args *uap) 305cd28f17dSMarcel Moolenaar /* long id, int sig */ 30689bb1cefSJeff Roberson { 30789bb1cefSJeff Roberson struct thread *ttd; 30889bb1cefSJeff Roberson struct proc *p; 30989bb1cefSJeff Roberson int error; 31089bb1cefSJeff Roberson 31189bb1cefSJeff Roberson p = td->td_proc; 31289bb1cefSJeff Roberson error = 0; 31389bb1cefSJeff Roberson PROC_LOCK(p); 3140a5cd498SDavid Xu if (uap->id == -1) { 3150a5cd498SDavid Xu if (uap->sig != 0 && !_SIG_VALID(uap->sig)) { 31689bb1cefSJeff Roberson error = EINVAL; 3170a5cd498SDavid Xu } else { 3180a5cd498SDavid Xu error = ESRCH; 3190a5cd498SDavid Xu FOREACH_THREAD_IN_PROC(p, ttd) { 3200a5cd498SDavid Xu if (ttd != td) { 3210a5cd498SDavid Xu error = 0; 3220a5cd498SDavid Xu if (uap->sig == 0) 3230a5cd498SDavid Xu break; 3246d7b314bSDavid Xu tdsignal(p, ttd, uap->sig, NULL); 3250a5cd498SDavid Xu } 3260a5cd498SDavid Xu } 3270a5cd498SDavid Xu } 3280a5cd498SDavid Xu } else { 3290a5cd498SDavid Xu if (uap->id != td->td_tid) 3300a5cd498SDavid Xu ttd = thread_find(p, uap->id); 3310a5cd498SDavid Xu else 3320a5cd498SDavid Xu ttd = td; 3330a5cd498SDavid Xu if (ttd == NULL) 3340a5cd498SDavid Xu error = ESRCH; 3350a5cd498SDavid Xu else if (uap->sig == 0) 3360a5cd498SDavid Xu ; 3370a5cd498SDavid Xu else if (!_SIG_VALID(uap->sig)) 3380a5cd498SDavid Xu error = EINVAL; 3390a5cd498SDavid Xu else 3400a5cd498SDavid Xu tdsignal(p, ttd, uap->sig, NULL); 3410a5cd498SDavid Xu } 34289bb1cefSJeff Roberson PROC_UNLOCK(p); 34389bb1cefSJeff Roberson return (error); 34489bb1cefSJeff Roberson } 3451713a516SMike Makonnen 3461713a516SMike Makonnen int 3470b1f0611SDavid Xu thr_kill2(struct thread *td, struct thr_kill2_args *uap) 3480b1f0611SDavid Xu /* pid_t pid, long id, int sig */ 3490b1f0611SDavid Xu { 3500b1f0611SDavid Xu struct thread *ttd; 3510b1f0611SDavid Xu struct proc *p; 3520b1f0611SDavid Xu int error; 3530b1f0611SDavid Xu 3540b1f0611SDavid Xu AUDIT_ARG(signum, uap->sig); 3550b1f0611SDavid Xu 3560b1f0611SDavid Xu if (uap->pid == td->td_proc->p_pid) { 3570b1f0611SDavid Xu p = td->td_proc; 3580b1f0611SDavid Xu PROC_LOCK(p); 3590b1f0611SDavid Xu } else if ((p = pfind(uap->pid)) == NULL) { 3600b1f0611SDavid Xu return (ESRCH); 3610b1f0611SDavid Xu } 3620b1f0611SDavid Xu AUDIT_ARG(process, p); 3630b1f0611SDavid Xu 3640b1f0611SDavid Xu error = p_cansignal(td, p, uap->sig); 3650b1f0611SDavid Xu if (error == 0) { 3660b1f0611SDavid Xu if (uap->id == -1) { 3670b1f0611SDavid Xu if (uap->sig != 0 && !_SIG_VALID(uap->sig)) { 3680b1f0611SDavid Xu error = EINVAL; 3690b1f0611SDavid Xu } else { 3700b1f0611SDavid Xu error = ESRCH; 3710b1f0611SDavid Xu FOREACH_THREAD_IN_PROC(p, ttd) { 3720b1f0611SDavid Xu if (ttd != td) { 3730b1f0611SDavid Xu error = 0; 3740b1f0611SDavid Xu if (uap->sig == 0) 3750b1f0611SDavid Xu break; 3760b1f0611SDavid Xu tdsignal(p, ttd, uap->sig, NULL); 3770b1f0611SDavid Xu } 3780b1f0611SDavid Xu } 3790b1f0611SDavid Xu } 3800b1f0611SDavid Xu } else { 3810b1f0611SDavid Xu if (uap->id != td->td_tid) 3820b1f0611SDavid Xu ttd = thread_find(p, uap->id); 3830b1f0611SDavid Xu else 3840b1f0611SDavid Xu ttd = td; 3850b1f0611SDavid Xu if (ttd == NULL) 3860b1f0611SDavid Xu error = ESRCH; 3870b1f0611SDavid Xu else if (uap->sig == 0) 3880b1f0611SDavid Xu ; 3890b1f0611SDavid Xu else if (!_SIG_VALID(uap->sig)) 3900b1f0611SDavid Xu error = EINVAL; 3910b1f0611SDavid Xu else 3920b1f0611SDavid Xu tdsignal(p, ttd, uap->sig, NULL); 3930b1f0611SDavid Xu } 3940b1f0611SDavid Xu } 3950b1f0611SDavid Xu PROC_UNLOCK(p); 3960b1f0611SDavid Xu return (error); 3970b1f0611SDavid Xu } 3980b1f0611SDavid Xu 3990b1f0611SDavid Xu int 4001713a516SMike Makonnen thr_suspend(struct thread *td, struct thr_suspend_args *uap) 4011713a516SMike Makonnen /* const struct timespec *timeout */ 4021713a516SMike Makonnen { 403cda9a0d1SDavid Xu struct timespec ts, *tsp; 4041713a516SMike Makonnen int error; 4051713a516SMike Makonnen 4061713a516SMike Makonnen error = 0; 407cda9a0d1SDavid Xu tsp = NULL; 4081713a516SMike Makonnen if (uap->timeout != NULL) { 4091713a516SMike Makonnen error = copyin((const void *)uap->timeout, (void *)&ts, 4101713a516SMike Makonnen sizeof(struct timespec)); 4111713a516SMike Makonnen if (error != 0) 4121713a516SMike Makonnen return (error); 413cda9a0d1SDavid Xu tsp = &ts; 414cda9a0d1SDavid Xu } 415cda9a0d1SDavid Xu 416cda9a0d1SDavid Xu return (kern_thr_suspend(td, tsp)); 417cda9a0d1SDavid Xu } 418cda9a0d1SDavid Xu 419cda9a0d1SDavid Xu int 420cda9a0d1SDavid Xu kern_thr_suspend(struct thread *td, struct timespec *tsp) 421cda9a0d1SDavid Xu { 422cda9a0d1SDavid Xu struct timeval tv; 423cda9a0d1SDavid Xu int error = 0, hz = 0; 424cda9a0d1SDavid Xu 425cda9a0d1SDavid Xu if (tsp != NULL) { 426cda9a0d1SDavid Xu if (tsp->tv_nsec < 0 || tsp->tv_nsec > 1000000000) 4271713a516SMike Makonnen return (EINVAL); 428cda9a0d1SDavid Xu if (tsp->tv_sec == 0 && tsp->tv_nsec == 0) 4291713a516SMike Makonnen return (ETIMEDOUT); 430cda9a0d1SDavid Xu TIMESPEC_TO_TIMEVAL(&tv, tsp); 4311713a516SMike Makonnen hz = tvtohz(&tv); 4321713a516SMike Makonnen } 433745fbd3aSDavid Xu 434745fbd3aSDavid Xu if (td->td_pflags & TDP_WAKEUP) { 435745fbd3aSDavid Xu td->td_pflags &= ~TDP_WAKEUP; 436745fbd3aSDavid Xu return (0); 437745fbd3aSDavid Xu } 438745fbd3aSDavid Xu 4391713a516SMike Makonnen PROC_LOCK(td->td_proc); 440c21e3b38SMike Makonnen if ((td->td_flags & TDF_THRWAKEUP) == 0) 4410f180a7cSJohn Baldwin error = msleep((void *)td, &td->td_proc->p_mtx, PCATCH, "lthr", 4420f180a7cSJohn Baldwin hz); 443c1df5a1aSDavid Xu if (td->td_flags & TDF_THRWAKEUP) { 444982d11f8SJeff Roberson thread_lock(td); 4451713a516SMike Makonnen td->td_flags &= ~TDF_THRWAKEUP; 446982d11f8SJeff Roberson thread_unlock(td); 4471713a516SMike Makonnen PROC_UNLOCK(td->td_proc); 448c1df5a1aSDavid Xu return (0); 449c1df5a1aSDavid Xu } 450c1df5a1aSDavid Xu PROC_UNLOCK(td->td_proc); 451c1df5a1aSDavid Xu if (error == EWOULDBLOCK) 452c1df5a1aSDavid Xu error = ETIMEDOUT; 453c1df5a1aSDavid Xu else if (error == ERESTART) { 454c1df5a1aSDavid Xu if (hz != 0) 455c1df5a1aSDavid Xu error = EINTR; 456c1df5a1aSDavid Xu } 457c1df5a1aSDavid Xu return (error); 4581713a516SMike Makonnen } 4591713a516SMike Makonnen 4601713a516SMike Makonnen int 4611713a516SMike Makonnen thr_wake(struct thread *td, struct thr_wake_args *uap) 462cd28f17dSMarcel Moolenaar /* long id */ 4631713a516SMike Makonnen { 46444355392SDavid Xu struct proc *p; 465cd28f17dSMarcel Moolenaar struct thread *ttd; 4661713a516SMike Makonnen 467745fbd3aSDavid Xu if (uap->id == td->td_tid) { 468745fbd3aSDavid Xu td->td_pflags |= TDP_WAKEUP; 469745fbd3aSDavid Xu return (0); 470745fbd3aSDavid Xu } 471745fbd3aSDavid Xu 47244355392SDavid Xu p = td->td_proc; 47344355392SDavid Xu PROC_LOCK(p); 47444355392SDavid Xu ttd = thread_find(p, uap->id); 4751713a516SMike Makonnen if (ttd == NULL) { 47644355392SDavid Xu PROC_UNLOCK(p); 4771713a516SMike Makonnen return (ESRCH); 4781713a516SMike Makonnen } 479982d11f8SJeff Roberson thread_lock(ttd); 480cd28f17dSMarcel Moolenaar ttd->td_flags |= TDF_THRWAKEUP; 481982d11f8SJeff Roberson thread_unlock(ttd); 482c1df5a1aSDavid Xu wakeup((void *)ttd); 48344355392SDavid Xu PROC_UNLOCK(p); 4841713a516SMike Makonnen return (0); 4851713a516SMike Makonnen } 4869e7d7224SDavid Xu 4879e7d7224SDavid Xu int 4889e7d7224SDavid Xu thr_set_name(struct thread *td, struct thr_set_name_args *uap) 4899e7d7224SDavid Xu { 4909e7d7224SDavid Xu struct proc *p = td->td_proc; 4919e7d7224SDavid Xu char name[MAXCOMLEN + 1]; 4929e7d7224SDavid Xu struct thread *ttd; 4939e7d7224SDavid Xu int error; 4949e7d7224SDavid Xu 4959e7d7224SDavid Xu error = 0; 4969e7d7224SDavid Xu name[0] = '\0'; 4979e7d7224SDavid Xu if (uap->name != NULL) { 4989e7d7224SDavid Xu error = copyinstr(uap->name, name, sizeof(name), 4999e7d7224SDavid Xu NULL); 5009e7d7224SDavid Xu if (error) 5019e7d7224SDavid Xu return (error); 5029e7d7224SDavid Xu } 5039e7d7224SDavid Xu PROC_LOCK(p); 5049e7d7224SDavid Xu if (uap->id == td->td_tid) 5059e7d7224SDavid Xu ttd = td; 5069e7d7224SDavid Xu else 5079e7d7224SDavid Xu ttd = thread_find(p, uap->id); 5089e7d7224SDavid Xu if (ttd != NULL) 5099e7d7224SDavid Xu strcpy(ttd->td_name, name); 5109e7d7224SDavid Xu else 5119e7d7224SDavid Xu error = ESRCH; 5129e7d7224SDavid Xu PROC_UNLOCK(p); 5139e7d7224SDavid Xu return (error); 5149e7d7224SDavid Xu } 515