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