kern_thread.c revision 103838
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 103838 2002-09-23 06:14:30Z 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.
32399026Sjulian */
32499026Sjulianint
32599026Sjulianthread_export_context(struct thread *td)
32699026Sjulian{
32799026Sjulian	struct kse *ke;
32899026Sjulian	uintptr_t td2_mbx;
32999026Sjulian	void *addr1;
33099026Sjulian	void *addr2;
33199026Sjulian	int error;
332103410Smini	ucontext_t uc;
333103838Sjulian	int unbound;
33499026Sjulian
335103838Sjulian	unbound = (td->td_flags & TDF_UNBOUND);
336103838Sjulian	td->td_flags &= ~TDF_UNBOUND;
337100271Speter#ifdef __ia64__
338100271Speter	td2_mbx = 0;		/* pacify gcc (!) */
339100271Speter#endif
340103410Smini	/* Export the user/machine context. */
341103410Smini	error = copyin((caddr_t)td->td_mailbox +
342103410Smini	    offsetof(struct thread_mailbox, tm_context),
343103410Smini	    &uc,
344103410Smini	    sizeof(ucontext_t));
345103410Smini	if (error == 0) {
346103410Smini		thread_getcontext(td, &uc);
347103410Smini		error = copyout(&uc, (caddr_t)td->td_mailbox +
348103410Smini		offsetof(struct thread_mailbox, tm_context),
349103410Smini		sizeof(ucontext_t));
350103410Smini	}
35199026Sjulian
35299026Sjulian	ke = td->td_kse;
35399026Sjulian	addr1 = (caddr_t)ke->ke_mailbox
354103410Smini			+ offsetof(struct kse_mailbox, km_completed);
35599026Sjulian	addr2 = (caddr_t)td->td_mailbox
356103410Smini			+ offsetof(struct thread_mailbox , tm_next);
35799026Sjulian	/* Then link it into it's KSE's list of completed threads. */
35899026Sjulian	if (!error) {
35999026Sjulian		error = td2_mbx = fuword(addr1);
36099026Sjulian		if (error == -1)
36199026Sjulian			error = EFAULT;
36299026Sjulian		else
36399026Sjulian			error = 0;
36499026Sjulian	}
36599026Sjulian	if (!error)
36699026Sjulian		error = suword(addr2, td2_mbx);
36799026Sjulian	if (!error)
36899026Sjulian		error = suword(addr1, (u_long)td->td_mailbox);
36999026Sjulian	if (error == -1)
37099026Sjulian		error = EFAULT;
371103838Sjulian	td->td_flags |= unbound;
37299026Sjulian	return (error);
37399026Sjulian}
37499026Sjulian
37599026Sjulian
37699026Sjulian/*
37799026Sjulian * Discard the current thread and exit from its context.
37899026Sjulian *
37999026Sjulian * Because we can't free a thread while we're operating under its context,
38099026Sjulian * push the current thread into our KSE's ke_tdspare slot, freeing the
38199026Sjulian * thread that might be there currently. Because we know that only this
38299026Sjulian * processor will run our KSE, we needn't worry about someone else grabbing
38399026Sjulian * our context before we do a cpu_throw.
38499026Sjulian */
38599026Sjulianvoid
38699026Sjulianthread_exit(void)
38799026Sjulian{
38899026Sjulian	struct thread *td;
38999026Sjulian	struct kse *ke;
39099026Sjulian	struct proc *p;
39199026Sjulian	struct ksegrp	*kg;
39299026Sjulian
39399026Sjulian	td = curthread;
39499026Sjulian	kg = td->td_ksegrp;
39599026Sjulian	p = td->td_proc;
39699026Sjulian	ke = td->td_kse;
39799026Sjulian
39899026Sjulian	mtx_assert(&sched_lock, MA_OWNED);
399102581Sjulian	KASSERT(p != NULL, ("thread exiting without a process"));
400102581Sjulian	KASSERT(ke != NULL, ("thread exiting without a kse"));
401102581Sjulian	KASSERT(kg != NULL, ("thread exiting without a kse group"));
40299026Sjulian	PROC_LOCK_ASSERT(p, MA_OWNED);
40399026Sjulian	CTR1(KTR_PROC, "thread_exit: thread %p", td);
40499026Sjulian	KASSERT(!mtx_owned(&Giant), ("dying thread owns giant"));
40599026Sjulian
40699026Sjulian	if (ke->ke_tdspare != NULL) {
407103216Sjulian		thread_stash(ke->ke_tdspare);
40899026Sjulian		ke->ke_tdspare = NULL;
40999026Sjulian	}
41099026Sjulian	cpu_thread_exit(td);	/* XXXSMP */
41199026Sjulian
412102581Sjulian	/*
413103002Sjulian	 * The last thread is left attached to the process
414103002Sjulian	 * So that the whole bundle gets recycled. Skip
415103002Sjulian	 * all this stuff.
416102581Sjulian	 */
417103002Sjulian	if (p->p_numthreads > 1) {
418103002Sjulian		/* Reassign this thread's KSE. */
419103002Sjulian		ke->ke_thread = NULL;
420103002Sjulian		td->td_kse = NULL;
421103002Sjulian		ke->ke_state = KES_UNQUEUED;
422103002Sjulian		kse_reassign(ke);
423103002Sjulian
424103002Sjulian		/* Unlink this thread from its proc. and the kseg */
425103002Sjulian		TAILQ_REMOVE(&p->p_threads, td, td_plist);
426103002Sjulian		p->p_numthreads--;
427103002Sjulian		TAILQ_REMOVE(&kg->kg_threads, td, td_kglist);
428103002Sjulian		kg->kg_numthreads--;
429103002Sjulian		/*
430103002Sjulian		 * The test below is NOT true if we are the
431103002Sjulian		 * sole exiting thread. P_STOPPED_SNGL is unset
432103002Sjulian		 * in exit1() after it is the only survivor.
433103002Sjulian		 */
434103002Sjulian		if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
435103002Sjulian			if (p->p_numthreads == p->p_suspcount) {
436103216Sjulian				thread_unsuspend_one(p->p_singlethread);
437103002Sjulian			}
43899026Sjulian		}
439103002Sjulian		PROC_UNLOCK(p);
440103216Sjulian		td->td_state	= TDS_INACTIVE;
441103002Sjulian		td->td_proc	= NULL;
442103002Sjulian		td->td_ksegrp	= NULL;
443103002Sjulian		td->td_last_kse	= NULL;
444103002Sjulian		ke->ke_tdspare = td;
445103002Sjulian	} else {
446103002Sjulian		PROC_UNLOCK(p);
44799026Sjulian	}
448103002Sjulian
44999026Sjulian	cpu_throw();
45099026Sjulian	/* NOTREACHED */
45199026Sjulian}
45299026Sjulian
45399026Sjulian/*
45499026Sjulian * Link a thread to a process.
455103002Sjulian * set up anything that needs to be initialized for it to
456103002Sjulian * be used by the process.
45799026Sjulian *
45899026Sjulian * Note that we do not link to the proc's ucred here.
45999026Sjulian * The thread is linked as if running but no KSE assigned.
46099026Sjulian */
46199026Sjulianvoid
46299026Sjulianthread_link(struct thread *td, struct ksegrp *kg)
46399026Sjulian{
46499026Sjulian	struct proc *p;
46599026Sjulian
46699026Sjulian	p = kg->kg_proc;
467103216Sjulian	td->td_state = TDS_INACTIVE;
46899026Sjulian	td->td_proc	= p;
46999026Sjulian	td->td_ksegrp	= kg;
47099026Sjulian	td->td_last_kse	= NULL;
47199026Sjulian
472103002Sjulian	LIST_INIT(&td->td_contested);
473103002Sjulian	callout_init(&td->td_slpcallout, 1);
47499026Sjulian	TAILQ_INSERT_HEAD(&p->p_threads, td, td_plist);
47599026Sjulian	TAILQ_INSERT_HEAD(&kg->kg_threads, td, td_kglist);
47699026Sjulian	p->p_numthreads++;
47799026Sjulian	kg->kg_numthreads++;
478103367Sjulian	if (oiks_debug && p->p_numthreads > max_threads_per_proc) {
47999026Sjulian		printf("OIKS %d\n", p->p_numthreads);
48099026Sjulian		if (oiks_debug > 1)
48199026Sjulian			Debugger("OIKS");
48299026Sjulian	}
48399026Sjulian	td->td_kse	= NULL;
48499026Sjulian}
48599026Sjulian
48699026Sjulian/*
487103410Smini * Create a thread and schedule it for upcall on the KSE given.
48899026Sjulian */
48999026Sjulianstruct thread *
49099026Sjulianthread_schedule_upcall(struct thread *td, struct kse *ke)
49199026Sjulian{
49299026Sjulian	struct thread *td2;
49399026Sjulian
49499026Sjulian	mtx_assert(&sched_lock, MA_OWNED);
49599026Sjulian	if (ke->ke_tdspare != NULL) {
49699026Sjulian		td2 = ke->ke_tdspare;
49799026Sjulian		ke->ke_tdspare = NULL;
49899026Sjulian	} else {
49999026Sjulian		mtx_unlock_spin(&sched_lock);
50099026Sjulian		td2 = thread_alloc();
50199026Sjulian		mtx_lock_spin(&sched_lock);
50299026Sjulian	}
50399026Sjulian	CTR3(KTR_PROC, "thread_schedule_upcall: thread %p (pid %d, %s)",
50499026Sjulian	     td, td->td_proc->p_pid, td->td_proc->p_comm);
505103072Sjulian	bzero(&td2->td_startzero,
506103002Sjulian	    (unsigned)RANGEOF(struct thread, td_startzero, td_endzero));
507103002Sjulian	bcopy(&td->td_startcopy, &td2->td_startcopy,
508103002Sjulian	    (unsigned) RANGEOF(struct thread, td_startcopy, td_endcopy));
50999026Sjulian	thread_link(td2, ke->ke_ksegrp);
510103410Smini	cpu_set_upcall(td2, td->td_pcb);
511103410Smini	bcopy(td->td_frame, td2->td_frame, sizeof(struct trapframe));
512103410Smini	/*
513103410Smini	 * The user context for this thread is selected when we choose
514103410Smini	 * a KSE and return to userland on it. All we need do here is
515103410Smini	 * note that the thread exists in order to perform an upcall.
516103410Smini	 *
517103410Smini	 * Since selecting a KSE to perform the upcall involves locking
518103410Smini	 * that KSE's context to our upcall, its best to wait until the
519103410Smini	 * last possible moment before grabbing a KSE. We do this in
520103410Smini	 * userret().
521103410Smini	 */
52299026Sjulian	td2->td_ucred = crhold(td->td_ucred);
52399026Sjulian	td2->td_flags = TDF_UNBOUND|TDF_UPCALLING;
524103216Sjulian	TD_SET_CAN_RUN(td2);
52599026Sjulian	setrunqueue(td2);
52699026Sjulian	return (td2);
52799026Sjulian}
52899026Sjulian
52999026Sjulian/*
530103410Smini * Schedule an upcall to notify a KSE process recieved signals.
53199026Sjulian *
532103410Smini * XXX - Modifying a sigset_t like this is totally bogus.
533103410Smini */
534103410Sministruct thread *
535103410Sminisignal_upcall(struct proc *p, int sig)
536103410Smini{
537103410Smini	struct thread *td, *td2;
538103410Smini	struct kse *ke;
539103410Smini	sigset_t ss;
540103410Smini	int error;
541103410Smini
542103410Smini	PROC_LOCK_ASSERT(p, MA_OWNED);
543103410Smini
544103410Smini	td = FIRST_THREAD_IN_PROC(p);
545103410Smini	ke = td->td_kse;
546103410Smini	PROC_UNLOCK(p);
547103410Smini	error = copyin(&ke->ke_mailbox->km_sigscaught, &ss, sizeof(sigset_t));
548103410Smini	PROC_LOCK(p);
549103410Smini	if (error)
550103410Smini		return (NULL);
551103410Smini	SIGADDSET(ss, sig);
552103410Smini	PROC_UNLOCK(p);
553103410Smini	error = copyout(&ss, &ke->ke_mailbox->km_sigscaught, sizeof(sigset_t));
554103410Smini	PROC_LOCK(p);
555103410Smini	if (error)
556103410Smini		return (NULL);
557103410Smini	mtx_lock_spin(&sched_lock);
558103410Smini	td2 = thread_schedule_upcall(td, ke);
559103410Smini	mtx_unlock_spin(&sched_lock);
560103410Smini	return (td2);
561103410Smini}
562103410Smini
563103410Smini/*
564103410Smini * Consider whether or not an upcall should be made, and update the
565103410Smini * TDF_UPCALLING flag appropriately.
566103410Smini *
567103410Smini * This function is called when the current thread had been bound to a user
568103410Smini * thread that performed a syscall that blocked, and is now returning.
569103410Smini * Got that? syscall -> msleep -> wakeup -> syscall_return -> us.
570103410Smini *
571103410Smini * This thread will be returned to the UTS in its mailbox as a completed
572103410Smini * thread.  We need to decide whether or not to perform an upcall now,
573103410Smini * or simply queue the thread for later.
574103410Smini *
575103410Smini * XXXKSE Future enhancement: We could also return back to
576103410Smini * the thread if we haven't had to do an upcall since then.
577103410Smini * If the KSE's copy is == the thread's copy, and there are
578103410Smini * no other completed threads.
579103410Smini */
580103410Sministatic int
581103838Sjulianthread_consider_upcalling(struct thread *td)
582103410Smini{
583103838Sjulian	struct proc *p;
584103838Sjulian	struct ksegrp *kg;
585103410Smini	int error;
586103410Smini
587103410Smini	/*
588103410Smini	 * Save the thread's context, and link it
589103410Smini	 * into the KSE's list of completed threads.
590103410Smini	 */
591103410Smini	error = thread_export_context(td);
592103410Smini	td->td_mailbox = NULL;
593103410Smini	if (error)
594103410Smini		/*
595103410Smini		 * Failing to do the KSE operation just defaults
596103410Smini		 * back to synchonous operation, so just return from
597103410Smini		 * the syscall.
598103410Smini		 */
599103410Smini		return (error);
600103410Smini
601103410Smini	/*
602103410Smini	 * Decide whether to perfom an upcall now.
603103410Smini	 */
604103410Smini	/* Make sure there are no other threads waiting to run. */
605103838Sjulian	p = td->td_proc;
606103838Sjulian	kg = td->td_ksegrp;
607103838Sjulian	PROC_LOCK(p);
608103838Sjulian	mtx_lock_spin(&sched_lock);
609103838Sjulian	/* bogus test, ok for testing though */
610103838Sjulian	if (TAILQ_FIRST(&kg->kg_runq) &&
611103838Sjulian	    (TAILQ_LAST(&kg->kg_runq, threadqueue)
612103838Sjulian		!= kg->kg_last_assigned)) {
613103410Smini		/*
614103410Smini		 * Another thread in this KSEG needs to run.
615103410Smini		 * Switch to it instead of performing an upcall,
616103410Smini		 * abondoning this thread.  Perform the upcall
617103410Smini		 * later; discard this thread for now.
618103410Smini		 *
619103410Smini		 * XXXKSE - As for the other threads to run;
620103410Smini		 * we COULD rush through all the threads
621103410Smini		 * in this KSEG at this priority, or we
622103410Smini		 * could throw the ball back into the court
623103410Smini		 * and just run the highest prio kse available.
624103410Smini		 * What is OUR priority?  The priority of the highest
625103410Smini		 * sycall waiting to be returned?
626103410Smini		 * For now, just let another KSE run (easiest).
627103410Smini		 *
628103410Smini		 * XXXKSE Future enhancement: Shove threads in this
629103410Smini		 * state onto a list of completed threads hanging
630103410Smini		 * off the KSEG. Then, collect them before performing
631103410Smini		 * an upcall. This way, we don't commit to an upcall
632103410Smini		 * on a particular KSE, but report completed threads on
633103410Smini		 * the next upcall to any KSE in this KSEG.
634103410Smini		 *
635103410Smini		 */
636103410Smini		thread_exit(); /* Abandon current thread. */
637103410Smini		/* NOTREACHED */
638103410Smini	} else
639103410Smini		/*
640103410Smini		 * Perform an upcall now.
641103410Smini		 *
642103410Smini		 * XXXKSE - Assumes we are going to userland, and not
643103410Smini		 * nested in the kernel.
644103410Smini		 */
645103410Smini		td->td_flags |= TDF_UPCALLING;
646103838Sjulian	mtx_unlock_spin(&sched_lock);
647103838Sjulian	PROC_UNLOCK(p);
648103410Smini	return (0);
649103410Smini}
650103410Smini
651103410Smini/*
652103410Smini * The extra work we go through if we are a threaded process when we
653103410Smini * return to userland.
654103410Smini *
65599026Sjulian * If we are a KSE process and returning to user mode, check for
65699026Sjulian * extra work to do before we return (e.g. for more syscalls
65799026Sjulian * to complete first).  If we were in a critical section, we should
65899026Sjulian * just return to let it finish. Same if we were in the UTS (in
659103410Smini * which case the mailbox's context's busy indicator will be set).
660103410Smini * The only traps we suport will have set the mailbox.
661103410Smini * We will clear it here.
66299026Sjulian */
66399026Sjulianint
664103838Sjulianthread_userret(struct thread *td, struct trapframe *frame)
66599026Sjulian{
666103410Smini	int error;
66799026Sjulian
668103838Sjulian#if 0
669103410Smini	/*
670103410Smini	 * Ensure that we have a spare thread available.
671103410Smini	 */
67299026Sjulian	if (ke->ke_tdspare == NULL) {
673103410Smini		mtx_lock(&Giant);
67499026Sjulian		ke->ke_tdspare = thread_alloc();
675103410Smini		mtx_unlock(&Giant);
67699026Sjulian	}
677103838Sjulian#endif
678103410Smini	/*
679103410Smini	 * Bound threads need no additional work.
680103410Smini	 */
681103410Smini	if ((td->td_flags & TDF_UNBOUND) == 0)
682103410Smini		return (0);
683103410Smini	error = 0;
684103410Smini	/*
685103410Smini	 * Decide whether or not we should perform an upcall now.
686103410Smini	 */
687103410Smini	if (((td->td_flags & TDF_UPCALLING) == 0) && td->td_mailbox) {
688103838Sjulian		error = thread_consider_upcalling(td);
689103410Smini		if (error != 0)
690103410Smini			/*
691103410Smini			 * Failing to do the KSE operation just defaults
692103410Smini			 * back to synchonous operation, so just return from
693103410Smini			 * the syscall.
694103410Smini			 */
695103410Smini			goto cont;
696103410Smini	}
697103410Smini	if (td->td_flags & TDF_UPCALLING) {
69899026Sjulian		/*
699103410Smini		 * There is no more work to do and we are going to ride
700103410Smini		 * this thead/KSE up to userland.
70199026Sjulian		 */
702103410Smini		CTR3(KTR_PROC, "userret: upcall thread %p (pid %d, %s)",
703103410Smini		    td, p->p_pid, p->p_comm);
70499026Sjulian
70599026Sjulian		/*
706103410Smini		 * Set user context to the UTS.
70799026Sjulian		 */
708103838Sjulian		td->td_flags &= ~TDF_UNBOUND;
709103838Sjulian		cpu_set_upcall_kse(td, td->td_kse);
710103410Smini		if (error)
71199026Sjulian			/*
712103410Smini			 * Failing to do the KSE operation just defaults
713103410Smini			 * back to synchonous operation, so just return from
714103410Smini			 * the syscall.
71599026Sjulian			 */
716103410Smini			goto cont;
71799026Sjulian
71899026Sjulian		/*
719103410Smini		 * Set state and mailbox.
72099026Sjulian		 */
721103410Smini		td->td_flags &= ~TDF_UPCALLING;
722103410Smini		error = suword((caddr_t)td->td_kse->ke_mailbox +
723103410Smini		    offsetof(struct kse_mailbox, km_curthread),
724103410Smini		    0);
725103410Smini	}
72699026Sjuliancont:
727103410Smini	/*
728103410Smini	 * Stop any chance that we may be separated from
729103410Smini	 * the KSE we are currently on. This is "biting the bullet",
730103410Smini	 * we are committing to go to user space as as this KSE here.
731103410Smini	 */
732103410Smini	td->td_flags &= ~TDF_UNBOUND;	/* Bind to this user thread. */
73399026Sjulian	return (error);
73499026Sjulian}
73599026Sjulian
73699026Sjulian/*
73799026Sjulian * Enforce single-threading.
73899026Sjulian *
73999026Sjulian * Returns 1 if the caller must abort (another thread is waiting to
74099026Sjulian * exit the process or similar). Process is locked!
74199026Sjulian * Returns 0 when you are successfully the only thread running.
74299026Sjulian * A process has successfully single threaded in the suspend mode when
74399026Sjulian * There are no threads in user mode. Threads in the kernel must be
74499026Sjulian * allowed to continue until they get to the user boundary. They may even
74599026Sjulian * copy out their return values and data before suspending. They may however be
74699026Sjulian * accellerated in reaching the user boundary as we will wake up
74799026Sjulian * any sleeping threads that are interruptable. (PCATCH).
74899026Sjulian */
74999026Sjulianint
75099026Sjulianthread_single(int force_exit)
75199026Sjulian{
75299026Sjulian	struct thread *td;
75399026Sjulian	struct thread *td2;
75499026Sjulian	struct proc *p;
75599026Sjulian
75699026Sjulian	td = curthread;
75799026Sjulian	p = td->td_proc;
75899026Sjulian	PROC_LOCK_ASSERT(p, MA_OWNED);
75999026Sjulian	KASSERT((td != NULL), ("curthread is NULL"));
76099026Sjulian
76199026Sjulian	if ((p->p_flag & P_KSES) == 0)
76299026Sjulian		return (0);
76399026Sjulian
764100648Sjulian	/* Is someone already single threading? */
765100648Sjulian	if (p->p_singlethread)
76699026Sjulian		return (1);
76799026Sjulian
768102950Sdavidxu	if (force_exit == SINGLE_EXIT)
76999026Sjulian		p->p_flag |= P_SINGLE_EXIT;
77099026Sjulian	else
77199026Sjulian		p->p_flag &= ~P_SINGLE_EXIT;
772102950Sdavidxu	p->p_flag |= P_STOPPED_SINGLE;
77399026Sjulian	p->p_singlethread = td;
77499026Sjulian	while ((p->p_numthreads - p->p_suspcount) != 1) {
775103216Sjulian		mtx_lock_spin(&sched_lock);
77699026Sjulian		FOREACH_THREAD_IN_PROC(p, td2) {
77799026Sjulian			if (td2 == td)
77899026Sjulian				continue;
779103216Sjulian			if (TD_IS_INHIBITED(td2)) {
780103216Sjulian				if (TD_IS_SUSPENDED(td2)) {
781103216Sjulian					if (force_exit == SINGLE_EXIT) {
782103216Sjulian						thread_unsuspend_one(td2);
783103216Sjulian					}
78499026Sjulian				}
785103216Sjulian				if ( TD_IS_SLEEPING(td2)) {
786103216Sjulian					if (td2->td_flags & TDF_CVWAITQ)
787103216Sjulian						cv_waitq_remove(td2);
788103216Sjulian					else
789103216Sjulian						unsleep(td2);
790103216Sjulian					break;
791103216Sjulian				}
792103216Sjulian				if (TD_CAN_RUN(td2))
793103216Sjulian					setrunqueue(td2);
79499026Sjulian			}
79599026Sjulian		}
79699026Sjulian		/*
79799026Sjulian		 * Wake us up when everyone else has suspended.
798100648Sjulian		 * In the mean time we suspend as well.
79999026Sjulian		 */
800103216Sjulian		thread_suspend_one(td);
80199026Sjulian		mtx_unlock(&Giant);
80299026Sjulian		PROC_UNLOCK(p);
80399026Sjulian		mi_switch();
80499026Sjulian		mtx_unlock_spin(&sched_lock);
80599026Sjulian		mtx_lock(&Giant);
80699026Sjulian		PROC_LOCK(p);
80799026Sjulian	}
80899026Sjulian	return (0);
80999026Sjulian}
81099026Sjulian
81199026Sjulian/*
81299026Sjulian * Called in from locations that can safely check to see
81399026Sjulian * whether we have to suspend or at least throttle for a
81499026Sjulian * single-thread event (e.g. fork).
81599026Sjulian *
81699026Sjulian * Such locations include userret().
81799026Sjulian * If the "return_instead" argument is non zero, the thread must be able to
81899026Sjulian * accept 0 (caller may continue), or 1 (caller must abort) as a result.
81999026Sjulian *
82099026Sjulian * The 'return_instead' argument tells the function if it may do a
82199026Sjulian * thread_exit() or suspend, or whether the caller must abort and back
82299026Sjulian * out instead.
82399026Sjulian *
82499026Sjulian * If the thread that set the single_threading request has set the
82599026Sjulian * P_SINGLE_EXIT bit in the process flags then this call will never return
82699026Sjulian * if 'return_instead' is false, but will exit.
82799026Sjulian *
82899026Sjulian * P_SINGLE_EXIT | return_instead == 0| return_instead != 0
82999026Sjulian *---------------+--------------------+---------------------
83099026Sjulian *       0       | returns 0          |   returns 0 or 1
83199026Sjulian *               | when ST ends       |   immediatly
83299026Sjulian *---------------+--------------------+---------------------
83399026Sjulian *       1       | thread exits       |   returns 1
83499026Sjulian *               |                    |  immediatly
83599026Sjulian * 0 = thread_exit() or suspension ok,
83699026Sjulian * other = return error instead of stopping the thread.
83799026Sjulian *
83899026Sjulian * While a full suspension is under effect, even a single threading
83999026Sjulian * thread would be suspended if it made this call (but it shouldn't).
84099026Sjulian * This call should only be made from places where
84199026Sjulian * thread_exit() would be safe as that may be the outcome unless
84299026Sjulian * return_instead is set.
84399026Sjulian */
84499026Sjulianint
84599026Sjulianthread_suspend_check(int return_instead)
84699026Sjulian{
84799026Sjulian	struct thread *td = curthread;
84899026Sjulian	struct proc *p = td->td_proc;
84999026Sjulian
85099026Sjulian	td = curthread;
85199026Sjulian	p = td->td_proc;
85299026Sjulian	PROC_LOCK_ASSERT(p, MA_OWNED);
85399026Sjulian	while (P_SHOULDSTOP(p)) {
854102950Sdavidxu		if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
85599026Sjulian			KASSERT(p->p_singlethread != NULL,
85699026Sjulian			    ("singlethread not set"));
85799026Sjulian			/*
858100648Sjulian			 * The only suspension in action is a
859100648Sjulian			 * single-threading. Single threader need not stop.
860100646Sjulian			 * XXX Should be safe to access unlocked
861100646Sjulian			 * as it can only be set to be true by us.
86299026Sjulian			 */
863100648Sjulian			if (p->p_singlethread == td)
86499026Sjulian				return (0);	/* Exempt from stopping. */
86599026Sjulian		}
866100648Sjulian		if (return_instead)
86799026Sjulian			return (1);
86899026Sjulian
86999026Sjulian		/*
87099026Sjulian		 * If the process is waiting for us to exit,
87199026Sjulian		 * this thread should just suicide.
872102950Sdavidxu		 * Assumes that P_SINGLE_EXIT implies P_STOPPED_SINGLE.
87399026Sjulian		 */
87499026Sjulian		if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) {
87599026Sjulian			mtx_lock_spin(&sched_lock);
87699026Sjulian			while (mtx_owned(&Giant))
87799026Sjulian				mtx_unlock(&Giant);
87899026Sjulian			thread_exit();
87999026Sjulian		}
88099026Sjulian
88199026Sjulian		/*
88299026Sjulian		 * When a thread suspends, it just
88399026Sjulian		 * moves to the processes's suspend queue
88499026Sjulian		 * and stays there.
88599026Sjulian		 *
88699026Sjulian		 * XXXKSE if TDF_BOUND is true
88799026Sjulian		 * it will not release it's KSE which might
88899026Sjulian		 * lead to deadlock if there are not enough KSEs
88999026Sjulian		 * to complete all waiting threads.
89099026Sjulian		 * Maybe be able to 'lend' it out again.
89199026Sjulian		 * (lent kse's can not go back to userland?)
89299026Sjulian		 * and can only be lent in STOPPED state.
89399026Sjulian		 */
894102238Sjulian		mtx_lock_spin(&sched_lock);
895102950Sdavidxu		if ((p->p_flag & P_STOPPED_SIG) &&
896102238Sjulian		    (p->p_suspcount+1 == p->p_numthreads)) {
897102238Sjulian			mtx_unlock_spin(&sched_lock);
898102238Sjulian			PROC_LOCK(p->p_pptr);
899102238Sjulian			if ((p->p_pptr->p_procsig->ps_flag &
900102238Sjulian				PS_NOCLDSTOP) == 0) {
901102238Sjulian				psignal(p->p_pptr, SIGCHLD);
902102238Sjulian			}
903102238Sjulian			PROC_UNLOCK(p->p_pptr);
904103055Sjulian			mtx_lock_spin(&sched_lock);
905102238Sjulian		}
90699026Sjulian		mtx_assert(&Giant, MA_NOTOWNED);
907103216Sjulian		thread_suspend_one(td);
90899026Sjulian		PROC_UNLOCK(p);
909102950Sdavidxu		if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
910100632Sjulian			if (p->p_numthreads == p->p_suspcount) {
911103216Sjulian				thread_unsuspend_one(p->p_singlethread);
912100632Sjulian			}
913100632Sjulian		}
914100594Sjulian		p->p_stats->p_ru.ru_nivcsw++;
91599026Sjulian		mi_switch();
91699026Sjulian		mtx_unlock_spin(&sched_lock);
91799026Sjulian		PROC_LOCK(p);
91899026Sjulian	}
91999026Sjulian	return (0);
92099026Sjulian}
92199026Sjulian
922102898Sdavidxuvoid
923102898Sdavidxuthread_suspend_one(struct thread *td)
924102898Sdavidxu{
925102898Sdavidxu	struct proc *p = td->td_proc;
926102898Sdavidxu
927102898Sdavidxu	mtx_assert(&sched_lock, MA_OWNED);
928102898Sdavidxu	p->p_suspcount++;
929103216Sjulian	TD_SET_SUSPENDED(td);
930102898Sdavidxu	TAILQ_INSERT_TAIL(&p->p_suspended, td, td_runq);
931103216Sjulian	/*
932103216Sjulian	 * Hack: If we are suspending but are on the sleep queue
933103216Sjulian	 * then we are in msleep or the cv equivalent. We
934103216Sjulian	 * want to look like we have two Inhibitors.
935103216Sjulian	 */
936103216Sjulian	if (TD_ON_SLEEPQ(td))
937103216Sjulian		TD_SET_SLEEPING(td);
938102898Sdavidxu}
939102898Sdavidxu
940102898Sdavidxuvoid
941102898Sdavidxuthread_unsuspend_one(struct thread *td)
942102898Sdavidxu{
943102898Sdavidxu	struct proc *p = td->td_proc;
944102898Sdavidxu
945102898Sdavidxu	mtx_assert(&sched_lock, MA_OWNED);
946102898Sdavidxu	TAILQ_REMOVE(&p->p_suspended, td, td_runq);
947103216Sjulian	TD_CLR_SUSPENDED(td);
948102898Sdavidxu	p->p_suspcount--;
949103216Sjulian	setrunnable(td);
950102898Sdavidxu}
951102898Sdavidxu
95299026Sjulian/*
95399026Sjulian * Allow all threads blocked by single threading to continue running.
95499026Sjulian */
95599026Sjulianvoid
95699026Sjulianthread_unsuspend(struct proc *p)
95799026Sjulian{
95899026Sjulian	struct thread *td;
95999026Sjulian
960100646Sjulian	mtx_assert(&sched_lock, MA_OWNED);
96199026Sjulian	PROC_LOCK_ASSERT(p, MA_OWNED);
96299026Sjulian	if (!P_SHOULDSTOP(p)) {
96399026Sjulian		while (( td = TAILQ_FIRST(&p->p_suspended))) {
964102898Sdavidxu			thread_unsuspend_one(td);
96599026Sjulian		}
966102950Sdavidxu	} else if ((P_SHOULDSTOP(p) == P_STOPPED_SINGLE) &&
96799026Sjulian	    (p->p_numthreads == p->p_suspcount)) {
96899026Sjulian		/*
96999026Sjulian		 * Stopping everything also did the job for the single
97099026Sjulian		 * threading request. Now we've downgraded to single-threaded,
97199026Sjulian		 * let it continue.
97299026Sjulian		 */
973102898Sdavidxu		thread_unsuspend_one(p->p_singlethread);
97499026Sjulian	}
97599026Sjulian}
97699026Sjulian
97799026Sjulianvoid
97899026Sjulianthread_single_end(void)
97999026Sjulian{
98099026Sjulian	struct thread *td;
98199026Sjulian	struct proc *p;
98299026Sjulian
98399026Sjulian	td = curthread;
98499026Sjulian	p = td->td_proc;
98599026Sjulian	PROC_LOCK_ASSERT(p, MA_OWNED);
986102950Sdavidxu	p->p_flag &= ~P_STOPPED_SINGLE;
98799026Sjulian	p->p_singlethread = NULL;
988102292Sjulian	/*
989102292Sjulian	 * If there are other threads they mey now run,
990102292Sjulian	 * unless of course there is a blanket 'stop order'
991102292Sjulian	 * on the process. The single threader must be allowed
992102292Sjulian	 * to continue however as this is a bad place to stop.
993102292Sjulian	 */
994102292Sjulian	if ((p->p_numthreads != 1) && (!P_SHOULDSTOP(p))) {
995102292Sjulian		mtx_lock_spin(&sched_lock);
996102292Sjulian		while (( td = TAILQ_FIRST(&p->p_suspended))) {
997103216Sjulian			thread_unsuspend_one(td);
998102292Sjulian		}
999102292Sjulian		mtx_unlock_spin(&sched_lock);
1000102292Sjulian	}
100199026Sjulian}
100299026Sjulian
1003102292Sjulian
1004