xref: /xnu-10002.41.9/bsd/sys/perfmon_private.h (revision 699cd48037512bf4380799317ca44ca453c82f57)
1 // Copyright (c) 2020 Apple Inc. All rights reserved.
2 //
3 // @APPLE_OSREFERENCE_LICENSE_HEADER_START@
4 //
5 // This file contains Original Code and/or Modifications of Original Code
6 // as defined in and that are subject to the Apple Public Source License
7 // Version 2.0 (the 'License'). You may not use this file except in
8 // compliance with the License. The rights granted to you under the License
9 // may not be used to create, or enable the creation or redistribution of,
10 // unlawful or unlicensed copies of an Apple operating system, or to
11 // circumvent, violate, or enable the circumvention or violation of, any
12 // terms of an Apple operating system software license agreement.
13 //
14 // Please obtain a copy of the License at
15 // http://www.opensource.apple.com/apsl/ and read it before using this file.
16 //
17 // The Original Code and all software distributed under the License are
18 // distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
19 // EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
20 // INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
22 // Please see the License for the specific language governing rights and
23 // limitations under the License.
24 //
25 // @APPLE_OSREFERENCE_LICENSE_HEADER_END@
26 
27 #ifndef BSD_PERFMON_PRIVATE_H
28 #define BSD_PERFMON_PRIVATE_H
29 
30 #if KERNEL
31 #include <machine/limits.h>
32 #else // KERNEL
33 #include <limits.h>
34 #endif // !KERNEL
35 #include <stdint.h>
36 #include <stdbool.h>
37 #include <sys/cdefs.h>
38 
39 __BEGIN_DECLS
40 
41 struct perfmon_layout {
42 	unsigned short pl_counter_count;
43 	unsigned short pl_fixed_offset;
44 	unsigned short pl_fixed_count;
45 	unsigned short pl_unit_count;
46 	unsigned short pl_reg_count;
47 	unsigned short pl_attr_count;
48 };
49 
50 typedef char perfmon_name_t[16];
51 
52 struct perfmon_event {
53 	char pe_name[32];
54 	uint64_t pe_number;
55 	unsigned short pe_counter;
56 };
57 
58 struct perfmon_attr {
59 	perfmon_name_t pa_name;
60 	uint64_t pa_value;
61 };
62 
63 struct perfmon_spec {
64 	struct perfmon_event *ps_events;
65 	struct perfmon_attr *ps_attrs;
66 	unsigned short ps_event_count;
67 	unsigned short ps_attr_count;
68 };
69 
70 #if !MACH_KERNEL_PRIVATE
71 
72 __END_DECLS
73 
74 #include <sys/ioccom.h>
75 
76 __BEGIN_DECLS
77 
78 // A perfmon file is initially mutable, where events can be added and
79 // attributes set.  The fine-grained nature of this API gives clients insight
80 // into which specific configuration was erroneous and the associated overheads
81 // are taken only when the system is configured, at process startup.
82 enum perfmon_ioctl {
83 	// PMU metadata, always allowed.
84 
85 	// Immutable information about the PMU.
86 	PERFMON_CTL_GET_LAYOUT = _IOR('P', 0, struct perfmon_layout),
87 	PERFMON_CTL_LIST_ATTRS = _IO('P', 1),
88 	PERFMON_CTL_LIST_REGS = _IO('P', 2),
89 
90 	// Get diagnostic information by sampling the hardware registers.
91 	PERFMON_CTL_SAMPLE_REGS = _IO('P', 3),
92 
93 	// Set up a configuration, only allowed when open for writing and before
94 	// configure.
95 
96 	// Add an event to the configuration.
97 	PERFMON_CTL_ADD_EVENT = _IOWR('P', 5, struct perfmon_event),
98 	// Apply attributes to a given event, or globally.
99 	PERFMON_CTL_SET_ATTR = _IOW('P', 6, struct perfmon_attr),
100 
101 	// Control the state of the PMU, only allowed when open for writing.
102 
103 	// Configure the monitor, no more settings can be adjusted after this is
104 	// called, one time only.
105 	PERFMON_CTL_CONFIGURE = _IO('P', 7),
106 	// Start counting, only allowed after configuration and counting stopped.
107 	PERFMON_CTL_START = _IO('P', 8),
108 	// Stop counting, only allowed with counting started.
109 	PERFMON_CTL_STOP = _IO('P', 9),
110 
111 	// Query the confguration, only allowed when open for writing.
112 	PERFMON_CTL_SPECIFY = _IOWR('P', 10, struct perfmon_spec),
113 };
114 
115 #if XNU_KERNEL_PRIVATE
116 
117 // Called from the pseudo-device initializer in config/MASTER.
118 int perfmon_dev_init(void);
119 
120 #endif // XNU_KERNEL_PRIVATE
121 
122 #endif // !MACH_KERNEL_PRIVATE
123 
124 __END_DECLS
125 
126 #endif // !defined(BSD_PERFMON_PRIVATE_H)
127