xref: /linux/tools/perf/tests/wp.c (revision 0e24147d69c9357b1ccb54a9bc028eb9a9f9ed1a)
12e85d597SRavi Bangoria // SPDX-License-Identifier: GPL-2.0
22e85d597SRavi Bangoria #include <stdlib.h>
32e85d597SRavi Bangoria #include <sys/ioctl.h>
42e85d597SRavi Bangoria #include <linux/hw_breakpoint.h>
52e85d597SRavi Bangoria #include "tests.h"
62e85d597SRavi Bangoria #include "debug.h"
72e85d597SRavi Bangoria #include "cloexec.h"
82e85d597SRavi Bangoria 
92e85d597SRavi Bangoria #define WP_TEST_ASSERT_VAL(fd, text, val)       \
102e85d597SRavi Bangoria do {                                            \
112e85d597SRavi Bangoria 	long long count;                        \
122e85d597SRavi Bangoria 	wp_read(fd, &count, sizeof(long long)); \
132e85d597SRavi Bangoria 	TEST_ASSERT_VAL(text, count == val);    \
142e85d597SRavi Bangoria } while (0)
152e85d597SRavi Bangoria 
162e85d597SRavi Bangoria volatile u64 data1;
172e85d597SRavi Bangoria volatile u8 data2[3];
182e85d597SRavi Bangoria 
192e85d597SRavi Bangoria static int wp_read(int fd, long long *count, int size)
202e85d597SRavi Bangoria {
212e85d597SRavi Bangoria 	int ret = read(fd, count, size);
222e85d597SRavi Bangoria 
232e85d597SRavi Bangoria 	if (ret != size) {
242e85d597SRavi Bangoria 		pr_debug("failed to read: %d\n", ret);
252e85d597SRavi Bangoria 		return -1;
262e85d597SRavi Bangoria 	}
272e85d597SRavi Bangoria 	return 0;
282e85d597SRavi Bangoria }
292e85d597SRavi Bangoria 
302e85d597SRavi Bangoria static void get__perf_event_attr(struct perf_event_attr *attr, int wp_type,
312e85d597SRavi Bangoria 				 void *wp_addr, unsigned long wp_len)
322e85d597SRavi Bangoria {
332e85d597SRavi Bangoria 	memset(attr, 0, sizeof(struct perf_event_attr));
342e85d597SRavi Bangoria 	attr->type           = PERF_TYPE_BREAKPOINT;
352e85d597SRavi Bangoria 	attr->size           = sizeof(struct perf_event_attr);
362e85d597SRavi Bangoria 	attr->config         = 0;
372e85d597SRavi Bangoria 	attr->bp_type        = wp_type;
382e85d597SRavi Bangoria 	attr->bp_addr        = (unsigned long)wp_addr;
392e85d597SRavi Bangoria 	attr->bp_len         = wp_len;
402e85d597SRavi Bangoria 	attr->sample_period  = 1;
412e85d597SRavi Bangoria 	attr->sample_type    = PERF_SAMPLE_IP;
422e85d597SRavi Bangoria 	attr->exclude_kernel = 1;
432e85d597SRavi Bangoria 	attr->exclude_hv     = 1;
442e85d597SRavi Bangoria }
452e85d597SRavi Bangoria 
462e85d597SRavi Bangoria static int __event(int wp_type, void *wp_addr, unsigned long wp_len)
472e85d597SRavi Bangoria {
482e85d597SRavi Bangoria 	int fd;
492e85d597SRavi Bangoria 	struct perf_event_attr attr;
502e85d597SRavi Bangoria 
512e85d597SRavi Bangoria 	get__perf_event_attr(&attr, wp_type, wp_addr, wp_len);
522e85d597SRavi Bangoria 	fd = sys_perf_event_open(&attr, 0, -1, -1,
532e85d597SRavi Bangoria 				 perf_event_open_cloexec_flag());
542e85d597SRavi Bangoria 	if (fd < 0)
552e85d597SRavi Bangoria 		pr_debug("failed opening event %x\n", attr.bp_type);
562e85d597SRavi Bangoria 
572e85d597SRavi Bangoria 	return fd;
582e85d597SRavi Bangoria }
592e85d597SRavi Bangoria 
602e85d597SRavi Bangoria static int wp_ro_test(void)
612e85d597SRavi Bangoria {
622e85d597SRavi Bangoria 	int fd;
632e85d597SRavi Bangoria 	unsigned long tmp, tmp1 = rand();
642e85d597SRavi Bangoria 
652e85d597SRavi Bangoria 	fd = __event(HW_BREAKPOINT_R, (void *)&data1, sizeof(data1));
662e85d597SRavi Bangoria 	if (fd < 0)
672e85d597SRavi Bangoria 		return -1;
682e85d597SRavi Bangoria 
692e85d597SRavi Bangoria 	tmp = data1;
702e85d597SRavi Bangoria 	WP_TEST_ASSERT_VAL(fd, "RO watchpoint", 1);
712e85d597SRavi Bangoria 
722e85d597SRavi Bangoria 	data1 = tmp1 + tmp;
732e85d597SRavi Bangoria 	WP_TEST_ASSERT_VAL(fd, "RO watchpoint", 1);
742e85d597SRavi Bangoria 
752e85d597SRavi Bangoria 	close(fd);
762e85d597SRavi Bangoria 	return 0;
772e85d597SRavi Bangoria }
782e85d597SRavi Bangoria 
792e85d597SRavi Bangoria static int wp_wo_test(void)
802e85d597SRavi Bangoria {
812e85d597SRavi Bangoria 	int fd;
822e85d597SRavi Bangoria 	unsigned long tmp, tmp1 = rand();
832e85d597SRavi Bangoria 
842e85d597SRavi Bangoria 	fd = __event(HW_BREAKPOINT_W, (void *)&data1, sizeof(data1));
852e85d597SRavi Bangoria 	if (fd < 0)
862e85d597SRavi Bangoria 		return -1;
872e85d597SRavi Bangoria 
882e85d597SRavi Bangoria 	tmp = data1;
892e85d597SRavi Bangoria 	WP_TEST_ASSERT_VAL(fd, "WO watchpoint", 0);
902e85d597SRavi Bangoria 
912e85d597SRavi Bangoria 	data1 = tmp1 + tmp;
922e85d597SRavi Bangoria 	WP_TEST_ASSERT_VAL(fd, "WO watchpoint", 1);
932e85d597SRavi Bangoria 
942e85d597SRavi Bangoria 	close(fd);
952e85d597SRavi Bangoria 	return 0;
962e85d597SRavi Bangoria }
972e85d597SRavi Bangoria 
982e85d597SRavi Bangoria static int wp_rw_test(void)
992e85d597SRavi Bangoria {
1002e85d597SRavi Bangoria 	int fd;
1012e85d597SRavi Bangoria 	unsigned long tmp, tmp1 = rand();
1022e85d597SRavi Bangoria 
1032e85d597SRavi Bangoria 	fd = __event(HW_BREAKPOINT_R | HW_BREAKPOINT_W, (void *)&data1,
1042e85d597SRavi Bangoria 		     sizeof(data1));
1052e85d597SRavi Bangoria 	if (fd < 0)
1062e85d597SRavi Bangoria 		return -1;
1072e85d597SRavi Bangoria 
1082e85d597SRavi Bangoria 	tmp = data1;
1092e85d597SRavi Bangoria 	WP_TEST_ASSERT_VAL(fd, "RW watchpoint", 1);
1102e85d597SRavi Bangoria 
1112e85d597SRavi Bangoria 	data1 = tmp1 + tmp;
1122e85d597SRavi Bangoria 	WP_TEST_ASSERT_VAL(fd, "RW watchpoint", 2);
1132e85d597SRavi Bangoria 
1142e85d597SRavi Bangoria 	close(fd);
1152e85d597SRavi Bangoria 	return 0;
1162e85d597SRavi Bangoria }
1172e85d597SRavi Bangoria 
1182e85d597SRavi Bangoria static int wp_modify_test(void)
1192e85d597SRavi Bangoria {
1202e85d597SRavi Bangoria 	int fd, ret;
1212e85d597SRavi Bangoria 	unsigned long tmp = rand();
1222e85d597SRavi Bangoria 	struct perf_event_attr new_attr;
1232e85d597SRavi Bangoria 
1242e85d597SRavi Bangoria 	fd = __event(HW_BREAKPOINT_W, (void *)&data1, sizeof(data1));
1252e85d597SRavi Bangoria 	if (fd < 0)
1262e85d597SRavi Bangoria 		return -1;
1272e85d597SRavi Bangoria 
1282e85d597SRavi Bangoria 	data1 = tmp;
1292e85d597SRavi Bangoria 	WP_TEST_ASSERT_VAL(fd, "Modify watchpoint", 1);
1302e85d597SRavi Bangoria 
1312e85d597SRavi Bangoria 	/* Modify watchpoint with disabled = 1 */
1322e85d597SRavi Bangoria 	get__perf_event_attr(&new_attr, HW_BREAKPOINT_W, (void *)&data2[0],
1332e85d597SRavi Bangoria 			     sizeof(u8) * 2);
1342e85d597SRavi Bangoria 	new_attr.disabled = 1;
1352e85d597SRavi Bangoria 	ret = ioctl(fd, PERF_EVENT_IOC_MODIFY_ATTRIBUTES, &new_attr);
1362e85d597SRavi Bangoria 	if (ret < 0) {
1372e85d597SRavi Bangoria 		pr_debug("ioctl(PERF_EVENT_IOC_MODIFY_ATTRIBUTES) failed\n");
1382e85d597SRavi Bangoria 		close(fd);
1392e85d597SRavi Bangoria 		return ret;
1402e85d597SRavi Bangoria 	}
1412e85d597SRavi Bangoria 
1422e85d597SRavi Bangoria 	data2[1] = tmp; /* Not Counted */
1432e85d597SRavi Bangoria 	WP_TEST_ASSERT_VAL(fd, "Modify watchpoint", 1);
1442e85d597SRavi Bangoria 
1452e85d597SRavi Bangoria 	/* Enable the event */
1462e85d597SRavi Bangoria 	ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
1472e85d597SRavi Bangoria 	if (ret < 0) {
1482e85d597SRavi Bangoria 		pr_debug("Failed to enable event\n");
1492e85d597SRavi Bangoria 		close(fd);
1502e85d597SRavi Bangoria 		return ret;
1512e85d597SRavi Bangoria 	}
1522e85d597SRavi Bangoria 
1532e85d597SRavi Bangoria 	data2[1] = tmp; /* Counted */
1542e85d597SRavi Bangoria 	WP_TEST_ASSERT_VAL(fd, "Modify watchpoint", 2);
1552e85d597SRavi Bangoria 
1562e85d597SRavi Bangoria 	data2[2] = tmp; /* Not Counted */
1572e85d597SRavi Bangoria 	WP_TEST_ASSERT_VAL(fd, "Modify watchpoint", 2);
1582e85d597SRavi Bangoria 
1592e85d597SRavi Bangoria 	close(fd);
1602e85d597SRavi Bangoria 	return 0;
1612e85d597SRavi Bangoria }
1622e85d597SRavi Bangoria 
1632e85d597SRavi Bangoria static bool wp_ro_supported(void)
1642e85d597SRavi Bangoria {
1652e85d597SRavi Bangoria #if defined (__x86_64__) || defined (__i386__)
1662e85d597SRavi Bangoria 	return false;
1672e85d597SRavi Bangoria #else
1682e85d597SRavi Bangoria 	return true;
1692e85d597SRavi Bangoria #endif
1702e85d597SRavi Bangoria }
1712e85d597SRavi Bangoria 
1722e85d597SRavi Bangoria static void wp_ro_skip_msg(void)
1732e85d597SRavi Bangoria {
1742e85d597SRavi Bangoria #if defined (__x86_64__) || defined (__i386__)
1752e85d597SRavi Bangoria 	pr_debug("Hardware does not support read only watchpoints.\n");
1762e85d597SRavi Bangoria #endif
1772e85d597SRavi Bangoria }
1782e85d597SRavi Bangoria 
1792e85d597SRavi Bangoria static struct {
1802e85d597SRavi Bangoria 	const char *desc;
1812e85d597SRavi Bangoria 	int (*target_func)(void);
1822e85d597SRavi Bangoria 	bool (*is_supported)(void);
1832e85d597SRavi Bangoria 	void (*skip_msg)(void);
1842e85d597SRavi Bangoria } wp_testcase_table[] = {
1852e85d597SRavi Bangoria 	{
1862e85d597SRavi Bangoria 		.desc = "Read Only Watchpoint",
1872e85d597SRavi Bangoria 		.target_func = &wp_ro_test,
1882e85d597SRavi Bangoria 		.is_supported = &wp_ro_supported,
1892e85d597SRavi Bangoria 		.skip_msg = &wp_ro_skip_msg,
1902e85d597SRavi Bangoria 	},
1912e85d597SRavi Bangoria 	{
1922e85d597SRavi Bangoria 		.desc = "Write Only Watchpoint",
1932e85d597SRavi Bangoria 		.target_func = &wp_wo_test,
1942e85d597SRavi Bangoria 	},
1952e85d597SRavi Bangoria 	{
1962e85d597SRavi Bangoria 		.desc = "Read / Write Watchpoint",
1972e85d597SRavi Bangoria 		.target_func = &wp_rw_test,
1982e85d597SRavi Bangoria 	},
1992e85d597SRavi Bangoria 	{
2002e85d597SRavi Bangoria 		.desc = "Modify Watchpoint",
2012e85d597SRavi Bangoria 		.target_func = &wp_modify_test,
2022e85d597SRavi Bangoria 	},
2032e85d597SRavi Bangoria };
2042e85d597SRavi Bangoria 
2052e85d597SRavi Bangoria int test__wp_subtest_get_nr(void)
2062e85d597SRavi Bangoria {
2072e85d597SRavi Bangoria 	return (int)ARRAY_SIZE(wp_testcase_table);
2082e85d597SRavi Bangoria }
2092e85d597SRavi Bangoria 
2102e85d597SRavi Bangoria const char *test__wp_subtest_get_desc(int i)
2112e85d597SRavi Bangoria {
2122e85d597SRavi Bangoria 	if (i < 0 || i >= (int)ARRAY_SIZE(wp_testcase_table))
2132e85d597SRavi Bangoria 		return NULL;
2142e85d597SRavi Bangoria 	return wp_testcase_table[i].desc;
2152e85d597SRavi Bangoria }
2162e85d597SRavi Bangoria 
2172e85d597SRavi Bangoria int test__wp(struct test *test __maybe_unused, int i)
2182e85d597SRavi Bangoria {
2192e85d597SRavi Bangoria 	if (i < 0 || i >= (int)ARRAY_SIZE(wp_testcase_table))
2202e85d597SRavi Bangoria 		return TEST_FAIL;
2212e85d597SRavi Bangoria 
2222e85d597SRavi Bangoria 	if (wp_testcase_table[i].is_supported &&
2232e85d597SRavi Bangoria 	    !wp_testcase_table[i].is_supported()) {
2242e85d597SRavi Bangoria 		wp_testcase_table[i].skip_msg();
2252e85d597SRavi Bangoria 		return TEST_SKIP;
2262e85d597SRavi Bangoria 	}
2272e85d597SRavi Bangoria 
2282e85d597SRavi Bangoria 	return !wp_testcase_table[i].target_func() ? TEST_OK : TEST_FAIL;
2292e85d597SRavi Bangoria }
230*0e24147dSThomas Richter 
231*0e24147dSThomas Richter /* The s390 so far does not have support for
232*0e24147dSThomas Richter  * instruction breakpoint using the perf_event_open() system call.
233*0e24147dSThomas Richter  */
234*0e24147dSThomas Richter bool test__wp_is_supported(void)
235*0e24147dSThomas Richter {
236*0e24147dSThomas Richter #if defined(__s390x__)
237*0e24147dSThomas Richter 	return false;
238*0e24147dSThomas Richter #else
239*0e24147dSThomas Richter 	return true;
240*0e24147dSThomas Richter #endif
241*0e24147dSThomas Richter }
242