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 /* Unlink the thread from the process and kseg. */ 85 thread_unlink(td); 86 87 ke->ke_state = KES_UNQUEUED; 88 ke->ke_thread = NULL; 89 kse_unlink(ke); 90 sched_exit_kse(TAILQ_NEXT(ke, ke_kglist), ke); 91 92 /* 93 * If we were stopped while waiting for all threads to exit and this 94 * is the last thread wakeup the exiting thread. 95 */ 96 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) 97 if (p->p_numthreads == 1) 98 thread_unsuspend_one(p->p_singlethread); 99 100 PROC_UNLOCK(p); 101 td->td_kse = NULL; 102 td->td_state = TDS_INACTIVE; 103 #if 0 104 td->td_proc = NULL; 105 #endif 106 td->td_ksegrp = NULL; 107 td->td_last_kse = NULL; 108 sched_exit_thread(TAILQ_NEXT(td, td_kglist), td); 109 thread_stash(td); 110 111 cpu_throw(td, choosethread()); 112 } 113 114 #define RANGEOF(type, start, end) (offsetof(type, end) - offsetof(type, start)) 115 116 /* 117 * System call interface. 118 */ 119 int 120 thr_create(struct thread *td, struct thr_create_args *uap) 121 /* ucontext_t *ctx, thr_id_t *id, int flags */ 122 { 123 struct kse *ke0; 124 struct thread *td0; 125 ucontext_t ctx; 126 int error; 127 128 if ((error = copyin(uap->ctx, &ctx, sizeof(ctx)))) 129 return (error); 130 131 /* Initialize our td. */ 132 td0 = thread_alloc(); 133 td0->td_tid = thread_new_tid(); 134 135 /* 136 * Try the copyout as soon as we allocate the td so we don't have to 137 * tear things down in a failure case below. 138 */ 139 if ((error = copyout(&td0, uap->id, sizeof(thr_id_t)))) { 140 thread_free(td0); 141 return (error); 142 } 143 144 bzero(&td0->td_startzero, 145 (unsigned)RANGEOF(struct thread, td_startzero, td_endzero)); 146 bcopy(&td->td_startcopy, &td0->td_startcopy, 147 (unsigned) RANGEOF(struct thread, td_startcopy, td_endcopy)); 148 149 td0->td_proc = td->td_proc; 150 PROC_LOCK(td->td_proc); 151 td0->td_sigmask = td->td_sigmask; 152 PROC_UNLOCK(td->td_proc); 153 td0->td_ucred = crhold(td->td_ucred); 154 155 /* Initialize our kse structure. */ 156 ke0 = kse_alloc(); 157 bzero(&ke0->ke_startzero, 158 RANGEOF(struct kse, ke_startzero, ke_endzero)); 159 160 /* Set up our machine context. */ 161 cpu_set_upcall(td0, td); 162 error = set_mcontext(td0, &ctx.uc_mcontext); 163 if (error != 0) { 164 kse_free(ke0); 165 thread_free(td0); 166 goto out; 167 } 168 169 /* Link the thread and kse into the ksegrp and make it runnable. */ 170 mtx_lock_spin(&sched_lock); 171 172 thread_link(td0, td->td_ksegrp); 173 kse_link(ke0, td->td_ksegrp); 174 175 /* Bind this thread and kse together. */ 176 td0->td_kse = ke0; 177 ke0->ke_thread = td0; 178 179 sched_fork_kse(td->td_kse, ke0); 180 sched_fork_thread(td, td0); 181 182 TD_SET_CAN_RUN(td0); 183 if ((uap->flags & THR_SUSPENDED) == 0) 184 setrunqueue(td0); 185 186 mtx_unlock_spin(&sched_lock); 187 188 out: 189 return (error); 190 } 191 192 int 193 thr_self(struct thread *td, struct thr_self_args *uap) 194 /* thr_id_t *id */ 195 { 196 int error; 197 198 if ((error = copyout(&td, uap->id, sizeof(thr_id_t)))) 199 return (error); 200 201 return (0); 202 } 203 204 int 205 thr_exit(struct thread *td, struct thr_exit_args *uap) 206 /* NULL */ 207 { 208 struct proc *p; 209 210 p = td->td_proc; 211 212 PROC_LOCK(p); 213 mtx_lock_spin(&sched_lock); 214 215 /* 216 * This unlocks proc and doesn't return unless this is the last 217 * thread. 218 */ 219 thr_exit1(); 220 mtx_unlock_spin(&sched_lock); 221 222 return (0); 223 } 224 225 int 226 thr_kill(struct thread *td, struct thr_kill_args *uap) 227 /* thr_id_t id, int sig */ 228 { 229 struct thread *ttd; 230 struct proc *p; 231 int error; 232 233 p = td->td_proc; 234 error = 0; 235 PROC_LOCK(p); 236 FOREACH_THREAD_IN_PROC(p, ttd) { 237 if (ttd == uap->id) 238 break; 239 } 240 if (ttd == NULL) { 241 error = ESRCH; 242 goto out; 243 } 244 if (uap->sig == 0) 245 goto out; 246 if (!_SIG_VALID(uap->sig)) { 247 error = EINVAL; 248 goto out; 249 } 250 tdsignal(ttd, uap->sig, SIGTARGET_TD); 251 out: 252 PROC_UNLOCK(p); 253 return (error); 254 } 255 256 int 257 thr_suspend(struct thread *td, struct thr_suspend_args *uap) 258 /* const struct timespec *timeout */ 259 { 260 struct timespec ts; 261 struct timeval tv; 262 int error; 263 int hz; 264 265 hz = 0; 266 error = 0; 267 if (uap->timeout != NULL) { 268 error = copyin((const void *)uap->timeout, (void *)&ts, 269 sizeof(struct timespec)); 270 if (error != 0) 271 return (error); 272 if (ts.tv_nsec < 0 || ts.tv_nsec > 1000000000) 273 return (EINVAL); 274 if (ts.tv_sec == 0 && ts.tv_nsec == 0) 275 return (ETIMEDOUT); 276 TIMESPEC_TO_TIMEVAL(&tv, &ts); 277 hz = tvtohz(&tv); 278 } 279 PROC_LOCK(td->td_proc); 280 mtx_lock_spin(&sched_lock); 281 if ((td->td_flags & TDF_THRWAKEUP) == 0) { 282 mtx_unlock_spin(&sched_lock); 283 error = msleep((void *)td, &td->td_proc->p_mtx, 284 td->td_priority | PCATCH, "lthr", hz); 285 mtx_lock_spin(&sched_lock); 286 } 287 td->td_flags &= ~TDF_THRWAKEUP; 288 mtx_unlock_spin(&sched_lock); 289 PROC_UNLOCK(td->td_proc); 290 return (error == EWOULDBLOCK ? ETIMEDOUT : error); 291 } 292 293 int 294 thr_wake(struct thread *td, struct thr_wake_args *uap) 295 /* thr_id_t id */ 296 { 297 struct thread *tdsleeper, *ttd; 298 299 tdsleeper = ((struct thread *)uap->id); 300 PROC_LOCK(td->td_proc); 301 FOREACH_THREAD_IN_PROC(td->td_proc, ttd) { 302 if (ttd == tdsleeper) 303 break; 304 } 305 if (ttd == NULL) { 306 PROC_UNLOCK(td->td_proc); 307 return (ESRCH); 308 } 309 mtx_lock_spin(&sched_lock); 310 tdsleeper->td_flags |= TDF_THRWAKEUP; 311 mtx_unlock_spin(&sched_lock); 312 wakeup_one((void *)tdsleeper); 313 PROC_UNLOCK(td->td_proc); 314 return (0); 315 } 316