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