xref: /onnv-gate/usr/src/uts/intel/ia32/os/sendsig.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */
28*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T   */
29*0Sstevel@tonic-gate /*	All Rights Reserved   */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <sys/types.h>
34*0Sstevel@tonic-gate #include <sys/param.h>
35*0Sstevel@tonic-gate #include <sys/sysmacros.h>
36*0Sstevel@tonic-gate #include <sys/signal.h>
37*0Sstevel@tonic-gate #include <sys/systm.h>
38*0Sstevel@tonic-gate #include <sys/user.h>
39*0Sstevel@tonic-gate #include <sys/mman.h>
40*0Sstevel@tonic-gate #include <sys/class.h>
41*0Sstevel@tonic-gate #include <sys/proc.h>
42*0Sstevel@tonic-gate #include <sys/procfs.h>
43*0Sstevel@tonic-gate #include <sys/buf.h>
44*0Sstevel@tonic-gate #include <sys/kmem.h>
45*0Sstevel@tonic-gate #include <sys/cred.h>
46*0Sstevel@tonic-gate #include <sys/archsystm.h>
47*0Sstevel@tonic-gate #include <sys/vmparam.h>
48*0Sstevel@tonic-gate #include <sys/prsystm.h>
49*0Sstevel@tonic-gate #include <sys/reboot.h>
50*0Sstevel@tonic-gate #include <sys/uadmin.h>
51*0Sstevel@tonic-gate #include <sys/vfs.h>
52*0Sstevel@tonic-gate #include <sys/vnode.h>
53*0Sstevel@tonic-gate #include <sys/file.h>
54*0Sstevel@tonic-gate #include <sys/session.h>
55*0Sstevel@tonic-gate #include <sys/ucontext.h>
56*0Sstevel@tonic-gate #include <sys/dnlc.h>
57*0Sstevel@tonic-gate #include <sys/var.h>
58*0Sstevel@tonic-gate #include <sys/cmn_err.h>
59*0Sstevel@tonic-gate #include <sys/debugreg.h>
60*0Sstevel@tonic-gate #include <sys/thread.h>
61*0Sstevel@tonic-gate #include <sys/vtrace.h>
62*0Sstevel@tonic-gate #include <sys/consdev.h>
63*0Sstevel@tonic-gate #include <sys/psw.h>
64*0Sstevel@tonic-gate #include <sys/regset.h>
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate #include <sys/privregs.h>
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate #include <sys/stack.h>
69*0Sstevel@tonic-gate #include <sys/swap.h>
70*0Sstevel@tonic-gate #include <vm/hat.h>
71*0Sstevel@tonic-gate #include <vm/anon.h>
72*0Sstevel@tonic-gate #include <vm/as.h>
73*0Sstevel@tonic-gate #include <vm/page.h>
74*0Sstevel@tonic-gate #include <vm/seg.h>
75*0Sstevel@tonic-gate #include <vm/seg_kmem.h>
76*0Sstevel@tonic-gate #include <vm/seg_map.h>
77*0Sstevel@tonic-gate #include <vm/seg_vn.h>
78*0Sstevel@tonic-gate #include <sys/exec.h>
79*0Sstevel@tonic-gate #include <sys/acct.h>
80*0Sstevel@tonic-gate #include <sys/core.h>
81*0Sstevel@tonic-gate #include <sys/corectl.h>
82*0Sstevel@tonic-gate #include <sys/modctl.h>
83*0Sstevel@tonic-gate #include <sys/tuneable.h>
84*0Sstevel@tonic-gate #include <c2/audit.h>
85*0Sstevel@tonic-gate #include <sys/bootconf.h>
86*0Sstevel@tonic-gate #include <sys/dumphdr.h>
87*0Sstevel@tonic-gate #include <sys/promif.h>
88*0Sstevel@tonic-gate #include <sys/systeminfo.h>
89*0Sstevel@tonic-gate #include <sys/kdi.h>
90*0Sstevel@tonic-gate #include <sys/contract_impl.h>
91*0Sstevel@tonic-gate #include <sys/x86_archext.h>
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate /*
94*0Sstevel@tonic-gate  * Construct the execution environment for the user's signal
95*0Sstevel@tonic-gate  * handler and arrange for control to be given to it on return
96*0Sstevel@tonic-gate  * to userland.  The library code now calls setcontext() to
97*0Sstevel@tonic-gate  * clean up after the signal handler, so sigret() is no longer
98*0Sstevel@tonic-gate  * needed.
99*0Sstevel@tonic-gate  *
100*0Sstevel@tonic-gate  * (The various 'volatile' declarations are need to ensure that values
101*0Sstevel@tonic-gate  * are correct on the error return from on_fault().)
102*0Sstevel@tonic-gate  */
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate #if defined(__amd64)
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate /*
107*0Sstevel@tonic-gate  * An amd64 signal frame looks like this on the stack:
108*0Sstevel@tonic-gate  *
109*0Sstevel@tonic-gate  * old %rsp:
110*0Sstevel@tonic-gate  *		<128 bytes of untouched stack space>
111*0Sstevel@tonic-gate  *		<a siginfo_t [optional]>
112*0Sstevel@tonic-gate  *		<a ucontext_t>
113*0Sstevel@tonic-gate  *		<siginfo_t *>
114*0Sstevel@tonic-gate  *		<signal number>
115*0Sstevel@tonic-gate  * new %rsp:	<return address (deliberately invalid)>
116*0Sstevel@tonic-gate  *
117*0Sstevel@tonic-gate  * The signal number and siginfo_t pointer are only pushed onto the stack in
118*0Sstevel@tonic-gate  * order to allow stack backtraces.  The actual signal handling code expects the
119*0Sstevel@tonic-gate  * arguments in registers.
120*0Sstevel@tonic-gate  */
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate struct sigframe {
123*0Sstevel@tonic-gate 	caddr_t retaddr;
124*0Sstevel@tonic-gate 	long	signo;
125*0Sstevel@tonic-gate 	siginfo_t *sip;
126*0Sstevel@tonic-gate };
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate int
129*0Sstevel@tonic-gate sendsig(int sig, k_siginfo_t *sip, void (*hdlr)())
130*0Sstevel@tonic-gate {
131*0Sstevel@tonic-gate 	volatile int minstacksz;
132*0Sstevel@tonic-gate 	int newstack;
133*0Sstevel@tonic-gate 	label_t ljb;
134*0Sstevel@tonic-gate 	volatile caddr_t sp;
135*0Sstevel@tonic-gate 	caddr_t fp;
136*0Sstevel@tonic-gate 	volatile struct regs *rp;
137*0Sstevel@tonic-gate 	volatile greg_t upc;
138*0Sstevel@tonic-gate 	volatile proc_t *p = ttoproc(curthread);
139*0Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
140*0Sstevel@tonic-gate 	ucontext_t *volatile tuc = NULL;
141*0Sstevel@tonic-gate 	ucontext_t *uc;
142*0Sstevel@tonic-gate 	siginfo_t *sip_addr;
143*0Sstevel@tonic-gate 	volatile int watched;
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	/*
146*0Sstevel@tonic-gate 	 * This routine is utterly dependent upon STACK_ALIGN being
147*0Sstevel@tonic-gate 	 * 16 and STACK_ENTRY_ALIGN being 8. Let's just acknowledge
148*0Sstevel@tonic-gate 	 * that and require it.
149*0Sstevel@tonic-gate 	 */
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate #if STACK_ALIGN != 16 || STACK_ENTRY_ALIGN != 8
152*0Sstevel@tonic-gate #error "sendsig() amd64 did not find the expected stack alignments"
153*0Sstevel@tonic-gate #endif
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 	rp = lwptoregs(lwp);
156*0Sstevel@tonic-gate 	upc = rp->r_pc;
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 	/*
159*0Sstevel@tonic-gate 	 * Since we're setting up to run the signal handler we have to
160*0Sstevel@tonic-gate 	 * arrange that the stack at entry to the handler is (only)
161*0Sstevel@tonic-gate 	 * STACK_ENTRY_ALIGN (i.e. 8) byte aligned so that when the handler
162*0Sstevel@tonic-gate 	 * executes its push of %rbp, the stack realigns to STACK_ALIGN
163*0Sstevel@tonic-gate 	 * (i.e. 16) correctly.
164*0Sstevel@tonic-gate 	 *
165*0Sstevel@tonic-gate 	 * The new sp will point to the sigframe and the ucontext_t. The
166*0Sstevel@tonic-gate 	 * above means that sp (and thus sigframe) will be 8-byte aligned,
167*0Sstevel@tonic-gate 	 * but not 16-byte aligned. ucontext_t, however, contains %xmm regs
168*0Sstevel@tonic-gate 	 * which must be 16-byte aligned. Because of this, for correct
169*0Sstevel@tonic-gate 	 * alignment, sigframe must be a multiple of 8-bytes in length, but
170*0Sstevel@tonic-gate 	 * not 16-bytes. This will place ucontext_t at a nice 16-byte boundary.
171*0Sstevel@tonic-gate 	 */
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 	/* LINTED: logical expression always true: op "||" */
174*0Sstevel@tonic-gate 	ASSERT((sizeof (struct sigframe) % 16) == 8);
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 	minstacksz = sizeof (struct sigframe) + SA(sizeof (*uc));
177*0Sstevel@tonic-gate 	if (sip != NULL)
178*0Sstevel@tonic-gate 		minstacksz += SA(sizeof (siginfo_t));
179*0Sstevel@tonic-gate 	ASSERT((minstacksz & (STACK_ENTRY_ALIGN - 1ul)) == 0);
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	/*
182*0Sstevel@tonic-gate 	 * Figure out whether we will be handling this signal on
183*0Sstevel@tonic-gate 	 * an alternate stack specified by the user.  Then allocate
184*0Sstevel@tonic-gate 	 * and validate the stack requirements for the signal handler
185*0Sstevel@tonic-gate 	 * context.  on_fault will catch any faults.
186*0Sstevel@tonic-gate 	 */
187*0Sstevel@tonic-gate 	newstack = sigismember(&u.u_sigonstack, sig) &&
188*0Sstevel@tonic-gate 	    !(lwp->lwp_sigaltstack.ss_flags & (SS_ONSTACK|SS_DISABLE));
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 	if (newstack) {
191*0Sstevel@tonic-gate 		fp = (caddr_t)(SA((uintptr_t)lwp->lwp_sigaltstack.ss_sp) +
192*0Sstevel@tonic-gate 		    SA(lwp->lwp_sigaltstack.ss_size) - STACK_ALIGN);
193*0Sstevel@tonic-gate 	} else {
194*0Sstevel@tonic-gate 		/*
195*0Sstevel@tonic-gate 		 * Drop below the 128-byte reserved region of the stack frame
196*0Sstevel@tonic-gate 		 * we're interrupting.
197*0Sstevel@tonic-gate 		 */
198*0Sstevel@tonic-gate 		fp = (caddr_t)rp->r_sp - STACK_RESERVE;
199*0Sstevel@tonic-gate 	}
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 	/*
202*0Sstevel@tonic-gate 	 * Most of the time during normal execution, the stack pointer
203*0Sstevel@tonic-gate 	 * is aligned on a STACK_ALIGN (i.e. 16 byte) boundary.  However,
204*0Sstevel@tonic-gate 	 * (for example) just after a call instruction (which pushes
205*0Sstevel@tonic-gate 	 * the return address), the callers stack misaligns until the
206*0Sstevel@tonic-gate 	 * 'push %rbp' happens in the callee prolog.  So while we should
207*0Sstevel@tonic-gate 	 * expect the stack pointer to be always at least STACK_ENTRY_ALIGN
208*0Sstevel@tonic-gate 	 * aligned, we should -not- expect it to always be STACK_ALIGN aligned.
209*0Sstevel@tonic-gate 	 * We now adjust to ensure that the new sp is aligned to
210*0Sstevel@tonic-gate 	 * STACK_ENTRY_ALIGN but not to STACK_ALIGN.
211*0Sstevel@tonic-gate 	 */
212*0Sstevel@tonic-gate 	sp = fp - minstacksz;
213*0Sstevel@tonic-gate 	if (((uintptr_t)sp & (STACK_ALIGN - 1ul)) == 0) {
214*0Sstevel@tonic-gate 		sp -= STACK_ENTRY_ALIGN;
215*0Sstevel@tonic-gate 		minstacksz = fp - sp;
216*0Sstevel@tonic-gate 	}
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 	/*
219*0Sstevel@tonic-gate 	 * Now, make sure the resulting signal frame address is sane
220*0Sstevel@tonic-gate 	 */
221*0Sstevel@tonic-gate 	if (((uintptr_t)(sp - STACK_ENTRY_ALIGN) & (STACK_ALIGN - 1ul)) != 0 ||
222*0Sstevel@tonic-gate 	    sp >= (caddr_t)USERLIMIT || fp >= (caddr_t)USERLIMIT) {
223*0Sstevel@tonic-gate #ifdef DEBUG
224*0Sstevel@tonic-gate 		printf("sendsig: bad signal stack cmd=%s, pid=%d, sig=%d\n",
225*0Sstevel@tonic-gate 		    PTOU(p)->u_comm, p->p_pid, sig);
226*0Sstevel@tonic-gate 		printf("sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
227*0Sstevel@tonic-gate 		    (void *)sp, (void *)hdlr, (uintptr_t)upc);
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate 		if (((uintptr_t)(sp - STACK_ENTRY_ALIGN) &
230*0Sstevel@tonic-gate 		    (STACK_ALIGN - 1ul)) != 0)
231*0Sstevel@tonic-gate 			printf("bad stack alignment\n");
232*0Sstevel@tonic-gate 		else
233*0Sstevel@tonic-gate 			printf("sp above USERLIMIT\n");
234*0Sstevel@tonic-gate #endif
235*0Sstevel@tonic-gate 		return (0);
236*0Sstevel@tonic-gate 	}
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate 	watched = watch_disable_addr((caddr_t)sp, minstacksz, S_WRITE);
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate 	if (on_fault(&ljb))
241*0Sstevel@tonic-gate 		goto badstack;
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate 	if (sip != NULL) {
244*0Sstevel@tonic-gate 		zoneid_t zoneid;
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate 		fp -= SA(sizeof (siginfo_t));
247*0Sstevel@tonic-gate 		uzero(fp, sizeof (siginfo_t));
248*0Sstevel@tonic-gate 		if (SI_FROMUSER(sip) &&
249*0Sstevel@tonic-gate 		    (zoneid = p->p_zone->zone_id) != GLOBAL_ZONEID &&
250*0Sstevel@tonic-gate 		    zoneid != sip->si_zoneid) {
251*0Sstevel@tonic-gate 			k_siginfo_t sani_sip = *sip;
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate 			sani_sip.si_pid = p->p_zone->zone_zsched->p_pid;
254*0Sstevel@tonic-gate 			sani_sip.si_uid = 0;
255*0Sstevel@tonic-gate 			sani_sip.si_ctid = -1;
256*0Sstevel@tonic-gate 			sani_sip.si_zoneid = zoneid;
257*0Sstevel@tonic-gate 			copyout_noerr(&sani_sip, fp, sizeof (sani_sip));
258*0Sstevel@tonic-gate 		} else
259*0Sstevel@tonic-gate 			copyout_noerr(sip, fp, sizeof (*sip));
260*0Sstevel@tonic-gate 		sip_addr = (siginfo_t *)fp;
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate 		if (sig == SIGPROF &&
263*0Sstevel@tonic-gate 		    curthread->t_rprof != NULL &&
264*0Sstevel@tonic-gate 		    curthread->t_rprof->rp_anystate) {
265*0Sstevel@tonic-gate 			/*
266*0Sstevel@tonic-gate 			 * We stand on our head to deal with
267*0Sstevel@tonic-gate 			 * the real time profiling signal.
268*0Sstevel@tonic-gate 			 * Fill in the stuff that doesn't fit
269*0Sstevel@tonic-gate 			 * in a normal k_siginfo structure.
270*0Sstevel@tonic-gate 			 */
271*0Sstevel@tonic-gate 			int i = sip->si_nsysarg;
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 			while (--i >= 0)
274*0Sstevel@tonic-gate 				sulword_noerr(
275*0Sstevel@tonic-gate 				    (ulong_t *)&(sip_addr->si_sysarg[i]),
276*0Sstevel@tonic-gate 				    (ulong_t)lwp->lwp_arg[i]);
277*0Sstevel@tonic-gate 			copyout_noerr(curthread->t_rprof->rp_state,
278*0Sstevel@tonic-gate 			    sip_addr->si_mstate,
279*0Sstevel@tonic-gate 			    sizeof (curthread->t_rprof->rp_state));
280*0Sstevel@tonic-gate 		}
281*0Sstevel@tonic-gate 	} else
282*0Sstevel@tonic-gate 		sip_addr = NULL;
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate 	/*
285*0Sstevel@tonic-gate 	 * save the current context on the user stack directly after the
286*0Sstevel@tonic-gate 	 * sigframe. Since sigframe is 8-byte-but-not-16-byte aligned,
287*0Sstevel@tonic-gate 	 * and since sizeof (struct sigframe) is 24, this guarantees
288*0Sstevel@tonic-gate 	 * 16-byte alignment for ucontext_t and its %xmm registers.
289*0Sstevel@tonic-gate 	 */
290*0Sstevel@tonic-gate 	uc = (ucontext_t *)(sp + sizeof (struct sigframe));
291*0Sstevel@tonic-gate 	tuc = kmem_alloc(sizeof (*tuc), KM_SLEEP);
292*0Sstevel@tonic-gate 	savecontext(tuc, lwp->lwp_sigoldmask);
293*0Sstevel@tonic-gate 	copyout_noerr(tuc, uc, sizeof (*tuc));
294*0Sstevel@tonic-gate 	kmem_free(tuc, sizeof (*tuc));
295*0Sstevel@tonic-gate 	tuc = NULL;
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate 	lwp->lwp_oldcontext = (uintptr_t)uc;
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate 	if (newstack) {
300*0Sstevel@tonic-gate 		lwp->lwp_sigaltstack.ss_flags |= SS_ONSTACK;
301*0Sstevel@tonic-gate 		if (lwp->lwp_ustack)
302*0Sstevel@tonic-gate 			copyout_noerr(&lwp->lwp_sigaltstack,
303*0Sstevel@tonic-gate 			    (stack_t *)lwp->lwp_ustack, sizeof (stack_t));
304*0Sstevel@tonic-gate 	}
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate 	/*
307*0Sstevel@tonic-gate 	 * Set up signal handler return and stack linkage
308*0Sstevel@tonic-gate 	 */
309*0Sstevel@tonic-gate 	{
310*0Sstevel@tonic-gate 		struct sigframe frame;
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate 		/*
313*0Sstevel@tonic-gate 		 * ensure we never return "normally"
314*0Sstevel@tonic-gate 		 */
315*0Sstevel@tonic-gate 		frame.retaddr = (caddr_t)(uintptr_t)-1L;
316*0Sstevel@tonic-gate 		frame.signo = sig;
317*0Sstevel@tonic-gate 		frame.sip = sip_addr;
318*0Sstevel@tonic-gate 		copyout_noerr(&frame, sp, sizeof (frame));
319*0Sstevel@tonic-gate 	}
320*0Sstevel@tonic-gate 
321*0Sstevel@tonic-gate 	no_fault();
322*0Sstevel@tonic-gate 	if (watched)
323*0Sstevel@tonic-gate 		watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE);
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate 	/*
326*0Sstevel@tonic-gate 	 * Set up user registers for execution of signal handler.
327*0Sstevel@tonic-gate 	 */
328*0Sstevel@tonic-gate 	rp->r_sp = (greg_t)sp;
329*0Sstevel@tonic-gate 	rp->r_pc = (greg_t)hdlr;
330*0Sstevel@tonic-gate 	rp->r_ps = PSL_USER | (rp->r_ps & PS_IOPL);
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate 	rp->r_rdi = sig;
333*0Sstevel@tonic-gate 	rp->r_rsi = (uintptr_t)sip_addr;
334*0Sstevel@tonic-gate 	rp->r_rdx = (uintptr_t)uc;
335*0Sstevel@tonic-gate 
336*0Sstevel@tonic-gate 	if ((rp->r_cs & 0xffff) != UCS_SEL ||
337*0Sstevel@tonic-gate 	    (rp->r_ss & 0xffff) != UDS_SEL) {
338*0Sstevel@tonic-gate 		/*
339*0Sstevel@tonic-gate 		 * Try our best to deliver the signal.
340*0Sstevel@tonic-gate 		 */
341*0Sstevel@tonic-gate 		rp->r_cs = UCS_SEL;
342*0Sstevel@tonic-gate 		rp->r_ss = UDS_SEL;
343*0Sstevel@tonic-gate 	}
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate 	/*
346*0Sstevel@tonic-gate 	 * Don't set lwp_eosys here.  sendsig() is called via psig() after
347*0Sstevel@tonic-gate 	 * lwp_eosys is handled, so setting it here would affect the next
348*0Sstevel@tonic-gate 	 * system call.
349*0Sstevel@tonic-gate 	 */
350*0Sstevel@tonic-gate 	return (1);
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate badstack:
353*0Sstevel@tonic-gate 	no_fault();
354*0Sstevel@tonic-gate 	if (watched)
355*0Sstevel@tonic-gate 		watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE);
356*0Sstevel@tonic-gate 	if (tuc)
357*0Sstevel@tonic-gate 		kmem_free(tuc, sizeof (*tuc));
358*0Sstevel@tonic-gate #ifdef DEBUG
359*0Sstevel@tonic-gate 	printf("sendsig: bad signal stack cmd=%s, pid=%d, sig=%d\n",
360*0Sstevel@tonic-gate 	    PTOU(p)->u_comm, p->p_pid, sig);
361*0Sstevel@tonic-gate 	printf("on fault, sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
362*0Sstevel@tonic-gate 	    (void *)sp, (void *)hdlr, (uintptr_t)upc);
363*0Sstevel@tonic-gate #endif
364*0Sstevel@tonic-gate 	return (0);
365*0Sstevel@tonic-gate }
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
368*0Sstevel@tonic-gate 
369*0Sstevel@tonic-gate /*
370*0Sstevel@tonic-gate  * An i386 SVR4/ABI signal frame looks like this on the stack:
371*0Sstevel@tonic-gate  *
372*0Sstevel@tonic-gate  * old %esp:
373*0Sstevel@tonic-gate  *		<a siginfo32_t [optional]>
374*0Sstevel@tonic-gate  *		<a ucontext32_t>
375*0Sstevel@tonic-gate  *		<pointer to that ucontext32_t>
376*0Sstevel@tonic-gate  *		<pointer to that siginfo32_t>
377*0Sstevel@tonic-gate  *		<signo>
378*0Sstevel@tonic-gate  * new %esp:	<return address (deliberately invalid)>
379*0Sstevel@tonic-gate  */
380*0Sstevel@tonic-gate struct sigframe32 {
381*0Sstevel@tonic-gate 	caddr32_t	retaddr;
382*0Sstevel@tonic-gate 	uint32_t	signo;
383*0Sstevel@tonic-gate 	caddr32_t	sip;
384*0Sstevel@tonic-gate 	caddr32_t	ucp;
385*0Sstevel@tonic-gate };
386*0Sstevel@tonic-gate 
387*0Sstevel@tonic-gate int
388*0Sstevel@tonic-gate sendsig32(int sig, k_siginfo_t *sip, void (*hdlr)())
389*0Sstevel@tonic-gate {
390*0Sstevel@tonic-gate 	volatile int minstacksz;
391*0Sstevel@tonic-gate 	int newstack;
392*0Sstevel@tonic-gate 	label_t ljb;
393*0Sstevel@tonic-gate 	volatile caddr_t sp;
394*0Sstevel@tonic-gate 	caddr_t fp;
395*0Sstevel@tonic-gate 	volatile struct regs *rp;
396*0Sstevel@tonic-gate 	volatile greg_t upc;
397*0Sstevel@tonic-gate 	volatile proc_t *p = ttoproc(curthread);
398*0Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
399*0Sstevel@tonic-gate 	ucontext32_t *volatile tuc = NULL;
400*0Sstevel@tonic-gate 	ucontext32_t *uc;
401*0Sstevel@tonic-gate 	siginfo32_t *sip_addr;
402*0Sstevel@tonic-gate 	volatile int watched;
403*0Sstevel@tonic-gate 
404*0Sstevel@tonic-gate 	rp = lwptoregs(lwp);
405*0Sstevel@tonic-gate 	upc = rp->r_pc;
406*0Sstevel@tonic-gate 
407*0Sstevel@tonic-gate 	minstacksz = SA32(sizeof (struct sigframe32)) + SA32(sizeof (*uc));
408*0Sstevel@tonic-gate 	if (sip != NULL)
409*0Sstevel@tonic-gate 		minstacksz += SA32(sizeof (siginfo32_t));
410*0Sstevel@tonic-gate 	ASSERT((minstacksz & (STACK_ALIGN32 - 1)) == 0);
411*0Sstevel@tonic-gate 
412*0Sstevel@tonic-gate 	/*
413*0Sstevel@tonic-gate 	 * Figure out whether we will be handling this signal on
414*0Sstevel@tonic-gate 	 * an alternate stack specified by the user.  Then allocate
415*0Sstevel@tonic-gate 	 * and validate the stack requirements for the signal handler
416*0Sstevel@tonic-gate 	 * context.  on_fault will catch any faults.
417*0Sstevel@tonic-gate 	 */
418*0Sstevel@tonic-gate 	newstack = sigismember(&u.u_sigonstack, sig) &&
419*0Sstevel@tonic-gate 	    !(lwp->lwp_sigaltstack.ss_flags & (SS_ONSTACK|SS_DISABLE));
420*0Sstevel@tonic-gate 
421*0Sstevel@tonic-gate 	if (newstack) {
422*0Sstevel@tonic-gate 		fp = (caddr_t)(SA32((uintptr_t)lwp->lwp_sigaltstack.ss_sp) +
423*0Sstevel@tonic-gate 		    SA32(lwp->lwp_sigaltstack.ss_size) - STACK_ALIGN32);
424*0Sstevel@tonic-gate 	} else if ((rp->r_ss & 0xffff) != UDS_SEL) {
425*0Sstevel@tonic-gate 		user_desc_t *ldt;
426*0Sstevel@tonic-gate 		/*
427*0Sstevel@tonic-gate 		 * If the stack segment selector is -not- pointing at
428*0Sstevel@tonic-gate 		 * the UDS_SEL descriptor and we have an LDT entry for
429*0Sstevel@tonic-gate 		 * it instead, add the base address to find the effective va.
430*0Sstevel@tonic-gate 		 */
431*0Sstevel@tonic-gate 		if ((ldt = p->p_ldt) != NULL)
432*0Sstevel@tonic-gate 			fp = (caddr_t)rp->r_sp +
433*0Sstevel@tonic-gate 			    USEGD_GETBASE(&ldt[SELTOIDX(rp->r_ss)]);
434*0Sstevel@tonic-gate 		else
435*0Sstevel@tonic-gate 			fp = (caddr_t)rp->r_sp;
436*0Sstevel@tonic-gate 	} else
437*0Sstevel@tonic-gate 		fp = (caddr_t)rp->r_sp;
438*0Sstevel@tonic-gate 	sp = fp - minstacksz;
439*0Sstevel@tonic-gate 
440*0Sstevel@tonic-gate 	/*
441*0Sstevel@tonic-gate 	 * Make sure lwp hasn't trashed its stack
442*0Sstevel@tonic-gate 	 */
443*0Sstevel@tonic-gate 	if (((uintptr_t)sp & (STACK_ALIGN32 - 1)) != 0 ||
444*0Sstevel@tonic-gate 	    sp >= (caddr_t)(uintptr_t)USERLIMIT32 ||
445*0Sstevel@tonic-gate 	    fp >= (caddr_t)(uintptr_t)USERLIMIT32) {
446*0Sstevel@tonic-gate #ifdef DEBUG
447*0Sstevel@tonic-gate 		printf("sendsig32: bad signal stack cmd=%s, pid=%d, sig=%d\n",
448*0Sstevel@tonic-gate 		    PTOU(p)->u_comm, p->p_pid, sig);
449*0Sstevel@tonic-gate 		printf("sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
450*0Sstevel@tonic-gate 		    (void *)sp, (void *)hdlr, (uintptr_t)upc);
451*0Sstevel@tonic-gate 
452*0Sstevel@tonic-gate 		if (((uintptr_t)sp & (STACK_ALIGN32 - 1)) != 0)
453*0Sstevel@tonic-gate 			printf("bad stack alignment\n");
454*0Sstevel@tonic-gate 		else
455*0Sstevel@tonic-gate 			printf("sp above USERLIMIT\n");
456*0Sstevel@tonic-gate #endif
457*0Sstevel@tonic-gate 		return (0);
458*0Sstevel@tonic-gate 	}
459*0Sstevel@tonic-gate 
460*0Sstevel@tonic-gate 	watched = watch_disable_addr((caddr_t)sp, minstacksz, S_WRITE);
461*0Sstevel@tonic-gate 
462*0Sstevel@tonic-gate 	if (on_fault(&ljb))
463*0Sstevel@tonic-gate 		goto badstack;
464*0Sstevel@tonic-gate 
465*0Sstevel@tonic-gate 	if (sip != NULL) {
466*0Sstevel@tonic-gate 		siginfo32_t si32;
467*0Sstevel@tonic-gate 		zoneid_t zoneid;
468*0Sstevel@tonic-gate 
469*0Sstevel@tonic-gate 		siginfo_kto32(sip, &si32);
470*0Sstevel@tonic-gate 		if (SI_FROMUSER(sip) &&
471*0Sstevel@tonic-gate 		    (zoneid = p->p_zone->zone_id) != GLOBAL_ZONEID &&
472*0Sstevel@tonic-gate 		    zoneid != sip->si_zoneid) {
473*0Sstevel@tonic-gate 			si32.si_pid = p->p_zone->zone_zsched->p_pid;
474*0Sstevel@tonic-gate 			si32.si_uid = 0;
475*0Sstevel@tonic-gate 			si32.si_ctid = -1;
476*0Sstevel@tonic-gate 			si32.si_zoneid = zoneid;
477*0Sstevel@tonic-gate 		}
478*0Sstevel@tonic-gate 		fp -= SA32(sizeof (si32));
479*0Sstevel@tonic-gate 		uzero(fp, sizeof (si32));
480*0Sstevel@tonic-gate 		copyout_noerr(&si32, fp, sizeof (si32));
481*0Sstevel@tonic-gate 		sip_addr = (siginfo32_t *)fp;
482*0Sstevel@tonic-gate 
483*0Sstevel@tonic-gate 		if (sig == SIGPROF &&
484*0Sstevel@tonic-gate 		    curthread->t_rprof != NULL &&
485*0Sstevel@tonic-gate 		    curthread->t_rprof->rp_anystate) {
486*0Sstevel@tonic-gate 			/*
487*0Sstevel@tonic-gate 			 * We stand on our head to deal with
488*0Sstevel@tonic-gate 			 * the real-time profiling signal.
489*0Sstevel@tonic-gate 			 * Fill in the stuff that doesn't fit
490*0Sstevel@tonic-gate 			 * in a normal k_siginfo structure.
491*0Sstevel@tonic-gate 			 */
492*0Sstevel@tonic-gate 			int i = sip->si_nsysarg;
493*0Sstevel@tonic-gate 
494*0Sstevel@tonic-gate 			while (--i >= 0)
495*0Sstevel@tonic-gate 				suword32_noerr(&(sip_addr->si_sysarg[i]),
496*0Sstevel@tonic-gate 				    (uint32_t)lwp->lwp_arg[i]);
497*0Sstevel@tonic-gate 			copyout_noerr(curthread->t_rprof->rp_state,
498*0Sstevel@tonic-gate 			    sip_addr->si_mstate,
499*0Sstevel@tonic-gate 			    sizeof (curthread->t_rprof->rp_state));
500*0Sstevel@tonic-gate 		}
501*0Sstevel@tonic-gate 	} else
502*0Sstevel@tonic-gate 		sip_addr = NULL;
503*0Sstevel@tonic-gate 
504*0Sstevel@tonic-gate 	/* save the current context on the user stack */
505*0Sstevel@tonic-gate 	fp -= SA32(sizeof (*tuc));
506*0Sstevel@tonic-gate 	uc = (ucontext32_t *)fp;
507*0Sstevel@tonic-gate 	tuc = kmem_alloc(sizeof (*tuc), KM_SLEEP);
508*0Sstevel@tonic-gate 	savecontext32(tuc, lwp->lwp_sigoldmask);
509*0Sstevel@tonic-gate 	copyout_noerr(tuc, uc, sizeof (*tuc));
510*0Sstevel@tonic-gate 	kmem_free(tuc, sizeof (*tuc));
511*0Sstevel@tonic-gate 	tuc = NULL;
512*0Sstevel@tonic-gate 
513*0Sstevel@tonic-gate 	lwp->lwp_oldcontext = (uintptr_t)uc;
514*0Sstevel@tonic-gate 
515*0Sstevel@tonic-gate 	if (newstack) {
516*0Sstevel@tonic-gate 		lwp->lwp_sigaltstack.ss_flags |= SS_ONSTACK;
517*0Sstevel@tonic-gate 		if (lwp->lwp_ustack) {
518*0Sstevel@tonic-gate 			stack32_t stk32;
519*0Sstevel@tonic-gate 
520*0Sstevel@tonic-gate 			stk32.ss_sp = (caddr32_t)(uintptr_t)
521*0Sstevel@tonic-gate 			    lwp->lwp_sigaltstack.ss_sp;
522*0Sstevel@tonic-gate 			stk32.ss_size = (size32_t)
523*0Sstevel@tonic-gate 			    lwp->lwp_sigaltstack.ss_size;
524*0Sstevel@tonic-gate 			stk32.ss_flags = (int32_t)
525*0Sstevel@tonic-gate 			    lwp->lwp_sigaltstack.ss_flags;
526*0Sstevel@tonic-gate 			copyout_noerr(&stk32,
527*0Sstevel@tonic-gate 			    (stack32_t *)lwp->lwp_ustack, sizeof (stk32));
528*0Sstevel@tonic-gate 		}
529*0Sstevel@tonic-gate 	}
530*0Sstevel@tonic-gate 
531*0Sstevel@tonic-gate 	/*
532*0Sstevel@tonic-gate 	 * Set up signal handler arguments
533*0Sstevel@tonic-gate 	 */
534*0Sstevel@tonic-gate 	{
535*0Sstevel@tonic-gate 		struct sigframe32 frame32;
536*0Sstevel@tonic-gate 
537*0Sstevel@tonic-gate 		frame32.sip = (caddr32_t)(uintptr_t)sip_addr;
538*0Sstevel@tonic-gate 		frame32.ucp = (caddr32_t)(uintptr_t)uc;
539*0Sstevel@tonic-gate 		frame32.signo = sig;
540*0Sstevel@tonic-gate 		frame32.retaddr = 0xffffffff;	/* never return! */
541*0Sstevel@tonic-gate 		copyout_noerr(&frame32, sp, sizeof (frame32));
542*0Sstevel@tonic-gate 	}
543*0Sstevel@tonic-gate 
544*0Sstevel@tonic-gate 	no_fault();
545*0Sstevel@tonic-gate 	if (watched)
546*0Sstevel@tonic-gate 		watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE);
547*0Sstevel@tonic-gate 
548*0Sstevel@tonic-gate 	rp->r_sp = (greg_t)(uintptr_t)sp;
549*0Sstevel@tonic-gate 	rp->r_pc = (greg_t)(uintptr_t)hdlr;
550*0Sstevel@tonic-gate 	rp->r_ps = PSL_USER | (rp->r_ps & PS_IOPL);
551*0Sstevel@tonic-gate 
552*0Sstevel@tonic-gate 	if ((rp->r_cs & 0xffff) != U32CS_SEL ||
553*0Sstevel@tonic-gate 	    (rp->r_ss & 0xffff) != UDS_SEL) {
554*0Sstevel@tonic-gate 		/*
555*0Sstevel@tonic-gate 		 * Try our best to deliver the signal.
556*0Sstevel@tonic-gate 		 */
557*0Sstevel@tonic-gate 		rp->r_cs = U32CS_SEL;
558*0Sstevel@tonic-gate 		rp->r_ss = UDS_SEL;
559*0Sstevel@tonic-gate 	}
560*0Sstevel@tonic-gate 
561*0Sstevel@tonic-gate 	/*
562*0Sstevel@tonic-gate 	 * Don't set lwp_eosys here.  sendsig() is called via psig() after
563*0Sstevel@tonic-gate 	 * lwp_eosys is handled, so setting it here would affect the next
564*0Sstevel@tonic-gate 	 * system call.
565*0Sstevel@tonic-gate 	 */
566*0Sstevel@tonic-gate 	return (1);
567*0Sstevel@tonic-gate 
568*0Sstevel@tonic-gate badstack:
569*0Sstevel@tonic-gate 	no_fault();
570*0Sstevel@tonic-gate 	if (watched)
571*0Sstevel@tonic-gate 		watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE);
572*0Sstevel@tonic-gate 	if (tuc)
573*0Sstevel@tonic-gate 		kmem_free(tuc, sizeof (*tuc));
574*0Sstevel@tonic-gate #ifdef DEBUG
575*0Sstevel@tonic-gate 	printf("sendsig32: bad signal stack cmd=%s pid=%d, sig=%d\n",
576*0Sstevel@tonic-gate 	    PTOU(p)->u_comm, p->p_pid, sig);
577*0Sstevel@tonic-gate 	printf("on fault, sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
578*0Sstevel@tonic-gate 	    (void *)sp, (void *)hdlr, (uintptr_t)upc);
579*0Sstevel@tonic-gate #endif
580*0Sstevel@tonic-gate 	return (0);
581*0Sstevel@tonic-gate }
582*0Sstevel@tonic-gate 
583*0Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
584*0Sstevel@tonic-gate 
585*0Sstevel@tonic-gate #elif defined(__i386)
586*0Sstevel@tonic-gate 
587*0Sstevel@tonic-gate /*
588*0Sstevel@tonic-gate  * An i386 SVR4/ABI signal frame looks like this on the stack:
589*0Sstevel@tonic-gate  *
590*0Sstevel@tonic-gate  * old %esp:
591*0Sstevel@tonic-gate  *		<a siginfo32_t [optional]>
592*0Sstevel@tonic-gate  *		<a ucontext32_t>
593*0Sstevel@tonic-gate  *		<pointer to that ucontext32_t>
594*0Sstevel@tonic-gate  *		<pointer to that siginfo32_t>
595*0Sstevel@tonic-gate  *		<signo>
596*0Sstevel@tonic-gate  * new %esp:	<return address (deliberately invalid)>
597*0Sstevel@tonic-gate  */
598*0Sstevel@tonic-gate struct sigframe {
599*0Sstevel@tonic-gate 	void		(*retaddr)();
600*0Sstevel@tonic-gate 	uint_t		signo;
601*0Sstevel@tonic-gate 	siginfo_t	*sip;
602*0Sstevel@tonic-gate 	ucontext_t	*ucp;
603*0Sstevel@tonic-gate };
604*0Sstevel@tonic-gate 
605*0Sstevel@tonic-gate int
606*0Sstevel@tonic-gate sendsig(int sig, k_siginfo_t *sip, void (*hdlr)())
607*0Sstevel@tonic-gate {
608*0Sstevel@tonic-gate 	volatile int minstacksz;
609*0Sstevel@tonic-gate 	int newstack;
610*0Sstevel@tonic-gate 	label_t ljb;
611*0Sstevel@tonic-gate 	volatile caddr_t sp;
612*0Sstevel@tonic-gate 	caddr_t fp;
613*0Sstevel@tonic-gate 	struct regs *rp;
614*0Sstevel@tonic-gate 	volatile greg_t upc;
615*0Sstevel@tonic-gate 	volatile proc_t *p = ttoproc(curthread);
616*0Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
617*0Sstevel@tonic-gate 	ucontext_t *volatile tuc = NULL;
618*0Sstevel@tonic-gate 	ucontext_t *uc;
619*0Sstevel@tonic-gate 	siginfo_t *sip_addr;
620*0Sstevel@tonic-gate 	volatile int watched;
621*0Sstevel@tonic-gate 
622*0Sstevel@tonic-gate 	rp = lwptoregs(lwp);
623*0Sstevel@tonic-gate 	upc = rp->r_pc;
624*0Sstevel@tonic-gate 
625*0Sstevel@tonic-gate 	minstacksz = SA(sizeof (struct sigframe)) + SA(sizeof (*uc));
626*0Sstevel@tonic-gate 	if (sip != NULL)
627*0Sstevel@tonic-gate 		minstacksz += SA(sizeof (siginfo_t));
628*0Sstevel@tonic-gate 	ASSERT((minstacksz & (STACK_ALIGN - 1ul)) == 0);
629*0Sstevel@tonic-gate 
630*0Sstevel@tonic-gate 	/*
631*0Sstevel@tonic-gate 	 * Figure out whether we will be handling this signal on
632*0Sstevel@tonic-gate 	 * an alternate stack specified by the user. Then allocate
633*0Sstevel@tonic-gate 	 * and validate the stack requirements for the signal handler
634*0Sstevel@tonic-gate 	 * context. on_fault will catch any faults.
635*0Sstevel@tonic-gate 	 */
636*0Sstevel@tonic-gate 	newstack = sigismember(&u.u_sigonstack, sig) &&
637*0Sstevel@tonic-gate 	    !(lwp->lwp_sigaltstack.ss_flags & (SS_ONSTACK|SS_DISABLE));
638*0Sstevel@tonic-gate 
639*0Sstevel@tonic-gate 	if (newstack) {
640*0Sstevel@tonic-gate 		fp = (caddr_t)(SA((uintptr_t)lwp->lwp_sigaltstack.ss_sp) +
641*0Sstevel@tonic-gate 		    SA(lwp->lwp_sigaltstack.ss_size) - STACK_ALIGN);
642*0Sstevel@tonic-gate 	} else if ((rp->r_ss & 0xffff) != UDS_SEL) {
643*0Sstevel@tonic-gate 		user_desc_t *ldt;
644*0Sstevel@tonic-gate 		/*
645*0Sstevel@tonic-gate 		 * If the stack segment selector is -not- pointing at
646*0Sstevel@tonic-gate 		 * the UDS_SEL descriptor and we have an LDT entry for
647*0Sstevel@tonic-gate 		 * it instead, add the base address to find the effective va.
648*0Sstevel@tonic-gate 		 */
649*0Sstevel@tonic-gate 		if ((ldt = p->p_ldt) != NULL)
650*0Sstevel@tonic-gate 			fp = (caddr_t)rp->r_sp +
651*0Sstevel@tonic-gate 			    USEGD_GETBASE(&ldt[SELTOIDX(rp->r_ss)]);
652*0Sstevel@tonic-gate 		else
653*0Sstevel@tonic-gate 			fp = (caddr_t)rp->r_sp;
654*0Sstevel@tonic-gate 	} else
655*0Sstevel@tonic-gate 		fp = (caddr_t)rp->r_sp;
656*0Sstevel@tonic-gate 	sp = fp - minstacksz;
657*0Sstevel@tonic-gate 
658*0Sstevel@tonic-gate 	/*
659*0Sstevel@tonic-gate 	 * Make sure lwp hasn't trashed its stack.
660*0Sstevel@tonic-gate 	 */
661*0Sstevel@tonic-gate 	if (((uintptr_t)sp & (STACK_ALIGN - 1ul)) != 0 ||
662*0Sstevel@tonic-gate 	    sp >= (caddr_t)USERLIMIT || fp >= (caddr_t)USERLIMIT) {
663*0Sstevel@tonic-gate #ifdef DEBUG
664*0Sstevel@tonic-gate 		printf("sendsig: bad signal stack cmd=%s, pid=%d, sig=%d\n",
665*0Sstevel@tonic-gate 		    PTOU(p)->u_comm, p->p_pid, sig);
666*0Sstevel@tonic-gate 		printf("sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
667*0Sstevel@tonic-gate 		    (void *)sp, (void *)hdlr, (uintptr_t)upc);
668*0Sstevel@tonic-gate 
669*0Sstevel@tonic-gate 		if (((uintptr_t)sp & (STACK_ALIGN - 1ul)) != 0)
670*0Sstevel@tonic-gate 			printf("bad stack alignment\n");
671*0Sstevel@tonic-gate 		else
672*0Sstevel@tonic-gate 			printf("sp above USERLIMIT\n");
673*0Sstevel@tonic-gate #endif
674*0Sstevel@tonic-gate 		return (0);
675*0Sstevel@tonic-gate 	}
676*0Sstevel@tonic-gate 
677*0Sstevel@tonic-gate 	watched = watch_disable_addr((caddr_t)sp, minstacksz, S_WRITE);
678*0Sstevel@tonic-gate 
679*0Sstevel@tonic-gate 	if (on_fault(&ljb))
680*0Sstevel@tonic-gate 		goto badstack;
681*0Sstevel@tonic-gate 
682*0Sstevel@tonic-gate 	if (sip != NULL) {
683*0Sstevel@tonic-gate 		zoneid_t zoneid;
684*0Sstevel@tonic-gate 
685*0Sstevel@tonic-gate 		fp -= SA(sizeof (siginfo_t));
686*0Sstevel@tonic-gate 		uzero(fp, sizeof (siginfo_t));
687*0Sstevel@tonic-gate 		if (SI_FROMUSER(sip) &&
688*0Sstevel@tonic-gate 		    (zoneid = p->p_zone->zone_id) != GLOBAL_ZONEID &&
689*0Sstevel@tonic-gate 		    zoneid != sip->si_zoneid) {
690*0Sstevel@tonic-gate 			k_siginfo_t sani_sip = *sip;
691*0Sstevel@tonic-gate 
692*0Sstevel@tonic-gate 			sani_sip.si_pid = p->p_zone->zone_zsched->p_pid;
693*0Sstevel@tonic-gate 			sani_sip.si_uid = 0;
694*0Sstevel@tonic-gate 			sani_sip.si_ctid = -1;
695*0Sstevel@tonic-gate 			sani_sip.si_zoneid = zoneid;
696*0Sstevel@tonic-gate 			copyout_noerr(&sani_sip, fp, sizeof (sani_sip));
697*0Sstevel@tonic-gate 		} else
698*0Sstevel@tonic-gate 			copyout_noerr(sip, fp, sizeof (*sip));
699*0Sstevel@tonic-gate 		sip_addr = (siginfo_t *)fp;
700*0Sstevel@tonic-gate 
701*0Sstevel@tonic-gate 		if (sig == SIGPROF &&
702*0Sstevel@tonic-gate 		    curthread->t_rprof != NULL &&
703*0Sstevel@tonic-gate 		    curthread->t_rprof->rp_anystate) {
704*0Sstevel@tonic-gate 			/*
705*0Sstevel@tonic-gate 			 * We stand on our head to deal with
706*0Sstevel@tonic-gate 			 * the real time profiling signal.
707*0Sstevel@tonic-gate 			 * Fill in the stuff that doesn't fit
708*0Sstevel@tonic-gate 			 * in a normal k_siginfo structure.
709*0Sstevel@tonic-gate 			 */
710*0Sstevel@tonic-gate 			int i = sip->si_nsysarg;
711*0Sstevel@tonic-gate 
712*0Sstevel@tonic-gate 			while (--i >= 0)
713*0Sstevel@tonic-gate 				suword32_noerr(&(sip_addr->si_sysarg[i]),
714*0Sstevel@tonic-gate 				    (uint32_t)lwp->lwp_arg[i]);
715*0Sstevel@tonic-gate 			copyout_noerr(curthread->t_rprof->rp_state,
716*0Sstevel@tonic-gate 			    sip_addr->si_mstate,
717*0Sstevel@tonic-gate 			    sizeof (curthread->t_rprof->rp_state));
718*0Sstevel@tonic-gate 		}
719*0Sstevel@tonic-gate 	} else
720*0Sstevel@tonic-gate 		sip_addr = NULL;
721*0Sstevel@tonic-gate 
722*0Sstevel@tonic-gate 	/* save the current context on the user stack */
723*0Sstevel@tonic-gate 	fp -= SA(sizeof (*tuc));
724*0Sstevel@tonic-gate 	uc = (ucontext_t *)fp;
725*0Sstevel@tonic-gate 	tuc = kmem_alloc(sizeof (*tuc), KM_SLEEP);
726*0Sstevel@tonic-gate 	savecontext(tuc, lwp->lwp_sigoldmask);
727*0Sstevel@tonic-gate 	copyout_noerr(tuc, uc, sizeof (*tuc));
728*0Sstevel@tonic-gate 	kmem_free(tuc, sizeof (*tuc));
729*0Sstevel@tonic-gate 	tuc = NULL;
730*0Sstevel@tonic-gate 
731*0Sstevel@tonic-gate 	lwp->lwp_oldcontext = (uintptr_t)uc;
732*0Sstevel@tonic-gate 
733*0Sstevel@tonic-gate 	if (newstack) {
734*0Sstevel@tonic-gate 		lwp->lwp_sigaltstack.ss_flags |= SS_ONSTACK;
735*0Sstevel@tonic-gate 		if (lwp->lwp_ustack)
736*0Sstevel@tonic-gate 			copyout_noerr(&lwp->lwp_sigaltstack,
737*0Sstevel@tonic-gate 			    (stack_t *)lwp->lwp_ustack, sizeof (stack_t));
738*0Sstevel@tonic-gate 	}
739*0Sstevel@tonic-gate 
740*0Sstevel@tonic-gate 	/*
741*0Sstevel@tonic-gate 	 * Set up signal handler arguments
742*0Sstevel@tonic-gate 	 */
743*0Sstevel@tonic-gate 	{
744*0Sstevel@tonic-gate 		struct sigframe frame;
745*0Sstevel@tonic-gate 
746*0Sstevel@tonic-gate 		frame.sip = sip_addr;
747*0Sstevel@tonic-gate 		frame.ucp = uc;
748*0Sstevel@tonic-gate 		frame.signo = sig;
749*0Sstevel@tonic-gate 		frame.retaddr = (void (*)())0xffffffff;	/* never return! */
750*0Sstevel@tonic-gate 		copyout_noerr(&frame, sp, sizeof (frame));
751*0Sstevel@tonic-gate 	}
752*0Sstevel@tonic-gate 
753*0Sstevel@tonic-gate 	no_fault();
754*0Sstevel@tonic-gate 	if (watched)
755*0Sstevel@tonic-gate 		watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE);
756*0Sstevel@tonic-gate 
757*0Sstevel@tonic-gate 	rp->r_sp = (greg_t)sp;
758*0Sstevel@tonic-gate 	rp->r_pc = (greg_t)hdlr;
759*0Sstevel@tonic-gate 	rp->r_ps = PSL_USER | (rp->r_ps & PS_IOPL);
760*0Sstevel@tonic-gate 
761*0Sstevel@tonic-gate 	if ((rp->r_cs & 0xffff) != UCS_SEL ||
762*0Sstevel@tonic-gate 	    (rp->r_ss & 0xffff) != UDS_SEL) {
763*0Sstevel@tonic-gate 		rp->r_cs = UCS_SEL;
764*0Sstevel@tonic-gate 		rp->r_ss = UDS_SEL;
765*0Sstevel@tonic-gate 	}
766*0Sstevel@tonic-gate 
767*0Sstevel@tonic-gate 	/*
768*0Sstevel@tonic-gate 	 * Don't set lwp_eosys here.  sendsig() is called via psig() after
769*0Sstevel@tonic-gate 	 * lwp_eosys is handled, so setting it here would affect the next
770*0Sstevel@tonic-gate 	 * system call.
771*0Sstevel@tonic-gate 	 */
772*0Sstevel@tonic-gate 	return (1);
773*0Sstevel@tonic-gate 
774*0Sstevel@tonic-gate badstack:
775*0Sstevel@tonic-gate 	no_fault();
776*0Sstevel@tonic-gate 	if (watched)
777*0Sstevel@tonic-gate 		watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE);
778*0Sstevel@tonic-gate 	if (tuc)
779*0Sstevel@tonic-gate 		kmem_free(tuc, sizeof (*tuc));
780*0Sstevel@tonic-gate #ifdef DEBUG
781*0Sstevel@tonic-gate 	printf("sendsig: bad signal stack cmd=%s, pid=%d, sig=%d\n",
782*0Sstevel@tonic-gate 	    PTOU(p)->u_comm, p->p_pid, sig);
783*0Sstevel@tonic-gate 	printf("on fault, sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
784*0Sstevel@tonic-gate 	    (void *)sp, (void *)hdlr, (uintptr_t)upc);
785*0Sstevel@tonic-gate #endif
786*0Sstevel@tonic-gate 	return (0);
787*0Sstevel@tonic-gate }
788*0Sstevel@tonic-gate 
789*0Sstevel@tonic-gate #endif	/* __i386 */
790