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