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)
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>
33 __attribute__ ((noinline
))
34 static int test_function(void)
39 static void sig_handler(int signum __maybe_unused
,
40 siginfo_t
*oh __maybe_unused
,
41 void *uc __maybe_unused
)
47 * This should be executed only once during
48 * this test, if we are here for the 10th
49 * time, consider this the recursive issue.
51 * We can get out of here by disable events,
52 * so no new SIGIO is delivered.
54 ioctl(fd1
, PERF_EVENT_IOC_DISABLE
, 0);
55 ioctl(fd2
, PERF_EVENT_IOC_DISABLE
, 0);
59 static int bp_event(void *fn
, int setup_signal
)
61 struct perf_event_attr pe
;
64 memset(&pe
, 0, sizeof(struct perf_event_attr
));
65 pe
.type
= PERF_TYPE_BREAKPOINT
;
66 pe
.size
= sizeof(struct perf_event_attr
);
69 pe
.bp_type
= HW_BREAKPOINT_X
;
70 pe
.bp_addr
= (unsigned long) fn
;
71 pe
.bp_len
= sizeof(long);
74 pe
.sample_type
= PERF_SAMPLE_IP
;
78 pe
.exclude_kernel
= 1;
81 fd
= sys_perf_event_open(&pe
, 0, -1, -1, 0);
83 pr_debug("failed opening event %llx\n", pe
.config
);
88 fcntl(fd
, F_SETFL
, O_RDWR
|O_NONBLOCK
|O_ASYNC
);
89 fcntl(fd
, F_SETSIG
, SIGIO
);
90 fcntl(fd
, F_SETOWN
, getpid());
93 ioctl(fd
, PERF_EVENT_IOC_RESET
, 0);
98 static long long bp_count(int fd
)
103 ret
= read(fd
, &count
, sizeof(long long));
104 if (ret
!= sizeof(long long)) {
105 pr_debug("failed to read: %d\n", ret
);
112 int test__bp_signal(void)
115 long long count1
, count2
;
117 /* setup SIGIO signal handler */
118 memset(&sa
, 0, sizeof(struct sigaction
));
119 sa
.sa_sigaction
= (void *) sig_handler
;
120 sa
.sa_flags
= SA_SIGINFO
;
122 if (sigaction(SIGIO
, &sa
, NULL
) < 0) {
123 pr_debug("failed setting up signal handler\n");
128 * We create following events:
130 * fd1 - breakpoint event on test_function with SIGIO
131 * signal configured. We should get signal
132 * notification each time the breakpoint is hit
134 * fd2 - breakpoint event on sig_handler without SIGIO
137 * Following processing should happen:
138 * - execute test_function
139 * - fd1 event breakpoint hit -> count1 == 1
140 * - SIGIO is delivered -> overflows == 1
141 * - fd2 event breakpoint hit -> count2 == 1
143 * The test case check following error conditions:
144 * - we get stuck in signal handler because of debug
145 * exception being triggered receursively due to
146 * the wrong RF EFLAG management
148 * - we never trigger the sig_handler breakpoint due
149 * to the rong RF EFLAG management
153 fd1
= bp_event(test_function
, 1);
154 fd2
= bp_event(sig_handler
, 0);
156 ioctl(fd1
, PERF_EVENT_IOC_ENABLE
, 0);
157 ioctl(fd2
, PERF_EVENT_IOC_ENABLE
, 0);
160 * Kick off the test by trigering 'fd1'
165 ioctl(fd1
, PERF_EVENT_IOC_DISABLE
, 0);
166 ioctl(fd2
, PERF_EVENT_IOC_DISABLE
, 0);
168 count1
= bp_count(fd1
);
169 count2
= bp_count(fd2
);
174 pr_debug("count1 %lld, count2 %lld, overflow %d\n",
175 count1
, count2
, overflows
);
179 pr_debug("failed: RF EFLAG recursion issue detected\n");
181 pr_debug("failed: wrong count for bp1%lld\n", count1
);
185 pr_debug("failed: wrong overflow hit\n");
188 pr_debug("failed: wrong count for bp2\n");
190 return count1
== 1 && overflows
== 1 && count2
== 1 ?