wp.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <errno.h>
  6. #include <sys/ioctl.h>
  7. #include <linux/compiler.h>
  8. #include <linux/hw_breakpoint.h>
  9. #include <linux/kernel.h>
  10. #include "tests.h"
  11. #include "debug.h"
  12. #include "event.h"
  13. #include "cloexec.h"
  14. #include "../perf-sys.h"
  15. #define WP_TEST_ASSERT_VAL(fd, text, val) \
  16. do { \
  17. long long count; \
  18. wp_read(fd, &count, sizeof(long long)); \
  19. TEST_ASSERT_VAL(text, count == val); \
  20. } while (0)
  21. #ifdef __i386__
  22. /* Only breakpoint length less-than 8 has hardware support on i386. */
  23. volatile u32 data1;
  24. #else
  25. volatile u64 data1;
  26. #endif
  27. volatile u8 data2[3];
  28. #ifndef __s390x__
  29. static int wp_read(int fd, long long *count, int size)
  30. {
  31. int ret = read(fd, count, size);
  32. if (ret != size) {
  33. pr_debug("failed to read: %d\n", ret);
  34. return -1;
  35. }
  36. return 0;
  37. }
  38. static void get__perf_event_attr(struct perf_event_attr *attr, int wp_type,
  39. void *wp_addr, unsigned long wp_len)
  40. {
  41. memset(attr, 0, sizeof(struct perf_event_attr));
  42. attr->type = PERF_TYPE_BREAKPOINT;
  43. attr->size = sizeof(struct perf_event_attr);
  44. attr->config = 0;
  45. attr->bp_type = wp_type;
  46. attr->bp_addr = (unsigned long)wp_addr;
  47. attr->bp_len = wp_len;
  48. attr->sample_period = 1;
  49. attr->sample_type = PERF_SAMPLE_IP;
  50. attr->exclude_kernel = 1;
  51. attr->exclude_hv = 1;
  52. }
  53. static int __event(int wp_type, void *wp_addr, unsigned long wp_len)
  54. {
  55. int fd;
  56. struct perf_event_attr attr;
  57. get__perf_event_attr(&attr, wp_type, wp_addr, wp_len);
  58. fd = sys_perf_event_open(&attr, 0, -1, -1,
  59. perf_event_open_cloexec_flag());
  60. if (fd < 0) {
  61. fd = -errno;
  62. pr_debug("failed opening event %x\n", attr.bp_type);
  63. }
  64. return fd;
  65. }
  66. #endif
  67. static int test__wp_ro(struct test_suite *test __maybe_unused,
  68. int subtest __maybe_unused)
  69. {
  70. #if defined(__s390x__) || defined(__x86_64__) || defined(__i386__)
  71. return TEST_SKIP;
  72. #else
  73. int fd;
  74. unsigned long tmp, tmp1 = rand();
  75. fd = __event(HW_BREAKPOINT_R, (void *)&data1, sizeof(data1));
  76. if (fd < 0)
  77. return fd == -ENODEV ? TEST_SKIP : -1;
  78. tmp = data1;
  79. WP_TEST_ASSERT_VAL(fd, "RO watchpoint", 1);
  80. data1 = tmp1 + tmp;
  81. WP_TEST_ASSERT_VAL(fd, "RO watchpoint", 1);
  82. close(fd);
  83. return 0;
  84. #endif
  85. }
  86. static int test__wp_wo(struct test_suite *test __maybe_unused,
  87. int subtest __maybe_unused)
  88. {
  89. #if defined(__s390x__)
  90. return TEST_SKIP;
  91. #else
  92. int fd;
  93. unsigned long tmp, tmp1 = rand();
  94. fd = __event(HW_BREAKPOINT_W, (void *)&data1, sizeof(data1));
  95. if (fd < 0)
  96. return fd == -ENODEV ? TEST_SKIP : -1;
  97. tmp = data1;
  98. WP_TEST_ASSERT_VAL(fd, "WO watchpoint", 0);
  99. data1 = tmp1 + tmp;
  100. WP_TEST_ASSERT_VAL(fd, "WO watchpoint", 1);
  101. close(fd);
  102. return 0;
  103. #endif
  104. }
  105. static int test__wp_rw(struct test_suite *test __maybe_unused,
  106. int subtest __maybe_unused)
  107. {
  108. #if defined(__s390x__)
  109. return TEST_SKIP;
  110. #else
  111. int fd;
  112. unsigned long tmp, tmp1 = rand();
  113. fd = __event(HW_BREAKPOINT_R | HW_BREAKPOINT_W, (void *)&data1,
  114. sizeof(data1));
  115. if (fd < 0)
  116. return fd == -ENODEV ? TEST_SKIP : -1;
  117. tmp = data1;
  118. WP_TEST_ASSERT_VAL(fd, "RW watchpoint", 1);
  119. data1 = tmp1 + tmp;
  120. WP_TEST_ASSERT_VAL(fd, "RW watchpoint", 2);
  121. close(fd);
  122. return 0;
  123. #endif
  124. }
  125. static int test__wp_modify(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
  126. {
  127. #if defined(__s390x__)
  128. return TEST_SKIP;
  129. #else
  130. int fd, ret;
  131. unsigned long tmp = rand();
  132. struct perf_event_attr new_attr;
  133. fd = __event(HW_BREAKPOINT_W, (void *)&data1, sizeof(data1));
  134. if (fd < 0)
  135. return fd == -ENODEV ? TEST_SKIP : -1;
  136. data1 = tmp;
  137. WP_TEST_ASSERT_VAL(fd, "Modify watchpoint", 1);
  138. /* Modify watchpoint with disabled = 1 */
  139. get__perf_event_attr(&new_attr, HW_BREAKPOINT_W, (void *)&data2[0],
  140. sizeof(u8) * 2);
  141. new_attr.disabled = 1;
  142. ret = ioctl(fd, PERF_EVENT_IOC_MODIFY_ATTRIBUTES, &new_attr);
  143. if (ret < 0) {
  144. if (errno == ENOTTY) {
  145. test->test_cases[subtest].skip_reason = "missing kernel support";
  146. ret = TEST_SKIP;
  147. }
  148. pr_debug("ioctl(PERF_EVENT_IOC_MODIFY_ATTRIBUTES) failed\n");
  149. close(fd);
  150. return ret;
  151. }
  152. data2[1] = tmp; /* Not Counted */
  153. WP_TEST_ASSERT_VAL(fd, "Modify watchpoint", 1);
  154. /* Enable the event */
  155. ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
  156. if (ret < 0) {
  157. pr_debug("Failed to enable event\n");
  158. close(fd);
  159. return ret;
  160. }
  161. data2[1] = tmp; /* Counted */
  162. WP_TEST_ASSERT_VAL(fd, "Modify watchpoint", 2);
  163. data2[2] = tmp; /* Not Counted */
  164. WP_TEST_ASSERT_VAL(fd, "Modify watchpoint", 2);
  165. close(fd);
  166. return 0;
  167. #endif
  168. }
  169. static struct test_case wp_tests[] = {
  170. TEST_CASE_REASON("Read Only Watchpoint", wp_ro, "missing hardware support"),
  171. TEST_CASE_REASON("Write Only Watchpoint", wp_wo, "missing hardware support"),
  172. TEST_CASE_REASON("Read / Write Watchpoint", wp_rw, "missing hardware support"),
  173. TEST_CASE_REASON("Modify Watchpoint", wp_modify, "missing hardware support"),
  174. { .name = NULL, }
  175. };
  176. struct test_suite suite__wp = {
  177. .desc = "Watchpoint",
  178. .test_cases = wp_tests,
  179. };