xref: /freebsd/sys/kern/kern_thread.c (revision 49539972e9daccdf4ec5ee14dc7415126efd3ccf)
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);
32344990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
32444990b8cSJulian Elischer 	CTR1(KTR_PROC, "thread_exit: thread %p", td);
32544990b8cSJulian Elischer 	KASSERT(!mtx_owned(&Giant), ("dying thread owns giant"));
32644990b8cSJulian Elischer 
32744990b8cSJulian Elischer 	if (ke->ke_tdspare != NULL) {
32844990b8cSJulian Elischer 		thread_stash(ke->ke_tdspare);
32944990b8cSJulian Elischer 		ke->ke_tdspare = NULL;
33044990b8cSJulian Elischer 	}
33144990b8cSJulian Elischer 	cpu_thread_exit(td);	/* XXXSMP */
33244990b8cSJulian Elischer 
33344990b8cSJulian Elischer 	/* Reassign this thread's KSE. */
33444990b8cSJulian Elischer 	if (ke != NULL) {
33544990b8cSJulian Elischer 		ke->ke_thread = NULL;
33644990b8cSJulian Elischer 		td->td_kse = NULL;
33744990b8cSJulian Elischer 		ke->ke_state = KES_UNQUEUED;
33844990b8cSJulian Elischer 		kse_reassign(ke);
33944990b8cSJulian Elischer 	}
34044990b8cSJulian Elischer 
34144990b8cSJulian Elischer 	/* Unlink this thread from its proc. and the kseg */
34244990b8cSJulian Elischer 	if (p != NULL) {
34344990b8cSJulian Elischer 		TAILQ_REMOVE(&p->p_threads, td, td_plist);
34444990b8cSJulian Elischer 		p->p_numthreads--;
34544990b8cSJulian Elischer 		if (kg != NULL) {
34644990b8cSJulian Elischer 			TAILQ_REMOVE(&kg->kg_threads, td, td_kglist);
34744990b8cSJulian Elischer 			kg->kg_numthreads--;
34844990b8cSJulian Elischer 		}
34944990b8cSJulian Elischer 		/*
35044990b8cSJulian Elischer 		 * The test below is NOT true if we are the
35144990b8cSJulian Elischer 		 * sole exiting thread. P_STOPPED_SNGL is unset
35244990b8cSJulian Elischer 		 * in exit1() after it is the only survivor.
35344990b8cSJulian Elischer 		 */
35444990b8cSJulian Elischer 		if (P_SHOULDSTOP(p) == P_STOPPED_SNGL) {
35544990b8cSJulian Elischer 			if (p->p_numthreads == p->p_suspcount) {
35644990b8cSJulian Elischer 				TAILQ_REMOVE(&p->p_suspended,
35744990b8cSJulian Elischer 				    p->p_singlethread, td_runq);
35844990b8cSJulian Elischer 				setrunqueue(p->p_singlethread);
35944990b8cSJulian Elischer 				p->p_suspcount--;
36044990b8cSJulian Elischer 			}
36144990b8cSJulian Elischer 		}
36244990b8cSJulian Elischer 	}
36344990b8cSJulian Elischer 	td->td_state	= TDS_SURPLUS;
36444990b8cSJulian Elischer 	td->td_proc	= NULL;
36544990b8cSJulian Elischer 	td->td_ksegrp	= NULL;
36644990b8cSJulian Elischer 	td->td_last_kse	= NULL;
36744990b8cSJulian Elischer 	ke->ke_tdspare = td;
36844990b8cSJulian Elischer 	PROC_UNLOCK(p);
36944990b8cSJulian Elischer 	cpu_throw();
37044990b8cSJulian Elischer 	/* NOTREACHED */
37144990b8cSJulian Elischer }
37244990b8cSJulian Elischer 
37344990b8cSJulian Elischer /*
37444990b8cSJulian Elischer  * Link a thread to a process.
37544990b8cSJulian Elischer  *
37644990b8cSJulian Elischer  * Note that we do not link to the proc's ucred here.
37744990b8cSJulian Elischer  * The thread is linked as if running but no KSE assigned.
37844990b8cSJulian Elischer  */
37944990b8cSJulian Elischer void
38044990b8cSJulian Elischer thread_link(struct thread *td, struct ksegrp *kg)
38144990b8cSJulian Elischer {
38244990b8cSJulian Elischer 	struct proc *p;
38344990b8cSJulian Elischer 
38444990b8cSJulian Elischer 	p = kg->kg_proc;
38544990b8cSJulian Elischer 	td->td_state = TDS_NEW;
38644990b8cSJulian Elischer 	td->td_proc	= p;
38744990b8cSJulian Elischer 	td->td_ksegrp	= kg;
38844990b8cSJulian Elischer 	td->td_last_kse	= NULL;
38944990b8cSJulian Elischer 
39044990b8cSJulian Elischer 	TAILQ_INSERT_HEAD(&p->p_threads, td, td_plist);
39144990b8cSJulian Elischer 	TAILQ_INSERT_HEAD(&kg->kg_threads, td, td_kglist);
39244990b8cSJulian Elischer 	p->p_numthreads++;
39344990b8cSJulian Elischer 	kg->kg_numthreads++;
39444990b8cSJulian Elischer 	if (oiks_debug && p->p_numthreads > 4) {
39544990b8cSJulian Elischer 		printf("OIKS %d\n", p->p_numthreads);
39644990b8cSJulian Elischer 		if (oiks_debug > 1)
39744990b8cSJulian Elischer 			Debugger("OIKS");
39844990b8cSJulian Elischer 	}
39944990b8cSJulian Elischer 	td->td_critnest = 0;
40044990b8cSJulian Elischer 	td->td_kse	= NULL;
40144990b8cSJulian Elischer }
40244990b8cSJulian Elischer 
40344990b8cSJulian Elischer /*
40444990b8cSJulian Elischer  * Set up the upcall pcb in either a given thread or a new one
40544990b8cSJulian Elischer  * if none given. Use the upcall for the given KSE
40644990b8cSJulian Elischer  * XXXKSE possibly fix cpu_set_upcall() to not need td->td_kse set.
40744990b8cSJulian Elischer  */
40844990b8cSJulian Elischer struct thread *
40944990b8cSJulian Elischer thread_schedule_upcall(struct thread *td, struct kse *ke)
41044990b8cSJulian Elischer {
41144990b8cSJulian Elischer 	struct thread *td2;
41244990b8cSJulian Elischer 
41344990b8cSJulian Elischer 	mtx_assert(&sched_lock, MA_OWNED);
41444990b8cSJulian Elischer 	if (ke->ke_tdspare != NULL) {
41544990b8cSJulian Elischer 		td2 = ke->ke_tdspare;
41644990b8cSJulian Elischer 		ke->ke_tdspare = NULL;
41744990b8cSJulian Elischer 	} else {
41844990b8cSJulian Elischer 		mtx_unlock_spin(&sched_lock);
41944990b8cSJulian Elischer 		td2 = thread_alloc();
42044990b8cSJulian Elischer 		mtx_lock_spin(&sched_lock);
42144990b8cSJulian Elischer 	}
42244990b8cSJulian Elischer 	CTR3(KTR_PROC, "thread_schedule_upcall: thread %p (pid %d, %s)",
42344990b8cSJulian Elischer 	     td, td->td_proc->p_pid, td->td_proc->p_comm);
42444990b8cSJulian Elischer 	thread_link(td2, ke->ke_ksegrp);
42544990b8cSJulian Elischer 	cpu_set_upcall(td2, ke->ke_pcb);
42644990b8cSJulian Elischer 	td2->td_ucred = crhold(td->td_ucred);
42744990b8cSJulian Elischer 	td2->td_flags = TDF_UNBOUND|TDF_UPCALLING;
42844990b8cSJulian Elischer 	td2->td_priority = td->td_priority;
42944990b8cSJulian Elischer 	setrunqueue(td2);
43044990b8cSJulian Elischer 	return (td2);
43144990b8cSJulian Elischer }
43244990b8cSJulian Elischer 
43344990b8cSJulian Elischer /*
43444990b8cSJulian Elischer  * The extra work we go through if we are a threaded process when we
43544990b8cSJulian Elischer  * return to userland
43644990b8cSJulian Elischer  *
43744990b8cSJulian Elischer  * If we are a KSE process and returning to user mode, check for
43844990b8cSJulian Elischer  * extra work to do before we return (e.g. for more syscalls
43944990b8cSJulian Elischer  * to complete first).  If we were in a critical section, we should
44044990b8cSJulian Elischer  * just return to let it finish. Same if we were in the UTS (in
44144990b8cSJulian Elischer  * which case we will have no thread mailbox registered).  The only
44244990b8cSJulian Elischer  * traps we suport will have set the mailbox.  We will clear it here.
44344990b8cSJulian Elischer  */
44444990b8cSJulian Elischer int
44544990b8cSJulian Elischer thread_userret(struct proc *p, struct ksegrp *kg, struct kse *ke,
44644990b8cSJulian Elischer     struct thread *td, struct trapframe *frame)
44744990b8cSJulian Elischer {
44844990b8cSJulian Elischer 	int error = 0;
44944990b8cSJulian Elischer 
45044990b8cSJulian Elischer 	if (ke->ke_tdspare == NULL) {
45144990b8cSJulian Elischer 		ke->ke_tdspare = thread_alloc();
45244990b8cSJulian Elischer 	}
45344990b8cSJulian Elischer 	if (td->td_flags & TDF_UNBOUND) {
45444990b8cSJulian Elischer 		/*
45544990b8cSJulian Elischer 		 * Are we returning from a thread that had a mailbox?
45644990b8cSJulian Elischer 		 *
45744990b8cSJulian Elischer 		 * XXX Maybe this should be in a separate function.
45844990b8cSJulian Elischer 		 */
45944990b8cSJulian Elischer 		if (((td->td_flags & TDF_UPCALLING) == 0) && td->td_mailbox) {
46044990b8cSJulian Elischer 			/*
46144990b8cSJulian Elischer 			 * [XXXKSE Future enhancement]
46244990b8cSJulian Elischer 			 * We could also go straight back to the syscall
46344990b8cSJulian Elischer 			 * if we never had to do an upcall since then.
46444990b8cSJulian Elischer 			 * If the KSE's copy is == the thread's copy..
46544990b8cSJulian Elischer 			 * AND there are no other completed threads.
46644990b8cSJulian Elischer 			 */
46744990b8cSJulian Elischer 			/*
46844990b8cSJulian Elischer 			 * We will go back as an upcall or go do another thread.
46944990b8cSJulian Elischer 			 * Either way we need to save the context back to
47044990b8cSJulian Elischer 			 * the user thread mailbox.
47144990b8cSJulian Elischer 			 * So the UTS can restart it later.
47244990b8cSJulian Elischer 			 */
47344990b8cSJulian Elischer 			error = thread_export_context(td);
47444990b8cSJulian Elischer 			td->td_mailbox = NULL;
47544990b8cSJulian Elischer 			if (error) {
47644990b8cSJulian Elischer 				/*
47744990b8cSJulian Elischer 				 * Failing to do the KSE
47844990b8cSJulian Elischer 				 * operation just defaults operation
47944990b8cSJulian Elischer 				 * back to synchonous operation.
48044990b8cSJulian Elischer 				 */
48144990b8cSJulian Elischer 				goto cont;
48244990b8cSJulian Elischer 			}
48344990b8cSJulian Elischer 
48444990b8cSJulian Elischer 			if (TAILQ_FIRST(&kg->kg_runq)) {
48544990b8cSJulian Elischer 				/*
48644990b8cSJulian Elischer 				 * Uh-oh.. don't return to the user.
48744990b8cSJulian Elischer 				 * Instead, switch to the thread that
48844990b8cSJulian Elischer 				 * needs to run. The question is:
48944990b8cSJulian Elischer 				 * What do we do with the thread we have now?
49044990b8cSJulian Elischer 				 * We have put the completion block
49144990b8cSJulian Elischer 				 * on the kse mailbox. If we had more energy,
49244990b8cSJulian Elischer 				 * we could lazily do so, assuming someone
49344990b8cSJulian Elischer 				 * else might get to userland earlier
49444990b8cSJulian Elischer 				 * and deliver it earlier than we could.
49544990b8cSJulian Elischer 				 * To do that we could save it off the KSEG.
49644990b8cSJulian Elischer 				 * An upcalling KSE would 'reap' all completed
49744990b8cSJulian Elischer 				 * threads.
49844990b8cSJulian Elischer 				 * Being in a hurry, we'll do nothing and
49944990b8cSJulian Elischer 				 * leave it on the current KSE for now.
50044990b8cSJulian Elischer 				 *
50144990b8cSJulian Elischer 				 * As for the other threads to run;
50244990b8cSJulian Elischer 				 * we COULD rush through all the threads
50344990b8cSJulian Elischer 				 * in this KSEG at this priority, or we
50444990b8cSJulian Elischer 				 * could throw the ball back into the court
50544990b8cSJulian Elischer 				 * and just run the highest prio kse available.
50644990b8cSJulian Elischer 				 * What is OUR priority?
50744990b8cSJulian Elischer 				 * the priority of the highest sycall waiting
50844990b8cSJulian Elischer 				 * to be returned?
50944990b8cSJulian Elischer 				 * For now, just let another KSE run (easiest).
51044990b8cSJulian Elischer 				 */
51144990b8cSJulian Elischer 				PROC_LOCK(p);
51244990b8cSJulian Elischer 				mtx_lock_spin(&sched_lock);
51344990b8cSJulian Elischer 				thread_exit(); /* Abandon current thread. */
51444990b8cSJulian Elischer 				/* NOTREACHED */
51544990b8cSJulian Elischer 			} else { /* if (number of returning syscalls = 1) */
51644990b8cSJulian Elischer 				/*
51744990b8cSJulian Elischer 				 * Swap our frame for the upcall frame.
51844990b8cSJulian Elischer 				 *
51944990b8cSJulian Elischer 				 * XXXKSE Assumes we are going to user land
52044990b8cSJulian Elischer 				 * and not nested in the kernel
52144990b8cSJulian Elischer 				 */
52244990b8cSJulian Elischer 				td->td_flags |= TDF_UPCALLING;
52344990b8cSJulian Elischer 			}
52444990b8cSJulian Elischer 		}
52544990b8cSJulian Elischer 		/*
52644990b8cSJulian Elischer 		 * This is NOT just an 'else' clause for the above test...
52744990b8cSJulian Elischer 		 */
52844990b8cSJulian Elischer 		if (td->td_flags & TDF_UPCALLING) {
52944990b8cSJulian Elischer 			CTR3(KTR_PROC, "userret: upcall thread %p (pid %d, %s)",
53044990b8cSJulian Elischer 			    td, p->p_pid, p->p_comm);
53144990b8cSJulian Elischer 			/*
53244990b8cSJulian Elischer 			 * Make sure that it has the correct frame loaded.
53344990b8cSJulian Elischer 			 * While we know that we are on the same KSEGRP
53444990b8cSJulian Elischer 			 * as we were created on, we could very easily
53544990b8cSJulian Elischer 			 * have come in on another KSE. We therefore need
53644990b8cSJulian Elischer 			 * to do the copy of the frame after the last
53744990b8cSJulian Elischer 			 * possible switch() (the one above).
53844990b8cSJulian Elischer 			 */
53944990b8cSJulian Elischer 			bcopy(ke->ke_frame, frame, sizeof(struct trapframe));
54044990b8cSJulian Elischer 
54144990b8cSJulian Elischer 			/*
54244990b8cSJulian Elischer 			 * Decide what we are sending to the user
54344990b8cSJulian Elischer 			 * upcall sets one argument. The address of the mbox.
54444990b8cSJulian Elischer 			 */
54544990b8cSJulian Elischer 			cpu_set_args(td, ke);
54644990b8cSJulian Elischer 
54744990b8cSJulian Elischer 			/*
54844990b8cSJulian Elischer 			 * There is no more work to do and we are going to ride
54944990b8cSJulian Elischer 			 * this thead/KSE up to userland. Make sure the user's
55044990b8cSJulian Elischer 			 * pointer to the thread mailbox is cleared before we
55144990b8cSJulian Elischer 			 * re-enter the kernel next time for any reason..
55244990b8cSJulian Elischer 			 * We might as well do it here.
55344990b8cSJulian Elischer 			 */
55444990b8cSJulian Elischer 			td->td_flags &= ~TDF_UPCALLING;	/* Hmmmm. */
55544990b8cSJulian Elischer 			error = suword((caddr_t)td->td_kse->ke_mailbox +
55644990b8cSJulian Elischer 			    offsetof(struct kse_mailbox, kmbx_current_thread),
55744990b8cSJulian Elischer 			    0);
55844990b8cSJulian Elischer 		}
55944990b8cSJulian Elischer 		/*
56044990b8cSJulian Elischer 		 * Stop any chance that we may be separated from
56144990b8cSJulian Elischer 		 * the KSE we are currently on. This is "biting the bullet",
56244990b8cSJulian Elischer 		 * we are committing to go to user space as as THIS KSE here.
56344990b8cSJulian Elischer 		 */
56444990b8cSJulian Elischer cont:
56544990b8cSJulian Elischer 		td->td_flags &= ~TDF_UNBOUND;
56644990b8cSJulian Elischer 	}
56744990b8cSJulian Elischer 	return (error);
56844990b8cSJulian Elischer }
56944990b8cSJulian Elischer 
57044990b8cSJulian Elischer /*
57144990b8cSJulian Elischer  * Enforce single-threading.
57244990b8cSJulian Elischer  *
57344990b8cSJulian Elischer  * Returns 1 if the caller must abort (another thread is waiting to
57444990b8cSJulian Elischer  * exit the process or similar). Process is locked!
57544990b8cSJulian Elischer  * Returns 0 when you are successfully the only thread running.
57644990b8cSJulian Elischer  * A process has successfully single threaded in the suspend mode when
57744990b8cSJulian Elischer  * There are no threads in user mode. Threads in the kernel must be
57844990b8cSJulian Elischer  * allowed to continue until they get to the user boundary. They may even
57944990b8cSJulian Elischer  * copy out their return values and data before suspending. They may however be
58044990b8cSJulian Elischer  * accellerated in reaching the user boundary as we will wake up
58144990b8cSJulian Elischer  * any sleeping threads that are interruptable. (PCATCH).
58244990b8cSJulian Elischer  */
58344990b8cSJulian Elischer int
58444990b8cSJulian Elischer thread_single(int force_exit)
58544990b8cSJulian Elischer {
58644990b8cSJulian Elischer 	struct thread *td;
58744990b8cSJulian Elischer 	struct thread *td2;
58844990b8cSJulian Elischer 	struct proc *p;
58944990b8cSJulian Elischer 
59044990b8cSJulian Elischer 	td = curthread;
59144990b8cSJulian Elischer 	p = td->td_proc;
59244990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
59344990b8cSJulian Elischer 	KASSERT((td != NULL), ("curthread is NULL"));
59444990b8cSJulian Elischer 
59544990b8cSJulian Elischer 	if ((p->p_flag & P_KSES) == 0)
59644990b8cSJulian Elischer 		return (0);
59744990b8cSJulian Elischer 
598e3b9bf71SJulian Elischer 	/* Is someone already single threading? */
599e3b9bf71SJulian Elischer 	if (p->p_singlethread)
60044990b8cSJulian Elischer 		return (1);
60144990b8cSJulian Elischer 
60244990b8cSJulian Elischer 	if (force_exit == SNGLE_EXIT)
60344990b8cSJulian Elischer 		p->p_flag |= P_SINGLE_EXIT;
60444990b8cSJulian Elischer 	else
60544990b8cSJulian Elischer 		p->p_flag &= ~P_SINGLE_EXIT;
60644990b8cSJulian Elischer 	p->p_flag |= P_STOPPED_SNGL;
60744990b8cSJulian Elischer 	p->p_singlethread = td;
60844990b8cSJulian Elischer 	while ((p->p_numthreads - p->p_suspcount) != 1) {
60944990b8cSJulian Elischer 		FOREACH_THREAD_IN_PROC(p, td2) {
61044990b8cSJulian Elischer 			if (td2 == td)
61144990b8cSJulian Elischer 				continue;
61244990b8cSJulian Elischer 			switch(td2->td_state) {
61344990b8cSJulian Elischer 			case TDS_SUSPENDED:
61444990b8cSJulian Elischer 				if (force_exit == SNGLE_EXIT) {
615e3b9bf71SJulian Elischer 					mtx_lock_spin(&sched_lock);
61644990b8cSJulian Elischer 					TAILQ_REMOVE(&p->p_suspended,
61744990b8cSJulian Elischer 					    td, td_runq);
6188625e372SJulian Elischer 					p->p_suspcount--;
61944990b8cSJulian Elischer 					setrunqueue(td); /* Should suicide. */
620e3b9bf71SJulian Elischer 					mtx_unlock_spin(&sched_lock);
62144990b8cSJulian Elischer 				}
62244990b8cSJulian Elischer 			case TDS_SLP:
623e3b9bf71SJulian Elischer 				if (td2->td_flags & TDF_CVWAITQ)
62444990b8cSJulian Elischer 					cv_abort(td2);
625e3b9bf71SJulian Elischer 				else
62644990b8cSJulian Elischer 					abortsleep(td2);
62744990b8cSJulian Elischer 				break;
628e3b9bf71SJulian Elischer 			/* case TDS RUNNABLE: XXXKSE maybe raise priority? */
6298625e372SJulian Elischer 			default: 	/* needed to avoid an error */
6308625e372SJulian Elischer 				break;
63144990b8cSJulian Elischer 			}
63244990b8cSJulian Elischer 		}
63344990b8cSJulian Elischer 		/*
63444990b8cSJulian Elischer 		 * Wake us up when everyone else has suspended.
635e3b9bf71SJulian Elischer 		 * In the mean time we suspend as well.
63644990b8cSJulian Elischer 		 */
63744990b8cSJulian Elischer 		mtx_lock_spin(&sched_lock);
63844990b8cSJulian Elischer 		TAILQ_INSERT_TAIL(&p->p_suspended, td, td_runq);
63944990b8cSJulian Elischer 		td->td_state = TDS_SUSPENDED;
64044990b8cSJulian Elischer 		p->p_suspcount++;
64144990b8cSJulian Elischer 		mtx_unlock(&Giant);
64244990b8cSJulian Elischer 		PROC_UNLOCK(p);
64344990b8cSJulian Elischer 		mi_switch();
64444990b8cSJulian Elischer 		mtx_unlock_spin(&sched_lock);
64544990b8cSJulian Elischer 		mtx_lock(&Giant);
64644990b8cSJulian Elischer 		PROC_LOCK(p);
64744990b8cSJulian Elischer 	}
64844990b8cSJulian Elischer 	return (0);
64944990b8cSJulian Elischer }
65044990b8cSJulian Elischer 
65144990b8cSJulian Elischer /*
65244990b8cSJulian Elischer  * Called in from locations that can safely check to see
65344990b8cSJulian Elischer  * whether we have to suspend or at least throttle for a
65444990b8cSJulian Elischer  * single-thread event (e.g. fork).
65544990b8cSJulian Elischer  *
65644990b8cSJulian Elischer  * Such locations include userret().
65744990b8cSJulian Elischer  * If the "return_instead" argument is non zero, the thread must be able to
65844990b8cSJulian Elischer  * accept 0 (caller may continue), or 1 (caller must abort) as a result.
65944990b8cSJulian Elischer  *
66044990b8cSJulian Elischer  * The 'return_instead' argument tells the function if it may do a
66144990b8cSJulian Elischer  * thread_exit() or suspend, or whether the caller must abort and back
66244990b8cSJulian Elischer  * out instead.
66344990b8cSJulian Elischer  *
66444990b8cSJulian Elischer  * If the thread that set the single_threading request has set the
66544990b8cSJulian Elischer  * P_SINGLE_EXIT bit in the process flags then this call will never return
66644990b8cSJulian Elischer  * if 'return_instead' is false, but will exit.
66744990b8cSJulian Elischer  *
66844990b8cSJulian Elischer  * P_SINGLE_EXIT | return_instead == 0| return_instead != 0
66944990b8cSJulian Elischer  *---------------+--------------------+---------------------
67044990b8cSJulian Elischer  *       0       | returns 0          |   returns 0 or 1
67144990b8cSJulian Elischer  *               | when ST ends       |   immediatly
67244990b8cSJulian Elischer  *---------------+--------------------+---------------------
67344990b8cSJulian Elischer  *       1       | thread exits       |   returns 1
67444990b8cSJulian Elischer  *               |                    |  immediatly
67544990b8cSJulian Elischer  * 0 = thread_exit() or suspension ok,
67644990b8cSJulian Elischer  * other = return error instead of stopping the thread.
67744990b8cSJulian Elischer  *
67844990b8cSJulian Elischer  * While a full suspension is under effect, even a single threading
67944990b8cSJulian Elischer  * thread would be suspended if it made this call (but it shouldn't).
68044990b8cSJulian Elischer  * This call should only be made from places where
68144990b8cSJulian Elischer  * thread_exit() would be safe as that may be the outcome unless
68244990b8cSJulian Elischer  * return_instead is set.
68344990b8cSJulian Elischer  */
68444990b8cSJulian Elischer int
68544990b8cSJulian Elischer thread_suspend_check(int return_instead)
68644990b8cSJulian Elischer {
68744990b8cSJulian Elischer 	struct thread *td = curthread;
68844990b8cSJulian Elischer 	struct proc *p = td->td_proc;
68944990b8cSJulian Elischer 
69044990b8cSJulian Elischer 	td = curthread;
69144990b8cSJulian Elischer 	p = td->td_proc;
69244990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
69344990b8cSJulian Elischer 	while (P_SHOULDSTOP(p)) {
69444990b8cSJulian Elischer 		if (P_SHOULDSTOP(p) == P_STOPPED_SNGL) {
69544990b8cSJulian Elischer 			KASSERT(p->p_singlethread != NULL,
69644990b8cSJulian Elischer 			    ("singlethread not set"));
69744990b8cSJulian Elischer 			/*
698e3b9bf71SJulian Elischer 			 * The only suspension in action is a
699e3b9bf71SJulian Elischer 			 * single-threading. Single threader need not stop.
700b6d5995eSJulian Elischer 			 * XXX Should be safe to access unlocked
701b6d5995eSJulian Elischer 			 * as it can only be set to be true by us.
70244990b8cSJulian Elischer 			 */
703e3b9bf71SJulian Elischer 			if (p->p_singlethread == td)
70444990b8cSJulian Elischer 				return (0);	/* Exempt from stopping. */
70544990b8cSJulian Elischer 		}
706e3b9bf71SJulian Elischer 		if (return_instead)
70744990b8cSJulian Elischer 			return (1);
70844990b8cSJulian Elischer 
70944990b8cSJulian Elischer 		/*
71044990b8cSJulian Elischer 		 * If the process is waiting for us to exit,
71144990b8cSJulian Elischer 		 * this thread should just suicide.
71244990b8cSJulian Elischer 		 * Assumes that P_SINGLE_EXIT implies P_STOPPED_SNGL.
71344990b8cSJulian Elischer 		 */
71444990b8cSJulian Elischer 		if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) {
71544990b8cSJulian Elischer 			mtx_lock_spin(&sched_lock);
71644990b8cSJulian Elischer 			while (mtx_owned(&Giant))
71744990b8cSJulian Elischer 				mtx_unlock(&Giant);
71844990b8cSJulian Elischer 			thread_exit();
71944990b8cSJulian Elischer 		}
72044990b8cSJulian Elischer 
72144990b8cSJulian Elischer 		/*
72244990b8cSJulian Elischer 		 * When a thread suspends, it just
72344990b8cSJulian Elischer 		 * moves to the processes's suspend queue
72444990b8cSJulian Elischer 		 * and stays there.
72544990b8cSJulian Elischer 		 *
72644990b8cSJulian Elischer 		 * XXXKSE if TDF_BOUND is true
72744990b8cSJulian Elischer 		 * it will not release it's KSE which might
72844990b8cSJulian Elischer 		 * lead to deadlock if there are not enough KSEs
72944990b8cSJulian Elischer 		 * to complete all waiting threads.
73044990b8cSJulian Elischer 		 * Maybe be able to 'lend' it out again.
73144990b8cSJulian Elischer 		 * (lent kse's can not go back to userland?)
73244990b8cSJulian Elischer 		 * and can only be lent in STOPPED state.
73344990b8cSJulian Elischer 		 */
734721e5910SJulian Elischer 		mtx_lock_spin(&sched_lock);
735721e5910SJulian Elischer 		if ((p->p_flag & P_STOPPED_SGNL) &&
736721e5910SJulian Elischer 		    (p->p_suspcount+1 == p->p_numthreads)) {
737721e5910SJulian Elischer 			mtx_unlock_spin(&sched_lock);
738721e5910SJulian Elischer 			PROC_LOCK(p->p_pptr);
739721e5910SJulian Elischer 			if ((p->p_pptr->p_procsig->ps_flag &
740721e5910SJulian Elischer 				PS_NOCLDSTOP) == 0) {
741721e5910SJulian Elischer 				psignal(p->p_pptr, SIGCHLD);
742721e5910SJulian Elischer 			}
743721e5910SJulian Elischer 			PROC_UNLOCK(p->p_pptr);
744721e5910SJulian Elischer 		}
74544990b8cSJulian Elischer 		mtx_assert(&Giant, MA_NOTOWNED);
74644990b8cSJulian Elischer 		mtx_lock_spin(&sched_lock);
74744990b8cSJulian Elischer 		p->p_suspcount++;
74844990b8cSJulian Elischer 		td->td_state = TDS_SUSPENDED;
74944990b8cSJulian Elischer 		TAILQ_INSERT_TAIL(&p->p_suspended, td, td_runq);
75044990b8cSJulian Elischer 		PROC_UNLOCK(p);
751cf19bf91SJulian Elischer 		if (P_SHOULDSTOP(p) == P_STOPPED_SNGL) {
752cf19bf91SJulian Elischer 			if (p->p_numthreads == p->p_suspcount) {
753cf19bf91SJulian Elischer 				TAILQ_REMOVE(&p->p_suspended,
754cf19bf91SJulian Elischer 				    p->p_singlethread, td_runq);
7558625e372SJulian Elischer 				p->p_suspcount--;
756cf19bf91SJulian Elischer 				setrunqueue(p->p_singlethread);
757cf19bf91SJulian Elischer 			}
758cf19bf91SJulian Elischer 		}
75920568366SJulian Elischer 		p->p_stats->p_ru.ru_nivcsw++;
76044990b8cSJulian Elischer 		mi_switch();
76144990b8cSJulian Elischer 		mtx_unlock_spin(&sched_lock);
76244990b8cSJulian Elischer 		PROC_LOCK(p);
76344990b8cSJulian Elischer 	}
76444990b8cSJulian Elischer 	return (0);
76544990b8cSJulian Elischer }
76644990b8cSJulian Elischer 
76744990b8cSJulian Elischer /*
76844990b8cSJulian Elischer  * Allow all threads blocked by single threading to continue running.
76944990b8cSJulian Elischer  */
77044990b8cSJulian Elischer void
77144990b8cSJulian Elischer thread_unsuspend(struct proc *p)
77244990b8cSJulian Elischer {
77344990b8cSJulian Elischer 	struct thread *td;
77444990b8cSJulian Elischer 
775b6d5995eSJulian Elischer 	mtx_assert(&sched_lock, MA_OWNED);
77644990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
77744990b8cSJulian Elischer 	if (!P_SHOULDSTOP(p)) {
77844990b8cSJulian Elischer 		while (( td = TAILQ_FIRST(&p->p_suspended))) {
77944990b8cSJulian Elischer 			TAILQ_REMOVE(&p->p_suspended, td, td_runq);
78044990b8cSJulian Elischer 			p->p_suspcount--;
78144990b8cSJulian Elischer 			setrunqueue(td);
78244990b8cSJulian Elischer 		}
78344990b8cSJulian Elischer 	} else if ((P_SHOULDSTOP(p) == P_STOPPED_SNGL) &&
78444990b8cSJulian Elischer 	    (p->p_numthreads == p->p_suspcount)) {
78544990b8cSJulian Elischer 		/*
78644990b8cSJulian Elischer 		 * Stopping everything also did the job for the single
78744990b8cSJulian Elischer 		 * threading request. Now we've downgraded to single-threaded,
78844990b8cSJulian Elischer 		 * let it continue.
78944990b8cSJulian Elischer 		 */
79044990b8cSJulian Elischer 		TAILQ_REMOVE(&p->p_suspended, p->p_singlethread, td_runq);
79144990b8cSJulian Elischer 		p->p_suspcount--;
79244990b8cSJulian Elischer 		setrunqueue(p->p_singlethread);
79344990b8cSJulian Elischer 	}
79444990b8cSJulian Elischer }
79544990b8cSJulian Elischer 
79644990b8cSJulian Elischer void
79744990b8cSJulian Elischer thread_single_end(void)
79844990b8cSJulian Elischer {
79944990b8cSJulian Elischer 	struct thread *td;
80044990b8cSJulian Elischer 	struct proc *p;
80144990b8cSJulian Elischer 
80244990b8cSJulian Elischer 	td = curthread;
80344990b8cSJulian Elischer 	p = td->td_proc;
80444990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
80544990b8cSJulian Elischer 	p->p_flag &= ~P_STOPPED_SNGL;
80644990b8cSJulian Elischer 	p->p_singlethread = NULL;
80749539972SJulian Elischer 	/*
80849539972SJulian Elischer 	 * If there are other threads they mey now run,
80949539972SJulian Elischer 	 * unless of course there is a blanket 'stop order'
81049539972SJulian Elischer 	 * on the process. The single threader must be allowed
81149539972SJulian Elischer 	 * to continue however as this is a bad place to stop.
81249539972SJulian Elischer 	 */
81349539972SJulian Elischer 	if ((p->p_numthreads != 1) && (!P_SHOULDSTOP(p))) {
81449539972SJulian Elischer 		mtx_lock_spin(&sched_lock);
81549539972SJulian Elischer 		while (( td = TAILQ_FIRST(&p->p_suspended))) {
81649539972SJulian Elischer 			TAILQ_REMOVE(&p->p_suspended, td, td_runq);
81749539972SJulian Elischer 			p->p_suspcount--;
81849539972SJulian Elischer 			setrunqueue(td);
81944990b8cSJulian Elischer 		}
82049539972SJulian Elischer 		mtx_unlock_spin(&sched_lock);
82149539972SJulian Elischer 	}
82249539972SJulian Elischer }
82349539972SJulian Elischer 
82444990b8cSJulian Elischer 
825