Print this page
15254 %ymm registers not restored after signal handler
15367 x86 getfpregs() summons corrupting %xmm ghosts
15333 want x86 /proc xregs support (libc_db, libproc, mdb, etc.)
15336 want libc functions for extended ucontext_t
15334 want ps_lwphandle-specific reg routines
15328 FPU_CW_INIT mistreats reserved bit
15335 i86pc fpu_subr.c isn't really platform-specific
15332 setcontext(2) isn't actually noreturn
15331 need <sys/stdalign.h>
Change-Id: I7060aa86042dfb989f77fc3323c065ea2eafa9ad
Conflicts:
    usr/src/uts/common/fs/proc/prcontrol.c
    usr/src/uts/intel/os/archdep.c
    usr/src/uts/intel/sys/ucontext.h
    usr/src/uts/intel/syscall/getcontext.c


  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>


  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 
 108 /*
 109  * An amd64 signal frame looks like this on the stack:
 110  *
 111  * old %rsp:
 112  *              <128 bytes of untouched stack space>
 113  *              <a siginfo_t [optional]>
 114  *              <a ucontext_t>
 115  *              <siginfo_t *>
 116  *              <signal number>
 117  * new %rsp:    <return address (deliberately invalid)>

 118  *
 119  * The signal number and siginfo_t pointer are only pushed onto the stack in
 120  * order to allow stack backtraces.  The actual signal handling code expects the
 121  * arguments in registers.
 122  */
 123 
 124 struct sigframe {
 125         caddr_t retaddr;
 126         long    signo;
 127         siginfo_t *sip;
 128 };
 129 
 130 int
 131 sendsig(int sig, k_siginfo_t *sip, void (*hdlr)())
 132 {
 133         volatile int minstacksz;
 134         int newstack;


 135         label_t ljb;
 136         volatile caddr_t sp;
 137         caddr_t fp;
 138         volatile struct regs *rp;
 139         volatile greg_t upc;
 140         volatile proc_t *p = ttoproc(curthread);
 141         struct as *as = p->p_as;
 142         klwp_t *lwp = ttolwp(curthread);
 143         ucontext_t *volatile tuc = NULL;
 144         ucontext_t *uc;
 145         siginfo_t *sip_addr;
 146         volatile int watched;
 147 
 148         /*
 149          * This routine is utterly dependent upon STACK_ALIGN being
 150          * 16 and STACK_ENTRY_ALIGN being 8. Let's just acknowledge
 151          * that and require it.
 152          */
 153 
 154 #if STACK_ALIGN != 16 || STACK_ENTRY_ALIGN != 8
 155 #error "sendsig() amd64 did not find the expected stack alignments"
 156 #endif
 157 
 158         rp = lwptoregs(lwp);
 159         upc = rp->r_pc;
 160 
 161         /*
 162          * Since we're setting up to run the signal handler we have to
 163          * arrange that the stack at entry to the handler is (only)
 164          * STACK_ENTRY_ALIGN (i.e. 8) byte aligned so that when the handler
 165          * executes its push of %rbp, the stack realigns to STACK_ALIGN
 166          * (i.e. 16) correctly.
 167          *
 168          * The new sp will point to the sigframe and the ucontext_t. The
 169          * above means that sp (and thus sigframe) will be 8-byte aligned,
 170          * but not 16-byte aligned. ucontext_t, however, contains %xmm regs
 171          * which must be 16-byte aligned. Because of this, for correct
 172          * alignment, sigframe must be a multiple of 8-bytes in length, but
 173          * not 16-bytes. This will place ucontext_t at a nice 16-byte boundary.





 174          */
 175 
 176         /* LINTED: logical expression always true: op "||" */
 177         ASSERT((sizeof (struct sigframe) % 16) == 8);
 178 
 179         minstacksz = sizeof (struct sigframe) + SA(sizeof (*uc));
 180         if (sip != NULL)
 181                 minstacksz += SA(sizeof (siginfo_t));








 182         ASSERT((minstacksz & (STACK_ENTRY_ALIGN - 1ul)) == 0);
 183 
 184         /*
 185          * Figure out whether we will be handling this signal on
 186          * an alternate stack specified by the user.  Then allocate
 187          * and validate the stack requirements for the signal handler
 188          * context.  on_fault will catch any faults.
 189          */
 190         newstack = sigismember(&PTOU(curproc)->u_sigonstack, sig) &&
 191             !(lwp->lwp_sigaltstack.ss_flags & (SS_ONSTACK|SS_DISABLE));
 192 
 193         /*
 194          * If this is a branded process, the brand may provide an alternate
 195          * stack pointer for signal delivery:
 196          */
 197         if (PROC_IS_BRANDED(p) && BROP(p)->b_sendsig_stack != NULL) {
 198                 /*
 199                  * Use the stack pointer value provided by the brand,
 200                  * accounting for the 128-byte reserved region.
 201                  */


 278                     curthread->t_rprof->rp_anystate) {
 279                         /*
 280                          * We stand on our head to deal with
 281                          * the real time profiling signal.
 282                          * Fill in the stuff that doesn't fit
 283                          * in a normal k_siginfo structure.
 284                          */
 285                         int i = sip->si_nsysarg;
 286 
 287                         while (--i >= 0)
 288                                 sulword_noerr(
 289                                     (ulong_t *)&(sip_addr->si_sysarg[i]),
 290                                     (ulong_t)lwp->lwp_arg[i]);
 291                         copyout_noerr(curthread->t_rprof->rp_state,
 292                             sip_addr->si_mstate,
 293                             sizeof (curthread->t_rprof->rp_state));
 294                 }
 295         } else
 296                 sip_addr = NULL;
 297 


 298         /*
 299          * save the current context on the user stack directly after the
 300          * sigframe. Since sigframe is 8-byte-but-not-16-byte aligned,
 301          * and since sizeof (struct sigframe) is 24, this guarantees
 302          * 16-byte alignment for ucontext_t and its %xmm registers.



 303          */
 304         uc = (ucontext_t *)(sp + sizeof (struct sigframe));
 305         tuc = kmem_alloc(sizeof (*tuc), KM_SLEEP);
 306         no_fault();
 307         savecontext(tuc, &lwp->lwp_sigoldmask);






 308         if (on_fault(&ljb))
 309                 goto badstack;
 310         copyout_noerr(tuc, uc, sizeof (*tuc));
 311         kmem_free(tuc, sizeof (*tuc));
 312         tuc = NULL;
 313 
 314         DTRACE_PROBE3(oldcontext__set, klwp_t *, lwp,
 315             uintptr_t, lwp->lwp_oldcontext, uintptr_t, (uintptr_t)uc);
 316         lwp->lwp_oldcontext = (uintptr_t)uc;
 317 
 318         if (newstack) {
 319                 lwp->lwp_sigaltstack.ss_flags |= SS_ONSTACK;
 320                 if (lwp->lwp_ustack)
 321                         copyout_noerr(&lwp->lwp_sigaltstack,
 322                             (stack_t *)lwp->lwp_ustack, sizeof (stack_t));
 323         }
 324 
 325         /*
 326          * Set up signal handler return and stack linkage
 327          */


 361                 rp->r_ss = UDS_SEL;
 362         }
 363 
 364         /*
 365          * Allow the brand to perform additional book-keeping once the signal
 366          * handling frame has been fully assembled:
 367          */
 368         if (PROC_IS_BRANDED(p) && BROP(p)->b_sendsig != NULL) {
 369                 BROP(p)->b_sendsig(sig);
 370         }
 371 
 372         /*
 373          * Don't set lwp_eosys here.  sendsig() is called via psig() after
 374          * lwp_eosys is handled, so setting it here would affect the next
 375          * system call.
 376          */
 377         return (1);
 378 
 379 badstack:
 380         no_fault();

 381         if (watched)
 382                 watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE);
 383         if (tuc)
 384                 kmem_free(tuc, sizeof (*tuc));
 385 #ifdef DEBUG
 386         printf("sendsig: bad signal stack cmd=%s, pid=%d, sig=%d\n",
 387             PTOU(p)->u_comm, p->p_pid, sig);
 388         printf("on fault, sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
 389             (void *)sp, (void *)hdlr, (uintptr_t)upc);
 390 #endif
 391         return (0);
 392 }
 393 
 394 #ifdef _SYSCALL32_IMPL
 395 
 396 /*
 397  * An i386 SVR4/ABI signal frame looks like this on the stack:
 398  *
 399  * old %esp:
 400  *              <a siginfo32_t [optional]>
 401  *              <a ucontext32_t>

 402  *              <pointer to that ucontext32_t>
 403  *              <pointer to that siginfo32_t>
 404  *              <signo>
 405  * new %esp:    <return address (deliberately invalid)>
 406  */
 407 struct sigframe32 {
 408         caddr32_t       retaddr;
 409         uint32_t        signo;
 410         caddr32_t       sip;
 411         caddr32_t       ucp;
 412 };
 413 
 414 int
 415 sendsig32(int sig, k_siginfo_t *sip, void (*hdlr)())
 416 {
 417         volatile int minstacksz;
 418         int newstack;


 419         label_t ljb;
 420         volatile caddr_t sp;
 421         caddr_t fp;
 422         volatile struct regs *rp;
 423         volatile greg_t upc;
 424         volatile proc_t *p = ttoproc(curthread);
 425         klwp_t *lwp = ttolwp(curthread);
 426         ucontext32_t *volatile tuc = NULL;
 427         ucontext32_t *uc;
 428         siginfo32_t *sip_addr;
 429         volatile int watched;
 430 
 431         rp = lwptoregs(lwp);
 432         upc = rp->r_pc;
 433 
 434         minstacksz = SA32(sizeof (struct sigframe32)) + SA32(sizeof (*uc));
 435         if (sip != NULL)
 436                 minstacksz += SA32(sizeof (siginfo32_t));







 437         ASSERT((minstacksz & (STACK_ALIGN32 - 1)) == 0);
 438 
 439         /*
 440          * Figure out whether we will be handling this signal on
 441          * an alternate stack specified by the user.  Then allocate
 442          * and validate the stack requirements for the signal handler
 443          * context.  on_fault will catch any faults.
 444          */
 445         newstack = sigismember(&PTOU(curproc)->u_sigonstack, sig) &&
 446             !(lwp->lwp_sigaltstack.ss_flags & (SS_ONSTACK|SS_DISABLE));
 447 
 448         /*
 449          * If this is a branded process, the brand may provide an alternate
 450          * stack pointer for signal delivery:
 451          */
 452         if (PROC_IS_BRANDED(p) && BROP(p)->b_sendsig_stack != NULL) {
 453                 /*
 454                  * Use the stack pointer value provided by the brand:
 455                  */
 456                 newstack = 0;


 523                 if (sig == SIGPROF &&
 524                     curthread->t_rprof != NULL &&
 525                     curthread->t_rprof->rp_anystate) {
 526                         /*
 527                          * We stand on our head to deal with
 528                          * the real-time profiling signal.
 529                          * Fill in the stuff that doesn't fit
 530                          * in a normal k_siginfo structure.
 531                          */
 532                         int i = sip->si_nsysarg;
 533 
 534                         while (--i >= 0)
 535                                 suword32_noerr(&(sip_addr->si_sysarg[i]),
 536                                     (uint32_t)lwp->lwp_arg[i]);
 537                         copyout_noerr(curthread->t_rprof->rp_state,
 538                             sip_addr->si_mstate,
 539                             sizeof (curthread->t_rprof->rp_state));
 540                 }
 541         } else
 542                 sip_addr = NULL;

 543 
 544         /* save the current context on the user stack */

 545         fp -= SA32(sizeof (*tuc));
 546         uc = (ucontext32_t *)fp;
 547         tuc = kmem_alloc(sizeof (*tuc), KM_SLEEP);
 548         no_fault();
 549         savecontext32(tuc, &lwp->lwp_sigoldmask);





 550         if (on_fault(&ljb))
 551                 goto badstack;
 552         copyout_noerr(tuc, uc, sizeof (*tuc));
 553         kmem_free(tuc, sizeof (*tuc));
 554         tuc = NULL;
 555 
 556         DTRACE_PROBE3(oldcontext__set, klwp_t *, lwp,
 557             uintptr_t, lwp->lwp_oldcontext, uintptr_t, (uintptr_t)uc);
 558         lwp->lwp_oldcontext = (uintptr_t)uc;
 559 
 560         if (newstack) {
 561                 lwp->lwp_sigaltstack.ss_flags |= SS_ONSTACK;
 562                 if (lwp->lwp_ustack) {
 563                         stack32_t stk32;
 564 
 565                         stk32.ss_sp = (caddr32_t)(uintptr_t)
 566                             lwp->lwp_sigaltstack.ss_sp;
 567                         stk32.ss_size = (size32_t)
 568                             lwp->lwp_sigaltstack.ss_size;
 569                         stk32.ss_flags = (int32_t)


 603                 rp->r_ss = UDS_SEL;
 604         }
 605 
 606         /*
 607          * Allow the brand to perform additional book-keeping once the signal
 608          * handling frame has been fully assembled:
 609          */
 610         if (PROC_IS_BRANDED(p) && BROP(p)->b_sendsig != NULL) {
 611                 BROP(p)->b_sendsig(sig);
 612         }
 613 
 614         /*
 615          * Don't set lwp_eosys here.  sendsig() is called via psig() after
 616          * lwp_eosys is handled, so setting it here would affect the next
 617          * system call.
 618          */
 619         return (1);
 620 
 621 badstack:
 622         no_fault();

 623         if (watched)
 624                 watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE);
 625         if (tuc)
 626                 kmem_free(tuc, sizeof (*tuc));
 627 #ifdef DEBUG
 628         printf("sendsig32: bad signal stack cmd=%s pid=%d, sig=%d\n",
 629             PTOU(p)->u_comm, p->p_pid, sig);
 630         printf("on fault, sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
 631             (void *)sp, (void *)hdlr, (uintptr_t)upc);
 632 #endif
 633         return (0);
 634 }
 635 
 636 #endif  /* _SYSCALL32_IMPL */


  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 /*
  35  * Copyright 2023 Oxide Computer Company
  36  */
  37 
  38 #include <sys/types.h>
  39 #include <sys/param.h>
  40 #include <sys/sysmacros.h>
  41 #include <sys/signal.h>
  42 #include <sys/systm.h>
  43 #include <sys/user.h>
  44 #include <sys/mman.h>
  45 #include <sys/class.h>
  46 #include <sys/proc.h>
  47 #include <sys/procfs.h>
  48 #include <sys/buf.h>
  49 #include <sys/kmem.h>
  50 #include <sys/cred.h>
  51 #include <sys/archsystm.h>
  52 #include <sys/vmparam.h>
  53 #include <sys/prsystm.h>
  54 #include <sys/reboot.h>
  55 #include <sys/uadmin.h>
  56 #include <sys/vfs.h>
  57 #include <sys/vnode.h>


  99 
 100 /*
 101  * Construct the execution environment for the user's signal
 102  * handler and arrange for control to be given to it on return
 103  * to userland.  The library code now calls setcontext() to
 104  * clean up after the signal handler, so sigret() is no longer
 105  * needed.
 106  *
 107  * (The various 'volatile' declarations are need to ensure that values
 108  * are correct on the error return from on_fault().)
 109  */
 110 
 111 
 112 /*
 113  * An amd64 signal frame looks like this on the stack:
 114  *
 115  * old %rsp:
 116  *              <128 bytes of untouched stack space>
 117  *              <a siginfo_t [optional]>
 118  *              <a ucontext_t>
 119  *              <a ucontext_t's xsave state>
 120  *              <siginfo_t *>                             ---+
 121  *              <signal number>                              | sigframe
 122  * new %rsp:    <return address (deliberately invalid)>   ---+
 123  *
 124  * The signal number and siginfo_t pointer are only pushed onto the stack in
 125  * order to allow stack backtraces.  The actual signal handling code expects the
 126  * arguments in registers.
 127  */
 128 
 129 struct sigframe {
 130         caddr_t retaddr;
 131         long    signo;
 132         siginfo_t *sip;
 133 };
 134 
 135 int
 136 sendsig(int sig, k_siginfo_t *sip, void (*hdlr)())
 137 {
 138         volatile size_t minstacksz;
 139         boolean_t newstack;
 140         size_t xsave_size;
 141         int ret;
 142         label_t ljb;
 143         volatile caddr_t sp;
 144         caddr_t fp;
 145         volatile struct regs *rp;
 146         volatile greg_t upc;
 147         volatile proc_t *p = ttoproc(curthread);
 148         struct as *as = p->p_as;
 149         klwp_t *lwp = ttolwp(curthread);
 150         ucontext_t *volatile tuc = NULL;
 151         ucontext_t *uc;
 152         siginfo_t *sip_addr;
 153         volatile int watched;
 154 
 155         /*
 156          * This routine is utterly dependent upon STACK_ALIGN being
 157          * 16 and STACK_ENTRY_ALIGN being 8. Let's just acknowledge
 158          * that and require it.
 159          */
 160 
 161 #if STACK_ALIGN != 16 || STACK_ENTRY_ALIGN != 8
 162 #error "sendsig() amd64 did not find the expected stack alignments"
 163 #endif
 164 
 165         rp = lwptoregs(lwp);
 166         upc = rp->r_pc;
 167 
 168         /*
 169          * Since we're setting up to run the signal handler we have to
 170          * arrange that the stack at entry to the handler is (only)
 171          * STACK_ENTRY_ALIGN (i.e. 8) byte aligned so that when the handler
 172          * executes its push of %rbp, the stack realigns to STACK_ALIGN
 173          * (i.e. 16) correctly.
 174          *
 175          * The new sp will point to the sigframe and the ucontext_t. The
 176          * above means that sp (and thus sigframe) will be 8-byte aligned,
 177          * but not 16-byte aligned. ucontext_t, however, contains %xmm regs
 178          * which must be 16-byte aligned. Because of this, for correct
 179          * alignment, sigframe must be a multiple of 8-bytes in length, but
 180          * not 16-bytes. This will place ucontext_t at a nice 16-byte boundary.
 181          *
 182          * When we move onto the xsave state, right now, we don't guarantee any
 183          * alignment of the resulting data, but we will ensure that the
 184          * resulting sp does have proper alignment. This will ensure that the
 185          * guarantee on the ucontex_t is not violated.
 186          */
 187 
 188         CTASSERT((sizeof (struct sigframe) % 16) == 8);

 189 
 190         minstacksz = sizeof (struct sigframe) + SA(sizeof (*uc));
 191         if (sip != NULL)
 192                 minstacksz += SA(sizeof (siginfo_t));
 193 
 194         if (fpu_xsave_enabled()) {
 195                 xsave_size = SA(fpu_signal_size(lwp));
 196                 minstacksz += xsave_size;
 197         } else {
 198                 xsave_size = 0;
 199         }
 200 
 201         ASSERT((minstacksz & (STACK_ENTRY_ALIGN - 1ul)) == 0);
 202 
 203         /*
 204          * Figure out whether we will be handling this signal on
 205          * an alternate stack specified by the user.  Then allocate
 206          * and validate the stack requirements for the signal handler
 207          * context.  on_fault will catch any faults.
 208          */
 209         newstack = sigismember(&PTOU(curproc)->u_sigonstack, sig) &&
 210             !(lwp->lwp_sigaltstack.ss_flags & (SS_ONSTACK|SS_DISABLE));
 211 
 212         /*
 213          * If this is a branded process, the brand may provide an alternate
 214          * stack pointer for signal delivery:
 215          */
 216         if (PROC_IS_BRANDED(p) && BROP(p)->b_sendsig_stack != NULL) {
 217                 /*
 218                  * Use the stack pointer value provided by the brand,
 219                  * accounting for the 128-byte reserved region.
 220                  */


 297                     curthread->t_rprof->rp_anystate) {
 298                         /*
 299                          * We stand on our head to deal with
 300                          * the real time profiling signal.
 301                          * Fill in the stuff that doesn't fit
 302                          * in a normal k_siginfo structure.
 303                          */
 304                         int i = sip->si_nsysarg;
 305 
 306                         while (--i >= 0)
 307                                 sulword_noerr(
 308                                     (ulong_t *)&(sip_addr->si_sysarg[i]),
 309                                     (ulong_t)lwp->lwp_arg[i]);
 310                         copyout_noerr(curthread->t_rprof->rp_state,
 311                             sip_addr->si_mstate,
 312                             sizeof (curthread->t_rprof->rp_state));
 313                 }
 314         } else
 315                 sip_addr = NULL;
 316 
 317         no_fault();
 318 
 319         /*
 320          * Save the current context on the user stack directly after the
 321          * sigframe. Since sigframe is 8-byte-but-not-16-byte aligned, and since
 322          * sizeof (struct sigframe) is 24, this guarantees 16-byte alignment for
 323          * ucontext_t and its %xmm registers. The xsave state part of the
 324          * ucontext_t may be inbetween these two. However, we have ensured that
 325          * the size of the stack space is 16-byte aligned as the actual size may
 326          * vary.
 327          */

 328         tuc = kmem_alloc(sizeof (*tuc), KM_SLEEP);
 329         if (xsave_size != 0) {
 330                 tuc->uc_xsave = (unsigned long)(sp + sizeof (struct sigframe));
 331         }
 332         uc = (ucontext_t *)(sp + sizeof (struct sigframe) + xsave_size);
 333         ret = savecontext(tuc, &lwp->lwp_sigoldmask, SAVECTXT_F_EXTD |
 334             SAVECTXT_F_ONFAULT);
 335         if (ret != 0)
 336                 goto postfault;
 337         if (on_fault(&ljb))
 338                 goto badstack;
 339         copyout_noerr(tuc, uc, sizeof (*tuc));
 340         kmem_free(tuc, sizeof (*tuc));
 341         tuc = NULL;
 342 
 343         DTRACE_PROBE3(oldcontext__set, klwp_t *, lwp,
 344             uintptr_t, lwp->lwp_oldcontext, uintptr_t, (uintptr_t)uc);
 345         lwp->lwp_oldcontext = (uintptr_t)uc;
 346 
 347         if (newstack) {
 348                 lwp->lwp_sigaltstack.ss_flags |= SS_ONSTACK;
 349                 if (lwp->lwp_ustack)
 350                         copyout_noerr(&lwp->lwp_sigaltstack,
 351                             (stack_t *)lwp->lwp_ustack, sizeof (stack_t));
 352         }
 353 
 354         /*
 355          * Set up signal handler return and stack linkage
 356          */


 390                 rp->r_ss = UDS_SEL;
 391         }
 392 
 393         /*
 394          * Allow the brand to perform additional book-keeping once the signal
 395          * handling frame has been fully assembled:
 396          */
 397         if (PROC_IS_BRANDED(p) && BROP(p)->b_sendsig != NULL) {
 398                 BROP(p)->b_sendsig(sig);
 399         }
 400 
 401         /*
 402          * Don't set lwp_eosys here.  sendsig() is called via psig() after
 403          * lwp_eosys is handled, so setting it here would affect the next
 404          * system call.
 405          */
 406         return (1);
 407 
 408 badstack:
 409         no_fault();
 410 postfault:
 411         if (watched)
 412                 watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE);
 413         if (tuc)
 414                 kmem_free(tuc, sizeof (*tuc));
 415 #ifdef DEBUG
 416         printf("sendsig: bad signal stack cmd=%s, pid=%d, sig=%d\n",
 417             PTOU(p)->u_comm, p->p_pid, sig);
 418         printf("on fault, sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
 419             (void *)sp, (void *)hdlr, (uintptr_t)upc);
 420 #endif
 421         return (0);
 422 }
 423 
 424 #ifdef _SYSCALL32_IMPL
 425 
 426 /*
 427  * An i386 SVR4/ABI signal frame looks like this on the stack:
 428  *
 429  * old %esp:
 430  *              <a siginfo32_t [optional]>
 431  *              <a ucontext32_t>
 432  *              <a ucontext32_t's xsave state>
 433  *              <pointer to that ucontext32_t>
 434  *              <pointer to that siginfo32_t>
 435  *              <signo>
 436  * new %esp:    <return address (deliberately invalid)>
 437  */
 438 struct sigframe32 {
 439         caddr32_t       retaddr;
 440         uint32_t        signo;
 441         caddr32_t       sip;
 442         caddr32_t       ucp;
 443 };
 444 
 445 int
 446 sendsig32(int sig, k_siginfo_t *sip, void (*hdlr)())
 447 {
 448         volatile size_t minstacksz;
 449         boolean_t newstack;
 450         size_t xsave_size;
 451         int ret;
 452         label_t ljb;
 453         volatile caddr_t sp;
 454         caddr_t fp;
 455         volatile struct regs *rp;
 456         volatile greg_t upc;
 457         volatile proc_t *p = ttoproc(curthread);
 458         klwp_t *lwp = ttolwp(curthread);
 459         ucontext32_t *volatile tuc = NULL;
 460         ucontext32_t *uc;
 461         siginfo32_t *sip_addr;
 462         volatile int watched;
 463 
 464         rp = lwptoregs(lwp);
 465         upc = rp->r_pc;
 466 
 467         minstacksz = SA32(sizeof (struct sigframe32)) + SA32(sizeof (*uc));
 468         if (sip != NULL)
 469                 minstacksz += SA32(sizeof (siginfo32_t));
 470 
 471         if (fpu_xsave_enabled()) {
 472                 xsave_size = SA32(fpu_signal_size(lwp));
 473                 minstacksz += xsave_size;
 474         } else {
 475                 xsave_size = 0;
 476         }
 477         ASSERT((minstacksz & (STACK_ALIGN32 - 1)) == 0);
 478 
 479         /*
 480          * Figure out whether we will be handling this signal on
 481          * an alternate stack specified by the user.  Then allocate
 482          * and validate the stack requirements for the signal handler
 483          * context.  on_fault will catch any faults.
 484          */
 485         newstack = sigismember(&PTOU(curproc)->u_sigonstack, sig) &&
 486             !(lwp->lwp_sigaltstack.ss_flags & (SS_ONSTACK|SS_DISABLE));
 487 
 488         /*
 489          * If this is a branded process, the brand may provide an alternate
 490          * stack pointer for signal delivery:
 491          */
 492         if (PROC_IS_BRANDED(p) && BROP(p)->b_sendsig_stack != NULL) {
 493                 /*
 494                  * Use the stack pointer value provided by the brand:
 495                  */
 496                 newstack = 0;


 563                 if (sig == SIGPROF &&
 564                     curthread->t_rprof != NULL &&
 565                     curthread->t_rprof->rp_anystate) {
 566                         /*
 567                          * We stand on our head to deal with
 568                          * the real-time profiling signal.
 569                          * Fill in the stuff that doesn't fit
 570                          * in a normal k_siginfo structure.
 571                          */
 572                         int i = sip->si_nsysarg;
 573 
 574                         while (--i >= 0)
 575                                 suword32_noerr(&(sip_addr->si_sysarg[i]),
 576                                     (uint32_t)lwp->lwp_arg[i]);
 577                         copyout_noerr(curthread->t_rprof->rp_state,
 578                             sip_addr->si_mstate,
 579                             sizeof (curthread->t_rprof->rp_state));
 580                 }
 581         } else
 582                 sip_addr = NULL;
 583         no_fault();
 584 
 585         /* save the current context on the user stack */
 586         tuc = kmem_alloc(sizeof (*tuc), KM_SLEEP);
 587         fp -= SA32(sizeof (*tuc));
 588         uc = (ucontext32_t *)fp;
 589         if (xsave_size != 0) {
 590                 fp -= xsave_size;
 591                 tuc->uc_xsave = (int32_t)(uintptr_t)fp;
 592         }
 593         ret = savecontext32(tuc, &lwp->lwp_sigoldmask, SAVECTXT_F_EXTD |
 594             SAVECTXT_F_ONFAULT);
 595         if (ret != 0)
 596                 goto postfault;
 597         if (on_fault(&ljb))
 598                 goto badstack;
 599         copyout_noerr(tuc, uc, sizeof (*tuc));
 600         kmem_free(tuc, sizeof (*tuc));
 601         tuc = NULL;
 602 
 603         DTRACE_PROBE3(oldcontext__set, klwp_t *, lwp,
 604             uintptr_t, lwp->lwp_oldcontext, uintptr_t, (uintptr_t)uc);
 605         lwp->lwp_oldcontext = (uintptr_t)uc;
 606 
 607         if (newstack) {
 608                 lwp->lwp_sigaltstack.ss_flags |= SS_ONSTACK;
 609                 if (lwp->lwp_ustack) {
 610                         stack32_t stk32;
 611 
 612                         stk32.ss_sp = (caddr32_t)(uintptr_t)
 613                             lwp->lwp_sigaltstack.ss_sp;
 614                         stk32.ss_size = (size32_t)
 615                             lwp->lwp_sigaltstack.ss_size;
 616                         stk32.ss_flags = (int32_t)


 650                 rp->r_ss = UDS_SEL;
 651         }
 652 
 653         /*
 654          * Allow the brand to perform additional book-keeping once the signal
 655          * handling frame has been fully assembled:
 656          */
 657         if (PROC_IS_BRANDED(p) && BROP(p)->b_sendsig != NULL) {
 658                 BROP(p)->b_sendsig(sig);
 659         }
 660 
 661         /*
 662          * Don't set lwp_eosys here.  sendsig() is called via psig() after
 663          * lwp_eosys is handled, so setting it here would affect the next
 664          * system call.
 665          */
 666         return (1);
 667 
 668 badstack:
 669         no_fault();
 670 postfault:
 671         if (watched)
 672                 watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE);
 673         if (tuc)
 674                 kmem_free(tuc, sizeof (*tuc));
 675 #ifdef DEBUG
 676         printf("sendsig32: bad signal stack cmd=%s pid=%d, sig=%d\n",
 677             PTOU(p)->u_comm, p->p_pid, sig);
 678         printf("on fault, sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
 679             (void *)sp, (void *)hdlr, (uintptr_t)upc);
 680 #endif
 681         return (0);
 682 }
 683 
 684 #endif  /* _SYSCALL32_IMPL */