1 // SPDX-License-Identifier: GPL-2.0
6 #include <linux/hw_breakpoint.h>
7 #include <linux/kernel.h>
12 #include "../perf-sys.h"
14 #define WP_TEST_ASSERT_VAL(fd, text, val) \
17 wp_read(fd, &count, sizeof(long long)); \
18 TEST_ASSERT_VAL(text, count == val); \
24 static int wp_read(int fd
, long long *count
, int size
)
26 int ret
= read(fd
, count
, size
);
29 pr_debug("failed to read: %d\n", ret
);
35 static void get__perf_event_attr(struct perf_event_attr
*attr
, int wp_type
,
36 void *wp_addr
, unsigned long wp_len
)
38 memset(attr
, 0, sizeof(struct perf_event_attr
));
39 attr
->type
= PERF_TYPE_BREAKPOINT
;
40 attr
->size
= sizeof(struct perf_event_attr
);
42 attr
->bp_type
= wp_type
;
43 attr
->bp_addr
= (unsigned long)wp_addr
;
44 attr
->bp_len
= wp_len
;
45 attr
->sample_period
= 1;
46 attr
->sample_type
= PERF_SAMPLE_IP
;
47 attr
->exclude_kernel
= 1;
51 static int __event(int wp_type
, void *wp_addr
, unsigned long wp_len
)
54 struct perf_event_attr attr
;
56 get__perf_event_attr(&attr
, wp_type
, wp_addr
, wp_len
);
57 fd
= sys_perf_event_open(&attr
, 0, -1, -1,
58 perf_event_open_cloexec_flag());
60 pr_debug("failed opening event %x\n", attr
.bp_type
);
65 static int wp_ro_test(void)
68 unsigned long tmp
, tmp1
= rand();
70 fd
= __event(HW_BREAKPOINT_R
, (void *)&data1
, sizeof(data1
));
75 WP_TEST_ASSERT_VAL(fd
, "RO watchpoint", 1);
78 WP_TEST_ASSERT_VAL(fd
, "RO watchpoint", 1);
84 static int wp_wo_test(void)
87 unsigned long tmp
, tmp1
= rand();
89 fd
= __event(HW_BREAKPOINT_W
, (void *)&data1
, sizeof(data1
));
94 WP_TEST_ASSERT_VAL(fd
, "WO watchpoint", 0);
97 WP_TEST_ASSERT_VAL(fd
, "WO watchpoint", 1);
103 static int wp_rw_test(void)
106 unsigned long tmp
, tmp1
= rand();
108 fd
= __event(HW_BREAKPOINT_R
| HW_BREAKPOINT_W
, (void *)&data1
,
114 WP_TEST_ASSERT_VAL(fd
, "RW watchpoint", 1);
117 WP_TEST_ASSERT_VAL(fd
, "RW watchpoint", 2);
123 static int wp_modify_test(void)
126 unsigned long tmp
= rand();
127 struct perf_event_attr new_attr
;
129 fd
= __event(HW_BREAKPOINT_W
, (void *)&data1
, sizeof(data1
));
134 WP_TEST_ASSERT_VAL(fd
, "Modify watchpoint", 1);
136 /* Modify watchpoint with disabled = 1 */
137 get__perf_event_attr(&new_attr
, HW_BREAKPOINT_W
, (void *)&data2
[0],
139 new_attr
.disabled
= 1;
140 ret
= ioctl(fd
, PERF_EVENT_IOC_MODIFY_ATTRIBUTES
, &new_attr
);
142 pr_debug("ioctl(PERF_EVENT_IOC_MODIFY_ATTRIBUTES) failed\n");
147 data2
[1] = tmp
; /* Not Counted */
148 WP_TEST_ASSERT_VAL(fd
, "Modify watchpoint", 1);
150 /* Enable the event */
151 ioctl(fd
, PERF_EVENT_IOC_ENABLE
, 0);
153 pr_debug("Failed to enable event\n");
158 data2
[1] = tmp
; /* Counted */
159 WP_TEST_ASSERT_VAL(fd
, "Modify watchpoint", 2);
161 data2
[2] = tmp
; /* Not Counted */
162 WP_TEST_ASSERT_VAL(fd
, "Modify watchpoint", 2);
168 static bool wp_ro_supported(void)
170 #if defined (__x86_64__) || defined (__i386__)
177 static void wp_ro_skip_msg(void)
179 #if defined (__x86_64__) || defined (__i386__)
180 pr_debug("Hardware does not support read only watchpoints.\n");
186 int (*target_func
)(void);
187 bool (*is_supported
)(void);
188 void (*skip_msg
)(void);
189 } wp_testcase_table
[] = {
191 .desc
= "Read Only Watchpoint",
192 .target_func
= &wp_ro_test
,
193 .is_supported
= &wp_ro_supported
,
194 .skip_msg
= &wp_ro_skip_msg
,
197 .desc
= "Write Only Watchpoint",
198 .target_func
= &wp_wo_test
,
201 .desc
= "Read / Write Watchpoint",
202 .target_func
= &wp_rw_test
,
205 .desc
= "Modify Watchpoint",
206 .target_func
= &wp_modify_test
,
210 int test__wp_subtest_get_nr(void)
212 return (int)ARRAY_SIZE(wp_testcase_table
);
215 const char *test__wp_subtest_get_desc(int i
)
217 if (i
< 0 || i
>= (int)ARRAY_SIZE(wp_testcase_table
))
219 return wp_testcase_table
[i
].desc
;
222 int test__wp(struct test
*test __maybe_unused
, int i
)
224 if (i
< 0 || i
>= (int)ARRAY_SIZE(wp_testcase_table
))
227 if (wp_testcase_table
[i
].is_supported
&&
228 !wp_testcase_table
[i
].is_supported()) {
229 wp_testcase_table
[i
].skip_msg();
233 return !wp_testcase_table
[i
].target_func() ? TEST_OK
: TEST_FAIL
;
236 /* The s390 so far does not have support for
237 * instruction breakpoint using the perf_event_open() system call.
239 bool test__wp_is_supported(void)
241 #if defined(__s390x__)