1 // SPDX-License-Identifier: GPL-2.0-only
4 #include <asm/hwprobe.h>
8 #include "../../kselftest.h"
11 * Rather than relying on having a new enough libc to define this, just do it
12 * ourselves. This way we don't need to be coupled to a new-enough libc to
15 long riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count,
16 size_t cpu_count, unsigned long *cpus, unsigned int flags);
18 #define NEXT_PROGRAM "./vstate_exec_nolibc"
19 static int launch_test(int test_inherit)
21 char *exec_argv[3], *exec_envp[1];
26 ksft_test_result_fail("fork failed %d", pid);
31 exec_argv[0] = NEXT_PROGRAM;
32 exec_argv[1] = test_inherit != 0 ? "x" : NULL;
35 /* launch the program again to check inherit */
36 rc = execve(NEXT_PROGRAM, exec_argv, exec_envp);
39 ksft_test_result_fail("child execve failed %d\n", rc);
44 rc = waitpid(-1, &status, 0);
46 ksft_test_result_fail("waitpid failed\n");
50 if ((WIFEXITED(status) && WEXITSTATUS(status) == -1) ||
51 WIFSIGNALED(status)) {
52 ksft_test_result_fail("child exited abnormally\n");
56 return WEXITSTATUS(status);
59 int test_and_compare_child(long provided, long expected, int inherit)
63 rc = prctl(PR_RISCV_V_SET_CONTROL, provided);
65 ksft_test_result_fail("prctl with provided arg %lx failed with code %d\n",
69 rc = launch_test(inherit);
71 ksft_test_result_fail("Test failed, check %d != %d\n", rc,
78 #define PR_RISCV_V_VSTATE_CTRL_CUR_SHIFT 0
79 #define PR_RISCV_V_VSTATE_CTRL_NEXT_SHIFT 2
83 struct riscv_hwprobe pair;
87 pair.key = RISCV_HWPROBE_KEY_IMA_EXT_0;
88 rc = riscv_hwprobe(&pair, 1, 0, NULL, 0);
90 ksft_test_result_fail("hwprobe() failed with %d\n", rc);
94 if (pair.key != RISCV_HWPROBE_KEY_IMA_EXT_0) {
95 ksft_test_result_fail("hwprobe cannot probe RISCV_HWPROBE_KEY_IMA_EXT_0\n");
99 if (!(pair.value & RISCV_HWPROBE_IMA_V)) {
100 rc = prctl(PR_RISCV_V_GET_CONTROL);
101 if (rc != -1 || errno != EINVAL) {
102 ksft_test_result_fail("GET_CONTROL should fail on kernel/hw without V\n");
106 rc = prctl(PR_RISCV_V_SET_CONTROL, PR_RISCV_V_VSTATE_CTRL_ON);
107 if (rc != -1 || errno != EINVAL) {
108 ksft_test_result_fail("GET_CONTROL should fail on kernel/hw without V\n");
112 ksft_test_result_skip("Vector not supported\n");
116 flag = PR_RISCV_V_VSTATE_CTRL_ON;
117 rc = prctl(PR_RISCV_V_SET_CONTROL, flag);
119 ksft_test_result_fail("Enabling V for current should always success\n");
123 flag = PR_RISCV_V_VSTATE_CTRL_OFF;
124 rc = prctl(PR_RISCV_V_SET_CONTROL, flag);
125 if (rc != -1 || errno != EPERM) {
126 ksft_test_result_fail("Disabling current's V alive must fail with EPERM(%d)\n",
131 /* Turn on next's vector explicitly and test */
132 flag = PR_RISCV_V_VSTATE_CTRL_ON << PR_RISCV_V_VSTATE_CTRL_NEXT_SHIFT;
133 if (test_and_compare_child(flag, PR_RISCV_V_VSTATE_CTRL_ON, 0))
136 /* Turn off next's vector explicitly and test */
137 flag = PR_RISCV_V_VSTATE_CTRL_OFF << PR_RISCV_V_VSTATE_CTRL_NEXT_SHIFT;
138 if (test_and_compare_child(flag, PR_RISCV_V_VSTATE_CTRL_OFF, 0))
141 /* Turn on next's vector explicitly and test inherit */
142 flag = PR_RISCV_V_VSTATE_CTRL_ON << PR_RISCV_V_VSTATE_CTRL_NEXT_SHIFT;
143 flag |= PR_RISCV_V_VSTATE_CTRL_INHERIT;
144 expected = flag | PR_RISCV_V_VSTATE_CTRL_ON;
145 if (test_and_compare_child(flag, expected, 0))
148 if (test_and_compare_child(flag, expected, 1))
151 /* Turn off next's vector explicitly and test inherit */
152 flag = PR_RISCV_V_VSTATE_CTRL_OFF << PR_RISCV_V_VSTATE_CTRL_NEXT_SHIFT;
153 flag |= PR_RISCV_V_VSTATE_CTRL_INHERIT;
154 expected = flag | PR_RISCV_V_VSTATE_CTRL_OFF;
155 if (test_and_compare_child(flag, expected, 0))
158 if (test_and_compare_child(flag, expected, 1))
161 /* arguments should fail with EINVAL */
162 rc = prctl(PR_RISCV_V_SET_CONTROL, 0xff0);
163 if (rc != -1 || errno != EINVAL) {
164 ksft_test_result_fail("Undefined control argument should return EINVAL\n");
168 rc = prctl(PR_RISCV_V_SET_CONTROL, 0x3);
169 if (rc != -1 || errno != EINVAL) {
170 ksft_test_result_fail("Undefined control argument should return EINVAL\n");
174 rc = prctl(PR_RISCV_V_SET_CONTROL, 0xc);
175 if (rc != -1 || errno != EINVAL) {
176 ksft_test_result_fail("Undefined control argument should return EINVAL\n");
180 rc = prctl(PR_RISCV_V_SET_CONTROL, 0xc);
181 if (rc != -1 || errno != EINVAL) {
182 ksft_test_result_fail("Undefined control argument should return EINVAL\n");
186 ksft_test_result_pass("tests for riscv_v_vstate_ctrl pass\n");