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
;
25 static int set_ctl
= PR_SVE_SET_VL
;
26 static int get_ctl
= PR_SVE_GET_VL
;
28 static const struct option options
[] = {
29 { "force", no_argument
, NULL
, 'f' },
30 { "inherit", no_argument
, NULL
, 'i' },
31 { "max", no_argument
, NULL
, 'M' },
32 { "no-inherit", no_argument
, &no_inherit
, 1 },
33 { "sme", no_argument
, NULL
, 's' },
34 { "help", no_argument
, NULL
, '?' },
38 static char const *program_name
;
40 static int parse_options(int argc
, char **argv
)
45 program_name
= strrchr(argv
[0], '/');
49 program_name
= argv
[0];
51 while ((c
= getopt_long(argc
, argv
, "Mfhi", options
, NULL
)) != -1)
53 case 'M': vl
= SVE_VL_MAX
; break;
54 case 'f': force
= 1; break;
55 case 'i': inherit
= 1; break;
56 case 's': set_ctl
= PR_SME_SET_VL
;
57 get_ctl
= PR_SME_GET_VL
;
63 if (inherit
&& no_inherit
)
72 vl
= strtoul(argv
[optind
], &rest
, 0);
77 if (vl
== ULONG_MAX
&& errno
) {
78 fprintf(stderr
, "%s: %s: %s\n",
79 program_name
, argv
[optind
], strerror(errno
));
94 "Usage: %s [-f | --force] "
95 "[-i | --inherit | --no-inherit] "
96 "{-M | --max | <vector length>} "
97 "<command> [<arguments> ...]\n",
102 int main(int argc
, char **argv
)
104 int ret
= 126; /* same as sh(1) command-not-executable error */
109 if (parse_options(argc
, argv
))
110 return 2; /* same as sh(1) builtin incorrect-usage */
112 if (vl
& ~(vl
& PR_SVE_VL_LEN_MASK
)) {
113 fprintf(stderr
, "%s: Invalid vector length %lu\n",
115 return 2; /* same as sh(1) builtin incorrect-usage */
118 if (!(getauxval(AT_HWCAP
) & HWCAP_SVE
)) {
119 fprintf(stderr
, "%s: Scalable Vector Extension not present\n",
125 fputs("Going ahead anyway (--force): "
126 "This is a debug option. Don't rely on it.\n",
130 flags
= PR_SVE_SET_VL_ONEXEC
;
132 flags
|= PR_SVE_VL_INHERIT
;
134 t
= prctl(set_ctl
, vl
| flags
);
136 fprintf(stderr
, "%s: PR_SVE_SET_VL: %s\n",
137 program_name
, strerror(errno
));
143 fprintf(stderr
, "%s: PR_SVE_GET_VL: %s\n",
144 program_name
, strerror(errno
));
147 flags
= PR_SVE_VL_LEN_MASK
;
150 assert(optind
< argc
);
153 execvp(path
, &argv
[optind
]);
156 ret
= 127; /* same as sh(1) not-found error */
157 fprintf(stderr
, "%s: %s: %s\n", program_name
, path
, strerror(e
));
160 return ret
; /* same as sh(1) not-executable error */