clk: samsung: Add bus clock for GPU/G3D on Exynos4412
[linux/fpc-iii.git] / tools / perf / tests / wp.c
blobf89e6806557b2aa39ca56a6055cd88d948922722
1 // SPDX-License-Identifier: GPL-2.0
2 #include <stdlib.h>
3 #include <sys/ioctl.h>
4 #include <linux/hw_breakpoint.h>
5 #include "tests.h"
6 #include "debug.h"
7 #include "cloexec.h"
9 #define WP_TEST_ASSERT_VAL(fd, text, val) \
10 do { \
11 long long count; \
12 wp_read(fd, &count, sizeof(long long)); \
13 TEST_ASSERT_VAL(text, count == val); \
14 } while (0)
16 volatile u64 data1;
17 volatile u8 data2[3];
19 static int wp_read(int fd, long long *count, int size)
21 int ret = read(fd, count, size);
23 if (ret != size) {
24 pr_debug("failed to read: %d\n", ret);
25 return -1;
27 return 0;
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);
36 attr->config = 0;
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;
43 attr->exclude_hv = 1;
46 static int __event(int wp_type, void *wp_addr, unsigned long wp_len)
48 int fd;
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());
54 if (fd < 0)
55 pr_debug("failed opening event %x\n", attr.bp_type);
57 return fd;
60 static int wp_ro_test(void)
62 int fd;
63 unsigned long tmp, tmp1 = rand();
65 fd = __event(HW_BREAKPOINT_R, (void *)&data1, sizeof(data1));
66 if (fd < 0)
67 return -1;
69 tmp = data1;
70 WP_TEST_ASSERT_VAL(fd, "RO watchpoint", 1);
72 data1 = tmp1 + tmp;
73 WP_TEST_ASSERT_VAL(fd, "RO watchpoint", 1);
75 close(fd);
76 return 0;
79 static int wp_wo_test(void)
81 int fd;
82 unsigned long tmp, tmp1 = rand();
84 fd = __event(HW_BREAKPOINT_W, (void *)&data1, sizeof(data1));
85 if (fd < 0)
86 return -1;
88 tmp = data1;
89 WP_TEST_ASSERT_VAL(fd, "WO watchpoint", 0);
91 data1 = tmp1 + tmp;
92 WP_TEST_ASSERT_VAL(fd, "WO watchpoint", 1);
94 close(fd);
95 return 0;
98 static int wp_rw_test(void)
100 int fd;
101 unsigned long tmp, tmp1 = rand();
103 fd = __event(HW_BREAKPOINT_R | HW_BREAKPOINT_W, (void *)&data1,
104 sizeof(data1));
105 if (fd < 0)
106 return -1;
108 tmp = data1;
109 WP_TEST_ASSERT_VAL(fd, "RW watchpoint", 1);
111 data1 = tmp1 + tmp;
112 WP_TEST_ASSERT_VAL(fd, "RW watchpoint", 2);
114 close(fd);
115 return 0;
118 static int wp_modify_test(void)
120 int fd, ret;
121 unsigned long tmp = rand();
122 struct perf_event_attr new_attr;
124 fd = __event(HW_BREAKPOINT_W, (void *)&data1, sizeof(data1));
125 if (fd < 0)
126 return -1;
128 data1 = tmp;
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],
133 sizeof(u8) * 2);
134 new_attr.disabled = 1;
135 ret = ioctl(fd, PERF_EVENT_IOC_MODIFY_ATTRIBUTES, &new_attr);
136 if (ret < 0) {
137 pr_debug("ioctl(PERF_EVENT_IOC_MODIFY_ATTRIBUTES) failed\n");
138 close(fd);
139 return ret;
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);
147 if (ret < 0) {
148 pr_debug("Failed to enable event\n");
149 close(fd);
150 return ret;
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);
159 close(fd);
160 return 0;
163 static bool wp_ro_supported(void)
165 #if defined (__x86_64__) || defined (__i386__)
166 return false;
167 #else
168 return true;
169 #endif
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");
176 #endif
179 static struct {
180 const char *desc;
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))
213 return NULL;
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))
220 return TEST_FAIL;
222 if (wp_testcase_table[i].is_supported &&
223 !wp_testcase_table[i].is_supported()) {
224 wp_testcase_table[i].skip_msg();
225 return TEST_SKIP;
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__)
237 return false;
238 #else
239 return true;
240 #endif