xref: /freebsd/sys/kern/kern_thread.c (revision 44990b8cb8221e098affe1f49efd329185c338fb)
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 
5444990b8cSJulian Elischer /*
5544990b8cSJulian Elischer  * Thread related storage.
5644990b8cSJulian Elischer  */
5744990b8cSJulian Elischer static uma_zone_t thread_zone;
5844990b8cSJulian Elischer static int allocated_threads;
5944990b8cSJulian Elischer static int active_threads;
6044990b8cSJulian Elischer static int cached_threads;
6144990b8cSJulian Elischer 
6244990b8cSJulian Elischer SYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW, 0, "thread allocation");
6344990b8cSJulian Elischer 
6444990b8cSJulian Elischer SYSCTL_INT(_kern_threads, OID_AUTO, active, CTLFLAG_RD,
6544990b8cSJulian Elischer 	&active_threads, 0, "Number of active threads in system.");
6644990b8cSJulian Elischer 
6744990b8cSJulian Elischer SYSCTL_INT(_kern_threads, OID_AUTO, cached, CTLFLAG_RD,
6844990b8cSJulian Elischer 	&cached_threads, 0, "Number of threads in thread cache.");
6944990b8cSJulian Elischer 
7044990b8cSJulian Elischer SYSCTL_INT(_kern_threads, OID_AUTO, allocated, CTLFLAG_RD,
7144990b8cSJulian Elischer 	&allocated_threads, 0, "Number of threads in zone.");
7244990b8cSJulian Elischer 
7344990b8cSJulian Elischer static int oiks_debug = 1;	/* 0 disable, 1 printf, 2 enter debugger */
7444990b8cSJulian Elischer SYSCTL_INT(_kern_threads, OID_AUTO, oiks, CTLFLAG_RW,
7544990b8cSJulian Elischer 	&oiks_debug, 0, "OIKS thread debug");
7644990b8cSJulian Elischer 
7744990b8cSJulian Elischer #define RANGEOF(type, start, end) (offsetof(type, end) - offsetof(type, start))
7844990b8cSJulian Elischer 
7944990b8cSJulian Elischer struct threadqueue zombie_threads = TAILQ_HEAD_INITIALIZER(zombie_threads);
8044990b8cSJulian Elischer struct mtx zombie_thread_lock;
8144990b8cSJulian Elischer MTX_SYSINIT(zombie_thread_lock, &zombie_thread_lock,
8244990b8cSJulian Elischer     "zombie_thread_lock", MTX_SPIN);
8344990b8cSJulian Elischer 
8444990b8cSJulian Elischer /*
8544990b8cSJulian Elischer  * Pepare a thread for use.
8644990b8cSJulian Elischer  */
8744990b8cSJulian Elischer static void
8844990b8cSJulian Elischer thread_ctor(void *mem, int size, void *arg)
8944990b8cSJulian Elischer {
9044990b8cSJulian Elischer 	struct thread	*td;
9144990b8cSJulian Elischer 
9244990b8cSJulian Elischer 	KASSERT((size == sizeof(struct thread)),
9344990b8cSJulian Elischer 	    ("size mismatch: %d != %d\n", size, sizeof(struct thread)));
9444990b8cSJulian Elischer 
9544990b8cSJulian Elischer 	td = (struct thread *)mem;
9644990b8cSJulian Elischer 	bzero(&td->td_startzero,
9744990b8cSJulian Elischer 	    (unsigned)RANGEOF(struct thread, td_startzero, td_endzero));
9844990b8cSJulian Elischer 	td->td_state = TDS_NEW;
9944990b8cSJulian Elischer 	td->td_flags |= TDF_UNBOUND;
10044990b8cSJulian Elischer 	cached_threads--;	/* XXXSMP */
10144990b8cSJulian Elischer 	active_threads++;	/* XXXSMP */
10244990b8cSJulian Elischer }
10344990b8cSJulian Elischer 
10444990b8cSJulian Elischer /*
10544990b8cSJulian Elischer  * Reclaim a thread after use.
10644990b8cSJulian Elischer  */
10744990b8cSJulian Elischer static void
10844990b8cSJulian Elischer thread_dtor(void *mem, int size, void *arg)
10944990b8cSJulian Elischer {
11044990b8cSJulian Elischer 	struct thread	*td;
11144990b8cSJulian Elischer 
11244990b8cSJulian Elischer 	KASSERT((size == sizeof(struct thread)),
11344990b8cSJulian Elischer 	    ("size mismatch: %d != %d\n", size, sizeof(struct thread)));
11444990b8cSJulian Elischer 
11544990b8cSJulian Elischer 	td = (struct thread *)mem;
11644990b8cSJulian Elischer 
11744990b8cSJulian Elischer #ifdef INVARIANTS
11844990b8cSJulian Elischer 	/* Verify that this thread is in a safe state to free. */
11944990b8cSJulian Elischer 	switch (td->td_state) {
12044990b8cSJulian Elischer 	case TDS_SLP:
12144990b8cSJulian Elischer 	case TDS_MTX:
12244990b8cSJulian Elischer 	case TDS_RUNQ:
12344990b8cSJulian Elischer 		/*
12444990b8cSJulian Elischer 		 * We must never unlink a thread that is in one of
12544990b8cSJulian Elischer 		 * these states, because it is currently active.
12644990b8cSJulian Elischer 		 */
12744990b8cSJulian Elischer 		panic("bad state for thread unlinking");
12844990b8cSJulian Elischer 		/* NOTREACHED */
12944990b8cSJulian Elischer 	case TDS_UNQUEUED:
13044990b8cSJulian Elischer 	case TDS_NEW:
13144990b8cSJulian Elischer 	case TDS_RUNNING:
13244990b8cSJulian Elischer 	case TDS_SURPLUS:
13344990b8cSJulian Elischer 		break;
13444990b8cSJulian Elischer 	default:
13544990b8cSJulian Elischer 		panic("bad thread state");
13644990b8cSJulian Elischer 		/* NOTREACHED */
13744990b8cSJulian Elischer 	}
13844990b8cSJulian Elischer #endif
13944990b8cSJulian Elischer 
14044990b8cSJulian Elischer 	/* Update counters. */
14144990b8cSJulian Elischer 	active_threads--;	/* XXXSMP */
14244990b8cSJulian Elischer 	cached_threads++;	/* XXXSMP */
14344990b8cSJulian Elischer }
14444990b8cSJulian Elischer 
14544990b8cSJulian Elischer /*
14644990b8cSJulian Elischer  * Initialize type-stable parts of a thread (when newly created).
14744990b8cSJulian Elischer  */
14844990b8cSJulian Elischer static void
14944990b8cSJulian Elischer thread_init(void *mem, int size)
15044990b8cSJulian Elischer {
15144990b8cSJulian Elischer 	struct thread	*td;
15244990b8cSJulian Elischer 
15344990b8cSJulian Elischer 	KASSERT((size == sizeof(struct thread)),
15444990b8cSJulian Elischer 	    ("size mismatch: %d != %d\n", size, sizeof(struct thread)));
15544990b8cSJulian Elischer 
15644990b8cSJulian Elischer 	td = (struct thread *)mem;
15744990b8cSJulian Elischer 	pmap_new_thread(td);
15844990b8cSJulian Elischer 	cpu_thread_setup(td);
15944990b8cSJulian Elischer 	cached_threads++;	/* XXXSMP */
16044990b8cSJulian Elischer 	allocated_threads++;	/* XXXSMP */
16144990b8cSJulian Elischer }
16244990b8cSJulian Elischer 
16344990b8cSJulian Elischer /*
16444990b8cSJulian Elischer  * Tear down type-stable parts of a thread (just before being discarded).
16544990b8cSJulian Elischer  */
16644990b8cSJulian Elischer static void
16744990b8cSJulian Elischer thread_fini(void *mem, int size)
16844990b8cSJulian Elischer {
16944990b8cSJulian Elischer 	struct thread	*td;
17044990b8cSJulian Elischer 
17144990b8cSJulian Elischer 	KASSERT((size == sizeof(struct thread)),
17244990b8cSJulian Elischer 	    ("size mismatch: %d != %d\n", size, sizeof(struct thread)));
17344990b8cSJulian Elischer 
17444990b8cSJulian Elischer 	td = (struct thread *)mem;
17544990b8cSJulian Elischer 	pmap_dispose_thread(td);
17644990b8cSJulian Elischer 	cached_threads--;	/* XXXSMP */
17744990b8cSJulian Elischer 	allocated_threads--;	/* XXXSMP */
17844990b8cSJulian Elischer }
17944990b8cSJulian Elischer 
18044990b8cSJulian Elischer /*
18144990b8cSJulian Elischer  * Initialize global thread allocation resources.
18244990b8cSJulian Elischer  */
18344990b8cSJulian Elischer void
18444990b8cSJulian Elischer threadinit(void)
18544990b8cSJulian Elischer {
18644990b8cSJulian Elischer 
18744990b8cSJulian Elischer 	thread_zone = uma_zcreate("THREAD", sizeof (struct thread),
18844990b8cSJulian Elischer 	    thread_ctor, thread_dtor, thread_init, thread_fini,
18944990b8cSJulian Elischer 	    UMA_ALIGN_CACHE, 0);
19044990b8cSJulian Elischer }
19144990b8cSJulian Elischer 
19244990b8cSJulian Elischer /*
19344990b8cSJulian Elischer  * Stash an embarasingly esxtra thread into the zombie thread queue.
19444990b8cSJulian Elischer  */
19544990b8cSJulian Elischer void
19644990b8cSJulian Elischer thread_stash(struct thread *td)
19744990b8cSJulian Elischer {
19844990b8cSJulian Elischer 	mtx_lock_spin(&zombie_thread_lock);
19944990b8cSJulian Elischer 	TAILQ_INSERT_HEAD(&zombie_threads, td, td_runq);
20044990b8cSJulian Elischer 	mtx_unlock_spin(&zombie_thread_lock);
20144990b8cSJulian Elischer }
20244990b8cSJulian Elischer 
20344990b8cSJulian Elischer /*
20444990b8cSJulian Elischer  * reap any  zombie threads for this Processor.
20544990b8cSJulian Elischer  */
20644990b8cSJulian Elischer void
20744990b8cSJulian Elischer thread_reap(void)
20844990b8cSJulian Elischer {
20944990b8cSJulian Elischer 	struct thread *td_reaped;
21044990b8cSJulian Elischer 
21144990b8cSJulian Elischer 	/*
21244990b8cSJulian Elischer 	 * don't even bother to lock if none at this instant
21344990b8cSJulian Elischer 	 * We really don't care about the next instant..
21444990b8cSJulian Elischer 	 */
21544990b8cSJulian Elischer 	if (!TAILQ_EMPTY(&zombie_threads)) {
21644990b8cSJulian Elischer 		mtx_lock_spin(&zombie_thread_lock);
21744990b8cSJulian Elischer 		while (!TAILQ_EMPTY(&zombie_threads)) {
21844990b8cSJulian Elischer 			td_reaped = TAILQ_FIRST(&zombie_threads);
21944990b8cSJulian Elischer 			TAILQ_REMOVE(&zombie_threads, td_reaped, td_runq);
22044990b8cSJulian Elischer 			mtx_unlock_spin(&zombie_thread_lock);
22144990b8cSJulian Elischer 			thread_free(td_reaped);
22244990b8cSJulian Elischer 			mtx_lock_spin(&zombie_thread_lock);
22344990b8cSJulian Elischer 		}
22444990b8cSJulian Elischer 		mtx_unlock_spin(&zombie_thread_lock);
22544990b8cSJulian Elischer 	}
22644990b8cSJulian Elischer }
22744990b8cSJulian Elischer 
22844990b8cSJulian Elischer /*
22944990b8cSJulian Elischer  * Allocate a thread.
23044990b8cSJulian Elischer  */
23144990b8cSJulian Elischer struct thread *
23244990b8cSJulian Elischer thread_alloc(void)
23344990b8cSJulian Elischer {
23444990b8cSJulian Elischer 	thread_reap(); /* check if any zombies to get */
23544990b8cSJulian Elischer 	return (uma_zalloc(thread_zone, M_WAITOK));
23644990b8cSJulian Elischer }
23744990b8cSJulian Elischer 
23844990b8cSJulian Elischer /*
23944990b8cSJulian Elischer  * Deallocate a thread.
24044990b8cSJulian Elischer  */
24144990b8cSJulian Elischer void
24244990b8cSJulian Elischer thread_free(struct thread *td)
24344990b8cSJulian Elischer {
24444990b8cSJulian Elischer 	uma_zfree(thread_zone, td);
24544990b8cSJulian Elischer }
24644990b8cSJulian Elischer 
24744990b8cSJulian Elischer /*
24844990b8cSJulian Elischer  * Store the thread context in the UTS's mailbox.
24944990b8cSJulian Elischer  */
25044990b8cSJulian Elischer int
25144990b8cSJulian Elischer thread_export_context(struct thread *td)
25244990b8cSJulian Elischer {
25344990b8cSJulian Elischer 	struct kse *ke;
25444990b8cSJulian Elischer 	uintptr_t td2_mbx;
25544990b8cSJulian Elischer 	void *addr1;
25644990b8cSJulian Elischer 	void *addr2;
25744990b8cSJulian Elischer 	int error;
25844990b8cSJulian Elischer 
25944990b8cSJulian Elischer 	/* Export the register contents. */
26044990b8cSJulian Elischer 	error = cpu_export_context(td);
26144990b8cSJulian Elischer 
26244990b8cSJulian Elischer 	ke = td->td_kse;
26344990b8cSJulian Elischer 	addr1 = (caddr_t)ke->ke_mailbox
26444990b8cSJulian Elischer 			+ offsetof(struct kse_mailbox, kmbx_completed_threads);
26544990b8cSJulian Elischer 	addr2 = (caddr_t)td->td_mailbox
26644990b8cSJulian Elischer 			+ offsetof(struct thread_mailbox , next_completed);
26744990b8cSJulian Elischer 	/* Then link it into it's KSE's list of completed threads. */
26844990b8cSJulian Elischer 	if (!error) {
26944990b8cSJulian Elischer 		error = td2_mbx = fuword(addr1);
27044990b8cSJulian Elischer 		if (error == -1)
27144990b8cSJulian Elischer 			error = EFAULT;
27244990b8cSJulian Elischer 		else
27344990b8cSJulian Elischer 			error = 0;
27444990b8cSJulian Elischer 	}
27544990b8cSJulian Elischer 	if (!error)
27644990b8cSJulian Elischer 		error = suword(addr2, td2_mbx);
27744990b8cSJulian Elischer 	if (!error)
27844990b8cSJulian Elischer 		error = suword(addr1, (u_long)td->td_mailbox);
27944990b8cSJulian Elischer 	if (error == -1)
28044990b8cSJulian Elischer 		error = EFAULT;
28144990b8cSJulian Elischer 	return (error);
28244990b8cSJulian Elischer }
28344990b8cSJulian Elischer 
28444990b8cSJulian Elischer 
28544990b8cSJulian Elischer /*
28644990b8cSJulian Elischer  * Discard the current thread and exit from its context.
28744990b8cSJulian Elischer  *
28844990b8cSJulian Elischer  * Because we can't free a thread while we're operating under its context,
28944990b8cSJulian Elischer  * push the current thread into our KSE's ke_tdspare slot, freeing the
29044990b8cSJulian Elischer  * thread that might be there currently. Because we know that only this
29144990b8cSJulian Elischer  * processor will run our KSE, we needn't worry about someone else grabbing
29244990b8cSJulian Elischer  * our context before we do a cpu_throw.
29344990b8cSJulian Elischer  */
29444990b8cSJulian Elischer void
29544990b8cSJulian Elischer thread_exit(void)
29644990b8cSJulian Elischer {
29744990b8cSJulian Elischer 	struct thread *td;
29844990b8cSJulian Elischer 	struct kse *ke;
29944990b8cSJulian Elischer 	struct proc *p;
30044990b8cSJulian Elischer 	struct ksegrp	*kg;
30144990b8cSJulian Elischer 
30244990b8cSJulian Elischer 	td = curthread;
30344990b8cSJulian Elischer 	kg = td->td_ksegrp;
30444990b8cSJulian Elischer 	p = td->td_proc;
30544990b8cSJulian Elischer 	ke = td->td_kse;
30644990b8cSJulian Elischer 
30744990b8cSJulian Elischer 	mtx_assert(&sched_lock, MA_OWNED);
30844990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
30944990b8cSJulian Elischer 	CTR1(KTR_PROC, "thread_exit: thread %p", td);
31044990b8cSJulian Elischer 	KASSERT(!mtx_owned(&Giant), ("dying thread owns giant"));
31144990b8cSJulian Elischer 
31244990b8cSJulian Elischer 	if (ke->ke_tdspare != NULL) {
31344990b8cSJulian Elischer 		thread_stash(ke->ke_tdspare);
31444990b8cSJulian Elischer 		ke->ke_tdspare = NULL;
31544990b8cSJulian Elischer 	}
31644990b8cSJulian Elischer 	cpu_thread_exit(td);	/* XXXSMP */
31744990b8cSJulian Elischer 
31844990b8cSJulian Elischer 	/* Reassign this thread's KSE. */
31944990b8cSJulian Elischer 	if (ke != NULL) {
32044990b8cSJulian Elischer KASSERT((ke->ke_state == KES_RUNNING), ("zapping kse not running"));
32144990b8cSJulian Elischer KASSERT((ke->ke_thread == td ), ("kse ke_thread mismatch against curthread"));
32244990b8cSJulian Elischer KASSERT((ke->ke_thread->td_state == TDS_RUNNING), ("zapping thread not running"));
32344990b8cSJulian Elischer 		ke->ke_thread = NULL;
32444990b8cSJulian Elischer 		td->td_kse = NULL;
32544990b8cSJulian Elischer 		ke->ke_state = KES_UNQUEUED;
32644990b8cSJulian Elischer 		kse_reassign(ke);
32744990b8cSJulian Elischer 	}
32844990b8cSJulian Elischer 
32944990b8cSJulian Elischer 	/* Unlink this thread from its proc. and the kseg */
33044990b8cSJulian Elischer 	if (p != NULL) {
33144990b8cSJulian Elischer 		TAILQ_REMOVE(&p->p_threads, td, td_plist);
33244990b8cSJulian Elischer 		p->p_numthreads--;
33344990b8cSJulian Elischer 		if (kg != NULL) {
33444990b8cSJulian Elischer 			TAILQ_REMOVE(&kg->kg_threads, td, td_kglist);
33544990b8cSJulian Elischer 			kg->kg_numthreads--;
33644990b8cSJulian Elischer 		}
33744990b8cSJulian Elischer 		/*
33844990b8cSJulian Elischer 		 * The test below is NOT true if we are the
33944990b8cSJulian Elischer 		 * sole exiting thread. P_STOPPED_SNGL is unset
34044990b8cSJulian Elischer 		 * in exit1() after it is the only survivor.
34144990b8cSJulian Elischer 		 */
34244990b8cSJulian Elischer 		if (P_SHOULDSTOP(p) == P_STOPPED_SNGL) {
34344990b8cSJulian Elischer 			if (p->p_numthreads == p->p_suspcount) {
34444990b8cSJulian Elischer 				TAILQ_REMOVE(&p->p_suspended,
34544990b8cSJulian Elischer 				    p->p_singlethread, td_runq);
34644990b8cSJulian Elischer 				setrunqueue(p->p_singlethread);
34744990b8cSJulian Elischer 				p->p_suspcount--;
34844990b8cSJulian Elischer 			}
34944990b8cSJulian Elischer 		}
35044990b8cSJulian Elischer 	}
35144990b8cSJulian Elischer 	td->td_state	= TDS_SURPLUS;
35244990b8cSJulian Elischer 	td->td_proc	= NULL;
35344990b8cSJulian Elischer 	td->td_ksegrp	= NULL;
35444990b8cSJulian Elischer 	td->td_last_kse	= NULL;
35544990b8cSJulian Elischer 	ke->ke_tdspare = td;
35644990b8cSJulian Elischer 	PROC_UNLOCK(p);
35744990b8cSJulian Elischer 	cpu_throw();
35844990b8cSJulian Elischer 	/* NOTREACHED */
35944990b8cSJulian Elischer }
36044990b8cSJulian Elischer 
36144990b8cSJulian Elischer /*
36244990b8cSJulian Elischer  * Link a thread to a process.
36344990b8cSJulian Elischer  *
36444990b8cSJulian Elischer  * Note that we do not link to the proc's ucred here.
36544990b8cSJulian Elischer  * The thread is linked as if running but no KSE assigned.
36644990b8cSJulian Elischer  */
36744990b8cSJulian Elischer void
36844990b8cSJulian Elischer thread_link(struct thread *td, struct ksegrp *kg)
36944990b8cSJulian Elischer {
37044990b8cSJulian Elischer 	struct proc *p;
37144990b8cSJulian Elischer 
37244990b8cSJulian Elischer 	p = kg->kg_proc;
37344990b8cSJulian Elischer 	td->td_state = TDS_NEW;
37444990b8cSJulian Elischer 	td->td_proc	= p;
37544990b8cSJulian Elischer 	td->td_ksegrp	= kg;
37644990b8cSJulian Elischer 	td->td_last_kse	= NULL;
37744990b8cSJulian Elischer 
37844990b8cSJulian Elischer 	TAILQ_INSERT_HEAD(&p->p_threads, td, td_plist);
37944990b8cSJulian Elischer 	TAILQ_INSERT_HEAD(&kg->kg_threads, td, td_kglist);
38044990b8cSJulian Elischer 	p->p_numthreads++;
38144990b8cSJulian Elischer 	kg->kg_numthreads++;
38244990b8cSJulian Elischer 	if (oiks_debug && p->p_numthreads > 4) {
38344990b8cSJulian Elischer 		printf("OIKS %d\n", p->p_numthreads);
38444990b8cSJulian Elischer 		if (oiks_debug > 1)
38544990b8cSJulian Elischer 			Debugger("OIKS");
38644990b8cSJulian Elischer 	}
38744990b8cSJulian Elischer 	td->td_critnest = 0;
38844990b8cSJulian Elischer 	td->td_kse	= NULL;
38944990b8cSJulian Elischer }
39044990b8cSJulian Elischer 
39144990b8cSJulian Elischer /*
39244990b8cSJulian Elischer  * Set up the upcall pcb in either a given thread or a new one
39344990b8cSJulian Elischer  * if none given. Use the upcall for the given KSE
39444990b8cSJulian Elischer  * XXXKSE possibly fix cpu_set_upcall() to not need td->td_kse set.
39544990b8cSJulian Elischer  */
39644990b8cSJulian Elischer struct thread *
39744990b8cSJulian Elischer thread_schedule_upcall(struct thread *td, struct kse *ke)
39844990b8cSJulian Elischer {
39944990b8cSJulian Elischer 	struct thread *td2;
40044990b8cSJulian Elischer 
40144990b8cSJulian Elischer 	mtx_assert(&sched_lock, MA_OWNED);
40244990b8cSJulian Elischer 	if (ke->ke_tdspare != NULL) {
40344990b8cSJulian Elischer 		td2 = ke->ke_tdspare;
40444990b8cSJulian Elischer 		ke->ke_tdspare = NULL;
40544990b8cSJulian Elischer 	} else {
40644990b8cSJulian Elischer 		mtx_unlock_spin(&sched_lock);
40744990b8cSJulian Elischer 		td2 = thread_alloc();
40844990b8cSJulian Elischer 		mtx_lock_spin(&sched_lock);
40944990b8cSJulian Elischer 	}
41044990b8cSJulian Elischer 	CTR3(KTR_PROC, "thread_schedule_upcall: thread %p (pid %d, %s)",
41144990b8cSJulian Elischer 	     td, td->td_proc->p_pid, td->td_proc->p_comm);
41244990b8cSJulian Elischer 	thread_link(td2, ke->ke_ksegrp);
41344990b8cSJulian Elischer 	cpu_set_upcall(td2, ke->ke_pcb);
41444990b8cSJulian Elischer 	td2->td_ucred = crhold(td->td_ucred);
41544990b8cSJulian Elischer 	td2->td_flags = TDF_UNBOUND|TDF_UPCALLING;
41644990b8cSJulian Elischer 	td2->td_priority = td->td_priority;
41744990b8cSJulian Elischer 	setrunqueue(td2);
41844990b8cSJulian Elischer 	return (td2);
41944990b8cSJulian Elischer }
42044990b8cSJulian Elischer 
42144990b8cSJulian Elischer /*
42244990b8cSJulian Elischer  * The extra work we go through if we are a threaded process when we
42344990b8cSJulian Elischer  * return to userland
42444990b8cSJulian Elischer  *
42544990b8cSJulian Elischer  * If we are a KSE process and returning to user mode, check for
42644990b8cSJulian Elischer  * extra work to do before we return (e.g. for more syscalls
42744990b8cSJulian Elischer  * to complete first).  If we were in a critical section, we should
42844990b8cSJulian Elischer  * just return to let it finish. Same if we were in the UTS (in
42944990b8cSJulian Elischer  * which case we will have no thread mailbox registered).  The only
43044990b8cSJulian Elischer  * traps we suport will have set the mailbox.  We will clear it here.
43144990b8cSJulian Elischer  */
43244990b8cSJulian Elischer int
43344990b8cSJulian Elischer thread_userret(struct proc *p, struct ksegrp *kg, struct kse *ke,
43444990b8cSJulian Elischer     struct thread *td, struct trapframe *frame)
43544990b8cSJulian Elischer {
43644990b8cSJulian Elischer 	int error = 0;
43744990b8cSJulian Elischer 
43844990b8cSJulian Elischer 	if (ke->ke_tdspare == NULL) {
43944990b8cSJulian Elischer 		mtx_lock(&Giant);
44044990b8cSJulian Elischer 		ke->ke_tdspare = thread_alloc();
44144990b8cSJulian Elischer 		mtx_unlock(&Giant);
44244990b8cSJulian Elischer 	}
44344990b8cSJulian Elischer 	if (td->td_flags & TDF_UNBOUND) {
44444990b8cSJulian Elischer 		/*
44544990b8cSJulian Elischer 		 * Are we returning from a thread that had a mailbox?
44644990b8cSJulian Elischer 		 *
44744990b8cSJulian Elischer 		 * XXX Maybe this should be in a separate function.
44844990b8cSJulian Elischer 		 */
44944990b8cSJulian Elischer 		if (((td->td_flags & TDF_UPCALLING) == 0) && td->td_mailbox) {
45044990b8cSJulian Elischer 			/*
45144990b8cSJulian Elischer 			 * [XXXKSE Future enhancement]
45244990b8cSJulian Elischer 			 * We could also go straight back to the syscall
45344990b8cSJulian Elischer 			 * if we never had to do an upcall since then.
45444990b8cSJulian Elischer 			 * If the KSE's copy is == the thread's copy..
45544990b8cSJulian Elischer 			 * AND there are no other completed threads.
45644990b8cSJulian Elischer 			 */
45744990b8cSJulian Elischer 			/*
45844990b8cSJulian Elischer 			 * We will go back as an upcall or go do another thread.
45944990b8cSJulian Elischer 			 * Either way we need to save the context back to
46044990b8cSJulian Elischer 			 * the user thread mailbox.
46144990b8cSJulian Elischer 			 * So the UTS can restart it later.
46244990b8cSJulian Elischer 			 */
46344990b8cSJulian Elischer 			error = thread_export_context(td);
46444990b8cSJulian Elischer 			td->td_mailbox = NULL;
46544990b8cSJulian Elischer 			if (error) {
46644990b8cSJulian Elischer 				/*
46744990b8cSJulian Elischer 				 * Failing to do the KSE
46844990b8cSJulian Elischer 				 * operation just defaults operation
46944990b8cSJulian Elischer 				 * back to synchonous operation.
47044990b8cSJulian Elischer 				 */
47144990b8cSJulian Elischer 				goto cont;
47244990b8cSJulian Elischer 			}
47344990b8cSJulian Elischer 
47444990b8cSJulian Elischer 			if (TAILQ_FIRST(&kg->kg_runq)) {
47544990b8cSJulian Elischer 				/*
47644990b8cSJulian Elischer 				 * Uh-oh.. don't return to the user.
47744990b8cSJulian Elischer 				 * Instead, switch to the thread that
47844990b8cSJulian Elischer 				 * needs to run. The question is:
47944990b8cSJulian Elischer 				 * What do we do with the thread we have now?
48044990b8cSJulian Elischer 				 * We have put the completion block
48144990b8cSJulian Elischer 				 * on the kse mailbox. If we had more energy,
48244990b8cSJulian Elischer 				 * we could lazily do so, assuming someone
48344990b8cSJulian Elischer 				 * else might get to userland earlier
48444990b8cSJulian Elischer 				 * and deliver it earlier than we could.
48544990b8cSJulian Elischer 				 * To do that we could save it off the KSEG.
48644990b8cSJulian Elischer 				 * An upcalling KSE would 'reap' all completed
48744990b8cSJulian Elischer 				 * threads.
48844990b8cSJulian Elischer 				 * Being in a hurry, we'll do nothing and
48944990b8cSJulian Elischer 				 * leave it on the current KSE for now.
49044990b8cSJulian Elischer 				 *
49144990b8cSJulian Elischer 				 * As for the other threads to run;
49244990b8cSJulian Elischer 				 * we COULD rush through all the threads
49344990b8cSJulian Elischer 				 * in this KSEG at this priority, or we
49444990b8cSJulian Elischer 				 * could throw the ball back into the court
49544990b8cSJulian Elischer 				 * and just run the highest prio kse available.
49644990b8cSJulian Elischer 				 * What is OUR priority?
49744990b8cSJulian Elischer 				 * the priority of the highest sycall waiting
49844990b8cSJulian Elischer 				 * to be returned?
49944990b8cSJulian Elischer 				 * For now, just let another KSE run (easiest).
50044990b8cSJulian Elischer 				 */
50144990b8cSJulian Elischer 				PROC_LOCK(p);
50244990b8cSJulian Elischer 				mtx_lock_spin(&sched_lock);
50344990b8cSJulian Elischer 				thread_exit(); /* Abandon current thread. */
50444990b8cSJulian Elischer 				/* NOTREACHED */
50544990b8cSJulian Elischer 			} else { /* if (number of returning syscalls = 1) */
50644990b8cSJulian Elischer 				/*
50744990b8cSJulian Elischer 				 * Swap our frame for the upcall frame.
50844990b8cSJulian Elischer 				 *
50944990b8cSJulian Elischer 				 * XXXKSE Assumes we are going to user land
51044990b8cSJulian Elischer 				 * and not nested in the kernel
51144990b8cSJulian Elischer 				 */
51244990b8cSJulian Elischer 				td->td_flags |= TDF_UPCALLING;
51344990b8cSJulian Elischer 			}
51444990b8cSJulian Elischer 		}
51544990b8cSJulian Elischer 		/*
51644990b8cSJulian Elischer 		 * This is NOT just an 'else' clause for the above test...
51744990b8cSJulian Elischer 		 */
51844990b8cSJulian Elischer 		if (td->td_flags & TDF_UPCALLING) {
51944990b8cSJulian Elischer 			CTR3(KTR_PROC, "userret: upcall thread %p (pid %d, %s)",
52044990b8cSJulian Elischer 			    td, p->p_pid, p->p_comm);
52144990b8cSJulian Elischer 			/*
52244990b8cSJulian Elischer 			 * Make sure that it has the correct frame loaded.
52344990b8cSJulian Elischer 			 * While we know that we are on the same KSEGRP
52444990b8cSJulian Elischer 			 * as we were created on, we could very easily
52544990b8cSJulian Elischer 			 * have come in on another KSE. We therefore need
52644990b8cSJulian Elischer 			 * to do the copy of the frame after the last
52744990b8cSJulian Elischer 			 * possible switch() (the one above).
52844990b8cSJulian Elischer 			 */
52944990b8cSJulian Elischer 			bcopy(ke->ke_frame, frame, sizeof(struct trapframe));
53044990b8cSJulian Elischer 
53144990b8cSJulian Elischer 			/*
53244990b8cSJulian Elischer 			 * Decide what we are sending to the user
53344990b8cSJulian Elischer 			 * upcall sets one argument. The address of the mbox.
53444990b8cSJulian Elischer 			 */
53544990b8cSJulian Elischer 			cpu_set_args(td, ke);
53644990b8cSJulian Elischer 
53744990b8cSJulian Elischer 			/*
53844990b8cSJulian Elischer 			 * There is no more work to do and we are going to ride
53944990b8cSJulian Elischer 			 * this thead/KSE up to userland. Make sure the user's
54044990b8cSJulian Elischer 			 * pointer to the thread mailbox is cleared before we
54144990b8cSJulian Elischer 			 * re-enter the kernel next time for any reason..
54244990b8cSJulian Elischer 			 * We might as well do it here.
54344990b8cSJulian Elischer 			 */
54444990b8cSJulian Elischer 			td->td_flags &= ~TDF_UPCALLING;	/* Hmmmm. */
54544990b8cSJulian Elischer 			error = suword((caddr_t)td->td_kse->ke_mailbox +
54644990b8cSJulian Elischer 			    offsetof(struct kse_mailbox, kmbx_current_thread),
54744990b8cSJulian Elischer 			    0);
54844990b8cSJulian Elischer 		}
54944990b8cSJulian Elischer 		/*
55044990b8cSJulian Elischer 		 * Stop any chance that we may be separated from
55144990b8cSJulian Elischer 		 * the KSE we are currently on. This is "biting the bullet",
55244990b8cSJulian Elischer 		 * we are committing to go to user space as as THIS KSE here.
55344990b8cSJulian Elischer 		 */
55444990b8cSJulian Elischer cont:
55544990b8cSJulian Elischer 		td->td_flags &= ~TDF_UNBOUND;
55644990b8cSJulian Elischer 	}
55744990b8cSJulian Elischer 	return (error);
55844990b8cSJulian Elischer }
55944990b8cSJulian Elischer 
56044990b8cSJulian Elischer /*
56144990b8cSJulian Elischer  * Enforce single-threading.
56244990b8cSJulian Elischer  *
56344990b8cSJulian Elischer  * Returns 1 if the caller must abort (another thread is waiting to
56444990b8cSJulian Elischer  * exit the process or similar). Process is locked!
56544990b8cSJulian Elischer  * Returns 0 when you are successfully the only thread running.
56644990b8cSJulian Elischer  * A process has successfully single threaded in the suspend mode when
56744990b8cSJulian Elischer  * There are no threads in user mode. Threads in the kernel must be
56844990b8cSJulian Elischer  * allowed to continue until they get to the user boundary. They may even
56944990b8cSJulian Elischer  * copy out their return values and data before suspending. They may however be
57044990b8cSJulian Elischer  * accellerated in reaching the user boundary as we will wake up
57144990b8cSJulian Elischer  * any sleeping threads that are interruptable. (PCATCH).
57244990b8cSJulian Elischer  */
57344990b8cSJulian Elischer int
57444990b8cSJulian Elischer thread_single(int force_exit)
57544990b8cSJulian Elischer {
57644990b8cSJulian Elischer 	struct thread *td;
57744990b8cSJulian Elischer 	struct thread *td2;
57844990b8cSJulian Elischer 	struct proc *p;
57944990b8cSJulian Elischer 
58044990b8cSJulian Elischer 	td = curthread;
58144990b8cSJulian Elischer 	p = td->td_proc;
58244990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
58344990b8cSJulian Elischer 	KASSERT((td != NULL), ("curthread is NULL"));
58444990b8cSJulian Elischer 
58544990b8cSJulian Elischer 	if ((p->p_flag & P_KSES) == 0)
58644990b8cSJulian Elischer 		return (0);
58744990b8cSJulian Elischer 
58844990b8cSJulian Elischer 	if (p->p_singlethread) {
58944990b8cSJulian Elischer 		/*
59044990b8cSJulian Elischer 		 * Someone is already single threading!
59144990b8cSJulian Elischer 		 */
59244990b8cSJulian Elischer 		return (1);
59344990b8cSJulian Elischer 	}
59444990b8cSJulian Elischer 
59544990b8cSJulian Elischer 	if (force_exit == SNGLE_EXIT)
59644990b8cSJulian Elischer 		p->p_flag |= P_SINGLE_EXIT;
59744990b8cSJulian Elischer 	else
59844990b8cSJulian Elischer 		p->p_flag &= ~P_SINGLE_EXIT;
59944990b8cSJulian Elischer 	p->p_flag |= P_STOPPED_SNGL;
60044990b8cSJulian Elischer 	p->p_singlethread = td;
60144990b8cSJulian Elischer 	while ((p->p_numthreads - p->p_suspcount) != 1) {
60244990b8cSJulian Elischer 		FOREACH_THREAD_IN_PROC(p, td2) {
60344990b8cSJulian Elischer 			if (td2 == td)
60444990b8cSJulian Elischer 				continue;
60544990b8cSJulian Elischer 			switch(td2->td_state) {
60644990b8cSJulian Elischer 			case TDS_SUSPENDED:
60744990b8cSJulian Elischer 				if (force_exit == SNGLE_EXIT) {
60844990b8cSJulian Elischer 					TAILQ_REMOVE(&p->p_suspended,
60944990b8cSJulian Elischer 					    td, td_runq);
61044990b8cSJulian Elischer 					setrunqueue(td); /* Should suicide. */
61144990b8cSJulian Elischer 				}
61244990b8cSJulian Elischer 			case TDS_SLP:
61344990b8cSJulian Elischer 				if (td2->td_flags & TDF_CVWAITQ) {
61444990b8cSJulian Elischer 					cv_abort(td2);
61544990b8cSJulian Elischer 				} else {
61644990b8cSJulian Elischer 					abortsleep(td2);
61744990b8cSJulian Elischer 				}
61844990b8cSJulian Elischer 				break;
61944990b8cSJulian Elischer 			/* etc. XXXKSE */
62044990b8cSJulian Elischer 			default:
62144990b8cSJulian Elischer 				;
62244990b8cSJulian Elischer 			}
62344990b8cSJulian Elischer 		}
62444990b8cSJulian Elischer 		/*
62544990b8cSJulian Elischer 		 * XXXKSE-- idea
62644990b8cSJulian Elischer 		 * It's possible that we can just wake up when
62744990b8cSJulian Elischer 		 * there are no runnable KSEs, because that would
62844990b8cSJulian Elischer 		 * indicate that only this thread is runnable and
62944990b8cSJulian Elischer 		 * there are no running KSEs in userland.
63044990b8cSJulian Elischer 		 * --
63144990b8cSJulian Elischer 		 * Wake us up when everyone else has suspended.
63244990b8cSJulian Elischer 		 * (or died)
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)) {
69144990b8cSJulian Elischer 		if (P_SHOULDSTOP(p) == P_STOPPED_SNGL) {
69244990b8cSJulian Elischer 			KASSERT(p->p_singlethread != NULL,
69344990b8cSJulian Elischer 			    ("singlethread not set"));
69444990b8cSJulian Elischer 
69544990b8cSJulian Elischer 			/*
69644990b8cSJulian Elischer 			 * The only suspension in action is
69744990b8cSJulian Elischer 			 * a single-threading. Treat it ever
69844990b8cSJulian Elischer 			 * so slightly different if it is
69944990b8cSJulian Elischer 			 * in a special situation.
70044990b8cSJulian Elischer 			 */
70144990b8cSJulian Elischer 			if (p->p_singlethread == td) {
70244990b8cSJulian Elischer 				return (0);	/* Exempt from stopping. */
70344990b8cSJulian Elischer 			}
70444990b8cSJulian Elischer 
70544990b8cSJulian Elischer 		}
70644990b8cSJulian Elischer 
70744990b8cSJulian Elischer 		if (return_instead) {
70844990b8cSJulian Elischer 			return (1);
70944990b8cSJulian Elischer 		}
71044990b8cSJulian Elischer 
71144990b8cSJulian Elischer 		/*
71244990b8cSJulian Elischer 		 * If the process is waiting for us to exit,
71344990b8cSJulian Elischer 		 * this thread should just suicide.
71444990b8cSJulian Elischer 		 * Assumes that P_SINGLE_EXIT implies P_STOPPED_SNGL.
71544990b8cSJulian Elischer 		 */
71644990b8cSJulian Elischer 		if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) {
71744990b8cSJulian Elischer 			mtx_lock_spin(&sched_lock);
71844990b8cSJulian Elischer 			while (mtx_owned(&Giant))
71944990b8cSJulian Elischer 				mtx_unlock(&Giant);
72044990b8cSJulian Elischer 			thread_exit();
72144990b8cSJulian Elischer 		}
72244990b8cSJulian Elischer 
72344990b8cSJulian Elischer 		/*
72444990b8cSJulian Elischer 		 * When a thread suspends, it just
72544990b8cSJulian Elischer 		 * moves to the processes's suspend queue
72644990b8cSJulian Elischer 		 * and stays there.
72744990b8cSJulian Elischer 		 *
72844990b8cSJulian Elischer 		 * XXXKSE if TDF_BOUND is true
72944990b8cSJulian Elischer 		 * it will not release it's KSE which might
73044990b8cSJulian Elischer 		 * lead to deadlock if there are not enough KSEs
73144990b8cSJulian Elischer 		 * to complete all waiting threads.
73244990b8cSJulian Elischer 		 * Maybe be able to 'lend' it out again.
73344990b8cSJulian Elischer 		 * (lent kse's can not go back to userland?)
73444990b8cSJulian Elischer 		 * and can only be lent in STOPPED state.
73544990b8cSJulian Elischer 		 */
73644990b8cSJulian Elischer 		mtx_assert(&Giant, MA_NOTOWNED);
73744990b8cSJulian Elischer 		mtx_lock_spin(&sched_lock);
73844990b8cSJulian Elischer 		p->p_suspcount++;
73944990b8cSJulian Elischer 		td->td_state = TDS_SUSPENDED;
74044990b8cSJulian Elischer 		TAILQ_INSERT_TAIL(&p->p_suspended, td, td_runq);
74144990b8cSJulian Elischer 		PROC_UNLOCK(p);
74244990b8cSJulian Elischer 		mi_switch();
74344990b8cSJulian Elischer 		mtx_unlock_spin(&sched_lock);
74444990b8cSJulian Elischer 		PROC_LOCK(p);
74544990b8cSJulian Elischer 	}
74644990b8cSJulian Elischer 	return (0);
74744990b8cSJulian Elischer }
74844990b8cSJulian Elischer 
74944990b8cSJulian Elischer /*
75044990b8cSJulian Elischer  * Allow all threads blocked by single threading to continue running.
75144990b8cSJulian Elischer  */
75244990b8cSJulian Elischer void
75344990b8cSJulian Elischer thread_unsuspend(struct proc *p)
75444990b8cSJulian Elischer {
75544990b8cSJulian Elischer 	struct thread *td;
75644990b8cSJulian Elischer 
75744990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
75844990b8cSJulian Elischer 	if (!P_SHOULDSTOP(p)) {
75944990b8cSJulian Elischer 		while (( td = TAILQ_FIRST(&p->p_suspended))) {
76044990b8cSJulian Elischer 			TAILQ_REMOVE(&p->p_suspended, td, td_runq);
76144990b8cSJulian Elischer 			p->p_suspcount--;
76244990b8cSJulian Elischer 			setrunqueue(td);
76344990b8cSJulian Elischer 		}
76444990b8cSJulian Elischer 	} else if ((P_SHOULDSTOP(p) == P_STOPPED_SNGL) &&
76544990b8cSJulian Elischer 	    (p->p_numthreads == p->p_suspcount)) {
76644990b8cSJulian Elischer 		/*
76744990b8cSJulian Elischer 		 * Stopping everything also did the job for the single
76844990b8cSJulian Elischer 		 * threading request. Now we've downgraded to single-threaded,
76944990b8cSJulian Elischer 		 * let it continue.
77044990b8cSJulian Elischer 		 */
77144990b8cSJulian Elischer 		TAILQ_REMOVE(&p->p_suspended, p->p_singlethread, td_runq);
77244990b8cSJulian Elischer 		p->p_suspcount--;
77344990b8cSJulian Elischer 		setrunqueue(p->p_singlethread);
77444990b8cSJulian Elischer 	}
77544990b8cSJulian Elischer }
77644990b8cSJulian Elischer 
77744990b8cSJulian Elischer void
77844990b8cSJulian Elischer thread_single_end(void)
77944990b8cSJulian Elischer {
78044990b8cSJulian Elischer 	struct thread *td;
78144990b8cSJulian Elischer 	struct proc *p;
78244990b8cSJulian Elischer 
78344990b8cSJulian Elischer 	td = curthread;
78444990b8cSJulian Elischer 	p = td->td_proc;
78544990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
78644990b8cSJulian Elischer 	p->p_flag &= ~P_STOPPED_SNGL;
78744990b8cSJulian Elischer 	p->p_singlethread = NULL;
78844990b8cSJulian Elischer 	thread_unsuspend(p);
78944990b8cSJulian Elischer }
79044990b8cSJulian Elischer 
791