189bb1cefSJeff Roberson /* 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 * $FreeBSD$ 2789bb1cefSJeff Roberson * 2889bb1cefSJeff Roberson */ 2989bb1cefSJeff Roberson 3089bb1cefSJeff Roberson #include <sys/param.h> 3189bb1cefSJeff Roberson #include <sys/kernel.h> 3289bb1cefSJeff Roberson #include <sys/lock.h> 3389bb1cefSJeff Roberson #include <sys/mutex.h> 3489bb1cefSJeff Roberson #include <sys/proc.h> 3589bb1cefSJeff Roberson #include <sys/resourcevar.h> 36a22ec9d8SJeff Roberson #include <sys/sched.h> 3789bb1cefSJeff Roberson #include <sys/sysent.h> 3889bb1cefSJeff Roberson #include <sys/systm.h> 3989bb1cefSJeff Roberson #include <sys/sysproto.h> 4089bb1cefSJeff Roberson #include <sys/signalvar.h> 4189bb1cefSJeff Roberson #include <sys/ucontext.h> 4289bb1cefSJeff Roberson #include <sys/thr.h> 4389bb1cefSJeff Roberson 4489bb1cefSJeff Roberson #include <machine/frame.h> 4589bb1cefSJeff Roberson 4689bb1cefSJeff Roberson /* 4789bb1cefSJeff Roberson * Back end support functions. 4889bb1cefSJeff Roberson */ 4989bb1cefSJeff Roberson 5089bb1cefSJeff Roberson void 5189bb1cefSJeff Roberson thr_exit1(void) 5289bb1cefSJeff Roberson { 5389bb1cefSJeff Roberson struct ksegrp *kg; 5489bb1cefSJeff Roberson struct thread *td; 5589bb1cefSJeff Roberson struct kse *ke; 5689bb1cefSJeff Roberson struct proc *p; 5789bb1cefSJeff Roberson 5889bb1cefSJeff Roberson td = curthread; 5989bb1cefSJeff Roberson p = td->td_proc; 6089bb1cefSJeff Roberson kg = td->td_ksegrp; 6189bb1cefSJeff Roberson ke = td->td_kse; 6289bb1cefSJeff Roberson 6389bb1cefSJeff Roberson mtx_assert(&sched_lock, MA_OWNED); 6489bb1cefSJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 6589bb1cefSJeff Roberson KASSERT(!mtx_owned(&Giant), ("dying thread owns giant")); 6689bb1cefSJeff Roberson 6789bb1cefSJeff Roberson /* 6889bb1cefSJeff Roberson * Shutting down last thread in the proc. This will actually 6989bb1cefSJeff Roberson * call exit() in the trampoline when it returns. 7089bb1cefSJeff Roberson */ 7189bb1cefSJeff Roberson if (p->p_numthreads == 1) { 7289bb1cefSJeff Roberson PROC_UNLOCK(p); 7389bb1cefSJeff Roberson return; 7489bb1cefSJeff Roberson } 7589bb1cefSJeff Roberson 7689bb1cefSJeff Roberson /* 7789bb1cefSJeff Roberson * XXX Undelivered process wide signals should be reposted to the 7889bb1cefSJeff Roberson * proc. 7989bb1cefSJeff Roberson */ 8089bb1cefSJeff Roberson 8189bb1cefSJeff Roberson /* Clean up cpu resources. */ 8289bb1cefSJeff Roberson cpu_thread_exit(td); 8389bb1cefSJeff Roberson 8489bb1cefSJeff Roberson /* XXX make thread_unlink() */ 8589bb1cefSJeff Roberson TAILQ_REMOVE(&p->p_threads, td, td_plist); 8689bb1cefSJeff Roberson p->p_numthreads--; 8789bb1cefSJeff Roberson TAILQ_REMOVE(&kg->kg_threads, td, td_kglist); 8889bb1cefSJeff Roberson kg->kg_numthreads--; 8989bb1cefSJeff Roberson 9089bb1cefSJeff Roberson ke->ke_state = KES_UNQUEUED; 9189bb1cefSJeff Roberson ke->ke_thread = NULL; 9289bb1cefSJeff Roberson kse_unlink(ke); 93a22ec9d8SJeff Roberson sched_exit_kse(TAILQ_NEXT(ke, ke_kglist), ke); 9489bb1cefSJeff Roberson 9589bb1cefSJeff Roberson /* 9689bb1cefSJeff Roberson * If we were stopped while waiting for all threads to exit and this 9789bb1cefSJeff Roberson * is the last thread wakeup the exiting thread. 9889bb1cefSJeff Roberson */ 9989bb1cefSJeff Roberson if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) 10089bb1cefSJeff Roberson if (p->p_numthreads == 1) 10189bb1cefSJeff Roberson thread_unsuspend_one(p->p_singlethread); 10289bb1cefSJeff Roberson 10389bb1cefSJeff Roberson PROC_UNLOCK(p); 10489bb1cefSJeff Roberson td->td_kse = NULL; 10589bb1cefSJeff Roberson td->td_state = TDS_INACTIVE; 106a12efae1SJake Burkholder #if 0 10789bb1cefSJeff Roberson td->td_proc = NULL; 108a12efae1SJake Burkholder #endif 10989bb1cefSJeff Roberson td->td_ksegrp = NULL; 11089bb1cefSJeff Roberson td->td_last_kse = NULL; 111a22ec9d8SJeff Roberson sched_exit_thread(TAILQ_NEXT(td, td_kglist), td); 11289bb1cefSJeff Roberson thread_stash(td); 11389bb1cefSJeff Roberson 114cc66ebe2SPeter Wemm #if defined(__i386__) || defined(__sparc64__) 115cc66ebe2SPeter Wemm cpu_throw(td, choosethread()); 116cc66ebe2SPeter Wemm #else 11789bb1cefSJeff Roberson cpu_throw(); 118cc66ebe2SPeter Wemm #endif 11989bb1cefSJeff Roberson } 12089bb1cefSJeff Roberson 12189bb1cefSJeff Roberson #define RANGEOF(type, start, end) (offsetof(type, end) - offsetof(type, start)) 12289bb1cefSJeff Roberson 12389bb1cefSJeff Roberson /* 12489bb1cefSJeff Roberson * System call interface. 12589bb1cefSJeff Roberson */ 12689bb1cefSJeff Roberson int 12789bb1cefSJeff Roberson thr_create(struct thread *td, struct thr_create_args *uap) 12889bb1cefSJeff Roberson /* ucontext_t *ctx, thr_id_t *id, int flags */ 12989bb1cefSJeff Roberson { 13089bb1cefSJeff Roberson struct kse *ke0; 13189bb1cefSJeff Roberson struct thread *td0; 13289bb1cefSJeff Roberson ucontext_t ctx; 13389bb1cefSJeff Roberson int error; 13489bb1cefSJeff Roberson 13589bb1cefSJeff Roberson if ((error = copyin(uap->ctx, &ctx, sizeof(ctx)))) 13689bb1cefSJeff Roberson return (error); 13789bb1cefSJeff Roberson 13889bb1cefSJeff Roberson /* Initialize our td. */ 13989bb1cefSJeff Roberson td0 = thread_alloc(); 14089bb1cefSJeff Roberson 14189bb1cefSJeff Roberson /* 14289bb1cefSJeff Roberson * Try the copyout as soon as we allocate the td so we don't have to 14389bb1cefSJeff Roberson * tear things down in a failure case below. 14489bb1cefSJeff Roberson */ 14589bb1cefSJeff Roberson if ((error = copyout(&td0, uap->id, sizeof(thr_id_t)))) { 14689bb1cefSJeff Roberson thread_free(td0); 14789bb1cefSJeff Roberson return (error); 14889bb1cefSJeff Roberson } 14989bb1cefSJeff Roberson 15089bb1cefSJeff Roberson bzero(&td0->td_startzero, 15189bb1cefSJeff Roberson (unsigned)RANGEOF(struct thread, td_startzero, td_endzero)); 15289bb1cefSJeff Roberson bcopy(&td->td_startcopy, &td0->td_startcopy, 15389bb1cefSJeff Roberson (unsigned) RANGEOF(struct thread, td_startcopy, td_endcopy)); 15489bb1cefSJeff Roberson 15589bb1cefSJeff Roberson td0->td_proc = td->td_proc; 15689bb1cefSJeff Roberson td0->td_sigmask = td->td_sigmask; 15789bb1cefSJeff Roberson bcopy(td->td_frame, td0->td_frame, sizeof(struct trapframe)); 15889bb1cefSJeff Roberson td0->td_ucred = crhold(td->td_ucred); 15989bb1cefSJeff Roberson 16089bb1cefSJeff Roberson /* Initialize our kse structure. */ 16189bb1cefSJeff Roberson ke0 = kse_alloc(); 16289bb1cefSJeff Roberson bzero(&ke0->ke_startzero, 16389bb1cefSJeff Roberson RANGEOF(struct kse, ke_startzero, ke_endzero)); 16489bb1cefSJeff Roberson 16589bb1cefSJeff Roberson /* Set up our machine context. */ 16689bb1cefSJeff Roberson cpu_set_upcall(td0, td->td_pcb); 16789bb1cefSJeff Roberson error = set_mcontext(td0, &ctx.uc_mcontext); 16889bb1cefSJeff Roberson if (error != 0) { 16989bb1cefSJeff Roberson kse_free(ke0); 17089bb1cefSJeff Roberson thread_free(td0); 17189bb1cefSJeff Roberson goto out; 17289bb1cefSJeff Roberson } 17389bb1cefSJeff Roberson 17489bb1cefSJeff Roberson /* Link the thread and kse into the ksegrp and make it runnable. */ 17589bb1cefSJeff Roberson mtx_lock_spin(&sched_lock); 17689bb1cefSJeff Roberson 17789bb1cefSJeff Roberson thread_link(td0, td->td_ksegrp); 17889bb1cefSJeff Roberson kse_link(ke0, td->td_ksegrp); 17989bb1cefSJeff Roberson 18089bb1cefSJeff Roberson /* Bind this thread and kse together. */ 18189bb1cefSJeff Roberson td0->td_kse = ke0; 18289bb1cefSJeff Roberson ke0->ke_thread = td0; 18389bb1cefSJeff Roberson 184a22ec9d8SJeff Roberson sched_fork_kse(td->td_kse, ke0); 185a22ec9d8SJeff Roberson sched_fork_thread(td, td0); 186a22ec9d8SJeff Roberson 18789bb1cefSJeff Roberson TD_SET_CAN_RUN(td0); 18889bb1cefSJeff Roberson if ((uap->flags & THR_SUSPENDED) == 0) 18989bb1cefSJeff Roberson setrunqueue(td0); 19089bb1cefSJeff Roberson 19189bb1cefSJeff Roberson mtx_unlock_spin(&sched_lock); 19289bb1cefSJeff Roberson 19389bb1cefSJeff Roberson out: 19489bb1cefSJeff Roberson return (error); 19589bb1cefSJeff Roberson } 19689bb1cefSJeff Roberson 19789bb1cefSJeff Roberson int 19889bb1cefSJeff Roberson thr_self(struct thread *td, struct thr_self_args *uap) 19989bb1cefSJeff Roberson /* thr_id_t *id */ 20089bb1cefSJeff Roberson { 20189bb1cefSJeff Roberson int error; 20289bb1cefSJeff Roberson 20389bb1cefSJeff Roberson if ((error = copyout(&td, uap->id, sizeof(thr_id_t)))) 20489bb1cefSJeff Roberson return (error); 20589bb1cefSJeff Roberson 20689bb1cefSJeff Roberson return (0); 20789bb1cefSJeff Roberson } 20889bb1cefSJeff Roberson 20989bb1cefSJeff Roberson int 21089bb1cefSJeff Roberson thr_exit(struct thread *td, struct thr_exit_args *uap) 21189bb1cefSJeff Roberson /* NULL */ 21289bb1cefSJeff Roberson { 21389bb1cefSJeff Roberson struct proc *p; 21489bb1cefSJeff Roberson 21589bb1cefSJeff Roberson p = td->td_proc; 21689bb1cefSJeff Roberson 21789bb1cefSJeff Roberson PROC_LOCK(p); 21889bb1cefSJeff Roberson mtx_lock_spin(&sched_lock); 21989bb1cefSJeff Roberson 22089bb1cefSJeff Roberson /* 22189bb1cefSJeff Roberson * This unlocks proc and doesn't return unless this is the last 22289bb1cefSJeff Roberson * thread. 22389bb1cefSJeff Roberson */ 22489bb1cefSJeff Roberson thr_exit1(); 22589bb1cefSJeff Roberson mtx_unlock_spin(&sched_lock); 22689bb1cefSJeff Roberson 22789bb1cefSJeff Roberson return (0); 22889bb1cefSJeff Roberson } 22989bb1cefSJeff Roberson 23089bb1cefSJeff Roberson int 23189bb1cefSJeff Roberson thr_kill(struct thread *td, struct thr_kill_args *uap) 23289bb1cefSJeff Roberson /* thr_id_t id, int sig */ 23389bb1cefSJeff Roberson { 23489bb1cefSJeff Roberson struct thread *ttd; 23589bb1cefSJeff Roberson struct proc *p; 23689bb1cefSJeff Roberson int error; 23789bb1cefSJeff Roberson 23889bb1cefSJeff Roberson p = td->td_proc; 23989bb1cefSJeff Roberson error = 0; 24089bb1cefSJeff Roberson 24189bb1cefSJeff Roberson PROC_LOCK(p); 24289bb1cefSJeff Roberson 24389bb1cefSJeff Roberson FOREACH_THREAD_IN_PROC(p, ttd) 24489bb1cefSJeff Roberson if (ttd == uap->id) 24589bb1cefSJeff Roberson break; 24689bb1cefSJeff Roberson 24789bb1cefSJeff Roberson if (ttd == NULL) { 24889bb1cefSJeff Roberson error = ESRCH; 24989bb1cefSJeff Roberson goto out; 25089bb1cefSJeff Roberson } 25189bb1cefSJeff Roberson 25289bb1cefSJeff Roberson if (uap->sig == 0) 25389bb1cefSJeff Roberson goto out; 25489bb1cefSJeff Roberson 25589bb1cefSJeff Roberson if (!_SIG_VALID(uap->sig)) { 25689bb1cefSJeff Roberson error = EINVAL; 25789bb1cefSJeff Roberson goto out; 25889bb1cefSJeff Roberson } 25989bb1cefSJeff Roberson 26089bb1cefSJeff Roberson /* 26189bb1cefSJeff Roberson * We need a way to force this to go into this thread's siglist. 26289bb1cefSJeff Roberson * Until then blocked signals will go to the proc. 26389bb1cefSJeff Roberson */ 26489bb1cefSJeff Roberson tdsignal(ttd, uap->sig); 26589bb1cefSJeff Roberson out: 26689bb1cefSJeff Roberson PROC_UNLOCK(p); 26789bb1cefSJeff Roberson 26889bb1cefSJeff Roberson return (error); 26989bb1cefSJeff Roberson } 270