1 // SPDX-License-Identifier: GPL-2.0
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)
9 * Powerpc needs __SANE_USERSPACE_TYPES__ before <linux/types.h> to select
10 * 'int-ll64.h' and avoid compile warnings when printing __u64 with %llu.
12 #define __SANE_USERSPACE_TYPES__
18 #include <sys/ioctl.h>
23 #include <linux/compiler.h>
24 #include <linux/hw_breakpoint.h>
29 #include "parse-events.h"
37 static int overflows_2
;
39 volatile long the_var
;
43 * Use ASM to ensure watchpoint and breakpoint can be triggered
46 #if defined (__x86_64__)
47 extern void __test_function(volatile long *ptr
);
50 ".globl __test_function\n"
51 ".type __test_function, @function;"
57 static void __test_function(volatile long *ptr
)
63 static noinline
int test_function(void)
65 __test_function(&the_var
);
70 static void sig_handler_2(int signum __maybe_unused
,
71 siginfo_t
*oh __maybe_unused
,
72 void *uc __maybe_unused
)
75 if (overflows_2
> 10) {
76 ioctl(fd1
, PERF_EVENT_IOC_DISABLE
, 0);
77 ioctl(fd2
, PERF_EVENT_IOC_DISABLE
, 0);
78 ioctl(fd3
, PERF_EVENT_IOC_DISABLE
, 0);
82 static void sig_handler(int signum __maybe_unused
,
83 siginfo_t
*oh __maybe_unused
,
84 void *uc __maybe_unused
)
90 * This should be executed only once during
91 * this test, if we are here for the 10th
92 * time, consider this the recursive issue.
94 * We can get out of here by disable events,
95 * so no new SIGIO is delivered.
97 ioctl(fd1
, PERF_EVENT_IOC_DISABLE
, 0);
98 ioctl(fd2
, PERF_EVENT_IOC_DISABLE
, 0);
99 ioctl(fd3
, PERF_EVENT_IOC_DISABLE
, 0);
103 static int __event(bool is_x
, void *addr
, int sig
)
105 struct perf_event_attr pe
;
108 memset(&pe
, 0, sizeof(struct perf_event_attr
));
109 pe
.type
= PERF_TYPE_BREAKPOINT
;
110 pe
.size
= sizeof(struct perf_event_attr
);
113 pe
.bp_type
= is_x
? HW_BREAKPOINT_X
: HW_BREAKPOINT_W
;
114 pe
.bp_addr
= (unsigned long) addr
;
115 pe
.bp_len
= is_x
? default_breakpoint_len() : sizeof(long);
117 pe
.sample_period
= 1;
118 pe
.sample_type
= PERF_SAMPLE_IP
;
119 pe
.wakeup_events
= 1;
122 pe
.exclude_kernel
= 1;
125 fd
= sys_perf_event_open(&pe
, 0, -1, -1,
126 perf_event_open_cloexec_flag());
128 pr_debug("failed opening event %llx\n", pe
.config
);
132 fcntl(fd
, F_SETFL
, O_RDWR
|O_NONBLOCK
|O_ASYNC
);
133 fcntl(fd
, F_SETSIG
, sig
);
134 fcntl(fd
, F_SETOWN
, getpid());
136 ioctl(fd
, PERF_EVENT_IOC_RESET
, 0);
141 static int bp_event(void *addr
, int sig
)
143 return __event(true, addr
, sig
);
146 static int wp_event(void *addr
, int sig
)
148 return __event(false, addr
, sig
);
151 static long long bp_count(int fd
)
156 ret
= read(fd
, &count
, sizeof(long long));
157 if (ret
!= sizeof(long long)) {
158 pr_debug("failed to read: %d\n", ret
);
165 static int test__bp_signal(struct test_suite
*test __maybe_unused
, int subtest __maybe_unused
)
168 long long count1
, count2
, count3
;
170 if (!BP_SIGNAL_IS_SUPPORTED
) {
171 pr_debug("Test not supported on this architecture");
175 /* setup SIGIO signal handler */
176 memset(&sa
, 0, sizeof(struct sigaction
));
177 sa
.sa_sigaction
= (void *) sig_handler
;
178 sa
.sa_flags
= SA_SIGINFO
;
180 if (sigaction(SIGIO
, &sa
, NULL
) < 0) {
181 pr_debug("failed setting up signal handler\n");
185 sa
.sa_sigaction
= (void *) sig_handler_2
;
186 if (sigaction(SIGUSR1
, &sa
, NULL
) < 0) {
187 pr_debug("failed setting up signal handler 2\n");
192 * We create following events:
194 * fd1 - breakpoint event on __test_function with SIGIO
195 * signal configured. We should get signal
196 * notification each time the breakpoint is hit
198 * fd2 - breakpoint event on sig_handler with SIGUSR1
199 * configured. We should get SIGUSR1 each time when
202 * fd3 - watchpoint event on __test_function with SIGIO
205 * Following processing should happen:
206 * Exec: Action: Result:
207 * incq (%rdi) - fd1 event breakpoint hit -> count1 == 1
208 * - SIGIO is delivered
209 * sig_handler - fd2 event breakpoint hit -> count2 == 1
210 * - SIGUSR1 is delivered
211 * sig_handler_2 -> overflows_2 == 1 (nested signal)
212 * sys_rt_sigreturn - return from sig_handler_2
213 * overflows++ -> overflows = 1
214 * sys_rt_sigreturn - return from sig_handler
215 * incq (%rdi) - fd3 event watchpoint hit -> count3 == 1 (wp and bp in one insn)
216 * - SIGIO is delivered
217 * sig_handler - fd2 event breakpoint hit -> count2 == 2
218 * - SIGUSR1 is delivered
219 * sig_handler_2 -> overflows_2 == 2 (nested signal)
220 * sys_rt_sigreturn - return from sig_handler_2
221 * overflows++ -> overflows = 2
222 * sys_rt_sigreturn - return from sig_handler
223 * the_var++ - fd3 event watchpoint hit -> count3 == 2 (standalone watchpoint)
224 * - SIGIO is delivered
225 * sig_handler - fd2 event breakpoint hit -> count2 == 3
226 * - SIGUSR1 is delivered
227 * sig_handler_2 -> overflows_2 == 3 (nested signal)
228 * sys_rt_sigreturn - return from sig_handler_2
229 * overflows++ -> overflows == 3
230 * sys_rt_sigreturn - return from sig_handler
232 * The test case check following error conditions:
233 * - we get stuck in signal handler because of debug
234 * exception being triggered recursively due to
235 * the wrong RF EFLAG management
237 * - we never trigger the sig_handler breakpoint due
238 * to the wrong RF EFLAG management
242 fd1
= bp_event(__test_function
, SIGIO
);
243 fd2
= bp_event(sig_handler
, SIGUSR1
);
244 fd3
= wp_event((void *)&the_var
, SIGIO
);
246 ioctl(fd1
, PERF_EVENT_IOC_ENABLE
, 0);
247 ioctl(fd2
, PERF_EVENT_IOC_ENABLE
, 0);
248 ioctl(fd3
, PERF_EVENT_IOC_ENABLE
, 0);
251 * Kick off the test by triggering 'fd1'
256 ioctl(fd1
, PERF_EVENT_IOC_DISABLE
, 0);
257 ioctl(fd2
, PERF_EVENT_IOC_DISABLE
, 0);
258 ioctl(fd3
, PERF_EVENT_IOC_DISABLE
, 0);
260 count1
= bp_count(fd1
);
261 count2
= bp_count(fd2
);
262 count3
= bp_count(fd3
);
268 pr_debug("count1 %lld, count2 %lld, count3 %lld, overflow %d, overflows_2 %d\n",
269 count1
, count2
, count3
, overflows
, overflows_2
);
273 pr_debug("failed: RF EFLAG recursion issue detected\n");
275 pr_debug("failed: wrong count for bp1: %lld, expected 1\n", count1
);
279 pr_debug("failed: wrong overflow (%d) hit, expected 3\n", overflows
);
281 if (overflows_2
!= 3)
282 pr_debug("failed: wrong overflow_2 (%d) hit, expected 3\n", overflows_2
);
285 pr_debug("failed: wrong count for bp2 (%lld), expected 3\n", count2
);
288 pr_debug("failed: wrong count for bp3 (%lld), expected 2\n", count3
);
290 return count1
== 1 && overflows
== 3 && count2
== 3 && overflows_2
== 3 && count3
== 2 ?
294 DEFINE_SUITE("Breakpoint overflow signal handler", bp_signal
);