vstate_prctl.c 4.7 KB

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