]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/kern/kern_thread.c
MFC r248563:
[FreeBSD/stable/8.git] / sys / kern / kern_thread.c
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 "opt_witness.h"
30 #include "opt_kdtrace.h"
31 #include "opt_hwpmc_hooks.h"
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 #include <sys/proc.h>
42 #include <sys/resourcevar.h>
43 #include <sys/sdt.h>
44 #include <sys/smp.h>
45 #include <sys/sched.h>
46 #include <sys/sleepqueue.h>
47 #include <sys/selinfo.h>
48 #include <sys/turnstile.h>
49 #include <sys/ktr.h>
50 #include <sys/umtx.h>
51 #include <sys/cpuset.h>
52 #ifdef  HWPMC_HOOKS
53 #include <sys/pmckern.h>
54 #endif
55
56 #include <security/audit/audit.h>
57
58 #include <vm/vm.h>
59 #include <vm/vm_extern.h>
60 #include <vm/uma.h>
61 #include <sys/eventhandler.h>
62
63 SDT_PROVIDER_DECLARE(proc);
64 SDT_PROBE_DEFINE(proc, , , lwp_exit, lwp-exit);
65
66
67 /*
68  * thread related storage.
69  */
70 static uma_zone_t thread_zone;
71
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 struct mtx tid_lock;
79 static struct unrhdr *tid_unrhdr;
80
81 /*
82  * Prepare a thread for use.
83  */
84 static int
85 thread_ctor(void *mem, int size, void *arg, int flags)
86 {
87         struct thread   *td;
88
89         td = (struct thread *)mem;
90         td->td_state = TDS_INACTIVE;
91         td->td_oncpu = NOCPU;
92
93         td->td_tid = alloc_unr(tid_unrhdr);
94         td->td_syscalls = 0;
95
96         /*
97          * Note that td_critnest begins life as 1 because the thread is not
98          * running and is thereby implicitly waiting to be on the receiving
99          * end of a context switch.
100          */
101         td->td_critnest = 1;
102         EVENTHANDLER_INVOKE(thread_ctor, td);
103 #ifdef AUDIT
104         audit_thread_alloc(td);
105 #endif
106         umtx_thread_alloc(td);
107         return (0);
108 }
109
110 /*
111  * Reclaim a thread after use.
112  */
113 static void
114 thread_dtor(void *mem, int size, void *arg)
115 {
116         struct thread *td;
117
118         td = (struct thread *)mem;
119
120 #ifdef INVARIANTS
121         /* Verify that this thread is in a safe state to free. */
122         switch (td->td_state) {
123         case TDS_INHIBITED:
124         case TDS_RUNNING:
125         case TDS_CAN_RUN:
126         case TDS_RUNQ:
127                 /*
128                  * We must never unlink a thread that is in one of
129                  * these states, because it is currently active.
130                  */
131                 panic("bad state for thread unlinking");
132                 /* NOTREACHED */
133         case TDS_INACTIVE:
134                 break;
135         default:
136                 panic("bad thread state");
137                 /* NOTREACHED */
138         }
139 #endif
140 #ifdef AUDIT
141         audit_thread_free(td);
142 #endif
143         /* Free all OSD associated to this thread. */
144         osd_thread_exit(td);
145
146         EVENTHANDLER_INVOKE(thread_dtor, td);
147         free_unr(tid_unrhdr, td->td_tid);
148 }
149
150 /*
151  * Initialize type-stable parts of a thread (when newly created).
152  */
153 static int
154 thread_init(void *mem, int size, int flags)
155 {
156         struct thread *td;
157
158         td = (struct thread *)mem;
159
160         td->td_sleepqueue = sleepq_alloc();
161         td->td_turnstile = turnstile_alloc();
162         EVENTHANDLER_INVOKE(thread_init, td);
163         td->td_sched = (struct td_sched *)&td[1];
164         umtx_thread_init(td);
165         td->td_kstack = 0;
166         return (0);
167 }
168
169 /*
170  * Tear down type-stable parts of a thread (just before being discarded).
171  */
172 static void
173 thread_fini(void *mem, int size)
174 {
175         struct thread *td;
176
177         td = (struct thread *)mem;
178         EVENTHANDLER_INVOKE(thread_fini, td);
179         turnstile_free(td->td_turnstile);
180         sleepq_free(td->td_sleepqueue);
181         umtx_thread_fini(td);
182         seltdfini(td);
183 }
184
185 /*
186  * For a newly created process,
187  * link up all the structures and its initial threads etc.
188  * called from:
189  * {arch}/{arch}/machdep.c   ia64_init(), init386() etc.
190  * proc_dtor() (should go away)
191  * proc_init()
192  */
193 void
194 proc_linkup0(struct proc *p, struct thread *td)
195 {
196         TAILQ_INIT(&p->p_threads);           /* all threads in proc */
197         proc_linkup(p, td);
198 }
199
200 void
201 proc_linkup(struct proc *p, struct thread *td)
202 {
203
204         sigqueue_init(&p->p_sigqueue, p);
205         p->p_ksi = ksiginfo_alloc(1);
206         if (p->p_ksi != NULL) {
207                 /* XXX p_ksi may be null if ksiginfo zone is not ready */
208                 p->p_ksi->ksi_flags = KSI_EXT | KSI_INS;
209         }
210         LIST_INIT(&p->p_mqnotifier);
211         p->p_numthreads = 0;
212         thread_link(td, p);
213 }
214
215 /*
216  * Initialize global thread allocation resources.
217  */
218 void
219 threadinit(void)
220 {
221
222         mtx_init(&tid_lock, "TID lock", NULL, MTX_DEF);
223         /* leave one number for thread0 */
224         tid_unrhdr = new_unrhdr(PID_MAX + 2, INT_MAX, &tid_lock);
225
226         thread_zone = uma_zcreate("THREAD", sched_sizeof_thread(),
227             thread_ctor, thread_dtor, thread_init, thread_fini,
228             16 - 1, 0);
229 }
230
231 /*
232  * Place an unused thread on the zombie list.
233  * Use the slpq as that must be unused by now.
234  */
235 void
236 thread_zombie(struct thread *td)
237 {
238         mtx_lock_spin(&zombie_lock);
239         TAILQ_INSERT_HEAD(&zombie_threads, td, td_slpq);
240         mtx_unlock_spin(&zombie_lock);
241 }
242
243 /*
244  * Release a thread that has exited after cpu_throw().
245  */
246 void
247 thread_stash(struct thread *td)
248 {
249         atomic_subtract_rel_int(&td->td_proc->p_exitthreads, 1);
250         thread_zombie(td);
251 }
252
253 /*
254  * Reap zombie resources.
255  */
256 void
257 thread_reap(void)
258 {
259         struct thread *td_first, *td_next;
260
261         /*
262          * Don't even bother to lock if none at this instant,
263          * we really don't care about the next instant..
264          */
265         if (!TAILQ_EMPTY(&zombie_threads)) {
266                 mtx_lock_spin(&zombie_lock);
267                 td_first = TAILQ_FIRST(&zombie_threads);
268                 if (td_first)
269                         TAILQ_INIT(&zombie_threads);
270                 mtx_unlock_spin(&zombie_lock);
271                 while (td_first) {
272                         td_next = TAILQ_NEXT(td_first, td_slpq);
273                         if (td_first->td_ucred)
274                                 crfree(td_first->td_ucred);
275                         thread_free(td_first);
276                         td_first = td_next;
277                 }
278         }
279 }
280
281 /*
282  * Allocate a thread.
283  */
284 struct thread *
285 thread_alloc(int pages)
286 {
287         struct thread *td;
288
289         thread_reap(); /* check if any zombies to get */
290
291         td = (struct thread *)uma_zalloc(thread_zone, M_WAITOK);
292         KASSERT(td->td_kstack == 0, ("thread_alloc got thread with kstack"));
293         if (!vm_thread_new(td, pages)) {
294                 uma_zfree(thread_zone, td);
295                 return (NULL);
296         }
297         cpu_thread_alloc(td);
298         return (td);
299 }
300
301 int
302 thread_alloc_stack(struct thread *td, int pages)
303 {
304
305         KASSERT(td->td_kstack == 0,
306             ("thread_alloc_stack called on a thread with kstack"));
307         if (!vm_thread_new(td, pages))
308                 return (0);
309         cpu_thread_alloc(td);
310         return (1);
311 }
312
313 /*
314  * Deallocate a thread.
315  */
316 void
317 thread_free(struct thread *td)
318 {
319
320         lock_profile_thread_exit(td);
321         if (td->td_cpuset)
322                 cpuset_rel(td->td_cpuset);
323         td->td_cpuset = NULL;
324         cpu_thread_free(td);
325         if (td->td_kstack != 0)
326                 vm_thread_dispose(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().
338  */
339 void
340 thread_exit(void)
341 {
342         uint64_t runtime, new_switchtime;
343         struct thread *td;
344         struct thread *td2;
345         struct proc *p;
346         int wakeup_swapper;
347
348         td = curthread;
349         p = td->td_proc;
350
351         PROC_SLOCK_ASSERT(p, MA_OWNED);
352         mtx_assert(&Giant, MA_NOTOWNED);
353
354         PROC_LOCK_ASSERT(p, MA_OWNED);
355         KASSERT(p != NULL, ("thread exiting without a process"));
356         CTR3(KTR_PROC, "thread_exit: thread %p (pid %ld, %s)", td,
357             (long)p->p_pid, td->td_name);
358         KASSERT(TAILQ_EMPTY(&td->td_sigqueue.sq_list), ("signal pending"));
359
360 #ifdef AUDIT
361         AUDIT_SYSCALL_EXIT(0, td);
362 #endif
363         umtx_thread_exit(td);
364         /*
365          * drop FPU & debug register state storage, or any other
366          * architecture specific resources that
367          * would not be on a new untouched process.
368          */
369         cpu_thread_exit(td);    /* XXXSMP */
370
371         /*
372          * The last thread is left attached to the process
373          * So that the whole bundle gets recycled. Skip
374          * all this stuff if we never had threads.
375          * EXIT clears all sign of other threads when
376          * it goes to single threading, so the last thread always
377          * takes the short path.
378          */
379         if (p->p_flag & P_HADTHREADS) {
380                 if (p->p_numthreads > 1) {
381                         thread_unlink(td);
382                         td2 = FIRST_THREAD_IN_PROC(p);
383                         sched_exit_thread(td2, td);
384
385                         /*
386                          * The test below is NOT true if we are the
387                          * sole exiting thread. P_STOPPED_SINGLE is unset
388                          * in exit1() after it is the only survivor.
389                          */
390                         if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
391                                 if (p->p_numthreads == p->p_suspcount) {
392                                         thread_lock(p->p_singlethread);
393                                         wakeup_swapper = thread_unsuspend_one(
394                                                 p->p_singlethread);
395                                         thread_unlock(p->p_singlethread);
396                                         if (wakeup_swapper)
397                                                 kick_proc0();
398                                 }
399                         }
400
401                         atomic_add_int(&td->td_proc->p_exitthreads, 1);
402                         PCPU_SET(deadthread, td);
403                 } else {
404                         /*
405                          * The last thread is exiting.. but not through exit()
406                          */
407                         panic ("thread_exit: Last thread exiting on its own");
408                 }
409         } 
410 #ifdef  HWPMC_HOOKS
411         /*
412          * If this thread is part of a process that is being tracked by hwpmc(4),
413          * inform the module of the thread's impending exit.
414          */
415         if (PMC_PROC_IS_USING_PMCS(td->td_proc))
416                 PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT);
417 #endif
418         PROC_UNLOCK(p);
419
420         /* Do the same timestamp bookkeeping that mi_switch() would do. */
421         new_switchtime = cpu_ticks();
422         runtime = new_switchtime - PCPU_GET(switchtime);
423         td->td_runtime += runtime;
424         td->td_incruntime += runtime;
425         PCPU_SET(switchtime, new_switchtime);
426         PCPU_SET(switchticks, ticks);
427         PCPU_INC(cnt.v_swtch);
428
429         /* Save our resource usage in our process. */
430         td->td_ru.ru_nvcsw++;
431         ruxagg(p, td);
432         rucollect(&p->p_ru, &td->td_ru);
433
434         thread_lock(td);
435         PROC_SUNLOCK(p);
436         td->td_state = TDS_INACTIVE;
437 #ifdef WITNESS
438         witness_thread_exit(td);
439 #endif
440         CTR1(KTR_PROC, "thread_exit: cpu_throw() thread %p", td);
441         sched_throw(td);
442         panic("I'm a teapot!");
443         /* NOTREACHED */
444 }
445
446 /*
447  * Do any thread specific cleanups that may be needed in wait()
448  * called with Giant, proc and schedlock not held.
449  */
450 void
451 thread_wait(struct proc *p)
452 {
453         struct thread *td;
454
455         mtx_assert(&Giant, MA_NOTOWNED);
456         KASSERT((p->p_numthreads == 1), ("Multiple threads in wait1()"));
457         td = FIRST_THREAD_IN_PROC(p);
458         /* Lock the last thread so we spin until it exits cpu_throw(). */
459         thread_lock(td);
460         thread_unlock(td);
461         /* Wait for any remaining threads to exit cpu_throw(). */
462         while (p->p_exitthreads)
463                 sched_relinquish(curthread);
464         lock_profile_thread_exit(td);
465         cpuset_rel(td->td_cpuset);
466         td->td_cpuset = NULL;
467         cpu_thread_clean(td);
468         crfree(td->td_ucred);
469         thread_reap();  /* check for zombie threads etc. */
470 }
471
472 /*
473  * Link a thread to a process.
474  * set up anything that needs to be initialized for it to
475  * be used by the process.
476  */
477 void
478 thread_link(struct thread *td, struct proc *p)
479 {
480
481         /*
482          * XXX This can't be enabled because it's called for proc0 before
483          * its lock has been created.
484          * PROC_LOCK_ASSERT(p, MA_OWNED);
485          */
486         td->td_state    = TDS_INACTIVE;
487         td->td_proc     = p;
488         td->td_flags    = TDF_INMEM;
489
490         LIST_INIT(&td->td_contested);
491         LIST_INIT(&td->td_lprof[0]);
492         LIST_INIT(&td->td_lprof[1]);
493         sigqueue_init(&td->td_sigqueue, p);
494         callout_init(&td->td_slpcallout, CALLOUT_MPSAFE);
495         TAILQ_INSERT_HEAD(&p->p_threads, td, td_plist);
496         p->p_numthreads++;
497 }
498
499 /*
500  * Convert a process with one thread to an unthreaded process.
501  */
502 void
503 thread_unthread(struct thread *td)
504 {
505         struct proc *p = td->td_proc;
506
507         KASSERT((p->p_numthreads == 1), ("Unthreading with >1 threads"));
508         p->p_flag &= ~P_HADTHREADS;
509 }
510
511 /*
512  * Called from:
513  *  thread_exit()
514  */
515 void
516 thread_unlink(struct thread *td)
517 {
518         struct proc *p = td->td_proc;
519
520         PROC_LOCK_ASSERT(p, MA_OWNED);
521         TAILQ_REMOVE(&p->p_threads, td, td_plist);
522         p->p_numthreads--;
523         /* could clear a few other things here */
524         /* Must  NOT clear links to proc! */
525 }
526
527 static int
528 calc_remaining(struct proc *p, int mode)
529 {
530         int remaining;
531
532         PROC_LOCK_ASSERT(p, MA_OWNED);
533         PROC_SLOCK_ASSERT(p, MA_OWNED);
534         if (mode == SINGLE_EXIT)
535                 remaining = p->p_numthreads;
536         else if (mode == SINGLE_BOUNDARY)
537                 remaining = p->p_numthreads - p->p_boundary_count;
538         else if (mode == SINGLE_NO_EXIT)
539                 remaining = p->p_numthreads - p->p_suspcount;
540         else
541                 panic("calc_remaining: wrong mode %d", mode);
542         return (remaining);
543 }
544
545 /*
546  * Enforce single-threading.
547  *
548  * Returns 1 if the caller must abort (another thread is waiting to
549  * exit the process or similar). Process is locked!
550  * Returns 0 when you are successfully the only thread running.
551  * A process has successfully single threaded in the suspend mode when
552  * There are no threads in user mode. Threads in the kernel must be
553  * allowed to continue until they get to the user boundary. They may even
554  * copy out their return values and data before suspending. They may however be
555  * accelerated in reaching the user boundary as we will wake up
556  * any sleeping threads that are interruptable. (PCATCH).
557  */
558 int
559 thread_single(int mode)
560 {
561         struct thread *td;
562         struct thread *td2;
563         struct proc *p;
564         int remaining, wakeup_swapper;
565
566         td = curthread;
567         p = td->td_proc;
568         mtx_assert(&Giant, MA_NOTOWNED);
569         PROC_LOCK_ASSERT(p, MA_OWNED);
570         KASSERT((td != NULL), ("curthread is NULL"));
571
572         if ((p->p_flag & P_HADTHREADS) == 0)
573                 return (0);
574
575         /* Is someone already single threading? */
576         if (p->p_singlethread != NULL && p->p_singlethread != td)
577                 return (1);
578
579         if (mode == SINGLE_EXIT) {
580                 p->p_flag |= P_SINGLE_EXIT;
581                 p->p_flag &= ~P_SINGLE_BOUNDARY;
582         } else {
583                 p->p_flag &= ~P_SINGLE_EXIT;
584                 if (mode == SINGLE_BOUNDARY)
585                         p->p_flag |= P_SINGLE_BOUNDARY;
586                 else
587                         p->p_flag &= ~P_SINGLE_BOUNDARY;
588         }
589         p->p_flag |= P_STOPPED_SINGLE;
590         PROC_SLOCK(p);
591         p->p_singlethread = td;
592         remaining = calc_remaining(p, mode);
593         while (remaining != 1) {
594                 if (P_SHOULDSTOP(p) != P_STOPPED_SINGLE)
595                         goto stopme;
596                 wakeup_swapper = 0;
597                 FOREACH_THREAD_IN_PROC(p, td2) {
598                         if (td2 == td)
599                                 continue;
600                         thread_lock(td2);
601                         td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
602                         if (TD_IS_INHIBITED(td2)) {
603                                 switch (mode) {
604                                 case SINGLE_EXIT:
605                                         if (TD_IS_SUSPENDED(td2))
606                                                 wakeup_swapper |=
607                                                     thread_unsuspend_one(td2);
608                                         if (TD_ON_SLEEPQ(td2) &&
609                                             (td2->td_flags & TDF_SINTR))
610                                                 wakeup_swapper |=
611                                                     sleepq_abort(td2, EINTR);
612                                         break;
613                                 case SINGLE_BOUNDARY:
614                                         if (TD_IS_SUSPENDED(td2) &&
615                                             !(td2->td_flags & TDF_BOUNDARY))
616                                                 wakeup_swapper |=
617                                                     thread_unsuspend_one(td2);
618                                         if (TD_ON_SLEEPQ(td2) &&
619                                             (td2->td_flags & TDF_SINTR))
620                                                 wakeup_swapper |=
621                                                     sleepq_abort(td2, ERESTART);
622                                         break;
623                                 case SINGLE_NO_EXIT:
624                                         if (TD_IS_SUSPENDED(td2) &&
625                                             !(td2->td_flags & TDF_BOUNDARY))
626                                                 wakeup_swapper |=
627                                                     thread_unsuspend_one(td2);
628                                         if (TD_ON_SLEEPQ(td2) &&
629                                             (td2->td_flags & TDF_SINTR))
630                                                 wakeup_swapper |=
631                                                     sleepq_abort(td2, ERESTART);
632                                         break;
633                                 default:
634                                         break;
635                                 }
636                         }
637 #ifdef SMP
638                         else if (TD_IS_RUNNING(td2) && td != td2) {
639                                 forward_signal(td2);
640                         }
641 #endif
642                         thread_unlock(td2);
643                 }
644                 if (wakeup_swapper)
645                         kick_proc0();
646                 remaining = calc_remaining(p, mode);
647
648                 /*
649                  * Maybe we suspended some threads.. was it enough?
650                  */
651                 if (remaining == 1)
652                         break;
653
654 stopme:
655                 /*
656                  * Wake us up when everyone else has suspended.
657                  * In the mean time we suspend as well.
658                  */
659                 thread_suspend_switch(td);
660                 remaining = calc_remaining(p, mode);
661         }
662         if (mode == SINGLE_EXIT) {
663                 /*
664                  * We have gotten rid of all the other threads and we
665                  * are about to either exit or exec. In either case,
666                  * we try our utmost  to revert to being a non-threaded
667                  * process.
668                  */
669                 p->p_singlethread = NULL;
670                 p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT);
671                 thread_unthread(td);
672         }
673         PROC_SUNLOCK(p);
674         return (0);
675 }
676
677 /*
678  * Called in from locations that can safely check to see
679  * whether we have to suspend or at least throttle for a
680  * single-thread event (e.g. fork).
681  *
682  * Such locations include userret().
683  * If the "return_instead" argument is non zero, the thread must be able to
684  * accept 0 (caller may continue), or 1 (caller must abort) as a result.
685  *
686  * The 'return_instead' argument tells the function if it may do a
687  * thread_exit() or suspend, or whether the caller must abort and back
688  * out instead.
689  *
690  * If the thread that set the single_threading request has set the
691  * P_SINGLE_EXIT bit in the process flags then this call will never return
692  * if 'return_instead' is false, but will exit.
693  *
694  * P_SINGLE_EXIT | return_instead == 0| return_instead != 0
695  *---------------+--------------------+---------------------
696  *       0       | returns 0          |   returns 0 or 1
697  *               | when ST ends       |   immediatly
698  *---------------+--------------------+---------------------
699  *       1       | thread exits       |   returns 1
700  *               |                    |  immediatly
701  * 0 = thread_exit() or suspension ok,
702  * other = return error instead of stopping the thread.
703  *
704  * While a full suspension is under effect, even a single threading
705  * thread would be suspended if it made this call (but it shouldn't).
706  * This call should only be made from places where
707  * thread_exit() would be safe as that may be the outcome unless
708  * return_instead is set.
709  */
710 int
711 thread_suspend_check(int return_instead)
712 {
713         struct thread *td;
714         struct proc *p;
715         int wakeup_swapper;
716
717         td = curthread;
718         p = td->td_proc;
719         mtx_assert(&Giant, MA_NOTOWNED);
720         PROC_LOCK_ASSERT(p, MA_OWNED);
721         while (P_SHOULDSTOP(p) ||
722               ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_SUSPEND))) {
723                 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
724                         KASSERT(p->p_singlethread != NULL,
725                             ("singlethread not set"));
726                         /*
727                          * The only suspension in action is a
728                          * single-threading. Single threader need not stop.
729                          * XXX Should be safe to access unlocked
730                          * as it can only be set to be true by us.
731                          */
732                         if (p->p_singlethread == td)
733                                 return (0);     /* Exempt from stopping. */
734                 }
735                 if ((p->p_flag & P_SINGLE_EXIT) && return_instead)
736                         return (EINTR);
737
738                 /* Should we goto user boundary if we didn't come from there? */
739                 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE &&
740                     (p->p_flag & P_SINGLE_BOUNDARY) && return_instead)
741                         return (ERESTART);
742
743                 /* If thread will exit, flush its pending signals */
744                 if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td))
745                         sigqueue_flush(&td->td_sigqueue);
746
747                 PROC_SLOCK(p);
748                 thread_stopped(p);
749                 /*
750                  * If the process is waiting for us to exit,
751                  * this thread should just suicide.
752                  * Assumes that P_SINGLE_EXIT implies P_STOPPED_SINGLE.
753                  */
754                 if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td))
755                         thread_exit();
756                 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
757                         if (p->p_numthreads == p->p_suspcount + 1) {
758                                 thread_lock(p->p_singlethread);
759                                 wakeup_swapper =
760                                     thread_unsuspend_one(p->p_singlethread);
761                                 thread_unlock(p->p_singlethread);
762                                 if (wakeup_swapper)
763                                         kick_proc0();
764                         }
765                 }
766                 PROC_UNLOCK(p);
767                 thread_lock(td);
768                 /*
769                  * When a thread suspends, it just
770                  * gets taken off all queues.
771                  */
772                 thread_suspend_one(td);
773                 if (return_instead == 0) {
774                         p->p_boundary_count++;
775                         td->td_flags |= TDF_BOUNDARY;
776                 }
777                 PROC_SUNLOCK(p);
778                 mi_switch(SW_INVOL | SWT_SUSPEND, NULL);
779                 if (return_instead == 0)
780                         td->td_flags &= ~TDF_BOUNDARY;
781                 thread_unlock(td);
782                 PROC_LOCK(p);
783                 if (return_instead == 0) {
784                         PROC_SLOCK(p);
785                         p->p_boundary_count--;
786                         PROC_SUNLOCK(p);
787                 }
788         }
789         return (0);
790 }
791
792 void
793 thread_suspend_switch(struct thread *td)
794 {
795         struct proc *p;
796
797         p = td->td_proc;
798         KASSERT(!TD_IS_SUSPENDED(td), ("already suspended"));
799         PROC_LOCK_ASSERT(p, MA_OWNED);
800         PROC_SLOCK_ASSERT(p, MA_OWNED);
801         /*
802          * We implement thread_suspend_one in stages here to avoid
803          * dropping the proc lock while the thread lock is owned.
804          */
805         thread_stopped(p);
806         p->p_suspcount++;
807         PROC_UNLOCK(p);
808         thread_lock(td);
809         td->td_flags &= ~TDF_NEEDSUSPCHK;
810         TD_SET_SUSPENDED(td);
811         sched_sleep(td, 0);
812         PROC_SUNLOCK(p);
813         DROP_GIANT();
814         mi_switch(SW_VOL | SWT_SUSPEND, NULL);
815         thread_unlock(td);
816         PICKUP_GIANT();
817         PROC_LOCK(p);
818         PROC_SLOCK(p);
819 }
820
821 void
822 thread_suspend_one(struct thread *td)
823 {
824         struct proc *p = td->td_proc;
825
826         PROC_SLOCK_ASSERT(p, MA_OWNED);
827         THREAD_LOCK_ASSERT(td, MA_OWNED);
828         KASSERT(!TD_IS_SUSPENDED(td), ("already suspended"));
829         p->p_suspcount++;
830         td->td_flags &= ~TDF_NEEDSUSPCHK;
831         TD_SET_SUSPENDED(td);
832         sched_sleep(td, 0);
833 }
834
835 int
836 thread_unsuspend_one(struct thread *td)
837 {
838         struct proc *p = td->td_proc;
839
840         PROC_SLOCK_ASSERT(p, MA_OWNED);
841         THREAD_LOCK_ASSERT(td, MA_OWNED);
842         KASSERT(TD_IS_SUSPENDED(td), ("Thread not suspended"));
843         TD_CLR_SUSPENDED(td);
844         p->p_suspcount--;
845         return (setrunnable(td));
846 }
847
848 /*
849  * Allow all threads blocked by single threading to continue running.
850  */
851 void
852 thread_unsuspend(struct proc *p)
853 {
854         struct thread *td;
855         int wakeup_swapper;
856
857         PROC_LOCK_ASSERT(p, MA_OWNED);
858         PROC_SLOCK_ASSERT(p, MA_OWNED);
859         wakeup_swapper = 0;
860         if (!P_SHOULDSTOP(p)) {
861                 FOREACH_THREAD_IN_PROC(p, td) {
862                         thread_lock(td);
863                         if (TD_IS_SUSPENDED(td)) {
864                                 wakeup_swapper |= thread_unsuspend_one(td);
865                         }
866                         thread_unlock(td);
867                 }
868         } else if ((P_SHOULDSTOP(p) == P_STOPPED_SINGLE) &&
869             (p->p_numthreads == p->p_suspcount)) {
870                 /*
871                  * Stopping everything also did the job for the single
872                  * threading request. Now we've downgraded to single-threaded,
873                  * let it continue.
874                  */
875                 thread_lock(p->p_singlethread);
876                 wakeup_swapper = thread_unsuspend_one(p->p_singlethread);
877                 thread_unlock(p->p_singlethread);
878         }
879         if (wakeup_swapper)
880                 kick_proc0();
881 }
882
883 /*
884  * End the single threading mode..
885  */
886 void
887 thread_single_end(void)
888 {
889         struct thread *td;
890         struct proc *p;
891         int wakeup_swapper;
892
893         td = curthread;
894         p = td->td_proc;
895         PROC_LOCK_ASSERT(p, MA_OWNED);
896         p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT | P_SINGLE_BOUNDARY);
897         PROC_SLOCK(p);
898         p->p_singlethread = NULL;
899         wakeup_swapper = 0;
900         /*
901          * If there are other threads they may now run,
902          * unless of course there is a blanket 'stop order'
903          * on the process. The single threader must be allowed
904          * to continue however as this is a bad place to stop.
905          */
906         if ((p->p_numthreads != 1) && (!P_SHOULDSTOP(p))) {
907                 FOREACH_THREAD_IN_PROC(p, td) {
908                         thread_lock(td);
909                         if (TD_IS_SUSPENDED(td)) {
910                                 wakeup_swapper |= thread_unsuspend_one(td);
911                         }
912                         thread_unlock(td);
913                 }
914         }
915         PROC_SUNLOCK(p);
916         if (wakeup_swapper)
917                 kick_proc0();
918 }
919
920 struct thread *
921 thread_find(struct proc *p, lwpid_t tid)
922 {
923         struct thread *td;
924
925         PROC_LOCK_ASSERT(p, MA_OWNED);
926         FOREACH_THREAD_IN_PROC(p, td) {
927                 if (td->td_tid == tid)
928                         break;
929         }
930         return (td);
931 }