ptrace.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 11
  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] + 1;
  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 void test_hw_debug(pid_t child, int type, const char *type_name)
  117. {
  118. struct user_hwdebug_state state;
  119. struct iovec iov;
  120. int slots, arch, ret;
  121. iov.iov_len = sizeof(state);
  122. iov.iov_base = &state;
  123. /* Should be able to read the values */
  124. ret = ptrace(PTRACE_GETREGSET, child, type, &iov);
  125. ksft_test_result(ret == 0, "read_%s\n", type_name);
  126. if (ret == 0) {
  127. /* Low 8 bits is the number of slots, next 4 bits the arch */
  128. slots = state.dbg_info & 0xff;
  129. arch = (state.dbg_info >> 8) & 0xf;
  130. ksft_print_msg("%s version %d with %d slots\n", type_name,
  131. arch, slots);
  132. /* Zero is not currently architecturally valid */
  133. ksft_test_result(arch, "%s_arch_set\n", type_name);
  134. } else {
  135. ksft_test_result_skip("%s_arch_set\n", type_name);
  136. }
  137. }
  138. static int do_child(void)
  139. {
  140. if (ptrace(PTRACE_TRACEME, -1, NULL, NULL))
  141. ksft_exit_fail_perror("PTRACE_TRACEME");
  142. if (raise(SIGSTOP))
  143. ksft_exit_fail_perror("raise(SIGSTOP)");
  144. return EXIT_SUCCESS;
  145. }
  146. static int do_parent(pid_t child)
  147. {
  148. int ret = EXIT_FAILURE;
  149. pid_t pid;
  150. int status;
  151. siginfo_t si;
  152. /* Attach to the child */
  153. while (1) {
  154. int sig;
  155. pid = wait(&status);
  156. if (pid == -1) {
  157. perror("wait");
  158. goto error;
  159. }
  160. /*
  161. * This should never happen but it's hard to flag in
  162. * the framework.
  163. */
  164. if (pid != child)
  165. continue;
  166. if (WIFEXITED(status) || WIFSIGNALED(status))
  167. ksft_exit_fail_msg("Child died unexpectedly\n");
  168. if (!WIFSTOPPED(status))
  169. goto error;
  170. sig = WSTOPSIG(status);
  171. if (ptrace(PTRACE_GETSIGINFO, pid, NULL, &si)) {
  172. if (errno == ESRCH)
  173. goto disappeared;
  174. if (errno == EINVAL) {
  175. sig = 0; /* bust group-stop */
  176. goto cont;
  177. }
  178. ksft_test_result_fail("PTRACE_GETSIGINFO: %s\n",
  179. strerror(errno));
  180. goto error;
  181. }
  182. if (sig == SIGSTOP && si.si_code == SI_TKILL &&
  183. si.si_pid == pid)
  184. break;
  185. cont:
  186. if (ptrace(PTRACE_CONT, pid, NULL, sig)) {
  187. if (errno == ESRCH)
  188. goto disappeared;
  189. ksft_test_result_fail("PTRACE_CONT: %s\n",
  190. strerror(errno));
  191. goto error;
  192. }
  193. }
  194. ksft_print_msg("Parent is %d, child is %d\n", getpid(), child);
  195. test_tpidr(child);
  196. test_hw_debug(child, NT_ARM_HW_WATCH, "NT_ARM_HW_WATCH");
  197. test_hw_debug(child, NT_ARM_HW_BREAK, "NT_ARM_HW_BREAK");
  198. ret = EXIT_SUCCESS;
  199. error:
  200. kill(child, SIGKILL);
  201. disappeared:
  202. return ret;
  203. }
  204. int main(void)
  205. {
  206. int ret = EXIT_SUCCESS;
  207. pid_t child;
  208. srandom(getpid());
  209. ksft_print_header();
  210. ksft_set_plan(EXPECTED_TESTS);
  211. child = fork();
  212. if (!child)
  213. return do_child();
  214. if (do_parent(child))
  215. ret = EXIT_FAILURE;
  216. ksft_print_cnts();
  217. return ret;
  218. }