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