1 // SPDX-License-Identifier: GPL-2.0
3 * Powerpc needs __SANE_USERSPACE_TYPES__ before <linux/types.h> to select
4 * 'int-ll64.h' and avoid compile warnings when printing __u64 with %llu.
6 #define __SANE_USERSPACE_TYPES__
12 #include <sys/ioctl.h>
14 #include <linux/hw_breakpoint.h>
19 #include "parse-events.h"
20 #include "../perf-sys.h"
24 * PowerPC and S390 do not support creation of instruction breakpoints using the
25 * perf_event interface.
27 * Just disable the test for these architectures until these issues are
30 #if defined(__powerpc__) || defined(__s390x__)
31 #define BP_ACCOUNT_IS_SUPPORTED 0
33 #define BP_ACCOUNT_IS_SUPPORTED 1
36 static volatile long the_var;
38 static noinline int test_function(void)
43 static int __event(bool is_x, void *addr, struct perf_event_attr *attr)
47 memset(attr, 0, sizeof(struct perf_event_attr));
48 attr->type = PERF_TYPE_BREAKPOINT;
49 attr->size = sizeof(struct perf_event_attr);
52 attr->bp_type = is_x ? HW_BREAKPOINT_X : HW_BREAKPOINT_W;
53 attr->bp_addr = (unsigned long) addr;
54 attr->bp_len = is_x ? default_breakpoint_len() : sizeof(long);
56 attr->sample_period = 1;
57 attr->sample_type = PERF_SAMPLE_IP;
59 attr->exclude_kernel = 1;
62 fd = sys_perf_event_open(attr, -1, 0, -1,
63 perf_event_open_cloexec_flag());
65 pr_debug("failed opening event %llx\n", attr->config);
72 static int wp_event(void *addr, struct perf_event_attr *attr)
74 return __event(false, addr, attr);
77 static int bp_event(void *addr, struct perf_event_attr *attr)
79 return __event(true, addr, attr);
82 static int bp_accounting(int wp_cnt, int share)
84 struct perf_event_attr attr, attr_mod, attr_new;
85 int i, fd[wp_cnt], fd_wp, ret;
87 for (i = 0; i < wp_cnt; i++) {
88 fd[i] = wp_event((void *)&the_var, &attr);
89 TEST_ASSERT_VAL("failed to create wp\n", fd[i] != -1);
90 pr_debug("wp %d created\n", i);
94 attr_mod.bp_type = HW_BREAKPOINT_X;
95 attr_mod.bp_addr = (unsigned long) test_function;
96 attr_mod.bp_len = default_breakpoint_len();
98 ret = ioctl(fd[0], PERF_EVENT_IOC_MODIFY_ATTRIBUTES, &attr_mod);
99 TEST_ASSERT_VAL("failed to modify wp\n", ret == 0);
101 pr_debug("wp 0 modified to bp\n");
104 fd_wp = wp_event((void *)&the_var, &attr_new);
105 TEST_ASSERT_VAL("failed to create max wp\n", fd_wp != -1);
106 pr_debug("wp max created\n");
109 for (i = 0; i < wp_cnt; i++)
115 static int detect_cnt(bool is_x)
117 struct perf_event_attr attr;
118 void *addr = is_x ? (void *)test_function : (void *)&the_var;
119 int fd[100], cnt = 0, i;
123 pr_debug("way too many debug registers, fix the test\n");
126 fd[cnt] = __event(is_x, addr, &attr);
133 for (i = 0; i < cnt; i++)
139 static int detect_ioctl(void)
141 struct perf_event_attr attr;
144 fd = wp_event((void *) &the_var, &attr);
146 ret = ioctl(fd, PERF_EVENT_IOC_MODIFY_ATTRIBUTES, &attr);
153 static int detect_share(int wp_cnt, int bp_cnt)
155 struct perf_event_attr attr;
156 int i, *fd = NULL, ret = -1;
158 if (wp_cnt + bp_cnt == 0)
161 fd = malloc(sizeof(int) * (wp_cnt + bp_cnt));
165 for (i = 0; i < wp_cnt; i++) {
166 fd[i] = wp_event((void *)&the_var, &attr);
168 pr_err("failed to create wp\n");
173 for (; i < (bp_cnt + wp_cnt); i++) {
174 fd[i] = bp_event((void *)test_function, &attr);
179 ret = i != (bp_cnt + wp_cnt);
190 * This test does following:
191 * - detects the number of watch/break-points,
192 * skip test if any is missing
193 * - detects PERF_EVENT_IOC_MODIFY_ATTRIBUTES ioctl,
194 * skip test if it's missing
195 * - detects if watchpoints and breakpoints share
197 * - create all possible watchpoints on cpu 0
198 * - change one of it to breakpoint
199 * - in case wp and bp do not share slots,
200 * we create another watchpoint to ensure
201 * the slot accounting is correct
203 static int test__bp_accounting(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
205 int has_ioctl = detect_ioctl();
206 int wp_cnt = detect_cnt(false);
207 int bp_cnt = detect_cnt(true);
208 int share = detect_share(wp_cnt, bp_cnt);
210 if (!BP_ACCOUNT_IS_SUPPORTED) {
211 pr_debug("Test not supported on this architecture");
215 pr_debug("watchpoints count %d, breakpoints count %d, has_ioctl %d, share %d\n",
216 wp_cnt, bp_cnt, has_ioctl, share);
218 if (!wp_cnt || !bp_cnt || !has_ioctl)
221 return bp_accounting(wp_cnt, share);
224 DEFINE_SUITE("Breakpoint accounting", bp_accounting);