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