bp_account.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Powerpc needs __SANE_USERSPACE_TYPES__ before <linux/types.h> to select
  4. * 'int-ll64.h' and avoid compile warnings when printing __u64 with %llu.
  5. */
  6. #define __SANE_USERSPACE_TYPES__
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <string.h>
  11. #include <sys/ioctl.h>
  12. #include <fcntl.h>
  13. #include <linux/hw_breakpoint.h>
  14. #include "tests.h"
  15. #include "debug.h"
  16. #include "event.h"
  17. #include "../perf-sys.h"
  18. #include "cloexec.h"
  19. static volatile long the_var;
  20. static noinline int test_function(void)
  21. {
  22. return 0;
  23. }
  24. static int __event(bool is_x, void *addr, struct perf_event_attr *attr)
  25. {
  26. int fd;
  27. memset(attr, 0, sizeof(struct perf_event_attr));
  28. attr->type = PERF_TYPE_BREAKPOINT;
  29. attr->size = sizeof(struct perf_event_attr);
  30. attr->config = 0;
  31. attr->bp_type = is_x ? HW_BREAKPOINT_X : HW_BREAKPOINT_W;
  32. attr->bp_addr = (unsigned long) addr;
  33. attr->bp_len = sizeof(long);
  34. attr->sample_period = 1;
  35. attr->sample_type = PERF_SAMPLE_IP;
  36. attr->exclude_kernel = 1;
  37. attr->exclude_hv = 1;
  38. fd = sys_perf_event_open(attr, -1, 0, -1,
  39. perf_event_open_cloexec_flag());
  40. if (fd < 0) {
  41. pr_debug("failed opening event %llx\n", attr->config);
  42. return TEST_FAIL;
  43. }
  44. return fd;
  45. }
  46. static int wp_event(void *addr, struct perf_event_attr *attr)
  47. {
  48. return __event(false, addr, attr);
  49. }
  50. static int bp_event(void *addr, struct perf_event_attr *attr)
  51. {
  52. return __event(true, addr, attr);
  53. }
  54. static int bp_accounting(int wp_cnt, int share)
  55. {
  56. struct perf_event_attr attr, attr_mod, attr_new;
  57. int i, fd[wp_cnt], fd_wp, ret;
  58. for (i = 0; i < wp_cnt; i++) {
  59. fd[i] = wp_event((void *)&the_var, &attr);
  60. TEST_ASSERT_VAL("failed to create wp\n", fd[i] != -1);
  61. pr_debug("wp %d created\n", i);
  62. }
  63. attr_mod = attr;
  64. attr_mod.bp_type = HW_BREAKPOINT_X;
  65. attr_mod.bp_addr = (unsigned long) test_function;
  66. ret = ioctl(fd[0], PERF_EVENT_IOC_MODIFY_ATTRIBUTES, &attr_mod);
  67. TEST_ASSERT_VAL("failed to modify wp\n", ret == 0);
  68. pr_debug("wp 0 modified to bp\n");
  69. if (!share) {
  70. fd_wp = wp_event((void *)&the_var, &attr_new);
  71. TEST_ASSERT_VAL("failed to create max wp\n", fd_wp != -1);
  72. pr_debug("wp max created\n");
  73. }
  74. for (i = 0; i < wp_cnt; i++)
  75. close(fd[i]);
  76. return 0;
  77. }
  78. static int detect_cnt(bool is_x)
  79. {
  80. struct perf_event_attr attr;
  81. void *addr = is_x ? (void *)test_function : (void *)&the_var;
  82. int fd[100], cnt = 0, i;
  83. while (1) {
  84. if (cnt == 100) {
  85. pr_debug("way too many debug registers, fix the test\n");
  86. return 0;
  87. }
  88. fd[cnt] = __event(is_x, addr, &attr);
  89. if (fd[cnt] < 0)
  90. break;
  91. cnt++;
  92. }
  93. for (i = 0; i < cnt; i++)
  94. close(fd[i]);
  95. return cnt;
  96. }
  97. static int detect_ioctl(void)
  98. {
  99. struct perf_event_attr attr;
  100. int fd, ret = 1;
  101. fd = wp_event((void *) &the_var, &attr);
  102. if (fd > 0) {
  103. ret = ioctl(fd, PERF_EVENT_IOC_MODIFY_ATTRIBUTES, &attr);
  104. close(fd);
  105. }
  106. return ret ? 0 : 1;
  107. }
  108. static int detect_share(int wp_cnt, int bp_cnt)
  109. {
  110. struct perf_event_attr attr;
  111. int i, fd[wp_cnt + bp_cnt], ret;
  112. for (i = 0; i < wp_cnt; i++) {
  113. fd[i] = wp_event((void *)&the_var, &attr);
  114. TEST_ASSERT_VAL("failed to create wp\n", fd[i] != -1);
  115. }
  116. for (; i < (bp_cnt + wp_cnt); i++) {
  117. fd[i] = bp_event((void *)test_function, &attr);
  118. if (fd[i] == -1)
  119. break;
  120. }
  121. ret = i != (bp_cnt + wp_cnt);
  122. while (i--)
  123. close(fd[i]);
  124. return ret;
  125. }
  126. /*
  127. * This test does following:
  128. * - detects the number of watch/break-points,
  129. * skip test if any is missing
  130. * - detects PERF_EVENT_IOC_MODIFY_ATTRIBUTES ioctl,
  131. * skip test if it's missing
  132. * - detects if watchpoints and breakpoints share
  133. * same slots
  134. * - create all possible watchpoints on cpu 0
  135. * - change one of it to breakpoint
  136. * - in case wp and bp do not share slots,
  137. * we create another watchpoint to ensure
  138. * the slot accounting is correct
  139. */
  140. int test__bp_accounting(struct test *test __maybe_unused, int subtest __maybe_unused)
  141. {
  142. int has_ioctl = detect_ioctl();
  143. int wp_cnt = detect_cnt(false);
  144. int bp_cnt = detect_cnt(true);
  145. int share = detect_share(wp_cnt, bp_cnt);
  146. pr_debug("watchpoints count %d, breakpoints count %d, has_ioctl %d, share %d\n",
  147. wp_cnt, bp_cnt, has_ioctl, share);
  148. if (!wp_cnt || !bp_cnt || !has_ioctl)
  149. return TEST_SKIP;
  150. return bp_accounting(wp_cnt, share);
  151. }
  152. bool test__bp_account_is_supported(void)
  153. {
  154. /*
  155. * PowerPC and S390 do not support creation of instruction
  156. * breakpoints using the perf_event interface.
  157. *
  158. * Just disable the test for these architectures until these
  159. * issues are resolved.
  160. */
  161. #if defined(__powerpc__) || defined(__s390x__)
  162. return false;
  163. #else
  164. return true;
  165. #endif
  166. }