xref: /titanic_44/usr/src/uts/common/os/sig.c (revision a398035bff356df3a17376cda89486682fb1f23b)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #include <sys/param.h>
31 #include <sys/types.h>
32 #include <sys/bitmap.h>
33 #include <sys/sysmacros.h>
34 #include <sys/systm.h>
35 #include <sys/cred.h>
36 #include <sys/user.h>
37 #include <sys/errno.h>
38 #include <sys/proc.h>
39 #include <sys/poll_impl.h> /* only needed for kludge in sigwaiting_send() */
40 #include <sys/signal.h>
41 #include <sys/siginfo.h>
42 #include <sys/fault.h>
43 #include <sys/ucontext.h>
44 #include <sys/procfs.h>
45 #include <sys/wait.h>
46 #include <sys/class.h>
47 #include <sys/mman.h>
48 #include <sys/procset.h>
49 #include <sys/kmem.h>
50 #include <sys/cpuvar.h>
51 #include <sys/prsystm.h>
52 #include <sys/debug.h>
53 #include <vm/as.h>
54 #include <sys/bitmap.h>
55 #include <c2/audit.h>
56 #include <sys/core.h>
57 #include <sys/schedctl.h>
58 #include <sys/contract/process_impl.h>
59 #include <sys/cyclic.h>
60 #include <sys/dtrace.h>
61 #include <sys/sdt.h>
62 
63 				/* MUST be contiguous */
64 k_sigset_t nullsmask = {0, 0};
65 
66 k_sigset_t fillset = {FILLSET0, FILLSET1};
67 
68 k_sigset_t cantmask = {CANTMASK0, CANTMASK1};
69 
70 k_sigset_t cantreset = {(sigmask(SIGILL)|sigmask(SIGTRAP)|sigmask(SIGPWR)), 0};
71 
72 k_sigset_t ignoredefault = {(sigmask(SIGCONT)|sigmask(SIGCLD)|sigmask(SIGPWR)
73 			|sigmask(SIGWINCH)|sigmask(SIGURG)|sigmask(SIGWAITING)),
74 			(sigmask(SIGLWP)|sigmask(SIGCANCEL)|sigmask(SIGFREEZE)
75 			|sigmask(SIGTHAW)|sigmask(SIGXRES)|sigmask(SIGJVM1)
76 			|sigmask(SIGJVM2))};
77 
78 k_sigset_t stopdefault = {(sigmask(SIGSTOP)|sigmask(SIGTSTP)
79 			|sigmask(SIGTTOU)|sigmask(SIGTTIN)), 0};
80 
81 k_sigset_t coredefault = {(sigmask(SIGQUIT)|sigmask(SIGILL)|sigmask(SIGTRAP)
82 			|sigmask(SIGIOT)|sigmask(SIGEMT)|sigmask(SIGFPE)
83 			|sigmask(SIGBUS)|sigmask(SIGSEGV)|sigmask(SIGSYS)
84 			|sigmask(SIGXCPU)|sigmask(SIGXFSZ)), 0};
85 
86 k_sigset_t holdvfork = {(sigmask(SIGTTOU)|sigmask(SIGTTIN)|sigmask(SIGTSTP)),
87 			0};
88 
89 static	int	isjobstop(int);
90 static	void	post_sigcld(proc_t *, sigqueue_t *);
91 
92 /*
93  * Internal variables for counting number of user thread stop requests posted.
94  * They may not be accurate at some special situation such as that a virtually
95  * stopped thread starts to run.
96  */
97 static int num_utstop;
98 /*
99  * Internal variables for broadcasting an event when all thread stop requests
100  * are processed.
101  */
102 static kcondvar_t utstop_cv;
103 
104 static kmutex_t thread_stop_lock;
105 void del_one_utstop(void);
106 
107 /*
108  * Send the specified signal to the specified process.
109  */
110 void
111 psignal(proc_t *p, int sig)
112 {
113 	mutex_enter(&p->p_lock);
114 	sigtoproc(p, NULL, sig);
115 	mutex_exit(&p->p_lock);
116 }
117 
118 /*
119  * Send the specified signal to the specified thread.
120  */
121 void
122 tsignal(kthread_t *t, int sig)
123 {
124 	proc_t *p = ttoproc(t);
125 
126 	mutex_enter(&p->p_lock);
127 	sigtoproc(p, t, sig);
128 	mutex_exit(&p->p_lock);
129 }
130 
131 int
132 signal_is_blocked(kthread_t *t, int sig)
133 {
134 	return (sigismember(&t->t_hold, sig) ||
135 	    (schedctl_sigblock(t) && !sigismember(&cantmask, sig)));
136 }
137 
138 /*
139  * Return true if the signal can safely be discarded on generation.
140  * That is, if there is no need for the signal on the receiving end.
141  * The answer is true if the process is a zombie or
142  * if all of these conditions are true:
143  *	the signal is being ignored
144  *	the process is single-threaded
145  *	the signal is not being traced by /proc
146  * 	the signal is not blocked by the process
147  *	the signal is not being accepted via sigwait()
148  */
149 static int
150 sig_discardable(proc_t *p, int sig)
151 {
152 	kthread_t *t = p->p_tlist;
153 
154 	return (t == NULL ||		/* if zombie or ... */
155 	    (sigismember(&p->p_ignore, sig) &&	/* signal is ignored */
156 	    t->t_forw == t &&			/* and single-threaded */
157 	    !tracing(p, sig) &&			/* and no /proc tracing */
158 	    !signal_is_blocked(t, sig) &&	/* and signal not blocked */
159 	    !sigismember(&t->t_sigwait, sig)));	/* and not being accepted */
160 }
161 
162 /*
163  * Return true if this thread is going to eat this signal soon.
164  * Note that, if the signal is SIGKILL, we force stopped threads to be
165  * set running (to make SIGKILL be a sure kill), but only if the process
166  * is not currently locked by /proc (the P_PR_LOCK flag).  Code in /proc
167  * relies on the fact that a process will not change shape while P_PR_LOCK
168  * is set (it drops and reacquires p->p_lock while leaving P_PR_LOCK set).
169  * We wish that we could simply call prbarrier() below, in sigtoproc(), to
170  * ensure that the process is not locked by /proc, but prbarrier() drops
171  * and reacquires p->p_lock and dropping p->p_lock here would be damaging.
172  */
173 int
174 eat_signal(kthread_t *t, int sig)
175 {
176 	int rval = 0;
177 	ASSERT(THREAD_LOCK_HELD(t));
178 
179 	/*
180 	 * Do not do anything if the target thread has the signal blocked.
181 	 */
182 	if (!signal_is_blocked(t, sig)) {
183 		t->t_sig_check = 1;	/* have thread do an issig */
184 		if (ISWAKEABLE(t) || ISWAITING(t)) {
185 			setrun_locked(t);
186 			rval = 1;
187 		} else if (t->t_state == TS_STOPPED && sig == SIGKILL &&
188 		    !(ttoproc(t)->p_proc_flag & P_PR_LOCK)) {
189 			ttoproc(t)->p_stopsig = 0;
190 			t->t_dtrace_stop = 0;
191 			t->t_schedflag |= TS_XSTART | TS_PSTART;
192 			setrun_locked(t);
193 		} else if (t != curthread && t->t_state == TS_ONPROC) {
194 			aston(t);	/* make it do issig promptly */
195 			if (t->t_cpu != CPU)
196 				poke_cpu(t->t_cpu->cpu_id);
197 			rval = 1;
198 		} else if (t->t_state == TS_RUN) {
199 			rval = 1;
200 		}
201 	}
202 
203 	return (rval);
204 }
205 
206 /*
207  * Post a signal.
208  * If a non-null thread pointer is passed, then post the signal
209  * to the thread/lwp, otherwise post the signal to the process.
210  */
211 void
212 sigtoproc(proc_t *p, kthread_t *t, int sig)
213 {
214 	kthread_t *tt;
215 	int ext = !(curproc->p_flag & SSYS) &&
216 	    (curproc->p_ct_process != p->p_ct_process);
217 
218 	ASSERT(MUTEX_HELD(&p->p_lock));
219 
220 	if (sig <= 0 || sig >= NSIG)
221 		return;
222 
223 	/*
224 	 * Regardless of origin or directedness,
225 	 * SIGKILL kills all lwps in the process immediately
226 	 * and jobcontrol signals affect all lwps in the process.
227 	 */
228 	if (sig == SIGKILL) {
229 		p->p_flag |= SKILLED | (ext ? SEXTKILLED : 0);
230 		t = NULL;
231 	} else if (sig == SIGCONT) {
232 		/*
233 		 * The SSCONT flag will remain set until a stopping
234 		 * signal comes in (below).  This is harmless.
235 		 */
236 		p->p_flag |= SSCONT;
237 		sigdelq(p, NULL, SIGSTOP);
238 		sigdelq(p, NULL, SIGTSTP);
239 		sigdelq(p, NULL, SIGTTOU);
240 		sigdelq(p, NULL, SIGTTIN);
241 		sigdiffset(&p->p_sig, &stopdefault);
242 		sigdiffset(&p->p_extsig, &stopdefault);
243 		p->p_stopsig = 0;
244 		if ((tt = p->p_tlist) != NULL) {
245 			do {
246 				sigdelq(p, tt, SIGSTOP);
247 				sigdelq(p, tt, SIGTSTP);
248 				sigdelq(p, tt, SIGTTOU);
249 				sigdelq(p, tt, SIGTTIN);
250 				sigdiffset(&tt->t_sig, &stopdefault);
251 				sigdiffset(&tt->t_extsig, &stopdefault);
252 			} while ((tt = tt->t_forw) != p->p_tlist);
253 		}
254 		if ((tt = p->p_tlist) != NULL) {
255 			do {
256 				thread_lock(tt);
257 				if (tt->t_state == TS_STOPPED &&
258 				    tt->t_whystop == PR_JOBCONTROL) {
259 					tt->t_schedflag |= TS_XSTART;
260 					setrun_locked(tt);
261 				}
262 				thread_unlock(tt);
263 			} while ((tt = tt->t_forw) != p->p_tlist);
264 		}
265 	} else if (sigismember(&stopdefault, sig)) {
266 		/*
267 		 * This test has a race condition which we can't fix:
268 		 * By the time the stopping signal is received by
269 		 * the target process/thread, the signal handler
270 		 * and/or the detached state might have changed.
271 		 */
272 		if (PTOU(p)->u_signal[sig-1] == SIG_DFL &&
273 		    (sig == SIGSTOP || !p->p_pgidp->pid_pgorphaned))
274 			p->p_flag &= ~SSCONT;
275 		sigdelq(p, NULL, SIGCONT);
276 		sigdelset(&p->p_sig, SIGCONT);
277 		sigdelset(&p->p_extsig, SIGCONT);
278 		if ((tt = p->p_tlist) != NULL) {
279 			do {
280 				sigdelq(p, tt, SIGCONT);
281 				sigdelset(&tt->t_sig, SIGCONT);
282 				sigdelset(&tt->t_extsig, SIGCONT);
283 			} while ((tt = tt->t_forw) != p->p_tlist);
284 		}
285 	}
286 
287 	if (sig_discardable(p, sig)) {
288 		DTRACE_PROC3(signal__discard, kthread_t *, p->p_tlist,
289 		    proc_t *, p, int, sig);
290 		return;
291 	}
292 
293 	if (t != NULL) {
294 		/*
295 		 * This is a directed signal, wake up the lwp.
296 		 */
297 		sigaddset(&t->t_sig, sig);
298 		if (ext)
299 			sigaddset(&t->t_extsig, sig);
300 		thread_lock(t);
301 		(void) eat_signal(t, sig);
302 		thread_unlock(t);
303 		DTRACE_PROC2(signal__send, kthread_t *, t, int, sig);
304 	} else if ((tt = p->p_tlist) != NULL) {
305 		/*
306 		 * Make sure that some lwp that already exists
307 		 * in the process fields the signal soon.
308 		 * Wake up an interruptibly sleeping lwp if necessary.
309 		 * For SIGKILL make all of the lwps see the signal;
310 		 * This is needed to guarantee a sure kill for processes
311 		 * with a mix of realtime and non-realtime threads.
312 		 */
313 		int su = 0;
314 
315 		sigaddset(&p->p_sig, sig);
316 		if (ext)
317 			sigaddset(&p->p_extsig, sig);
318 		do {
319 			thread_lock(tt);
320 			if (eat_signal(tt, sig) && sig != SIGKILL) {
321 				thread_unlock(tt);
322 				break;
323 			}
324 			if (SUSPENDED(tt))
325 				su++;
326 			thread_unlock(tt);
327 		} while ((tt = tt->t_forw) != p->p_tlist);
328 		/*
329 		 * If the process is deadlocked, make somebody run and die.
330 		 */
331 		if (sig == SIGKILL && p->p_stat != SIDL &&
332 		    p->p_lwprcnt == 0 && p->p_lwpcnt == su &&
333 		    !(p->p_proc_flag & P_PR_LOCK)) {
334 			thread_lock(tt);
335 			p->p_lwprcnt++;
336 			tt->t_schedflag |= TS_CSTART;
337 			setrun_locked(tt);
338 			thread_unlock(tt);
339 		}
340 
341 		DTRACE_PROC2(signal__send, kthread_t *, tt, int, sig);
342 	}
343 }
344 
345 static int
346 isjobstop(int sig)
347 {
348 	proc_t *p = ttoproc(curthread);
349 
350 	ASSERT(MUTEX_HELD(&p->p_lock));
351 
352 	if (PTOU(curproc)->u_signal[sig-1] == SIG_DFL &&
353 	    sigismember(&stopdefault, sig)) {
354 		/*
355 		 * If SIGCONT has been posted since we promoted this signal
356 		 * from pending to current, then don't do a jobcontrol stop.
357 		 */
358 		if (!(p->p_flag & SSCONT) &&
359 		    (sig == SIGSTOP || !p->p_pgidp->pid_pgorphaned) &&
360 		    curthread != p->p_agenttp) {
361 			sigqueue_t *sqp;
362 
363 			stop(PR_JOBCONTROL, sig);
364 			mutex_exit(&p->p_lock);
365 			sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
366 			mutex_enter(&pidlock);
367 			/*
368 			 * Only the first lwp to continue notifies the parent.
369 			 */
370 			if (p->p_pidflag & CLDCONT)
371 				siginfofree(sqp);
372 			else {
373 				p->p_pidflag |= CLDCONT;
374 				p->p_wcode = CLD_CONTINUED;
375 				p->p_wdata = SIGCONT;
376 				sigcld(p, sqp);
377 			}
378 			mutex_exit(&pidlock);
379 			mutex_enter(&p->p_lock);
380 		}
381 		return (1);
382 	}
383 	return (0);
384 }
385 
386 /*
387  * Returns true if the current process has a signal to process, and
388  * the signal is not held.  The signal to process is put in p_cursig.
389  * This is asked at least once each time a process enters the system
390  * (though this can usually be done without actually calling issig by
391  * checking the pending signal masks).  A signal does not do anything
392  * directly to a process; it sets a flag that asks the process to do
393  * something to itself.
394  *
395  * The "why" argument indicates the allowable side-effects of the call:
396  *
397  * FORREAL:  Extract the next pending signal from p_sig into p_cursig;
398  * stop the process if a stop has been requested or if a traced signal
399  * is pending.
400  *
401  * JUSTLOOKING:  Don't stop the process, just indicate whether or not
402  * a signal might be pending (FORREAL is needed to tell for sure).
403  *
404  * XXX: Changes to the logic in these routines should be propagated
405  * to lm_sigispending().  See bug 1201594.
406  */
407 
408 static int issig_forreal(void);
409 static int issig_justlooking(void);
410 
411 int
412 issig(int why)
413 {
414 	ASSERT(why == FORREAL || why == JUSTLOOKING);
415 
416 	return ((why == FORREAL)? issig_forreal() : issig_justlooking());
417 }
418 
419 
420 static int
421 issig_justlooking(void)
422 {
423 	kthread_t *t = curthread;
424 	klwp_t *lwp = ttolwp(t);
425 	proc_t *p = ttoproc(t);
426 	k_sigset_t set;
427 
428 	/*
429 	 * This function answers the question:
430 	 * "Is there any reason to call issig_forreal()?"
431 	 *
432 	 * We have to answer the question w/o grabbing any locks
433 	 * because we are (most likely) being called after we
434 	 * put ourselves on the sleep queue.
435 	 */
436 
437 	if (t->t_dtrace_stop | t->t_dtrace_sig)
438 		return (1);
439 
440 	/*
441 	 * Another piece of complexity in this process.  When single-stepping a
442 	 * process, we don't want an intervening signal or TP_PAUSE request to
443 	 * suspend the current thread.  Otherwise, the controlling process will
444 	 * hang beacuse we will be stopped with TS_PSTART set in t_schedflag.
445 	 * We will trigger any remaining signals when we re-enter the kernel on
446 	 * the single step trap.
447 	 */
448 	if (lwp->lwp_pcb.pcb_flags & NORMAL_STEP)
449 		return (0);
450 
451 	if ((lwp->lwp_asleep && MUSTRETURN(p, t)) ||
452 	    (p->p_flag & (SEXITLWPS|SKILLED)) ||
453 	    (lwp->lwp_nostop == 0 &&
454 	    (p->p_stopsig | (p->p_flag & (SHOLDFORK1|SHOLDWATCH)) |
455 	    (t->t_proc_flag &
456 	    (TP_PRSTOP|TP_HOLDLWP|TP_CHKPT|TP_PAUSE)))) ||
457 	    lwp->lwp_cursig)
458 		return (1);
459 
460 	if (p->p_flag & SVFWAIT)
461 		return (0);
462 	set = p->p_sig;
463 	sigorset(&set, &t->t_sig);
464 	if (schedctl_sigblock(t))	/* all blockable signals blocked */
465 		sigandset(&set, &cantmask);
466 	else
467 		sigdiffset(&set, &t->t_hold);
468 	if (p->p_flag & SVFORK)
469 		sigdiffset(&set, &holdvfork);
470 
471 	if (!sigisempty(&set)) {
472 		int sig;
473 
474 		for (sig = 1; sig < NSIG; sig++) {
475 			if (sigismember(&set, sig) &&
476 			    (tracing(p, sig) ||
477 			    sigismember(&t->t_sigwait, sig) ||
478 			    !sigismember(&p->p_ignore, sig))) {
479 				/*
480 				 * Don't promote a signal that will stop
481 				 * the process when lwp_nostop is set.
482 				 */
483 				if (!lwp->lwp_nostop ||
484 				    PTOU(curproc)->u_signal[sig-1] != SIG_DFL ||
485 				    !sigismember(&stopdefault, sig))
486 					return (1);
487 			}
488 		}
489 	}
490 
491 	return (0);
492 }
493 
494 static int
495 issig_forreal(void)
496 {
497 	int sig = 0, ext = 0;
498 	kthread_t *t = curthread;
499 	klwp_t *lwp = ttolwp(t);
500 	proc_t *p = ttoproc(t);
501 	int toproc = 0;
502 	int sigcld_found = 0;
503 	int nostop_break = 0;
504 
505 	ASSERT(t->t_state == TS_ONPROC);
506 
507 	mutex_enter(&p->p_lock);
508 	schedctl_finish_sigblock(t);
509 
510 	if (t->t_dtrace_stop | t->t_dtrace_sig) {
511 		if (t->t_dtrace_stop) {
512 			/*
513 			 * If DTrace's "stop" action has been invoked on us,
514 			 * set TP_PRSTOP.
515 			 */
516 			t->t_proc_flag |= TP_PRSTOP;
517 		}
518 
519 		if (t->t_dtrace_sig != 0) {
520 			k_siginfo_t info;
521 
522 			/*
523 			 * Post the signal generated as the result of
524 			 * DTrace's "raise" action as a normal signal before
525 			 * the full-fledged signal checking begins.
526 			 */
527 			bzero(&info, sizeof (info));
528 			info.si_signo = t->t_dtrace_sig;
529 			info.si_code = SI_DTRACE;
530 
531 			sigaddq(p, NULL, &info, KM_NOSLEEP);
532 
533 			t->t_dtrace_sig = 0;
534 		}
535 	}
536 
537 	for (;;) {
538 		if (p->p_flag & (SEXITLWPS|SKILLED)) {
539 			lwp->lwp_cursig = sig = SIGKILL;
540 			lwp->lwp_extsig = ext = (p->p_flag & SEXTKILLED) != 0;
541 			t->t_sig_check = 1;
542 			break;
543 		}
544 
545 		/*
546 		 * Another piece of complexity in this process.  When
547 		 * single-stepping a process, we don't want an intervening
548 		 * signal or TP_PAUSE request to suspend the current thread.
549 		 * Otherwise, the controlling process will hang beacuse we will
550 		 * be stopped with TS_PSTART set in t_schedflag.  We will
551 		 * trigger any remaining signals when we re-enter the kernel on
552 		 * the single step trap.
553 		 */
554 		if (lwp->lwp_pcb.pcb_flags & NORMAL_STEP) {
555 			sig = 0;
556 			break;
557 		}
558 
559 		/*
560 		 * Hold the lwp here for watchpoint manipulation.
561 		 */
562 		if ((t->t_proc_flag & TP_PAUSE) && !lwp->lwp_nostop) {
563 			stop(PR_SUSPENDED, SUSPEND_PAUSE);
564 			continue;
565 		}
566 
567 		if (lwp->lwp_asleep && MUSTRETURN(p, t)) {
568 			if ((sig = lwp->lwp_cursig) != 0) {
569 				/*
570 				 * Make sure we call ISSIG() in post_syscall()
571 				 * to re-validate this current signal.
572 				 */
573 				t->t_sig_check = 1;
574 			}
575 			break;
576 		}
577 
578 		/*
579 		 * If the request is PR_CHECKPOINT, ignore the rest of signals
580 		 * or requests.  Honor other stop requests or signals later.
581 		 * Go back to top of loop here to check if an exit or hold
582 		 * event has occurred while stopped.
583 		 */
584 		if ((t->t_proc_flag & TP_CHKPT) && !lwp->lwp_nostop) {
585 			stop(PR_CHECKPOINT, 0);
586 			continue;
587 		}
588 
589 		/*
590 		 * Honor SHOLDFORK1, SHOLDWATCH, and TP_HOLDLWP before dealing
591 		 * with signals or /proc.  Another lwp is executing fork1(),
592 		 * or is undergoing watchpoint activity (remapping a page),
593 		 * or is executing lwp_suspend() on this lwp.
594 		 * Again, go back to top of loop to check if an exit
595 		 * or hold event has occurred while stopped.
596 		 */
597 		if (((p->p_flag & (SHOLDFORK1|SHOLDWATCH)) ||
598 		    (t->t_proc_flag & TP_HOLDLWP)) && !lwp->lwp_nostop) {
599 			stop(PR_SUSPENDED, SUSPEND_NORMAL);
600 			continue;
601 		}
602 
603 		/*
604 		 * Honor requested stop before dealing with the
605 		 * current signal; a debugger may change it.
606 		 * Do not want to go back to loop here since this is a special
607 		 * stop that means: make incremental progress before the next
608 		 * stop. The danger is that returning to top of loop would most
609 		 * likely drop the thread right back here to stop soon after it
610 		 * was continued, violating the incremental progress request.
611 		 */
612 		if ((t->t_proc_flag & TP_PRSTOP) && !lwp->lwp_nostop)
613 			stop(PR_REQUESTED, 0);
614 
615 		/*
616 		 * If a debugger wants us to take a signal it will have
617 		 * left it in lwp->lwp_cursig.  If lwp_cursig has been cleared
618 		 * or if it's being ignored, we continue on looking for another
619 		 * signal.  Otherwise we return the specified signal, provided
620 		 * it's not a signal that causes a job control stop.
621 		 *
622 		 * When stopped on PR_JOBCONTROL, there is no current
623 		 * signal; we cancel lwp->lwp_cursig temporarily before
624 		 * calling isjobstop().  The current signal may be reset
625 		 * by a debugger while we are stopped in isjobstop().
626 		 *
627 		 * If the current thread is accepting the signal
628 		 * (via sigwait(), sigwaitinfo(), or sigtimedwait()),
629 		 * we allow the signal to be accepted, even if it is
630 		 * being ignored, and without causing a job control stop.
631 		 */
632 		if ((sig = lwp->lwp_cursig) != 0) {
633 			ext = lwp->lwp_extsig;
634 			lwp->lwp_cursig = 0;
635 			lwp->lwp_extsig = 0;
636 			if (sigismember(&t->t_sigwait, sig) ||
637 			    (!sigismember(&p->p_ignore, sig) &&
638 			    !isjobstop(sig))) {
639 				if (p->p_flag & (SEXITLWPS|SKILLED)) {
640 					sig = SIGKILL;
641 					ext = (p->p_flag & SEXTKILLED) != 0;
642 				}
643 				lwp->lwp_cursig = (uchar_t)sig;
644 				lwp->lwp_extsig = (uchar_t)ext;
645 				break;
646 			}
647 			/*
648 			 * The signal is being ignored or it caused a
649 			 * job-control stop.  If another current signal
650 			 * has not been established, return the current
651 			 * siginfo, if any, to the memory manager.
652 			 */
653 			if (lwp->lwp_cursig == 0 && lwp->lwp_curinfo != NULL) {
654 				siginfofree(lwp->lwp_curinfo);
655 				lwp->lwp_curinfo = NULL;
656 			}
657 			/*
658 			 * Loop around again in case we were stopped
659 			 * on a job control signal and a /proc stop
660 			 * request was posted or another current signal
661 			 * was established while we were stopped.
662 			 */
663 			continue;
664 		}
665 
666 		if (p->p_stopsig && !lwp->lwp_nostop &&
667 		    curthread != p->p_agenttp) {
668 			/*
669 			 * Some lwp in the process has already stopped
670 			 * showing PR_JOBCONTROL.  This is a stop in
671 			 * sympathy with the other lwp, even if this
672 			 * lwp is blocking the stopping signal.
673 			 */
674 			stop(PR_JOBCONTROL, p->p_stopsig);
675 			continue;
676 		}
677 
678 		/*
679 		 * Loop on the pending signals until we find a
680 		 * non-held signal that is traced or not ignored.
681 		 * First check the signals pending for the lwp,
682 		 * then the signals pending for the process as a whole.
683 		 */
684 		for (;;) {
685 			if ((sig = fsig(&t->t_sig, t)) != 0) {
686 				if (sig == SIGCLD)
687 					sigcld_found = 1;
688 				toproc = 0;
689 				if (tracing(p, sig) ||
690 				    sigismember(&t->t_sigwait, sig) ||
691 				    !sigismember(&p->p_ignore, sig)) {
692 					if (sigismember(&t->t_extsig, sig))
693 						ext = 1;
694 					break;
695 				}
696 				sigdelset(&t->t_sig, sig);
697 				sigdelset(&t->t_extsig, sig);
698 				sigdelq(p, t, sig);
699 			} else if ((sig = fsig(&p->p_sig, t)) != 0) {
700 				if (sig == SIGCLD)
701 					sigcld_found = 1;
702 				toproc = 1;
703 				if (tracing(p, sig) ||
704 				    sigismember(&t->t_sigwait, sig) ||
705 				    !sigismember(&p->p_ignore, sig)) {
706 					if (sigismember(&p->p_extsig, sig))
707 						ext = 1;
708 					break;
709 				}
710 				sigdelset(&p->p_sig, sig);
711 				sigdelset(&p->p_extsig, sig);
712 				sigdelq(p, NULL, sig);
713 			} else {
714 				/* no signal was found */
715 				break;
716 			}
717 		}
718 
719 		if (sig == 0) {	/* no signal was found */
720 			if (p->p_flag & (SEXITLWPS|SKILLED)) {
721 				lwp->lwp_cursig = SIGKILL;
722 				sig = SIGKILL;
723 				ext = (p->p_flag & SEXTKILLED) != 0;
724 			}
725 			break;
726 		}
727 
728 		/*
729 		 * If we have been informed not to stop (i.e., we are being
730 		 * called from within a network operation), then don't promote
731 		 * the signal at this time, just return the signal number.
732 		 * We will call issig() again later when it is safe.
733 		 *
734 		 * fsig() does not return a jobcontrol stopping signal
735 		 * with a default action of stopping the process if
736 		 * lwp_nostop is set, so we won't be causing a bogus
737 		 * EINTR by this action.  (Such a signal is eaten by
738 		 * isjobstop() when we loop around to do final checks.)
739 		 */
740 		if (lwp->lwp_nostop) {
741 			nostop_break = 1;
742 			break;
743 		}
744 
745 		/*
746 		 * Promote the signal from pending to current.
747 		 *
748 		 * Note that sigdeq() will set lwp->lwp_curinfo to NULL
749 		 * if no siginfo_t exists for this signal.
750 		 */
751 		lwp->lwp_cursig = (uchar_t)sig;
752 		lwp->lwp_extsig = (uchar_t)ext;
753 		t->t_sig_check = 1;	/* so post_syscall will see signal */
754 		ASSERT(lwp->lwp_curinfo == NULL);
755 		sigdeq(p, toproc ? NULL : t, sig, &lwp->lwp_curinfo);
756 
757 		if (tracing(p, sig))
758 			stop(PR_SIGNALLED, sig);
759 
760 		/*
761 		 * Loop around to check for requested stop before
762 		 * performing the usual current-signal actions.
763 		 */
764 	}
765 
766 	mutex_exit(&p->p_lock);
767 
768 	/*
769 	 * If SIGCLD was dequeued, search for other pending SIGCLD's.
770 	 * Don't do it if we are returning SIGCLD and the signal
771 	 * handler will be reset by psig(); this enables reliable
772 	 * delivery of SIGCLD even when using the old, broken
773 	 * signal() interface for setting the signal handler.
774 	 */
775 	if (sigcld_found &&
776 	    (sig != SIGCLD || !sigismember(&PTOU(curproc)->u_sigresethand,
777 	    SIGCLD)))
778 		sigcld_repost();
779 
780 	if (sig != 0)
781 		(void) undo_watch_step(NULL);
782 
783 	/*
784 	 * If we have been blocked since the p_lock was dropped off
785 	 * above, then this promoted signal might have been handled
786 	 * already when we were on the way back from sleep queue, so
787 	 * just ignore it.
788 	 * If we have been informed not to stop, just return the signal
789 	 * number. Also see comments above.
790 	 */
791 	if (!nostop_break) {
792 		sig = lwp->lwp_cursig;
793 	}
794 
795 	return (sig != 0);
796 }
797 
798 /*
799  * Return true if the process is currently stopped showing PR_JOBCONTROL.
800  * This is true only if all of the process's lwp's are so stopped.
801  * If this is asked by one of the lwps in the process, exclude that lwp.
802  */
803 int
804 jobstopped(proc_t *p)
805 {
806 	kthread_t *t;
807 
808 	ASSERT(MUTEX_HELD(&p->p_lock));
809 
810 	if ((t = p->p_tlist) == NULL)
811 		return (0);
812 
813 	do {
814 		thread_lock(t);
815 		/* ignore current, zombie and suspended lwps in the test */
816 		if (!(t == curthread || t->t_state == TS_ZOMB ||
817 		    SUSPENDED(t)) &&
818 		    (t->t_state != TS_STOPPED ||
819 		    t->t_whystop != PR_JOBCONTROL)) {
820 			thread_unlock(t);
821 			return (0);
822 		}
823 		thread_unlock(t);
824 	} while ((t = t->t_forw) != p->p_tlist);
825 
826 	return (1);
827 }
828 
829 /*
830  * Put ourself (curthread) into the stopped state and notify tracers.
831  */
832 void
833 stop(int why, int what)
834 {
835 	kthread_t	*t = curthread;
836 	proc_t		*p = ttoproc(t);
837 	klwp_t		*lwp = ttolwp(t);
838 	kthread_t	*tx;
839 	lwpent_t	*lep;
840 	int		procstop;
841 	int		flags = TS_ALLSTART;
842 	hrtime_t	stoptime;
843 
844 	/*
845 	 * Can't stop a system process.
846 	 */
847 	if (p == NULL || lwp == NULL || (p->p_flag & SSYS) || p->p_as == &kas)
848 		return;
849 
850 	ASSERT(MUTEX_HELD(&p->p_lock));
851 
852 	if (why != PR_SUSPENDED && why != PR_CHECKPOINT) {
853 		/*
854 		 * Don't stop an lwp with SIGKILL pending.
855 		 * Don't stop if the process or lwp is exiting.
856 		 */
857 		if (lwp->lwp_cursig == SIGKILL ||
858 		    sigismember(&t->t_sig, SIGKILL) ||
859 		    sigismember(&p->p_sig, SIGKILL) ||
860 		    (t->t_proc_flag & TP_LWPEXIT) ||
861 		    (p->p_flag & (SEXITLWPS|SKILLED))) {
862 			p->p_stopsig = 0;
863 			t->t_proc_flag &= ~(TP_PRSTOP|TP_PRVSTOP);
864 			return;
865 		}
866 	}
867 
868 	/*
869 	 * Make sure we don't deadlock on a recursive call to prstop().
870 	 * prstop() sets the lwp_nostop flag.
871 	 */
872 	if (lwp->lwp_nostop)
873 		return;
874 
875 	/*
876 	 * Make sure the lwp is in an orderly state for inspection
877 	 * by a debugger through /proc or for dumping via core().
878 	 */
879 	schedctl_finish_sigblock(t);
880 	t->t_proc_flag |= TP_STOPPING;	/* must set before dropping p_lock */
881 	mutex_exit(&p->p_lock);
882 	stoptime = gethrtime();
883 	prstop(why, what);
884 	(void) undo_watch_step(NULL);
885 	mutex_enter(&p->p_lock);
886 	ASSERT(t->t_state == TS_ONPROC);
887 
888 	switch (why) {
889 	case PR_CHECKPOINT:
890 		/*
891 		 * The situation may have changed since we dropped
892 		 * and reacquired p->p_lock. Double-check now
893 		 * whether we should stop or not.
894 		 */
895 		if (!(t->t_proc_flag & TP_CHKPT)) {
896 			t->t_proc_flag &= ~TP_STOPPING;
897 			return;
898 		}
899 		t->t_proc_flag &= ~TP_CHKPT;
900 		flags &= ~TS_RESUME;
901 		break;
902 
903 	case PR_JOBCONTROL:
904 		ASSERT(what == SIGSTOP || what == SIGTSTP ||
905 		    what == SIGTTIN || what == SIGTTOU);
906 		flags &= ~TS_XSTART;
907 		break;
908 
909 	case PR_SUSPENDED:
910 		ASSERT(what == SUSPEND_NORMAL || what == SUSPEND_PAUSE);
911 		/*
912 		 * The situation may have changed since we dropped
913 		 * and reacquired p->p_lock.  Double-check now
914 		 * whether we should stop or not.
915 		 */
916 		if (what == SUSPEND_PAUSE) {
917 			if (!(t->t_proc_flag & TP_PAUSE)) {
918 				t->t_proc_flag &= ~TP_STOPPING;
919 				return;
920 			}
921 			flags &= ~TS_UNPAUSE;
922 		} else {
923 			if (!((t->t_proc_flag & TP_HOLDLWP) ||
924 			    (p->p_flag & (SHOLDFORK|SHOLDFORK1|SHOLDWATCH)))) {
925 				t->t_proc_flag &= ~TP_STOPPING;
926 				return;
927 			}
928 			/*
929 			 * If SHOLDFORK is in effect and we are stopping
930 			 * while asleep (not at the top of the stack),
931 			 * we return now to allow the hold to take effect
932 			 * when we reach the top of the kernel stack.
933 			 */
934 			if (lwp->lwp_asleep && (p->p_flag & SHOLDFORK)) {
935 				t->t_proc_flag &= ~TP_STOPPING;
936 				return;
937 			}
938 			flags &= ~TS_CSTART;
939 		}
940 		break;
941 
942 	default:	/* /proc stop */
943 		flags &= ~TS_PSTART;
944 		/*
945 		 * Do synchronous stop unless the async-stop flag is set.
946 		 * If why is PR_REQUESTED and t->t_dtrace_stop flag is set,
947 		 * then no debugger is present and we also do synchronous stop.
948 		 */
949 		if ((why != PR_REQUESTED || t->t_dtrace_stop) &&
950 		    !(p->p_proc_flag & P_PR_ASYNC)) {
951 			int notify;
952 
953 			for (tx = t->t_forw; tx != t; tx = tx->t_forw) {
954 				notify = 0;
955 				thread_lock(tx);
956 				if (ISTOPPED(tx) ||
957 				    (tx->t_proc_flag & TP_PRSTOP)) {
958 					thread_unlock(tx);
959 					continue;
960 				}
961 				tx->t_proc_flag |= TP_PRSTOP;
962 				tx->t_sig_check = 1;
963 				if (tx->t_state == TS_SLEEP &&
964 				    (tx->t_flag & T_WAKEABLE)) {
965 					/*
966 					 * Don't actually wake it up if it's
967 					 * in one of the lwp_*() syscalls.
968 					 * Mark it virtually stopped and
969 					 * notify /proc waiters (below).
970 					 */
971 					if (tx->t_wchan0 == NULL)
972 						setrun_locked(tx);
973 					else {
974 						tx->t_proc_flag |= TP_PRVSTOP;
975 						tx->t_stoptime = stoptime;
976 						notify = 1;
977 					}
978 				}
979 
980 				/* Move waiting thread to run queue */
981 				if (ISWAITING(tx))
982 					setrun_locked(tx);
983 
984 				/*
985 				 * force the thread into the kernel
986 				 * if it is not already there.
987 				 */
988 				if (tx->t_state == TS_ONPROC &&
989 				    tx->t_cpu != CPU)
990 					poke_cpu(tx->t_cpu->cpu_id);
991 				thread_unlock(tx);
992 				lep = p->p_lwpdir[tx->t_dslot].ld_entry;
993 				if (notify && lep->le_trace)
994 					prnotify(lep->le_trace);
995 			}
996 			/*
997 			 * We do this just in case one of the threads we asked
998 			 * to stop is in holdlwps() (called from cfork()) or
999 			 * lwp_suspend().
1000 			 */
1001 			cv_broadcast(&p->p_holdlwps);
1002 		}
1003 		break;
1004 	}
1005 
1006 	t->t_stoptime = stoptime;
1007 
1008 	if (why == PR_JOBCONTROL || (why == PR_SUSPENDED && p->p_stopsig)) {
1009 		/*
1010 		 * Determine if the whole process is jobstopped.
1011 		 */
1012 		if (jobstopped(p)) {
1013 			sigqueue_t *sqp;
1014 			int sig;
1015 
1016 			if ((sig = p->p_stopsig) == 0)
1017 				p->p_stopsig = (uchar_t)(sig = what);
1018 			mutex_exit(&p->p_lock);
1019 			sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
1020 			mutex_enter(&pidlock);
1021 			/*
1022 			 * The last lwp to stop notifies the parent.
1023 			 * Turn off the CLDCONT flag now so the first
1024 			 * lwp to continue knows what to do.
1025 			 */
1026 			p->p_pidflag &= ~CLDCONT;
1027 			p->p_wcode = CLD_STOPPED;
1028 			p->p_wdata = sig;
1029 			sigcld(p, sqp);
1030 			/*
1031 			 * Grab p->p_lock before releasing pidlock so the
1032 			 * parent and the child don't have a race condition.
1033 			 */
1034 			mutex_enter(&p->p_lock);
1035 			mutex_exit(&pidlock);
1036 			p->p_stopsig = 0;
1037 		} else if (why == PR_JOBCONTROL && p->p_stopsig == 0) {
1038 			/*
1039 			 * Set p->p_stopsig and wake up sleeping lwps
1040 			 * so they will stop in sympathy with this lwp.
1041 			 */
1042 			p->p_stopsig = (uchar_t)what;
1043 			pokelwps(p);
1044 			/*
1045 			 * We do this just in case one of the threads we asked
1046 			 * to stop is in holdlwps() (called from cfork()) or
1047 			 * lwp_suspend().
1048 			 */
1049 			cv_broadcast(&p->p_holdlwps);
1050 		}
1051 	}
1052 
1053 	if (why != PR_JOBCONTROL && why != PR_CHECKPOINT) {
1054 		/*
1055 		 * Do process-level notification when all lwps are
1056 		 * either stopped on events of interest to /proc
1057 		 * or are stopped showing PR_SUSPENDED or are zombies.
1058 		 */
1059 		procstop = 1;
1060 		for (tx = t->t_forw; procstop && tx != t; tx = tx->t_forw) {
1061 			if (VSTOPPED(tx))
1062 				continue;
1063 			thread_lock(tx);
1064 			switch (tx->t_state) {
1065 			case TS_ZOMB:
1066 				break;
1067 			case TS_STOPPED:
1068 				/* neither ISTOPPED nor SUSPENDED? */
1069 				if ((tx->t_schedflag &
1070 				    (TS_CSTART | TS_UNPAUSE | TS_PSTART)) ==
1071 				    (TS_CSTART | TS_UNPAUSE | TS_PSTART))
1072 					procstop = 0;
1073 				break;
1074 			case TS_SLEEP:
1075 				/* not paused for watchpoints? */
1076 				if (!(tx->t_flag & T_WAKEABLE) ||
1077 				    tx->t_wchan0 == NULL ||
1078 				    !(tx->t_proc_flag & TP_PAUSE))
1079 					procstop = 0;
1080 				break;
1081 			default:
1082 				procstop = 0;
1083 				break;
1084 			}
1085 			thread_unlock(tx);
1086 		}
1087 		if (procstop) {
1088 			/* there must not be any remapped watched pages now */
1089 			ASSERT(p->p_mapcnt == 0);
1090 			if (p->p_proc_flag & P_PR_PTRACE) {
1091 				/* ptrace() compatibility */
1092 				mutex_exit(&p->p_lock);
1093 				mutex_enter(&pidlock);
1094 				p->p_wcode = CLD_TRAPPED;
1095 				p->p_wdata = (why == PR_SIGNALLED)?
1096 				    what : SIGTRAP;
1097 				cv_broadcast(&p->p_parent->p_cv);
1098 				/*
1099 				 * Grab p->p_lock before releasing pidlock so
1100 				 * parent and child don't have a race condition.
1101 				 */
1102 				mutex_enter(&p->p_lock);
1103 				mutex_exit(&pidlock);
1104 			}
1105 			if (p->p_trace)			/* /proc */
1106 				prnotify(p->p_trace);
1107 			cv_broadcast(&pr_pid_cv[p->p_slot]); /* pauselwps() */
1108 			cv_broadcast(&p->p_holdlwps);	/* holdwatch() */
1109 		}
1110 		if (why != PR_SUSPENDED) {
1111 			lep = p->p_lwpdir[t->t_dslot].ld_entry;
1112 			if (lep->le_trace)		/* /proc */
1113 				prnotify(lep->le_trace);
1114 			/*
1115 			 * Special notification for creation of the agent lwp.
1116 			 */
1117 			if (t == p->p_agenttp &&
1118 			    (t->t_proc_flag & TP_PRSTOP) &&
1119 			    p->p_trace)
1120 				prnotify(p->p_trace);
1121 			/*
1122 			 * The situation may have changed since we dropped
1123 			 * and reacquired p->p_lock. Double-check now
1124 			 * whether we should stop or not.
1125 			 */
1126 			if (!(t->t_proc_flag & TP_STOPPING)) {
1127 				if (t->t_proc_flag & TP_PRSTOP)
1128 					t->t_proc_flag |= TP_STOPPING;
1129 			}
1130 			t->t_proc_flag &= ~(TP_PRSTOP|TP_PRVSTOP);
1131 			prnostep(lwp);
1132 		}
1133 	}
1134 
1135 	if (why == PR_SUSPENDED) {
1136 
1137 		/*
1138 		 * We always broadcast in the case of SUSPEND_PAUSE.  This is
1139 		 * because checks for TP_PAUSE take precedence over checks for
1140 		 * SHOLDWATCH.  If a thread is trying to stop because of
1141 		 * SUSPEND_PAUSE and tries to do a holdwatch(), it will be
1142 		 * waiting for the rest of the threads to enter a stopped state.
1143 		 * If we are stopping for a SUSPEND_PAUSE, we may be the last
1144 		 * lwp and not know it, so broadcast just in case.
1145 		 */
1146 		if (what == SUSPEND_PAUSE ||
1147 		    --p->p_lwprcnt == 0 || (t->t_proc_flag & TP_HOLDLWP))
1148 			cv_broadcast(&p->p_holdlwps);
1149 
1150 	}
1151 
1152 	/*
1153 	 * Need to do this here (rather than after the thread is officially
1154 	 * stopped) because we can't call mutex_enter from a stopped thread.
1155 	 */
1156 	if (why == PR_CHECKPOINT)
1157 		del_one_utstop();
1158 
1159 	thread_lock(t);
1160 	ASSERT((t->t_schedflag & TS_ALLSTART) == 0);
1161 	t->t_schedflag |= flags;
1162 	t->t_whystop = (short)why;
1163 	t->t_whatstop = (short)what;
1164 	CL_STOP(t, why, what);
1165 	(void) new_mstate(t, LMS_STOPPED);
1166 	thread_stop(t);			/* set stop state and drop lock */
1167 
1168 	if (why != PR_SUSPENDED && why != PR_CHECKPOINT) {
1169 		/*
1170 		 * We may have gotten a SIGKILL or a SIGCONT when
1171 		 * we released p->p_lock; make one last check.
1172 		 * Also check for a /proc run-on-last-close.
1173 		 */
1174 		if (sigismember(&t->t_sig, SIGKILL) ||
1175 		    sigismember(&p->p_sig, SIGKILL) ||
1176 		    (t->t_proc_flag & TP_LWPEXIT) ||
1177 		    (p->p_flag & (SEXITLWPS|SKILLED))) {
1178 			p->p_stopsig = 0;
1179 			thread_lock(t);
1180 			t->t_schedflag |= TS_XSTART | TS_PSTART;
1181 			setrun_locked(t);
1182 			thread_unlock_nopreempt(t);
1183 		} else if (why == PR_JOBCONTROL) {
1184 			if (p->p_flag & SSCONT) {
1185 				/*
1186 				 * This resulted from a SIGCONT posted
1187 				 * while we were not holding p->p_lock.
1188 				 */
1189 				p->p_stopsig = 0;
1190 				thread_lock(t);
1191 				t->t_schedflag |= TS_XSTART;
1192 				setrun_locked(t);
1193 				thread_unlock_nopreempt(t);
1194 			}
1195 		} else if (!(t->t_proc_flag & TP_STOPPING)) {
1196 			/*
1197 			 * This resulted from a /proc run-on-last-close.
1198 			 */
1199 			thread_lock(t);
1200 			t->t_schedflag |= TS_PSTART;
1201 			setrun_locked(t);
1202 			thread_unlock_nopreempt(t);
1203 		}
1204 	}
1205 
1206 	t->t_proc_flag &= ~TP_STOPPING;
1207 	mutex_exit(&p->p_lock);
1208 
1209 	swtch();
1210 	setallwatch();	/* reestablish any watchpoints set while stopped */
1211 	mutex_enter(&p->p_lock);
1212 	prbarrier(p);	/* barrier against /proc locking */
1213 }
1214 
1215 /* Interface for resetting user thread stop count. */
1216 void
1217 utstop_init(void)
1218 {
1219 	mutex_enter(&thread_stop_lock);
1220 	num_utstop = 0;
1221 	mutex_exit(&thread_stop_lock);
1222 }
1223 
1224 /* Interface for registering a user thread stop request. */
1225 void
1226 add_one_utstop(void)
1227 {
1228 	mutex_enter(&thread_stop_lock);
1229 	num_utstop++;
1230 	mutex_exit(&thread_stop_lock);
1231 }
1232 
1233 /* Interface for cancelling a user thread stop request */
1234 void
1235 del_one_utstop(void)
1236 {
1237 	mutex_enter(&thread_stop_lock);
1238 	num_utstop--;
1239 	if (num_utstop == 0)
1240 		cv_broadcast(&utstop_cv);
1241 	mutex_exit(&thread_stop_lock);
1242 }
1243 
1244 /* Interface to wait for all user threads to be stopped */
1245 void
1246 utstop_timedwait(clock_t ticks)
1247 {
1248 	mutex_enter(&thread_stop_lock);
1249 	if (num_utstop > 0)
1250 		(void) cv_timedwait(&utstop_cv, &thread_stop_lock,
1251 		    ticks + lbolt);
1252 	mutex_exit(&thread_stop_lock);
1253 }
1254 
1255 /*
1256  * Perform the action specified by the current signal.
1257  * The usual sequence is:
1258  * 	if (issig())
1259  * 		psig();
1260  * The signal bit has already been cleared by issig(),
1261  * the current signal number has been stored in lwp_cursig,
1262  * and the current siginfo is now referenced by lwp_curinfo.
1263  */
1264 void
1265 psig(void)
1266 {
1267 	kthread_t *t = curthread;
1268 	proc_t *p = ttoproc(t);
1269 	klwp_t *lwp = ttolwp(t);
1270 	void (*func)();
1271 	int sig, rc, code, ext;
1272 	pid_t pid = -1;
1273 	id_t ctid = 0;
1274 	zoneid_t zoneid = -1;
1275 	sigqueue_t *sqp = NULL;
1276 
1277 	mutex_enter(&p->p_lock);
1278 	schedctl_finish_sigblock(t);
1279 	code = CLD_KILLED;
1280 
1281 	if (p->p_flag & SEXITLWPS) {
1282 		lwp_exit();
1283 		return;			/* not reached */
1284 	}
1285 	sig = lwp->lwp_cursig;
1286 	ext = lwp->lwp_extsig;
1287 
1288 	ASSERT(sig < NSIG);
1289 
1290 	/*
1291 	 * Re-check lwp_cursig after we acquire p_lock.  Since p_lock was
1292 	 * dropped between issig() and psig(), a debugger may have cleared
1293 	 * lwp_cursig via /proc in the intervening window.
1294 	 */
1295 	if (sig == 0) {
1296 		if (lwp->lwp_curinfo) {
1297 			siginfofree(lwp->lwp_curinfo);
1298 			lwp->lwp_curinfo = NULL;
1299 		}
1300 		if (t->t_flag & T_TOMASK) {	/* sigsuspend or pollsys */
1301 			t->t_flag &= ~T_TOMASK;
1302 			t->t_hold = lwp->lwp_sigoldmask;
1303 		}
1304 		mutex_exit(&p->p_lock);
1305 		return;
1306 	}
1307 	func = PTOU(curproc)->u_signal[sig-1];
1308 
1309 	/*
1310 	 * The signal disposition could have changed since we promoted
1311 	 * this signal from pending to current (we dropped p->p_lock).
1312 	 * This can happen only in a multi-threaded process.
1313 	 */
1314 	if (sigismember(&p->p_ignore, sig) ||
1315 	    (func == SIG_DFL && sigismember(&stopdefault, sig))) {
1316 		lwp->lwp_cursig = 0;
1317 		lwp->lwp_extsig = 0;
1318 		if (lwp->lwp_curinfo) {
1319 			siginfofree(lwp->lwp_curinfo);
1320 			lwp->lwp_curinfo = NULL;
1321 		}
1322 		if (t->t_flag & T_TOMASK) {	/* sigsuspend or pollsys */
1323 			t->t_flag &= ~T_TOMASK;
1324 			t->t_hold = lwp->lwp_sigoldmask;
1325 		}
1326 		mutex_exit(&p->p_lock);
1327 		return;
1328 	}
1329 
1330 	/*
1331 	 * We check lwp_curinfo first since pr_setsig can actually
1332 	 * stuff a sigqueue_t there for SIGKILL.
1333 	 */
1334 	if (lwp->lwp_curinfo) {
1335 		sqp = lwp->lwp_curinfo;
1336 	} else if (sig == SIGKILL && p->p_killsqp) {
1337 		sqp = p->p_killsqp;
1338 	}
1339 
1340 	if (sqp != NULL) {
1341 		if (SI_FROMUSER(&sqp->sq_info)) {
1342 			pid = sqp->sq_info.si_pid;
1343 			ctid = sqp->sq_info.si_ctid;
1344 			zoneid = sqp->sq_info.si_zoneid;
1345 		}
1346 		/*
1347 		 * If we have a sigqueue_t, its sq_external value
1348 		 * trumps the lwp_extsig value.  It is theoretically
1349 		 * possible to make lwp_extsig reflect reality, but it
1350 		 * would unnecessarily complicate things elsewhere.
1351 		 */
1352 		ext = sqp->sq_external;
1353 	}
1354 
1355 	if (func == SIG_DFL) {
1356 		mutex_exit(&p->p_lock);
1357 		DTRACE_PROC3(signal__handle, int, sig, k_siginfo_t *,
1358 		    NULL, void (*)(void), func);
1359 	} else {
1360 		k_siginfo_t *sip = NULL;
1361 
1362 		/*
1363 		 * If DTrace user-land tracing is active, give DTrace a
1364 		 * chance to defer the signal until after tracing is
1365 		 * complete.
1366 		 */
1367 		if (t->t_dtrace_on && dtrace_safe_defer_signal()) {
1368 			mutex_exit(&p->p_lock);
1369 			return;
1370 		}
1371 
1372 		/*
1373 		 * save siginfo pointer here, in case the
1374 		 * the signal's reset bit is on
1375 		 *
1376 		 * The presence of a current signal prevents paging
1377 		 * from succeeding over a network.  We copy the current
1378 		 * signal information to the side and cancel the current
1379 		 * signal so that sendsig() will succeed.
1380 		 */
1381 		if (sigismember(&p->p_siginfo, sig)) {
1382 			sip = &lwp->lwp_siginfo;
1383 			if (sqp) {
1384 				bcopy(&sqp->sq_info, sip, sizeof (*sip));
1385 				/*
1386 				 * If we were interrupted out of a system call
1387 				 * due to pthread_cancel(), inform libc.
1388 				 */
1389 				if (sig == SIGCANCEL &&
1390 				    sip->si_code == SI_LWP &&
1391 				    t->t_sysnum != 0)
1392 					schedctl_cancel_eintr();
1393 			} else if (sig == SIGPROF && sip->si_signo == SIGPROF &&
1394 			    t->t_rprof != NULL && t->t_rprof->rp_anystate) {
1395 				/* EMPTY */;
1396 			} else {
1397 				bzero(sip, sizeof (*sip));
1398 				sip->si_signo = sig;
1399 				sip->si_code = SI_NOINFO;
1400 			}
1401 		}
1402 
1403 		if (t->t_flag & T_TOMASK)
1404 			t->t_flag &= ~T_TOMASK;
1405 		else
1406 			lwp->lwp_sigoldmask = t->t_hold;
1407 		sigorset(&t->t_hold, &PTOU(curproc)->u_sigmask[sig-1]);
1408 		if (!sigismember(&PTOU(curproc)->u_signodefer, sig))
1409 			sigaddset(&t->t_hold, sig);
1410 		if (sigismember(&PTOU(curproc)->u_sigresethand, sig))
1411 			setsigact(sig, SIG_DFL, nullsmask, 0);
1412 
1413 		DTRACE_PROC3(signal__handle, int, sig, k_siginfo_t *,
1414 		    sip, void (*)(void), func);
1415 
1416 		lwp->lwp_cursig = 0;
1417 		lwp->lwp_extsig = 0;
1418 		if (lwp->lwp_curinfo) {
1419 			/* p->p_killsqp is freed by freeproc */
1420 			siginfofree(lwp->lwp_curinfo);
1421 			lwp->lwp_curinfo = NULL;
1422 		}
1423 		mutex_exit(&p->p_lock);
1424 		lwp->lwp_ru.nsignals++;
1425 
1426 		if (p->p_model == DATAMODEL_NATIVE)
1427 			rc = sendsig(sig, sip, func);
1428 #ifdef _SYSCALL32_IMPL
1429 		else
1430 			rc = sendsig32(sig, sip, func);
1431 #endif	/* _SYSCALL32_IMPL */
1432 		if (rc)
1433 			return;
1434 		sig = lwp->lwp_cursig = SIGSEGV;
1435 		ext = 0;	/* lwp_extsig was set above */
1436 		pid = -1;
1437 		ctid = 0;
1438 	}
1439 
1440 	if (sigismember(&coredefault, sig)) {
1441 		/*
1442 		 * Terminate all LWPs but don't discard them.
1443 		 * If another lwp beat us to the punch by calling exit(),
1444 		 * evaporate now.
1445 		 */
1446 		proc_is_exiting(p);
1447 		if (exitlwps(1) != 0) {
1448 			mutex_enter(&p->p_lock);
1449 			lwp_exit();
1450 		}
1451 		/* if we got a SIGKILL from anywhere, no core dump */
1452 		if (p->p_flag & SKILLED) {
1453 			sig = SIGKILL;
1454 			ext = (p->p_flag & SEXTKILLED) != 0;
1455 		} else {
1456 			if (audit_active)		/* audit core dump */
1457 				audit_core_start(sig);
1458 			if (core(sig, ext) == 0)
1459 				code = CLD_DUMPED;
1460 			if (audit_active)		/* audit core dump */
1461 				audit_core_finish(code);
1462 		}
1463 	}
1464 
1465 	/*
1466 	 * Generate a contract event once if the process is killed
1467 	 * by a signal.
1468 	 */
1469 	if (ext) {
1470 		proc_is_exiting(p);
1471 		if (exitlwps(0) != 0) {
1472 			mutex_enter(&p->p_lock);
1473 			lwp_exit();
1474 		}
1475 		contract_process_sig(p->p_ct_process, p, sig, pid, ctid,
1476 		    zoneid);
1477 	}
1478 
1479 	exit(code, sig);
1480 }
1481 
1482 /*
1483  * Find next unheld signal in ssp for thread t.
1484  */
1485 int
1486 fsig(k_sigset_t *ssp, kthread_t *t)
1487 {
1488 	proc_t *p = ttoproc(t);
1489 	user_t *up = PTOU(p);
1490 	int i;
1491 	k_sigset_t temp;
1492 
1493 	ASSERT(MUTEX_HELD(&p->p_lock));
1494 
1495 	/*
1496 	 * Don't promote any signals for the parent of a vfork()d
1497 	 * child that hasn't yet released the parent's memory.
1498 	 */
1499 	if (p->p_flag & SVFWAIT)
1500 		return (0);
1501 
1502 	temp = *ssp;
1503 	sigdiffset(&temp, &t->t_hold);
1504 
1505 	/*
1506 	 * Don't promote stopping signals (except SIGSTOP) for a child
1507 	 * of vfork() that hasn't yet released the parent's memory.
1508 	 */
1509 	if (p->p_flag & SVFORK)
1510 		sigdiffset(&temp, &holdvfork);
1511 
1512 	/*
1513 	 * Don't promote a signal that will stop
1514 	 * the process when lwp_nostop is set.
1515 	 */
1516 	if (ttolwp(t)->lwp_nostop) {
1517 		sigdelset(&temp, SIGSTOP);
1518 		if (!p->p_pgidp->pid_pgorphaned) {
1519 			if (up->u_signal[SIGTSTP-1] == SIG_DFL)
1520 				sigdelset(&temp, SIGTSTP);
1521 			if (up->u_signal[SIGTTIN-1] == SIG_DFL)
1522 				sigdelset(&temp, SIGTTIN);
1523 			if (up->u_signal[SIGTTOU-1] == SIG_DFL)
1524 				sigdelset(&temp, SIGTTOU);
1525 		}
1526 	}
1527 
1528 	/*
1529 	 * Choose SIGKILL and SIGPROF before all other pending signals.
1530 	 * The rest are promoted in signal number order.
1531 	 */
1532 	if (sigismember(&temp, SIGKILL))
1533 		return (SIGKILL);
1534 	if (sigismember(&temp, SIGPROF))
1535 		return (SIGPROF);
1536 
1537 	for (i = 0; i < sizeof (temp) / sizeof (temp.__sigbits[0]); i++) {
1538 		if (temp.__sigbits[i])
1539 			return ((i * NBBY * sizeof (temp.__sigbits[0])) +
1540 			    lowbit(temp.__sigbits[i]));
1541 	}
1542 
1543 	return (0);
1544 }
1545 
1546 void
1547 setsigact(int sig, void (*disp)(), k_sigset_t mask, int flags)
1548 {
1549 	proc_t *p = ttoproc(curthread);
1550 	kthread_t *t;
1551 
1552 	ASSERT(MUTEX_HELD(&p->p_lock));
1553 
1554 	PTOU(curproc)->u_signal[sig - 1] = disp;
1555 
1556 	/*
1557 	 * Honor the SA_SIGINFO flag if the signal is being caught.
1558 	 * Force the SA_SIGINFO flag if the signal is not being caught.
1559 	 * This is necessary to make sigqueue() and sigwaitinfo() work
1560 	 * properly together when the signal is set to default or is
1561 	 * being temporarily ignored.
1562 	 */
1563 	if ((flags & SA_SIGINFO) || disp == SIG_DFL || disp == SIG_IGN)
1564 		sigaddset(&p->p_siginfo, sig);
1565 	else
1566 		sigdelset(&p->p_siginfo, sig);
1567 
1568 	if (disp != SIG_DFL && disp != SIG_IGN) {
1569 		sigdelset(&p->p_ignore, sig);
1570 		PTOU(curproc)->u_sigmask[sig - 1] = mask;
1571 		if (!sigismember(&cantreset, sig)) {
1572 			if (flags & SA_RESETHAND)
1573 				sigaddset(&PTOU(curproc)->u_sigresethand, sig);
1574 			else
1575 				sigdelset(&PTOU(curproc)->u_sigresethand, sig);
1576 		}
1577 		if (flags & SA_NODEFER)
1578 			sigaddset(&PTOU(curproc)->u_signodefer, sig);
1579 		else
1580 			sigdelset(&PTOU(curproc)->u_signodefer, sig);
1581 		if (flags & SA_RESTART)
1582 			sigaddset(&PTOU(curproc)->u_sigrestart, sig);
1583 		else
1584 			sigdelset(&PTOU(curproc)->u_sigrestart, sig);
1585 		if (flags & SA_ONSTACK)
1586 			sigaddset(&PTOU(curproc)->u_sigonstack, sig);
1587 		else
1588 			sigdelset(&PTOU(curproc)->u_sigonstack, sig);
1589 
1590 	} else if (disp == SIG_IGN ||
1591 	    (disp == SIG_DFL && sigismember(&ignoredefault, sig))) {
1592 		/*
1593 		 * Setting the signal action to SIG_IGN results in the
1594 		 * discarding of all pending signals of that signal number.
1595 		 * Setting the signal action to SIG_DFL does the same *only*
1596 		 * if the signal's default behavior is to be ignored.
1597 		 */
1598 		sigaddset(&p->p_ignore, sig);
1599 		sigdelset(&p->p_sig, sig);
1600 		sigdelset(&p->p_extsig, sig);
1601 		sigdelq(p, NULL, sig);
1602 		t = p->p_tlist;
1603 		do {
1604 			sigdelset(&t->t_sig, sig);
1605 			sigdelset(&t->t_extsig, sig);
1606 			sigdelq(p, t, sig);
1607 		} while ((t = t->t_forw) != p->p_tlist);
1608 
1609 	} else {
1610 		/*
1611 		 * The signal action is being set to SIG_DFL and the default
1612 		 * behavior is to do something: make sure it is not ignored.
1613 		 */
1614 		sigdelset(&p->p_ignore, sig);
1615 	}
1616 
1617 	if (sig == SIGCLD) {
1618 		if (flags & SA_NOCLDWAIT)
1619 			p->p_flag |= SNOWAIT;
1620 		else
1621 			p->p_flag &= ~SNOWAIT;
1622 
1623 		if (flags & SA_NOCLDSTOP)
1624 			p->p_flag &= ~SJCTL;
1625 		else
1626 			p->p_flag |= SJCTL;
1627 
1628 		if ((p->p_flag & SNOWAIT) || disp == SIG_IGN) {
1629 			proc_t *cp, *tp;
1630 
1631 			mutex_exit(&p->p_lock);
1632 			mutex_enter(&pidlock);
1633 			for (cp = p->p_child; cp != NULL; cp = tp) {
1634 				tp = cp->p_sibling;
1635 				if (cp->p_stat == SZOMB &&
1636 				    !(cp->p_pidflag & CLDWAITPID))
1637 					freeproc(cp);
1638 			}
1639 			mutex_exit(&pidlock);
1640 			mutex_enter(&p->p_lock);
1641 		}
1642 	}
1643 }
1644 
1645 /*
1646  * Set all signal actions not already set to SIG_DFL or SIG_IGN to SIG_DFL.
1647  * Called from exec_common() for a process undergoing execve()
1648  * and from cfork() for a newly-created child of vfork().
1649  * In the vfork() case, 'p' is not the current process.
1650  * In both cases, there is only one thread in the process.
1651  */
1652 void
1653 sigdefault(proc_t *p)
1654 {
1655 	kthread_t *t = p->p_tlist;
1656 	struct user *up = PTOU(p);
1657 	int sig;
1658 
1659 	ASSERT(MUTEX_HELD(&p->p_lock));
1660 
1661 	for (sig = 1; sig < NSIG; sig++) {
1662 		if (up->u_signal[sig - 1] != SIG_DFL &&
1663 		    up->u_signal[sig - 1] != SIG_IGN) {
1664 			up->u_signal[sig - 1] = SIG_DFL;
1665 			sigemptyset(&up->u_sigmask[sig - 1]);
1666 			if (sigismember(&ignoredefault, sig)) {
1667 				sigdelq(p, NULL, sig);
1668 				sigdelq(p, t, sig);
1669 			}
1670 			if (sig == SIGCLD)
1671 				p->p_flag &= ~(SNOWAIT|SJCTL);
1672 		}
1673 	}
1674 	sigorset(&p->p_ignore, &ignoredefault);
1675 	sigfillset(&p->p_siginfo);
1676 	sigdiffset(&p->p_siginfo, &cantmask);
1677 	sigdiffset(&p->p_sig, &ignoredefault);
1678 	sigdiffset(&p->p_extsig, &ignoredefault);
1679 	sigdiffset(&t->t_sig, &ignoredefault);
1680 	sigdiffset(&t->t_extsig, &ignoredefault);
1681 }
1682 
1683 void
1684 sigcld(proc_t *cp, sigqueue_t *sqp)
1685 {
1686 	proc_t *pp = cp->p_parent;
1687 
1688 	ASSERT(MUTEX_HELD(&pidlock));
1689 
1690 	switch (cp->p_wcode) {
1691 	case CLD_EXITED:
1692 	case CLD_DUMPED:
1693 	case CLD_KILLED:
1694 		ASSERT(cp->p_stat == SZOMB);
1695 		/*
1696 		 * The broadcast on p_srwchan_cv is a kludge to
1697 		 * wakeup a possible thread in uadmin(A_SHUTDOWN).
1698 		 */
1699 		cv_broadcast(&cp->p_srwchan_cv);
1700 
1701 		/*
1702 		 * Add to newstate list of the parent
1703 		 */
1704 		add_ns(pp, cp);
1705 
1706 		cv_broadcast(&pp->p_cv);
1707 		if ((pp->p_flag & SNOWAIT) ||
1708 		    PTOU(pp)->u_signal[SIGCLD - 1] == SIG_IGN) {
1709 			if (!(cp->p_pidflag & CLDWAITPID))
1710 				freeproc(cp);
1711 		} else if (!(cp->p_pidflag & CLDNOSIGCHLD)) {
1712 			post_sigcld(cp, sqp);
1713 			sqp = NULL;
1714 		}
1715 		break;
1716 
1717 	case CLD_STOPPED:
1718 	case CLD_CONTINUED:
1719 		cv_broadcast(&pp->p_cv);
1720 		if (pp->p_flag & SJCTL) {
1721 			post_sigcld(cp, sqp);
1722 			sqp = NULL;
1723 		}
1724 		break;
1725 	}
1726 
1727 	if (sqp)
1728 		siginfofree(sqp);
1729 }
1730 
1731 /*
1732  * Common code called from sigcld() and issig_forreal()
1733  * Give the parent process a SIGCLD if it does not have one pending,
1734  * else mark the child process so a SIGCLD can be posted later.
1735  */
1736 static void
1737 post_sigcld(proc_t *cp, sigqueue_t *sqp)
1738 {
1739 	proc_t *pp = cp->p_parent;
1740 	void (*handler)() = PTOU(pp)->u_signal[SIGCLD - 1];
1741 	k_siginfo_t info;
1742 
1743 	ASSERT(MUTEX_HELD(&pidlock));
1744 	mutex_enter(&pp->p_lock);
1745 
1746 	/*
1747 	 * If a SIGCLD is pending, or if SIGCLD is not now being caught,
1748 	 * then just mark the child process so that its SIGCLD will
1749 	 * be posted later, when the first SIGCLD is taken off the
1750 	 * queue or when the parent is ready to receive it, if ever.
1751 	 */
1752 	if (handler == SIG_DFL || handler == SIG_IGN ||
1753 	    sigismember(&pp->p_sig, SIGCLD))
1754 		cp->p_pidflag |= CLDPEND;
1755 	else {
1756 		cp->p_pidflag &= ~CLDPEND;
1757 		if (sqp == NULL) {
1758 			/*
1759 			 * This can only happen when the parent is init.
1760 			 * (See call to sigcld(q, NULL) in exit().)
1761 			 * Use KM_NOSLEEP to avoid deadlock.
1762 			 */
1763 			ASSERT(pp == proc_init);
1764 			winfo(cp, &info, 0);
1765 			sigaddq(pp, NULL, &info, KM_NOSLEEP);
1766 		} else {
1767 			winfo(cp, &sqp->sq_info, 0);
1768 			sigaddqa(pp, NULL, sqp);
1769 			sqp = NULL;
1770 		}
1771 	}
1772 
1773 	mutex_exit(&pp->p_lock);
1774 
1775 	if (sqp)
1776 		siginfofree(sqp);
1777 }
1778 
1779 /*
1780  * Search for a child that has a pending SIGCLD for us, the parent.
1781  * The queue of SIGCLD signals is implied by the list of children.
1782  * We post the SIGCLD signals one at a time so they don't get lost.
1783  * When one is dequeued, another is enqueued, until there are no more.
1784  */
1785 void
1786 sigcld_repost()
1787 {
1788 	proc_t *pp = curproc;
1789 	proc_t *cp;
1790 	void (*handler)() = PTOU(pp)->u_signal[SIGCLD - 1];
1791 	sigqueue_t *sqp;
1792 
1793 	/*
1794 	 * Don't bother if SIGCLD is not now being caught.
1795 	 */
1796 	if (handler == SIG_DFL || handler == SIG_IGN)
1797 		return;
1798 
1799 	sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
1800 	mutex_enter(&pidlock);
1801 	for (cp = pp->p_child; cp; cp = cp->p_sibling) {
1802 		if (cp->p_pidflag & CLDPEND) {
1803 			post_sigcld(cp, sqp);
1804 			mutex_exit(&pidlock);
1805 			return;
1806 		}
1807 	}
1808 	mutex_exit(&pidlock);
1809 	kmem_free(sqp, sizeof (sigqueue_t));
1810 }
1811 
1812 /*
1813  * count number of sigqueue send by sigaddqa()
1814  */
1815 void
1816 sigqsend(int cmd, proc_t *p, kthread_t *t, sigqueue_t *sigqp)
1817 {
1818 	sigqhdr_t *sqh;
1819 
1820 	sqh = (sigqhdr_t *)sigqp->sq_backptr;
1821 	ASSERT(sqh);
1822 
1823 	mutex_enter(&sqh->sqb_lock);
1824 	sqh->sqb_sent++;
1825 	mutex_exit(&sqh->sqb_lock);
1826 
1827 	if (cmd == SN_SEND)
1828 		sigaddqa(p, t, sigqp);
1829 	else
1830 		siginfofree(sigqp);
1831 }
1832 
1833 int
1834 sigsendproc(proc_t *p, sigsend_t *pv)
1835 {
1836 	struct cred *cr;
1837 	proc_t *myprocp = curproc;
1838 
1839 	ASSERT(MUTEX_HELD(&pidlock));
1840 
1841 	if (p->p_pid == 1 && pv->sig && sigismember(&cantmask, pv->sig))
1842 		return (EPERM);
1843 
1844 	cr = CRED();
1845 
1846 	if (pv->checkperm == 0 ||
1847 	    (pv->sig == SIGCONT && p->p_sessp == myprocp->p_sessp) ||
1848 	    prochasprocperm(p, myprocp, cr)) {
1849 		pv->perm++;
1850 		if (pv->sig) {
1851 			/* Make sure we should be setting si_pid and friends */
1852 			ASSERT(pv->sicode <= 0);
1853 			if (SI_CANQUEUE(pv->sicode)) {
1854 				sigqueue_t *sqp;
1855 
1856 				mutex_enter(&myprocp->p_lock);
1857 				sqp = sigqalloc(myprocp->p_sigqhdr);
1858 				mutex_exit(&myprocp->p_lock);
1859 				if (sqp == NULL)
1860 					return (EAGAIN);
1861 				sqp->sq_info.si_signo = pv->sig;
1862 				sqp->sq_info.si_code = pv->sicode;
1863 				sqp->sq_info.si_pid = myprocp->p_pid;
1864 				sqp->sq_info.si_ctid = PRCTID(myprocp);
1865 				sqp->sq_info.si_zoneid = getzoneid();
1866 				sqp->sq_info.si_uid = crgetruid(cr);
1867 				sqp->sq_info.si_value = pv->value;
1868 				mutex_enter(&p->p_lock);
1869 				sigqsend(SN_SEND, p, NULL, sqp);
1870 				mutex_exit(&p->p_lock);
1871 			} else {
1872 				k_siginfo_t info;
1873 				bzero(&info, sizeof (info));
1874 				info.si_signo = pv->sig;
1875 				info.si_code = pv->sicode;
1876 				info.si_pid = myprocp->p_pid;
1877 				info.si_ctid = PRCTID(myprocp);
1878 				info.si_zoneid = getzoneid();
1879 				info.si_uid = crgetruid(cr);
1880 				mutex_enter(&p->p_lock);
1881 				/*
1882 				 * XXX: Should be KM_SLEEP but
1883 				 * we have to avoid deadlock.
1884 				 */
1885 				sigaddq(p, NULL, &info, KM_NOSLEEP);
1886 				mutex_exit(&p->p_lock);
1887 			}
1888 		}
1889 	}
1890 
1891 	return (0);
1892 }
1893 
1894 int
1895 sigsendset(procset_t *psp, sigsend_t *pv)
1896 {
1897 	int error;
1898 
1899 	error = dotoprocs(psp, sigsendproc, (char *)pv);
1900 	if (error == 0 && pv->perm == 0)
1901 		return (EPERM);
1902 
1903 	return (error);
1904 }
1905 
1906 /*
1907  * Dequeue a queued siginfo structure.
1908  * If a non-null thread pointer is passed then dequeue from
1909  * the thread queue, otherwise dequeue from the process queue.
1910  */
1911 void
1912 sigdeq(proc_t *p, kthread_t *t, int sig, sigqueue_t **qpp)
1913 {
1914 	sigqueue_t **psqp, *sqp;
1915 
1916 	ASSERT(MUTEX_HELD(&p->p_lock));
1917 
1918 	*qpp = NULL;
1919 
1920 	if (t != NULL) {
1921 		sigdelset(&t->t_sig, sig);
1922 		sigdelset(&t->t_extsig, sig);
1923 		psqp = &t->t_sigqueue;
1924 	} else {
1925 		sigdelset(&p->p_sig, sig);
1926 		sigdelset(&p->p_extsig, sig);
1927 		psqp = &p->p_sigqueue;
1928 	}
1929 
1930 	for (;;) {
1931 		if ((sqp = *psqp) == NULL)
1932 			return;
1933 		if (sqp->sq_info.si_signo == sig)
1934 			break;
1935 		else
1936 			psqp = &sqp->sq_next;
1937 	}
1938 	*qpp = sqp;
1939 	*psqp = sqp->sq_next;
1940 	for (sqp = *psqp; sqp; sqp = sqp->sq_next) {
1941 		if (sqp->sq_info.si_signo == sig) {
1942 			if (t != (kthread_t *)NULL) {
1943 				sigaddset(&t->t_sig, sig);
1944 				t->t_sig_check = 1;
1945 			} else {
1946 				sigaddset(&p->p_sig, sig);
1947 				set_proc_ast(p);
1948 			}
1949 			break;
1950 		}
1951 	}
1952 }
1953 
1954 /*
1955  * Delete a queued SIGCLD siginfo structure matching the k_siginfo_t argument.
1956  */
1957 void
1958 sigcld_delete(k_siginfo_t *ip)
1959 {
1960 	proc_t *p = curproc;
1961 	int another_sigcld = 0;
1962 	sigqueue_t **psqp, *sqp;
1963 
1964 	ASSERT(ip->si_signo == SIGCLD);
1965 
1966 	mutex_enter(&p->p_lock);
1967 
1968 	if (!sigismember(&p->p_sig, SIGCLD)) {
1969 		mutex_exit(&p->p_lock);
1970 		return;
1971 	}
1972 
1973 	psqp = &p->p_sigqueue;
1974 	for (;;) {
1975 		if ((sqp = *psqp) == NULL) {
1976 			mutex_exit(&p->p_lock);
1977 			return;
1978 		}
1979 		if (sqp->sq_info.si_signo == SIGCLD) {
1980 			if (sqp->sq_info.si_pid == ip->si_pid &&
1981 			    sqp->sq_info.si_code == ip->si_code &&
1982 			    sqp->sq_info.si_status == ip->si_status)
1983 				break;
1984 			another_sigcld = 1;
1985 		}
1986 		psqp = &sqp->sq_next;
1987 	}
1988 	*psqp = sqp->sq_next;
1989 
1990 	siginfofree(sqp);
1991 
1992 	for (sqp = *psqp; !another_sigcld && sqp; sqp = sqp->sq_next) {
1993 		if (sqp->sq_info.si_signo == SIGCLD)
1994 			another_sigcld = 1;
1995 	}
1996 
1997 	if (!another_sigcld) {
1998 		sigdelset(&p->p_sig, SIGCLD);
1999 		sigdelset(&p->p_extsig, SIGCLD);
2000 	}
2001 
2002 	mutex_exit(&p->p_lock);
2003 }
2004 
2005 /*
2006  * Delete queued siginfo structures.
2007  * If a non-null thread pointer is passed then delete from
2008  * the thread queue, otherwise delete from the process queue.
2009  */
2010 void
2011 sigdelq(proc_t *p, kthread_t *t, int sig)
2012 {
2013 	sigqueue_t **psqp, *sqp;
2014 
2015 	/*
2016 	 * We must be holding p->p_lock unless the process is
2017 	 * being reaped or has failed to get started on fork.
2018 	 */
2019 	ASSERT(MUTEX_HELD(&p->p_lock) ||
2020 	    p->p_stat == SIDL || p->p_stat == SZOMB);
2021 
2022 	if (t != (kthread_t *)NULL)
2023 		psqp = &t->t_sigqueue;
2024 	else
2025 		psqp = &p->p_sigqueue;
2026 
2027 	while (*psqp) {
2028 		sqp = *psqp;
2029 		if (sig == 0 || sqp->sq_info.si_signo == sig) {
2030 			*psqp = sqp->sq_next;
2031 			siginfofree(sqp);
2032 		} else
2033 			psqp = &sqp->sq_next;
2034 	}
2035 }
2036 
2037 /*
2038  * Insert a siginfo structure into a queue.
2039  * If a non-null thread pointer is passed then add to the thread queue,
2040  * otherwise add to the process queue.
2041  *
2042  * The function sigaddqins() is called with sigqueue already allocated.
2043  * It is called from sigaddqa() and sigaddq() below.
2044  *
2045  * The value of si_code implicitly indicates whether sigp is to be
2046  * explicitly queued, or to be queued to depth one.
2047  */
2048 static void
2049 sigaddqins(proc_t *p, kthread_t *t, sigqueue_t *sigqp)
2050 {
2051 	sigqueue_t **psqp;
2052 	int sig = sigqp->sq_info.si_signo;
2053 
2054 	sigqp->sq_external = (curproc != &p0) &&
2055 	    (curproc->p_ct_process != p->p_ct_process);
2056 
2057 	/*
2058 	 * issig_forreal() doesn't bother dequeueing signals if SKILLED
2059 	 * is set, and even if it did, we would want to avoid situation
2060 	 * (which would be unique to SIGKILL) where one thread dequeued
2061 	 * the sigqueue_t and another executed psig().  So we create a
2062 	 * separate stash for SIGKILL's sigqueue_t.  Because a second
2063 	 * SIGKILL can set SEXTKILLED, we overwrite the existing entry
2064 	 * if (and only if) it was non-extracontractual.
2065 	 */
2066 	if (sig == SIGKILL) {
2067 		if (p->p_killsqp == NULL || !p->p_killsqp->sq_external) {
2068 			if (p->p_killsqp != NULL)
2069 				siginfofree(p->p_killsqp);
2070 			p->p_killsqp = sigqp;
2071 			sigqp->sq_next = NULL;
2072 		} else {
2073 			siginfofree(sigqp);
2074 		}
2075 		return;
2076 	}
2077 
2078 	ASSERT(sig >= 1 && sig < NSIG);
2079 	if (t != NULL)	/* directed to a thread */
2080 		psqp = &t->t_sigqueue;
2081 	else 		/* directed to a process */
2082 		psqp = &p->p_sigqueue;
2083 	if (SI_CANQUEUE(sigqp->sq_info.si_code) &&
2084 	    sigismember(&p->p_siginfo, sig)) {
2085 		for (; *psqp != NULL; psqp = &(*psqp)->sq_next)
2086 				;
2087 	} else {
2088 		for (; *psqp != NULL; psqp = &(*psqp)->sq_next) {
2089 			if ((*psqp)->sq_info.si_signo == sig) {
2090 				siginfofree(sigqp);
2091 				return;
2092 			}
2093 		}
2094 	}
2095 	*psqp = sigqp;
2096 	sigqp->sq_next = NULL;
2097 }
2098 
2099 /*
2100  * The function sigaddqa() is called with sigqueue already allocated.
2101  * If signal is ignored, discard but guarantee KILL and generation semantics.
2102  * It is called from sigqueue() and other places.
2103  */
2104 void
2105 sigaddqa(proc_t *p, kthread_t *t, sigqueue_t *sigqp)
2106 {
2107 	int sig = sigqp->sq_info.si_signo;
2108 
2109 	ASSERT(MUTEX_HELD(&p->p_lock));
2110 	ASSERT(sig >= 1 && sig < NSIG);
2111 
2112 	if (sig_discardable(p, sig))
2113 		siginfofree(sigqp);
2114 	else
2115 		sigaddqins(p, t, sigqp);
2116 
2117 	sigtoproc(p, t, sig);
2118 }
2119 
2120 /*
2121  * Allocate the sigqueue_t structure and call sigaddqins().
2122  */
2123 void
2124 sigaddq(proc_t *p, kthread_t *t, k_siginfo_t *infop, int km_flags)
2125 {
2126 	sigqueue_t *sqp;
2127 	int sig = infop->si_signo;
2128 
2129 	ASSERT(MUTEX_HELD(&p->p_lock));
2130 	ASSERT(sig >= 1 && sig < NSIG);
2131 
2132 	/*
2133 	 * If the signal will be discarded by sigtoproc() or
2134 	 * if the process isn't requesting siginfo and it isn't
2135 	 * blocking the signal (it *could* change it's mind while
2136 	 * the signal is pending) then don't bother creating one.
2137 	 */
2138 	if (!sig_discardable(p, sig) &&
2139 	    (sigismember(&p->p_siginfo, sig) ||
2140 	    (curproc->p_ct_process != p->p_ct_process) ||
2141 	    (sig == SIGCLD && SI_FROMKERNEL(infop))) &&
2142 	    ((sqp = kmem_alloc(sizeof (sigqueue_t), km_flags)) != NULL)) {
2143 		bcopy(infop, &sqp->sq_info, sizeof (k_siginfo_t));
2144 		sqp->sq_func = NULL;
2145 		sqp->sq_next = NULL;
2146 		sigaddqins(p, t, sqp);
2147 	}
2148 	sigtoproc(p, t, sig);
2149 }
2150 
2151 /*
2152  * Handle stop-on-fault processing for the debugger.  Returns 0
2153  * if the fault is cleared during the stop, nonzero if it isn't.
2154  */
2155 int
2156 stop_on_fault(uint_t fault, k_siginfo_t *sip)
2157 {
2158 	proc_t *p = ttoproc(curthread);
2159 	klwp_t *lwp = ttolwp(curthread);
2160 
2161 	ASSERT(prismember(&p->p_fltmask, fault));
2162 
2163 	/*
2164 	 * Record current fault and siginfo structure so debugger can
2165 	 * find it.
2166 	 */
2167 	mutex_enter(&p->p_lock);
2168 	lwp->lwp_curflt = (uchar_t)fault;
2169 	lwp->lwp_siginfo = *sip;
2170 
2171 	stop(PR_FAULTED, fault);
2172 
2173 	fault = lwp->lwp_curflt;
2174 	lwp->lwp_curflt = 0;
2175 	mutex_exit(&p->p_lock);
2176 	return (fault);
2177 }
2178 
2179 void
2180 sigorset(k_sigset_t *s1, k_sigset_t *s2)
2181 {
2182 	s1->__sigbits[0] |= s2->__sigbits[0];
2183 	s1->__sigbits[1] |= s2->__sigbits[1];
2184 }
2185 
2186 void
2187 sigandset(k_sigset_t *s1, k_sigset_t *s2)
2188 {
2189 	s1->__sigbits[0] &= s2->__sigbits[0];
2190 	s1->__sigbits[1] &= s2->__sigbits[1];
2191 }
2192 
2193 void
2194 sigdiffset(k_sigset_t *s1, k_sigset_t *s2)
2195 {
2196 	s1->__sigbits[0] &= ~(s2->__sigbits[0]);
2197 	s1->__sigbits[1] &= ~(s2->__sigbits[1]);
2198 }
2199 
2200 /*
2201  * Return non-zero if curthread->t_sig_check should be set to 1, that is,
2202  * if there are any signals the thread might take on return from the kernel.
2203  * If ksigset_t's were a single word, we would do:
2204  *	return (((p->p_sig | t->t_sig) & ~t->t_hold) & fillset);
2205  */
2206 int
2207 sigcheck(proc_t *p, kthread_t *t)
2208 {
2209 	sc_shared_t *tdp = t->t_schedctl;
2210 
2211 	/*
2212 	 * If signals are blocked via the schedctl interface
2213 	 * then we only check for the unmaskable signals.
2214 	 */
2215 	if (tdp != NULL && tdp->sc_sigblock)
2216 		return ((p->p_sig.__sigbits[0] | t->t_sig.__sigbits[0]) &
2217 		    CANTMASK0);
2218 
2219 	return (((p->p_sig.__sigbits[0] | t->t_sig.__sigbits[0]) &
2220 	    ~t->t_hold.__sigbits[0]) |
2221 	    (((p->p_sig.__sigbits[1] | t->t_sig.__sigbits[1]) &
2222 	    ~t->t_hold.__sigbits[1]) & FILLSET1));
2223 }
2224 
2225 /* ONC_PLUS EXTRACT START */
2226 void
2227 sigintr(k_sigset_t *smask, int intable)
2228 {
2229 	proc_t *p;
2230 	int owned;
2231 	k_sigset_t lmask;		/* local copy of cantmask */
2232 	klwp_t *lwp = ttolwp(curthread);
2233 
2234 	/*
2235 	 * Mask out all signals except SIGHUP, SIGINT, SIGQUIT
2236 	 *    and SIGTERM. (Preserving the existing masks).
2237 	 *    This function supports the -intr nfs and ufs mount option.
2238 	 */
2239 
2240 	/*
2241 	 * don't do kernel threads
2242 	 */
2243 	if (lwp == NULL)
2244 		return;
2245 
2246 	/*
2247 	 * get access to signal mask
2248 	 */
2249 	p = ttoproc(curthread);
2250 	owned = mutex_owned(&p->p_lock);	/* this is filthy */
2251 	if (!owned)
2252 		mutex_enter(&p->p_lock);
2253 
2254 	/*
2255 	 * remember the current mask
2256 	 */
2257 	schedctl_finish_sigblock(curthread);
2258 	*smask = curthread->t_hold;
2259 
2260 	/*
2261 	 * mask out all signals
2262 	 */
2263 	sigfillset(&curthread->t_hold);
2264 
2265 	/*
2266 	 * Unmask the non-maskable signals (e.g., KILL), as long as
2267 	 * they aren't already masked (which could happen at exit).
2268 	 * The first sigdiffset sets lmask to (cantmask & ~curhold).  The
2269 	 * second sets the current hold mask to (~0 & ~lmask), which reduces
2270 	 * to (~cantmask | curhold).
2271 	 */
2272 	lmask = cantmask;
2273 	sigdiffset(&lmask, smask);
2274 	sigdiffset(&curthread->t_hold, &lmask);
2275 
2276 	/*
2277 	 * Re-enable HUP, QUIT, and TERM iff they were originally enabled
2278 	 * Re-enable INT if it's originally enabled and the NFS mount option
2279 	 * nointr is not set.
2280 	 */
2281 	if (!sigismember(smask, SIGHUP))
2282 		sigdelset(&curthread->t_hold, SIGHUP);
2283 	if (!sigismember(smask, SIGINT) && intable)
2284 		sigdelset(&curthread->t_hold, SIGINT);
2285 	if (!sigismember(smask, SIGQUIT))
2286 		sigdelset(&curthread->t_hold, SIGQUIT);
2287 	if (!sigismember(smask, SIGTERM))
2288 		sigdelset(&curthread->t_hold, SIGTERM);
2289 
2290 	/*
2291 	 * release access to signal mask
2292 	 */
2293 	if (!owned)
2294 		mutex_exit(&p->p_lock);
2295 
2296 	/*
2297 	 * Indicate that this lwp is not to be stopped.
2298 	 */
2299 	lwp->lwp_nostop++;
2300 
2301 }
2302 /* ONC_PLUS EXTRACT END */
2303 
2304 void
2305 sigunintr(k_sigset_t *smask)
2306 {
2307 	proc_t *p;
2308 	int owned;
2309 	klwp_t *lwp = ttolwp(curthread);
2310 
2311 	/*
2312 	 * Reset previous mask (See sigintr() above)
2313 	 */
2314 	if (lwp != NULL) {
2315 		lwp->lwp_nostop--;	/* restore lwp stoppability */
2316 		p = ttoproc(curthread);
2317 		owned = mutex_owned(&p->p_lock);	/* this is filthy */
2318 		if (!owned)
2319 			mutex_enter(&p->p_lock);
2320 		curthread->t_hold = *smask;
2321 		/* so unmasked signals will be seen */
2322 		curthread->t_sig_check = 1;
2323 		if (!owned)
2324 			mutex_exit(&p->p_lock);
2325 	}
2326 }
2327 
2328 void
2329 sigreplace(k_sigset_t *newmask, k_sigset_t *oldmask)
2330 {
2331 	proc_t	*p;
2332 	int owned;
2333 	/*
2334 	 * Save current signal mask in oldmask, then
2335 	 * set it to newmask.
2336 	 */
2337 	if (ttolwp(curthread) != NULL) {
2338 		p = ttoproc(curthread);
2339 		owned = mutex_owned(&p->p_lock);	/* this is filthy */
2340 		if (!owned)
2341 			mutex_enter(&p->p_lock);
2342 		schedctl_finish_sigblock(curthread);
2343 		if (oldmask != NULL)
2344 			*oldmask = curthread->t_hold;
2345 		curthread->t_hold = *newmask;
2346 		curthread->t_sig_check = 1;
2347 		if (!owned)
2348 			mutex_exit(&p->p_lock);
2349 	}
2350 }
2351 
2352 /*
2353  * Return true if the signal number is in range
2354  * and the signal code specifies signal queueing.
2355  */
2356 int
2357 sigwillqueue(int sig, int code)
2358 {
2359 	if (sig >= 0 && sig < NSIG) {
2360 		switch (code) {
2361 		case SI_QUEUE:
2362 		case SI_TIMER:
2363 		case SI_ASYNCIO:
2364 		case SI_MESGQ:
2365 			return (1);
2366 		}
2367 	}
2368 	return (0);
2369 }
2370 
2371 #ifndef	UCHAR_MAX
2372 #define	UCHAR_MAX	255
2373 #endif
2374 
2375 /*
2376  * The entire pool (with maxcount entries) is pre-allocated at
2377  * the first sigqueue/signotify call.
2378  */
2379 sigqhdr_t *
2380 sigqhdralloc(size_t size, uint_t maxcount)
2381 {
2382 	size_t i;
2383 	sigqueue_t *sq, *next;
2384 	sigqhdr_t *sqh;
2385 
2386 	i = (maxcount * size) + sizeof (sigqhdr_t);
2387 	ASSERT(maxcount <= UCHAR_MAX && i <= USHRT_MAX);
2388 	sqh = kmem_alloc(i, KM_SLEEP);
2389 	sqh->sqb_count = (uchar_t)maxcount;
2390 	sqh->sqb_maxcount = (uchar_t)maxcount;
2391 	sqh->sqb_size = (ushort_t)i;
2392 	sqh->sqb_pexited = 0;
2393 	sqh->sqb_sent = 0;
2394 	sqh->sqb_free = sq = (sigqueue_t *)(sqh + 1);
2395 	for (i = maxcount - 1; i != 0; i--) {
2396 		next = (sigqueue_t *)((uintptr_t)sq + size);
2397 		sq->sq_next = next;
2398 		sq = next;
2399 	}
2400 	sq->sq_next = NULL;
2401 	cv_init(&sqh->sqb_cv, NULL, CV_DEFAULT, NULL);
2402 	mutex_init(&sqh->sqb_lock, NULL, MUTEX_DEFAULT, NULL);
2403 	return (sqh);
2404 }
2405 
2406 static void sigqrel(sigqueue_t *);
2407 
2408 /*
2409  * allocate a sigqueue/signotify structure from the per process
2410  * pre-allocated pool.
2411  */
2412 sigqueue_t *
2413 sigqalloc(sigqhdr_t *sqh)
2414 {
2415 	sigqueue_t *sq = NULL;
2416 
2417 	ASSERT(MUTEX_HELD(&curproc->p_lock));
2418 
2419 	if (sqh != NULL) {
2420 		mutex_enter(&sqh->sqb_lock);
2421 		if (sqh->sqb_count > 0) {
2422 			sqh->sqb_count--;
2423 			sq = sqh->sqb_free;
2424 			sqh->sqb_free = sq->sq_next;
2425 			mutex_exit(&sqh->sqb_lock);
2426 			bzero(&sq->sq_info, sizeof (k_siginfo_t));
2427 			sq->sq_backptr = sqh;
2428 			sq->sq_func = sigqrel;
2429 			sq->sq_next = NULL;
2430 			sq->sq_external = 0;
2431 		} else {
2432 			mutex_exit(&sqh->sqb_lock);
2433 		}
2434 	}
2435 	return (sq);
2436 }
2437 
2438 /*
2439  * Return a sigqueue structure back to the pre-allocated pool.
2440  */
2441 static void
2442 sigqrel(sigqueue_t *sq)
2443 {
2444 	sigqhdr_t *sqh;
2445 
2446 	/* make sure that p_lock of the affected process is held */
2447 
2448 	sqh = (sigqhdr_t *)sq->sq_backptr;
2449 	mutex_enter(&sqh->sqb_lock);
2450 	if (sqh->sqb_pexited && sqh->sqb_sent == 1) {
2451 		mutex_exit(&sqh->sqb_lock);
2452 		cv_destroy(&sqh->sqb_cv);
2453 		mutex_destroy(&sqh->sqb_lock);
2454 		kmem_free(sqh, sqh->sqb_size);
2455 	} else {
2456 		sqh->sqb_count++;
2457 		sqh->sqb_sent--;
2458 		sq->sq_next = sqh->sqb_free;
2459 		sq->sq_backptr = NULL;
2460 		sqh->sqb_free = sq;
2461 		cv_signal(&sqh->sqb_cv);
2462 		mutex_exit(&sqh->sqb_lock);
2463 	}
2464 }
2465 
2466 /*
2467  * Free up the pre-allocated sigqueue headers of sigqueue pool
2468  * and signotify pool, if possible.
2469  * Called only by the owning process during exec() and exit().
2470  */
2471 void
2472 sigqfree(proc_t *p)
2473 {
2474 	ASSERT(MUTEX_HELD(&p->p_lock));
2475 
2476 	if (p->p_sigqhdr != NULL) {	/* sigqueue pool */
2477 		sigqhdrfree(p->p_sigqhdr);
2478 		p->p_sigqhdr = NULL;
2479 	}
2480 	if (p->p_signhdr != NULL) {	/* signotify pool */
2481 		sigqhdrfree(p->p_signhdr);
2482 		p->p_signhdr = NULL;
2483 	}
2484 }
2485 
2486 /*
2487  * Free up the pre-allocated header and sigq pool if possible.
2488  */
2489 void
2490 sigqhdrfree(sigqhdr_t *sqh)
2491 {
2492 	mutex_enter(&sqh->sqb_lock);
2493 	if (sqh->sqb_sent == 0) {
2494 		mutex_exit(&sqh->sqb_lock);
2495 		cv_destroy(&sqh->sqb_cv);
2496 		mutex_destroy(&sqh->sqb_lock);
2497 		kmem_free(sqh, sqh->sqb_size);
2498 	} else {
2499 		sqh->sqb_pexited = 1;
2500 		mutex_exit(&sqh->sqb_lock);
2501 	}
2502 }
2503 
2504 /*
2505  * Free up a single sigqueue structure.
2506  * No other code should free a sigqueue directly.
2507  */
2508 void
2509 siginfofree(sigqueue_t *sqp)
2510 {
2511 	if (sqp != NULL) {
2512 		if (sqp->sq_func != NULL)
2513 			(sqp->sq_func)(sqp);
2514 		else
2515 			kmem_free(sqp, sizeof (sigqueue_t));
2516 	}
2517 }
2518 
2519 /*
2520  * Generate a synchronous signal caused by a hardware
2521  * condition encountered by an lwp.  Called from trap().
2522  */
2523 void
2524 trapsig(k_siginfo_t *ip, int restartable)
2525 {
2526 	proc_t *p = ttoproc(curthread);
2527 	int sig = ip->si_signo;
2528 	sigqueue_t *sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
2529 
2530 	ASSERT(sig > 0 && sig < NSIG);
2531 
2532 	if (curthread->t_dtrace_on)
2533 		dtrace_safe_synchronous_signal();
2534 
2535 	mutex_enter(&p->p_lock);
2536 	schedctl_finish_sigblock(curthread);
2537 	/*
2538 	 * Avoid a possible infinite loop if the lwp is holding the
2539 	 * signal generated by a trap of a restartable instruction or
2540 	 * if the signal so generated is being ignored by the process.
2541 	 */
2542 	if (restartable &&
2543 	    (sigismember(&curthread->t_hold, sig) ||
2544 	    p->p_user.u_signal[sig-1] == SIG_IGN)) {
2545 		sigdelset(&curthread->t_hold, sig);
2546 		p->p_user.u_signal[sig-1] = SIG_DFL;
2547 		sigdelset(&p->p_ignore, sig);
2548 	}
2549 	bcopy(ip, &sqp->sq_info, sizeof (k_siginfo_t));
2550 	sigaddqa(p, curthread, sqp);
2551 	mutex_exit(&p->p_lock);
2552 }
2553 
2554 /*
2555  * Arrange for the real time profiling signal to be dispatched.
2556  */
2557 void
2558 realsigprof(int sysnum, int nsysarg, int error)
2559 {
2560 	proc_t *p;
2561 	klwp_t *lwp;
2562 
2563 	if (curthread->t_rprof->rp_anystate == 0)
2564 		return;
2565 	p = ttoproc(curthread);
2566 	lwp = ttolwp(curthread);
2567 	mutex_enter(&p->p_lock);
2568 	if (p->p_rprof_cyclic == CYCLIC_NONE) {
2569 		bzero(curthread->t_rprof, sizeof (*curthread->t_rprof));
2570 		mutex_exit(&p->p_lock);
2571 		return;
2572 	}
2573 	if (sigismember(&p->p_ignore, SIGPROF) ||
2574 	    signal_is_blocked(curthread, SIGPROF)) {
2575 		mutex_exit(&p->p_lock);
2576 		return;
2577 	}
2578 	lwp->lwp_siginfo.si_signo = SIGPROF;
2579 	lwp->lwp_siginfo.si_code = PROF_SIG;
2580 	lwp->lwp_siginfo.si_errno = error;
2581 	hrt2ts(gethrtime(), &lwp->lwp_siginfo.si_tstamp);
2582 	lwp->lwp_siginfo.si_syscall = sysnum;
2583 	lwp->lwp_siginfo.si_nsysarg = nsysarg;
2584 	lwp->lwp_siginfo.si_fault = lwp->lwp_lastfault;
2585 	lwp->lwp_siginfo.si_faddr = lwp->lwp_lastfaddr;
2586 	lwp->lwp_lastfault = 0;
2587 	lwp->lwp_lastfaddr = NULL;
2588 	sigtoproc(p, curthread, SIGPROF);
2589 	mutex_exit(&p->p_lock);
2590 	ASSERT(lwp->lwp_cursig == 0);
2591 	if (issig(FORREAL))
2592 		psig();
2593 	mutex_enter(&p->p_lock);
2594 	lwp->lwp_siginfo.si_signo = 0;
2595 	bzero(curthread->t_rprof, sizeof (*curthread->t_rprof));
2596 	mutex_exit(&p->p_lock);
2597 }
2598 
2599 #ifdef _SYSCALL32_IMPL
2600 
2601 /*
2602  * It's tricky to transmit a sigval between 32-bit and 64-bit
2603  * process, since in the 64-bit world, a pointer and an integer
2604  * are different sizes.  Since we're constrained by the standards
2605  * world not to change the types, and it's unclear how useful it is
2606  * to send pointers between address spaces this way, we preserve
2607  * the 'int' interpretation for 32-bit processes interoperating
2608  * with 64-bit processes.  The full semantics (pointers or integers)
2609  * are available for N-bit processes interoperating with N-bit
2610  * processes.
2611  */
2612 void
2613 siginfo_kto32(const k_siginfo_t *src, siginfo32_t *dest)
2614 {
2615 	bzero(dest, sizeof (*dest));
2616 
2617 	/*
2618 	 * The absolute minimum content is si_signo and si_code.
2619 	 */
2620 	dest->si_signo = src->si_signo;
2621 	if ((dest->si_code = src->si_code) == SI_NOINFO)
2622 		return;
2623 
2624 	/*
2625 	 * A siginfo generated by user level is structured
2626 	 * differently from one generated by the kernel.
2627 	 */
2628 	if (SI_FROMUSER(src)) {
2629 		dest->si_pid = src->si_pid;
2630 		dest->si_ctid = src->si_ctid;
2631 		dest->si_zoneid = src->si_zoneid;
2632 		dest->si_uid = src->si_uid;
2633 		if (SI_CANQUEUE(src->si_code))
2634 			dest->si_value.sival_int =
2635 			    (int32_t)src->si_value.sival_int;
2636 		return;
2637 	}
2638 
2639 	dest->si_errno = src->si_errno;
2640 
2641 	switch (src->si_signo) {
2642 	default:
2643 		dest->si_pid = src->si_pid;
2644 		dest->si_ctid = src->si_ctid;
2645 		dest->si_zoneid = src->si_zoneid;
2646 		dest->si_uid = src->si_uid;
2647 		dest->si_value.sival_int = (int32_t)src->si_value.sival_int;
2648 		break;
2649 	case SIGCLD:
2650 		dest->si_pid = src->si_pid;
2651 		dest->si_ctid = src->si_ctid;
2652 		dest->si_zoneid = src->si_zoneid;
2653 		dest->si_status = src->si_status;
2654 		dest->si_stime = src->si_stime;
2655 		dest->si_utime = src->si_utime;
2656 		break;
2657 	case SIGSEGV:
2658 	case SIGBUS:
2659 	case SIGILL:
2660 	case SIGTRAP:
2661 	case SIGFPE:
2662 	case SIGEMT:
2663 		dest->si_addr = (caddr32_t)(uintptr_t)src->si_addr;
2664 		dest->si_trapno = src->si_trapno;
2665 		dest->si_pc = (caddr32_t)(uintptr_t)src->si_pc;
2666 		break;
2667 	case SIGPOLL:
2668 	case SIGXFSZ:
2669 		dest->si_fd = src->si_fd;
2670 		dest->si_band = src->si_band;
2671 		break;
2672 	case SIGPROF:
2673 		dest->si_faddr = (caddr32_t)(uintptr_t)src->si_faddr;
2674 		dest->si_tstamp.tv_sec = src->si_tstamp.tv_sec;
2675 		dest->si_tstamp.tv_nsec = src->si_tstamp.tv_nsec;
2676 		dest->si_syscall = src->si_syscall;
2677 		dest->si_nsysarg = src->si_nsysarg;
2678 		dest->si_fault = src->si_fault;
2679 		break;
2680 	}
2681 }
2682 
2683 void
2684 siginfo_32tok(const siginfo32_t *src, k_siginfo_t *dest)
2685 {
2686 	bzero(dest, sizeof (*dest));
2687 
2688 	/*
2689 	 * The absolute minimum content is si_signo and si_code.
2690 	 */
2691 	dest->si_signo = src->si_signo;
2692 	if ((dest->si_code = src->si_code) == SI_NOINFO)
2693 		return;
2694 
2695 	/*
2696 	 * A siginfo generated by user level is structured
2697 	 * differently from one generated by the kernel.
2698 	 */
2699 	if (SI_FROMUSER(src)) {
2700 		dest->si_pid = src->si_pid;
2701 		dest->si_ctid = src->si_ctid;
2702 		dest->si_zoneid = src->si_zoneid;
2703 		dest->si_uid = src->si_uid;
2704 		if (SI_CANQUEUE(src->si_code))
2705 			dest->si_value.sival_int =
2706 			    (int)src->si_value.sival_int;
2707 		return;
2708 	}
2709 
2710 	dest->si_errno = src->si_errno;
2711 
2712 	switch (src->si_signo) {
2713 	default:
2714 		dest->si_pid = src->si_pid;
2715 		dest->si_ctid = src->si_ctid;
2716 		dest->si_zoneid = src->si_zoneid;
2717 		dest->si_uid = src->si_uid;
2718 		dest->si_value.sival_int = (int)src->si_value.sival_int;
2719 		break;
2720 	case SIGCLD:
2721 		dest->si_pid = src->si_pid;
2722 		dest->si_ctid = src->si_ctid;
2723 		dest->si_zoneid = src->si_zoneid;
2724 		dest->si_status = src->si_status;
2725 		dest->si_stime = src->si_stime;
2726 		dest->si_utime = src->si_utime;
2727 		break;
2728 	case SIGSEGV:
2729 	case SIGBUS:
2730 	case SIGILL:
2731 	case SIGTRAP:
2732 	case SIGFPE:
2733 	case SIGEMT:
2734 		dest->si_addr = (void *)(uintptr_t)src->si_addr;
2735 		dest->si_trapno = src->si_trapno;
2736 		dest->si_pc = (void *)(uintptr_t)src->si_pc;
2737 		break;
2738 	case SIGPOLL:
2739 	case SIGXFSZ:
2740 		dest->si_fd = src->si_fd;
2741 		dest->si_band = src->si_band;
2742 		break;
2743 	case SIGPROF:
2744 		dest->si_faddr = (void *)(uintptr_t)src->si_faddr;
2745 		dest->si_tstamp.tv_sec = src->si_tstamp.tv_sec;
2746 		dest->si_tstamp.tv_nsec = src->si_tstamp.tv_nsec;
2747 		dest->si_syscall = src->si_syscall;
2748 		dest->si_nsysarg = src->si_nsysarg;
2749 		dest->si_fault = src->si_fault;
2750 		break;
2751 	}
2752 }
2753 
2754 #endif /* _SYSCALL32_IMPL */
2755