#include <err.h>
#include <stdlib.h>
#include <ucontext.h>
#include <limits.h>
#include <errno.h>
#include "xsave_util.h"
static xsu_fpu_t mk_fpu, orig_fpu, found;
static ucontext_t *mk_ctx, *orig_ctx;
static volatile int estatus = EXIT_SUCCESS;
static void
mkctx_target(uint32_t hwsup, uint32_t *testp)
{
xsu_getfpu(&found, hwsup);
if (!xsu_same(&mk_fpu, &found, hwsup)) {
estatus = EXIT_FAILURE;
warnx("TEST FAILED: initial swap had bad FPU");
} else {
(void) printf("TEST PASSED: initial swap had correct FPU\n");
}
(void) swapcontext_extd(mk_ctx, 0, orig_ctx);
err(EXIT_FAILURE, "swapcontext_extd() back failed");
}
static void
mkctx_failure(void)
{
errx(EXIT_FAILURE, "swapcontext_extd() called failure func");
}
int
main(void)
{
uint32_t hwsup = xsu_hwsupport();
uint32_t start = arc4random();
xsu_fill(&mk_fpu, hwsup, start);
xsu_fill(&orig_fpu, hwsup, start + INT_MAX);
mk_ctx = ucontext_alloc(0);
if (mk_ctx == NULL)
err(EXIT_FAILURE, "failed to allocate extended ucontext_t");
orig_ctx = ucontext_alloc(0);
if (orig_ctx == NULL)
err(EXIT_FAILURE, "failed to allocate extended ucontext_t");
xsu_setfpu(&mk_fpu, hwsup);
if (getcontext_extd(mk_ctx, 0) != 0) {
errx(EXIT_FAILURE, "failed to get initial extended context for "
"makecontext");
}
xsu_ustack_alloc(mk_ctx);
makecontext(mk_ctx, mkctx_target, 2, hwsup, &hwsup);
xsu_setfpu(&orig_fpu, hwsup);
if (swapcontext_extd(orig_ctx, 0, mk_ctx) != 0) {
err(EXIT_FAILURE, "failed to swap contexts");
}
xsu_getfpu(&found, hwsup);
if (!xsu_same(&orig_fpu, &found, hwsup)) {
estatus = EXIT_FAILURE;
warnx("TEST FAILED: swap back did not have the right FPU");
} else {
(void) printf("TEST PASSED: swap back had the correct FPU\n");
}
xsu_ustack_alloc(mk_ctx);
makecontext(mk_ctx, mkctx_failure, 0);
if (swapcontext_extd(orig_ctx, 23, mk_ctx) != -1) {
errx(EXIT_FAILURE, "somehow got back from error test "
"swapcontext_extd() with bad func");
}
if (errno != EINVAL) {
estatus = EXIT_FAILURE;
warnx("TEST FAILED: swapcontext_extd() with bad flags had "
"errno %d expected EINVAL", errno);
} else {
(void) printf("TEST PASSED: swapcontext_extd() with bad flags "
"has correct errno (EINVAL)\n");
}
return (estatus);
}