xref: /OK3568_Linux_fs/kernel/tools/perf/tests/bp_account.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Powerpc needs __SANE_USERSPACE_TYPES__ before <linux/types.h> to select
4*4882a593Smuzhiyun  * 'int-ll64.h' and avoid compile warnings when printing __u64 with %llu.
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun #define __SANE_USERSPACE_TYPES__
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <stdlib.h>
9*4882a593Smuzhiyun #include <stdio.h>
10*4882a593Smuzhiyun #include <unistd.h>
11*4882a593Smuzhiyun #include <string.h>
12*4882a593Smuzhiyun #include <sys/ioctl.h>
13*4882a593Smuzhiyun #include <fcntl.h>
14*4882a593Smuzhiyun #include <linux/hw_breakpoint.h>
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #include "tests.h"
17*4882a593Smuzhiyun #include "debug.h"
18*4882a593Smuzhiyun #include "event.h"
19*4882a593Smuzhiyun #include "../perf-sys.h"
20*4882a593Smuzhiyun #include "cloexec.h"
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun static volatile long the_var;
23*4882a593Smuzhiyun 
test_function(void)24*4882a593Smuzhiyun static noinline int test_function(void)
25*4882a593Smuzhiyun {
26*4882a593Smuzhiyun 	return 0;
27*4882a593Smuzhiyun }
28*4882a593Smuzhiyun 
__event(bool is_x,void * addr,struct perf_event_attr * attr)29*4882a593Smuzhiyun static int __event(bool is_x, void *addr, struct perf_event_attr *attr)
30*4882a593Smuzhiyun {
31*4882a593Smuzhiyun 	int fd;
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun 	memset(attr, 0, sizeof(struct perf_event_attr));
34*4882a593Smuzhiyun 	attr->type = PERF_TYPE_BREAKPOINT;
35*4882a593Smuzhiyun 	attr->size = sizeof(struct perf_event_attr);
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun 	attr->config = 0;
38*4882a593Smuzhiyun 	attr->bp_type = is_x ? HW_BREAKPOINT_X : HW_BREAKPOINT_W;
39*4882a593Smuzhiyun 	attr->bp_addr = (unsigned long) addr;
40*4882a593Smuzhiyun 	attr->bp_len = sizeof(long);
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	attr->sample_period = 1;
43*4882a593Smuzhiyun 	attr->sample_type = PERF_SAMPLE_IP;
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 	attr->exclude_kernel = 1;
46*4882a593Smuzhiyun 	attr->exclude_hv = 1;
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	fd = sys_perf_event_open(attr, -1, 0, -1,
49*4882a593Smuzhiyun 				 perf_event_open_cloexec_flag());
50*4882a593Smuzhiyun 	if (fd < 0) {
51*4882a593Smuzhiyun 		pr_debug("failed opening event %llx\n", attr->config);
52*4882a593Smuzhiyun 		return TEST_FAIL;
53*4882a593Smuzhiyun 	}
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 	return fd;
56*4882a593Smuzhiyun }
57*4882a593Smuzhiyun 
wp_event(void * addr,struct perf_event_attr * attr)58*4882a593Smuzhiyun static int wp_event(void *addr, struct perf_event_attr *attr)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun 	return __event(false, addr, attr);
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun 
bp_event(void * addr,struct perf_event_attr * attr)63*4882a593Smuzhiyun static int bp_event(void *addr, struct perf_event_attr *attr)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun 	return __event(true, addr, attr);
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun 
bp_accounting(int wp_cnt,int share)68*4882a593Smuzhiyun static int bp_accounting(int wp_cnt, int share)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun 	struct perf_event_attr attr, attr_mod, attr_new;
71*4882a593Smuzhiyun 	int i, fd[wp_cnt], fd_wp, ret;
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	for (i = 0; i < wp_cnt; i++) {
74*4882a593Smuzhiyun 		fd[i] = wp_event((void *)&the_var, &attr);
75*4882a593Smuzhiyun 		TEST_ASSERT_VAL("failed to create wp\n", fd[i] != -1);
76*4882a593Smuzhiyun 		pr_debug("wp %d created\n", i);
77*4882a593Smuzhiyun 	}
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	attr_mod = attr;
80*4882a593Smuzhiyun 	attr_mod.bp_type = HW_BREAKPOINT_X;
81*4882a593Smuzhiyun 	attr_mod.bp_addr = (unsigned long) test_function;
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	ret = ioctl(fd[0], PERF_EVENT_IOC_MODIFY_ATTRIBUTES, &attr_mod);
84*4882a593Smuzhiyun 	TEST_ASSERT_VAL("failed to modify wp\n", ret == 0);
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	pr_debug("wp 0 modified to bp\n");
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	if (!share) {
89*4882a593Smuzhiyun 		fd_wp = wp_event((void *)&the_var, &attr_new);
90*4882a593Smuzhiyun 		TEST_ASSERT_VAL("failed to create max wp\n", fd_wp != -1);
91*4882a593Smuzhiyun 		pr_debug("wp max created\n");
92*4882a593Smuzhiyun 	}
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	for (i = 0; i < wp_cnt; i++)
95*4882a593Smuzhiyun 		close(fd[i]);
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	return 0;
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun 
detect_cnt(bool is_x)100*4882a593Smuzhiyun static int detect_cnt(bool is_x)
101*4882a593Smuzhiyun {
102*4882a593Smuzhiyun 	struct perf_event_attr attr;
103*4882a593Smuzhiyun 	void *addr = is_x ? (void *)test_function : (void *)&the_var;
104*4882a593Smuzhiyun 	int fd[100], cnt = 0, i;
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	while (1) {
107*4882a593Smuzhiyun 		if (cnt == 100) {
108*4882a593Smuzhiyun 			pr_debug("way too many debug registers, fix the test\n");
109*4882a593Smuzhiyun 			return 0;
110*4882a593Smuzhiyun 		}
111*4882a593Smuzhiyun 		fd[cnt] = __event(is_x, addr, &attr);
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 		if (fd[cnt] < 0)
114*4882a593Smuzhiyun 			break;
115*4882a593Smuzhiyun 		cnt++;
116*4882a593Smuzhiyun 	}
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	for (i = 0; i < cnt; i++)
119*4882a593Smuzhiyun 		close(fd[i]);
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	return cnt;
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun 
detect_ioctl(void)124*4882a593Smuzhiyun static int detect_ioctl(void)
125*4882a593Smuzhiyun {
126*4882a593Smuzhiyun 	struct perf_event_attr attr;
127*4882a593Smuzhiyun 	int fd, ret = 1;
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	fd = wp_event((void *) &the_var, &attr);
130*4882a593Smuzhiyun 	if (fd > 0) {
131*4882a593Smuzhiyun 		ret = ioctl(fd, PERF_EVENT_IOC_MODIFY_ATTRIBUTES, &attr);
132*4882a593Smuzhiyun 		close(fd);
133*4882a593Smuzhiyun 	}
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	return ret ? 0 : 1;
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun 
detect_share(int wp_cnt,int bp_cnt)138*4882a593Smuzhiyun static int detect_share(int wp_cnt, int bp_cnt)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun 	struct perf_event_attr attr;
141*4882a593Smuzhiyun 	int i, fd[wp_cnt + bp_cnt], ret;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	for (i = 0; i < wp_cnt; i++) {
144*4882a593Smuzhiyun 		fd[i] = wp_event((void *)&the_var, &attr);
145*4882a593Smuzhiyun 		TEST_ASSERT_VAL("failed to create wp\n", fd[i] != -1);
146*4882a593Smuzhiyun 	}
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	for (; i < (bp_cnt + wp_cnt); i++) {
149*4882a593Smuzhiyun 		fd[i] = bp_event((void *)test_function, &attr);
150*4882a593Smuzhiyun 		if (fd[i] == -1)
151*4882a593Smuzhiyun 			break;
152*4882a593Smuzhiyun 	}
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	ret = i != (bp_cnt + wp_cnt);
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	while (i--)
157*4882a593Smuzhiyun 		close(fd[i]);
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	return ret;
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun /*
163*4882a593Smuzhiyun  * This test does following:
164*4882a593Smuzhiyun  *   - detects the number of watch/break-points,
165*4882a593Smuzhiyun  *     skip test if any is missing
166*4882a593Smuzhiyun  *   - detects PERF_EVENT_IOC_MODIFY_ATTRIBUTES ioctl,
167*4882a593Smuzhiyun  *     skip test if it's missing
168*4882a593Smuzhiyun  *   - detects if watchpoints and breakpoints share
169*4882a593Smuzhiyun  *     same slots
170*4882a593Smuzhiyun  *   - create all possible watchpoints on cpu 0
171*4882a593Smuzhiyun  *   - change one of it to breakpoint
172*4882a593Smuzhiyun  *   - in case wp and bp do not share slots,
173*4882a593Smuzhiyun  *     we create another watchpoint to ensure
174*4882a593Smuzhiyun  *     the slot accounting is correct
175*4882a593Smuzhiyun  */
test__bp_accounting(struct test * test __maybe_unused,int subtest __maybe_unused)176*4882a593Smuzhiyun int test__bp_accounting(struct test *test __maybe_unused, int subtest __maybe_unused)
177*4882a593Smuzhiyun {
178*4882a593Smuzhiyun 	int has_ioctl = detect_ioctl();
179*4882a593Smuzhiyun 	int wp_cnt = detect_cnt(false);
180*4882a593Smuzhiyun 	int bp_cnt = detect_cnt(true);
181*4882a593Smuzhiyun 	int share  = detect_share(wp_cnt, bp_cnt);
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 	pr_debug("watchpoints count %d, breakpoints count %d, has_ioctl %d, share %d\n",
184*4882a593Smuzhiyun 		 wp_cnt, bp_cnt, has_ioctl, share);
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	if (!wp_cnt || !bp_cnt || !has_ioctl)
187*4882a593Smuzhiyun 		return TEST_SKIP;
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	return bp_accounting(wp_cnt, share);
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun 
test__bp_account_is_supported(void)192*4882a593Smuzhiyun bool test__bp_account_is_supported(void)
193*4882a593Smuzhiyun {
194*4882a593Smuzhiyun 	/*
195*4882a593Smuzhiyun 	 * PowerPC and S390 do not support creation of instruction
196*4882a593Smuzhiyun 	 * breakpoints using the perf_event interface.
197*4882a593Smuzhiyun 	 *
198*4882a593Smuzhiyun 	 * Just disable the test for these architectures until these
199*4882a593Smuzhiyun 	 * issues are resolved.
200*4882a593Smuzhiyun 	 */
201*4882a593Smuzhiyun #if defined(__powerpc__) || defined(__s390x__)
202*4882a593Smuzhiyun 	return false;
203*4882a593Smuzhiyun #else
204*4882a593Smuzhiyun 	return true;
205*4882a593Smuzhiyun #endif
206*4882a593Smuzhiyun }
207