bp_signal.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Inspired by breakpoint overflow test done by
  4. * Vince Weaver <vincent.weaver@maine.edu> for perf_event_tests
  5. * (git://github.com/deater/perf_event_tests)
  6. */
  7. /*
  8. * Powerpc needs __SANE_USERSPACE_TYPES__ before <linux/types.h> to select
  9. * 'int-ll64.h' and avoid compile warnings when printing __u64 with %llu.
  10. */
  11. #define __SANE_USERSPACE_TYPES__
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <unistd.h>
  15. #include <string.h>
  16. #include <sys/ioctl.h>
  17. #include <time.h>
  18. #include <fcntl.h>
  19. #include <signal.h>
  20. #include <sys/mman.h>
  21. #include <linux/compiler.h>
  22. #include <linux/hw_breakpoint.h>
  23. #include "tests.h"
  24. #include "debug.h"
  25. #include "event.h"
  26. #include "parse-events.h"
  27. #include "perf-sys.h"
  28. #include "cloexec.h"
  29. static int fd1;
  30. static int fd2;
  31. static int fd3;
  32. static int overflows;
  33. static int overflows_2;
  34. volatile long the_var;
  35. /*
  36. * Use ASM to ensure watchpoint and breakpoint can be triggered
  37. * at one instruction.
  38. */
  39. #if defined (__x86_64__)
  40. extern void __test_function(volatile long *ptr);
  41. asm (
  42. ".pushsection .text;"
  43. ".globl __test_function\n"
  44. ".type __test_function, @function;"
  45. "__test_function:\n"
  46. "incq (%rdi)\n"
  47. "ret\n"
  48. ".popsection\n");
  49. #else
  50. static void __test_function(volatile long *ptr)
  51. {
  52. *ptr = 0x1234;
  53. }
  54. #endif
  55. static noinline int test_function(void)
  56. {
  57. __test_function(&the_var);
  58. the_var++;
  59. return time(NULL);
  60. }
  61. static void sig_handler_2(int signum __maybe_unused,
  62. siginfo_t *oh __maybe_unused,
  63. void *uc __maybe_unused)
  64. {
  65. overflows_2++;
  66. if (overflows_2 > 10) {
  67. ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
  68. ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
  69. ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
  70. }
  71. }
  72. static void sig_handler(int signum __maybe_unused,
  73. siginfo_t *oh __maybe_unused,
  74. void *uc __maybe_unused)
  75. {
  76. overflows++;
  77. if (overflows > 10) {
  78. /*
  79. * This should be executed only once during
  80. * this test, if we are here for the 10th
  81. * time, consider this the recursive issue.
  82. *
  83. * We can get out of here by disable events,
  84. * so no new SIGIO is delivered.
  85. */
  86. ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
  87. ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
  88. ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
  89. }
  90. }
  91. static int __event(bool is_x, void *addr, int sig)
  92. {
  93. struct perf_event_attr pe;
  94. int fd;
  95. memset(&pe, 0, sizeof(struct perf_event_attr));
  96. pe.type = PERF_TYPE_BREAKPOINT;
  97. pe.size = sizeof(struct perf_event_attr);
  98. pe.config = 0;
  99. pe.bp_type = is_x ? HW_BREAKPOINT_X : HW_BREAKPOINT_W;
  100. pe.bp_addr = (unsigned long) addr;
  101. pe.bp_len = is_x ? default_breakpoint_len() : sizeof(long);
  102. pe.sample_period = 1;
  103. pe.sample_type = PERF_SAMPLE_IP;
  104. pe.wakeup_events = 1;
  105. pe.disabled = 1;
  106. pe.exclude_kernel = 1;
  107. pe.exclude_hv = 1;
  108. fd = sys_perf_event_open(&pe, 0, -1, -1,
  109. perf_event_open_cloexec_flag());
  110. if (fd < 0) {
  111. pr_debug("failed opening event %llx\n", pe.config);
  112. return TEST_FAIL;
  113. }
  114. fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
  115. fcntl(fd, F_SETSIG, sig);
  116. fcntl(fd, F_SETOWN, getpid());
  117. ioctl(fd, PERF_EVENT_IOC_RESET, 0);
  118. return fd;
  119. }
  120. static int bp_event(void *addr, int sig)
  121. {
  122. return __event(true, addr, sig);
  123. }
  124. static int wp_event(void *addr, int sig)
  125. {
  126. return __event(false, addr, sig);
  127. }
  128. static long long bp_count(int fd)
  129. {
  130. long long count;
  131. int ret;
  132. ret = read(fd, &count, sizeof(long long));
  133. if (ret != sizeof(long long)) {
  134. pr_debug("failed to read: %d\n", ret);
  135. return TEST_FAIL;
  136. }
  137. return count;
  138. }
  139. static int test__bp_signal(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
  140. {
  141. struct sigaction sa;
  142. long long count1, count2, count3;
  143. if (!BP_SIGNAL_IS_SUPPORTED) {
  144. pr_debug("Test not supported on this architecture");
  145. return TEST_SKIP;
  146. }
  147. /* setup SIGIO signal handler */
  148. memset(&sa, 0, sizeof(struct sigaction));
  149. sa.sa_sigaction = (void *) sig_handler;
  150. sa.sa_flags = SA_SIGINFO;
  151. if (sigaction(SIGIO, &sa, NULL) < 0) {
  152. pr_debug("failed setting up signal handler\n");
  153. return TEST_FAIL;
  154. }
  155. sa.sa_sigaction = (void *) sig_handler_2;
  156. if (sigaction(SIGUSR1, &sa, NULL) < 0) {
  157. pr_debug("failed setting up signal handler 2\n");
  158. return TEST_FAIL;
  159. }
  160. /*
  161. * We create following events:
  162. *
  163. * fd1 - breakpoint event on __test_function with SIGIO
  164. * signal configured. We should get signal
  165. * notification each time the breakpoint is hit
  166. *
  167. * fd2 - breakpoint event on sig_handler with SIGUSR1
  168. * configured. We should get SIGUSR1 each time when
  169. * breakpoint is hit
  170. *
  171. * fd3 - watchpoint event on __test_function with SIGIO
  172. * configured.
  173. *
  174. * Following processing should happen:
  175. * Exec: Action: Result:
  176. * incq (%rdi) - fd1 event breakpoint hit -> count1 == 1
  177. * - SIGIO is delivered
  178. * sig_handler - fd2 event breakpoint hit -> count2 == 1
  179. * - SIGUSR1 is delivered
  180. * sig_handler_2 -> overflows_2 == 1 (nested signal)
  181. * sys_rt_sigreturn - return from sig_handler_2
  182. * overflows++ -> overflows = 1
  183. * sys_rt_sigreturn - return from sig_handler
  184. * incq (%rdi) - fd3 event watchpoint hit -> count3 == 1 (wp and bp in one insn)
  185. * - SIGIO is delivered
  186. * sig_handler - fd2 event breakpoint hit -> count2 == 2
  187. * - SIGUSR1 is delivered
  188. * sig_handler_2 -> overflows_2 == 2 (nested signal)
  189. * sys_rt_sigreturn - return from sig_handler_2
  190. * overflows++ -> overflows = 2
  191. * sys_rt_sigreturn - return from sig_handler
  192. * the_var++ - fd3 event watchpoint hit -> count3 == 2 (standalone watchpoint)
  193. * - SIGIO is delivered
  194. * sig_handler - fd2 event breakpoint hit -> count2 == 3
  195. * - SIGUSR1 is delivered
  196. * sig_handler_2 -> overflows_2 == 3 (nested signal)
  197. * sys_rt_sigreturn - return from sig_handler_2
  198. * overflows++ -> overflows == 3
  199. * sys_rt_sigreturn - return from sig_handler
  200. *
  201. * The test case check following error conditions:
  202. * - we get stuck in signal handler because of debug
  203. * exception being triggered recursively due to
  204. * the wrong RF EFLAG management
  205. *
  206. * - we never trigger the sig_handler breakpoint due
  207. * to the wrong RF EFLAG management
  208. *
  209. */
  210. fd1 = bp_event(__test_function, SIGIO);
  211. fd2 = bp_event(sig_handler, SIGUSR1);
  212. fd3 = wp_event((void *)&the_var, SIGIO);
  213. ioctl(fd1, PERF_EVENT_IOC_ENABLE, 0);
  214. ioctl(fd2, PERF_EVENT_IOC_ENABLE, 0);
  215. ioctl(fd3, PERF_EVENT_IOC_ENABLE, 0);
  216. /*
  217. * Kick off the test by triggering 'fd1'
  218. * breakpoint.
  219. */
  220. test_function();
  221. ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
  222. ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
  223. ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
  224. count1 = bp_count(fd1);
  225. count2 = bp_count(fd2);
  226. count3 = bp_count(fd3);
  227. close(fd1);
  228. close(fd2);
  229. close(fd3);
  230. pr_debug("count1 %lld, count2 %lld, count3 %lld, overflow %d, overflows_2 %d\n",
  231. count1, count2, count3, overflows, overflows_2);
  232. if (count1 != 1) {
  233. if (count1 == 11)
  234. pr_debug("failed: RF EFLAG recursion issue detected\n");
  235. else
  236. pr_debug("failed: wrong count for bp1: %lld, expected 1\n", count1);
  237. }
  238. if (overflows != 3)
  239. pr_debug("failed: wrong overflow (%d) hit, expected 3\n", overflows);
  240. if (overflows_2 != 3)
  241. pr_debug("failed: wrong overflow_2 (%d) hit, expected 3\n", overflows_2);
  242. if (count2 != 3)
  243. pr_debug("failed: wrong count for bp2 (%lld), expected 3\n", count2);
  244. if (count3 != 2)
  245. pr_debug("failed: wrong count for bp3 (%lld), expected 2\n", count3);
  246. return count1 == 1 && overflows == 3 && count2 == 3 && overflows_2 == 3 && count3 == 2 ?
  247. TEST_OK : TEST_FAIL;
  248. }
  249. DEFINE_SUITE("Breakpoint overflow signal handler", bp_signal);