1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <sys/prctl.h>
3 #include <unistd.h>
4 #include <errno.h>
5 #include <sys/wait.h>
6 
7 #include "../hwprobe/hwprobe.h"
8 #include "../../kselftest.h"
9 
10 #define NEXT_PROGRAM "./vstate_exec_nolibc"
11 static int launch_test(int test_inherit)
12 {
13 	char *exec_argv[3], *exec_envp[1];
14 	int rc, pid, status;
15 
16 	pid = fork();
17 	if (pid < 0) {
18 		ksft_test_result_fail("fork failed %d", pid);
19 		return -1;
20 	}
21 
22 	if (!pid) {
23 		exec_argv[0] = NEXT_PROGRAM;
24 		exec_argv[1] = test_inherit != 0 ? "x" : NULL;
25 		exec_argv[2] = NULL;
26 		exec_envp[0] = NULL;
27 		/* launch the program again to check inherit */
28 		rc = execve(NEXT_PROGRAM, exec_argv, exec_envp);
29 		if (rc) {
30 			perror("execve");
31 			ksft_test_result_fail("child execve failed %d\n", rc);
32 			exit(-1);
33 		}
34 	}
35 
36 	rc = waitpid(-1, &status, 0);
37 	if (rc < 0) {
38 		ksft_test_result_fail("waitpid failed\n");
39 		return -3;
40 	}
41 
42 	if ((WIFEXITED(status) && WEXITSTATUS(status) == -1) ||
43 	    WIFSIGNALED(status)) {
44 		ksft_test_result_fail("child exited abnormally\n");
45 		return -4;
46 	}
47 
48 	return WEXITSTATUS(status);
49 }
50 
51 int test_and_compare_child(long provided, long expected, int inherit)
52 {
53 	int rc;
54 
55 	rc = prctl(PR_RISCV_V_SET_CONTROL, provided);
56 	if (rc != 0) {
57 		ksft_test_result_fail("prctl with provided arg %lx failed with code %d\n",
58 				      provided, rc);
59 		return -1;
60 	}
61 	rc = launch_test(inherit);
62 	if (rc != expected) {
63 		ksft_test_result_fail("Test failed, check %d != %ld\n", rc,
64 				      expected);
65 		return -2;
66 	}
67 	return 0;
68 }
69 
70 #define PR_RISCV_V_VSTATE_CTRL_CUR_SHIFT	0
71 #define PR_RISCV_V_VSTATE_CTRL_NEXT_SHIFT	2
72 
73 int main(void)
74 {
75 	struct riscv_hwprobe pair;
76 	long flag, expected;
77 	long rc;
78 
79 	pair.key = RISCV_HWPROBE_KEY_IMA_EXT_0;
80 	rc = riscv_hwprobe(&pair, 1, 0, NULL, 0);
81 	if (rc < 0) {
82 		ksft_test_result_fail("hwprobe() failed with %ld\n", rc);
83 		return -1;
84 	}
85 
86 	if (pair.key != RISCV_HWPROBE_KEY_IMA_EXT_0) {
87 		ksft_test_result_fail("hwprobe cannot probe RISCV_HWPROBE_KEY_IMA_EXT_0\n");
88 		return -2;
89 	}
90 
91 	if (!(pair.value & RISCV_HWPROBE_IMA_V)) {
92 		rc = prctl(PR_RISCV_V_GET_CONTROL);
93 		if (rc != -1 || errno != EINVAL) {
94 			ksft_test_result_fail("GET_CONTROL should fail on kernel/hw without V\n");
95 			return -3;
96 		}
97 
98 		rc = prctl(PR_RISCV_V_SET_CONTROL, PR_RISCV_V_VSTATE_CTRL_ON);
99 		if (rc != -1 || errno != EINVAL) {
100 			ksft_test_result_fail("GET_CONTROL should fail on kernel/hw without V\n");
101 			return -4;
102 		}
103 
104 		ksft_test_result_skip("Vector not supported\n");
105 		return 0;
106 	}
107 
108 	flag = PR_RISCV_V_VSTATE_CTRL_ON;
109 	rc = prctl(PR_RISCV_V_SET_CONTROL, flag);
110 	if (rc != 0) {
111 		ksft_test_result_fail("Enabling V for current should always success\n");
112 		return -5;
113 	}
114 
115 	flag = PR_RISCV_V_VSTATE_CTRL_OFF;
116 	rc = prctl(PR_RISCV_V_SET_CONTROL, flag);
117 	if (rc != -1 || errno != EPERM) {
118 		ksft_test_result_fail("Disabling current's V alive must fail with EPERM(%d)\n",
119 				      errno);
120 		return -5;
121 	}
122 
123 	/* Turn on next's vector explicitly and test */
124 	flag = PR_RISCV_V_VSTATE_CTRL_ON << PR_RISCV_V_VSTATE_CTRL_NEXT_SHIFT;
125 	if (test_and_compare_child(flag, PR_RISCV_V_VSTATE_CTRL_ON, 0))
126 		return -6;
127 
128 	/* Turn off next's vector explicitly and test */
129 	flag = PR_RISCV_V_VSTATE_CTRL_OFF << PR_RISCV_V_VSTATE_CTRL_NEXT_SHIFT;
130 	if (test_and_compare_child(flag, PR_RISCV_V_VSTATE_CTRL_OFF, 0))
131 		return -7;
132 
133 	/* Turn on next's vector explicitly and test inherit */
134 	flag = PR_RISCV_V_VSTATE_CTRL_ON << PR_RISCV_V_VSTATE_CTRL_NEXT_SHIFT;
135 	flag |= PR_RISCV_V_VSTATE_CTRL_INHERIT;
136 	expected = flag | PR_RISCV_V_VSTATE_CTRL_ON;
137 	if (test_and_compare_child(flag, expected, 0))
138 		return -8;
139 
140 	if (test_and_compare_child(flag, expected, 1))
141 		return -9;
142 
143 	/* Turn off next's vector explicitly and test inherit */
144 	flag = PR_RISCV_V_VSTATE_CTRL_OFF << PR_RISCV_V_VSTATE_CTRL_NEXT_SHIFT;
145 	flag |= PR_RISCV_V_VSTATE_CTRL_INHERIT;
146 	expected = flag | PR_RISCV_V_VSTATE_CTRL_OFF;
147 	if (test_and_compare_child(flag, expected, 0))
148 		return -10;
149 
150 	if (test_and_compare_child(flag, expected, 1))
151 		return -11;
152 
153 	/* arguments should fail with EINVAL */
154 	rc = prctl(PR_RISCV_V_SET_CONTROL, 0xff0);
155 	if (rc != -1 || errno != EINVAL) {
156 		ksft_test_result_fail("Undefined control argument should return EINVAL\n");
157 		return -12;
158 	}
159 
160 	rc = prctl(PR_RISCV_V_SET_CONTROL, 0x3);
161 	if (rc != -1 || errno != EINVAL) {
162 		ksft_test_result_fail("Undefined control argument should return EINVAL\n");
163 		return -12;
164 	}
165 
166 	rc = prctl(PR_RISCV_V_SET_CONTROL, 0xc);
167 	if (rc != -1 || errno != EINVAL) {
168 		ksft_test_result_fail("Undefined control argument should return EINVAL\n");
169 		return -12;
170 	}
171 
172 	rc = prctl(PR_RISCV_V_SET_CONTROL, 0xc);
173 	if (rc != -1 || errno != EINVAL) {
174 		ksft_test_result_fail("Undefined control argument should return EINVAL\n");
175 		return -12;
176 	}
177 
178 	ksft_test_result_pass("tests for riscv_v_vstate_ctrl pass\n");
179 	ksft_exit_pass();
180 	return 0;
181 }
182