1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2015-2019 ARM Limited.
4 * Original author: Dave Martin <Dave.Martin@arm.com>
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;
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
)
42 program_name
= strrchr(argv
[0], '/');
46 program_name
= argv
[0];
48 while ((c
= getopt_long(argc
, argv
, "Mfhi", options
, NULL
)) != -1)
50 case 'M': vl
= SVE_VL_MAX
; break;
51 case 'f': force
= 1; break;
52 case 'i': inherit
= 1; break;
57 if (inherit
&& no_inherit
)
66 vl
= strtoul(argv
[optind
], &rest
, 0);
71 if (vl
== ULONG_MAX
&& errno
) {
72 fprintf(stderr
, "%s: %s: %s\n",
73 program_name
, argv
[optind
], strerror(errno
));
88 "Usage: %s [-f | --force] "
89 "[-i | --inherit | --no-inherit] "
90 "{-M | --max | <vector length>} "
91 "<command> [<arguments> ...]\n",
96 int main(int argc
, char **argv
)
98 int ret
= 126; /* same as sh(1) command-not-executable error */
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",
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",
119 fputs("Going ahead anyway (--force): "
120 "This is a debug option. Don't rely on it.\n",
124 flags
= PR_SVE_SET_VL_ONEXEC
;
126 flags
|= PR_SVE_VL_INHERIT
;
128 t
= prctl(PR_SVE_SET_VL
, vl
| flags
);
130 fprintf(stderr
, "%s: PR_SVE_SET_VL: %s\n",
131 program_name
, strerror(errno
));
135 t
= prctl(PR_SVE_GET_VL
);
137 fprintf(stderr
, "%s: PR_SVE_GET_VL: %s\n",
138 program_name
, strerror(errno
));
141 flags
= PR_SVE_VL_LEN_MASK
;
144 assert(optind
< argc
);
147 execvp(path
, &argv
[optind
]);
150 ret
= 127; /* same as sh(1) not-found error */
151 fprintf(stderr
, "%s: %s: %s\n", program_name
, path
, strerror(e
));
154 return ret
; /* same as sh(1) not-executable error */