xref: /openbmc/linux/tools/perf/tests/bp_signal.c (revision 4d39c89f0b94bf4a6e1ccf42702e7d80d210a5fd)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
25a6bef47SJiri Olsa /*
35a6bef47SJiri Olsa  * Inspired by breakpoint overflow test done by
45a6bef47SJiri Olsa  * Vince Weaver <vincent.weaver@maine.edu> for perf_event_tests
55a6bef47SJiri Olsa  * (git://github.com/deater/perf_event_tests)
65a6bef47SJiri Olsa  */
75a6bef47SJiri Olsa 
8b3539d21SSukadev Bhattiprolu /*
9b3539d21SSukadev Bhattiprolu  * Powerpc needs __SANE_USERSPACE_TYPES__ before <linux/types.h> to select
10b3539d21SSukadev Bhattiprolu  * 'int-ll64.h' and avoid compile warnings when printing __u64 with %llu.
11b3539d21SSukadev Bhattiprolu  */
12b3539d21SSukadev Bhattiprolu #define __SANE_USERSPACE_TYPES__
13b3539d21SSukadev Bhattiprolu 
145a6bef47SJiri Olsa #include <stdlib.h>
155a6bef47SJiri Olsa #include <stdio.h>
165a6bef47SJiri Olsa #include <unistd.h>
175a6bef47SJiri Olsa #include <string.h>
185a6bef47SJiri Olsa #include <sys/ioctl.h>
195a6bef47SJiri Olsa #include <time.h>
205a6bef47SJiri Olsa #include <fcntl.h>
215a6bef47SJiri Olsa #include <signal.h>
225a6bef47SJiri Olsa #include <sys/mman.h>
235a6bef47SJiri Olsa #include <linux/compiler.h>
245a6bef47SJiri Olsa #include <linux/hw_breakpoint.h>
255a6bef47SJiri Olsa 
265a6bef47SJiri Olsa #include "tests.h"
275a6bef47SJiri Olsa #include "debug.h"
288520a98dSArnaldo Carvalho de Melo #include "event.h"
2991854f9aSArnaldo Carvalho de Melo #include "perf-sys.h"
3057480d2cSYann Droneaud #include "cloexec.h"
315a6bef47SJiri Olsa 
325a6bef47SJiri Olsa static int fd1;
335a6bef47SJiri Olsa static int fd2;
348fd34e1cSWang Nan static int fd3;
355a6bef47SJiri Olsa static int overflows;
368fd34e1cSWang Nan static int overflows_2;
378fd34e1cSWang Nan 
388fd34e1cSWang Nan volatile long the_var;
398fd34e1cSWang Nan 
408fd34e1cSWang Nan 
418fd34e1cSWang Nan /*
428fd34e1cSWang Nan  * Use ASM to ensure watchpoint and breakpoint can be triggered
438fd34e1cSWang Nan  * at one instruction.
448fd34e1cSWang Nan  */
458fd34e1cSWang Nan #if defined (__x86_64__)
468fd34e1cSWang Nan extern void __test_function(volatile long *ptr);
478fd34e1cSWang Nan asm (
488a39e8c4SJiri Olsa 	".pushsection .text;"
498fd34e1cSWang Nan 	".globl __test_function\n"
508a39e8c4SJiri Olsa 	".type __test_function, @function;"
518fd34e1cSWang Nan 	"__test_function:\n"
528fd34e1cSWang Nan 	"incq (%rdi)\n"
538a39e8c4SJiri Olsa 	"ret\n"
548a39e8c4SJiri Olsa 	".popsection\n");
558fd34e1cSWang Nan #else
568fd34e1cSWang Nan static void __test_function(volatile long *ptr)
578fd34e1cSWang Nan {
588fd34e1cSWang Nan 	*ptr = 0x1234;
598fd34e1cSWang Nan }
608fd34e1cSWang Nan #endif
615a6bef47SJiri Olsa 
629dd4ca47SArnaldo Carvalho de Melo static noinline int test_function(void)
635a6bef47SJiri Olsa {
648fd34e1cSWang Nan 	__test_function(&the_var);
658fd34e1cSWang Nan 	the_var++;
665a6bef47SJiri Olsa 	return time(NULL);
675a6bef47SJiri Olsa }
685a6bef47SJiri Olsa 
698fd34e1cSWang Nan static void sig_handler_2(int signum __maybe_unused,
708fd34e1cSWang Nan 			  siginfo_t *oh __maybe_unused,
718fd34e1cSWang Nan 			  void *uc __maybe_unused)
728fd34e1cSWang Nan {
738fd34e1cSWang Nan 	overflows_2++;
748fd34e1cSWang Nan 	if (overflows_2 > 10) {
758fd34e1cSWang Nan 		ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
768fd34e1cSWang Nan 		ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
778fd34e1cSWang Nan 		ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
788fd34e1cSWang Nan 	}
798fd34e1cSWang Nan }
808fd34e1cSWang Nan 
815a6bef47SJiri Olsa static void sig_handler(int signum __maybe_unused,
825a6bef47SJiri Olsa 			siginfo_t *oh __maybe_unused,
835a6bef47SJiri Olsa 			void *uc __maybe_unused)
845a6bef47SJiri Olsa {
855a6bef47SJiri Olsa 	overflows++;
865a6bef47SJiri Olsa 
875a6bef47SJiri Olsa 	if (overflows > 10) {
885a6bef47SJiri Olsa 		/*
895a6bef47SJiri Olsa 		 * This should be executed only once during
905a6bef47SJiri Olsa 		 * this test, if we are here for the 10th
915a6bef47SJiri Olsa 		 * time, consider this the recursive issue.
925a6bef47SJiri Olsa 		 *
935a6bef47SJiri Olsa 		 * We can get out of here by disable events,
945a6bef47SJiri Olsa 		 * so no new SIGIO is delivered.
955a6bef47SJiri Olsa 		 */
965a6bef47SJiri Olsa 		ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
975a6bef47SJiri Olsa 		ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
988fd34e1cSWang Nan 		ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
995a6bef47SJiri Olsa 	}
1005a6bef47SJiri Olsa }
1015a6bef47SJiri Olsa 
1021ad826baSArnaldo Carvalho de Melo static int __event(bool is_x, void *addr, int sig)
1035a6bef47SJiri Olsa {
1045a6bef47SJiri Olsa 	struct perf_event_attr pe;
1055a6bef47SJiri Olsa 	int fd;
1065a6bef47SJiri Olsa 
1075a6bef47SJiri Olsa 	memset(&pe, 0, sizeof(struct perf_event_attr));
1085a6bef47SJiri Olsa 	pe.type = PERF_TYPE_BREAKPOINT;
1095a6bef47SJiri Olsa 	pe.size = sizeof(struct perf_event_attr);
1105a6bef47SJiri Olsa 
1115a6bef47SJiri Olsa 	pe.config = 0;
1128fd34e1cSWang Nan 	pe.bp_type = is_x ? HW_BREAKPOINT_X : HW_BREAKPOINT_W;
1138fd34e1cSWang Nan 	pe.bp_addr = (unsigned long) addr;
1145a6bef47SJiri Olsa 	pe.bp_len = sizeof(long);
1155a6bef47SJiri Olsa 
1165a6bef47SJiri Olsa 	pe.sample_period = 1;
1175a6bef47SJiri Olsa 	pe.sample_type = PERF_SAMPLE_IP;
1185a6bef47SJiri Olsa 	pe.wakeup_events = 1;
1195a6bef47SJiri Olsa 
1205a6bef47SJiri Olsa 	pe.disabled = 1;
1215a6bef47SJiri Olsa 	pe.exclude_kernel = 1;
1225a6bef47SJiri Olsa 	pe.exclude_hv = 1;
1235a6bef47SJiri Olsa 
12457480d2cSYann Droneaud 	fd = sys_perf_event_open(&pe, 0, -1, -1,
12557480d2cSYann Droneaud 				 perf_event_open_cloexec_flag());
1265a6bef47SJiri Olsa 	if (fd < 0) {
1275a6bef47SJiri Olsa 		pr_debug("failed opening event %llx\n", pe.config);
1285a6bef47SJiri Olsa 		return TEST_FAIL;
1295a6bef47SJiri Olsa 	}
1305a6bef47SJiri Olsa 
1315a6bef47SJiri Olsa 	fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
1321ad826baSArnaldo Carvalho de Melo 	fcntl(fd, F_SETSIG, sig);
1335a6bef47SJiri Olsa 	fcntl(fd, F_SETOWN, getpid());
1345a6bef47SJiri Olsa 
1355a6bef47SJiri Olsa 	ioctl(fd, PERF_EVENT_IOC_RESET, 0);
1365a6bef47SJiri Olsa 
1375a6bef47SJiri Olsa 	return fd;
1385a6bef47SJiri Olsa }
1395a6bef47SJiri Olsa 
1401ad826baSArnaldo Carvalho de Melo static int bp_event(void *addr, int sig)
1418fd34e1cSWang Nan {
1421ad826baSArnaldo Carvalho de Melo 	return __event(true, addr, sig);
1438fd34e1cSWang Nan }
1448fd34e1cSWang Nan 
1451ad826baSArnaldo Carvalho de Melo static int wp_event(void *addr, int sig)
1468fd34e1cSWang Nan {
1471ad826baSArnaldo Carvalho de Melo 	return __event(false, addr, sig);
1488fd34e1cSWang Nan }
1498fd34e1cSWang Nan 
1505a6bef47SJiri Olsa static long long bp_count(int fd)
1515a6bef47SJiri Olsa {
1525a6bef47SJiri Olsa 	long long count;
1535a6bef47SJiri Olsa 	int ret;
1545a6bef47SJiri Olsa 
1555a6bef47SJiri Olsa 	ret = read(fd, &count, sizeof(long long));
1565a6bef47SJiri Olsa 	if (ret != sizeof(long long)) {
1575a6bef47SJiri Olsa 		pr_debug("failed to read: %d\n", ret);
1585a6bef47SJiri Olsa 		return TEST_FAIL;
1595a6bef47SJiri Olsa 	}
1605a6bef47SJiri Olsa 
1615a6bef47SJiri Olsa 	return count;
1625a6bef47SJiri Olsa }
1635a6bef47SJiri Olsa 
16481f17c90SArnaldo Carvalho de Melo int test__bp_signal(struct test *test __maybe_unused, int subtest __maybe_unused)
1655a6bef47SJiri Olsa {
1665a6bef47SJiri Olsa 	struct sigaction sa;
1678fd34e1cSWang Nan 	long long count1, count2, count3;
1685a6bef47SJiri Olsa 
1695a6bef47SJiri Olsa 	/* setup SIGIO signal handler */
1705a6bef47SJiri Olsa 	memset(&sa, 0, sizeof(struct sigaction));
1715a6bef47SJiri Olsa 	sa.sa_sigaction = (void *) sig_handler;
1725a6bef47SJiri Olsa 	sa.sa_flags = SA_SIGINFO;
1735a6bef47SJiri Olsa 
1745a6bef47SJiri Olsa 	if (sigaction(SIGIO, &sa, NULL) < 0) {
1755a6bef47SJiri Olsa 		pr_debug("failed setting up signal handler\n");
1765a6bef47SJiri Olsa 		return TEST_FAIL;
1775a6bef47SJiri Olsa 	}
1785a6bef47SJiri Olsa 
1798fd34e1cSWang Nan 	sa.sa_sigaction = (void *) sig_handler_2;
1808fd34e1cSWang Nan 	if (sigaction(SIGUSR1, &sa, NULL) < 0) {
1818fd34e1cSWang Nan 		pr_debug("failed setting up signal handler 2\n");
1828fd34e1cSWang Nan 		return TEST_FAIL;
1838fd34e1cSWang Nan 	}
1848fd34e1cSWang Nan 
1855a6bef47SJiri Olsa 	/*
1865a6bef47SJiri Olsa 	 * We create following events:
1875a6bef47SJiri Olsa 	 *
1888fd34e1cSWang Nan 	 * fd1 - breakpoint event on __test_function with SIGIO
1895a6bef47SJiri Olsa 	 *       signal configured. We should get signal
1905a6bef47SJiri Olsa 	 *       notification each time the breakpoint is hit
1915a6bef47SJiri Olsa 	 *
1928fd34e1cSWang Nan 	 * fd2 - breakpoint event on sig_handler with SIGUSR1
1938fd34e1cSWang Nan 	 *       configured. We should get SIGUSR1 each time when
1948fd34e1cSWang Nan 	 *       breakpoint is hit
1958fd34e1cSWang Nan 	 *
1968fd34e1cSWang Nan 	 * fd3 - watchpoint event on __test_function with SIGIO
1975a6bef47SJiri Olsa 	 *       configured.
1985a6bef47SJiri Olsa 	 *
1995a6bef47SJiri Olsa 	 * Following processing should happen:
2008fd34e1cSWang Nan 	 *   Exec:               Action:                       Result:
2018fd34e1cSWang Nan 	 *   incq (%rdi)       - fd1 event breakpoint hit   -> count1 == 1
2028fd34e1cSWang Nan 	 *                     - SIGIO is delivered
2038fd34e1cSWang Nan 	 *   sig_handler       - fd2 event breakpoint hit   -> count2 == 1
2048fd34e1cSWang Nan 	 *                     - SIGUSR1 is delivered
2058fd34e1cSWang Nan 	 *   sig_handler_2                                  -> overflows_2 == 1  (nested signal)
2068fd34e1cSWang Nan 	 *   sys_rt_sigreturn  - return from sig_handler_2
2078fd34e1cSWang Nan 	 *   overflows++                                    -> overflows = 1
2088fd34e1cSWang Nan 	 *   sys_rt_sigreturn  - return from sig_handler
2098fd34e1cSWang Nan 	 *   incq (%rdi)       - fd3 event watchpoint hit   -> count3 == 1       (wp and bp in one insn)
2108fd34e1cSWang Nan 	 *                     - SIGIO is delivered
2118fd34e1cSWang Nan 	 *   sig_handler       - fd2 event breakpoint hit   -> count2 == 2
2128fd34e1cSWang Nan 	 *                     - SIGUSR1 is delivered
2138fd34e1cSWang Nan 	 *   sig_handler_2                                  -> overflows_2 == 2  (nested signal)
2148fd34e1cSWang Nan 	 *   sys_rt_sigreturn  - return from sig_handler_2
2158fd34e1cSWang Nan 	 *   overflows++                                    -> overflows = 2
2168fd34e1cSWang Nan 	 *   sys_rt_sigreturn  - return from sig_handler
2178fd34e1cSWang Nan 	 *   the_var++         - fd3 event watchpoint hit   -> count3 == 2       (standalone watchpoint)
2188fd34e1cSWang Nan 	 *                     - SIGIO is delivered
2198fd34e1cSWang Nan 	 *   sig_handler       - fd2 event breakpoint hit   -> count2 == 3
2208fd34e1cSWang Nan 	 *                     - SIGUSR1 is delivered
2218fd34e1cSWang Nan 	 *   sig_handler_2                                  -> overflows_2 == 3  (nested signal)
2228fd34e1cSWang Nan 	 *   sys_rt_sigreturn  - return from sig_handler_2
2238fd34e1cSWang Nan 	 *   overflows++                                    -> overflows == 3
2248fd34e1cSWang Nan 	 *   sys_rt_sigreturn  - return from sig_handler
2255a6bef47SJiri Olsa 	 *
2265a6bef47SJiri Olsa 	 * The test case check following error conditions:
2275a6bef47SJiri Olsa 	 * - we get stuck in signal handler because of debug
228*4d39c89fSIngo Molnar 	 *   exception being triggered recursively due to
2295a6bef47SJiri Olsa 	 *   the wrong RF EFLAG management
2305a6bef47SJiri Olsa 	 *
2315a6bef47SJiri Olsa 	 * - we never trigger the sig_handler breakpoint due
232*4d39c89fSIngo Molnar 	 *   to the wrong RF EFLAG management
2335a6bef47SJiri Olsa 	 *
2345a6bef47SJiri Olsa 	 */
2355a6bef47SJiri Olsa 
2368fd34e1cSWang Nan 	fd1 = bp_event(__test_function, SIGIO);
2378fd34e1cSWang Nan 	fd2 = bp_event(sig_handler, SIGUSR1);
2388fd34e1cSWang Nan 	fd3 = wp_event((void *)&the_var, SIGIO);
2395a6bef47SJiri Olsa 
2405a6bef47SJiri Olsa 	ioctl(fd1, PERF_EVENT_IOC_ENABLE, 0);
2415a6bef47SJiri Olsa 	ioctl(fd2, PERF_EVENT_IOC_ENABLE, 0);
2428fd34e1cSWang Nan 	ioctl(fd3, PERF_EVENT_IOC_ENABLE, 0);
2435a6bef47SJiri Olsa 
2445a6bef47SJiri Olsa 	/*
245*4d39c89fSIngo Molnar 	 * Kick off the test by triggering 'fd1'
2465a6bef47SJiri Olsa 	 * breakpoint.
2475a6bef47SJiri Olsa 	 */
2485a6bef47SJiri Olsa 	test_function();
2495a6bef47SJiri Olsa 
2505a6bef47SJiri Olsa 	ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
2515a6bef47SJiri Olsa 	ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
2528fd34e1cSWang Nan 	ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
2535a6bef47SJiri Olsa 
2545a6bef47SJiri Olsa 	count1 = bp_count(fd1);
2555a6bef47SJiri Olsa 	count2 = bp_count(fd2);
2568fd34e1cSWang Nan 	count3 = bp_count(fd3);
2575a6bef47SJiri Olsa 
2585a6bef47SJiri Olsa 	close(fd1);
2595a6bef47SJiri Olsa 	close(fd2);
2608fd34e1cSWang Nan 	close(fd3);
2615a6bef47SJiri Olsa 
2628fd34e1cSWang Nan 	pr_debug("count1 %lld, count2 %lld, count3 %lld, overflow %d, overflows_2 %d\n",
2638fd34e1cSWang Nan 		 count1, count2, count3, overflows, overflows_2);
2645a6bef47SJiri Olsa 
2655a6bef47SJiri Olsa 	if (count1 != 1) {
2665a6bef47SJiri Olsa 		if (count1 == 11)
2675a6bef47SJiri Olsa 			pr_debug("failed: RF EFLAG recursion issue detected\n");
2685a6bef47SJiri Olsa 		else
2696ae9c10bSArnaldo Carvalho de Melo 			pr_debug("failed: wrong count for bp1: %lld, expected 1\n", count1);
2705a6bef47SJiri Olsa 	}
2715a6bef47SJiri Olsa 
2728fd34e1cSWang Nan 	if (overflows != 3)
2736ae9c10bSArnaldo Carvalho de Melo 		pr_debug("failed: wrong overflow (%d) hit, expected 3\n", overflows);
2745a6bef47SJiri Olsa 
2758fd34e1cSWang Nan 	if (overflows_2 != 3)
2766ae9c10bSArnaldo Carvalho de Melo 		pr_debug("failed: wrong overflow_2 (%d) hit, expected 3\n", overflows_2);
2778fd34e1cSWang Nan 
2788fd34e1cSWang Nan 	if (count2 != 3)
2796ae9c10bSArnaldo Carvalho de Melo 		pr_debug("failed: wrong count for bp2 (%lld), expected 3\n", count2);
2805a6bef47SJiri Olsa 
2818fd34e1cSWang Nan 	if (count3 != 2)
2826ae9c10bSArnaldo Carvalho de Melo 		pr_debug("failed: wrong count for bp3 (%lld), expected 2\n", count3);
2838fd34e1cSWang Nan 
2848fd34e1cSWang Nan 	return count1 == 1 && overflows == 3 && count2 == 3 && overflows_2 == 3 && count3 == 2 ?
2855a6bef47SJiri Olsa 		TEST_OK : TEST_FAIL;
2865a6bef47SJiri Olsa }
287598762cfSJiri Olsa 
288598762cfSJiri Olsa bool test__bp_signal_is_supported(void)
289598762cfSJiri Olsa {
290598762cfSJiri Olsa 	/*
29124f96733SFlorian Fainelli 	 * PowerPC and S390 do not support creation of instruction
29224f96733SFlorian Fainelli 	 * breakpoints using the perf_event interface.
29324f96733SFlorian Fainelli 	 *
29424f96733SFlorian Fainelli 	 * ARM requires explicit rounding down of the instruction
29524f96733SFlorian Fainelli 	 * pointer in Thumb mode, and then requires the single-step
29624f96733SFlorian Fainelli 	 * to be handled explicitly in the overflow handler to avoid
29724f96733SFlorian Fainelli 	 * stepping into the SIGIO handler and getting stuck on the
29824f96733SFlorian Fainelli 	 * breakpointed instruction.
29924f96733SFlorian Fainelli 	 *
3006a5f3d94SLeo Yan 	 * Since arm64 has the same issue with arm for the single-step
301b7dc21f5SLeo Yan 	 * handling, this case also gets stuck on the breakpointed
3026a5f3d94SLeo Yan 	 * instruction.
3036a5f3d94SLeo Yan 	 *
30424f96733SFlorian Fainelli 	 * Just disable the test for these architectures until these
30524f96733SFlorian Fainelli 	 * issues are resolved.
306598762cfSJiri Olsa 	 */
3076a5f3d94SLeo Yan #if defined(__powerpc__) || defined(__s390x__) || defined(__arm__) || \
3086a5f3d94SLeo Yan     defined(__aarch64__)
309598762cfSJiri Olsa 	return false;
310598762cfSJiri Olsa #else
311598762cfSJiri Olsa 	return true;
312598762cfSJiri Olsa #endif
313598762cfSJiri Olsa }
314