1 /*
   2  * This file and its contents are supplied under the terms of the
   3  * Common Development and Distribution License ("CDDL"), version 1.0.
   4  * You may only use this file in accordance with the terms of version
   5  * 1.0 of the CDDL.
   6  *
   7  * A full copy of the text of the CDDL should have accompanied this
   8  * source.  A copy of the CDDL is also available via the Internet at
   9  * http://www.illumos.org/license/CDDL.
  10  */
  11 
  12 /*
  13  * Copyright 2023 Oxide Computer Company
  14  */
  15 
  16 /*
  17  * This test verifies the following:
  18  *
  19  *   o xregs and fpregs report the same content for the xmm registers at least.
  20  *   o A write to xregs is reflected in reads of fpregs.
  21  *   o A write to the fpregs is reflected in reads of xregs and doesn't
  22  *     clobber additional state in xregs.
  23  *   o A thread in our victim process sees the final state here and can print
  24  *     that out.
  25  *   o As a side effect it makes sure that libproc isn't incorrectly caching
  26  *     register info on handles.
  27  *
  28  * We use the xsu_dump process of the same bitness as us.
  29  */
  30 
  31 #include <err.h>
  32 #include <stdlib.h>
  33 #include <errno.h>
  34 #include <string.h>
  35 
  36 #include "xsave_util.h"
  37 
  38 static xsu_fpu_t fpu;
  39 
  40 int
  41 main(int argc, char *argv[])
  42 {
  43         uint32_t seed, hwsup;
  44         unsigned long ul;
  45         char *eptr;
  46         prxregset_t *prx, *cmp_prx;
  47         size_t prx_len, cmp_prx_len;
  48         xsu_proc_t xp;
  49         fpregset_t fpr;
  50 
  51         if (argc != 5) {
  52                 errx(EXIT_FAILURE, "missing args: <prog> <output file> "
  53                     "<seed> <func>");
  54         }
  55 
  56         errno = 0;
  57         ul = strtoul(argv[3], &eptr, 0);
  58         if (errno != 0 || *eptr != '\0') {
  59                 errx(EXIT_FAILURE, "seed value is bad: %s", argv[3]);
  60         }
  61 
  62 #if defined(_LP64)
  63         if (ul > UINT32_MAX) {
  64                 errx(EXIT_FAILURE, "seed %s, exceeds [0, UINT32_MAX]", argv[3]);
  65         }
  66 #endif
  67 
  68         seed = (uint32_t)ul;
  69         hwsup = xsu_hwsupport();
  70         xsu_fill(&fpu, hwsup, seed);
  71         xsu_fpu_to_xregs(&fpu, hwsup, &prx, &prx_len);
  72 
  73         (void) memset(&xp, 0, sizeof (xsu_proc_t));
  74         xp.xp_prog = argv[1];
  75         xp.xp_arg = argv[2];
  76         xp.xp_object = "a.out";
  77         xp.xp_symname = argv[4];
  78         xsu_proc_bkpt(&xp);
  79 
  80         /*
  81          * First get the xregs into a reasonable place.
  82          */
  83         if (Plwp_setxregs(xp.xp_proc, 1, prx, prx_len) != 0) {
  84                 err(EXIT_FAILURE, "failed to set target's xregs");
  85         }
  86 
  87         /*
  88          * Now that we have that, let's go and get the fpregs. Because of
  89          * differences between the 32-bit representation and the xsave state in
  90          * the xregs, we stick to different checking in an ILP32 vs. LP64 pieces
  91          * of this.
  92          */
  93         if (Plwp_getfpregs(xp.xp_proc, 1, &fpr) != 0) {
  94                 err(EXIT_FAILURE, "failed to get the fp registers");
  95         }
  96 
  97         if (!xsu_fpregs_cmp(&fpr, prx)) {
  98                 errx(EXIT_FAILURE, "fpregs do not reflect xsave changes!");
  99         }
 100         (void) printf("TEST PASSED: fpregs read respects xregs write\n");
 101 
 102         /*
 103          * Override the xmm registers with the known variant of the seed and set
 104          * that. Update the xregs data so we can later compare them usefully.
 105          */
 106         xsu_fpregset_xmm_set(&fpr, seed + INT32_MAX);
 107         xsu_xregs_xmm_set(prx, seed + INT32_MAX);
 108         if (Plwp_setfpregs(xp.xp_proc, 1, &fpr) != 0) {
 109                 err(EXIT_FAILURE, "failed to set fpregs");
 110         }
 111 
 112         if (Plwp_getxregs(xp.xp_proc, 1, &cmp_prx, &cmp_prx_len) != 0) {
 113                 err(EXIT_FAILURE, "failed to get comparison xregs");
 114         }
 115 
 116         if (!xsu_fpregs_cmp(&fpr, cmp_prx)) {
 117                 errx(EXIT_FAILURE, "fpregs do not reflect xsave changes!");
 118         }
 119         (void) printf("TEST PASSED: xregs read respects fpregs write\n");
 120 
 121         if (!xsu_xregs_comp_equal(prx, cmp_prx, PRX_INFO_YMM)) {
 122                 errx(EXIT_FAILURE, "%%ymm state changed across fpregs write");
 123         }
 124         (void) printf("TEST PASSED: fpregs did not change other xregs "
 125             "components\n");
 126 
 127         xsu_proc_finish(&xp);
 128 
 129         return (EXIT_SUCCESS);
 130 }