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