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 29677b542eSDavid E. O'Brien #include <sys/cdefs.h> 30677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 31677b542eSDavid E. O'Brien 3244990b8cSJulian Elischer #include <sys/param.h> 3344990b8cSJulian Elischer #include <sys/systm.h> 3444990b8cSJulian Elischer #include <sys/kernel.h> 3544990b8cSJulian Elischer #include <sys/lock.h> 3644990b8cSJulian Elischer #include <sys/mutex.h> 3744990b8cSJulian Elischer #include <sys/proc.h> 38e170bfdaSDavid Xu #include <sys/resourcevar.h> 3994e0a4cdSJulian Elischer #include <sys/smp.h> 4044990b8cSJulian Elischer #include <sys/sysctl.h> 41de028f5aSJeff Roberson #include <sys/sched.h> 4244f3b092SJohn Baldwin #include <sys/sleepqueue.h> 43961a7b24SJohn Baldwin #include <sys/turnstile.h> 4444990b8cSJulian Elischer #include <sys/ktr.h> 45bc8e6d81SDavid Xu #include <sys/umtx.h> 4644990b8cSJulian Elischer 47911b84b0SRobert Watson #include <security/audit/audit.h> 48911b84b0SRobert Watson 4944990b8cSJulian Elischer #include <vm/vm.h> 5049a2507bSAlan Cox #include <vm/vm_extern.h> 5144990b8cSJulian Elischer #include <vm/uma.h> 5202fb42b0SPeter Wemm 538460a577SJohn Birrell /* 548460a577SJohn Birrell * thread related storage. 558460a577SJohn Birrell */ 5644990b8cSJulian Elischer static uma_zone_t thread_zone; 5744990b8cSJulian Elischer 584f0db5e0SJulian Elischer /* DEBUG ONLY */ 5944990b8cSJulian Elischer SYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW, 0, "thread allocation"); 60696058c3SJulian Elischer static int thread_debug = 0; 61696058c3SJulian Elischer SYSCTL_INT(_kern_threads, OID_AUTO, debug, CTLFLAG_RW, 62696058c3SJulian Elischer &thread_debug, 0, "thread debug"); 63fdc5ecd2SDavid Xu 64345ad866SJulian Elischer int max_threads_per_proc = 1500; 65fdc5ecd2SDavid Xu SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_per_proc, CTLFLAG_RW, 664f0db5e0SJulian Elischer &max_threads_per_proc, 0, "Limit on threads per proc"); 674f0db5e0SJulian Elischer 68ed062c8dSJulian Elischer int max_groups_per_proc = 1500; 69fdc5ecd2SDavid Xu SYSCTL_INT(_kern_threads, OID_AUTO, max_groups_per_proc, CTLFLAG_RW, 70fdc5ecd2SDavid Xu &max_groups_per_proc, 0, "Limit on thread groups per proc"); 71fdc5ecd2SDavid Xu 72345ad866SJulian Elischer int max_threads_hits; 730252d203SDavid Xu SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_hits, CTLFLAG_RD, 740252d203SDavid Xu &max_threads_hits, 0, ""); 750252d203SDavid Xu 768460a577SJohn Birrell #ifdef KSE 7794e0a4cdSJulian Elischer int virtual_cpu; 7894e0a4cdSJulian Elischer 798460a577SJohn Birrell #endif 805215b187SJeff Roberson TAILQ_HEAD(, thread) zombie_threads = TAILQ_HEAD_INITIALIZER(zombie_threads); 815215b187SJeff Roberson struct mtx kse_zombie_lock; 825215b187SJeff Roberson MTX_SYSINIT(kse_zombie_lock, &kse_zombie_lock, "kse zombie lock", MTX_SPIN); 8344990b8cSJulian Elischer 848460a577SJohn Birrell #ifdef KSE 8594e0a4cdSJulian Elischer static int 8694e0a4cdSJulian Elischer sysctl_kse_virtual_cpu(SYSCTL_HANDLER_ARGS) 8794e0a4cdSJulian Elischer { 8894e0a4cdSJulian Elischer int error, new_val; 8994e0a4cdSJulian Elischer int def_val; 9094e0a4cdSJulian Elischer 9194e0a4cdSJulian Elischer def_val = mp_ncpus; 9294e0a4cdSJulian Elischer if (virtual_cpu == 0) 9394e0a4cdSJulian Elischer new_val = def_val; 9494e0a4cdSJulian Elischer else 9594e0a4cdSJulian Elischer new_val = virtual_cpu; 9694e0a4cdSJulian Elischer error = sysctl_handle_int(oidp, &new_val, 0, req); 9794e0a4cdSJulian Elischer if (error != 0 || req->newptr == NULL) 9894e0a4cdSJulian Elischer return (error); 9994e0a4cdSJulian Elischer if (new_val < 0) 10094e0a4cdSJulian Elischer return (EINVAL); 10194e0a4cdSJulian Elischer virtual_cpu = new_val; 10294e0a4cdSJulian Elischer return (0); 10394e0a4cdSJulian Elischer } 10494e0a4cdSJulian Elischer 10594e0a4cdSJulian Elischer /* DEBUG ONLY */ 10694e0a4cdSJulian Elischer SYSCTL_PROC(_kern_threads, OID_AUTO, virtual_cpu, CTLTYPE_INT|CTLFLAG_RW, 10794e0a4cdSJulian Elischer 0, sizeof(virtual_cpu), sysctl_kse_virtual_cpu, "I", 10894e0a4cdSJulian Elischer "debug virtual cpus"); 1098460a577SJohn Birrell #endif 1105c8329edSJulian Elischer 111fdcac928SMarcel Moolenaar struct mtx tid_lock; 1121ea7a6f8SPoul-Henning Kamp static struct unrhdr *tid_unrhdr; 113fdcac928SMarcel Moolenaar 114fdcac928SMarcel Moolenaar /* 115696058c3SJulian Elischer * Prepare a thread for use. 11644990b8cSJulian Elischer */ 117b23f72e9SBrian Feldman static int 118b23f72e9SBrian Feldman thread_ctor(void *mem, int size, void *arg, int flags) 11944990b8cSJulian Elischer { 12044990b8cSJulian Elischer struct thread *td; 12144990b8cSJulian Elischer 12244990b8cSJulian Elischer td = (struct thread *)mem; 12371fad9fdSJulian Elischer td->td_state = TDS_INACTIVE; 124060563ecSJulian Elischer td->td_oncpu = NOCPU; 1256c27c603SJuli Mallett 126773eff9dSPoul-Henning Kamp td->td_tid = alloc_unr(tid_unrhdr); 127773eff9dSPoul-Henning Kamp 1286c27c603SJuli Mallett /* 1296c27c603SJuli Mallett * Note that td_critnest begins life as 1 because the thread is not 1306c27c603SJuli Mallett * running and is thereby implicitly waiting to be on the receiving 1316c27c603SJuli Mallett * end of a context switch. A context switch must occur inside a 1326c27c603SJuli Mallett * critical section, and in fact, includes hand-off of the sched_lock. 1336c27c603SJuli Mallett * After a context switch to a newly created thread, it will release 1346c27c603SJuli Mallett * sched_lock for the first time, and its td_critnest will hit 0 for 1356c27c603SJuli Mallett * the first time. This happens on the far end of a context switch, 1366c27c603SJuli Mallett * and when it context switches away from itself, it will in fact go 1376c27c603SJuli Mallett * back into a critical section, and hand off the sched lock to the 1386c27c603SJuli Mallett * next thread. 1396c27c603SJuli Mallett */ 140139b7550SJohn Baldwin td->td_critnest = 1; 141911b84b0SRobert Watson 142911b84b0SRobert Watson #ifdef AUDIT 143911b84b0SRobert Watson audit_thread_alloc(td); 144911b84b0SRobert Watson #endif 145d10183d9SDavid Xu umtx_thread_alloc(td); 146b23f72e9SBrian Feldman return (0); 14744990b8cSJulian Elischer } 14844990b8cSJulian Elischer 14944990b8cSJulian Elischer /* 15044990b8cSJulian Elischer * Reclaim a thread after use. 15144990b8cSJulian Elischer */ 15244990b8cSJulian Elischer static void 15344990b8cSJulian Elischer thread_dtor(void *mem, int size, void *arg) 15444990b8cSJulian Elischer { 15544990b8cSJulian Elischer struct thread *td; 15644990b8cSJulian Elischer 15744990b8cSJulian Elischer td = (struct thread *)mem; 15844990b8cSJulian Elischer 15944990b8cSJulian Elischer #ifdef INVARIANTS 16044990b8cSJulian Elischer /* Verify that this thread is in a safe state to free. */ 16144990b8cSJulian Elischer switch (td->td_state) { 16271fad9fdSJulian Elischer case TDS_INHIBITED: 16371fad9fdSJulian Elischer case TDS_RUNNING: 16471fad9fdSJulian Elischer case TDS_CAN_RUN: 16544990b8cSJulian Elischer case TDS_RUNQ: 16644990b8cSJulian Elischer /* 16744990b8cSJulian Elischer * We must never unlink a thread that is in one of 16844990b8cSJulian Elischer * these states, because it is currently active. 16944990b8cSJulian Elischer */ 17044990b8cSJulian Elischer panic("bad state for thread unlinking"); 17144990b8cSJulian Elischer /* NOTREACHED */ 17271fad9fdSJulian Elischer case TDS_INACTIVE: 17344990b8cSJulian Elischer break; 17444990b8cSJulian Elischer default: 17544990b8cSJulian Elischer panic("bad thread state"); 17644990b8cSJulian Elischer /* NOTREACHED */ 17744990b8cSJulian Elischer } 17844990b8cSJulian Elischer #endif 1796e8525ceSRobert Watson #ifdef AUDIT 1806e8525ceSRobert Watson audit_thread_free(td); 1816e8525ceSRobert Watson #endif 182773eff9dSPoul-Henning Kamp free_unr(tid_unrhdr, td->td_tid); 183ed062c8dSJulian Elischer sched_newthread(td); 18444990b8cSJulian Elischer } 18544990b8cSJulian Elischer 18644990b8cSJulian Elischer /* 18744990b8cSJulian Elischer * Initialize type-stable parts of a thread (when newly created). 18844990b8cSJulian Elischer */ 189b23f72e9SBrian Feldman static int 190b23f72e9SBrian Feldman thread_init(void *mem, int size, int flags) 19144990b8cSJulian Elischer { 19244990b8cSJulian Elischer struct thread *td; 19344990b8cSJulian Elischer 19444990b8cSJulian Elischer td = (struct thread *)mem; 195247aba24SMarcel Moolenaar 19649a2507bSAlan Cox vm_thread_new(td, 0); 19744990b8cSJulian Elischer cpu_thread_setup(td); 19844f3b092SJohn Baldwin td->td_sleepqueue = sleepq_alloc(); 199961a7b24SJohn Baldwin td->td_turnstile = turnstile_alloc(); 200de028f5aSJeff Roberson td->td_sched = (struct td_sched *)&td[1]; 201ed062c8dSJulian Elischer sched_newthread(td); 202d10183d9SDavid Xu umtx_thread_init(td); 203b23f72e9SBrian Feldman return (0); 20444990b8cSJulian Elischer } 20544990b8cSJulian Elischer 20644990b8cSJulian Elischer /* 20744990b8cSJulian Elischer * Tear down type-stable parts of a thread (just before being discarded). 20844990b8cSJulian Elischer */ 20944990b8cSJulian Elischer static void 21044990b8cSJulian Elischer thread_fini(void *mem, int size) 21144990b8cSJulian Elischer { 21244990b8cSJulian Elischer struct thread *td; 21344990b8cSJulian Elischer 21444990b8cSJulian Elischer td = (struct thread *)mem; 215961a7b24SJohn Baldwin turnstile_free(td->td_turnstile); 21644f3b092SJohn Baldwin sleepq_free(td->td_sleepqueue); 217d10183d9SDavid Xu umtx_thread_fini(td); 21849a2507bSAlan Cox vm_thread_dispose(td); 21944990b8cSJulian Elischer } 2205215b187SJeff Roberson 2215c8329edSJulian Elischer /* 2225215b187SJeff Roberson * For a newly created process, 2235215b187SJeff Roberson * link up all the structures and its initial threads etc. 224ed062c8dSJulian Elischer * called from: 225ed062c8dSJulian Elischer * {arch}/{arch}/machdep.c ia64_init(), init386() etc. 226ed062c8dSJulian Elischer * proc_dtor() (should go away) 227ed062c8dSJulian Elischer * proc_init() 2285c8329edSJulian Elischer */ 2295c8329edSJulian Elischer void 2308460a577SJohn Birrell proc_linkup(struct proc *p, struct thread *td) 2315c8329edSJulian Elischer { 2325c8329edSJulian Elischer TAILQ_INIT(&p->p_threads); /* all threads in proc */ 233ad1e7d28SJulian Elischer TAILQ_INIT(&p->p_upcalls); /* upcall list */ 2349104847fSDavid Xu sigqueue_init(&p->p_sigqueue, p); 235ebceaf6dSDavid Xu p->p_ksi = ksiginfo_alloc(1); 236ebceaf6dSDavid Xu if (p->p_ksi != NULL) { 2375c474517SDavid Xu /* XXX p_ksi may be null if ksiginfo zone is not ready */ 238ebceaf6dSDavid Xu p->p_ksi->ksi_flags = KSI_EXT | KSI_INS; 239ebceaf6dSDavid Xu } 240b2f92ef9SDavid Xu LIST_INIT(&p->p_mqnotifier); 2415c8329edSJulian Elischer p->p_numthreads = 0; 2428460a577SJohn Birrell thread_link(td, p); 2435c8329edSJulian Elischer } 2445c8329edSJulian Elischer 2455c8329edSJulian Elischer /* 24644990b8cSJulian Elischer * Initialize global thread allocation resources. 24744990b8cSJulian Elischer */ 24844990b8cSJulian Elischer void 24944990b8cSJulian Elischer threadinit(void) 25044990b8cSJulian Elischer { 25144990b8cSJulian Elischer 2521ea7a6f8SPoul-Henning Kamp mtx_init(&tid_lock, "TID lock", NULL, MTX_DEF); 2531ea7a6f8SPoul-Henning Kamp tid_unrhdr = new_unrhdr(PID_MAX + 1, INT_MAX, &tid_lock); 2541ea7a6f8SPoul-Henning Kamp 255de028f5aSJeff Roberson thread_zone = uma_zcreate("THREAD", sched_sizeof_thread(), 25644990b8cSJulian Elischer thread_ctor, thread_dtor, thread_init, thread_fini, 25744990b8cSJulian Elischer UMA_ALIGN_CACHE, 0); 2588460a577SJohn Birrell #ifdef KSE 259ed062c8dSJulian Elischer kseinit(); /* set up kse specific stuff e.g. upcall zone*/ 2608460a577SJohn Birrell #endif 26144990b8cSJulian Elischer } 26244990b8cSJulian Elischer 26344990b8cSJulian Elischer /* 2641faf202eSJulian Elischer * Stash an embarasingly extra thread into the zombie thread queue. 265ad1e7d28SJulian Elischer * Use the slpq as that must be unused by now. 26644990b8cSJulian Elischer */ 26744990b8cSJulian Elischer void 26844990b8cSJulian Elischer thread_stash(struct thread *td) 26944990b8cSJulian Elischer { 2705215b187SJeff Roberson mtx_lock_spin(&kse_zombie_lock); 271ad1e7d28SJulian Elischer TAILQ_INSERT_HEAD(&zombie_threads, td, td_slpq); 2725215b187SJeff Roberson mtx_unlock_spin(&kse_zombie_lock); 27344990b8cSJulian Elischer } 27444990b8cSJulian Elischer 2755c8329edSJulian Elischer /* 2765215b187SJeff Roberson * Reap zombie kse resource. 27744990b8cSJulian Elischer */ 27844990b8cSJulian Elischer void 27944990b8cSJulian Elischer thread_reap(void) 28044990b8cSJulian Elischer { 2815c8329edSJulian Elischer struct thread *td_first, *td_next; 28244990b8cSJulian Elischer 28344990b8cSJulian Elischer /* 2845215b187SJeff Roberson * Don't even bother to lock if none at this instant, 2855215b187SJeff Roberson * we really don't care about the next instant.. 28644990b8cSJulian Elischer */ 2878460a577SJohn Birrell if (!TAILQ_EMPTY(&zombie_threads)) { 2885215b187SJeff Roberson mtx_lock_spin(&kse_zombie_lock); 2895c8329edSJulian Elischer td_first = TAILQ_FIRST(&zombie_threads); 2905c8329edSJulian Elischer if (td_first) 2915c8329edSJulian Elischer TAILQ_INIT(&zombie_threads); 2925215b187SJeff Roberson mtx_unlock_spin(&kse_zombie_lock); 2935c8329edSJulian Elischer while (td_first) { 294ad1e7d28SJulian Elischer td_next = TAILQ_NEXT(td_first, td_slpq); 2955215b187SJeff Roberson if (td_first->td_ucred) 2965215b187SJeff Roberson crfree(td_first->td_ucred); 2975c8329edSJulian Elischer thread_free(td_first); 2985c8329edSJulian Elischer td_first = td_next; 29944990b8cSJulian Elischer } 30044990b8cSJulian Elischer } 301ed062c8dSJulian Elischer } 30244990b8cSJulian Elischer 3034f0db5e0SJulian Elischer /* 30444990b8cSJulian Elischer * Allocate a thread. 30544990b8cSJulian Elischer */ 30644990b8cSJulian Elischer struct thread * 30744990b8cSJulian Elischer thread_alloc(void) 30844990b8cSJulian Elischer { 3098460a577SJohn Birrell 31044990b8cSJulian Elischer thread_reap(); /* check if any zombies to get */ 311a163d034SWarner Losh return (uma_zalloc(thread_zone, M_WAITOK)); 31244990b8cSJulian Elischer } 31344990b8cSJulian Elischer 3144f0db5e0SJulian Elischer 3154f0db5e0SJulian Elischer /* 31644990b8cSJulian Elischer * Deallocate a thread. 31744990b8cSJulian Elischer */ 31844990b8cSJulian Elischer void 31944990b8cSJulian Elischer thread_free(struct thread *td) 32044990b8cSJulian Elischer { 321696058c3SJulian Elischer 322696058c3SJulian Elischer cpu_thread_clean(td); 32344990b8cSJulian Elischer uma_zfree(thread_zone, td); 32444990b8cSJulian Elischer } 32544990b8cSJulian Elischer 32644990b8cSJulian Elischer /* 32744990b8cSJulian Elischer * Discard the current thread and exit from its context. 32894e0a4cdSJulian Elischer * Always called with scheduler locked. 32944990b8cSJulian Elischer * 33044990b8cSJulian Elischer * Because we can't free a thread while we're operating under its context, 331696058c3SJulian Elischer * push the current thread into our CPU's deadthread holder. This means 332696058c3SJulian Elischer * we needn't worry about someone else grabbing our context before we 33394e0a4cdSJulian Elischer * do a cpu_throw(). This may not be needed now as we are under schedlock. 33494e0a4cdSJulian Elischer * Maybe we can just do a thread_stash() as thr_exit1 does. 33594e0a4cdSJulian Elischer */ 33694e0a4cdSJulian Elischer /* XXX 33794e0a4cdSJulian Elischer * libthr expects its thread exit to return for the last 33894e0a4cdSJulian Elischer * thread, meaning that the program is back to non-threaded 33994e0a4cdSJulian Elischer * mode I guess. Because we do this (cpu_throw) unconditionally 34094e0a4cdSJulian Elischer * here, they have their own version of it. (thr_exit1()) 34194e0a4cdSJulian Elischer * that doesn't do it all if this was the last thread. 34294e0a4cdSJulian Elischer * It is also called from thread_suspend_check(). 34394e0a4cdSJulian Elischer * Of course in the end, they end up coming here through exit1 34494e0a4cdSJulian Elischer * anyhow.. After fixing 'thr' to play by the rules we should be able 34594e0a4cdSJulian Elischer * to merge these two functions together. 346ed062c8dSJulian Elischer * 347ed062c8dSJulian Elischer * called from: 348ed062c8dSJulian Elischer * exit1() 349ed062c8dSJulian Elischer * kse_exit() 350ed062c8dSJulian Elischer * thr_exit() 3518460a577SJohn Birrell * ifdef KSE 352ed062c8dSJulian Elischer * thread_user_enter() 353ed062c8dSJulian Elischer * thread_userret() 3548460a577SJohn Birrell * endif 355ed062c8dSJulian Elischer * thread_suspend_check() 35644990b8cSJulian Elischer */ 35744990b8cSJulian Elischer void 35844990b8cSJulian Elischer thread_exit(void) 35944990b8cSJulian Elischer { 360e170bfdaSDavid Xu uint64_t new_switchtime; 36144990b8cSJulian Elischer struct thread *td; 36244990b8cSJulian Elischer struct proc *p; 36344990b8cSJulian Elischer 36444990b8cSJulian Elischer td = curthread; 36544990b8cSJulian Elischer p = td->td_proc; 36644990b8cSJulian Elischer 36744990b8cSJulian Elischer mtx_assert(&sched_lock, MA_OWNED); 368ed062c8dSJulian Elischer mtx_assert(&Giant, MA_NOTOWNED); 36944990b8cSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 370ed062c8dSJulian Elischer KASSERT(p != NULL, ("thread exiting without a process")); 371cc701b73SRobert Watson CTR3(KTR_PROC, "thread_exit: thread %p (pid %ld, %s)", td, 372cc701b73SRobert Watson (long)p->p_pid, p->p_comm); 3739104847fSDavid Xu KASSERT(TAILQ_EMPTY(&td->td_sigqueue.sq_list), ("signal pending")); 37444990b8cSJulian Elischer 37589964dd2SRobert Watson #ifdef AUDIT 37689964dd2SRobert Watson AUDIT_SYSCALL_EXIT(0, td); 37789964dd2SRobert Watson #endif 37889964dd2SRobert Watson 3798460a577SJohn Birrell #ifdef KSE 38048bfcdddSJulian Elischer if (td->td_standin != NULL) { 381ed062c8dSJulian Elischer /* 382ed062c8dSJulian Elischer * Note that we don't need to free the cred here as it 383ed062c8dSJulian Elischer * is done in thread_reap(). 384ed062c8dSJulian Elischer */ 38548bfcdddSJulian Elischer thread_stash(td->td_standin); 38648bfcdddSJulian Elischer td->td_standin = NULL; 38748bfcdddSJulian Elischer } 3888460a577SJohn Birrell #endif 38948bfcdddSJulian Elischer 390d10183d9SDavid Xu umtx_thread_exit(td); 391d10183d9SDavid Xu 392ed062c8dSJulian Elischer /* 393ed062c8dSJulian Elischer * drop FPU & debug register state storage, or any other 394ed062c8dSJulian Elischer * architecture specific resources that 395ed062c8dSJulian Elischer * would not be on a new untouched process. 396ed062c8dSJulian Elischer */ 39744990b8cSJulian Elischer cpu_thread_exit(td); /* XXXSMP */ 39844990b8cSJulian Elischer 3998460a577SJohn Birrell #ifdef KSE 4001faf202eSJulian Elischer /* 401ed062c8dSJulian Elischer * The thread is exiting. scheduler can release its stuff 402ed062c8dSJulian Elischer * and collect stats etc. 403e170bfdaSDavid Xu * XXX this is not very right, since PROC_UNLOCK may still 404e170bfdaSDavid Xu * need scheduler stuff. 405ed062c8dSJulian Elischer */ 406ed062c8dSJulian Elischer sched_thread_exit(td); 4078460a577SJohn Birrell #endif 408ed062c8dSJulian Elischer 409e170bfdaSDavid Xu /* Do the same timestamp bookkeeping that mi_switch() would do. */ 410e170bfdaSDavid Xu new_switchtime = cpu_ticks(); 411e170bfdaSDavid Xu p->p_rux.rux_runtime += (new_switchtime - PCPU_GET(switchtime)); 412e170bfdaSDavid Xu p->p_rux.rux_uticks += td->td_uticks; 413e170bfdaSDavid Xu p->p_rux.rux_sticks += td->td_sticks; 414e170bfdaSDavid Xu p->p_rux.rux_iticks += td->td_iticks; 415e170bfdaSDavid Xu PCPU_SET(switchtime, new_switchtime); 416e170bfdaSDavid Xu PCPU_SET(switchticks, ticks); 417e170bfdaSDavid Xu cnt.v_swtch++; 418e170bfdaSDavid Xu 419e170bfdaSDavid Xu /* Add our usage into the usage of all our children. */ 420e170bfdaSDavid Xu if (p->p_numthreads == 1) 421e170bfdaSDavid Xu ruadd(p->p_ru, &p->p_rux, &p->p_stats->p_cru, &p->p_crux); 422e170bfdaSDavid Xu 423ed062c8dSJulian Elischer /* 4241faf202eSJulian Elischer * The last thread is left attached to the process 4251faf202eSJulian Elischer * So that the whole bundle gets recycled. Skip 426ed062c8dSJulian Elischer * all this stuff if we never had threads. 427ed062c8dSJulian Elischer * EXIT clears all sign of other threads when 428ed062c8dSJulian Elischer * it goes to single threading, so the last thread always 429ed062c8dSJulian Elischer * takes the short path. 4301faf202eSJulian Elischer */ 431ed062c8dSJulian Elischer if (p->p_flag & P_HADTHREADS) { 4321faf202eSJulian Elischer if (p->p_numthreads > 1) { 433d3a0bd78SJulian Elischer thread_unlink(td); 434ed062c8dSJulian Elischer 435ed062c8dSJulian Elischer sched_exit_thread(FIRST_THREAD_IN_PROC(p), td); 436ed062c8dSJulian Elischer 437ed062c8dSJulian Elischer /* 43844990b8cSJulian Elischer * The test below is NOT true if we are the 4391faf202eSJulian Elischer * sole exiting thread. P_STOPPED_SNGL is unset 44044990b8cSJulian Elischer * in exit1() after it is the only survivor. 44144990b8cSJulian Elischer */ 4421279572aSDavid Xu if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 44344990b8cSJulian Elischer if (p->p_numthreads == p->p_suspcount) { 44471fad9fdSJulian Elischer thread_unsuspend_one(p->p_singlethread); 44544990b8cSJulian Elischer } 44644990b8cSJulian Elischer } 44748bfcdddSJulian Elischer 4488460a577SJohn Birrell #ifdef KSE 4495215b187SJeff Roberson /* 4505215b187SJeff Roberson * Because each upcall structure has an owner thread, 4515215b187SJeff Roberson * owner thread exits only when process is in exiting 4525215b187SJeff Roberson * state, so upcall to userland is no longer needed, 4535215b187SJeff Roberson * deleting upcall structure is safe here. 4545215b187SJeff Roberson * So when all threads in a group is exited, all upcalls 4555215b187SJeff Roberson * in the group should be automatically freed. 456ed062c8dSJulian Elischer * XXXKSE This is a KSE thing and should be exported 457ed062c8dSJulian Elischer * there somehow. 4585215b187SJeff Roberson */ 4595215b187SJeff Roberson upcall_remove(td); 460ad1e7d28SJulian Elischer #endif 4616f8132a8SJulian Elischer 4626f8132a8SJulian Elischer PROC_UNLOCK(p); 463696058c3SJulian Elischer PCPU_SET(deadthread, td); 4641faf202eSJulian Elischer } else { 465ed062c8dSJulian Elischer /* 466ed062c8dSJulian Elischer * The last thread is exiting.. but not through exit() 467ed062c8dSJulian Elischer * what should we do? 468ed062c8dSJulian Elischer * Theoretically this can't happen 469ed062c8dSJulian Elischer * exit1() - clears threading flags before coming here 470ed062c8dSJulian Elischer * kse_exit() - treats last thread specially 471ed062c8dSJulian Elischer * thr_exit() - treats last thread specially 4728460a577SJohn Birrell * ifdef KSE 473ed062c8dSJulian Elischer * thread_user_enter() - only if more exist 474ed062c8dSJulian Elischer * thread_userret() - only if more exist 4758460a577SJohn Birrell * endif 476ed062c8dSJulian Elischer * thread_suspend_check() - only if more exist 477ed062c8dSJulian Elischer */ 478ed062c8dSJulian Elischer panic ("thread_exit: Last thread exiting on its own"); 479ed062c8dSJulian Elischer } 480ed062c8dSJulian Elischer } else { 481ed062c8dSJulian Elischer /* 482ed062c8dSJulian Elischer * non threaded process comes here. 483ed062c8dSJulian Elischer * This includes an EX threaded process that is coming 484ed062c8dSJulian Elischer * here via exit1(). (exit1 dethreads the proc first). 485ed062c8dSJulian Elischer */ 4861faf202eSJulian Elischer PROC_UNLOCK(p); 4871faf202eSJulian Elischer } 488dcc9954eSJulian Elischer td->td_state = TDS_INACTIVE; 489732d9528SJulian Elischer CTR1(KTR_PROC, "thread_exit: cpu_throw() thread %p", td); 490cc66ebe2SPeter Wemm cpu_throw(td, choosethread()); 491cc66ebe2SPeter Wemm panic("I'm a teapot!"); 49244990b8cSJulian Elischer /* NOTREACHED */ 49344990b8cSJulian Elischer } 49444990b8cSJulian Elischer 49544990b8cSJulian Elischer /* 496696058c3SJulian Elischer * Do any thread specific cleanups that may be needed in wait() 49737814395SPeter Wemm * called with Giant, proc and schedlock not held. 498696058c3SJulian Elischer */ 499696058c3SJulian Elischer void 500696058c3SJulian Elischer thread_wait(struct proc *p) 501696058c3SJulian Elischer { 502696058c3SJulian Elischer struct thread *td; 503696058c3SJulian Elischer 50437814395SPeter Wemm mtx_assert(&Giant, MA_NOTOWNED); 50585495c72SJens Schweikhardt KASSERT((p->p_numthreads == 1), ("Multiple threads in wait1()")); 506696058c3SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 5078460a577SJohn Birrell #ifdef KSE 508696058c3SJulian Elischer if (td->td_standin != NULL) { 509b75b0311SJulian Elischer if (td->td_standin->td_ucred != NULL) { 510b75b0311SJulian Elischer crfree(td->td_standin->td_ucred); 511b75b0311SJulian Elischer td->td_standin->td_ucred = NULL; 512b75b0311SJulian Elischer } 513696058c3SJulian Elischer thread_free(td->td_standin); 514696058c3SJulian Elischer td->td_standin = NULL; 515696058c3SJulian Elischer } 5168460a577SJohn Birrell #endif 517696058c3SJulian Elischer cpu_thread_clean(td); 518ed062c8dSJulian Elischer crfree(td->td_ucred); 519696058c3SJulian Elischer } 520696058c3SJulian Elischer thread_reap(); /* check for zombie threads etc. */ 521696058c3SJulian Elischer } 522696058c3SJulian Elischer 523696058c3SJulian Elischer /* 52444990b8cSJulian Elischer * Link a thread to a process. 5251faf202eSJulian Elischer * set up anything that needs to be initialized for it to 5261faf202eSJulian Elischer * be used by the process. 52744990b8cSJulian Elischer * 52844990b8cSJulian Elischer * Note that we do not link to the proc's ucred here. 52944990b8cSJulian Elischer * The thread is linked as if running but no KSE assigned. 530ed062c8dSJulian Elischer * Called from: 531ed062c8dSJulian Elischer * proc_linkup() 532ed062c8dSJulian Elischer * thread_schedule_upcall() 533ed062c8dSJulian Elischer * thr_create() 53444990b8cSJulian Elischer */ 53544990b8cSJulian Elischer void 5368460a577SJohn Birrell thread_link(struct thread *td, struct proc *p) 53744990b8cSJulian Elischer { 53844990b8cSJulian Elischer 53971fad9fdSJulian Elischer td->td_state = TDS_INACTIVE; 54044990b8cSJulian Elischer td->td_proc = p; 5415215b187SJeff Roberson td->td_flags = 0; 54244990b8cSJulian Elischer 5431faf202eSJulian Elischer LIST_INIT(&td->td_contested); 5449104847fSDavid Xu sigqueue_init(&td->td_sigqueue, p); 545c06eb4e2SSam Leffler callout_init(&td->td_slpcallout, CALLOUT_MPSAFE); 54644990b8cSJulian Elischer TAILQ_INSERT_HEAD(&p->p_threads, td, td_plist); 54744990b8cSJulian Elischer p->p_numthreads++; 54844990b8cSJulian Elischer } 54944990b8cSJulian Elischer 550ed062c8dSJulian Elischer /* 551e5bedcefSJulian Elischer * Convert a process with one thread to an unthreaded process. 552e5bedcefSJulian Elischer * Called from: 553e5bedcefSJulian Elischer * thread_single(exit) (called from execve and exit) 554e5bedcefSJulian Elischer * kse_exit() XXX may need cleaning up wrt KSE stuff 555e5bedcefSJulian Elischer */ 556e5bedcefSJulian Elischer void 557e5bedcefSJulian Elischer thread_unthread(struct thread *td) 558e5bedcefSJulian Elischer { 559e5bedcefSJulian Elischer struct proc *p = td->td_proc; 560e5bedcefSJulian Elischer 561e5bedcefSJulian Elischer KASSERT((p->p_numthreads == 1), ("Unthreading with >1 threads")); 5628460a577SJohn Birrell #ifdef KSE 563e5bedcefSJulian Elischer upcall_remove(td); 564e5bedcefSJulian Elischer p->p_flag &= ~(P_SA|P_HADTHREADS); 565e5bedcefSJulian Elischer td->td_mailbox = NULL; 566e5bedcefSJulian Elischer td->td_pflags &= ~(TDP_SA | TDP_CAN_UNBIND); 567e5bedcefSJulian Elischer if (td->td_standin != NULL) { 568e5bedcefSJulian Elischer thread_stash(td->td_standin); 569e5bedcefSJulian Elischer td->td_standin = NULL; 570e5bedcefSJulian Elischer } 571ad1e7d28SJulian Elischer sched_set_concurrency(p, 1); 5728460a577SJohn Birrell #else 5738460a577SJohn Birrell p->p_flag &= ~P_HADTHREADS; 5748460a577SJohn Birrell #endif 575e5bedcefSJulian Elischer } 576e5bedcefSJulian Elischer 577e5bedcefSJulian Elischer /* 578ed062c8dSJulian Elischer * Called from: 579ed062c8dSJulian Elischer * thread_exit() 580ed062c8dSJulian Elischer */ 581d3a0bd78SJulian Elischer void 582d3a0bd78SJulian Elischer thread_unlink(struct thread *td) 583d3a0bd78SJulian Elischer { 584d3a0bd78SJulian Elischer struct proc *p = td->td_proc; 585d3a0bd78SJulian Elischer 586112afcb2SJohn Baldwin mtx_assert(&sched_lock, MA_OWNED); 587d3a0bd78SJulian Elischer TAILQ_REMOVE(&p->p_threads, td, td_plist); 588d3a0bd78SJulian Elischer p->p_numthreads--; 589d3a0bd78SJulian Elischer /* could clear a few other things here */ 5908460a577SJohn Birrell /* Must NOT clear links to proc! */ 5915c8329edSJulian Elischer } 5925c8329edSJulian Elischer 5935215b187SJeff Roberson /* 59444990b8cSJulian Elischer * Enforce single-threading. 59544990b8cSJulian Elischer * 59644990b8cSJulian Elischer * Returns 1 if the caller must abort (another thread is waiting to 59744990b8cSJulian Elischer * exit the process or similar). Process is locked! 59844990b8cSJulian Elischer * Returns 0 when you are successfully the only thread running. 59944990b8cSJulian Elischer * A process has successfully single threaded in the suspend mode when 60044990b8cSJulian Elischer * There are no threads in user mode. Threads in the kernel must be 60144990b8cSJulian Elischer * allowed to continue until they get to the user boundary. They may even 60244990b8cSJulian Elischer * copy out their return values and data before suspending. They may however be 603e2668f55SMaxim Konovalov * accelerated in reaching the user boundary as we will wake up 60444990b8cSJulian Elischer * any sleeping threads that are interruptable. (PCATCH). 60544990b8cSJulian Elischer */ 60644990b8cSJulian Elischer int 607906ac69dSDavid Xu thread_single(int mode) 60844990b8cSJulian Elischer { 60944990b8cSJulian Elischer struct thread *td; 61044990b8cSJulian Elischer struct thread *td2; 61144990b8cSJulian Elischer struct proc *p; 612ec008e96SDavid Xu int remaining; 61344990b8cSJulian Elischer 61444990b8cSJulian Elischer td = curthread; 61544990b8cSJulian Elischer p = td->td_proc; 61637814395SPeter Wemm mtx_assert(&Giant, MA_NOTOWNED); 61744990b8cSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 61844990b8cSJulian Elischer KASSERT((td != NULL), ("curthread is NULL")); 61944990b8cSJulian Elischer 620ed062c8dSJulian Elischer if ((p->p_flag & P_HADTHREADS) == 0) 62144990b8cSJulian Elischer return (0); 62244990b8cSJulian Elischer 623e3b9bf71SJulian Elischer /* Is someone already single threading? */ 624906ac69dSDavid Xu if (p->p_singlethread != NULL && p->p_singlethread != td) 62544990b8cSJulian Elischer return (1); 62644990b8cSJulian Elischer 627906ac69dSDavid Xu if (mode == SINGLE_EXIT) { 628906ac69dSDavid Xu p->p_flag |= P_SINGLE_EXIT; 629906ac69dSDavid Xu p->p_flag &= ~P_SINGLE_BOUNDARY; 630906ac69dSDavid Xu } else { 631906ac69dSDavid Xu p->p_flag &= ~P_SINGLE_EXIT; 632906ac69dSDavid Xu if (mode == SINGLE_BOUNDARY) 633906ac69dSDavid Xu p->p_flag |= P_SINGLE_BOUNDARY; 634906ac69dSDavid Xu else 635906ac69dSDavid Xu p->p_flag &= ~P_SINGLE_BOUNDARY; 636906ac69dSDavid Xu } 6371279572aSDavid Xu p->p_flag |= P_STOPPED_SINGLE; 63871fad9fdSJulian Elischer mtx_lock_spin(&sched_lock); 639112afcb2SJohn Baldwin p->p_singlethread = td; 640906ac69dSDavid Xu if (mode == SINGLE_EXIT) 641ec008e96SDavid Xu remaining = p->p_numthreads; 642906ac69dSDavid Xu else if (mode == SINGLE_BOUNDARY) 643906ac69dSDavid Xu remaining = p->p_numthreads - p->p_boundary_count; 644906ac69dSDavid Xu else 645ec008e96SDavid Xu remaining = p->p_numthreads - p->p_suspcount; 646ec008e96SDavid Xu while (remaining != 1) { 647bf1a3220SDavid Xu if (P_SHOULDSTOP(p) != P_STOPPED_SINGLE) 648bf1a3220SDavid Xu goto stopme; 64944990b8cSJulian Elischer FOREACH_THREAD_IN_PROC(p, td2) { 65044990b8cSJulian Elischer if (td2 == td) 65144990b8cSJulian Elischer continue; 652588257e8SDavid Xu td2->td_flags |= TDF_ASTPENDING; 65371fad9fdSJulian Elischer if (TD_IS_INHIBITED(td2)) { 654906ac69dSDavid Xu switch (mode) { 655906ac69dSDavid Xu case SINGLE_EXIT: 656cbf4e354SDavid Xu if (td->td_flags & TDF_DBSUSPEND) 657cbf4e354SDavid Xu td->td_flags &= ~TDF_DBSUSPEND; 658906ac69dSDavid Xu if (TD_IS_SUSPENDED(td2)) 65971fad9fdSJulian Elischer thread_unsuspend_one(td2); 66033862f40SDavid Xu if (TD_ON_SLEEPQ(td2) && 661906ac69dSDavid Xu (td2->td_flags & TDF_SINTR)) 66294f0972bSDavid Xu sleepq_abort(td2, EINTR); 663906ac69dSDavid Xu break; 664906ac69dSDavid Xu case SINGLE_BOUNDARY: 665906ac69dSDavid Xu if (TD_IS_SUSPENDED(td2) && 666906ac69dSDavid Xu !(td2->td_flags & TDF_BOUNDARY)) 667906ac69dSDavid Xu thread_unsuspend_one(td2); 668906ac69dSDavid Xu if (TD_ON_SLEEPQ(td2) && 669906ac69dSDavid Xu (td2->td_flags & TDF_SINTR)) 67094f0972bSDavid Xu sleepq_abort(td2, ERESTART); 671906ac69dSDavid Xu break; 672906ac69dSDavid Xu default: 6739d102777SJulian Elischer if (TD_IS_SUSPENDED(td2)) 6749d102777SJulian Elischer continue; 6755215b187SJeff Roberson /* 6765215b187SJeff Roberson * maybe other inhibitted states too? 6775215b187SJeff Roberson */ 6788acf6057SDavid Xu if ((td2->td_flags & TDF_SINTR) && 6798acf6057SDavid Xu (td2->td_inhibitors & 6808acf6057SDavid Xu (TDI_SLEEPING | TDI_SWAPPED))) 6819d102777SJulian Elischer thread_suspend_one(td2); 682906ac69dSDavid Xu break; 68344990b8cSJulian Elischer } 68444990b8cSJulian Elischer } 685d8267df7SDavid Xu #ifdef SMP 686d8267df7SDavid Xu else if (TD_IS_RUNNING(td2) && td != td2) { 687d8267df7SDavid Xu forward_signal(td2); 688d8267df7SDavid Xu } 689d8267df7SDavid Xu #endif 6909d102777SJulian Elischer } 691906ac69dSDavid Xu if (mode == SINGLE_EXIT) 692ec008e96SDavid Xu remaining = p->p_numthreads; 693906ac69dSDavid Xu else if (mode == SINGLE_BOUNDARY) 694906ac69dSDavid Xu remaining = p->p_numthreads - p->p_boundary_count; 695ec008e96SDavid Xu else 696ec008e96SDavid Xu remaining = p->p_numthreads - p->p_suspcount; 697ec008e96SDavid Xu 6989d102777SJulian Elischer /* 6999d102777SJulian Elischer * Maybe we suspended some threads.. was it enough? 7009d102777SJulian Elischer */ 701ec008e96SDavid Xu if (remaining == 1) 7029d102777SJulian Elischer break; 7039d102777SJulian Elischer 704bf1a3220SDavid Xu stopme: 70544990b8cSJulian Elischer /* 70644990b8cSJulian Elischer * Wake us up when everyone else has suspended. 707e3b9bf71SJulian Elischer * In the mean time we suspend as well. 70844990b8cSJulian Elischer */ 709568b4ebbSDavid Xu thread_stopped(p); 71071fad9fdSJulian Elischer thread_suspend_one(td); 71144990b8cSJulian Elischer PROC_UNLOCK(p); 712bf0acc27SJohn Baldwin mi_switch(SW_VOL, NULL); 71344990b8cSJulian Elischer mtx_unlock_spin(&sched_lock); 71444990b8cSJulian Elischer PROC_LOCK(p); 715112afcb2SJohn Baldwin mtx_lock_spin(&sched_lock); 716906ac69dSDavid Xu if (mode == SINGLE_EXIT) 717ec008e96SDavid Xu remaining = p->p_numthreads; 718906ac69dSDavid Xu else if (mode == SINGLE_BOUNDARY) 719906ac69dSDavid Xu remaining = p->p_numthreads - p->p_boundary_count; 720ec008e96SDavid Xu else 721ec008e96SDavid Xu remaining = p->p_numthreads - p->p_suspcount; 72244990b8cSJulian Elischer } 723906ac69dSDavid Xu if (mode == SINGLE_EXIT) { 72491599697SJulian Elischer /* 72591599697SJulian Elischer * We have gotten rid of all the other threads and we 72691599697SJulian Elischer * are about to either exit or exec. In either case, 72791599697SJulian Elischer * we try our utmost to revert to being a non-threaded 72891599697SJulian Elischer * process. 72991599697SJulian Elischer */ 730ed062c8dSJulian Elischer p->p_singlethread = NULL; 73164895117SDavid Xu p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT); 732e5bedcefSJulian Elischer thread_unthread(td); 73391599697SJulian Elischer } 734112afcb2SJohn Baldwin mtx_unlock_spin(&sched_lock); 73544990b8cSJulian Elischer return (0); 73644990b8cSJulian Elischer } 73744990b8cSJulian Elischer 73844990b8cSJulian Elischer /* 73944990b8cSJulian Elischer * Called in from locations that can safely check to see 74044990b8cSJulian Elischer * whether we have to suspend or at least throttle for a 74144990b8cSJulian Elischer * single-thread event (e.g. fork). 74244990b8cSJulian Elischer * 74344990b8cSJulian Elischer * Such locations include userret(). 74444990b8cSJulian Elischer * If the "return_instead" argument is non zero, the thread must be able to 74544990b8cSJulian Elischer * accept 0 (caller may continue), or 1 (caller must abort) as a result. 74644990b8cSJulian Elischer * 74744990b8cSJulian Elischer * The 'return_instead' argument tells the function if it may do a 74844990b8cSJulian Elischer * thread_exit() or suspend, or whether the caller must abort and back 74944990b8cSJulian Elischer * out instead. 75044990b8cSJulian Elischer * 75144990b8cSJulian Elischer * If the thread that set the single_threading request has set the 75244990b8cSJulian Elischer * P_SINGLE_EXIT bit in the process flags then this call will never return 75344990b8cSJulian Elischer * if 'return_instead' is false, but will exit. 75444990b8cSJulian Elischer * 75544990b8cSJulian Elischer * P_SINGLE_EXIT | return_instead == 0| return_instead != 0 75644990b8cSJulian Elischer *---------------+--------------------+--------------------- 75744990b8cSJulian Elischer * 0 | returns 0 | returns 0 or 1 75844990b8cSJulian Elischer * | when ST ends | immediatly 75944990b8cSJulian Elischer *---------------+--------------------+--------------------- 76044990b8cSJulian Elischer * 1 | thread exits | returns 1 76144990b8cSJulian Elischer * | | immediatly 76244990b8cSJulian Elischer * 0 = thread_exit() or suspension ok, 76344990b8cSJulian Elischer * other = return error instead of stopping the thread. 76444990b8cSJulian Elischer * 76544990b8cSJulian Elischer * While a full suspension is under effect, even a single threading 76644990b8cSJulian Elischer * thread would be suspended if it made this call (but it shouldn't). 76744990b8cSJulian Elischer * This call should only be made from places where 76844990b8cSJulian Elischer * thread_exit() would be safe as that may be the outcome unless 76944990b8cSJulian Elischer * return_instead is set. 77044990b8cSJulian Elischer */ 77144990b8cSJulian Elischer int 77244990b8cSJulian Elischer thread_suspend_check(int return_instead) 77344990b8cSJulian Elischer { 774ecafb24bSJuli Mallett struct thread *td; 775ecafb24bSJuli Mallett struct proc *p; 77644990b8cSJulian Elischer 77744990b8cSJulian Elischer td = curthread; 77844990b8cSJulian Elischer p = td->td_proc; 77937814395SPeter Wemm mtx_assert(&Giant, MA_NOTOWNED); 78044990b8cSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 781cbf4e354SDavid Xu while (P_SHOULDSTOP(p) || 782cbf4e354SDavid Xu ((p->p_flag & P_TRACED) && (td->td_flags & TDF_DBSUSPEND))) { 7831279572aSDavid Xu if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 78444990b8cSJulian Elischer KASSERT(p->p_singlethread != NULL, 78544990b8cSJulian Elischer ("singlethread not set")); 78644990b8cSJulian Elischer /* 787e3b9bf71SJulian Elischer * The only suspension in action is a 788e3b9bf71SJulian Elischer * single-threading. Single threader need not stop. 789b6d5995eSJulian Elischer * XXX Should be safe to access unlocked 790b6d5995eSJulian Elischer * as it can only be set to be true by us. 79144990b8cSJulian Elischer */ 792e3b9bf71SJulian Elischer if (p->p_singlethread == td) 79344990b8cSJulian Elischer return (0); /* Exempt from stopping. */ 79444990b8cSJulian Elischer } 79545a4bfa1SDavid Xu if ((p->p_flag & P_SINGLE_EXIT) && return_instead) 79694f0972bSDavid Xu return (EINTR); 79744990b8cSJulian Elischer 798906ac69dSDavid Xu /* Should we goto user boundary if we didn't come from there? */ 799906ac69dSDavid Xu if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE && 800906ac69dSDavid Xu (p->p_flag & P_SINGLE_BOUNDARY) && return_instead) 80194f0972bSDavid Xu return (ERESTART); 802906ac69dSDavid Xu 8039104847fSDavid Xu /* If thread will exit, flush its pending signals */ 8049104847fSDavid Xu if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) 8059104847fSDavid Xu sigqueue_flush(&td->td_sigqueue); 8069104847fSDavid Xu 807e574e444SDavid Xu mtx_lock_spin(&sched_lock); 808e574e444SDavid Xu thread_stopped(p); 80944990b8cSJulian Elischer /* 81044990b8cSJulian Elischer * If the process is waiting for us to exit, 81144990b8cSJulian Elischer * this thread should just suicide. 8121279572aSDavid Xu * Assumes that P_SINGLE_EXIT implies P_STOPPED_SINGLE. 81344990b8cSJulian Elischer */ 814906ac69dSDavid Xu if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) 81544990b8cSJulian Elischer thread_exit(); 81644990b8cSJulian Elischer 81744990b8cSJulian Elischer /* 81844990b8cSJulian Elischer * When a thread suspends, it just 819ad1e7d28SJulian Elischer * gets taken off all queues. 82044990b8cSJulian Elischer */ 82171fad9fdSJulian Elischer thread_suspend_one(td); 822906ac69dSDavid Xu if (return_instead == 0) { 823906ac69dSDavid Xu p->p_boundary_count++; 824906ac69dSDavid Xu td->td_flags |= TDF_BOUNDARY; 825cf19bf91SJulian Elischer } 826906ac69dSDavid Xu if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 827906ac69dSDavid Xu if (p->p_numthreads == p->p_suspcount) 828906ac69dSDavid Xu thread_unsuspend_one(p->p_singlethread); 829cf19bf91SJulian Elischer } 830a6f37ac9SJohn Baldwin PROC_UNLOCK(p); 831bf0acc27SJohn Baldwin mi_switch(SW_INVOL, NULL); 832906ac69dSDavid Xu if (return_instead == 0) { 833906ac69dSDavid Xu p->p_boundary_count--; 834906ac69dSDavid Xu td->td_flags &= ~TDF_BOUNDARY; 835906ac69dSDavid Xu } 83644990b8cSJulian Elischer mtx_unlock_spin(&sched_lock); 83744990b8cSJulian Elischer PROC_LOCK(p); 83844990b8cSJulian Elischer } 83944990b8cSJulian Elischer return (0); 84044990b8cSJulian Elischer } 84144990b8cSJulian Elischer 84235c32a76SDavid Xu void 84335c32a76SDavid Xu thread_suspend_one(struct thread *td) 84435c32a76SDavid Xu { 84535c32a76SDavid Xu struct proc *p = td->td_proc; 84635c32a76SDavid Xu 84735c32a76SDavid Xu mtx_assert(&sched_lock, MA_OWNED); 848112afcb2SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 849e574e444SDavid Xu KASSERT(!TD_IS_SUSPENDED(td), ("already suspended")); 85035c32a76SDavid Xu p->p_suspcount++; 85171fad9fdSJulian Elischer TD_SET_SUSPENDED(td); 85235c32a76SDavid Xu } 85335c32a76SDavid Xu 85435c32a76SDavid Xu void 85535c32a76SDavid Xu thread_unsuspend_one(struct thread *td) 85635c32a76SDavid Xu { 85735c32a76SDavid Xu struct proc *p = td->td_proc; 85835c32a76SDavid Xu 85935c32a76SDavid Xu mtx_assert(&sched_lock, MA_OWNED); 860112afcb2SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 861ad1e7d28SJulian Elischer KASSERT(TD_IS_SUSPENDED(td), ("Thread not suspended")); 86271fad9fdSJulian Elischer TD_CLR_SUSPENDED(td); 86335c32a76SDavid Xu p->p_suspcount--; 86471fad9fdSJulian Elischer setrunnable(td); 86535c32a76SDavid Xu } 86635c32a76SDavid Xu 86744990b8cSJulian Elischer /* 86844990b8cSJulian Elischer * Allow all threads blocked by single threading to continue running. 86944990b8cSJulian Elischer */ 87044990b8cSJulian Elischer void 87144990b8cSJulian Elischer thread_unsuspend(struct proc *p) 87244990b8cSJulian Elischer { 87344990b8cSJulian Elischer struct thread *td; 87444990b8cSJulian Elischer 875b6d5995eSJulian Elischer mtx_assert(&sched_lock, MA_OWNED); 87644990b8cSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 87744990b8cSJulian Elischer if (!P_SHOULDSTOP(p)) { 878ad1e7d28SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 879ad1e7d28SJulian Elischer if (TD_IS_SUSPENDED(td)) { 88035c32a76SDavid Xu thread_unsuspend_one(td); 88144990b8cSJulian Elischer } 882ad1e7d28SJulian Elischer } 8831279572aSDavid Xu } else if ((P_SHOULDSTOP(p) == P_STOPPED_SINGLE) && 88444990b8cSJulian Elischer (p->p_numthreads == p->p_suspcount)) { 88544990b8cSJulian Elischer /* 88644990b8cSJulian Elischer * Stopping everything also did the job for the single 88744990b8cSJulian Elischer * threading request. Now we've downgraded to single-threaded, 88844990b8cSJulian Elischer * let it continue. 88944990b8cSJulian Elischer */ 89035c32a76SDavid Xu thread_unsuspend_one(p->p_singlethread); 89144990b8cSJulian Elischer } 89244990b8cSJulian Elischer } 89344990b8cSJulian Elischer 894ed062c8dSJulian Elischer /* 895ed062c8dSJulian Elischer * End the single threading mode.. 896ed062c8dSJulian Elischer */ 89744990b8cSJulian Elischer void 89844990b8cSJulian Elischer thread_single_end(void) 89944990b8cSJulian Elischer { 90044990b8cSJulian Elischer struct thread *td; 90144990b8cSJulian Elischer struct proc *p; 90244990b8cSJulian Elischer 90344990b8cSJulian Elischer td = curthread; 90444990b8cSJulian Elischer p = td->td_proc; 90544990b8cSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 906906ac69dSDavid Xu p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT | P_SINGLE_BOUNDARY); 907112afcb2SJohn Baldwin mtx_lock_spin(&sched_lock); 90844990b8cSJulian Elischer p->p_singlethread = NULL; 90949539972SJulian Elischer /* 91049539972SJulian Elischer * If there are other threads they mey now run, 91149539972SJulian Elischer * unless of course there is a blanket 'stop order' 91249539972SJulian Elischer * on the process. The single threader must be allowed 91349539972SJulian Elischer * to continue however as this is a bad place to stop. 91449539972SJulian Elischer */ 91549539972SJulian Elischer if ((p->p_numthreads != 1) && (!P_SHOULDSTOP(p))) { 916ad1e7d28SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 917ad1e7d28SJulian Elischer if (TD_IS_SUSPENDED(td)) { 91871fad9fdSJulian Elischer thread_unsuspend_one(td); 91944990b8cSJulian Elischer } 92049539972SJulian Elischer } 921ad1e7d28SJulian Elischer } 922112afcb2SJohn Baldwin mtx_unlock_spin(&sched_lock); 92349539972SJulian Elischer } 9244fc21c09SDaniel Eischen 92544355392SDavid Xu struct thread * 92644355392SDavid Xu thread_find(struct proc *p, lwpid_t tid) 92744355392SDavid Xu { 92844355392SDavid Xu struct thread *td; 92944355392SDavid Xu 93044355392SDavid Xu PROC_LOCK_ASSERT(p, MA_OWNED); 93144355392SDavid Xu mtx_lock_spin(&sched_lock); 93244355392SDavid Xu FOREACH_THREAD_IN_PROC(p, td) { 93344355392SDavid Xu if (td->td_tid == tid) 93444355392SDavid Xu break; 93544355392SDavid Xu } 93644355392SDavid Xu mtx_unlock_spin(&sched_lock); 93744355392SDavid Xu return (td); 93844355392SDavid Xu } 939