xref: /linux/tools/perf/tests/wp.c (revision 891e8abed532423d3b918b0c445dc8919bc445b5)
12e85d597SRavi Bangoria // SPDX-License-Identifier: GPL-2.0
22e85d597SRavi Bangoria #include <stdlib.h>
38520a98dSArnaldo Carvalho de Melo #include <string.h>
491854f9aSArnaldo Carvalho de Melo #include <unistd.h>
5e1dda48eSNamhyung Kim #include <errno.h>
62e85d597SRavi Bangoria #include <sys/ioctl.h>
7e1dda48eSNamhyung Kim #include <linux/compiler.h>
82e85d597SRavi Bangoria #include <linux/hw_breakpoint.h>
98520a98dSArnaldo Carvalho de Melo #include <linux/kernel.h>
102e85d597SRavi Bangoria #include "tests.h"
112e85d597SRavi Bangoria #include "debug.h"
128520a98dSArnaldo Carvalho de Melo #include "event.h"
132e85d597SRavi Bangoria #include "cloexec.h"
1491854f9aSArnaldo Carvalho de Melo #include "../perf-sys.h"
152e85d597SRavi Bangoria 
162e85d597SRavi Bangoria #define WP_TEST_ASSERT_VAL(fd, text, val)       \
172e85d597SRavi Bangoria do {                                            \
182e85d597SRavi Bangoria 	long long count;                        \
192e85d597SRavi Bangoria 	wp_read(fd, &count, sizeof(long long)); \
202e85d597SRavi Bangoria 	TEST_ASSERT_VAL(text, count == val);    \
212e85d597SRavi Bangoria } while (0)
222e85d597SRavi Bangoria 
23*18f41f1bSIan Rogers #ifdef __i386__
24*18f41f1bSIan Rogers /* Only breakpoint length less-than 8 has hardware support on i386. */
25*18f41f1bSIan Rogers volatile u32 data1;
26*18f41f1bSIan Rogers #else
272e85d597SRavi Bangoria volatile u64 data1;
28*18f41f1bSIan Rogers #endif
292e85d597SRavi Bangoria volatile u8 data2[3];
302e85d597SRavi Bangoria 
31ccb05590SArnaldo Carvalho de Melo #ifndef __s390x__
322e85d597SRavi Bangoria static int wp_read(int fd, long long *count, int size)
332e85d597SRavi Bangoria {
342e85d597SRavi Bangoria 	int ret = read(fd, count, size);
352e85d597SRavi Bangoria 
362e85d597SRavi Bangoria 	if (ret != size) {
372e85d597SRavi Bangoria 		pr_debug("failed to read: %d\n", ret);
382e85d597SRavi Bangoria 		return -1;
392e85d597SRavi Bangoria 	}
402e85d597SRavi Bangoria 	return 0;
412e85d597SRavi Bangoria }
422e85d597SRavi Bangoria 
432e85d597SRavi Bangoria static void get__perf_event_attr(struct perf_event_attr *attr, int wp_type,
442e85d597SRavi Bangoria 				 void *wp_addr, unsigned long wp_len)
452e85d597SRavi Bangoria {
462e85d597SRavi Bangoria 	memset(attr, 0, sizeof(struct perf_event_attr));
472e85d597SRavi Bangoria 	attr->type           = PERF_TYPE_BREAKPOINT;
482e85d597SRavi Bangoria 	attr->size           = sizeof(struct perf_event_attr);
492e85d597SRavi Bangoria 	attr->config         = 0;
502e85d597SRavi Bangoria 	attr->bp_type        = wp_type;
512e85d597SRavi Bangoria 	attr->bp_addr        = (unsigned long)wp_addr;
522e85d597SRavi Bangoria 	attr->bp_len         = wp_len;
532e85d597SRavi Bangoria 	attr->sample_period  = 1;
542e85d597SRavi Bangoria 	attr->sample_type    = PERF_SAMPLE_IP;
552e85d597SRavi Bangoria 	attr->exclude_kernel = 1;
562e85d597SRavi Bangoria 	attr->exclude_hv     = 1;
572e85d597SRavi Bangoria }
582e85d597SRavi Bangoria 
592e85d597SRavi Bangoria static int __event(int wp_type, void *wp_addr, unsigned long wp_len)
602e85d597SRavi Bangoria {
612e85d597SRavi Bangoria 	int fd;
622e85d597SRavi Bangoria 	struct perf_event_attr attr;
632e85d597SRavi Bangoria 
642e85d597SRavi Bangoria 	get__perf_event_attr(&attr, wp_type, wp_addr, wp_len);
652e85d597SRavi Bangoria 	fd = sys_perf_event_open(&attr, 0, -1, -1,
662e85d597SRavi Bangoria 				 perf_event_open_cloexec_flag());
677d54a4acSNaveen N. Rao 	if (fd < 0) {
687d54a4acSNaveen N. Rao 		fd = -errno;
692e85d597SRavi Bangoria 		pr_debug("failed opening event %x\n", attr.bp_type);
707d54a4acSNaveen N. Rao 	}
712e85d597SRavi Bangoria 
722e85d597SRavi Bangoria 	return fd;
732e85d597SRavi Bangoria }
74e47c6ecaSIan Rogers #endif
752e85d597SRavi Bangoria 
76e47c6ecaSIan Rogers static int test__wp_ro(struct test_suite *test __maybe_unused,
77e47c6ecaSIan Rogers 		       int subtest __maybe_unused)
782e85d597SRavi Bangoria {
79e47c6ecaSIan Rogers #if defined(__s390x__) || defined(__x86_64__) || defined(__i386__)
80e47c6ecaSIan Rogers 	return TEST_SKIP;
81e47c6ecaSIan Rogers #else
822e85d597SRavi Bangoria 	int fd;
832e85d597SRavi Bangoria 	unsigned long tmp, tmp1 = rand();
842e85d597SRavi Bangoria 
852e85d597SRavi Bangoria 	fd = __event(HW_BREAKPOINT_R, (void *)&data1, sizeof(data1));
862e85d597SRavi Bangoria 	if (fd < 0)
877d54a4acSNaveen N. Rao 		return fd == -ENODEV ? TEST_SKIP : -1;
882e85d597SRavi Bangoria 
892e85d597SRavi Bangoria 	tmp = data1;
902e85d597SRavi Bangoria 	WP_TEST_ASSERT_VAL(fd, "RO watchpoint", 1);
912e85d597SRavi Bangoria 
922e85d597SRavi Bangoria 	data1 = tmp1 + tmp;
932e85d597SRavi Bangoria 	WP_TEST_ASSERT_VAL(fd, "RO watchpoint", 1);
942e85d597SRavi Bangoria 
952e85d597SRavi Bangoria 	close(fd);
962e85d597SRavi Bangoria 	return 0;
97e47c6ecaSIan Rogers #endif
982e85d597SRavi Bangoria }
992e85d597SRavi Bangoria 
100e47c6ecaSIan Rogers static int test__wp_wo(struct test_suite *test __maybe_unused,
101e47c6ecaSIan Rogers 		       int subtest __maybe_unused)
1022e85d597SRavi Bangoria {
103e47c6ecaSIan Rogers #if defined(__s390x__)
104e47c6ecaSIan Rogers 	return TEST_SKIP;
105e47c6ecaSIan Rogers #else
1062e85d597SRavi Bangoria 	int fd;
1072e85d597SRavi Bangoria 	unsigned long tmp, tmp1 = rand();
1082e85d597SRavi Bangoria 
1092e85d597SRavi Bangoria 	fd = __event(HW_BREAKPOINT_W, (void *)&data1, sizeof(data1));
1102e85d597SRavi Bangoria 	if (fd < 0)
1117d54a4acSNaveen N. Rao 		return fd == -ENODEV ? TEST_SKIP : -1;
1122e85d597SRavi Bangoria 
1132e85d597SRavi Bangoria 	tmp = data1;
1142e85d597SRavi Bangoria 	WP_TEST_ASSERT_VAL(fd, "WO watchpoint", 0);
1152e85d597SRavi Bangoria 
1162e85d597SRavi Bangoria 	data1 = tmp1 + tmp;
1172e85d597SRavi Bangoria 	WP_TEST_ASSERT_VAL(fd, "WO watchpoint", 1);
1182e85d597SRavi Bangoria 
1192e85d597SRavi Bangoria 	close(fd);
1202e85d597SRavi Bangoria 	return 0;
121e47c6ecaSIan Rogers #endif
1222e85d597SRavi Bangoria }
1232e85d597SRavi Bangoria 
124e47c6ecaSIan Rogers static int test__wp_rw(struct test_suite *test __maybe_unused,
125e47c6ecaSIan Rogers 		       int subtest __maybe_unused)
1262e85d597SRavi Bangoria {
127e47c6ecaSIan Rogers #if defined(__s390x__)
128e47c6ecaSIan Rogers 	return TEST_SKIP;
129e47c6ecaSIan Rogers #else
1302e85d597SRavi Bangoria 	int fd;
1312e85d597SRavi Bangoria 	unsigned long tmp, tmp1 = rand();
1322e85d597SRavi Bangoria 
1332e85d597SRavi Bangoria 	fd = __event(HW_BREAKPOINT_R | HW_BREAKPOINT_W, (void *)&data1,
1342e85d597SRavi Bangoria 		     sizeof(data1));
1352e85d597SRavi Bangoria 	if (fd < 0)
1367d54a4acSNaveen N. Rao 		return fd == -ENODEV ? TEST_SKIP : -1;
1372e85d597SRavi Bangoria 
1382e85d597SRavi Bangoria 	tmp = data1;
1392e85d597SRavi Bangoria 	WP_TEST_ASSERT_VAL(fd, "RW watchpoint", 1);
1402e85d597SRavi Bangoria 
1412e85d597SRavi Bangoria 	data1 = tmp1 + tmp;
1422e85d597SRavi Bangoria 	WP_TEST_ASSERT_VAL(fd, "RW watchpoint", 2);
1432e85d597SRavi Bangoria 
1442e85d597SRavi Bangoria 	close(fd);
1452e85d597SRavi Bangoria 	return 0;
146e47c6ecaSIan Rogers #endif
1472e85d597SRavi Bangoria }
1482e85d597SRavi Bangoria 
149e1dda48eSNamhyung Kim static int test__wp_modify(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
1502e85d597SRavi Bangoria {
151e47c6ecaSIan Rogers #if defined(__s390x__)
152e47c6ecaSIan Rogers 	return TEST_SKIP;
153e47c6ecaSIan Rogers #else
1542e85d597SRavi Bangoria 	int fd, ret;
1552e85d597SRavi Bangoria 	unsigned long tmp = rand();
1562e85d597SRavi Bangoria 	struct perf_event_attr new_attr;
1572e85d597SRavi Bangoria 
1582e85d597SRavi Bangoria 	fd = __event(HW_BREAKPOINT_W, (void *)&data1, sizeof(data1));
1592e85d597SRavi Bangoria 	if (fd < 0)
1607d54a4acSNaveen N. Rao 		return fd == -ENODEV ? TEST_SKIP : -1;
1612e85d597SRavi Bangoria 
1622e85d597SRavi Bangoria 	data1 = tmp;
1632e85d597SRavi Bangoria 	WP_TEST_ASSERT_VAL(fd, "Modify watchpoint", 1);
1642e85d597SRavi Bangoria 
1652e85d597SRavi Bangoria 	/* Modify watchpoint with disabled = 1 */
1662e85d597SRavi Bangoria 	get__perf_event_attr(&new_attr, HW_BREAKPOINT_W, (void *)&data2[0],
1672e85d597SRavi Bangoria 			     sizeof(u8) * 2);
1682e85d597SRavi Bangoria 	new_attr.disabled = 1;
1692e85d597SRavi Bangoria 	ret = ioctl(fd, PERF_EVENT_IOC_MODIFY_ATTRIBUTES, &new_attr);
1702e85d597SRavi Bangoria 	if (ret < 0) {
171e1dda48eSNamhyung Kim 		if (errno == ENOTTY) {
172e1dda48eSNamhyung Kim 			test->test_cases[subtest].skip_reason = "missing kernel support";
173e1dda48eSNamhyung Kim 			ret = TEST_SKIP;
174e1dda48eSNamhyung Kim 		}
175e1dda48eSNamhyung Kim 
1762e85d597SRavi Bangoria 		pr_debug("ioctl(PERF_EVENT_IOC_MODIFY_ATTRIBUTES) failed\n");
1772e85d597SRavi Bangoria 		close(fd);
1782e85d597SRavi Bangoria 		return ret;
1792e85d597SRavi Bangoria 	}
1802e85d597SRavi Bangoria 
1812e85d597SRavi Bangoria 	data2[1] = tmp; /* Not Counted */
1822e85d597SRavi Bangoria 	WP_TEST_ASSERT_VAL(fd, "Modify watchpoint", 1);
1832e85d597SRavi Bangoria 
1842e85d597SRavi Bangoria 	/* Enable the event */
1852e85d597SRavi Bangoria 	ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
1862e85d597SRavi Bangoria 	if (ret < 0) {
1872e85d597SRavi Bangoria 		pr_debug("Failed to enable event\n");
1882e85d597SRavi Bangoria 		close(fd);
1892e85d597SRavi Bangoria 		return ret;
1902e85d597SRavi Bangoria 	}
1912e85d597SRavi Bangoria 
1922e85d597SRavi Bangoria 	data2[1] = tmp; /* Counted */
1932e85d597SRavi Bangoria 	WP_TEST_ASSERT_VAL(fd, "Modify watchpoint", 2);
1942e85d597SRavi Bangoria 
1952e85d597SRavi Bangoria 	data2[2] = tmp; /* Not Counted */
1962e85d597SRavi Bangoria 	WP_TEST_ASSERT_VAL(fd, "Modify watchpoint", 2);
1972e85d597SRavi Bangoria 
1982e85d597SRavi Bangoria 	close(fd);
1992e85d597SRavi Bangoria 	return 0;
2002e85d597SRavi Bangoria #endif
2012e85d597SRavi Bangoria }
2022e85d597SRavi Bangoria 
203e47c6ecaSIan Rogers static struct test_case wp_tests[] = {
204e47c6ecaSIan Rogers 	TEST_CASE_REASON("Read Only Watchpoint", wp_ro, "missing hardware support"),
205e47c6ecaSIan Rogers 	TEST_CASE_REASON("Write Only Watchpoint", wp_wo, "missing hardware support"),
206e47c6ecaSIan Rogers 	TEST_CASE_REASON("Read / Write Watchpoint", wp_rw, "missing hardware support"),
207e47c6ecaSIan Rogers 	TEST_CASE_REASON("Modify Watchpoint", wp_modify, "missing hardware support"),
208e47c6ecaSIan Rogers 	{ .name = NULL, }
2092e85d597SRavi Bangoria };
2102e85d597SRavi Bangoria 
21133f44bfdSIan Rogers struct test_suite suite__wp = {
212d68f0365SIan Rogers 	.desc = "Watchpoint",
213e47c6ecaSIan Rogers 	.test_cases = wp_tests,
214d68f0365SIan Rogers };
215