WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / arm64 / fp / vlset.c
blob308d27a68226987cc7063b2479976bcb5d7a144d
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2015-2019 ARM Limited.
4 * Original author: Dave Martin <Dave.Martin@arm.com>
5 */
6 #define _GNU_SOURCE
7 #include <assert.h>
8 #include <errno.h>
9 #include <limits.h>
10 #include <stddef.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <getopt.h>
15 #include <unistd.h>
16 #include <sys/auxv.h>
17 #include <sys/prctl.h>
18 #include <asm/hwcap.h>
19 #include <asm/sigcontext.h>
21 static int inherit = 0;
22 static int no_inherit = 0;
23 static int force = 0;
24 static unsigned long vl;
26 static const struct option options[] = {
27 { "force", no_argument, NULL, 'f' },
28 { "inherit", no_argument, NULL, 'i' },
29 { "max", no_argument, NULL, 'M' },
30 { "no-inherit", no_argument, &no_inherit, 1 },
31 { "help", no_argument, NULL, '?' },
35 static char const *program_name;
37 static int parse_options(int argc, char **argv)
39 int c;
40 char *rest;
42 program_name = strrchr(argv[0], '/');
43 if (program_name)
44 ++program_name;
45 else
46 program_name = argv[0];
48 while ((c = getopt_long(argc, argv, "Mfhi", options, NULL)) != -1)
49 switch (c) {
50 case 'M': vl = SVE_VL_MAX; break;
51 case 'f': force = 1; break;
52 case 'i': inherit = 1; break;
53 case 0: break;
54 default: goto error;
57 if (inherit && no_inherit)
58 goto error;
60 if (!vl) {
61 /* vector length */
62 if (optind >= argc)
63 goto error;
65 errno = 0;
66 vl = strtoul(argv[optind], &rest, 0);
67 if (*rest) {
68 vl = ULONG_MAX;
69 errno = EINVAL;
71 if (vl == ULONG_MAX && errno) {
72 fprintf(stderr, "%s: %s: %s\n",
73 program_name, argv[optind], strerror(errno));
74 goto error;
77 ++optind;
80 /* command */
81 if (optind >= argc)
82 goto error;
84 return 0;
86 error:
87 fprintf(stderr,
88 "Usage: %s [-f | --force] "
89 "[-i | --inherit | --no-inherit] "
90 "{-M | --max | <vector length>} "
91 "<command> [<arguments> ...]\n",
92 program_name);
93 return -1;
96 int main(int argc, char **argv)
98 int ret = 126; /* same as sh(1) command-not-executable error */
99 long flags;
100 char *path;
101 int t, e;
103 if (parse_options(argc, argv))
104 return 2; /* same as sh(1) builtin incorrect-usage */
106 if (vl & ~(vl & PR_SVE_VL_LEN_MASK)) {
107 fprintf(stderr, "%s: Invalid vector length %lu\n",
108 program_name, vl);
109 return 2; /* same as sh(1) builtin incorrect-usage */
112 if (!(getauxval(AT_HWCAP) & HWCAP_SVE)) {
113 fprintf(stderr, "%s: Scalable Vector Extension not present\n",
114 program_name);
116 if (!force)
117 goto error;
119 fputs("Going ahead anyway (--force): "
120 "This is a debug option. Don't rely on it.\n",
121 stderr);
124 flags = PR_SVE_SET_VL_ONEXEC;
125 if (inherit)
126 flags |= PR_SVE_VL_INHERIT;
128 t = prctl(PR_SVE_SET_VL, vl | flags);
129 if (t < 0) {
130 fprintf(stderr, "%s: PR_SVE_SET_VL: %s\n",
131 program_name, strerror(errno));
132 goto error;
135 t = prctl(PR_SVE_GET_VL);
136 if (t == -1) {
137 fprintf(stderr, "%s: PR_SVE_GET_VL: %s\n",
138 program_name, strerror(errno));
139 goto error;
141 flags = PR_SVE_VL_LEN_MASK;
142 flags = t & ~flags;
144 assert(optind < argc);
145 path = argv[optind];
147 execvp(path, &argv[optind]);
148 e = errno;
149 if (errno == ENOENT)
150 ret = 127; /* same as sh(1) not-found error */
151 fprintf(stderr, "%s: %s: %s\n", program_name, path, strerror(e));
153 error:
154 return ret; /* same as sh(1) not-executable error */