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(); 17889b57fcfSKonstantin Belousov if (newtd == NULL) 17989b57fcfSKonstantin Belousov return (ENOMEM); 180c4bd610fSDavid Xu 18189bb1cefSJeff Roberson /* 182c4bd610fSDavid Xu * Try the copyout as soon as we allocate the td so we don't 183c4bd610fSDavid Xu * have to tear things down in a failure case below. 184c4bd610fSDavid Xu * Here we copy out tid to two places, one for child and one 185c4bd610fSDavid Xu * for parent, because pthread can create a detached thread, 186c4bd610fSDavid Xu * if parent wants to safely access child tid, it has to provide 187c4bd610fSDavid Xu * its storage, because child thread may exit quickly and 188c4bd610fSDavid Xu * memory is freed before parent thread can access it. 18989bb1cefSJeff Roberson */ 190c4bd610fSDavid Xu if ((child_tid != NULL && 191cda9a0d1SDavid Xu suword_lwpid(child_tid, newtd->td_tid)) || 192c4bd610fSDavid Xu (parent_tid != NULL && 193cda9a0d1SDavid Xu suword_lwpid(parent_tid, newtd->td_tid))) { 194ed062c8dSJulian Elischer thread_free(newtd); 195cda9a0d1SDavid Xu return (EFAULT); 19689bb1cefSJeff Roberson } 197cda9a0d1SDavid Xu 198ed062c8dSJulian Elischer bzero(&newtd->td_startzero, 1996db36923SDavid Schultz __rangeof(struct thread, td_startzero, td_endzero)); 200ed062c8dSJulian Elischer bcopy(&td->td_startcopy, &newtd->td_startcopy, 2016db36923SDavid Schultz __rangeof(struct thread, td_startcopy, td_endcopy)); 202c4bd610fSDavid Xu newtd->td_proc = td->td_proc; 203c4bd610fSDavid Xu newtd->td_ucred = crhold(td->td_ucred); 20489bb1cefSJeff Roberson 205c4bd610fSDavid Xu cpu_set_upcall(newtd, td); 206c4bd610fSDavid Xu 207c4bd610fSDavid Xu if (ctx != NULL) { /* old way to set user context */ 208c4bd610fSDavid Xu error = set_mcontext(newtd, ctx); 209c4bd610fSDavid Xu if (error != 0) { 210c4bd610fSDavid Xu thread_free(newtd); 211c4bd610fSDavid Xu crfree(td->td_ucred); 212c4bd610fSDavid Xu return (error); 213c4bd610fSDavid Xu } 214c4bd610fSDavid Xu } else { 215c4bd610fSDavid Xu /* Set up our machine context. */ 216c4bd610fSDavid Xu stack.ss_sp = stack_base; 217c4bd610fSDavid Xu stack.ss_size = stack_size; 218c4bd610fSDavid Xu /* Set upcall address to user thread entry function. */ 219c4bd610fSDavid Xu cpu_set_upcall_kse(newtd, start_func, arg, &stack); 220c4bd610fSDavid Xu /* Setup user TLS address and TLS pointer register. */ 221740fd64dSDavid Xu error = cpu_set_user_tls(newtd, tls_base); 222740fd64dSDavid Xu if (error != 0) { 223740fd64dSDavid Xu thread_free(newtd); 224740fd64dSDavid Xu crfree(td->td_ucred); 225740fd64dSDavid Xu return (error); 226740fd64dSDavid Xu } 227c4bd610fSDavid Xu } 228c4bd610fSDavid Xu 2298460a577SJohn Birrell PROC_LOCK(td->td_proc); 2308460a577SJohn Birrell td->td_proc->p_flag |= P_HADTHREADS; 2318460a577SJohn Birrell newtd->td_sigmask = td->td_sigmask; 232982d11f8SJeff Roberson PROC_SLOCK(p); 2338460a577SJohn Birrell thread_link(newtd, p); 234c67ddc21SJulian Elischer bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name)); 235982d11f8SJeff Roberson thread_lock(td); 236ed062c8dSJulian Elischer /* let the scheduler know about these things. */ 237ed062c8dSJulian Elischer sched_fork_thread(td, newtd); 238982d11f8SJeff Roberson thread_unlock(td); 239982d11f8SJeff Roberson PROC_SUNLOCK(p); 240982d11f8SJeff Roberson PROC_UNLOCK(p); 241982d11f8SJeff Roberson thread_lock(newtd); 24273fa3e5bSDavid Xu if (rtp != NULL) { 2438460a577SJohn Birrell if (!(td->td_pri_class == PRI_TIMESHARE && 2448460a577SJohn Birrell rtp->type == RTP_PRIO_NORMAL)) { 2458460a577SJohn Birrell rtp_to_pri(rtp, newtd); 2468460a577SJohn Birrell sched_prio(newtd, newtd->td_user_pri); 2478460a577SJohn Birrell } /* ignore timesharing class */ 2488460a577SJohn Birrell } 249ed062c8dSJulian Elischer TD_SET_CAN_RUN(newtd); 250c4bd610fSDavid Xu /* if ((flags & THR_SUSPENDED) == 0) */ 251f0393f06SJeff Roberson sched_add(newtd, SRQ_BORING); 252982d11f8SJeff Roberson thread_unlock(newtd); 25389bb1cefSJeff Roberson 25489bb1cefSJeff Roberson return (error); 25589bb1cefSJeff Roberson } 25689bb1cefSJeff Roberson 25789bb1cefSJeff Roberson int 25889bb1cefSJeff Roberson thr_self(struct thread *td, struct thr_self_args *uap) 259cd28f17dSMarcel Moolenaar /* long *id */ 26089bb1cefSJeff Roberson { 26189bb1cefSJeff Roberson int error; 26289bb1cefSJeff Roberson 263cda9a0d1SDavid Xu error = suword_lwpid(uap->id, (unsigned)td->td_tid); 264cda9a0d1SDavid Xu if (error == -1) 265cda9a0d1SDavid Xu return (EFAULT); 26689bb1cefSJeff Roberson return (0); 26789bb1cefSJeff Roberson } 26889bb1cefSJeff Roberson 26989bb1cefSJeff Roberson int 27089bb1cefSJeff Roberson thr_exit(struct thread *td, struct thr_exit_args *uap) 271401901acSMike Makonnen /* long *state */ 27289bb1cefSJeff Roberson { 27389bb1cefSJeff Roberson struct proc *p; 27489bb1cefSJeff Roberson 27589bb1cefSJeff Roberson p = td->td_proc; 27689bb1cefSJeff Roberson 277401901acSMike Makonnen /* Signal userland that it can free the stack. */ 2784938faa6SDavid Xu if ((void *)uap->state != NULL) { 279cda9a0d1SDavid Xu suword_lwpid(uap->state, 1); 2804938faa6SDavid Xu kern_umtx_wake(td, uap->state, INT_MAX); 2814938faa6SDavid Xu } 282401901acSMike Makonnen 28389bb1cefSJeff Roberson PROC_LOCK(p); 2849104847fSDavid Xu sigqueue_flush(&td->td_sigqueue); 285982d11f8SJeff Roberson PROC_SLOCK(p); 28689bb1cefSJeff Roberson 28789bb1cefSJeff Roberson /* 288ed062c8dSJulian Elischer * Shutting down last thread in the proc. This will actually 289ed062c8dSJulian Elischer * call exit() in the trampoline when it returns. 29089bb1cefSJeff Roberson */ 291ed062c8dSJulian Elischer if (p->p_numthreads != 1) { 29271b7afb2SDavid Xu thread_stopped(p); 293ed062c8dSJulian Elischer thread_exit(); 294ed062c8dSJulian Elischer /* NOTREACHED */ 295ed062c8dSJulian Elischer } 296982d11f8SJeff Roberson PROC_SUNLOCK(p); 297ed062c8dSJulian Elischer PROC_UNLOCK(p); 29889bb1cefSJeff Roberson return (0); 29989bb1cefSJeff Roberson } 30089bb1cefSJeff Roberson 30189bb1cefSJeff Roberson int 30289bb1cefSJeff Roberson thr_kill(struct thread *td, struct thr_kill_args *uap) 303cd28f17dSMarcel Moolenaar /* long id, int sig */ 30489bb1cefSJeff Roberson { 30589bb1cefSJeff Roberson struct thread *ttd; 30689bb1cefSJeff Roberson struct proc *p; 30789bb1cefSJeff Roberson int error; 30889bb1cefSJeff Roberson 30989bb1cefSJeff Roberson p = td->td_proc; 31089bb1cefSJeff Roberson error = 0; 31189bb1cefSJeff Roberson PROC_LOCK(p); 3120a5cd498SDavid Xu if (uap->id == -1) { 3130a5cd498SDavid Xu if (uap->sig != 0 && !_SIG_VALID(uap->sig)) { 31489bb1cefSJeff Roberson error = EINVAL; 3150a5cd498SDavid Xu } else { 3160a5cd498SDavid Xu error = ESRCH; 3170a5cd498SDavid Xu FOREACH_THREAD_IN_PROC(p, ttd) { 3180a5cd498SDavid Xu if (ttd != td) { 3190a5cd498SDavid Xu error = 0; 3200a5cd498SDavid Xu if (uap->sig == 0) 3210a5cd498SDavid Xu break; 3226d7b314bSDavid Xu tdsignal(p, ttd, uap->sig, NULL); 3230a5cd498SDavid Xu } 3240a5cd498SDavid Xu } 3250a5cd498SDavid Xu } 3260a5cd498SDavid Xu } else { 3270a5cd498SDavid Xu if (uap->id != td->td_tid) 3280a5cd498SDavid Xu ttd = thread_find(p, uap->id); 3290a5cd498SDavid Xu else 3300a5cd498SDavid Xu ttd = td; 3310a5cd498SDavid Xu if (ttd == NULL) 3320a5cd498SDavid Xu error = ESRCH; 3330a5cd498SDavid Xu else if (uap->sig == 0) 3340a5cd498SDavid Xu ; 3350a5cd498SDavid Xu else if (!_SIG_VALID(uap->sig)) 3360a5cd498SDavid Xu error = EINVAL; 3370a5cd498SDavid Xu else 3380a5cd498SDavid Xu tdsignal(p, ttd, uap->sig, NULL); 3390a5cd498SDavid Xu } 34089bb1cefSJeff Roberson PROC_UNLOCK(p); 34189bb1cefSJeff Roberson return (error); 34289bb1cefSJeff Roberson } 3431713a516SMike Makonnen 3441713a516SMike Makonnen int 3450b1f0611SDavid Xu thr_kill2(struct thread *td, struct thr_kill2_args *uap) 3460b1f0611SDavid Xu /* pid_t pid, long id, int sig */ 3470b1f0611SDavid Xu { 3480b1f0611SDavid Xu struct thread *ttd; 3490b1f0611SDavid Xu struct proc *p; 3500b1f0611SDavid Xu int error; 3510b1f0611SDavid Xu 3520b1f0611SDavid Xu AUDIT_ARG(signum, uap->sig); 3530b1f0611SDavid Xu 3540b1f0611SDavid Xu if (uap->pid == td->td_proc->p_pid) { 3550b1f0611SDavid Xu p = td->td_proc; 3560b1f0611SDavid Xu PROC_LOCK(p); 3570b1f0611SDavid Xu } else if ((p = pfind(uap->pid)) == NULL) { 3580b1f0611SDavid Xu return (ESRCH); 3590b1f0611SDavid Xu } 3600b1f0611SDavid Xu AUDIT_ARG(process, p); 3610b1f0611SDavid Xu 3620b1f0611SDavid Xu error = p_cansignal(td, p, uap->sig); 3630b1f0611SDavid Xu if (error == 0) { 3640b1f0611SDavid Xu if (uap->id == -1) { 3650b1f0611SDavid Xu if (uap->sig != 0 && !_SIG_VALID(uap->sig)) { 3660b1f0611SDavid Xu error = EINVAL; 3670b1f0611SDavid Xu } else { 3680b1f0611SDavid Xu error = ESRCH; 3690b1f0611SDavid Xu FOREACH_THREAD_IN_PROC(p, ttd) { 3700b1f0611SDavid Xu if (ttd != td) { 3710b1f0611SDavid Xu error = 0; 3720b1f0611SDavid Xu if (uap->sig == 0) 3730b1f0611SDavid Xu break; 3740b1f0611SDavid Xu tdsignal(p, ttd, uap->sig, NULL); 3750b1f0611SDavid Xu } 3760b1f0611SDavid Xu } 3770b1f0611SDavid Xu } 3780b1f0611SDavid Xu } else { 3790b1f0611SDavid Xu if (uap->id != td->td_tid) 3800b1f0611SDavid Xu ttd = thread_find(p, uap->id); 3810b1f0611SDavid Xu else 3820b1f0611SDavid Xu ttd = td; 3830b1f0611SDavid Xu if (ttd == NULL) 3840b1f0611SDavid Xu error = ESRCH; 3850b1f0611SDavid Xu else if (uap->sig == 0) 3860b1f0611SDavid Xu ; 3870b1f0611SDavid Xu else if (!_SIG_VALID(uap->sig)) 3880b1f0611SDavid Xu error = EINVAL; 3890b1f0611SDavid Xu else 3900b1f0611SDavid Xu tdsignal(p, ttd, uap->sig, NULL); 3910b1f0611SDavid Xu } 3920b1f0611SDavid Xu } 3930b1f0611SDavid Xu PROC_UNLOCK(p); 3940b1f0611SDavid Xu return (error); 3950b1f0611SDavid Xu } 3960b1f0611SDavid Xu 3970b1f0611SDavid Xu int 3981713a516SMike Makonnen thr_suspend(struct thread *td, struct thr_suspend_args *uap) 3991713a516SMike Makonnen /* const struct timespec *timeout */ 4001713a516SMike Makonnen { 401cda9a0d1SDavid Xu struct timespec ts, *tsp; 4021713a516SMike Makonnen int error; 4031713a516SMike Makonnen 4041713a516SMike Makonnen error = 0; 405cda9a0d1SDavid Xu tsp = NULL; 4061713a516SMike Makonnen if (uap->timeout != NULL) { 4071713a516SMike Makonnen error = copyin((const void *)uap->timeout, (void *)&ts, 4081713a516SMike Makonnen sizeof(struct timespec)); 4091713a516SMike Makonnen if (error != 0) 4101713a516SMike Makonnen return (error); 411cda9a0d1SDavid Xu tsp = &ts; 412cda9a0d1SDavid Xu } 413cda9a0d1SDavid Xu 414cda9a0d1SDavid Xu return (kern_thr_suspend(td, tsp)); 415cda9a0d1SDavid Xu } 416cda9a0d1SDavid Xu 417cda9a0d1SDavid Xu int 418cda9a0d1SDavid Xu kern_thr_suspend(struct thread *td, struct timespec *tsp) 419cda9a0d1SDavid Xu { 420cda9a0d1SDavid Xu struct timeval tv; 421cda9a0d1SDavid Xu int error = 0, hz = 0; 422cda9a0d1SDavid Xu 423cda9a0d1SDavid Xu if (tsp != NULL) { 424cda9a0d1SDavid Xu if (tsp->tv_nsec < 0 || tsp->tv_nsec > 1000000000) 4251713a516SMike Makonnen return (EINVAL); 426cda9a0d1SDavid Xu if (tsp->tv_sec == 0 && tsp->tv_nsec == 0) 4271713a516SMike Makonnen return (ETIMEDOUT); 428cda9a0d1SDavid Xu TIMESPEC_TO_TIMEVAL(&tv, tsp); 4291713a516SMike Makonnen hz = tvtohz(&tv); 4301713a516SMike Makonnen } 431745fbd3aSDavid Xu 432745fbd3aSDavid Xu if (td->td_pflags & TDP_WAKEUP) { 433745fbd3aSDavid Xu td->td_pflags &= ~TDP_WAKEUP; 434745fbd3aSDavid Xu return (0); 435745fbd3aSDavid Xu } 436745fbd3aSDavid Xu 4371713a516SMike Makonnen PROC_LOCK(td->td_proc); 438c21e3b38SMike Makonnen if ((td->td_flags & TDF_THRWAKEUP) == 0) 4390f180a7cSJohn Baldwin error = msleep((void *)td, &td->td_proc->p_mtx, PCATCH, "lthr", 4400f180a7cSJohn Baldwin hz); 441c1df5a1aSDavid Xu if (td->td_flags & TDF_THRWAKEUP) { 442982d11f8SJeff Roberson thread_lock(td); 4431713a516SMike Makonnen td->td_flags &= ~TDF_THRWAKEUP; 444982d11f8SJeff Roberson thread_unlock(td); 4451713a516SMike Makonnen PROC_UNLOCK(td->td_proc); 446c1df5a1aSDavid Xu return (0); 447c1df5a1aSDavid Xu } 448c1df5a1aSDavid Xu PROC_UNLOCK(td->td_proc); 449c1df5a1aSDavid Xu if (error == EWOULDBLOCK) 450c1df5a1aSDavid Xu error = ETIMEDOUT; 451c1df5a1aSDavid Xu else if (error == ERESTART) { 452c1df5a1aSDavid Xu if (hz != 0) 453c1df5a1aSDavid Xu error = EINTR; 454c1df5a1aSDavid Xu } 455c1df5a1aSDavid Xu return (error); 4561713a516SMike Makonnen } 4571713a516SMike Makonnen 4581713a516SMike Makonnen int 4591713a516SMike Makonnen thr_wake(struct thread *td, struct thr_wake_args *uap) 460cd28f17dSMarcel Moolenaar /* long id */ 4611713a516SMike Makonnen { 46244355392SDavid Xu struct proc *p; 463cd28f17dSMarcel Moolenaar struct thread *ttd; 4641713a516SMike Makonnen 465745fbd3aSDavid Xu if (uap->id == td->td_tid) { 466745fbd3aSDavid Xu td->td_pflags |= TDP_WAKEUP; 467745fbd3aSDavid Xu return (0); 468745fbd3aSDavid Xu } 469745fbd3aSDavid Xu 47044355392SDavid Xu p = td->td_proc; 47144355392SDavid Xu PROC_LOCK(p); 47244355392SDavid Xu ttd = thread_find(p, uap->id); 4731713a516SMike Makonnen if (ttd == NULL) { 47444355392SDavid Xu PROC_UNLOCK(p); 4751713a516SMike Makonnen return (ESRCH); 4761713a516SMike Makonnen } 477982d11f8SJeff Roberson thread_lock(ttd); 478cd28f17dSMarcel Moolenaar ttd->td_flags |= TDF_THRWAKEUP; 479982d11f8SJeff Roberson thread_unlock(ttd); 480c1df5a1aSDavid Xu wakeup((void *)ttd); 48144355392SDavid Xu PROC_UNLOCK(p); 4821713a516SMike Makonnen return (0); 4831713a516SMike Makonnen } 4849e7d7224SDavid Xu 4859e7d7224SDavid Xu int 4869e7d7224SDavid Xu thr_set_name(struct thread *td, struct thr_set_name_args *uap) 4879e7d7224SDavid Xu { 4889e7d7224SDavid Xu struct proc *p = td->td_proc; 4899e7d7224SDavid Xu char name[MAXCOMLEN + 1]; 4909e7d7224SDavid Xu struct thread *ttd; 4919e7d7224SDavid Xu int error; 4929e7d7224SDavid Xu 4939e7d7224SDavid Xu error = 0; 4949e7d7224SDavid Xu name[0] = '\0'; 4959e7d7224SDavid Xu if (uap->name != NULL) { 4969e7d7224SDavid Xu error = copyinstr(uap->name, name, sizeof(name), 4979e7d7224SDavid Xu NULL); 4989e7d7224SDavid Xu if (error) 4999e7d7224SDavid Xu return (error); 5009e7d7224SDavid Xu } 5019e7d7224SDavid Xu PROC_LOCK(p); 5029e7d7224SDavid Xu if (uap->id == td->td_tid) 5039e7d7224SDavid Xu ttd = td; 5049e7d7224SDavid Xu else 5059e7d7224SDavid Xu ttd = thread_find(p, uap->id); 5069e7d7224SDavid Xu if (ttd != NULL) 5079e7d7224SDavid Xu strcpy(ttd->td_name, name); 5089e7d7224SDavid Xu else 5099e7d7224SDavid Xu error = ESRCH; 5109e7d7224SDavid Xu PROC_UNLOCK(p); 5119e7d7224SDavid Xu return (error); 5129e7d7224SDavid Xu } 513