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 /*
  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>
  58 #include <sys/file.h>
  59 #include <sys/session.h>
  60 #include <sys/ucontext.h>
  61 #include <sys/dnlc.h>
  62 #include <sys/var.h>
  63 #include <sys/cmn_err.h>
  64 #include <sys/debugreg.h>
  65 #include <sys/thread.h>
  66 #include <sys/vtrace.h>
  67 #include <sys/consdev.h>
  68 #include <sys/psw.h>
  69 #include <sys/regset.h>
  70 
  71 #include <sys/privregs.h>
  72 
  73 #include <sys/stack.h>
  74 #include <sys/swap.h>
  75 #include <vm/hat.h>
  76 #include <vm/anon.h>
  77 #include <vm/as.h>
  78 #include <vm/page.h>
  79 #include <vm/seg.h>
  80 #include <vm/seg_kmem.h>
  81 #include <vm/seg_map.h>
  82 #include <vm/seg_vn.h>
  83 #include <sys/exec.h>
  84 #include <sys/acct.h>
  85 #include <sys/core.h>
  86 #include <sys/corectl.h>
  87 #include <sys/modctl.h>
  88 #include <sys/tuneable.h>
  89 #include <c2/audit.h>
  90 #include <sys/bootconf.h>
  91 #include <sys/dumphdr.h>
  92 #include <sys/promif.h>
  93 #include <sys/systeminfo.h>
  94 #include <sys/kdi.h>
  95 #include <sys/contract_impl.h>
  96 #include <sys/x86_archext.h>
  97 #include <sys/brand.h>
  98 #include <sys/sdt.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                  */
 221                 newstack = 0;
 222                 fp = BROP(p)->b_sendsig_stack(sig) - STACK_RESERVE;
 223         } else if (newstack) {
 224                 fp = (caddr_t)(SA((uintptr_t)lwp->lwp_sigaltstack.ss_sp) +
 225                     SA(lwp->lwp_sigaltstack.ss_size) - STACK_ALIGN);
 226         } else {
 227                 /*
 228                  * Drop below the 128-byte reserved region of the stack frame
 229                  * we're interrupting.
 230                  */
 231                 fp = (caddr_t)rp->r_sp - STACK_RESERVE;
 232         }
 233 
 234         /*
 235          * Force proper stack pointer alignment, even in the face of a
 236          * misaligned stack pointer from user-level before the signal.
 237          */
 238         fp = (caddr_t)((uintptr_t)fp & ~(STACK_ENTRY_ALIGN - 1ul));
 239 
 240         /*
 241          * Most of the time during normal execution, the stack pointer
 242          * is aligned on a STACK_ALIGN (i.e. 16 byte) boundary.  However,
 243          * (for example) just after a call instruction (which pushes
 244          * the return address), the callers stack misaligns until the
 245          * 'push %rbp' happens in the callee prolog.  So while we should
 246          * expect the stack pointer to be always at least STACK_ENTRY_ALIGN
 247          * aligned, we should -not- expect it to always be STACK_ALIGN aligned.
 248          * We now adjust to ensure that the new sp is aligned to
 249          * STACK_ENTRY_ALIGN but not to STACK_ALIGN.
 250          */
 251         sp = fp - minstacksz;
 252         if (((uintptr_t)sp & (STACK_ALIGN - 1ul)) == 0) {
 253                 sp -= STACK_ENTRY_ALIGN;
 254                 minstacksz = fp - sp;
 255         }
 256 
 257         /*
 258          * Now, make sure the resulting signal frame address is sane
 259          */
 260         if (sp >= as->a_userlimit || fp >= as->a_userlimit) {
 261 #ifdef DEBUG
 262                 printf("sendsig: bad signal stack cmd=%s, pid=%d, sig=%d\n",
 263                     PTOU(p)->u_comm, p->p_pid, sig);
 264                 printf("sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
 265                     (void *)sp, (void *)hdlr, (uintptr_t)upc);
 266                 printf("sp above USERLIMIT\n");
 267 #endif
 268                 return (0);
 269         }
 270 
 271         watched = watch_disable_addr((caddr_t)sp, minstacksz, S_WRITE);
 272 
 273         if (on_fault(&ljb))
 274                 goto badstack;
 275 
 276         if (sip != NULL) {
 277                 zoneid_t zoneid;
 278 
 279                 fp -= SA(sizeof (siginfo_t));
 280                 uzero(fp, sizeof (siginfo_t));
 281                 if (SI_FROMUSER(sip) &&
 282                     (zoneid = p->p_zone->zone_id) != GLOBAL_ZONEID &&
 283                     zoneid != sip->si_zoneid) {
 284                         k_siginfo_t sani_sip = *sip;
 285 
 286                         sani_sip.si_pid = p->p_zone->zone_zsched->p_pid;
 287                         sani_sip.si_uid = 0;
 288                         sani_sip.si_ctid = -1;
 289                         sani_sip.si_zoneid = zoneid;
 290                         copyout_noerr(&sani_sip, fp, sizeof (sani_sip));
 291                 } else
 292                         copyout_noerr(sip, fp, sizeof (*sip));
 293                 sip_addr = (siginfo_t *)fp;
 294 
 295                 if (sig == SIGPROF &&
 296                     curthread->t_rprof != NULL &&
 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          */
 357         {
 358                 struct sigframe frame;
 359 
 360                 /*
 361                  * ensure we never return "normally"
 362                  */
 363                 frame.retaddr = (caddr_t)(uintptr_t)-1L;
 364                 frame.signo = sig;
 365                 frame.sip = sip_addr;
 366                 copyout_noerr(&frame, sp, sizeof (frame));
 367         }
 368 
 369         no_fault();
 370         if (watched)
 371                 watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE);
 372 
 373         /*
 374          * Set up user registers for execution of signal handler.
 375          */
 376         rp->r_sp = (greg_t)sp;
 377         rp->r_pc = (greg_t)hdlr;
 378         rp->r_ps = PSL_USER | (rp->r_ps & PS_IOPL);
 379 
 380         rp->r_rdi = sig;
 381         rp->r_rsi = (uintptr_t)sip_addr;
 382         rp->r_rdx = (uintptr_t)uc;
 383 
 384         if ((rp->r_cs & 0xffff) != UCS_SEL ||
 385             (rp->r_ss & 0xffff) != UDS_SEL) {
 386                 /*
 387                  * Try our best to deliver the signal.
 388                  */
 389                 rp->r_cs = UCS_SEL;
 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;
 497                 fp = BROP(p)->b_sendsig_stack(sig);
 498         } else if (newstack) {
 499                 fp = (caddr_t)(SA32((uintptr_t)lwp->lwp_sigaltstack.ss_sp) +
 500                     SA32(lwp->lwp_sigaltstack.ss_size) - STACK_ALIGN32);
 501         } else if ((rp->r_ss & 0xffff) != UDS_SEL) {
 502                 user_desc_t *ldt;
 503                 /*
 504                  * If the stack segment selector is -not- pointing at
 505                  * the UDS_SEL descriptor and we have an LDT entry for
 506                  * it instead, add the base address to find the effective va.
 507                  */
 508                 if ((ldt = p->p_ldt) != NULL)
 509                         fp = (caddr_t)rp->r_sp +
 510                             USEGD_GETBASE(&ldt[SELTOIDX(rp->r_ss)]);
 511                 else
 512                         fp = (caddr_t)rp->r_sp;
 513         } else {
 514                 fp = (caddr_t)rp->r_sp;
 515         }
 516 
 517         /*
 518          * Force proper stack pointer alignment, even in the face of a
 519          * misaligned stack pointer from user-level before the signal.
 520          * Don't use the SA32() macro because that rounds up, not down.
 521          */
 522         fp = (caddr_t)((uintptr_t)fp & ~(STACK_ALIGN32 - 1));
 523         sp = fp - minstacksz;
 524 
 525         /*
 526          * Make sure lwp hasn't trashed its stack
 527          */
 528         if (sp >= (caddr_t)(uintptr_t)USERLIMIT32 ||
 529             fp >= (caddr_t)(uintptr_t)USERLIMIT32) {
 530 #ifdef DEBUG
 531                 printf("sendsig32: bad signal stack cmd=%s, pid=%d, sig=%d\n",
 532                     PTOU(p)->u_comm, p->p_pid, sig);
 533                 printf("sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
 534                     (void *)sp, (void *)hdlr, (uintptr_t)upc);
 535                 printf("sp above USERLIMIT\n");
 536 #endif
 537                 return (0);
 538         }
 539 
 540         watched = watch_disable_addr((caddr_t)sp, minstacksz, S_WRITE);
 541 
 542         if (on_fault(&ljb))
 543                 goto badstack;
 544 
 545         if (sip != NULL) {
 546                 siginfo32_t si32;
 547                 zoneid_t zoneid;
 548 
 549                 siginfo_kto32(sip, &si32);
 550                 if (SI_FROMUSER(sip) &&
 551                     (zoneid = p->p_zone->zone_id) != GLOBAL_ZONEID &&
 552                     zoneid != sip->si_zoneid) {
 553                         si32.si_pid = p->p_zone->zone_zsched->p_pid;
 554                         si32.si_uid = 0;
 555                         si32.si_ctid = -1;
 556                         si32.si_zoneid = zoneid;
 557                 }
 558                 fp -= SA32(sizeof (si32));
 559                 uzero(fp, sizeof (si32));
 560                 copyout_noerr(&si32, fp, sizeof (si32));
 561                 sip_addr = (siginfo32_t *)fp;
 562 
 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)
 617                             lwp->lwp_sigaltstack.ss_flags;
 618                         copyout_noerr(&stk32,
 619                             (stack32_t *)lwp->lwp_ustack, sizeof (stk32));
 620                 }
 621         }
 622 
 623         /*
 624          * Set up signal handler arguments
 625          */
 626         {
 627                 struct sigframe32 frame32;
 628 
 629                 frame32.sip = (caddr32_t)(uintptr_t)sip_addr;
 630                 frame32.ucp = (caddr32_t)(uintptr_t)uc;
 631                 frame32.signo = sig;
 632                 frame32.retaddr = 0xffffffff;   /* never return! */
 633                 copyout_noerr(&frame32, sp, sizeof (frame32));
 634         }
 635 
 636         no_fault();
 637         if (watched)
 638                 watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE);
 639 
 640         rp->r_sp = (greg_t)(uintptr_t)sp;
 641         rp->r_pc = (greg_t)(uintptr_t)hdlr;
 642         rp->r_ps = PSL_USER | (rp->r_ps & PS_IOPL);
 643 
 644         if ((rp->r_cs & 0xffff) != U32CS_SEL ||
 645             (rp->r_ss & 0xffff) != UDS_SEL) {
 646                 /*
 647                  * Try our best to deliver the signal.
 648                  */
 649                 rp->r_cs = U32CS_SEL;
 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 */