xref: /xnu-8019.80.24/bsd/kern/kern_sysctl.c (revision a325d9c4a84054e40bbe985afedcb50ab80993ea)
1 /*
2  * Copyright (c) 2000-2019 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 <stdatomic.h>
108 #include <uuid/uuid.h>
109 
110 #include <security/audit/audit.h>
111 #include <kern/kalloc.h>
112 
113 #include <machine/smp.h>
114 #include <machine/atomic.h>
115 #include <machine/config.h>
116 #include <mach/machine.h>
117 #include <mach/mach_host.h>
118 #include <mach/mach_types.h>
119 #include <mach/processor_info.h>
120 #include <mach/vm_param.h>
121 #include <kern/debug.h>
122 #include <kern/mach_param.h>
123 #include <kern/task.h>
124 #include <kern/thread.h>
125 #include <kern/thread_group.h>
126 #include <kern/processor.h>
127 #include <kern/cpu_number.h>
128 #include <kern/cpu_quiesce.h>
129 #include <kern/sched_prim.h>
130 #include <vm/vm_kern.h>
131 #include <vm/vm_map.h>
132 #include <mach/host_info.h>
133 
134 #include <sys/mount_internal.h>
135 #include <sys/kdebug.h>
136 #include <sys/kern_sysctl.h>
137 
138 #include <IOKit/IOPlatformExpert.h>
139 #include <pexpert/pexpert.h>
140 
141 #include <machine/machine_routines.h>
142 #include <machine/exec.h>
143 
144 #include <nfs/nfs_conf.h>
145 
146 #include <vm/vm_protos.h>
147 #include <vm/vm_pageout.h>
148 #include <vm/vm_compressor_algorithms.h>
149 #include <sys/imgsrc.h>
150 #include <kern/timer_call.h>
151 #include <sys/codesign.h>
152 #include <IOKit/IOBSD.h>
153 #if CONFIG_CSR
154 #include <sys/csr.h>
155 #endif
156 
157 #if defined(__i386__) || defined(__x86_64__)
158 #include <i386/cpuid.h>
159 #endif
160 
161 #if CONFIG_FREEZE
162 #include <sys/kern_memorystatus.h>
163 #endif
164 
165 #if KPERF
166 #include <kperf/kperf.h>
167 #endif
168 
169 #if HYPERVISOR
170 #include <kern/hv_support.h>
171 #endif
172 
173 /*
174  * deliberately setting max requests to really high number
175  * so that runaway settings do not cause MALLOC overflows
176  */
177 #define AIO_MAX_REQUESTS (128 * CONFIG_AIO_MAX)
178 
179 extern int aio_max_requests;
180 extern int aio_max_requests_per_process;
181 extern int aio_worker_threads;
182 extern int lowpri_IO_window_msecs;
183 extern int lowpri_IO_delay_msecs;
184 #if DEVELOPMENT || DEBUG
185 extern int nx_enabled;
186 #endif
187 extern int speculative_reads_disabled;
188 extern unsigned int speculative_prefetch_max;
189 extern unsigned int speculative_prefetch_max_iosize;
190 extern unsigned int preheat_max_bytes;
191 extern unsigned int preheat_min_bytes;
192 extern long numvnodes;
193 extern long freevnodes;
194 extern long num_recycledvnodes;
195 
196 extern uuid_string_t bootsessionuuid_string;
197 
198 extern unsigned int vm_max_delayed_work_limit;
199 extern unsigned int vm_max_batch;
200 
201 extern unsigned int vm_page_free_min;
202 extern unsigned int vm_page_free_target;
203 extern unsigned int vm_page_free_reserved;
204 
205 #if (DEVELOPMENT || DEBUG)
206 extern uint32_t vm_page_creation_throttled_hard;
207 extern uint32_t vm_page_creation_throttled_soft;
208 #endif /* DEVELOPMENT || DEBUG */
209 
210 /*
211  * Conditionally allow dtrace to see these functions for debugging purposes.
212  */
213 #ifdef STATIC
214 #undef STATIC
215 #endif
216 #if 0
217 #define STATIC
218 #else
219 #define STATIC static
220 #endif
221 
222 extern boolean_t    mach_timer_coalescing_enabled;
223 
224 extern uint64_t timer_deadline_tracking_bin_1, timer_deadline_tracking_bin_2;
225 
226 STATIC void
227 fill_user32_eproc(proc_t, struct user32_eproc *__restrict);
228 STATIC void
229 fill_user32_externproc(proc_t, struct user32_extern_proc *__restrict);
230 STATIC void
231 fill_user64_eproc(proc_t, struct user64_eproc *__restrict);
232 STATIC void
233 fill_user64_proc(proc_t, struct user64_kinfo_proc *__restrict);
234 STATIC void
235 fill_user64_externproc(proc_t, struct user64_extern_proc *__restrict);
236 STATIC void
237 fill_user32_proc(proc_t, struct user32_kinfo_proc *__restrict);
238 
239 extern int
240 kdbg_control(int *name, u_int namelen, user_addr_t where, size_t * sizep);
241 #if CONFIG_NFS_CLIENT
242 extern int
243 netboot_root(void);
244 #endif
245 int
246 sysctl_procargs(int *name, u_int namelen, user_addr_t where,
247     size_t *sizep, proc_t cur_proc);
248 STATIC int
249 sysctl_procargsx(int *name, u_int namelen, user_addr_t where, size_t *sizep,
250     proc_t cur_proc, int argc_yes);
251 int
252 sysctl_struct(user_addr_t oldp, size_t *oldlenp, user_addr_t newp,
253     size_t newlen, void *sp, int len);
254 
255 STATIC int sysdoproc_filt_KERN_PROC_PID(proc_t p, void * arg);
256 STATIC int sysdoproc_filt_KERN_PROC_PGRP(proc_t p, void * arg);
257 STATIC int sysdoproc_filt_KERN_PROC_TTY(proc_t p, void * arg);
258 STATIC int  sysdoproc_filt_KERN_PROC_UID(proc_t p, void * arg);
259 STATIC int  sysdoproc_filt_KERN_PROC_RUID(proc_t p, void * arg);
260 int sysdoproc_callback(proc_t p, void *arg);
261 
262 #if CONFIG_THREAD_GROUPS && (DEVELOPMENT || DEBUG)
263 STATIC int sysctl_get_thread_group_id SYSCTL_HANDLER_ARGS;
264 #endif
265 
266 /* forward declarations for non-static STATIC */
267 STATIC void fill_loadavg64(struct loadavg *la, struct user64_loadavg *la64);
268 STATIC void fill_loadavg32(struct loadavg *la, struct user32_loadavg *la32);
269 STATIC int sysctl_handle_kern_threadname(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
270 STATIC int sysctl_sched_stats(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
271 STATIC int sysctl_sched_stats_enable(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
272 STATIC int sysctl_kdebug_ops SYSCTL_HANDLER_ARGS;
273 #if COUNT_SYSCALLS
274 STATIC int sysctl_docountsyscalls SYSCTL_HANDLER_ARGS;
275 #endif  /* COUNT_SYSCALLS */
276 #if defined(XNU_TARGET_OS_OSX)
277 STATIC int sysctl_doprocargs SYSCTL_HANDLER_ARGS;
278 #endif  /* defined(XNU_TARGET_OS_OSX) */
279 STATIC int sysctl_doprocargs2 SYSCTL_HANDLER_ARGS;
280 STATIC int sysctl_prochandle SYSCTL_HANDLER_ARGS;
281 STATIC int sysctl_aiomax(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
282 STATIC int sysctl_aioprocmax(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
283 STATIC int sysctl_aiothreads(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
284 STATIC int sysctl_maxproc(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
285 STATIC int sysctl_osversion(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
286 STATIC int sysctl_sysctl_bootargs(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
287 STATIC int sysctl_maxvnodes(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
288 STATIC int sysctl_securelvl(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
289 STATIC int sysctl_domainname(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
290 STATIC int sysctl_hostname(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
291 STATIC int sysctl_procname(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
292 STATIC int sysctl_boottime(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
293 STATIC int sysctl_bootuuid(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
294 STATIC int sysctl_symfile(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
295 #if CONFIG_NFS_CLIENT
296 STATIC int sysctl_netboot(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
297 #endif
298 #ifdef CONFIG_IMGSRC_ACCESS
299 STATIC int sysctl_imgsrcdev(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
300 #endif
301 STATIC int sysctl_usrstack(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
302 STATIC int sysctl_usrstack64(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
303 #if CONFIG_COREDUMP
304 STATIC int sysctl_coredump(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
305 STATIC int sysctl_suid_coredump(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
306 #endif
307 STATIC int sysctl_delayterm(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
308 STATIC int sysctl_rage_vnode(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
309 STATIC int sysctl_kern_check_openevt(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
310 #if DEVELOPMENT || DEBUG
311 STATIC int sysctl_nx(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
312 #endif
313 STATIC int sysctl_loadavg(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
314 STATIC int sysctl_vm_toggle_address_reuse(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
315 STATIC int sysctl_swapusage(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
316 STATIC int fetch_process_cputype( proc_t cur_proc, int *name, u_int namelen, cpu_type_t *cputype);
317 STATIC int sysctl_sysctl_native(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
318 STATIC int sysctl_sysctl_cputype(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
319 STATIC int sysctl_safeboot(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
320 STATIC int sysctl_singleuser(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
321 STATIC int sysctl_minimalboot(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
322 STATIC int sysctl_slide(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
323 
324 #ifdef CONFIG_XNUPOST
325 #include <tests/xnupost.h>
326 
327 STATIC int sysctl_debug_test_oslog_ctl(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
328 STATIC int sysctl_debug_test_stackshot_mutex_owner(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
329 STATIC int sysctl_debug_test_stackshot_rwlck_owner(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
330 #endif
331 
332 extern void IORegistrySetOSBuildVersion(char * build_version);
333 
334 STATIC void
fill_loadavg64(struct loadavg * la,struct user64_loadavg * la64)335 fill_loadavg64(struct loadavg *la, struct user64_loadavg *la64)
336 {
337 	la64->ldavg[0]  = la->ldavg[0];
338 	la64->ldavg[1]  = la->ldavg[1];
339 	la64->ldavg[2]  = la->ldavg[2];
340 	la64->fscale    = (user64_long_t)la->fscale;
341 }
342 
343 STATIC void
fill_loadavg32(struct loadavg * la,struct user32_loadavg * la32)344 fill_loadavg32(struct loadavg *la, struct user32_loadavg *la32)
345 {
346 	la32->ldavg[0]  = la->ldavg[0];
347 	la32->ldavg[1]  = la->ldavg[1];
348 	la32->ldavg[2]  = la->ldavg[2];
349 	la32->fscale    = (user32_long_t)la->fscale;
350 }
351 
352 #if CONFIG_COREDUMP
353 /*
354  * Attributes stored in the kernel.
355  */
356 extern char corefilename[MAXPATHLEN + 1];
357 extern int do_coredump;
358 extern int sugid_coredump;
359 #endif
360 
361 #if COUNT_SYSCALLS
362 extern int do_count_syscalls;
363 #endif
364 
365 #ifdef INSECURE
366 int securelevel = -1;
367 #else
368 int securelevel;
369 #endif
370 
371 STATIC int
sysctl_handle_kern_threadname(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)372 sysctl_handle_kern_threadname(  __unused struct sysctl_oid *oidp, __unused void *arg1,
373     __unused int arg2, struct sysctl_req *req)
374 {
375 	int error;
376 	struct uthread *ut = current_uthread();
377 	user_addr_t oldp = 0, newp = 0;
378 	size_t *oldlenp = NULL;
379 	size_t newlen = 0;
380 
381 	oldp = req->oldptr;
382 	oldlenp = &(req->oldlen);
383 	newp = req->newptr;
384 	newlen = req->newlen;
385 
386 	/* We want the current length, and maybe the string itself */
387 	if (oldlenp) {
388 		/* if we have no thread name yet tell'em we want MAXTHREADNAMESIZE - 1 */
389 		size_t currlen = MAXTHREADNAMESIZE - 1;
390 
391 		if (ut->pth_name) {
392 			/* use length of current thread name */
393 			currlen = strlen(ut->pth_name);
394 		}
395 		if (oldp) {
396 			if (*oldlenp < currlen) {
397 				return ENOMEM;
398 			}
399 			/* NOTE - we do not copy the NULL terminator */
400 			if (ut->pth_name) {
401 				error = copyout(ut->pth_name, oldp, currlen);
402 				if (error) {
403 					return error;
404 				}
405 			}
406 		}
407 		/* return length of thread name minus NULL terminator (just like strlen)  */
408 		req->oldidx = currlen;
409 	}
410 
411 	/* We want to set the name to something */
412 	if (newp) {
413 		if (newlen > (MAXTHREADNAMESIZE - 1)) {
414 			return ENAMETOOLONG;
415 		}
416 		if (!ut->pth_name) {
417 			char *tmp_pth_name = (char *)kalloc_data(MAXTHREADNAMESIZE,
418 			    Z_WAITOK | Z_ZERO);
419 			if (!tmp_pth_name) {
420 				return ENOMEM;
421 			}
422 			if (!OSCompareAndSwapPtr(NULL, tmp_pth_name, &ut->pth_name)) {
423 				kfree_data(tmp_pth_name, MAXTHREADNAMESIZE);
424 				return EBUSY;
425 			}
426 		} else {
427 			kernel_debug_string_simple(TRACE_STRING_THREADNAME_PREV, ut->pth_name);
428 			bzero(ut->pth_name, MAXTHREADNAMESIZE);
429 		}
430 		error = copyin(newp, ut->pth_name, newlen);
431 		if (error) {
432 			return error;
433 		}
434 
435 		kernel_debug_string_simple(TRACE_STRING_THREADNAME, ut->pth_name);
436 	}
437 
438 	return 0;
439 }
440 
441 SYSCTL_PROC(_kern, KERN_THREADNAME, threadname, CTLFLAG_ANYBODY | CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_LOCKED, 0, 0, sysctl_handle_kern_threadname, "A", "");
442 
443 #define BSD_HOST 1
444 STATIC int
sysctl_sched_stats(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)445 sysctl_sched_stats(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
446 {
447 	host_basic_info_data_t hinfo;
448 	kern_return_t kret;
449 	uint32_t size;
450 	uint32_t buf_size = 0;
451 	int changed;
452 	mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
453 	struct _processor_statistics_np *buf;
454 	int error;
455 
456 	kret = host_info((host_t)BSD_HOST, HOST_BASIC_INFO, (host_info_t)&hinfo, &count);
457 	if (kret != KERN_SUCCESS) {
458 		return EINVAL;
459 	}
460 
461 	size = sizeof(struct _processor_statistics_np) * (hinfo.logical_cpu_max + 2); /* One for RT Queue, One for Fair Share Queue */
462 
463 	if (req->oldlen < size) {
464 		return EINVAL;
465 	}
466 
467 	buf_size = size;
468 	buf = (struct _processor_statistics_np *)kalloc_data(buf_size, Z_ZERO | Z_WAITOK);
469 
470 	kret = get_sched_statistics(buf, &size);
471 	if (kret != KERN_SUCCESS) {
472 		error = EINVAL;
473 		goto out;
474 	}
475 
476 	error = sysctl_io_opaque(req, buf, size, &changed);
477 	if (error) {
478 		goto out;
479 	}
480 
481 	if (changed) {
482 		panic("Sched info changed?!");
483 	}
484 out:
485 	kfree_data(buf, buf_size);
486 	return error;
487 }
488 
489 SYSCTL_PROC(_kern, OID_AUTO, sched_stats, CTLFLAG_LOCKED, 0, 0, sysctl_sched_stats, "-", "");
490 
491 STATIC int
sysctl_sched_stats_enable(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,__unused struct sysctl_req * req)492 sysctl_sched_stats_enable(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, __unused struct sysctl_req *req)
493 {
494 	boolean_t active;
495 	int res;
496 
497 	if (req->newlen != sizeof(active)) {
498 		return EINVAL;
499 	}
500 
501 	res = copyin(req->newptr, &active, sizeof(active));
502 	if (res != 0) {
503 		return res;
504 	}
505 
506 	return set_sched_stats_active(active);
507 }
508 
509 SYSCTL_PROC(_kern, OID_AUTO, sched_stats_enable, CTLFLAG_LOCKED | CTLFLAG_WR, 0, 0, sysctl_sched_stats_enable, "-", "");
510 
511 extern uint32_t sched_debug_flags;
512 SYSCTL_INT(_debug, OID_AUTO, sched, CTLFLAG_RW | CTLFLAG_LOCKED, &sched_debug_flags, 0, "scheduler debug");
513 
514 #if (DEBUG || DEVELOPMENT)
515 extern boolean_t doprnt_hide_pointers;
516 SYSCTL_INT(_debug, OID_AUTO, hide_kernel_pointers, CTLFLAG_RW | CTLFLAG_LOCKED, &doprnt_hide_pointers, 0, "hide kernel pointers from log");
517 #endif
518 
519 
520 extern int get_kernel_symfile(proc_t, char **);
521 
522 #if COUNT_SYSCALLS
523 #define KERN_COUNT_SYSCALLS (KERN_OSTYPE + 1000)
524 
525 extern const unsigned int     nsysent;
526 extern int syscalls_log[];
527 extern const char *syscallnames[];
528 
529 STATIC int
530 sysctl_docountsyscalls SYSCTL_HANDLER_ARGS
531 {
532 	__unused int cmd = oidp->oid_arg2;      /* subcommand*/
533 	__unused int *name = arg1;      /* oid element argument vector */
534 	__unused int namelen = arg2;    /* number of oid element arguments */
535 	int error, changed;
536 
537 	int tmp;
538 
539 	/* valid values passed in:
540 	 * = 0 means don't keep called counts for each bsd syscall
541 	 * > 0 means keep called counts for each bsd syscall
542 	 * = 2 means dump current counts to the system log
543 	 * = 3 means reset all counts
544 	 * for example, to dump current counts:
545 	 *		sysctl -w kern.count_calls=2
546 	 */
547 	error = sysctl_io_number(req, do_count_syscalls,
548 	    sizeof(do_count_syscalls), &tmp, &changed);
549 
550 	if (error != 0 || !changed) {
551 		return error;
552 	}
553 
554 	if (tmp == 1) {
555 		do_count_syscalls = 1;
556 	} else if (tmp == 0 || tmp == 2 || tmp == 3) {
557 		for (int i = 0; i < nsysent; i++) {
558 			if (syscalls_log[i] != 0) {
559 				if (tmp == 2) {
560 					printf("%d calls - name %s \n", syscalls_log[i], syscallnames[i]);
561 				} else {
562 					syscalls_log[i] = 0;
563 				}
564 			}
565 		}
566 		do_count_syscalls = (tmp != 0);
567 	}
568 
569 	return error;
570 }
571 SYSCTL_PROC(_kern, KERN_COUNT_SYSCALLS, count_syscalls, CTLTYPE_NODE | CTLFLAG_RD | CTLFLAG_LOCKED,
572     0,                          /* Pointer argument (arg1) */
573     0,                          /* Integer argument (arg2) */
574     sysctl_docountsyscalls,     /* Handler function */
575     NULL,                       /* Data pointer */
576     "");
577 #endif  /* COUNT_SYSCALLS */
578 
579 /*
580  * The following sysctl_* functions should not be used
581  * any more, as they can only cope with callers in
582  * user mode: Use new-style
583  *  sysctl_io_number()
584  *  sysctl_io_string()
585  *  sysctl_io_opaque()
586  * instead.
587  */
588 
589 STATIC int
sysdoproc_filt_KERN_PROC_PID(proc_t p,void * arg)590 sysdoproc_filt_KERN_PROC_PID(proc_t p, void * arg)
591 {
592 	if (proc_getpid(p) != (pid_t)*(int*)arg) {
593 		return 0;
594 	} else {
595 		return 1;
596 	}
597 }
598 
599 STATIC int
sysdoproc_filt_KERN_PROC_PGRP(proc_t p,void * arg)600 sysdoproc_filt_KERN_PROC_PGRP(proc_t p, void * arg)
601 {
602 	if (p->p_pgrpid != (pid_t)*(int*)arg) {
603 		return 0;
604 	} else {
605 		return 1;
606 	}
607 }
608 
609 STATIC int
sysdoproc_filt_KERN_PROC_TTY(proc_t p,void * arg)610 sysdoproc_filt_KERN_PROC_TTY(proc_t p, void * arg)
611 {
612 	struct pgrp *pg;
613 	dev_t dev = NODEV;
614 
615 	if ((p->p_flag & P_CONTROLT) && (pg = proc_pgrp(p, NULL)) != PGRP_NULL) {
616 		dev = os_atomic_load(&pg->pg_session->s_ttydev, relaxed);
617 		pgrp_rele(pg);
618 	}
619 
620 	return dev != NODEV && dev == (dev_t)*(int *)arg;
621 }
622 
623 STATIC int
sysdoproc_filt_KERN_PROC_UID(proc_t p,void * arg)624 sysdoproc_filt_KERN_PROC_UID(proc_t p, void * arg)
625 {
626 	kauth_cred_t my_cred;
627 	uid_t uid;
628 
629 	if (proc_ucred(p) == NULL) {
630 		return 0;
631 	}
632 	my_cred = kauth_cred_proc_ref(p);
633 	uid = kauth_cred_getuid(my_cred);
634 	kauth_cred_unref(&my_cred);
635 
636 	if (uid != (uid_t)*(int*)arg) {
637 		return 0;
638 	} else {
639 		return 1;
640 	}
641 }
642 
643 
644 STATIC int
sysdoproc_filt_KERN_PROC_RUID(proc_t p,void * arg)645 sysdoproc_filt_KERN_PROC_RUID(proc_t p, void * arg)
646 {
647 	kauth_cred_t my_cred;
648 	uid_t ruid;
649 
650 	if (proc_ucred(p) == NULL) {
651 		return 0;
652 	}
653 	my_cred = kauth_cred_proc_ref(p);
654 	ruid = kauth_cred_getruid(my_cred);
655 	kauth_cred_unref(&my_cred);
656 
657 	if (ruid != (uid_t)*(int*)arg) {
658 		return 0;
659 	} else {
660 		return 1;
661 	}
662 }
663 
664 /*
665  * try over estimating by 5 procs
666  */
667 #define KERN_PROCSLOP (5 * sizeof(struct kinfo_proc))
668 struct sysdoproc_args {
669 	size_t buflen;
670 	void *kprocp;
671 	boolean_t is_64_bit;
672 	user_addr_t dp;
673 	size_t needed;
674 	unsigned int sizeof_kproc;
675 	int *errorp;
676 	int uidcheck;
677 	int ruidcheck;
678 	int ttycheck;
679 	int uidval;
680 };
681 
682 int
sysdoproc_callback(proc_t p,void * arg)683 sysdoproc_callback(proc_t p, void *arg)
684 {
685 	struct sysdoproc_args *args = arg;
686 
687 	if (args->buflen >= args->sizeof_kproc) {
688 		if ((args->ruidcheck != 0) && (sysdoproc_filt_KERN_PROC_RUID(p, &args->uidval) == 0)) {
689 			return PROC_RETURNED;
690 		}
691 		if ((args->uidcheck != 0) && (sysdoproc_filt_KERN_PROC_UID(p, &args->uidval) == 0)) {
692 			return PROC_RETURNED;
693 		}
694 		if ((args->ttycheck != 0) && (sysdoproc_filt_KERN_PROC_TTY(p, &args->uidval) == 0)) {
695 			return PROC_RETURNED;
696 		}
697 
698 		bzero(args->kprocp, args->sizeof_kproc);
699 		if (args->is_64_bit) {
700 			fill_user64_proc(p, args->kprocp);
701 		} else {
702 			fill_user32_proc(p, args->kprocp);
703 		}
704 		int error = copyout(args->kprocp, args->dp, args->sizeof_kproc);
705 		if (error) {
706 			*args->errorp = error;
707 			return PROC_RETURNED_DONE;
708 		}
709 		args->dp += args->sizeof_kproc;
710 		args->buflen -= args->sizeof_kproc;
711 	}
712 	args->needed += args->sizeof_kproc;
713 	return PROC_RETURNED;
714 }
715 
716 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD | CTLFLAG_LOCKED, 0, "");
717 STATIC int
718 sysctl_prochandle SYSCTL_HANDLER_ARGS
719 {
720 	int cmd = oidp->oid_arg2;       /* subcommand for multiple nodes */
721 	int *name = arg1;               /* oid element argument vector */
722 	int namelen = arg2;             /* number of oid element arguments */
723 	user_addr_t where = req->oldptr;/* user buffer copy out address */
724 
725 	user_addr_t dp = where;
726 	size_t needed = 0;
727 	size_t buflen = where != USER_ADDR_NULL ? req->oldlen : 0;
728 	int error = 0;
729 	boolean_t is_64_bit = proc_is64bit(current_proc());
730 	struct user32_kinfo_proc  user32_kproc;
731 	struct user64_kinfo_proc  user_kproc;
732 	int sizeof_kproc;
733 	void *kprocp;
734 	int (*filterfn)(proc_t, void *) = 0;
735 	struct sysdoproc_args args;
736 	int uidcheck = 0;
737 	int ruidcheck = 0;
738 	int ttycheck = 0;
739 
740 	if (namelen != 1 && !(namelen == 0 && cmd == KERN_PROC_ALL)) {
741 		return EINVAL;
742 	}
743 
744 	if (is_64_bit) {
745 		sizeof_kproc = sizeof(user_kproc);
746 		kprocp = &user_kproc;
747 	} else {
748 		sizeof_kproc = sizeof(user32_kproc);
749 		kprocp = &user32_kproc;
750 	}
751 
752 	switch (cmd) {
753 	case KERN_PROC_PID:
754 		filterfn = sysdoproc_filt_KERN_PROC_PID;
755 		break;
756 
757 	case KERN_PROC_PGRP:
758 		filterfn = sysdoproc_filt_KERN_PROC_PGRP;
759 		break;
760 
761 	case KERN_PROC_TTY:
762 		ttycheck = 1;
763 		break;
764 
765 	case KERN_PROC_UID:
766 		uidcheck = 1;
767 		break;
768 
769 	case KERN_PROC_RUID:
770 		ruidcheck = 1;
771 		break;
772 
773 	case KERN_PROC_ALL:
774 		break;
775 
776 	default:
777 		/* must be kern.proc.<unknown> */
778 		return ENOTSUP;
779 	}
780 
781 	error = 0;
782 	args.buflen = buflen;
783 	args.kprocp = kprocp;
784 	args.is_64_bit = is_64_bit;
785 	args.dp = dp;
786 	args.needed = needed;
787 	args.errorp = &error;
788 	args.uidcheck = uidcheck;
789 	args.ruidcheck = ruidcheck;
790 	args.ttycheck = ttycheck;
791 	args.sizeof_kproc = sizeof_kproc;
792 	if (namelen) {
793 		args.uidval = name[0];
794 	}
795 
796 	proc_iterate((PROC_ALLPROCLIST | PROC_ZOMBPROCLIST),
797 	    sysdoproc_callback, &args, filterfn, name);
798 
799 	if (error) {
800 		return error;
801 	}
802 
803 	dp = args.dp;
804 	needed = args.needed;
805 
806 	if (where != USER_ADDR_NULL) {
807 		req->oldlen = dp - where;
808 		if (needed > req->oldlen) {
809 			return ENOMEM;
810 		}
811 	} else {
812 		needed += KERN_PROCSLOP;
813 		req->oldlen = needed;
814 	}
815 	/* adjust index so we return the right required/consumed amount */
816 	req->oldidx += req->oldlen;
817 	return 0;
818 }
819 
820 
821 /*
822  * We specify the subcommand code for multiple nodes as the 'req->arg2' value
823  * in the sysctl declaration itself, which comes into the handler function
824  * as 'oidp->oid_arg2'.
825  *
826  * For these particular sysctls, since they have well known OIDs, we could
827  * have just obtained it from the '((int *)arg1)[0]' parameter, but that would
828  * not demonstrate how to handle multiple sysctls that used OID_AUTO instead
829  * of a well known value with a common handler function.  This is desirable,
830  * because we want well known values to "go away" at some future date.
831  *
832  * It should be noted that the value of '((int *)arg1)[1]' is used for many
833  * an integer parameter to the subcommand for many of these sysctls; we'd
834  * rather have used '((int *)arg1)[0]' for that, or even better, an element
835  * in a structure passed in as the the 'newp' argument to sysctlbyname(3),
836  * and then use leaf-node permissions enforcement, but that would have
837  * necessitated modifying user space code to correspond to the interface
838  * change, and we are striving for binary backward compatibility here; even
839  * though these are SPI, and not intended for use by user space applications
840  * which are not themselves system tools or libraries, some applications
841  * have erroneously used them.
842  */
843 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLTYPE_NODE | CTLFLAG_RD | CTLFLAG_LOCKED,
844     0,                          /* Pointer argument (arg1) */
845     KERN_PROC_ALL,              /* Integer argument (arg2) */
846     sysctl_prochandle,          /* Handler function */
847     NULL,                       /* Data is size variant on ILP32/LP64 */
848     "");
849 SYSCTL_PROC(_kern_proc, KERN_PROC_PID, pid, CTLTYPE_NODE | CTLFLAG_RD | CTLFLAG_LOCKED,
850     0,                          /* Pointer argument (arg1) */
851     KERN_PROC_PID,              /* Integer argument (arg2) */
852     sysctl_prochandle,          /* Handler function */
853     NULL,                       /* Data is size variant on ILP32/LP64 */
854     "");
855 SYSCTL_PROC(_kern_proc, KERN_PROC_TTY, tty, CTLTYPE_NODE | CTLFLAG_RD | CTLFLAG_LOCKED,
856     0,                          /* Pointer argument (arg1) */
857     KERN_PROC_TTY,              /* Integer argument (arg2) */
858     sysctl_prochandle,          /* Handler function */
859     NULL,                       /* Data is size variant on ILP32/LP64 */
860     "");
861 SYSCTL_PROC(_kern_proc, KERN_PROC_PGRP, pgrp, CTLTYPE_NODE | CTLFLAG_RD | CTLFLAG_LOCKED,
862     0,                          /* Pointer argument (arg1) */
863     KERN_PROC_PGRP,             /* Integer argument (arg2) */
864     sysctl_prochandle,          /* Handler function */
865     NULL,                       /* Data is size variant on ILP32/LP64 */
866     "");
867 SYSCTL_PROC(_kern_proc, KERN_PROC_UID, uid, CTLTYPE_NODE | CTLFLAG_RD | CTLFLAG_LOCKED,
868     0,                          /* Pointer argument (arg1) */
869     KERN_PROC_UID,              /* Integer argument (arg2) */
870     sysctl_prochandle,          /* Handler function */
871     NULL,                       /* Data is size variant on ILP32/LP64 */
872     "");
873 SYSCTL_PROC(_kern_proc, KERN_PROC_RUID, ruid, CTLTYPE_NODE | CTLFLAG_RD | CTLFLAG_LOCKED,
874     0,                          /* Pointer argument (arg1) */
875     KERN_PROC_RUID,             /* Integer argument (arg2) */
876     sysctl_prochandle,          /* Handler function */
877     NULL,                       /* Data is size variant on ILP32/LP64 */
878     "");
879 SYSCTL_PROC(_kern_proc, KERN_PROC_LCID, lcid, CTLTYPE_NODE | CTLFLAG_RD | CTLFLAG_LOCKED,
880     0,                          /* Pointer argument (arg1) */
881     KERN_PROC_LCID,             /* Integer argument (arg2) */
882     sysctl_prochandle,          /* Handler function */
883     NULL,                       /* Data is size variant on ILP32/LP64 */
884     "");
885 
886 
887 /*
888  * Fill in non-zero fields of an eproc structure for the specified process.
889  */
890 STATIC void
fill_user32_eproc(proc_t p,struct user32_eproc * __restrict ep)891 fill_user32_eproc(proc_t p, struct user32_eproc *__restrict ep)
892 {
893 	struct pgrp *pg;
894 	struct session *sessp;
895 	kauth_cred_t my_cred;
896 
897 	pg = proc_pgrp(p, &sessp);
898 
899 	if (pg != PGRP_NULL) {
900 		ep->e_pgid = p->p_pgrpid;
901 		ep->e_jobc = pg->pg_jobc;
902 		if (sessp->s_ttyvp) {
903 			ep->e_flag = EPROC_CTTY;
904 		}
905 	}
906 
907 	ep->e_ppid = p->p_ppid;
908 	if (proc_ucred(p)) {
909 		my_cred = kauth_cred_proc_ref(p);
910 
911 		/* A fake historical pcred */
912 		ep->e_pcred.p_ruid = kauth_cred_getruid(my_cred);
913 		ep->e_pcred.p_svuid = kauth_cred_getsvuid(my_cred);
914 		ep->e_pcred.p_rgid = kauth_cred_getrgid(my_cred);
915 		ep->e_pcred.p_svgid = kauth_cred_getsvgid(my_cred);
916 
917 		/* A fake historical *kauth_cred_t */
918 		unsigned long refcnt = os_atomic_load(&my_cred->cr_ref, relaxed);
919 		ep->e_ucred.cr_ref = (uint32_t)MIN(refcnt, UINT32_MAX);
920 		ep->e_ucred.cr_uid = kauth_cred_getuid(my_cred);
921 		ep->e_ucred.cr_ngroups = (short)posix_cred_get(my_cred)->cr_ngroups;
922 		bcopy(posix_cred_get(my_cred)->cr_groups,
923 		    ep->e_ucred.cr_groups, NGROUPS * sizeof(gid_t));
924 
925 		kauth_cred_unref(&my_cred);
926 	}
927 
928 	ep->e_tdev = NODEV;
929 	if (pg != PGRP_NULL) {
930 		if (p->p_flag & P_CONTROLT) {
931 			session_lock(sessp);
932 			ep->e_tdev = os_atomic_load(&sessp->s_ttydev, relaxed);
933 			ep->e_tpgid = sessp->s_ttypgrpid;
934 			session_unlock(sessp);
935 		}
936 		if (SESS_LEADER(p, sessp)) {
937 			ep->e_flag |= EPROC_SLEADER;
938 		}
939 		pgrp_rele(pg);
940 	}
941 }
942 
943 /*
944  * Fill in non-zero fields of an LP64 eproc structure for the specified process.
945  */
946 STATIC void
fill_user64_eproc(proc_t p,struct user64_eproc * __restrict ep)947 fill_user64_eproc(proc_t p, struct user64_eproc *__restrict ep)
948 {
949 	struct pgrp *pg;
950 	struct session *sessp;
951 	kauth_cred_t my_cred;
952 
953 	pg = proc_pgrp(p, &sessp);
954 
955 	if (pg != PGRP_NULL) {
956 		ep->e_pgid = p->p_pgrpid;
957 		ep->e_jobc = pg->pg_jobc;
958 		if (sessp->s_ttyvp) {
959 			ep->e_flag = EPROC_CTTY;
960 		}
961 	}
962 
963 	ep->e_ppid = p->p_ppid;
964 	if (proc_ucred(p)) {
965 		my_cred = kauth_cred_proc_ref(p);
966 
967 		/* A fake historical pcred */
968 		ep->e_pcred.p_ruid = kauth_cred_getruid(my_cred);
969 		ep->e_pcred.p_svuid = kauth_cred_getsvuid(my_cred);
970 		ep->e_pcred.p_rgid = kauth_cred_getrgid(my_cred);
971 		ep->e_pcred.p_svgid = kauth_cred_getsvgid(my_cred);
972 
973 		/* A fake historical *kauth_cred_t */
974 		unsigned long refcnt = os_atomic_load(&my_cred->cr_ref, relaxed);
975 		ep->e_ucred.cr_ref = (uint32_t)MIN(refcnt, UINT32_MAX);
976 		ep->e_ucred.cr_uid = kauth_cred_getuid(my_cred);
977 		ep->e_ucred.cr_ngroups = (short)posix_cred_get(my_cred)->cr_ngroups;
978 		bcopy(posix_cred_get(my_cred)->cr_groups,
979 		    ep->e_ucred.cr_groups, NGROUPS * sizeof(gid_t));
980 
981 		kauth_cred_unref(&my_cred);
982 	}
983 
984 	ep->e_tdev = NODEV;
985 	if (pg != PGRP_NULL) {
986 		if (p->p_flag & P_CONTROLT) {
987 			session_lock(sessp);
988 			ep->e_tdev = os_atomic_load(&sessp->s_ttydev, relaxed);
989 			ep->e_tpgid = sessp->s_ttypgrpid;
990 			session_unlock(sessp);
991 		}
992 		if (SESS_LEADER(p, sessp)) {
993 			ep->e_flag |= EPROC_SLEADER;
994 		}
995 		pgrp_rele(pg);
996 	}
997 }
998 
999 /*
1000  * Fill in an eproc structure for the specified process.
1001  * bzeroed by our caller, so only set non-zero fields.
1002  */
1003 STATIC void
fill_user32_externproc(proc_t p,struct user32_extern_proc * __restrict exp)1004 fill_user32_externproc(proc_t p, struct user32_extern_proc *__restrict exp)
1005 {
1006 	exp->p_starttime.tv_sec = (user32_time_t)p->p_start.tv_sec;
1007 	exp->p_starttime.tv_usec = p->p_start.tv_usec;
1008 	exp->p_flag = p->p_flag;
1009 	if (p->p_lflag & P_LTRACED) {
1010 		exp->p_flag |= P_TRACED;
1011 	}
1012 	if (p->p_lflag & P_LPPWAIT) {
1013 		exp->p_flag |= P_PPWAIT;
1014 	}
1015 	if (p->p_lflag & P_LEXIT) {
1016 		exp->p_flag |= P_WEXIT;
1017 	}
1018 	exp->p_stat = p->p_stat;
1019 	exp->p_pid = proc_getpid(p);
1020 	exp->p_oppid = p->p_oppid;
1021 	/* Mach related  */
1022 	exp->p_debugger = p->p_debugger;
1023 	exp->sigwait = p->sigwait;
1024 	/* scheduling */
1025 #ifdef _PROC_HAS_SCHEDINFO_
1026 	exp->p_estcpu = p->p_estcpu;
1027 	exp->p_pctcpu = p->p_pctcpu;
1028 	exp->p_slptime = p->p_slptime;
1029 #endif
1030 	exp->p_realtimer.it_interval.tv_sec =
1031 	    (user32_time_t)p->p_realtimer.it_interval.tv_sec;
1032 	exp->p_realtimer.it_interval.tv_usec =
1033 	    (__int32_t)p->p_realtimer.it_interval.tv_usec;
1034 
1035 	exp->p_realtimer.it_value.tv_sec =
1036 	    (user32_time_t)p->p_realtimer.it_value.tv_sec;
1037 	exp->p_realtimer.it_value.tv_usec =
1038 	    (__int32_t)p->p_realtimer.it_value.tv_usec;
1039 
1040 	exp->p_rtime.tv_sec = (user32_time_t)p->p_rtime.tv_sec;
1041 	exp->p_rtime.tv_usec = (__int32_t)p->p_rtime.tv_usec;
1042 
1043 	exp->p_sigignore = p->p_sigignore;
1044 	exp->p_sigcatch = p->p_sigcatch;
1045 	exp->p_priority = p->p_priority;
1046 	exp->p_nice = p->p_nice;
1047 	bcopy(&p->p_comm, &exp->p_comm, MAXCOMLEN);
1048 	exp->p_xstat = (u_short)MIN(p->p_xstat, USHRT_MAX);
1049 	exp->p_acflag = p->p_acflag;
1050 }
1051 
1052 /*
1053  * Fill in an LP64 version of extern_proc structure for the specified process.
1054  */
1055 STATIC void
fill_user64_externproc(proc_t p,struct user64_extern_proc * __restrict exp)1056 fill_user64_externproc(proc_t p, struct user64_extern_proc *__restrict exp)
1057 {
1058 	exp->p_starttime.tv_sec = p->p_start.tv_sec;
1059 	exp->p_starttime.tv_usec = p->p_start.tv_usec;
1060 	exp->p_flag = p->p_flag;
1061 	if (p->p_lflag & P_LTRACED) {
1062 		exp->p_flag |= P_TRACED;
1063 	}
1064 	if (p->p_lflag & P_LPPWAIT) {
1065 		exp->p_flag |= P_PPWAIT;
1066 	}
1067 	if (p->p_lflag & P_LEXIT) {
1068 		exp->p_flag |= P_WEXIT;
1069 	}
1070 	exp->p_stat = p->p_stat;
1071 	exp->p_pid = proc_getpid(p);
1072 	exp->p_oppid = p->p_oppid;
1073 	/* Mach related  */
1074 	exp->p_debugger = p->p_debugger;
1075 	exp->sigwait = p->sigwait;
1076 	/* scheduling */
1077 #ifdef _PROC_HAS_SCHEDINFO_
1078 	exp->p_estcpu = p->p_estcpu;
1079 	exp->p_pctcpu = p->p_pctcpu;
1080 	exp->p_slptime = p->p_slptime;
1081 #endif
1082 	exp->p_realtimer.it_interval.tv_sec = p->p_realtimer.it_interval.tv_sec;
1083 	exp->p_realtimer.it_interval.tv_usec = p->p_realtimer.it_interval.tv_usec;
1084 
1085 	exp->p_realtimer.it_value.tv_sec = p->p_realtimer.it_value.tv_sec;
1086 	exp->p_realtimer.it_value.tv_usec = p->p_realtimer.it_value.tv_usec;
1087 
1088 	exp->p_rtime.tv_sec = p->p_rtime.tv_sec;
1089 	exp->p_rtime.tv_usec = p->p_rtime.tv_usec;
1090 
1091 	exp->p_sigignore = p->p_sigignore;
1092 	exp->p_sigcatch = p->p_sigcatch;
1093 	exp->p_priority = p->p_priority;
1094 	exp->p_nice = p->p_nice;
1095 	bcopy(&p->p_comm, &exp->p_comm, MAXCOMLEN);
1096 	exp->p_xstat = (u_short)MIN(p->p_xstat, USHRT_MAX);
1097 	exp->p_acflag = p->p_acflag;
1098 }
1099 
1100 STATIC void
fill_user32_proc(proc_t p,struct user32_kinfo_proc * __restrict kp)1101 fill_user32_proc(proc_t p, struct user32_kinfo_proc *__restrict kp)
1102 {
1103 	/* on a 64 bit kernel, 32 bit users get some truncated information */
1104 	fill_user32_externproc(p, &kp->kp_proc);
1105 	fill_user32_eproc(p, &kp->kp_eproc);
1106 }
1107 
1108 STATIC void
fill_user64_proc(proc_t p,struct user64_kinfo_proc * __restrict kp)1109 fill_user64_proc(proc_t p, struct user64_kinfo_proc *__restrict kp)
1110 {
1111 	fill_user64_externproc(p, &kp->kp_proc);
1112 	fill_user64_eproc(p, &kp->kp_eproc);
1113 }
1114 
1115 STATIC int
1116 sysctl_kdebug_ops SYSCTL_HANDLER_ARGS
1117 {
1118 	__unused int cmd = oidp->oid_arg2;      /* subcommand*/
1119 	int *name = arg1;               /* oid element argument vector */
1120 	int namelen = arg2;             /* number of oid element arguments */
1121 	user_addr_t oldp = req->oldptr; /* user buffer copy out address */
1122 	size_t *oldlenp = &req->oldlen; /* user buffer copy out size */
1123 //	user_addr_t newp = req->newptr;	/* user buffer copy in address */
1124 //	size_t newlen = req->newlen;	/* user buffer copy in size */
1125 
1126 	int ret = 0;
1127 
1128 	if (namelen == 0) {
1129 		return ENOTSUP;
1130 	}
1131 
1132 	switch (name[0]) {
1133 	case KERN_KDEFLAGS:
1134 	case KERN_KDDFLAGS:
1135 	case KERN_KDENABLE:
1136 	case KERN_KDGETBUF:
1137 	case KERN_KDSETUP:
1138 	case KERN_KDREMOVE:
1139 	case KERN_KDSETREG:
1140 	case KERN_KDGETREG:
1141 	case KERN_KDREADTR:
1142 	case KERN_KDWRITETR:
1143 	case KERN_KDWRITEMAP:
1144 	case KERN_KDTEST:
1145 	case KERN_KDPIDTR:
1146 	case KERN_KDTHRMAP:
1147 	case KERN_KDPIDEX:
1148 	case KERN_KDSETBUF:
1149 	case KERN_KDREADCURTHRMAP:
1150 	case KERN_KDSET_TYPEFILTER:
1151 	case KERN_KDBUFWAIT:
1152 	case KERN_KDCPUMAP:
1153 	case KERN_KDCPUMAP_EXT:
1154 	case KERN_KDWRITETR_V3:
1155 		ret = kdbg_control(name, namelen, oldp, oldlenp);
1156 		break;
1157 	default:
1158 		ret = ENOTSUP;
1159 		break;
1160 	}
1161 
1162 	/* adjust index so we return the right required/consumed amount */
1163 	if (!ret) {
1164 		req->oldidx += req->oldlen;
1165 	}
1166 
1167 	return ret;
1168 }
1169 SYSCTL_PROC(_kern, KERN_KDEBUG, kdebug, CTLTYPE_NODE | CTLFLAG_RD | CTLFLAG_LOCKED,
1170     0,                          /* Pointer argument (arg1) */
1171     0,                          /* Integer argument (arg2) */
1172     sysctl_kdebug_ops,          /* Handler function */
1173     NULL,                       /* Data pointer */
1174     "");
1175 
1176 
1177 #if defined(XNU_TARGET_OS_OSX)
1178 /*
1179  * Return the top *sizep bytes of the user stack, or the entire area of the
1180  * user stack down through the saved exec_path, whichever is smaller.
1181  */
1182 STATIC int
1183 sysctl_doprocargs SYSCTL_HANDLER_ARGS
1184 {
1185 	__unused int cmd = oidp->oid_arg2;      /* subcommand*/
1186 	int *name = arg1;               /* oid element argument vector */
1187 	int namelen = arg2;             /* number of oid element arguments */
1188 	user_addr_t oldp = req->oldptr; /* user buffer copy out address */
1189 	size_t *oldlenp = &req->oldlen; /* user buffer copy out size */
1190 //	user_addr_t newp = req->newptr;	/* user buffer copy in address */
1191 //	size_t newlen = req->newlen;	/* user buffer copy in size */
1192 	int error;
1193 
1194 	error =  sysctl_procargsx( name, namelen, oldp, oldlenp, current_proc(), 0);
1195 
1196 	/* adjust index so we return the right required/consumed amount */
1197 	if (!error) {
1198 		req->oldidx += req->oldlen;
1199 	}
1200 
1201 	return error;
1202 }
1203 SYSCTL_PROC(_kern, KERN_PROCARGS, procargs, CTLTYPE_NODE | CTLFLAG_RD | CTLFLAG_LOCKED,
1204     0,                          /* Pointer argument (arg1) */
1205     0,                          /* Integer argument (arg2) */
1206     sysctl_doprocargs,          /* Handler function */
1207     NULL,                       /* Data pointer */
1208     "");
1209 #endif  /* defined(XNU_TARGET_OS_OSX) */
1210 
1211 STATIC int
1212 sysctl_doprocargs2 SYSCTL_HANDLER_ARGS
1213 {
1214 	__unused int cmd = oidp->oid_arg2;      /* subcommand*/
1215 	int *name = arg1;               /* oid element argument vector */
1216 	int namelen = arg2;             /* number of oid element arguments */
1217 	user_addr_t oldp = req->oldptr; /* user buffer copy out address */
1218 	size_t *oldlenp = &req->oldlen; /* user buffer copy out size */
1219 //	user_addr_t newp = req->newptr;	/* user buffer copy in address */
1220 //	size_t newlen = req->newlen;	/* user buffer copy in size */
1221 	int error;
1222 
1223 	error = sysctl_procargsx( name, namelen, oldp, oldlenp, current_proc(), 1);
1224 
1225 	/* adjust index so we return the right required/consumed amount */
1226 	if (!error) {
1227 		req->oldidx += req->oldlen;
1228 	}
1229 
1230 	return error;
1231 }
1232 SYSCTL_PROC(_kern, KERN_PROCARGS2, procargs2, CTLTYPE_NODE | CTLFLAG_RD | CTLFLAG_LOCKED,
1233     0,                          /* Pointer argument (arg1) */
1234     0,                          /* Integer argument (arg2) */
1235     sysctl_doprocargs2,         /* Handler function */
1236     NULL,                       /* Data pointer */
1237     "");
1238 
1239 #define SYSCTL_PROCARGS_READ_ENVVARS_ENTITLEMENT "com.apple.private.read-environment-variables"
1240 STATIC int
sysctl_procargsx(int * name,u_int namelen,user_addr_t where,size_t * sizep,proc_t cur_proc,int argc_yes)1241 sysctl_procargsx(int *name, u_int namelen, user_addr_t where,
1242     size_t *sizep, proc_t cur_proc, int argc_yes)
1243 {
1244 	assert(sizep != NULL);
1245 	proc_t p = NULL;
1246 	size_t buflen = where != USER_ADDR_NULL ? *sizep : 0;
1247 	int error = 0;
1248 	struct _vm_map *proc_map = NULL;
1249 	struct task * task;
1250 	vm_map_copy_t   tmp = NULL;
1251 	user_addr_t     arg_addr;
1252 	size_t          arg_size;
1253 	caddr_t data;
1254 	size_t argslen = 0;
1255 	size_t size = 0;
1256 	vm_offset_t     copy_start = 0, copy_end;
1257 	vm_offset_t     smallbuffer_start;
1258 	kern_return_t ret;
1259 	int pid;
1260 	kauth_cred_t my_cred;
1261 	uid_t uid;
1262 	int argc = -1;
1263 	size_t argvsize;
1264 	size_t remaining;
1265 	size_t current_arg_index;
1266 	size_t current_arg_len;
1267 	const char * current_arg;
1268 	bool omit_env_vars = true;
1269 	user_addr_t user_stack;
1270 	vm_map_offset_t effective_page_mask;
1271 
1272 	if (namelen < 1) {
1273 		error = EINVAL;
1274 		goto finish;
1275 	}
1276 
1277 	if (argc_yes) {
1278 		buflen -= sizeof(int);          /* reserve first word to return argc */
1279 	}
1280 	/* we only care about buflen when where (oldp from sysctl) is not NULL. */
1281 	/* when where (oldp from sysctl) is NULL and sizep (oldlenp from sysctl */
1282 	/* is not NULL then the caller wants us to return the length needed to */
1283 	/* hold the data we would return */
1284 	if (where != USER_ADDR_NULL && (buflen <= 0 || buflen > ARG_MAX)) {
1285 		error = EINVAL;
1286 		goto finish;
1287 	}
1288 
1289 	/*
1290 	 *	Lookup process by pid
1291 	 */
1292 	pid = name[0];
1293 	p = proc_find(pid);
1294 	if (p == NULL) {
1295 		error = EINVAL;
1296 		goto finish;
1297 	}
1298 
1299 	/* Allow reading environment variables if any of the following are true:
1300 	 * - kernel is DEVELOPMENT || DEBUG
1301 	 * - target process is same as current_proc()
1302 	 * - target process is not cs_restricted
1303 	 * - SIP is off
1304 	 * - caller has an entitlement
1305 	 */
1306 
1307 #if DEVELOPMENT || DEBUG
1308 	omit_env_vars = false;
1309 #endif
1310 	if (p == current_proc() ||
1311 	    !cs_restricted(p) ||
1312 #if CONFIG_CSR
1313 	    csr_check(CSR_ALLOW_UNRESTRICTED_DTRACE) == 0 ||
1314 #endif
1315 	    IOCurrentTaskHasEntitlement(SYSCTL_PROCARGS_READ_ENVVARS_ENTITLEMENT)
1316 	    ) {
1317 		omit_env_vars = false;
1318 	}
1319 
1320 	/*
1321 	 *	Copy the top N bytes of the stack.
1322 	 *	On all machines we have so far, the stack grows
1323 	 *	downwards.
1324 	 *
1325 	 *	If the user expects no more than N bytes of
1326 	 *	argument list, use that as a guess for the
1327 	 *	size.
1328 	 */
1329 
1330 	if (!p->user_stack) {
1331 		error = EINVAL;
1332 		goto finish;
1333 	}
1334 
1335 	/* save off argc, argslen, user_stack before releasing the proc */
1336 	argc = p->p_argc;
1337 	argslen = p->p_argslen;
1338 	user_stack = p->user_stack;
1339 
1340 	/*
1341 	 * When these sysctls were introduced, the first string in the strings
1342 	 * section was just the bare path of the executable.  However, for security
1343 	 * reasons we now prefix this string with executable_path= so it can be
1344 	 * parsed getenv style.  To avoid binary compatability issues with exising
1345 	 * callers of this sysctl, we strip it off here.
1346 	 * (rdar://problem/13746466)
1347 	 */
1348 #define        EXECUTABLE_KEY "executable_path="
1349 	argslen -= strlen(EXECUTABLE_KEY);
1350 
1351 	if (where == USER_ADDR_NULL && !omit_env_vars) {
1352 		/* caller only wants to know length of proc args data.
1353 		 * If we don't need to omit environment variables, we can skip
1354 		 * copying the target process stack */
1355 		goto calculate_size;
1356 	}
1357 
1358 	my_cred = kauth_cred_proc_ref(p);
1359 	uid = kauth_cred_getuid(my_cred);
1360 	kauth_cred_unref(&my_cred);
1361 
1362 	if ((uid != kauth_cred_getuid(kauth_cred_get()))
1363 	    && suser(kauth_cred_get(), &cur_proc->p_acflag)) {
1364 		error = EINVAL;
1365 		goto finish;
1366 	}
1367 
1368 	/*
1369 	 *	Before we can block (any VM code), make another
1370 	 *	reference to the map to keep it alive.  We do
1371 	 *	that by getting a reference on the task itself.
1372 	 */
1373 	task = p->task;
1374 	if (task == NULL) {
1375 		error = EINVAL;
1376 		goto finish;
1377 	}
1378 
1379 	/*
1380 	 * Once we have a task reference we can convert that into a
1381 	 * map reference, which we will use in the calls below.  The
1382 	 * task/process may change its map after we take this reference
1383 	 * (see execve), but the worst that will happen then is a return
1384 	 * of stale info (which is always a possibility).
1385 	 */
1386 	task_reference(task);
1387 	proc_rele(p);
1388 	p = NULL;
1389 	proc_map = get_task_map_reference(task);
1390 	task_deallocate(task);
1391 
1392 	if (proc_map == NULL) {
1393 		error = EINVAL;
1394 		goto finish;
1395 	}
1396 
1397 	effective_page_mask = vm_map_page_mask(proc_map);
1398 
1399 	arg_size = vm_map_round_page(argslen, effective_page_mask);
1400 
1401 	arg_addr = user_stack - arg_size;
1402 
1403 	ret = kmem_alloc_flags(kernel_map, &copy_start, arg_size, VM_KERN_MEMORY_BSD, KMA_ZERO);
1404 	if (ret != KERN_SUCCESS) {
1405 		error = ENOMEM;
1406 		goto finish;
1407 	}
1408 
1409 	copy_end = copy_start + arg_size;
1410 
1411 	if (vm_map_copyin(proc_map, (vm_map_address_t)arg_addr,
1412 	    (vm_map_size_t)arg_size, FALSE, &tmp) != KERN_SUCCESS) {
1413 		error = EIO;
1414 		goto finish;
1415 	}
1416 
1417 	/*
1418 	 *	Now that we've done the copyin from the process'
1419 	 *	map, we can release the reference to it.
1420 	 */
1421 	vm_map_deallocate(proc_map);
1422 	proc_map = NULL;
1423 
1424 	if (vm_map_copy_overwrite(kernel_map,
1425 	    (vm_map_address_t)copy_start,
1426 	    tmp, (vm_map_size_t) arg_size, FALSE) != KERN_SUCCESS) {
1427 		error = EIO;
1428 		goto finish;
1429 	}
1430 	/* tmp was consumed */
1431 	tmp = NULL;
1432 
1433 	if (omit_env_vars) {
1434 		argvsize = 0;
1435 
1436 		/* Iterate over everything in argv, plus one for the bare executable path */
1437 		for (current_arg_index = 0; current_arg_index < argc + 1 && argvsize < argslen; ++current_arg_index) {
1438 			current_arg = (const char *)(copy_end - argslen) + argvsize;
1439 			remaining = argslen - argvsize;
1440 			current_arg_len = strnlen(current_arg, remaining);
1441 			if (current_arg_len < remaining) {
1442 				/* We have space for the null terminator */
1443 				current_arg_len += 1;
1444 
1445 				if (current_arg_index == 0) {
1446 					/* The bare executable path may have multiple null bytes after it for alignment */
1447 					while (current_arg_len < remaining && current_arg[current_arg_len] == 0) {
1448 						current_arg_len += 1;
1449 					}
1450 				}
1451 			}
1452 			argvsize += current_arg_len;
1453 		}
1454 		assert(argvsize <= argslen);
1455 
1456 		/* Adjust argslen and copy_end to make the copyout range extend to the end of argv */
1457 		copy_end = copy_end - argslen + argvsize;
1458 		argslen = argvsize;
1459 	}
1460 
1461 	if (where == USER_ADDR_NULL) {
1462 		/* Skip copyout */
1463 		goto calculate_size;
1464 	}
1465 
1466 	if (buflen >= argslen) {
1467 		data = (caddr_t) (copy_end - argslen);
1468 		size = argslen;
1469 	} else {
1470 		/*
1471 		 * Before rdar://25397314, this function contained incorrect logic when buflen is less
1472 		 * than argslen. The problem was that it copied in `buflen` bytes from the end of the target
1473 		 * process user stack into the beginning of a buffer of size round_page(buflen), and then
1474 		 * copied out `buflen` bytes from the end of this buffer. The effect of this was that
1475 		 * the caller of this sysctl would get zeros at the end of their buffer.
1476 		 *
1477 		 * To preserve this behavior, bzero everything from copy_end-round_page(buflen)+buflen to the
1478 		 * end of the buffer. This emulates copying in only `buflen` bytes.
1479 		 *
1480 		 *
1481 		 * In the old code:
1482 		 *
1483 		 *   copy_start     .... size: round_page(buflen) ....        copy_end
1484 		 *      [---copied in data (size: buflen)---|--- zeros ----------]
1485 		 *                           ^
1486 		 *                          data = copy_end - buflen
1487 		 *
1488 		 *
1489 		 * In the new code:
1490 		 *   copy_start        .... size: round_page(p->argslen) ....                full copy_end
1491 		 *      ^         ....................... p->argslen ...............................^
1492 		 *      ^         ^                                         truncated copy_end      ^
1493 		 *      ^         ^                                                 ^               ^
1494 		 *      ^         ................  argslen  ........................               ^
1495 		 *      ^         ^                                                 ^               ^
1496 		 *      [-------copied in data (size: round_page(p->argslen))-------:----env vars---]
1497 		 *                                ^            ^
1498 		 *                                ^         data = copy_end - buflen
1499 		 *                smallbuffer_start = max(copy_end - round_page(buflen), copy_start)
1500 		 *
1501 		 *
1502 		 * Full copy_end: copy_end calculated from copy_start + round_page(p->argslen)
1503 		 * Truncated copy_end: copy_end after truncation to remove environment variables.
1504 		 *
1505 		 * If environment variables were omitted, then we use the truncated copy_end, otherwise
1506 		 * we use full copy_end.
1507 		 *
1508 		 * smallbuffer_start: represents where copy_start would be in the old code.
1509 		 * data: The beginning of the region we copyout
1510 		 */
1511 		smallbuffer_start = copy_end - vm_map_round_page(buflen, effective_page_mask);
1512 		if (smallbuffer_start < copy_start) {
1513 			smallbuffer_start = copy_start;
1514 		}
1515 		bzero((void *)(smallbuffer_start + buflen), copy_end - (smallbuffer_start + buflen));
1516 		data = (caddr_t) (copy_end - buflen);
1517 		size = buflen;
1518 	}
1519 
1520 	if (argc_yes) {
1521 		/* Put processes argc as the first word in the copyout buffer */
1522 		suword(where, argc);
1523 		error = copyout(data, (where + sizeof(int)), size);
1524 		size += sizeof(int);
1525 	} else {
1526 		error = copyout(data, where, size);
1527 
1528 		/*
1529 		 * Make the old PROCARGS work to return the executable's path
1530 		 * But, only if there is enough space in the provided buffer
1531 		 *
1532 		 * on entry: data [possibily] points to the beginning of the path
1533 		 *
1534 		 * Note: we keep all pointers&sizes aligned to word boundries
1535 		 */
1536 		if ((!error) && (buflen > 0 && (u_int)buflen > size)) {
1537 			int binPath_sz, alignedBinPath_sz = 0;
1538 			int extraSpaceNeeded, addThis;
1539 			user_addr_t placeHere;
1540 			char * str = (char *) data;
1541 			size_t max_len = size;
1542 
1543 			/* Some apps are really bad about messing up their stacks
1544 			 *  So, we have to be extra careful about getting the length
1545 			 *  of the executing binary.  If we encounter an error, we bail.
1546 			 */
1547 
1548 			/* Limit ourselves to PATH_MAX paths */
1549 			if (max_len > PATH_MAX) {
1550 				max_len = PATH_MAX;
1551 			}
1552 
1553 			binPath_sz = 0;
1554 
1555 			while ((binPath_sz < max_len - 1) && (*str++ != 0)) {
1556 				binPath_sz++;
1557 			}
1558 
1559 			/* If we have a NUL terminator, copy it, too */
1560 			if (binPath_sz < max_len - 1) {
1561 				binPath_sz += 1;
1562 			}
1563 
1564 			/* Pre-Flight the space requiremnts */
1565 
1566 			/* Account for the padding that fills out binPath to the next word */
1567 			alignedBinPath_sz += (binPath_sz & (sizeof(int) - 1)) ? (sizeof(int) - (binPath_sz & (sizeof(int) - 1))) : 0;
1568 
1569 			placeHere = where + size;
1570 
1571 			/* Account for the bytes needed to keep placeHere word aligned */
1572 			addThis = (placeHere & (sizeof(int) - 1)) ? (sizeof(int) - (placeHere & (sizeof(int) - 1))) : 0;
1573 
1574 			/* Add up all the space that is needed */
1575 			extraSpaceNeeded = alignedBinPath_sz + addThis + binPath_sz + (4 * sizeof(int));
1576 
1577 			/* is there is room to tack on argv[0]? */
1578 			if ((buflen & ~(sizeof(int) - 1)) >= (size + extraSpaceNeeded)) {
1579 				placeHere += addThis;
1580 				suword(placeHere, 0);
1581 				placeHere += sizeof(int);
1582 				suword(placeHere, 0xBFFF0000);
1583 				placeHere += sizeof(int);
1584 				suword(placeHere, 0);
1585 				placeHere += sizeof(int);
1586 				error = copyout(data, placeHere, binPath_sz);
1587 				if (!error) {
1588 					placeHere += binPath_sz;
1589 					suword(placeHere, 0);
1590 					size += extraSpaceNeeded;
1591 				}
1592 			}
1593 		}
1594 	}
1595 
1596 calculate_size:
1597 	/* Size has already been calculated for the where != NULL case */
1598 	if (where == USER_ADDR_NULL) {
1599 		size = argslen;
1600 		if (argc_yes) {
1601 			size += sizeof(int);
1602 		} else {
1603 			/*
1604 			 * old PROCARGS will return the executable's path and plus some
1605 			 * extra space for work alignment and data tags
1606 			 */
1607 			size += PATH_MAX + (6 * sizeof(int));
1608 		}
1609 		size += (size & (sizeof(int) - 1)) ? (sizeof(int) - (size & (sizeof(int) - 1))) : 0;
1610 	}
1611 
1612 	*sizep = size;
1613 
1614 finish:
1615 	if (p != NULL) {
1616 		proc_rele(p);
1617 	}
1618 	if (tmp != NULL) {
1619 		vm_map_copy_discard(tmp);
1620 	}
1621 	if (proc_map != NULL) {
1622 		vm_map_deallocate(proc_map);
1623 	}
1624 	if (copy_start != (vm_offset_t) 0) {
1625 		kmem_free(kernel_map, copy_start, arg_size);
1626 	}
1627 	return error;
1628 }
1629 
1630 
1631 /*
1632  * Max number of concurrent aio requests
1633  */
1634 STATIC int
sysctl_aiomax(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)1635 sysctl_aiomax
1636 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1637 {
1638 	int new_value, changed;
1639 	int error = sysctl_io_number(req, aio_max_requests, sizeof(int), &new_value, &changed);
1640 	if (changed) {
1641 		/* make sure the system-wide limit is greater than the per process limit */
1642 		if (new_value >= aio_max_requests_per_process && new_value <= AIO_MAX_REQUESTS) {
1643 			aio_max_requests = new_value;
1644 		} else {
1645 			error = EINVAL;
1646 		}
1647 	}
1648 	return error;
1649 }
1650 
1651 
1652 /*
1653  * Max number of concurrent aio requests per process
1654  */
1655 STATIC int
sysctl_aioprocmax(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)1656 sysctl_aioprocmax
1657 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1658 {
1659 	int new_value, changed;
1660 	int error = sysctl_io_number(req, aio_max_requests_per_process, sizeof(int), &new_value, &changed);
1661 	if (changed) {
1662 		/* make sure per process limit is less than the system-wide limit */
1663 		if (new_value <= aio_max_requests && new_value >= AIO_LISTIO_MAX) {
1664 			aio_max_requests_per_process = new_value;
1665 		} else {
1666 			error = EINVAL;
1667 		}
1668 	}
1669 	return error;
1670 }
1671 
1672 
1673 /*
1674  * Max number of async IO worker threads
1675  */
1676 STATIC int
sysctl_aiothreads(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)1677 sysctl_aiothreads
1678 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1679 {
1680 	int new_value, changed;
1681 	int error = sysctl_io_number(req, aio_worker_threads, sizeof(int), &new_value, &changed);
1682 	if (changed) {
1683 		/* we only allow an increase in the number of worker threads */
1684 		if (new_value > aio_worker_threads) {
1685 			_aio_create_worker_threads((new_value - aio_worker_threads));
1686 			aio_worker_threads = new_value;
1687 		} else {
1688 			error = EINVAL;
1689 		}
1690 	}
1691 	return error;
1692 }
1693 
1694 
1695 /*
1696  * System-wide limit on the max number of processes
1697  */
1698 STATIC int
sysctl_maxproc(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)1699 sysctl_maxproc
1700 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1701 {
1702 	int new_value, changed;
1703 	int error = sysctl_io_number(req, maxproc, sizeof(int), &new_value, &changed);
1704 	if (changed) {
1705 		AUDIT_ARG(value32, new_value);
1706 		/* make sure the system-wide limit is less than the configured hard
1707 		 *  limit set at kernel compilation */
1708 		if (new_value <= hard_maxproc && new_value > 0) {
1709 			maxproc = new_value;
1710 		} else {
1711 			error = EINVAL;
1712 		}
1713 	}
1714 	return error;
1715 }
1716 
1717 extern int sched_enable_smt;
1718 STATIC int
sysctl_sched_enable_smt(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)1719 sysctl_sched_enable_smt
1720 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1721 {
1722 	int new_value, changed;
1723 	int error = sysctl_io_number(req, sched_enable_smt, sizeof(int), &new_value, &changed);
1724 	if (error) {
1725 		return error;
1726 	}
1727 	kern_return_t kret = KERN_SUCCESS;
1728 	if (changed) {
1729 		AUDIT_ARG(value32, new_value);
1730 		if (new_value == 0) {
1731 			sched_enable_smt = 0;
1732 			kret = enable_smt_processors(false);
1733 		} else {
1734 			sched_enable_smt = 1;
1735 			kret = enable_smt_processors(true);
1736 		}
1737 	}
1738 	switch (kret) {
1739 	case KERN_SUCCESS:
1740 		error = 0;
1741 		break;
1742 	case KERN_INVALID_ARGUMENT:
1743 		error = EINVAL;
1744 		break;
1745 	case KERN_FAILURE:
1746 		error = EBUSY;
1747 		break;
1748 	default:
1749 		error = ENOENT;
1750 		break;
1751 	}
1752 
1753 	return error;
1754 }
1755 
1756 SYSCTL_STRING(_kern, KERN_OSTYPE, ostype,
1757     CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1758     ostype, 0, "");
1759 SYSCTL_STRING(_kern, KERN_OSRELEASE, osrelease,
1760     CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1761     osrelease, 0, "");
1762 SYSCTL_INT(_kern, KERN_OSREV, osrevision,
1763     CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1764     (int *)NULL, BSD, "");
1765 SYSCTL_STRING(_kern, KERN_VERSION, version,
1766     CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1767     version, 0, "");
1768 SYSCTL_STRING(_kern, OID_AUTO, uuid,
1769     CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1770     &kernel_uuid_string[0], 0, "");
1771 
1772 SYSCTL_STRING(_kern, OID_AUTO, osbuildconfig,
1773     CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED | CTLFLAG_MASKED,
1774     &osbuild_config[0], 0, "");
1775 
1776 #if DEBUG
1777 #ifndef DKPR
1778 #define DKPR 1
1779 #endif
1780 #endif
1781 
1782 #if DKPR
1783 int debug_kprint_syscall = 0;
1784 char debug_kprint_syscall_process[MAXCOMLEN + 1];
1785 
1786 /* Thread safe: bits and string value are not used to reclaim state */
1787 SYSCTL_INT(_debug, OID_AUTO, kprint_syscall,
1788     CTLFLAG_RW | CTLFLAG_LOCKED, &debug_kprint_syscall, 0, "kprintf syscall tracing");
1789 SYSCTL_STRING(_debug, OID_AUTO, kprint_syscall_process,
1790     CTLFLAG_RW | CTLFLAG_LOCKED, debug_kprint_syscall_process, sizeof(debug_kprint_syscall_process),
1791     "name of process for kprintf syscall tracing");
1792 
1793 int
debug_kprint_current_process(const char ** namep)1794 debug_kprint_current_process(const char **namep)
1795 {
1796 	struct proc *p = current_proc();
1797 
1798 	if (p == NULL) {
1799 		return 0;
1800 	}
1801 
1802 	if (debug_kprint_syscall_process[0]) {
1803 		/* user asked to scope tracing to a particular process name */
1804 		if (0 == strncmp(debug_kprint_syscall_process,
1805 		    p->p_comm, sizeof(debug_kprint_syscall_process))) {
1806 			/* no value in telling the user that we traced what they asked */
1807 			if (namep) {
1808 				*namep = NULL;
1809 			}
1810 
1811 			return 1;
1812 		} else {
1813 			return 0;
1814 		}
1815 	}
1816 
1817 	/* trace all processes. Tell user what we traced */
1818 	if (namep) {
1819 		*namep = p->p_comm;
1820 	}
1821 
1822 	return 1;
1823 }
1824 #endif
1825 
1826 /* PR-5293665: need to use a callback function for kern.osversion to set
1827  * osversion in IORegistry */
1828 
1829 STATIC int
sysctl_osversion(__unused struct sysctl_oid * oidp,void * arg1,int arg2,struct sysctl_req * req)1830 sysctl_osversion(__unused struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req)
1831 {
1832 	int rval = 0;
1833 
1834 	rval = sysctl_handle_string(oidp, arg1, arg2, req);
1835 
1836 	if (req->newptr) {
1837 		IORegistrySetOSBuildVersion((char *)arg1);
1838 	}
1839 
1840 	return rval;
1841 }
1842 
1843 SYSCTL_PROC(_kern, KERN_OSVERSION, osversion,
1844     CTLFLAG_RW | CTLFLAG_KERN | CTLTYPE_STRING | CTLFLAG_LOCKED,
1845     osversion, 256 /* OSVERSIZE*/,
1846     sysctl_osversion, "A", "");
1847 
1848 static bool
_already_set_or_not_launchd(struct sysctl_req * req,char * val)1849 _already_set_or_not_launchd(struct sysctl_req *req, char *val)
1850 {
1851 	if (req->newptr != 0) {
1852 		/*
1853 		 * Can only ever be set by launchd, and only once at boot.
1854 		 */
1855 		if (proc_getpid(req->p) != 1 || val[0] != '\0') {
1856 			return true;
1857 		}
1858 	}
1859 	return false;
1860 }
1861 
1862 #if XNU_TARGET_OS_OSX
1863 static int
sysctl_system_version_compat(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)1864 sysctl_system_version_compat
1865 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1866 {
1867 	int oldval = (task_has_system_version_compat_enabled(current_task()));
1868 	int new_value = 0, changed = 0;
1869 
1870 	int error = sysctl_io_number(req, oldval, sizeof(int), &new_value, &changed);
1871 	if (changed) {
1872 		task_set_system_version_compat_enabled(current_task(), (new_value));
1873 	}
1874 	return error;
1875 }
1876 
1877 SYSCTL_PROC(_kern, OID_AUTO, system_version_compat,
1878     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_LOCKED,
1879     0, 0, sysctl_system_version_compat, "A", "");
1880 
1881 char osproductversioncompat[48] = { '\0' };
1882 
1883 static int
sysctl_osproductversioncompat(struct sysctl_oid * oidp,void * arg1,int arg2,struct sysctl_req * req)1884 sysctl_osproductversioncompat(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req)
1885 {
1886 	if (_already_set_or_not_launchd(req, osproductversioncompat)) {
1887 		return EPERM;
1888 	}
1889 	return sysctl_handle_string(oidp, arg1, arg2, req);
1890 }
1891 
1892 
1893 SYSCTL_PROC(_kern, OID_AUTO, osproductversioncompat,
1894     CTLFLAG_RW | CTLFLAG_KERN | CTLTYPE_STRING | CTLFLAG_LOCKED,
1895     osproductversioncompat, sizeof(osproductversioncompat),
1896     sysctl_osproductversioncompat, "A", "The ProductVersion from SystemVersionCompat.plist");
1897 #endif
1898 
1899 char osproductversion[48] = { '\0' };
1900 
1901 static char iossupportversion_string[48] = { '\0' };
1902 
1903 static int
sysctl_osproductversion(__unused struct sysctl_oid * oidp,void * arg1,int arg2,struct sysctl_req * req)1904 sysctl_osproductversion(__unused struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req)
1905 {
1906 	if (_already_set_or_not_launchd(req, osproductversion)) {
1907 		return EPERM;
1908 	}
1909 
1910 #if XNU_TARGET_OS_OSX
1911 	if (task_has_system_version_compat_enabled(current_task()) && (osproductversioncompat[0] != '\0')) {
1912 		return sysctl_handle_string(oidp, osproductversioncompat, arg2, req);
1913 	} else {
1914 		return sysctl_handle_string(oidp, arg1, arg2, req);
1915 	}
1916 #else
1917 	return sysctl_handle_string(oidp, arg1, arg2, req);
1918 #endif
1919 }
1920 
1921 #if XNU_TARGET_OS_OSX
1922 static_assert(sizeof(osproductversioncompat) == sizeof(osproductversion),
1923     "osproductversion size matches osproductversioncompat size");
1924 #endif
1925 
1926 SYSCTL_PROC(_kern, OID_AUTO, osproductversion,
1927     CTLFLAG_RW | CTLFLAG_KERN | CTLTYPE_STRING | CTLFLAG_LOCKED,
1928     osproductversion, sizeof(osproductversion),
1929     sysctl_osproductversion, "A", "The ProductVersion from SystemVersion.plist");
1930 
1931 char osreleasetype[48] = { '\0' };
1932 
1933 STATIC int
sysctl_osreleasetype(__unused struct sysctl_oid * oidp,void * arg1,int arg2,struct sysctl_req * req)1934 sysctl_osreleasetype(__unused struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req)
1935 {
1936 	if (_already_set_or_not_launchd(req, osreleasetype)) {
1937 		return EPERM;
1938 	}
1939 	return sysctl_handle_string(oidp, arg1, arg2, req);
1940 }
1941 
1942 void reset_osreleasetype(void);
1943 
1944 void
reset_osreleasetype(void)1945 reset_osreleasetype(void)
1946 {
1947 	memset(osreleasetype, 0, sizeof(osreleasetype));
1948 }
1949 
1950 SYSCTL_PROC(_kern, OID_AUTO, osreleasetype,
1951     CTLFLAG_RW | CTLFLAG_KERN | CTLTYPE_STRING | CTLFLAG_LOCKED,
1952     osreleasetype, sizeof(osreleasetype),
1953     sysctl_osreleasetype, "A", "The ReleaseType from SystemVersion.plist");
1954 
1955 STATIC int
sysctl_iossupportversion(__unused struct sysctl_oid * oidp,void * arg1,int arg2,struct sysctl_req * req)1956 sysctl_iossupportversion(__unused struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req)
1957 {
1958 	if (_already_set_or_not_launchd(req, iossupportversion_string)) {
1959 		return EPERM;
1960 	}
1961 
1962 	return sysctl_handle_string(oidp, arg1, arg2, req);
1963 }
1964 
1965 SYSCTL_PROC(_kern, OID_AUTO, iossupportversion,
1966     CTLFLAG_RW | CTLFLAG_KERN | CTLTYPE_STRING | CTLFLAG_LOCKED,
1967     iossupportversion_string, sizeof(iossupportversion_string),
1968     sysctl_iossupportversion, "A", "The iOSSupportVersion from SystemVersion.plist");
1969 
1970 static uint64_t osvariant_status = 0;
1971 
1972 STATIC int
sysctl_osvariant_status(__unused struct sysctl_oid * oidp,void * arg1,int arg2,struct sysctl_req * req)1973 sysctl_osvariant_status(__unused struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req)
1974 {
1975 	if (req->newptr != 0) {
1976 		/*
1977 		 * Can only ever be set by launchd, and only once.
1978 		 * Reset by usrctl() -> reset_osvariant_status() during
1979 		 * userspace reboot, since userspace could reboot into
1980 		 * a different variant.
1981 		 */
1982 		if (proc_getpid(req->p) != 1 || osvariant_status != 0) {
1983 			return EPERM;
1984 		}
1985 	}
1986 
1987 	return sysctl_handle_quad(oidp, arg1, arg2, req);
1988 }
1989 
1990 SYSCTL_PROC(_kern, OID_AUTO, osvariant_status,
1991     CTLFLAG_RW | CTLTYPE_QUAD | CTLFLAG_LOCKED | CTLFLAG_MASKED,
1992     &osvariant_status, sizeof(osvariant_status),
1993     sysctl_osvariant_status, "Q", "Opaque flags used to cache OS variant information");
1994 
1995 void reset_osvariant_status(void);
1996 
1997 void
reset_osvariant_status(void)1998 reset_osvariant_status(void)
1999 {
2000 	osvariant_status = 0;
2001 }
2002 
2003 extern void commpage_update_dyld_flags(uint64_t);
2004 TUNABLE_WRITEABLE(uint64_t, dyld_flags, "dyld_flags", 0);
2005 
2006 STATIC int
sysctl_dyld_flags(__unused struct sysctl_oid * oidp,void * arg1,int arg2,struct sysctl_req * req)2007 sysctl_dyld_flags(__unused struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req)
2008 {
2009 	/*
2010 	 * Can only ever be set by launchd, possibly several times
2011 	 * as dyld may change its mind after a userspace reboot.
2012 	 */
2013 	if (req->newptr != 0 && proc_getpid(req->p) != 1) {
2014 		return EPERM;
2015 	}
2016 
2017 	int res = sysctl_handle_quad(oidp, arg1, arg2, req);
2018 	if (req->newptr && res == 0) {
2019 		commpage_update_dyld_flags(dyld_flags);
2020 	}
2021 	return res;
2022 }
2023 
2024 SYSCTL_PROC(_kern, OID_AUTO, dyld_flags,
2025     CTLFLAG_RW | CTLTYPE_QUAD | CTLFLAG_LOCKED | CTLFLAG_MASKED,
2026     &dyld_flags, sizeof(dyld_flags),
2027     sysctl_dyld_flags, "Q", "Opaque flags used to cache dyld system-wide configuration");
2028 
2029 #if defined(XNU_TARGET_OS_BRIDGE)
2030 char macosproductversion[MACOS_VERS_LEN] = { '\0' };
2031 
2032 SYSCTL_STRING(_kern, OID_AUTO, macosproductversion,
2033     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
2034     &macosproductversion[0], MACOS_VERS_LEN, "The currently running macOS ProductVersion (from SystemVersion.plist on macOS)");
2035 
2036 char macosversion[MACOS_VERS_LEN] = { '\0' };
2037 
2038 SYSCTL_STRING(_kern, OID_AUTO, macosversion,
2039     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
2040     &macosversion[0], MACOS_VERS_LEN, "The currently running macOS build version");
2041 #endif
2042 
2043 STATIC int
sysctl_sysctl_bootargs(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)2044 sysctl_sysctl_bootargs
2045 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2046 {
2047 	int error;
2048 	char buf[BOOT_LINE_LENGTH];
2049 
2050 	strlcpy(buf, PE_boot_args(), BOOT_LINE_LENGTH);
2051 	error = sysctl_io_string(req, buf, BOOT_LINE_LENGTH, 0, NULL);
2052 	return error;
2053 }
2054 
2055 SYSCTL_PROC(_kern, OID_AUTO, bootargs,
2056     CTLFLAG_LOCKED | CTLFLAG_RD | CTLFLAG_KERN | CTLTYPE_STRING,
2057     NULL, 0,
2058     sysctl_sysctl_bootargs, "A", "bootargs");
2059 
2060 STATIC int
sysctl_kernelcacheuuid(struct sysctl_oid * oidp,void * arg1,int arg2,struct sysctl_req * req)2061 sysctl_kernelcacheuuid(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req)
2062 {
2063 	int rval = ENOENT;
2064 	if (kernelcache_uuid_valid) {
2065 		rval = sysctl_handle_string(oidp, arg1, arg2, req);
2066 	}
2067 	return rval;
2068 }
2069 
2070 SYSCTL_PROC(_kern, OID_AUTO, kernelcacheuuid,
2071     CTLFLAG_RD | CTLFLAG_KERN | CTLTYPE_STRING | CTLFLAG_LOCKED,
2072     kernelcache_uuid_string, sizeof(kernelcache_uuid_string),
2073     sysctl_kernelcacheuuid, "A", "");
2074 
2075 STATIC int
sysctl_systemfilesetuuid(struct sysctl_oid * oidp,void * arg1,int arg2,struct sysctl_req * req)2076 sysctl_systemfilesetuuid(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req)
2077 {
2078 	int rval = ENOENT;
2079 	if (pageablekc_uuid_valid) {
2080 		rval = sysctl_handle_string(oidp, arg1, arg2, req);
2081 	}
2082 	return rval;
2083 }
2084 
2085 SYSCTL_PROC(_kern, OID_AUTO, systemfilesetuuid,
2086     CTLFLAG_RD | CTLFLAG_KERN | CTLTYPE_STRING | CTLFLAG_LOCKED,
2087     pageablekc_uuid_string, sizeof(pageablekc_uuid_string),
2088     sysctl_systemfilesetuuid, "A", "");
2089 
2090 STATIC int
sysctl_auxiliaryfilesetuuid(struct sysctl_oid * oidp,void * arg1,int arg2,struct sysctl_req * req)2091 sysctl_auxiliaryfilesetuuid(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req)
2092 {
2093 	int rval = ENOENT;
2094 	if (auxkc_uuid_valid) {
2095 		rval = sysctl_handle_string(oidp, arg1, arg2, req);
2096 	}
2097 	return rval;
2098 }
2099 
2100 SYSCTL_PROC(_kern, OID_AUTO, auxiliaryfilesetuuid,
2101     CTLFLAG_RD | CTLFLAG_KERN | CTLTYPE_STRING | CTLFLAG_LOCKED,
2102     auxkc_uuid_string, sizeof(auxkc_uuid_string),
2103     sysctl_auxiliaryfilesetuuid, "A", "");
2104 
2105 STATIC int
sysctl_filesetuuid(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)2106 sysctl_filesetuuid(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2107 {
2108 	int rval = ENOENT;
2109 	kc_format_t kcformat;
2110 	kernel_mach_header_t *mh;
2111 	void *uuid = NULL;
2112 	unsigned long uuidlen = 0;
2113 	uuid_string_t uuid_str;
2114 
2115 	if (!PE_get_primary_kc_format(&kcformat) || kcformat != KCFormatFileset) {
2116 		return rval;
2117 	}
2118 
2119 	mh = (kernel_mach_header_t *)PE_get_kc_header(KCKindPrimary);
2120 	uuid = getuuidfromheader(mh, &uuidlen);
2121 
2122 	if ((uuid != NULL) && (uuidlen == sizeof(uuid_t))) {
2123 		uuid_unparse_upper(*(uuid_t *)uuid, uuid_str);
2124 		rval = sysctl_io_string(req, (char *)uuid_str, sizeof(uuid_str), 0, NULL);
2125 	}
2126 
2127 	return rval;
2128 }
2129 
2130 SYSCTL_PROC(_kern, OID_AUTO, filesetuuid,
2131     CTLFLAG_RD | CTLFLAG_KERN | CTLTYPE_STRING | CTLFLAG_LOCKED,
2132     NULL, 0,
2133     sysctl_filesetuuid, "A", "");
2134 
2135 
2136 SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles,
2137     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
2138     &maxfiles, 0, "");
2139 SYSCTL_INT(_kern, KERN_ARGMAX, argmax,
2140     CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
2141     (int *)NULL, ARG_MAX, "");
2142 SYSCTL_INT(_kern, KERN_POSIX1, posix1version,
2143     CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
2144     (int *)NULL, _POSIX_VERSION, "");
2145 SYSCTL_INT(_kern, KERN_NGROUPS, ngroups,
2146     CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
2147     (int *)NULL, NGROUPS_MAX, "");
2148 SYSCTL_INT(_kern, KERN_JOB_CONTROL, job_control,
2149     CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
2150     (int *)NULL, 1, "");
2151 #if 1   /* _POSIX_SAVED_IDS from <unistd.h> */
2152 SYSCTL_INT(_kern, KERN_SAVED_IDS, saved_ids,
2153     CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
2154     (int *)NULL, 1, "");
2155 #else
2156 SYSCTL_INT(_kern, KERN_SAVED_IDS, saved_ids,
2157     CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
2158     NULL, 0, "");
2159 #endif
2160 SYSCTL_INT(_kern, OID_AUTO, num_files,
2161     CTLFLAG_RD | CTLFLAG_LOCKED,
2162     &nfiles, 0, "");
2163 SYSCTL_COMPAT_INT(_kern, OID_AUTO, num_vnodes,
2164     CTLFLAG_RD | CTLFLAG_LOCKED,
2165     &numvnodes, 0, "");
2166 SYSCTL_INT(_kern, OID_AUTO, num_tasks,
2167     CTLFLAG_RD | CTLFLAG_LOCKED,
2168     &task_max, 0, "");
2169 SYSCTL_INT(_kern, OID_AUTO, num_threads,
2170     CTLFLAG_RD | CTLFLAG_LOCKED,
2171     &thread_max, 0, "");
2172 SYSCTL_INT(_kern, OID_AUTO, num_taskthreads,
2173     CTLFLAG_RD | CTLFLAG_LOCKED,
2174     &task_threadmax, 0, "");
2175 SYSCTL_LONG(_kern, OID_AUTO, num_recycledvnodes,
2176     CTLFLAG_RD | CTLFLAG_LOCKED,
2177     &num_recycledvnodes, "");
2178 SYSCTL_COMPAT_INT(_kern, OID_AUTO, free_vnodes,
2179     CTLFLAG_RD | CTLFLAG_LOCKED,
2180     &freevnodes, 0, "");
2181 
2182 STATIC int
sysctl_maxvnodes(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)2183 sysctl_maxvnodes(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2184 {
2185 	int oldval = desiredvnodes;
2186 	int error = sysctl_io_number(req, desiredvnodes, sizeof(int), &desiredvnodes, NULL);
2187 
2188 	if (oldval != desiredvnodes) {
2189 		resize_namecache(desiredvnodes);
2190 	}
2191 
2192 	return error;
2193 }
2194 
2195 SYSCTL_INT(_kern, OID_AUTO, namecache_disabled,
2196     CTLFLAG_RW | CTLFLAG_LOCKED,
2197     &nc_disabled, 0, "");
2198 
2199 SYSCTL_PROC(_kern, KERN_MAXVNODES, maxvnodes,
2200     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
2201     0, 0, sysctl_maxvnodes, "I", "");
2202 
2203 SYSCTL_PROC(_kern, KERN_MAXPROC, maxproc,
2204     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
2205     0, 0, sysctl_maxproc, "I", "");
2206 
2207 SYSCTL_PROC(_kern, KERN_AIOMAX, aiomax,
2208     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
2209     0, 0, sysctl_aiomax, "I", "");
2210 
2211 SYSCTL_PROC(_kern, KERN_AIOPROCMAX, aioprocmax,
2212     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
2213     0, 0, sysctl_aioprocmax, "I", "");
2214 
2215 SYSCTL_PROC(_kern, KERN_AIOTHREADS, aiothreads,
2216     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
2217     0, 0, sysctl_aiothreads, "I", "");
2218 
2219 SYSCTL_PROC(_kern, OID_AUTO, sched_enable_smt,
2220     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN,
2221     0, 0, sysctl_sched_enable_smt, "I", "");
2222 
2223 extern int sched_allow_NO_SMT_threads;
2224 SYSCTL_INT(_kern, OID_AUTO, sched_allow_NO_SMT_threads,
2225     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
2226     &sched_allow_NO_SMT_threads, 0, "");
2227 
2228 extern int sched_avoid_cpu0;
2229 SYSCTL_INT(_kern, OID_AUTO, sched_rt_avoid_cpu0,
2230     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
2231     &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");
2232 
2233 #if (DEVELOPMENT || DEBUG)
2234 extern int smt_sched_bonus_16ths;
2235 SYSCTL_INT(_kern, OID_AUTO, smt_sched_bonus_16ths,
2236     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
2237     &smt_sched_bonus_16ths, 0, "");
2238 
2239 extern int smt_timeshare_enabled;
2240 SYSCTL_INT(_kern, OID_AUTO, sched_smt_timeshare_enable,
2241     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
2242     &smt_timeshare_enabled, 0, "");
2243 
2244 extern int sched_smt_balance;
2245 SYSCTL_INT(_kern, OID_AUTO, sched_smt_balance,
2246     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
2247     &sched_smt_balance, 0, "");
2248 extern int sched_allow_rt_smt;
2249 SYSCTL_INT(_kern, OID_AUTO, sched_allow_rt_smt,
2250     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
2251     &sched_allow_rt_smt, 0, "");
2252 extern int sched_allow_rt_steal;
2253 SYSCTL_INT(_kern, OID_AUTO, sched_allow_rt_steal,
2254     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
2255     &sched_allow_rt_steal, 0, "");
2256 extern int sched_choose_first_fd_processor;
2257 SYSCTL_INT(_kern, OID_AUTO, sched_choose_first_fd_processor,
2258     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
2259     &sched_choose_first_fd_processor, 0, "");
2260 extern int sched_backup_cpu_timeout_count;
2261 SYSCTL_INT(_kern, OID_AUTO, sched_backup_cpu_timeout_count,
2262     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
2263     &sched_backup_cpu_timeout_count, 0, "The maximum number of 10us delays before allowing a backup cpu to select a thread");
2264 #if __arm__ || __arm64__
2265 extern uint32_t perfcontrol_requested_recommended_cores;
2266 SYSCTL_UINT(_kern, OID_AUTO, sched_recommended_cores,
2267     CTLFLAG_KERN | CTLFLAG_RD | CTLFLAG_LOCKED,
2268     &perfcontrol_requested_recommended_cores, 0, "");
2269 
2270 /* Scheduler perfcontrol callouts sysctls */
2271 SYSCTL_DECL(_kern_perfcontrol_callout);
2272 SYSCTL_NODE(_kern, OID_AUTO, perfcontrol_callout, CTLFLAG_RW | CTLFLAG_LOCKED, 0,
2273     "scheduler perfcontrol callouts");
2274 
2275 extern int perfcontrol_callout_stats_enabled;
2276 SYSCTL_INT(_kern_perfcontrol_callout, OID_AUTO, stats_enabled,
2277     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
2278     &perfcontrol_callout_stats_enabled, 0, "");
2279 
2280 extern uint64_t perfcontrol_callout_stat_avg(perfcontrol_callout_type_t type,
2281     perfcontrol_callout_stat_t stat);
2282 
2283 /* On-Core Callout */
2284 STATIC int
sysctl_perfcontrol_callout_stat(__unused struct sysctl_oid * oidp,void * arg1,int arg2,struct sysctl_req * req)2285 sysctl_perfcontrol_callout_stat
2286 (__unused struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req)
2287 {
2288 	perfcontrol_callout_stat_t stat = (perfcontrol_callout_stat_t)arg1;
2289 	perfcontrol_callout_type_t type = (perfcontrol_callout_type_t)arg2;
2290 	return sysctl_io_number(req, (int)perfcontrol_callout_stat_avg(type, stat),
2291 	           sizeof(int), NULL, NULL);
2292 }
2293 
2294 SYSCTL_PROC(_kern_perfcontrol_callout, OID_AUTO, oncore_instr,
2295     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
2296     (void *)PERFCONTROL_STAT_INSTRS, PERFCONTROL_CALLOUT_ON_CORE,
2297     sysctl_perfcontrol_callout_stat, "I", "");
2298 SYSCTL_PROC(_kern_perfcontrol_callout, OID_AUTO, oncore_cycles,
2299     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
2300     (void *)PERFCONTROL_STAT_CYCLES, PERFCONTROL_CALLOUT_ON_CORE,
2301     sysctl_perfcontrol_callout_stat, "I", "");
2302 SYSCTL_PROC(_kern_perfcontrol_callout, OID_AUTO, offcore_instr,
2303     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
2304     (void *)PERFCONTROL_STAT_INSTRS, PERFCONTROL_CALLOUT_OFF_CORE,
2305     sysctl_perfcontrol_callout_stat, "I", "");
2306 SYSCTL_PROC(_kern_perfcontrol_callout, OID_AUTO, offcore_cycles,
2307     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
2308     (void *)PERFCONTROL_STAT_CYCLES, PERFCONTROL_CALLOUT_OFF_CORE,
2309     sysctl_perfcontrol_callout_stat, "I", "");
2310 SYSCTL_PROC(_kern_perfcontrol_callout, OID_AUTO, context_instr,
2311     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
2312     (void *)PERFCONTROL_STAT_INSTRS, PERFCONTROL_CALLOUT_CONTEXT,
2313     sysctl_perfcontrol_callout_stat, "I", "");
2314 SYSCTL_PROC(_kern_perfcontrol_callout, OID_AUTO, context_cycles,
2315     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
2316     (void *)PERFCONTROL_STAT_CYCLES, PERFCONTROL_CALLOUT_CONTEXT,
2317     sysctl_perfcontrol_callout_stat, "I", "");
2318 SYSCTL_PROC(_kern_perfcontrol_callout, OID_AUTO, update_instr,
2319     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
2320     (void *)PERFCONTROL_STAT_INSTRS, PERFCONTROL_CALLOUT_STATE_UPDATE,
2321     sysctl_perfcontrol_callout_stat, "I", "");
2322 SYSCTL_PROC(_kern_perfcontrol_callout, OID_AUTO, update_cycles,
2323     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
2324     (void *)PERFCONTROL_STAT_CYCLES, PERFCONTROL_CALLOUT_STATE_UPDATE,
2325     sysctl_perfcontrol_callout_stat, "I", "");
2326 
2327 #if __AMP__
2328 extern int sched_amp_idle_steal;
2329 SYSCTL_INT(_kern, OID_AUTO, sched_amp_idle_steal,
2330     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
2331     &sched_amp_idle_steal, 0, "");
2332 extern int sched_amp_spill_steal;
2333 SYSCTL_INT(_kern, OID_AUTO, sched_amp_spill_steal,
2334     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
2335     &sched_amp_spill_steal, 0, "");
2336 extern int sched_amp_spill_count;
2337 SYSCTL_INT(_kern, OID_AUTO, sched_amp_spill_count,
2338     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
2339     &sched_amp_spill_count, 0, "");
2340 extern int sched_amp_spill_deferred_ipi;
2341 SYSCTL_INT(_kern, OID_AUTO, sched_amp_spill_deferred_ipi,
2342     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
2343     &sched_amp_spill_deferred_ipi, 0, "");
2344 extern int sched_amp_pcores_preempt_immediate_ipi;
2345 SYSCTL_INT(_kern, OID_AUTO, sched_amp_pcores_preempt_immediate_ipi,
2346     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
2347     &sched_amp_pcores_preempt_immediate_ipi, 0, "");
2348 #endif /* __AMP__ */
2349 #endif /* __arm__ || __arm64__ */
2350 
2351 #if __arm64__
2352 extern int legacy_footprint_entitlement_mode;
2353 SYSCTL_INT(_kern, OID_AUTO, legacy_footprint_entitlement_mode,
2354     CTLFLAG_KERN | CTLFLAG_RD | CTLFLAG_LOCKED,
2355     &legacy_footprint_entitlement_mode, 0, "");
2356 #endif /* __arm64__ */
2357 
2358 /*
2359  * Realtime threads are ordered by highest priority first then,
2360  * for threads of the same priority, by earliest deadline first.
2361  * But if sched_rt_runq_strict_priority is false (the default),
2362  * a lower priority thread with an earlier deadline will be preferred
2363  * over a higher priority thread with a later deadline, as long as
2364  * both threads' computations will fit before the later deadline.
2365  */
2366 extern int sched_rt_runq_strict_priority;
2367 SYSCTL_INT(_kern, OID_AUTO, sched_rt_runq_strict_priority,
2368     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
2369     &sched_rt_runq_strict_priority, 0, "");
2370 
2371 static int
sysctl_kern_sched_rt_n_backup_processors(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)2372 sysctl_kern_sched_rt_n_backup_processors(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2373 {
2374 	int new_value, changed;
2375 	int old_value = sched_get_rt_n_backup_processors();
2376 	int error = sysctl_io_number(req, old_value, sizeof(int), &new_value, &changed);
2377 	if (changed) {
2378 		sched_set_rt_n_backup_processors(new_value);
2379 	}
2380 
2381 	return error;
2382 }
2383 
2384 SYSCTL_PROC(_kern, OID_AUTO, sched_rt_n_backup_processors,
2385     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
2386     0, 0, sysctl_kern_sched_rt_n_backup_processors, "I", "");
2387 
2388 static int
sysctl_kern_sched_rt_constraint_ll_us(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)2389 sysctl_kern_sched_rt_constraint_ll_us(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2390 {
2391 	int new_value, changed;
2392 	int old_value = sched_get_rt_constraint_ll();
2393 	int error = sysctl_io_number(req, old_value, sizeof(int), &new_value, &changed);
2394 	if (changed) {
2395 		sched_set_rt_constraint_ll(new_value);
2396 	}
2397 
2398 	return error;
2399 }
2400 
2401 SYSCTL_PROC(_kern, OID_AUTO, sched_rt_constraint_ll_us,
2402     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
2403     0, 0, sysctl_kern_sched_rt_constraint_ll_us, "I", "");
2404 
2405 static int
sysctl_kern_sched_rt_deadline_epsilon_us(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)2406 sysctl_kern_sched_rt_deadline_epsilon_us(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2407 {
2408 	int new_value, changed;
2409 	int old_value = sched_get_rt_deadline_epsilon();
2410 	int error = sysctl_io_number(req, old_value, sizeof(int), &new_value, &changed);
2411 	if (changed) {
2412 		sched_set_rt_deadline_epsilon(new_value);
2413 	}
2414 
2415 	return error;
2416 }
2417 
2418 SYSCTL_PROC(_kern, OID_AUTO, sched_rt_deadline_epsilon_us,
2419     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
2420     0, 0, sysctl_kern_sched_rt_deadline_epsilon_us, "I", "");
2421 
2422 extern int sched_idle_delay_cpuid;
2423 SYSCTL_INT(_kern, OID_AUTO, sched_idle_delay_cpuid,
2424     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
2425     &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");
2426 
2427 #endif /* (DEVELOPMENT || DEBUG) */
2428 
2429 STATIC int
sysctl_securelvl(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)2430 sysctl_securelvl
2431 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2432 {
2433 	int new_value, changed;
2434 	int error = sysctl_io_number(req, securelevel, sizeof(int), &new_value, &changed);
2435 	if (changed) {
2436 		if (!(new_value < securelevel && proc_getpid(req->p) != 1)) {
2437 			proc_list_lock();
2438 			securelevel = new_value;
2439 			proc_list_unlock();
2440 		} else {
2441 			error = EPERM;
2442 		}
2443 	}
2444 	return error;
2445 }
2446 
2447 SYSCTL_PROC(_kern, KERN_SECURELVL, securelevel,
2448     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
2449     0, 0, sysctl_securelvl, "I", "");
2450 
2451 
2452 STATIC int
sysctl_domainname(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)2453 sysctl_domainname
2454 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2455 {
2456 	int error, changed;
2457 	char tmpname[MAXHOSTNAMELEN] = {};
2458 
2459 	lck_mtx_lock(&domainname_lock);
2460 	strlcpy(tmpname, domainname, sizeof(tmpname));
2461 	lck_mtx_unlock(&domainname_lock);
2462 
2463 	error = sysctl_io_string(req, tmpname, sizeof(tmpname), 0, &changed);
2464 	if (!error && changed) {
2465 		lck_mtx_lock(&hostname_lock);
2466 		strlcpy(domainname, tmpname, sizeof(domainname));
2467 		lck_mtx_unlock(&hostname_lock);
2468 	}
2469 	return error;
2470 }
2471 
2472 SYSCTL_PROC(_kern, KERN_DOMAINNAME, nisdomainname,
2473     CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_LOCKED,
2474     0, 0, sysctl_domainname, "A", "");
2475 
2476 SYSCTL_COMPAT_INT(_kern, KERN_HOSTID, hostid,
2477     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
2478     &hostid, 0, "");
2479 
2480 STATIC int
sysctl_hostname(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)2481 sysctl_hostname
2482 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2483 {
2484 	int error, changed;
2485 	char tmpname[MAXHOSTNAMELEN] = {};
2486 
2487 	lck_mtx_lock(&hostname_lock);
2488 	strlcpy(tmpname, hostname, sizeof(tmpname));
2489 	lck_mtx_unlock(&hostname_lock);
2490 
2491 	error = sysctl_io_string(req, tmpname, sizeof(tmpname), 1, &changed);
2492 	if (!error && changed) {
2493 		lck_mtx_lock(&hostname_lock);
2494 		strlcpy(hostname, tmpname, sizeof(hostname));
2495 		lck_mtx_unlock(&hostname_lock);
2496 	}
2497 	return error;
2498 }
2499 
2500 SYSCTL_PROC(_kern, KERN_HOSTNAME, hostname,
2501     CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_LOCKED,
2502     0, 0, sysctl_hostname, "A", "");
2503 
2504 STATIC int
sysctl_procname(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)2505 sysctl_procname
2506 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2507 {
2508 	/* Original code allowed writing, I'm copying this, although this all makes
2509 	 *  no sense to me. Besides, this sysctl is never used. */
2510 	return sysctl_io_string(req, &req->p->p_name[0], (2 * MAXCOMLEN + 1), 1, NULL);
2511 }
2512 
2513 SYSCTL_PROC(_kern, KERN_PROCNAME, procname,
2514     CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_LOCKED,
2515     0, 0, sysctl_procname, "A", "");
2516 
2517 SYSCTL_INT(_kern, KERN_SPECULATIVE_READS, speculative_reads_disabled,
2518     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
2519     &speculative_reads_disabled, 0, "");
2520 
2521 SYSCTL_UINT(_kern, OID_AUTO, preheat_max_bytes,
2522     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
2523     &preheat_max_bytes, 0, "");
2524 
2525 SYSCTL_UINT(_kern, OID_AUTO, preheat_min_bytes,
2526     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
2527     &preheat_min_bytes, 0, "");
2528 
2529 SYSCTL_UINT(_kern, OID_AUTO, speculative_prefetch_max,
2530     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
2531     &speculative_prefetch_max, 0, "");
2532 
2533 SYSCTL_UINT(_kern, OID_AUTO, speculative_prefetch_max_iosize,
2534     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
2535     &speculative_prefetch_max_iosize, 0, "");
2536 
2537 SYSCTL_UINT(_kern, OID_AUTO, vm_page_free_target,
2538     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
2539     &vm_page_free_target, 0, "");
2540 
2541 SYSCTL_UINT(_kern, OID_AUTO, vm_page_free_min,
2542     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
2543     &vm_page_free_min, 0, "");
2544 
2545 SYSCTL_UINT(_kern, OID_AUTO, vm_page_free_reserved,
2546     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
2547     &vm_page_free_reserved, 0, "");
2548 
2549 SYSCTL_UINT(_kern, OID_AUTO, vm_page_speculative_percentage,
2550     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
2551     &vm_pageout_state.vm_page_speculative_percentage, 0, "");
2552 
2553 SYSCTL_UINT(_kern, OID_AUTO, vm_page_speculative_q_age_ms,
2554     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
2555     &vm_pageout_state.vm_page_speculative_q_age_ms, 0, "");
2556 
2557 SYSCTL_UINT(_kern, OID_AUTO, vm_max_delayed_work_limit,
2558     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
2559     &vm_max_delayed_work_limit, 0, "");
2560 
2561 SYSCTL_UINT(_kern, OID_AUTO, vm_max_batch,
2562     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
2563     &vm_max_batch, 0, "");
2564 
2565 SYSCTL_STRING(_kern, OID_AUTO, bootsessionuuid,
2566     CTLFLAG_RD | CTLFLAG_LOCKED,
2567     &bootsessionuuid_string, sizeof(bootsessionuuid_string), "");
2568 
2569 
2570 STATIC int
sysctl_boottime(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)2571 sysctl_boottime
2572 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2573 {
2574 	struct timeval tv;
2575 	boottime_timeval(&tv);
2576 	struct proc *p = req->p;
2577 
2578 	if (proc_is64bit(p)) {
2579 		struct user64_timeval t = {};
2580 		t.tv_sec = tv.tv_sec;
2581 		t.tv_usec = tv.tv_usec;
2582 		return sysctl_io_opaque(req, &t, sizeof(t), NULL);
2583 	} else {
2584 		struct user32_timeval t = {};
2585 		t.tv_sec = (user32_time_t)tv.tv_sec;
2586 		t.tv_usec = tv.tv_usec;
2587 		return sysctl_io_opaque(req, &t, sizeof(t), NULL);
2588 	}
2589 }
2590 
2591 SYSCTL_PROC(_kern, KERN_BOOTTIME, boottime,
2592     CTLTYPE_STRUCT | CTLFLAG_KERN | CTLFLAG_RD | CTLFLAG_LOCKED,
2593     0, 0, sysctl_boottime, "S,timeval", "");
2594 
2595 extern bool IOGetBootUUID(char *);
2596 
2597 /* non-static: written by imageboot.c */
2598 uuid_string_t fake_bootuuid;
2599 
2600 STATIC int
sysctl_bootuuid(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)2601 sysctl_bootuuid
2602 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2603 {
2604 	int error = ENOENT;
2605 
2606 	/* check the first byte to see if the string has been
2607 	 * populated. this is a uuid_STRING_t, this check would
2608 	 * not work with a uuid_t.
2609 	 */
2610 	if (fake_bootuuid[0] != '\0') {
2611 		error = sysctl_io_string(req, fake_bootuuid, 0, 0, NULL);
2612 		goto out;
2613 	}
2614 
2615 	uuid_string_t uuid_string;
2616 	if (IOGetBootUUID(uuid_string)) {
2617 		uuid_t boot_uuid;
2618 		error = uuid_parse(uuid_string, boot_uuid);
2619 		if (!error) {
2620 			error = sysctl_io_string(req, __DECONST(char *, uuid_string), 0, 0, NULL);
2621 		}
2622 	}
2623 
2624 out:
2625 	return error;
2626 }
2627 
2628 SYSCTL_PROC(_kern, OID_AUTO, bootuuid,
2629     CTLTYPE_STRING | CTLFLAG_KERN | CTLFLAG_RD | CTLFLAG_LOCKED,
2630     0, 0, sysctl_bootuuid, "A", "");
2631 
2632 
2633 extern bool IOGetApfsPrebootUUID(char *);
2634 extern bool IOGetAssociatedApfsVolgroupUUID(char *);
2635 
2636 STATIC int
sysctl_apfsprebootuuid(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)2637 sysctl_apfsprebootuuid
2638 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2639 {
2640 	int error = ENOENT;
2641 
2642 	uuid_string_t uuid_string;
2643 	if (IOGetApfsPrebootUUID(uuid_string)) {
2644 		uuid_t apfs_preboot_uuid;
2645 		error = uuid_parse(uuid_string, apfs_preboot_uuid);
2646 		if (!error) {
2647 			error = sysctl_io_string(req, __DECONST(char *, uuid_string), 0, 0, NULL);
2648 		}
2649 	}
2650 
2651 	return error;
2652 }
2653 
2654 SYSCTL_PROC(_kern, OID_AUTO, apfsprebootuuid,
2655     CTLTYPE_STRING | CTLFLAG_KERN | CTLFLAG_RD | CTLFLAG_LOCKED,
2656     0, 0, sysctl_apfsprebootuuid, "A", "");
2657 
2658 STATIC int
sysctl_targetsystemvolgroupuuid(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)2659 sysctl_targetsystemvolgroupuuid
2660 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2661 {
2662 	int error = ENOENT;
2663 
2664 	uuid_string_t uuid_string;
2665 	if (IOGetApfsPrebootUUID(uuid_string)) {
2666 		uuid_t apfs_preboot_uuid;
2667 		error = uuid_parse(uuid_string, apfs_preboot_uuid);
2668 		if (!error) {
2669 			error = sysctl_io_string(req, __DECONST(char *, uuid_string), 0, 0, NULL);
2670 		}
2671 	} else {
2672 		/*
2673 		 * In special boot modes, such as kcgen-mode, the
2674 		 * apfs-preboot-uuid property will not be set. Instead, a
2675 		 * different property, associated-volume-group, will be set
2676 		 * which indicates the UUID of the VolumeGroup containing the
2677 		 * system volume into which you will boot.
2678 		 */
2679 		if (IOGetAssociatedApfsVolgroupUUID(uuid_string)) {
2680 			uuid_t apfs_preboot_uuid;
2681 			error = uuid_parse(uuid_string, apfs_preboot_uuid);
2682 			if (!error) {
2683 				error = sysctl_io_string(req, __DECONST(char *, uuid_string), 0, 0, NULL);
2684 			}
2685 		}
2686 	}
2687 
2688 	return error;
2689 }
2690 
2691 SYSCTL_PROC(_kern, OID_AUTO, targetsystemvolgroupuuid,
2692     CTLTYPE_STRING | CTLFLAG_KERN | CTLFLAG_RD | CTLFLAG_MASKED | CTLFLAG_LOCKED,
2693     0, 0, sysctl_targetsystemvolgroupuuid, "A", "");
2694 
2695 STATIC int
sysctl_symfile(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)2696 sysctl_symfile
2697 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2698 {
2699 	char *str;
2700 	int error = get_kernel_symfile(req->p, &str);
2701 	if (error) {
2702 		return error;
2703 	}
2704 	return sysctl_io_string(req, str, 0, 0, NULL);
2705 }
2706 
2707 
2708 SYSCTL_PROC(_kern, KERN_SYMFILE, symfile,
2709     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_LOCKED,
2710     0, 0, sysctl_symfile, "A", "");
2711 
2712 #if CONFIG_NFS_CLIENT
2713 STATIC int
sysctl_netboot(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)2714 sysctl_netboot
2715 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2716 {
2717 	return sysctl_io_number(req, netboot_root(), sizeof(int), NULL, NULL);
2718 }
2719 
2720 SYSCTL_PROC(_kern, KERN_NETBOOT, netboot,
2721     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
2722     0, 0, sysctl_netboot, "I", "");
2723 #endif
2724 
2725 #ifdef CONFIG_IMGSRC_ACCESS
2726 /*
2727  * Legacy--act as if only one layer of nesting is possible.
2728  */
2729 STATIC int
sysctl_imgsrcdev(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)2730 sysctl_imgsrcdev
2731 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2732 {
2733 	vfs_context_t ctx = vfs_context_current();
2734 	vnode_t devvp;
2735 	int result;
2736 
2737 	if (!vfs_context_issuser(ctx)) {
2738 		return EPERM;
2739 	}
2740 
2741 	if (imgsrc_rootvnodes[0] == NULL) {
2742 		return ENOENT;
2743 	}
2744 
2745 	result = vnode_getwithref(imgsrc_rootvnodes[0]);
2746 	if (result != 0) {
2747 		return result;
2748 	}
2749 
2750 	devvp = vnode_mount(imgsrc_rootvnodes[0])->mnt_devvp;
2751 	result = vnode_getwithref(devvp);
2752 	if (result != 0) {
2753 		goto out;
2754 	}
2755 
2756 	result = sysctl_io_number(req, vnode_specrdev(devvp), sizeof(dev_t), NULL, NULL);
2757 
2758 	vnode_put(devvp);
2759 out:
2760 	vnode_put(imgsrc_rootvnodes[0]);
2761 	return result;
2762 }
2763 
2764 SYSCTL_PROC(_kern, OID_AUTO, imgsrcdev,
2765     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
2766     0, 0, sysctl_imgsrcdev, "I", "");
2767 
2768 STATIC int
sysctl_imgsrcinfo(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)2769 sysctl_imgsrcinfo
2770 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2771 {
2772 	int error;
2773 	struct imgsrc_info info[MAX_IMAGEBOOT_NESTING] = {};    /* 2 for now, no problem */
2774 	uint32_t i;
2775 	vnode_t rvp, devvp;
2776 
2777 	if (imgsrc_rootvnodes[0] == NULLVP) {
2778 		return ENXIO;
2779 	}
2780 
2781 	for (i = 0; i < MAX_IMAGEBOOT_NESTING; i++) {
2782 		/*
2783 		 * Go get the root vnode.
2784 		 */
2785 		rvp = imgsrc_rootvnodes[i];
2786 		if (rvp == NULLVP) {
2787 			break;
2788 		}
2789 
2790 		error = vnode_get(rvp);
2791 		if (error != 0) {
2792 			return error;
2793 		}
2794 
2795 		/*
2796 		 * For now, no getting at a non-local volume.
2797 		 */
2798 		devvp = vnode_mount(rvp)->mnt_devvp;
2799 		if (devvp == NULL) {
2800 			vnode_put(rvp);
2801 			return EINVAL;
2802 		}
2803 
2804 		error = vnode_getwithref(devvp);
2805 		if (error != 0) {
2806 			vnode_put(rvp);
2807 			return error;
2808 		}
2809 
2810 		/*
2811 		 * Fill in info.
2812 		 */
2813 		info[i].ii_dev = vnode_specrdev(devvp);
2814 		info[i].ii_flags = 0;
2815 		info[i].ii_height = i;
2816 		bzero(info[i].ii_reserved, sizeof(info[i].ii_reserved));
2817 
2818 		vnode_put(devvp);
2819 		vnode_put(rvp);
2820 	}
2821 
2822 	return sysctl_io_opaque(req, info, i * sizeof(info[0]), NULL);
2823 }
2824 
2825 SYSCTL_PROC(_kern, OID_AUTO, imgsrcinfo,
2826     CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_LOCKED,
2827     0, 0, sysctl_imgsrcinfo, "I", "");
2828 
2829 #endif /* CONFIG_IMGSRC_ACCESS */
2830 
2831 
2832 SYSCTL_DECL(_kern_timer);
2833 SYSCTL_NODE(_kern, OID_AUTO, timer, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "timer");
2834 
2835 
2836 SYSCTL_INT(_kern_timer, OID_AUTO, coalescing_enabled,
2837     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
2838     &mach_timer_coalescing_enabled, 0, "");
2839 
2840 SYSCTL_QUAD(_kern_timer, OID_AUTO, deadline_tracking_bin_1,
2841     CTLFLAG_RW | CTLFLAG_LOCKED,
2842     &timer_deadline_tracking_bin_1, "");
2843 SYSCTL_QUAD(_kern_timer, OID_AUTO, deadline_tracking_bin_2,
2844     CTLFLAG_RW | CTLFLAG_LOCKED,
2845     &timer_deadline_tracking_bin_2, "");
2846 
2847 SYSCTL_DECL(_kern_timer_longterm);
2848 SYSCTL_NODE(_kern_timer, OID_AUTO, longterm, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "longterm");
2849 
2850 
2851 /* Must match definition in osfmk/kern/timer_call.c */
2852 enum {
2853 	THRESHOLD, QCOUNT,
2854 	ENQUEUES, DEQUEUES, ESCALATES, SCANS, PREEMPTS,
2855 	LATENCY, LATENCY_MIN, LATENCY_MAX, SCAN_LIMIT, SCAN_INTERVAL, PAUSES
2856 };
2857 extern uint64_t timer_sysctl_get(int);
2858 extern int      timer_sysctl_set(int, uint64_t);
2859 
2860 STATIC int
sysctl_timer(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)2861 sysctl_timer
2862 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2863 {
2864 	int             oid = (int)arg1;
2865 	uint64_t        value = timer_sysctl_get(oid);
2866 	uint64_t        new_value;
2867 	int             error;
2868 	int             changed;
2869 
2870 	error = sysctl_io_number(req, value, sizeof(value), &new_value, &changed);
2871 	if (changed) {
2872 		error = timer_sysctl_set(oid, new_value);
2873 	}
2874 
2875 	return error;
2876 }
2877 
2878 SYSCTL_PROC(_kern_timer_longterm, OID_AUTO, threshold,
2879     CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
2880     (void *) THRESHOLD, 0, sysctl_timer, "Q", "");
2881 SYSCTL_PROC(_kern_timer_longterm, OID_AUTO, scan_limit,
2882     CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
2883     (void *) SCAN_LIMIT, 0, sysctl_timer, "Q", "");
2884 SYSCTL_PROC(_kern_timer_longterm, OID_AUTO, scan_interval,
2885     CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
2886     (void *) SCAN_INTERVAL, 0, sysctl_timer, "Q", "");
2887 
2888 SYSCTL_PROC(_kern_timer_longterm, OID_AUTO, qlen,
2889     CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED,
2890     (void *) QCOUNT, 0, sysctl_timer, "Q", "");
2891 SYSCTL_PROC(_kern_timer_longterm, OID_AUTO, scan_pauses,
2892     CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED,
2893     (void *) PAUSES, 0, sysctl_timer, "Q", "");
2894 
2895 #if  DEBUG
2896 SYSCTL_PROC(_kern_timer_longterm, OID_AUTO, enqueues,
2897     CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED,
2898     (void *) ENQUEUES, 0, sysctl_timer, "Q", "");
2899 SYSCTL_PROC(_kern_timer_longterm, OID_AUTO, dequeues,
2900     CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED,
2901     (void *) DEQUEUES, 0, sysctl_timer, "Q", "");
2902 SYSCTL_PROC(_kern_timer_longterm, OID_AUTO, escalates,
2903     CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED,
2904     (void *) ESCALATES, 0, sysctl_timer, "Q", "");
2905 SYSCTL_PROC(_kern_timer_longterm, OID_AUTO, scans,
2906     CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED,
2907     (void *) SCANS, 0, sysctl_timer, "Q", "");
2908 SYSCTL_PROC(_kern_timer_longterm, OID_AUTO, preempts,
2909     CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED,
2910     (void *) PREEMPTS, 0, sysctl_timer, "Q", "");
2911 SYSCTL_PROC(_kern_timer_longterm, OID_AUTO, latency,
2912     CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED,
2913     (void *) LATENCY, 0, sysctl_timer, "Q", "");
2914 SYSCTL_PROC(_kern_timer_longterm, OID_AUTO, latency_min,
2915     CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED,
2916     (void *) LATENCY_MIN, 0, sysctl_timer, "Q", "");
2917 SYSCTL_PROC(_kern_timer_longterm, OID_AUTO, latency_max,
2918     CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED,
2919     (void *) LATENCY_MAX, 0, sysctl_timer, "Q", "");
2920 #endif /* DEBUG */
2921 
2922 STATIC int
sysctl_usrstack(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)2923 sysctl_usrstack
2924 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2925 {
2926 	return sysctl_io_number(req, (int)req->p->user_stack, sizeof(int), NULL, NULL);
2927 }
2928 
2929 SYSCTL_PROC(_kern, KERN_USRSTACK32, usrstack,
2930     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
2931     0, 0, sysctl_usrstack, "I", "");
2932 
2933 STATIC int
sysctl_usrstack64(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)2934 sysctl_usrstack64
2935 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2936 {
2937 	return sysctl_io_number(req, req->p->user_stack, sizeof(req->p->user_stack), NULL, NULL);
2938 }
2939 
2940 SYSCTL_PROC(_kern, KERN_USRSTACK64, usrstack64,
2941     CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED,
2942     0, 0, sysctl_usrstack64, "Q", "");
2943 
2944 #if CONFIG_COREDUMP
2945 
2946 SYSCTL_STRING(_kern, KERN_COREFILE, corefile,
2947     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
2948     corefilename, sizeof(corefilename), "");
2949 
2950 STATIC int
sysctl_coredump(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)2951 sysctl_coredump
2952 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2953 {
2954 #ifdef SECURE_KERNEL
2955 	(void)req;
2956 	return ENOTSUP;
2957 #else
2958 	int new_value, changed;
2959 	int error = sysctl_io_number(req, do_coredump, sizeof(int), &new_value, &changed);
2960 	if (changed) {
2961 		if ((new_value == 0) || (new_value == 1)) {
2962 			do_coredump = new_value;
2963 		} else {
2964 			error = EINVAL;
2965 		}
2966 	}
2967 	return error;
2968 #endif
2969 }
2970 
2971 SYSCTL_PROC(_kern, KERN_COREDUMP, coredump,
2972     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
2973     0, 0, sysctl_coredump, "I", "");
2974 
2975 STATIC int
sysctl_suid_coredump(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)2976 sysctl_suid_coredump
2977 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2978 {
2979 #ifdef SECURE_KERNEL
2980 	(void)req;
2981 	return ENOTSUP;
2982 #else
2983 	int new_value, changed;
2984 	int error = sysctl_io_number(req, sugid_coredump, sizeof(int), &new_value, &changed);
2985 	if (changed) {
2986 		if ((new_value == 0) || (new_value == 1)) {
2987 			sugid_coredump = new_value;
2988 		} else {
2989 			error = EINVAL;
2990 		}
2991 	}
2992 	return error;
2993 #endif
2994 }
2995 
2996 SYSCTL_PROC(_kern, KERN_SUGID_COREDUMP, sugid_coredump,
2997     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
2998     0, 0, sysctl_suid_coredump, "I", "");
2999 
3000 #endif /* CONFIG_COREDUMP */
3001 
3002 STATIC int
sysctl_delayterm(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)3003 sysctl_delayterm
3004 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3005 {
3006 	struct proc *p = req->p;
3007 	int new_value, changed;
3008 	int error = sysctl_io_number(req, (req->p->p_lflag & P_LDELAYTERM)? 1: 0, sizeof(int), &new_value, &changed);
3009 	if (changed) {
3010 		proc_lock(p);
3011 		if (new_value) {
3012 			req->p->p_lflag |=  P_LDELAYTERM;
3013 		} else {
3014 			req->p->p_lflag &=  ~P_LDELAYTERM;
3015 		}
3016 		proc_unlock(p);
3017 	}
3018 	return error;
3019 }
3020 
3021 SYSCTL_PROC(_kern, KERN_PROCDELAYTERM, delayterm,
3022     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
3023     0, 0, sysctl_delayterm, "I", "");
3024 
3025 
3026 STATIC int
sysctl_rage_vnode(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)3027 sysctl_rage_vnode
3028 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3029 {
3030 	struct proc *p = req->p;
3031 	struct  uthread *ut;
3032 	int new_value, old_value, changed;
3033 	int error;
3034 
3035 	ut = current_uthread();
3036 
3037 	if (ut->uu_flag & UT_RAGE_VNODES) {
3038 		old_value = KERN_RAGE_THREAD;
3039 	} else if (p->p_lflag & P_LRAGE_VNODES) {
3040 		old_value = KERN_RAGE_PROC;
3041 	} else {
3042 		old_value = 0;
3043 	}
3044 
3045 	error = sysctl_io_number(req, old_value, sizeof(int), &new_value, &changed);
3046 
3047 	if ((error == 0) && (changed != 0)) {
3048 		switch (new_value) {
3049 		case KERN_RAGE_PROC:
3050 			proc_lock(p);
3051 			p->p_lflag |= P_LRAGE_VNODES;
3052 			proc_unlock(p);
3053 			break;
3054 		case KERN_UNRAGE_PROC:
3055 			proc_lock(p);
3056 			p->p_lflag &= ~P_LRAGE_VNODES;
3057 			proc_unlock(p);
3058 			break;
3059 
3060 		case KERN_RAGE_THREAD:
3061 			ut->uu_flag |= UT_RAGE_VNODES;
3062 			break;
3063 		case KERN_UNRAGE_THREAD:
3064 			ut = current_uthread();
3065 			ut->uu_flag &= ~UT_RAGE_VNODES;
3066 			break;
3067 		}
3068 	}
3069 	return error;
3070 }
3071 
3072 SYSCTL_PROC(_kern, KERN_RAGEVNODE, rage_vnode,
3073     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_LOCKED,
3074     0, 0, sysctl_rage_vnode, "I", "");
3075 
3076 /* XXX until filecoordinationd fixes a bit of inverted logic. */
3077 STATIC int
sysctl_vfsnspace(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)3078 sysctl_vfsnspace
3079 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3080 {
3081 	int old_value = 0, new_value, changed;
3082 
3083 	return sysctl_io_number(req, old_value, sizeof(int), &new_value,
3084 	           &changed);
3085 }
3086 
3087 SYSCTL_PROC(_kern, OID_AUTO, vfsnspace,
3088     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_LOCKED,
3089     0, 0, sysctl_vfsnspace, "I", "");
3090 
3091 /* XXX move this interface into libproc and remove this sysctl */
3092 STATIC int
sysctl_setthread_cpupercent(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)3093 sysctl_setthread_cpupercent
3094 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3095 {
3096 	int new_value, old_value;
3097 	int error = 0;
3098 	kern_return_t kret = KERN_SUCCESS;
3099 	uint8_t percent = 0;
3100 	int ms_refill = 0;
3101 
3102 	if (!req->newptr) {
3103 		return 0;
3104 	}
3105 
3106 	old_value = 0;
3107 
3108 	if ((error = sysctl_io_number(req, old_value, sizeof(old_value), &new_value, NULL)) != 0) {
3109 		return error;
3110 	}
3111 
3112 	percent = new_value & 0xff;                     /* low 8 bytes for perent */
3113 	ms_refill = (new_value >> 8) & 0xffffff;        /* upper 24bytes represent ms refill value */
3114 	if (percent > 100) {
3115 		return EINVAL;
3116 	}
3117 
3118 	/*
3119 	 * If the caller is specifying a percentage of 0, this will unset the CPU limit, if present.
3120 	 */
3121 	if ((kret = thread_set_cpulimit(THREAD_CPULIMIT_BLOCK, percent, ms_refill * (int)NSEC_PER_MSEC)) != 0) {
3122 		return EIO;
3123 	}
3124 
3125 	return 0;
3126 }
3127 
3128 SYSCTL_PROC(_kern, OID_AUTO, setthread_cpupercent,
3129     CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_ANYBODY,
3130     0, 0, sysctl_setthread_cpupercent, "I", "set thread cpu percentage limit");
3131 
3132 
3133 STATIC int
sysctl_kern_check_openevt(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)3134 sysctl_kern_check_openevt
3135 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3136 {
3137 	struct proc *p = req->p;
3138 	int new_value, old_value, changed;
3139 	int error;
3140 
3141 	if (p->p_flag & P_CHECKOPENEVT) {
3142 		old_value = KERN_OPENEVT_PROC;
3143 	} else {
3144 		old_value = 0;
3145 	}
3146 
3147 	error = sysctl_io_number(req, old_value, sizeof(int), &new_value, &changed);
3148 
3149 	if ((error == 0) && (changed != 0)) {
3150 		switch (new_value) {
3151 		case KERN_OPENEVT_PROC:
3152 			OSBitOrAtomic(P_CHECKOPENEVT, &p->p_flag);
3153 			break;
3154 
3155 		case KERN_UNOPENEVT_PROC:
3156 			OSBitAndAtomic(~((uint32_t)P_CHECKOPENEVT), &p->p_flag);
3157 			break;
3158 
3159 		default:
3160 			error = EINVAL;
3161 		}
3162 	}
3163 	return error;
3164 }
3165 
3166 SYSCTL_PROC(_kern, KERN_CHECKOPENEVT, check_openevt, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_LOCKED,
3167     0, 0, sysctl_kern_check_openevt, "I", "set the per-process check-open-evt flag");
3168 
3169 
3170 #if DEVELOPMENT || DEBUG
3171 STATIC int
sysctl_nx(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)3172 sysctl_nx
3173 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3174 {
3175 #ifdef SECURE_KERNEL
3176 	(void)req;
3177 	return ENOTSUP;
3178 #else
3179 	int new_value, changed;
3180 	int error;
3181 
3182 	error = sysctl_io_number(req, nx_enabled, sizeof(nx_enabled), &new_value, &changed);
3183 	if (error) {
3184 		return error;
3185 	}
3186 
3187 	if (changed) {
3188 #if defined(__x86_64__)
3189 		/*
3190 		 * Only allow setting if NX is supported on the chip
3191 		 */
3192 		if (!(cpuid_extfeatures() & CPUID_EXTFEATURE_XD)) {
3193 			return ENOTSUP;
3194 		}
3195 #endif
3196 		nx_enabled = new_value;
3197 	}
3198 	return error;
3199 #endif /* SECURE_KERNEL */
3200 }
3201 #endif
3202 
3203 #if DEVELOPMENT || DEBUG
3204 SYSCTL_PROC(_kern, KERN_NX_PROTECTION, nx,
3205     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
3206     0, 0, sysctl_nx, "I", "");
3207 #endif
3208 
3209 STATIC int
sysctl_loadavg(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)3210 sysctl_loadavg
3211 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3212 {
3213 	if (proc_is64bit(req->p)) {
3214 		struct user64_loadavg loadinfo64 = {};
3215 		fill_loadavg64(&averunnable, &loadinfo64);
3216 		return sysctl_io_opaque(req, &loadinfo64, sizeof(loadinfo64), NULL);
3217 	} else {
3218 		struct user32_loadavg loadinfo32 = {};
3219 		fill_loadavg32(&averunnable, &loadinfo32);
3220 		return sysctl_io_opaque(req, &loadinfo32, sizeof(loadinfo32), NULL);
3221 	}
3222 }
3223 
3224 SYSCTL_PROC(_vm, VM_LOADAVG, loadavg,
3225     CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED,
3226     0, 0, sysctl_loadavg, "S,loadavg", "");
3227 
3228 /*
3229  * Note:	Thread safe; vm_map_lock protects in  vm_toggle_entry_reuse()
3230  */
3231 STATIC int
sysctl_vm_toggle_address_reuse(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)3232 sysctl_vm_toggle_address_reuse(__unused struct sysctl_oid *oidp, __unused void *arg1,
3233     __unused int arg2, struct sysctl_req *req)
3234 {
3235 	int old_value = 0, new_value = 0, error = 0;
3236 
3237 	if (vm_toggle_entry_reuse( VM_TOGGLE_GETVALUE, &old_value )) {
3238 		return error;
3239 	}
3240 	error = sysctl_io_number(req, old_value, sizeof(int), &new_value, NULL);
3241 	if (!error) {
3242 		return vm_toggle_entry_reuse(new_value, NULL);
3243 	}
3244 	return error;
3245 }
3246 
3247 SYSCTL_PROC(_debug, OID_AUTO, toggle_address_reuse, CTLFLAG_ANYBODY | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, 0, 0, sysctl_vm_toggle_address_reuse, "I", "");
3248 
3249 #ifdef CONFIG_XNUPOST
3250 
3251 extern uint32_t xnupost_get_estimated_testdata_size(void);
3252 extern int xnupost_reset_all_tests(void);
3253 
3254 STATIC int
3255 sysctl_handle_xnupost_get_tests SYSCTL_HANDLER_ARGS
3256 {
3257 	/* fixup unused arguments warnings */
3258 	__unused int _oa2                  = arg2;
3259 	__unused void * _oa1               = arg1;
3260 	__unused struct sysctl_oid * _oidp = oidp;
3261 
3262 	int error          = 0;
3263 	user_addr_t oldp   = 0;
3264 	user_addr_t newp   = 0;
3265 	uint32_t usedbytes = 0;
3266 
3267 	oldp = req->oldptr;
3268 	newp = req->newptr;
3269 
3270 	if (newp) {
3271 		return ENOTSUP;
3272 	}
3273 
3274 	if ((void *)oldp == NULL) {
3275 		/* return estimated size for second call where info can be placed */
3276 		req->oldidx = xnupost_get_estimated_testdata_size();
3277 	} else {
3278 		error       = xnupost_export_testdata((void *)oldp, req->oldlen, &usedbytes);
3279 		req->oldidx = usedbytes;
3280 	}
3281 
3282 	return error;
3283 }
3284 
3285 SYSCTL_PROC(_debug,
3286     OID_AUTO,
3287     xnupost_get_tests,
3288     CTLFLAG_MASKED | CTLFLAG_ANYBODY | CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_LOCKED,
3289     0,
3290     0,
3291     sysctl_handle_xnupost_get_tests,
3292     "-",
3293     "read xnupost test data in kernel");
3294 
3295 STATIC int
3296 sysctl_debug_xnupost_ctl SYSCTL_HANDLER_ARGS
3297 {
3298 	/* fixup unused arguments warnings */
3299 	__unused int _oa2                  = arg2;
3300 	__unused void * _oa1               = arg1;
3301 	__unused struct sysctl_oid * _oidp = oidp;
3302 
3303 #define ARRCOUNT 4
3304 	/*
3305 	 * INPUT: ACTION,  PARAM1, PARAM2, PARAM3
3306 	 * OUTPUT: RESULTCODE, ADDITIONAL DATA
3307 	 */
3308 	int32_t outval[ARRCOUNT] = {0};
3309 	int32_t input[ARRCOUNT]  = {0};
3310 	int32_t out_size         = sizeof(outval);
3311 	int32_t in_size          = sizeof(input);
3312 	int error                = 0;
3313 
3314 	/* if this is NULL call to find out size, send out size info */
3315 	if (!req->newptr) {
3316 		goto out;
3317 	}
3318 
3319 	/* pull in provided value from userspace */
3320 	error = SYSCTL_IN(req, &input[0], in_size);
3321 	if (error) {
3322 		return error;
3323 	}
3324 
3325 	if (input[0] == XTCTL_RESET_TESTDATA) {
3326 		outval[0] = xnupost_reset_all_tests();
3327 		goto out;
3328 	}
3329 
3330 out:
3331 	error = SYSCTL_OUT(req, &outval[0], out_size);
3332 	return error;
3333 }
3334 
3335 SYSCTL_PROC(_debug,
3336     OID_AUTO,
3337     xnupost_testctl,
3338     CTLFLAG_MASKED | CTLFLAG_ANYBODY | CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_LOCKED,
3339     0,
3340     0,
3341     sysctl_debug_xnupost_ctl,
3342     "I",
3343     "xnupost control for kernel testing");
3344 
3345 extern void test_oslog_handleOSLogCtl(int32_t * in, int32_t * out, int32_t arraycount);
3346 
3347 STATIC int
sysctl_debug_test_oslog_ctl(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)3348 sysctl_debug_test_oslog_ctl(__unused struct sysctl_oid * oidp, __unused void * arg1, __unused int arg2, struct sysctl_req * req)
3349 {
3350 #define ARRCOUNT 4
3351 	int32_t outval[ARRCOUNT] = {0};
3352 	int32_t input[ARRCOUNT]  = {0};
3353 	int32_t size_outval      = sizeof(outval);
3354 	int32_t size_inval       = sizeof(input);
3355 	int32_t error;
3356 
3357 	/* if this is NULL call to find out size, send out size info */
3358 	if (!req->newptr) {
3359 		error = SYSCTL_OUT(req, &outval[0], size_outval);
3360 		return error;
3361 	}
3362 
3363 	/* pull in provided value from userspace */
3364 	error = SYSCTL_IN(req, &input[0], size_inval);
3365 	if (error) {
3366 		return error;
3367 	}
3368 
3369 	test_oslog_handleOSLogCtl(input, outval, ARRCOUNT);
3370 
3371 	error = SYSCTL_OUT(req, &outval[0], size_outval);
3372 
3373 	return error;
3374 }
3375 
3376 SYSCTL_PROC(_debug,
3377     OID_AUTO,
3378     test_OSLogCtl,
3379     CTLFLAG_MASKED | CTLFLAG_ANYBODY | CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_LOCKED,
3380     0,
3381     0,
3382     sysctl_debug_test_oslog_ctl,
3383     "I",
3384     "testing oslog in kernel");
3385 
3386 #include <mach/task.h>
3387 #include <mach/semaphore.h>
3388 
3389 static LCK_GRP_DECLARE(sysctl_debug_test_stackshot_owner_grp, "test-stackshot-owner-grp");
3390 static LCK_MTX_DECLARE(sysctl_debug_test_stackshot_owner_init_mtx,
3391     &sysctl_debug_test_stackshot_owner_grp);
3392 
3393 /* This is a sysctl for testing collection of owner info on a lock in kernel space. A multi-threaded
3394  * test from userland sets this sysctl in such a way that a thread blocks in kernel mode, and a
3395  * stackshot is taken to see if the owner of the lock can be identified.
3396  *
3397  * We can't return to userland with a kernel lock held, so be sure to unlock before we leave.
3398  * the semaphores allow us to artificially create cases where the lock is being held and the
3399  * thread is hanging / taking a long time to do something. */
3400 
3401 volatile char      sysctl_debug_test_stackshot_mtx_inited = 0;
3402 semaphore_t        sysctl_debug_test_stackshot_mutex_sem;
3403 lck_mtx_t          sysctl_debug_test_stackshot_owner_lck;
3404 
3405 #define SYSCTL_DEBUG_MTX_ACQUIRE_WAIT   1
3406 #define SYSCTL_DEBUG_MTX_ACQUIRE_NOWAIT 2
3407 #define SYSCTL_DEBUG_MTX_SIGNAL         3
3408 #define SYSCTL_DEBUG_MTX_TEARDOWN       4
3409 
3410 STATIC int
sysctl_debug_test_stackshot_mutex_owner(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)3411 sysctl_debug_test_stackshot_mutex_owner(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3412 {
3413 	long long option = -1;
3414 	/* if the user tries to read the sysctl, we tell them what the address of the lock is (to test against stackshot's output) */
3415 	long long mtx_unslid_addr = (long long)VM_KERNEL_UNSLIDE_OR_PERM(&sysctl_debug_test_stackshot_owner_lck);
3416 	int error = sysctl_io_number(req, mtx_unslid_addr, sizeof(long long), (void*)&option, NULL);
3417 
3418 	lck_mtx_lock(&sysctl_debug_test_stackshot_owner_init_mtx);
3419 	if (!sysctl_debug_test_stackshot_mtx_inited) {
3420 		lck_mtx_init(&sysctl_debug_test_stackshot_owner_lck,
3421 		    &sysctl_debug_test_stackshot_owner_grp,
3422 		    LCK_ATTR_NULL);
3423 		semaphore_create(kernel_task,
3424 		    &sysctl_debug_test_stackshot_mutex_sem,
3425 		    SYNC_POLICY_FIFO, 0);
3426 		sysctl_debug_test_stackshot_mtx_inited = 1;
3427 	}
3428 	lck_mtx_unlock(&sysctl_debug_test_stackshot_owner_init_mtx);
3429 
3430 	if (!error) {
3431 		switch (option) {
3432 		case SYSCTL_DEBUG_MTX_ACQUIRE_NOWAIT:
3433 			lck_mtx_lock(&sysctl_debug_test_stackshot_owner_lck);
3434 			lck_mtx_unlock(&sysctl_debug_test_stackshot_owner_lck);
3435 			break;
3436 		case SYSCTL_DEBUG_MTX_ACQUIRE_WAIT:
3437 			lck_mtx_lock(&sysctl_debug_test_stackshot_owner_lck);
3438 			semaphore_wait(sysctl_debug_test_stackshot_mutex_sem);
3439 			lck_mtx_unlock(&sysctl_debug_test_stackshot_owner_lck);
3440 			break;
3441 		case SYSCTL_DEBUG_MTX_SIGNAL:
3442 			semaphore_signal(sysctl_debug_test_stackshot_mutex_sem);
3443 			break;
3444 		case SYSCTL_DEBUG_MTX_TEARDOWN:
3445 			lck_mtx_lock(&sysctl_debug_test_stackshot_owner_init_mtx);
3446 
3447 			lck_mtx_destroy(&sysctl_debug_test_stackshot_owner_lck,
3448 			    &sysctl_debug_test_stackshot_owner_grp);
3449 			semaphore_destroy(kernel_task,
3450 			    sysctl_debug_test_stackshot_mutex_sem);
3451 			sysctl_debug_test_stackshot_mtx_inited = 0;
3452 
3453 			lck_mtx_unlock(&sysctl_debug_test_stackshot_owner_init_mtx);
3454 			break;
3455 		case -1:         /* user just wanted to read the value, so do nothing */
3456 			break;
3457 		default:
3458 			error = EINVAL;
3459 			break;
3460 		}
3461 	}
3462 	return error;
3463 }
3464 
3465 /* we can't return to userland with a kernel rwlock held, so be sure to unlock before we leave.
3466  * the semaphores allow us to artificially create cases where the lock is being held and the
3467  * thread is hanging / taking a long time to do something. */
3468 
3469 SYSCTL_PROC(_debug,
3470     OID_AUTO,
3471     test_MutexOwnerCtl,
3472     CTLFLAG_MASKED | CTLFLAG_ANYBODY | CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
3473     0,
3474     0,
3475     sysctl_debug_test_stackshot_mutex_owner,
3476     "-",
3477     "Testing mutex owner in kernel");
3478 
3479 volatile char sysctl_debug_test_stackshot_rwlck_inited = 0;
3480 lck_rw_t      sysctl_debug_test_stackshot_owner_rwlck;
3481 semaphore_t   sysctl_debug_test_stackshot_rwlck_sem;
3482 
3483 #define SYSCTL_DEBUG_KRWLCK_RACQUIRE_NOWAIT 1
3484 #define SYSCTL_DEBUG_KRWLCK_RACQUIRE_WAIT   2
3485 #define SYSCTL_DEBUG_KRWLCK_WACQUIRE_NOWAIT 3
3486 #define SYSCTL_DEBUG_KRWLCK_WACQUIRE_WAIT   4
3487 #define SYSCTL_DEBUG_KRWLCK_SIGNAL          5
3488 #define SYSCTL_DEBUG_KRWLCK_TEARDOWN        6
3489 
3490 STATIC int
sysctl_debug_test_stackshot_rwlck_owner(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)3491 sysctl_debug_test_stackshot_rwlck_owner(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3492 {
3493 	long long option = -1;
3494 	/* if the user tries to read the sysctl, we tell them what the address of the lock is
3495 	 * (to test against stackshot's output) */
3496 	long long rwlck_unslid_addr = (long long)VM_KERNEL_UNSLIDE_OR_PERM(&sysctl_debug_test_stackshot_owner_rwlck);
3497 	int error = sysctl_io_number(req, rwlck_unslid_addr, sizeof(long long), (void*)&option, NULL);
3498 
3499 	lck_mtx_lock(&sysctl_debug_test_stackshot_owner_init_mtx);
3500 	if (!sysctl_debug_test_stackshot_rwlck_inited) {
3501 		lck_rw_init(&sysctl_debug_test_stackshot_owner_rwlck,
3502 		    &sysctl_debug_test_stackshot_owner_grp,
3503 		    LCK_ATTR_NULL);
3504 		semaphore_create(kernel_task,
3505 		    &sysctl_debug_test_stackshot_rwlck_sem,
3506 		    SYNC_POLICY_FIFO,
3507 		    0);
3508 		sysctl_debug_test_stackshot_rwlck_inited = 1;
3509 	}
3510 	lck_mtx_unlock(&sysctl_debug_test_stackshot_owner_init_mtx);
3511 
3512 	if (!error) {
3513 		switch (option) {
3514 		case SYSCTL_DEBUG_KRWLCK_RACQUIRE_NOWAIT:
3515 			lck_rw_lock(&sysctl_debug_test_stackshot_owner_rwlck, LCK_RW_TYPE_SHARED);
3516 			lck_rw_unlock(&sysctl_debug_test_stackshot_owner_rwlck, LCK_RW_TYPE_SHARED);
3517 			break;
3518 		case SYSCTL_DEBUG_KRWLCK_RACQUIRE_WAIT:
3519 			lck_rw_lock(&sysctl_debug_test_stackshot_owner_rwlck, LCK_RW_TYPE_SHARED);
3520 			semaphore_wait(sysctl_debug_test_stackshot_rwlck_sem);
3521 			lck_rw_unlock(&sysctl_debug_test_stackshot_owner_rwlck, LCK_RW_TYPE_SHARED);
3522 			break;
3523 		case SYSCTL_DEBUG_KRWLCK_WACQUIRE_NOWAIT:
3524 			lck_rw_lock(&sysctl_debug_test_stackshot_owner_rwlck, LCK_RW_TYPE_EXCLUSIVE);
3525 			lck_rw_unlock(&sysctl_debug_test_stackshot_owner_rwlck, LCK_RW_TYPE_EXCLUSIVE);
3526 			break;
3527 		case SYSCTL_DEBUG_KRWLCK_WACQUIRE_WAIT:
3528 			lck_rw_lock(&sysctl_debug_test_stackshot_owner_rwlck, LCK_RW_TYPE_EXCLUSIVE);
3529 			semaphore_wait(sysctl_debug_test_stackshot_rwlck_sem);
3530 			lck_rw_unlock(&sysctl_debug_test_stackshot_owner_rwlck, LCK_RW_TYPE_EXCLUSIVE);
3531 			break;
3532 		case SYSCTL_DEBUG_KRWLCK_SIGNAL:
3533 			semaphore_signal(sysctl_debug_test_stackshot_rwlck_sem);
3534 			break;
3535 		case SYSCTL_DEBUG_KRWLCK_TEARDOWN:
3536 			lck_mtx_lock(&sysctl_debug_test_stackshot_owner_init_mtx);
3537 
3538 			lck_rw_destroy(&sysctl_debug_test_stackshot_owner_rwlck,
3539 			    &sysctl_debug_test_stackshot_owner_grp);
3540 			semaphore_destroy(kernel_task,
3541 			    sysctl_debug_test_stackshot_rwlck_sem);
3542 			sysctl_debug_test_stackshot_rwlck_inited = 0;
3543 
3544 			lck_mtx_unlock(&sysctl_debug_test_stackshot_owner_init_mtx);
3545 			break;
3546 		case -1:         /* user just wanted to read the value, so do nothing */
3547 			break;
3548 		default:
3549 			error = EINVAL;
3550 			break;
3551 		}
3552 	}
3553 	return error;
3554 }
3555 
3556 
3557 SYSCTL_PROC(_debug,
3558     OID_AUTO,
3559     test_RWLockOwnerCtl,
3560     CTLFLAG_MASKED | CTLFLAG_ANYBODY | CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
3561     0,
3562     0,
3563     sysctl_debug_test_stackshot_rwlck_owner,
3564     "-",
3565     "Testing rwlock owner in kernel");
3566 #endif /* !CONFIG_XNUPOST */
3567 
3568 STATIC int
sysctl_swapusage(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)3569 sysctl_swapusage
3570 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3571 {
3572 	int                     error;
3573 	uint64_t                swap_total;
3574 	uint64_t                swap_avail;
3575 	vm_size_t               swap_pagesize;
3576 	boolean_t               swap_encrypted;
3577 	struct xsw_usage        xsu = {};
3578 
3579 	error = macx_swapinfo(&swap_total,
3580 	    &swap_avail,
3581 	    &swap_pagesize,
3582 	    &swap_encrypted);
3583 	if (error) {
3584 		return error;
3585 	}
3586 
3587 	xsu.xsu_total = swap_total;
3588 	xsu.xsu_avail = swap_avail;
3589 	xsu.xsu_used = swap_total - swap_avail;
3590 	xsu.xsu_pagesize = (u_int32_t)MIN(swap_pagesize, UINT32_MAX);
3591 	xsu.xsu_encrypted = swap_encrypted;
3592 	return sysctl_io_opaque(req, &xsu, sizeof(xsu), NULL);
3593 }
3594 
3595 
3596 
3597 SYSCTL_PROC(_vm, VM_SWAPUSAGE, swapusage,
3598     CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED,
3599     0, 0, sysctl_swapusage, "S,xsw_usage", "");
3600 
3601 #if DEVELOPMENT || DEBUG
3602 extern int vm_num_swap_files_config;
3603 extern int vm_num_swap_files;
3604 extern lck_mtx_t vm_swap_data_lock;
3605 #define VM_MAX_SWAP_FILE_NUM            100
3606 
3607 static int
3608 sysctl_vm_config_num_swap_files SYSCTL_HANDLER_ARGS
3609 {
3610 #pragma unused(arg1, arg2)
3611 	int error = 0, val = vm_num_swap_files_config;
3612 
3613 	error = sysctl_handle_int(oidp, &val, 0, req);
3614 	if (error || !req->newptr) {
3615 		goto out;
3616 	}
3617 
3618 	if (!VM_CONFIG_SWAP_IS_ACTIVE && !VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) {
3619 		printf("Swap is disabled\n");
3620 		error = EINVAL;
3621 		goto out;
3622 	}
3623 
3624 	lck_mtx_lock(&vm_swap_data_lock);
3625 
3626 	if (val < vm_num_swap_files) {
3627 		printf("Cannot configure fewer swap files than already exist.\n");
3628 		error = EINVAL;
3629 		lck_mtx_unlock(&vm_swap_data_lock);
3630 		goto out;
3631 	}
3632 
3633 	if (val > VM_MAX_SWAP_FILE_NUM) {
3634 		printf("Capping number of swap files to upper bound.\n");
3635 		val = VM_MAX_SWAP_FILE_NUM;
3636 	}
3637 
3638 	vm_num_swap_files_config = val;
3639 	lck_mtx_unlock(&vm_swap_data_lock);
3640 out:
3641 
3642 	return 0;
3643 }
3644 
3645 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", "");
3646 #endif /* DEVELOPMENT || DEBUG */
3647 
3648 /* this kernel does NOT implement shared_region_make_private_np() */
3649 SYSCTL_INT(_kern, KERN_SHREG_PRIVATIZABLE, shreg_private,
3650     CTLFLAG_RD | CTLFLAG_LOCKED,
3651     (int *)NULL, 0, "");
3652 
3653 STATIC int
fetch_process_cputype(proc_t cur_proc,int * name,u_int namelen,cpu_type_t * cputype)3654 fetch_process_cputype(
3655 	proc_t cur_proc,
3656 	int *name,
3657 	u_int namelen,
3658 	cpu_type_t *cputype)
3659 {
3660 	proc_t p = PROC_NULL;
3661 	int refheld = 0;
3662 	cpu_type_t ret = 0;
3663 	int error = 0;
3664 
3665 	if (namelen == 0) {
3666 		p = cur_proc;
3667 	} else if (namelen == 1) {
3668 		p = proc_find(name[0]);
3669 		if (p == NULL) {
3670 			return EINVAL;
3671 		}
3672 		refheld = 1;
3673 	} else {
3674 		error = EINVAL;
3675 		goto out;
3676 	}
3677 
3678 	ret = cpu_type() & ~CPU_ARCH_MASK;
3679 	if (IS_64BIT_PROCESS(p)) {
3680 		ret |= CPU_ARCH_ABI64;
3681 	}
3682 
3683 	*cputype = ret;
3684 
3685 	if (refheld != 0) {
3686 		proc_rele(p);
3687 	}
3688 out:
3689 	return error;
3690 }
3691 
3692 
3693 STATIC int
sysctl_sysctl_native(__unused struct sysctl_oid * oidp,void * arg1,int arg2,struct sysctl_req * req)3694 sysctl_sysctl_native(__unused struct sysctl_oid *oidp, void *arg1, int arg2,
3695     struct sysctl_req *req)
3696 {
3697 	int error;
3698 	cpu_type_t proc_cputype = 0;
3699 	if ((error = fetch_process_cputype(req->p, (int *)arg1, arg2, &proc_cputype)) != 0) {
3700 		return error;
3701 	}
3702 	int res = 1;
3703 	if ((proc_cputype & ~CPU_ARCH_MASK) != (cpu_type() & ~CPU_ARCH_MASK)) {
3704 		res = 0;
3705 	}
3706 	return SYSCTL_OUT(req, &res, sizeof(res));
3707 }
3708 SYSCTL_PROC(_sysctl, OID_AUTO, proc_native, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, sysctl_sysctl_native, "I", "proc_native");
3709 
3710 STATIC int
sysctl_sysctl_cputype(__unused struct sysctl_oid * oidp,void * arg1,int arg2,struct sysctl_req * req)3711 sysctl_sysctl_cputype(__unused struct sysctl_oid *oidp, void *arg1, int arg2,
3712     struct sysctl_req *req)
3713 {
3714 	int error;
3715 	cpu_type_t proc_cputype = 0;
3716 	if ((error = fetch_process_cputype(req->p, (int *)arg1, arg2, &proc_cputype)) != 0) {
3717 		return error;
3718 	}
3719 	return SYSCTL_OUT(req, &proc_cputype, sizeof(proc_cputype));
3720 }
3721 SYSCTL_PROC(_sysctl, OID_AUTO, proc_cputype, CTLTYPE_NODE | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, sysctl_sysctl_cputype, "I", "proc_cputype");
3722 
3723 STATIC int
sysctl_safeboot(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)3724 sysctl_safeboot
3725 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3726 {
3727 	return sysctl_io_number(req, boothowto & RB_SAFEBOOT ? 1 : 0, sizeof(int), NULL, NULL);
3728 }
3729 
3730 SYSCTL_PROC(_kern, KERN_SAFEBOOT, safeboot,
3731     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
3732     0, 0, sysctl_safeboot, "I", "");
3733 
3734 STATIC int
sysctl_singleuser(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)3735 sysctl_singleuser
3736 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3737 {
3738 	return sysctl_io_number(req, boothowto & RB_SINGLE ? 1 : 0, sizeof(int), NULL, NULL);
3739 }
3740 
3741 SYSCTL_PROC(_kern, OID_AUTO, singleuser,
3742     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
3743     0, 0, sysctl_singleuser, "I", "");
3744 
3745 STATIC int
sysctl_minimalboot(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)3746 sysctl_minimalboot
3747 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3748 {
3749 	return sysctl_io_number(req, minimalboot, sizeof(int), NULL, NULL);
3750 }
3751 
3752 SYSCTL_PROC(_kern, OID_AUTO, minimalboot,
3753     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
3754     0, 0, sysctl_minimalboot, "I", "");
3755 
3756 /*
3757  * Controls for debugging affinity sets - see osfmk/kern/affinity.c
3758  */
3759 extern boolean_t        affinity_sets_enabled;
3760 extern int              affinity_sets_mapping;
3761 
3762 SYSCTL_INT(_kern, OID_AUTO, affinity_sets_enabled,
3763     CTLFLAG_RW | CTLFLAG_LOCKED, (int *) &affinity_sets_enabled, 0, "hinting enabled");
3764 SYSCTL_INT(_kern, OID_AUTO, affinity_sets_mapping,
3765     CTLFLAG_RW | CTLFLAG_LOCKED, &affinity_sets_mapping, 0, "mapping policy");
3766 
3767 /*
3768  * Boolean indicating if KASLR is active.
3769  */
3770 STATIC int
sysctl_slide(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)3771 sysctl_slide
3772 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3773 {
3774 	uint32_t        slide;
3775 
3776 	slide = vm_kernel_slide ? 1 : 0;
3777 
3778 	return sysctl_io_number( req, slide, sizeof(int), NULL, NULL);
3779 }
3780 
3781 SYSCTL_PROC(_kern, OID_AUTO, slide,
3782     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
3783     0, 0, sysctl_slide, "I", "");
3784 
3785 #if DEBUG || DEVELOPMENT
3786 #if defined(__arm64__)
3787 extern vm_offset_t segTEXTEXECB;
3788 
3789 static int
3790 sysctl_kernel_text_exec_base_slide SYSCTL_HANDLER_ARGS
3791 {
3792 #pragma unused(arg1, arg2, oidp)
3793 	unsigned long slide = 0;
3794 	kc_format_t kc_format;
3795 
3796 	PE_get_primary_kc_format(&kc_format);
3797 
3798 	if (kc_format == KCFormatFileset) {
3799 		void *kch = PE_get_kc_header(KCKindPrimary);
3800 		slide = (unsigned long)segTEXTEXECB - (unsigned long)kch + vm_kernel_slide;
3801 	}
3802 	return SYSCTL_OUT(req, &slide, sizeof(slide));
3803 }
3804 
3805 SYSCTL_QUAD(_kern, OID_AUTO, kernel_slide, CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED, &vm_kernel_slide, "");
3806 SYSCTL_QUAD(_kern, OID_AUTO, kernel_text_exec_base, CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED, &segTEXTEXECB, "");
3807 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", "");
3808 #endif /* defined(__arm64__) */
3809 
3810 /* User address of the PFZ */
3811 extern user32_addr_t commpage_text32_location;
3812 extern user64_addr_t commpage_text64_location;
3813 
3814 STATIC int
3815 sysctl_pfz_start SYSCTL_HANDLER_ARGS
3816 {
3817 #pragma unused(oidp, arg1, arg2)
3818 
3819 #ifdef __LP64__
3820 	return sysctl_io_number(req, commpage_text64_location, sizeof(user64_addr_t), NULL, NULL);
3821 #else
3822 	return sysctl_io_number(req, commpage_text32_location, sizeof(user32_addr_t), NULL, NULL);
3823 #endif
3824 }
3825 
3826 SYSCTL_PROC(_kern, OID_AUTO, pfz,
3827     CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED | CTLFLAG_MASKED,
3828     0, 0, sysctl_pfz_start, "I", "");
3829 #endif
3830 
3831 
3832 /*
3833  * Limit on total memory users can wire.
3834  *
3835  * vm_global_user_wire_limit - system wide limit on wired memory from all processes combined.
3836  *
3837  * vm_per_task_user_wire_limit - per address space limit on wired memory.  This puts a cap on the process's rlimit value.
3838  *
3839  * These values are initialized to reasonable defaults at boot time based on the available physical memory in
3840  * kmem_init().
3841  *
3842  * All values are in bytes.
3843  */
3844 
3845 vm_map_size_t   vm_global_user_wire_limit;
3846 vm_map_size_t   vm_per_task_user_wire_limit;
3847 extern uint64_t max_mem_actual, max_mem;
3848 
3849 uint64_t        vm_add_wire_count_over_global_limit;
3850 uint64_t        vm_add_wire_count_over_user_limit;
3851 /*
3852  * We used to have a global in the kernel called vm_global_no_user_wire_limit which was the inverse
3853  * of vm_global_user_wire_limit. But maintaining both of those is silly, and vm_global_user_wire_limit is the
3854  * real limit.
3855  * This function is for backwards compatibility with userspace
3856  * since we exposed the old global via a sysctl.
3857  */
3858 STATIC int
sysctl_global_no_user_wire_amount(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)3859 sysctl_global_no_user_wire_amount(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3860 {
3861 	vm_map_size_t old_value;
3862 	vm_map_size_t new_value;
3863 	int changed;
3864 	int error;
3865 	uint64_t config_memsize = max_mem;
3866 #if defined(XNU_TARGET_OS_OSX)
3867 	config_memsize = max_mem_actual;
3868 #endif /* defined(XNU_TARGET_OS_OSX) */
3869 
3870 	old_value = (vm_map_size_t)(config_memsize - vm_global_user_wire_limit);
3871 	error = sysctl_io_number(req, old_value, sizeof(vm_map_size_t), &new_value, &changed);
3872 	if (changed) {
3873 		if ((uint64_t)new_value > config_memsize) {
3874 			error = EINVAL;
3875 		} else {
3876 			vm_global_user_wire_limit = (vm_map_size_t)(config_memsize - new_value);
3877 		}
3878 	}
3879 	return error;
3880 }
3881 /*
3882  * There needs to be a more automatic/elegant way to do this
3883  */
3884 #if defined(__ARM__)
3885 SYSCTL_INT(_vm, OID_AUTO, global_user_wire_limit, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_global_user_wire_limit, 0, "");
3886 SYSCTL_INT(_vm, OID_AUTO, user_wire_limit, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_per_task_user_wire_limit, 0, "");
3887 SYSCTL_PROC(_vm, OID_AUTO, global_no_user_wire_amount, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, 0, 0, &sysctl_global_no_user_wire_amount, "I", "");
3888 #else
3889 SYSCTL_QUAD(_vm, OID_AUTO, global_user_wire_limit, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_global_user_wire_limit, "");
3890 SYSCTL_QUAD(_vm, OID_AUTO, user_wire_limit, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_per_task_user_wire_limit, "");
3891 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", "");
3892 #endif
3893 
3894 /*
3895  * Relaxed atomic RW of a 64bit value via sysctl.
3896  */
3897 STATIC int
sysctl_r_64bit_atomic(uint64_t * ptr,struct sysctl_req * req)3898 sysctl_r_64bit_atomic(uint64_t *ptr, struct sysctl_req *req)
3899 {
3900 	uint64_t old_value;
3901 	uint64_t new_value;
3902 	int error;
3903 
3904 	old_value = os_atomic_load_wide(ptr, relaxed);
3905 	error = sysctl_io_number(req, old_value, sizeof(vm_map_size_t), &new_value, NULL);
3906 	return error;
3907 }
3908 STATIC int
sysctl_add_wire_count_over_global_limit(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)3909 sysctl_add_wire_count_over_global_limit(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3910 {
3911 	return sysctl_r_64bit_atomic(&vm_add_wire_count_over_global_limit, req);
3912 }
3913 STATIC int
sysctl_add_wire_count_over_user_limit(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)3914 sysctl_add_wire_count_over_user_limit(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
3915 {
3916 	return sysctl_r_64bit_atomic(&vm_add_wire_count_over_user_limit, req);
3917 }
3918 
3919 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", "");
3920 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", "");
3921 
3922 #if DEVELOPMENT || DEBUG
3923 /* These sysctls are used to test the wired limit. */
3924 extern unsigned int    vm_page_wire_count;
3925 extern uint32_t        vm_lopage_free_count;
3926 SYSCTL_INT(_vm, OID_AUTO, page_wire_count, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_page_wire_count, 0, "");
3927 SYSCTL_INT(_vm, OID_AUTO, lopage_free_count, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_lopage_free_count, 0, "");
3928 
3929 /*
3930  * Setting the per task variable exclude_physfootprint_ledger to 1 will allow the calling task to exclude memory entries that are
3931  * tagged by VM_LEDGER_TAG_DEFAULT and flagged by VM_LEDGER_FLAG_EXCLUDE_FOOTPRINT_DEBUG from its phys_footprint ledger.
3932  */
3933 
3934 STATIC int
sysctl_rw_task_no_footprint_for_debug(struct sysctl_oid * oidp __unused,void * arg1 __unused,int arg2 __unused,struct sysctl_req * req)3935 sysctl_rw_task_no_footprint_for_debug(struct sysctl_oid *oidp __unused, void *arg1 __unused, int arg2 __unused, struct sysctl_req *req)
3936 {
3937 	int error;
3938 	int value;
3939 	proc_t p = current_proc();
3940 
3941 	if (req->newptr) {
3942 		// Write request
3943 		error = SYSCTL_IN(req, &value, sizeof(value));
3944 		if (!error) {
3945 			if (value == 1) {
3946 				task_set_no_footprint_for_debug(p->task, TRUE);
3947 			} else if (value == 0) {
3948 				task_set_no_footprint_for_debug(p->task, FALSE);
3949 			} else {
3950 				error = EINVAL;
3951 			}
3952 		}
3953 	} else {
3954 		// Read request
3955 		value = task_get_no_footprint_for_debug(p->task);
3956 		error = SYSCTL_OUT(req, &value, sizeof(value));
3957 	}
3958 	return error;
3959 }
3960 
3961 SYSCTL_PROC(_vm, OID_AUTO, task_no_footprint_for_debug,
3962     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
3963     0, 0, &sysctl_rw_task_no_footprint_for_debug, "I", "Allow debug memory to be excluded from this task's memory footprint (debug only)");
3964 
3965 #endif /* DEVELOPMENT || DEBUG */
3966 
3967 
3968 extern int vm_map_copy_overwrite_aligned_src_not_internal;
3969 extern int vm_map_copy_overwrite_aligned_src_not_symmetric;
3970 extern int vm_map_copy_overwrite_aligned_src_large;
3971 SYSCTL_INT(_vm, OID_AUTO, vm_copy_src_not_internal, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_map_copy_overwrite_aligned_src_not_internal, 0, "");
3972 SYSCTL_INT(_vm, OID_AUTO, vm_copy_src_not_symmetric, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_map_copy_overwrite_aligned_src_not_symmetric, 0, "");
3973 SYSCTL_INT(_vm, OID_AUTO, vm_copy_src_large, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_map_copy_overwrite_aligned_src_large, 0, "");
3974 
3975 
3976 extern uint32_t vm_page_external_count;
3977 
3978 SYSCTL_INT(_vm, OID_AUTO, vm_page_external_count, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_page_external_count, 0, "");
3979 
3980 SYSCTL_INT(_vm, OID_AUTO, vm_page_filecache_min, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_pageout_state.vm_page_filecache_min, 0, "");
3981 SYSCTL_INT(_vm, OID_AUTO, vm_page_xpmapped_min, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_pageout_state.vm_page_xpmapped_min, 0, "");
3982 
3983 #if DEVELOPMENT || DEBUG
3984 SYSCTL_INT(_vm, OID_AUTO, vm_page_filecache_min_divisor, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_pageout_state.vm_page_filecache_min_divisor, 0, "");
3985 SYSCTL_INT(_vm, OID_AUTO, vm_page_xpmapped_min_divisor, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_pageout_state.vm_page_xpmapped_min_divisor, 0, "");
3986 #endif
3987 
3988 extern int      vm_compressor_mode;
3989 extern int      vm_compressor_is_active;
3990 extern int      vm_compressor_available;
3991 extern uint32_t c_seg_bufsize;
3992 extern uint32_t vm_ripe_target_age;
3993 extern uint32_t swapout_target_age;
3994 extern int64_t  compressor_bytes_used;
3995 extern int64_t  c_segment_input_bytes;
3996 extern int64_t  c_segment_compressed_bytes;
3997 extern uint32_t compressor_eval_period_in_msecs;
3998 extern uint32_t compressor_sample_min_in_msecs;
3999 extern uint32_t compressor_sample_max_in_msecs;
4000 extern uint32_t compressor_thrashing_threshold_per_10msecs;
4001 extern uint32_t compressor_thrashing_min_per_10msecs;
4002 extern uint32_t vm_compressor_time_thread;
4003 
4004 #if DEVELOPMENT || DEBUG
4005 extern uint32_t vm_compressor_minorcompact_threshold_divisor;
4006 extern uint32_t vm_compressor_majorcompact_threshold_divisor;
4007 extern uint32_t vm_compressor_unthrottle_threshold_divisor;
4008 extern uint32_t vm_compressor_catchup_threshold_divisor;
4009 
4010 extern uint32_t vm_compressor_minorcompact_threshold_divisor_overridden;
4011 extern uint32_t vm_compressor_majorcompact_threshold_divisor_overridden;
4012 extern uint32_t vm_compressor_unthrottle_threshold_divisor_overridden;
4013 extern uint32_t vm_compressor_catchup_threshold_divisor_overridden;
4014 
4015 extern vmct_stats_t vmct_stats;
4016 
4017 
4018 STATIC int
sysctl_minorcompact_threshold_divisor(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)4019 sysctl_minorcompact_threshold_divisor(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
4020 {
4021 	int new_value, changed;
4022 	int error = sysctl_io_number(req, vm_compressor_minorcompact_threshold_divisor, sizeof(int), &new_value, &changed);
4023 
4024 	if (changed) {
4025 		vm_compressor_minorcompact_threshold_divisor = new_value;
4026 		vm_compressor_minorcompact_threshold_divisor_overridden = 1;
4027 	}
4028 	return error;
4029 }
4030 
4031 SYSCTL_PROC(_vm, OID_AUTO, compressor_minorcompact_threshold_divisor,
4032     CTLTYPE_INT | CTLFLAG_LOCKED | CTLFLAG_RW,
4033     0, 0, sysctl_minorcompact_threshold_divisor, "I", "");
4034 
4035 
4036 STATIC int
sysctl_majorcompact_threshold_divisor(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)4037 sysctl_majorcompact_threshold_divisor(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
4038 {
4039 	int new_value, changed;
4040 	int error = sysctl_io_number(req, vm_compressor_majorcompact_threshold_divisor, sizeof(int), &new_value, &changed);
4041 
4042 	if (changed) {
4043 		vm_compressor_majorcompact_threshold_divisor = new_value;
4044 		vm_compressor_majorcompact_threshold_divisor_overridden = 1;
4045 	}
4046 	return error;
4047 }
4048 
4049 SYSCTL_PROC(_vm, OID_AUTO, compressor_majorcompact_threshold_divisor,
4050     CTLTYPE_INT | CTLFLAG_LOCKED | CTLFLAG_RW,
4051     0, 0, sysctl_majorcompact_threshold_divisor, "I", "");
4052 
4053 
4054 STATIC int
sysctl_unthrottle_threshold_divisor(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)4055 sysctl_unthrottle_threshold_divisor(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
4056 {
4057 	int new_value, changed;
4058 	int error = sysctl_io_number(req, vm_compressor_unthrottle_threshold_divisor, sizeof(int), &new_value, &changed);
4059 
4060 	if (changed) {
4061 		vm_compressor_unthrottle_threshold_divisor = new_value;
4062 		vm_compressor_unthrottle_threshold_divisor_overridden = 1;
4063 	}
4064 	return error;
4065 }
4066 
4067 SYSCTL_PROC(_vm, OID_AUTO, compressor_unthrottle_threshold_divisor,
4068     CTLTYPE_INT | CTLFLAG_LOCKED | CTLFLAG_RW,
4069     0, 0, sysctl_unthrottle_threshold_divisor, "I", "");
4070 
4071 
4072 STATIC int
sysctl_catchup_threshold_divisor(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)4073 sysctl_catchup_threshold_divisor(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
4074 {
4075 	int new_value, changed;
4076 	int error = sysctl_io_number(req, vm_compressor_catchup_threshold_divisor, sizeof(int), &new_value, &changed);
4077 
4078 	if (changed) {
4079 		vm_compressor_catchup_threshold_divisor = new_value;
4080 		vm_compressor_catchup_threshold_divisor_overridden = 1;
4081 	}
4082 	return error;
4083 }
4084 
4085 SYSCTL_PROC(_vm, OID_AUTO, compressor_catchup_threshold_divisor,
4086     CTLTYPE_INT | CTLFLAG_LOCKED | CTLFLAG_RW,
4087     0, 0, sysctl_catchup_threshold_divisor, "I", "");
4088 #endif
4089 
4090 
4091 SYSCTL_QUAD(_vm, OID_AUTO, compressor_input_bytes, CTLFLAG_RD | CTLFLAG_LOCKED, &c_segment_input_bytes, "");
4092 SYSCTL_QUAD(_vm, OID_AUTO, compressor_compressed_bytes, CTLFLAG_RD | CTLFLAG_LOCKED, &c_segment_compressed_bytes, "");
4093 SYSCTL_QUAD(_vm, OID_AUTO, compressor_bytes_used, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_bytes_used, "");
4094 
4095 SYSCTL_INT(_vm, OID_AUTO, compressor_mode, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_compressor_mode, 0, "");
4096 SYSCTL_INT(_vm, OID_AUTO, compressor_is_active, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_compressor_is_active, 0, "");
4097 SYSCTL_INT(_vm, OID_AUTO, compressor_swapout_target_age, CTLFLAG_RD | CTLFLAG_LOCKED, &swapout_target_age, 0, "");
4098 SYSCTL_INT(_vm, OID_AUTO, compressor_available, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_compressor_available, 0, "");
4099 SYSCTL_INT(_vm, OID_AUTO, compressor_segment_buffer_size, CTLFLAG_RD | CTLFLAG_LOCKED, &c_seg_bufsize, 0, "");
4100 
4101 extern int min_csegs_per_major_compaction;
4102 SYSCTL_INT(_vm, OID_AUTO, compressor_min_csegs_per_major_compaction, CTLFLAG_RW | CTLFLAG_LOCKED, &min_csegs_per_major_compaction, 0, "");
4103 
4104 SYSCTL_INT(_vm, OID_AUTO, vm_ripe_target_age_in_secs, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_ripe_target_age, 0, "");
4105 
4106 SYSCTL_INT(_vm, OID_AUTO, compressor_eval_period_in_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &compressor_eval_period_in_msecs, 0, "");
4107 SYSCTL_INT(_vm, OID_AUTO, compressor_sample_min_in_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &compressor_sample_min_in_msecs, 0, "");
4108 SYSCTL_INT(_vm, OID_AUTO, compressor_sample_max_in_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &compressor_sample_max_in_msecs, 0, "");
4109 SYSCTL_INT(_vm, OID_AUTO, compressor_thrashing_threshold_per_10msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &compressor_thrashing_threshold_per_10msecs, 0, "");
4110 SYSCTL_INT(_vm, OID_AUTO, compressor_thrashing_min_per_10msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &compressor_thrashing_min_per_10msecs, 0, "");
4111 
4112 SYSCTL_QUAD(_vm, OID_AUTO, compressor_swapouts_under_30s, CTLFLAG_RD | CTLFLAG_LOCKED, &vmcs_stats.unripe_under_30s, "");
4113 SYSCTL_QUAD(_vm, OID_AUTO, compressor_swapouts_under_60s, CTLFLAG_RD | CTLFLAG_LOCKED, &vmcs_stats.unripe_under_60s, "");
4114 SYSCTL_QUAD(_vm, OID_AUTO, compressor_swapouts_under_300s, CTLFLAG_RD | CTLFLAG_LOCKED, &vmcs_stats.unripe_under_300s, "");
4115 SYSCTL_QUAD(_vm, OID_AUTO, compressor_swapper_reclaim_swapins, CTLFLAG_RD | CTLFLAG_LOCKED, &vmcs_stats.reclaim_swapins, "");
4116 SYSCTL_QUAD(_vm, OID_AUTO, compressor_swapper_defrag_swapins, CTLFLAG_RD | CTLFLAG_LOCKED, &vmcs_stats.defrag_swapins, "");
4117 SYSCTL_QUAD(_vm, OID_AUTO, compressor_swapper_swapout_threshold_exceeded, CTLFLAG_RD | CTLFLAG_LOCKED, &vmcs_stats.compressor_swap_threshold_exceeded, "");
4118 SYSCTL_QUAD(_vm, OID_AUTO, compressor_swapper_swapout_fileq_throttled, CTLFLAG_RD | CTLFLAG_LOCKED, &vmcs_stats.external_q_throttled, "");
4119 SYSCTL_QUAD(_vm, OID_AUTO, compressor_swapper_swapout_free_count_low, CTLFLAG_RD | CTLFLAG_LOCKED, &vmcs_stats.free_count_below_reserve, "");
4120 SYSCTL_QUAD(_vm, OID_AUTO, compressor_swapper_swapout_thrashing_detected, CTLFLAG_RD | CTLFLAG_LOCKED, &vmcs_stats.thrashing_detected, "");
4121 SYSCTL_QUAD(_vm, OID_AUTO, compressor_swapper_swapout_fragmentation_detected, CTLFLAG_RD | CTLFLAG_LOCKED, &vmcs_stats.fragmentation_detected, "");
4122 
4123 SYSCTL_STRING(_vm, OID_AUTO, swapfileprefix, CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED, swapfilename, sizeof(swapfilename) - SWAPFILENAME_INDEX_LEN, "");
4124 
4125 SYSCTL_INT(_vm, OID_AUTO, compressor_timing_enabled, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_compressor_time_thread, 0, "");
4126 
4127 #if DEVELOPMENT || DEBUG
4128 SYSCTL_QUAD(_vm, OID_AUTO, compressor_thread_runtime0, CTLFLAG_RD | CTLFLAG_LOCKED, &vmct_stats.vmct_runtimes[0], "");
4129 SYSCTL_QUAD(_vm, OID_AUTO, compressor_thread_runtime1, CTLFLAG_RD | CTLFLAG_LOCKED, &vmct_stats.vmct_runtimes[1], "");
4130 
4131 SYSCTL_QUAD(_vm, OID_AUTO, compressor_threads_total, CTLFLAG_RD | CTLFLAG_LOCKED, &vmct_stats.vmct_cthreads_total, "");
4132 
4133 SYSCTL_QUAD(_vm, OID_AUTO, compressor_thread_pages0, CTLFLAG_RD | CTLFLAG_LOCKED, &vmct_stats.vmct_pages[0], "");
4134 SYSCTL_QUAD(_vm, OID_AUTO, compressor_thread_pages1, CTLFLAG_RD | CTLFLAG_LOCKED, &vmct_stats.vmct_pages[1], "");
4135 
4136 SYSCTL_QUAD(_vm, OID_AUTO, compressor_thread_iterations0, CTLFLAG_RD | CTLFLAG_LOCKED, &vmct_stats.vmct_iterations[0], "");
4137 SYSCTL_QUAD(_vm, OID_AUTO, compressor_thread_iterations1, CTLFLAG_RD | CTLFLAG_LOCKED, &vmct_stats.vmct_iterations[1], "");
4138 
4139 SYSCTL_INT(_vm, OID_AUTO, compressor_thread_minpages0, CTLFLAG_RD | CTLFLAG_LOCKED, &vmct_stats.vmct_minpages[0], 0, "");
4140 SYSCTL_INT(_vm, OID_AUTO, compressor_thread_minpages1, CTLFLAG_RD | CTLFLAG_LOCKED, &vmct_stats.vmct_minpages[1], 0, "");
4141 
4142 SYSCTL_INT(_vm, OID_AUTO, compressor_thread_maxpages0, CTLFLAG_RD | CTLFLAG_LOCKED, &vmct_stats.vmct_maxpages[0], 0, "");
4143 SYSCTL_INT(_vm, OID_AUTO, compressor_thread_maxpages1, CTLFLAG_RD | CTLFLAG_LOCKED, &vmct_stats.vmct_maxpages[1], 0, "");
4144 
4145 int vm_compressor_injected_error_count;
4146 
4147 SYSCTL_INT(_vm, OID_AUTO, compressor_injected_error_count, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_compressor_injected_error_count, 0, "");
4148 
4149 static int
sysctl_compressor_inject_error(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)4150 sysctl_compressor_inject_error(__unused struct sysctl_oid *oidp,
4151     __unused void *arg1, __unused int arg2, struct sysctl_req *req)
4152 {
4153 	int result;
4154 	vm_address_t va = 0;
4155 	int changed;
4156 
4157 	result = sysctl_io_number(req, va, sizeof(va), &va, &changed);
4158 	if (result == 0 && changed) {
4159 		result = vm_map_inject_error(current_map(), va);
4160 		if (result == 0) {
4161 			/*
4162 			 * Count the number of errors injected successfully to detect
4163 			 * situations where corruption was caused by improper use of this
4164 			 * sysctl.
4165 			 */
4166 			os_atomic_inc(&vm_compressor_injected_error_count, relaxed);
4167 		}
4168 	}
4169 	return result;
4170 }
4171 
4172 SYSCTL_PROC(_vm, OID_AUTO, compressor_inject_error, CTLTYPE_QUAD | CTLFLAG_LOCKED | CTLFLAG_RW,
4173     0, 0, sysctl_compressor_inject_error, "Q", "flips a bit in a compressed page for the current task");
4174 
4175 #endif
4176 
4177 SYSCTL_QUAD(_vm, OID_AUTO, lz4_compressions, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.lz4_compressions, "");
4178 SYSCTL_QUAD(_vm, OID_AUTO, lz4_compression_failures, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.lz4_compression_failures, "");
4179 SYSCTL_QUAD(_vm, OID_AUTO, lz4_compressed_bytes, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.lz4_compressed_bytes, "");
4180 SYSCTL_QUAD(_vm, OID_AUTO, lz4_wk_compression_delta, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.lz4_wk_compression_delta, "");
4181 SYSCTL_QUAD(_vm, OID_AUTO, lz4_wk_compression_negative_delta, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.lz4_wk_compression_negative_delta, "");
4182 
4183 SYSCTL_QUAD(_vm, OID_AUTO, lz4_decompressions, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.lz4_decompressions, "");
4184 SYSCTL_QUAD(_vm, OID_AUTO, lz4_decompressed_bytes, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.lz4_decompressed_bytes, "");
4185 
4186 SYSCTL_QUAD(_vm, OID_AUTO, uc_decompressions, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.uc_decompressions, "");
4187 
4188 SYSCTL_QUAD(_vm, OID_AUTO, wk_compressions, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wk_compressions, "");
4189 
4190 SYSCTL_QUAD(_vm, OID_AUTO, wk_catime, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wk_cabstime, "");
4191 
4192 SYSCTL_QUAD(_vm, OID_AUTO, wkh_catime, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wkh_cabstime, "");
4193 SYSCTL_QUAD(_vm, OID_AUTO, wkh_compressions, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wkh_compressions, "");
4194 
4195 SYSCTL_QUAD(_vm, OID_AUTO, wks_catime, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wks_cabstime, "");
4196 SYSCTL_QUAD(_vm, OID_AUTO, wks_compressions, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wks_compressions, "");
4197 
4198 SYSCTL_QUAD(_vm, OID_AUTO, wk_compressions_exclusive, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wk_compressions_exclusive, "");
4199 SYSCTL_QUAD(_vm, OID_AUTO, wk_sv_compressions, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wk_sv_compressions, "");
4200 SYSCTL_QUAD(_vm, OID_AUTO, wk_mzv_compressions, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wk_mzv_compressions, "");
4201 SYSCTL_QUAD(_vm, OID_AUTO, wk_compression_failures, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wk_compression_failures, "");
4202 SYSCTL_QUAD(_vm, OID_AUTO, wk_compressed_bytes_exclusive, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wk_compressed_bytes_exclusive, "");
4203 SYSCTL_QUAD(_vm, OID_AUTO, wk_compressed_bytes_total, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wk_compressed_bytes_total, "");
4204 
4205 SYSCTL_QUAD(_vm, OID_AUTO, wks_compressed_bytes, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wks_compressed_bytes, "");
4206 SYSCTL_QUAD(_vm, OID_AUTO, wks_compression_failures, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wks_compression_failures, "");
4207 SYSCTL_QUAD(_vm, OID_AUTO, wks_sv_compressions, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wks_sv_compressions, "");
4208 
4209 
4210 SYSCTL_QUAD(_vm, OID_AUTO, wk_decompressions, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wk_decompressions, "");
4211 
4212 SYSCTL_QUAD(_vm, OID_AUTO, wk_datime, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wk_dabstime, "");
4213 
4214 SYSCTL_QUAD(_vm, OID_AUTO, wkh_datime, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wkh_dabstime, "");
4215 SYSCTL_QUAD(_vm, OID_AUTO, wkh_decompressions, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wkh_decompressions, "");
4216 
4217 SYSCTL_QUAD(_vm, OID_AUTO, wks_datime, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wks_dabstime, "");
4218 SYSCTL_QUAD(_vm, OID_AUTO, wks_decompressions, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wks_decompressions, "");
4219 
4220 SYSCTL_QUAD(_vm, OID_AUTO, wk_decompressed_bytes, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wk_decompressed_bytes, "");
4221 SYSCTL_QUAD(_vm, OID_AUTO, wk_sv_decompressions, CTLFLAG_RD | CTLFLAG_LOCKED, &compressor_stats.wk_sv_decompressions, "");
4222 
4223 SYSCTL_INT(_vm, OID_AUTO, lz4_threshold, CTLFLAG_RW | CTLFLAG_LOCKED, &vmctune.lz4_threshold, 0, "");
4224 SYSCTL_INT(_vm, OID_AUTO, wkdm_reeval_threshold, CTLFLAG_RW | CTLFLAG_LOCKED, &vmctune.wkdm_reeval_threshold, 0, "");
4225 SYSCTL_INT(_vm, OID_AUTO, lz4_max_failure_skips, CTLFLAG_RW | CTLFLAG_LOCKED, &vmctune.lz4_max_failure_skips, 0, "");
4226 SYSCTL_INT(_vm, OID_AUTO, lz4_max_failure_run_length, CTLFLAG_RW | CTLFLAG_LOCKED, &vmctune.lz4_max_failure_run_length, 0, "");
4227 SYSCTL_INT(_vm, OID_AUTO, lz4_max_preselects, CTLFLAG_RW | CTLFLAG_LOCKED, &vmctune.lz4_max_preselects, 0, "");
4228 SYSCTL_INT(_vm, OID_AUTO, lz4_run_preselection_threshold, CTLFLAG_RW | CTLFLAG_LOCKED, &vmctune.lz4_run_preselection_threshold, 0, "");
4229 SYSCTL_INT(_vm, OID_AUTO, lz4_run_continue_bytes, CTLFLAG_RW | CTLFLAG_LOCKED, &vmctune.lz4_run_continue_bytes, 0, "");
4230 SYSCTL_INT(_vm, OID_AUTO, lz4_profitable_bytes, CTLFLAG_RW | CTLFLAG_LOCKED, &vmctune.lz4_profitable_bytes, 0, "");
4231 #if DEVELOPMENT || DEBUG
4232 extern int vm_compressor_current_codec;
4233 extern int vm_compressor_test_seg_wp;
4234 extern boolean_t vm_compressor_force_sw_wkdm;
4235 SYSCTL_INT(_vm, OID_AUTO, compressor_codec, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_compressor_current_codec, 0, "");
4236 SYSCTL_INT(_vm, OID_AUTO, compressor_test_wp, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_compressor_test_seg_wp, 0, "");
4237 
4238 SYSCTL_INT(_vm, OID_AUTO, wksw_force, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_compressor_force_sw_wkdm, 0, "");
4239 extern int precompy, wkswhw;
4240 
4241 SYSCTL_INT(_vm, OID_AUTO, precompy, CTLFLAG_RW | CTLFLAG_LOCKED, &precompy, 0, "");
4242 SYSCTL_INT(_vm, OID_AUTO, wkswhw, CTLFLAG_RW | CTLFLAG_LOCKED, &wkswhw, 0, "");
4243 extern unsigned int vm_ktrace_enabled;
4244 SYSCTL_INT(_vm, OID_AUTO, vm_ktrace, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_ktrace_enabled, 0, "");
4245 #endif
4246 
4247 #if CONFIG_PHANTOM_CACHE
4248 extern uint32_t phantom_cache_thrashing_threshold;
4249 extern uint32_t phantom_cache_eval_period_in_msecs;
4250 extern uint32_t phantom_cache_thrashing_threshold_ssd;
4251 
4252 
4253 SYSCTL_INT(_vm, OID_AUTO, phantom_cache_eval_period_in_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &phantom_cache_eval_period_in_msecs, 0, "");
4254 SYSCTL_INT(_vm, OID_AUTO, phantom_cache_thrashing_threshold, CTLFLAG_RW | CTLFLAG_LOCKED, &phantom_cache_thrashing_threshold, 0, "");
4255 SYSCTL_INT(_vm, OID_AUTO, phantom_cache_thrashing_threshold_ssd, CTLFLAG_RW | CTLFLAG_LOCKED, &phantom_cache_thrashing_threshold_ssd, 0, "");
4256 #endif
4257 
4258 #if CONFIG_BACKGROUND_QUEUE
4259 
4260 extern uint32_t vm_page_background_count;
4261 extern uint32_t vm_page_background_target;
4262 extern uint32_t vm_page_background_internal_count;
4263 extern uint32_t vm_page_background_external_count;
4264 extern uint32_t vm_page_background_mode;
4265 extern uint32_t vm_page_background_exclude_external;
4266 extern uint64_t vm_page_background_promoted_count;
4267 extern uint64_t vm_pageout_rejected_bq_internal;
4268 extern uint64_t vm_pageout_rejected_bq_external;
4269 
4270 SYSCTL_INT(_vm, OID_AUTO, vm_page_background_mode, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_page_background_mode, 0, "");
4271 SYSCTL_INT(_vm, OID_AUTO, vm_page_background_exclude_external, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_page_background_exclude_external, 0, "");
4272 SYSCTL_INT(_vm, OID_AUTO, vm_page_background_target, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_page_background_target, 0, "");
4273 SYSCTL_INT(_vm, OID_AUTO, vm_page_background_count, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_page_background_count, 0, "");
4274 SYSCTL_INT(_vm, OID_AUTO, vm_page_background_internal_count, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_page_background_internal_count, 0, "");
4275 SYSCTL_INT(_vm, OID_AUTO, vm_page_background_external_count, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_page_background_external_count, 0, "");
4276 
4277 SYSCTL_QUAD(_vm, OID_AUTO, vm_page_background_promoted_count, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_page_background_promoted_count, "");
4278 SYSCTL_QUAD(_vm, OID_AUTO, vm_pageout_considered_bq_internal, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_pageout_vminfo.vm_pageout_considered_bq_internal, "");
4279 SYSCTL_QUAD(_vm, OID_AUTO, vm_pageout_considered_bq_external, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_pageout_vminfo.vm_pageout_considered_bq_external, "");
4280 SYSCTL_QUAD(_vm, OID_AUTO, vm_pageout_rejected_bq_internal, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_pageout_rejected_bq_internal, "");
4281 SYSCTL_QUAD(_vm, OID_AUTO, vm_pageout_rejected_bq_external, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_pageout_rejected_bq_external, "");
4282 
4283 #endif /* CONFIG_BACKGROUND_QUEUE */
4284 
4285 extern void vm_update_darkwake_mode(boolean_t);
4286 extern boolean_t vm_darkwake_mode;
4287 
4288 STATIC int
sysctl_toggle_darkwake_mode(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)4289 sysctl_toggle_darkwake_mode(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
4290 {
4291 	int new_value, changed;
4292 	int error = sysctl_io_number(req, vm_darkwake_mode, sizeof(int), &new_value, &changed);
4293 
4294 	if (!error && changed) {
4295 		if (new_value != 0 && new_value != 1) {
4296 			printf("Error: Invalid value passed to darkwake sysctl. Acceptable: 0 or 1.\n");
4297 			error = EINVAL;
4298 		} else {
4299 			vm_update_darkwake_mode((boolean_t) new_value);
4300 		}
4301 	}
4302 
4303 	return error;
4304 }
4305 
4306 SYSCTL_PROC(_vm, OID_AUTO, darkwake_mode,
4307     CTLTYPE_INT | CTLFLAG_LOCKED | CTLFLAG_RW,
4308     0, 0, sysctl_toggle_darkwake_mode, "I", "");
4309 
4310 #if (DEVELOPMENT || DEBUG)
4311 
4312 SYSCTL_UINT(_vm, OID_AUTO, vm_page_creation_throttled_hard,
4313     CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
4314     &vm_page_creation_throttled_hard, 0, "");
4315 
4316 SYSCTL_UINT(_vm, OID_AUTO, vm_page_creation_throttled_soft,
4317     CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
4318     &vm_page_creation_throttled_soft, 0, "");
4319 
4320 extern uint32_t vm_pageout_memorystatus_fb_factor_nr;
4321 extern uint32_t vm_pageout_memorystatus_fb_factor_dr;
4322 SYSCTL_INT(_vm, OID_AUTO, vm_pageout_memorystatus_fb_factor_nr, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_pageout_memorystatus_fb_factor_nr, 0, "");
4323 SYSCTL_INT(_vm, OID_AUTO, vm_pageout_memorystatus_fb_factor_dr, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_pageout_memorystatus_fb_factor_dr, 0, "");
4324 
4325 extern uint32_t vm_grab_anon_nops;
4326 
4327 SYSCTL_INT(_vm, OID_AUTO, vm_grab_anon_overrides, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_pageout_debug.vm_grab_anon_overrides, 0, "");
4328 SYSCTL_INT(_vm, OID_AUTO, vm_grab_anon_nops, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_pageout_debug.vm_grab_anon_nops, 0, "");
4329 
4330 extern int vm_page_delayed_work_ctx_needed;
4331 SYSCTL_INT(_vm, OID_AUTO, vm_page_needed_delayed_work_ctx, CTLFLAG_RD | CTLFLAG_LOCKED, &vm_page_delayed_work_ctx_needed, 0, "");
4332 
4333 /* log message counters for persistence mode */
4334 SCALABLE_COUNTER_DECLARE(oslog_p_total_msgcount);
4335 SCALABLE_COUNTER_DECLARE(oslog_p_metadata_saved_msgcount);
4336 SCALABLE_COUNTER_DECLARE(oslog_p_metadata_dropped_msgcount);
4337 SCALABLE_COUNTER_DECLARE(oslog_p_error_count);
4338 SCALABLE_COUNTER_DECLARE(oslog_p_saved_msgcount);
4339 SCALABLE_COUNTER_DECLARE(oslog_p_dropped_msgcount);
4340 SCALABLE_COUNTER_DECLARE(oslog_p_boot_dropped_msgcount);
4341 SCALABLE_COUNTER_DECLARE(oslog_p_coprocessor_total_msgcount);
4342 SCALABLE_COUNTER_DECLARE(oslog_p_coprocessor_dropped_msgcount);
4343 SCALABLE_COUNTER_DECLARE(oslog_p_unresolved_kc_msgcount);
4344 SCALABLE_COUNTER_DECLARE(oslog_p_fmt_invalid_msgcount);
4345 SCALABLE_COUNTER_DECLARE(oslog_p_fmt_max_args_msgcount);
4346 SCALABLE_COUNTER_DECLARE(oslog_p_truncated_msgcount);
4347 
4348 SCALABLE_COUNTER_DECLARE(log_queue_cnt_received);
4349 SCALABLE_COUNTER_DECLARE(log_queue_cnt_rejected_fh);
4350 SCALABLE_COUNTER_DECLARE(log_queue_cnt_sent);
4351 SCALABLE_COUNTER_DECLARE(log_queue_cnt_dropped_nomem);
4352 SCALABLE_COUNTER_DECLARE(log_queue_cnt_queued);
4353 SCALABLE_COUNTER_DECLARE(log_queue_cnt_dropped_off);
4354 SCALABLE_COUNTER_DECLARE(log_queue_cnt_mem_active);
4355 SCALABLE_COUNTER_DECLARE(log_queue_cnt_mem_allocated);
4356 SCALABLE_COUNTER_DECLARE(log_queue_cnt_mem_released);
4357 SCALABLE_COUNTER_DECLARE(log_queue_cnt_mem_failed);
4358 
4359 /* log message counters for streaming mode */
4360 SCALABLE_COUNTER_DECLARE(oslog_s_total_msgcount);
4361 SCALABLE_COUNTER_DECLARE(oslog_s_metadata_msgcount);
4362 SCALABLE_COUNTER_DECLARE(oslog_s_error_count);
4363 SCALABLE_COUNTER_DECLARE(oslog_s_streamed_msgcount);
4364 SCALABLE_COUNTER_DECLARE(oslog_s_dropped_msgcount);
4365 
4366 /* log message counters for msgbuf logging */
4367 SCALABLE_COUNTER_DECLARE(oslog_msgbuf_msgcount);
4368 SCALABLE_COUNTER_DECLARE(oslog_msgbuf_dropped_msgcount);
4369 extern uint32_t oslog_msgbuf_dropped_charcount;
4370 
4371 /* log message counters for vaddlog logging */
4372 extern uint32_t vaddlog_msgcount;
4373 extern uint32_t vaddlog_msgcount_dropped;
4374 
4375 SYSCTL_SCALABLE_COUNTER(_debug, oslog_p_total_msgcount, oslog_p_total_msgcount, "");
4376 SYSCTL_SCALABLE_COUNTER(_debug, oslog_p_metadata_saved_msgcount, oslog_p_metadata_saved_msgcount, "");
4377 SYSCTL_SCALABLE_COUNTER(_debug, oslog_p_metadata_dropped_msgcount, oslog_p_metadata_dropped_msgcount, "");
4378 SYSCTL_SCALABLE_COUNTER(_debug, oslog_p_error_count, oslog_p_error_count, "");
4379 SYSCTL_SCALABLE_COUNTER(_debug, oslog_p_saved_msgcount, oslog_p_saved_msgcount, "");
4380 SYSCTL_SCALABLE_COUNTER(_debug, oslog_p_dropped_msgcount, oslog_p_dropped_msgcount, "");
4381 SYSCTL_SCALABLE_COUNTER(_debug, oslog_p_boot_dropped_msgcount, oslog_p_boot_dropped_msgcount, "");
4382 SYSCTL_SCALABLE_COUNTER(_debug, oslog_p_coprocessor_total_msgcount, oslog_p_coprocessor_total_msgcount, "");
4383 SYSCTL_SCALABLE_COUNTER(_debug, oslog_p_coprocessor_dropped_msgcount, oslog_p_coprocessor_dropped_msgcount, "");
4384 SYSCTL_SCALABLE_COUNTER(_debug, oslog_p_unresolved_kc_msgcount, oslog_p_unresolved_kc_msgcount, "");
4385 
4386 SYSCTL_SCALABLE_COUNTER(_debug, oslog_p_fmt_invalid_msgcount, oslog_p_fmt_invalid_msgcount, "");
4387 SYSCTL_SCALABLE_COUNTER(_debug, oslog_p_fmt_max_args_msgcount, oslog_p_fmt_max_args_msgcount, "");
4388 SYSCTL_SCALABLE_COUNTER(_debug, oslog_p_truncated_msgcount, oslog_p_truncated_msgcount, "");
4389 
4390 SYSCTL_SCALABLE_COUNTER(_debug, oslog_s_total_msgcount, oslog_s_total_msgcount, "Number of logs sent to streaming");
4391 SYSCTL_SCALABLE_COUNTER(_debug, oslog_s_metadata_msgcount, oslog_s_metadata_msgcount, "Number of metadata sent to streaming");
4392 SYSCTL_SCALABLE_COUNTER(_debug, oslog_s_error_count, oslog_s_error_count, "Number of invalid stream logs");
4393 SYSCTL_SCALABLE_COUNTER(_debug, oslog_s_streamed_msgcount, oslog_s_streamed_msgcount, "Number of streamed logs");
4394 SYSCTL_SCALABLE_COUNTER(_debug, oslog_s_dropped_msgcount, oslog_s_dropped_msgcount, "Number of logs dropped from stream");
4395 
4396 SYSCTL_SCALABLE_COUNTER(_debug, oslog_msgbuf_msgcount, oslog_msgbuf_msgcount, "Number of dmesg log messages");
4397 SYSCTL_SCALABLE_COUNTER(_debug, oslog_msgbuf_dropped_msgcount, oslog_msgbuf_dropped_msgcount, "Number of dropped dmesg log messages");
4398 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");
4399 
4400 SYSCTL_UINT(_debug, OID_AUTO, vaddlog_msgcount, CTLFLAG_ANYBODY | CTLFLAG_RD | CTLFLAG_LOCKED, &vaddlog_msgcount, 0, "");
4401 SYSCTL_UINT(_debug, OID_AUTO, vaddlog_msgcount_dropped, CTLFLAG_ANYBODY | CTLFLAG_RD | CTLFLAG_LOCKED, &vaddlog_msgcount_dropped, 0, "");
4402 
4403 SYSCTL_SCALABLE_COUNTER(_debug, log_queue_cnt_received, log_queue_cnt_received, "Number of received logs");
4404 SYSCTL_SCALABLE_COUNTER(_debug, log_queue_cnt_rejected_fh, log_queue_cnt_rejected_fh, "Number of logs initially rejected by FH");
4405 SYSCTL_SCALABLE_COUNTER(_debug, log_queue_cnt_sent, log_queue_cnt_sent, "Number of logs successfully saved in FH");
4406 SYSCTL_SCALABLE_COUNTER(_debug, log_queue_cnt_dropped_nomem, log_queue_cnt_dropped_nomem, "Number of logs dropped due to lack of queue memory");
4407 SYSCTL_SCALABLE_COUNTER(_debug, log_queue_cnt_queued, log_queue_cnt_queued, "Current number of logs stored in log queues");
4408 SYSCTL_SCALABLE_COUNTER(_debug, log_queue_cnt_dropped_off, log_queue_cnt_dropped_off, "Number of logs dropped due to disabled log queues");
4409 SYSCTL_SCALABLE_COUNTER(_debug, log_queue_cnt_mem_allocated, log_queue_cnt_mem_allocated, "Number of memory allocations");
4410 SYSCTL_SCALABLE_COUNTER(_debug, log_queue_cnt_mem_released, log_queue_cnt_mem_released, "Number of memory releases");
4411 SYSCTL_SCALABLE_COUNTER(_debug, log_queue_cnt_mem_failed, log_queue_cnt_mem_failed, "Number of failed memory allocations");
4412 
4413 #endif /* DEVELOPMENT || DEBUG */
4414 
4415 /*
4416  * Enable tracing of voucher contents
4417  */
4418 extern uint32_t ipc_voucher_trace_contents;
4419 
4420 SYSCTL_INT(_kern, OID_AUTO, ipc_voucher_trace_contents,
4421     CTLFLAG_RW | CTLFLAG_LOCKED, &ipc_voucher_trace_contents, 0, "Enable tracing voucher contents");
4422 
4423 /*
4424  * Kernel stack size and depth
4425  */
4426 SYSCTL_INT(_kern, OID_AUTO, stack_size,
4427     CTLFLAG_RD | CTLFLAG_LOCKED, (int *) &kernel_stack_size, 0, "Kernel stack size");
4428 SYSCTL_INT(_kern, OID_AUTO, stack_depth_max,
4429     CTLFLAG_RD | CTLFLAG_LOCKED, (int *) &kernel_stack_depth_max, 0, "Max kernel stack depth at interrupt or context switch");
4430 
4431 extern unsigned int kern_feature_overrides;
4432 SYSCTL_INT(_kern, OID_AUTO, kern_feature_overrides,
4433     CTLFLAG_RD | CTLFLAG_LOCKED, &kern_feature_overrides, 0, "Kernel feature override mask");
4434 
4435 /*
4436  * enable back trace for port allocations
4437  */
4438 extern int ipc_portbt;
4439 
4440 SYSCTL_INT(_kern, OID_AUTO, ipc_portbt,
4441     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
4442     &ipc_portbt, 0, "");
4443 
4444 /*
4445  * Mach message signature validation control and outputs
4446  */
4447 extern unsigned int ikm_signature_failures;
4448 SYSCTL_INT(_kern, OID_AUTO, ikm_signature_failures,
4449     CTLFLAG_RD | CTLFLAG_LOCKED, &ikm_signature_failures, 0, "Message signature failure count");
4450 extern unsigned int ikm_signature_failure_id;
4451 SYSCTL_INT(_kern, OID_AUTO, ikm_signature_failure_id,
4452     CTLFLAG_RD | CTLFLAG_LOCKED, &ikm_signature_failure_id, 0, "Message signature failure count");
4453 
4454 #if (DEVELOPMENT || DEBUG)
4455 extern unsigned int ikm_signature_panic_disable;
4456 SYSCTL_INT(_kern, OID_AUTO, ikm_signature_panic_disable,
4457     CTLFLAG_RW | CTLFLAG_LOCKED, &ikm_signature_panic_disable, 0, "Message signature failure mode");
4458 extern unsigned int ikm_signature_header_failures;
4459 SYSCTL_INT(_kern, OID_AUTO, ikm_signature_header_failures,
4460     CTLFLAG_RD | CTLFLAG_LOCKED, &ikm_signature_header_failures, 0, "Message header signature failure count");
4461 extern unsigned int ikm_signature_trailer_failures;
4462 SYSCTL_INT(_kern, OID_AUTO, ikm_signature_trailer_failures,
4463     CTLFLAG_RD | CTLFLAG_LOCKED, &ikm_signature_trailer_failures, 0, "Message trailer signature failure count");
4464 #endif
4465 
4466 /*
4467  * Scheduler sysctls
4468  */
4469 
4470 SYSCTL_STRING(_kern, OID_AUTO, sched,
4471     CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
4472     sched_string, sizeof(sched_string),
4473     "Timeshare scheduler implementation");
4474 
4475 #if CONFIG_QUIESCE_COUNTER
4476 static int
4477 sysctl_cpu_quiescent_counter_interval SYSCTL_HANDLER_ARGS
4478 {
4479 #pragma unused(arg1, arg2)
4480 
4481 	uint32_t local_min_interval_us = cpu_quiescent_counter_get_min_interval_us();
4482 
4483 	int error = sysctl_handle_int(oidp, &local_min_interval_us, 0, req);
4484 	if (error || !req->newptr) {
4485 		return error;
4486 	}
4487 
4488 	cpu_quiescent_counter_set_min_interval_us(local_min_interval_us);
4489 
4490 	return 0;
4491 }
4492 
4493 SYSCTL_PROC(_kern, OID_AUTO, cpu_checkin_interval,
4494     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
4495     0, 0,
4496     sysctl_cpu_quiescent_counter_interval, "I",
4497     "Quiescent CPU checkin interval (microseconds)");
4498 #endif /* CONFIG_QUIESCE_COUNTER */
4499 
4500 
4501 /*
4502  * Only support runtime modification on development / debug
4503  */
4504 #if DEVELOPMENT || DEBUG
4505 extern int precise_user_kernel_time;
4506 SYSCTL_INT(_kern, OID_AUTO, precise_user_kernel_time,
4507     CTLFLAG_RW | CTLFLAG_LOCKED,
4508     &precise_user_kernel_time, 0, "Precise accounting of kernel vs. user time");
4509 #endif /* DEVELOPMENT || DEBUG */
4510 
4511 /* Parameters related to timer coalescing tuning, to be replaced
4512  * with a dedicated systemcall in the future.
4513  */
4514 /* Enable processing pending timers in the context of any other interrupt
4515  * Coalescing tuning parameters for various thread/task attributes */
4516 STATIC int
4517 sysctl_timer_user_us_kernel_abstime SYSCTL_HANDLER_ARGS
4518 {
4519 #pragma unused(oidp)
4520 	int size = arg2;        /* subcommand*/
4521 	int error;
4522 	int changed = 0;
4523 	uint64_t old_value_ns;
4524 	uint64_t new_value_ns;
4525 	uint64_t value_abstime;
4526 	if (size == sizeof(uint32_t)) {
4527 		value_abstime = *((uint32_t *)arg1);
4528 	} else if (size == sizeof(uint64_t)) {
4529 		value_abstime = *((uint64_t *)arg1);
4530 	} else {
4531 		return ENOTSUP;
4532 	}
4533 
4534 	absolutetime_to_nanoseconds(value_abstime, &old_value_ns);
4535 	error = sysctl_io_number(req, old_value_ns, sizeof(old_value_ns), &new_value_ns, &changed);
4536 	if ((error) || (!changed)) {
4537 		return error;
4538 	}
4539 
4540 	nanoseconds_to_absolutetime(new_value_ns, &value_abstime);
4541 	if (size == sizeof(uint32_t)) {
4542 		*((uint32_t *)arg1) = (uint32_t)value_abstime;
4543 	} else {
4544 		*((uint64_t *)arg1) = value_abstime;
4545 	}
4546 	return error;
4547 }
4548 
4549 SYSCTL_INT(_kern, OID_AUTO, timer_coalesce_bg_scale,
4550     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
4551     &tcoal_prio_params.timer_coalesce_bg_shift, 0, "");
4552 SYSCTL_PROC(_kern, OID_AUTO, timer_resort_threshold_ns,
4553     CTLTYPE_QUAD | CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
4554     &tcoal_prio_params.timer_resort_threshold_abstime,
4555     sizeof(tcoal_prio_params.timer_resort_threshold_abstime),
4556     sysctl_timer_user_us_kernel_abstime,
4557     "Q", "");
4558 SYSCTL_PROC(_kern, OID_AUTO, timer_coalesce_bg_ns_max,
4559     CTLTYPE_QUAD | CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
4560     &tcoal_prio_params.timer_coalesce_bg_abstime_max,
4561     sizeof(tcoal_prio_params.timer_coalesce_bg_abstime_max),
4562     sysctl_timer_user_us_kernel_abstime,
4563     "Q", "");
4564 
4565 SYSCTL_INT(_kern, OID_AUTO, timer_coalesce_kt_scale,
4566     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
4567     &tcoal_prio_params.timer_coalesce_kt_shift, 0, "");
4568 
4569 SYSCTL_PROC(_kern, OID_AUTO, timer_coalesce_kt_ns_max,
4570     CTLTYPE_QUAD | CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
4571     &tcoal_prio_params.timer_coalesce_kt_abstime_max,
4572     sizeof(tcoal_prio_params.timer_coalesce_kt_abstime_max),
4573     sysctl_timer_user_us_kernel_abstime,
4574     "Q", "");
4575 
4576 SYSCTL_INT(_kern, OID_AUTO, timer_coalesce_fp_scale,
4577     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
4578     &tcoal_prio_params.timer_coalesce_fp_shift, 0, "");
4579 
4580 SYSCTL_PROC(_kern, OID_AUTO, timer_coalesce_fp_ns_max,
4581     CTLTYPE_QUAD | CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
4582     &tcoal_prio_params.timer_coalesce_fp_abstime_max,
4583     sizeof(tcoal_prio_params.timer_coalesce_fp_abstime_max),
4584     sysctl_timer_user_us_kernel_abstime,
4585     "Q", "");
4586 
4587 SYSCTL_INT(_kern, OID_AUTO, timer_coalesce_ts_scale,
4588     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
4589     &tcoal_prio_params.timer_coalesce_ts_shift, 0, "");
4590 
4591 SYSCTL_PROC(_kern, OID_AUTO, timer_coalesce_ts_ns_max,
4592     CTLTYPE_QUAD | CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
4593     &tcoal_prio_params.timer_coalesce_ts_abstime_max,
4594     sizeof(tcoal_prio_params.timer_coalesce_ts_abstime_max),
4595     sysctl_timer_user_us_kernel_abstime,
4596     "Q", "");
4597 
4598 SYSCTL_INT(_kern, OID_AUTO, timer_coalesce_tier0_scale,
4599     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
4600     &tcoal_prio_params.latency_qos_scale[0], 0, "");
4601 
4602 SYSCTL_PROC(_kern, OID_AUTO, timer_coalesce_tier0_ns_max,
4603     CTLTYPE_QUAD | CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
4604     &tcoal_prio_params.latency_qos_abstime_max[0],
4605     sizeof(tcoal_prio_params.latency_qos_abstime_max[0]),
4606     sysctl_timer_user_us_kernel_abstime,
4607     "Q", "");
4608 
4609 SYSCTL_INT(_kern, OID_AUTO, timer_coalesce_tier1_scale,
4610     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
4611     &tcoal_prio_params.latency_qos_scale[1], 0, "");
4612 
4613 SYSCTL_PROC(_kern, OID_AUTO, timer_coalesce_tier1_ns_max,
4614     CTLTYPE_QUAD | CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
4615     &tcoal_prio_params.latency_qos_abstime_max[1],
4616     sizeof(tcoal_prio_params.latency_qos_abstime_max[1]),
4617     sysctl_timer_user_us_kernel_abstime,
4618     "Q", "");
4619 
4620 SYSCTL_INT(_kern, OID_AUTO, timer_coalesce_tier2_scale,
4621     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
4622     &tcoal_prio_params.latency_qos_scale[2], 0, "");
4623 
4624 SYSCTL_PROC(_kern, OID_AUTO, timer_coalesce_tier2_ns_max,
4625     CTLTYPE_QUAD | CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
4626     &tcoal_prio_params.latency_qos_abstime_max[2],
4627     sizeof(tcoal_prio_params.latency_qos_abstime_max[2]),
4628     sysctl_timer_user_us_kernel_abstime,
4629     "Q", "");
4630 
4631 SYSCTL_INT(_kern, OID_AUTO, timer_coalesce_tier3_scale,
4632     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
4633     &tcoal_prio_params.latency_qos_scale[3], 0, "");
4634 
4635 SYSCTL_PROC(_kern, OID_AUTO, timer_coalesce_tier3_ns_max,
4636     CTLTYPE_QUAD | CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
4637     &tcoal_prio_params.latency_qos_abstime_max[3],
4638     sizeof(tcoal_prio_params.latency_qos_abstime_max[3]),
4639     sysctl_timer_user_us_kernel_abstime,
4640     "Q", "");
4641 
4642 SYSCTL_INT(_kern, OID_AUTO, timer_coalesce_tier4_scale,
4643     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
4644     &tcoal_prio_params.latency_qos_scale[4], 0, "");
4645 
4646 SYSCTL_PROC(_kern, OID_AUTO, timer_coalesce_tier4_ns_max,
4647     CTLTYPE_QUAD | CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
4648     &tcoal_prio_params.latency_qos_abstime_max[4],
4649     sizeof(tcoal_prio_params.latency_qos_abstime_max[4]),
4650     sysctl_timer_user_us_kernel_abstime,
4651     "Q", "");
4652 
4653 SYSCTL_INT(_kern, OID_AUTO, timer_coalesce_tier5_scale,
4654     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
4655     &tcoal_prio_params.latency_qos_scale[5], 0, "");
4656 
4657 SYSCTL_PROC(_kern, OID_AUTO, timer_coalesce_tier5_ns_max,
4658     CTLTYPE_QUAD | CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
4659     &tcoal_prio_params.latency_qos_abstime_max[5],
4660     sizeof(tcoal_prio_params.latency_qos_abstime_max[5]),
4661     sysctl_timer_user_us_kernel_abstime,
4662     "Q", "");
4663 
4664 /* Communicate the "user idle level" heuristic to the timer layer, and
4665  * potentially other layers in the future.
4666  */
4667 
4668 static int
timer_user_idle_level(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)4669 timer_user_idle_level(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
4670 {
4671 	int new_value = 0, old_value = 0, changed = 0, error;
4672 
4673 	old_value = timer_get_user_idle_level();
4674 
4675 	error = sysctl_io_number(req, old_value, sizeof(int), &new_value, &changed);
4676 
4677 	if (error == 0 && changed) {
4678 		if (timer_set_user_idle_level(new_value) != KERN_SUCCESS) {
4679 			error = ERANGE;
4680 		}
4681 	}
4682 
4683 	return error;
4684 }
4685 
4686 SYSCTL_PROC(_machdep, OID_AUTO, user_idle_level,
4687     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
4688     0, 0,
4689     timer_user_idle_level, "I", "User idle level heuristic, 0-128");
4690 
4691 #if HYPERVISOR
4692 SYSCTL_INT(_kern, OID_AUTO, hv_support,
4693     CTLFLAG_KERN | CTLFLAG_RD | CTLFLAG_LOCKED,
4694     &hv_support_available, 0, "");
4695 
4696 SYSCTL_INT(_kern, OID_AUTO, hv_disable,
4697     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
4698     &hv_disable, 0, "");
4699 #endif
4700 
4701 #if DEVELOPMENT || DEBUG
4702 extern uint64_t driverkit_checkin_timed_out;
4703 SYSCTL_QUAD(_kern, OID_AUTO, driverkit_checkin_timed_out,
4704     CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
4705     &driverkit_checkin_timed_out, "timestamp of dext checkin timeout");
4706 #endif
4707 
4708 extern int IOGetVMMPresent(void);
4709 
4710 static int
4711 hv_vmm_present SYSCTL_HANDLER_ARGS
4712 {
4713 	__unused struct sysctl_oid *unused_oidp = oidp;
4714 	__unused void *unused_arg1 = arg1;
4715 	__unused int unused_arg2 = arg2;
4716 
4717 	int hv_vmm_present = 0;
4718 
4719 	hv_vmm_present = IOGetVMMPresent();
4720 
4721 	return SYSCTL_OUT(req, &hv_vmm_present, sizeof(hv_vmm_present));
4722 }
4723 
4724 SYSCTL_PROC(_kern, OID_AUTO, hv_vmm_present,
4725     CTLTYPE_INT | CTLFLAG_ANYBODY | CTLFLAG_KERN | CTLFLAG_LOCKED,
4726     0, 0,
4727     hv_vmm_present, "I", "");
4728 
4729 #if CONFIG_DARKBOOT
4730 STATIC int
4731 sysctl_darkboot SYSCTL_HANDLER_ARGS
4732 {
4733 	int err = 0, value = 0;
4734 #pragma unused(oidp, arg1, arg2, err, value, req)
4735 
4736 	/*
4737 	 * Handle the sysctl request.
4738 	 *
4739 	 * If this is a read, the function will set the value to the current darkboot value. Otherwise,
4740 	 * we'll get the request identifier into "value" and then we can honor it.
4741 	 */
4742 	if ((err = sysctl_io_number(req, darkboot, sizeof(int), &value, NULL)) != 0) {
4743 		goto exit;
4744 	}
4745 
4746 	/* writing requested, let's process the request */
4747 	if (req->newptr) {
4748 		/* writing is protected by an entitlement */
4749 		if (priv_check_cred(kauth_cred_get(), PRIV_DARKBOOT, 0) != 0) {
4750 			err = EPERM;
4751 			goto exit;
4752 		}
4753 
4754 		switch (value) {
4755 		case MEMORY_MAINTENANCE_DARK_BOOT_UNSET:
4756 			/*
4757 			 * If the darkboot sysctl is unset, the NVRAM variable
4758 			 * must be unset too. If that's not the case, it means
4759 			 * someone is doing something crazy and not supported.
4760 			 */
4761 			if (darkboot != 0) {
4762 				int ret = PERemoveNVRAMProperty(MEMORY_MAINTENANCE_DARK_BOOT_NVRAM_NAME);
4763 				if (ret) {
4764 					darkboot = 0;
4765 				} else {
4766 					err = EINVAL;
4767 				}
4768 			}
4769 			break;
4770 		case MEMORY_MAINTENANCE_DARK_BOOT_SET:
4771 			darkboot = 1;
4772 			break;
4773 		case MEMORY_MAINTENANCE_DARK_BOOT_SET_PERSISTENT: {
4774 			/*
4775 			 * Set the NVRAM and update 'darkboot' in case
4776 			 * of success. Otherwise, do not update
4777 			 * 'darkboot' and report the failure.
4778 			 */
4779 			if (PEWriteNVRAMBooleanProperty(MEMORY_MAINTENANCE_DARK_BOOT_NVRAM_NAME, TRUE)) {
4780 				darkboot = 1;
4781 			} else {
4782 				err = EINVAL;
4783 			}
4784 
4785 			break;
4786 		}
4787 		default:
4788 			err = EINVAL;
4789 		}
4790 	}
4791 
4792 exit:
4793 	return err;
4794 }
4795 
4796 SYSCTL_PROC(_kern, OID_AUTO, darkboot,
4797     CTLFLAG_KERN | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_ANYBODY,
4798     0, 0, sysctl_darkboot, "I", "");
4799 #endif /* CONFIG_DARKBOOT */
4800 
4801 #if DEVELOPMENT || DEBUG
4802 #include <sys/sysent.h>
4803 /* This should result in a fatal exception, verifying that "sysent" is
4804  * write-protected.
4805  */
4806 static int
kern_sysent_write(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)4807 kern_sysent_write(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
4808 {
4809 	uint64_t new_value = 0, old_value = 0;
4810 	int changed = 0, error;
4811 
4812 	error = sysctl_io_number(req, old_value, sizeof(uint64_t), &new_value, &changed);
4813 	if ((error == 0) && changed) {
4814 		volatile uint32_t *wraddr = __DECONST(uint32_t *, &sysent[0]);
4815 		*wraddr = 0;
4816 		printf("sysent[0] write succeeded\n");
4817 	}
4818 	return error;
4819 }
4820 
4821 SYSCTL_PROC(_kern, OID_AUTO, sysent_const_check,
4822     CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
4823     0, 0,
4824     kern_sysent_write, "I", "Attempt sysent[0] write");
4825 
4826 #endif
4827 
4828 #if DEVELOPMENT || DEBUG
4829 SYSCTL_COMPAT_INT(_kern, OID_AUTO, development, CTLFLAG_RD | CTLFLAG_MASKED | CTLFLAG_KERN, NULL, 1, "");
4830 #else
4831 SYSCTL_COMPAT_INT(_kern, OID_AUTO, development, CTLFLAG_RD | CTLFLAG_MASKED, NULL, 0, "");
4832 #endif
4833 
4834 
4835 #if DEVELOPMENT || DEBUG
4836 
4837 static int
4838 sysctl_panic_test SYSCTL_HANDLER_ARGS
4839 {
4840 #pragma unused(arg1, arg2)
4841 	int rval = 0;
4842 	char str[32] = "entry prelog postlog postcore";
4843 
4844 	rval = sysctl_handle_string(oidp, str, sizeof(str), req);
4845 
4846 	if (rval == 0 && req->newptr) {
4847 		if (strncmp("entry", str, strlen("entry")) == 0) {
4848 			panic_with_options(0, NULL, DEBUGGER_OPTION_RECURPANIC_ENTRY, "test recursive panic at entry");
4849 		} else if (strncmp("prelog", str, strlen("prelog")) == 0) {
4850 			panic_with_options(0, NULL, DEBUGGER_OPTION_RECURPANIC_PRELOG, "test recursive panic prior to writing a paniclog");
4851 		} else if (strncmp("postlog", str, strlen("postlog")) == 0) {
4852 			panic_with_options(0, NULL, DEBUGGER_OPTION_RECURPANIC_POSTLOG, "test recursive panic subsequent to paniclog");
4853 		} else if (strncmp("postcore", str, strlen("postcore")) == 0) {
4854 			panic_with_options(0, NULL, DEBUGGER_OPTION_RECURPANIC_POSTCORE, "test recursive panic subsequent to on-device core");
4855 		}
4856 	}
4857 
4858 	return rval;
4859 }
4860 
4861 static int
4862 sysctl_debugger_test SYSCTL_HANDLER_ARGS
4863 {
4864 #pragma unused(arg1, arg2)
4865 	int rval = 0;
4866 	char str[32] = "entry prelog postlog postcore";
4867 
4868 	rval = sysctl_handle_string(oidp, str, sizeof(str), req);
4869 
4870 	if (rval == 0 && req->newptr) {
4871 		if (strncmp("entry", str, strlen("entry")) == 0) {
4872 			DebuggerWithContext(0, NULL, "test recursive panic via debugger at entry", DEBUGGER_OPTION_RECURPANIC_ENTRY, (unsigned long)(char *)__builtin_return_address(0));
4873 		} else if (strncmp("prelog", str, strlen("prelog")) == 0) {
4874 			DebuggerWithContext(0, NULL, "test recursive panic via debugger prior to writing a paniclog", DEBUGGER_OPTION_RECURPANIC_PRELOG, (unsigned long)(char *)__builtin_return_address(0));
4875 		} else if (strncmp("postlog", str, strlen("postlog")) == 0) {
4876 			DebuggerWithContext(0, NULL, "test recursive panic via debugger subsequent to paniclog", DEBUGGER_OPTION_RECURPANIC_POSTLOG, (unsigned long)(char *)__builtin_return_address(0));
4877 		} else if (strncmp("postcore", str, strlen("postcore")) == 0) {
4878 			DebuggerWithContext(0, NULL, "test recursive panic via debugger subsequent to on-device core", DEBUGGER_OPTION_RECURPANIC_POSTCORE, (unsigned long)(char *)__builtin_return_address(0));
4879 		}
4880 	}
4881 
4882 	return rval;
4883 }
4884 
4885 decl_lck_spin_data(, spinlock_panic_test_lock);
4886 
4887 __attribute__((noreturn))
4888 static void
spinlock_panic_test_acquire_spinlock(void * arg __unused,wait_result_t wres __unused)4889 spinlock_panic_test_acquire_spinlock(void * arg __unused, wait_result_t wres __unused)
4890 {
4891 	lck_spin_lock(&spinlock_panic_test_lock);
4892 	while (1) {
4893 		;
4894 	}
4895 }
4896 
4897 static int
4898 sysctl_spinlock_panic_test SYSCTL_HANDLER_ARGS
4899 {
4900 #pragma unused(oidp, arg1, arg2)
4901 	if (req->newlen == 0) {
4902 		return EINVAL;
4903 	}
4904 
4905 	thread_t panic_spinlock_thread;
4906 	/* Initialize panic spinlock */
4907 	lck_grp_t * panic_spinlock_grp;
4908 	lck_grp_attr_t * panic_spinlock_grp_attr;
4909 	lck_attr_t * panic_spinlock_attr;
4910 
4911 	panic_spinlock_grp_attr = lck_grp_attr_alloc_init();
4912 	panic_spinlock_grp = lck_grp_alloc_init("panic_spinlock", panic_spinlock_grp_attr);
4913 	panic_spinlock_attr = lck_attr_alloc_init();
4914 
4915 	lck_spin_init(&spinlock_panic_test_lock, panic_spinlock_grp, panic_spinlock_attr);
4916 
4917 
4918 	/* Create thread to acquire spinlock */
4919 	if (kernel_thread_start(spinlock_panic_test_acquire_spinlock, NULL, &panic_spinlock_thread) != KERN_SUCCESS) {
4920 		return EBUSY;
4921 	}
4922 
4923 	/* Try to acquire spinlock -- should panic eventually */
4924 	lck_spin_lock(&spinlock_panic_test_lock);
4925 	while (1) {
4926 		;
4927 	}
4928 }
4929 
4930 __attribute__((noreturn))
4931 static void
simultaneous_panic_worker(void * arg,wait_result_t wres __unused)4932 simultaneous_panic_worker
4933 (void * arg, wait_result_t wres __unused)
4934 {
4935 	atomic_int *start_panic = (atomic_int *)arg;
4936 
4937 	while (!atomic_load(start_panic)) {
4938 		;
4939 	}
4940 	panic("SIMULTANEOUS PANIC TEST: INITIATING PANIC FROM CPU %d", cpu_number());
4941 	__builtin_unreachable();
4942 }
4943 
4944 static int
4945 sysctl_simultaneous_panic_test SYSCTL_HANDLER_ARGS
4946 {
4947 #pragma unused(oidp, arg1, arg2)
4948 	if (req->newlen == 0) {
4949 		return EINVAL;
4950 	}
4951 
4952 	int i = 0, threads_to_create = 2 * processor_count;
4953 	atomic_int start_panic = 0;
4954 	unsigned int threads_created = 0;
4955 	thread_t new_panic_thread;
4956 
4957 	for (i = threads_to_create; i > 0; i--) {
4958 		if (kernel_thread_start(simultaneous_panic_worker, (void *) &start_panic, &new_panic_thread) == KERN_SUCCESS) {
4959 			threads_created++;
4960 		}
4961 	}
4962 
4963 	/* FAIL if we couldn't create at least processor_count threads */
4964 	if (threads_created < processor_count) {
4965 		panic("SIMULTANEOUS PANIC TEST: FAILED TO CREATE ENOUGH THREADS, ONLY CREATED %d (of %d)",
4966 		    threads_created, threads_to_create);
4967 	}
4968 
4969 	atomic_exchange(&start_panic, 1);
4970 	while (1) {
4971 		;
4972 	}
4973 }
4974 
4975 SYSCTL_PROC(_debug, OID_AUTO, panic_test, CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_MASKED, 0, 0, sysctl_panic_test, "A", "panic test");
4976 SYSCTL_PROC(_debug, OID_AUTO, debugger_test, CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_MASKED, 0, 0, sysctl_debugger_test, "A", "debugger test");
4977 SYSCTL_PROC(_debug, OID_AUTO, spinlock_panic_test, CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_MASKED, 0, 0, sysctl_spinlock_panic_test, "A", "spinlock panic test");
4978 SYSCTL_PROC(_debug, OID_AUTO, simultaneous_panic_test, CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_MASKED, 0, 0, sysctl_simultaneous_panic_test, "A", "simultaneous panic test");
4979 
4980 extern int exc_resource_threads_enabled;
4981 
4982 SYSCTL_INT(_kern, OID_AUTO, exc_resource_threads_enabled, CTLFLAG_RD | CTLFLAG_LOCKED, &exc_resource_threads_enabled, 0, "exc_resource thread limit enabled");
4983 
4984 
4985 #endif /* DEVELOPMENT || DEBUG */
4986 
4987 #if CONFIG_THREAD_GROUPS
4988 #if DEVELOPMENT || DEBUG
4989 
4990 static int
4991 sysctl_get_thread_group_id SYSCTL_HANDLER_ARGS
4992 {
4993 #pragma unused(arg1, arg2, oidp)
4994 	uint64_t thread_group_id = thread_group_get_id(thread_group_get(current_thread()));
4995 	return SYSCTL_OUT(req, &thread_group_id, sizeof(thread_group_id));
4996 }
4997 
4998 SYSCTL_PROC(_kern, OID_AUTO, thread_group_id, CTLFLAG_RD | CTLFLAG_LOCKED | CTLTYPE_QUAD,
4999     0, 0, &sysctl_get_thread_group_id, "I", "thread group id of the thread");
5000 
5001 STATIC int
sysctl_thread_group_count(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)5002 sysctl_thread_group_count(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
5003 {
5004 	int value = thread_group_count();
5005 	return sysctl_io_number(req, value, sizeof(value), NULL, NULL);
5006 }
5007 
5008 SYSCTL_PROC(_kern, OID_AUTO, thread_group_count, CTLFLAG_RD | CTLFLAG_LOCKED | CTLFLAG_KERN,
5009     0, 0, &sysctl_thread_group_count, "I", "count of thread groups");
5010 
5011 #endif /* DEVELOPMENT || DEBUG */
5012 const uint32_t thread_groups_supported = 1;
5013 #else /* CONFIG_THREAD_GROUPS */
5014 const uint32_t thread_groups_supported = 0;
5015 #endif /* CONFIG_THREAD_GROUPS */
5016 
5017 STATIC int
sysctl_thread_groups_supported(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)5018 sysctl_thread_groups_supported(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
5019 {
5020 	int value = thread_groups_supported;
5021 	return sysctl_io_number(req, value, sizeof(value), NULL, NULL);
5022 }
5023 
5024 SYSCTL_PROC(_kern, OID_AUTO, thread_groups_supported, CTLFLAG_RD | CTLFLAG_LOCKED | CTLFLAG_KERN,
5025     0, 0, &sysctl_thread_groups_supported, "I", "thread groups supported");
5026 
5027 static int
5028 sysctl_grade_cputype SYSCTL_HANDLER_ARGS
5029 {
5030 #pragma unused(arg1, arg2, oidp)
5031 	int error = 0;
5032 	int type_tuple[2] = {};
5033 	int return_value = 0;
5034 
5035 	error = SYSCTL_IN(req, &type_tuple, sizeof(type_tuple));
5036 
5037 	if (error) {
5038 		return error;
5039 	}
5040 
5041 	return_value = grade_binary(type_tuple[0], type_tuple[1] & ~CPU_SUBTYPE_MASK, type_tuple[1] & CPU_SUBTYPE_MASK, FALSE);
5042 
5043 	error = SYSCTL_OUT(req, &return_value, sizeof(return_value));
5044 
5045 	if (error) {
5046 		return error;
5047 	}
5048 
5049 	return error;
5050 }
5051 
5052 SYSCTL_PROC(_kern, OID_AUTO, grade_cputype,
5053     CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MASKED | CTLFLAG_LOCKED | CTLTYPE_OPAQUE,
5054     0, 0, &sysctl_grade_cputype, "S",
5055     "grade value of cpu_type_t+cpu_sub_type_t");
5056 
5057 extern boolean_t allow_direct_handoff;
5058 SYSCTL_INT(_kern, OID_AUTO, direct_handoff,
5059     CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED,
5060     &allow_direct_handoff, 0, "Enable direct handoff for realtime threads");
5061 
5062 #if DEVELOPMENT || DEBUG
5063 
5064 SYSCTL_LONG(_kern, OID_AUTO, phys_carveout_pa, CTLFLAG_RD | CTLFLAG_LOCKED | CTLFLAG_KERN,
5065     &phys_carveout_pa,
5066     "base physical address of the phys_carveout_mb boot-arg region");
5067 SYSCTL_LONG(_kern, OID_AUTO, phys_carveout_size, CTLFLAG_RD | CTLFLAG_LOCKED | CTLFLAG_KERN,
5068     &phys_carveout_size,
5069     "size in bytes of the phys_carveout_mb boot-arg region");
5070 SYSCTL_LONG(_kern, OID_AUTO, phys_carveout_metadata_pa, CTLFLAG_RD | CTLFLAG_LOCKED | CTLFLAG_KERN,
5071     &phys_carveout_metadata_pa,
5072     "base physical address of the phys_carveout_metadata region");
5073 SYSCTL_LONG(_kern, OID_AUTO, phys_carveout_metadata_size, CTLFLAG_RD | CTLFLAG_LOCKED | CTLFLAG_KERN,
5074     &phys_carveout_metadata_size,
5075     "size in bytes of the phys_carveout_metadata region");
5076 
5077 extern void do_cseg_wedge_thread(void);
5078 extern void do_cseg_unwedge_thread(void);
5079 
5080 static int
5081 cseg_wedge_thread SYSCTL_HANDLER_ARGS
5082 {
5083 #pragma unused(arg1, arg2)
5084 
5085 	int error, val = 0;
5086 	error = sysctl_handle_int(oidp, &val, 0, req);
5087 	if (error || val == 0) {
5088 		return error;
5089 	}
5090 
5091 	do_cseg_wedge_thread();
5092 	return 0;
5093 }
5094 SYSCTL_PROC(_kern, OID_AUTO, cseg_wedge_thread, CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_MASKED, 0, 0, cseg_wedge_thread, "I", "wedge c_seg thread");
5095 
5096 static int
5097 cseg_unwedge_thread SYSCTL_HANDLER_ARGS
5098 {
5099 #pragma unused(arg1, arg2)
5100 
5101 	int error, val = 0;
5102 	error = sysctl_handle_int(oidp, &val, 0, req);
5103 	if (error || val == 0) {
5104 		return error;
5105 	}
5106 
5107 	do_cseg_unwedge_thread();
5108 	return 0;
5109 }
5110 SYSCTL_PROC(_kern, OID_AUTO, cseg_unwedge_thread, CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_MASKED, 0, 0, cseg_unwedge_thread, "I", "unstuck c_seg thread");
5111 
5112 static atomic_int wedge_thread_should_wake = 0;
5113 
5114 static int
5115 unwedge_thread SYSCTL_HANDLER_ARGS
5116 {
5117 #pragma unused(arg1, arg2)
5118 	int error, val = 0;
5119 	error = sysctl_handle_int(oidp, &val, 0, req);
5120 	if (error || val == 0) {
5121 		return error;
5122 	}
5123 
5124 	atomic_store(&wedge_thread_should_wake, 1);
5125 	return 0;
5126 }
5127 
5128 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");
5129 
5130 static int
5131 wedge_thread SYSCTL_HANDLER_ARGS
5132 {
5133 #pragma unused(arg1, arg2)
5134 
5135 	int error, val = 0;
5136 	error = sysctl_handle_int(oidp, &val, 0, req);
5137 	if (error || val == 0) {
5138 		return error;
5139 	}
5140 
5141 	uint64_t interval = 1;
5142 	nanoseconds_to_absolutetime(1000 * 1000 * 50, &interval);
5143 
5144 	atomic_store(&wedge_thread_should_wake, 0);
5145 	while (!atomic_load(&wedge_thread_should_wake)) {
5146 		tsleep1(NULL, 0, "wedge_thread", mach_absolute_time() + interval, NULL);
5147 	}
5148 
5149 	return 0;
5150 }
5151 
5152 SYSCTL_PROC(_kern, OID_AUTO, wedge_thread,
5153     CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_LOCKED, 0, 0, wedge_thread, "I",
5154     "wedge this thread so it cannot be cleaned up");
5155 
5156 static int
5157 sysctl_total_corpses_count SYSCTL_HANDLER_ARGS
5158 {
5159 #pragma unused(oidp, arg1, arg2)
5160 	extern unsigned long total_corpses_count(void);
5161 
5162 	unsigned long corpse_count_long = total_corpses_count();
5163 	unsigned int corpse_count = (unsigned int)MIN(corpse_count_long, UINT_MAX);
5164 	return sysctl_io_opaque(req, &corpse_count, sizeof(corpse_count), NULL);
5165 }
5166 
5167 SYSCTL_PROC(_kern, OID_AUTO, total_corpses_count,
5168     CTLFLAG_RD | CTLFLAG_ANYBODY | CTLFLAG_LOCKED, 0, 0,
5169     sysctl_total_corpses_count, "I", "total corpses on the system");
5170 
5171 static int
5172 sysctl_turnstile_test_prim_lock SYSCTL_HANDLER_ARGS;
5173 static int
5174 sysctl_turnstile_test_prim_unlock SYSCTL_HANDLER_ARGS;
5175 int
5176 tstile_test_prim_lock(boolean_t use_hashtable);
5177 int
5178 tstile_test_prim_unlock(boolean_t use_hashtable);
5179 
5180 static int
5181 sysctl_turnstile_test_prim_lock SYSCTL_HANDLER_ARGS
5182 {
5183 #pragma unused(arg1, arg2)
5184 	int error, val = 0;
5185 	error = sysctl_handle_int(oidp, &val, 0, req);
5186 	if (error || val == 0) {
5187 		return error;
5188 	}
5189 	switch (val) {
5190 	case SYSCTL_TURNSTILE_TEST_USER_DEFAULT:
5191 	case SYSCTL_TURNSTILE_TEST_USER_HASHTABLE:
5192 	case SYSCTL_TURNSTILE_TEST_KERNEL_DEFAULT:
5193 	case SYSCTL_TURNSTILE_TEST_KERNEL_HASHTABLE:
5194 		return tstile_test_prim_lock(val);
5195 	default:
5196 		return error;
5197 	}
5198 }
5199 
5200 static int
5201 sysctl_turnstile_test_prim_unlock SYSCTL_HANDLER_ARGS
5202 {
5203 #pragma unused(arg1, arg2)
5204 	int error, val = 0;
5205 	error = sysctl_handle_int(oidp, &val, 0, req);
5206 	if (error || val == 0) {
5207 		return error;
5208 	}
5209 	switch (val) {
5210 	case SYSCTL_TURNSTILE_TEST_USER_DEFAULT:
5211 	case SYSCTL_TURNSTILE_TEST_USER_HASHTABLE:
5212 	case SYSCTL_TURNSTILE_TEST_KERNEL_DEFAULT:
5213 	case SYSCTL_TURNSTILE_TEST_KERNEL_HASHTABLE:
5214 		return tstile_test_prim_unlock(val);
5215 	default:
5216 		return error;
5217 	}
5218 }
5219 
5220 SYSCTL_PROC(_kern, OID_AUTO, turnstiles_test_lock, CTLFLAG_WR | CTLFLAG_ANYBODY | CTLFLAG_KERN | CTLFLAG_LOCKED,
5221     0, 0, sysctl_turnstile_test_prim_lock, "I", "turnstiles test lock");
5222 
5223 SYSCTL_PROC(_kern, OID_AUTO, turnstiles_test_unlock, CTLFLAG_WR | CTLFLAG_ANYBODY | CTLFLAG_KERN | CTLFLAG_LOCKED,
5224     0, 0, sysctl_turnstile_test_prim_unlock, "I", "turnstiles test unlock");
5225 
5226 int
5227 turnstile_get_boost_stats_sysctl(void *req);
5228 int
5229 turnstile_get_unboost_stats_sysctl(void *req);
5230 static int
5231 sysctl_turnstile_boost_stats SYSCTL_HANDLER_ARGS;
5232 static int
5233 sysctl_turnstile_unboost_stats SYSCTL_HANDLER_ARGS;
5234 extern uint64_t thread_block_on_turnstile_count;
5235 extern uint64_t thread_block_on_regular_waitq_count;
5236 
5237 static int
5238 sysctl_turnstile_boost_stats SYSCTL_HANDLER_ARGS
5239 {
5240 #pragma unused(arg1, arg2, oidp)
5241 	return turnstile_get_boost_stats_sysctl(req);
5242 }
5243 
5244 static int
5245 sysctl_turnstile_unboost_stats SYSCTL_HANDLER_ARGS
5246 {
5247 #pragma unused(arg1, arg2, oidp)
5248 	return turnstile_get_unboost_stats_sysctl(req);
5249 }
5250 
5251 SYSCTL_PROC(_kern, OID_AUTO, turnstile_boost_stats, CTLFLAG_RD | CTLFLAG_ANYBODY | CTLFLAG_KERN | CTLFLAG_LOCKED | CTLTYPE_STRUCT,
5252     0, 0, sysctl_turnstile_boost_stats, "S", "turnstiles boost stats");
5253 SYSCTL_PROC(_kern, OID_AUTO, turnstile_unboost_stats, CTLFLAG_RD | CTLFLAG_ANYBODY | CTLFLAG_KERN | CTLFLAG_LOCKED | CTLTYPE_STRUCT,
5254     0, 0, sysctl_turnstile_unboost_stats, "S", "turnstiles unboost stats");
5255 SYSCTL_QUAD(_kern, OID_AUTO, thread_block_count_on_turnstile,
5256     CTLFLAG_RD | CTLFLAG_ANYBODY | CTLFLAG_KERN | CTLFLAG_LOCKED,
5257     &thread_block_on_turnstile_count, "thread blocked on turnstile count");
5258 SYSCTL_QUAD(_kern, OID_AUTO, thread_block_count_on_reg_waitq,
5259     CTLFLAG_RD | CTLFLAG_ANYBODY | CTLFLAG_KERN | CTLFLAG_LOCKED,
5260     &thread_block_on_regular_waitq_count, "thread blocked on regular waitq count");
5261 
5262 #if defined(__x86_64__)
5263 extern uint64_t MutexSpin;
5264 
5265 SYSCTL_QUAD(_kern, OID_AUTO, mutex_spin_abs, CTLFLAG_RW, &MutexSpin,
5266     "Spin time in abs for acquiring a kernel mutex");
5267 #else
5268 extern machine_timeout32_t MutexSpin;
5269 
5270 SYSCTL_UINT(_kern, OID_AUTO, mutex_spin_abs, CTLFLAG_RW, &MutexSpin, 0,
5271     "Spin time in abs for acquiring a kernel mutex");
5272 #endif
5273 
5274 extern uint64_t low_MutexSpin;
5275 extern int64_t high_MutexSpin;
5276 extern unsigned int real_ncpus;
5277 
5278 SYSCTL_QUAD(_kern, OID_AUTO, low_mutex_spin_abs, CTLFLAG_RW, &low_MutexSpin,
5279     "Low spin threshold in abs for acquiring a kernel mutex");
5280 
5281 static int
5282 sysctl_high_mutex_spin_ns SYSCTL_HANDLER_ARGS
5283 {
5284 #pragma unused(oidp, arg1, arg2)
5285 	int error;
5286 	int64_t val = 0;
5287 	int64_t res;
5288 
5289 	/* Check if the user is writing to high_MutexSpin, or just reading it */
5290 	if (req->newptr) {
5291 		error = SYSCTL_IN(req, &val, sizeof(val));
5292 		if (error || (val < 0 && val != -1)) {
5293 			return error;
5294 		}
5295 		high_MutexSpin = val;
5296 	}
5297 
5298 	if (high_MutexSpin >= 0) {
5299 		res = high_MutexSpin;
5300 	} else {
5301 		res = low_MutexSpin * real_ncpus;
5302 	}
5303 	return SYSCTL_OUT(req, &res, sizeof(res));
5304 }
5305 SYSCTL_PROC(_kern, OID_AUTO, high_mutex_spin_abs, CTLFLAG_RW | CTLTYPE_QUAD, 0, 0, sysctl_high_mutex_spin_ns, "I",
5306     "High spin threshold in abs for acquiring a kernel mutex");
5307 
5308 
5309 #if defined (__x86_64__)
5310 
5311 semaphore_t sysctl_test_panic_with_thread_sem;
5312 
5313 #pragma clang diagnostic push
5314 #pragma clang diagnostic ignored "-Winfinite-recursion" /* rdar://38801963 */
5315 __attribute__((noreturn))
5316 static void
panic_thread_test_child_spin(void * arg,wait_result_t wres)5317 panic_thread_test_child_spin(void * arg, wait_result_t wres)
5318 {
5319 	static int panic_thread_recurse_count = 5;
5320 
5321 	if (panic_thread_recurse_count > 0) {
5322 		panic_thread_recurse_count--;
5323 		panic_thread_test_child_spin(arg, wres);
5324 	}
5325 
5326 	semaphore_signal(sysctl_test_panic_with_thread_sem);
5327 	while (1) {
5328 		;
5329 	}
5330 }
5331 #pragma clang diagnostic pop
5332 
5333 static void
panic_thread_test_child_park(void * arg __unused,wait_result_t wres __unused)5334 panic_thread_test_child_park(void * arg __unused, wait_result_t wres __unused)
5335 {
5336 	int event;
5337 
5338 	assert_wait(&event, THREAD_UNINT);
5339 	semaphore_signal(sysctl_test_panic_with_thread_sem);
5340 	thread_block(panic_thread_test_child_park);
5341 }
5342 
5343 static int
5344 sysctl_test_panic_with_thread SYSCTL_HANDLER_ARGS
5345 {
5346 #pragma unused(arg1, arg2)
5347 	int rval = 0;
5348 	char str[16] = { '\0' };
5349 	thread_t child_thread = THREAD_NULL;
5350 
5351 	rval = sysctl_handle_string(oidp, str, sizeof(str), req);
5352 	if (rval != 0 || !req->newptr) {
5353 		return EINVAL;
5354 	}
5355 
5356 	semaphore_create(kernel_task, &sysctl_test_panic_with_thread_sem, SYNC_POLICY_FIFO, 0);
5357 
5358 	/* Create thread to spin or park in continuation */
5359 	if (strncmp("spin", str, strlen("spin")) == 0) {
5360 		if (kernel_thread_start(panic_thread_test_child_spin, NULL, &child_thread) != KERN_SUCCESS) {
5361 			semaphore_destroy(kernel_task, sysctl_test_panic_with_thread_sem);
5362 			return EBUSY;
5363 		}
5364 	} else if (strncmp("continuation", str, strlen("continuation")) == 0) {
5365 		if (kernel_thread_start(panic_thread_test_child_park, NULL, &child_thread) != KERN_SUCCESS) {
5366 			semaphore_destroy(kernel_task, sysctl_test_panic_with_thread_sem);
5367 			return EBUSY;
5368 		}
5369 	} else {
5370 		semaphore_destroy(kernel_task, sysctl_test_panic_with_thread_sem);
5371 		return EINVAL;
5372 	}
5373 
5374 	semaphore_wait(sysctl_test_panic_with_thread_sem);
5375 
5376 	panic_with_thread_context(0, NULL, 0, child_thread, "testing panic_with_thread_context for thread %p", child_thread);
5377 
5378 	/* Not reached */
5379 	return EINVAL;
5380 }
5381 
5382 SYSCTL_PROC(_kern, OID_AUTO, test_panic_with_thread,
5383     CTLFLAG_MASKED | CTLFLAG_KERN | CTLFLAG_LOCKED | CTLFLAG_WR | CTLTYPE_STRING,
5384     0, 0, sysctl_test_panic_with_thread, "A", "test panic flow for backtracing a different thread");
5385 #endif /* defined (__x86_64__) */
5386 
5387 #endif /* DEVELOPMENT || DEBUG */
5388 
5389 static int
5390 sysctl_get_owned_vmobjects SYSCTL_HANDLER_ARGS
5391 {
5392 #pragma unused(oidp, arg1, arg2)
5393 
5394 	/* validate */
5395 	if (req->newlen != sizeof(mach_port_name_t) || req->newptr == USER_ADDR_NULL ||
5396     req->oldidx != 0 || req->newidx != 0 || req->p == NULL ||
5397     (req->oldlen == 0 && req->oldptr != USER_ADDR_NULL)) {
5398 		return EINVAL;
5399 	}
5400 
5401 	int error;
5402 	mach_port_name_t task_port_name;
5403 	task_t task;
5404 	size_t buffer_size = (req->oldptr != USER_ADDR_NULL) ? req->oldlen : 0;
5405 	vmobject_list_output_t buffer = NULL;
5406 	size_t output_size;
5407 	size_t entries;
5408 
5409 	/* we have a "newptr" (for write) we get a task port name from the caller. */
5410 	error = SYSCTL_IN(req, &task_port_name, sizeof(mach_port_name_t));
5411 
5412 	if (error != 0) {
5413 		goto sysctl_get_vmobject_list_exit;
5414 	}
5415 
5416 	task = port_name_to_task_read(task_port_name);
5417 	if (task == TASK_NULL) {
5418 		error = ESRCH;
5419 		goto sysctl_get_vmobject_list_exit;
5420 	}
5421 
5422 	bool corpse = is_corpsetask(task);
5423 
5424 	/* get the current size */
5425 	size_t max_size;
5426 	task_get_owned_vmobjects(task, 0, NULL, &max_size, &entries);
5427 
5428 	if (buffer_size && (buffer_size < sizeof(*buffer) + sizeof(vm_object_query_data_t))) {
5429 		error = ENOMEM;
5430 		goto sysctl_get_vmobject_list_deallocate_and_exit;
5431 	}
5432 
5433 	if (corpse == false) {
5434 		/* copy the vmobjects and vmobject data out of the task */
5435 		if (buffer_size == 0) {
5436 			output_size = max_size;
5437 		} else {
5438 			buffer_size = (buffer_size > max_size) ? max_size : buffer_size;
5439 			buffer = (struct _vmobject_list_output_ *)kalloc_data(buffer_size, Z_WAITOK);
5440 
5441 			if (!buffer) {
5442 				error = ENOMEM;
5443 				goto sysctl_get_vmobject_list_deallocate_and_exit;
5444 			}
5445 
5446 			task_get_owned_vmobjects(task, buffer_size, buffer, &output_size, &entries);
5447 		}
5448 
5449 		/* req->oldptr should be USER_ADDR_NULL if buffer == NULL and return the current size */
5450 		/* otherwise copy buffer to oldptr and return the bytes copied */
5451 		error = SYSCTL_OUT(req, (char *)buffer, output_size);
5452 	} else {
5453 		vmobject_list_output_t list;
5454 
5455 		task_get_corpse_vmobject_list(task, &list, &max_size);
5456 		assert(buffer == NULL);
5457 
5458 		/* copy corpse_vmobject_list to output buffer to avoid double copy */
5459 		if (buffer_size) {
5460 			size_t temp_size;
5461 
5462 			temp_size = buffer_size > max_size ? max_size : buffer_size;
5463 			output_size = temp_size - sizeof(*buffer);
5464 			/* whole multiple of vm_object_query_data_t */
5465 			output_size = (output_size / sizeof(vm_object_query_data_t)) * sizeof(vm_object_query_data_t) + sizeof(*buffer);
5466 			buffer = list;
5467 		} else {
5468 			output_size = max_size;
5469 		}
5470 
5471 		/* req->oldptr should be USER_ADDR_NULL if buffer == NULL and return the current size */
5472 		/* otherwise copy buffer to oldptr and return the bytes copied */
5473 		error = SYSCTL_OUT(req, (char*)buffer, output_size);
5474 		buffer = NULL;
5475 	}
5476 
5477 sysctl_get_vmobject_list_deallocate_and_exit:
5478 	task_deallocate(task);
5479 
5480 sysctl_get_vmobject_list_exit:
5481 	if (buffer) {
5482 		kfree_data(buffer, buffer_size);
5483 	}
5484 
5485 	return error;
5486 }
5487 
5488 SYSCTL_PROC(_vm, OID_AUTO, get_owned_vmobjects,
5489     CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_WR | CTLFLAG_MASKED | CTLFLAG_KERN | CTLFLAG_LOCKED | CTLFLAG_ANYBODY,
5490     0, 0, sysctl_get_owned_vmobjects, "A", "get owned vmobjects in task");
5491 
5492 extern uint64_t num_static_scalable_counters;
5493 SYSCTL_QUAD(_kern, OID_AUTO, num_static_scalable_counters, CTLFLAG_RD | CTLFLAG_LOCKED, &num_static_scalable_counters, "");
5494 
5495 uuid_string_t trial_treatment_id;
5496 uuid_string_t trial_experiment_id;
5497 int trial_deployment_id = -1;
5498 
5499 SYSCTL_STRING(_kern, OID_AUTO, trial_treatment_id, CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_ANYBODY | CTLFLAG_EXPERIMENT, trial_treatment_id, sizeof(trial_treatment_id), "");
5500 SYSCTL_STRING(_kern, OID_AUTO, trial_experiment_id, CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_ANYBODY | CTLFLAG_EXPERIMENT, trial_experiment_id, sizeof(trial_experiment_id), "");
5501 SYSCTL_INT(_kern, OID_AUTO, trial_deployment_id, CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_ANYBODY | CTLFLAG_EXPERIMENT, &trial_deployment_id, 0, "");
5502 
5503 #if (DEVELOPMENT || DEBUG)
5504 /* For unit testing setting factors & limits. */
5505 unsigned int testing_experiment_factor;
5506 EXPERIMENT_FACTOR_UINT(_kern, testing_experiment_factor, &testing_experiment_factor, 5, 10, "");
5507 
5508 extern int exception_log_max_pid;
5509 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");
5510 #endif /* (DEVELOPMENT || DEBUG) */
5511