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 exercises the swapcontext_extd(2) functionality and verifies that
  18  * we can swap contexts tht have both extended and non-extended ucontext_t's and
  19  * that we can get at that state in both cases. This test relies on extended
  20  * states as we try to validate and ensure that they exist.
  21  */
  22 
  23 #include <err.h>
  24 #include <stdlib.h>
  25 #include <ucontext.h>
  26 #include <limits.h>
  27 #include <errno.h>
  28 
  29 #include "xsave_util.h"
  30 
  31 static xsu_fpu_t mk_fpu, orig_fpu, found;
  32 static ucontext_t *mk_ctx, *orig_ctx;
  33 static volatile int estatus = EXIT_SUCCESS;
  34 
  35 static void
  36 mkctx_target(uint32_t hwsup)
  37 {
  38         xsu_getfpu(&found, hwsup);
  39         if (!xsu_same(&mk_fpu, &found, hwsup)) {
  40                 estatus = EXIT_FAILURE;
  41                 warnx("TEST FAILED: initial swap had bad FPU");
  42         } else {
  43                 (void) printf("TEST PASSED: initial swap had correct FPU\n");
  44         }
  45 
  46         (void) swapcontext_extd(mk_ctx, 0, orig_ctx);
  47         err(EXIT_FAILURE, "swapcontext_extd() back failed");
  48 }
  49 
  50 static void
  51 mkctx_failure(void)
  52 {
  53         errx(EXIT_FAILURE, "swapcontext_extd() called failure func");
  54 }
  55 
  56 int
  57 main(void)
  58 {
  59         uint32_t hwsup = xsu_hwsupport();
  60         uint32_t start = arc4random();
  61 
  62         xsu_fill(&mk_fpu, hwsup, start);
  63         xsu_fill(&orig_fpu, hwsup, start + INT_MAX);
  64 
  65         mk_ctx = ucontext_alloc(0);
  66         if (mk_ctx == NULL)
  67                 err(EXIT_FAILURE, "failed to allocate extended ucontext_t");
  68         orig_ctx = ucontext_alloc(0);
  69         if (orig_ctx == NULL)
  70                 err(EXIT_FAILURE, "failed to allocate extended ucontext_t");
  71 
  72         /*
  73          * Set the FPU and snag our initial context. We'll use makecontext to
  74          * call this and then change our FPU, swap and start checking and then
  75          * swap back.
  76          */
  77         xsu_setfpu(&mk_fpu, hwsup);
  78         if (getcontext_extd(mk_ctx, 0) != 0) {
  79                 errx(EXIT_FAILURE, "failed to get initial extended context for "
  80                     "makecontext");
  81         }
  82 
  83         makecontext(mk_ctx, mkctx_target, 1, hwsup);
  84         xsu_setfpu(&orig_fpu, hwsup);
  85         if (swapcontext_extd(orig_ctx, 0, mk_ctx) != 0) {
  86                 err(EXIT_FAILURE, "failed to swap contexts");
  87         }
  88         xsu_getfpu(&found, hwsup);
  89         if (!xsu_same(&orig_fpu, &found, hwsup)) {
  90                 estatus = EXIT_FAILURE;
  91                 warnx("TEST FAILED: swap back did not have the right FPU");
  92         } else {
  93                 (void) printf("TEST PASSED: swap back had the correct FPU\n");
  94         }
  95 
  96         makecontext(mk_ctx, mkctx_failure, 0);
  97         if (swapcontext_extd(orig_ctx, 23, mk_ctx) != -1) {
  98                 errx(EXIT_FAILURE, "somehow got back from error test "
  99                     "swapcontext_extd() with bad func");
 100         }
 101         if (errno != EINVAL) {
 102                 estatus = EXIT_FAILURE;
 103                 warnx("TEST FAILED: swapcontext_extd() with bad flags had "
 104                     "errno %d expected EINVAL", errno);
 105         } else {
 106                 (void) printf("TEST PASSED: swapcontext_extd() with bad flags "
 107                     "has correct errno (EINVAL)\n");
 108         }
 109 
 110         return (estatus);
 111 }