breakpoint_test_arm64.c 5.8 KB

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