~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/riscv/vector/vstate_prctl.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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_EXT_ZVE32X)) {
 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 ZVE32X\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("SET_CONTROL should fail on kernel/hw without ZVE32X\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 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php