bp_signal.c 4.0 KB

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