kern_thread.c revision 104126
199026Sjulian/* 299026Sjulian * Copyright (C) 2001 Julian Elischer <julian@freebsd.org>. 399026Sjulian * All rights reserved. 499026Sjulian * 599026Sjulian * Redistribution and use in source and binary forms, with or without 699026Sjulian * modification, are permitted provided that the following conditions 799026Sjulian * are met: 899026Sjulian * 1. Redistributions of source code must retain the above copyright 999026Sjulian * notice(s), this list of conditions and the following disclaimer as 1099026Sjulian * the first lines of this file unmodified other than the possible 1199026Sjulian * addition of one or more copyright notices. 1299026Sjulian * 2. Redistributions in binary form must reproduce the above copyright 1399026Sjulian * notice(s), this list of conditions and the following disclaimer in the 1499026Sjulian * documentation and/or other materials provided with the distribution. 1599026Sjulian * 1699026Sjulian * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY 1799026Sjulian * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 1899026Sjulian * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 1999026Sjulian * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY 2099026Sjulian * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 2199026Sjulian * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 2299026Sjulian * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 2399026Sjulian * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2499026Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2599026Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 2699026Sjulian * DAMAGE. 2799026Sjulian * 2899026Sjulian * $FreeBSD: head/sys/kern/kern_thread.c 104126 2002-09-29 02:48:37Z julian $ 2999026Sjulian */ 3099026Sjulian 3199026Sjulian#include <sys/param.h> 3299026Sjulian#include <sys/systm.h> 3399026Sjulian#include <sys/kernel.h> 3499026Sjulian#include <sys/lock.h> 3599026Sjulian#include <sys/malloc.h> 3699026Sjulian#include <sys/mutex.h> 3799026Sjulian#include <sys/proc.h> 3899026Sjulian#include <sys/sysctl.h> 3999026Sjulian#include <sys/filedesc.h> 4099026Sjulian#include <sys/tty.h> 4199026Sjulian#include <sys/signalvar.h> 4299026Sjulian#include <sys/sx.h> 4399026Sjulian#include <sys/user.h> 4499026Sjulian#include <sys/jail.h> 4599026Sjulian#include <sys/kse.h> 4699026Sjulian#include <sys/ktr.h> 47103410Smini#include <sys/ucontext.h> 4899026Sjulian 4999026Sjulian#include <vm/vm.h> 5099026Sjulian#include <vm/vm_object.h> 5199026Sjulian#include <vm/pmap.h> 5299026Sjulian#include <vm/uma.h> 5399026Sjulian#include <vm/vm_map.h> 5499026Sjulian 55100273Speter#include <machine/frame.h> 56100273Speter 5799026Sjulian/* 58103367Sjulian * KSEGRP related storage. 5999026Sjulian */ 60103367Sjulianstatic uma_zone_t ksegrp_zone; 61103367Sjulianstatic uma_zone_t kse_zone; 6299026Sjulianstatic uma_zone_t thread_zone; 6399026Sjulian 64103367Sjulian/* DEBUG ONLY */ 6599026SjulianSYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW, 0, "thread allocation"); 6699026Sjulianstatic int oiks_debug = 1; /* 0 disable, 1 printf, 2 enter debugger */ 6799026SjulianSYSCTL_INT(_kern_threads, OID_AUTO, oiks, CTLFLAG_RW, 6899026Sjulian &oiks_debug, 0, "OIKS thread debug"); 6999026Sjulian 70103838Sjulianstatic int max_threads_per_proc = 6; 71103367SjulianSYSCTL_INT(_kern_threads, OID_AUTO, max_per_proc, CTLFLAG_RW, 72103367Sjulian &max_threads_per_proc, 0, "Limit on threads per proc"); 73103367Sjulian 7499026Sjulian#define RANGEOF(type, start, end) (offsetof(type, end) - offsetof(type, start)) 7599026Sjulian 7699026Sjulianstruct threadqueue zombie_threads = TAILQ_HEAD_INITIALIZER(zombie_threads); 7799026Sjulianstruct mtx zombie_thread_lock; 7899026SjulianMTX_SYSINIT(zombie_thread_lock, &zombie_thread_lock, 7999026Sjulian "zombie_thread_lock", MTX_SPIN); 8099026Sjulian 8199026Sjulian/* 8299026Sjulian * Pepare a thread for use. 8399026Sjulian */ 8499026Sjulianstatic void 8599026Sjulianthread_ctor(void *mem, int size, void *arg) 8699026Sjulian{ 8799026Sjulian struct thread *td; 8899026Sjulian 8999026Sjulian KASSERT((size == sizeof(struct thread)), 9099552Speter ("size mismatch: %d != %d\n", size, (int)sizeof(struct thread))); 9199026Sjulian 9299026Sjulian td = (struct thread *)mem; 93103216Sjulian td->td_state = TDS_INACTIVE; 9499026Sjulian td->td_flags |= TDF_UNBOUND; 9599026Sjulian} 9699026Sjulian 9799026Sjulian/* 9899026Sjulian * Reclaim a thread after use. 9999026Sjulian */ 10099026Sjulianstatic void 10199026Sjulianthread_dtor(void *mem, int size, void *arg) 10299026Sjulian{ 10399026Sjulian struct thread *td; 10499026Sjulian 10599026Sjulian KASSERT((size == sizeof(struct thread)), 10699552Speter ("size mismatch: %d != %d\n", size, (int)sizeof(struct thread))); 10799026Sjulian 10899026Sjulian td = (struct thread *)mem; 10999026Sjulian 11099026Sjulian#ifdef INVARIANTS 11199026Sjulian /* Verify that this thread is in a safe state to free. */ 11299026Sjulian switch (td->td_state) { 113103216Sjulian case TDS_INHIBITED: 114103216Sjulian case TDS_RUNNING: 115103216Sjulian case TDS_CAN_RUN: 11699026Sjulian case TDS_RUNQ: 11799026Sjulian /* 11899026Sjulian * We must never unlink a thread that is in one of 11999026Sjulian * these states, because it is currently active. 12099026Sjulian */ 12199026Sjulian panic("bad state for thread unlinking"); 12299026Sjulian /* NOTREACHED */ 123103216Sjulian case TDS_INACTIVE: 12499026Sjulian break; 12599026Sjulian default: 12699026Sjulian panic("bad thread state"); 12799026Sjulian /* NOTREACHED */ 12899026Sjulian } 12999026Sjulian#endif 13099026Sjulian} 13199026Sjulian 13299026Sjulian/* 13399026Sjulian * Initialize type-stable parts of a thread (when newly created). 13499026Sjulian */ 13599026Sjulianstatic void 13699026Sjulianthread_init(void *mem, int size) 13799026Sjulian{ 13899026Sjulian struct thread *td; 13999026Sjulian 14099026Sjulian KASSERT((size == sizeof(struct thread)), 14199552Speter ("size mismatch: %d != %d\n", size, (int)sizeof(struct thread))); 14299026Sjulian 14399026Sjulian td = (struct thread *)mem; 144103312Sjulian mtx_lock(&Giant); 14599026Sjulian pmap_new_thread(td); 146103312Sjulian mtx_unlock(&Giant); 14799026Sjulian cpu_thread_setup(td); 14899026Sjulian} 14999026Sjulian 15099026Sjulian/* 15199026Sjulian * Tear down type-stable parts of a thread (just before being discarded). 15299026Sjulian */ 15399026Sjulianstatic void 15499026Sjulianthread_fini(void *mem, int size) 15599026Sjulian{ 15699026Sjulian struct thread *td; 15799026Sjulian 15899026Sjulian KASSERT((size == sizeof(struct thread)), 15999552Speter ("size mismatch: %d != %d\n", size, (int)sizeof(struct thread))); 16099026Sjulian 16199026Sjulian td = (struct thread *)mem; 16299026Sjulian pmap_dispose_thread(td); 16399026Sjulian} 16499026Sjulian 16599026Sjulian/* 166103410Smini * Fill a ucontext_t with a thread's context information. 167103410Smini * 168103410Smini * This is an analogue to getcontext(3). 169103410Smini */ 170103410Sminivoid 171103410Sminithread_getcontext(struct thread *td, ucontext_t *uc) 172103410Smini{ 173103410Smini 174103464Speter/* 175103464Speter * XXX this is declared in a MD include file, i386/include/ucontext.h but 176103464Speter * is used in MI code. 177103464Speter */ 178103463Speter#ifdef __i386__ 179103410Smini get_mcontext(td, &uc->uc_mcontext); 180103463Speter#endif 181103410Smini uc->uc_sigmask = td->td_proc->p_sigmask; 182103410Smini} 183103410Smini 184103410Smini/* 185103410Smini * Set a thread's context from a ucontext_t. 186103410Smini * 187103410Smini * This is an analogue to setcontext(3). 188103410Smini */ 189103410Sminiint 190103410Sminithread_setcontext(struct thread *td, ucontext_t *uc) 191103410Smini{ 192103410Smini int ret; 193103410Smini 194103464Speter/* 195103464Speter * XXX this is declared in a MD include file, i386/include/ucontext.h but 196103464Speter * is used in MI code. 197103464Speter */ 198103463Speter#ifdef __i386__ 199103410Smini ret = set_mcontext(td, &uc->uc_mcontext); 200103463Speter#else 201103463Speter ret = ENOSYS; 202103463Speter#endif 203103410Smini if (ret == 0) { 204103410Smini SIG_CANTMASK(uc->uc_sigmask); 205103410Smini PROC_LOCK(td->td_proc); 206103410Smini td->td_proc->p_sigmask = uc->uc_sigmask; 207103410Smini PROC_UNLOCK(td->td_proc); 208103410Smini } 209103410Smini return (ret); 210103410Smini} 211103410Smini 212103410Smini/* 21399026Sjulian * Initialize global thread allocation resources. 21499026Sjulian */ 21599026Sjulianvoid 21699026Sjulianthreadinit(void) 21799026Sjulian{ 21899026Sjulian 21999026Sjulian thread_zone = uma_zcreate("THREAD", sizeof (struct thread), 22099026Sjulian thread_ctor, thread_dtor, thread_init, thread_fini, 22199026Sjulian UMA_ALIGN_CACHE, 0); 222103367Sjulian ksegrp_zone = uma_zcreate("KSEGRP", sizeof (struct ksegrp), 223103367Sjulian NULL, NULL, NULL, NULL, 224103367Sjulian UMA_ALIGN_CACHE, 0); 225103367Sjulian kse_zone = uma_zcreate("KSE", sizeof (struct kse), 226103367Sjulian NULL, NULL, NULL, NULL, 227103367Sjulian UMA_ALIGN_CACHE, 0); 22899026Sjulian} 22999026Sjulian 23099026Sjulian/* 231103002Sjulian * Stash an embarasingly extra thread into the zombie thread queue. 23299026Sjulian */ 23399026Sjulianvoid 23499026Sjulianthread_stash(struct thread *td) 23599026Sjulian{ 23699026Sjulian mtx_lock_spin(&zombie_thread_lock); 23799026Sjulian TAILQ_INSERT_HEAD(&zombie_threads, td, td_runq); 23899026Sjulian mtx_unlock_spin(&zombie_thread_lock); 23999026Sjulian} 24099026Sjulian 241103410Smini/* 242103410Smini * Reap zombie threads. 24399026Sjulian */ 24499026Sjulianvoid 24599026Sjulianthread_reap(void) 24699026Sjulian{ 24799026Sjulian struct thread *td_reaped; 24899026Sjulian 24999026Sjulian /* 25099026Sjulian * don't even bother to lock if none at this instant 25199026Sjulian * We really don't care about the next instant.. 25299026Sjulian */ 25399026Sjulian if (!TAILQ_EMPTY(&zombie_threads)) { 25499026Sjulian mtx_lock_spin(&zombie_thread_lock); 25599026Sjulian while (!TAILQ_EMPTY(&zombie_threads)) { 25699026Sjulian td_reaped = TAILQ_FIRST(&zombie_threads); 25799026Sjulian TAILQ_REMOVE(&zombie_threads, td_reaped, td_runq); 25899026Sjulian mtx_unlock_spin(&zombie_thread_lock); 25999026Sjulian thread_free(td_reaped); 26099026Sjulian mtx_lock_spin(&zombie_thread_lock); 26199026Sjulian } 26299026Sjulian mtx_unlock_spin(&zombie_thread_lock); 26399026Sjulian } 26499026Sjulian} 26599026Sjulian 26699026Sjulian/* 267103367Sjulian * Allocate a ksegrp. 268103367Sjulian */ 269103367Sjulianstruct ksegrp * 270103367Sjulianksegrp_alloc(void) 271103367Sjulian{ 272103367Sjulian return (uma_zalloc(ksegrp_zone, M_WAITOK)); 273103367Sjulian} 274103367Sjulian 275103367Sjulian/* 276103367Sjulian * Allocate a kse. 277103367Sjulian */ 278103367Sjulianstruct kse * 279103367Sjuliankse_alloc(void) 280103367Sjulian{ 281103367Sjulian return (uma_zalloc(kse_zone, M_WAITOK)); 282103367Sjulian} 283103367Sjulian 284103367Sjulian/* 28599026Sjulian * Allocate a thread. 28699026Sjulian */ 28799026Sjulianstruct thread * 28899026Sjulianthread_alloc(void) 28999026Sjulian{ 29099026Sjulian thread_reap(); /* check if any zombies to get */ 29199026Sjulian return (uma_zalloc(thread_zone, M_WAITOK)); 29299026Sjulian} 29399026Sjulian 29499026Sjulian/* 295103367Sjulian * Deallocate a ksegrp. 296103367Sjulian */ 297103367Sjulianvoid 298103367Sjulianksegrp_free(struct ksegrp *td) 299103367Sjulian{ 300103367Sjulian uma_zfree(ksegrp_zone, td); 301103367Sjulian} 302103367Sjulian 303103367Sjulian/* 304103367Sjulian * Deallocate a kse. 305103367Sjulian */ 306103367Sjulianvoid 307103367Sjuliankse_free(struct kse *td) 308103367Sjulian{ 309103367Sjulian uma_zfree(kse_zone, td); 310103367Sjulian} 311103367Sjulian 312103367Sjulian/* 31399026Sjulian * Deallocate a thread. 31499026Sjulian */ 31599026Sjulianvoid 31699026Sjulianthread_free(struct thread *td) 31799026Sjulian{ 31899026Sjulian uma_zfree(thread_zone, td); 31999026Sjulian} 32099026Sjulian 32199026Sjulian/* 32299026Sjulian * Store the thread context in the UTS's mailbox. 323104031Sjulian * then add the mailbox at the head of a list we are building in user space. 324104031Sjulian * The list is anchored in the ksegrp structure. 32599026Sjulian */ 32699026Sjulianint 32799026Sjulianthread_export_context(struct thread *td) 32899026Sjulian{ 329104126Sjulian struct proc *p = td->td_proc; 330104031Sjulian struct ksegrp *kg; 331104031Sjulian uintptr_t mbx; 332104031Sjulian void *addr; 33399026Sjulian int error; 334103410Smini ucontext_t uc; 33599026Sjulian 336104031Sjulian /* Export the user/machine context. */ 337104031Sjulian#if 0 338104031Sjulian addr = (caddr_t)td->td_mailbox + 339104031Sjulian offsetof(struct kse_thr_mailbox, tm_context); 340104031Sjulian#else /* if user pointer arithmetic is valid in the kernel */ 341104031Sjulian addr = (void *)(&td->td_mailbox->tm_context); 342100271Speter#endif 343104031Sjulian error = copyin(addr, &uc, sizeof(ucontext_t)); 344103410Smini if (error == 0) { 345103410Smini thread_getcontext(td, &uc); 346104031Sjulian error = copyout(&uc, addr, sizeof(ucontext_t)); 347104031Sjulian 348103410Smini } 349104031Sjulian if (error) { 350104126Sjulian PROC_LOCK(p); 351104126Sjulian psignal(p, SIGSEGV); 352104126Sjulian PROC_UNLOCK(p); 353104031Sjulian return (error); 354104031Sjulian } 355104031Sjulian /* get address in latest mbox of list pointer */ 356104031Sjulian#if 0 357104031Sjulian addr = (caddr_t)td->td_mailbox 358104031Sjulian + offsetof(struct kse_thr_mailbox , tm_next); 359104031Sjulian#else /* if user pointer arithmetic is valid in the kernel */ 360104031Sjulian addr = (void *)(&td->td_mailbox->tm_next); 361104031Sjulian#endif 362104031Sjulian /* 363104031Sjulian * Put the saved address of the previous first 364104031Sjulian * entry into this one 365104031Sjulian */ 366104031Sjulian kg = td->td_ksegrp; 367104031Sjulian for (;;) { 368104031Sjulian mbx = (uintptr_t)kg->kg_completed; 369104031Sjulian if (suword(addr, mbx)) { 370104126Sjulian PROC_LOCK(p); 371104126Sjulian psignal(p, SIGSEGV); 372104126Sjulian PROC_UNLOCK(p); 373104031Sjulian return (EFAULT); 374104031Sjulian } 375104126Sjulian PROC_LOCK(p); 376104031Sjulian if (mbx == (uintptr_t)kg->kg_completed) { 377104031Sjulian kg->kg_completed = td->td_mailbox; 378104126Sjulian PROC_UNLOCK(p); 379104031Sjulian break; 380104031Sjulian } 381104126Sjulian PROC_UNLOCK(p); 382104031Sjulian } 383104031Sjulian return (0); 384104031Sjulian} 38599026Sjulian 386104031Sjulian/* 387104031Sjulian * Take the list of completed mailboxes for this KSEGRP and put them on this 388104031Sjulian * KSE's mailbox as it's the next one going up. 389104031Sjulian */ 390104031Sjulianstatic int 391104031Sjulianthread_link_mboxes(struct ksegrp *kg, struct kse *ke) 392104031Sjulian{ 393104126Sjulian struct proc *p = kg->kg_proc; 394104031Sjulian void *addr; 395104031Sjulian uintptr_t mbx; 396104031Sjulian 397104031Sjulian#if 0 398104031Sjulian addr = (caddr_t)ke->ke_mailbox 399104031Sjulian + offsetof(struct kse_mailbox, km_completed); 400104031Sjulian#else /* if user pointer arithmetic is valid in the kernel */ 401104031Sjulian addr = (void *)(&ke->ke_mailbox->km_completed); 402104031Sjulian#endif 403104031Sjulian for (;;) { 404104031Sjulian mbx = (uintptr_t)kg->kg_completed; 405104031Sjulian if (suword(addr, mbx)) { 406104126Sjulian PROC_LOCK(p); 407104126Sjulian psignal(p, SIGSEGV); 408104126Sjulian PROC_UNLOCK(p); 409104031Sjulian return (EFAULT); 410104031Sjulian } 411104031Sjulian /* XXXKSE could use atomic CMPXCH here */ 412104126Sjulian PROC_LOCK(p); 413104031Sjulian if (mbx == (uintptr_t)kg->kg_completed) { 414104031Sjulian kg->kg_completed = NULL; 415104126Sjulian PROC_UNLOCK(p); 416104031Sjulian break; 417104031Sjulian } 418104126Sjulian PROC_UNLOCK(p); 41999026Sjulian } 420104031Sjulian return (0); 42199026Sjulian} 42299026Sjulian 42399026Sjulian/* 42499026Sjulian * Discard the current thread and exit from its context. 42599026Sjulian * 42699026Sjulian * Because we can't free a thread while we're operating under its context, 42799026Sjulian * push the current thread into our KSE's ke_tdspare slot, freeing the 42899026Sjulian * thread that might be there currently. Because we know that only this 42999026Sjulian * processor will run our KSE, we needn't worry about someone else grabbing 43099026Sjulian * our context before we do a cpu_throw. 43199026Sjulian */ 43299026Sjulianvoid 43399026Sjulianthread_exit(void) 43499026Sjulian{ 43599026Sjulian struct thread *td; 43699026Sjulian struct kse *ke; 43799026Sjulian struct proc *p; 43899026Sjulian struct ksegrp *kg; 43999026Sjulian 44099026Sjulian td = curthread; 44199026Sjulian kg = td->td_ksegrp; 44299026Sjulian p = td->td_proc; 44399026Sjulian ke = td->td_kse; 44499026Sjulian 44599026Sjulian mtx_assert(&sched_lock, MA_OWNED); 446102581Sjulian KASSERT(p != NULL, ("thread exiting without a process")); 447102581Sjulian KASSERT(ke != NULL, ("thread exiting without a kse")); 448102581Sjulian KASSERT(kg != NULL, ("thread exiting without a kse group")); 44999026Sjulian PROC_LOCK_ASSERT(p, MA_OWNED); 45099026Sjulian CTR1(KTR_PROC, "thread_exit: thread %p", td); 45199026Sjulian KASSERT(!mtx_owned(&Giant), ("dying thread owns giant")); 45299026Sjulian 45399026Sjulian if (ke->ke_tdspare != NULL) { 454103216Sjulian thread_stash(ke->ke_tdspare); 45599026Sjulian ke->ke_tdspare = NULL; 45699026Sjulian } 45799026Sjulian cpu_thread_exit(td); /* XXXSMP */ 45899026Sjulian 459102581Sjulian /* 460103002Sjulian * The last thread is left attached to the process 461103002Sjulian * So that the whole bundle gets recycled. Skip 462103002Sjulian * all this stuff. 463102581Sjulian */ 464103002Sjulian if (p->p_numthreads > 1) { 465103002Sjulian /* Reassign this thread's KSE. */ 466103002Sjulian ke->ke_thread = NULL; 467103002Sjulian td->td_kse = NULL; 468103002Sjulian ke->ke_state = KES_UNQUEUED; 469103002Sjulian kse_reassign(ke); 470103002Sjulian 471103002Sjulian /* Unlink this thread from its proc. and the kseg */ 472103002Sjulian TAILQ_REMOVE(&p->p_threads, td, td_plist); 473103002Sjulian p->p_numthreads--; 474103002Sjulian TAILQ_REMOVE(&kg->kg_threads, td, td_kglist); 475103002Sjulian kg->kg_numthreads--; 476103002Sjulian /* 477103002Sjulian * The test below is NOT true if we are the 478103002Sjulian * sole exiting thread. P_STOPPED_SNGL is unset 479103002Sjulian * in exit1() after it is the only survivor. 480103002Sjulian */ 481103002Sjulian if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 482103002Sjulian if (p->p_numthreads == p->p_suspcount) { 483103216Sjulian thread_unsuspend_one(p->p_singlethread); 484103002Sjulian } 48599026Sjulian } 486103002Sjulian PROC_UNLOCK(p); 487103216Sjulian td->td_state = TDS_INACTIVE; 488103002Sjulian td->td_proc = NULL; 489103002Sjulian td->td_ksegrp = NULL; 490103002Sjulian td->td_last_kse = NULL; 491103002Sjulian ke->ke_tdspare = td; 492103002Sjulian } else { 493103002Sjulian PROC_UNLOCK(p); 49499026Sjulian } 495103002Sjulian 49699026Sjulian cpu_throw(); 49799026Sjulian /* NOTREACHED */ 49899026Sjulian} 49999026Sjulian 50099026Sjulian/* 50199026Sjulian * Link a thread to a process. 502103002Sjulian * set up anything that needs to be initialized for it to 503103002Sjulian * be used by the process. 50499026Sjulian * 50599026Sjulian * Note that we do not link to the proc's ucred here. 50699026Sjulian * The thread is linked as if running but no KSE assigned. 50799026Sjulian */ 50899026Sjulianvoid 50999026Sjulianthread_link(struct thread *td, struct ksegrp *kg) 51099026Sjulian{ 51199026Sjulian struct proc *p; 51299026Sjulian 51399026Sjulian p = kg->kg_proc; 514103216Sjulian td->td_state = TDS_INACTIVE; 51599026Sjulian td->td_proc = p; 51699026Sjulian td->td_ksegrp = kg; 51799026Sjulian td->td_last_kse = NULL; 51899026Sjulian 519103002Sjulian LIST_INIT(&td->td_contested); 520103002Sjulian callout_init(&td->td_slpcallout, 1); 52199026Sjulian TAILQ_INSERT_HEAD(&p->p_threads, td, td_plist); 52299026Sjulian TAILQ_INSERT_HEAD(&kg->kg_threads, td, td_kglist); 52399026Sjulian p->p_numthreads++; 52499026Sjulian kg->kg_numthreads++; 525103367Sjulian if (oiks_debug && p->p_numthreads > max_threads_per_proc) { 52699026Sjulian printf("OIKS %d\n", p->p_numthreads); 52799026Sjulian if (oiks_debug > 1) 52899026Sjulian Debugger("OIKS"); 52999026Sjulian } 53099026Sjulian td->td_kse = NULL; 53199026Sjulian} 53299026Sjulian 53399026Sjulian/* 534103410Smini * Create a thread and schedule it for upcall on the KSE given. 53599026Sjulian */ 53699026Sjulianstruct thread * 53799026Sjulianthread_schedule_upcall(struct thread *td, struct kse *ke) 53899026Sjulian{ 53999026Sjulian struct thread *td2; 54099026Sjulian 54199026Sjulian mtx_assert(&sched_lock, MA_OWNED); 54299026Sjulian if (ke->ke_tdspare != NULL) { 54399026Sjulian td2 = ke->ke_tdspare; 54499026Sjulian ke->ke_tdspare = NULL; 54599026Sjulian } else { 54699026Sjulian mtx_unlock_spin(&sched_lock); 54799026Sjulian td2 = thread_alloc(); 54899026Sjulian mtx_lock_spin(&sched_lock); 54999026Sjulian } 55099026Sjulian CTR3(KTR_PROC, "thread_schedule_upcall: thread %p (pid %d, %s)", 55199026Sjulian td, td->td_proc->p_pid, td->td_proc->p_comm); 552103072Sjulian bzero(&td2->td_startzero, 553103002Sjulian (unsigned)RANGEOF(struct thread, td_startzero, td_endzero)); 554103002Sjulian bcopy(&td->td_startcopy, &td2->td_startcopy, 555103002Sjulian (unsigned) RANGEOF(struct thread, td_startcopy, td_endcopy)); 55699026Sjulian thread_link(td2, ke->ke_ksegrp); 557103410Smini cpu_set_upcall(td2, td->td_pcb); 558103410Smini bcopy(td->td_frame, td2->td_frame, sizeof(struct trapframe)); 559103410Smini /* 560103410Smini * The user context for this thread is selected when we choose 561103410Smini * a KSE and return to userland on it. All we need do here is 562103410Smini * note that the thread exists in order to perform an upcall. 563103410Smini * 564103410Smini * Since selecting a KSE to perform the upcall involves locking 565103410Smini * that KSE's context to our upcall, its best to wait until the 566103410Smini * last possible moment before grabbing a KSE. We do this in 567103410Smini * userret(). 568103410Smini */ 56999026Sjulian td2->td_ucred = crhold(td->td_ucred); 57099026Sjulian td2->td_flags = TDF_UNBOUND|TDF_UPCALLING; 571103216Sjulian TD_SET_CAN_RUN(td2); 57299026Sjulian setrunqueue(td2); 57399026Sjulian return (td2); 57499026Sjulian} 57599026Sjulian 57699026Sjulian/* 577103410Smini * Schedule an upcall to notify a KSE process recieved signals. 57899026Sjulian * 579103410Smini * XXX - Modifying a sigset_t like this is totally bogus. 580103410Smini */ 581103410Sministruct thread * 582103410Sminisignal_upcall(struct proc *p, int sig) 583103410Smini{ 584103410Smini struct thread *td, *td2; 585103410Smini struct kse *ke; 586103410Smini sigset_t ss; 587103410Smini int error; 588103410Smini 589103410Smini PROC_LOCK_ASSERT(p, MA_OWNED); 590103410Smini 591103410Smini td = FIRST_THREAD_IN_PROC(p); 592103410Smini ke = td->td_kse; 593103410Smini PROC_UNLOCK(p); 594103410Smini error = copyin(&ke->ke_mailbox->km_sigscaught, &ss, sizeof(sigset_t)); 595103410Smini PROC_LOCK(p); 596103410Smini if (error) 597103410Smini return (NULL); 598103410Smini SIGADDSET(ss, sig); 599103410Smini PROC_UNLOCK(p); 600103410Smini error = copyout(&ss, &ke->ke_mailbox->km_sigscaught, sizeof(sigset_t)); 601103410Smini PROC_LOCK(p); 602103410Smini if (error) 603103410Smini return (NULL); 604103410Smini mtx_lock_spin(&sched_lock); 605103410Smini td2 = thread_schedule_upcall(td, ke); 606103410Smini mtx_unlock_spin(&sched_lock); 607103410Smini return (td2); 608103410Smini} 609103410Smini 610103410Smini/* 611103410Smini * Consider whether or not an upcall should be made, and update the 612103410Smini * TDF_UPCALLING flag appropriately. 613103410Smini * 614103410Smini * This function is called when the current thread had been bound to a user 615103410Smini * thread that performed a syscall that blocked, and is now returning. 616103410Smini * Got that? syscall -> msleep -> wakeup -> syscall_return -> us. 617103410Smini * 618103410Smini * This thread will be returned to the UTS in its mailbox as a completed 619103410Smini * thread. We need to decide whether or not to perform an upcall now, 620103410Smini * or simply queue the thread for later. 621103410Smini * 622103410Smini * XXXKSE Future enhancement: We could also return back to 623103410Smini * the thread if we haven't had to do an upcall since then. 624103410Smini * If the KSE's copy is == the thread's copy, and there are 625103410Smini * no other completed threads. 626103410Smini */ 627103410Sministatic int 628103838Sjulianthread_consider_upcalling(struct thread *td) 629103410Smini{ 630103838Sjulian struct proc *p; 631103838Sjulian struct ksegrp *kg; 632103410Smini int error; 633103410Smini 634103410Smini /* 635103410Smini * Save the thread's context, and link it 636104031Sjulian * into the KSEGRP's list of completed threads. 637103410Smini */ 638103410Smini error = thread_export_context(td); 639104031Sjulian td->td_flags &= ~TDF_UNBOUND; 640103410Smini td->td_mailbox = NULL; 641103410Smini if (error) 642103410Smini /* 643103410Smini * Failing to do the KSE operation just defaults 644103410Smini * back to synchonous operation, so just return from 645103410Smini * the syscall. 646103410Smini */ 647103410Smini return (error); 648103410Smini 649103410Smini /* 650104031Sjulian * Decide whether to perform an upcall now. 651103410Smini */ 652103410Smini /* Make sure there are no other threads waiting to run. */ 653103838Sjulian p = td->td_proc; 654103838Sjulian kg = td->td_ksegrp; 655103838Sjulian PROC_LOCK(p); 656103838Sjulian mtx_lock_spin(&sched_lock); 657103838Sjulian /* bogus test, ok for testing though */ 658103838Sjulian if (TAILQ_FIRST(&kg->kg_runq) && 659103838Sjulian (TAILQ_LAST(&kg->kg_runq, threadqueue) 660103838Sjulian != kg->kg_last_assigned)) { 661103410Smini /* 662103410Smini * Another thread in this KSEG needs to run. 663103410Smini * Switch to it instead of performing an upcall, 664103410Smini * abondoning this thread. Perform the upcall 665103410Smini * later; discard this thread for now. 666103410Smini * 667103410Smini * XXXKSE - As for the other threads to run; 668103410Smini * we COULD rush through all the threads 669103410Smini * in this KSEG at this priority, or we 670103410Smini * could throw the ball back into the court 671103410Smini * and just run the highest prio kse available. 672103410Smini * What is OUR priority? The priority of the highest 673103410Smini * sycall waiting to be returned? 674103410Smini * For now, just let another KSE run (easiest). 675103410Smini */ 676103410Smini thread_exit(); /* Abandon current thread. */ 677103410Smini /* NOTREACHED */ 678104031Sjulian } 679104031Sjulian /* 680104031Sjulian * Perform an upcall now. 681104031Sjulian * 682104031Sjulian * XXXKSE - Assumes we are going to userland, and not 683104031Sjulian * nested in the kernel. 684104031Sjulian */ 685104031Sjulian td->td_flags |= TDF_UPCALLING; 686103838Sjulian mtx_unlock_spin(&sched_lock); 687103838Sjulian PROC_UNLOCK(p); 688103410Smini return (0); 689103410Smini} 690103410Smini 691103410Smini/* 692103410Smini * The extra work we go through if we are a threaded process when we 693103410Smini * return to userland. 694103410Smini * 69599026Sjulian * If we are a KSE process and returning to user mode, check for 69699026Sjulian * extra work to do before we return (e.g. for more syscalls 69799026Sjulian * to complete first). If we were in a critical section, we should 69899026Sjulian * just return to let it finish. Same if we were in the UTS (in 699103410Smini * which case the mailbox's context's busy indicator will be set). 700103410Smini * The only traps we suport will have set the mailbox. 701103410Smini * We will clear it here. 70299026Sjulian */ 70399026Sjulianint 704103838Sjulianthread_userret(struct thread *td, struct trapframe *frame) 70599026Sjulian{ 706103410Smini int error; 707104031Sjulian int unbound; 708104031Sjulian struct kse *ke; 70999026Sjulian 710104031Sjulian /* Make the thread bound from now on, but remember what it was. */ 711104031Sjulian unbound = td->td_flags & TDF_UNBOUND; 712104031Sjulian td->td_flags &= ~TDF_UNBOUND; 713103410Smini /* 714103410Smini * Ensure that we have a spare thread available. 715103410Smini */ 716104031Sjulian ke = td->td_kse; 71799026Sjulian if (ke->ke_tdspare == NULL) { 718103410Smini mtx_lock(&Giant); 71999026Sjulian ke->ke_tdspare = thread_alloc(); 720103410Smini mtx_unlock(&Giant); 72199026Sjulian } 722103410Smini /* 723104031Sjulian * Originally bound threads need no additional work. 724103410Smini */ 725104031Sjulian if (unbound == 0) 726103410Smini return (0); 727103410Smini error = 0; 728103410Smini /* 729103410Smini * Decide whether or not we should perform an upcall now. 730103410Smini */ 731104031Sjulian if (((td->td_flags & TDF_UPCALLING) == 0) && unbound) { 732104031Sjulian /* if we have other threads to run we will not return */ 733104031Sjulian if ((error = thread_consider_upcalling(td))) 734104031Sjulian return (error); /* coundn't go async , just go sync. */ 735103410Smini } 736103410Smini if (td->td_flags & TDF_UPCALLING) { 73799026Sjulian /* 738103410Smini * There is no more work to do and we are going to ride 739104031Sjulian * this thead/KSE up to userland as an upcall. 74099026Sjulian */ 741103410Smini CTR3(KTR_PROC, "userret: upcall thread %p (pid %d, %s)", 742103859Sjulian td, td->td_proc->p_pid, td->td_proc->p_comm); 74399026Sjulian 74499026Sjulian /* 745103410Smini * Set user context to the UTS. 74699026Sjulian */ 747104031Sjulian cpu_set_upcall_kse(td, ke); 748104031Sjulian 749104031Sjulian /* 750104031Sjulian * Put any completed mailboxes on this KSE's list. 751104031Sjulian */ 752104031Sjulian error = thread_link_mboxes(td->td_ksegrp, ke); 753103410Smini if (error) 754104031Sjulian goto bad; 75599026Sjulian 75699026Sjulian /* 757103410Smini * Set state and mailbox. 75899026Sjulian */ 759103410Smini td->td_flags &= ~TDF_UPCALLING; 760104031Sjulian#if 0 761104031Sjulian error = suword((caddr_t)ke->ke_mailbox + 762103410Smini offsetof(struct kse_mailbox, km_curthread), 763103410Smini 0); 764104031Sjulian#else /* if user pointer arithmetic is ok in the kernel */ 765104031Sjulian error = suword((caddr_t)&ke->ke_mailbox->km_curthread, 0); 766104031Sjulian#endif 767104031Sjulian if (error) 768104031Sjulian goto bad; 769103410Smini } 770103410Smini /* 771103410Smini * Stop any chance that we may be separated from 772103410Smini * the KSE we are currently on. This is "biting the bullet", 773103410Smini * we are committing to go to user space as as this KSE here. 774103410Smini */ 77599026Sjulian return (error); 776104031Sjulianbad: 777104031Sjulian /* 778104031Sjulian * Things are going to be so screwed we should just kill the process. 779104031Sjulian * how do we do that? 780104031Sjulian */ 781104031Sjulian panic ("thread_userret.. need to kill proc..... how?"); 78299026Sjulian} 78399026Sjulian 78499026Sjulian/* 78599026Sjulian * Enforce single-threading. 78699026Sjulian * 78799026Sjulian * Returns 1 if the caller must abort (another thread is waiting to 78899026Sjulian * exit the process or similar). Process is locked! 78999026Sjulian * Returns 0 when you are successfully the only thread running. 79099026Sjulian * A process has successfully single threaded in the suspend mode when 79199026Sjulian * There are no threads in user mode. Threads in the kernel must be 79299026Sjulian * allowed to continue until they get to the user boundary. They may even 79399026Sjulian * copy out their return values and data before suspending. They may however be 79499026Sjulian * accellerated in reaching the user boundary as we will wake up 79599026Sjulian * any sleeping threads that are interruptable. (PCATCH). 79699026Sjulian */ 79799026Sjulianint 79899026Sjulianthread_single(int force_exit) 79999026Sjulian{ 80099026Sjulian struct thread *td; 80199026Sjulian struct thread *td2; 80299026Sjulian struct proc *p; 80399026Sjulian 80499026Sjulian td = curthread; 80599026Sjulian p = td->td_proc; 80699026Sjulian PROC_LOCK_ASSERT(p, MA_OWNED); 80799026Sjulian KASSERT((td != NULL), ("curthread is NULL")); 80899026Sjulian 80999026Sjulian if ((p->p_flag & P_KSES) == 0) 81099026Sjulian return (0); 81199026Sjulian 812100648Sjulian /* Is someone already single threading? */ 813100648Sjulian if (p->p_singlethread) 81499026Sjulian return (1); 81599026Sjulian 816102950Sdavidxu if (force_exit == SINGLE_EXIT) 81799026Sjulian p->p_flag |= P_SINGLE_EXIT; 81899026Sjulian else 81999026Sjulian p->p_flag &= ~P_SINGLE_EXIT; 820102950Sdavidxu p->p_flag |= P_STOPPED_SINGLE; 82199026Sjulian p->p_singlethread = td; 82299026Sjulian while ((p->p_numthreads - p->p_suspcount) != 1) { 823103216Sjulian mtx_lock_spin(&sched_lock); 82499026Sjulian FOREACH_THREAD_IN_PROC(p, td2) { 82599026Sjulian if (td2 == td) 82699026Sjulian continue; 827103216Sjulian if (TD_IS_INHIBITED(td2)) { 828103216Sjulian if (TD_IS_SUSPENDED(td2)) { 829103216Sjulian if (force_exit == SINGLE_EXIT) { 830103216Sjulian thread_unsuspend_one(td2); 831103216Sjulian } 83299026Sjulian } 833103216Sjulian if ( TD_IS_SLEEPING(td2)) { 834103216Sjulian if (td2->td_flags & TDF_CVWAITQ) 835103216Sjulian cv_waitq_remove(td2); 836103216Sjulian else 837103216Sjulian unsleep(td2); 838103216Sjulian break; 839103216Sjulian } 840103216Sjulian if (TD_CAN_RUN(td2)) 841103216Sjulian setrunqueue(td2); 84299026Sjulian } 84399026Sjulian } 84499026Sjulian /* 84599026Sjulian * Wake us up when everyone else has suspended. 846100648Sjulian * In the mean time we suspend as well. 84799026Sjulian */ 848103216Sjulian thread_suspend_one(td); 84999026Sjulian mtx_unlock(&Giant); 85099026Sjulian PROC_UNLOCK(p); 85199026Sjulian mi_switch(); 85299026Sjulian mtx_unlock_spin(&sched_lock); 85399026Sjulian mtx_lock(&Giant); 85499026Sjulian PROC_LOCK(p); 85599026Sjulian } 85699026Sjulian return (0); 85799026Sjulian} 85899026Sjulian 85999026Sjulian/* 86099026Sjulian * Called in from locations that can safely check to see 86199026Sjulian * whether we have to suspend or at least throttle for a 86299026Sjulian * single-thread event (e.g. fork). 86399026Sjulian * 86499026Sjulian * Such locations include userret(). 86599026Sjulian * If the "return_instead" argument is non zero, the thread must be able to 86699026Sjulian * accept 0 (caller may continue), or 1 (caller must abort) as a result. 86799026Sjulian * 86899026Sjulian * The 'return_instead' argument tells the function if it may do a 86999026Sjulian * thread_exit() or suspend, or whether the caller must abort and back 87099026Sjulian * out instead. 87199026Sjulian * 87299026Sjulian * If the thread that set the single_threading request has set the 87399026Sjulian * P_SINGLE_EXIT bit in the process flags then this call will never return 87499026Sjulian * if 'return_instead' is false, but will exit. 87599026Sjulian * 87699026Sjulian * P_SINGLE_EXIT | return_instead == 0| return_instead != 0 87799026Sjulian *---------------+--------------------+--------------------- 87899026Sjulian * 0 | returns 0 | returns 0 or 1 87999026Sjulian * | when ST ends | immediatly 88099026Sjulian *---------------+--------------------+--------------------- 88199026Sjulian * 1 | thread exits | returns 1 88299026Sjulian * | | immediatly 88399026Sjulian * 0 = thread_exit() or suspension ok, 88499026Sjulian * other = return error instead of stopping the thread. 88599026Sjulian * 88699026Sjulian * While a full suspension is under effect, even a single threading 88799026Sjulian * thread would be suspended if it made this call (but it shouldn't). 88899026Sjulian * This call should only be made from places where 88999026Sjulian * thread_exit() would be safe as that may be the outcome unless 89099026Sjulian * return_instead is set. 89199026Sjulian */ 89299026Sjulianint 89399026Sjulianthread_suspend_check(int return_instead) 89499026Sjulian{ 89599026Sjulian struct thread *td = curthread; 89699026Sjulian struct proc *p = td->td_proc; 89799026Sjulian 89899026Sjulian td = curthread; 89999026Sjulian p = td->td_proc; 90099026Sjulian PROC_LOCK_ASSERT(p, MA_OWNED); 90199026Sjulian while (P_SHOULDSTOP(p)) { 902102950Sdavidxu if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 90399026Sjulian KASSERT(p->p_singlethread != NULL, 90499026Sjulian ("singlethread not set")); 90599026Sjulian /* 906100648Sjulian * The only suspension in action is a 907100648Sjulian * single-threading. Single threader need not stop. 908100646Sjulian * XXX Should be safe to access unlocked 909100646Sjulian * as it can only be set to be true by us. 91099026Sjulian */ 911100648Sjulian if (p->p_singlethread == td) 91299026Sjulian return (0); /* Exempt from stopping. */ 91399026Sjulian } 914100648Sjulian if (return_instead) 91599026Sjulian return (1); 91699026Sjulian 91799026Sjulian /* 91899026Sjulian * If the process is waiting for us to exit, 91999026Sjulian * this thread should just suicide. 920102950Sdavidxu * Assumes that P_SINGLE_EXIT implies P_STOPPED_SINGLE. 92199026Sjulian */ 92299026Sjulian if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) { 92399026Sjulian mtx_lock_spin(&sched_lock); 92499026Sjulian while (mtx_owned(&Giant)) 92599026Sjulian mtx_unlock(&Giant); 92699026Sjulian thread_exit(); 92799026Sjulian } 92899026Sjulian 92999026Sjulian /* 93099026Sjulian * When a thread suspends, it just 93199026Sjulian * moves to the processes's suspend queue 93299026Sjulian * and stays there. 93399026Sjulian * 93499026Sjulian * XXXKSE if TDF_BOUND is true 93599026Sjulian * it will not release it's KSE which might 93699026Sjulian * lead to deadlock if there are not enough KSEs 93799026Sjulian * to complete all waiting threads. 93899026Sjulian * Maybe be able to 'lend' it out again. 93999026Sjulian * (lent kse's can not go back to userland?) 94099026Sjulian * and can only be lent in STOPPED state. 94199026Sjulian */ 942102238Sjulian mtx_lock_spin(&sched_lock); 943102950Sdavidxu if ((p->p_flag & P_STOPPED_SIG) && 944102238Sjulian (p->p_suspcount+1 == p->p_numthreads)) { 945102238Sjulian mtx_unlock_spin(&sched_lock); 946102238Sjulian PROC_LOCK(p->p_pptr); 947102238Sjulian if ((p->p_pptr->p_procsig->ps_flag & 948102238Sjulian PS_NOCLDSTOP) == 0) { 949102238Sjulian psignal(p->p_pptr, SIGCHLD); 950102238Sjulian } 951102238Sjulian PROC_UNLOCK(p->p_pptr); 952103055Sjulian mtx_lock_spin(&sched_lock); 953102238Sjulian } 95499026Sjulian mtx_assert(&Giant, MA_NOTOWNED); 955103216Sjulian thread_suspend_one(td); 95699026Sjulian PROC_UNLOCK(p); 957102950Sdavidxu if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 958100632Sjulian if (p->p_numthreads == p->p_suspcount) { 959103216Sjulian thread_unsuspend_one(p->p_singlethread); 960100632Sjulian } 961100632Sjulian } 962100594Sjulian p->p_stats->p_ru.ru_nivcsw++; 96399026Sjulian mi_switch(); 96499026Sjulian mtx_unlock_spin(&sched_lock); 96599026Sjulian PROC_LOCK(p); 96699026Sjulian } 96799026Sjulian return (0); 96899026Sjulian} 96999026Sjulian 970102898Sdavidxuvoid 971102898Sdavidxuthread_suspend_one(struct thread *td) 972102898Sdavidxu{ 973102898Sdavidxu struct proc *p = td->td_proc; 974102898Sdavidxu 975102898Sdavidxu mtx_assert(&sched_lock, MA_OWNED); 976102898Sdavidxu p->p_suspcount++; 977103216Sjulian TD_SET_SUSPENDED(td); 978102898Sdavidxu TAILQ_INSERT_TAIL(&p->p_suspended, td, td_runq); 979103216Sjulian /* 980103216Sjulian * Hack: If we are suspending but are on the sleep queue 981103216Sjulian * then we are in msleep or the cv equivalent. We 982103216Sjulian * want to look like we have two Inhibitors. 983103216Sjulian */ 984103216Sjulian if (TD_ON_SLEEPQ(td)) 985103216Sjulian TD_SET_SLEEPING(td); 986102898Sdavidxu} 987102898Sdavidxu 988102898Sdavidxuvoid 989102898Sdavidxuthread_unsuspend_one(struct thread *td) 990102898Sdavidxu{ 991102898Sdavidxu struct proc *p = td->td_proc; 992102898Sdavidxu 993102898Sdavidxu mtx_assert(&sched_lock, MA_OWNED); 994102898Sdavidxu TAILQ_REMOVE(&p->p_suspended, td, td_runq); 995103216Sjulian TD_CLR_SUSPENDED(td); 996102898Sdavidxu p->p_suspcount--; 997103216Sjulian setrunnable(td); 998102898Sdavidxu} 999102898Sdavidxu 100099026Sjulian/* 100199026Sjulian * Allow all threads blocked by single threading to continue running. 100299026Sjulian */ 100399026Sjulianvoid 100499026Sjulianthread_unsuspend(struct proc *p) 100599026Sjulian{ 100699026Sjulian struct thread *td; 100799026Sjulian 1008100646Sjulian mtx_assert(&sched_lock, MA_OWNED); 100999026Sjulian PROC_LOCK_ASSERT(p, MA_OWNED); 101099026Sjulian if (!P_SHOULDSTOP(p)) { 101199026Sjulian while (( td = TAILQ_FIRST(&p->p_suspended))) { 1012102898Sdavidxu thread_unsuspend_one(td); 101399026Sjulian } 1014102950Sdavidxu } else if ((P_SHOULDSTOP(p) == P_STOPPED_SINGLE) && 101599026Sjulian (p->p_numthreads == p->p_suspcount)) { 101699026Sjulian /* 101799026Sjulian * Stopping everything also did the job for the single 101899026Sjulian * threading request. Now we've downgraded to single-threaded, 101999026Sjulian * let it continue. 102099026Sjulian */ 1021102898Sdavidxu thread_unsuspend_one(p->p_singlethread); 102299026Sjulian } 102399026Sjulian} 102499026Sjulian 102599026Sjulianvoid 102699026Sjulianthread_single_end(void) 102799026Sjulian{ 102899026Sjulian struct thread *td; 102999026Sjulian struct proc *p; 103099026Sjulian 103199026Sjulian td = curthread; 103299026Sjulian p = td->td_proc; 103399026Sjulian PROC_LOCK_ASSERT(p, MA_OWNED); 1034102950Sdavidxu p->p_flag &= ~P_STOPPED_SINGLE; 103599026Sjulian p->p_singlethread = NULL; 1036102292Sjulian /* 1037102292Sjulian * If there are other threads they mey now run, 1038102292Sjulian * unless of course there is a blanket 'stop order' 1039102292Sjulian * on the process. The single threader must be allowed 1040102292Sjulian * to continue however as this is a bad place to stop. 1041102292Sjulian */ 1042102292Sjulian if ((p->p_numthreads != 1) && (!P_SHOULDSTOP(p))) { 1043102292Sjulian mtx_lock_spin(&sched_lock); 1044102292Sjulian while (( td = TAILQ_FIRST(&p->p_suspended))) { 1045103216Sjulian thread_unsuspend_one(td); 1046102292Sjulian } 1047102292Sjulian mtx_unlock_spin(&sched_lock); 1048102292Sjulian } 104999026Sjulian} 105099026Sjulian 1051102292Sjulian 1052