xref: /freebsd/sys/kern/kern_thread.c (revision 30d239bc4c510432e65a84fa1c14ed67a3ab1c92)
1 /*-
2  * Copyright (C) 2001 Julian Elischer <julian@freebsd.org>.
3  *  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice(s), this list of conditions and the following disclaimer as
10  *    the first lines of this file unmodified other than the possible
11  *    addition of one or more copyright notices.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice(s), this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26  * DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 #include <sys/proc.h>
38 #include <sys/resourcevar.h>
39 #include <sys/smp.h>
40 #include <sys/sysctl.h>
41 #include <sys/sched.h>
42 #include <sys/sleepqueue.h>
43 #include <sys/turnstile.h>
44 #include <sys/ktr.h>
45 #include <sys/umtx.h>
46 
47 #include <security/audit/audit.h>
48 
49 #include <vm/vm.h>
50 #include <vm/vm_extern.h>
51 #include <vm/uma.h>
52 
53 /*
54  * thread related storage.
55  */
56 static uma_zone_t thread_zone;
57 
58 SYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW, 0, "thread allocation");
59 
60 int max_threads_per_proc = 1500;
61 SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_per_proc, CTLFLAG_RW,
62 	&max_threads_per_proc, 0, "Limit on threads per proc");
63 
64 int max_threads_hits;
65 SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_hits, CTLFLAG_RD,
66 	&max_threads_hits, 0, "");
67 
68 #ifdef KSE
69 int virtual_cpu;
70 
71 #endif
72 TAILQ_HEAD(, thread) zombie_threads = TAILQ_HEAD_INITIALIZER(zombie_threads);
73 static struct mtx zombie_lock;
74 MTX_SYSINIT(zombie_lock, &zombie_lock, "zombie lock", MTX_SPIN);
75 
76 static void thread_zombie(struct thread *);
77 
78 #ifdef KSE
79 static int
80 sysctl_kse_virtual_cpu(SYSCTL_HANDLER_ARGS)
81 {
82 	int error, new_val;
83 	int def_val;
84 
85 	def_val = mp_ncpus;
86 	if (virtual_cpu == 0)
87 		new_val = def_val;
88 	else
89 		new_val = virtual_cpu;
90 	error = sysctl_handle_int(oidp, &new_val, 0, req);
91 	if (error != 0 || req->newptr == NULL)
92 		return (error);
93 	if (new_val < 0)
94 		return (EINVAL);
95 	virtual_cpu = new_val;
96 	return (0);
97 }
98 
99 /* DEBUG ONLY */
100 SYSCTL_PROC(_kern_threads, OID_AUTO, virtual_cpu, CTLTYPE_INT|CTLFLAG_RW,
101 	0, sizeof(virtual_cpu), sysctl_kse_virtual_cpu, "I",
102 	"debug virtual cpus");
103 #endif
104 
105 struct mtx tid_lock;
106 static struct unrhdr *tid_unrhdr;
107 
108 /*
109  * Prepare a thread for use.
110  */
111 static int
112 thread_ctor(void *mem, int size, void *arg, int flags)
113 {
114 	struct thread	*td;
115 
116 	td = (struct thread *)mem;
117 	td->td_state = TDS_INACTIVE;
118 	td->td_oncpu = NOCPU;
119 
120 	td->td_tid = alloc_unr(tid_unrhdr);
121 	td->td_syscalls = 0;
122 
123 	/*
124 	 * Note that td_critnest begins life as 1 because the thread is not
125 	 * running and is thereby implicitly waiting to be on the receiving
126 	 * end of a context switch.
127 	 */
128 	td->td_critnest = 1;
129 
130 #ifdef AUDIT
131 	audit_thread_alloc(td);
132 #endif
133 	umtx_thread_alloc(td);
134 	return (0);
135 }
136 
137 /*
138  * Reclaim a thread after use.
139  */
140 static void
141 thread_dtor(void *mem, int size, void *arg)
142 {
143 	struct thread *td;
144 
145 	td = (struct thread *)mem;
146 
147 #ifdef INVARIANTS
148 	/* Verify that this thread is in a safe state to free. */
149 	switch (td->td_state) {
150 	case TDS_INHIBITED:
151 	case TDS_RUNNING:
152 	case TDS_CAN_RUN:
153 	case TDS_RUNQ:
154 		/*
155 		 * We must never unlink a thread that is in one of
156 		 * these states, because it is currently active.
157 		 */
158 		panic("bad state for thread unlinking");
159 		/* NOTREACHED */
160 	case TDS_INACTIVE:
161 		break;
162 	default:
163 		panic("bad thread state");
164 		/* NOTREACHED */
165 	}
166 #endif
167 #ifdef AUDIT
168 	audit_thread_free(td);
169 #endif
170 	free_unr(tid_unrhdr, td->td_tid);
171 	sched_newthread(td);
172 }
173 
174 /*
175  * Initialize type-stable parts of a thread (when newly created).
176  */
177 static int
178 thread_init(void *mem, int size, int flags)
179 {
180 	struct thread *td;
181 
182 	td = (struct thread *)mem;
183 
184 	vm_thread_new(td, 0);
185 	cpu_thread_setup(td);
186 	td->td_sleepqueue = sleepq_alloc();
187 	td->td_turnstile = turnstile_alloc();
188 	td->td_sched = (struct td_sched *)&td[1];
189 	sched_newthread(td);
190 	umtx_thread_init(td);
191 	return (0);
192 }
193 
194 /*
195  * Tear down type-stable parts of a thread (just before being discarded).
196  */
197 static void
198 thread_fini(void *mem, int size)
199 {
200 	struct thread *td;
201 
202 	td = (struct thread *)mem;
203 	turnstile_free(td->td_turnstile);
204 	sleepq_free(td->td_sleepqueue);
205 	umtx_thread_fini(td);
206 	vm_thread_dispose(td);
207 }
208 
209 /*
210  * For a newly created process,
211  * link up all the structures and its initial threads etc.
212  * called from:
213  * {arch}/{arch}/machdep.c   ia64_init(), init386() etc.
214  * proc_dtor() (should go away)
215  * proc_init()
216  */
217 void
218 proc_linkup(struct proc *p, struct thread *td)
219 {
220 
221 	TAILQ_INIT(&p->p_threads);	     /* all threads in proc */
222 #ifdef KSE
223 	TAILQ_INIT(&p->p_upcalls);	     /* upcall list */
224 #endif
225 	sigqueue_init(&p->p_sigqueue, p);
226 	p->p_ksi = ksiginfo_alloc(1);
227 	if (p->p_ksi != NULL) {
228 		/* XXX p_ksi may be null if ksiginfo zone is not ready */
229 		p->p_ksi->ksi_flags = KSI_EXT | KSI_INS;
230 	}
231 	LIST_INIT(&p->p_mqnotifier);
232 	p->p_numthreads = 0;
233 	thread_link(td, p);
234 }
235 
236 /*
237  * Initialize global thread allocation resources.
238  */
239 void
240 threadinit(void)
241 {
242 
243 	mtx_init(&tid_lock, "TID lock", NULL, MTX_DEF);
244 	tid_unrhdr = new_unrhdr(PID_MAX + 1, INT_MAX, &tid_lock);
245 
246 	thread_zone = uma_zcreate("THREAD", sched_sizeof_thread(),
247 	    thread_ctor, thread_dtor, thread_init, thread_fini,
248 	    16 - 1, 0);
249 #ifdef KSE
250 	kseinit();	/* set up kse specific stuff  e.g. upcall zone*/
251 #endif
252 }
253 
254 /*
255  * Place an unused thread on the zombie list.
256  * Use the slpq as that must be unused by now.
257  */
258 void
259 thread_zombie(struct thread *td)
260 {
261 	mtx_lock_spin(&zombie_lock);
262 	TAILQ_INSERT_HEAD(&zombie_threads, td, td_slpq);
263 	mtx_unlock_spin(&zombie_lock);
264 }
265 
266 /*
267  * Release a thread that has exited after cpu_throw().
268  */
269 void
270 thread_stash(struct thread *td)
271 {
272 	atomic_subtract_rel_int(&td->td_proc->p_exitthreads, 1);
273 	thread_zombie(td);
274 }
275 
276 /*
277  * Reap zombie kse resource.
278  */
279 void
280 thread_reap(void)
281 {
282 	struct thread *td_first, *td_next;
283 
284 	/*
285 	 * Don't even bother to lock if none at this instant,
286 	 * we really don't care about the next instant..
287 	 */
288 	if (!TAILQ_EMPTY(&zombie_threads)) {
289 		mtx_lock_spin(&zombie_lock);
290 		td_first = TAILQ_FIRST(&zombie_threads);
291 		if (td_first)
292 			TAILQ_INIT(&zombie_threads);
293 		mtx_unlock_spin(&zombie_lock);
294 		while (td_first) {
295 			td_next = TAILQ_NEXT(td_first, td_slpq);
296 			if (td_first->td_ucred)
297 				crfree(td_first->td_ucred);
298 			thread_free(td_first);
299 			td_first = td_next;
300 		}
301 	}
302 #ifdef KSE
303 	upcall_reap();
304 #endif
305 }
306 
307 /*
308  * Allocate a thread.
309  */
310 struct thread *
311 thread_alloc(void)
312 {
313 
314 	thread_reap(); /* check if any zombies to get */
315 	return (uma_zalloc(thread_zone, M_WAITOK));
316 }
317 
318 
319 /*
320  * Deallocate a thread.
321  */
322 void
323 thread_free(struct thread *td)
324 {
325 
326 	cpu_thread_clean(td);
327 	uma_zfree(thread_zone, td);
328 }
329 
330 /*
331  * Discard the current thread and exit from its context.
332  * Always called with scheduler locked.
333  *
334  * Because we can't free a thread while we're operating under its context,
335  * push the current thread into our CPU's deadthread holder. This means
336  * we needn't worry about someone else grabbing our context before we
337  * do a cpu_throw().  This may not be needed now as we are under schedlock.
338  * Maybe we can just do a thread_stash() as thr_exit1 does.
339  */
340 /*  XXX
341  * libthr expects its thread exit to return for the last
342  * thread, meaning that the program is back to non-threaded
343  * mode I guess. Because we do this (cpu_throw) unconditionally
344  * here, they have their own version of it. (thr_exit1())
345  * that doesn't do it all if this was the last thread.
346  * It is also called from thread_suspend_check().
347  * Of course in the end, they end up coming here through exit1
348  * anyhow..  After fixing 'thr' to play by the rules we should be able
349  * to merge these two functions together.
350  *
351  * called from:
352  * exit1()
353  * kse_exit()
354  * thr_exit()
355  * ifdef KSE
356  * thread_user_enter()
357  * thread_userret()
358  * endif
359  * thread_suspend_check()
360  */
361 void
362 thread_exit(void)
363 {
364 	uint64_t new_switchtime;
365 	struct thread *td;
366 	struct thread *td2;
367 	struct proc *p;
368 
369 	td = curthread;
370 	p = td->td_proc;
371 
372 	PROC_SLOCK_ASSERT(p, MA_OWNED);
373 	mtx_assert(&Giant, MA_NOTOWNED);
374 
375 	PROC_LOCK_ASSERT(p, MA_OWNED);
376 	KASSERT(p != NULL, ("thread exiting without a process"));
377 	CTR3(KTR_PROC, "thread_exit: thread %p (pid %ld, %s)", td,
378 	    (long)p->p_pid, p->p_comm);
379 	KASSERT(TAILQ_EMPTY(&td->td_sigqueue.sq_list), ("signal pending"));
380 
381 #ifdef AUDIT
382 	AUDIT_SYSCALL_EXIT(0, td);
383 #endif
384 
385 #ifdef KSE
386 	if (td->td_standin != NULL) {
387 		/*
388 		 * Note that we don't need to free the cred here as it
389 		 * is done in thread_reap().
390 		 */
391 		thread_zombie(td->td_standin);
392 		td->td_standin = NULL;
393 	}
394 #endif
395 
396 	umtx_thread_exit(td);
397 
398 	/*
399 	 * drop FPU & debug register state storage, or any other
400 	 * architecture specific resources that
401 	 * would not be on a new untouched process.
402 	 */
403 	cpu_thread_exit(td);	/* XXXSMP */
404 
405 	/* Do the same timestamp bookkeeping that mi_switch() would do. */
406 	new_switchtime = cpu_ticks();
407 	p->p_rux.rux_runtime += (new_switchtime - PCPU_GET(switchtime));
408 	PCPU_SET(switchtime, new_switchtime);
409 	PCPU_SET(switchticks, ticks);
410 	PCPU_INC(cnt.v_swtch);
411 	/* Save our resource usage in our process. */
412 	td->td_ru.ru_nvcsw++;
413 	rucollect(&p->p_ru, &td->td_ru);
414 	/*
415 	 * The last thread is left attached to the process
416 	 * So that the whole bundle gets recycled. Skip
417 	 * all this stuff if we never had threads.
418 	 * EXIT clears all sign of other threads when
419 	 * it goes to single threading, so the last thread always
420 	 * takes the short path.
421 	 */
422 	if (p->p_flag & P_HADTHREADS) {
423 		if (p->p_numthreads > 1) {
424 			thread_lock(td);
425 #ifdef KSE
426 			kse_unlink(td);
427 #else
428 			thread_unlink(td);
429 #endif
430 			thread_unlock(td);
431 			td2 = FIRST_THREAD_IN_PROC(p);
432 			sched_exit_thread(td2, td);
433 
434 			/*
435 			 * The test below is NOT true if we are the
436 			 * sole exiting thread. P_STOPPED_SNGL is unset
437 			 * in exit1() after it is the only survivor.
438 			 */
439 			if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
440 				if (p->p_numthreads == p->p_suspcount) {
441 					thread_lock(p->p_singlethread);
442 					thread_unsuspend_one(p->p_singlethread);
443 					thread_unlock(p->p_singlethread);
444 				}
445 			}
446 
447 			atomic_add_int(&td->td_proc->p_exitthreads, 1);
448 			PCPU_SET(deadthread, td);
449 		} else {
450 			/*
451 			 * The last thread is exiting.. but not through exit()
452 			 * what should we do?
453 			 * Theoretically this can't happen
454  			 * exit1() - clears threading flags before coming here
455  			 * kse_exit() - treats last thread specially
456  			 * thr_exit() - treats last thread specially
457 			 * ifdef KSE
458  			 * thread_user_enter() - only if more exist
459  			 * thread_userret() - only if more exist
460 			 * endif
461  			 * thread_suspend_check() - only if more exist
462 			 */
463 			panic ("thread_exit: Last thread exiting on its own");
464 		}
465 	}
466 	PROC_UNLOCK(p);
467 	thread_lock(td);
468 	/* Save our tick information with both the thread and proc locked */
469 	ruxagg(&p->p_rux, td);
470 	PROC_SUNLOCK(p);
471 	td->td_state = TDS_INACTIVE;
472 	CTR1(KTR_PROC, "thread_exit: cpu_throw() thread %p", td);
473 	sched_throw(td);
474 	panic("I'm a teapot!");
475 	/* NOTREACHED */
476 }
477 
478 /*
479  * Do any thread specific cleanups that may be needed in wait()
480  * called with Giant, proc and schedlock not held.
481  */
482 void
483 thread_wait(struct proc *p)
484 {
485 	struct thread *td;
486 
487 	mtx_assert(&Giant, MA_NOTOWNED);
488 	KASSERT((p->p_numthreads == 1), ("Multiple threads in wait1()"));
489 	td = FIRST_THREAD_IN_PROC(p);
490 #ifdef KSE
491 	if (td->td_standin != NULL) {
492 		if (td->td_standin->td_ucred != NULL) {
493 			crfree(td->td_standin->td_ucred);
494 			td->td_standin->td_ucred = NULL;
495 		}
496 		thread_free(td->td_standin);
497 		td->td_standin = NULL;
498 	}
499 #endif
500 	/* Lock the last thread so we spin until it exits cpu_throw(). */
501 	thread_lock(td);
502 	thread_unlock(td);
503 	/* Wait for any remaining threads to exit cpu_throw(). */
504 	while (p->p_exitthreads)
505 		sched_relinquish(curthread);
506 	cpu_thread_clean(td);
507 	crfree(td->td_ucred);
508 	thread_reap();	/* check for zombie threads etc. */
509 }
510 
511 /*
512  * Link a thread to a process.
513  * set up anything that needs to be initialized for it to
514  * be used by the process.
515  *
516  * Note that we do not link to the proc's ucred here.
517  * The thread is linked as if running but no KSE assigned.
518  * Called from:
519  *  proc_linkup()
520  *  thread_schedule_upcall()
521  *  thr_create()
522  */
523 void
524 thread_link(struct thread *td, struct proc *p)
525 {
526 
527 	/*
528 	 * XXX This can't be enabled because it's called for proc0 before
529 	 * it's spinlock has been created.
530 	 * PROC_SLOCK_ASSERT(p, MA_OWNED);
531 	 */
532 	td->td_state    = TDS_INACTIVE;
533 	td->td_proc     = p;
534 	td->td_flags    = TDF_INMEM;
535 
536 	LIST_INIT(&td->td_contested);
537 	sigqueue_init(&td->td_sigqueue, p);
538 	callout_init(&td->td_slpcallout, CALLOUT_MPSAFE);
539 	TAILQ_INSERT_HEAD(&p->p_threads, td, td_plist);
540 	p->p_numthreads++;
541 }
542 
543 /*
544  * Convert a process with one thread to an unthreaded process.
545  * Called from:
546  *  thread_single(exit)  (called from execve and exit)
547  *  kse_exit()		XXX may need cleaning up wrt KSE stuff
548  */
549 void
550 thread_unthread(struct thread *td)
551 {
552 	struct proc *p = td->td_proc;
553 
554 	KASSERT((p->p_numthreads == 1), ("Unthreading with >1 threads"));
555 #ifdef KSE
556 	thread_lock(td);
557 	upcall_remove(td);
558 	thread_unlock(td);
559 	p->p_flag &= ~(P_SA|P_HADTHREADS);
560 	td->td_mailbox = NULL;
561 	td->td_pflags &= ~(TDP_SA | TDP_CAN_UNBIND);
562 	if (td->td_standin != NULL) {
563 		thread_zombie(td->td_standin);
564 		td->td_standin = NULL;
565 	}
566 #else
567 	p->p_flag &= ~P_HADTHREADS;
568 #endif
569 }
570 
571 /*
572  * Called from:
573  *  thread_exit()
574  */
575 void
576 thread_unlink(struct thread *td)
577 {
578 	struct proc *p = td->td_proc;
579 
580 	PROC_SLOCK_ASSERT(p, MA_OWNED);
581 	TAILQ_REMOVE(&p->p_threads, td, td_plist);
582 	p->p_numthreads--;
583 	/* could clear a few other things here */
584 	/* Must  NOT clear links to proc! */
585 }
586 
587 /*
588  * Enforce single-threading.
589  *
590  * Returns 1 if the caller must abort (another thread is waiting to
591  * exit the process or similar). Process is locked!
592  * Returns 0 when you are successfully the only thread running.
593  * A process has successfully single threaded in the suspend mode when
594  * There are no threads in user mode. Threads in the kernel must be
595  * allowed to continue until they get to the user boundary. They may even
596  * copy out their return values and data before suspending. They may however be
597  * accelerated in reaching the user boundary as we will wake up
598  * any sleeping threads that are interruptable. (PCATCH).
599  */
600 int
601 thread_single(int mode)
602 {
603 	struct thread *td;
604 	struct thread *td2;
605 	struct proc *p;
606 	int remaining;
607 
608 	td = curthread;
609 	p = td->td_proc;
610 	mtx_assert(&Giant, MA_NOTOWNED);
611 	PROC_LOCK_ASSERT(p, MA_OWNED);
612 	KASSERT((td != NULL), ("curthread is NULL"));
613 
614 	if ((p->p_flag & P_HADTHREADS) == 0)
615 		return (0);
616 
617 	/* Is someone already single threading? */
618 	if (p->p_singlethread != NULL && p->p_singlethread != td)
619 		return (1);
620 
621 	if (mode == SINGLE_EXIT) {
622 		p->p_flag |= P_SINGLE_EXIT;
623 		p->p_flag &= ~P_SINGLE_BOUNDARY;
624 	} else {
625 		p->p_flag &= ~P_SINGLE_EXIT;
626 		if (mode == SINGLE_BOUNDARY)
627 			p->p_flag |= P_SINGLE_BOUNDARY;
628 		else
629 			p->p_flag &= ~P_SINGLE_BOUNDARY;
630 	}
631 	p->p_flag |= P_STOPPED_SINGLE;
632 	PROC_SLOCK(p);
633 	p->p_singlethread = td;
634 	if (mode == SINGLE_EXIT)
635 		remaining = p->p_numthreads;
636 	else if (mode == SINGLE_BOUNDARY)
637 		remaining = p->p_numthreads - p->p_boundary_count;
638 	else
639 		remaining = p->p_numthreads - p->p_suspcount;
640 	while (remaining != 1) {
641 		if (P_SHOULDSTOP(p) != P_STOPPED_SINGLE)
642 			goto stopme;
643 		FOREACH_THREAD_IN_PROC(p, td2) {
644 			if (td2 == td)
645 				continue;
646 			thread_lock(td2);
647 			td2->td_flags |= TDF_ASTPENDING;
648 			if (TD_IS_INHIBITED(td2)) {
649 				switch (mode) {
650 				case SINGLE_EXIT:
651 					if (td->td_flags & TDF_DBSUSPEND)
652 						td->td_flags &= ~TDF_DBSUSPEND;
653 					if (TD_IS_SUSPENDED(td2))
654 						thread_unsuspend_one(td2);
655 					if (TD_ON_SLEEPQ(td2) &&
656 					    (td2->td_flags & TDF_SINTR))
657 						sleepq_abort(td2, EINTR);
658 					break;
659 				case SINGLE_BOUNDARY:
660 					if (TD_IS_SUSPENDED(td2) &&
661 					    !(td2->td_flags & TDF_BOUNDARY))
662 						thread_unsuspend_one(td2);
663 					if (TD_ON_SLEEPQ(td2) &&
664 					    (td2->td_flags & TDF_SINTR))
665 						sleepq_abort(td2, ERESTART);
666 					break;
667 				default:
668 					if (TD_IS_SUSPENDED(td2)) {
669 						thread_unlock(td2);
670 						continue;
671 					}
672 					/*
673 					 * maybe other inhibited states too?
674 					 */
675 					if ((td2->td_flags & TDF_SINTR) &&
676 					    (td2->td_inhibitors &
677 					    (TDI_SLEEPING | TDI_SWAPPED)))
678 						thread_suspend_one(td2);
679 					break;
680 				}
681 			}
682 #ifdef SMP
683 			else if (TD_IS_RUNNING(td2) && td != td2) {
684 				forward_signal(td2);
685 			}
686 #endif
687 			thread_unlock(td2);
688 		}
689 		if (mode == SINGLE_EXIT)
690 			remaining = p->p_numthreads;
691 		else if (mode == SINGLE_BOUNDARY)
692 			remaining = p->p_numthreads - p->p_boundary_count;
693 		else
694 			remaining = p->p_numthreads - p->p_suspcount;
695 
696 		/*
697 		 * Maybe we suspended some threads.. was it enough?
698 		 */
699 		if (remaining == 1)
700 			break;
701 
702 stopme:
703 		/*
704 		 * Wake us up when everyone else has suspended.
705 		 * In the mean time we suspend as well.
706 		 */
707 		thread_suspend_switch(td);
708 		if (mode == SINGLE_EXIT)
709 			remaining = p->p_numthreads;
710 		else if (mode == SINGLE_BOUNDARY)
711 			remaining = p->p_numthreads - p->p_boundary_count;
712 		else
713 			remaining = p->p_numthreads - p->p_suspcount;
714 	}
715 	if (mode == SINGLE_EXIT) {
716 		/*
717 		 * We have gotten rid of all the other threads and we
718 		 * are about to either exit or exec. In either case,
719 		 * we try our utmost  to revert to being a non-threaded
720 		 * process.
721 		 */
722 		p->p_singlethread = NULL;
723 		p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT);
724 		thread_unthread(td);
725 	}
726 	PROC_SUNLOCK(p);
727 	return (0);
728 }
729 
730 /*
731  * Called in from locations that can safely check to see
732  * whether we have to suspend or at least throttle for a
733  * single-thread event (e.g. fork).
734  *
735  * Such locations include userret().
736  * If the "return_instead" argument is non zero, the thread must be able to
737  * accept 0 (caller may continue), or 1 (caller must abort) as a result.
738  *
739  * The 'return_instead' argument tells the function if it may do a
740  * thread_exit() or suspend, or whether the caller must abort and back
741  * out instead.
742  *
743  * If the thread that set the single_threading request has set the
744  * P_SINGLE_EXIT bit in the process flags then this call will never return
745  * if 'return_instead' is false, but will exit.
746  *
747  * P_SINGLE_EXIT | return_instead == 0| return_instead != 0
748  *---------------+--------------------+---------------------
749  *       0       | returns 0          |   returns 0 or 1
750  *               | when ST ends       |   immediatly
751  *---------------+--------------------+---------------------
752  *       1       | thread exits       |   returns 1
753  *               |                    |  immediatly
754  * 0 = thread_exit() or suspension ok,
755  * other = return error instead of stopping the thread.
756  *
757  * While a full suspension is under effect, even a single threading
758  * thread would be suspended if it made this call (but it shouldn't).
759  * This call should only be made from places where
760  * thread_exit() would be safe as that may be the outcome unless
761  * return_instead is set.
762  */
763 int
764 thread_suspend_check(int return_instead)
765 {
766 	struct thread *td;
767 	struct proc *p;
768 
769 	td = curthread;
770 	p = td->td_proc;
771 	mtx_assert(&Giant, MA_NOTOWNED);
772 	PROC_LOCK_ASSERT(p, MA_OWNED);
773 	while (P_SHOULDSTOP(p) ||
774 	      ((p->p_flag & P_TRACED) && (td->td_flags & TDF_DBSUSPEND))) {
775 		if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
776 			KASSERT(p->p_singlethread != NULL,
777 			    ("singlethread not set"));
778 			/*
779 			 * The only suspension in action is a
780 			 * single-threading. Single threader need not stop.
781 			 * XXX Should be safe to access unlocked
782 			 * as it can only be set to be true by us.
783 			 */
784 			if (p->p_singlethread == td)
785 				return (0);	/* Exempt from stopping. */
786 		}
787 		if ((p->p_flag & P_SINGLE_EXIT) && return_instead)
788 			return (EINTR);
789 
790 		/* Should we goto user boundary if we didn't come from there? */
791 		if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE &&
792 		    (p->p_flag & P_SINGLE_BOUNDARY) && return_instead)
793 			return (ERESTART);
794 
795 		/* If thread will exit, flush its pending signals */
796 		if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td))
797 			sigqueue_flush(&td->td_sigqueue);
798 
799 		PROC_SLOCK(p);
800 		thread_stopped(p);
801 		/*
802 		 * If the process is waiting for us to exit,
803 		 * this thread should just suicide.
804 		 * Assumes that P_SINGLE_EXIT implies P_STOPPED_SINGLE.
805 		 */
806 		if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td))
807 			thread_exit();
808 		if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
809 			if (p->p_numthreads == p->p_suspcount + 1) {
810 				thread_lock(p->p_singlethread);
811 				thread_unsuspend_one(p->p_singlethread);
812 				thread_unlock(p->p_singlethread);
813 			}
814 		}
815 		PROC_UNLOCK(p);
816 		thread_lock(td);
817 		/*
818 		 * When a thread suspends, it just
819 		 * gets taken off all queues.
820 		 */
821 		thread_suspend_one(td);
822 		if (return_instead == 0) {
823 			p->p_boundary_count++;
824 			td->td_flags |= TDF_BOUNDARY;
825 		}
826 		PROC_SUNLOCK(p);
827 		mi_switch(SW_INVOL, NULL);
828 		if (return_instead == 0)
829 			td->td_flags &= ~TDF_BOUNDARY;
830 		thread_unlock(td);
831 		PROC_LOCK(p);
832 		if (return_instead == 0)
833 			p->p_boundary_count--;
834 	}
835 	return (0);
836 }
837 
838 void
839 thread_suspend_switch(struct thread *td)
840 {
841 	struct proc *p;
842 
843 	p = td->td_proc;
844 	KASSERT(!TD_IS_SUSPENDED(td), ("already suspended"));
845 	PROC_LOCK_ASSERT(p, MA_OWNED);
846 	PROC_SLOCK_ASSERT(p, MA_OWNED);
847 	/*
848 	 * We implement thread_suspend_one in stages here to avoid
849 	 * dropping the proc lock while the thread lock is owned.
850 	 */
851 	thread_stopped(p);
852 	p->p_suspcount++;
853 	PROC_UNLOCK(p);
854 	thread_lock(td);
855 	sched_sleep(td);
856 	TD_SET_SUSPENDED(td);
857 	PROC_SUNLOCK(p);
858 	DROP_GIANT();
859 	mi_switch(SW_VOL, NULL);
860 	thread_unlock(td);
861 	PICKUP_GIANT();
862 	PROC_LOCK(p);
863 	PROC_SLOCK(p);
864 }
865 
866 void
867 thread_suspend_one(struct thread *td)
868 {
869 	struct proc *p = td->td_proc;
870 
871 	PROC_SLOCK_ASSERT(p, MA_OWNED);
872 	THREAD_LOCK_ASSERT(td, MA_OWNED);
873 	KASSERT(!TD_IS_SUSPENDED(td), ("already suspended"));
874 	p->p_suspcount++;
875 	sched_sleep(td);
876 	TD_SET_SUSPENDED(td);
877 }
878 
879 void
880 thread_unsuspend_one(struct thread *td)
881 {
882 	struct proc *p = td->td_proc;
883 
884 	PROC_SLOCK_ASSERT(p, MA_OWNED);
885 	THREAD_LOCK_ASSERT(td, MA_OWNED);
886 	KASSERT(TD_IS_SUSPENDED(td), ("Thread not suspended"));
887 	TD_CLR_SUSPENDED(td);
888 	p->p_suspcount--;
889 	setrunnable(td);
890 }
891 
892 /*
893  * Allow all threads blocked by single threading to continue running.
894  */
895 void
896 thread_unsuspend(struct proc *p)
897 {
898 	struct thread *td;
899 
900 	PROC_LOCK_ASSERT(p, MA_OWNED);
901 	PROC_SLOCK_ASSERT(p, MA_OWNED);
902 	if (!P_SHOULDSTOP(p)) {
903                 FOREACH_THREAD_IN_PROC(p, td) {
904 			thread_lock(td);
905 			if (TD_IS_SUSPENDED(td)) {
906 				thread_unsuspend_one(td);
907 			}
908 			thread_unlock(td);
909 		}
910 	} else if ((P_SHOULDSTOP(p) == P_STOPPED_SINGLE) &&
911 	    (p->p_numthreads == p->p_suspcount)) {
912 		/*
913 		 * Stopping everything also did the job for the single
914 		 * threading request. Now we've downgraded to single-threaded,
915 		 * let it continue.
916 		 */
917 		thread_lock(p->p_singlethread);
918 		thread_unsuspend_one(p->p_singlethread);
919 		thread_unlock(p->p_singlethread);
920 	}
921 }
922 
923 /*
924  * End the single threading mode..
925  */
926 void
927 thread_single_end(void)
928 {
929 	struct thread *td;
930 	struct proc *p;
931 
932 	td = curthread;
933 	p = td->td_proc;
934 	PROC_LOCK_ASSERT(p, MA_OWNED);
935 	p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT | P_SINGLE_BOUNDARY);
936 	PROC_SLOCK(p);
937 	p->p_singlethread = NULL;
938 	/*
939 	 * If there are other threads they mey now run,
940 	 * unless of course there is a blanket 'stop order'
941 	 * on the process. The single threader must be allowed
942 	 * to continue however as this is a bad place to stop.
943 	 */
944 	if ((p->p_numthreads != 1) && (!P_SHOULDSTOP(p))) {
945                 FOREACH_THREAD_IN_PROC(p, td) {
946 			thread_lock(td);
947 			if (TD_IS_SUSPENDED(td)) {
948 				thread_unsuspend_one(td);
949 			}
950 			thread_unlock(td);
951 		}
952 	}
953 	PROC_SUNLOCK(p);
954 }
955 
956 struct thread *
957 thread_find(struct proc *p, lwpid_t tid)
958 {
959 	struct thread *td;
960 
961 	PROC_LOCK_ASSERT(p, MA_OWNED);
962 	PROC_SLOCK(p);
963 	FOREACH_THREAD_IN_PROC(p, td) {
964 		if (td->td_tid == tid)
965 			break;
966 	}
967 	PROC_SUNLOCK(p);
968 	return (td);
969 }
970