Linux Audio

Check our new training course

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