bp_signal.c 7.7 KB

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