]> git.karo-electronics.de Git - karo-tx-linux.git/blob - tools/perf/tests/bp_signal.c
Merge tag 'stable/for-linus-3.13-rc4-tag' of git://git.kernel.org/pub/scm/linux/kerne...
[karo-tx-linux.git] / tools / perf / tests / bp_signal.c
1 /*
2  * Inspired by breakpoint overflow test done by
3  * Vince Weaver <vincent.weaver@maine.edu> for perf_event_tests
4  * (git://github.com/deater/perf_event_tests)
5  */
6
7 /*
8  * Powerpc needs __SANE_USERSPACE_TYPES__ before <linux/types.h> to select
9  * 'int-ll64.h' and avoid compile warnings when printing __u64 with %llu.
10  */
11 #define __SANE_USERSPACE_TYPES__
12
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <string.h>
17 #include <sys/ioctl.h>
18 #include <time.h>
19 #include <fcntl.h>
20 #include <signal.h>
21 #include <sys/mman.h>
22 #include <linux/compiler.h>
23 #include <linux/hw_breakpoint.h>
24
25 #include "tests.h"
26 #include "debug.h"
27 #include "perf.h"
28
29 static int fd1;
30 static int fd2;
31 static int overflows;
32
33 __attribute__ ((noinline))
34 static int test_function(void)
35 {
36         return time(NULL);
37 }
38
39 static void sig_handler(int signum __maybe_unused,
40                         siginfo_t *oh __maybe_unused,
41                         void *uc __maybe_unused)
42 {
43         overflows++;
44
45         if (overflows > 10) {
46                 /*
47                  * This should be executed only once during
48                  * this test, if we are here for the 10th
49                  * time, consider this the recursive issue.
50                  *
51                  * We can get out of here by disable events,
52                  * so no new SIGIO is delivered.
53                  */
54                 ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
55                 ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
56         }
57 }
58
59 static int bp_event(void *fn, int setup_signal)
60 {
61         struct perf_event_attr pe;
62         int fd;
63
64         memset(&pe, 0, sizeof(struct perf_event_attr));
65         pe.type = PERF_TYPE_BREAKPOINT;
66         pe.size = sizeof(struct perf_event_attr);
67
68         pe.config = 0;
69         pe.bp_type = HW_BREAKPOINT_X;
70         pe.bp_addr = (unsigned long) fn;
71         pe.bp_len = sizeof(long);
72
73         pe.sample_period = 1;
74         pe.sample_type = PERF_SAMPLE_IP;
75         pe.wakeup_events = 1;
76
77         pe.disabled = 1;
78         pe.exclude_kernel = 1;
79         pe.exclude_hv = 1;
80
81         fd = sys_perf_event_open(&pe, 0, -1, -1, 0);
82         if (fd < 0) {
83                 pr_debug("failed opening event %llx\n", pe.config);
84                 return TEST_FAIL;
85         }
86
87         if (setup_signal) {
88                 fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
89                 fcntl(fd, F_SETSIG, SIGIO);
90                 fcntl(fd, F_SETOWN, getpid());
91         }
92
93         ioctl(fd, PERF_EVENT_IOC_RESET, 0);
94
95         return fd;
96 }
97
98 static long long bp_count(int fd)
99 {
100         long long count;
101         int ret;
102
103         ret = read(fd, &count, sizeof(long long));
104         if (ret != sizeof(long long)) {
105                 pr_debug("failed to read: %d\n", ret);
106                 return TEST_FAIL;
107         }
108
109         return count;
110 }
111
112 int test__bp_signal(void)
113 {
114         struct sigaction sa;
115         long long count1, count2;
116
117         /* setup SIGIO signal handler */
118         memset(&sa, 0, sizeof(struct sigaction));
119         sa.sa_sigaction = (void *) sig_handler;
120         sa.sa_flags = SA_SIGINFO;
121
122         if (sigaction(SIGIO, &sa, NULL) < 0) {
123                 pr_debug("failed setting up signal handler\n");
124                 return TEST_FAIL;
125         }
126
127         /*
128          * We create following events:
129          *
130          * fd1 - breakpoint event on test_function with SIGIO
131          *       signal configured. We should get signal
132          *       notification each time the breakpoint is hit
133          *
134          * fd2 - breakpoint event on sig_handler without SIGIO
135          *       configured.
136          *
137          * Following processing should happen:
138          *   - execute test_function
139          *   - fd1 event breakpoint hit -> count1 == 1
140          *   - SIGIO is delivered       -> overflows == 1
141          *   - fd2 event breakpoint hit -> count2 == 1
142          *
143          * The test case check following error conditions:
144          * - we get stuck in signal handler because of debug
145          *   exception being triggered receursively due to
146          *   the wrong RF EFLAG management
147          *
148          * - we never trigger the sig_handler breakpoint due
149          *   to the rong RF EFLAG management
150          *
151          */
152
153         fd1 = bp_event(test_function, 1);
154         fd2 = bp_event(sig_handler, 0);
155
156         ioctl(fd1, PERF_EVENT_IOC_ENABLE, 0);
157         ioctl(fd2, PERF_EVENT_IOC_ENABLE, 0);
158
159         /*
160          * Kick off the test by trigering 'fd1'
161          * breakpoint.
162          */
163         test_function();
164
165         ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
166         ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
167
168         count1 = bp_count(fd1);
169         count2 = bp_count(fd2);
170
171         close(fd1);
172         close(fd2);
173
174         pr_debug("count1 %lld, count2 %lld, overflow %d\n",
175                  count1, count2, overflows);
176
177         if (count1 != 1) {
178                 if (count1 == 11)
179                         pr_debug("failed: RF EFLAG recursion issue detected\n");
180                 else
181                         pr_debug("failed: wrong count for bp1%lld\n", count1);
182         }
183
184         if (overflows != 1)
185                 pr_debug("failed: wrong overflow hit\n");
186
187         if (count2 != 1)
188                 pr_debug("failed: wrong count for bp2\n");
189
190         return count1 == 1 && overflows == 1 && count2 == 1 ?
191                 TEST_OK : TEST_FAIL;
192 }