bp_signal.c 8.2 KB

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