0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #define __SANE_USERSPACE_TYPES__
0013
0014 #include <stdlib.h>
0015 #include <stdio.h>
0016 #include <unistd.h>
0017 #include <string.h>
0018 #include <sys/ioctl.h>
0019 #include <time.h>
0020 #include <fcntl.h>
0021 #include <signal.h>
0022 #include <sys/mman.h>
0023 #include <linux/compiler.h>
0024 #include <linux/hw_breakpoint.h>
0025
0026 #include "tests.h"
0027 #include "debug.h"
0028 #include "event.h"
0029 #include "perf-sys.h"
0030 #include "cloexec.h"
0031
0032 static int fd1;
0033 static int fd2;
0034 static int fd3;
0035 static int overflows;
0036 static int overflows_2;
0037
0038 volatile long the_var;
0039
0040
0041
0042
0043
0044
0045 #if defined (__x86_64__)
0046 extern void __test_function(volatile long *ptr);
0047 asm (
0048 ".pushsection .text;"
0049 ".globl __test_function\n"
0050 ".type __test_function, @function;"
0051 "__test_function:\n"
0052 "incq (%rdi)\n"
0053 "ret\n"
0054 ".popsection\n");
0055 #else
0056 static void __test_function(volatile long *ptr)
0057 {
0058 *ptr = 0x1234;
0059 }
0060 #endif
0061
0062 static noinline int test_function(void)
0063 {
0064 __test_function(&the_var);
0065 the_var++;
0066 return time(NULL);
0067 }
0068
0069 static void sig_handler_2(int signum __maybe_unused,
0070 siginfo_t *oh __maybe_unused,
0071 void *uc __maybe_unused)
0072 {
0073 overflows_2++;
0074 if (overflows_2 > 10) {
0075 ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
0076 ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
0077 ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
0078 }
0079 }
0080
0081 static void sig_handler(int signum __maybe_unused,
0082 siginfo_t *oh __maybe_unused,
0083 void *uc __maybe_unused)
0084 {
0085 overflows++;
0086
0087 if (overflows > 10) {
0088
0089
0090
0091
0092
0093
0094
0095
0096 ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
0097 ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
0098 ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
0099 }
0100 }
0101
0102 static int __event(bool is_x, void *addr, int sig)
0103 {
0104 struct perf_event_attr pe;
0105 int fd;
0106
0107 memset(&pe, 0, sizeof(struct perf_event_attr));
0108 pe.type = PERF_TYPE_BREAKPOINT;
0109 pe.size = sizeof(struct perf_event_attr);
0110
0111 pe.config = 0;
0112 pe.bp_type = is_x ? HW_BREAKPOINT_X : HW_BREAKPOINT_W;
0113 pe.bp_addr = (unsigned long) addr;
0114 pe.bp_len = sizeof(long);
0115
0116 pe.sample_period = 1;
0117 pe.sample_type = PERF_SAMPLE_IP;
0118 pe.wakeup_events = 1;
0119
0120 pe.disabled = 1;
0121 pe.exclude_kernel = 1;
0122 pe.exclude_hv = 1;
0123
0124 fd = sys_perf_event_open(&pe, 0, -1, -1,
0125 perf_event_open_cloexec_flag());
0126 if (fd < 0) {
0127 pr_debug("failed opening event %llx\n", pe.config);
0128 return TEST_FAIL;
0129 }
0130
0131 fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
0132 fcntl(fd, F_SETSIG, sig);
0133 fcntl(fd, F_SETOWN, getpid());
0134
0135 ioctl(fd, PERF_EVENT_IOC_RESET, 0);
0136
0137 return fd;
0138 }
0139
0140 static int bp_event(void *addr, int sig)
0141 {
0142 return __event(true, addr, sig);
0143 }
0144
0145 static int wp_event(void *addr, int sig)
0146 {
0147 return __event(false, addr, sig);
0148 }
0149
0150 static long long bp_count(int fd)
0151 {
0152 long long count;
0153 int ret;
0154
0155 ret = read(fd, &count, sizeof(long long));
0156 if (ret != sizeof(long long)) {
0157 pr_debug("failed to read: %d\n", ret);
0158 return TEST_FAIL;
0159 }
0160
0161 return count;
0162 }
0163
0164 static int test__bp_signal(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
0165 {
0166 struct sigaction sa;
0167 long long count1, count2, count3;
0168
0169 if (!BP_SIGNAL_IS_SUPPORTED) {
0170 pr_debug("Test not supported on this architecture");
0171 return TEST_SKIP;
0172 }
0173
0174
0175 memset(&sa, 0, sizeof(struct sigaction));
0176 sa.sa_sigaction = (void *) sig_handler;
0177 sa.sa_flags = SA_SIGINFO;
0178
0179 if (sigaction(SIGIO, &sa, NULL) < 0) {
0180 pr_debug("failed setting up signal handler\n");
0181 return TEST_FAIL;
0182 }
0183
0184 sa.sa_sigaction = (void *) sig_handler_2;
0185 if (sigaction(SIGUSR1, &sa, NULL) < 0) {
0186 pr_debug("failed setting up signal handler 2\n");
0187 return TEST_FAIL;
0188 }
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241 fd1 = bp_event(__test_function, SIGIO);
0242 fd2 = bp_event(sig_handler, SIGUSR1);
0243 fd3 = wp_event((void *)&the_var, SIGIO);
0244
0245 ioctl(fd1, PERF_EVENT_IOC_ENABLE, 0);
0246 ioctl(fd2, PERF_EVENT_IOC_ENABLE, 0);
0247 ioctl(fd3, PERF_EVENT_IOC_ENABLE, 0);
0248
0249
0250
0251
0252
0253 test_function();
0254
0255 ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
0256 ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
0257 ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
0258
0259 count1 = bp_count(fd1);
0260 count2 = bp_count(fd2);
0261 count3 = bp_count(fd3);
0262
0263 close(fd1);
0264 close(fd2);
0265 close(fd3);
0266
0267 pr_debug("count1 %lld, count2 %lld, count3 %lld, overflow %d, overflows_2 %d\n",
0268 count1, count2, count3, overflows, overflows_2);
0269
0270 if (count1 != 1) {
0271 if (count1 == 11)
0272 pr_debug("failed: RF EFLAG recursion issue detected\n");
0273 else
0274 pr_debug("failed: wrong count for bp1: %lld, expected 1\n", count1);
0275 }
0276
0277 if (overflows != 3)
0278 pr_debug("failed: wrong overflow (%d) hit, expected 3\n", overflows);
0279
0280 if (overflows_2 != 3)
0281 pr_debug("failed: wrong overflow_2 (%d) hit, expected 3\n", overflows_2);
0282
0283 if (count2 != 3)
0284 pr_debug("failed: wrong count for bp2 (%lld), expected 3\n", count2);
0285
0286 if (count3 != 2)
0287 pr_debug("failed: wrong count for bp3 (%lld), expected 2\n", count3);
0288
0289 return count1 == 1 && overflows == 3 && count2 == 3 && overflows_2 == 3 && count3 == 2 ?
0290 TEST_OK : TEST_FAIL;
0291 }
0292
0293 DEFINE_SUITE("Breakpoint overflow signal handler", bp_signal);