xref: /xnu-12377.81.4/osfmk/kern/exc_resource.h (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1 /*
2  * Copyright (c) 2011-2012 Apple Computer, Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 /*
29  * Mach Operating System
30  * Copyright (c) 1989 Carnegie-Mellon University
31  * Copyright (c) 1988 Carnegie-Mellon University
32  * Copyright (c) 1987 Carnegie-Mellon University
33  * All rights reserved.  The CMU software License Agreement specifies
34  * the terms and conditions for use and redistribution.
35  */
36 
37 /*
38  *        EXC_RESOURCE related macros, namespace etc.
39  */
40 
41 #ifndef _EXC_RESOURCE_H_
42 #define _EXC_RESOURCE_H_
43 
44 /*
45  * Generic exception code format:
46  *
47  * code:
48  * +----------------------------------------------------------+
49  * |[63:61] type | [60:58] flavor | [57:0] type-specific data |
50  * +----------------------------------------------------------+
51  */
52 
53 
54 /* EXC_RESOURCE type and flavor decoding routines */
55 #define EXC_RESOURCE_DECODE_RESOURCE_TYPE(code) \
56 	(((code) >> 61) & 0x7ULL)
57 #define EXC_RESOURCE_DECODE_FLAVOR(code) \
58 	(((code) >> 58) & 0x7ULL)
59 
60 /* EXC_RESOURCE Types */
61 #define RESOURCE_TYPE_CPU       1
62 #define RESOURCE_TYPE_WAKEUPS   2
63 #define RESOURCE_TYPE_MEMORY    3
64 #define RESOURCE_TYPE_IO        4
65 #define RESOURCE_TYPE_THREADS   5
66 #define RESOURCE_TYPE_PORTS     6
67 
68 /* RESOURCE_TYPE_CPU flavors */
69 #define FLAVOR_CPU_MONITOR              1
70 #define FLAVOR_CPU_MONITOR_FATAL        2
71 
72 /*
73  * RESOURCE_TYPE_CPU exception code & subcode.
74  *
75  * This is sent by the kernel when the CPU usage monitor
76  * is tripped. [See proc_set_cpumon_params()]
77  *
78  * code:
79  * +-----------------------------------------------+
80  * |[63:61] RESOURCE |[60:58] FLAVOR_CPU_ |[57:32] |
81  * |_TYPE_CPU        |MONITOR[_FATAL]     |Unused  |
82  * +-----------------------------------------------+
83  * |[31:7]  Interval (sec)    | [6:0] CPU limit (%)|
84  * +-----------------------------------------------+
85  *
86  * subcode:
87  * +-----------------------------------------------+
88  * |                          | [6:0] % of CPU     |
89  * |                          | actually consumed  |
90  * +-----------------------------------------------+
91  *
92  */
93 
94 /* RESOURCE_TYPE_CPU decoding macros */
95 #define EXC_RESOURCE_CPUMONITOR_DECODE_INTERVAL(code) \
96 	(((code) >> 7) & 0x1FFFFFFULL)
97 #define EXC_RESOURCE_CPUMONITOR_DECODE_PERCENTAGE(code) \
98 	((code) & 0x7FULL)
99 #define EXC_RESOURCE_CPUMONITOR_DECODE_PERCENTAGE_OBSERVED(subcode) \
100 	((subcode) & 0x7FULL)
101 
102 
103 /* RESOURCE_TYPE_WAKEUPS flavors */
104 #define FLAVOR_WAKEUPS_MONITOR  1
105 
106 /*
107  * RESOURCE_TYPE_WAKEUPS exception code & subcode.
108  *
109  * This is sent by the kernel when the platform idle
110  * wakeups monitor is tripped.
111  * [See proc_set_wakeupsmon_params()]
112  *
113  * code:
114  * +-----------------------------------------------+
115  * |[63:61] RESOURCE |[60:58] FLAVOR_     |[57:32] |
116  * |_TYPE_WAKEUPS    |WAKEUPS_MONITOR     |Unused  |
117  * +-----------------------------------------------+
118  * | [31:20] Observation     | [19:0] # of wakeups |
119  * |         interval (sec)  | permitted (per sec) |
120  * +-----------------------------------------------+
121  *
122  * subcode:
123  * +-----------------------------------------------+
124  * |                         | [19:0] # of wakeups |
125  * |                         | observed (per sec)  |
126  * +-----------------------------------------------+
127  *
128  */
129 
130 #define EXC_RESOURCE_CPUMONITOR_DECODE_WAKEUPS_PERMITTED(code) \
131 	((code) & 0xFFFULL)
132 #define EXC_RESOURCE_CPUMONITOR_DECODE_OBSERVATION_INTERVAL(code) \
133 	(((code) >> 20) & 0xFFFFFULL)
134 #define EXC_RESOURCE_CPUMONITOR_DECODE_WAKEUPS_OBSERVED(subcode) \
135 	((subcode) & 0xFFFFFULL)
136 
137 /* RESOURCE_TYPE_MEMORY flavors */
138 #define FLAVOR_HIGH_WATERMARK   1       /* Indicates that the exception is due to memory limit warning */
139 #define FLAVOR_DIAG_MEMLIMIT    2       /* Indicates that the exception is due to a preset diagnostics memory consumption threshold */
140 #define FLAVOR_CONCLAVE_LIMIT   3       /* Indicates that the exception is due to the hard conclave memory limit */
141 
142 /*
143  * RESOURCE_TYPE_MEMORY / FLAVOR_HIGH_WATERMARK
144  * exception code & subcode.
145  *
146  * This is sent by the kernel when a task crosses its high
147  * watermark memory limit or when a preset memory consumption
148  * threshold is crossed.
149  *
150  * code:
151  * +------------------------------------------------+
152  * |[63:61] RESOURCE |[60:58] FLAVOR_HIGH_ |[57:32] |
153  * |_TYPE_MEMORY     |WATERMARK            |Unused  |
154  * +------------------------------------------------+
155  * |[31:16] Unused           | [15:0] HWM limit (MB)|
156  * +------------------------------------------------+
157  *
158  * subcode:
159  * +------------------------------------------------+
160  * |                                         unused |
161  * +------------------------------------------------+
162  *
163  */
164 
165 #define EXC_RESOURCE_HWM_LIMIT_MASK 0xFFFFULL
166 
167 #define EXC_RESOURCE_HWM_DECODE_LIMIT(code) \
168 	((code) & EXC_RESOURCE_HWM_LIMIT_MASK)
169 
170 /* RESOURCE_TYPE_IO flavors */
171 #define FLAVOR_IO_PHYSICAL_WRITES               1
172 #define FLAVOR_IO_LOGICAL_WRITES                2
173 
174 /*
175  * RESOURCE_TYPE_IO exception code & subcode.
176  *
177  * This is sent by the kernel when a task crosses its
178  * I/O limits.
179  *
180  * code:
181  * +-----------------------------------------------+
182  * |[63:61] RESOURCE |[60:58] FLAVOR_IO_  |[57:32] |
183  * |_TYPE_IO         |PHYSICAL/LOGICAL    |Unused  |
184  * +-----------------------------------------------+
185  * |[31:15]  Interval (sec)    | [14:0] Limit (MB) |
186  * +-----------------------------------------------+
187  *
188  * subcode:
189  * +-----------------------------------------------+
190  * |                           | [14:0] I/O Count  |
191  * |                           | (in MB)           |
192  * +-----------------------------------------------+
193  *
194  */
195 
196 /* RESOURCE_TYPE_IO decoding macros */
197 #define EXC_RESOURCE_IO_DECODE_INTERVAL(code) \
198 	(((code) >> 15) & 0x1FFFFULL)
199 #define EXC_RESOURCE_IO_DECODE_LIMIT(code) \
200 	((code) & 0x7FFFULL)
201 #define EXC_RESOURCE_IO_OBSERVED(subcode) \
202 	((subcode) & 0x7FFFULL)
203 
204 
205 /*
206  * RESOURCE_TYPE_THREADS exception code & subcode
207  *
208  * This is sent by the kernel when a task crosses its
209  * thread limit.
210  */
211 
212 #define EXC_RESOURCE_THREADS_DECODE_THREADS(code) \
213 	((code) & 0x7FFFULL)
214 
215 /* RESOURCE_TYPE_THREADS flavors */
216 #define FLAVOR_THREADS_HIGH_WATERMARK 1
217 
218 /* RESOURCE_TYPE_PORTS flavors */
219 #define FLAVOR_PORT_SPACE_FULL 1
220 
221 /*
222  * RESOURCE_TYPE_PORTS exception code & subcode.
223  *
224  * This is sent by the kernel when the process is
225  * leaking ipc ports and has filled its port space
226  *
227  * code:
228  * +-----------------------------------------------+
229  * |[63:61] RESOURCE |[60:58] FLAVOR_     |[57:32] |
230  * |_TYPE_PORTS      |PORT_SPACE_FULL      |Unused  |
231  * +-----------------------------------------------+
232  * | [31:24] Unused          | [23:0] # of ports   |
233  * |                         | allocated           |
234  * +-----------------------------------------------+
235  *
236  * subcode:
237  * +-----------------------------------------------+
238  * |                         | Unused              |
239  * |                         |                     |
240  * +-----------------------------------------------+
241  *
242  */
243 #define EXC_RESOURCE_THREADS_DECODE_PORTS(code) \
244 	((code) & 0xFFFFFFULL)
245 
246 #ifdef KERNEL
247 
248 /* EXC_RESOURCE type and flavor encoding macros */
249 #define EXC_RESOURCE_ENCODE_TYPE(code, type) \
250 	((code) |= (((uint64_t)(type) & 0x7ULL) << 61))
251 #define EXC_RESOURCE_ENCODE_FLAVOR(code, flavor) \
252 	((code) |= (((uint64_t)(flavor) & 0x7ULL) << 58))
253 
254 /* RESOURCE_TYPE_CPU::FLAVOR_CPU_MONITOR specific encoding macros */
255 #define EXC_RESOURCE_CPUMONITOR_ENCODE_INTERVAL(code, interval) \
256 	((code) |= (((uint64_t)(interval) & 0x1FFFFFFULL) << 7))
257 #define EXC_RESOURCE_CPUMONITOR_ENCODE_PERCENTAGE(code, percentage) \
258 	((code) |= (((uint64_t)(percentage) & 0x7FULL)))
259 
260 /* RESOURCE_TYPE_WAKEUPS::FLAVOR_WAKEUPS_MONITOR specific encoding macros */
261 #define EXC_RESOURCE_CPUMONITOR_ENCODE_WAKEUPS_PERMITTED(code, num) \
262 	((code) |= ((uint64_t)(num) & 0xFFFFFULL))
263 #define EXC_RESOURCE_CPUMONITOR_ENCODE_OBSERVATION_INTERVAL(code, num) \
264 	((code) |= (((uint64_t)(num) & 0xFFFULL) << 20))
265 #define EXC_RESOURCE_CPUMONITOR_ENCODE_WAKEUPS_OBSERVED(subcode, num) \
266 	((subcode) |= ((uint64_t)(num) & 0xFFFFFULL))
267 
268 /* RESOURCE_TYPE_MEMORY::FLAVOR_HIGH_WATERMARK specific encoding macros */
269 #define EXC_RESOURCE_HWM_ENCODE_LIMIT(code, num) \
270 	((code) |= ((uint64_t)(num) & EXC_RESOURCE_HWM_LIMIT_MASK))
271 
272 /* RESOURCE_TYPE_IO::FLAVOR_IO_PHYSICAL_WRITES/FLAVOR_IO_LOGICAL_WRITES specific encoding macros */
273 #define EXC_RESOURCE_IO_ENCODE_INTERVAL(code, interval) \
274 	((code) |= (((uint64_t)(interval) & 0x1FFFFULL) << 15))
275 #define EXC_RESOURCE_IO_ENCODE_LIMIT(code, limit) \
276 	((code) |= (((uint64_t)(limit) & 0x7FFFULL)))
277 #define EXC_RESOURCE_IO_ENCODE_OBSERVED(subcode, num) \
278 	((subcode) |= (((uint64_t)(num) & 0x7FFFULL)))
279 
280 /* RESOURCE_TYPE_THREADS specific encoding macros */
281 #define EXC_RESOURCE_THREADS_ENCODE_THREADS(code, threads) \
282 	((code) |= (((uint64_t)(threads) & 0x7FFFULL)))
283 
284 /* RESOURCE_TYPE_PORTS::FLAVOR_PORT_SPACE_FULL specific encoding macros */
285 #define EXC_RESOURCE_PORTS_ENCODE_PORTS(code, num) \
286 	((code) |= ((uint64_t)(num) & 0xFFFFFFULL))
287 
288 #endif /* KERNEL */
289 
290 
291 #endif /* _EXC_RESOURCE_H_ */
292