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