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 ksft_set_plan(1); 80 81 pair.key = RISCV_HWPROBE_KEY_IMA_EXT_0; 82 rc = riscv_hwprobe(&pair, 1, 0, NULL, 0); 83 if (rc < 0) { 84 ksft_test_result_fail("hwprobe() failed with %ld\n", rc); 85 return -1; 86 } 87 88 if (pair.key != RISCV_HWPROBE_KEY_IMA_EXT_0) { 89 ksft_test_result_fail("hwprobe cannot probe RISCV_HWPROBE_KEY_IMA_EXT_0\n"); 90 return -2; 91 } 92 93 if (!(pair.value & RISCV_HWPROBE_EXT_ZVE32X)) { 94 rc = prctl(PR_RISCV_V_GET_CONTROL); 95 if (rc != -1 || errno != EINVAL) { 96 ksft_test_result_fail("GET_CONTROL should fail on kernel/hw without ZVE32X\n"); 97 return -3; 98 } 99 100 rc = prctl(PR_RISCV_V_SET_CONTROL, PR_RISCV_V_VSTATE_CTRL_ON); 101 if (rc != -1 || errno != EINVAL) { 102 ksft_test_result_fail("SET_CONTROL should fail on kernel/hw without ZVE32X\n"); 103 return -4; 104 } 105 106 ksft_test_result_skip("Vector not supported\n"); 107 return 0; 108 } 109 110 flag = PR_RISCV_V_VSTATE_CTRL_ON; 111 rc = prctl(PR_RISCV_V_SET_CONTROL, flag); 112 if (rc != 0) { 113 ksft_test_result_fail("Enabling V for current should always success\n"); 114 return -5; 115 } 116 117 flag = PR_RISCV_V_VSTATE_CTRL_OFF; 118 rc = prctl(PR_RISCV_V_SET_CONTROL, flag); 119 if (rc != -1 || errno != EPERM) { 120 ksft_test_result_fail("Disabling current's V alive must fail with EPERM(%d)\n", 121 errno); 122 return -5; 123 } 124 125 /* Turn on next's vector explicitly and test */ 126 flag = PR_RISCV_V_VSTATE_CTRL_ON << PR_RISCV_V_VSTATE_CTRL_NEXT_SHIFT; 127 if (test_and_compare_child(flag, PR_RISCV_V_VSTATE_CTRL_ON, 0)) 128 return -6; 129 130 /* Turn off next's vector explicitly and test */ 131 flag = PR_RISCV_V_VSTATE_CTRL_OFF << PR_RISCV_V_VSTATE_CTRL_NEXT_SHIFT; 132 if (test_and_compare_child(flag, PR_RISCV_V_VSTATE_CTRL_OFF, 0)) 133 return -7; 134 135 /* Turn on next's vector explicitly and test inherit */ 136 flag = PR_RISCV_V_VSTATE_CTRL_ON << PR_RISCV_V_VSTATE_CTRL_NEXT_SHIFT; 137 flag |= PR_RISCV_V_VSTATE_CTRL_INHERIT; 138 expected = flag | PR_RISCV_V_VSTATE_CTRL_ON; 139 if (test_and_compare_child(flag, expected, 0)) 140 return -8; 141 142 if (test_and_compare_child(flag, expected, 1)) 143 return -9; 144 145 /* Turn off next's vector explicitly and test inherit */ 146 flag = PR_RISCV_V_VSTATE_CTRL_OFF << PR_RISCV_V_VSTATE_CTRL_NEXT_SHIFT; 147 flag |= PR_RISCV_V_VSTATE_CTRL_INHERIT; 148 expected = flag | PR_RISCV_V_VSTATE_CTRL_OFF; 149 if (test_and_compare_child(flag, expected, 0)) 150 return -10; 151 152 if (test_and_compare_child(flag, expected, 1)) 153 return -11; 154 155 /* arguments should fail with EINVAL */ 156 rc = prctl(PR_RISCV_V_SET_CONTROL, 0xff0); 157 if (rc != -1 || errno != EINVAL) { 158 ksft_test_result_fail("Undefined control argument should return EINVAL\n"); 159 return -12; 160 } 161 162 rc = prctl(PR_RISCV_V_SET_CONTROL, 0x3); 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, 0xc); 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 ksft_test_result_pass("tests for riscv_v_vstate_ctrl pass\n"); 181 ksft_exit_pass(); 182 return 0; 183 } 184