xref: /xnu-12377.1.9/bsd/kern/kern_sysctl.c (revision f6217f891ac0bb64f3d375211650a4c1ff8ca1ea)
1 /*
2  * Copyright (c) 2000-2025 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) 1982, 1986, 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  *	@(#)kern_sysctl.c	8.4 (Berkeley) 4/14/94
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 /*
74  * DEPRECATED sysctl system call code
75  *
76  * Everything in this file is deprecated. Sysctls should be handled
77  * by the code in kern_newsysctl.c.
78  * The remaining "case" sections are supposed to be converted into
79  * SYSCTL_*-style definitions, and as soon as all of them are gone,
80  * this source file is supposed to die.
81  *
82  * DO NOT ADD ANY MORE "case" SECTIONS TO THIS FILE, instead define
83  * your sysctl with SYSCTL_INT, SYSCTL_PROC etc. in your source file.
84  */
85 
86 #include <sys/param.h>
87 #include <sys/systm.h>
88 #include <sys/kernel.h>
89 #include <sys/malloc.h>
90 #include <sys/proc_internal.h>
91 #include <sys/kauth.h>
92 #include <sys/file_internal.h>
93 #include <sys/vnode_internal.h>
94 #include <sys/unistd.h>
95 #include <sys/buf.h>
96 #include <sys/ioctl.h>
97 #include <sys/namei.h>
98 #include <sys/tty.h>
99 #include <sys/disklabel.h>
100 #include <sys/vm.h>
101 #include <sys/sysctl.h>
102 #include <sys/user.h>
103 #include <sys/aio_kern.h>
104 #include <sys/reboot.h>
105 #include <sys/memory_maintenance.h>
106 #include <sys/priv.h>
107 #include <sys/ubc.h> /* mach_to_bsd_errno */
108 
109 #include <stdatomic.h>
110 #include <uuid/uuid.h>
111 
112 #include <security/audit/audit.h>
113 #include <kern/kalloc.h>
114 
115 #include <machine/smp.h>
116 #include <machine/atomic.h>
117 #include <machine/config.h>
118 #include <mach/machine.h>
119 #include <mach/mach_host.h>
120 #include <mach/mach_types.h>
121 #include <mach/processor_info.h>
122 #include <mach/vm_param.h>
123 #include <kern/debug.h>
124 #include <kern/mach_param.h>
125 #include <kern/task.h>
126 #include <kern/thread.h>
127 #include <kern/thread_group.h>
128 #include <kern/processor.h>
129 #include <kern/cpu_number.h>
130 #include <kern/sched_prim.h>
131 #include <kern/workload_config.h>
132 #include <kern/iotrace.h>
133 #include <vm/vm_kern_xnu.h>
134 #include <vm/vm_map_xnu.h>
135 #include <mach/host_info.h>
136 #include <mach/exclaves.h>
137 #include <kern/hvg_hypercall.h>
138 #include <kdp/sk_core.h>
139 
140 #if DEVELOPMENT || DEBUG
141 #include <kern/ext_paniclog.h>
142 #endif
143 
144 #include <sys/mount_internal.h>
145 #include <sys/kdebug.h>
146 #include <sys/kern_debug.h>
147 #include <sys/kern_sysctl.h>
148 #include <sys/variant_internal.h>
149 
150 #include <IOKit/IOPlatformExpert.h>
151 #include <pexpert/pexpert.h>
152 
153 #include <machine/machine_routines.h>
154 #include <machine/exec.h>
155 
156 #include <nfs/nfs_conf.h>
157 
158 #include <vm/vm_protos.h>
159 #include <vm/vm_page.h>
160 #include <vm/vm_pageout_xnu.h>
161 #include <vm/vm_compressor_algorithms_xnu.h>
162 #include <vm/vm_compressor_xnu.h>
163 #include <sys/imgsrc.h>
164 #include <kern/timer_call.h>
165 #include <sys/codesign.h>
166 #include <IOKit/IOBSD.h>
167 #if CONFIG_CSR
168 #include <sys/csr.h>
169 #endif
170 
171 #if defined(__i386__) || defined(__x86_64__)
172 #include <i386/cpuid.h>
173 #endif
174 
175 #if CONFIG_FREEZE
176 #include <sys/kern_memorystatus.h>
177 #endif
178 
179 #if KPERF
180 #include <kperf/kperf.h>
181 #endif
182 
183 #if HYPERVISOR
184 #include <kern/hv_support.h>
185 #endif
186 
187 
188 #include <corecrypto/ccsha2.h>
189 
190 /*
191  * deliberately setting max requests to really high number
192  * so that runaway settings do not cause MALLOC overflows
193  */
194 #define AIO_MAX_REQUESTS (128 * CONFIG_AIO_MAX)
195 
196 extern int aio_max_requests;
197 extern int aio_max_requests_per_process;
198 extern int aio_worker_threads;
199 extern int lowpri_IO_window_msecs;
200 extern int lowpri_IO_delay_msecs;
201 #if DEVELOPMENT || DEBUG
202 extern int nx_enabled;
203 #endif
204 extern int speculative_reads_disabled;
205 extern unsigned int speculative_prefetch_max;
206 extern unsigned int speculative_prefetch_max_iosize;
207 extern unsigned int preheat_max_bytes;
208 extern unsigned int preheat_min_bytes;
209 extern long numvnodes;
210 extern long freevnodes;
211 extern long num_recycledvnodes;
212 
213 extern uuid_string_t bootsessionuuid_string;
214 
215 extern unsigned int vm_max_delayed_work_limit;
216 extern unsigned int vm_max_batch;
217 
218 extern unsigned int vm_page_free_min;
219 extern unsigned int vm_page_free_target;
220 extern unsigned int vm_page_free_reserved;
221 extern unsigned int vm_page_max_speculative_age_q;
222 
223 static uint64_t userspacereboottime = 0;
224 static unsigned int userspacerebootpurpose = 0;
225 
226 #if (DEVELOPMENT || DEBUG)
227 extern uint32_t vm_page_creation_throttled_hard;
228 extern uint32_t vm_page_creation_throttled_soft;
229 #endif /* DEVELOPMENT || DEBUG */
230 
231 #if DEVELOPMENT || DEBUG
232 extern bool bootarg_hide_process_traced;
233 #endif
234 
235 /*
236  * Conditionally allow dtrace to see these functions for debugging purposes.
237  */
238 #ifdef STATIC
239 #undef STATIC
240 #endif
241 #if 0
242 #define STATIC
243 #else
244 #define STATIC static
245 #endif
246 
247 extern boolean_t    mach_timer_coalescing_enabled;
248 
249 extern uint64_t timer_deadline_tracking_bin_1, timer_deadline_tracking_bin_2;
250 
251 STATIC void
252 fill_user32_eproc(proc_t, struct user32_eproc *__restrict);
253 STATIC void
254 fill_user32_externproc(proc_t, struct user32_extern_proc *__restrict);
255 STATIC void
256 fill_user64_eproc(proc_t, struct user64_eproc *__restrict);
257 STATIC void
258 fill_user64_proc(proc_t, struct user64_kinfo_proc *__restrict);
259 STATIC void
260 fill_user64_externproc(proc_t, struct user64_extern_proc *__restrict);
261 STATIC void
262 fill_user32_proc(proc_t, struct user32_kinfo_proc *__restrict);
263 
264 #if CONFIG_NETBOOT
265 extern int
266 netboot_root(void);
267 #endif
268 int
269 sysctl_procargs(int *name, u_int namelen, user_addr_t where,
270     size_t *sizep, proc_t cur_proc);
271 STATIC int
272 sysctl_procargsx(int *name, u_int namelen, user_addr_t where, size_t *sizep,
273     proc_t cur_proc, int argc_yes);
274 int
275 sysctl_struct(user_addr_t oldp, size_t *oldlenp, user_addr_t newp,
276     size_t newlen, void *sp, int len);
277 
278 STATIC int sysdoproc_filt_KERN_PROC_PID(proc_t p, void * arg);
279 STATIC int sysdoproc_filt_KERN_PROC_PGRP(proc_t p, void * arg);
280 STATIC int sysdoproc_filt_KERN_PROC_TTY(proc_t p, void * arg);
281 STATIC int  sysdoproc_filt_KERN_PROC_UID(proc_t p, void * arg);
282 STATIC int  sysdoproc_filt_KERN_PROC_RUID(proc_t p, void * arg);
283 int sysdoproc_callback(proc_t p, void *arg);
284 
285 #if CONFIG_THREAD_GROUPS && (DEVELOPMENT || DEBUG)
286 STATIC int sysctl_get_thread_group_id SYSCTL_HANDLER_ARGS;
287 #endif
288 
289 /* forward declarations for non-static STATIC */
290 STATIC void fill_loadavg64(struct loadavg *la, struct user64_loadavg *la64);
291 STATIC void fill_loadavg32(struct loadavg *la, struct user32_loadavg *la32);
292 STATIC int sysctl_handle_kern_threadname(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
293 STATIC int sysctl_sched_stats(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
294 STATIC int sysctl_sched_stats_enable(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
295 #if COUNT_SYSCALLS
296 STATIC int sysctl_docountsyscalls SYSCTL_HANDLER_ARGS;
297 #endif  /* COUNT_SYSCALLS */
298 #if defined(XNU_TARGET_OS_OSX)
299 STATIC int sysctl_doprocargs SYSCTL_HANDLER_ARGS;
300 #endif  /* defined(XNU_TARGET_OS_OSX) */
301 STATIC int sysctl_doprocargs2 SYSCTL_HANDLER_ARGS;
302 STATIC int sysctl_prochandle SYSCTL_HANDLER_ARGS;
303 STATIC int sysctl_aiomax(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
304 STATIC int sysctl_aioprocmax(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
305 STATIC int sysctl_aiothreads(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
306 STATIC int sysctl_maxproc(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
307 STATIC int sysctl_osversion(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
308 STATIC int sysctl_sysctl_bootargs(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
309 STATIC int sysctl_maxvnodes(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
310 STATIC int sysctl_securelvl(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
311 STATIC int sysctl_domainname(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
312 STATIC int sysctl_hostname(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
313 STATIC int sysctl_procname(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
314 STATIC int sysctl_boottime(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
315 STATIC int sysctl_bootuuid(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
316 STATIC int sysctl_symfile(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
317 #if CONFIG_NETBOOT
318 STATIC int sysctl_netboot(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
319 #endif
320 #ifdef CONFIG_IMGSRC_ACCESS
321 STATIC int sysctl_imgsrcdev(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
322 #endif
323 STATIC int sysctl_usrstack(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
324 STATIC int sysctl_usrstack64(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
325 #if CONFIG_COREDUMP || CONFIG_UCOREDUMP
326 STATIC int sysctl_coredump(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
327 STATIC int sysctl_suid_coredump(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
328 #if CONFIG_UCOREDUMP
329 STATIC int sysctl_ucoredump(struct sysctl_oid *, void *, int, struct sysctl_req *);
330 #endif
331 #endif
332 STATIC int sysctl_delayterm(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
333 STATIC int sysctl_rage_vnode(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
334 STATIC int sysctl_kern_check_openevt(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
335 #if DEVELOPMENT || DEBUG
336 STATIC int sysctl_nx(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
337 #endif
338 STATIC int sysctl_loadavg(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
339 STATIC int sysctl_vm_toggle_address_reuse(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
340 STATIC int sysctl_swapusage(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
341 STATIC int fetch_process_cputype( proc_t cur_proc, int *name, u_int namelen, cpu_type_t *cputype);
342 STATIC int sysctl_sysctl_native(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
343 STATIC int sysctl_sysctl_cputype(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
344 STATIC int sysctl_safeboot(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
345 STATIC int sysctl_singleuser(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
346 STATIC int sysctl_minimalboot(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
347 STATIC int sysctl_slide(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
348 
349 #ifdef CONFIG_XNUPOST
350 #include <tests/xnupost.h>
351 
352 STATIC int sysctl_debug_test_oslog_ctl(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
353 STATIC int sysctl_debug_test_stackshot_mutex_owner(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
354 STATIC int sysctl_debug_test_stackshot_rwlck_owner(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
355 #endif
356 
357 extern void IORegistrySetOSBuildVersion(char * build_version);
358 extern int IOParseWorkloadConfig(workload_config_ctx_t *ctx, const char * buffer, size_t size);
359 extern int IOUnparseWorkloadConfig(char *buffer, size_t *size);
360 
361 STATIC void
fill_loadavg64(struct loadavg * la,struct user64_loadavg * la64)362 fill_loadavg64(struct loadavg *la, struct user64_loadavg *la64)
363 {
364 	la64->ldavg[0]  = la->ldavg[0];
365 	la64->ldavg[1]  = la->ldavg[1];
366 	la64->ldavg[2]  = la->ldavg[2];
367 	la64->fscale    = (user64_long_t)la->fscale;
368 }
369 
370 STATIC void
fill_loadavg32(struct loadavg * la,struct user32_loadavg * la32)371 fill_loadavg32(struct loadavg *la, struct user32_loadavg *la32)
372 {
373 	la32->ldavg[0]  = la->ldavg[0];
374 	la32->ldavg[1]  = la->ldavg[1];
375 	la32->ldavg[2]  = la->ldavg[2];
376 	la32->fscale    = (user32_long_t)la->fscale;
377 }
378 
379 #if COUNT_SYSCALLS
380 extern int do_count_syscalls;
381 #endif
382 
383 #ifdef INSECURE
384 int securelevel = -1;
385 #else
386 int securelevel;
387 #endif
388 
389 STATIC int
sysctl_handle_kern_threadname(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)390 sysctl_handle_kern_threadname(  __unused struct sysctl_oid *oidp, __unused void *arg1,
391     __unused int arg2, struct sysctl_req *req)
392 {
393 	int error;
394 	struct uthread *ut = current_uthread();
395 	user_addr_t oldp = 0, newp = 0;
396 	size_t *oldlenp = NULL;
397 	size_t newlen = 0;
398 
399 	oldp = req->oldptr;
400 	oldlenp = &(req->oldlen);
401 	newp = req->newptr;
402 	newlen = req->newlen;
403 
404 	/* We want the current length, and maybe the string itself */
405 	if (oldlenp) {
406 		/* if we have no thread name yet tell'em we want MAXTHREADNAMESIZE - 1 */
407 		size_t currlen = MAXTHREADNAMESIZE - 1;
408 
409 		if (ut->pth_name) {
410 			/* use length of current thread name */
411 			currlen = strlen(ut->pth_name);
412 		}
413 		if (oldp) {
414 			if (*oldlenp < currlen) {
415 				return ENOMEM;
416 			}
417 			/* NOTE - we do not copy the NULL terminator */
418 			if (ut->pth_name) {
419 				error = copyout(ut->pth_name, oldp, currlen);
420 				if (error) {
421 					return error;
422 				}
423 			}
424 		}
425 		/* return length of thread name minus NULL terminator (just like strlen)  */
426 		req->oldidx = currlen;
427 	}
428 
429 	/* We want to set the name to something */
430 	if (newp) {
431 		if (newlen > (MAXTHREADNAMESIZE - 1)) {
432 			return ENAMETOOLONG;
433 		}
434 		if (!ut->pth_name) {
435 			char *tmp_pth_name = (char *)kalloc_data(MAXTHREADNAMESIZE,
436 			    Z_WAITOK | Z_ZERO);
437 			if (!tmp_pth_name) {
438 				return ENOMEM;
439 			}
440 			if (!OSCompareAndSwapPtr(NULL, tmp_pth_name, &ut->pth_name)) {
441 				kfree_data(tmp_pth_name, MAXTHREADNAMESIZE);
442 				return EBUSY;
443 			}
444 		} else {
445 			kernel_debug_string_simple(TRACE_STRING_THREADNAME_PREV, ut->pth_name);
446 			bzero(ut->pth_name, MAXTHREADNAMESIZE);
447 		}
448 		error = copyin(newp, ut->pth_name, newlen);
449 		if (error) {
450 			return error;
451 		}
452 
453 		kernel_debug_string_simple(TRACE_STRING_THREADNAME, ut->pth_name);
454 	}
455 
456 	return 0;
457 }
458 
459 SYSCTL_PROC(_kern, KERN_THREADNAME, threadname, CTLFLAG_ANYBODY | CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_LOCKED, 0, 0, sysctl_handle_kern_threadname, "A", "");
460 
461 #define WORKLOAD_CONFIG_MAX_SIZE (128 * 1024 * 1024)
462 
463 /* Called locked - sysctl defined without CTLFLAG_LOCKED. */
464 static int
465 sysctl_workload_config SYSCTL_HANDLER_ARGS
466 {
467 #pragma unused(arg1, arg2)
468 
469 	char *plist_blob = NULL;
470 	kern_return_t ret = KERN_FAILURE;
471 	int error = -1;
472 
473 	/* Only allow reading of workload config on non-RELEASE kernels. */
474 #if DEVELOPMENT || DEBUG
475 
476 	const size_t buf_size = req->oldlen;
477 
478 	if (!req->oldptr) {
479 		/* Just looking for the size to allocate. */
480 		size_t size = 0;
481 		ret = IOUnparseWorkloadConfig(NULL, &size);
482 		if (ret != KERN_SUCCESS) {
483 			return ENOMEM;
484 		}
485 
486 		error = SYSCTL_OUT(req, NULL, size);
487 		if (error) {
488 			return error;
489 		}
490 	} else {
491 		if (buf_size > (WORKLOAD_CONFIG_MAX_SIZE - 1) ||
492 		    buf_size == 0) {
493 			return EINVAL;
494 		}
495 
496 		plist_blob = kalloc_data(buf_size, Z_WAITOK | Z_ZERO);
497 		if (!plist_blob) {
498 			return ENOMEM;
499 		}
500 
501 		size_t size = buf_size;
502 		ret = IOUnparseWorkloadConfig(plist_blob, &size);
503 		if (ret != KERN_SUCCESS) {
504 			kfree_data(plist_blob, buf_size);
505 			return ENOMEM;
506 		}
507 
508 		error = SYSCTL_OUT(req, plist_blob, MIN(buf_size, size));
509 
510 		/* If the buffer was too small to fit the entire config. */
511 		if (buf_size < size) {
512 			error = ENOMEM;
513 		}
514 
515 		kfree_data(plist_blob, buf_size);
516 		if (error) {
517 			return error;
518 		}
519 	}
520 #endif /* DEVELOPMENT || DEBUG */
521 
522 	if (req->newptr) {
523 		size_t newlen = req->newlen;
524 		if (newlen > (WORKLOAD_CONFIG_MAX_SIZE - 1)) {
525 			return EINVAL;
526 		}
527 
528 
529 		workload_config_ctx_t *ctx = NULL;
530 		/*
531 		 * Only allow workload_config_boot to be loaded once at boot by launchd.
532 		 */
533 		if (current_proc() == initproc &&
534 		    !workload_config_initialized(&workload_config_boot)) {
535 			ctx = &workload_config_boot;
536 		} else {
537 #if DEVELOPMENT || DEBUG
538 			/*
539 			 * Use the devel config context otherwise. If a devel config has been
540 			 * initialized it will be used for lookups in place of the boot config.
541 			 */
542 			ctx = &workload_config_devel;
543 			if (workload_config_initialized(ctx)) {
544 				workload_config_free(ctx);
545 			}
546 
547 			/* The devel context can be explicitly cleared by an empty string. */
548 			if (newlen == 1) {
549 				return 0;
550 			}
551 #else
552 			return EINVAL;
553 #endif
554 		}
555 
556 		plist_blob = kalloc_data(newlen + 1, Z_WAITOK | Z_ZERO);
557 		if (!plist_blob) {
558 			return ENOMEM;
559 		}
560 		error = copyin(req->newptr, plist_blob, newlen);
561 		if (error) {
562 			kfree_data(plist_blob, newlen + 1);
563 			return error;
564 		}
565 		plist_blob[newlen] = '\0';
566 		ret = IOParseWorkloadConfig(ctx, plist_blob, newlen + 1);
567 
568 		kfree_data(plist_blob, newlen + 1);
569 		return ret == KERN_SUCCESS ? 0 : EINVAL;
570 	}
571 
572 	return 0;
573 }
574 
575 SYSCTL_PROC(_kern, OID_AUTO, workload_config, CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MASKED,
576     0, 0, sysctl_workload_config, "A", "global workgroup configuration plist load/unload");
577 
578 #define BSD_HOST 1
579 STATIC int
sysctl_sched_stats(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)580 sysctl_sched_stats(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
581 {
582 	host_basic_info_data_t hinfo;
583 	kern_return_t kret;
584 	uint32_t size;
585 	uint32_t buf_size = 0;
586 	int changed;
587 	mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
588 	struct _processor_statistics_np *buf;
589 	int error;
590 
591 	kret = host_info((host_t)BSD_HOST, HOST_BASIC_INFO, (host_info_t)&hinfo, &count);
592 	if (kret != KERN_SUCCESS) {
593 		return EINVAL;
594 	}
595 
596 	size = sizeof(struct _processor_statistics_np) * (hinfo.logical_cpu_max + 2); /* One for RT Queue, One for Fair Share Queue */
597 
598 	if (req->oldlen < size) {
599 		return EINVAL;
600 	}
601 
602 	buf_size = size;
603 	buf = (struct _processor_statistics_np *)kalloc_data(buf_size, Z_ZERO | Z_WAITOK);
604 
605 	kret = get_sched_statistics(buf, &size);
606 	if (kret != KERN_SUCCESS) {
607 		error = EINVAL;
608 		goto out;
609 	}
610 
611 	error = sysctl_io_opaque(req, buf, size, &changed);
612 	if (error) {
613 		goto out;
614 	}
615 
616 	if (changed) {
617 		panic("Sched info changed?!");
618 	}
619 out:
620 	kfree_data(buf, buf_size);
621 	return error;
622 }
623 
624 SYSCTL_PROC(_kern, OID_AUTO, sched_stats, CTLFLAG_LOCKED, 0, 0, sysctl_sched_stats, "-", "");
625 
626 STATIC int
sysctl_sched_stats_enable(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,__unused struct sysctl_req * req)627 sysctl_sched_stats_enable(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, __unused struct sysctl_req *req)
628 {
629 	boolean_t active;
630 	int res;
631 
632 	if (req->newlen != sizeof(active)) {
633 		return EINVAL;
634 	}
635 
636 	res = copyin(req->newptr, &active, sizeof(active));
637 	if (res != 0) {
638 		return res;
639 	}
640 
641 	return set_sched_stats_active(active);
642 }
643 
644 SYSCTL_PROC(_kern, OID_AUTO, sched_stats_enable, CTLFLAG_LOCKED | CTLFLAG_WR, 0, 0, sysctl_sched_stats_enable, "-", "");
645 
646 extern uint32_t sched_debug_flags;
647 SYSCTL_INT(_debug, OID_AUTO, sched, CTLFLAG_RW | CTLFLAG_LOCKED, &sched_debug_flags, 0, "scheduler debug");
648 
649 #if (DEBUG || DEVELOPMENT)
650 extern boolean_t doprnt_hide_pointers;
651 SYSCTL_INT(_debug, OID_AUTO, hide_kernel_pointers, CTLFLAG_RW | CTLFLAG_LOCKED, &doprnt_hide_pointers, 0, "hide kernel pointers from log");
652 #endif
653 
654 
655 extern int get_kernel_symfile(proc_t, char **);
656 
657 #if COUNT_SYSCALLS
658 #define KERN_COUNT_SYSCALLS (KERN_OSTYPE + 1000)
659 
660 extern const unsigned int     nsysent;
661 extern int syscalls_log[];
662 extern const char *syscallnames[];
663 
664 STATIC int
665 sysctl_docountsyscalls SYSCTL_HANDLER_ARGS
666 {
667 	__unused int cmd = oidp->oid_arg2;      /* subcommand*/
668 	__unused int *name = arg1;      /* oid element argument vector */
669 	__unused int namelen = arg2;    /* number of oid element arguments */
670 	int error, changed;
671 
672 	int tmp;
673 
674 	/* valid values passed in:
675 	 * = 0 means don't keep called counts for each bsd syscall
676 	 * > 0 means keep called counts for each bsd syscall
677 	 * = 2 means dump current counts to the system log
678 	 * = 3 means reset all counts
679 	 * for example, to dump current counts:
680 	 *		sysctl -w kern.count_calls=2
681 	 */
682 	error = sysctl_io_number(req, do_count_syscalls,
683 	    sizeof(do_count_syscalls), &tmp, &changed);
684 
685 	if (error != 0 || !changed) {
686 		return error;
687 	}
688 
689 	if (tmp == 1) {
690 		do_count_syscalls = 1;
691 	} else if (tmp == 0 || tmp == 2 || tmp == 3) {
692 		for (int i = 0; i < nsysent; i++) {
693 			if (syscalls_log[i] != 0) {
694 				if (tmp == 2) {
695 					printf("%d calls - name %s \n", syscalls_log[i], syscallnames[i]);
696 				} else {
697 					syscalls_log[i] = 0;
698 				}
699 			}
700 		}
701 		do_count_syscalls = (tmp != 0);
702 	}
703 
704 	return error;
705 }
706 SYSCTL_PROC(_kern, KERN_COUNT_SYSCALLS, count_syscalls, CTLTYPE_NODE | CTLFLAG_RD | CTLFLAG_LOCKED,
707     0,                          /* Pointer argument (arg1) */
708     0,                          /* Integer argument (arg2) */
709     sysctl_docountsyscalls,     /* Handler function */
710     NULL,                       /* Data pointer */
711     "");
712 #endif  /* COUNT_SYSCALLS */
713 
714 /*
715  * The following sysctl_* functions should not be used
716  * any more, as they can only cope with callers in
717  * user mode: Use new-style
718  *  sysctl_io_number()
719  *  sysctl_io_string()
720  *  sysctl_io_opaque()
721  * instead.
722  */
723 
724 STATIC int
sysdoproc_filt_KERN_PROC_PID(proc_t p,void * arg)725 sysdoproc_filt_KERN_PROC_PID(proc_t p, void * arg)
726 {
727 	if (proc_getpid(p) != (pid_t)*(int*)arg) {
728 		return 0;
729 	} else {
730 		return 1;
731 	}
732 }
733 
734 STATIC int
sysdoproc_filt_KERN_PROC_PGRP(proc_t p,void * arg)735 sysdoproc_filt_KERN_PROC_PGRP(proc_t p, void * arg)
736 {
737 	if (p->p_pgrpid != (pid_t)*(int*)arg) {
738 		return 0;
739 	} else {
740 		return 1;
741 	}
742 }
743 
744 STATIC int
sysdoproc_filt_KERN_PROC_TTY(proc_t p,void * arg)745 sysdoproc_filt_KERN_PROC_TTY(proc_t p, void * arg)
746 {
747 	struct pgrp *pg;
748 	dev_t dev = NODEV;
749 
750 	if ((p->p_flag & P_CONTROLT) && (pg = proc_pgrp(p, NULL)) != PGRP_NULL) {
751 		dev = os_atomic_load(&pg->pg_session->s_ttydev, relaxed);
752 		pgrp_rele(pg);
753 	}
754 
755 	return dev != NODEV && dev == (dev_t)*(int *)arg;
756 }
757 
758 STATIC int
sysdoproc_filt_KERN_PROC_UID(proc_t p,void * arg)759 sysdoproc_filt_KERN_PROC_UID(proc_t p, void * arg)
760 {
761 	uid_t uid;
762 
763 	smr_proc_task_enter();
764 	uid = kauth_cred_getuid(proc_ucred_smr(p));
765 	smr_proc_task_leave();
766 
767 	if (uid != (uid_t)*(int*)arg) {
768 		return 0;
769 	} else {
770 		return 1;
771 	}
772 }
773 
774 
775 STATIC int
sysdoproc_filt_KERN_PROC_RUID(proc_t p,void * arg)776 sysdoproc_filt_KERN_PROC_RUID(proc_t p, void * arg)
777 {
778 	uid_t ruid;
779 
780 	smr_proc_task_enter();
781 	ruid = kauth_cred_getruid(proc_ucred_smr(p));
782 	smr_proc_task_leave();
783 
784 	if (ruid != (uid_t)*(int*)arg) {
785 		return 0;
786 	} else {
787 		return 1;
788 	}
789 }
790 
791 /*
792  * try over estimating by 5 procs
793  */
794 #define KERN_PROCSLOP (5 * sizeof(struct kinfo_proc))
795 struct sysdoproc_args {
796 	size_t buflen;
797 	void *kprocp;
798 	boolean_t is_64_bit;
799 	user_addr_t dp;
800 	size_t needed;
801 	unsigned int sizeof_kproc;
802 	int *errorp;
803 	int uidcheck;
804 	int ruidcheck;
805 	int ttycheck;
806 	int uidval;
807 };
808 
809 int
sysdoproc_callback(proc_t p,void * arg)810 sysdoproc_callback(proc_t p, void *arg)
811 {
812 	struct sysdoproc_args *args = arg;
813 
814 	if (args->buflen >= args->sizeof_kproc) {
815 		if ((args->ruidcheck != 0) && (sysdoproc_filt_KERN_PROC_RUID(p, &args->uidval) == 0)) {
816 			return PROC_RETURNED;
817 		}
818 		if ((args->uidcheck != 0) && (sysdoproc_filt_KERN_PROC_UID(p, &args->uidval) == 0)) {
819 			return PROC_RETURNED;
820 		}
821 		if ((args->ttycheck != 0) && (sysdoproc_filt_KERN_PROC_TTY(p, &args->uidval) == 0)) {
822 			return PROC_RETURNED;
823 		}
824 
825 		bzero(args->kprocp, args->sizeof_kproc);
826 		if (args->is_64_bit) {
827 			fill_user64_proc(p, args->kprocp);
828 		} else {
829 			fill_user32_proc(p, args->kprocp);
830 		}
831 		int error = copyout(args->kprocp, args->dp, args->sizeof_kproc);
832 		if (error) {
833 			*args->errorp = error;
834 			return PROC_RETURNED_DONE;
835 		}
836 		args->dp += args->sizeof_kproc;
837 		args->buflen -= args->sizeof_kproc;
838 	}
839 	args->needed += args->sizeof_kproc;
840 	return PROC_RETURNED;
841 }
842 
843 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD | CTLFLAG_LOCKED, 0, "");
844 STATIC int
845 sysctl_prochandle SYSCTL_HANDLER_ARGS
846 {
847 	int cmd = oidp->oid_arg2;       /* subcommand for multiple nodes */
848 	int *name = arg1;               /* oid element argument vector */
849 	int namelen = arg2;             /* number of oid element arguments */
850 	user_addr_t where = req->oldptr;/* user buffer copy out address */
851 
852 	user_addr_t dp = where;
853 	size_t needed = 0;
854 	size_t buflen = where != USER_ADDR_NULL ? req->oldlen : 0;
855 	int error = 0;
856 	boolean_t is_64_bit = proc_is64bit(current_proc());
857 	struct user32_kinfo_proc  user32_kproc;
858 	struct user64_kinfo_proc  user_kproc;
859 	int sizeof_kproc;
860 	void *kprocp;
861 	int (*filterfn)(proc_t, void *) = 0;
862 	struct sysdoproc_args args;
863 	int uidcheck = 0;
864 	int ruidcheck = 0;
865 	int ttycheck = 0;
866 
867 	if (namelen != 1 && !(namelen == 0 && cmd == KERN_PROC_ALL)) {
868 		return EINVAL;
869 	}
870 
871 	if (is_64_bit) {
872 		sizeof_kproc = sizeof(user_kproc);
873 		kprocp = &user_kproc;
874 	} else {
875 		sizeof_kproc = sizeof(user32_kproc);
876 		kprocp = &user32_kproc;
877 	}
878 
879 	switch (cmd) {
880 	case KERN_PROC_PID:
881 		filterfn = sysdoproc_filt_KERN_PROC_PID;
882 		break;
883 
884 	case KERN_PROC_PGRP:
885 		filterfn = sysdoproc_filt_KERN_PROC_PGRP;
886 		break;
887 
888 	case KERN_PROC_TTY:
889 		ttycheck = 1;
890 		break;
891 
892 	case KERN_PROC_UID:
893 		uidcheck = 1;
894 		break;
895 
896 	case KERN_PROC_RUID:
897 		ruidcheck = 1;
898 		break;
899 
900 	case KERN_PROC_ALL:
901 		break;
902 
903 	default:
904 		/* must be kern.proc.<unknown> */
905 		return ENOTSUP;
906 	}
907 
908 	error = 0;
909 	args.buflen = buflen;
910 	args.kprocp = kprocp;
911 	args.is_64_bit = is_64_bit;
912 	args.dp = dp;
913 	args.needed = needed;
914 	args.errorp = &error;
915 	args.uidcheck = uidcheck;
916 	args.ruidcheck = ruidcheck;
917 	args.ttycheck = ttycheck;
918 	args.sizeof_kproc = sizeof_kproc;
919 	if (namelen) {
920 		args.uidval = name[0];
921 	}
922 
923 	proc_iterate((PROC_ALLPROCLIST | PROC_ZOMBPROCLIST),
924 	    sysdoproc_callback, &args, filterfn, name);
925 
926 	if (error) {
927 		return error;
928 	}
929 
930 	dp = args.dp;
931 	needed = args.needed;
932 
933 	if (where != USER_ADDR_NULL) {
934 		req->oldlen = dp - where;
935 		if (needed > req->oldlen) {
936 			return ENOMEM;
937 		}
938 	} else {
939 		needed += KERN_PROCSLOP;
940 		req->oldlen = needed;
941 	}
942 	/* adjust index so we return the right required/consumed amount */
943 	req->oldidx += req->oldlen;
944 	return 0;
945 }
946 
947 
948 /*
949  * We specify the subcommand code for multiple nodes as the 'req->arg2' value
950  * in the sysctl declaration itself, which comes into the handler function
951  * as 'oidp->oid_arg2'.
952  *
953  * For these particular sysctls, since they have well known OIDs, we could
954  * have just obtained it from the '((int *)arg1)[0]' parameter, but that would
955  * not demonstrate how to handle multiple sysctls that used OID_AUTO instead
956  * of a well known value with a common handler function.  This is desirable,
957  * because we want well known values to "go away" at some future date.
958  *
959  * It should be noted that the value of '((int *)arg1)[1]' is used for many
960  * an integer parameter to the subcommand for many of these sysctls; we'd
961  * rather have used '((int *)arg1)[0]' for that, or even better, an element
962  * in a structure passed in as the the 'newp' argument to sysctlbyname(3),
963  * and then use leaf-node permissions enforcement, but that would have
964  * necessitated modifying user space code to correspond to the interface
965  * change, and we are striving for binary backward compatibility here; even
966  * though these are SPI, and not intended for use by user space applications
967  * which are not themselves system tools or libraries, some applications
968  * have erroneously used them.
969  */
970 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLTYPE_NODE | CTLFLAG_RD | CTLFLAG_LOCKED,
971     0,                          /* Pointer argument (arg1) */
972     KERN_PROC_ALL,              /* Integer argument (arg2) */
973     sysctl_prochandle,          /* Handler function */
974     NULL,                       /* Data is size variant on ILP32/LP64 */
975     "");
976 SYSCTL_PROC(_kern_proc, KERN_PROC_PID, pid, CTLTYPE_NODE | CTLFLAG_RD | CTLFLAG_LOCKED,
977     0,                          /* Pointer argument (arg1) */
978     KERN_PROC_PID,              /* Integer argument (arg2) */
979     sysctl_prochandle,          /* Handler function */
980     NULL,                       /* Data is size variant on ILP32/LP64 */
981     "");
982 SYSCTL_PROC(_kern_proc, KERN_PROC_TTY, tty, CTLTYPE_NODE | CTLFLAG_RD | CTLFLAG_LOCKED,
983     0,                          /* Pointer argument (arg1) */
984     KERN_PROC_TTY,              /* Integer argument (arg2) */
985     sysctl_prochandle,          /* Handler function */
986     NULL,                       /* Data is size variant on ILP32/LP64 */
987     "");
988 SYSCTL_PROC(_kern_proc, KERN_PROC_PGRP, pgrp, CTLTYPE_NODE | CTLFLAG_RD | CTLFLAG_LOCKED,
989     0,                          /* Pointer argument (arg1) */
990     KERN_PROC_PGRP,             /* Integer argument (arg2) */
991     sysctl_prochandle,          /* Handler function */
992     NULL,                       /* Data is size variant on ILP32/LP64 */
993     "");
994 SYSCTL_PROC(_kern_proc, KERN_PROC_UID, uid, CTLTYPE_NODE | CTLFLAG_RD | CTLFLAG_LOCKED,
995     0,                          /* Pointer argument (arg1) */
996     KERN_PROC_UID,              /* Integer argument (arg2) */
997     sysctl_prochandle,          /* Handler function */
998     NULL,                       /* Data is size variant on ILP32/LP64 */
999     "");
1000 SYSCTL_PROC(_kern_proc, KERN_PROC_RUID, ruid, CTLTYPE_NODE | CTLFLAG_RD | CTLFLAG_LOCKED,
1001     0,                          /* Pointer argument (arg1) */
1002     KERN_PROC_RUID,             /* Integer argument (arg2) */
1003     sysctl_prochandle,          /* Handler function */
1004     NULL,                       /* Data is size variant on ILP32/LP64 */
1005     "");
1006 SYSCTL_PROC(_kern_proc, KERN_PROC_LCID, lcid, CTLTYPE_NODE | CTLFLAG_RD | CTLFLAG_LOCKED,
1007     0,                          /* Pointer argument (arg1) */
1008     KERN_PROC_LCID,             /* Integer argument (arg2) */
1009     sysctl_prochandle,          /* Handler function */
1010     NULL,                       /* Data is size variant on ILP32/LP64 */
1011     "");
1012 
1013 
1014 /*
1015  * Fill in non-zero fields of an eproc structure for the specified process.
1016  */
1017 STATIC void
fill_user32_eproc(proc_t p,struct user32_eproc * __restrict ep)1018 fill_user32_eproc(proc_t p, struct user32_eproc *__restrict ep)
1019 {
1020 	struct pgrp *pg;
1021 	struct session *sessp;
1022 	kauth_cred_t my_cred;
1023 
1024 	pg = proc_pgrp(p, &sessp);
1025 
1026 	if (pg != PGRP_NULL) {
1027 		ep->e_pgid = p->p_pgrpid;
1028 		ep->e_jobc = pg->pg_jobc;
1029 		if (sessp->s_ttyvp) {
1030 			ep->e_flag = EPROC_CTTY;
1031 		}
1032 	}
1033 
1034 	ep->e_ppid = p->p_ppid;
1035 
1036 	smr_proc_task_enter();
1037 	my_cred = proc_ucred_smr(p);
1038 
1039 	/* A fake historical pcred */
1040 	ep->e_pcred.p_ruid = kauth_cred_getruid(my_cred);
1041 	ep->e_pcred.p_svuid = kauth_cred_getsvuid(my_cred);
1042 	ep->e_pcred.p_rgid = kauth_cred_getrgid(my_cred);
1043 	ep->e_pcred.p_svgid = kauth_cred_getsvgid(my_cred);
1044 
1045 	/* A fake historical *kauth_cred_t */
1046 	unsigned long refcnt = os_atomic_load(&my_cred->cr_ref, relaxed);
1047 	ep->e_ucred.cr_ref = (uint32_t)MIN(refcnt, UINT32_MAX);
1048 	ep->e_ucred.cr_uid = kauth_cred_getuid(my_cred);
1049 	ep->e_ucred.cr_ngroups = (short)posix_cred_get(my_cred)->cr_ngroups;
1050 	bcopy(posix_cred_get(my_cred)->cr_groups,
1051 	    ep->e_ucred.cr_groups, NGROUPS * sizeof(gid_t));
1052 
1053 	my_cred = NOCRED;
1054 	smr_proc_task_leave();
1055 
1056 	ep->e_tdev = NODEV;
1057 	if (pg != PGRP_NULL) {
1058 		if (p->p_flag & P_CONTROLT) {
1059 			session_lock(sessp);
1060 			ep->e_tdev = os_atomic_load(&sessp->s_ttydev, relaxed);
1061 			ep->e_tpgid = sessp->s_ttypgrpid;
1062 			session_unlock(sessp);
1063 		}
1064 		if (SESS_LEADER(p, sessp)) {
1065 			ep->e_flag |= EPROC_SLEADER;
1066 		}
1067 		pgrp_rele(pg);
1068 	}
1069 }
1070 
1071 /*
1072  * Fill in non-zero fields of an LP64 eproc structure for the specified process.
1073  */
1074 STATIC void
fill_user64_eproc(proc_t p,struct user64_eproc * __restrict ep)1075 fill_user64_eproc(proc_t p, struct user64_eproc *__restrict ep)
1076 {
1077 	struct pgrp *pg;
1078 	struct session *sessp;
1079 	kauth_cred_t my_cred;
1080 
1081 	pg = proc_pgrp(p, &sessp);
1082 
1083 	if (pg != PGRP_NULL) {
1084 		ep->e_pgid = p->p_pgrpid;
1085 		ep->e_jobc = pg->pg_jobc;
1086 		if (sessp->s_ttyvp) {
1087 			ep->e_flag = EPROC_CTTY;
1088 		}
1089 	}
1090 
1091 	ep->e_ppid = p->p_ppid;
1092 
1093 	smr_proc_task_enter();
1094 	my_cred = proc_ucred_smr(p);
1095 
1096 	/* A fake historical pcred */
1097 	ep->e_pcred.p_ruid = kauth_cred_getruid(my_cred);
1098 	ep->e_pcred.p_svuid = kauth_cred_getsvuid(my_cred);
1099 	ep->e_pcred.p_rgid = kauth_cred_getrgid(my_cred);
1100 	ep->e_pcred.p_svgid = kauth_cred_getsvgid(my_cred);
1101 
1102 	/* A fake historical *kauth_cred_t */
1103 	unsigned long refcnt = os_atomic_load(&my_cred->cr_ref, relaxed);
1104 	ep->e_ucred.cr_ref = (uint32_t)MIN(refcnt, UINT32_MAX);
1105 	ep->e_ucred.cr_uid = kauth_cred_getuid(my_cred);
1106 	ep->e_ucred.cr_ngroups = (short)posix_cred_get(my_cred)->cr_ngroups;
1107 	bcopy(posix_cred_get(my_cred)->cr_groups,
1108 	    ep->e_ucred.cr_groups, NGROUPS * sizeof(gid_t));
1109 
1110 	my_cred = NOCRED;
1111 	smr_proc_task_leave();
1112 
1113 	ep->e_tdev = NODEV;
1114 	if (pg != PGRP_NULL) {
1115 		if (p->p_flag & P_CONTROLT) {
1116 			session_lock(sessp);
1117 			ep->e_tdev = os_atomic_load(&sessp->s_ttydev, relaxed);
1118 			ep->e_tpgid = sessp->s_ttypgrpid;
1119 			session_unlock(sessp);
1120 		}
1121 		if (SESS_LEADER(p, sessp)) {
1122 			ep->e_flag |= EPROC_SLEADER;
1123 		}
1124 		pgrp_rele(pg);
1125 	}
1126 }
1127 
1128 /*
1129  * Fill in an eproc structure for the specified process.
1130  * bzeroed by our caller, so only set non-zero fields.
1131  */
1132 STATIC void
fill_user32_externproc(proc_t p,struct user32_extern_proc * __restrict exp)1133 fill_user32_externproc(proc_t p, struct user32_extern_proc *__restrict exp)
1134 {
1135 	exp->p_starttime.tv_sec = (user32_time_t)p->p_start.tv_sec;
1136 	exp->p_starttime.tv_usec = p->p_start.tv_usec;
1137 	exp->p_flag = p->p_flag;
1138 #if DEVELOPMENT || DEBUG
1139 	if (p->p_lflag & P_LTRACED && !bootarg_hide_process_traced) {
1140 #else
1141 	if (p->p_lflag & P_LTRACED) {
1142 #endif
1143 		exp->p_flag |= P_TRACED;
1144 	}
1145 	if (p->p_lflag & P_LPPWAIT) {
1146 		exp->p_flag |= P_PPWAIT;
1147 	}
1148 	if (p->p_lflag & P_LEXIT) {
1149 		exp->p_flag |= P_WEXIT;
1150 	}
1151 	exp->p_stat = p->p_stat;
1152 	exp->p_pid = proc_getpid(p);
1153 #if DEVELOPMENT || DEBUG
1154 	if (bootarg_hide_process_traced) {
1155 		exp->p_oppid = 0;
1156 	} else
1157 #endif
1158 	{
1159 		exp->p_oppid = p->p_oppid;
1160 	}
1161 	/* Mach related  */
1162 	exp->p_debugger = p->p_debugger;
1163 	exp->sigwait = p->sigwait;
1164 	/* scheduling */
1165 #ifdef _PROC_HAS_SCHEDINFO_
1166 	exp->p_estcpu = p->p_estcpu;
1167 	exp->p_pctcpu = p->p_pctcpu;
1168 	exp->p_slptime = p->p_slptime;
1169 #endif
1170 	exp->p_realtimer.it_interval.tv_sec =
1171 	    (user32_time_t)p->p_realtimer.it_interval.tv_sec;
1172 	exp->p_realtimer.it_interval.tv_usec =
1173 	    (__int32_t)p->p_realtimer.it_interval.tv_usec;
1174 
1175 	exp->p_realtimer.it_value.tv_sec =
1176 	    (user32_time_t)p->p_realtimer.it_value.tv_sec;
1177 	exp->p_realtimer.it_value.tv_usec =
1178 	    (__int32_t)p->p_realtimer.it_value.tv_usec;
1179 
1180 	exp->p_rtime.tv_sec = (user32_time_t)p->p_rtime.tv_sec;
1181 	exp->p_rtime.tv_usec = (__int32_t)p->p_rtime.tv_usec;
1182 
1183 	exp->p_sigignore = p->p_sigignore;
1184 	exp->p_sigcatch = p->p_sigcatch;
1185 	exp->p_priority = p->p_priority;
1186 	exp->p_nice = p->p_nice;
1187 	bcopy(&p->p_comm, &exp->p_comm, MAXCOMLEN);
1188 	exp->p_xstat = (u_short)MIN(p->p_xstat, USHRT_MAX);
1189 	exp->p_acflag = p->p_acflag;
1190 }
1191 
1192 /*
1193  * Fill in an LP64 version of extern_proc structure for the specified process.
1194  */
1195 STATIC void
1196 fill_user64_externproc(proc_t p, struct user64_extern_proc *__restrict exp)
1197 {
1198 	exp->p_starttime.tv_sec = p->p_start.tv_sec;
1199 	exp->p_starttime.tv_usec = p->p_start.tv_usec;
1200 	exp->p_flag = p->p_flag;
1201 #if DEVELOPMENT || DEBUG
1202 	if (p->p_lflag & P_LTRACED && !bootarg_hide_process_traced) {
1203 #else
1204 	if (p->p_lflag & P_LTRACED) {
1205 #endif
1206 		exp->p_flag |= P_TRACED;
1207 	}
1208 	if (p->p_lflag & P_LPPWAIT) {
1209 		exp->p_flag |= P_PPWAIT;
1210 	}
1211 	if (p->p_lflag & P_LEXIT) {
1212 		exp->p_flag |= P_WEXIT;
1213 	}
1214 	exp->p_stat = p->p_stat;
1215 	exp->p_pid = proc_getpid(p);
1216 #if DEVELOPMENT || DEBUG
1217 	if (bootarg_hide_process_traced) {
1218 		exp->p_oppid = 0;
1219 	} else
1220 #endif
1221 	{
1222 		exp->p_oppid = p->p_oppid;
1223 	}
1224 	/* Mach related  */
1225 	exp->p_debugger = p->p_debugger;
1226 	exp->sigwait = p->sigwait;
1227 	/* scheduling */
1228 #ifdef _PROC_HAS_SCHEDINFO_
1229 	exp->p_estcpu = p->p_estcpu;
1230 	exp->p_pctcpu = p->p_pctcpu;
1231 	exp->p_slptime = p->p_slptime;
1232 #endif
1233 	exp->p_realtimer.it_interval.tv_sec = p->p_realtimer.it_interval.tv_sec;
1234 	exp->p_realtimer.it_interval.tv_usec = p->p_realtimer.it_interval.tv_usec;
1235 
1236 	exp->p_realtimer.it_value.tv_sec = p->p_realtimer.it_value.tv_sec;
1237 	exp->p_realtimer.it_value.tv_usec = p->p_realtimer.it_value.tv_usec;
1238 
1239 	exp->p_rtime.tv_sec = p->p_rtime.tv_sec;
1240 	exp->p_rtime.tv_usec = p->p_rtime.tv_usec;
1241 
1242 	exp->p_sigignore = p->p_sigignore;
1243 	exp->p_sigcatch = p->p_sigcatch;
1244 	exp->p_priority = p->p_priority;
1245 	exp->p_nice = p->p_nice;
1246 	bcopy(&p->p_comm, &exp->p_comm, MAXCOMLEN);
1247 	exp->p_xstat = (u_short)MIN(p->p_xstat, USHRT_MAX);
1248 	exp->p_acflag = p->p_acflag;
1249 }
1250 
1251 STATIC void
1252 fill_user32_proc(proc_t p, struct user32_kinfo_proc *__restrict kp)
1253 {
1254 	/* on a 64 bit kernel, 32 bit users get some truncated information */
1255 	fill_user32_externproc(p, &kp->kp_proc);
1256 	fill_user32_eproc(p, &kp->kp_eproc);
1257 }
1258 
1259 STATIC void
1260 fill_user64_proc(proc_t p, struct user64_kinfo_proc *__restrict kp)
1261 {
1262 	fill_user64_externproc(p, &kp->kp_proc);
1263 	fill_user64_eproc(p, &kp->kp_eproc);
1264 }
1265 
1266 #if defined(XNU_TARGET_OS_OSX)
1267 /*
1268  * Return the top *sizep bytes of the user stack, or the entire area of the
1269  * user stack down through the saved exec_path, whichever is smaller.
1270  */
1271 STATIC int
1272 sysctl_doprocargs SYSCTL_HANDLER_ARGS
1273 {
1274 	__unused int cmd = oidp->oid_arg2;      /* subcommand*/
1275 	int *name = arg1;               /* oid element argument vector */
1276 	int namelen = arg2;             /* number of oid element arguments */
1277 	user_addr_t oldp = req->oldptr; /* user buffer copy out address */
1278 	size_t *oldlenp = &req->oldlen; /* user buffer copy out size */
1279 //	user_addr_t newp = req->newptr;	/* user buffer copy in address */
1280 //	size_t newlen = req->newlen;	/* user buffer copy in size */
1281 	int error;
1282 
1283 	error =  sysctl_procargsx( name, namelen, oldp, oldlenp, current_proc(), 0);
1284 
1285 	/* adjust index so we return the right required/consumed amount */
1286 	if (!error) {
1287 		req->oldidx += req->oldlen;
1288 	}
1289 
1290 	return error;
1291 }
1292 SYSCTL_PROC(_kern, KERN_PROCARGS, procargs, CTLTYPE_NODE | CTLFLAG_RD | CTLFLAG_LOCKED,
1293     0,                          /* Pointer argument (arg1) */
1294     0,                          /* Integer argument (arg2) */
1295     sysctl_doprocargs,          /* Handler function */
1296     NULL,                       /* Data pointer */
1297     "");
1298 #endif  /* defined(XNU_TARGET_OS_OSX) */
1299 
1300 STATIC int
1301 sysctl_doprocargs2 SYSCTL_HANDLER_ARGS
1302 {
1303 	__unused int cmd = oidp->oid_arg2;      /* subcommand*/
1304 	int *name = arg1;               /* oid element argument vector */
1305 	int namelen = arg2;             /* number of oid element arguments */
1306 	user_addr_t oldp = req->oldptr; /* user buffer copy out address */
1307 	size_t *oldlenp = &req->oldlen; /* user buffer copy out size */
1308 //	user_addr_t newp = req->newptr;	/* user buffer copy in address */
1309 //	size_t newlen = req->newlen;	/* user buffer copy in size */
1310 	int error;
1311 
1312 	error = sysctl_procargsx( name, namelen, oldp, oldlenp, current_proc(), 1);
1313 
1314 	/* adjust index so we return the right required/consumed amount */
1315 	if (!error) {
1316 		req->oldidx += req->oldlen;
1317 	}
1318 
1319 	return error;
1320 }
1321 SYSCTL_PROC(_kern, KERN_PROCARGS2, procargs2, CTLTYPE_NODE | CTLFLAG_RD | CTLFLAG_LOCKED,
1322     0,                          /* Pointer argument (arg1) */
1323     0,                          /* Integer argument (arg2) */
1324     sysctl_doprocargs2,         /* Handler function */
1325     NULL,                       /* Data pointer */
1326     "");
1327 
1328 #define SYSCTL_PROCARGS_READ_ENVVARS_ENTITLEMENT "com.apple.private.read-environment-variables"
1329 STATIC int
1330 sysctl_procargsx(int *name, u_int namelen, user_addr_t where,
1331     size_t *sizep, proc_t cur_proc, int argc_yes)
1332 {
1333 	assert(sizep != NULL);
1334 	proc_t p = NULL;
1335 	size_t buflen = where != USER_ADDR_NULL ? *sizep : 0;
1336 	int error = 0;
1337 	struct _vm_map *proc_map = NULL;
1338 	struct task * task;
1339 	vm_map_copy_t   tmp = NULL;
1340 	user_addr_t     arg_addr;
1341 	size_t          arg_size;
1342 	caddr_t data;
1343 	size_t argslen = 0;
1344 	size_t size = 0;
1345 	vm_offset_t     copy_start = 0, copy_end;
1346 	vm_offset_t     smallbuffer_start;
1347 	kern_return_t ret;
1348 	int pid;
1349 	uid_t uid;
1350 	int argc = -1;
1351 	size_t argvsize;
1352 	size_t remaining;
1353 	size_t current_arg_index;
1354 	size_t current_arg_len;
1355 	const char * current_arg;
1356 	bool omit_env_vars = true;
1357 	user_addr_t user_stack;
1358 	vm_map_offset_t effective_page_mask;
1359 
1360 	if (namelen < 1) {
1361 		error = EINVAL;
1362 		goto finish;
1363 	}
1364 
1365 	if (argc_yes) {
1366 		buflen -= sizeof(int);          /* reserve first word to return argc */
1367 	}
1368 	/* we only care about buflen when where (oldp from sysctl) is not NULL. */
1369 	/* when where (oldp from sysctl) is NULL and sizep (oldlenp from sysctl */
1370 	/* is not NULL then the caller wants us to return the length needed to */
1371 	/* hold the data we would return */
1372 	if (where != USER_ADDR_NULL && (buflen <= 0 || buflen > ARG_MAX)) {
1373 		error = EINVAL;
1374 		goto finish;
1375 	}
1376 
1377 	/*
1378 	 *	Lookup process by pid
1379 	 */
1380 	pid = name[0];
1381 	p = proc_find(pid);
1382 	if (p == NULL) {
1383 		error = EINVAL;
1384 		goto finish;
1385 	}
1386 
1387 	/* Allow reading environment variables if any of the following are true:
1388 	 * - kernel is DEVELOPMENT || DEBUG
1389 	 * - target process is same as current_proc()
1390 	 * - target process is not cs_restricted
1391 	 * - SIP is off
1392 	 * - caller has an entitlement
1393 	 */
1394 
1395 #if DEVELOPMENT || DEBUG
1396 	omit_env_vars = false;
1397 #endif
1398 	if (p == current_proc() ||
1399 	    !cs_restricted(p) ||
1400 #if CONFIG_CSR
1401 	    csr_check(CSR_ALLOW_UNRESTRICTED_DTRACE) == 0 ||
1402 #endif
1403 	    IOCurrentTaskHasEntitlement(SYSCTL_PROCARGS_READ_ENVVARS_ENTITLEMENT)
1404 	    ) {
1405 		omit_env_vars = false;
1406 	}
1407 
1408 	/*
1409 	 *	Copy the top N bytes of the stack.
1410 	 *	On all machines we have so far, the stack grows
1411 	 *	downwards.
1412 	 *
1413 	 *	If the user expects no more than N bytes of
1414 	 *	argument list, use that as a guess for the
1415 	 *	size.
1416 	 */
1417 
1418 	if (!p->user_stack) {
1419 		error = EINVAL;
1420 		goto finish;
1421 	}
1422 
1423 	/* save off argc, argslen, user_stack before releasing the proc */
1424 	argc = p->p_argc;
1425 	argslen = p->p_argslen;
1426 	user_stack = p->user_stack;
1427 
1428 	/*
1429 	 * When these sysctls were introduced, the first string in the strings
1430 	 * section was just the bare path of the executable.  However, for security
1431 	 * reasons we now prefix this string with executable_path= so it can be
1432 	 * parsed getenv style.  To avoid binary compatability issues with exising
1433 	 * callers of this sysctl, we strip it off here.
1434 	 * (rdar://problem/13746466)
1435 	 */
1436 #define        EXECUTABLE_KEY "executable_path="
1437 	argslen -= strlen(EXECUTABLE_KEY);
1438 
1439 	if (where == USER_ADDR_NULL && !omit_env_vars) {
1440 		/* caller only wants to know length of proc args data.
1441 		 * If we don't need to omit environment variables, we can skip
1442 		 * copying the target process stack */
1443 		goto calculate_size;
1444 	}
1445 
1446 	smr_proc_task_enter();
1447 	uid = kauth_cred_getuid(proc_ucred_smr(p));
1448 	smr_proc_task_leave();
1449 
1450 	if ((uid != kauth_cred_getuid(kauth_cred_get()))
1451 	    && suser(kauth_cred_get(), &cur_proc->p_acflag)) {
1452 		error = EINVAL;
1453 		goto finish;
1454 	}
1455 
1456 	/*
1457 	 *	Before we can block (any VM code), make another
1458 	 *	reference to the map to keep it alive.  We do
1459 	 *	that by getting a reference on the task itself.
1460 	 *
1461 	 *	Additionally, if the task is not IPC active, we
1462 	 *	must fail early. Other tasks can't yet look up
1463 	 *	this task's task port to make Mach API calls, so
1464 	 *	we shouldn't make such calls on their behalf.
1465 	 */
1466 	task = proc_task(p);
1467 	if (task == NULL || !task_is_ipc_active(task)) {
1468 		error = EINVAL;
1469 		goto finish;
1470 	}
1471 
1472 	/*
1473 	 * Once we have a task reference we can convert that into a
1474 	 * map reference, which we will use in the calls below.  The
1475 	 * task/process may change its map after we take this reference
1476 	 * (see execve), but the worst that will happen then is a return
1477 	 * of stale info (which is always a possibility).
1478 	 */
1479 	task_reference(task);
1480 	proc_rele(p);
1481 	p = NULL;
1482 	proc_map = get_task_map_reference(task);
1483 	task_deallocate(task);
1484 
1485 	if (proc_map == NULL) {
1486 		error = EINVAL;
1487 		goto finish;
1488 	}
1489 
1490 	effective_page_mask = vm_map_page_mask(proc_map);
1491 
1492 	arg_size = vm_map_round_page(argslen, effective_page_mask);
1493 
1494 	arg_addr = user_stack - arg_size;
1495 
1496 	ret = kmem_alloc(kernel_map, &copy_start, arg_size,
1497 	    KMA_DATA | KMA_ZERO, VM_KERN_MEMORY_BSD);
1498 	if (ret != KERN_SUCCESS) {
1499 		error = ENOMEM;
1500 		goto finish;
1501 	}
1502 
1503 	copy_end = copy_start + arg_size;
1504 
1505 	if (vm_map_copyin(proc_map, (vm_map_address_t)arg_addr,
1506 	    (vm_map_size_t)arg_size, FALSE, &tmp) != KERN_SUCCESS) {
1507 		error = EIO;
1508 		goto finish;
1509 	}
1510 
1511 	/*
1512 	 *	Now that we've done the copyin from the process'
1513 	 *	map, we can release the reference to it.
1514 	 */
1515 	vm_map_deallocate(proc_map);
1516 	proc_map = NULL;
1517 
1518 	if (vm_map_copy_overwrite(kernel_map,
1519 	    (vm_map_address_t)copy_start,
1520 	    tmp, (vm_map_size_t) arg_size,
1521 	    FALSE) != KERN_SUCCESS) {
1522 		error = EIO;
1523 		goto finish;
1524 	}
1525 	/* tmp was consumed */
1526 	tmp = NULL;
1527 
1528 	if (omit_env_vars) {
1529 		argvsize = 0;
1530 
1531 		/* Iterate over everything in argv, plus one for the bare executable path */
1532 		for (current_arg_index = 0; current_arg_index < argc + 1 && argvsize < argslen; ++current_arg_index) {
1533 			current_arg = (const char *)(copy_end - argslen) + argvsize;
1534 			remaining = argslen - argvsize;
1535 			current_arg_len = strnlen(current_arg, remaining);
1536 			if (current_arg_len < remaining) {
1537 				/* We have space for the null terminator */
1538 				current_arg_len += 1;
1539 
1540 				if (current_arg_index == 0) {
1541 					/* The bare executable path may have multiple null bytes after it for alignment */
1542 					while (current_arg_len < remaining && current_arg[current_arg_len] == 0) {
1543 						current_arg_len += 1;
1544 					}
1545 				}
1546 			}
1547 			argvsize += current_arg_len;
1548 		}
1549 		assert(argvsize <= argslen);
1550 
1551 		/* Adjust argslen and copy_end to make the copyout range extend to the end of argv */
1552 		copy_end = copy_end - argslen + argvsize;
1553 		argslen = argvsize;
1554 	}
1555 
1556 	if (where == USER_ADDR_NULL) {
1557 		/* Skip copyout */
1558 		goto calculate_size;
1559 	}
1560 
1561 	if (buflen >= argslen) {
1562 		data = (caddr_t) (copy_end - argslen);
1563 		size = argslen;
1564 	} else {
1565 		/*
1566 		 * Before rdar://25397314, this function contained incorrect logic when buflen is less
1567 		 * than argslen. The problem was that it copied in `buflen` bytes from the end of the target
1568 		 * process user stack into the beginning of a buffer of size round_page(buflen), and then
1569 		 * copied out `buflen` bytes from the end of this buffer. The effect of this was that
1570 		 * the caller of this sysctl would get zeros at the end of their buffer.
1571 		 *
1572 		 * To preserve this behavior, bzero everything from copy_end-round_page(buflen)+buflen to the
1573 		 * end of the buffer. This emulates copying in only `buflen` bytes.
1574 		 *
1575 		 *
1576 		 * In the old code:
1577 		 *
1578 		 *   copy_start     .... size: round_page(buflen) ....        copy_end
1579 		 *      [---copied in data (size: buflen)---|--- zeros ----------]
1580 		 *                           ^
1581 		 *                          data = copy_end - buflen
1582 		 *
1583 		 *
1584 		 * In the new code:
1585 		 *   copy_start        .... size: round_page(p->argslen) ....                full copy_end
1586 		 *      ^         ....................... p->argslen ...............................^
1587 		 *      ^         ^                                         truncated copy_end      ^
1588 		 *      ^         ^                                                 ^               ^
1589 		 *      ^         ................  argslen  ........................               ^
1590 		 *      ^         ^                                                 ^               ^
1591 		 *      [-------copied in data (size: round_page(p->argslen))-------:----env vars---]
1592 		 *                                ^            ^
1593 		 *                                ^         data = copy_end - buflen
1594 		 *                smallbuffer_start = max(copy_end - round_page(buflen), copy_start)
1595 		 *
1596 		 *
1597 		 * Full copy_end: copy_end calculated from copy_start + round_page(p->argslen)
1598 		 * Truncated copy_end: copy_end after truncation to remove environment variables.
1599 		 *
1600 		 * If environment variables were omitted, then we use the truncated copy_end, otherwise
1601 		 * we use full copy_end.
1602 		 *
1603 		 * smallbuffer_start: represents where copy_start would be in the old code.
1604 		 * data: The beginning of the region we copyout
1605 		 */
1606 		smallbuffer_start = copy_end - vm_map_round_page(buflen, effective_page_mask);
1607 		if (smallbuffer_start < copy_start) {
1608 			smallbuffer_start = copy_start;
1609 		}
1610 		bzero((void *)(smallbuffer_start + buflen), copy_end - (smallbuffer_start + buflen));
1611 		data = (caddr_t) (copy_end - buflen);
1612 		size = buflen;
1613 	}
1614 
1615 	if (argc_yes) {
1616 		/* Put processes argc as the first word in the copyout buffer */
1617 		suword(where, argc);
1618 		error = copyout(data, (where + sizeof(int)), size);
1619 		size += sizeof(int);
1620 	} else {
1621 		error = copyout(data, where, size);
1622 
1623 		/*
1624 		 * Make the old PROCARGS work to return the executable's path
1625 		 * But, only if there is enough space in the provided buffer
1626 		 *
1627 		 * on entry: data [possibily] points to the beginning of the path
1628 		 *
1629 		 * Note: we keep all pointers&sizes aligned to word boundries
1630 		 */
1631 		if ((!error) && (buflen > 0 && (u_int)buflen > size)) {
1632 			int binPath_sz, alignedBinPath_sz = 0;
1633 			int extraSpaceNeeded, addThis;
1634 			user_addr_t placeHere;
1635 			char * str = (char *) data;
1636 			size_t max_len = size;
1637 
1638 			/* Some apps are really bad about messing up their stacks
1639 			 *  So, we have to be extra careful about getting the length
1640 			 *  of the executing binary.  If we encounter an error, we bail.
1641 			 */
1642 
1643 			/* Limit ourselves to PATH_MAX paths */
1644 			if (max_len > PATH_MAX) {
1645 				max_len = PATH_MAX;
1646 			}
1647 
1648 			binPath_sz = 0;
1649 
1650 			while ((binPath_sz < max_len - 1) && (*str++ != 0)) {
1651 				binPath_sz++;
1652 			}
1653 
1654 			/* If we have a NUL terminator, copy it, too */
1655 			if (binPath_sz < max_len - 1) {
1656 				binPath_sz += 1;
1657 			}
1658 
1659 			/* Pre-Flight the space requiremnts */
1660 
1661 			/* Account for the padding that fills out binPath to the next word */
1662 			alignedBinPath_sz += (binPath_sz & (sizeof(int) - 1)) ? (sizeof(int) - (binPath_sz & (sizeof(int) - 1))) : 0;
1663 
1664 			placeHere = where + size;
1665 
1666 			/* Account for the bytes needed to keep placeHere word aligned */
1667 			addThis = (placeHere & (sizeof(int) - 1)) ? (sizeof(int) - (placeHere & (sizeof(int) - 1))) : 0;
1668 
1669 			/* Add up all the space that is needed */
1670 			extraSpaceNeeded = alignedBinPath_sz + addThis + binPath_sz + (4 * sizeof(int));
1671 
1672 			/* is there is room to tack on argv[0]? */
1673 			if ((buflen & ~(sizeof(int) - 1)) >= (size + extraSpaceNeeded)) {
1674 				placeHere += addThis;
1675 				suword(placeHere, 0);
1676 				placeHere += sizeof(int);
1677 				suword(placeHere, 0xBFFF0000);
1678 				placeHere += sizeof(int);
1679 				suword(placeHere, 0);
1680 				placeHere += sizeof(int);
1681 				error = copyout(data, placeHere, binPath_sz);
1682 				if (!error) {
1683 					placeHere += binPath_sz;
1684 					suword(placeHere, 0);
1685 					size += extraSpaceNeeded;
1686 				}
1687 			}
1688 		}
1689 	}
1690 
1691 calculate_size:
1692 	/* Size has already been calculated for the where != NULL case */
1693 	if (where == USER_ADDR_NULL) {
1694 		size = argslen;
1695 		if (argc_yes) {
1696 			size += sizeof(int);
1697 		} else {
1698 			/*
1699 			 * old PROCARGS will return the executable's path and plus some
1700 			 * extra space for work alignment and data tags
1701 			 */
1702 			size += PATH_MAX + (6 * sizeof(int));
1703 		}
1704 		size += (size & (sizeof(int) - 1)) ? (sizeof(int) - (size & (sizeof(int) - 1))) : 0;
1705 	}
1706 
1707 	*sizep = size;
1708 
1709 finish:
1710 	if (p != NULL) {
1711 		proc_rele(p);
1712 	}
1713 	if (tmp != NULL) {
1714 		vm_map_copy_discard(tmp);
1715 	}
1716 	if (proc_map != NULL) {
1717 		vm_map_deallocate(proc_map);
1718 	}
1719 	if (copy_start != (vm_offset_t) 0) {
1720 		kmem_free(kernel_map, copy_start, arg_size);
1721 	}
1722 	return error;
1723 }
1724 
1725 
1726 /*
1727  * Max number of concurrent aio requests
1728  */
1729 STATIC int
1730 sysctl_aiomax
1731 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1732 {
1733 	int new_value, changed;
1734 	int error = sysctl_io_number(req, aio_max_requests, sizeof(int), &new_value, &changed);
1735 	if (changed) {
1736 		/* make sure the system-wide limit is greater than the per process limit */
1737 		if (new_value >= aio_max_requests_per_process && new_value <= AIO_MAX_REQUESTS) {
1738 			aio_max_requests = new_value;
1739 		} else {
1740 			error = EINVAL;
1741 		}
1742 	}
1743 	return error;
1744 }
1745 
1746 
1747 /*
1748  * Max number of concurrent aio requests per process
1749  */
1750 STATIC int
1751 sysctl_aioprocmax
1752 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1753 {
1754 	int new_value, changed;
1755 	int error = sysctl_io_number(req, aio_max_requests_per_process, sizeof(int), &new_value, &changed);
1756 	if (changed) {
1757 		/* make sure per process limit is less than the system-wide limit */
1758 		if (new_value <= aio_max_requests && new_value >= AIO_LISTIO_MAX) {
1759 			aio_max_requests_per_process = new_value;
1760 		} else {
1761 			error = EINVAL;
1762 		}
1763 	}
1764 	return error;
1765 }
1766 
1767 
1768 /*
1769  * Max number of async IO worker threads
1770  */
1771 STATIC int
1772 sysctl_aiothreads
1773 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1774 {
1775 	int new_value, changed;
1776 	int error = sysctl_io_number(req, aio_worker_threads, sizeof(int), &new_value, &changed);
1777 	if (changed) {
1778 		/* we only allow an increase in the number of worker threads */
1779 		if (new_value > aio_worker_threads) {
1780 			_aio_create_worker_threads((new_value - aio_worker_threads));
1781 			aio_worker_threads = new_value;
1782 		} else {
1783 			error = EINVAL;
1784 		}
1785 	}
1786 	return error;
1787 }
1788 
1789 
1790 /*
1791  * System-wide limit on the max number of processes
1792  */
1793 STATIC int
1794 sysctl_maxproc
1795 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1796 {
1797 	int new_value, changed;
1798 	int error = sysctl_io_number(req, maxproc, sizeof(int), &new_value, &changed);
1799 	if (changed) {
1800 		AUDIT_ARG(value32, new_value);
1801 		/* make sure the system-wide limit is less than the configured hard
1802 		 *  limit set at kernel compilation */
1803 		if (new_value <= hard_maxproc && new_value > 0) {
1804 			maxproc = new_value;
1805 		} else {
1806 			error = EINVAL;
1807 		}
1808 	}
1809 	return error;
1810 }
1811 
1812 #if CONFIG_SCHED_SMT
1813 STATIC int
1814 sysctl_sched_enable_smt
1815 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1816 {
1817 	int new_value, changed;
1818 	int error = sysctl_io_number(req, sched_enable_smt, sizeof(int), &new_value, &changed);
1819 	if (error) {
1820 		return error;
1821 	}
1822 	kern_return_t kret = KERN_SUCCESS;
1823 	if (changed) {
1824 		AUDIT_ARG(value32, new_value);
1825 		if (new_value == 0) {
1826 			sched_enable_smt = 0;
1827 			kret = enable_smt_processors(false);
1828 		} else {
1829 			sched_enable_smt = 1;
1830 			kret = enable_smt_processors(true);
1831 		}
1832 	}
1833 	switch (kret) {
1834 	case KERN_SUCCESS:
1835 		error = 0;
1836 		break;
1837 	case KERN_INVALID_ARGUMENT:
1838 		error = EINVAL;
1839 		break;
1840 	case KERN_FAILURE:
1841 		error = EBUSY;
1842 		break;
1843 	default:
1844 		error = ENOENT;
1845 		break;
1846 	}
1847 	return error;
1848 }
1849 #else /* CONFIG_SCHED_SMT */
1850 STATIC int
1851 sysctl_sched_enable_smt
1852 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, __unused struct sysctl_req *req)
1853 {
1854 	return 0;
1855 }
1856 #endif /* CONFIG_SCHED_SMT */
1857 
1858 SYSCTL_STRING(_kern, KERN_OSTYPE, ostype,
1859     CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1860     ostype, 0, "");
1861 SYSCTL_STRING(_kern, KERN_OSRELEASE, osrelease,
1862     CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1863     osrelease, 0, "");
1864 SYSCTL_INT(_kern, KERN_OSREV, osrevision,
1865     CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1866     (int *)NULL, BSD, "");
1867 SYSCTL_STRING(_kern, KERN_VERSION, version,
1868     CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1869     version, 0, "");
1870 SYSCTL_STRING(_kern, OID_AUTO, uuid,
1871     CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1872     &kernel_uuid_string[0], 0, "");
1873 
1874 SYSCTL_STRING(_kern, OID_AUTO, osbuildconfig,
1875     CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED | CTLFLAG_MASKED,
1876     &osbuild_config[0], 0, "");
1877 
1878 #if DEBUG
1879 #ifndef DKPR
1880 #define DKPR 1
1881 #endif
1882 #endif
1883 
1884 #if DKPR
1885 int debug_kprint_syscall = 0;
1886 char debug_kprint_syscall_process[MAXCOMLEN + 1];
1887 
1888 /* Thread safe: bits and string value are not used to reclaim state */
1889 SYSCTL_INT(_debug, OID_AUTO, kprint_syscall,
1890     CTLFLAG_RW | CTLFLAG_LOCKED, &debug_kprint_syscall, 0, "kprintf syscall tracing");
1891 SYSCTL_STRING(_debug, OID_AUTO, kprint_syscall_process,
1892     CTLFLAG_RW | CTLFLAG_LOCKED, debug_kprint_syscall_process, sizeof(debug_kprint_syscall_process),
1893     "name of process for kprintf syscall tracing");
1894 
1895 int
1896 debug_kprint_current_process(const char **namep)
1897 {
1898 	struct proc *p = current_proc();
1899 
1900 	if (p == NULL) {
1901 		return 0;
1902 	}
1903 
1904 	if (debug_kprint_syscall_process[0]) {
1905 		/* user asked to scope tracing to a particular process name */
1906 		if (0 == strncmp(debug_kprint_syscall_process,
1907 		    p->p_comm, sizeof(debug_kprint_syscall_process))) {
1908 			/* no value in telling the user that we traced what they asked */
1909 			if (namep) {
1910 				*namep = NULL;
1911 			}
1912 
1913 			return 1;
1914 		} else {
1915 			return 0;
1916 		}
1917 	}
1918 
1919 	/* trace all processes. Tell user what we traced */
1920 	if (namep) {
1921 		*namep = p->p_comm;
1922 	}
1923 
1924 	return 1;
1925 }
1926 #endif
1927 
1928 /* PR-5293665: need to use a callback function for kern.osversion to set
1929  * osversion in IORegistry */
1930 
1931 STATIC int
1932 sysctl_osversion(__unused struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req)
1933 {
1934 	int rval = 0;
1935 
1936 	rval = sysctl_handle_string(oidp, arg1, arg2, req);
1937 
1938 	if (req->newptr) {
1939 		IORegistrySetOSBuildVersion((char *)arg1);
1940 	}
1941 
1942 	return rval;
1943 }
1944 
1945 SYSCTL_PROC(_kern, KERN_OSVERSION, osversion,
1946     CTLFLAG_RW | CTLFLAG_KERN | CTLTYPE_STRING | CTLFLAG_LOCKED,
1947     osversion, 256 /* OSVERSIZE*/,
1948     sysctl_osversion, "A", "");
1949 
1950 static bool
1951 _already_set_or_not_launchd(struct sysctl_req *req, char *val)
1952 {
1953 	if (req->newptr != 0) {
1954 		/*
1955 		 * Can only ever be set by launchd, and only once at boot.
1956 		 */
1957 		if (proc_getpid(req->p) != 1 || val[0] != '\0') {
1958 			return true;
1959 		}
1960 	}
1961 	return false;
1962 }
1963 
1964 #define kRootsInstalledReadWriteEntitlement "com.apple.private.roots-installed-read-write"
1965 #define kRootsInstalledReadOnlyEntitlement "com.apple.private.roots-installed-read-only"
1966 uint64_t roots_installed = 0;
1967 
1968 static int
1969 sysctl_roots_installed
1970 (__unused struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req)
1971 {
1972 	int error = 0;
1973 
1974 	if (req->newptr != 0) {
1975 		/* a ReadWrite entitlement is required for updating this syscl
1976 		 * meanwhile, only allow write once
1977 		 */
1978 		if (!IOCurrentTaskHasEntitlement(kRootsInstalledReadWriteEntitlement) || (roots_installed != 0)) {
1979 			return EPERM;
1980 		}
1981 	} else {
1982 		/* for reader of this sysctl, need either ReadWrite or ReadOnly entitlement */
1983 		if (!IOCurrentTaskHasEntitlement(kRootsInstalledReadWriteEntitlement) &&
1984 		    !IOCurrentTaskHasEntitlement(kRootsInstalledReadOnlyEntitlement)) {
1985 			return EPERM;
1986 		}
1987 	}
1988 
1989 	error = sysctl_handle_quad(oidp, arg1, arg2, req);
1990 
1991 	return error;
1992 }
1993 
1994 SYSCTL_PROC(_kern, OID_AUTO, roots_installed,
1995     CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_LOCKED,
1996     &roots_installed, sizeof(roots_installed),
1997     sysctl_roots_installed, "Q", "");
1998 
1999 #if XNU_TARGET_OS_OSX
2000 static int
2001 sysctl_system_version_compat
2002 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2003 {
2004 	int oldval = (task_has_system_version_compat_enabled(current_task()));
2005 	int new_value = 0, changed = 0;
2006 
2007 	int error = sysctl_io_number(req, oldval, sizeof(int), &new_value, &changed);
2008 	if (changed) {
2009 		task_set_system_version_compat_enabled(current_task(), (new_value));
2010 	}
2011 	return error;
2012 }
2013 
2014 SYSCTL_PROC(_kern, OID_AUTO, system_version_compat,
2015     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_LOCKED,
2016     0, 0, sysctl_system_version_compat, "A", "");
2017 #endif /* XNU_TARGET_OS_OSX */
2018 
2019 #if XNU_TARGET_OS_OSX || defined(XNU_EXPERIMENTAL_SYSTEM_VERSION_COMPAT)
2020 char osproductversioncompat[48] = { '\0' };
2021 
2022 static int
2023 sysctl_osproductversioncompat(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req)
2024 {
2025 	if (_already_set_or_not_launchd(req, osproductversioncompat)) {
2026 		return EPERM;
2027 	}
2028 	return sysctl_handle_string(oidp, arg1, arg2, req);
2029 }
2030 
2031 
2032 SYSCTL_PROC(_kern, OID_AUTO, osproductversioncompat,
2033     CTLFLAG_RW | CTLFLAG_KERN | CTLTYPE_STRING | CTLFLAG_LOCKED,
2034     osproductversioncompat, sizeof(osproductversioncompat),
2035     sysctl_osproductversioncompat, "A", "The ProductVersion from SystemVersionCompat.plist");
2036 #endif /* XNU_TARGET_OS_OSX || defined(XNU_EXPERIMENTAL_SYSTEM_VERSION_COMPAT) */
2037 
2038 char osproductversion[48] = { '\0' };
2039 
2040 static char iossupportversion_string[48] = { '\0' };
2041 
2042 #if defined(XNU_EXPERIMENTAL_SYSTEM_VERSION_COMPAT)
2043 /*
2044  * Equivalent to dyld_program_sdk_at_least(dyld_fall_2025_os_versions).
2045  */
2046 static bool
2047 proc_2025_fall_os_sdk_or_later(struct proc *p)
2048 {
2049 	const uint32_t proc_sdk_ver = proc_sdk(p);
2050 
2051 	switch (proc_platform(p)) {
2052 	case PLATFORM_MACOS:
2053 		return proc_sdk_ver >= 0x00100000; // DYLD_MACOSX_VERSION_16_0
2054 	case PLATFORM_IOS:
2055 	case PLATFORM_IOSSIMULATOR:
2056 	case PLATFORM_MACCATALYST:
2057 		return proc_sdk_ver >= 0x00130000; // DYLD_IOS_VERSION_19_0
2058 	case PLATFORM_BRIDGEOS:
2059 		return proc_sdk_ver >= 0x000a0000; // DYLD_BRIDGEOS_VERSION_10_0
2060 	case PLATFORM_TVOS:
2061 	case PLATFORM_TVOSSIMULATOR:
2062 		return proc_sdk_ver >= 0x00130000; // DYLD_TVOS_VERSION_19_0
2063 	case PLATFORM_WATCHOS:
2064 	case PLATFORM_WATCHOSSIMULATOR:
2065 		return proc_sdk_ver >= 0x000c0000; // DYLD_WATCHOS_VERSION_12_0
2066 	default:
2067 		return true;
2068 	}
2069 }
2070 #endif /* defined(XNU_EXPERIMENTAL_SYSTEM_VERSION_COMPAT) */
2071 
2072 static int
2073 sysctl_osproductversion(__unused struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req)
2074 {
2075 	if (_already_set_or_not_launchd(req, osproductversion)) {
2076 		return EPERM;
2077 	}
2078 
2079 #if XNU_TARGET_OS_OSX
2080 	if (task_has_system_version_compat_enabled(current_task()) && (osproductversioncompat[0] != '\0')) {
2081 		return sysctl_handle_string(oidp, osproductversioncompat, arg2, req);
2082 	}
2083 #endif /* XNU_TARGET_OS_OSX */
2084 
2085 #if defined(XNU_TARGET_OS_XR)
2086 	if (proc_platform(req->p) == PLATFORM_IOS && (iossupportversion_string[0] != '\0')) {
2087 		return sysctl_handle_string(oidp, iossupportversion_string, arg2, req);
2088 	}
2089 #endif /* defined(XNU_TARGET_OS_XR) */
2090 
2091 #if defined(XNU_EXPERIMENTAL_SYSTEM_VERSION_COMPAT)
2092 	if (!proc_2025_fall_os_sdk_or_later(req->p) && (osproductversioncompat[0] != '\0')) {
2093 		return sysctl_handle_string(oidp, osproductversioncompat, arg2, req);
2094 	}
2095 #endif /* defined(XNU_EXPERIMENTAL_SYSTEM_VERSION_COMPAT) */
2096 
2097 	return sysctl_handle_string(oidp, arg1, arg2, req);
2098 }
2099 
2100 #if XNU_TARGET_OS_OSX
2101 static_assert(sizeof(osproductversioncompat) == sizeof(osproductversion),
2102     "osproductversion size matches osproductversioncompat size");
2103 #endif
2104 
2105 SYSCTL_PROC(_kern, OID_AUTO, osproductversion,
2106     CTLFLAG_RW | CTLFLAG_KERN | CTLTYPE_STRING | CTLFLAG_LOCKED,
2107     osproductversion, sizeof(osproductversion),
2108     sysctl_osproductversion, "A", "The ProductVersion from SystemVersion.plist");
2109 
2110 char osreleasetype[OSRELEASETYPE_SIZE] = { '\0' };
2111 
2112 STATIC int
2113 sysctl_osreleasetype(__unused struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req)
2114 {
2115 	if (_already_set_or_not_launchd(req, osreleasetype)) {
2116 		return EPERM;
2117 	}
2118 	return sysctl_handle_string(oidp, arg1, arg2, req);
2119 }
2120 
2121 void reset_osreleasetype(void);
2122 
2123 void
2124 reset_osreleasetype(void)
2125 {
2126 	memset(osreleasetype, 0, sizeof(osreleasetype));
2127 }
2128 
2129 SYSCTL_PROC(_kern, OID_AUTO, osreleasetype,
2130     CTLFLAG_RW | CTLFLAG_KERN | CTLTYPE_STRING | CTLFLAG_LOCKED,
2131     osreleasetype, sizeof(osreleasetype),
2132     sysctl_osreleasetype, "A", "The ReleaseType from SystemVersion.plist");
2133 
2134 STATIC int
2135 sysctl_iossupportversion(__unused struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req)
2136 {
2137 	if (_already_set_or_not_launchd(req, iossupportversion_string)) {
2138 		return EPERM;
2139 	}
2140 
2141 #if defined(XNU_TARGET_OS_XR)
2142 	if (proc_platform(req->p) == PLATFORM_IOS) {
2143 		/* return empty string for iOS processes to match how this would behave on iOS */
2144 		return sysctl_handle_string(oidp, "", arg2, req);
2145 	} else {
2146 		/* native processes see the actual value */
2147 		return sysctl_handle_string(oidp, arg1, arg2, req);
2148 	}
2149 #else
2150 	return sysctl_handle_string(oidp, arg1, arg2, req);
2151 #endif
2152 }
2153 
2154 SYSCTL_PROC(_kern, OID_AUTO, iossupportversion,
2155     CTLFLAG_RW | CTLFLAG_KERN | CTLTYPE_STRING | CTLFLAG_LOCKED,
2156     iossupportversion_string, sizeof(iossupportversion_string),
2157     sysctl_iossupportversion, "A", "The iOSSupportVersion from SystemVersion.plist");
2158 
2159 static uint64_t osvariant_status = 0;
2160 
2161 STATIC int
2162 sysctl_osvariant_status(__unused struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req)
2163 {
2164 	if (req->newptr != 0) {
2165 		/*
2166 		 * Can only ever be set by launchd, and only once.
2167 		 * Reset by usrctl() -> reset_osvariant_status() during
2168 		 * userspace reboot, since userspace could reboot into
2169 		 * a different variant.
2170 		 */
2171 		if (proc_getpid(req->p) != 1 || osvariant_status != 0) {
2172 			return EPERM;
2173 		}
2174 	}
2175 
2176 	int err = sysctl_handle_quad(oidp, arg1, arg2, req);
2177 
2178 	reset_debug_syscall_rejection_mode();
2179 
2180 	return err;
2181 }
2182 
2183 SYSCTL_PROC(_kern, OID_AUTO, osvariant_status,
2184     CTLFLAG_RW | CTLTYPE_QUAD | CTLFLAG_LOCKED | CTLFLAG_MASKED,
2185     &osvariant_status, sizeof(osvariant_status),
2186     sysctl_osvariant_status, "Q", "Opaque flags used to cache OS variant information");
2187 
2188 static bool
2189 _os_variant_check_disabled(enum os_variant_property property)
2190 {
2191 	return (osvariant_status >> (32 + property)) & 0x1;
2192 }
2193 
2194 static bool
2195 _os_variant_has(enum os_variant_status_flags_positions p)
2196 {
2197 	return ((osvariant_status >> (p * OS_VARIANT_STATUS_BIT_WIDTH)) & OS_VARIANT_STATUS_MASK) == OS_VARIANT_S_YES;
2198 }
2199 
2200 bool
2201 os_variant_has_internal_diagnostics(__unused const char *subsystem)
2202 {
2203 	if (_os_variant_check_disabled(OS_VARIANT_PROPERTY_DIAGNOSTICS)) {
2204 		return false;
2205 	}
2206 #if XNU_TARGET_OS_OSX
2207 	return _os_variant_has(OS_VARIANT_SFP_INTERNAL_CONTENT) || _os_variant_has(OS_VARIANT_SFP_INTERNAL_DIAGS_PROFILE);
2208 #else
2209 	return _os_variant_has(OS_VARIANT_SFP_INTERNAL_RELEASE_TYPE);
2210 #endif /* XNU_TARGET_OS_OSX */
2211 }
2212 
2213 void reset_osvariant_status(void);
2214 
2215 void
2216 reset_osvariant_status(void)
2217 {
2218 	osvariant_status = 0;
2219 	reset_debug_syscall_rejection_mode();
2220 }
2221 
2222 extern void commpage_update_dyld_flags(uint64_t);
2223 TUNABLE_WRITEABLE(uint64_t, dyld_flags, "dyld_flags", 0);
2224 
2225 STATIC int
2226 sysctl_dyld_flags(__unused struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req)
2227 {
2228 	/*
2229 	 * Can only ever be set by launchd, possibly several times
2230 	 * as dyld may change its mind after a userspace reboot.
2231 	 */
2232 	if (req->newptr != 0 && proc_getpid(req->p) != 1) {
2233 		return EPERM;
2234 	}
2235 
2236 	int res = sysctl_handle_quad(oidp, arg1, arg2, req);
2237 	if (req->newptr && res == 0) {
2238 		commpage_update_dyld_flags(dyld_flags);
2239 	}
2240 	return res;
2241 }
2242 
2243 SYSCTL_PROC(_kern, OID_AUTO, dyld_flags,
2244     CTLFLAG_RW | CTLTYPE_QUAD | CTLFLAG_LOCKED | CTLFLAG_MASKED,
2245     &dyld_flags, sizeof(dyld_flags),
2246     sysctl_dyld_flags, "Q", "Opaque flags used to cache dyld system-wide configuration");
2247 
2248 #if defined(XNU_TARGET_OS_BRIDGE)
2249 char macosproductversion[MACOS_VERS_LEN] = { '\0' };
2250 
2251 SYSCTL_STRING(_kern, OID_AUTO, macosproductversion,
2252     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
2253     &macosproductversion[0], MACOS_VERS_LEN, "The currently running macOS ProductVersion (from SystemVersion.plist on macOS)");
2254 
2255 char macosversion[MACOS_VERS_LEN] = { '\0' };
2256 
2257 SYSCTL_STRING(_kern, OID_AUTO, macosversion,
2258     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
2259     &macosversion[0], MACOS_VERS_LEN, "The currently running macOS build version");
2260 #endif
2261 
2262 STATIC int
2263 sysctl_sysctl_bootargs
2264 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2265 {
2266 	int error;
2267 	char buf[BOOT_LINE_LENGTH];
2268 
2269 	strlcpy(buf, PE_boot_args(), BOOT_LINE_LENGTH);
2270 	error = sysctl_io_string(req, buf, BOOT_LINE_LENGTH, 0, NULL);
2271 	return error;
2272 }
2273 
2274 SYSCTL_PROC(_kern, OID_AUTO, bootargs,
2275     CTLFLAG_LOCKED | CTLFLAG_RD | CTLFLAG_KERN | CTLTYPE_STRING,
2276     NULL, 0,
2277     sysctl_sysctl_bootargs, "A", "bootargs");
2278 
2279 STATIC int
2280 sysctl_kernelcacheuuid(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req)
2281 {
2282 	int rval = ENOENT;
2283 	if (kernelcache_uuid_valid) {
2284 		rval = sysctl_handle_string(oidp, arg1, arg2, req);
2285 	}
2286 	return rval;
2287 }
2288 
2289 SYSCTL_PROC(_kern, OID_AUTO, kernelcacheuuid,
2290     CTLFLAG_RD | CTLFLAG_KERN | CTLTYPE_STRING | CTLFLAG_LOCKED,
2291     kernelcache_uuid_string, sizeof(kernelcache_uuid_string),
2292     sysctl_kernelcacheuuid, "A", "");
2293 
2294 STATIC int
2295 sysctl_systemfilesetuuid(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req)
2296 {
2297 	int rval = ENOENT;
2298 	if (pageablekc_uuid_valid) {
2299 		rval = sysctl_handle_string(oidp, arg1, arg2, req);
2300 	}
2301 	return rval;
2302 }
2303 
2304 SYSCTL_PROC(_kern, OID_AUTO, systemfilesetuuid,
2305     CTLFLAG_RD | CTLFLAG_KERN | CTLTYPE_STRING | CTLFLAG_LOCKED,
2306     pageablekc_uuid_string, sizeof(pageablekc_uuid_string),
2307     sysctl_systemfilesetuuid, "A", "");
2308 
2309 STATIC int
2310 sysctl_auxiliaryfilesetuuid(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req)
2311 {
2312 	int rval = ENOENT;
2313 	if (auxkc_uuid_valid) {
2314 		rval = sysctl_handle_string(oidp, arg1, arg2, req);
2315 	}
2316 	return rval;
2317 }
2318 
2319 SYSCTL_PROC(_kern, OID_AUTO, auxiliaryfilesetuuid,
2320     CTLFLAG_RD | CTLFLAG_KERN | CTLTYPE_STRING | CTLFLAG_LOCKED,
2321     auxkc_uuid_string, sizeof(auxkc_uuid_string),
2322     sysctl_auxiliaryfilesetuuid, "A", "");
2323 
2324 STATIC int
2325 sysctl_filesetuuid(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2326 {
2327 	int rval = ENOENT;
2328 	kc_format_t kcformat;
2329 	kernel_mach_header_t *mh;
2330 	void *uuid = NULL;
2331 	unsigned long uuidlen = 0;
2332 	uuid_string_t uuid_str;
2333 
2334 	if (!PE_get_primary_kc_format(&kcformat) || kcformat != KCFormatFileset) {
2335 		return rval;
2336 	}
2337 
2338 	mh = (kernel_mach_header_t *)PE_get_kc_header(KCKindPrimary);
2339 	uuid = getuuidfromheader(mh, &uuidlen);
2340 
2341 	if ((uuid != NULL) && (uuidlen == sizeof(uuid_t))) {
2342 		uuid_unparse_upper(*(uuid_t *)uuid, uuid_str);
2343 		rval = sysctl_io_string(req, (char *)uuid_str, sizeof(uuid_str), 0, NULL);
2344 	}
2345 
2346 	return rval;
2347 }
2348 
2349 SYSCTL_PROC(_kern, OID_AUTO, filesetuuid,
2350     CTLFLAG_RD | CTLFLAG_KERN | CTLTYPE_STRING | CTLFLAG_LOCKED,
2351     NULL, 0,
2352     sysctl_filesetuuid, "A", "");
2353 
2354 
2355 SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles,
2356     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
2357     &maxfiles, 0, "");
2358 SYSCTL_INT(_kern, KERN_ARGMAX, argmax,
2359     CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
2360     (int *)NULL, ARG_MAX, "");
2361 SYSCTL_INT(_kern, KERN_POSIX1, posix1version,
2362     CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
2363     (int *)NULL, _POSIX_VERSION, "");
2364 SYSCTL_INT(_kern, KERN_NGROUPS, ngroups,
2365     CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
2366     (int *)NULL, NGROUPS_MAX, "");
2367 SYSCTL_INT(_kern, KERN_JOB_CONTROL, job_control,
2368     CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
2369     (int *)NULL, 1, "");
2370 #if 1   /* _POSIX_SAVED_IDS from <unistd.h> */
2371 SYSCTL_INT(_kern, KERN_SAVED_IDS, saved_ids,
2372     CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
2373     (int *)NULL, 1, "");
2374 #else
2375 SYSCTL_INT(_kern, KERN_SAVED_IDS, saved_ids,
2376     CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
2377     NULL, 0, "");
2378 #endif
2379 SYSCTL_INT(_kern, OID_AUTO, num_files,
2380     CTLFLAG_RD | CTLFLAG_LOCKED,
2381     &nfiles, 0, "");
2382 SYSCTL_COMPAT_INT(_kern, OID_AUTO, num_vnodes,
2383     CTLFLAG_RD | CTLFLAG_LOCKED,
2384     &numvnodes, 0, "");
2385 SYSCTL_INT(_kern, OID_AUTO, num_tasks,
2386     CTLFLAG_RD | CTLFLAG_LOCKED,
2387     &task_max, 0, "");
2388 SYSCTL_INT(_kern, OID_AUTO, num_threads,
2389     CTLFLAG_RD | CTLFLAG_LOCKED,
2390     &thread_max, 0, "");
2391 SYSCTL_INT(_kern, OID_AUTO, num_taskthreads,
2392     CTLFLAG_RD | CTLFLAG_LOCKED,
2393     &task_threadmax, 0, "");
2394 SYSCTL_LONG(_kern, OID_AUTO, num_recycledvnodes,
2395     CTLFLAG_RD | CTLFLAG_LOCKED,
2396     &num_recycledvnodes, "");
2397 SYSCTL_COMPAT_INT(_kern, OID_AUTO, free_vnodes,
2398     CTLFLAG_RD | CTLFLAG_LOCKED,
2399     &freevnodes, 0, "");
2400 
2401 STATIC int
2402 sysctl_maxvnodes(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2403 {
2404 	int oldval = desiredvnodes;
2405 	int error = sysctl_io_number(req, desiredvnodes, sizeof(int), &desiredvnodes, NULL);
2406 
2407 	if (oldval != desiredvnodes) {
2408 		resize_namecache(desiredvnodes);
2409 	}
2410 
2411 	return error;
2412 }
2413 
2414 SYSCTL_INT(_kern, OID_AUTO, namecache_disabled,
2415     CTLFLAG_RW | CTLFLAG_LOCKED,
2416     &nc_disabled, 0, "");
2417 
2418 SYSCTL_PROC(_kern, KERN_MAXVNODES, maxvnodes,
2419     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
2420     0, 0, sysctl_maxvnodes, "I", "");
2421 
2422 SYSCTL_PROC(_kern, KERN_MAXPROC, maxproc,
2423     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
2424     0, 0, sysctl_maxproc, "I", "");
2425 
2426 SYSCTL_PROC(_kern, KERN_AIOMAX, aiomax,
2427     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
2428     0, 0, sysctl_aiomax, "I", "");
2429 
2430 SYSCTL_PROC(_kern, KERN_AIOPROCMAX, aioprocmax,
2431     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
2432     0, 0, sysctl_aioprocmax, "I", "");
2433 
2434 SYSCTL_PROC(_kern, KERN_AIOTHREADS, aiothreads,
2435     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
2436     0, 0, sysctl_aiothreads, "I", "");
2437 
2438 SYSCTL_PROC(_kern, OID_AUTO, sched_enable_smt,
2439     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN,
2440     0, 0, sysctl_sched_enable_smt, "I", "");
2441 
2442 extern int sched_allow_NO_SMT_threads;
2443 SYSCTL_INT(_kern, OID_AUTO, sched_allow_NO_SMT_threads,
2444     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
2445     &sched_allow_NO_SMT_threads, 0, "");
2446 
2447 extern int sched_avoid_cpu0;
2448 SYSCTL_INT(_kern, OID_AUTO, sched_rt_avoid_cpu0,
2449     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
2450     &sched_avoid_cpu0, 0, "If 1, choose cpu0 after all other primaries; if 2, choose cpu0 and cpu1 last, after all other cpus including secondaries");
2451 
2452 #if (DEVELOPMENT || DEBUG)
2453 
2454 static int
2455 sysctl_kern_max_unsafe_rt_quanta(__unused struct sysctl_oid *oidp,
2456     __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2457 {
2458 	extern void sched_set_max_unsafe_rt_quanta(int);
2459 	extern int max_unsafe_rt_quanta;
2460 
2461 	int new_value, changed;
2462 	int old_value = max_unsafe_rt_quanta;
2463 	int error = sysctl_io_number(req, old_value, sizeof(int), &new_value,
2464 	    &changed);
2465 	if (changed) {
2466 		sched_set_max_unsafe_rt_quanta(new_value);
2467 	}
2468 
2469 	return error;
2470 }
2471 
2472 SYSCTL_PROC(_kern, OID_AUTO, max_unsafe_rt_quanta,
2473     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
2474     0, 0, sysctl_kern_max_unsafe_rt_quanta, "I",
2475     "Number of quanta to allow a realtime "
2476     "thread to run before being penalized");
2477 
2478 static int
2479 sysctl_kern_max_unsafe_fixed_quanta(__unused struct sysctl_oid *oidp,
2480     __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2481 {
2482 	extern void sched_set_max_unsafe_fixed_quanta(int);
2483 	extern int max_unsafe_fixed_quanta;
2484 
2485 	int new_value, changed;
2486 	int old_value = max_unsafe_fixed_quanta;
2487 	int error = sysctl_io_number(req, old_value, sizeof(int), &new_value,
2488 	    &changed);
2489 	if (changed) {
2490 		sched_set_max_unsafe_fixed_quanta(new_value);
2491 	}
2492 
2493 	return error;
2494 }
2495 
2496 SYSCTL_PROC(_kern, OID_AUTO, max_unsafe_fixed_quanta,
2497     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
2498     0, 0, sysctl_kern_max_unsafe_fixed_quanta, "I",
2499     "Number of quanta to allow a fixed sched mode "
2500     "thread to run before being penalized");
2501 
2502 static int
2503 sysctl_kern_quantum_us(__unused struct sysctl_oid *oidp, __unused void *arg1,
2504     __unused int arg2, struct sysctl_req *req)
2505 {
2506 	const uint64_t quantum_us = sched_get_quantum_us();
2507 
2508 	return sysctl_io_number(req, quantum_us, sizeof(quantum_us), NULL, NULL);
2509 }
2510 
2511 SYSCTL_PROC(_kern, OID_AUTO, quantum_us,
2512     CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED,
2513     0, 0, sysctl_kern_quantum_us, "Q",
2514     "Length of scheduling quantum in microseconds");
2515 
2516 extern int smt_sched_bonus_16ths;
2517 SYSCTL_INT(_kern, OID_AUTO, smt_sched_bonus_16ths,
2518     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
2519     &smt_sched_bonus_16ths, 0, "");
2520 
2521 extern int smt_timeshare_enabled;
2522 SYSCTL_INT(_kern, OID_AUTO, sched_smt_timeshare_enable,
2523     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
2524     &smt_timeshare_enabled, 0, "");
2525 
2526 extern int sched_smt_balance;
2527 SYSCTL_INT(_kern, OID_AUTO, sched_smt_balance,
2528     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
2529     &sched_smt_balance, 0, "");
2530 extern int sched_allow_rt_smt;
2531 SYSCTL_INT(_kern, OID_AUTO, sched_allow_rt_smt,
2532     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
2533     &sched_allow_rt_smt, 0, "");
2534 extern int sched_backup_cpu_timeout_count;
2535 SYSCTL_INT(_kern, OID_AUTO, sched_backup_cpu_timeout_count,
2536     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
2537     &sched_backup_cpu_timeout_count, 0, "The maximum number of 10us delays before allowing a backup cpu to select a thread");
2538 #if __arm64__
2539 /* Scheduler perfcontrol callouts sysctls */
2540 SYSCTL_DECL(_kern_perfcontrol_callout);
2541 SYSCTL_NODE(_kern, OID_AUTO, perfcontrol_callout, CTLFLAG_RW | CTLFLAG_LOCKED, 0,
2542     "scheduler perfcontrol callouts");
2543 
2544 extern int perfcontrol_callout_stats_enabled;
2545 SYSCTL_INT(_kern_perfcontrol_callout, OID_AUTO, stats_enabled,
2546     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
2547     &perfcontrol_callout_stats_enabled, 0, "");
2548 
2549 extern uint64_t perfcontrol_callout_stat_avg(perfcontrol_callout_type_t type,
2550     perfcontrol_callout_stat_t stat);
2551 
2552 /* On-Core Callout */
2553 STATIC int
2554 sysctl_perfcontrol_callout_stat
2555 (__unused struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req)
2556 {
2557 	perfcontrol_callout_stat_t stat = (perfcontrol_callout_stat_t)arg1;
2558 	perfcontrol_callout_type_t type = (perfcontrol_callout_type_t)arg2;
2559 	return sysctl_io_number(req, (int)perfcontrol_callout_stat_avg(type, stat),
2560 	           sizeof(int), NULL, NULL);
2561 }
2562 
2563 SYSCTL_PROC(_kern_perfcontrol_callout, OID_AUTO, oncore_instr,
2564     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
2565     (void *)PERFCONTROL_STAT_INSTRS, PERFCONTROL_CALLOUT_ON_CORE,
2566     sysctl_perfcontrol_callout_stat, "I", "");
2567 SYSCTL_PROC(_kern_perfcontrol_callout, OID_AUTO, oncore_cycles,
2568     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
2569     (void *)PERFCONTROL_STAT_CYCLES, PERFCONTROL_CALLOUT_ON_CORE,
2570     sysctl_perfcontrol_callout_stat, "I", "");
2571 SYSCTL_PROC(_kern_perfcontrol_callout, OID_AUTO, offcore_instr,
2572     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
2573     (void *)PERFCONTROL_STAT_INSTRS, PERFCONTROL_CALLOUT_OFF_CORE,
2574     sysctl_perfcontrol_callout_stat, "I", "");
2575 SYSCTL_PROC(_kern_perfcontrol_callout, OID_AUTO, offcore_cycles,
2576     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
2577     (void *)PERFCONTROL_STAT_CYCLES, PERFCONTROL_CALLOUT_OFF_CORE,
2578     sysctl_perfcontrol_callout_stat, "I", "");
2579 SYSCTL_PROC(_kern_perfcontrol_callout, OID_AUTO, context_instr,
2580     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
2581     (void *)PERFCONTROL_STAT_INSTRS, PERFCONTROL_CALLOUT_CONTEXT,
2582     sysctl_perfcontrol_callout_stat, "I", "");
2583 SYSCTL_PROC(_kern_perfcontrol_callout, OID_AUTO, context_cycles,
2584     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
2585     (void *)PERFCONTROL_STAT_CYCLES, PERFCONTROL_CALLOUT_CONTEXT,
2586     sysctl_perfcontrol_callout_stat, "I", "");
2587 SYSCTL_PROC(_kern_perfcontrol_callout, OID_AUTO, update_instr,
2588     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
2589     (void *)PERFCONTROL_STAT_INSTRS, PERFCONTROL_CALLOUT_STATE_UPDATE,
2590     sysctl_perfcontrol_callout_stat, "I", "");
2591 SYSCTL_PROC(_kern_perfcontrol_callout, OID_AUTO, update_cycles,
2592     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
2593     (void *)PERFCONTROL_STAT_CYCLES, PERFCONTROL_CALLOUT_STATE_UPDATE,
2594     sysctl_perfcontrol_callout_stat, "I", "");
2595 
2596 #if __AMP__
2597 #if !CONFIG_CLUTCH
2598 extern int sched_amp_idle_steal;
2599 SYSCTL_INT(_kern, OID_AUTO, sched_amp_idle_steal,
2600     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
2601     &sched_amp_idle_steal, 0, "");
2602 extern int sched_amp_spill_steal;
2603 SYSCTL_INT(_kern, OID_AUTO, sched_amp_spill_steal,
2604     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
2605     &sched_amp_spill_steal, 0, "");
2606 extern int sched_amp_spill_count;
2607 SYSCTL_INT(_kern, OID_AUTO, sched_amp_spill_count,
2608     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
2609     &sched_amp_spill_count, 0, "");
2610 #endif /* !CONFIG_CLUTCH */
2611 extern int sched_amp_spill_deferred_ipi;
2612 SYSCTL_INT(_kern, OID_AUTO, sched_amp_spill_deferred_ipi,
2613     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
2614     &sched_amp_spill_deferred_ipi, 0, "");
2615 extern int sched_amp_pcores_preempt_immediate_ipi;
2616 SYSCTL_INT(_kern, OID_AUTO, sched_amp_pcores_preempt_immediate_ipi,
2617     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
2618     &sched_amp_pcores_preempt_immediate_ipi, 0, "");
2619 #endif /* __AMP__ */
2620 #endif /* __arm64__ */
2621 
2622 #if __arm64__
2623 extern int legacy_footprint_entitlement_mode;
2624 SYSCTL_INT(_kern, OID_AUTO, legacy_footprint_entitlement_mode,
2625     CTLFLAG_KERN | CTLFLAG_RD | CTLFLAG_LOCKED,
2626     &legacy_footprint_entitlement_mode, 0, "");
2627 #endif /* __arm64__ */
2628 
2629 /*
2630  * Realtime threads are ordered by highest priority first then,
2631  * for threads of the same priority, by earliest deadline first.
2632  * But if sched_rt_runq_strict_priority is false (the default),
2633  * a lower priority thread with an earlier deadline will be preferred
2634  * over a higher priority thread with a later deadline, as long as
2635  * both threads' computations will fit before the later deadline.
2636  */
2637 extern int sched_rt_runq_strict_priority;
2638 SYSCTL_INT(_kern, OID_AUTO, sched_rt_runq_strict_priority,
2639     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
2640     &sched_rt_runq_strict_priority, 0, "");
2641 
2642 static int
2643 sysctl_kern_sched_rt_n_backup_processors(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2644 {
2645 	int new_value, changed;
2646 	int old_value = sched_get_rt_n_backup_processors();
2647 	int error = sysctl_io_number(req, old_value, sizeof(int), &new_value, &changed);
2648 	if (changed) {
2649 		sched_set_rt_n_backup_processors(new_value);
2650 	}
2651 
2652 	return error;
2653 }
2654 
2655 SYSCTL_PROC(_kern, OID_AUTO, sched_rt_n_backup_processors,
2656     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
2657     0, 0, sysctl_kern_sched_rt_n_backup_processors, "I", "");
2658 
2659 static int
2660 sysctl_kern_sched_rt_deadline_epsilon_us(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2661 {
2662 	int new_value, changed;
2663 	int old_value = sched_get_rt_deadline_epsilon();
2664 	int error = sysctl_io_number(req, old_value, sizeof(int), &new_value, &changed);
2665 	if (changed) {
2666 		sched_set_rt_deadline_epsilon(new_value);
2667 	}
2668 
2669 	return error;
2670 }
2671 
2672 SYSCTL_PROC(_kern, OID_AUTO, sched_rt_deadline_epsilon_us,
2673     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
2674     0, 0, sysctl_kern_sched_rt_deadline_epsilon_us, "I", "");
2675 
2676 extern int sched_idle_delay_cpuid;
2677 SYSCTL_INT(_kern, OID_AUTO, sched_idle_delay_cpuid,
2678     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
2679     &sched_idle_delay_cpuid, 0, "This cpuid will be delayed by 500us on exiting idle, to simulate interrupt or preemption delays when testing the scheduler");
2680 
2681 static int
2682 sysctl_kern_sched_powered_cores(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2683 {
2684 	int new_value, changed;
2685 	int old_value = sched_get_powered_cores();
2686 	int error = sysctl_io_number(req, old_value, sizeof(int), &new_value, &changed);
2687 	if (changed) {
2688 		if (!PE_parse_boot_argn("enable_skstb", NULL, 0)) {
2689 			return ENOTSUP;
2690 		}
2691 
2692 		sched_set_powered_cores(new_value);
2693 	}
2694 
2695 	return error;
2696 }
2697 
2698 SYSCTL_PROC(_kern, OID_AUTO, sched_powered_cores,
2699     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
2700     0, 0, sysctl_kern_sched_powered_cores, "I", "");
2701 
2702 #if __arm64__
2703 
2704 static int
2705 sysctl_kern_update_sched_recommended_cores(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2706 {
2707 	uint64_t new_value;
2708 	int changed;
2709 	uint64_t old_value = sched_sysctl_get_recommended_cores();
2710 	int error = sysctl_io_number(req, old_value, sizeof(uint64_t), &new_value, &changed);
2711 	if (changed) {
2712 		if (!PE_parse_boot_argn("enable_skstb", NULL, 0)) {
2713 			return ENOTSUP;
2714 		}
2715 
2716 		sched_perfcontrol_update_recommended_cores_reason(new_value, REASON_CLPC_USER, 0);
2717 	}
2718 
2719 	return error;
2720 }
2721 
2722 SYSCTL_PROC(_kern, OID_AUTO, sched_update_recommended_cores,
2723     CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
2724     0, 0, sysctl_kern_update_sched_recommended_cores, "I", "");
2725 
2726 #endif /* __arm64__ */
2727 
2728 #endif /* (DEVELOPMENT || DEBUG) */
2729 
2730 extern uint64_t sysctl_sched_recommended_cores;
2731 SYSCTL_QUAD(_kern, OID_AUTO, sched_recommended_cores,
2732     CTLFLAG_KERN | CTLFLAG_RD | CTLFLAG_LOCKED,
2733     &sysctl_sched_recommended_cores, "");
2734 
2735 static int
2736 sysctl_kern_suspend_cluster_powerdown(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2737 {
2738 	int new_value, changed;
2739 	int old_value = get_cluster_powerdown_user_suspended();
2740 	int error = sysctl_io_number(req, old_value, sizeof(int), &new_value, &changed);
2741 	if (!error && changed) {
2742 		if (new_value > 0) {
2743 			error = suspend_cluster_powerdown_from_user();
2744 		} else {
2745 			error = resume_cluster_powerdown_from_user();
2746 		}
2747 		if (error) {
2748 			error = EALREADY;
2749 		}
2750 	}
2751 
2752 	return error;
2753 }
2754 
2755 SYSCTL_PROC(_kern, OID_AUTO, suspend_cluster_powerdown,
2756     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
2757     0, 0, sysctl_kern_suspend_cluster_powerdown, "I", "");
2758 
2759 
2760 STATIC int
2761 sysctl_securelvl
2762 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2763 {
2764 	int new_value, changed;
2765 	int error = sysctl_io_number(req, securelevel, sizeof(int), &new_value, &changed);
2766 	if (changed) {
2767 		if (!(new_value < securelevel && proc_getpid(req->p) != 1)) {
2768 			proc_list_lock();
2769 			securelevel = new_value;
2770 			proc_list_unlock();
2771 		} else {
2772 			error = EPERM;
2773 		}
2774 	}
2775 	return error;
2776 }
2777 
2778 SYSCTL_PROC(_kern, KERN_SECURELVL, securelevel,
2779     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
2780     0, 0, sysctl_securelvl, "I", "");
2781 
2782 
2783 STATIC int
2784 sysctl_domainname
2785 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2786 {
2787 	int error, changed;
2788 	char tmpname[MAXHOSTNAMELEN] = {};
2789 
2790 	lck_mtx_lock(&domainname_lock);
2791 	strlcpy(tmpname, domainname, sizeof(tmpname));
2792 	lck_mtx_unlock(&domainname_lock);
2793 
2794 	error = sysctl_io_string(req, tmpname, sizeof(tmpname), 0, &changed);
2795 	if (!error && changed) {
2796 		lck_mtx_lock(&domainname_lock);
2797 		strlcpy(domainname, tmpname, sizeof(domainname));
2798 		lck_mtx_unlock(&domainname_lock);
2799 	}
2800 	return error;
2801 }
2802 
2803 SYSCTL_PROC(_kern, KERN_DOMAINNAME, nisdomainname,
2804     CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_LOCKED,
2805     0, 0, sysctl_domainname, "A", "");
2806 
2807 SYSCTL_COMPAT_INT(_kern, KERN_HOSTID, hostid,
2808     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
2809     &hostid, 0, "");
2810 
2811 STATIC int
2812 sysctl_hostname
2813 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2814 {
2815 	int error, changed;
2816 	char tmpname[MAXHOSTNAMELEN] = {};
2817 	const char * name;
2818 
2819 #if  XNU_TARGET_OS_OSX
2820 	name = hostname;
2821 #else /* XNU_TARGET_OS_OSX */
2822 #define ENTITLEMENT_USER_ASSIGNED_DEVICE_NAME                           \
2823 	"com.apple.developer.device-information.user-assigned-device-name"
2824 	if (csproc_get_platform_binary(current_proc()) ||
2825 	    IOCurrentTaskHasEntitlement(ENTITLEMENT_USER_ASSIGNED_DEVICE_NAME)) {
2826 		name = hostname;
2827 	} else {
2828 		/* Deny writes if we don't pass entitlement check */
2829 		if (req->newptr) {
2830 			return EPERM;
2831 		}
2832 
2833 		name = "localhost";
2834 	}
2835 #endif /* ! XNU_TARGET_OS_OSX */
2836 
2837 	lck_mtx_lock(&hostname_lock);
2838 	strlcpy(tmpname, name, sizeof(tmpname));
2839 	lck_mtx_unlock(&hostname_lock);
2840 
2841 	error = sysctl_io_string(req, tmpname, sizeof(tmpname), 1, &changed);
2842 	if (!error && changed) {
2843 		lck_mtx_lock(&hostname_lock);
2844 		strlcpy(hostname, tmpname, sizeof(hostname));
2845 		lck_mtx_unlock(&hostname_lock);
2846 	}
2847 	return error;
2848 }
2849 
2850 SYSCTL_PROC(_kern, KERN_HOSTNAME, hostname,
2851     CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_LOCKED,
2852     0, 0, sysctl_hostname, "A", "");
2853 
2854 STATIC int
2855 sysctl_procname
2856 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2857 {
2858 	/* Original code allowed writing, I'm copying this, although this all makes
2859 	 *  no sense to me. Besides, this sysctl is never used. */
2860 	return sysctl_io_string(req, &req->p->p_name[0], (2 * MAXCOMLEN + 1), 1, NULL);
2861 }
2862 
2863 SYSCTL_PROC(_kern, KERN_PROCNAME, procname,
2864     CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_LOCKED,
2865     0, 0, sysctl_procname, "A", "");
2866 
2867 SYSCTL_INT(_kern, KERN_SPECULATIVE_READS, speculative_reads_disabled,
2868     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
2869     &speculative_reads_disabled, 0, "");
2870 
2871 SYSCTL_UINT(_kern, OID_AUTO, preheat_max_bytes,
2872     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
2873     &preheat_max_bytes, 0, "");
2874 
2875 SYSCTL_UINT(_kern, OID_AUTO, preheat_min_bytes,
2876     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
2877     &preheat_min_bytes, 0, "");
2878 
2879 SYSCTL_UINT(_kern, OID_AUTO, speculative_prefetch_max,
2880     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
2881     &speculative_prefetch_max, 0, "");
2882 
2883 SYSCTL_UINT(_kern, OID_AUTO, speculative_prefetch_max_iosize,
2884     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
2885     &speculative_prefetch_max_iosize, 0, "");
2886 
2887 SYSCTL_UINT(_kern, OID_AUTO, vm_page_free_target,
2888     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
2889     &vm_page_free_target, 0, "");
2890 
2891 SYSCTL_UINT(_kern, OID_AUTO, vm_page_free_min,
2892     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
2893     &vm_page_free_min, 0, "");
2894 
2895 SYSCTL_UINT(_kern, OID_AUTO, vm_page_free_reserved,
2896     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
2897     &vm_page_free_reserved, 0, "");
2898 
2899 SYSCTL_UINT(_kern, OID_AUTO, vm_page_speculative_percentage,
2900     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
2901     &vm_pageout_state.vm_page_speculative_percentage, 0, "");
2902 
2903 SYSCTL_UINT(_kern, OID_AUTO, vm_page_speculative_q_age_ms,
2904     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
2905     &vm_pageout_state.vm_page_speculative_q_age_ms, 0, "");
2906 
2907 #if (DEVELOPMENT || DEBUG)
2908 SYSCTL_UINT(_kern, OID_AUTO, vm_page_max_speculative_age_q,
2909     CTLFLAG_RD,
2910     &vm_page_max_speculative_age_q, 0, "");
2911 #endif /* (DEVELOPMENT || DEBUG) */
2912 
2913 SYSCTL_UINT(_kern, OID_AUTO, vm_max_delayed_work_limit,
2914     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
2915     &vm_max_delayed_work_limit, 0, "");
2916 
2917 SYSCTL_UINT(_kern, OID_AUTO, vm_max_batch,
2918     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
2919     &vm_max_batch, 0, "");
2920 
2921 SYSCTL_STRING(_kern, OID_AUTO, bootsessionuuid,
2922     CTLFLAG_RD | CTLFLAG_LOCKED,
2923     &bootsessionuuid_string, sizeof(bootsessionuuid_string), "");
2924 
2925 
2926 STATIC int
2927 sysctl_boottime
2928 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2929 {
2930 	struct timeval tv;
2931 	boottime_timeval(&tv);
2932 	struct proc *p = req->p;
2933 
2934 	if (proc_is64bit(p)) {
2935 		struct user64_timeval t = {};
2936 		t.tv_sec = tv.tv_sec;
2937 		t.tv_usec = tv.tv_usec;
2938 		return sysctl_io_opaque(req, &t, sizeof(t), NULL);
2939 	} else {
2940 		struct user32_timeval t = {};
2941 		t.tv_sec = (user32_time_t)tv.tv_sec;
2942 		t.tv_usec = tv.tv_usec;
2943 		return sysctl_io_opaque(req, &t, sizeof(t), NULL);
2944 	}
2945 }
2946 
2947 SYSCTL_PROC(_kern, KERN_BOOTTIME, boottime,
2948     CTLTYPE_STRUCT | CTLFLAG_KERN | CTLFLAG_RD | CTLFLAG_LOCKED,
2949     0, 0, sysctl_boottime, "S,timeval", "");
2950 
2951 extern bool IOGetBootUUID(char *);
2952 
2953 /* non-static: written by imageboot.c */
2954 uuid_string_t fake_bootuuid;
2955 
2956 STATIC int
2957 sysctl_bootuuid
2958 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2959 {
2960 	int error = ENOENT;
2961 
2962 	/* check the first byte to see if the string has been
2963 	 * populated. this is a uuid_STRING_t, this check would
2964 	 * not work with a uuid_t.
2965 	 */
2966 	if (fake_bootuuid[0] != '\0') {
2967 		error = sysctl_io_string(req, fake_bootuuid, 0, 0, NULL);
2968 		goto out;
2969 	}
2970 
2971 	uuid_string_t uuid_string;
2972 	if (IOGetBootUUID(uuid_string)) {
2973 		uuid_t boot_uuid;
2974 		error = uuid_parse(uuid_string, boot_uuid);
2975 		if (!error) {
2976 			error = sysctl_io_string(req, __DECONST(char *, uuid_string), 0, 0, NULL);
2977 		}
2978 	}
2979 
2980 out:
2981 	return error;
2982 }
2983 
2984 SYSCTL_PROC(_kern, OID_AUTO, bootuuid,
2985     CTLTYPE_STRING | CTLFLAG_KERN | CTLFLAG_RD | CTLFLAG_LOCKED,
2986     0, 0, sysctl_bootuuid, "A", "");
2987 
2988 
2989 extern bool IOGetApfsPrebootUUID(char *);
2990 extern bool IOGetAssociatedApfsVolgroupUUID(char *);
2991 
2992 STATIC int
2993 sysctl_apfsprebootuuid
2994 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2995 {
2996 	int error = ENOENT;
2997 
2998 	uuid_string_t uuid_string;
2999 	if (IOGetApfsPrebootUUID(uuid_string)) {
3000 		uuid_t apfs_preboot_uuid;
3001 		error = uuid_parse(uuid_string, apfs_preboot_uuid);
3002 		if (!error) {
3003 			error = sysctl_io_string(req, __DECONST(char *, uuid_string), 0, 0, NULL);
3004 		}
3005 	}
3006 
3007 	return error;
3008 }
3009 
3010 SYSCTL_PROC(_kern, OID_AUTO, apfsprebootuuid,
3011     CTLTYPE_STRING | CTLFLAG_KERN | CTLFLAG_RD | CTLFLAG_LOCKED,
3012     0, 0, sysctl_apfsprebootuuid, "A", "");
3013 
3014 STATIC int
3015 sysctl_targetsystemvolgroupuuid
3016 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3017 {
3018 	int error = ENOENT;
3019 
3020 	uuid_string_t uuid_string;
3021 	if (IOGetApfsPrebootUUID(uuid_string)) {
3022 		uuid_t apfs_preboot_uuid;
3023 		error = uuid_parse(uuid_string, apfs_preboot_uuid);
3024 		if (!error) {
3025 			error = sysctl_io_string(req, __DECONST(char *, uuid_string), 0, 0, NULL);
3026 		}
3027 	} else {
3028 		/*
3029 		 * In special boot modes, such as kcgen-mode, the
3030 		 * apfs-preboot-uuid property will not be set. Instead, a
3031 		 * different property, associated-volume-group, will be set
3032 		 * which indicates the UUID of the VolumeGroup containing the
3033 		 * system volume into which you will boot.
3034 		 */
3035 		if (IOGetAssociatedApfsVolgroupUUID(uuid_string)) {
3036 			uuid_t apfs_preboot_uuid;
3037 			error = uuid_parse(uuid_string, apfs_preboot_uuid);
3038 			if (!error) {
3039 				error = sysctl_io_string(req, __DECONST(char *, uuid_string), 0, 0, NULL);
3040 			}
3041 		}
3042 	}
3043 
3044 	return error;
3045 }
3046 
3047 SYSCTL_PROC(_kern, OID_AUTO, targetsystemvolgroupuuid,
3048     CTLTYPE_STRING | CTLFLAG_KERN | CTLFLAG_RD | CTLFLAG_MASKED | CTLFLAG_LOCKED,
3049     0, 0, sysctl_targetsystemvolgroupuuid, "A", "");
3050 
3051 
3052 extern bool IOGetBootManifestHash(char *, size_t *);
3053 extern bool IOGetBootObjectsPath(char *);
3054 
3055 STATIC int
3056 sysctl_bootobjectspath
3057 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3058 {
3059 	int error = ENOENT;
3060 
3061 #if defined(__x86_64__)
3062 	/* auth-root-dmg is used for the Intel BaseSystem in some flows,
3063 	 * e.g. createinstallmedia and as part of upgrading from 10.15 or earlier
3064 	 * under these scenarios, set_fake_bootuuid will be called when pivoting to
3065 	 * the new root filesystem. need honor the fake bootuuid.
3066 	 */
3067 	if (fake_bootuuid[0] != '\0') {
3068 		error = sysctl_io_string(req, fake_bootuuid, 0, 0, NULL);
3069 	} else {
3070 		/* for intel mac, boot objects reside in [preboot volume]/[bootuuid]
3071 		 * bootuuid and apfsprebootuuid are populated by efiboot and they are alias.
3072 		 */
3073 		uuid_string_t uuid_string;
3074 		if (IOGetBootUUID(uuid_string)) {
3075 			uuid_t boot_uuid;
3076 			error = uuid_parse(uuid_string, boot_uuid);
3077 			if (!error) {
3078 				error = sysctl_io_string(req, (char *)uuid_string, 0, 0, NULL);
3079 			}
3080 		}
3081 	}
3082 #else
3083 	char boot_obj_path[MAXPATHLEN] = { "\0" };
3084 	static const char kAsciiHexChars[] = "0123456789ABCDEF";
3085 	unsigned int i, j;
3086 
3087 	/* Hashed with SHA2-384 or SHA1, boot manifest hash is 48 bytes or 20 bytes
3088 	 * hence, need a 97 bytes char array for the string.
3089 	 */
3090 	size_t hash_data_size = CCSHA384_OUTPUT_SIZE;
3091 	char hash_data[CCSHA384_OUTPUT_SIZE] = { "\0" };
3092 	char boot_manifest_hash[CCSHA384_OUTPUT_SIZE * 2 + 1] = { "\0" };;
3093 
3094 	/* for Apple Silicon Macs, there is a boot-objects-path under IODeviceTree:/chosen
3095 	 * and boot objects reside in [preboot volume]/[boot-objects-path]
3096 	 * for embedded platforms, there would be a boot-manifest-hash under IODeviceTree:/chosen
3097 	 * and boot objects reside in [preboot volume]/[boot-manifest-hash]
3098 	 */
3099 	if (IOGetBootObjectsPath(boot_obj_path)) {
3100 		error = sysctl_io_string(req, (char *)boot_obj_path, 0, 0, NULL);
3101 	} else if (IOGetBootManifestHash(hash_data, &hash_data_size)) {
3102 		j = 0;
3103 		for (i = 0; i < hash_data_size; ++i) {
3104 			char octet = hash_data[i];
3105 			boot_manifest_hash[j++] = kAsciiHexChars[((octet & 0xF0) >> 4)];
3106 			boot_manifest_hash[j++] = kAsciiHexChars[(octet & 0x0F)];
3107 		}
3108 		/* make sure string has null termination */
3109 		boot_manifest_hash[j] = '\0';
3110 		error = sysctl_io_string(req, (char *)boot_manifest_hash, 0, 0, NULL);
3111 	}
3112 #endif
3113 	return error;
3114 }
3115 
3116 SYSCTL_PROC(_kern, OID_AUTO, bootobjectspath,
3117     CTLTYPE_STRING | CTLFLAG_KERN | CTLFLAG_RD | CTLFLAG_LOCKED,
3118     0, 0, sysctl_bootobjectspath, "A", "");
3119 
3120 
3121 STATIC int
3122 sysctl_symfile
3123 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3124 {
3125 	char *str;
3126 	int error = get_kernel_symfile(req->p, &str);
3127 	if (error) {
3128 		return error;
3129 	}
3130 	return sysctl_io_string(req, str, 0, 0, NULL);
3131 }
3132 
3133 
3134 SYSCTL_PROC(_kern, KERN_SYMFILE, symfile,
3135     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_LOCKED,
3136     0, 0, sysctl_symfile, "A", "");
3137 
3138 #if CONFIG_NETBOOT
3139 STATIC int
3140 sysctl_netboot
3141 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3142 {
3143 	return sysctl_io_number(req, netboot_root(), sizeof(int), NULL, NULL);
3144 }
3145 
3146 SYSCTL_PROC(_kern, KERN_NETBOOT, netboot,
3147     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
3148     0, 0, sysctl_netboot, "I", "");
3149 #endif
3150 
3151 #ifdef CONFIG_IMGSRC_ACCESS
3152 /*
3153  * Legacy--act as if only one layer of nesting is possible.
3154  */
3155 STATIC int
3156 sysctl_imgsrcdev
3157 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3158 {
3159 	vfs_context_t ctx = vfs_context_current();
3160 	vnode_t devvp;
3161 	int result;
3162 
3163 	if (!vfs_context_issuser(ctx)) {
3164 		return EPERM;
3165 	}
3166 
3167 	if (imgsrc_rootvnodes[0] == NULL) {
3168 		return ENOENT;
3169 	}
3170 
3171 	result = vnode_getwithref(imgsrc_rootvnodes[0]);
3172 	if (result != 0) {
3173 		return result;
3174 	}
3175 
3176 	devvp = vnode_mount(imgsrc_rootvnodes[0])->mnt_devvp;
3177 	result = vnode_getwithref(devvp);
3178 	if (result != 0) {
3179 		goto out;
3180 	}
3181 
3182 	result = sysctl_io_number(req, vnode_specrdev(devvp), sizeof(dev_t), NULL, NULL);
3183 
3184 	vnode_put(devvp);
3185 out:
3186 	vnode_put(imgsrc_rootvnodes[0]);
3187 	return result;
3188 }
3189 
3190 SYSCTL_PROC(_kern, OID_AUTO, imgsrcdev,
3191     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
3192     0, 0, sysctl_imgsrcdev, "I", "");
3193 
3194 STATIC int
3195 sysctl_imgsrcinfo
3196 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3197 {
3198 	int error;
3199 	struct imgsrc_info info[MAX_IMAGEBOOT_NESTING] = {};    /* 2 for now, no problem */
3200 	uint32_t i;
3201 	vnode_t rvp, devvp;
3202 
3203 	if (imgsrc_rootvnodes[0] == NULLVP) {
3204 		return ENXIO;
3205 	}
3206 
3207 	for (i = 0; i < MAX_IMAGEBOOT_NESTING; i++) {
3208 		/*
3209 		 * Go get the root vnode.
3210 		 */
3211 		rvp = imgsrc_rootvnodes[i];
3212 		if (rvp == NULLVP) {
3213 			break;
3214 		}
3215 
3216 		error = vnode_get(rvp);
3217 		if (error != 0) {
3218 			return error;
3219 		}
3220 
3221 		/*
3222 		 * For now, no getting at a non-local volume.
3223 		 */
3224 		devvp = vnode_mount(rvp)->mnt_devvp;
3225 		if (devvp == NULL) {
3226 			vnode_put(rvp);
3227 			return EINVAL;
3228 		}
3229 
3230 		error = vnode_getwithref(devvp);
3231 		if (error != 0) {
3232 			vnode_put(rvp);
3233 			return error;
3234 		}
3235 
3236 		/*
3237 		 * Fill in info.
3238 		 */
3239 		info[i].ii_dev = vnode_specrdev(devvp);
3240 		info[i].ii_flags = 0;
3241 		info[i].ii_height = i;
3242 		bzero(info[i].ii_reserved, sizeof(info[i].ii_reserved));
3243 
3244 		vnode_put(devvp);
3245 		vnode_put(rvp);
3246 	}
3247 
3248 	return sysctl_io_opaque(req, info, i * sizeof(info[0]), NULL);
3249 }
3250 
3251 SYSCTL_PROC(_kern, OID_AUTO, imgsrcinfo,
3252     CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_LOCKED,
3253     0, 0, sysctl_imgsrcinfo, "I", "");
3254 
3255 #endif /* CONFIG_IMGSRC_ACCESS */
3256 
3257 
3258 SYSCTL_DECL(_kern_timer);
3259 SYSCTL_NODE(_kern, OID_AUTO, timer, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "timer");
3260 
3261 
3262 SYSCTL_INT(_kern_timer, OID_AUTO, coalescing_enabled,
3263     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
3264     &mach_timer_coalescing_enabled, 0, "");
3265 
3266 SYSCTL_QUAD(_kern_timer, OID_AUTO, deadline_tracking_bin_1,
3267     CTLFLAG_RW | CTLFLAG_LOCKED,
3268     &timer_deadline_tracking_bin_1, "");
3269 SYSCTL_QUAD(_kern_timer, OID_AUTO, deadline_tracking_bin_2,
3270     CTLFLAG_RW | CTLFLAG_LOCKED,
3271     &timer_deadline_tracking_bin_2, "");
3272 
3273 SYSCTL_DECL(_kern_timer_longterm);
3274 SYSCTL_NODE(_kern_timer, OID_AUTO, longterm, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "longterm");
3275 
3276 
3277 /* Must match definition in osfmk/kern/timer_call.c */
3278 enum {
3279 	THRESHOLD, QCOUNT,
3280 	ENQUEUES, DEQUEUES, ESCALATES, SCANS, PREEMPTS,
3281 	LATENCY, LATENCY_MIN, LATENCY_MAX, LONG_TERM_SCAN_LIMIT,
3282 	LONG_TERM_SCAN_INTERVAL, LONG_TERM_SCAN_PAUSES,
3283 	SCAN_LIMIT, SCAN_INTERVAL, SCAN_PAUSES, SCAN_POSTPONES,
3284 };
3285 extern uint64_t timer_sysctl_get(int);
3286 extern kern_return_t timer_sysctl_set(int, uint64_t);
3287 
3288 STATIC int
3289 sysctl_timer
3290 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3291 {
3292 	int             oid = (int)arg1;
3293 	uint64_t        value = timer_sysctl_get(oid);
3294 	uint64_t        new_value;
3295 	int             error;
3296 	int             changed;
3297 
3298 	error = sysctl_io_number(req, value, sizeof(value), &new_value, &changed);
3299 	if (changed) {
3300 		kern_return_t kr = timer_sysctl_set(oid, new_value);
3301 		error = mach_to_bsd_errno(kr);
3302 	}
3303 
3304 	return error;
3305 }
3306 
3307 SYSCTL_PROC(_kern_timer_longterm, OID_AUTO, threshold,
3308     CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
3309     (void *) THRESHOLD, 0, sysctl_timer, "Q", "");
3310 SYSCTL_PROC(_kern_timer_longterm, OID_AUTO, scan_limit,
3311     CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
3312     (void *) LONG_TERM_SCAN_LIMIT, 0, sysctl_timer, "Q", "");
3313 SYSCTL_PROC(_kern_timer_longterm, OID_AUTO, scan_interval,
3314     CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
3315     (void *) LONG_TERM_SCAN_INTERVAL, 0, sysctl_timer, "Q", "");
3316 
3317 SYSCTL_PROC(_kern_timer_longterm, OID_AUTO, qlen,
3318     CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED,
3319     (void *) QCOUNT, 0, sysctl_timer, "Q", "");
3320 SYSCTL_PROC(_kern_timer_longterm, OID_AUTO, scan_pauses,
3321     CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED,
3322     (void *) LONG_TERM_SCAN_PAUSES, 0, sysctl_timer, "Q", "");
3323 
3324 #if  DEBUG
3325 SYSCTL_PROC(_kern_timer_longterm, OID_AUTO, enqueues,
3326     CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED,
3327     (void *) ENQUEUES, 0, sysctl_timer, "Q", "");
3328 SYSCTL_PROC(_kern_timer_longterm, OID_AUTO, dequeues,
3329     CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED,
3330     (void *) DEQUEUES, 0, sysctl_timer, "Q", "");
3331 SYSCTL_PROC(_kern_timer_longterm, OID_AUTO, escalates,
3332     CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED,
3333     (void *) ESCALATES, 0, sysctl_timer, "Q", "");
3334 SYSCTL_PROC(_kern_timer_longterm, OID_AUTO, scans,
3335     CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED,
3336     (void *) SCANS, 0, sysctl_timer, "Q", "");
3337 SYSCTL_PROC(_kern_timer_longterm, OID_AUTO, preempts,
3338     CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED,
3339     (void *) PREEMPTS, 0, sysctl_timer, "Q", "");
3340 SYSCTL_PROC(_kern_timer_longterm, OID_AUTO, latency,
3341     CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED,
3342     (void *) LATENCY, 0, sysctl_timer, "Q", "");
3343 SYSCTL_PROC(_kern_timer_longterm, OID_AUTO, latency_min,
3344     CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED,
3345     (void *) LATENCY_MIN, 0, sysctl_timer, "Q", "");
3346 SYSCTL_PROC(_kern_timer_longterm, OID_AUTO, latency_max,
3347     CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED,
3348     (void *) LATENCY_MAX, 0, sysctl_timer, "Q", "");
3349 #endif /* DEBUG */
3350 
3351 SYSCTL_PROC(_kern_timer, OID_AUTO, scan_limit,
3352     CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
3353     (void *) SCAN_LIMIT, 0, sysctl_timer, "Q", "");
3354 SYSCTL_PROC(_kern_timer, OID_AUTO, scan_interval,
3355     CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
3356     (void *) SCAN_INTERVAL, 0, sysctl_timer, "Q", "");
3357 SYSCTL_PROC(_kern_timer, OID_AUTO, scan_pauses,
3358     CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED,
3359     (void *) SCAN_PAUSES, 0, sysctl_timer, "Q", "");
3360 SYSCTL_PROC(_kern_timer, OID_AUTO, scan_postpones,
3361     CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED,
3362     (void *) SCAN_POSTPONES, 0, sysctl_timer, "Q", "");
3363 
3364 STATIC int
3365 sysctl_usrstack
3366 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3367 {
3368 	return sysctl_io_number(req, (int)req->p->user_stack, sizeof(int), NULL, NULL);
3369 }
3370 
3371 SYSCTL_PROC(_kern, KERN_USRSTACK32, usrstack,
3372     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
3373     0, 0, sysctl_usrstack, "I", "");
3374 
3375 STATIC int
3376 sysctl_usrstack64
3377 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3378 {
3379 	return sysctl_io_number(req, req->p->user_stack, sizeof(req->p->user_stack), NULL, NULL);
3380 }
3381 
3382 SYSCTL_PROC(_kern, KERN_USRSTACK64, usrstack64,
3383     CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED,
3384     0, 0, sysctl_usrstack64, "Q", "");
3385 
3386 
3387 #if EXCLAVES_COREDUMP
3388 
3389 /* secure kernel coredump support. */
3390 extern unsigned int sc_dump_mode;
3391 SYSCTL_UINT(_kern, OID_AUTO, secure_coredump, CTLFLAG_RD, &sc_dump_mode, 0, "secure_coredump");
3392 
3393 #endif /* EXCLAVES_COREDUMP */
3394 
3395 #if CONFIG_COREDUMP || CONFIG_UCOREDUMP
3396 
3397 SYSCTL_STRING(_kern, KERN_COREFILE, corefile,
3398     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
3399     corefilename, sizeof(corefilename), "");
3400 
3401 SYSCTL_STRING(_kern, OID_AUTO, drivercorefile,
3402     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
3403     drivercorefilename, sizeof(drivercorefilename), "");
3404 
3405 STATIC int
3406 sysctl_coredump
3407 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3408 {
3409 #ifdef SECURE_KERNEL
3410 	(void)req;
3411 	return ENOTSUP;
3412 #else
3413 	int new_value, changed;
3414 	int error = sysctl_io_number(req, do_coredump, sizeof(int), &new_value, &changed);
3415 	if (changed) {
3416 		if ((new_value == 0) || (new_value == 1)) {
3417 			do_coredump = new_value;
3418 		} else {
3419 			error = EINVAL;
3420 		}
3421 	}
3422 	return error;
3423 #endif
3424 }
3425 
3426 SYSCTL_PROC(_kern, KERN_COREDUMP, coredump,
3427     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
3428     0, 0, sysctl_coredump, "I", "");
3429 
3430 STATIC int
3431 sysctl_suid_coredump
3432 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3433 {
3434 #ifdef SECURE_KERNEL
3435 	(void)req;
3436 	return ENOTSUP;
3437 #else
3438 	int new_value, changed;
3439 	int error = sysctl_io_number(req, sugid_coredump, sizeof(int), &new_value, &changed);
3440 	if (changed) {
3441 		if ((new_value == 0) || (new_value == 1)) {
3442 			sugid_coredump = new_value;
3443 		} else {
3444 			error = EINVAL;
3445 		}
3446 	}
3447 	return error;
3448 #endif
3449 }
3450 
3451 SYSCTL_PROC(_kern, KERN_SUGID_COREDUMP, sugid_coredump,
3452     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
3453     0, 0, sysctl_suid_coredump, "I", "");
3454 
3455 #if CONFIG_UCOREDUMP
3456 
3457 STATIC int
3458 sysctl_ucoredump
3459 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3460 {
3461 #ifdef SECURE_KERNEL
3462 	(void)req;
3463 	return ENOTSUP;
3464 #else
3465 	int new_value, changed;
3466 	int error = sysctl_io_number(req, do_ucoredump, sizeof(int), &new_value, &changed);
3467 	if (changed) {
3468 		if (new_value == 0 || new_value == 1) {
3469 			do_ucoredump = new_value;
3470 		} else {
3471 			error = EINVAL;
3472 		}
3473 	}
3474 	return error;
3475 #endif
3476 }
3477 
3478 SYSCTL_PROC(_kern, OID_AUTO, ucoredump,
3479     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
3480     0, 0, sysctl_ucoredump, "I", "");
3481 #endif /* CONFIG_UCOREDUMP */
3482 #endif /* CONFIG_COREDUMP || CONFIG_UCOREDUMP */
3483 
3484 #if CONFIG_KDP_INTERACTIVE_DEBUGGING
3485 
3486 extern const char* kdp_corefile_path;
3487 STATIC int
3488 sysctl_kdp_corefile(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3489 {
3490 	return SYSCTL_OUT(req, kdp_corefile_path, strlen(kdp_corefile_path) + 1);
3491 }
3492 
3493 /* this needs to be a proc rather than a string since kdp_corefile_path is not a compile-time constant */
3494 SYSCTL_PROC(_kern, OID_AUTO, kdp_corefile,
3495     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_LOCKED,
3496     0, 0, sysctl_kdp_corefile, "A", "");
3497 
3498 #endif /* CONFIG_KDP_INTERACTIVE_DEBUGGING */
3499 
3500 STATIC int
3501 sysctl_delayterm
3502 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3503 {
3504 	struct proc *p = req->p;
3505 	int new_value, changed;
3506 	int error = sysctl_io_number(req, (req->p->p_lflag & P_LDELAYTERM)? 1: 0, sizeof(int), &new_value, &changed);
3507 	if (changed) {
3508 		proc_lock(p);
3509 		if (new_value) {
3510 			req->p->p_lflag |=  P_LDELAYTERM;
3511 		} else {
3512 			req->p->p_lflag &=  ~P_LDELAYTERM;
3513 		}
3514 		proc_unlock(p);
3515 	}
3516 	return error;
3517 }
3518 
3519 SYSCTL_PROC(_kern, KERN_PROCDELAYTERM, delayterm,
3520     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
3521     0, 0, sysctl_delayterm, "I", "");
3522 
3523 
3524 STATIC int
3525 sysctl_rage_vnode
3526 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3527 {
3528 	struct proc *p = req->p;
3529 	struct  uthread *ut;
3530 	int new_value, old_value, changed;
3531 	int error;
3532 
3533 	ut = current_uthread();
3534 
3535 	if (ut->uu_flag & UT_RAGE_VNODES) {
3536 		old_value = KERN_RAGE_THREAD;
3537 	} else if (p->p_lflag & P_LRAGE_VNODES) {
3538 		old_value = KERN_RAGE_PROC;
3539 	} else {
3540 		old_value = 0;
3541 	}
3542 
3543 	error = sysctl_io_number(req, old_value, sizeof(int), &new_value, &changed);
3544 
3545 	if ((error == 0) && (changed != 0)) {
3546 		switch (new_value) {
3547 		case KERN_RAGE_PROC:
3548 			proc_lock(p);
3549 			p->p_lflag |= P_LRAGE_VNODES;
3550 			proc_unlock(p);
3551 			break;
3552 		case KERN_UNRAGE_PROC:
3553 			proc_lock(p);
3554 			p->p_lflag &= ~P_LRAGE_VNODES;
3555 			proc_unlock(p);
3556 			break;
3557 
3558 		case KERN_RAGE_THREAD:
3559 			ut->uu_flag |= UT_RAGE_VNODES;
3560 			break;
3561 		case KERN_UNRAGE_THREAD:
3562 			ut = current_uthread();
3563 			ut->uu_flag &= ~UT_RAGE_VNODES;
3564 			break;
3565 		}
3566 	}
3567 	return error;
3568 }
3569 
3570 SYSCTL_PROC(_kern, KERN_RAGEVNODE, rage_vnode,
3571     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_LOCKED,
3572     0, 0, sysctl_rage_vnode, "I", "");
3573 
3574 /* XXX until filecoordinationd fixes a bit of inverted logic. */
3575 STATIC int
3576 sysctl_vfsnspace
3577 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3578 {
3579 	int old_value = 0, new_value, changed;
3580 
3581 	return sysctl_io_number(req, old_value, sizeof(int), &new_value,
3582 	           &changed);
3583 }
3584 
3585 SYSCTL_PROC(_kern, OID_AUTO, vfsnspace,
3586     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_LOCKED,
3587     0, 0, sysctl_vfsnspace, "I", "");
3588 
3589 /* XXX move this interface into libproc and remove this sysctl */
3590 STATIC int
3591 sysctl_setthread_cpupercent
3592 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3593 {
3594 	int new_value, old_value;
3595 	int error = 0;
3596 	kern_return_t kret = KERN_SUCCESS;
3597 	uint8_t percent = 0;
3598 	int ms_refill = 0;
3599 
3600 	if (!req->newptr) {
3601 		return 0;
3602 	}
3603 
3604 	old_value = 0;
3605 
3606 	if ((error = sysctl_io_number(req, old_value, sizeof(old_value), &new_value, NULL)) != 0) {
3607 		return error;
3608 	}
3609 
3610 	percent = new_value & 0xff;                     /* low 8 bytes for perent */
3611 	ms_refill = (new_value >> 8) & 0xffffff;        /* upper 24bytes represent ms refill value */
3612 	if (percent > 100) {
3613 		return EINVAL;
3614 	}
3615 
3616 	/*
3617 	 * If the caller is specifying a percentage of 0, this will unset the CPU limit, if present.
3618 	 */
3619 	kret = percent == 0 ?
3620 	    thread_set_cpulimit(THREAD_CPULIMIT_DISABLE, 0, 0) :
3621 	    thread_set_cpulimit(THREAD_CPULIMIT_BLOCK, percent, ms_refill * (int)NSEC_PER_MSEC);
3622 
3623 	if (kret != 0) {
3624 		return EIO;
3625 	}
3626 
3627 	return 0;
3628 }
3629 
3630 SYSCTL_PROC(_kern, OID_AUTO, setthread_cpupercent,
3631     CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_ANYBODY,
3632     0, 0, sysctl_setthread_cpupercent, "I", "set thread cpu percentage limit");
3633 
3634 
3635 STATIC int
3636 sysctl_kern_check_openevt
3637 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3638 {
3639 	struct proc *p = req->p;
3640 	int new_value, old_value, changed;
3641 	int error;
3642 
3643 	if (p->p_flag & P_CHECKOPENEVT) {
3644 		old_value = KERN_OPENEVT_PROC;
3645 	} else {
3646 		old_value = 0;
3647 	}
3648 
3649 	error = sysctl_io_number(req, old_value, sizeof(int), &new_value, &changed);
3650 
3651 	if ((error == 0) && (changed != 0)) {
3652 		switch (new_value) {
3653 		case KERN_OPENEVT_PROC:
3654 			OSBitOrAtomic(P_CHECKOPENEVT, &p->p_flag);
3655 			break;
3656 
3657 		case KERN_UNOPENEVT_PROC:
3658 			OSBitAndAtomic(~((uint32_t)P_CHECKOPENEVT), &p->p_flag);
3659 			break;
3660 
3661 		default:
3662 			error = EINVAL;
3663 		}
3664 	}
3665 	return error;
3666 }
3667 
3668 SYSCTL_PROC(_kern, KERN_CHECKOPENEVT, check_openevt, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_LOCKED,
3669     0, 0, sysctl_kern_check_openevt, "I", "set the per-process check-open-evt flag");
3670 
3671 
3672 #if DEVELOPMENT || DEBUG
3673 STATIC int
3674 sysctl_nx
3675 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3676 {
3677 #ifdef SECURE_KERNEL
3678 	(void)req;
3679 	return ENOTSUP;
3680 #else
3681 	int new_value, changed;
3682 	int error;
3683 
3684 	error = sysctl_io_number(req, nx_enabled, sizeof(nx_enabled), &new_value, &changed);
3685 	if (error) {
3686 		return error;
3687 	}
3688 
3689 	if (changed) {
3690 #if defined(__x86_64__)
3691 		/*
3692 		 * Only allow setting if NX is supported on the chip
3693 		 */
3694 		if (!(cpuid_extfeatures() & CPUID_EXTFEATURE_XD)) {
3695 			return ENOTSUP;
3696 		}
3697 #endif
3698 		nx_enabled = new_value;
3699 	}
3700 	return error;
3701 #endif /* SECURE_KERNEL */
3702 }
3703 #endif
3704 
3705 #if DEVELOPMENT || DEBUG
3706 SYSCTL_PROC(_kern, KERN_NX_PROTECTION, nx,
3707     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
3708     0, 0, sysctl_nx, "I", "");
3709 #endif
3710 
3711 STATIC int
3712 sysctl_loadavg
3713 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3714 {
3715 	if (proc_is64bit(req->p)) {
3716 		struct user64_loadavg loadinfo64 = {};
3717 		fill_loadavg64(&averunnable, &loadinfo64);
3718 		return sysctl_io_opaque(req, &loadinfo64, sizeof(loadinfo64), NULL);
3719 	} else {
3720 		struct user32_loadavg loadinfo32 = {};
3721 		fill_loadavg32(&averunnable, &loadinfo32);
3722 		return sysctl_io_opaque(req, &loadinfo32, sizeof(loadinfo32), NULL);
3723 	}
3724 }
3725 
3726 SYSCTL_PROC(_vm, VM_LOADAVG, loadavg,
3727     CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED,
3728     0, 0, sysctl_loadavg, "S,loadavg", "");
3729 
3730 /*
3731  * Note:	Thread safe; vm_map_lock protects in  vm_toggle_entry_reuse()
3732  */
3733 STATIC int
3734 sysctl_vm_toggle_address_reuse(__unused struct sysctl_oid *oidp, __unused void *arg1,
3735     __unused int arg2, struct sysctl_req *req)
3736 {
3737 	int old_value = 0, new_value = 0, error = 0;
3738 
3739 	if (vm_toggle_entry_reuse( VM_TOGGLE_GETVALUE, &old_value )) {
3740 		return error;
3741 	}
3742 	error = sysctl_io_number(req, old_value, sizeof(int), &new_value, NULL);
3743 	if (!error) {
3744 		return vm_toggle_entry_reuse(new_value, NULL);
3745 	}
3746 	return error;
3747 }
3748 
3749 SYSCTL_PROC(_debug, OID_AUTO, toggle_address_reuse, CTLFLAG_ANYBODY | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, 0, 0, sysctl_vm_toggle_address_reuse, "I", "");
3750 
3751 #ifdef CONFIG_XNUPOST
3752 
3753 extern uint32_t xnupost_get_estimated_testdata_size(void);
3754 extern int xnupost_reset_all_tests(void);
3755 
3756 STATIC int
3757 sysctl_handle_xnupost_get_tests SYSCTL_HANDLER_ARGS
3758 {
3759 	/* fixup unused arguments warnings */
3760 	__unused int _oa2                  = arg2;
3761 	__unused void * _oa1               = arg1;
3762 	__unused struct sysctl_oid * _oidp = oidp;
3763 
3764 	int error          = 0;
3765 	user_addr_t oldp   = 0;
3766 	user_addr_t newp   = 0;
3767 	uint32_t usedbytes = 0;
3768 
3769 	oldp = req->oldptr;
3770 	newp = req->newptr;
3771 
3772 	if (newp) {
3773 		return ENOTSUP;
3774 	}
3775 
3776 	if ((void *)oldp == NULL) {
3777 		/* return estimated size for second call where info can be placed */
3778 		req->oldidx = xnupost_get_estimated_testdata_size();
3779 	} else {
3780 		error       = xnupost_export_testdata((void *)oldp, req->oldlen, &usedbytes);
3781 		req->oldidx = usedbytes;
3782 	}
3783 
3784 	return error;
3785 }
3786 
3787 SYSCTL_PROC(_debug,
3788     OID_AUTO,
3789     xnupost_get_tests,
3790     CTLFLAG_MASKED | CTLFLAG_ANYBODY | CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_LOCKED,
3791     0,
3792     0,
3793     sysctl_handle_xnupost_get_tests,
3794     "-",
3795     "read xnupost test data in kernel");
3796 
3797 #if CONFIG_EXT_PANICLOG
3798 /*
3799  * Extensible panic log test hooks
3800  */
3801 static int
3802 sysctl_debug_ext_paniclog_test_hook SYSCTL_HANDLER_ARGS
3803 {
3804 #pragma unused(arg1, arg2)
3805 	int rval = 0;
3806 	uint32_t test_option = 0;
3807 
3808 	rval = sysctl_handle_int(oidp, &test_option, 0, req);
3809 
3810 	if (rval == 0 && req->newptr) {
3811 		rval = ext_paniclog_test_hook(test_option);
3812 	}
3813 
3814 	return rval;
3815 }
3816 
3817 SYSCTL_PROC(_debug, OID_AUTO, ext_paniclog_test_hook,
3818     CTLTYPE_INT | CTLFLAG_RW,
3819     0, 0,
3820     sysctl_debug_ext_paniclog_test_hook, "A", "ext paniclog test hook");
3821 
3822 #endif
3823 
3824 STATIC int
3825 sysctl_debug_xnupost_ctl SYSCTL_HANDLER_ARGS
3826 {
3827 	/* fixup unused arguments warnings */
3828 	__unused int _oa2                  = arg2;
3829 	__unused void * _oa1               = arg1;
3830 	__unused struct sysctl_oid * _oidp = oidp;
3831 
3832 #define ARRCOUNT 4
3833 	/*
3834 	 * INPUT: ACTION,  PARAM1, PARAM2, PARAM3
3835 	 * OUTPUT: RESULTCODE, ADDITIONAL DATA
3836 	 */
3837 	int32_t outval[ARRCOUNT] = {0};
3838 	int32_t input[ARRCOUNT]  = {0};
3839 	int32_t out_size         = sizeof(outval);
3840 	int32_t in_size          = sizeof(input);
3841 	int error                = 0;
3842 
3843 	/* if this is NULL call to find out size, send out size info */
3844 	if (!req->newptr) {
3845 		goto out;
3846 	}
3847 
3848 	/* pull in provided value from userspace */
3849 	error = SYSCTL_IN(req, &input[0], in_size);
3850 	if (error) {
3851 		return error;
3852 	}
3853 
3854 	if (input[0] == XTCTL_RESET_TESTDATA) {
3855 		outval[0] = xnupost_reset_all_tests();
3856 		goto out;
3857 	}
3858 
3859 out:
3860 	error = SYSCTL_OUT(req, &outval[0], out_size);
3861 	return error;
3862 }
3863 
3864 SYSCTL_PROC(_debug,
3865     OID_AUTO,
3866     xnupost_testctl,
3867     CTLFLAG_MASKED | CTLFLAG_ANYBODY | CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_LOCKED,
3868     0,
3869     0,
3870     sysctl_debug_xnupost_ctl,
3871     "I",
3872     "xnupost control for kernel testing");
3873 
3874 extern void test_oslog_handleOSLogCtl(int32_t * in, int32_t * out, int32_t arraycount);
3875 
3876 STATIC int
3877 sysctl_debug_test_oslog_ctl(__unused struct sysctl_oid * oidp, __unused void * arg1, __unused int arg2, struct sysctl_req * req)
3878 {
3879 #define ARRCOUNT 4
3880 	int32_t outval[ARRCOUNT] = {0};
3881 	int32_t input[ARRCOUNT]  = {0};
3882 	int32_t size_outval      = sizeof(outval);
3883 	int32_t size_inval       = sizeof(input);
3884 	int32_t error;
3885 
3886 	/* if this is NULL call to find out size, send out size info */
3887 	if (!req->newptr) {
3888 		error = SYSCTL_OUT(req, &outval[0], size_outval);
3889 		return error;
3890 	}
3891 
3892 	/* pull in provided value from userspace */
3893 	error = SYSCTL_IN(req, &input[0], size_inval);
3894 	if (error) {
3895 		return error;
3896 	}
3897 
3898 	test_oslog_handleOSLogCtl(input, outval, ARRCOUNT);
3899 
3900 	error = SYSCTL_OUT(req, &outval[0], size_outval);
3901 
3902 	return error;
3903 }
3904 
3905 SYSCTL_PROC(_debug,
3906     OID_AUTO,
3907     test_OSLogCtl,
3908     CTLFLAG_MASKED | CTLFLAG_ANYBODY | CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_LOCKED,
3909     0,
3910     0,
3911     sysctl_debug_test_oslog_ctl,
3912     "I",
3913     "testing oslog in kernel");
3914 
3915 #include <mach/task.h>
3916 #include <mach/semaphore.h>
3917 
3918 static LCK_GRP_DECLARE(sysctl_debug_test_stackshot_owner_grp, "test-stackshot-owner-grp");
3919 static LCK_MTX_DECLARE(sysctl_debug_test_stackshot_owner_init_mtx,
3920     &sysctl_debug_test_stackshot_owner_grp);
3921 
3922 /* This is a sysctl for testing collection of owner info on a lock in kernel space. A multi-threaded
3923  * test from userland sets this sysctl in such a way that a thread blocks in kernel mode, and a
3924  * stackshot is taken to see if the owner of the lock can be identified.
3925  *
3926  * We can't return to userland with a kernel lock held, so be sure to unlock before we leave.
3927  * the semaphores allow us to artificially create cases where the lock is being held and the
3928  * thread is hanging / taking a long time to do something. */
3929 
3930 volatile char      sysctl_debug_test_stackshot_mtx_inited = 0;
3931 semaphore_t        sysctl_debug_test_stackshot_mutex_sem;
3932 lck_mtx_t          sysctl_debug_test_stackshot_owner_lck;
3933 
3934 #define SYSCTL_DEBUG_MTX_ACQUIRE_WAIT   1
3935 #define SYSCTL_DEBUG_MTX_ACQUIRE_NOWAIT 2
3936 #define SYSCTL_DEBUG_MTX_SIGNAL         3
3937 #define SYSCTL_DEBUG_MTX_TEARDOWN       4
3938 
3939 STATIC int
3940 sysctl_debug_test_stackshot_mutex_owner(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3941 {
3942 	long long option = -1;
3943 	/* if the user tries to read the sysctl, we tell them what the address of the lock is (to test against stackshot's output) */
3944 	long long mtx_unslid_addr = (long long)VM_KERNEL_UNSLIDE_OR_PERM(&sysctl_debug_test_stackshot_owner_lck);
3945 	int error = sysctl_io_number(req, mtx_unslid_addr, sizeof(long long), (void*)&option, NULL);
3946 
3947 	lck_mtx_lock(&sysctl_debug_test_stackshot_owner_init_mtx);
3948 	if (!sysctl_debug_test_stackshot_mtx_inited) {
3949 		lck_mtx_init(&sysctl_debug_test_stackshot_owner_lck,
3950 		    &sysctl_debug_test_stackshot_owner_grp,
3951 		    LCK_ATTR_NULL);
3952 		semaphore_create(kernel_task,
3953 		    &sysctl_debug_test_stackshot_mutex_sem,
3954 		    SYNC_POLICY_FIFO, 0);
3955 		sysctl_debug_test_stackshot_mtx_inited = 1;
3956 	}
3957 	lck_mtx_unlock(&sysctl_debug_test_stackshot_owner_init_mtx);
3958 
3959 	if (!error) {
3960 		switch (option) {
3961 		case SYSCTL_DEBUG_MTX_ACQUIRE_NOWAIT:
3962 			lck_mtx_lock(&sysctl_debug_test_stackshot_owner_lck);
3963 			lck_mtx_unlock(&sysctl_debug_test_stackshot_owner_lck);
3964 			break;
3965 		case SYSCTL_DEBUG_MTX_ACQUIRE_WAIT:
3966 			lck_mtx_lock(&sysctl_debug_test_stackshot_owner_lck);
3967 			semaphore_wait(sysctl_debug_test_stackshot_mutex_sem);
3968 			lck_mtx_unlock(&sysctl_debug_test_stackshot_owner_lck);
3969 			break;
3970 		case SYSCTL_DEBUG_MTX_SIGNAL:
3971 			semaphore_signal(sysctl_debug_test_stackshot_mutex_sem);
3972 			break;
3973 		case SYSCTL_DEBUG_MTX_TEARDOWN:
3974 			lck_mtx_lock(&sysctl_debug_test_stackshot_owner_init_mtx);
3975 
3976 			lck_mtx_destroy(&sysctl_debug_test_stackshot_owner_lck,
3977 			    &sysctl_debug_test_stackshot_owner_grp);
3978 			semaphore_destroy(kernel_task,
3979 			    sysctl_debug_test_stackshot_mutex_sem);
3980 			sysctl_debug_test_stackshot_mtx_inited = 0;
3981 
3982 			lck_mtx_unlock(&sysctl_debug_test_stackshot_owner_init_mtx);
3983 			break;
3984 		case -1:         /* user just wanted to read the value, so do nothing */
3985 			break;
3986 		default:
3987 			error = EINVAL;
3988 			break;
3989 		}
3990 	}
3991 	return error;
3992 }
3993 
3994 /* we can't return to userland with a kernel rwlock held, so be sure to unlock before we leave.
3995  * the semaphores allow us to artificially create cases where the lock is being held and the
3996  * thread is hanging / taking a long time to do something. */
3997 
3998 SYSCTL_PROC(_debug,
3999     OID_AUTO,
4000     test_MutexOwnerCtl,
4001     CTLFLAG_MASKED | CTLFLAG_ANYBODY | CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
4002     0,
4003     0,
4004     sysctl_debug_test_stackshot_mutex_owner,
4005     "-",
4006     "Testing mutex owner in kernel");
4007 
4008 volatile char sysctl_debug_test_stackshot_rwlck_inited = 0;
4009 lck_rw_t      sysctl_debug_test_stackshot_owner_rwlck;
4010 semaphore_t   sysctl_debug_test_stackshot_rwlck_sem;
4011 
4012 #define SYSCTL_DEBUG_KRWLCK_RACQUIRE_NOWAIT 1
4013 #define SYSCTL_DEBUG_KRWLCK_RACQUIRE_WAIT   2
4014 #define SYSCTL_DEBUG_KRWLCK_WACQUIRE_NOWAIT 3
4015 #define SYSCTL_DEBUG_KRWLCK_WACQUIRE_WAIT   4
4016 #define SYSCTL_DEBUG_KRWLCK_SIGNAL          5
4017 #define SYSCTL_DEBUG_KRWLCK_TEARDOWN        6
4018 
4019 STATIC int
4020 sysctl_debug_test_stackshot_rwlck_owner(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
4021 {
4022 	long long option = -1;
4023 	/* if the user tries to read the sysctl, we tell them what the address of the lock is
4024 	 * (to test against stackshot's output) */
4025 	long long rwlck_unslid_addr = (long long)VM_KERNEL_UNSLIDE_OR_PERM(&sysctl_debug_test_stackshot_owner_rwlck);
4026 	int error = sysctl_io_number(req, rwlck_unslid_addr, sizeof(long long), (void*)&option, NULL);
4027 
4028 	lck_mtx_lock(&sysctl_debug_test_stackshot_owner_init_mtx);
4029 	if (!sysctl_debug_test_stackshot_rwlck_inited) {
4030 		lck_rw_init(&sysctl_debug_test_stackshot_owner_rwlck,
4031 		    &sysctl_debug_test_stackshot_owner_grp,
4032 		    LCK_ATTR_NULL);
4033 		semaphore_create(kernel_task,
4034 		    &sysctl_debug_test_stackshot_rwlck_sem,
4035 		    SYNC_POLICY_FIFO,
4036 		    0);
4037 		sysctl_debug_test_stackshot_rwlck_inited = 1;
4038 	}
4039 	lck_mtx_unlock(&sysctl_debug_test_stackshot_owner_init_mtx);
4040 
4041 	if (!error) {
4042 		switch (option) {
4043 		case SYSCTL_DEBUG_KRWLCK_RACQUIRE_NOWAIT:
4044 			lck_rw_lock(&sysctl_debug_test_stackshot_owner_rwlck, LCK_RW_TYPE_SHARED);
4045 			lck_rw_unlock(&sysctl_debug_test_stackshot_owner_rwlck, LCK_RW_TYPE_SHARED);
4046 			break;
4047 		case SYSCTL_DEBUG_KRWLCK_RACQUIRE_WAIT:
4048 			lck_rw_lock(&sysctl_debug_test_stackshot_owner_rwlck, LCK_RW_TYPE_SHARED);
4049 			semaphore_wait(sysctl_debug_test_stackshot_rwlck_sem);
4050 			lck_rw_unlock(&sysctl_debug_test_stackshot_owner_rwlck, LCK_RW_TYPE_SHARED);
4051 			break;
4052 		case SYSCTL_DEBUG_KRWLCK_WACQUIRE_NOWAIT:
4053 			lck_rw_lock(&sysctl_debug_test_stackshot_owner_rwlck, LCK_RW_TYPE_EXCLUSIVE);
4054 			lck_rw_unlock(&sysctl_debug_test_stackshot_owner_rwlck, LCK_RW_TYPE_EXCLUSIVE);
4055 			break;
4056 		case SYSCTL_DEBUG_KRWLCK_WACQUIRE_WAIT:
4057 			lck_rw_lock(&sysctl_debug_test_stackshot_owner_rwlck, LCK_RW_TYPE_EXCLUSIVE);
4058 			semaphore_wait(sysctl_debug_test_stackshot_rwlck_sem);
4059 			lck_rw_unlock(&sysctl_debug_test_stackshot_owner_rwlck, LCK_RW_TYPE_EXCLUSIVE);
4060 			break;
4061 		case SYSCTL_DEBUG_KRWLCK_SIGNAL:
4062 			semaphore_signal(sysctl_debug_test_stackshot_rwlck_sem);
4063 			break;
4064 		case SYSCTL_DEBUG_KRWLCK_TEARDOWN:
4065 			lck_mtx_lock(&sysctl_debug_test_stackshot_owner_init_mtx);
4066 
4067 			lck_rw_destroy(&sysctl_debug_test_stackshot_owner_rwlck,
4068 			    &sysctl_debug_test_stackshot_owner_grp);
4069 			semaphore_destroy(kernel_task,
4070 			    sysctl_debug_test_stackshot_rwlck_sem);
4071 			sysctl_debug_test_stackshot_rwlck_inited = 0;
4072 
4073 			lck_mtx_unlock(&sysctl_debug_test_stackshot_owner_init_mtx);
4074 			break;
4075 		case -1:         /* user just wanted to read the value, so do nothing */
4076 			break;
4077 		default:
4078 			error = EINVAL;
4079 			break;
4080 		}
4081 	}
4082 	return error;
4083 }
4084 
4085 
4086 SYSCTL_PROC(_debug,
4087     OID_AUTO,
4088     test_RWLockOwnerCtl,
4089     CTLFLAG_MASKED | CTLFLAG_ANYBODY | CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
4090     0,
4091     0,
4092     sysctl_debug_test_stackshot_rwlck_owner,
4093     "-",
4094     "Testing rwlock owner in kernel");
4095 #endif /* !CONFIG_XNUPOST */
4096 
4097 STATIC int
4098 sysctl_swapusage
4099 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
4100 {
4101 	int                     error;
4102 	uint64_t                swap_total;
4103 	uint64_t                swap_avail;
4104 	vm_size_t               swap_pagesize;
4105 	boolean_t               swap_encrypted;
4106 	struct xsw_usage        xsu = {};
4107 
4108 	error = macx_swapinfo(&swap_total,
4109 	    &swap_avail,
4110 	    &swap_pagesize,
4111 	    &swap_encrypted);
4112 	if (error) {
4113 		return error;
4114 	}
4115 
4116 	xsu.xsu_total = swap_total;
4117 	xsu.xsu_avail = swap_avail;
4118 	xsu.xsu_used = swap_total - swap_avail;
4119 	xsu.xsu_pagesize = (u_int32_t)MIN(swap_pagesize, UINT32_MAX);
4120 	xsu.xsu_encrypted = swap_encrypted;
4121 	return sysctl_io_opaque(req, &xsu, sizeof(xsu), NULL);
4122 }
4123 
4124 
4125 
4126 SYSCTL_PROC(_vm, VM_SWAPUSAGE, swapusage,
4127     CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED,
4128     0, 0, sysctl_swapusage, "S,xsw_usage", "");
4129 
4130 extern int vm_swap_enabled;
4131 SYSCTL_INT(_vm, OID_AUTO, swap_enabled, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_swap_enabled, 0, "");
4132 
4133 #if DEVELOPMENT || DEBUG
4134 extern int vm_num_swap_files_config;
4135 extern int vm_num_swap_files;
4136 extern lck_mtx_t vm_swap_data_lock;
4137 #define VM_MAX_SWAP_FILE_NUM            100
4138 
4139 static int
4140 sysctl_vm_config_num_swap_files SYSCTL_HANDLER_ARGS
4141 {
4142 #pragma unused(arg1, arg2)
4143 	int error = 0, val = vm_num_swap_files_config;
4144 
4145 	error = sysctl_handle_int(oidp, &val, 0, req);
4146 	if (error || !req->newptr) {
4147 		goto out;
4148 	}
4149 
4150 	if (!VM_CONFIG_SWAP_IS_ACTIVE && !VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) {
4151 		printf("Swap is disabled\n");
4152 		error = EINVAL;
4153 		goto out;
4154 	}
4155 
4156 	lck_mtx_lock(&vm_swap_data_lock);
4157 
4158 	if (val < vm_num_swap_files) {
4159 		printf("Cannot configure fewer swap files than already exist.\n");
4160 		error = EINVAL;
4161 		lck_mtx_unlock(&vm_swap_data_lock);
4162 		goto out;
4163 	}
4164 
4165 	if (val > VM_MAX_SWAP_FILE_NUM) {
4166 		printf("Capping number of swap files to upper bound.\n");
4167 		val = VM_MAX_SWAP_FILE_NUM;
4168 	}
4169 
4170 	vm_num_swap_files_config = val;
4171 	lck_mtx_unlock(&vm_swap_data_lock);
4172 out:
4173 
4174 	return 0;
4175 }
4176 
4177 SYSCTL_PROC(_debug, OID_AUTO, num_swap_files_configured, CTLFLAG_ANYBODY | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, 0, 0, sysctl_vm_config_num_swap_files, "I", "");
4178 #endif /* DEVELOPMENT || DEBUG */
4179 
4180 /* this kernel does NOT implement shared_region_make_private_np() */
4181 SYSCTL_INT(_kern, KERN_SHREG_PRIVATIZABLE, shreg_private,
4182     CTLFLAG_RD | CTLFLAG_LOCKED,
4183     (int *)NULL, 0, "");
4184 
4185 STATIC int
4186 fetch_process_cputype(
4187 	proc_t cur_proc,
4188 	int *name,
4189 	u_int namelen,
4190 	cpu_type_t *cputype)
4191 {
4192 	proc_t p = PROC_NULL;
4193 	int refheld = 0;
4194 	cpu_type_t ret = 0;
4195 	int error = 0;
4196 
4197 	if (namelen == 0) {
4198 		p = cur_proc;
4199 	} else if (namelen == 1) {
4200 		p = proc_find(name[0]);
4201 		if (p == NULL) {
4202 			return EINVAL;
4203 		}
4204 		refheld = 1;
4205 	} else {
4206 		error = EINVAL;
4207 		goto out;
4208 	}
4209 
4210 	ret = cpu_type() & ~CPU_ARCH_MASK;
4211 	if (IS_64BIT_PROCESS(p)) {
4212 		ret |= CPU_ARCH_ABI64;
4213 	}
4214 
4215 	*cputype = ret;
4216 
4217 	if (refheld != 0) {
4218 		proc_rele(p);
4219 	}
4220 out:
4221 	return error;
4222 }
4223 
4224 #if CONFIG_ROSETTA
4225 STATIC int
4226 sysctl_sysctl_translated(
4227 	__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, __unused struct sysctl_req *req)
4228 {
4229 	int res = 0;
4230 	if (proc_is_translated(req->p)) {
4231 		res = 1;
4232 	}
4233 	return SYSCTL_OUT(req, &res, sizeof(res));
4234 }
4235 SYSCTL_PROC(_sysctl, OID_AUTO, proc_translated, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, sysctl_sysctl_translated, "I", "proc_translated");
4236 #endif /* CONFIG_ROSETTA */
4237 
4238 STATIC int
4239 sysctl_sysctl_native(__unused struct sysctl_oid *oidp, void *arg1, int arg2,
4240     struct sysctl_req *req)
4241 {
4242 	int error;
4243 	cpu_type_t proc_cputype = 0;
4244 	if ((error = fetch_process_cputype(req->p, (int *)arg1, arg2, &proc_cputype)) != 0) {
4245 		return error;
4246 	}
4247 	int res = 1;
4248 	if ((proc_cputype & ~CPU_ARCH_MASK) != (cpu_type() & ~CPU_ARCH_MASK)) {
4249 		res = 0;
4250 	}
4251 	return SYSCTL_OUT(req, &res, sizeof(res));
4252 }
4253 SYSCTL_PROC(_sysctl, OID_AUTO, proc_native, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, sysctl_sysctl_native, "I", "proc_native");
4254 
4255 STATIC int
4256 sysctl_sysctl_cputype(__unused struct sysctl_oid *oidp, void *arg1, int arg2,
4257     struct sysctl_req *req)
4258 {
4259 	int error;
4260 	cpu_type_t proc_cputype = 0;
4261 	if ((error = fetch_process_cputype(req->p, (int *)arg1, arg2, &proc_cputype)) != 0) {
4262 		return error;
4263 	}
4264 	return SYSCTL_OUT(req, &proc_cputype, sizeof(proc_cputype));
4265 }
4266 SYSCTL_PROC(_sysctl, OID_AUTO, proc_cputype, CTLTYPE_NODE | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, sysctl_sysctl_cputype, "I", "proc_cputype");
4267 
4268 STATIC int
4269 sysctl_safeboot
4270 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
4271 {
4272 	return sysctl_io_number(req, boothowto & RB_SAFEBOOT ? 1 : 0, sizeof(int), NULL, NULL);
4273 }
4274 
4275 SYSCTL_PROC(_kern, KERN_SAFEBOOT, safeboot,
4276     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
4277     0, 0, sysctl_safeboot, "I", "");
4278 
4279 STATIC int
4280 sysctl_singleuser
4281 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
4282 {
4283 	return sysctl_io_number(req, boothowto & RB_SINGLE ? 1 : 0, sizeof(int), NULL, NULL);
4284 }
4285 
4286 SYSCTL_PROC(_kern, OID_AUTO, singleuser,
4287     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
4288     0, 0, sysctl_singleuser, "I", "");
4289 
4290 STATIC int
4291 sysctl_minimalboot
4292 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
4293 {
4294 	return sysctl_io_number(req, minimalboot, sizeof(int), NULL, NULL);
4295 }
4296 
4297 SYSCTL_PROC(_kern, OID_AUTO, minimalboot,
4298     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
4299     0, 0, sysctl_minimalboot, "I", "");
4300 
4301 /*
4302  * Controls for debugging affinity sets - see osfmk/kern/affinity.c
4303  */
4304 extern boolean_t        affinity_sets_enabled;
4305 extern int              affinity_sets_mapping;
4306 
4307 SYSCTL_INT(_kern, OID_AUTO, affinity_sets_enabled,
4308     CTLFLAG_RW | CTLFLAG_LOCKED, (int *) &affinity_sets_enabled, 0, "hinting enabled");
4309 SYSCTL_INT(_kern, OID_AUTO, affinity_sets_mapping,
4310     CTLFLAG_RW | CTLFLAG_LOCKED, &affinity_sets_mapping, 0, "mapping policy");
4311 
4312 /*
4313  * Boolean indicating if KASLR is active.
4314  */
4315 STATIC int
4316 sysctl_slide
4317 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
4318 {
4319 	uint32_t        slide;
4320 
4321 	slide = vm_kernel_slide ? 1 : 0;
4322 
4323 	return sysctl_io_number( req, slide, sizeof(int), NULL, NULL);
4324 }
4325 
4326 SYSCTL_PROC(_kern, OID_AUTO, slide,
4327     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
4328     0, 0, sysctl_slide, "I", "");
4329 
4330 #if DEBUG || DEVELOPMENT
4331 #if defined(__arm64__)
4332 extern vm_offset_t segTEXTEXECB;
4333 
4334 static int
4335 sysctl_kernel_text_exec_base_slide SYSCTL_HANDLER_ARGS
4336 {
4337 #pragma unused(arg1, arg2, oidp)
4338 	unsigned long slide = 0;
4339 	kc_format_t kc_format;
4340 
4341 	PE_get_primary_kc_format(&kc_format);
4342 
4343 	if (kc_format == KCFormatFileset) {
4344 		void *kch = PE_get_kc_header(KCKindPrimary);
4345 		slide = (unsigned long)segTEXTEXECB - (unsigned long)kch + vm_kernel_slide;
4346 	}
4347 	return SYSCTL_OUT(req, &slide, sizeof(slide));
4348 }
4349 
4350 SYSCTL_QUAD(_kern, OID_AUTO, kernel_slide, CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED, &vm_kernel_slide, "");
4351 SYSCTL_QUAD(_kern, OID_AUTO, kernel_text_exec_base, CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED, &segTEXTEXECB, "");
4352 SYSCTL_PROC(_kern, OID_AUTO, kernel_text_exec_base_slide, CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, sysctl_kernel_text_exec_base_slide, "Q", "");
4353 #endif /* defined(__arm64__) */
4354 
4355 /* User address of the PFZ */
4356 extern user32_addr_t commpage_text32_location;
4357 extern user64_addr_t commpage_text64_location;
4358 
4359 STATIC int
4360 sysctl_pfz_start SYSCTL_HANDLER_ARGS
4361 {
4362 #pragma unused(oidp, arg1, arg2)
4363 
4364 #ifdef __LP64__
4365 	return sysctl_io_number(req, commpage_text64_location, sizeof(user64_addr_t), NULL, NULL);
4366 #else
4367 	return sysctl_io_number(req, commpage_text32_location, sizeof(user32_addr_t), NULL, NULL);
4368 #endif
4369 }
4370 
4371 SYSCTL_PROC(_kern, OID_AUTO, pfz,
4372     CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED | CTLFLAG_MASKED,
4373     0, 0, sysctl_pfz_start, "I", "");
4374 #endif
4375 
4376 
4377 /*
4378  * Limit on total memory users can wire.
4379  *
4380  * vm_global_user_wire_limit - system wide limit on wired memory from all processes combined.
4381  *
4382  * vm_per_task_user_wire_limit - per address space limit on wired memory.  This puts a cap on the process's rlimit value.
4383  *
4384  * These values are initialized to reasonable defaults at boot time based on the available physical memory in
4385  * kmem_init().
4386  *
4387  * All values are in bytes.
4388  */
4389 
4390 vm_map_size_t   vm_global_user_wire_limit;
4391 vm_map_size_t   vm_per_task_user_wire_limit;
4392 extern uint64_t max_mem_actual, max_mem;
4393 
4394 uint64_t        vm_add_wire_count_over_global_limit;
4395 uint64_t        vm_add_wire_count_over_user_limit;
4396 /*
4397  * We used to have a global in the kernel called vm_global_no_user_wire_limit which was the inverse
4398  * of vm_global_user_wire_limit. But maintaining both of those is silly, and vm_global_user_wire_limit is the
4399  * real limit.
4400  * This function is for backwards compatibility with userspace
4401  * since we exposed the old global via a sysctl.
4402  */
4403 STATIC int
4404 sysctl_global_no_user_wire_amount(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
4405 {
4406 	vm_map_size_t old_value;
4407 	vm_map_size_t new_value;
4408 	int changed;
4409 	int error;
4410 	uint64_t config_memsize = max_mem;
4411 #if defined(XNU_TARGET_OS_OSX)
4412 	config_memsize = max_mem_actual;
4413 #endif /* defined(XNU_TARGET_OS_OSX) */
4414 
4415 	old_value = (vm_map_size_t)(config_memsize - vm_global_user_wire_limit);
4416 	error = sysctl_io_number(req, old_value, sizeof(vm_map_size_t), &new_value, &changed);
4417 	if (changed) {
4418 		if ((uint64_t)new_value > config_memsize) {
4419 			error = EINVAL;
4420 		} else {
4421 			vm_global_user_wire_limit = (vm_map_size_t)(config_memsize - new_value);
4422 		}
4423 	}
4424 	return error;
4425 }
4426 /*
4427  * There needs to be a more automatic/elegant way to do this
4428  */
4429 SYSCTL_QUAD(_vm, OID_AUTO, global_user_wire_limit, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_global_user_wire_limit, "");
4430 SYSCTL_QUAD(_vm, OID_AUTO, user_wire_limit, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_per_task_user_wire_limit, "");
4431 SYSCTL_PROC(_vm, OID_AUTO, global_no_user_wire_amount, CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED, 0, 0, &sysctl_global_no_user_wire_amount, "Q", "");
4432 
4433 /*
4434  * Relaxed atomic RW of a 64bit value via sysctl.
4435  */
4436 STATIC int
4437 sysctl_r_64bit_atomic(uint64_t *ptr, struct sysctl_req *req)
4438 {
4439 	uint64_t old_value;
4440 	uint64_t new_value;
4441 	int error;
4442 
4443 	old_value = os_atomic_load_wide(ptr, relaxed);
4444 	error = sysctl_io_number(req, old_value, sizeof(vm_map_size_t), &new_value, NULL);
4445 	return error;
4446 }
4447 STATIC int
4448 sysctl_add_wire_count_over_global_limit(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
4449 {
4450 	return sysctl_r_64bit_atomic(&vm_add_wire_count_over_global_limit, req);
4451 }
4452 STATIC int
4453 sysctl_add_wire_count_over_user_limit(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
4454 {
4455 	return sysctl_r_64bit_atomic(&vm_add_wire_count_over_user_limit, req);
4456 }
4457 
4458 SYSCTL_PROC(_vm, OID_AUTO, add_wire_count_over_global_limit, CTLTYPE_QUAD | CTLFLAG_RD |  CTLFLAG_LOCKED, 0, 0, &sysctl_add_wire_count_over_global_limit, "Q", "");
4459 SYSCTL_PROC(_vm, OID_AUTO, add_wire_count_over_user_limit, CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, &sysctl_add_wire_count_over_user_limit, "Q", "");
4460 
4461 #if DEVELOPMENT || DEBUG
4462 /* These sysctls are used to test the wired limit. */
4463 SYSCTL_INT(_vm, OID_AUTO, page_wire_count, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_page_wire_count, 0,
4464     "The number of physical pages which are pinned and cannot be evicted");
4465 #if XNU_VM_HAS_LOPAGE
4466 SYSCTL_INT(_vm, OID_AUTO, lopage_free_count, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_lopage_free_count, 0, "");
4467 #endif
4468 SYSCTL_INT(_vm, OID_AUTO, page_stolen_count, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_page_stolen_count, 0, "");
4469 SYSCTL_UINT(_vm, OID_AUTO, page_swapped_count, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_page_swapped_count, 0,
4470     "The number of virtual pages whose contents are currently compressed and swapped to disk");
4471 
4472 /*
4473  * Setting the per task variable exclude_physfootprint_ledger to 1 will allow the calling task to exclude memory entries that are
4474  * tagged by VM_LEDGER_TAG_DEFAULT and flagged by VM_LEDGER_FLAG_EXCLUDE_FOOTPRINT_DEBUG from its phys_footprint ledger.
4475  */
4476 
4477 STATIC int
4478 sysctl_rw_task_no_footprint_for_debug(struct sysctl_oid *oidp __unused, void *arg1 __unused, int arg2 __unused, struct sysctl_req *req)
4479 {
4480 	int error;
4481 	int value;
4482 	proc_t p = current_proc();
4483 
4484 	if (req->newptr) {
4485 		// Write request
4486 		error = SYSCTL_IN(req, &value, sizeof(value));
4487 		if (!error) {
4488 			if (value == 1) {
4489 				task_set_no_footprint_for_debug(proc_task(p), TRUE);
4490 			} else if (value == 0) {
4491 				task_set_no_footprint_for_debug(proc_task(p), FALSE);
4492 			} else {
4493 				error = EINVAL;
4494 			}
4495 		}
4496 	} else {
4497 		// Read request
4498 		value = task_get_no_footprint_for_debug(proc_task(p));
4499 		error = SYSCTL_OUT(req, &value, sizeof(value));
4500 	}
4501 	return error;
4502 }
4503 
4504 SYSCTL_PROC(_vm, OID_AUTO, task_no_footprint_for_debug,
4505     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_ANYBODY,
4506     0, 0, &sysctl_rw_task_no_footprint_for_debug, "I", "Allow debug memory to be excluded from this task's memory footprint (debug only)");
4507 
4508 #endif /* DEVELOPMENT || DEBUG */
4509 
4510 
4511 extern int vm_map_copy_overwrite_aligned_src_not_internal;
4512 extern int vm_map_copy_overwrite_aligned_src_not_symmetric;
4513 extern int vm_map_copy_overwrite_aligned_src_large;
4514 SYSCTL_INT(_vm, OID_AUTO, vm_copy_src_not_internal, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_map_copy_overwrite_aligned_src_not_internal, 0, "");
4515 SYSCTL_INT(_vm, OID_AUTO, vm_copy_src_not_symmetric, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_map_copy_overwrite_aligned_src_not_symmetric, 0, "");
4516 SYSCTL_INT(_vm, OID_AUTO, vm_copy_src_large, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_map_copy_overwrite_aligned_src_large, 0, "");
4517 
4518 
4519 extern uint32_t vm_page_external_count;
4520 
4521 SYSCTL_INT(_vm, OID_AUTO, vm_page_external_count, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_page_external_count, 0, "");
4522 
4523 SYSCTL_INT(_vm, OID_AUTO, vm_page_filecache_min, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_pageout_state.vm_page_filecache_min, 0, "");
4524 SYSCTL_INT(_vm, OID_AUTO, vm_page_xpmapped_min, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_pageout_state.vm_page_xpmapped_min, 0, "");
4525 
4526 #if DEVELOPMENT || DEBUG
4527 SYSCTL_INT(_vm, OID_AUTO, vm_page_filecache_min_divisor, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_pageout_state.vm_page_filecache_min_divisor, 0, "");
4528 SYSCTL_INT(_vm, OID_AUTO, vm_page_xpmapped_min_divisor, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_pageout_state.vm_page_xpmapped_min_divisor, 0, "");
4529 extern boolean_t vps_yield_for_pgqlockwaiters;
4530 SYSCTL_INT(_vm, OID_AUTO, vm_pageoutscan_yields_for_pageQlockwaiters, CTLFLAG_RW | CTLFLAG_LOCKED, &vps_yield_for_pgqlockwaiters, 0, "");
4531 #endif
4532 
4533 extern int      vm_compressor_mode;
4534 extern int      vm_compressor_is_active;
4535 extern int      vm_compressor_available;
4536 extern uint32_t c_seg_bufsize;
4537 extern uint32_t c_seg_allocsize;
4538 extern int      c_seg_fixed_array_len;
4539 extern uint32_t c_segments_limit;
4540 extern uint32_t c_segment_pages_compressed_limit;
4541 extern uint64_t compressor_pool_size;
4542 extern uint32_t compressor_pool_multiplier;
4543 extern uint32_t vm_ripe_target_age;
4544 extern uint32_t swapout_target_age;
4545 extern _Atomic uint64_t compressor_bytes_used;
4546 extern _Atomic uint64_t c_segment_input_bytes;
4547 extern _Atomic uint64_t c_segment_compressed_bytes;
4548 extern uint32_t c_segment_pages_compressed;
4549 extern uint32_t compressor_eval_period_in_msecs;
4550 extern uint32_t compressor_sample_min_in_msecs;
4551 extern uint32_t compressor_sample_max_in_msecs;
4552 extern uint32_t compressor_thrashing_threshold_per_10msecs;
4553 extern uint32_t compressor_thrashing_min_per_10msecs;
4554 extern uint32_t vm_compressor_time_thread;
4555 extern uint32_t c_segment_svp_in_hash;
4556 extern uint32_t c_segment_svp_hash_succeeded;
4557 extern uint32_t c_segment_svp_hash_failed;
4558 
4559 #if DEVELOPMENT || DEBUG
4560 extern uint32_t vm_compressor_minorcompact_threshold_divisor;
4561 extern uint32_t vm_compressor_majorcompact_threshold_divisor;
4562 extern uint32_t vm_compressor_unthrottle_threshold_divisor;
4563 extern uint32_t vm_compressor_catchup_threshold_divisor;
4564 
4565 extern uint32_t vm_compressor_minorcompact_threshold_divisor_overridden;
4566 extern uint32_t vm_compressor_majorcompact_threshold_divisor_overridden;
4567 extern uint32_t vm_compressor_unthrottle_threshold_divisor_overridden;
4568 extern uint32_t vm_compressor_catchup_threshold_divisor_overridden;
4569 
4570 extern vmct_stats_t vmct_stats;
4571 
4572 
4573 STATIC int
4574 sysctl_minorcompact_threshold_divisor(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
4575 {
4576 	int new_value, changed;
4577 	int error = sysctl_io_number(req, vm_compressor_minorcompact_threshold_divisor, sizeof(int), &new_value, &changed);
4578 
4579 	if (changed) {
4580 		vm_compressor_minorcompact_threshold_divisor = new_value;
4581 		vm_compressor_minorcompact_threshold_divisor_overridden = 1;
4582 	}
4583 	return error;
4584 }
4585 
4586 SYSCTL_PROC(_vm, OID_AUTO, compressor_minorcompact_threshold_divisor,
4587     CTLTYPE_INT | CTLFLAG_LOCKED | CTLFLAG_RW,
4588     0, 0, sysctl_minorcompact_threshold_divisor, "I", "");
4589 
4590 
4591 STATIC int
4592 sysctl_majorcompact_threshold_divisor(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
4593 {
4594 	int new_value, changed;
4595 	int error = sysctl_io_number(req, vm_compressor_majorcompact_threshold_divisor, sizeof(int), &new_value, &changed);
4596 
4597 	if (changed) {
4598 		vm_compressor_majorcompact_threshold_divisor = new_value;
4599 		vm_compressor_majorcompact_threshold_divisor_overridden = 1;
4600 	}
4601 	return error;
4602 }
4603 
4604 SYSCTL_PROC(_vm, OID_AUTO, compressor_majorcompact_threshold_divisor,
4605     CTLTYPE_INT | CTLFLAG_LOCKED | CTLFLAG_RW,
4606     0, 0, sysctl_majorcompact_threshold_divisor, "I", "");
4607 
4608 
4609 STATIC int
4610 sysctl_unthrottle_threshold_divisor(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
4611 {
4612 	int new_value, changed;
4613 	int error = sysctl_io_number(req, vm_compressor_unthrottle_threshold_divisor, sizeof(int), &new_value, &changed);
4614 
4615 	if (changed) {
4616 		vm_compressor_unthrottle_threshold_divisor = new_value;
4617 		vm_compressor_unthrottle_threshold_divisor_overridden = 1;
4618 	}
4619 	return error;
4620 }
4621 
4622 SYSCTL_PROC(_vm, OID_AUTO, compressor_unthrottle_threshold_divisor,
4623     CTLTYPE_INT | CTLFLAG_LOCKED | CTLFLAG_RW,
4624     0, 0, sysctl_unthrottle_threshold_divisor, "I", "");
4625 
4626 
4627 STATIC int
4628 sysctl_catchup_threshold_divisor(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
4629 {
4630 	int new_value, changed;
4631 	int error = sysctl_io_number(req, vm_compressor_catchup_threshold_divisor, sizeof(int), &new_value, &changed);
4632 
4633 	if (changed) {
4634 		vm_compressor_catchup_threshold_divisor = new_value;
4635 		vm_compressor_catchup_threshold_divisor_overridden = 1;
4636 	}
4637 	return error;
4638 }
4639 
4640 SYSCTL_PROC(_vm, OID_AUTO, compressor_catchup_threshold_divisor,
4641     CTLTYPE_INT | CTLFLAG_LOCKED | CTLFLAG_RW,
4642     0, 0, sysctl_catchup_threshold_divisor, "I", "");
4643 #endif
4644 
4645 
4646 SYSCTL_QUAD(_vm, OID_AUTO, compressor_input_bytes, CTLFLAG_RD | CTLFLAG_LOCKED, ((uint64_t *)&c_segment_input_bytes), "");
4647 SYSCTL_QUAD(_vm, OID_AUTO, compressor_compressed_bytes, CTLFLAG_RD | CTLFLAG_LOCKED, ((uint64_t *)&c_segment_compressed_bytes), "");
4648 SYSCTL_QUAD(_vm, OID_AUTO, compressor_bytes_used, CTLFLAG_RD | CTLFLAG_LOCKED, ((uint64_t *)&compressor_bytes_used), "");
4649 
4650 SYSCTL_INT(_vm, OID_AUTO, compressor_mode, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_compressor_mode, 0, "");
4651 SYSCTL_INT(_vm, OID_AUTO, compressor_is_active, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_compressor_is_active, 0, "");
4652 SYSCTL_INT(_vm, OID_AUTO, compressor_swapout_target_age, CTLFLAG_RD | CTLFLAG_LOCKED, &swapout_target_age, 0, "");
4653 SYSCTL_INT(_vm, OID_AUTO, compressor_available, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_compressor_available, 0, "");
4654 SYSCTL_INT(_vm, OID_AUTO, compressor_segment_buffer_size, CTLFLAG_RD | CTLFLAG_LOCKED, &c_seg_bufsize, 0, "");
4655 SYSCTL_QUAD(_vm, OID_AUTO, compressor_pool_size, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_pool_size, "");
4656 SYSCTL_UINT(_vm, OID_AUTO, compressor_pool_multiplier, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_pool_multiplier, 0, "");
4657 SYSCTL_INT(_vm, OID_AUTO, compressor_segment_slots_fixed_array_len, CTLFLAG_RD | CTLFLAG_LOCKED, &c_seg_fixed_array_len, 0, "");
4658 SYSCTL_UINT(_vm, OID_AUTO, compressor_segment_limit, CTLFLAG_RD | CTLFLAG_LOCKED, &c_segments_limit, 0, "");
4659 SYSCTL_UINT(_vm, OID_AUTO, compressor_segment_pages_compressed_limit, CTLFLAG_RD | CTLFLAG_LOCKED, &c_segment_pages_compressed_limit, 0, "");
4660 SYSCTL_UINT(_vm, OID_AUTO, compressor_segment_alloc_size, CTLFLAG_RD | CTLFLAG_LOCKED, &c_seg_allocsize, 0, "");
4661 SYSCTL_UINT(_vm, OID_AUTO, compressor_segment_pages_compressed, CTLFLAG_RD | CTLFLAG_LOCKED, &c_segment_pages_compressed, 0, "");
4662 SYSCTL_UINT(_vm, OID_AUTO, compressor_segment_svp_in_hash, CTLFLAG_RD | CTLFLAG_LOCKED, &c_segment_svp_in_hash, 0, "");
4663 SYSCTL_UINT(_vm, OID_AUTO, compressor_segment_svp_hash_succeeded, CTLFLAG_RD | CTLFLAG_LOCKED, &c_segment_svp_hash_succeeded, 0, "");
4664 SYSCTL_UINT(_vm, OID_AUTO, compressor_segment_svp_hash_failed, CTLFLAG_RD | CTLFLAG_LOCKED, &c_segment_svp_hash_failed, 0, "");
4665 
4666 #if CONFIG_TRACK_UNMODIFIED_ANON_PAGES
4667 extern uint64_t compressor_ro_uncompressed;
4668 extern uint64_t compressor_ro_uncompressed_total_returned;
4669 extern uint64_t compressor_ro_uncompressed_skip_returned;
4670 extern uint64_t compressor_ro_uncompressed_get;
4671 extern uint64_t compressor_ro_uncompressed_put;
4672 extern uint64_t compressor_ro_uncompressed_swap_usage;
4673 
4674 SYSCTL_QUAD(_vm, OID_AUTO, compressor_ro_uncompressed_total_returned, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_ro_uncompressed_total_returned, "");
4675 SYSCTL_QUAD(_vm, OID_AUTO, compressor_ro_uncompressed_writes_saved, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_ro_uncompressed_skip_returned, "");
4676 SYSCTL_QUAD(_vm, OID_AUTO, compressor_ro_uncompressed_candidates, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_ro_uncompressed, "");
4677 SYSCTL_QUAD(_vm, OID_AUTO, compressor_ro_uncompressed_rereads, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_ro_uncompressed_get, "");
4678 SYSCTL_QUAD(_vm, OID_AUTO, compressor_ro_uncompressed_swap_pages_on_disk, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_ro_uncompressed_swap_usage, "");
4679 #endif /* CONFIG_TRACK_UNMODIFIED_ANON_PAGES */
4680 
4681 extern int min_csegs_per_major_compaction;
4682 SYSCTL_INT(_vm, OID_AUTO, compressor_min_csegs_per_major_compaction, CTLFLAG_RW | CTLFLAG_LOCKED, &min_csegs_per_major_compaction, 0, "");
4683 
4684 SYSCTL_INT(_vm, OID_AUTO, vm_ripe_target_age_in_secs, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_ripe_target_age, 0, "");
4685 
4686 SYSCTL_INT(_vm, OID_AUTO, compressor_eval_period_in_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &compressor_eval_period_in_msecs, 0, "");
4687 SYSCTL_INT(_vm, OID_AUTO, compressor_sample_min_in_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &compressor_sample_min_in_msecs, 0, "");
4688 SYSCTL_INT(_vm, OID_AUTO, compressor_sample_max_in_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &compressor_sample_max_in_msecs, 0, "");
4689 SYSCTL_INT(_vm, OID_AUTO, compressor_thrashing_threshold_per_10msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &compressor_thrashing_threshold_per_10msecs, 0, "");
4690 SYSCTL_INT(_vm, OID_AUTO, compressor_thrashing_min_per_10msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &compressor_thrashing_min_per_10msecs, 0, "");
4691 
4692 SYSCTL_QUAD(_vm, OID_AUTO, compressor_swapouts_under_30s, CTLFLAG_RD | CTLFLAG_LOCKED, &vmcs_stats.unripe_under_30s, "");
4693 SYSCTL_QUAD(_vm, OID_AUTO, compressor_swapouts_under_60s, CTLFLAG_RD | CTLFLAG_LOCKED, &vmcs_stats.unripe_under_60s, "");
4694 SYSCTL_QUAD(_vm, OID_AUTO, compressor_swapouts_under_300s, CTLFLAG_RD | CTLFLAG_LOCKED, &vmcs_stats.unripe_under_300s, "");
4695 SYSCTL_QUAD(_vm, OID_AUTO, compressor_swapper_reclaim_swapins, CTLFLAG_RD | CTLFLAG_LOCKED, &vmcs_stats.reclaim_swapins, "");
4696 SYSCTL_QUAD(_vm, OID_AUTO, compressor_swapper_defrag_swapins, CTLFLAG_RD | CTLFLAG_LOCKED, &vmcs_stats.defrag_swapins, "");
4697 SYSCTL_QUAD(_vm, OID_AUTO, compressor_swapper_swapout_threshold_exceeded, CTLFLAG_RD | CTLFLAG_LOCKED, &vmcs_stats.compressor_swap_threshold_exceeded, "");
4698 SYSCTL_QUAD(_vm, OID_AUTO, compressor_swapper_swapout_fileq_throttled, CTLFLAG_RD | CTLFLAG_LOCKED, &vmcs_stats.external_q_throttled, "");
4699 SYSCTL_QUAD(_vm, OID_AUTO, compressor_swapper_swapout_free_count_low, CTLFLAG_RD | CTLFLAG_LOCKED, &vmcs_stats.free_count_below_reserve, "");
4700 SYSCTL_QUAD(_vm, OID_AUTO, compressor_swapper_swapout_thrashing_detected, CTLFLAG_RD | CTLFLAG_LOCKED, &vmcs_stats.thrashing_detected, "");
4701 SYSCTL_QUAD(_vm, OID_AUTO, compressor_swapper_swapout_fragmentation_detected, CTLFLAG_RD | CTLFLAG_LOCKED, &vmcs_stats.fragmentation_detected, "");
4702 
4703 SYSCTL_STRING(_vm, OID_AUTO, swapfileprefix, CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED, swapfilename, sizeof(swapfilename) - SWAPFILENAME_INDEX_LEN, "");
4704 
4705 SYSCTL_INT(_vm, OID_AUTO, compressor_timing_enabled, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_compressor_time_thread, 0, "");
4706 
4707 #if DEVELOPMENT || DEBUG
4708 SYSCTL_QUAD(_vm, OID_AUTO, compressor_thread_runtime0, CTLFLAG_RD | CTLFLAG_LOCKED, &vmct_stats.vmct_runtimes[0], "");
4709 SYSCTL_QUAD(_vm, OID_AUTO, compressor_thread_runtime1, CTLFLAG_RD | CTLFLAG_LOCKED, &vmct_stats.vmct_runtimes[1], "");
4710 
4711 SYSCTL_QUAD(_vm, OID_AUTO, compressor_threads_total_execution_time, CTLFLAG_RD | CTLFLAG_LOCKED, &vmct_stats.vmct_cthreads_total, "");
4712 
4713 SYSCTL_QUAD(_vm, OID_AUTO, compressor_thread_pages0, CTLFLAG_RD | CTLFLAG_LOCKED, &vmct_stats.vmct_pages[0], "");
4714 SYSCTL_QUAD(_vm, OID_AUTO, compressor_thread_pages1, CTLFLAG_RD | CTLFLAG_LOCKED, &vmct_stats.vmct_pages[1], "");
4715 
4716 SYSCTL_QUAD(_vm, OID_AUTO, compressor_thread_iterations0, CTLFLAG_RD | CTLFLAG_LOCKED, &vmct_stats.vmct_iterations[0], "");
4717 SYSCTL_QUAD(_vm, OID_AUTO, compressor_thread_iterations1, CTLFLAG_RD | CTLFLAG_LOCKED, &vmct_stats.vmct_iterations[1], "");
4718 
4719 SYSCTL_INT(_vm, OID_AUTO, compressor_thread_minpages0, CTLFLAG_RD | CTLFLAG_LOCKED, &vmct_stats.vmct_minpages[0], 0, "");
4720 SYSCTL_INT(_vm, OID_AUTO, compressor_thread_minpages1, CTLFLAG_RD | CTLFLAG_LOCKED, &vmct_stats.vmct_minpages[1], 0, "");
4721 
4722 SYSCTL_INT(_vm, OID_AUTO, compressor_thread_maxpages0, CTLFLAG_RD | CTLFLAG_LOCKED, &vmct_stats.vmct_maxpages[0], 0, "");
4723 SYSCTL_INT(_vm, OID_AUTO, compressor_thread_maxpages1, CTLFLAG_RD | CTLFLAG_LOCKED, &vmct_stats.vmct_maxpages[1], 0, "");
4724 
4725 int vm_compressor_injected_error_count;
4726 
4727 SYSCTL_INT(_vm, OID_AUTO, compressor_injected_error_count, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_compressor_injected_error_count, 0, "");
4728 
4729 static int
4730 sysctl_compressor_inject_error(__unused struct sysctl_oid *oidp,
4731     __unused void *arg1, __unused int arg2, struct sysctl_req *req)
4732 {
4733 	int result;
4734 	vm_address_t va = 0;
4735 	int changed;
4736 
4737 	result = sysctl_io_number(req, va, sizeof(va), &va, &changed);
4738 	if (result == 0 && changed) {
4739 		result = vm_map_inject_error(current_map(), va);
4740 		if (result == 0) {
4741 			/*
4742 			 * Count the number of errors injected successfully to detect
4743 			 * situations where corruption was caused by improper use of this
4744 			 * sysctl.
4745 			 */
4746 			os_atomic_inc(&vm_compressor_injected_error_count, relaxed);
4747 		}
4748 	}
4749 	return result;
4750 }
4751 
4752 SYSCTL_PROC(_vm, OID_AUTO, compressor_inject_error, CTLTYPE_QUAD | CTLFLAG_LOCKED | CTLFLAG_RW,
4753     0, 0, sysctl_compressor_inject_error, "Q", "flips a bit in a compressed page for the current task");
4754 
4755 /*
4756  * Opt a process in/out of self donation mode.
4757  */
4758 static int
4759 sysctl_vm_pid_toggle_selfdonate_pages SYSCTL_HANDLER_ARGS
4760 {
4761 #pragma unused(arg1, arg2)
4762 	int error, pid = 0;
4763 	proc_t p;
4764 
4765 	error = sysctl_handle_int(oidp, &pid, 0, req);
4766 	if (error || !req->newptr) {
4767 		return error;
4768 	}
4769 
4770 	p = proc_find(pid);
4771 	if (p != NULL) {
4772 		(void) vm_toggle_task_selfdonate_pages(proc_task(p));
4773 		proc_rele(p);
4774 		return error;
4775 	} else {
4776 		printf("sysctl_vm_pid_selfdonate_pages: Invalid process\n");
4777 	}
4778 
4779 	return EINVAL;
4780 }
4781 SYSCTL_PROC(_vm, OID_AUTO, pid_toggle_selfdonate_pages, CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED | CTLFLAG_MASKED,
4782     0, 0, &sysctl_vm_pid_toggle_selfdonate_pages, "I", "");
4783 #endif /* DEVELOPMENT || DEBUG */
4784 extern uint32_t vm_page_donate_mode;
4785 extern uint32_t vm_page_donate_target_high, vm_page_donate_target_low;
4786 SYSCTL_INT(_vm, OID_AUTO, vm_page_donate_mode, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_page_donate_mode, 0, "");
4787 SYSCTL_INT(_vm, OID_AUTO, vm_page_donate_target_high, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_page_donate_target_high, 0, "");
4788 SYSCTL_INT(_vm, OID_AUTO, vm_page_donate_target_low, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_page_donate_target_low, 0, "");
4789 
4790 SYSCTL_QUAD(_vm, OID_AUTO, lz4_compressions, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.lz4_compressions, "");
4791 SYSCTL_QUAD(_vm, OID_AUTO, lz4_compression_failures, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.lz4_compression_failures, "");
4792 SYSCTL_QUAD(_vm, OID_AUTO, lz4_compressed_bytes, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.lz4_compressed_bytes, "");
4793 SYSCTL_QUAD(_vm, OID_AUTO, lz4_wk_compression_delta, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.lz4_wk_compression_delta, "");
4794 SYSCTL_QUAD(_vm, OID_AUTO, lz4_wk_compression_negative_delta, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.lz4_wk_compression_negative_delta, "");
4795 
4796 SYSCTL_QUAD(_vm, OID_AUTO, lz4_decompressions, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.lz4_decompressions, "");
4797 SYSCTL_QUAD(_vm, OID_AUTO, lz4_decompressed_bytes, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.lz4_decompressed_bytes, "");
4798 
4799 SYSCTL_QUAD(_vm, OID_AUTO, uc_decompressions, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.uc_decompressions, "");
4800 
4801 SYSCTL_QUAD(_vm, OID_AUTO, wk_compressions, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wk_compressions, "");
4802 
4803 SYSCTL_QUAD(_vm, OID_AUTO, wk_catime, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wk_cabstime, "");
4804 
4805 SYSCTL_QUAD(_vm, OID_AUTO, wkh_catime, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wkh_cabstime, "");
4806 SYSCTL_QUAD(_vm, OID_AUTO, wkh_compressions, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wkh_compressions, "");
4807 
4808 SYSCTL_QUAD(_vm, OID_AUTO, wks_catime, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wks_cabstime, "");
4809 SYSCTL_QUAD(_vm, OID_AUTO, wks_compressions, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wks_compressions, "");
4810 
4811 SYSCTL_QUAD(_vm, OID_AUTO, wk_compressions_exclusive, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wk_compressions_exclusive, "");
4812 SYSCTL_QUAD(_vm, OID_AUTO, wk_sv_compressions, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wk_sv_compressions, "");
4813 SYSCTL_QUAD(_vm, OID_AUTO, wk_mzv_compressions, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wk_mzv_compressions, "");
4814 SYSCTL_QUAD(_vm, OID_AUTO, wk_compression_failures, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wk_compression_failures, "");
4815 SYSCTL_QUAD(_vm, OID_AUTO, wk_compressed_bytes_exclusive, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wk_compressed_bytes_exclusive, "");
4816 SYSCTL_QUAD(_vm, OID_AUTO, wk_compressed_bytes_total, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wk_compressed_bytes_total, "");
4817 
4818 SYSCTL_QUAD(_vm, OID_AUTO, wks_compressed_bytes, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wks_compressed_bytes, "");
4819 SYSCTL_QUAD(_vm, OID_AUTO, wks_compression_failures, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wks_compression_failures, "");
4820 SYSCTL_QUAD(_vm, OID_AUTO, wks_sv_compressions, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wks_sv_compressions, "");
4821 
4822 
4823 SYSCTL_QUAD(_vm, OID_AUTO, wk_decompressions, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wk_decompressions, "");
4824 
4825 SYSCTL_QUAD(_vm, OID_AUTO, wk_datime, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wk_dabstime, "");
4826 
4827 SYSCTL_QUAD(_vm, OID_AUTO, wkh_datime, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wkh_dabstime, "");
4828 SYSCTL_QUAD(_vm, OID_AUTO, wkh_decompressions, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wkh_decompressions, "");
4829 
4830 SYSCTL_QUAD(_vm, OID_AUTO, wks_datime, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wks_dabstime, "");
4831 SYSCTL_QUAD(_vm, OID_AUTO, wks_decompressions, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wks_decompressions, "");
4832 
4833 SYSCTL_QUAD(_vm, OID_AUTO, wk_decompressed_bytes, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wk_decompressed_bytes, "");
4834 SYSCTL_QUAD(_vm, OID_AUTO, wk_sv_decompressions, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wk_sv_decompressions, "");
4835 
4836 SYSCTL_INT(_vm, OID_AUTO, lz4_threshold, CTLFLAG_RW | CTLFLAG_LOCKED, &vmctune.lz4_threshold, 0, "");
4837 SYSCTL_INT(_vm, OID_AUTO, wkdm_reeval_threshold, CTLFLAG_RW | CTLFLAG_LOCKED, &vmctune.wkdm_reeval_threshold, 0, "");
4838 SYSCTL_INT(_vm, OID_AUTO, lz4_max_failure_skips, CTLFLAG_RW | CTLFLAG_LOCKED, &vmctune.lz4_max_failure_skips, 0, "");
4839 SYSCTL_INT(_vm, OID_AUTO, lz4_max_failure_run_length, CTLFLAG_RW | CTLFLAG_LOCKED, &vmctune.lz4_max_failure_run_length, 0, "");
4840 SYSCTL_INT(_vm, OID_AUTO, lz4_max_preselects, CTLFLAG_RW | CTLFLAG_LOCKED, &vmctune.lz4_max_preselects, 0, "");
4841 SYSCTL_INT(_vm, OID_AUTO, lz4_run_preselection_threshold, CTLFLAG_RW | CTLFLAG_LOCKED, &vmctune.lz4_run_preselection_threshold, 0, "");
4842 SYSCTL_INT(_vm, OID_AUTO, lz4_run_continue_bytes, CTLFLAG_RW | CTLFLAG_LOCKED, &vmctune.lz4_run_continue_bytes, 0, "");
4843 SYSCTL_INT(_vm, OID_AUTO, lz4_profitable_bytes, CTLFLAG_RW | CTLFLAG_LOCKED, &vmctune.lz4_profitable_bytes, 0, "");
4844 #if DEVELOPMENT || DEBUG
4845 extern int vm_compressor_current_codec;
4846 extern int vm_compressor_test_seg_wp;
4847 extern boolean_t vm_compressor_force_sw_wkdm;
4848 SYSCTL_INT(_vm, OID_AUTO, compressor_codec, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_compressor_current_codec, 0, "");
4849 SYSCTL_INT(_vm, OID_AUTO, compressor_test_wp, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_compressor_test_seg_wp, 0, "");
4850 
4851 SYSCTL_INT(_vm, OID_AUTO, wksw_force, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_compressor_force_sw_wkdm, 0, "");
4852 extern int precompy, wkswhw;
4853 
4854 SYSCTL_INT(_vm, OID_AUTO, precompy, CTLFLAG_RW | CTLFLAG_LOCKED, &precompy, 0, "");
4855 SYSCTL_INT(_vm, OID_AUTO, wkswhw, CTLFLAG_RW | CTLFLAG_LOCKED, &wkswhw, 0, "");
4856 extern unsigned int vm_ktrace_enabled;
4857 SYSCTL_INT(_vm, OID_AUTO, vm_ktrace, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_ktrace_enabled, 0, "");
4858 #endif /* DEVELOPMENT || DEBUG */
4859 
4860 #if CONFIG_PHANTOM_CACHE
4861 extern uint32_t phantom_cache_thrashing_threshold;
4862 extern uint32_t phantom_cache_eval_period_in_msecs;
4863 extern uint32_t phantom_cache_thrashing_threshold_ssd;
4864 
4865 
4866 SYSCTL_INT(_vm, OID_AUTO, phantom_cache_eval_period_in_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &phantom_cache_eval_period_in_msecs, 0, "");
4867 SYSCTL_INT(_vm, OID_AUTO, phantom_cache_thrashing_threshold, CTLFLAG_RW | CTLFLAG_LOCKED, &phantom_cache_thrashing_threshold, 0, "");
4868 SYSCTL_INT(_vm, OID_AUTO, phantom_cache_thrashing_threshold_ssd, CTLFLAG_RW | CTLFLAG_LOCKED, &phantom_cache_thrashing_threshold_ssd, 0, "");
4869 #endif
4870 
4871 #if    defined(__LP64__)
4872 extern uint32_t vm_page_background_count;
4873 extern uint32_t vm_page_background_target;
4874 extern uint32_t vm_page_background_internal_count;
4875 extern uint32_t vm_page_background_external_count;
4876 extern uint32_t vm_page_background_mode;
4877 extern uint32_t vm_page_background_exclude_external;
4878 extern uint64_t vm_page_background_promoted_count;
4879 extern uint64_t vm_pageout_rejected_bq_internal;
4880 extern uint64_t vm_pageout_rejected_bq_external;
4881 
4882 SYSCTL_INT(_vm, OID_AUTO, vm_page_background_mode, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_page_background_mode, 0, "");
4883 SYSCTL_INT(_vm, OID_AUTO, vm_page_background_exclude_external, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_page_background_exclude_external, 0, "");
4884 SYSCTL_INT(_vm, OID_AUTO, vm_page_background_target, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_page_background_target, 0, "");
4885 SYSCTL_INT(_vm, OID_AUTO, vm_page_background_count, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_page_background_count, 0, "");
4886 SYSCTL_INT(_vm, OID_AUTO, vm_page_background_internal_count, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_page_background_internal_count, 0, "");
4887 SYSCTL_INT(_vm, OID_AUTO, vm_page_background_external_count, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_page_background_external_count, 0, "");
4888 
4889 SYSCTL_QUAD(_vm, OID_AUTO, vm_page_background_promoted_count, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_page_background_promoted_count, "");
4890 SYSCTL_QUAD(_vm, OID_AUTO, vm_pageout_considered_bq_internal, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_pageout_vminfo.vm_pageout_considered_bq_internal, "");
4891 SYSCTL_QUAD(_vm, OID_AUTO, vm_pageout_considered_bq_external, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_pageout_vminfo.vm_pageout_considered_bq_external, "");
4892 SYSCTL_QUAD(_vm, OID_AUTO, vm_pageout_rejected_bq_internal, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_pageout_rejected_bq_internal, "");
4893 SYSCTL_QUAD(_vm, OID_AUTO, vm_pageout_rejected_bq_external, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_pageout_rejected_bq_external, "");
4894 
4895 #endif /* __LP64__ */
4896 
4897 extern boolean_t vm_darkwake_mode;
4898 
4899 STATIC int
4900 sysctl_toggle_darkwake_mode(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
4901 {
4902 	int new_value, changed;
4903 	int error = sysctl_io_number(req, vm_darkwake_mode, sizeof(int), &new_value, &changed);
4904 
4905 	if (!error && changed) {
4906 		if (new_value != 0 && new_value != 1) {
4907 			printf("Error: Invalid value passed to darkwake sysctl. Acceptable: 0 or 1.\n");
4908 			error = EINVAL;
4909 		} else {
4910 			vm_update_darkwake_mode((boolean_t) new_value);
4911 		}
4912 	}
4913 
4914 	return error;
4915 }
4916 
4917 SYSCTL_PROC(_vm, OID_AUTO, darkwake_mode,
4918     CTLTYPE_INT | CTLFLAG_LOCKED | CTLFLAG_RW,
4919     0, 0, sysctl_toggle_darkwake_mode, "I", "");
4920 
4921 #if (DEVELOPMENT || DEBUG)
4922 
4923 SYSCTL_UINT(_vm, OID_AUTO, vm_page_creation_throttled_hard,
4924     CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
4925     &vm_page_creation_throttled_hard, 0, "");
4926 
4927 SYSCTL_UINT(_vm, OID_AUTO, vm_page_creation_throttled_soft,
4928     CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
4929     &vm_page_creation_throttled_soft, 0, "");
4930 
4931 extern uint32_t vm_pageout_memorystatus_fb_factor_nr;
4932 extern uint32_t vm_pageout_memorystatus_fb_factor_dr;
4933 SYSCTL_INT(_vm, OID_AUTO, vm_pageout_memorystatus_fb_factor_nr, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_pageout_memorystatus_fb_factor_nr, 0, "");
4934 SYSCTL_INT(_vm, OID_AUTO, vm_pageout_memorystatus_fb_factor_dr, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_pageout_memorystatus_fb_factor_dr, 0, "");
4935 
4936 extern uint32_t vm_grab_anon_nops;
4937 
4938 SYSCTL_INT(_vm, OID_AUTO, vm_grab_anon_overrides, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_pageout_debug.vm_grab_anon_overrides, 0, "");
4939 SYSCTL_INT(_vm, OID_AUTO, vm_grab_anon_nops, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_pageout_debug.vm_grab_anon_nops, 0, "");
4940 SYSCTL_INT(_vm, OID_AUTO, vm_pageout_yield_for_free_pages, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_pageout_debug.vm_pageout_yield_for_free_pages, 0, "");
4941 
4942 
4943 extern int vm_page_delayed_work_ctx_needed;
4944 SYSCTL_INT(_vm, OID_AUTO, vm_page_needed_delayed_work_ctx, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_page_delayed_work_ctx_needed, 0, "");
4945 
4946 
4947 /* log message counters for persistence mode */
4948 SCALABLE_COUNTER_DECLARE(oslog_p_total_msgcount);
4949 SCALABLE_COUNTER_DECLARE(oslog_p_metadata_saved_msgcount);
4950 SCALABLE_COUNTER_DECLARE(oslog_p_metadata_dropped_msgcount);
4951 SCALABLE_COUNTER_DECLARE(oslog_p_signpost_saved_msgcount);
4952 SCALABLE_COUNTER_DECLARE(oslog_p_signpost_dropped_msgcount);
4953 SCALABLE_COUNTER_DECLARE(oslog_p_error_count);
4954 SCALABLE_COUNTER_DECLARE(oslog_p_error_count);
4955 SCALABLE_COUNTER_DECLARE(oslog_p_saved_msgcount);
4956 SCALABLE_COUNTER_DECLARE(oslog_p_dropped_msgcount);
4957 SCALABLE_COUNTER_DECLARE(oslog_p_boot_dropped_msgcount);
4958 SCALABLE_COUNTER_DECLARE(oslog_p_coprocessor_total_msgcount);
4959 SCALABLE_COUNTER_DECLARE(oslog_p_coprocessor_dropped_msgcount);
4960 SCALABLE_COUNTER_DECLARE(oslog_p_unresolved_kc_msgcount);
4961 SCALABLE_COUNTER_DECLARE(oslog_p_fmt_invalid_msgcount);
4962 SCALABLE_COUNTER_DECLARE(oslog_p_fmt_max_args_msgcount);
4963 SCALABLE_COUNTER_DECLARE(oslog_p_truncated_msgcount);
4964 
4965 SCALABLE_COUNTER_DECLARE(oslog_subsystem_count);
4966 SCALABLE_COUNTER_DECLARE(oslog_subsystem_found);
4967 SCALABLE_COUNTER_DECLARE(oslog_subsystem_dropped);
4968 
4969 SCALABLE_COUNTER_DECLARE(log_queue_cnt_received);
4970 SCALABLE_COUNTER_DECLARE(log_queue_cnt_rejected_fh);
4971 SCALABLE_COUNTER_DECLARE(log_queue_cnt_sent);
4972 SCALABLE_COUNTER_DECLARE(log_queue_cnt_dropped_nomem);
4973 SCALABLE_COUNTER_DECLARE(log_queue_cnt_queued);
4974 SCALABLE_COUNTER_DECLARE(log_queue_cnt_dropped_off);
4975 SCALABLE_COUNTER_DECLARE(log_queue_cnt_mem_active);
4976 SCALABLE_COUNTER_DECLARE(log_queue_cnt_mem_allocated);
4977 SCALABLE_COUNTER_DECLARE(log_queue_cnt_mem_released);
4978 SCALABLE_COUNTER_DECLARE(log_queue_cnt_mem_failed);
4979 
4980 /* log message counters for streaming mode */
4981 SCALABLE_COUNTER_DECLARE(oslog_s_total_msgcount);
4982 SCALABLE_COUNTER_DECLARE(oslog_s_metadata_msgcount);
4983 SCALABLE_COUNTER_DECLARE(oslog_s_error_count);
4984 SCALABLE_COUNTER_DECLARE(oslog_s_streamed_msgcount);
4985 SCALABLE_COUNTER_DECLARE(oslog_s_dropped_msgcount);
4986 
4987 /* log message counters for msgbuf logging */
4988 SCALABLE_COUNTER_DECLARE(oslog_msgbuf_msgcount);
4989 SCALABLE_COUNTER_DECLARE(oslog_msgbuf_dropped_msgcount);
4990 extern uint32_t oslog_msgbuf_dropped_charcount;
4991 
4992 #if CONFIG_EXCLAVES
4993 /* log message counters for exclaves logging */
4994 SCALABLE_COUNTER_DECLARE(oslog_e_log_count);
4995 SCALABLE_COUNTER_DECLARE(oslog_e_log_dropped_count);
4996 SCALABLE_COUNTER_DECLARE(oslog_e_metadata_count);
4997 SCALABLE_COUNTER_DECLARE(oslog_e_metadata_dropped_count);
4998 SCALABLE_COUNTER_DECLARE(oslog_e_signpost_count);
4999 SCALABLE_COUNTER_DECLARE(oslog_e_signpost_dropped_count);
5000 SCALABLE_COUNTER_DECLARE(oslog_e_replay_failure_count);
5001 SCALABLE_COUNTER_DECLARE(oslog_e_query_count);
5002 SCALABLE_COUNTER_DECLARE(oslog_e_query_error_count);
5003 SCALABLE_COUNTER_DECLARE(oslog_e_trace_mode_set_count);
5004 SCALABLE_COUNTER_DECLARE(oslog_e_trace_mode_error_count);
5005 #endif // CONFIG_EXCLAVES
5006 
5007 SYSCTL_SCALABLE_COUNTER(_debug, oslog_p_total_msgcount, oslog_p_total_msgcount, "");
5008 SYSCTL_SCALABLE_COUNTER(_debug, oslog_p_metadata_saved_msgcount, oslog_p_metadata_saved_msgcount, "");
5009 SYSCTL_SCALABLE_COUNTER(_debug, oslog_p_metadata_dropped_msgcount, oslog_p_metadata_dropped_msgcount, "");
5010 SYSCTL_SCALABLE_COUNTER(_debug, oslog_p_signpost_saved_msgcount, oslog_p_signpost_saved_msgcount, "");
5011 SYSCTL_SCALABLE_COUNTER(_debug, oslog_p_signpost_dropped_msgcount, oslog_p_signpost_dropped_msgcount, "");
5012 SYSCTL_SCALABLE_COUNTER(_debug, oslog_p_error_count, oslog_p_error_count, "");
5013 SYSCTL_SCALABLE_COUNTER(_debug, oslog_p_saved_msgcount, oslog_p_saved_msgcount, "");
5014 SYSCTL_SCALABLE_COUNTER(_debug, oslog_p_dropped_msgcount, oslog_p_dropped_msgcount, "");
5015 SYSCTL_SCALABLE_COUNTER(_debug, oslog_p_boot_dropped_msgcount, oslog_p_boot_dropped_msgcount, "");
5016 SYSCTL_SCALABLE_COUNTER(_debug, oslog_p_coprocessor_total_msgcount, oslog_p_coprocessor_total_msgcount, "");
5017 SYSCTL_SCALABLE_COUNTER(_debug, oslog_p_coprocessor_dropped_msgcount, oslog_p_coprocessor_dropped_msgcount, "");
5018 SYSCTL_SCALABLE_COUNTER(_debug, oslog_p_unresolved_kc_msgcount, oslog_p_unresolved_kc_msgcount, "");
5019 
5020 SYSCTL_SCALABLE_COUNTER(_debug, oslog_p_fmt_invalid_msgcount, oslog_p_fmt_invalid_msgcount, "");
5021 SYSCTL_SCALABLE_COUNTER(_debug, oslog_p_fmt_max_args_msgcount, oslog_p_fmt_max_args_msgcount, "");
5022 SYSCTL_SCALABLE_COUNTER(_debug, oslog_p_truncated_msgcount, oslog_p_truncated_msgcount, "");
5023 
5024 SYSCTL_SCALABLE_COUNTER(_debug, oslog_s_total_msgcount, oslog_s_total_msgcount, "Number of logs sent to streaming");
5025 SYSCTL_SCALABLE_COUNTER(_debug, oslog_s_metadata_msgcount, oslog_s_metadata_msgcount, "Number of metadata sent to streaming");
5026 SYSCTL_SCALABLE_COUNTER(_debug, oslog_s_error_count, oslog_s_error_count, "Number of invalid stream logs");
5027 SYSCTL_SCALABLE_COUNTER(_debug, oslog_s_streamed_msgcount, oslog_s_streamed_msgcount, "Number of streamed logs");
5028 SYSCTL_SCALABLE_COUNTER(_debug, oslog_s_dropped_msgcount, oslog_s_dropped_msgcount, "Number of logs dropped from stream");
5029 
5030 SYSCTL_SCALABLE_COUNTER(_debug, oslog_msgbuf_msgcount, oslog_msgbuf_msgcount, "Number of dmesg log messages");
5031 SYSCTL_SCALABLE_COUNTER(_debug, oslog_msgbuf_dropped_msgcount, oslog_msgbuf_dropped_msgcount, "Number of dropped dmesg log messages");
5032 SYSCTL_UINT(_debug, OID_AUTO, oslog_msgbuf_dropped_charcount, CTLFLAG_ANYBODY | CTLFLAG_RD | CTLFLAG_LOCKED, &oslog_msgbuf_dropped_charcount, 0, "Number of dropped dmesg log chars");
5033 
5034 SYSCTL_SCALABLE_COUNTER(_debug, log_queue_cnt_received, log_queue_cnt_received, "Number of received logs");
5035 SYSCTL_SCALABLE_COUNTER(_debug, log_queue_cnt_rejected_fh, log_queue_cnt_rejected_fh, "Number of logs initially rejected by FH");
5036 SYSCTL_SCALABLE_COUNTER(_debug, log_queue_cnt_sent, log_queue_cnt_sent, "Number of logs successfully saved in FH");
5037 SYSCTL_SCALABLE_COUNTER(_debug, log_queue_cnt_dropped_nomem, log_queue_cnt_dropped_nomem, "Number of logs dropped due to lack of queue memory");
5038 SYSCTL_SCALABLE_COUNTER(_debug, log_queue_cnt_queued, log_queue_cnt_queued, "Current number of logs stored in log queues");
5039 SYSCTL_SCALABLE_COUNTER(_debug, log_queue_cnt_dropped_off, log_queue_cnt_dropped_off, "Number of logs dropped due to disabled log queues");
5040 SYSCTL_SCALABLE_COUNTER(_debug, log_queue_cnt_mem_allocated, log_queue_cnt_mem_allocated, "Number of memory allocations");
5041 SYSCTL_SCALABLE_COUNTER(_debug, log_queue_cnt_mem_released, log_queue_cnt_mem_released, "Number of memory releases");
5042 SYSCTL_SCALABLE_COUNTER(_debug, log_queue_cnt_mem_failed, log_queue_cnt_mem_failed, "Number of failed memory allocations");
5043 
5044 SYSCTL_SCALABLE_COUNTER(_debug, oslog_subsystem_count, oslog_subsystem_count, "Number of registered log subsystems");
5045 SYSCTL_SCALABLE_COUNTER(_debug, oslog_subsystem_found, oslog_subsystem_found, "Number of sucessful log subsystem lookups");
5046 SYSCTL_SCALABLE_COUNTER(_debug, oslog_subsystem_dropped, oslog_subsystem_dropped, "Number of dropped log subsystem registrations");
5047 
5048 #if CONFIG_EXCLAVES
5049 SYSCTL_SCALABLE_COUNTER(_debug, oslog_e_metadata_count, oslog_e_metadata_count,
5050     "Number of metadata messages retrieved from the exclaves log server");
5051 SYSCTL_SCALABLE_COUNTER(_debug, oslog_e_metadata_dropped_count, oslog_e_metadata_dropped_count,
5052     "Number of dropped metadata messages retrieved from the exclaves log server");
5053 SYSCTL_SCALABLE_COUNTER(_debug, oslog_e_log_count, oslog_e_log_count,
5054     "Number of logs retrieved from the exclaves log server");
5055 SYSCTL_SCALABLE_COUNTER(_debug, oslog_e_log_dropped_count, oslog_e_log_dropped_count,
5056     "Number of dropeed logs retrieved from the exclaves log server");
5057 SYSCTL_SCALABLE_COUNTER(_debug, oslog_e_signpost_count, oslog_e_signpost_count,
5058     "Number of signposts retrieved from the exclaves log server");
5059 SYSCTL_SCALABLE_COUNTER(_debug, oslog_e_signpost_dropped_count, oslog_e_signpost_dropped_count,
5060     "Number of dropped signposts retrieved from the exclaves log server");
5061 SYSCTL_SCALABLE_COUNTER(_debug, oslog_e_replay_failure_count, oslog_e_replay_failure_count,
5062     "Number of dropped messages that couldn't be replayed and failed generically");
5063 SYSCTL_SCALABLE_COUNTER(_debug, oslog_e_query_count, oslog_e_query_count,
5064     "Number of sucessful queries to the exclaves log server");
5065 SYSCTL_SCALABLE_COUNTER(_debug, oslog_e_query_error_count, oslog_e_query_error_count,
5066     "Number of failed queries to the exclaves log server");
5067 SYSCTL_SCALABLE_COUNTER(_debug, oslog_e_trace_mode_set_count, oslog_e_trace_mode_set_count,
5068     "Number of exclaves trace mode updates");
5069 SYSCTL_SCALABLE_COUNTER(_debug, oslog_e_trace_mode_error_count, oslog_e_trace_mode_error_count,
5070     "Number of failed exclaves trace mode updates");
5071 #endif // CONFIG_EXCLAVES
5072 
5073 #endif /* DEVELOPMENT || DEBUG */
5074 
5075 /*
5076  * Enable tracing of voucher contents
5077  */
5078 extern uint32_t ipc_voucher_trace_contents;
5079 
5080 SYSCTL_INT(_kern, OID_AUTO, ipc_voucher_trace_contents,
5081     CTLFLAG_RW | CTLFLAG_LOCKED, &ipc_voucher_trace_contents, 0, "Enable tracing voucher contents");
5082 
5083 /*
5084  * Kernel stack size and depth
5085  */
5086 SYSCTL_INT(_kern, OID_AUTO, stack_size,
5087     CTLFLAG_RD | CTLFLAG_LOCKED, (int *) &kernel_stack_size, 0, "Kernel stack size");
5088 SYSCTL_INT(_kern, OID_AUTO, stack_depth_max,
5089     CTLFLAG_RD | CTLFLAG_LOCKED, (int *) &kernel_stack_depth_max, 0, "Max kernel stack depth at interrupt or context switch");
5090 
5091 extern unsigned int kern_feature_overrides;
5092 SYSCTL_INT(_kern, OID_AUTO, kern_feature_overrides,
5093     CTLFLAG_RD | CTLFLAG_LOCKED, &kern_feature_overrides, 0, "Kernel feature override mask");
5094 
5095 /*
5096  * enable back trace for port allocations
5097  */
5098 extern int ipc_portbt;
5099 
5100 SYSCTL_INT(_kern, OID_AUTO, ipc_portbt,
5101     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
5102     &ipc_portbt, 0, "");
5103 
5104 /*
5105  * Scheduler sysctls
5106  */
5107 
5108 SYSCTL_STRING(_kern, OID_AUTO, sched,
5109     CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
5110     sched_string, sizeof(sched_string),
5111     "Timeshare scheduler implementation");
5112 
5113 static int
5114 sysctl_cpu_quiescent_counter_interval SYSCTL_HANDLER_ARGS
5115 {
5116 #pragma unused(arg1, arg2)
5117 
5118 	uint32_t local_min_interval_us = smr_cpu_checkin_get_min_interval_us();
5119 
5120 	int error = sysctl_handle_int(oidp, &local_min_interval_us, 0, req);
5121 	if (error || !req->newptr) {
5122 		return error;
5123 	}
5124 
5125 	smr_cpu_checkin_set_min_interval_us(local_min_interval_us);
5126 
5127 	return 0;
5128 }
5129 
5130 SYSCTL_PROC(_kern, OID_AUTO, cpu_checkin_interval,
5131     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
5132     0, 0,
5133     sysctl_cpu_quiescent_counter_interval, "I",
5134     "Quiescent CPU checkin interval (microseconds)");
5135 
5136 /*
5137  * Allow the precise user/kernel time sysctl to be set, but don't allow it to
5138  * affect anything.  Some tools expect to be able to set this, even though
5139  * runtime configuration is no longer supported.
5140  */
5141 
5142 static int
5143 sysctl_precise_user_kernel_time SYSCTL_HANDLER_ARGS
5144 {
5145 #if PRECISE_USER_KERNEL_TIME
5146 	int dummy_set = 1;
5147 #else /* PRECISE_USER_KERNEL_TIME */
5148 	int dummy_set = 0;
5149 #endif /* !PRECISE_USER_KERNEL_TIME */
5150 	return sysctl_handle_int(oidp, &dummy_set, 0, req);
5151 }
5152 
5153 SYSCTL_PROC(_kern, OID_AUTO, precise_user_kernel_time,
5154     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
5155     0, 0, sysctl_precise_user_kernel_time, "I",
5156     "Precise accounting of kernel vs. user time (deprecated)");
5157 
5158 #if CONFIG_PERVASIVE_ENERGY && HAS_CPU_DPE_COUNTER
5159 __security_const_late static int pervasive_energy = 1;
5160 #else /* CONFIG_PERVASIVE_ENERGY && HAS_CPU_DPE_COUNTER */
5161 __security_const_late static int pervasive_energy = 0;
5162 #endif /* !CONFIG_PERVASIVE_ENERGY || !HAS_CPU_DPE_COUNTER */
5163 
5164 SYSCTL_INT(_kern, OID_AUTO, pervasive_energy,
5165     CTLFLAG_KERN | CTLFLAG_RD | CTLFLAG_LOCKED, &pervasive_energy, 0, "");
5166 
5167 /* Parameters related to timer coalescing tuning, to be replaced
5168  * with a dedicated systemcall in the future.
5169  */
5170 /* Enable processing pending timers in the context of any other interrupt
5171  * Coalescing tuning parameters for various thread/task attributes */
5172 STATIC int
5173 sysctl_timer_user_us_kernel_abstime SYSCTL_HANDLER_ARGS
5174 {
5175 #pragma unused(oidp)
5176 	int size = arg2;        /* subcommand*/
5177 	int error;
5178 	int changed = 0;
5179 	uint64_t old_value_ns;
5180 	uint64_t new_value_ns;
5181 	uint64_t value_abstime;
5182 	if (size == sizeof(uint32_t)) {
5183 		value_abstime = *((uint32_t *)arg1);
5184 	} else if (size == sizeof(uint64_t)) {
5185 		value_abstime = *((uint64_t *)arg1);
5186 	} else {
5187 		return ENOTSUP;
5188 	}
5189 
5190 	absolutetime_to_nanoseconds(value_abstime, &old_value_ns);
5191 	error = sysctl_io_number(req, old_value_ns, sizeof(old_value_ns), &new_value_ns, &changed);
5192 	if ((error) || (!changed)) {
5193 		return error;
5194 	}
5195 
5196 	nanoseconds_to_absolutetime(new_value_ns, &value_abstime);
5197 	if (size == sizeof(uint32_t)) {
5198 		*((uint32_t *)arg1) = (uint32_t)value_abstime;
5199 	} else {
5200 		*((uint64_t *)arg1) = value_abstime;
5201 	}
5202 	return error;
5203 }
5204 
5205 SYSCTL_INT(_kern, OID_AUTO, timer_coalesce_bg_scale,
5206     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
5207     &tcoal_prio_params.timer_coalesce_bg_shift, 0, "");
5208 SYSCTL_PROC(_kern, OID_AUTO, timer_resort_threshold_ns,
5209     CTLTYPE_QUAD | CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
5210     &tcoal_prio_params.timer_resort_threshold_abstime,
5211     sizeof(tcoal_prio_params.timer_resort_threshold_abstime),
5212     sysctl_timer_user_us_kernel_abstime,
5213     "Q", "");
5214 SYSCTL_PROC(_kern, OID_AUTO, timer_coalesce_bg_ns_max,
5215     CTLTYPE_QUAD | CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
5216     &tcoal_prio_params.timer_coalesce_bg_abstime_max,
5217     sizeof(tcoal_prio_params.timer_coalesce_bg_abstime_max),
5218     sysctl_timer_user_us_kernel_abstime,
5219     "Q", "");
5220 
5221 SYSCTL_INT(_kern, OID_AUTO, timer_coalesce_kt_scale,
5222     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
5223     &tcoal_prio_params.timer_coalesce_kt_shift, 0, "");
5224 
5225 SYSCTL_PROC(_kern, OID_AUTO, timer_coalesce_kt_ns_max,
5226     CTLTYPE_QUAD | CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
5227     &tcoal_prio_params.timer_coalesce_kt_abstime_max,
5228     sizeof(tcoal_prio_params.timer_coalesce_kt_abstime_max),
5229     sysctl_timer_user_us_kernel_abstime,
5230     "Q", "");
5231 
5232 SYSCTL_INT(_kern, OID_AUTO, timer_coalesce_fp_scale,
5233     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
5234     &tcoal_prio_params.timer_coalesce_fp_shift, 0, "");
5235 
5236 SYSCTL_PROC(_kern, OID_AUTO, timer_coalesce_fp_ns_max,
5237     CTLTYPE_QUAD | CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
5238     &tcoal_prio_params.timer_coalesce_fp_abstime_max,
5239     sizeof(tcoal_prio_params.timer_coalesce_fp_abstime_max),
5240     sysctl_timer_user_us_kernel_abstime,
5241     "Q", "");
5242 
5243 SYSCTL_INT(_kern, OID_AUTO, timer_coalesce_ts_scale,
5244     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
5245     &tcoal_prio_params.timer_coalesce_ts_shift, 0, "");
5246 
5247 SYSCTL_PROC(_kern, OID_AUTO, timer_coalesce_ts_ns_max,
5248     CTLTYPE_QUAD | CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
5249     &tcoal_prio_params.timer_coalesce_ts_abstime_max,
5250     sizeof(tcoal_prio_params.timer_coalesce_ts_abstime_max),
5251     sysctl_timer_user_us_kernel_abstime,
5252     "Q", "");
5253 
5254 SYSCTL_INT(_kern, OID_AUTO, timer_coalesce_tier0_scale,
5255     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
5256     &tcoal_prio_params.latency_qos_scale[0], 0, "");
5257 
5258 SYSCTL_PROC(_kern, OID_AUTO, timer_coalesce_tier0_ns_max,
5259     CTLTYPE_QUAD | CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
5260     &tcoal_prio_params.latency_qos_abstime_max[0],
5261     sizeof(tcoal_prio_params.latency_qos_abstime_max[0]),
5262     sysctl_timer_user_us_kernel_abstime,
5263     "Q", "");
5264 
5265 SYSCTL_INT(_kern, OID_AUTO, timer_coalesce_tier1_scale,
5266     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
5267     &tcoal_prio_params.latency_qos_scale[1], 0, "");
5268 
5269 SYSCTL_PROC(_kern, OID_AUTO, timer_coalesce_tier1_ns_max,
5270     CTLTYPE_QUAD | CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
5271     &tcoal_prio_params.latency_qos_abstime_max[1],
5272     sizeof(tcoal_prio_params.latency_qos_abstime_max[1]),
5273     sysctl_timer_user_us_kernel_abstime,
5274     "Q", "");
5275 
5276 SYSCTL_INT(_kern, OID_AUTO, timer_coalesce_tier2_scale,
5277     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
5278     &tcoal_prio_params.latency_qos_scale[2], 0, "");
5279 
5280 SYSCTL_PROC(_kern, OID_AUTO, timer_coalesce_tier2_ns_max,
5281     CTLTYPE_QUAD | CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
5282     &tcoal_prio_params.latency_qos_abstime_max[2],
5283     sizeof(tcoal_prio_params.latency_qos_abstime_max[2]),
5284     sysctl_timer_user_us_kernel_abstime,
5285     "Q", "");
5286 
5287 SYSCTL_INT(_kern, OID_AUTO, timer_coalesce_tier3_scale,
5288     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
5289     &tcoal_prio_params.latency_qos_scale[3], 0, "");
5290 
5291 SYSCTL_PROC(_kern, OID_AUTO, timer_coalesce_tier3_ns_max,
5292     CTLTYPE_QUAD | CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
5293     &tcoal_prio_params.latency_qos_abstime_max[3],
5294     sizeof(tcoal_prio_params.latency_qos_abstime_max[3]),
5295     sysctl_timer_user_us_kernel_abstime,
5296     "Q", "");
5297 
5298 SYSCTL_INT(_kern, OID_AUTO, timer_coalesce_tier4_scale,
5299     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
5300     &tcoal_prio_params.latency_qos_scale[4], 0, "");
5301 
5302 SYSCTL_PROC(_kern, OID_AUTO, timer_coalesce_tier4_ns_max,
5303     CTLTYPE_QUAD | CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
5304     &tcoal_prio_params.latency_qos_abstime_max[4],
5305     sizeof(tcoal_prio_params.latency_qos_abstime_max[4]),
5306     sysctl_timer_user_us_kernel_abstime,
5307     "Q", "");
5308 
5309 SYSCTL_INT(_kern, OID_AUTO, timer_coalesce_tier5_scale,
5310     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
5311     &tcoal_prio_params.latency_qos_scale[5], 0, "");
5312 
5313 SYSCTL_PROC(_kern, OID_AUTO, timer_coalesce_tier5_ns_max,
5314     CTLTYPE_QUAD | CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
5315     &tcoal_prio_params.latency_qos_abstime_max[5],
5316     sizeof(tcoal_prio_params.latency_qos_abstime_max[5]),
5317     sysctl_timer_user_us_kernel_abstime,
5318     "Q", "");
5319 
5320 /* Communicate the "user idle level" heuristic to the timer layer, and
5321  * potentially other layers in the future.
5322  */
5323 
5324 static int
5325 timer_user_idle_level(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
5326 {
5327 	int new_value = 0, old_value = 0, changed = 0, error;
5328 
5329 	old_value = timer_get_user_idle_level();
5330 
5331 	error = sysctl_io_number(req, old_value, sizeof(int), &new_value, &changed);
5332 
5333 	if (error == 0 && changed) {
5334 		if (timer_set_user_idle_level(new_value) != KERN_SUCCESS) {
5335 			error = ERANGE;
5336 		}
5337 	}
5338 
5339 	return error;
5340 }
5341 
5342 SYSCTL_PROC(_machdep, OID_AUTO, user_idle_level,
5343     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
5344     0, 0,
5345     timer_user_idle_level, "I", "User idle level heuristic, 0-128");
5346 
5347 #if DEVELOPMENT || DEBUG
5348 /*
5349  * Basic console mode for games; used for development purposes only.
5350  * Final implementation for this feature (with possible removal of
5351  * sysctl) tracked via rdar://101215873.
5352  */
5353 static int console_mode = 0;
5354 SYSCTL_INT(_kern, OID_AUTO, console_mode,
5355     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_MASKED,
5356     &console_mode, 0, "Game Console Mode");
5357 #endif /* DEVELOPMENT || DEBUG */
5358 
5359 
5360 #if HYPERVISOR
5361 SYSCTL_INT(_kern, OID_AUTO, hv_support,
5362     CTLFLAG_KERN | CTLFLAG_RD | CTLFLAG_LOCKED,
5363     &hv_support_available, 0, "");
5364 
5365 SYSCTL_INT(_kern, OID_AUTO, hv_disable,
5366     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
5367     &hv_disable, 0, "");
5368 
5369 #endif /* HYPERVISOR */
5370 
5371 #if DEVELOPMENT || DEBUG
5372 extern uint64_t driverkit_checkin_timed_out;
5373 SYSCTL_QUAD(_kern, OID_AUTO, driverkit_checkin_timed_out,
5374     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
5375     &driverkit_checkin_timed_out, "timestamp of dext checkin timeout");
5376 #endif
5377 
5378 #if CONFIG_DARKBOOT
5379 STATIC int
5380 sysctl_darkboot SYSCTL_HANDLER_ARGS
5381 {
5382 	int err = 0, value = 0;
5383 #pragma unused(oidp, arg1, arg2, err, value, req)
5384 
5385 	/*
5386 	 * Handle the sysctl request.
5387 	 *
5388 	 * If this is a read, the function will set the value to the current darkboot value. Otherwise,
5389 	 * we'll get the request identifier into "value" and then we can honor it.
5390 	 */
5391 	if ((err = sysctl_io_number(req, darkboot, sizeof(int), &value, NULL)) != 0) {
5392 		goto exit;
5393 	}
5394 
5395 	/* writing requested, let's process the request */
5396 	if (req->newptr) {
5397 		/* writing is protected by an entitlement */
5398 		if (priv_check_cred(kauth_cred_get(), PRIV_DARKBOOT, 0) != 0) {
5399 			err = EPERM;
5400 			goto exit;
5401 		}
5402 
5403 		switch (value) {
5404 		case MEMORY_MAINTENANCE_DARK_BOOT_UNSET:
5405 			/*
5406 			 * If the darkboot sysctl is unset, the NVRAM variable
5407 			 * must be unset too. If that's not the case, it means
5408 			 * someone is doing something crazy and not supported.
5409 			 */
5410 			if (darkboot != 0) {
5411 				int ret = PERemoveNVRAMProperty(MEMORY_MAINTENANCE_DARK_BOOT_NVRAM_NAME);
5412 				if (ret) {
5413 					darkboot = 0;
5414 				} else {
5415 					err = EINVAL;
5416 				}
5417 			}
5418 			break;
5419 		case MEMORY_MAINTENANCE_DARK_BOOT_SET:
5420 			darkboot = 1;
5421 			break;
5422 		case MEMORY_MAINTENANCE_DARK_BOOT_SET_PERSISTENT: {
5423 			/*
5424 			 * Set the NVRAM and update 'darkboot' in case
5425 			 * of success. Otherwise, do not update
5426 			 * 'darkboot' and report the failure.
5427 			 */
5428 			if (PEWriteNVRAMBooleanProperty(MEMORY_MAINTENANCE_DARK_BOOT_NVRAM_NAME, TRUE)) {
5429 				darkboot = 1;
5430 			} else {
5431 				err = EINVAL;
5432 			}
5433 
5434 			break;
5435 		}
5436 		default:
5437 			err = EINVAL;
5438 		}
5439 	}
5440 
5441 exit:
5442 	return err;
5443 }
5444 
5445 SYSCTL_PROC(_kern, OID_AUTO, darkboot,
5446     CTLFLAG_KERN | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_ANYBODY,
5447     0, 0, sysctl_darkboot, "I", "");
5448 #endif /* CONFIG_DARKBOOT */
5449 
5450 #if DEVELOPMENT || DEBUG
5451 #include <sys/sysent.h>
5452 /* This should result in a fatal exception, verifying that "sysent" is
5453  * write-protected.
5454  */
5455 static int
5456 kern_sysent_write(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
5457 {
5458 	uint64_t new_value = 0, old_value = 0;
5459 	int changed = 0, error;
5460 
5461 	error = sysctl_io_number(req, old_value, sizeof(uint64_t), &new_value, &changed);
5462 	if ((error == 0) && changed) {
5463 		volatile uint32_t *wraddr = __DECONST(uint32_t *, &sysent[0]);
5464 		*wraddr = 0;
5465 		printf("sysent[0] write succeeded\n");
5466 	}
5467 	return error;
5468 }
5469 
5470 SYSCTL_PROC(_kern, OID_AUTO, sysent_const_check,
5471     CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
5472     0, 0,
5473     kern_sysent_write, "I", "Attempt sysent[0] write");
5474 
5475 #endif
5476 
5477 #if DEVELOPMENT || DEBUG
5478 SYSCTL_COMPAT_INT(_kern, OID_AUTO, development, CTLFLAG_RD | CTLFLAG_MASKED | CTLFLAG_KERN, NULL, 1, "");
5479 #else
5480 SYSCTL_COMPAT_INT(_kern, OID_AUTO, development, CTLFLAG_RD | CTLFLAG_MASKED, NULL, 0, "");
5481 #endif
5482 
5483 SYSCTL_INT(_kern, OID_AUTO, serverperfmode, CTLFLAG_RD, &serverperfmode, 0, "");
5484 
5485 #if DEVELOPMENT || DEBUG
5486 
5487 decl_lck_spin_data(, spinlock_panic_test_lock);
5488 
5489 __attribute__((noreturn))
5490 static void
5491 spinlock_panic_test_acquire_spinlock(void * arg __unused, wait_result_t wres __unused)
5492 {
5493 	lck_spin_lock(&spinlock_panic_test_lock);
5494 	while (1) {
5495 		;
5496 	}
5497 }
5498 
5499 static int
5500 sysctl_spinlock_panic_test SYSCTL_HANDLER_ARGS
5501 {
5502 #pragma unused(oidp, arg1, arg2)
5503 	if (req->newlen == 0) {
5504 		return EINVAL;
5505 	}
5506 
5507 	thread_t panic_spinlock_thread;
5508 	/* Initialize panic spinlock */
5509 	lck_grp_t * panic_spinlock_grp;
5510 	lck_grp_attr_t * panic_spinlock_grp_attr;
5511 	lck_attr_t * panic_spinlock_attr;
5512 
5513 	panic_spinlock_grp_attr = lck_grp_attr_alloc_init();
5514 	panic_spinlock_grp = lck_grp_alloc_init("panic_spinlock", panic_spinlock_grp_attr);
5515 	panic_spinlock_attr = lck_attr_alloc_init();
5516 
5517 	lck_spin_init(&spinlock_panic_test_lock, panic_spinlock_grp, panic_spinlock_attr);
5518 
5519 
5520 	/* Create thread to acquire spinlock */
5521 	if (kernel_thread_start(spinlock_panic_test_acquire_spinlock, NULL, &panic_spinlock_thread) != KERN_SUCCESS) {
5522 		return EBUSY;
5523 	}
5524 
5525 	/* Try to acquire spinlock -- should panic eventually */
5526 	lck_spin_lock(&spinlock_panic_test_lock);
5527 	while (1) {
5528 		;
5529 	}
5530 }
5531 
5532 __attribute__((noreturn))
5533 static void
5534 simultaneous_panic_worker
5535 (void * arg, wait_result_t wres __unused)
5536 {
5537 	atomic_int *start_panic = (atomic_int *)arg;
5538 
5539 	while (!atomic_load(start_panic)) {
5540 		;
5541 	}
5542 	panic("SIMULTANEOUS PANIC TEST: INITIATING PANIC FROM CPU %d", cpu_number());
5543 	__builtin_unreachable();
5544 }
5545 
5546 static int
5547 sysctl_simultaneous_panic_test SYSCTL_HANDLER_ARGS
5548 {
5549 #pragma unused(oidp, arg1, arg2)
5550 	if (req->newlen == 0) {
5551 		return EINVAL;
5552 	}
5553 
5554 	int i = 0, threads_to_create = 2 * processor_count;
5555 	atomic_int start_panic = 0;
5556 	unsigned int threads_created = 0;
5557 	thread_t new_panic_thread;
5558 
5559 	for (i = threads_to_create; i > 0; i--) {
5560 		if (kernel_thread_start(simultaneous_panic_worker, (void *) &start_panic, &new_panic_thread) == KERN_SUCCESS) {
5561 			threads_created++;
5562 		}
5563 	}
5564 
5565 	/* FAIL if we couldn't create at least processor_count threads */
5566 	if (threads_created < processor_count) {
5567 		panic("SIMULTANEOUS PANIC TEST: FAILED TO CREATE ENOUGH THREADS, ONLY CREATED %d (of %d)",
5568 		    threads_created, threads_to_create);
5569 	}
5570 
5571 	atomic_exchange(&start_panic, 1);
5572 	while (1) {
5573 		;
5574 	}
5575 }
5576 
5577 extern unsigned int panic_test_failure_mode;
5578 SYSCTL_INT(_debug, OID_AUTO, xnu_panic_failure_mode, CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_KERN, &panic_test_failure_mode, 0, "panic/debugger test failure mode");
5579 
5580 extern unsigned int panic_test_action_count;
5581 SYSCTL_INT(_debug, OID_AUTO, xnu_panic_action_count, CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_KERN, &panic_test_action_count, 0, "panic/debugger test action count");
5582 
5583 extern unsigned int panic_test_case;
5584 SYSCTL_INT(_debug, OID_AUTO, xnu_panic_test_case, CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_KERN, &panic_test_case, 0, "panic/debugger testcase");
5585 
5586 SYSCTL_PROC(_debug, OID_AUTO, xnu_spinlock_panic_test, CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_MASKED, 0, 0, sysctl_spinlock_panic_test, "A", "spinlock panic test");
5587 SYSCTL_PROC(_debug, OID_AUTO, xnu_simultaneous_panic_test, CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_MASKED, 0, 0, sysctl_simultaneous_panic_test, "A", "simultaneous panic test");
5588 
5589 extern int exc_resource_threads_enabled;
5590 SYSCTL_INT(_kern, OID_AUTO, exc_resource_threads_enabled, CTLFLAG_RW | CTLFLAG_LOCKED, &exc_resource_threads_enabled, 0, "exc_resource thread limit enabled");
5591 
5592 extern unsigned int verbose_panic_flow_logging;
5593 SYSCTL_INT(_debug, OID_AUTO, verbose_panic_flow_logging, CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_KERN, &verbose_panic_flow_logging, 0, "verbose logging during panic");
5594 
5595 #endif /* DEVELOPMENT || DEBUG */
5596 
5597 #if BUILT_LTO
5598 static int _built_lto = 1;
5599 #else // BUILT_LTO
5600 static int _built_lto = 0;
5601 #endif // !BUILT_LTO
5602 
5603 SYSCTL_INT(_kern, OID_AUTO, link_time_optimized, CTLFLAG_RD | CTLFLAG_LOCKED | CTLFLAG_KERN, &_built_lto, 0, "Whether the kernel was built with Link Time Optimization enabled");
5604 
5605 #if CONFIG_THREAD_GROUPS
5606 #if DEVELOPMENT || DEBUG
5607 
5608 static int
5609 sysctl_get_thread_group_id SYSCTL_HANDLER_ARGS
5610 {
5611 #pragma unused(arg1, arg2, oidp)
5612 	uint64_t thread_group_id = thread_group_get_id(thread_group_get(current_thread()));
5613 	return SYSCTL_OUT(req, &thread_group_id, sizeof(thread_group_id));
5614 }
5615 
5616 SYSCTL_PROC(_kern, OID_AUTO, thread_group_id, CTLFLAG_RD | CTLFLAG_LOCKED | CTLTYPE_QUAD,
5617     0, 0, &sysctl_get_thread_group_id, "I", "thread group id of the thread");
5618 
5619 extern kern_return_t sysctl_clutch_thread_group_cpu_time_for_thread(thread_t thread, int sched_bucket, uint64_t *cpu_stats);
5620 
5621 static int
5622 sysctl_get_clutch_bucket_group_cpu_stats SYSCTL_HANDLER_ARGS
5623 {
5624 	int error;
5625 	kern_return_t kr;
5626 	int sched_bucket = -1;
5627 	error = SYSCTL_IN(req, &sched_bucket, sizeof(sched_bucket));
5628 	if (error) {
5629 		return error;
5630 	}
5631 	uint64_t cpu_stats[2];
5632 	kr = sysctl_clutch_thread_group_cpu_time_for_thread(current_thread(), sched_bucket, cpu_stats);
5633 	error = mach_to_bsd_errno(kr);
5634 	if (error) {
5635 		return error;
5636 	}
5637 	return SYSCTL_OUT(req, cpu_stats, sizeof(cpu_stats));
5638 }
5639 
5640 SYSCTL_PROC(_kern, OID_AUTO, clutch_bucket_group_cpu_stats, CTLFLAG_RW | CTLFLAG_LOCKED | CTLTYPE_OPAQUE,
5641     0, 0, &sysctl_get_clutch_bucket_group_cpu_stats, "I",
5642     "CPU used and blocked time for the current thread group at a specified scheduling bucket");
5643 
5644 STATIC int
5645 sysctl_thread_group_count(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
5646 {
5647 	int value = thread_group_count();
5648 	return sysctl_io_number(req, value, sizeof(value), NULL, NULL);
5649 }
5650 
5651 SYSCTL_PROC(_kern, OID_AUTO, thread_group_count, CTLFLAG_RD | CTLFLAG_LOCKED | CTLFLAG_KERN,
5652     0, 0, &sysctl_thread_group_count, "I", "count of thread groups");
5653 
5654 #endif /* DEVELOPMENT || DEBUG */
5655 const uint32_t thread_groups_supported = 1;
5656 #else /* CONFIG_THREAD_GROUPS */
5657 const uint32_t thread_groups_supported = 0;
5658 #endif /* CONFIG_THREAD_GROUPS */
5659 
5660 STATIC int
5661 sysctl_thread_groups_supported(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
5662 {
5663 	int value = thread_groups_supported;
5664 	return sysctl_io_number(req, value, sizeof(value), NULL, NULL);
5665 }
5666 
5667 SYSCTL_PROC(_kern, OID_AUTO, thread_groups_supported, CTLFLAG_RD | CTLFLAG_LOCKED | CTLFLAG_KERN,
5668     0, 0, &sysctl_thread_groups_supported, "I", "thread groups supported");
5669 
5670 static int
5671 sysctl_grade_cputype SYSCTL_HANDLER_ARGS
5672 {
5673 #pragma unused(arg1, arg2, oidp)
5674 	int error = 0;
5675 	int type_tuple[2] = {};
5676 	int return_value = 0;
5677 
5678 	error = SYSCTL_IN(req, &type_tuple, sizeof(type_tuple));
5679 
5680 	if (error) {
5681 		return error;
5682 	}
5683 
5684 	return_value = grade_binary(type_tuple[0], type_tuple[1] & ~CPU_SUBTYPE_MASK, type_tuple[1] & CPU_SUBTYPE_MASK, FALSE);
5685 
5686 	error = SYSCTL_OUT(req, &return_value, sizeof(return_value));
5687 
5688 	if (error) {
5689 		return error;
5690 	}
5691 
5692 	return error;
5693 }
5694 
5695 SYSCTL_PROC(_kern, OID_AUTO, grade_cputype,
5696     CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MASKED | CTLFLAG_LOCKED | CTLTYPE_OPAQUE,
5697     0, 0, &sysctl_grade_cputype, "S",
5698     "grade value of cpu_type_t+cpu_sub_type_t");
5699 
5700 
5701 #if DEVELOPMENT || DEBUG
5702 STATIC int
5703 sysctl_binary_grade_override(  __unused struct sysctl_oid *oidp, __unused void *arg1,
5704     __unused int arg2, struct sysctl_req *req)
5705 {
5706 	int error;
5707 	user_addr_t oldp = 0, newp = 0;
5708 	size_t *oldlenp = NULL;
5709 	size_t newlen = 0;
5710 
5711 	oldp = req->oldptr;
5712 	oldlenp = &(req->oldlen);
5713 	newp = req->newptr;
5714 	newlen = req->newlen;
5715 
5716 	/* We want the current length, and maybe the string itself */
5717 	if (oldlenp) {
5718 		char existing_overrides[256] = { 0 };
5719 
5720 		size_t currlen = bingrade_get_override_string(existing_overrides, sizeof(existing_overrides));
5721 
5722 		if (oldp && currlen > 0) {
5723 			if (*oldlenp < currlen) {
5724 				return ENOMEM;
5725 			}
5726 			/* NOTE - we do not copy the NULL terminator */
5727 			error = copyout(existing_overrides, oldp, currlen);
5728 			if (error) {
5729 				return error;
5730 			}
5731 		}
5732 		/* return length of overrides minus the NULL terminator (just like strlen)  */
5733 		req->oldidx = currlen;
5734 	}
5735 
5736 	/* We want to set the override string to something */
5737 	if (newp) {
5738 		char *tmp_override = (char *)kalloc_data(newlen + 1, Z_WAITOK | Z_ZERO);
5739 		if (!tmp_override) {
5740 			return ENOMEM;
5741 		}
5742 
5743 		error = copyin(newp, tmp_override, newlen);
5744 		if (error) {
5745 			kfree_data(tmp_override, newlen + 1);
5746 			return error;
5747 		}
5748 
5749 		tmp_override[newlen] = 0;       /* Terminate string */
5750 
5751 		/* Set the binary grading overrides */
5752 		if (binary_grade_overrides_update(tmp_override) == 0) {
5753 			/* Nothing got set. */
5754 			kfree_data(tmp_override, newlen + 1);
5755 			return EINVAL;
5756 		}
5757 
5758 		kfree_data(tmp_override, newlen + 1);
5759 	}
5760 
5761 	return 0;
5762 }
5763 
5764 
5765 SYSCTL_PROC(_kern, OID_AUTO, grade_override,
5766     CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_LOCKED,
5767     0, 0, &sysctl_binary_grade_override, "A",
5768     "");
5769 #endif /* DEVELOPMENT || DEBUG */
5770 
5771 extern boolean_t allow_direct_handoff;
5772 SYSCTL_INT(_kern, OID_AUTO, direct_handoff,
5773     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
5774     &allow_direct_handoff, 0, "Enable direct handoff for realtime threads");
5775 
5776 #if DEVELOPMENT || DEBUG
5777 
5778 SYSCTL_QUAD(_kern, OID_AUTO, phys_carveout_pa, CTLFLAG_RD | CTLFLAG_LOCKED | CTLFLAG_KERN,
5779     &phys_carveout_pa,
5780     "base physical address of the phys_carveout_mb boot-arg region");
5781 SYSCTL_QUAD(_kern, OID_AUTO, phys_carveout_va, CTLFLAG_RD | CTLFLAG_LOCKED | CTLFLAG_KERN,
5782     &phys_carveout,
5783     "base virtual address of the phys_carveout_mb boot-arg region");
5784 SYSCTL_QUAD(_kern, OID_AUTO, phys_carveout_size, CTLFLAG_RD | CTLFLAG_LOCKED | CTLFLAG_KERN,
5785     &phys_carveout_size,
5786     "size in bytes of the phys_carveout_mb boot-arg region");
5787 
5788 
5789 
5790 static int
5791 cseg_wedge_thread SYSCTL_HANDLER_ARGS
5792 {
5793 #pragma unused(arg1, arg2)
5794 
5795 	int error, val = 0;
5796 	error = sysctl_handle_int(oidp, &val, 0, req);
5797 	if (error || val == 0) {
5798 		return error;
5799 	}
5800 
5801 	do_cseg_wedge_thread();
5802 	return 0;
5803 }
5804 SYSCTL_PROC(_kern, OID_AUTO, cseg_wedge_thread, CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_MASKED, 0, 0, cseg_wedge_thread, "I", "wedge c_seg thread");
5805 
5806 static int
5807 cseg_unwedge_thread SYSCTL_HANDLER_ARGS
5808 {
5809 #pragma unused(arg1, arg2)
5810 
5811 	int error, val = 0;
5812 	error = sysctl_handle_int(oidp, &val, 0, req);
5813 	if (error || val == 0) {
5814 		return error;
5815 	}
5816 
5817 	do_cseg_unwedge_thread();
5818 	return 0;
5819 }
5820 SYSCTL_PROC(_kern, OID_AUTO, cseg_unwedge_thread, CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_MASKED, 0, 0, cseg_unwedge_thread, "I", "unstuck c_seg thread");
5821 
5822 static atomic_int wedge_thread_should_wake = 0;
5823 
5824 static int
5825 unwedge_thread SYSCTL_HANDLER_ARGS
5826 {
5827 #pragma unused(arg1, arg2)
5828 	int error, val = 0;
5829 	error = sysctl_handle_int(oidp, &val, 0, req);
5830 	if (error || val == 0) {
5831 		return error;
5832 	}
5833 
5834 	atomic_store(&wedge_thread_should_wake, 1);
5835 	return 0;
5836 }
5837 
5838 SYSCTL_PROC(_kern, OID_AUTO, unwedge_thread, CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_LOCKED, 0, 0, unwedge_thread, "I", "unwedge the thread wedged by kern.wedge_thread");
5839 
5840 static int
5841 wedge_thread SYSCTL_HANDLER_ARGS
5842 {
5843 #pragma unused(arg1, arg2)
5844 
5845 	int error, val = 0;
5846 	error = sysctl_handle_int(oidp, &val, 0, req);
5847 	if (error || val == 0) {
5848 		return error;
5849 	}
5850 
5851 	uint64_t interval = 1;
5852 	nanoseconds_to_absolutetime(1000 * 1000 * 50, &interval);
5853 
5854 	atomic_store(&wedge_thread_should_wake, 0);
5855 	while (!atomic_load(&wedge_thread_should_wake)) {
5856 		tsleep1(NULL, 0, "wedge_thread", mach_absolute_time() + interval, NULL);
5857 	}
5858 
5859 	return 0;
5860 }
5861 
5862 SYSCTL_PROC(_kern, OID_AUTO, wedge_thread,
5863     CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_LOCKED, 0, 0, wedge_thread, "I",
5864     "wedge this thread so it cannot be cleaned up");
5865 
5866 static int
5867 sysctl_total_corpses_count SYSCTL_HANDLER_ARGS
5868 {
5869 #pragma unused(oidp, arg1, arg2)
5870 	extern unsigned long total_corpses_count(void);
5871 
5872 	unsigned long corpse_count_long = total_corpses_count();
5873 	unsigned int corpse_count = (unsigned int)MIN(corpse_count_long, UINT_MAX);
5874 	return sysctl_io_opaque(req, &corpse_count, sizeof(corpse_count), NULL);
5875 }
5876 
5877 SYSCTL_PROC(_kern, OID_AUTO, total_corpses_count,
5878     CTLFLAG_RD | CTLFLAG_ANYBODY | CTLFLAG_LOCKED, 0, 0,
5879     sysctl_total_corpses_count, "I", "total corpses on the system");
5880 
5881 static int
5882 sysctl_turnstile_test_prim_lock SYSCTL_HANDLER_ARGS;
5883 static int
5884 sysctl_turnstile_test_prim_unlock SYSCTL_HANDLER_ARGS;
5885 int
5886 tstile_test_prim_lock(boolean_t use_hashtable);
5887 int
5888 tstile_test_prim_unlock(boolean_t use_hashtable);
5889 
5890 static int
5891 sysctl_turnstile_test_prim_lock SYSCTL_HANDLER_ARGS
5892 {
5893 #pragma unused(arg1, arg2)
5894 	int error, val = 0;
5895 	error = sysctl_handle_int(oidp, &val, 0, req);
5896 	if (error || val == 0) {
5897 		return error;
5898 	}
5899 	switch (val) {
5900 	case SYSCTL_TURNSTILE_TEST_USER_DEFAULT:
5901 	case SYSCTL_TURNSTILE_TEST_USER_HASHTABLE:
5902 	case SYSCTL_TURNSTILE_TEST_KERNEL_DEFAULT:
5903 	case SYSCTL_TURNSTILE_TEST_KERNEL_HASHTABLE:
5904 		return tstile_test_prim_lock(val);
5905 	default:
5906 		return error;
5907 	}
5908 }
5909 
5910 static int
5911 sysctl_turnstile_test_prim_unlock SYSCTL_HANDLER_ARGS
5912 {
5913 #pragma unused(arg1, arg2)
5914 	int error, val = 0;
5915 	error = sysctl_handle_int(oidp, &val, 0, req);
5916 	if (error || val == 0) {
5917 		return error;
5918 	}
5919 	switch (val) {
5920 	case SYSCTL_TURNSTILE_TEST_USER_DEFAULT:
5921 	case SYSCTL_TURNSTILE_TEST_USER_HASHTABLE:
5922 	case SYSCTL_TURNSTILE_TEST_KERNEL_DEFAULT:
5923 	case SYSCTL_TURNSTILE_TEST_KERNEL_HASHTABLE:
5924 		return tstile_test_prim_unlock(val);
5925 	default:
5926 		return error;
5927 	}
5928 }
5929 
5930 SYSCTL_PROC(_kern, OID_AUTO, turnstiles_test_lock, CTLFLAG_WR | CTLFLAG_ANYBODY | CTLFLAG_KERN | CTLFLAG_LOCKED,
5931     0, 0, sysctl_turnstile_test_prim_lock, "I", "turnstiles test lock");
5932 
5933 SYSCTL_PROC(_kern, OID_AUTO, turnstiles_test_unlock, CTLFLAG_WR | CTLFLAG_ANYBODY | CTLFLAG_KERN | CTLFLAG_LOCKED,
5934     0, 0, sysctl_turnstile_test_prim_unlock, "I", "turnstiles test unlock");
5935 
5936 int
5937 turnstile_get_boost_stats_sysctl(void *req);
5938 int
5939 turnstile_get_unboost_stats_sysctl(void *req);
5940 static int
5941 sysctl_turnstile_boost_stats SYSCTL_HANDLER_ARGS;
5942 static int
5943 sysctl_turnstile_unboost_stats SYSCTL_HANDLER_ARGS;
5944 extern uint64_t thread_block_on_turnstile_count;
5945 extern uint64_t thread_block_on_regular_waitq_count;
5946 
5947 static int
5948 sysctl_turnstile_boost_stats SYSCTL_HANDLER_ARGS
5949 {
5950 #pragma unused(arg1, arg2, oidp)
5951 	return turnstile_get_boost_stats_sysctl(req);
5952 }
5953 
5954 static int
5955 sysctl_turnstile_unboost_stats SYSCTL_HANDLER_ARGS
5956 {
5957 #pragma unused(arg1, arg2, oidp)
5958 	return turnstile_get_unboost_stats_sysctl(req);
5959 }
5960 
5961 SYSCTL_PROC(_kern, OID_AUTO, turnstile_boost_stats, CTLFLAG_RD | CTLFLAG_ANYBODY | CTLFLAG_KERN | CTLFLAG_LOCKED | CTLTYPE_STRUCT,
5962     0, 0, sysctl_turnstile_boost_stats, "S", "turnstiles boost stats");
5963 SYSCTL_PROC(_kern, OID_AUTO, turnstile_unboost_stats, CTLFLAG_RD | CTLFLAG_ANYBODY | CTLFLAG_KERN | CTLFLAG_LOCKED | CTLTYPE_STRUCT,
5964     0, 0, sysctl_turnstile_unboost_stats, "S", "turnstiles unboost stats");
5965 SYSCTL_QUAD(_kern, OID_AUTO, thread_block_count_on_turnstile,
5966     CTLFLAG_RD | CTLFLAG_ANYBODY | CTLFLAG_KERN | CTLFLAG_LOCKED,
5967     &thread_block_on_turnstile_count, "thread blocked on turnstile count");
5968 SYSCTL_QUAD(_kern, OID_AUTO, thread_block_count_on_reg_waitq,
5969     CTLFLAG_RD | CTLFLAG_ANYBODY | CTLFLAG_KERN | CTLFLAG_LOCKED,
5970     &thread_block_on_regular_waitq_count, "thread blocked on regular waitq count");
5971 
5972 #if CONFIG_PV_TICKET
5973 
5974 extern int ticket_lock_spins;
5975 SYSCTL_INT(_kern, OID_AUTO, ticket_lock_spins,
5976     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
5977     &ticket_lock_spins, 0, "loops before hypercall");
5978 
5979 #if (DEBUG || DEVELOPMENT)
5980 
5981 /* PV ticket lock stats */
5982 
5983 SYSCTL_SCALABLE_COUNTER(_kern, ticket_lock_kicks, ticket_kick_count,
5984     "ticket lock kicks");
5985 SYSCTL_SCALABLE_COUNTER(_kern, ticket_lock_waits, ticket_wait_count,
5986     "ticket lock waits");
5987 SYSCTL_SCALABLE_COUNTER(_kern, ticket_lock_already, ticket_already_count,
5988     "ticket lock already unlocked");
5989 SYSCTL_SCALABLE_COUNTER(_kern, ticket_lock_just_unlock, ticket_just_unlock,
5990     "ticket unlock without kick");
5991 SYSCTL_SCALABLE_COUNTER(_kern, ticket_lock_wflag_cleared, ticket_wflag_cleared,
5992     "ticket lock wait flag cleared");
5993 SYSCTL_SCALABLE_COUNTER(_kern, ticket_lock_wflag_still, ticket_wflag_still,
5994     "ticket lock wait flag not cleared");
5995 SYSCTL_SCALABLE_COUNTER(_kern, ticket_lock_spin_count, ticket_spin_count,
5996     "ticket lock spin count");
5997 
5998 /* sysctl kern.hcall_probe=n -- does hypercall #n exist? */
5999 
6000 static int
6001 sysctl_hcall_probe SYSCTL_HANDLER_ARGS
6002 {
6003 	char instr[20];
6004 
6005 	if (!req->newptr) {
6006 		return 0;
6007 	}
6008 	if (req->newlen >= sizeof(instr)) {
6009 		return EOVERFLOW;
6010 	}
6011 
6012 	int error = SYSCTL_IN(req, instr, req->newlen);
6013 	if (error) {
6014 		return error;
6015 	}
6016 	instr[req->newlen] = '\0';
6017 
6018 	int hcall = 0;
6019 	error = sscanf(instr, "%d", &hcall);
6020 	if (error != 1 || hcall < 0) {
6021 		return EINVAL;
6022 	}
6023 	uprintf("%savailable\n",
6024 	    hvg_is_hcall_available((hvg_hcall_code_t)hcall) ? "" : "not ");
6025 	return 0;
6026 }
6027 
6028 SYSCTL_PROC(_kern, OID_AUTO, hcall_probe,
6029     CTLTYPE_STRING | CTLFLAG_WR | CTLFLAG_LOCKED | CTLFLAG_MASKED,
6030     0, 0, sysctl_hcall_probe, "A", "probe hypercall by id");
6031 
6032 #endif /* (DEBUG || DEVELOPMENT) */
6033 #endif /* CONFIG_PV_TICKET */
6034 
6035 #if defined(__x86_64__)
6036 extern uint64_t MutexSpin;
6037 
6038 SYSCTL_QUAD(_kern, OID_AUTO, mutex_spin_abs, CTLFLAG_RW, &MutexSpin,
6039     "Spin time in abs for acquiring a kernel mutex");
6040 #else
6041 extern machine_timeout_t MutexSpin;
6042 
6043 SYSCTL_QUAD(_kern, OID_AUTO, mutex_spin_abs, CTLFLAG_RW, &MutexSpin,
6044     "Spin time in abs for acquiring a kernel mutex");
6045 #endif
6046 
6047 extern uint64_t low_MutexSpin;
6048 extern int64_t high_MutexSpin;
6049 extern unsigned int real_ncpus;
6050 
6051 SYSCTL_QUAD(_kern, OID_AUTO, low_mutex_spin_abs, CTLFLAG_RW, &low_MutexSpin,
6052     "Low spin threshold in abs for acquiring a kernel mutex");
6053 
6054 static int
6055 sysctl_high_mutex_spin_ns SYSCTL_HANDLER_ARGS
6056 {
6057 #pragma unused(oidp, arg1, arg2)
6058 	int error;
6059 	int64_t val = 0;
6060 	int64_t res;
6061 
6062 	/* Check if the user is writing to high_MutexSpin, or just reading it */
6063 	if (req->newptr) {
6064 		error = SYSCTL_IN(req, &val, sizeof(val));
6065 		if (error || (val < 0 && val != -1)) {
6066 			return error;
6067 		}
6068 		high_MutexSpin = val;
6069 	}
6070 
6071 	if (high_MutexSpin >= 0) {
6072 		res = high_MutexSpin;
6073 	} else {
6074 		res = low_MutexSpin * real_ncpus;
6075 	}
6076 	return SYSCTL_OUT(req, &res, sizeof(res));
6077 }
6078 SYSCTL_PROC(_kern, OID_AUTO, high_mutex_spin_abs, CTLFLAG_RW | CTLTYPE_QUAD, 0, 0, sysctl_high_mutex_spin_ns, "I",
6079     "High spin threshold in abs for acquiring a kernel mutex");
6080 
6081 #if defined (__x86_64__)
6082 
6083 semaphore_t sysctl_test_panic_with_thread_sem;
6084 
6085 #pragma clang diagnostic push
6086 #pragma clang diagnostic ignored "-Winfinite-recursion" /* rdar://38801963 */
6087 __attribute__((noreturn))
6088 static void
6089 panic_thread_test_child_spin(void * arg, wait_result_t wres)
6090 {
6091 	static int panic_thread_recurse_count = 5;
6092 
6093 	if (panic_thread_recurse_count > 0) {
6094 		panic_thread_recurse_count--;
6095 		panic_thread_test_child_spin(arg, wres);
6096 	}
6097 
6098 	semaphore_signal(sysctl_test_panic_with_thread_sem);
6099 	while (1) {
6100 		;
6101 	}
6102 }
6103 #pragma clang diagnostic pop
6104 
6105 static void
6106 panic_thread_test_child_park(void * arg __unused, wait_result_t wres __unused)
6107 {
6108 	int event;
6109 
6110 	assert_wait(&event, THREAD_UNINT);
6111 	semaphore_signal(sysctl_test_panic_with_thread_sem);
6112 	thread_block(panic_thread_test_child_park);
6113 }
6114 
6115 static int
6116 sysctl_test_panic_with_thread SYSCTL_HANDLER_ARGS
6117 {
6118 #pragma unused(arg1, arg2)
6119 	int rval = 0;
6120 	char str[16] = { '\0' };
6121 	thread_t child_thread = THREAD_NULL;
6122 
6123 	rval = sysctl_handle_string(oidp, str, sizeof(str), req);
6124 	if (rval != 0 || !req->newptr) {
6125 		return EINVAL;
6126 	}
6127 
6128 	semaphore_create(kernel_task, &sysctl_test_panic_with_thread_sem, SYNC_POLICY_FIFO, 0);
6129 
6130 	/* Create thread to spin or park in continuation */
6131 	if (strncmp("spin", str, strlen("spin")) == 0) {
6132 		if (kernel_thread_start(panic_thread_test_child_spin, NULL, &child_thread) != KERN_SUCCESS) {
6133 			semaphore_destroy(kernel_task, sysctl_test_panic_with_thread_sem);
6134 			return EBUSY;
6135 		}
6136 	} else if (strncmp("continuation", str, strlen("continuation")) == 0) {
6137 		if (kernel_thread_start(panic_thread_test_child_park, NULL, &child_thread) != KERN_SUCCESS) {
6138 			semaphore_destroy(kernel_task, sysctl_test_panic_with_thread_sem);
6139 			return EBUSY;
6140 		}
6141 	} else {
6142 		semaphore_destroy(kernel_task, sysctl_test_panic_with_thread_sem);
6143 		return EINVAL;
6144 	}
6145 
6146 	semaphore_wait(sysctl_test_panic_with_thread_sem);
6147 
6148 	panic_with_thread_context(0, NULL, 0, child_thread, "testing panic_with_thread_context for thread %p", child_thread);
6149 
6150 	/* Not reached */
6151 	return EINVAL;
6152 }
6153 
6154 SYSCTL_PROC(_kern, OID_AUTO, test_panic_with_thread,
6155     CTLFLAG_MASKED | CTLFLAG_KERN | CTLFLAG_LOCKED | CTLFLAG_WR | CTLTYPE_STRING,
6156     0, 0, sysctl_test_panic_with_thread, "A", "test panic flow for backtracing a different thread");
6157 #endif /* defined (__x86_64__) */
6158 
6159 static int
6160 sysctl_generate_file_permissions_guard_exception SYSCTL_HANDLER_ARGS
6161 {
6162 #pragma unused(arg1, arg2)
6163 	int error, val = 0;
6164 	error = sysctl_handle_int(oidp, &val, 0, req);
6165 	if (error || val == 0) {
6166 		return error;
6167 	}
6168 	generate_file_permissions_guard_exception(0, val);
6169 	return 0;
6170 }
6171 
6172 SYSCTL_PROC(_kern, OID_AUTO, file_perm_guard_exception, CTLFLAG_WR | CTLFLAG_ANYBODY | CTLFLAG_KERN | CTLFLAG_LOCKED,
6173     0, 0, sysctl_generate_file_permissions_guard_exception, "I", "Test File Permission Guard exception");
6174 
6175 #endif /* DEVELOPMENT || DEBUG */
6176 
6177 extern const int copysize_limit_panic;
6178 static int
6179 sysctl_get_owned_vmobjects SYSCTL_HANDLER_ARGS
6180 {
6181 #pragma unused(oidp, arg1, arg2)
6182 
6183 	/* validate */
6184 	if (req->newlen != sizeof(mach_port_name_t) || req->newptr == USER_ADDR_NULL ||
6185     req->oldidx != 0 || req->newidx != 0 || req->p == NULL ||
6186     (req->oldlen == 0 && req->oldptr != USER_ADDR_NULL)) {
6187 		return EINVAL;
6188 	}
6189 
6190 	int error;
6191 	mach_port_name_t task_port_name;
6192 	task_t task;
6193 	size_t buffer_size = (req->oldptr != USER_ADDR_NULL) ? req->oldlen : 0;
6194 	vmobject_list_output_t buffer = NULL;
6195 	size_t output_size;
6196 	size_t entries;
6197 	bool free_buffer = false;
6198 
6199 	/* we have a "newptr" (for write) we get a task port name from the caller. */
6200 	error = SYSCTL_IN(req, &task_port_name, sizeof(mach_port_name_t));
6201 
6202 	if (error != 0) {
6203 		goto sysctl_get_vmobject_list_exit;
6204 	}
6205 
6206 	task = port_name_to_task_read(task_port_name);
6207 	if (task == TASK_NULL) {
6208 		error = ESRCH;
6209 		goto sysctl_get_vmobject_list_exit;
6210 	}
6211 
6212 	bool corpse = task_is_a_corpse(task);
6213 
6214 	/* get the current size */
6215 	size_t max_size;
6216 	task_get_owned_vmobjects(task, 0, NULL, &max_size, &entries);
6217 
6218 	if (buffer_size && (buffer_size < sizeof(*buffer) + sizeof(vm_object_query_data_t))) {
6219 		error = ENOMEM;
6220 		goto sysctl_get_vmobject_list_deallocate_and_exit;
6221 	}
6222 
6223 	if (corpse == false) {
6224 		/* copy the vmobjects and vmobject data out of the task */
6225 		if (buffer_size == 0) {
6226 			output_size = max_size;
6227 		} else {
6228 			buffer_size = (buffer_size > max_size) ? max_size : buffer_size;
6229 			buffer = (struct _vmobject_list_output_ *)kalloc_data(buffer_size, Z_WAITOK);
6230 
6231 			if (!buffer) {
6232 				error = ENOMEM;
6233 				goto sysctl_get_vmobject_list_deallocate_and_exit;
6234 			}
6235 			free_buffer = true;
6236 
6237 			task_get_owned_vmobjects(task, buffer_size, buffer, &output_size, &entries);
6238 		}
6239 	} else {
6240 		vmobject_list_output_t list;
6241 
6242 		task_get_corpse_vmobject_list(task, &list, &max_size);
6243 		assert(buffer == NULL);
6244 
6245 		/* copy corpse_vmobject_list to output buffer to avoid double copy */
6246 		if (buffer_size) {
6247 			size_t temp_size;
6248 
6249 			temp_size = buffer_size > max_size ? max_size : buffer_size;
6250 			output_size = temp_size - sizeof(*buffer);
6251 			/* whole multiple of vm_object_query_data_t */
6252 			output_size = (output_size / sizeof(vm_object_query_data_t)) * sizeof(vm_object_query_data_t) + sizeof(*buffer);
6253 			buffer = list;
6254 		} else {
6255 			output_size = max_size;
6256 		}
6257 	}
6258 
6259 	/* req->oldptr should be USER_ADDR_NULL if buffer == NULL and return the current size */
6260 	/* otherwise copy buffer to oldptr and return the bytes copied */
6261 	size_t num_copied, chunk_size;
6262 	for (num_copied = 0, chunk_size = 0;
6263 	    num_copied < output_size;
6264 	    num_copied += chunk_size) {
6265 		chunk_size = MIN(output_size - num_copied, copysize_limit_panic);
6266 		error = SYSCTL_OUT(req, (char *)buffer + num_copied, chunk_size);
6267 		if (error) {
6268 			break;
6269 		}
6270 	}
6271 
6272 sysctl_get_vmobject_list_deallocate_and_exit:
6273 	task_deallocate(task);
6274 
6275 sysctl_get_vmobject_list_exit:
6276 	if (free_buffer) {
6277 		kfree_data(buffer, buffer_size);
6278 	}
6279 
6280 	return error;
6281 }
6282 
6283 SYSCTL_PROC(_vm, OID_AUTO, get_owned_vmobjects,
6284     CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_WR | CTLFLAG_MASKED | CTLFLAG_KERN | CTLFLAG_LOCKED | CTLFLAG_ANYBODY,
6285     0, 0, sysctl_get_owned_vmobjects, "A", "get owned vmobjects in task");
6286 
6287 extern uint64_t num_static_scalable_counters;
6288 SYSCTL_QUAD(_kern, OID_AUTO, num_static_scalable_counters, CTLFLAG_RD | CTLFLAG_LOCKED, &num_static_scalable_counters, "");
6289 
6290 #if SCHED_HYGIENE_DEBUG
6291 TUNABLE_DT(bool, sched_hygiene_nonspec_tb, "machine-timeouts", "nonspec-tb", "sched-hygiene-nonspec-tb", false, TUNABLE_DT_NONE);
6292 static SECURITY_READ_ONLY_LATE(int) sched_hygiene_debug_available = 1;
6293 #else
6294 static SECURITY_READ_ONLY_LATE(int) sched_hygiene_debug_available = 0;
6295 #endif /* SCHED_HYGIENE_DEBUG */
6296 
6297 SYSCTL_INT(_debug, OID_AUTO, sched_hygiene_debug_available,
6298     CTLFLAG_KERN | CTLFLAG_RD | CTLFLAG_LOCKED,
6299     &sched_hygiene_debug_available, 0, "");
6300 
6301 uuid_string_t trial_treatment_id;
6302 uuid_string_t trial_experiment_id;
6303 int trial_deployment_id = -1;
6304 
6305 SYSCTL_STRING(_kern, OID_AUTO, trial_treatment_id, CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_ANYBODY | CTLFLAG_LEGACY_EXPERIMENT, trial_treatment_id, sizeof(trial_treatment_id), "");
6306 SYSCTL_STRING(_kern, OID_AUTO, trial_experiment_id, CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_ANYBODY | CTLFLAG_LEGACY_EXPERIMENT, trial_experiment_id, sizeof(trial_experiment_id), "");
6307 SYSCTL_INT(_kern, OID_AUTO, trial_deployment_id, CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_ANYBODY | CTLFLAG_LEGACY_EXPERIMENT, &trial_deployment_id, 0, "");
6308 
6309 #if (DEVELOPMENT || DEBUG)
6310 /* For unit testing setting factors & limits. */
6311 unsigned int testing_experiment_factor;
6312 EXPERIMENT_FACTOR_LEGACY_UINT(_kern, testing_experiment_factor, &testing_experiment_factor, 5, 10, "");
6313 
6314 static int32_t experiment_factor_test;
6315 EXPERIMENT_FACTOR_INT(test, &experiment_factor_test, 0, 32, "test factor");
6316 
6317 #if MACH_ASSERT && __arm64__
6318 /* rdar://149041040 */
6319 extern unsigned int panic_on_jit_guard;
6320 EXPERIMENT_FACTOR_UINT(jitguard, &panic_on_jit_guard, 0, 7, "Panic on JIT guard failure");
6321 #endif /* MACH_ASSERT && __arm64__ */
6322 
6323 extern int exception_log_max_pid;
6324 SYSCTL_INT(_debug, OID_AUTO, exception_log_max_pid, CTLFLAG_RW | CTLFLAG_LOCKED, &exception_log_max_pid, 0, "Log exceptions for all processes up to this pid");
6325 #endif /* (DEVELOPMENT || DEBUG) */
6326 
6327 #if DEVELOPMENT || DEBUG
6328 static int
6329 unlink_kernelcore_sysctl SYSCTL_HANDLER_ARGS
6330 {
6331 	if (!req->newptr) {
6332 		return EINVAL;
6333 	}
6334 	void IOBSDLowSpaceUnlinkKernelCore(void);
6335 	IOBSDLowSpaceUnlinkKernelCore();
6336 	return 0;
6337 }
6338 
6339 SYSCTL_PROC(_kern, OID_AUTO, unlink_kernelcore,
6340     CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED | CTLFLAG_MASKED, 0, 0,
6341     unlink_kernelcore_sysctl, "-", "unlink the kernelcore file");
6342 #endif /* DEVELOPMENT || DEBUG */
6343 
6344 #if CONFIG_IOTRACE
6345 #pragma clang diagnostic push
6346 #pragma clang diagnostic ignored "-Wcast-qual"
6347 SYSCTL_INT(_debug, OID_AUTO, MMIOtrace,
6348     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
6349     (int *)&mmiotrace_enabled, 0, "");
6350 #pragma clang diagnostic pop
6351 #endif /* CONFIG_IOTRACE */
6352 
6353 static int
6354 sysctl_page_protection_type SYSCTL_HANDLER_ARGS
6355 {
6356 #pragma unused(oidp, arg1, arg2)
6357 	int value = ml_page_protection_type();
6358 	return SYSCTL_OUT(req, &value, sizeof(value));
6359 }
6360 
6361 SYSCTL_PROC(_kern, OID_AUTO, page_protection_type,
6362     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
6363     0, 0, sysctl_page_protection_type, "I", "Type of page protection that the system supports");
6364 
6365 #if CONFIG_SPTM && HAS_SPTM_SYSCTL
6366 extern bool disarm_protected_io;
6367 static int sysctl_sptm_disarm_protected_io SYSCTL_HANDLER_ARGS
6368 {
6369 	int error = 0;
6370 
6371 	uint64_t old_disarm_protected_io = (uint64_t) disarm_protected_io;
6372 	error = SYSCTL_OUT(req, &old_disarm_protected_io, sizeof(old_disarm_protected_io));
6373 
6374 	if (error) {
6375 		return error;
6376 	}
6377 
6378 	uint64_t new_disarm_protected_io = old_disarm_protected_io;
6379 	if (req->newptr) {
6380 		error = SYSCTL_IN(req, &new_disarm_protected_io, sizeof(new_disarm_protected_io));
6381 		if (!disarm_protected_io && new_disarm_protected_io) {
6382 			sptm_sysctl(SPTM_SYSCTL_DISARM_PROTECTED_IO, SPTM_SYSCTL_SET, 1);
6383 			os_atomic_thread_fence(release);
6384 			disarm_protected_io = true;
6385 		}
6386 	}
6387 
6388 	return error;
6389 }
6390 SYSCTL_PROC(_kern, OID_AUTO, sptm_disarm_protected_io, CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED, 0, 0, sysctl_sptm_disarm_protected_io, "Q", "");
6391 
6392 /**
6393  * Usage of kern.sptm_sysctl_poke
6394  *
6395  * This sysctl provides a convenient way to trigger the "getter" handler of a
6396  * specified SPTM sysctl. With this sysctl, you can trigger arbitrary SPTM
6397  * code without modifying xnu source code. All you need to do is define a
6398  * new SPTM sysctl and implement its "getter". After that, you can write
6399  * the SPTM sysctl number to this sysctl to trigger it.
6400  */
6401 static int sysctl_sptm_sysctl_poke SYSCTL_HANDLER_ARGS
6402 {
6403 	int error = 0;
6404 
6405 	/* Always read-as-zero. */
6406 	const uint64_t out = 0;
6407 	error = SYSCTL_OUT(req, &out, sizeof(out));
6408 
6409 	if (error) {
6410 		return error;
6411 	}
6412 
6413 	uint64_t selector;
6414 	if (req->newptr) {
6415 		error = SYSCTL_IN(req, &selector, sizeof(selector));
6416 		sptm_sysctl(selector, SPTM_SYSCTL_GET, 0);
6417 	}
6418 
6419 	return error;
6420 }
6421 SYSCTL_PROC(_kern, OID_AUTO, sptm_sysctl_poke, CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED, 0, 0, sysctl_sptm_sysctl_poke, "Q", "");
6422 #endif /* CONFIG_SPTM && HAS_SPTM_SYSCTL */
6423 
6424 #if CONFIG_SPTM && (DEVELOPMENT || DEBUG)
6425 /**
6426  * Sysctls to get SPTM allowed I/O ranges, pmap I/O ranges and I/O ranges by index.
6427  * Used by SEAR/LASER tools.
6428  */
6429 static int
6430 sysctl_sptm_allowed_io_ranges SYSCTL_HANDLER_ARGS
6431 {
6432 #pragma unused(oidp, arg1, arg2)
6433 	sptm_io_range_t io_range = { 0 };
6434 	unsigned int index = 0;
6435 
6436 	int error = SYSCTL_IN(req, &index, sizeof(index));
6437 	if (error) {
6438 		return error;
6439 	}
6440 
6441 	libsptm_error_t ret = sptm_get_info(INFO_SPTM_ALLOWED_IO_RANGES, index, &io_range);
6442 	if (__improbable(ret != LIBSPTM_SUCCESS)) {
6443 		return EINVAL;
6444 	}
6445 
6446 	return SYSCTL_OUT(req, &io_range, sizeof(io_range));
6447 }
6448 SYSCTL_PROC(_kern, OID_AUTO, sptm_allowed_io_ranges, CTLTYPE_STRUCT | CTLFLAG_RW | CTLFLAG_LOCKED,
6449     0, 0, sysctl_sptm_allowed_io_ranges, "S,sptm_io_range_t", "SPTM allowed I/O ranges by index");
6450 
6451 static int
6452 sysctl_sptm_allowed_io_ranges_count SYSCTL_HANDLER_ARGS
6453 {
6454 #pragma unused(oidp, arg1, arg2)
6455 	unsigned int count = 0;
6456 
6457 	libsptm_error_t ret = sptm_get_info(INFO_SPTM_ALLOWED_IO_RANGES_COUNT, 0, &count);
6458 	if (__improbable(ret != LIBSPTM_SUCCESS)) {
6459 		return EINVAL;
6460 	}
6461 
6462 	return SYSCTL_OUT(req, &count, sizeof(count));
6463 }
6464 SYSCTL_PROC(_kern, OID_AUTO, sptm_allowed_io_ranges_count, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
6465     0, 0, sysctl_sptm_allowed_io_ranges_count, "I", "SPTM allowed I/O ranges count");
6466 
6467 static int
6468 sysctl_sptm_pmap_io_ranges SYSCTL_HANDLER_ARGS
6469 {
6470 #pragma unused(oidp, arg1, arg2)
6471 	sptm_io_range_t io_range = { 0 };
6472 	unsigned int index = 0;
6473 
6474 	int error = SYSCTL_IN(req, &index, sizeof(index));
6475 	if (error) {
6476 		return error;
6477 	}
6478 
6479 	libsptm_error_t ret = sptm_get_info(INFO_SPTM_PMAP_IO_RANGES, index, &io_range);
6480 	if (__improbable(ret != LIBSPTM_SUCCESS)) {
6481 		return EINVAL;
6482 	}
6483 
6484 	return SYSCTL_OUT(req, &io_range, sizeof(io_range));
6485 }
6486 SYSCTL_PROC(_kern, OID_AUTO, sptm_pmap_io_ranges, CTLTYPE_STRUCT | CTLFLAG_RW | CTLFLAG_LOCKED,
6487     0, 0, sysctl_sptm_pmap_io_ranges, "S,sptm_io_range_t", "SPTM pmap I/O ranges by index");
6488 
6489 static int
6490 sysctl_sptm_pmap_io_ranges_count SYSCTL_HANDLER_ARGS
6491 {
6492 #pragma unused(oidp, arg1, arg2)
6493 	unsigned int count = 0;
6494 
6495 	libsptm_error_t ret = sptm_get_info(INFO_SPTM_PMAP_IO_RANGES_COUNT, 0, &count);
6496 	if (__improbable(ret != LIBSPTM_SUCCESS)) {
6497 		return EINVAL;
6498 	}
6499 
6500 	return SYSCTL_OUT(req, &count, sizeof(count));
6501 }
6502 SYSCTL_PROC(_kern, OID_AUTO, sptm_pmap_io_ranges_count, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
6503     0, 0, sysctl_sptm_pmap_io_ranges_count, "I", "SPTM pmap I/O ranges count");
6504 
6505 static int
6506 sysctl_sptm_io_ranges SYSCTL_HANDLER_ARGS
6507 {
6508 #pragma unused(oidp, arg1, arg2)
6509 	sptm_io_range_t io_range = { 0 };
6510 	unsigned int index = 0;
6511 
6512 	int error = SYSCTL_IN(req, &index, sizeof(index));
6513 	if (error) {
6514 		return error;
6515 	}
6516 
6517 	libsptm_error_t ret = sptm_get_info(INFO_SPTM_IO_RANGES, index, &io_range);
6518 	if (__improbable(ret != LIBSPTM_SUCCESS)) {
6519 		return EINVAL;
6520 	}
6521 
6522 	return SYSCTL_OUT(req, &io_range, sizeof(io_range));
6523 }
6524 SYSCTL_PROC(_kern, OID_AUTO, sptm_io_ranges, CTLTYPE_STRUCT | CTLFLAG_RW | CTLFLAG_LOCKED,
6525     0, 0, sysctl_sptm_io_ranges, "S,sptm_io_range_t", "SPTM I/O ranges by index");
6526 
6527 static int
6528 sysctl_sptm_io_ranges_count SYSCTL_HANDLER_ARGS
6529 {
6530 #pragma unused(oidp, arg1, arg2)
6531 	unsigned int count = 0;
6532 
6533 	libsptm_error_t ret = sptm_get_info(INFO_SPTM_IO_RANGES_COUNT, 0, &count);
6534 	if (__improbable(ret != LIBSPTM_SUCCESS)) {
6535 		return EINVAL;
6536 	}
6537 
6538 	return SYSCTL_OUT(req, &count, sizeof(count));
6539 }
6540 SYSCTL_PROC(_kern, OID_AUTO, sptm_io_ranges_count, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
6541     0, 0, sysctl_sptm_io_ranges_count, "I", "SPTM I/O ranges count");
6542 #endif /* CONFIG_SPTM && (DEVELOPMENT || DEBUG) */
6543 
6544 #if __ARM64_PMAP_SUBPAGE_L1__ && CONFIG_SPTM
6545 extern bool surt_ready;
6546 static int
6547 sysctl_surt_ready SYSCTL_HANDLER_ARGS
6548 {
6549 #pragma unused(oidp, arg1, arg2)
6550 	unsigned int surt_ready_uint = (unsigned int)surt_ready;
6551 	return SYSCTL_OUT(req, &surt_ready_uint, sizeof(surt_ready_uint));
6552 }
6553 SYSCTL_PROC(_kern, OID_AUTO, surt_ready, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
6554     0, 0, sysctl_surt_ready, "I", "SURT system readiness");
6555 #endif /* __ARM64_PMAP_SUBPAGE_L1__ && CONFIG_SPTM */
6556 
6557 #if __arm64__ && (DEBUG || DEVELOPMENT)
6558 extern unsigned int pmap_wcrt_on_non_dram_count_get(void);
6559 static int
6560 sysctl_pmap_wcrt_on_non_dram_count SYSCTL_HANDLER_ARGS
6561 {
6562 #pragma unused(oidp, arg1, arg2)
6563 	unsigned int count = pmap_wcrt_on_non_dram_count_get();
6564 
6565 	return SYSCTL_OUT(req, &count, sizeof(count));
6566 }
6567 SYSCTL_PROC(_kern, OID_AUTO, pmap_wcrt_on_non_dram_count, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
6568     0, 0, sysctl_pmap_wcrt_on_non_dram_count, "I", "pmap WC/RT mapping request on non-DRAM count");
6569 #endif /* __arm64__ && (DEBUG || DEVELOPMENT) */
6570 
6571 TUNABLE_DT(int, gpu_pmem_selector, "defaults", "kern.gpu_pmem_selector", "gpu-pmem-selector", 0, TUNABLE_DT_NONE);
6572 
6573 #if CONFIG_EXCLAVES
6574 
6575 static int
6576 sysctl_task_conclave SYSCTL_HANDLER_ARGS
6577 {
6578 	extern const char *exclaves_resource_name(void *);
6579 
6580 #pragma unused(arg2)
6581 	void *conclave = task_get_conclave(current_task());
6582 	if (conclave != NULL) {
6583 		const char *name = exclaves_resource_name(conclave);
6584 		assert3u(strlen(name), >, 0);
6585 
6586 		/*
6587 		 * This is a RO operation already and the string is never
6588 		 * written to.
6589 		 */
6590 #pragma clang diagnostic push
6591 #pragma clang diagnostic ignored "-Wcast-qual"
6592 		return sysctl_handle_string(oidp, (char *)name, 0, req);
6593 #pragma clang diagnostic pop
6594 	}
6595 	return sysctl_handle_string(oidp, arg1, MAXCONCLAVENAME, req);
6596 }
6597 
6598 SYSCTL_PROC(_kern, OID_AUTO, task_conclave,
6599     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
6600     "", 0, sysctl_task_conclave, "A", "Conclave string for the task");
6601 
6602 
6603 void task_set_conclave_untaintable(task_t task);
6604 
6605 static int
6606 sysctl_task_conclave_untaintable SYSCTL_HANDLER_ARGS
6607 {
6608 #pragma unused(arg1, arg2)
6609 	int error, val = 0;
6610 	error = sysctl_handle_int(oidp, &val, 0, req);
6611 	if (error || val == 0) {
6612 		return error;
6613 	}
6614 
6615 	task_set_conclave_untaintable(current_task());
6616 	return 0;
6617 }
6618 
6619 SYSCTL_PROC(_kern, OID_AUTO, task_conclave_untaintable,
6620     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
6621     "", 0, sysctl_task_conclave_untaintable, "A", "Task could not be tainted by talking to conclaves");
6622 
6623 extern exclaves_requirement_t exclaves_relaxed_requirements;
6624 SYSCTL_QUAD(_kern, OID_AUTO, exclaves_relaxed_requirements,
6625     CTLFLAG_KERN | CTLFLAG_RD | CTLFLAG_LOCKED,
6626     &exclaves_relaxed_requirements, "Exclaves requirements which have been relaxed");
6627 
6628 #endif /* CONFIG_EXCLAVES */
6629 
6630 #if (DEVELOPMENT || DEBUG)
6631 SYSCTL_INT(_kern, OID_AUTO, gpu_pmem_selector,
6632     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED | CTLFLAG_KERN,
6633     &gpu_pmem_selector, 0, "GPU wire down limit selector");
6634 #else /* !(DEVELOPMENT || DEBUG) */
6635 SYSCTL_INT(_kern, OID_AUTO, gpu_pmem_selector,
6636     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED | CTLFLAG_KERN | CTLFLAG_MASKED,
6637     &gpu_pmem_selector, 0, "GPU wire down limit selector");
6638 #endif /* (DEVELOPMENT || DEBUG) */
6639 
6640 static int
6641 sysctl_exclaves_status SYSCTL_HANDLER_ARGS
6642 {
6643 	int value = exclaves_get_status();
6644 	return sysctl_io_number(req, value, sizeof(value), NULL, NULL);
6645 }
6646 
6647 SYSCTL_PROC(_kern, OID_AUTO, exclaves_status,
6648     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
6649     0, 0, sysctl_exclaves_status, "I", "Running status of Exclaves");
6650 
6651 
6652 static int
6653 sysctl_exclaves_boot_stage SYSCTL_HANDLER_ARGS
6654 {
6655 	int value = exclaves_get_boot_stage();
6656 	return sysctl_io_number(req, value, sizeof(value), NULL, NULL);
6657 }
6658 
6659 SYSCTL_PROC(_kern, OID_AUTO, exclaves_boot_stage,
6660     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
6661     0, 0, sysctl_exclaves_boot_stage, "I", "Boot stage of Exclaves");
6662 
6663 #if CONFIG_EXCLAVES && (DEVELOPMENT || DEBUG)
6664 extern unsigned int exclaves_debug;
6665 SYSCTL_UINT(_kern, OID_AUTO, exclaves_debug, CTLFLAG_RW | CTLFLAG_LOCKED,
6666     &exclaves_debug, 0, "Exclaves debug flags");
6667 
6668 static int
6669 sysctl_exclaves_inspection_status SYSCTL_HANDLER_ARGS
6670 {
6671 #pragma unused(oidp, arg1, arg2)
6672 	int value = (int)exclaves_inspection_is_initialized();
6673 	return sysctl_io_number(req, value, sizeof(value), NULL, NULL);
6674 }
6675 SYSCTL_PROC(_kern, OID_AUTO, exclaves_inspection_status,
6676     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
6677     0, 0, sysctl_exclaves_inspection_status, "I", "Exclaves debug inspection status");
6678 #endif /* CONFIG_EXCLAVES && (DEVELOPMENT || DEBUG) */
6679 
6680 #if (DEBUG || DEVELOPMENT)
6681 extern uint32_t disable_vm_sanitize_telemetry;
6682 SYSCTL_UINT(_debug, OID_AUTO, disable_vm_sanitize_telemetry, CTLFLAG_RW | CTLFLAG_LOCKED /*| CTLFLAG_MASKED*/, &disable_vm_sanitize_telemetry, 0, "disable VM API sanitization telemetry");
6683 #endif
6684 
6685 #define kReadUserspaceRebootInfoEntitlement "com.apple.private.kernel.userspacereboot-info-read-only"
6686 static int
6687 _sysctl_userspacereboot_info(struct sysctl_req *req, void *ptr, size_t ptr_size)
6688 {
6689 	if (req->newptr != 0) {
6690 		/* initproc is the only process that can write to these sysctls */
6691 		if (proc_getpid(req->p) != 1) {
6692 			return EPERM;
6693 		}
6694 		return SYSCTL_IN(req, ptr, ptr_size);
6695 	} else {
6696 		/* A read entitlement is required to read these sysctls */
6697 		if (!IOCurrentTaskHasEntitlement(kReadUserspaceRebootInfoEntitlement)) {
6698 			return EPERM;
6699 		}
6700 		return SYSCTL_OUT(req, ptr, ptr_size);
6701 	}
6702 }
6703 
6704 static int
6705 sysctl_userspacereboottime(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
6706 {
6707 	return _sysctl_userspacereboot_info(req, &userspacereboottime, sizeof(userspacereboottime));
6708 }
6709 
6710 static int
6711 sysctl_userspacerebootpurpose(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
6712 {
6713 	return _sysctl_userspacereboot_info(req, &userspacerebootpurpose, sizeof(userspacerebootpurpose));
6714 }
6715 
6716 SYSCTL_PROC(_kern, OID_AUTO, userspacereboottime, CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED, 0, 0, sysctl_userspacereboottime, "Q", "");
6717 SYSCTL_PROC(_kern, OID_AUTO, userspacerebootpurpose, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, 0, 0, sysctl_userspacerebootpurpose, "I", "");
6718 
6719 #if XNU_TARGET_OS_IOS
6720 
6721 static LCK_GRP_DECLARE(erm_config_lock_grp, "ERM sysctl");
6722 static LCK_RW_DECLARE(erm_config_lock, &erm_config_lock_grp);
6723 #define ERM_CONFIG_SYSCTL_WRITE_ENTITLEMENT "com.apple.private.security-research-device.extended-research-mode"
6724 #define ERM_CONFIG_SYSCTL_MAX_SIZE PAGE_SIZE
6725 
6726 // This sysctl handler is only registered when Extended Research Mode (ERM) is active.
6727 static int
6728 sysctl_user_extended_research_mode_config_handler(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
6729 {
6730 	// Pointer for the dynamically allocated buffer
6731 	static void *extended_research_mode_config_data = NULL;
6732 
6733 	// Current size of the valid data stored in the buffer
6734 	static size_t extended_research_mode_config_current_size = 0;
6735 
6736 	// Handle Read request (user wants to read the current config, before it is overwritten)
6737 	if (req->oldptr != USER_ADDR_NULL) {
6738 		int error = 0;
6739 
6740 		lck_rw_lock_shared(&erm_config_lock);
6741 
6742 		if (req->oldlen < extended_research_mode_config_current_size) {
6743 			error = ENOMEM;
6744 		} else {
6745 			if (extended_research_mode_config_current_size > 0) {
6746 				error = copyout(extended_research_mode_config_data,
6747 				    req->oldptr,
6748 				    extended_research_mode_config_current_size);
6749 			}
6750 		}
6751 		// In all cases, report the total size of the currently stored config back to the user,
6752 		req->oldlen = extended_research_mode_config_current_size;
6753 		req->oldidx = req->oldlen;
6754 
6755 		lck_rw_unlock_shared(&erm_config_lock);
6756 
6757 		if (error != 0) {
6758 			return error;
6759 		}
6760 	} else {
6761 		// User just want to know the current buffer size.
6762 		// All accesses to extended_research_mode_config* variables are expected
6763 		// to be done under erm_config_lock.
6764 		lck_rw_lock_shared(&erm_config_lock);
6765 		req->oldidx = extended_research_mode_config_current_size;
6766 		lck_rw_unlock_shared(&erm_config_lock);
6767 	}
6768 
6769 
6770 	// Handle Write request (new data provided by user)
6771 	if (req->newptr != USER_ADDR_NULL) {
6772 		if (!IOTaskHasEntitlement(proc_task(req->p), ERM_CONFIG_SYSCTL_WRITE_ENTITLEMENT)) {
6773 			return EPERM;
6774 		}
6775 
6776 		size_t requested_len = req->newlen;
6777 
6778 		if (requested_len > ERM_CONFIG_SYSCTL_MAX_SIZE) {
6779 			// We ensure the config provided by user-space is not too big
6780 			return EINVAL;
6781 		}
6782 
6783 		// Allocate a new buffer for the incoming data
6784 		void *new_buffer = (void *)kalloc_data(requested_len, Z_WAITOK | Z_ZERO);
6785 
6786 		if (new_buffer == NULL) {
6787 			return ENOMEM; // Allocation failed
6788 		}
6789 
6790 		// Copy data from user space into the newly allocated buffer
6791 		int error = copyin(req->newptr, new_buffer, requested_len);
6792 
6793 		if (error == 0) {
6794 			// Success: Replace the old buffer with the new one
6795 			lck_rw_lock_exclusive(&erm_config_lock);
6796 
6797 			// Backup old buffer info for freeing it in a second step
6798 			void *old_buffer_to_free = extended_research_mode_config_data;
6799 			size_t old_buffer_size = extended_research_mode_config_current_size;
6800 
6801 			// Point to the new buffer and update size
6802 			extended_research_mode_config_data = new_buffer;
6803 			extended_research_mode_config_current_size = requested_len;
6804 			lck_rw_unlock_exclusive(&erm_config_lock);
6805 			new_buffer = NULL;  // transferred to the static pointer
6806 
6807 			// Previous buffer is not referenced anymore, good to be deleted.
6808 			kfree_data(old_buffer_to_free, old_buffer_size);
6809 		} else {
6810 			// Copyin failed, free the buffer we just allocated and keep the old data and size intact
6811 			kfree_data(new_buffer, requested_len);
6812 			return error;
6813 		}
6814 	}
6815 
6816 	return 0;
6817 }
6818 
6819 // We don't register this sysctl handler automatically , but rather only register it only if the extended
6820 // research mode is active.
6821 SYSCTL_PROC(_user,                // Parent node structure (_kern)
6822     OID_AUTO,                     // Automatically assign OID
6823     extended_research_mode_config,         // Name of the node
6824     CTLFLAG_NOAUTO |              // We will register this sysctl on our own
6825     CTLTYPE_OPAQUE |              // Type: Opaque binary data
6826     CTLFLAG_WR |                  // Allow both read and write
6827     CTLFLAG_ANYBODY |                             // No user filtering
6828     CTLFLAG_LOCKED,               // The handler manages its own locking.
6829     NULL,                         // arg1 (not used)
6830     0,                            // arg2 (not used)
6831     &sysctl_user_extended_research_mode_config_handler,
6832     "-",                          // don't print the content (as it is a blob)
6833     "Configuration blob for Extended Research Mode");
6834 
6835 // This function is defined in kern_codesigning.c but don't worth include the whole .h just for it.
6836 bool extended_research_mode_state(void);
6837 
6838 // Only register the research_mode_config sysctl if Extended Research Mode is active
6839 __startup_func
6840 static void
6841 extended_research_mode_config_sysctl_startup(void)
6842 {
6843 	if (__improbable(extended_research_mode_state())) {
6844 		// Register the sysctl handler
6845 		sysctl_register_oid_early(&sysctl__user_extended_research_mode_config);
6846 	}
6847 }
6848 STARTUP(SYSCTL, STARTUP_RANK_MIDDLE, extended_research_mode_config_sysctl_startup);
6849 #endif /* XNU_TARGET_OS_IOS */
6850 
6851 #if DEBUG || DEVELOPMENT
6852 SCALABLE_COUNTER_DEFINE(mach_eventlink_handoff_success_count);
6853 SYSCTL_SCALABLE_COUNTER(_kern, mach_eventlink_handoff_success_count,
6854     mach_eventlink_handoff_success_count, "Number of successful handoffs");
6855 #endif /* DEBUG || DEVELOPMENT*/
6856