1 /* 2 * Copyright (c) 2003, Jeffrey Roberson <jeff@freebsd.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice unmodified, this list of conditions, and the following 10 * disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/kernel.h> 32 #include <sys/lock.h> 33 #include <sys/mutex.h> 34 #include <sys/proc.h> 35 #include <sys/resourcevar.h> 36 #include <sys/sched.h> 37 #include <sys/sysent.h> 38 #include <sys/systm.h> 39 #include <sys/sysproto.h> 40 #include <sys/signalvar.h> 41 #include <sys/ucontext.h> 42 #include <sys/thr.h> 43 44 #include <machine/frame.h> 45 46 /* 47 * Back end support functions. 48 */ 49 50 void 51 thr_exit1(void) 52 { 53 struct ksegrp *kg; 54 struct thread *td; 55 struct kse *ke; 56 struct proc *p; 57 58 td = curthread; 59 p = td->td_proc; 60 kg = td->td_ksegrp; 61 ke = td->td_kse; 62 63 mtx_assert(&sched_lock, MA_OWNED); 64 PROC_LOCK_ASSERT(p, MA_OWNED); 65 KASSERT(!mtx_owned(&Giant), ("dying thread owns giant")); 66 67 /* 68 * Shutting down last thread in the proc. This will actually 69 * call exit() in the trampoline when it returns. 70 */ 71 if (p->p_numthreads == 1) { 72 PROC_UNLOCK(p); 73 return; 74 } 75 76 /* 77 * XXX Undelivered process wide signals should be reposted to the 78 * proc. 79 */ 80 81 /* Clean up cpu resources. */ 82 cpu_thread_exit(td); 83 84 /* XXX make thread_unlink() */ 85 TAILQ_REMOVE(&p->p_threads, td, td_plist); 86 p->p_numthreads--; 87 TAILQ_REMOVE(&kg->kg_threads, td, td_kglist); 88 kg->kg_numthreads--; 89 90 ke->ke_state = KES_UNQUEUED; 91 ke->ke_thread = NULL; 92 kse_unlink(ke); 93 sched_exit_kse(TAILQ_NEXT(ke, ke_kglist), ke); 94 95 /* 96 * If we were stopped while waiting for all threads to exit and this 97 * is the last thread wakeup the exiting thread. 98 */ 99 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) 100 if (p->p_numthreads == 1) 101 thread_unsuspend_one(p->p_singlethread); 102 103 PROC_UNLOCK(p); 104 td->td_kse = NULL; 105 td->td_state = TDS_INACTIVE; 106 #if 0 107 td->td_proc = NULL; 108 #endif 109 td->td_ksegrp = NULL; 110 td->td_last_kse = NULL; 111 sched_exit_thread(TAILQ_NEXT(td, td_kglist), td); 112 thread_stash(td); 113 114 cpu_throw(td, choosethread()); 115 } 116 117 #define RANGEOF(type, start, end) (offsetof(type, end) - offsetof(type, start)) 118 119 /* 120 * System call interface. 121 */ 122 int 123 thr_create(struct thread *td, struct thr_create_args *uap) 124 /* ucontext_t *ctx, thr_id_t *id, int flags */ 125 { 126 struct kse *ke0; 127 struct thread *td0; 128 ucontext_t ctx; 129 int error; 130 131 if ((error = copyin(uap->ctx, &ctx, sizeof(ctx)))) 132 return (error); 133 134 /* Initialize our td. */ 135 td0 = thread_alloc(); 136 137 /* 138 * Try the copyout as soon as we allocate the td so we don't have to 139 * tear things down in a failure case below. 140 */ 141 if ((error = copyout(&td0, uap->id, sizeof(thr_id_t)))) { 142 thread_free(td0); 143 return (error); 144 } 145 146 bzero(&td0->td_startzero, 147 (unsigned)RANGEOF(struct thread, td_startzero, td_endzero)); 148 bcopy(&td->td_startcopy, &td0->td_startcopy, 149 (unsigned) RANGEOF(struct thread, td_startcopy, td_endcopy)); 150 151 td0->td_proc = td->td_proc; 152 PROC_LOCK(td->td_proc); 153 td0->td_sigmask = td->td_sigmask; 154 PROC_UNLOCK(td->td_proc); 155 td0->td_ucred = crhold(td->td_ucred); 156 157 /* Initialize our kse structure. */ 158 ke0 = kse_alloc(); 159 bzero(&ke0->ke_startzero, 160 RANGEOF(struct kse, ke_startzero, ke_endzero)); 161 162 /* Set up our machine context. */ 163 cpu_set_upcall(td0, td); 164 error = set_mcontext(td0, &ctx.uc_mcontext); 165 if (error != 0) { 166 kse_free(ke0); 167 thread_free(td0); 168 goto out; 169 } 170 171 /* Link the thread and kse into the ksegrp and make it runnable. */ 172 mtx_lock_spin(&sched_lock); 173 174 thread_link(td0, td->td_ksegrp); 175 kse_link(ke0, td->td_ksegrp); 176 177 /* Bind this thread and kse together. */ 178 td0->td_kse = ke0; 179 ke0->ke_thread = td0; 180 181 sched_fork_kse(td->td_kse, ke0); 182 sched_fork_thread(td, td0); 183 184 TD_SET_CAN_RUN(td0); 185 if ((uap->flags & THR_SUSPENDED) == 0) 186 setrunqueue(td0); 187 188 mtx_unlock_spin(&sched_lock); 189 190 out: 191 return (error); 192 } 193 194 int 195 thr_self(struct thread *td, struct thr_self_args *uap) 196 /* thr_id_t *id */ 197 { 198 int error; 199 200 if ((error = copyout(&td, uap->id, sizeof(thr_id_t)))) 201 return (error); 202 203 return (0); 204 } 205 206 int 207 thr_exit(struct thread *td, struct thr_exit_args *uap) 208 /* NULL */ 209 { 210 struct proc *p; 211 212 p = td->td_proc; 213 214 PROC_LOCK(p); 215 mtx_lock_spin(&sched_lock); 216 217 /* 218 * This unlocks proc and doesn't return unless this is the last 219 * thread. 220 */ 221 thr_exit1(); 222 mtx_unlock_spin(&sched_lock); 223 224 return (0); 225 } 226 227 int 228 thr_kill(struct thread *td, struct thr_kill_args *uap) 229 /* thr_id_t id, int sig */ 230 { 231 struct thread *ttd; 232 struct proc *p; 233 int error; 234 235 p = td->td_proc; 236 error = 0; 237 PROC_LOCK(p); 238 FOREACH_THREAD_IN_PROC(p, ttd) { 239 if (ttd == uap->id) 240 break; 241 } 242 if (ttd == NULL) { 243 error = ESRCH; 244 goto out; 245 } 246 if (uap->sig == 0) 247 goto out; 248 if (!_SIG_VALID(uap->sig)) { 249 error = EINVAL; 250 goto out; 251 } 252 tdsignal(ttd, uap->sig, SIGTARGET_TD); 253 out: 254 PROC_UNLOCK(p); 255 return (error); 256 } 257