xref: /freebsd/sys/kern/kern_thread.c (revision 91d1786f656545195de240cd1906b4875d8831a1)
19454b2d8SWarner Losh /*-
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 
293d06b4b3SAttilio Rao #include "opt_witness.h"
3016d95d4fSJoseph Koshy #include "opt_hwpmc_hooks.h"
313d06b4b3SAttilio Rao 
32677b542eSDavid E. O'Brien #include <sys/cdefs.h>
33677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
34677b542eSDavid E. O'Brien 
3544990b8cSJulian Elischer #include <sys/param.h>
3644990b8cSJulian Elischer #include <sys/systm.h>
3744990b8cSJulian Elischer #include <sys/kernel.h>
3844990b8cSJulian Elischer #include <sys/lock.h>
3944990b8cSJulian Elischer #include <sys/mutex.h>
4044990b8cSJulian Elischer #include <sys/proc.h>
418f0e9130SKonstantin Belousov #include <sys/rangelock.h>
42e170bfdaSDavid Xu #include <sys/resourcevar.h>
43b3e9e682SRyan Stone #include <sys/sdt.h>
4494e0a4cdSJulian Elischer #include <sys/smp.h>
45de028f5aSJeff Roberson #include <sys/sched.h>
4644f3b092SJohn Baldwin #include <sys/sleepqueue.h>
47ace8398dSJeff Roberson #include <sys/selinfo.h>
48*91d1786fSDmitry Chagin #include <sys/sysent.h>
49961a7b24SJohn Baldwin #include <sys/turnstile.h>
5044990b8cSJulian Elischer #include <sys/ktr.h>
51cf7d9a8cSDavid Xu #include <sys/rwlock.h>
52bc8e6d81SDavid Xu #include <sys/umtx.h>
53d7f687fcSJeff Roberson #include <sys/cpuset.h>
5416d95d4fSJoseph Koshy #ifdef	HWPMC_HOOKS
5516d95d4fSJoseph Koshy #include <sys/pmckern.h>
5616d95d4fSJoseph Koshy #endif
5744990b8cSJulian Elischer 
58911b84b0SRobert Watson #include <security/audit/audit.h>
59911b84b0SRobert Watson 
6044990b8cSJulian Elischer #include <vm/vm.h>
6149a2507bSAlan Cox #include <vm/vm_extern.h>
6244990b8cSJulian Elischer #include <vm/uma.h>
63b209f889SRandall Stewart #include <sys/eventhandler.h>
6402fb42b0SPeter Wemm 
65b3e9e682SRyan Stone SDT_PROVIDER_DECLARE(proc);
66d9fae5abSAndriy Gapon SDT_PROBE_DEFINE(proc, , , lwp__exit);
67b3e9e682SRyan Stone 
688460a577SJohn Birrell /*
698460a577SJohn Birrell  * thread related storage.
708460a577SJohn Birrell  */
7144990b8cSJulian Elischer static uma_zone_t thread_zone;
7244990b8cSJulian Elischer 
735215b187SJeff Roberson TAILQ_HEAD(, thread) zombie_threads = TAILQ_HEAD_INITIALIZER(zombie_threads);
74c8790f5dSAttilio Rao static struct mtx zombie_lock;
75a54e85fdSJeff Roberson MTX_SYSINIT(zombie_lock, &zombie_lock, "zombie lock", MTX_SPIN);
7644990b8cSJulian Elischer 
77ff8fbcffSJeff Roberson static void thread_zombie(struct thread *);
7884cdea97SKonstantin Belousov static int thread_unsuspend_one(struct thread *td, struct proc *p,
7984cdea97SKonstantin Belousov     bool boundary);
80ff8fbcffSJeff Roberson 
81ec6ea5e8SDavid Xu #define TID_BUFFER_SIZE	1024
82ec6ea5e8SDavid Xu 
83fdcac928SMarcel Moolenaar struct mtx tid_lock;
841ea7a6f8SPoul-Henning Kamp static struct unrhdr *tid_unrhdr;
85ec6ea5e8SDavid Xu static lwpid_t tid_buffer[TID_BUFFER_SIZE];
86ec6ea5e8SDavid Xu static int tid_head, tid_tail;
87cf7d9a8cSDavid Xu static MALLOC_DEFINE(M_TIDHASH, "tidhash", "thread hash");
88cf7d9a8cSDavid Xu 
89cf7d9a8cSDavid Xu struct	tidhashhead *tidhashtbl;
90cf7d9a8cSDavid Xu u_long	tidhash;
91cf7d9a8cSDavid Xu struct	rwlock tidhash_lock;
92cf7d9a8cSDavid Xu 
93ec6ea5e8SDavid Xu static lwpid_t
94ec6ea5e8SDavid Xu tid_alloc(void)
95ec6ea5e8SDavid Xu {
96ec6ea5e8SDavid Xu 	lwpid_t	tid;
97ec6ea5e8SDavid Xu 
98ec6ea5e8SDavid Xu 	tid = alloc_unr(tid_unrhdr);
99ec6ea5e8SDavid Xu 	if (tid != -1)
100ec6ea5e8SDavid Xu 		return (tid);
101ec6ea5e8SDavid Xu 	mtx_lock(&tid_lock);
102ec6ea5e8SDavid Xu 	if (tid_head == tid_tail) {
103ec6ea5e8SDavid Xu 		mtx_unlock(&tid_lock);
104ec6ea5e8SDavid Xu 		return (-1);
105ec6ea5e8SDavid Xu 	}
10694cb3545SKonstantin Belousov 	tid = tid_buffer[tid_head];
10794cb3545SKonstantin Belousov 	tid_head = (tid_head + 1) % TID_BUFFER_SIZE;
108ec6ea5e8SDavid Xu 	mtx_unlock(&tid_lock);
109ec6ea5e8SDavid Xu 	return (tid);
110ec6ea5e8SDavid Xu }
111ec6ea5e8SDavid Xu 
112ec6ea5e8SDavid Xu static void
113ec6ea5e8SDavid Xu tid_free(lwpid_t tid)
114ec6ea5e8SDavid Xu {
115ec6ea5e8SDavid Xu 	lwpid_t tmp_tid = -1;
116ec6ea5e8SDavid Xu 
117ec6ea5e8SDavid Xu 	mtx_lock(&tid_lock);
118ec6ea5e8SDavid Xu 	if ((tid_tail + 1) % TID_BUFFER_SIZE == tid_head) {
11994cb3545SKonstantin Belousov 		tmp_tid = tid_buffer[tid_head];
12094cb3545SKonstantin Belousov 		tid_head = (tid_head + 1) % TID_BUFFER_SIZE;
121ec6ea5e8SDavid Xu 	}
12294cb3545SKonstantin Belousov 	tid_buffer[tid_tail] = tid;
12394cb3545SKonstantin Belousov 	tid_tail = (tid_tail + 1) % TID_BUFFER_SIZE;
124ec6ea5e8SDavid Xu 	mtx_unlock(&tid_lock);
125ec6ea5e8SDavid Xu 	if (tmp_tid != -1)
126ec6ea5e8SDavid Xu 		free_unr(tid_unrhdr, tmp_tid);
127ec6ea5e8SDavid Xu }
128ec6ea5e8SDavid Xu 
129fdcac928SMarcel Moolenaar /*
130696058c3SJulian Elischer  * Prepare a thread for use.
13144990b8cSJulian Elischer  */
132b23f72e9SBrian Feldman static int
133b23f72e9SBrian Feldman thread_ctor(void *mem, int size, void *arg, int flags)
13444990b8cSJulian Elischer {
13544990b8cSJulian Elischer 	struct thread	*td;
13644990b8cSJulian Elischer 
13744990b8cSJulian Elischer 	td = (struct thread *)mem;
13871fad9fdSJulian Elischer 	td->td_state = TDS_INACTIVE;
139060563ecSJulian Elischer 	td->td_oncpu = NOCPU;
1406c27c603SJuli Mallett 
141ec6ea5e8SDavid Xu 	td->td_tid = tid_alloc();
142773eff9dSPoul-Henning Kamp 
1436c27c603SJuli Mallett 	/*
1446c27c603SJuli Mallett 	 * Note that td_critnest begins life as 1 because the thread is not
1456c27c603SJuli Mallett 	 * running and is thereby implicitly waiting to be on the receiving
146a54e85fdSJeff Roberson 	 * end of a context switch.
1476c27c603SJuli Mallett 	 */
148139b7550SJohn Baldwin 	td->td_critnest = 1;
149acbe332aSDavid Xu 	td->td_lend_user_pri = PRI_MAX;
150b209f889SRandall Stewart 	EVENTHANDLER_INVOKE(thread_ctor, td);
151911b84b0SRobert Watson #ifdef AUDIT
152911b84b0SRobert Watson 	audit_thread_alloc(td);
153911b84b0SRobert Watson #endif
154d10183d9SDavid Xu 	umtx_thread_alloc(td);
155b23f72e9SBrian Feldman 	return (0);
15644990b8cSJulian Elischer }
15744990b8cSJulian Elischer 
15844990b8cSJulian Elischer /*
15944990b8cSJulian Elischer  * Reclaim a thread after use.
16044990b8cSJulian Elischer  */
16144990b8cSJulian Elischer static void
16244990b8cSJulian Elischer thread_dtor(void *mem, int size, void *arg)
16344990b8cSJulian Elischer {
16444990b8cSJulian Elischer 	struct thread *td;
16544990b8cSJulian Elischer 
16644990b8cSJulian Elischer 	td = (struct thread *)mem;
16744990b8cSJulian Elischer 
16844990b8cSJulian Elischer #ifdef INVARIANTS
16944990b8cSJulian Elischer 	/* Verify that this thread is in a safe state to free. */
17044990b8cSJulian Elischer 	switch (td->td_state) {
17171fad9fdSJulian Elischer 	case TDS_INHIBITED:
17271fad9fdSJulian Elischer 	case TDS_RUNNING:
17371fad9fdSJulian Elischer 	case TDS_CAN_RUN:
17444990b8cSJulian Elischer 	case TDS_RUNQ:
17544990b8cSJulian Elischer 		/*
17644990b8cSJulian Elischer 		 * We must never unlink a thread that is in one of
17744990b8cSJulian Elischer 		 * these states, because it is currently active.
17844990b8cSJulian Elischer 		 */
17944990b8cSJulian Elischer 		panic("bad state for thread unlinking");
18044990b8cSJulian Elischer 		/* NOTREACHED */
18171fad9fdSJulian Elischer 	case TDS_INACTIVE:
18244990b8cSJulian Elischer 		break;
18344990b8cSJulian Elischer 	default:
18444990b8cSJulian Elischer 		panic("bad thread state");
18544990b8cSJulian Elischer 		/* NOTREACHED */
18644990b8cSJulian Elischer 	}
18744990b8cSJulian Elischer #endif
1886e8525ceSRobert Watson #ifdef AUDIT
1896e8525ceSRobert Watson 	audit_thread_free(td);
1906e8525ceSRobert Watson #endif
1911ba4a712SPawel Jakub Dawidek 	/* Free all OSD associated to this thread. */
1921ba4a712SPawel Jakub Dawidek 	osd_thread_exit(td);
1931ba4a712SPawel Jakub Dawidek 
194b209f889SRandall Stewart 	EVENTHANDLER_INVOKE(thread_dtor, td);
195ec6ea5e8SDavid Xu 	tid_free(td->td_tid);
19644990b8cSJulian Elischer }
19744990b8cSJulian Elischer 
19844990b8cSJulian Elischer /*
19944990b8cSJulian Elischer  * Initialize type-stable parts of a thread (when newly created).
20044990b8cSJulian Elischer  */
201b23f72e9SBrian Feldman static int
202b23f72e9SBrian Feldman thread_init(void *mem, int size, int flags)
20344990b8cSJulian Elischer {
20444990b8cSJulian Elischer 	struct thread *td;
20544990b8cSJulian Elischer 
20644990b8cSJulian Elischer 	td = (struct thread *)mem;
207247aba24SMarcel Moolenaar 
20844f3b092SJohn Baldwin 	td->td_sleepqueue = sleepq_alloc();
209961a7b24SJohn Baldwin 	td->td_turnstile = turnstile_alloc();
2108f0e9130SKonstantin Belousov 	td->td_rlqe = NULL;
211b209f889SRandall Stewart 	EVENTHANDLER_INVOKE(thread_init, td);
212de028f5aSJeff Roberson 	td->td_sched = (struct td_sched *)&td[1];
213d10183d9SDavid Xu 	umtx_thread_init(td);
21489b57fcfSKonstantin Belousov 	td->td_kstack = 0;
215ad8b1d85SKonstantin Belousov 	td->td_sel = NULL;
216b23f72e9SBrian Feldman 	return (0);
21744990b8cSJulian Elischer }
21844990b8cSJulian Elischer 
21944990b8cSJulian Elischer /*
22044990b8cSJulian Elischer  * Tear down type-stable parts of a thread (just before being discarded).
22144990b8cSJulian Elischer  */
22244990b8cSJulian Elischer static void
22344990b8cSJulian Elischer thread_fini(void *mem, int size)
22444990b8cSJulian Elischer {
22544990b8cSJulian Elischer 	struct thread *td;
22644990b8cSJulian Elischer 
22744990b8cSJulian Elischer 	td = (struct thread *)mem;
228b209f889SRandall Stewart 	EVENTHANDLER_INVOKE(thread_fini, td);
2298f0e9130SKonstantin Belousov 	rlqentry_free(td->td_rlqe);
230961a7b24SJohn Baldwin 	turnstile_free(td->td_turnstile);
23144f3b092SJohn Baldwin 	sleepq_free(td->td_sleepqueue);
232d10183d9SDavid Xu 	umtx_thread_fini(td);
233ace8398dSJeff Roberson 	seltdfini(td);
23444990b8cSJulian Elischer }
2355215b187SJeff Roberson 
2365c8329edSJulian Elischer /*
2375215b187SJeff Roberson  * For a newly created process,
2385215b187SJeff Roberson  * link up all the structures and its initial threads etc.
239ed062c8dSJulian Elischer  * called from:
240e7d939bdSMarcel Moolenaar  * {arch}/{arch}/machdep.c   {arch}_init(), init386() etc.
241ed062c8dSJulian Elischer  * proc_dtor() (should go away)
242ed062c8dSJulian Elischer  * proc_init()
2435c8329edSJulian Elischer  */
2445c8329edSJulian Elischer void
24589b57fcfSKonstantin Belousov proc_linkup0(struct proc *p, struct thread *td)
24689b57fcfSKonstantin Belousov {
24789b57fcfSKonstantin Belousov 	TAILQ_INIT(&p->p_threads);	     /* all threads in proc */
24889b57fcfSKonstantin Belousov 	proc_linkup(p, td);
24989b57fcfSKonstantin Belousov }
25089b57fcfSKonstantin Belousov 
25189b57fcfSKonstantin Belousov void
2528460a577SJohn Birrell proc_linkup(struct proc *p, struct thread *td)
2535c8329edSJulian Elischer {
254a54e85fdSJeff Roberson 
2559104847fSDavid Xu 	sigqueue_init(&p->p_sigqueue, p);
256ebceaf6dSDavid Xu 	p->p_ksi = ksiginfo_alloc(1);
257ebceaf6dSDavid Xu 	if (p->p_ksi != NULL) {
2585c474517SDavid Xu 		/* XXX p_ksi may be null if ksiginfo zone is not ready */
259ebceaf6dSDavid Xu 		p->p_ksi->ksi_flags = KSI_EXT | KSI_INS;
260ebceaf6dSDavid Xu 	}
261b2f92ef9SDavid Xu 	LIST_INIT(&p->p_mqnotifier);
2625c8329edSJulian Elischer 	p->p_numthreads = 0;
2638460a577SJohn Birrell 	thread_link(td, p);
2645c8329edSJulian Elischer }
2655c8329edSJulian Elischer 
2665c8329edSJulian Elischer /*
26744990b8cSJulian Elischer  * Initialize global thread allocation resources.
26844990b8cSJulian Elischer  */
26944990b8cSJulian Elischer void
27044990b8cSJulian Elischer threadinit(void)
27144990b8cSJulian Elischer {
27244990b8cSJulian Elischer 
2731ea7a6f8SPoul-Henning Kamp 	mtx_init(&tid_lock, "TID lock", NULL, MTX_DEF);
27402c6fc21SKonstantin Belousov 
27502c6fc21SKonstantin Belousov 	/*
276abce621cSKonstantin Belousov 	 * pid_max cannot be greater than PID_MAX.
27702c6fc21SKonstantin Belousov 	 * leave one number for thread0.
27802c6fc21SKonstantin Belousov 	 */
2796829a5c5SJulian Elischer 	tid_unrhdr = new_unrhdr(PID_MAX + 2, INT_MAX, &tid_lock);
2801ea7a6f8SPoul-Henning Kamp 
281de028f5aSJeff Roberson 	thread_zone = uma_zcreate("THREAD", sched_sizeof_thread(),
28244990b8cSJulian Elischer 	    thread_ctor, thread_dtor, thread_init, thread_fini,
2834649e92bSJohn Baldwin 	    16 - 1, 0);
284cf7d9a8cSDavid Xu 	tidhashtbl = hashinit(maxproc / 2, M_TIDHASH, &tidhash);
285cf7d9a8cSDavid Xu 	rw_init(&tidhash_lock, "tidhash");
28644990b8cSJulian Elischer }
28744990b8cSJulian Elischer 
28844990b8cSJulian Elischer /*
289ff8fbcffSJeff Roberson  * Place an unused thread on the zombie list.
290ad1e7d28SJulian Elischer  * Use the slpq as that must be unused by now.
29144990b8cSJulian Elischer  */
29244990b8cSJulian Elischer void
293ff8fbcffSJeff Roberson thread_zombie(struct thread *td)
29444990b8cSJulian Elischer {
295a54e85fdSJeff Roberson 	mtx_lock_spin(&zombie_lock);
296ad1e7d28SJulian Elischer 	TAILQ_INSERT_HEAD(&zombie_threads, td, td_slpq);
297a54e85fdSJeff Roberson 	mtx_unlock_spin(&zombie_lock);
29844990b8cSJulian Elischer }
29944990b8cSJulian Elischer 
3005c8329edSJulian Elischer /*
301ff8fbcffSJeff Roberson  * Release a thread that has exited after cpu_throw().
302ff8fbcffSJeff Roberson  */
303ff8fbcffSJeff Roberson void
304ff8fbcffSJeff Roberson thread_stash(struct thread *td)
305ff8fbcffSJeff Roberson {
306ff8fbcffSJeff Roberson 	atomic_subtract_rel_int(&td->td_proc->p_exitthreads, 1);
307ff8fbcffSJeff Roberson 	thread_zombie(td);
308ff8fbcffSJeff Roberson }
309ff8fbcffSJeff Roberson 
310ff8fbcffSJeff Roberson /*
3116617724cSJeff Roberson  * Reap zombie resources.
31244990b8cSJulian Elischer  */
31344990b8cSJulian Elischer void
31444990b8cSJulian Elischer thread_reap(void)
31544990b8cSJulian Elischer {
3165c8329edSJulian Elischer 	struct thread *td_first, *td_next;
31744990b8cSJulian Elischer 
31844990b8cSJulian Elischer 	/*
3195215b187SJeff Roberson 	 * Don't even bother to lock if none at this instant,
3205215b187SJeff Roberson 	 * we really don't care about the next instant..
32144990b8cSJulian Elischer 	 */
3228460a577SJohn Birrell 	if (!TAILQ_EMPTY(&zombie_threads)) {
323a54e85fdSJeff Roberson 		mtx_lock_spin(&zombie_lock);
3245c8329edSJulian Elischer 		td_first = TAILQ_FIRST(&zombie_threads);
3255c8329edSJulian Elischer 		if (td_first)
3265c8329edSJulian Elischer 			TAILQ_INIT(&zombie_threads);
327a54e85fdSJeff Roberson 		mtx_unlock_spin(&zombie_lock);
3285c8329edSJulian Elischer 		while (td_first) {
329ad1e7d28SJulian Elischer 			td_next = TAILQ_NEXT(td_first, td_slpq);
3305215b187SJeff Roberson 			if (td_first->td_ucred)
3315215b187SJeff Roberson 				crfree(td_first->td_ucred);
3325c8329edSJulian Elischer 			thread_free(td_first);
3335c8329edSJulian Elischer 			td_first = td_next;
33444990b8cSJulian Elischer 		}
33544990b8cSJulian Elischer 	}
336ed062c8dSJulian Elischer }
33744990b8cSJulian Elischer 
3384f0db5e0SJulian Elischer /*
33944990b8cSJulian Elischer  * Allocate a thread.
34044990b8cSJulian Elischer  */
34144990b8cSJulian Elischer struct thread *
3428a945d10SKonstantin Belousov thread_alloc(int pages)
34344990b8cSJulian Elischer {
34489b57fcfSKonstantin Belousov 	struct thread *td;
3458460a577SJohn Birrell 
34644990b8cSJulian Elischer 	thread_reap(); /* check if any zombies to get */
34789b57fcfSKonstantin Belousov 
34889b57fcfSKonstantin Belousov 	td = (struct thread *)uma_zalloc(thread_zone, M_WAITOK);
34989b57fcfSKonstantin Belousov 	KASSERT(td->td_kstack == 0, ("thread_alloc got thread with kstack"));
3508a945d10SKonstantin Belousov 	if (!vm_thread_new(td, pages)) {
35189b57fcfSKonstantin Belousov 		uma_zfree(thread_zone, td);
35289b57fcfSKonstantin Belousov 		return (NULL);
35389b57fcfSKonstantin Belousov 	}
3540c3967e7SMarcel Moolenaar 	cpu_thread_alloc(td);
35589b57fcfSKonstantin Belousov 	return (td);
35644990b8cSJulian Elischer }
35744990b8cSJulian Elischer 
3588a945d10SKonstantin Belousov int
3598a945d10SKonstantin Belousov thread_alloc_stack(struct thread *td, int pages)
3608a945d10SKonstantin Belousov {
3618a945d10SKonstantin Belousov 
3628a945d10SKonstantin Belousov 	KASSERT(td->td_kstack == 0,
3638a945d10SKonstantin Belousov 	    ("thread_alloc_stack called on a thread with kstack"));
3648a945d10SKonstantin Belousov 	if (!vm_thread_new(td, pages))
3658a945d10SKonstantin Belousov 		return (0);
3668a945d10SKonstantin Belousov 	cpu_thread_alloc(td);
3678a945d10SKonstantin Belousov 	return (1);
3688a945d10SKonstantin Belousov }
3694f0db5e0SJulian Elischer 
3704f0db5e0SJulian Elischer /*
37144990b8cSJulian Elischer  * Deallocate a thread.
37244990b8cSJulian Elischer  */
37344990b8cSJulian Elischer void
37444990b8cSJulian Elischer thread_free(struct thread *td)
37544990b8cSJulian Elischer {
3762e6b8de4SJeff Roberson 
3772e6b8de4SJeff Roberson 	lock_profile_thread_exit(td);
37845aea8deSJeff Roberson 	if (td->td_cpuset)
379d7f687fcSJeff Roberson 		cpuset_rel(td->td_cpuset);
380d7f687fcSJeff Roberson 	td->td_cpuset = NULL;
3810c3967e7SMarcel Moolenaar 	cpu_thread_free(td);
38289b57fcfSKonstantin Belousov 	if (td->td_kstack != 0)
38389b57fcfSKonstantin Belousov 		vm_thread_dispose(td);
38444990b8cSJulian Elischer 	uma_zfree(thread_zone, td);
38544990b8cSJulian Elischer }
38644990b8cSJulian Elischer 
38744990b8cSJulian Elischer /*
38844990b8cSJulian Elischer  * Discard the current thread and exit from its context.
38994e0a4cdSJulian Elischer  * Always called with scheduler locked.
39044990b8cSJulian Elischer  *
39144990b8cSJulian Elischer  * Because we can't free a thread while we're operating under its context,
392696058c3SJulian Elischer  * push the current thread into our CPU's deadthread holder. This means
393696058c3SJulian Elischer  * we needn't worry about someone else grabbing our context before we
3946617724cSJeff Roberson  * do a cpu_throw().
39544990b8cSJulian Elischer  */
39644990b8cSJulian Elischer void
39744990b8cSJulian Elischer thread_exit(void)
39844990b8cSJulian Elischer {
3997e3a96eaSJohn Baldwin 	uint64_t runtime, new_switchtime;
40044990b8cSJulian Elischer 	struct thread *td;
4011c4bcd05SJeff Roberson 	struct thread *td2;
40244990b8cSJulian Elischer 	struct proc *p;
4037847a9daSJohn Baldwin 	int wakeup_swapper;
40444990b8cSJulian Elischer 
40544990b8cSJulian Elischer 	td = curthread;
40644990b8cSJulian Elischer 	p = td->td_proc;
40744990b8cSJulian Elischer 
408a54e85fdSJeff Roberson 	PROC_SLOCK_ASSERT(p, MA_OWNED);
409ed062c8dSJulian Elischer 	mtx_assert(&Giant, MA_NOTOWNED);
410a54e85fdSJeff Roberson 
41144990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
412ed062c8dSJulian Elischer 	KASSERT(p != NULL, ("thread exiting without a process"));
413cc701b73SRobert Watson 	CTR3(KTR_PROC, "thread_exit: thread %p (pid %ld, %s)", td,
414e01eafefSJulian Elischer 	    (long)p->p_pid, td->td_name);
4159104847fSDavid Xu 	KASSERT(TAILQ_EMPTY(&td->td_sigqueue.sq_list), ("signal pending"));
41644990b8cSJulian Elischer 
41789964dd2SRobert Watson #ifdef AUDIT
41889964dd2SRobert Watson 	AUDIT_SYSCALL_EXIT(0, td);
41989964dd2SRobert Watson #endif
420ed062c8dSJulian Elischer 	/*
421ed062c8dSJulian Elischer 	 * drop FPU & debug register state storage, or any other
422ed062c8dSJulian Elischer 	 * architecture specific resources that
423ed062c8dSJulian Elischer 	 * would not be on a new untouched process.
424ed062c8dSJulian Elischer 	 */
42544990b8cSJulian Elischer 	cpu_thread_exit(td);	/* XXXSMP */
42644990b8cSJulian Elischer 
427ed062c8dSJulian Elischer 	/*
4281faf202eSJulian Elischer 	 * The last thread is left attached to the process
4291faf202eSJulian Elischer 	 * So that the whole bundle gets recycled. Skip
430ed062c8dSJulian Elischer 	 * all this stuff if we never had threads.
431ed062c8dSJulian Elischer 	 * EXIT clears all sign of other threads when
432ed062c8dSJulian Elischer 	 * it goes to single threading, so the last thread always
433ed062c8dSJulian Elischer 	 * takes the short path.
4341faf202eSJulian Elischer 	 */
435ed062c8dSJulian Elischer 	if (p->p_flag & P_HADTHREADS) {
4361faf202eSJulian Elischer 		if (p->p_numthreads > 1) {
437fd229b5bSKonstantin Belousov 			atomic_add_int(&td->td_proc->p_exitthreads, 1);
438d3a0bd78SJulian Elischer 			thread_unlink(td);
4391c4bcd05SJeff Roberson 			td2 = FIRST_THREAD_IN_PROC(p);
4401c4bcd05SJeff Roberson 			sched_exit_thread(td2, td);
441ed062c8dSJulian Elischer 
442ed062c8dSJulian Elischer 			/*
44344990b8cSJulian Elischer 			 * The test below is NOT true if we are the
4449182554aSKonstantin Belousov 			 * sole exiting thread. P_STOPPED_SINGLE is unset
44544990b8cSJulian Elischer 			 * in exit1() after it is the only survivor.
44644990b8cSJulian Elischer 			 */
4471279572aSDavid Xu 			if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
44844990b8cSJulian Elischer 				if (p->p_numthreads == p->p_suspcount) {
449a54e85fdSJeff Roberson 					thread_lock(p->p_singlethread);
4507847a9daSJohn Baldwin 					wakeup_swapper = thread_unsuspend_one(
45184cdea97SKonstantin Belousov 						p->p_singlethread, p, false);
452a54e85fdSJeff Roberson 					thread_unlock(p->p_singlethread);
4537847a9daSJohn Baldwin 					if (wakeup_swapper)
4547847a9daSJohn Baldwin 						kick_proc0();
45544990b8cSJulian Elischer 				}
45644990b8cSJulian Elischer 			}
45748bfcdddSJulian Elischer 
458696058c3SJulian Elischer 			PCPU_SET(deadthread, td);
4591faf202eSJulian Elischer 		} else {
460ed062c8dSJulian Elischer 			/*
461ed062c8dSJulian Elischer 			 * The last thread is exiting.. but not through exit()
462ed062c8dSJulian Elischer 			 */
463ed062c8dSJulian Elischer 			panic ("thread_exit: Last thread exiting on its own");
464ed062c8dSJulian Elischer 		}
4651faf202eSJulian Elischer 	}
46616d95d4fSJoseph Koshy #ifdef	HWPMC_HOOKS
46716d95d4fSJoseph Koshy 	/*
46816d95d4fSJoseph Koshy 	 * If this thread is part of a process that is being tracked by hwpmc(4),
46916d95d4fSJoseph Koshy 	 * inform the module of the thread's impending exit.
47016d95d4fSJoseph Koshy 	 */
47116d95d4fSJoseph Koshy 	if (PMC_PROC_IS_USING_PMCS(td->td_proc))
47216d95d4fSJoseph Koshy 		PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT);
47316d95d4fSJoseph Koshy #endif
474a54e85fdSJeff Roberson 	PROC_UNLOCK(p);
4755c7bebf9SKonstantin Belousov 	PROC_STATLOCK(p);
4765c7bebf9SKonstantin Belousov 	thread_lock(td);
4775c7bebf9SKonstantin Belousov 	PROC_SUNLOCK(p);
4787e3a96eaSJohn Baldwin 
4797e3a96eaSJohn Baldwin 	/* Do the same timestamp bookkeeping that mi_switch() would do. */
4807e3a96eaSJohn Baldwin 	new_switchtime = cpu_ticks();
4817e3a96eaSJohn Baldwin 	runtime = new_switchtime - PCPU_GET(switchtime);
4827e3a96eaSJohn Baldwin 	td->td_runtime += runtime;
4837e3a96eaSJohn Baldwin 	td->td_incruntime += runtime;
4847e3a96eaSJohn Baldwin 	PCPU_SET(switchtime, new_switchtime);
4857e3a96eaSJohn Baldwin 	PCPU_SET(switchticks, ticks);
4867e3a96eaSJohn Baldwin 	PCPU_INC(cnt.v_swtch);
4877e3a96eaSJohn Baldwin 
4887e3a96eaSJohn Baldwin 	/* Save our resource usage in our process. */
4897e3a96eaSJohn Baldwin 	td->td_ru.ru_nvcsw++;
49041fd9c63SKonstantin Belousov 	ruxagg(p, td);
4917e3a96eaSJohn Baldwin 	rucollect(&p->p_ru, &td->td_ru);
4925c7bebf9SKonstantin Belousov 	PROC_STATUNLOCK(p);
4937e3a96eaSJohn Baldwin 
494dcc9954eSJulian Elischer 	td->td_state = TDS_INACTIVE;
4953d06b4b3SAttilio Rao #ifdef WITNESS
4963d06b4b3SAttilio Rao 	witness_thread_exit(td);
4973d06b4b3SAttilio Rao #endif
498732d9528SJulian Elischer 	CTR1(KTR_PROC, "thread_exit: cpu_throw() thread %p", td);
499a54e85fdSJeff Roberson 	sched_throw(td);
500cc66ebe2SPeter Wemm 	panic("I'm a teapot!");
50144990b8cSJulian Elischer 	/* NOTREACHED */
50244990b8cSJulian Elischer }
50344990b8cSJulian Elischer 
50444990b8cSJulian Elischer /*
505696058c3SJulian Elischer  * Do any thread specific cleanups that may be needed in wait()
50637814395SPeter Wemm  * called with Giant, proc and schedlock not held.
507696058c3SJulian Elischer  */
508696058c3SJulian Elischer void
509696058c3SJulian Elischer thread_wait(struct proc *p)
510696058c3SJulian Elischer {
511696058c3SJulian Elischer 	struct thread *td;
512696058c3SJulian Elischer 
51337814395SPeter Wemm 	mtx_assert(&Giant, MA_NOTOWNED);
514624bf9e1SKonstantin Belousov 	KASSERT(p->p_numthreads == 1, ("multiple threads in thread_wait()"));
515624bf9e1SKonstantin Belousov 	KASSERT(p->p_exitthreads == 0, ("p_exitthreads leaking"));
516ff8fbcffSJeff Roberson 	td = FIRST_THREAD_IN_PROC(p);
517ff8fbcffSJeff Roberson 	/* Lock the last thread so we spin until it exits cpu_throw(). */
518ff8fbcffSJeff Roberson 	thread_lock(td);
519ff8fbcffSJeff Roberson 	thread_unlock(td);
5202e6b8de4SJeff Roberson 	lock_profile_thread_exit(td);
521d7f687fcSJeff Roberson 	cpuset_rel(td->td_cpuset);
522d7f687fcSJeff Roberson 	td->td_cpuset = NULL;
523696058c3SJulian Elischer 	cpu_thread_clean(td);
524ed062c8dSJulian Elischer 	crfree(td->td_ucred);
525696058c3SJulian Elischer 	thread_reap();	/* check for zombie threads etc. */
526696058c3SJulian Elischer }
527696058c3SJulian Elischer 
528696058c3SJulian Elischer /*
52944990b8cSJulian Elischer  * Link a thread to a process.
5301faf202eSJulian Elischer  * set up anything that needs to be initialized for it to
5311faf202eSJulian Elischer  * be used by the process.
53244990b8cSJulian Elischer  */
53344990b8cSJulian Elischer void
5348460a577SJohn Birrell thread_link(struct thread *td, struct proc *p)
53544990b8cSJulian Elischer {
53644990b8cSJulian Elischer 
537a54e85fdSJeff Roberson 	/*
538a54e85fdSJeff Roberson 	 * XXX This can't be enabled because it's called for proc0 before
539374ae2a3SJeff Roberson 	 * its lock has been created.
540374ae2a3SJeff Roberson 	 * PROC_LOCK_ASSERT(p, MA_OWNED);
541a54e85fdSJeff Roberson 	 */
54271fad9fdSJulian Elischer 	td->td_state    = TDS_INACTIVE;
54344990b8cSJulian Elischer 	td->td_proc     = p;
544b61ce5b0SJeff Roberson 	td->td_flags    = TDF_INMEM;
54544990b8cSJulian Elischer 
5461faf202eSJulian Elischer 	LIST_INIT(&td->td_contested);
547eea4f254SJeff Roberson 	LIST_INIT(&td->td_lprof[0]);
548eea4f254SJeff Roberson 	LIST_INIT(&td->td_lprof[1]);
5499104847fSDavid Xu 	sigqueue_init(&td->td_sigqueue, p);
550fd90e2edSJung-uk Kim 	callout_init(&td->td_slpcallout, 1);
55166d8df9dSDaniel Eischen 	TAILQ_INSERT_TAIL(&p->p_threads, td, td_plist);
55244990b8cSJulian Elischer 	p->p_numthreads++;
55344990b8cSJulian Elischer }
55444990b8cSJulian Elischer 
555ed062c8dSJulian Elischer /*
556ed062c8dSJulian Elischer  * Called from:
557ed062c8dSJulian Elischer  *  thread_exit()
558ed062c8dSJulian Elischer  */
559d3a0bd78SJulian Elischer void
560d3a0bd78SJulian Elischer thread_unlink(struct thread *td)
561d3a0bd78SJulian Elischer {
562d3a0bd78SJulian Elischer 	struct proc *p = td->td_proc;
563d3a0bd78SJulian Elischer 
564374ae2a3SJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
565d3a0bd78SJulian Elischer 	TAILQ_REMOVE(&p->p_threads, td, td_plist);
566d3a0bd78SJulian Elischer 	p->p_numthreads--;
567d3a0bd78SJulian Elischer 	/* could clear a few other things here */
5688460a577SJohn Birrell 	/* Must  NOT clear links to proc! */
5695c8329edSJulian Elischer }
5705c8329edSJulian Elischer 
57179799053SKonstantin Belousov static int
57279799053SKonstantin Belousov calc_remaining(struct proc *p, int mode)
57379799053SKonstantin Belousov {
57479799053SKonstantin Belousov 	int remaining;
57579799053SKonstantin Belousov 
5767b519077SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
5777b519077SKonstantin Belousov 	PROC_SLOCK_ASSERT(p, MA_OWNED);
57879799053SKonstantin Belousov 	if (mode == SINGLE_EXIT)
57979799053SKonstantin Belousov 		remaining = p->p_numthreads;
58079799053SKonstantin Belousov 	else if (mode == SINGLE_BOUNDARY)
58179799053SKonstantin Belousov 		remaining = p->p_numthreads - p->p_boundary_count;
5826ddcc233SKonstantin Belousov 	else if (mode == SINGLE_NO_EXIT || mode == SINGLE_ALLPROC)
58379799053SKonstantin Belousov 		remaining = p->p_numthreads - p->p_suspcount;
58479799053SKonstantin Belousov 	else
58579799053SKonstantin Belousov 		panic("calc_remaining: wrong mode %d", mode);
58679799053SKonstantin Belousov 	return (remaining);
58779799053SKonstantin Belousov }
58879799053SKonstantin Belousov 
58907a9368aSKonstantin Belousov static int
59007a9368aSKonstantin Belousov remain_for_mode(int mode)
59107a9368aSKonstantin Belousov {
59207a9368aSKonstantin Belousov 
5936ddcc233SKonstantin Belousov 	return (mode == SINGLE_ALLPROC ? 0 : 1);
59407a9368aSKonstantin Belousov }
59507a9368aSKonstantin Belousov 
59607a9368aSKonstantin Belousov static int
59707a9368aSKonstantin Belousov weed_inhib(int mode, struct thread *td2, struct proc *p)
59807a9368aSKonstantin Belousov {
59907a9368aSKonstantin Belousov 	int wakeup_swapper;
60007a9368aSKonstantin Belousov 
60107a9368aSKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
60207a9368aSKonstantin Belousov 	PROC_SLOCK_ASSERT(p, MA_OWNED);
60307a9368aSKonstantin Belousov 	THREAD_LOCK_ASSERT(td2, MA_OWNED);
60407a9368aSKonstantin Belousov 
60507a9368aSKonstantin Belousov 	wakeup_swapper = 0;
60607a9368aSKonstantin Belousov 	switch (mode) {
60707a9368aSKonstantin Belousov 	case SINGLE_EXIT:
60807a9368aSKonstantin Belousov 		if (TD_IS_SUSPENDED(td2))
60984cdea97SKonstantin Belousov 			wakeup_swapper |= thread_unsuspend_one(td2, p, true);
61007a9368aSKonstantin Belousov 		if (TD_ON_SLEEPQ(td2) && (td2->td_flags & TDF_SINTR) != 0)
61107a9368aSKonstantin Belousov 			wakeup_swapper |= sleepq_abort(td2, EINTR);
61207a9368aSKonstantin Belousov 		break;
61307a9368aSKonstantin Belousov 	case SINGLE_BOUNDARY:
61407a9368aSKonstantin Belousov 		if (TD_IS_SUSPENDED(td2) && (td2->td_flags & TDF_BOUNDARY) == 0)
61584cdea97SKonstantin Belousov 			wakeup_swapper |= thread_unsuspend_one(td2, p, false);
61607a9368aSKonstantin Belousov 		if (TD_ON_SLEEPQ(td2) && (td2->td_flags & TDF_SINTR) != 0)
61707a9368aSKonstantin Belousov 			wakeup_swapper |= sleepq_abort(td2, ERESTART);
61807a9368aSKonstantin Belousov 		break;
61907a9368aSKonstantin Belousov 	case SINGLE_NO_EXIT:
62007a9368aSKonstantin Belousov 		if (TD_IS_SUSPENDED(td2) && (td2->td_flags & TDF_BOUNDARY) == 0)
62184cdea97SKonstantin Belousov 			wakeup_swapper |= thread_unsuspend_one(td2, p, false);
62207a9368aSKonstantin Belousov 		if (TD_ON_SLEEPQ(td2) && (td2->td_flags & TDF_SINTR) != 0)
62307a9368aSKonstantin Belousov 			wakeup_swapper |= sleepq_abort(td2, ERESTART);
624917dd390SKonstantin Belousov 		break;
6256ddcc233SKonstantin Belousov 	case SINGLE_ALLPROC:
6266ddcc233SKonstantin Belousov 		/*
6276ddcc233SKonstantin Belousov 		 * ALLPROC suspend tries to avoid spurious EINTR for
6286ddcc233SKonstantin Belousov 		 * threads sleeping interruptable, by suspending the
6296ddcc233SKonstantin Belousov 		 * thread directly, similarly to sig_suspend_threads().
6306ddcc233SKonstantin Belousov 		 * Since such sleep is not performed at the user
6316ddcc233SKonstantin Belousov 		 * boundary, TDF_BOUNDARY flag is not set, and TDF_ALLPROCSUSP
6326ddcc233SKonstantin Belousov 		 * is used to avoid immediate un-suspend.
6336ddcc233SKonstantin Belousov 		 */
6346ddcc233SKonstantin Belousov 		if (TD_IS_SUSPENDED(td2) && (td2->td_flags & (TDF_BOUNDARY |
6356ddcc233SKonstantin Belousov 		    TDF_ALLPROCSUSP)) == 0)
63684cdea97SKonstantin Belousov 			wakeup_swapper |= thread_unsuspend_one(td2, p, false);
6376ddcc233SKonstantin Belousov 		if (TD_ON_SLEEPQ(td2) && (td2->td_flags & TDF_SINTR) != 0) {
6386ddcc233SKonstantin Belousov 			if ((td2->td_flags & TDF_SBDRY) == 0) {
6396ddcc233SKonstantin Belousov 				thread_suspend_one(td2);
6406ddcc233SKonstantin Belousov 				td2->td_flags |= TDF_ALLPROCSUSP;
6416ddcc233SKonstantin Belousov 			} else {
6426ddcc233SKonstantin Belousov 				wakeup_swapper |= sleepq_abort(td2, ERESTART);
6436ddcc233SKonstantin Belousov 			}
6446ddcc233SKonstantin Belousov 		}
64507a9368aSKonstantin Belousov 		break;
64607a9368aSKonstantin Belousov 	}
64707a9368aSKonstantin Belousov 	return (wakeup_swapper);
64807a9368aSKonstantin Belousov }
64907a9368aSKonstantin Belousov 
6505215b187SJeff Roberson /*
65144990b8cSJulian Elischer  * Enforce single-threading.
65244990b8cSJulian Elischer  *
65344990b8cSJulian Elischer  * Returns 1 if the caller must abort (another thread is waiting to
65444990b8cSJulian Elischer  * exit the process or similar). Process is locked!
65544990b8cSJulian Elischer  * Returns 0 when you are successfully the only thread running.
65644990b8cSJulian Elischer  * A process has successfully single threaded in the suspend mode when
65744990b8cSJulian Elischer  * There are no threads in user mode. Threads in the kernel must be
65844990b8cSJulian Elischer  * allowed to continue until they get to the user boundary. They may even
65944990b8cSJulian Elischer  * copy out their return values and data before suspending. They may however be
660e2668f55SMaxim Konovalov  * accelerated in reaching the user boundary as we will wake up
66144990b8cSJulian Elischer  * any sleeping threads that are interruptable. (PCATCH).
66244990b8cSJulian Elischer  */
66344990b8cSJulian Elischer int
6646ddcc233SKonstantin Belousov thread_single(struct proc *p, int mode)
66544990b8cSJulian Elischer {
66644990b8cSJulian Elischer 	struct thread *td;
66744990b8cSJulian Elischer 	struct thread *td2;
668da7bbd2cSJohn Baldwin 	int remaining, wakeup_swapper;
66944990b8cSJulian Elischer 
67044990b8cSJulian Elischer 	td = curthread;
6716ddcc233SKonstantin Belousov 	KASSERT(mode == SINGLE_EXIT || mode == SINGLE_BOUNDARY ||
6726ddcc233SKonstantin Belousov 	    mode == SINGLE_ALLPROC || mode == SINGLE_NO_EXIT,
6736ddcc233SKonstantin Belousov 	    ("invalid mode %d", mode));
6746ddcc233SKonstantin Belousov 	/*
6756ddcc233SKonstantin Belousov 	 * If allowing non-ALLPROC singlethreading for non-curproc
6766ddcc233SKonstantin Belousov 	 * callers, calc_remaining() and remain_for_mode() should be
6776ddcc233SKonstantin Belousov 	 * adjusted to also account for td->td_proc != p.  For now
6786ddcc233SKonstantin Belousov 	 * this is not implemented because it is not used.
6796ddcc233SKonstantin Belousov 	 */
6806ddcc233SKonstantin Belousov 	KASSERT((mode == SINGLE_ALLPROC && td->td_proc != p) ||
6816ddcc233SKonstantin Belousov 	    (mode != SINGLE_ALLPROC && td->td_proc == p),
6826ddcc233SKonstantin Belousov 	    ("mode %d proc %p curproc %p", mode, p, td->td_proc));
68337814395SPeter Wemm 	mtx_assert(&Giant, MA_NOTOWNED);
68444990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
68544990b8cSJulian Elischer 
6866ddcc233SKonstantin Belousov 	if ((p->p_flag & P_HADTHREADS) == 0 && mode != SINGLE_ALLPROC)
68744990b8cSJulian Elischer 		return (0);
68844990b8cSJulian Elischer 
689e3b9bf71SJulian Elischer 	/* Is someone already single threading? */
690906ac69dSDavid Xu 	if (p->p_singlethread != NULL && p->p_singlethread != td)
69144990b8cSJulian Elischer 		return (1);
69244990b8cSJulian Elischer 
693906ac69dSDavid Xu 	if (mode == SINGLE_EXIT) {
694906ac69dSDavid Xu 		p->p_flag |= P_SINGLE_EXIT;
695906ac69dSDavid Xu 		p->p_flag &= ~P_SINGLE_BOUNDARY;
696906ac69dSDavid Xu 	} else {
697906ac69dSDavid Xu 		p->p_flag &= ~P_SINGLE_EXIT;
698906ac69dSDavid Xu 		if (mode == SINGLE_BOUNDARY)
699906ac69dSDavid Xu 			p->p_flag |= P_SINGLE_BOUNDARY;
700906ac69dSDavid Xu 		else
701906ac69dSDavid Xu 			p->p_flag &= ~P_SINGLE_BOUNDARY;
702906ac69dSDavid Xu 	}
7036ddcc233SKonstantin Belousov 	if (mode == SINGLE_ALLPROC)
7046ddcc233SKonstantin Belousov 		p->p_flag |= P_TOTAL_STOP;
7051279572aSDavid Xu 	p->p_flag |= P_STOPPED_SINGLE;
7067b4a950aSDavid Xu 	PROC_SLOCK(p);
707112afcb2SJohn Baldwin 	p->p_singlethread = td;
70879799053SKonstantin Belousov 	remaining = calc_remaining(p, mode);
70907a9368aSKonstantin Belousov 	while (remaining != remain_for_mode(mode)) {
710bf1a3220SDavid Xu 		if (P_SHOULDSTOP(p) != P_STOPPED_SINGLE)
711bf1a3220SDavid Xu 			goto stopme;
712da7bbd2cSJohn Baldwin 		wakeup_swapper = 0;
71344990b8cSJulian Elischer 		FOREACH_THREAD_IN_PROC(p, td2) {
71444990b8cSJulian Elischer 			if (td2 == td)
71544990b8cSJulian Elischer 				continue;
716a54e85fdSJeff Roberson 			thread_lock(td2);
717b7edba77SJeff Roberson 			td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
7186ddcc233SKonstantin Belousov 			if (TD_IS_INHIBITED(td2)) {
71907a9368aSKonstantin Belousov 				wakeup_swapper |= weed_inhib(mode, td2, p);
720d8267df7SDavid Xu #ifdef SMP
7216ddcc233SKonstantin Belousov 			} else if (TD_IS_RUNNING(td2) && td != td2) {
722d8267df7SDavid Xu 				forward_signal(td2);
723d8267df7SDavid Xu #endif
7246ddcc233SKonstantin Belousov 			}
725a54e85fdSJeff Roberson 			thread_unlock(td2);
7269d102777SJulian Elischer 		}
727da7bbd2cSJohn Baldwin 		if (wakeup_swapper)
728da7bbd2cSJohn Baldwin 			kick_proc0();
72979799053SKonstantin Belousov 		remaining = calc_remaining(p, mode);
730ec008e96SDavid Xu 
7319d102777SJulian Elischer 		/*
7329d102777SJulian Elischer 		 * Maybe we suspended some threads.. was it enough?
7339d102777SJulian Elischer 		 */
73407a9368aSKonstantin Belousov 		if (remaining == remain_for_mode(mode))
7359d102777SJulian Elischer 			break;
7369d102777SJulian Elischer 
737bf1a3220SDavid Xu stopme:
73844990b8cSJulian Elischer 		/*
73944990b8cSJulian Elischer 		 * Wake us up when everyone else has suspended.
740e3b9bf71SJulian Elischer 		 * In the mean time we suspend as well.
74144990b8cSJulian Elischer 		 */
7426ddcc233SKonstantin Belousov 		thread_suspend_switch(td, p);
74379799053SKonstantin Belousov 		remaining = calc_remaining(p, mode);
74444990b8cSJulian Elischer 	}
745906ac69dSDavid Xu 	if (mode == SINGLE_EXIT) {
74691599697SJulian Elischer 		/*
7478626a0ddSKonstantin Belousov 		 * Convert the process to an unthreaded process.  The
7488626a0ddSKonstantin Belousov 		 * SINGLE_EXIT is called by exit1() or execve(), in
7498626a0ddSKonstantin Belousov 		 * both cases other threads must be retired.
75091599697SJulian Elischer 		 */
7518626a0ddSKonstantin Belousov 		KASSERT(p->p_numthreads == 1, ("Unthreading with >1 threads"));
752ed062c8dSJulian Elischer 		p->p_singlethread = NULL;
7538626a0ddSKonstantin Belousov 		p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT | P_HADTHREADS);
754fd229b5bSKonstantin Belousov 
755fd229b5bSKonstantin Belousov 		/*
756fd229b5bSKonstantin Belousov 		 * Wait for any remaining threads to exit cpu_throw().
757fd229b5bSKonstantin Belousov 		 */
758fd229b5bSKonstantin Belousov 		while (p->p_exitthreads != 0) {
759fd229b5bSKonstantin Belousov 			PROC_SUNLOCK(p);
760fd229b5bSKonstantin Belousov 			PROC_UNLOCK(p);
761fd229b5bSKonstantin Belousov 			sched_relinquish(td);
762fd229b5bSKonstantin Belousov 			PROC_LOCK(p);
763fd229b5bSKonstantin Belousov 			PROC_SLOCK(p);
764fd229b5bSKonstantin Belousov 		}
765ac437c07SKonstantin Belousov 	} else if (mode == SINGLE_BOUNDARY) {
766ac437c07SKonstantin Belousov 		/*
767ac437c07SKonstantin Belousov 		 * Wait until all suspended threads are removed from
768ac437c07SKonstantin Belousov 		 * the processors.  The thread_suspend_check()
769ac437c07SKonstantin Belousov 		 * increments p_boundary_count while it is still
770ac437c07SKonstantin Belousov 		 * running, which makes it possible for the execve()
771ac437c07SKonstantin Belousov 		 * to destroy vmspace while our other threads are
772ac437c07SKonstantin Belousov 		 * still using the address space.
773ac437c07SKonstantin Belousov 		 *
774ac437c07SKonstantin Belousov 		 * We lock the thread, which is only allowed to
775ac437c07SKonstantin Belousov 		 * succeed after context switch code finished using
776ac437c07SKonstantin Belousov 		 * the address space.
777ac437c07SKonstantin Belousov 		 */
778ac437c07SKonstantin Belousov 		FOREACH_THREAD_IN_PROC(p, td2) {
779ac437c07SKonstantin Belousov 			if (td2 == td)
780ac437c07SKonstantin Belousov 				continue;
781ac437c07SKonstantin Belousov 			thread_lock(td2);
782ac437c07SKonstantin Belousov 			KASSERT((td2->td_flags & TDF_BOUNDARY) != 0,
783ac437c07SKonstantin Belousov 			    ("td %p not on boundary", td2));
784ac437c07SKonstantin Belousov 			KASSERT(TD_IS_SUSPENDED(td2),
785ac437c07SKonstantin Belousov 			    ("td %p is not suspended", td2));
786ac437c07SKonstantin Belousov 			thread_unlock(td2);
787ac437c07SKonstantin Belousov 		}
78891599697SJulian Elischer 	}
7897b4a950aSDavid Xu 	PROC_SUNLOCK(p);
79044990b8cSJulian Elischer 	return (0);
79144990b8cSJulian Elischer }
79244990b8cSJulian Elischer 
7938638fe7bSKonstantin Belousov bool
7948638fe7bSKonstantin Belousov thread_suspend_check_needed(void)
7958638fe7bSKonstantin Belousov {
7968638fe7bSKonstantin Belousov 	struct proc *p;
7978638fe7bSKonstantin Belousov 	struct thread *td;
7988638fe7bSKonstantin Belousov 
7998638fe7bSKonstantin Belousov 	td = curthread;
8008638fe7bSKonstantin Belousov 	p = td->td_proc;
8018638fe7bSKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
8028638fe7bSKonstantin Belousov 	return (P_SHOULDSTOP(p) || ((p->p_flag & P_TRACED) != 0 &&
8038638fe7bSKonstantin Belousov 	    (td->td_dbgflags & TDB_SUSPEND) != 0));
8048638fe7bSKonstantin Belousov }
8058638fe7bSKonstantin Belousov 
80644990b8cSJulian Elischer /*
80744990b8cSJulian Elischer  * Called in from locations that can safely check to see
80844990b8cSJulian Elischer  * whether we have to suspend or at least throttle for a
80944990b8cSJulian Elischer  * single-thread event (e.g. fork).
81044990b8cSJulian Elischer  *
81144990b8cSJulian Elischer  * Such locations include userret().
81244990b8cSJulian Elischer  * If the "return_instead" argument is non zero, the thread must be able to
81344990b8cSJulian Elischer  * accept 0 (caller may continue), or 1 (caller must abort) as a result.
81444990b8cSJulian Elischer  *
81544990b8cSJulian Elischer  * The 'return_instead' argument tells the function if it may do a
81644990b8cSJulian Elischer  * thread_exit() or suspend, or whether the caller must abort and back
81744990b8cSJulian Elischer  * out instead.
81844990b8cSJulian Elischer  *
81944990b8cSJulian Elischer  * If the thread that set the single_threading request has set the
82044990b8cSJulian Elischer  * P_SINGLE_EXIT bit in the process flags then this call will never return
82144990b8cSJulian Elischer  * if 'return_instead' is false, but will exit.
82244990b8cSJulian Elischer  *
82344990b8cSJulian Elischer  * P_SINGLE_EXIT | return_instead == 0| return_instead != 0
82444990b8cSJulian Elischer  *---------------+--------------------+---------------------
82544990b8cSJulian Elischer  *       0       | returns 0          |   returns 0 or 1
826353374b5SJohn Baldwin  *               | when ST ends       |   immediately
82744990b8cSJulian Elischer  *---------------+--------------------+---------------------
82844990b8cSJulian Elischer  *       1       | thread exits       |   returns 1
829353374b5SJohn Baldwin  *               |                    |  immediately
83044990b8cSJulian Elischer  * 0 = thread_exit() or suspension ok,
83144990b8cSJulian Elischer  * other = return error instead of stopping the thread.
83244990b8cSJulian Elischer  *
83344990b8cSJulian Elischer  * While a full suspension is under effect, even a single threading
83444990b8cSJulian Elischer  * thread would be suspended if it made this call (but it shouldn't).
83544990b8cSJulian Elischer  * This call should only be made from places where
83644990b8cSJulian Elischer  * thread_exit() would be safe as that may be the outcome unless
83744990b8cSJulian Elischer  * return_instead is set.
83844990b8cSJulian Elischer  */
83944990b8cSJulian Elischer int
84044990b8cSJulian Elischer thread_suspend_check(int return_instead)
84144990b8cSJulian Elischer {
842ecafb24bSJuli Mallett 	struct thread *td;
843ecafb24bSJuli Mallett 	struct proc *p;
8447847a9daSJohn Baldwin 	int wakeup_swapper;
84544990b8cSJulian Elischer 
84644990b8cSJulian Elischer 	td = curthread;
84744990b8cSJulian Elischer 	p = td->td_proc;
84837814395SPeter Wemm 	mtx_assert(&Giant, MA_NOTOWNED);
84944990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
8508638fe7bSKonstantin Belousov 	while (thread_suspend_check_needed()) {
8511279572aSDavid Xu 		if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
85244990b8cSJulian Elischer 			KASSERT(p->p_singlethread != NULL,
85344990b8cSJulian Elischer 			    ("singlethread not set"));
85444990b8cSJulian Elischer 			/*
855e3b9bf71SJulian Elischer 			 * The only suspension in action is a
856e3b9bf71SJulian Elischer 			 * single-threading. Single threader need not stop.
857b6d5995eSJulian Elischer 			 * XXX Should be safe to access unlocked
858b6d5995eSJulian Elischer 			 * as it can only be set to be true by us.
85944990b8cSJulian Elischer 			 */
860e3b9bf71SJulian Elischer 			if (p->p_singlethread == td)
86144990b8cSJulian Elischer 				return (0);	/* Exempt from stopping. */
86244990b8cSJulian Elischer 		}
86345a4bfa1SDavid Xu 		if ((p->p_flag & P_SINGLE_EXIT) && return_instead)
86494f0972bSDavid Xu 			return (EINTR);
86544990b8cSJulian Elischer 
866906ac69dSDavid Xu 		/* Should we goto user boundary if we didn't come from there? */
867906ac69dSDavid Xu 		if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE &&
868906ac69dSDavid Xu 		    (p->p_flag & P_SINGLE_BOUNDARY) && return_instead)
86994f0972bSDavid Xu 			return (ERESTART);
870906ac69dSDavid Xu 
87144990b8cSJulian Elischer 		/*
8723077f938SKonstantin Belousov 		 * Ignore suspend requests if they are deferred.
873d071a6faSJohn Baldwin 		 */
8743077f938SKonstantin Belousov 		if ((td->td_flags & TDF_SBDRY) != 0) {
875d071a6faSJohn Baldwin 			KASSERT(return_instead,
876d071a6faSJohn Baldwin 			    ("TDF_SBDRY set for unsafe thread_suspend_check"));
877d071a6faSJohn Baldwin 			return (0);
878d071a6faSJohn Baldwin 		}
879d071a6faSJohn Baldwin 
880d071a6faSJohn Baldwin 		/*
88144990b8cSJulian Elischer 		 * If the process is waiting for us to exit,
88244990b8cSJulian Elischer 		 * this thread should just suicide.
8831279572aSDavid Xu 		 * Assumes that P_SINGLE_EXIT implies P_STOPPED_SINGLE.
88444990b8cSJulian Elischer 		 */
885cf7d9a8cSDavid Xu 		if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) {
886cf7d9a8cSDavid Xu 			PROC_UNLOCK(p);
887cf7d9a8cSDavid Xu 			tidhash_remove(td);
888*91d1786fSDmitry Chagin 
889*91d1786fSDmitry Chagin 			/*
890*91d1786fSDmitry Chagin 			 * Allow Linux emulation layer to do some work
891*91d1786fSDmitry Chagin 			 * before thread suicide.
892*91d1786fSDmitry Chagin 			 */
893*91d1786fSDmitry Chagin 			if (__predict_false(p->p_sysent->sv_thread_detach != NULL))
894*91d1786fSDmitry Chagin 				(p->p_sysent->sv_thread_detach)(td);
895*91d1786fSDmitry Chagin 
896cf7d9a8cSDavid Xu 			PROC_LOCK(p);
89721ecd1e9SDavid Xu 			tdsigcleanup(td);
89813dad108SKonstantin Belousov 			umtx_thread_exit(td);
899cf7d9a8cSDavid Xu 			PROC_SLOCK(p);
90021ecd1e9SDavid Xu 			thread_stopped(p);
90144990b8cSJulian Elischer 			thread_exit();
902cf7d9a8cSDavid Xu 		}
90321ecd1e9SDavid Xu 
90421ecd1e9SDavid Xu 		PROC_SLOCK(p);
90521ecd1e9SDavid Xu 		thread_stopped(p);
906a54e85fdSJeff Roberson 		if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
907a54e85fdSJeff Roberson 			if (p->p_numthreads == p->p_suspcount + 1) {
908a54e85fdSJeff Roberson 				thread_lock(p->p_singlethread);
90984cdea97SKonstantin Belousov 				wakeup_swapper = thread_unsuspend_one(
91084cdea97SKonstantin Belousov 				    p->p_singlethread, p, false);
911a54e85fdSJeff Roberson 				thread_unlock(p->p_singlethread);
9127847a9daSJohn Baldwin 				if (wakeup_swapper)
9137847a9daSJohn Baldwin 					kick_proc0();
914a54e85fdSJeff Roberson 			}
915a54e85fdSJeff Roberson 		}
9163f9be10eSDavid Xu 		PROC_UNLOCK(p);
9177b4a950aSDavid Xu 		thread_lock(td);
91844990b8cSJulian Elischer 		/*
91944990b8cSJulian Elischer 		 * When a thread suspends, it just
920ad1e7d28SJulian Elischer 		 * gets taken off all queues.
92144990b8cSJulian Elischer 		 */
92271fad9fdSJulian Elischer 		thread_suspend_one(td);
923906ac69dSDavid Xu 		if (return_instead == 0) {
924906ac69dSDavid Xu 			p->p_boundary_count++;
925906ac69dSDavid Xu 			td->td_flags |= TDF_BOUNDARY;
926cf19bf91SJulian Elischer 		}
9277b4a950aSDavid Xu 		PROC_SUNLOCK(p);
9288df78c41SJeff Roberson 		mi_switch(SW_INVOL | SWT_SUSPEND, NULL);
929a54e85fdSJeff Roberson 		thread_unlock(td);
93044990b8cSJulian Elischer 		PROC_LOCK(p);
93144990b8cSJulian Elischer 	}
93244990b8cSJulian Elischer 	return (0);
93344990b8cSJulian Elischer }
93444990b8cSJulian Elischer 
93535c32a76SDavid Xu void
9366ddcc233SKonstantin Belousov thread_suspend_switch(struct thread *td, struct proc *p)
937a54e85fdSJeff Roberson {
938a54e85fdSJeff Roberson 
939a54e85fdSJeff Roberson 	KASSERT(!TD_IS_SUSPENDED(td), ("already suspended"));
940a54e85fdSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
9417b4a950aSDavid Xu 	PROC_SLOCK_ASSERT(p, MA_OWNED);
942a54e85fdSJeff Roberson 	/*
943a54e85fdSJeff Roberson 	 * We implement thread_suspend_one in stages here to avoid
944a54e85fdSJeff Roberson 	 * dropping the proc lock while the thread lock is owned.
945a54e85fdSJeff Roberson 	 */
9466ddcc233SKonstantin Belousov 	if (p == td->td_proc) {
947a54e85fdSJeff Roberson 		thread_stopped(p);
948a54e85fdSJeff Roberson 		p->p_suspcount++;
9496ddcc233SKonstantin Belousov 	}
9503f9be10eSDavid Xu 	PROC_UNLOCK(p);
9517b4a950aSDavid Xu 	thread_lock(td);
952b7edba77SJeff Roberson 	td->td_flags &= ~TDF_NEEDSUSPCHK;
953a54e85fdSJeff Roberson 	TD_SET_SUSPENDED(td);
954c5aa6b58SJeff Roberson 	sched_sleep(td, 0);
9557b4a950aSDavid Xu 	PROC_SUNLOCK(p);
956a54e85fdSJeff Roberson 	DROP_GIANT();
9578df78c41SJeff Roberson 	mi_switch(SW_VOL | SWT_SUSPEND, NULL);
958a54e85fdSJeff Roberson 	thread_unlock(td);
959a54e85fdSJeff Roberson 	PICKUP_GIANT();
960a54e85fdSJeff Roberson 	PROC_LOCK(p);
9617b4a950aSDavid Xu 	PROC_SLOCK(p);
962a54e85fdSJeff Roberson }
963a54e85fdSJeff Roberson 
964a54e85fdSJeff Roberson void
96535c32a76SDavid Xu thread_suspend_one(struct thread *td)
96635c32a76SDavid Xu {
9676ddcc233SKonstantin Belousov 	struct proc *p;
96835c32a76SDavid Xu 
9696ddcc233SKonstantin Belousov 	p = td->td_proc;
9707b4a950aSDavid Xu 	PROC_SLOCK_ASSERT(p, MA_OWNED);
971a54e85fdSJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
972e574e444SDavid Xu 	KASSERT(!TD_IS_SUSPENDED(td), ("already suspended"));
97335c32a76SDavid Xu 	p->p_suspcount++;
974b7edba77SJeff Roberson 	td->td_flags &= ~TDF_NEEDSUSPCHK;
97571fad9fdSJulian Elischer 	TD_SET_SUSPENDED(td);
976c5aa6b58SJeff Roberson 	sched_sleep(td, 0);
97735c32a76SDavid Xu }
97835c32a76SDavid Xu 
97984cdea97SKonstantin Belousov static int
98084cdea97SKonstantin Belousov thread_unsuspend_one(struct thread *td, struct proc *p, bool boundary)
98135c32a76SDavid Xu {
98235c32a76SDavid Xu 
983a54e85fdSJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
984ad1e7d28SJulian Elischer 	KASSERT(TD_IS_SUSPENDED(td), ("Thread not suspended"));
98571fad9fdSJulian Elischer 	TD_CLR_SUSPENDED(td);
9866ddcc233SKonstantin Belousov 	td->td_flags &= ~TDF_ALLPROCSUSP;
9876ddcc233SKonstantin Belousov 	if (td->td_proc == p) {
9886ddcc233SKonstantin Belousov 		PROC_SLOCK_ASSERT(p, MA_OWNED);
98935c32a76SDavid Xu 		p->p_suspcount--;
99084cdea97SKonstantin Belousov 		if (boundary && (td->td_flags & TDF_BOUNDARY) != 0) {
99184cdea97SKonstantin Belousov 			td->td_flags &= ~TDF_BOUNDARY;
99284cdea97SKonstantin Belousov 			p->p_boundary_count--;
99384cdea97SKonstantin Belousov 		}
9946ddcc233SKonstantin Belousov 	}
9957847a9daSJohn Baldwin 	return (setrunnable(td));
99635c32a76SDavid Xu }
99735c32a76SDavid Xu 
99844990b8cSJulian Elischer /*
99944990b8cSJulian Elischer  * Allow all threads blocked by single threading to continue running.
100044990b8cSJulian Elischer  */
100144990b8cSJulian Elischer void
100244990b8cSJulian Elischer thread_unsuspend(struct proc *p)
100344990b8cSJulian Elischer {
100444990b8cSJulian Elischer 	struct thread *td;
10057847a9daSJohn Baldwin 	int wakeup_swapper;
100644990b8cSJulian Elischer 
100744990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
10087b4a950aSDavid Xu 	PROC_SLOCK_ASSERT(p, MA_OWNED);
10097847a9daSJohn Baldwin 	wakeup_swapper = 0;
101044990b8cSJulian Elischer 	if (!P_SHOULDSTOP(p)) {
1011ad1e7d28SJulian Elischer                 FOREACH_THREAD_IN_PROC(p, td) {
1012a54e85fdSJeff Roberson 			thread_lock(td);
1013ad1e7d28SJulian Elischer 			if (TD_IS_SUSPENDED(td)) {
101484cdea97SKonstantin Belousov 				wakeup_swapper |= thread_unsuspend_one(td, p,
101584cdea97SKonstantin Belousov 				    true);
101644990b8cSJulian Elischer 			}
1017a54e85fdSJeff Roberson 			thread_unlock(td);
1018ad1e7d28SJulian Elischer 		}
101984cdea97SKonstantin Belousov 	} else if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE &&
102084cdea97SKonstantin Belousov 	    p->p_numthreads == p->p_suspcount) {
102144990b8cSJulian Elischer 		/*
102244990b8cSJulian Elischer 		 * Stopping everything also did the job for the single
102344990b8cSJulian Elischer 		 * threading request. Now we've downgraded to single-threaded,
102444990b8cSJulian Elischer 		 * let it continue.
102544990b8cSJulian Elischer 		 */
10266ddcc233SKonstantin Belousov 		if (p->p_singlethread->td_proc == p) {
1027a54e85fdSJeff Roberson 			thread_lock(p->p_singlethread);
10286ddcc233SKonstantin Belousov 			wakeup_swapper = thread_unsuspend_one(
102984cdea97SKonstantin Belousov 			    p->p_singlethread, p, false);
1030a54e85fdSJeff Roberson 			thread_unlock(p->p_singlethread);
103144990b8cSJulian Elischer 		}
10326ddcc233SKonstantin Belousov 	}
10337847a9daSJohn Baldwin 	if (wakeup_swapper)
10347847a9daSJohn Baldwin 		kick_proc0();
103544990b8cSJulian Elischer }
103644990b8cSJulian Elischer 
1037ed062c8dSJulian Elischer /*
1038ed062c8dSJulian Elischer  * End the single threading mode..
1039ed062c8dSJulian Elischer  */
104044990b8cSJulian Elischer void
10416ddcc233SKonstantin Belousov thread_single_end(struct proc *p, int mode)
104244990b8cSJulian Elischer {
104344990b8cSJulian Elischer 	struct thread *td;
10447847a9daSJohn Baldwin 	int wakeup_swapper;
104544990b8cSJulian Elischer 
10466ddcc233SKonstantin Belousov 	KASSERT(mode == SINGLE_EXIT || mode == SINGLE_BOUNDARY ||
10476ddcc233SKonstantin Belousov 	    mode == SINGLE_ALLPROC || mode == SINGLE_NO_EXIT,
10486ddcc233SKonstantin Belousov 	    ("invalid mode %d", mode));
104944990b8cSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
10506ddcc233SKonstantin Belousov 	KASSERT((mode == SINGLE_ALLPROC && (p->p_flag & P_TOTAL_STOP) != 0) ||
10516ddcc233SKonstantin Belousov 	    (mode != SINGLE_ALLPROC && (p->p_flag & P_TOTAL_STOP) == 0),
10526ddcc233SKonstantin Belousov 	    ("mode %d does not match P_TOTAL_STOP", mode));
105384cdea97SKonstantin Belousov 	KASSERT(mode == SINGLE_ALLPROC || p->p_singlethread == curthread,
105484cdea97SKonstantin Belousov 	    ("thread_single_end from other thread %p %p",
105584cdea97SKonstantin Belousov 	    curthread, p->p_singlethread));
105684cdea97SKonstantin Belousov 	KASSERT(mode != SINGLE_BOUNDARY ||
105784cdea97SKonstantin Belousov 	    (p->p_flag & P_SINGLE_BOUNDARY) != 0,
105884cdea97SKonstantin Belousov 	    ("mis-matched SINGLE_BOUNDARY flags %x", p->p_flag));
10596ddcc233SKonstantin Belousov 	p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT | P_SINGLE_BOUNDARY |
10606ddcc233SKonstantin Belousov 	    P_TOTAL_STOP);
10617b4a950aSDavid Xu 	PROC_SLOCK(p);
106244990b8cSJulian Elischer 	p->p_singlethread = NULL;
10637847a9daSJohn Baldwin 	wakeup_swapper = 0;
106449539972SJulian Elischer 	/*
10657847a9daSJohn Baldwin 	 * If there are other threads they may now run,
106649539972SJulian Elischer 	 * unless of course there is a blanket 'stop order'
106749539972SJulian Elischer 	 * on the process. The single threader must be allowed
106849539972SJulian Elischer 	 * to continue however as this is a bad place to stop.
106949539972SJulian Elischer 	 */
10706ddcc233SKonstantin Belousov 	if (p->p_numthreads != remain_for_mode(mode) && !P_SHOULDSTOP(p)) {
1071ad1e7d28SJulian Elischer                 FOREACH_THREAD_IN_PROC(p, td) {
1072a54e85fdSJeff Roberson 			thread_lock(td);
1073ad1e7d28SJulian Elischer 			if (TD_IS_SUSPENDED(td)) {
107484cdea97SKonstantin Belousov 				wakeup_swapper |= thread_unsuspend_one(td, p,
107584cdea97SKonstantin Belousov 				    mode == SINGLE_BOUNDARY);
107644990b8cSJulian Elischer 			}
1077a54e85fdSJeff Roberson 			thread_unlock(td);
107849539972SJulian Elischer 		}
1079ad1e7d28SJulian Elischer 	}
108084cdea97SKonstantin Belousov 	KASSERT(mode != SINGLE_BOUNDARY || p->p_boundary_count == 0,
108184cdea97SKonstantin Belousov 	    ("inconsistent boundary count %d", p->p_boundary_count));
10827b4a950aSDavid Xu 	PROC_SUNLOCK(p);
10837847a9daSJohn Baldwin 	if (wakeup_swapper)
10847847a9daSJohn Baldwin 		kick_proc0();
108549539972SJulian Elischer }
10864fc21c09SDaniel Eischen 
108744355392SDavid Xu struct thread *
108844355392SDavid Xu thread_find(struct proc *p, lwpid_t tid)
108944355392SDavid Xu {
109044355392SDavid Xu 	struct thread *td;
109144355392SDavid Xu 
109244355392SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
109344355392SDavid Xu 	FOREACH_THREAD_IN_PROC(p, td) {
109444355392SDavid Xu 		if (td->td_tid == tid)
109544355392SDavid Xu 			break;
109644355392SDavid Xu 	}
109744355392SDavid Xu 	return (td);
109844355392SDavid Xu }
1099cf7d9a8cSDavid Xu 
1100cf7d9a8cSDavid Xu /* Locate a thread by number; return with proc lock held. */
1101cf7d9a8cSDavid Xu struct thread *
1102cf7d9a8cSDavid Xu tdfind(lwpid_t tid, pid_t pid)
1103cf7d9a8cSDavid Xu {
1104cf7d9a8cSDavid Xu #define RUN_THRESH	16
1105cf7d9a8cSDavid Xu 	struct thread *td;
1106cf7d9a8cSDavid Xu 	int run = 0;
1107cf7d9a8cSDavid Xu 
1108cf7d9a8cSDavid Xu 	rw_rlock(&tidhash_lock);
1109cf7d9a8cSDavid Xu 	LIST_FOREACH(td, TIDHASH(tid), td_hash) {
1110cf7d9a8cSDavid Xu 		if (td->td_tid == tid) {
1111cf7d9a8cSDavid Xu 			if (pid != -1 && td->td_proc->p_pid != pid) {
1112cf7d9a8cSDavid Xu 				td = NULL;
1113cf7d9a8cSDavid Xu 				break;
1114cf7d9a8cSDavid Xu 			}
11158e6fa660SJohn Baldwin 			PROC_LOCK(td->td_proc);
1116cf7d9a8cSDavid Xu 			if (td->td_proc->p_state == PRS_NEW) {
11178e6fa660SJohn Baldwin 				PROC_UNLOCK(td->td_proc);
1118cf7d9a8cSDavid Xu 				td = NULL;
1119cf7d9a8cSDavid Xu 				break;
1120cf7d9a8cSDavid Xu 			}
1121cf7d9a8cSDavid Xu 			if (run > RUN_THRESH) {
1122cf7d9a8cSDavid Xu 				if (rw_try_upgrade(&tidhash_lock)) {
1123cf7d9a8cSDavid Xu 					LIST_REMOVE(td, td_hash);
1124cf7d9a8cSDavid Xu 					LIST_INSERT_HEAD(TIDHASH(td->td_tid),
1125cf7d9a8cSDavid Xu 						td, td_hash);
1126cf7d9a8cSDavid Xu 					rw_wunlock(&tidhash_lock);
1127cf7d9a8cSDavid Xu 					return (td);
1128cf7d9a8cSDavid Xu 				}
1129cf7d9a8cSDavid Xu 			}
1130cf7d9a8cSDavid Xu 			break;
1131cf7d9a8cSDavid Xu 		}
1132cf7d9a8cSDavid Xu 		run++;
1133cf7d9a8cSDavid Xu 	}
1134cf7d9a8cSDavid Xu 	rw_runlock(&tidhash_lock);
1135cf7d9a8cSDavid Xu 	return (td);
1136cf7d9a8cSDavid Xu }
1137cf7d9a8cSDavid Xu 
1138cf7d9a8cSDavid Xu void
1139cf7d9a8cSDavid Xu tidhash_add(struct thread *td)
1140cf7d9a8cSDavid Xu {
1141cf7d9a8cSDavid Xu 	rw_wlock(&tidhash_lock);
1142cf7d9a8cSDavid Xu 	LIST_INSERT_HEAD(TIDHASH(td->td_tid), td, td_hash);
1143cf7d9a8cSDavid Xu 	rw_wunlock(&tidhash_lock);
1144cf7d9a8cSDavid Xu }
1145cf7d9a8cSDavid Xu 
1146cf7d9a8cSDavid Xu void
1147cf7d9a8cSDavid Xu tidhash_remove(struct thread *td)
1148cf7d9a8cSDavid Xu {
1149cf7d9a8cSDavid Xu 	rw_wlock(&tidhash_lock);
1150cf7d9a8cSDavid Xu 	LIST_REMOVE(td, td_hash);
1151cf7d9a8cSDavid Xu 	rw_wunlock(&tidhash_lock);
1152cf7d9a8cSDavid Xu }
1153