bp_signal.c 8.3 KB

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