18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Powerpc needs __SANE_USERSPACE_TYPES__ before <linux/types.h> to select 48c2ecf20Sopenharmony_ci * 'int-ll64.h' and avoid compile warnings when printing __u64 with %llu. 58c2ecf20Sopenharmony_ci */ 68c2ecf20Sopenharmony_ci#define __SANE_USERSPACE_TYPES__ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <stdlib.h> 98c2ecf20Sopenharmony_ci#include <stdio.h> 108c2ecf20Sopenharmony_ci#include <unistd.h> 118c2ecf20Sopenharmony_ci#include <string.h> 128c2ecf20Sopenharmony_ci#include <sys/ioctl.h> 138c2ecf20Sopenharmony_ci#include <fcntl.h> 148c2ecf20Sopenharmony_ci#include <linux/hw_breakpoint.h> 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#include "tests.h" 178c2ecf20Sopenharmony_ci#include "debug.h" 188c2ecf20Sopenharmony_ci#include "event.h" 198c2ecf20Sopenharmony_ci#include "../perf-sys.h" 208c2ecf20Sopenharmony_ci#include "cloexec.h" 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_cistatic volatile long the_var; 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_cistatic noinline int test_function(void) 258c2ecf20Sopenharmony_ci{ 268c2ecf20Sopenharmony_ci return 0; 278c2ecf20Sopenharmony_ci} 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_cistatic int __event(bool is_x, void *addr, struct perf_event_attr *attr) 308c2ecf20Sopenharmony_ci{ 318c2ecf20Sopenharmony_ci int fd; 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci memset(attr, 0, sizeof(struct perf_event_attr)); 348c2ecf20Sopenharmony_ci attr->type = PERF_TYPE_BREAKPOINT; 358c2ecf20Sopenharmony_ci attr->size = sizeof(struct perf_event_attr); 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci attr->config = 0; 388c2ecf20Sopenharmony_ci attr->bp_type = is_x ? HW_BREAKPOINT_X : HW_BREAKPOINT_W; 398c2ecf20Sopenharmony_ci attr->bp_addr = (unsigned long) addr; 408c2ecf20Sopenharmony_ci attr->bp_len = sizeof(long); 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci attr->sample_period = 1; 438c2ecf20Sopenharmony_ci attr->sample_type = PERF_SAMPLE_IP; 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci attr->exclude_kernel = 1; 468c2ecf20Sopenharmony_ci attr->exclude_hv = 1; 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci fd = sys_perf_event_open(attr, -1, 0, -1, 498c2ecf20Sopenharmony_ci perf_event_open_cloexec_flag()); 508c2ecf20Sopenharmony_ci if (fd < 0) { 518c2ecf20Sopenharmony_ci pr_debug("failed opening event %llx\n", attr->config); 528c2ecf20Sopenharmony_ci return TEST_FAIL; 538c2ecf20Sopenharmony_ci } 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci return fd; 568c2ecf20Sopenharmony_ci} 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_cistatic int wp_event(void *addr, struct perf_event_attr *attr) 598c2ecf20Sopenharmony_ci{ 608c2ecf20Sopenharmony_ci return __event(false, addr, attr); 618c2ecf20Sopenharmony_ci} 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_cistatic int bp_event(void *addr, struct perf_event_attr *attr) 648c2ecf20Sopenharmony_ci{ 658c2ecf20Sopenharmony_ci return __event(true, addr, attr); 668c2ecf20Sopenharmony_ci} 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_cistatic int bp_accounting(int wp_cnt, int share) 698c2ecf20Sopenharmony_ci{ 708c2ecf20Sopenharmony_ci struct perf_event_attr attr, attr_mod, attr_new; 718c2ecf20Sopenharmony_ci int i, fd[wp_cnt], fd_wp, ret; 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci for (i = 0; i < wp_cnt; i++) { 748c2ecf20Sopenharmony_ci fd[i] = wp_event((void *)&the_var, &attr); 758c2ecf20Sopenharmony_ci TEST_ASSERT_VAL("failed to create wp\n", fd[i] != -1); 768c2ecf20Sopenharmony_ci pr_debug("wp %d created\n", i); 778c2ecf20Sopenharmony_ci } 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci attr_mod = attr; 808c2ecf20Sopenharmony_ci attr_mod.bp_type = HW_BREAKPOINT_X; 818c2ecf20Sopenharmony_ci attr_mod.bp_addr = (unsigned long) test_function; 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci ret = ioctl(fd[0], PERF_EVENT_IOC_MODIFY_ATTRIBUTES, &attr_mod); 848c2ecf20Sopenharmony_ci TEST_ASSERT_VAL("failed to modify wp\n", ret == 0); 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci pr_debug("wp 0 modified to bp\n"); 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci if (!share) { 898c2ecf20Sopenharmony_ci fd_wp = wp_event((void *)&the_var, &attr_new); 908c2ecf20Sopenharmony_ci TEST_ASSERT_VAL("failed to create max wp\n", fd_wp != -1); 918c2ecf20Sopenharmony_ci pr_debug("wp max created\n"); 928c2ecf20Sopenharmony_ci } 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci for (i = 0; i < wp_cnt; i++) 958c2ecf20Sopenharmony_ci close(fd[i]); 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci return 0; 988c2ecf20Sopenharmony_ci} 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_cistatic int detect_cnt(bool is_x) 1018c2ecf20Sopenharmony_ci{ 1028c2ecf20Sopenharmony_ci struct perf_event_attr attr; 1038c2ecf20Sopenharmony_ci void *addr = is_x ? (void *)test_function : (void *)&the_var; 1048c2ecf20Sopenharmony_ci int fd[100], cnt = 0, i; 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci while (1) { 1078c2ecf20Sopenharmony_ci if (cnt == 100) { 1088c2ecf20Sopenharmony_ci pr_debug("way too many debug registers, fix the test\n"); 1098c2ecf20Sopenharmony_ci return 0; 1108c2ecf20Sopenharmony_ci } 1118c2ecf20Sopenharmony_ci fd[cnt] = __event(is_x, addr, &attr); 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci if (fd[cnt] < 0) 1148c2ecf20Sopenharmony_ci break; 1158c2ecf20Sopenharmony_ci cnt++; 1168c2ecf20Sopenharmony_ci } 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci for (i = 0; i < cnt; i++) 1198c2ecf20Sopenharmony_ci close(fd[i]); 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci return cnt; 1228c2ecf20Sopenharmony_ci} 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_cistatic int detect_ioctl(void) 1258c2ecf20Sopenharmony_ci{ 1268c2ecf20Sopenharmony_ci struct perf_event_attr attr; 1278c2ecf20Sopenharmony_ci int fd, ret = 1; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci fd = wp_event((void *) &the_var, &attr); 1308c2ecf20Sopenharmony_ci if (fd > 0) { 1318c2ecf20Sopenharmony_ci ret = ioctl(fd, PERF_EVENT_IOC_MODIFY_ATTRIBUTES, &attr); 1328c2ecf20Sopenharmony_ci close(fd); 1338c2ecf20Sopenharmony_ci } 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci return ret ? 0 : 1; 1368c2ecf20Sopenharmony_ci} 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_cistatic int detect_share(int wp_cnt, int bp_cnt) 1398c2ecf20Sopenharmony_ci{ 1408c2ecf20Sopenharmony_ci struct perf_event_attr attr; 1418c2ecf20Sopenharmony_ci int i, fd[wp_cnt + bp_cnt], ret; 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci for (i = 0; i < wp_cnt; i++) { 1448c2ecf20Sopenharmony_ci fd[i] = wp_event((void *)&the_var, &attr); 1458c2ecf20Sopenharmony_ci TEST_ASSERT_VAL("failed to create wp\n", fd[i] != -1); 1468c2ecf20Sopenharmony_ci } 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci for (; i < (bp_cnt + wp_cnt); i++) { 1498c2ecf20Sopenharmony_ci fd[i] = bp_event((void *)test_function, &attr); 1508c2ecf20Sopenharmony_ci if (fd[i] == -1) 1518c2ecf20Sopenharmony_ci break; 1528c2ecf20Sopenharmony_ci } 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci ret = i != (bp_cnt + wp_cnt); 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci while (i--) 1578c2ecf20Sopenharmony_ci close(fd[i]); 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci return ret; 1608c2ecf20Sopenharmony_ci} 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci/* 1638c2ecf20Sopenharmony_ci * This test does following: 1648c2ecf20Sopenharmony_ci * - detects the number of watch/break-points, 1658c2ecf20Sopenharmony_ci * skip test if any is missing 1668c2ecf20Sopenharmony_ci * - detects PERF_EVENT_IOC_MODIFY_ATTRIBUTES ioctl, 1678c2ecf20Sopenharmony_ci * skip test if it's missing 1688c2ecf20Sopenharmony_ci * - detects if watchpoints and breakpoints share 1698c2ecf20Sopenharmony_ci * same slots 1708c2ecf20Sopenharmony_ci * - create all possible watchpoints on cpu 0 1718c2ecf20Sopenharmony_ci * - change one of it to breakpoint 1728c2ecf20Sopenharmony_ci * - in case wp and bp do not share slots, 1738c2ecf20Sopenharmony_ci * we create another watchpoint to ensure 1748c2ecf20Sopenharmony_ci * the slot accounting is correct 1758c2ecf20Sopenharmony_ci */ 1768c2ecf20Sopenharmony_ciint test__bp_accounting(struct test *test __maybe_unused, int subtest __maybe_unused) 1778c2ecf20Sopenharmony_ci{ 1788c2ecf20Sopenharmony_ci int has_ioctl = detect_ioctl(); 1798c2ecf20Sopenharmony_ci int wp_cnt = detect_cnt(false); 1808c2ecf20Sopenharmony_ci int bp_cnt = detect_cnt(true); 1818c2ecf20Sopenharmony_ci int share = detect_share(wp_cnt, bp_cnt); 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci pr_debug("watchpoints count %d, breakpoints count %d, has_ioctl %d, share %d\n", 1848c2ecf20Sopenharmony_ci wp_cnt, bp_cnt, has_ioctl, share); 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci if (!wp_cnt || !bp_cnt || !has_ioctl) 1878c2ecf20Sopenharmony_ci return TEST_SKIP; 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci return bp_accounting(wp_cnt, share); 1908c2ecf20Sopenharmony_ci} 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_cibool test__bp_account_is_supported(void) 1938c2ecf20Sopenharmony_ci{ 1948c2ecf20Sopenharmony_ci /* 1958c2ecf20Sopenharmony_ci * PowerPC and S390 do not support creation of instruction 1968c2ecf20Sopenharmony_ci * breakpoints using the perf_event interface. 1978c2ecf20Sopenharmony_ci * 1988c2ecf20Sopenharmony_ci * Just disable the test for these architectures until these 1998c2ecf20Sopenharmony_ci * issues are resolved. 2008c2ecf20Sopenharmony_ci */ 2018c2ecf20Sopenharmony_ci#if defined(__powerpc__) || defined(__s390x__) 2028c2ecf20Sopenharmony_ci return false; 2038c2ecf20Sopenharmony_ci#else 2048c2ecf20Sopenharmony_ci return true; 2058c2ecf20Sopenharmony_ci#endif 2068c2ecf20Sopenharmony_ci} 207