xref: /linux/tools/perf/tests/wp.c (revision e47c6ecaae1df54aec9211f3c12d6f4f5bb09346)
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>
52e85d597SRavi Bangoria #include <sys/ioctl.h>
62e85d597SRavi Bangoria #include <linux/hw_breakpoint.h>
78520a98dSArnaldo Carvalho de Melo #include <linux/kernel.h>
82e85d597SRavi Bangoria #include "tests.h"
92e85d597SRavi Bangoria #include "debug.h"
108520a98dSArnaldo Carvalho de Melo #include "event.h"
112e85d597SRavi Bangoria #include "cloexec.h"
1291854f9aSArnaldo Carvalho de Melo #include "../perf-sys.h"
132e85d597SRavi Bangoria 
142e85d597SRavi Bangoria #define WP_TEST_ASSERT_VAL(fd, text, val)       \
152e85d597SRavi Bangoria do {                                            \
162e85d597SRavi Bangoria 	long long count;                        \
172e85d597SRavi Bangoria 	wp_read(fd, &count, sizeof(long long)); \
182e85d597SRavi Bangoria 	TEST_ASSERT_VAL(text, count == val);    \
192e85d597SRavi Bangoria } while (0)
202e85d597SRavi Bangoria 
212e85d597SRavi Bangoria volatile u64 data1;
222e85d597SRavi Bangoria volatile u8 data2[3];
232e85d597SRavi Bangoria 
242e85d597SRavi Bangoria static int wp_read(int fd, long long *count, int size)
252e85d597SRavi Bangoria {
262e85d597SRavi Bangoria 	int ret = read(fd, count, size);
272e85d597SRavi Bangoria 
282e85d597SRavi Bangoria 	if (ret != size) {
292e85d597SRavi Bangoria 		pr_debug("failed to read: %d\n", ret);
302e85d597SRavi Bangoria 		return -1;
312e85d597SRavi Bangoria 	}
322e85d597SRavi Bangoria 	return 0;
332e85d597SRavi Bangoria }
342e85d597SRavi Bangoria 
352e85d597SRavi Bangoria static void get__perf_event_attr(struct perf_event_attr *attr, int wp_type,
362e85d597SRavi Bangoria 				 void *wp_addr, unsigned long wp_len)
372e85d597SRavi Bangoria {
382e85d597SRavi Bangoria 	memset(attr, 0, sizeof(struct perf_event_attr));
392e85d597SRavi Bangoria 	attr->type           = PERF_TYPE_BREAKPOINT;
402e85d597SRavi Bangoria 	attr->size           = sizeof(struct perf_event_attr);
412e85d597SRavi Bangoria 	attr->config         = 0;
422e85d597SRavi Bangoria 	attr->bp_type        = wp_type;
432e85d597SRavi Bangoria 	attr->bp_addr        = (unsigned long)wp_addr;
442e85d597SRavi Bangoria 	attr->bp_len         = wp_len;
452e85d597SRavi Bangoria 	attr->sample_period  = 1;
462e85d597SRavi Bangoria 	attr->sample_type    = PERF_SAMPLE_IP;
472e85d597SRavi Bangoria 	attr->exclude_kernel = 1;
482e85d597SRavi Bangoria 	attr->exclude_hv     = 1;
492e85d597SRavi Bangoria }
502e85d597SRavi Bangoria 
51*e47c6ecaSIan Rogers #ifndef __s390x__
522e85d597SRavi Bangoria static int __event(int wp_type, void *wp_addr, unsigned long wp_len)
532e85d597SRavi Bangoria {
542e85d597SRavi Bangoria 	int fd;
552e85d597SRavi Bangoria 	struct perf_event_attr attr;
562e85d597SRavi Bangoria 
572e85d597SRavi Bangoria 	get__perf_event_attr(&attr, wp_type, wp_addr, wp_len);
582e85d597SRavi Bangoria 	fd = sys_perf_event_open(&attr, 0, -1, -1,
592e85d597SRavi Bangoria 				 perf_event_open_cloexec_flag());
602e85d597SRavi Bangoria 	if (fd < 0)
612e85d597SRavi Bangoria 		pr_debug("failed opening event %x\n", attr.bp_type);
622e85d597SRavi Bangoria 
632e85d597SRavi Bangoria 	return fd;
642e85d597SRavi Bangoria }
65*e47c6ecaSIan Rogers #endif
662e85d597SRavi Bangoria 
67*e47c6ecaSIan Rogers static int test__wp_ro(struct test_suite *test __maybe_unused,
68*e47c6ecaSIan Rogers 		       int subtest __maybe_unused)
692e85d597SRavi Bangoria {
70*e47c6ecaSIan Rogers #if defined(__s390x__) || defined(__x86_64__) || defined(__i386__)
71*e47c6ecaSIan Rogers 	return TEST_SKIP;
72*e47c6ecaSIan Rogers #else
732e85d597SRavi Bangoria 	int fd;
742e85d597SRavi Bangoria 	unsigned long tmp, tmp1 = rand();
752e85d597SRavi Bangoria 
762e85d597SRavi Bangoria 	fd = __event(HW_BREAKPOINT_R, (void *)&data1, sizeof(data1));
772e85d597SRavi Bangoria 	if (fd < 0)
782e85d597SRavi Bangoria 		return -1;
792e85d597SRavi Bangoria 
802e85d597SRavi Bangoria 	tmp = data1;
812e85d597SRavi Bangoria 	WP_TEST_ASSERT_VAL(fd, "RO watchpoint", 1);
822e85d597SRavi Bangoria 
832e85d597SRavi Bangoria 	data1 = tmp1 + tmp;
842e85d597SRavi Bangoria 	WP_TEST_ASSERT_VAL(fd, "RO watchpoint", 1);
852e85d597SRavi Bangoria 
862e85d597SRavi Bangoria 	close(fd);
872e85d597SRavi Bangoria 	return 0;
88*e47c6ecaSIan Rogers #endif
892e85d597SRavi Bangoria }
902e85d597SRavi Bangoria 
91*e47c6ecaSIan Rogers static int test__wp_wo(struct test_suite *test __maybe_unused,
92*e47c6ecaSIan Rogers 		       int subtest __maybe_unused)
932e85d597SRavi Bangoria {
94*e47c6ecaSIan Rogers #if defined(__s390x__)
95*e47c6ecaSIan Rogers 	return TEST_SKIP;
96*e47c6ecaSIan Rogers #else
972e85d597SRavi Bangoria 	int fd;
982e85d597SRavi Bangoria 	unsigned long tmp, tmp1 = rand();
992e85d597SRavi Bangoria 
1002e85d597SRavi Bangoria 	fd = __event(HW_BREAKPOINT_W, (void *)&data1, sizeof(data1));
1012e85d597SRavi Bangoria 	if (fd < 0)
1022e85d597SRavi Bangoria 		return -1;
1032e85d597SRavi Bangoria 
1042e85d597SRavi Bangoria 	tmp = data1;
1052e85d597SRavi Bangoria 	WP_TEST_ASSERT_VAL(fd, "WO watchpoint", 0);
1062e85d597SRavi Bangoria 
1072e85d597SRavi Bangoria 	data1 = tmp1 + tmp;
1082e85d597SRavi Bangoria 	WP_TEST_ASSERT_VAL(fd, "WO watchpoint", 1);
1092e85d597SRavi Bangoria 
1102e85d597SRavi Bangoria 	close(fd);
1112e85d597SRavi Bangoria 	return 0;
112*e47c6ecaSIan Rogers #endif
1132e85d597SRavi Bangoria }
1142e85d597SRavi Bangoria 
115*e47c6ecaSIan Rogers static int test__wp_rw(struct test_suite *test __maybe_unused,
116*e47c6ecaSIan Rogers 		       int subtest __maybe_unused)
1172e85d597SRavi Bangoria {
118*e47c6ecaSIan Rogers #if defined(__s390x__)
119*e47c6ecaSIan Rogers 	return TEST_SKIP;
120*e47c6ecaSIan Rogers #else
1212e85d597SRavi Bangoria 	int fd;
1222e85d597SRavi Bangoria 	unsigned long tmp, tmp1 = rand();
1232e85d597SRavi Bangoria 
1242e85d597SRavi Bangoria 	fd = __event(HW_BREAKPOINT_R | HW_BREAKPOINT_W, (void *)&data1,
1252e85d597SRavi Bangoria 		     sizeof(data1));
1262e85d597SRavi Bangoria 	if (fd < 0)
1272e85d597SRavi Bangoria 		return -1;
1282e85d597SRavi Bangoria 
1292e85d597SRavi Bangoria 	tmp = data1;
1302e85d597SRavi Bangoria 	WP_TEST_ASSERT_VAL(fd, "RW watchpoint", 1);
1312e85d597SRavi Bangoria 
1322e85d597SRavi Bangoria 	data1 = tmp1 + tmp;
1332e85d597SRavi Bangoria 	WP_TEST_ASSERT_VAL(fd, "RW watchpoint", 2);
1342e85d597SRavi Bangoria 
1352e85d597SRavi Bangoria 	close(fd);
1362e85d597SRavi Bangoria 	return 0;
137*e47c6ecaSIan Rogers #endif
1382e85d597SRavi Bangoria }
1392e85d597SRavi Bangoria 
140*e47c6ecaSIan Rogers static int test__wp_modify(struct test_suite *test __maybe_unused,
141*e47c6ecaSIan Rogers 			   int subtest __maybe_unused)
1422e85d597SRavi Bangoria {
143*e47c6ecaSIan Rogers #if defined(__s390x__)
144*e47c6ecaSIan Rogers 	return TEST_SKIP;
145*e47c6ecaSIan Rogers #else
1462e85d597SRavi Bangoria 	int fd, ret;
1472e85d597SRavi Bangoria 	unsigned long tmp = rand();
1482e85d597SRavi Bangoria 	struct perf_event_attr new_attr;
1492e85d597SRavi Bangoria 
1502e85d597SRavi Bangoria 	fd = __event(HW_BREAKPOINT_W, (void *)&data1, sizeof(data1));
1512e85d597SRavi Bangoria 	if (fd < 0)
1522e85d597SRavi Bangoria 		return -1;
1532e85d597SRavi Bangoria 
1542e85d597SRavi Bangoria 	data1 = tmp;
1552e85d597SRavi Bangoria 	WP_TEST_ASSERT_VAL(fd, "Modify watchpoint", 1);
1562e85d597SRavi Bangoria 
1572e85d597SRavi Bangoria 	/* Modify watchpoint with disabled = 1 */
1582e85d597SRavi Bangoria 	get__perf_event_attr(&new_attr, HW_BREAKPOINT_W, (void *)&data2[0],
1592e85d597SRavi Bangoria 			     sizeof(u8) * 2);
1602e85d597SRavi Bangoria 	new_attr.disabled = 1;
1612e85d597SRavi Bangoria 	ret = ioctl(fd, PERF_EVENT_IOC_MODIFY_ATTRIBUTES, &new_attr);
1622e85d597SRavi Bangoria 	if (ret < 0) {
1632e85d597SRavi Bangoria 		pr_debug("ioctl(PERF_EVENT_IOC_MODIFY_ATTRIBUTES) failed\n");
1642e85d597SRavi Bangoria 		close(fd);
1652e85d597SRavi Bangoria 		return ret;
1662e85d597SRavi Bangoria 	}
1672e85d597SRavi Bangoria 
1682e85d597SRavi Bangoria 	data2[1] = tmp; /* Not Counted */
1692e85d597SRavi Bangoria 	WP_TEST_ASSERT_VAL(fd, "Modify watchpoint", 1);
1702e85d597SRavi Bangoria 
1712e85d597SRavi Bangoria 	/* Enable the event */
1722e85d597SRavi Bangoria 	ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
1732e85d597SRavi Bangoria 	if (ret < 0) {
1742e85d597SRavi Bangoria 		pr_debug("Failed to enable event\n");
1752e85d597SRavi Bangoria 		close(fd);
1762e85d597SRavi Bangoria 		return ret;
1772e85d597SRavi Bangoria 	}
1782e85d597SRavi Bangoria 
1792e85d597SRavi Bangoria 	data2[1] = tmp; /* Counted */
1802e85d597SRavi Bangoria 	WP_TEST_ASSERT_VAL(fd, "Modify watchpoint", 2);
1812e85d597SRavi Bangoria 
1822e85d597SRavi Bangoria 	data2[2] = tmp; /* Not Counted */
1832e85d597SRavi Bangoria 	WP_TEST_ASSERT_VAL(fd, "Modify watchpoint", 2);
1842e85d597SRavi Bangoria 
1852e85d597SRavi Bangoria 	close(fd);
1862e85d597SRavi Bangoria 	return 0;
1872e85d597SRavi Bangoria #endif
1882e85d597SRavi Bangoria }
1892e85d597SRavi Bangoria 
190*e47c6ecaSIan Rogers static struct test_case wp_tests[] = {
191*e47c6ecaSIan Rogers 	TEST_CASE_REASON("Read Only Watchpoint", wp_ro, "missing hardware support"),
192*e47c6ecaSIan Rogers 	TEST_CASE_REASON("Write Only Watchpoint", wp_wo, "missing hardware support"),
193*e47c6ecaSIan Rogers 	TEST_CASE_REASON("Read / Write Watchpoint", wp_rw, "missing hardware support"),
194*e47c6ecaSIan Rogers 	TEST_CASE_REASON("Modify Watchpoint", wp_modify, "missing hardware support"),
195*e47c6ecaSIan Rogers 	{ .name = NULL, }
1962e85d597SRavi Bangoria };
1972e85d597SRavi Bangoria 
19833f44bfdSIan Rogers struct test_suite suite__wp = {
199d68f0365SIan Rogers 	.desc = "Watchpoint",
200*e47c6ecaSIan Rogers 	.test_cases = wp_tests,
201d68f0365SIan Rogers };
202