kern_thread.c revision 114268
199026Sjulian/* 299026Sjulian * Copyright (C) 2001 Julian Elischer <julian@freebsd.org>. 399026Sjulian * All rights reserved. 499026Sjulian * 599026Sjulian * Redistribution and use in source and binary forms, with or without 699026Sjulian * modification, are permitted provided that the following conditions 799026Sjulian * are met: 899026Sjulian * 1. Redistributions of source code must retain the above copyright 999026Sjulian * notice(s), this list of conditions and the following disclaimer as 1099026Sjulian * the first lines of this file unmodified other than the possible 1199026Sjulian * addition of one or more copyright notices. 1299026Sjulian * 2. Redistributions in binary form must reproduce the above copyright 1399026Sjulian * notice(s), this list of conditions and the following disclaimer in the 1499026Sjulian * documentation and/or other materials provided with the distribution. 1599026Sjulian * 1699026Sjulian * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY 1799026Sjulian * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 1899026Sjulian * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 1999026Sjulian * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY 2099026Sjulian * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 2199026Sjulian * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 2299026Sjulian * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 2399026Sjulian * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2499026Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2599026Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 2699026Sjulian * DAMAGE. 2799026Sjulian * 2899026Sjulian * $FreeBSD: head/sys/kern/kern_thread.c 114268 2003-04-30 01:18:29Z davidxu $ 2999026Sjulian */ 3099026Sjulian 3199026Sjulian#include <sys/param.h> 3299026Sjulian#include <sys/systm.h> 3399026Sjulian#include <sys/kernel.h> 3499026Sjulian#include <sys/lock.h> 3599026Sjulian#include <sys/malloc.h> 3699026Sjulian#include <sys/mutex.h> 3799026Sjulian#include <sys/proc.h> 38107029Sjulian#include <sys/smp.h> 3999026Sjulian#include <sys/sysctl.h> 40105854Sjulian#include <sys/sysproto.h> 4199026Sjulian#include <sys/filedesc.h> 42107126Sjeff#include <sys/sched.h> 4399026Sjulian#include <sys/signalvar.h> 4499026Sjulian#include <sys/sx.h> 45107126Sjeff#include <sys/tty.h> 4699026Sjulian#include <sys/user.h> 4799026Sjulian#include <sys/jail.h> 4899026Sjulian#include <sys/kse.h> 4999026Sjulian#include <sys/ktr.h> 50103410Smini#include <sys/ucontext.h> 5199026Sjulian 5299026Sjulian#include <vm/vm.h> 5399026Sjulian#include <vm/vm_object.h> 5499026Sjulian#include <vm/pmap.h> 5599026Sjulian#include <vm/uma.h> 5699026Sjulian#include <vm/vm_map.h> 5799026Sjulian 58100273Speter#include <machine/frame.h> 59100273Speter 6099026Sjulian/* 61103367Sjulian * KSEGRP related storage. 6299026Sjulian */ 63103367Sjulianstatic uma_zone_t ksegrp_zone; 64103367Sjulianstatic uma_zone_t kse_zone; 6599026Sjulianstatic uma_zone_t thread_zone; 66111028Sjeffstatic uma_zone_t upcall_zone; 6799026Sjulian 68103367Sjulian/* DEBUG ONLY */ 6999026SjulianSYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW, 0, "thread allocation"); 70107719Sjulianstatic int thread_debug = 0; 71107719SjulianSYSCTL_INT(_kern_threads, OID_AUTO, debug, CTLFLAG_RW, 72107719Sjulian &thread_debug, 0, "thread debug"); 7399026Sjulian 74114268Sdavidxustatic int max_threads_per_proc = 150; 75107006SdavidxuSYSCTL_INT(_kern_threads, OID_AUTO, max_threads_per_proc, CTLFLAG_RW, 76103367Sjulian &max_threads_per_proc, 0, "Limit on threads per proc"); 77103367Sjulian 78114268Sdavidxustatic int max_groups_per_proc = 50; 79107006SdavidxuSYSCTL_INT(_kern_threads, OID_AUTO, max_groups_per_proc, CTLFLAG_RW, 80107006Sdavidxu &max_groups_per_proc, 0, "Limit on thread groups per proc"); 81107006Sdavidxu 82111115Sdavidxustatic int max_threads_hits; 83111115SdavidxuSYSCTL_INT(_kern_threads, OID_AUTO, max_threads_hits, CTLFLAG_RD, 84111115Sdavidxu &max_threads_hits, 0, ""); 85111115Sdavidxu 86111028Sjeffstatic int virtual_cpu; 87111028Sjeff 8899026Sjulian#define RANGEOF(type, start, end) (offsetof(type, end) - offsetof(type, start)) 8999026Sjulian 90111028SjeffTAILQ_HEAD(, thread) zombie_threads = TAILQ_HEAD_INITIALIZER(zombie_threads); 91105854SjulianTAILQ_HEAD(, kse) zombie_kses = TAILQ_HEAD_INITIALIZER(zombie_kses); 92105854SjulianTAILQ_HEAD(, ksegrp) zombie_ksegrps = TAILQ_HEAD_INITIALIZER(zombie_ksegrps); 93111028SjeffTAILQ_HEAD(, kse_upcall) zombie_upcalls = 94111028Sjeff TAILQ_HEAD_INITIALIZER(zombie_upcalls); 95111028Sjeffstruct mtx kse_zombie_lock; 96111028SjeffMTX_SYSINIT(kse_zombie_lock, &kse_zombie_lock, "kse zombie lock", MTX_SPIN); 9799026Sjulian 98107719Sjulianstatic void kse_purge(struct proc *p, struct thread *td); 99111028Sjeffstatic void kse_purge_group(struct thread *td); 100111515Sdavidxustatic int thread_update_usr_ticks(struct thread *td, int user); 101111028Sjeffstatic void thread_alloc_spare(struct thread *td, struct thread *spare); 102105854Sjulian 103111028Sjeffstatic int 104111028Sjeffsysctl_kse_virtual_cpu(SYSCTL_HANDLER_ARGS) 105111028Sjeff{ 106111028Sjeff int error, new_val; 107111028Sjeff int def_val; 108111028Sjeff 109111028Sjeff#ifdef SMP 110111028Sjeff def_val = mp_ncpus; 111111028Sjeff#else 112111028Sjeff def_val = 1; 113111028Sjeff#endif 114111028Sjeff if (virtual_cpu == 0) 115111028Sjeff new_val = def_val; 116111028Sjeff else 117111028Sjeff new_val = virtual_cpu; 118111028Sjeff error = sysctl_handle_int(oidp, &new_val, 0, req); 119111028Sjeff if (error != 0 || req->newptr == NULL) 120111028Sjeff return (error); 121111028Sjeff if (new_val < 0) 122111028Sjeff return (EINVAL); 123111028Sjeff virtual_cpu = new_val; 124111028Sjeff return (0); 125111028Sjeff} 126111028Sjeff 127111028Sjeff/* DEBUG ONLY */ 128111028SjeffSYSCTL_PROC(_kern_threads, OID_AUTO, virtual_cpu, CTLTYPE_INT|CTLFLAG_RW, 129111028Sjeff 0, sizeof(virtual_cpu), sysctl_kse_virtual_cpu, "I", 130111028Sjeff "debug virtual cpus"); 131111028Sjeff 13299026Sjulian/* 133107719Sjulian * Prepare a thread for use. 13499026Sjulian */ 13599026Sjulianstatic void 13699026Sjulianthread_ctor(void *mem, int size, void *arg) 13799026Sjulian{ 13899026Sjulian struct thread *td; 13999026Sjulian 14099026Sjulian td = (struct thread *)mem; 141103216Sjulian td->td_state = TDS_INACTIVE; 142113339Sjulian td->td_oncpu = NOCPU; 14399026Sjulian} 14499026Sjulian 14599026Sjulian/* 14699026Sjulian * Reclaim a thread after use. 14799026Sjulian */ 14899026Sjulianstatic void 14999026Sjulianthread_dtor(void *mem, int size, void *arg) 15099026Sjulian{ 15199026Sjulian struct thread *td; 15299026Sjulian 15399026Sjulian td = (struct thread *)mem; 15499026Sjulian 15599026Sjulian#ifdef INVARIANTS 15699026Sjulian /* Verify that this thread is in a safe state to free. */ 15799026Sjulian switch (td->td_state) { 158103216Sjulian case TDS_INHIBITED: 159103216Sjulian case TDS_RUNNING: 160103216Sjulian case TDS_CAN_RUN: 16199026Sjulian case TDS_RUNQ: 16299026Sjulian /* 16399026Sjulian * We must never unlink a thread that is in one of 16499026Sjulian * these states, because it is currently active. 16599026Sjulian */ 16699026Sjulian panic("bad state for thread unlinking"); 16799026Sjulian /* NOTREACHED */ 168103216Sjulian case TDS_INACTIVE: 16999026Sjulian break; 17099026Sjulian default: 17199026Sjulian panic("bad thread state"); 17299026Sjulian /* NOTREACHED */ 17399026Sjulian } 17499026Sjulian#endif 17599026Sjulian} 17699026Sjulian 17799026Sjulian/* 17899026Sjulian * Initialize type-stable parts of a thread (when newly created). 17999026Sjulian */ 18099026Sjulianstatic void 18199026Sjulianthread_init(void *mem, int size) 18299026Sjulian{ 18399026Sjulian struct thread *td; 18499026Sjulian 18599026Sjulian td = (struct thread *)mem; 186103312Sjulian mtx_lock(&Giant); 187104354Sscottl pmap_new_thread(td, 0); 188103312Sjulian mtx_unlock(&Giant); 18999026Sjulian cpu_thread_setup(td); 190107126Sjeff td->td_sched = (struct td_sched *)&td[1]; 19199026Sjulian} 19299026Sjulian 19399026Sjulian/* 19499026Sjulian * Tear down type-stable parts of a thread (just before being discarded). 19599026Sjulian */ 19699026Sjulianstatic void 19799026Sjulianthread_fini(void *mem, int size) 19899026Sjulian{ 19999026Sjulian struct thread *td; 20099026Sjulian 20199026Sjulian td = (struct thread *)mem; 20299026Sjulian pmap_dispose_thread(td); 20399026Sjulian} 204111028Sjeff 205107126Sjeff/* 206107126Sjeff * Initialize type-stable parts of a kse (when newly created). 207107126Sjeff */ 208107126Sjeffstatic void 209107126Sjeffkse_init(void *mem, int size) 210107126Sjeff{ 211107126Sjeff struct kse *ke; 21299026Sjulian 213107126Sjeff ke = (struct kse *)mem; 214107126Sjeff ke->ke_sched = (struct ke_sched *)&ke[1]; 215107126Sjeff} 216111028Sjeff 217107126Sjeff/* 218107126Sjeff * Initialize type-stable parts of a ksegrp (when newly created). 219107126Sjeff */ 220107126Sjeffstatic void 221107126Sjeffksegrp_init(void *mem, int size) 222107126Sjeff{ 223107126Sjeff struct ksegrp *kg; 224107126Sjeff 225107126Sjeff kg = (struct ksegrp *)mem; 226107126Sjeff kg->kg_sched = (struct kg_sched *)&kg[1]; 227107126Sjeff} 228107126Sjeff 229105854Sjulian/* 230111028Sjeff * KSE is linked into kse group. 231105854Sjulian */ 232105854Sjulianvoid 233105854Sjuliankse_link(struct kse *ke, struct ksegrp *kg) 234105854Sjulian{ 235105854Sjulian struct proc *p = kg->kg_proc; 236105854Sjulian 237105854Sjulian TAILQ_INSERT_HEAD(&kg->kg_kseq, ke, ke_kglist); 238105854Sjulian kg->kg_kses++; 239111028Sjeff ke->ke_state = KES_UNQUEUED; 240105854Sjulian ke->ke_proc = p; 241105854Sjulian ke->ke_ksegrp = kg; 242105854Sjulian ke->ke_thread = NULL; 243111028Sjeff ke->ke_oncpu = NOCPU; 244111028Sjeff ke->ke_flags = 0; 245105854Sjulian} 246105854Sjulian 247105854Sjulianvoid 248105854Sjuliankse_unlink(struct kse *ke) 249105854Sjulian{ 250105854Sjulian struct ksegrp *kg; 251105854Sjulian 252105854Sjulian mtx_assert(&sched_lock, MA_OWNED); 253105854Sjulian kg = ke->ke_ksegrp; 254105854Sjulian TAILQ_REMOVE(&kg->kg_kseq, ke, ke_kglist); 255111028Sjeff if (ke->ke_state == KES_IDLE) { 256111028Sjeff TAILQ_REMOVE(&kg->kg_iq, ke, ke_kgrlist); 257111028Sjeff kg->kg_idle_kses--; 258105854Sjulian } 259111028Sjeff if (--kg->kg_kses == 0) 260111028Sjeff ksegrp_unlink(kg); 261105854Sjulian /* 262105854Sjulian * Aggregate stats from the KSE 263105854Sjulian */ 264105854Sjulian kse_stash(ke); 265105854Sjulian} 266105854Sjulian 267105854Sjulianvoid 268105854Sjulianksegrp_link(struct ksegrp *kg, struct proc *p) 269105854Sjulian{ 270105854Sjulian 271105854Sjulian TAILQ_INIT(&kg->kg_threads); 272105854Sjulian TAILQ_INIT(&kg->kg_runq); /* links with td_runq */ 273105854Sjulian TAILQ_INIT(&kg->kg_slpq); /* links with td_runq */ 274105854Sjulian TAILQ_INIT(&kg->kg_kseq); /* all kses in ksegrp */ 275111028Sjeff TAILQ_INIT(&kg->kg_iq); /* all idle kses in ksegrp */ 276111028Sjeff TAILQ_INIT(&kg->kg_upcalls); /* all upcall structure in ksegrp */ 277111028Sjeff kg->kg_proc = p; 278111028Sjeff /* 279111028Sjeff * the following counters are in the -zero- section 280111028Sjeff * and may not need clearing 281111028Sjeff */ 282105854Sjulian kg->kg_numthreads = 0; 283111028Sjeff kg->kg_runnable = 0; 284111028Sjeff kg->kg_kses = 0; 285111028Sjeff kg->kg_runq_kses = 0; /* XXXKSE change name */ 286111028Sjeff kg->kg_idle_kses = 0; 287111028Sjeff kg->kg_numupcalls = 0; 288111028Sjeff /* link it in now that it's consistent */ 289105854Sjulian p->p_numksegrps++; 290105854Sjulian TAILQ_INSERT_HEAD(&p->p_ksegrps, kg, kg_ksegrp); 291105854Sjulian} 292105854Sjulian 293105854Sjulianvoid 294105854Sjulianksegrp_unlink(struct ksegrp *kg) 295105854Sjulian{ 296105854Sjulian struct proc *p; 297105854Sjulian 298105854Sjulian mtx_assert(&sched_lock, MA_OWNED); 299111028Sjeff KASSERT((kg->kg_numthreads == 0), ("ksegrp_unlink: residual threads")); 300111028Sjeff KASSERT((kg->kg_kses == 0), ("ksegrp_unlink: residual kses")); 301111028Sjeff KASSERT((kg->kg_numupcalls == 0), ("ksegrp_unlink: residual upcalls")); 302111028Sjeff 303105854Sjulian p = kg->kg_proc; 304105854Sjulian TAILQ_REMOVE(&p->p_ksegrps, kg, kg_ksegrp); 305105854Sjulian p->p_numksegrps--; 306105854Sjulian /* 307105854Sjulian * Aggregate stats from the KSE 308105854Sjulian */ 309105854Sjulian ksegrp_stash(kg); 310105854Sjulian} 311105854Sjulian 312111028Sjeffstruct kse_upcall * 313111028Sjeffupcall_alloc(void) 314111028Sjeff{ 315111028Sjeff struct kse_upcall *ku; 316111028Sjeff 317111125Sdavidxu ku = uma_zalloc(upcall_zone, M_WAITOK); 318111028Sjeff bzero(ku, sizeof(*ku)); 319111028Sjeff return (ku); 320111028Sjeff} 321111028Sjeff 322111028Sjeffvoid 323111028Sjeffupcall_free(struct kse_upcall *ku) 324111028Sjeff{ 325111028Sjeff 326111028Sjeff uma_zfree(upcall_zone, ku); 327111028Sjeff} 328111028Sjeff 329111028Sjeffvoid 330111028Sjeffupcall_link(struct kse_upcall *ku, struct ksegrp *kg) 331111028Sjeff{ 332111028Sjeff 333111028Sjeff mtx_assert(&sched_lock, MA_OWNED); 334111028Sjeff TAILQ_INSERT_TAIL(&kg->kg_upcalls, ku, ku_link); 335111028Sjeff ku->ku_ksegrp = kg; 336111028Sjeff kg->kg_numupcalls++; 337111028Sjeff} 338111028Sjeff 339111028Sjeffvoid 340111028Sjeffupcall_unlink(struct kse_upcall *ku) 341111028Sjeff{ 342111028Sjeff struct ksegrp *kg = ku->ku_ksegrp; 343111028Sjeff 344111028Sjeff mtx_assert(&sched_lock, MA_OWNED); 345111028Sjeff KASSERT(ku->ku_owner == NULL, ("%s: have owner", __func__)); 346111028Sjeff TAILQ_REMOVE(&kg->kg_upcalls, ku, ku_link); 347111028Sjeff kg->kg_numupcalls--; 348111028Sjeff upcall_stash(ku); 349111028Sjeff} 350111028Sjeff 351111028Sjeffvoid 352111028Sjeffupcall_remove(struct thread *td) 353111028Sjeff{ 354111028Sjeff 355111028Sjeff if (td->td_upcall) { 356111028Sjeff td->td_upcall->ku_owner = NULL; 357111028Sjeff upcall_unlink(td->td_upcall); 358111028Sjeff td->td_upcall = 0; 359111028Sjeff } 360111028Sjeff} 361111028Sjeff 36299026Sjulian/* 363111028Sjeff * For a newly created process, 364111028Sjeff * link up all the structures and its initial threads etc. 365105854Sjulian */ 366105854Sjulianvoid 367105854Sjulianproc_linkup(struct proc *p, struct ksegrp *kg, 368111028Sjeff struct kse *ke, struct thread *td) 369105854Sjulian{ 370105854Sjulian 371105854Sjulian TAILQ_INIT(&p->p_ksegrps); /* all ksegrps in proc */ 372105854Sjulian TAILQ_INIT(&p->p_threads); /* all threads in proc */ 373105854Sjulian TAILQ_INIT(&p->p_suspended); /* Threads suspended */ 374105854Sjulian p->p_numksegrps = 0; 375105854Sjulian p->p_numthreads = 0; 376105854Sjulian 377105854Sjulian ksegrp_link(kg, p); 378105854Sjulian kse_link(ke, kg); 379105854Sjulian thread_link(td, kg); 380105854Sjulian} 381105854Sjulian 382111028Sjeff/* 383111028Sjeffstruct kse_thr_interrupt_args { 384111028Sjeff struct kse_thr_mailbox * tmbx; 385111028Sjeff}; 386111028Sjeff*/ 387105854Sjulianint 388105854Sjuliankse_thr_interrupt(struct thread *td, struct kse_thr_interrupt_args *uap) 389105854Sjulian{ 390106180Sdavidxu struct proc *p; 391106180Sdavidxu struct thread *td2; 392105854Sjulian 393106242Sdavidxu p = td->td_proc; 394111585Sjulian if (!(p->p_flag & P_THREADED) || (uap->tmbx == NULL)) 395106242Sdavidxu return (EINVAL); 396106180Sdavidxu mtx_lock_spin(&sched_lock); 397106180Sdavidxu FOREACH_THREAD_IN_PROC(p, td2) { 398106180Sdavidxu if (td2->td_mailbox == uap->tmbx) { 399106180Sdavidxu td2->td_flags |= TDF_INTERRUPT; 400106180Sdavidxu if (TD_ON_SLEEPQ(td2) && (td2->td_flags & TDF_SINTR)) { 401106180Sdavidxu if (td2->td_flags & TDF_CVWAITQ) 402106180Sdavidxu cv_abort(td2); 403106180Sdavidxu else 404106180Sdavidxu abortsleep(td2); 405111028Sjeff } 406106180Sdavidxu mtx_unlock_spin(&sched_lock); 407106182Sdavidxu return (0); 408106180Sdavidxu } 409106180Sdavidxu } 410106180Sdavidxu mtx_unlock_spin(&sched_lock); 411106182Sdavidxu return (ESRCH); 412105854Sjulian} 413105854Sjulian 414111028Sjeff/* 415111028Sjeffstruct kse_exit_args { 416111028Sjeff register_t dummy; 417111028Sjeff}; 418111028Sjeff*/ 419105854Sjulianint 420105854Sjuliankse_exit(struct thread *td, struct kse_exit_args *uap) 421105854Sjulian{ 422105854Sjulian struct proc *p; 423105854Sjulian struct ksegrp *kg; 424108640Sdavidxu struct kse *ke; 425105854Sjulian 426105854Sjulian p = td->td_proc; 427113793Sdavidxu if (td->td_upcall == NULL || TD_CAN_UNBIND(td)) 428106182Sdavidxu return (EINVAL); 429105854Sjulian kg = td->td_ksegrp; 430111028Sjeff /* Serialize removing upcall */ 431105854Sjulian PROC_LOCK(p); 432105854Sjulian mtx_lock_spin(&sched_lock); 433111028Sjeff if ((kg->kg_numupcalls == 1) && (kg->kg_numthreads > 1)) { 434105854Sjulian mtx_unlock_spin(&sched_lock); 435105854Sjulian PROC_UNLOCK(p); 436105854Sjulian return (EDEADLK); 437105854Sjulian } 438108640Sdavidxu ke = td->td_kse; 439111028Sjeff upcall_remove(td); 440108640Sdavidxu if (p->p_numthreads == 1) { 441111028Sjeff kse_purge(p, td); 442111585Sjulian p->p_flag &= ~P_THREADED; 443105854Sjulian mtx_unlock_spin(&sched_lock); 444105854Sjulian PROC_UNLOCK(p); 445105854Sjulian } else { 446111028Sjeff if (kg->kg_numthreads == 1) { /* Shutdown a group */ 447111028Sjeff kse_purge_group(td); 448111028Sjeff ke->ke_flags |= KEF_EXIT; 449111028Sjeff } 450112071Sdavidxu thread_stopped(p); 451105854Sjulian thread_exit(); 452105854Sjulian /* NOTREACHED */ 453105854Sjulian } 454106182Sdavidxu return (0); 455105854Sjulian} 456105854Sjulian 457107719Sjulian/* 458108338Sjulian * Either becomes an upcall or waits for an awakening event and 459111028Sjeff * then becomes an upcall. Only error cases return. 460107719Sjulian */ 461111028Sjeff/* 462111028Sjeffstruct kse_release_args { 463111169Sdavidxu struct timespec *timeout; 464111028Sjeff}; 465111028Sjeff*/ 466105854Sjulianint 467111028Sjeffkse_release(struct thread *td, struct kse_release_args *uap) 468105854Sjulian{ 469105854Sjulian struct proc *p; 470107719Sjulian struct ksegrp *kg; 471111169Sdavidxu struct timespec ts, ts2, ts3, timeout; 472111169Sdavidxu struct timeval tv; 473111169Sdavidxu int error; 474105854Sjulian 475105854Sjulian p = td->td_proc; 476107719Sjulian kg = td->td_ksegrp; 477113793Sdavidxu if (td->td_upcall == NULL || TD_CAN_UNBIND(td)) 478107719Sjulian return (EINVAL); 479111169Sdavidxu if (uap->timeout != NULL) { 480111169Sdavidxu if ((error = copyin(uap->timeout, &timeout, sizeof(timeout)))) 481111169Sdavidxu return (error); 482111169Sdavidxu getnanouptime(&ts); 483111169Sdavidxu timespecadd(&ts, &timeout); 484111169Sdavidxu TIMESPEC_TO_TIMEVAL(&tv, &timeout); 485111169Sdavidxu } 486108613Sjulian mtx_lock_spin(&sched_lock); 487108338Sjulian /* Change OURSELF to become an upcall. */ 488111028Sjeff td->td_flags = TDF_UPCALLING; 489112888Sjeff#if 0 /* XXX This shouldn't be necessary */ 490111042Sdavidxu if (p->p_sflag & PS_NEEDSIGCHK) 491111042Sdavidxu td->td_flags |= TDF_ASTPENDING; 492112888Sjeff#endif 493111169Sdavidxu mtx_unlock_spin(&sched_lock); 494111169Sdavidxu PROC_LOCK(p); 495111169Sdavidxu while ((td->td_upcall->ku_flags & KUF_DOUPCALL) == 0 && 496111169Sdavidxu (kg->kg_completed == NULL)) { 497111028Sjeff kg->kg_upsleeps++; 498111169Sdavidxu error = msleep(&kg->kg_completed, &p->p_mtx, PPAUSE|PCATCH, 499111169Sdavidxu "kse_rel", (uap->timeout ? tvtohz(&tv) : 0)); 500111028Sjeff kg->kg_upsleeps--; 501110190Sjulian PROC_UNLOCK(p); 502111169Sdavidxu if (uap->timeout == NULL || error != EWOULDBLOCK) 503111169Sdavidxu return (0); 504111169Sdavidxu getnanouptime(&ts2); 505111169Sdavidxu if (timespeccmp(&ts2, &ts, >=)) 506111169Sdavidxu return (0); 507111169Sdavidxu ts3 = ts; 508111169Sdavidxu timespecsub(&ts3, &ts2); 509111169Sdavidxu TIMESPEC_TO_TIMEVAL(&tv, &ts3); 510111169Sdavidxu PROC_LOCK(p); 511105854Sjulian } 512111169Sdavidxu PROC_UNLOCK(p); 513107719Sjulian return (0); 514105854Sjulian} 515105854Sjulian 516105854Sjulian/* struct kse_wakeup_args { 517105854Sjulian struct kse_mailbox *mbx; 518105854Sjulian}; */ 519105854Sjulianint 520105854Sjuliankse_wakeup(struct thread *td, struct kse_wakeup_args *uap) 521105854Sjulian{ 522105854Sjulian struct proc *p; 523105854Sjulian struct ksegrp *kg; 524111028Sjeff struct kse_upcall *ku; 525108338Sjulian struct thread *td2; 526105854Sjulian 527105854Sjulian p = td->td_proc; 528108338Sjulian td2 = NULL; 529111028Sjeff ku = NULL; 530105854Sjulian /* KSE-enabled processes only, please. */ 531111585Sjulian if (!(p->p_flag & P_THREADED)) 532111028Sjeff return (EINVAL); 533111028Sjeff PROC_LOCK(p); 534108613Sjulian mtx_lock_spin(&sched_lock); 535105854Sjulian if (uap->mbx) { 536105854Sjulian FOREACH_KSEGRP_IN_PROC(p, kg) { 537111028Sjeff FOREACH_UPCALL_IN_GROUP(kg, ku) { 538111207Sdavidxu if (ku->ku_mailbox == uap->mbx) 539111028Sjeff break; 540108613Sjulian } 541111028Sjeff if (ku) 542108338Sjulian break; 543105854Sjulian } 544105854Sjulian } else { 545105854Sjulian kg = td->td_ksegrp; 546111028Sjeff if (kg->kg_upsleeps) { 547111028Sjeff wakeup_one(&kg->kg_completed); 548111028Sjeff mtx_unlock_spin(&sched_lock); 549111028Sjeff PROC_UNLOCK(p); 550111028Sjeff return (0); 551108338Sjulian } 552111028Sjeff ku = TAILQ_FIRST(&kg->kg_upcalls); 553105854Sjulian } 554111028Sjeff if (ku) { 555111028Sjeff if ((td2 = ku->ku_owner) == NULL) { 556111028Sjeff panic("%s: no owner", __func__); 557111028Sjeff } else if (TD_ON_SLEEPQ(td2) && 558111028Sjeff (td2->td_wchan == &kg->kg_completed)) { 559111028Sjeff abortsleep(td2); 560111028Sjeff } else { 561111028Sjeff ku->ku_flags |= KUF_DOUPCALL; 562108613Sjulian } 563105854Sjulian mtx_unlock_spin(&sched_lock); 564111028Sjeff PROC_UNLOCK(p); 565108338Sjulian return (0); 566108613Sjulian } 567105854Sjulian mtx_unlock_spin(&sched_lock); 568111028Sjeff PROC_UNLOCK(p); 569108338Sjulian return (ESRCH); 570105854Sjulian} 571105854Sjulian 572105854Sjulian/* 573105854Sjulian * No new KSEG: first call: use current KSE, don't schedule an upcall 574111028Sjeff * All other situations, do allocate max new KSEs and schedule an upcall. 575105854Sjulian */ 576105854Sjulian/* struct kse_create_args { 577105854Sjulian struct kse_mailbox *mbx; 578105854Sjulian int newgroup; 579105854Sjulian}; */ 580105854Sjulianint 581105854Sjuliankse_create(struct thread *td, struct kse_create_args *uap) 582105854Sjulian{ 583105854Sjulian struct kse *newke; 584105854Sjulian struct ksegrp *newkg; 585105854Sjulian struct ksegrp *kg; 586105854Sjulian struct proc *p; 587105854Sjulian struct kse_mailbox mbx; 588111028Sjeff struct kse_upcall *newku; 589111028Sjeff int err, ncpus; 590105854Sjulian 591105854Sjulian p = td->td_proc; 592105854Sjulian if ((err = copyin(uap->mbx, &mbx, sizeof(mbx)))) 593105854Sjulian return (err); 594105854Sjulian 595111028Sjeff /* Too bad, why hasn't kernel always a cpu counter !? */ 596111028Sjeff#ifdef SMP 597111028Sjeff ncpus = mp_ncpus; 598111028Sjeff#else 599111028Sjeff ncpus = 1; 600111028Sjeff#endif 601111028Sjeff if (thread_debug && virtual_cpu != 0) 602111028Sjeff ncpus = virtual_cpu; 603111028Sjeff 604111028Sjeff /* Easier to just set it than to test and set */ 605112078Sdavidxu PROC_LOCK(p); 606111585Sjulian p->p_flag |= P_THREADED; 607112078Sdavidxu PROC_UNLOCK(p); 608105854Sjulian kg = td->td_ksegrp; 609105854Sjulian if (uap->newgroup) { 610111028Sjeff /* Have race condition but it is cheap */ 611107006Sdavidxu if (p->p_numksegrps >= max_groups_per_proc) 612107006Sdavidxu return (EPROCLIM); 613105854Sjulian /* 614105854Sjulian * If we want a new KSEGRP it doesn't matter whether 615105854Sjulian * we have already fired up KSE mode before or not. 616111028Sjeff * We put the process in KSE mode and create a new KSEGRP. 617105854Sjulian */ 618105854Sjulian newkg = ksegrp_alloc(); 619105854Sjulian bzero(&newkg->kg_startzero, RANGEOF(struct ksegrp, 620111028Sjeff kg_startzero, kg_endzero)); 621105854Sjulian bcopy(&kg->kg_startcopy, &newkg->kg_startcopy, 622105854Sjulian RANGEOF(struct ksegrp, kg_startcopy, kg_endcopy)); 623111028Sjeff mtx_lock_spin(&sched_lock); 624111028Sjeff if (p->p_numksegrps >= max_groups_per_proc) { 625111028Sjeff mtx_unlock_spin(&sched_lock); 626111677Sdavidxu ksegrp_free(newkg); 627111028Sjeff return (EPROCLIM); 628111028Sjeff } 629111677Sdavidxu ksegrp_link(newkg, p); 630111028Sjeff mtx_unlock_spin(&sched_lock); 631105854Sjulian } else { 632111028Sjeff newkg = kg; 633111028Sjeff } 634111028Sjeff 635111028Sjeff /* 636111028Sjeff * Creating upcalls more than number of physical cpu does 637111028Sjeff * not help performance. 638111028Sjeff */ 639111028Sjeff if (newkg->kg_numupcalls >= ncpus) 640111028Sjeff return (EPROCLIM); 641111028Sjeff 642111028Sjeff if (newkg->kg_numupcalls == 0) { 643111028Sjeff /* 644111028Sjeff * Initialize KSE group, optimized for MP. 645111028Sjeff * Create KSEs as many as physical cpus, this increases 646111028Sjeff * concurrent even if userland is not MP safe and can only run 647111028Sjeff * on single CPU (for early version of libpthread, it is true). 648111028Sjeff * In ideal world, every physical cpu should execute a thread. 649111028Sjeff * If there is enough KSEs, threads in kernel can be 650111028Sjeff * executed parallel on different cpus with full speed, 651111028Sjeff * Concurrent in kernel shouldn't be restricted by number of 652111028Sjeff * upcalls userland provides. 653111028Sjeff * Adding more upcall structures only increases concurrent 654111028Sjeff * in userland. 655111028Sjeff * Highest performance configuration is: 656111028Sjeff * N kses = N upcalls = N phyiscal cpus 657105854Sjulian */ 658111028Sjeff while (newkg->kg_kses < ncpus) { 659105854Sjulian newke = kse_alloc(); 660111028Sjeff bzero(&newke->ke_startzero, RANGEOF(struct kse, 661111028Sjeff ke_startzero, ke_endzero)); 662105854Sjulian#if 0 663111028Sjeff mtx_lock_spin(&sched_lock); 664111028Sjeff bcopy(&ke->ke_startcopy, &newke->ke_startcopy, 665111028Sjeff RANGEOF(struct kse, ke_startcopy, ke_endcopy)); 666111028Sjeff mtx_unlock_spin(&sched_lock); 667105854Sjulian#endif 668111028Sjeff mtx_lock_spin(&sched_lock); 669111028Sjeff kse_link(newke, newkg); 670111028Sjeff /* Add engine */ 671111028Sjeff kse_reassign(newke); 672111028Sjeff mtx_unlock_spin(&sched_lock); 673105854Sjulian } 674111028Sjeff } 675111028Sjeff newku = upcall_alloc(); 676111028Sjeff newku->ku_mailbox = uap->mbx; 677111028Sjeff newku->ku_func = mbx.km_func; 678111028Sjeff bcopy(&mbx.km_stack, &newku->ku_stack, sizeof(stack_t)); 679111028Sjeff 680111028Sjeff /* For the first call this may not have been set */ 681111028Sjeff if (td->td_standin == NULL) 682111028Sjeff thread_alloc_spare(td, NULL); 683111028Sjeff 684111028Sjeff mtx_lock_spin(&sched_lock); 685111028Sjeff if (newkg->kg_numupcalls >= ncpus) { 686111595Sdavidxu mtx_unlock_spin(&sched_lock); 687111028Sjeff upcall_free(newku); 688111028Sjeff return (EPROCLIM); 689111028Sjeff } 690111028Sjeff upcall_link(newku, newkg); 691112397Sdavidxu if (mbx.km_quantum) 692112397Sdavidxu newkg->kg_upquantum = max(1, mbx.km_quantum/tick); 693111028Sjeff 694111028Sjeff /* 695111028Sjeff * Each upcall structure has an owner thread, find which 696111028Sjeff * one owns it. 697111028Sjeff */ 698111028Sjeff if (uap->newgroup) { 699111028Sjeff /* 700111028Sjeff * Because new ksegrp hasn't thread, 701111028Sjeff * create an initial upcall thread to own it. 702111028Sjeff */ 703111028Sjeff thread_schedule_upcall(td, newku); 704105854Sjulian } else { 705105854Sjulian /* 706111028Sjeff * If current thread hasn't an upcall structure, 707111028Sjeff * just assign the upcall to it. 708105854Sjulian */ 709111028Sjeff if (td->td_upcall == NULL) { 710111028Sjeff newku->ku_owner = td; 711111028Sjeff td->td_upcall = newku; 712111028Sjeff } else { 713111028Sjeff /* 714111028Sjeff * Create a new upcall thread to own it. 715111028Sjeff */ 716111028Sjeff thread_schedule_upcall(td, newku); 717111028Sjeff } 718105854Sjulian } 719111028Sjeff mtx_unlock_spin(&sched_lock); 720105854Sjulian return (0); 721105854Sjulian} 722105854Sjulian 723105854Sjulian/* 724103410Smini * Fill a ucontext_t with a thread's context information. 725103410Smini * 726103410Smini * This is an analogue to getcontext(3). 727103410Smini */ 728103410Sminivoid 729103410Sminithread_getcontext(struct thread *td, ucontext_t *uc) 730103410Smini{ 731103410Smini 732113998Sdeischen get_mcontext(td, &uc->uc_mcontext, 0); 733113626Sjhb PROC_LOCK(td->td_proc); 734112888Sjeff uc->uc_sigmask = td->td_sigmask; 735113626Sjhb PROC_UNLOCK(td->td_proc); 736103410Smini} 737103410Smini 738103410Smini/* 739103410Smini * Set a thread's context from a ucontext_t. 740103410Smini * 741103410Smini * This is an analogue to setcontext(3). 742103410Smini */ 743103410Sminiint 744103410Sminithread_setcontext(struct thread *td, ucontext_t *uc) 745103410Smini{ 746103410Smini int ret; 747103410Smini 748103410Smini ret = set_mcontext(td, &uc->uc_mcontext); 749103410Smini if (ret == 0) { 750103410Smini SIG_CANTMASK(uc->uc_sigmask); 751103410Smini PROC_LOCK(td->td_proc); 752112888Sjeff td->td_sigmask = uc->uc_sigmask; 753103410Smini PROC_UNLOCK(td->td_proc); 754103410Smini } 755103410Smini return (ret); 756103410Smini} 757103410Smini 758103410Smini/* 75999026Sjulian * Initialize global thread allocation resources. 76099026Sjulian */ 76199026Sjulianvoid 76299026Sjulianthreadinit(void) 76399026Sjulian{ 76499026Sjulian 765104437Speter#ifndef __ia64__ 766107126Sjeff thread_zone = uma_zcreate("THREAD", sched_sizeof_thread(), 76799026Sjulian thread_ctor, thread_dtor, thread_init, thread_fini, 76899026Sjulian UMA_ALIGN_CACHE, 0); 769104437Speter#else 770104437Speter /* 771104437Speter * XXX the ia64 kstack allocator is really lame and is at the mercy 772104437Speter * of contigmallloc(). This hackery is to pre-construct a whole 773104437Speter * pile of thread structures with associated kernel stacks early 774104437Speter * in the system startup while contigmalloc() still works. Once we 775104437Speter * have them, keep them. Sigh. 776104437Speter */ 777107126Sjeff thread_zone = uma_zcreate("THREAD", sched_sizeof_thread(), 778104437Speter thread_ctor, thread_dtor, thread_init, thread_fini, 779104437Speter UMA_ALIGN_CACHE, UMA_ZONE_NOFREE); 780104437Speter uma_prealloc(thread_zone, 512); /* XXX arbitary */ 781104437Speter#endif 782107126Sjeff ksegrp_zone = uma_zcreate("KSEGRP", sched_sizeof_ksegrp(), 783107126Sjeff NULL, NULL, ksegrp_init, NULL, 784103367Sjulian UMA_ALIGN_CACHE, 0); 785107126Sjeff kse_zone = uma_zcreate("KSE", sched_sizeof_kse(), 786107126Sjeff NULL, NULL, kse_init, NULL, 787103367Sjulian UMA_ALIGN_CACHE, 0); 788111028Sjeff upcall_zone = uma_zcreate("UPCALL", sizeof(struct kse_upcall), 789111028Sjeff NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0); 79099026Sjulian} 79199026Sjulian 79299026Sjulian/* 793103002Sjulian * Stash an embarasingly extra thread into the zombie thread queue. 79499026Sjulian */ 79599026Sjulianvoid 79699026Sjulianthread_stash(struct thread *td) 79799026Sjulian{ 798111028Sjeff mtx_lock_spin(&kse_zombie_lock); 79999026Sjulian TAILQ_INSERT_HEAD(&zombie_threads, td, td_runq); 800111028Sjeff mtx_unlock_spin(&kse_zombie_lock); 80199026Sjulian} 80299026Sjulian 803103410Smini/* 804105854Sjulian * Stash an embarasingly extra kse into the zombie kse queue. 805105854Sjulian */ 806105854Sjulianvoid 807105854Sjuliankse_stash(struct kse *ke) 808105854Sjulian{ 809111028Sjeff mtx_lock_spin(&kse_zombie_lock); 810105854Sjulian TAILQ_INSERT_HEAD(&zombie_kses, ke, ke_procq); 811111028Sjeff mtx_unlock_spin(&kse_zombie_lock); 812105854Sjulian} 813105854Sjulian 814105854Sjulian/* 815111028Sjeff * Stash an embarasingly extra upcall into the zombie upcall queue. 816111028Sjeff */ 817111028Sjeff 818111028Sjeffvoid 819111028Sjeffupcall_stash(struct kse_upcall *ku) 820111028Sjeff{ 821111028Sjeff mtx_lock_spin(&kse_zombie_lock); 822111028Sjeff TAILQ_INSERT_HEAD(&zombie_upcalls, ku, ku_link); 823111028Sjeff mtx_unlock_spin(&kse_zombie_lock); 824111028Sjeff} 825111028Sjeff 826111028Sjeff/* 827105854Sjulian * Stash an embarasingly extra ksegrp into the zombie ksegrp queue. 828105854Sjulian */ 829105854Sjulianvoid 830105854Sjulianksegrp_stash(struct ksegrp *kg) 831105854Sjulian{ 832111028Sjeff mtx_lock_spin(&kse_zombie_lock); 833105854Sjulian TAILQ_INSERT_HEAD(&zombie_ksegrps, kg, kg_ksegrp); 834111028Sjeff mtx_unlock_spin(&kse_zombie_lock); 835105854Sjulian} 836105854Sjulian 837105854Sjulian/* 838111028Sjeff * Reap zombie kse resource. 83999026Sjulian */ 84099026Sjulianvoid 84199026Sjulianthread_reap(void) 84299026Sjulian{ 843105854Sjulian struct thread *td_first, *td_next; 844105854Sjulian struct kse *ke_first, *ke_next; 845105854Sjulian struct ksegrp *kg_first, * kg_next; 846111028Sjeff struct kse_upcall *ku_first, *ku_next; 84799026Sjulian 84899026Sjulian /* 849111028Sjeff * Don't even bother to lock if none at this instant, 850111028Sjeff * we really don't care about the next instant.. 85199026Sjulian */ 852105854Sjulian if ((!TAILQ_EMPTY(&zombie_threads)) 853105854Sjulian || (!TAILQ_EMPTY(&zombie_kses)) 854111028Sjeff || (!TAILQ_EMPTY(&zombie_ksegrps)) 855111028Sjeff || (!TAILQ_EMPTY(&zombie_upcalls))) { 856111028Sjeff mtx_lock_spin(&kse_zombie_lock); 857105854Sjulian td_first = TAILQ_FIRST(&zombie_threads); 858105854Sjulian ke_first = TAILQ_FIRST(&zombie_kses); 859105854Sjulian kg_first = TAILQ_FIRST(&zombie_ksegrps); 860111028Sjeff ku_first = TAILQ_FIRST(&zombie_upcalls); 861105854Sjulian if (td_first) 862105854Sjulian TAILQ_INIT(&zombie_threads); 863105854Sjulian if (ke_first) 864105854Sjulian TAILQ_INIT(&zombie_kses); 865105854Sjulian if (kg_first) 866105854Sjulian TAILQ_INIT(&zombie_ksegrps); 867111028Sjeff if (ku_first) 868111028Sjeff TAILQ_INIT(&zombie_upcalls); 869111028Sjeff mtx_unlock_spin(&kse_zombie_lock); 870105854Sjulian while (td_first) { 871105854Sjulian td_next = TAILQ_NEXT(td_first, td_runq); 872111028Sjeff if (td_first->td_ucred) 873111028Sjeff crfree(td_first->td_ucred); 874105854Sjulian thread_free(td_first); 875105854Sjulian td_first = td_next; 87699026Sjulian } 877105854Sjulian while (ke_first) { 878105854Sjulian ke_next = TAILQ_NEXT(ke_first, ke_procq); 879105854Sjulian kse_free(ke_first); 880105854Sjulian ke_first = ke_next; 881105854Sjulian } 882105854Sjulian while (kg_first) { 883105854Sjulian kg_next = TAILQ_NEXT(kg_first, kg_ksegrp); 884105854Sjulian ksegrp_free(kg_first); 885105854Sjulian kg_first = kg_next; 886105854Sjulian } 887111028Sjeff while (ku_first) { 888111028Sjeff ku_next = TAILQ_NEXT(ku_first, ku_link); 889111028Sjeff upcall_free(ku_first); 890111028Sjeff ku_first = ku_next; 891111028Sjeff } 89299026Sjulian } 89399026Sjulian} 89499026Sjulian 89599026Sjulian/* 896103367Sjulian * Allocate a ksegrp. 897103367Sjulian */ 898103367Sjulianstruct ksegrp * 899103367Sjulianksegrp_alloc(void) 900103367Sjulian{ 901111119Simp return (uma_zalloc(ksegrp_zone, M_WAITOK)); 902103367Sjulian} 903103367Sjulian 904103367Sjulian/* 905103367Sjulian * Allocate a kse. 906103367Sjulian */ 907103367Sjulianstruct kse * 908103367Sjuliankse_alloc(void) 909103367Sjulian{ 910111119Simp return (uma_zalloc(kse_zone, M_WAITOK)); 911103367Sjulian} 912103367Sjulian 913103367Sjulian/* 91499026Sjulian * Allocate a thread. 91599026Sjulian */ 91699026Sjulianstruct thread * 91799026Sjulianthread_alloc(void) 91899026Sjulian{ 91999026Sjulian thread_reap(); /* check if any zombies to get */ 920111119Simp return (uma_zalloc(thread_zone, M_WAITOK)); 92199026Sjulian} 92299026Sjulian 92399026Sjulian/* 924103367Sjulian * Deallocate a ksegrp. 925103367Sjulian */ 926103367Sjulianvoid 927103367Sjulianksegrp_free(struct ksegrp *td) 928103367Sjulian{ 929103367Sjulian uma_zfree(ksegrp_zone, td); 930103367Sjulian} 931103367Sjulian 932103367Sjulian/* 933103367Sjulian * Deallocate a kse. 934103367Sjulian */ 935103367Sjulianvoid 936103367Sjuliankse_free(struct kse *td) 937103367Sjulian{ 938103367Sjulian uma_zfree(kse_zone, td); 939103367Sjulian} 940103367Sjulian 941103367Sjulian/* 94299026Sjulian * Deallocate a thread. 94399026Sjulian */ 94499026Sjulianvoid 94599026Sjulianthread_free(struct thread *td) 94699026Sjulian{ 947107719Sjulian 948107719Sjulian cpu_thread_clean(td); 94999026Sjulian uma_zfree(thread_zone, td); 95099026Sjulian} 95199026Sjulian 95299026Sjulian/* 95399026Sjulian * Store the thread context in the UTS's mailbox. 954104031Sjulian * then add the mailbox at the head of a list we are building in user space. 955104031Sjulian * The list is anchored in the ksegrp structure. 95699026Sjulian */ 95799026Sjulianint 95899026Sjulianthread_export_context(struct thread *td) 95999026Sjulian{ 960104503Sjmallett struct proc *p; 961104031Sjulian struct ksegrp *kg; 962104031Sjulian uintptr_t mbx; 963104031Sjulian void *addr; 964111028Sjeff int error,temp; 965103410Smini ucontext_t uc; 96699026Sjulian 967104503Sjmallett p = td->td_proc; 968104503Sjmallett kg = td->td_ksegrp; 969104503Sjmallett 970104031Sjulian /* Export the user/machine context. */ 971111028Sjeff addr = (void *)(&td->td_mailbox->tm_context); 972104031Sjulian error = copyin(addr, &uc, sizeof(ucontext_t)); 973108338Sjulian if (error) 974108338Sjulian goto bad; 975104031Sjulian 976108338Sjulian thread_getcontext(td, &uc); 977108338Sjulian error = copyout(&uc, addr, sizeof(ucontext_t)); 978108338Sjulian if (error) 979108338Sjulian goto bad; 980108338Sjulian 981111028Sjeff /* Exports clock ticks in kernel mode */ 982111028Sjeff addr = (caddr_t)(&td->td_mailbox->tm_sticks); 983111028Sjeff temp = fuword(addr) + td->td_usticks; 984111028Sjeff if (suword(addr, temp)) 985111028Sjeff goto bad; 986111028Sjeff 987111028Sjeff /* Get address in latest mbox of list pointer */ 988104031Sjulian addr = (void *)(&td->td_mailbox->tm_next); 989104031Sjulian /* 990104031Sjulian * Put the saved address of the previous first 991104031Sjulian * entry into this one 992104031Sjulian */ 993104031Sjulian for (;;) { 994104031Sjulian mbx = (uintptr_t)kg->kg_completed; 995104031Sjulian if (suword(addr, mbx)) { 996108338Sjulian error = EFAULT; 997107034Sdavidxu goto bad; 998104031Sjulian } 999104126Sjulian PROC_LOCK(p); 1000104031Sjulian if (mbx == (uintptr_t)kg->kg_completed) { 1001104031Sjulian kg->kg_completed = td->td_mailbox; 1002111028Sjeff /* 1003111028Sjeff * The thread context may be taken away by 1004111028Sjeff * other upcall threads when we unlock 1005111028Sjeff * process lock. it's no longer valid to 1006111028Sjeff * use it again in any other places. 1007111028Sjeff */ 1008111028Sjeff td->td_mailbox = NULL; 1009104126Sjulian PROC_UNLOCK(p); 1010104031Sjulian break; 1011104031Sjulian } 1012104126Sjulian PROC_UNLOCK(p); 1013104031Sjulian } 1014111028Sjeff td->td_usticks = 0; 1015104031Sjulian return (0); 1016107034Sdavidxu 1017107034Sdavidxubad: 1018107034Sdavidxu PROC_LOCK(p); 1019107034Sdavidxu psignal(p, SIGSEGV); 1020107034Sdavidxu PROC_UNLOCK(p); 1021111028Sjeff /* The mailbox is bad, don't use it */ 1022111028Sjeff td->td_mailbox = NULL; 1023111028Sjeff td->td_usticks = 0; 1024108338Sjulian return (error); 1025104031Sjulian} 102699026Sjulian 1027104031Sjulian/* 1028104031Sjulian * Take the list of completed mailboxes for this KSEGRP and put them on this 1029111028Sjeff * upcall's mailbox as it's the next one going up. 1030104031Sjulian */ 1031104031Sjulianstatic int 1032111028Sjeffthread_link_mboxes(struct ksegrp *kg, struct kse_upcall *ku) 1033104031Sjulian{ 1034104126Sjulian struct proc *p = kg->kg_proc; 1035104031Sjulian void *addr; 1036104031Sjulian uintptr_t mbx; 1037104031Sjulian 1038111028Sjeff addr = (void *)(&ku->ku_mailbox->km_completed); 1039104031Sjulian for (;;) { 1040104031Sjulian mbx = (uintptr_t)kg->kg_completed; 1041104031Sjulian if (suword(addr, mbx)) { 1042104126Sjulian PROC_LOCK(p); 1043104126Sjulian psignal(p, SIGSEGV); 1044104126Sjulian PROC_UNLOCK(p); 1045104031Sjulian return (EFAULT); 1046104031Sjulian } 1047104126Sjulian PROC_LOCK(p); 1048104031Sjulian if (mbx == (uintptr_t)kg->kg_completed) { 1049104031Sjulian kg->kg_completed = NULL; 1050104126Sjulian PROC_UNLOCK(p); 1051104031Sjulian break; 1052104031Sjulian } 1053104126Sjulian PROC_UNLOCK(p); 105499026Sjulian } 1055104031Sjulian return (0); 105699026Sjulian} 105799026Sjulian 105899026Sjulian/* 1059107034Sdavidxu * This function should be called at statclock interrupt time 1060107034Sdavidxu */ 1061107034Sdavidxuint 1062111028Sjeffthread_statclock(int user) 1063107034Sdavidxu{ 1064107034Sdavidxu struct thread *td = curthread; 1065107034Sdavidxu 1066111028Sjeff if (td->td_ksegrp->kg_numupcalls == 0) 1067111028Sjeff return (-1); 1068107034Sdavidxu if (user) { 1069107034Sdavidxu /* Current always do via ast() */ 1070111976Sdavidxu mtx_lock_spin(&sched_lock); 1071111032Sjulian td->td_flags |= (TDF_USTATCLOCK|TDF_ASTPENDING); 1072111976Sdavidxu mtx_unlock_spin(&sched_lock); 1073111028Sjeff td->td_uuticks++; 1074107034Sdavidxu } else { 1075107034Sdavidxu if (td->td_mailbox != NULL) 1076111028Sjeff td->td_usticks++; 1077111028Sjeff else { 1078111028Sjeff /* XXXKSE 1079111028Sjeff * We will call thread_user_enter() for every 1080111028Sjeff * kernel entry in future, so if the thread mailbox 1081111028Sjeff * is NULL, it must be a UTS kernel, don't account 1082111028Sjeff * clock ticks for it. 1083111028Sjeff */ 1084111028Sjeff } 1085107034Sdavidxu } 1086111028Sjeff return (0); 1087107034Sdavidxu} 1088107034Sdavidxu 1089111028Sjeff/* 1090111515Sdavidxu * Export state clock ticks for userland 1091111028Sjeff */ 1092107034Sdavidxustatic int 1093111515Sdavidxuthread_update_usr_ticks(struct thread *td, int user) 1094107034Sdavidxu{ 1095107034Sdavidxu struct proc *p = td->td_proc; 1096107034Sdavidxu struct kse_thr_mailbox *tmbx; 1097111028Sjeff struct kse_upcall *ku; 1098112397Sdavidxu struct ksegrp *kg; 1099107034Sdavidxu caddr_t addr; 1100111028Sjeff uint uticks; 1101107034Sdavidxu 1102111028Sjeff if ((ku = td->td_upcall) == NULL) 1103111028Sjeff return (-1); 1104111028Sjeff 1105111028Sjeff tmbx = (void *)fuword((void *)&ku->ku_mailbox->km_curthread); 1106107034Sdavidxu if ((tmbx == NULL) || (tmbx == (void *)-1)) 1107111028Sjeff return (-1); 1108111515Sdavidxu if (user) { 1109111515Sdavidxu uticks = td->td_uuticks; 1110111515Sdavidxu td->td_uuticks = 0; 1111111515Sdavidxu addr = (caddr_t)&tmbx->tm_uticks; 1112111515Sdavidxu } else { 1113111515Sdavidxu uticks = td->td_usticks; 1114111515Sdavidxu td->td_usticks = 0; 1115111515Sdavidxu addr = (caddr_t)&tmbx->tm_sticks; 1116111515Sdavidxu } 1117107034Sdavidxu if (uticks) { 1118111515Sdavidxu if (suword(addr, uticks+fuword(addr))) { 1119111028Sjeff PROC_LOCK(p); 1120111028Sjeff psignal(p, SIGSEGV); 1121111028Sjeff PROC_UNLOCK(p); 1122111028Sjeff return (-2); 1123111028Sjeff } 1124107034Sdavidxu } 1125112397Sdavidxu kg = td->td_ksegrp; 1126112397Sdavidxu if (kg->kg_upquantum && ticks >= kg->kg_nextupcall) { 1127112397Sdavidxu mtx_lock_spin(&sched_lock); 1128112397Sdavidxu td->td_upcall->ku_flags |= KUF_DOUPCALL; 1129112397Sdavidxu mtx_unlock_spin(&sched_lock); 1130112397Sdavidxu } 1131111028Sjeff return (0); 1132111028Sjeff} 1133111028Sjeff 1134111028Sjeff/* 113599026Sjulian * Discard the current thread and exit from its context. 113699026Sjulian * 113799026Sjulian * Because we can't free a thread while we're operating under its context, 1138107719Sjulian * push the current thread into our CPU's deadthread holder. This means 1139107719Sjulian * we needn't worry about someone else grabbing our context before we 1140107719Sjulian * do a cpu_throw(). 114199026Sjulian */ 114299026Sjulianvoid 114399026Sjulianthread_exit(void) 114499026Sjulian{ 114599026Sjulian struct thread *td; 114699026Sjulian struct kse *ke; 114799026Sjulian struct proc *p; 114899026Sjulian struct ksegrp *kg; 114999026Sjulian 115099026Sjulian td = curthread; 115199026Sjulian kg = td->td_ksegrp; 115299026Sjulian p = td->td_proc; 115399026Sjulian ke = td->td_kse; 115499026Sjulian 115599026Sjulian mtx_assert(&sched_lock, MA_OWNED); 1156102581Sjulian KASSERT(p != NULL, ("thread exiting without a process")); 1157102581Sjulian KASSERT(ke != NULL, ("thread exiting without a kse")); 1158102581Sjulian KASSERT(kg != NULL, ("thread exiting without a kse group")); 115999026Sjulian PROC_LOCK_ASSERT(p, MA_OWNED); 116099026Sjulian CTR1(KTR_PROC, "thread_exit: thread %p", td); 116199026Sjulian KASSERT(!mtx_owned(&Giant), ("dying thread owns giant")); 116299026Sjulian 1163104695Sjulian if (td->td_standin != NULL) { 1164104695Sjulian thread_stash(td->td_standin); 1165104695Sjulian td->td_standin = NULL; 1166104695Sjulian } 1167104695Sjulian 116899026Sjulian cpu_thread_exit(td); /* XXXSMP */ 116999026Sjulian 1170102581Sjulian /* 1171103002Sjulian * The last thread is left attached to the process 1172103002Sjulian * So that the whole bundle gets recycled. Skip 1173103002Sjulian * all this stuff. 1174102581Sjulian */ 1175103002Sjulian if (p->p_numthreads > 1) { 1176113641Sjulian thread_unlink(td); 1177111115Sdavidxu if (p->p_maxthrwaits) 1178111115Sdavidxu wakeup(&p->p_numthreads); 1179103002Sjulian /* 1180103002Sjulian * The test below is NOT true if we are the 1181103002Sjulian * sole exiting thread. P_STOPPED_SNGL is unset 1182103002Sjulian * in exit1() after it is the only survivor. 1183103002Sjulian */ 1184103002Sjulian if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 1185103002Sjulian if (p->p_numthreads == p->p_suspcount) { 1186103216Sjulian thread_unsuspend_one(p->p_singlethread); 1187103002Sjulian } 118899026Sjulian } 1189104695Sjulian 1190111028Sjeff /* 1191111028Sjeff * Because each upcall structure has an owner thread, 1192111028Sjeff * owner thread exits only when process is in exiting 1193111028Sjeff * state, so upcall to userland is no longer needed, 1194111028Sjeff * deleting upcall structure is safe here. 1195111028Sjeff * So when all threads in a group is exited, all upcalls 1196111028Sjeff * in the group should be automatically freed. 1197111028Sjeff */ 1198111028Sjeff if (td->td_upcall) 1199111028Sjeff upcall_remove(td); 1200111028Sjeff 1201104695Sjulian ke->ke_state = KES_UNQUEUED; 1202111028Sjeff ke->ke_thread = NULL; 1203104695Sjulian /* 1204108338Sjulian * Decide what to do with the KSE attached to this thread. 1205104695Sjulian */ 1206111028Sjeff if (ke->ke_flags & KEF_EXIT) 1207105854Sjulian kse_unlink(ke); 1208111028Sjeff else 1209105854Sjulian kse_reassign(ke); 1210105854Sjulian PROC_UNLOCK(p); 1211111028Sjeff td->td_kse = NULL; 1212105854Sjulian td->td_state = TDS_INACTIVE; 1213113244Sdavidxu#if 0 1214105854Sjulian td->td_proc = NULL; 1215113244Sdavidxu#endif 1216105854Sjulian td->td_ksegrp = NULL; 1217105854Sjulian td->td_last_kse = NULL; 1218107719Sjulian PCPU_SET(deadthread, td); 1219103002Sjulian } else { 1220103002Sjulian PROC_UNLOCK(p); 122199026Sjulian } 1222112888Sjeff /* XXX Shouldn't cpu_throw() here. */ 1223112993Speter mtx_assert(&sched_lock, MA_OWNED); 1224112993Speter#if defined(__i386__) || defined(__sparc64__) 1225112993Speter cpu_throw(td, choosethread()); 1226112993Speter#else 122799026Sjulian cpu_throw(); 1228112993Speter#endif 1229112993Speter panic("I'm a teapot!"); 123099026Sjulian /* NOTREACHED */ 123199026Sjulian} 123299026Sjulian 1233107719Sjulian/* 1234107719Sjulian * Do any thread specific cleanups that may be needed in wait() 1235107719Sjulian * called with Giant held, proc and schedlock not held. 1236107719Sjulian */ 1237107719Sjulianvoid 1238107719Sjulianthread_wait(struct proc *p) 1239107719Sjulian{ 1240107719Sjulian struct thread *td; 1241107719Sjulian 1242107719Sjulian KASSERT((p->p_numthreads == 1), ("Muliple threads in wait1()")); 1243107719Sjulian KASSERT((p->p_numksegrps == 1), ("Muliple ksegrps in wait1()")); 1244107719Sjulian FOREACH_THREAD_IN_PROC(p, td) { 1245107719Sjulian if (td->td_standin != NULL) { 1246107719Sjulian thread_free(td->td_standin); 1247107719Sjulian td->td_standin = NULL; 1248107719Sjulian } 1249107719Sjulian cpu_thread_clean(td); 1250107719Sjulian } 1251107719Sjulian thread_reap(); /* check for zombie threads etc. */ 1252107719Sjulian} 1253107719Sjulian 125499026Sjulian/* 125599026Sjulian * Link a thread to a process. 1256103002Sjulian * set up anything that needs to be initialized for it to 1257103002Sjulian * be used by the process. 125899026Sjulian * 125999026Sjulian * Note that we do not link to the proc's ucred here. 126099026Sjulian * The thread is linked as if running but no KSE assigned. 126199026Sjulian */ 126299026Sjulianvoid 126399026Sjulianthread_link(struct thread *td, struct ksegrp *kg) 126499026Sjulian{ 126599026Sjulian struct proc *p; 126699026Sjulian 126799026Sjulian p = kg->kg_proc; 1268111028Sjeff td->td_state = TDS_INACTIVE; 1269111028Sjeff td->td_proc = p; 1270111028Sjeff td->td_ksegrp = kg; 1271111028Sjeff td->td_last_kse = NULL; 1272111028Sjeff td->td_flags = 0; 1273111028Sjeff td->td_kse = NULL; 127499026Sjulian 1275103002Sjulian LIST_INIT(&td->td_contested); 1276103002Sjulian callout_init(&td->td_slpcallout, 1); 127799026Sjulian TAILQ_INSERT_HEAD(&p->p_threads, td, td_plist); 127899026Sjulian TAILQ_INSERT_HEAD(&kg->kg_threads, td, td_kglist); 127999026Sjulian p->p_numthreads++; 128099026Sjulian kg->kg_numthreads++; 128199026Sjulian} 128299026Sjulian 1283113641Sjulianvoid 1284113641Sjulianthread_unlink(struct thread *td) 1285113641Sjulian{ 1286113641Sjulian struct proc *p = td->td_proc; 1287113641Sjulian struct ksegrp *kg = td->td_ksegrp; 1288113920Sjhb 1289113920Sjhb mtx_assert(&sched_lock, MA_OWNED); 1290113641Sjulian TAILQ_REMOVE(&p->p_threads, td, td_plist); 1291113641Sjulian p->p_numthreads--; 1292113641Sjulian TAILQ_REMOVE(&kg->kg_threads, td, td_kglist); 1293113641Sjulian kg->kg_numthreads--; 1294113641Sjulian /* could clear a few other things here */ 1295113641Sjulian} 1296113641Sjulian 1297111028Sjeff/* 1298111028Sjeff * Purge a ksegrp resource. When a ksegrp is preparing to 1299111028Sjeff * exit, it calls this function. 1300111028Sjeff */ 1301113864Sjhbstatic void 1302111028Sjeffkse_purge_group(struct thread *td) 1303111028Sjeff{ 1304111028Sjeff struct ksegrp *kg; 1305111028Sjeff struct kse *ke; 1306111028Sjeff 1307111028Sjeff kg = td->td_ksegrp; 1308111028Sjeff KASSERT(kg->kg_numthreads == 1, ("%s: bad thread number", __func__)); 1309111028Sjeff while ((ke = TAILQ_FIRST(&kg->kg_iq)) != NULL) { 1310111028Sjeff KASSERT(ke->ke_state == KES_IDLE, 1311111028Sjeff ("%s: wrong idle KSE state", __func__)); 1312111028Sjeff kse_unlink(ke); 1313111028Sjeff } 1314111028Sjeff KASSERT((kg->kg_kses == 1), 1315111028Sjeff ("%s: ksegrp still has %d KSEs", __func__, kg->kg_kses)); 1316111028Sjeff KASSERT((kg->kg_numupcalls == 0), 1317111028Sjeff ("%s: ksegrp still has %d upcall datas", 1318111028Sjeff __func__, kg->kg_numupcalls)); 1319111028Sjeff} 1320111028Sjeff 1321111028Sjeff/* 1322111028Sjeff * Purge a process's KSE resource. When a process is preparing to 1323111028Sjeff * exit, it calls kse_purge to release any extra KSE resources in 1324111028Sjeff * the process. 1325111028Sjeff */ 1326113864Sjhbstatic void 1327105854Sjuliankse_purge(struct proc *p, struct thread *td) 1328105854Sjulian{ 1329105854Sjulian struct ksegrp *kg; 1330111028Sjeff struct kse *ke; 1331105854Sjulian 1332105854Sjulian KASSERT(p->p_numthreads == 1, ("bad thread number")); 1333105854Sjulian while ((kg = TAILQ_FIRST(&p->p_ksegrps)) != NULL) { 1334105854Sjulian TAILQ_REMOVE(&p->p_ksegrps, kg, kg_ksegrp); 1335105854Sjulian p->p_numksegrps--; 1336111028Sjeff /* 1337111028Sjeff * There is no ownership for KSE, after all threads 1338111028Sjeff * in the group exited, it is possible that some KSEs 1339111028Sjeff * were left in idle queue, gc them now. 1340111028Sjeff */ 1341111028Sjeff while ((ke = TAILQ_FIRST(&kg->kg_iq)) != NULL) { 1342111028Sjeff KASSERT(ke->ke_state == KES_IDLE, 1343111028Sjeff ("%s: wrong idle KSE state", __func__)); 1344111028Sjeff TAILQ_REMOVE(&kg->kg_iq, ke, ke_kgrlist); 1345111028Sjeff kg->kg_idle_kses--; 1346111028Sjeff TAILQ_REMOVE(&kg->kg_kseq, ke, ke_kglist); 1347111028Sjeff kg->kg_kses--; 1348111028Sjeff kse_stash(ke); 1349111028Sjeff } 1350105854Sjulian KASSERT(((kg->kg_kses == 0) && (kg != td->td_ksegrp)) || 1351111028Sjeff ((kg->kg_kses == 1) && (kg == td->td_ksegrp)), 1352111028Sjeff ("ksegrp has wrong kg_kses: %d", kg->kg_kses)); 1353111028Sjeff KASSERT((kg->kg_numupcalls == 0), 1354111028Sjeff ("%s: ksegrp still has %d upcall datas", 1355111028Sjeff __func__, kg->kg_numupcalls)); 1356111028Sjeff 1357111028Sjeff if (kg != td->td_ksegrp) 1358105854Sjulian ksegrp_stash(kg); 1359105854Sjulian } 1360105854Sjulian TAILQ_INSERT_HEAD(&p->p_ksegrps, td->td_ksegrp, kg_ksegrp); 1361105854Sjulian p->p_numksegrps++; 1362105854Sjulian} 1363105854Sjulian 1364111028Sjeff/* 1365111028Sjeff * This function is intended to be used to initialize a spare thread 1366111028Sjeff * for upcall. Initialize thread's large data area outside sched_lock 1367111028Sjeff * for thread_schedule_upcall(). 1368111028Sjeff */ 1369111028Sjeffvoid 1370111028Sjeffthread_alloc_spare(struct thread *td, struct thread *spare) 1371111028Sjeff{ 1372111028Sjeff if (td->td_standin) 1373111028Sjeff return; 1374111028Sjeff if (spare == NULL) 1375111028Sjeff spare = thread_alloc(); 1376111028Sjeff td->td_standin = spare; 1377111028Sjeff bzero(&spare->td_startzero, 1378111028Sjeff (unsigned)RANGEOF(struct thread, td_startzero, td_endzero)); 1379111028Sjeff spare->td_proc = td->td_proc; 1380111028Sjeff spare->td_ucred = crhold(td->td_ucred); 1381111028Sjeff} 1382105854Sjulian 138399026Sjulian/* 1384103410Smini * Create a thread and schedule it for upcall on the KSE given. 1385108338Sjulian * Use our thread's standin so that we don't have to allocate one. 138699026Sjulian */ 138799026Sjulianstruct thread * 1388111028Sjeffthread_schedule_upcall(struct thread *td, struct kse_upcall *ku) 138999026Sjulian{ 139099026Sjulian struct thread *td2; 139199026Sjulian 139299026Sjulian mtx_assert(&sched_lock, MA_OWNED); 1393104695Sjulian 1394104695Sjulian /* 1395111028Sjeff * Schedule an upcall thread on specified kse_upcall, 1396111028Sjeff * the kse_upcall must be free. 1397111028Sjeff * td must have a spare thread. 1398104695Sjulian */ 1399111028Sjeff KASSERT(ku->ku_owner == NULL, ("%s: upcall has owner", __func__)); 1400104695Sjulian if ((td2 = td->td_standin) != NULL) { 1401104695Sjulian td->td_standin = NULL; 140299026Sjulian } else { 1403111028Sjeff panic("no reserve thread when scheduling an upcall"); 1404106182Sdavidxu return (NULL); 140599026Sjulian } 140699026Sjulian CTR3(KTR_PROC, "thread_schedule_upcall: thread %p (pid %d, %s)", 1407104695Sjulian td2, td->td_proc->p_pid, td->td_proc->p_comm); 1408103002Sjulian bcopy(&td->td_startcopy, &td2->td_startcopy, 1409103002Sjulian (unsigned) RANGEOF(struct thread, td_startcopy, td_endcopy)); 1410111028Sjeff thread_link(td2, ku->ku_ksegrp); 1411113244Sdavidxu /* inherit blocked thread's context */ 1412113244Sdavidxu bcopy(td->td_frame, td2->td_frame, sizeof(struct trapframe)); 1413113244Sdavidxu cpu_set_upcall(td2, td->td_pcb); 1414111028Sjeff /* Let the new thread become owner of the upcall */ 1415111028Sjeff ku->ku_owner = td2; 1416111028Sjeff td2->td_upcall = ku; 1417111028Sjeff td2->td_flags = TDF_UPCALLING; 1418112888Sjeff#if 0 /* XXX This shouldn't be necessary */ 1419111041Sdavidxu if (td->td_proc->p_sflag & PS_NEEDSIGCHK) 1420111041Sdavidxu td2->td_flags |= TDF_ASTPENDING; 1421112888Sjeff#endif 1422111028Sjeff td2->td_kse = NULL; 1423111028Sjeff td2->td_state = TDS_CAN_RUN; 1424104695Sjulian td2->td_inhibitors = 0; 1425111028Sjeff setrunqueue(td2); 1426104695Sjulian return (td2); /* bogus.. should be a void function */ 142799026Sjulian} 142899026Sjulian 1429111033Sjeffvoid 1430111033Sjeffthread_signal_add(struct thread *td, int sig) 1431103410Smini{ 1432111033Sjeff struct kse_upcall *ku; 1433111033Sjeff struct proc *p; 1434103410Smini sigset_t ss; 1435103410Smini int error; 1436103410Smini 1437111033Sjeff PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); 1438111033Sjeff td = curthread; 1439111033Sjeff ku = td->td_upcall; 1440111033Sjeff p = td->td_proc; 1441111033Sjeff 1442103410Smini PROC_UNLOCK(p); 1443111033Sjeff error = copyin(&ku->ku_mailbox->km_sigscaught, &ss, sizeof(sigset_t)); 1444103410Smini if (error) 1445111033Sjeff goto error; 1446111033Sjeff 1447103410Smini SIGADDSET(ss, sig); 1448111033Sjeff 1449111033Sjeff error = copyout(&ss, &ku->ku_mailbox->km_sigscaught, sizeof(sigset_t)); 1450111033Sjeff if (error) 1451111033Sjeff goto error; 1452111033Sjeff 1453103410Smini PROC_LOCK(p); 1454111033Sjeff return; 1455111033Sjefferror: 1456111033Sjeff PROC_LOCK(p); 1457111033Sjeff sigexit(td, SIGILL); 1458111033Sjeff} 1459111033Sjeff 1460111033Sjeff 1461111033Sjeff/* 1462111033Sjeff * Schedule an upcall to notify a KSE process recieved signals. 1463111033Sjeff * 1464111033Sjeff */ 1465111033Sjeffvoid 1466111033Sjeffthread_signal_upcall(struct thread *td) 1467111033Sjeff{ 1468103410Smini mtx_lock_spin(&sched_lock); 1469111033Sjeff td->td_flags |= TDF_UPCALLING; 1470103410Smini mtx_unlock_spin(&sched_lock); 1471111033Sjeff 1472111033Sjeff return; 1473103410Smini} 1474103410Smini 1475112397Sdavidxuvoid 1476112397Sdavidxuthread_switchout(struct thread *td) 1477112397Sdavidxu{ 1478112397Sdavidxu struct kse_upcall *ku; 1479112397Sdavidxu 1480112397Sdavidxu mtx_assert(&sched_lock, MA_OWNED); 1481112397Sdavidxu 1482112397Sdavidxu /* 1483112397Sdavidxu * If the outgoing thread is in threaded group and has never 1484112397Sdavidxu * scheduled an upcall, decide whether this is a short 1485112397Sdavidxu * or long term event and thus whether or not to schedule 1486112397Sdavidxu * an upcall. 1487112397Sdavidxu * If it is a short term event, just suspend it in 1488112397Sdavidxu * a way that takes its KSE with it. 1489112397Sdavidxu * Select the events for which we want to schedule upcalls. 1490112397Sdavidxu * For now it's just sleep. 1491112397Sdavidxu * XXXKSE eventually almost any inhibition could do. 1492112397Sdavidxu */ 1493112397Sdavidxu if (TD_CAN_UNBIND(td) && (td->td_standin) && TD_ON_SLEEPQ(td)) { 1494112397Sdavidxu /* 1495112397Sdavidxu * Release ownership of upcall, and schedule an upcall 1496112397Sdavidxu * thread, this new upcall thread becomes the owner of 1497112397Sdavidxu * the upcall structure. 1498112397Sdavidxu */ 1499112397Sdavidxu ku = td->td_upcall; 1500112397Sdavidxu ku->ku_owner = NULL; 1501112397Sdavidxu td->td_upcall = NULL; 1502112397Sdavidxu td->td_flags &= ~TDF_CAN_UNBIND; 1503112397Sdavidxu thread_schedule_upcall(td, ku); 1504112397Sdavidxu } 1505112397Sdavidxu} 1506112397Sdavidxu 1507103410Smini/* 1508111028Sjeff * Setup done on the thread when it enters the kernel. 1509105900Sjulian * XXXKSE Presently only for syscalls but eventually all kernel entries. 1510105900Sjulian */ 1511105900Sjulianvoid 1512105900Sjulianthread_user_enter(struct proc *p, struct thread *td) 1513105900Sjulian{ 1514111028Sjeff struct ksegrp *kg; 1515111028Sjeff struct kse_upcall *ku; 1516113793Sdavidxu struct kse_thr_mailbox *tmbx; 1517105900Sjulian 1518111028Sjeff kg = td->td_ksegrp; 1519113793Sdavidxu 1520105900Sjulian /* 1521105900Sjulian * First check that we shouldn't just abort. 1522105900Sjulian * But check if we are the single thread first! 1523105900Sjulian */ 1524113686Sjhb PROC_LOCK(p); 1525111028Sjeff if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) { 1526105900Sjulian mtx_lock_spin(&sched_lock); 1527112071Sdavidxu thread_stopped(p); 1528105900Sjulian thread_exit(); 1529105900Sjulian /* NOTREACHED */ 1530105900Sjulian } 1531113686Sjhb PROC_UNLOCK(p); 1532105900Sjulian 1533105900Sjulian /* 1534105900Sjulian * If we are doing a syscall in a KSE environment, 1535105900Sjulian * note where our mailbox is. There is always the 1536108338Sjulian * possibility that we could do this lazily (in kse_reassign()), 1537105900Sjulian * but for now do it every time. 1538105900Sjulian */ 1539111028Sjeff kg = td->td_ksegrp; 1540111028Sjeff if (kg->kg_numupcalls) { 1541111028Sjeff ku = td->td_upcall; 1542111028Sjeff KASSERT(ku, ("%s: no upcall owned", __func__)); 1543111028Sjeff KASSERT((ku->ku_owner == td), ("%s: wrong owner", __func__)); 1544113793Sdavidxu KASSERT(!TD_CAN_UNBIND(td), ("%s: can unbind", __func__)); 1545113793Sdavidxu ku->ku_mflags = fuword((void *)&ku->ku_mailbox->km_flags); 1546113793Sdavidxu tmbx = (void *)fuword((void *)&ku->ku_mailbox->km_curthread); 1547113793Sdavidxu if ((tmbx == NULL) || (tmbx == (void *)-1)) { 1548111028Sjeff td->td_mailbox = NULL; 1549105900Sjulian } else { 1550113793Sdavidxu td->td_mailbox = tmbx; 1551111115Sdavidxu if (td->td_standin == NULL) 1552111115Sdavidxu thread_alloc_spare(td, NULL); 1553111115Sdavidxu mtx_lock_spin(&sched_lock); 1554113793Sdavidxu if (ku->ku_mflags & KMF_NOUPCALL) 1555113793Sdavidxu td->td_flags &= ~TDF_CAN_UNBIND; 1556113793Sdavidxu else 1557113793Sdavidxu td->td_flags |= TDF_CAN_UNBIND; 1558111115Sdavidxu mtx_unlock_spin(&sched_lock); 1559105900Sjulian } 1560105900Sjulian } 1561105900Sjulian} 1562105900Sjulian 1563105900Sjulian/* 1564103410Smini * The extra work we go through if we are a threaded process when we 1565103410Smini * return to userland. 1566103410Smini * 156799026Sjulian * If we are a KSE process and returning to user mode, check for 156899026Sjulian * extra work to do before we return (e.g. for more syscalls 156999026Sjulian * to complete first). If we were in a critical section, we should 157099026Sjulian * just return to let it finish. Same if we were in the UTS (in 1571103410Smini * which case the mailbox's context's busy indicator will be set). 1572103410Smini * The only traps we suport will have set the mailbox. 1573103410Smini * We will clear it here. 157499026Sjulian */ 157599026Sjulianint 1576103838Sjulianthread_userret(struct thread *td, struct trapframe *frame) 157799026Sjulian{ 1578113793Sdavidxu int error = 0, upcalls, uts_crit; 1579111028Sjeff struct kse_upcall *ku; 1580111115Sdavidxu struct ksegrp *kg, *kg2; 1581104695Sjulian struct proc *p; 1582107060Sdavidxu struct timespec ts; 158399026Sjulian 1584111028Sjeff p = td->td_proc; 1585110190Sjulian kg = td->td_ksegrp; 1586104695Sjulian 1587111028Sjeff /* Nothing to do with non-threaded group/process */ 1588111028Sjeff if (td->td_ksegrp->kg_numupcalls == 0) 1589111028Sjeff return (0); 1590108338Sjulian 1591103410Smini /* 1592111028Sjeff * Stat clock interrupt hit in userland, it 1593111028Sjeff * is returning from interrupt, charge thread's 1594111028Sjeff * userland time for UTS. 1595103410Smini */ 1596111028Sjeff if (td->td_flags & TDF_USTATCLOCK) { 1597111515Sdavidxu thread_update_usr_ticks(td, 1); 1598111028Sjeff mtx_lock_spin(&sched_lock); 1599111028Sjeff td->td_flags &= ~TDF_USTATCLOCK; 1600111028Sjeff mtx_unlock_spin(&sched_lock); 1601111515Sdavidxu if (kg->kg_completed || 1602111515Sdavidxu (td->td_upcall->ku_flags & KUF_DOUPCALL)) 1603111515Sdavidxu thread_user_enter(p, td); 1604111028Sjeff } 1605108338Sjulian 1606113793Sdavidxu uts_crit = (td->td_mailbox == NULL); 1607113793Sdavidxu ku = td->td_upcall; 1608111028Sjeff /* 1609111028Sjeff * Optimisation: 1610111028Sjeff * This thread has not started any upcall. 1611111028Sjeff * If there is no work to report other than ourself, 1612111028Sjeff * then it can return direct to userland. 1613111028Sjeff */ 1614108338Sjulian if (TD_CAN_UNBIND(td)) { 1615111028Sjeff mtx_lock_spin(&sched_lock); 1616111028Sjeff td->td_flags &= ~TDF_CAN_UNBIND; 1617112888Sjeff if ((td->td_flags & TDF_NEEDSIGCHK) == 0 && 1618112077Sdavidxu (kg->kg_completed == NULL) && 1619112397Sdavidxu (ku->ku_flags & KUF_DOUPCALL) == 0 && 1620113708Sdavidxu (kg->kg_upquantum && ticks < kg->kg_nextupcall)) { 1621112888Sjeff mtx_unlock_spin(&sched_lock); 1622111515Sdavidxu thread_update_usr_ticks(td, 0); 1623112222Sdavidxu nanotime(&ts); 1624112397Sdavidxu error = copyout(&ts, 1625112222Sdavidxu (caddr_t)&ku->ku_mailbox->km_timeofday, 1626112222Sdavidxu sizeof(ts)); 1627112077Sdavidxu td->td_mailbox = 0; 1628113793Sdavidxu ku->ku_mflags = 0; 1629112222Sdavidxu if (error) 1630112222Sdavidxu goto out; 1631112077Sdavidxu return (0); 1632108338Sjulian } 1633112888Sjeff mtx_unlock_spin(&sched_lock); 1634104695Sjulian error = thread_export_context(td); 1635104695Sjulian if (error) { 1636104695Sjulian /* 1637111028Sjeff * Failing to do the KSE operation just defaults 1638104695Sjulian * back to synchonous operation, so just return from 1639108338Sjulian * the syscall. 1640104695Sjulian */ 1641113793Sdavidxu goto out; 1642104695Sjulian } 1643104695Sjulian /* 1644111028Sjeff * There is something to report, and we own an upcall 1645111028Sjeff * strucuture, we can go to userland. 1646111028Sjeff * Turn ourself into an upcall thread. 1647104695Sjulian */ 1648111028Sjeff mtx_lock_spin(&sched_lock); 1649104695Sjulian td->td_flags |= TDF_UPCALLING; 1650108338Sjulian mtx_unlock_spin(&sched_lock); 1651113793Sdavidxu } else if (td->td_mailbox && (ku == NULL)) { 1652108338Sjulian error = thread_export_context(td); 1653112071Sdavidxu /* possibly upcall with error? */ 1654112071Sdavidxu PROC_LOCK(p); 1655112071Sdavidxu /* 1656112071Sdavidxu * There are upcall threads waiting for 1657112071Sdavidxu * work to do, wake one of them up. 1658112071Sdavidxu * XXXKSE Maybe wake all of them up. 1659112071Sdavidxu */ 1660112071Sdavidxu if (!error && kg->kg_upsleeps) 1661112071Sdavidxu wakeup_one(&kg->kg_completed); 1662112071Sdavidxu mtx_lock_spin(&sched_lock); 1663112071Sdavidxu thread_stopped(p); 1664108338Sjulian thread_exit(); 1665111028Sjeff /* NOTREACHED */ 1666104695Sjulian } 1667104695Sjulian 1668111154Sdavidxu KASSERT(TD_CAN_UNBIND(td) == 0, ("can unbind")); 1669111154Sdavidxu 1670111154Sdavidxu if (p->p_numthreads > max_threads_per_proc) { 1671111154Sdavidxu max_threads_hits++; 1672111154Sdavidxu PROC_LOCK(p); 1673113920Sjhb mtx_lock_spin(&sched_lock); 1674111154Sdavidxu while (p->p_numthreads > max_threads_per_proc) { 1675111154Sdavidxu if (P_SHOULDSTOP(p)) 1676111154Sdavidxu break; 1677111154Sdavidxu upcalls = 0; 1678111154Sdavidxu FOREACH_KSEGRP_IN_PROC(p, kg2) { 1679111154Sdavidxu if (kg2->kg_numupcalls == 0) 1680111154Sdavidxu upcalls++; 1681111154Sdavidxu else 1682111154Sdavidxu upcalls += kg2->kg_numupcalls; 1683111154Sdavidxu } 1684111154Sdavidxu if (upcalls >= max_threads_per_proc) 1685111154Sdavidxu break; 1686114106Sdavidxu mtx_unlock_spin(&sched_lock); 1687111154Sdavidxu p->p_maxthrwaits++; 1688111154Sdavidxu msleep(&p->p_numthreads, &p->p_mtx, PPAUSE|PCATCH, 1689111154Sdavidxu "maxthreads", NULL); 1690111154Sdavidxu p->p_maxthrwaits--; 1691113920Sjhb mtx_lock_spin(&sched_lock); 1692111154Sdavidxu } 1693113920Sjhb mtx_unlock_spin(&sched_lock); 1694111154Sdavidxu PROC_UNLOCK(p); 1695111154Sdavidxu } 1696111154Sdavidxu 1697108338Sjulian if (td->td_flags & TDF_UPCALLING) { 1698113793Sdavidxu uts_crit = 0; 1699112397Sdavidxu kg->kg_nextupcall = ticks+kg->kg_upquantum; 1700108338Sjulian /* 1701108338Sjulian * There is no more work to do and we are going to ride 1702111028Sjeff * this thread up to userland as an upcall. 1703108338Sjulian * Do the last parts of the setup needed for the upcall. 1704108338Sjulian */ 1705108338Sjulian CTR3(KTR_PROC, "userret: upcall thread %p (pid %d, %s)", 1706108338Sjulian td, td->td_proc->p_pid, td->td_proc->p_comm); 1707104695Sjulian 1708111028Sjeff mtx_lock_spin(&sched_lock); 1709111028Sjeff td->td_flags &= ~TDF_UPCALLING; 1710111028Sjeff if (ku->ku_flags & KUF_DOUPCALL) 1711111028Sjeff ku->ku_flags &= ~KUF_DOUPCALL; 1712111028Sjeff mtx_unlock_spin(&sched_lock); 1713111028Sjeff 1714111028Sjeff /* 1715113793Sdavidxu * Set user context to the UTS 1716113793Sdavidxu */ 1717113793Sdavidxu if (!(ku->ku_mflags & KMF_NOUPCALL)) { 1718113793Sdavidxu cpu_set_upcall_kse(td, ku); 1719113793Sdavidxu error = suword(&ku->ku_mailbox->km_curthread, 0); 1720113793Sdavidxu if (error) 1721113793Sdavidxu goto out; 1722113793Sdavidxu } 1723113793Sdavidxu 1724113793Sdavidxu /* 1725108338Sjulian * Unhook the list of completed threads. 1726108338Sjulian * anything that completes after this gets to 1727108338Sjulian * come in next time. 1728108338Sjulian * Put the list of completed thread mailboxes on 1729108338Sjulian * this KSE's mailbox. 1730108338Sjulian */ 1731113793Sdavidxu if (!(ku->ku_mflags & KMF_NOCOMPLETED) && 1732113793Sdavidxu (error = thread_link_mboxes(kg, ku)) != 0) 1733111115Sdavidxu goto out; 1734113793Sdavidxu } 1735113793Sdavidxu if (!uts_crit) { 1736107060Sdavidxu nanotime(&ts); 1737113793Sdavidxu error = copyout(&ts, &ku->ku_mailbox->km_timeofday, sizeof(ts)); 1738111115Sdavidxu } 1739111115Sdavidxu 1740111115Sdavidxuout: 1741111115Sdavidxu if (error) { 1742111115Sdavidxu /* 1743111129Sdavidxu * Things are going to be so screwed we should just kill 1744111129Sdavidxu * the process. 1745111115Sdavidxu * how do we do that? 1746111115Sdavidxu */ 1747111115Sdavidxu PROC_LOCK(td->td_proc); 1748111115Sdavidxu psignal(td->td_proc, SIGSEGV); 1749111115Sdavidxu PROC_UNLOCK(td->td_proc); 1750111115Sdavidxu } else { 1751111115Sdavidxu /* 1752111115Sdavidxu * Optimisation: 1753111115Sdavidxu * Ensure that we have a spare thread available, 1754111115Sdavidxu * for when we re-enter the kernel. 1755111115Sdavidxu */ 1756111115Sdavidxu if (td->td_standin == NULL) 1757111115Sdavidxu thread_alloc_spare(td, NULL); 1758111115Sdavidxu } 1759111115Sdavidxu 1760113793Sdavidxu ku->ku_mflags = 0; 1761111028Sjeff /* 1762111028Sjeff * Clear thread mailbox first, then clear system tick count. 1763111028Sjeff * The order is important because thread_statclock() use 1764111028Sjeff * mailbox pointer to see if it is an userland thread or 1765111028Sjeff * an UTS kernel thread. 1766111028Sjeff */ 1767108338Sjulian td->td_mailbox = NULL; 1768111028Sjeff td->td_usticks = 0; 1769104695Sjulian return (error); /* go sync */ 177099026Sjulian} 177199026Sjulian 177299026Sjulian/* 177399026Sjulian * Enforce single-threading. 177499026Sjulian * 177599026Sjulian * Returns 1 if the caller must abort (another thread is waiting to 177699026Sjulian * exit the process or similar). Process is locked! 177799026Sjulian * Returns 0 when you are successfully the only thread running. 177899026Sjulian * A process has successfully single threaded in the suspend mode when 177999026Sjulian * There are no threads in user mode. Threads in the kernel must be 178099026Sjulian * allowed to continue until they get to the user boundary. They may even 178199026Sjulian * copy out their return values and data before suspending. They may however be 178299026Sjulian * accellerated in reaching the user boundary as we will wake up 178399026Sjulian * any sleeping threads that are interruptable. (PCATCH). 178499026Sjulian */ 178599026Sjulianint 178699026Sjulianthread_single(int force_exit) 178799026Sjulian{ 178899026Sjulian struct thread *td; 178999026Sjulian struct thread *td2; 179099026Sjulian struct proc *p; 179199026Sjulian 179299026Sjulian td = curthread; 179399026Sjulian p = td->td_proc; 1794107719Sjulian mtx_assert(&Giant, MA_OWNED); 179599026Sjulian PROC_LOCK_ASSERT(p, MA_OWNED); 179699026Sjulian KASSERT((td != NULL), ("curthread is NULL")); 179799026Sjulian 1798112910Sjeff if ((p->p_flag & P_THREADED) == 0 && p->p_numthreads == 1) 179999026Sjulian return (0); 180099026Sjulian 1801100648Sjulian /* Is someone already single threading? */ 1802100648Sjulian if (p->p_singlethread) 180399026Sjulian return (1); 180499026Sjulian 1805108338Sjulian if (force_exit == SINGLE_EXIT) { 180699026Sjulian p->p_flag |= P_SINGLE_EXIT; 1807108338Sjulian } else 180899026Sjulian p->p_flag &= ~P_SINGLE_EXIT; 1809102950Sdavidxu p->p_flag |= P_STOPPED_SINGLE; 1810113920Sjhb mtx_lock_spin(&sched_lock); 181199026Sjulian p->p_singlethread = td; 181299026Sjulian while ((p->p_numthreads - p->p_suspcount) != 1) { 181399026Sjulian FOREACH_THREAD_IN_PROC(p, td2) { 181499026Sjulian if (td2 == td) 181599026Sjulian continue; 1816113705Sdavidxu td2->td_flags |= TDF_ASTPENDING; 1817103216Sjulian if (TD_IS_INHIBITED(td2)) { 1818105911Sjulian if (force_exit == SINGLE_EXIT) { 1819105911Sjulian if (TD_IS_SUSPENDED(td2)) { 1820103216Sjulian thread_unsuspend_one(td2); 1821105911Sjulian } 1822105911Sjulian if (TD_ON_SLEEPQ(td2) && 1823105911Sjulian (td2->td_flags & TDF_SINTR)) { 1824105911Sjulian if (td2->td_flags & TDF_CVWAITQ) 1825105911Sjulian cv_abort(td2); 1826105911Sjulian else 1827105911Sjulian abortsleep(td2); 1828105911Sjulian } 1829105911Sjulian } else { 1830105911Sjulian if (TD_IS_SUSPENDED(td2)) 1831105874Sdavidxu continue; 1832111028Sjeff /* 1833111028Sjeff * maybe other inhibitted states too? 1834111028Sjeff * XXXKSE Is it totally safe to 1835111028Sjeff * suspend a non-interruptable thread? 1836111028Sjeff */ 1837108338Sjulian if (td2->td_inhibitors & 1838111028Sjeff (TDI_SLEEPING | TDI_SWAPPED)) 1839105911Sjulian thread_suspend_one(td2); 184099026Sjulian } 184199026Sjulian } 184299026Sjulian } 1843105911Sjulian /* 1844105911Sjulian * Maybe we suspended some threads.. was it enough? 1845105911Sjulian */ 1846113920Sjhb if ((p->p_numthreads - p->p_suspcount) == 1) 1847105911Sjulian break; 1848105911Sjulian 184999026Sjulian /* 185099026Sjulian * Wake us up when everyone else has suspended. 1851100648Sjulian * In the mean time we suspend as well. 185299026Sjulian */ 1853103216Sjulian thread_suspend_one(td); 1854113795Sdavidxu DROP_GIANT(); 185599026Sjulian PROC_UNLOCK(p); 1856107719Sjulian p->p_stats->p_ru.ru_nvcsw++; 185799026Sjulian mi_switch(); 185899026Sjulian mtx_unlock_spin(&sched_lock); 1859113795Sdavidxu PICKUP_GIANT(); 186099026Sjulian PROC_LOCK(p); 1861113920Sjhb mtx_lock_spin(&sched_lock); 186299026Sjulian } 1863111028Sjeff if (force_exit == SINGLE_EXIT) { 1864113920Sjhb if (td->td_upcall) 1865111028Sjeff upcall_remove(td); 1866105854Sjulian kse_purge(p, td); 1867111028Sjeff } 1868113920Sjhb mtx_unlock_spin(&sched_lock); 186999026Sjulian return (0); 187099026Sjulian} 187199026Sjulian 187299026Sjulian/* 187399026Sjulian * Called in from locations that can safely check to see 187499026Sjulian * whether we have to suspend or at least throttle for a 187599026Sjulian * single-thread event (e.g. fork). 187699026Sjulian * 187799026Sjulian * Such locations include userret(). 187899026Sjulian * If the "return_instead" argument is non zero, the thread must be able to 187999026Sjulian * accept 0 (caller may continue), or 1 (caller must abort) as a result. 188099026Sjulian * 188199026Sjulian * The 'return_instead' argument tells the function if it may do a 188299026Sjulian * thread_exit() or suspend, or whether the caller must abort and back 188399026Sjulian * out instead. 188499026Sjulian * 188599026Sjulian * If the thread that set the single_threading request has set the 188699026Sjulian * P_SINGLE_EXIT bit in the process flags then this call will never return 188799026Sjulian * if 'return_instead' is false, but will exit. 188899026Sjulian * 188999026Sjulian * P_SINGLE_EXIT | return_instead == 0| return_instead != 0 189099026Sjulian *---------------+--------------------+--------------------- 189199026Sjulian * 0 | returns 0 | returns 0 or 1 189299026Sjulian * | when ST ends | immediatly 189399026Sjulian *---------------+--------------------+--------------------- 189499026Sjulian * 1 | thread exits | returns 1 189599026Sjulian * | | immediatly 189699026Sjulian * 0 = thread_exit() or suspension ok, 189799026Sjulian * other = return error instead of stopping the thread. 189899026Sjulian * 189999026Sjulian * While a full suspension is under effect, even a single threading 190099026Sjulian * thread would be suspended if it made this call (but it shouldn't). 190199026Sjulian * This call should only be made from places where 190299026Sjulian * thread_exit() would be safe as that may be the outcome unless 190399026Sjulian * return_instead is set. 190499026Sjulian */ 190599026Sjulianint 190699026Sjulianthread_suspend_check(int return_instead) 190799026Sjulian{ 1908104502Sjmallett struct thread *td; 1909104502Sjmallett struct proc *p; 1910105854Sjulian struct ksegrp *kg; 191199026Sjulian 191299026Sjulian td = curthread; 191399026Sjulian p = td->td_proc; 1914105854Sjulian kg = td->td_ksegrp; 191599026Sjulian PROC_LOCK_ASSERT(p, MA_OWNED); 191699026Sjulian while (P_SHOULDSTOP(p)) { 1917102950Sdavidxu if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 191899026Sjulian KASSERT(p->p_singlethread != NULL, 191999026Sjulian ("singlethread not set")); 192099026Sjulian /* 1921100648Sjulian * The only suspension in action is a 1922100648Sjulian * single-threading. Single threader need not stop. 1923100646Sjulian * XXX Should be safe to access unlocked 1924100646Sjulian * as it can only be set to be true by us. 192599026Sjulian */ 1926100648Sjulian if (p->p_singlethread == td) 192799026Sjulian return (0); /* Exempt from stopping. */ 192899026Sjulian } 1929100648Sjulian if (return_instead) 193099026Sjulian return (1); 193199026Sjulian 1932112071Sdavidxu mtx_lock_spin(&sched_lock); 1933112071Sdavidxu thread_stopped(p); 193499026Sjulian /* 193599026Sjulian * If the process is waiting for us to exit, 193699026Sjulian * this thread should just suicide. 1937102950Sdavidxu * Assumes that P_SINGLE_EXIT implies P_STOPPED_SINGLE. 193899026Sjulian */ 193999026Sjulian if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) { 194099026Sjulian while (mtx_owned(&Giant)) 194199026Sjulian mtx_unlock(&Giant); 1942112910Sjeff if (p->p_flag & P_THREADED) 1943112910Sjeff thread_exit(); 1944112910Sjeff else 1945112910Sjeff thr_exit1(); 194699026Sjulian } 194799026Sjulian 1948112910Sjeff mtx_assert(&Giant, MA_NOTOWNED); 194999026Sjulian /* 195099026Sjulian * When a thread suspends, it just 195199026Sjulian * moves to the processes's suspend queue 195299026Sjulian * and stays there. 195399026Sjulian */ 1954103216Sjulian thread_suspend_one(td); 1955102950Sdavidxu if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 1956100632Sjulian if (p->p_numthreads == p->p_suspcount) { 1957103216Sjulian thread_unsuspend_one(p->p_singlethread); 1958100632Sjulian } 1959100632Sjulian } 1960113864Sjhb PROC_UNLOCK(p); 1961100594Sjulian p->p_stats->p_ru.ru_nivcsw++; 196299026Sjulian mi_switch(); 196399026Sjulian mtx_unlock_spin(&sched_lock); 196499026Sjulian PROC_LOCK(p); 196599026Sjulian } 196699026Sjulian return (0); 196799026Sjulian} 196899026Sjulian 1969102898Sdavidxuvoid 1970102898Sdavidxuthread_suspend_one(struct thread *td) 1971102898Sdavidxu{ 1972102898Sdavidxu struct proc *p = td->td_proc; 1973102898Sdavidxu 1974102898Sdavidxu mtx_assert(&sched_lock, MA_OWNED); 1975113920Sjhb PROC_LOCK_ASSERT(p, MA_OWNED); 1976112071Sdavidxu KASSERT(!TD_IS_SUSPENDED(td), ("already suspended")); 1977102898Sdavidxu p->p_suspcount++; 1978103216Sjulian TD_SET_SUSPENDED(td); 1979102898Sdavidxu TAILQ_INSERT_TAIL(&p->p_suspended, td, td_runq); 1980103216Sjulian /* 1981103216Sjulian * Hack: If we are suspending but are on the sleep queue 1982103216Sjulian * then we are in msleep or the cv equivalent. We 1983103216Sjulian * want to look like we have two Inhibitors. 1984105911Sjulian * May already be set.. doesn't matter. 1985103216Sjulian */ 1986103216Sjulian if (TD_ON_SLEEPQ(td)) 1987103216Sjulian TD_SET_SLEEPING(td); 1988102898Sdavidxu} 1989102898Sdavidxu 1990102898Sdavidxuvoid 1991102898Sdavidxuthread_unsuspend_one(struct thread *td) 1992102898Sdavidxu{ 1993102898Sdavidxu struct proc *p = td->td_proc; 1994102898Sdavidxu 1995102898Sdavidxu mtx_assert(&sched_lock, MA_OWNED); 1996113920Sjhb PROC_LOCK_ASSERT(p, MA_OWNED); 1997102898Sdavidxu TAILQ_REMOVE(&p->p_suspended, td, td_runq); 1998103216Sjulian TD_CLR_SUSPENDED(td); 1999102898Sdavidxu p->p_suspcount--; 2000103216Sjulian setrunnable(td); 2001102898Sdavidxu} 2002102898Sdavidxu 200399026Sjulian/* 200499026Sjulian * Allow all threads blocked by single threading to continue running. 200599026Sjulian */ 200699026Sjulianvoid 200799026Sjulianthread_unsuspend(struct proc *p) 200899026Sjulian{ 200999026Sjulian struct thread *td; 201099026Sjulian 2011100646Sjulian mtx_assert(&sched_lock, MA_OWNED); 201299026Sjulian PROC_LOCK_ASSERT(p, MA_OWNED); 201399026Sjulian if (!P_SHOULDSTOP(p)) { 201499026Sjulian while (( td = TAILQ_FIRST(&p->p_suspended))) { 2015102898Sdavidxu thread_unsuspend_one(td); 201699026Sjulian } 2017102950Sdavidxu } else if ((P_SHOULDSTOP(p) == P_STOPPED_SINGLE) && 201899026Sjulian (p->p_numthreads == p->p_suspcount)) { 201999026Sjulian /* 202099026Sjulian * Stopping everything also did the job for the single 202199026Sjulian * threading request. Now we've downgraded to single-threaded, 202299026Sjulian * let it continue. 202399026Sjulian */ 2024102898Sdavidxu thread_unsuspend_one(p->p_singlethread); 202599026Sjulian } 202699026Sjulian} 202799026Sjulian 202899026Sjulianvoid 202999026Sjulianthread_single_end(void) 203099026Sjulian{ 203199026Sjulian struct thread *td; 203299026Sjulian struct proc *p; 203399026Sjulian 203499026Sjulian td = curthread; 203599026Sjulian p = td->td_proc; 203699026Sjulian PROC_LOCK_ASSERT(p, MA_OWNED); 2037102950Sdavidxu p->p_flag &= ~P_STOPPED_SINGLE; 2038113920Sjhb mtx_lock_spin(&sched_lock); 203999026Sjulian p->p_singlethread = NULL; 2040102292Sjulian /* 2041102292Sjulian * If there are other threads they mey now run, 2042102292Sjulian * unless of course there is a blanket 'stop order' 2043102292Sjulian * on the process. The single threader must be allowed 2044102292Sjulian * to continue however as this is a bad place to stop. 2045102292Sjulian */ 2046102292Sjulian if ((p->p_numthreads != 1) && (!P_SHOULDSTOP(p))) { 2047102292Sjulian while (( td = TAILQ_FIRST(&p->p_suspended))) { 2048103216Sjulian thread_unsuspend_one(td); 2049102292Sjulian } 2050102292Sjulian } 2051113920Sjhb mtx_unlock_spin(&sched_lock); 205299026Sjulian} 205399026Sjulian 2054102292Sjulian 2055