~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/powerpc/dexcr/chdexcr.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0-or-later
  2 
  3 #include <errno.h>
  4 #include <stddef.h>
  5 #include <stdio.h>
  6 #include <stdlib.h>
  7 #include <string.h>
  8 #include <sys/prctl.h>
  9 
 10 #include "dexcr.h"
 11 #include "utils.h"
 12 
 13 static void die(const char *msg)
 14 {
 15         printf("%s\n", msg);
 16         exit(1);
 17 }
 18 
 19 static void help(void)
 20 {
 21         printf("Invoke a provided program with a custom DEXCR on-exec reset value\n"
 22                "\n"
 23                "usage: chdexcr [CHDEXCR OPTIONS] -- PROGRAM [ARGS...]\n"
 24                "\n"
 25                "Each configurable DEXCR aspect is exposed as an option.\n"
 26                "\n"
 27                "The normal option sets the aspect in the DEXCR. The --no- variant\n"
 28                "clears that aspect. For example, --ibrtpd sets the IBRTPD aspect bit,\n"
 29                "so indirect branch prediction will be disabled in the provided program.\n"
 30                "Conversely, --no-ibrtpd clears the aspect bit, so indirect branch\n"
 31                "prediction may occur.\n"
 32                "\n"
 33                "CHDEXCR OPTIONS:\n");
 34 
 35         for (int i = 0; i < ARRAY_SIZE(aspects); i++) {
 36                 const struct dexcr_aspect *aspect = &aspects[i];
 37 
 38                 if (aspect->prctl == -1)
 39                         continue;
 40 
 41                 printf("  --%-6s / --no-%-6s : %s\n", aspect->opt, aspect->opt, aspect->desc);
 42         }
 43 }
 44 
 45 static const struct dexcr_aspect *opt_to_aspect(const char *opt)
 46 {
 47         for (int i = 0; i < ARRAY_SIZE(aspects); i++)
 48                 if (aspects[i].prctl != -1 && !strcmp(aspects[i].opt, opt))
 49                         return &aspects[i];
 50 
 51         return NULL;
 52 }
 53 
 54 static int apply_option(const char *option)
 55 {
 56         const struct dexcr_aspect *aspect;
 57         const char *opt = NULL;
 58         const char *set_prefix = "--";
 59         const char *clear_prefix = "--no-";
 60         unsigned long ctrl = 0;
 61         int err;
 62 
 63         if (!strcmp(option, "-h") || !strcmp(option, "--help")) {
 64                 help();
 65                 exit(0);
 66         }
 67 
 68         /* Strip out --(no-) prefix and determine ctrl value */
 69         if (!strncmp(option, clear_prefix, strlen(clear_prefix))) {
 70                 opt = &option[strlen(clear_prefix)];
 71                 ctrl |= PR_PPC_DEXCR_CTRL_CLEAR_ONEXEC;
 72         } else if (!strncmp(option, set_prefix, strlen(set_prefix))) {
 73                 opt = &option[strlen(set_prefix)];
 74                 ctrl |= PR_PPC_DEXCR_CTRL_SET_ONEXEC;
 75         }
 76 
 77         if (!opt || !*opt)
 78                 return 1;
 79 
 80         aspect = opt_to_aspect(opt);
 81         if (!aspect)
 82                 die("unknown aspect");
 83 
 84         err = pr_set_dexcr(aspect->prctl, ctrl);
 85         if (err)
 86                 die("failed to apply option");
 87 
 88         return 0;
 89 }
 90 
 91 int main(int argc, char *const argv[])
 92 {
 93         int i;
 94 
 95         if (!dexcr_exists())
 96                 die("DEXCR not detected on this hardware");
 97 
 98         for (i = 1; i < argc; i++)
 99                 if (apply_option(argv[i]))
100                         break;
101 
102         if (i < argc && !strcmp(argv[i], "--"))
103                 i++;
104 
105         if (i >= argc)
106                 die("missing command");
107 
108         execvp(argv[i], &argv[i]);
109         perror("execve");
110 
111         return errno;
112 }
113 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php