blob: 39bbb97cd30aac7704d460e0093c1d82769facf5 [file] [log] [blame]
Jiri Olsa5a6bef42013-03-10 19:41:10 +01001/*
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
Sukadev Bhattiprolub3539d22013-04-26 10:17:56 -07007/*
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
Jiri Olsa5a6bef42013-03-10 19:41:10 +010013#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"
Yann Droneaud57480d22014-06-30 22:28:47 +020028#include "cloexec.h"
Jiri Olsa5a6bef42013-03-10 19:41:10 +010029
30static int fd1;
31static int fd2;
Wang Nan8fd34e12016-01-25 09:55:55 +000032static int fd3;
Jiri Olsa5a6bef42013-03-10 19:41:10 +010033static int overflows;
Wang Nan8fd34e12016-01-25 09:55:55 +000034static int overflows_2;
35
36volatile long the_var;
37
38
39/*
40 * Use ASM to ensure watchpoint and breakpoint can be triggered
41 * at one instruction.
42 */
43#if defined (__x86_64__)
44extern void __test_function(volatile long *ptr);
45asm (
46 ".globl __test_function\n"
47 "__test_function:\n"
48 "incq (%rdi)\n"
49 "ret\n");
50#elif defined (__aarch64__)
51extern void __test_function(volatile long *ptr);
52asm (
53 ".globl __test_function\n"
54 "__test_function:\n"
55 "str x30, [x0]\n"
56 "ret\n");
57
58#else
59static void __test_function(volatile long *ptr)
60{
61 *ptr = 0x1234;
62}
63#endif
Jiri Olsa5a6bef42013-03-10 19:41:10 +010064
Arnaldo Carvalho de Melo9dd4ca42017-06-16 11:39:15 -030065static noinline int test_function(void)
Jiri Olsa5a6bef42013-03-10 19:41:10 +010066{
Wang Nan8fd34e12016-01-25 09:55:55 +000067 __test_function(&the_var);
68 the_var++;
Jiri Olsa5a6bef42013-03-10 19:41:10 +010069 return time(NULL);
70}
71
Wang Nan8fd34e12016-01-25 09:55:55 +000072static void sig_handler_2(int signum __maybe_unused,
73 siginfo_t *oh __maybe_unused,
74 void *uc __maybe_unused)
75{
76 overflows_2++;
77 if (overflows_2 > 10) {
78 ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
79 ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
80 ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
81 }
82}
83
Jiri Olsa5a6bef42013-03-10 19:41:10 +010084static void sig_handler(int signum __maybe_unused,
85 siginfo_t *oh __maybe_unused,
86 void *uc __maybe_unused)
87{
88 overflows++;
89
90 if (overflows > 10) {
91 /*
92 * This should be executed only once during
93 * this test, if we are here for the 10th
94 * time, consider this the recursive issue.
95 *
96 * We can get out of here by disable events,
97 * so no new SIGIO is delivered.
98 */
99 ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
100 ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
Wang Nan8fd34e12016-01-25 09:55:55 +0000101 ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
Jiri Olsa5a6bef42013-03-10 19:41:10 +0100102 }
103}
104
Arnaldo Carvalho de Melo1ad826b2016-02-12 18:30:01 -0300105static int __event(bool is_x, void *addr, int sig)
Jiri Olsa5a6bef42013-03-10 19:41:10 +0100106{
107 struct perf_event_attr pe;
108 int fd;
109
110 memset(&pe, 0, sizeof(struct perf_event_attr));
111 pe.type = PERF_TYPE_BREAKPOINT;
112 pe.size = sizeof(struct perf_event_attr);
113
114 pe.config = 0;
Wang Nan8fd34e12016-01-25 09:55:55 +0000115 pe.bp_type = is_x ? HW_BREAKPOINT_X : HW_BREAKPOINT_W;
116 pe.bp_addr = (unsigned long) addr;
Jiri Olsa5a6bef42013-03-10 19:41:10 +0100117 pe.bp_len = sizeof(long);
118
119 pe.sample_period = 1;
120 pe.sample_type = PERF_SAMPLE_IP;
121 pe.wakeup_events = 1;
122
123 pe.disabled = 1;
124 pe.exclude_kernel = 1;
125 pe.exclude_hv = 1;
126
Yann Droneaud57480d22014-06-30 22:28:47 +0200127 fd = sys_perf_event_open(&pe, 0, -1, -1,
128 perf_event_open_cloexec_flag());
Jiri Olsa5a6bef42013-03-10 19:41:10 +0100129 if (fd < 0) {
130 pr_debug("failed opening event %llx\n", pe.config);
131 return TEST_FAIL;
132 }
133
Wang Nan8fd34e12016-01-25 09:55:55 +0000134 fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
Arnaldo Carvalho de Melo1ad826b2016-02-12 18:30:01 -0300135 fcntl(fd, F_SETSIG, sig);
Wang Nan8fd34e12016-01-25 09:55:55 +0000136 fcntl(fd, F_SETOWN, getpid());
Jiri Olsa5a6bef42013-03-10 19:41:10 +0100137
138 ioctl(fd, PERF_EVENT_IOC_RESET, 0);
139
140 return fd;
141}
142
Arnaldo Carvalho de Melo1ad826b2016-02-12 18:30:01 -0300143static int bp_event(void *addr, int sig)
Wang Nan8fd34e12016-01-25 09:55:55 +0000144{
Arnaldo Carvalho de Melo1ad826b2016-02-12 18:30:01 -0300145 return __event(true, addr, sig);
Wang Nan8fd34e12016-01-25 09:55:55 +0000146}
147
Arnaldo Carvalho de Melo1ad826b2016-02-12 18:30:01 -0300148static int wp_event(void *addr, int sig)
Wang Nan8fd34e12016-01-25 09:55:55 +0000149{
Arnaldo Carvalho de Melo1ad826b2016-02-12 18:30:01 -0300150 return __event(false, addr, sig);
Wang Nan8fd34e12016-01-25 09:55:55 +0000151}
152
Jiri Olsa5a6bef42013-03-10 19:41:10 +0100153static long long bp_count(int fd)
154{
155 long long count;
156 int ret;
157
158 ret = read(fd, &count, sizeof(long long));
159 if (ret != sizeof(long long)) {
160 pr_debug("failed to read: %d\n", ret);
161 return TEST_FAIL;
162 }
163
164 return count;
165}
166
Arnaldo Carvalho de Melo721a1f52015-11-19 12:01:48 -0300167int test__bp_signal(int subtest __maybe_unused)
Jiri Olsa5a6bef42013-03-10 19:41:10 +0100168{
169 struct sigaction sa;
Wang Nan8fd34e12016-01-25 09:55:55 +0000170 long long count1, count2, count3;
Jiri Olsa5a6bef42013-03-10 19:41:10 +0100171
172 /* setup SIGIO signal handler */
173 memset(&sa, 0, sizeof(struct sigaction));
174 sa.sa_sigaction = (void *) sig_handler;
175 sa.sa_flags = SA_SIGINFO;
176
177 if (sigaction(SIGIO, &sa, NULL) < 0) {
178 pr_debug("failed setting up signal handler\n");
179 return TEST_FAIL;
180 }
181
Wang Nan8fd34e12016-01-25 09:55:55 +0000182 sa.sa_sigaction = (void *) sig_handler_2;
183 if (sigaction(SIGUSR1, &sa, NULL) < 0) {
184 pr_debug("failed setting up signal handler 2\n");
185 return TEST_FAIL;
186 }
187
Jiri Olsa5a6bef42013-03-10 19:41:10 +0100188 /*
189 * We create following events:
190 *
Wang Nan8fd34e12016-01-25 09:55:55 +0000191 * fd1 - breakpoint event on __test_function with SIGIO
Jiri Olsa5a6bef42013-03-10 19:41:10 +0100192 * signal configured. We should get signal
193 * notification each time the breakpoint is hit
194 *
Wang Nan8fd34e12016-01-25 09:55:55 +0000195 * fd2 - breakpoint event on sig_handler with SIGUSR1
196 * configured. We should get SIGUSR1 each time when
197 * breakpoint is hit
198 *
199 * fd3 - watchpoint event on __test_function with SIGIO
Jiri Olsa5a6bef42013-03-10 19:41:10 +0100200 * configured.
201 *
202 * Following processing should happen:
Wang Nan8fd34e12016-01-25 09:55:55 +0000203 * Exec: Action: Result:
204 * incq (%rdi) - fd1 event breakpoint hit -> count1 == 1
205 * - SIGIO is delivered
206 * sig_handler - fd2 event breakpoint hit -> count2 == 1
207 * - SIGUSR1 is delivered
208 * sig_handler_2 -> overflows_2 == 1 (nested signal)
209 * sys_rt_sigreturn - return from sig_handler_2
210 * overflows++ -> overflows = 1
211 * sys_rt_sigreturn - return from sig_handler
212 * incq (%rdi) - fd3 event watchpoint hit -> count3 == 1 (wp and bp in one insn)
213 * - SIGIO is delivered
214 * sig_handler - fd2 event breakpoint hit -> count2 == 2
215 * - SIGUSR1 is delivered
216 * sig_handler_2 -> overflows_2 == 2 (nested signal)
217 * sys_rt_sigreturn - return from sig_handler_2
218 * overflows++ -> overflows = 2
219 * sys_rt_sigreturn - return from sig_handler
220 * the_var++ - fd3 event watchpoint hit -> count3 == 2 (standalone watchpoint)
221 * - SIGIO is delivered
222 * sig_handler - fd2 event breakpoint hit -> count2 == 3
223 * - SIGUSR1 is delivered
224 * sig_handler_2 -> overflows_2 == 3 (nested signal)
225 * sys_rt_sigreturn - return from sig_handler_2
226 * overflows++ -> overflows == 3
227 * sys_rt_sigreturn - return from sig_handler
Jiri Olsa5a6bef42013-03-10 19:41:10 +0100228 *
229 * The test case check following error conditions:
230 * - we get stuck in signal handler because of debug
231 * exception being triggered receursively due to
232 * the wrong RF EFLAG management
233 *
234 * - we never trigger the sig_handler breakpoint due
235 * to the rong RF EFLAG management
236 *
237 */
238
Wang Nan8fd34e12016-01-25 09:55:55 +0000239 fd1 = bp_event(__test_function, SIGIO);
240 fd2 = bp_event(sig_handler, SIGUSR1);
241 fd3 = wp_event((void *)&the_var, SIGIO);
Jiri Olsa5a6bef42013-03-10 19:41:10 +0100242
243 ioctl(fd1, PERF_EVENT_IOC_ENABLE, 0);
244 ioctl(fd2, PERF_EVENT_IOC_ENABLE, 0);
Wang Nan8fd34e12016-01-25 09:55:55 +0000245 ioctl(fd3, PERF_EVENT_IOC_ENABLE, 0);
Jiri Olsa5a6bef42013-03-10 19:41:10 +0100246
247 /*
248 * Kick off the test by trigering 'fd1'
249 * breakpoint.
250 */
251 test_function();
252
253 ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
254 ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
Wang Nan8fd34e12016-01-25 09:55:55 +0000255 ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
Jiri Olsa5a6bef42013-03-10 19:41:10 +0100256
257 count1 = bp_count(fd1);
258 count2 = bp_count(fd2);
Wang Nan8fd34e12016-01-25 09:55:55 +0000259 count3 = bp_count(fd3);
Jiri Olsa5a6bef42013-03-10 19:41:10 +0100260
261 close(fd1);
262 close(fd2);
Wang Nan8fd34e12016-01-25 09:55:55 +0000263 close(fd3);
Jiri Olsa5a6bef42013-03-10 19:41:10 +0100264
Wang Nan8fd34e12016-01-25 09:55:55 +0000265 pr_debug("count1 %lld, count2 %lld, count3 %lld, overflow %d, overflows_2 %d\n",
266 count1, count2, count3, overflows, overflows_2);
Jiri Olsa5a6bef42013-03-10 19:41:10 +0100267
268 if (count1 != 1) {
269 if (count1 == 11)
270 pr_debug("failed: RF EFLAG recursion issue detected\n");
271 else
272 pr_debug("failed: wrong count for bp1%lld\n", count1);
273 }
274
Wang Nan8fd34e12016-01-25 09:55:55 +0000275 if (overflows != 3)
Jiri Olsa5a6bef42013-03-10 19:41:10 +0100276 pr_debug("failed: wrong overflow hit\n");
277
Wang Nan8fd34e12016-01-25 09:55:55 +0000278 if (overflows_2 != 3)
279 pr_debug("failed: wrong overflow_2 hit\n");
280
281 if (count2 != 3)
Jiri Olsa5a6bef42013-03-10 19:41:10 +0100282 pr_debug("failed: wrong count for bp2\n");
283
Wang Nan8fd34e12016-01-25 09:55:55 +0000284 if (count3 != 2)
285 pr_debug("failed: wrong count for bp3\n");
286
287 return count1 == 1 && overflows == 3 && count2 == 3 && overflows_2 == 3 && count3 == 2 ?
Jiri Olsa5a6bef42013-03-10 19:41:10 +0100288 TEST_OK : TEST_FAIL;
289}
Jiri Olsa598762c2017-06-01 22:54:50 +0200290
291bool test__bp_signal_is_supported(void)
292{
293/*
294 * The powerpc so far does not have support to even create
295 * instruction breakpoint using the perf event interface.
296 * Once it's there we can release this.
297 */
298#ifdef __powerpc__
299 return false;
300#else
301 return true;
302#endif
303}