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>
35 static int overflows_2;
37 volatile long the_var;
41 * Use ASM to ensure watchpoint and breakpoint can be triggered
44 #if defined (__x86_64__)
45 extern void __test_function(volatile long *ptr);
48 ".globl __test_function\n"
49 ".type __test_function, @function;"
55 static void __test_function(volatile long *ptr)
61 static noinline int test_function(void)
63 __test_function(&the_var);
68 static void sig_handler_2(int signum __maybe_unused,
69 siginfo_t *oh __maybe_unused,
70 void *uc __maybe_unused)
73 if (overflows_2 > 10) {
74 ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
75 ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
76 ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
80 static void sig_handler(int signum __maybe_unused,
81 siginfo_t *oh __maybe_unused,
82 void *uc __maybe_unused)
88 * This should be executed only once during
89 * this test, if we are here for the 10th
90 * time, consider this the recursive issue.
92 * We can get out of here by disable events,
93 * so no new SIGIO is delivered.
95 ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
96 ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
97 ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
101 static int __event(bool is_x, void *addr, int sig)
103 struct perf_event_attr pe;
106 memset(&pe, 0, sizeof(struct perf_event_attr));
107 pe.type = PERF_TYPE_BREAKPOINT;
108 pe.size = sizeof(struct perf_event_attr);
111 pe.bp_type = is_x ? HW_BREAKPOINT_X : HW_BREAKPOINT_W;
112 pe.bp_addr = (unsigned long) addr;
113 pe.bp_len = sizeof(long);
115 pe.sample_period = 1;
116 pe.sample_type = PERF_SAMPLE_IP;
117 pe.wakeup_events = 1;
120 pe.exclude_kernel = 1;
123 fd = sys_perf_event_open(&pe, 0, -1, -1,
124 perf_event_open_cloexec_flag());
126 pr_debug("failed opening event %llx\n", pe.config);
130 fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
131 fcntl(fd, F_SETSIG, sig);
132 fcntl(fd, F_SETOWN, getpid());
134 ioctl(fd, PERF_EVENT_IOC_RESET, 0);
139 static int bp_event(void *addr, int sig)
141 return __event(true, addr, sig);
144 static int wp_event(void *addr, int sig)
146 return __event(false, addr, sig);
149 static long long bp_count(int fd)
154 ret = read(fd, &count, sizeof(long long));
155 if (ret != sizeof(long long)) {
156 pr_debug("failed to read: %d\n", ret);
163 int test__bp_signal(struct test *test __maybe_unused, int subtest __maybe_unused)
166 long long count1, count2, count3;
168 /* setup SIGIO signal handler */
169 memset(&sa, 0, sizeof(struct sigaction));
170 sa.sa_sigaction = (void *) sig_handler;
171 sa.sa_flags = SA_SIGINFO;
173 if (sigaction(SIGIO, &sa, NULL) < 0) {
174 pr_debug("failed setting up signal handler\n");
178 sa.sa_sigaction = (void *) sig_handler_2;
179 if (sigaction(SIGUSR1, &sa, NULL) < 0) {
180 pr_debug("failed setting up signal handler 2\n");
185 * We create following events:
187 * fd1 - breakpoint event on __test_function with SIGIO
188 * signal configured. We should get signal
189 * notification each time the breakpoint is hit
191 * fd2 - breakpoint event on sig_handler with SIGUSR1
192 * configured. We should get SIGUSR1 each time when
195 * fd3 - watchpoint event on __test_function with SIGIO
198 * Following processing should happen:
199 * Exec: Action: Result:
200 * incq (%rdi) - fd1 event breakpoint hit -> count1 == 1
201 * - SIGIO is delivered
202 * sig_handler - fd2 event breakpoint hit -> count2 == 1
203 * - SIGUSR1 is delivered
204 * sig_handler_2 -> overflows_2 == 1 (nested signal)
205 * sys_rt_sigreturn - return from sig_handler_2
206 * overflows++ -> overflows = 1
207 * sys_rt_sigreturn - return from sig_handler
208 * incq (%rdi) - fd3 event watchpoint hit -> count3 == 1 (wp and bp in one insn)
209 * - SIGIO is delivered
210 * sig_handler - fd2 event breakpoint hit -> count2 == 2
211 * - SIGUSR1 is delivered
212 * sig_handler_2 -> overflows_2 == 2 (nested signal)
213 * sys_rt_sigreturn - return from sig_handler_2
214 * overflows++ -> overflows = 2
215 * sys_rt_sigreturn - return from sig_handler
216 * the_var++ - fd3 event watchpoint hit -> count3 == 2 (standalone watchpoint)
217 * - SIGIO is delivered
218 * sig_handler - fd2 event breakpoint hit -> count2 == 3
219 * - SIGUSR1 is delivered
220 * sig_handler_2 -> overflows_2 == 3 (nested signal)
221 * sys_rt_sigreturn - return from sig_handler_2
222 * overflows++ -> overflows == 3
223 * sys_rt_sigreturn - return from sig_handler
225 * The test case check following error conditions:
226 * - we get stuck in signal handler because of debug
227 * exception being triggered receursively due to
228 * the wrong RF EFLAG management
230 * - we never trigger the sig_handler breakpoint due
231 * to the rong RF EFLAG management
235 fd1 = bp_event(__test_function, SIGIO);
236 fd2 = bp_event(sig_handler, SIGUSR1);
237 fd3 = wp_event((void *)&the_var, SIGIO);
239 ioctl(fd1, PERF_EVENT_IOC_ENABLE, 0);
240 ioctl(fd2, PERF_EVENT_IOC_ENABLE, 0);
241 ioctl(fd3, PERF_EVENT_IOC_ENABLE, 0);
244 * Kick off the test by trigering 'fd1'
249 ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
250 ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
251 ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
253 count1 = bp_count(fd1);
254 count2 = bp_count(fd2);
255 count3 = bp_count(fd3);
261 pr_debug("count1 %lld, count2 %lld, count3 %lld, overflow %d, overflows_2 %d\n",
262 count1, count2, count3, overflows, overflows_2);
266 pr_debug("failed: RF EFLAG recursion issue detected\n");
268 pr_debug("failed: wrong count for bp1%lld\n", count1);
272 pr_debug("failed: wrong overflow hit\n");
274 if (overflows_2 != 3)
275 pr_debug("failed: wrong overflow_2 hit\n");
278 pr_debug("failed: wrong count for bp2\n");
281 pr_debug("failed: wrong count for bp3\n");
283 return count1 == 1 && overflows == 3 && count2 == 3 && overflows_2 == 3 && count3 == 2 ?
287 bool test__bp_signal_is_supported(void)
290 * PowerPC and S390 do not support creation of instruction
291 * breakpoints using the perf_event interface.
293 * ARM requires explicit rounding down of the instruction
294 * pointer in Thumb mode, and then requires the single-step
295 * to be handled explicitly in the overflow handler to avoid
296 * stepping into the SIGIO handler and getting stuck on the
297 * breakpointed instruction.
299 * Since arm64 has the same issue with arm for the single-step
300 * handling, this case also gets suck on the breakpointed
303 * Just disable the test for these architectures until these
304 * issues are resolved.
306 #if defined(__powerpc__) || defined(__s390x__) || defined(__arm__) || \