bp_account.c 4.2 KB

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