za-ptrace.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2021 ARM Limited.
  4. */
  5. #include <errno.h>
  6. #include <stdbool.h>
  7. #include <stddef.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include <sys/auxv.h>
  13. #include <sys/prctl.h>
  14. #include <sys/ptrace.h>
  15. #include <sys/types.h>
  16. #include <sys/uio.h>
  17. #include <sys/wait.h>
  18. #include <asm/sigcontext.h>
  19. #include <asm/ptrace.h>
  20. #include "../../kselftest.h"
  21. /* <linux/elf.h> and <sys/auxv.h> don't like each other, so: */
  22. #ifndef NT_ARM_ZA
  23. #define NT_ARM_ZA 0x40c
  24. #endif
  25. #define EXPECTED_TESTS (((SVE_VQ_MAX - SVE_VQ_MIN) + 1) * 3)
  26. static void fill_buf(char *buf, size_t size)
  27. {
  28. int i;
  29. for (i = 0; i < size; i++)
  30. buf[i] = random();
  31. }
  32. static int do_child(void)
  33. {
  34. if (ptrace(PTRACE_TRACEME, -1, NULL, NULL))
  35. ksft_exit_fail_msg("PTRACE_TRACEME", strerror(errno));
  36. if (raise(SIGSTOP))
  37. ksft_exit_fail_msg("raise(SIGSTOP)", strerror(errno));
  38. return EXIT_SUCCESS;
  39. }
  40. static struct user_za_header *get_za(pid_t pid, void **buf, size_t *size)
  41. {
  42. struct user_za_header *za;
  43. void *p;
  44. size_t sz = sizeof(*za);
  45. struct iovec iov;
  46. while (1) {
  47. if (*size < sz) {
  48. p = realloc(*buf, sz);
  49. if (!p) {
  50. errno = ENOMEM;
  51. goto error;
  52. }
  53. *buf = p;
  54. *size = sz;
  55. }
  56. iov.iov_base = *buf;
  57. iov.iov_len = sz;
  58. if (ptrace(PTRACE_GETREGSET, pid, NT_ARM_ZA, &iov))
  59. goto error;
  60. za = *buf;
  61. if (za->size <= sz)
  62. break;
  63. sz = za->size;
  64. }
  65. return za;
  66. error:
  67. return NULL;
  68. }
  69. static int set_za(pid_t pid, const struct user_za_header *za)
  70. {
  71. struct iovec iov;
  72. iov.iov_base = (void *)za;
  73. iov.iov_len = za->size;
  74. return ptrace(PTRACE_SETREGSET, pid, NT_ARM_ZA, &iov);
  75. }
  76. /* Validate attempting to set the specfied VL via ptrace */
  77. static void ptrace_set_get_vl(pid_t child, unsigned int vl, bool *supported)
  78. {
  79. struct user_za_header za;
  80. struct user_za_header *new_za = NULL;
  81. size_t new_za_size = 0;
  82. int ret, prctl_vl;
  83. *supported = false;
  84. /* Check if the VL is supported in this process */
  85. prctl_vl = prctl(PR_SME_SET_VL, vl);
  86. if (prctl_vl == -1)
  87. ksft_exit_fail_msg("prctl(PR_SME_SET_VL) failed: %s (%d)\n",
  88. strerror(errno), errno);
  89. /* If the VL is not supported then a supported VL will be returned */
  90. *supported = (prctl_vl == vl);
  91. /* Set the VL by doing a set with no register payload */
  92. memset(&za, 0, sizeof(za));
  93. za.size = sizeof(za);
  94. za.vl = vl;
  95. ret = set_za(child, &za);
  96. if (ret != 0) {
  97. ksft_test_result_fail("Failed to set VL %u\n", vl);
  98. return;
  99. }
  100. /*
  101. * Read back the new register state and verify that we have the
  102. * same VL that we got from prctl() on ourselves.
  103. */
  104. if (!get_za(child, (void **)&new_za, &new_za_size)) {
  105. ksft_test_result_fail("Failed to read VL %u\n", vl);
  106. return;
  107. }
  108. ksft_test_result(new_za->vl = prctl_vl, "Set VL %u\n", vl);
  109. free(new_za);
  110. }
  111. /* Validate attempting to set no ZA data and read it back */
  112. static void ptrace_set_no_data(pid_t child, unsigned int vl)
  113. {
  114. void *read_buf = NULL;
  115. struct user_za_header write_za;
  116. struct user_za_header *read_za;
  117. size_t read_za_size = 0;
  118. int ret;
  119. /* Set up some data and write it out */
  120. memset(&write_za, 0, sizeof(write_za));
  121. write_za.size = ZA_PT_ZA_OFFSET;
  122. write_za.vl = vl;
  123. ret = set_za(child, &write_za);
  124. if (ret != 0) {
  125. ksft_test_result_fail("Failed to set VL %u no data\n", vl);
  126. return;
  127. }
  128. /* Read the data back */
  129. if (!get_za(child, (void **)&read_buf, &read_za_size)) {
  130. ksft_test_result_fail("Failed to read VL %u no data\n", vl);
  131. return;
  132. }
  133. read_za = read_buf;
  134. /* We might read more data if there's extensions we don't know */
  135. if (read_za->size < write_za.size) {
  136. ksft_test_result_fail("VL %u wrote %d bytes, only read %d\n",
  137. vl, write_za.size, read_za->size);
  138. goto out_read;
  139. }
  140. ksft_test_result(read_za->size == write_za.size,
  141. "Disabled ZA for VL %u\n", vl);
  142. out_read:
  143. free(read_buf);
  144. }
  145. /* Validate attempting to set data and read it back */
  146. static void ptrace_set_get_data(pid_t child, unsigned int vl)
  147. {
  148. void *write_buf;
  149. void *read_buf = NULL;
  150. struct user_za_header *write_za;
  151. struct user_za_header *read_za;
  152. size_t read_za_size = 0;
  153. unsigned int vq = sve_vq_from_vl(vl);
  154. int ret;
  155. size_t data_size;
  156. data_size = ZA_PT_SIZE(vq);
  157. write_buf = malloc(data_size);
  158. if (!write_buf) {
  159. ksft_test_result_fail("Error allocating %d byte buffer for VL %u\n",
  160. data_size, vl);
  161. return;
  162. }
  163. write_za = write_buf;
  164. /* Set up some data and write it out */
  165. memset(write_za, 0, data_size);
  166. write_za->size = data_size;
  167. write_za->vl = vl;
  168. fill_buf(write_buf + ZA_PT_ZA_OFFSET, ZA_PT_ZA_SIZE(vq));
  169. ret = set_za(child, write_za);
  170. if (ret != 0) {
  171. ksft_test_result_fail("Failed to set VL %u data\n", vl);
  172. goto out;
  173. }
  174. /* Read the data back */
  175. if (!get_za(child, (void **)&read_buf, &read_za_size)) {
  176. ksft_test_result_fail("Failed to read VL %u data\n", vl);
  177. goto out;
  178. }
  179. read_za = read_buf;
  180. /* We might read more data if there's extensions we don't know */
  181. if (read_za->size < write_za->size) {
  182. ksft_test_result_fail("VL %u wrote %d bytes, only read %d\n",
  183. vl, write_za->size, read_za->size);
  184. goto out_read;
  185. }
  186. ksft_test_result(memcmp(write_buf + ZA_PT_ZA_OFFSET,
  187. read_buf + ZA_PT_ZA_OFFSET,
  188. ZA_PT_ZA_SIZE(vq)) == 0,
  189. "Data match for VL %u\n", vl);
  190. out_read:
  191. free(read_buf);
  192. out:
  193. free(write_buf);
  194. }
  195. static int do_parent(pid_t child)
  196. {
  197. int ret = EXIT_FAILURE;
  198. pid_t pid;
  199. int status;
  200. siginfo_t si;
  201. unsigned int vq, vl;
  202. bool vl_supported;
  203. /* Attach to the child */
  204. while (1) {
  205. int sig;
  206. pid = wait(&status);
  207. if (pid == -1) {
  208. perror("wait");
  209. goto error;
  210. }
  211. /*
  212. * This should never happen but it's hard to flag in
  213. * the framework.
  214. */
  215. if (pid != child)
  216. continue;
  217. if (WIFEXITED(status) || WIFSIGNALED(status))
  218. ksft_exit_fail_msg("Child died unexpectedly\n");
  219. if (!WIFSTOPPED(status))
  220. goto error;
  221. sig = WSTOPSIG(status);
  222. if (ptrace(PTRACE_GETSIGINFO, pid, NULL, &si)) {
  223. if (errno == ESRCH)
  224. goto disappeared;
  225. if (errno == EINVAL) {
  226. sig = 0; /* bust group-stop */
  227. goto cont;
  228. }
  229. ksft_test_result_fail("PTRACE_GETSIGINFO: %s\n",
  230. strerror(errno));
  231. goto error;
  232. }
  233. if (sig == SIGSTOP && si.si_code == SI_TKILL &&
  234. si.si_pid == pid)
  235. break;
  236. cont:
  237. if (ptrace(PTRACE_CONT, pid, NULL, sig)) {
  238. if (errno == ESRCH)
  239. goto disappeared;
  240. ksft_test_result_fail("PTRACE_CONT: %s\n",
  241. strerror(errno));
  242. goto error;
  243. }
  244. }
  245. ksft_print_msg("Parent is %d, child is %d\n", getpid(), child);
  246. /* Step through every possible VQ */
  247. for (vq = SVE_VQ_MIN; vq <= SVE_VQ_MAX; vq++) {
  248. vl = sve_vl_from_vq(vq);
  249. /* First, try to set this vector length */
  250. ptrace_set_get_vl(child, vl, &vl_supported);
  251. /* If the VL is supported validate data set/get */
  252. if (vl_supported) {
  253. ptrace_set_no_data(child, vl);
  254. ptrace_set_get_data(child, vl);
  255. } else {
  256. ksft_test_result_skip("Disabled ZA for VL %u\n", vl);
  257. ksft_test_result_skip("Get and set data for VL %u\n",
  258. vl);
  259. }
  260. }
  261. ret = EXIT_SUCCESS;
  262. error:
  263. kill(child, SIGKILL);
  264. disappeared:
  265. return ret;
  266. }
  267. int main(void)
  268. {
  269. int ret = EXIT_SUCCESS;
  270. pid_t child;
  271. srandom(getpid());
  272. ksft_print_header();
  273. if (!(getauxval(AT_HWCAP2) & HWCAP2_SME)) {
  274. ksft_set_plan(1);
  275. ksft_exit_skip("SME not available\n");
  276. }
  277. ksft_set_plan(EXPECTED_TESTS);
  278. child = fork();
  279. if (!child)
  280. return do_child();
  281. if (do_parent(child))
  282. ret = EXIT_FAILURE;
  283. ksft_print_cnts();
  284. return ret;
  285. }