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 program pairs with the xregs_dump.32 and xregs_dump.64 program. It's 18 * main purpose is to use /proc to overwrite the FPU contents right before our 19 * target program calls xsu_getfpu(). 20 * 21 * To accomplish this we end up using libproc for some mischief via support 22 * routines. In particular we go through the following to logically accomplish 23 * this: 24 * 25 * o Generate the target FPU contents that we'll write (seeded from the CLI) 26 * o Explicitly create the process, which will be stopped. 27 * o Set it to be killed if we die. 28 * o Find the xsu_getfpu() symbol in the target and set a breakpoint. 29 * o Resume execution of the process. 30 * o When the break point hits, use libproc to set the FPU. 31 * o Delete the breakpoint and resume the process, which will print the FPU 32 * regs to a designated file. 33 * o Verify the process successfully terminates and returns 0. 34 * 35 * A critical assumption here is that our hardware support is not going to 36 * change between processes (something that may be mucked around with via 37 * environment variables for rtld). 38 */ 39 40 #include <err.h> 41 #include <stdlib.h> 42 #include <errno.h> 43 #include <sys/types.h> 44 #include <sys/wait.h> 45 #include <string.h> 46 47 #include "xsave_util.h" 48 49 static xsu_fpu_t fpu; 50 51 int 52 main(int argc, char *argv[]) 53 { 54 uint32_t seed, hwsup; 55 unsigned long ul; 56 char *eptr; 57 prxregset_t *prx; 58 size_t prx_len; 59 xsu_proc_t xp; 60 61 if (argc != 5) { 62 errx(EXIT_FAILURE, "missing args: <prog> <output file> " 63 "<seed> <func>"); 64 } 65 66 errno = 0; 67 ul = strtoul(argv[3], &eptr, 0); 68 if (errno != 0 || *eptr != '\0') { 69 errx(EXIT_FAILURE, "seed value is bad: %s", argv[3]); 70 } 71 72 #if defined(_LP64) 73 if (ul > UINT32_MAX) { 74 errx(EXIT_FAILURE, "seed %s, exceeds [0, UINT32_MAX]", argv[3]); 75 } 76 #endif 77 78 seed = (uint32_t)ul; 79 hwsup = xsu_hwsupport(); 80 xsu_fill(&fpu, hwsup, seed); 81 xsu_fpu_to_xregs(&fpu, hwsup, &prx, &prx_len); 82 83 (void) memset(&xp, 0, sizeof (xsu_proc_t)); 84 xp.xp_prog = argv[1]; 85 xp.xp_arg = argv[2]; 86 xp.xp_object = "a.out"; 87 xp.xp_symname = argv[4]; 88 89 xsu_proc_bkpt(&xp); 90 /* We know that libc always creates a default thread with id of 1 */ 91 if (Plwp_setxregs(xp.xp_proc, 1, prx, prx_len) != 0) { 92 err(EXIT_FAILURE, "failed to set target's xregs"); 93 } 94 95 xsu_proc_finish(&xp); 96 97 if (WEXITSTATUS(xp.xp_wait) != EXIT_SUCCESS) { 98 errx(EXIT_FAILURE, "our target process didn't exit non-zero, " 99 "got %d", WEXITSTATUS(xp.xp_wait)); 100 } 101 102 return (EXIT_SUCCESS); 103 } 104