kern_thread.c revision 111042
1/* 2 * Copyright (C) 2001 Julian Elischer <julian@freebsd.org>. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice(s), this list of conditions and the following disclaimer as 10 * the first lines of this file unmodified other than the possible 11 * addition of one or more copyright notices. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice(s), this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY 17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY 20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 26 * DAMAGE. 27 * 28 * $FreeBSD: head/sys/kern/kern_thread.c 111042 2003-02-17 14:54:57Z davidxu $ 29 */ 30 31#include <sys/param.h> 32#include <sys/systm.h> 33#include <sys/kernel.h> 34#include <sys/lock.h> 35#include <sys/malloc.h> 36#include <sys/mutex.h> 37#include <sys/proc.h> 38#include <sys/smp.h> 39#include <sys/sysctl.h> 40#include <sys/sysproto.h> 41#include <sys/filedesc.h> 42#include <sys/sched.h> 43#include <sys/signalvar.h> 44#include <sys/sx.h> 45#include <sys/tty.h> 46#include <sys/user.h> 47#include <sys/jail.h> 48#include <sys/kse.h> 49#include <sys/ktr.h> 50#include <sys/ucontext.h> 51 52#include <vm/vm.h> 53#include <vm/vm_object.h> 54#include <vm/pmap.h> 55#include <vm/uma.h> 56#include <vm/vm_map.h> 57 58#include <machine/frame.h> 59 60/* 61 * KSEGRP related storage. 62 */ 63static uma_zone_t ksegrp_zone; 64static uma_zone_t kse_zone; 65static uma_zone_t thread_zone; 66static uma_zone_t upcall_zone; 67 68/* DEBUG ONLY */ 69SYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW, 0, "thread allocation"); 70static int thread_debug = 0; 71SYSCTL_INT(_kern_threads, OID_AUTO, debug, CTLFLAG_RW, 72 &thread_debug, 0, "thread debug"); 73 74static int max_threads_per_proc = 30; 75SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_per_proc, CTLFLAG_RW, 76 &max_threads_per_proc, 0, "Limit on threads per proc"); 77 78static int max_groups_per_proc = 5; 79SYSCTL_INT(_kern_threads, OID_AUTO, max_groups_per_proc, CTLFLAG_RW, 80 &max_groups_per_proc, 0, "Limit on thread groups per proc"); 81 82static int virtual_cpu; 83 84#define RANGEOF(type, start, end) (offsetof(type, end) - offsetof(type, start)) 85 86TAILQ_HEAD(, thread) zombie_threads = TAILQ_HEAD_INITIALIZER(zombie_threads); 87TAILQ_HEAD(, kse) zombie_kses = TAILQ_HEAD_INITIALIZER(zombie_kses); 88TAILQ_HEAD(, ksegrp) zombie_ksegrps = TAILQ_HEAD_INITIALIZER(zombie_ksegrps); 89TAILQ_HEAD(, kse_upcall) zombie_upcalls = 90 TAILQ_HEAD_INITIALIZER(zombie_upcalls); 91struct mtx kse_zombie_lock; 92MTX_SYSINIT(kse_zombie_lock, &kse_zombie_lock, "kse zombie lock", MTX_SPIN); 93 94static void kse_purge(struct proc *p, struct thread *td); 95static void kse_purge_group(struct thread *td); 96static int thread_update_usr_ticks(struct thread *td); 97static int thread_update_sys_ticks(struct thread *td); 98static void thread_alloc_spare(struct thread *td, struct thread *spare); 99 100static int 101sysctl_kse_virtual_cpu(SYSCTL_HANDLER_ARGS) 102{ 103 int error, new_val; 104 int def_val; 105 106#ifdef SMP 107 def_val = mp_ncpus; 108#else 109 def_val = 1; 110#endif 111 if (virtual_cpu == 0) 112 new_val = def_val; 113 else 114 new_val = virtual_cpu; 115 error = sysctl_handle_int(oidp, &new_val, 0, req); 116 if (error != 0 || req->newptr == NULL) 117 return (error); 118 if (new_val < 0) 119 return (EINVAL); 120 virtual_cpu = new_val; 121 return (0); 122} 123 124/* DEBUG ONLY */ 125SYSCTL_PROC(_kern_threads, OID_AUTO, virtual_cpu, CTLTYPE_INT|CTLFLAG_RW, 126 0, sizeof(virtual_cpu), sysctl_kse_virtual_cpu, "I", 127 "debug virtual cpus"); 128 129/* 130 * Prepare a thread for use. 131 */ 132static void 133thread_ctor(void *mem, int size, void *arg) 134{ 135 struct thread *td; 136 137 td = (struct thread *)mem; 138 td->td_state = TDS_INACTIVE; 139} 140 141/* 142 * Reclaim a thread after use. 143 */ 144static void 145thread_dtor(void *mem, int size, void *arg) 146{ 147 struct thread *td; 148 149 td = (struct thread *)mem; 150 151#ifdef INVARIANTS 152 /* Verify that this thread is in a safe state to free. */ 153 switch (td->td_state) { 154 case TDS_INHIBITED: 155 case TDS_RUNNING: 156 case TDS_CAN_RUN: 157 case TDS_RUNQ: 158 /* 159 * We must never unlink a thread that is in one of 160 * these states, because it is currently active. 161 */ 162 panic("bad state for thread unlinking"); 163 /* NOTREACHED */ 164 case TDS_INACTIVE: 165 break; 166 default: 167 panic("bad thread state"); 168 /* NOTREACHED */ 169 } 170#endif 171} 172 173/* 174 * Initialize type-stable parts of a thread (when newly created). 175 */ 176static void 177thread_init(void *mem, int size) 178{ 179 struct thread *td; 180 181 td = (struct thread *)mem; 182 mtx_lock(&Giant); 183 pmap_new_thread(td, 0); 184 mtx_unlock(&Giant); 185 cpu_thread_setup(td); 186 td->td_sched = (struct td_sched *)&td[1]; 187} 188 189/* 190 * Tear down type-stable parts of a thread (just before being discarded). 191 */ 192static void 193thread_fini(void *mem, int size) 194{ 195 struct thread *td; 196 197 td = (struct thread *)mem; 198 pmap_dispose_thread(td); 199} 200 201/* 202 * Initialize type-stable parts of a kse (when newly created). 203 */ 204static void 205kse_init(void *mem, int size) 206{ 207 struct kse *ke; 208 209 ke = (struct kse *)mem; 210 ke->ke_sched = (struct ke_sched *)&ke[1]; 211} 212 213/* 214 * Initialize type-stable parts of a ksegrp (when newly created). 215 */ 216static void 217ksegrp_init(void *mem, int size) 218{ 219 struct ksegrp *kg; 220 221 kg = (struct ksegrp *)mem; 222 kg->kg_sched = (struct kg_sched *)&kg[1]; 223} 224 225/* 226 * KSE is linked into kse group. 227 */ 228void 229kse_link(struct kse *ke, struct ksegrp *kg) 230{ 231 struct proc *p = kg->kg_proc; 232 233 TAILQ_INSERT_HEAD(&kg->kg_kseq, ke, ke_kglist); 234 kg->kg_kses++; 235 ke->ke_state = KES_UNQUEUED; 236 ke->ke_proc = p; 237 ke->ke_ksegrp = kg; 238 ke->ke_thread = NULL; 239 ke->ke_oncpu = NOCPU; 240 ke->ke_flags = 0; 241} 242 243void 244kse_unlink(struct kse *ke) 245{ 246 struct ksegrp *kg; 247 248 mtx_assert(&sched_lock, MA_OWNED); 249 kg = ke->ke_ksegrp; 250 TAILQ_REMOVE(&kg->kg_kseq, ke, ke_kglist); 251 if (ke->ke_state == KES_IDLE) { 252 TAILQ_REMOVE(&kg->kg_iq, ke, ke_kgrlist); 253 kg->kg_idle_kses--; 254 } 255 if (--kg->kg_kses == 0) 256 ksegrp_unlink(kg); 257 /* 258 * Aggregate stats from the KSE 259 */ 260 kse_stash(ke); 261} 262 263void 264ksegrp_link(struct ksegrp *kg, struct proc *p) 265{ 266 267 TAILQ_INIT(&kg->kg_threads); 268 TAILQ_INIT(&kg->kg_runq); /* links with td_runq */ 269 TAILQ_INIT(&kg->kg_slpq); /* links with td_runq */ 270 TAILQ_INIT(&kg->kg_kseq); /* all kses in ksegrp */ 271 TAILQ_INIT(&kg->kg_iq); /* all idle kses in ksegrp */ 272 TAILQ_INIT(&kg->kg_upcalls); /* all upcall structure in ksegrp */ 273 kg->kg_proc = p; 274 /* 275 * the following counters are in the -zero- section 276 * and may not need clearing 277 */ 278 kg->kg_numthreads = 0; 279 kg->kg_runnable = 0; 280 kg->kg_kses = 0; 281 kg->kg_runq_kses = 0; /* XXXKSE change name */ 282 kg->kg_idle_kses = 0; 283 kg->kg_numupcalls = 0; 284 /* link it in now that it's consistent */ 285 p->p_numksegrps++; 286 TAILQ_INSERT_HEAD(&p->p_ksegrps, kg, kg_ksegrp); 287} 288 289void 290ksegrp_unlink(struct ksegrp *kg) 291{ 292 struct proc *p; 293 294 mtx_assert(&sched_lock, MA_OWNED); 295 KASSERT((kg->kg_numthreads == 0), ("ksegrp_unlink: residual threads")); 296 KASSERT((kg->kg_kses == 0), ("ksegrp_unlink: residual kses")); 297 KASSERT((kg->kg_numupcalls == 0), ("ksegrp_unlink: residual upcalls")); 298 299 p = kg->kg_proc; 300 TAILQ_REMOVE(&p->p_ksegrps, kg, kg_ksegrp); 301 p->p_numksegrps--; 302 /* 303 * Aggregate stats from the KSE 304 */ 305 ksegrp_stash(kg); 306} 307 308struct kse_upcall * 309upcall_alloc(void) 310{ 311 struct kse_upcall *ku; 312 313 ku = uma_zalloc(upcall_zone, 0); 314 bzero(ku, sizeof(*ku)); 315 return (ku); 316} 317 318void 319upcall_free(struct kse_upcall *ku) 320{ 321 322 uma_zfree(upcall_zone, ku); 323} 324 325void 326upcall_link(struct kse_upcall *ku, struct ksegrp *kg) 327{ 328 329 mtx_assert(&sched_lock, MA_OWNED); 330 TAILQ_INSERT_TAIL(&kg->kg_upcalls, ku, ku_link); 331 ku->ku_ksegrp = kg; 332 kg->kg_numupcalls++; 333} 334 335void 336upcall_unlink(struct kse_upcall *ku) 337{ 338 struct ksegrp *kg = ku->ku_ksegrp; 339 340 mtx_assert(&sched_lock, MA_OWNED); 341 KASSERT(ku->ku_owner == NULL, ("%s: have owner", __func__)); 342 TAILQ_REMOVE(&kg->kg_upcalls, ku, ku_link); 343 kg->kg_numupcalls--; 344 upcall_stash(ku); 345} 346 347void 348upcall_remove(struct thread *td) 349{ 350 351 if (td->td_upcall) { 352 td->td_upcall->ku_owner = NULL; 353 upcall_unlink(td->td_upcall); 354 td->td_upcall = 0; 355 } 356} 357 358/* 359 * For a newly created process, 360 * link up all the structures and its initial threads etc. 361 */ 362void 363proc_linkup(struct proc *p, struct ksegrp *kg, 364 struct kse *ke, struct thread *td) 365{ 366 367 TAILQ_INIT(&p->p_ksegrps); /* all ksegrps in proc */ 368 TAILQ_INIT(&p->p_threads); /* all threads in proc */ 369 TAILQ_INIT(&p->p_suspended); /* Threads suspended */ 370 p->p_numksegrps = 0; 371 p->p_numthreads = 0; 372 373 ksegrp_link(kg, p); 374 kse_link(ke, kg); 375 thread_link(td, kg); 376} 377 378/* 379struct kse_thr_interrupt_args { 380 struct kse_thr_mailbox * tmbx; 381}; 382*/ 383int 384kse_thr_interrupt(struct thread *td, struct kse_thr_interrupt_args *uap) 385{ 386 struct proc *p; 387 struct thread *td2; 388 389 p = td->td_proc; 390 if (!(p->p_flag & P_KSES) || (uap->tmbx == NULL)) 391 return (EINVAL); 392 mtx_lock_spin(&sched_lock); 393 FOREACH_THREAD_IN_PROC(p, td2) { 394 if (td2->td_mailbox == uap->tmbx) { 395 td2->td_flags |= TDF_INTERRUPT; 396 if (TD_ON_SLEEPQ(td2) && (td2->td_flags & TDF_SINTR)) { 397 if (td2->td_flags & TDF_CVWAITQ) 398 cv_abort(td2); 399 else 400 abortsleep(td2); 401 } 402 mtx_unlock_spin(&sched_lock); 403 return (0); 404 } 405 } 406 mtx_unlock_spin(&sched_lock); 407 return (ESRCH); 408} 409 410/* 411struct kse_exit_args { 412 register_t dummy; 413}; 414*/ 415int 416kse_exit(struct thread *td, struct kse_exit_args *uap) 417{ 418 struct proc *p; 419 struct ksegrp *kg; 420 struct kse *ke; 421 422 p = td->td_proc; 423 /* 424 * Only UTS can call the syscall and current group 425 * should be a threaded group. 426 */ 427 if ((td->td_mailbox != NULL) || (td->td_ksegrp->kg_numupcalls == 0)) 428 return (EINVAL); 429 KASSERT((td->td_upcall != NULL), ("%s: not own an upcall", __func__)); 430 431 kg = td->td_ksegrp; 432 /* Serialize removing upcall */ 433 PROC_LOCK(p); 434 mtx_lock_spin(&sched_lock); 435 if ((kg->kg_numupcalls == 1) && (kg->kg_numthreads > 1)) { 436 mtx_unlock_spin(&sched_lock); 437 PROC_UNLOCK(p); 438 return (EDEADLK); 439 } 440 ke = td->td_kse; 441 upcall_remove(td); 442 if (p->p_numthreads == 1) { 443 kse_purge(p, td); 444 p->p_flag &= ~P_KSES; 445 mtx_unlock_spin(&sched_lock); 446 PROC_UNLOCK(p); 447 } else { 448 if (kg->kg_numthreads == 1) { /* Shutdown a group */ 449 kse_purge_group(td); 450 ke->ke_flags |= KEF_EXIT; 451 } 452 thread_exit(); 453 /* NOTREACHED */ 454 } 455 return (0); 456} 457 458/* 459 * Either becomes an upcall or waits for an awakening event and 460 * then becomes an upcall. Only error cases return. 461 */ 462/* 463struct kse_release_args { 464 register_t dummy; 465}; 466*/ 467int 468kse_release(struct thread *td, struct kse_release_args *uap) 469{ 470 struct proc *p; 471 struct ksegrp *kg; 472 473 p = td->td_proc; 474 kg = td->td_ksegrp; 475 /* 476 * Only UTS can call the syscall and current group 477 * should be a threaded group. 478 */ 479 if ((td->td_mailbox != NULL) || (td->td_ksegrp->kg_numupcalls == 0)) 480 return (EINVAL); 481 KASSERT((td->td_upcall != NULL), ("%s: not own an upcall", __func__)); 482 483 PROC_LOCK(p); 484 mtx_lock_spin(&sched_lock); 485 /* Change OURSELF to become an upcall. */ 486 td->td_flags = TDF_UPCALLING; 487 if (p->p_sflag & PS_NEEDSIGCHK) 488 td->td_flags |= TDF_ASTPENDING; 489 if ((td->td_upcall->ku_flags & KUF_DOUPCALL) == 0 && 490 (kg->kg_completed == NULL)) { 491 kg->kg_upsleeps++; 492 mtx_unlock_spin(&sched_lock); 493 msleep(&kg->kg_completed, &p->p_mtx, PPAUSE|PCATCH, "ksepause", 494 NULL); 495 kg->kg_upsleeps--; 496 PROC_UNLOCK(p); 497 } else { 498 mtx_unlock_spin(&sched_lock); 499 PROC_UNLOCK(p); 500 } 501 return (0); 502} 503 504/* struct kse_wakeup_args { 505 struct kse_mailbox *mbx; 506}; */ 507int 508kse_wakeup(struct thread *td, struct kse_wakeup_args *uap) 509{ 510 struct proc *p; 511 struct ksegrp *kg; 512 struct kse_upcall *ku; 513 struct thread *td2; 514 515 p = td->td_proc; 516 td2 = NULL; 517 ku = NULL; 518 /* KSE-enabled processes only, please. */ 519 if (!(p->p_flag & P_KSES)) 520 return (EINVAL); 521 522 PROC_LOCK(p); 523 mtx_lock_spin(&sched_lock); 524 if (uap->mbx) { 525 FOREACH_KSEGRP_IN_PROC(p, kg) { 526 FOREACH_UPCALL_IN_GROUP(kg, ku) { 527 if (ku->ku_mailbox == uap->mbx) 528 break; 529 } 530 if (ku) 531 break; 532 } 533 } else { 534 kg = td->td_ksegrp; 535 if (kg->kg_upsleeps) { 536 wakeup_one(&kg->kg_completed); 537 mtx_unlock_spin(&sched_lock); 538 PROC_UNLOCK(p); 539 return (0); 540 } 541 ku = TAILQ_FIRST(&kg->kg_upcalls); 542 } 543 if (ku) { 544 if ((td2 = ku->ku_owner) == NULL) { 545 panic("%s: no owner", __func__); 546 } else if (TD_ON_SLEEPQ(td2) && 547 (td2->td_wchan == &kg->kg_completed)) { 548 abortsleep(td2); 549 } else { 550 ku->ku_flags |= KUF_DOUPCALL; 551 } 552 mtx_unlock_spin(&sched_lock); 553 PROC_UNLOCK(p); 554 return (0); 555 } 556 mtx_unlock_spin(&sched_lock); 557 PROC_UNLOCK(p); 558 return (ESRCH); 559} 560 561/* 562 * No new KSEG: first call: use current KSE, don't schedule an upcall 563 * All other situations, do allocate max new KSEs and schedule an upcall. 564 */ 565/* struct kse_create_args { 566 struct kse_mailbox *mbx; 567 int newgroup; 568}; */ 569int 570kse_create(struct thread *td, struct kse_create_args *uap) 571{ 572 struct kse *newke; 573 struct ksegrp *newkg; 574 struct ksegrp *kg; 575 struct proc *p; 576 struct kse_mailbox mbx; 577 struct kse_upcall *newku; 578 int err, ncpus; 579 580 p = td->td_proc; 581 if ((err = copyin(uap->mbx, &mbx, sizeof(mbx)))) 582 return (err); 583 584 /* Too bad, why hasn't kernel always a cpu counter !? */ 585#ifdef SMP 586 ncpus = mp_ncpus; 587#else 588 ncpus = 1; 589#endif 590 if (thread_debug && virtual_cpu != 0) 591 ncpus = virtual_cpu; 592 593 /* Easier to just set it than to test and set */ 594 p->p_flag |= P_KSES; 595 kg = td->td_ksegrp; 596 if (uap->newgroup) { 597 /* Have race condition but it is cheap */ 598 if (p->p_numksegrps >= max_groups_per_proc) 599 return (EPROCLIM); 600 /* 601 * If we want a new KSEGRP it doesn't matter whether 602 * we have already fired up KSE mode before or not. 603 * We put the process in KSE mode and create a new KSEGRP. 604 */ 605 newkg = ksegrp_alloc(); 606 bzero(&newkg->kg_startzero, RANGEOF(struct ksegrp, 607 kg_startzero, kg_endzero)); 608 bcopy(&kg->kg_startcopy, &newkg->kg_startcopy, 609 RANGEOF(struct ksegrp, kg_startcopy, kg_endcopy)); 610 mtx_lock_spin(&sched_lock); 611 ksegrp_link(newkg, p); 612 if (p->p_numksegrps >= max_groups_per_proc) { 613 ksegrp_unlink(newkg); 614 mtx_unlock_spin(&sched_lock); 615 return (EPROCLIM); 616 } 617 mtx_unlock_spin(&sched_lock); 618 } else { 619 newkg = kg; 620 } 621 622 /* 623 * Creating upcalls more than number of physical cpu does 624 * not help performance. 625 */ 626 if (newkg->kg_numupcalls >= ncpus) 627 return (EPROCLIM); 628 629 if (newkg->kg_numupcalls == 0) { 630 /* 631 * Initialize KSE group, optimized for MP. 632 * Create KSEs as many as physical cpus, this increases 633 * concurrent even if userland is not MP safe and can only run 634 * on single CPU (for early version of libpthread, it is true). 635 * In ideal world, every physical cpu should execute a thread. 636 * If there is enough KSEs, threads in kernel can be 637 * executed parallel on different cpus with full speed, 638 * Concurrent in kernel shouldn't be restricted by number of 639 * upcalls userland provides. 640 * Adding more upcall structures only increases concurrent 641 * in userland. 642 * Highest performance configuration is: 643 * N kses = N upcalls = N phyiscal cpus 644 */ 645 while (newkg->kg_kses < ncpus) { 646 newke = kse_alloc(); 647 bzero(&newke->ke_startzero, RANGEOF(struct kse, 648 ke_startzero, ke_endzero)); 649#if 0 650 mtx_lock_spin(&sched_lock); 651 bcopy(&ke->ke_startcopy, &newke->ke_startcopy, 652 RANGEOF(struct kse, ke_startcopy, ke_endcopy)); 653 mtx_unlock_spin(&sched_lock); 654#endif 655 mtx_lock_spin(&sched_lock); 656 kse_link(newke, newkg); 657 /* Add engine */ 658 kse_reassign(newke); 659 mtx_unlock_spin(&sched_lock); 660 } 661 } 662 newku = upcall_alloc(); 663 newku->ku_mailbox = uap->mbx; 664 newku->ku_func = mbx.km_func; 665 bcopy(&mbx.km_stack, &newku->ku_stack, sizeof(stack_t)); 666 667 /* For the first call this may not have been set */ 668 if (td->td_standin == NULL) 669 thread_alloc_spare(td, NULL); 670 671 mtx_lock_spin(&sched_lock); 672 if (newkg->kg_numupcalls >= ncpus) { 673 upcall_free(newku); 674 mtx_unlock_spin(&sched_lock); 675 return (EPROCLIM); 676 } 677 upcall_link(newku, newkg); 678 679 /* 680 * Each upcall structure has an owner thread, find which 681 * one owns it. 682 */ 683 if (uap->newgroup) { 684 /* 685 * Because new ksegrp hasn't thread, 686 * create an initial upcall thread to own it. 687 */ 688 thread_schedule_upcall(td, newku); 689 } else { 690 /* 691 * If current thread hasn't an upcall structure, 692 * just assign the upcall to it. 693 */ 694 if (td->td_upcall == NULL) { 695 newku->ku_owner = td; 696 td->td_upcall = newku; 697 } else { 698 /* 699 * Create a new upcall thread to own it. 700 */ 701 thread_schedule_upcall(td, newku); 702 } 703 } 704 mtx_unlock_spin(&sched_lock); 705 return (0); 706} 707 708/* 709 * Fill a ucontext_t with a thread's context information. 710 * 711 * This is an analogue to getcontext(3). 712 */ 713void 714thread_getcontext(struct thread *td, ucontext_t *uc) 715{ 716 717/* 718 * XXX this is declared in a MD include file, i386/include/ucontext.h but 719 * is used in MI code. 720 */ 721#ifdef __i386__ 722 get_mcontext(td, &uc->uc_mcontext); 723#endif 724 uc->uc_sigmask = td->td_proc->p_sigmask; 725} 726 727/* 728 * Set a thread's context from a ucontext_t. 729 * 730 * This is an analogue to setcontext(3). 731 */ 732int 733thread_setcontext(struct thread *td, ucontext_t *uc) 734{ 735 int ret; 736 737/* 738 * XXX this is declared in a MD include file, i386/include/ucontext.h but 739 * is used in MI code. 740 */ 741#ifdef __i386__ 742 ret = set_mcontext(td, &uc->uc_mcontext); 743#else 744 ret = ENOSYS; 745#endif 746 if (ret == 0) { 747 SIG_CANTMASK(uc->uc_sigmask); 748 PROC_LOCK(td->td_proc); 749 td->td_proc->p_sigmask = uc->uc_sigmask; 750 PROC_UNLOCK(td->td_proc); 751 } 752 return (ret); 753} 754 755/* 756 * Initialize global thread allocation resources. 757 */ 758void 759threadinit(void) 760{ 761 762#ifndef __ia64__ 763 thread_zone = uma_zcreate("THREAD", sched_sizeof_thread(), 764 thread_ctor, thread_dtor, thread_init, thread_fini, 765 UMA_ALIGN_CACHE, 0); 766#else 767 /* 768 * XXX the ia64 kstack allocator is really lame and is at the mercy 769 * of contigmallloc(). This hackery is to pre-construct a whole 770 * pile of thread structures with associated kernel stacks early 771 * in the system startup while contigmalloc() still works. Once we 772 * have them, keep them. Sigh. 773 */ 774 thread_zone = uma_zcreate("THREAD", sched_sizeof_thread(), 775 thread_ctor, thread_dtor, thread_init, thread_fini, 776 UMA_ALIGN_CACHE, UMA_ZONE_NOFREE); 777 uma_prealloc(thread_zone, 512); /* XXX arbitary */ 778#endif 779 ksegrp_zone = uma_zcreate("KSEGRP", sched_sizeof_ksegrp(), 780 NULL, NULL, ksegrp_init, NULL, 781 UMA_ALIGN_CACHE, 0); 782 kse_zone = uma_zcreate("KSE", sched_sizeof_kse(), 783 NULL, NULL, kse_init, NULL, 784 UMA_ALIGN_CACHE, 0); 785 upcall_zone = uma_zcreate("UPCALL", sizeof(struct kse_upcall), 786 NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0); 787} 788 789/* 790 * Stash an embarasingly extra thread into the zombie thread queue. 791 */ 792void 793thread_stash(struct thread *td) 794{ 795 mtx_lock_spin(&kse_zombie_lock); 796 TAILQ_INSERT_HEAD(&zombie_threads, td, td_runq); 797 mtx_unlock_spin(&kse_zombie_lock); 798} 799 800/* 801 * Stash an embarasingly extra kse into the zombie kse queue. 802 */ 803void 804kse_stash(struct kse *ke) 805{ 806 mtx_lock_spin(&kse_zombie_lock); 807 TAILQ_INSERT_HEAD(&zombie_kses, ke, ke_procq); 808 mtx_unlock_spin(&kse_zombie_lock); 809} 810 811/* 812 * Stash an embarasingly extra upcall into the zombie upcall queue. 813 */ 814 815void 816upcall_stash(struct kse_upcall *ku) 817{ 818 mtx_lock_spin(&kse_zombie_lock); 819 TAILQ_INSERT_HEAD(&zombie_upcalls, ku, ku_link); 820 mtx_unlock_spin(&kse_zombie_lock); 821} 822 823/* 824 * Stash an embarasingly extra ksegrp into the zombie ksegrp queue. 825 */ 826void 827ksegrp_stash(struct ksegrp *kg) 828{ 829 mtx_lock_spin(&kse_zombie_lock); 830 TAILQ_INSERT_HEAD(&zombie_ksegrps, kg, kg_ksegrp); 831 mtx_unlock_spin(&kse_zombie_lock); 832} 833 834/* 835 * Reap zombie kse resource. 836 */ 837void 838thread_reap(void) 839{ 840 struct thread *td_first, *td_next; 841 struct kse *ke_first, *ke_next; 842 struct ksegrp *kg_first, * kg_next; 843 struct kse_upcall *ku_first, *ku_next; 844 845 /* 846 * Don't even bother to lock if none at this instant, 847 * we really don't care about the next instant.. 848 */ 849 if ((!TAILQ_EMPTY(&zombie_threads)) 850 || (!TAILQ_EMPTY(&zombie_kses)) 851 || (!TAILQ_EMPTY(&zombie_ksegrps)) 852 || (!TAILQ_EMPTY(&zombie_upcalls))) { 853 mtx_lock_spin(&kse_zombie_lock); 854 td_first = TAILQ_FIRST(&zombie_threads); 855 ke_first = TAILQ_FIRST(&zombie_kses); 856 kg_first = TAILQ_FIRST(&zombie_ksegrps); 857 ku_first = TAILQ_FIRST(&zombie_upcalls); 858 if (td_first) 859 TAILQ_INIT(&zombie_threads); 860 if (ke_first) 861 TAILQ_INIT(&zombie_kses); 862 if (kg_first) 863 TAILQ_INIT(&zombie_ksegrps); 864 if (ku_first) 865 TAILQ_INIT(&zombie_upcalls); 866 mtx_unlock_spin(&kse_zombie_lock); 867 while (td_first) { 868 td_next = TAILQ_NEXT(td_first, td_runq); 869 if (td_first->td_ucred) 870 crfree(td_first->td_ucred); 871 thread_free(td_first); 872 td_first = td_next; 873 } 874 while (ke_first) { 875 ke_next = TAILQ_NEXT(ke_first, ke_procq); 876 kse_free(ke_first); 877 ke_first = ke_next; 878 } 879 while (kg_first) { 880 kg_next = TAILQ_NEXT(kg_first, kg_ksegrp); 881 ksegrp_free(kg_first); 882 kg_first = kg_next; 883 } 884 while (ku_first) { 885 ku_next = TAILQ_NEXT(ku_first, ku_link); 886 upcall_free(ku_first); 887 ku_first = ku_next; 888 } 889 } 890} 891 892/* 893 * Allocate a ksegrp. 894 */ 895struct ksegrp * 896ksegrp_alloc(void) 897{ 898 return (uma_zalloc(ksegrp_zone, 0)); 899} 900 901/* 902 * Allocate a kse. 903 */ 904struct kse * 905kse_alloc(void) 906{ 907 return (uma_zalloc(kse_zone, 0)); 908} 909 910/* 911 * Allocate a thread. 912 */ 913struct thread * 914thread_alloc(void) 915{ 916 thread_reap(); /* check if any zombies to get */ 917 return (uma_zalloc(thread_zone, 0)); 918} 919 920/* 921 * Deallocate a ksegrp. 922 */ 923void 924ksegrp_free(struct ksegrp *td) 925{ 926 uma_zfree(ksegrp_zone, td); 927} 928 929/* 930 * Deallocate a kse. 931 */ 932void 933kse_free(struct kse *td) 934{ 935 uma_zfree(kse_zone, td); 936} 937 938/* 939 * Deallocate a thread. 940 */ 941void 942thread_free(struct thread *td) 943{ 944 945 cpu_thread_clean(td); 946 uma_zfree(thread_zone, td); 947} 948 949/* 950 * Store the thread context in the UTS's mailbox. 951 * then add the mailbox at the head of a list we are building in user space. 952 * The list is anchored in the ksegrp structure. 953 */ 954int 955thread_export_context(struct thread *td) 956{ 957 struct proc *p; 958 struct ksegrp *kg; 959 uintptr_t mbx; 960 void *addr; 961 int error,temp; 962 ucontext_t uc; 963 964 p = td->td_proc; 965 kg = td->td_ksegrp; 966 967 /* Export the user/machine context. */ 968 addr = (void *)(&td->td_mailbox->tm_context); 969 error = copyin(addr, &uc, sizeof(ucontext_t)); 970 if (error) 971 goto bad; 972 973 thread_getcontext(td, &uc); 974 error = copyout(&uc, addr, sizeof(ucontext_t)); 975 if (error) 976 goto bad; 977 978 /* Exports clock ticks in kernel mode */ 979 addr = (caddr_t)(&td->td_mailbox->tm_sticks); 980 temp = fuword(addr) + td->td_usticks; 981 if (suword(addr, temp)) 982 goto bad; 983 984 /* Get address in latest mbox of list pointer */ 985 addr = (void *)(&td->td_mailbox->tm_next); 986 /* 987 * Put the saved address of the previous first 988 * entry into this one 989 */ 990 for (;;) { 991 mbx = (uintptr_t)kg->kg_completed; 992 if (suword(addr, mbx)) { 993 error = EFAULT; 994 goto bad; 995 } 996 PROC_LOCK(p); 997 if (mbx == (uintptr_t)kg->kg_completed) { 998 kg->kg_completed = td->td_mailbox; 999 /* 1000 * The thread context may be taken away by 1001 * other upcall threads when we unlock 1002 * process lock. it's no longer valid to 1003 * use it again in any other places. 1004 */ 1005 td->td_mailbox = NULL; 1006 PROC_UNLOCK(p); 1007 break; 1008 } 1009 PROC_UNLOCK(p); 1010 } 1011 td->td_usticks = 0; 1012 return (0); 1013 1014bad: 1015 PROC_LOCK(p); 1016 psignal(p, SIGSEGV); 1017 PROC_UNLOCK(p); 1018 /* The mailbox is bad, don't use it */ 1019 td->td_mailbox = NULL; 1020 td->td_usticks = 0; 1021 return (error); 1022} 1023 1024/* 1025 * Take the list of completed mailboxes for this KSEGRP and put them on this 1026 * upcall's mailbox as it's the next one going up. 1027 */ 1028static int 1029thread_link_mboxes(struct ksegrp *kg, struct kse_upcall *ku) 1030{ 1031 struct proc *p = kg->kg_proc; 1032 void *addr; 1033 uintptr_t mbx; 1034 1035 addr = (void *)(&ku->ku_mailbox->km_completed); 1036 for (;;) { 1037 mbx = (uintptr_t)kg->kg_completed; 1038 if (suword(addr, mbx)) { 1039 PROC_LOCK(p); 1040 psignal(p, SIGSEGV); 1041 PROC_UNLOCK(p); 1042 return (EFAULT); 1043 } 1044 /* XXXKSE could use atomic CMPXCH here */ 1045 PROC_LOCK(p); 1046 if (mbx == (uintptr_t)kg->kg_completed) { 1047 kg->kg_completed = NULL; 1048 PROC_UNLOCK(p); 1049 break; 1050 } 1051 PROC_UNLOCK(p); 1052 } 1053 return (0); 1054} 1055 1056/* 1057 * This function should be called at statclock interrupt time 1058 */ 1059int 1060thread_statclock(int user) 1061{ 1062 struct thread *td = curthread; 1063 1064 if (td->td_ksegrp->kg_numupcalls == 0) 1065 return (-1); 1066 if (user) { 1067 /* Current always do via ast() */ 1068 td->td_flags |= (TDF_USTATCLOCK|TDF_ASTPENDING); 1069 td->td_uuticks++; 1070 } else { 1071 if (td->td_mailbox != NULL) 1072 td->td_usticks++; 1073 else { 1074 /* XXXKSE 1075 * We will call thread_user_enter() for every 1076 * kernel entry in future, so if the thread mailbox 1077 * is NULL, it must be a UTS kernel, don't account 1078 * clock ticks for it. 1079 */ 1080 } 1081 } 1082 return (0); 1083} 1084 1085/* 1086 * Export user mode state clock ticks 1087 */ 1088static int 1089thread_update_usr_ticks(struct thread *td) 1090{ 1091 struct proc *p = td->td_proc; 1092 struct kse_thr_mailbox *tmbx; 1093 struct kse_upcall *ku; 1094 caddr_t addr; 1095 uint uticks; 1096 1097 if ((ku = td->td_upcall) == NULL) 1098 return (-1); 1099 1100 tmbx = (void *)fuword((void *)&ku->ku_mailbox->km_curthread); 1101 if ((tmbx == NULL) || (tmbx == (void *)-1)) 1102 return (-1); 1103 uticks = td->td_uuticks; 1104 td->td_uuticks = 0; 1105 if (uticks) { 1106 addr = (caddr_t)&tmbx->tm_uticks; 1107 uticks += fuword(addr); 1108 if (suword(addr, uticks)) { 1109 PROC_LOCK(p); 1110 psignal(p, SIGSEGV); 1111 PROC_UNLOCK(p); 1112 return (-2); 1113 } 1114 } 1115 return (0); 1116} 1117 1118/* 1119 * Export kernel mode state clock ticks 1120 */ 1121 1122static int 1123thread_update_sys_ticks(struct thread *td) 1124{ 1125 struct proc *p = td->td_proc; 1126 caddr_t addr; 1127 int sticks; 1128 1129 if (td->td_mailbox == NULL) 1130 return (-1); 1131 if (td->td_usticks == 0) 1132 return (0); 1133 addr = (caddr_t)&td->td_mailbox->tm_sticks; 1134 sticks = fuword(addr); 1135 /* XXXKSE use XCHG instead */ 1136 sticks += td->td_usticks; 1137 td->td_usticks = 0; 1138 if (suword(addr, sticks)) { 1139 PROC_LOCK(p); 1140 psignal(p, SIGSEGV); 1141 PROC_UNLOCK(p); 1142 return (-2); 1143 } 1144 return (0); 1145} 1146 1147/* 1148 * Discard the current thread and exit from its context. 1149 * 1150 * Because we can't free a thread while we're operating under its context, 1151 * push the current thread into our CPU's deadthread holder. This means 1152 * we needn't worry about someone else grabbing our context before we 1153 * do a cpu_throw(). 1154 */ 1155void 1156thread_exit(void) 1157{ 1158 struct thread *td; 1159 struct kse *ke; 1160 struct proc *p; 1161 struct ksegrp *kg; 1162 1163 td = curthread; 1164 kg = td->td_ksegrp; 1165 p = td->td_proc; 1166 ke = td->td_kse; 1167 1168 mtx_assert(&sched_lock, MA_OWNED); 1169 KASSERT(p != NULL, ("thread exiting without a process")); 1170 KASSERT(ke != NULL, ("thread exiting without a kse")); 1171 KASSERT(kg != NULL, ("thread exiting without a kse group")); 1172 PROC_LOCK_ASSERT(p, MA_OWNED); 1173 CTR1(KTR_PROC, "thread_exit: thread %p", td); 1174 KASSERT(!mtx_owned(&Giant), ("dying thread owns giant")); 1175 1176 if (td->td_standin != NULL) { 1177 thread_stash(td->td_standin); 1178 td->td_standin = NULL; 1179 } 1180 1181 cpu_thread_exit(td); /* XXXSMP */ 1182 1183 /* 1184 * The last thread is left attached to the process 1185 * So that the whole bundle gets recycled. Skip 1186 * all this stuff. 1187 */ 1188 if (p->p_numthreads > 1) { 1189 /* 1190 * Unlink this thread from its proc and the kseg. 1191 * In keeping with the other structs we probably should 1192 * have a thread_unlink() that does some of this but it 1193 * would only be called from here (I think) so it would 1194 * be a waste. (might be useful for proc_fini() as well.) 1195 */ 1196 TAILQ_REMOVE(&p->p_threads, td, td_plist); 1197 p->p_numthreads--; 1198 TAILQ_REMOVE(&kg->kg_threads, td, td_kglist); 1199 kg->kg_numthreads--; 1200 1201 /* 1202 * The test below is NOT true if we are the 1203 * sole exiting thread. P_STOPPED_SNGL is unset 1204 * in exit1() after it is the only survivor. 1205 */ 1206 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 1207 if (p->p_numthreads == p->p_suspcount) { 1208 thread_unsuspend_one(p->p_singlethread); 1209 } 1210 } 1211 1212 /* 1213 * Because each upcall structure has an owner thread, 1214 * owner thread exits only when process is in exiting 1215 * state, so upcall to userland is no longer needed, 1216 * deleting upcall structure is safe here. 1217 * So when all threads in a group is exited, all upcalls 1218 * in the group should be automatically freed. 1219 */ 1220 if (td->td_upcall) 1221 upcall_remove(td); 1222 1223 ke->ke_state = KES_UNQUEUED; 1224 ke->ke_thread = NULL; 1225 /* 1226 * Decide what to do with the KSE attached to this thread. 1227 */ 1228 if (ke->ke_flags & KEF_EXIT) 1229 kse_unlink(ke); 1230 else 1231 kse_reassign(ke); 1232 PROC_UNLOCK(p); 1233 td->td_kse = NULL; 1234 td->td_state = TDS_INACTIVE; 1235 td->td_proc = NULL; 1236 td->td_ksegrp = NULL; 1237 td->td_last_kse = NULL; 1238 PCPU_SET(deadthread, td); 1239 } else { 1240 PROC_UNLOCK(p); 1241 } 1242 cpu_throw(); 1243 /* NOTREACHED */ 1244} 1245 1246/* 1247 * Do any thread specific cleanups that may be needed in wait() 1248 * called with Giant held, proc and schedlock not held. 1249 */ 1250void 1251thread_wait(struct proc *p) 1252{ 1253 struct thread *td; 1254 1255 KASSERT((p->p_numthreads == 1), ("Muliple threads in wait1()")); 1256 KASSERT((p->p_numksegrps == 1), ("Muliple ksegrps in wait1()")); 1257 FOREACH_THREAD_IN_PROC(p, td) { 1258 if (td->td_standin != NULL) { 1259 thread_free(td->td_standin); 1260 td->td_standin = NULL; 1261 } 1262 cpu_thread_clean(td); 1263 } 1264 thread_reap(); /* check for zombie threads etc. */ 1265} 1266 1267/* 1268 * Link a thread to a process. 1269 * set up anything that needs to be initialized for it to 1270 * be used by the process. 1271 * 1272 * Note that we do not link to the proc's ucred here. 1273 * The thread is linked as if running but no KSE assigned. 1274 */ 1275void 1276thread_link(struct thread *td, struct ksegrp *kg) 1277{ 1278 struct proc *p; 1279 1280 p = kg->kg_proc; 1281 td->td_state = TDS_INACTIVE; 1282 td->td_proc = p; 1283 td->td_ksegrp = kg; 1284 td->td_last_kse = NULL; 1285 td->td_flags = 0; 1286 td->td_kse = NULL; 1287 1288 LIST_INIT(&td->td_contested); 1289 callout_init(&td->td_slpcallout, 1); 1290 TAILQ_INSERT_HEAD(&p->p_threads, td, td_plist); 1291 TAILQ_INSERT_HEAD(&kg->kg_threads, td, td_kglist); 1292 p->p_numthreads++; 1293 kg->kg_numthreads++; 1294} 1295 1296/* 1297 * Purge a ksegrp resource. When a ksegrp is preparing to 1298 * exit, it calls this function. 1299 */ 1300void 1301kse_purge_group(struct thread *td) 1302{ 1303 struct ksegrp *kg; 1304 struct kse *ke; 1305 1306 kg = td->td_ksegrp; 1307 KASSERT(kg->kg_numthreads == 1, ("%s: bad thread number", __func__)); 1308 while ((ke = TAILQ_FIRST(&kg->kg_iq)) != NULL) { 1309 KASSERT(ke->ke_state == KES_IDLE, 1310 ("%s: wrong idle KSE state", __func__)); 1311 kse_unlink(ke); 1312 } 1313 KASSERT((kg->kg_kses == 1), 1314 ("%s: ksegrp still has %d KSEs", __func__, kg->kg_kses)); 1315 KASSERT((kg->kg_numupcalls == 0), 1316 ("%s: ksegrp still has %d upcall datas", 1317 __func__, kg->kg_numupcalls)); 1318} 1319 1320/* 1321 * Purge a process's KSE resource. When a process is preparing to 1322 * exit, it calls kse_purge to release any extra KSE resources in 1323 * the process. 1324 */ 1325void 1326kse_purge(struct proc *p, struct thread *td) 1327{ 1328 struct ksegrp *kg; 1329 struct kse *ke; 1330 1331 KASSERT(p->p_numthreads == 1, ("bad thread number")); 1332 mtx_lock_spin(&sched_lock); 1333 while ((kg = TAILQ_FIRST(&p->p_ksegrps)) != NULL) { 1334 TAILQ_REMOVE(&p->p_ksegrps, kg, kg_ksegrp); 1335 p->p_numksegrps--; 1336 /* 1337 * There is no ownership for KSE, after all threads 1338 * in the group exited, it is possible that some KSEs 1339 * were left in idle queue, gc them now. 1340 */ 1341 while ((ke = TAILQ_FIRST(&kg->kg_iq)) != NULL) { 1342 KASSERT(ke->ke_state == KES_IDLE, 1343 ("%s: wrong idle KSE state", __func__)); 1344 TAILQ_REMOVE(&kg->kg_iq, ke, ke_kgrlist); 1345 kg->kg_idle_kses--; 1346 TAILQ_REMOVE(&kg->kg_kseq, ke, ke_kglist); 1347 kg->kg_kses--; 1348 kse_stash(ke); 1349 } 1350 KASSERT(((kg->kg_kses == 0) && (kg != td->td_ksegrp)) || 1351 ((kg->kg_kses == 1) && (kg == td->td_ksegrp)), 1352 ("ksegrp has wrong kg_kses: %d", kg->kg_kses)); 1353 KASSERT((kg->kg_numupcalls == 0), 1354 ("%s: ksegrp still has %d upcall datas", 1355 __func__, kg->kg_numupcalls)); 1356 1357 if (kg != td->td_ksegrp) 1358 ksegrp_stash(kg); 1359 } 1360 TAILQ_INSERT_HEAD(&p->p_ksegrps, td->td_ksegrp, kg_ksegrp); 1361 p->p_numksegrps++; 1362 mtx_unlock_spin(&sched_lock); 1363} 1364 1365/* 1366 * This function is intended to be used to initialize a spare thread 1367 * for upcall. Initialize thread's large data area outside sched_lock 1368 * for thread_schedule_upcall(). 1369 */ 1370void 1371thread_alloc_spare(struct thread *td, struct thread *spare) 1372{ 1373 if (td->td_standin) 1374 return; 1375 if (spare == NULL) 1376 spare = thread_alloc(); 1377 td->td_standin = spare; 1378 bzero(&spare->td_startzero, 1379 (unsigned)RANGEOF(struct thread, td_startzero, td_endzero)); 1380 spare->td_proc = td->td_proc; 1381 /* Setup PCB and fork address */ 1382 cpu_set_upcall(spare, td->td_pcb); 1383 /* 1384 * XXXKSE do we really need this? (default values for the 1385 * frame). 1386 */ 1387 bcopy(td->td_frame, spare->td_frame, sizeof(struct trapframe)); 1388 spare->td_ucred = crhold(td->td_ucred); 1389} 1390 1391/* 1392 * Create a thread and schedule it for upcall on the KSE given. 1393 * Use our thread's standin so that we don't have to allocate one. 1394 */ 1395struct thread * 1396thread_schedule_upcall(struct thread *td, struct kse_upcall *ku) 1397{ 1398 struct thread *td2; 1399 1400 mtx_assert(&sched_lock, MA_OWNED); 1401 1402 /* 1403 * Schedule an upcall thread on specified kse_upcall, 1404 * the kse_upcall must be free. 1405 * td must have a spare thread. 1406 */ 1407 KASSERT(ku->ku_owner == NULL, ("%s: upcall has owner", __func__)); 1408 if ((td2 = td->td_standin) != NULL) { 1409 td->td_standin = NULL; 1410 } else { 1411 panic("no reserve thread when scheduling an upcall"); 1412 return (NULL); 1413 } 1414 CTR3(KTR_PROC, "thread_schedule_upcall: thread %p (pid %d, %s)", 1415 td2, td->td_proc->p_pid, td->td_proc->p_comm); 1416 bcopy(&td->td_startcopy, &td2->td_startcopy, 1417 (unsigned) RANGEOF(struct thread, td_startcopy, td_endcopy)); 1418 thread_link(td2, ku->ku_ksegrp); 1419 /* Let the new thread become owner of the upcall */ 1420 ku->ku_owner = td2; 1421 td2->td_upcall = ku; 1422 td2->td_flags = TDF_UPCALLING; 1423 if (td->td_proc->p_sflag & PS_NEEDSIGCHK) 1424 td2->td_flags |= TDF_ASTPENDING; 1425 td2->td_kse = NULL; 1426 td2->td_state = TDS_CAN_RUN; 1427 td2->td_inhibitors = 0; 1428 setrunqueue(td2); 1429 return (td2); /* bogus.. should be a void function */ 1430} 1431 1432void 1433thread_signal_add(struct thread *td, int sig) 1434{ 1435 struct kse_upcall *ku; 1436 struct proc *p; 1437 sigset_t ss; 1438 int error; 1439 1440 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); 1441 td = curthread; 1442 ku = td->td_upcall; 1443 p = td->td_proc; 1444 1445 PROC_UNLOCK(p); 1446 error = copyin(&ku->ku_mailbox->km_sigscaught, &ss, sizeof(sigset_t)); 1447 if (error) 1448 goto error; 1449 1450 SIGADDSET(ss, sig); 1451 1452 error = copyout(&ss, &ku->ku_mailbox->km_sigscaught, sizeof(sigset_t)); 1453 if (error) 1454 goto error; 1455 1456 PROC_LOCK(p); 1457 return; 1458error: 1459 PROC_LOCK(p); 1460 sigexit(td, SIGILL); 1461} 1462 1463 1464/* 1465 * Schedule an upcall to notify a KSE process recieved signals. 1466 * 1467 */ 1468void 1469thread_signal_upcall(struct thread *td) 1470{ 1471 mtx_lock_spin(&sched_lock); 1472 td->td_flags |= TDF_UPCALLING; 1473 mtx_unlock_spin(&sched_lock); 1474 1475 return; 1476} 1477 1478/* 1479 * Setup done on the thread when it enters the kernel. 1480 * XXXKSE Presently only for syscalls but eventually all kernel entries. 1481 */ 1482void 1483thread_user_enter(struct proc *p, struct thread *td) 1484{ 1485 struct ksegrp *kg; 1486 struct kse_upcall *ku; 1487 1488 kg = td->td_ksegrp; 1489 /* 1490 * First check that we shouldn't just abort. 1491 * But check if we are the single thread first! 1492 * XXX p_singlethread not locked, but should be safe. 1493 */ 1494 if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) { 1495 PROC_LOCK(p); 1496 mtx_lock_spin(&sched_lock); 1497 thread_exit(); 1498 /* NOTREACHED */ 1499 } 1500 1501 /* 1502 * If we are doing a syscall in a KSE environment, 1503 * note where our mailbox is. There is always the 1504 * possibility that we could do this lazily (in kse_reassign()), 1505 * but for now do it every time. 1506 */ 1507 kg = td->td_ksegrp; 1508 if (kg->kg_numupcalls) { 1509 ku = td->td_upcall; 1510 KASSERT(ku, ("%s: no upcall owned", __func__)); 1511 KASSERT((ku->ku_owner == td), ("%s: wrong owner", __func__)); 1512 td->td_mailbox = 1513 (void *)fuword((void *)&ku->ku_mailbox->km_curthread); 1514 if ((td->td_mailbox == NULL) || 1515 (td->td_mailbox == (void *)-1)) { 1516 /* Don't schedule upcall when blocked */ 1517 td->td_mailbox = NULL; 1518 mtx_lock_spin(&sched_lock); 1519 td->td_flags &= ~TDF_CAN_UNBIND; 1520 mtx_unlock_spin(&sched_lock); 1521 } else { 1522 if (p->p_numthreads > max_threads_per_proc) { 1523 /* 1524 * Since kernel thread limit reached, 1525 * don't schedule upcall anymore. 1526 * XXXKSE These code in fact needn't. 1527 */ 1528 mtx_lock_spin(&sched_lock); 1529 td->td_flags &= ~TDF_CAN_UNBIND; 1530 mtx_unlock_spin(&sched_lock); 1531 } else { 1532 if (td->td_standin == NULL) 1533 thread_alloc_spare(td, NULL); 1534 mtx_lock_spin(&sched_lock); 1535 td->td_flags |= TDF_CAN_UNBIND; 1536 mtx_unlock_spin(&sched_lock); 1537 } 1538 } 1539 } 1540} 1541 1542/* 1543 * The extra work we go through if we are a threaded process when we 1544 * return to userland. 1545 * 1546 * If we are a KSE process and returning to user mode, check for 1547 * extra work to do before we return (e.g. for more syscalls 1548 * to complete first). If we were in a critical section, we should 1549 * just return to let it finish. Same if we were in the UTS (in 1550 * which case the mailbox's context's busy indicator will be set). 1551 * The only traps we suport will have set the mailbox. 1552 * We will clear it here. 1553 */ 1554int 1555thread_userret(struct thread *td, struct trapframe *frame) 1556{ 1557 int error; 1558 struct kse_upcall *ku; 1559 struct ksegrp *kg; 1560 struct proc *p; 1561 struct timespec ts; 1562 1563 p = td->td_proc; 1564 kg = td->td_ksegrp; 1565 1566 /* Nothing to do with non-threaded group/process */ 1567 if (td->td_ksegrp->kg_numupcalls == 0) 1568 return (0); 1569 1570 /* 1571 * Stat clock interrupt hit in userland, it 1572 * is returning from interrupt, charge thread's 1573 * userland time for UTS. 1574 */ 1575 if (td->td_flags & TDF_USTATCLOCK) { 1576 thread_update_usr_ticks(td); 1577 mtx_lock_spin(&sched_lock); 1578 td->td_flags &= ~TDF_USTATCLOCK; 1579 mtx_unlock_spin(&sched_lock); 1580 } 1581 1582 /* 1583 * Optimisation: 1584 * This thread has not started any upcall. 1585 * If there is no work to report other than ourself, 1586 * then it can return direct to userland. 1587 */ 1588 if (TD_CAN_UNBIND(td)) { 1589 mtx_lock_spin(&sched_lock); 1590 td->td_flags &= ~TDF_CAN_UNBIND; 1591 mtx_unlock_spin(&sched_lock); 1592 if ((kg->kg_completed == NULL) && 1593 (td->td_upcall->ku_flags & KUF_DOUPCALL) == 0) { 1594 thread_update_sys_ticks(td); 1595 td->td_mailbox = NULL; 1596 return (0); 1597 } 1598 error = thread_export_context(td); 1599 if (error) { 1600 /* 1601 * Failing to do the KSE operation just defaults 1602 * back to synchonous operation, so just return from 1603 * the syscall. 1604 */ 1605 return (0); 1606 } 1607 /* 1608 * There is something to report, and we own an upcall 1609 * strucuture, we can go to userland. 1610 * Turn ourself into an upcall thread. 1611 */ 1612 mtx_lock_spin(&sched_lock); 1613 td->td_flags |= TDF_UPCALLING; 1614 mtx_unlock_spin(&sched_lock); 1615 } else if (td->td_mailbox) { 1616 error = thread_export_context(td); 1617 if (error) { 1618 PROC_LOCK(td->td_proc); 1619 mtx_lock_spin(&sched_lock); 1620 /* possibly upcall with error? */ 1621 } else { 1622 PROC_LOCK(td->td_proc); 1623 mtx_lock_spin(&sched_lock); 1624 /* 1625 * There are upcall threads waiting for 1626 * work to do, wake one of them up. 1627 * XXXKSE Maybe wake all of them up. 1628 */ 1629 if (kg->kg_upsleeps) 1630 wakeup_one(&kg->kg_completed); 1631 } 1632 thread_exit(); 1633 /* NOTREACHED */ 1634 } 1635 1636 if (td->td_flags & TDF_UPCALLING) { 1637 KASSERT(TD_CAN_UNBIND(td) == 0, ("upcall thread can unbind")); 1638 ku = td->td_upcall; 1639 /* 1640 * There is no more work to do and we are going to ride 1641 * this thread up to userland as an upcall. 1642 * Do the last parts of the setup needed for the upcall. 1643 */ 1644 CTR3(KTR_PROC, "userret: upcall thread %p (pid %d, %s)", 1645 td, td->td_proc->p_pid, td->td_proc->p_comm); 1646 1647 /* 1648 * Set user context to the UTS. 1649 * Will use Giant in cpu_thread_clean() because it uses 1650 * kmem_free(kernel_map, ...) 1651 */ 1652 cpu_set_upcall_kse(td, ku); 1653 1654 /* 1655 * Clear TDF_UPCALLING after set upcall context, 1656 * profiling code looks TDF_UPCALLING to avoid account 1657 * a wrong user %EIP 1658 */ 1659 mtx_lock_spin(&sched_lock); 1660 td->td_flags &= ~TDF_UPCALLING; 1661 if (ku->ku_flags & KUF_DOUPCALL) 1662 ku->ku_flags &= ~KUF_DOUPCALL; 1663 mtx_unlock_spin(&sched_lock); 1664 1665 /* 1666 * Unhook the list of completed threads. 1667 * anything that completes after this gets to 1668 * come in next time. 1669 * Put the list of completed thread mailboxes on 1670 * this KSE's mailbox. 1671 */ 1672 error = thread_link_mboxes(kg, ku); 1673 if (error) 1674 goto bad; 1675 1676 /* 1677 * Set state and clear the thread mailbox pointer. 1678 * From now on we are just a bound outgoing process. 1679 * **Problem** userret is often called several times. 1680 * it would be nice if this all happenned only on the first 1681 * time through. (the scan for extra work etc.) 1682 */ 1683 error = suword((caddr_t)&ku->ku_mailbox->km_curthread, 0); 1684 if (error) 1685 goto bad; 1686 1687 /* Export current system time */ 1688 nanotime(&ts); 1689 if (copyout(&ts, 1690 (caddr_t)&ku->ku_mailbox->km_timeofday, sizeof(ts))) { 1691 goto bad; 1692 } 1693 } 1694 /* 1695 * Optimisation: 1696 * Ensure that we have a spare thread available, 1697 * for when we re-enter the kernel. 1698 */ 1699 if (td->td_standin == NULL) 1700 thread_alloc_spare(td, NULL); 1701 1702 /* 1703 * Clear thread mailbox first, then clear system tick count. 1704 * The order is important because thread_statclock() use 1705 * mailbox pointer to see if it is an userland thread or 1706 * an UTS kernel thread. 1707 */ 1708 td->td_mailbox = NULL; 1709 td->td_usticks = 0; 1710 return (0); 1711 1712bad: 1713 /* 1714 * Things are going to be so screwed we should just kill the process. 1715 * how do we do that? 1716 */ 1717 PROC_LOCK(td->td_proc); 1718 psignal(td->td_proc, SIGSEGV); 1719 PROC_UNLOCK(td->td_proc); 1720 td->td_mailbox = NULL; 1721 td->td_usticks = 0; 1722 return (error); /* go sync */ 1723} 1724 1725/* 1726 * Enforce single-threading. 1727 * 1728 * Returns 1 if the caller must abort (another thread is waiting to 1729 * exit the process or similar). Process is locked! 1730 * Returns 0 when you are successfully the only thread running. 1731 * A process has successfully single threaded in the suspend mode when 1732 * There are no threads in user mode. Threads in the kernel must be 1733 * allowed to continue until they get to the user boundary. They may even 1734 * copy out their return values and data before suspending. They may however be 1735 * accellerated in reaching the user boundary as we will wake up 1736 * any sleeping threads that are interruptable. (PCATCH). 1737 */ 1738int 1739thread_single(int force_exit) 1740{ 1741 struct thread *td; 1742 struct thread *td2; 1743 struct proc *p; 1744 1745 td = curthread; 1746 p = td->td_proc; 1747 mtx_assert(&Giant, MA_OWNED); 1748 PROC_LOCK_ASSERT(p, MA_OWNED); 1749 KASSERT((td != NULL), ("curthread is NULL")); 1750 1751 if ((p->p_flag & P_KSES) == 0) 1752 return (0); 1753 1754 /* Is someone already single threading? */ 1755 if (p->p_singlethread) 1756 return (1); 1757 1758 if (force_exit == SINGLE_EXIT) { 1759 p->p_flag |= P_SINGLE_EXIT; 1760 } else 1761 p->p_flag &= ~P_SINGLE_EXIT; 1762 p->p_flag |= P_STOPPED_SINGLE; 1763 p->p_singlethread = td; 1764 /* XXXKSE Which lock protects the below values? */ 1765 while ((p->p_numthreads - p->p_suspcount) != 1) { 1766 mtx_lock_spin(&sched_lock); 1767 FOREACH_THREAD_IN_PROC(p, td2) { 1768 if (td2 == td) 1769 continue; 1770 if (TD_IS_INHIBITED(td2)) { 1771 if (force_exit == SINGLE_EXIT) { 1772 if (TD_IS_SUSPENDED(td2)) { 1773 thread_unsuspend_one(td2); 1774 } 1775 if (TD_ON_SLEEPQ(td2) && 1776 (td2->td_flags & TDF_SINTR)) { 1777 if (td2->td_flags & TDF_CVWAITQ) 1778 cv_abort(td2); 1779 else 1780 abortsleep(td2); 1781 } 1782 } else { 1783 if (TD_IS_SUSPENDED(td2)) 1784 continue; 1785 /* 1786 * maybe other inhibitted states too? 1787 * XXXKSE Is it totally safe to 1788 * suspend a non-interruptable thread? 1789 */ 1790 if (td2->td_inhibitors & 1791 (TDI_SLEEPING | TDI_SWAPPED)) 1792 thread_suspend_one(td2); 1793 } 1794 } 1795 } 1796 /* 1797 * Maybe we suspended some threads.. was it enough? 1798 */ 1799 if ((p->p_numthreads - p->p_suspcount) == 1) { 1800 mtx_unlock_spin(&sched_lock); 1801 break; 1802 } 1803 1804 /* 1805 * Wake us up when everyone else has suspended. 1806 * In the mean time we suspend as well. 1807 */ 1808 thread_suspend_one(td); 1809 mtx_unlock(&Giant); 1810 PROC_UNLOCK(p); 1811 p->p_stats->p_ru.ru_nvcsw++; 1812 mi_switch(); 1813 mtx_unlock_spin(&sched_lock); 1814 mtx_lock(&Giant); 1815 PROC_LOCK(p); 1816 } 1817 if (force_exit == SINGLE_EXIT) { 1818 if (td->td_upcall) { 1819 mtx_lock_spin(&sched_lock); 1820 upcall_remove(td); 1821 mtx_unlock_spin(&sched_lock); 1822 } 1823 kse_purge(p, td); 1824 } 1825 return (0); 1826} 1827 1828/* 1829 * Called in from locations that can safely check to see 1830 * whether we have to suspend or at least throttle for a 1831 * single-thread event (e.g. fork). 1832 * 1833 * Such locations include userret(). 1834 * If the "return_instead" argument is non zero, the thread must be able to 1835 * accept 0 (caller may continue), or 1 (caller must abort) as a result. 1836 * 1837 * The 'return_instead' argument tells the function if it may do a 1838 * thread_exit() or suspend, or whether the caller must abort and back 1839 * out instead. 1840 * 1841 * If the thread that set the single_threading request has set the 1842 * P_SINGLE_EXIT bit in the process flags then this call will never return 1843 * if 'return_instead' is false, but will exit. 1844 * 1845 * P_SINGLE_EXIT | return_instead == 0| return_instead != 0 1846 *---------------+--------------------+--------------------- 1847 * 0 | returns 0 | returns 0 or 1 1848 * | when ST ends | immediatly 1849 *---------------+--------------------+--------------------- 1850 * 1 | thread exits | returns 1 1851 * | | immediatly 1852 * 0 = thread_exit() or suspension ok, 1853 * other = return error instead of stopping the thread. 1854 * 1855 * While a full suspension is under effect, even a single threading 1856 * thread would be suspended if it made this call (but it shouldn't). 1857 * This call should only be made from places where 1858 * thread_exit() would be safe as that may be the outcome unless 1859 * return_instead is set. 1860 */ 1861int 1862thread_suspend_check(int return_instead) 1863{ 1864 struct thread *td; 1865 struct proc *p; 1866 struct ksegrp *kg; 1867 1868 td = curthread; 1869 p = td->td_proc; 1870 kg = td->td_ksegrp; 1871 PROC_LOCK_ASSERT(p, MA_OWNED); 1872 while (P_SHOULDSTOP(p)) { 1873 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 1874 KASSERT(p->p_singlethread != NULL, 1875 ("singlethread not set")); 1876 /* 1877 * The only suspension in action is a 1878 * single-threading. Single threader need not stop. 1879 * XXX Should be safe to access unlocked 1880 * as it can only be set to be true by us. 1881 */ 1882 if (p->p_singlethread == td) 1883 return (0); /* Exempt from stopping. */ 1884 } 1885 if (return_instead) 1886 return (1); 1887 1888 /* 1889 * If the process is waiting for us to exit, 1890 * this thread should just suicide. 1891 * Assumes that P_SINGLE_EXIT implies P_STOPPED_SINGLE. 1892 */ 1893 if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) { 1894 mtx_lock_spin(&sched_lock); 1895 while (mtx_owned(&Giant)) 1896 mtx_unlock(&Giant); 1897 thread_exit(); 1898 } 1899 1900 /* 1901 * When a thread suspends, it just 1902 * moves to the processes's suspend queue 1903 * and stays there. 1904 */ 1905 mtx_lock_spin(&sched_lock); 1906 if ((p->p_flag & P_STOPPED_SIG) && 1907 (p->p_suspcount+1 == p->p_numthreads)) { 1908 mtx_unlock_spin(&sched_lock); 1909 PROC_LOCK(p->p_pptr); 1910 if ((p->p_pptr->p_procsig->ps_flag & 1911 PS_NOCLDSTOP) == 0) { 1912 psignal(p->p_pptr, SIGCHLD); 1913 } 1914 PROC_UNLOCK(p->p_pptr); 1915 mtx_lock_spin(&sched_lock); 1916 } 1917 mtx_assert(&Giant, MA_NOTOWNED); 1918 thread_suspend_one(td); 1919 PROC_UNLOCK(p); 1920 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 1921 if (p->p_numthreads == p->p_suspcount) { 1922 thread_unsuspend_one(p->p_singlethread); 1923 } 1924 } 1925 p->p_stats->p_ru.ru_nivcsw++; 1926 mi_switch(); 1927 mtx_unlock_spin(&sched_lock); 1928 PROC_LOCK(p); 1929 } 1930 return (0); 1931} 1932 1933void 1934thread_suspend_one(struct thread *td) 1935{ 1936 struct proc *p = td->td_proc; 1937 1938 mtx_assert(&sched_lock, MA_OWNED); 1939 p->p_suspcount++; 1940 TD_SET_SUSPENDED(td); 1941 TAILQ_INSERT_TAIL(&p->p_suspended, td, td_runq); 1942 /* 1943 * Hack: If we are suspending but are on the sleep queue 1944 * then we are in msleep or the cv equivalent. We 1945 * want to look like we have two Inhibitors. 1946 * May already be set.. doesn't matter. 1947 */ 1948 if (TD_ON_SLEEPQ(td)) 1949 TD_SET_SLEEPING(td); 1950} 1951 1952void 1953thread_unsuspend_one(struct thread *td) 1954{ 1955 struct proc *p = td->td_proc; 1956 1957 mtx_assert(&sched_lock, MA_OWNED); 1958 TAILQ_REMOVE(&p->p_suspended, td, td_runq); 1959 TD_CLR_SUSPENDED(td); 1960 p->p_suspcount--; 1961 setrunnable(td); 1962} 1963 1964/* 1965 * Allow all threads blocked by single threading to continue running. 1966 */ 1967void 1968thread_unsuspend(struct proc *p) 1969{ 1970 struct thread *td; 1971 1972 mtx_assert(&sched_lock, MA_OWNED); 1973 PROC_LOCK_ASSERT(p, MA_OWNED); 1974 if (!P_SHOULDSTOP(p)) { 1975 while (( td = TAILQ_FIRST(&p->p_suspended))) { 1976 thread_unsuspend_one(td); 1977 } 1978 } else if ((P_SHOULDSTOP(p) == P_STOPPED_SINGLE) && 1979 (p->p_numthreads == p->p_suspcount)) { 1980 /* 1981 * Stopping everything also did the job for the single 1982 * threading request. Now we've downgraded to single-threaded, 1983 * let it continue. 1984 */ 1985 thread_unsuspend_one(p->p_singlethread); 1986 } 1987} 1988 1989void 1990thread_single_end(void) 1991{ 1992 struct thread *td; 1993 struct proc *p; 1994 1995 td = curthread; 1996 p = td->td_proc; 1997 PROC_LOCK_ASSERT(p, MA_OWNED); 1998 p->p_flag &= ~P_STOPPED_SINGLE; 1999 p->p_singlethread = NULL; 2000 /* 2001 * If there are other threads they mey now run, 2002 * unless of course there is a blanket 'stop order' 2003 * on the process. The single threader must be allowed 2004 * to continue however as this is a bad place to stop. 2005 */ 2006 if ((p->p_numthreads != 1) && (!P_SHOULDSTOP(p))) { 2007 mtx_lock_spin(&sched_lock); 2008 while (( td = TAILQ_FIRST(&p->p_suspended))) { 2009 thread_unsuspend_one(td); 2010 } 2011 mtx_unlock_spin(&sched_lock); 2012 } 2013} 2014 2015 2016