19454b2d8SWarner Losh /*- 244990b8cSJulian Elischer * Copyright (C) 2001 Julian Elischer <julian@freebsd.org>. 344990b8cSJulian Elischer * All rights reserved. 444990b8cSJulian Elischer * 544990b8cSJulian Elischer * Redistribution and use in source and binary forms, with or without 644990b8cSJulian Elischer * modification, are permitted provided that the following conditions 744990b8cSJulian Elischer * are met: 844990b8cSJulian Elischer * 1. Redistributions of source code must retain the above copyright 944990b8cSJulian Elischer * notice(s), this list of conditions and the following disclaimer as 1044990b8cSJulian Elischer * the first lines of this file unmodified other than the possible 1144990b8cSJulian Elischer * addition of one or more copyright notices. 1244990b8cSJulian Elischer * 2. Redistributions in binary form must reproduce the above copyright 1344990b8cSJulian Elischer * notice(s), this list of conditions and the following disclaimer in the 1444990b8cSJulian Elischer * documentation and/or other materials provided with the distribution. 1544990b8cSJulian Elischer * 1644990b8cSJulian Elischer * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY 1744990b8cSJulian Elischer * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 1844990b8cSJulian Elischer * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 1944990b8cSJulian Elischer * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY 2044990b8cSJulian Elischer * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 2144990b8cSJulian Elischer * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 2244990b8cSJulian Elischer * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 2344990b8cSJulian Elischer * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2444990b8cSJulian Elischer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2544990b8cSJulian Elischer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 2644990b8cSJulian Elischer * DAMAGE. 2744990b8cSJulian Elischer */ 2844990b8cSJulian Elischer 293d06b4b3SAttilio Rao #include "opt_witness.h" 303d06b4b3SAttilio Rao 31677b542eSDavid E. O'Brien #include <sys/cdefs.h> 32677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 33677b542eSDavid E. O'Brien 3444990b8cSJulian Elischer #include <sys/param.h> 3544990b8cSJulian Elischer #include <sys/systm.h> 3644990b8cSJulian Elischer #include <sys/kernel.h> 3744990b8cSJulian Elischer #include <sys/lock.h> 3844990b8cSJulian Elischer #include <sys/mutex.h> 3944990b8cSJulian Elischer #include <sys/proc.h> 40e170bfdaSDavid Xu #include <sys/resourcevar.h> 4194e0a4cdSJulian Elischer #include <sys/smp.h> 4244990b8cSJulian Elischer #include <sys/sysctl.h> 43de028f5aSJeff Roberson #include <sys/sched.h> 4444f3b092SJohn Baldwin #include <sys/sleepqueue.h> 45ace8398dSJeff Roberson #include <sys/selinfo.h> 46961a7b24SJohn Baldwin #include <sys/turnstile.h> 4744990b8cSJulian Elischer #include <sys/ktr.h> 48bc8e6d81SDavid Xu #include <sys/umtx.h> 49d7f687fcSJeff Roberson #include <sys/cpuset.h> 5044990b8cSJulian Elischer 51911b84b0SRobert Watson #include <security/audit/audit.h> 52911b84b0SRobert Watson 5344990b8cSJulian Elischer #include <vm/vm.h> 5449a2507bSAlan Cox #include <vm/vm_extern.h> 5544990b8cSJulian Elischer #include <vm/uma.h> 56b209f889SRandall Stewart #include <sys/eventhandler.h> 5702fb42b0SPeter Wemm 588460a577SJohn Birrell /* 598460a577SJohn Birrell * thread related storage. 608460a577SJohn Birrell */ 6144990b8cSJulian Elischer static uma_zone_t thread_zone; 6244990b8cSJulian Elischer 6344990b8cSJulian Elischer SYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW, 0, "thread allocation"); 64fdc5ecd2SDavid Xu 65345ad866SJulian Elischer int max_threads_per_proc = 1500; 66fdc5ecd2SDavid Xu SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_per_proc, CTLFLAG_RW, 674f0db5e0SJulian Elischer &max_threads_per_proc, 0, "Limit on threads per proc"); 684f0db5e0SJulian Elischer 69345ad866SJulian Elischer int max_threads_hits; 700252d203SDavid Xu SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_hits, CTLFLAG_RD, 710252d203SDavid Xu &max_threads_hits, 0, ""); 720252d203SDavid Xu 735215b187SJeff Roberson TAILQ_HEAD(, thread) zombie_threads = TAILQ_HEAD_INITIALIZER(zombie_threads); 74c8790f5dSAttilio Rao static struct mtx zombie_lock; 75a54e85fdSJeff Roberson MTX_SYSINIT(zombie_lock, &zombie_lock, "zombie lock", MTX_SPIN); 7644990b8cSJulian Elischer 77ff8fbcffSJeff Roberson static void thread_zombie(struct thread *); 78ff8fbcffSJeff Roberson 79fdcac928SMarcel Moolenaar struct mtx tid_lock; 801ea7a6f8SPoul-Henning Kamp static struct unrhdr *tid_unrhdr; 81fdcac928SMarcel Moolenaar 82fdcac928SMarcel Moolenaar /* 83696058c3SJulian Elischer * Prepare a thread for use. 8444990b8cSJulian Elischer */ 85b23f72e9SBrian Feldman static int 86b23f72e9SBrian Feldman thread_ctor(void *mem, int size, void *arg, int flags) 8744990b8cSJulian Elischer { 8844990b8cSJulian Elischer struct thread *td; 8944990b8cSJulian Elischer 9044990b8cSJulian Elischer td = (struct thread *)mem; 9171fad9fdSJulian Elischer td->td_state = TDS_INACTIVE; 92060563ecSJulian Elischer td->td_oncpu = NOCPU; 936c27c603SJuli Mallett 94773eff9dSPoul-Henning Kamp td->td_tid = alloc_unr(tid_unrhdr); 95f9bb7538SMohan Srinivasan td->td_syscalls = 0; 96773eff9dSPoul-Henning Kamp 976c27c603SJuli Mallett /* 986c27c603SJuli Mallett * Note that td_critnest begins life as 1 because the thread is not 996c27c603SJuli Mallett * running and is thereby implicitly waiting to be on the receiving 100a54e85fdSJeff Roberson * end of a context switch. 1016c27c603SJuli Mallett */ 102139b7550SJohn Baldwin td->td_critnest = 1; 103b209f889SRandall Stewart EVENTHANDLER_INVOKE(thread_ctor, td); 104911b84b0SRobert Watson #ifdef AUDIT 105911b84b0SRobert Watson audit_thread_alloc(td); 106911b84b0SRobert Watson #endif 107d10183d9SDavid Xu umtx_thread_alloc(td); 108b23f72e9SBrian Feldman return (0); 10944990b8cSJulian Elischer } 11044990b8cSJulian Elischer 11144990b8cSJulian Elischer /* 11244990b8cSJulian Elischer * Reclaim a thread after use. 11344990b8cSJulian Elischer */ 11444990b8cSJulian Elischer static void 11544990b8cSJulian Elischer thread_dtor(void *mem, int size, void *arg) 11644990b8cSJulian Elischer { 11744990b8cSJulian Elischer struct thread *td; 11844990b8cSJulian Elischer 11944990b8cSJulian Elischer td = (struct thread *)mem; 12044990b8cSJulian Elischer 12144990b8cSJulian Elischer #ifdef INVARIANTS 12244990b8cSJulian Elischer /* Verify that this thread is in a safe state to free. */ 12344990b8cSJulian Elischer switch (td->td_state) { 12471fad9fdSJulian Elischer case TDS_INHIBITED: 12571fad9fdSJulian Elischer case TDS_RUNNING: 12671fad9fdSJulian Elischer case TDS_CAN_RUN: 12744990b8cSJulian Elischer case TDS_RUNQ: 12844990b8cSJulian Elischer /* 12944990b8cSJulian Elischer * We must never unlink a thread that is in one of 13044990b8cSJulian Elischer * these states, because it is currently active. 13144990b8cSJulian Elischer */ 13244990b8cSJulian Elischer panic("bad state for thread unlinking"); 13344990b8cSJulian Elischer /* NOTREACHED */ 13471fad9fdSJulian Elischer case TDS_INACTIVE: 13544990b8cSJulian Elischer break; 13644990b8cSJulian Elischer default: 13744990b8cSJulian Elischer panic("bad thread state"); 13844990b8cSJulian Elischer /* NOTREACHED */ 13944990b8cSJulian Elischer } 14044990b8cSJulian Elischer #endif 1416e8525ceSRobert Watson #ifdef AUDIT 1426e8525ceSRobert Watson audit_thread_free(td); 1436e8525ceSRobert Watson #endif 1441ba4a712SPawel Jakub Dawidek /* Free all OSD associated to this thread. */ 1451ba4a712SPawel Jakub Dawidek osd_thread_exit(td); 1461ba4a712SPawel Jakub Dawidek 147b209f889SRandall Stewart EVENTHANDLER_INVOKE(thread_dtor, td); 148773eff9dSPoul-Henning Kamp free_unr(tid_unrhdr, td->td_tid); 14944990b8cSJulian Elischer } 15044990b8cSJulian Elischer 15144990b8cSJulian Elischer /* 15244990b8cSJulian Elischer * Initialize type-stable parts of a thread (when newly created). 15344990b8cSJulian Elischer */ 154b23f72e9SBrian Feldman static int 155b23f72e9SBrian Feldman thread_init(void *mem, int size, int flags) 15644990b8cSJulian Elischer { 15744990b8cSJulian Elischer struct thread *td; 15844990b8cSJulian Elischer 15944990b8cSJulian Elischer td = (struct thread *)mem; 160247aba24SMarcel Moolenaar 16144f3b092SJohn Baldwin td->td_sleepqueue = sleepq_alloc(); 162961a7b24SJohn Baldwin td->td_turnstile = turnstile_alloc(); 163b209f889SRandall Stewart EVENTHANDLER_INVOKE(thread_init, td); 164de028f5aSJeff Roberson td->td_sched = (struct td_sched *)&td[1]; 165d10183d9SDavid Xu umtx_thread_init(td); 16689b57fcfSKonstantin Belousov td->td_kstack = 0; 167b23f72e9SBrian Feldman return (0); 16844990b8cSJulian Elischer } 16944990b8cSJulian Elischer 17044990b8cSJulian Elischer /* 17144990b8cSJulian Elischer * Tear down type-stable parts of a thread (just before being discarded). 17244990b8cSJulian Elischer */ 17344990b8cSJulian Elischer static void 17444990b8cSJulian Elischer thread_fini(void *mem, int size) 17544990b8cSJulian Elischer { 17644990b8cSJulian Elischer struct thread *td; 17744990b8cSJulian Elischer 17844990b8cSJulian Elischer td = (struct thread *)mem; 179b209f889SRandall Stewart EVENTHANDLER_INVOKE(thread_fini, td); 180961a7b24SJohn Baldwin turnstile_free(td->td_turnstile); 18144f3b092SJohn Baldwin sleepq_free(td->td_sleepqueue); 182d10183d9SDavid Xu umtx_thread_fini(td); 183ace8398dSJeff Roberson seltdfini(td); 18444990b8cSJulian Elischer } 1855215b187SJeff Roberson 1865c8329edSJulian Elischer /* 1875215b187SJeff Roberson * For a newly created process, 1885215b187SJeff Roberson * link up all the structures and its initial threads etc. 189ed062c8dSJulian Elischer * called from: 190ed062c8dSJulian Elischer * {arch}/{arch}/machdep.c ia64_init(), init386() etc. 191ed062c8dSJulian Elischer * proc_dtor() (should go away) 192ed062c8dSJulian Elischer * proc_init() 1935c8329edSJulian Elischer */ 1945c8329edSJulian Elischer void 19589b57fcfSKonstantin Belousov proc_linkup0(struct proc *p, struct thread *td) 19689b57fcfSKonstantin Belousov { 19789b57fcfSKonstantin Belousov TAILQ_INIT(&p->p_threads); /* all threads in proc */ 19889b57fcfSKonstantin Belousov proc_linkup(p, td); 19989b57fcfSKonstantin Belousov } 20089b57fcfSKonstantin Belousov 20189b57fcfSKonstantin Belousov void 2028460a577SJohn Birrell proc_linkup(struct proc *p, struct thread *td) 2035c8329edSJulian Elischer { 204a54e85fdSJeff Roberson 2059104847fSDavid Xu sigqueue_init(&p->p_sigqueue, p); 206ebceaf6dSDavid Xu p->p_ksi = ksiginfo_alloc(1); 207ebceaf6dSDavid Xu if (p->p_ksi != NULL) { 2085c474517SDavid Xu /* XXX p_ksi may be null if ksiginfo zone is not ready */ 209ebceaf6dSDavid Xu p->p_ksi->ksi_flags = KSI_EXT | KSI_INS; 210ebceaf6dSDavid Xu } 211b2f92ef9SDavid Xu LIST_INIT(&p->p_mqnotifier); 2125c8329edSJulian Elischer p->p_numthreads = 0; 2138460a577SJohn Birrell thread_link(td, p); 2145c8329edSJulian Elischer } 2155c8329edSJulian Elischer 2165c8329edSJulian Elischer /* 21744990b8cSJulian Elischer * Initialize global thread allocation resources. 21844990b8cSJulian Elischer */ 21944990b8cSJulian Elischer void 22044990b8cSJulian Elischer threadinit(void) 22144990b8cSJulian Elischer { 22244990b8cSJulian Elischer 2231ea7a6f8SPoul-Henning Kamp mtx_init(&tid_lock, "TID lock", NULL, MTX_DEF); 2246829a5c5SJulian Elischer /* leave one number for thread0 */ 2256829a5c5SJulian Elischer tid_unrhdr = new_unrhdr(PID_MAX + 2, INT_MAX, &tid_lock); 2261ea7a6f8SPoul-Henning Kamp 227de028f5aSJeff Roberson thread_zone = uma_zcreate("THREAD", sched_sizeof_thread(), 22844990b8cSJulian Elischer thread_ctor, thread_dtor, thread_init, thread_fini, 2294649e92bSJohn Baldwin 16 - 1, 0); 23044990b8cSJulian Elischer } 23144990b8cSJulian Elischer 23244990b8cSJulian Elischer /* 233ff8fbcffSJeff Roberson * Place an unused thread on the zombie list. 234ad1e7d28SJulian Elischer * Use the slpq as that must be unused by now. 23544990b8cSJulian Elischer */ 23644990b8cSJulian Elischer void 237ff8fbcffSJeff Roberson thread_zombie(struct thread *td) 23844990b8cSJulian Elischer { 239a54e85fdSJeff Roberson mtx_lock_spin(&zombie_lock); 240ad1e7d28SJulian Elischer TAILQ_INSERT_HEAD(&zombie_threads, td, td_slpq); 241a54e85fdSJeff Roberson mtx_unlock_spin(&zombie_lock); 24244990b8cSJulian Elischer } 24344990b8cSJulian Elischer 2445c8329edSJulian Elischer /* 245ff8fbcffSJeff Roberson * Release a thread that has exited after cpu_throw(). 246ff8fbcffSJeff Roberson */ 247ff8fbcffSJeff Roberson void 248ff8fbcffSJeff Roberson thread_stash(struct thread *td) 249ff8fbcffSJeff Roberson { 250ff8fbcffSJeff Roberson atomic_subtract_rel_int(&td->td_proc->p_exitthreads, 1); 251ff8fbcffSJeff Roberson thread_zombie(td); 252ff8fbcffSJeff Roberson } 253ff8fbcffSJeff Roberson 254ff8fbcffSJeff Roberson /* 2556617724cSJeff Roberson * Reap zombie resources. 25644990b8cSJulian Elischer */ 25744990b8cSJulian Elischer void 25844990b8cSJulian Elischer thread_reap(void) 25944990b8cSJulian Elischer { 2605c8329edSJulian Elischer struct thread *td_first, *td_next; 26144990b8cSJulian Elischer 26244990b8cSJulian Elischer /* 2635215b187SJeff Roberson * Don't even bother to lock if none at this instant, 2645215b187SJeff Roberson * we really don't care about the next instant.. 26544990b8cSJulian Elischer */ 2668460a577SJohn Birrell if (!TAILQ_EMPTY(&zombie_threads)) { 267a54e85fdSJeff Roberson mtx_lock_spin(&zombie_lock); 2685c8329edSJulian Elischer td_first = TAILQ_FIRST(&zombie_threads); 2695c8329edSJulian Elischer if (td_first) 2705c8329edSJulian Elischer TAILQ_INIT(&zombie_threads); 271a54e85fdSJeff Roberson mtx_unlock_spin(&zombie_lock); 2725c8329edSJulian Elischer while (td_first) { 273ad1e7d28SJulian Elischer td_next = TAILQ_NEXT(td_first, td_slpq); 2745215b187SJeff Roberson if (td_first->td_ucred) 2755215b187SJeff Roberson crfree(td_first->td_ucred); 2765c8329edSJulian Elischer thread_free(td_first); 2775c8329edSJulian Elischer td_first = td_next; 27844990b8cSJulian Elischer } 27944990b8cSJulian Elischer } 280ed062c8dSJulian Elischer } 28144990b8cSJulian Elischer 2824f0db5e0SJulian Elischer /* 28344990b8cSJulian Elischer * Allocate a thread. 28444990b8cSJulian Elischer */ 28544990b8cSJulian Elischer struct thread * 28644990b8cSJulian Elischer thread_alloc(void) 28744990b8cSJulian Elischer { 28889b57fcfSKonstantin Belousov struct thread *td; 2898460a577SJohn Birrell 29044990b8cSJulian Elischer thread_reap(); /* check if any zombies to get */ 29189b57fcfSKonstantin Belousov 29289b57fcfSKonstantin Belousov td = (struct thread *)uma_zalloc(thread_zone, M_WAITOK); 29389b57fcfSKonstantin Belousov KASSERT(td->td_kstack == 0, ("thread_alloc got thread with kstack")); 29489b57fcfSKonstantin Belousov if (!vm_thread_new(td, 0)) { 29589b57fcfSKonstantin Belousov uma_zfree(thread_zone, td); 29689b57fcfSKonstantin Belousov return (NULL); 29789b57fcfSKonstantin Belousov } 2980c3967e7SMarcel Moolenaar cpu_thread_alloc(td); 29989b57fcfSKonstantin Belousov return (td); 30044990b8cSJulian Elischer } 30144990b8cSJulian Elischer 3024f0db5e0SJulian Elischer 3034f0db5e0SJulian Elischer /* 30444990b8cSJulian Elischer * Deallocate a thread. 30544990b8cSJulian Elischer */ 30644990b8cSJulian Elischer void 30744990b8cSJulian Elischer thread_free(struct thread *td) 30844990b8cSJulian Elischer { 3092e6b8de4SJeff Roberson 3102e6b8de4SJeff Roberson lock_profile_thread_exit(td); 31145aea8deSJeff Roberson if (td->td_cpuset) 312d7f687fcSJeff Roberson cpuset_rel(td->td_cpuset); 313d7f687fcSJeff Roberson td->td_cpuset = NULL; 3140c3967e7SMarcel Moolenaar cpu_thread_free(td); 31589b57fcfSKonstantin Belousov if (td->td_altkstack != 0) 31689b57fcfSKonstantin Belousov vm_thread_dispose_altkstack(td); 31789b57fcfSKonstantin Belousov if (td->td_kstack != 0) 31889b57fcfSKonstantin Belousov vm_thread_dispose(td); 31944990b8cSJulian Elischer uma_zfree(thread_zone, td); 32044990b8cSJulian Elischer } 32144990b8cSJulian Elischer 32244990b8cSJulian Elischer /* 32344990b8cSJulian Elischer * Discard the current thread and exit from its context. 32494e0a4cdSJulian Elischer * Always called with scheduler locked. 32544990b8cSJulian Elischer * 32644990b8cSJulian Elischer * Because we can't free a thread while we're operating under its context, 327696058c3SJulian Elischer * push the current thread into our CPU's deadthread holder. This means 328696058c3SJulian Elischer * we needn't worry about someone else grabbing our context before we 3296617724cSJeff Roberson * do a cpu_throw(). 33044990b8cSJulian Elischer */ 33144990b8cSJulian Elischer void 33244990b8cSJulian Elischer thread_exit(void) 33344990b8cSJulian Elischer { 334e170bfdaSDavid Xu uint64_t new_switchtime; 33544990b8cSJulian Elischer struct thread *td; 3361c4bcd05SJeff Roberson struct thread *td2; 33744990b8cSJulian Elischer struct proc *p; 3387847a9daSJohn Baldwin int wakeup_swapper; 33944990b8cSJulian Elischer 34044990b8cSJulian Elischer td = curthread; 34144990b8cSJulian Elischer p = td->td_proc; 34244990b8cSJulian Elischer 343a54e85fdSJeff Roberson PROC_SLOCK_ASSERT(p, MA_OWNED); 344ed062c8dSJulian Elischer mtx_assert(&Giant, MA_NOTOWNED); 345a54e85fdSJeff Roberson 34644990b8cSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 347ed062c8dSJulian Elischer KASSERT(p != NULL, ("thread exiting without a process")); 348cc701b73SRobert Watson CTR3(KTR_PROC, "thread_exit: thread %p (pid %ld, %s)", td, 349e01eafefSJulian Elischer (long)p->p_pid, td->td_name); 3509104847fSDavid Xu KASSERT(TAILQ_EMPTY(&td->td_sigqueue.sq_list), ("signal pending")); 35144990b8cSJulian Elischer 35289964dd2SRobert Watson #ifdef AUDIT 35389964dd2SRobert Watson AUDIT_SYSCALL_EXIT(0, td); 35489964dd2SRobert Watson #endif 355d10183d9SDavid Xu umtx_thread_exit(td); 356ed062c8dSJulian Elischer /* 357ed062c8dSJulian Elischer * drop FPU & debug register state storage, or any other 358ed062c8dSJulian Elischer * architecture specific resources that 359ed062c8dSJulian Elischer * would not be on a new untouched process. 360ed062c8dSJulian Elischer */ 36144990b8cSJulian Elischer cpu_thread_exit(td); /* XXXSMP */ 36244990b8cSJulian Elischer 363e170bfdaSDavid Xu /* Do the same timestamp bookkeeping that mi_switch() would do. */ 364e170bfdaSDavid Xu new_switchtime = cpu_ticks(); 365e170bfdaSDavid Xu p->p_rux.rux_runtime += (new_switchtime - PCPU_GET(switchtime)); 366e170bfdaSDavid Xu PCPU_SET(switchtime, new_switchtime); 367e170bfdaSDavid Xu PCPU_SET(switchticks, ticks); 368b4b70819SAttilio Rao PCPU_INC(cnt.v_swtch); 369a140976eSAttilio Rao /* Save our resource usage in our process. */ 370a140976eSAttilio Rao td->td_ru.ru_nvcsw++; 371a140976eSAttilio Rao rucollect(&p->p_ru, &td->td_ru); 372ed062c8dSJulian Elischer /* 3731faf202eSJulian Elischer * The last thread is left attached to the process 3741faf202eSJulian Elischer * So that the whole bundle gets recycled. Skip 375ed062c8dSJulian Elischer * all this stuff if we never had threads. 376ed062c8dSJulian Elischer * EXIT clears all sign of other threads when 377ed062c8dSJulian Elischer * it goes to single threading, so the last thread always 378ed062c8dSJulian Elischer * takes the short path. 3791faf202eSJulian Elischer */ 380ed062c8dSJulian Elischer if (p->p_flag & P_HADTHREADS) { 3811faf202eSJulian Elischer if (p->p_numthreads > 1) { 382d3a0bd78SJulian Elischer thread_unlink(td); 3831c4bcd05SJeff Roberson td2 = FIRST_THREAD_IN_PROC(p); 3841c4bcd05SJeff Roberson sched_exit_thread(td2, td); 385ed062c8dSJulian Elischer 386ed062c8dSJulian Elischer /* 38744990b8cSJulian Elischer * The test below is NOT true if we are the 3881faf202eSJulian Elischer * sole exiting thread. P_STOPPED_SNGL is unset 38944990b8cSJulian Elischer * in exit1() after it is the only survivor. 39044990b8cSJulian Elischer */ 3911279572aSDavid Xu if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 39244990b8cSJulian Elischer if (p->p_numthreads == p->p_suspcount) { 393a54e85fdSJeff Roberson thread_lock(p->p_singlethread); 3947847a9daSJohn Baldwin wakeup_swapper = thread_unsuspend_one( 3957847a9daSJohn Baldwin p->p_singlethread); 396a54e85fdSJeff Roberson thread_unlock(p->p_singlethread); 3977847a9daSJohn Baldwin if (wakeup_swapper) 3987847a9daSJohn Baldwin kick_proc0(); 39944990b8cSJulian Elischer } 40044990b8cSJulian Elischer } 40148bfcdddSJulian Elischer 402ff8fbcffSJeff Roberson atomic_add_int(&td->td_proc->p_exitthreads, 1); 403696058c3SJulian Elischer PCPU_SET(deadthread, td); 4041faf202eSJulian Elischer } else { 405ed062c8dSJulian Elischer /* 406ed062c8dSJulian Elischer * The last thread is exiting.. but not through exit() 407ed062c8dSJulian Elischer */ 408ed062c8dSJulian Elischer panic ("thread_exit: Last thread exiting on its own"); 409ed062c8dSJulian Elischer } 4101faf202eSJulian Elischer } 411a54e85fdSJeff Roberson PROC_UNLOCK(p); 412a54e85fdSJeff Roberson thread_lock(td); 413a140976eSAttilio Rao /* Save our tick information with both the thread and proc locked */ 414a54e85fdSJeff Roberson ruxagg(&p->p_rux, td); 415a54e85fdSJeff Roberson PROC_SUNLOCK(p); 416dcc9954eSJulian Elischer td->td_state = TDS_INACTIVE; 4173d06b4b3SAttilio Rao #ifdef WITNESS 4183d06b4b3SAttilio Rao witness_thread_exit(td); 4193d06b4b3SAttilio Rao #endif 420732d9528SJulian Elischer CTR1(KTR_PROC, "thread_exit: cpu_throw() thread %p", td); 421a54e85fdSJeff Roberson sched_throw(td); 422cc66ebe2SPeter Wemm panic("I'm a teapot!"); 42344990b8cSJulian Elischer /* NOTREACHED */ 42444990b8cSJulian Elischer } 42544990b8cSJulian Elischer 42644990b8cSJulian Elischer /* 427696058c3SJulian Elischer * Do any thread specific cleanups that may be needed in wait() 42837814395SPeter Wemm * called with Giant, proc and schedlock not held. 429696058c3SJulian Elischer */ 430696058c3SJulian Elischer void 431696058c3SJulian Elischer thread_wait(struct proc *p) 432696058c3SJulian Elischer { 433696058c3SJulian Elischer struct thread *td; 434696058c3SJulian Elischer 43537814395SPeter Wemm mtx_assert(&Giant, MA_NOTOWNED); 43685495c72SJens Schweikhardt KASSERT((p->p_numthreads == 1), ("Multiple threads in wait1()")); 437ff8fbcffSJeff Roberson td = FIRST_THREAD_IN_PROC(p); 438ff8fbcffSJeff Roberson /* Lock the last thread so we spin until it exits cpu_throw(). */ 439ff8fbcffSJeff Roberson thread_lock(td); 440ff8fbcffSJeff Roberson thread_unlock(td); 441ff8fbcffSJeff Roberson /* Wait for any remaining threads to exit cpu_throw(). */ 442ff8fbcffSJeff Roberson while (p->p_exitthreads) 443ff8fbcffSJeff Roberson sched_relinquish(curthread); 4442e6b8de4SJeff Roberson lock_profile_thread_exit(td); 445d7f687fcSJeff Roberson cpuset_rel(td->td_cpuset); 446d7f687fcSJeff Roberson td->td_cpuset = NULL; 447696058c3SJulian Elischer cpu_thread_clean(td); 448ed062c8dSJulian Elischer crfree(td->td_ucred); 449696058c3SJulian Elischer thread_reap(); /* check for zombie threads etc. */ 450696058c3SJulian Elischer } 451696058c3SJulian Elischer 452696058c3SJulian Elischer /* 45344990b8cSJulian Elischer * Link a thread to a process. 4541faf202eSJulian Elischer * set up anything that needs to be initialized for it to 4551faf202eSJulian Elischer * be used by the process. 45644990b8cSJulian Elischer */ 45744990b8cSJulian Elischer void 4588460a577SJohn Birrell thread_link(struct thread *td, struct proc *p) 45944990b8cSJulian Elischer { 46044990b8cSJulian Elischer 461a54e85fdSJeff Roberson /* 462a54e85fdSJeff Roberson * XXX This can't be enabled because it's called for proc0 before 463374ae2a3SJeff Roberson * its lock has been created. 464374ae2a3SJeff Roberson * PROC_LOCK_ASSERT(p, MA_OWNED); 465a54e85fdSJeff Roberson */ 46671fad9fdSJulian Elischer td->td_state = TDS_INACTIVE; 46744990b8cSJulian Elischer td->td_proc = p; 468b61ce5b0SJeff Roberson td->td_flags = TDF_INMEM; 46944990b8cSJulian Elischer 4701faf202eSJulian Elischer LIST_INIT(&td->td_contested); 471eea4f254SJeff Roberson LIST_INIT(&td->td_lprof[0]); 472eea4f254SJeff Roberson LIST_INIT(&td->td_lprof[1]); 4739104847fSDavid Xu sigqueue_init(&td->td_sigqueue, p); 474c06eb4e2SSam Leffler callout_init(&td->td_slpcallout, CALLOUT_MPSAFE); 47544990b8cSJulian Elischer TAILQ_INSERT_HEAD(&p->p_threads, td, td_plist); 47644990b8cSJulian Elischer p->p_numthreads++; 47744990b8cSJulian Elischer } 47844990b8cSJulian Elischer 479ed062c8dSJulian Elischer /* 480e5bedcefSJulian Elischer * Convert a process with one thread to an unthreaded process. 481e5bedcefSJulian Elischer */ 482e5bedcefSJulian Elischer void 483e5bedcefSJulian Elischer thread_unthread(struct thread *td) 484e5bedcefSJulian Elischer { 485e5bedcefSJulian Elischer struct proc *p = td->td_proc; 486e5bedcefSJulian Elischer 487e5bedcefSJulian Elischer KASSERT((p->p_numthreads == 1), ("Unthreading with >1 threads")); 4888460a577SJohn Birrell p->p_flag &= ~P_HADTHREADS; 489e5bedcefSJulian Elischer } 490e5bedcefSJulian Elischer 491e5bedcefSJulian Elischer /* 492ed062c8dSJulian Elischer * Called from: 493ed062c8dSJulian Elischer * thread_exit() 494ed062c8dSJulian Elischer */ 495d3a0bd78SJulian Elischer void 496d3a0bd78SJulian Elischer thread_unlink(struct thread *td) 497d3a0bd78SJulian Elischer { 498d3a0bd78SJulian Elischer struct proc *p = td->td_proc; 499d3a0bd78SJulian Elischer 500374ae2a3SJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 501d3a0bd78SJulian Elischer TAILQ_REMOVE(&p->p_threads, td, td_plist); 502d3a0bd78SJulian Elischer p->p_numthreads--; 503d3a0bd78SJulian Elischer /* could clear a few other things here */ 5048460a577SJohn Birrell /* Must NOT clear links to proc! */ 5055c8329edSJulian Elischer } 5065c8329edSJulian Elischer 50779799053SKonstantin Belousov static int 50879799053SKonstantin Belousov calc_remaining(struct proc *p, int mode) 50979799053SKonstantin Belousov { 51079799053SKonstantin Belousov int remaining; 51179799053SKonstantin Belousov 51279799053SKonstantin Belousov if (mode == SINGLE_EXIT) 51379799053SKonstantin Belousov remaining = p->p_numthreads; 51479799053SKonstantin Belousov else if (mode == SINGLE_BOUNDARY) 51579799053SKonstantin Belousov remaining = p->p_numthreads - p->p_boundary_count; 51679799053SKonstantin Belousov else if (mode == SINGLE_NO_EXIT) 51779799053SKonstantin Belousov remaining = p->p_numthreads - p->p_suspcount; 51879799053SKonstantin Belousov else 51979799053SKonstantin Belousov panic("calc_remaining: wrong mode %d", mode); 52079799053SKonstantin Belousov return (remaining); 52179799053SKonstantin Belousov } 52279799053SKonstantin Belousov 5235215b187SJeff Roberson /* 52444990b8cSJulian Elischer * Enforce single-threading. 52544990b8cSJulian Elischer * 52644990b8cSJulian Elischer * Returns 1 if the caller must abort (another thread is waiting to 52744990b8cSJulian Elischer * exit the process or similar). Process is locked! 52844990b8cSJulian Elischer * Returns 0 when you are successfully the only thread running. 52944990b8cSJulian Elischer * A process has successfully single threaded in the suspend mode when 53044990b8cSJulian Elischer * There are no threads in user mode. Threads in the kernel must be 53144990b8cSJulian Elischer * allowed to continue until they get to the user boundary. They may even 53244990b8cSJulian Elischer * copy out their return values and data before suspending. They may however be 533e2668f55SMaxim Konovalov * accelerated in reaching the user boundary as we will wake up 53444990b8cSJulian Elischer * any sleeping threads that are interruptable. (PCATCH). 53544990b8cSJulian Elischer */ 53644990b8cSJulian Elischer int 537906ac69dSDavid Xu thread_single(int mode) 53844990b8cSJulian Elischer { 53944990b8cSJulian Elischer struct thread *td; 54044990b8cSJulian Elischer struct thread *td2; 54144990b8cSJulian Elischer struct proc *p; 542da7bbd2cSJohn Baldwin int remaining, wakeup_swapper; 54344990b8cSJulian Elischer 54444990b8cSJulian Elischer td = curthread; 54544990b8cSJulian Elischer p = td->td_proc; 54637814395SPeter Wemm mtx_assert(&Giant, MA_NOTOWNED); 54744990b8cSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 54844990b8cSJulian Elischer KASSERT((td != NULL), ("curthread is NULL")); 54944990b8cSJulian Elischer 550ed062c8dSJulian Elischer if ((p->p_flag & P_HADTHREADS) == 0) 55144990b8cSJulian Elischer return (0); 55244990b8cSJulian Elischer 553e3b9bf71SJulian Elischer /* Is someone already single threading? */ 554906ac69dSDavid Xu if (p->p_singlethread != NULL && p->p_singlethread != td) 55544990b8cSJulian Elischer return (1); 55644990b8cSJulian Elischer 557906ac69dSDavid Xu if (mode == SINGLE_EXIT) { 558906ac69dSDavid Xu p->p_flag |= P_SINGLE_EXIT; 559906ac69dSDavid Xu p->p_flag &= ~P_SINGLE_BOUNDARY; 560906ac69dSDavid Xu } else { 561906ac69dSDavid Xu p->p_flag &= ~P_SINGLE_EXIT; 562906ac69dSDavid Xu if (mode == SINGLE_BOUNDARY) 563906ac69dSDavid Xu p->p_flag |= P_SINGLE_BOUNDARY; 564906ac69dSDavid Xu else 565906ac69dSDavid Xu p->p_flag &= ~P_SINGLE_BOUNDARY; 566906ac69dSDavid Xu } 5671279572aSDavid Xu p->p_flag |= P_STOPPED_SINGLE; 5687b4a950aSDavid Xu PROC_SLOCK(p); 569112afcb2SJohn Baldwin p->p_singlethread = td; 57079799053SKonstantin Belousov remaining = calc_remaining(p, mode); 571ec008e96SDavid Xu while (remaining != 1) { 572bf1a3220SDavid Xu if (P_SHOULDSTOP(p) != P_STOPPED_SINGLE) 573bf1a3220SDavid Xu goto stopme; 574da7bbd2cSJohn Baldwin wakeup_swapper = 0; 57544990b8cSJulian Elischer FOREACH_THREAD_IN_PROC(p, td2) { 57644990b8cSJulian Elischer if (td2 == td) 57744990b8cSJulian Elischer continue; 578a54e85fdSJeff Roberson thread_lock(td2); 579b7edba77SJeff Roberson td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK; 58071fad9fdSJulian Elischer if (TD_IS_INHIBITED(td2)) { 581906ac69dSDavid Xu switch (mode) { 582906ac69dSDavid Xu case SINGLE_EXIT: 583906ac69dSDavid Xu if (TD_IS_SUSPENDED(td2)) 5847847a9daSJohn Baldwin wakeup_swapper |= 58571fad9fdSJulian Elischer thread_unsuspend_one(td2); 58633862f40SDavid Xu if (TD_ON_SLEEPQ(td2) && 587906ac69dSDavid Xu (td2->td_flags & TDF_SINTR)) 5887847a9daSJohn Baldwin wakeup_swapper |= 58994f0972bSDavid Xu sleepq_abort(td2, EINTR); 590906ac69dSDavid Xu break; 591906ac69dSDavid Xu case SINGLE_BOUNDARY: 592ffdc5a34SDavid Xu if (TD_IS_SUSPENDED(td2) && 593ffdc5a34SDavid Xu !(td2->td_flags & TDF_BOUNDARY)) 594ffdc5a34SDavid Xu wakeup_swapper |= 595ffdc5a34SDavid Xu thread_unsuspend_one(td2); 596ffdc5a34SDavid Xu if (TD_ON_SLEEPQ(td2) && 597ffdc5a34SDavid Xu (td2->td_flags & TDF_SINTR)) 598ffdc5a34SDavid Xu wakeup_swapper |= 599ffdc5a34SDavid Xu sleepq_abort(td2, ERESTART); 600906ac69dSDavid Xu break; 601906ac69dSDavid Xu default: 602a54e85fdSJeff Roberson if (TD_IS_SUSPENDED(td2)) { 603a54e85fdSJeff Roberson thread_unlock(td2); 6049d102777SJulian Elischer continue; 605a54e85fdSJeff Roberson } 6065215b187SJeff Roberson /* 6072da78e38SRobert Watson * maybe other inhibited states too? 6085215b187SJeff Roberson */ 6098acf6057SDavid Xu if ((td2->td_flags & TDF_SINTR) && 6108acf6057SDavid Xu (td2->td_inhibitors & 6118acf6057SDavid Xu (TDI_SLEEPING | TDI_SWAPPED))) 6129d102777SJulian Elischer thread_suspend_one(td2); 613906ac69dSDavid Xu break; 61444990b8cSJulian Elischer } 61544990b8cSJulian Elischer } 616d8267df7SDavid Xu #ifdef SMP 617d8267df7SDavid Xu else if (TD_IS_RUNNING(td2) && td != td2) { 618d8267df7SDavid Xu forward_signal(td2); 619d8267df7SDavid Xu } 620d8267df7SDavid Xu #endif 621a54e85fdSJeff Roberson thread_unlock(td2); 6229d102777SJulian Elischer } 623da7bbd2cSJohn Baldwin if (wakeup_swapper) 624da7bbd2cSJohn Baldwin kick_proc0(); 62579799053SKonstantin Belousov remaining = calc_remaining(p, mode); 626ec008e96SDavid Xu 6279d102777SJulian Elischer /* 6289d102777SJulian Elischer * Maybe we suspended some threads.. was it enough? 6299d102777SJulian Elischer */ 630ec008e96SDavid Xu if (remaining == 1) 6319d102777SJulian Elischer break; 6329d102777SJulian Elischer 633bf1a3220SDavid Xu stopme: 63444990b8cSJulian Elischer /* 63544990b8cSJulian Elischer * Wake us up when everyone else has suspended. 636e3b9bf71SJulian Elischer * In the mean time we suspend as well. 63744990b8cSJulian Elischer */ 638a54e85fdSJeff Roberson thread_suspend_switch(td); 63979799053SKonstantin Belousov remaining = calc_remaining(p, mode); 64044990b8cSJulian Elischer } 641906ac69dSDavid Xu if (mode == SINGLE_EXIT) { 64291599697SJulian Elischer /* 64391599697SJulian Elischer * We have gotten rid of all the other threads and we 64491599697SJulian Elischer * are about to either exit or exec. In either case, 64591599697SJulian Elischer * we try our utmost to revert to being a non-threaded 64691599697SJulian Elischer * process. 64791599697SJulian Elischer */ 648ed062c8dSJulian Elischer p->p_singlethread = NULL; 64964895117SDavid Xu p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT); 650e5bedcefSJulian Elischer thread_unthread(td); 65191599697SJulian Elischer } 6527b4a950aSDavid Xu PROC_SUNLOCK(p); 65344990b8cSJulian Elischer return (0); 65444990b8cSJulian Elischer } 65544990b8cSJulian Elischer 65644990b8cSJulian Elischer /* 65744990b8cSJulian Elischer * Called in from locations that can safely check to see 65844990b8cSJulian Elischer * whether we have to suspend or at least throttle for a 65944990b8cSJulian Elischer * single-thread event (e.g. fork). 66044990b8cSJulian Elischer * 66144990b8cSJulian Elischer * Such locations include userret(). 66244990b8cSJulian Elischer * If the "return_instead" argument is non zero, the thread must be able to 66344990b8cSJulian Elischer * accept 0 (caller may continue), or 1 (caller must abort) as a result. 66444990b8cSJulian Elischer * 66544990b8cSJulian Elischer * The 'return_instead' argument tells the function if it may do a 66644990b8cSJulian Elischer * thread_exit() or suspend, or whether the caller must abort and back 66744990b8cSJulian Elischer * out instead. 66844990b8cSJulian Elischer * 66944990b8cSJulian Elischer * If the thread that set the single_threading request has set the 67044990b8cSJulian Elischer * P_SINGLE_EXIT bit in the process flags then this call will never return 67144990b8cSJulian Elischer * if 'return_instead' is false, but will exit. 67244990b8cSJulian Elischer * 67344990b8cSJulian Elischer * P_SINGLE_EXIT | return_instead == 0| return_instead != 0 67444990b8cSJulian Elischer *---------------+--------------------+--------------------- 67544990b8cSJulian Elischer * 0 | returns 0 | returns 0 or 1 67644990b8cSJulian Elischer * | when ST ends | immediatly 67744990b8cSJulian Elischer *---------------+--------------------+--------------------- 67844990b8cSJulian Elischer * 1 | thread exits | returns 1 67944990b8cSJulian Elischer * | | immediatly 68044990b8cSJulian Elischer * 0 = thread_exit() or suspension ok, 68144990b8cSJulian Elischer * other = return error instead of stopping the thread. 68244990b8cSJulian Elischer * 68344990b8cSJulian Elischer * While a full suspension is under effect, even a single threading 68444990b8cSJulian Elischer * thread would be suspended if it made this call (but it shouldn't). 68544990b8cSJulian Elischer * This call should only be made from places where 68644990b8cSJulian Elischer * thread_exit() would be safe as that may be the outcome unless 68744990b8cSJulian Elischer * return_instead is set. 68844990b8cSJulian Elischer */ 68944990b8cSJulian Elischer int 69044990b8cSJulian Elischer thread_suspend_check(int return_instead) 69144990b8cSJulian Elischer { 692ecafb24bSJuli Mallett struct thread *td; 693ecafb24bSJuli Mallett struct proc *p; 6947847a9daSJohn Baldwin int wakeup_swapper; 69544990b8cSJulian Elischer 69644990b8cSJulian Elischer td = curthread; 69744990b8cSJulian Elischer p = td->td_proc; 69837814395SPeter Wemm mtx_assert(&Giant, MA_NOTOWNED); 69944990b8cSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 700cbf4e354SDavid Xu while (P_SHOULDSTOP(p) || 701904c5ec4SDavid Xu ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_SUSPEND))) { 7021279572aSDavid Xu if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 70344990b8cSJulian Elischer KASSERT(p->p_singlethread != NULL, 70444990b8cSJulian Elischer ("singlethread not set")); 70544990b8cSJulian Elischer /* 706e3b9bf71SJulian Elischer * The only suspension in action is a 707e3b9bf71SJulian Elischer * single-threading. Single threader need not stop. 708b6d5995eSJulian Elischer * XXX Should be safe to access unlocked 709b6d5995eSJulian Elischer * as it can only be set to be true by us. 71044990b8cSJulian Elischer */ 711e3b9bf71SJulian Elischer if (p->p_singlethread == td) 71244990b8cSJulian Elischer return (0); /* Exempt from stopping. */ 71344990b8cSJulian Elischer } 71445a4bfa1SDavid Xu if ((p->p_flag & P_SINGLE_EXIT) && return_instead) 71594f0972bSDavid Xu return (EINTR); 71644990b8cSJulian Elischer 717906ac69dSDavid Xu /* Should we goto user boundary if we didn't come from there? */ 718906ac69dSDavid Xu if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE && 719906ac69dSDavid Xu (p->p_flag & P_SINGLE_BOUNDARY) && return_instead) 72094f0972bSDavid Xu return (ERESTART); 721906ac69dSDavid Xu 7229104847fSDavid Xu /* If thread will exit, flush its pending signals */ 7239104847fSDavid Xu if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) 7249104847fSDavid Xu sigqueue_flush(&td->td_sigqueue); 7259104847fSDavid Xu 7267b4a950aSDavid Xu PROC_SLOCK(p); 727e574e444SDavid Xu thread_stopped(p); 72844990b8cSJulian Elischer /* 72944990b8cSJulian Elischer * If the process is waiting for us to exit, 73044990b8cSJulian Elischer * this thread should just suicide. 7311279572aSDavid Xu * Assumes that P_SINGLE_EXIT implies P_STOPPED_SINGLE. 73244990b8cSJulian Elischer */ 7337b4a950aSDavid Xu if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) 73444990b8cSJulian Elischer thread_exit(); 735a54e85fdSJeff Roberson if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 736a54e85fdSJeff Roberson if (p->p_numthreads == p->p_suspcount + 1) { 737a54e85fdSJeff Roberson thread_lock(p->p_singlethread); 7387847a9daSJohn Baldwin wakeup_swapper = 739a54e85fdSJeff Roberson thread_unsuspend_one(p->p_singlethread); 740a54e85fdSJeff Roberson thread_unlock(p->p_singlethread); 7417847a9daSJohn Baldwin if (wakeup_swapper) 7427847a9daSJohn Baldwin kick_proc0(); 743a54e85fdSJeff Roberson } 744a54e85fdSJeff Roberson } 7453f9be10eSDavid Xu PROC_UNLOCK(p); 7467b4a950aSDavid Xu thread_lock(td); 74744990b8cSJulian Elischer /* 74844990b8cSJulian Elischer * When a thread suspends, it just 749ad1e7d28SJulian Elischer * gets taken off all queues. 75044990b8cSJulian Elischer */ 75171fad9fdSJulian Elischer thread_suspend_one(td); 752906ac69dSDavid Xu if (return_instead == 0) { 753906ac69dSDavid Xu p->p_boundary_count++; 754906ac69dSDavid Xu td->td_flags |= TDF_BOUNDARY; 755cf19bf91SJulian Elischer } 7567b4a950aSDavid Xu PROC_SUNLOCK(p); 7578df78c41SJeff Roberson mi_switch(SW_INVOL | SWT_SUSPEND, NULL); 758a54e85fdSJeff Roberson if (return_instead == 0) 759906ac69dSDavid Xu td->td_flags &= ~TDF_BOUNDARY; 760a54e85fdSJeff Roberson thread_unlock(td); 76144990b8cSJulian Elischer PROC_LOCK(p); 762a54e85fdSJeff Roberson if (return_instead == 0) 763a54e85fdSJeff Roberson p->p_boundary_count--; 76444990b8cSJulian Elischer } 76544990b8cSJulian Elischer return (0); 76644990b8cSJulian Elischer } 76744990b8cSJulian Elischer 76835c32a76SDavid Xu void 769a54e85fdSJeff Roberson thread_suspend_switch(struct thread *td) 770a54e85fdSJeff Roberson { 771a54e85fdSJeff Roberson struct proc *p; 772a54e85fdSJeff Roberson 773a54e85fdSJeff Roberson p = td->td_proc; 774a54e85fdSJeff Roberson KASSERT(!TD_IS_SUSPENDED(td), ("already suspended")); 775a54e85fdSJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 7767b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_OWNED); 777a54e85fdSJeff Roberson /* 778a54e85fdSJeff Roberson * We implement thread_suspend_one in stages here to avoid 779a54e85fdSJeff Roberson * dropping the proc lock while the thread lock is owned. 780a54e85fdSJeff Roberson */ 781a54e85fdSJeff Roberson thread_stopped(p); 782a54e85fdSJeff Roberson p->p_suspcount++; 7833f9be10eSDavid Xu PROC_UNLOCK(p); 7847b4a950aSDavid Xu thread_lock(td); 785b7edba77SJeff Roberson td->td_flags &= ~TDF_NEEDSUSPCHK; 786a54e85fdSJeff Roberson TD_SET_SUSPENDED(td); 787c5aa6b58SJeff Roberson sched_sleep(td, 0); 7887b4a950aSDavid Xu PROC_SUNLOCK(p); 789a54e85fdSJeff Roberson DROP_GIANT(); 7908df78c41SJeff Roberson mi_switch(SW_VOL | SWT_SUSPEND, NULL); 791a54e85fdSJeff Roberson thread_unlock(td); 792a54e85fdSJeff Roberson PICKUP_GIANT(); 793a54e85fdSJeff Roberson PROC_LOCK(p); 7947b4a950aSDavid Xu PROC_SLOCK(p); 795a54e85fdSJeff Roberson } 796a54e85fdSJeff Roberson 797a54e85fdSJeff Roberson void 79835c32a76SDavid Xu thread_suspend_one(struct thread *td) 79935c32a76SDavid Xu { 80035c32a76SDavid Xu struct proc *p = td->td_proc; 80135c32a76SDavid Xu 8027b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_OWNED); 803a54e85fdSJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 804e574e444SDavid Xu KASSERT(!TD_IS_SUSPENDED(td), ("already suspended")); 80535c32a76SDavid Xu p->p_suspcount++; 806b7edba77SJeff Roberson td->td_flags &= ~TDF_NEEDSUSPCHK; 80771fad9fdSJulian Elischer TD_SET_SUSPENDED(td); 808c5aa6b58SJeff Roberson sched_sleep(td, 0); 80935c32a76SDavid Xu } 81035c32a76SDavid Xu 8117847a9daSJohn Baldwin int 81235c32a76SDavid Xu thread_unsuspend_one(struct thread *td) 81335c32a76SDavid Xu { 81435c32a76SDavid Xu struct proc *p = td->td_proc; 81535c32a76SDavid Xu 8167b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_OWNED); 817a54e85fdSJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 818ad1e7d28SJulian Elischer KASSERT(TD_IS_SUSPENDED(td), ("Thread not suspended")); 81971fad9fdSJulian Elischer TD_CLR_SUSPENDED(td); 82035c32a76SDavid Xu p->p_suspcount--; 8217847a9daSJohn Baldwin return (setrunnable(td)); 82235c32a76SDavid Xu } 82335c32a76SDavid Xu 82444990b8cSJulian Elischer /* 82544990b8cSJulian Elischer * Allow all threads blocked by single threading to continue running. 82644990b8cSJulian Elischer */ 82744990b8cSJulian Elischer void 82844990b8cSJulian Elischer thread_unsuspend(struct proc *p) 82944990b8cSJulian Elischer { 83044990b8cSJulian Elischer struct thread *td; 8317847a9daSJohn Baldwin int wakeup_swapper; 83244990b8cSJulian Elischer 83344990b8cSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 8347b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_OWNED); 8357847a9daSJohn Baldwin wakeup_swapper = 0; 83644990b8cSJulian Elischer if (!P_SHOULDSTOP(p)) { 837ad1e7d28SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 838a54e85fdSJeff Roberson thread_lock(td); 839ad1e7d28SJulian Elischer if (TD_IS_SUSPENDED(td)) { 8407847a9daSJohn Baldwin wakeup_swapper |= thread_unsuspend_one(td); 84144990b8cSJulian Elischer } 842a54e85fdSJeff Roberson thread_unlock(td); 843ad1e7d28SJulian Elischer } 8441279572aSDavid Xu } else if ((P_SHOULDSTOP(p) == P_STOPPED_SINGLE) && 84544990b8cSJulian Elischer (p->p_numthreads == p->p_suspcount)) { 84644990b8cSJulian Elischer /* 84744990b8cSJulian Elischer * Stopping everything also did the job for the single 84844990b8cSJulian Elischer * threading request. Now we've downgraded to single-threaded, 84944990b8cSJulian Elischer * let it continue. 85044990b8cSJulian Elischer */ 851a54e85fdSJeff Roberson thread_lock(p->p_singlethread); 8527847a9daSJohn Baldwin wakeup_swapper = thread_unsuspend_one(p->p_singlethread); 853a54e85fdSJeff Roberson thread_unlock(p->p_singlethread); 85444990b8cSJulian Elischer } 8557847a9daSJohn Baldwin if (wakeup_swapper) 8567847a9daSJohn Baldwin kick_proc0(); 85744990b8cSJulian Elischer } 85844990b8cSJulian Elischer 859ed062c8dSJulian Elischer /* 860ed062c8dSJulian Elischer * End the single threading mode.. 861ed062c8dSJulian Elischer */ 86244990b8cSJulian Elischer void 86344990b8cSJulian Elischer thread_single_end(void) 86444990b8cSJulian Elischer { 86544990b8cSJulian Elischer struct thread *td; 86644990b8cSJulian Elischer struct proc *p; 8677847a9daSJohn Baldwin int wakeup_swapper; 86844990b8cSJulian Elischer 86944990b8cSJulian Elischer td = curthread; 87044990b8cSJulian Elischer p = td->td_proc; 87144990b8cSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 872906ac69dSDavid Xu p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT | P_SINGLE_BOUNDARY); 8737b4a950aSDavid Xu PROC_SLOCK(p); 87444990b8cSJulian Elischer p->p_singlethread = NULL; 8757847a9daSJohn Baldwin wakeup_swapper = 0; 87649539972SJulian Elischer /* 8777847a9daSJohn Baldwin * If there are other threads they may now run, 87849539972SJulian Elischer * unless of course there is a blanket 'stop order' 87949539972SJulian Elischer * on the process. The single threader must be allowed 88049539972SJulian Elischer * to continue however as this is a bad place to stop. 88149539972SJulian Elischer */ 88249539972SJulian Elischer if ((p->p_numthreads != 1) && (!P_SHOULDSTOP(p))) { 883ad1e7d28SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 884a54e85fdSJeff Roberson thread_lock(td); 885ad1e7d28SJulian Elischer if (TD_IS_SUSPENDED(td)) { 8867847a9daSJohn Baldwin wakeup_swapper |= thread_unsuspend_one(td); 88744990b8cSJulian Elischer } 888a54e85fdSJeff Roberson thread_unlock(td); 88949539972SJulian Elischer } 890ad1e7d28SJulian Elischer } 8917b4a950aSDavid Xu PROC_SUNLOCK(p); 8927847a9daSJohn Baldwin if (wakeup_swapper) 8937847a9daSJohn Baldwin kick_proc0(); 89449539972SJulian Elischer } 8954fc21c09SDaniel Eischen 89644355392SDavid Xu struct thread * 89744355392SDavid Xu thread_find(struct proc *p, lwpid_t tid) 89844355392SDavid Xu { 89944355392SDavid Xu struct thread *td; 90044355392SDavid Xu 90144355392SDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 90244355392SDavid Xu FOREACH_THREAD_IN_PROC(p, td) { 90344355392SDavid Xu if (td->td_tid == tid) 90444355392SDavid Xu break; 90544355392SDavid Xu } 90644355392SDavid Xu return (td); 90744355392SDavid Xu } 908