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 pinned cpu event vs an EBB - in that order. The pinned cpu event
19  * should remain and the EBB event should fail to enable.
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.pinned = 1;
27 
28 	event->attr.exclude_kernel = 1;
29 	event->attr.exclude_hv = 1;
30 	event->attr.exclude_idle = 1;
31 
32 	SKIP_IF(require_paranoia_below(1));
33 	FAIL_IF(event_open_with_cpu(event, cpu));
34 	FAIL_IF(event_enable(event));
35 
36 	return 0;
37 }
38 
cpu_event_pinned_vs_ebb(void)39 int cpu_event_pinned_vs_ebb(void)
40 {
41 	union pipe read_pipe, write_pipe;
42 	struct event event;
43 	int cpu, rc;
44 	pid_t pid;
45 
46 	SKIP_IF(!ebb_is_supported());
47 
48 	cpu = bind_to_cpu(BIND_CPU_ANY);
49 	FAIL_IF(cpu < 0);
50 
51 	FAIL_IF(pipe(read_pipe.fds) == -1);
52 	FAIL_IF(pipe(write_pipe.fds) == -1);
53 
54 	pid = fork();
55 	if (pid == 0) {
56 		/* NB order of pipes looks reversed */
57 		exit(ebb_child(write_pipe, read_pipe));
58 	}
59 
60 	/* We setup the cpu event first */
61 	rc = setup_cpu_event(&event, cpu);
62 	if (rc) {
63 		kill_child_and_wait(pid);
64 		return rc;
65 	}
66 
67 	/* Signal the child to install its EBB event and wait */
68 	if (sync_with_child(read_pipe, write_pipe))
69 		/* If it fails, wait for it to exit */
70 		goto wait;
71 
72 	/* Signal the child to run */
73 	FAIL_IF(sync_with_child(read_pipe, write_pipe));
74 
75 wait:
76 	/* We expect it to fail to read the event */
77 	FAIL_IF(wait_for_child(pid) != 2);
78 
79 	FAIL_IF(event_disable(&event));
80 	FAIL_IF(event_read(&event));
81 
82 	event_report(&event);
83 
84 	/* The cpu event should have run */
85 	FAIL_IF(event.result.value == 0);
86 	FAIL_IF(event.result.enabled != event.result.running);
87 
88 	return 0;
89 }
90 
main(void)91 int main(void)
92 {
93 	return test_harness(cpu_event_pinned_vs_ebb, "cpu_event_pinned_vs_ebb");
94 }
95