^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Inspired by breakpoint overflow test done by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Vince Weaver <vincent.weaver@maine.edu> for perf_event_tests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * (git://github.com/deater/perf_event_tests)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Powerpc needs __SANE_USERSPACE_TYPES__ before <linux/types.h> to select
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * 'int-ll64.h' and avoid compile warnings when printing __u64 with %llu.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #define __SANE_USERSPACE_TYPES__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <sys/ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <sys/mman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/hw_breakpoint.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include "tests.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include "debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include "event.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include "perf-sys.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include "cloexec.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static int fd1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static int fd2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static int fd3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static int overflows;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static int overflows_2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) volatile long the_var;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * Use ASM to ensure watchpoint and breakpoint can be triggered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * at one instruction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #if defined (__x86_64__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) extern void __test_function(volatile long *ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) asm (
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) ".pushsection .text;"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) ".globl __test_function\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) ".type __test_function, @function;"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) "__test_function:\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) "incq (%rdi)\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) "ret\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) ".popsection\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static void __test_function(volatile long *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) *ptr = 0x1234;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static noinline int test_function(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) __test_function(&the_var);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) the_var++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return time(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static void sig_handler_2(int signum __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) siginfo_t *oh __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) void *uc __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) overflows_2++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (overflows_2 > 10) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static void sig_handler(int signum __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) siginfo_t *oh __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) void *uc __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) overflows++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (overflows > 10) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * This should be executed only once during
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * this test, if we are here for the 10th
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * time, consider this the recursive issue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * We can get out of here by disable events,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * so no new SIGIO is delivered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static int __event(bool is_x, void *addr, int sig)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) struct perf_event_attr pe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) memset(&pe, 0, sizeof(struct perf_event_attr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) pe.type = PERF_TYPE_BREAKPOINT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) pe.size = sizeof(struct perf_event_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) pe.config = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) pe.bp_type = is_x ? HW_BREAKPOINT_X : HW_BREAKPOINT_W;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) pe.bp_addr = (unsigned long) addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) pe.bp_len = sizeof(long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) pe.sample_period = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) pe.sample_type = PERF_SAMPLE_IP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) pe.wakeup_events = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) pe.disabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) pe.exclude_kernel = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) pe.exclude_hv = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) fd = sys_perf_event_open(&pe, 0, -1, -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) perf_event_open_cloexec_flag());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) pr_debug("failed opening event %llx\n", pe.config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return TEST_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) fcntl(fd, F_SETSIG, sig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) fcntl(fd, F_SETOWN, getpid());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) ioctl(fd, PERF_EVENT_IOC_RESET, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static int bp_event(void *addr, int sig)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return __event(true, addr, sig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static int wp_event(void *addr, int sig)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return __event(false, addr, sig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static long long bp_count(int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) long long count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) ret = read(fd, &count, sizeof(long long));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (ret != sizeof(long long)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) pr_debug("failed to read: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) return TEST_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) int test__bp_signal(struct test *test __maybe_unused, int subtest __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) struct sigaction sa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) long long count1, count2, count3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) /* setup SIGIO signal handler */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) memset(&sa, 0, sizeof(struct sigaction));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) sa.sa_sigaction = (void *) sig_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) sa.sa_flags = SA_SIGINFO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (sigaction(SIGIO, &sa, NULL) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) pr_debug("failed setting up signal handler\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return TEST_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) sa.sa_sigaction = (void *) sig_handler_2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (sigaction(SIGUSR1, &sa, NULL) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) pr_debug("failed setting up signal handler 2\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return TEST_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * We create following events:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) * fd1 - breakpoint event on __test_function with SIGIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * signal configured. We should get signal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) * notification each time the breakpoint is hit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * fd2 - breakpoint event on sig_handler with SIGUSR1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * configured. We should get SIGUSR1 each time when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * breakpoint is hit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * fd3 - watchpoint event on __test_function with SIGIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * configured.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * Following processing should happen:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * Exec: Action: Result:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * incq (%rdi) - fd1 event breakpoint hit -> count1 == 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * - SIGIO is delivered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * sig_handler - fd2 event breakpoint hit -> count2 == 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * - SIGUSR1 is delivered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * sig_handler_2 -> overflows_2 == 1 (nested signal)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * sys_rt_sigreturn - return from sig_handler_2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * overflows++ -> overflows = 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * sys_rt_sigreturn - return from sig_handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * incq (%rdi) - fd3 event watchpoint hit -> count3 == 1 (wp and bp in one insn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * - SIGIO is delivered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * sig_handler - fd2 event breakpoint hit -> count2 == 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * - SIGUSR1 is delivered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * sig_handler_2 -> overflows_2 == 2 (nested signal)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) * sys_rt_sigreturn - return from sig_handler_2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * overflows++ -> overflows = 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * sys_rt_sigreturn - return from sig_handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * the_var++ - fd3 event watchpoint hit -> count3 == 2 (standalone watchpoint)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * - SIGIO is delivered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * sig_handler - fd2 event breakpoint hit -> count2 == 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) * - SIGUSR1 is delivered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * sig_handler_2 -> overflows_2 == 3 (nested signal)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * sys_rt_sigreturn - return from sig_handler_2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) * overflows++ -> overflows == 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * sys_rt_sigreturn - return from sig_handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * The test case check following error conditions:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) * - we get stuck in signal handler because of debug
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) * exception being triggered receursively due to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) * the wrong RF EFLAG management
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) * - we never trigger the sig_handler breakpoint due
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) * to the rong RF EFLAG management
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) fd1 = bp_event(__test_function, SIGIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) fd2 = bp_event(sig_handler, SIGUSR1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) fd3 = wp_event((void *)&the_var, SIGIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) ioctl(fd1, PERF_EVENT_IOC_ENABLE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) ioctl(fd2, PERF_EVENT_IOC_ENABLE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) ioctl(fd3, PERF_EVENT_IOC_ENABLE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * Kick off the test by trigering 'fd1'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * breakpoint.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) test_function();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) count1 = bp_count(fd1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) count2 = bp_count(fd2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) count3 = bp_count(fd3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) close(fd1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) close(fd2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) close(fd3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) pr_debug("count1 %lld, count2 %lld, count3 %lld, overflow %d, overflows_2 %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) count1, count2, count3, overflows, overflows_2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) if (count1 != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if (count1 == 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) pr_debug("failed: RF EFLAG recursion issue detected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) pr_debug("failed: wrong count for bp1: %lld, expected 1\n", count1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (overflows != 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) pr_debug("failed: wrong overflow (%d) hit, expected 3\n", overflows);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (overflows_2 != 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) pr_debug("failed: wrong overflow_2 (%d) hit, expected 3\n", overflows_2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (count2 != 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) pr_debug("failed: wrong count for bp2 (%lld), expected 3\n", count2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) if (count3 != 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) pr_debug("failed: wrong count for bp3 (%lld), expected 2\n", count3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) return count1 == 1 && overflows == 3 && count2 == 3 && overflows_2 == 3 && count3 == 2 ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) TEST_OK : TEST_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) bool test__bp_signal_is_supported(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * PowerPC and S390 do not support creation of instruction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) * breakpoints using the perf_event interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) * ARM requires explicit rounding down of the instruction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) * pointer in Thumb mode, and then requires the single-step
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) * to be handled explicitly in the overflow handler to avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) * stepping into the SIGIO handler and getting stuck on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) * breakpointed instruction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * Since arm64 has the same issue with arm for the single-step
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) * handling, this case also gets stuck on the breakpointed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * instruction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) * Just disable the test for these architectures until these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) * issues are resolved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) #if defined(__powerpc__) || defined(__s390x__) || defined(__arm__) || \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) defined(__aarch64__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }