1
2
3
4
5
6
7
8
9
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#include "cloexec.h"
29
30static int fd1;
31static int fd2;
32static int overflows;
33
34__attribute__ ((noinline))
35static int test_function(void)
36{
37 return time(NULL);
38}
39
40static void sig_handler(int signum __maybe_unused,
41 siginfo_t *oh __maybe_unused,
42 void *uc __maybe_unused)
43{
44 overflows++;
45
46 if (overflows > 10) {
47
48
49
50
51
52
53
54
55 ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
56 ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
57 }
58}
59
60static int bp_event(void *fn, int setup_signal)
61{
62 struct perf_event_attr pe;
63 int fd;
64
65 memset(&pe, 0, sizeof(struct perf_event_attr));
66 pe.type = PERF_TYPE_BREAKPOINT;
67 pe.size = sizeof(struct perf_event_attr);
68
69 pe.config = 0;
70 pe.bp_type = HW_BREAKPOINT_X;
71 pe.bp_addr = (unsigned long) fn;
72 pe.bp_len = sizeof(long);
73
74 pe.sample_period = 1;
75 pe.sample_type = PERF_SAMPLE_IP;
76 pe.wakeup_events = 1;
77
78 pe.disabled = 1;
79 pe.exclude_kernel = 1;
80 pe.exclude_hv = 1;
81
82 fd = sys_perf_event_open(&pe, 0, -1, -1,
83 perf_event_open_cloexec_flag());
84 if (fd < 0) {
85 pr_debug("failed opening event %llx\n", pe.config);
86 return TEST_FAIL;
87 }
88
89 if (setup_signal) {
90 fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
91 fcntl(fd, F_SETSIG, SIGIO);
92 fcntl(fd, F_SETOWN, getpid());
93 }
94
95 ioctl(fd, PERF_EVENT_IOC_RESET, 0);
96
97 return fd;
98}
99
100static long long bp_count(int fd)
101{
102 long long count;
103 int ret;
104
105 ret = read(fd, &count, sizeof(long long));
106 if (ret != sizeof(long long)) {
107 pr_debug("failed to read: %d\n", ret);
108 return TEST_FAIL;
109 }
110
111 return count;
112}
113
114int test__bp_signal(void)
115{
116 struct sigaction sa;
117 long long count1, count2;
118
119
120 memset(&sa, 0, sizeof(struct sigaction));
121 sa.sa_sigaction = (void *) sig_handler;
122 sa.sa_flags = SA_SIGINFO;
123
124 if (sigaction(SIGIO, &sa, NULL) < 0) {
125 pr_debug("failed setting up signal handler\n");
126 return TEST_FAIL;
127 }
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155 fd1 = bp_event(test_function, 1);
156 fd2 = bp_event(sig_handler, 0);
157
158 ioctl(fd1, PERF_EVENT_IOC_ENABLE, 0);
159 ioctl(fd2, PERF_EVENT_IOC_ENABLE, 0);
160
161
162
163
164
165 test_function();
166
167 ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
168 ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
169
170 count1 = bp_count(fd1);
171 count2 = bp_count(fd2);
172
173 close(fd1);
174 close(fd2);
175
176 pr_debug("count1 %lld, count2 %lld, overflow %d\n",
177 count1, count2, overflows);
178
179 if (count1 != 1) {
180 if (count1 == 11)
181 pr_debug("failed: RF EFLAG recursion issue detected\n");
182 else
183 pr_debug("failed: wrong count for bp1%lld\n", count1);
184 }
185
186 if (overflows != 1)
187 pr_debug("failed: wrong overflow hit\n");
188
189 if (count2 != 1)
190 pr_debug("failed: wrong count for bp2\n");
191
192 return count1 == 1 && overflows == 1 && count2 == 1 ?
193 TEST_OK : TEST_FAIL;
194}
195