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 per-task event vs an EBB - in that order. The pinned per-task
19  * event should prevent the EBB event from being enabled.
20  */
21 
setup_child_event(struct event * event,pid_t child_pid)22 static int setup_child_event(struct event *event, pid_t child_pid)
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 	FAIL_IF(event_open_with_pid(event, child_pid));
33 	FAIL_IF(event_enable(event));
34 
35 	return 0;
36 }
37 
task_event_pinned_vs_ebb(void)38 int task_event_pinned_vs_ebb(void)
39 {
40 	union pipe read_pipe, write_pipe;
41 	struct event event;
42 	pid_t pid;
43 	int rc;
44 
45 	SKIP_IF(!ebb_is_supported());
46 
47 	FAIL_IF(pipe(read_pipe.fds) == -1);
48 	FAIL_IF(pipe(write_pipe.fds) == -1);
49 
50 	pid = fork();
51 	if (pid == 0) {
52 		/* NB order of pipes looks reversed */
53 		exit(ebb_child(write_pipe, read_pipe));
54 	}
55 
56 	/* We setup the task event first */
57 	rc = setup_child_event(&event, pid);
58 	if (rc) {
59 		kill_child_and_wait(pid);
60 		return rc;
61 	}
62 
63 	/* Signal the child to install its EBB event and wait */
64 	if (sync_with_child(read_pipe, write_pipe))
65 		/* If it fails, wait for it to exit */
66 		goto wait;
67 
68 	/* Signal the child to run */
69 	FAIL_IF(sync_with_child(read_pipe, write_pipe));
70 
71 wait:
72 	/* We expect it to fail to read the event */
73 	FAIL_IF(wait_for_child(pid) != 2);
74 	FAIL_IF(event_disable(&event));
75 	FAIL_IF(event_read(&event));
76 
77 	event_report(&event);
78 
79 	FAIL_IF(event.result.value == 0);
80 	/*
81 	 * For reasons I don't understand enabled is usually just slightly
82 	 * lower than running. Would be good to confirm why.
83 	 */
84 	FAIL_IF(event.result.enabled == 0);
85 	FAIL_IF(event.result.running == 0);
86 
87 	return 0;
88 }
89 
main(void)90 int main(void)
91 {
92 	return test_harness(task_event_pinned_vs_ebb, "task_event_pinned_vs_ebb");
93 }
94