breakpoint_test_arm64.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2016 Google, Inc.
  4. *
  5. * Original Code by Pavel Labath <labath@google.com>
  6. *
  7. * Code modified by Pratyush Anand <panand@redhat.com>
  8. * for testing different byte select for each access size.
  9. */
  10. #define _GNU_SOURCE
  11. #include <asm/ptrace.h>
  12. #include <sys/types.h>
  13. #include <sys/wait.h>
  14. #include <sys/ptrace.h>
  15. #include <sys/param.h>
  16. #include <sys/uio.h>
  17. #include <stdint.h>
  18. #include <stdbool.h>
  19. #include <stddef.h>
  20. #include <string.h>
  21. #include <stdio.h>
  22. #include <unistd.h>
  23. #include <elf.h>
  24. #include <errno.h>
  25. #include <signal.h>
  26. #include "kselftest.h"
  27. static volatile uint8_t var[96] __attribute__((__aligned__(32)));
  28. static void child(int size, int wr)
  29. {
  30. volatile uint8_t *addr = &var[32 + wr];
  31. if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) != 0) {
  32. ksft_print_msg(
  33. "ptrace(PTRACE_TRACEME) failed: %s\n",
  34. strerror(errno));
  35. _exit(1);
  36. }
  37. if (raise(SIGSTOP) != 0) {
  38. ksft_print_msg(
  39. "raise(SIGSTOP) failed: %s\n", strerror(errno));
  40. _exit(1);
  41. }
  42. if ((uintptr_t) addr % size) {
  43. ksft_print_msg(
  44. "Wrong address write for the given size: %s\n",
  45. strerror(errno));
  46. _exit(1);
  47. }
  48. switch (size) {
  49. case 1:
  50. *addr = 47;
  51. break;
  52. case 2:
  53. *(uint16_t *)addr = 47;
  54. break;
  55. case 4:
  56. *(uint32_t *)addr = 47;
  57. break;
  58. case 8:
  59. *(uint64_t *)addr = 47;
  60. break;
  61. case 16:
  62. __asm__ volatile ("stp x29, x30, %0" : "=m" (addr[0]));
  63. break;
  64. case 32:
  65. __asm__ volatile ("stp q29, q30, %0" : "=m" (addr[0]));
  66. break;
  67. }
  68. _exit(0);
  69. }
  70. static bool set_watchpoint(pid_t pid, int size, int wp)
  71. {
  72. const volatile uint8_t *addr = &var[32 + wp];
  73. const int offset = (uintptr_t)addr % 8;
  74. const unsigned int byte_mask = ((1 << size) - 1) << offset;
  75. const unsigned int type = 2; /* Write */
  76. const unsigned int enable = 1;
  77. const unsigned int control = byte_mask << 5 | type << 3 | enable;
  78. struct user_hwdebug_state dreg_state;
  79. struct iovec iov;
  80. memset(&dreg_state, 0, sizeof(dreg_state));
  81. dreg_state.dbg_regs[0].addr = (uintptr_t)(addr - offset);
  82. dreg_state.dbg_regs[0].ctrl = control;
  83. iov.iov_base = &dreg_state;
  84. iov.iov_len = offsetof(struct user_hwdebug_state, dbg_regs) +
  85. sizeof(dreg_state.dbg_regs[0]);
  86. if (ptrace(PTRACE_SETREGSET, pid, NT_ARM_HW_WATCH, &iov) == 0)
  87. return true;
  88. if (errno == EIO)
  89. ksft_print_msg(
  90. "ptrace(PTRACE_SETREGSET, NT_ARM_HW_WATCH) not supported on this hardware: %s\n",
  91. strerror(errno));
  92. ksft_print_msg(
  93. "ptrace(PTRACE_SETREGSET, NT_ARM_HW_WATCH) failed: %s\n",
  94. strerror(errno));
  95. return false;
  96. }
  97. static bool run_test(int wr_size, int wp_size, int wr, int wp)
  98. {
  99. int status;
  100. siginfo_t siginfo;
  101. pid_t pid = fork();
  102. pid_t wpid;
  103. if (pid < 0) {
  104. ksft_test_result_fail(
  105. "fork() failed: %s\n", strerror(errno));
  106. return false;
  107. }
  108. if (pid == 0)
  109. child(wr_size, wr);
  110. wpid = waitpid(pid, &status, __WALL);
  111. if (wpid != pid) {
  112. ksft_print_msg(
  113. "waitpid() failed: %s\n", strerror(errno));
  114. return false;
  115. }
  116. if (!WIFSTOPPED(status)) {
  117. ksft_print_msg(
  118. "child did not stop: %s\n", strerror(errno));
  119. return false;
  120. }
  121. if (WSTOPSIG(status) != SIGSTOP) {
  122. ksft_print_msg("child did not stop with SIGSTOP\n");
  123. return false;
  124. }
  125. if (!set_watchpoint(pid, wp_size, wp))
  126. return false;
  127. if (ptrace(PTRACE_CONT, pid, NULL, NULL) < 0) {
  128. ksft_print_msg(
  129. "ptrace(PTRACE_CONT) failed: %s\n",
  130. strerror(errno));
  131. return false;
  132. }
  133. alarm(3);
  134. wpid = waitpid(pid, &status, __WALL);
  135. if (wpid != pid) {
  136. ksft_print_msg(
  137. "waitpid() failed: %s\n", strerror(errno));
  138. return false;
  139. }
  140. alarm(0);
  141. if (WIFEXITED(status)) {
  142. ksft_print_msg("child exited prematurely\n");
  143. return false;
  144. }
  145. if (!WIFSTOPPED(status)) {
  146. ksft_print_msg("child did not stop\n");
  147. return false;
  148. }
  149. if (WSTOPSIG(status) != SIGTRAP) {
  150. ksft_print_msg("child did not stop with SIGTRAP\n");
  151. return false;
  152. }
  153. if (ptrace(PTRACE_GETSIGINFO, pid, NULL, &siginfo) != 0) {
  154. ksft_print_msg(
  155. "ptrace(PTRACE_GETSIGINFO): %s\n",
  156. strerror(errno));
  157. return false;
  158. }
  159. if (siginfo.si_code != TRAP_HWBKPT) {
  160. ksft_print_msg(
  161. "Unexpected si_code %d\n", siginfo.si_code);
  162. return false;
  163. }
  164. kill(pid, SIGKILL);
  165. wpid = waitpid(pid, &status, 0);
  166. if (wpid != pid) {
  167. ksft_print_msg(
  168. "waitpid() failed: %s\n", strerror(errno));
  169. return false;
  170. }
  171. return true;
  172. }
  173. static void sigalrm(int sig)
  174. {
  175. }
  176. int main(int argc, char **argv)
  177. {
  178. int opt;
  179. bool succeeded = true;
  180. struct sigaction act;
  181. int wr, wp, size;
  182. bool result;
  183. ksft_print_header();
  184. ksft_set_plan(213);
  185. act.sa_handler = sigalrm;
  186. sigemptyset(&act.sa_mask);
  187. act.sa_flags = 0;
  188. sigaction(SIGALRM, &act, NULL);
  189. for (size = 1; size <= 32; size = size*2) {
  190. for (wr = 0; wr <= 32; wr = wr + size) {
  191. for (wp = wr - size; wp <= wr + size; wp = wp + size) {
  192. result = run_test(size, MIN(size, 8), wr, wp);
  193. if ((result && wr == wp) ||
  194. (!result && wr != wp))
  195. ksft_test_result_pass(
  196. "Test size = %d write offset = %d watchpoint offset = %d\n",
  197. size, wr, wp);
  198. else {
  199. ksft_test_result_fail(
  200. "Test size = %d write offset = %d watchpoint offset = %d\n",
  201. size, wr, wp);
  202. succeeded = false;
  203. }
  204. }
  205. }
  206. }
  207. for (size = 1; size <= 32; size = size*2) {
  208. if (run_test(size, 8, -size, -8))
  209. ksft_test_result_pass(
  210. "Test size = %d write offset = %d watchpoint offset = -8\n",
  211. size, -size);
  212. else {
  213. ksft_test_result_fail(
  214. "Test size = %d write offset = %d watchpoint offset = -8\n",
  215. size, -size);
  216. succeeded = false;
  217. }
  218. }
  219. if (succeeded)
  220. ksft_exit_pass();
  221. else
  222. ksft_exit_fail();
  223. }