1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2014, Michael Ellerman, IBM Corp.
4  */
5 
6 #include <signal.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <stdbool.h>
10 #include <sys/types.h>
11 #include <sys/wait.h>
12 #include <unistd.h>
13 
14 #include "ebb.h"
15 
16 
17 /*
18  * Tests a cpu event vs an EBB - in that order. The EBB should force the cpu
19  * event off the PMU.
20  */
21 
setup_cpu_event(struct event * event,int cpu)22 static int setup_cpu_event(struct event *event, int cpu)
23 {
24 	event_init_named(event, 0x400FA, "PM_RUN_INST_CMPL");
25 
26 	event->attr.exclude_kernel = 1;
27 	event->attr.exclude_hv = 1;
28 	event->attr.exclude_idle = 1;
29 
30 	SKIP_IF(require_paranoia_below(1));
31 	FAIL_IF(event_open_with_cpu(event, cpu));
32 	FAIL_IF(event_enable(event));
33 
34 	return 0;
35 }
36 
cpu_event_vs_ebb(void)37 int cpu_event_vs_ebb(void)
38 {
39 	union pipe read_pipe, write_pipe;
40 	struct event event;
41 	int cpu, rc;
42 	pid_t pid;
43 
44 	SKIP_IF(!ebb_is_supported());
45 
46 	cpu = bind_to_cpu(BIND_CPU_ANY);
47 	FAIL_IF(cpu < 0);
48 
49 	FAIL_IF(pipe(read_pipe.fds) == -1);
50 	FAIL_IF(pipe(write_pipe.fds) == -1);
51 
52 	pid = fork();
53 	if (pid == 0) {
54 		/* NB order of pipes looks reversed */
55 		exit(ebb_child(write_pipe, read_pipe));
56 	}
57 
58 	/* We setup the cpu event first */
59 	rc = setup_cpu_event(&event, cpu);
60 	if (rc) {
61 		kill_child_and_wait(pid);
62 		return rc;
63 	}
64 
65 	/* Signal the child to install its EBB event and wait */
66 	if (sync_with_child(read_pipe, write_pipe))
67 		/* If it fails, wait for it to exit */
68 		goto wait;
69 
70 	/* Signal the child to run */
71 	FAIL_IF(sync_with_child(read_pipe, write_pipe));
72 
73 wait:
74 	/* We expect the child to succeed */
75 	FAIL_IF(wait_for_child(pid));
76 
77 	FAIL_IF(event_disable(&event));
78 	FAIL_IF(event_read(&event));
79 
80 	event_report(&event);
81 
82 	/* The cpu event may have run */
83 
84 	return 0;
85 }
86 
main(void)87 int main(void)
88 {
89 	return test_harness(cpu_event_vs_ebb, "cpu_event_vs_ebb");
90 }
91