4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
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.
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]
23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
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>
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>
47 #include <sys/vtrace.h>
49 #include <vm/seg_kp.h>
50 #include <sys/debug.h>
52 #include <sys/schedctl.h>
54 #include <sys/copyops.h>
55 #include <sys/lwp_upimutex_impl.h>
56 #include <sys/cpupart.h>
59 #include <sys/contract_impl.h>
60 #include <sys/cpc_impl.h>
62 #include <sys/cmn_err.h>
63 #include <sys/brand.h>
64 #include <sys/cyclic.h>
67 /* hash function for the lwpid hash table, p->p_tidhash[] */
68 #define TIDHASH(tid, hash_sz) ((tid) & ((hash_sz) - 1))
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
*);
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.
79 lwp_kernel_create(proc_t
*p
, void (*proc
)(), void *arg
, int state
, pri_t pri
)
83 VERIFY((p
->p_flag
& SSYS
) != 0);
85 lwp
= lwp_create(proc
, arg
, 0, p
, state
, pri
, &t0
.t_hold
, syscid
, 0);
93 * Create a thread that appears to be stopped at sys_rtt.
96 lwp_create(void (*proc
)(), caddr_t arg
, size_t len
, proc_t
*p
,
97 int state
, int pri
, const k_sigset_t
*smask
, int cid
, id_t lwpid
)
102 cpupart_t
*oldpart
= NULL
;
104 caddr_t lwpdata
= NULL
;
105 processorid_t binding
;
107 kproject_t
*oldkpj
, *newkpj
;
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
;
118 boolean_t branded
= 0;
119 struct ctxop
*ctx
= NULL
;
121 ASSERT(cid
!= sysdccid
); /* system threads must start in SYS */
123 ASSERT(p
!= &p0
); /* No new LWPs in p0. */
125 mutex_enter(&p
->p_lock
);
126 mutex_enter(&p
->p_zone
->zone_nlwps_lock
);
128 * don't enforce rctl limits on system processes
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
,
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)
141 if (p
->p_zone
->zone_nlwps
>= p
->p_zone
->zone_nlwps_ctl
)
142 if (rctl_test(rc_zone_nlwps
, p
->p_zone
->zone_rctls
, p
,
147 mutex_exit(&p
->p_zone
->zone_nlwps_lock
);
148 mutex_exit(&p
->p_lock
);
149 atomic_inc_32(&p
->p_zone
->zone_ffcap
);
152 p
->p_task
->tk_nlwps
++;
153 p
->p_task
->tk_proj
->kpj_nlwps
++;
154 p
->p_zone
->zone_nlwps
++;
155 mutex_exit(&p
->p_zone
->zone_nlwps_lock
);
156 mutex_exit(&p
->p_lock
);
158 curlwp
= ttolwp(curthread
);
159 if (curlwp
== NULL
|| (stksize
= curlwp
->lwp_childstksz
) == 0)
160 stksize
= lwp_default_stksize
;
162 if (CLASS_KERNEL(cid
)) {
164 * Since we are creating an LWP in an SSYS process, we do not
165 * inherit anything from the current thread's LWP. We set
166 * stksize and lwpdata to 0 in order to let thread_create()
167 * allocate a regular kernel thread stack for this thread.
173 } else if (stksize
== lwp_default_stksize
) {
175 * Try to reuse an <lwp,stack> from the LWP deathrow.
177 if (lwp_reapcnt
> 0) {
178 mutex_enter(&reaplock
);
179 if ((t
= lwp_deathrow
) != NULL
) {
181 lwp_deathrow
= t
->t_forw
;
189 reapq_move_lq_to_tq(t
);
191 mutex_exit(&reaplock
);
199 if (lwpdata
== NULL
&&
200 (lwpdata
= (caddr_t
)segkp_cache_get(segkp_lwp
)) == NULL
) {
201 mutex_enter(&p
->p_lock
);
202 mutex_enter(&p
->p_zone
->zone_nlwps_lock
);
203 p
->p_task
->tk_nlwps
--;
204 p
->p_task
->tk_proj
->kpj_nlwps
--;
205 p
->p_zone
->zone_nlwps
--;
206 mutex_exit(&p
->p_zone
->zone_nlwps_lock
);
207 mutex_exit(&p
->p_lock
);
208 atomic_inc_32(&p
->p_zone
->zone_ffnomem
);
212 stksize
= roundup(stksize
, PAGESIZE
);
213 if ((lwpdata
= (caddr_t
)segkp_get(segkp
, stksize
,
214 (KPD_NOWAIT
| KPD_HASREDZONE
| KPD_LOCKED
))) == NULL
) {
215 mutex_enter(&p
->p_lock
);
216 mutex_enter(&p
->p_zone
->zone_nlwps_lock
);
217 p
->p_task
->tk_nlwps
--;
218 p
->p_task
->tk_proj
->kpj_nlwps
--;
219 p
->p_zone
->zone_nlwps
--;
220 mutex_exit(&p
->p_zone
->zone_nlwps_lock
);
221 mutex_exit(&p
->p_lock
);
222 atomic_inc_32(&p
->p_zone
->zone_ffnomem
);
228 * Create a thread, initializing the stack pointer
230 t
= thread_create(lwpdata
, stksize
, NULL
, NULL
, 0, p
, TS_STOPPED
, pri
);
233 * If a non-NULL stack base is passed in, thread_create() assumes
234 * that the stack might be statically allocated (as opposed to being
235 * allocated from segkp), and so it does not set t_swap. Since
236 * the lwpdata was allocated from segkp, we must set t_swap to point
239 * This would be less confusing if t_swap had a better name; it really
240 * indicates that the stack is allocated from segkp, regardless of
241 * whether or not it is swappable.
243 if (lwpdata
!= NULL
) {
244 ASSERT(!CLASS_KERNEL(cid
));
245 ASSERT(t
->t_swap
== NULL
);
246 t
->t_swap
= lwpdata
; /* Start of page-able data */
250 * If the stack and lwp can be reused, mark the thread as such.
251 * When we get to reapq_add() from resume_from_zombie(), these
252 * threads will go onto lwp_deathrow instead of thread_deathrow.
254 if (!CLASS_KERNEL(cid
) && stksize
== lwp_default_stksize
)
255 t
->t_flag
|= T_LWPREUSE
;
258 lwp
= kmem_cache_alloc(lwp_cache
, KM_SLEEP
);
259 bzero(lwp
, sizeof (*lwp
));
265 lwp
->lwp_sigaltstack
.ss_flags
= SS_DISABLE
;
266 if (curlwp
!= NULL
&& curlwp
->lwp_childstksz
!= 0)
267 lwp
->lwp_childstksz
= curlwp
->lwp_childstksz
;
269 t
->t_stk
= lwp_stk_init(lwp
, t
->t_stk
);
270 thread_load(t
, proc
, arg
, len
);
273 * Allocate the SIGPROF buffer if ITIMER_REALPROF is in effect.
275 if (p
->p_rprof_cyclic
!= CYCLIC_NONE
)
276 t
->t_rprof
= kmem_zalloc(sizeof (struct rprof
), KM_SLEEP
);
279 (void) CL_ALLOC(&bufp
, cid
, KM_SLEEP
);
282 * Allocate an lwp directory entry for the new lwp.
284 lep
= kmem_zalloc(sizeof (*lep
), KM_SLEEP
);
286 mutex_enter(&p
->p_lock
);
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:
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.
307 * Rather, it calls lwp_hash_lookup_and_lock() which
308 * acquires only the individual hash bucket lock, taking
309 * care to deal with reallocation of the hash table
310 * during the time it takes to acquire the lock.
312 * This is sufficient to protect the integrity of the
313 * hash table, but it requires us to acquire all of the
314 * old hash bucket locks before growing the hash table
315 * and to release them afterwards. It also requires us
316 * not to free the old hash table because some thread
317 * in lwp_hash_lookup_and_lock() might still be trying
318 * to acquire the old bucket lock.
320 * So we adopt the tactic of keeping all of the retired
321 * hash tables on a linked list, so they can be safely
322 * freed when the process exits or execs.
324 * Because the hash table grows in powers of two, the
325 * total size of all of the hash tables will be slightly
326 * less than twice the size of the largest hash table.
328 while (p
->p_lwpfree
== NULL
) {
329 uint_t dirsz
= p
->p_lwpdir_sz
;
336 mutex_exit(&p
->p_lock
);
339 * Prepare to remember the old p_tidhash for later
340 * kmem_free()ing when the process exits or execs.
342 if (ret_tidhash
== NULL
)
343 ret_tidhash
= kmem_zalloc(sizeof (ret_tidhash_t
),
346 kmem_free(old_dir
, old_dirsz
* sizeof (*old_dir
));
347 if (old_hash
!= NULL
)
348 kmem_free(old_hash
, old_hashsz
* sizeof (*old_hash
));
350 new_dirsz
= 2 * dirsz
+ 2;
351 new_dir
= kmem_zalloc(new_dirsz
* sizeof (lwpdir_t
), KM_SLEEP
);
352 for (ldp
= new_dir
, i
= 1; i
< new_dirsz
; i
++, ldp
++)
353 ldp
->ld_next
= ldp
+ 1;
354 new_hashsz
= (new_dirsz
+ 2) / 2;
355 new_hash
= kmem_zalloc(new_hashsz
* sizeof (tidhash_t
),
358 mutex_enter(&p
->p_lock
);
362 if (dirsz
!= p
->p_lwpdir_sz
|| p
->p_lwpfree
!= NULL
) {
364 * Someone else beat us to it or some lwp exited.
365 * Set up to free our memory and take a lap.
368 old_dirsz
= new_dirsz
;
370 old_hashsz
= new_hashsz
;
373 * For the benefit of lwp_hash_lookup_and_lock(),
374 * called from lwp_unpark(), which searches the
375 * tid hash table without acquiring p->p_lock,
376 * we must acquire all of the tid hash table
377 * locks before replacing p->p_tidhash.
379 old_hash
= p
->p_tidhash
;
380 old_hashsz
= p
->p_tidhash_sz
;
381 for (i
= 0; i
< old_hashsz
; i
++) {
382 mutex_enter(&old_hash
[i
].th_lock
);
383 mutex_enter(&new_hash
[i
].th_lock
);
387 * We simply hash in all of the old directory entries.
388 * This works because the old directory has no empty
389 * slots and the new hash table starts out empty.
390 * This reproduces the original directory ordering
391 * (required for /proc directory semantics).
393 old_dir
= p
->p_lwpdir
;
394 old_dirsz
= p
->p_lwpdir_sz
;
395 p
->p_lwpdir
= new_dir
;
396 p
->p_lwpfree
= new_dir
;
397 p
->p_lwpdir_sz
= new_dirsz
;
398 for (ldp
= old_dir
, i
= 0; i
< old_dirsz
; i
++, ldp
++)
399 lwp_hash_in(p
, ldp
->ld_entry
,
400 new_hash
, new_hashsz
, 0);
403 * Remember the old hash table along with all
404 * of the previously-remembered hash tables.
405 * We will free them at process exit or exec.
407 ret_tidhash
->rth_tidhash
= old_hash
;
408 ret_tidhash
->rth_tidhash_sz
= old_hashsz
;
409 ret_tidhash
->rth_next
= p
->p_ret_tidhash
;
410 p
->p_ret_tidhash
= ret_tidhash
;
413 * Now establish the new tid hash table.
414 * As soon as we assign p->p_tidhash,
415 * code in lwp_unpark() can start using it.
418 p
->p_tidhash
= new_hash
;
421 * It is necessary that p_tidhash reach global
422 * visibility before p_tidhash_sz. Otherwise,
423 * code in lwp_hash_lookup_and_lock() could
424 * index into the old p_tidhash using the new
425 * p_tidhash_sz and thereby access invalid data.
428 p
->p_tidhash_sz
= new_hashsz
;
431 * Release the locks; allow lwp_unpark() to carry on.
433 for (i
= 0; i
< old_hashsz
; i
++) {
434 mutex_exit(&old_hash
[i
].th_lock
);
435 mutex_exit(&new_hash
[i
].th_lock
);
439 * Avoid freeing these objects below.
448 * Block the process against /proc while we manipulate p->p_tlist,
449 * unless lwp_create() was called by /proc for the PCAGENT operation.
450 * We want to do this early enough so that we don't drop p->p_lock
451 * until the thread is put on the p->p_tlist.
456 * If the current lwp has been requested to stop, do so now.
457 * Otherwise we have a race condition between /proc attempting
458 * to stop the process and this thread creating a new lwp
459 * that was not seen when the /proc PCSTOP request was issued.
460 * We rely on stop() to call prbarrier(p) before returning.
462 while ((curthread
->t_proc_flag
& TP_PRSTOP
) &&
463 !ttolwp(curthread
)->lwp_nostop
) {
465 * We called pool_barrier_enter() before calling
466 * here to lwp_create(). We have to call
467 * pool_barrier_exit() before stopping.
471 stop(PR_REQUESTED
, 0);
473 * And we have to repeat the call to
474 * pool_barrier_enter after stopping.
476 pool_barrier_enter();
481 * If process is exiting, there could be a race between
482 * the agent lwp creation and the new lwp currently being
483 * created. So to prevent this race lwp creation is failed
484 * if the process is exiting.
486 if (p
->p_flag
& (SEXITLWPS
|SKILLED
)) {
492 * Since we might have dropped p->p_lock, the
493 * lwp directory free list might have changed.
495 if (p
->p_lwpfree
== NULL
)
499 kpreempt_disable(); /* can't grab cpu_lock here */
502 * Inherit processor and processor set bindings from curthread.
504 * For kernel LWPs, we do not inherit processor set bindings at
505 * process creation time (i.e. when p != curproc). After the
506 * kernel process is created, any subsequent LWPs must be created
507 * by threads in the kernel process, at which point we *will*
508 * inherit processor set bindings.
510 if (CLASS_KERNEL(cid
) && p
!= curproc
) {
511 t
->t_bind_cpu
= binding
= PBIND_NONE
;
512 t
->t_cpupart
= oldpart
= &cp_default
;
513 t
->t_bind_pset
= PS_NONE
;
514 t
->t_bindflag
= (uchar_t
)default_binding_mode
;
516 binding
= curthread
->t_bind_cpu
;
517 t
->t_bind_cpu
= binding
;
518 oldpart
= t
->t_cpupart
;
519 t
->t_cpupart
= curthread
->t_cpupart
;
520 t
->t_bind_pset
= curthread
->t_bind_pset
;
521 t
->t_bindflag
= curthread
->t_bindflag
|
522 (uchar_t
)default_binding_mode
;
526 * thread_create() initializes this thread's home lgroup to the root.
527 * Choose a more suitable lgroup, since this thread is associated
530 ASSERT(oldpart
!= NULL
);
531 if (binding
!= PBIND_NONE
&& t
->t_affinitycnt
== 0) {
532 t
->t_bound_cpu
= cpu
[binding
];
533 if (t
->t_lpl
!= t
->t_bound_cpu
->cpu_lpl
)
534 lgrp_move_thread(t
, t
->t_bound_cpu
->cpu_lpl
, 1);
535 } else if (CLASS_KERNEL(cid
)) {
537 * Kernel threads are always in the root lgrp.
540 &t
->t_cpupart
->cp_lgrploads
[LGRP_ROOTID
], 1);
542 lgrp_move_thread(t
, lgrp_choose(t
, t
->t_cpupart
), 1);
548 * make sure lpl points to our own partition
550 ASSERT(t
->t_lpl
>= t
->t_cpupart
->cp_lgrploads
);
551 ASSERT(t
->t_lpl
< t
->t_cpupart
->cp_lgrploads
+
552 t
->t_cpupart
->cp_nlgrploads
);
555 * It is safe to point the thread to the new project without holding it
556 * since we're holding the target process' p_lock here and therefore
557 * we're guaranteed that it will not move to another project.
559 newkpj
= p
->p_task
->tk_proj
;
561 if (newkpj
!= oldkpj
) {
563 (void) project_hold(newkpj
);
564 project_rele(oldkpj
);
567 if (cid
!= NOCLASS
) {
569 * If the lwp is being created in the current process
570 * and matches the current thread's scheduling class,
571 * we should propagate the current thread's scheduling
572 * parameters by calling CL_FORK. Otherwise just use
573 * the defaults by calling CL_ENTERCLASS.
575 if (p
!= curproc
|| curthread
->t_cid
!= cid
) {
576 err
= CL_ENTERCLASS(t
, cid
, NULL
, NULL
, bufp
);
577 t
->t_pri
= pri
; /* CL_ENTERCLASS may have changed it */
579 * We don't call schedctl_set_cidpri(t) here
580 * because the schedctl data is not yet set
581 * up for the newly-created lwp.
584 t
->t_clfuncs
= &(sclass
[cid
].cl_funcs
->thread
);
585 err
= CL_FORK(curthread
, t
, bufp
);
589 atomic_inc_32(&p
->p_zone
->zone_ffmisc
);
597 * If we were given an lwpid then use it, else allocate one.
603 * lwp/thread id 0 is never valid; reserved for special checks.
604 * lwp/thread id 1 is reserved for the main thread.
605 * Start again at 2 when INT_MAX has been reached
606 * (id_t is a signed 32-bit integer).
608 id_t prev_id
= p
->p_lwpid
; /* last allocated tid */
610 do { /* avoid lwpid duplication */
611 if (p
->p_lwpid
== INT_MAX
) {
612 p
->p_flag
|= SLWPWRAP
;
615 if ((t
->t_tid
= ++p
->p_lwpid
) == prev_id
) {
617 * All lwpids are allocated; fail the request.
620 atomic_inc_32(&p
->p_zone
->zone_ffnoproc
);
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.
628 if ((p
->p_flag
& SLWPWRAP
) == 0)
630 } while (lwp_hash_lookup(p
, t
->t_tid
) != NULL
);
634 * If this is a branded process, let the brand do any necessary lwp
637 if (PROC_IS_BRANDED(p
)) {
638 if (BROP(p
)->b_initlwp(lwp
)) {
640 atomic_inc_32(&p
->p_zone
->zone_ffmisc
);
648 ASSERT(t
->t_lpl
!= NULL
);
649 p
->p_t1_lgrpid
= t
->t_lpl
->lpl_lgrpid
;
651 if (p
->p_tr_lgrpid
!= LGRP_NONE
&&
652 p
->p_tr_lgrpid
!= p
->p_t1_lgrpid
) {
653 lgrp_update_trthr_migrations(1);
661 * Turn microstate accounting on for thread if on for process.
663 if (p
->p_flag
& SMSACCT
)
664 t
->t_proc_flag
|= TP_MSACCT
;
667 * If the process has watchpoints, mark the new thread as such.
669 if (pr_watch_active(p
))
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.
678 * When the caller is finished with the newly-created lwp,
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.
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 */
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.
699 * Insert the new thread into the list of all threads.
701 if ((tx
= p
->p_tlist
) == NULL
) {
707 t
->t_back
= tx
->t_back
;
708 tx
->t_back
->t_forw
= t
;
713 * Insert the new lwp into an lwp directory slot position
714 * and into the lwpid hash table.
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);
721 if (state
== TS_RUN
) {
723 * We set the new lwp running immediately.
725 t
->t_proc_flag
&= ~TP_HOLDLWP
;
731 if (CLASS_KERNEL(cid
)) {
733 * This should only happen if a system process runs
734 * out of lwpids, which shouldn't occur.
736 panic("Failed to create a system LWP");
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.
744 lgrp_move_thread(t
, NULL
, 1);
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
)
757 BROP(p
)->b_freelwp(lwp
);
759 mutex_exit(&p
->p_lock
);
760 t
->t_state
= TS_FREE
;
764 * We need to remove t from the list of all threads
765 * because thread_exit()/lwp_exit() isn't called on t.
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
);
774 kmem_free(lep
, sizeof (*lep
));
777 mutex_exit(&p
->p_lock
);
781 kmem_free(old_dir
, old_dirsz
* sizeof (*old_dir
));
782 if (old_hash
!= NULL
)
783 kmem_free(old_hash
, old_hashsz
* sizeof (*old_hash
));
784 if (ret_tidhash
!= NULL
)
785 kmem_free(ret_tidhash
, sizeof (ret_tidhash_t
));
787 DTRACE_PROC1(lwp__create
, kthread_t
*, t
);
792 * lwp_create_done() is called by the caller of lwp_create() to set the
793 * newly-created lwp running after the caller has finished manipulating it.
796 lwp_create_done(kthread_t
*t
)
798 proc_t
*p
= ttoproc(t
);
800 ASSERT(MUTEX_HELD(&p
->p_lock
));
803 * We set the TS_CREATE and TS_CSTART flags and call setrun_locked().
804 * (The absence of the TS_CREATE flag prevents the lwp from running
805 * until we are finished with it, even if lwp_continue() is called on
806 * it by some other lwp in the process or elsewhere in the kernel.)
809 ASSERT(t
->t_state
== TS_STOPPED
&& !(t
->t_schedflag
& TS_CREATE
));
811 * If TS_CSTART is set, lwp_continue(t) has been called and
812 * has already incremented p_lwprcnt; avoid doing this twice.
814 if (!(t
->t_schedflag
& TS_CSTART
))
816 t
->t_schedflag
|= (TS_CSTART
| TS_CREATE
);
822 * Copy an LWP's active templates, and clear the latest contracts.
825 lwp_ctmpl_copy(klwp_t
*dst
, klwp_t
*src
)
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
;
836 * Clear an LWP's contract template state.
839 lwp_ctmpl_clear(klwp_t
*lwp
)
844 for (i
= 0; i
< ct_ntypes
; i
++) {
845 if ((tmpl
= lwp
->lwp_ct_active
[i
]) != NULL
) {
847 lwp
->lwp_ct_active
[i
] = NULL
;
850 if (lwp
->lwp_ct_latest
[i
] != NULL
) {
851 contract_rele(lwp
->lwp_ct_latest
[i
]);
852 lwp
->lwp_ct_latest
[i
] = NULL
;
858 * Individual lwp exit.
859 * If this is the last lwp, exit the whole process.
864 kthread_t
*t
= curthread
;
865 klwp_t
*lwp
= ttolwp(t
);
866 proc_t
*p
= ttoproc(t
);
868 ASSERT(MUTEX_HELD(&p
->p_lock
));
870 mutex_exit(&p
->p_lock
);
873 tsd_exit(); /* free thread specific data */
875 kcpc_passivate(); /* Clean up performance counter state */
882 if (t
->t_schedctl
!= NULL
)
883 schedctl_lwp_cleanup(t
);
885 if (t
->t_upimutex
!= NULL
)
889 * Perform any brand specific exit processing, then release any
890 * brand data associated with the lwp
892 if (PROC_IS_BRANDED(p
))
893 BROP(p
)->b_lwpexit(lwp
);
897 mutex_enter(&p
->p_lock
);
901 * When this process is dumping core, its lwps are held here
902 * until the core dump is finished. Then exitlwps() is called
903 * again to release these lwps so that they can finish exiting.
905 if (p
->p_flag
& SCOREDUMP
)
906 stop(PR_SUSPENDED
, SUSPEND_NORMAL
);
909 * Block the process against /proc now that we have really acquired
910 * p->p_lock (to decrement p_lwpcnt and manipulate p_tlist at least).
915 * Call proc_exit() if this is the last non-daemon lwp in the process.
917 if (!(t
->t_proc_flag
& TP_DAEMON
) &&
918 p
->p_lwpcnt
== p
->p_lwpdaemon
+ 1) {
919 mutex_exit(&p
->p_lock
);
920 if (proc_exit(CLD_EXITED
, 0) == 0) {
921 /* Restarting init. */
926 * proc_exit() returns a non-zero value when some other
927 * lwp got there first. We just have to continue in
930 mutex_enter(&p
->p_lock
);
931 ASSERT(curproc
->p_flag
& SEXITLWPS
);
935 DTRACE_PROC(lwp__exit
);
938 * If the lwp is a detached lwp or if the process is exiting,
939 * remove (lwp_hash_out()) the lwp from the lwp directory.
940 * Otherwise null out the lwp's le_thread pointer in the lwp
941 * directory so that other threads will see it as a zombie lwp.
943 prlwpexit(t
); /* notify /proc */
944 if (!(t
->t_proc_flag
& TP_TWAIT
) || (p
->p_flag
& SEXITLWPS
))
945 lwp_hash_out(p
, t
->t_tid
);
947 ASSERT(!(t
->t_proc_flag
& TP_DAEMON
));
948 p
->p_lwpdir
[t
->t_dslot
].ld_entry
->le_thread
= NULL
;
950 cv_broadcast(&p
->p_lwpexit
);
952 if (t
->t_proc_flag
& TP_DAEMON
) {
954 t
->t_proc_flag
&= ~TP_DAEMON
;
956 t
->t_proc_flag
&= ~TP_TWAIT
;
959 * Maintain accurate lwp count for task.max-lwps resource control.
961 mutex_enter(&p
->p_zone
->zone_nlwps_lock
);
962 p
->p_task
->tk_nlwps
--;
963 p
->p_task
->tk_proj
->kpj_nlwps
--;
964 p
->p_zone
->zone_nlwps
--;
965 mutex_exit(&p
->p_zone
->zone_nlwps_lock
);
967 CL_EXIT(t
); /* tell the scheduler that t is exiting */
968 ASSERT(p
->p_lwpcnt
!= 0);
972 * If all remaining non-daemon lwps are waiting in lwp_wait(),
973 * wake them up so someone can return EDEADLK.
974 * (See the block comment preceeding lwp_wait().)
976 if (p
->p_lwpcnt
== p
->p_lwpdaemon
+ (p
->p_lwpwait
- p
->p_lwpdwait
))
977 cv_broadcast(&p
->p_lwpexit
);
979 t
->t_proc_flag
|= TP_LWPEXIT
;
988 t
->t_forw
->t_back
= t
->t_back
;
989 t
->t_back
->t_forw
= t
->t_forw
;
991 p
->p_tlist
= t
->t_forw
;
994 * Clean up the signal state.
996 if (t
->t_sigqueue
!= NULL
)
998 if (lwp
->lwp_curinfo
!= NULL
) {
999 siginfofree(lwp
->lwp_curinfo
);
1000 lwp
->lwp_curinfo
= NULL
;
1004 * If we have spymaster information (that is, if we're an agent LWP),
1007 if (lwp
->lwp_spymaster
!= NULL
) {
1008 kmem_free(lwp
->lwp_spymaster
, sizeof (psinfo_t
));
1009 lwp
->lwp_spymaster
= NULL
;
1015 * Terminated lwps are associated with process zero and are put onto
1016 * death-row by resume(). Avoid preemption after resetting t->t_procp.
1020 if (t
->t_ctx
!= NULL
)
1022 if (p
->p_pctx
!= NULL
)
1028 * Notify the HAT about the change of address space
1032 * When this is the last running lwp in this process and some lwp is
1033 * waiting for this condition to become true, or this thread was being
1034 * suspended, then the waiting lwp is awakened.
1036 * Also, if the process is exiting, we may have a thread waiting in
1037 * exitlwps() that needs to be notified.
1039 if (--p
->p_lwprcnt
== 0 || (t
->t_proc_flag
& TP_HOLDLWP
) ||
1040 (p
->p_flag
& SEXITLWPS
))
1041 cv_broadcast(&p
->p_holdlwps
);
1044 * Need to drop p_lock so we can reacquire pidlock.
1046 mutex_exit(&p
->p_lock
);
1047 mutex_enter(&pidlock
);
1049 ASSERT(t
!= t
->t_next
); /* t0 never exits */
1050 t
->t_next
->t_prev
= t
->t_prev
;
1051 t
->t_prev
->t_next
= t
->t_next
;
1052 cv_broadcast(&t
->t_joincv
); /* wake up anyone in thread_join */
1053 mutex_exit(&pidlock
);
1055 t
->t_state
= TS_ZOMB
;
1056 swtch_from_zombie();
1062 * Cleanup function for an exiting lwp.
1063 * Called both from lwp_exit() and from proc_exit().
1064 * p->p_lock is repeatedly released and grabbed in this function.
1069 kthread_t
*t
= curthread
;
1070 proc_t
*p
= ttoproc(t
);
1072 ASSERT(MUTEX_HELD(&p
->p_lock
));
1074 /* untimeout any lwp-bound realtime timers */
1075 if (p
->p_itimer
!= NULL
)
1079 * If this is the /proc agent lwp that is exiting, readjust p_lwpid
1080 * so it appears that the agent never existed, and clear p_agenttp.
1082 if (t
== p
->p_agenttp
) {
1083 ASSERT(t
->t_tid
== p
->p_lwpid
);
1085 p
->p_agenttp
= NULL
;
1089 * Do lgroup bookkeeping to account for thread exiting.
1092 lgrp_move_thread(t
, NULL
, 1);
1093 if (t
->t_tid
== 1) {
1094 p
->p_t1_lgrpid
= LGRP_NONE
;
1098 lwp_ctmpl_clear(ttolwp(t
));
1102 lwp_suspend(kthread_t
*t
)
1105 proc_t
*p
= ttoproc(t
);
1107 ASSERT(MUTEX_HELD(&p
->p_lock
));
1110 * Set the thread's TP_HOLDLWP flag so it will stop in holdlwp().
1111 * If an lwp is stopping itself, there is no need to wait.
1114 t
->t_proc_flag
|= TP_HOLDLWP
;
1115 if (t
== curthread
) {
1119 * Make sure the lwp stops promptly.
1124 * XXX Should use virtual stop like /proc does instead of
1125 * XXX waking the thread to get it to stop.
1127 if (ISWAKEABLE(t
) || ISWAITING(t
)) {
1129 } else if (t
->t_state
== TS_ONPROC
&& t
->t_cpu
!= CPU
) {
1130 poke_cpu(t
->t_cpu
->cpu_id
);
1133 tid
= t
->t_tid
; /* remember thread ID */
1135 * Wait for lwp to stop
1137 while (!SUSPENDED(t
)) {
1139 * Drop the thread lock before waiting and reacquire it
1140 * afterwards, so the thread can change its t_state
1146 * Check if aborted by exitlwps().
1148 if (p
->p_flag
& SEXITLWPS
)
1152 * Cooperate with jobcontrol signals and /proc stopping
1153 * by calling cv_wait_sig() to wait for the target
1154 * lwp to stop. Just using cv_wait() can lead to
1155 * deadlock because, if some other lwp has stopped
1156 * by either of these mechanisms, then p_lwprcnt will
1157 * never become zero if we do a cv_wait().
1159 if (!cv_wait_sig(&p
->p_holdlwps
, &p
->p_lock
))
1163 * Check to see if thread died while we were
1164 * waiting for it to suspend.
1166 if (idtot(p
, tid
) == NULL
)
1171 * If the TP_HOLDLWP flag went away, lwp_continue()
1172 * or vfork() must have been called while we were
1173 * waiting, so start over again.
1175 if ((t
->t_proc_flag
& TP_HOLDLWP
) == 0) {
1186 * continue a lwp that's been stopped by lwp_suspend().
1189 lwp_continue(kthread_t
*t
)
1191 proc_t
*p
= ttoproc(t
);
1192 int was_suspended
= t
->t_proc_flag
& TP_HOLDLWP
;
1194 ASSERT(MUTEX_HELD(&p
->p_lock
));
1196 t
->t_proc_flag
&= ~TP_HOLDLWP
;
1199 !(p
->p_flag
& (SHOLDFORK
| SHOLDFORK1
| SHOLDWATCH
))) {
1201 t
->t_schedflag
|= TS_CSTART
;
1206 * Wakeup anyone waiting for this thread to be suspended
1209 cv_broadcast(&p
->p_holdlwps
);
1213 * ********************************
1214 * Miscellaneous lwp routines *
1215 * ********************************
1218 * When a process is undergoing a forkall(), its p_flag is set to SHOLDFORK.
1219 * This will cause the process's lwps to stop at a hold point. A hold
1220 * point is where a kernel thread has a flat stack. This is at the
1221 * return from a system call and at the return from a user level trap.
1223 * When a process is undergoing a fork1() or vfork(), its p_flag is set to
1224 * SHOLDFORK1. This will cause the process's lwps to stop at a modified
1225 * hold point. The lwps in the process are not being cloned, so they
1226 * are held at the usual hold points and also within issig_forreal().
1227 * This has the side-effect that their system calls do not return
1230 * An lwp can also be held. This is identified by the TP_HOLDLWP flag on
1231 * the thread. The TP_HOLDLWP flag is set in lwp_suspend(), where the active
1232 * lwp is waiting for the target lwp to be stopped.
1237 proc_t
*p
= curproc
;
1238 kthread_t
*t
= curthread
;
1240 mutex_enter(&p
->p_lock
);
1242 * Don't terminate immediately if the process is dumping core.
1243 * Once the process has dumped core, all lwps are terminated.
1245 if (!(p
->p_flag
& SCOREDUMP
)) {
1246 if ((p
->p_flag
& SEXITLWPS
) || (t
->t_proc_flag
& TP_EXITLWP
))
1249 if (!(ISHOLD(p
)) && !(p
->p_flag
& (SHOLDFORK1
| SHOLDWATCH
))) {
1250 mutex_exit(&p
->p_lock
);
1254 * stop() decrements p->p_lwprcnt and cv_signal()s &p->p_holdlwps
1255 * when p->p_lwprcnt becomes zero.
1257 stop(PR_SUSPENDED
, SUSPEND_NORMAL
);
1258 if (p
->p_flag
& SEXITLWPS
)
1260 mutex_exit(&p
->p_lock
);
1264 * Have all lwps within the process hold at a point where they are
1265 * cloneable (SHOLDFORK) or just safe w.r.t. fork1 (SHOLDFORK1).
1268 holdlwps(int holdflag
)
1270 proc_t
*p
= curproc
;
1272 ASSERT(holdflag
== SHOLDFORK
|| holdflag
== SHOLDFORK1
);
1273 mutex_enter(&p
->p_lock
);
1274 schedctl_finish_sigblock(curthread
);
1276 while (p
->p_flag
& (SEXITLWPS
| SHOLDFORK
| SHOLDFORK1
| SHOLDWATCH
)) {
1278 * If another lwp is doing a forkall() or proc_exit(), bail out.
1280 if (p
->p_flag
& (SEXITLWPS
| SHOLDFORK
)) {
1281 mutex_exit(&p
->p_lock
);
1285 * Another lwp is doing a fork1() or is undergoing
1286 * watchpoint activity. We hold here for it to complete.
1288 stop(PR_SUSPENDED
, SUSPEND_NORMAL
);
1290 p
->p_flag
|= holdflag
;
1294 * Wait for the process to become quiescent (p->p_lwprcnt == 0).
1296 while (p
->p_lwprcnt
> 0) {
1298 * Check if aborted by exitlwps().
1299 * Also check if SHOLDWATCH is set; it takes precedence.
1301 if (p
->p_flag
& (SEXITLWPS
| SHOLDWATCH
)) {
1303 p
->p_flag
&= ~holdflag
;
1304 cv_broadcast(&p
->p_holdlwps
);
1308 * Cooperate with jobcontrol signals and /proc stopping.
1309 * If some other lwp has stopped by either of these
1310 * mechanisms, then p_lwprcnt will never become zero
1311 * and the process will appear deadlocked unless we
1312 * stop here in sympathy with the other lwp before
1313 * doing the cv_wait() below.
1315 * If the other lwp stops after we do the cv_wait(), it
1316 * will wake us up to loop around and do the sympathy stop.
1318 * Since stop() drops p->p_lock, we must start from
1319 * the top again on returning from stop().
1321 if (p
->p_stopsig
| (curthread
->t_proc_flag
& TP_PRSTOP
)) {
1322 int whystop
= p
->p_stopsig
? PR_JOBCONTROL
:
1325 p
->p_flag
&= ~holdflag
;
1326 stop(whystop
, p
->p_stopsig
);
1329 cv_wait(&p
->p_holdlwps
, &p
->p_lock
);
1332 p
->p_flag
&= ~holdflag
;
1333 mutex_exit(&p
->p_lock
);
1338 * See comments for holdwatch(), below.
1341 holdcheck(int clearflags
)
1343 proc_t
*p
= curproc
;
1346 * If we are trying to exit, that takes precedence over anything else.
1348 if (p
->p_flag
& SEXITLWPS
) {
1350 p
->p_flag
&= ~clearflags
;
1355 * If another thread is calling fork1(), stop the current thread so the
1356 * other can complete.
1358 if (p
->p_flag
& SHOLDFORK1
) {
1360 stop(PR_SUSPENDED
, SUSPEND_NORMAL
);
1361 if (p
->p_flag
& SEXITLWPS
) {
1362 p
->p_flag
&= ~clearflags
;
1369 * If another thread is calling fork(), then indicate we are doing
1370 * watchpoint activity. This will cause holdlwps() above to stop the
1371 * forking thread, at which point we can continue with watchpoint
1374 if (p
->p_flag
& SHOLDFORK
) {
1376 while (p
->p_flag
& SHOLDFORK
) {
1377 p
->p_flag
|= SHOLDWATCH
;
1378 cv_broadcast(&p
->p_holdlwps
);
1379 cv_wait(&p
->p_holdlwps
, &p
->p_lock
);
1380 p
->p_flag
&= ~SHOLDWATCH
;
1389 * Stop all lwps within the process, holding themselves in the kernel while the
1390 * active lwp undergoes watchpoint activity. This is more complicated than
1391 * expected because stop() relies on calling holdwatch() in order to copyin data
1392 * from the user's address space. A double barrier is used to prevent an
1395 * o The first thread into holdwatch() is the 'master' thread and does
1398 * - Sets SHOLDWATCH on the current process
1399 * - Sets TP_WATCHSTOP on the current thread
1400 * - Waits for all threads to be either stopped or have
1402 * - Sets the SWATCHOK flag on the process
1403 * - Unsets TP_WATCHSTOP
1404 * - Waits for the other threads to completely stop
1407 * o If SHOLDWATCH is already set when we enter this function, then another
1408 * thread is already trying to stop this thread. This 'slave' thread
1409 * does the following:
1411 * - Sets TP_WATCHSTOP on the current thread
1412 * - Waits for SWATCHOK flag to be set
1415 * o If SWATCHOK is set on the process, then this function immediately
1416 * returns, as we must have been called via stop().
1418 * In addition, there are other flags that take precedence over SHOLDWATCH:
1420 * o If SEXITLWPS is set, exit immediately.
1422 * o If SHOLDFORK1 is set, wait for fork1() to complete.
1424 * o If SHOLDFORK is set, then watchpoint activity takes precedence In this
1425 * case, set SHOLDWATCH, signalling the forking thread to stop first.
1427 * o If the process is being stopped via /proc (TP_PRSTOP is set), then we
1428 * stop the current thread.
1430 * Returns 0 if all threads have been quiesced. Returns non-zero if not all
1431 * threads were stopped, or the list of watched pages has changed.
1436 proc_t
*p
= curproc
;
1437 kthread_t
*t
= curthread
;
1440 mutex_enter(&p
->p_lock
);
1445 * Check for bail-out conditions as outlined above.
1447 if (holdcheck(0) != 0) {
1448 mutex_exit(&p
->p_lock
);
1452 if (!(p
->p_flag
& SHOLDWATCH
)) {
1454 * We are the master watchpoint thread. Set SHOLDWATCH and poke
1455 * the other threads.
1457 p
->p_flag
|= SHOLDWATCH
;
1461 * Wait for all threads to be stopped or have TP_WATCHSTOP set.
1463 while (pr_allstopped(p
, 1) > 0) {
1464 if (holdcheck(SHOLDWATCH
) != 0) {
1465 p
->p_flag
&= ~SHOLDWATCH
;
1466 mutex_exit(&p
->p_lock
);
1470 cv_wait(&p
->p_holdlwps
, &p
->p_lock
);
1474 * All threads are now stopped or in the process of stopping.
1475 * Set SWATCHOK and let them stop completely.
1477 p
->p_flag
|= SWATCHOK
;
1478 t
->t_proc_flag
&= ~TP_WATCHSTOP
;
1479 cv_broadcast(&p
->p_holdlwps
);
1481 while (pr_allstopped(p
, 0) > 0) {
1483 * At first glance, it may appear that we don't need a
1484 * call to holdcheck() here. But if the process gets a
1485 * SIGKILL signal, one of our stopped threads may have
1486 * been awakened and is waiting in exitlwps(), which
1487 * takes precedence over watchpoints.
1489 if (holdcheck(SHOLDWATCH
| SWATCHOK
) != 0) {
1490 p
->p_flag
&= ~(SHOLDWATCH
| SWATCHOK
);
1491 mutex_exit(&p
->p_lock
);
1495 cv_wait(&p
->p_holdlwps
, &p
->p_lock
);
1499 * All threads are now completely stopped.
1501 p
->p_flag
&= ~SWATCHOK
;
1502 p
->p_flag
&= ~SHOLDWATCH
;
1505 } else if (!(p
->p_flag
& SWATCHOK
)) {
1508 * SHOLDWATCH is set, so another thread is trying to do
1509 * watchpoint activity. Indicate this thread is stopping, and
1510 * wait for the OK from the master thread.
1512 t
->t_proc_flag
|= TP_WATCHSTOP
;
1513 cv_broadcast(&p
->p_holdlwps
);
1515 while (!(p
->p_flag
& SWATCHOK
)) {
1516 if (holdcheck(0) != 0) {
1517 t
->t_proc_flag
&= ~TP_WATCHSTOP
;
1518 mutex_exit(&p
->p_lock
);
1522 cv_wait(&p
->p_holdlwps
, &p
->p_lock
);
1526 * Once the master thread has given the OK, this thread can
1527 * actually call stop().
1529 t
->t_proc_flag
&= ~TP_WATCHSTOP
;
1532 stop(PR_SUSPENDED
, SUSPEND_NORMAL
);
1535 * It's not OK to do watchpoint activity, notify caller to
1543 * The only way we can hit the case where SHOLDWATCH is set and
1544 * SWATCHOK is set is if we are triggering this from within a
1545 * stop() call. Assert that this is the case.
1548 ASSERT(t
->t_proc_flag
& TP_STOPPING
);
1552 mutex_exit(&p
->p_lock
);
1558 * force all interruptible lwps to trap into the kernel.
1565 ASSERT(MUTEX_HELD(&p
->p_lock
));
1572 aston(t
); /* make thread trap or do post_syscall */
1573 if (ISWAKEABLE(t
) || ISWAITING(t
)) {
1575 } else if (t
->t_state
== TS_STOPPED
) {
1577 * Ensure that proc_exit() is not blocked by lwps
1578 * that were stopped via jobcontrol or /proc.
1580 if (p
->p_flag
& SEXITLWPS
) {
1582 t
->t_schedflag
|= (TS_XSTART
| TS_PSTART
);
1586 * If we are holding lwps for a forkall(),
1587 * force lwps that have been suspended via
1588 * lwp_suspend() and are suspended inside
1589 * of a system call to proceed to their
1590 * holdlwp() points where they are clonable.
1592 if ((p
->p_flag
& SHOLDFORK
) && SUSPENDED(t
)) {
1593 if ((t
->t_schedflag
& TS_CSTART
) == 0) {
1595 t
->t_schedflag
|= TS_CSTART
;
1599 } else if (t
->t_state
== TS_ONPROC
) {
1600 if (t
->t_cpu
!= CPU
)
1601 poke_cpu(t
->t_cpu
->cpu_id
);
1604 } while ((t
= t
->t_forw
) != p
->p_tlist
);
1608 * undo the effects of holdlwps() or holdwatch().
1611 continuelwps(proc_t
*p
)
1616 * If this flag is set, then the original holdwatch() didn't actually
1617 * stop the process. See comments for holdwatch().
1619 if (p
->p_flag
& SWATCHOK
) {
1620 ASSERT(curthread
->t_proc_flag
& TP_STOPPING
);
1624 ASSERT(MUTEX_HELD(&p
->p_lock
));
1625 ASSERT((p
->p_flag
& (SHOLDFORK
| SHOLDFORK1
| SHOLDWATCH
)) == 0);
1629 thread_lock(t
); /* SUSPENDED looks at t_schedflag */
1630 if (SUSPENDED(t
) && !(t
->t_proc_flag
& TP_HOLDLWP
)) {
1632 t
->t_schedflag
|= TS_CSTART
;
1636 } while ((t
= t
->t_forw
) != p
->p_tlist
);
1640 * Force all other LWPs in the current process other than the caller to exit,
1641 * and then cv_wait() on p_holdlwps for them to exit. The exitlwps() function
1642 * is typically used in these situations:
1644 * (a) prior to an exec() system call
1645 * (b) prior to dumping a core file
1646 * (c) prior to a uadmin() shutdown
1648 * If the 'coredump' flag is set, other LWPs are quiesced but not destroyed.
1649 * Multiple threads in the process can call this function at one time by
1650 * triggering execs or core dumps simultaneously, so the SEXITLWPS bit is used
1651 * to declare one particular thread the winner who gets to kill the others.
1652 * If a thread wins the exitlwps() dance, zero is returned; otherwise an
1653 * appropriate errno value is returned to caller for its system call to return.
1656 exitlwps(int coredump
)
1658 proc_t
*p
= curproc
;
1661 if (curthread
->t_door
)
1665 if (curthread
->t_schedctl
!= NULL
)
1666 schedctl_lwp_cleanup(curthread
);
1669 * Ensure that before starting to wait for other lwps to exit,
1670 * cleanup all upimutexes held by curthread. Otherwise, some other
1671 * lwp could be waiting (uninterruptibly) for a upimutex held by
1672 * curthread, and the call to pokelwps() below would deadlock.
1673 * Even if a blocked upimutex_lock is made interruptible,
1674 * curthread's upimutexes need to be unlocked: do it here.
1676 if (curthread
->t_upimutex
!= NULL
)
1680 * Grab p_lock in order to check and set SEXITLWPS to declare a winner.
1681 * We must also block any further /proc access from this point forward.
1683 mutex_enter(&p
->p_lock
);
1686 if (p
->p_flag
& SEXITLWPS
) {
1687 mutex_exit(&p
->p_lock
);
1688 aston(curthread
); /* force a trip through post_syscall */
1689 return (set_errno(EINTR
));
1692 p
->p_flag
|= SEXITLWPS
;
1693 if (coredump
) /* tell other lwps to stop, not exit */
1694 p
->p_flag
|= SCOREDUMP
;
1697 * Give precedence to exitlwps() if a holdlwps() is
1698 * in progress. The lwp doing the holdlwps() operation
1699 * is aborted when it is awakened.
1701 while (p
->p_flag
& (SHOLDFORK
| SHOLDFORK1
| SHOLDWATCH
)) {
1702 cv_broadcast(&p
->p_holdlwps
);
1703 cv_wait(&p
->p_holdlwps
, &p
->p_lock
);
1706 p
->p_flag
|= SHOLDFORK
;
1710 * Wait for process to become quiescent.
1713 while (p
->p_lwprcnt
> 0) {
1714 cv_wait(&p
->p_holdlwps
, &p
->p_lock
);
1718 ASSERT(p
->p_lwprcnt
== 1);
1721 * The SCOREDUMP flag puts the process into a quiescent
1722 * state. The process's lwps remain attached to this
1723 * process until exitlwps() is called again without the
1724 * 'coredump' flag set, then the lwps are terminated
1725 * and the process can exit.
1728 p
->p_flag
&= ~(SCOREDUMP
| SHOLDFORK
| SEXITLWPS
);
1733 * Determine if there are any lwps left dangling in
1734 * the stopped state. This happens when exitlwps()
1735 * aborts a holdlwps() operation.
1737 p
->p_flag
&= ~SHOLDFORK
;
1738 if ((heldcnt
= p
->p_lwpcnt
) > 1) {
1740 for (t
= curthread
->t_forw
; --heldcnt
> 0; t
= t
->t_forw
) {
1741 t
->t_proc_flag
&= ~TP_TWAIT
;
1747 * Wait for all other lwps to exit.
1750 while (p
->p_lwpcnt
> 1) {
1751 cv_wait(&p
->p_holdlwps
, &p
->p_lock
);
1755 ASSERT(p
->p_lwpcnt
== 1 && p
->p_lwprcnt
== 1);
1757 p
->p_flag
&= ~SEXITLWPS
;
1758 curthread
->t_proc_flag
&= ~TP_TWAIT
;
1761 if (!coredump
&& p
->p_zombcnt
) { /* cleanup the zombie lwps */
1766 for (ldp
= p
->p_lwpdir
, i
= 0; i
< p
->p_lwpdir_sz
; i
++, ldp
++) {
1767 lep
= ldp
->ld_entry
;
1768 if (lep
!= NULL
&& lep
->le_thread
!= curthread
) {
1769 ASSERT(lep
->le_thread
== NULL
);
1771 lwp_hash_out(p
, lep
->le_lwpid
);
1774 ASSERT(p
->p_zombcnt
== 0);
1778 * If some other LWP in the process wanted us to suspend ourself,
1779 * then we will not do it. The other LWP is now terminated and
1780 * no one will ever continue us again if we suspend ourself.
1782 curthread
->t_proc_flag
&= ~TP_HOLDLWP
;
1783 p
->p_flag
&= ~(SHOLDFORK
| SHOLDFORK1
| SHOLDWATCH
| SLWPWRAP
);
1784 mutex_exit(&p
->p_lock
);
1792 forklwp(klwp_t
*lwp
, proc_t
*cp
, id_t lwpid
)
1796 kthread_t
*t
= lwptot(lwp
);
1798 proc_t
*p
= lwptoproc(lwp
);
1804 ASSERT(p
== curproc
);
1805 ASSERT(t
== curthread
|| (SUSPENDED(t
) && lwp
->lwp_asleep
== 0));
1809 /* copy args out of registers first */
1810 (void) save_syscall_args();
1812 clwp
= lwp_create(cp
->p_lwpcnt
== 0 ? lwp_rtt_initial
: lwp_rtt
,
1813 NULL
, 0, cp
, TS_STOPPED
, t
->t_pri
, &t
->t_hold
, NOCLASS
, lwpid
);
1818 * most of the parent's lwp can be copied to its duplicate,
1819 * except for the fields that are unique to each lwp, like
1820 * lwp_thread, lwp_procp, lwp_regs, and lwp_ap.
1822 ct
= clwp
->lwp_thread
;
1823 tregs
= clwp
->lwp_regs
;
1824 tfpu
= clwp
->lwp_fpu
;
1825 brand_data
= clwp
->lwp_brand
;
1828 * Copy parent lwp to child lwp. Hold child's p_lock to prevent
1829 * mstate_aggr_state() from reading stale mstate entries copied
1832 mutex_enter(&cp
->p_lock
);
1835 /* clear microstate and resource usage data in new lwp */
1836 init_mstate(ct
, LMS_STOPPED
);
1837 bzero(&clwp
->lwp_ru
, sizeof (clwp
->lwp_ru
));
1838 mutex_exit(&cp
->p_lock
);
1840 /* fix up child's lwp */
1842 clwp
->lwp_pcb
.pcb_flags
= 0;
1843 clwp
->lwp_cursig
= 0;
1844 clwp
->lwp_extsig
= 0;
1845 clwp
->lwp_curinfo
= NULL
;
1846 clwp
->lwp_thread
= ct
;
1847 ct
->t_sysnum
= t
->t_sysnum
;
1848 clwp
->lwp_regs
= tregs
;
1849 clwp
->lwp_fpu
= tfpu
;
1850 clwp
->lwp_brand
= brand_data
;
1851 clwp
->lwp_ap
= clwp
->lwp_arg
;
1852 clwp
->lwp_procp
= cp
;
1853 bzero(clwp
->lwp_timer
, sizeof (clwp
->lwp_timer
));
1854 clwp
->lwp_lastfault
= 0;
1855 clwp
->lwp_lastfaddr
= 0;
1857 /* copy parent's struct regs to child. */
1858 lwp_forkregs(lwp
, clwp
);
1861 * Fork thread context ops, if any.
1866 /* fix door state in the child */
1870 /* copy current contract templates, clear latest contracts */
1871 lwp_ctmpl_copy(clwp
, lwp
);
1873 mutex_enter(&cp
->p_lock
);
1874 /* lwp_create() set the TP_HOLDLWP flag */
1875 if (!(t
->t_proc_flag
& TP_HOLDLWP
))
1876 ct
->t_proc_flag
&= ~TP_HOLDLWP
;
1877 if (cp
->p_flag
& SMSACCT
)
1878 ct
->t_proc_flag
|= TP_MSACCT
;
1879 mutex_exit(&cp
->p_lock
);
1881 /* Allow brand to propagate brand-specific state */
1882 if (PROC_IS_BRANDED(p
))
1883 BROP(p
)->b_forklwp(lwp
, clwp
);
1888 val
= CL_ALLOC(&bufp
, cid
, KM_SLEEP
);
1891 mutex_enter(&p
->p_lock
);
1892 if (cid
!= t
->t_cid
) {
1894 * Someone just changed this thread's scheduling class,
1895 * so try pre-allocating the buffer again. Hopefully we
1896 * don't hit this often.
1898 mutex_exit(&p
->p_lock
);
1903 ct
->t_unpark
= t
->t_unpark
;
1904 ct
->t_clfuncs
= t
->t_clfuncs
;
1905 CL_FORK(t
, ct
, bufp
);
1906 ct
->t_cid
= t
->t_cid
; /* after data allocated so prgetpsinfo works */
1907 mutex_exit(&p
->p_lock
);
1913 * Add a new lwp entry to the lwp directory and to the lwpid hash table.
1916 lwp_hash_in(proc_t
*p
, lwpent_t
*lep
, tidhash_t
*tidhash
, uint_t tidhash_sz
,
1919 tidhash_t
*thp
= &tidhash
[TIDHASH(lep
->le_lwpid
, tidhash_sz
)];
1925 * Allocate a directory element from the free list.
1926 * Code elsewhere guarantees a free slot.
1929 p
->p_lwpfree
= ldp
->ld_next
;
1930 ASSERT(ldp
->ld_entry
== NULL
);
1931 ldp
->ld_entry
= lep
;
1934 mutex_enter(&thp
->th_lock
);
1937 * Insert it into the lwpid hash table.
1939 ldpp
= &thp
->th_list
;
1940 ldp
->ld_next
= *ldpp
;
1944 * Set the active thread's directory slot entry.
1946 if ((t
= lep
->le_thread
) != NULL
) {
1947 ASSERT(lep
->le_lwpid
== t
->t_tid
);
1948 t
->t_dslot
= (int)(ldp
- p
->p_lwpdir
);
1952 mutex_exit(&thp
->th_lock
);
1956 * Remove an lwp from the lwpid hash table and free its directory entry.
1957 * This is done when a detached lwp exits in lwp_exit() or
1958 * when a non-detached lwp is waited for in lwp_wait() or
1959 * when a zombie lwp is detached in lwp_detach().
1962 lwp_hash_out(proc_t
*p
, id_t lwpid
)
1964 tidhash_t
*thp
= &p
->p_tidhash
[TIDHASH(lwpid
, p
->p_tidhash_sz
)];
1969 mutex_enter(&thp
->th_lock
);
1970 for (ldpp
= &thp
->th_list
;
1971 (ldp
= *ldpp
) != NULL
; ldpp
= &ldp
->ld_next
) {
1972 lep
= ldp
->ld_entry
;
1973 if (lep
->le_lwpid
== lwpid
) {
1974 prlwpfree(p
, lep
); /* /proc deals with le_trace */
1975 *ldpp
= ldp
->ld_next
;
1976 ldp
->ld_entry
= NULL
;
1977 ldp
->ld_next
= p
->p_lwpfree
;
1979 kmem_free(lep
, sizeof (*lep
));
1983 mutex_exit(&thp
->th_lock
);
1987 * Lookup an lwp in the lwpid hash table by lwpid.
1990 lwp_hash_lookup(proc_t
*p
, id_t lwpid
)
1996 * The process may be exiting, after p_tidhash has been set to NULL in
1997 * proc_exit() but before prfee() has been called. Return failure in
2000 if (p
->p_tidhash
== NULL
)
2003 thp
= &p
->p_tidhash
[TIDHASH(lwpid
, p
->p_tidhash_sz
)];
2004 for (ldp
= thp
->th_list
; ldp
!= NULL
; ldp
= ldp
->ld_next
) {
2005 if (ldp
->ld_entry
->le_lwpid
== lwpid
)
2013 * Same as lwp_hash_lookup(), but acquire and return
2014 * the tid hash table entry lock on success.
2017 lwp_hash_lookup_and_lock(proc_t
*p
, id_t lwpid
, kmutex_t
**mpp
)
2025 tidhash_sz
= p
->p_tidhash_sz
;
2027 if ((tidhash
= p
->p_tidhash
) == NULL
)
2030 thp
= &tidhash
[TIDHASH(lwpid
, tidhash_sz
)];
2031 mutex_enter(&thp
->th_lock
);
2034 * Since we are not holding p->p_lock, the tid hash table
2035 * may have changed. If so, start over. If not, then
2036 * it cannot change until after we drop &thp->th_lock;
2038 if (tidhash
!= p
->p_tidhash
|| tidhash_sz
!= p
->p_tidhash_sz
) {
2039 mutex_exit(&thp
->th_lock
);
2043 for (ldp
= thp
->th_list
; ldp
!= NULL
; ldp
= ldp
->ld_next
) {
2044 if (ldp
->ld_entry
->le_lwpid
== lwpid
) {
2045 *mpp
= &thp
->th_lock
;
2050 mutex_exit(&thp
->th_lock
);
2055 * Update the indicated LWP usage statistic for the current LWP.
2058 lwp_stat_update(lwp_stat_id_t lwp_stat_id
, long inc
)
2060 klwp_t
*lwp
= ttolwp(curthread
);
2065 switch (lwp_stat_id
) {
2066 case LWP_STAT_INBLK
:
2067 lwp
->lwp_ru
.inblock
+= inc
;
2069 case LWP_STAT_OUBLK
:
2070 lwp
->lwp_ru
.oublock
+= inc
;
2072 case LWP_STAT_MSGRCV
:
2073 lwp
->lwp_ru
.msgrcv
+= inc
;
2075 case LWP_STAT_MSGSND
:
2076 lwp
->lwp_ru
.msgsnd
+= inc
;
2079 panic("lwp_stat_update: invalid lwp_stat_id 0x%x", lwp_stat_id
);