ptrace.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2022 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. #define EXPECTED_TESTS 7
  22. #define MAX_TPIDRS 2
  23. static bool have_sme(void)
  24. {
  25. return getauxval(AT_HWCAP2) & HWCAP2_SME;
  26. }
  27. static void test_tpidr(pid_t child)
  28. {
  29. uint64_t read_val[MAX_TPIDRS];
  30. uint64_t write_val[MAX_TPIDRS];
  31. struct iovec read_iov, write_iov;
  32. bool test_tpidr2 = false;
  33. int ret, i;
  34. read_iov.iov_base = read_val;
  35. write_iov.iov_base = write_val;
  36. /* Should be able to read a single TPIDR... */
  37. read_iov.iov_len = sizeof(uint64_t);
  38. ret = ptrace(PTRACE_GETREGSET, child, NT_ARM_TLS, &read_iov);
  39. ksft_test_result(ret == 0, "read_tpidr_one\n");
  40. /* ...write a new value.. */
  41. write_iov.iov_len = sizeof(uint64_t);
  42. write_val[0] = read_val[0]++;
  43. ret = ptrace(PTRACE_SETREGSET, child, NT_ARM_TLS, &write_iov);
  44. ksft_test_result(ret == 0, "write_tpidr_one\n");
  45. /* ...then read it back */
  46. ret = ptrace(PTRACE_GETREGSET, child, NT_ARM_TLS, &read_iov);
  47. ksft_test_result(ret == 0 && write_val[0] == read_val[0],
  48. "verify_tpidr_one\n");
  49. /* If we have TPIDR2 we should be able to read it */
  50. read_iov.iov_len = sizeof(read_val);
  51. ret = ptrace(PTRACE_GETREGSET, child, NT_ARM_TLS, &read_iov);
  52. if (ret == 0) {
  53. /* If we have SME there should be two TPIDRs */
  54. if (read_iov.iov_len >= sizeof(read_val))
  55. test_tpidr2 = true;
  56. if (have_sme() && test_tpidr2) {
  57. ksft_test_result(test_tpidr2, "count_tpidrs\n");
  58. } else {
  59. ksft_test_result(read_iov.iov_len % sizeof(uint64_t) == 0,
  60. "count_tpidrs\n");
  61. }
  62. } else {
  63. ksft_test_result_fail("count_tpidrs\n");
  64. }
  65. if (test_tpidr2) {
  66. /* Try to write new values to all known TPIDRs... */
  67. write_iov.iov_len = sizeof(write_val);
  68. for (i = 0; i < MAX_TPIDRS; i++)
  69. write_val[i] = read_val[i] + 1;
  70. ret = ptrace(PTRACE_SETREGSET, child, NT_ARM_TLS, &write_iov);
  71. ksft_test_result(ret == 0 &&
  72. write_iov.iov_len == sizeof(write_val),
  73. "tpidr2_write\n");
  74. /* ...then read them back */
  75. read_iov.iov_len = sizeof(read_val);
  76. ret = ptrace(PTRACE_GETREGSET, child, NT_ARM_TLS, &read_iov);
  77. if (have_sme()) {
  78. /* Should read back the written value */
  79. ksft_test_result(ret == 0 &&
  80. read_iov.iov_len >= sizeof(read_val) &&
  81. memcmp(read_val, write_val,
  82. sizeof(read_val)) == 0,
  83. "tpidr2_read\n");
  84. } else {
  85. /* TPIDR2 should read as zero */
  86. ksft_test_result(ret == 0 &&
  87. read_iov.iov_len >= sizeof(read_val) &&
  88. read_val[0] == write_val[0] &&
  89. read_val[1] == 0,
  90. "tpidr2_read\n");
  91. }
  92. /* Writing only TPIDR... */
  93. write_iov.iov_len = sizeof(uint64_t);
  94. memcpy(write_val, read_val, sizeof(read_val));
  95. write_val[0] += 1;
  96. ret = ptrace(PTRACE_SETREGSET, child, NT_ARM_TLS, &write_iov);
  97. if (ret == 0) {
  98. /* ...should leave TPIDR2 untouched */
  99. read_iov.iov_len = sizeof(read_val);
  100. ret = ptrace(PTRACE_GETREGSET, child, NT_ARM_TLS,
  101. &read_iov);
  102. ksft_test_result(ret == 0 &&
  103. read_iov.iov_len >= sizeof(read_val) &&
  104. memcmp(read_val, write_val,
  105. sizeof(read_val)) == 0,
  106. "write_tpidr_only\n");
  107. } else {
  108. ksft_test_result_fail("write_tpidr_only\n");
  109. }
  110. } else {
  111. ksft_test_result_skip("tpidr2_write\n");
  112. ksft_test_result_skip("tpidr2_read\n");
  113. ksft_test_result_skip("write_tpidr_only\n");
  114. }
  115. }
  116. static int do_child(void)
  117. {
  118. if (ptrace(PTRACE_TRACEME, -1, NULL, NULL))
  119. ksft_exit_fail_msg("PTRACE_TRACEME", strerror(errno));
  120. if (raise(SIGSTOP))
  121. ksft_exit_fail_msg("raise(SIGSTOP)", strerror(errno));
  122. return EXIT_SUCCESS;
  123. }
  124. static int do_parent(pid_t child)
  125. {
  126. int ret = EXIT_FAILURE;
  127. pid_t pid;
  128. int status;
  129. siginfo_t si;
  130. /* Attach to the child */
  131. while (1) {
  132. int sig;
  133. pid = wait(&status);
  134. if (pid == -1) {
  135. perror("wait");
  136. goto error;
  137. }
  138. /*
  139. * This should never happen but it's hard to flag in
  140. * the framework.
  141. */
  142. if (pid != child)
  143. continue;
  144. if (WIFEXITED(status) || WIFSIGNALED(status))
  145. ksft_exit_fail_msg("Child died unexpectedly\n");
  146. if (!WIFSTOPPED(status))
  147. goto error;
  148. sig = WSTOPSIG(status);
  149. if (ptrace(PTRACE_GETSIGINFO, pid, NULL, &si)) {
  150. if (errno == ESRCH)
  151. goto disappeared;
  152. if (errno == EINVAL) {
  153. sig = 0; /* bust group-stop */
  154. goto cont;
  155. }
  156. ksft_test_result_fail("PTRACE_GETSIGINFO: %s\n",
  157. strerror(errno));
  158. goto error;
  159. }
  160. if (sig == SIGSTOP && si.si_code == SI_TKILL &&
  161. si.si_pid == pid)
  162. break;
  163. cont:
  164. if (ptrace(PTRACE_CONT, pid, NULL, sig)) {
  165. if (errno == ESRCH)
  166. goto disappeared;
  167. ksft_test_result_fail("PTRACE_CONT: %s\n",
  168. strerror(errno));
  169. goto error;
  170. }
  171. }
  172. ksft_print_msg("Parent is %d, child is %d\n", getpid(), child);
  173. test_tpidr(child);
  174. ret = EXIT_SUCCESS;
  175. error:
  176. kill(child, SIGKILL);
  177. disappeared:
  178. return ret;
  179. }
  180. int main(void)
  181. {
  182. int ret = EXIT_SUCCESS;
  183. pid_t child;
  184. srandom(getpid());
  185. ksft_print_header();
  186. ksft_set_plan(EXPECTED_TESTS);
  187. child = fork();
  188. if (!child)
  189. return do_child();
  190. if (do_parent(child))
  191. ret = EXIT_FAILURE;
  192. ksft_print_cnts();
  193. return ret;
  194. }