xref: /xnu-12377.61.12/bsd/sys/sysctl.h (revision 4d495c6e23c53686cf65f45067f79024cf5dcee8)
1 /*
2  * Copyright (c) 2000-2021 Apple 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 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29 /*
30  * Copyright (c) 1989, 1993
31  *	The Regents of the University of California.  All rights reserved.
32  *
33  * This code is derived from software contributed to Berkeley by
34  * Mike Karels at Berkeley Software Design, Inc.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. All advertising materials mentioning features or use of this software
45  *    must display the following acknowledgement:
46  *	This product includes software developed by the University of
47  *	California, Berkeley and its contributors.
48  * 4. Neither the name of the University nor the names of its contributors
49  *    may be used to endorse or promote products derived from this software
50  *    without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62  * SUCH DAMAGE.
63  *
64  *	@(#)sysctl.h	8.1 (Berkeley) 6/2/93
65  */
66 /*
67  * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
68  * support for mandatory and extensible security protections.  This notice
69  * is included in support of clause 2.2 (b) of the Apple Public License,
70  * Version 2.0.
71  */
72 
73 #ifndef _SYS_SYSCTL_H_
74 #define _SYS_SYSCTL_H_
75 
76 /*
77  * These are for the eproc structure defined below.
78  */
79 #include <sys/cdefs.h>
80 
81 #include <sys/appleapiopts.h>
82 #ifndef KERNEL
83 #include <sys/time.h>
84 #include <sys/ucred.h>
85 #else
86 #ifdef XNU_KERNEL_PRIVATE
87 #include <kern/startup.h>
88 #include <libkern/section_keywords.h>
89 #include <string.h>
90 #else
91 #include <libkern/sysctl.h>
92 #include <os/base.h>
93 #endif /* XNU_KERNEL_PRIVATE */
94 #endif /* KERNEL */
95 
96 #include <sys/proc.h>
97 #include <sys/vm.h>
98 
99 /*
100  * Definitions for sysctl call.  The sysctl call uses a hierarchical name
101  * for objects that can be examined or modified.  The name is expressed as
102  * a sequence of integers.  Like a file path name, the meaning of each
103  * component depends on its place in the hierarchy.  The top-level and kern
104  * identifiers are defined here, and other identifiers are defined in the
105  * respective subsystem header files.
106  */
107 
108 #define CTL_MAXNAME     12      /* largest number of components supported */
109 
110 /*
111  * Each subsystem defined by sysctl defines a list of variables
112  * for that subsystem. Each name is either a node with further
113  * levels defined below it, or it is a leaf of some particular
114  * type given below. Each sysctl level defines a set of name/type
115  * pairs to be used by sysctl(1) in manipulating the subsystem.
116  *
117  * When declaring new sysctl names, use the CTLFLAG_LOCKED flag in the
118  * type to indicate that all necessary locking will be handled
119  * within the sysctl.
120  *
121  * Any sysctl defined without CTLFLAG_LOCKED is considered legacy
122  * and will be protected by a global mutex.
123  *
124  * Note:	This is not optimal, so it is best to handle locking
125  *		yourself, if you are able to do so.  A simple design
126  *		pattern for use to avoid in a single function known
127  *		to potentially be in the paging path ot doing a DMA
128  *		to physical memory in a user space process is:
129  *
130  *			lock
131  *			perform operation vs. local buffer
132  *			unlock
133  *			SYSCTL_OUT(rey, local buffer, length)
134  *
135  *		...this assumes you are not using a deep call graph
136  *		or are unable to pass a local buffer address as a
137  *		parameter into your deep call graph.
138  *
139  *		Note that very large user buffers can fail the wire
140  *		if to do so would require more physical pages than
141  *		are available (the caller will get an ENOMEM error,
142  *		see sysctl_mem_hold() for details).
143  */
144 struct ctlname {
145 	char    *ctl_name;      /* subsystem name */
146 	int     ctl_type;       /* type of name */
147 };
148 
149 #define CTLTYPE             0xf             /* Mask for the type */
150 #define CTLTYPE_NODE        1               /* name is a node */
151 #define CTLTYPE_INT         2               /* name describes an integer */
152 #define CTLTYPE_STRING      3               /* name describes a string */
153 #define CTLTYPE_QUAD        4               /* name describes a 64-bit number */
154 #define CTLTYPE_OPAQUE      5               /* name describes a structure */
155 #define CTLTYPE_STRUCT      CTLTYPE_OPAQUE  /* name describes a structure */
156 
157 #define CTLFLAG_RD          0x80000000      /* Allow reads of variable */
158 #define CTLFLAG_WR          0x40000000      /* Allow writes to the variable */
159 #define CTLFLAG_RW          (CTLFLAG_RD|CTLFLAG_WR)
160 #define CTLFLAG_NOLOCK      0x20000000      /* XXX Don't Lock */
161 #define CTLFLAG_ANYBODY     0x10000000      /* All users can set this var */
162 #define CTLFLAG_SECURE      0x08000000      /* Permit set only if securelevel<=0 */
163 #define CTLFLAG_MASKED      0x04000000      /* deprecated variable, do not display */
164 #define CTLFLAG_NOAUTO      0x02000000      /* do not auto-register */
165 #define CTLFLAG_KERN        0x01000000      /* valid inside the kernel */
166 #define CTLFLAG_LOCKED      0x00800000      /* node will handle locking itself */
167 #define CTLFLAG_OID2        0x00400000      /* struct sysctl_oid has version info */
168 #if XNU_KERNEL_PRIVATE
169 #define CTLFLAG_PERMANENT   0x00200000      /* permanent sysctl_oid */
170 #endif
171 #define CTLFLAG_EXPERIMENT  0x00100000 /* Allows read/write w/ the trial experiment entitlement. */
172 #define CTLFLAG_LEGACY_EXPERIMENT 0x00080000 /* Allows writing w/ the legacy trial experiment entitlement. */
173 
174 /*
175  * USE THIS instead of a hardwired number from the categories below
176  * to get dynamically assigned sysctl entries using the linker-set
177  * technology. This is the way nearly all new sysctl variables should
178  * be implemented.
179  *
180  * e.g. SYSCTL_INT(_parent, OID_AUTO, name, CTLFLAG_RW, &variable, 0, "");
181  *
182  * Note that linker set technology will automatically register all nodes
183  * declared like this on kernel initialization, UNLESS they are defined
184  * in I/O-Kit. In this case, you have to call sysctl_register_oid()
185  * manually - just like in a KEXT.
186  */
187 #define OID_AUTO              (-1)
188 #if XNU_KERNEL_PRIVATE
189 /*
190  * Used to allow for most of the core kernel sysctl OIDs to be in immutable
191  * memory. The nodes that can be extensible have a fake first node with this
192  * particular oid_number which hangs a second mutable list from this node.
193  *
194  * This node is always first when it is used
195  */
196 #define OID_MUTABLE_ANCHOR    (INT_MIN)
197 #endif
198 #define OID_AUTO_START        100 /* conventional */
199 
200 #ifdef KERNEL
201 #define SYSCTL_HANDLER_ARGS \
202 	(struct sysctl_oid *oidp __unused, void *arg1 __unused, int arg2 __unused, \
203 	struct sysctl_req *req)
204 
205 
206 /*
207  * This describes the access space for a sysctl request.  This is needed
208  * so that we can use the interface from the kernel or from user-space.
209  */
210 struct sysctl_req {
211 	struct proc     *p;
212 	int             lock;
213 	user_addr_t     oldptr;         /* pointer to user supplied buffer */
214 	size_t          oldlen;         /* user buffer length (also returned) */
215 	size_t          oldidx;         /* total data iteratively copied out */
216 	int             (*oldfunc)(struct sysctl_req *, const void *, size_t);
217 	user_addr_t     newptr;         /* buffer containing new value */
218 	size_t          newlen;         /* length of new value */
219 	size_t          newidx;         /* total data iteratively copied in */
220 	int             (*newfunc)(struct sysctl_req *, void *, size_t);
221 };
222 
223 SLIST_HEAD(sysctl_oid_list, sysctl_oid);
224 
225 #define SYSCTL_OID_VERSION      1       /* current OID structure version */
226 
227 /*
228  * This describes one "oid" in the MIB tree.  Potentially more nodes can
229  * be hidden behind it, expanded by the handler.
230  *
231  * NOTES:	We implement binary comparibility between CTLFLAG_OID2 and
232  *		pre-CTLFLAG_OID2 structure in sysctl_register_oid() and in
233  *		sysctl_unregister_oid() using the fact that the fields up
234  *		to oid_fmt are unchanged, and that the field immediately
235  *		following is on an alignment boundary following a pointer
236  *		type and is also a pointer.  This lets us get the previous
237  *		size of the structure, and the copy-cut-off point, using
238  *		the offsetof() language primitive, and these values  are
239  *		used in conjunction with the fact that earlier and future
240  *		statically compiled sysctl_oid structures are declared via
241  *		macros.  This lets us overload the macros so that the addition
242  *		of the CTLFLAG_OID2 in newly compiled code containing sysctl
243  *		node declarations, subsequently allowing us to to avoid
244  *		changing the KPI used for non-static (un)registration in
245  *		KEXTs.
246  *
247  *		Non CTLFLAG_OID2 based sysctls are deprecated and unavailable
248  *		to non Intel platforms.
249  *
250  *		This depends on the fact that people declare SYSCTLs,
251  *		rather than declaring sysctl_oid structures.  All new code
252  *		should avoid declaring struct sysctl_oid's directly without
253  *		the macros; the current risk for this is limited to losing
254  *		your description field and ending up with a malloc'ed copy,
255  *		as if it were a legacy binary static declaration via SYSCTL;
256  *		in the future, we may deprecate access to a named structure
257  *		type in third party code.  Use the macros, or our code will
258  *		end up with compile errors when that happens.
259  *
260  *		Please try to include a long description of the field in any
261  *		new sysctl declarations (all the macros support this).  This
262  *		field may be the only human readable documentation your users
263  *		get for your sysctl.
264  */
265 struct sysctl_oid {
266 	struct sysctl_oid_list * OS_PTRAUTH_SIGNED_PTR("sysctl_oid.oid_parent") oid_parent;
267 	SLIST_ENTRY(sysctl_oid) oid_link;
268 	int             oid_number;
269 	int             oid_kind;
270 	void            *oid_arg1;
271 	int             oid_arg2;
272 	const char      *oid_name;
273 	int             (*oid_handler)SYSCTL_HANDLER_ARGS;
274 	const char      *oid_fmt;
275 	const char      *oid_descr; /* offsetof() field / long description */
276 	int             oid_version;
277 	int             oid_refcnt;
278 };
279 
280 #define SYSCTL_IN(r, p, l) (r->newfunc)(r, p, l)
281 #define SYSCTL_OUT(r, p, l) (r->oldfunc)(r, p, l)
282 
283 typedef int (* sysctl_handler_t) SYSCTL_HANDLER_ARGS;
284 
285 __BEGIN_DECLS
286 
287 /* old interface */
288 int sysctl_handle_int SYSCTL_HANDLER_ARGS;
289 int sysctl_handle_long SYSCTL_HANDLER_ARGS;
290 int sysctl_handle_quad SYSCTL_HANDLER_ARGS;
291 int sysctl_handle_int2quad SYSCTL_HANDLER_ARGS;
292 int sysctl_handle_string SYSCTL_HANDLER_ARGS;
293 int sysctl_handle_opaque SYSCTL_HANDLER_ARGS;
294 /* new interface */
295 int sysctl_io_number(struct sysctl_req *req, long long bigValue, size_t valueSize, void *pValue, int *changed);
296 int sysctl_io_string(struct sysctl_req *req, char *pValue, size_t valueSize, int trunc, int *changed);
297 int sysctl_io_opaque(struct sysctl_req *req, void *pValue, size_t valueSize, int *changed);
298 
299 /*
300  * These functions are used to add/remove an oid from the mib.
301  */
302 void sysctl_register_oid(struct sysctl_oid *oidp);
303 void sysctl_unregister_oid(struct sysctl_oid *oidp);
304 
305 #define nvram_osenvironment "osenvironment"
306 void sysctl_set_osenvironment(unsigned int size, const void* value);
307 void sysctl_unblock_osenvironment(void);
308 
309 /* Deprecated */
310 void sysctl_register_fixed(void) __deprecated;
311 
312 __END_DECLS
313 
314 /* Declare an oid to allow child oids to be added to it. */
315 #define SYSCTL_DECL(name)                                       \
316 	extern struct sysctl_oid_list sysctl_##name##_children
317 
318 /*
319  * Macros to define sysctl entries.  Which to use?  Pure data that are
320  * returned without modification, SYSCTL_<data type> is for you, like
321  * SYSCTL_QUAD for a 64-bit value.  When you want to run a handler of your
322  * own, SYSCTL_PROC.
323  *
324  * parent:	parent in name hierarchy (e.g. _kern for "kern")
325  * nbr:		ID.  Almost certainly OID_AUTO ("pick one for me") for you.
326  * name:	name for this particular item (e.g. "thesysctl" for "kern.thesysctl")
327  * kind/access: Control flags (CTLFLAG_*).  Some notable options include:
328  *                      CTLFLAG_ANYBODY:        non-root users allowed
329  *                      CTLFLAG_MASKED:	        don't show in sysctl listing in userland
330  *                      CTLFLAG_LOCKED:		does own locking (no additional protection needed)
331  *                      CTLFLAG_KERN:		valid inside kernel (best avoided generally)
332  *                      CTLFLAG_WR:		"new" value accepted
333  * a1, a2:	entry-data, passed to handler (see specific macros)
334  * Format String: Tells "sysctl" tool how to print data from this entry.
335  *	                "A" - string
336  *	                "I" - list of integers. "IU" - list of unsigned integers. space-separated.
337  *	                "-" - do not print
338  *	                "L" - longs, as ints with I
339  *			"P" - pointer
340  *                      "Q" - quads
341  *                      "S","T" - clock info, see sysctl.c in system_cmds (you probably don't need this)
342  * Description: unused
343  */
344 
345 
346 /* This constructs a "raw" MIB oid. */
347 #define SYSCTL_STRUCT_INIT(parent, nbr, name, kind, a1, a2, fn, fmt, desc) {    \
348 	    .oid_parent     = &sysctl_##parent##_children,                      \
349 	    .oid_number     = nbr,                                              \
350 	    .oid_kind       = (int)(kind | CTLFLAG_OID2),                       \
351 	    .oid_arg1       = a1,                                               \
352 	    .oid_arg2       = (int)(a2),                                        \
353 	    .oid_name       = #name,                                            \
354 	    .oid_handler    = fn,                                               \
355 	    .oid_fmt        = fmt,                                              \
356 	    .oid_descr      = desc,                                             \
357 	    .oid_version    = SYSCTL_OID_VERSION,                               \
358 	}
359 
360 #define __SYSCTL_OID(parent, nbr, name, kind, a1, a2, handler, fmt, descr) \
361 	struct sysctl_oid sysctl_##parent##_##name = SYSCTL_STRUCT_INIT(\
362 	    parent, nbr, name, kind, a1, a2, handler, fmt, descr)
363 
364 #if XNU_KERNEL_PRIVATE
365 
366 /*
367  * Core kernel registers sysctls before lockdown and protects those entries
368  * in immutable memory.
369  *
370  * When a node needs to support dynamic extension after lockdown, it needs to be
371  * declared with SYSCTL_EXTENSIBLE_NODE() to insert a dummy "OID_MUTABLE_ANCHOR"
372  * node in this node chain which will allow extensibility.
373  *
374  * OIDs that are to be inserted dynamically based on system properties that
375  * aren't known at compile time, have three options, in increasing order of
376  * unsafety:
377  *
378  * - The OID can use the CTLFLAG_NOAUTO flag. Such entries aren't inserted to
379  *   the sysctl tree automatically but will be made read-only at lock down.
380  *
381  *   Such entries must be inserted in the STARTUP_SUB_SYSCTL "Middle" phase
382  *   using sysctl_register_oid_early().
383  *
384  * - The OID can be always registered and test whether it is ready to operate.
385  *   When it is not, it must return ENOENT which simulates an absent entry.
386  *
387  *   This however has the downside that the entry is still resolvable as an MIB
388  *   or listed in `sysctl -a` when it isn't masked.
389  *
390  *   This is acceptable for sysctls that will become valid quickly during boot
391  *   (but after lockdown).
392  *
393  * - SYSCTL_OID_MANUAL / SYSCTL_NODE_MANUAL can be used for completely
394  *   dynamic/manual oid registration. Such nodes must be registered with
395  *   sysctl_register_oid() after lockdown.
396  *
397  *   This is the least preferred solution.
398  */
399 
400 __BEGIN_DECLS
401 void sysctl_register_oid_early(struct sysctl_oid *oidp);
402 __END_DECLS
403 
404 #define SYSCTL_OID_MANUAL(parent, nbr, name, kind, a1, a2, handler, fmt, descr) \
405 	__XNU_PRIVATE_EXTERN                                                    \
406 	__SYSCTL_OID(parent, nbr, name, kind, a1, a2, handler, fmt, descr)
407 
408 #define SYSCTL_NODE_MANUAL(parent, nbr, name, access, handler, descr)           \
409 	struct sysctl_oid_list sysctl_##parent##_##name##_children;             \
410 	__XNU_PRIVATE_EXTERN                                                    \
411 	__SYSCTL_OID(parent, nbr, name, CTLTYPE_NODE|access,                    \
412 	    &sysctl_##parent##_##name##_children, 0, handler, "N", descr);
413 
414 #define SYSCTL_OID(parent, nbr, name, kind, a1, a2, handler, fmt, descr)        \
415 	__security_const_late __XNU_PRIVATE_EXTERN                              \
416 	__SYSCTL_OID(parent, nbr, name, CTLFLAG_PERMANENT|kind,                 \
417 	    a1, a2, handler, fmt, descr);                                       \
418 	__STARTUP_ARG(sysctl_##parent, _##name,                                 \
419 	    SYSCTL, STARTUP_RANK_SECOND, sysctl_register_oid_early,             \
420 	    &sysctl_##parent##_##name)
421 
422 #define __SYSCTL_NODE(parent, nbr, name, access, handler, descr)                \
423 	__security_const_late                                                   \
424 	struct sysctl_oid_list sysctl_##parent##_##name##_children;             \
425 	__security_const_late __XNU_PRIVATE_EXTERN                              \
426 	__SYSCTL_OID(parent, nbr, name, CTLFLAG_PERMANENT|CTLTYPE_NODE|access,  \
427 	    &sysctl_##parent##_##name##_children, 0, handler, "N", descr);      \
428 	__STARTUP_ARG(sysctl_##parent, _##name,                                 \
429 	    SYSCTL, STARTUP_RANK_FIRST, sysctl_register_oid_early,              \
430 	    &sysctl_##parent##_##name)
431 
432 #define __SYSCTL_EXTENSION_NODE(name)                                           \
433 	static __security_read_write                                            \
434 	struct sysctl_oid_list sysctl_##name##_children_mutable;                \
435 	static __security_const_late                                            \
436 	struct sysctl_oid sysctl_##name##_wranchor = {                          \
437 	    .oid_parent     = &sysctl_##name##_children,                        \
438 	    .oid_number     = OID_MUTABLE_ANCHOR,                               \
439 	    .oid_kind       = CTLFLAG_OID2 | CTLFLAG_PERMANENT,                 \
440 	    .oid_arg1       = &sysctl_##name##_children_mutable,                \
441 	    .oid_name       = "__anchor__(" #name ")",                          \
442 	    .oid_version    = SYSCTL_OID_VERSION,                               \
443 	};                                                                      \
444 	__STARTUP_ARG(sysctl_##name, _wranchor,                                 \
445 	    SYSCTL, STARTUP_RANK_LAST, sysctl_register_oid_early,               \
446 	    &sysctl_##name##_wranchor)
447 
448 #define SYSCTL_NODE(parent, nbr, name, access, handler, descr)                  \
449 	__XNU_PRIVATE_EXTERN                                                    \
450 	__SYSCTL_NODE(parent, nbr, name, access, handler, descr)
451 
452 #define SYSCTL_EXTENSIBLE_NODE(parent, nbr, name, access, handler, descr)       \
453 	__SYSCTL_NODE(parent, nbr, name, access, handler, descr);               \
454 	__SYSCTL_EXTENSION_NODE(parent##_##name)
455 #else
456 #define SYSCTL_OID(parent, nbr, name, kind, a1, a2, handler, fmt, descr) \
457 	__SYSCTL_OID(parent, nbr, name, kind, a1, a2, handler, fmt, descr)
458 
459 /* This constructs a node from which other oids can hang. */
460 #define SYSCTL_NODE(parent, nbr, name, access, handler, descr)                  \
461 	struct sysctl_oid_list sysctl_##parent##_##name##_children;             \
462 	SYSCTL_OID(parent, nbr, name, CTLTYPE_NODE|access,                      \
463 	    &sysctl_##parent##_##name##_children, 0, handler, "N", descr)
464 #endif /* XNU_KERNEL_PRIVATE */
465 
466 /* Oid for a string.  len can be 0 to indicate '\0' termination. */
467 #define SYSCTL_STRING(parent, nbr, name, access, arg, len, descr) \
468 	SYSCTL_OID(parent, nbr, name, CTLTYPE_STRING|access, \
469 	    arg, len, sysctl_handle_string, "A", descr)
470 
471 #define SYSCTL_COMPAT_INT(parent, nbr, name, access, ptr, val, descr) \
472 	SYSCTL_OID(parent, nbr, name, CTLTYPE_INT|access, \
473 	    ptr, val, sysctl_handle_int, "I", descr)
474 
475 #define SYSCTL_COMPAT_UINT(parent, nbr, name, access, ptr, val, descr) \
476 	SYSCTL_OID(parent, nbr, name, CTLTYPE_INT|access, \
477 	    ptr, val, sysctl_handle_int, "IU", descr)
478 
479 /* Oid for an int.  If ptr is NULL, val is returned. */
480 #define SYSCTL_INT(parent, nbr, name, access, ptr, val, descr) \
481 	SYSCTL_OID(parent, nbr, name, CTLTYPE_INT|access, \
482 	    ptr, val, sysctl_handle_int, "I", descr); \
483 	_Static_assert(__builtin_constant_p(ptr) || sizeof(*(ptr)) == sizeof(int), \
484 	    "must be integer sized");
485 
486 /* Oid for an unsigned int.  If ptr is NULL, val is returned. */
487 #define SYSCTL_UINT(parent, nbr, name, access, ptr, val, descr) \
488 	SYSCTL_OID(parent, nbr, name, CTLTYPE_INT|access, \
489 	    ptr, val, sysctl_handle_int, "IU", descr); \
490 	_Static_assert(__builtin_constant_p(ptr) || sizeof(*(ptr)) == sizeof(unsigned int), \
491 	    "must be integer sized");
492 
493 /* Oid for a long.  The pointer must be non NULL. */
494 #define SYSCTL_LONG(parent, nbr, name, access, ptr, descr) \
495 	SYSCTL_OID(parent, nbr, name, CTLTYPE_INT|access, \
496 	    ptr, 0, sysctl_handle_long, "L", descr); \
497 	_Static_assert(__builtin_constant_p(ptr) || sizeof(*(ptr)) == sizeof(long), \
498 	    "must be long sized");
499 
500 /* Oid for a unsigned long.  The pointer must be non NULL. */
501 #define SYSCTL_ULONG(parent, nbr, name, access, ptr, descr) \
502 	SYSCTL_OID(parent, nbr, name, CTLTYPE_INT|access, \
503 	    ptr, 0, sysctl_handle_long, "LU", descr); \
504 	_Static_assert(__builtin_constant_p(ptr) || sizeof(*(ptr)) == sizeof(unsigned long), \
505 	    "must be long sized");
506 
507 /* Oid for a quad.  The pointer must be non NULL. */
508 #define SYSCTL_QUAD(parent, nbr, name, access, ptr, descr) \
509 	SYSCTL_OID(parent, nbr, name, CTLTYPE_QUAD|access, \
510 	    ptr, 0, sysctl_handle_quad, "Q", descr); \
511 	_Static_assert(__builtin_constant_p(ptr) || sizeof(*(ptr)) == sizeof(long long), \
512 	    "must be long long sized");
513 
514 /* Oid for an opaque object.  Specified by a pointer and a length. */
515 #define SYSCTL_OPAQUE(parent, nbr, name, access, ptr, len, fmt, descr) \
516 	SYSCTL_OID(parent, nbr, name, CTLTYPE_OPAQUE|access, \
517 	        ptr, len, sysctl_handle_opaque, fmt, descr)
518 
519 /* Oid for a struct.  Specified by a pointer and a type. */
520 #define SYSCTL_STRUCT(parent, nbr, name, access, ptr, type, descr) \
521 	SYSCTL_OID(parent, nbr, name, CTLTYPE_OPAQUE|access, \
522 	    ptr, sizeof(struct type), sysctl_handle_opaque, \
523 	    "S," #type, descr)
524 
525 /*
526  * Oid for a procedure.  Specified by a pointer and an arg.
527  * CTLTYPE_* macros can determine how the "sysctl" tool deals with
528  * input (e.g. converting to int).
529  */
530 #define SYSCTL_PROC(parent, nbr, name, access, ptr, arg, handler, fmt, descr) \
531 	SYSCTL_OID(parent, nbr, name, access, \
532 	    ptr, arg, handler, fmt, descr)
533 
534 #pragma mark Trial Experiments
535 
536 /*
537  * The EXPERIMENT macros below expose values for on-device experimentation (A/B testing) via Trial.
538  * These values will be set shortly after boot by triald based on any active experiments on the
539  * device. Values exposed via these macros are still normal sysctls and can be set by the
540  * superuser in the development or debug kernel. However, on the release kernel they can ONLY be
541  * set by processes with the com.apple.private.kernel.read-write-trial-experiment-factors
542  * entitlement.
543  *
544  * For numeric types, special macros are provided that enforce a valid range for the value
545  * (inclusive) to ensure that an errant experiment can't set a totally unexpected value. These
546  * macros also track which values have been modified via sycstl(3) so that they can be inspected
547  * with the showexperiments lldb macro.
548  */
549 
550 struct experiment_spec {
551 	void *ptr; /* ptr to numeric experiment factor. */
552 	uint64_t min_value; /* Min value that can be set via sysctl(3) (inclusive). */
553 	uint64_t max_value; /* Max value that can be set via sysctl(3) (inclusive). */
554 	uint64_t original_value; /* First value that was overwritten via sysctl(3). */
555 	_Atomic bool modified; /* Has this value ever been overwritten via sysctl(3)? */
556 };
557 
558 /*
559  * The handlers for the numeric types can be easily parameterized by type.
560  * So they're defined via an X macro.
561  */
562 #define experiment_factor_numeric_types \
563     X(uint, unsigned int) \
564     X(int, int) \
565     X(ulong, unsigned long) \
566     X(long, long) \
567     X(uint64, uint64_t) \
568     X(int64, int64_t)
569 
570 #define X(experiment_factor_typename, _) \
571 int experiment_factor_##experiment_factor_typename##_handler SYSCTL_HANDLER_ARGS;
572 
573 experiment_factor_numeric_types
574 #undef X
575 
576 #define __EXPERIMENT_FACTOR_SPEC(name, p, min, max) \
577 	struct experiment_spec _experiment_##name = { \
578 	        .ptr = p, \
579 	        .min_value = min, \
580 	        .max_value = max, \
581 	        .original_value = 0, \
582 	        .modified = false \
583 	}
584 
585 #define EXPERIMENT_FACTOR_UINT(name, ptr, min, max, descr) \
586 	__EXPERIMENT_FACTOR_SPEC(name, ptr, min, max); \
587 	_Static_assert(sizeof(*(ptr)) == sizeof(unsigned int), "must be integer sized"); \
588 	SYSCTL_PROC(_kern_trial, OID_AUTO, name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_EXPERIMENT, &_experiment_##name, 1, &experiment_factor_uint_handler, "IU", descr);
589 
590 #define EXPERIMENT_FACTOR_INT(name, ptr, min, max, descr) \
591 	__EXPERIMENT_FACTOR_SPEC(name, ptr, min, max); \
592 	_Static_assert(sizeof(*(ptr)) == sizeof(int), "must be integer sized"); \
593 	SYSCTL_PROC(_kern_trial, OID_AUTO, name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_EXPERIMENT, &_experiment_##name, 1, &experiment_factor_int_handler, "I", descr);
594 
595 #define EXPERIMENT_FACTOR_ULONG(name, ptr, min, max, descr) \
596 	__EXPERIMENT_FACTOR_SPEC(name, ptr, min, max); \
597 	_Static_assert(sizeof(*(ptr)) == sizeof(unsigned long), "must be long sized"); \
598 	SYSCTL_PROC(_kern_trial, OID_AUTO, name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_EXPERIMENT, &_experiment_##name, 1, &experiment_factor_ulong_handler, "LU", descr);
599 
600 #define EXPERIMENT_FACTOR_LONG(name, ptr, min, max, descr) \
601 	__EXPERIMENT_FACTOR_SPEC(name, ptr, min, max); \
602 	_Static_assert(sizeof(*(ptr)) == sizeof(long), "must be long sized"); \
603 	SYSCTL_PROC(_kern_trial, OID_AUTO, name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_EXPERIMENT, &_experiment_##name, 1, &experiment_factor_long_handler, "L", descr);
604 
605 #define EXPERIMENT_FACTOR_UINT64(name, ptr, min, max, descr) \
606 	__EXPERIMENT_FACTOR_SPEC(name, ptr, min, max); \
607 	_Static_assert(sizeof(*(ptr)) == sizeof(uint64_t), "must be 8 bytes"); \
608 	SYSCTL_PROC(_kern_trial, OID_AUTO, name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_EXPERIMENT, &_experiment_##name, 1, &experiment_factor_uint64_handler, "QU", descr);
609 
610 #define EXPERIMENT_FACTOR_INT64(name, ptr, min, max, descr) \
611 	__EXPERIMENT_FACTOR_SPEC(name, ptr, min, max); \
612 	_Static_assert(sizeof(*(ptr)) == sizeof(int64_t), "must be 8 bytes"); \
613 	SYSCTL_PROC(_kern_trial, OID_AUTO, name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_EXPERIMENT, &_experiment_##name, 1, &experiment_factor_int64_handler, "Q", descr);
614 
615 /*
616  * Calls an user provided handler to read / write this factor.
617  * Entitlement checking will still be done by sysctl, but it's the callers responsibility to validate any new values.
618  * This factor will not be printed out via the showexperiments lldb macro.
619  */
620 #define EXPERIMENT_FACTOR_PROC(access, ptr, arg, handler, fmt, descr) \
621 	_Static_assert(arg != 1, "arg can not be 1"); \
622 	SYSCTL_PROC(_kern_trial, OID_AUTO, name, access | CTLFLAG_ANYBODY | CTLFLAG_EXPERIMENT, ptr, arg, handler, fmt, descr);
623 
624 /* Legacy factors */
625 
626 #define __EXPERIMENT_FACTOR_LEGACY_SPEC(parent, name, p, min, max) \
627 	struct experiment_spec experiment_##parent##_##name = { \
628 	        .ptr = p, \
629 	        .min_value = min, \
630 	        .max_value = max, \
631 	        .original_value = 0, \
632 	        .modified = false \
633 	}
634 
635 #define EXPERIMENT_FACTOR_LEGACY_UINT(parent, name, ptr, min, max, descr) \
636 	__EXPERIMENT_FACTOR_LEGACY_SPEC(parent, name, ptr, min, max); \
637 	_Static_assert(sizeof(*(ptr)) == sizeof(unsigned int), "must be integer sized"); \
638 	SYSCTL_PROC(parent, OID_AUTO, name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_LEGACY_EXPERIMENT, &experiment_##parent##_##name, 1, &experiment_factor_uint_handler, "IU", descr);
639 
640 #define EXPERIMENT_FACTOR_LEGACY_INT(parent, name, ptr, min, max, descr) \
641 	__EXPERIMENT_FACTOR_LEGACY_SPEC(parent, name, ptr, min, max); \
642 	_Static_assert(sizeof(*(ptr)) == sizeof(int), "must be integer sized"); \
643 	SYSCTL_PROC(parent, OID_AUTO, name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_LEGACY_EXPERIMENT, &experiment_##parent##_##name, 1, &experiment_factor_int_handler, "I", descr);
644 
645 #define EXPERIMENT_FACTOR_LEGACY_ULONG(parent, name, ptr, min, max, descr) \
646 	__EXPERIMENT_FACTOR_LEGACY_SPEC(parent, name, ptr, min, max); \
647 	_Static_assert(sizeof(*(ptr)) == sizeof(unsigned long), "must be long sized"); \
648 	SYSCTL_PROC(parent, OID_AUTO, name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_LEGACY_EXPERIMENT, &experiment_##parent##_##name, 1, &experiment_factor_ulong_handler, "LU", descr);
649 
650 #define EXPERIMENT_FACTOR_LEGACY_LONG(parent, name, ptr, min, max, descr) \
651 	__EXPERIMENT_FACTOR_LEGACY_SPEC(parent, name, ptr, min, max); \
652 	_Static_assert(sizeof(*(ptr)) == sizeof(long), "must be long sized"); \
653 	SYSCTL_PROC(parent, OID_AUTO, name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_LEGACY_EXPERIMENT, &experiment_##parent##_##name, 1, &experiment_factor_long_handler, "L", descr);
654 
655 #define EXPERIMENT_FACTOR_LEGACY_UINT64(parent, name, ptr, min, max, descr) \
656 	__EXPERIMENT_FACTOR_LEGACY_SPEC(parent, name, ptr, min, max); \
657 	_Static_assert(sizeof(*(ptr)) == sizeof(uint64_t), "must be 8 bytes"); \
658 	SYSCTL_PROC(parent, OID_AUTO, name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_LEGACY_EXPERIMENT, &experiment_##parent##_##name, 1, &experiment_factor_uint64_handler, "QU", descr);
659 
660 #define EXPERIMENT_FACTOR_LEGACY_INT64(parent, name, ptr, min, max, descr) \
661 	__EXPERIMENT_FACTOR_LEGACY_SPEC(parent, name, ptr, min, max); \
662 	_Static_assert(sizeof(*(ptr)) == sizeof(int64_t), "must be 8 bytes"); \
663 	SYSCTL_PROC(parent, OID_AUTO, name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_LEGACY_EXPERIMENT, &experiment_##parent##_##name, 1, &experiment_factor_int64_handler, "Q", descr);
664 
665 /*
666  * Calls an user provided handler to read / write this factor.
667  * Entitlement checking will still be done by sysctl, but it's the callers responsibility to validate any new values.
668  * This factor will not be printed out via the showexperiments lldb macro.
669  */
670 #define EXPERIMENT_FACTOR_LEGACY_PROC(parent, name, access, ptr, arg, handler, fmt, descr) \
671 	_Static_assert(arg != 1, "arg can not be 1"); \
672 	SYSCTL_PROC(parent, OID_AUTO, name, access | CTLFLAG_ANYBODY | CTLFLAG_LEGACY_EXPERIMENT, ptr, arg, handler, fmt, descr);
673 
674 #ifdef XNU_KERNEL_PRIVATE
675 /*
676  * Sysctl handler for reading a simple counter.
677  * Using this directly is not recommended. Use the SYSCTL_SCALABLE_COUNTER macro
678  */
679 int scalable_counter_sysctl_handler SYSCTL_HANDLER_ARGS;
680 
681 /*!
682  * @macro SYSCTL_SCALABLE_COUNTER
683  *
684  * @abstract
685  * Provides a sysctl for reading the value of a percpu counter.
686  */
687 #define SYSCTL_SCALABLE_COUNTER(parent, name, counter, descr) \
688 SYSCTL_PROC(parent, OID_AUTO, name, CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED, \
689     (void *)(&counter), 0, &scalable_counter_sysctl_handler, "Q", descr);
690 #endif /* XNU_KERNEL_PRIVATE */
691 
692 extern struct sysctl_oid_list sysctl__children;
693 SYSCTL_DECL(_kern);
694 SYSCTL_DECL(_sysctl);
695 SYSCTL_DECL(_vm);
696 SYSCTL_DECL(_vfs);
697 SYSCTL_DECL(_net);
698 SYSCTL_DECL(_debug);
699 SYSCTL_DECL(_hw);
700 SYSCTL_DECL(_machdep);
701 SYSCTL_DECL(_user);
702 #if DEVELOPMENT || DEBUG
703 SYSCTL_DECL(_debug_test);
704 #endif /* DEVELOPMENT || DEBUG */
705 
706 #ifdef PRIVATE
707 SYSCTL_DECL(_kern_bridge);
708 SYSCTL_DECL(_hw_features);
709 SYSCTL_DECL(_kern_trial);
710 #endif
711 
712 #if BSD_KERNEL_PRIVATE
713 SYSCTL_DECL(_kern_memorystatus);
714 #endif
715 
716 #if defined(BSD_KERNEL_PRIVATE) && SKYWALK
717 #include <skywalk/os_sysctls_private.h>
718 #endif  /* defined(BSD_KERNEL_PRIVATE) && SKYWALK */
719 
720 #ifndef SYSCTL_SKMEM_UPDATE_FIELD
721 
722 #define SYSCTL_SKMEM 0
723 #define SYSCTL_SKMEM_UPDATE_FIELD(field, value)
724 #define SYSCTL_SKMEM_UPDATE_AT_OFFSET(offset, value)
725 #define SYSCTL_SKMEM_INT(parent, oid, sysctl_name, access, ptr, offset, descr) \
726 	SYSCTL_INT(parent, oid, sysctl_name, access, ptr, 0, descr)
727 
728 #define SYSCTL_SKMEM_TCP_INT(oid, sysctl_name, access, variable_type,   \
729 	    variable_name, initial_value, descr)           \
730 	variable_type variable_name = initial_value;                                            \
731 	SYSCTL_SKMEM_INT(_net_inet_tcp, oid, sysctl_name, access,                       \
732 	                                 &variable_name, 0, descr)
733 
734 #else /* SYSCTL_SKMEM_UPDATE_FIELD */
735 #define SYSCTL_SKMEM 1
736 #endif /* SYSCTL_SKMEM_UPDATE_FIELD */
737 
738 
739 
740 #endif /* KERNEL */
741 
742 #ifdef XNU_KERNEL_PRIVATE
743 #define SYSCTL_DEF_ENABLED
744 #else
745 #ifndef KERNEL
746 #define SYSCTL_DEF_ENABLED
747 #endif
748 #endif
749 
750 #ifdef SYSCTL_DEF_ENABLED
751 /*
752  * Top-level identifiers
753  */
754 #define CTL_UNSPEC      0               /* unused */
755 #define CTL_KERN        1               /* "high kernel": proc, limits */
756 #define CTL_VM          2               /* virtual memory */
757 #define CTL_VFS         3               /* file system, mount type is next */
758 #define CTL_NET         4               /* network, see socket.h */
759 #define CTL_DEBUG       5               /* debugging parameters */
760 #define CTL_HW          6               /* generic cpu/io */
761 #define CTL_MACHDEP     7               /* machine dependent */
762 #define CTL_USER        8               /* user-level */
763 #define CTL_MAXID       9               /* number of valid top-level ids */
764 
765 #define CTL_NAMES { \
766 	{ 0, 0 }, \
767 	{ "kern", CTLTYPE_NODE }, \
768 	{ "vm", CTLTYPE_NODE }, \
769 	{ "vfs", CTLTYPE_NODE }, \
770 	{ "net", CTLTYPE_NODE }, \
771 	{ "debug", CTLTYPE_NODE }, \
772 	{ "hw", CTLTYPE_NODE }, \
773 	{ "machdep", CTLTYPE_NODE }, \
774 	{ "user", CTLTYPE_NODE }, \
775 }
776 
777 /*
778  * CTL_KERN identifiers
779  */
780 #define KERN_OSTYPE              1      /* string: system version */
781 #define KERN_OSRELEASE           2      /* string: system release */
782 #define KERN_OSREV               3      /* int: system revision */
783 #define KERN_VERSION             4      /* string: compile time info */
784 #define KERN_MAXVNODES           5      /* int: max vnodes */
785 #define KERN_MAXPROC             6      /* int: max processes */
786 #define KERN_MAXFILES            7      /* int: max open files */
787 #define KERN_ARGMAX              8      /* int: max arguments to exec */
788 #define KERN_SECURELVL           9      /* int: system security level */
789 #define KERN_HOSTNAME           10      /* string: hostname */
790 #define KERN_HOSTID             11      /* int: host identifier */
791 #define KERN_CLOCKRATE          12      /* struct: struct clockrate */
792 #define KERN_VNODE              13      /* struct: vnode structures */
793 #define KERN_PROC               14      /* struct: process entries */
794 #define KERN_FILE               15      /* struct: file entries */
795 #define KERN_PROF               16      /* node: kernel profiling info */
796 #define KERN_POSIX1             17      /* int: POSIX.1 version */
797 #define KERN_NGROUPS            18      /* int: # of supplemental group ids */
798 #define KERN_JOB_CONTROL        19      /* int: is job control available */
799 #define KERN_SAVED_IDS          20      /* int: saved set-user/group-ID */
800 #define KERN_BOOTTIME           21      /* struct: time kernel was booted */
801 #define KERN_NISDOMAINNAME      22      /* string: YP domain name */
802 #define KERN_DOMAINNAME         KERN_NISDOMAINNAME
803 #define KERN_MAXPARTITIONS      23      /* int: number of partitions/disk */
804 #define KERN_KDEBUG             24      /* int: kernel trace points */
805 #define KERN_UPDATEINTERVAL     25      /* int: update process sleep time */
806 #define KERN_OSRELDATE          26      /* int: OS release date */
807 #define KERN_NTP_PLL            27      /* node: NTP PLL control */
808 #define KERN_BOOTFILE           28      /* string: name of booted kernel */
809 #define KERN_MAXFILESPERPROC    29      /* int: max open files per proc */
810 #define KERN_MAXPROCPERUID      30      /* int: max processes per uid */
811 #define KERN_DUMPDEV            31      /* dev_t: device to dump on */
812 #define KERN_IPC                32      /* node: anything related to IPC */
813 #define KERN_DUMMY              33      /* unused */
814 #define KERN_PS_STRINGS 34      /* int: address of PS_STRINGS */
815 #define KERN_USRSTACK32 35      /* int: address of USRSTACK */
816 #define KERN_LOGSIGEXIT 36      /* int: do we log sigexit procs? */
817 #define KERN_SYMFILE            37      /* string: kernel symbol filename */
818 #define KERN_PROCARGS           38
819 /* 39 was KERN_PCSAMPLES... now obsolete */
820 #define KERN_NETBOOT            40      /* int: are we netbooted? 1=yes,0=no */
821 /* 41 was KERN_PANICINFO : panic UI information (deprecated) */
822 #define KERN_SYSV               42      /* node: System V IPC information */
823 #define KERN_AFFINITY           43      /* xxx */
824 #define KERN_TRANSLATE          44      /* xxx */
825 #define KERN_CLASSIC            KERN_TRANSLATE  /* XXX backwards compat */
826 #define KERN_EXEC               45      /* xxx */
827 #define KERN_CLASSICHANDLER     KERN_EXEC /* XXX backwards compatibility */
828 #define KERN_AIOMAX             46      /* int: max aio requests */
829 #define KERN_AIOPROCMAX         47      /* int: max aio requests per process */
830 #define KERN_AIOTHREADS         48      /* int: max aio worker threads */
831 #ifdef __APPLE_API_UNSTABLE
832 #define KERN_PROCARGS2          49
833 #endif /* __APPLE_API_UNSTABLE */
834 #define KERN_COREFILE           50      /* string: corefile format string */
835 #define KERN_COREDUMP           51      /* int: whether to coredump at all */
836 #define KERN_SUGID_COREDUMP     52      /* int: whether to dump SUGID cores */
837 #define KERN_PROCDELAYTERM      53      /* int: set/reset current proc for delayed termination during shutdown */
838 #define KERN_SHREG_PRIVATIZABLE 54      /* int: can shared regions be privatized ? */
839 /* 55 was KERN_PROC_LOW_PRI_IO... now deprecated */
840 #define KERN_LOW_PRI_WINDOW     56      /* int: set/reset throttle window - milliseconds */
841 #define KERN_LOW_PRI_DELAY      57      /* int: set/reset throttle delay - milliseconds */
842 #define KERN_POSIX              58      /* node: posix tunables */
843 #define KERN_USRSTACK64         59      /* LP64 user stack query */
844 #define KERN_NX_PROTECTION      60      /* int: whether no-execute protection is enabled */
845 #define KERN_TFP                61      /* Task for pid settings */
846 #define KERN_PROCNAME           62      /* setup process program  name(2*MAXCOMLEN) */
847 #define KERN_THALTSTACK         63      /* for compat with older x86 and does nothing */
848 #define KERN_SPECULATIVE_READS  64      /* int: whether speculative reads are disabled */
849 #define KERN_OSVERSION          65      /* for build number i.e. 9A127 */
850 #define KERN_SAFEBOOT           66      /* are we booted safe? */
851 /*	67 was KERN_LCTX (login context) */
852 #define KERN_RAGEVNODE          68
853 #define KERN_TTY                69      /* node: tty settings */
854 #define KERN_CHECKOPENEVT       70      /* spi: check the VOPENEVT flag on vnodes at open time */
855 #define KERN_THREADNAME         71      /* set/get thread name */
856 #define KERN_MAXID              72      /* number of valid kern ids */
857 /*
858  * Don't add any more sysctls like this.  Instead, use the SYSCTL_*() macros
859  * and OID_AUTO. This will have the added benefit of not having to recompile
860  * sysctl(8) to pick up your changes.
861  */
862 
863 #if COUNT_SYSCALLS && defined(KERNEL)
864 #define KERN_COUNT_SYSCALLS (KERN_OSTYPE + 1000)        /* keep called count for each bsd syscall */
865 #endif
866 
867 #if defined(__LP64__)
868 #define KERN_USRSTACK KERN_USRSTACK64
869 #else
870 #define KERN_USRSTACK KERN_USRSTACK32
871 #endif
872 
873 
874 /* KERN_RAGEVNODE types */
875 #define KERN_RAGE_PROC          1
876 #define KERN_RAGE_THREAD        2
877 #define KERN_UNRAGE_PROC        3
878 #define KERN_UNRAGE_THREAD      4
879 
880 /* KERN_OPENEVT types */
881 #define KERN_OPENEVT_PROC     1
882 #define KERN_UNOPENEVT_PROC   2
883 
884 /* KERN_TFP types */
885 #define KERN_TFP_POLICY                 1
886 
887 /* KERN_TFP_POLICY values . All policies allow task port for self */
888 #define KERN_TFP_POLICY_DENY            0       /* Deny Mode: None allowed except privileged */
889 #define KERN_TFP_POLICY_DEFAULT         2       /* Default  Mode: related ones allowed and upcall authentication */
890 
891 /* KERN_KDEBUG types */
892 #define KERN_KDEFLAGS         1
893 #define KERN_KDDFLAGS         2
894 #define KERN_KDENABLE         3
895 #define KERN_KDSETBUF         4
896 #define KERN_KDGETBUF         5
897 #define KERN_KDSETUP          6
898 #define KERN_KDREMOVE         7
899 #define KERN_KDSETREG         8
900 #define KERN_KDGETREG         9
901 #define KERN_KDREADTR         10
902 #define KERN_KDPIDTR          11
903 #define KERN_KDTHRMAP         12
904 /* Don't use 13 as it is overloaded with KERN_VNODE */
905 #define KERN_KDPIDEX          14
906 #define KERN_KDSETRTCDEC      15 /* obsolete */
907 #define KERN_KDGETENTROPY     16 /* obsolete */
908 #define KERN_KDWRITETR        17
909 #define KERN_KDWRITEMAP       18
910 #define KERN_KDTEST           19
911 /* 20 unused */
912 #define KERN_KDREADCURTHRMAP  21
913 #define KERN_KDSET_TYPEFILTER 22
914 #define KERN_KDBUFWAIT        23
915 #define KERN_KDCPUMAP         24
916 #define KERN_KDCPUMAP_EXT     25
917 #define KERN_KDSET_EDM        26
918 #define KERN_KDGET_EDM        27
919 #define KERN_KDWRITETR_V3     28
920 
921 #define CTL_KERN_NAMES { \
922 	{ 0, 0 }, \
923 	{ "ostype", CTLTYPE_STRING }, \
924 	{ "osrelease", CTLTYPE_STRING }, \
925 	{ "osrevision", CTLTYPE_INT }, \
926 	{ "version", CTLTYPE_STRING }, \
927 	{ "maxvnodes", CTLTYPE_INT }, \
928 	{ "maxproc", CTLTYPE_INT }, \
929 	{ "maxfiles", CTLTYPE_INT }, \
930 	{ "argmax", CTLTYPE_INT }, \
931 	{ "securelevel", CTLTYPE_INT }, \
932 	{ "hostname", CTLTYPE_STRING }, \
933 	{ "hostid", CTLTYPE_INT }, \
934 	{ "clockrate", CTLTYPE_STRUCT }, \
935 	{ "vnode", CTLTYPE_STRUCT }, \
936 	{ "proc", CTLTYPE_STRUCT }, \
937 	{ "file", CTLTYPE_STRUCT }, \
938 	{ "profiling", CTLTYPE_NODE }, \
939 	{ "posix1version", CTLTYPE_INT }, \
940 	{ "ngroups", CTLTYPE_INT }, \
941 	{ "job_control", CTLTYPE_INT }, \
942 	{ "saved_ids", CTLTYPE_INT }, \
943 	{ "boottime", CTLTYPE_STRUCT }, \
944 	{ "nisdomainname", CTLTYPE_STRING }, \
945 	{ "maxpartitions", CTLTYPE_INT }, \
946 	{ "kdebug", CTLTYPE_INT }, \
947 	{ "update", CTLTYPE_INT }, \
948 	{ "osreldate", CTLTYPE_INT }, \
949 	{ "ntp_pll", CTLTYPE_NODE }, \
950 	{ "bootfile", CTLTYPE_STRING }, \
951 	{ "maxfilesperproc", CTLTYPE_INT }, \
952 	{ "maxprocperuid", CTLTYPE_INT }, \
953 	{ "dumpdev", CTLTYPE_STRUCT }, /* we lie; don't print as int */ \
954 	{ "ipc", CTLTYPE_NODE }, \
955 	{ "dummy", CTLTYPE_INT }, \
956 	{ "dummy", CTLTYPE_INT }, \
957 	{ "usrstack", CTLTYPE_INT }, \
958 	{ "logsigexit", CTLTYPE_INT }, \
959 	{ "symfile",CTLTYPE_STRING },\
960 	{ "procargs",CTLTYPE_STRUCT },\
961 	{ "dummy", CTLTYPE_INT },               /* deprecated pcsamples */ \
962 	{ "netboot", CTLTYPE_INT }, \
963 	{ "dummy", CTLTYPE_INT },               /* deprecated: panicinfo */ \
964 	{ "sysv", CTLTYPE_NODE }, \
965 	{ "dummy", CTLTYPE_INT }, \
966 	{ "dummy", CTLTYPE_INT }, \
967 	{ "exec", CTLTYPE_NODE }, \
968 	{ "aiomax", CTLTYPE_INT }, \
969 	{ "aioprocmax", CTLTYPE_INT }, \
970 	{ "aiothreads", CTLTYPE_INT }, \
971 	{ "procargs2",CTLTYPE_STRUCT }, \
972 	{ "corefile",CTLTYPE_STRING }, \
973 	{ "coredump", CTLTYPE_INT }, \
974 	{ "sugid_coredump", CTLTYPE_INT }, \
975 	{ "delayterm", CTLTYPE_INT }, \
976 	{ "shreg_private", CTLTYPE_INT }, \
977 	{ "proc_low_pri_io", CTLTYPE_INT }, \
978 	{ "low_pri_window", CTLTYPE_INT }, \
979 	{ "low_pri_delay", CTLTYPE_INT }, \
980 	{ "posix", CTLTYPE_NODE }, \
981 	{ "usrstack64", CTLTYPE_QUAD }, \
982 	{ "nx", CTLTYPE_INT }, \
983 	{ "tfp", CTLTYPE_NODE }, \
984 	{ "procname", CTLTYPE_STRING }, \
985 	{ "threadsigaltstack", CTLTYPE_INT }, \
986 	{ "speculative_reads_disabled", CTLTYPE_INT }, \
987 	{ "osversion", CTLTYPE_STRING }, \
988 	{ "safeboot", CTLTYPE_INT }, \
989 	{ "dummy", CTLTYPE_INT },               /* deprecated: lctx */ \
990 	{ "rage_vnode", CTLTYPE_INT }, \
991 	{ "tty", CTLTYPE_NODE },        \
992 	{ "check_openevt", CTLTYPE_INT }, \
993 	{ "thread_name", CTLTYPE_STRING } \
994 }
995 
996 /*
997  * CTL_VFS identifiers
998  */
999 #define CTL_VFS_NAMES { \
1000 	{ "vfsconf", CTLTYPE_STRUCT } \
1001 }
1002 
1003 /*
1004  * KERN_PROC subtypes
1005  */
1006 #define KERN_PROC_ALL           0       /* everything */
1007 #define KERN_PROC_PID           1       /* by process id */
1008 #define KERN_PROC_PGRP          2       /* by process group id */
1009 #define KERN_PROC_SESSION       3       /* by session of pid */
1010 #define KERN_PROC_TTY           4       /* by controlling tty */
1011 #define KERN_PROC_UID           5       /* by effective uid */
1012 #define KERN_PROC_RUID          6       /* by real uid */
1013 #define KERN_PROC_LCID          7       /* by login context id */
1014 
1015 /*
1016  * KERN_VFSNSPACE subtypes
1017  */
1018 #define KERN_VFSNSPACE_HANDLE_PROC              1
1019 #define KERN_VFSNSPACE_UNHANDLE_PROC    2
1020 
1021 #if defined(XNU_KERNEL_PRIVATE) || !defined(KERNEL)
1022 /*
1023  * KERN_PROC subtype ops return arrays of augmented proc structures:
1024  */
1025 
1026 struct _pcred {
1027 	char    pc_lock[72];            /* opaque content */
1028 	struct  ucred *pc_ucred;        /* Current credentials. */
1029 	uid_t   p_ruid;                 /* Real user id. */
1030 	uid_t   p_svuid;                /* Saved effective user id. */
1031 	gid_t   p_rgid;                 /* Real group id. */
1032 	gid_t   p_svgid;                /* Saved effective group id. */
1033 	int     p_refcnt;               /* Number of references. */
1034 };
1035 
1036 struct _ucred {
1037 	int32_t cr_ref;                 /* reference count */
1038 	uid_t   cr_uid;                 /* effective user id */
1039 	short   cr_ngroups;             /* number of groups */
1040 	gid_t   cr_groups[NGROUPS];     /* groups */
1041 };
1042 
1043 struct kinfo_proc {
1044 	struct  extern_proc kp_proc;                    /* proc structure */
1045 	struct  eproc {
1046 		struct  proc *e_paddr;          /* address of proc */
1047 		struct  session *e_sess;        /* session pointer */
1048 		struct  _pcred e_pcred;         /* process credentials */
1049 		struct  _ucred e_ucred;         /* current credentials */
1050 		struct   vmspace e_vm;          /* address space */
1051 		pid_t   e_ppid;                 /* parent process id */
1052 		pid_t   e_pgid;                 /* process group id */
1053 		short   e_jobc;                 /* job control counter */
1054 		dev_t   e_tdev;                 /* controlling tty dev */
1055 		pid_t   e_tpgid;                /* tty process group id */
1056 		struct  session *e_tsess;       /* tty session pointer */
1057 #define WMESGLEN        7
1058 		char    e_wmesg[WMESGLEN + 1];    /* wchan message */
1059 		segsz_t e_xsize;                /* text size */
1060 		short   e_xrssize;              /* text rss */
1061 		short   e_xccount;              /* text references */
1062 		short   e_xswrss;
1063 		int32_t e_flag;
1064 #define EPROC_CTTY      0x01    /* controlling tty vnode active */
1065 #define EPROC_SLEADER   0x02    /* session leader */
1066 #define COMAPT_MAXLOGNAME       12
1067 		char    e_login[COMAPT_MAXLOGNAME];     /* short setlogin() name */
1068 		int32_t e_spare[4];
1069 	} kp_eproc;
1070 };
1071 
1072 #endif /* defined(XNU_KERNEL_PRIVATE) || !defined(KERNEL) */
1073 
1074 #ifdef BSD_KERNEL_PRIVATE
1075 #include <sys/proc_internal.h>
1076 
1077 /* LP64 version of _pcred.  all pointers
1078  * grow when we're dealing with a 64-bit process.
1079  * WARNING - keep in sync with _pcred
1080  */
1081 
1082 struct user32_pcred {
1083 	char          pc_lock[72];    /* opaque content */
1084 	user32_addr_t pc_ucred;       /* Current credentials. */
1085 	uid_t         p_ruid;         /* Real user id. */
1086 	uid_t         p_svuid;        /* Saved effective user id. */
1087 	gid_t         p_rgid;         /* Real group id. */
1088 	gid_t         p_svgid;        /* Saved effective group id. */
1089 	int           p_refcnt;       /* Number of references. */
1090 };
1091 struct user64_pcred {
1092 	char          pc_lock[72];      /* opaque content */
1093 	user64_addr_t pc_ucred;         /* Current credentials. */
1094 	uid_t         p_ruid;           /* Real user id. */
1095 	uid_t         p_svuid;          /* Saved effective user id. */
1096 	gid_t         p_rgid;           /* Real group id. */
1097 	gid_t         p_svgid;          /* Saved effective group id. */
1098 	int           p_refcnt;         /* Number of references. */
1099 };
1100 
1101 /* LP64 version of kinfo_proc.  all pointers
1102  * grow when we're dealing with a 64-bit process.
1103  * WARNING - keep in sync with kinfo_proc
1104  */
1105 struct user32_kinfo_proc {
1106 	struct  user32_extern_proc kp_proc;     /* proc structure */
1107 	struct  user32_eproc {
1108 		user32_addr_t e_paddr;          /* address of proc */
1109 		user32_addr_t e_sess;                   /* session pointer */
1110 		struct  user32_pcred e_pcred;           /* process credentials */
1111 		struct  _ucred e_ucred;         /* current credentials */
1112 		struct  user32_vmspace e_vm; /* address space */
1113 		pid_t   e_ppid;                 /* parent process id */
1114 		pid_t   e_pgid;                 /* process group id */
1115 		int     e_jobc;                 /* job control counter */
1116 		dev_t   e_tdev;                 /* controlling tty dev */
1117 		pid_t   e_tpgid;                /* tty process group id */
1118 		user32_addr_t   e_tsess;        /* tty session pointer */
1119 		char    e_wmesg[WMESGLEN + 1];    /* wchan message */
1120 		segsz_t e_xsize;                /* text size */
1121 		short   e_xrssize;              /* text rss */
1122 		short   e_xccount;              /* text references */
1123 		short   e_xswrss;
1124 		int32_t e_flag;
1125 		char    e_login[COMAPT_MAXLOGNAME];     /* short setlogin() name */
1126 		int32_t e_spare[4];
1127 	} kp_eproc;
1128 };
1129 struct user64_kinfo_proc {
1130 	struct  user64_extern_proc kp_proc;     /* proc structure */
1131 	struct  user64_eproc {
1132 		user_addr_t e_paddr;            /* address of proc */
1133 		user_addr_t e_sess;                     /* session pointer */
1134 		struct  user64_pcred e_pcred;           /* process credentials */
1135 		struct  _ucred e_ucred;         /* current credentials */
1136 		struct   user_vmspace e_vm; /* address space */
1137 		pid_t   e_ppid;                 /* parent process id */
1138 		pid_t   e_pgid;                 /* process group id */
1139 		int     e_jobc;                 /* job control counter */
1140 		dev_t   e_tdev;                 /* controlling tty dev */
1141 		pid_t   e_tpgid;                /* tty process group id */
1142 		user64_addr_t   e_tsess __attribute((aligned(8)));      /* tty session pointer */
1143 		char    e_wmesg[WMESGLEN + 1];    /* wchan message */
1144 		segsz_t e_xsize;                /* text size */
1145 		short   e_xrssize;              /* text rss */
1146 		short   e_xccount;              /* text references */
1147 		short   e_xswrss;
1148 		int32_t e_flag;
1149 		char    e_login[COMAPT_MAXLOGNAME];     /* short setlogin() name */
1150 		int32_t e_spare[4];
1151 	} kp_eproc;
1152 };
1153 
1154 #endif  /* BSD_KERNEL_PRIVATE */
1155 
1156 /*
1157  * KERN_IPC identifiers
1158  */
1159 #define KIPC_MAXSOCKBUF         1       /* int: max size of a socket buffer */
1160 #define KIPC_SOCKBUF_WASTE      2       /* int: wastage factor in sockbuf */
1161 #define KIPC_SOMAXCONN          3       /* int: max length of connection q */
1162 #define KIPC_MAX_LINKHDR        4       /* int: max length of link header */
1163 #define KIPC_MAX_PROTOHDR       5       /* int: max length of network header */
1164 #define KIPC_MAX_HDR            6       /* int: max total length of headers */
1165 #define KIPC_MAX_DATALEN        7       /* int: max length of data? */
1166 #define KIPC_MBSTAT             8       /* struct: mbuf usage statistics */
1167 #define KIPC_NMBCLUSTERS        9       /* int: maximum mbuf clusters */
1168 #define KIPC_SOQLIMITCOMPAT     10      /* int: socket queue limit */
1169 
1170 /*
1171  * CTL_VM identifiers
1172  */
1173 #define VM_METER        1               /* struct vmmeter */
1174 #define VM_LOADAVG      2               /* struct loadavg */
1175 /*
1176  * Note: "3" was skipped sometime ago and should probably remain unused
1177  * to avoid any new entry from being accepted by older kernels...
1178  */
1179 #define VM_MACHFACTOR   4               /* struct loadavg with mach factor*/
1180 #define VM_SWAPUSAGE    5               /* total swap usage */
1181 #define VM_MAXID        6               /* number of valid vm ids */
1182 
1183 #define CTL_VM_NAMES { \
1184 	{ 0, 0 }, \
1185 	{ "vmmeter", CTLTYPE_STRUCT }, \
1186 	{ "loadavg", CTLTYPE_STRUCT }, \
1187 	{ 0, 0 }, /* placeholder for "3" (see comment above) */ \
1188 	{ "dummy", CTLTYPE_INT }, \
1189 	{ "swapusage", CTLTYPE_STRUCT } \
1190 }
1191 
1192 struct xsw_usage {
1193 	u_int64_t       xsu_total;
1194 	u_int64_t       xsu_avail;
1195 	u_int64_t       xsu_used;
1196 	u_int32_t       xsu_pagesize;
1197 	boolean_t       xsu_encrypted;
1198 };
1199 
1200 #ifdef __APPLE_API_PRIVATE
1201 /* Load average structure.  Use of fixpt_t assume <sys/types.h> in scope. */
1202 /* XXX perhaps we should protect fixpt_t, and define it here (or discard it) */
1203 struct loadavg {
1204 	fixpt_t ldavg[3];
1205 	long    fscale;
1206 };
1207 extern struct loadavg averunnable;
1208 #define LSCALE  1000            /* scaling for "fixed point" arithmetic */
1209 
1210 #ifdef BSD_KERNEL_PRIVATE
1211 
1212 struct user32_loadavg {
1213 	fixpt_t ldavg[3];
1214 	user32_long_t       fscale;
1215 };
1216 
1217 struct user64_loadavg {
1218 	fixpt_t ldavg[3];
1219 	user64_long_t       fscale;
1220 };
1221 
1222 #endif  /* BSD_KERNEL_PRIVATE */
1223 #endif /* __APPLE_API_PRIVATE */
1224 
1225 
1226 /*
1227  * CTL_HW identifiers
1228  */
1229 #define HW_MACHINE       1              /* string: machine class (deprecated: use HW_PRODUCT) */
1230 #define HW_MODEL         2              /* string: specific machine model (deprecated: use HW_TARGET) */
1231 #define HW_NCPU          3              /* int: number of cpus */
1232 #define HW_BYTEORDER     4              /* int: machine byte order */
1233 #define HW_PHYSMEM       5              /* int: total memory */
1234 #define HW_USERMEM       6              /* int: non-kernel memory */
1235 #define HW_PAGESIZE      7              /* int: software page size */
1236 #define HW_DISKNAMES     8              /* strings: disk drive names */
1237 #define HW_DISKSTATS     9              /* struct: diskstats[] */
1238 #define HW_EPOCH        10              /* int: 0 for Legacy, else NewWorld */
1239 #define HW_FLOATINGPT   11              /* int: has HW floating point? */
1240 #define HW_MACHINE_ARCH 12              /* string: machine architecture */
1241 #define HW_VECTORUNIT   13              /* int: has HW vector unit? */
1242 #define HW_BUS_FREQ     14              /* int: Bus Frequency */
1243 #define HW_CPU_FREQ     15              /* int: CPU Frequency */
1244 #define HW_CACHELINE    16              /* int: Cache Line Size in Bytes */
1245 #define HW_L1ICACHESIZE 17              /* int: L1 I Cache Size in Bytes */
1246 #define HW_L1DCACHESIZE 18              /* int: L1 D Cache Size in Bytes */
1247 #define HW_L2SETTINGS   19              /* int: L2 Cache Settings */
1248 #define HW_L2CACHESIZE  20              /* int: L2 Cache Size in Bytes */
1249 #define HW_L3SETTINGS   21              /* int: L3 Cache Settings */
1250 #define HW_L3CACHESIZE  22              /* int: L3 Cache Size in Bytes */
1251 #define HW_TB_FREQ      23              /* int: Bus Frequency */
1252 #define HW_MEMSIZE      24              /* uint64_t: physical ram size */
1253 #define HW_AVAILCPU     25              /* int: number of available CPUs */
1254 #define HW_TARGET       26              /* string: model identifier */
1255 #define HW_PRODUCT      27              /* string: product identifier */
1256 #define HW_MAXID        28              /* number of valid hw ids */
1257 
1258 #define CTL_HW_NAMES { \
1259 	{ 0, 0 }, \
1260 	{ "machine", CTLTYPE_STRING },          /* Deprecated: use hw.product */ \
1261 	{ "model", CTLTYPE_STRING },            /* Deprecated: use hw.target */ \
1262 	{ "ncpu", CTLTYPE_INT }, \
1263 	{ "byteorder", CTLTYPE_INT }, \
1264 	{ "physmem", CTLTYPE_INT }, \
1265 	{ "usermem", CTLTYPE_INT }, \
1266 	{ "pagesize", CTLTYPE_INT }, \
1267 	{ "disknames", CTLTYPE_STRUCT }, \
1268 	{ "diskstats", CTLTYPE_STRUCT }, \
1269 	{ "epoch", CTLTYPE_INT }, \
1270 	{ "floatingpoint", CTLTYPE_INT }, \
1271 	{ "machinearch", CTLTYPE_STRING }, \
1272 	{ "vectorunit", CTLTYPE_INT }, \
1273 	{ "busfrequency", CTLTYPE_INT }, \
1274 	{ "cpufrequency", CTLTYPE_INT }, \
1275 	{ "cachelinesize", CTLTYPE_INT }, \
1276 	{ "l1icachesize", CTLTYPE_INT }, \
1277 	{ "l1dcachesize", CTLTYPE_INT }, \
1278 	{ "l2settings", CTLTYPE_INT }, \
1279 	{ "l2cachesize", CTLTYPE_INT }, \
1280 	{ "l3settings", CTLTYPE_INT }, \
1281 	{ "l3cachesize", CTLTYPE_INT }, \
1282 	{ "tbfrequency", CTLTYPE_INT }, \
1283 	{ "memsize", CTLTYPE_QUAD }, \
1284 	{ "availcpu", CTLTYPE_INT }, \
1285 	{ "target", CTLTYPE_STRING }, \
1286 	{ "product", CTLTYPE_STRING }, \
1287 }
1288 
1289 /*
1290  * XXX This information should be moved to the man page.
1291  *
1292  * These are the support HW selectors for sysctlbyname.  Parameters that are byte counts or frequencies are 64 bit numbers.
1293  * All other parameters are 32 bit numbers.
1294  *
1295  *   hw.memsize                - The number of bytes of physical memory in the system.
1296  *
1297  *   hw.ncpu                   - The maximum number of processors that could be available this boot.
1298  *                               Use this value for sizing of static per processor arrays; i.e. processor load statistics.
1299  *
1300  *   hw.activecpu              - The number of processors currently available for executing threads.
1301  *                               Use this number to determine the number threads to create in SMP aware applications.
1302  *                               This number can change when power management modes are changed.
1303  *
1304  *   hw.physicalcpu            - The number of physical processors available in the current power management mode.
1305  *   hw.physicalcpu_max        - The maximum number of physical processors that could be available this boot
1306  *
1307  *   hw.logicalcpu             - The number of logical processors available in the current power management mode.
1308  *   hw.logicalcpu_max         - The maximum number of logical processors that could be available this boot
1309  *
1310  *   hw.tbfrequency            - This gives the time base frequency used by the OS and is the basis of all timing services.
1311  *                               In general is is better to use mach's or higher level timing services, but this value
1312  *                               is needed to convert the PPC Time Base registers to real time.
1313  *
1314  *   hw.cpufrequency, hw.busfrequency and their min/max versions are deprecated because frequency isn't consistent.
1315  *
1316  *   hw.cpufrequency           - (deprecated) These values provide the current, min and max cpu frequency.  The min and max are for
1317  *   hw.cpufrequency_max       - (deprecated) all power management modes.  The current frequency is the max frequency in the current mode.
1318  *   hw.cpufrequency_min       - (deprecated) All frequencies are in Hz.
1319  *
1320  *   hw.busfrequency           - (deprecated) These values provide the current, min and max bus frequency.  The min and max are for
1321  *   hw.busfrequency_max       - (deprecated) all power management modes.  The current frequency is the max frequency in the current mode.
1322  *   hw.busfrequency_min       - (deprecated) All frequencies are in Hz.
1323  *
1324  *   hw.cputype                - These values provide the mach-o cpu type and subtype.  A complete list is in <mach/machine.h>
1325  *   hw.cpusubtype             - These values should be used to determine what processor family the running cpu is from so that
1326  *                               the best binary can be chosen, or the best dynamic code generated.  They should not be used
1327  *                               to determine if a given processor feature is available.
1328  *   hw.cputhreadtype          - This value will be present if the processor supports threads.  Like hw.cpusubtype this selector
1329  *                               should not be used to infer features, and only used to name the processors thread architecture.
1330  *                               The values are defined in <mach/machine.h>
1331  *
1332  *   hw.byteorder              - Gives the byte order of the processor.  4321 for big endian, 1234 for little.
1333  *
1334  *   hw.pagesize               - Gives the size in bytes of the pages used by the processor and VM system.
1335  *
1336  *   hw.cachelinesize          - Gives the size in bytes of the processor's cache lines.
1337  *                               This value should be use to control the strides of loops that use cache control instructions
1338  *                               like dcbz, dcbt or dcbst.
1339  *
1340  *   hw.l1dcachesize           - These values provide the size in bytes of the L1, L2 and L3 caches.  If a cache is not present
1341  *   hw.l1icachesize           - then the selector will return and error.
1342  *   hw.l2cachesize            -
1343  *   hw.l3cachesize            -
1344  *
1345  *   hw.nperflevels            - Number of core types in the system. See the parameters below, which can be used to get
1346  *                             - information associated with a specific perf level.
1347  *
1348  *   The following parameters apply to perflevel N, where N is a number between 0 and the number of core types in the system minus one.
1349  *   perflevel 0 always refers to the highest performance core type in the system.
1350  *
1351  *   hw.perflevelN.physicalcpu      - The number of physical processors available in the current power management mode.
1352  *   hw.perflevelN.physicalcpumax   - The maximum number of physical processors that could be available this boot.
1353  *   hw.perflevelN.logicalcpu       - The number of logical processors available in the current power management mode.
1354  *   hw.perflevelN.logicalcpumax    - The maximum number of logical processors that could be available this boot.
1355  *
1356  *   hw.perflevelN.l1dcachesize     - These values provide the size in bytes of the L1, L2 and L3 caches.  If a cache is not present
1357  *   hw.perflevelN.l1icachesize     - then the selector will return and error.
1358  *   hw.perflevelN.l2cachesize      -
1359  *   hw.perflevelN.l3cachesize      -
1360  *
1361  *   hw.perflevelN.cpusperl2        - These values provide the number of CPUs of the same type that share L2 and L3 caches.
1362  *   hw.perflevelN.cpusperl3        - If a cache is not present then the selector will return and error.
1363  *
1364  *   hw.perflevelN.l2perflevels     - These values provide a bitmap, where bit  number of CPUs of the same type that share L2 and L3 caches.
1365  *   hw.perflevelN.l3perflevels     - If a cache is not present then the selector will return and error.
1366  *
1367  *   hw.packages               - Gives the number of processor packages.
1368  *
1369  * These are the selectors for optional processor features for specific processors.  Selectors that return errors are not support
1370  * on the system.  Supported features will return 1 if they are recommended or 0 if they are supported but are not expected to help .
1371  * performance.  Future versions of these selectors may return larger values as necessary so it is best to test for non zero.
1372  *
1373  * For PowerPC:
1374  *
1375  *   hw.optional.floatingpoint - Floating Point Instructions
1376  *   hw.optional.altivec       - AltiVec Instructions
1377  *   hw.optional.graphicsops   - Graphics Operations
1378  *   hw.optional.64bitops      - 64-bit Instructions
1379  *   hw.optional.fsqrt         - HW Floating Point Square Root Instruction
1380  *   hw.optional.stfiwx        - Store Floating Point as Integer Word Indexed Instructions
1381  *   hw.optional.dcba          - Data Cache Block Allocate Instruction
1382  *   hw.optional.datastreams   - Data Streams Instructions
1383  *   hw.optional.dcbtstreams   - Data Cache Block Touch Steams Instruction Form
1384  *
1385  * For x86 Architecture:
1386  *
1387  *   hw.optional.floatingpoint     - Floating Point Instructions
1388  *   hw.optional.mmx               - Original MMX vector instructions
1389  *   hw.optional.sse               - Streaming SIMD Extensions
1390  *   hw.optional.sse2              - Streaming SIMD Extensions 2
1391  *   hw.optional.sse3              - Streaming SIMD Extensions 3
1392  *   hw.optional.supplementalsse3  - Supplemental Streaming SIMD Extensions 3
1393  *   hw.optional.x86_64            - 64-bit support
1394  */
1395 
1396 
1397 /*
1398  * CTL_USER definitions
1399  */
1400 #define USER_CS_PATH             1      /* string: _CS_PATH */
1401 #define USER_BC_BASE_MAX         2      /* int: BC_BASE_MAX */
1402 #define USER_BC_DIM_MAX          3      /* int: BC_DIM_MAX */
1403 #define USER_BC_SCALE_MAX        4      /* int: BC_SCALE_MAX */
1404 #define USER_BC_STRING_MAX       5      /* int: BC_STRING_MAX */
1405 #define USER_COLL_WEIGHTS_MAX    6      /* int: COLL_WEIGHTS_MAX */
1406 #define USER_EXPR_NEST_MAX       7      /* int: EXPR_NEST_MAX */
1407 #define USER_LINE_MAX            8      /* int: LINE_MAX */
1408 #define USER_RE_DUP_MAX          9      /* int: RE_DUP_MAX */
1409 #define USER_POSIX2_VERSION     10      /* int: POSIX2_VERSION */
1410 #define USER_POSIX2_C_BIND      11      /* int: POSIX2_C_BIND */
1411 #define USER_POSIX2_C_DEV       12      /* int: POSIX2_C_DEV */
1412 #define USER_POSIX2_CHAR_TERM   13      /* int: POSIX2_CHAR_TERM */
1413 #define USER_POSIX2_FORT_DEV    14      /* int: POSIX2_FORT_DEV */
1414 #define USER_POSIX2_FORT_RUN    15      /* int: POSIX2_FORT_RUN */
1415 #define USER_POSIX2_LOCALEDEF   16      /* int: POSIX2_LOCALEDEF */
1416 #define USER_POSIX2_SW_DEV      17      /* int: POSIX2_SW_DEV */
1417 #define USER_POSIX2_UPE         18      /* int: POSIX2_UPE */
1418 #define USER_STREAM_MAX         19      /* int: POSIX2_STREAM_MAX */
1419 #define USER_TZNAME_MAX         20      /* int: POSIX2_TZNAME_MAX */
1420 #define USER_MAXID              21      /* number of valid user ids */
1421 
1422 #define CTL_USER_NAMES { \
1423 	{ 0, 0 }, \
1424 	{ "cs_path", CTLTYPE_STRING }, \
1425 	{ "bc_base_max", CTLTYPE_INT }, \
1426 	{ "bc_dim_max", CTLTYPE_INT }, \
1427 	{ "bc_scale_max", CTLTYPE_INT }, \
1428 	{ "bc_string_max", CTLTYPE_INT }, \
1429 	{ "coll_weights_max", CTLTYPE_INT }, \
1430 	{ "expr_nest_max", CTLTYPE_INT }, \
1431 	{ "line_max", CTLTYPE_INT }, \
1432 	{ "re_dup_max", CTLTYPE_INT }, \
1433 	{ "posix2_version", CTLTYPE_INT }, \
1434 	{ "posix2_c_bind", CTLTYPE_INT }, \
1435 	{ "posix2_c_dev", CTLTYPE_INT }, \
1436 	{ "posix2_char_term", CTLTYPE_INT }, \
1437 	{ "posix2_fort_dev", CTLTYPE_INT }, \
1438 	{ "posix2_fort_run", CTLTYPE_INT }, \
1439 	{ "posix2_localedef", CTLTYPE_INT }, \
1440 	{ "posix2_sw_dev", CTLTYPE_INT }, \
1441 	{ "posix2_upe", CTLTYPE_INT }, \
1442 	{ "stream_max", CTLTYPE_INT }, \
1443 	{ "tzname_max", CTLTYPE_INT } \
1444 }
1445 
1446 
1447 
1448 /*
1449  * CTL_DEBUG definitions
1450  *
1451  * Second level identifier specifies which debug variable.
1452  * Third level identifier specifies which stucture component.
1453  */
1454 #define CTL_DEBUG_NAME          0       /* string: variable name */
1455 #define CTL_DEBUG_VALUE         1       /* int: variable value */
1456 #define CTL_DEBUG_MAXID         20
1457 
1458 
1459 #if (CTL_MAXID != 9) || (KERN_MAXID != 72) || (VM_MAXID != 6) || (HW_MAXID != 28) || (USER_MAXID != 21) || (CTL_DEBUG_MAXID != 20)
1460 #error Use the SYSCTL_*() macros and OID_AUTO instead!
1461 #endif
1462 
1463 
1464 #ifdef  KERNEL
1465 
1466 #ifdef BSD_KERNEL_PRIVATE
1467 extern char     machine[];
1468 extern char     osrelease[];
1469 #define OSRELEASETYPE_SIZE 48
1470 extern char     osreleasetype[OSRELEASETYPE_SIZE];
1471 extern char     ostype[];
1472 extern char     osversion[];
1473 extern char     osproductversion[];
1474 extern char     osbuild_config[];
1475 
1476 /*
1477  * Tries to match variants inside osreleasetype such as matching "Darwin" in
1478  * "Darwin Internal".
1479  */
1480 static inline bool
kern_osreleasetype_matches(const char * variant)1481 kern_osreleasetype_matches(const char *variant)
1482 {
1483 	const size_t len = sizeof(osreleasetype);
1484 
1485 	return strnstr(__unsafe_null_terminated_from_indexable(osreleasetype, &osreleasetype[len - 1]),
1486 	           variant, len);
1487 }
1488 
1489 #if defined(XNU_TARGET_OS_BRIDGE)
1490 /*
1491  * 15 characters at maximum so both the productversion
1492  * and the build version can fit in the panic header
1493  * osversion field with the formatting requirements.
1494  */
1495 #define MACOS_VERS_LEN 15
1496 
1497 extern char     macosproductversion[];
1498 extern char     macosversion[];
1499 #endif
1500 
1501 void    sysctl_mib_init(void);
1502 
1503 #endif /* BSD_KERNEL_PRIVATE */
1504 #else   /* !KERNEL */
1505 
1506 __BEGIN_DECLS
1507 int     sysctl(int *, u_int, void *__sized_by(*oldlenp), size_t *oldlenp,
1508     void *__sized_by(newlen), size_t newlen);
1509 int     sysctlbyname(const char *, void *__sized_by(*oldlenp), size_t *oldlenp,
1510     void *__sized_by(newlen), size_t newlen);
1511 int     sysctlnametomib(const char *, int *__counted_by(*sizep), size_t *sizep);
1512 __END_DECLS
1513 
1514 #endif  /* KERNEL */
1515 
1516 
1517 #endif /* SYSCTL_DEF_ENABLED */
1518 
1519 
1520 #endif  /* !_SYS_SYSCTL_H_ */
1521