1 // SPDX-License-Identifier: GPL-2.0
4 #include <linux/hw_breakpoint.h>
9 #define WP_TEST_ASSERT_VAL(fd, text, val) \
12 wp_read(fd, &count, sizeof(long long)); \
13 TEST_ASSERT_VAL(text, count == val); \
19 static int wp_read(int fd
, long long *count
, int size
)
21 int ret
= read(fd
, count
, size
);
24 pr_debug("failed to read: %d\n", ret
);
30 static void get__perf_event_attr(struct perf_event_attr
*attr
, int wp_type
,
31 void *wp_addr
, unsigned long wp_len
)
33 memset(attr
, 0, sizeof(struct perf_event_attr
));
34 attr
->type
= PERF_TYPE_BREAKPOINT
;
35 attr
->size
= sizeof(struct perf_event_attr
);
37 attr
->bp_type
= wp_type
;
38 attr
->bp_addr
= (unsigned long)wp_addr
;
39 attr
->bp_len
= wp_len
;
40 attr
->sample_period
= 1;
41 attr
->sample_type
= PERF_SAMPLE_IP
;
42 attr
->exclude_kernel
= 1;
46 static int __event(int wp_type
, void *wp_addr
, unsigned long wp_len
)
49 struct perf_event_attr attr
;
51 get__perf_event_attr(&attr
, wp_type
, wp_addr
, wp_len
);
52 fd
= sys_perf_event_open(&attr
, 0, -1, -1,
53 perf_event_open_cloexec_flag());
55 pr_debug("failed opening event %x\n", attr
.bp_type
);
60 static int wp_ro_test(void)
63 unsigned long tmp
, tmp1
= rand();
65 fd
= __event(HW_BREAKPOINT_R
, (void *)&data1
, sizeof(data1
));
70 WP_TEST_ASSERT_VAL(fd
, "RO watchpoint", 1);
73 WP_TEST_ASSERT_VAL(fd
, "RO watchpoint", 1);
79 static int wp_wo_test(void)
82 unsigned long tmp
, tmp1
= rand();
84 fd
= __event(HW_BREAKPOINT_W
, (void *)&data1
, sizeof(data1
));
89 WP_TEST_ASSERT_VAL(fd
, "WO watchpoint", 0);
92 WP_TEST_ASSERT_VAL(fd
, "WO watchpoint", 1);
98 static int wp_rw_test(void)
101 unsigned long tmp
, tmp1
= rand();
103 fd
= __event(HW_BREAKPOINT_R
| HW_BREAKPOINT_W
, (void *)&data1
,
109 WP_TEST_ASSERT_VAL(fd
, "RW watchpoint", 1);
112 WP_TEST_ASSERT_VAL(fd
, "RW watchpoint", 2);
118 static int wp_modify_test(void)
121 unsigned long tmp
= rand();
122 struct perf_event_attr new_attr
;
124 fd
= __event(HW_BREAKPOINT_W
, (void *)&data1
, sizeof(data1
));
129 WP_TEST_ASSERT_VAL(fd
, "Modify watchpoint", 1);
131 /* Modify watchpoint with disabled = 1 */
132 get__perf_event_attr(&new_attr
, HW_BREAKPOINT_W
, (void *)&data2
[0],
134 new_attr
.disabled
= 1;
135 ret
= ioctl(fd
, PERF_EVENT_IOC_MODIFY_ATTRIBUTES
, &new_attr
);
137 pr_debug("ioctl(PERF_EVENT_IOC_MODIFY_ATTRIBUTES) failed\n");
142 data2
[1] = tmp
; /* Not Counted */
143 WP_TEST_ASSERT_VAL(fd
, "Modify watchpoint", 1);
145 /* Enable the event */
146 ioctl(fd
, PERF_EVENT_IOC_ENABLE
, 0);
148 pr_debug("Failed to enable event\n");
153 data2
[1] = tmp
; /* Counted */
154 WP_TEST_ASSERT_VAL(fd
, "Modify watchpoint", 2);
156 data2
[2] = tmp
; /* Not Counted */
157 WP_TEST_ASSERT_VAL(fd
, "Modify watchpoint", 2);
163 static bool wp_ro_supported(void)
165 #if defined (__x86_64__) || defined (__i386__)
172 static void wp_ro_skip_msg(void)
174 #if defined (__x86_64__) || defined (__i386__)
175 pr_debug("Hardware does not support read only watchpoints.\n");
181 int (*target_func
)(void);
182 bool (*is_supported
)(void);
183 void (*skip_msg
)(void);
184 } wp_testcase_table
[] = {
186 .desc
= "Read Only Watchpoint",
187 .target_func
= &wp_ro_test
,
188 .is_supported
= &wp_ro_supported
,
189 .skip_msg
= &wp_ro_skip_msg
,
192 .desc
= "Write Only Watchpoint",
193 .target_func
= &wp_wo_test
,
196 .desc
= "Read / Write Watchpoint",
197 .target_func
= &wp_rw_test
,
200 .desc
= "Modify Watchpoint",
201 .target_func
= &wp_modify_test
,
205 int test__wp_subtest_get_nr(void)
207 return (int)ARRAY_SIZE(wp_testcase_table
);
210 const char *test__wp_subtest_get_desc(int i
)
212 if (i
< 0 || i
>= (int)ARRAY_SIZE(wp_testcase_table
))
214 return wp_testcase_table
[i
].desc
;
217 int test__wp(struct test
*test __maybe_unused
, int i
)
219 if (i
< 0 || i
>= (int)ARRAY_SIZE(wp_testcase_table
))
222 if (wp_testcase_table
[i
].is_supported
&&
223 !wp_testcase_table
[i
].is_supported()) {
224 wp_testcase_table
[i
].skip_msg();
228 return !wp_testcase_table
[i
].target_func() ? TEST_OK
: TEST_FAIL
;
231 /* The s390 so far does not have support for
232 * instruction breakpoint using the perf_event_open() system call.
234 bool test__wp_is_supported(void)
236 #if defined(__s390x__)