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 "../perf-sys.h"
22 static volatile long the_var
;
24 static noinline
int test_function(void)
29 static int __event(bool is_x
, void *addr
, struct perf_event_attr
*attr
)
33 memset(attr
, 0, sizeof(struct perf_event_attr
));
34 attr
->type
= PERF_TYPE_BREAKPOINT
;
35 attr
->size
= sizeof(struct perf_event_attr
);
38 attr
->bp_type
= is_x
? HW_BREAKPOINT_X
: HW_BREAKPOINT_W
;
39 attr
->bp_addr
= (unsigned long) addr
;
40 attr
->bp_len
= sizeof(long);
42 attr
->sample_period
= 1;
43 attr
->sample_type
= PERF_SAMPLE_IP
;
45 attr
->exclude_kernel
= 1;
48 fd
= sys_perf_event_open(attr
, -1, 0, -1,
49 perf_event_open_cloexec_flag());
51 pr_debug("failed opening event %llx\n", attr
->config
);
58 static int wp_event(void *addr
, struct perf_event_attr
*attr
)
60 return __event(false, addr
, attr
);
63 static int bp_event(void *addr
, struct perf_event_attr
*attr
)
65 return __event(true, addr
, attr
);
68 static int bp_accounting(int wp_cnt
, int share
)
70 struct perf_event_attr attr
, attr_mod
, attr_new
;
71 int i
, fd
[wp_cnt
], fd_wp
, ret
;
73 for (i
= 0; i
< wp_cnt
; i
++) {
74 fd
[i
] = wp_event((void *)&the_var
, &attr
);
75 TEST_ASSERT_VAL("failed to create wp\n", fd
[i
] != -1);
76 pr_debug("wp %d created\n", i
);
80 attr_mod
.bp_type
= HW_BREAKPOINT_X
;
81 attr_mod
.bp_addr
= (unsigned long) test_function
;
83 ret
= ioctl(fd
[0], PERF_EVENT_IOC_MODIFY_ATTRIBUTES
, &attr_mod
);
84 TEST_ASSERT_VAL("failed to modify wp\n", ret
== 0);
86 pr_debug("wp 0 modified to bp\n");
89 fd_wp
= wp_event((void *)&the_var
, &attr_new
);
90 TEST_ASSERT_VAL("failed to create max wp\n", fd_wp
!= -1);
91 pr_debug("wp max created\n");
94 for (i
= 0; i
< wp_cnt
; i
++)
100 static int detect_cnt(bool is_x
)
102 struct perf_event_attr attr
;
103 void *addr
= is_x
? (void *)test_function
: (void *)&the_var
;
104 int fd
[100], cnt
= 0, i
;
108 pr_debug("way too many debug registers, fix the test\n");
111 fd
[cnt
] = __event(is_x
, addr
, &attr
);
118 for (i
= 0; i
< cnt
; i
++)
124 static int detect_ioctl(void)
126 struct perf_event_attr attr
;
129 fd
= wp_event((void *) &the_var
, &attr
);
131 ret
= ioctl(fd
, PERF_EVENT_IOC_MODIFY_ATTRIBUTES
, &attr
);
138 static int detect_share(int wp_cnt
, int bp_cnt
)
140 struct perf_event_attr attr
;
141 int i
, fd
[wp_cnt
+ bp_cnt
], ret
;
143 for (i
= 0; i
< wp_cnt
; i
++) {
144 fd
[i
] = wp_event((void *)&the_var
, &attr
);
145 TEST_ASSERT_VAL("failed to create wp\n", fd
[i
] != -1);
148 for (; i
< (bp_cnt
+ wp_cnt
); i
++) {
149 fd
[i
] = bp_event((void *)test_function
, &attr
);
154 ret
= i
!= (bp_cnt
+ wp_cnt
);
163 * This test does following:
164 * - detects the number of watch/break-points,
165 * skip test if any is missing
166 * - detects PERF_EVENT_IOC_MODIFY_ATTRIBUTES ioctl,
167 * skip test if it's missing
168 * - detects if watchpoints and breakpoints share
170 * - create all possible watchpoints on cpu 0
171 * - change one of it to breakpoint
172 * - in case wp and bp do not share slots,
173 * we create another watchpoint to ensure
174 * the slot accounting is correct
176 int test__bp_accounting(struct test
*test __maybe_unused
, int subtest __maybe_unused
)
178 int has_ioctl
= detect_ioctl();
179 int wp_cnt
= detect_cnt(false);
180 int bp_cnt
= detect_cnt(true);
181 int share
= detect_share(wp_cnt
, bp_cnt
);
183 pr_debug("watchpoints count %d, breakpoints count %d, has_ioctl %d, share %d\n",
184 wp_cnt
, bp_cnt
, has_ioctl
, share
);
186 if (!wp_cnt
|| !bp_cnt
|| !has_ioctl
)
189 return bp_accounting(wp_cnt
, share
);
192 bool test__bp_account_is_supported(void)
195 * PowerPC and S390 do not support creation of instruction
196 * breakpoints using the perf_event interface.
198 * Just disable the test for these architectures until these
199 * issues are resolved.
201 #if defined(__powerpc__) || defined(__s390x__)