2 * Copyright (c) 2000-2020 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Mach Operating System
30 * Copyright (c) 1987 Carnegie-Mellon University
31 * All rights reserved. The CMU software License Agreement specifies
32 * the terms and conditions for use and redistribution.
35 * NOTICE: This file was modified by SPARTA, Inc. in 2006 to introduce
36 * support for mandatory and extensible security protections. This notice
37 * is included in support of clause 2.2 (b) of the Apple Public License,
40 #include <vm/vm_options.h>
42 #include <kern/task.h>
43 #include <kern/thread.h>
44 #include <kern/debug.h>
45 #include <kern/extmod_statistics.h>
46 #include <mach/mach_traps.h>
47 #include <mach/port.h>
49 #include <mach/task.h>
50 #include <mach/task_access.h>
51 #include <mach/task_special_ports.h>
52 #include <mach/time_value.h>
53 #include <mach/vm_map.h>
54 #include <mach/vm_param.h>
55 #include <mach/vm_prot.h>
56 #include <machine/machine_routines.h>
58 #include <sys/file_internal.h>
59 #include <sys/param.h>
60 #include <sys/systm.h>
62 #include <sys/namei.h>
63 #include <sys/proc_internal.h>
64 #include <sys/kauth.h>
67 #include <sys/vnode_internal.h>
68 #include <sys/mount.h>
69 #include <sys/xattr.h>
70 #include <sys/trace.h>
71 #include <sys/kernel.h>
72 #include <sys/ubc_internal.h>
74 #include <sys/syslog.h>
76 #include <sys/sysproto.h>
78 #include <sys/sysctl.h>
79 #include <sys/cprotect.h>
80 #include <sys/kpi_socket.h>
81 #include <sys/kas_info.h>
82 #include <sys/socket.h>
83 #include <sys/socketvar.h>
84 #include <sys/random.h>
89 #include <security/audit/audit.h>
90 #include <security/mac.h>
91 #include <bsm/audit_kevents.h>
93 #include <kern/kalloc.h>
94 #include <vm/vm_map.h>
95 #include <vm/vm_kern.h>
96 #include <vm/vm_pageout.h>
98 #include <mach/shared_region.h>
99 #include <vm/vm_shared_region.h>
101 #include <vm/vm_protos.h>
103 #include <sys/kern_memorystatus.h>
104 #include <sys/kern_memorystatus_freeze.h>
105 #include <sys/proc_internal.h>
108 #include <security/mac_framework.h>
111 #include <kern/bits.h>
115 #endif /* CONFIG_CSR */
116 #include <IOKit/IOBSD.h>
118 #if VM_MAP_DEBUG_APPLE_PROTECT
119 SYSCTL_INT(_vm
, OID_AUTO
, map_debug_apple_protect
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &vm_map_debug_apple_protect
, 0, "");
120 #endif /* VM_MAP_DEBUG_APPLE_PROTECT */
122 #if VM_MAP_DEBUG_FOURK
123 SYSCTL_INT(_vm
, OID_AUTO
, map_debug_fourk
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &vm_map_debug_fourk
, 0, "");
124 #endif /* VM_MAP_DEBUG_FOURK */
126 #if DEVELOPMENT || DEBUG
129 sysctl_kmem_alloc_contig SYSCTL_HANDLER_ARGS
131 #pragma unused(arg1, arg2)
137 error
= sysctl_handle_int(oidp
, &size
, 0, req
);
138 if (error
|| !req
->newptr
) {
142 kr
= kmem_alloc_contig(kernel_map
, &kaddr
, (vm_size_t
)size
, 0, 0, 0, 0, VM_KERN_MEMORY_IOKIT
);
144 if (kr
== KERN_SUCCESS
) {
145 kmem_free(kernel_map
, kaddr
, size
);
151 SYSCTL_PROC(_vm
, OID_AUTO
, kmem_alloc_contig
, CTLTYPE_INT
| CTLFLAG_WR
| CTLFLAG_LOCKED
| CTLFLAG_MASKED
,
152 0, 0, &sysctl_kmem_alloc_contig
, "I", "");
154 extern int vm_region_footprint
;
155 SYSCTL_INT(_vm
, OID_AUTO
, region_footprint
, CTLFLAG_RW
| CTLFLAG_ANYBODY
| CTLFLAG_LOCKED
, &vm_region_footprint
, 0, "");
157 #endif /* DEVELOPMENT || DEBUG */
160 sysctl_vm_self_region_footprint SYSCTL_HANDLER_ARGS
162 #pragma unused(arg1, arg2, oidp)
166 value
= task_self_region_footprint();
167 error
= SYSCTL_OUT(req
, &value
, sizeof(int));
176 error
= SYSCTL_IN(req
, &value
, sizeof(int));
180 task_self_region_footprint_set(value
);
183 SYSCTL_PROC(_vm
, OID_AUTO
, self_region_footprint
, CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_ANYBODY
| CTLFLAG_LOCKED
| CTLFLAG_MASKED
, 0, 0, &sysctl_vm_self_region_footprint
, "I", "");
186 sysctl_vm_self_region_page_size SYSCTL_HANDLER_ARGS
188 #pragma unused(arg1, arg2, oidp)
192 value
= (1 << thread_self_region_page_shift());
193 error
= SYSCTL_OUT(req
, &value
, sizeof(int));
202 error
= SYSCTL_IN(req
, &value
, sizeof(int));
207 if (value
!= 0 && value
!= 4096 && value
!= 16384) {
211 #if !__ARM_MIXED_PAGE_SIZE__
212 if (value
!= vm_map_page_size(current_map())) {
215 #endif /* !__ARM_MIXED_PAGE_SIZE__ */
217 thread_self_region_page_shift_set(bit_first(value
));
220 SYSCTL_PROC(_vm
, OID_AUTO
, self_region_page_size
, CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_ANYBODY
| CTLFLAG_LOCKED
| CTLFLAG_MASKED
, 0, 0, &sysctl_vm_self_region_page_size
, "I", "");
223 #if DEVELOPMENT || DEBUG
224 extern int panic_on_unsigned_execute
;
225 SYSCTL_INT(_vm
, OID_AUTO
, panic_on_unsigned_execute
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &panic_on_unsigned_execute
, 0, "");
226 #endif /* DEVELOPMENT || DEBUG */
228 extern int cs_executable_create_upl
;
229 extern int cs_executable_wire
;
230 SYSCTL_INT(_vm
, OID_AUTO
, cs_executable_create_upl
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &cs_executable_create_upl
, 0, "");
231 SYSCTL_INT(_vm
, OID_AUTO
, cs_executable_wire
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &cs_executable_wire
, 0, "");
233 #if DEVELOPMENT || DEBUG
234 extern int radar_20146450
;
235 SYSCTL_INT(_vm
, OID_AUTO
, radar_20146450
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &radar_20146450
, 0, "");
237 extern int macho_printf
;
238 SYSCTL_INT(_vm
, OID_AUTO
, macho_printf
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &macho_printf
, 0, "");
240 extern int apple_protect_pager_data_request_debug
;
241 SYSCTL_INT(_vm
, OID_AUTO
, apple_protect_pager_data_request_debug
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &apple_protect_pager_data_request_debug
, 0, "");
243 #if __arm__ || __arm64__
244 /* These are meant to support the page table accounting unit test. */
245 extern unsigned int arm_hardware_page_size
;
246 extern unsigned int arm_pt_desc_size
;
247 extern unsigned int arm_pt_root_size
;
248 extern unsigned int free_page_size_tt_count
;
249 extern unsigned int free_two_page_size_tt_count
;
250 extern unsigned int free_tt_count
;
251 extern unsigned int inuse_user_tteroot_count
;
252 extern unsigned int inuse_kernel_tteroot_count
;
253 extern unsigned int inuse_user_ttepages_count
;
254 extern unsigned int inuse_kernel_ttepages_count
;
255 extern unsigned int inuse_user_ptepages_count
;
256 extern unsigned int inuse_kernel_ptepages_count
;
257 SYSCTL_UINT(_vm
, OID_AUTO
, native_hw_pagesize
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &arm_hardware_page_size
, 0, "");
258 SYSCTL_UINT(_vm
, OID_AUTO
, arm_pt_desc_size
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &arm_pt_desc_size
, 0, "");
259 SYSCTL_UINT(_vm
, OID_AUTO
, arm_pt_root_size
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &arm_pt_root_size
, 0, "");
260 SYSCTL_UINT(_vm
, OID_AUTO
, free_1page_tte_root
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &free_page_size_tt_count
, 0, "");
261 SYSCTL_UINT(_vm
, OID_AUTO
, free_2page_tte_root
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &free_two_page_size_tt_count
, 0, "");
262 SYSCTL_UINT(_vm
, OID_AUTO
, free_tte_root
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &free_tt_count
, 0, "");
263 SYSCTL_UINT(_vm
, OID_AUTO
, user_tte_root
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &inuse_user_tteroot_count
, 0, "");
264 SYSCTL_UINT(_vm
, OID_AUTO
, kernel_tte_root
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &inuse_kernel_tteroot_count
, 0, "");
265 SYSCTL_UINT(_vm
, OID_AUTO
, user_tte_pages
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &inuse_user_ttepages_count
, 0, "");
266 SYSCTL_UINT(_vm
, OID_AUTO
, kernel_tte_pages
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &inuse_kernel_ttepages_count
, 0, "");
267 SYSCTL_UINT(_vm
, OID_AUTO
, user_pte_pages
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &inuse_user_ptepages_count
, 0, "");
268 SYSCTL_UINT(_vm
, OID_AUTO
, kernel_pte_pages
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &inuse_kernel_ptepages_count
, 0, "");
269 #if DEVELOPMENT || DEBUG
270 extern unsigned long pmap_asid_flushes
;
271 SYSCTL_ULONG(_vm
, OID_AUTO
, pmap_asid_flushes
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &pmap_asid_flushes
, "");
272 extern unsigned long pmap_asid_hits
;
273 SYSCTL_ULONG(_vm
, OID_AUTO
, pmap_asid_hits
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &pmap_asid_hits
, "");
274 extern unsigned long pmap_asid_misses
;
275 SYSCTL_ULONG(_vm
, OID_AUTO
, pmap_asid_misses
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &pmap_asid_misses
, "");
277 #endif /* __arm__ || __arm64__ */
280 extern int fourk_pager_data_request_debug
;
281 SYSCTL_INT(_vm
, OID_AUTO
, fourk_pager_data_request_debug
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &fourk_pager_data_request_debug
, 0, "");
282 #endif /* __arm64__ */
283 #endif /* DEVELOPMENT || DEBUG */
285 SYSCTL_INT(_vm
, OID_AUTO
, vm_do_collapse_compressor
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_counters
.do_collapse_compressor
, 0, "");
286 SYSCTL_INT(_vm
, OID_AUTO
, vm_do_collapse_compressor_pages
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_counters
.do_collapse_compressor_pages
, 0, "");
287 SYSCTL_INT(_vm
, OID_AUTO
, vm_do_collapse_terminate
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_counters
.do_collapse_terminate
, 0, "");
288 SYSCTL_INT(_vm
, OID_AUTO
, vm_do_collapse_terminate_failure
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_counters
.do_collapse_terminate_failure
, 0, "");
289 SYSCTL_INT(_vm
, OID_AUTO
, vm_should_cow_but_wired
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_counters
.should_cow_but_wired
, 0, "");
290 SYSCTL_INT(_vm
, OID_AUTO
, vm_create_upl_extra_cow
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_counters
.create_upl_extra_cow
, 0, "");
291 SYSCTL_INT(_vm
, OID_AUTO
, vm_create_upl_extra_cow_pages
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_counters
.create_upl_extra_cow_pages
, 0, "");
292 SYSCTL_INT(_vm
, OID_AUTO
, vm_create_upl_lookup_failure_write
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_counters
.create_upl_lookup_failure_write
, 0, "");
293 SYSCTL_INT(_vm
, OID_AUTO
, vm_create_upl_lookup_failure_copy
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_counters
.create_upl_lookup_failure_copy
, 0, "");
294 #if VM_SCAN_FOR_SHADOW_CHAIN
295 static int vm_shadow_max_enabled
= 0; /* Disabled by default */
296 extern int proc_shadow_max(void);
298 vm_shadow_max SYSCTL_HANDLER_ARGS
300 #pragma unused(arg1, arg2, oidp)
303 if (vm_shadow_max_enabled
) {
304 value
= proc_shadow_max();
307 return SYSCTL_OUT(req
, &value
, sizeof(value
));
309 SYSCTL_PROC(_vm
, OID_AUTO
, vm_shadow_max
, CTLTYPE_INT
| CTLFLAG_RD
| CTLFLAG_LOCKED
,
310 0, 0, &vm_shadow_max
, "I", "");
312 SYSCTL_INT(_vm
, OID_AUTO
, vm_shadow_max_enabled
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &vm_shadow_max_enabled
, 0, "");
314 #endif /* VM_SCAN_FOR_SHADOW_CHAIN */
316 SYSCTL_INT(_vm
, OID_AUTO
, vm_debug_events
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &vm_debug_events
, 0, "");
318 __attribute__((noinline
)) int __KERNEL_WAITING_ON_TASKGATED_CHECK_ACCESS_UPCALL__(
319 mach_port_t task_access_port
, int32_t calling_pid
, uint32_t calling_gid
, int32_t target_pid
);
321 * Sysctl's related to data/stack execution. See osfmk/vm/vm_map.c
324 #if DEVELOPMENT || DEBUG
325 extern int allow_stack_exec
, allow_data_exec
;
327 SYSCTL_INT(_vm
, OID_AUTO
, allow_stack_exec
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &allow_stack_exec
, 0, "");
328 SYSCTL_INT(_vm
, OID_AUTO
, allow_data_exec
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &allow_data_exec
, 0, "");
330 #endif /* DEVELOPMENT || DEBUG */
332 static const char *prot_values
[] = {
344 log_stack_execution_failure(addr64_t vaddr
, vm_prot_t prot
)
346 printf("Data/Stack execution not permitted: %s[pid %d] at virtual address 0x%qx, protections were %s\n",
347 current_proc()->p_comm
, current_proc()->p_pid
, vaddr
, prot_values
[prot
& VM_PROT_ALL
]);
351 * shared_region_unnest_logging: level of logging of unnesting events
353 * 1 - throttled logging of unexpected unnesting events (default)
354 * 2 - unthrottled logging of unexpected unnesting events
355 * 3+ - unthrottled logging of all unnesting events
357 int shared_region_unnest_logging
= 1;
359 SYSCTL_INT(_vm
, OID_AUTO
, shared_region_unnest_logging
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
360 &shared_region_unnest_logging
, 0, "");
362 int vm_shared_region_unnest_log_interval
= 10;
363 int shared_region_unnest_log_count_threshold
= 5;
366 * Shared cache path enforcement.
369 #if XNU_TARGET_OS_OSX
371 #if defined (__x86_64__)
372 static int scdir_enforce
= 1;
373 #else /* defined (__x86_64__) */
374 static int scdir_enforce
= 0; /* AOT caches live elsewhere */
375 #endif /* defined (__x86_64__) */
377 static char scdir_path
[] = "/System/Library/dyld/";
379 #else /* XNU_TARGET_OS_OSX */
381 static int scdir_enforce
= 0;
382 static char scdir_path
[] = "/System/Library/Caches/com.apple.dyld/";
384 #endif /* XNU_TARGET_OS_OSX */
386 #ifndef SECURE_KERNEL
387 static int sysctl_scdir_enforce SYSCTL_HANDLER_ARGS
390 if (csr_check(CSR_ALLOW_UNRESTRICTED_FS
) != 0) {
391 printf("Failed attempt to set vm.enforce_shared_cache_dir sysctl\n");
394 #endif /* CONFIG_CSR */
395 return sysctl_handle_int(oidp
, arg1
, arg2
, req
);
398 SYSCTL_PROC(_vm
, OID_AUTO
, enforce_shared_cache_dir
, CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, &scdir_enforce
, 0, sysctl_scdir_enforce
, "I", "");
401 /* These log rate throttling state variables aren't thread safe, but
402 * are sufficient unto the task.
404 static int64_t last_unnest_log_time
= 0;
405 static int shared_region_unnest_log_count
= 0;
412 boolean_t is_nested_map
,
413 vm_map_offset_t lowest_unnestable_addr
)
417 if (shared_region_unnest_logging
== 0) {
421 if (shared_region_unnest_logging
<= 2 &&
423 s
>= lowest_unnestable_addr
) {
425 * Unnesting of writable map entries is fine.
430 if (shared_region_unnest_logging
<= 1) {
432 if ((tv
.tv_sec
- last_unnest_log_time
) <
433 vm_shared_region_unnest_log_interval
) {
434 if (shared_region_unnest_log_count
++ >
435 shared_region_unnest_log_count_threshold
) {
439 last_unnest_log_time
= tv
.tv_sec
;
440 shared_region_unnest_log_count
= 0;
444 DTRACE_VM4(log_unnest_badness
,
448 vm_map_offset_t
, lowest_unnestable_addr
);
449 printf("%s[%d] triggered unnest of range 0x%qx->0x%qx of DYLD shared region in VM map %p. While not abnormal for debuggers, this increases system memory footprint until the target exits.\n", current_proc()->p_comm
, current_proc()->p_pid
, (uint64_t)s
, (uint64_t)e
, (void *) VM_KERNEL_ADDRPERM(m
));
461 return vm_map_check_protection(
463 vm_map_trunc_page(addr
,
464 vm_map_page_mask(map
)),
465 vm_map_round_page(addr
+ len
,
466 vm_map_page_mask(map
)),
467 prot
== B_READ
? VM_PROT_READ
: VM_PROT_WRITE
);
479 kret
= vm_map_wire_kernel(map
,
480 vm_map_trunc_page(addr
,
481 vm_map_page_mask(map
)),
482 vm_map_round_page(addr
+ len
,
483 vm_map_page_mask(map
)),
484 VM_PROT_READ
| VM_PROT_WRITE
, VM_KERN_MEMORY_BSD
,
490 case KERN_INVALID_ADDRESS
:
493 case KERN_PROTECTION_FAILURE
:
504 __unused
int dirtied
)
509 vm_map_offset_t vaddr
;
519 pmap
= get_task_pmap(current_task());
520 for (vaddr
= vm_map_trunc_page(addr
, PAGE_MASK
);
521 vaddr
< vm_map_round_page(addr
+ len
, PAGE_MASK
);
522 vaddr
+= PAGE_SIZE
) {
523 paddr
= pmap_find_phys(pmap
, vaddr
);
524 pg
= PHYS_TO_VM_PAGE(paddr
);
525 vm_page_set_modified(pg
);
532 kret
= vm_map_unwire(map
,
533 vm_map_trunc_page(addr
,
534 vm_map_page_mask(map
)),
535 vm_map_round_page(addr
+ len
,
536 vm_map_page_mask(map
)),
541 case KERN_INVALID_ADDRESS
:
544 case KERN_PROTECTION_FAILURE
:
558 character
= (char)byte
;
559 return copyout((void *)&(character
), addr
, sizeof(char)) == 0 ? 0 : -1;
569 character
= (char)byte
;
570 return copyout((void *)&(character
), addr
, sizeof(char)) == 0 ? 0 : -1;
574 fubyte(user_addr_t addr
)
578 if (copyin(addr
, (void *) &byte
, sizeof(char))) {
585 fuibyte(user_addr_t addr
)
589 if (copyin(addr
, (void *) &(byte
), sizeof(char))) {
600 return copyout((void *) &word
, addr
, sizeof(int)) == 0 ? 0 : -1;
604 fuword(user_addr_t addr
)
608 if (copyin(addr
, (void *) &word
, sizeof(int))) {
614 /* suiword and fuiword are the same as suword and fuword, respectively */
621 return copyout((void *) &word
, addr
, sizeof(int)) == 0 ? 0 : -1;
625 fuiword(user_addr_t addr
)
629 if (copyin(addr
, (void *) &word
, sizeof(int))) {
636 * With a 32-bit kernel and mixed 32/64-bit user tasks, this interface allows the
637 * fetching and setting of process-sized size_t and pointer values.
640 sulong(user_addr_t addr
, int64_t word
)
642 if (IS_64BIT_PROCESS(current_proc())) {
643 return copyout((void *)&word
, addr
, sizeof(word
)) == 0 ? 0 : -1;
645 return suiword(addr
, (long)word
);
650 fulong(user_addr_t addr
)
654 if (IS_64BIT_PROCESS(current_proc())) {
655 if (copyin(addr
, (void *)&longword
, sizeof(longword
)) != 0) {
660 return (int64_t)fuiword(addr
);
665 suulong(user_addr_t addr
, uint64_t uword
)
667 if (IS_64BIT_PROCESS(current_proc())) {
668 return copyout((void *)&uword
, addr
, sizeof(uword
)) == 0 ? 0 : -1;
670 return suiword(addr
, (uint32_t)uword
);
675 fuulong(user_addr_t addr
)
679 if (IS_64BIT_PROCESS(current_proc())) {
680 if (copyin(addr
, (void *)&ulongword
, sizeof(ulongword
)) != 0) {
685 return (uint64_t)fuiword(addr
);
690 swapon(__unused proc_t procp
, __unused
struct swapon_args
*uap
, __unused
int *retval
)
698 * Find the BSD process ID for the Mach task associated with the given Mach port
701 * Parameters: args User argument descriptor (see below)
703 * Indirect parameters: args->t Mach port name
704 * args->pid Process ID (returned value; see below)
706 * Returns: KERL_SUCCESS Success
707 * KERN_FAILURE Not success
709 * Implicit returns: args->pid Process ID
714 struct pid_for_task_args
*args
)
716 mach_port_name_t t
= args
->t
;
717 user_addr_t pid_addr
= args
->pid
;
721 kern_return_t err
= KERN_SUCCESS
;
723 AUDIT_MACH_SYSCALL_ENTER(AUE_PIDFORTASK
);
724 AUDIT_ARG(mach_port1
, t
);
726 t1
= port_name_to_task_name(t
);
728 if (t1
== TASK_NULL
) {
732 p
= get_bsdtask_info(t1
);
736 } else if (is_corpsetask(t1
)) {
746 (void) copyout((char *) &pid
, pid_addr
, sizeof(int));
747 AUDIT_MACH_SYSCALL_EXIT(err
);
753 * tfp_policy = KERN_TFP_POLICY_DENY; Deny Mode: None allowed except for self
754 * tfp_policy = KERN_TFP_POLICY_DEFAULT; default mode: all posix checks and upcall via task port for authentication
757 static int tfp_policy
= KERN_TFP_POLICY_DEFAULT
;
760 * Routine: task_for_pid_posix_check
762 * Verify that the current process should be allowed to
763 * get the target process's task port. This is only
765 * - The current process is root
766 * OR all of the following are true:
767 * - The target process's real, effective, and saved uids
768 * are the same as the current proc's euid,
769 * - The target process's group set is a subset of the
770 * calling process's group set, and
771 * - The target process hasn't switched credentials.
773 * Returns: TRUE: permitted
777 task_for_pid_posix_check(proc_t target
)
779 kauth_cred_t targetcred
, mycred
;
783 /* No task_for_pid on bad targets */
784 if (target
->p_stat
== SZOMB
) {
788 mycred
= kauth_cred_get();
789 myuid
= kauth_cred_getuid(mycred
);
791 /* If we're running as root, the check passes */
792 if (kauth_cred_issuser(mycred
)) {
796 /* We're allowed to get our own task port */
797 if (target
== current_proc()) {
802 * Under DENY, only root can get another proc's task port,
803 * so no more checks are needed.
805 if (tfp_policy
== KERN_TFP_POLICY_DENY
) {
809 targetcred
= kauth_cred_proc_ref(target
);
812 /* Do target's ruid, euid, and saved uid match my euid? */
813 if ((kauth_cred_getuid(targetcred
) != myuid
) ||
814 (kauth_cred_getruid(targetcred
) != myuid
) ||
815 (kauth_cred_getsvuid(targetcred
) != myuid
)) {
820 /* Are target's groups a subset of my groups? */
821 if (kauth_cred_gid_subset(targetcred
, mycred
, &allowed
) ||
827 /* Has target switched credentials? */
828 if (target
->p_flag
& P_SUGID
) {
834 kauth_cred_unref(&targetcred
);
839 * __KERNEL_WAITING_ON_TASKGATED_CHECK_ACCESS_UPCALL__
841 * Description: Waits for the user space daemon to respond to the request
842 * we made. Function declared non inline to be visible in
843 * stackshots and spindumps as well as debugging.
845 __attribute__((noinline
)) int
846 __KERNEL_WAITING_ON_TASKGATED_CHECK_ACCESS_UPCALL__(
847 mach_port_t task_access_port
, int32_t calling_pid
, uint32_t calling_gid
, int32_t target_pid
)
849 return check_task_access(task_access_port
, calling_pid
, calling_gid
, target_pid
);
853 * Routine: task_for_pid
855 * Get the task port for another "process", named by its
856 * process ID on the same host as "target_task".
858 * Only permitted to privileged processes, or processes
859 * with the same user ID.
861 * Note: if pid == 0, an error is return no matter who is calling.
863 * XXX This should be a BSD system call, not a Mach trap!!!
867 struct task_for_pid_args
*args
)
869 mach_port_name_t target_tport
= args
->target_tport
;
871 user_addr_t task_addr
= args
->t
;
872 proc_t p
= PROC_NULL
;
873 task_t t1
= TASK_NULL
;
874 task_t task
= TASK_NULL
;
875 mach_port_name_t tret
= MACH_PORT_NULL
;
876 ipc_port_t tfpport
= MACH_PORT_NULL
;
877 void * sright
= NULL
;
879 boolean_t is_current_proc
= FALSE
;
880 struct proc_ident pident
= {0};
882 AUDIT_MACH_SYSCALL_ENTER(AUE_TASKFORPID
);
884 AUDIT_ARG(mach_port1
, target_tport
);
886 /* Always check if pid == 0 */
888 (void) copyout((char *)&t1
, task_addr
, sizeof(mach_port_name_t
));
889 AUDIT_MACH_SYSCALL_EXIT(KERN_FAILURE
);
893 t1
= port_name_to_task(target_tport
);
894 if (t1
== TASK_NULL
) {
895 (void) copyout((char *)&t1
, task_addr
, sizeof(mach_port_name_t
));
896 AUDIT_MACH_SYSCALL_EXIT(KERN_FAILURE
);
902 if (p
== PROC_NULL
) {
903 error
= KERN_FAILURE
;
906 pident
= proc_ident(p
);
907 is_current_proc
= (p
== current_proc());
910 AUDIT_ARG(process
, p
);
913 if (!(task_for_pid_posix_check(p
))) {
914 error
= KERN_FAILURE
;
918 if (p
->task
== TASK_NULL
) {
919 error
= KERN_SUCCESS
;
924 * Grab a task reference and drop the proc reference as the proc ref
925 * shouldn't be held accross upcalls.
928 task_reference(task
);
934 error
= mac_proc_check_get_task(kauth_cred_get(), &pident
);
936 error
= KERN_FAILURE
;
941 /* If we aren't root and target's task access port is set... */
942 if (!kauth_cred_issuser(kauth_cred_get()) &&
944 (task_get_task_access_port(task
, &tfpport
) == 0) &&
945 (tfpport
!= IPC_PORT_NULL
)) {
946 if (tfpport
== IPC_PORT_DEAD
) {
947 error
= KERN_PROTECTION_FAILURE
;
951 /* Call up to the task access server */
952 error
= __KERNEL_WAITING_ON_TASKGATED_CHECK_ACCESS_UPCALL__(tfpport
, proc_selfpid(), kauth_getgid(), pid
);
954 if (error
!= MACH_MSG_SUCCESS
) {
955 if (error
== MACH_RCV_INTERRUPTED
) {
956 error
= KERN_ABORTED
;
958 error
= KERN_FAILURE
;
964 /* Grant task port access */
965 extmod_statistics_incr_task_for_pid(task
);
966 sright
= (void *) convert_task_to_port(task
);
968 /* Check if the task has been corpsified */
969 if (is_corpsetask(task
)) {
970 /* task ref consumed by convert_task_to_port */
972 ipc_port_release_send(sright
);
973 error
= KERN_FAILURE
;
977 /* task ref consumed by convert_task_to_port */
979 tret
= ipc_port_copyout_send(
981 get_task_ipcspace(current_task()));
983 error
= KERN_SUCCESS
;
987 AUDIT_ARG(mach_port2
, tret
);
988 (void) copyout((char *) &tret
, task_addr
, sizeof(mach_port_name_t
));
990 if (tfpport
!= IPC_PORT_NULL
) {
991 ipc_port_release_send(tfpport
);
993 if (task
!= TASK_NULL
) {
994 task_deallocate(task
);
996 if (p
!= PROC_NULL
) {
999 AUDIT_MACH_SYSCALL_EXIT(error
);
1004 * Routine: task_name_for_pid
1006 * Get the task name port for another "process", named by its
1007 * process ID on the same host as "target_task".
1009 * Only permitted to privileged processes, or processes
1010 * with the same user ID.
1012 * XXX This should be a BSD system call, not a Mach trap!!!
1017 struct task_name_for_pid_args
*args
)
1019 mach_port_name_t target_tport
= args
->target_tport
;
1020 int pid
= args
->pid
;
1021 user_addr_t task_addr
= args
->t
;
1022 proc_t p
= PROC_NULL
;
1024 mach_port_name_t tret
;
1026 int error
= 0, refheld
= 0;
1027 kauth_cred_t target_cred
;
1029 AUDIT_MACH_SYSCALL_ENTER(AUE_TASKNAMEFORPID
);
1030 AUDIT_ARG(pid
, pid
);
1031 AUDIT_ARG(mach_port1
, target_tport
);
1033 t1
= port_name_to_task(target_tport
);
1034 if (t1
== TASK_NULL
) {
1035 (void) copyout((char *)&t1
, task_addr
, sizeof(mach_port_name_t
));
1036 AUDIT_MACH_SYSCALL_EXIT(KERN_FAILURE
);
1037 return KERN_FAILURE
;
1041 if (p
!= PROC_NULL
) {
1042 AUDIT_ARG(process
, p
);
1043 target_cred
= kauth_cred_proc_ref(p
);
1046 if ((p
->p_stat
!= SZOMB
)
1047 && ((current_proc() == p
)
1048 || kauth_cred_issuser(kauth_cred_get())
1049 || ((kauth_cred_getuid(target_cred
) == kauth_cred_getuid(kauth_cred_get())) &&
1050 ((kauth_cred_getruid(target_cred
) == kauth_getruid()))))) {
1051 if (p
->task
!= TASK_NULL
) {
1052 struct proc_ident pident
= proc_ident(p
);
1054 task_t task
= p
->task
;
1056 task_reference(p
->task
);
1060 error
= mac_proc_check_get_task_name(kauth_cred_get(), &pident
);
1062 task_deallocate(task
);
1066 sright
= (void *)convert_task_name_to_port(task
);
1068 tret
= ipc_port_copyout_send(sright
,
1069 get_task_ipcspace(current_task()));
1071 tret
= MACH_PORT_NULL
;
1074 AUDIT_ARG(mach_port2
, tret
);
1075 (void) copyout((char *)&tret
, task_addr
, sizeof(mach_port_name_t
));
1076 task_deallocate(t1
);
1077 error
= KERN_SUCCESS
;
1085 task_deallocate(t1
);
1086 tret
= MACH_PORT_NULL
;
1087 (void) copyout((char *) &tret
, task_addr
, sizeof(mach_port_name_t
));
1088 error
= KERN_FAILURE
;
1091 kauth_cred_unref(&target_cred
);
1093 if (p
!= PROC_NULL
) {
1096 AUDIT_MACH_SYSCALL_EXIT(error
);
1101 * Routine: task_inspect_for_pid
1103 * Get the task inspect port for another "process", named by its
1104 * process ID on the same host as "target_task".
1107 task_inspect_for_pid(struct proc
*p __unused
, struct task_inspect_for_pid_args
*args
, int *ret
)
1109 mach_port_name_t target_tport
= args
->target_tport
;
1110 int pid
= args
->pid
;
1111 user_addr_t task_addr
= args
->t
;
1113 proc_t proc
= PROC_NULL
;
1114 task_t t1
= TASK_NULL
;
1115 task_inspect_t task_insp
= TASK_INSPECT_NULL
;
1116 mach_port_name_t tret
= MACH_PORT_NULL
;
1117 ipc_port_t tfpport
= MACH_PORT_NULL
;
1119 void *sright
= NULL
;
1120 boolean_t is_current_proc
= FALSE
;
1121 struct proc_ident pident
= {0};
1123 /* Disallow inspect port for kernel_task */
1125 (void) copyout((char *)&t1
, task_addr
, sizeof(mach_port_name_t
));
1129 t1
= port_name_to_task(target_tport
);
1130 if (t1
== TASK_NULL
) {
1131 (void) copyout((char *) &t1
, task_addr
, sizeof(mach_port_name_t
));
1135 proc
= proc_find(pid
);
1136 if (proc
== PROC_NULL
) {
1140 pident
= proc_ident(proc
);
1141 is_current_proc
= (proc
== current_proc());
1143 if (!(task_for_pid_posix_check(proc
))) {
1148 task_insp
= proc
->task
;
1149 if (task_insp
== TASK_INSPECT_NULL
) {
1154 * Grab a task reference and drop the proc reference before making any upcalls.
1156 task_reference(task_insp
);
1162 * For now, it performs the same set of permission checks as task_for_pid. This
1163 * will be addressed in rdar://problem/53478660
1166 error
= mac_proc_check_get_task(kauth_cred_get(), &pident
);
1173 /* If we aren't root and target's task access port is set... */
1174 if (!kauth_cred_issuser(kauth_cred_get()) &&
1176 (task_get_task_access_port(task_insp
, &tfpport
) == 0) &&
1177 (tfpport
!= IPC_PORT_NULL
)) {
1178 if (tfpport
== IPC_PORT_DEAD
) {
1184 /* Call up to the task access server */
1185 error
= __KERNEL_WAITING_ON_TASKGATED_CHECK_ACCESS_UPCALL__(tfpport
, proc_selfpid(), kauth_getgid(), pid
);
1187 if (error
!= MACH_MSG_SUCCESS
) {
1188 if (error
== MACH_RCV_INTERRUPTED
) {
1197 /* Check if the task has been corpsified */
1198 if (is_corpsetask(task_insp
)) {
1203 /* could be IP_NULL, consumes a ref */
1204 sright
= (void*) convert_task_inspect_to_port(task_insp
);
1205 task_insp
= TASK_INSPECT_NULL
;
1206 tret
= ipc_port_copyout_send(sright
, get_task_ipcspace(current_task()));
1209 task_deallocate(t1
);
1210 (void) copyout((char *) &tret
, task_addr
, sizeof(mach_port_name_t
));
1211 if (proc
!= PROC_NULL
) {
1214 if (tfpport
!= IPC_PORT_NULL
) {
1215 ipc_port_release_send(tfpport
);
1217 if (task_insp
!= TASK_INSPECT_NULL
) {
1218 task_deallocate(task_insp
);
1226 * Routine: task_read_for_pid
1228 * Get the task read port for another "process", named by its
1229 * process ID on the same host as "target_task".
1232 task_read_for_pid(struct proc
*p __unused
, struct task_read_for_pid_args
*args
, int *ret
)
1234 mach_port_name_t target_tport
= args
->target_tport
;
1235 int pid
= args
->pid
;
1236 user_addr_t task_addr
= args
->t
;
1238 proc_t proc
= PROC_NULL
;
1239 task_t t1
= TASK_NULL
;
1240 task_read_t task_read
= TASK_READ_NULL
;
1241 mach_port_name_t tret
= MACH_PORT_NULL
;
1242 ipc_port_t tfpport
= MACH_PORT_NULL
;
1244 void *sright
= NULL
;
1245 boolean_t is_current_proc
= FALSE
;
1246 struct proc_ident pident
= {0};
1248 /* Disallow read port for kernel_task */
1250 (void) copyout((char *)&t1
, task_addr
, sizeof(mach_port_name_t
));
1254 t1
= port_name_to_task(target_tport
);
1255 if (t1
== TASK_NULL
) {
1256 (void) copyout((char *) &t1
, task_addr
, sizeof(mach_port_name_t
));
1260 proc
= proc_find(pid
);
1261 if (proc
== PROC_NULL
) {
1265 pident
= proc_ident(proc
);
1266 is_current_proc
= (proc
== current_proc());
1268 if (!(task_for_pid_posix_check(proc
))) {
1273 task_read
= proc
->task
;
1274 if (task_read
== TASK_INSPECT_NULL
) {
1279 * Grab a task reference and drop the proc reference before making any upcalls.
1281 task_reference(task_read
);
1287 * For now, it performs the same set of permission checks as task_for_pid. This
1288 * will be addressed in rdar://problem/53478660
1291 error
= mac_proc_check_get_task(kauth_cred_get(), &pident
);
1298 /* If we aren't root and target's task access port is set... */
1299 if (!kauth_cred_issuser(kauth_cred_get()) &&
1301 (task_get_task_access_port(task_read
, &tfpport
) == 0) &&
1302 (tfpport
!= IPC_PORT_NULL
)) {
1303 if (tfpport
== IPC_PORT_DEAD
) {
1309 /* Call up to the task access server */
1310 error
= __KERNEL_WAITING_ON_TASKGATED_CHECK_ACCESS_UPCALL__(tfpport
, proc_selfpid(), kauth_getgid(), pid
);
1312 if (error
!= MACH_MSG_SUCCESS
) {
1313 if (error
== MACH_RCV_INTERRUPTED
) {
1322 /* Check if the task has been corpsified */
1323 if (is_corpsetask(task_read
)) {
1328 /* could be IP_NULL, consumes a ref */
1329 sright
= (void*) convert_task_read_to_port(task_read
);
1330 task_read
= TASK_READ_NULL
;
1331 tret
= ipc_port_copyout_send(sright
, get_task_ipcspace(current_task()));
1334 task_deallocate(t1
);
1335 (void) copyout((char *) &tret
, task_addr
, sizeof(mach_port_name_t
));
1336 if (proc
!= PROC_NULL
) {
1339 if (tfpport
!= IPC_PORT_NULL
) {
1340 ipc_port_release_send(tfpport
);
1342 if (task_read
!= TASK_READ_NULL
) {
1343 task_deallocate(task_read
);
1351 pid_suspend(struct proc
*p __unused
, struct pid_suspend_args
*args
, int *ret
)
1353 task_t target
= NULL
;
1354 proc_t targetproc
= PROC_NULL
;
1355 int pid
= args
->pid
;
1357 mach_port_t tfpport
= MACH_PORT_NULL
;
1364 targetproc
= proc_find(pid
);
1365 if (targetproc
== PROC_NULL
) {
1370 if (!task_for_pid_posix_check(targetproc
) &&
1371 !IOTaskHasEntitlement(current_task(), PROCESS_RESUME_SUSPEND_ENTITLEMENT
)) {
1377 error
= mac_proc_check_suspend_resume(targetproc
, MAC_PROC_CHECK_SUSPEND
);
1384 target
= targetproc
->task
;
1385 #ifndef CONFIG_EMBEDDED
1386 if (target
!= TASK_NULL
) {
1387 /* If we aren't root and target's task access port is set... */
1388 if (!kauth_cred_issuser(kauth_cred_get()) &&
1389 targetproc
!= current_proc() &&
1390 (task_get_task_access_port(target
, &tfpport
) == 0) &&
1391 (tfpport
!= IPC_PORT_NULL
)) {
1392 if (tfpport
== IPC_PORT_DEAD
) {
1397 /* Call up to the task access server */
1398 error
= __KERNEL_WAITING_ON_TASKGATED_CHECK_ACCESS_UPCALL__(tfpport
, proc_selfpid(), kauth_getgid(), pid
);
1400 if (error
!= MACH_MSG_SUCCESS
) {
1401 if (error
== MACH_RCV_INTERRUPTED
) {
1412 task_reference(target
);
1413 error
= task_pidsuspend(target
);
1415 if (error
== KERN_INVALID_ARGUMENT
) {
1421 #if CONFIG_MEMORYSTATUS
1423 memorystatus_on_suspend(targetproc
);
1427 task_deallocate(target
);
1430 if (tfpport
!= IPC_PORT_NULL
) {
1431 ipc_port_release_send(tfpport
);
1434 if (targetproc
!= PROC_NULL
) {
1435 proc_rele(targetproc
);
1442 debug_control_port_for_pid(struct debug_control_port_for_pid_args
*args
)
1444 mach_port_name_t target_tport
= args
->target_tport
;
1445 int pid
= args
->pid
;
1446 user_addr_t task_addr
= args
->t
;
1447 proc_t p
= PROC_NULL
;
1448 task_t t1
= TASK_NULL
;
1449 task_t task
= TASK_NULL
;
1450 mach_port_name_t tret
= MACH_PORT_NULL
;
1451 ipc_port_t tfpport
= MACH_PORT_NULL
;
1452 ipc_port_t sright
= NULL
;
1454 boolean_t is_current_proc
= FALSE
;
1455 struct proc_ident pident
= {0};
1457 AUDIT_MACH_SYSCALL_ENTER(AUE_DBGPORTFORPID
);
1458 AUDIT_ARG(pid
, pid
);
1459 AUDIT_ARG(mach_port1
, target_tport
);
1461 /* Always check if pid == 0 */
1463 (void) copyout((char *)&t1
, task_addr
, sizeof(mach_port_name_t
));
1464 AUDIT_MACH_SYSCALL_EXIT(KERN_FAILURE
);
1465 return KERN_FAILURE
;
1468 t1
= port_name_to_task(target_tport
);
1469 if (t1
== TASK_NULL
) {
1470 (void) copyout((char *)&t1
, task_addr
, sizeof(mach_port_name_t
));
1471 AUDIT_MACH_SYSCALL_EXIT(KERN_FAILURE
);
1472 return KERN_FAILURE
;
1476 if (p
== PROC_NULL
) {
1477 error
= KERN_FAILURE
;
1480 pident
= proc_ident(p
);
1481 is_current_proc
= (p
== current_proc());
1484 AUDIT_ARG(process
, p
);
1487 if (!(task_for_pid_posix_check(p
))) {
1488 error
= KERN_FAILURE
;
1492 if (p
->task
== TASK_NULL
) {
1493 error
= KERN_SUCCESS
;
1498 * Grab a task reference and drop the proc reference before making any upcalls.
1501 task_reference(task
);
1506 if (!IOTaskHasEntitlement(current_task(), DEBUG_PORT_ENTITLEMENT
)) {
1508 error
= mac_proc_check_get_task(kauth_cred_get(), &pident
);
1510 error
= KERN_FAILURE
;
1515 /* If we aren't root and target's task access port is set... */
1516 if (!kauth_cred_issuser(kauth_cred_get()) &&
1518 (task_get_task_access_port(task
, &tfpport
) == 0) &&
1519 (tfpport
!= IPC_PORT_NULL
)) {
1520 if (tfpport
== IPC_PORT_DEAD
) {
1521 error
= KERN_PROTECTION_FAILURE
;
1526 /* Call up to the task access server */
1527 error
= __KERNEL_WAITING_ON_TASKGATED_CHECK_ACCESS_UPCALL__(tfpport
, proc_selfpid(), kauth_getgid(), pid
);
1529 if (error
!= MACH_MSG_SUCCESS
) {
1530 if (error
== MACH_RCV_INTERRUPTED
) {
1531 error
= KERN_ABORTED
;
1533 error
= KERN_FAILURE
;
1540 /* Check if the task has been corpsified */
1541 if (is_corpsetask(task
)) {
1542 error
= KERN_FAILURE
;
1546 error
= task_get_debug_control_port(task
, &sright
);
1547 if (error
!= KERN_SUCCESS
) {
1551 tret
= ipc_port_copyout_send(
1553 get_task_ipcspace(current_task()));
1555 error
= KERN_SUCCESS
;
1558 task_deallocate(t1
);
1559 AUDIT_ARG(mach_port2
, tret
);
1560 (void) copyout((char *) &tret
, task_addr
, sizeof(mach_port_name_t
));
1562 if (tfpport
!= IPC_PORT_NULL
) {
1563 ipc_port_release_send(tfpport
);
1565 if (task
!= TASK_NULL
) {
1566 task_deallocate(task
);
1568 if (p
!= PROC_NULL
) {
1571 AUDIT_MACH_SYSCALL_EXIT(error
);
1576 pid_resume(struct proc
*p __unused
, struct pid_resume_args
*args
, int *ret
)
1578 task_t target
= NULL
;
1579 proc_t targetproc
= PROC_NULL
;
1580 int pid
= args
->pid
;
1582 mach_port_t tfpport
= MACH_PORT_NULL
;
1589 targetproc
= proc_find(pid
);
1590 if (targetproc
== PROC_NULL
) {
1595 if (!task_for_pid_posix_check(targetproc
) &&
1596 !IOTaskHasEntitlement(current_task(), PROCESS_RESUME_SUSPEND_ENTITLEMENT
)) {
1602 error
= mac_proc_check_suspend_resume(targetproc
, MAC_PROC_CHECK_RESUME
);
1609 target
= targetproc
->task
;
1610 #ifndef CONFIG_EMBEDDED
1611 if (target
!= TASK_NULL
) {
1612 /* If we aren't root and target's task access port is set... */
1613 if (!kauth_cred_issuser(kauth_cred_get()) &&
1614 targetproc
!= current_proc() &&
1615 (task_get_task_access_port(target
, &tfpport
) == 0) &&
1616 (tfpport
!= IPC_PORT_NULL
)) {
1617 if (tfpport
== IPC_PORT_DEAD
) {
1622 /* Call up to the task access server */
1623 error
= __KERNEL_WAITING_ON_TASKGATED_CHECK_ACCESS_UPCALL__(tfpport
, proc_selfpid(), kauth_getgid(), pid
);
1625 if (error
!= MACH_MSG_SUCCESS
) {
1626 if (error
== MACH_RCV_INTERRUPTED
) {
1637 #if !XNU_TARGET_OS_OSX
1639 resume_proc_sockets(targetproc
);
1640 #endif /* SOCKETS */
1641 #endif /* !XNU_TARGET_OS_OSX */
1643 task_reference(target
);
1645 #if CONFIG_MEMORYSTATUS
1646 memorystatus_on_resume(targetproc
);
1649 error
= task_pidresume(target
);
1651 if (error
== KERN_INVALID_ARGUMENT
) {
1654 if (error
== KERN_MEMORY_ERROR
) {
1655 psignal(targetproc
, SIGKILL
);
1663 task_deallocate(target
);
1666 if (tfpport
!= IPC_PORT_NULL
) {
1667 ipc_port_release_send(tfpport
);
1670 if (targetproc
!= PROC_NULL
) {
1671 proc_rele(targetproc
);
1680 * Freeze the specified process (provided in args->pid), or find and freeze a PID.
1681 * When a process is specified, this call is blocking, otherwise we wake up the
1682 * freezer thread and do not block on a process being frozen.
1685 pid_hibernate(struct proc
*p __unused
, struct pid_hibernate_args
*args
, int *ret
)
1688 proc_t targetproc
= PROC_NULL
;
1689 int pid
= args
->pid
;
1691 #ifndef CONFIG_FREEZE
1696 * If a pid has been provided, we obtain the process handle and call task_for_pid_posix_check().
1700 targetproc
= proc_find(pid
);
1702 if (targetproc
== PROC_NULL
) {
1707 if (!task_for_pid_posix_check(targetproc
)) {
1714 //Note that targetproc may be null
1715 error
= mac_proc_check_suspend_resume(targetproc
, MAC_PROC_CHECK_HIBERNATE
);
1723 vm_pageout_anonymous_pages();
1724 } else if (pid
== -1) {
1725 memorystatus_on_inactivity(targetproc
);
1727 error
= memorystatus_freeze_process_sync(targetproc
);
1732 #endif /* CONFIG_FREEZE */
1734 if (targetproc
!= PROC_NULL
) {
1735 proc_rele(targetproc
);
1740 #endif /* CONFIG_EMBEDDED */
1744 networking_memstatus_callout(proc_t p
, uint32_t status
)
1746 struct fileproc
*fp
;
1749 * proc list lock NOT held
1750 * proc lock NOT held
1751 * a reference on the proc has been held / shall be dropped by the caller.
1753 LCK_MTX_ASSERT(proc_list_mlock
, LCK_MTX_ASSERT_NOTOWNED
);
1754 LCK_MTX_ASSERT(&p
->p_mlock
, LCK_MTX_ASSERT_NOTOWNED
);
1758 fdt_foreach(fp
, p
) {
1759 switch (FILEGLOB_DTYPE(fp
->fp_glob
)) {
1761 case DTYPE_NETPOLICY
:
1762 necp_fd_memstatus(p
, status
,
1763 (struct necp_fd_data
*)fp
->fp_glob
->fg_data
);
1777 networking_defunct_callout(proc_t p
, void *arg
)
1779 struct pid_shutdown_sockets_args
*args
= arg
;
1780 int pid
= args
->pid
;
1781 int level
= args
->level
;
1782 struct fileproc
*fp
;
1786 fdt_foreach(fp
, p
) {
1787 struct fileglob
*fg
= fp
->fp_glob
;
1789 switch (FILEGLOB_DTYPE(fg
)) {
1790 case DTYPE_SOCKET
: {
1791 struct socket
*so
= (struct socket
*)fg
->fg_data
;
1792 if (p
->p_pid
== pid
|| so
->last_pid
== pid
||
1793 ((so
->so_flags
& SOF_DELEGATED
) && so
->e_pid
== pid
)) {
1794 /* Call networking stack with socket and level */
1795 (void)socket_defunct(p
, so
, level
);
1800 case DTYPE_NETPOLICY
:
1801 /* first pass: defunct necp and get stats for ntstat */
1802 if (p
->p_pid
== pid
) {
1804 (struct necp_fd_data
*)fg
->fg_data
);
1815 return PROC_RETURNED
;
1819 pid_shutdown_sockets(struct proc
*p __unused
, struct pid_shutdown_sockets_args
*args
, int *ret
)
1822 proc_t targetproc
= PROC_NULL
;
1823 int pid
= args
->pid
;
1824 int level
= args
->level
;
1826 if (level
!= SHUTDOWN_SOCKET_LEVEL_DISCONNECT_SVC
&&
1827 level
!= SHUTDOWN_SOCKET_LEVEL_DISCONNECT_ALL
) {
1832 targetproc
= proc_find(pid
);
1833 if (targetproc
== PROC_NULL
) {
1838 if (!task_for_pid_posix_check(targetproc
) &&
1839 !IOTaskHasEntitlement(current_task(), PROCESS_RESUME_SUSPEND_ENTITLEMENT
)) {
1845 error
= mac_proc_check_suspend_resume(targetproc
, MAC_PROC_CHECK_SHUTDOWN_SOCKETS
);
1852 proc_iterate(PROC_ALLPROCLIST
| PROC_NOWAITTRANS
,
1853 networking_defunct_callout
, args
, NULL
, NULL
);
1856 if (targetproc
!= PROC_NULL
) {
1857 proc_rele(targetproc
);
1863 #endif /* SOCKETS */
1866 sysctl_settfp_policy(__unused
struct sysctl_oid
*oidp
, void *arg1
,
1867 __unused
int arg2
, struct sysctl_req
*req
)
1872 error
= SYSCTL_OUT(req
, arg1
, sizeof(int));
1873 if (error
|| req
->newptr
== USER_ADDR_NULL
) {
1877 if (!kauth_cred_issuser(kauth_cred_get())) {
1881 if ((error
= SYSCTL_IN(req
, &new_value
, sizeof(int)))) {
1884 if ((new_value
== KERN_TFP_POLICY_DENY
)
1885 || (new_value
== KERN_TFP_POLICY_DEFAULT
)) {
1886 tfp_policy
= new_value
;
1894 #if defined(SECURE_KERNEL)
1895 static int kern_secure_kernel
= 1;
1897 static int kern_secure_kernel
= 0;
1900 SYSCTL_INT(_kern
, OID_AUTO
, secure_kernel
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &kern_secure_kernel
, 0, "");
1902 SYSCTL_NODE(_kern
, KERN_TFP
, tfp
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "tfp");
1903 SYSCTL_PROC(_kern_tfp
, KERN_TFP_POLICY
, policy
, CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
1904 &tfp_policy
, sizeof(uint32_t), &sysctl_settfp_policy
, "I", "policy");
1906 SYSCTL_INT(_vm
, OID_AUTO
, shared_region_trace_level
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
1907 &shared_region_trace_level
, 0, "");
1908 SYSCTL_INT(_vm
, OID_AUTO
, shared_region_version
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
1909 &shared_region_version
, 0, "");
1910 SYSCTL_INT(_vm
, OID_AUTO
, shared_region_persistence
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
1911 &shared_region_persistence
, 0, "");
1914 * shared_region_check_np:
1916 * This system call is intended for dyld.
1918 * dyld calls this when any process starts to see if the process's shared
1919 * region is already set up and ready to use.
1920 * This call returns the base address of the first mapping in the
1921 * process's shared region's first mapping.
1922 * dyld will then check what's mapped at that address.
1924 * If the shared region is empty, dyld will then attempt to map the shared
1925 * cache file in the shared region via the shared_region_map_np() system call.
1927 * If something's already mapped in the shared region, dyld will check if it
1928 * matches the shared cache it would like to use for that process.
1929 * If it matches, evrything's ready and the process can proceed and use the
1931 * If it doesn't match, dyld will unmap the shared region and map the shared
1932 * cache into the process's address space via mmap().
1935 * EINVAL no shared region
1936 * ENOMEM shared region is empty
1937 * EFAULT bad address for "start_address"
1940 shared_region_check_np(
1941 __unused
struct proc
*p
,
1942 struct shared_region_check_np_args
*uap
,
1943 __unused
int *retvalp
)
1945 vm_shared_region_t shared_region
;
1946 mach_vm_offset_t start_address
= 0;
1950 SHARED_REGION_TRACE_DEBUG(
1951 ("shared_region: %p [%d(%s)] -> check_np(0x%llx)\n",
1952 (void *)VM_KERNEL_ADDRPERM(current_thread()),
1953 p
->p_pid
, p
->p_comm
,
1954 (uint64_t)uap
->start_address
));
1956 /* retrieve the current tasks's shared region */
1957 shared_region
= vm_shared_region_get(current_task());
1958 if (shared_region
!= NULL
) {
1959 /* retrieve address of its first mapping... */
1960 kr
= vm_shared_region_start_address(shared_region
, &start_address
);
1961 if (kr
!= KERN_SUCCESS
) {
1964 #if __has_feature(ptrauth_calls)
1966 * Remap any section of the shared library that
1967 * has authenticated pointers into private memory.
1969 if (vm_shared_region_auth_remap(shared_region
) != KERN_SUCCESS
) {
1972 #endif /* __has_feature(ptrauth_calls) */
1974 /* ... and give it to the caller */
1976 error
= copyout(&start_address
,
1977 (user_addr_t
) uap
->start_address
,
1978 sizeof(start_address
));
1981 SHARED_REGION_TRACE_ERROR(
1982 ("shared_region: %p [%d(%s)] "
1984 "copyout(0x%llx) error %d\n",
1985 (void *)VM_KERNEL_ADDRPERM(current_thread()),
1986 p
->p_pid
, p
->p_comm
,
1987 (uint64_t)uap
->start_address
, (uint64_t)start_address
,
1991 vm_shared_region_deallocate(shared_region
);
1993 /* no shared region ! */
1997 SHARED_REGION_TRACE_DEBUG(
1998 ("shared_region: %p [%d(%s)] check_np(0x%llx) <- 0x%llx %d\n",
1999 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2000 p
->p_pid
, p
->p_comm
,
2001 (uint64_t)uap
->start_address
, (uint64_t)start_address
, error
));
2008 shared_region_copyin(
2010 user_addr_t user_addr
,
2012 unsigned int element_size
,
2016 vm_size_t size
= count
* element_size
;
2018 error
= copyin(user_addr
, kernel_data
, size
);
2020 SHARED_REGION_TRACE_ERROR(
2021 ("shared_region: %p [%d(%s)] map(): "
2022 "copyin(0x%llx, %ld) failed (error=%d)\n",
2023 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2024 p
->p_pid
, p
->p_comm
,
2025 (uint64_t)user_addr
, (long)size
, error
));
2030 #define _SR_FILE_MAPPINGS_MAX_FILES 2
2032 /* forward declaration */
2033 __attribute__((noinline
))
2034 static void shared_region_map_and_slide_cleanup(
2036 uint32_t files_count
,
2037 struct _sr_file_mappings
*sr_file_mappings
,
2038 struct vm_shared_region
*shared_region
,
2039 struct vnode
*scdir_vp
);
2042 * Setup part of _shared_region_map_and_slide().
2043 * It had to be broken out of _shared_region_map_and_slide() to
2044 * prevent compiler inlining from blowing out the stack.
2046 __attribute__((noinline
))
2048 shared_region_map_and_slide_setup(
2050 uint32_t files_count
,
2051 struct shared_file_np
*files
,
2052 uint32_t mappings_count
,
2053 struct shared_file_mapping_slide_np
*mappings
,
2054 struct _sr_file_mappings
**sr_file_mappings
,
2055 struct vm_shared_region
**shared_region_ptr
,
2056 struct vnode
**scdir_vp
,
2057 struct vnode
*rdir_vp
)
2060 struct _sr_file_mappings
*srfmp
;
2061 uint32_t mappings_next
;
2062 struct vnode_attr va
;
2065 vm_prot_t maxprot
= VM_PROT_ALL
;
2068 struct vm_shared_region
*shared_region
;
2070 SHARED_REGION_TRACE_DEBUG(
2071 ("shared_region: %p [%d(%s)] -> map\n",
2072 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2073 p
->p_pid
, p
->p_comm
));
2075 if (files_count
> _SR_FILE_MAPPINGS_MAX_FILES
) {
2079 if (files_count
== 0) {
2083 *sr_file_mappings
= kheap_alloc(KHEAP_TEMP
, files_count
* sizeof(struct _sr_file_mappings
), Z_WAITOK
);
2084 if (*sr_file_mappings
== NULL
) {
2088 bzero(*sr_file_mappings
, files_count
* sizeof(struct _sr_file_mappings
));
2090 for (i
= 0; i
< files_count
; i
++) {
2091 srfmp
= &(*sr_file_mappings
)[i
];
2092 srfmp
->fd
= files
[i
].sf_fd
;
2093 srfmp
->mappings_count
= files
[i
].sf_mappings_count
;
2094 srfmp
->mappings
= &mappings
[mappings_next
];
2095 mappings_next
+= srfmp
->mappings_count
;
2096 if (mappings_next
> mappings_count
) {
2100 srfmp
->slide
= files
[i
].sf_slide
;
2103 if (scdir_enforce
) {
2104 /* get vnode for scdir_path */
2105 error
= vnode_lookup(scdir_path
, 0, scdir_vp
, vfs_context_current());
2107 SHARED_REGION_TRACE_ERROR(
2108 ("shared_region: %p [%d(%s)]: "
2109 "vnode_lookup(%s) failed (error=%d)\n",
2110 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2111 p
->p_pid
, p
->p_comm
,
2112 scdir_path
, error
));
2117 /* get the process's shared region (setup in vm_map_exec()) */
2118 shared_region
= vm_shared_region_trim_and_get(current_task());
2119 *shared_region_ptr
= shared_region
;
2120 if (shared_region
== NULL
) {
2121 SHARED_REGION_TRACE_ERROR(
2122 ("shared_region: %p [%d(%s)] map(): "
2123 "no shared region\n",
2124 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2125 p
->p_pid
, p
->p_comm
));
2131 * Check the shared region matches the current root
2132 * directory of this process. Deny the mapping to
2133 * avoid tainting the shared region with something that
2134 * doesn't quite belong into it.
2136 struct vnode
*sr_vnode
= vm_shared_region_root_dir(shared_region
);
2137 if (sr_vnode
!= NULL
? rdir_vp
!= sr_vnode
: rdir_vp
!= rootvnode
) {
2138 SHARED_REGION_TRACE_ERROR(
2139 ("shared_region: map(%p) root_dir mismatch\n",
2140 (void *)VM_KERNEL_ADDRPERM(current_thread())));
2146 for (srfmp
= &(*sr_file_mappings
)[0];
2147 srfmp
< &(*sr_file_mappings
)[files_count
];
2149 if (srfmp
->mappings_count
== 0) {
2150 /* no mappings here... */
2154 /* get file structure from file descriptor */
2155 error
= fp_get_ftype(p
, srfmp
->fd
, DTYPE_VNODE
, EINVAL
, &srfmp
->fp
);
2157 SHARED_REGION_TRACE_ERROR(
2158 ("shared_region: %p [%d(%s)] map: "
2159 "fd=%d lookup failed (error=%d)\n",
2160 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2161 p
->p_pid
, p
->p_comm
, srfmp
->fd
, error
));
2165 /* we need at least read permission on the file */
2166 if (!(srfmp
->fp
->fp_glob
->fg_flag
& FREAD
)) {
2167 SHARED_REGION_TRACE_ERROR(
2168 ("shared_region: %p [%d(%s)] map: "
2169 "fd=%d not readable\n",
2170 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2171 p
->p_pid
, p
->p_comm
, srfmp
->fd
));
2176 /* get vnode from file structure */
2177 error
= vnode_getwithref((vnode_t
) srfmp
->fp
->fp_glob
->fg_data
);
2179 SHARED_REGION_TRACE_ERROR(
2180 ("shared_region: %p [%d(%s)] map: "
2181 "fd=%d getwithref failed (error=%d)\n",
2182 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2183 p
->p_pid
, p
->p_comm
, srfmp
->fd
, error
));
2186 srfmp
->vp
= (struct vnode
*) srfmp
->fp
->fp_glob
->fg_data
;
2188 /* make sure the vnode is a regular file */
2189 if (srfmp
->vp
->v_type
!= VREG
) {
2190 SHARED_REGION_TRACE_ERROR(
2191 ("shared_region: %p [%d(%s)] map(%p:'%s'): "
2192 "not a file (type=%d)\n",
2193 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2194 p
->p_pid
, p
->p_comm
,
2195 (void *)VM_KERNEL_ADDRPERM(srfmp
->vp
),
2196 srfmp
->vp
->v_name
, srfmp
->vp
->v_type
));
2202 /* pass in 0 for the offset argument because AMFI does not need the offset
2203 * of the shared cache */
2204 error
= mac_file_check_mmap(vfs_context_ucred(vfs_context_current()),
2205 srfmp
->fp
->fp_glob
, VM_PROT_ALL
, MAP_FILE
, 0, &maxprot
);
2211 #if XNU_TARGET_OS_OSX && defined(__arm64__)
2213 * Check if the shared cache is in the trust cache;
2214 * if so, we can skip the root ownership check.
2216 #if DEVELOPMENT || DEBUG
2218 * Skip both root ownership and trust cache check if
2219 * enforcement is disabled.
2221 if (!cs_system_enforcement()) {
2222 goto after_root_check
;
2224 #endif /* DEVELOPMENT || DEBUG */
2225 struct cs_blob
*blob
= csvnode_get_blob(srfmp
->vp
, 0);
2227 SHARED_REGION_TRACE_ERROR(
2228 ("shared_region: %p [%d(%s)] map(%p:'%s'): "
2229 "missing CS blob\n",
2230 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2231 p
->p_pid
, p
->p_comm
,
2232 (void *)VM_KERNEL_ADDRPERM(srfmp
->vp
),
2233 srfmp
->vp
->v_name
));
2236 const uint8_t *cdhash
= csblob_get_cdhash(blob
);
2237 if (cdhash
== NULL
) {
2238 SHARED_REGION_TRACE_ERROR(
2239 ("shared_region: %p [%d(%s)] map(%p:'%s'): "
2241 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2242 p
->p_pid
, p
->p_comm
,
2243 (void *)VM_KERNEL_ADDRPERM(srfmp
->vp
),
2244 srfmp
->vp
->v_name
));
2247 uint32_t result
= pmap_lookup_in_static_trust_cache(cdhash
);
2248 boolean_t in_trust_cache
= result
& (TC_LOOKUP_FOUND
<< TC_LOOKUP_RESULT_SHIFT
);
2249 if (!in_trust_cache
) {
2250 SHARED_REGION_TRACE_ERROR(
2251 ("shared_region: %p [%d(%s)] map(%p:'%s'): "
2252 "not in trust cache\n",
2253 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2254 p
->p_pid
, p
->p_comm
,
2255 (void *)VM_KERNEL_ADDRPERM(srfmp
->vp
),
2256 srfmp
->vp
->v_name
));
2259 goto after_root_check
;
2261 #endif /* XNU_TARGET_OS_OSX && defined(__arm64__) */
2263 /* The shared cache file must be owned by root */
2265 VATTR_WANTED(&va
, va_uid
);
2266 error
= vnode_getattr(srfmp
->vp
, &va
, vfs_context_current());
2268 SHARED_REGION_TRACE_ERROR(
2269 ("shared_region: %p [%d(%s)] map(%p:'%s'): "
2270 "vnode_getattr(%p) failed (error=%d)\n",
2271 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2272 p
->p_pid
, p
->p_comm
,
2273 (void *)VM_KERNEL_ADDRPERM(srfmp
->vp
),
2275 (void *)VM_KERNEL_ADDRPERM(srfmp
->vp
),
2279 if (va
.va_uid
!= 0) {
2280 SHARED_REGION_TRACE_ERROR(
2281 ("shared_region: %p [%d(%s)] map(%p:'%s'): "
2282 "owned by uid=%d instead of 0\n",
2283 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2284 p
->p_pid
, p
->p_comm
,
2285 (void *)VM_KERNEL_ADDRPERM(srfmp
->vp
),
2286 srfmp
->vp
->v_name
, va
.va_uid
));
2291 #if XNU_TARGET_OS_OSX && defined(__arm64__)
2293 #endif /* XNU_TARGET_OS_OSX && defined(__arm64__) */
2296 if (csr_check(CSR_ALLOW_UNRESTRICTED_FS
) != 0) {
2298 VATTR_WANTED(&va
, va_flags
);
2299 error
= vnode_getattr(srfmp
->vp
, &va
, vfs_context_current());
2301 SHARED_REGION_TRACE_ERROR(
2302 ("shared_region: %p [%d(%s)] map(%p:'%s'): "
2303 "vnode_getattr(%p) failed (error=%d)\n",
2304 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2305 p
->p_pid
, p
->p_comm
,
2306 (void *)VM_KERNEL_ADDRPERM(srfmp
->vp
),
2308 (void *)VM_KERNEL_ADDRPERM(srfmp
->vp
),
2313 if (!(va
.va_flags
& SF_RESTRICTED
)) {
2315 * CSR is not configured in CSR_ALLOW_UNRESTRICTED_FS mode, and
2316 * the shared cache file is NOT SIP-protected, so reject the
2319 SHARED_REGION_TRACE_ERROR(
2320 ("shared_region: %p [%d(%s)] map(%p:'%s'), "
2321 "vnode is not SIP-protected. \n",
2322 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2323 p
->p_pid
, p
->p_comm
,
2324 (void *)VM_KERNEL_ADDRPERM(srfmp
->vp
),
2325 srfmp
->vp
->v_name
));
2330 #else /* CONFIG_CSR */
2331 /* Devices without SIP/ROSP need to make sure that the shared cache is on the root volume. */
2333 assert(rdir_vp
!= NULL
);
2334 if (srfmp
->vp
->v_mount
!= rdir_vp
->v_mount
) {
2335 SHARED_REGION_TRACE_ERROR(
2336 ("shared_region: %p [%d(%s)] map(%p:'%s'): "
2337 "not on process's root volume\n",
2338 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2339 p
->p_pid
, p
->p_comm
,
2340 (void *)VM_KERNEL_ADDRPERM(srfmp
->vp
),
2341 srfmp
->vp
->v_name
));
2345 #endif /* CONFIG_CSR */
2347 if (scdir_enforce
) {
2348 /* ensure parent is scdir_vp */
2349 assert(*scdir_vp
!= NULL
);
2350 if (vnode_parent(srfmp
->vp
) != *scdir_vp
) {
2351 SHARED_REGION_TRACE_ERROR(
2352 ("shared_region: %p [%d(%s)] map(%p:'%s'): "
2353 "shared cache file not in %s\n",
2354 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2355 p
->p_pid
, p
->p_comm
,
2356 (void *)VM_KERNEL_ADDRPERM(srfmp
->vp
),
2357 srfmp
->vp
->v_name
, scdir_path
));
2363 /* get vnode size */
2364 error
= vnode_size(srfmp
->vp
, &fs
, vfs_context_current());
2366 SHARED_REGION_TRACE_ERROR(
2367 ("shared_region: %p [%d(%s)] map(%p:'%s'): "
2368 "vnode_size(%p) failed (error=%d)\n",
2369 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2370 p
->p_pid
, p
->p_comm
,
2371 (void *)VM_KERNEL_ADDRPERM(srfmp
->vp
),
2373 (void *)VM_KERNEL_ADDRPERM(srfmp
->vp
), error
));
2376 srfmp
->file_size
= fs
;
2378 /* get the file's memory object handle */
2379 srfmp
->file_control
= ubc_getobject(srfmp
->vp
, UBC_HOLDOBJECT
);
2380 if (srfmp
->file_control
== MEMORY_OBJECT_CONTROL_NULL
) {
2381 SHARED_REGION_TRACE_ERROR(
2382 ("shared_region: %p [%d(%s)] map(%p:'%s'): "
2383 "no memory object\n",
2384 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2385 p
->p_pid
, p
->p_comm
,
2386 (void *)VM_KERNEL_ADDRPERM(srfmp
->vp
),
2387 srfmp
->vp
->v_name
));
2392 /* check that the mappings are properly covered by code signatures */
2393 if (!cs_system_enforcement()) {
2394 /* code signing is not enforced: no need to check */
2396 for (i
= 0; i
< srfmp
->mappings_count
; i
++) {
2397 if (srfmp
->mappings
[i
].sms_init_prot
& VM_PROT_ZF
) {
2398 /* zero-filled mapping: not backed by the file */
2401 if (ubc_cs_is_range_codesigned(srfmp
->vp
,
2402 srfmp
->mappings
[i
].sms_file_offset
,
2403 srfmp
->mappings
[i
].sms_size
)) {
2404 /* this mapping is fully covered by code signatures */
2407 SHARED_REGION_TRACE_ERROR(
2408 ("shared_region: %p [%d(%s)] map(%p:'%s'): "
2409 "mapping #%d/%d [0x%llx:0x%llx:0x%llx:0x%x:0x%x] "
2410 "is not code-signed\n",
2411 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2412 p
->p_pid
, p
->p_comm
,
2413 (void *)VM_KERNEL_ADDRPERM(srfmp
->vp
),
2415 i
, srfmp
->mappings_count
,
2416 srfmp
->mappings
[i
].sms_address
,
2417 srfmp
->mappings
[i
].sms_size
,
2418 srfmp
->mappings
[i
].sms_file_offset
,
2419 srfmp
->mappings
[i
].sms_max_prot
,
2420 srfmp
->mappings
[i
].sms_init_prot
));
2428 shared_region_map_and_slide_cleanup(p
, files_count
, *sr_file_mappings
, shared_region
, *scdir_vp
);
2429 *sr_file_mappings
= NULL
;
2430 *shared_region_ptr
= NULL
;
2437 * shared_region_map_np()
2439 * This system call is intended for dyld.
2441 * dyld uses this to map a shared cache file into a shared region.
2442 * This is usually done only the first time a shared cache is needed.
2443 * Subsequent processes will just use the populated shared region without
2444 * requiring any further setup.
2447 _shared_region_map_and_slide(
2449 uint32_t files_count
,
2450 struct shared_file_np
*files
,
2451 uint32_t mappings_count
,
2452 struct shared_file_mapping_slide_np
*mappings
)
2455 kern_return_t kr
= KERN_SUCCESS
;
2456 struct _sr_file_mappings
*sr_file_mappings
= NULL
;
2457 struct vnode
*scdir_vp
= NULL
;
2458 struct vnode
*rdir_vp
= NULL
;
2459 struct vm_shared_region
*shared_region
= NULL
;
2462 * Get a reference to the current proc's root dir.
2463 * Need this to prevent racing with chroot.
2466 rdir_vp
= p
->p_fd
->fd_rdir
;
2467 if (rdir_vp
== NULL
) {
2468 rdir_vp
= rootvnode
;
2470 assert(rdir_vp
!= NULL
);
2475 * Turn files, mappings into sr_file_mappings and other setup.
2477 error
= shared_region_map_and_slide_setup(p
, files_count
,
2478 files
, mappings_count
, mappings
,
2479 &sr_file_mappings
, &shared_region
, &scdir_vp
, rdir_vp
);
2485 /* map the file(s) into that shared region's submap */
2486 kr
= vm_shared_region_map_file(shared_region
, files_count
, sr_file_mappings
);
2487 if (kr
!= KERN_SUCCESS
) {
2488 SHARED_REGION_TRACE_ERROR(("shared_region: %p [%d(%s)] map(): "
2489 "vm_shared_region_map_file() failed kr=0x%x\n",
2490 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2491 p
->p_pid
, p
->p_comm
, kr
));
2494 /* convert kern_return_t to errno */
2499 case KERN_INVALID_ADDRESS
:
2502 case KERN_PROTECTION_FAILURE
:
2509 case KERN_INVALID_ARGUMENT
:
2516 * Mark that this process is now using split libraries.
2518 if (error
== 0 && (p
->p_flag
& P_NOSHLIB
)) {
2519 OSBitAndAtomic(~((uint32_t)P_NOSHLIB
), &p
->p_flag
);
2523 shared_region_map_and_slide_cleanup(p
, files_count
, sr_file_mappings
, shared_region
, scdir_vp
);
2525 SHARED_REGION_TRACE_DEBUG(
2526 ("shared_region: %p [%d(%s)] <- map\n",
2527 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2528 p
->p_pid
, p
->p_comm
));
2534 * Clean up part of _shared_region_map_and_slide()
2535 * It had to be broken out of _shared_region_map_and_slide() to
2536 * prevent compiler inlining from blowing out the stack.
2538 __attribute__((noinline
))
2540 shared_region_map_and_slide_cleanup(
2542 uint32_t files_count
,
2543 struct _sr_file_mappings
*sr_file_mappings
,
2544 struct vm_shared_region
*shared_region
,
2545 struct vnode
*scdir_vp
)
2547 struct _sr_file_mappings
*srfmp
;
2548 struct vnode_attr va
;
2550 if (sr_file_mappings
!= NULL
) {
2551 for (srfmp
= &sr_file_mappings
[0]; srfmp
< &sr_file_mappings
[files_count
]; srfmp
++) {
2552 if (srfmp
->vp
!= NULL
) {
2553 vnode_lock_spin(srfmp
->vp
);
2554 srfmp
->vp
->v_flag
|= VSHARED_DYLD
;
2555 vnode_unlock(srfmp
->vp
);
2557 /* update the vnode's access time */
2558 if (!(vnode_vfsvisflags(srfmp
->vp
) & MNT_NOATIME
)) {
2560 nanotime(&va
.va_access_time
);
2561 VATTR_SET_ACTIVE(&va
, va_access_time
);
2562 vnode_setattr(srfmp
->vp
, &va
, vfs_context_current());
2567 * If the shared cache is compressed, it may
2568 * have a namedstream vnode instantiated for
2569 * for it. That namedstream vnode will also
2570 * have to be marked with VSHARED_DYLD.
2572 if (vnode_hasnamedstreams(srfmp
->vp
)) {
2574 if (vnode_getnamedstream(srfmp
->vp
, &svp
, XATTR_RESOURCEFORK_NAME
,
2575 NS_OPEN
, 0, vfs_context_kernel()) == 0) {
2576 vnode_lock_spin(svp
);
2577 svp
->v_flag
|= VSHARED_DYLD
;
2582 #endif /* NAMEDSTREAMS */
2584 * release the vnode...
2585 * ubc_map() still holds it for us in the non-error case
2587 (void) vnode_put(srfmp
->vp
);
2590 if (srfmp
->fp
!= NULL
) {
2591 /* release the file descriptor */
2592 fp_drop(p
, srfmp
->fd
, srfmp
->fp
, 0);
2596 kheap_free(KHEAP_TEMP
, sr_file_mappings
, files_count
* sizeof(*sr_file_mappings
));
2599 if (scdir_vp
!= NULL
) {
2600 (void)vnode_put(scdir_vp
);
2604 if (shared_region
!= NULL
) {
2605 vm_shared_region_deallocate(shared_region
);
2610 #define SFM_MAX 1024 /* max mapping structs allowed to pass in */
2613 * This interface is used by dyld to map shared caches which are
2614 * for any architecture which doesn't have run time support of pointer
2615 * authentication. Note dyld could also use the new ...map_and_slide_2_np()
2616 * call for this case, however, it just doesn't do that yet.
2619 shared_region_map_and_slide_np(
2621 struct shared_region_map_and_slide_np_args
*uap
,
2622 __unused
int *retvalp
)
2624 unsigned int mappings_count
= uap
->count
;
2626 uint32_t slide
= uap
->slide
;
2627 struct shared_file_np shared_files
[1];
2628 struct shared_file_mapping_np legacy_mapping
;
2629 struct shared_file_mapping_slide_np
*mappings
= NULL
;
2630 kern_return_t kr
= KERN_SUCCESS
;
2632 if ((kr
= vm_shared_region_sliding_valid(slide
)) != KERN_SUCCESS
) {
2633 if (kr
== KERN_INVALID_ARGUMENT
) {
2635 * This will happen if we request sliding again
2636 * with the same slide value that was used earlier
2637 * for the very first sliding.
2644 if (mappings_count
== 0) {
2645 SHARED_REGION_TRACE_INFO(
2646 ("shared_region: %p [%d(%s)] map(): "
2648 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2649 p
->p_pid
, p
->p_comm
));
2650 kr
= 0; /* no mappings: we're done ! */
2652 } else if (mappings_count
<= SFM_MAX
) {
2653 mappings
= kheap_alloc(KHEAP_TEMP
,
2654 mappings_count
* sizeof(mappings
[0]), Z_WAITOK
);
2655 if (mappings
== NULL
) {
2656 kr
= KERN_RESOURCE_SHORTAGE
;
2660 SHARED_REGION_TRACE_ERROR(
2661 ("shared_region: %p [%d(%s)] map(): "
2662 "too many mappings (%d) max %d\n",
2663 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2664 p
->p_pid
, p
->p_comm
,
2665 mappings_count
, SFM_MAX
));
2671 * Read in the mappings and translate to new format.
2673 for (m
= 0; m
< mappings_count
; ++m
) {
2674 user_addr_t from_uaddr
= uap
->mappings
+ (m
* sizeof(struct shared_file_mapping_np
));
2675 kr
= shared_region_copyin(p
, from_uaddr
, 1, sizeof(legacy_mapping
), &legacy_mapping
);
2679 mappings
[m
].sms_address
= legacy_mapping
.sfm_address
;
2680 mappings
[m
].sms_size
= legacy_mapping
.sfm_size
;
2681 mappings
[m
].sms_file_offset
= legacy_mapping
.sfm_file_offset
;
2682 mappings
[m
].sms_max_prot
= legacy_mapping
.sfm_max_prot
;
2683 mappings
[m
].sms_init_prot
= legacy_mapping
.sfm_init_prot
;
2684 mappings
[m
].sms_slide_size
= uap
->slide_size
;
2685 mappings
[m
].sms_slide_start
= uap
->slide_start
;
2688 bzero(shared_files
, sizeof(shared_files
));
2689 shared_files
[0].sf_fd
= uap
->fd
;
2690 shared_files
[0].sf_mappings_count
= mappings_count
;
2691 shared_files
[0].sf_slide
= slide
;
2693 kr
= _shared_region_map_and_slide(p
,
2694 1, /* # of files to map */
2695 &shared_files
[0], /* files to map */
2700 if (mappings
!= NULL
) {
2701 kheap_free(KHEAP_TEMP
, mappings
, mappings_count
* sizeof(mappings
[0]));
2708 * This interface for setting up shared region mappings is what dyld
2709 * uses for shared caches that have __AUTH sections. All other shared
2710 * caches use the non _2 version.
2712 * The slide used for shared regions setup using this interface is done differently
2713 * from the old interface. The slide value passed in the shared_files_np represents
2714 * a max value. The kernel will choose a random value based on that, then use it
2715 * for all shared regions.
2717 #define SLIDE_AMOUNT_MASK ~PAGE_MASK
2720 shared_region_map_and_slide_2_np(
2722 struct shared_region_map_and_slide_2_np_args
*uap
,
2723 __unused
int *retvalp
)
2725 unsigned int files_count
;
2726 struct shared_file_np
*shared_files
= NULL
;
2727 unsigned int mappings_count
;
2728 struct shared_file_mapping_slide_np
*mappings
= NULL
;
2729 kern_return_t kr
= KERN_SUCCESS
;
2730 boolean_t should_slide_mappings
= TRUE
;
2732 files_count
= uap
->files_count
;
2733 mappings_count
= uap
->mappings_count
;
2736 if (files_count
== 0) {
2737 SHARED_REGION_TRACE_INFO(
2738 ("shared_region: %p [%d(%s)] map(): "
2740 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2741 p
->p_pid
, p
->p_comm
));
2742 kr
= 0; /* no files to map: we're done ! */
2744 } else if (files_count
<= _SR_FILE_MAPPINGS_MAX_FILES
) {
2745 shared_files
= kheap_alloc(KHEAP_TEMP
,
2746 files_count
* sizeof(shared_files
[0]), Z_WAITOK
);
2747 if (shared_files
== NULL
) {
2748 kr
= KERN_RESOURCE_SHORTAGE
;
2752 SHARED_REGION_TRACE_ERROR(
2753 ("shared_region: %p [%d(%s)] map(): "
2754 "too many files (%d) max %d\n",
2755 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2756 p
->p_pid
, p
->p_comm
,
2757 files_count
, _SR_FILE_MAPPINGS_MAX_FILES
));
2762 if (mappings_count
== 0) {
2763 SHARED_REGION_TRACE_INFO(
2764 ("shared_region: %p [%d(%s)] map(): "
2766 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2767 p
->p_pid
, p
->p_comm
));
2768 kr
= 0; /* no mappings: we're done ! */
2770 } else if (mappings_count
<= SFM_MAX
) {
2771 mappings
= kheap_alloc(KHEAP_TEMP
,
2772 mappings_count
* sizeof(mappings
[0]), Z_WAITOK
);
2773 if (mappings
== NULL
) {
2774 kr
= KERN_RESOURCE_SHORTAGE
;
2778 SHARED_REGION_TRACE_ERROR(
2779 ("shared_region: %p [%d(%s)] map(): "
2780 "too many mappings (%d) max %d\n",
2781 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2782 p
->p_pid
, p
->p_comm
,
2783 mappings_count
, SFM_MAX
));
2788 kr
= shared_region_copyin(p
, uap
->files
, files_count
, sizeof(shared_files
[0]), shared_files
);
2789 if (kr
!= KERN_SUCCESS
) {
2793 kr
= shared_region_copyin(p
, uap
->mappings
, mappings_count
, sizeof(mappings
[0]), mappings
);
2794 if (kr
!= KERN_SUCCESS
) {
2798 if (should_slide_mappings
) {
2799 uint32_t max_slide
= shared_files
[0].sf_slide
;
2800 uint32_t random_val
;
2801 uint32_t slide_amount
;
2803 if (max_slide
!= 0) {
2804 read_random(&random_val
, sizeof random_val
);
2805 slide_amount
= ((random_val
% max_slide
) & SLIDE_AMOUNT_MASK
);
2811 * Fix up the mappings to reflect the desired slide.
2816 for (f
= 0; f
< files_count
; ++f
) {
2817 shared_files
[f
].sf_slide
= slide_amount
;
2818 for (i
= 0; i
< shared_files
[f
].sf_mappings_count
; ++i
, ++m
) {
2819 if (m
>= mappings_count
) {
2820 SHARED_REGION_TRACE_ERROR(
2821 ("shared_region: %p [%d(%s)] map(): "
2822 "mapping count argument was too small\n",
2823 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2824 p
->p_pid
, p
->p_comm
));
2828 mappings
[m
].sms_address
+= slide_amount
;
2829 if (mappings
[m
].sms_slide_size
!= 0) {
2830 mappings
[i
].sms_slide_start
+= slide_amount
;
2835 kr
= _shared_region_map_and_slide(p
, files_count
, shared_files
, mappings_count
, mappings
);
2837 if (shared_files
!= NULL
) {
2838 kheap_free(KHEAP_TEMP
, shared_files
, files_count
* sizeof(shared_files
[0]));
2839 shared_files
= NULL
;
2841 if (mappings
!= NULL
) {
2842 kheap_free(KHEAP_TEMP
, mappings
, mappings_count
* sizeof(mappings
[0]));
2848 /* sysctl overflow room */
2850 SYSCTL_INT(_vm
, OID_AUTO
, pagesize
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2851 (int *) &page_size
, 0, "vm page size");
2853 /* vm_page_free_target is provided as a makeshift solution for applications that want to
2854 * allocate buffer space, possibly purgeable memory, but not cause inactive pages to be
2855 * reclaimed. It allows the app to calculate how much memory is free outside the free target. */
2856 extern unsigned int vm_page_free_target
;
2857 SYSCTL_INT(_vm
, OID_AUTO
, vm_page_free_target
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2858 &vm_page_free_target
, 0, "Pageout daemon free target");
2860 SYSCTL_INT(_vm
, OID_AUTO
, memory_pressure
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2861 &vm_pageout_state
.vm_memory_pressure
, 0, "Memory pressure indicator");
2864 vm_ctl_page_free_wanted SYSCTL_HANDLER_ARGS
2866 #pragma unused(oidp, arg1, arg2)
2867 unsigned int page_free_wanted
;
2869 page_free_wanted
= mach_vm_ctl_page_free_wanted();
2870 return SYSCTL_OUT(req
, &page_free_wanted
, sizeof(page_free_wanted
));
2872 SYSCTL_PROC(_vm
, OID_AUTO
, page_free_wanted
,
2873 CTLTYPE_INT
| CTLFLAG_RD
| CTLFLAG_LOCKED
,
2874 0, 0, vm_ctl_page_free_wanted
, "I", "");
2876 extern unsigned int vm_page_purgeable_count
;
2877 SYSCTL_INT(_vm
, OID_AUTO
, page_purgeable_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2878 &vm_page_purgeable_count
, 0, "Purgeable page count");
2880 extern unsigned int vm_page_purgeable_wired_count
;
2881 SYSCTL_INT(_vm
, OID_AUTO
, page_purgeable_wired_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2882 &vm_page_purgeable_wired_count
, 0, "Wired purgeable page count");
2884 extern unsigned int vm_page_kern_lpage_count
;
2885 SYSCTL_INT(_vm
, OID_AUTO
, kern_lpage_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2886 &vm_page_kern_lpage_count
, 0, "kernel used large pages");
2888 #if DEVELOPMENT || DEBUG
2889 #if __ARM_MIXED_PAGE_SIZE__
2890 static int vm_mixed_pagesize_supported
= 1;
2892 static int vm_mixed_pagesize_supported
= 0;
2893 #endif /*__ARM_MIXED_PAGE_SIZE__ */
2894 SYSCTL_INT(_debug
, OID_AUTO
, vm_mixed_pagesize_supported
, CTLFLAG_ANYBODY
| CTLFLAG_RD
| CTLFLAG_LOCKED
,
2895 &vm_mixed_pagesize_supported
, 0, "kernel support for mixed pagesize");
2898 extern uint64_t get_pages_grabbed_count(void);
2901 pages_grabbed SYSCTL_HANDLER_ARGS
2903 #pragma unused(arg1, arg2, oidp)
2904 uint64_t value
= get_pages_grabbed_count();
2905 return SYSCTL_OUT(req
, &value
, sizeof(value
));
2908 SYSCTL_PROC(_vm
, OID_AUTO
, pages_grabbed
, CTLTYPE_QUAD
| CTLFLAG_RD
| CTLFLAG_LOCKED
,
2909 0, 0, &pages_grabbed
, "QU", "Total pages grabbed");
2910 SYSCTL_ULONG(_vm
, OID_AUTO
, pages_freed
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2911 &vm_pageout_vminfo
.vm_page_pages_freed
, "Total pages freed");
2913 SYSCTL_INT(_vm
, OID_AUTO
, pageout_purged_objects
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2914 &vm_pageout_debug
.vm_pageout_purged_objects
, 0, "System purged object count");
2915 SYSCTL_UINT(_vm
, OID_AUTO
, pageout_cleaned_busy
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2916 &vm_pageout_debug
.vm_pageout_cleaned_busy
, 0, "Cleaned pages busy (deactivated)");
2917 SYSCTL_UINT(_vm
, OID_AUTO
, pageout_cleaned_nolock
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2918 &vm_pageout_debug
.vm_pageout_cleaned_nolock
, 0, "Cleaned pages no-lock (deactivated)");
2920 SYSCTL_UINT(_vm
, OID_AUTO
, pageout_cleaned_volatile_reactivated
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2921 &vm_pageout_debug
.vm_pageout_cleaned_volatile_reactivated
, 0, "Cleaned pages volatile reactivated");
2922 SYSCTL_UINT(_vm
, OID_AUTO
, pageout_cleaned_fault_reactivated
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2923 &vm_pageout_debug
.vm_pageout_cleaned_fault_reactivated
, 0, "Cleaned pages fault reactivated");
2924 SYSCTL_UINT(_vm
, OID_AUTO
, pageout_cleaned_reactivated
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2925 &vm_pageout_debug
.vm_pageout_cleaned_reactivated
, 0, "Cleaned pages reactivated"); /* sum of all reactivated AND busy and nolock (even though those actually get reDEactivated */
2926 SYSCTL_ULONG(_vm
, OID_AUTO
, pageout_cleaned
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2927 &vm_pageout_vminfo
.vm_pageout_freed_cleaned
, "Cleaned pages freed");
2928 SYSCTL_UINT(_vm
, OID_AUTO
, pageout_cleaned_reference_reactivated
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2929 &vm_pageout_debug
.vm_pageout_cleaned_reference_reactivated
, 0, "Cleaned pages reference reactivated");
2930 SYSCTL_UINT(_vm
, OID_AUTO
, pageout_enqueued_cleaned
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2931 &vm_pageout_debug
.vm_pageout_enqueued_cleaned
, 0, ""); /* sum of next two */
2932 #endif /* DEVELOPMENT || DEBUG */
2934 extern int madvise_free_debug
;
2935 SYSCTL_INT(_vm
, OID_AUTO
, madvise_free_debug
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
2936 &madvise_free_debug
, 0, "zero-fill on madvise(MADV_FREE*)");
2938 SYSCTL_INT(_vm
, OID_AUTO
, page_reusable_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2939 &vm_page_stats_reusable
.reusable_count
, 0, "Reusable page count");
2940 SYSCTL_QUAD(_vm
, OID_AUTO
, reusable_success
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2941 &vm_page_stats_reusable
.reusable_pages_success
, "");
2942 SYSCTL_QUAD(_vm
, OID_AUTO
, reusable_failure
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2943 &vm_page_stats_reusable
.reusable_pages_failure
, "");
2944 SYSCTL_QUAD(_vm
, OID_AUTO
, reusable_pages_shared
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2945 &vm_page_stats_reusable
.reusable_pages_shared
, "");
2946 SYSCTL_QUAD(_vm
, OID_AUTO
, all_reusable_calls
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2947 &vm_page_stats_reusable
.all_reusable_calls
, "");
2948 SYSCTL_QUAD(_vm
, OID_AUTO
, partial_reusable_calls
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2949 &vm_page_stats_reusable
.partial_reusable_calls
, "");
2950 SYSCTL_QUAD(_vm
, OID_AUTO
, reuse_success
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2951 &vm_page_stats_reusable
.reuse_pages_success
, "");
2952 SYSCTL_QUAD(_vm
, OID_AUTO
, reuse_failure
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2953 &vm_page_stats_reusable
.reuse_pages_failure
, "");
2954 SYSCTL_QUAD(_vm
, OID_AUTO
, all_reuse_calls
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2955 &vm_page_stats_reusable
.all_reuse_calls
, "");
2956 SYSCTL_QUAD(_vm
, OID_AUTO
, partial_reuse_calls
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2957 &vm_page_stats_reusable
.partial_reuse_calls
, "");
2958 SYSCTL_QUAD(_vm
, OID_AUTO
, can_reuse_success
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2959 &vm_page_stats_reusable
.can_reuse_success
, "");
2960 SYSCTL_QUAD(_vm
, OID_AUTO
, can_reuse_failure
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2961 &vm_page_stats_reusable
.can_reuse_failure
, "");
2962 SYSCTL_QUAD(_vm
, OID_AUTO
, reusable_reclaimed
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2963 &vm_page_stats_reusable
.reusable_reclaimed
, "");
2964 SYSCTL_QUAD(_vm
, OID_AUTO
, reusable_nonwritable
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2965 &vm_page_stats_reusable
.reusable_nonwritable
, "");
2966 SYSCTL_QUAD(_vm
, OID_AUTO
, reusable_shared
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2967 &vm_page_stats_reusable
.reusable_shared
, "");
2968 SYSCTL_QUAD(_vm
, OID_AUTO
, free_shared
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2969 &vm_page_stats_reusable
.free_shared
, "");
2972 extern unsigned int vm_page_free_count
, vm_page_speculative_count
;
2973 SYSCTL_UINT(_vm
, OID_AUTO
, page_free_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_free_count
, 0, "");
2974 SYSCTL_UINT(_vm
, OID_AUTO
, page_speculative_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_speculative_count
, 0, "");
2976 extern unsigned int vm_page_cleaned_count
;
2977 SYSCTL_UINT(_vm
, OID_AUTO
, page_cleaned_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_cleaned_count
, 0, "Cleaned queue size");
2979 extern unsigned int vm_page_pageable_internal_count
, vm_page_pageable_external_count
;
2980 SYSCTL_UINT(_vm
, OID_AUTO
, page_pageable_internal_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_pageable_internal_count
, 0, "");
2981 SYSCTL_UINT(_vm
, OID_AUTO
, page_pageable_external_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_pageable_external_count
, 0, "");
2983 /* pageout counts */
2984 SYSCTL_UINT(_vm
, OID_AUTO
, pageout_inactive_clean
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_pageout_state
.vm_pageout_inactive_clean
, 0, "");
2985 SYSCTL_UINT(_vm
, OID_AUTO
, pageout_inactive_used
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_pageout_state
.vm_pageout_inactive_used
, 0, "");
2987 SYSCTL_ULONG(_vm
, OID_AUTO
, pageout_inactive_dirty_internal
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_pageout_vminfo
.vm_pageout_inactive_dirty_internal
, "");
2988 SYSCTL_ULONG(_vm
, OID_AUTO
, pageout_inactive_dirty_external
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_pageout_vminfo
.vm_pageout_inactive_dirty_external
, "");
2989 SYSCTL_ULONG(_vm
, OID_AUTO
, pageout_speculative_clean
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_pageout_vminfo
.vm_pageout_freed_speculative
, "");
2990 SYSCTL_ULONG(_vm
, OID_AUTO
, pageout_freed_external
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_pageout_vminfo
.vm_pageout_freed_external
, "");
2991 SYSCTL_ULONG(_vm
, OID_AUTO
, pageout_freed_speculative
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_pageout_vminfo
.vm_pageout_freed_speculative
, "");
2992 SYSCTL_ULONG(_vm
, OID_AUTO
, pageout_freed_cleaned
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_pageout_vminfo
.vm_pageout_freed_cleaned
, "");
2995 /* counts of pages prefaulted when entering a memory object */
2996 extern int64_t vm_prefault_nb_pages
, vm_prefault_nb_bailout
;
2997 SYSCTL_QUAD(_vm
, OID_AUTO
, prefault_nb_pages
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &vm_prefault_nb_pages
, "");
2998 SYSCTL_QUAD(_vm
, OID_AUTO
, prefault_nb_bailout
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &vm_prefault_nb_bailout
, "");
3000 #if defined (__x86_64__)
3001 extern unsigned int vm_clump_promote_threshold
;
3002 SYSCTL_UINT(_vm
, OID_AUTO
, vm_clump_promote_threshold
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &vm_clump_promote_threshold
, 0, "clump size threshold for promotes");
3003 #if DEVELOPMENT || DEBUG
3004 extern unsigned long vm_clump_stats
[];
3005 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_stats1
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_stats
[1], "free page allocations from clump of 1 page");
3006 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_stats2
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_stats
[2], "free page allocations from clump of 2 pages");
3007 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_stats3
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_stats
[3], "free page allocations from clump of 3 pages");
3008 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_stats4
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_stats
[4], "free page allocations from clump of 4 pages");
3009 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_stats5
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_stats
[5], "free page allocations from clump of 5 pages");
3010 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_stats6
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_stats
[6], "free page allocations from clump of 6 pages");
3011 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_stats7
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_stats
[7], "free page allocations from clump of 7 pages");
3012 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_stats8
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_stats
[8], "free page allocations from clump of 8 pages");
3013 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_stats9
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_stats
[9], "free page allocations from clump of 9 pages");
3014 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_stats10
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_stats
[10], "free page allocations from clump of 10 pages");
3015 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_stats11
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_stats
[11], "free page allocations from clump of 11 pages");
3016 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_stats12
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_stats
[12], "free page allocations from clump of 12 pages");
3017 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_stats13
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_stats
[13], "free page allocations from clump of 13 pages");
3018 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_stats14
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_stats
[14], "free page allocations from clump of 14 pages");
3019 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_stats15
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_stats
[15], "free page allocations from clump of 15 pages");
3020 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_stats16
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_stats
[16], "free page allocations from clump of 16 pages");
3021 extern unsigned long vm_clump_allocs
, vm_clump_inserts
, vm_clump_inrange
, vm_clump_promotes
;
3022 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_alloc
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_allocs
, "free page allocations");
3023 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_inserts
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_inserts
, "free page insertions");
3024 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_inrange
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_inrange
, "free page insertions that are part of vm_pages");
3025 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_promotes
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_promotes
, "pages promoted to head");
3026 #endif /* if DEVELOPMENT || DEBUG */
3027 #endif /* #if defined (__x86_64__) */
3029 #if CONFIG_SECLUDED_MEMORY
3031 SYSCTL_UINT(_vm
, OID_AUTO
, num_tasks_can_use_secluded_mem
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &num_tasks_can_use_secluded_mem
, 0, "");
3032 extern unsigned int vm_page_secluded_target
;
3033 extern unsigned int vm_page_secluded_count
;
3034 extern unsigned int vm_page_secluded_count_free
;
3035 extern unsigned int vm_page_secluded_count_inuse
;
3036 extern unsigned int vm_page_secluded_count_over_target
;
3037 SYSCTL_UINT(_vm
, OID_AUTO
, page_secluded_target
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_secluded_target
, 0, "");
3038 SYSCTL_UINT(_vm
, OID_AUTO
, page_secluded_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_secluded_count
, 0, "");
3039 SYSCTL_UINT(_vm
, OID_AUTO
, page_secluded_count_free
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_secluded_count_free
, 0, "");
3040 SYSCTL_UINT(_vm
, OID_AUTO
, page_secluded_count_inuse
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_secluded_count_inuse
, 0, "");
3041 SYSCTL_UINT(_vm
, OID_AUTO
, page_secluded_count_over_target
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_secluded_count_over_target
, 0, "");
3043 extern struct vm_page_secluded_data vm_page_secluded
;
3044 SYSCTL_UINT(_vm
, OID_AUTO
, page_secluded_eligible
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_secluded
.eligible_for_secluded
, 0, "");
3045 SYSCTL_UINT(_vm
, OID_AUTO
, page_secluded_grab_success_free
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_secluded
.grab_success_free
, 0, "");
3046 SYSCTL_UINT(_vm
, OID_AUTO
, page_secluded_grab_success_other
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_secluded
.grab_success_other
, 0, "");
3047 SYSCTL_UINT(_vm
, OID_AUTO
, page_secluded_grab_failure_locked
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_secluded
.grab_failure_locked
, 0, "");
3048 SYSCTL_UINT(_vm
, OID_AUTO
, page_secluded_grab_failure_state
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_secluded
.grab_failure_state
, 0, "");
3049 SYSCTL_UINT(_vm
, OID_AUTO
, page_secluded_grab_failure_dirty
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_secluded
.grab_failure_dirty
, 0, "");
3050 SYSCTL_UINT(_vm
, OID_AUTO
, page_secluded_grab_for_iokit
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_secluded
.grab_for_iokit
, 0, "");
3051 SYSCTL_UINT(_vm
, OID_AUTO
, page_secluded_grab_for_iokit_success
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_secluded
.grab_for_iokit_success
, 0, "");
3053 #endif /* CONFIG_SECLUDED_MEMORY */
3055 #include <kern/thread.h>
3056 #include <sys/user.h>
3058 void vm_pageout_io_throttle(void);
3061 vm_pageout_io_throttle(void)
3063 struct uthread
*uthread
= get_bsdthread_info(current_thread());
3066 * thread is marked as a low priority I/O type
3067 * and the I/O we issued while in this cleaning operation
3068 * collided with normal I/O operations... we'll
3069 * delay in order to mitigate the impact of this
3070 * task on the normal operation of the system
3073 if (uthread
->uu_lowpri_window
) {
3074 throttle_lowpri_io(1);
3079 vm_pressure_monitor(
3080 __unused
struct proc
*p
,
3081 struct vm_pressure_monitor_args
*uap
,
3085 uint32_t pages_reclaimed
;
3086 uint32_t pages_wanted
;
3088 kr
= mach_vm_pressure_monitor(
3089 (boolean_t
) uap
->wait_for_pressure
,
3090 uap
->nsecs_monitored
,
3091 (uap
->pages_reclaimed
) ? &pages_reclaimed
: NULL
,
3103 if (uap
->pages_reclaimed
) {
3104 if (copyout((void *)&pages_reclaimed
,
3105 uap
->pages_reclaimed
,
3106 sizeof(pages_reclaimed
)) != 0) {
3111 *retval
= (int) pages_wanted
;
3116 kas_info(struct proc
*p
,
3117 struct kas_info_args
*uap
,
3118 int *retval __unused
)
3120 #ifndef CONFIG_KAS_INFO
3124 #else /* CONFIG_KAS_INFO */
3125 int selector
= uap
->selector
;
3126 user_addr_t valuep
= uap
->value
;
3127 user_addr_t sizep
= uap
->size
;
3128 user_size_t size
, rsize
;
3131 if (!kauth_cred_issuser(kauth_cred_get())) {
3136 error
= mac_system_check_kas_info(kauth_cred_get(), selector
);
3142 if (IS_64BIT_PROCESS(p
)) {
3143 user64_size_t size64
;
3144 error
= copyin(sizep
, &size64
, sizeof(size64
));
3145 size
= (user_size_t
)size64
;
3147 user32_size_t size32
;
3148 error
= copyin(sizep
, &size32
, sizeof(size32
));
3149 size
= (user_size_t
)size32
;
3156 case KAS_INFO_KERNEL_TEXT_SLIDE_SELECTOR
:
3158 uint64_t slide
= vm_kernel_slide
;
3160 if (sizeof(slide
) != size
) {
3164 error
= copyout(&slide
, valuep
, sizeof(slide
));
3171 case KAS_INFO_KERNEL_SEGMENT_VMADDR_SELECTOR
:
3174 kernel_mach_header_t
*mh
= &_mh_execute_header
;
3175 struct load_command
*cmd
;
3176 cmd
= (struct load_command
*) &mh
[1];
3178 rsize
= mh
->ncmds
* sizeof(uint64_t);
3181 * Return the size if no data was passed
3191 bases
= kheap_alloc(KHEAP_TEMP
, rsize
, Z_WAITOK
| Z_ZERO
);
3193 for (i
= 0; i
< mh
->ncmds
; i
++) {
3194 if (cmd
->cmd
== LC_SEGMENT_KERNEL
) {
3195 __IGNORE_WCASTALIGN(kernel_segment_command_t
* sg
= (kernel_segment_command_t
*) cmd
);
3196 bases
[i
] = (uint64_t)sg
->vmaddr
;
3198 cmd
= (struct load_command
*) ((uintptr_t) cmd
+ cmd
->cmdsize
);
3201 error
= copyout(bases
, valuep
, rsize
);
3203 kheap_free(KHEAP_TEMP
, bases
, rsize
);
3214 if (IS_64BIT_PROCESS(p
)) {
3215 user64_size_t size64
= (user64_size_t
)rsize
;
3216 error
= copyout(&size64
, sizep
, sizeof(size64
));
3218 user32_size_t size32
= (user32_size_t
)rsize
;
3219 error
= copyout(&size32
, sizep
, sizeof(size32
));
3223 #endif /* CONFIG_KAS_INFO */
3226 #if __has_feature(ptrauth_calls)
3228 * Generate a random pointer signing key that isn't 0.
3231 generate_jop_key(void)
3236 read_random(&key
, sizeof key
);
3240 #endif /* __has_feature(ptrauth_calls) */
3243 #pragma clang diagnostic push
3244 #pragma clang diagnostic ignored "-Wcast-qual"
3245 #pragma clang diagnostic ignored "-Wunused-function"
3250 static_assert(sizeof(vm_min_kernel_address
) == sizeof(unsigned long));
3251 static_assert(sizeof(vm_max_kernel_address
) == sizeof(unsigned long));
3254 SYSCTL_ULONG(_vm
, OID_AUTO
, vm_min_kernel_address
, CTLFLAG_RD
, (unsigned long *) &vm_min_kernel_address
, "");
3255 SYSCTL_ULONG(_vm
, OID_AUTO
, vm_max_kernel_address
, CTLFLAG_RD
, (unsigned long *) &vm_max_kernel_address
, "");
3256 #pragma clang diagnostic pop
3258 extern uint32_t vm_page_pages
;
3259 SYSCTL_UINT(_vm
, OID_AUTO
, pages
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_pages
, 0, "");
3261 extern uint32_t vm_page_busy_absent_skipped
;
3262 SYSCTL_UINT(_vm
, OID_AUTO
, page_busy_absent_skipped
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_busy_absent_skipped
, 0, "");
3264 extern uint32_t vm_page_upl_tainted
;
3265 SYSCTL_UINT(_vm
, OID_AUTO
, upl_pages_tainted
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_upl_tainted
, 0, "");
3267 extern uint32_t vm_page_iopl_tainted
;
3268 SYSCTL_UINT(_vm
, OID_AUTO
, iopl_pages_tainted
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_iopl_tainted
, 0, "");
3270 #if (__arm__ || __arm64__) && (DEVELOPMENT || DEBUG)
3271 extern int vm_footprint_suspend_allowed
;
3272 SYSCTL_INT(_vm
, OID_AUTO
, footprint_suspend_allowed
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &vm_footprint_suspend_allowed
, 0, "");
3274 extern void pmap_footprint_suspend(vm_map_t map
, boolean_t suspend
);
3276 sysctl_vm_footprint_suspend SYSCTL_HANDLER_ARGS
3278 #pragma unused(oidp, arg1, arg2)
3282 if (req
->newptr
== USER_ADDR_NULL
) {
3285 error
= SYSCTL_IN(req
, &new_value
, sizeof(int));
3289 if (!vm_footprint_suspend_allowed
) {
3290 if (new_value
!= 0) {
3291 /* suspends are not allowed... */
3294 /* ... but let resumes proceed */
3296 DTRACE_VM2(footprint_suspend
,
3297 vm_map_t
, current_map(),
3300 pmap_footprint_suspend(current_map(), new_value
);
3304 SYSCTL_PROC(_vm
, OID_AUTO
, footprint_suspend
,
3305 CTLTYPE_INT
| CTLFLAG_WR
| CTLFLAG_ANYBODY
| CTLFLAG_LOCKED
| CTLFLAG_MASKED
,
3306 0, 0, &sysctl_vm_footprint_suspend
, "I", "");
3307 #endif /* (__arm__ || __arm64__) && (DEVELOPMENT || DEBUG) */
3309 extern uint64_t vm_map_corpse_footprint_count
;
3310 extern uint64_t vm_map_corpse_footprint_size_avg
;
3311 extern uint64_t vm_map_corpse_footprint_size_max
;
3312 extern uint64_t vm_map_corpse_footprint_full
;
3313 extern uint64_t vm_map_corpse_footprint_no_buf
;
3314 SYSCTL_QUAD(_vm
, OID_AUTO
, corpse_footprint_count
,
3315 CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_map_corpse_footprint_count
, "");
3316 SYSCTL_QUAD(_vm
, OID_AUTO
, corpse_footprint_size_avg
,
3317 CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_map_corpse_footprint_size_avg
, "");
3318 SYSCTL_QUAD(_vm
, OID_AUTO
, corpse_footprint_size_max
,
3319 CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_map_corpse_footprint_size_max
, "");
3320 SYSCTL_QUAD(_vm
, OID_AUTO
, corpse_footprint_full
,
3321 CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_map_corpse_footprint_full
, "");
3322 SYSCTL_QUAD(_vm
, OID_AUTO
, corpse_footprint_no_buf
,
3323 CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_map_corpse_footprint_no_buf
, "");
3326 extern uint64_t shared_region_pager_copied
;
3327 extern uint64_t shared_region_pager_slid
;
3328 extern uint64_t shared_region_pager_slid_error
;
3329 extern uint64_t shared_region_pager_reclaimed
;
3330 SYSCTL_QUAD(_vm
, OID_AUTO
, shared_region_pager_copied
,
3331 CTLFLAG_RD
| CTLFLAG_LOCKED
, &shared_region_pager_copied
, "");
3332 SYSCTL_QUAD(_vm
, OID_AUTO
, shared_region_pager_slid
,
3333 CTLFLAG_RD
| CTLFLAG_LOCKED
, &shared_region_pager_slid
, "");
3334 SYSCTL_QUAD(_vm
, OID_AUTO
, shared_region_pager_slid_error
,
3335 CTLFLAG_RD
| CTLFLAG_LOCKED
, &shared_region_pager_slid_error
, "");
3336 SYSCTL_QUAD(_vm
, OID_AUTO
, shared_region_pager_reclaimed
,
3337 CTLFLAG_RD
| CTLFLAG_LOCKED
, &shared_region_pager_reclaimed
, "");
3338 extern int shared_region_destroy_delay
;
3339 SYSCTL_INT(_vm
, OID_AUTO
, shared_region_destroy_delay
,
3340 CTLFLAG_RW
| CTLFLAG_LOCKED
, &shared_region_destroy_delay
, 0, "");
3343 extern int pmap_ledgers_panic_leeway
;
3344 SYSCTL_INT(_vm
, OID_AUTO
, pmap_ledgers_panic_leeway
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &pmap_ledgers_panic_leeway
, 0, "");
3345 #endif /* MACH_ASSERT */
3347 extern int vm_protect_privileged_from_untrusted
;
3348 SYSCTL_INT(_vm
, OID_AUTO
, protect_privileged_from_untrusted
,
3349 CTLFLAG_RW
| CTLFLAG_LOCKED
, &vm_protect_privileged_from_untrusted
, 0, "");
3350 extern uint64_t vm_copied_on_read
;
3351 SYSCTL_QUAD(_vm
, OID_AUTO
, copied_on_read
,
3352 CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_copied_on_read
, "");
3354 extern int vm_shared_region_count
;
3355 extern int vm_shared_region_peak
;
3356 SYSCTL_INT(_vm
, OID_AUTO
, shared_region_count
,
3357 CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_shared_region_count
, 0, "");
3358 SYSCTL_INT(_vm
, OID_AUTO
, shared_region_peak
,
3359 CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_shared_region_peak
, 0, "");
3360 #if DEVELOPMENT || DEBUG
3361 extern unsigned int shared_region_pagers_resident_count
;
3362 SYSCTL_INT(_vm
, OID_AUTO
, shared_region_pagers_resident_count
,
3363 CTLFLAG_RD
| CTLFLAG_LOCKED
, &shared_region_pagers_resident_count
, 0, "");
3364 extern unsigned int shared_region_pagers_resident_peak
;
3365 SYSCTL_INT(_vm
, OID_AUTO
, shared_region_pagers_resident_peak
,
3366 CTLFLAG_RD
| CTLFLAG_LOCKED
, &shared_region_pagers_resident_peak
, 0, "");
3367 extern int shared_region_pager_count
;
3368 SYSCTL_INT(_vm
, OID_AUTO
, shared_region_pager_count
,
3369 CTLFLAG_RD
| CTLFLAG_LOCKED
, &shared_region_pager_count
, 0, "");
3370 #if __has_feature(ptrauth_calls)
3371 extern int shared_region_key_count
;
3372 SYSCTL_INT(_vm
, OID_AUTO
, shared_region_key_count
,
3373 CTLFLAG_RD
| CTLFLAG_LOCKED
, &shared_region_key_count
, 0, "");
3374 extern int vm_shared_region_reslide_count
;
3375 SYSCTL_INT(_vm
, OID_AUTO
, shared_region_reslide_count
,
3376 CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_shared_region_reslide_count
, 0, "");
3377 #endif /* __has_feature(ptrauth_calls) */
3378 #endif /* DEVELOPMENT || DEBUG */
3381 extern int debug4k_filter
;
3382 SYSCTL_INT(_vm
, OID_AUTO
, debug4k_filter
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &debug4k_filter
, 0, "");
3383 extern int debug4k_panic_on_terminate
;
3384 SYSCTL_INT(_vm
, OID_AUTO
, debug4k_panic_on_terminate
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &debug4k_panic_on_terminate
, 0, "");
3385 extern int debug4k_panic_on_exception
;
3386 SYSCTL_INT(_vm
, OID_AUTO
, debug4k_panic_on_exception
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &debug4k_panic_on_exception
, 0, "");
3387 extern int debug4k_panic_on_misaligned_sharing
;
3388 SYSCTL_INT(_vm
, OID_AUTO
, debug4k_panic_on_misaligned_sharing
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &debug4k_panic_on_misaligned_sharing
, 0, "");
3389 #endif /* MACH_ASSERT */
3392 * A sysctl which causes all existing shared regions to become stale. They
3393 * will no longer be used by anything new and will be torn down as soon as
3394 * the last existing user exits. A write of non-zero value causes that to happen.
3395 * This should only be used by launchd, so we check that this is initproc.
3398 shared_region_pivot(__unused
struct sysctl_oid
*oidp
, __unused
void *arg1
, __unused
int arg2
, struct sysctl_req
*req
)
3400 unsigned int value
= 0;
3402 int error
= sysctl_io_number(req
, 0, sizeof(value
), &value
, &changed
);
3403 if (error
|| !changed
) {
3406 if (current_proc() != initproc
) {
3410 vm_shared_region_pivot();
3415 SYSCTL_PROC(_vm
, OID_AUTO
, shared_region_pivot
,
3416 CTLTYPE_INT
| CTLFLAG_WR
| CTLFLAG_LOCKED
,
3417 0, 0, shared_region_pivot
, "I", "");
3419 extern int vm_remap_old_path
, vm_remap_new_path
;
3420 SYSCTL_INT(_vm
, OID_AUTO
, remap_old_path
,
3421 CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_remap_old_path
, 0, "");
3422 SYSCTL_INT(_vm
, OID_AUTO
, remap_new_path
,
3423 CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_remap_new_path
, 0, "");