1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2014, Michael Ellerman, IBM Corp.
4  */
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 
9 #include "ebb.h"
10 
11 
12 /*
13  * Test various attributes of the EBB event are enforced.
14  */
15 int event_attributes(void)
16 {
17 	struct event event, leader;
18 
19 	SKIP_IF(!ebb_is_supported());
20 
21 	event_init(&event, 0x1001e);
22 	event_leader_ebb_init(&event);
23 	/* Expected to succeed */
24 	FAIL_IF(event_open(&event));
25 	event_close(&event);
26 
27 
28 	event_init(&event, 0x001e); /* CYCLES - no PMC specified */
29 	event_leader_ebb_init(&event);
30 	/* Expected to fail, no PMC specified */
31 	FAIL_IF(event_open(&event) == 0);
32 
33 
34 	event_init(&event, 0x2001e);
35 	event_leader_ebb_init(&event);
36 	event.attr.exclusive = 0;
37 	/* Expected to fail, not exclusive */
38 	FAIL_IF(event_open(&event) == 0);
39 
40 
41 	event_init(&event, 0x3001e);
42 	event_leader_ebb_init(&event);
43 	event.attr.freq = 1;
44 	/* Expected to fail, sets freq */
45 	FAIL_IF(event_open(&event) == 0);
46 
47 
48 	event_init(&event, 0x4001e);
49 	event_leader_ebb_init(&event);
50 	event.attr.sample_period = 1;
51 	/* Expected to fail, sets sample_period */
52 	FAIL_IF(event_open(&event) == 0);
53 
54 
55 	event_init(&event, 0x1001e);
56 	event_leader_ebb_init(&event);
57 	event.attr.enable_on_exec = 1;
58 	/* Expected to fail, sets enable_on_exec */
59 	FAIL_IF(event_open(&event) == 0);
60 
61 
62 	event_init(&event, 0x1001e);
63 	event_leader_ebb_init(&event);
64 	event.attr.inherit = 1;
65 	/* Expected to fail, sets inherit */
66 	FAIL_IF(event_open(&event) == 0);
67 
68 
69 	event_init(&leader, 0x1001e);
70 	event_leader_ebb_init(&leader);
71 	FAIL_IF(event_open(&leader));
72 
73 	event_init(&event, 0x20002);
74 	event_ebb_init(&event);
75 
76 	/* Expected to succeed */
77 	FAIL_IF(event_open_with_group(&event, leader.fd));
78 	event_close(&leader);
79 	event_close(&event);
80 
81 
82 	event_init(&leader, 0x1001e);
83 	event_leader_ebb_init(&leader);
84 	FAIL_IF(event_open(&leader));
85 
86 	event_init(&event, 0x20002);
87 
88 	/* Expected to fail, event doesn't request EBB, leader does */
89 	FAIL_IF(event_open_with_group(&event, leader.fd) == 0);
90 	event_close(&leader);
91 
92 
93 	event_init(&leader, 0x1001e);
94 	event_leader_ebb_init(&leader);
95 	/* Clear the EBB flag */
96 	leader.attr.config &= ~(1ull << 63);
97 
98 	FAIL_IF(event_open(&leader));
99 
100 	event_init(&event, 0x20002);
101 	event_ebb_init(&event);
102 
103 	/* Expected to fail, leader doesn't request EBB */
104 	FAIL_IF(event_open_with_group(&event, leader.fd) == 0);
105 	event_close(&leader);
106 
107 
108 	event_init(&leader, 0x1001e);
109 	event_leader_ebb_init(&leader);
110 	leader.attr.exclusive = 0;
111 	/* Expected to fail, leader isn't exclusive */
112 	FAIL_IF(event_open(&leader) == 0);
113 
114 
115 	event_init(&leader, 0x1001e);
116 	event_leader_ebb_init(&leader);
117 	leader.attr.pinned = 0;
118 	/* Expected to fail, leader isn't pinned */
119 	FAIL_IF(event_open(&leader) == 0);
120 
121 	event_init(&event, 0x1001e);
122 	event_leader_ebb_init(&event);
123 	/* Expected to fail, not a task event */
124 	SKIP_IF(require_paranoia_below(1));
125 	FAIL_IF(event_open_with_cpu(&event, 0) == 0);
126 
127 	return 0;
128 }
129 
130 int main(void)
131 {
132 	return test_harness(event_attributes, "event_attributes");
133 }
134