xref: /freebsd/sys/kern/kern_thread.c (revision 11f0b352e05306cf6f1f85e9087022c0a92624a3)
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  * $FreeBSD$
29  */
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/lock.h>
35 #include <sys/malloc.h>
36 #include <sys/mutex.h>
37 #include <sys/proc.h>
38 #include <sys/sysctl.h>
39 #include <sys/filedesc.h>
40 #include <sys/tty.h>
41 #include <sys/signalvar.h>
42 #include <sys/sx.h>
43 #include <sys/user.h>
44 #include <sys/jail.h>
45 #include <sys/kse.h>
46 #include <sys/ktr.h>
47 
48 #include <vm/vm.h>
49 #include <vm/vm_object.h>
50 #include <vm/pmap.h>
51 #include <vm/uma.h>
52 #include <vm/vm_map.h>
53 
54 /*
55  * Thread related storage.
56  */
57 static uma_zone_t thread_zone;
58 static int allocated_threads;
59 static int active_threads;
60 static int cached_threads;
61 
62 SYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW, 0, "thread allocation");
63 
64 SYSCTL_INT(_kern_threads, OID_AUTO, active, CTLFLAG_RD,
65 	&active_threads, 0, "Number of active threads in system.");
66 
67 SYSCTL_INT(_kern_threads, OID_AUTO, cached, CTLFLAG_RD,
68 	&cached_threads, 0, "Number of threads in thread cache.");
69 
70 SYSCTL_INT(_kern_threads, OID_AUTO, allocated, CTLFLAG_RD,
71 	&allocated_threads, 0, "Number of threads in zone.");
72 
73 static int oiks_debug = 1;	/* 0 disable, 1 printf, 2 enter debugger */
74 SYSCTL_INT(_kern_threads, OID_AUTO, oiks, CTLFLAG_RW,
75 	&oiks_debug, 0, "OIKS thread debug");
76 
77 #define RANGEOF(type, start, end) (offsetof(type, end) - offsetof(type, start))
78 
79 struct threadqueue zombie_threads = TAILQ_HEAD_INITIALIZER(zombie_threads);
80 struct mtx zombie_thread_lock;
81 MTX_SYSINIT(zombie_thread_lock, &zombie_thread_lock,
82     "zombie_thread_lock", MTX_SPIN);
83 
84 /*
85  * Pepare a thread for use.
86  */
87 static void
88 thread_ctor(void *mem, int size, void *arg)
89 {
90 	struct thread	*td;
91 
92 	KASSERT((size == sizeof(struct thread)),
93 	    ("size mismatch: %d != %d\n", size, (int)sizeof(struct thread)));
94 
95 	td = (struct thread *)mem;
96 	bzero(&td->td_startzero,
97 	    (unsigned)RANGEOF(struct thread, td_startzero, td_endzero));
98 	td->td_state = TDS_NEW;
99 	td->td_flags |= TDF_UNBOUND;
100 #if 0
101 	/*
102 	 * Maybe move these here from process creation, but maybe not.
103 	 * Moving them here takes them away from their "natural" place
104 	 * in the fork process.
105 	 */
106 	/* XXX td_contested does not appear to be initialized for threads! */
107 	LIST_INIT(&td->td_contested);
108 	callout_init(&td->td_slpcallout, 1);
109 #endif
110 	cached_threads--;	/* XXXSMP */
111 	active_threads++;	/* XXXSMP */
112 }
113 
114 /*
115  * Reclaim a thread after use.
116  */
117 static void
118 thread_dtor(void *mem, int size, void *arg)
119 {
120 	struct thread	*td;
121 
122 	KASSERT((size == sizeof(struct thread)),
123 	    ("size mismatch: %d != %d\n", size, (int)sizeof(struct thread)));
124 
125 	td = (struct thread *)mem;
126 
127 #ifdef INVARIANTS
128 	/* Verify that this thread is in a safe state to free. */
129 	switch (td->td_state) {
130 	case TDS_SLP:
131 	case TDS_MTX:
132 	case TDS_RUNQ:
133 		/*
134 		 * We must never unlink a thread that is in one of
135 		 * these states, because it is currently active.
136 		 */
137 		panic("bad state for thread unlinking");
138 		/* NOTREACHED */
139 	case TDS_UNQUEUED:
140 	case TDS_NEW:
141 	case TDS_RUNNING:
142 	case TDS_SURPLUS:
143 		break;
144 	default:
145 		panic("bad thread state");
146 		/* NOTREACHED */
147 	}
148 #endif
149 
150 	/* Update counters. */
151 	active_threads--;	/* XXXSMP */
152 	cached_threads++;	/* XXXSMP */
153 }
154 
155 /*
156  * Initialize type-stable parts of a thread (when newly created).
157  */
158 static void
159 thread_init(void *mem, int size)
160 {
161 	struct thread	*td;
162 
163 	KASSERT((size == sizeof(struct thread)),
164 	    ("size mismatch: %d != %d\n", size, (int)sizeof(struct thread)));
165 
166 	td = (struct thread *)mem;
167 	pmap_new_thread(td);
168 	cpu_thread_setup(td);
169 	cached_threads++;	/* XXXSMP */
170 	allocated_threads++;	/* XXXSMP */
171 }
172 
173 /*
174  * Tear down type-stable parts of a thread (just before being discarded).
175  */
176 static void
177 thread_fini(void *mem, int size)
178 {
179 	struct thread	*td;
180 
181 	KASSERT((size == sizeof(struct thread)),
182 	    ("size mismatch: %d != %d\n", size, (int)sizeof(struct thread)));
183 
184 	td = (struct thread *)mem;
185 	pmap_dispose_thread(td);
186 	cached_threads--;	/* XXXSMP */
187 	allocated_threads--;	/* XXXSMP */
188 }
189 
190 /*
191  * Initialize global thread allocation resources.
192  */
193 void
194 threadinit(void)
195 {
196 
197 	thread_zone = uma_zcreate("THREAD", sizeof (struct thread),
198 	    thread_ctor, thread_dtor, thread_init, thread_fini,
199 	    UMA_ALIGN_CACHE, 0);
200 }
201 
202 /*
203  * Stash an embarasingly esxtra thread into the zombie thread queue.
204  */
205 void
206 thread_stash(struct thread *td)
207 {
208 	mtx_lock_spin(&zombie_thread_lock);
209 	TAILQ_INSERT_HEAD(&zombie_threads, td, td_runq);
210 	mtx_unlock_spin(&zombie_thread_lock);
211 }
212 
213 /*
214  * reap any  zombie threads for this Processor.
215  */
216 void
217 thread_reap(void)
218 {
219 	struct thread *td_reaped;
220 
221 	/*
222 	 * don't even bother to lock if none at this instant
223 	 * We really don't care about the next instant..
224 	 */
225 	if (!TAILQ_EMPTY(&zombie_threads)) {
226 		mtx_lock_spin(&zombie_thread_lock);
227 		while (!TAILQ_EMPTY(&zombie_threads)) {
228 			td_reaped = TAILQ_FIRST(&zombie_threads);
229 			TAILQ_REMOVE(&zombie_threads, td_reaped, td_runq);
230 			mtx_unlock_spin(&zombie_thread_lock);
231 			thread_free(td_reaped);
232 			mtx_lock_spin(&zombie_thread_lock);
233 		}
234 		mtx_unlock_spin(&zombie_thread_lock);
235 	}
236 }
237 
238 /*
239  * Allocate a thread.
240  */
241 struct thread *
242 thread_alloc(void)
243 {
244 	thread_reap(); /* check if any zombies to get */
245 	return (uma_zalloc(thread_zone, M_WAITOK));
246 }
247 
248 /*
249  * Deallocate a thread.
250  */
251 void
252 thread_free(struct thread *td)
253 {
254 	uma_zfree(thread_zone, td);
255 }
256 
257 /*
258  * Store the thread context in the UTS's mailbox.
259  */
260 int
261 thread_export_context(struct thread *td)
262 {
263 	struct kse *ke;
264 	uintptr_t td2_mbx;
265 	void *addr1;
266 	void *addr2;
267 	int error;
268 
269 	/* Export the register contents. */
270 	error = cpu_export_context(td);
271 
272 	ke = td->td_kse;
273 	addr1 = (caddr_t)ke->ke_mailbox
274 			+ offsetof(struct kse_mailbox, kmbx_completed_threads);
275 	addr2 = (caddr_t)td->td_mailbox
276 			+ offsetof(struct thread_mailbox , next_completed);
277 	/* Then link it into it's KSE's list of completed threads. */
278 	if (!error) {
279 		error = td2_mbx = fuword(addr1);
280 		if (error == -1)
281 			error = EFAULT;
282 		else
283 			error = 0;
284 	}
285 	if (!error)
286 		error = suword(addr2, td2_mbx);
287 	if (!error)
288 		error = suword(addr1, (u_long)td->td_mailbox);
289 	if (error == -1)
290 		error = EFAULT;
291 	return (error);
292 }
293 
294 
295 /*
296  * Discard the current thread and exit from its context.
297  *
298  * Because we can't free a thread while we're operating under its context,
299  * push the current thread into our KSE's ke_tdspare slot, freeing the
300  * thread that might be there currently. Because we know that only this
301  * processor will run our KSE, we needn't worry about someone else grabbing
302  * our context before we do a cpu_throw.
303  */
304 void
305 thread_exit(void)
306 {
307 	struct thread *td;
308 	struct kse *ke;
309 	struct proc *p;
310 	struct ksegrp	*kg;
311 
312 	td = curthread;
313 	kg = td->td_ksegrp;
314 	p = td->td_proc;
315 	ke = td->td_kse;
316 
317 	mtx_assert(&sched_lock, MA_OWNED);
318 	PROC_LOCK_ASSERT(p, MA_OWNED);
319 	CTR1(KTR_PROC, "thread_exit: thread %p", td);
320 	KASSERT(!mtx_owned(&Giant), ("dying thread owns giant"));
321 
322 	if (ke->ke_tdspare != NULL) {
323 		thread_stash(ke->ke_tdspare);
324 		ke->ke_tdspare = NULL;
325 	}
326 	cpu_thread_exit(td);	/* XXXSMP */
327 
328 	/* Reassign this thread's KSE. */
329 	if (ke != NULL) {
330 KASSERT((ke->ke_state == KES_RUNNING), ("zapping kse not running"));
331 KASSERT((ke->ke_thread == td ), ("kse ke_thread mismatch against curthread"));
332 KASSERT((ke->ke_thread->td_state == TDS_RUNNING), ("zapping thread not running"));
333 		ke->ke_thread = NULL;
334 		td->td_kse = NULL;
335 		ke->ke_state = KES_UNQUEUED;
336 		kse_reassign(ke);
337 	}
338 
339 	/* Unlink this thread from its proc. and the kseg */
340 	if (p != NULL) {
341 		TAILQ_REMOVE(&p->p_threads, td, td_plist);
342 		p->p_numthreads--;
343 		if (kg != NULL) {
344 			TAILQ_REMOVE(&kg->kg_threads, td, td_kglist);
345 			kg->kg_numthreads--;
346 		}
347 		/*
348 		 * The test below is NOT true if we are the
349 		 * sole exiting thread. P_STOPPED_SNGL is unset
350 		 * in exit1() after it is the only survivor.
351 		 */
352 		if (P_SHOULDSTOP(p) == P_STOPPED_SNGL) {
353 			if (p->p_numthreads == p->p_suspcount) {
354 				TAILQ_REMOVE(&p->p_suspended,
355 				    p->p_singlethread, td_runq);
356 				setrunqueue(p->p_singlethread);
357 				p->p_suspcount--;
358 			}
359 		}
360 	}
361 	td->td_state	= TDS_SURPLUS;
362 	td->td_proc	= NULL;
363 	td->td_ksegrp	= NULL;
364 	td->td_last_kse	= NULL;
365 	ke->ke_tdspare = td;
366 	PROC_UNLOCK(p);
367 	cpu_throw();
368 	/* NOTREACHED */
369 }
370 
371 /*
372  * Link a thread to a process.
373  *
374  * Note that we do not link to the proc's ucred here.
375  * The thread is linked as if running but no KSE assigned.
376  */
377 void
378 thread_link(struct thread *td, struct ksegrp *kg)
379 {
380 	struct proc *p;
381 
382 	p = kg->kg_proc;
383 	td->td_state = TDS_NEW;
384 	td->td_proc	= p;
385 	td->td_ksegrp	= kg;
386 	td->td_last_kse	= NULL;
387 
388 	TAILQ_INSERT_HEAD(&p->p_threads, td, td_plist);
389 	TAILQ_INSERT_HEAD(&kg->kg_threads, td, td_kglist);
390 	p->p_numthreads++;
391 	kg->kg_numthreads++;
392 	if (oiks_debug && p->p_numthreads > 4) {
393 		printf("OIKS %d\n", p->p_numthreads);
394 		if (oiks_debug > 1)
395 			Debugger("OIKS");
396 	}
397 	td->td_critnest = 0;
398 	td->td_kse	= NULL;
399 }
400 
401 /*
402  * Set up the upcall pcb in either a given thread or a new one
403  * if none given. Use the upcall for the given KSE
404  * XXXKSE possibly fix cpu_set_upcall() to not need td->td_kse set.
405  */
406 struct thread *
407 thread_schedule_upcall(struct thread *td, struct kse *ke)
408 {
409 	struct thread *td2;
410 
411 	mtx_assert(&sched_lock, MA_OWNED);
412 	if (ke->ke_tdspare != NULL) {
413 		td2 = ke->ke_tdspare;
414 		ke->ke_tdspare = NULL;
415 	} else {
416 		mtx_unlock_spin(&sched_lock);
417 		td2 = thread_alloc();
418 		mtx_lock_spin(&sched_lock);
419 	}
420 	CTR3(KTR_PROC, "thread_schedule_upcall: thread %p (pid %d, %s)",
421 	     td, td->td_proc->p_pid, td->td_proc->p_comm);
422 	thread_link(td2, ke->ke_ksegrp);
423 	cpu_set_upcall(td2, ke->ke_pcb);
424 	td2->td_ucred = crhold(td->td_ucred);
425 	td2->td_flags = TDF_UNBOUND|TDF_UPCALLING;
426 	td2->td_priority = td->td_priority;
427 	setrunqueue(td2);
428 	return (td2);
429 }
430 
431 /*
432  * The extra work we go through if we are a threaded process when we
433  * return to userland
434  *
435  * If we are a KSE process and returning to user mode, check for
436  * extra work to do before we return (e.g. for more syscalls
437  * to complete first).  If we were in a critical section, we should
438  * just return to let it finish. Same if we were in the UTS (in
439  * which case we will have no thread mailbox registered).  The only
440  * traps we suport will have set the mailbox.  We will clear it here.
441  */
442 int
443 thread_userret(struct proc *p, struct ksegrp *kg, struct kse *ke,
444     struct thread *td, struct trapframe *frame)
445 {
446 	int error = 0;
447 
448 	if (ke->ke_tdspare == NULL) {
449 		ke->ke_tdspare = thread_alloc();
450 	}
451 	if (td->td_flags & TDF_UNBOUND) {
452 		/*
453 		 * Are we returning from a thread that had a mailbox?
454 		 *
455 		 * XXX Maybe this should be in a separate function.
456 		 */
457 		if (((td->td_flags & TDF_UPCALLING) == 0) && td->td_mailbox) {
458 			/*
459 			 * [XXXKSE Future enhancement]
460 			 * We could also go straight back to the syscall
461 			 * if we never had to do an upcall since then.
462 			 * If the KSE's copy is == the thread's copy..
463 			 * AND there are no other completed threads.
464 			 */
465 			/*
466 			 * We will go back as an upcall or go do another thread.
467 			 * Either way we need to save the context back to
468 			 * the user thread mailbox.
469 			 * So the UTS can restart it later.
470 			 */
471 			error = thread_export_context(td);
472 			td->td_mailbox = NULL;
473 			if (error) {
474 				/*
475 				 * Failing to do the KSE
476 				 * operation just defaults operation
477 				 * back to synchonous operation.
478 				 */
479 				goto cont;
480 			}
481 
482 			if (TAILQ_FIRST(&kg->kg_runq)) {
483 				/*
484 				 * Uh-oh.. don't return to the user.
485 				 * Instead, switch to the thread that
486 				 * needs to run. The question is:
487 				 * What do we do with the thread we have now?
488 				 * We have put the completion block
489 				 * on the kse mailbox. If we had more energy,
490 				 * we could lazily do so, assuming someone
491 				 * else might get to userland earlier
492 				 * and deliver it earlier than we could.
493 				 * To do that we could save it off the KSEG.
494 				 * An upcalling KSE would 'reap' all completed
495 				 * threads.
496 				 * Being in a hurry, we'll do nothing and
497 				 * leave it on the current KSE for now.
498 				 *
499 				 * As for the other threads to run;
500 				 * we COULD rush through all the threads
501 				 * in this KSEG at this priority, or we
502 				 * could throw the ball back into the court
503 				 * and just run the highest prio kse available.
504 				 * What is OUR priority?
505 				 * the priority of the highest sycall waiting
506 				 * to be returned?
507 				 * For now, just let another KSE run (easiest).
508 				 */
509 				PROC_LOCK(p);
510 				mtx_lock_spin(&sched_lock);
511 				thread_exit(); /* Abandon current thread. */
512 				/* NOTREACHED */
513 			} else { /* if (number of returning syscalls = 1) */
514 				/*
515 				 * Swap our frame for the upcall frame.
516 				 *
517 				 * XXXKSE Assumes we are going to user land
518 				 * and not nested in the kernel
519 				 */
520 				td->td_flags |= TDF_UPCALLING;
521 			}
522 		}
523 		/*
524 		 * This is NOT just an 'else' clause for the above test...
525 		 */
526 		if (td->td_flags & TDF_UPCALLING) {
527 			CTR3(KTR_PROC, "userret: upcall thread %p (pid %d, %s)",
528 			    td, p->p_pid, p->p_comm);
529 			/*
530 			 * Make sure that it has the correct frame loaded.
531 			 * While we know that we are on the same KSEGRP
532 			 * as we were created on, we could very easily
533 			 * have come in on another KSE. We therefore need
534 			 * to do the copy of the frame after the last
535 			 * possible switch() (the one above).
536 			 */
537 			bcopy(ke->ke_frame, frame, sizeof(struct trapframe));
538 
539 			/*
540 			 * Decide what we are sending to the user
541 			 * upcall sets one argument. The address of the mbox.
542 			 */
543 			cpu_set_args(td, ke);
544 
545 			/*
546 			 * There is no more work to do and we are going to ride
547 			 * this thead/KSE up to userland. Make sure the user's
548 			 * pointer to the thread mailbox is cleared before we
549 			 * re-enter the kernel next time for any reason..
550 			 * We might as well do it here.
551 			 */
552 			td->td_flags &= ~TDF_UPCALLING;	/* Hmmmm. */
553 			error = suword((caddr_t)td->td_kse->ke_mailbox +
554 			    offsetof(struct kse_mailbox, kmbx_current_thread),
555 			    0);
556 		}
557 		/*
558 		 * Stop any chance that we may be separated from
559 		 * the KSE we are currently on. This is "biting the bullet",
560 		 * we are committing to go to user space as as THIS KSE here.
561 		 */
562 cont:
563 		td->td_flags &= ~TDF_UNBOUND;
564 	}
565 	return (error);
566 }
567 
568 /*
569  * Enforce single-threading.
570  *
571  * Returns 1 if the caller must abort (another thread is waiting to
572  * exit the process or similar). Process is locked!
573  * Returns 0 when you are successfully the only thread running.
574  * A process has successfully single threaded in the suspend mode when
575  * There are no threads in user mode. Threads in the kernel must be
576  * allowed to continue until they get to the user boundary. They may even
577  * copy out their return values and data before suspending. They may however be
578  * accellerated in reaching the user boundary as we will wake up
579  * any sleeping threads that are interruptable. (PCATCH).
580  */
581 int
582 thread_single(int force_exit)
583 {
584 	struct thread *td;
585 	struct thread *td2;
586 	struct proc *p;
587 
588 	td = curthread;
589 	p = td->td_proc;
590 	PROC_LOCK_ASSERT(p, MA_OWNED);
591 	KASSERT((td != NULL), ("curthread is NULL"));
592 
593 	if ((p->p_flag & P_KSES) == 0)
594 		return (0);
595 
596 	if (p->p_singlethread) {
597 		/*
598 		 * Someone is already single threading!
599 		 */
600 		return (1);
601 	}
602 
603 	if (force_exit == SNGLE_EXIT)
604 		p->p_flag |= P_SINGLE_EXIT;
605 	else
606 		p->p_flag &= ~P_SINGLE_EXIT;
607 	p->p_flag |= P_STOPPED_SNGL;
608 	p->p_singlethread = td;
609 	while ((p->p_numthreads - p->p_suspcount) != 1) {
610 		FOREACH_THREAD_IN_PROC(p, td2) {
611 			if (td2 == td)
612 				continue;
613 			switch(td2->td_state) {
614 			case TDS_SUSPENDED:
615 				if (force_exit == SNGLE_EXIT) {
616 					TAILQ_REMOVE(&p->p_suspended,
617 					    td, td_runq);
618 					setrunqueue(td); /* Should suicide. */
619 				}
620 			case TDS_SLP:
621 				if (td2->td_flags & TDF_CVWAITQ) {
622 					cv_abort(td2);
623 				} else {
624 					abortsleep(td2);
625 				}
626 				break;
627 			/* etc. XXXKSE */
628 			default:
629 				;
630 			}
631 		}
632 		/*
633 		 * XXXKSE-- idea
634 		 * It's possible that we can just wake up when
635 		 * there are no runnable KSEs, because that would
636 		 * indicate that only this thread is runnable and
637 		 * there are no running KSEs in userland.
638 		 * --
639 		 * Wake us up when everyone else has suspended.
640 		 * (or died)
641 		 */
642 		mtx_lock_spin(&sched_lock);
643 		TAILQ_INSERT_TAIL(&p->p_suspended, td, td_runq);
644 		td->td_state = TDS_SUSPENDED;
645 		p->p_suspcount++;
646 		mtx_unlock(&Giant);
647 		PROC_UNLOCK(p);
648 		mi_switch();
649 		mtx_unlock_spin(&sched_lock);
650 		mtx_lock(&Giant);
651 		PROC_LOCK(p);
652 	}
653 	return (0);
654 }
655 
656 /*
657  * Called in from locations that can safely check to see
658  * whether we have to suspend or at least throttle for a
659  * single-thread event (e.g. fork).
660  *
661  * Such locations include userret().
662  * If the "return_instead" argument is non zero, the thread must be able to
663  * accept 0 (caller may continue), or 1 (caller must abort) as a result.
664  *
665  * The 'return_instead' argument tells the function if it may do a
666  * thread_exit() or suspend, or whether the caller must abort and back
667  * out instead.
668  *
669  * If the thread that set the single_threading request has set the
670  * P_SINGLE_EXIT bit in the process flags then this call will never return
671  * if 'return_instead' is false, but will exit.
672  *
673  * P_SINGLE_EXIT | return_instead == 0| return_instead != 0
674  *---------------+--------------------+---------------------
675  *       0       | returns 0          |   returns 0 or 1
676  *               | when ST ends       |   immediatly
677  *---------------+--------------------+---------------------
678  *       1       | thread exits       |   returns 1
679  *               |                    |  immediatly
680  * 0 = thread_exit() or suspension ok,
681  * other = return error instead of stopping the thread.
682  *
683  * While a full suspension is under effect, even a single threading
684  * thread would be suspended if it made this call (but it shouldn't).
685  * This call should only be made from places where
686  * thread_exit() would be safe as that may be the outcome unless
687  * return_instead is set.
688  */
689 int
690 thread_suspend_check(int return_instead)
691 {
692 	struct thread *td = curthread;
693 	struct proc *p = td->td_proc;
694 
695 	td = curthread;
696 	p = td->td_proc;
697 	PROC_LOCK_ASSERT(p, MA_OWNED);
698 	while (P_SHOULDSTOP(p)) {
699 		if (P_SHOULDSTOP(p) == P_STOPPED_SNGL) {
700 			KASSERT(p->p_singlethread != NULL,
701 			    ("singlethread not set"));
702 
703 			/*
704 			 * The only suspension in action is
705 			 * a single-threading. Treat it ever
706 			 * so slightly different if it is
707 			 * in a special situation.
708 			 */
709 			if (p->p_singlethread == td) {
710 				return (0);	/* Exempt from stopping. */
711 			}
712 
713 		}
714 
715 		if (return_instead) {
716 			return (1);
717 		}
718 
719 		/*
720 		 * If the process is waiting for us to exit,
721 		 * this thread should just suicide.
722 		 * Assumes that P_SINGLE_EXIT implies P_STOPPED_SNGL.
723 		 */
724 		if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) {
725 			mtx_lock_spin(&sched_lock);
726 			while (mtx_owned(&Giant))
727 				mtx_unlock(&Giant);
728 			thread_exit();
729 		}
730 
731 		/*
732 		 * When a thread suspends, it just
733 		 * moves to the processes's suspend queue
734 		 * and stays there.
735 		 *
736 		 * XXXKSE if TDF_BOUND is true
737 		 * it will not release it's KSE which might
738 		 * lead to deadlock if there are not enough KSEs
739 		 * to complete all waiting threads.
740 		 * Maybe be able to 'lend' it out again.
741 		 * (lent kse's can not go back to userland?)
742 		 * and can only be lent in STOPPED state.
743 		 */
744 		mtx_assert(&Giant, MA_NOTOWNED);
745 		mtx_lock_spin(&sched_lock);
746 		p->p_suspcount++;
747 		td->td_state = TDS_SUSPENDED;
748 		TAILQ_INSERT_TAIL(&p->p_suspended, td, td_runq);
749 		PROC_UNLOCK(p);
750 		mi_switch();
751 		mtx_unlock_spin(&sched_lock);
752 		PROC_LOCK(p);
753 	}
754 	return (0);
755 }
756 
757 /*
758  * Allow all threads blocked by single threading to continue running.
759  */
760 void
761 thread_unsuspend(struct proc *p)
762 {
763 	struct thread *td;
764 
765 	PROC_LOCK_ASSERT(p, MA_OWNED);
766 	if (!P_SHOULDSTOP(p)) {
767 		while (( td = TAILQ_FIRST(&p->p_suspended))) {
768 			TAILQ_REMOVE(&p->p_suspended, td, td_runq);
769 			p->p_suspcount--;
770 			setrunqueue(td);
771 		}
772 	} else if ((P_SHOULDSTOP(p) == P_STOPPED_SNGL) &&
773 	    (p->p_numthreads == p->p_suspcount)) {
774 		/*
775 		 * Stopping everything also did the job for the single
776 		 * threading request. Now we've downgraded to single-threaded,
777 		 * let it continue.
778 		 */
779 		TAILQ_REMOVE(&p->p_suspended, p->p_singlethread, td_runq);
780 		p->p_suspcount--;
781 		setrunqueue(p->p_singlethread);
782 	}
783 }
784 
785 void
786 thread_single_end(void)
787 {
788 	struct thread *td;
789 	struct proc *p;
790 
791 	td = curthread;
792 	p = td->td_proc;
793 	PROC_LOCK_ASSERT(p, MA_OWNED);
794 	p->p_flag &= ~P_STOPPED_SNGL;
795 	p->p_singlethread = NULL;
796 	thread_unsuspend(p);
797 }
798 
799