diff options
Diffstat (limited to 'tools/perf/tests/wp.c')
-rw-r--r-- | tools/perf/tests/wp.c | 229 |
1 files changed, 229 insertions, 0 deletions
diff --git a/tools/perf/tests/wp.c b/tools/perf/tests/wp.c new file mode 100644 index 000000000000..017a99317f94 --- /dev/null +++ b/tools/perf/tests/wp.c | |||
@@ -0,0 +1,229 @@ | |||
1 | // SPDX-License-Identifier: GPL-2.0 | ||
2 | #include <stdlib.h> | ||
3 | #include <sys/ioctl.h> | ||
4 | #include <linux/hw_breakpoint.h> | ||
5 | #include "tests.h" | ||
6 | #include "debug.h" | ||
7 | #include "cloexec.h" | ||
8 | |||
9 | #define WP_TEST_ASSERT_VAL(fd, text, val) \ | ||
10 | do { \ | ||
11 | long long count; \ | ||
12 | wp_read(fd, &count, sizeof(long long)); \ | ||
13 | TEST_ASSERT_VAL(text, count == val); \ | ||
14 | } while (0) | ||
15 | |||
16 | volatile u64 data1; | ||
17 | volatile u8 data2[3]; | ||
18 | |||
19 | static int wp_read(int fd, long long *count, int size) | ||
20 | { | ||
21 | int ret = read(fd, count, size); | ||
22 | |||
23 | if (ret != size) { | ||
24 | pr_debug("failed to read: %d\n", ret); | ||
25 | return -1; | ||
26 | } | ||
27 | return 0; | ||
28 | } | ||
29 | |||
30 | static void get__perf_event_attr(struct perf_event_attr *attr, int wp_type, | ||
31 | void *wp_addr, unsigned long wp_len) | ||
32 | { | ||
33 | memset(attr, 0, sizeof(struct perf_event_attr)); | ||
34 | attr->type = PERF_TYPE_BREAKPOINT; | ||
35 | attr->size = sizeof(struct perf_event_attr); | ||
36 | attr->config = 0; | ||
37 | attr->bp_type = wp_type; | ||
38 | attr->bp_addr = (unsigned long)wp_addr; | ||
39 | attr->bp_len = wp_len; | ||
40 | attr->sample_period = 1; | ||
41 | attr->sample_type = PERF_SAMPLE_IP; | ||
42 | attr->exclude_kernel = 1; | ||
43 | attr->exclude_hv = 1; | ||
44 | } | ||
45 | |||
46 | static int __event(int wp_type, void *wp_addr, unsigned long wp_len) | ||
47 | { | ||
48 | int fd; | ||
49 | struct perf_event_attr attr; | ||
50 | |||
51 | get__perf_event_attr(&attr, wp_type, wp_addr, wp_len); | ||
52 | fd = sys_perf_event_open(&attr, 0, -1, -1, | ||
53 | perf_event_open_cloexec_flag()); | ||
54 | if (fd < 0) | ||
55 | pr_debug("failed opening event %x\n", attr.bp_type); | ||
56 | |||
57 | return fd; | ||
58 | } | ||
59 | |||
60 | static int wp_ro_test(void) | ||
61 | { | ||
62 | int fd; | ||
63 | unsigned long tmp, tmp1 = rand(); | ||
64 | |||
65 | fd = __event(HW_BREAKPOINT_R, (void *)&data1, sizeof(data1)); | ||
66 | if (fd < 0) | ||
67 | return -1; | ||
68 | |||
69 | tmp = data1; | ||
70 | WP_TEST_ASSERT_VAL(fd, "RO watchpoint", 1); | ||
71 | |||
72 | data1 = tmp1 + tmp; | ||
73 | WP_TEST_ASSERT_VAL(fd, "RO watchpoint", 1); | ||
74 | |||
75 | close(fd); | ||
76 | return 0; | ||
77 | } | ||
78 | |||
79 | static int wp_wo_test(void) | ||
80 | { | ||
81 | int fd; | ||
82 | unsigned long tmp, tmp1 = rand(); | ||
83 | |||
84 | fd = __event(HW_BREAKPOINT_W, (void *)&data1, sizeof(data1)); | ||
85 | if (fd < 0) | ||
86 | return -1; | ||
87 | |||
88 | tmp = data1; | ||
89 | WP_TEST_ASSERT_VAL(fd, "WO watchpoint", 0); | ||
90 | |||
91 | data1 = tmp1 + tmp; | ||
92 | WP_TEST_ASSERT_VAL(fd, "WO watchpoint", 1); | ||
93 | |||
94 | close(fd); | ||
95 | return 0; | ||
96 | } | ||
97 | |||
98 | static int wp_rw_test(void) | ||
99 | { | ||
100 | int fd; | ||
101 | unsigned long tmp, tmp1 = rand(); | ||
102 | |||
103 | fd = __event(HW_BREAKPOINT_R | HW_BREAKPOINT_W, (void *)&data1, | ||
104 | sizeof(data1)); | ||
105 | if (fd < 0) | ||
106 | return -1; | ||
107 | |||
108 | tmp = data1; | ||
109 | WP_TEST_ASSERT_VAL(fd, "RW watchpoint", 1); | ||
110 | |||
111 | data1 = tmp1 + tmp; | ||
112 | WP_TEST_ASSERT_VAL(fd, "RW watchpoint", 2); | ||
113 | |||
114 | close(fd); | ||
115 | return 0; | ||
116 | } | ||
117 | |||
118 | static int wp_modify_test(void) | ||
119 | { | ||
120 | int fd, ret; | ||
121 | unsigned long tmp = rand(); | ||
122 | struct perf_event_attr new_attr; | ||
123 | |||
124 | fd = __event(HW_BREAKPOINT_W, (void *)&data1, sizeof(data1)); | ||
125 | if (fd < 0) | ||
126 | return -1; | ||
127 | |||
128 | data1 = tmp; | ||
129 | WP_TEST_ASSERT_VAL(fd, "Modify watchpoint", 1); | ||
130 | |||
131 | /* Modify watchpoint with disabled = 1 */ | ||
132 | get__perf_event_attr(&new_attr, HW_BREAKPOINT_W, (void *)&data2[0], | ||
133 | sizeof(u8) * 2); | ||
134 | new_attr.disabled = 1; | ||
135 | ret = ioctl(fd, PERF_EVENT_IOC_MODIFY_ATTRIBUTES, &new_attr); | ||
136 | if (ret < 0) { | ||
137 | pr_debug("ioctl(PERF_EVENT_IOC_MODIFY_ATTRIBUTES) failed\n"); | ||
138 | close(fd); | ||
139 | return ret; | ||
140 | } | ||
141 | |||
142 | data2[1] = tmp; /* Not Counted */ | ||
143 | WP_TEST_ASSERT_VAL(fd, "Modify watchpoint", 1); | ||
144 | |||
145 | /* Enable the event */ | ||
146 | ioctl(fd, PERF_EVENT_IOC_ENABLE, 0); | ||
147 | if (ret < 0) { | ||
148 | pr_debug("Failed to enable event\n"); | ||
149 | close(fd); | ||
150 | return ret; | ||
151 | } | ||
152 | |||
153 | data2[1] = tmp; /* Counted */ | ||
154 | WP_TEST_ASSERT_VAL(fd, "Modify watchpoint", 2); | ||
155 | |||
156 | data2[2] = tmp; /* Not Counted */ | ||
157 | WP_TEST_ASSERT_VAL(fd, "Modify watchpoint", 2); | ||
158 | |||
159 | close(fd); | ||
160 | return 0; | ||
161 | } | ||
162 | |||
163 | static bool wp_ro_supported(void) | ||
164 | { | ||
165 | #if defined (__x86_64__) || defined (__i386__) | ||
166 | return false; | ||
167 | #else | ||
168 | return true; | ||
169 | #endif | ||
170 | } | ||
171 | |||
172 | static void wp_ro_skip_msg(void) | ||
173 | { | ||
174 | #if defined (__x86_64__) || defined (__i386__) | ||
175 | pr_debug("Hardware does not support read only watchpoints.\n"); | ||
176 | #endif | ||
177 | } | ||
178 | |||
179 | static struct { | ||
180 | const char *desc; | ||
181 | int (*target_func)(void); | ||
182 | bool (*is_supported)(void); | ||
183 | void (*skip_msg)(void); | ||
184 | } wp_testcase_table[] = { | ||
185 | { | ||
186 | .desc = "Read Only Watchpoint", | ||
187 | .target_func = &wp_ro_test, | ||
188 | .is_supported = &wp_ro_supported, | ||
189 | .skip_msg = &wp_ro_skip_msg, | ||
190 | }, | ||
191 | { | ||
192 | .desc = "Write Only Watchpoint", | ||
193 | .target_func = &wp_wo_test, | ||
194 | }, | ||
195 | { | ||
196 | .desc = "Read / Write Watchpoint", | ||
197 | .target_func = &wp_rw_test, | ||
198 | }, | ||
199 | { | ||
200 | .desc = "Modify Watchpoint", | ||
201 | .target_func = &wp_modify_test, | ||
202 | }, | ||
203 | }; | ||
204 | |||
205 | int test__wp_subtest_get_nr(void) | ||
206 | { | ||
207 | return (int)ARRAY_SIZE(wp_testcase_table); | ||
208 | } | ||
209 | |||
210 | const char *test__wp_subtest_get_desc(int i) | ||
211 | { | ||
212 | if (i < 0 || i >= (int)ARRAY_SIZE(wp_testcase_table)) | ||
213 | return NULL; | ||
214 | return wp_testcase_table[i].desc; | ||
215 | } | ||
216 | |||
217 | int test__wp(struct test *test __maybe_unused, int i) | ||
218 | { | ||
219 | if (i < 0 || i >= (int)ARRAY_SIZE(wp_testcase_table)) | ||
220 | return TEST_FAIL; | ||
221 | |||
222 | if (wp_testcase_table[i].is_supported && | ||
223 | !wp_testcase_table[i].is_supported()) { | ||
224 | wp_testcase_table[i].skip_msg(); | ||
225 | return TEST_SKIP; | ||
226 | } | ||
227 | |||
228 | return !wp_testcase_table[i].target_func() ? TEST_OK : TEST_FAIL; | ||
229 | } | ||