xref: /titanic_44/usr/src/uts/intel/ia32/os/sendsig.c (revision ea8dc4b6d2251b437950c0056bc626b311c73c27)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 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 	 * Most of the time during normal execution, the stack pointer
203 	 * is aligned on a STACK_ALIGN (i.e. 16 byte) boundary.  However,
204 	 * (for example) just after a call instruction (which pushes
205 	 * the return address), the callers stack misaligns until the
206 	 * 'push %rbp' happens in the callee prolog.  So while we should
207 	 * expect the stack pointer to be always at least STACK_ENTRY_ALIGN
208 	 * aligned, we should -not- expect it to always be STACK_ALIGN aligned.
209 	 * We now adjust to ensure that the new sp is aligned to
210 	 * STACK_ENTRY_ALIGN but not to STACK_ALIGN.
211 	 */
212 	sp = fp - minstacksz;
213 	if (((uintptr_t)sp & (STACK_ALIGN - 1ul)) == 0) {
214 		sp -= STACK_ENTRY_ALIGN;
215 		minstacksz = fp - sp;
216 	}
217 
218 	/*
219 	 * Now, make sure the resulting signal frame address is sane
220 	 */
221 	if (((uintptr_t)(sp - STACK_ENTRY_ALIGN) & (STACK_ALIGN - 1ul)) != 0 ||
222 	    sp >= (caddr_t)USERLIMIT || fp >= (caddr_t)USERLIMIT) {
223 #ifdef DEBUG
224 		printf("sendsig: bad signal stack cmd=%s, pid=%d, sig=%d\n",
225 		    PTOU(p)->u_comm, p->p_pid, sig);
226 		printf("sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
227 		    (void *)sp, (void *)hdlr, (uintptr_t)upc);
228 
229 		if (((uintptr_t)(sp - STACK_ENTRY_ALIGN) &
230 		    (STACK_ALIGN - 1ul)) != 0)
231 			printf("bad stack alignment\n");
232 		else
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 	sp = fp - minstacksz;
439 
440 	/*
441 	 * Make sure lwp hasn't trashed its stack
442 	 */
443 	if (((uintptr_t)sp & (STACK_ALIGN32 - 1)) != 0 ||
444 	    sp >= (caddr_t)(uintptr_t)USERLIMIT32 ||
445 	    fp >= (caddr_t)(uintptr_t)USERLIMIT32) {
446 #ifdef DEBUG
447 		printf("sendsig32: bad signal stack cmd=%s, pid=%d, sig=%d\n",
448 		    PTOU(p)->u_comm, p->p_pid, sig);
449 		printf("sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
450 		    (void *)sp, (void *)hdlr, (uintptr_t)upc);
451 
452 		if (((uintptr_t)sp & (STACK_ALIGN32 - 1)) != 0)
453 			printf("bad stack alignment\n");
454 		else
455 			printf("sp above USERLIMIT\n");
456 #endif
457 		return (0);
458 	}
459 
460 	watched = watch_disable_addr((caddr_t)sp, minstacksz, S_WRITE);
461 
462 	if (on_fault(&ljb))
463 		goto badstack;
464 
465 	if (sip != NULL) {
466 		siginfo32_t si32;
467 		zoneid_t zoneid;
468 
469 		siginfo_kto32(sip, &si32);
470 		if (SI_FROMUSER(sip) &&
471 		    (zoneid = p->p_zone->zone_id) != GLOBAL_ZONEID &&
472 		    zoneid != sip->si_zoneid) {
473 			si32.si_pid = p->p_zone->zone_zsched->p_pid;
474 			si32.si_uid = 0;
475 			si32.si_ctid = -1;
476 			si32.si_zoneid = zoneid;
477 		}
478 		fp -= SA32(sizeof (si32));
479 		uzero(fp, sizeof (si32));
480 		copyout_noerr(&si32, fp, sizeof (si32));
481 		sip_addr = (siginfo32_t *)fp;
482 
483 		if (sig == SIGPROF &&
484 		    curthread->t_rprof != NULL &&
485 		    curthread->t_rprof->rp_anystate) {
486 			/*
487 			 * We stand on our head to deal with
488 			 * the real-time profiling signal.
489 			 * Fill in the stuff that doesn't fit
490 			 * in a normal k_siginfo structure.
491 			 */
492 			int i = sip->si_nsysarg;
493 
494 			while (--i >= 0)
495 				suword32_noerr(&(sip_addr->si_sysarg[i]),
496 				    (uint32_t)lwp->lwp_arg[i]);
497 			copyout_noerr(curthread->t_rprof->rp_state,
498 			    sip_addr->si_mstate,
499 			    sizeof (curthread->t_rprof->rp_state));
500 		}
501 	} else
502 		sip_addr = NULL;
503 
504 	/* save the current context on the user stack */
505 	fp -= SA32(sizeof (*tuc));
506 	uc = (ucontext32_t *)fp;
507 	tuc = kmem_alloc(sizeof (*tuc), KM_SLEEP);
508 	savecontext32(tuc, lwp->lwp_sigoldmask);
509 	copyout_noerr(tuc, uc, sizeof (*tuc));
510 	kmem_free(tuc, sizeof (*tuc));
511 	tuc = NULL;
512 
513 	lwp->lwp_oldcontext = (uintptr_t)uc;
514 
515 	if (newstack) {
516 		lwp->lwp_sigaltstack.ss_flags |= SS_ONSTACK;
517 		if (lwp->lwp_ustack) {
518 			stack32_t stk32;
519 
520 			stk32.ss_sp = (caddr32_t)(uintptr_t)
521 			    lwp->lwp_sigaltstack.ss_sp;
522 			stk32.ss_size = (size32_t)
523 			    lwp->lwp_sigaltstack.ss_size;
524 			stk32.ss_flags = (int32_t)
525 			    lwp->lwp_sigaltstack.ss_flags;
526 			copyout_noerr(&stk32,
527 			    (stack32_t *)lwp->lwp_ustack, sizeof (stk32));
528 		}
529 	}
530 
531 	/*
532 	 * Set up signal handler arguments
533 	 */
534 	{
535 		struct sigframe32 frame32;
536 
537 		frame32.sip = (caddr32_t)(uintptr_t)sip_addr;
538 		frame32.ucp = (caddr32_t)(uintptr_t)uc;
539 		frame32.signo = sig;
540 		frame32.retaddr = 0xffffffff;	/* never return! */
541 		copyout_noerr(&frame32, sp, sizeof (frame32));
542 	}
543 
544 	no_fault();
545 	if (watched)
546 		watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE);
547 
548 	rp->r_sp = (greg_t)(uintptr_t)sp;
549 	rp->r_pc = (greg_t)(uintptr_t)hdlr;
550 	rp->r_ps = PSL_USER | (rp->r_ps & PS_IOPL);
551 
552 	if ((rp->r_cs & 0xffff) != U32CS_SEL ||
553 	    (rp->r_ss & 0xffff) != UDS_SEL) {
554 		/*
555 		 * Try our best to deliver the signal.
556 		 */
557 		rp->r_cs = U32CS_SEL;
558 		rp->r_ss = UDS_SEL;
559 	}
560 
561 	/*
562 	 * Don't set lwp_eosys here.  sendsig() is called via psig() after
563 	 * lwp_eosys is handled, so setting it here would affect the next
564 	 * system call.
565 	 */
566 	return (1);
567 
568 badstack:
569 	no_fault();
570 	if (watched)
571 		watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE);
572 	if (tuc)
573 		kmem_free(tuc, sizeof (*tuc));
574 #ifdef DEBUG
575 	printf("sendsig32: bad signal stack cmd=%s pid=%d, sig=%d\n",
576 	    PTOU(p)->u_comm, p->p_pid, sig);
577 	printf("on fault, sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
578 	    (void *)sp, (void *)hdlr, (uintptr_t)upc);
579 #endif
580 	return (0);
581 }
582 
583 #endif	/* _SYSCALL32_IMPL */
584 
585 #elif defined(__i386)
586 
587 /*
588  * An i386 SVR4/ABI signal frame looks like this on the stack:
589  *
590  * old %esp:
591  *		<a siginfo32_t [optional]>
592  *		<a ucontext32_t>
593  *		<pointer to that ucontext32_t>
594  *		<pointer to that siginfo32_t>
595  *		<signo>
596  * new %esp:	<return address (deliberately invalid)>
597  */
598 struct sigframe {
599 	void		(*retaddr)();
600 	uint_t		signo;
601 	siginfo_t	*sip;
602 	ucontext_t	*ucp;
603 };
604 
605 int
606 sendsig(int sig, k_siginfo_t *sip, void (*hdlr)())
607 {
608 	volatile int minstacksz;
609 	int newstack;
610 	label_t ljb;
611 	volatile caddr_t sp;
612 	caddr_t fp;
613 	struct regs *rp;
614 	volatile greg_t upc;
615 	volatile proc_t *p = ttoproc(curthread);
616 	klwp_t *lwp = ttolwp(curthread);
617 	ucontext_t *volatile tuc = NULL;
618 	ucontext_t *uc;
619 	siginfo_t *sip_addr;
620 	volatile int watched;
621 
622 	rp = lwptoregs(lwp);
623 	upc = rp->r_pc;
624 
625 	minstacksz = SA(sizeof (struct sigframe)) + SA(sizeof (*uc));
626 	if (sip != NULL)
627 		minstacksz += SA(sizeof (siginfo_t));
628 	ASSERT((minstacksz & (STACK_ALIGN - 1ul)) == 0);
629 
630 	/*
631 	 * Figure out whether we will be handling this signal on
632 	 * an alternate stack specified by the user. Then allocate
633 	 * and validate the stack requirements for the signal handler
634 	 * context. on_fault will catch any faults.
635 	 */
636 	newstack = sigismember(&u.u_sigonstack, sig) &&
637 	    !(lwp->lwp_sigaltstack.ss_flags & (SS_ONSTACK|SS_DISABLE));
638 
639 	if (newstack) {
640 		fp = (caddr_t)(SA((uintptr_t)lwp->lwp_sigaltstack.ss_sp) +
641 		    SA(lwp->lwp_sigaltstack.ss_size) - STACK_ALIGN);
642 	} else if ((rp->r_ss & 0xffff) != UDS_SEL) {
643 		user_desc_t *ldt;
644 		/*
645 		 * If the stack segment selector is -not- pointing at
646 		 * the UDS_SEL descriptor and we have an LDT entry for
647 		 * it instead, add the base address to find the effective va.
648 		 */
649 		if ((ldt = p->p_ldt) != NULL)
650 			fp = (caddr_t)rp->r_sp +
651 			    USEGD_GETBASE(&ldt[SELTOIDX(rp->r_ss)]);
652 		else
653 			fp = (caddr_t)rp->r_sp;
654 	} else
655 		fp = (caddr_t)rp->r_sp;
656 	sp = fp - minstacksz;
657 
658 	/*
659 	 * Make sure lwp hasn't trashed its stack.
660 	 */
661 	if (((uintptr_t)sp & (STACK_ALIGN - 1ul)) != 0 ||
662 	    sp >= (caddr_t)USERLIMIT || fp >= (caddr_t)USERLIMIT) {
663 #ifdef DEBUG
664 		printf("sendsig: bad signal stack cmd=%s, pid=%d, sig=%d\n",
665 		    PTOU(p)->u_comm, p->p_pid, sig);
666 		printf("sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
667 		    (void *)sp, (void *)hdlr, (uintptr_t)upc);
668 
669 		if (((uintptr_t)sp & (STACK_ALIGN - 1ul)) != 0)
670 			printf("bad stack alignment\n");
671 		else
672 			printf("sp above USERLIMIT\n");
673 #endif
674 		return (0);
675 	}
676 
677 	watched = watch_disable_addr((caddr_t)sp, minstacksz, S_WRITE);
678 
679 	if (on_fault(&ljb))
680 		goto badstack;
681 
682 	if (sip != NULL) {
683 		zoneid_t zoneid;
684 
685 		fp -= SA(sizeof (siginfo_t));
686 		uzero(fp, sizeof (siginfo_t));
687 		if (SI_FROMUSER(sip) &&
688 		    (zoneid = p->p_zone->zone_id) != GLOBAL_ZONEID &&
689 		    zoneid != sip->si_zoneid) {
690 			k_siginfo_t sani_sip = *sip;
691 
692 			sani_sip.si_pid = p->p_zone->zone_zsched->p_pid;
693 			sani_sip.si_uid = 0;
694 			sani_sip.si_ctid = -1;
695 			sani_sip.si_zoneid = zoneid;
696 			copyout_noerr(&sani_sip, fp, sizeof (sani_sip));
697 		} else
698 			copyout_noerr(sip, fp, sizeof (*sip));
699 		sip_addr = (siginfo_t *)fp;
700 
701 		if (sig == SIGPROF &&
702 		    curthread->t_rprof != NULL &&
703 		    curthread->t_rprof->rp_anystate) {
704 			/*
705 			 * We stand on our head to deal with
706 			 * the real time profiling signal.
707 			 * Fill in the stuff that doesn't fit
708 			 * in a normal k_siginfo structure.
709 			 */
710 			int i = sip->si_nsysarg;
711 
712 			while (--i >= 0)
713 				suword32_noerr(&(sip_addr->si_sysarg[i]),
714 				    (uint32_t)lwp->lwp_arg[i]);
715 			copyout_noerr(curthread->t_rprof->rp_state,
716 			    sip_addr->si_mstate,
717 			    sizeof (curthread->t_rprof->rp_state));
718 		}
719 	} else
720 		sip_addr = NULL;
721 
722 	/* save the current context on the user stack */
723 	fp -= SA(sizeof (*tuc));
724 	uc = (ucontext_t *)fp;
725 	tuc = kmem_alloc(sizeof (*tuc), KM_SLEEP);
726 	savecontext(tuc, lwp->lwp_sigoldmask);
727 	copyout_noerr(tuc, uc, sizeof (*tuc));
728 	kmem_free(tuc, sizeof (*tuc));
729 	tuc = NULL;
730 
731 	lwp->lwp_oldcontext = (uintptr_t)uc;
732 
733 	if (newstack) {
734 		lwp->lwp_sigaltstack.ss_flags |= SS_ONSTACK;
735 		if (lwp->lwp_ustack)
736 			copyout_noerr(&lwp->lwp_sigaltstack,
737 			    (stack_t *)lwp->lwp_ustack, sizeof (stack_t));
738 	}
739 
740 	/*
741 	 * Set up signal handler arguments
742 	 */
743 	{
744 		struct sigframe frame;
745 
746 		frame.sip = sip_addr;
747 		frame.ucp = uc;
748 		frame.signo = sig;
749 		frame.retaddr = (void (*)())0xffffffff;	/* never return! */
750 		copyout_noerr(&frame, sp, sizeof (frame));
751 	}
752 
753 	no_fault();
754 	if (watched)
755 		watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE);
756 
757 	rp->r_sp = (greg_t)sp;
758 	rp->r_pc = (greg_t)hdlr;
759 	rp->r_ps = PSL_USER | (rp->r_ps & PS_IOPL);
760 
761 	if ((rp->r_cs & 0xffff) != UCS_SEL ||
762 	    (rp->r_ss & 0xffff) != UDS_SEL) {
763 		rp->r_cs = UCS_SEL;
764 		rp->r_ss = UDS_SEL;
765 	}
766 
767 	/*
768 	 * Don't set lwp_eosys here.  sendsig() is called via psig() after
769 	 * lwp_eosys is handled, so setting it here would affect the next
770 	 * system call.
771 	 */
772 	return (1);
773 
774 badstack:
775 	no_fault();
776 	if (watched)
777 		watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE);
778 	if (tuc)
779 		kmem_free(tuc, sizeof (*tuc));
780 #ifdef DEBUG
781 	printf("sendsig: bad signal stack cmd=%s, pid=%d, sig=%d\n",
782 	    PTOU(p)->u_comm, p->p_pid, sig);
783 	printf("on fault, sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
784 	    (void *)sp, (void *)hdlr, (uintptr_t)upc);
785 #endif
786 	return (0);
787 }
788 
789 #endif	/* __i386 */
790