bp_signal.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 "perf.h"
  26. #include "cloexec.h"
  27. static int fd1;
  28. static int fd2;
  29. static int fd3;
  30. static int overflows;
  31. static int overflows_2;
  32. volatile long the_var;
  33. /*
  34. * Use ASM to ensure watchpoint and breakpoint can be triggered
  35. * at one instruction.
  36. */
  37. #if defined (__x86_64__)
  38. extern void __test_function(volatile long *ptr);
  39. asm (
  40. ".globl __test_function\n"
  41. "__test_function:\n"
  42. "incq (%rdi)\n"
  43. "ret\n");
  44. #else
  45. static void __test_function(volatile long *ptr)
  46. {
  47. *ptr = 0x1234;
  48. }
  49. #endif
  50. static noinline int test_function(void)
  51. {
  52. __test_function(&the_var);
  53. the_var++;
  54. return time(NULL);
  55. }
  56. static void sig_handler_2(int signum __maybe_unused,
  57. siginfo_t *oh __maybe_unused,
  58. void *uc __maybe_unused)
  59. {
  60. overflows_2++;
  61. if (overflows_2 > 10) {
  62. ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
  63. ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
  64. ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
  65. }
  66. }
  67. static void sig_handler(int signum __maybe_unused,
  68. siginfo_t *oh __maybe_unused,
  69. void *uc __maybe_unused)
  70. {
  71. overflows++;
  72. if (overflows > 10) {
  73. /*
  74. * This should be executed only once during
  75. * this test, if we are here for the 10th
  76. * time, consider this the recursive issue.
  77. *
  78. * We can get out of here by disable events,
  79. * so no new SIGIO is delivered.
  80. */
  81. ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
  82. ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
  83. ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
  84. }
  85. }
  86. static int __event(bool is_x, void *addr, int sig)
  87. {
  88. struct perf_event_attr pe;
  89. int fd;
  90. memset(&pe, 0, sizeof(struct perf_event_attr));
  91. pe.type = PERF_TYPE_BREAKPOINT;
  92. pe.size = sizeof(struct perf_event_attr);
  93. pe.config = 0;
  94. pe.bp_type = is_x ? HW_BREAKPOINT_X : HW_BREAKPOINT_W;
  95. pe.bp_addr = (unsigned long) addr;
  96. pe.bp_len = sizeof(long);
  97. pe.sample_period = 1;
  98. pe.sample_type = PERF_SAMPLE_IP;
  99. pe.wakeup_events = 1;
  100. pe.disabled = 1;
  101. pe.exclude_kernel = 1;
  102. pe.exclude_hv = 1;
  103. fd = sys_perf_event_open(&pe, 0, -1, -1,
  104. perf_event_open_cloexec_flag());
  105. if (fd < 0) {
  106. pr_debug("failed opening event %llx\n", pe.config);
  107. return TEST_FAIL;
  108. }
  109. fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
  110. fcntl(fd, F_SETSIG, sig);
  111. fcntl(fd, F_SETOWN, getpid());
  112. ioctl(fd, PERF_EVENT_IOC_RESET, 0);
  113. return fd;
  114. }
  115. static int bp_event(void *addr, int sig)
  116. {
  117. return __event(true, addr, sig);
  118. }
  119. static int wp_event(void *addr, int sig)
  120. {
  121. return __event(false, addr, sig);
  122. }
  123. static long long bp_count(int fd)
  124. {
  125. long long count;
  126. int ret;
  127. ret = read(fd, &count, sizeof(long long));
  128. if (ret != sizeof(long long)) {
  129. pr_debug("failed to read: %d\n", ret);
  130. return TEST_FAIL;
  131. }
  132. return count;
  133. }
  134. int test__bp_signal(struct test *test __maybe_unused, int subtest __maybe_unused)
  135. {
  136. struct sigaction sa;
  137. long long count1, count2, count3;
  138. /* setup SIGIO signal handler */
  139. memset(&sa, 0, sizeof(struct sigaction));
  140. sa.sa_sigaction = (void *) sig_handler;
  141. sa.sa_flags = SA_SIGINFO;
  142. if (sigaction(SIGIO, &sa, NULL) < 0) {
  143. pr_debug("failed setting up signal handler\n");
  144. return TEST_FAIL;
  145. }
  146. sa.sa_sigaction = (void *) sig_handler_2;
  147. if (sigaction(SIGUSR1, &sa, NULL) < 0) {
  148. pr_debug("failed setting up signal handler 2\n");
  149. return TEST_FAIL;
  150. }
  151. /*
  152. * We create following events:
  153. *
  154. * fd1 - breakpoint event on __test_function with SIGIO
  155. * signal configured. We should get signal
  156. * notification each time the breakpoint is hit
  157. *
  158. * fd2 - breakpoint event on sig_handler with SIGUSR1
  159. * configured. We should get SIGUSR1 each time when
  160. * breakpoint is hit
  161. *
  162. * fd3 - watchpoint event on __test_function with SIGIO
  163. * configured.
  164. *
  165. * Following processing should happen:
  166. * Exec: Action: Result:
  167. * incq (%rdi) - fd1 event breakpoint hit -> count1 == 1
  168. * - SIGIO is delivered
  169. * sig_handler - fd2 event breakpoint hit -> count2 == 1
  170. * - SIGUSR1 is delivered
  171. * sig_handler_2 -> overflows_2 == 1 (nested signal)
  172. * sys_rt_sigreturn - return from sig_handler_2
  173. * overflows++ -> overflows = 1
  174. * sys_rt_sigreturn - return from sig_handler
  175. * incq (%rdi) - fd3 event watchpoint hit -> count3 == 1 (wp and bp in one insn)
  176. * - SIGIO is delivered
  177. * sig_handler - fd2 event breakpoint hit -> count2 == 2
  178. * - SIGUSR1 is delivered
  179. * sig_handler_2 -> overflows_2 == 2 (nested signal)
  180. * sys_rt_sigreturn - return from sig_handler_2
  181. * overflows++ -> overflows = 2
  182. * sys_rt_sigreturn - return from sig_handler
  183. * the_var++ - fd3 event watchpoint hit -> count3 == 2 (standalone watchpoint)
  184. * - SIGIO is delivered
  185. * sig_handler - fd2 event breakpoint hit -> count2 == 3
  186. * - SIGUSR1 is delivered
  187. * sig_handler_2 -> overflows_2 == 3 (nested signal)
  188. * sys_rt_sigreturn - return from sig_handler_2
  189. * overflows++ -> overflows == 3
  190. * sys_rt_sigreturn - return from sig_handler
  191. *
  192. * The test case check following error conditions:
  193. * - we get stuck in signal handler because of debug
  194. * exception being triggered receursively due to
  195. * the wrong RF EFLAG management
  196. *
  197. * - we never trigger the sig_handler breakpoint due
  198. * to the rong RF EFLAG management
  199. *
  200. */
  201. fd1 = bp_event(__test_function, SIGIO);
  202. fd2 = bp_event(sig_handler, SIGUSR1);
  203. fd3 = wp_event((void *)&the_var, SIGIO);
  204. ioctl(fd1, PERF_EVENT_IOC_ENABLE, 0);
  205. ioctl(fd2, PERF_EVENT_IOC_ENABLE, 0);
  206. ioctl(fd3, PERF_EVENT_IOC_ENABLE, 0);
  207. /*
  208. * Kick off the test by trigering 'fd1'
  209. * breakpoint.
  210. */
  211. test_function();
  212. ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
  213. ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
  214. ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
  215. count1 = bp_count(fd1);
  216. count2 = bp_count(fd2);
  217. count3 = bp_count(fd3);
  218. close(fd1);
  219. close(fd2);
  220. close(fd3);
  221. pr_debug("count1 %lld, count2 %lld, count3 %lld, overflow %d, overflows_2 %d\n",
  222. count1, count2, count3, overflows, overflows_2);
  223. if (count1 != 1) {
  224. if (count1 == 11)
  225. pr_debug("failed: RF EFLAG recursion issue detected\n");
  226. else
  227. pr_debug("failed: wrong count for bp1%lld\n", count1);
  228. }
  229. if (overflows != 3)
  230. pr_debug("failed: wrong overflow hit\n");
  231. if (overflows_2 != 3)
  232. pr_debug("failed: wrong overflow_2 hit\n");
  233. if (count2 != 3)
  234. pr_debug("failed: wrong count for bp2\n");
  235. if (count3 != 2)
  236. pr_debug("failed: wrong count for bp3\n");
  237. return count1 == 1 && overflows == 3 && count2 == 3 && overflows_2 == 3 && count3 == 2 ?
  238. TEST_OK : TEST_FAIL;
  239. }
  240. bool test__bp_signal_is_supported(void)
  241. {
  242. /*
  243. * PowerPC and S390 do not support creation of instruction
  244. * breakpoints using the perf_event interface.
  245. *
  246. * ARM requires explicit rounding down of the instruction
  247. * pointer in Thumb mode, and then requires the single-step
  248. * to be handled explicitly in the overflow handler to avoid
  249. * stepping into the SIGIO handler and getting stuck on the
  250. * breakpointed instruction.
  251. *
  252. * Since arm64 has the same issue with arm for the single-step
  253. * handling, this case also gets suck on the breakpointed
  254. * instruction.
  255. *
  256. * Just disable the test for these architectures until these
  257. * issues are resolved.
  258. */
  259. #if defined(__powerpc__) || defined(__s390x__) || defined(__arm__) || \
  260. defined(__aarch64__)
  261. return false;
  262. #else
  263. return true;
  264. #endif
  265. }