1 // SPDX-License-Identifier: GPL-2.0
3 * Inspired by breakpoint overflow test done by
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"
52 #elif defined (__aarch64__)
53 extern void __test_function(volatile long *ptr);
55 ".globl __test_function\n"
61 static void __test_function(volatile long *ptr)
67 static noinline int test_function(void)
69 __test_function(&the_var);
74 static void sig_handler_2(int signum __maybe_unused,
75 siginfo_t *oh __maybe_unused,
76 void *uc __maybe_unused)
79 if (overflows_2 > 10) {
80 ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
81 ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
82 ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
86 static void sig_handler(int signum __maybe_unused,
87 siginfo_t *oh __maybe_unused,
88 void *uc __maybe_unused)
94 * This should be executed only once during
95 * this test, if we are here for the 10th
96 * time, consider this the recursive issue.
98 * We can get out of here by disable events,
99 * so no new SIGIO is delivered.
101 ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
102 ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
103 ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
107 static int __event(bool is_x, void *addr, int sig)
109 struct perf_event_attr pe;
112 memset(&pe, 0, sizeof(struct perf_event_attr));
113 pe.type = PERF_TYPE_BREAKPOINT;
114 pe.size = sizeof(struct perf_event_attr);
117 pe.bp_type = is_x ? HW_BREAKPOINT_X : HW_BREAKPOINT_W;
118 pe.bp_addr = (unsigned long) addr;
119 pe.bp_len = sizeof(long);
121 pe.sample_period = 1;
122 pe.sample_type = PERF_SAMPLE_IP;
123 pe.wakeup_events = 1;
126 pe.exclude_kernel = 1;
129 fd = sys_perf_event_open(&pe, 0, -1, -1,
130 perf_event_open_cloexec_flag());
132 pr_debug("failed opening event %llx\n", pe.config);
136 fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
137 fcntl(fd, F_SETSIG, sig);
138 fcntl(fd, F_SETOWN, getpid());
140 ioctl(fd, PERF_EVENT_IOC_RESET, 0);
145 static int bp_event(void *addr, int sig)
147 return __event(true, addr, sig);
150 static int wp_event(void *addr, int sig)
152 return __event(false, addr, sig);
155 static long long bp_count(int fd)
160 ret = read(fd, &count, sizeof(long long));
161 if (ret != sizeof(long long)) {
162 pr_debug("failed to read: %d\n", ret);
169 int test__bp_signal(struct test *test __maybe_unused, int subtest __maybe_unused)
172 long long count1, count2, count3;
174 /* setup SIGIO signal handler */
175 memset(&sa, 0, sizeof(struct sigaction));
176 sa.sa_sigaction = (void *) sig_handler;
177 sa.sa_flags = SA_SIGINFO;
179 if (sigaction(SIGIO, &sa, NULL) < 0) {
180 pr_debug("failed setting up signal handler\n");
184 sa.sa_sigaction = (void *) sig_handler_2;
185 if (sigaction(SIGUSR1, &sa, NULL) < 0) {
186 pr_debug("failed setting up signal handler 2\n");
191 * We create following events:
193 * fd1 - breakpoint event on __test_function with SIGIO
194 * signal configured. We should get signal
195 * notification each time the breakpoint is hit
197 * fd2 - breakpoint event on sig_handler with SIGUSR1
198 * configured. We should get SIGUSR1 each time when
201 * fd3 - watchpoint event on __test_function with SIGIO
204 * Following processing should happen:
205 * Exec: Action: Result:
206 * incq (%rdi) - fd1 event breakpoint hit -> count1 == 1
207 * - SIGIO is delivered
208 * sig_handler - fd2 event breakpoint hit -> count2 == 1
209 * - SIGUSR1 is delivered
210 * sig_handler_2 -> overflows_2 == 1 (nested signal)
211 * sys_rt_sigreturn - return from sig_handler_2
212 * overflows++ -> overflows = 1
213 * sys_rt_sigreturn - return from sig_handler
214 * incq (%rdi) - fd3 event watchpoint hit -> count3 == 1 (wp and bp in one insn)
215 * - SIGIO is delivered
216 * sig_handler - fd2 event breakpoint hit -> count2 == 2
217 * - SIGUSR1 is delivered
218 * sig_handler_2 -> overflows_2 == 2 (nested signal)
219 * sys_rt_sigreturn - return from sig_handler_2
220 * overflows++ -> overflows = 2
221 * sys_rt_sigreturn - return from sig_handler
222 * the_var++ - fd3 event watchpoint hit -> count3 == 2 (standalone watchpoint)
223 * - SIGIO is delivered
224 * sig_handler - fd2 event breakpoint hit -> count2 == 3
225 * - SIGUSR1 is delivered
226 * sig_handler_2 -> overflows_2 == 3 (nested signal)
227 * sys_rt_sigreturn - return from sig_handler_2
228 * overflows++ -> overflows == 3
229 * sys_rt_sigreturn - return from sig_handler
231 * The test case check following error conditions:
232 * - we get stuck in signal handler because of debug
233 * exception being triggered receursively due to
234 * the wrong RF EFLAG management
236 * - we never trigger the sig_handler breakpoint due
237 * to the rong RF EFLAG management
241 fd1 = bp_event(__test_function, SIGIO);
242 fd2 = bp_event(sig_handler, SIGUSR1);
243 fd3 = wp_event((void *)&the_var, SIGIO);
245 ioctl(fd1, PERF_EVENT_IOC_ENABLE, 0);
246 ioctl(fd2, PERF_EVENT_IOC_ENABLE, 0);
247 ioctl(fd3, PERF_EVENT_IOC_ENABLE, 0);
250 * Kick off the test by trigering 'fd1'
255 ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
256 ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
257 ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
259 count1 = bp_count(fd1);
260 count2 = bp_count(fd2);
261 count3 = bp_count(fd3);
267 pr_debug("count1 %lld, count2 %lld, count3 %lld, overflow %d, overflows_2 %d\n",
268 count1, count2, count3, overflows, overflows_2);
272 pr_debug("failed: RF EFLAG recursion issue detected\n");
274 pr_debug("failed: wrong count for bp1%lld\n", count1);
278 pr_debug("failed: wrong overflow hit\n");
280 if (overflows_2 != 3)
281 pr_debug("failed: wrong overflow_2 hit\n");
284 pr_debug("failed: wrong count for bp2\n");
287 pr_debug("failed: wrong count for bp3\n");
289 return count1 == 1 && overflows == 3 && count2 == 3 && overflows_2 == 3 && count3 == 2 ?
293 bool test__bp_signal_is_supported(void)
296 * PowerPC and S390 do not support creation of instruction
297 * breakpoints using the perf_event interface.
299 * ARM requires explicit rounding down of the instruction
300 * pointer in Thumb mode, and then requires the single-step
301 * to be handled explicitly in the overflow handler to avoid
302 * stepping into the SIGIO handler and getting stuck on the
303 * breakpointed instruction.
305 * Just disable the test for these architectures until these
306 * issues are resolved.
308 #if defined(__powerpc__) || defined(__s390x__) || defined(__arm__)