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
↓ 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>
↓ 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;
↓ 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));
↓ 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  
↓ 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
↓ 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));
↓ 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  
↓ 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