Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <stdlib.h>
0003 #include <string.h>
0004 #include <unistd.h>
0005 #include <errno.h>
0006 #include <sys/ioctl.h>
0007 #include <linux/compiler.h>
0008 #include <linux/hw_breakpoint.h>
0009 #include <linux/kernel.h>
0010 #include "tests.h"
0011 #include "debug.h"
0012 #include "event.h"
0013 #include "cloexec.h"
0014 #include "../perf-sys.h"
0015 
0016 #define WP_TEST_ASSERT_VAL(fd, text, val)       \
0017 do {                                            \
0018     long long count;                        \
0019     wp_read(fd, &count, sizeof(long long)); \
0020     TEST_ASSERT_VAL(text, count == val);    \
0021 } while (0)
0022 
0023 volatile u64 data1;
0024 volatile u8 data2[3];
0025 
0026 #ifndef __s390x__
0027 static int wp_read(int fd, long long *count, int size)
0028 {
0029     int ret = read(fd, count, size);
0030 
0031     if (ret != size) {
0032         pr_debug("failed to read: %d\n", ret);
0033         return -1;
0034     }
0035     return 0;
0036 }
0037 
0038 static void get__perf_event_attr(struct perf_event_attr *attr, int wp_type,
0039                  void *wp_addr, unsigned long wp_len)
0040 {
0041     memset(attr, 0, sizeof(struct perf_event_attr));
0042     attr->type           = PERF_TYPE_BREAKPOINT;
0043     attr->size           = sizeof(struct perf_event_attr);
0044     attr->config         = 0;
0045     attr->bp_type        = wp_type;
0046     attr->bp_addr        = (unsigned long)wp_addr;
0047     attr->bp_len         = wp_len;
0048     attr->sample_period  = 1;
0049     attr->sample_type    = PERF_SAMPLE_IP;
0050     attr->exclude_kernel = 1;
0051     attr->exclude_hv     = 1;
0052 }
0053 
0054 static int __event(int wp_type, void *wp_addr, unsigned long wp_len)
0055 {
0056     int fd;
0057     struct perf_event_attr attr;
0058 
0059     get__perf_event_attr(&attr, wp_type, wp_addr, wp_len);
0060     fd = sys_perf_event_open(&attr, 0, -1, -1,
0061                  perf_event_open_cloexec_flag());
0062     if (fd < 0)
0063         pr_debug("failed opening event %x\n", attr.bp_type);
0064 
0065     return fd;
0066 }
0067 #endif
0068 
0069 static int test__wp_ro(struct test_suite *test __maybe_unused,
0070                int subtest __maybe_unused)
0071 {
0072 #if defined(__s390x__) || defined(__x86_64__) || defined(__i386__)
0073     return TEST_SKIP;
0074 #else
0075     int fd;
0076     unsigned long tmp, tmp1 = rand();
0077 
0078     fd = __event(HW_BREAKPOINT_R, (void *)&data1, sizeof(data1));
0079     if (fd < 0)
0080         return -1;
0081 
0082     tmp = data1;
0083     WP_TEST_ASSERT_VAL(fd, "RO watchpoint", 1);
0084 
0085     data1 = tmp1 + tmp;
0086     WP_TEST_ASSERT_VAL(fd, "RO watchpoint", 1);
0087 
0088     close(fd);
0089     return 0;
0090 #endif
0091 }
0092 
0093 static int test__wp_wo(struct test_suite *test __maybe_unused,
0094                int subtest __maybe_unused)
0095 {
0096 #if defined(__s390x__)
0097     return TEST_SKIP;
0098 #else
0099     int fd;
0100     unsigned long tmp, tmp1 = rand();
0101 
0102     fd = __event(HW_BREAKPOINT_W, (void *)&data1, sizeof(data1));
0103     if (fd < 0)
0104         return -1;
0105 
0106     tmp = data1;
0107     WP_TEST_ASSERT_VAL(fd, "WO watchpoint", 0);
0108 
0109     data1 = tmp1 + tmp;
0110     WP_TEST_ASSERT_VAL(fd, "WO watchpoint", 1);
0111 
0112     close(fd);
0113     return 0;
0114 #endif
0115 }
0116 
0117 static int test__wp_rw(struct test_suite *test __maybe_unused,
0118                int subtest __maybe_unused)
0119 {
0120 #if defined(__s390x__)
0121     return TEST_SKIP;
0122 #else
0123     int fd;
0124     unsigned long tmp, tmp1 = rand();
0125 
0126     fd = __event(HW_BREAKPOINT_R | HW_BREAKPOINT_W, (void *)&data1,
0127              sizeof(data1));
0128     if (fd < 0)
0129         return -1;
0130 
0131     tmp = data1;
0132     WP_TEST_ASSERT_VAL(fd, "RW watchpoint", 1);
0133 
0134     data1 = tmp1 + tmp;
0135     WP_TEST_ASSERT_VAL(fd, "RW watchpoint", 2);
0136 
0137     close(fd);
0138     return 0;
0139 #endif
0140 }
0141 
0142 static int test__wp_modify(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
0143 {
0144 #if defined(__s390x__)
0145     return TEST_SKIP;
0146 #else
0147     int fd, ret;
0148     unsigned long tmp = rand();
0149     struct perf_event_attr new_attr;
0150 
0151     fd = __event(HW_BREAKPOINT_W, (void *)&data1, sizeof(data1));
0152     if (fd < 0)
0153         return -1;
0154 
0155     data1 = tmp;
0156     WP_TEST_ASSERT_VAL(fd, "Modify watchpoint", 1);
0157 
0158     /* Modify watchpoint with disabled = 1 */
0159     get__perf_event_attr(&new_attr, HW_BREAKPOINT_W, (void *)&data2[0],
0160                  sizeof(u8) * 2);
0161     new_attr.disabled = 1;
0162     ret = ioctl(fd, PERF_EVENT_IOC_MODIFY_ATTRIBUTES, &new_attr);
0163     if (ret < 0) {
0164         if (errno == ENOTTY) {
0165             test->test_cases[subtest].skip_reason = "missing kernel support";
0166             ret = TEST_SKIP;
0167         }
0168 
0169         pr_debug("ioctl(PERF_EVENT_IOC_MODIFY_ATTRIBUTES) failed\n");
0170         close(fd);
0171         return ret;
0172     }
0173 
0174     data2[1] = tmp; /* Not Counted */
0175     WP_TEST_ASSERT_VAL(fd, "Modify watchpoint", 1);
0176 
0177     /* Enable the event */
0178     ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
0179     if (ret < 0) {
0180         pr_debug("Failed to enable event\n");
0181         close(fd);
0182         return ret;
0183     }
0184 
0185     data2[1] = tmp; /* Counted */
0186     WP_TEST_ASSERT_VAL(fd, "Modify watchpoint", 2);
0187 
0188     data2[2] = tmp; /* Not Counted */
0189     WP_TEST_ASSERT_VAL(fd, "Modify watchpoint", 2);
0190 
0191     close(fd);
0192     return 0;
0193 #endif
0194 }
0195 
0196 static struct test_case wp_tests[] = {
0197     TEST_CASE_REASON("Read Only Watchpoint", wp_ro, "missing hardware support"),
0198     TEST_CASE_REASON("Write Only Watchpoint", wp_wo, "missing hardware support"),
0199     TEST_CASE_REASON("Read / Write Watchpoint", wp_rw, "missing hardware support"),
0200     TEST_CASE_REASON("Modify Watchpoint", wp_modify, "missing hardware support"),
0201     { .name = NULL, }
0202 };
0203 
0204 struct test_suite suite__wp = {
0205     .desc = "Watchpoint",
0206     .test_cases = wp_tests,
0207 };