xref: /titanic_51/usr/src/uts/intel/ia32/os/sendsig.c (revision bbaa8b60dd95d714741fc474adad3cf710ef4efd)
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 	savecontext(tuc, &lwp->lwp_sigoldmask);
292 	copyout_noerr(tuc, uc, sizeof (*tuc));
293 	kmem_free(tuc, sizeof (*tuc));
294 	tuc = NULL;
295 
296 	lwp->lwp_oldcontext = (uintptr_t)uc;
297 
298 	if (newstack) {
299 		lwp->lwp_sigaltstack.ss_flags |= SS_ONSTACK;
300 		if (lwp->lwp_ustack)
301 			copyout_noerr(&lwp->lwp_sigaltstack,
302 			    (stack_t *)lwp->lwp_ustack, sizeof (stack_t));
303 	}
304 
305 	/*
306 	 * Set up signal handler return and stack linkage
307 	 */
308 	{
309 		struct sigframe frame;
310 
311 		/*
312 		 * ensure we never return "normally"
313 		 */
314 		frame.retaddr = (caddr_t)(uintptr_t)-1L;
315 		frame.signo = sig;
316 		frame.sip = sip_addr;
317 		copyout_noerr(&frame, sp, sizeof (frame));
318 	}
319 
320 	no_fault();
321 	if (watched)
322 		watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE);
323 
324 	/*
325 	 * Set up user registers for execution of signal handler.
326 	 */
327 	rp->r_sp = (greg_t)sp;
328 	rp->r_pc = (greg_t)hdlr;
329 	rp->r_ps = PSL_USER | (rp->r_ps & PS_IOPL);
330 
331 	rp->r_rdi = sig;
332 	rp->r_rsi = (uintptr_t)sip_addr;
333 	rp->r_rdx = (uintptr_t)uc;
334 
335 	if ((rp->r_cs & 0xffff) != UCS_SEL ||
336 	    (rp->r_ss & 0xffff) != UDS_SEL) {
337 		/*
338 		 * Try our best to deliver the signal.
339 		 */
340 		rp->r_cs = UCS_SEL;
341 		rp->r_ss = UDS_SEL;
342 	}
343 
344 	/*
345 	 * Don't set lwp_eosys here.  sendsig() is called via psig() after
346 	 * lwp_eosys is handled, so setting it here would affect the next
347 	 * system call.
348 	 */
349 	return (1);
350 
351 badstack:
352 	no_fault();
353 	if (watched)
354 		watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE);
355 	if (tuc)
356 		kmem_free(tuc, sizeof (*tuc));
357 #ifdef DEBUG
358 	printf("sendsig: bad signal stack cmd=%s, pid=%d, sig=%d\n",
359 	    PTOU(p)->u_comm, p->p_pid, sig);
360 	printf("on fault, sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
361 	    (void *)sp, (void *)hdlr, (uintptr_t)upc);
362 #endif
363 	return (0);
364 }
365 
366 #ifdef _SYSCALL32_IMPL
367 
368 /*
369  * An i386 SVR4/ABI signal frame looks like this on the stack:
370  *
371  * old %esp:
372  *		<a siginfo32_t [optional]>
373  *		<a ucontext32_t>
374  *		<pointer to that ucontext32_t>
375  *		<pointer to that siginfo32_t>
376  *		<signo>
377  * new %esp:	<return address (deliberately invalid)>
378  */
379 struct sigframe32 {
380 	caddr32_t	retaddr;
381 	uint32_t	signo;
382 	caddr32_t	sip;
383 	caddr32_t	ucp;
384 };
385 
386 int
387 sendsig32(int sig, k_siginfo_t *sip, void (*hdlr)())
388 {
389 	volatile int minstacksz;
390 	int newstack;
391 	label_t ljb;
392 	volatile caddr_t sp;
393 	caddr_t fp;
394 	volatile struct regs *rp;
395 	volatile greg_t upc;
396 	volatile proc_t *p = ttoproc(curthread);
397 	klwp_t *lwp = ttolwp(curthread);
398 	ucontext32_t *volatile tuc = NULL;
399 	ucontext32_t *uc;
400 	siginfo32_t *sip_addr;
401 	volatile int watched;
402 
403 	rp = lwptoregs(lwp);
404 	upc = rp->r_pc;
405 
406 	minstacksz = SA32(sizeof (struct sigframe32)) + SA32(sizeof (*uc));
407 	if (sip != NULL)
408 		minstacksz += SA32(sizeof (siginfo32_t));
409 	ASSERT((minstacksz & (STACK_ALIGN32 - 1)) == 0);
410 
411 	/*
412 	 * Figure out whether we will be handling this signal on
413 	 * an alternate stack specified by the user.  Then allocate
414 	 * and validate the stack requirements for the signal handler
415 	 * context.  on_fault will catch any faults.
416 	 */
417 	newstack = sigismember(&PTOU(curproc)->u_sigonstack, sig) &&
418 	    !(lwp->lwp_sigaltstack.ss_flags & (SS_ONSTACK|SS_DISABLE));
419 
420 	if (newstack) {
421 		fp = (caddr_t)(SA32((uintptr_t)lwp->lwp_sigaltstack.ss_sp) +
422 		    SA32(lwp->lwp_sigaltstack.ss_size) - STACK_ALIGN32);
423 	} else if ((rp->r_ss & 0xffff) != UDS_SEL) {
424 		user_desc_t *ldt;
425 		/*
426 		 * If the stack segment selector is -not- pointing at
427 		 * the UDS_SEL descriptor and we have an LDT entry for
428 		 * it instead, add the base address to find the effective va.
429 		 */
430 		if ((ldt = p->p_ldt) != NULL)
431 			fp = (caddr_t)rp->r_sp +
432 			    USEGD_GETBASE(&ldt[SELTOIDX(rp->r_ss)]);
433 		else
434 			fp = (caddr_t)rp->r_sp;
435 	} else
436 		fp = (caddr_t)rp->r_sp;
437 
438 	/*
439 	 * Force proper stack pointer alignment, even in the face of a
440 	 * misaligned stack pointer from user-level before the signal.
441 	 * Don't use the SA32() macro because that rounds up, not down.
442 	 */
443 	fp = (caddr_t)((uintptr_t)fp & ~(STACK_ALIGN32 - 1));
444 	sp = fp - minstacksz;
445 
446 	/*
447 	 * Make sure lwp hasn't trashed its stack
448 	 */
449 	if (sp >= (caddr_t)(uintptr_t)USERLIMIT32 ||
450 	    fp >= (caddr_t)(uintptr_t)USERLIMIT32) {
451 #ifdef DEBUG
452 		printf("sendsig32: bad signal stack cmd=%s, pid=%d, sig=%d\n",
453 		    PTOU(p)->u_comm, p->p_pid, sig);
454 		printf("sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
455 		    (void *)sp, (void *)hdlr, (uintptr_t)upc);
456 		printf("sp above USERLIMIT\n");
457 #endif
458 		return (0);
459 	}
460 
461 	watched = watch_disable_addr((caddr_t)sp, minstacksz, S_WRITE);
462 
463 	if (on_fault(&ljb))
464 		goto badstack;
465 
466 	if (sip != NULL) {
467 		siginfo32_t si32;
468 		zoneid_t zoneid;
469 
470 		siginfo_kto32(sip, &si32);
471 		if (SI_FROMUSER(sip) &&
472 		    (zoneid = p->p_zone->zone_id) != GLOBAL_ZONEID &&
473 		    zoneid != sip->si_zoneid) {
474 			si32.si_pid = p->p_zone->zone_zsched->p_pid;
475 			si32.si_uid = 0;
476 			si32.si_ctid = -1;
477 			si32.si_zoneid = zoneid;
478 		}
479 		fp -= SA32(sizeof (si32));
480 		uzero(fp, sizeof (si32));
481 		copyout_noerr(&si32, fp, sizeof (si32));
482 		sip_addr = (siginfo32_t *)fp;
483 
484 		if (sig == SIGPROF &&
485 		    curthread->t_rprof != NULL &&
486 		    curthread->t_rprof->rp_anystate) {
487 			/*
488 			 * We stand on our head to deal with
489 			 * the real-time profiling signal.
490 			 * Fill in the stuff that doesn't fit
491 			 * in a normal k_siginfo structure.
492 			 */
493 			int i = sip->si_nsysarg;
494 
495 			while (--i >= 0)
496 				suword32_noerr(&(sip_addr->si_sysarg[i]),
497 				    (uint32_t)lwp->lwp_arg[i]);
498 			copyout_noerr(curthread->t_rprof->rp_state,
499 			    sip_addr->si_mstate,
500 			    sizeof (curthread->t_rprof->rp_state));
501 		}
502 	} else
503 		sip_addr = NULL;
504 
505 	/* save the current context on the user stack */
506 	fp -= SA32(sizeof (*tuc));
507 	uc = (ucontext32_t *)fp;
508 	tuc = kmem_alloc(sizeof (*tuc), KM_SLEEP);
509 	savecontext32(tuc, &lwp->lwp_sigoldmask);
510 	copyout_noerr(tuc, uc, sizeof (*tuc));
511 	kmem_free(tuc, sizeof (*tuc));
512 	tuc = NULL;
513 
514 	lwp->lwp_oldcontext = (uintptr_t)uc;
515 
516 	if (newstack) {
517 		lwp->lwp_sigaltstack.ss_flags |= SS_ONSTACK;
518 		if (lwp->lwp_ustack) {
519 			stack32_t stk32;
520 
521 			stk32.ss_sp = (caddr32_t)(uintptr_t)
522 			    lwp->lwp_sigaltstack.ss_sp;
523 			stk32.ss_size = (size32_t)
524 			    lwp->lwp_sigaltstack.ss_size;
525 			stk32.ss_flags = (int32_t)
526 			    lwp->lwp_sigaltstack.ss_flags;
527 			copyout_noerr(&stk32,
528 			    (stack32_t *)lwp->lwp_ustack, sizeof (stk32));
529 		}
530 	}
531 
532 	/*
533 	 * Set up signal handler arguments
534 	 */
535 	{
536 		struct sigframe32 frame32;
537 
538 		frame32.sip = (caddr32_t)(uintptr_t)sip_addr;
539 		frame32.ucp = (caddr32_t)(uintptr_t)uc;
540 		frame32.signo = sig;
541 		frame32.retaddr = 0xffffffff;	/* never return! */
542 		copyout_noerr(&frame32, sp, sizeof (frame32));
543 	}
544 
545 	no_fault();
546 	if (watched)
547 		watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE);
548 
549 	rp->r_sp = (greg_t)(uintptr_t)sp;
550 	rp->r_pc = (greg_t)(uintptr_t)hdlr;
551 	rp->r_ps = PSL_USER | (rp->r_ps & PS_IOPL);
552 
553 	if ((rp->r_cs & 0xffff) != U32CS_SEL ||
554 	    (rp->r_ss & 0xffff) != UDS_SEL) {
555 		/*
556 		 * Try our best to deliver the signal.
557 		 */
558 		rp->r_cs = U32CS_SEL;
559 		rp->r_ss = UDS_SEL;
560 	}
561 
562 	/*
563 	 * Don't set lwp_eosys here.  sendsig() is called via psig() after
564 	 * lwp_eosys is handled, so setting it here would affect the next
565 	 * system call.
566 	 */
567 	return (1);
568 
569 badstack:
570 	no_fault();
571 	if (watched)
572 		watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE);
573 	if (tuc)
574 		kmem_free(tuc, sizeof (*tuc));
575 #ifdef DEBUG
576 	printf("sendsig32: bad signal stack cmd=%s pid=%d, sig=%d\n",
577 	    PTOU(p)->u_comm, p->p_pid, sig);
578 	printf("on fault, sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
579 	    (void *)sp, (void *)hdlr, (uintptr_t)upc);
580 #endif
581 	return (0);
582 }
583 
584 #endif	/* _SYSCALL32_IMPL */
585 
586 #elif defined(__i386)
587 
588 /*
589  * An i386 SVR4/ABI signal frame looks like this on the stack:
590  *
591  * old %esp:
592  *		<a siginfo32_t [optional]>
593  *		<a ucontext32_t>
594  *		<pointer to that ucontext32_t>
595  *		<pointer to that siginfo32_t>
596  *		<signo>
597  * new %esp:	<return address (deliberately invalid)>
598  */
599 struct sigframe {
600 	void		(*retaddr)();
601 	uint_t		signo;
602 	siginfo_t	*sip;
603 	ucontext_t	*ucp;
604 };
605 
606 int
607 sendsig(int sig, k_siginfo_t *sip, void (*hdlr)())
608 {
609 	volatile int minstacksz;
610 	int newstack;
611 	label_t ljb;
612 	volatile caddr_t sp;
613 	caddr_t fp;
614 	struct regs *rp;
615 	volatile greg_t upc;
616 	volatile proc_t *p = ttoproc(curthread);
617 	klwp_t *lwp = ttolwp(curthread);
618 	ucontext_t *volatile tuc = NULL;
619 	ucontext_t *uc;
620 	siginfo_t *sip_addr;
621 	volatile int watched;
622 
623 	rp = lwptoregs(lwp);
624 	upc = rp->r_pc;
625 
626 	minstacksz = SA(sizeof (struct sigframe)) + SA(sizeof (*uc));
627 	if (sip != NULL)
628 		minstacksz += SA(sizeof (siginfo_t));
629 	ASSERT((minstacksz & (STACK_ALIGN - 1ul)) == 0);
630 
631 	/*
632 	 * Figure out whether we will be handling this signal on
633 	 * an alternate stack specified by the user. Then allocate
634 	 * and validate the stack requirements for the signal handler
635 	 * context. on_fault will catch any faults.
636 	 */
637 	newstack = sigismember(&PTOU(curproc)->u_sigonstack, sig) &&
638 	    !(lwp->lwp_sigaltstack.ss_flags & (SS_ONSTACK|SS_DISABLE));
639 
640 	if (newstack) {
641 		fp = (caddr_t)(SA((uintptr_t)lwp->lwp_sigaltstack.ss_sp) +
642 		    SA(lwp->lwp_sigaltstack.ss_size) - STACK_ALIGN);
643 	} else if ((rp->r_ss & 0xffff) != UDS_SEL) {
644 		user_desc_t *ldt;
645 		/*
646 		 * If the stack segment selector is -not- pointing at
647 		 * the UDS_SEL descriptor and we have an LDT entry for
648 		 * it instead, add the base address to find the effective va.
649 		 */
650 		if ((ldt = p->p_ldt) != NULL)
651 			fp = (caddr_t)rp->r_sp +
652 			    USEGD_GETBASE(&ldt[SELTOIDX(rp->r_ss)]);
653 		else
654 			fp = (caddr_t)rp->r_sp;
655 	} else
656 		fp = (caddr_t)rp->r_sp;
657 
658 	/*
659 	 * Force proper stack pointer alignment, even in the face of a
660 	 * misaligned stack pointer from user-level before the signal.
661 	 * Don't use the SA() macro because that rounds up, not down.
662 	 */
663 	fp = (caddr_t)((uintptr_t)fp & ~(STACK_ALIGN - 1ul));
664 	sp = fp - minstacksz;
665 
666 	/*
667 	 * Make sure lwp hasn't trashed its stack.
668 	 */
669 	if (sp >= (caddr_t)USERLIMIT || fp >= (caddr_t)USERLIMIT) {
670 #ifdef DEBUG
671 		printf("sendsig: bad signal stack cmd=%s, pid=%d, sig=%d\n",
672 		    PTOU(p)->u_comm, p->p_pid, sig);
673 		printf("sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
674 		    (void *)sp, (void *)hdlr, (uintptr_t)upc);
675 		printf("sp above USERLIMIT\n");
676 #endif
677 		return (0);
678 	}
679 
680 	watched = watch_disable_addr((caddr_t)sp, minstacksz, S_WRITE);
681 
682 	if (on_fault(&ljb))
683 		goto badstack;
684 
685 	if (sip != NULL) {
686 		zoneid_t zoneid;
687 
688 		fp -= SA(sizeof (siginfo_t));
689 		uzero(fp, sizeof (siginfo_t));
690 		if (SI_FROMUSER(sip) &&
691 		    (zoneid = p->p_zone->zone_id) != GLOBAL_ZONEID &&
692 		    zoneid != sip->si_zoneid) {
693 			k_siginfo_t sani_sip = *sip;
694 
695 			sani_sip.si_pid = p->p_zone->zone_zsched->p_pid;
696 			sani_sip.si_uid = 0;
697 			sani_sip.si_ctid = -1;
698 			sani_sip.si_zoneid = zoneid;
699 			copyout_noerr(&sani_sip, fp, sizeof (sani_sip));
700 		} else
701 			copyout_noerr(sip, fp, sizeof (*sip));
702 		sip_addr = (siginfo_t *)fp;
703 
704 		if (sig == SIGPROF &&
705 		    curthread->t_rprof != NULL &&
706 		    curthread->t_rprof->rp_anystate) {
707 			/*
708 			 * We stand on our head to deal with
709 			 * the real time profiling signal.
710 			 * Fill in the stuff that doesn't fit
711 			 * in a normal k_siginfo structure.
712 			 */
713 			int i = sip->si_nsysarg;
714 
715 			while (--i >= 0)
716 				suword32_noerr(&(sip_addr->si_sysarg[i]),
717 				    (uint32_t)lwp->lwp_arg[i]);
718 			copyout_noerr(curthread->t_rprof->rp_state,
719 			    sip_addr->si_mstate,
720 			    sizeof (curthread->t_rprof->rp_state));
721 		}
722 	} else
723 		sip_addr = NULL;
724 
725 	/* save the current context on the user stack */
726 	fp -= SA(sizeof (*tuc));
727 	uc = (ucontext_t *)fp;
728 	tuc = kmem_alloc(sizeof (*tuc), KM_SLEEP);
729 	savecontext(tuc, &lwp->lwp_sigoldmask);
730 	copyout_noerr(tuc, uc, sizeof (*tuc));
731 	kmem_free(tuc, sizeof (*tuc));
732 	tuc = NULL;
733 
734 	lwp->lwp_oldcontext = (uintptr_t)uc;
735 
736 	if (newstack) {
737 		lwp->lwp_sigaltstack.ss_flags |= SS_ONSTACK;
738 		if (lwp->lwp_ustack)
739 			copyout_noerr(&lwp->lwp_sigaltstack,
740 			    (stack_t *)lwp->lwp_ustack, sizeof (stack_t));
741 	}
742 
743 	/*
744 	 * Set up signal handler arguments
745 	 */
746 	{
747 		struct sigframe frame;
748 
749 		frame.sip = sip_addr;
750 		frame.ucp = uc;
751 		frame.signo = sig;
752 		frame.retaddr = (void (*)())0xffffffff;	/* never return! */
753 		copyout_noerr(&frame, sp, sizeof (frame));
754 	}
755 
756 	no_fault();
757 	if (watched)
758 		watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE);
759 
760 	rp->r_sp = (greg_t)sp;
761 	rp->r_pc = (greg_t)hdlr;
762 	rp->r_ps = PSL_USER | (rp->r_ps & PS_IOPL);
763 
764 	if ((rp->r_cs & 0xffff) != UCS_SEL ||
765 	    (rp->r_ss & 0xffff) != UDS_SEL) {
766 		rp->r_cs = UCS_SEL;
767 		rp->r_ss = UDS_SEL;
768 	}
769 
770 	/*
771 	 * Don't set lwp_eosys here.  sendsig() is called via psig() after
772 	 * lwp_eosys is handled, so setting it here would affect the next
773 	 * system call.
774 	 */
775 	return (1);
776 
777 badstack:
778 	no_fault();
779 	if (watched)
780 		watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE);
781 	if (tuc)
782 		kmem_free(tuc, sizeof (*tuc));
783 #ifdef DEBUG
784 	printf("sendsig: bad signal stack cmd=%s, pid=%d, sig=%d\n",
785 	    PTOU(p)->u_comm, p->p_pid, sig);
786 	printf("on fault, sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
787 	    (void *)sp, (void *)hdlr, (uintptr_t)upc);
788 #endif
789 	return (0);
790 }
791 
792 #endif	/* __i386 */
793