Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Powerpc needs __SANE_USERSPACE_TYPES__ before <linux/types.h> to select
0004  * 'int-ll64.h' and avoid compile warnings when printing __u64 with %llu.
0005  */
0006 #define __SANE_USERSPACE_TYPES__
0007 
0008 #include <stdlib.h>
0009 #include <stdio.h>
0010 #include <unistd.h>
0011 #include <string.h>
0012 #include <sys/ioctl.h>
0013 #include <fcntl.h>
0014 #include <linux/hw_breakpoint.h>
0015 
0016 #include "tests.h"
0017 #include "debug.h"
0018 #include "event.h"
0019 #include "../perf-sys.h"
0020 #include "cloexec.h"
0021 
0022 /*
0023  * PowerPC and S390 do not support creation of instruction breakpoints using the
0024  * perf_event interface.
0025  *
0026  * Just disable the test for these architectures until these issues are
0027  * resolved.
0028  */
0029 #if defined(__powerpc__) || defined(__s390x__)
0030 #define BP_ACCOUNT_IS_SUPPORTED 0
0031 #else
0032 #define BP_ACCOUNT_IS_SUPPORTED 1
0033 #endif
0034 
0035 static volatile long the_var;
0036 
0037 static noinline int test_function(void)
0038 {
0039     return 0;
0040 }
0041 
0042 static int __event(bool is_x, void *addr, struct perf_event_attr *attr)
0043 {
0044     int fd;
0045 
0046     memset(attr, 0, sizeof(struct perf_event_attr));
0047     attr->type = PERF_TYPE_BREAKPOINT;
0048     attr->size = sizeof(struct perf_event_attr);
0049 
0050     attr->config = 0;
0051     attr->bp_type = is_x ? HW_BREAKPOINT_X : HW_BREAKPOINT_W;
0052     attr->bp_addr = (unsigned long) addr;
0053     attr->bp_len = sizeof(long);
0054 
0055     attr->sample_period = 1;
0056     attr->sample_type = PERF_SAMPLE_IP;
0057 
0058     attr->exclude_kernel = 1;
0059     attr->exclude_hv = 1;
0060 
0061     fd = sys_perf_event_open(attr, -1, 0, -1,
0062                  perf_event_open_cloexec_flag());
0063     if (fd < 0) {
0064         pr_debug("failed opening event %llx\n", attr->config);
0065         return TEST_FAIL;
0066     }
0067 
0068     return fd;
0069 }
0070 
0071 static int wp_event(void *addr, struct perf_event_attr *attr)
0072 {
0073     return __event(false, addr, attr);
0074 }
0075 
0076 static int bp_event(void *addr, struct perf_event_attr *attr)
0077 {
0078     return __event(true, addr, attr);
0079 }
0080 
0081 static int bp_accounting(int wp_cnt, int share)
0082 {
0083     struct perf_event_attr attr, attr_mod, attr_new;
0084     int i, fd[wp_cnt], fd_wp, ret;
0085 
0086     for (i = 0; i < wp_cnt; i++) {
0087         fd[i] = wp_event((void *)&the_var, &attr);
0088         TEST_ASSERT_VAL("failed to create wp\n", fd[i] != -1);
0089         pr_debug("wp %d created\n", i);
0090     }
0091 
0092     attr_mod = attr;
0093     attr_mod.bp_type = HW_BREAKPOINT_X;
0094     attr_mod.bp_addr = (unsigned long) test_function;
0095 
0096     ret = ioctl(fd[0], PERF_EVENT_IOC_MODIFY_ATTRIBUTES, &attr_mod);
0097     TEST_ASSERT_VAL("failed to modify wp\n", ret == 0);
0098 
0099     pr_debug("wp 0 modified to bp\n");
0100 
0101     if (!share) {
0102         fd_wp = wp_event((void *)&the_var, &attr_new);
0103         TEST_ASSERT_VAL("failed to create max wp\n", fd_wp != -1);
0104         pr_debug("wp max created\n");
0105     }
0106 
0107     for (i = 0; i < wp_cnt; i++)
0108         close(fd[i]);
0109 
0110     return 0;
0111 }
0112 
0113 static int detect_cnt(bool is_x)
0114 {
0115     struct perf_event_attr attr;
0116     void *addr = is_x ? (void *)test_function : (void *)&the_var;
0117     int fd[100], cnt = 0, i;
0118 
0119     while (1) {
0120         if (cnt == 100) {
0121             pr_debug("way too many debug registers, fix the test\n");
0122             return 0;
0123         }
0124         fd[cnt] = __event(is_x, addr, &attr);
0125 
0126         if (fd[cnt] < 0)
0127             break;
0128         cnt++;
0129     }
0130 
0131     for (i = 0; i < cnt; i++)
0132         close(fd[i]);
0133 
0134     return cnt;
0135 }
0136 
0137 static int detect_ioctl(void)
0138 {
0139     struct perf_event_attr attr;
0140     int fd, ret = 1;
0141 
0142     fd = wp_event((void *) &the_var, &attr);
0143     if (fd > 0) {
0144         ret = ioctl(fd, PERF_EVENT_IOC_MODIFY_ATTRIBUTES, &attr);
0145         close(fd);
0146     }
0147 
0148     return ret ? 0 : 1;
0149 }
0150 
0151 static int detect_share(int wp_cnt, int bp_cnt)
0152 {
0153     struct perf_event_attr attr;
0154     int i, *fd = NULL, ret = -1;
0155 
0156     if (wp_cnt + bp_cnt == 0)
0157         return 0;
0158 
0159     fd = malloc(sizeof(int) * (wp_cnt + bp_cnt));
0160     if (!fd)
0161         return -1;
0162 
0163     for (i = 0; i < wp_cnt; i++) {
0164         fd[i] = wp_event((void *)&the_var, &attr);
0165         if (fd[i] == -1) {
0166             pr_err("failed to create wp\n");
0167             goto out;
0168         }
0169     }
0170 
0171     for (; i < (bp_cnt + wp_cnt); i++) {
0172         fd[i] = bp_event((void *)test_function, &attr);
0173         if (fd[i] == -1)
0174             break;
0175     }
0176 
0177     ret = i != (bp_cnt + wp_cnt);
0178 
0179 out:
0180     while (i--)
0181         close(fd[i]);
0182 
0183     free(fd);
0184     return ret;
0185 }
0186 
0187 /*
0188  * This test does following:
0189  *   - detects the number of watch/break-points,
0190  *     skip test if any is missing
0191  *   - detects PERF_EVENT_IOC_MODIFY_ATTRIBUTES ioctl,
0192  *     skip test if it's missing
0193  *   - detects if watchpoints and breakpoints share
0194  *     same slots
0195  *   - create all possible watchpoints on cpu 0
0196  *   - change one of it to breakpoint
0197  *   - in case wp and bp do not share slots,
0198  *     we create another watchpoint to ensure
0199  *     the slot accounting is correct
0200  */
0201 static int test__bp_accounting(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
0202 {
0203     int has_ioctl = detect_ioctl();
0204     int wp_cnt = detect_cnt(false);
0205     int bp_cnt = detect_cnt(true);
0206     int share  = detect_share(wp_cnt, bp_cnt);
0207 
0208     if (!BP_ACCOUNT_IS_SUPPORTED) {
0209         pr_debug("Test not supported on this architecture");
0210         return TEST_SKIP;
0211     }
0212 
0213     pr_debug("watchpoints count %d, breakpoints count %d, has_ioctl %d, share %d\n",
0214          wp_cnt, bp_cnt, has_ioctl, share);
0215 
0216     if (!wp_cnt || !bp_cnt || !has_ioctl)
0217         return TEST_SKIP;
0218 
0219     return bp_accounting(wp_cnt, share);
0220 }
0221 
0222 DEFINE_SUITE("Breakpoint accounting", bp_accounting);