2 * Inspired by breakpoint overflow test done by
4 * (git://github.com/deater/perf_event_tests)
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.
11 #define __SANE_USERSPACE_TYPES__
17 #include <sys/ioctl.h>
22 #include <linux/compiler.h>
23 #include <linux/hw_breakpoint.h>
34 __attribute__ ((noinline))
35 static int test_function(void)
40 static void sig_handler(int signum __maybe_unused,
41 siginfo_t *oh __maybe_unused,
42 void *uc __maybe_unused)
48 * This should be executed only once during
49 * this test, if we are here for the 10th
50 * time, consider this the recursive issue.
52 * We can get out of here by disable events,
53 * so no new SIGIO is delivered.
55 ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
56 ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
60 static int bp_event(void *fn, int setup_signal)
62 struct perf_event_attr pe;
65 memset(&pe, 0, sizeof(struct perf_event_attr));
66 pe.type = PERF_TYPE_BREAKPOINT;
67 pe.size = sizeof(struct perf_event_attr);
70 pe.bp_type = HW_BREAKPOINT_X;
71 pe.bp_addr = (unsigned long) fn;
72 pe.bp_len = sizeof(long);
75 pe.sample_type = PERF_SAMPLE_IP;
79 pe.exclude_kernel = 1;
82 fd = sys_perf_event_open(&pe, 0, -1, -1,
83 perf_event_open_cloexec_flag());
85 pr_debug("failed opening event %llx\n", pe.config);
90 fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
91 fcntl(fd, F_SETSIG, SIGIO);
92 fcntl(fd, F_SETOWN, getpid());
95 ioctl(fd, PERF_EVENT_IOC_RESET, 0);
100 static long long bp_count(int fd)
105 ret = read(fd, &count, sizeof(long long));
106 if (ret != sizeof(long long)) {
107 pr_debug("failed to read: %d\n", ret);
114 int test__bp_signal(int subtest __maybe_unused)
117 long long count1, count2;
119 /* setup SIGIO signal handler */
120 memset(&sa, 0, sizeof(struct sigaction));
121 sa.sa_sigaction = (void *) sig_handler;
122 sa.sa_flags = SA_SIGINFO;
124 if (sigaction(SIGIO, &sa, NULL) < 0) {
125 pr_debug("failed setting up signal handler\n");
130 * We create following events:
132 * fd1 - breakpoint event on test_function with SIGIO
133 * signal configured. We should get signal
134 * notification each time the breakpoint is hit
136 * fd2 - breakpoint event on sig_handler without SIGIO
139 * Following processing should happen:
140 * - execute test_function
141 * - fd1 event breakpoint hit -> count1 == 1
142 * - SIGIO is delivered -> overflows == 1
143 * - fd2 event breakpoint hit -> count2 == 1
145 * The test case check following error conditions:
146 * - we get stuck in signal handler because of debug
147 * exception being triggered receursively due to
148 * the wrong RF EFLAG management
150 * - we never trigger the sig_handler breakpoint due
151 * to the rong RF EFLAG management
155 fd1 = bp_event(test_function, 1);
156 fd2 = bp_event(sig_handler, 0);
158 ioctl(fd1, PERF_EVENT_IOC_ENABLE, 0);
159 ioctl(fd2, PERF_EVENT_IOC_ENABLE, 0);
162 * Kick off the test by trigering 'fd1'
167 ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
168 ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
170 count1 = bp_count(fd1);
171 count2 = bp_count(fd2);
176 pr_debug("count1 %lld, count2 %lld, overflow %d\n",
177 count1, count2, overflows);
181 pr_debug("failed: RF EFLAG recursion issue detected\n");
183 pr_debug("failed: wrong count for bp1%lld\n", count1);
187 pr_debug("failed: wrong overflow hit\n");
190 pr_debug("failed: wrong count for bp2\n");
192 return count1 == 1 && overflows == 1 && count2 == 1 ?