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 extern struct sysentvec ia32_freebsd_sysvec; 61cda9a0d1SDavid Xu 62cda9a0d1SDavid Xu static inline int 63cda9a0d1SDavid Xu suword_lwpid(void *addr, lwpid_t lwpid) 64cda9a0d1SDavid Xu { 65cda9a0d1SDavid Xu int error; 66cda9a0d1SDavid Xu 67cda9a0d1SDavid Xu if (curproc->p_sysent != &ia32_freebsd_sysvec) 68cda9a0d1SDavid Xu error = suword(addr, lwpid); 69cda9a0d1SDavid Xu else 70cda9a0d1SDavid Xu error = suword32(addr, lwpid); 71cda9a0d1SDavid Xu return (error); 72cda9a0d1SDavid Xu } 73cda9a0d1SDavid Xu 74cda9a0d1SDavid Xu #else 75cda9a0d1SDavid Xu #define suword_lwpid suword 76cda9a0d1SDavid Xu #endif 77cda9a0d1SDavid Xu 78ed062c8dSJulian Elischer extern int max_threads_per_proc; 79a8b491c1SJulian Elischer 80c4bd610fSDavid Xu static int create_thread(struct thread *td, mcontext_t *ctx, 81c4bd610fSDavid Xu void (*start_func)(void *), void *arg, 82c4bd610fSDavid Xu char *stack_base, size_t stack_size, 83c4bd610fSDavid Xu char *tls_base, 84c4bd610fSDavid Xu long *child_tid, long *parent_tid, 8573fa3e5bSDavid Xu int flags, struct rtprio *rtp); 86c4bd610fSDavid Xu 8789bb1cefSJeff Roberson /* 8889bb1cefSJeff Roberson * System call interface. 8989bb1cefSJeff Roberson */ 9089bb1cefSJeff Roberson int 9189bb1cefSJeff Roberson thr_create(struct thread *td, struct thr_create_args *uap) 92cd28f17dSMarcel Moolenaar /* ucontext_t *ctx, long *id, int flags */ 9389bb1cefSJeff Roberson { 9489bb1cefSJeff Roberson ucontext_t ctx; 9589bb1cefSJeff Roberson int error; 9689bb1cefSJeff Roberson 9789bb1cefSJeff Roberson if ((error = copyin(uap->ctx, &ctx, sizeof(ctx)))) 9889bb1cefSJeff Roberson return (error); 9989bb1cefSJeff Roberson 100c4bd610fSDavid Xu error = create_thread(td, &ctx.uc_mcontext, NULL, NULL, 101a0712c99SDavid Xu NULL, 0, NULL, uap->id, NULL, uap->flags, NULL); 102c4bd610fSDavid Xu return (error); 103c4bd610fSDavid Xu } 104c4bd610fSDavid Xu 105c4bd610fSDavid Xu int 106c4bd610fSDavid Xu thr_new(struct thread *td, struct thr_new_args *uap) 107c4bd610fSDavid Xu /* struct thr_param * */ 108c4bd610fSDavid Xu { 109c4bd610fSDavid Xu struct thr_param param; 110cda9a0d1SDavid Xu int error; 111cda9a0d1SDavid Xu 112cda9a0d1SDavid Xu if (uap->param_size < 0 || uap->param_size > sizeof(param)) 113cda9a0d1SDavid Xu return (EINVAL); 114cda9a0d1SDavid Xu bzero(¶m, sizeof(param)); 115cda9a0d1SDavid Xu if ((error = copyin(uap->param, ¶m, uap->param_size))) 116cda9a0d1SDavid Xu return (error); 117cda9a0d1SDavid Xu return (kern_thr_new(td, ¶m)); 118cda9a0d1SDavid Xu } 119cda9a0d1SDavid Xu 120cda9a0d1SDavid Xu int 121cda9a0d1SDavid Xu kern_thr_new(struct thread *td, struct thr_param *param) 122cda9a0d1SDavid Xu { 12373fa3e5bSDavid Xu struct rtprio rtp, *rtpp; 124c4bd610fSDavid Xu int error; 125c4bd610fSDavid Xu 12673fa3e5bSDavid Xu rtpp = NULL; 127cda9a0d1SDavid Xu if (param->rtp != 0) { 128cda9a0d1SDavid Xu error = copyin(param->rtp, &rtp, sizeof(struct rtprio)); 12973fa3e5bSDavid Xu rtpp = &rtp; 130a0712c99SDavid Xu } 131cda9a0d1SDavid Xu error = create_thread(td, NULL, param->start_func, param->arg, 132cda9a0d1SDavid Xu param->stack_base, param->stack_size, param->tls_base, 133cda9a0d1SDavid Xu param->child_tid, param->parent_tid, param->flags, 13473fa3e5bSDavid Xu rtpp); 135c4bd610fSDavid Xu return (error); 136c4bd610fSDavid Xu } 137c4bd610fSDavid Xu 138c4bd610fSDavid Xu static int 139c4bd610fSDavid Xu create_thread(struct thread *td, mcontext_t *ctx, 140c4bd610fSDavid Xu void (*start_func)(void *), void *arg, 141c4bd610fSDavid Xu char *stack_base, size_t stack_size, 142c4bd610fSDavid Xu char *tls_base, 143c4bd610fSDavid Xu long *child_tid, long *parent_tid, 14473fa3e5bSDavid Xu int flags, struct rtprio *rtp) 145c4bd610fSDavid Xu { 146c4bd610fSDavid Xu stack_t stack; 147c4bd610fSDavid Xu struct thread *newtd; 148c4bd610fSDavid Xu struct proc *p; 149adc9c950SDavid Xu int error; 150c4bd610fSDavid Xu 151c4bd610fSDavid Xu error = 0; 152c4bd610fSDavid Xu p = td->td_proc; 153c4bd610fSDavid Xu 154c4bd610fSDavid Xu /* Have race condition but it is cheap. */ 155adc9c950SDavid Xu if (p->p_numthreads >= max_threads_per_proc) 156ed062c8dSJulian Elischer return (EPROCLIM); 157c4bd610fSDavid Xu 15873fa3e5bSDavid Xu if (rtp != NULL) { 15973fa3e5bSDavid Xu switch(rtp->type) { 16073fa3e5bSDavid Xu case RTP_PRIO_REALTIME: 16173fa3e5bSDavid Xu case RTP_PRIO_FIFO: 162a0712c99SDavid Xu /* Only root can set scheduler policy */ 163acd3428bSRobert Watson if (priv_check(td, PRIV_SCHED_SETPOLICY) != 0) 164a0712c99SDavid Xu return (EPERM); 16573fa3e5bSDavid Xu if (rtp->prio > RTP_PRIO_MAX) 166a0712c99SDavid Xu return (EINVAL); 1672dca4ca7SDavid Xu break; 16873fa3e5bSDavid Xu case RTP_PRIO_NORMAL: 16973fa3e5bSDavid Xu rtp->prio = 0; 1702dca4ca7SDavid Xu break; 1712dca4ca7SDavid Xu default: 1722dca4ca7SDavid Xu return (EINVAL); 173a0712c99SDavid Xu } 174a0712c99SDavid Xu } 175a0712c99SDavid Xu 176ad1e7d28SJulian Elischer /* Initialize our td */ 177ed062c8dSJulian Elischer newtd = thread_alloc(); 178c4bd610fSDavid Xu 17989bb1cefSJeff Roberson /* 180c4bd610fSDavid Xu * Try the copyout as soon as we allocate the td so we don't 181c4bd610fSDavid Xu * have to tear things down in a failure case below. 182c4bd610fSDavid Xu * Here we copy out tid to two places, one for child and one 183c4bd610fSDavid Xu * for parent, because pthread can create a detached thread, 184c4bd610fSDavid Xu * if parent wants to safely access child tid, it has to provide 185c4bd610fSDavid Xu * its storage, because child thread may exit quickly and 186c4bd610fSDavid Xu * memory is freed before parent thread can access it. 18789bb1cefSJeff Roberson */ 188c4bd610fSDavid Xu if ((child_tid != NULL && 189cda9a0d1SDavid Xu suword_lwpid(child_tid, newtd->td_tid)) || 190c4bd610fSDavid Xu (parent_tid != NULL && 191cda9a0d1SDavid Xu suword_lwpid(parent_tid, newtd->td_tid))) { 192ed062c8dSJulian Elischer thread_free(newtd); 193cda9a0d1SDavid Xu return (EFAULT); 19489bb1cefSJeff Roberson } 195cda9a0d1SDavid Xu 196ed062c8dSJulian Elischer bzero(&newtd->td_startzero, 1976db36923SDavid Schultz __rangeof(struct thread, td_startzero, td_endzero)); 198ed062c8dSJulian Elischer bcopy(&td->td_startcopy, &newtd->td_startcopy, 1996db36923SDavid Schultz __rangeof(struct thread, td_startcopy, td_endcopy)); 200c4bd610fSDavid Xu newtd->td_proc = td->td_proc; 201c4bd610fSDavid Xu newtd->td_ucred = crhold(td->td_ucred); 20289bb1cefSJeff Roberson 203c4bd610fSDavid Xu cpu_set_upcall(newtd, td); 204c4bd610fSDavid Xu 205c4bd610fSDavid Xu if (ctx != NULL) { /* old way to set user context */ 206c4bd610fSDavid Xu error = set_mcontext(newtd, ctx); 207c4bd610fSDavid Xu if (error != 0) { 208c4bd610fSDavid Xu thread_free(newtd); 209c4bd610fSDavid Xu crfree(td->td_ucred); 210c4bd610fSDavid Xu return (error); 211c4bd610fSDavid Xu } 212c4bd610fSDavid Xu } else { 213c4bd610fSDavid Xu /* Set up our machine context. */ 214c4bd610fSDavid Xu stack.ss_sp = stack_base; 215c4bd610fSDavid Xu stack.ss_size = stack_size; 216c4bd610fSDavid Xu /* Set upcall address to user thread entry function. */ 217c4bd610fSDavid Xu cpu_set_upcall_kse(newtd, start_func, arg, &stack); 218c4bd610fSDavid Xu /* Setup user TLS address and TLS pointer register. */ 219740fd64dSDavid Xu error = cpu_set_user_tls(newtd, tls_base); 220740fd64dSDavid Xu if (error != 0) { 221740fd64dSDavid Xu thread_free(newtd); 222740fd64dSDavid Xu crfree(td->td_ucred); 223740fd64dSDavid Xu return (error); 224740fd64dSDavid Xu } 225c4bd610fSDavid Xu } 226c4bd610fSDavid Xu 2278460a577SJohn Birrell PROC_LOCK(td->td_proc); 2288460a577SJohn Birrell td->td_proc->p_flag |= P_HADTHREADS; 2298460a577SJohn Birrell newtd->td_sigmask = td->td_sigmask; 230982d11f8SJeff Roberson PROC_SLOCK(p); 2318460a577SJohn Birrell thread_link(newtd, p); 232982d11f8SJeff Roberson thread_lock(td); 233ed062c8dSJulian Elischer /* let the scheduler know about these things. */ 234ed062c8dSJulian Elischer sched_fork_thread(td, newtd); 235982d11f8SJeff Roberson thread_unlock(td); 236982d11f8SJeff Roberson PROC_SUNLOCK(p); 237982d11f8SJeff Roberson PROC_UNLOCK(p); 238982d11f8SJeff Roberson thread_lock(newtd); 23973fa3e5bSDavid Xu if (rtp != NULL) { 2408460a577SJohn Birrell if (!(td->td_pri_class == PRI_TIMESHARE && 2418460a577SJohn Birrell rtp->type == RTP_PRIO_NORMAL)) { 2428460a577SJohn Birrell rtp_to_pri(rtp, newtd); 2438460a577SJohn Birrell sched_prio(newtd, newtd->td_user_pri); 2448460a577SJohn Birrell } /* ignore timesharing class */ 2458460a577SJohn Birrell } 246ed062c8dSJulian Elischer TD_SET_CAN_RUN(newtd); 247c4bd610fSDavid Xu /* if ((flags & THR_SUSPENDED) == 0) */ 248f0393f06SJeff Roberson sched_add(newtd, SRQ_BORING); 249982d11f8SJeff Roberson thread_unlock(newtd); 25089bb1cefSJeff Roberson 25189bb1cefSJeff Roberson return (error); 25289bb1cefSJeff Roberson } 25389bb1cefSJeff Roberson 25489bb1cefSJeff Roberson int 25589bb1cefSJeff Roberson thr_self(struct thread *td, struct thr_self_args *uap) 256cd28f17dSMarcel Moolenaar /* long *id */ 25789bb1cefSJeff Roberson { 25889bb1cefSJeff Roberson int error; 25989bb1cefSJeff Roberson 260cda9a0d1SDavid Xu error = suword_lwpid(uap->id, (unsigned)td->td_tid); 261cda9a0d1SDavid Xu if (error == -1) 262cda9a0d1SDavid Xu return (EFAULT); 26389bb1cefSJeff Roberson return (0); 26489bb1cefSJeff Roberson } 26589bb1cefSJeff Roberson 26689bb1cefSJeff Roberson int 26789bb1cefSJeff Roberson thr_exit(struct thread *td, struct thr_exit_args *uap) 268401901acSMike Makonnen /* long *state */ 26989bb1cefSJeff Roberson { 27089bb1cefSJeff Roberson struct proc *p; 27189bb1cefSJeff Roberson 27289bb1cefSJeff Roberson p = td->td_proc; 27389bb1cefSJeff Roberson 274401901acSMike Makonnen /* Signal userland that it can free the stack. */ 2754938faa6SDavid Xu if ((void *)uap->state != NULL) { 276cda9a0d1SDavid Xu suword_lwpid(uap->state, 1); 2774938faa6SDavid Xu kern_umtx_wake(td, uap->state, INT_MAX); 2784938faa6SDavid Xu } 279401901acSMike Makonnen 28089bb1cefSJeff Roberson PROC_LOCK(p); 2819104847fSDavid Xu sigqueue_flush(&td->td_sigqueue); 282982d11f8SJeff Roberson PROC_SLOCK(p); 28389bb1cefSJeff Roberson 28489bb1cefSJeff Roberson /* 285ed062c8dSJulian Elischer * Shutting down last thread in the proc. This will actually 286ed062c8dSJulian Elischer * call exit() in the trampoline when it returns. 28789bb1cefSJeff Roberson */ 288ed062c8dSJulian Elischer if (p->p_numthreads != 1) { 28971b7afb2SDavid Xu thread_stopped(p); 290ed062c8dSJulian Elischer thread_exit(); 291ed062c8dSJulian Elischer /* NOTREACHED */ 292ed062c8dSJulian Elischer } 293982d11f8SJeff Roberson PROC_SUNLOCK(p); 294ed062c8dSJulian Elischer PROC_UNLOCK(p); 29589bb1cefSJeff Roberson return (0); 29689bb1cefSJeff Roberson } 29789bb1cefSJeff Roberson 29889bb1cefSJeff Roberson int 29989bb1cefSJeff Roberson thr_kill(struct thread *td, struct thr_kill_args *uap) 300cd28f17dSMarcel Moolenaar /* long id, int sig */ 30189bb1cefSJeff Roberson { 30289bb1cefSJeff Roberson struct thread *ttd; 30389bb1cefSJeff Roberson struct proc *p; 30489bb1cefSJeff Roberson int error; 30589bb1cefSJeff Roberson 30689bb1cefSJeff Roberson p = td->td_proc; 30789bb1cefSJeff Roberson error = 0; 30889bb1cefSJeff Roberson PROC_LOCK(p); 3090a5cd498SDavid Xu if (uap->id == -1) { 3100a5cd498SDavid Xu if (uap->sig != 0 && !_SIG_VALID(uap->sig)) { 31189bb1cefSJeff Roberson error = EINVAL; 3120a5cd498SDavid Xu } else { 3130a5cd498SDavid Xu error = ESRCH; 3140a5cd498SDavid Xu FOREACH_THREAD_IN_PROC(p, ttd) { 3150a5cd498SDavid Xu if (ttd != td) { 3160a5cd498SDavid Xu error = 0; 3170a5cd498SDavid Xu if (uap->sig == 0) 3180a5cd498SDavid Xu break; 3196d7b314bSDavid Xu tdsignal(p, ttd, uap->sig, NULL); 3200a5cd498SDavid Xu } 3210a5cd498SDavid Xu } 3220a5cd498SDavid Xu } 3230a5cd498SDavid Xu } else { 3240a5cd498SDavid Xu if (uap->id != td->td_tid) 3250a5cd498SDavid Xu ttd = thread_find(p, uap->id); 3260a5cd498SDavid Xu else 3270a5cd498SDavid Xu ttd = td; 3280a5cd498SDavid Xu if (ttd == NULL) 3290a5cd498SDavid Xu error = ESRCH; 3300a5cd498SDavid Xu else if (uap->sig == 0) 3310a5cd498SDavid Xu ; 3320a5cd498SDavid Xu else if (!_SIG_VALID(uap->sig)) 3330a5cd498SDavid Xu error = EINVAL; 3340a5cd498SDavid Xu else 3350a5cd498SDavid Xu tdsignal(p, ttd, uap->sig, NULL); 3360a5cd498SDavid Xu } 33789bb1cefSJeff Roberson PROC_UNLOCK(p); 33889bb1cefSJeff Roberson return (error); 33989bb1cefSJeff Roberson } 3401713a516SMike Makonnen 3411713a516SMike Makonnen int 3420b1f0611SDavid Xu thr_kill2(struct thread *td, struct thr_kill2_args *uap) 3430b1f0611SDavid Xu /* pid_t pid, long id, int sig */ 3440b1f0611SDavid Xu { 3450b1f0611SDavid Xu struct thread *ttd; 3460b1f0611SDavid Xu struct proc *p; 3470b1f0611SDavid Xu int error; 3480b1f0611SDavid Xu 3490b1f0611SDavid Xu AUDIT_ARG(signum, uap->sig); 3500b1f0611SDavid Xu 3510b1f0611SDavid Xu if (uap->pid == td->td_proc->p_pid) { 3520b1f0611SDavid Xu p = td->td_proc; 3530b1f0611SDavid Xu PROC_LOCK(p); 3540b1f0611SDavid Xu } else if ((p = pfind(uap->pid)) == NULL) { 3550b1f0611SDavid Xu return (ESRCH); 3560b1f0611SDavid Xu } 3570b1f0611SDavid Xu AUDIT_ARG(process, p); 3580b1f0611SDavid Xu 3590b1f0611SDavid Xu error = p_cansignal(td, p, uap->sig); 3600b1f0611SDavid Xu if (error == 0) { 3610b1f0611SDavid Xu if (uap->id == -1) { 3620b1f0611SDavid Xu if (uap->sig != 0 && !_SIG_VALID(uap->sig)) { 3630b1f0611SDavid Xu error = EINVAL; 3640b1f0611SDavid Xu } else { 3650b1f0611SDavid Xu error = ESRCH; 3660b1f0611SDavid Xu FOREACH_THREAD_IN_PROC(p, ttd) { 3670b1f0611SDavid Xu if (ttd != td) { 3680b1f0611SDavid Xu error = 0; 3690b1f0611SDavid Xu if (uap->sig == 0) 3700b1f0611SDavid Xu break; 3710b1f0611SDavid Xu tdsignal(p, ttd, uap->sig, NULL); 3720b1f0611SDavid Xu } 3730b1f0611SDavid Xu } 3740b1f0611SDavid Xu } 3750b1f0611SDavid Xu } else { 3760b1f0611SDavid Xu if (uap->id != td->td_tid) 3770b1f0611SDavid Xu ttd = thread_find(p, uap->id); 3780b1f0611SDavid Xu else 3790b1f0611SDavid Xu ttd = td; 3800b1f0611SDavid Xu if (ttd == NULL) 3810b1f0611SDavid Xu error = ESRCH; 3820b1f0611SDavid Xu else if (uap->sig == 0) 3830b1f0611SDavid Xu ; 3840b1f0611SDavid Xu else if (!_SIG_VALID(uap->sig)) 3850b1f0611SDavid Xu error = EINVAL; 3860b1f0611SDavid Xu else 3870b1f0611SDavid Xu tdsignal(p, ttd, uap->sig, NULL); 3880b1f0611SDavid Xu } 3890b1f0611SDavid Xu } 3900b1f0611SDavid Xu PROC_UNLOCK(p); 3910b1f0611SDavid Xu return (error); 3920b1f0611SDavid Xu } 3930b1f0611SDavid Xu 3940b1f0611SDavid Xu int 3951713a516SMike Makonnen thr_suspend(struct thread *td, struct thr_suspend_args *uap) 3961713a516SMike Makonnen /* const struct timespec *timeout */ 3971713a516SMike Makonnen { 398cda9a0d1SDavid Xu struct timespec ts, *tsp; 3991713a516SMike Makonnen int error; 4001713a516SMike Makonnen 4011713a516SMike Makonnen error = 0; 402cda9a0d1SDavid Xu tsp = NULL; 4031713a516SMike Makonnen if (uap->timeout != NULL) { 4041713a516SMike Makonnen error = copyin((const void *)uap->timeout, (void *)&ts, 4051713a516SMike Makonnen sizeof(struct timespec)); 4061713a516SMike Makonnen if (error != 0) 4071713a516SMike Makonnen return (error); 408cda9a0d1SDavid Xu tsp = &ts; 409cda9a0d1SDavid Xu } 410cda9a0d1SDavid Xu 411cda9a0d1SDavid Xu return (kern_thr_suspend(td, tsp)); 412cda9a0d1SDavid Xu } 413cda9a0d1SDavid Xu 414cda9a0d1SDavid Xu int 415cda9a0d1SDavid Xu kern_thr_suspend(struct thread *td, struct timespec *tsp) 416cda9a0d1SDavid Xu { 417cda9a0d1SDavid Xu struct timeval tv; 418cda9a0d1SDavid Xu int error = 0, hz = 0; 419cda9a0d1SDavid Xu 420cda9a0d1SDavid Xu if (tsp != NULL) { 421cda9a0d1SDavid Xu if (tsp->tv_nsec < 0 || tsp->tv_nsec > 1000000000) 4221713a516SMike Makonnen return (EINVAL); 423cda9a0d1SDavid Xu if (tsp->tv_sec == 0 && tsp->tv_nsec == 0) 4241713a516SMike Makonnen return (ETIMEDOUT); 425cda9a0d1SDavid Xu TIMESPEC_TO_TIMEVAL(&tv, tsp); 4261713a516SMike Makonnen hz = tvtohz(&tv); 4271713a516SMike Makonnen } 428745fbd3aSDavid Xu 429745fbd3aSDavid Xu if (td->td_pflags & TDP_WAKEUP) { 430745fbd3aSDavid Xu td->td_pflags &= ~TDP_WAKEUP; 431745fbd3aSDavid Xu return (0); 432745fbd3aSDavid Xu } 433745fbd3aSDavid Xu 4341713a516SMike Makonnen PROC_LOCK(td->td_proc); 435c21e3b38SMike Makonnen if ((td->td_flags & TDF_THRWAKEUP) == 0) 4360f180a7cSJohn Baldwin error = msleep((void *)td, &td->td_proc->p_mtx, PCATCH, "lthr", 4370f180a7cSJohn Baldwin hz); 438c1df5a1aSDavid Xu if (td->td_flags & TDF_THRWAKEUP) { 439982d11f8SJeff Roberson thread_lock(td); 4401713a516SMike Makonnen td->td_flags &= ~TDF_THRWAKEUP; 441982d11f8SJeff Roberson thread_unlock(td); 4421713a516SMike Makonnen PROC_UNLOCK(td->td_proc); 443c1df5a1aSDavid Xu return (0); 444c1df5a1aSDavid Xu } 445c1df5a1aSDavid Xu PROC_UNLOCK(td->td_proc); 446c1df5a1aSDavid Xu if (error == EWOULDBLOCK) 447c1df5a1aSDavid Xu error = ETIMEDOUT; 448c1df5a1aSDavid Xu else if (error == ERESTART) { 449c1df5a1aSDavid Xu if (hz != 0) 450c1df5a1aSDavid Xu error = EINTR; 451c1df5a1aSDavid Xu } 452c1df5a1aSDavid Xu return (error); 4531713a516SMike Makonnen } 4541713a516SMike Makonnen 4551713a516SMike Makonnen int 4561713a516SMike Makonnen thr_wake(struct thread *td, struct thr_wake_args *uap) 457cd28f17dSMarcel Moolenaar /* long id */ 4581713a516SMike Makonnen { 45944355392SDavid Xu struct proc *p; 460cd28f17dSMarcel Moolenaar struct thread *ttd; 4611713a516SMike Makonnen 462745fbd3aSDavid Xu if (uap->id == td->td_tid) { 463745fbd3aSDavid Xu td->td_pflags |= TDP_WAKEUP; 464745fbd3aSDavid Xu return (0); 465745fbd3aSDavid Xu } 466745fbd3aSDavid Xu 46744355392SDavid Xu p = td->td_proc; 46844355392SDavid Xu PROC_LOCK(p); 46944355392SDavid Xu ttd = thread_find(p, uap->id); 4701713a516SMike Makonnen if (ttd == NULL) { 47144355392SDavid Xu PROC_UNLOCK(p); 4721713a516SMike Makonnen return (ESRCH); 4731713a516SMike Makonnen } 474982d11f8SJeff Roberson thread_lock(ttd); 475cd28f17dSMarcel Moolenaar ttd->td_flags |= TDF_THRWAKEUP; 476982d11f8SJeff Roberson thread_unlock(ttd); 477c1df5a1aSDavid Xu wakeup((void *)ttd); 47844355392SDavid Xu PROC_UNLOCK(p); 4791713a516SMike Makonnen return (0); 4801713a516SMike Makonnen } 4819e7d7224SDavid Xu 4829e7d7224SDavid Xu int 4839e7d7224SDavid Xu thr_set_name(struct thread *td, struct thr_set_name_args *uap) 4849e7d7224SDavid Xu { 4859e7d7224SDavid Xu struct proc *p = td->td_proc; 4869e7d7224SDavid Xu char name[MAXCOMLEN + 1]; 4879e7d7224SDavid Xu struct thread *ttd; 4889e7d7224SDavid Xu int error; 4899e7d7224SDavid Xu 4909e7d7224SDavid Xu error = 0; 4919e7d7224SDavid Xu name[0] = '\0'; 4929e7d7224SDavid Xu if (uap->name != NULL) { 4939e7d7224SDavid Xu error = copyinstr(uap->name, name, sizeof(name), 4949e7d7224SDavid Xu NULL); 4959e7d7224SDavid Xu if (error) 4969e7d7224SDavid Xu return (error); 4979e7d7224SDavid Xu } 4989e7d7224SDavid Xu PROC_LOCK(p); 4999e7d7224SDavid Xu if (uap->id == td->td_tid) 5009e7d7224SDavid Xu ttd = td; 5019e7d7224SDavid Xu else 5029e7d7224SDavid Xu ttd = thread_find(p, uap->id); 5039e7d7224SDavid Xu if (ttd != NULL) 5049e7d7224SDavid Xu strcpy(ttd->td_name, name); 5059e7d7224SDavid Xu else 5069e7d7224SDavid Xu error = ESRCH; 5079e7d7224SDavid Xu PROC_UNLOCK(p); 5089e7d7224SDavid Xu return (error); 5099e7d7224SDavid Xu } 510