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
,
2056 struct vnode
**scdir_vp
)
2059 struct _sr_file_mappings
*srfmp
;
2060 uint32_t mappings_next
;
2061 struct vnode_attr va
;
2064 vm_prot_t maxprot
= VM_PROT_ALL
;
2068 SHARED_REGION_TRACE_DEBUG(
2069 ("shared_region: %p [%d(%s)] -> map\n",
2070 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2071 p
->p_pid
, p
->p_comm
));
2073 if (files_count
> _SR_FILE_MAPPINGS_MAX_FILES
) {
2077 if (files_count
== 0) {
2081 *sr_file_mappings
= kheap_alloc(KHEAP_TEMP
, files_count
* sizeof(struct _sr_file_mappings
), Z_WAITOK
);
2082 if (*sr_file_mappings
== NULL
) {
2086 bzero(*sr_file_mappings
, files_count
* sizeof(struct _sr_file_mappings
));
2088 for (i
= 0; i
< files_count
; i
++) {
2089 srfmp
= &(*sr_file_mappings
)[i
];
2090 srfmp
->fd
= files
[i
].sf_fd
;
2091 srfmp
->mappings_count
= files
[i
].sf_mappings_count
;
2092 srfmp
->mappings
= &mappings
[mappings_next
];
2093 mappings_next
+= srfmp
->mappings_count
;
2094 if (mappings_next
> mappings_count
) {
2098 srfmp
->slide
= files
[i
].sf_slide
;
2101 if (scdir_enforce
) {
2102 /* get vnode for scdir_path */
2103 error
= vnode_lookup(scdir_path
, 0, scdir_vp
, vfs_context_current());
2105 SHARED_REGION_TRACE_ERROR(
2106 ("shared_region: %p [%d(%s)]: "
2107 "vnode_lookup(%s) failed (error=%d)\n",
2108 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2109 p
->p_pid
, p
->p_comm
,
2110 scdir_path
, error
));
2115 /* get the process's shared region (setup in vm_map_exec()) */
2116 *shared_region
= vm_shared_region_trim_and_get(current_task());
2117 if (*shared_region
== NULL
) {
2118 SHARED_REGION_TRACE_ERROR(
2119 ("shared_region: %p [%d(%s)] map(): "
2120 "no shared region\n",
2121 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2122 p
->p_pid
, p
->p_comm
));
2127 for (srfmp
= &(*sr_file_mappings
)[0];
2128 srfmp
< &(*sr_file_mappings
)[files_count
];
2130 if (srfmp
->mappings_count
== 0) {
2131 /* no mappings here... */
2135 /* get file structure from file descriptor */
2136 error
= fp_get_ftype(p
, srfmp
->fd
, DTYPE_VNODE
, EINVAL
, &srfmp
->fp
);
2138 SHARED_REGION_TRACE_ERROR(
2139 ("shared_region: %p [%d(%s)] map: "
2140 "fd=%d lookup failed (error=%d)\n",
2141 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2142 p
->p_pid
, p
->p_comm
, srfmp
->fd
, error
));
2146 /* we need at least read permission on the file */
2147 if (!(srfmp
->fp
->fp_glob
->fg_flag
& FREAD
)) {
2148 SHARED_REGION_TRACE_ERROR(
2149 ("shared_region: %p [%d(%s)] map: "
2150 "fd=%d not readable\n",
2151 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2152 p
->p_pid
, p
->p_comm
, srfmp
->fd
));
2157 /* get vnode from file structure */
2158 error
= vnode_getwithref((vnode_t
) srfmp
->fp
->fp_glob
->fg_data
);
2160 SHARED_REGION_TRACE_ERROR(
2161 ("shared_region: %p [%d(%s)] map: "
2162 "fd=%d getwithref failed (error=%d)\n",
2163 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2164 p
->p_pid
, p
->p_comm
, srfmp
->fd
, error
));
2167 srfmp
->vp
= (struct vnode
*) srfmp
->fp
->fp_glob
->fg_data
;
2169 /* make sure the vnode is a regular file */
2170 if (srfmp
->vp
->v_type
!= VREG
) {
2171 SHARED_REGION_TRACE_ERROR(
2172 ("shared_region: %p [%d(%s)] map(%p:'%s'): "
2173 "not a file (type=%d)\n",
2174 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2175 p
->p_pid
, p
->p_comm
,
2176 (void *)VM_KERNEL_ADDRPERM(srfmp
->vp
),
2177 srfmp
->vp
->v_name
, srfmp
->vp
->v_type
));
2183 /* pass in 0 for the offset argument because AMFI does not need the offset
2184 * of the shared cache */
2185 error
= mac_file_check_mmap(vfs_context_ucred(vfs_context_current()),
2186 srfmp
->fp
->fp_glob
, VM_PROT_ALL
, MAP_FILE
, 0, &maxprot
);
2192 #if XNU_TARGET_OS_OSX && defined(__arm64__)
2194 * Check if the shared cache is in the trust cache;
2195 * if so, we can skip the root ownership check.
2197 #if DEVELOPMENT || DEBUG
2199 * Skip both root ownership and trust cache check if
2200 * enforcement is disabled.
2202 if (!cs_system_enforcement()) {
2203 goto after_root_check
;
2205 #endif /* DEVELOPMENT || DEBUG */
2206 struct cs_blob
*blob
= csvnode_get_blob(srfmp
->vp
, 0);
2208 SHARED_REGION_TRACE_ERROR(
2209 ("shared_region: %p [%d(%s)] map(%p:'%s'): "
2210 "missing CS blob\n",
2211 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2212 p
->p_pid
, p
->p_comm
,
2213 (void *)VM_KERNEL_ADDRPERM(srfmp
->vp
),
2214 srfmp
->vp
->v_name
));
2217 const uint8_t *cdhash
= csblob_get_cdhash(blob
);
2218 if (cdhash
== NULL
) {
2219 SHARED_REGION_TRACE_ERROR(
2220 ("shared_region: %p [%d(%s)] map(%p:'%s'): "
2222 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2223 p
->p_pid
, p
->p_comm
,
2224 (void *)VM_KERNEL_ADDRPERM(srfmp
->vp
),
2225 srfmp
->vp
->v_name
));
2228 uint32_t result
= pmap_lookup_in_static_trust_cache(cdhash
);
2229 boolean_t in_trust_cache
= result
& (TC_LOOKUP_FOUND
<< TC_LOOKUP_RESULT_SHIFT
);
2230 if (!in_trust_cache
) {
2231 SHARED_REGION_TRACE_ERROR(
2232 ("shared_region: %p [%d(%s)] map(%p:'%s'): "
2233 "not in trust cache\n",
2234 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2235 p
->p_pid
, p
->p_comm
,
2236 (void *)VM_KERNEL_ADDRPERM(srfmp
->vp
),
2237 srfmp
->vp
->v_name
));
2240 goto after_root_check
;
2242 #endif /* XNU_TARGET_OS_OSX && defined(__arm64__) */
2244 /* The shared cache file must be owned by root */
2246 VATTR_WANTED(&va
, va_uid
);
2247 error
= vnode_getattr(srfmp
->vp
, &va
, vfs_context_current());
2249 SHARED_REGION_TRACE_ERROR(
2250 ("shared_region: %p [%d(%s)] map(%p:'%s'): "
2251 "vnode_getattr(%p) failed (error=%d)\n",
2252 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2253 p
->p_pid
, p
->p_comm
,
2254 (void *)VM_KERNEL_ADDRPERM(srfmp
->vp
),
2256 (void *)VM_KERNEL_ADDRPERM(srfmp
->vp
),
2260 if (va
.va_uid
!= 0) {
2261 SHARED_REGION_TRACE_ERROR(
2262 ("shared_region: %p [%d(%s)] map(%p:'%s'): "
2263 "owned by uid=%d instead of 0\n",
2264 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2265 p
->p_pid
, p
->p_comm
,
2266 (void *)VM_KERNEL_ADDRPERM(srfmp
->vp
),
2267 srfmp
->vp
->v_name
, va
.va_uid
));
2272 #if XNU_TARGET_OS_OSX && defined(__arm64__)
2274 #endif /* XNU_TARGET_OS_OSX && defined(__arm64__) */
2277 if (csr_check(CSR_ALLOW_UNRESTRICTED_FS
) != 0) {
2279 VATTR_WANTED(&va
, va_flags
);
2280 error
= vnode_getattr(srfmp
->vp
, &va
, vfs_context_current());
2282 SHARED_REGION_TRACE_ERROR(
2283 ("shared_region: %p [%d(%s)] map(%p:'%s'): "
2284 "vnode_getattr(%p) failed (error=%d)\n",
2285 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2286 p
->p_pid
, p
->p_comm
,
2287 (void *)VM_KERNEL_ADDRPERM(srfmp
->vp
),
2289 (void *)VM_KERNEL_ADDRPERM(srfmp
->vp
),
2294 if (!(va
.va_flags
& SF_RESTRICTED
)) {
2296 * CSR is not configured in CSR_ALLOW_UNRESTRICTED_FS mode, and
2297 * the shared cache file is NOT SIP-protected, so reject the
2300 SHARED_REGION_TRACE_ERROR(
2301 ("shared_region: %p [%d(%s)] map(%p:'%s'), "
2302 "vnode is not SIP-protected. \n",
2303 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2304 p
->p_pid
, p
->p_comm
,
2305 (void *)VM_KERNEL_ADDRPERM(srfmp
->vp
),
2306 srfmp
->vp
->v_name
));
2311 #else /* CONFIG_CSR */
2312 /* Devices without SIP/ROSP need to make sure that the shared cache is on the root volume. */
2314 struct vnode
*root_vp
= p
->p_fd
->fd_rdir
;
2315 if (root_vp
== NULL
) {
2316 root_vp
= rootvnode
;
2318 if (srfmp
->vp
->v_mount
!= root_vp
->v_mount
) {
2319 SHARED_REGION_TRACE_ERROR(
2320 ("shared_region: %p [%d(%s)] map(%p:'%s'): "
2321 "not on process's root volume\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
));
2329 #endif /* CONFIG_CSR */
2331 if (scdir_enforce
) {
2332 /* ensure parent is scdir_vp */
2333 assert(*scdir_vp
!= NULL
);
2334 if (vnode_parent(srfmp
->vp
) != *scdir_vp
) {
2335 SHARED_REGION_TRACE_ERROR(
2336 ("shared_region: %p [%d(%s)] map(%p:'%s'): "
2337 "shared cache file not in %s\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
, scdir_path
));
2347 /* get vnode size */
2348 error
= vnode_size(srfmp
->vp
, &fs
, vfs_context_current());
2350 SHARED_REGION_TRACE_ERROR(
2351 ("shared_region: %p [%d(%s)] map(%p:'%s'): "
2352 "vnode_size(%p) failed (error=%d)\n",
2353 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2354 p
->p_pid
, p
->p_comm
,
2355 (void *)VM_KERNEL_ADDRPERM(srfmp
->vp
),
2357 (void *)VM_KERNEL_ADDRPERM(srfmp
->vp
), error
));
2360 srfmp
->file_size
= fs
;
2362 /* get the file's memory object handle */
2363 srfmp
->file_control
= ubc_getobject(srfmp
->vp
, UBC_HOLDOBJECT
);
2364 if (srfmp
->file_control
== MEMORY_OBJECT_CONTROL_NULL
) {
2365 SHARED_REGION_TRACE_ERROR(
2366 ("shared_region: %p [%d(%s)] map(%p:'%s'): "
2367 "no memory object\n",
2368 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2369 p
->p_pid
, p
->p_comm
,
2370 (void *)VM_KERNEL_ADDRPERM(srfmp
->vp
),
2371 srfmp
->vp
->v_name
));
2376 /* check that the mappings are properly covered by code signatures */
2377 if (!cs_system_enforcement()) {
2378 /* code signing is not enforced: no need to check */
2380 for (i
= 0; i
< srfmp
->mappings_count
; i
++) {
2381 if (srfmp
->mappings
[i
].sms_init_prot
& VM_PROT_ZF
) {
2382 /* zero-filled mapping: not backed by the file */
2385 if (ubc_cs_is_range_codesigned(srfmp
->vp
,
2386 srfmp
->mappings
[i
].sms_file_offset
,
2387 srfmp
->mappings
[i
].sms_size
)) {
2388 /* this mapping is fully covered by code signatures */
2391 SHARED_REGION_TRACE_ERROR(
2392 ("shared_region: %p [%d(%s)] map(%p:'%s'): "
2393 "mapping #%d/%d [0x%llx:0x%llx:0x%llx:0x%x:0x%x] "
2394 "is not code-signed\n",
2395 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2396 p
->p_pid
, p
->p_comm
,
2397 (void *)VM_KERNEL_ADDRPERM(srfmp
->vp
),
2399 i
, srfmp
->mappings_count
,
2400 srfmp
->mappings
[i
].sms_address
,
2401 srfmp
->mappings
[i
].sms_size
,
2402 srfmp
->mappings
[i
].sms_file_offset
,
2403 srfmp
->mappings
[i
].sms_max_prot
,
2404 srfmp
->mappings
[i
].sms_init_prot
));
2412 shared_region_map_and_slide_cleanup(p
, files_count
, *sr_file_mappings
, *shared_region
, *scdir_vp
);
2413 *sr_file_mappings
= NULL
;
2414 *shared_region
= NULL
;
2421 * shared_region_map_np()
2423 * This system call is intended for dyld.
2425 * dyld uses this to map a shared cache file into a shared region.
2426 * This is usually done only the first time a shared cache is needed.
2427 * Subsequent processes will just use the populated shared region without
2428 * requiring any further setup.
2431 _shared_region_map_and_slide(
2433 uint32_t files_count
,
2434 struct shared_file_np
*files
,
2435 uint32_t mappings_count
,
2436 struct shared_file_mapping_slide_np
*mappings
)
2439 kern_return_t kr
= KERN_SUCCESS
;
2440 struct _sr_file_mappings
*sr_file_mappings
= NULL
;
2441 struct vnode
*scdir_vp
= NULL
;
2442 struct vm_shared_region
*shared_region
= NULL
;
2445 * Turn files, mappings into sr_file_mappings and other setup.
2447 error
= shared_region_map_and_slide_setup(p
, files_count
,
2448 files
, mappings_count
, mappings
,
2449 &sr_file_mappings
, &shared_region
, &scdir_vp
);
2454 /* map the file(s) into that shared region's submap */
2455 kr
= vm_shared_region_map_file(shared_region
,
2456 (void *) p
->p_fd
->fd_rdir
,
2459 if (kr
!= KERN_SUCCESS
) {
2460 SHARED_REGION_TRACE_ERROR(("shared_region: %p [%d(%s)] map(): "
2461 "vm_shared_region_map_file() failed kr=0x%x\n",
2462 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2463 p
->p_pid
, p
->p_comm
, kr
));
2466 /* convert kern_return_t to errno */
2471 case KERN_INVALID_ADDRESS
:
2474 case KERN_PROTECTION_FAILURE
:
2481 case KERN_INVALID_ARGUMENT
:
2488 * Mark that this process is now using split libraries.
2490 if (error
== 0 && (p
->p_flag
& P_NOSHLIB
)) {
2491 OSBitAndAtomic(~((uint32_t)P_NOSHLIB
), &p
->p_flag
);
2494 shared_region_map_and_slide_cleanup(p
, files_count
, sr_file_mappings
, shared_region
, scdir_vp
);
2496 SHARED_REGION_TRACE_DEBUG(
2497 ("shared_region: %p [%d(%s)] <- map\n",
2498 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2499 p
->p_pid
, p
->p_comm
));
2505 * Clean up part of _shared_region_map_and_slide()
2506 * It had to be broken out of _shared_region_map_and_slide() to
2507 * prevent compiler inlining from blowing out the stack.
2509 __attribute__((noinline
))
2511 shared_region_map_and_slide_cleanup(
2513 uint32_t files_count
,
2514 struct _sr_file_mappings
*sr_file_mappings
,
2515 struct vm_shared_region
*shared_region
,
2516 struct vnode
*scdir_vp
)
2518 struct _sr_file_mappings
*srfmp
;
2519 struct vnode_attr va
;
2521 if (sr_file_mappings
!= NULL
) {
2522 for (srfmp
= &sr_file_mappings
[0]; srfmp
< &sr_file_mappings
[files_count
]; srfmp
++) {
2523 if (srfmp
->vp
!= NULL
) {
2524 vnode_lock_spin(srfmp
->vp
);
2525 srfmp
->vp
->v_flag
|= VSHARED_DYLD
;
2526 vnode_unlock(srfmp
->vp
);
2528 /* update the vnode's access time */
2529 if (!(vnode_vfsvisflags(srfmp
->vp
) & MNT_NOATIME
)) {
2531 nanotime(&va
.va_access_time
);
2532 VATTR_SET_ACTIVE(&va
, va_access_time
);
2533 vnode_setattr(srfmp
->vp
, &va
, vfs_context_current());
2538 * If the shared cache is compressed, it may
2539 * have a namedstream vnode instantiated for
2540 * for it. That namedstream vnode will also
2541 * have to be marked with VSHARED_DYLD.
2543 if (vnode_hasnamedstreams(srfmp
->vp
)) {
2545 if (vnode_getnamedstream(srfmp
->vp
, &svp
, XATTR_RESOURCEFORK_NAME
,
2546 NS_OPEN
, 0, vfs_context_kernel()) == 0) {
2547 vnode_lock_spin(svp
);
2548 svp
->v_flag
|= VSHARED_DYLD
;
2553 #endif /* NAMEDSTREAMS */
2555 * release the vnode...
2556 * ubc_map() still holds it for us in the non-error case
2558 (void) vnode_put(srfmp
->vp
);
2561 if (srfmp
->fp
!= NULL
) {
2562 /* release the file descriptor */
2563 fp_drop(p
, srfmp
->fd
, srfmp
->fp
, 0);
2567 kheap_free(KHEAP_TEMP
, sr_file_mappings
, files_count
* sizeof(*sr_file_mappings
));
2570 if (scdir_vp
!= NULL
) {
2571 (void)vnode_put(scdir_vp
);
2575 if (shared_region
!= NULL
) {
2576 vm_shared_region_deallocate(shared_region
);
2581 #define SFM_MAX 1024 /* max mapping structs allowed to pass in */
2584 * This interface is used by dyld to map shared caches which are
2585 * for any architecture which doesn't have run time support of pointer
2586 * authentication. Note dyld could also use the new ...map_and_slide_2_np()
2587 * call for this case, however, it just doesn't do that yet.
2590 shared_region_map_and_slide_np(
2592 struct shared_region_map_and_slide_np_args
*uap
,
2593 __unused
int *retvalp
)
2595 unsigned int mappings_count
= uap
->count
;
2597 uint32_t slide
= uap
->slide
;
2598 struct shared_file_np shared_files
[1];
2599 struct shared_file_mapping_np legacy_mapping
;
2600 struct shared_file_mapping_slide_np
*mappings
= NULL
;
2601 kern_return_t kr
= KERN_SUCCESS
;
2603 if ((kr
= vm_shared_region_sliding_valid(slide
)) != KERN_SUCCESS
) {
2604 if (kr
== KERN_INVALID_ARGUMENT
) {
2606 * This will happen if we request sliding again
2607 * with the same slide value that was used earlier
2608 * for the very first sliding.
2615 if (mappings_count
== 0) {
2616 SHARED_REGION_TRACE_INFO(
2617 ("shared_region: %p [%d(%s)] map(): "
2619 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2620 p
->p_pid
, p
->p_comm
));
2621 kr
= 0; /* no mappings: we're done ! */
2623 } else if (mappings_count
<= SFM_MAX
) {
2624 mappings
= kheap_alloc(KHEAP_TEMP
,
2625 mappings_count
* sizeof(mappings
[0]), Z_WAITOK
);
2626 if (mappings
== NULL
) {
2627 kr
= KERN_RESOURCE_SHORTAGE
;
2631 SHARED_REGION_TRACE_ERROR(
2632 ("shared_region: %p [%d(%s)] map(): "
2633 "too many mappings (%d) max %d\n",
2634 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2635 p
->p_pid
, p
->p_comm
,
2636 mappings_count
, SFM_MAX
));
2642 * Read in the mappings and translate to new format.
2644 for (m
= 0; m
< mappings_count
; ++m
) {
2645 user_addr_t from_uaddr
= uap
->mappings
+ (m
* sizeof(struct shared_file_mapping_np
));
2646 kr
= shared_region_copyin(p
, from_uaddr
, 1, sizeof(legacy_mapping
), &legacy_mapping
);
2650 mappings
[m
].sms_address
= legacy_mapping
.sfm_address
;
2651 mappings
[m
].sms_size
= legacy_mapping
.sfm_size
;
2652 mappings
[m
].sms_file_offset
= legacy_mapping
.sfm_file_offset
;
2653 mappings
[m
].sms_max_prot
= legacy_mapping
.sfm_max_prot
;
2654 mappings
[m
].sms_init_prot
= legacy_mapping
.sfm_init_prot
;
2655 mappings
[m
].sms_slide_size
= uap
->slide_size
;
2656 mappings
[m
].sms_slide_start
= uap
->slide_start
;
2659 bzero(shared_files
, sizeof(shared_files
));
2660 shared_files
[0].sf_fd
= uap
->fd
;
2661 shared_files
[0].sf_mappings_count
= mappings_count
;
2662 shared_files
[0].sf_slide
= slide
;
2664 kr
= _shared_region_map_and_slide(p
,
2665 1, /* # of files to map */
2666 &shared_files
[0], /* files to map */
2671 if (mappings
!= NULL
) {
2672 kheap_free(KHEAP_TEMP
, mappings
, mappings_count
* sizeof(mappings
[0]));
2679 * This interface for setting up shared region mappings is what dyld
2680 * uses for shared caches that have __AUTH sections. All other shared
2681 * caches use the non _2 version.
2683 * The slide used for shared regions setup using this interface is done differently
2684 * from the old interface. The slide value passed in the shared_files_np represents
2685 * a max value. The kernel will choose a random value based on that, then use it
2686 * for all shared regions.
2688 #define SLIDE_AMOUNT_MASK ~PAGE_MASK
2691 shared_region_map_and_slide_2_np(
2693 struct shared_region_map_and_slide_2_np_args
*uap
,
2694 __unused
int *retvalp
)
2696 unsigned int files_count
;
2697 struct shared_file_np
*shared_files
= NULL
;
2698 unsigned int mappings_count
;
2699 struct shared_file_mapping_slide_np
*mappings
= NULL
;
2700 kern_return_t kr
= KERN_SUCCESS
;
2701 boolean_t should_slide_mappings
= TRUE
;
2703 files_count
= uap
->files_count
;
2704 mappings_count
= uap
->mappings_count
;
2707 if (files_count
== 0) {
2708 SHARED_REGION_TRACE_INFO(
2709 ("shared_region: %p [%d(%s)] map(): "
2711 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2712 p
->p_pid
, p
->p_comm
));
2713 kr
= 0; /* no files to map: we're done ! */
2715 } else if (files_count
<= _SR_FILE_MAPPINGS_MAX_FILES
) {
2716 shared_files
= kheap_alloc(KHEAP_TEMP
,
2717 files_count
* sizeof(shared_files
[0]), Z_WAITOK
);
2718 if (shared_files
== NULL
) {
2719 kr
= KERN_RESOURCE_SHORTAGE
;
2723 SHARED_REGION_TRACE_ERROR(
2724 ("shared_region: %p [%d(%s)] map(): "
2725 "too many files (%d) max %d\n",
2726 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2727 p
->p_pid
, p
->p_comm
,
2728 files_count
, _SR_FILE_MAPPINGS_MAX_FILES
));
2733 if (mappings_count
== 0) {
2734 SHARED_REGION_TRACE_INFO(
2735 ("shared_region: %p [%d(%s)] map(): "
2737 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2738 p
->p_pid
, p
->p_comm
));
2739 kr
= 0; /* no mappings: we're done ! */
2741 } else if (mappings_count
<= SFM_MAX
) {
2742 mappings
= kheap_alloc(KHEAP_TEMP
,
2743 mappings_count
* sizeof(mappings
[0]), Z_WAITOK
);
2744 if (mappings
== NULL
) {
2745 kr
= KERN_RESOURCE_SHORTAGE
;
2749 SHARED_REGION_TRACE_ERROR(
2750 ("shared_region: %p [%d(%s)] map(): "
2751 "too many mappings (%d) max %d\n",
2752 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2753 p
->p_pid
, p
->p_comm
,
2754 mappings_count
, SFM_MAX
));
2759 kr
= shared_region_copyin(p
, uap
->files
, files_count
, sizeof(shared_files
[0]), shared_files
);
2760 if (kr
!= KERN_SUCCESS
) {
2764 kr
= shared_region_copyin(p
, uap
->mappings
, mappings_count
, sizeof(mappings
[0]), mappings
);
2765 if (kr
!= KERN_SUCCESS
) {
2769 if (should_slide_mappings
) {
2770 uint32_t max_slide
= shared_files
[0].sf_slide
;
2771 uint32_t random_val
;
2772 uint32_t slide_amount
;
2774 if (max_slide
!= 0) {
2775 read_random(&random_val
, sizeof random_val
);
2776 slide_amount
= ((random_val
% max_slide
) & SLIDE_AMOUNT_MASK
);
2782 * Fix up the mappings to reflect the desired slide.
2787 for (f
= 0; f
< files_count
; ++f
) {
2788 shared_files
[f
].sf_slide
= slide_amount
;
2789 for (i
= 0; i
< shared_files
[f
].sf_mappings_count
; ++i
, ++m
) {
2790 if (m
>= mappings_count
) {
2791 SHARED_REGION_TRACE_ERROR(
2792 ("shared_region: %p [%d(%s)] map(): "
2793 "mapping count argument was too small\n",
2794 (void *)VM_KERNEL_ADDRPERM(current_thread()),
2795 p
->p_pid
, p
->p_comm
));
2799 mappings
[m
].sms_address
+= slide_amount
;
2800 if (mappings
[m
].sms_slide_size
!= 0) {
2801 mappings
[i
].sms_slide_start
+= slide_amount
;
2806 kr
= _shared_region_map_and_slide(p
, files_count
, shared_files
, mappings_count
, mappings
);
2808 if (shared_files
!= NULL
) {
2809 kheap_free(KHEAP_TEMP
, shared_files
, files_count
* sizeof(shared_files
[0]));
2810 shared_files
= NULL
;
2812 if (mappings
!= NULL
) {
2813 kheap_free(KHEAP_TEMP
, mappings
, mappings_count
* sizeof(mappings
[0]));
2819 /* sysctl overflow room */
2821 SYSCTL_INT(_vm
, OID_AUTO
, pagesize
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2822 (int *) &page_size
, 0, "vm page size");
2824 /* vm_page_free_target is provided as a makeshift solution for applications that want to
2825 * allocate buffer space, possibly purgeable memory, but not cause inactive pages to be
2826 * reclaimed. It allows the app to calculate how much memory is free outside the free target. */
2827 extern unsigned int vm_page_free_target
;
2828 SYSCTL_INT(_vm
, OID_AUTO
, vm_page_free_target
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2829 &vm_page_free_target
, 0, "Pageout daemon free target");
2831 SYSCTL_INT(_vm
, OID_AUTO
, memory_pressure
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2832 &vm_pageout_state
.vm_memory_pressure
, 0, "Memory pressure indicator");
2835 vm_ctl_page_free_wanted SYSCTL_HANDLER_ARGS
2837 #pragma unused(oidp, arg1, arg2)
2838 unsigned int page_free_wanted
;
2840 page_free_wanted
= mach_vm_ctl_page_free_wanted();
2841 return SYSCTL_OUT(req
, &page_free_wanted
, sizeof(page_free_wanted
));
2843 SYSCTL_PROC(_vm
, OID_AUTO
, page_free_wanted
,
2844 CTLTYPE_INT
| CTLFLAG_RD
| CTLFLAG_LOCKED
,
2845 0, 0, vm_ctl_page_free_wanted
, "I", "");
2847 extern unsigned int vm_page_purgeable_count
;
2848 SYSCTL_INT(_vm
, OID_AUTO
, page_purgeable_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2849 &vm_page_purgeable_count
, 0, "Purgeable page count");
2851 extern unsigned int vm_page_purgeable_wired_count
;
2852 SYSCTL_INT(_vm
, OID_AUTO
, page_purgeable_wired_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2853 &vm_page_purgeable_wired_count
, 0, "Wired purgeable page count");
2855 extern unsigned int vm_page_kern_lpage_count
;
2856 SYSCTL_INT(_vm
, OID_AUTO
, kern_lpage_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2857 &vm_page_kern_lpage_count
, 0, "kernel used large pages");
2859 #if DEVELOPMENT || DEBUG
2860 #if __ARM_MIXED_PAGE_SIZE__
2861 static int vm_mixed_pagesize_supported
= 1;
2863 static int vm_mixed_pagesize_supported
= 0;
2864 #endif /*__ARM_MIXED_PAGE_SIZE__ */
2865 SYSCTL_INT(_debug
, OID_AUTO
, vm_mixed_pagesize_supported
, CTLFLAG_ANYBODY
| CTLFLAG_RD
| CTLFLAG_LOCKED
,
2866 &vm_mixed_pagesize_supported
, 0, "kernel support for mixed pagesize");
2869 extern uint64_t get_pages_grabbed_count(void);
2872 pages_grabbed SYSCTL_HANDLER_ARGS
2874 #pragma unused(arg1, arg2, oidp)
2875 uint64_t value
= get_pages_grabbed_count();
2876 return SYSCTL_OUT(req
, &value
, sizeof(value
));
2879 SYSCTL_PROC(_vm
, OID_AUTO
, pages_grabbed
, CTLTYPE_QUAD
| CTLFLAG_RD
| CTLFLAG_LOCKED
,
2880 0, 0, &pages_grabbed
, "QU", "Total pages grabbed");
2881 SYSCTL_ULONG(_vm
, OID_AUTO
, pages_freed
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2882 &vm_pageout_vminfo
.vm_page_pages_freed
, "Total pages freed");
2884 SYSCTL_INT(_vm
, OID_AUTO
, pageout_purged_objects
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2885 &vm_pageout_debug
.vm_pageout_purged_objects
, 0, "System purged object count");
2886 SYSCTL_UINT(_vm
, OID_AUTO
, pageout_cleaned_busy
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2887 &vm_pageout_debug
.vm_pageout_cleaned_busy
, 0, "Cleaned pages busy (deactivated)");
2888 SYSCTL_UINT(_vm
, OID_AUTO
, pageout_cleaned_nolock
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2889 &vm_pageout_debug
.vm_pageout_cleaned_nolock
, 0, "Cleaned pages no-lock (deactivated)");
2891 SYSCTL_UINT(_vm
, OID_AUTO
, pageout_cleaned_volatile_reactivated
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2892 &vm_pageout_debug
.vm_pageout_cleaned_volatile_reactivated
, 0, "Cleaned pages volatile reactivated");
2893 SYSCTL_UINT(_vm
, OID_AUTO
, pageout_cleaned_fault_reactivated
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2894 &vm_pageout_debug
.vm_pageout_cleaned_fault_reactivated
, 0, "Cleaned pages fault reactivated");
2895 SYSCTL_UINT(_vm
, OID_AUTO
, pageout_cleaned_reactivated
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2896 &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 */
2897 SYSCTL_ULONG(_vm
, OID_AUTO
, pageout_cleaned
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2898 &vm_pageout_vminfo
.vm_pageout_freed_cleaned
, "Cleaned pages freed");
2899 SYSCTL_UINT(_vm
, OID_AUTO
, pageout_cleaned_reference_reactivated
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2900 &vm_pageout_debug
.vm_pageout_cleaned_reference_reactivated
, 0, "Cleaned pages reference reactivated");
2901 SYSCTL_UINT(_vm
, OID_AUTO
, pageout_enqueued_cleaned
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2902 &vm_pageout_debug
.vm_pageout_enqueued_cleaned
, 0, ""); /* sum of next two */
2903 #endif /* DEVELOPMENT || DEBUG */
2905 extern int madvise_free_debug
;
2906 SYSCTL_INT(_vm
, OID_AUTO
, madvise_free_debug
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
2907 &madvise_free_debug
, 0, "zero-fill on madvise(MADV_FREE*)");
2909 SYSCTL_INT(_vm
, OID_AUTO
, page_reusable_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2910 &vm_page_stats_reusable
.reusable_count
, 0, "Reusable page count");
2911 SYSCTL_QUAD(_vm
, OID_AUTO
, reusable_success
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2912 &vm_page_stats_reusable
.reusable_pages_success
, "");
2913 SYSCTL_QUAD(_vm
, OID_AUTO
, reusable_failure
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2914 &vm_page_stats_reusable
.reusable_pages_failure
, "");
2915 SYSCTL_QUAD(_vm
, OID_AUTO
, reusable_pages_shared
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2916 &vm_page_stats_reusable
.reusable_pages_shared
, "");
2917 SYSCTL_QUAD(_vm
, OID_AUTO
, all_reusable_calls
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2918 &vm_page_stats_reusable
.all_reusable_calls
, "");
2919 SYSCTL_QUAD(_vm
, OID_AUTO
, partial_reusable_calls
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2920 &vm_page_stats_reusable
.partial_reusable_calls
, "");
2921 SYSCTL_QUAD(_vm
, OID_AUTO
, reuse_success
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2922 &vm_page_stats_reusable
.reuse_pages_success
, "");
2923 SYSCTL_QUAD(_vm
, OID_AUTO
, reuse_failure
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2924 &vm_page_stats_reusable
.reuse_pages_failure
, "");
2925 SYSCTL_QUAD(_vm
, OID_AUTO
, all_reuse_calls
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2926 &vm_page_stats_reusable
.all_reuse_calls
, "");
2927 SYSCTL_QUAD(_vm
, OID_AUTO
, partial_reuse_calls
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2928 &vm_page_stats_reusable
.partial_reuse_calls
, "");
2929 SYSCTL_QUAD(_vm
, OID_AUTO
, can_reuse_success
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2930 &vm_page_stats_reusable
.can_reuse_success
, "");
2931 SYSCTL_QUAD(_vm
, OID_AUTO
, can_reuse_failure
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2932 &vm_page_stats_reusable
.can_reuse_failure
, "");
2933 SYSCTL_QUAD(_vm
, OID_AUTO
, reusable_reclaimed
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2934 &vm_page_stats_reusable
.reusable_reclaimed
, "");
2935 SYSCTL_QUAD(_vm
, OID_AUTO
, reusable_nonwritable
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2936 &vm_page_stats_reusable
.reusable_nonwritable
, "");
2937 SYSCTL_QUAD(_vm
, OID_AUTO
, reusable_shared
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2938 &vm_page_stats_reusable
.reusable_shared
, "");
2939 SYSCTL_QUAD(_vm
, OID_AUTO
, free_shared
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
2940 &vm_page_stats_reusable
.free_shared
, "");
2943 extern unsigned int vm_page_free_count
, vm_page_speculative_count
;
2944 SYSCTL_UINT(_vm
, OID_AUTO
, page_free_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_free_count
, 0, "");
2945 SYSCTL_UINT(_vm
, OID_AUTO
, page_speculative_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_speculative_count
, 0, "");
2947 extern unsigned int vm_page_cleaned_count
;
2948 SYSCTL_UINT(_vm
, OID_AUTO
, page_cleaned_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_cleaned_count
, 0, "Cleaned queue size");
2950 extern unsigned int vm_page_pageable_internal_count
, vm_page_pageable_external_count
;
2951 SYSCTL_UINT(_vm
, OID_AUTO
, page_pageable_internal_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_pageable_internal_count
, 0, "");
2952 SYSCTL_UINT(_vm
, OID_AUTO
, page_pageable_external_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_pageable_external_count
, 0, "");
2954 /* pageout counts */
2955 SYSCTL_UINT(_vm
, OID_AUTO
, pageout_inactive_clean
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_pageout_state
.vm_pageout_inactive_clean
, 0, "");
2956 SYSCTL_UINT(_vm
, OID_AUTO
, pageout_inactive_used
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_pageout_state
.vm_pageout_inactive_used
, 0, "");
2958 SYSCTL_ULONG(_vm
, OID_AUTO
, pageout_inactive_dirty_internal
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_pageout_vminfo
.vm_pageout_inactive_dirty_internal
, "");
2959 SYSCTL_ULONG(_vm
, OID_AUTO
, pageout_inactive_dirty_external
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_pageout_vminfo
.vm_pageout_inactive_dirty_external
, "");
2960 SYSCTL_ULONG(_vm
, OID_AUTO
, pageout_speculative_clean
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_pageout_vminfo
.vm_pageout_freed_speculative
, "");
2961 SYSCTL_ULONG(_vm
, OID_AUTO
, pageout_freed_external
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_pageout_vminfo
.vm_pageout_freed_external
, "");
2962 SYSCTL_ULONG(_vm
, OID_AUTO
, pageout_freed_speculative
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_pageout_vminfo
.vm_pageout_freed_speculative
, "");
2963 SYSCTL_ULONG(_vm
, OID_AUTO
, pageout_freed_cleaned
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_pageout_vminfo
.vm_pageout_freed_cleaned
, "");
2966 /* counts of pages prefaulted when entering a memory object */
2967 extern int64_t vm_prefault_nb_pages
, vm_prefault_nb_bailout
;
2968 SYSCTL_QUAD(_vm
, OID_AUTO
, prefault_nb_pages
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &vm_prefault_nb_pages
, "");
2969 SYSCTL_QUAD(_vm
, OID_AUTO
, prefault_nb_bailout
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &vm_prefault_nb_bailout
, "");
2971 #if defined (__x86_64__)
2972 extern unsigned int vm_clump_promote_threshold
;
2973 SYSCTL_UINT(_vm
, OID_AUTO
, vm_clump_promote_threshold
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &vm_clump_promote_threshold
, 0, "clump size threshold for promotes");
2974 #if DEVELOPMENT || DEBUG
2975 extern unsigned long vm_clump_stats
[];
2976 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_stats1
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_stats
[1], "free page allocations from clump of 1 page");
2977 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_stats2
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_stats
[2], "free page allocations from clump of 2 pages");
2978 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_stats3
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_stats
[3], "free page allocations from clump of 3 pages");
2979 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_stats4
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_stats
[4], "free page allocations from clump of 4 pages");
2980 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_stats5
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_stats
[5], "free page allocations from clump of 5 pages");
2981 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_stats6
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_stats
[6], "free page allocations from clump of 6 pages");
2982 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_stats7
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_stats
[7], "free page allocations from clump of 7 pages");
2983 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_stats8
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_stats
[8], "free page allocations from clump of 8 pages");
2984 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_stats9
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_stats
[9], "free page allocations from clump of 9 pages");
2985 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_stats10
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_stats
[10], "free page allocations from clump of 10 pages");
2986 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_stats11
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_stats
[11], "free page allocations from clump of 11 pages");
2987 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_stats12
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_stats
[12], "free page allocations from clump of 12 pages");
2988 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_stats13
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_stats
[13], "free page allocations from clump of 13 pages");
2989 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_stats14
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_stats
[14], "free page allocations from clump of 14 pages");
2990 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_stats15
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_stats
[15], "free page allocations from clump of 15 pages");
2991 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_stats16
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_stats
[16], "free page allocations from clump of 16 pages");
2992 extern unsigned long vm_clump_allocs
, vm_clump_inserts
, vm_clump_inrange
, vm_clump_promotes
;
2993 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_alloc
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_allocs
, "free page allocations");
2994 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_inserts
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_inserts
, "free page insertions");
2995 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_inrange
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_inrange
, "free page insertions that are part of vm_pages");
2996 SYSCTL_LONG(_vm
, OID_AUTO
, vm_clump_promotes
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_clump_promotes
, "pages promoted to head");
2997 #endif /* if DEVELOPMENT || DEBUG */
2998 #endif /* #if defined (__x86_64__) */
3000 #if CONFIG_SECLUDED_MEMORY
3002 SYSCTL_UINT(_vm
, OID_AUTO
, num_tasks_can_use_secluded_mem
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &num_tasks_can_use_secluded_mem
, 0, "");
3003 extern unsigned int vm_page_secluded_target
;
3004 extern unsigned int vm_page_secluded_count
;
3005 extern unsigned int vm_page_secluded_count_free
;
3006 extern unsigned int vm_page_secluded_count_inuse
;
3007 extern unsigned int vm_page_secluded_count_over_target
;
3008 SYSCTL_UINT(_vm
, OID_AUTO
, page_secluded_target
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_secluded_target
, 0, "");
3009 SYSCTL_UINT(_vm
, OID_AUTO
, page_secluded_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_secluded_count
, 0, "");
3010 SYSCTL_UINT(_vm
, OID_AUTO
, page_secluded_count_free
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_secluded_count_free
, 0, "");
3011 SYSCTL_UINT(_vm
, OID_AUTO
, page_secluded_count_inuse
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_secluded_count_inuse
, 0, "");
3012 SYSCTL_UINT(_vm
, OID_AUTO
, page_secluded_count_over_target
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_secluded_count_over_target
, 0, "");
3014 extern struct vm_page_secluded_data vm_page_secluded
;
3015 SYSCTL_UINT(_vm
, OID_AUTO
, page_secluded_eligible
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_secluded
.eligible_for_secluded
, 0, "");
3016 SYSCTL_UINT(_vm
, OID_AUTO
, page_secluded_grab_success_free
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_secluded
.grab_success_free
, 0, "");
3017 SYSCTL_UINT(_vm
, OID_AUTO
, page_secluded_grab_success_other
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_secluded
.grab_success_other
, 0, "");
3018 SYSCTL_UINT(_vm
, OID_AUTO
, page_secluded_grab_failure_locked
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_secluded
.grab_failure_locked
, 0, "");
3019 SYSCTL_UINT(_vm
, OID_AUTO
, page_secluded_grab_failure_state
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_secluded
.grab_failure_state
, 0, "");
3020 SYSCTL_UINT(_vm
, OID_AUTO
, page_secluded_grab_failure_dirty
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_secluded
.grab_failure_dirty
, 0, "");
3021 SYSCTL_UINT(_vm
, OID_AUTO
, page_secluded_grab_for_iokit
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_secluded
.grab_for_iokit
, 0, "");
3022 SYSCTL_UINT(_vm
, OID_AUTO
, page_secluded_grab_for_iokit_success
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_secluded
.grab_for_iokit_success
, 0, "");
3024 #endif /* CONFIG_SECLUDED_MEMORY */
3026 #include <kern/thread.h>
3027 #include <sys/user.h>
3029 void vm_pageout_io_throttle(void);
3032 vm_pageout_io_throttle(void)
3034 struct uthread
*uthread
= get_bsdthread_info(current_thread());
3037 * thread is marked as a low priority I/O type
3038 * and the I/O we issued while in this cleaning operation
3039 * collided with normal I/O operations... we'll
3040 * delay in order to mitigate the impact of this
3041 * task on the normal operation of the system
3044 if (uthread
->uu_lowpri_window
) {
3045 throttle_lowpri_io(1);
3050 vm_pressure_monitor(
3051 __unused
struct proc
*p
,
3052 struct vm_pressure_monitor_args
*uap
,
3056 uint32_t pages_reclaimed
;
3057 uint32_t pages_wanted
;
3059 kr
= mach_vm_pressure_monitor(
3060 (boolean_t
) uap
->wait_for_pressure
,
3061 uap
->nsecs_monitored
,
3062 (uap
->pages_reclaimed
) ? &pages_reclaimed
: NULL
,
3074 if (uap
->pages_reclaimed
) {
3075 if (copyout((void *)&pages_reclaimed
,
3076 uap
->pages_reclaimed
,
3077 sizeof(pages_reclaimed
)) != 0) {
3082 *retval
= (int) pages_wanted
;
3087 kas_info(struct proc
*p
,
3088 struct kas_info_args
*uap
,
3089 int *retval __unused
)
3091 #ifndef CONFIG_KAS_INFO
3095 #else /* CONFIG_KAS_INFO */
3096 int selector
= uap
->selector
;
3097 user_addr_t valuep
= uap
->value
;
3098 user_addr_t sizep
= uap
->size
;
3099 user_size_t size
, rsize
;
3102 if (!kauth_cred_issuser(kauth_cred_get())) {
3107 error
= mac_system_check_kas_info(kauth_cred_get(), selector
);
3113 if (IS_64BIT_PROCESS(p
)) {
3114 user64_size_t size64
;
3115 error
= copyin(sizep
, &size64
, sizeof(size64
));
3116 size
= (user_size_t
)size64
;
3118 user32_size_t size32
;
3119 error
= copyin(sizep
, &size32
, sizeof(size32
));
3120 size
= (user_size_t
)size32
;
3127 case KAS_INFO_KERNEL_TEXT_SLIDE_SELECTOR
:
3129 uint64_t slide
= vm_kernel_slide
;
3131 if (sizeof(slide
) != size
) {
3135 error
= copyout(&slide
, valuep
, sizeof(slide
));
3142 case KAS_INFO_KERNEL_SEGMENT_VMADDR_SELECTOR
:
3145 kernel_mach_header_t
*mh
= &_mh_execute_header
;
3146 struct load_command
*cmd
;
3147 cmd
= (struct load_command
*) &mh
[1];
3149 rsize
= mh
->ncmds
* sizeof(uint64_t);
3152 * Return the size if no data was passed
3162 bases
= kheap_alloc(KHEAP_TEMP
, rsize
, Z_WAITOK
| Z_ZERO
);
3164 for (i
= 0; i
< mh
->ncmds
; i
++) {
3165 if (cmd
->cmd
== LC_SEGMENT_KERNEL
) {
3166 __IGNORE_WCASTALIGN(kernel_segment_command_t
* sg
= (kernel_segment_command_t
*) cmd
);
3167 bases
[i
] = (uint64_t)sg
->vmaddr
;
3169 cmd
= (struct load_command
*) ((uintptr_t) cmd
+ cmd
->cmdsize
);
3172 error
= copyout(bases
, valuep
, rsize
);
3174 kheap_free(KHEAP_TEMP
, bases
, rsize
);
3185 if (IS_64BIT_PROCESS(p
)) {
3186 user64_size_t size64
= (user64_size_t
)rsize
;
3187 error
= copyout(&size64
, sizep
, sizeof(size64
));
3189 user32_size_t size32
= (user32_size_t
)rsize
;
3190 error
= copyout(&size32
, sizep
, sizeof(size32
));
3194 #endif /* CONFIG_KAS_INFO */
3197 #if __has_feature(ptrauth_calls)
3199 * Generate a random pointer signing key that isn't 0.
3202 generate_jop_key(void)
3207 read_random(&key
, sizeof key
);
3211 #endif /* __has_feature(ptrauth_calls) */
3214 #pragma clang diagnostic push
3215 #pragma clang diagnostic ignored "-Wcast-qual"
3216 #pragma clang diagnostic ignored "-Wunused-function"
3221 static_assert(sizeof(vm_min_kernel_address
) == sizeof(unsigned long));
3222 static_assert(sizeof(vm_max_kernel_address
) == sizeof(unsigned long));
3225 SYSCTL_ULONG(_vm
, OID_AUTO
, vm_min_kernel_address
, CTLFLAG_RD
, (unsigned long *) &vm_min_kernel_address
, "");
3226 SYSCTL_ULONG(_vm
, OID_AUTO
, vm_max_kernel_address
, CTLFLAG_RD
, (unsigned long *) &vm_max_kernel_address
, "");
3227 #pragma clang diagnostic pop
3229 extern uint32_t vm_page_pages
;
3230 SYSCTL_UINT(_vm
, OID_AUTO
, pages
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_pages
, 0, "");
3232 extern uint32_t vm_page_busy_absent_skipped
;
3233 SYSCTL_UINT(_vm
, OID_AUTO
, page_busy_absent_skipped
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_busy_absent_skipped
, 0, "");
3235 extern uint32_t vm_page_upl_tainted
;
3236 SYSCTL_UINT(_vm
, OID_AUTO
, upl_pages_tainted
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_upl_tainted
, 0, "");
3238 extern uint32_t vm_page_iopl_tainted
;
3239 SYSCTL_UINT(_vm
, OID_AUTO
, iopl_pages_tainted
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_page_iopl_tainted
, 0, "");
3241 #if (__arm__ || __arm64__) && (DEVELOPMENT || DEBUG)
3242 extern int vm_footprint_suspend_allowed
;
3243 SYSCTL_INT(_vm
, OID_AUTO
, footprint_suspend_allowed
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &vm_footprint_suspend_allowed
, 0, "");
3245 extern void pmap_footprint_suspend(vm_map_t map
, boolean_t suspend
);
3247 sysctl_vm_footprint_suspend SYSCTL_HANDLER_ARGS
3249 #pragma unused(oidp, arg1, arg2)
3253 if (req
->newptr
== USER_ADDR_NULL
) {
3256 error
= SYSCTL_IN(req
, &new_value
, sizeof(int));
3260 if (!vm_footprint_suspend_allowed
) {
3261 if (new_value
!= 0) {
3262 /* suspends are not allowed... */
3265 /* ... but let resumes proceed */
3267 DTRACE_VM2(footprint_suspend
,
3268 vm_map_t
, current_map(),
3271 pmap_footprint_suspend(current_map(), new_value
);
3275 SYSCTL_PROC(_vm
, OID_AUTO
, footprint_suspend
,
3276 CTLTYPE_INT
| CTLFLAG_WR
| CTLFLAG_ANYBODY
| CTLFLAG_LOCKED
| CTLFLAG_MASKED
,
3277 0, 0, &sysctl_vm_footprint_suspend
, "I", "");
3278 #endif /* (__arm__ || __arm64__) && (DEVELOPMENT || DEBUG) */
3280 extern uint64_t vm_map_corpse_footprint_count
;
3281 extern uint64_t vm_map_corpse_footprint_size_avg
;
3282 extern uint64_t vm_map_corpse_footprint_size_max
;
3283 extern uint64_t vm_map_corpse_footprint_full
;
3284 extern uint64_t vm_map_corpse_footprint_no_buf
;
3285 SYSCTL_QUAD(_vm
, OID_AUTO
, corpse_footprint_count
,
3286 CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_map_corpse_footprint_count
, "");
3287 SYSCTL_QUAD(_vm
, OID_AUTO
, corpse_footprint_size_avg
,
3288 CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_map_corpse_footprint_size_avg
, "");
3289 SYSCTL_QUAD(_vm
, OID_AUTO
, corpse_footprint_size_max
,
3290 CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_map_corpse_footprint_size_max
, "");
3291 SYSCTL_QUAD(_vm
, OID_AUTO
, corpse_footprint_full
,
3292 CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_map_corpse_footprint_full
, "");
3293 SYSCTL_QUAD(_vm
, OID_AUTO
, corpse_footprint_no_buf
,
3294 CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_map_corpse_footprint_no_buf
, "");
3297 extern uint64_t vm_cs_defer_to_pmap_cs
;
3298 extern uint64_t vm_cs_defer_to_pmap_cs_not
;
3299 SYSCTL_QUAD(_vm
, OID_AUTO
, cs_defer_to_pmap_cs
,
3300 CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_cs_defer_to_pmap_cs
, "");
3301 SYSCTL_QUAD(_vm
, OID_AUTO
, cs_defer_to_pmap_cs_not
,
3302 CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_cs_defer_to_pmap_cs_not
, "");
3303 #endif /* PMAP_CS */
3305 extern uint64_t shared_region_pager_copied
;
3306 extern uint64_t shared_region_pager_slid
;
3307 extern uint64_t shared_region_pager_slid_error
;
3308 extern uint64_t shared_region_pager_reclaimed
;
3309 SYSCTL_QUAD(_vm
, OID_AUTO
, shared_region_pager_copied
,
3310 CTLFLAG_RD
| CTLFLAG_LOCKED
, &shared_region_pager_copied
, "");
3311 SYSCTL_QUAD(_vm
, OID_AUTO
, shared_region_pager_slid
,
3312 CTLFLAG_RD
| CTLFLAG_LOCKED
, &shared_region_pager_slid
, "");
3313 SYSCTL_QUAD(_vm
, OID_AUTO
, shared_region_pager_slid_error
,
3314 CTLFLAG_RD
| CTLFLAG_LOCKED
, &shared_region_pager_slid_error
, "");
3315 SYSCTL_QUAD(_vm
, OID_AUTO
, shared_region_pager_reclaimed
,
3316 CTLFLAG_RD
| CTLFLAG_LOCKED
, &shared_region_pager_reclaimed
, "");
3317 extern int shared_region_destroy_delay
;
3318 SYSCTL_INT(_vm
, OID_AUTO
, shared_region_destroy_delay
,
3319 CTLFLAG_RW
| CTLFLAG_LOCKED
, &shared_region_destroy_delay
, 0, "");
3322 extern int pmap_ledgers_panic_leeway
;
3323 SYSCTL_INT(_vm
, OID_AUTO
, pmap_ledgers_panic_leeway
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &pmap_ledgers_panic_leeway
, 0, "");
3324 #endif /* MACH_ASSERT */
3326 extern int vm_protect_privileged_from_untrusted
;
3327 SYSCTL_INT(_vm
, OID_AUTO
, protect_privileged_from_untrusted
,
3328 CTLFLAG_RW
| CTLFLAG_LOCKED
, &vm_protect_privileged_from_untrusted
, 0, "");
3329 extern uint64_t vm_copied_on_read
;
3330 SYSCTL_QUAD(_vm
, OID_AUTO
, copied_on_read
,
3331 CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_copied_on_read
, "");
3333 extern int vm_shared_region_count
;
3334 extern int vm_shared_region_peak
;
3335 SYSCTL_INT(_vm
, OID_AUTO
, shared_region_count
,
3336 CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_shared_region_count
, 0, "");
3337 SYSCTL_INT(_vm
, OID_AUTO
, shared_region_peak
,
3338 CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_shared_region_peak
, 0, "");
3339 #if DEVELOPMENT || DEBUG
3340 extern unsigned int shared_region_pagers_resident_count
;
3341 SYSCTL_INT(_vm
, OID_AUTO
, shared_region_pagers_resident_count
,
3342 CTLFLAG_RD
| CTLFLAG_LOCKED
, &shared_region_pagers_resident_count
, 0, "");
3343 extern unsigned int shared_region_pagers_resident_peak
;
3344 SYSCTL_INT(_vm
, OID_AUTO
, shared_region_pagers_resident_peak
,
3345 CTLFLAG_RD
| CTLFLAG_LOCKED
, &shared_region_pagers_resident_peak
, 0, "");
3346 extern int shared_region_pager_count
;
3347 SYSCTL_INT(_vm
, OID_AUTO
, shared_region_pager_count
,
3348 CTLFLAG_RD
| CTLFLAG_LOCKED
, &shared_region_pager_count
, 0, "");
3349 #if __has_feature(ptrauth_calls)
3350 extern int shared_region_key_count
;
3351 SYSCTL_INT(_vm
, OID_AUTO
, shared_region_key_count
,
3352 CTLFLAG_RD
| CTLFLAG_LOCKED
, &shared_region_key_count
, 0, "");
3353 extern int vm_shared_region_reslide_count
;
3354 SYSCTL_INT(_vm
, OID_AUTO
, shared_region_reslide_count
,
3355 CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_shared_region_reslide_count
, 0, "");
3356 #endif /* __has_feature(ptrauth_calls) */
3357 #endif /* DEVELOPMENT || DEBUG */
3360 extern int debug4k_filter
;
3361 SYSCTL_INT(_vm
, OID_AUTO
, debug4k_filter
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &debug4k_filter
, 0, "");
3362 extern int debug4k_panic_on_terminate
;
3363 SYSCTL_INT(_vm
, OID_AUTO
, debug4k_panic_on_terminate
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &debug4k_panic_on_terminate
, 0, "");
3364 extern int debug4k_panic_on_exception
;
3365 SYSCTL_INT(_vm
, OID_AUTO
, debug4k_panic_on_exception
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &debug4k_panic_on_exception
, 0, "");
3366 extern int debug4k_panic_on_misaligned_sharing
;
3367 SYSCTL_INT(_vm
, OID_AUTO
, debug4k_panic_on_misaligned_sharing
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &debug4k_panic_on_misaligned_sharing
, 0, "");
3368 #endif /* MACH_ASSERT */
3371 * A sysctl which causes all existing shared regions to become stale. They
3372 * will no longer be used by anything new and will be torn down as soon as
3373 * the last existing user exits. A write of non-zero value causes that to happen.
3374 * This should only be used by launchd, so we check that this is initproc.
3377 shared_region_pivot(__unused
struct sysctl_oid
*oidp
, __unused
void *arg1
, __unused
int arg2
, struct sysctl_req
*req
)
3379 unsigned int value
= 0;
3381 int error
= sysctl_io_number(req
, 0, sizeof(value
), &value
, &changed
);
3382 if (error
|| !changed
) {
3385 if (current_proc() != initproc
) {
3389 vm_shared_region_pivot();
3394 SYSCTL_PROC(_vm
, OID_AUTO
, shared_region_pivot
,
3395 CTLTYPE_INT
| CTLFLAG_WR
| CTLFLAG_LOCKED
,
3396 0, 0, shared_region_pivot
, "I", "");
3398 extern int vm_remap_old_path
, vm_remap_new_path
;
3399 SYSCTL_INT(_vm
, OID_AUTO
, remap_old_path
,
3400 CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_remap_old_path
, 0, "");
3401 SYSCTL_INT(_vm
, OID_AUTO
, remap_new_path
,
3402 CTLFLAG_RD
| CTLFLAG_LOCKED
, &vm_remap_new_path
, 0, "");