15a6bef47SJiri Olsa /* 25a6bef47SJiri Olsa * Inspired by breakpoint overflow test done by 35a6bef47SJiri Olsa * Vince Weaver <vincent.weaver@maine.edu> for perf_event_tests 45a6bef47SJiri Olsa * (git://github.com/deater/perf_event_tests) 55a6bef47SJiri Olsa */ 65a6bef47SJiri Olsa 7b3539d21SSukadev Bhattiprolu /* 8b3539d21SSukadev Bhattiprolu * Powerpc needs __SANE_USERSPACE_TYPES__ before <linux/types.h> to select 9b3539d21SSukadev Bhattiprolu * 'int-ll64.h' and avoid compile warnings when printing __u64 with %llu. 10b3539d21SSukadev Bhattiprolu */ 11b3539d21SSukadev Bhattiprolu #define __SANE_USERSPACE_TYPES__ 12b3539d21SSukadev Bhattiprolu 135a6bef47SJiri Olsa #include <stdlib.h> 145a6bef47SJiri Olsa #include <stdio.h> 155a6bef47SJiri Olsa #include <unistd.h> 165a6bef47SJiri Olsa #include <string.h> 175a6bef47SJiri Olsa #include <sys/ioctl.h> 185a6bef47SJiri Olsa #include <time.h> 195a6bef47SJiri Olsa #include <fcntl.h> 205a6bef47SJiri Olsa #include <signal.h> 215a6bef47SJiri Olsa #include <sys/mman.h> 225a6bef47SJiri Olsa #include <linux/compiler.h> 235a6bef47SJiri Olsa #include <linux/hw_breakpoint.h> 245a6bef47SJiri Olsa 255a6bef47SJiri Olsa #include "tests.h" 265a6bef47SJiri Olsa #include "debug.h" 275a6bef47SJiri Olsa #include "perf.h" 2857480d2cSYann Droneaud #include "cloexec.h" 295a6bef47SJiri Olsa 305a6bef47SJiri Olsa static int fd1; 315a6bef47SJiri Olsa static int fd2; 328fd34e1cSWang Nan static int fd3; 335a6bef47SJiri Olsa static int overflows; 348fd34e1cSWang Nan static int overflows_2; 358fd34e1cSWang Nan 368fd34e1cSWang Nan volatile long the_var; 378fd34e1cSWang Nan 388fd34e1cSWang Nan 398fd34e1cSWang Nan /* 408fd34e1cSWang Nan * Use ASM to ensure watchpoint and breakpoint can be triggered 418fd34e1cSWang Nan * at one instruction. 428fd34e1cSWang Nan */ 438fd34e1cSWang Nan #if defined (__x86_64__) 448fd34e1cSWang Nan extern void __test_function(volatile long *ptr); 458fd34e1cSWang Nan asm ( 468fd34e1cSWang Nan ".globl __test_function\n" 478fd34e1cSWang Nan "__test_function:\n" 488fd34e1cSWang Nan "incq (%rdi)\n" 498fd34e1cSWang Nan "ret\n"); 508fd34e1cSWang Nan #elif defined (__aarch64__) 518fd34e1cSWang Nan extern void __test_function(volatile long *ptr); 528fd34e1cSWang Nan asm ( 538fd34e1cSWang Nan ".globl __test_function\n" 548fd34e1cSWang Nan "__test_function:\n" 558fd34e1cSWang Nan "str x30, [x0]\n" 568fd34e1cSWang Nan "ret\n"); 578fd34e1cSWang Nan 588fd34e1cSWang Nan #else 598fd34e1cSWang Nan static void __test_function(volatile long *ptr) 608fd34e1cSWang Nan { 618fd34e1cSWang Nan *ptr = 0x1234; 628fd34e1cSWang Nan } 638fd34e1cSWang Nan #endif 645a6bef47SJiri Olsa 65*9dd4ca47SArnaldo Carvalho de Melo static noinline int test_function(void) 665a6bef47SJiri Olsa { 678fd34e1cSWang Nan __test_function(&the_var); 688fd34e1cSWang Nan the_var++; 695a6bef47SJiri Olsa return time(NULL); 705a6bef47SJiri Olsa } 715a6bef47SJiri Olsa 728fd34e1cSWang Nan static void sig_handler_2(int signum __maybe_unused, 738fd34e1cSWang Nan siginfo_t *oh __maybe_unused, 748fd34e1cSWang Nan void *uc __maybe_unused) 758fd34e1cSWang Nan { 768fd34e1cSWang Nan overflows_2++; 778fd34e1cSWang Nan if (overflows_2 > 10) { 788fd34e1cSWang Nan ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0); 798fd34e1cSWang Nan ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0); 808fd34e1cSWang Nan ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0); 818fd34e1cSWang Nan } 828fd34e1cSWang Nan } 838fd34e1cSWang Nan 845a6bef47SJiri Olsa static void sig_handler(int signum __maybe_unused, 855a6bef47SJiri Olsa siginfo_t *oh __maybe_unused, 865a6bef47SJiri Olsa void *uc __maybe_unused) 875a6bef47SJiri Olsa { 885a6bef47SJiri Olsa overflows++; 895a6bef47SJiri Olsa 905a6bef47SJiri Olsa if (overflows > 10) { 915a6bef47SJiri Olsa /* 925a6bef47SJiri Olsa * This should be executed only once during 935a6bef47SJiri Olsa * this test, if we are here for the 10th 945a6bef47SJiri Olsa * time, consider this the recursive issue. 955a6bef47SJiri Olsa * 965a6bef47SJiri Olsa * We can get out of here by disable events, 975a6bef47SJiri Olsa * so no new SIGIO is delivered. 985a6bef47SJiri Olsa */ 995a6bef47SJiri Olsa ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0); 1005a6bef47SJiri Olsa ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0); 1018fd34e1cSWang Nan ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0); 1025a6bef47SJiri Olsa } 1035a6bef47SJiri Olsa } 1045a6bef47SJiri Olsa 1051ad826baSArnaldo Carvalho de Melo static int __event(bool is_x, void *addr, int sig) 1065a6bef47SJiri Olsa { 1075a6bef47SJiri Olsa struct perf_event_attr pe; 1085a6bef47SJiri Olsa int fd; 1095a6bef47SJiri Olsa 1105a6bef47SJiri Olsa memset(&pe, 0, sizeof(struct perf_event_attr)); 1115a6bef47SJiri Olsa pe.type = PERF_TYPE_BREAKPOINT; 1125a6bef47SJiri Olsa pe.size = sizeof(struct perf_event_attr); 1135a6bef47SJiri Olsa 1145a6bef47SJiri Olsa pe.config = 0; 1158fd34e1cSWang Nan pe.bp_type = is_x ? HW_BREAKPOINT_X : HW_BREAKPOINT_W; 1168fd34e1cSWang Nan pe.bp_addr = (unsigned long) addr; 1175a6bef47SJiri Olsa pe.bp_len = sizeof(long); 1185a6bef47SJiri Olsa 1195a6bef47SJiri Olsa pe.sample_period = 1; 1205a6bef47SJiri Olsa pe.sample_type = PERF_SAMPLE_IP; 1215a6bef47SJiri Olsa pe.wakeup_events = 1; 1225a6bef47SJiri Olsa 1235a6bef47SJiri Olsa pe.disabled = 1; 1245a6bef47SJiri Olsa pe.exclude_kernel = 1; 1255a6bef47SJiri Olsa pe.exclude_hv = 1; 1265a6bef47SJiri Olsa 12757480d2cSYann Droneaud fd = sys_perf_event_open(&pe, 0, -1, -1, 12857480d2cSYann Droneaud perf_event_open_cloexec_flag()); 1295a6bef47SJiri Olsa if (fd < 0) { 1305a6bef47SJiri Olsa pr_debug("failed opening event %llx\n", pe.config); 1315a6bef47SJiri Olsa return TEST_FAIL; 1325a6bef47SJiri Olsa } 1335a6bef47SJiri Olsa 1345a6bef47SJiri Olsa fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC); 1351ad826baSArnaldo Carvalho de Melo fcntl(fd, F_SETSIG, sig); 1365a6bef47SJiri Olsa fcntl(fd, F_SETOWN, getpid()); 1375a6bef47SJiri Olsa 1385a6bef47SJiri Olsa ioctl(fd, PERF_EVENT_IOC_RESET, 0); 1395a6bef47SJiri Olsa 1405a6bef47SJiri Olsa return fd; 1415a6bef47SJiri Olsa } 1425a6bef47SJiri Olsa 1431ad826baSArnaldo Carvalho de Melo static int bp_event(void *addr, int sig) 1448fd34e1cSWang Nan { 1451ad826baSArnaldo Carvalho de Melo return __event(true, addr, sig); 1468fd34e1cSWang Nan } 1478fd34e1cSWang Nan 1481ad826baSArnaldo Carvalho de Melo static int wp_event(void *addr, int sig) 1498fd34e1cSWang Nan { 1501ad826baSArnaldo Carvalho de Melo return __event(false, addr, sig); 1518fd34e1cSWang Nan } 1528fd34e1cSWang Nan 1535a6bef47SJiri Olsa static long long bp_count(int fd) 1545a6bef47SJiri Olsa { 1555a6bef47SJiri Olsa long long count; 1565a6bef47SJiri Olsa int ret; 1575a6bef47SJiri Olsa 1585a6bef47SJiri Olsa ret = read(fd, &count, sizeof(long long)); 1595a6bef47SJiri Olsa if (ret != sizeof(long long)) { 1605a6bef47SJiri Olsa pr_debug("failed to read: %d\n", ret); 1615a6bef47SJiri Olsa return TEST_FAIL; 1625a6bef47SJiri Olsa } 1635a6bef47SJiri Olsa 1645a6bef47SJiri Olsa return count; 1655a6bef47SJiri Olsa } 1665a6bef47SJiri Olsa 167721a1f53SArnaldo Carvalho de Melo int test__bp_signal(int subtest __maybe_unused) 1685a6bef47SJiri Olsa { 1695a6bef47SJiri Olsa struct sigaction sa; 1708fd34e1cSWang Nan long long count1, count2, count3; 1715a6bef47SJiri Olsa 1725a6bef47SJiri Olsa /* setup SIGIO signal handler */ 1735a6bef47SJiri Olsa memset(&sa, 0, sizeof(struct sigaction)); 1745a6bef47SJiri Olsa sa.sa_sigaction = (void *) sig_handler; 1755a6bef47SJiri Olsa sa.sa_flags = SA_SIGINFO; 1765a6bef47SJiri Olsa 1775a6bef47SJiri Olsa if (sigaction(SIGIO, &sa, NULL) < 0) { 1785a6bef47SJiri Olsa pr_debug("failed setting up signal handler\n"); 1795a6bef47SJiri Olsa return TEST_FAIL; 1805a6bef47SJiri Olsa } 1815a6bef47SJiri Olsa 1828fd34e1cSWang Nan sa.sa_sigaction = (void *) sig_handler_2; 1838fd34e1cSWang Nan if (sigaction(SIGUSR1, &sa, NULL) < 0) { 1848fd34e1cSWang Nan pr_debug("failed setting up signal handler 2\n"); 1858fd34e1cSWang Nan return TEST_FAIL; 1868fd34e1cSWang Nan } 1878fd34e1cSWang Nan 1885a6bef47SJiri Olsa /* 1895a6bef47SJiri Olsa * We create following events: 1905a6bef47SJiri Olsa * 1918fd34e1cSWang Nan * fd1 - breakpoint event on __test_function with SIGIO 1925a6bef47SJiri Olsa * signal configured. We should get signal 1935a6bef47SJiri Olsa * notification each time the breakpoint is hit 1945a6bef47SJiri Olsa * 1958fd34e1cSWang Nan * fd2 - breakpoint event on sig_handler with SIGUSR1 1968fd34e1cSWang Nan * configured. We should get SIGUSR1 each time when 1978fd34e1cSWang Nan * breakpoint is hit 1988fd34e1cSWang Nan * 1998fd34e1cSWang Nan * fd3 - watchpoint event on __test_function with SIGIO 2005a6bef47SJiri Olsa * configured. 2015a6bef47SJiri Olsa * 2025a6bef47SJiri Olsa * Following processing should happen: 2038fd34e1cSWang Nan * Exec: Action: Result: 2048fd34e1cSWang Nan * incq (%rdi) - fd1 event breakpoint hit -> count1 == 1 2058fd34e1cSWang Nan * - SIGIO is delivered 2068fd34e1cSWang Nan * sig_handler - fd2 event breakpoint hit -> count2 == 1 2078fd34e1cSWang Nan * - SIGUSR1 is delivered 2088fd34e1cSWang Nan * sig_handler_2 -> overflows_2 == 1 (nested signal) 2098fd34e1cSWang Nan * sys_rt_sigreturn - return from sig_handler_2 2108fd34e1cSWang Nan * overflows++ -> overflows = 1 2118fd34e1cSWang Nan * sys_rt_sigreturn - return from sig_handler 2128fd34e1cSWang Nan * incq (%rdi) - fd3 event watchpoint hit -> count3 == 1 (wp and bp in one insn) 2138fd34e1cSWang Nan * - SIGIO is delivered 2148fd34e1cSWang Nan * sig_handler - fd2 event breakpoint hit -> count2 == 2 2158fd34e1cSWang Nan * - SIGUSR1 is delivered 2168fd34e1cSWang Nan * sig_handler_2 -> overflows_2 == 2 (nested signal) 2178fd34e1cSWang Nan * sys_rt_sigreturn - return from sig_handler_2 2188fd34e1cSWang Nan * overflows++ -> overflows = 2 2198fd34e1cSWang Nan * sys_rt_sigreturn - return from sig_handler 2208fd34e1cSWang Nan * the_var++ - fd3 event watchpoint hit -> count3 == 2 (standalone watchpoint) 2218fd34e1cSWang Nan * - SIGIO is delivered 2228fd34e1cSWang Nan * sig_handler - fd2 event breakpoint hit -> count2 == 3 2238fd34e1cSWang Nan * - SIGUSR1 is delivered 2248fd34e1cSWang Nan * sig_handler_2 -> overflows_2 == 3 (nested signal) 2258fd34e1cSWang Nan * sys_rt_sigreturn - return from sig_handler_2 2268fd34e1cSWang Nan * overflows++ -> overflows == 3 2278fd34e1cSWang Nan * sys_rt_sigreturn - return from sig_handler 2285a6bef47SJiri Olsa * 2295a6bef47SJiri Olsa * The test case check following error conditions: 2305a6bef47SJiri Olsa * - we get stuck in signal handler because of debug 2315a6bef47SJiri Olsa * exception being triggered receursively due to 2325a6bef47SJiri Olsa * the wrong RF EFLAG management 2335a6bef47SJiri Olsa * 2345a6bef47SJiri Olsa * - we never trigger the sig_handler breakpoint due 2355a6bef47SJiri Olsa * to the rong RF EFLAG management 2365a6bef47SJiri Olsa * 2375a6bef47SJiri Olsa */ 2385a6bef47SJiri Olsa 2398fd34e1cSWang Nan fd1 = bp_event(__test_function, SIGIO); 2408fd34e1cSWang Nan fd2 = bp_event(sig_handler, SIGUSR1); 2418fd34e1cSWang Nan fd3 = wp_event((void *)&the_var, SIGIO); 2425a6bef47SJiri Olsa 2435a6bef47SJiri Olsa ioctl(fd1, PERF_EVENT_IOC_ENABLE, 0); 2445a6bef47SJiri Olsa ioctl(fd2, PERF_EVENT_IOC_ENABLE, 0); 2458fd34e1cSWang Nan ioctl(fd3, PERF_EVENT_IOC_ENABLE, 0); 2465a6bef47SJiri Olsa 2475a6bef47SJiri Olsa /* 2485a6bef47SJiri Olsa * Kick off the test by trigering 'fd1' 2495a6bef47SJiri Olsa * breakpoint. 2505a6bef47SJiri Olsa */ 2515a6bef47SJiri Olsa test_function(); 2525a6bef47SJiri Olsa 2535a6bef47SJiri Olsa ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0); 2545a6bef47SJiri Olsa ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0); 2558fd34e1cSWang Nan ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0); 2565a6bef47SJiri Olsa 2575a6bef47SJiri Olsa count1 = bp_count(fd1); 2585a6bef47SJiri Olsa count2 = bp_count(fd2); 2598fd34e1cSWang Nan count3 = bp_count(fd3); 2605a6bef47SJiri Olsa 2615a6bef47SJiri Olsa close(fd1); 2625a6bef47SJiri Olsa close(fd2); 2638fd34e1cSWang Nan close(fd3); 2645a6bef47SJiri Olsa 2658fd34e1cSWang Nan pr_debug("count1 %lld, count2 %lld, count3 %lld, overflow %d, overflows_2 %d\n", 2668fd34e1cSWang Nan count1, count2, count3, overflows, overflows_2); 2675a6bef47SJiri Olsa 2685a6bef47SJiri Olsa if (count1 != 1) { 2695a6bef47SJiri Olsa if (count1 == 11) 2705a6bef47SJiri Olsa pr_debug("failed: RF EFLAG recursion issue detected\n"); 2715a6bef47SJiri Olsa else 2725a6bef47SJiri Olsa pr_debug("failed: wrong count for bp1%lld\n", count1); 2735a6bef47SJiri Olsa } 2745a6bef47SJiri Olsa 2758fd34e1cSWang Nan if (overflows != 3) 2765a6bef47SJiri Olsa pr_debug("failed: wrong overflow hit\n"); 2775a6bef47SJiri Olsa 2788fd34e1cSWang Nan if (overflows_2 != 3) 2798fd34e1cSWang Nan pr_debug("failed: wrong overflow_2 hit\n"); 2808fd34e1cSWang Nan 2818fd34e1cSWang Nan if (count2 != 3) 2825a6bef47SJiri Olsa pr_debug("failed: wrong count for bp2\n"); 2835a6bef47SJiri Olsa 2848fd34e1cSWang Nan if (count3 != 2) 2858fd34e1cSWang Nan pr_debug("failed: wrong count for bp3\n"); 2868fd34e1cSWang Nan 2878fd34e1cSWang Nan return count1 == 1 && overflows == 3 && count2 == 3 && overflows_2 == 3 && count3 == 2 ? 2885a6bef47SJiri Olsa TEST_OK : TEST_FAIL; 2895a6bef47SJiri Olsa } 290598762cfSJiri Olsa 291598762cfSJiri Olsa bool test__bp_signal_is_supported(void) 292598762cfSJiri Olsa { 293598762cfSJiri Olsa /* 294598762cfSJiri Olsa * The powerpc so far does not have support to even create 295598762cfSJiri Olsa * instruction breakpoint using the perf event interface. 296598762cfSJiri Olsa * Once it's there we can release this. 297598762cfSJiri Olsa */ 298598762cfSJiri Olsa #ifdef __powerpc__ 299598762cfSJiri Olsa return false; 300598762cfSJiri Olsa #else 301598762cfSJiri Olsa return true; 302598762cfSJiri Olsa #endif 303598762cfSJiri Olsa } 304