GNU Linux-libre 4.19.264-gnu1
[releases.git] / tools / perf / tests / bp_signal.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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)
6  */
7
8 /*
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.
11  */
12 #define __SANE_USERSPACE_TYPES__
13
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <unistd.h>
17 #include <string.h>
18 #include <sys/ioctl.h>
19 #include <time.h>
20 #include <fcntl.h>
21 #include <signal.h>
22 #include <sys/mman.h>
23 #include <linux/compiler.h>
24 #include <linux/hw_breakpoint.h>
25
26 #include "tests.h"
27 #include "debug.h"
28 #include "perf.h"
29 #include "cloexec.h"
30
31 static int fd1;
32 static int fd2;
33 static int fd3;
34 static int overflows;
35 static int overflows_2;
36
37 volatile long the_var;
38
39
40 /*
41  * Use ASM to ensure watchpoint and breakpoint can be triggered
42  * at one instruction.
43  */
44 #if defined (__x86_64__)
45 extern void __test_function(volatile long *ptr);
46 asm (
47         ".pushsection .text;"
48         ".globl __test_function\n"
49         ".type __test_function, @function;"
50         "__test_function:\n"
51         "incq (%rdi)\n"
52         "ret\n"
53         ".popsection\n");
54 #else
55 static void __test_function(volatile long *ptr)
56 {
57         *ptr = 0x1234;
58 }
59 #endif
60
61 static noinline int test_function(void)
62 {
63         __test_function(&the_var);
64         the_var++;
65         return time(NULL);
66 }
67
68 static void sig_handler_2(int signum __maybe_unused,
69                           siginfo_t *oh __maybe_unused,
70                           void *uc __maybe_unused)
71 {
72         overflows_2++;
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);
77         }
78 }
79
80 static void sig_handler(int signum __maybe_unused,
81                         siginfo_t *oh __maybe_unused,
82                         void *uc __maybe_unused)
83 {
84         overflows++;
85
86         if (overflows > 10) {
87                 /*
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.
91                  *
92                  * We can get out of here by disable events,
93                  * so no new SIGIO is delivered.
94                  */
95                 ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
96                 ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
97                 ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
98         }
99 }
100
101 static int __event(bool is_x, void *addr, int sig)
102 {
103         struct perf_event_attr pe;
104         int fd;
105
106         memset(&pe, 0, sizeof(struct perf_event_attr));
107         pe.type = PERF_TYPE_BREAKPOINT;
108         pe.size = sizeof(struct perf_event_attr);
109
110         pe.config = 0;
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);
114
115         pe.sample_period = 1;
116         pe.sample_type = PERF_SAMPLE_IP;
117         pe.wakeup_events = 1;
118
119         pe.disabled = 1;
120         pe.exclude_kernel = 1;
121         pe.exclude_hv = 1;
122
123         fd = sys_perf_event_open(&pe, 0, -1, -1,
124                                  perf_event_open_cloexec_flag());
125         if (fd < 0) {
126                 pr_debug("failed opening event %llx\n", pe.config);
127                 return TEST_FAIL;
128         }
129
130         fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
131         fcntl(fd, F_SETSIG, sig);
132         fcntl(fd, F_SETOWN, getpid());
133
134         ioctl(fd, PERF_EVENT_IOC_RESET, 0);
135
136         return fd;
137 }
138
139 static int bp_event(void *addr, int sig)
140 {
141         return __event(true, addr, sig);
142 }
143
144 static int wp_event(void *addr, int sig)
145 {
146         return __event(false, addr, sig);
147 }
148
149 static long long bp_count(int fd)
150 {
151         long long count;
152         int ret;
153
154         ret = read(fd, &count, sizeof(long long));
155         if (ret != sizeof(long long)) {
156                 pr_debug("failed to read: %d\n", ret);
157                 return TEST_FAIL;
158         }
159
160         return count;
161 }
162
163 int test__bp_signal(struct test *test __maybe_unused, int subtest __maybe_unused)
164 {
165         struct sigaction sa;
166         long long count1, count2, count3;
167
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;
172
173         if (sigaction(SIGIO, &sa, NULL) < 0) {
174                 pr_debug("failed setting up signal handler\n");
175                 return TEST_FAIL;
176         }
177
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");
181                 return TEST_FAIL;
182         }
183
184         /*
185          * We create following events:
186          *
187          * fd1 - breakpoint event on __test_function with SIGIO
188          *       signal configured. We should get signal
189          *       notification each time the breakpoint is hit
190          *
191          * fd2 - breakpoint event on sig_handler with SIGUSR1
192          *       configured. We should get SIGUSR1 each time when
193          *       breakpoint is hit
194          *
195          * fd3 - watchpoint event on __test_function with SIGIO
196          *       configured.
197          *
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
224          *
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
229          *
230          * - we never trigger the sig_handler breakpoint due
231          *   to the rong RF EFLAG management
232          *
233          */
234
235         fd1 = bp_event(__test_function, SIGIO);
236         fd2 = bp_event(sig_handler, SIGUSR1);
237         fd3 = wp_event((void *)&the_var, SIGIO);
238
239         ioctl(fd1, PERF_EVENT_IOC_ENABLE, 0);
240         ioctl(fd2, PERF_EVENT_IOC_ENABLE, 0);
241         ioctl(fd3, PERF_EVENT_IOC_ENABLE, 0);
242
243         /*
244          * Kick off the test by trigering 'fd1'
245          * breakpoint.
246          */
247         test_function();
248
249         ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
250         ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
251         ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
252
253         count1 = bp_count(fd1);
254         count2 = bp_count(fd2);
255         count3 = bp_count(fd3);
256
257         close(fd1);
258         close(fd2);
259         close(fd3);
260
261         pr_debug("count1 %lld, count2 %lld, count3 %lld, overflow %d, overflows_2 %d\n",
262                  count1, count2, count3, overflows, overflows_2);
263
264         if (count1 != 1) {
265                 if (count1 == 11)
266                         pr_debug("failed: RF EFLAG recursion issue detected\n");
267                 else
268                         pr_debug("failed: wrong count for bp1%lld\n", count1);
269         }
270
271         if (overflows != 3)
272                 pr_debug("failed: wrong overflow hit\n");
273
274         if (overflows_2 != 3)
275                 pr_debug("failed: wrong overflow_2 hit\n");
276
277         if (count2 != 3)
278                 pr_debug("failed: wrong count for bp2\n");
279
280         if (count3 != 2)
281                 pr_debug("failed: wrong count for bp3\n");
282
283         return count1 == 1 && overflows == 3 && count2 == 3 && overflows_2 == 3 && count3 == 2 ?
284                 TEST_OK : TEST_FAIL;
285 }
286
287 bool test__bp_signal_is_supported(void)
288 {
289         /*
290          * PowerPC and S390 do not support creation of instruction
291          * breakpoints using the perf_event interface.
292          *
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.
298          *
299          * Since arm64 has the same issue with arm for the single-step
300          * handling, this case also gets suck on the breakpointed
301          * instruction.
302          *
303          * Just disable the test for these architectures until these
304          * issues are resolved.
305          */
306 #if defined(__powerpc__) || defined(__s390x__) || defined(__arm__) || \
307     defined(__aarch64__)
308         return false;
309 #else
310         return true;
311 #endif
312 }