Print this page
OS-4818 contract template disappears on exec
OS-5311 docker init children not always in the correct contract
Reviewed by: Patrick Mooney <patrick.mooney@joyent.com>
OS-4937 lxbrand ptracer count updates can race
Reviewed by: Jerry Jelinek <jerry.jelinek@joyent.com>
Reviewed by: Joshua M. Clulow <jmc@joyent.com>
OS-4534 lwp_exit P_PR_LOCK assertion failure
OS-4188 NULL dereference in lwp_hash_in
Reviewed by: Jerry Jelinek <jerry.jelinek@joyent.com>
Reviewed by: Joshua M. Clulow <jmc@joyent.com>
OS-4151 setbrand hooks should be sane during fork
Reviewed by: Jerry Jelinek <jerry.jelinek@joyent.com>
Reviewed by: Joshua M. Clulow <jmc@joyent.com>
OS-4129 lxbrand should not abuse p_brand_data for storing exit signal
Reviewed by: Jerry Jelinek <jerry.jelinek@joyent.com>
Reviewed by: Joshua M. Clulow <jmc@joyent.com>


   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 /*
  28  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
  29  */
  30 
  31 #include <sys/param.h>
  32 #include <sys/types.h>
  33 #include <sys/sysmacros.h>
  34 #include <sys/systm.h>
  35 #include <sys/thread.h>
  36 #include <sys/proc.h>
  37 #include <sys/task.h>
  38 #include <sys/project.h>
  39 #include <sys/signal.h>
  40 #include <sys/errno.h>
  41 #include <sys/vmparam.h>
  42 #include <sys/stack.h>
  43 #include <sys/procfs.h>
  44 #include <sys/prsystm.h>
  45 #include <sys/cpuvar.h>
  46 #include <sys/kmem.h>
  47 #include <sys/vtrace.h>
  48 #include <sys/door.h>
  49 #include <vm/seg_kp.h>
  50 #include <sys/debug.h>
  51 #include <sys/tnf.h>
  52 #include <sys/schedctl.h>
  53 #include <sys/poll.h>
  54 #include <sys/copyops.h>
  55 #include <sys/lwp_upimutex_impl.h>
  56 #include <sys/cpupart.h>
  57 #include <sys/lgrp.h>
  58 #include <sys/rctl.h>
  59 #include <sys/contract_impl.h>


  60 #include <sys/cpc_impl.h>
  61 #include <sys/sdt.h>
  62 #include <sys/cmn_err.h>
  63 #include <sys/brand.h>
  64 #include <sys/cyclic.h>
  65 #include <sys/pool.h>
  66 
  67 /* hash function for the lwpid hash table, p->p_tidhash[] */
  68 #define TIDHASH(tid, hash_sz)   ((tid) & ((hash_sz) - 1))
  69 
  70 void *segkp_lwp;                /* cookie for pool of segkp resources */
  71 extern void reapq_move_lq_to_tq(kthread_t *);
  72 extern void freectx_ctx(struct ctxop *);
  73 
  74 /*
  75  * Create a kernel thread associated with a particular system process.  Give
  76  * it an LWP so that microstate accounting will be available for it.
  77  */
  78 kthread_t *
  79 lwp_kernel_create(proc_t *p, void (*proc)(), void *arg, int state, pri_t pri)


  98 {
  99         klwp_t *lwp = NULL;
 100         kthread_t *t;
 101         kthread_t *tx;
 102         cpupart_t *oldpart = NULL;
 103         size_t  stksize;
 104         caddr_t lwpdata = NULL;
 105         processorid_t   binding;
 106         int err = 0;
 107         kproject_t *oldkpj, *newkpj;
 108         void *bufp = NULL;
 109         klwp_t *curlwp;
 110         lwpent_t *lep;
 111         lwpdir_t *old_dir = NULL;
 112         uint_t old_dirsz = 0;
 113         tidhash_t *old_hash = NULL;
 114         uint_t old_hashsz = 0;
 115         ret_tidhash_t *ret_tidhash = NULL;
 116         int i;
 117         int rctlfail = 0;
 118         boolean_t branded = 0;
 119         struct ctxop *ctx = NULL;
 120 
 121         ASSERT(cid != sysdccid);        /* system threads must start in SYS */
 122 
 123         ASSERT(p != &p0);           /* No new LWPs in p0. */
 124 
 125         mutex_enter(&p->p_lock);
 126         mutex_enter(&p->p_zone->zone_nlwps_lock);
 127         /*
 128          * don't enforce rctl limits on system processes
 129          */
 130         if (!CLASS_KERNEL(cid)) {
 131                 if (p->p_task->tk_nlwps >= p->p_task->tk_nlwps_ctl)
 132                         if (rctl_test(rc_task_lwps, p->p_task->tk_rctls, p,
 133                             1, 0) & RCT_DENY)
 134                                 rctlfail = 1;
 135                 if (p->p_task->tk_proj->kpj_nlwps >=
 136                     p->p_task->tk_proj->kpj_nlwps_ctl)
 137                         if (rctl_test(rc_project_nlwps,
 138                             p->p_task->tk_proj->kpj_rctls, p, 1, 0)


 266         if (curlwp != NULL && curlwp->lwp_childstksz != 0)
 267                 lwp->lwp_childstksz = curlwp->lwp_childstksz;
 268 
 269         t->t_stk = lwp_stk_init(lwp, t->t_stk);
 270         thread_load(t, proc, arg, len);
 271 
 272         /*
 273          * Allocate the SIGPROF buffer if ITIMER_REALPROF is in effect.
 274          */
 275         if (p->p_rprof_cyclic != CYCLIC_NONE)
 276                 t->t_rprof = kmem_zalloc(sizeof (struct rprof), KM_SLEEP);
 277 
 278         if (cid != NOCLASS)
 279                 (void) CL_ALLOC(&bufp, cid, KM_SLEEP);
 280 
 281         /*
 282          * Allocate an lwp directory entry for the new lwp.
 283          */
 284         lep = kmem_zalloc(sizeof (*lep), KM_SLEEP);
 285 






 286         mutex_enter(&p->p_lock);







 287 grow:
 288         /*
 289          * Grow the lwp (thread) directory and lwpid hash table if necessary.
 290          * A note on the growth algorithm:
 291          *      The new lwp directory size is computed as:
 292          *              new = 2 * old + 2
 293          *      Starting with an initial size of 2 (see exec_common()),
 294          *      this yields numbers that are a power of two minus 2:
 295          *              2, 6, 14, 30, 62, 126, 254, 510, 1022, ...
 296          *      The size of the lwpid hash table must be a power of two
 297          *      and must be commensurate in size with the lwp directory
 298          *      so that hash bucket chains remain short.  Therefore,
 299          *      the lwpid hash table size is computed as:
 300          *              hashsz = (dirsz + 2) / 2
 301          *      which leads to these hash table sizes corresponding to
 302          *      the above directory sizes:
 303          *              2, 4, 8, 16, 32, 64, 128, 256, 512, ...
 304          * A note on growing the hash table:
 305          *      For performance reasons, code in lwp_unpark() does not
 306          *      acquire curproc->p_lock when searching the hash table.


 613                                 p->p_lwpid = 1;
 614                         }
 615                         if ((t->t_tid = ++p->p_lwpid) == prev_id) {
 616                                 /*
 617                                  * All lwpids are allocated; fail the request.
 618                                  */
 619                                 err = 1;
 620                                 atomic_inc_32(&p->p_zone->zone_ffnoproc);
 621                                 goto error;
 622                         }
 623                         /*
 624                          * We only need to worry about colliding with an id
 625                          * that's already in use if this process has
 626                          * cycled through all available lwp ids.
 627                          */
 628                         if ((p->p_flag & SLWPWRAP) == 0)
 629                                 break;
 630                 } while (lwp_hash_lookup(p, t->t_tid) != NULL);
 631         }
 632 
 633         /*
 634          * If this is a branded process, let the brand do any necessary lwp
 635          * initialization.
 636          */
 637         if (PROC_IS_BRANDED(p)) {
 638                 if (BROP(p)->b_initlwp(lwp)) {
 639                         err = 1;
 640                         atomic_inc_32(&p->p_zone->zone_ffmisc);
 641                         goto error;
 642                 }
 643                 branded = 1;
 644         }
 645 
 646         if (t->t_tid == 1) {
 647                 kpreempt_disable();
 648                 ASSERT(t->t_lpl != NULL);
 649                 p->p_t1_lgrpid = t->t_lpl->lpl_lgrpid;
 650                 kpreempt_enable();
 651                 if (p->p_tr_lgrpid != LGRP_NONE &&
 652                     p->p_tr_lgrpid != p->p_t1_lgrpid) {
 653                         lgrp_update_trthr_migrations(1);
 654                 }
 655         }
 656 
 657         p->p_lwpcnt++;
 658         t->t_waitfor = -1;
 659 
 660         /*
 661          * Turn microstate accounting on for thread if on for process.
 662          */
 663         if (p->p_flag & SMSACCT)
 664                 t->t_proc_flag |= TP_MSACCT;
 665 
 666         /*
 667          * If the process has watchpoints, mark the new thread as such.
 668          */
 669         if (pr_watch_active(p))
 670                 watch_enable(t);
 671 
 672         /*
 673          * The lwp is being created in the stopped state.
 674          * We set all the necessary flags to indicate that fact here.
 675          * We omit the TS_CREATE flag from t_schedflag so that the lwp
 676          * cannot be set running until the caller is finished with it,
 677          * even if lwp_continue() is called on it after we drop p->p_lock.


 679          * the caller must call lwp_create_done() to allow the lwp
 680          * to be set running.  If the TP_HOLDLWP is left set, the
 681          * lwp will suspend itself after reaching system call exit.
 682          */
 683         init_mstate(t, LMS_STOPPED);
 684         t->t_proc_flag |= TP_HOLDLWP;
 685         t->t_schedflag |= (TS_ALLSTART & ~(TS_CSTART | TS_CREATE));
 686         t->t_whystop = PR_SUSPENDED;
 687         t->t_whatstop = SUSPEND_NORMAL;
 688         t->t_sig_check = 1;  /* ensure that TP_HOLDLWP is honored */
 689 
 690         /*
 691          * Set system call processing flags in case tracing or profiling
 692          * is set.  The first system call will evaluate these and turn
 693          * them off if they aren't needed.
 694          */
 695         t->t_pre_sys = 1;
 696         t->t_post_sys = 1;
 697 
 698         /*


















 699          * Insert the new thread into the list of all threads.
 700          */

 701         if ((tx = p->p_tlist) == NULL) {
 702                 t->t_back = t;
 703                 t->t_forw = t;
 704                 p->p_tlist = t;
 705         } else {
 706                 t->t_forw = tx;
 707                 t->t_back = tx->t_back;
 708                 tx->t_back->t_forw = t;
 709                 tx->t_back = t;
 710         }
 711 
 712         /*
 713          * Insert the new lwp into an lwp directory slot position
 714          * and into the lwpid hash table.
 715          */
 716         lep->le_thread = t;
 717         lep->le_lwpid = t->t_tid;
 718         lep->le_start = t->t_start;
 719         lwp_hash_in(p, lep, p->p_tidhash, p->p_tidhash_sz, 1);
 720 







 721         if (state == TS_RUN) {
 722                 /*
 723                  * We set the new lwp running immediately.
 724                  */
 725                 t->t_proc_flag &= ~TP_HOLDLWP;
 726                 lwp_create_done(t);
 727         }
 728 
 729 error:
 730         if (err) {
 731                 if (CLASS_KERNEL(cid)) {
 732                         /*
 733                          * This should only happen if a system process runs
 734                          * out of lwpids, which shouldn't occur.
 735                          */
 736                         panic("Failed to create a system LWP");
 737                 }
 738                 /*
 739                  * We have failed to create an lwp, so decrement the number
 740                  * of lwps in the task and let the lgroup load averages know
 741                  * that this thread isn't going to show up.
 742                  */
 743                 kpreempt_disable();
 744                 lgrp_move_thread(t, NULL, 1);
 745                 kpreempt_enable();
 746 
 747                 ASSERT(MUTEX_HELD(&p->p_lock));
 748                 mutex_enter(&p->p_zone->zone_nlwps_lock);
 749                 p->p_task->tk_nlwps--;
 750                 p->p_task->tk_proj->kpj_nlwps--;
 751                 p->p_zone->zone_nlwps--;
 752                 mutex_exit(&p->p_zone->zone_nlwps_lock);
 753                 if (cid != NOCLASS && bufp != NULL)
 754                         CL_FREE(cid, bufp);
 755 
 756                 if (branded)
 757                         BROP(p)->b_freelwp(lwp);

 758 
 759                 mutex_exit(&p->p_lock);
 760                 t->t_state = TS_FREE;
 761                 thread_rele(t);
 762 
 763                 /*
 764                  * We need to remove t from the list of all threads
 765                  * because thread_exit()/lwp_exit() isn't called on t.
 766                  */
 767                 mutex_enter(&pidlock);
 768                 ASSERT(t != t->t_next);              /* t0 never exits */
 769                 t->t_next->t_prev = t->t_prev;
 770                 t->t_prev->t_next = t->t_next;
 771                 mutex_exit(&pidlock);
 772 
 773                 thread_free(t);
 774                 kmem_free(lep, sizeof (*lep));
 775                 lwp = NULL;
 776         } else {
 777                 mutex_exit(&p->p_lock);


 810         /*
 811          * If TS_CSTART is set, lwp_continue(t) has been called and
 812          * has already incremented p_lwprcnt; avoid doing this twice.
 813          */
 814         if (!(t->t_schedflag & TS_CSTART))
 815                 p->p_lwprcnt++;
 816         t->t_schedflag |= (TS_CSTART | TS_CREATE);
 817         setrun_locked(t);
 818         thread_unlock(t);
 819 }
 820 
 821 /*
 822  * Copy an LWP's active templates, and clear the latest contracts.
 823  */
 824 void
 825 lwp_ctmpl_copy(klwp_t *dst, klwp_t *src)
 826 {
 827         int i;
 828 
 829         for (i = 0; i < ct_ntypes; i++) {
 830                 dst->lwp_ct_active[i] = ctmpl_dup(src->lwp_ct_active[i]);


















 831                 dst->lwp_ct_latest[i] = NULL;

 832         }
 833 }
 834 
 835 /*
 836  * Clear an LWP's contract template state.
 837  */
 838 void
 839 lwp_ctmpl_clear(klwp_t *lwp)
 840 {
 841         ct_template_t *tmpl;
 842         int i;
 843 
 844         for (i = 0; i < ct_ntypes; i++) {
 845                 if ((tmpl = lwp->lwp_ct_active[i]) != NULL) {
 846                         ctmpl_free(tmpl);
 847                         lwp->lwp_ct_active[i] = NULL;
 848                 }
 849 
 850                 if (lwp->lwp_ct_latest[i] != NULL) {
 851                         contract_rele(lwp->lwp_ct_latest[i]);
 852                         lwp->lwp_ct_latest[i] = NULL;
 853                 }












 854         }





 855 }
 856 
 857 /*
 858  * Individual lwp exit.
 859  * If this is the last lwp, exit the whole process.
 860  */
 861 void
 862 lwp_exit(void)
 863 {
 864         kthread_t *t = curthread;
 865         klwp_t *lwp = ttolwp(t);
 866         proc_t *p = ttoproc(t);
 867 
 868         ASSERT(MUTEX_HELD(&p->p_lock));
 869 
 870         mutex_exit(&p->p_lock);
 871 
 872 #if defined(__sparc)
 873         /*
 874          * Ensure that the user stack is fully abandoned..
 875          */
 876         trash_user_windows();
 877 #endif
 878 
 879         tsd_exit();                     /* free thread specific data */
 880 
 881         kcpc_passivate();               /* Clean up performance counter state */
 882 
 883         pollcleanup();
 884 
 885         if (t->t_door)
 886                 door_slam();
 887 
 888         if (t->t_schedctl != NULL)
 889                 schedctl_lwp_cleanup(t);
 890 
 891         if (t->t_upimutex != NULL)
 892                 upimutex_cleanup();
 893 
 894         /*
 895          * Perform any brand specific exit processing, then release any
 896          * brand data associated with the lwp
 897          */
 898         if (PROC_IS_BRANDED(p))
 899                 BROP(p)->b_lwpexit(lwp);
 900 
 901         lwp_pcb_exit();
 902 
 903         mutex_enter(&p->p_lock);
 904         lwp_cleanup();
 905 
 906         /*
 907          * When this process is dumping core, its lwps are held here
 908          * until the core dump is finished. Then exitlwps() is called
 909          * again to release these lwps so that they can finish exiting.
 910          */
 911         if (p->p_flag & SCOREDUMP)
 912                 stop(PR_SUSPENDED, SUSPEND_NORMAL);
 913 
 914         /*
 915          * Block the process against /proc now that we have really acquired
 916          * p->p_lock (to decrement p_lwpcnt and manipulate p_tlist at least).
 917          */
 918         prbarrier(p);
 919 
 920         /*


 924             p->p_lwpcnt == p->p_lwpdaemon + 1) {
 925                 mutex_exit(&p->p_lock);
 926                 if (proc_exit(CLD_EXITED, 0) == 0) {
 927                         /* Restarting init. */
 928                         return;
 929                 }
 930 
 931                 /*
 932                  * proc_exit() returns a non-zero value when some other
 933                  * lwp got there first.  We just have to continue in
 934                  * lwp_exit().
 935                  */
 936                 mutex_enter(&p->p_lock);
 937                 ASSERT(curproc->p_flag & SEXITLWPS);
 938                 prbarrier(p);
 939         }
 940 
 941         DTRACE_PROC(lwp__exit);
 942 
 943         /*












 944          * If the lwp is a detached lwp or if the process is exiting,
 945          * remove (lwp_hash_out()) the lwp from the lwp directory.
 946          * Otherwise null out the lwp's le_thread pointer in the lwp
 947          * directory so that other threads will see it as a zombie lwp.
 948          */
 949         prlwpexit(t);           /* notify /proc */
 950         if (!(t->t_proc_flag & TP_TWAIT) || (p->p_flag & SEXITLWPS))
 951                 lwp_hash_out(p, t->t_tid);
 952         else {
 953                 ASSERT(!(t->t_proc_flag & TP_DAEMON));
 954                 p->p_lwpdir[t->t_dslot].ld_entry->le_thread = NULL;
 955                 p->p_zombcnt++;
 956                 cv_broadcast(&p->p_lwpexit);
 957         }
 958         if (t->t_proc_flag & TP_DAEMON) {
 959                 p->p_lwpdaemon--;
 960                 t->t_proc_flag &= ~TP_DAEMON;
 961         }
 962         t->t_proc_flag &= ~TP_TWAIT;
 963 


1084         /*
1085          * If this is the /proc agent lwp that is exiting, readjust p_lwpid
1086          * so it appears that the agent never existed, and clear p_agenttp.
1087          */
1088         if (t == p->p_agenttp) {
1089                 ASSERT(t->t_tid == p->p_lwpid);
1090                 p->p_lwpid--;
1091                 p->p_agenttp = NULL;
1092         }
1093 
1094         /*
1095          * Do lgroup bookkeeping to account for thread exiting.
1096          */
1097         kpreempt_disable();
1098         lgrp_move_thread(t, NULL, 1);
1099         if (t->t_tid == 1) {
1100                 p->p_t1_lgrpid = LGRP_NONE;
1101         }
1102         kpreempt_enable();
1103 
1104         lwp_ctmpl_clear(ttolwp(t));
1105 }
1106 
1107 int
1108 lwp_suspend(kthread_t *t)
1109 {
1110         int tid;
1111         proc_t *p = ttoproc(t);
1112 
1113         ASSERT(MUTEX_HELD(&p->p_lock));
1114 
1115         /*
1116          * Set the thread's TP_HOLDLWP flag so it will stop in holdlwp().
1117          * If an lwp is stopping itself, there is no need to wait.
1118          */
1119 top:
1120         t->t_proc_flag |= TP_HOLDLWP;
1121         if (t == curthread) {
1122                 t->t_sig_check = 1;
1123         } else {
1124                 /*




   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 /*
  28  * Copyright 2016, Joyent, Inc.
  29  */
  30 
  31 #include <sys/param.h>
  32 #include <sys/types.h>
  33 #include <sys/sysmacros.h>
  34 #include <sys/systm.h>
  35 #include <sys/thread.h>
  36 #include <sys/proc.h>
  37 #include <sys/task.h>
  38 #include <sys/project.h>
  39 #include <sys/signal.h>
  40 #include <sys/errno.h>
  41 #include <sys/vmparam.h>
  42 #include <sys/stack.h>
  43 #include <sys/procfs.h>
  44 #include <sys/prsystm.h>
  45 #include <sys/cpuvar.h>
  46 #include <sys/kmem.h>
  47 #include <sys/vtrace.h>
  48 #include <sys/door.h>
  49 #include <vm/seg_kp.h>
  50 #include <sys/debug.h>
  51 #include <sys/tnf.h>
  52 #include <sys/schedctl.h>
  53 #include <sys/poll.h>
  54 #include <sys/copyops.h>
  55 #include <sys/lwp_upimutex_impl.h>
  56 #include <sys/cpupart.h>
  57 #include <sys/lgrp.h>
  58 #include <sys/rctl.h>
  59 #include <sys/contract_impl.h>
  60 #include <sys/contract/process.h>
  61 #include <sys/contract/process_impl.h>
  62 #include <sys/cpc_impl.h>
  63 #include <sys/sdt.h>
  64 #include <sys/cmn_err.h>
  65 #include <sys/brand.h>
  66 #include <sys/cyclic.h>
  67 #include <sys/pool.h>
  68 
  69 /* hash function for the lwpid hash table, p->p_tidhash[] */
  70 #define TIDHASH(tid, hash_sz)   ((tid) & ((hash_sz) - 1))
  71 
  72 void *segkp_lwp;                /* cookie for pool of segkp resources */
  73 extern void reapq_move_lq_to_tq(kthread_t *);
  74 extern void freectx_ctx(struct ctxop *);
  75 
  76 /*
  77  * Create a kernel thread associated with a particular system process.  Give
  78  * it an LWP so that microstate accounting will be available for it.
  79  */
  80 kthread_t *
  81 lwp_kernel_create(proc_t *p, void (*proc)(), void *arg, int state, pri_t pri)


 100 {
 101         klwp_t *lwp = NULL;
 102         kthread_t *t;
 103         kthread_t *tx;
 104         cpupart_t *oldpart = NULL;
 105         size_t  stksize;
 106         caddr_t lwpdata = NULL;
 107         processorid_t   binding;
 108         int err = 0;
 109         kproject_t *oldkpj, *newkpj;
 110         void *bufp = NULL;
 111         klwp_t *curlwp;
 112         lwpent_t *lep;
 113         lwpdir_t *old_dir = NULL;
 114         uint_t old_dirsz = 0;
 115         tidhash_t *old_hash = NULL;
 116         uint_t old_hashsz = 0;
 117         ret_tidhash_t *ret_tidhash = NULL;
 118         int i;
 119         int rctlfail = 0;
 120         void *brand_data = NULL;
 121         struct ctxop *ctx = NULL;
 122 
 123         ASSERT(cid != sysdccid);        /* system threads must start in SYS */
 124 
 125         ASSERT(p != &p0);           /* No new LWPs in p0. */
 126 
 127         mutex_enter(&p->p_lock);
 128         mutex_enter(&p->p_zone->zone_nlwps_lock);
 129         /*
 130          * don't enforce rctl limits on system processes
 131          */
 132         if (!CLASS_KERNEL(cid)) {
 133                 if (p->p_task->tk_nlwps >= p->p_task->tk_nlwps_ctl)
 134                         if (rctl_test(rc_task_lwps, p->p_task->tk_rctls, p,
 135                             1, 0) & RCT_DENY)
 136                                 rctlfail = 1;
 137                 if (p->p_task->tk_proj->kpj_nlwps >=
 138                     p->p_task->tk_proj->kpj_nlwps_ctl)
 139                         if (rctl_test(rc_project_nlwps,
 140                             p->p_task->tk_proj->kpj_rctls, p, 1, 0)


 268         if (curlwp != NULL && curlwp->lwp_childstksz != 0)
 269                 lwp->lwp_childstksz = curlwp->lwp_childstksz;
 270 
 271         t->t_stk = lwp_stk_init(lwp, t->t_stk);
 272         thread_load(t, proc, arg, len);
 273 
 274         /*
 275          * Allocate the SIGPROF buffer if ITIMER_REALPROF is in effect.
 276          */
 277         if (p->p_rprof_cyclic != CYCLIC_NONE)
 278                 t->t_rprof = kmem_zalloc(sizeof (struct rprof), KM_SLEEP);
 279 
 280         if (cid != NOCLASS)
 281                 (void) CL_ALLOC(&bufp, cid, KM_SLEEP);
 282 
 283         /*
 284          * Allocate an lwp directory entry for the new lwp.
 285          */
 286         lep = kmem_zalloc(sizeof (*lep), KM_SLEEP);
 287 
 288         /*
 289          * If necessary, speculatively allocate lwp brand data.  This is done
 290          * ahead of time so p_lock need not be dropped during lwp branding.
 291          */
 292         if (PROC_IS_BRANDED(p) && BROP(p)->b_lwpdata_alloc != NULL) {
 293                 if ((brand_data = BROP(p)->b_lwpdata_alloc(p)) == NULL) {
 294                         mutex_enter(&p->p_lock);
 295                         err = 1;
 296                         atomic_inc_32(&p->p_zone->zone_ffmisc);
 297                         goto error;
 298                 }
 299         }
 300 
 301         mutex_enter(&p->p_lock);
 302 grow:
 303         /*
 304          * Grow the lwp (thread) directory and lwpid hash table if necessary.
 305          * A note on the growth algorithm:
 306          *      The new lwp directory size is computed as:
 307          *              new = 2 * old + 2
 308          *      Starting with an initial size of 2 (see exec_common()),
 309          *      this yields numbers that are a power of two minus 2:
 310          *              2, 6, 14, 30, 62, 126, 254, 510, 1022, ...
 311          *      The size of the lwpid hash table must be a power of two
 312          *      and must be commensurate in size with the lwp directory
 313          *      so that hash bucket chains remain short.  Therefore,
 314          *      the lwpid hash table size is computed as:
 315          *              hashsz = (dirsz + 2) / 2
 316          *      which leads to these hash table sizes corresponding to
 317          *      the above directory sizes:
 318          *              2, 4, 8, 16, 32, 64, 128, 256, 512, ...
 319          * A note on growing the hash table:
 320          *      For performance reasons, code in lwp_unpark() does not
 321          *      acquire curproc->p_lock when searching the hash table.


 628                                 p->p_lwpid = 1;
 629                         }
 630                         if ((t->t_tid = ++p->p_lwpid) == prev_id) {
 631                                 /*
 632                                  * All lwpids are allocated; fail the request.
 633                                  */
 634                                 err = 1;
 635                                 atomic_inc_32(&p->p_zone->zone_ffnoproc);
 636                                 goto error;
 637                         }
 638                         /*
 639                          * We only need to worry about colliding with an id
 640                          * that's already in use if this process has
 641                          * cycled through all available lwp ids.
 642                          */
 643                         if ((p->p_flag & SLWPWRAP) == 0)
 644                                 break;
 645                 } while (lwp_hash_lookup(p, t->t_tid) != NULL);
 646         }
 647 












 648 
 649         if (t->t_tid == 1) {
 650                 kpreempt_disable();
 651                 ASSERT(t->t_lpl != NULL);
 652                 p->p_t1_lgrpid = t->t_lpl->lpl_lgrpid;
 653                 kpreempt_enable();
 654                 if (p->p_tr_lgrpid != LGRP_NONE &&
 655                     p->p_tr_lgrpid != p->p_t1_lgrpid) {
 656                         lgrp_update_trthr_migrations(1);
 657                 }
 658         }
 659 

 660         t->t_waitfor = -1;
 661 
 662         /*
 663          * Turn microstate accounting on for thread if on for process.
 664          */
 665         if (p->p_flag & SMSACCT)
 666                 t->t_proc_flag |= TP_MSACCT;
 667 
 668         /*
 669          * If the process has watchpoints, mark the new thread as such.
 670          */
 671         if (pr_watch_active(p))
 672                 watch_enable(t);
 673 
 674         /*
 675          * The lwp is being created in the stopped state.
 676          * We set all the necessary flags to indicate that fact here.
 677          * We omit the TS_CREATE flag from t_schedflag so that the lwp
 678          * cannot be set running until the caller is finished with it,
 679          * even if lwp_continue() is called on it after we drop p->p_lock.


 681          * the caller must call lwp_create_done() to allow the lwp
 682          * to be set running.  If the TP_HOLDLWP is left set, the
 683          * lwp will suspend itself after reaching system call exit.
 684          */
 685         init_mstate(t, LMS_STOPPED);
 686         t->t_proc_flag |= TP_HOLDLWP;
 687         t->t_schedflag |= (TS_ALLSTART & ~(TS_CSTART | TS_CREATE));
 688         t->t_whystop = PR_SUSPENDED;
 689         t->t_whatstop = SUSPEND_NORMAL;
 690         t->t_sig_check = 1;  /* ensure that TP_HOLDLWP is honored */
 691 
 692         /*
 693          * Set system call processing flags in case tracing or profiling
 694          * is set.  The first system call will evaluate these and turn
 695          * them off if they aren't needed.
 696          */
 697         t->t_pre_sys = 1;
 698         t->t_post_sys = 1;
 699 
 700         /*
 701          * Perform lwp branding
 702          *
 703          * The b_initlwp hook is _not_ allowed to drop p->p_lock as it must be
 704          * continuously held between when the tidhash is sized and when the lwp
 705          * is inserted into it.  Operations requiring p->p_lock to be
 706          * temporarily dropped can be performed in b_initlwp_post.
 707          */
 708         if (PROC_IS_BRANDED(p)) {
 709                 BROP(p)->b_initlwp(lwp, brand_data);
 710                 /*
 711                  * The b_initlwp hook is expected to consume any preallocated
 712                  * brand_data in a way that prepares it for deallocation by the
 713                  * b_freelwp hook.
 714                  */
 715                 brand_data = NULL;
 716         }
 717 
 718         /*
 719          * Insert the new thread into the list of all threads.
 720          */
 721         p->p_lwpcnt++;
 722         if ((tx = p->p_tlist) == NULL) {
 723                 t->t_back = t;
 724                 t->t_forw = t;
 725                 p->p_tlist = t;
 726         } else {
 727                 t->t_forw = tx;
 728                 t->t_back = tx->t_back;
 729                 tx->t_back->t_forw = t;
 730                 tx->t_back = t;
 731         }
 732 
 733         /*
 734          * Insert the new lwp into an lwp directory slot position
 735          * and into the lwpid hash table.
 736          */
 737         lep->le_thread = t;
 738         lep->le_lwpid = t->t_tid;
 739         lep->le_start = t->t_start;
 740         lwp_hash_in(p, lep, p->p_tidhash, p->p_tidhash_sz, 1);
 741 
 742         /*
 743          * Complete lwp branding
 744          */
 745         if (PROC_IS_BRANDED(p) && BROP(p)->b_initlwp_post != NULL) {
 746                 BROP(p)->b_initlwp_post(lwp);
 747         }
 748 
 749         if (state == TS_RUN) {
 750                 /*
 751                  * We set the new lwp running immediately.
 752                  */
 753                 t->t_proc_flag &= ~TP_HOLDLWP;
 754                 lwp_create_done(t);
 755         }
 756 
 757 error:
 758         if (err) {
 759                 if (CLASS_KERNEL(cid)) {
 760                         /*
 761                          * This should only happen if a system process runs
 762                          * out of lwpids, which shouldn't occur.
 763                          */
 764                         panic("Failed to create a system LWP");
 765                 }
 766                 /*
 767                  * We have failed to create an lwp, so decrement the number
 768                  * of lwps in the task and let the lgroup load averages know
 769                  * that this thread isn't going to show up.
 770                  */
 771                 kpreempt_disable();
 772                 lgrp_move_thread(t, NULL, 1);
 773                 kpreempt_enable();
 774 
 775                 ASSERT(MUTEX_HELD(&p->p_lock));
 776                 mutex_enter(&p->p_zone->zone_nlwps_lock);
 777                 p->p_task->tk_nlwps--;
 778                 p->p_task->tk_proj->kpj_nlwps--;
 779                 p->p_zone->zone_nlwps--;
 780                 mutex_exit(&p->p_zone->zone_nlwps_lock);
 781                 if (cid != NOCLASS && bufp != NULL)
 782                         CL_FREE(cid, bufp);
 783 
 784                 if (brand_data != NULL) {
 785                         BROP(p)->b_lwpdata_free(brand_data);
 786                 }
 787 
 788                 mutex_exit(&p->p_lock);
 789                 t->t_state = TS_FREE;
 790                 thread_rele(t);
 791 
 792                 /*
 793                  * We need to remove t from the list of all threads
 794                  * because thread_exit()/lwp_exit() isn't called on t.
 795                  */
 796                 mutex_enter(&pidlock);
 797                 ASSERT(t != t->t_next);              /* t0 never exits */
 798                 t->t_next->t_prev = t->t_prev;
 799                 t->t_prev->t_next = t->t_next;
 800                 mutex_exit(&pidlock);
 801 
 802                 thread_free(t);
 803                 kmem_free(lep, sizeof (*lep));
 804                 lwp = NULL;
 805         } else {
 806                 mutex_exit(&p->p_lock);


 839         /*
 840          * If TS_CSTART is set, lwp_continue(t) has been called and
 841          * has already incremented p_lwprcnt; avoid doing this twice.
 842          */
 843         if (!(t->t_schedflag & TS_CSTART))
 844                 p->p_lwprcnt++;
 845         t->t_schedflag |= (TS_CSTART | TS_CREATE);
 846         setrun_locked(t);
 847         thread_unlock(t);
 848 }
 849 
 850 /*
 851  * Copy an LWP's active templates, and clear the latest contracts.
 852  */
 853 void
 854 lwp_ctmpl_copy(klwp_t *dst, klwp_t *src)
 855 {
 856         int i;
 857 
 858         for (i = 0; i < ct_ntypes; i++) {
 859                 ct_template_t *tmpl = src->lwp_ct_active[i];
 860 
 861                 /*
 862                  * If the process contract template is setup to be preserved
 863                  * across exec, then if we're forking, perform an implicit
 864                  * template_clear now. This ensures that future children of
 865                  * this child will remain in the same contract unless they're
 866                  * explicitly setup differently. We know we're forking if the
 867                  * two LWPs belong to different processes.
 868                  */
 869                 if (i == CTT_PROCESS && tmpl != NULL) {
 870                         ctmpl_process_t *ctp = tmpl->ctmpl_data;
 871 
 872                         if (dst->lwp_procp != src->lwp_procp &&
 873                             (ctp->ctp_params & CT_PR_KEEP_EXEC) != 0)
 874                                 tmpl = NULL;
 875                 }
 876 
 877                 dst->lwp_ct_active[i] = ctmpl_dup(tmpl);
 878                 dst->lwp_ct_latest[i] = NULL;
 879 
 880         }
 881 }
 882 
 883 /*
 884  * Clear an LWP's contract template state.
 885  */
 886 void
 887 lwp_ctmpl_clear(klwp_t *lwp, boolean_t is_exec)
 888 {
 889         ct_template_t *tmpl;
 890         int i;
 891 
 892         for (i = 0; i < ct_ntypes; i++) {





 893                 if (lwp->lwp_ct_latest[i] != NULL) {
 894                         contract_rele(lwp->lwp_ct_latest[i]);
 895                         lwp->lwp_ct_latest[i] = NULL;
 896                 }
 897 
 898                 if ((tmpl = lwp->lwp_ct_active[i]) != NULL) {
 899                         /*
 900                          * If we're exec-ing a new program and the process
 901                          * contract template is setup to be preserved across
 902                          * exec, then don't clear it.
 903                          */
 904                         if (is_exec && i == CTT_PROCESS) {
 905                                 ctmpl_process_t *ctp = tmpl->ctmpl_data;
 906 
 907                                 if ((ctp->ctp_params & CT_PR_KEEP_EXEC) != 0)
 908                                         continue;
 909                         }
 910 
 911                         ctmpl_free(tmpl);
 912                         lwp->lwp_ct_active[i] = NULL;
 913                 }
 914         }
 915 }
 916 
 917 /*
 918  * Individual lwp exit.
 919  * If this is the last lwp, exit the whole process.
 920  */
 921 void
 922 lwp_exit(void)
 923 {
 924         kthread_t *t = curthread;
 925         klwp_t *lwp = ttolwp(t);
 926         proc_t *p = ttoproc(t);
 927 
 928         ASSERT(MUTEX_HELD(&p->p_lock));
 929 
 930         mutex_exit(&p->p_lock);
 931 
 932 #if defined(__sparc)
 933         /*
 934          * Ensure that the user stack is fully abandoned..
 935          */
 936         trash_user_windows();
 937 #endif
 938 
 939         tsd_exit();                     /* free thread specific data */
 940 
 941         kcpc_passivate();               /* Clean up performance counter state */
 942 
 943         pollcleanup();
 944 
 945         if (t->t_door)
 946                 door_slam();
 947 
 948         if (t->t_schedctl != NULL)
 949                 schedctl_lwp_cleanup(t);
 950 
 951         if (t->t_upimutex != NULL)
 952                 upimutex_cleanup();
 953 







 954         lwp_pcb_exit();
 955 
 956         mutex_enter(&p->p_lock);
 957         lwp_cleanup();
 958 
 959         /*
 960          * When this process is dumping core, its lwps are held here
 961          * until the core dump is finished. Then exitlwps() is called
 962          * again to release these lwps so that they can finish exiting.
 963          */
 964         if (p->p_flag & SCOREDUMP)
 965                 stop(PR_SUSPENDED, SUSPEND_NORMAL);
 966 
 967         /*
 968          * Block the process against /proc now that we have really acquired
 969          * p->p_lock (to decrement p_lwpcnt and manipulate p_tlist at least).
 970          */
 971         prbarrier(p);
 972 
 973         /*


 977             p->p_lwpcnt == p->p_lwpdaemon + 1) {
 978                 mutex_exit(&p->p_lock);
 979                 if (proc_exit(CLD_EXITED, 0) == 0) {
 980                         /* Restarting init. */
 981                         return;
 982                 }
 983 
 984                 /*
 985                  * proc_exit() returns a non-zero value when some other
 986                  * lwp got there first.  We just have to continue in
 987                  * lwp_exit().
 988                  */
 989                 mutex_enter(&p->p_lock);
 990                 ASSERT(curproc->p_flag & SEXITLWPS);
 991                 prbarrier(p);
 992         }
 993 
 994         DTRACE_PROC(lwp__exit);
 995 
 996         /*
 997          * Perform any brand specific exit processing, then release any
 998          * brand data associated with the lwp
 999          */
1000         if (PROC_IS_BRANDED(p)) {
1001                 mutex_exit(&p->p_lock);
1002                 BROP(p)->b_lwpexit(lwp);
1003                 BROP(p)->b_freelwp(lwp);
1004                 mutex_enter(&p->p_lock);
1005                 prbarrier(p);
1006         }
1007 
1008         /*
1009          * If the lwp is a detached lwp or if the process is exiting,
1010          * remove (lwp_hash_out()) the lwp from the lwp directory.
1011          * Otherwise null out the lwp's le_thread pointer in the lwp
1012          * directory so that other threads will see it as a zombie lwp.
1013          */
1014         prlwpexit(t);           /* notify /proc */
1015         if (!(t->t_proc_flag & TP_TWAIT) || (p->p_flag & SEXITLWPS))
1016                 lwp_hash_out(p, t->t_tid);
1017         else {
1018                 ASSERT(!(t->t_proc_flag & TP_DAEMON));
1019                 p->p_lwpdir[t->t_dslot].ld_entry->le_thread = NULL;
1020                 p->p_zombcnt++;
1021                 cv_broadcast(&p->p_lwpexit);
1022         }
1023         if (t->t_proc_flag & TP_DAEMON) {
1024                 p->p_lwpdaemon--;
1025                 t->t_proc_flag &= ~TP_DAEMON;
1026         }
1027         t->t_proc_flag &= ~TP_TWAIT;
1028 


1149         /*
1150          * If this is the /proc agent lwp that is exiting, readjust p_lwpid
1151          * so it appears that the agent never existed, and clear p_agenttp.
1152          */
1153         if (t == p->p_agenttp) {
1154                 ASSERT(t->t_tid == p->p_lwpid);
1155                 p->p_lwpid--;
1156                 p->p_agenttp = NULL;
1157         }
1158 
1159         /*
1160          * Do lgroup bookkeeping to account for thread exiting.
1161          */
1162         kpreempt_disable();
1163         lgrp_move_thread(t, NULL, 1);
1164         if (t->t_tid == 1) {
1165                 p->p_t1_lgrpid = LGRP_NONE;
1166         }
1167         kpreempt_enable();
1168 
1169         lwp_ctmpl_clear(ttolwp(t), B_FALSE);
1170 }
1171 
1172 int
1173 lwp_suspend(kthread_t *t)
1174 {
1175         int tid;
1176         proc_t *p = ttoproc(t);
1177 
1178         ASSERT(MUTEX_HELD(&p->p_lock));
1179 
1180         /*
1181          * Set the thread's TP_HOLDLWP flag so it will stop in holdlwp().
1182          * If an lwp is stopping itself, there is no need to wait.
1183          */
1184 top:
1185         t->t_proc_flag |= TP_HOLDLWP;
1186         if (t == curthread) {
1187                 t->t_sig_check = 1;
1188         } else {
1189                 /*