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>
36 static int overflows_2
;
38 volatile long the_var
;
42 * Use ASM to ensure watchpoint and breakpoint can be triggered
45 #if defined (__x86_64__)
46 extern void __test_function(volatile long *ptr
);
48 ".globl __test_function\n"
53 static void __test_function(volatile long *ptr
)
59 static noinline
int test_function(void)
61 __test_function(&the_var
);
66 static void sig_handler_2(int signum __maybe_unused
,
67 siginfo_t
*oh __maybe_unused
,
68 void *uc __maybe_unused
)
71 if (overflows_2
> 10) {
72 ioctl(fd1
, PERF_EVENT_IOC_DISABLE
, 0);
73 ioctl(fd2
, PERF_EVENT_IOC_DISABLE
, 0);
74 ioctl(fd3
, PERF_EVENT_IOC_DISABLE
, 0);
78 static void sig_handler(int signum __maybe_unused
,
79 siginfo_t
*oh __maybe_unused
,
80 void *uc __maybe_unused
)
86 * This should be executed only once during
87 * this test, if we are here for the 10th
88 * time, consider this the recursive issue.
90 * We can get out of here by disable events,
91 * so no new SIGIO is delivered.
93 ioctl(fd1
, PERF_EVENT_IOC_DISABLE
, 0);
94 ioctl(fd2
, PERF_EVENT_IOC_DISABLE
, 0);
95 ioctl(fd3
, PERF_EVENT_IOC_DISABLE
, 0);
99 static int __event(bool is_x
, void *addr
, int sig
)
101 struct perf_event_attr pe
;
104 memset(&pe
, 0, sizeof(struct perf_event_attr
));
105 pe
.type
= PERF_TYPE_BREAKPOINT
;
106 pe
.size
= sizeof(struct perf_event_attr
);
109 pe
.bp_type
= is_x
? HW_BREAKPOINT_X
: HW_BREAKPOINT_W
;
110 pe
.bp_addr
= (unsigned long) addr
;
111 pe
.bp_len
= sizeof(long);
113 pe
.sample_period
= 1;
114 pe
.sample_type
= PERF_SAMPLE_IP
;
115 pe
.wakeup_events
= 1;
118 pe
.exclude_kernel
= 1;
121 fd
= sys_perf_event_open(&pe
, 0, -1, -1,
122 perf_event_open_cloexec_flag());
124 pr_debug("failed opening event %llx\n", pe
.config
);
128 fcntl(fd
, F_SETFL
, O_RDWR
|O_NONBLOCK
|O_ASYNC
);
129 fcntl(fd
, F_SETSIG
, sig
);
130 fcntl(fd
, F_SETOWN
, getpid());
132 ioctl(fd
, PERF_EVENT_IOC_RESET
, 0);
137 static int bp_event(void *addr
, int sig
)
139 return __event(true, addr
, sig
);
142 static int wp_event(void *addr
, int sig
)
144 return __event(false, addr
, sig
);
147 static long long bp_count(int fd
)
152 ret
= read(fd
, &count
, sizeof(long long));
153 if (ret
!= sizeof(long long)) {
154 pr_debug("failed to read: %d\n", ret
);
161 int test__bp_signal(struct test
*test __maybe_unused
, int subtest __maybe_unused
)
164 long long count1
, count2
, count3
;
166 /* setup SIGIO signal handler */
167 memset(&sa
, 0, sizeof(struct sigaction
));
168 sa
.sa_sigaction
= (void *) sig_handler
;
169 sa
.sa_flags
= SA_SIGINFO
;
171 if (sigaction(SIGIO
, &sa
, NULL
) < 0) {
172 pr_debug("failed setting up signal handler\n");
176 sa
.sa_sigaction
= (void *) sig_handler_2
;
177 if (sigaction(SIGUSR1
, &sa
, NULL
) < 0) {
178 pr_debug("failed setting up signal handler 2\n");
183 * We create following events:
185 * fd1 - breakpoint event on __test_function with SIGIO
186 * signal configured. We should get signal
187 * notification each time the breakpoint is hit
189 * fd2 - breakpoint event on sig_handler with SIGUSR1
190 * configured. We should get SIGUSR1 each time when
193 * fd3 - watchpoint event on __test_function with SIGIO
196 * Following processing should happen:
197 * Exec: Action: Result:
198 * incq (%rdi) - fd1 event breakpoint hit -> count1 == 1
199 * - SIGIO is delivered
200 * sig_handler - fd2 event breakpoint hit -> count2 == 1
201 * - SIGUSR1 is delivered
202 * sig_handler_2 -> overflows_2 == 1 (nested signal)
203 * sys_rt_sigreturn - return from sig_handler_2
204 * overflows++ -> overflows = 1
205 * sys_rt_sigreturn - return from sig_handler
206 * incq (%rdi) - fd3 event watchpoint hit -> count3 == 1 (wp and bp in one insn)
207 * - SIGIO is delivered
208 * sig_handler - fd2 event breakpoint hit -> count2 == 2
209 * - SIGUSR1 is delivered
210 * sig_handler_2 -> overflows_2 == 2 (nested signal)
211 * sys_rt_sigreturn - return from sig_handler_2
212 * overflows++ -> overflows = 2
213 * sys_rt_sigreturn - return from sig_handler
214 * the_var++ - fd3 event watchpoint hit -> count3 == 2 (standalone watchpoint)
215 * - SIGIO is delivered
216 * sig_handler - fd2 event breakpoint hit -> count2 == 3
217 * - SIGUSR1 is delivered
218 * sig_handler_2 -> overflows_2 == 3 (nested signal)
219 * sys_rt_sigreturn - return from sig_handler_2
220 * overflows++ -> overflows == 3
221 * sys_rt_sigreturn - return from sig_handler
223 * The test case check following error conditions:
224 * - we get stuck in signal handler because of debug
225 * exception being triggered receursively due to
226 * the wrong RF EFLAG management
228 * - we never trigger the sig_handler breakpoint due
229 * to the rong RF EFLAG management
233 fd1
= bp_event(__test_function
, SIGIO
);
234 fd2
= bp_event(sig_handler
, SIGUSR1
);
235 fd3
= wp_event((void *)&the_var
, SIGIO
);
237 ioctl(fd1
, PERF_EVENT_IOC_ENABLE
, 0);
238 ioctl(fd2
, PERF_EVENT_IOC_ENABLE
, 0);
239 ioctl(fd3
, PERF_EVENT_IOC_ENABLE
, 0);
242 * Kick off the test by trigering 'fd1'
247 ioctl(fd1
, PERF_EVENT_IOC_DISABLE
, 0);
248 ioctl(fd2
, PERF_EVENT_IOC_DISABLE
, 0);
249 ioctl(fd3
, PERF_EVENT_IOC_DISABLE
, 0);
251 count1
= bp_count(fd1
);
252 count2
= bp_count(fd2
);
253 count3
= bp_count(fd3
);
259 pr_debug("count1 %lld, count2 %lld, count3 %lld, overflow %d, overflows_2 %d\n",
260 count1
, count2
, count3
, overflows
, overflows_2
);
264 pr_debug("failed: RF EFLAG recursion issue detected\n");
266 pr_debug("failed: wrong count for bp1: %lld, expected 1\n", count1
);
270 pr_debug("failed: wrong overflow (%d) hit, expected 3\n", overflows
);
272 if (overflows_2
!= 3)
273 pr_debug("failed: wrong overflow_2 (%d) hit, expected 3\n", overflows_2
);
276 pr_debug("failed: wrong count for bp2 (%lld), expected 3\n", count2
);
279 pr_debug("failed: wrong count for bp3 (%lld), expected 2\n", count3
);
281 return count1
== 1 && overflows
== 3 && count2
== 3 && overflows_2
== 3 && count3
== 2 ?
285 bool test__bp_signal_is_supported(void)
288 * PowerPC and S390 do not support creation of instruction
289 * breakpoints using the perf_event interface.
291 * ARM requires explicit rounding down of the instruction
292 * pointer in Thumb mode, and then requires the single-step
293 * to be handled explicitly in the overflow handler to avoid
294 * stepping into the SIGIO handler and getting stuck on the
295 * breakpointed instruction.
297 * Since arm64 has the same issue with arm for the single-step
298 * handling, this case also gets stuck on the breakpointed
301 * Just disable the test for these architectures until these
302 * issues are resolved.
304 #if defined(__powerpc__) || defined(__s390x__) || defined(__arm__) || \