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 that have both extended and non-extended ucontext_t's 19 * and 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, uint32_t *testp) 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 xsu_ustack_alloc(mk_ctx); 84 makecontext(mk_ctx, mkctx_target, 2, hwsup, &hwsup); 85 xsu_setfpu(&orig_fpu, hwsup); 86 if (swapcontext_extd(orig_ctx, 0, mk_ctx) != 0) { 87 err(EXIT_FAILURE, "failed to swap contexts"); 88 } 89 xsu_getfpu(&found, hwsup); 90 if (!xsu_same(&orig_fpu, &found, hwsup)) { 91 estatus = EXIT_FAILURE; 92 warnx("TEST FAILED: swap back did not have the right FPU"); 93 } else { 94 (void) printf("TEST PASSED: swap back had the correct FPU\n"); 95 } 96 97 xsu_ustack_alloc(mk_ctx); 98 makecontext(mk_ctx, mkctx_failure, 0); 99 if (swapcontext_extd(orig_ctx, 23, mk_ctx) != -1) { 100 errx(EXIT_FAILURE, "somehow got back from error test " 101 "swapcontext_extd() with bad func"); 102 } 103 if (errno != EINVAL) { 104 estatus = EXIT_FAILURE; 105 warnx("TEST FAILED: swapcontext_extd() with bad flags had " 106 "errno %d expected EINVAL", errno); 107 } else { 108 (void) printf("TEST PASSED: swapcontext_extd() with bad flags " 109 "has correct errno (EINVAL)\n"); 110 } 111 112 return (estatus); 113 } 114