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