bp_signal.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Inspired by breakpoint overflow test done by
  3. * Vince Weaver <vincent.weaver@maine.edu> for perf_event_tests
  4. * (git://github.com/deater/perf_event_tests)
  5. */
  6. /*
  7. * Powerpc needs __SANE_USERSPACE_TYPES__ before <linux/types.h> to select
  8. * 'int-ll64.h' and avoid compile warnings when printing __u64 with %llu.
  9. */
  10. #define __SANE_USERSPACE_TYPES__
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <unistd.h>
  14. #include <string.h>
  15. #include <sys/ioctl.h>
  16. #include <time.h>
  17. #include <fcntl.h>
  18. #include <signal.h>
  19. #include <sys/mman.h>
  20. #include <linux/compiler.h>
  21. #include <linux/hw_breakpoint.h>
  22. #include "tests.h"
  23. #include "debug.h"
  24. #include "perf.h"
  25. #include "cloexec.h"
  26. static int fd1;
  27. static int fd2;
  28. static int overflows;
  29. __attribute__ ((noinline))
  30. static int test_function(void)
  31. {
  32. return time(NULL);
  33. }
  34. static void sig_handler(int signum __maybe_unused,
  35. siginfo_t *oh __maybe_unused,
  36. void *uc __maybe_unused)
  37. {
  38. overflows++;
  39. if (overflows > 10) {
  40. /*
  41. * This should be executed only once during
  42. * this test, if we are here for the 10th
  43. * time, consider this the recursive issue.
  44. *
  45. * We can get out of here by disable events,
  46. * so no new SIGIO is delivered.
  47. */
  48. ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
  49. ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
  50. }
  51. }
  52. static int bp_event(void *fn, int setup_signal)
  53. {
  54. struct perf_event_attr pe;
  55. int fd;
  56. memset(&pe, 0, sizeof(struct perf_event_attr));
  57. pe.type = PERF_TYPE_BREAKPOINT;
  58. pe.size = sizeof(struct perf_event_attr);
  59. pe.config = 0;
  60. pe.bp_type = HW_BREAKPOINT_X;
  61. pe.bp_addr = (unsigned long) fn;
  62. pe.bp_len = sizeof(long);
  63. pe.sample_period = 1;
  64. pe.sample_type = PERF_SAMPLE_IP;
  65. pe.wakeup_events = 1;
  66. pe.disabled = 1;
  67. pe.exclude_kernel = 1;
  68. pe.exclude_hv = 1;
  69. fd = sys_perf_event_open(&pe, 0, -1, -1,
  70. perf_event_open_cloexec_flag());
  71. if (fd < 0) {
  72. pr_debug("failed opening event %llx\n", pe.config);
  73. return TEST_FAIL;
  74. }
  75. if (setup_signal) {
  76. fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
  77. fcntl(fd, F_SETSIG, SIGIO);
  78. fcntl(fd, F_SETOWN, getpid());
  79. }
  80. ioctl(fd, PERF_EVENT_IOC_RESET, 0);
  81. return fd;
  82. }
  83. static long long bp_count(int fd)
  84. {
  85. long long count;
  86. int ret;
  87. ret = read(fd, &count, sizeof(long long));
  88. if (ret != sizeof(long long)) {
  89. pr_debug("failed to read: %d\n", ret);
  90. return TEST_FAIL;
  91. }
  92. return count;
  93. }
  94. int test__bp_signal(void)
  95. {
  96. struct sigaction sa;
  97. long long count1, count2;
  98. /* setup SIGIO signal handler */
  99. memset(&sa, 0, sizeof(struct sigaction));
  100. sa.sa_sigaction = (void *) sig_handler;
  101. sa.sa_flags = SA_SIGINFO;
  102. if (sigaction(SIGIO, &sa, NULL) < 0) {
  103. pr_debug("failed setting up signal handler\n");
  104. return TEST_FAIL;
  105. }
  106. /*
  107. * We create following events:
  108. *
  109. * fd1 - breakpoint event on test_function with SIGIO
  110. * signal configured. We should get signal
  111. * notification each time the breakpoint is hit
  112. *
  113. * fd2 - breakpoint event on sig_handler without SIGIO
  114. * configured.
  115. *
  116. * Following processing should happen:
  117. * - execute test_function
  118. * - fd1 event breakpoint hit -> count1 == 1
  119. * - SIGIO is delivered -> overflows == 1
  120. * - fd2 event breakpoint hit -> count2 == 1
  121. *
  122. * The test case check following error conditions:
  123. * - we get stuck in signal handler because of debug
  124. * exception being triggered receursively due to
  125. * the wrong RF EFLAG management
  126. *
  127. * - we never trigger the sig_handler breakpoint due
  128. * to the rong RF EFLAG management
  129. *
  130. */
  131. fd1 = bp_event(test_function, 1);
  132. fd2 = bp_event(sig_handler, 0);
  133. ioctl(fd1, PERF_EVENT_IOC_ENABLE, 0);
  134. ioctl(fd2, PERF_EVENT_IOC_ENABLE, 0);
  135. /*
  136. * Kick off the test by trigering 'fd1'
  137. * breakpoint.
  138. */
  139. test_function();
  140. ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
  141. ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
  142. count1 = bp_count(fd1);
  143. count2 = bp_count(fd2);
  144. close(fd1);
  145. close(fd2);
  146. pr_debug("count1 %lld, count2 %lld, overflow %d\n",
  147. count1, count2, overflows);
  148. if (count1 != 1) {
  149. if (count1 == 11)
  150. pr_debug("failed: RF EFLAG recursion issue detected\n");
  151. else
  152. pr_debug("failed: wrong count for bp1%lld\n", count1);
  153. }
  154. if (overflows != 1)
  155. pr_debug("failed: wrong overflow hit\n");
  156. if (count2 != 1)
  157. pr_debug("failed: wrong count for bp2\n");
  158. return count1 == 1 && overflows == 1 && count2 == 1 ?
  159. TEST_OK : TEST_FAIL;
  160. }