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