breakpoint_test_arm64.c 5.3 KB

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