xref: /onnv-gate/usr/src/uts/intel/ia32/os/sendsig.c (revision 1629)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1629Sraf  * Common Development and Distribution License (the "License").
6*1629Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*1629Sraf 
220Sstevel@tonic-gate /*
23*1629Sraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */
280Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T   */
290Sstevel@tonic-gate /*	All Rights Reserved   */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <sys/param.h>
350Sstevel@tonic-gate #include <sys/sysmacros.h>
360Sstevel@tonic-gate #include <sys/signal.h>
370Sstevel@tonic-gate #include <sys/systm.h>
380Sstevel@tonic-gate #include <sys/user.h>
390Sstevel@tonic-gate #include <sys/mman.h>
400Sstevel@tonic-gate #include <sys/class.h>
410Sstevel@tonic-gate #include <sys/proc.h>
420Sstevel@tonic-gate #include <sys/procfs.h>
430Sstevel@tonic-gate #include <sys/buf.h>
440Sstevel@tonic-gate #include <sys/kmem.h>
450Sstevel@tonic-gate #include <sys/cred.h>
460Sstevel@tonic-gate #include <sys/archsystm.h>
470Sstevel@tonic-gate #include <sys/vmparam.h>
480Sstevel@tonic-gate #include <sys/prsystm.h>
490Sstevel@tonic-gate #include <sys/reboot.h>
500Sstevel@tonic-gate #include <sys/uadmin.h>
510Sstevel@tonic-gate #include <sys/vfs.h>
520Sstevel@tonic-gate #include <sys/vnode.h>
530Sstevel@tonic-gate #include <sys/file.h>
540Sstevel@tonic-gate #include <sys/session.h>
550Sstevel@tonic-gate #include <sys/ucontext.h>
560Sstevel@tonic-gate #include <sys/dnlc.h>
570Sstevel@tonic-gate #include <sys/var.h>
580Sstevel@tonic-gate #include <sys/cmn_err.h>
590Sstevel@tonic-gate #include <sys/debugreg.h>
600Sstevel@tonic-gate #include <sys/thread.h>
610Sstevel@tonic-gate #include <sys/vtrace.h>
620Sstevel@tonic-gate #include <sys/consdev.h>
630Sstevel@tonic-gate #include <sys/psw.h>
640Sstevel@tonic-gate #include <sys/regset.h>
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #include <sys/privregs.h>
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #include <sys/stack.h>
690Sstevel@tonic-gate #include <sys/swap.h>
700Sstevel@tonic-gate #include <vm/hat.h>
710Sstevel@tonic-gate #include <vm/anon.h>
720Sstevel@tonic-gate #include <vm/as.h>
730Sstevel@tonic-gate #include <vm/page.h>
740Sstevel@tonic-gate #include <vm/seg.h>
750Sstevel@tonic-gate #include <vm/seg_kmem.h>
760Sstevel@tonic-gate #include <vm/seg_map.h>
770Sstevel@tonic-gate #include <vm/seg_vn.h>
780Sstevel@tonic-gate #include <sys/exec.h>
790Sstevel@tonic-gate #include <sys/acct.h>
800Sstevel@tonic-gate #include <sys/core.h>
810Sstevel@tonic-gate #include <sys/corectl.h>
820Sstevel@tonic-gate #include <sys/modctl.h>
830Sstevel@tonic-gate #include <sys/tuneable.h>
840Sstevel@tonic-gate #include <c2/audit.h>
850Sstevel@tonic-gate #include <sys/bootconf.h>
860Sstevel@tonic-gate #include <sys/dumphdr.h>
870Sstevel@tonic-gate #include <sys/promif.h>
880Sstevel@tonic-gate #include <sys/systeminfo.h>
890Sstevel@tonic-gate #include <sys/kdi.h>
900Sstevel@tonic-gate #include <sys/contract_impl.h>
910Sstevel@tonic-gate #include <sys/x86_archext.h>
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate  * Construct the execution environment for the user's signal
950Sstevel@tonic-gate  * handler and arrange for control to be given to it on return
960Sstevel@tonic-gate  * to userland.  The library code now calls setcontext() to
970Sstevel@tonic-gate  * clean up after the signal handler, so sigret() is no longer
980Sstevel@tonic-gate  * needed.
990Sstevel@tonic-gate  *
1000Sstevel@tonic-gate  * (The various 'volatile' declarations are need to ensure that values
1010Sstevel@tonic-gate  * are correct on the error return from on_fault().)
1020Sstevel@tonic-gate  */
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate #if defined(__amd64)
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate /*
1070Sstevel@tonic-gate  * An amd64 signal frame looks like this on the stack:
1080Sstevel@tonic-gate  *
1090Sstevel@tonic-gate  * old %rsp:
1100Sstevel@tonic-gate  *		<128 bytes of untouched stack space>
1110Sstevel@tonic-gate  *		<a siginfo_t [optional]>
1120Sstevel@tonic-gate  *		<a ucontext_t>
1130Sstevel@tonic-gate  *		<siginfo_t *>
1140Sstevel@tonic-gate  *		<signal number>
1150Sstevel@tonic-gate  * new %rsp:	<return address (deliberately invalid)>
1160Sstevel@tonic-gate  *
1170Sstevel@tonic-gate  * The signal number and siginfo_t pointer are only pushed onto the stack in
1180Sstevel@tonic-gate  * order to allow stack backtraces.  The actual signal handling code expects the
1190Sstevel@tonic-gate  * arguments in registers.
1200Sstevel@tonic-gate  */
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate struct sigframe {
1230Sstevel@tonic-gate 	caddr_t retaddr;
1240Sstevel@tonic-gate 	long	signo;
1250Sstevel@tonic-gate 	siginfo_t *sip;
1260Sstevel@tonic-gate };
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate int
1290Sstevel@tonic-gate sendsig(int sig, k_siginfo_t *sip, void (*hdlr)())
1300Sstevel@tonic-gate {
1310Sstevel@tonic-gate 	volatile int minstacksz;
1320Sstevel@tonic-gate 	int newstack;
1330Sstevel@tonic-gate 	label_t ljb;
1340Sstevel@tonic-gate 	volatile caddr_t sp;
1350Sstevel@tonic-gate 	caddr_t fp;
1360Sstevel@tonic-gate 	volatile struct regs *rp;
1370Sstevel@tonic-gate 	volatile greg_t upc;
1380Sstevel@tonic-gate 	volatile proc_t *p = ttoproc(curthread);
1390Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
1400Sstevel@tonic-gate 	ucontext_t *volatile tuc = NULL;
1410Sstevel@tonic-gate 	ucontext_t *uc;
1420Sstevel@tonic-gate 	siginfo_t *sip_addr;
1430Sstevel@tonic-gate 	volatile int watched;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	/*
1460Sstevel@tonic-gate 	 * This routine is utterly dependent upon STACK_ALIGN being
1470Sstevel@tonic-gate 	 * 16 and STACK_ENTRY_ALIGN being 8. Let's just acknowledge
1480Sstevel@tonic-gate 	 * that and require it.
1490Sstevel@tonic-gate 	 */
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate #if STACK_ALIGN != 16 || STACK_ENTRY_ALIGN != 8
1520Sstevel@tonic-gate #error "sendsig() amd64 did not find the expected stack alignments"
1530Sstevel@tonic-gate #endif
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	rp = lwptoregs(lwp);
1560Sstevel@tonic-gate 	upc = rp->r_pc;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	/*
1590Sstevel@tonic-gate 	 * Since we're setting up to run the signal handler we have to
1600Sstevel@tonic-gate 	 * arrange that the stack at entry to the handler is (only)
1610Sstevel@tonic-gate 	 * STACK_ENTRY_ALIGN (i.e. 8) byte aligned so that when the handler
1620Sstevel@tonic-gate 	 * executes its push of %rbp, the stack realigns to STACK_ALIGN
1630Sstevel@tonic-gate 	 * (i.e. 16) correctly.
1640Sstevel@tonic-gate 	 *
1650Sstevel@tonic-gate 	 * The new sp will point to the sigframe and the ucontext_t. The
1660Sstevel@tonic-gate 	 * above means that sp (and thus sigframe) will be 8-byte aligned,
1670Sstevel@tonic-gate 	 * but not 16-byte aligned. ucontext_t, however, contains %xmm regs
1680Sstevel@tonic-gate 	 * which must be 16-byte aligned. Because of this, for correct
1690Sstevel@tonic-gate 	 * alignment, sigframe must be a multiple of 8-bytes in length, but
1700Sstevel@tonic-gate 	 * not 16-bytes. This will place ucontext_t at a nice 16-byte boundary.
1710Sstevel@tonic-gate 	 */
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	/* LINTED: logical expression always true: op "||" */
1740Sstevel@tonic-gate 	ASSERT((sizeof (struct sigframe) % 16) == 8);
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	minstacksz = sizeof (struct sigframe) + SA(sizeof (*uc));
1770Sstevel@tonic-gate 	if (sip != NULL)
1780Sstevel@tonic-gate 		minstacksz += SA(sizeof (siginfo_t));
1790Sstevel@tonic-gate 	ASSERT((minstacksz & (STACK_ENTRY_ALIGN - 1ul)) == 0);
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	/*
1820Sstevel@tonic-gate 	 * Figure out whether we will be handling this signal on
1830Sstevel@tonic-gate 	 * an alternate stack specified by the user.  Then allocate
1840Sstevel@tonic-gate 	 * and validate the stack requirements for the signal handler
1850Sstevel@tonic-gate 	 * context.  on_fault will catch any faults.
1860Sstevel@tonic-gate 	 */
1870Sstevel@tonic-gate 	newstack = sigismember(&u.u_sigonstack, sig) &&
1880Sstevel@tonic-gate 	    !(lwp->lwp_sigaltstack.ss_flags & (SS_ONSTACK|SS_DISABLE));
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	if (newstack) {
1910Sstevel@tonic-gate 		fp = (caddr_t)(SA((uintptr_t)lwp->lwp_sigaltstack.ss_sp) +
1920Sstevel@tonic-gate 		    SA(lwp->lwp_sigaltstack.ss_size) - STACK_ALIGN);
1930Sstevel@tonic-gate 	} else {
1940Sstevel@tonic-gate 		/*
1950Sstevel@tonic-gate 		 * Drop below the 128-byte reserved region of the stack frame
1960Sstevel@tonic-gate 		 * we're interrupting.
1970Sstevel@tonic-gate 		 */
1980Sstevel@tonic-gate 		fp = (caddr_t)rp->r_sp - STACK_RESERVE;
1990Sstevel@tonic-gate 	}
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	/*
202*1629Sraf 	 * Force proper stack pointer alignment, even in the face of a
203*1629Sraf 	 * misaligned stack pointer from user-level before the signal.
204*1629Sraf 	 */
205*1629Sraf 	fp = (caddr_t)((uintptr_t)fp & ~(STACK_ENTRY_ALIGN - 1ul));
206*1629Sraf 
207*1629Sraf 	/*
2080Sstevel@tonic-gate 	 * Most of the time during normal execution, the stack pointer
2090Sstevel@tonic-gate 	 * is aligned on a STACK_ALIGN (i.e. 16 byte) boundary.  However,
2100Sstevel@tonic-gate 	 * (for example) just after a call instruction (which pushes
2110Sstevel@tonic-gate 	 * the return address), the callers stack misaligns until the
2120Sstevel@tonic-gate 	 * 'push %rbp' happens in the callee prolog.  So while we should
2130Sstevel@tonic-gate 	 * expect the stack pointer to be always at least STACK_ENTRY_ALIGN
2140Sstevel@tonic-gate 	 * aligned, we should -not- expect it to always be STACK_ALIGN aligned.
2150Sstevel@tonic-gate 	 * We now adjust to ensure that the new sp is aligned to
2160Sstevel@tonic-gate 	 * STACK_ENTRY_ALIGN but not to STACK_ALIGN.
2170Sstevel@tonic-gate 	 */
2180Sstevel@tonic-gate 	sp = fp - minstacksz;
2190Sstevel@tonic-gate 	if (((uintptr_t)sp & (STACK_ALIGN - 1ul)) == 0) {
2200Sstevel@tonic-gate 		sp -= STACK_ENTRY_ALIGN;
2210Sstevel@tonic-gate 		minstacksz = fp - sp;
2220Sstevel@tonic-gate 	}
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	/*
2250Sstevel@tonic-gate 	 * Now, make sure the resulting signal frame address is sane
2260Sstevel@tonic-gate 	 */
227*1629Sraf 	if (sp >= (caddr_t)USERLIMIT || fp >= (caddr_t)USERLIMIT) {
2280Sstevel@tonic-gate #ifdef DEBUG
2290Sstevel@tonic-gate 		printf("sendsig: bad signal stack cmd=%s, pid=%d, sig=%d\n",
2300Sstevel@tonic-gate 		    PTOU(p)->u_comm, p->p_pid, sig);
2310Sstevel@tonic-gate 		printf("sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
2320Sstevel@tonic-gate 		    (void *)sp, (void *)hdlr, (uintptr_t)upc);
233*1629Sraf 		printf("sp above USERLIMIT\n");
2340Sstevel@tonic-gate #endif
2350Sstevel@tonic-gate 		return (0);
2360Sstevel@tonic-gate 	}
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	watched = watch_disable_addr((caddr_t)sp, minstacksz, S_WRITE);
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	if (on_fault(&ljb))
2410Sstevel@tonic-gate 		goto badstack;
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	if (sip != NULL) {
2440Sstevel@tonic-gate 		zoneid_t zoneid;
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 		fp -= SA(sizeof (siginfo_t));
2470Sstevel@tonic-gate 		uzero(fp, sizeof (siginfo_t));
2480Sstevel@tonic-gate 		if (SI_FROMUSER(sip) &&
2490Sstevel@tonic-gate 		    (zoneid = p->p_zone->zone_id) != GLOBAL_ZONEID &&
2500Sstevel@tonic-gate 		    zoneid != sip->si_zoneid) {
2510Sstevel@tonic-gate 			k_siginfo_t sani_sip = *sip;
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 			sani_sip.si_pid = p->p_zone->zone_zsched->p_pid;
2540Sstevel@tonic-gate 			sani_sip.si_uid = 0;
2550Sstevel@tonic-gate 			sani_sip.si_ctid = -1;
2560Sstevel@tonic-gate 			sani_sip.si_zoneid = zoneid;
2570Sstevel@tonic-gate 			copyout_noerr(&sani_sip, fp, sizeof (sani_sip));
2580Sstevel@tonic-gate 		} else
2590Sstevel@tonic-gate 			copyout_noerr(sip, fp, sizeof (*sip));
2600Sstevel@tonic-gate 		sip_addr = (siginfo_t *)fp;
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 		if (sig == SIGPROF &&
2630Sstevel@tonic-gate 		    curthread->t_rprof != NULL &&
2640Sstevel@tonic-gate 		    curthread->t_rprof->rp_anystate) {
2650Sstevel@tonic-gate 			/*
2660Sstevel@tonic-gate 			 * We stand on our head to deal with
2670Sstevel@tonic-gate 			 * the real time profiling signal.
2680Sstevel@tonic-gate 			 * Fill in the stuff that doesn't fit
2690Sstevel@tonic-gate 			 * in a normal k_siginfo structure.
2700Sstevel@tonic-gate 			 */
2710Sstevel@tonic-gate 			int i = sip->si_nsysarg;
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 			while (--i >= 0)
2740Sstevel@tonic-gate 				sulword_noerr(
2750Sstevel@tonic-gate 				    (ulong_t *)&(sip_addr->si_sysarg[i]),
2760Sstevel@tonic-gate 				    (ulong_t)lwp->lwp_arg[i]);
2770Sstevel@tonic-gate 			copyout_noerr(curthread->t_rprof->rp_state,
2780Sstevel@tonic-gate 			    sip_addr->si_mstate,
2790Sstevel@tonic-gate 			    sizeof (curthread->t_rprof->rp_state));
2800Sstevel@tonic-gate 		}
2810Sstevel@tonic-gate 	} else
2820Sstevel@tonic-gate 		sip_addr = NULL;
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	/*
2850Sstevel@tonic-gate 	 * save the current context on the user stack directly after the
2860Sstevel@tonic-gate 	 * sigframe. Since sigframe is 8-byte-but-not-16-byte aligned,
2870Sstevel@tonic-gate 	 * and since sizeof (struct sigframe) is 24, this guarantees
2880Sstevel@tonic-gate 	 * 16-byte alignment for ucontext_t and its %xmm registers.
2890Sstevel@tonic-gate 	 */
2900Sstevel@tonic-gate 	uc = (ucontext_t *)(sp + sizeof (struct sigframe));
2910Sstevel@tonic-gate 	tuc = kmem_alloc(sizeof (*tuc), KM_SLEEP);
2920Sstevel@tonic-gate 	savecontext(tuc, lwp->lwp_sigoldmask);
2930Sstevel@tonic-gate 	copyout_noerr(tuc, uc, sizeof (*tuc));
2940Sstevel@tonic-gate 	kmem_free(tuc, sizeof (*tuc));
2950Sstevel@tonic-gate 	tuc = NULL;
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	lwp->lwp_oldcontext = (uintptr_t)uc;
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	if (newstack) {
3000Sstevel@tonic-gate 		lwp->lwp_sigaltstack.ss_flags |= SS_ONSTACK;
3010Sstevel@tonic-gate 		if (lwp->lwp_ustack)
3020Sstevel@tonic-gate 			copyout_noerr(&lwp->lwp_sigaltstack,
3030Sstevel@tonic-gate 			    (stack_t *)lwp->lwp_ustack, sizeof (stack_t));
3040Sstevel@tonic-gate 	}
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	/*
3070Sstevel@tonic-gate 	 * Set up signal handler return and stack linkage
3080Sstevel@tonic-gate 	 */
3090Sstevel@tonic-gate 	{
3100Sstevel@tonic-gate 		struct sigframe frame;
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 		/*
3130Sstevel@tonic-gate 		 * ensure we never return "normally"
3140Sstevel@tonic-gate 		 */
3150Sstevel@tonic-gate 		frame.retaddr = (caddr_t)(uintptr_t)-1L;
3160Sstevel@tonic-gate 		frame.signo = sig;
3170Sstevel@tonic-gate 		frame.sip = sip_addr;
3180Sstevel@tonic-gate 		copyout_noerr(&frame, sp, sizeof (frame));
3190Sstevel@tonic-gate 	}
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	no_fault();
3220Sstevel@tonic-gate 	if (watched)
3230Sstevel@tonic-gate 		watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE);
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 	/*
3260Sstevel@tonic-gate 	 * Set up user registers for execution of signal handler.
3270Sstevel@tonic-gate 	 */
3280Sstevel@tonic-gate 	rp->r_sp = (greg_t)sp;
3290Sstevel@tonic-gate 	rp->r_pc = (greg_t)hdlr;
3300Sstevel@tonic-gate 	rp->r_ps = PSL_USER | (rp->r_ps & PS_IOPL);
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	rp->r_rdi = sig;
3330Sstevel@tonic-gate 	rp->r_rsi = (uintptr_t)sip_addr;
3340Sstevel@tonic-gate 	rp->r_rdx = (uintptr_t)uc;
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	if ((rp->r_cs & 0xffff) != UCS_SEL ||
3370Sstevel@tonic-gate 	    (rp->r_ss & 0xffff) != UDS_SEL) {
3380Sstevel@tonic-gate 		/*
3390Sstevel@tonic-gate 		 * Try our best to deliver the signal.
3400Sstevel@tonic-gate 		 */
3410Sstevel@tonic-gate 		rp->r_cs = UCS_SEL;
3420Sstevel@tonic-gate 		rp->r_ss = UDS_SEL;
3430Sstevel@tonic-gate 	}
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	/*
3460Sstevel@tonic-gate 	 * Don't set lwp_eosys here.  sendsig() is called via psig() after
3470Sstevel@tonic-gate 	 * lwp_eosys is handled, so setting it here would affect the next
3480Sstevel@tonic-gate 	 * system call.
3490Sstevel@tonic-gate 	 */
3500Sstevel@tonic-gate 	return (1);
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate badstack:
3530Sstevel@tonic-gate 	no_fault();
3540Sstevel@tonic-gate 	if (watched)
3550Sstevel@tonic-gate 		watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE);
3560Sstevel@tonic-gate 	if (tuc)
3570Sstevel@tonic-gate 		kmem_free(tuc, sizeof (*tuc));
3580Sstevel@tonic-gate #ifdef DEBUG
3590Sstevel@tonic-gate 	printf("sendsig: bad signal stack cmd=%s, pid=%d, sig=%d\n",
3600Sstevel@tonic-gate 	    PTOU(p)->u_comm, p->p_pid, sig);
3610Sstevel@tonic-gate 	printf("on fault, sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
3620Sstevel@tonic-gate 	    (void *)sp, (void *)hdlr, (uintptr_t)upc);
3630Sstevel@tonic-gate #endif
3640Sstevel@tonic-gate 	return (0);
3650Sstevel@tonic-gate }
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate /*
3700Sstevel@tonic-gate  * An i386 SVR4/ABI signal frame looks like this on the stack:
3710Sstevel@tonic-gate  *
3720Sstevel@tonic-gate  * old %esp:
3730Sstevel@tonic-gate  *		<a siginfo32_t [optional]>
3740Sstevel@tonic-gate  *		<a ucontext32_t>
3750Sstevel@tonic-gate  *		<pointer to that ucontext32_t>
3760Sstevel@tonic-gate  *		<pointer to that siginfo32_t>
3770Sstevel@tonic-gate  *		<signo>
3780Sstevel@tonic-gate  * new %esp:	<return address (deliberately invalid)>
3790Sstevel@tonic-gate  */
3800Sstevel@tonic-gate struct sigframe32 {
3810Sstevel@tonic-gate 	caddr32_t	retaddr;
3820Sstevel@tonic-gate 	uint32_t	signo;
3830Sstevel@tonic-gate 	caddr32_t	sip;
3840Sstevel@tonic-gate 	caddr32_t	ucp;
3850Sstevel@tonic-gate };
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate int
3880Sstevel@tonic-gate sendsig32(int sig, k_siginfo_t *sip, void (*hdlr)())
3890Sstevel@tonic-gate {
3900Sstevel@tonic-gate 	volatile int minstacksz;
3910Sstevel@tonic-gate 	int newstack;
3920Sstevel@tonic-gate 	label_t ljb;
3930Sstevel@tonic-gate 	volatile caddr_t sp;
3940Sstevel@tonic-gate 	caddr_t fp;
3950Sstevel@tonic-gate 	volatile struct regs *rp;
3960Sstevel@tonic-gate 	volatile greg_t upc;
3970Sstevel@tonic-gate 	volatile proc_t *p = ttoproc(curthread);
3980Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
3990Sstevel@tonic-gate 	ucontext32_t *volatile tuc = NULL;
4000Sstevel@tonic-gate 	ucontext32_t *uc;
4010Sstevel@tonic-gate 	siginfo32_t *sip_addr;
4020Sstevel@tonic-gate 	volatile int watched;
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 	rp = lwptoregs(lwp);
4050Sstevel@tonic-gate 	upc = rp->r_pc;
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	minstacksz = SA32(sizeof (struct sigframe32)) + SA32(sizeof (*uc));
4080Sstevel@tonic-gate 	if (sip != NULL)
4090Sstevel@tonic-gate 		minstacksz += SA32(sizeof (siginfo32_t));
4100Sstevel@tonic-gate 	ASSERT((minstacksz & (STACK_ALIGN32 - 1)) == 0);
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	/*
4130Sstevel@tonic-gate 	 * Figure out whether we will be handling this signal on
4140Sstevel@tonic-gate 	 * an alternate stack specified by the user.  Then allocate
4150Sstevel@tonic-gate 	 * and validate the stack requirements for the signal handler
4160Sstevel@tonic-gate 	 * context.  on_fault will catch any faults.
4170Sstevel@tonic-gate 	 */
4180Sstevel@tonic-gate 	newstack = sigismember(&u.u_sigonstack, sig) &&
4190Sstevel@tonic-gate 	    !(lwp->lwp_sigaltstack.ss_flags & (SS_ONSTACK|SS_DISABLE));
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	if (newstack) {
4220Sstevel@tonic-gate 		fp = (caddr_t)(SA32((uintptr_t)lwp->lwp_sigaltstack.ss_sp) +
4230Sstevel@tonic-gate 		    SA32(lwp->lwp_sigaltstack.ss_size) - STACK_ALIGN32);
4240Sstevel@tonic-gate 	} else if ((rp->r_ss & 0xffff) != UDS_SEL) {
4250Sstevel@tonic-gate 		user_desc_t *ldt;
4260Sstevel@tonic-gate 		/*
4270Sstevel@tonic-gate 		 * If the stack segment selector is -not- pointing at
4280Sstevel@tonic-gate 		 * the UDS_SEL descriptor and we have an LDT entry for
4290Sstevel@tonic-gate 		 * it instead, add the base address to find the effective va.
4300Sstevel@tonic-gate 		 */
4310Sstevel@tonic-gate 		if ((ldt = p->p_ldt) != NULL)
4320Sstevel@tonic-gate 			fp = (caddr_t)rp->r_sp +
4330Sstevel@tonic-gate 			    USEGD_GETBASE(&ldt[SELTOIDX(rp->r_ss)]);
4340Sstevel@tonic-gate 		else
4350Sstevel@tonic-gate 			fp = (caddr_t)rp->r_sp;
4360Sstevel@tonic-gate 	} else
4370Sstevel@tonic-gate 		fp = (caddr_t)rp->r_sp;
438*1629Sraf 
439*1629Sraf 	/*
440*1629Sraf 	 * Force proper stack pointer alignment, even in the face of a
441*1629Sraf 	 * misaligned stack pointer from user-level before the signal.
442*1629Sraf 	 * Don't use the SA32() macro because that rounds up, not down.
443*1629Sraf 	 */
444*1629Sraf 	fp = (caddr_t)((uintptr_t)fp & ~(STACK_ALIGN32 - 1));
4450Sstevel@tonic-gate 	sp = fp - minstacksz;
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 	/*
4480Sstevel@tonic-gate 	 * Make sure lwp hasn't trashed its stack
4490Sstevel@tonic-gate 	 */
450*1629Sraf 	if (sp >= (caddr_t)(uintptr_t)USERLIMIT32 ||
4510Sstevel@tonic-gate 	    fp >= (caddr_t)(uintptr_t)USERLIMIT32) {
4520Sstevel@tonic-gate #ifdef DEBUG
4530Sstevel@tonic-gate 		printf("sendsig32: bad signal stack cmd=%s, pid=%d, sig=%d\n",
4540Sstevel@tonic-gate 		    PTOU(p)->u_comm, p->p_pid, sig);
4550Sstevel@tonic-gate 		printf("sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
4560Sstevel@tonic-gate 		    (void *)sp, (void *)hdlr, (uintptr_t)upc);
457*1629Sraf 		printf("sp above USERLIMIT\n");
4580Sstevel@tonic-gate #endif
4590Sstevel@tonic-gate 		return (0);
4600Sstevel@tonic-gate 	}
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	watched = watch_disable_addr((caddr_t)sp, minstacksz, S_WRITE);
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	if (on_fault(&ljb))
4650Sstevel@tonic-gate 		goto badstack;
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	if (sip != NULL) {
4680Sstevel@tonic-gate 		siginfo32_t si32;
4690Sstevel@tonic-gate 		zoneid_t zoneid;
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 		siginfo_kto32(sip, &si32);
4720Sstevel@tonic-gate 		if (SI_FROMUSER(sip) &&
4730Sstevel@tonic-gate 		    (zoneid = p->p_zone->zone_id) != GLOBAL_ZONEID &&
4740Sstevel@tonic-gate 		    zoneid != sip->si_zoneid) {
4750Sstevel@tonic-gate 			si32.si_pid = p->p_zone->zone_zsched->p_pid;
4760Sstevel@tonic-gate 			si32.si_uid = 0;
4770Sstevel@tonic-gate 			si32.si_ctid = -1;
4780Sstevel@tonic-gate 			si32.si_zoneid = zoneid;
4790Sstevel@tonic-gate 		}
4800Sstevel@tonic-gate 		fp -= SA32(sizeof (si32));
4810Sstevel@tonic-gate 		uzero(fp, sizeof (si32));
4820Sstevel@tonic-gate 		copyout_noerr(&si32, fp, sizeof (si32));
4830Sstevel@tonic-gate 		sip_addr = (siginfo32_t *)fp;
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 		if (sig == SIGPROF &&
4860Sstevel@tonic-gate 		    curthread->t_rprof != NULL &&
4870Sstevel@tonic-gate 		    curthread->t_rprof->rp_anystate) {
4880Sstevel@tonic-gate 			/*
4890Sstevel@tonic-gate 			 * We stand on our head to deal with
4900Sstevel@tonic-gate 			 * the real-time profiling signal.
4910Sstevel@tonic-gate 			 * Fill in the stuff that doesn't fit
4920Sstevel@tonic-gate 			 * in a normal k_siginfo structure.
4930Sstevel@tonic-gate 			 */
4940Sstevel@tonic-gate 			int i = sip->si_nsysarg;
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 			while (--i >= 0)
4970Sstevel@tonic-gate 				suword32_noerr(&(sip_addr->si_sysarg[i]),
4980Sstevel@tonic-gate 				    (uint32_t)lwp->lwp_arg[i]);
4990Sstevel@tonic-gate 			copyout_noerr(curthread->t_rprof->rp_state,
5000Sstevel@tonic-gate 			    sip_addr->si_mstate,
5010Sstevel@tonic-gate 			    sizeof (curthread->t_rprof->rp_state));
5020Sstevel@tonic-gate 		}
5030Sstevel@tonic-gate 	} else
5040Sstevel@tonic-gate 		sip_addr = NULL;
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 	/* save the current context on the user stack */
5070Sstevel@tonic-gate 	fp -= SA32(sizeof (*tuc));
5080Sstevel@tonic-gate 	uc = (ucontext32_t *)fp;
5090Sstevel@tonic-gate 	tuc = kmem_alloc(sizeof (*tuc), KM_SLEEP);
5100Sstevel@tonic-gate 	savecontext32(tuc, lwp->lwp_sigoldmask);
5110Sstevel@tonic-gate 	copyout_noerr(tuc, uc, sizeof (*tuc));
5120Sstevel@tonic-gate 	kmem_free(tuc, sizeof (*tuc));
5130Sstevel@tonic-gate 	tuc = NULL;
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	lwp->lwp_oldcontext = (uintptr_t)uc;
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	if (newstack) {
5180Sstevel@tonic-gate 		lwp->lwp_sigaltstack.ss_flags |= SS_ONSTACK;
5190Sstevel@tonic-gate 		if (lwp->lwp_ustack) {
5200Sstevel@tonic-gate 			stack32_t stk32;
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 			stk32.ss_sp = (caddr32_t)(uintptr_t)
5230Sstevel@tonic-gate 			    lwp->lwp_sigaltstack.ss_sp;
5240Sstevel@tonic-gate 			stk32.ss_size = (size32_t)
5250Sstevel@tonic-gate 			    lwp->lwp_sigaltstack.ss_size;
5260Sstevel@tonic-gate 			stk32.ss_flags = (int32_t)
5270Sstevel@tonic-gate 			    lwp->lwp_sigaltstack.ss_flags;
5280Sstevel@tonic-gate 			copyout_noerr(&stk32,
5290Sstevel@tonic-gate 			    (stack32_t *)lwp->lwp_ustack, sizeof (stk32));
5300Sstevel@tonic-gate 		}
5310Sstevel@tonic-gate 	}
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	/*
5340Sstevel@tonic-gate 	 * Set up signal handler arguments
5350Sstevel@tonic-gate 	 */
5360Sstevel@tonic-gate 	{
5370Sstevel@tonic-gate 		struct sigframe32 frame32;
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 		frame32.sip = (caddr32_t)(uintptr_t)sip_addr;
5400Sstevel@tonic-gate 		frame32.ucp = (caddr32_t)(uintptr_t)uc;
5410Sstevel@tonic-gate 		frame32.signo = sig;
5420Sstevel@tonic-gate 		frame32.retaddr = 0xffffffff;	/* never return! */
5430Sstevel@tonic-gate 		copyout_noerr(&frame32, sp, sizeof (frame32));
5440Sstevel@tonic-gate 	}
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 	no_fault();
5470Sstevel@tonic-gate 	if (watched)
5480Sstevel@tonic-gate 		watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE);
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	rp->r_sp = (greg_t)(uintptr_t)sp;
5510Sstevel@tonic-gate 	rp->r_pc = (greg_t)(uintptr_t)hdlr;
5520Sstevel@tonic-gate 	rp->r_ps = PSL_USER | (rp->r_ps & PS_IOPL);
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 	if ((rp->r_cs & 0xffff) != U32CS_SEL ||
5550Sstevel@tonic-gate 	    (rp->r_ss & 0xffff) != UDS_SEL) {
5560Sstevel@tonic-gate 		/*
5570Sstevel@tonic-gate 		 * Try our best to deliver the signal.
5580Sstevel@tonic-gate 		 */
5590Sstevel@tonic-gate 		rp->r_cs = U32CS_SEL;
5600Sstevel@tonic-gate 		rp->r_ss = UDS_SEL;
5610Sstevel@tonic-gate 	}
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	/*
5640Sstevel@tonic-gate 	 * Don't set lwp_eosys here.  sendsig() is called via psig() after
5650Sstevel@tonic-gate 	 * lwp_eosys is handled, so setting it here would affect the next
5660Sstevel@tonic-gate 	 * system call.
5670Sstevel@tonic-gate 	 */
5680Sstevel@tonic-gate 	return (1);
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate badstack:
5710Sstevel@tonic-gate 	no_fault();
5720Sstevel@tonic-gate 	if (watched)
5730Sstevel@tonic-gate 		watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE);
5740Sstevel@tonic-gate 	if (tuc)
5750Sstevel@tonic-gate 		kmem_free(tuc, sizeof (*tuc));
5760Sstevel@tonic-gate #ifdef DEBUG
5770Sstevel@tonic-gate 	printf("sendsig32: bad signal stack cmd=%s pid=%d, sig=%d\n",
5780Sstevel@tonic-gate 	    PTOU(p)->u_comm, p->p_pid, sig);
5790Sstevel@tonic-gate 	printf("on fault, sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
5800Sstevel@tonic-gate 	    (void *)sp, (void *)hdlr, (uintptr_t)upc);
5810Sstevel@tonic-gate #endif
5820Sstevel@tonic-gate 	return (0);
5830Sstevel@tonic-gate }
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate #elif defined(__i386)
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate /*
5900Sstevel@tonic-gate  * An i386 SVR4/ABI signal frame looks like this on the stack:
5910Sstevel@tonic-gate  *
5920Sstevel@tonic-gate  * old %esp:
5930Sstevel@tonic-gate  *		<a siginfo32_t [optional]>
5940Sstevel@tonic-gate  *		<a ucontext32_t>
5950Sstevel@tonic-gate  *		<pointer to that ucontext32_t>
5960Sstevel@tonic-gate  *		<pointer to that siginfo32_t>
5970Sstevel@tonic-gate  *		<signo>
5980Sstevel@tonic-gate  * new %esp:	<return address (deliberately invalid)>
5990Sstevel@tonic-gate  */
6000Sstevel@tonic-gate struct sigframe {
6010Sstevel@tonic-gate 	void		(*retaddr)();
6020Sstevel@tonic-gate 	uint_t		signo;
6030Sstevel@tonic-gate 	siginfo_t	*sip;
6040Sstevel@tonic-gate 	ucontext_t	*ucp;
6050Sstevel@tonic-gate };
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate int
6080Sstevel@tonic-gate sendsig(int sig, k_siginfo_t *sip, void (*hdlr)())
6090Sstevel@tonic-gate {
6100Sstevel@tonic-gate 	volatile int minstacksz;
6110Sstevel@tonic-gate 	int newstack;
6120Sstevel@tonic-gate 	label_t ljb;
6130Sstevel@tonic-gate 	volatile caddr_t sp;
6140Sstevel@tonic-gate 	caddr_t fp;
6150Sstevel@tonic-gate 	struct regs *rp;
6160Sstevel@tonic-gate 	volatile greg_t upc;
6170Sstevel@tonic-gate 	volatile proc_t *p = ttoproc(curthread);
6180Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
6190Sstevel@tonic-gate 	ucontext_t *volatile tuc = NULL;
6200Sstevel@tonic-gate 	ucontext_t *uc;
6210Sstevel@tonic-gate 	siginfo_t *sip_addr;
6220Sstevel@tonic-gate 	volatile int watched;
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	rp = lwptoregs(lwp);
6250Sstevel@tonic-gate 	upc = rp->r_pc;
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	minstacksz = SA(sizeof (struct sigframe)) + SA(sizeof (*uc));
6280Sstevel@tonic-gate 	if (sip != NULL)
6290Sstevel@tonic-gate 		minstacksz += SA(sizeof (siginfo_t));
6300Sstevel@tonic-gate 	ASSERT((minstacksz & (STACK_ALIGN - 1ul)) == 0);
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	/*
6330Sstevel@tonic-gate 	 * Figure out whether we will be handling this signal on
6340Sstevel@tonic-gate 	 * an alternate stack specified by the user. Then allocate
6350Sstevel@tonic-gate 	 * and validate the stack requirements for the signal handler
6360Sstevel@tonic-gate 	 * context. on_fault will catch any faults.
6370Sstevel@tonic-gate 	 */
6380Sstevel@tonic-gate 	newstack = sigismember(&u.u_sigonstack, sig) &&
6390Sstevel@tonic-gate 	    !(lwp->lwp_sigaltstack.ss_flags & (SS_ONSTACK|SS_DISABLE));
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	if (newstack) {
6420Sstevel@tonic-gate 		fp = (caddr_t)(SA((uintptr_t)lwp->lwp_sigaltstack.ss_sp) +
6430Sstevel@tonic-gate 		    SA(lwp->lwp_sigaltstack.ss_size) - STACK_ALIGN);
6440Sstevel@tonic-gate 	} else if ((rp->r_ss & 0xffff) != UDS_SEL) {
6450Sstevel@tonic-gate 		user_desc_t *ldt;
6460Sstevel@tonic-gate 		/*
6470Sstevel@tonic-gate 		 * If the stack segment selector is -not- pointing at
6480Sstevel@tonic-gate 		 * the UDS_SEL descriptor and we have an LDT entry for
6490Sstevel@tonic-gate 		 * it instead, add the base address to find the effective va.
6500Sstevel@tonic-gate 		 */
6510Sstevel@tonic-gate 		if ((ldt = p->p_ldt) != NULL)
6520Sstevel@tonic-gate 			fp = (caddr_t)rp->r_sp +
6530Sstevel@tonic-gate 			    USEGD_GETBASE(&ldt[SELTOIDX(rp->r_ss)]);
6540Sstevel@tonic-gate 		else
6550Sstevel@tonic-gate 			fp = (caddr_t)rp->r_sp;
6560Sstevel@tonic-gate 	} else
6570Sstevel@tonic-gate 		fp = (caddr_t)rp->r_sp;
658*1629Sraf 
659*1629Sraf 	/*
660*1629Sraf 	 * Force proper stack pointer alignment, even in the face of a
661*1629Sraf 	 * misaligned stack pointer from user-level before the signal.
662*1629Sraf 	 * Don't use the SA() macro because that rounds up, not down.
663*1629Sraf 	 */
664*1629Sraf 	fp = (caddr_t)((uintptr_t)fp & ~(STACK_ALIGN - 1ul));
6650Sstevel@tonic-gate 	sp = fp - minstacksz;
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 	/*
6680Sstevel@tonic-gate 	 * Make sure lwp hasn't trashed its stack.
6690Sstevel@tonic-gate 	 */
670*1629Sraf 	if (sp >= (caddr_t)USERLIMIT || fp >= (caddr_t)USERLIMIT) {
6710Sstevel@tonic-gate #ifdef DEBUG
6720Sstevel@tonic-gate 		printf("sendsig: bad signal stack cmd=%s, pid=%d, sig=%d\n",
6730Sstevel@tonic-gate 		    PTOU(p)->u_comm, p->p_pid, sig);
6740Sstevel@tonic-gate 		printf("sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
6750Sstevel@tonic-gate 		    (void *)sp, (void *)hdlr, (uintptr_t)upc);
676*1629Sraf 		printf("sp above USERLIMIT\n");
6770Sstevel@tonic-gate #endif
6780Sstevel@tonic-gate 		return (0);
6790Sstevel@tonic-gate 	}
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	watched = watch_disable_addr((caddr_t)sp, minstacksz, S_WRITE);
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 	if (on_fault(&ljb))
6840Sstevel@tonic-gate 		goto badstack;
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 	if (sip != NULL) {
6870Sstevel@tonic-gate 		zoneid_t zoneid;
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 		fp -= SA(sizeof (siginfo_t));
6900Sstevel@tonic-gate 		uzero(fp, sizeof (siginfo_t));
6910Sstevel@tonic-gate 		if (SI_FROMUSER(sip) &&
6920Sstevel@tonic-gate 		    (zoneid = p->p_zone->zone_id) != GLOBAL_ZONEID &&
6930Sstevel@tonic-gate 		    zoneid != sip->si_zoneid) {
6940Sstevel@tonic-gate 			k_siginfo_t sani_sip = *sip;
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 			sani_sip.si_pid = p->p_zone->zone_zsched->p_pid;
6970Sstevel@tonic-gate 			sani_sip.si_uid = 0;
6980Sstevel@tonic-gate 			sani_sip.si_ctid = -1;
6990Sstevel@tonic-gate 			sani_sip.si_zoneid = zoneid;
7000Sstevel@tonic-gate 			copyout_noerr(&sani_sip, fp, sizeof (sani_sip));
7010Sstevel@tonic-gate 		} else
7020Sstevel@tonic-gate 			copyout_noerr(sip, fp, sizeof (*sip));
7030Sstevel@tonic-gate 		sip_addr = (siginfo_t *)fp;
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 		if (sig == SIGPROF &&
7060Sstevel@tonic-gate 		    curthread->t_rprof != NULL &&
7070Sstevel@tonic-gate 		    curthread->t_rprof->rp_anystate) {
7080Sstevel@tonic-gate 			/*
7090Sstevel@tonic-gate 			 * We stand on our head to deal with
7100Sstevel@tonic-gate 			 * the real time profiling signal.
7110Sstevel@tonic-gate 			 * Fill in the stuff that doesn't fit
7120Sstevel@tonic-gate 			 * in a normal k_siginfo structure.
7130Sstevel@tonic-gate 			 */
7140Sstevel@tonic-gate 			int i = sip->si_nsysarg;
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 			while (--i >= 0)
7170Sstevel@tonic-gate 				suword32_noerr(&(sip_addr->si_sysarg[i]),
7180Sstevel@tonic-gate 				    (uint32_t)lwp->lwp_arg[i]);
7190Sstevel@tonic-gate 			copyout_noerr(curthread->t_rprof->rp_state,
7200Sstevel@tonic-gate 			    sip_addr->si_mstate,
7210Sstevel@tonic-gate 			    sizeof (curthread->t_rprof->rp_state));
7220Sstevel@tonic-gate 		}
7230Sstevel@tonic-gate 	} else
7240Sstevel@tonic-gate 		sip_addr = NULL;
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 	/* save the current context on the user stack */
7270Sstevel@tonic-gate 	fp -= SA(sizeof (*tuc));
7280Sstevel@tonic-gate 	uc = (ucontext_t *)fp;
7290Sstevel@tonic-gate 	tuc = kmem_alloc(sizeof (*tuc), KM_SLEEP);
7300Sstevel@tonic-gate 	savecontext(tuc, lwp->lwp_sigoldmask);
7310Sstevel@tonic-gate 	copyout_noerr(tuc, uc, sizeof (*tuc));
7320Sstevel@tonic-gate 	kmem_free(tuc, sizeof (*tuc));
7330Sstevel@tonic-gate 	tuc = NULL;
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 	lwp->lwp_oldcontext = (uintptr_t)uc;
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 	if (newstack) {
7380Sstevel@tonic-gate 		lwp->lwp_sigaltstack.ss_flags |= SS_ONSTACK;
7390Sstevel@tonic-gate 		if (lwp->lwp_ustack)
7400Sstevel@tonic-gate 			copyout_noerr(&lwp->lwp_sigaltstack,
7410Sstevel@tonic-gate 			    (stack_t *)lwp->lwp_ustack, sizeof (stack_t));
7420Sstevel@tonic-gate 	}
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	/*
7450Sstevel@tonic-gate 	 * Set up signal handler arguments
7460Sstevel@tonic-gate 	 */
7470Sstevel@tonic-gate 	{
7480Sstevel@tonic-gate 		struct sigframe frame;
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 		frame.sip = sip_addr;
7510Sstevel@tonic-gate 		frame.ucp = uc;
7520Sstevel@tonic-gate 		frame.signo = sig;
7530Sstevel@tonic-gate 		frame.retaddr = (void (*)())0xffffffff;	/* never return! */
7540Sstevel@tonic-gate 		copyout_noerr(&frame, sp, sizeof (frame));
7550Sstevel@tonic-gate 	}
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 	no_fault();
7580Sstevel@tonic-gate 	if (watched)
7590Sstevel@tonic-gate 		watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE);
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 	rp->r_sp = (greg_t)sp;
7620Sstevel@tonic-gate 	rp->r_pc = (greg_t)hdlr;
7630Sstevel@tonic-gate 	rp->r_ps = PSL_USER | (rp->r_ps & PS_IOPL);
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	if ((rp->r_cs & 0xffff) != UCS_SEL ||
7660Sstevel@tonic-gate 	    (rp->r_ss & 0xffff) != UDS_SEL) {
7670Sstevel@tonic-gate 		rp->r_cs = UCS_SEL;
7680Sstevel@tonic-gate 		rp->r_ss = UDS_SEL;
7690Sstevel@tonic-gate 	}
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	/*
7720Sstevel@tonic-gate 	 * Don't set lwp_eosys here.  sendsig() is called via psig() after
7730Sstevel@tonic-gate 	 * lwp_eosys is handled, so setting it here would affect the next
7740Sstevel@tonic-gate 	 * system call.
7750Sstevel@tonic-gate 	 */
7760Sstevel@tonic-gate 	return (1);
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate badstack:
7790Sstevel@tonic-gate 	no_fault();
7800Sstevel@tonic-gate 	if (watched)
7810Sstevel@tonic-gate 		watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE);
7820Sstevel@tonic-gate 	if (tuc)
7830Sstevel@tonic-gate 		kmem_free(tuc, sizeof (*tuc));
7840Sstevel@tonic-gate #ifdef DEBUG
7850Sstevel@tonic-gate 	printf("sendsig: bad signal stack cmd=%s, pid=%d, sig=%d\n",
7860Sstevel@tonic-gate 	    PTOU(p)->u_comm, p->p_pid, sig);
7870Sstevel@tonic-gate 	printf("on fault, sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
7880Sstevel@tonic-gate 	    (void *)sp, (void *)hdlr, (uintptr_t)upc);
7890Sstevel@tonic-gate #endif
7900Sstevel@tonic-gate 	return (0);
7910Sstevel@tonic-gate }
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate #endif	/* __i386 */
794