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 144b209f889SRandall Stewart EVENTHANDLER_INVOKE(thread_dtor, td); 145773eff9dSPoul-Henning Kamp free_unr(tid_unrhdr, td->td_tid); 14644990b8cSJulian Elischer } 14744990b8cSJulian Elischer 14844990b8cSJulian Elischer /* 14944990b8cSJulian Elischer * Initialize type-stable parts of a thread (when newly created). 15044990b8cSJulian Elischer */ 151b23f72e9SBrian Feldman static int 152b23f72e9SBrian Feldman thread_init(void *mem, int size, int flags) 15344990b8cSJulian Elischer { 15444990b8cSJulian Elischer struct thread *td; 15544990b8cSJulian Elischer 15644990b8cSJulian Elischer td = (struct thread *)mem; 157247aba24SMarcel Moolenaar 15844f3b092SJohn Baldwin td->td_sleepqueue = sleepq_alloc(); 159961a7b24SJohn Baldwin td->td_turnstile = turnstile_alloc(); 160b209f889SRandall Stewart EVENTHANDLER_INVOKE(thread_init, td); 161de028f5aSJeff Roberson td->td_sched = (struct td_sched *)&td[1]; 162d10183d9SDavid Xu umtx_thread_init(td); 16389b57fcfSKonstantin Belousov td->td_kstack = 0; 164b23f72e9SBrian Feldman return (0); 16544990b8cSJulian Elischer } 16644990b8cSJulian Elischer 16744990b8cSJulian Elischer /* 16844990b8cSJulian Elischer * Tear down type-stable parts of a thread (just before being discarded). 16944990b8cSJulian Elischer */ 17044990b8cSJulian Elischer static void 17144990b8cSJulian Elischer thread_fini(void *mem, int size) 17244990b8cSJulian Elischer { 17344990b8cSJulian Elischer struct thread *td; 17444990b8cSJulian Elischer 17544990b8cSJulian Elischer td = (struct thread *)mem; 176b209f889SRandall Stewart EVENTHANDLER_INVOKE(thread_fini, td); 177961a7b24SJohn Baldwin turnstile_free(td->td_turnstile); 17844f3b092SJohn Baldwin sleepq_free(td->td_sleepqueue); 179d10183d9SDavid Xu umtx_thread_fini(td); 180ace8398dSJeff Roberson seltdfini(td); 18144990b8cSJulian Elischer } 1825215b187SJeff Roberson 1835c8329edSJulian Elischer /* 1845215b187SJeff Roberson * For a newly created process, 1855215b187SJeff Roberson * link up all the structures and its initial threads etc. 186ed062c8dSJulian Elischer * called from: 187ed062c8dSJulian Elischer * {arch}/{arch}/machdep.c ia64_init(), init386() etc. 188ed062c8dSJulian Elischer * proc_dtor() (should go away) 189ed062c8dSJulian Elischer * proc_init() 1905c8329edSJulian Elischer */ 1915c8329edSJulian Elischer void 19289b57fcfSKonstantin Belousov proc_linkup0(struct proc *p, struct thread *td) 19389b57fcfSKonstantin Belousov { 19489b57fcfSKonstantin Belousov TAILQ_INIT(&p->p_threads); /* all threads in proc */ 19589b57fcfSKonstantin Belousov proc_linkup(p, td); 19689b57fcfSKonstantin Belousov } 19789b57fcfSKonstantin Belousov 19889b57fcfSKonstantin Belousov void 1998460a577SJohn Birrell proc_linkup(struct proc *p, struct thread *td) 2005c8329edSJulian Elischer { 201a54e85fdSJeff Roberson 2029104847fSDavid Xu sigqueue_init(&p->p_sigqueue, p); 203ebceaf6dSDavid Xu p->p_ksi = ksiginfo_alloc(1); 204ebceaf6dSDavid Xu if (p->p_ksi != NULL) { 2055c474517SDavid Xu /* XXX p_ksi may be null if ksiginfo zone is not ready */ 206ebceaf6dSDavid Xu p->p_ksi->ksi_flags = KSI_EXT | KSI_INS; 207ebceaf6dSDavid Xu } 208b2f92ef9SDavid Xu LIST_INIT(&p->p_mqnotifier); 2095c8329edSJulian Elischer p->p_numthreads = 0; 2108460a577SJohn Birrell thread_link(td, p); 2115c8329edSJulian Elischer } 2125c8329edSJulian Elischer 2135c8329edSJulian Elischer /* 21444990b8cSJulian Elischer * Initialize global thread allocation resources. 21544990b8cSJulian Elischer */ 21644990b8cSJulian Elischer void 21744990b8cSJulian Elischer threadinit(void) 21844990b8cSJulian Elischer { 21944990b8cSJulian Elischer 2201ea7a6f8SPoul-Henning Kamp mtx_init(&tid_lock, "TID lock", NULL, MTX_DEF); 2216829a5c5SJulian Elischer /* leave one number for thread0 */ 2226829a5c5SJulian Elischer tid_unrhdr = new_unrhdr(PID_MAX + 2, INT_MAX, &tid_lock); 2231ea7a6f8SPoul-Henning Kamp 224de028f5aSJeff Roberson thread_zone = uma_zcreate("THREAD", sched_sizeof_thread(), 22544990b8cSJulian Elischer thread_ctor, thread_dtor, thread_init, thread_fini, 2264649e92bSJohn Baldwin 16 - 1, 0); 22744990b8cSJulian Elischer } 22844990b8cSJulian Elischer 22944990b8cSJulian Elischer /* 230ff8fbcffSJeff Roberson * Place an unused thread on the zombie list. 231ad1e7d28SJulian Elischer * Use the slpq as that must be unused by now. 23244990b8cSJulian Elischer */ 23344990b8cSJulian Elischer void 234ff8fbcffSJeff Roberson thread_zombie(struct thread *td) 23544990b8cSJulian Elischer { 236a54e85fdSJeff Roberson mtx_lock_spin(&zombie_lock); 237ad1e7d28SJulian Elischer TAILQ_INSERT_HEAD(&zombie_threads, td, td_slpq); 238a54e85fdSJeff Roberson mtx_unlock_spin(&zombie_lock); 23944990b8cSJulian Elischer } 24044990b8cSJulian Elischer 2415c8329edSJulian Elischer /* 242ff8fbcffSJeff Roberson * Release a thread that has exited after cpu_throw(). 243ff8fbcffSJeff Roberson */ 244ff8fbcffSJeff Roberson void 245ff8fbcffSJeff Roberson thread_stash(struct thread *td) 246ff8fbcffSJeff Roberson { 247ff8fbcffSJeff Roberson atomic_subtract_rel_int(&td->td_proc->p_exitthreads, 1); 248ff8fbcffSJeff Roberson thread_zombie(td); 249ff8fbcffSJeff Roberson } 250ff8fbcffSJeff Roberson 251ff8fbcffSJeff Roberson /* 2526617724cSJeff Roberson * Reap zombie resources. 25344990b8cSJulian Elischer */ 25444990b8cSJulian Elischer void 25544990b8cSJulian Elischer thread_reap(void) 25644990b8cSJulian Elischer { 2575c8329edSJulian Elischer struct thread *td_first, *td_next; 25844990b8cSJulian Elischer 25944990b8cSJulian Elischer /* 2605215b187SJeff Roberson * Don't even bother to lock if none at this instant, 2615215b187SJeff Roberson * we really don't care about the next instant.. 26244990b8cSJulian Elischer */ 2638460a577SJohn Birrell if (!TAILQ_EMPTY(&zombie_threads)) { 264a54e85fdSJeff Roberson mtx_lock_spin(&zombie_lock); 2655c8329edSJulian Elischer td_first = TAILQ_FIRST(&zombie_threads); 2665c8329edSJulian Elischer if (td_first) 2675c8329edSJulian Elischer TAILQ_INIT(&zombie_threads); 268a54e85fdSJeff Roberson mtx_unlock_spin(&zombie_lock); 2695c8329edSJulian Elischer while (td_first) { 270ad1e7d28SJulian Elischer td_next = TAILQ_NEXT(td_first, td_slpq); 2715215b187SJeff Roberson if (td_first->td_ucred) 2725215b187SJeff Roberson crfree(td_first->td_ucred); 2735c8329edSJulian Elischer thread_free(td_first); 2745c8329edSJulian Elischer td_first = td_next; 27544990b8cSJulian Elischer } 27644990b8cSJulian Elischer } 277ed062c8dSJulian Elischer } 27844990b8cSJulian Elischer 2794f0db5e0SJulian Elischer /* 28044990b8cSJulian Elischer * Allocate a thread. 28144990b8cSJulian Elischer */ 28244990b8cSJulian Elischer struct thread * 28344990b8cSJulian Elischer thread_alloc(void) 28444990b8cSJulian Elischer { 28589b57fcfSKonstantin Belousov struct thread *td; 2868460a577SJohn Birrell 28744990b8cSJulian Elischer thread_reap(); /* check if any zombies to get */ 28889b57fcfSKonstantin Belousov 28989b57fcfSKonstantin Belousov td = (struct thread *)uma_zalloc(thread_zone, M_WAITOK); 29089b57fcfSKonstantin Belousov KASSERT(td->td_kstack == 0, ("thread_alloc got thread with kstack")); 29189b57fcfSKonstantin Belousov if (!vm_thread_new(td, 0)) { 29289b57fcfSKonstantin Belousov uma_zfree(thread_zone, td); 29389b57fcfSKonstantin Belousov return (NULL); 29489b57fcfSKonstantin Belousov } 2950c3967e7SMarcel Moolenaar cpu_thread_alloc(td); 29689b57fcfSKonstantin Belousov return (td); 29744990b8cSJulian Elischer } 29844990b8cSJulian Elischer 2994f0db5e0SJulian Elischer 3004f0db5e0SJulian Elischer /* 30144990b8cSJulian Elischer * Deallocate a thread. 30244990b8cSJulian Elischer */ 30344990b8cSJulian Elischer void 30444990b8cSJulian Elischer thread_free(struct thread *td) 30544990b8cSJulian Elischer { 30645aea8deSJeff Roberson if (td->td_cpuset) 307d7f687fcSJeff Roberson cpuset_rel(td->td_cpuset); 308d7f687fcSJeff Roberson td->td_cpuset = NULL; 3090c3967e7SMarcel Moolenaar cpu_thread_free(td); 31089b57fcfSKonstantin Belousov if (td->td_altkstack != 0) 31189b57fcfSKonstantin Belousov vm_thread_dispose_altkstack(td); 31289b57fcfSKonstantin Belousov if (td->td_kstack != 0) 31389b57fcfSKonstantin Belousov vm_thread_dispose(td); 31444990b8cSJulian Elischer uma_zfree(thread_zone, td); 31544990b8cSJulian Elischer } 31644990b8cSJulian Elischer 31744990b8cSJulian Elischer /* 31844990b8cSJulian Elischer * Discard the current thread and exit from its context. 31994e0a4cdSJulian Elischer * Always called with scheduler locked. 32044990b8cSJulian Elischer * 32144990b8cSJulian Elischer * Because we can't free a thread while we're operating under its context, 322696058c3SJulian Elischer * push the current thread into our CPU's deadthread holder. This means 323696058c3SJulian Elischer * we needn't worry about someone else grabbing our context before we 3246617724cSJeff Roberson * do a cpu_throw(). 32544990b8cSJulian Elischer */ 32644990b8cSJulian Elischer void 32744990b8cSJulian Elischer thread_exit(void) 32844990b8cSJulian Elischer { 329e170bfdaSDavid Xu uint64_t new_switchtime; 33044990b8cSJulian Elischer struct thread *td; 3311c4bcd05SJeff Roberson struct thread *td2; 33244990b8cSJulian Elischer struct proc *p; 3337847a9daSJohn Baldwin int wakeup_swapper; 33444990b8cSJulian Elischer 33544990b8cSJulian Elischer td = curthread; 33644990b8cSJulian Elischer p = td->td_proc; 33744990b8cSJulian Elischer 338a54e85fdSJeff Roberson PROC_SLOCK_ASSERT(p, MA_OWNED); 339ed062c8dSJulian Elischer mtx_assert(&Giant, MA_NOTOWNED); 340a54e85fdSJeff Roberson 34144990b8cSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 342ed062c8dSJulian Elischer KASSERT(p != NULL, ("thread exiting without a process")); 343cc701b73SRobert Watson CTR3(KTR_PROC, "thread_exit: thread %p (pid %ld, %s)", td, 344e01eafefSJulian Elischer (long)p->p_pid, td->td_name); 3459104847fSDavid Xu KASSERT(TAILQ_EMPTY(&td->td_sigqueue.sq_list), ("signal pending")); 34644990b8cSJulian Elischer 34789964dd2SRobert Watson #ifdef AUDIT 34889964dd2SRobert Watson AUDIT_SYSCALL_EXIT(0, td); 34989964dd2SRobert Watson #endif 350d10183d9SDavid Xu umtx_thread_exit(td); 351ed062c8dSJulian Elischer /* 352ed062c8dSJulian Elischer * drop FPU & debug register state storage, or any other 353ed062c8dSJulian Elischer * architecture specific resources that 354ed062c8dSJulian Elischer * would not be on a new untouched process. 355ed062c8dSJulian Elischer */ 35644990b8cSJulian Elischer cpu_thread_exit(td); /* XXXSMP */ 35744990b8cSJulian Elischer 358e170bfdaSDavid Xu /* Do the same timestamp bookkeeping that mi_switch() would do. */ 359e170bfdaSDavid Xu new_switchtime = cpu_ticks(); 360e170bfdaSDavid Xu p->p_rux.rux_runtime += (new_switchtime - PCPU_GET(switchtime)); 361e170bfdaSDavid Xu PCPU_SET(switchtime, new_switchtime); 362e170bfdaSDavid Xu PCPU_SET(switchticks, ticks); 363b4b70819SAttilio Rao PCPU_INC(cnt.v_swtch); 364a140976eSAttilio Rao /* Save our resource usage in our process. */ 365a140976eSAttilio Rao td->td_ru.ru_nvcsw++; 366a140976eSAttilio Rao rucollect(&p->p_ru, &td->td_ru); 367ed062c8dSJulian Elischer /* 3681faf202eSJulian Elischer * The last thread is left attached to the process 3691faf202eSJulian Elischer * So that the whole bundle gets recycled. Skip 370ed062c8dSJulian Elischer * all this stuff if we never had threads. 371ed062c8dSJulian Elischer * EXIT clears all sign of other threads when 372ed062c8dSJulian Elischer * it goes to single threading, so the last thread always 373ed062c8dSJulian Elischer * takes the short path. 3741faf202eSJulian Elischer */ 375ed062c8dSJulian Elischer if (p->p_flag & P_HADTHREADS) { 3761faf202eSJulian Elischer if (p->p_numthreads > 1) { 377d3a0bd78SJulian Elischer thread_unlink(td); 3781c4bcd05SJeff Roberson td2 = FIRST_THREAD_IN_PROC(p); 3791c4bcd05SJeff Roberson sched_exit_thread(td2, td); 380ed062c8dSJulian Elischer 381ed062c8dSJulian Elischer /* 38244990b8cSJulian Elischer * The test below is NOT true if we are the 3831faf202eSJulian Elischer * sole exiting thread. P_STOPPED_SNGL is unset 38444990b8cSJulian Elischer * in exit1() after it is the only survivor. 38544990b8cSJulian Elischer */ 3861279572aSDavid Xu if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 38744990b8cSJulian Elischer if (p->p_numthreads == p->p_suspcount) { 388a54e85fdSJeff Roberson thread_lock(p->p_singlethread); 3897847a9daSJohn Baldwin wakeup_swapper = thread_unsuspend_one( 3907847a9daSJohn Baldwin p->p_singlethread); 391a54e85fdSJeff Roberson thread_unlock(p->p_singlethread); 3927847a9daSJohn Baldwin if (wakeup_swapper) 3937847a9daSJohn Baldwin kick_proc0(); 39444990b8cSJulian Elischer } 39544990b8cSJulian Elischer } 39648bfcdddSJulian Elischer 397ff8fbcffSJeff Roberson atomic_add_int(&td->td_proc->p_exitthreads, 1); 398696058c3SJulian Elischer PCPU_SET(deadthread, td); 3991faf202eSJulian Elischer } else { 400ed062c8dSJulian Elischer /* 401ed062c8dSJulian Elischer * The last thread is exiting.. but not through exit() 402ed062c8dSJulian Elischer */ 403ed062c8dSJulian Elischer panic ("thread_exit: Last thread exiting on its own"); 404ed062c8dSJulian Elischer } 4051faf202eSJulian Elischer } 406a54e85fdSJeff Roberson PROC_UNLOCK(p); 407a54e85fdSJeff Roberson thread_lock(td); 408a140976eSAttilio Rao /* Save our tick information with both the thread and proc locked */ 409a54e85fdSJeff Roberson ruxagg(&p->p_rux, td); 410a54e85fdSJeff Roberson PROC_SUNLOCK(p); 411dcc9954eSJulian Elischer td->td_state = TDS_INACTIVE; 4123d06b4b3SAttilio Rao #ifdef WITNESS 4133d06b4b3SAttilio Rao witness_thread_exit(td); 4143d06b4b3SAttilio Rao #endif 415732d9528SJulian Elischer CTR1(KTR_PROC, "thread_exit: cpu_throw() thread %p", td); 416a54e85fdSJeff Roberson sched_throw(td); 417cc66ebe2SPeter Wemm panic("I'm a teapot!"); 41844990b8cSJulian Elischer /* NOTREACHED */ 41944990b8cSJulian Elischer } 42044990b8cSJulian Elischer 42144990b8cSJulian Elischer /* 422696058c3SJulian Elischer * Do any thread specific cleanups that may be needed in wait() 42337814395SPeter Wemm * called with Giant, proc and schedlock not held. 424696058c3SJulian Elischer */ 425696058c3SJulian Elischer void 426696058c3SJulian Elischer thread_wait(struct proc *p) 427696058c3SJulian Elischer { 428696058c3SJulian Elischer struct thread *td; 429696058c3SJulian Elischer 43037814395SPeter Wemm mtx_assert(&Giant, MA_NOTOWNED); 43185495c72SJens Schweikhardt KASSERT((p->p_numthreads == 1), ("Multiple threads in wait1()")); 432ff8fbcffSJeff Roberson td = FIRST_THREAD_IN_PROC(p); 433ff8fbcffSJeff Roberson /* Lock the last thread so we spin until it exits cpu_throw(). */ 434ff8fbcffSJeff Roberson thread_lock(td); 435ff8fbcffSJeff Roberson thread_unlock(td); 436ff8fbcffSJeff Roberson /* Wait for any remaining threads to exit cpu_throw(). */ 437ff8fbcffSJeff Roberson while (p->p_exitthreads) 438ff8fbcffSJeff Roberson sched_relinquish(curthread); 439d7f687fcSJeff Roberson cpuset_rel(td->td_cpuset); 440d7f687fcSJeff Roberson td->td_cpuset = NULL; 441696058c3SJulian Elischer cpu_thread_clean(td); 442ed062c8dSJulian Elischer crfree(td->td_ucred); 443696058c3SJulian Elischer thread_reap(); /* check for zombie threads etc. */ 444696058c3SJulian Elischer } 445696058c3SJulian Elischer 446696058c3SJulian Elischer /* 44744990b8cSJulian Elischer * Link a thread to a process. 4481faf202eSJulian Elischer * set up anything that needs to be initialized for it to 4491faf202eSJulian Elischer * be used by the process. 45044990b8cSJulian Elischer */ 45144990b8cSJulian Elischer void 4528460a577SJohn Birrell thread_link(struct thread *td, struct proc *p) 45344990b8cSJulian Elischer { 45444990b8cSJulian Elischer 455a54e85fdSJeff Roberson /* 456a54e85fdSJeff Roberson * XXX This can't be enabled because it's called for proc0 before 457374ae2a3SJeff Roberson * its lock has been created. 458374ae2a3SJeff Roberson * PROC_LOCK_ASSERT(p, MA_OWNED); 459a54e85fdSJeff Roberson */ 46071fad9fdSJulian Elischer td->td_state = TDS_INACTIVE; 46144990b8cSJulian Elischer td->td_proc = p; 462b61ce5b0SJeff Roberson td->td_flags = TDF_INMEM; 46344990b8cSJulian Elischer 4641faf202eSJulian Elischer LIST_INIT(&td->td_contested); 465eea4f254SJeff Roberson LIST_INIT(&td->td_lprof[0]); 466eea4f254SJeff Roberson LIST_INIT(&td->td_lprof[1]); 4679104847fSDavid Xu sigqueue_init(&td->td_sigqueue, p); 468c06eb4e2SSam Leffler callout_init(&td->td_slpcallout, CALLOUT_MPSAFE); 46944990b8cSJulian Elischer TAILQ_INSERT_HEAD(&p->p_threads, td, td_plist); 47044990b8cSJulian Elischer p->p_numthreads++; 47144990b8cSJulian Elischer } 47244990b8cSJulian Elischer 473ed062c8dSJulian Elischer /* 474e5bedcefSJulian Elischer * Convert a process with one thread to an unthreaded process. 475e5bedcefSJulian Elischer */ 476e5bedcefSJulian Elischer void 477e5bedcefSJulian Elischer thread_unthread(struct thread *td) 478e5bedcefSJulian Elischer { 479e5bedcefSJulian Elischer struct proc *p = td->td_proc; 480e5bedcefSJulian Elischer 481e5bedcefSJulian Elischer KASSERT((p->p_numthreads == 1), ("Unthreading with >1 threads")); 4828460a577SJohn Birrell p->p_flag &= ~P_HADTHREADS; 483e5bedcefSJulian Elischer } 484e5bedcefSJulian Elischer 485e5bedcefSJulian Elischer /* 486ed062c8dSJulian Elischer * Called from: 487ed062c8dSJulian Elischer * thread_exit() 488ed062c8dSJulian Elischer */ 489d3a0bd78SJulian Elischer void 490d3a0bd78SJulian Elischer thread_unlink(struct thread *td) 491d3a0bd78SJulian Elischer { 492d3a0bd78SJulian Elischer struct proc *p = td->td_proc; 493d3a0bd78SJulian Elischer 494374ae2a3SJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 495d3a0bd78SJulian Elischer TAILQ_REMOVE(&p->p_threads, td, td_plist); 496d3a0bd78SJulian Elischer p->p_numthreads--; 497d3a0bd78SJulian Elischer /* could clear a few other things here */ 4988460a577SJohn Birrell /* Must NOT clear links to proc! */ 4995c8329edSJulian Elischer } 5005c8329edSJulian Elischer 5015215b187SJeff Roberson /* 50244990b8cSJulian Elischer * Enforce single-threading. 50344990b8cSJulian Elischer * 50444990b8cSJulian Elischer * Returns 1 if the caller must abort (another thread is waiting to 50544990b8cSJulian Elischer * exit the process or similar). Process is locked! 50644990b8cSJulian Elischer * Returns 0 when you are successfully the only thread running. 50744990b8cSJulian Elischer * A process has successfully single threaded in the suspend mode when 50844990b8cSJulian Elischer * There are no threads in user mode. Threads in the kernel must be 50944990b8cSJulian Elischer * allowed to continue until they get to the user boundary. They may even 51044990b8cSJulian Elischer * copy out their return values and data before suspending. They may however be 511e2668f55SMaxim Konovalov * accelerated in reaching the user boundary as we will wake up 51244990b8cSJulian Elischer * any sleeping threads that are interruptable. (PCATCH). 51344990b8cSJulian Elischer */ 51444990b8cSJulian Elischer int 515906ac69dSDavid Xu thread_single(int mode) 51644990b8cSJulian Elischer { 51744990b8cSJulian Elischer struct thread *td; 51844990b8cSJulian Elischer struct thread *td2; 51944990b8cSJulian Elischer struct proc *p; 520da7bbd2cSJohn Baldwin int remaining, wakeup_swapper; 52144990b8cSJulian Elischer 52244990b8cSJulian Elischer td = curthread; 52344990b8cSJulian Elischer p = td->td_proc; 52437814395SPeter Wemm mtx_assert(&Giant, MA_NOTOWNED); 52544990b8cSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 52644990b8cSJulian Elischer KASSERT((td != NULL), ("curthread is NULL")); 52744990b8cSJulian Elischer 528ed062c8dSJulian Elischer if ((p->p_flag & P_HADTHREADS) == 0) 52944990b8cSJulian Elischer return (0); 53044990b8cSJulian Elischer 531e3b9bf71SJulian Elischer /* Is someone already single threading? */ 532906ac69dSDavid Xu if (p->p_singlethread != NULL && p->p_singlethread != td) 53344990b8cSJulian Elischer return (1); 53444990b8cSJulian Elischer 535906ac69dSDavid Xu if (mode == SINGLE_EXIT) { 536906ac69dSDavid Xu p->p_flag |= P_SINGLE_EXIT; 537906ac69dSDavid Xu p->p_flag &= ~P_SINGLE_BOUNDARY; 538906ac69dSDavid Xu } else { 539906ac69dSDavid Xu p->p_flag &= ~P_SINGLE_EXIT; 540906ac69dSDavid Xu if (mode == SINGLE_BOUNDARY) 541906ac69dSDavid Xu p->p_flag |= P_SINGLE_BOUNDARY; 542906ac69dSDavid Xu else 543906ac69dSDavid Xu p->p_flag &= ~P_SINGLE_BOUNDARY; 544906ac69dSDavid Xu } 5451279572aSDavid Xu p->p_flag |= P_STOPPED_SINGLE; 5467b4a950aSDavid Xu PROC_SLOCK(p); 547112afcb2SJohn Baldwin p->p_singlethread = td; 548906ac69dSDavid Xu if (mode == SINGLE_EXIT) 549ec008e96SDavid Xu remaining = p->p_numthreads; 550906ac69dSDavid Xu else if (mode == SINGLE_BOUNDARY) 551906ac69dSDavid Xu remaining = p->p_numthreads - p->p_boundary_count; 552906ac69dSDavid Xu else 553ec008e96SDavid Xu remaining = p->p_numthreads - p->p_suspcount; 554ec008e96SDavid Xu while (remaining != 1) { 555bf1a3220SDavid Xu if (P_SHOULDSTOP(p) != P_STOPPED_SINGLE) 556bf1a3220SDavid Xu goto stopme; 557da7bbd2cSJohn Baldwin wakeup_swapper = 0; 55844990b8cSJulian Elischer FOREACH_THREAD_IN_PROC(p, td2) { 55944990b8cSJulian Elischer if (td2 == td) 56044990b8cSJulian Elischer continue; 561a54e85fdSJeff Roberson thread_lock(td2); 562b7edba77SJeff Roberson td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK; 56371fad9fdSJulian Elischer if (TD_IS_INHIBITED(td2)) { 564906ac69dSDavid Xu switch (mode) { 565906ac69dSDavid Xu case SINGLE_EXIT: 566906ac69dSDavid Xu if (TD_IS_SUSPENDED(td2)) 5677847a9daSJohn Baldwin wakeup_swapper |= 56871fad9fdSJulian Elischer thread_unsuspend_one(td2); 56933862f40SDavid Xu if (TD_ON_SLEEPQ(td2) && 570906ac69dSDavid Xu (td2->td_flags & TDF_SINTR)) 5717847a9daSJohn Baldwin wakeup_swapper |= 57294f0972bSDavid Xu sleepq_abort(td2, EINTR); 573906ac69dSDavid Xu break; 574906ac69dSDavid Xu case SINGLE_BOUNDARY: 575ffdc5a34SDavid Xu if (TD_IS_SUSPENDED(td2) && 576ffdc5a34SDavid Xu !(td2->td_flags & TDF_BOUNDARY)) 577ffdc5a34SDavid Xu wakeup_swapper |= 578ffdc5a34SDavid Xu thread_unsuspend_one(td2); 579ffdc5a34SDavid Xu if (TD_ON_SLEEPQ(td2) && 580ffdc5a34SDavid Xu (td2->td_flags & TDF_SINTR)) 581ffdc5a34SDavid Xu wakeup_swapper |= 582ffdc5a34SDavid Xu sleepq_abort(td2, ERESTART); 583906ac69dSDavid Xu break; 584906ac69dSDavid Xu default: 585a54e85fdSJeff Roberson if (TD_IS_SUSPENDED(td2)) { 586a54e85fdSJeff Roberson thread_unlock(td2); 5879d102777SJulian Elischer continue; 588a54e85fdSJeff Roberson } 5895215b187SJeff Roberson /* 5902da78e38SRobert Watson * maybe other inhibited states too? 5915215b187SJeff Roberson */ 5928acf6057SDavid Xu if ((td2->td_flags & TDF_SINTR) && 5938acf6057SDavid Xu (td2->td_inhibitors & 5948acf6057SDavid Xu (TDI_SLEEPING | TDI_SWAPPED))) 5959d102777SJulian Elischer thread_suspend_one(td2); 596906ac69dSDavid Xu break; 59744990b8cSJulian Elischer } 59844990b8cSJulian Elischer } 599d8267df7SDavid Xu #ifdef SMP 600d8267df7SDavid Xu else if (TD_IS_RUNNING(td2) && td != td2) { 601d8267df7SDavid Xu forward_signal(td2); 602d8267df7SDavid Xu } 603d8267df7SDavid Xu #endif 604a54e85fdSJeff Roberson thread_unlock(td2); 6059d102777SJulian Elischer } 606da7bbd2cSJohn Baldwin if (wakeup_swapper) 607da7bbd2cSJohn Baldwin kick_proc0(); 608906ac69dSDavid Xu if (mode == SINGLE_EXIT) 609ec008e96SDavid Xu remaining = p->p_numthreads; 610906ac69dSDavid Xu else if (mode == SINGLE_BOUNDARY) 611906ac69dSDavid Xu remaining = p->p_numthreads - p->p_boundary_count; 612ec008e96SDavid Xu else 613ec008e96SDavid Xu remaining = p->p_numthreads - p->p_suspcount; 614ec008e96SDavid Xu 6159d102777SJulian Elischer /* 6169d102777SJulian Elischer * Maybe we suspended some threads.. was it enough? 6179d102777SJulian Elischer */ 618ec008e96SDavid Xu if (remaining == 1) 6199d102777SJulian Elischer break; 6209d102777SJulian Elischer 621bf1a3220SDavid Xu stopme: 62244990b8cSJulian Elischer /* 62344990b8cSJulian Elischer * Wake us up when everyone else has suspended. 624e3b9bf71SJulian Elischer * In the mean time we suspend as well. 62544990b8cSJulian Elischer */ 626a54e85fdSJeff Roberson thread_suspend_switch(td); 627906ac69dSDavid Xu if (mode == SINGLE_EXIT) 628ec008e96SDavid Xu remaining = p->p_numthreads; 629906ac69dSDavid Xu else if (mode == SINGLE_BOUNDARY) 630906ac69dSDavid Xu remaining = p->p_numthreads - p->p_boundary_count; 631ec008e96SDavid Xu else 632ec008e96SDavid Xu remaining = p->p_numthreads - p->p_suspcount; 63344990b8cSJulian Elischer } 634906ac69dSDavid Xu if (mode == SINGLE_EXIT) { 63591599697SJulian Elischer /* 63691599697SJulian Elischer * We have gotten rid of all the other threads and we 63791599697SJulian Elischer * are about to either exit or exec. In either case, 63891599697SJulian Elischer * we try our utmost to revert to being a non-threaded 63991599697SJulian Elischer * process. 64091599697SJulian Elischer */ 641ed062c8dSJulian Elischer p->p_singlethread = NULL; 64264895117SDavid Xu p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT); 643e5bedcefSJulian Elischer thread_unthread(td); 64491599697SJulian Elischer } 6457b4a950aSDavid Xu PROC_SUNLOCK(p); 64644990b8cSJulian Elischer return (0); 64744990b8cSJulian Elischer } 64844990b8cSJulian Elischer 64944990b8cSJulian Elischer /* 65044990b8cSJulian Elischer * Called in from locations that can safely check to see 65144990b8cSJulian Elischer * whether we have to suspend or at least throttle for a 65244990b8cSJulian Elischer * single-thread event (e.g. fork). 65344990b8cSJulian Elischer * 65444990b8cSJulian Elischer * Such locations include userret(). 65544990b8cSJulian Elischer * If the "return_instead" argument is non zero, the thread must be able to 65644990b8cSJulian Elischer * accept 0 (caller may continue), or 1 (caller must abort) as a result. 65744990b8cSJulian Elischer * 65844990b8cSJulian Elischer * The 'return_instead' argument tells the function if it may do a 65944990b8cSJulian Elischer * thread_exit() or suspend, or whether the caller must abort and back 66044990b8cSJulian Elischer * out instead. 66144990b8cSJulian Elischer * 66244990b8cSJulian Elischer * If the thread that set the single_threading request has set the 66344990b8cSJulian Elischer * P_SINGLE_EXIT bit in the process flags then this call will never return 66444990b8cSJulian Elischer * if 'return_instead' is false, but will exit. 66544990b8cSJulian Elischer * 66644990b8cSJulian Elischer * P_SINGLE_EXIT | return_instead == 0| return_instead != 0 66744990b8cSJulian Elischer *---------------+--------------------+--------------------- 66844990b8cSJulian Elischer * 0 | returns 0 | returns 0 or 1 66944990b8cSJulian Elischer * | when ST ends | immediatly 67044990b8cSJulian Elischer *---------------+--------------------+--------------------- 67144990b8cSJulian Elischer * 1 | thread exits | returns 1 67244990b8cSJulian Elischer * | | immediatly 67344990b8cSJulian Elischer * 0 = thread_exit() or suspension ok, 67444990b8cSJulian Elischer * other = return error instead of stopping the thread. 67544990b8cSJulian Elischer * 67644990b8cSJulian Elischer * While a full suspension is under effect, even a single threading 67744990b8cSJulian Elischer * thread would be suspended if it made this call (but it shouldn't). 67844990b8cSJulian Elischer * This call should only be made from places where 67944990b8cSJulian Elischer * thread_exit() would be safe as that may be the outcome unless 68044990b8cSJulian Elischer * return_instead is set. 68144990b8cSJulian Elischer */ 68244990b8cSJulian Elischer int 68344990b8cSJulian Elischer thread_suspend_check(int return_instead) 68444990b8cSJulian Elischer { 685ecafb24bSJuli Mallett struct thread *td; 686ecafb24bSJuli Mallett struct proc *p; 6877847a9daSJohn Baldwin int wakeup_swapper; 68844990b8cSJulian Elischer 68944990b8cSJulian Elischer td = curthread; 69044990b8cSJulian Elischer p = td->td_proc; 69137814395SPeter Wemm mtx_assert(&Giant, MA_NOTOWNED); 69244990b8cSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 693cbf4e354SDavid Xu while (P_SHOULDSTOP(p) || 694904c5ec4SDavid Xu ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_SUSPEND))) { 6951279572aSDavid Xu if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 69644990b8cSJulian Elischer KASSERT(p->p_singlethread != NULL, 69744990b8cSJulian Elischer ("singlethread not set")); 69844990b8cSJulian Elischer /* 699e3b9bf71SJulian Elischer * The only suspension in action is a 700e3b9bf71SJulian Elischer * single-threading. Single threader need not stop. 701b6d5995eSJulian Elischer * XXX Should be safe to access unlocked 702b6d5995eSJulian Elischer * as it can only be set to be true by us. 70344990b8cSJulian Elischer */ 704e3b9bf71SJulian Elischer if (p->p_singlethread == td) 70544990b8cSJulian Elischer return (0); /* Exempt from stopping. */ 70644990b8cSJulian Elischer } 70745a4bfa1SDavid Xu if ((p->p_flag & P_SINGLE_EXIT) && return_instead) 70894f0972bSDavid Xu return (EINTR); 70944990b8cSJulian Elischer 710906ac69dSDavid Xu /* Should we goto user boundary if we didn't come from there? */ 711906ac69dSDavid Xu if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE && 712906ac69dSDavid Xu (p->p_flag & P_SINGLE_BOUNDARY) && return_instead) 71394f0972bSDavid Xu return (ERESTART); 714906ac69dSDavid Xu 7159104847fSDavid Xu /* If thread will exit, flush its pending signals */ 7169104847fSDavid Xu if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) 7179104847fSDavid Xu sigqueue_flush(&td->td_sigqueue); 7189104847fSDavid Xu 7197b4a950aSDavid Xu PROC_SLOCK(p); 720e574e444SDavid Xu thread_stopped(p); 72144990b8cSJulian Elischer /* 72244990b8cSJulian Elischer * If the process is waiting for us to exit, 72344990b8cSJulian Elischer * this thread should just suicide. 7241279572aSDavid Xu * Assumes that P_SINGLE_EXIT implies P_STOPPED_SINGLE. 72544990b8cSJulian Elischer */ 7267b4a950aSDavid Xu if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) 72744990b8cSJulian Elischer thread_exit(); 728a54e85fdSJeff Roberson if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 729a54e85fdSJeff Roberson if (p->p_numthreads == p->p_suspcount + 1) { 730a54e85fdSJeff Roberson thread_lock(p->p_singlethread); 7317847a9daSJohn Baldwin wakeup_swapper = 732a54e85fdSJeff Roberson thread_unsuspend_one(p->p_singlethread); 733a54e85fdSJeff Roberson thread_unlock(p->p_singlethread); 7347847a9daSJohn Baldwin if (wakeup_swapper) 7357847a9daSJohn Baldwin kick_proc0(); 736a54e85fdSJeff Roberson } 737a54e85fdSJeff Roberson } 7383f9be10eSDavid Xu PROC_UNLOCK(p); 7397b4a950aSDavid Xu thread_lock(td); 74044990b8cSJulian Elischer /* 74144990b8cSJulian Elischer * When a thread suspends, it just 742ad1e7d28SJulian Elischer * gets taken off all queues. 74344990b8cSJulian Elischer */ 74471fad9fdSJulian Elischer thread_suspend_one(td); 745906ac69dSDavid Xu if (return_instead == 0) { 746906ac69dSDavid Xu p->p_boundary_count++; 747906ac69dSDavid Xu td->td_flags |= TDF_BOUNDARY; 748cf19bf91SJulian Elischer } 7497b4a950aSDavid Xu PROC_SUNLOCK(p); 7508df78c41SJeff Roberson mi_switch(SW_INVOL | SWT_SUSPEND, NULL); 751a54e85fdSJeff Roberson if (return_instead == 0) 752906ac69dSDavid Xu td->td_flags &= ~TDF_BOUNDARY; 753a54e85fdSJeff Roberson thread_unlock(td); 75444990b8cSJulian Elischer PROC_LOCK(p); 755a54e85fdSJeff Roberson if (return_instead == 0) 756a54e85fdSJeff Roberson p->p_boundary_count--; 75744990b8cSJulian Elischer } 75844990b8cSJulian Elischer return (0); 75944990b8cSJulian Elischer } 76044990b8cSJulian Elischer 76135c32a76SDavid Xu void 762a54e85fdSJeff Roberson thread_suspend_switch(struct thread *td) 763a54e85fdSJeff Roberson { 764a54e85fdSJeff Roberson struct proc *p; 765a54e85fdSJeff Roberson 766a54e85fdSJeff Roberson p = td->td_proc; 767a54e85fdSJeff Roberson KASSERT(!TD_IS_SUSPENDED(td), ("already suspended")); 768a54e85fdSJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 7697b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_OWNED); 770a54e85fdSJeff Roberson /* 771a54e85fdSJeff Roberson * We implement thread_suspend_one in stages here to avoid 772a54e85fdSJeff Roberson * dropping the proc lock while the thread lock is owned. 773a54e85fdSJeff Roberson */ 774a54e85fdSJeff Roberson thread_stopped(p); 775a54e85fdSJeff Roberson p->p_suspcount++; 7763f9be10eSDavid Xu PROC_UNLOCK(p); 7777b4a950aSDavid Xu thread_lock(td); 778b7edba77SJeff Roberson td->td_flags &= ~TDF_NEEDSUSPCHK; 779a54e85fdSJeff Roberson TD_SET_SUSPENDED(td); 780c5aa6b58SJeff Roberson sched_sleep(td, 0); 7817b4a950aSDavid Xu PROC_SUNLOCK(p); 782a54e85fdSJeff Roberson DROP_GIANT(); 7838df78c41SJeff Roberson mi_switch(SW_VOL | SWT_SUSPEND, NULL); 784a54e85fdSJeff Roberson thread_unlock(td); 785a54e85fdSJeff Roberson PICKUP_GIANT(); 786a54e85fdSJeff Roberson PROC_LOCK(p); 7877b4a950aSDavid Xu PROC_SLOCK(p); 788a54e85fdSJeff Roberson } 789a54e85fdSJeff Roberson 790a54e85fdSJeff Roberson void 79135c32a76SDavid Xu thread_suspend_one(struct thread *td) 79235c32a76SDavid Xu { 79335c32a76SDavid Xu struct proc *p = td->td_proc; 79435c32a76SDavid Xu 7957b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_OWNED); 796a54e85fdSJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 797e574e444SDavid Xu KASSERT(!TD_IS_SUSPENDED(td), ("already suspended")); 79835c32a76SDavid Xu p->p_suspcount++; 799b7edba77SJeff Roberson td->td_flags &= ~TDF_NEEDSUSPCHK; 80071fad9fdSJulian Elischer TD_SET_SUSPENDED(td); 801c5aa6b58SJeff Roberson sched_sleep(td, 0); 80235c32a76SDavid Xu } 80335c32a76SDavid Xu 8047847a9daSJohn Baldwin int 80535c32a76SDavid Xu thread_unsuspend_one(struct thread *td) 80635c32a76SDavid Xu { 80735c32a76SDavid Xu struct proc *p = td->td_proc; 80835c32a76SDavid Xu 8097b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_OWNED); 810a54e85fdSJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 811ad1e7d28SJulian Elischer KASSERT(TD_IS_SUSPENDED(td), ("Thread not suspended")); 81271fad9fdSJulian Elischer TD_CLR_SUSPENDED(td); 81335c32a76SDavid Xu p->p_suspcount--; 8147847a9daSJohn Baldwin return (setrunnable(td)); 81535c32a76SDavid Xu } 81635c32a76SDavid Xu 81744990b8cSJulian Elischer /* 81844990b8cSJulian Elischer * Allow all threads blocked by single threading to continue running. 81944990b8cSJulian Elischer */ 82044990b8cSJulian Elischer void 82144990b8cSJulian Elischer thread_unsuspend(struct proc *p) 82244990b8cSJulian Elischer { 82344990b8cSJulian Elischer struct thread *td; 8247847a9daSJohn Baldwin int wakeup_swapper; 82544990b8cSJulian Elischer 82644990b8cSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 8277b4a950aSDavid Xu PROC_SLOCK_ASSERT(p, MA_OWNED); 8287847a9daSJohn Baldwin wakeup_swapper = 0; 82944990b8cSJulian Elischer if (!P_SHOULDSTOP(p)) { 830ad1e7d28SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 831a54e85fdSJeff Roberson thread_lock(td); 832ad1e7d28SJulian Elischer if (TD_IS_SUSPENDED(td)) { 8337847a9daSJohn Baldwin wakeup_swapper |= thread_unsuspend_one(td); 83444990b8cSJulian Elischer } 835a54e85fdSJeff Roberson thread_unlock(td); 836ad1e7d28SJulian Elischer } 8371279572aSDavid Xu } else if ((P_SHOULDSTOP(p) == P_STOPPED_SINGLE) && 83844990b8cSJulian Elischer (p->p_numthreads == p->p_suspcount)) { 83944990b8cSJulian Elischer /* 84044990b8cSJulian Elischer * Stopping everything also did the job for the single 84144990b8cSJulian Elischer * threading request. Now we've downgraded to single-threaded, 84244990b8cSJulian Elischer * let it continue. 84344990b8cSJulian Elischer */ 844a54e85fdSJeff Roberson thread_lock(p->p_singlethread); 8457847a9daSJohn Baldwin wakeup_swapper = thread_unsuspend_one(p->p_singlethread); 846a54e85fdSJeff Roberson thread_unlock(p->p_singlethread); 84744990b8cSJulian Elischer } 8487847a9daSJohn Baldwin if (wakeup_swapper) 8497847a9daSJohn Baldwin kick_proc0(); 85044990b8cSJulian Elischer } 85144990b8cSJulian Elischer 852ed062c8dSJulian Elischer /* 853ed062c8dSJulian Elischer * End the single threading mode.. 854ed062c8dSJulian Elischer */ 85544990b8cSJulian Elischer void 85644990b8cSJulian Elischer thread_single_end(void) 85744990b8cSJulian Elischer { 85844990b8cSJulian Elischer struct thread *td; 85944990b8cSJulian Elischer struct proc *p; 8607847a9daSJohn Baldwin int wakeup_swapper; 86144990b8cSJulian Elischer 86244990b8cSJulian Elischer td = curthread; 86344990b8cSJulian Elischer p = td->td_proc; 86444990b8cSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 865906ac69dSDavid Xu p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT | P_SINGLE_BOUNDARY); 8667b4a950aSDavid Xu PROC_SLOCK(p); 86744990b8cSJulian Elischer p->p_singlethread = NULL; 8687847a9daSJohn Baldwin wakeup_swapper = 0; 86949539972SJulian Elischer /* 8707847a9daSJohn Baldwin * If there are other threads they may now run, 87149539972SJulian Elischer * unless of course there is a blanket 'stop order' 87249539972SJulian Elischer * on the process. The single threader must be allowed 87349539972SJulian Elischer * to continue however as this is a bad place to stop. 87449539972SJulian Elischer */ 87549539972SJulian Elischer if ((p->p_numthreads != 1) && (!P_SHOULDSTOP(p))) { 876ad1e7d28SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 877a54e85fdSJeff Roberson thread_lock(td); 878ad1e7d28SJulian Elischer if (TD_IS_SUSPENDED(td)) { 8797847a9daSJohn Baldwin wakeup_swapper |= thread_unsuspend_one(td); 88044990b8cSJulian Elischer } 881a54e85fdSJeff Roberson thread_unlock(td); 88249539972SJulian Elischer } 883ad1e7d28SJulian Elischer } 8847b4a950aSDavid Xu PROC_SUNLOCK(p); 8857847a9daSJohn Baldwin if (wakeup_swapper) 8867847a9daSJohn Baldwin kick_proc0(); 88749539972SJulian Elischer } 8884fc21c09SDaniel Eischen 88944355392SDavid Xu struct thread * 89044355392SDavid Xu thread_find(struct proc *p, lwpid_t tid) 89144355392SDavid Xu { 89244355392SDavid Xu struct thread *td; 89344355392SDavid Xu 89444355392SDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 89544355392SDavid Xu FOREACH_THREAD_IN_PROC(p, td) { 89644355392SDavid Xu if (td->td_tid == tid) 89744355392SDavid Xu break; 89844355392SDavid Xu } 89944355392SDavid Xu return (td); 90044355392SDavid Xu } 901