xref: /freebsd/sys/kern/kern_thread.c (revision 1279572a92e0f2d004dcca18343e11558ebcd9af)
144990b8cSJulian Elischer /*
244990b8cSJulian Elischer  * Copyright (C) 2001 Julian Elischer <julian@freebsd.org>.
344990b8cSJulian Elischer  *  All rights reserved.
444990b8cSJulian Elischer  *
544990b8cSJulian Elischer  * Redistribution and use in source and binary forms, with or without
644990b8cSJulian Elischer  * modification, are permitted provided that the following conditions
744990b8cSJulian Elischer  * are met:
844990b8cSJulian Elischer  * 1. Redistributions of source code must retain the above copyright
944990b8cSJulian Elischer  *    notice(s), this list of conditions and the following disclaimer as
1044990b8cSJulian Elischer  *    the first lines of this file unmodified other than the possible
1144990b8cSJulian Elischer  *    addition of one or more copyright notices.
1244990b8cSJulian Elischer  * 2. Redistributions in binary form must reproduce the above copyright
1344990b8cSJulian Elischer  *    notice(s), this list of conditions and the following disclaimer in the
1444990b8cSJulian Elischer  *    documentation and/or other materials provided with the distribution.
1544990b8cSJulian Elischer  *
1644990b8cSJulian Elischer  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
1744990b8cSJulian Elischer  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1844990b8cSJulian Elischer  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1944990b8cSJulian Elischer  * DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
2044990b8cSJulian Elischer  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2144990b8cSJulian Elischer  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2244990b8cSJulian Elischer  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
2344990b8cSJulian Elischer  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2444990b8cSJulian Elischer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2544990b8cSJulian Elischer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
2644990b8cSJulian Elischer  * DAMAGE.
2744990b8cSJulian Elischer  *
2844990b8cSJulian Elischer  * $FreeBSD$
2944990b8cSJulian Elischer  */
3044990b8cSJulian Elischer 
3144990b8cSJulian Elischer #include <sys/param.h>
3244990b8cSJulian Elischer #include <sys/systm.h>
3344990b8cSJulian Elischer #include <sys/kernel.h>
3444990b8cSJulian Elischer #include <sys/lock.h>
3544990b8cSJulian Elischer #include <sys/malloc.h>
3644990b8cSJulian Elischer #include <sys/mutex.h>
3744990b8cSJulian Elischer #include <sys/proc.h>
3844990b8cSJulian Elischer #include <sys/sysctl.h>
3944990b8cSJulian Elischer #include <sys/filedesc.h>
4044990b8cSJulian Elischer #include <sys/tty.h>
4144990b8cSJulian Elischer #include <sys/signalvar.h>
4244990b8cSJulian Elischer #include <sys/sx.h>
4344990b8cSJulian Elischer #include <sys/user.h>
4444990b8cSJulian Elischer #include <sys/jail.h>
4544990b8cSJulian Elischer #include <sys/kse.h>
4644990b8cSJulian Elischer #include <sys/ktr.h>
4744990b8cSJulian Elischer 
4844990b8cSJulian Elischer #include <vm/vm.h>
4944990b8cSJulian Elischer #include <vm/vm_object.h>
5044990b8cSJulian Elischer #include <vm/pmap.h>
5144990b8cSJulian Elischer #include <vm/uma.h>
5244990b8cSJulian Elischer #include <vm/vm_map.h>
5344990b8cSJulian Elischer 
5402fb42b0SPeter Wemm #include <machine/frame.h>
5502fb42b0SPeter Wemm 
5644990b8cSJulian Elischer /*
5744990b8cSJulian Elischer  * Thread related storage.
5844990b8cSJulian Elischer  */
5944990b8cSJulian Elischer static uma_zone_t thread_zone;
6044990b8cSJulian Elischer static int allocated_threads;
6144990b8cSJulian Elischer static int active_threads;
6244990b8cSJulian Elischer static int cached_threads;
6344990b8cSJulian Elischer 
6444990b8cSJulian Elischer SYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW, 0, "thread allocation");
6544990b8cSJulian Elischer 
6644990b8cSJulian Elischer SYSCTL_INT(_kern_threads, OID_AUTO, active, CTLFLAG_RD,
6744990b8cSJulian Elischer 	&active_threads, 0, "Number of active threads in system.");
6844990b8cSJulian Elischer 
6944990b8cSJulian Elischer SYSCTL_INT(_kern_threads, OID_AUTO, cached, CTLFLAG_RD,
7044990b8cSJulian Elischer 	&cached_threads, 0, "Number of threads in thread cache.");
7144990b8cSJulian Elischer 
7244990b8cSJulian Elischer SYSCTL_INT(_kern_threads, OID_AUTO, allocated, CTLFLAG_RD,
7344990b8cSJulian Elischer 	&allocated_threads, 0, "Number of threads in zone.");
7444990b8cSJulian Elischer 
7544990b8cSJulian Elischer static int oiks_debug = 1;	/* 0 disable, 1 printf, 2 enter debugger */
7644990b8cSJulian Elischer SYSCTL_INT(_kern_threads, OID_AUTO, oiks, CTLFLAG_RW,
7744990b8cSJulian Elischer 	&oiks_debug, 0, "OIKS thread debug");
7844990b8cSJulian Elischer 
7944990b8cSJulian Elischer #define RANGEOF(type, start, end) (offsetof(type, end) - offsetof(type, start))
8044990b8cSJulian Elischer 
8144990b8cSJulian Elischer struct threadqueue zombie_threads = TAILQ_HEAD_INITIALIZER(zombie_threads);
8244990b8cSJulian Elischer struct mtx zombie_thread_lock;
8344990b8cSJulian Elischer MTX_SYSINIT(zombie_thread_lock, &zombie_thread_lock,
8444990b8cSJulian Elischer     "zombie_thread_lock", MTX_SPIN);
8544990b8cSJulian Elischer 
8644990b8cSJulian Elischer /*
8744990b8cSJulian Elischer  * Pepare a thread for use.
8844990b8cSJulian Elischer  */
8944990b8cSJulian Elischer static void
9044990b8cSJulian Elischer thread_ctor(void *mem, int size, void *arg)
9144990b8cSJulian Elischer {
9244990b8cSJulian Elischer 	struct thread	*td;
9344990b8cSJulian Elischer 
9444990b8cSJulian Elischer 	KASSERT((size == sizeof(struct thread)),
95b799f5a4SPeter Wemm 	    ("size mismatch: %d != %d\n", size, (int)sizeof(struct thread)));
9644990b8cSJulian Elischer 
9744990b8cSJulian Elischer 	td = (struct thread *)mem;
9844990b8cSJulian Elischer 	bzero(&td->td_startzero,
9944990b8cSJulian Elischer 	    (unsigned)RANGEOF(struct thread, td_startzero, td_endzero));
10044990b8cSJulian Elischer 	td->td_state = TDS_NEW;
10144990b8cSJulian Elischer 	td->td_flags |= TDF_UNBOUND;
102a136efe9SPeter Wemm #if 0
103a136efe9SPeter Wemm 	/*
104a136efe9SPeter Wemm 	 * Maybe move these here from process creation, but maybe not.
105a136efe9SPeter Wemm 	 * Moving them here takes them away from their "natural" place
106a136efe9SPeter Wemm 	 * in the fork process.
107a136efe9SPeter Wemm 	 */
108a136efe9SPeter Wemm 	/* XXX td_contested does not appear to be initialized for threads! */
109a136efe9SPeter Wemm 	LIST_INIT(&td->td_contested);
110a136efe9SPeter Wemm 	callout_init(&td->td_slpcallout, 1);
111a136efe9SPeter Wemm #endif
11244990b8cSJulian Elischer 	cached_threads--;	/* XXXSMP */
11344990b8cSJulian Elischer 	active_threads++;	/* XXXSMP */
11444990b8cSJulian Elischer }
11544990b8cSJulian Elischer 
11644990b8cSJulian Elischer /*
11744990b8cSJulian Elischer  * Reclaim a thread after use.
11844990b8cSJulian Elischer  */
11944990b8cSJulian Elischer static void
12044990b8cSJulian Elischer thread_dtor(void *mem, int size, void *arg)
12144990b8cSJulian Elischer {
12244990b8cSJulian Elischer 	struct thread	*td;
12344990b8cSJulian Elischer 
12444990b8cSJulian Elischer 	KASSERT((size == sizeof(struct thread)),
125b799f5a4SPeter Wemm 	    ("size mismatch: %d != %d\n", size, (int)sizeof(struct thread)));
12644990b8cSJulian Elischer 
12744990b8cSJulian Elischer 	td = (struct thread *)mem;
12844990b8cSJulian Elischer 
12944990b8cSJulian Elischer #ifdef INVARIANTS
13044990b8cSJulian Elischer 	/* Verify that this thread is in a safe state to free. */
13144990b8cSJulian Elischer 	switch (td->td_state) {
13244990b8cSJulian Elischer 	case TDS_SLP:
13344990b8cSJulian Elischer 	case TDS_MTX:
13444990b8cSJulian Elischer 	case TDS_RUNQ:
13544990b8cSJulian Elischer 		/*
13644990b8cSJulian Elischer 		 * We must never unlink a thread that is in one of
13744990b8cSJulian Elischer 		 * these states, because it is currently active.
13844990b8cSJulian Elischer 		 */
13944990b8cSJulian Elischer 		panic("bad state for thread unlinking");
14044990b8cSJulian Elischer 		/* NOTREACHED */
14144990b8cSJulian Elischer 	case TDS_UNQUEUED:
14244990b8cSJulian Elischer 	case TDS_NEW:
14344990b8cSJulian Elischer 	case TDS_RUNNING:
14444990b8cSJulian Elischer 	case TDS_SURPLUS:
14544990b8cSJulian Elischer 		break;
14644990b8cSJulian Elischer 	default:
14744990b8cSJulian Elischer 		panic("bad thread state");
14844990b8cSJulian Elischer 		/* NOTREACHED */
14944990b8cSJulian Elischer 	}
15044990b8cSJulian Elischer #endif
15144990b8cSJulian Elischer 
15244990b8cSJulian Elischer 	/* Update counters. */
15344990b8cSJulian Elischer 	active_threads--;	/* XXXSMP */
15444990b8cSJulian Elischer 	cached_threads++;	/* XXXSMP */
15544990b8cSJulian Elischer }
15644990b8cSJulian Elischer 
15744990b8cSJulian Elischer /*
15844990b8cSJulian Elischer  * Initialize type-stable parts of a thread (when newly created).
15944990b8cSJulian Elischer  */
16044990b8cSJulian Elischer static void
16144990b8cSJulian Elischer thread_init(void *mem, int size)
16244990b8cSJulian Elischer {
16344990b8cSJulian Elischer 	struct thread	*td;
16444990b8cSJulian Elischer 
16544990b8cSJulian Elischer 	KASSERT((size == sizeof(struct thread)),
166b799f5a4SPeter Wemm 	    ("size mismatch: %d != %d\n", size, (int)sizeof(struct thread)));
16744990b8cSJulian Elischer 
16844990b8cSJulian Elischer 	td = (struct thread *)mem;
16944990b8cSJulian Elischer 	pmap_new_thread(td);
17044990b8cSJulian Elischer 	cpu_thread_setup(td);
17144990b8cSJulian Elischer 	cached_threads++;	/* XXXSMP */
17244990b8cSJulian Elischer 	allocated_threads++;	/* XXXSMP */
17344990b8cSJulian Elischer }
17444990b8cSJulian Elischer 
17544990b8cSJulian Elischer /*
17644990b8cSJulian Elischer  * Tear down type-stable parts of a thread (just before being discarded).
17744990b8cSJulian Elischer  */
17844990b8cSJulian Elischer static void
17944990b8cSJulian Elischer thread_fini(void *mem, int size)
18044990b8cSJulian Elischer {
18144990b8cSJulian Elischer 	struct thread	*td;
18244990b8cSJulian Elischer 
18344990b8cSJulian Elischer 	KASSERT((size == sizeof(struct thread)),
184b799f5a4SPeter Wemm 	    ("size mismatch: %d != %d\n", size, (int)sizeof(struct thread)));
18544990b8cSJulian Elischer 
18644990b8cSJulian Elischer 	td = (struct thread *)mem;
18744990b8cSJulian Elischer 	pmap_dispose_thread(td);
18844990b8cSJulian Elischer 	cached_threads--;	/* XXXSMP */
18944990b8cSJulian Elischer 	allocated_threads--;	/* XXXSMP */
19044990b8cSJulian Elischer }
19144990b8cSJulian Elischer 
19244990b8cSJulian Elischer /*
19344990b8cSJulian Elischer  * Initialize global thread allocation resources.
19444990b8cSJulian Elischer  */
19544990b8cSJulian Elischer void
19644990b8cSJulian Elischer threadinit(void)
19744990b8cSJulian Elischer {
19844990b8cSJulian Elischer 
19944990b8cSJulian Elischer 	thread_zone = uma_zcreate("THREAD", sizeof (struct thread),
20044990b8cSJulian Elischer 	    thread_ctor, thread_dtor, thread_init, thread_fini,
20144990b8cSJulian Elischer 	    UMA_ALIGN_CACHE, 0);
20244990b8cSJulian Elischer }
20344990b8cSJulian Elischer 
20444990b8cSJulian Elischer /*
20544990b8cSJulian Elischer  * Stash an embarasingly esxtra thread into the zombie thread queue.
20644990b8cSJulian Elischer  */
20744990b8cSJulian Elischer void
20844990b8cSJulian Elischer thread_stash(struct thread *td)
20944990b8cSJulian Elischer {
21044990b8cSJulian Elischer 	mtx_lock_spin(&zombie_thread_lock);
21144990b8cSJulian Elischer 	TAILQ_INSERT_HEAD(&zombie_threads, td, td_runq);
21244990b8cSJulian Elischer 	mtx_unlock_spin(&zombie_thread_lock);
21344990b8cSJulian Elischer }
21444990b8cSJulian Elischer 
21544990b8cSJulian Elischer /*
21667759b33SJulian Elischer  * reap any  zombie threads.
21744990b8cSJulian Elischer  */
21844990b8cSJulian Elischer void
21944990b8cSJulian Elischer thread_reap(void)
22044990b8cSJulian Elischer {
22144990b8cSJulian Elischer 	struct thread *td_reaped;
22244990b8cSJulian Elischer 
22344990b8cSJulian Elischer 	/*
22444990b8cSJulian Elischer 	 * don't even bother to lock if none at this instant
22544990b8cSJulian Elischer 	 * We really don't care about the next instant..
22644990b8cSJulian Elischer 	 */
22744990b8cSJulian Elischer 	if (!TAILQ_EMPTY(&zombie_threads)) {
22844990b8cSJulian Elischer 		mtx_lock_spin(&zombie_thread_lock);
22944990b8cSJulian Elischer 		while (!TAILQ_EMPTY(&zombie_threads)) {
23044990b8cSJulian Elischer 			td_reaped = TAILQ_FIRST(&zombie_threads);
23144990b8cSJulian Elischer 			TAILQ_REMOVE(&zombie_threads, td_reaped, td_runq);
23244990b8cSJulian Elischer 			mtx_unlock_spin(&zombie_thread_lock);
23344990b8cSJulian Elischer 			thread_free(td_reaped);
23444990b8cSJulian Elischer 			mtx_lock_spin(&zombie_thread_lock);
23544990b8cSJulian Elischer 		}
23644990b8cSJulian Elischer 		mtx_unlock_spin(&zombie_thread_lock);
23744990b8cSJulian Elischer 	}
23844990b8cSJulian Elischer }
23944990b8cSJulian Elischer 
24044990b8cSJulian Elischer /*
24144990b8cSJulian Elischer  * Allocate a thread.
24244990b8cSJulian Elischer  */
24344990b8cSJulian Elischer struct thread *
24444990b8cSJulian Elischer thread_alloc(void)
24544990b8cSJulian Elischer {
24644990b8cSJulian Elischer 	thread_reap(); /* check if any zombies to get */
24744990b8cSJulian Elischer 	return (uma_zalloc(thread_zone, M_WAITOK));
24844990b8cSJulian Elischer }
24944990b8cSJulian Elischer 
25044990b8cSJulian Elischer /*
25144990b8cSJulian Elischer  * Deallocate a thread.
25244990b8cSJulian Elischer  */
25344990b8cSJulian Elischer void
25444990b8cSJulian Elischer thread_free(struct thread *td)
25544990b8cSJulian Elischer {
25644990b8cSJulian Elischer 	uma_zfree(thread_zone, td);
25744990b8cSJulian Elischer }
25844990b8cSJulian Elischer 
25944990b8cSJulian Elischer /*
26044990b8cSJulian Elischer  * Store the thread context in the UTS's mailbox.
26144990b8cSJulian Elischer  */
26244990b8cSJulian Elischer int
26344990b8cSJulian Elischer thread_export_context(struct thread *td)
26444990b8cSJulian Elischer {
26544990b8cSJulian Elischer 	struct kse *ke;
26644990b8cSJulian Elischer 	uintptr_t td2_mbx;
26744990b8cSJulian Elischer 	void *addr1;
26844990b8cSJulian Elischer 	void *addr2;
26944990b8cSJulian Elischer 	int error;
27044990b8cSJulian Elischer 
2718a2bd345SPeter Wemm #ifdef __ia64__
2728a2bd345SPeter Wemm 	td2_mbx = 0;		/* pacify gcc (!) */
2738a2bd345SPeter Wemm #endif
27444990b8cSJulian Elischer 	/* Export the register contents. */
27544990b8cSJulian Elischer 	error = cpu_export_context(td);
27644990b8cSJulian Elischer 
27744990b8cSJulian Elischer 	ke = td->td_kse;
27844990b8cSJulian Elischer 	addr1 = (caddr_t)ke->ke_mailbox
27944990b8cSJulian Elischer 			+ offsetof(struct kse_mailbox, kmbx_completed_threads);
28044990b8cSJulian Elischer 	addr2 = (caddr_t)td->td_mailbox
28144990b8cSJulian Elischer 			+ offsetof(struct thread_mailbox , next_completed);
28244990b8cSJulian Elischer 	/* Then link it into it's KSE's list of completed threads. */
28344990b8cSJulian Elischer 	if (!error) {
28444990b8cSJulian Elischer 		error = td2_mbx = fuword(addr1);
28544990b8cSJulian Elischer 		if (error == -1)
28644990b8cSJulian Elischer 			error = EFAULT;
28744990b8cSJulian Elischer 		else
28844990b8cSJulian Elischer 			error = 0;
28944990b8cSJulian Elischer 	}
29044990b8cSJulian Elischer 	if (!error)
29144990b8cSJulian Elischer 		error = suword(addr2, td2_mbx);
29244990b8cSJulian Elischer 	if (!error)
29344990b8cSJulian Elischer 		error = suword(addr1, (u_long)td->td_mailbox);
29444990b8cSJulian Elischer 	if (error == -1)
29544990b8cSJulian Elischer 		error = EFAULT;
29644990b8cSJulian Elischer 	return (error);
29744990b8cSJulian Elischer }
29844990b8cSJulian Elischer 
29944990b8cSJulian Elischer 
30044990b8cSJulian Elischer /*
30144990b8cSJulian Elischer  * Discard the current thread and exit from its context.
30244990b8cSJulian Elischer  *
30344990b8cSJulian Elischer  * Because we can't free a thread while we're operating under its context,
30444990b8cSJulian Elischer  * push the current thread into our KSE's ke_tdspare slot, freeing the
30544990b8cSJulian Elischer  * thread that might be there currently. Because we know that only this
30644990b8cSJulian Elischer  * processor will run our KSE, we needn't worry about someone else grabbing
30744990b8cSJulian Elischer  * our context before we do a cpu_throw.
30844990b8cSJulian Elischer  */
30944990b8cSJulian Elischer void
31044990b8cSJulian Elischer thread_exit(void)
31144990b8cSJulian Elischer {
31244990b8cSJulian Elischer 	struct thread *td;
31344990b8cSJulian Elischer 	struct kse *ke;
31444990b8cSJulian Elischer 	struct proc *p;
31544990b8cSJulian Elischer 	struct ksegrp	*kg;
31644990b8cSJulian Elischer 
31744990b8cSJulian Elischer 	td = curthread;
31844990b8cSJulian Elischer 	kg = td->td_ksegrp;
31944990b8cSJulian Elischer 	p = td->td_proc;
32044990b8cSJulian Elischer 	ke = td->td_kse;
32144990b8cSJulian Elischer 
32244990b8cSJulian Elischer 	mtx_assert(&sched_lock, MA_OWNED);
32388151aa3SJulian Elischer 	KASSERT(p != NULL, ("thread exiting without a process"));
32488151aa3SJulian Elischer 	KASSERT(ke != NULL, ("thread exiting without a kse"));
32588151aa3SJulian Elischer 	KASSERT(kg != NULL, ("thread exiting without a kse group"));
32644990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
32744990b8cSJulian Elischer 	CTR1(KTR_PROC, "thread_exit: thread %p", td);
32844990b8cSJulian Elischer 	KASSERT(!mtx_owned(&Giant), ("dying thread owns giant"));
32944990b8cSJulian Elischer 
33044990b8cSJulian Elischer 	if (ke->ke_tdspare != NULL) {
33144990b8cSJulian Elischer 		thread_stash(ke->ke_tdspare);
33244990b8cSJulian Elischer 		ke->ke_tdspare = NULL;
33344990b8cSJulian Elischer 	}
33444990b8cSJulian Elischer 	cpu_thread_exit(td);	/* XXXSMP */
33544990b8cSJulian Elischer 
33644990b8cSJulian Elischer 	/* Reassign this thread's KSE. */
33744990b8cSJulian Elischer 	ke->ke_thread = NULL;
33844990b8cSJulian Elischer 	td->td_kse = NULL;
33944990b8cSJulian Elischer 	ke->ke_state = KES_UNQUEUED;
34044990b8cSJulian Elischer 	kse_reassign(ke);
34144990b8cSJulian Elischer 
34244990b8cSJulian Elischer 	/* Unlink this thread from its proc. and the kseg */
34344990b8cSJulian Elischer 	TAILQ_REMOVE(&p->p_threads, td, td_plist);
34444990b8cSJulian Elischer 	p->p_numthreads--;
34544990b8cSJulian Elischer 	TAILQ_REMOVE(&kg->kg_threads, td, td_kglist);
34644990b8cSJulian Elischer 	kg->kg_numthreads--;
34744990b8cSJulian Elischer 	/*
34844990b8cSJulian Elischer 	 * The test below is NOT true if we are the
3491279572aSDavid Xu 	 * sole exiting thread. P_STOPPED_SINGLE is unset
35044990b8cSJulian Elischer 	 * in exit1() after it is the only survivor.
35144990b8cSJulian Elischer 	 */
3521279572aSDavid Xu 	if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
35344990b8cSJulian Elischer 		if (p->p_numthreads == p->p_suspcount) {
35444990b8cSJulian Elischer 			TAILQ_REMOVE(&p->p_suspended,
35544990b8cSJulian Elischer 			    p->p_singlethread, td_runq);
35644990b8cSJulian Elischer 			setrunqueue(p->p_singlethread);
35744990b8cSJulian Elischer 			p->p_suspcount--;
35844990b8cSJulian Elischer 		}
35944990b8cSJulian Elischer 	}
36088151aa3SJulian Elischer 	PROC_UNLOCK(p);
36144990b8cSJulian Elischer 	td->td_state	= TDS_SURPLUS;
36244990b8cSJulian Elischer 	td->td_proc	= NULL;
36344990b8cSJulian Elischer 	td->td_ksegrp	= NULL;
36444990b8cSJulian Elischer 	td->td_last_kse	= NULL;
36544990b8cSJulian Elischer 	ke->ke_tdspare = td;
36644990b8cSJulian Elischer 	cpu_throw();
36744990b8cSJulian Elischer 	/* NOTREACHED */
36844990b8cSJulian Elischer }
36944990b8cSJulian Elischer 
37044990b8cSJulian Elischer /*
37144990b8cSJulian Elischer  * Link a thread to a process.
37244990b8cSJulian Elischer  *
37344990b8cSJulian Elischer  * Note that we do not link to the proc's ucred here.
37444990b8cSJulian Elischer  * The thread is linked as if running but no KSE assigned.
37544990b8cSJulian Elischer  */
37644990b8cSJulian Elischer void
37744990b8cSJulian Elischer thread_link(struct thread *td, struct ksegrp *kg)
37844990b8cSJulian Elischer {
37944990b8cSJulian Elischer 	struct proc *p;
38044990b8cSJulian Elischer 
38144990b8cSJulian Elischer 	p = kg->kg_proc;
38244990b8cSJulian Elischer 	td->td_state = TDS_NEW;
38344990b8cSJulian Elischer 	td->td_proc	= p;
38444990b8cSJulian Elischer 	td->td_ksegrp	= kg;
38544990b8cSJulian Elischer 	td->td_last_kse	= NULL;
38644990b8cSJulian Elischer 
38744990b8cSJulian Elischer 	TAILQ_INSERT_HEAD(&p->p_threads, td, td_plist);
38844990b8cSJulian Elischer 	TAILQ_INSERT_HEAD(&kg->kg_threads, td, td_kglist);
38944990b8cSJulian Elischer 	p->p_numthreads++;
39044990b8cSJulian Elischer 	kg->kg_numthreads++;
39144990b8cSJulian Elischer 	if (oiks_debug && p->p_numthreads > 4) {
39244990b8cSJulian Elischer 		printf("OIKS %d\n", p->p_numthreads);
39344990b8cSJulian Elischer 		if (oiks_debug > 1)
39444990b8cSJulian Elischer 			Debugger("OIKS");
39544990b8cSJulian Elischer 	}
39644990b8cSJulian Elischer 	td->td_critnest = 0;
39744990b8cSJulian Elischer 	td->td_kse	= NULL;
39844990b8cSJulian Elischer }
39944990b8cSJulian Elischer 
40044990b8cSJulian Elischer /*
40144990b8cSJulian Elischer  * Set up the upcall pcb in either a given thread or a new one
40244990b8cSJulian Elischer  * if none given. Use the upcall for the given KSE
40344990b8cSJulian Elischer  * XXXKSE possibly fix cpu_set_upcall() to not need td->td_kse set.
40444990b8cSJulian Elischer  */
40544990b8cSJulian Elischer struct thread *
40644990b8cSJulian Elischer thread_schedule_upcall(struct thread *td, struct kse *ke)
40744990b8cSJulian Elischer {
40844990b8cSJulian Elischer 	struct thread *td2;
40944990b8cSJulian Elischer 
41044990b8cSJulian Elischer 	mtx_assert(&sched_lock, MA_OWNED);
41144990b8cSJulian Elischer 	if (ke->ke_tdspare != NULL) {
41244990b8cSJulian Elischer 		td2 = ke->ke_tdspare;
41344990b8cSJulian Elischer 		ke->ke_tdspare = NULL;
41444990b8cSJulian Elischer 	} else {
41544990b8cSJulian Elischer 		mtx_unlock_spin(&sched_lock);
41644990b8cSJulian Elischer 		td2 = thread_alloc();
41744990b8cSJulian Elischer 		mtx_lock_spin(&sched_lock);
41844990b8cSJulian Elischer 	}
41944990b8cSJulian Elischer 	CTR3(KTR_PROC, "thread_schedule_upcall: thread %p (pid %d, %s)",
42044990b8cSJulian Elischer 	     td, td->td_proc->p_pid, td->td_proc->p_comm);
42144990b8cSJulian Elischer 	thread_link(td2, ke->ke_ksegrp);
42244990b8cSJulian Elischer 	cpu_set_upcall(td2, ke->ke_pcb);
42344990b8cSJulian Elischer 	td2->td_ucred = crhold(td->td_ucred);
42444990b8cSJulian Elischer 	td2->td_flags = TDF_UNBOUND|TDF_UPCALLING;
42544990b8cSJulian Elischer 	td2->td_priority = td->td_priority;
42644990b8cSJulian Elischer 	setrunqueue(td2);
42744990b8cSJulian Elischer 	return (td2);
42844990b8cSJulian Elischer }
42944990b8cSJulian Elischer 
43044990b8cSJulian Elischer /*
43144990b8cSJulian Elischer  * The extra work we go through if we are a threaded process when we
43244990b8cSJulian Elischer  * return to userland
43344990b8cSJulian Elischer  *
43444990b8cSJulian Elischer  * If we are a KSE process and returning to user mode, check for
43544990b8cSJulian Elischer  * extra work to do before we return (e.g. for more syscalls
43644990b8cSJulian Elischer  * to complete first).  If we were in a critical section, we should
43744990b8cSJulian Elischer  * just return to let it finish. Same if we were in the UTS (in
43844990b8cSJulian Elischer  * which case we will have no thread mailbox registered).  The only
43944990b8cSJulian Elischer  * traps we suport will have set the mailbox.  We will clear it here.
44044990b8cSJulian Elischer  */
44144990b8cSJulian Elischer int
44244990b8cSJulian Elischer thread_userret(struct proc *p, struct ksegrp *kg, struct kse *ke,
44344990b8cSJulian Elischer     struct thread *td, struct trapframe *frame)
44444990b8cSJulian Elischer {
44544990b8cSJulian Elischer 	int error = 0;
44644990b8cSJulian Elischer 
44744990b8cSJulian Elischer 	if (ke->ke_tdspare == NULL) {
44844990b8cSJulian Elischer 		ke->ke_tdspare = thread_alloc();
44944990b8cSJulian Elischer 	}
45044990b8cSJulian Elischer 	if (td->td_flags & TDF_UNBOUND) {
45144990b8cSJulian Elischer 		/*
45244990b8cSJulian Elischer 		 * Are we returning from a thread that had a mailbox?
45344990b8cSJulian Elischer 		 *
45444990b8cSJulian Elischer 		 * XXX Maybe this should be in a separate function.
45544990b8cSJulian Elischer 		 */
45644990b8cSJulian Elischer 		if (((td->td_flags & TDF_UPCALLING) == 0) && td->td_mailbox) {
45744990b8cSJulian Elischer 			/*
45844990b8cSJulian Elischer 			 * [XXXKSE Future enhancement]
45944990b8cSJulian Elischer 			 * We could also go straight back to the syscall
46044990b8cSJulian Elischer 			 * if we never had to do an upcall since then.
46144990b8cSJulian Elischer 			 * If the KSE's copy is == the thread's copy..
46244990b8cSJulian Elischer 			 * AND there are no other completed threads.
46344990b8cSJulian Elischer 			 */
46444990b8cSJulian Elischer 			/*
46544990b8cSJulian Elischer 			 * We will go back as an upcall or go do another thread.
46644990b8cSJulian Elischer 			 * Either way we need to save the context back to
46744990b8cSJulian Elischer 			 * the user thread mailbox.
46844990b8cSJulian Elischer 			 * So the UTS can restart it later.
46944990b8cSJulian Elischer 			 */
47044990b8cSJulian Elischer 			error = thread_export_context(td);
47144990b8cSJulian Elischer 			td->td_mailbox = NULL;
47244990b8cSJulian Elischer 			if (error) {
47344990b8cSJulian Elischer 				/*
47444990b8cSJulian Elischer 				 * Failing to do the KSE
47544990b8cSJulian Elischer 				 * operation just defaults operation
47644990b8cSJulian Elischer 				 * back to synchonous operation.
47744990b8cSJulian Elischer 				 */
47844990b8cSJulian Elischer 				goto cont;
47944990b8cSJulian Elischer 			}
48044990b8cSJulian Elischer 
48144990b8cSJulian Elischer 			if (TAILQ_FIRST(&kg->kg_runq)) {
48244990b8cSJulian Elischer 				/*
48344990b8cSJulian Elischer 				 * Uh-oh.. don't return to the user.
48444990b8cSJulian Elischer 				 * Instead, switch to the thread that
48544990b8cSJulian Elischer 				 * needs to run. The question is:
48644990b8cSJulian Elischer 				 * What do we do with the thread we have now?
48744990b8cSJulian Elischer 				 * We have put the completion block
48844990b8cSJulian Elischer 				 * on the kse mailbox. If we had more energy,
48944990b8cSJulian Elischer 				 * we could lazily do so, assuming someone
49044990b8cSJulian Elischer 				 * else might get to userland earlier
49144990b8cSJulian Elischer 				 * and deliver it earlier than we could.
49244990b8cSJulian Elischer 				 * To do that we could save it off the KSEG.
49344990b8cSJulian Elischer 				 * An upcalling KSE would 'reap' all completed
49444990b8cSJulian Elischer 				 * threads.
49544990b8cSJulian Elischer 				 * Being in a hurry, we'll do nothing and
49644990b8cSJulian Elischer 				 * leave it on the current KSE for now.
49744990b8cSJulian Elischer 				 *
49844990b8cSJulian Elischer 				 * As for the other threads to run;
49944990b8cSJulian Elischer 				 * we COULD rush through all the threads
50044990b8cSJulian Elischer 				 * in this KSEG at this priority, or we
50144990b8cSJulian Elischer 				 * could throw the ball back into the court
50244990b8cSJulian Elischer 				 * and just run the highest prio kse available.
50344990b8cSJulian Elischer 				 * What is OUR priority?
50444990b8cSJulian Elischer 				 * the priority of the highest sycall waiting
50544990b8cSJulian Elischer 				 * to be returned?
50644990b8cSJulian Elischer 				 * For now, just let another KSE run (easiest).
50744990b8cSJulian Elischer 				 */
50844990b8cSJulian Elischer 				PROC_LOCK(p);
50944990b8cSJulian Elischer 				mtx_lock_spin(&sched_lock);
51044990b8cSJulian Elischer 				thread_exit(); /* Abandon current thread. */
51144990b8cSJulian Elischer 				/* NOTREACHED */
51244990b8cSJulian Elischer 			} else { /* if (number of returning syscalls = 1) */
51344990b8cSJulian Elischer 				/*
51444990b8cSJulian Elischer 				 * Swap our frame for the upcall frame.
51544990b8cSJulian Elischer 				 *
51644990b8cSJulian Elischer 				 * XXXKSE Assumes we are going to user land
51744990b8cSJulian Elischer 				 * and not nested in the kernel
51844990b8cSJulian Elischer 				 */
51944990b8cSJulian Elischer 				td->td_flags |= TDF_UPCALLING;
52044990b8cSJulian Elischer 			}
52144990b8cSJulian Elischer 		}
52244990b8cSJulian Elischer 		/*
52344990b8cSJulian Elischer 		 * This is NOT just an 'else' clause for the above test...
52444990b8cSJulian Elischer 		 */
52544990b8cSJulian Elischer 		if (td->td_flags & TDF_UPCALLING) {
52644990b8cSJulian Elischer 			CTR3(KTR_PROC, "userret: upcall thread %p (pid %d, %s)",
52744990b8cSJulian Elischer 			    td, p->p_pid, p->p_comm);
52844990b8cSJulian Elischer 			/*
52944990b8cSJulian Elischer 			 * Make sure that it has the correct frame loaded.
53044990b8cSJulian Elischer 			 * While we know that we are on the same KSEGRP
53144990b8cSJulian Elischer 			 * as we were created on, we could very easily
53244990b8cSJulian Elischer 			 * have come in on another KSE. We therefore need
53344990b8cSJulian Elischer 			 * to do the copy of the frame after the last
53444990b8cSJulian Elischer 			 * possible switch() (the one above).
53544990b8cSJulian Elischer 			 */
53644990b8cSJulian Elischer 			bcopy(ke->ke_frame, frame, sizeof(struct trapframe));
53744990b8cSJulian Elischer 
53844990b8cSJulian Elischer 			/*
53944990b8cSJulian Elischer 			 * Decide what we are sending to the user
54044990b8cSJulian Elischer 			 * upcall sets one argument. The address of the mbox.
54144990b8cSJulian Elischer 			 */
54244990b8cSJulian Elischer 			cpu_set_args(td, ke);
54344990b8cSJulian Elischer 
54444990b8cSJulian Elischer 			/*
54544990b8cSJulian Elischer 			 * There is no more work to do and we are going to ride
54644990b8cSJulian Elischer 			 * this thead/KSE up to userland. Make sure the user's
54744990b8cSJulian Elischer 			 * pointer to the thread mailbox is cleared before we
54844990b8cSJulian Elischer 			 * re-enter the kernel next time for any reason..
54944990b8cSJulian Elischer 			 * We might as well do it here.
55044990b8cSJulian Elischer 			 */
55144990b8cSJulian Elischer 			td->td_flags &= ~TDF_UPCALLING;	/* Hmmmm. */
55244990b8cSJulian Elischer 			error = suword((caddr_t)td->td_kse->ke_mailbox +
55344990b8cSJulian Elischer 			    offsetof(struct kse_mailbox, kmbx_current_thread),
55444990b8cSJulian Elischer 			    0);
55544990b8cSJulian Elischer 		}
55644990b8cSJulian Elischer 		/*
55744990b8cSJulian Elischer 		 * Stop any chance that we may be separated from
55844990b8cSJulian Elischer 		 * the KSE we are currently on. This is "biting the bullet",
55944990b8cSJulian Elischer 		 * we are committing to go to user space as as THIS KSE here.
56044990b8cSJulian Elischer 		 */
56144990b8cSJulian Elischer cont:
56244990b8cSJulian Elischer 		td->td_flags &= ~TDF_UNBOUND;
56344990b8cSJulian Elischer 	}
56444990b8cSJulian Elischer 	return (error);
56544990b8cSJulian Elischer }
56644990b8cSJulian Elischer 
56744990b8cSJulian Elischer /*
56844990b8cSJulian Elischer  * Enforce single-threading.
56944990b8cSJulian Elischer  *
57044990b8cSJulian Elischer  * Returns 1 if the caller must abort (another thread is waiting to
57144990b8cSJulian Elischer  * exit the process or similar). Process is locked!
57244990b8cSJulian Elischer  * Returns 0 when you are successfully the only thread running.
57344990b8cSJulian Elischer  * A process has successfully single threaded in the suspend mode when
57444990b8cSJulian Elischer  * There are no threads in user mode. Threads in the kernel must be
57544990b8cSJulian Elischer  * allowed to continue until they get to the user boundary. They may even
57644990b8cSJulian Elischer  * copy out their return values and data before suspending. They may however be
57744990b8cSJulian Elischer  * accellerated in reaching the user boundary as we will wake up
57844990b8cSJulian Elischer  * any sleeping threads that are interruptable. (PCATCH).
57944990b8cSJulian Elischer  */
58044990b8cSJulian Elischer int
58144990b8cSJulian Elischer thread_single(int force_exit)
58244990b8cSJulian Elischer {
58344990b8cSJulian Elischer 	struct thread *td;
58444990b8cSJulian Elischer 	struct thread *td2;
58544990b8cSJulian Elischer 	struct proc *p;
58644990b8cSJulian Elischer 
58744990b8cSJulian Elischer 	td = curthread;
58844990b8cSJulian Elischer 	p = td->td_proc;
58944990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
59044990b8cSJulian Elischer 	KASSERT((td != NULL), ("curthread is NULL"));
59144990b8cSJulian Elischer 
59244990b8cSJulian Elischer 	if ((p->p_flag & P_KSES) == 0)
59344990b8cSJulian Elischer 		return (0);
59444990b8cSJulian Elischer 
595e3b9bf71SJulian Elischer 	/* Is someone already single threading? */
596e3b9bf71SJulian Elischer 	if (p->p_singlethread)
59744990b8cSJulian Elischer 		return (1);
59844990b8cSJulian Elischer 
5991279572aSDavid Xu 	if (force_exit == SINGLE_EXIT)
60044990b8cSJulian Elischer 		p->p_flag |= P_SINGLE_EXIT;
60144990b8cSJulian Elischer 	else
60244990b8cSJulian Elischer 		p->p_flag &= ~P_SINGLE_EXIT;
6031279572aSDavid Xu 	p->p_flag |= P_STOPPED_SINGLE;
60444990b8cSJulian Elischer 	p->p_singlethread = td;
60544990b8cSJulian Elischer 	while ((p->p_numthreads - p->p_suspcount) != 1) {
60644990b8cSJulian Elischer 		FOREACH_THREAD_IN_PROC(p, td2) {
60744990b8cSJulian Elischer 			if (td2 == td)
60844990b8cSJulian Elischer 				continue;
60944990b8cSJulian Elischer 			switch(td2->td_state) {
61044990b8cSJulian Elischer 			case TDS_SUSPENDED:
6111279572aSDavid Xu 				if (force_exit == SINGLE_EXIT) {
612e3b9bf71SJulian Elischer 					mtx_lock_spin(&sched_lock);
61344990b8cSJulian Elischer 					TAILQ_REMOVE(&p->p_suspended,
61444990b8cSJulian Elischer 					    td, td_runq);
6158625e372SJulian Elischer 					p->p_suspcount--;
61644990b8cSJulian Elischer 					setrunqueue(td); /* Should suicide. */
617e3b9bf71SJulian Elischer 					mtx_unlock_spin(&sched_lock);
61844990b8cSJulian Elischer 				}
61944990b8cSJulian Elischer 			case TDS_SLP:
620e3b9bf71SJulian Elischer 				if (td2->td_flags & TDF_CVWAITQ)
62144990b8cSJulian Elischer 					cv_abort(td2);
622e3b9bf71SJulian Elischer 				else
62344990b8cSJulian Elischer 					abortsleep(td2);
62444990b8cSJulian Elischer 				break;
625e3b9bf71SJulian Elischer 			/* case TDS RUNNABLE: XXXKSE maybe raise priority? */
6268625e372SJulian Elischer 			default: 	/* needed to avoid an error */
6278625e372SJulian Elischer 				break;
62844990b8cSJulian Elischer 			}
62944990b8cSJulian Elischer 		}
63044990b8cSJulian Elischer 		/*
63144990b8cSJulian Elischer 		 * Wake us up when everyone else has suspended.
632e3b9bf71SJulian Elischer 		 * In the mean time we suspend as well.
63344990b8cSJulian Elischer 		 */
63444990b8cSJulian Elischer 		mtx_lock_spin(&sched_lock);
63544990b8cSJulian Elischer 		TAILQ_INSERT_TAIL(&p->p_suspended, td, td_runq);
63644990b8cSJulian Elischer 		td->td_state = TDS_SUSPENDED;
63744990b8cSJulian Elischer 		p->p_suspcount++;
63844990b8cSJulian Elischer 		mtx_unlock(&Giant);
63944990b8cSJulian Elischer 		PROC_UNLOCK(p);
64044990b8cSJulian Elischer 		mi_switch();
64144990b8cSJulian Elischer 		mtx_unlock_spin(&sched_lock);
64244990b8cSJulian Elischer 		mtx_lock(&Giant);
64344990b8cSJulian Elischer 		PROC_LOCK(p);
64444990b8cSJulian Elischer 	}
64544990b8cSJulian Elischer 	return (0);
64644990b8cSJulian Elischer }
64744990b8cSJulian Elischer 
64844990b8cSJulian Elischer /*
64944990b8cSJulian Elischer  * Called in from locations that can safely check to see
65044990b8cSJulian Elischer  * whether we have to suspend or at least throttle for a
65144990b8cSJulian Elischer  * single-thread event (e.g. fork).
65244990b8cSJulian Elischer  *
65344990b8cSJulian Elischer  * Such locations include userret().
65444990b8cSJulian Elischer  * If the "return_instead" argument is non zero, the thread must be able to
65544990b8cSJulian Elischer  * accept 0 (caller may continue), or 1 (caller must abort) as a result.
65644990b8cSJulian Elischer  *
65744990b8cSJulian Elischer  * The 'return_instead' argument tells the function if it may do a
65844990b8cSJulian Elischer  * thread_exit() or suspend, or whether the caller must abort and back
65944990b8cSJulian Elischer  * out instead.
66044990b8cSJulian Elischer  *
66144990b8cSJulian Elischer  * If the thread that set the single_threading request has set the
66244990b8cSJulian Elischer  * P_SINGLE_EXIT bit in the process flags then this call will never return
66344990b8cSJulian Elischer  * if 'return_instead' is false, but will exit.
66444990b8cSJulian Elischer  *
66544990b8cSJulian Elischer  * P_SINGLE_EXIT | return_instead == 0| return_instead != 0
66644990b8cSJulian Elischer  *---------------+--------------------+---------------------
66744990b8cSJulian Elischer  *       0       | returns 0          |   returns 0 or 1
66844990b8cSJulian Elischer  *               | when ST ends       |   immediatly
66944990b8cSJulian Elischer  *---------------+--------------------+---------------------
67044990b8cSJulian Elischer  *       1       | thread exits       |   returns 1
67144990b8cSJulian Elischer  *               |                    |  immediatly
67244990b8cSJulian Elischer  * 0 = thread_exit() or suspension ok,
67344990b8cSJulian Elischer  * other = return error instead of stopping the thread.
67444990b8cSJulian Elischer  *
67544990b8cSJulian Elischer  * While a full suspension is under effect, even a single threading
67644990b8cSJulian Elischer  * thread would be suspended if it made this call (but it shouldn't).
67744990b8cSJulian Elischer  * This call should only be made from places where
67844990b8cSJulian Elischer  * thread_exit() would be safe as that may be the outcome unless
67944990b8cSJulian Elischer  * return_instead is set.
68044990b8cSJulian Elischer  */
68144990b8cSJulian Elischer int
68244990b8cSJulian Elischer thread_suspend_check(int return_instead)
68344990b8cSJulian Elischer {
68444990b8cSJulian Elischer 	struct thread *td = curthread;
68544990b8cSJulian Elischer 	struct proc *p = td->td_proc;
68644990b8cSJulian Elischer 
68744990b8cSJulian Elischer 	td = curthread;
68844990b8cSJulian Elischer 	p = td->td_proc;
68944990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
69044990b8cSJulian Elischer 	while (P_SHOULDSTOP(p)) {
6911279572aSDavid Xu 		if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
69244990b8cSJulian Elischer 			KASSERT(p->p_singlethread != NULL,
69344990b8cSJulian Elischer 			    ("singlethread not set"));
69444990b8cSJulian Elischer 			/*
695e3b9bf71SJulian Elischer 			 * The only suspension in action is a
696e3b9bf71SJulian Elischer 			 * single-threading. Single threader need not stop.
697b6d5995eSJulian Elischer 			 * XXX Should be safe to access unlocked
698b6d5995eSJulian Elischer 			 * as it can only be set to be true by us.
69944990b8cSJulian Elischer 			 */
700e3b9bf71SJulian Elischer 			if (p->p_singlethread == td)
70144990b8cSJulian Elischer 				return (0);	/* Exempt from stopping. */
70244990b8cSJulian Elischer 		}
703e3b9bf71SJulian Elischer 		if (return_instead)
70444990b8cSJulian Elischer 			return (1);
70544990b8cSJulian Elischer 
70644990b8cSJulian Elischer 		/*
70744990b8cSJulian Elischer 		 * If the process is waiting for us to exit,
70844990b8cSJulian Elischer 		 * this thread should just suicide.
7091279572aSDavid Xu 		 * Assumes that P_SINGLE_EXIT implies P_STOPPED_SINGLE.
71044990b8cSJulian Elischer 		 */
71144990b8cSJulian Elischer 		if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) {
71244990b8cSJulian Elischer 			mtx_lock_spin(&sched_lock);
71344990b8cSJulian Elischer 			while (mtx_owned(&Giant))
71444990b8cSJulian Elischer 				mtx_unlock(&Giant);
71544990b8cSJulian Elischer 			thread_exit();
71644990b8cSJulian Elischer 		}
71744990b8cSJulian Elischer 
71844990b8cSJulian Elischer 		/*
71944990b8cSJulian Elischer 		 * When a thread suspends, it just
72044990b8cSJulian Elischer 		 * moves to the processes's suspend queue
72144990b8cSJulian Elischer 		 * and stays there.
72244990b8cSJulian Elischer 		 *
72344990b8cSJulian Elischer 		 * XXXKSE if TDF_BOUND is true
72444990b8cSJulian Elischer 		 * it will not release it's KSE which might
72544990b8cSJulian Elischer 		 * lead to deadlock if there are not enough KSEs
72644990b8cSJulian Elischer 		 * to complete all waiting threads.
72744990b8cSJulian Elischer 		 * Maybe be able to 'lend' it out again.
72844990b8cSJulian Elischer 		 * (lent kse's can not go back to userland?)
72944990b8cSJulian Elischer 		 * and can only be lent in STOPPED state.
73044990b8cSJulian Elischer 		 */
731721e5910SJulian Elischer 		mtx_lock_spin(&sched_lock);
7321279572aSDavid Xu 		if ((p->p_flag & P_STOPPED_SIG) &&
733721e5910SJulian Elischer 		    (p->p_suspcount+1 == p->p_numthreads)) {
734721e5910SJulian Elischer 			mtx_unlock_spin(&sched_lock);
735721e5910SJulian Elischer 			PROC_LOCK(p->p_pptr);
736721e5910SJulian Elischer 			if ((p->p_pptr->p_procsig->ps_flag &
737721e5910SJulian Elischer 				PS_NOCLDSTOP) == 0) {
738721e5910SJulian Elischer 				psignal(p->p_pptr, SIGCHLD);
739721e5910SJulian Elischer 			}
740721e5910SJulian Elischer 			PROC_UNLOCK(p->p_pptr);
741721e5910SJulian Elischer 		}
74244990b8cSJulian Elischer 		mtx_assert(&Giant, MA_NOTOWNED);
74344990b8cSJulian Elischer 		mtx_lock_spin(&sched_lock);
74444990b8cSJulian Elischer 		p->p_suspcount++;
74544990b8cSJulian Elischer 		td->td_state = TDS_SUSPENDED;
74644990b8cSJulian Elischer 		TAILQ_INSERT_TAIL(&p->p_suspended, td, td_runq);
74744990b8cSJulian Elischer 		PROC_UNLOCK(p);
7481279572aSDavid Xu 		if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
749cf19bf91SJulian Elischer 			if (p->p_numthreads == p->p_suspcount) {
750cf19bf91SJulian Elischer 				TAILQ_REMOVE(&p->p_suspended,
751cf19bf91SJulian Elischer 				    p->p_singlethread, td_runq);
7528625e372SJulian Elischer 				p->p_suspcount--;
753cf19bf91SJulian Elischer 				setrunqueue(p->p_singlethread);
754cf19bf91SJulian Elischer 			}
755cf19bf91SJulian Elischer 		}
75620568366SJulian Elischer 		p->p_stats->p_ru.ru_nivcsw++;
75744990b8cSJulian Elischer 		mi_switch();
75844990b8cSJulian Elischer 		mtx_unlock_spin(&sched_lock);
75944990b8cSJulian Elischer 		PROC_LOCK(p);
76044990b8cSJulian Elischer 	}
76144990b8cSJulian Elischer 	return (0);
76244990b8cSJulian Elischer }
76344990b8cSJulian Elischer 
76435c32a76SDavid Xu void
76535c32a76SDavid Xu thread_suspend_one(struct thread *td)
76635c32a76SDavid Xu {
76735c32a76SDavid Xu 	struct proc *p = td->td_proc;
76835c32a76SDavid Xu 
76935c32a76SDavid Xu 	mtx_assert(&sched_lock, MA_OWNED);
77035c32a76SDavid Xu 	p->p_suspcount++;
77135c32a76SDavid Xu 	td->td_state = TDS_SUSPENDED;
77235c32a76SDavid Xu 	TAILQ_INSERT_TAIL(&p->p_suspended, td, td_runq);
77335c32a76SDavid Xu }
77435c32a76SDavid Xu 
77535c32a76SDavid Xu void
77635c32a76SDavid Xu thread_unsuspend_one(struct thread *td)
77735c32a76SDavid Xu {
77835c32a76SDavid Xu 	struct proc *p = td->td_proc;
77935c32a76SDavid Xu 
78035c32a76SDavid Xu 	mtx_assert(&sched_lock, MA_OWNED);
78135c32a76SDavid Xu 	TAILQ_REMOVE(&p->p_suspended, td, td_runq);
78235c32a76SDavid Xu 	p->p_suspcount--;
78335c32a76SDavid Xu 	if (td->td_wchan != NULL) {
78435c32a76SDavid Xu 		td->td_state = TDS_SLP;
78535c32a76SDavid Xu 	} else {
78635c32a76SDavid Xu 		if (td->td_ksegrp->kg_slptime > 1) {
78735c32a76SDavid Xu 			updatepri(td->td_ksegrp);
78835c32a76SDavid Xu 			td->td_ksegrp->kg_slptime = 0;
78935c32a76SDavid Xu 		}
79035c32a76SDavid Xu 		setrunqueue(td);
79135c32a76SDavid Xu 	}
79235c32a76SDavid Xu }
79335c32a76SDavid Xu 
79444990b8cSJulian Elischer /*
79544990b8cSJulian Elischer  * Allow all threads blocked by single threading to continue running.
79644990b8cSJulian Elischer  */
79744990b8cSJulian Elischer void
79844990b8cSJulian Elischer thread_unsuspend(struct proc *p)
79944990b8cSJulian Elischer {
80044990b8cSJulian Elischer 	struct thread *td;
80144990b8cSJulian Elischer 
802b6d5995eSJulian Elischer 	mtx_assert(&sched_lock, MA_OWNED);
80344990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
80444990b8cSJulian Elischer 	if (!P_SHOULDSTOP(p)) {
80544990b8cSJulian Elischer 		while (( td = TAILQ_FIRST(&p->p_suspended))) {
80635c32a76SDavid Xu 			thread_unsuspend_one(td);
80744990b8cSJulian Elischer 		}
8081279572aSDavid Xu 	} else if ((P_SHOULDSTOP(p) == P_STOPPED_SINGLE) &&
80944990b8cSJulian Elischer 	    (p->p_numthreads == p->p_suspcount)) {
81044990b8cSJulian Elischer 		/*
81144990b8cSJulian Elischer 		 * Stopping everything also did the job for the single
81244990b8cSJulian Elischer 		 * threading request. Now we've downgraded to single-threaded,
81344990b8cSJulian Elischer 		 * let it continue.
81444990b8cSJulian Elischer 		 */
81535c32a76SDavid Xu 		thread_unsuspend_one(p->p_singlethread);
81644990b8cSJulian Elischer 	}
81744990b8cSJulian Elischer }
81844990b8cSJulian Elischer 
81944990b8cSJulian Elischer void
82044990b8cSJulian Elischer thread_single_end(void)
82144990b8cSJulian Elischer {
82244990b8cSJulian Elischer 	struct thread *td;
82344990b8cSJulian Elischer 	struct proc *p;
82444990b8cSJulian Elischer 
82544990b8cSJulian Elischer 	td = curthread;
82644990b8cSJulian Elischer 	p = td->td_proc;
82744990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
8281279572aSDavid Xu 	p->p_flag &= ~P_STOPPED_SINGLE;
82944990b8cSJulian Elischer 	p->p_singlethread = NULL;
83049539972SJulian Elischer 	/*
83149539972SJulian Elischer 	 * If there are other threads they mey now run,
83249539972SJulian Elischer 	 * unless of course there is a blanket 'stop order'
83349539972SJulian Elischer 	 * on the process. The single threader must be allowed
83449539972SJulian Elischer 	 * to continue however as this is a bad place to stop.
83549539972SJulian Elischer 	 */
83649539972SJulian Elischer 	if ((p->p_numthreads != 1) && (!P_SHOULDSTOP(p))) {
83749539972SJulian Elischer 		mtx_lock_spin(&sched_lock);
83849539972SJulian Elischer 		while (( td = TAILQ_FIRST(&p->p_suspended))) {
83949539972SJulian Elischer 			TAILQ_REMOVE(&p->p_suspended, td, td_runq);
84049539972SJulian Elischer 			p->p_suspcount--;
84149539972SJulian Elischer 			setrunqueue(td);
84244990b8cSJulian Elischer 		}
84349539972SJulian Elischer 		mtx_unlock_spin(&sched_lock);
84449539972SJulian Elischer 	}
84549539972SJulian Elischer }
84649539972SJulian Elischer 
84744990b8cSJulian Elischer 
848