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> 5*e1dda48eSNamhyung Kim #include <errno.h> 62e85d597SRavi Bangoria #include <sys/ioctl.h> 7*e1dda48eSNamhyung 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 232e85d597SRavi Bangoria volatile u64 data1; 242e85d597SRavi Bangoria volatile u8 data2[3]; 252e85d597SRavi Bangoria 26ccb05590SArnaldo Carvalho de Melo #ifndef __s390x__ 272e85d597SRavi Bangoria static int wp_read(int fd, long long *count, int size) 282e85d597SRavi Bangoria { 292e85d597SRavi Bangoria int ret = read(fd, count, size); 302e85d597SRavi Bangoria 312e85d597SRavi Bangoria if (ret != size) { 322e85d597SRavi Bangoria pr_debug("failed to read: %d\n", ret); 332e85d597SRavi Bangoria return -1; 342e85d597SRavi Bangoria } 352e85d597SRavi Bangoria return 0; 362e85d597SRavi Bangoria } 372e85d597SRavi Bangoria 382e85d597SRavi Bangoria static void get__perf_event_attr(struct perf_event_attr *attr, int wp_type, 392e85d597SRavi Bangoria void *wp_addr, unsigned long wp_len) 402e85d597SRavi Bangoria { 412e85d597SRavi Bangoria memset(attr, 0, sizeof(struct perf_event_attr)); 422e85d597SRavi Bangoria attr->type = PERF_TYPE_BREAKPOINT; 432e85d597SRavi Bangoria attr->size = sizeof(struct perf_event_attr); 442e85d597SRavi Bangoria attr->config = 0; 452e85d597SRavi Bangoria attr->bp_type = wp_type; 462e85d597SRavi Bangoria attr->bp_addr = (unsigned long)wp_addr; 472e85d597SRavi Bangoria attr->bp_len = wp_len; 482e85d597SRavi Bangoria attr->sample_period = 1; 492e85d597SRavi Bangoria attr->sample_type = PERF_SAMPLE_IP; 502e85d597SRavi Bangoria attr->exclude_kernel = 1; 512e85d597SRavi Bangoria attr->exclude_hv = 1; 522e85d597SRavi Bangoria } 532e85d597SRavi Bangoria 542e85d597SRavi Bangoria static int __event(int wp_type, void *wp_addr, unsigned long wp_len) 552e85d597SRavi Bangoria { 562e85d597SRavi Bangoria int fd; 572e85d597SRavi Bangoria struct perf_event_attr attr; 582e85d597SRavi Bangoria 592e85d597SRavi Bangoria get__perf_event_attr(&attr, wp_type, wp_addr, wp_len); 602e85d597SRavi Bangoria fd = sys_perf_event_open(&attr, 0, -1, -1, 612e85d597SRavi Bangoria perf_event_open_cloexec_flag()); 622e85d597SRavi Bangoria if (fd < 0) 632e85d597SRavi Bangoria pr_debug("failed opening event %x\n", attr.bp_type); 642e85d597SRavi Bangoria 652e85d597SRavi Bangoria return fd; 662e85d597SRavi Bangoria } 67e47c6ecaSIan Rogers #endif 682e85d597SRavi Bangoria 69e47c6ecaSIan Rogers static int test__wp_ro(struct test_suite *test __maybe_unused, 70e47c6ecaSIan Rogers int subtest __maybe_unused) 712e85d597SRavi Bangoria { 72e47c6ecaSIan Rogers #if defined(__s390x__) || defined(__x86_64__) || defined(__i386__) 73e47c6ecaSIan Rogers return TEST_SKIP; 74e47c6ecaSIan Rogers #else 752e85d597SRavi Bangoria int fd; 762e85d597SRavi Bangoria unsigned long tmp, tmp1 = rand(); 772e85d597SRavi Bangoria 782e85d597SRavi Bangoria fd = __event(HW_BREAKPOINT_R, (void *)&data1, sizeof(data1)); 792e85d597SRavi Bangoria if (fd < 0) 802e85d597SRavi Bangoria return -1; 812e85d597SRavi Bangoria 822e85d597SRavi Bangoria tmp = data1; 832e85d597SRavi Bangoria WP_TEST_ASSERT_VAL(fd, "RO watchpoint", 1); 842e85d597SRavi Bangoria 852e85d597SRavi Bangoria data1 = tmp1 + tmp; 862e85d597SRavi Bangoria WP_TEST_ASSERT_VAL(fd, "RO watchpoint", 1); 872e85d597SRavi Bangoria 882e85d597SRavi Bangoria close(fd); 892e85d597SRavi Bangoria return 0; 90e47c6ecaSIan Rogers #endif 912e85d597SRavi Bangoria } 922e85d597SRavi Bangoria 93e47c6ecaSIan Rogers static int test__wp_wo(struct test_suite *test __maybe_unused, 94e47c6ecaSIan Rogers int subtest __maybe_unused) 952e85d597SRavi Bangoria { 96e47c6ecaSIan Rogers #if defined(__s390x__) 97e47c6ecaSIan Rogers return TEST_SKIP; 98e47c6ecaSIan Rogers #else 992e85d597SRavi Bangoria int fd; 1002e85d597SRavi Bangoria unsigned long tmp, tmp1 = rand(); 1012e85d597SRavi Bangoria 1022e85d597SRavi Bangoria fd = __event(HW_BREAKPOINT_W, (void *)&data1, sizeof(data1)); 1032e85d597SRavi Bangoria if (fd < 0) 1042e85d597SRavi Bangoria return -1; 1052e85d597SRavi Bangoria 1062e85d597SRavi Bangoria tmp = data1; 1072e85d597SRavi Bangoria WP_TEST_ASSERT_VAL(fd, "WO watchpoint", 0); 1082e85d597SRavi Bangoria 1092e85d597SRavi Bangoria data1 = tmp1 + tmp; 1102e85d597SRavi Bangoria WP_TEST_ASSERT_VAL(fd, "WO watchpoint", 1); 1112e85d597SRavi Bangoria 1122e85d597SRavi Bangoria close(fd); 1132e85d597SRavi Bangoria return 0; 114e47c6ecaSIan Rogers #endif 1152e85d597SRavi Bangoria } 1162e85d597SRavi Bangoria 117e47c6ecaSIan Rogers static int test__wp_rw(struct test_suite *test __maybe_unused, 118e47c6ecaSIan Rogers int subtest __maybe_unused) 1192e85d597SRavi Bangoria { 120e47c6ecaSIan Rogers #if defined(__s390x__) 121e47c6ecaSIan Rogers return TEST_SKIP; 122e47c6ecaSIan Rogers #else 1232e85d597SRavi Bangoria int fd; 1242e85d597SRavi Bangoria unsigned long tmp, tmp1 = rand(); 1252e85d597SRavi Bangoria 1262e85d597SRavi Bangoria fd = __event(HW_BREAKPOINT_R | HW_BREAKPOINT_W, (void *)&data1, 1272e85d597SRavi Bangoria sizeof(data1)); 1282e85d597SRavi Bangoria if (fd < 0) 1292e85d597SRavi Bangoria return -1; 1302e85d597SRavi Bangoria 1312e85d597SRavi Bangoria tmp = data1; 1322e85d597SRavi Bangoria WP_TEST_ASSERT_VAL(fd, "RW watchpoint", 1); 1332e85d597SRavi Bangoria 1342e85d597SRavi Bangoria data1 = tmp1 + tmp; 1352e85d597SRavi Bangoria WP_TEST_ASSERT_VAL(fd, "RW watchpoint", 2); 1362e85d597SRavi Bangoria 1372e85d597SRavi Bangoria close(fd); 1382e85d597SRavi Bangoria return 0; 139e47c6ecaSIan Rogers #endif 1402e85d597SRavi Bangoria } 1412e85d597SRavi Bangoria 142*e1dda48eSNamhyung Kim static int test__wp_modify(struct test_suite *test __maybe_unused, int subtest __maybe_unused) 1432e85d597SRavi Bangoria { 144e47c6ecaSIan Rogers #if defined(__s390x__) 145e47c6ecaSIan Rogers return TEST_SKIP; 146e47c6ecaSIan Rogers #else 1472e85d597SRavi Bangoria int fd, ret; 1482e85d597SRavi Bangoria unsigned long tmp = rand(); 1492e85d597SRavi Bangoria struct perf_event_attr new_attr; 1502e85d597SRavi Bangoria 1512e85d597SRavi Bangoria fd = __event(HW_BREAKPOINT_W, (void *)&data1, sizeof(data1)); 1522e85d597SRavi Bangoria if (fd < 0) 1532e85d597SRavi Bangoria return -1; 1542e85d597SRavi Bangoria 1552e85d597SRavi Bangoria data1 = tmp; 1562e85d597SRavi Bangoria WP_TEST_ASSERT_VAL(fd, "Modify watchpoint", 1); 1572e85d597SRavi Bangoria 1582e85d597SRavi Bangoria /* Modify watchpoint with disabled = 1 */ 1592e85d597SRavi Bangoria get__perf_event_attr(&new_attr, HW_BREAKPOINT_W, (void *)&data2[0], 1602e85d597SRavi Bangoria sizeof(u8) * 2); 1612e85d597SRavi Bangoria new_attr.disabled = 1; 1622e85d597SRavi Bangoria ret = ioctl(fd, PERF_EVENT_IOC_MODIFY_ATTRIBUTES, &new_attr); 1632e85d597SRavi Bangoria if (ret < 0) { 164*e1dda48eSNamhyung Kim if (errno == ENOTTY) { 165*e1dda48eSNamhyung Kim test->test_cases[subtest].skip_reason = "missing kernel support"; 166*e1dda48eSNamhyung Kim ret = TEST_SKIP; 167*e1dda48eSNamhyung Kim } 168*e1dda48eSNamhyung Kim 1692e85d597SRavi Bangoria pr_debug("ioctl(PERF_EVENT_IOC_MODIFY_ATTRIBUTES) failed\n"); 1702e85d597SRavi Bangoria close(fd); 1712e85d597SRavi Bangoria return ret; 1722e85d597SRavi Bangoria } 1732e85d597SRavi Bangoria 1742e85d597SRavi Bangoria data2[1] = tmp; /* Not Counted */ 1752e85d597SRavi Bangoria WP_TEST_ASSERT_VAL(fd, "Modify watchpoint", 1); 1762e85d597SRavi Bangoria 1772e85d597SRavi Bangoria /* Enable the event */ 1782e85d597SRavi Bangoria ioctl(fd, PERF_EVENT_IOC_ENABLE, 0); 1792e85d597SRavi Bangoria if (ret < 0) { 1802e85d597SRavi Bangoria pr_debug("Failed to enable event\n"); 1812e85d597SRavi Bangoria close(fd); 1822e85d597SRavi Bangoria return ret; 1832e85d597SRavi Bangoria } 1842e85d597SRavi Bangoria 1852e85d597SRavi Bangoria data2[1] = tmp; /* Counted */ 1862e85d597SRavi Bangoria WP_TEST_ASSERT_VAL(fd, "Modify watchpoint", 2); 1872e85d597SRavi Bangoria 1882e85d597SRavi Bangoria data2[2] = tmp; /* Not Counted */ 1892e85d597SRavi Bangoria WP_TEST_ASSERT_VAL(fd, "Modify watchpoint", 2); 1902e85d597SRavi Bangoria 1912e85d597SRavi Bangoria close(fd); 1922e85d597SRavi Bangoria return 0; 1932e85d597SRavi Bangoria #endif 1942e85d597SRavi Bangoria } 1952e85d597SRavi Bangoria 196e47c6ecaSIan Rogers static struct test_case wp_tests[] = { 197e47c6ecaSIan Rogers TEST_CASE_REASON("Read Only Watchpoint", wp_ro, "missing hardware support"), 198e47c6ecaSIan Rogers TEST_CASE_REASON("Write Only Watchpoint", wp_wo, "missing hardware support"), 199e47c6ecaSIan Rogers TEST_CASE_REASON("Read / Write Watchpoint", wp_rw, "missing hardware support"), 200e47c6ecaSIan Rogers TEST_CASE_REASON("Modify Watchpoint", wp_modify, "missing hardware support"), 201e47c6ecaSIan Rogers { .name = NULL, } 2022e85d597SRavi Bangoria }; 2032e85d597SRavi Bangoria 20433f44bfdSIan Rogers struct test_suite suite__wp = { 205d68f0365SIan Rogers .desc = "Watchpoint", 206e47c6ecaSIan Rogers .test_cases = wp_tests, 207d68f0365SIan Rogers }; 208