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>
34 static int overflows_2
;
36 volatile long the_var
;
40 * Use ASM to ensure watchpoint and breakpoint can be triggered
43 #if defined (__x86_64__)
44 extern void __test_function(volatile long *ptr
);
46 ".globl __test_function\n"
50 #elif defined (__aarch64__)
51 extern void __test_function(volatile long *ptr
);
53 ".globl __test_function\n"
59 static void __test_function(volatile long *ptr
)
65 static noinline
int test_function(void)
67 __test_function(&the_var
);
72 static void sig_handler_2(int signum __maybe_unused
,
73 siginfo_t
*oh __maybe_unused
,
74 void *uc __maybe_unused
)
77 if (overflows_2
> 10) {
78 ioctl(fd1
, PERF_EVENT_IOC_DISABLE
, 0);
79 ioctl(fd2
, PERF_EVENT_IOC_DISABLE
, 0);
80 ioctl(fd3
, PERF_EVENT_IOC_DISABLE
, 0);
84 static void sig_handler(int signum __maybe_unused
,
85 siginfo_t
*oh __maybe_unused
,
86 void *uc __maybe_unused
)
92 * This should be executed only once during
93 * this test, if we are here for the 10th
94 * time, consider this the recursive issue.
96 * We can get out of here by disable events,
97 * so no new SIGIO is delivered.
99 ioctl(fd1
, PERF_EVENT_IOC_DISABLE
, 0);
100 ioctl(fd2
, PERF_EVENT_IOC_DISABLE
, 0);
101 ioctl(fd3
, PERF_EVENT_IOC_DISABLE
, 0);
105 static int __event(bool is_x
, void *addr
, int sig
)
107 struct perf_event_attr pe
;
110 memset(&pe
, 0, sizeof(struct perf_event_attr
));
111 pe
.type
= PERF_TYPE_BREAKPOINT
;
112 pe
.size
= sizeof(struct perf_event_attr
);
115 pe
.bp_type
= is_x
? HW_BREAKPOINT_X
: HW_BREAKPOINT_W
;
116 pe
.bp_addr
= (unsigned long) addr
;
117 pe
.bp_len
= sizeof(long);
119 pe
.sample_period
= 1;
120 pe
.sample_type
= PERF_SAMPLE_IP
;
121 pe
.wakeup_events
= 1;
124 pe
.exclude_kernel
= 1;
127 fd
= sys_perf_event_open(&pe
, 0, -1, -1,
128 perf_event_open_cloexec_flag());
130 pr_debug("failed opening event %llx\n", pe
.config
);
134 fcntl(fd
, F_SETFL
, O_RDWR
|O_NONBLOCK
|O_ASYNC
);
135 fcntl(fd
, F_SETSIG
, sig
);
136 fcntl(fd
, F_SETOWN
, getpid());
138 ioctl(fd
, PERF_EVENT_IOC_RESET
, 0);
143 static int bp_event(void *addr
, int sig
)
145 return __event(true, addr
, sig
);
148 static int wp_event(void *addr
, int sig
)
150 return __event(false, addr
, sig
);
153 static long long bp_count(int fd
)
158 ret
= read(fd
, &count
, sizeof(long long));
159 if (ret
!= sizeof(long long)) {
160 pr_debug("failed to read: %d\n", ret
);
167 int test__bp_signal(struct test
*test __maybe_unused
, int subtest __maybe_unused
)
170 long long count1
, count2
, count3
;
172 /* setup SIGIO signal handler */
173 memset(&sa
, 0, sizeof(struct sigaction
));
174 sa
.sa_sigaction
= (void *) sig_handler
;
175 sa
.sa_flags
= SA_SIGINFO
;
177 if (sigaction(SIGIO
, &sa
, NULL
) < 0) {
178 pr_debug("failed setting up signal handler\n");
182 sa
.sa_sigaction
= (void *) sig_handler_2
;
183 if (sigaction(SIGUSR1
, &sa
, NULL
) < 0) {
184 pr_debug("failed setting up signal handler 2\n");
189 * We create following events:
191 * fd1 - breakpoint event on __test_function with SIGIO
192 * signal configured. We should get signal
193 * notification each time the breakpoint is hit
195 * fd2 - breakpoint event on sig_handler with SIGUSR1
196 * configured. We should get SIGUSR1 each time when
199 * fd3 - watchpoint event on __test_function with SIGIO
202 * Following processing should happen:
203 * Exec: Action: Result:
204 * incq (%rdi) - fd1 event breakpoint hit -> count1 == 1
205 * - SIGIO is delivered
206 * sig_handler - fd2 event breakpoint hit -> count2 == 1
207 * - SIGUSR1 is delivered
208 * sig_handler_2 -> overflows_2 == 1 (nested signal)
209 * sys_rt_sigreturn - return from sig_handler_2
210 * overflows++ -> overflows = 1
211 * sys_rt_sigreturn - return from sig_handler
212 * incq (%rdi) - fd3 event watchpoint hit -> count3 == 1 (wp and bp in one insn)
213 * - SIGIO is delivered
214 * sig_handler - fd2 event breakpoint hit -> count2 == 2
215 * - SIGUSR1 is delivered
216 * sig_handler_2 -> overflows_2 == 2 (nested signal)
217 * sys_rt_sigreturn - return from sig_handler_2
218 * overflows++ -> overflows = 2
219 * sys_rt_sigreturn - return from sig_handler
220 * the_var++ - fd3 event watchpoint hit -> count3 == 2 (standalone watchpoint)
221 * - SIGIO is delivered
222 * sig_handler - fd2 event breakpoint hit -> count2 == 3
223 * - SIGUSR1 is delivered
224 * sig_handler_2 -> overflows_2 == 3 (nested signal)
225 * sys_rt_sigreturn - return from sig_handler_2
226 * overflows++ -> overflows == 3
227 * sys_rt_sigreturn - return from sig_handler
229 * The test case check following error conditions:
230 * - we get stuck in signal handler because of debug
231 * exception being triggered receursively due to
232 * the wrong RF EFLAG management
234 * - we never trigger the sig_handler breakpoint due
235 * to the rong RF EFLAG management
239 fd1
= bp_event(__test_function
, SIGIO
);
240 fd2
= bp_event(sig_handler
, SIGUSR1
);
241 fd3
= wp_event((void *)&the_var
, SIGIO
);
243 ioctl(fd1
, PERF_EVENT_IOC_ENABLE
, 0);
244 ioctl(fd2
, PERF_EVENT_IOC_ENABLE
, 0);
245 ioctl(fd3
, PERF_EVENT_IOC_ENABLE
, 0);
248 * Kick off the test by trigering 'fd1'
253 ioctl(fd1
, PERF_EVENT_IOC_DISABLE
, 0);
254 ioctl(fd2
, PERF_EVENT_IOC_DISABLE
, 0);
255 ioctl(fd3
, PERF_EVENT_IOC_DISABLE
, 0);
257 count1
= bp_count(fd1
);
258 count2
= bp_count(fd2
);
259 count3
= bp_count(fd3
);
265 pr_debug("count1 %lld, count2 %lld, count3 %lld, overflow %d, overflows_2 %d\n",
266 count1
, count2
, count3
, overflows
, overflows_2
);
270 pr_debug("failed: RF EFLAG recursion issue detected\n");
272 pr_debug("failed: wrong count for bp1%lld\n", count1
);
276 pr_debug("failed: wrong overflow hit\n");
278 if (overflows_2
!= 3)
279 pr_debug("failed: wrong overflow_2 hit\n");
282 pr_debug("failed: wrong count for bp2\n");
285 pr_debug("failed: wrong count for bp3\n");
287 return count1
== 1 && overflows
== 3 && count2
== 3 && overflows_2
== 3 && count3
== 2 ?
291 bool test__bp_signal_is_supported(void)
294 * The powerpc so far does not have support to even create
295 * instruction breakpoint using the perf event interface.
296 * Once it's there we can release this.