breakpoint_test_arm64.c 5.8 KB

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