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