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>
17 #include <linux/compiler.h>
18 #include <linux/hw_breakpoint.h>
23 #include "../perf-sys.h"
26 volatile long the_var;
28 static noinline int test_function(void)
33 static int __event(bool is_x, void *addr, struct perf_event_attr *attr)
37 memset(attr, 0, sizeof(struct perf_event_attr));
38 attr->type = PERF_TYPE_BREAKPOINT;
39 attr->size = sizeof(struct perf_event_attr);
42 attr->bp_type = is_x ? HW_BREAKPOINT_X : HW_BREAKPOINT_W;
43 attr->bp_addr = (unsigned long) addr;
44 attr->bp_len = sizeof(long);
46 attr->sample_period = 1;
47 attr->sample_type = PERF_SAMPLE_IP;
49 attr->exclude_kernel = 1;
52 fd = sys_perf_event_open(attr, -1, 0, -1,
53 perf_event_open_cloexec_flag());
55 pr_debug("failed opening event %llx\n", attr->config);
62 static int wp_event(void *addr, struct perf_event_attr *attr)
64 return __event(false, addr, attr);
67 static int bp_event(void *addr, struct perf_event_attr *attr)
69 return __event(true, addr, attr);
72 static int bp_accounting(int wp_cnt, int share)
74 struct perf_event_attr attr, attr_mod, attr_new;
75 int i, fd[wp_cnt], fd_wp, ret;
77 for (i = 0; i < wp_cnt; i++) {
78 fd[i] = wp_event((void *)&the_var, &attr);
79 TEST_ASSERT_VAL("failed to create wp\n", fd[i] != -1);
80 pr_debug("wp %d created\n", i);
84 attr_mod.bp_type = HW_BREAKPOINT_X;
85 attr_mod.bp_addr = (unsigned long) test_function;
87 ret = ioctl(fd[0], PERF_EVENT_IOC_MODIFY_ATTRIBUTES, &attr_mod);
88 TEST_ASSERT_VAL("failed to modify wp\n", ret == 0);
90 pr_debug("wp 0 modified to bp\n");
93 fd_wp = wp_event((void *)&the_var, &attr_new);
94 TEST_ASSERT_VAL("failed to create max wp\n", fd_wp != -1);
95 pr_debug("wp max created\n");
98 for (i = 0; i < wp_cnt; i++)
104 static int detect_cnt(bool is_x)
106 struct perf_event_attr attr;
107 void *addr = is_x ? (void *)test_function : (void *)&the_var;
108 int fd[100], cnt = 0, i;
112 pr_debug("way too many debug registers, fix the test\n");
115 fd[cnt] = __event(is_x, addr, &attr);
122 for (i = 0; i < cnt; i++)
128 static int detect_ioctl(void)
130 struct perf_event_attr attr;
133 fd = wp_event((void *) &the_var, &attr);
135 ret = ioctl(fd, PERF_EVENT_IOC_MODIFY_ATTRIBUTES, &attr);
142 static int detect_share(int wp_cnt, int bp_cnt)
144 struct perf_event_attr attr;
145 int i, fd[wp_cnt + bp_cnt], ret;
147 for (i = 0; i < wp_cnt; i++) {
148 fd[i] = wp_event((void *)&the_var, &attr);
149 TEST_ASSERT_VAL("failed to create wp\n", fd[i] != -1);
152 for (; i < (bp_cnt + wp_cnt); i++) {
153 fd[i] = bp_event((void *)test_function, &attr);
158 ret = i != (bp_cnt + wp_cnt);
167 * This test does following:
168 * - detects the number of watch/break-points,
169 * skip test if any is missing
170 * - detects PERF_EVENT_IOC_MODIFY_ATTRIBUTES ioctl,
171 * skip test if it's missing
172 * - detects if watchpoints and breakpoints share
174 * - create all possible watchpoints on cpu 0
175 * - change one of it to breakpoint
176 * - in case wp and bp do not share slots,
177 * we create another watchpoint to ensure
178 * the slot accounting is correct
180 int test__bp_accounting(struct test *test __maybe_unused, int subtest __maybe_unused)
182 int has_ioctl = detect_ioctl();
183 int wp_cnt = detect_cnt(false);
184 int bp_cnt = detect_cnt(true);
185 int share = detect_share(wp_cnt, bp_cnt);
187 pr_debug("watchpoints count %d, breakpoints count %d, has_ioctl %d, share %d\n",
188 wp_cnt, bp_cnt, has_ioctl, share);
190 if (!wp_cnt || !bp_cnt || !has_ioctl)
193 return bp_accounting(wp_cnt, share);