1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <sys/prctl.h>
3 #include <unistd.h>
4 #include <asm/hwprobe.h>
5 #include <errno.h>
6 #include <sys/wait.h>
7 
8 #include "../../kselftest.h"
9 
10 /*
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
13  * contain the call.
14  */
15 long riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count,
16 		   size_t cpu_count, unsigned long *cpus, unsigned int flags);
17 
18 #define NEXT_PROGRAM "./vstate_exec_nolibc"
19 static int launch_test(int test_inherit)
20 {
21 	char *exec_argv[3], *exec_envp[1];
22 	int rc, pid, status;
23 
24 	pid = fork();
25 	if (pid < 0) {
26 		ksft_test_result_fail("fork failed %d", pid);
27 		return -1;
28 	}
29 
30 	if (!pid) {
31 		exec_argv[0] = NEXT_PROGRAM;
32 		exec_argv[1] = test_inherit != 0 ? "x" : NULL;
33 		exec_argv[2] = NULL;
34 		exec_envp[0] = NULL;
35 		/* launch the program again to check inherit */
36 		rc = execve(NEXT_PROGRAM, exec_argv, exec_envp);
37 		if (rc) {
38 			perror("execve");
39 			ksft_test_result_fail("child execve failed %d\n", rc);
40 			exit(-1);
41 		}
42 	}
43 
44 	rc = waitpid(-1, &status, 0);
45 	if (rc < 0) {
46 		ksft_test_result_fail("waitpid failed\n");
47 		return -3;
48 	}
49 
50 	if ((WIFEXITED(status) && WEXITSTATUS(status) == -1) ||
51 	    WIFSIGNALED(status)) {
52 		ksft_test_result_fail("child exited abnormally\n");
53 		return -4;
54 	}
55 
56 	return WEXITSTATUS(status);
57 }
58 
59 int test_and_compare_child(long provided, long expected, int inherit)
60 {
61 	int rc;
62 
63 	rc = prctl(PR_RISCV_V_SET_CONTROL, provided);
64 	if (rc != 0) {
65 		ksft_test_result_fail("prctl with provided arg %lx failed with code %d\n",
66 				      provided, rc);
67 		return -1;
68 	}
69 	rc = launch_test(inherit);
70 	if (rc != expected) {
71 		ksft_test_result_fail("Test failed, check %d != %d\n", rc,
72 				      expected);
73 		return -2;
74 	}
75 	return 0;
76 }
77 
78 #define PR_RISCV_V_VSTATE_CTRL_CUR_SHIFT	0
79 #define PR_RISCV_V_VSTATE_CTRL_NEXT_SHIFT	2
80 
81 int main(void)
82 {
83 	struct riscv_hwprobe pair;
84 	long flag, expected;
85 	long rc;
86 
87 	pair.key = RISCV_HWPROBE_KEY_IMA_EXT_0;
88 	rc = riscv_hwprobe(&pair, 1, 0, NULL, 0);
89 	if (rc < 0) {
90 		ksft_test_result_fail("hwprobe() failed with %d\n", rc);
91 		return -1;
92 	}
93 
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");
96 		return -2;
97 	}
98 
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");
103 			return -3;
104 		}
105 
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");
109 			return -4;
110 		}
111 
112 		ksft_test_result_skip("Vector not supported\n");
113 		return 0;
114 	}
115 
116 	flag = PR_RISCV_V_VSTATE_CTRL_ON;
117 	rc = prctl(PR_RISCV_V_SET_CONTROL, flag);
118 	if (rc != 0) {
119 		ksft_test_result_fail("Enabling V for current should always success\n");
120 		return -5;
121 	}
122 
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",
127 				      errno);
128 		return -5;
129 	}
130 
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))
134 		return -6;
135 
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))
139 		return -7;
140 
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))
146 		return -8;
147 
148 	if (test_and_compare_child(flag, expected, 1))
149 		return -9;
150 
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))
156 		return -10;
157 
158 	if (test_and_compare_child(flag, expected, 1))
159 		return -11;
160 
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");
165 		return -12;
166 	}
167 
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");
171 		return -12;
172 	}
173 
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");
177 		return -12;
178 	}
179 
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");
183 		return -12;
184 	}
185 
186 	ksft_test_result_pass("tests for riscv_v_vstate_ctrl pass\n");
187 	ksft_exit_pass();
188 	return 0;
189 }
190